From 9a6cbafa04b0930eafedca6cb752c1789a19e181 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 10 Feb 2018 20:01:10 -0800 Subject: [PATCH] Add lexical parent to Type/Var and definition_spelling->spell, definition_extent->extent --- src/clang_symbol_kind.h | 14 +- src/indexer.cc | 118 +++++++++------- src/indexer.h | 50 +++---- src/messages/cquery_base.cc | 4 +- src/messages/cquery_call_tree.cc | 4 +- src/messages/cquery_derived.cc | 4 +- src/messages/cquery_member_hierarchy.cc | 8 +- src/messages/cquery_type_hierarchy_tree.cc | 16 +-- src/messages/text_document_code_action.cc | 24 ++-- src/messages/text_document_definition.cc | 8 +- src/messages/text_document_rename.cc | 2 + src/messages/workspace_symbol.cc | 4 +- src/query.cc | 152 ++++++++++----------- src/query.h | 2 +- src/query_utils.cc | 56 ++++---- src/query_utils.h | 2 +- src/serializer.cc | 12 +- 17 files changed, 253 insertions(+), 227 deletions(-) diff --git a/src/clang_symbol_kind.h b/src/clang_symbol_kind.h index 1f53bc4a..37f875c8 100644 --- a/src/clang_symbol_kind.h +++ b/src/clang_symbol_kind.h @@ -66,7 +66,7 @@ enum class StorageClass : uint8_t { }; MAKE_REFLECT_TYPE_PROXY(StorageClass); -enum class SymbolRole : uint8_t { +enum class Role : uint8_t { None = 0, Declaration = 1 << 0, Definition = 1 << 1, @@ -75,15 +75,15 @@ enum class SymbolRole : uint8_t { ChildOf = 1 << 4, BaseOf = 1 << 5, - CalledBy = 1 << 6, + Call = 1 << 6, }; -MAKE_REFLECT_TYPE_PROXY(SymbolRole); -MAKE_ENUM_HASHABLE(SymbolRole); +MAKE_REFLECT_TYPE_PROXY(Role); +MAKE_ENUM_HASHABLE(Role); -inline uint8_t operator&(SymbolRole lhs, SymbolRole rhs) { +inline uint8_t operator&(Role lhs, Role rhs) { return uint8_t(lhs) & uint8_t(rhs); } -inline SymbolRole operator|(SymbolRole lhs, SymbolRole rhs) { - return SymbolRole(uint8_t(lhs) | uint8_t(rhs)); +inline Role operator|(Role lhs, Role rhs) { + return Role(uint8_t(lhs) | uint8_t(rhs)); } diff --git a/src/indexer.cc b/src/indexer.cc index b72559c6..27ea3cd1 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -434,6 +434,29 @@ std::string GetDocumentContentInRange(CXTranslationUnit cx_tu, return result; } +void SetUse(IndexFile* db, Maybe* def, Range range, ClangCursor lex_parent, Role role) { + switch (GetSymbolKind(lex_parent.get_kind())) { + default: + *def = Use(range, Id(), SymbolKind::File, role); + break; + case SymbolKind::Func: { + IndexFuncId id = db->ToFuncId(lex_parent.cx_cursor); + *def = Use(range, id, SymbolKind::Func, Role::Definition); + break; + } + case SymbolKind::Type: { + IndexTypeId id = db->ToTypeId(lex_parent.cx_cursor); + *def = Use(range, id, SymbolKind::Type, Role::Definition); + break; + } + case SymbolKind::Var: { + IndexVarId id = db->ToVarId(lex_parent.cx_cursor); + *def = Use(range, id, SymbolKind::Var, Role::Definition); + break; + } + } +} + void SetTypeName(IndexType* type, const ClangCursor& cursor, const CXIdxContainerInfo* container, @@ -587,7 +610,7 @@ void OnIndexReference_Function(IndexFile* db, Range loc, ClangCursor parent_cursor, IndexFuncId called_id, - SymbolRole role) { + Role role) { switch (GetSymbolKind(parent_cursor.get_kind())) { case SymbolKind::Func: { IndexFunc* parent = db->Resolve(db->ToFuncId(parent_cursor.cx_cursor)); @@ -615,8 +638,8 @@ void OnIndexReference_Function(IndexFile* db, } // namespace // static -const int IndexFile::kMajorVersion = 11; -const int IndexFile::kMinorVersion = 3; +const int IndexFile::kMajorVersion = 12; +const int IndexFile::kMinorVersion = 0; IndexFile::IndexFile(const std::string& path, const std::string& contents) : id_cache(path), path(path), file_contents(contents) {} @@ -699,14 +722,14 @@ void UniqueAdd(std::vector& values, T value) { // FIXME Reference: set id in call sites and remove this void AddUse(std::vector& values, Range value) { values.push_back( - Use(value, Id(), SymbolKind::File, SymbolRole::Reference)); + Use(value, Id(), SymbolKind::File, Role::Reference)); } void AddUse(IndexFile* db, std::vector& uses, Range range, ClangCursor parent, - SymbolRole role = SymbolRole::Reference) { + Role role = Role::Reference) { switch (GetSymbolKind(parent.get_kind())) { default: uses.push_back(Use(range, Id(), SymbolKind::File, role)); @@ -730,7 +753,7 @@ void AddUseSpell(IndexFile* db, std::vector& uses, ClangCursor cursor) { AddUse(db, uses, cursor.get_spelling_range(), - cursor.get_lexical_parent().cx_cursor, SymbolRole::Reference); + cursor.get_lexical_parent().cx_cursor, Role::Reference); } template @@ -1197,9 +1220,11 @@ ClangCursor::VisitResult VisitMacroDefinitionAndExpansions(ClangCursor cursor, "#define " + GetDocumentContentInRange(param->tu->cx_tu, cx_extent); var_def->def.kind = ClangSymbolKind::Macro; var_def->def.comments = cursor.get_comments(); - var_def->def.definition_spelling = decl_loc_spelling; - var_def->def.definition_extent = - ResolveCXSourceRange(cx_extent, nullptr); + ClangCursor lex_parent; + SetUse(db, &var_def->def.spell, decl_loc_spelling, lex_parent, + Role::Definition); + SetUse(db, &var_def->def.extent, + ResolveCXSourceRange(cx_extent, nullptr), lex_parent, Role::None); } else UniqueAddUse(db, var_def->uses, decl_loc_spelling, parent); @@ -1232,13 +1257,13 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor, case CXCursor_DeclRefExpr: { ClangCursor ref_cursor = clang_getCursorReferenced(cursor.cx_cursor); if (ref_cursor.get_kind() == CXCursor_NonTypeTemplateParameter) { - IndexVar* ref_index = + IndexVar* ref_var = db->Resolve(db->ToVarId(ref_cursor.get_usr_hash())); - if (ref_index->def.detailed_name.empty()) { - ref_index->def.definition_spelling = ref_cursor.get_spelling_range(); - ref_index->def.definition_extent = ref_cursor.get_extent(); - ref_index->def.kind = ClangSymbolKind::Parameter; - SetVarDetail(ref_index, ref_cursor.get_spelling(), ref_cursor, + if (ref_var->def.detailed_name.empty()) { + SetUse(db, &ref_var->def.spell, ref_cursor.get_spelling_range(), parent, Role::Definition); + SetUse(db, &ref_var->def.extent, ref_cursor.get_extent(), parent, Role::None); + ref_var->def.kind = ClangSymbolKind::Parameter; + SetVarDetail(ref_var, ref_cursor.get_spelling(), ref_cursor, nullptr, true, db, param); ClangType ref_type = clang_getCursorType(ref_cursor.cx_cursor); @@ -1254,7 +1279,7 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor, ref_cursor.get_lexical_parent()); } } - UniqueAddUseSpell(db, ref_index->uses, cursor); + UniqueAddUseSpell(db, ref_var->uses, cursor); } break; } @@ -1270,7 +1295,7 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor, IndexFuncId called_id = db->ToFuncId(overloaded.get_usr_hash()); OnIndexReference_Function(db, cursor.get_spelling_range(), data->container, called_id, - SymbolRole::CalledBy); + Role::Call); break; } } @@ -1288,8 +1313,8 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor, // CXCursor_TranslationUnit, but not (confirm this) by visiting // {Class,Function}Template. Thus we need to initialize it here. if (ref_index->def.detailed_name.empty()) { - ref_index->def.definition_spelling = ref_cursor.get_spelling_range(); - ref_index->def.definition_extent = ref_cursor.get_extent(); + SetUse(db, &ref_index->def.spell, ref_cursor.get_spelling_range(), parent, Role::Definition); + SetUse(db, &ref_index->def.extent, ref_cursor.get_extent(), parent, Role::None); #if CINDEX_HAVE_PRETTY ref_index->def.detailed_name = param->PrettyPrintCursor(cursor.cx_cursor); #else @@ -1314,8 +1339,8 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor, // CXCursor_TranslationUnit, but not (confirm this) by visiting // {Class,Function}Template. Thus we need to initialize it here. if (ref_index->def.detailed_name.empty()) { - ref_index->def.definition_spelling = ref_cursor.get_spelling_range(); - ref_index->def.definition_extent = ref_cursor.get_extent(); + SetUse(db, &ref_index->def.spell, ref_cursor.get_spelling_range(), parent, Role::Definition); + SetUse(db, &ref_index->def.extent, ref_cursor.get_extent(), parent, Role::None); #if CINDEX_HAVE_PRETTY ref_index->def.detailed_name = param->PrettyPrintCursor(cursor.cx_cursor); #else @@ -1410,7 +1435,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { return; // The language of this declaration - LanguageId decl_lang = [decl]() { + LanguageId decl_lang = [&decl]() { switch (clang_getCursorLanguage(decl->cursor)) { case CXLanguage_C: return LanguageId::C; @@ -1429,6 +1454,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { } NamespaceHelper* ns = ¶m->ns; + ClangCursor lex_parent(fromContainer(decl->lexicalContainer)); switch (decl->entityInfo->kind) { case CXIdxEntity_CXXNamespace: { @@ -1440,8 +1466,8 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { if (ns->def.detailed_name.empty()) { SetTypeName(ns, decl_cursor, decl->semanticContainer, decl->entityInfo->name, param); - ns->def.definition_spelling = decl_spell; - ns->def.definition_extent = decl_cursor.get_extent(); + SetUse(db, &ns->def.spell, decl_spell, lex_parent, Role::Definition); + SetUse(db, &ns->def.extent, decl_cursor.get_extent(), lex_parent, Role::None); if (decl->semanticContainer) { IndexTypeId parent_id = db->ToTypeId( ClangCursor(decl->semanticContainer->cursor).get_usr_hash()); @@ -1487,8 +1513,8 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { //} if (decl->isDefinition) { - var->def.definition_spelling = decl_spell; - var->def.definition_extent = decl_cursor.get_extent(); + SetUse(db, &var->def.spell, decl_spell, lex_parent, Role::Definition); + SetUse(db, &var->def.extent, decl_cursor.get_extent(), lex_parent, Role::None); } else { var->declarations.push_back(decl_spell); } @@ -1566,10 +1592,10 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { // TODO: Support multiple function definitions, which is common for // template specializations. if (decl->isDefinition && !is_template_specialization) { - // assert(!func->def.definition_spelling); - // assert(!func->def.definition_extent); - func->def.definition_spelling = decl_spelling; - func->def.definition_extent = decl_extent; + // assert(!func->def.spell); + // assert(!func->def.extent); + func->def.spell = decl_spelling; + func->def.extent = decl_extent; } else { IndexFunc::Declaration declaration; declaration.spelling = decl_spelling; @@ -1697,8 +1723,8 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { ClangCursor decl_cursor = decl->cursor; Range spell = decl_cursor.get_spelling_range(); Range extent = decl_cursor.get_extent(); - type->def.definition_spelling = spell; - type->def.definition_extent = extent; + SetUse(db, &type->def.spell, spell, lex_parent, Role::Definition); + SetUse(db, &type->def.extent, extent, lex_parent, Role::None); SetTypeName(type, decl_cursor, decl->semanticContainer, decl->entityInfo->name, param); @@ -1753,8 +1779,8 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { // } if (decl->isDefinition) { - type->def.definition_spelling = decl_spell; - type->def.definition_extent = decl_cursor.get_extent(); + SetUse(db, &type->def.spell, decl_spell, lex_parent, Role::Definition); + SetUse(db, &type->def.extent, decl_cursor.get_extent(), lex_parent, Role::None); if (decl_cursor.get_kind() == CXCursor_EnumDecl) { ClangType enum_type = clang_getEnumDeclIntegerType(decl->cursor); @@ -1790,11 +1816,10 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { origin->def.kind = type->def.kind; } // TODO The name may be assigned in |ResolveToDeclarationType| but - // |definition_spelling| is nullopt. - if (!origin->def.definition_spelling) { - origin->def.definition_spelling = - origin_cursor.get_spelling_range(); - origin->def.definition_extent = origin_cursor.get_extent(); + // |spell| is nullopt. + if (!origin->def.spell) { + SetUse(db, &origin->def.spell, origin_cursor.get_spelling_range(), lex_parent, Role::Definition); + SetUse(db, &origin->def.extent, origin_cursor.get_extent(), lex_parent, Role::None); } origin->derived.push_back(type_id); type->def.parents.push_back(origin_id); @@ -1909,6 +1934,7 @@ void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) { return; ClangCursor cursor(ref->cursor); + ClangCursor lex_parent(fromContainer(ref->container)); switch (ref->referencedEntity->kind) { case CXIdxEntity_CXXNamespaceAlias: @@ -1942,8 +1968,8 @@ void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) { CXFile referenced_file; Range spelling = referenced.get_spelling_range(&referenced_file); if (file == referenced_file) { - var->def.definition_spelling = spelling; - var->def.definition_extent = referenced.get_extent(); + SetUse(db, &var->def.spell, spelling, lex_parent, Role::Definition); + SetUse(db, &var->def.extent, referenced.get_extent(), lex_parent, Role::None); // TODO Some of the logic here duplicates CXIdxEntity_Variable branch // of OnIndexDeclaration. But there `decl` is of type CXIdxDeclInfo @@ -1953,7 +1979,7 @@ void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) { var->def.kind = ClangSymbolKind::Parameter; } } - UniqueAddUse(db, var->uses, loc, fromContainer(ref->container), SymbolRole::Reference); + UniqueAddUse(db, var->uses, loc, fromContainer(ref->container), Role::Reference); break; } @@ -2010,8 +2036,8 @@ void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) { OnIndexReference_Function( db, loc, ref->container->cursor, called_id, - SymbolRole::CalledBy | - (is_implicit ? SymbolRole::Implicit : SymbolRole::None)); + Role::Call | + (is_implicit ? Role::Implicit : Role::None)); // Checks if |str| starts with |start|. Ignores case. auto str_begin = [](const char* start, const char* str) { @@ -2053,7 +2079,7 @@ void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) { IndexFunc* ctor = db->Resolve(db->ToFuncId(*ctor_usr)); AddFuncUse(&ctor->uses, Use(loc, Id(), SymbolKind::File, - SymbolRole::CalledBy | SymbolRole::Implicit)); + Role::Call | Role::Implicit)); } } } @@ -2338,7 +2364,7 @@ void Reflect(Reader& visitor, Reference& value) { s = strchr(s, '|'); value.id.id = RawId(strtol(s + 1, &s, 10)); value.kind = static_cast(strtol(s + 1, &s, 10)); - value.role = static_cast(strtol(s + 1, &s, 10)); + value.role = static_cast(strtol(s + 1, &s, 10)); } else { Reflect(visitor, value.range); Reflect(visitor, value.id); diff --git a/src/indexer.h b/src/indexer.h index da4aa3ce..1424d136 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -92,10 +92,10 @@ struct Reference { Range range; Id id; SymbolKind kind; - SymbolRole role; + Role role; - bool HasValue() const { return id.HasValue(); } - std::tuple, SymbolKind, SymbolRole> ToTuple() const { + bool HasValue() const { return range.HasValue(); } + std::tuple, SymbolKind, Role> ToTuple() const { return std::make_tuple(range, id, kind, role); } bool operator==(const Reference& o) const { @@ -126,11 +126,11 @@ MAKE_HASHABLE(SymbolIdx, t.kind, t.idx); // |id,kind| refer to the referenced entity. struct SymbolRef : Reference { SymbolRef() = default; - SymbolRef(Range range, Id id, SymbolKind kind, SymbolRole role) + SymbolRef(Range range, Id id, SymbolKind kind, Role role) : Reference{range, id, kind, role} {} SymbolRef(Reference ref) : Reference(ref) {} SymbolRef(SymbolIdx si) - : Reference{Range(), Id(si.idx), si.kind, SymbolRole::None} {} + : Reference{Range(), Id(si.idx), si.kind, Role::None} {} RawId Idx() const { return RawId(id); } operator SymbolIdx() const { return SymbolIdx{Idx(), kind}; } @@ -141,7 +141,7 @@ struct SymbolRef : Reference { struct Use : Reference { Use() = default; Use(Reference ref) : Reference(ref) {} - Use(Range range, Id id, SymbolKind kind, SymbolRole role) + Use(Range range, Id id, SymbolKind kind, Role role) : Reference{range, id, kind, role} {} }; @@ -172,8 +172,8 @@ struct TypeDefDefinitionData { // It's also difficult to identify a `class Foo;` statement with the clang // indexer API (it's doable using cursor AST traversal), so we don't bother // supporting the feature. - Maybe definition_spelling; - Maybe definition_extent; + Maybe spell; + Maybe extent; // Immediate parent types. std::vector parents; @@ -194,8 +194,8 @@ struct TypeDefDefinitionData { bool operator==(const TypeDefDefinitionData& o) const { return detailed_name == o.detailed_name && - definition_spelling == o.definition_spelling && - definition_extent == o.definition_extent && alias_of == o.alias_of && + spell == o.spell && + extent == o.extent && alias_of == o.alias_of && parents == o.parents && types == o.types && funcs == o.funcs && vars == o.vars && kind == o.kind && hover == o.hover && comments == o.comments; @@ -218,8 +218,8 @@ void Reflect(TVisitor& visitor, TypeDefDefinitionData& value) { REFLECT_MEMBER(kind); REFLECT_MEMBER(hover); REFLECT_MEMBER(comments); - REFLECT_MEMBER(definition_spelling); - REFLECT_MEMBER(definition_extent); + REFLECT_MEMBER(spell); + REFLECT_MEMBER(extent); REFLECT_MEMBER(file); REFLECT_MEMBER(alias_of); REFLECT_MEMBER(parents); @@ -260,8 +260,8 @@ struct FuncDefDefinitionData { std::string detailed_name; std::string hover; std::string comments; - Maybe definition_spelling; - Maybe definition_extent; + Maybe spell; + Maybe extent; // Method this method overrides. std::vector base; @@ -282,8 +282,8 @@ struct FuncDefDefinitionData { bool operator==(const FuncDefDefinitionData& o) const { return detailed_name == o.detailed_name && - definition_spelling == o.definition_spelling && - definition_extent == o.definition_extent && + spell == o.spell && + extent == o.extent && declaring_type == o.declaring_type && base == o.base && locals == o.locals && callees == o.callees && kind == o.kind && storage == o.storage && hover == o.hover && comments == o.comments; @@ -308,8 +308,8 @@ void Reflect(TVisitor& visitor, FuncDefDefinitionData& value) { REFLECT_MEMBER(storage); REFLECT_MEMBER(hover); REFLECT_MEMBER(comments); - REFLECT_MEMBER(definition_spelling); - REFLECT_MEMBER(definition_extent); + REFLECT_MEMBER(spell); + REFLECT_MEMBER(extent); REFLECT_MEMBER(file); REFLECT_MEMBER(declaring_type); REFLECT_MEMBER(base); @@ -347,7 +347,7 @@ struct IndexFunc { // function context then the FuncRef will not have an associated id. // // To get all usages, also include the ranges inside of declarations and - // def.definition_spelling. + // def.spell. std::vector uses; IndexFunc() {} // For serialization. @@ -372,8 +372,8 @@ struct VarDefDefinitionData { std::string comments; // TODO: definitions should be a list of ranges, since there can be more // than one - when?? - Maybe definition_spelling; - Maybe definition_extent; + Maybe spell; + Maybe extent; typename F::FileId file; // Type of the variable. @@ -398,8 +398,8 @@ struct VarDefDefinitionData { bool operator==(const VarDefDefinitionData& o) const { return detailed_name == o.detailed_name && - definition_spelling == o.definition_spelling && - definition_extent == o.definition_extent && + spell == o.spell && + extent == o.extent && variable_type == o.variable_type && parent_id == o.parent_id && parent_kind == o.parent_kind && kind == o.kind && storage == o.storage && hover == o.hover && comments == o.comments; @@ -422,8 +422,8 @@ void Reflect(TVisitor& visitor, VarDefDefinitionData& value) { REFLECT_MEMBER(short_name_offset); REFLECT_MEMBER(hover); REFLECT_MEMBER(comments); - REFLECT_MEMBER(definition_spelling); - REFLECT_MEMBER(definition_extent); + REFLECT_MEMBER(spell); + REFLECT_MEMBER(extent); REFLECT_MEMBER(file); REFLECT_MEMBER(variable_type); REFLECT_MEMBER(parent_id); diff --git a/src/messages/cquery_base.cc b/src/messages/cquery_base.cc index 4a7e64e1..f06adffc 100644 --- a/src/messages/cquery_base.cc +++ b/src/messages/cquery_base.cc @@ -30,8 +30,8 @@ struct CqueryBaseHandler : BaseMessageHandler { // Order by |Definition| DESC, range size ASC. std::stable_sort(syms.begin(), syms.end(), [](const SymbolRef& a, const SymbolRef& b) { - return (a.role & SymbolRole::Definition) > - (b.role & SymbolRole::Definition); + return (a.role & Role::Definition) > + (b.role & Role::Definition); }); for (SymbolRef sym : syms) { if (sym.kind == SymbolKind::Type) { diff --git a/src/messages/cquery_call_tree.cc b/src/messages/cquery_call_tree.cc index db210a12..7f563773 100644 --- a/src/messages/cquery_call_tree.cc +++ b/src/messages/cquery_call_tree.cc @@ -53,10 +53,10 @@ std::vector BuildInitialCallTree( WorkingFiles* working_files, QueryFuncId root) { QueryFunc& root_func = db->funcs[root.id]; - if (!root_func.def || !root_func.def->definition_spelling) + if (!root_func.def || !root_func.def->spell) return {}; optional def_loc = - GetLsLocation(db, working_files, *root_func.def->definition_spelling); + GetLsLocation(db, working_files, *root_func.def->spell); if (!def_loc) return {}; diff --git a/src/messages/cquery_derived.cc b/src/messages/cquery_derived.cc index bc982278..6c6283cd 100644 --- a/src/messages/cquery_derived.cc +++ b/src/messages/cquery_derived.cc @@ -30,8 +30,8 @@ struct CqueryDerivedHandler : BaseMessageHandler { // Order by |Definition| DESC, range size ASC. std::stable_sort(syms.begin(), syms.end(), [](const SymbolRef& a, const SymbolRef& b) { - return (a.role & SymbolRole::Definition) > - (b.role & SymbolRole::Definition); + return (a.role & Role::Definition) > + (b.role & Role::Definition); }); for (const SymbolRef& sym : syms) { if (sym.kind == SymbolKind::Type) { diff --git a/src/messages/cquery_member_hierarchy.cc b/src/messages/cquery_member_hierarchy.cc index 0749b954..a072749c 100644 --- a/src/messages/cquery_member_hierarchy.cc +++ b/src/messages/cquery_member_hierarchy.cc @@ -40,10 +40,10 @@ MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy, jsonrpc, id, result); std::vector BuildInitial(QueryDatabase* db, WorkingFiles* working_files, QueryTypeId root) { QueryType& root_type = db->types[root.id]; - if (!root_type.def || !root_type.def->definition_spelling) + if (!root_type.def || !root_type.def->spell) return {}; optional def_loc = - GetLsLocation(db, working_files, *root_type.def->definition_spelling); + GetLsLocation(db, working_files, *root_type.def->spell); if (!def_loc) return {}; @@ -67,9 +67,9 @@ ExpandNode(QueryDatabase* db, WorkingFiles* working_files, QueryTypeId root) { // FIXME WithGen entry.type_id = var.def->variable_type ? var.def->variable_type->id : RawId(-1); - if (var.def->definition_spelling) { + if (var.def->spell) { optional loc = - GetLsLocation(db, working_files, *var.def->definition_spelling); + GetLsLocation(db, working_files, *var.def->spell); // TODO invalid location if (loc) entry.location = *loc; diff --git a/src/messages/cquery_type_hierarchy_tree.cc b/src/messages/cquery_type_hierarchy_tree.cc index ac30ff16..f2091feb 100644 --- a/src/messages/cquery_type_hierarchy_tree.cc +++ b/src/messages/cquery_type_hierarchy_tree.cc @@ -37,9 +37,9 @@ BuildParentInheritanceHierarchyForType(QueryDatabase* db, EachWithGen(db->types, root_type.def->parents, [&](QueryType& parent_type) { Out_CqueryTypeHierarchyTree::TypeEntry parent_entry; parent_entry.name = parent_type.def->detailed_name; - if (parent_type.def->definition_spelling) + if (parent_type.def->spell) parent_entry.location = GetLsLocation( - db, working_files, *parent_type.def->definition_spelling); + db, working_files, *parent_type.def->spell); parent_entry.children = BuildParentInheritanceHierarchyForType(db, working_files, parent_type); @@ -57,9 +57,9 @@ BuildInheritanceHierarchyForType(QueryDatabase* db, // Name and location. entry.name = root_type.def->detailed_name; - if (root_type.def->definition_spelling) + if (root_type.def->spell) entry.location = - GetLsLocation(db, working_files, *root_type.def->definition_spelling); + GetLsLocation(db, working_files, *root_type.def->spell); entry.children.reserve(root_type.derived.size()); @@ -101,9 +101,9 @@ BuildParentInheritanceHierarchyForFunc(QueryDatabase* db, Out_CqueryTypeHierarchyTree::TypeEntry parent_entry; parent_entry.name = parent_func.def->detailed_name; - if (parent_func.def->definition_spelling) + if (parent_func.def->spell) parent_entry.location = GetLsLocation( - db, working_files, *parent_func.def->definition_spelling); + db, working_files, *parent_func.def->spell); parent_entry.children = BuildParentInheritanceHierarchyForFunc(db, working_files, parent_id); @@ -125,9 +125,9 @@ BuildInheritanceHierarchyForFunc(QueryDatabase* db, // Name and location. entry.name = root_func.def->detailed_name; - if (root_func.def->definition_spelling) + if (root_func.def->spell) entry.location = - GetLsLocation(db, working_files, *root_func.def->definition_spelling); + GetLsLocation(db, working_files, *root_func.def->spell); entry.children.reserve(root_func.derived.size()); diff --git a/src/messages/text_document_code_action.cc b/src/messages/text_document_code_action.cc index 214b3576..59efd9fd 100644 --- a/src/messages/text_document_code_action.cc +++ b/src/messages/text_document_code_action.cc @@ -72,8 +72,8 @@ optional GetImplementationFile(QueryDatabase* db, QueryFunc& func = db->GetFunc(sym); // Note: we ignore the definition if it is in the same file (ie, // possibly a header). - if (func.def && func.def->definition_extent) { - QueryFileId t = db->GetFileId(*func.def->definition_extent); + if (func.def && func.def->extent) { + QueryFileId t = db->GetFileId(*func.def->extent); if (t != file_id) return t; } @@ -83,8 +83,8 @@ optional GetImplementationFile(QueryDatabase* db, QueryVar& var = db->GetVar(sym); // Note: we ignore the definition if it is in the same file (ie, // possibly a header). - if (var.def && var.def->definition_extent) { - QueryFileId t = db->GetFileId(*var.def->definition_extent); + if (var.def && var.def->extent) { + QueryFileId t = db->GetFileId(*var.def->extent); if (t != file_id) return t; } @@ -164,10 +164,10 @@ optional BuildAutoImplementForFunction(QueryDatabase* db, QueryType& declaring_type = db->types[func.def->declaring_type->id]; if (declaring_type.def) { type_name = std::string(declaring_type.def->ShortName()); - optional ls_type_def_extent = GetLsRange( - working_file, declaring_type.def->definition_extent->range); - if (ls_type_def_extent) { - same_file_insert_end = ls_type_def_extent->end; + optional ls_type_extent = GetLsRange( + working_file, declaring_type.def->extent->range); + if (ls_type_extent) { + same_file_insert_end = ls_type_extent->end; same_file_insert_end->character += 1; // move past semicolon. } } @@ -201,7 +201,7 @@ optional BuildAutoImplementForFunction(QueryDatabase* db, switch (sym.kind) { case SymbolKind::Func: { QueryFunc& sym_func = db->GetFunc(sym); - if (!sym_func.def || !sym_func.def->definition_extent) + if (!sym_func.def || !sym_func.def->extent) break; for (const Reference& func_decl : sym_func.declarations) { @@ -209,7 +209,7 @@ optional BuildAutoImplementForFunction(QueryDatabase* db, int dist = func_decl.range.start.line - decl.range.start.line; if (abs(dist) < abs(best_dist)) { optional def_loc = GetLsLocation( - db, working_files, *sym_func.def->definition_extent); + db, working_files, *sym_func.def->extent); if (!def_loc) continue; @@ -356,7 +356,7 @@ struct TextDocumentCodeActionHandler Out_TextDocumentCodeAction::Command command; EachWithGen(db->funcs, type.def->funcs, [&](QueryFunc& func_def) { - if (func_def.def->definition_extent) + if (func_def.def->extent) return; EnsureImplFile(db, file_id, impl_uri /*out*/, impl_file_id /*out*/); optional edit = BuildAutoImplementForFunction( @@ -398,7 +398,7 @@ struct TextDocumentCodeActionHandler case SymbolKind::Func: { QueryFunc& func = db->GetFunc(sym); - if (!func.def || func.def->definition_extent) + if (!func.def || func.def->extent) break; EnsureImplFile(db, file_id, impl_uri /*out*/, impl_file_id /*out*/); diff --git a/src/messages/text_document_definition.cc b/src/messages/text_document_definition.cc index 83077a7a..91a5be21 100644 --- a/src/messages/text_document_definition.cc +++ b/src/messages/text_document_definition.cc @@ -35,7 +35,7 @@ std::vector GetGotoDefinitionTargets(QueryDatabase* db, if (var.def && var.def->variable_type) { std::vector types = GetDeclarationsOfSymbolForGotoDefinition( db, SymbolRef(Range(), Id(var.def->variable_type->id), - SymbolKind::Type, SymbolRole::None)); + SymbolKind::Type, Role::None)); ret.insert(ret.end(), types.begin(), types.end()); } return ret; @@ -79,9 +79,9 @@ struct TextDocumentDefinitionHandler // We use spelling start and extent end because this causes vscode to // highlight the entire definition when previewing / hoving with the // mouse. - Maybe def_extent = GetDefinitionExtentOfSymbol(db, sym); - if (def_loc && def_extent) - def_loc->range.end = def_extent->range.end; + Maybe extent = GetDefinitionExtentOfSymbol(db, sym); + if (def_loc && extent) + def_loc->range.end = extent->range.end; // If the cursor is currently at or in the definition we should goto // the declaration if possible. We also want to use declarations if diff --git a/src/messages/text_document_rename.cc b/src/messages/text_document_rename.cc index 8915491d..d39b4eab 100644 --- a/src/messages/text_document_rename.cc +++ b/src/messages/text_document_rename.cc @@ -17,6 +17,8 @@ lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db, continue; QueryFileId file_id = db->GetFileId(use); + if (!file_id.HasValue()) + continue; if (path_to_edit.find(file_id) == path_to_edit.end()) { path_to_edit[file_id] = lsTextDocumentEdit(); diff --git a/src/messages/workspace_symbol.cc b/src/messages/workspace_symbol.cc index 48c25168..40733c01 100644 --- a/src/messages/workspace_symbol.cc +++ b/src/messages/workspace_symbol.cc @@ -22,8 +22,8 @@ bool InsertSymbolIntoResult(QueryDatabase* db, if (!info) return false; - Maybe location = GetDefinitionExtentOfSymbol(db, symbol); - Reference loc; + Maybe location = GetDefinitionExtentOfSymbol(db, symbol); + Use loc; if (location) loc = *location; else { diff --git a/src/query.cc b/src/query.cc index 4e66a994..82202f39 100644 --- a/src/query.cc +++ b/src/query.cc @@ -43,12 +43,8 @@ optional ToQuery(const IdMap& id_map, result.hover = type.hover; result.comments = type.comments; result.file = id_map.primary_file; - if (type.definition_spelling) - result.definition_spelling = - id_map.ToQuery(*type.definition_spelling, SymbolRole::Definition); - if (type.definition_extent) - result.definition_extent = - id_map.ToQuery(*type.definition_extent, SymbolRole::None); + result.spell = id_map.ToQuery(type.spell); + result.extent = id_map.ToQuery(type.extent); result.alias_of = id_map.ToQuery(type.alias_of); result.parents = id_map.ToQuery(type.parents); result.types = id_map.ToQuery(type.types); @@ -71,12 +67,12 @@ optional ToQuery(const IdMap& id_map, result.hover = func.hover; result.comments = func.comments; result.file = id_map.primary_file; - if (func.definition_spelling) - result.definition_spelling = - id_map.ToQuery(*func.definition_spelling, SymbolRole::Definition); - if (func.definition_extent) - result.definition_extent = - id_map.ToQuery(*func.definition_extent, SymbolRole::None); + if (func.spell) + result.spell = + id_map.ToQuery(*func.spell, Role::Definition); + if (func.extent) + result.extent = + id_map.ToQuery(*func.extent, Role::None); result.declaring_type = id_map.ToQuery(func.declaring_type); result.base = id_map.ToQuery(func.base); result.locals = id_map.ToQuery(func.locals); @@ -95,12 +91,10 @@ optional ToQuery(const IdMap& id_map, const IndexVar::Def& var) { result.hover = var.hover; result.comments = var.comments; result.file = id_map.primary_file; - if (var.definition_spelling) - result.definition_spelling = - id_map.ToQuery(*var.definition_spelling, SymbolRole::Definition); - if (var.definition_extent) - result.definition_extent = - id_map.ToQuery(*var.definition_extent, SymbolRole::None); + if (var.spell) + result.spell = id_map.ToQuery(*var.spell); + if (var.extent) + result.extent = id_map.ToQuery(*var.extent); result.variable_type = id_map.ToQuery(var.variable_type); if (result.parent_id) switch (var.parent_kind) { @@ -252,38 +246,43 @@ QueryFile::DefUpdate BuildFileDefUpdate(const IdMap& id_map, const IndexFile& in } }(); - auto add_outline = [&](Range range, RawId id, SymbolKind kind, - SymbolRole role) { - def.outline.push_back(SymbolRef(range, Id(id), kind, role)); + auto add_outline = [&](Range range, Id id, SymbolKind kind, + Role role) { + def.outline.push_back(SymbolRef(range, id, kind, role)); }; - auto add_all_symbols = [&](Range range, RawId id, SymbolKind kind, - SymbolRole role) { - def.all_symbols.push_back(SymbolRef(range, Id(id), kind, role)); + auto add_all_symbols = [&](Range range, Id id, SymbolKind kind, + Role role) { + def.all_symbols.push_back(SymbolRef(range, id, kind, role)); + }; + auto add_all_symbols_use = [&](Use use, Id id, SymbolKind kind) { + def.all_symbols.push_back( + SymbolRef(use.range, id, kind, use.role)); + }; + auto add_outline_use = [&](Use use, Id id, SymbolKind kind) { + def.outline.push_back(SymbolRef(use.range, id, kind, use.role)); }; for (const IndexType& type : indexed.types) { - RawId id = id_map.ToQuery(type.id).id; - if (type.def.definition_spelling.has_value()) - add_all_symbols(*type.def.definition_spelling, id, SymbolKind::Type, - SymbolRole::Definition); - if (type.def.definition_extent) - add_outline(*type.def.definition_extent, id, SymbolKind::Type, - SymbolRole::None); - for (const Reference& use : type.uses) + QueryTypeId id = id_map.ToQuery(type.id); + if (type.def.spell) + add_all_symbols_use(*type.def.spell, id, SymbolKind::Type); + if (type.def.extent) + add_outline_use(*type.def.extent, id, SymbolKind::Type); + for (Use use : type.uses) add_all_symbols(use.range, id, SymbolKind::Type, use.role); } for (const IndexFunc& func : indexed.funcs) { - RawId id = id_map.ToQuery(func.id).id; - if (func.def.definition_spelling.has_value()) - add_all_symbols(*func.def.definition_spelling, id, SymbolKind::Func, - SymbolRole::Definition); - if (func.def.definition_extent.has_value()) - add_outline(*func.def.definition_extent, id, - SymbolKind::Func, SymbolRole::None); + QueryFuncId id = id_map.ToQuery(func.id); + if (func.def.spell.has_value()) + add_all_symbols(*func.def.spell, id, SymbolKind::Func, + Role::Definition); + if (func.def.extent.has_value()) + add_outline(*func.def.extent, id, + SymbolKind::Func, Role::None); for (const IndexFunc::Declaration& decl : func.declarations) { add_all_symbols(decl.spelling, id, SymbolKind::Func, - SymbolRole::Declaration); - add_outline(decl.spelling, id, SymbolKind::Func, SymbolRole::Declaration); + Role::Declaration); + add_outline(decl.spelling, id, SymbolKind::Func, Role::Declaration); } for (Use caller : func.uses) { // Make ranges of implicit function calls larger (spanning one more column @@ -291,30 +290,29 @@ QueryFile::DefUpdate BuildFileDefUpdate(const IdMap& id_map, const IndexFile& in // textDocument/definition on the space/semicolon in `A a;` or `return // 42;` will take you to the constructor. Range range = caller.range; - if (caller.role & SymbolRole::Implicit) { + if (caller.role & Role::Implicit) { if (range.start.column > 0) range.start.column--; range.end.column++; } add_all_symbols(range, id, SymbolKind::Func, - caller.role | SymbolRole::CalledBy); + caller.role | Role::Call); } } for (const IndexVar& var : indexed.vars) { - if (var.def.definition_spelling) - add_all_symbols(*var.def.definition_spelling, id_map.ToQuery(var.id).id, - SymbolKind::Var, SymbolRole::Definition); - if (var.def.definition_extent.has_value()) - add_outline(*var.def.definition_extent, id_map.ToQuery(var.id).id, - SymbolKind::Var, SymbolRole::None); + QueryVarId id = id_map.ToQuery(var.id); + if (var.def.spell) + add_all_symbols_use(*var.def.spell, id, SymbolKind::Var); + if (var.def.extent) + add_outline_use(*var.def.extent, id, SymbolKind::Var); for (const Range& decl : var.declarations) { - add_all_symbols(decl, id_map.ToQuery(var.id).id, SymbolKind::Var, - SymbolRole::Definition); - add_outline(decl, id_map.ToQuery(var.id).id, SymbolKind::Var, - SymbolRole::Declaration); + add_all_symbols(decl, id_map.ToQuery(var.id), SymbolKind::Var, + Role::Definition); + add_outline(decl, id_map.ToQuery(var.id), SymbolKind::Var, + Role::Declaration); } - for (auto& use : var.uses) - add_all_symbols(use.range, id_map.ToQuery(var.id).id, SymbolKind::Var, + for (Use use : var.uses) + add_all_symbols(use.range, id_map.ToQuery(var.id), SymbolKind::Var, use.role); } @@ -432,7 +430,7 @@ IdMap::IdMap(QueryDatabase* query_db, const IdCache& local_ids) *GetQueryVarIdFromUsr(query_db, entry.second, true); } -Use IdMap::ToQuery(Range range, SymbolRole role) const { +Use IdMap::ToQuery(Range range, Role role) const { return Use(range, primary_file, SymbolKind:: File, role); } QueryTypeId IdMap::ToQuery(IndexTypeId id) const { @@ -481,13 +479,13 @@ Use IdMap::ToQuery(Use use) const { Use IdMap::ToQuery(IndexFunc::Declaration decl) const { // TODO: expose more than just QueryLocation. return Use(decl.spelling, primary_file, SymbolKind::File, - SymbolRole::Declaration); + Role::Declaration); } std::vector IdMap::ToQuery(const std::vector& a) const { std::vector ret; ret.reserve(a.size()); for (auto& x : a) - ret.push_back(ToQuery(x, SymbolRole::Reference)); + ret.push_back(ToQuery(x, Role::Reference)); return ret; } @@ -547,7 +545,7 @@ IndexUpdate::IndexUpdate(const IdMap& previous_id_map, previous_file.types, current_file.types, /*onRemoved:*/ [this, &previous_id_map](IndexType* type) { - if (type->def.definition_spelling) + if (type->def.spell) types_removed.push_back(type->usr); else { if (!type->derived.empty()) @@ -609,7 +607,7 @@ IndexUpdate::IndexUpdate(const IdMap& previous_id_map, previous_file.funcs, current_file.funcs, /*onRemoved:*/ [this, &previous_id_map](IndexFunc* func) { - if (func->def.definition_spelling) { + if (func->def.spell) { funcs_removed.push_back(func->usr); } else { if (!func->declarations.empty()) @@ -671,7 +669,7 @@ IndexUpdate::IndexUpdate(const IdMap& previous_id_map, previous_file.vars, current_file.vars, /*onRemoved:*/ [this, &previous_id_map](IndexVar* var) { - if (var->def.definition_spelling) { + if (var->def.spell) { vars_removed.push_back(var->usr); } else { if (!var->declarations.empty()) @@ -894,8 +892,8 @@ void QueryDatabase::ImportOrUpdate( QueryType& existing = types[it->second.id]; // Keep the existing definition if it is higher quality. - if (!(existing.def && existing.def->definition_spelling && - !def.value.definition_spelling)) { + if (!(existing.def && existing.def->spell && + !def.value.spell)) { existing.def = std::move(def.value); UpdateSymbols(&existing.symbol_idx, SymbolKind::Type, it->second.id); @@ -917,8 +915,8 @@ void QueryDatabase::ImportOrUpdate( QueryFunc& existing = funcs[it->second.id]; // Keep the existing definition if it is higher quality. - if (!(existing.def && existing.def->definition_spelling && - !def.value.definition_spelling)) { + if (!(existing.def && existing.def->spell && + !def.value.spell)) { existing.def = std::move(def.value); UpdateSymbols(&existing.symbol_idx, SymbolKind::Func, it->second.id); @@ -939,8 +937,8 @@ void QueryDatabase::ImportOrUpdate(std::vector&& updates) { QueryVar& existing = vars[it->second.id]; // Keep the existing definition if it is higher quality. - if (!(existing.def && existing.def->definition_spelling && - !def.value.definition_spelling)) { + if (!(existing.def && existing.def->spell && + !def.value.spell)) { existing.def = std::move(def.value); if (!def.value.is_local()) UpdateSymbols(&existing.symbol_idx, SymbolKind::Var, @@ -1023,11 +1021,11 @@ TEST_SUITE("query") { IndexFile current("foo.cc", ""); previous.Resolve(previous.ToTypeId(HashUsr("usr1"))) - ->def.definition_spelling = Range(Position(1, 0)); + ->def.spell = Use(Range(Position(1, 0)), Id(), SymbolKind::File, Role::None); previous.Resolve(previous.ToFuncId(HashUsr("usr2"))) - ->def.definition_spelling = Range(Position(2, 0)); + ->def.spell = Range(Position(2, 0)); previous.Resolve(previous.ToVarId(HashUsr("usr3"))) - ->def.definition_spelling = Range(Position(3, 0)); + ->def.spell = Use(Range(Position(3, 0)), Id(), SymbolKind::File, Role::None); IndexUpdate update = GetDelta(previous, current); @@ -1044,7 +1042,7 @@ TEST_SUITE("query") { ->uses.push_back(Reference{Range(Position(1, 0))}); previous.Resolve(previous.ToFuncId(HashUsr("usr2"))) ->uses.push_back(Use(Range(Position(2, 0)), Id(0), - SymbolKind::Func, SymbolRole::None)); + SymbolKind::Func, Role::None)); previous.Resolve(previous.ToVarId(HashUsr("usr3"))) ->uses.push_back(Reference{Range(Position(3, 0))}); @@ -1063,9 +1061,9 @@ TEST_SUITE("query") { IndexFunc* cf = current.Resolve(current.ToFuncId(HashUsr("usr"))); pf->uses.push_back(Use(Range(Position(1, 0)), Id(0), - SymbolKind::Func, SymbolRole::None)); + SymbolKind::Func, Role::None)); cf->uses.push_back(Use(Range(Position(2, 0)), Id(0), - SymbolKind::Func, SymbolRole::None)); + SymbolKind::Func, Role::None)); IndexUpdate update = GetDelta(previous, current); @@ -1108,13 +1106,13 @@ TEST_SUITE("query") { IndexFunc* pf = previous.Resolve(previous.ToFuncId(HashUsr("usr"))); IndexFunc* cf = current.Resolve(current.ToFuncId(HashUsr("usr"))); pf->uses.push_back(Use(Range(Position(1, 0)), Id(0), - SymbolKind::Func, SymbolRole::None)); + SymbolKind::Func, Role::None)); pf->uses.push_back(Use(Range(Position(2, 0)), Id(0), - SymbolKind::Func, SymbolRole::None)); + SymbolKind::Func, Role::None)); cf->uses.push_back(Use(Range(Position(4, 0)), Id(0), - SymbolKind::Func, SymbolRole::None)); + SymbolKind::Func, Role::None)); cf->uses.push_back(Use(Range(Position(5, 0)), Id(0), - SymbolKind::Func, SymbolRole::None)); + SymbolKind::Func, Role::None)); QueryDatabase db; IdMap previous_map(&db, previous.id_cache); diff --git a/src/query.h b/src/query.h index e14b127c..7eb60022 100644 --- a/src/query.h +++ b/src/query.h @@ -363,7 +363,7 @@ struct IdMap { QueryFuncId ToQuery(IndexFuncId id) const; QueryVarId ToQuery(IndexVarId id) const; SymbolRef ToQuery(SymbolRef ref) const; - Use ToQuery(Range range, SymbolRole role) const; + Use ToQuery(Range range, Role role) const; Use ToQuery(Reference ref) const; Use ToQuery(Use ref) const; Use ToQuery(IndexFunc::Declaration decl) const; diff --git a/src/query_utils.cc b/src/query_utils.cc index 98cea0ff..73c4c8d2 100644 --- a/src/query_utils.cc +++ b/src/query_utils.cc @@ -22,7 +22,7 @@ Maybe GetDefinitionSpellingOfSymbol(QueryDatabase* db, QueryFuncId id) { QueryFunc& func = db->funcs[id.id]; if (func.def) - return func.def->definition_spelling; + return func.def->spell; return nullopt; } @@ -32,19 +32,19 @@ Maybe GetDefinitionSpellingOfSymbol(QueryDatabase* db, case SymbolKind::Type: { QueryType& type = db->GetType(sym); if (type.def) - return *type.def->definition_spelling; + return *type.def->spell; break; } case SymbolKind::Func: { QueryFunc& func = db->GetFunc(sym); if (func.def) - return func.def->definition_spelling; + return func.def->spell; break; } case SymbolKind::Var: { QueryVar& var = db->GetVar(sym); if (var.def) - return var.def->definition_spelling; + return *var.def->spell; break; } case SymbolKind::File: @@ -56,28 +56,28 @@ Maybe GetDefinitionSpellingOfSymbol(QueryDatabase* db, return nullopt; } -Maybe GetDefinitionExtentOfSymbol(QueryDatabase* db, SymbolRef sym) { +Maybe GetDefinitionExtentOfSymbol(QueryDatabase* db, SymbolRef sym) { switch (sym.kind) { case SymbolKind::Type: { QueryType& type = db->GetType(sym); if (type.def) - return type.def->definition_extent; + return type.def->extent; break; } case SymbolKind::Func: { QueryFunc& func = db->GetFunc(sym); if (func.def) - return func.def->definition_extent; + return Use(*func.def->extent); break; } case SymbolKind::Var: { QueryVar& var = db->GetVar(sym); if (var.def) - return var.def->definition_extent; + return var.def->extent; break; } case SymbolKind::File: - return sym; + return Use(Reference(sym)); case SymbolKind::Invalid: { assert(false && "unexpected"); break; @@ -91,22 +91,22 @@ Maybe GetDeclarationFileForSymbol(QueryDatabase* db, switch (sym.kind) { case SymbolKind::Type: { QueryType& type = db->GetType(sym); - if (type.def && type.def->definition_spelling) - return db->GetFileId(*type.def->definition_spelling); + if (type.def && type.def->spell) + return db->GetFileId(*type.def->spell); break; } case SymbolKind::Func: { QueryFunc& func = db->GetFunc(sym); if (!func.declarations.empty()) return db->GetFileId(func.declarations[0]); - if (func.def && func.def->definition_spelling) - return db->GetFileId(*func.def->definition_spelling); + if (func.def && func.def->spell) + return db->GetFileId(*func.def->spell); break; } case SymbolKind::Var: { QueryVar& var = db->GetVar(sym); - if (var.def && var.def->definition_spelling) - return db->GetFileId(*var.def->definition_spelling); + if (var.def && var.def->spell) + return db->GetFileId(*var.def->spell); break; } case SymbolKind::File: @@ -125,8 +125,8 @@ std::vector ToUses(QueryDatabase* db, ret.reserve(ids.size()); for (auto id : ids) { QueryFunc& func = db->funcs[id.id]; - if (func.def && func.def->definition_spelling) - ret.push_back(*func.def->definition_spelling); + if (func.def && func.def->spell) + ret.push_back(*func.def->spell); else if (func.declarations.size()) ret.push_back(func.declarations[0]); } @@ -139,8 +139,8 @@ std::vector ToUses(QueryDatabase* db, ret.reserve(ids.size()); for (auto id : ids) { QueryType& type = db->types[id.id]; - if (type.def && type.def->definition_spelling) - ret.push_back(*type.def->definition_spelling); + if (type.def && type.def->spell) + ret.push_back(*type.def->spell); } return ret; } @@ -150,8 +150,8 @@ std::vector ToUses(QueryDatabase* db, const std::vector& ids) { ret.reserve(ids.size()); for (auto id : ids) { QueryVar& var = db->vars[id.id]; - if (var.def && var.def->definition_spelling) - ret.push_back(*var.def->definition_spelling); + if (var.def && var.def->spell) + ret.push_back(*var.def->spell); else if (var.declarations.size()) ret.push_back(var.declarations[0]); } @@ -165,8 +165,8 @@ std::vector GetUsesOfSymbol(QueryDatabase* db, case SymbolKind::Type: { QueryType& type = db->types[sym.Idx()]; std::vector ret = type.uses; - if (include_decl && type.def && type.def->definition_spelling) - ret.push_back(*type.def->definition_spelling); + if (include_decl && type.def && type.def->spell) + ret.push_back(*type.def->spell); return ret; } case SymbolKind::Func: { @@ -175,8 +175,8 @@ std::vector GetUsesOfSymbol(QueryDatabase* db, std::vector ret = func.uses; if (include_decl) { AddRange(&ret, func.declarations); - if (func.def && func.def->definition_spelling) - ret.push_back(*func.def->definition_spelling); + if (func.def && func.def->spell) + ret.push_back(*func.def->spell); } return ret; } @@ -184,8 +184,8 @@ std::vector GetUsesOfSymbol(QueryDatabase* db, QueryVar& var = db->vars[sym.Idx()]; std::vector ret = var.uses; if (include_decl) { - if (var.def && var.def->definition_spelling) - ret.push_back(*var.def->definition_spelling); + if (var.def && var.def->spell) + ret.push_back(*var.def->spell); ret.insert(ret.end(), var.declarations.begin(), var.declarations.end()); } return ret; @@ -209,7 +209,7 @@ std::vector GetDeclarationsOfSymbolForGotoDefinition( // type from within the type definition. QueryType& type = db->GetType(sym); if (type.def) { - Maybe def = type.def->definition_spelling; + Maybe def = type.def->spell; if (def) return {*def}; } diff --git a/src/query_utils.h b/src/query_utils.h index de2c03da..24ad9805 100644 --- a/src/query_utils.h +++ b/src/query_utils.h @@ -11,7 +11,7 @@ Maybe GetDefinitionSpellingOfSymbol(QueryDatabase* db, QueryFuncId id); Maybe GetDefinitionSpellingOfSymbol(QueryDatabase* db, SymbolRef sym); -Maybe GetDefinitionExtentOfSymbol(QueryDatabase* db, SymbolRef sym); +Maybe GetDefinitionExtentOfSymbol(QueryDatabase* db, SymbolRef sym); Maybe GetDeclarationFileForSymbol(QueryDatabase* db, SymbolRef sym); diff --git a/src/serializer.cc b/src/serializer.cc index 48d77e01..3cb45aad 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -210,8 +210,8 @@ void Reflect(TVisitor& visitor, IndexType& value) { ReflectShortName(visitor, value.def); REFLECT_MEMBER2("kind", value.def.kind); ReflectHoverAndComments(visitor, value.def); - REFLECT_MEMBER2("definition_spelling", value.def.definition_spelling); - REFLECT_MEMBER2("definition_extent", value.def.definition_extent); + REFLECT_MEMBER2("spell", value.def.spell); + REFLECT_MEMBER2("extent", value.def.extent); REFLECT_MEMBER2("alias_of", value.def.alias_of); REFLECT_MEMBER2("parents", value.def.parents); REFLECT_MEMBER2("derived", value.derived); @@ -234,8 +234,8 @@ void Reflect(TVisitor& visitor, IndexFunc& value) { REFLECT_MEMBER2("storage", value.def.storage); ReflectHoverAndComments(visitor, value.def); REFLECT_MEMBER2("declarations", value.declarations); - REFLECT_MEMBER2("definition_spelling", value.def.definition_spelling); - REFLECT_MEMBER2("definition_extent", value.def.definition_extent); + REFLECT_MEMBER2("spell", value.def.spell); + REFLECT_MEMBER2("extent", value.def.extent); REFLECT_MEMBER2("declaring_type", value.def.declaring_type); REFLECT_MEMBER2("base", value.def.base); REFLECT_MEMBER2("derived", value.derived); @@ -254,8 +254,8 @@ void Reflect(TVisitor& visitor, IndexVar& value) { ReflectShortName(visitor, value.def); ReflectHoverAndComments(visitor, value.def); REFLECT_MEMBER2("declarations", value.declarations); - REFLECT_MEMBER2("definition_spelling", value.def.definition_spelling); - REFLECT_MEMBER2("definition_extent", value.def.definition_extent); + REFLECT_MEMBER2("spell", value.def.spell); + REFLECT_MEMBER2("extent", value.def.extent); REFLECT_MEMBER2("variable_type", value.def.variable_type); REFLECT_MEMBER2("uses", value.uses); REFLECT_MEMBER2("parent_id", value.def.parent_id);