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-08 01:35:12 +00:00
|
|
|
#include <sparsehash/dense_hash_map>
|
|
|
|
|
2017-02-26 08:11:47 +00:00
|
|
|
using Usr = std::string;
|
2017-02-27 02:03:14 +00:00
|
|
|
|
2017-04-07 06:57:26 +00:00
|
|
|
struct QueryableFile;
|
|
|
|
struct QueryableTypeDef;
|
|
|
|
struct QueryableFuncDef;
|
|
|
|
struct QueryableVarDef;
|
|
|
|
|
|
|
|
using QueryFileId = Id<QueryableFile>;
|
|
|
|
using QueryTypeId = Id<QueryableTypeDef>;
|
|
|
|
using QueryFuncId = Id<QueryableFuncDef>;
|
|
|
|
using QueryVarId = Id<QueryableVarDef>;
|
|
|
|
|
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-02-27 02:03:14 +00:00
|
|
|
struct QueryableLocation {
|
|
|
|
Usr path;
|
2017-04-07 06:20:30 +00:00
|
|
|
Range range;
|
2017-04-08 00:34:13 +00:00
|
|
|
|
2017-04-07 06:20:30 +00:00
|
|
|
QueryableLocation(Usr path, Range range)
|
|
|
|
: path(path), range(range) {}
|
|
|
|
|
|
|
|
QueryableLocation OffsetStartColumn(int offset) const {
|
|
|
|
QueryableLocation result = *this;
|
|
|
|
result.range.start.column += offset;
|
|
|
|
return result;
|
2017-04-03 01:34:15 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 02:03:14 +00:00
|
|
|
bool operator==(const QueryableLocation& other) const {
|
|
|
|
// 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-02-27 07:23:43 +00:00
|
|
|
bool operator!=(const QueryableLocation& other) const { return !(*this == other); }
|
|
|
|
bool operator<(const QueryableLocation& o) const {
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SymbolRef {
|
|
|
|
SymbolIdx idx;
|
|
|
|
QueryableLocation loc;
|
|
|
|
|
|
|
|
SymbolRef(SymbolIdx idx, QueryableLocation 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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-26 19:45:59 +00:00
|
|
|
struct UsrRef {
|
|
|
|
Usr usr;
|
2017-04-07 06:20:30 +00:00
|
|
|
QueryableLocation loc;
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-04-07 06:20:30 +00:00
|
|
|
UsrRef(Usr usr, QueryableLocation loc) : usr(usr), loc(loc) {}
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-02-26 19:45:59 +00:00
|
|
|
bool operator==(const UsrRef& other) const {
|
2017-04-07 06:20:30 +00:00
|
|
|
return usr == other.usr && loc.range.start == other.loc.range.start;
|
2017-02-26 19:45:59 +00:00
|
|
|
}
|
2017-02-27 07:23:43 +00:00
|
|
|
bool operator!=(const UsrRef& other) const { return !(*this == other); }
|
|
|
|
bool operator<(const UsrRef& other) const {
|
2017-04-07 06:20:30 +00:00
|
|
|
return usr < other.usr && loc.range.start < other.loc.range.start;
|
2017-02-27 07:23:43 +00:00
|
|
|
}
|
2017-02-26 19:45:59 +00:00
|
|
|
};
|
2017-02-27 02:03:14 +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-02-26 19:45:59 +00:00
|
|
|
template<typename TValue>
|
2017-02-26 08:11:47 +00:00
|
|
|
struct MergeableUpdate {
|
|
|
|
// The type/func/var which is getting new usages.
|
2017-02-26 19:45:59 +00:00
|
|
|
Usr usr;
|
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-07 05:42:57 +00:00
|
|
|
MergeableUpdate(Usr usr, const std::vector<TValue>& to_add)
|
|
|
|
: usr(usr), to_add(to_add) {}
|
2017-02-27 07:23:43 +00:00
|
|
|
MergeableUpdate(Usr usr, const std::vector<TValue>& to_add, const std::vector<TValue>& to_remove)
|
|
|
|
: usr(usr), to_add(to_add), to_remove(to_remove) {}
|
|
|
|
};
|
|
|
|
|
2017-04-07 05:42:57 +00:00
|
|
|
template<typename TValue>
|
|
|
|
struct ReplacementUpdate {
|
|
|
|
// The type/func/var which is getting new usages.
|
|
|
|
Usr usr;
|
|
|
|
// New entries.
|
|
|
|
std::vector<TValue> values;
|
|
|
|
|
2017-04-08 00:34:13 +00:00
|
|
|
ReplacementUpdate(Usr usr, const std::vector<TValue>& values)
|
|
|
|
: usr(usr), values(values) {}
|
2017-04-07 05:42:57 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2017-02-27 07:23:43 +00:00
|
|
|
struct QueryableFile {
|
2017-04-07 05:42:57 +00:00
|
|
|
struct Def {
|
|
|
|
Usr usr;
|
|
|
|
// 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-08 07:11:57 +00:00
|
|
|
size_t qualified_name_idx = -1;
|
2017-04-07 05:42:57 +00:00
|
|
|
|
2017-04-07 08:01:58 +00:00
|
|
|
QueryableFile(const Usr& usr) { def.usr = usr; }
|
|
|
|
QueryableFile(const Def& def) : def(def) {}
|
2017-04-07 07:30:08 +00:00
|
|
|
QueryableFile(const IdMap& id_map, const IndexedFile& indexed);
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2017-02-26 08:11:47 +00:00
|
|
|
struct QueryableTypeDef {
|
2017-04-07 06:29:18 +00:00
|
|
|
using DefUpdate = TypeDefDefinitionData<Usr, Usr, Usr, QueryableLocation>;
|
2017-02-26 19:45:59 +00:00
|
|
|
using DerivedUpdate = MergeableUpdate<Usr>;
|
2017-04-03 01:34:15 +00:00
|
|
|
using InstantiationsUpdate = MergeableUpdate<Usr>;
|
2017-04-07 06:20:30 +00:00
|
|
|
using UsesUpdate = MergeableUpdate<QueryableLocation>;
|
2017-02-27 02:03:14 +00:00
|
|
|
|
|
|
|
DefUpdate def;
|
|
|
|
std::vector<Usr> derived;
|
2017-04-03 01:34:15 +00:00
|
|
|
std::vector<Usr> instantiations;
|
2017-04-07 06:20:30 +00:00
|
|
|
std::vector<QueryableLocation> uses;
|
2017-04-08 07:11:57 +00:00
|
|
|
size_t qualified_name_idx = -1;
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-04-07 08:01:58 +00:00
|
|
|
QueryableTypeDef(const Usr& usr) : def(usr) {}
|
|
|
|
QueryableTypeDef(const DefUpdate& def) : def(def) {}
|
2017-04-07 07:30:08 +00:00
|
|
|
QueryableTypeDef(const IdMap& id_map, const IndexedTypeDef& indexed);
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2017-02-26 08:11:47 +00:00
|
|
|
struct QueryableFuncDef {
|
2017-04-07 06:29:18 +00:00
|
|
|
using DefUpdate = FuncDefDefinitionData<Usr, Usr, Usr, UsrRef, QueryableLocation>;
|
2017-04-07 06:20:30 +00:00
|
|
|
using DeclarationsUpdate = MergeableUpdate<QueryableLocation>;
|
2017-02-26 19:45:59 +00:00
|
|
|
using DerivedUpdate = MergeableUpdate<Usr>;
|
|
|
|
using CallersUpdate = MergeableUpdate<UsrRef>;
|
2017-04-07 06:20:30 +00:00
|
|
|
using UsesUpdate = MergeableUpdate<QueryableLocation>;
|
2017-02-27 02:03:14 +00:00
|
|
|
|
|
|
|
DefUpdate def;
|
2017-04-07 06:20:30 +00:00
|
|
|
std::vector<QueryableLocation> declarations;
|
2017-02-27 02:03:14 +00:00
|
|
|
std::vector<Usr> derived;
|
|
|
|
std::vector<UsrRef> callers;
|
2017-04-07 06:20:30 +00:00
|
|
|
std::vector<QueryableLocation> uses;
|
2017-04-08 07:11:57 +00:00
|
|
|
size_t qualified_name_idx = -1;
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-04-07 08:01:58 +00:00
|
|
|
QueryableFuncDef(const Usr& usr) : def(usr) {}
|
|
|
|
QueryableFuncDef(const DefUpdate& def) : def(def) {}
|
2017-04-07 07:30:08 +00:00
|
|
|
QueryableFuncDef(const IdMap& id_map, const IndexedFuncDef& indexed);
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2017-02-26 08:11:47 +00:00
|
|
|
struct QueryableVarDef {
|
2017-04-07 06:29:18 +00:00
|
|
|
using DefUpdate = VarDefDefinitionData<Usr, Usr, Usr, QueryableLocation>;
|
2017-04-07 06:20:30 +00:00
|
|
|
using UsesUpdate = MergeableUpdate<QueryableLocation>;
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-02-27 02:03:14 +00:00
|
|
|
DefUpdate def;
|
2017-04-07 06:20:30 +00:00
|
|
|
std::vector<QueryableLocation> uses;
|
2017-04-08 07:11:57 +00:00
|
|
|
size_t qualified_name_idx = -1;
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-04-07 08:01:58 +00:00
|
|
|
QueryableVarDef(const Usr& usr) : def(usr) {}
|
|
|
|
QueryableVarDef(const DefUpdate& def) : def(def) {}
|
2017-04-07 07:30:08 +00:00
|
|
|
QueryableVarDef(const IdMap& id_map, const IndexedVarDef& indexed);
|
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-07 05:42:57 +00:00
|
|
|
std::vector<QueryableFile::DefUpdate> files_def_update;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
|
|
|
// Type updates.
|
|
|
|
std::vector<Usr> types_removed;
|
2017-04-07 05:42:57 +00:00
|
|
|
std::vector<QueryableTypeDef::DefUpdate> types_def_update;
|
2017-02-27 07:23:43 +00:00
|
|
|
std::vector<QueryableTypeDef::DerivedUpdate> types_derived;
|
2017-04-03 01:34:15 +00:00
|
|
|
std::vector<QueryableTypeDef::InstantiationsUpdate> types_instantiations;
|
2017-02-27 07:23:43 +00:00
|
|
|
std::vector<QueryableTypeDef::UsesUpdate> types_uses;
|
|
|
|
|
|
|
|
// Function updates.
|
|
|
|
std::vector<Usr> funcs_removed;
|
2017-04-07 05:42:57 +00:00
|
|
|
std::vector<QueryableFuncDef::DefUpdate> funcs_def_update;
|
2017-02-27 07:23:43 +00:00
|
|
|
std::vector<QueryableFuncDef::DeclarationsUpdate> funcs_declarations;
|
|
|
|
std::vector<QueryableFuncDef::DerivedUpdate> funcs_derived;
|
|
|
|
std::vector<QueryableFuncDef::CallersUpdate> funcs_callers;
|
|
|
|
std::vector<QueryableFuncDef::UsesUpdate> funcs_uses;
|
|
|
|
|
|
|
|
// Variable updates.
|
|
|
|
std::vector<Usr> vars_removed;
|
2017-04-07 05:42:57 +00:00
|
|
|
std::vector<QueryableVarDef::DefUpdate> vars_def_update;
|
2017-02-27 07:23:43 +00:00
|
|
|
std::vector<QueryableVarDef::UsesUpdate> vars_uses;
|
|
|
|
|
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.
|
|
|
|
struct QueryableDatabase {
|
|
|
|
// Indicies between lookup vectors are related to symbols, ie, index 5 in
|
|
|
|
// |qualified_names| matches index 5 in |symbols|.
|
|
|
|
std::vector<std::string> qualified_names;
|
|
|
|
std::vector<SymbolIdx> symbols;
|
|
|
|
|
|
|
|
// Raw data storage.
|
|
|
|
std::vector<QueryableFile> files; // File path is stored as a Usr.
|
|
|
|
std::vector<QueryableTypeDef> types;
|
|
|
|
std::vector<QueryableFuncDef> funcs;
|
|
|
|
std::vector<QueryableVarDef> vars;
|
|
|
|
|
|
|
|
// Lookup symbol based on a usr.
|
2017-04-08 01:35:12 +00:00
|
|
|
google::dense_hash_map<Usr, SymbolIdx> usr_to_symbol;
|
|
|
|
|
|
|
|
QueryableDatabase() {
|
|
|
|
usr_to_symbol.set_empty_key("");
|
|
|
|
}
|
|
|
|
//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-07 05:42:57 +00:00
|
|
|
void ImportOrUpdate(const std::vector<QueryableFile::DefUpdate>& updates);
|
|
|
|
void ImportOrUpdate(const std::vector<QueryableTypeDef::DefUpdate>& updates);
|
|
|
|
void ImportOrUpdate(const std::vector<QueryableFuncDef::DefUpdate>& updates);
|
|
|
|
void ImportOrUpdate(const std::vector<QueryableVarDef::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;
|
|
|
|
|
|
|
|
IdMap(QueryableDatabase* query_db, const IdCache& local_ids);
|
|
|
|
|
2017-04-08 07:11:57 +00:00
|
|
|
QueryTypeId ToQuery(IndexTypeId id) const;
|
|
|
|
QueryFuncId ToQuery(IndexFuncId id) const;
|
|
|
|
QueryVarId ToQuery(IndexVarId id) const;
|
|
|
|
SymbolIdx ToSymbol(IndexTypeId id) const;
|
|
|
|
SymbolIdx ToSymbol(IndexFuncId id) const;
|
|
|
|
SymbolIdx ToSymbol(IndexVarId id) const;
|
|
|
|
|
2017-04-07 08:01:58 +00:00
|
|
|
// TODO
|
|
|
|
private:
|
|
|
|
QueryFileId index_file_id;
|
2017-04-08 07:11:57 +00:00
|
|
|
// TODO: make these type safe
|
|
|
|
google::dense_hash_map<size_t, size_t> cached_type_ids_; // IndexTypeId -> QueryTypeId
|
|
|
|
google::dense_hash_map<size_t, size_t> cached_func_ids_; // IndexFuncId -> QueryFuncId
|
|
|
|
google::dense_hash_map<size_t, size_t> cached_var_ids_; // IndexVarId -> QueryVarId
|
2017-04-07 08:01:58 +00:00
|
|
|
};
|