#pragma once #include "indexer.h" #include "serializer.h" #include #include using Usr = std::string; struct QueryFile; struct QueryType; struct QueryFunc; struct QueryVar; struct QueryDatabase; using QueryFileId = Id; using QueryTypeId = Id; using QueryFuncId = Id; using QueryVarId = Id; struct IdMap; // TODO: in types, store refs separately from irefs. Then we can drop // 'interesting' from location when that is cleaned up. struct QueryLocation { QueryFileId path; Range range; QueryLocation(QueryFileId path, Range range) : path(path), range(range) {} QueryLocation OffsetStartColumn(int offset) const { QueryLocation result = *this; result.range.start.column += offset; return result; } bool operator==(const QueryLocation& other) const { // Note: We ignore |is_interesting|. return path == other.path && range == other.range; } bool operator!=(const QueryLocation& other) const { return !(*this == other); } bool operator<(const QueryLocation& o) const { return path < o.path && range < o.range; } }; enum class SymbolKind { Invalid, File, Type, Func, Var }; struct SymbolIdx { SymbolKind kind; size_t idx; explicit SymbolIdx() : kind(SymbolKind::Invalid), idx(-1) {} // Default ctor needed by stdlib. Do not use. SymbolIdx(SymbolKind kind, uint64_t idx) : kind(kind), idx(idx) {} bool operator==(const SymbolIdx& that) const { return kind == that.kind && idx == that.idx; } bool operator!=(const SymbolIdx& that) const { return !(*this == that); } bool operator<(const SymbolIdx& that) const { return kind < that.kind || idx < that.idx; } QueryFile* ResolveFile(QueryDatabase* db) const; QueryType* ResolveType(QueryDatabase* db) const; QueryFunc* ResolveFunc(QueryDatabase* db) const; QueryVar* ResolveVar(QueryDatabase* db) const; }; struct SymbolRef { SymbolIdx idx; QueryLocation loc; SymbolRef(SymbolIdx idx, QueryLocation loc) : idx(idx), loc(loc) {} bool operator==(const SymbolRef& that) const { return idx == that.idx && loc == that.loc; } bool operator!=(const SymbolRef& that) const { return !(*this == that); } bool operator<(const SymbolRef& that) const { return idx < that.idx && loc.range.start < that.loc.range.start; } }; struct QueryFuncRef { QueryFuncId id() const { assert(has_id()); return id_; } bool has_id() const { return id_.id != -1; } QueryFuncId id_; QueryLocation loc; QueryFuncRef(QueryFuncId id, QueryLocation loc) : id_(id), loc(loc) {} bool operator==(const QueryFuncRef& that) const { return id_ == that.id_ && loc == that.loc; } bool operator!=(const QueryFuncRef& that) const { return !(*this == that); } bool operator<(const QueryFuncRef& that) const { return id_ < that.id_ && loc.range.start < that.loc.range.start; } }; // There are two sources of reindex updates: the (single) definition of a // symbol has changed, or one of many users of the symbol has changed. // // For simplicitly, if the single definition has changed, we update all of the // associated single-owner definition data. See |Update*DefId|. // // If one of the many symbol users submits an update, we store the update such // that it can be merged with other updates before actually being applied to // the main database. See |MergeableUpdate|. template struct MergeableUpdate { // The type/func/var which is getting new usages. TId id; // Entries to add and remove. std::vector to_add; std::vector to_remove; MergeableUpdate(TId id, const std::vector& to_add) : id(id), to_add(to_add) {} MergeableUpdate(TId id, const std::vector& to_add, const std::vector& to_remove) : id(id), to_add(to_add), to_remove(to_remove) {} }; struct QueryFile { struct Def { std::string path; // Outline of the file (ie, for code lens). std::vector outline; // Every symbol found in the file (ie, for goto definition) std::vector all_symbols; }; using DefUpdate = Def; DefUpdate def; size_t detailed_name_idx = -1; QueryFile(const std::string& path) { def.path = path; } }; struct QueryType { using DefUpdate = TypeDefDefinitionData; using DerivedUpdate = MergeableUpdate; using InstantiationsUpdate = MergeableUpdate; using UsesUpdate = MergeableUpdate; DefUpdate def; std::vector derived; std::vector instantiations; std::vector uses; size_t detailed_name_idx = -1; QueryType(const Usr& usr) : def(usr) {} }; struct QueryFunc { using DefUpdate = FuncDefDefinitionData; using DeclarationsUpdate = MergeableUpdate; using DerivedUpdate = MergeableUpdate; using CallersUpdate = MergeableUpdate; DefUpdate def; std::vector declarations; std::vector derived; std::vector callers; size_t detailed_name_idx = -1; QueryFunc(const Usr& usr) : def(usr) {} QueryFunc(const DefUpdate& def) : def(def) {} }; struct QueryVar { using DefUpdate = VarDefDefinitionData; using UsesUpdate = MergeableUpdate; DefUpdate def; std::vector uses; size_t detailed_name_idx = -1; QueryVar(const Usr& usr) : def(usr) {} }; struct IndexUpdate { // Creates a new IndexUpdate based on the delta from previous to current. If // no delta computation should be done just pass null for previous. static IndexUpdate CreateDelta(const IdMap* previous_id_map, const IdMap* current_id_map, IndexedFile* previous, IndexedFile* current); // Merge |update| into this update; this can reduce overhead / index update // work can be parallelized. void Merge(const IndexUpdate& update); // File updates. std::vector files_removed; std::vector files_def_update; // Type updates. std::vector types_removed; std::vector types_def_update; std::vector types_derived; std::vector types_instantiations; std::vector types_uses; // Function updates. std::vector funcs_removed; std::vector funcs_def_update; std::vector funcs_declarations; std::vector funcs_derived; std::vector funcs_callers; // Variable updates. std::vector vars_removed; std::vector vars_def_update; std::vector vars_uses; private: // Creates an index update assuming that |previous| is already // in the index, so only the delta between |previous| and |current| // will be applied. IndexUpdate(const IdMap& previous_id_map, const IdMap& current_id_map, IndexedFile& previous, IndexedFile& current); }; // The query database is heavily optimized for fast queries. It is stored // in-memory. struct QueryDatabase { // Indicies between lookup vectors are related to symbols, ie, index 5 in // |detailed_names| matches index 5 in |symbols|. std::vector detailed_names; std::vector symbols; // Raw data storage. std::vector files; // File path is stored as a Usr. std::vector types; std::vector funcs; std::vector vars; // Lookup symbol based on a usr. spp::sparse_hash_map usr_to_symbol; //google::dense_hash_map usr_to_symbol; QueryDatabase() { //usr_to_symbol.set_empty_key(""); } //std::unordered_map usr_to_symbol; // Insert the contents of |update| into |db|. void ApplyIndexUpdate(IndexUpdate* update); void RemoveUsrs(const std::vector& to_remove); void ImportOrUpdate(const std::vector& updates); void ImportOrUpdate(const std::vector& updates); void ImportOrUpdate(const std::vector& updates); void ImportOrUpdate(const std::vector& updates); }; struct IdMap { // TODO threading model // - [querydb] Create IdMap mapping from every id registered in local_ids // - [indexer] Create IndexUpdate using IdMap cached state // - [querydb] Apply IndexUpdate // // Then lookup in cached_* should *never* fail. const IdCache& local_ids; QueryFileId primary_file; IdMap(QueryDatabase* query_db, const IdCache& local_ids); QueryLocation ToQuery(Range range) const; QueryTypeId ToQuery(IndexTypeId id) const; QueryFuncId ToQuery(IndexFuncId id) const; QueryVarId ToQuery(IndexVarId id) const; QueryFuncRef ToQuery(IndexFuncRef ref) const; optional ToQuery(optional range) const; optional ToQuery(optional id) const; optional ToQuery(optional id) const; optional ToQuery(optional id) const; optional ToQuery(optional ref) const; std::vector ToQuery(std::vector ranges) const; std::vector ToQuery(std::vector ids) const; std::vector ToQuery(std::vector ids) const; std::vector ToQuery(std::vector ids) const; std::vector ToQuery(std::vector refs) const; SymbolIdx ToSymbol(IndexTypeId id) const; SymbolIdx ToSymbol(IndexFuncId id) const; SymbolIdx ToSymbol(IndexVarId id) const; private: spp::sparse_hash_map cached_type_ids_; spp::sparse_hash_map cached_func_ids_; spp::sparse_hash_map cached_var_ids_; //google::dense_hash_map> cached_type_ids_; //google::dense_hash_map> cached_func_ids_; //google::dense_hash_map> cached_var_ids_; };