Commit f2ee2b6c authored by Tobias Tebbi's avatar Tobias Tebbi Committed by Commit Bot

[torque] Allow type inference for local declarations

Bug: v8:7793
Change-Id: If304608a284edb09e0a19171bbb56645714f6c32
Reviewed-on: https://chromium-review.googlesource.com/c/1433779Reviewed-by: 's avatarDaniel Clifford <danno@chromium.org>
Commit-Queue: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59061}
parent eeef8206
...@@ -223,7 +223,7 @@ namespace array { ...@@ -223,7 +223,7 @@ namespace array {
labels Bailout(JSArray, Smi) { labels Bailout(JSArray, Smi) {
let k: Smi = 0; let k: Smi = 0;
let fastO: FastJSArray = Cast<FastJSArray>(o) otherwise unreachable; let fastO: FastJSArray = Cast<FastJSArray>(o) otherwise unreachable;
let vector: Vector = Vector{len}; let vector = Vector{len};
const elementsKind: ElementsKind = fastO.map.elements_kind; const elementsKind: ElementsKind = fastO.map.elements_kind;
try { try {
if (IsElementsKindLessThanOrEqual(elementsKind, HOLEY_SMI_ELEMENTS)) { if (IsElementsKindLessThanOrEqual(elementsKind, HOLEY_SMI_ELEMENTS)) {
......
...@@ -512,11 +512,6 @@ const Type* ImplementationVisitor::Visit(VarDeclarationStatement* stmt) { ...@@ -512,11 +512,6 @@ const Type* ImplementationVisitor::Visit(VarDeclarationStatement* stmt) {
const Type* ImplementationVisitor::Visit( const Type* ImplementationVisitor::Visit(
VarDeclarationStatement* stmt, BlockBindings<LocalValue>* block_bindings) { VarDeclarationStatement* stmt, BlockBindings<LocalValue>* block_bindings) {
if (!stmt->const_qualified && !stmt->type) {
ReportError(
"variable declaration is missing type. Only 'const' bindings can "
"infer the type.");
}
// const qualified variables are required to be initialized properly. // const qualified variables are required to be initialized properly.
if (stmt->const_qualified && !stmt->initializer) { if (stmt->const_qualified && !stmt->initializer) {
ReportError("local constant \"", stmt->name, "\" is not initialized."); ReportError("local constant \"", stmt->name, "\" is not initialized.");
......
...@@ -38,6 +38,7 @@ enum class ParseResultHolderBase::TypeId { ...@@ -38,6 +38,7 @@ enum class ParseResultHolderBase::TypeId {
kStatementPtr, kStatementPtr,
kDeclarationPtr, kDeclarationPtr,
kTypeExpressionPtr, kTypeExpressionPtr,
kOptionalTypeExpressionPtr,
kLabelBlockPtr, kLabelBlockPtr,
kOptionalLabelBlockPtr, kOptionalLabelBlockPtr,
kNameAndTypeExpression, kNameAndTypeExpression,
...@@ -84,6 +85,10 @@ V8_EXPORT_PRIVATE const ParseResultTypeId ...@@ -84,6 +85,10 @@ V8_EXPORT_PRIVATE const ParseResultTypeId
ParseResultHolder<TypeExpression*>::id = ParseResultHolder<TypeExpression*>::id =
ParseResultTypeId::kTypeExpressionPtr; ParseResultTypeId::kTypeExpressionPtr;
template <> template <>
V8_EXPORT_PRIVATE const ParseResultTypeId
ParseResultHolder<base::Optional<TypeExpression*>>::id =
ParseResultTypeId::kOptionalTypeExpressionPtr;
template <>
V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<LabelBlock*>::id = V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<LabelBlock*>::id =
ParseResultTypeId::kLabelBlockPtr; ParseResultTypeId::kLabelBlockPtr;
template <> template <>
...@@ -866,10 +871,13 @@ base::Optional<ParseResult> MakeVarDeclarationStatement( ...@@ -866,10 +871,13 @@ base::Optional<ParseResult> MakeVarDeclarationStatement(
NamingConventionError("Variable", name, "lowerCamelCase"); NamingConventionError("Variable", name, "lowerCamelCase");
} }
auto type = child_results->NextAs<TypeExpression*>(); auto type = child_results->NextAs<base::Optional<TypeExpression*>>();
base::Optional<Expression*> initializer; base::Optional<Expression*> initializer;
if (child_results->HasNext()) if (child_results->HasNext())
initializer = child_results->NextAs<Expression*>(); initializer = child_results->NextAs<Expression*>();
if (!initializer && !type) {
ReportError("Declaration is missing a type.");
}
Statement* result = MakeNode<VarDeclarationStatement>( Statement* result = MakeNode<VarDeclarationStatement>(
const_qualified, std::move(name), type, initializer); const_qualified, std::move(name), type, initializer);
return ParseResult{result}; return ParseResult{result};
...@@ -1522,15 +1530,18 @@ struct TorqueGrammar : Grammar { ...@@ -1522,15 +1530,18 @@ struct TorqueGrammar : Grammar {
Optional<Expression*>(expression), Token("]")}, Optional<Expression*>(expression), Token("]")},
MakeRangeExpression)}; MakeRangeExpression)};
Symbol* optionalTypeSpecifier =
Optional<TypeExpression*>(Sequence({Token(":"), &type}));
// Result: Statement* // Result: Statement*
Symbol varDeclaration = { Symbol varDeclaration = {
Rule({OneOf({"let", "const"}), &identifier, Token(":"), &type}, Rule({OneOf({"let", "const"}), &identifier, optionalTypeSpecifier},
MakeVarDeclarationStatement)}; MakeVarDeclarationStatement)};
// Result: Statement* // Result: Statement*
Symbol varDeclarationWithInitialization = { Symbol varDeclarationWithInitialization = {
Rule({OneOf({"let", "const"}), &identifier, Token(":"), &type, Token("="), Rule({OneOf({"let", "const"}), &identifier, optionalTypeSpecifier,
expression}, Token("="), expression},
MakeVarDeclarationStatement)}; MakeVarDeclarationStatement)};
// Result: Statement* // Result: Statement*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment