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
|
|
|
|
2018-02-18 05:52:14 +00:00
|
|
|
#include <forward_list>
|
2017-04-17 20:40:50 +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
|
|
|
|
2018-01-30 05:34:28 +00:00
|
|
|
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) {}
|
2018-01-30 05:34:28 +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;
|
2018-02-15 06:41:07 +00:00
|
|
|
std::vector<std::string> args;
|
2018-05-05 03:40:52 +00:00
|
|
|
LanguageId language;
|
2017-05-21 03:46:15 +00:00
|
|
|
// Includes in the file.
|
|
|
|
std::vector<IndexInclude> includes;
|
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-10-28 22:09:14 +00:00
|
|
|
// Parts of the file which are disabled.
|
|
|
|
std::vector<Range> inactive_regions;
|
2018-03-31 03:16:33 +00:00
|
|
|
// Used by |$ccls/freshenIndex|.
|
2018-01-27 17:29:28 +00:00
|
|
|
std::vector<std::string> dependencies;
|
2017-04-07 05:42:57 +00:00
|
|
|
};
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2018-01-30 05:34:28 +00:00
|
|
|
using DefUpdate = WithFileContent<Def>;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
int id = -1;
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<Def> def;
|
2018-04-30 04:49:03 +00:00
|
|
|
int symbol_idx = -1;
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2018-02-22 07:23:39 +00:00
|
|
|
template <typename Q, typename QDef>
|
2018-02-22 06:34:34 +00:00
|
|
|
struct QueryEntity {
|
2018-02-22 07:23:39 +00:00
|
|
|
using Def = QDef;
|
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
|
|
|
};
|
|
|
|
|
2018-05-04 04:20:10 +00:00
|
|
|
using UseUpdate =
|
|
|
|
std::unordered_map<Usr, std::pair<std::vector<Use>, std::vector<Use>>>;
|
|
|
|
using UsrUpdate =
|
|
|
|
std::unordered_map<Usr, std::pair<std::vector<Usr>, std::vector<Usr>>>;
|
2017-02-27 02:03:14 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
struct QueryFunc : QueryEntity<QueryFunc, FuncDef> {
|
2018-01-13 08:10:39 +00:00
|
|
|
Usr usr;
|
2018-04-30 04:49:03 +00:00
|
|
|
int symbol_idx = -1;
|
2018-02-18 07:09:05 +00:00
|
|
|
std::forward_list<Def> def;
|
2018-02-20 21:56:56 +00:00
|
|
|
std::vector<Use> declarations;
|
2018-02-10 20:53:18 +00:00
|
|
|
std::vector<Use> uses;
|
2018-04-30 04:49:03 +00:00
|
|
|
std::vector<Usr> derived;
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
struct QueryType : QueryEntity<QueryType, TypeDef> {
|
2018-01-13 08:10:39 +00:00
|
|
|
Usr usr;
|
2018-04-30 04:49:03 +00:00
|
|
|
int symbol_idx = -1;
|
2018-02-18 07:09:05 +00:00
|
|
|
std::forward_list<Def> def;
|
2018-02-10 20:53:18 +00:00
|
|
|
std::vector<Use> declarations;
|
|
|
|
std::vector<Use> uses;
|
2018-04-30 04:49:03 +00:00
|
|
|
std::vector<Usr> derived;
|
|
|
|
std::vector<Usr> instances;
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
struct QueryVar : QueryEntity<QueryVar, VarDef> {
|
2017-12-12 07:56:50 +00:00
|
|
|
Usr usr;
|
2018-04-30 04:49:03 +00:00
|
|
|
int symbol_idx = -1;
|
2018-02-18 05:52:14 +00:00
|
|
|
std::forward_list<Def> def;
|
2018-02-10 20:53:18 +00:00
|
|
|
std::vector<Use> declarations;
|
|
|
|
std::vector<Use> uses;
|
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.
|
2018-04-30 04:49:03 +00:00
|
|
|
static IndexUpdate CreateDelta(IndexFile* previous,
|
2017-09-22 01:14:57 +00:00
|
|
|
IndexFile* current);
|
2017-04-07 05:42:57 +00:00
|
|
|
|
|
|
|
// Merge |update| into this update; this can reduce overhead / index update
|
|
|
|
// work can be parallelized.
|
2018-02-06 07:22:44 +00:00
|
|
|
void Merge(IndexUpdate&& update);
|
2017-04-07 05:42:57 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
int file_id;
|
|
|
|
|
2017-02-27 07:23:43 +00:00
|
|
|
// File updates.
|
2018-04-30 04:49:03 +00:00
|
|
|
std::optional<std::string> files_removed;
|
|
|
|
std::optional<QueryFile::DefUpdate> files_def_update;
|
|
|
|
|
|
|
|
// Function updates.
|
|
|
|
std::vector<Usr> funcs_removed;
|
2018-05-04 04:20:10 +00:00
|
|
|
std::vector<std::pair<Usr, QueryFunc::Def>> funcs_def_update;
|
|
|
|
UseUpdate funcs_declarations;
|
|
|
|
UseUpdate funcs_uses;
|
|
|
|
UsrUpdate funcs_derived;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
|
|
|
// Type updates.
|
|
|
|
std::vector<Usr> types_removed;
|
2018-05-04 04:20:10 +00:00
|
|
|
std::vector<std::pair<Usr, QueryType::Def>> types_def_update;
|
|
|
|
UseUpdate types_declarations;
|
|
|
|
UseUpdate types_uses;
|
|
|
|
UsrUpdate types_derived;
|
|
|
|
UsrUpdate types_instances;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
|
|
|
// Variable updates.
|
2018-04-30 04:49:03 +00:00
|
|
|
std::vector<Usr> vars_removed;
|
2018-05-04 04:20:10 +00:00
|
|
|
std::vector<std::pair<Usr, QueryVar::Def>> vars_def_update;
|
|
|
|
UseUpdate vars_declarations;
|
|
|
|
UseUpdate vars_uses;
|
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 {
|
2018-02-02 01:59:01 +00:00
|
|
|
// All File/Func/Type/Var symbols.
|
2017-02-27 07:23:43 +00:00
|
|
|
std::vector<SymbolIdx> symbols;
|
|
|
|
|
2017-10-17 05:44:58 +00:00
|
|
|
std::vector<QueryFile> files;
|
2018-04-30 04:49:03 +00:00
|
|
|
std::unordered_map<std::string, int> name2file_id;
|
|
|
|
spp::sparse_hash_map<Usr, QueryFunc> usr2func;
|
|
|
|
spp::sparse_hash_map<Usr, QueryType> usr2type;
|
|
|
|
spp::sparse_hash_map<Usr, QueryVar> usr2var;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-04-19 06:56:37 +00:00
|
|
|
// Marks the given Usrs as invalid.
|
2017-05-09 04:20:28 +00:00
|
|
|
void RemoveUsrs(SymbolKind usr_kind, const std::vector<Usr>& to_remove);
|
2018-04-30 04:49:03 +00:00
|
|
|
void RemoveUsrs(SymbolKind usr_kind, int file_id, const std::vector<Usr>& to_remove);
|
2017-02-27 07:23:43 +00:00
|
|
|
// Insert the contents of |update| into |db|.
|
|
|
|
void ApplyIndexUpdate(IndexUpdate* update);
|
2018-04-30 04:49:03 +00:00
|
|
|
int Update(QueryFile::DefUpdate&& u);
|
2018-05-04 04:20:10 +00:00
|
|
|
void Update(int file_id, std::vector<std::pair<Usr, QueryType::Def>>&& us);
|
|
|
|
void Update(int file_id, std::vector<std::pair<Usr, QueryFunc::Def>>&& us);
|
|
|
|
void Update(int file_id, std::vector<std::pair<Usr, QueryVar::Def>>&& us);
|
|
|
|
void UpdateSymbols(int* symbol_idx, SymbolKind kind, Usr usr);
|
2018-04-30 04:49:03 +00:00
|
|
|
std::string_view GetSymbolName(int symbol_idx, bool qualified);
|
2017-04-07 08:01:58 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
QueryFile& GetFile(SymbolIdx ref) { return files[ref.usr]; }
|
|
|
|
QueryFunc& GetFunc(SymbolIdx ref) { return usr2func[ref.usr]; }
|
|
|
|
QueryType& GetType(SymbolIdx ref) { return usr2type[ref.usr]; }
|
|
|
|
QueryVar& GetVar(SymbolIdx ref) { return usr2var[ref.usr]; }
|
2017-04-08 07:52:57 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
QueryFunc& Func(Usr usr) { return usr2func[usr]; }
|
|
|
|
QueryType& Type(Usr usr) { return usr2type[usr]; }
|
|
|
|
QueryVar& Var(Usr usr) { return usr2var[usr]; }
|
2017-04-07 08:01:58 +00:00
|
|
|
};
|