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

[torque-ls] Add source positions for constant definitions

This CL changes 'Value' to use an 'Identifier' for its name, where
the source position represents the point where it is defined. This is
used to support "goto definition" for constants and extern constants.

R=tebbi@chromium.org

Bug: v8:8880
Change-Id: Ifb9ff08b36cbd9fb2691dbae579d2df29edd651d
Reviewed-on: https://chromium-review.googlesource.com/c/1495986Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Commit-Queue: Simon Zünd <szuend@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59977}
parent d5ef815c
......@@ -825,13 +825,13 @@ struct ExternalRuntimeDeclaration : CallableNode {
struct ConstDeclaration : Declaration {
DEFINE_AST_NODE_LEAF_BOILERPLATE(ConstDeclaration)
ConstDeclaration(SourcePosition pos, std::string name, TypeExpression* type,
ConstDeclaration(SourcePosition pos, Identifier* name, TypeExpression* type,
Expression* expression)
: Declaration(kKind, pos),
name(std::move(name)),
name(name),
type(type),
expression(expression) {}
std::string name;
Identifier* name;
TypeExpression* type;
Expression* expression;
};
......@@ -882,13 +882,13 @@ struct SpecializationDeclaration : Declaration {
struct ExternConstDeclaration : Declaration {
DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternConstDeclaration)
ExternConstDeclaration(SourcePosition pos, std::string name,
ExternConstDeclaration(SourcePosition pos, Identifier* name,
TypeExpression* type, std::string literal)
: Declaration(kKind, pos),
name(std::move(name)),
name(name),
type(type),
literal(std::move(literal)) {}
std::string name;
Identifier* name;
TypeExpression* type;
std::string literal;
};
......
......@@ -132,7 +132,7 @@ void CSAGenerator::EmitInstruction(
out_ << results[0] << " = ";
}
out_ << instruction.constant->ExternalAssemblerName() << "(state_)."
<< instruction.constant->constant_name() << "()";
<< instruction.constant->name()->value << "()";
if (type->IsStructType()) {
out_ << ".Flatten();\n";
} else {
......
......@@ -184,7 +184,7 @@ inline Namespace* CurrentNamespace() {
class Value : public Declarable {
public:
DECLARE_DECLARABLE_BOILERPLATE(Value, value)
const std::string& name() const { return name_; }
const Identifier* name() const { return name_; }
virtual bool IsConst() const { return true; }
VisitResult value() const { return *value_; }
const Type* type() const { return type_; }
......@@ -195,12 +195,12 @@ class Value : public Declarable {
}
protected:
Value(Kind kind, const Type* type, const std::string& name)
Value(Kind kind, const Type* type, Identifier* name)
: Declarable(kind), type_(type), name_(name) {}
private:
const Type* type_;
std::string name_;
Identifier* name_;
base::Optional<VisitResult> value_;
};
......@@ -208,7 +208,6 @@ class NamespaceConstant : public Value {
public:
DECLARE_DECLARABLE_BOILERPLATE(NamespaceConstant, constant)
const std::string& constant_name() const { return constant_name_; }
Expression* body() { return body_; }
std::string ExternalAssemblerName() const {
return Namespace::cast(ParentScope())->ExternalName();
......@@ -216,13 +215,11 @@ class NamespaceConstant : public Value {
private:
friend class Declarations;
explicit NamespaceConstant(std::string constant_name, const Type* type,
explicit NamespaceConstant(Identifier* constant_name, const Type* type,
Expression* body)
: Value(Declarable::kNamespaceConstant, type, constant_name),
constant_name_(std::move(constant_name)),
body_(body) {}
std::string constant_name_;
Expression* body_;
};
......@@ -232,8 +229,8 @@ class ExternConstant : public Value {
private:
friend class Declarations;
explicit ExternConstant(std::string name, const Type* type, std::string value)
: Value(Declarable::kExternConstant, type, std::move(name)) {
explicit ExternConstant(Identifier* name, const Type* type, std::string value)
: Value(Declarable::kExternConstant, type, name) {
set_value(VisitResult(type, std::move(value)));
}
};
......
......@@ -275,18 +275,19 @@ RuntimeFunction* Declarations::DeclareRuntimeFunction(
new RuntimeFunction(name, signature, transitioning))));
}
void Declarations::DeclareExternConstant(const std::string& name,
const Type* type, std::string value) {
CheckAlreadyDeclared<Value>(name, "constant");
void Declarations::DeclareExternConstant(Identifier* name, const Type* type,
std::string value) {
CheckAlreadyDeclared<Value>(name->value, "constant");
ExternConstant* result = new ExternConstant(name, type, value);
Declare(name, std::unique_ptr<Declarable>(result));
Declare(name->value, std::unique_ptr<Declarable>(result));
}
NamespaceConstant* Declarations::DeclareNamespaceConstant(
const std::string& name, const Type* type, Expression* body) {
CheckAlreadyDeclared<Value>(name, "constant");
NamespaceConstant* Declarations::DeclareNamespaceConstant(Identifier* name,
const Type* type,
Expression* body) {
CheckAlreadyDeclared<Value>(name->value, "constant");
NamespaceConstant* result = new NamespaceConstant(name, type, body);
Declare(name, std::unique_ptr<Declarable>(result));
Declare(name->value, std::unique_ptr<Declarable>(result));
return result;
}
......
......@@ -120,9 +120,9 @@ class Declarations {
const Signature& signature,
bool transitioning);
static void DeclareExternConstant(const std::string& name, const Type* type,
static void DeclareExternConstant(Identifier* name, const Type* type,
std::string value);
static NamespaceConstant* DeclareNamespaceConstant(const std::string& name,
static NamespaceConstant* DeclareNamespaceConstant(Identifier* name,
const Type* type,
Expression* body);
......
......@@ -117,7 +117,7 @@ void ImplementationVisitor::EndNamespaceFile(Namespace* nspace) {
void ImplementationVisitor::Visit(NamespaceConstant* decl) {
Signature signature{{}, base::nullopt, {{}, false}, 0, decl->type(), {}};
const std::string& name = decl->name();
const std::string& name = decl->name()->value;
BindingsManagersScope bindings_managers_scope;
......@@ -1768,7 +1768,7 @@ LocationReference ImplementationVisitor::GetLocationReference(
if (base::Optional<Binding<LocalValue>*> value =
TryLookupLocalValue(expr->name->value)) {
if (GlobalContext::collect_language_server_data()) {
LanguageServerData::AddDefinition(expr->pos,
LanguageServerData::AddDefinition(expr->name->pos,
(*value)->declaration_position());
}
if (expr->generic_arguments.size() != 0) {
......@@ -1789,6 +1789,9 @@ LocationReference ImplementationVisitor::GetLocationReference(
QualifiedName name =
QualifiedName(expr->namespace_qualification, expr->name->value);
if (base::Optional<Builtin*> builtin = Declarations::TryLookupBuiltin(name)) {
if (GlobalContext::collect_language_server_data()) {
LanguageServerData::AddDefinition(expr->name->pos, (*builtin)->pos());
}
return LocationReference::Temporary(GetBuiltinCode(*builtin),
"builtin " + expr->name->value);
}
......@@ -1806,12 +1809,15 @@ LocationReference ImplementationVisitor::GetLocationReference(
}
}
Value* value = Declarations::LookupValue(name);
if (GlobalContext::collect_language_server_data()) {
LanguageServerData::AddDefinition(expr->name->pos, value->name()->pos);
}
if (auto* constant = NamespaceConstant::DynamicCast(value)) {
if (constant->type()->IsConstexpr()) {
return LocationReference::Temporary(
VisitResult(constant->type(), constant->ExternalAssemblerName() +
"(state_)." +
constant->constant_name() + "()"),
constant->name()->value + "()"),
"namespace constant " + expr->name->value);
}
assembler().Emit(NamespaceConstantInstruction{constant});
......
......@@ -480,25 +480,24 @@ base::Optional<ParseResult> MakeTorqueBuiltinDeclaration(
base::Optional<ParseResult> MakeConstDeclaration(
ParseResultIterator* child_results) {
auto name = child_results->NextAs<std::string>();
if (!IsValidNamespaceConstName(name)) {
NamingConventionError("Constant", name, "kUpperCamelCase");
auto name = child_results->NextAs<Identifier*>();
if (!IsValidNamespaceConstName(name->value)) {
NamingConventionError("Constant", name->value, "kUpperCamelCase");
}
auto type = child_results->NextAs<TypeExpression*>();
auto expression = child_results->NextAs<Expression*>();
Declaration* result =
MakeNode<ConstDeclaration>(std::move(name), type, expression);
Declaration* result = MakeNode<ConstDeclaration>(name, type, expression);
return ParseResult{result};
}
base::Optional<ParseResult> MakeExternConstDeclaration(
ParseResultIterator* child_results) {
auto name = child_results->NextAs<std::string>();
auto name = child_results->NextAs<Identifier*>();
auto type = child_results->NextAs<TypeExpression*>();
auto literal = child_results->NextAs<std::string>();
Declaration* result = MakeNode<ExternConstDeclaration>(std::move(name), type,
std::move(literal));
Declaration* result =
MakeNode<ExternConstDeclaration>(name, type, std::move(literal));
return ParseResult{result};
}
......@@ -1584,10 +1583,10 @@ struct TorqueGrammar : Grammar {
// Result: Declaration*
Symbol declaration = {
Rule({Token("const"), &identifier, Token(":"), &type, Token("="),
expression, Token(";")},
Rule({Token("const"), &name, Token(":"), &type, Token("="), expression,
Token(";")},
MakeConstDeclaration),
Rule({Token("const"), &identifier, Token(":"), &type, Token("generates"),
Rule({Token("const"), &name, Token(":"), &type, Token("generates"),
&externalString, Token(";")},
MakeExternConstDeclaration),
Rule({CheckIf(Token("extern")), CheckIf(Token("transient")),
......
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