#pragma once #include "position.h" #include "serializer.h" #include "utils.h" #include "libclangmm/clangmm.h" #include "libclangmm/Utility.h" #include #include #include #include #include #include #include #include #include #include #include struct IndexedTypeDef; struct IndexedFuncDef; struct IndexedVarDef; using namespace std::experimental; template struct Id { uint64_t id; Id() : id(0) {} // Needed for containers. Do not use directly. explicit Id(uint64_t id) : id(id) {} bool operator==(const Id& other) const { return id == other.id; } bool operator<(const Id& other) const { return id < other.id; } }; namespace std { template struct hash> { size_t operator()(const Id& k) const { return hash()(k.id); } }; } template bool operator==(const Id& a, const Id& b) { assert(a.group == b.group && "Cannot compare Ids from different groups"); return a.id == b.id; } using IndexTypeId = Id; using IndexFuncId = Id; using IndexVarId = Id; struct IdCache; template struct Ref { Id id; Range loc; Ref() {} // For serialization. Ref(Id id, Range loc) : id(id), loc(loc) {} bool operator==(const Ref& other) { return id == other.id && loc == other.loc; } bool operator!=(const Ref& other) { return !(*this == other); } bool operator<(const Ref& other) const { return id < other.id && loc < other.loc; } }; template bool operator==(const Ref& a, const Ref& b) { return a.id == b.id && a.loc == b.loc; } template bool operator!=(const Ref& a, const Ref& b) { return !(a == b); } using TypeRef = Ref; using FuncRef = Ref; using VarRef = Ref; // TODO: skip as much forward-processing as possible when |is_system_def| is // set to false. // TODO: Either eliminate the defs created as a by-product of cross-referencing, // or do not emit things we don't have definitions for. template struct TypeDefDefinitionData { // General metadata. std::string usr; std::string short_name; std::string qualified_name; // While a class/type can technically have a separate declaration/definition, // it doesn't really happen in practice. The declaration never contains // comments or insightful information. The user always wants to jump from // the declaration to the definition - never the other way around like in // functions and (less often) variables. // // 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. optional definition_spelling; optional definition_extent; // If set, then this is the same underlying type as the given value (ie, this // type comes from a using or typedef statement). optional alias_of; // Immediate parent types. std::vector parents; // Types, functions, and variables defined in this type. std::vector types; std::vector funcs; std::vector vars; TypeDefDefinitionData() {} // For reflection. TypeDefDefinitionData(const std::string& usr) : usr(usr) {} bool operator==(const TypeDefDefinitionData& other) const { return usr == other.usr && short_name == other.short_name && qualified_name == other.qualified_name && definition_spelling == other.definition_spelling && definition_extent == other.definition_extent && alias_of == other.alias_of && parents == other.parents && types == other.types && funcs == other.funcs && vars == other.vars; } bool operator!=(const TypeDefDefinitionData& other) const { return !(*this == other); } }; template void Reflect(TVisitor& visitor, TypeDefDefinitionData& value) { REFLECT_MEMBER_START(); REFLECT_MEMBER(usr); REFLECT_MEMBER(short_name); REFLECT_MEMBER(qualified_name); REFLECT_MEMBER(definition); REFLECT_MEMBER(alias_of); REFLECT_MEMBER(parents); REFLECT_MEMBER(types); REFLECT_MEMBER(funcs); REFLECT_MEMBER(vars); REFLECT_MEMBER_END(); } struct IndexedTypeDef { using Def = TypeDefDefinitionData; Def def; IndexTypeId id; // Immediate derived types. std::vector derived; // Declared variables of this type. // TODO: this needs a lot more work and lots of tests. // TODO: add instantiation on ctor / dtor, do not add instantiation if type is ptr std::vector instantiations; // Every usage, useful for things like renames. // NOTE: Do not insert directly! Use AddUsage instead. std::vector uses; IndexedTypeDef() : def("") {} // For serialization IndexedTypeDef(IndexTypeId id, const std::string& usr); bool HasInterestingState() const { return def.definition_spelling || def.definition_extent || !derived.empty() || !instantiations.empty() || !uses.empty(); } bool operator<(const IndexedTypeDef& other) const { return def.usr < other.def.usr; } }; MAKE_HASHABLE(IndexedTypeDef, t.def.usr); template struct FuncDefDefinitionData { // General metadata. std::string usr; std::string short_name; std::string qualified_name; optional definition_spelling; optional definition_extent; // Type which declares this one (ie, it is a method) optional declaring_type; // Method this method overrides. optional base; // Local variables defined in this function. std::vector locals; // Functions that this function calls. std::vector callees; FuncDefDefinitionData() {} // For reflection. FuncDefDefinitionData(const std::string& usr) : usr(usr) { // assert(usr.size() > 0); } bool operator==( const FuncDefDefinitionData& other) const { return usr == other.usr && short_name == other.short_name && qualified_name == other.qualified_name && definition_spelling == other.definition_spelling && definition_extent == other.definition_extent && declaring_type == other.declaring_type && base == other.base && locals == other.locals && callees == other.callees; } bool operator!=( const FuncDefDefinitionData& other) const { return !(*this == other); } }; template void Reflect( TVisitor& visitor, FuncDefDefinitionData& value) { REFLECT_MEMBER_START(); REFLECT_MEMBER(usr); REFLECT_MEMBER(short_name); REFLECT_MEMBER(qualified_name); REFLECT_MEMBER(definition); REFLECT_MEMBER(declaring_type); REFLECT_MEMBER(base); REFLECT_MEMBER(locals); REFLECT_MEMBER(callees); REFLECT_MEMBER_END(); } struct IndexedFuncDef { using Def = FuncDefDefinitionData; Def def; IndexFuncId id; // Places the function is forward-declared. std::vector declarations; // Methods which directly override this one. std::vector derived; // Functions which call this one. // TODO: Functions can get called outside of just functions - for example, // they can get called in static context (maybe redirect to main?) // or in class initializer list (redirect to class ctor?) // - Right now those usages will not get listed here (but they should be // inside of all_uses). std::vector callers; // All usages. For interesting usages, see callees. std::vector uses; IndexedFuncDef() {} // For reflection. IndexedFuncDef(IndexFuncId id, const std::string& usr) : def(usr), id(id) { // assert(usr.size() > 0); } bool HasInterestingState() const { return def.definition_spelling || def.definition_extent || !def.callees.empty() || !declarations.empty() || !derived.empty() || !callers.empty() || !uses.empty(); } bool operator<(const IndexedFuncDef& other) const { return def.usr < other.def.usr; } }; MAKE_HASHABLE(IndexedFuncDef, t.def.usr); template struct VarDefDefinitionData { // General metadata. std::string usr; std::string short_name; std::string qualified_name; optional declaration; // TODO: definitions should be a list of ranges, since there can be more // than one - when?? optional definition_spelling; optional definition_extent; // Type of the variable. optional variable_type; // Type which declares this one (ie, it is a method) optional declaring_type; VarDefDefinitionData() {} // For reflection. VarDefDefinitionData(const std::string& usr) : usr(usr) {} bool operator==(const VarDefDefinitionData& other) const { return usr == other.usr && short_name == other.short_name && qualified_name == other.qualified_name && declaration == other.declaration && definition_spelling == other.definition_spelling && definition_extent == other.definition_extent && variable_type == other.variable_type && declaring_type == other.declaring_type; } bool operator!=(const VarDefDefinitionData& other) const { return !(*this == other); } }; template void Reflect(TVisitor& visitor, VarDefDefinitionData& value) { REFLECT_MEMBER_START(); REFLECT_MEMBER(usr); REFLECT_MEMBER(short_name); REFLECT_MEMBER(qualified_name); REFLECT_MEMBER(definition_spelling); REFLECT_MEMBER(definition_extent); REFLECT_MEMBER(variable_type); REFLECT_MEMBER(declaring_type); REFLECT_MEMBER_END(); } struct IndexedVarDef { using Def = VarDefDefinitionData; Def def; IndexVarId id; // Usages. std::vector uses; IndexedVarDef() : def("") {} // For serialization IndexedVarDef(IndexVarId id, const std::string& usr) : def(usr), id(id) { // assert(usr.size() > 0); } bool HasInterestingState() const { return def.definition_spelling || def.definition_extent || !uses.empty(); } bool operator<(const IndexedVarDef& other) const { return def.usr < other.def.usr; } }; MAKE_HASHABLE(IndexedVarDef, t.def.usr); struct IdCache { std::string primary_file; std::unordered_map usr_to_type_id; std::unordered_map usr_to_func_id; std::unordered_map usr_to_var_id; std::unordered_map type_id_to_usr; std::unordered_map func_id_to_usr; std::unordered_map var_id_to_usr; IdCache(const std::string& primary_file); Range ForceResolve(const CXSourceRange& range, bool interesting); Range ForceResolveSpelling(const CXCursor& cx_cursor, bool interesting); optional ResolveSpelling(const CXCursor& cx_cursor, bool interesting); optional ResolveSpelling(const clang::Cursor& cursor, bool interesting); Range ForceResolveExtent(const CXCursor& cx_cursor, bool interesting); optional ResolveExtent(const CXCursor& cx_cursor, bool interesting); optional ResolveExtent(const clang::Cursor& cursor, bool interesting); }; struct IndexedFile { IdCache id_cache; std::string path; std::vector types; std::vector funcs; std::vector vars; IndexedFile(const std::string& path); IndexTypeId ToTypeId(const std::string& usr); IndexFuncId ToFuncId(const std::string& usr); IndexVarId ToVarId(const std::string& usr); IndexTypeId ToTypeId(const CXCursor& usr); IndexFuncId ToFuncId(const CXCursor& usr); IndexVarId ToVarId(const CXCursor& usr); IndexedTypeDef* Resolve(IndexTypeId id); IndexedFuncDef* Resolve(IndexFuncId id); IndexedVarDef* Resolve(IndexVarId id); std::string ToString(); }; IndexedFile Parse(std::string filename, std::vector args, bool dump_ast = false);