From 749ecf0faa65276d81867c118eb61d1557a850cc Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 10 Feb 2018 00:06:45 -0800 Subject: [PATCH] Add {Index,Query}Family to simplify template parameters --- src/indexer.cc | 14 +----- src/indexer.h | 122 +++++++++++++++++-------------------------------- src/query.h | 28 +++++------- 3 files changed, 55 insertions(+), 109 deletions(-) diff --git a/src/indexer.cc b/src/indexer.cc index e0d7d8cb..6fd32fe7 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -607,18 +607,8 @@ void OnIndexReference_Function(IndexFile* db, const int IndexFile::kMajorVersion = 11; const int IndexFile::kMinorVersion = 3; -IndexFile::IndexFile(const std::string& path, - const std::string& contents) - : id_cache(path), path(path), file_contents(contents) { - // TODO: Reconsider if we should still be reusing the same id_cache. - // Preallocate any existing resolved ids. - for (const auto& entry : id_cache.usr_to_type_id) - types.push_back(IndexType(entry.second, entry.first)); - for (const auto& entry : id_cache.usr_to_func_id) - funcs.push_back(IndexFunc(entry.second, entry.first)); - for (const auto& entry : id_cache.usr_to_var_id) - vars.push_back(IndexVar(entry.second, entry.first)); -} +IndexFile::IndexFile(const std::string& path, const std::string& contents) + : id_cache(path), path(path), file_contents(contents) {} // TODO: Optimize for const char*? IndexTypeId IndexFile::ToTypeId(Usr usr) { diff --git a/src/indexer.h b/src/indexer.h index 62ee2df7..21549f8f 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -120,11 +120,16 @@ void Reflect(Writer& visitor, IndexFuncRef& value); void Reflect(Reader& visitor, Reference& value); void Reflect(Writer& visitor, Reference& value); -template +struct IndexFamily { + using FileId = Id; + using FuncId = Id; + using TypeId = Id; + using VarId = Id; + using Range = Range; + using FuncRef = IndexFuncRef; +}; + +template struct TypeDefDefinitionData { // General metadata. std::string detailed_name; @@ -140,21 +145,21 @@ 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 definition_spelling; + Maybe definition_extent; // Immediate parent types. - std::vector parents; + std::vector parents; // Types, functions, and variables defined in this type. - std::vector types; - std::vector funcs; - std::vector vars; + std::vector types; + std::vector funcs; + std::vector vars; - FileId file; + typename F::FileId file; // If set, then this is the same underlying type as the given value (ie, this // type comes from a using or typedef statement). - Maybe alias_of; + Maybe alias_of; int16_t short_name_offset = 0; int16_t short_name_size = 0; @@ -166,7 +171,7 @@ struct TypeDefDefinitionData { definition_extent == o.definition_extent && alias_of == o.alias_of && parents == o.parents && types == o.types && funcs == o.funcs && vars == o.vars && kind == o.kind && hover == o.hover && - kind == o.kind && hover == o.hover && comments == o.comments; + comments == o.comments; } bool operator!=(const TypeDefDefinitionData& o) const { return !(*this == o); @@ -177,15 +182,8 @@ struct TypeDefDefinitionData { short_name_size); } }; -template -void Reflect( - TVisitor& visitor, - TypeDefDefinitionData& value) { +template +void Reflect(TVisitor& visitor, TypeDefDefinitionData& value) { REFLECT_MEMBER_START(); REFLECT_MEMBER(detailed_name); REFLECT_MEMBER(short_name_offset); @@ -205,11 +203,7 @@ void Reflect( } struct IndexType { - using Def = TypeDefDefinitionData; + using Def = TypeDefDefinitionData; Usr usr; IndexTypeId id; @@ -233,32 +227,27 @@ struct IndexType { }; MAKE_HASHABLE(IndexType, t.id); -template +template struct FuncDefDefinitionData { // General metadata. std::string detailed_name; std::string hover; std::string comments; - Maybe definition_spelling; - Maybe definition_extent; + Maybe definition_spelling; + Maybe definition_extent; // Method this method overrides. - std::vector base; + std::vector base; // Local variables defined in this function. - std::vector locals; + std::vector locals; // Functions that this function calls. - std::vector callees; + std::vector callees; - FileId file; + typename F::FileId file; // Type which declares this one (ie, it is a method) - Maybe declaring_type; + Maybe declaring_type; int16_t short_name_offset = 0; int16_t short_name_size = 0; ClangSymbolKind kind = ClangSymbolKind::Unknown; @@ -282,17 +271,8 @@ struct FuncDefDefinitionData { } }; -template -void Reflect( - TVisitor& visitor, - FuncDefDefinitionData& - value) { +template +void Reflect(TVisitor& visitor, FuncDefDefinitionData& value) { REFLECT_MEMBER_START(); REFLECT_MEMBER(detailed_name); REFLECT_MEMBER(short_name_offset); @@ -312,12 +292,7 @@ void Reflect( } struct IndexFunc { - using Def = FuncDefDefinitionData; + using Def = FuncDefDefinitionData; Usr usr; IndexFuncId id; @@ -362,11 +337,7 @@ MAKE_REFLECT_STRUCT(IndexFunc::Declaration, content, param_spellings); -template +template struct VarDefDefinitionData { // General metadata. std::string detailed_name; @@ -374,12 +345,12 @@ 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 definition_spelling; + Maybe definition_extent; - FileId file; + typename F::FileId file; // Type of the variable. - Maybe variable_type; + Maybe variable_type; // Function/type which declares this one. Maybe> parent_id; @@ -416,15 +387,8 @@ struct VarDefDefinitionData { } }; -template -void Reflect( - TVisitor& visitor, - VarDefDefinitionData& value) { +template +void Reflect(TVisitor& visitor, VarDefDefinitionData& value) { REFLECT_MEMBER_START(); REFLECT_MEMBER(detailed_name); REFLECT_MEMBER(short_name_size); @@ -443,11 +407,7 @@ void Reflect( } struct IndexVar { - using Def = VarDefDefinitionData; + using Def = VarDefDefinitionData; Usr usr; IndexVarId id; diff --git a/src/query.h b/src/query.h index d6575b8b..3b64bc05 100644 --- a/src/query.h +++ b/src/query.h @@ -126,6 +126,15 @@ void Reflect(TVisitor& visitor, WithFileContent& value) { REFLECT_MEMBER_END(); } +struct QueryFamily { + using FileId = Id; + using FuncId = Id; + using TypeId = Id; + using VarId = Id; + using Range = Reference; + using FuncRef = QueryFuncRef; +}; + struct QueryFile { struct Def { std::string path; @@ -162,11 +171,7 @@ MAKE_REFLECT_STRUCT(QueryFile::Def, dependencies); struct QueryType { - using Def = TypeDefDefinitionData; + using Def = TypeDefDefinitionData; using DefUpdate = WithUsr; using DerivedUpdate = MergeableUpdate; using InstancesUpdate = MergeableUpdate; @@ -183,12 +188,7 @@ struct QueryType { }; struct QueryFunc { - using Def = FuncDefDefinitionData; + using Def = FuncDefDefinitionData; using DefUpdate = WithUsr; using DeclarationsUpdate = MergeableUpdate; using DerivedUpdate = MergeableUpdate; @@ -205,11 +205,7 @@ struct QueryFunc { }; struct QueryVar { - using Def = VarDefDefinitionData; + using Def = VarDefDefinitionData; using DefUpdate = WithUsr; using DeclarationsUpdate = MergeableUpdate; using UsesUpdate = MergeableUpdate;