2017-02-23 09:23:23 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-02-26 08:11:47 +00:00
|
|
|
#include "indexer.h"
|
2017-03-07 09:32:29 +00:00
|
|
|
#include "serializer.h"
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-04-19 05:45:37 +00:00
|
|
|
#include <sparsepp/spp.h>
|
2017-04-08 01:35:12 +00:00
|
|
|
|
2017-04-17 20:40:50 +00:00
|
|
|
#include <functional>
|
|
|
|
|
2017-02-26 08:11:47 +00:00
|
|
|
using Usr = std::string;
|
2017-02-27 02:03:14 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
struct QueryFile;
|
|
|
|
struct QueryType;
|
|
|
|
struct QueryFunc;
|
|
|
|
struct QueryVar;
|
|
|
|
struct QueryDatabase;
|
2017-04-07 06:57:26 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
using QueryFileId = Id<QueryFile>;
|
|
|
|
using QueryTypeId = Id<QueryType>;
|
|
|
|
using QueryFuncId = Id<QueryFunc>;
|
|
|
|
using QueryVarId = Id<QueryVar>;
|
2017-04-07 06:57:26 +00:00
|
|
|
|
2017-04-07 07:30:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-07 08:01:58 +00:00
|
|
|
struct IdMap;
|
2017-04-07 07:30:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-07 05:42:57 +00:00
|
|
|
// TODO: in types, store refs separately from irefs. Then we can drop
|
|
|
|
// 'interesting' from location when that is cleaned up.
|
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
struct QueryLocation {
|
2017-04-08 07:21:00 +00:00
|
|
|
QueryFileId path;
|
2017-04-07 06:20:30 +00:00
|
|
|
Range range;
|
2017-04-08 00:34:13 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
QueryLocation(QueryFileId path, Range range)
|
2017-04-07 06:20:30 +00:00
|
|
|
: path(path), range(range) {}
|
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
QueryLocation OffsetStartColumn(int offset) const {
|
|
|
|
QueryLocation result = *this;
|
2017-04-07 06:20:30 +00:00
|
|
|
result.range.start.column += offset;
|
|
|
|
return result;
|
2017-04-03 01:34:15 +00:00
|
|
|
}
|
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
bool operator==(const QueryLocation& other) const {
|
2017-02-27 02:03:14 +00:00
|
|
|
// Note: We ignore |is_interesting|.
|
|
|
|
return
|
|
|
|
path == other.path &&
|
2017-04-07 06:20:30 +00:00
|
|
|
range == other.range;
|
2017-02-27 02:03:14 +00:00
|
|
|
}
|
2017-04-15 05:41:35 +00:00
|
|
|
bool operator!=(const QueryLocation& other) const { return !(*this == other); }
|
|
|
|
bool operator<(const QueryLocation& o) const {
|
2017-02-27 07:23:43 +00:00
|
|
|
return
|
|
|
|
path < o.path &&
|
2017-04-07 06:20:30 +00:00
|
|
|
range < o.range;
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-08 07:11:57 +00:00
|
|
|
enum class SymbolKind { Invalid, File, Type, Func, Var };
|
|
|
|
struct SymbolIdx {
|
|
|
|
SymbolKind kind;
|
|
|
|
size_t idx;
|
|
|
|
|
2017-04-10 00:08:54 +00:00
|
|
|
explicit SymbolIdx() : kind(SymbolKind::Invalid), idx(-1) {} // Default ctor needed by stdlib. Do not use.
|
2017-04-08 07:11:57 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-04-09 02:48:50 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
QueryFile* ResolveFile(QueryDatabase* db) const;
|
|
|
|
QueryType* ResolveType(QueryDatabase* db) const;
|
|
|
|
QueryFunc* ResolveFunc(QueryDatabase* db) const;
|
|
|
|
QueryVar* ResolveVar(QueryDatabase* db) const;
|
2017-04-08 07:11:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SymbolRef {
|
|
|
|
SymbolIdx idx;
|
2017-04-15 05:41:35 +00:00
|
|
|
QueryLocation loc;
|
2017-04-08 07:11:57 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
SymbolRef(SymbolIdx idx, QueryLocation loc) : idx(idx), loc(loc) {}
|
2017-04-08 07:11:57 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-08 08:04:38 +00:00
|
|
|
struct QueryFuncRef {
|
2017-04-13 07:47:47 +00:00
|
|
|
QueryFuncId id() const {
|
|
|
|
assert(has_id());
|
|
|
|
return id_;
|
|
|
|
}
|
|
|
|
bool has_id() const {
|
|
|
|
return id_.id != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryFuncId id_;
|
2017-04-15 05:41:35 +00:00
|
|
|
QueryLocation loc;
|
2017-04-08 08:04:38 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
QueryFuncRef(QueryFuncId id, QueryLocation loc) : id_(id), loc(loc) {}
|
2017-04-08 08:04:38 +00:00
|
|
|
|
|
|
|
bool operator==(const QueryFuncRef& that) const {
|
2017-04-13 07:47:47 +00:00
|
|
|
return id_ == that.id_ && loc == that.loc;
|
2017-04-08 08:04:38 +00:00
|
|
|
}
|
|
|
|
bool operator!=(const QueryFuncRef& that) const { return !(*this == that); }
|
|
|
|
bool operator<(const QueryFuncRef& that) const {
|
2017-04-13 07:47:47 +00:00
|
|
|
return id_ < that.id_ && loc.range.start < that.loc.range.start;
|
2017-04-08 08:04:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-26 08:11:47 +00:00
|
|
|
// 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|.
|
|
|
|
|
2017-04-15 05:24:49 +00:00
|
|
|
template<typename TId, typename TValue>
|
2017-02-26 08:11:47 +00:00
|
|
|
struct MergeableUpdate {
|
|
|
|
// The type/func/var which is getting new usages.
|
2017-04-15 05:24:49 +00:00
|
|
|
TId id;
|
2017-02-26 08:11:47 +00:00
|
|
|
// Entries to add and remove.
|
|
|
|
std::vector<TValue> to_add;
|
|
|
|
std::vector<TValue> to_remove;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-04-15 05:24:49 +00:00
|
|
|
MergeableUpdate(TId id, const std::vector<TValue>& to_add)
|
|
|
|
: id(id), to_add(to_add) {}
|
|
|
|
MergeableUpdate(TId id, const std::vector<TValue>& to_add, const std::vector<TValue>& to_remove)
|
|
|
|
: id(id), to_add(to_add), to_remove(to_remove) {}
|
2017-02-27 07:23:43 +00:00
|
|
|
};
|
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
struct QueryFile {
|
2017-04-07 05:42:57 +00:00
|
|
|
struct Def {
|
2017-04-15 05:41:35 +00:00
|
|
|
std::string path;
|
2017-04-07 05:42:57 +00:00
|
|
|
// Outline of the file (ie, for code lens).
|
2017-04-08 07:11:57 +00:00
|
|
|
std::vector<SymbolRef> outline;
|
2017-04-07 05:42:57 +00:00
|
|
|
// Every symbol found in the file (ie, for goto definition)
|
2017-04-08 07:11:57 +00:00
|
|
|
std::vector<SymbolRef> all_symbols;
|
2017-04-07 05:42:57 +00:00
|
|
|
};
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-04-07 05:42:57 +00:00
|
|
|
using DefUpdate = Def;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-04-07 05:42:57 +00:00
|
|
|
DefUpdate def;
|
2017-04-15 04:58:07 +00:00
|
|
|
size_t detailed_name_idx = -1;
|
2017-04-07 05:42:57 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
QueryFile(const std::string& path) { def.path = path; }
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
struct QueryType {
|
|
|
|
using DefUpdate = TypeDefDefinitionData<QueryTypeId, QueryFuncId, QueryVarId, QueryLocation>;
|
2017-04-15 05:24:49 +00:00
|
|
|
using DerivedUpdate = MergeableUpdate<QueryTypeId, QueryTypeId>;
|
|
|
|
using InstantiationsUpdate = MergeableUpdate<QueryTypeId, QueryVarId>;
|
2017-04-15 05:41:35 +00:00
|
|
|
using UsesUpdate = MergeableUpdate<QueryTypeId, QueryLocation>;
|
2017-02-27 02:03:14 +00:00
|
|
|
|
|
|
|
DefUpdate def;
|
2017-04-08 07:52:57 +00:00
|
|
|
std::vector<QueryTypeId> derived;
|
|
|
|
std::vector<QueryVarId> instantiations;
|
2017-04-15 05:41:35 +00:00
|
|
|
std::vector<QueryLocation> uses;
|
2017-04-15 04:58:07 +00:00
|
|
|
size_t detailed_name_idx = -1;
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
QueryType(const Usr& usr) : def(usr) {}
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
struct QueryFunc {
|
|
|
|
using DefUpdate = FuncDefDefinitionData<QueryTypeId, QueryFuncId, QueryVarId, QueryFuncRef, QueryLocation>;
|
|
|
|
using DeclarationsUpdate = MergeableUpdate<QueryFuncId, QueryLocation>;
|
2017-04-15 05:24:49 +00:00
|
|
|
using DerivedUpdate = MergeableUpdate<QueryFuncId, QueryFuncId>;
|
|
|
|
using CallersUpdate = MergeableUpdate<QueryFuncId, QueryFuncRef>;
|
2017-02-27 02:03:14 +00:00
|
|
|
|
|
|
|
DefUpdate def;
|
2017-04-15 05:41:35 +00:00
|
|
|
std::vector<QueryLocation> declarations;
|
2017-04-08 08:04:38 +00:00
|
|
|
std::vector<QueryFuncId> derived;
|
|
|
|
std::vector<QueryFuncRef> callers;
|
2017-04-15 04:58:07 +00:00
|
|
|
size_t detailed_name_idx = -1;
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
QueryFunc(const Usr& usr) : def(usr) {}
|
|
|
|
QueryFunc(const DefUpdate& def) : def(def) {}
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
struct QueryVar {
|
|
|
|
using DefUpdate = VarDefDefinitionData<QueryTypeId, QueryFuncId, QueryVarId, QueryLocation>;
|
|
|
|
using UsesUpdate = MergeableUpdate<QueryVarId, QueryLocation>;
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-02-27 02:03:14 +00:00
|
|
|
DefUpdate def;
|
2017-04-15 05:41:35 +00:00
|
|
|
std::vector<QueryLocation> uses;
|
2017-04-15 04:58:07 +00:00
|
|
|
size_t detailed_name_idx = -1;
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
QueryVar(const Usr& usr) : def(usr) {}
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2017-02-27 07:23:43 +00:00
|
|
|
struct IndexUpdate {
|
2017-04-08 06:45:28 +00:00
|
|
|
// 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);
|
2017-04-07 05:42:57 +00:00
|
|
|
|
|
|
|
// Merge |update| into this update; this can reduce overhead / index update
|
|
|
|
// work can be parallelized.
|
|
|
|
void Merge(const IndexUpdate& update);
|
|
|
|
|
2017-02-27 07:23:43 +00:00
|
|
|
// File updates.
|
|
|
|
std::vector<Usr> files_removed;
|
2017-04-15 05:41:35 +00:00
|
|
|
std::vector<QueryFile::DefUpdate> files_def_update;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
|
|
|
// Type updates.
|
|
|
|
std::vector<Usr> types_removed;
|
2017-04-15 05:41:35 +00:00
|
|
|
std::vector<QueryType::DefUpdate> types_def_update;
|
|
|
|
std::vector<QueryType::DerivedUpdate> types_derived;
|
|
|
|
std::vector<QueryType::InstantiationsUpdate> types_instantiations;
|
|
|
|
std::vector<QueryType::UsesUpdate> types_uses;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
|
|
|
// Function updates.
|
|
|
|
std::vector<Usr> funcs_removed;
|
2017-04-15 05:41:35 +00:00
|
|
|
std::vector<QueryFunc::DefUpdate> funcs_def_update;
|
|
|
|
std::vector<QueryFunc::DeclarationsUpdate> funcs_declarations;
|
|
|
|
std::vector<QueryFunc::DerivedUpdate> funcs_derived;
|
|
|
|
std::vector<QueryFunc::CallersUpdate> funcs_callers;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
|
|
|
// Variable updates.
|
|
|
|
std::vector<Usr> vars_removed;
|
2017-04-15 05:41:35 +00:00
|
|
|
std::vector<QueryVar::DefUpdate> vars_def_update;
|
|
|
|
std::vector<QueryVar::UsesUpdate> vars_uses;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-04-07 05:42:57 +00:00
|
|
|
private:
|
2017-02-27 07:23:43 +00:00
|
|
|
// Creates an index update assuming that |previous| is already
|
|
|
|
// in the index, so only the delta between |previous| and |current|
|
|
|
|
// will be applied.
|
2017-04-08 06:45:28 +00:00
|
|
|
IndexUpdate(const IdMap& previous_id_map, const IdMap& current_id_map, IndexedFile& previous, IndexedFile& current);
|
2017-02-27 07:23:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// The query database is heavily optimized for fast queries. It is stored
|
|
|
|
// in-memory.
|
2017-04-15 05:41:35 +00:00
|
|
|
struct QueryDatabase {
|
2017-02-27 07:23:43 +00:00
|
|
|
// Indicies between lookup vectors are related to symbols, ie, index 5 in
|
2017-04-15 04:58:07 +00:00
|
|
|
// |detailed_names| matches index 5 in |symbols|.
|
|
|
|
std::vector<std::string> detailed_names;
|
2017-02-27 07:23:43 +00:00
|
|
|
std::vector<SymbolIdx> symbols;
|
|
|
|
|
|
|
|
// Raw data storage.
|
2017-04-15 05:41:35 +00:00
|
|
|
std::vector<QueryFile> files; // File path is stored as a Usr.
|
|
|
|
std::vector<QueryType> types;
|
|
|
|
std::vector<QueryFunc> funcs;
|
|
|
|
std::vector<QueryVar> vars;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
|
|
|
// Lookup symbol based on a usr.
|
2017-04-19 05:45:37 +00:00
|
|
|
spp::sparse_hash_map<Usr, SymbolIdx> usr_to_symbol;
|
|
|
|
//google::dense_hash_map<Usr, SymbolIdx> usr_to_symbol;
|
2017-04-08 01:35:12 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
QueryDatabase() {
|
2017-04-19 05:45:37 +00:00
|
|
|
//usr_to_symbol.set_empty_key("");
|
2017-04-08 01:35:12 +00:00
|
|
|
}
|
|
|
|
//std::unordered_map<Usr, SymbolIdx> usr_to_symbol;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
|
|
|
// Insert the contents of |update| into |db|.
|
|
|
|
void ApplyIndexUpdate(IndexUpdate* update);
|
|
|
|
|
|
|
|
void RemoveUsrs(const std::vector<Usr>& to_remove);
|
2017-04-15 05:41:35 +00:00
|
|
|
void ImportOrUpdate(const std::vector<QueryFile::DefUpdate>& updates);
|
|
|
|
void ImportOrUpdate(const std::vector<QueryType::DefUpdate>& updates);
|
|
|
|
void ImportOrUpdate(const std::vector<QueryFunc::DefUpdate>& updates);
|
|
|
|
void ImportOrUpdate(const std::vector<QueryVar::DefUpdate>& updates);
|
2017-04-07 08:01:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2017-04-08 07:21:00 +00:00
|
|
|
QueryFileId primary_file;
|
2017-04-07 08:01:58 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
IdMap(QueryDatabase* query_db, const IdCache& local_ids);
|
2017-04-07 08:01:58 +00:00
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
QueryLocation ToQuery(Range range) const;
|
2017-04-08 07:11:57 +00:00
|
|
|
QueryTypeId ToQuery(IndexTypeId id) const;
|
|
|
|
QueryFuncId ToQuery(IndexFuncId id) const;
|
|
|
|
QueryVarId ToQuery(IndexVarId id) const;
|
2017-04-08 08:04:38 +00:00
|
|
|
QueryFuncRef ToQuery(IndexFuncRef ref) const;
|
2017-04-15 05:41:35 +00:00
|
|
|
optional<QueryLocation> ToQuery(optional<Range> range) const;
|
2017-04-08 07:52:57 +00:00
|
|
|
optional<QueryTypeId> ToQuery(optional<IndexTypeId> id) const;
|
|
|
|
optional<QueryFuncId> ToQuery(optional<IndexFuncId> id) const;
|
|
|
|
optional<QueryVarId> ToQuery(optional<IndexVarId> id) const;
|
2017-04-08 08:04:38 +00:00
|
|
|
optional<QueryFuncRef> ToQuery(optional<IndexFuncRef> ref) const;
|
2017-04-15 05:41:35 +00:00
|
|
|
std::vector<QueryLocation> ToQuery(std::vector<Range> ranges) const;
|
2017-04-08 07:52:57 +00:00
|
|
|
std::vector<QueryTypeId> ToQuery(std::vector<IndexTypeId> ids) const;
|
|
|
|
std::vector<QueryFuncId> ToQuery(std::vector<IndexFuncId> ids) const;
|
|
|
|
std::vector<QueryVarId> ToQuery(std::vector<IndexVarId> ids) const;
|
2017-04-08 08:04:38 +00:00
|
|
|
std::vector<QueryFuncRef> ToQuery(std::vector<IndexFuncRef> refs) const;
|
2017-04-08 07:52:57 +00:00
|
|
|
|
2017-04-08 07:11:57 +00:00
|
|
|
SymbolIdx ToSymbol(IndexTypeId id) const;
|
|
|
|
SymbolIdx ToSymbol(IndexFuncId id) const;
|
|
|
|
SymbolIdx ToSymbol(IndexVarId id) const;
|
2017-04-07 08:01:58 +00:00
|
|
|
private:
|
2017-04-19 05:45:37 +00:00
|
|
|
spp::sparse_hash_map<IndexTypeId, QueryTypeId> cached_type_ids_;
|
|
|
|
spp::sparse_hash_map<IndexFuncId, QueryFuncId> cached_func_ids_;
|
|
|
|
spp::sparse_hash_map<IndexVarId, QueryVarId> cached_var_ids_;
|
|
|
|
//google::dense_hash_map<IndexTypeId, QueryTypeId, std::hash<IndexTypeId>> cached_type_ids_;
|
|
|
|
//google::dense_hash_map<IndexFuncId, QueryFuncId, std::hash<IndexFuncId>> cached_func_ids_;
|
|
|
|
//google::dense_hash_map<IndexVarId, QueryVarId, std::hash<IndexVarId>> cached_var_ids_;
|
2017-04-07 08:01:58 +00:00
|
|
|
};
|