Add {Index,Query}Family to simplify template parameters

This commit is contained in:
Fangrui Song 2018-02-10 00:06:45 -08:00
parent e7c90b62b5
commit 749ecf0faa
3 changed files with 55 additions and 109 deletions

View File

@ -607,18 +607,8 @@ void OnIndexReference_Function(IndexFile* db,
const int IndexFile::kMajorVersion = 11; const int IndexFile::kMajorVersion = 11;
const int IndexFile::kMinorVersion = 3; const int IndexFile::kMinorVersion = 3;
IndexFile::IndexFile(const std::string& path, IndexFile::IndexFile(const std::string& path, const std::string& contents)
const std::string& contents) : id_cache(path), path(path), file_contents(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));
}
// TODO: Optimize for const char*? // TODO: Optimize for const char*?
IndexTypeId IndexFile::ToTypeId(Usr usr) { IndexTypeId IndexFile::ToTypeId(Usr usr) {

View File

@ -120,11 +120,16 @@ void Reflect(Writer& visitor, IndexFuncRef& value);
void Reflect(Reader& visitor, Reference& value); void Reflect(Reader& visitor, Reference& value);
void Reflect(Writer& visitor, Reference& value); void Reflect(Writer& visitor, Reference& value);
template <typename FileId, struct IndexFamily {
typename TypeId, using FileId = Id<IndexFile>;
typename FuncId, using FuncId = Id<IndexFunc>;
typename VarId, using TypeId = Id<IndexType>;
typename Range> using VarId = Id<IndexVar>;
using Range = Range;
using FuncRef = IndexFuncRef;
};
template <typename F>
struct TypeDefDefinitionData { struct TypeDefDefinitionData {
// General metadata. // General metadata.
std::string detailed_name; std::string detailed_name;
@ -140,21 +145,21 @@ struct TypeDefDefinitionData {
// It's also difficult to identify a `class Foo;` statement with the clang // 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 // indexer API (it's doable using cursor AST traversal), so we don't bother
// supporting the feature. // supporting the feature.
Maybe<Range> definition_spelling; Maybe<typename F::Range> definition_spelling;
Maybe<Range> definition_extent; Maybe<typename F::Range> definition_extent;
// Immediate parent types. // Immediate parent types.
std::vector<TypeId> parents; std::vector<typename F::TypeId> parents;
// Types, functions, and variables defined in this type. // Types, functions, and variables defined in this type.
std::vector<TypeId> types; std::vector<typename F::TypeId> types;
std::vector<FuncId> funcs; std::vector<typename F::FuncId> funcs;
std::vector<VarId> vars; std::vector<typename F::VarId> vars;
FileId file; typename F::FileId file;
// If set, then this is the same underlying type as the given value (ie, this // If set, then this is the same underlying type as the given value (ie, this
// type comes from a using or typedef statement). // type comes from a using or typedef statement).
Maybe<TypeId> alias_of; Maybe<typename F::TypeId> alias_of;
int16_t short_name_offset = 0; int16_t short_name_offset = 0;
int16_t short_name_size = 0; int16_t short_name_size = 0;
@ -166,7 +171,7 @@ struct TypeDefDefinitionData {
definition_extent == o.definition_extent && alias_of == o.alias_of && definition_extent == o.definition_extent && alias_of == o.alias_of &&
parents == o.parents && types == o.types && funcs == o.funcs && parents == o.parents && types == o.types && funcs == o.funcs &&
vars == o.vars && kind == o.kind && hover == o.hover && 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 { bool operator!=(const TypeDefDefinitionData& o) const {
return !(*this == o); return !(*this == o);
@ -177,15 +182,8 @@ struct TypeDefDefinitionData {
short_name_size); short_name_size);
} }
}; };
template <typename TVisitor, template <typename TVisitor, typename Family>
typename FileId, void Reflect(TVisitor& visitor, TypeDefDefinitionData<Family>& value) {
typename TypeId,
typename FuncId,
typename VarId,
typename Range>
void Reflect(
TVisitor& visitor,
TypeDefDefinitionData<FileId, TypeId, FuncId, VarId, Range>& value) {
REFLECT_MEMBER_START(); REFLECT_MEMBER_START();
REFLECT_MEMBER(detailed_name); REFLECT_MEMBER(detailed_name);
REFLECT_MEMBER(short_name_offset); REFLECT_MEMBER(short_name_offset);
@ -205,11 +203,7 @@ void Reflect(
} }
struct IndexType { struct IndexType {
using Def = TypeDefDefinitionData<IndexFileId, using Def = TypeDefDefinitionData<IndexFamily>;
IndexTypeId,
IndexFuncId,
IndexVarId,
Range>;
Usr usr; Usr usr;
IndexTypeId id; IndexTypeId id;
@ -233,32 +227,27 @@ struct IndexType {
}; };
MAKE_HASHABLE(IndexType, t.id); MAKE_HASHABLE(IndexType, t.id);
template <typename FileId, template <typename F>
typename TypeId,
typename FuncId,
typename VarId,
typename FuncRef,
typename Range>
struct FuncDefDefinitionData { struct FuncDefDefinitionData {
// General metadata. // General metadata.
std::string detailed_name; std::string detailed_name;
std::string hover; std::string hover;
std::string comments; std::string comments;
Maybe<Range> definition_spelling; Maybe<typename F::Range> definition_spelling;
Maybe<Range> definition_extent; Maybe<typename F::Range> definition_extent;
// Method this method overrides. // Method this method overrides.
std::vector<FuncId> base; std::vector<typename F::FuncId> base;
// Local variables defined in this function. // Local variables defined in this function.
std::vector<VarId> locals; std::vector<typename F::VarId> locals;
// Functions that this function calls. // Functions that this function calls.
std::vector<FuncRef> callees; std::vector<typename F::FuncRef> callees;
FileId file; typename F::FileId file;
// Type which declares this one (ie, it is a method) // Type which declares this one (ie, it is a method)
Maybe<TypeId> declaring_type; Maybe<typename F::TypeId> declaring_type;
int16_t short_name_offset = 0; int16_t short_name_offset = 0;
int16_t short_name_size = 0; int16_t short_name_size = 0;
ClangSymbolKind kind = ClangSymbolKind::Unknown; ClangSymbolKind kind = ClangSymbolKind::Unknown;
@ -282,17 +271,8 @@ struct FuncDefDefinitionData {
} }
}; };
template <typename TVisitor, template <typename TVisitor, typename Family>
typename FileId, void Reflect(TVisitor& visitor, FuncDefDefinitionData<Family>& value) {
typename TypeId,
typename FuncId,
typename VarId,
typename FuncRef,
typename Range>
void Reflect(
TVisitor& visitor,
FuncDefDefinitionData<FileId, TypeId, FuncId, VarId, FuncRef, Range>&
value) {
REFLECT_MEMBER_START(); REFLECT_MEMBER_START();
REFLECT_MEMBER(detailed_name); REFLECT_MEMBER(detailed_name);
REFLECT_MEMBER(short_name_offset); REFLECT_MEMBER(short_name_offset);
@ -312,12 +292,7 @@ void Reflect(
} }
struct IndexFunc { struct IndexFunc {
using Def = FuncDefDefinitionData<IndexFileId, using Def = FuncDefDefinitionData<IndexFamily>;
IndexTypeId,
IndexFuncId,
IndexVarId,
IndexFuncRef,
Range>;
Usr usr; Usr usr;
IndexFuncId id; IndexFuncId id;
@ -362,11 +337,7 @@ MAKE_REFLECT_STRUCT(IndexFunc::Declaration,
content, content,
param_spellings); param_spellings);
template <typename FileId, template <typename F>
typename TypeId,
typename FuncId,
typename VarId,
typename Range>
struct VarDefDefinitionData { struct VarDefDefinitionData {
// General metadata. // General metadata.
std::string detailed_name; std::string detailed_name;
@ -374,12 +345,12 @@ struct VarDefDefinitionData {
std::string comments; std::string comments;
// TODO: definitions should be a list of ranges, since there can be more // TODO: definitions should be a list of ranges, since there can be more
// than one - when?? // than one - when??
Maybe<Range> definition_spelling; Maybe<typename F::Range> definition_spelling;
Maybe<Range> definition_extent; Maybe<typename F::Range> definition_extent;
FileId file; typename F::FileId file;
// Type of the variable. // Type of the variable.
Maybe<TypeId> variable_type; Maybe<typename F::TypeId> variable_type;
// Function/type which declares this one. // Function/type which declares this one.
Maybe<Id<void>> parent_id; Maybe<Id<void>> parent_id;
@ -416,15 +387,8 @@ struct VarDefDefinitionData {
} }
}; };
template <typename TVisitor, template <typename TVisitor, typename Family>
typename FileId, void Reflect(TVisitor& visitor, VarDefDefinitionData<Family>& value) {
typename TypeId,
typename FuncId,
typename VarId,
typename Range>
void Reflect(
TVisitor& visitor,
VarDefDefinitionData<FileId, TypeId, FuncId, VarId, Range>& value) {
REFLECT_MEMBER_START(); REFLECT_MEMBER_START();
REFLECT_MEMBER(detailed_name); REFLECT_MEMBER(detailed_name);
REFLECT_MEMBER(short_name_size); REFLECT_MEMBER(short_name_size);
@ -443,11 +407,7 @@ void Reflect(
} }
struct IndexVar { struct IndexVar {
using Def = VarDefDefinitionData<IndexFileId, using Def = VarDefDefinitionData<IndexFamily>;
IndexTypeId,
IndexFuncId,
IndexVarId,
Range>;
Usr usr; Usr usr;
IndexVarId id; IndexVarId id;

View File

@ -126,6 +126,15 @@ void Reflect(TVisitor& visitor, WithFileContent<T>& value) {
REFLECT_MEMBER_END(); REFLECT_MEMBER_END();
} }
struct QueryFamily {
using FileId = Id<QueryFile>;
using FuncId = Id<QueryFunc>;
using TypeId = Id<QueryType>;
using VarId = Id<QueryVar>;
using Range = Reference;
using FuncRef = QueryFuncRef;
};
struct QueryFile { struct QueryFile {
struct Def { struct Def {
std::string path; std::string path;
@ -162,11 +171,7 @@ MAKE_REFLECT_STRUCT(QueryFile::Def,
dependencies); dependencies);
struct QueryType { struct QueryType {
using Def = TypeDefDefinitionData<QueryFileId, using Def = TypeDefDefinitionData<QueryFamily>;
QueryTypeId,
QueryFuncId,
QueryVarId,
Reference>;
using DefUpdate = WithUsr<Def>; using DefUpdate = WithUsr<Def>;
using DerivedUpdate = MergeableUpdate<QueryTypeId, QueryTypeId>; using DerivedUpdate = MergeableUpdate<QueryTypeId, QueryTypeId>;
using InstancesUpdate = MergeableUpdate<QueryTypeId, QueryVarId>; using InstancesUpdate = MergeableUpdate<QueryTypeId, QueryVarId>;
@ -183,12 +188,7 @@ struct QueryType {
}; };
struct QueryFunc { struct QueryFunc {
using Def = FuncDefDefinitionData<QueryFileId, using Def = FuncDefDefinitionData<QueryFamily>;
QueryTypeId,
QueryFuncId,
QueryVarId,
QueryFuncRef,
Reference>;
using DefUpdate = WithUsr<Def>; using DefUpdate = WithUsr<Def>;
using DeclarationsUpdate = MergeableUpdate<QueryFuncId, Reference>; using DeclarationsUpdate = MergeableUpdate<QueryFuncId, Reference>;
using DerivedUpdate = MergeableUpdate<QueryFuncId, QueryFuncId>; using DerivedUpdate = MergeableUpdate<QueryFuncId, QueryFuncId>;
@ -205,11 +205,7 @@ struct QueryFunc {
}; };
struct QueryVar { struct QueryVar {
using Def = VarDefDefinitionData<QueryFileId, using Def = VarDefDefinitionData<QueryFamily>;
QueryTypeId,
QueryFuncId,
QueryVarId,
Reference>;
using DefUpdate = WithUsr<Def>; using DefUpdate = WithUsr<Def>;
using DeclarationsUpdate = MergeableUpdate<QueryVarId, Reference>; using DeclarationsUpdate = MergeableUpdate<QueryVarId, Reference>;
using UsesUpdate = MergeableUpdate<QueryVarId, Reference>; using UsesUpdate = MergeableUpdate<QueryVarId, Reference>;