Commit b95550e8 authored by Simon Zünd's avatar Simon Zünd Committed by Commit Bot

[torque] Add NameExpression to get SourcePositions for identifiers

This CL introduces a new expression that can replace "std::string"
fields in other expressions. The main goal is to get SourcePositions
for identifiers to make them available in the language server.

The CL introduces a separate symbol "name", that allows to
incrementellay replace strings with name expression where needed. As
an example, variable delcarations now use a NameExpression for the
variable name.

R=danno@chromium.org, tebbi@chromium.org

Bug: v8:7793
Change-Id: I5b88bbaeac597b8e9760d2e01880e5e599ebf802
Reviewed-on: https://chromium-review.googlesource.com/c/1488752
Commit-Queue: Simon Zünd <szuend@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59865}
parent cfdf4218
......@@ -87,6 +87,7 @@ namespace torque {
AST_STATEMENT_NODE_KIND_LIST(V) \
AST_DECLARATION_NODE_KIND_LIST(V) \
AST_CALLABLE_NODE_KIND_LIST(V) \
V(Identifier) \
V(LabelBlock)
struct AstNode {
......@@ -194,6 +195,14 @@ class Ast {
static const char* const kThisParameterName = "this";
// A Identifier is a string with a SourcePosition attached.
struct Identifier : AstNode {
DEFINE_AST_NODE_LEAF_BOILERPLATE(Identifier)
Identifier(SourcePosition pos, std::string identifier)
: AstNode(kKind, pos), value(std::move(identifier)) {}
std::string value;
};
struct IdentifierExpression : LocationExpression {
DEFINE_AST_NODE_LEAF_BOILERPLATE(IdentifierExpression)
IdentifierExpression(SourcePosition pos,
......@@ -532,16 +541,16 @@ struct TailCallStatement : Statement {
struct VarDeclarationStatement : Statement {
DEFINE_AST_NODE_LEAF_BOILERPLATE(VarDeclarationStatement)
VarDeclarationStatement(
SourcePosition pos, bool const_qualified, std::string name,
SourcePosition pos, bool const_qualified, Identifier* name,
base::Optional<TypeExpression*> type,
base::Optional<Expression*> initializer = base::nullopt)
: Statement(kKind, pos),
const_qualified(const_qualified),
name(std::move(name)),
name(name),
type(type),
initializer(initializer) {}
bool const_qualified;
std::string name;
Identifier* name;
base::Optional<TypeExpression*> type;
base::Optional<Expression*> initializer;
};
......
......@@ -44,6 +44,7 @@ enum class ParseResultHolderBase::TypeId {
kBool,
kStdVectorOfString,
kExpressionPtr,
kIdentifierPtr,
kLocationExpressionPtr,
kStatementPtr,
kDeclarationPtr,
......
......@@ -571,7 +571,7 @@ const Type* ImplementationVisitor::Visit(
TypeVector lowered_types = LowerType(*type);
for (const Type* type : lowered_types) {
assembler().Emit(PushUninitializedInstruction{TypeOracle::GetTopType(
"unitialized variable '" + stmt->name + "' of type " +
"unitialized variable '" + stmt->name->value + "' of type " +
type->ToString() + " originally defined at " +
PositionAsString(stmt->pos),
type)});
......@@ -579,7 +579,7 @@ const Type* ImplementationVisitor::Visit(
init_result =
VisitResult(*type, assembler().TopRange(lowered_types.size()));
}
block_bindings->Add(stmt->name,
block_bindings->Add(stmt->name->value,
LocalValue{stmt->const_qualified, *init_result});
return TypeOracle::GetVoidType();
}
......@@ -1121,7 +1121,7 @@ const Type* ImplementationVisitor::Visit(ForOfLoopStatement* stmt) {
element_result = element_scope.Yield(result);
}
Binding<LocalValue> element_var_binding{&ValueBindingsManager::Get(),
stmt->var_declaration->name,
stmt->var_declaration->name->value,
LocalValue{true, element_result}};
Visit(stmt->body);
}
......
......@@ -61,6 +61,9 @@ template <>
V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<Expression*>::id =
ParseResultTypeId::kExpressionPtr;
template <>
V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<Identifier*>::id =
ParseResultTypeId::kIdentifierPtr;
template <>
V8_EXPORT_PRIVATE const ParseResultTypeId
ParseResultHolder<LocationExpression*>::id =
ParseResultTypeId::kLocationExpressionPtr;
......@@ -750,7 +753,7 @@ base::Optional<ParseResult> MakeTypeswitchStatement(
{
CurrentSourcePosition::Scope current_source_position(expression->pos);
current_block->statements.push_back(MakeNode<VarDeclarationStatement>(
true, "_value", base::nullopt, expression));
true, MakeNode<Identifier>("_value"), base::nullopt, expression));
}
TypeExpression* accumulated_types;
......@@ -773,8 +776,8 @@ base::Optional<ParseResult> MakeTypeswitchStatement(
}
std::string name = "_case_value";
if (cases[i].name) name = *cases[i].name;
case_block->statements.push_back(
MakeNode<VarDeclarationStatement>(true, name, cases[i].type, value));
case_block->statements.push_back(MakeNode<VarDeclarationStatement>(
true, MakeNode<Identifier>(name), cases[i].type, value));
case_block->statements.push_back(cases[i].block);
if (i < cases.size() - 1) {
BlockStatement* next_block = MakeNode<BlockStatement>();
......@@ -829,9 +832,9 @@ base::Optional<ParseResult> MakeVarDeclarationStatement(
auto kind = child_results->NextAs<std::string>();
bool const_qualified = kind == "const";
if (!const_qualified) DCHECK_EQ("let", kind);
auto name = child_results->NextAs<std::string>();
if (!IsLowerCamelCase(name)) {
NamingConventionError("Variable", name, "lowerCamelCase");
auto name = child_results->NextAs<Identifier*>();
if (!IsLowerCamelCase(name->value)) {
NamingConventionError("Variable", name->value, "lowerCamelCase");
}
auto type = child_results->NextAs<base::Optional<TypeExpression*>>();
......@@ -841,8 +844,8 @@ base::Optional<ParseResult> MakeVarDeclarationStatement(
if (!initializer && !type) {
ReportError("Declaration is missing a type.");
}
Statement* result = MakeNode<VarDeclarationStatement>(
const_qualified, std::move(name), type, initializer);
Statement* result = MakeNode<VarDeclarationStatement>(const_qualified, name,
type, initializer);
return ParseResult{result};
}
......@@ -966,6 +969,12 @@ base::Optional<ParseResult> MakeExpressionWithSource(
ExpressionWithSource{e, child_results->matched_input().ToString()}};
}
base::Optional<ParseResult> MakeIdentifier(ParseResultIterator* child_results) {
auto name = child_results->NextAs<std::string>();
Identifier* result = MakeNode<Identifier>(std::move(name));
return ParseResult{result};
}
base::Optional<ParseResult> MakeIdentifierExpression(
ParseResultIterator* child_results) {
auto namespace_qualification =
......@@ -1209,6 +1218,9 @@ struct TorqueGrammar : Grammar {
// Result: std::string
Symbol identifier = {Rule({Pattern(MatchIdentifier)}, YieldMatchedInput)};
// Result: Identifier
Symbol name = {Rule({&identifier}, MakeIdentifier)};
// Result: std::string
Symbol intrinsicName = {
Rule({Pattern(MatchIntrinsicName)}, YieldMatchedInput)};
......@@ -1504,13 +1516,13 @@ struct TorqueGrammar : Grammar {
// Result: Statement*
Symbol varDeclaration = {
Rule({OneOf({"let", "const"}), &identifier, optionalTypeSpecifier},
Rule({OneOf({"let", "const"}), &name, optionalTypeSpecifier},
MakeVarDeclarationStatement)};
// Result: Statement*
Symbol varDeclarationWithInitialization = {
Rule({OneOf({"let", "const"}), &identifier, optionalTypeSpecifier,
Token("="), expression},
Rule({OneOf({"let", "const"}), &name, 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