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
|
|
|
|
|
|
|
using Usr = std::string;
|
2017-02-27 02:03:14 +00:00
|
|
|
|
|
|
|
// TODO: Switch over to QueryableLocation. Figure out if there is
|
|
|
|
// a good way to get the indexer using it. I don't think so
|
|
|
|
// since we may discover more files while indexing a file.
|
|
|
|
//
|
|
|
|
// We could also reuse planned USR caching system for file
|
|
|
|
// paths.
|
|
|
|
struct QueryableLocation {
|
|
|
|
Usr path;
|
|
|
|
int line;
|
|
|
|
int column;
|
|
|
|
bool interesting;
|
|
|
|
|
|
|
|
QueryableLocation()
|
|
|
|
: path(""), line(-1), column(-1), interesting(false) {}
|
|
|
|
QueryableLocation(Usr path, int line, int column, bool interesting)
|
|
|
|
: path(path), line(line), column(column), interesting(interesting) {}
|
|
|
|
|
2017-04-03 01:34:15 +00:00
|
|
|
QueryableLocation OffsetColumn(int offset) const {
|
|
|
|
return QueryableLocation(path, line, column + offset, interesting);
|
|
|
|
}
|
|
|
|
|
2017-02-27 02:03:14 +00:00
|
|
|
bool operator==(const QueryableLocation& other) const {
|
|
|
|
// Note: We ignore |is_interesting|.
|
|
|
|
return
|
|
|
|
path == other.path &&
|
|
|
|
line == other.line &&
|
|
|
|
column == other.column;
|
|
|
|
}
|
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 &&
|
|
|
|
line < o.line &&
|
|
|
|
column < o.column &&
|
|
|
|
interesting < o.interesting;
|
|
|
|
}
|
2017-02-27 02:03:14 +00:00
|
|
|
};
|
2017-03-25 23:58:11 +00:00
|
|
|
MAKE_REFLECT_STRUCT(QueryableLocation, path, line, column, interesting);
|
2017-02-27 02:03:14 +00:00
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
struct QueryableRange {
|
|
|
|
QueryableLocation start;
|
|
|
|
QueryableLocation end;
|
|
|
|
|
|
|
|
QueryableRange() {}
|
|
|
|
QueryableRange(QueryableLocation start, QueryableLocation end) : start(start), end(end) {}
|
|
|
|
|
|
|
|
QueryableRange OffsetStartColumn(int offset) const {
|
|
|
|
return QueryableRange(start.OffsetColumn(offset), end);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const QueryableRange& other) const {
|
|
|
|
// Note: We ignore |is_interesting|.
|
|
|
|
return start == other.start && end == other.end;
|
|
|
|
}
|
|
|
|
bool operator!=(const QueryableRange& other) const { return !(*this == other); }
|
|
|
|
bool operator<(const QueryableRange& o) const {
|
|
|
|
return start < o.start;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(QueryableRange, start, end);
|
|
|
|
|
2017-02-26 19:45:59 +00:00
|
|
|
struct UsrRef {
|
|
|
|
Usr usr;
|
2017-04-05 08:06:18 +00:00
|
|
|
QueryableRange loc;
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-02-27 07:23:43 +00:00
|
|
|
UsrRef() {}
|
2017-04-05 08:06:18 +00:00
|
|
|
UsrRef(Usr usr, QueryableRange 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-05 08:06:18 +00:00
|
|
|
return usr == other.usr && loc.start == other.loc.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-05 08:06:18 +00:00
|
|
|
return usr < other.usr && loc.start < other.loc.start;
|
2017-02-27 07:23:43 +00:00
|
|
|
}
|
2017-02-26 19:45:59 +00:00
|
|
|
};
|
2017-03-25 23:58:11 +00:00
|
|
|
MAKE_REFLECT_STRUCT(UsrRef, usr, loc);
|
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-03-14 08:33:39 +00:00
|
|
|
MergeableUpdate() {} // For reflection
|
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-03-14 08:33:39 +00:00
|
|
|
template<typename TVisitor, typename TValue>
|
|
|
|
void Reflect(TVisitor& visitor, MergeableUpdate<TValue>& value) {
|
|
|
|
REFLECT_MEMBER_START();
|
|
|
|
REFLECT_MEMBER(usr);
|
|
|
|
REFLECT_MEMBER(to_add);
|
|
|
|
REFLECT_MEMBER(to_remove);
|
|
|
|
REFLECT_MEMBER_END();
|
|
|
|
}
|
|
|
|
|
2017-02-27 07:23:43 +00:00
|
|
|
struct QueryableFile {
|
|
|
|
using OutlineUpdate = MergeableUpdate<UsrRef>;
|
2017-04-03 02:21:21 +00:00
|
|
|
using AllSymboslUpdate = MergeableUpdate<UsrRef>;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
|
|
|
Usr file_id;
|
2017-04-03 02:21:21 +00:00
|
|
|
// Outline of the file (ie, for code lens).
|
2017-02-27 07:23:43 +00:00
|
|
|
std::vector<UsrRef> outline;
|
2017-04-03 02:21:21 +00:00
|
|
|
// Every symbol found in the file (ie, for goto definition)
|
|
|
|
std::vector<UsrRef> all_symbols;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-03-14 08:33:39 +00:00
|
|
|
QueryableFile() {} // For serialization.
|
2017-02-27 07:23:43 +00:00
|
|
|
QueryableFile(const IndexedFile& indexed);
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-04-03 02:21:21 +00:00
|
|
|
MAKE_REFLECT_STRUCT(QueryableFile, file_id, outline, all_symbols);
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2017-02-26 08:11:47 +00:00
|
|
|
struct QueryableTypeDef {
|
2017-04-05 08:06:18 +00:00
|
|
|
using DefUpdate = TypeDefDefinitionData<Usr, Usr, Usr, QueryableRange, QueryableRange>;
|
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-05 08:06:18 +00:00
|
|
|
using UsesUpdate = MergeableUpdate<QueryableRange>;
|
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-05 08:06:18 +00:00
|
|
|
std::vector<QueryableRange> uses;
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-03-14 08:33:39 +00:00
|
|
|
QueryableTypeDef() : def("") {} // For serialization.
|
2017-02-27 07:23:43 +00:00
|
|
|
QueryableTypeDef(IdCache& id_cache, const IndexedTypeDef& indexed);
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-04-03 01:34:15 +00:00
|
|
|
MAKE_REFLECT_STRUCT(QueryableTypeDef, def, derived, instantiations, uses);
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2017-02-26 08:11:47 +00:00
|
|
|
struct QueryableFuncDef {
|
2017-04-05 08:06:18 +00:00
|
|
|
using DefUpdate = FuncDefDefinitionData<Usr, Usr, Usr, UsrRef, QueryableRange, QueryableRange>;
|
|
|
|
using DeclarationsUpdate = MergeableUpdate<QueryableRange>;
|
2017-02-26 19:45:59 +00:00
|
|
|
using DerivedUpdate = MergeableUpdate<Usr>;
|
|
|
|
using CallersUpdate = MergeableUpdate<UsrRef>;
|
2017-04-05 08:06:18 +00:00
|
|
|
using UsesUpdate = MergeableUpdate<QueryableRange>;
|
2017-02-27 02:03:14 +00:00
|
|
|
|
|
|
|
DefUpdate def;
|
2017-04-05 08:06:18 +00:00
|
|
|
std::vector<QueryableRange> declarations;
|
2017-02-27 02:03:14 +00:00
|
|
|
std::vector<Usr> derived;
|
|
|
|
std::vector<UsrRef> callers;
|
2017-04-05 08:06:18 +00:00
|
|
|
std::vector<QueryableRange> uses;
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-03-14 08:33:39 +00:00
|
|
|
QueryableFuncDef() : def("") {} // For serialization.
|
2017-02-27 07:23:43 +00:00
|
|
|
QueryableFuncDef(IdCache& id_cache, const IndexedFuncDef& indexed);
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-25 23:58:11 +00:00
|
|
|
MAKE_REFLECT_STRUCT(QueryableFuncDef, def, declarations, derived, callers, uses);
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2017-02-26 08:11:47 +00:00
|
|
|
struct QueryableVarDef {
|
2017-04-05 08:06:18 +00:00
|
|
|
using DefUpdate = VarDefDefinitionData<Usr, Usr, Usr, QueryableRange, QueryableRange>;
|
|
|
|
using UsesUpdate = MergeableUpdate<QueryableRange>;
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-02-27 02:03:14 +00:00
|
|
|
DefUpdate def;
|
2017-04-05 08:06:18 +00:00
|
|
|
std::vector<QueryableRange> uses;
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-03-14 08:33:39 +00:00
|
|
|
QueryableVarDef() : def("") {} // For serialization.
|
2017-02-27 07:23:43 +00:00
|
|
|
QueryableVarDef(IdCache& id_cache, const IndexedVarDef& indexed);
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-25 23:58:11 +00:00
|
|
|
MAKE_REFLECT_STRUCT(QueryableVarDef, def, uses);
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2017-02-27 02:03:14 +00:00
|
|
|
enum class SymbolKind { Invalid, File, Type, Func, Var };
|
2017-02-26 08:11:47 +00:00
|
|
|
struct SymbolIdx {
|
|
|
|
SymbolKind kind;
|
2017-02-26 19:45:59 +00:00
|
|
|
uint64_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) {}
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-02-28 08:37:20 +00:00
|
|
|
// TODO: We need to control Usr, std::vector allocation to make sure it happens on shmem. That or we
|
|
|
|
// make IndexUpdate a POD type.
|
|
|
|
// TODO: Instead of all of that work above, we pipe the IndexUpdate across processes as JSON.
|
|
|
|
// We need to verify we need multiple processes first. Maybe libclang can run in a single process...
|
|
|
|
// TODO: Compute IndexUpdates in main process, off the blocking thread. Use separate process for running
|
|
|
|
// libclang. Solves memory worries.
|
2017-03-01 04:12:57 +00:00
|
|
|
// TODO: Instead of passing to/from json, we can probably bass the IndexedFile type almost directly as
|
|
|
|
// a raw memory dump - the type has almost zero pointers inside of it. We could do a little bit of fixup
|
|
|
|
// so that passing from a separate process to the main db is really fast (no need to go through JSON).
|
2017-03-05 02:16:23 +00:00
|
|
|
/*
|
2017-02-28 08:37:20 +00:00
|
|
|
namespace foo2 {
|
|
|
|
using Usr = size_t;
|
|
|
|
struct UsrTable {
|
|
|
|
size_t allocated;
|
|
|
|
size_t used;
|
|
|
|
const char* usrs[];
|
|
|
|
};
|
|
|
|
}
|
2017-03-05 02:16:23 +00:00
|
|
|
*/
|
2017-02-27 07:23:43 +00:00
|
|
|
|
|
|
|
struct IndexUpdate {
|
|
|
|
// File updates.
|
|
|
|
std::vector<Usr> files_removed;
|
|
|
|
std::vector<QueryableFile> files_added;
|
|
|
|
std::vector<QueryableFile::OutlineUpdate> files_outline;
|
2017-04-03 02:21:21 +00:00
|
|
|
std::vector<QueryableFile::AllSymboslUpdate> files_all_symbols;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
|
|
|
// Type updates.
|
|
|
|
std::vector<Usr> types_removed;
|
|
|
|
std::vector<QueryableTypeDef> types_added;
|
|
|
|
std::vector<QueryableTypeDef::DefUpdate> types_def_changed;
|
|
|
|
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;
|
|
|
|
std::vector<QueryableFuncDef> funcs_added;
|
|
|
|
std::vector<QueryableFuncDef::DefUpdate> funcs_def_changed;
|
|
|
|
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;
|
|
|
|
std::vector<QueryableVarDef> vars_added;
|
|
|
|
std::vector<QueryableVarDef::DefUpdate> vars_def_changed;
|
|
|
|
std::vector<QueryableVarDef::UsesUpdate> vars_uses;
|
|
|
|
|
2017-03-15 04:59:05 +00:00
|
|
|
IndexUpdate() {}
|
2017-02-27 07:23:43 +00:00
|
|
|
|
|
|
|
// Creates a new IndexUpdate that will import |file|.
|
|
|
|
explicit IndexUpdate(IndexedFile& file);
|
|
|
|
|
|
|
|
// Creates an index update assuming that |previous| is already
|
|
|
|
// in the index, so only the delta between |previous| and |current|
|
|
|
|
// will be applied.
|
|
|
|
IndexUpdate(IndexedFile& previous, IndexedFile& current);
|
|
|
|
|
|
|
|
// Merges the contents of |update| into this IndexUpdate instance.
|
|
|
|
void Merge(const IndexUpdate& update);
|
|
|
|
};
|
2017-03-25 23:58:11 +00:00
|
|
|
MAKE_REFLECT_STRUCT(IndexUpdate,
|
|
|
|
files_removed,
|
|
|
|
files_added,
|
|
|
|
files_outline,
|
2017-04-03 02:21:21 +00:00
|
|
|
files_all_symbols,
|
2017-03-25 23:58:11 +00:00
|
|
|
types_removed,
|
|
|
|
types_added,
|
|
|
|
types_def_changed,
|
|
|
|
types_derived,
|
|
|
|
types_uses,
|
|
|
|
funcs_removed,
|
|
|
|
funcs_added,
|
|
|
|
funcs_def_changed,
|
|
|
|
funcs_declarations,
|
|
|
|
funcs_derived,
|
|
|
|
funcs_callers,
|
|
|
|
funcs_uses,
|
|
|
|
vars_removed,
|
|
|
|
vars_added,
|
|
|
|
vars_def_changed,
|
|
|
|
vars_uses);
|
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.
|
|
|
|
std::unordered_map<Usr, SymbolIdx> usr_to_symbol;
|
|
|
|
|
|
|
|
// Insert the contents of |update| into |db|.
|
|
|
|
void ApplyIndexUpdate(IndexUpdate* update);
|
|
|
|
|
|
|
|
void RemoveUsrs(const std::vector<Usr>& to_remove);
|
|
|
|
void Import(const std::vector<QueryableFile>& defs);
|
|
|
|
void Import(const std::vector<QueryableTypeDef>& defs);
|
|
|
|
void Import(const std::vector<QueryableFuncDef>& defs);
|
|
|
|
void Import(const std::vector<QueryableVarDef>& defs);
|
|
|
|
void Update(const std::vector<QueryableTypeDef::DefUpdate>& updates);
|
|
|
|
void Update(const std::vector<QueryableFuncDef::DefUpdate>& updates);
|
|
|
|
void Update(const std::vector<QueryableVarDef::DefUpdate>& updates);
|
2017-02-27 08:57:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: For supporting vscode, lets'
|
|
|
|
// - have our normal daemon system
|
|
|
|
// - have frontend --language-server which accepts JSON RPC language server in stdin and emits language server
|
|
|
|
// JSON in stdout. vscode extension will run the executable this way. it will connect to daemon as normal.
|
|
|
|
// this means that vscode instance can be killed without actually killing core indexer process.
|
|
|
|
// $ indexer --language-server
|
|
|
|
// - maybe? have simple front end which lets user run
|
|
|
|
// $ indexer --action references --location foo.cc:20:5
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// https://github.com/Microsoft/vscode-languageserver-node/blob/master/client/src/main.ts
|