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

[torque] Add SourcePosition field to TypeAlias

This CL introduces a declaration_position_ field on TypeAlias,
corresponding with the SourcePosition of the name of the
Type where it is declared.
This information is needed by the language server for
"goto defintion".

R=tebbi@chromium.org

Bug: v8:7793
Change-Id: I0de2f7b7ba23b86de34441107ca9982d190c227f
Reviewed-on: https://chromium-review.googlesource.com/c/1497952
Commit-Queue: Simon Zünd <szuend@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60008}
parent acdaa4c7
......@@ -666,17 +666,17 @@ struct BlockStatement : Statement {
struct TypeDeclaration : Declaration {
DEFINE_AST_NODE_LEAF_BOILERPLATE(TypeDeclaration)
TypeDeclaration(SourcePosition pos, std::string name, bool transient,
TypeDeclaration(SourcePosition pos, Identifier* name, bool transient,
base::Optional<std::string> extends,
base::Optional<std::string> generates,
base::Optional<std::string> constexpr_generates)
: Declaration(kKind, pos),
name(std::move(name)),
name(name),
transient(transient),
extends(std::move(extends)),
generates(std::move(generates)),
constexpr_generates(std::move(constexpr_generates)) {}
std::string name;
Identifier* name;
bool transient;
base::Optional<std::string> extends;
base::Optional<std::string> generates;
......@@ -685,10 +685,10 @@ struct TypeDeclaration : Declaration {
struct TypeAliasDeclaration : Declaration {
DEFINE_AST_NODE_LEAF_BOILERPLATE(TypeAliasDeclaration)
TypeAliasDeclaration(SourcePosition pos, std::string name,
TypeAliasDeclaration(SourcePosition pos, Identifier* name,
TypeExpression* type)
: Declaration(kKind, pos), name(std::move(name)), type(type) {}
std::string name;
: Declaration(kKind, pos), name(name), type(type) {}
Identifier* name;
TypeExpression* type;
};
......@@ -848,14 +848,14 @@ struct StandardDeclaration : Declaration {
struct GenericDeclaration : Declaration {
DEFINE_AST_NODE_LEAF_BOILERPLATE(GenericDeclaration)
GenericDeclaration(SourcePosition pos, CallableNode* callable,
std::vector<std::string> generic_parameters,
std::vector<Identifier*> generic_parameters,
base::Optional<Statement*> body = base::nullopt)
: Declaration(kKind, pos),
callable(callable),
generic_parameters(std::move(generic_parameters)),
body(body) {}
CallableNode* callable;
std::vector<std::string> generic_parameters;
std::vector<Identifier*> generic_parameters;
base::Optional<Statement*> body;
};
......@@ -895,34 +895,34 @@ struct ExternConstDeclaration : Declaration {
struct StructDeclaration : Declaration {
DEFINE_AST_NODE_LEAF_BOILERPLATE(StructDeclaration)
StructDeclaration(SourcePosition pos, std::string name,
StructDeclaration(SourcePosition pos, Identifier* name,
std::vector<Declaration*> methods,
std::vector<StructFieldExpression> fields)
: Declaration(kKind, pos),
name(std::move(name)),
name(name),
methods(std::move(methods)),
fields(std::move(fields)) {}
std::string name;
Identifier* name;
std::vector<Declaration*> methods;
std::vector<StructFieldExpression> fields;
};
struct ClassDeclaration : Declaration {
DEFINE_AST_NODE_LEAF_BOILERPLATE(ClassDeclaration)
ClassDeclaration(SourcePosition pos, std::string name, bool is_extern,
ClassDeclaration(SourcePosition pos, Identifier* name, bool is_extern,
bool transient, base::Optional<std::string> super,
base::Optional<std::string> generates,
std::vector<Declaration*> methods,
std::vector<ClassFieldExpression> fields)
: Declaration(kKind, pos),
name(std::move(name)),
name(name),
is_extern(is_extern),
transient(transient),
super(std::move(super)),
generates(std::move(generates)),
methods(std::move(methods)),
fields(std::move(fields)) {}
std::string name;
Identifier* name;
bool is_extern;
bool transient;
base::Optional<std::string> super;
......
......@@ -56,7 +56,9 @@ std::ostream& operator<<(std::ostream& os, const RuntimeFunction& b) {
std::ostream& operator<<(std::ostream& os, const Generic& g) {
os << "generic " << g.name() << "<";
PrintCommaSeparatedList(os, g.declaration()->generic_parameters);
PrintCommaSeparatedList(
os, g.declaration()->generic_parameters,
[](const Identifier* identifier) { return identifier->value; });
os << ">";
return os;
......@@ -64,7 +66,7 @@ std::ostream& operator<<(std::ostream& os, const Generic& g) {
base::Optional<const Type*> Generic::InferTypeArgument(
size_t i, const TypeVector& arguments) {
const std::string type_name = declaration()->generic_parameters[i];
const std::string type_name = declaration()->generic_parameters[i]->value;
const std::vector<TypeExpression*>& parameters =
declaration()->callable->signature->parameters.types;
size_t j = declaration()->callable->signature->parameters.implicit_count;
......
......@@ -396,7 +396,7 @@ class Generic : public Declarable {
DECLARE_DECLARABLE_BOILERPLATE(Generic, generic)
GenericDeclaration* declaration() const { return declaration_; }
const std::vector<std::string> generic_parameters() const {
const std::vector<Identifier*> generic_parameters() const {
return declaration()->generic_parameters;
}
const std::string& name() const { return name_; }
......@@ -441,16 +441,23 @@ class TypeAlias : public Declarable {
const Type* type() const { return type_; }
bool IsRedeclaration() const { return redeclaration_; }
SourcePosition GetDeclarationPosition() const {
return declaration_position_;
}
private:
friend class Declarations;
explicit TypeAlias(const Type* type, bool redeclaration)
explicit TypeAlias(
const Type* type, bool redeclaration,
SourcePosition declaration_position = SourcePosition::Invalid())
: Declarable(Declarable::kTypeAlias),
type_(type),
redeclaration_(redeclaration) {}
redeclaration_(redeclaration),
declaration_position_(declaration_position) {}
const Type* type_;
bool redeclaration_;
const SourcePosition declaration_position_;
};
std::ostream& operator<<(std::ostream& os, const Callable& m);
......
......@@ -285,13 +285,13 @@ void DeclarationVisitor::Visit(ClassDeclaration* decl) {
const ClassType* super_class = ClassType::DynamicCast(super_type);
if (!super_class) {
ReportError(
"class \"", decl->name,
"class \"", decl->name->value,
"\" must extend either Tagged or an already declared class");
}
}
// The generates clause must create a TNode<>
std::string generates = decl->name;
std::string generates = decl->name->value;
if (decl->generates) {
generates = *decl->generates;
if (generates.length() < 7 || generates.substr(0, 6) != "TNode<" ||
......@@ -315,7 +315,7 @@ void DeclarationVisitor::Visit(ClassDeclaration* decl) {
decl->name, decl->is_extern,
decl->transient, "FixedArray");
}
GlobalContext::RegisterClass(decl->name, new_class);
GlobalContext::RegisterClass(decl->name->value, new_class);
class_declarations_.push_back(
std::make_tuple(CurrentScope::Get(), decl, new_class));
}
......@@ -342,7 +342,12 @@ void DeclarationVisitor::Visit(TypeDeclaration* decl) {
if (decl->transient) {
ReportError("cannot declare a transient type that is also constexpr");
}
std::string constexpr_name = CONSTEXPR_TYPE_PREFIX + decl->name;
// DeclareAbstractType expects an Identifier*. A new one is created from the
// declaration, and the SourcePosition copied from the original name.
Identifier* constexpr_name =
MakeNode<Identifier>(CONSTEXPR_TYPE_PREFIX + decl->name->value);
constexpr_name->pos = decl->name->pos;
base::Optional<std::string> constexpr_extends;
if (decl->extends)
constexpr_extends = CONSTEXPR_TYPE_PREFIX + *decl->extends;
......@@ -365,7 +370,7 @@ void DeclarationVisitor::DeclareSpecializedTypes(const SpecializationKey& key) {
}
for (auto type : key.specialized_types) {
std::string generic_type_name =
Identifier* generic_type_name =
key.generic->declaration()->generic_parameters[i++];
Declarations::DeclareType(generic_type_name, type, true);
}
......
......@@ -52,7 +52,7 @@ class DeclarationVisitor : public FileVisitor {
void Visit(TypeAliasDeclaration* decl) {
const Type* type = Declarations::GetType(decl->type);
type->AddAlias(decl->name);
type->AddAlias(decl->name->value);
Declarations::DeclareType(decl->name, type, true);
}
......
......@@ -5,6 +5,7 @@
#include "src/torque/declarations.h"
#include "src/torque/declarable.h"
#include "src/torque/global-context.h"
#include "src/torque/server-data.h"
#include "src/torque/type-oracle.h"
namespace v8 {
......@@ -59,10 +60,14 @@ std::vector<Declarable*> Declarations::LookupGlobalScope(
return d;
}
const Type* Declarations::LookupType(const QualifiedName& name) {
const TypeAlias* Declarations::LookupTypeAlias(const QualifiedName& name) {
TypeAlias* declaration =
EnsureUnique(FilterDeclarables<TypeAlias>(Lookup(name)), name, "type");
return declaration->type();
return declaration;
}
const Type* Declarations::LookupType(const QualifiedName& name) {
return LookupTypeAlias(name)->type();
}
const Type* Declarations::LookupType(std::string name) {
......@@ -79,7 +84,13 @@ const Type* Declarations::GetType(TypeExpression* type_expression) {
if (auto* basic = BasicTypeExpression::DynamicCast(type_expression)) {
std::string name =
(basic->is_constexpr ? CONSTEXPR_TYPE_PREFIX : "") + basic->name;
return LookupType(QualifiedName{basic->namespace_qualification, name});
const TypeAlias* alias =
LookupTypeAlias(QualifiedName{basic->namespace_qualification, name});
if (GlobalContext::collect_language_server_data()) {
LanguageServerData::AddDefinition(type_expression->pos,
alias->GetDeclarationPosition());
}
return alias->type();
} else if (auto* union_type = UnionTypeExpression::cast(type_expression)) {
return TypeOracle::GetUnionType(GetType(union_type->a),
GetType(union_type->b));
......@@ -147,10 +158,10 @@ Namespace* Declarations::DeclareNamespace(const std::string& name) {
}
const AbstractType* Declarations::DeclareAbstractType(
const std::string& name, bool transient, std::string generated,
const Identifier* name, bool transient, std::string generated,
base::Optional<const AbstractType*> non_constexpr_version,
const base::Optional<std::string>& parent) {
CheckAlreadyDeclared<TypeAlias>(name, "type");
CheckAlreadyDeclared<TypeAlias>(name->value, "type");
const Type* parent_type = nullptr;
if (parent) {
parent_type = LookupType(QualifiedName{*parent});
......@@ -159,29 +170,30 @@ const AbstractType* Declarations::DeclareAbstractType(
generated = parent_type->GetGeneratedTNodeTypeName();
}
const AbstractType* type = TypeOracle::GetAbstractType(
parent_type, name, transient, generated, non_constexpr_version);
parent_type, name->value, transient, generated, non_constexpr_version);
DeclareType(name, type, false);
return type;
}
void Declarations::DeclareType(const std::string& name, const Type* type,
void Declarations::DeclareType(const Identifier* name, const Type* type,
bool redeclaration) {
CheckAlreadyDeclared<TypeAlias>(name, "type");
Declare(name, std::unique_ptr<TypeAlias>(new TypeAlias(type, redeclaration)));
CheckAlreadyDeclared<TypeAlias>(name->value, "type");
Declare(name->value, std::unique_ptr<TypeAlias>(
new TypeAlias(type, redeclaration, name->pos)));
}
StructType* Declarations::DeclareStruct(const std::string& name) {
StructType* new_type = TypeOracle::GetStructType(name);
StructType* Declarations::DeclareStruct(const Identifier* name) {
StructType* new_type = TypeOracle::GetStructType(name->value);
DeclareType(name, new_type, false);
return new_type;
}
ClassType* Declarations::DeclareClass(const Type* super_type,
const std::string& name, bool is_extern,
const Identifier* name, bool is_extern,
bool transient,
const std::string& generates) {
ClassType* new_type = TypeOracle::GetClassType(super_type, name, is_extern,
transient, generates);
ClassType* new_type = TypeOracle::GetClassType(
super_type, name->value, is_extern, transient, generates);
DeclareType(name, new_type, false);
return new_type;
}
......
......@@ -54,6 +54,7 @@ class Declarations {
static std::vector<Declarable*> LookupGlobalScope(const std::string& name);
static const TypeAlias* LookupTypeAlias(const QualifiedName& name);
static const Type* LookupType(const QualifiedName& name);
static const Type* LookupType(std::string name);
static const Type* LookupGlobalType(const std::string& name);
......@@ -74,16 +75,16 @@ class Declarations {
static Namespace* DeclareNamespace(const std::string& name);
static const AbstractType* DeclareAbstractType(
const std::string& name, bool transient, std::string generated,
const Identifier* name, bool transient, std::string generated,
base::Optional<const AbstractType*> non_constexpr_version,
const base::Optional<std::string>& parent = {});
static void DeclareType(const std::string& name, const Type* type,
static void DeclareType(const Identifier* name, const Type* type,
bool redeclaration);
static StructType* DeclareStruct(const std::string& name);
static StructType* DeclareStruct(const Identifier* name);
static ClassType* DeclareClass(const Type* super, const std::string& name,
static ClassType* DeclareClass(const Type* super, const Identifier* name,
bool is_extern, bool transient,
const std::string& generates);
......
......@@ -76,6 +76,7 @@ enum class ParseResultHolderBase::TypeId {
kOptionalExpressionPtr,
kTypeswitchCase,
kStdVectorOfTypeswitchCase,
kStdVectorOfIdentifierPtr,
kJsonValue,
kJsonMember,
......
......@@ -15,7 +15,7 @@ namespace torque {
DEFINE_CONTEXTUAL_VARIABLE(CurrentAst)
using TypeList = std::vector<TypeExpression*>;
using GenericParameters = std::vector<std::string>;
using GenericParameters = std::vector<Identifier*>;
struct ExpressionWithSource {
Expression* expression;
......@@ -162,6 +162,10 @@ template <>
V8_EXPORT_PRIVATE const ParseResultTypeId
ParseResultHolder<std::vector<TypeswitchCase>>::id =
ParseResultTypeId::kStdVectorOfTypeswitchCase;
template <>
V8_EXPORT_PRIVATE const ParseResultTypeId
ParseResultHolder<std::vector<Identifier*>>::id =
ParseResultTypeId::kStdVectorOfIdentifierPtr;
namespace {
......@@ -173,9 +177,10 @@ base::Optional<ParseResult> AddGlobalDeclaration(
}
void LintGenericParameters(const GenericParameters& parameters) {
for (const std::string& parameter : parameters) {
if (!IsUpperCamelCase(parameter)) {
NamingConventionError("Generic parameter", parameter, "UpperCamelCase");
for (const Identifier* parameter : parameters) {
if (!IsUpperCamelCase(parameter->value)) {
NamingConventionError("Generic parameter", parameter->value,
"UpperCamelCase");
}
}
}
......@@ -503,25 +508,25 @@ base::Optional<ParseResult> MakeExternConstDeclaration(
base::Optional<ParseResult> MakeTypeAliasDeclaration(
ParseResultIterator* child_results) {
auto name = child_results->NextAs<std::string>();
auto name = child_results->NextAs<Identifier*>();
auto type = child_results->NextAs<TypeExpression*>();
Declaration* result = MakeNode<TypeAliasDeclaration>(std::move(name), type);
Declaration* result = MakeNode<TypeAliasDeclaration>(name, type);
return ParseResult{result};
}
base::Optional<ParseResult> MakeTypeDeclaration(
ParseResultIterator* child_results) {
auto transient = child_results->NextAs<bool>();
auto name = child_results->NextAs<std::string>();
if (!IsValidTypeName(name)) {
NamingConventionError("Type", name, "UpperCamelCase");
auto name = child_results->NextAs<Identifier*>();
if (!IsValidTypeName(name->value)) {
NamingConventionError("Type", name->value, "UpperCamelCase");
}
auto extends = child_results->NextAs<base::Optional<std::string>>();
auto generates = child_results->NextAs<base::Optional<std::string>>();
auto constexpr_generates =
child_results->NextAs<base::Optional<std::string>>();
Declaration* result = MakeNode<TypeDeclaration>(
std::move(name), transient, std::move(extends), std::move(generates),
name, transient, std::move(extends), std::move(generates),
std::move(constexpr_generates));
return ParseResult{result};
}
......@@ -549,17 +554,17 @@ base::Optional<ParseResult> MakeClassDeclaration(
ParseResultIterator* child_results) {
auto is_extern = child_results->NextAs<bool>();
auto transient = child_results->NextAs<bool>();
auto name = child_results->NextAs<std::string>();
if (!IsValidTypeName(name)) {
NamingConventionError("Type", name, "UpperCamelCase");
auto name = child_results->NextAs<Identifier*>();
if (!IsValidTypeName(name->value)) {
NamingConventionError("Type", name->value, "UpperCamelCase");
}
auto extends = child_results->NextAs<base::Optional<std::string>>();
auto generates = child_results->NextAs<base::Optional<std::string>>();
auto methods = child_results->NextAs<std::vector<Declaration*>>();
auto fields = child_results->NextAs<std::vector<ClassFieldExpression>>();
Declaration* result = MakeNode<ClassDeclaration>(
std::move(name), is_extern, transient, std::move(extends),
std::move(generates), std::move(methods), fields);
name, is_extern, transient, std::move(extends), std::move(generates),
std::move(methods), fields);
return ParseResult{result};
}
......@@ -593,11 +598,11 @@ base::Optional<ParseResult> MakeSpecializationDeclaration(
base::Optional<ParseResult> MakeStructDeclaration(
ParseResultIterator* child_results) {
auto name = child_results->NextAs<std::string>();
auto name = child_results->NextAs<Identifier*>();
auto methods = child_results->NextAs<std::vector<Declaration*>>();
auto fields = child_results->NextAs<std::vector<StructFieldExpression>>();
Declaration* result = MakeNode<StructDeclaration>(
std::move(name), std::move(methods), std::move(fields));
Declaration* result =
MakeNode<StructDeclaration>(name, std::move(methods), std::move(fields));
return ParseResult{result};
}
......@@ -1248,8 +1253,8 @@ struct TorqueGrammar : Grammar {
// Result: GenericParameters
Symbol genericParameters = {
Rule({Token("<"),
List<std::string>(
Sequence({&identifier, Token(":"), Token("type")}), Token(",")),
List<Identifier*>(Sequence({&name, Token(":"), Token("type")}),
Token(",")),
Token(">")})};
// Result: TypeList
......@@ -1590,18 +1595,17 @@ struct TorqueGrammar : Grammar {
&externalString, Token(";")},
MakeExternConstDeclaration),
Rule({CheckIf(Token("extern")), CheckIf(Token("transient")),
Token("class"), &identifier,
Token("class"), &name,
Optional<std::string>(Sequence({Token("extends"), &identifier})),
Optional<std::string>(
Sequence({Token("generates"), &externalString})),
Token("{"), List<Declaration*>(&method),
List<ClassFieldExpression>(&classField), Token("}")},
MakeClassDeclaration),
Rule({Token("struct"), &identifier, Token("{"),
List<Declaration*>(&method),
Rule({Token("struct"), &name, Token("{"), List<Declaration*>(&method),
List<StructFieldExpression>(&structField), Token("}")},
MakeStructDeclaration),
Rule({CheckIf(Token("transient")), Token("type"), &identifier,
Rule({CheckIf(Token("transient")), Token("type"), &name,
Optional<std::string>(Sequence({Token("extends"), &identifier})),
Optional<std::string>(
Sequence({Token("generates"), &externalString})),
......@@ -1609,7 +1613,7 @@ struct TorqueGrammar : Grammar {
Sequence({Token("constexpr"), &externalString})),
Token(";")},
MakeTypeDeclaration),
Rule({Token("type"), &identifier, Token("="), &type, Token(";")},
Rule({Token("type"), &name, Token("="), &type, Token(";")},
MakeTypeAliasDeclaration),
Rule({Token("intrinsic"), &intrinsicName,
TryOrDefault<GenericParameters>(&genericParameters),
......
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