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

[torque-ls] Support types in document-wide symbol requests

This CL adds support for all kinds of Types to "textDocument/symbol"
requests. While LSP has support for classes and structs, it does not
have support for generic types. Only classes are marked as such,
while all other types are marked as structs in terms of the LSP.

Special care has to be taken with TypeAliases. Generic call sites
introduce a new scope (similar to namespace scopes), where new
TypeAliases are created for Generic type arguments. These TypeAliases
then point to the specialized type inside this call-site specific
scope. To omit the specialized TypeAliaes from the symbols list,
they are marked using the "is_user_defined" flag.

R=sigurds@chromium.org

Bug: v8:8880
Change-Id: I576d1c677a5255d54f7774aa053f431608a4cd0c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1613240
Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
Reviewed-by: 's avatarSigurd Schneider <sigurds@chromium.org>
Auto-Submit: Simon Zünd <szuend@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61534}
parent 4b60b40a
......@@ -296,7 +296,8 @@ void DeclarationVisitor::DeclareSpecializedTypes(const SpecializationKey& key) {
for (auto type : key.specialized_types) {
Identifier* generic_type_name =
key.generic->declaration()->generic_parameters[i++];
Declarations::DeclareType(generic_type_name, type);
TypeAlias* alias = Declarations::DeclareType(generic_type_name, type);
alias->SetIsUserDefined(false);
}
}
......
......@@ -137,10 +137,10 @@ Namespace* Declarations::DeclareNamespace(const std::string& name) {
return Declare(name, std::unique_ptr<Namespace>(new Namespace(name)));
}
void Declarations::DeclareType(const Identifier* name, const Type* type) {
TypeAlias* Declarations::DeclareType(const Identifier* name, const Type* type) {
CheckAlreadyDeclared<TypeAlias>(name->value, "type");
Declare(name->value,
std::unique_ptr<TypeAlias>(new TypeAlias(type, true, name->pos)));
return Declare(name->value, std::unique_ptr<TypeAlias>(
new TypeAlias(type, true, name->pos)));
}
const TypeAlias* Declarations::PredeclareTypeAlias(const Identifier* name,
......
......@@ -72,7 +72,7 @@ class Declarations {
static Generic* LookupUniqueGeneric(const QualifiedName& name);
static Namespace* DeclareNamespace(const std::string& name);
static void DeclareType(const Identifier* name, const Type* type);
static TypeAlias* DeclareType(const Identifier* name, const Type* type);
static const TypeAlias* PredeclareTypeAlias(const Identifier* name,
TypeDeclaration* type,
......
......@@ -312,6 +312,15 @@ void HandleDocumentSymbolRequest(DocumentSymbolRequest request,
symbol.set_name(generic->name());
symbol.set_kind(SymbolKind::kFunction);
symbol.location().SetTo(generic->Position());
} else if (symbol->IsTypeAlias()) {
const Type* type = TypeAlias::cast(symbol)->type();
SymbolKind kind =
type->IsClassType() ? SymbolKind::kClass : SymbolKind::kStruct;
SymbolInformation sym = response.add_result();
sym.set_name(type->ToString());
sym.set_kind(kind);
sym.location().SetTo(symbol->Position());
}
}
......
......@@ -50,6 +50,10 @@ class LanguageServerData : public ContextualClass<LanguageServerData> {
Get().PrepareAllDeclarableSymbols();
}
static void SetTypeOracle(TypeOracle type_oracle) {
Get().type_oracle_ = base::make_unique<TypeOracle>(std::move(type_oracle));
}
static const Symbols& SymbolsForSourceId(SourceId id) {
return Get().symbols_map_[id];
}
......@@ -61,6 +65,7 @@ class LanguageServerData : public ContextualClass<LanguageServerData> {
DefinitionsMap definitions_map_;
SymbolsMap symbols_map_;
std::unique_ptr<GlobalContext> global_context_;
std::unique_ptr<TypeOracle> type_oracle_;
};
} // namespace torque
......
......@@ -89,6 +89,7 @@ void CompileCurrentAst(TorqueCompilerOptions options) {
if (GlobalContext::collect_language_server_data()) {
LanguageServerData::SetGlobalContext(std::move(GlobalContext::Get()));
LanguageServerData::SetTypeOracle(std::move(TypeOracle::Get()));
}
}
......
......@@ -81,7 +81,7 @@ class TypeBase {
return static_cast<const x*>(declarable); \
}
class Type : public TypeBase {
class V8_EXPORT_PRIVATE Type : public TypeBase {
public:
virtual bool IsSubtypeOf(const Type* supertype) const;
......@@ -239,7 +239,7 @@ class AbstractType final : public Type {
};
// For now, builtin pointers are restricted to Torque-defined builtins.
class BuiltinPointerType final : public Type {
class V8_EXPORT_PRIVATE BuiltinPointerType final : public Type {
public:
DECLARE_TYPE_BOILERPLATE(BuiltinPointerType)
std::string ToExplicitString() const override;
......@@ -325,7 +325,7 @@ struct TypeLess {
}
};
class UnionType final : public Type {
class V8_EXPORT_PRIVATE UnionType final : public Type {
public:
DECLARE_TYPE_BOILERPLATE(UnionType)
std::string ToExplicitString() const override;
......
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