ccls/src/query.h

311 lines
9.9 KiB
C
Raw Normal View History

2017-02-23 09:23:23 +00:00
#pragma once
2017-02-26 08:11:47 +00:00
#include "indexer.h"
#include "serializer.h"
2017-02-26 08:11:47 +00:00
#include <sparsepp/spp.h>
2017-04-08 01:35:12 +00:00
#include <forward_list>
2017-04-17 20:40:50 +00:00
struct QueryFile;
struct QueryType;
struct QueryFunc;
struct QueryVar;
struct QueryDatabase;
using QueryFileId = Id<QueryFile>;
using QueryTypeId = Id<QueryType>;
using QueryFuncId = Id<QueryFunc>;
using QueryVarId = Id<QueryVar>;
2018-02-04 00:20:14 +00:00
struct IdMap;
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-09-22 01:14:57 +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.
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
2018-02-07 06:46:05 +00:00
MergeableUpdate(TId id, std::vector<TValue>&& to_add)
: id(id), to_add(std::move(to_add)) {}
2017-09-22 01:14:57 +00:00
MergeableUpdate(TId id,
2018-02-07 06:46:05 +00:00
std::vector<TValue>&& to_add,
std::vector<TValue>&& to_remove)
: id(id), to_add(std::move(to_add)), to_remove(std::move(to_remove)) {}
2017-02-27 07:23:43 +00:00
};
template <typename T>
struct WithUsr {
2018-01-13 08:10:39 +00:00
Usr usr;
T value;
2018-01-13 08:10:39 +00:00
WithUsr(Usr usr, const T& value) : usr(usr), value(value) {}
WithUsr(Usr usr, T&& value) : usr(usr), value(std::move(value)) {}
};
template <typename T>
struct WithFileContent {
T value;
std::string file_content;
2018-02-22 07:34:32 +00:00
WithFileContent(const T& value, const std::string& file_content)
: value(value), file_content(file_content) {}
};
struct QueryFamily {
using FileId = Id<QueryFile>;
using FuncId = Id<QueryFunc>;
using TypeId = Id<QueryType>;
using VarId = Id<QueryVar>;
using Range = Reference;
};
struct QueryFile {
struct Def {
2018-02-18 18:07:13 +00:00
Id<QueryFile> file;
std::string path;
2018-02-15 06:41:07 +00:00
std::vector<std::string> args;
// Language identifier
std::string language;
2017-05-21 03:46:15 +00:00
// Includes in the file.
std::vector<IndexInclude> includes;
// Outline of the file (ie, for code lens).
std::vector<SymbolRef> outline;
// Every symbol found in the file (ie, for goto definition)
std::vector<SymbolRef> all_symbols;
// Parts of the file which are disabled.
std::vector<Range> inactive_regions;
2018-03-31 03:16:33 +00:00
// Used by |$ccls/freshenIndex|.
std::vector<std::string> dependencies;
};
2017-02-27 07:23:43 +00:00
using DefUpdate = WithFileContent<Def>;
2017-02-27 07:23:43 +00:00
2018-03-31 03:16:33 +00:00
std::optional<Def> def;
Maybe<Id<void>> symbol_idx;
explicit QueryFile(const std::string& path) {
def = Def();
def->path = path;
}
2017-02-26 08:11:47 +00:00
};
2017-03-14 08:33:39 +00:00
template <typename Q, typename QDef>
2018-02-22 06:34:34 +00:00
struct QueryEntity {
using Def = QDef;
using DefUpdate = WithUsr<Def>;
using DeclarationsUpdate = MergeableUpdate<Id<Q>, Use>;
using UsesUpdate = MergeableUpdate<Id<Q>, Use>;
2018-02-22 06:34:34 +00:00
Def* AnyDef() {
Def* ret = nullptr;
for (auto& i : static_cast<Q*>(this)->def) {
ret = &i;
if (i.spell)
break;
}
return ret;
}
2018-02-22 07:34:32 +00:00
const Def* AnyDef() const { return const_cast<QueryEntity*>(this)->AnyDef(); }
2018-02-22 06:34:34 +00:00
};
struct QueryType : QueryEntity<QueryType, TypeDef<QueryFamily>> {
using DerivedUpdate = MergeableUpdate<QueryTypeId, QueryTypeId>;
2017-04-21 07:03:33 +00:00
using InstancesUpdate = MergeableUpdate<QueryTypeId, QueryVarId>;
2017-02-27 02:03:14 +00:00
2018-01-13 08:10:39 +00:00
Usr usr;
Maybe<Id<void>> symbol_idx;
std::forward_list<Def> def;
std::vector<Use> declarations;
std::vector<QueryTypeId> derived;
std::vector<QueryVarId> instances;
std::vector<Use> uses;
2017-02-26 08:11:47 +00:00
explicit QueryType(const Usr& usr) : usr(usr) {}
2017-02-26 08:11:47 +00:00
};
2017-03-14 08:33:39 +00:00
struct QueryFunc : QueryEntity<QueryFunc, FuncDef<QueryFamily>> {
using DerivedUpdate = MergeableUpdate<QueryFuncId, QueryFuncId>;
2017-02-27 02:03:14 +00:00
2018-01-13 08:10:39 +00:00
Usr usr;
Maybe<Id<void>> symbol_idx;
std::forward_list<Def> def;
std::vector<Use> declarations;
std::vector<QueryFuncId> derived;
std::vector<Use> uses;
2017-02-26 08:11:47 +00:00
explicit QueryFunc(const Usr& usr) : usr(usr) {}
2017-02-26 08:11:47 +00:00
};
2017-03-14 08:33:39 +00:00
struct QueryVar : QueryEntity<QueryVar, VarDef<QueryFamily>> {
Usr usr;
Maybe<Id<void>> symbol_idx;
std::forward_list<Def> def;
std::vector<Use> declarations;
std::vector<Use> uses;
2017-02-26 08:11:47 +00:00
explicit QueryVar(const Usr& usr) : usr(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 {
// Creates a new IndexUpdate based on the delta from previous to current. If
// no delta computation should be done just pass null for previous.
2017-09-22 01:14:57 +00:00
static IndexUpdate CreateDelta(const IdMap* previous_id_map,
const IdMap* current_id_map,
IndexFile* previous,
IndexFile* current);
// Merge |update| into this update; this can reduce overhead / index update
// work can be parallelized.
void Merge(IndexUpdate&& update);
2017-05-19 07:02:01 +00:00
// Dump the update to a string.
std::string ToString();
2017-02-27 07:23:43 +00:00
// File updates.
std::vector<std::string> files_removed;
std::vector<QueryFile::DefUpdate> files_def_update;
2017-02-27 07:23:43 +00:00
// Type updates.
std::vector<Usr> types_removed;
std::vector<QueryType::DefUpdate> types_def_update;
std::vector<QueryType::DeclarationsUpdate> types_declarations;
std::vector<QueryType::DerivedUpdate> types_derived;
2017-04-21 07:03:33 +00:00
std::vector<QueryType::InstancesUpdate> types_instances;
std::vector<QueryType::UsesUpdate> types_uses;
2017-02-27 07:23:43 +00:00
// Function updates.
std::vector<WithUsr<QueryFileId>> funcs_removed;
std::vector<QueryFunc::DefUpdate> funcs_def_update;
std::vector<QueryFunc::DeclarationsUpdate> funcs_declarations;
std::vector<QueryFunc::DerivedUpdate> funcs_derived;
std::vector<QueryFunc::UsesUpdate> funcs_uses;
2017-02-27 07:23:43 +00:00
// Variable updates.
std::vector<WithUsr<QueryFileId>> vars_removed;
std::vector<QueryVar::DefUpdate> vars_def_update;
std::vector<QueryVar::DeclarationsUpdate> vars_declarations;
std::vector<QueryVar::UsesUpdate> vars_uses;
2017-02-27 07:23:43 +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-09-22 01:14:57 +00:00
IndexUpdate(const IdMap& previous_id_map,
const IdMap& current_id_map,
IndexFile& previous,
IndexFile& current);
2017-02-27 07:23:43 +00:00
};
// The query database is heavily optimized for fast queries. It is stored
// in-memory.
struct QueryDatabase {
// All File/Func/Type/Var symbols.
2017-02-27 07:23:43 +00:00
std::vector<SymbolIdx> symbols;
// Raw data storage. Accessible via SymbolIdx instances.
std::vector<QueryFile> files;
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.
2018-03-31 20:59:27 +00:00
std::unordered_map<std::string, QueryFileId> usr_to_file;
spp::sparse_hash_map<Usr, QueryTypeId> usr_to_type;
spp::sparse_hash_map<Usr, QueryFuncId> usr_to_func;
spp::sparse_hash_map<Usr, QueryVarId> usr_to_var;
2017-02-27 07:23:43 +00:00
// Marks the given Usrs as invalid.
void RemoveUsrs(SymbolKind usr_kind, const std::vector<Usr>& to_remove);
2018-02-22 07:34:32 +00:00
void RemoveUsrs(SymbolKind usr_kind,
const std::vector<WithUsr<QueryFileId>>& to_remove);
2017-02-27 07:23:43 +00:00
// Insert the contents of |update| into |db|.
void ApplyIndexUpdate(IndexUpdate* update);
void ImportOrUpdate(const std::vector<QueryFile::DefUpdate>& updates);
2018-02-07 05:26:38 +00:00
void ImportOrUpdate(std::vector<QueryType::DefUpdate>&& updates);
void ImportOrUpdate(std::vector<QueryFunc::DefUpdate>&& updates);
void ImportOrUpdate(std::vector<QueryVar::DefUpdate>&& updates);
2018-02-22 07:34:32 +00:00
void UpdateSymbols(Maybe<Id<void>>* symbol_idx,
SymbolKind kind,
Id<void> idx);
std::string_view GetSymbolName(RawId symbol_idx, bool qualified) const;
// Query the indexing structure to look up symbol id for given Usr.
Maybe<QueryFileId> GetQueryFileIdFromPath(const std::string& path);
Maybe<QueryTypeId> GetQueryTypeIdFromUsr(Usr usr);
Maybe<QueryFuncId> GetQueryFuncIdFromUsr(Usr usr);
Maybe<QueryVarId> GetQueryVarIdFromUsr(Usr usr);
2018-02-22 07:34:32 +00:00
QueryFile& GetFile(SymbolIdx ref) { return files[ref.id.id]; }
QueryFunc& GetFunc(SymbolIdx ref) { return funcs[ref.id.id]; }
QueryType& GetType(SymbolIdx ref) { return types[ref.id.id]; }
QueryVar& GetVar(SymbolIdx ref) { return vars[ref.id.id]; }
2017-04-07 08:01:58 +00:00
};
2018-02-04 02:16:30 +00:00
template <typename I>
struct IndexToQuery;
// clang-format off
template <> struct IndexToQuery<IndexFileId> { using type = QueryFileId; };
2018-02-04 02:16:30 +00:00
template <> struct IndexToQuery<IndexFuncId> { using type = QueryFuncId; };
template <> struct IndexToQuery<IndexTypeId> { using type = QueryTypeId; };
template <> struct IndexToQuery<IndexVarId> { using type = QueryVarId; };
template <> struct IndexToQuery<Use> { using type = Use; };
template <> struct IndexToQuery<SymbolRef> { using type = SymbolRef; };
template <> struct IndexToQuery<IndexFunc::Declaration> { using type = Use; };
2018-03-31 03:16:33 +00:00
template <typename I> struct IndexToQuery<std::optional<I>> {
using type = std::optional<typename IndexToQuery<I>::type>;
2018-02-04 02:16:30 +00:00
};
template <typename I> struct IndexToQuery<std::vector<I>> {
using type = std::vector<typename IndexToQuery<I>::type>;
};
// clang-format on
2017-04-07 08:01:58 +00:00
struct IdMap {
const IdCache& local_ids;
QueryFileId primary_file;
2017-04-07 08:01:58 +00:00
IdMap(QueryDatabase* query_db, const IdCache& local_ids);
2017-04-07 08:01:58 +00:00
2018-02-04 00:20:14 +00:00
// FIXME Too verbose
// clang-format off
QueryTypeId ToQuery(IndexTypeId id) const;
QueryFuncId ToQuery(IndexFuncId id) const;
QueryVarId ToQuery(IndexVarId id) const;
SymbolRef ToQuery(SymbolRef ref) const;
Use ToQuery(Reference ref) const;
Use ToQuery(Use ref) const;
Use ToQuery(IndexFunc::Declaration decl) const;
2018-02-04 02:16:30 +00:00
template <typename I>
Maybe<typename IndexToQuery<I>::type> ToQuery(Maybe<I> id) const {
if (!id)
2018-03-31 03:16:33 +00:00
return std::nullopt;
2018-02-04 02:16:30 +00:00
return ToQuery(*id);
}
template <typename I>
std::vector<typename IndexToQuery<I>::type> ToQuery(const std::vector<I>& a) const {
std::vector<typename IndexToQuery<I>::type> ret;
ret.reserve(a.size());
for (auto& x : a)
ret.push_back(ToQuery(x));
return ret;
}
2018-02-04 00:20:14 +00:00
// clang-format on
2017-04-08 07:52:57 +00:00
2017-09-22 01:14:57 +00:00
private:
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_;
2017-04-07 08:01:58 +00:00
};