This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
/
query.h
360 lines (313 loc) · 11.6 KB
/
query.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#pragma once
#include "indexer.h"
#include "serializer.h"
#include <sparsepp/spp.h>
#include <forward_list>
#include <functional>
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>;
struct IdMap;
// 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|.
template <typename TId, typename TValue>
struct MergeableUpdate {
// The type/func/var which is getting new usages.
TId id;
// Entries to add and remove.
std::vector<TValue> to_add;
std::vector<TValue> to_remove;
MergeableUpdate(TId id, std::vector<TValue>&& to_add)
: id(id), to_add(std::move(to_add)) {}
MergeableUpdate(TId id,
std::vector<TValue>&& to_add,
std::vector<TValue>&& to_remove)
: id(id), to_add(std::move(to_add)), to_remove(std::move(to_remove)) {}
};
template <typename TVisitor, typename TId, typename TValue>
void Reflect(TVisitor& visitor, MergeableUpdate<TId, TValue>& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(id);
REFLECT_MEMBER(to_add);
REFLECT_MEMBER(to_remove);
REFLECT_MEMBER_END();
}
template <typename T>
struct WithUsr {
Usr usr;
T value;
WithUsr(Usr usr, const T& value) : usr(usr), value(value) {}
WithUsr(Usr usr, T&& value) : usr(usr), value(std::move(value)) {}
};
template <typename TVisitor, typename T>
void Reflect(TVisitor& visitor, WithUsr<T>& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(usr);
REFLECT_MEMBER(value);
REFLECT_MEMBER_END();
}
template <typename T>
struct WithFileContent {
T value;
std::string file_content;
WithFileContent(const T& value, const std::string& file_content)
: value(value), file_content(file_content) {}
};
template <typename TVisitor, typename T>
void Reflect(TVisitor& visitor, WithFileContent<T>& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(value);
REFLECT_MEMBER(file_content);
REFLECT_MEMBER_END();
}
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 {
Id<QueryFile> file;
AbsolutePath path;
std::vector<std::string> args;
// Language identifier
std::string language;
// 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;
// Used by |$cquery/freshenIndex|.
std::vector<AbsolutePath> dependencies;
};
using DefUpdate = WithFileContent<Def>;
optional<Def> def;
Maybe<Id<void>> symbol_idx;
explicit QueryFile(const AbsolutePath& path) {
def = Def();
def->path = path;
}
};
MAKE_REFLECT_STRUCT(QueryFile::Def,
file,
path,
args,
language,
outline,
all_symbols,
inactive_regions,
dependencies);
template <typename Q, typename QDef>
struct QueryEntity {
using Def = QDef;
using DefUpdate = WithUsr<Def>;
using DeclarationsUpdate = MergeableUpdate<Id<Q>, Use>;
using UsesUpdate = MergeableUpdate<Id<Q>, Use>;
Def* AnyDef() {
Def* ret = nullptr;
for (auto& i : static_cast<Q*>(this)->def) {
ret = &i;
if (i.spell)
break;
}
return ret;
}
const Def* AnyDef() const { return const_cast<QueryEntity*>(this)->AnyDef(); }
};
struct QueryType : QueryEntity<QueryType, TypeDefDefinitionData<QueryFamily>> {
using DerivedUpdate = MergeableUpdate<QueryTypeId, QueryTypeId>;
using InstancesUpdate = MergeableUpdate<QueryTypeId, QueryVarId>;
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;
explicit QueryType(const Usr& usr) : usr(usr) {}
};
struct QueryFunc : QueryEntity<QueryFunc, FuncDefDefinitionData<QueryFamily>> {
using DerivedUpdate = MergeableUpdate<QueryFuncId, QueryFuncId>;
Usr usr;
Maybe<Id<void>> symbol_idx;
std::forward_list<Def> def;
std::vector<Use> declarations;
std::vector<QueryFuncId> derived;
std::vector<Use> uses;
explicit QueryFunc(const Usr& usr) : usr(usr) {}
};
struct QueryVar : QueryEntity<QueryVar, VarDefDefinitionData<QueryFamily>> {
Usr usr;
Maybe<Id<void>> symbol_idx;
std::forward_list<Def> def;
std::vector<Use> declarations;
std::vector<Use> uses;
explicit QueryVar(const Usr& usr) : usr(usr) {}
};
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.
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);
// Dump the update to a string.
std::string ToString();
// File updates.
std::vector<AbsolutePath> files_removed;
std::vector<QueryFile::DefUpdate> files_def_update;
// 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;
std::vector<QueryType::InstancesUpdate> types_instances;
std::vector<QueryType::UsesUpdate> types_uses;
// 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;
// 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;
private:
// Creates an index update assuming that |previous| is already
// in the index, so only the delta between |previous| and |current|
// will be applied.
IndexUpdate(const IdMap& previous_id_map,
const IdMap& current_id_map,
IndexFile& previous,
IndexFile& current);
};
// NOTICE: We're not reflecting on files_removed or files_def_update, it is too
// much output when logging
MAKE_REFLECT_STRUCT(IndexUpdate,
types_removed,
types_def_update,
types_derived,
types_instances,
types_uses,
funcs_removed,
funcs_def_update,
funcs_declarations,
funcs_derived,
funcs_uses,
vars_removed,
vars_def_update,
vars_declarations,
vars_uses);
// The query database is heavily optimized for fast queries. It is stored
// in-memory.
struct QueryDatabase {
// All File/Func/Type/Var symbols.
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;
// Lookup symbol based on a usr.
spp::sparse_hash_map<AbsolutePath, 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;
// Marks the given Usrs as invalid.
void RemoveUsrs(SymbolKind usr_kind, const std::vector<Usr>& to_remove);
void RemoveUsrs(SymbolKind usr_kind,
const std::vector<WithUsr<QueryFileId>>& to_remove);
// Insert the contents of |update| into |db|.
void ApplyIndexUpdate(IndexUpdate* update);
void ImportOrUpdate(const std::vector<QueryFile::DefUpdate>& updates);
void ImportOrUpdate(std::vector<QueryType::DefUpdate>&& updates);
void ImportOrUpdate(std::vector<QueryFunc::DefUpdate>&& updates);
void ImportOrUpdate(std::vector<QueryVar::DefUpdate>&& updates);
void UpdateSymbols(Maybe<Id<void>>* symbol_idx,
SymbolKind kind,
Id<void> idx);
std::string_view GetSymbolDetailedName(RawId symbol_idx) const;
std::string_view GetSymbolShortName(RawId symbol_idx) 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);
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]; }
};
template <typename I>
struct IndexToQuery;
// clang-format off
template <> struct IndexToQuery<IndexFileId> { using type = QueryFileId; };
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; };
template <typename I> struct IndexToQuery<optional<I>> {
using type = optional<typename IndexToQuery<I>::type>;
};
template <typename I> struct IndexToQuery<std::vector<I>> {
using type = std::vector<typename IndexToQuery<I>::type>;
};
// clang-format on
struct IdMap {
const IdCache& local_ids;
QueryFileId primary_file;
IdMap(QueryDatabase* query_db, const IdCache& local_ids);
// 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;
template <typename I>
Maybe<typename IndexToQuery<I>::type> ToQuery(Maybe<I> id) const {
if (!id)
return nullopt;
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;
}
// clang-format on
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_;
};