Remove $ccls/random; remove DB::symbols; decrease DB::entities grow rate

This commit is contained in:
Fangrui Song 2018-05-29 23:56:14 -07:00
parent 42bcf2b58f
commit bb08fdfa02
21 changed files with 346 additions and 420 deletions

View File

@ -230,7 +230,6 @@ target_sources(ccls PRIVATE
src/messages/ccls_freshen_index.cc
src/messages/ccls_inheritance_hierarchy.cc
src/messages/ccls_member_hierarchy.cc
src/messages/ccls_random.cc
src/messages/ccls_vars.cc
src/messages/exit.cc
src/messages/initialize.cc

View File

@ -64,7 +64,7 @@ struct SymbolRef : Reference {
: Reference{range, usr, kind, role} {}
};
// Represents an occurrence of a variable/type, |id,kind| refer to the lexical
// Represents an occurrence of a variable/type, |usr,kind| refer to the lexical
// parent.
struct Use : Reference {
// |file| is used in Query* but not in Index*
@ -239,7 +239,10 @@ struct VarDef : NameMixin<VarDef> {
// (declaration).
StorageClass storage = StorageClass::Invalid;
bool is_local() const { return kind == lsSymbolKind::Variable; }
bool is_local() const {
return spell && spell->kind != SymbolKind::File &&
storage == StorageClass::None;
}
std::vector<Usr> GetBases() const { return {}; }
bool operator==(const VarDef& o) const {

View File

@ -125,7 +125,7 @@ MessageHandler::MessageHandler() {
// static
std::vector<MessageHandler*>* MessageHandler::message_handlers = nullptr;
bool FindFileOrFail(QueryDatabase* db,
bool FindFileOrFail(DB* db,
Project* project,
std::optional<lsRequestId> id,
const std::string& absolute_path,
@ -186,7 +186,7 @@ void EmitInactiveLines(WorkingFile* working_file,
pipeline::WriteStdout(kMethodType_CclsPublishInactiveRegions, out);
}
void EmitSemanticHighlighting(QueryDatabase* db,
void EmitSemanticHighlighting(DB* db,
SemanticHighlightSymbolCache* semantic_cache,
WorkingFile* working_file,
QueryFile* file) {

View File

@ -20,7 +20,7 @@ struct ImportManager;
struct IncludeComplete;
struct MultiQueueWaiter;
struct Project;
struct QueryDatabase;
struct DB;
struct WorkingFile;
struct WorkingFiles;
@ -101,7 +101,7 @@ MAKE_REFLECT_STRUCT(Out_CclsPublishSemanticHighlighting,
static type type##message_handler_instance_;
struct MessageHandler {
QueryDatabase* db = nullptr;
DB* db = nullptr;
MultiQueueWaiter* waiter = nullptr;
Project* project = nullptr;
DiagnosticsEngine* diag_engine = nullptr;
@ -134,7 +134,7 @@ struct BaseMessageHandler : MessageHandler {
}
};
bool FindFileOrFail(QueryDatabase* db,
bool FindFileOrFail(DB* db,
Project* project,
std::optional<lsRequestId> id,
const std::string& absolute_path,
@ -144,7 +144,7 @@ bool FindFileOrFail(QueryDatabase* db,
void EmitInactiveLines(WorkingFile* working_file,
const std::vector<Range>& inactive_regions);
void EmitSemanticHighlighting(QueryDatabase* db,
void EmitSemanticHighlighting(DB* db,
SemanticHighlightSymbolCache* semantic_cache,
WorkingFile* working_file,
QueryFile* file);

View File

@ -34,13 +34,13 @@ struct Handler_CclsBase : BaseMessageHandler<In_CclsBase> {
FindSymbolsAtLocation(working_file, file, request->params.position)) {
if (sym.kind == SymbolKind::Type) {
if (const auto* def = db->GetType(sym).AnyDef())
out.result = GetLsLocationExs(
db, working_files, GetDeclarations(db->usr2type, def->bases));
out.result = GetLsLocationExs(db, working_files,
GetTypeDeclarations(db, def->bases));
break;
} else if (sym.kind == SymbolKind::Func) {
if (const auto* def = db->GetFunc(sym).AnyDef())
out.result = GetLsLocationExs(
db, working_files, GetDeclarations(db->usr2func, def->bases));
out.result = GetLsLocationExs(db, working_files,
GetFuncDeclarations(db, def->bases));
break;
}
}

View File

@ -136,7 +136,7 @@ bool Expand(MessageHandler* m,
const QueryFunc& func1 = *stack.back();
stack.pop_back();
if (auto* def1 = func1.AnyDef()) {
EachDefinedEntity(m->db->usr2func, def1->bases, [&](QueryFunc& func2) {
EachDefinedFunc(m->db, def1->bases, [&](QueryFunc& func2) {
if (!seen.count(func2.usr)) {
seen.insert(func2.usr);
stack.push_back(&func2);
@ -153,7 +153,7 @@ bool Expand(MessageHandler* m,
while (stack.size()) {
const QueryFunc& func1 = *stack.back();
stack.pop_back();
EachDefinedEntity(m->db->usr2func, func1.derived, [&](QueryFunc& func2) {
EachDefinedFunc(m->db, func1.derived, [&](QueryFunc& func2) {
if (!seen.count(func2.usr)) {
seen.insert(func2.usr);
stack.push_back(&func2);
@ -206,7 +206,7 @@ struct Handler_CclsCallHierarchy
entry.id = std::to_string(params.usr);
entry.usr = params.usr;
entry.callType = CallType::Direct;
if (db->usr2func.count(params.usr))
if (db->HasFunc(params.usr))
Expand(this, &entry, params.callee, params.callType, params.qualified,
params.levels);
out.result = std::move(entry);

View File

@ -169,8 +169,8 @@ struct Handler_CclsInheritanceHierarchy
entry.id = std::to_string(params.usr);
entry.usr = params.usr;
entry.kind = params.kind;
if (((entry.kind == SymbolKind::Func && db->usr2func.count(entry.usr)) ||
(entry.kind == SymbolKind::Type && db->usr2type.count(entry.usr))) &&
if (((entry.kind == SymbolKind::Func && db->HasFunc(entry.usr)) ||
(entry.kind == SymbolKind::Type && db->HasType(entry.usr))) &&
Expand(this, &entry, params.derived, params.qualified, params.levels))
out.result = std::move(entry);
} else {

View File

@ -138,7 +138,7 @@ bool Expand(MessageHandler* m,
const auto* def = stack.back()->AnyDef();
stack.pop_back();
if (def) {
EachDefinedEntity(m->db->usr2type, def->bases, [&](QueryType& type1) {
EachDefinedType(m->db, def->bases, [&](QueryType& type1) {
if (!seen.count(type1.usr)) {
seen.insert(type1.usr);
stack.push_back(&type1);
@ -171,7 +171,7 @@ bool Expand(MessageHandler* m,
}
} else {
for (auto it : def->vars) {
QueryVar& var = m->db->usr2var[it.first];
QueryVar& var = m->db->Var(it.first);
if (!var.def.empty())
DoField(m, entry, var, it.second, qualified, levels - 1);
}
@ -208,7 +208,7 @@ struct Handler_CclsMemberHierarchy
GetLsLocation(db, working_files, *def->spell))
entry.location = *loc;
}
EachDefinedEntity(db->usr2var, def->vars, [&](QueryVar& var) {
EachDefinedVar(db, def->vars, [&](QueryVar& var) {
DoField(this, &entry, var, -1, qualified, levels - 1);
});
return entry;
@ -247,7 +247,7 @@ struct Handler_CclsMemberHierarchy
entry.id = std::to_string(params.usr);
entry.usr = params.usr;
// entry.name is empty as it is known by the client.
if (db->usr2type.count(entry.usr) &&
if (db->HasType(entry.usr) &&
Expand(this, &entry, params.qualified, params.levels))
out.result = std::move(entry);
} else {

View File

@ -1,134 +0,0 @@
#include "message_handler.h"
#include "query_utils.h"
#include "pipeline.hh"
using namespace ccls;
#include <math.h>
#include <stdlib.h>
#include <numeric>
MAKE_HASHABLE(SymbolIdx, t.usr, t.kind);
namespace {
MethodType kMethodType = "$ccls/random";
struct In_CclsRandom : public RequestInMessage {
MethodType GetMethodType() const override { return kMethodType; }
};
MAKE_REFLECT_STRUCT(In_CclsRandom, id);
REGISTER_IN_MESSAGE(In_CclsRandom);
const double kDeclWeight = 3;
const double kDamping = 0.1;
template <auto Q>
void Add(const std::unordered_map<SymbolIdx, int>& sym2id,
std::vector<std::unordered_map<int, double>>& adj,
const std::vector<Usr>& collection,
int n,
double w = 1) {
for (Usr usr : collection) {
auto it = sym2id.find(SymbolIdx{usr, Q});
if (it != sym2id.end())
adj[it->second][n] += w;
}
}
struct Handler_CclsRandom : BaseMessageHandler<In_CclsRandom> {
MethodType GetMethodType() const override { return kMethodType; }
void Run(In_CclsRandom* request) override {
std::unordered_map<SymbolIdx, int> sym2id;
std::vector<SymbolIdx> syms;
int n = 0;
for (auto& it : db->usr2func)
if (it.second.AnyDef()) {
syms.push_back(SymbolIdx{it.first, SymbolKind::Func});
sym2id[syms.back()] = n++;
}
for (auto& it : db->usr2type)
if (it.second.AnyDef()) {
syms.push_back(SymbolIdx{it.first, SymbolKind::Type});
sym2id[syms.back()] = n++;
}
for (auto& it : db->usr2var)
if (it.second.AnyDef()) {
syms.push_back(SymbolIdx{it.first, SymbolKind::Var});
sym2id[syms.back()] = n++;
}
std::vector<std::unordered_map<int, double>> adj(n);
auto add = [&](const std::vector<Use>& uses, double w) {
for (Use use : uses) {
auto it = sym2id.find(use);
if (it != sym2id.end())
adj[it->second][n] += w;
}
};
n = 0;
for (auto& it : db->usr2func)
if (it.second.AnyDef()) {
add(it.second.declarations, kDeclWeight);
add(it.second.uses, 1);
Add<SymbolKind::Func>(sym2id, adj, it.second.derived, n);
n++;
}
for (auto& it : db->usr2type)
if (const auto* def = it.second.AnyDef()) {
add(it.second.uses, 1);
Add<SymbolKind::Var>(sym2id, adj, it.second.instances, n);
Add<SymbolKind::Func>(sym2id, adj, def->funcs, n);
Add<SymbolKind::Type>(sym2id, adj, def->types, n);
//Add<SymbolKind::Var>(sym2id, adj, def->vars, n);
n++;
}
for (auto& it : db->usr2var)
if (it.second.AnyDef()) {
add(it.second.declarations, kDeclWeight);
add(it.second.uses, 1);
n++;
}
for (int i = 0; i < n; i++) {
double sum = 0;
adj[i][i] += 1;
for (auto& it : adj[i])
sum += it.second;
for (auto& it : adj[i])
it.second = it.second / sum * (1 - kDamping);
}
std::vector<double> x(n, 1), y;
for (int j = 0; j < 8; j++) {
y.assign(n, kDamping);
for (int i = 0; i < n; i++)
for (auto& it : adj[i])
y[it.first] += x[i] * it.second;
double d = 0;
for (int i = 0; i < n; i++)
d = std::max(d, fabs(x[i] - y[i]));
if (d < 1e-5)
break;
x.swap(y);
}
double sum = std::accumulate(x.begin(), x.end(), 0.);
Out_LocationList out;
out.id = request->id;
double roulette = rand() / (RAND_MAX + 1.0) * sum;
sum = 0;
for (int i = 0; i < n; i++) {
sum += x[i];
if (sum >= roulette) {
if (Maybe<Use> use = GetDefinitionExtent(db, syms[i]))
if (auto ls_loc = GetLsLocationEx(db, working_files, *use,
g_config->xref.container))
out.result.push_back(*ls_loc);
break;
}
}
pipeline::WriteStdout(kMethodType, out);
}
};
REGISTER_MESSAGE_HANDLER(Handler_CclsRandom);
} // namespace

View File

@ -43,9 +43,9 @@ struct Handler_CclsVars : BaseMessageHandler<In_CclsVars> {
[[fallthrough]];
}
case SymbolKind::Type:
out.result = GetLsLocationExs(
db, working_files,
GetDeclarations(db->usr2var, db->Type(usr).instances));
out.result =
GetLsLocationExs(db, working_files,
GetVarDeclarations(db, db->Type(usr).instances));
break;
}
}

View File

@ -31,7 +31,7 @@ MAKE_REFLECT_STRUCT(Out_TextDocumentCodeLens, jsonrpc, id, result);
struct CommonCodeLensParams {
std::vector<TCodeLens>* result;
QueryDatabase* db;
DB* db;
WorkingFiles* working_files;
WorkingFile* working_file;
};
@ -118,10 +118,10 @@ struct Handler_TextDocumentCodeLens
AddCodeLens("ref", "refs", &common, OffsetStartColumn(use, 0),
type.uses, true /*force_display*/);
AddCodeLens("derived", "derived", &common, OffsetStartColumn(use, 1),
GetDeclarations(db->usr2type, type.derived),
GetTypeDeclarations(db, type.derived),
false /*force_display*/);
AddCodeLens("var", "vars", &common, OffsetStartColumn(use, 2),
GetDeclarations(db->usr2var, type.instances),
GetVarDeclarations(db, type.instances),
false /*force_display*/);
break;
}
@ -168,7 +168,7 @@ struct Handler_TextDocumentCodeLens
AddCodeLens("derived", "derived", &common,
OffsetStartColumn(use, offset++),
GetDeclarations(db->usr2func, func.derived),
GetFuncDeclarations(db, func.derived),
false /*force_display*/);
// "Base"
@ -196,7 +196,7 @@ struct Handler_TextDocumentCodeLens
}
} else {
AddCodeLens("base", "base", &common, OffsetStartColumn(use, 1),
GetDeclarations(db->usr2type, def->bases),
GetTypeDeclarations(db, def->bases),
false /*force_display*/);
}

View File

@ -25,7 +25,7 @@ struct Out_TextDocumentDefinition
};
MAKE_REFLECT_STRUCT(Out_TextDocumentDefinition, jsonrpc, id, result);
std::vector<Use> GetNonDefDeclarationTargets(QueryDatabase* db, SymbolRef sym) {
std::vector<Use> GetNonDefDeclarationTargets(DB* db, SymbolRef sym) {
switch (sym.kind) {
case SymbolKind::Var: {
std::vector<Use> ret = GetNonDefDeclarations(db, sym);
@ -135,18 +135,16 @@ struct Handler_TextDocumentDefinition
// substring, we use the tuple <length difference, negative position,
// not in the same file, line distance> to find the best match.
std::tuple<int, int, bool, int> best_score{INT_MAX, 0, true, 0};
int best_i = -1;
for (int i = 0; i < (int)db->symbols.size(); ++i) {
if (db->symbols[i].kind == SymbolKind::Invalid)
continue;
std::string_view short_name = db->GetSymbolName(i, false),
SymbolIdx best_sym;
best_sym.kind = SymbolKind::Invalid;
auto fn = [&](SymbolIdx sym) {
std::string_view short_name = db->GetSymbolName(sym, false),
name = short_query.size() < query.size()
? db->GetSymbolName(i, true)
? db->GetSymbolName(sym, true)
: short_name;
if (short_name != short_query)
continue;
if (Maybe<Use> use = GetDefinitionSpell(db, db->symbols[i])) {
return;
if (Maybe<Use> use = GetDefinitionSpell(db, sym)) {
std::tuple<int, int, bool, int> score{
int(name.size() - short_query.size()), 0,
use->file_id != file_id,
@ -160,12 +158,20 @@ struct Handler_TextDocumentDefinition
}
if (score < best_score) {
best_score = score;
best_i = i;
best_sym = sym;
}
}
}
if (best_i != -1) {
Maybe<Use> use = GetDefinitionSpell(db, db->symbols[best_i]);
};
for (auto& func : db->funcs)
fn({func.usr, SymbolKind::Func});
for (auto& type : db->types)
fn({type.usr, SymbolKind::Type});
for (auto& var : db->vars)
if (var.def.size() && !var.def[0].is_local())
fn({var.usr, SymbolKind::Var});
if (best_sym.kind != SymbolKind::Invalid) {
Maybe<Use> use = GetDefinitionSpell(db, best_sym);
assert(use);
if (auto ls_loc = GetLsLocationEx(db, working_files, *use,
g_config->xref.container))

View File

@ -7,7 +7,7 @@ namespace {
MethodType kMethodType = "textDocument/hover";
// Find the comments for |sym|, if any.
std::optional<lsMarkedString> GetComments(QueryDatabase* db, SymbolRef sym) {
std::optional<lsMarkedString> GetComments(DB* db, SymbolRef sym) {
std::optional<lsMarkedString> ret;
WithEntity(db, sym, [&](const auto& entity) {
if (const auto* def = entity.AnyDef())
@ -21,7 +21,7 @@ std::optional<lsMarkedString> GetComments(QueryDatabase* db, SymbolRef sym) {
}
// Returns the hover or detailed name for `sym`, if any.
std::optional<lsMarkedString> GetHoverOrName(QueryDatabase* db,
std::optional<lsMarkedString> GetHoverOrName(DB* db,
LanguageId lang,
SymbolRef sym) {
std::optional<lsMarkedString> ret;

View File

@ -32,13 +32,13 @@ struct Handler_TextDocumentImplementation
FindSymbolsAtLocation(working_file, file, request->params.position)) {
if (sym.kind == SymbolKind::Type) {
QueryType& type = db->GetType(sym);
out.result = GetLsLocationExs(
db, working_files, GetDeclarations(db->usr2type, type.derived));
out.result = GetLsLocationExs(db, working_files,
GetTypeDeclarations(db, type.derived));
break;
} else if (sym.kind == SymbolKind::Func) {
QueryFunc& func = db->GetFunc(sym);
out.result = GetLsLocationExs(
db, working_files, GetDeclarations(db->usr2func, func.derived));
out.result = GetLsLocationExs(db, working_files,
GetFuncDeclarations(db, func.derived));
break;
}
}

View File

@ -6,7 +6,7 @@ using namespace ccls;
namespace {
MethodType kMethodType = "textDocument/rename";
lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db,
lsWorkspaceEdit BuildWorkspaceEdit(DB* db,
WorkingFiles* working_files,
SymbolRef sym,
const std::string& new_text) {

View File

@ -15,22 +15,21 @@ MethodType kMethodType = "workspace/symbol";
// Lookup |symbol| in |db| and insert the value into |result|.
bool AddSymbol(
QueryDatabase* db,
DB* db,
WorkingFiles* working_files,
int i,
SymbolIdx sym,
bool use_detailed,
std::vector<std::tuple<lsSymbolInformation, bool, int>>* result) {
SymbolIdx symbol = db->symbols[i];
std::vector<std::tuple<lsSymbolInformation, int, SymbolIdx>>* result) {
std::optional<lsSymbolInformation> info =
GetSymbolInfo(db, working_files, symbol, true);
GetSymbolInfo(db, working_files, sym, true);
if (!info)
return false;
Use loc;
if (Maybe<Use> location = GetDefinitionExtent(db, symbol))
if (Maybe<Use> location = GetDefinitionExtent(db, sym))
loc = *location;
else {
auto decls = GetNonDefDeclarations(db, symbol);
auto decls = GetNonDefDeclarations(db, sym);
if (decls.empty())
return false;
loc = decls[0];
@ -40,7 +39,7 @@ bool AddSymbol(
if (!ls_location)
return false;
info->location = *ls_location;
result->emplace_back(*info, use_detailed, i);
result->emplace_back(*info, int(use_detailed), sym);
return true;
}
@ -72,7 +71,7 @@ struct Handler_WorkspaceSymbol : BaseMessageHandler<In_WorkspaceSymbol> {
std::string query = request->params.query;
// {symbol info, matching detailed_name or short_name, index}
std::vector<std::tuple<lsSymbolInformation, bool, int>> unsorted;
std::vector<std::tuple<lsSymbolInformation, int, SymbolIdx>> cands;
bool sensitive = g_config->workspaceSymbol.caseSensitivity;
// Find subsequence matches.
@ -82,47 +81,52 @@ struct Handler_WorkspaceSymbol : BaseMessageHandler<In_WorkspaceSymbol> {
if (!isspace(c))
query_without_space += c;
for (int i = 0; i < (int)db->symbols.size(); ++i) {
std::string_view detailed_name = db->GetSymbolName(i, true);
auto Add = [&](SymbolIdx sym) {
std::string_view detailed_name = db->GetSymbolName(sym, true);
int pos =
ReverseSubseqMatch(query_without_space, detailed_name, sensitive);
if (pos >= 0 &&
AddSymbol(db, working_files, i,
return pos >= 0 &&
AddSymbol(db, working_files, sym,
detailed_name.find(':', pos) != std::string::npos,
&unsorted) &&
unsorted.size() >= g_config->workspaceSymbol.maxNum)
break;
}
&cands) &&
cands.size() >= g_config->workspaceSymbol.maxNum;
};
for (auto& func : db->funcs)
if (Add({func.usr, SymbolKind::Func}))
goto done_add;
for (auto& type : db->types)
if (Add({type.usr, SymbolKind::Type}))
goto done_add;
for (auto& var : db->vars)
if (var.def.size() && !var.def[0].is_local() &&
Add({var.usr, SymbolKind::Var}))
goto done_add;
done_add:
if (g_config->workspaceSymbol.sort && query.size() <= FuzzyMatcher::kMaxPat) {
// Sort results with a fuzzy matching algorithm.
int longest = 0;
for (int i = 0; i < int(unsorted.size()); i++) {
for (auto& cand : cands)
longest = std::max(
longest,
int(db->GetSymbolName(std::get<2>(unsorted[i]), true).size()));
}
longest, int(db->GetSymbolName(std::get<2>(cand), true).size()));
FuzzyMatcher fuzzy(query, g_config->workspaceSymbol.caseSensitivity);
std::vector<std::pair<int, int>> permutation(unsorted.size());
for (int i = 0; i < int(unsorted.size()); i++) {
permutation[i] = {
fuzzy.Match(db->GetSymbolName(std::get<2>(unsorted[i]),
std::get<1>(unsorted[i]))),
i};
}
std::sort(permutation.begin(), permutation.end(),
std::greater<std::pair<int, int>>());
out.result.reserve(unsorted.size());
for (auto& cand : cands)
std::get<1>(cand) = fuzzy.Match(
db->GetSymbolName(std::get<2>(cand), std::get<1>(cand)));
std::sort(cands.begin(), cands.end(), [](const auto& l, const auto& r) {
return std::get<1>(l) > std::get<1>(r);
});
out.result.reserve(cands.size());
for (auto& cand: cands) {
// Discard awful candidates.
for (int i = 0; i < int(unsorted.size()) &&
permutation[i].first > FuzzyMatcher::kMinScore;
i++)
out.result.push_back(
std::move(std::get<0>(unsorted[permutation[i].second])));
if (std::get<1>(cand) <= FuzzyMatcher::kMinScore)
break;
out.result.push_back(std::get<0>(cand));
}
} else {
out.result.reserve(unsorted.size());
for (auto& entry : unsorted)
out.result.push_back(std::get<0>(entry));
out.result.reserve(cands.size());
for (auto& cand : cands)
out.result.push_back(std::get<0>(cand));
}
pipeline::WriteStdout(kMethodType, out);

View File

@ -299,7 +299,7 @@ void Indexer_Main(DiagnosticsEngine* diag_engine,
indexer_waiter->Wait(index_request);
}
void Main_OnIndexed(QueryDatabase* db,
void Main_OnIndexed(DB* db,
SemanticHighlightSymbolCache* semantic_cache,
WorkingFiles* working_files,
Index_OnIndexed* response) {
@ -431,7 +431,7 @@ void MainLoop() {
auto global_code_complete_cache = std::make_unique<CodeCompleteCache>();
auto non_global_code_complete_cache = std::make_unique<CodeCompleteCache>();
auto signature_cache = std::make_unique<CodeCompleteCache>();
QueryDatabase db;
DB db;
// Setup shared references.
for (MessageHandler* handler : *MessageHandler::message_handlers) {

View File

@ -14,7 +14,7 @@
#include <unordered_map>
#include <unordered_set>
// Used by |HANDLE_MERGEABLE| so only |range| is needed.
// Used by |REMOVE_ADD| so only |range| is needed.
MAKE_HASHABLE(Use, t.range, t.file_id);
namespace {
@ -178,6 +178,7 @@ IndexUpdate IndexUpdate::CreateDelta(IndexFile* previous,
r.files_def_update = BuildFileDefUpdate(std::move(*current));
r.funcs_hint = int(current->usr2func.size() - previous->usr2func.size());
for (auto& it : previous->usr2func) {
auto& func = it.second;
if (func.def.spell)
@ -195,6 +196,7 @@ IndexUpdate IndexUpdate::CreateDelta(IndexFile* previous,
r.funcs_derived[func.usr].second = std::move(func.derived);
}
r.types_hint = int(current->usr2type.size() - previous->usr2type.size());
for (auto& it : previous->usr2type) {
auto& type = it.second;
if (type.def.spell)
@ -214,6 +216,7 @@ IndexUpdate IndexUpdate::CreateDelta(IndexFile* previous,
r.types_instances[type.usr].second = std::move(type.instances);
};
r.vars_hint = int(current->usr2var.size() - previous->usr2var.size());
for (auto& it : previous->usr2var) {
auto& var = it.second;
if (var.def.spell)
@ -232,34 +235,14 @@ IndexUpdate IndexUpdate::CreateDelta(IndexFile* previous,
return r;
}
// ------------------------
// QUERYDB THREAD FUNCTIONS
// ------------------------
void QueryDatabase::RemoveUsrs(SymbolKind usr_kind,
const std::vector<Usr>& to_remove) {
switch (usr_kind) {
case SymbolKind::Type: {
for (Usr usr : to_remove) {
QueryType& type = usr2type[usr];
if (type.symbol_idx >= 0)
symbols[type.symbol_idx].kind = SymbolKind::Invalid;
type.def.clear();
}
break;
}
default:
break;
}
}
void QueryDatabase::RemoveUsrs(
SymbolKind kind,
void DB::RemoveUsrs(SymbolKind kind,
int file_id,
const std::vector<Usr>& to_remove) {
switch (kind) {
case SymbolKind::Func: {
for (auto usr : to_remove) {
for (Usr usr : to_remove) {
// FIXME
if (!HasFunc(usr)) continue;
QueryFunc& func = Func(usr);
auto it = llvm::find_if(func.def, [=](const QueryFunc::Def& def) {
return def.spell->file_id == file_id;
@ -269,8 +252,23 @@ void QueryDatabase::RemoveUsrs(
}
break;
}
case SymbolKind::Type: {
for (Usr usr : to_remove) {
// FIXME
if (!HasType(usr)) continue;
QueryType& type = Type(usr);
auto it = llvm::find_if(type.def, [=](const QueryType::Def& def) {
return def.spell->file_id == file_id;
});
if (it != type.def.end())
type.def.erase(it);
}
break;
}
case SymbolKind::Var: {
for (auto usr : to_remove) {
for (Usr usr : to_remove) {
// FIXME
if (!HasVar(usr)) continue;
QueryVar& var = Var(usr);
auto it = llvm::find_if(var.def, [=](const QueryVar::Def& def) {
return def.spell->file_id == file_id;
@ -281,26 +279,21 @@ void QueryDatabase::RemoveUsrs(
break;
}
default:
assert(false);
break;
}
}
void QueryDatabase::ApplyIndexUpdate(IndexUpdate* u) {
// This function runs on the querydb thread.
// Example types:
// storage_name => std::vector<std::optional<QueryType>>
// merge_update => QueryType::DerivedUpdate =>
// MergeableUpdate<QueryTypeId, QueryTypeId> def => QueryType
// def->def_var_name => std::vector<QueryTypeId>
#define HANDLE_MERGEABLE(update_var_name, def_var_name, storage_name) \
for (auto& it : u->update_var_name) { \
auto& entity = storage_name[it.first]; \
void DB::ApplyIndexUpdate(IndexUpdate* u) {
#define REMOVE_ADD(C, F) \
for (auto& it : u->C##s_##F) { \
auto R = C##_usr.try_emplace({it.first}, C##_usr.size()); \
if (R.second) \
C##s.emplace_back().usr = it.first; \
auto& entity = C##s[R.first->second]; \
AssignFileId(u->file_id, it.second.first); \
RemoveRange(entity.def_var_name, it.second.first); \
RemoveRange(entity.F, it.second.first); \
AssignFileId(u->file_id, it.second.second); \
AddRange(u->file_id, entity.def_var_name, it.second.second); \
AddRange(u->file_id, entity.F, it.second.second); \
}
if (u->files_removed)
@ -308,101 +301,108 @@ void QueryDatabase::ApplyIndexUpdate(IndexUpdate* u) {
std::nullopt;
u->file_id = u->files_def_update ? Update(std::move(*u->files_def_update)) : -1;
const double grow = 1.3;
size_t t;
if ((t = funcs.size() + u->funcs_hint) > funcs.capacity()) {
t = size_t(t * grow);
funcs.reserve(t);
func_usr.reserve(t);
}
RemoveUsrs(SymbolKind::Func, u->file_id, u->funcs_removed);
Update(u->file_id, std::move(u->funcs_def_update));
HANDLE_MERGEABLE(funcs_declarations, declarations, usr2func);
HANDLE_MERGEABLE(funcs_derived, derived, usr2func);
HANDLE_MERGEABLE(funcs_uses, uses, usr2func);
REMOVE_ADD(func, declarations);
REMOVE_ADD(func, derived);
REMOVE_ADD(func, uses);
RemoveUsrs(SymbolKind::Type, u->types_removed);
if ((t = types.size() + u->types_hint) > types.capacity()) {
t = size_t(t * grow);
types.reserve(t);
type_usr.reserve(t);
}
RemoveUsrs(SymbolKind::Type, u->file_id, u->types_removed);
Update(u->file_id, std::move(u->types_def_update));
HANDLE_MERGEABLE(types_declarations, declarations, usr2type);
HANDLE_MERGEABLE(types_derived, derived, usr2type);
HANDLE_MERGEABLE(types_instances, instances, usr2type);
HANDLE_MERGEABLE(types_uses, uses, usr2type);
REMOVE_ADD(type, declarations);
REMOVE_ADD(type, derived);
REMOVE_ADD(type, instances);
REMOVE_ADD(type, uses);
if ((t = vars.size() + u->vars_hint) > vars.capacity()) {
t = size_t(t * grow);
vars.reserve(t);
var_usr.reserve(t);
}
RemoveUsrs(SymbolKind::Var, u->file_id, u->vars_removed);
Update(u->file_id, std::move(u->vars_def_update));
HANDLE_MERGEABLE(vars_declarations, declarations, usr2var);
HANDLE_MERGEABLE(vars_uses, uses, usr2var);
REMOVE_ADD(var, declarations);
REMOVE_ADD(var, uses);
#undef HANDLE_MERGEABLE
#undef REMOVE_ADD
}
int QueryDatabase::Update(QueryFile::DefUpdate&& u) {
int DB::Update(QueryFile::DefUpdate&& u) {
int id = files.size();
auto it = name2file_id.try_emplace(LowerPathIfInsensitive(u.value.path), id);
if (it.second)
files.emplace_back().id = id;
QueryFile& existing = files[it.first->second];
existing.def = u.value;
UpdateSymbols(&existing.symbol_idx, SymbolKind::File, it.first->second);
return existing.id;
}
void QueryDatabase::Update(int file_id,
std::vector<std::pair<Usr, QueryFunc::Def>>&& us) {
void DB::Update(int file_id, std::vector<std::pair<Usr, QueryFunc::Def>>&& us) {
for (auto& u : us) {
auto& def = u.second;
assert(!def.detailed_name.empty());
AssignFileId(file_id, def.spell);
AssignFileId(file_id, def.extent);
AssignFileId(file_id, def.callees);
QueryFunc& existing = Func(u.first);
auto R = func_usr.try_emplace({u.first}, func_usr.size());
if (R.second)
funcs.emplace_back();
QueryFunc& existing = funcs[R.first->second];
existing.usr = u.first;
if (!TryReplaceDef(existing.def, std::move(def))) {
if (!TryReplaceDef(existing.def, std::move(def)))
existing.def.push_back(std::move(def));
UpdateSymbols(&existing.symbol_idx, SymbolKind::Func, u.first);
}
}
}
void QueryDatabase::Update(int file_id,
std::vector<std::pair<Usr, QueryType::Def>>&& us) {
void DB::Update(int file_id, std::vector<std::pair<Usr, QueryType::Def>>&& us) {
for (auto& u : us) {
auto& def = u.second;
assert(!def.detailed_name.empty());
AssignFileId(file_id, def.spell);
AssignFileId(file_id, def.extent);
QueryType& existing = Type(u.first);
auto R = type_usr.try_emplace({u.first}, type_usr.size());
if (R.second)
types.emplace_back();
QueryType& existing = types[R.first->second];
existing.usr = u.first;
if (!TryReplaceDef(existing.def, std::move(def))) {
if (!TryReplaceDef(existing.def, std::move(def)))
existing.def.push_back(std::move(def));
UpdateSymbols(&existing.symbol_idx, SymbolKind::Type, u.first);
}
}
}
void QueryDatabase::Update(int file_id,
std::vector<std::pair<Usr, QueryVar::Def>>&& us) {
void DB::Update(int file_id, std::vector<std::pair<Usr, QueryVar::Def>>&& us) {
for (auto& u : us) {
auto& def = u.second;
assert(!def.detailed_name.empty());
AssignFileId(file_id, def.spell);
AssignFileId(file_id, def.extent);
QueryVar& existing = Var(u.first);
auto R = var_usr.try_emplace({u.first}, var_usr.size());
if (R.second)
vars.emplace_back();
QueryVar& existing = vars[R.first->second];
existing.usr = u.first;
if (!TryReplaceDef(existing.def, std::move(def))) {
if (!TryReplaceDef(existing.def, std::move(def)))
existing.def.push_back(std::move(def));
if (!existing.def.front().is_local())
UpdateSymbols(&existing.symbol_idx, SymbolKind::Var, u.first);
}
}
}
void QueryDatabase::UpdateSymbols(int* symbol_idx,
SymbolKind kind,
Usr usr) {
if (*symbol_idx < 0) {
*symbol_idx = symbols.size();
symbols.push_back(SymbolIdx{usr, kind});
}
}
std::string_view QueryDatabase::GetSymbolName(int symbol_idx,
bool qualified) {
Usr usr = symbols[symbol_idx].usr;
switch (symbols[symbol_idx].kind) {
std::string_view DB::GetSymbolName(SymbolIdx sym, bool qualified) {
Usr usr = sym.usr;
switch (sym.kind) {
default:
break;
case SymbolKind::File:

View File

@ -3,6 +3,7 @@
#include "indexer.h"
#include "serializer.h"
#include <llvm/ADT/DenseMap.h>
#include <llvm/ADT/SmallVector.h>
#include <llvm/ADT/StringMap.h>
@ -10,7 +11,7 @@ struct QueryFile;
struct QueryType;
struct QueryFunc;
struct QueryVar;
struct QueryDatabase;
struct DB;
template <typename T>
struct WithFileContent {
@ -42,7 +43,6 @@ struct QueryFile {
int id = -1;
std::optional<Def> def;
int symbol_idx = -1;
};
template <typename Q, typename QDef>
@ -67,7 +67,6 @@ using UsrUpdate =
struct QueryFunc : QueryEntity<QueryFunc, FuncDef> {
Usr usr;
int symbol_idx = -1;
llvm::SmallVector<Def, 1> def;
std::vector<Use> declarations;
std::vector<Use> uses;
@ -76,7 +75,6 @@ struct QueryFunc : QueryEntity<QueryFunc, FuncDef> {
struct QueryType : QueryEntity<QueryType, TypeDef> {
Usr usr;
int symbol_idx = -1;
llvm::SmallVector<Def, 1> def;
std::vector<Use> declarations;
std::vector<Use> uses;
@ -86,7 +84,6 @@ struct QueryType : QueryEntity<QueryType, TypeDef> {
struct QueryVar : QueryEntity<QueryVar, VarDef> {
Usr usr;
int symbol_idx = -1;
llvm::SmallVector<Def, 1> def;
std::vector<Use> declarations;
std::vector<Use> uses;
@ -108,6 +105,7 @@ struct IndexUpdate {
std::optional<QueryFile::DefUpdate> files_def_update;
// Function updates.
int funcs_hint;
std::vector<Usr> funcs_removed;
std::vector<std::pair<Usr, QueryFunc::Def>> funcs_def_update;
UseUpdate funcs_declarations;
@ -115,6 +113,7 @@ struct IndexUpdate {
UsrUpdate funcs_derived;
// Type updates.
int types_hint;
std::vector<Usr> types_removed;
std::vector<std::pair<Usr, QueryType::Def>> types_def_update;
UseUpdate types_declarations;
@ -123,23 +122,42 @@ struct IndexUpdate {
UsrUpdate types_instances;
// Variable updates.
int vars_hint;
std::vector<Usr> vars_removed;
std::vector<std::pair<Usr, QueryVar::Def>> vars_def_update;
UseUpdate vars_declarations;
UseUpdate vars_uses;
};
template <typename Q>
struct EntityToIndex {
using argument_type = const Q&;
llvm::DenseMap<Usr, unsigned> m;
unsigned operator()(const Q& entity) const {
return m[entity.usr];
}
};
struct WrappedUsr {
Usr usr;
};
template <>
struct llvm::DenseMapInfo<WrappedUsr> {
static inline WrappedUsr getEmptyKey() { return {0}; }
static inline WrappedUsr getTombstoneKey() { return {~0ULL}; }
static unsigned getHashValue(WrappedUsr w) { return w.usr; }
static bool isEqual(WrappedUsr l, WrappedUsr r) { return l.usr == r.usr; }
};
// 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;
struct DB {
std::vector<QueryFile> files;
llvm::StringMap<int> name2file_id;
std::unordered_map<Usr, QueryFunc> usr2func;
std::unordered_map<Usr, QueryType> usr2type;
std::unordered_map<Usr, QueryVar> usr2var;
llvm::DenseMap<WrappedUsr, int> func_usr, type_usr, var_usr;
std::vector<QueryFunc> funcs;
std::vector<QueryType> types;
std::vector<QueryVar> vars;
// Marks the given Usrs as invalid.
void RemoveUsrs(SymbolKind usr_kind, const std::vector<Usr>& to_remove);
@ -150,15 +168,18 @@ struct QueryDatabase {
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);
std::string_view GetSymbolName(int symbol_idx, bool qualified);
std::string_view GetSymbolName(SymbolIdx sym, bool qualified);
bool HasFunc(Usr usr) const { return func_usr.count({usr}); }
bool HasType(Usr usr) const { return type_usr.count({usr}); }
bool HasVar(Usr usr) const { return var_usr.count({usr}); }
QueryFunc& Func(Usr usr) { return funcs[func_usr[{usr}]]; }
QueryType& Type(Usr usr) { return types[type_usr[{usr}]]; }
QueryVar& Var(Usr usr) { return vars[var_usr[{usr}]]; }
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]; }
QueryFunc& Func(Usr usr) { return usr2func[usr]; }
QueryType& Type(Usr usr) { return usr2type[usr]; }
QueryVar& Var(Usr usr) { return usr2var[usr]; }
QueryFunc& GetFunc(SymbolIdx ref) { return Func(ref.usr); }
QueryType& GetType(SymbolIdx ref) { return Type(ref.usr); }
QueryVar& GetVar(SymbolIdx ref) { return Var(ref.usr); }
};

View File

@ -14,15 +14,36 @@ int ComputeRangeSize(const Range& range) {
return range.end.column - range.start.column;
}
template <typename Q>
std::vector<Use> GetDeclarations(llvm::DenseMap<WrappedUsr, int>& entity_usr,
std::vector<Q>& entities,
const std::vector<Usr>& usrs) {
std::vector<Use> ret;
ret.reserve(usrs.size());
for (Usr usr : usrs) {
Q& entity = entities[entity_usr[{usr}]];
bool has_def = false;
for (auto& def : entity.def)
if (def.spell) {
ret.push_back(*def.spell);
has_def = true;
break;
}
if (!has_def && entity.declarations.size())
ret.push_back(entity.declarations[0]);
}
return ret;
}
} // namespace
Maybe<Use> GetDefinitionSpell(QueryDatabase* db, SymbolIdx sym) {
Maybe<Use> GetDefinitionSpell(DB* db, SymbolIdx sym) {
Maybe<Use> ret;
EachEntityDef(db, sym, [&](const auto& def) { return !(ret = def.spell); });
return ret;
}
Maybe<Use> GetDefinitionExtent(QueryDatabase* db, SymbolIdx sym) {
Maybe<Use> GetDefinitionExtent(DB* db, SymbolIdx sym) {
// Used to jump to file.
if (sym.kind == SymbolKind::File)
return Use{{Range{{0, 0}, {0, 0}}, sym.usr, sym.kind, Role::None},
@ -32,7 +53,17 @@ Maybe<Use> GetDefinitionExtent(QueryDatabase* db, SymbolIdx sym) {
return ret;
}
std::vector<Use> GetNonDefDeclarations(QueryDatabase* db, SymbolIdx sym) {
std::vector<Use> GetFuncDeclarations(DB* db, const std::vector<Usr>& usrs) {
return GetDeclarations(db->func_usr, db->funcs, usrs);
}
std::vector<Use> GetTypeDeclarations(DB* db, const std::vector<Usr>& usrs) {
return GetDeclarations(db->type_usr, db->types, usrs);
}
std::vector<Use> GetVarDeclarations(DB* db, const std::vector<Usr>& usrs) {
return GetDeclarations(db->var_usr, db->vars, usrs);
}
std::vector<Use> GetNonDefDeclarations(DB* db, SymbolIdx sym) {
switch (sym.kind) {
case SymbolKind::Func:
return db->GetFunc(sym).declarations;
@ -45,7 +76,7 @@ std::vector<Use> GetNonDefDeclarations(QueryDatabase* db, SymbolIdx sym) {
}
}
std::vector<Use> GetUsesForAllBases(QueryDatabase* db, QueryFunc& root) {
std::vector<Use> GetUsesForAllBases(DB* db, QueryFunc& root) {
std::vector<Use> ret;
std::vector<QueryFunc*> stack{&root};
std::unordered_set<Usr> seen;
@ -54,7 +85,7 @@ std::vector<Use> GetUsesForAllBases(QueryDatabase* db, QueryFunc& root) {
QueryFunc& func = *stack.back();
stack.pop_back();
if (auto* def = func.AnyDef()) {
EachDefinedEntity(db->usr2func, def->bases, [&](QueryFunc& func1) {
EachDefinedFunc(db, def->bases, [&](QueryFunc& func1) {
if (!seen.count(func1.usr)) {
seen.insert(func1.usr);
stack.push_back(&func1);
@ -67,7 +98,7 @@ std::vector<Use> GetUsesForAllBases(QueryDatabase* db, QueryFunc& root) {
return ret;
}
std::vector<Use> GetUsesForAllDerived(QueryDatabase* db, QueryFunc& root) {
std::vector<Use> GetUsesForAllDerived(DB* db, QueryFunc& root) {
std::vector<Use> ret;
std::vector<QueryFunc*> stack{&root};
std::unordered_set<Usr> seen;
@ -75,7 +106,7 @@ std::vector<Use> GetUsesForAllDerived(QueryDatabase* db, QueryFunc& root) {
while (!stack.empty()) {
QueryFunc& func = *stack.back();
stack.pop_back();
EachDefinedEntity(db->usr2func, func.derived, [&](QueryFunc& func1) {
EachDefinedFunc(db, func.derived, [&](QueryFunc& func1) {
if (!seen.count(func1.usr)) {
seen.insert(func1.usr);
stack.push_back(&func1);
@ -99,7 +130,8 @@ std::optional<lsPosition> GetLsPosition(WorkingFile* working_file,
return std::nullopt;
}
std::optional<lsRange> GetLsRange(WorkingFile* working_file, const Range& location) {
std::optional<lsRange> GetLsRange(WorkingFile* working_file,
const Range& location) {
if (!working_file) {
return lsRange{lsPosition{location.start.line, location.start.column},
lsPosition{location.end.line, location.end.column}};
@ -108,8 +140,8 @@ std::optional<lsRange> GetLsRange(WorkingFile* working_file, const Range& locati
int start_column = location.start.column, end_column = location.end.column;
std::optional<int> start = working_file->GetBufferPosFromIndexPos(
location.start.line, &start_column, false);
std::optional<int> end = working_file->GetBufferPosFromIndexPos(location.end.line,
&end_column, true);
std::optional<int> end = working_file->GetBufferPosFromIndexPos(
location.end.line, &end_column, true);
if (!start || !end)
return std::nullopt;
@ -128,9 +160,7 @@ std::optional<lsRange> GetLsRange(WorkingFile* working_file, const Range& locati
lsPosition{*end, end_column}};
}
lsDocumentUri GetLsDocumentUri(QueryDatabase* db,
int file_id,
std::string* path) {
lsDocumentUri GetLsDocumentUri(DB* db, int file_id, std::string* path) {
QueryFile& file = db->files[file_id];
if (file.def) {
*path = file.def->path;
@ -141,7 +171,7 @@ lsDocumentUri GetLsDocumentUri(QueryDatabase* db,
}
}
lsDocumentUri GetLsDocumentUri(QueryDatabase* db, int file_id) {
lsDocumentUri GetLsDocumentUri(DB* db, int file_id) {
QueryFile& file = db->files[file_id];
if (file.def) {
return lsDocumentUri::FromPath(file.def->path);
@ -150,7 +180,7 @@ lsDocumentUri GetLsDocumentUri(QueryDatabase* db, int file_id) {
}
}
std::optional<lsLocation> GetLsLocation(QueryDatabase* db,
std::optional<lsLocation> GetLsLocation(DB* db,
WorkingFiles* working_files,
Use use) {
std::string path;
@ -162,7 +192,7 @@ std::optional<lsLocation> GetLsLocation(QueryDatabase* db,
return lsLocation{uri, *range};
}
std::optional<lsLocationEx> GetLsLocationEx(QueryDatabase* db,
std::optional<lsLocationEx> GetLsLocationEx(DB* db,
WorkingFiles* working_files,
Use use,
bool container) {
@ -181,7 +211,7 @@ std::optional<lsLocationEx> GetLsLocationEx(QueryDatabase* db,
return ret;
}
std::vector<lsLocationEx> GetLsLocationExs(QueryDatabase* db,
std::vector<lsLocationEx> GetLsLocationExs(DB* db,
WorkingFiles* working_files,
const std::vector<Use>& uses) {
std::vector<lsLocationEx> ret;
@ -196,7 +226,7 @@ std::vector<lsLocationEx> GetLsLocationExs(QueryDatabase* db,
return ret;
}
lsSymbolKind GetSymbolKind(QueryDatabase* db, SymbolIdx sym) {
lsSymbolKind GetSymbolKind(DB* db, SymbolIdx sym) {
lsSymbolKind ret;
if (sym.kind == SymbolKind::File)
ret = lsSymbolKind::File;
@ -213,7 +243,7 @@ lsSymbolKind GetSymbolKind(QueryDatabase* db, SymbolIdx sym) {
}
// Returns a symbol. The symbol will have *NOT* have a location assigned.
std::optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db,
std::optional<lsSymbolInformation> GetSymbolInfo(DB* db,
WorkingFiles* working_files,
SymbolIdx sym,
bool detailed_name) {

View File

@ -5,56 +5,39 @@
#include <optional>
Maybe<Use> GetDefinitionSpell(QueryDatabase* db, SymbolIdx sym);
Maybe<Use> GetDefinitionExtent(QueryDatabase* db, SymbolIdx sym);
Maybe<Use> GetDefinitionSpell(DB* db, SymbolIdx sym);
Maybe<Use> GetDefinitionExtent(DB* db, SymbolIdx sym);
// Get defining declaration (if exists) or an arbitrary declaration (otherwise)
// for each id.
template <typename Q>
std::vector<Use> GetDeclarations(std::unordered_map<Usr, Q>& usr2entity,
const std::vector<Usr>& usrs) {
std::vector<Use> ret;
ret.reserve(usrs.size());
for (Usr usr : usrs) {
Q& entity = usr2entity[usr];
bool has_def = false;
for (auto& def : entity.def)
if (def.spell) {
ret.push_back(*def.spell);
has_def = true;
break;
}
if (!has_def && entity.declarations.size())
ret.push_back(entity.declarations[0]);
}
return ret;
}
std::vector<Use> GetFuncDeclarations(DB*, const std::vector<Usr>&);
std::vector<Use> GetTypeDeclarations(DB*, const std::vector<Usr>&);
std::vector<Use> GetVarDeclarations(DB*, const std::vector<Usr>&);
// Get non-defining declarations.
std::vector<Use> GetNonDefDeclarations(QueryDatabase* db, SymbolIdx sym);
std::vector<Use> GetNonDefDeclarations(DB* db, SymbolIdx sym);
std::vector<Use> GetUsesForAllBases(QueryDatabase* db, QueryFunc& root);
std::vector<Use> GetUsesForAllDerived(QueryDatabase* db, QueryFunc& root);
std::vector<Use> GetUsesForAllBases(DB* db, QueryFunc& root);
std::vector<Use> GetUsesForAllDerived(DB* db, QueryFunc& root);
std::optional<lsPosition> GetLsPosition(WorkingFile* working_file,
const Position& position);
std::optional<lsRange> GetLsRange(WorkingFile* working_file, const Range& location);
lsDocumentUri GetLsDocumentUri(QueryDatabase* db,
int file_id,
std::string* path);
lsDocumentUri GetLsDocumentUri(QueryDatabase* db, int file_id);
std::optional<lsRange> GetLsRange(WorkingFile* working_file,
const Range& location);
lsDocumentUri GetLsDocumentUri(DB* db, int file_id, std::string* path);
lsDocumentUri GetLsDocumentUri(DB* db, int file_id);
std::optional<lsLocation> GetLsLocation(QueryDatabase* db,
std::optional<lsLocation> GetLsLocation(DB* db,
WorkingFiles* working_files,
Use use);
std::optional<lsLocationEx> GetLsLocationEx(QueryDatabase* db,
std::optional<lsLocationEx> GetLsLocationEx(DB* db,
WorkingFiles* working_files,
Use use,
bool container);
std::vector<lsLocationEx> GetLsLocationExs(QueryDatabase* db,
std::vector<lsLocationEx> GetLsLocationExs(DB* db,
WorkingFiles* working_files,
const std::vector<Use>& refs);
// Returns a symbol. The symbol will have *NOT* have a location assigned.
std::optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db,
std::optional<lsSymbolInformation> GetSymbolInfo(DB* db,
WorkingFiles* working_files,
SymbolIdx sym,
bool detailed_name);
@ -64,7 +47,7 @@ std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file,
lsPosition& ls_pos);
template <typename Fn>
void WithEntity(QueryDatabase* db, SymbolIdx sym, Fn&& fn) {
void WithEntity(DB* db, SymbolIdx sym, Fn&& fn) {
switch (sym.kind) {
case SymbolKind::Invalid:
case SymbolKind::File:
@ -82,7 +65,7 @@ void WithEntity(QueryDatabase* db, SymbolIdx sym, Fn&& fn) {
}
template <typename Fn>
void EachEntityDef(QueryDatabase* db, SymbolIdx sym, Fn&& fn) {
void EachEntityDef(DB* db, SymbolIdx sym, Fn&& fn) {
WithEntity(db, sym, [&](const auto& entity) {
for (auto& def : entity.def)
if (!fn(def))
@ -91,10 +74,7 @@ void EachEntityDef(QueryDatabase* db, SymbolIdx sym, Fn&& fn) {
}
template <typename Fn>
void EachOccurrence(QueryDatabase* db,
SymbolIdx sym,
bool include_decl,
Fn&& fn) {
void EachOccurrence(DB* db, SymbolIdx sym, bool include_decl, Fn&& fn) {
WithEntity(db, sym, [&](const auto& entity) {
for (Use use : entity.uses)
fn(use);
@ -108,14 +88,31 @@ void EachOccurrence(QueryDatabase* db,
});
}
lsSymbolKind GetSymbolKind(QueryDatabase* db, SymbolIdx sym);
lsSymbolKind GetSymbolKind(DB* db, SymbolIdx sym);
template <typename Q, typename Fn>
void EachDefinedEntity(std::unordered_map<Usr, Q>& collection,
const std::vector<Usr>& usrs,
Fn&& fn) {
template <typename Fn>
void EachDefinedFunc(DB* db, const std::vector<Usr>& usrs, Fn&& fn) {
for (Usr usr : usrs) {
Q& obj = collection[usr];
auto& obj = db->Func(usr);
if (!obj.def.empty())
fn(obj);
}
}
template <typename Fn>
void EachDefinedType(DB* db, const std::vector<Usr>& usrs, Fn&& fn) {
for (Usr usr : usrs) {
auto& obj = db->Type(usr);
if (!obj.def.empty())
fn(obj);
}
}
template <typename Fn>
void EachDefinedVar(DB* db, const std::vector<Usr>& usrs, Fn&& fn) {
for (Usr usr : usrs) {
auto& obj = db->Var(usr);
if (!obj.def.empty())
fn(obj);
}