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 {
labels Bailout(JSArray, Smi) {
let k: Smi = 0;
let fastO: FastJSArray = Cast<FastJSArray>(o) otherwise unreachable;
let vector: Vector = Vector{len};
let vector = Vector{len};
const elementsKind: ElementsKind = fastO.map.elements_kind;
try {
if (IsElementsKindLessThanOrEqual(elementsKind, HOLEY_SMI_ELEMENTS)) {
......
......@@ -512,11 +512,6 @@ const Type* ImplementationVisitor::Visit(VarDeclarationStatement* stmt) {
const Type* ImplementationVisitor::Visit(
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.
if (stmt->const_qualified && !stmt->initializer) {
ReportError("local constant \"", stmt->name, "\" is not initialized.");
......
......@@ -38,6 +38,7 @@ enum class ParseResultHolderBase::TypeId {
kStatementPtr,
kDeclarationPtr,
kTypeExpressionPtr,
kOptionalTypeExpressionPtr,
kLabelBlockPtr,
kOptionalLabelBlockPtr,
kNameAndTypeExpression,
......@@ -84,6 +85,10 @@ V8_EXPORT_PRIVATE const ParseResultTypeId
ParseResultHolder<TypeExpression*>::id =
ParseResultTypeId::kTypeExpressionPtr;
template <>
V8_EXPORT_PRIVATE const ParseResultTypeId
ParseResultHolder<base::Optional<TypeExpression*>>::id =
ParseResultTypeId::kOptionalTypeExpressionPtr;
template <>
V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<LabelBlock*>::id =
ParseResultTypeId::kLabelBlockPtr;
template <>
......@@ -866,10 +871,13 @@ base::Optional<ParseResult> MakeVarDeclarationStatement(
NamingConventionError("Variable", name, "lowerCamelCase");
}
auto type = child_results->NextAs<TypeExpression*>();
auto type = child_results->NextAs<base::Optional<TypeExpression*>>();
base::Optional<Expression*> initializer;
if (child_results->HasNext())
initializer = child_results->NextAs<Expression*>();
if (!initializer && !type) {
ReportError("Declaration is missing a type.");
}
Statement* result = MakeNode<VarDeclarationStatement>(
const_qualified, std::move(name), type, initializer);
return ParseResult{result};
......@@ -1522,15 +1530,18 @@ struct TorqueGrammar : Grammar {
Optional<Expression*>(expression), Token("]")},
MakeRangeExpression)};
Symbol* optionalTypeSpecifier =
Optional<TypeExpression*>(Sequence({Token(":"), &type}));
// Result: Statement*
Symbol varDeclaration = {
Rule({OneOf({"let", "const"}), &identifier, Token(":"), &type},
Rule({OneOf({"let", "const"}), &identifier, optionalTypeSpecifier},
MakeVarDeclarationStatement)};
// Result: Statement*
Symbol varDeclarationWithInitialization = {
Rule({OneOf({"let", "const"}), &identifier, Token(":"), &type, Token("="),
expression},
Rule({OneOf({"let", "const"}), &identifier, optionalTypeSpecifier,
Token("="), expression},
MakeVarDeclarationStatement)};
// 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