This repository was archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathquery_utils.h
161 lines (147 loc) · 5.35 KB
/
query_utils.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
#pragma once
#include "query.h"
#include "working_files.h"
#include <optional.h>
optional<QueryId::LexicalRef> GetDefinitionSpell(QueryDatabase* db,
SymbolIdx sym);
optional<QueryId::LexicalRef> GetDefinitionExtent(QueryDatabase* db,
SymbolIdx sym);
optional<QueryId::File> GetDeclarationFileForSymbol(QueryDatabase* db,
SymbolIdx sym);
// Get defining declaration (if exists) or an arbitrary declaration (otherwise)
// for each id.
std::vector<QueryId::LexicalRef> GetDeclarations(
QueryDatabase* db,
const std::vector<QueryId::Func>& ids);
std::vector<QueryId::LexicalRef> GetDeclarations(
QueryDatabase* db,
const std::vector<QueryId::Type>& ids);
std::vector<QueryId::LexicalRef> GetDeclarations(
QueryDatabase* db,
const std::vector<QueryId::Var>& ids);
// Get non-defining declarations.
std::vector<QueryId::LexicalRef> GetNonDefDeclarations(QueryDatabase* db,
SymbolIdx sym);
std::vector<QueryId::LexicalRef> GetRefsForAllBases(QueryDatabase* db,
QueryFunc& root);
std::vector<QueryId::LexicalRef> GetRefsForAllDerived(QueryDatabase* db,
QueryFunc& root);
optional<lsPosition> GetLsPosition(WorkingFile* working_file,
const Position& position);
optional<lsRange> GetLsRange(WorkingFile* working_file, const Range& location);
lsDocumentUri GetLsDocumentUri(QueryDatabase* db,
QueryId::File file_id,
AbsolutePath* path);
lsDocumentUri GetLsDocumentUri(QueryDatabase* db, QueryId::File file_id);
optional<lsLocation> GetLsLocation(QueryDatabase* db,
WorkingFiles* working_files,
QueryId::LexicalRef ref);
std::vector<lsLocation> GetLsLocations(
QueryDatabase* db,
WorkingFiles* working_files,
const std::vector<QueryId::LexicalRef>& refs);
// Returns a symbol. The symbol will have *NOT* have a location assigned.
optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db,
WorkingFiles* working_files,
SymbolIdx sym,
bool use_short_name);
std::vector<QueryId::SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file,
QueryFile* file,
lsPosition position);
// Calls fn with a QueryFunc, QueryType, or QueryVar instance.
template <typename Fn>
void WithEntity(QueryDatabase* db, SymbolIdx sym, Fn&& fn) {
switch (sym.kind) {
case SymbolKind::Invalid:
case SymbolKind::File:
break;
case SymbolKind::Func:
fn(db->GetFunc(sym));
break;
case SymbolKind::Type:
fn(db->GetType(sym));
break;
case SymbolKind::Var:
fn(db->GetVar(sym));
break;
}
}
template <typename Fn>
void EachEntityDef(QueryDatabase* db, SymbolIdx sym, Fn&& fn) {
WithEntity(db, sym, [&](const auto& entity) {
for (auto& def : entity.def)
if (!fn(def))
break;
});
}
template <typename Fn>
void EachOccurrence(QueryDatabase* db,
SymbolIdx sym,
bool include_decl,
Fn&& fn) {
WithEntity(db, sym, [&](const auto& entity) {
for (QueryId::LexicalRef ref : entity.uses)
fn(ref);
if (include_decl) {
for (auto& def : entity.def)
if (def.spell)
fn(*def.spell);
for (QueryId::LexicalRef ref : entity.declarations)
fn(ref);
}
});
}
lsSymbolKind GetSymbolKind(QueryDatabase* db, SymbolIdx sym);
template <typename Fn>
void EachOccurrenceWithParent(QueryDatabase* db,
SymbolIdx sym,
bool include_decl,
Fn&& fn) {
WithEntity(db, sym, [&](const auto& entity) {
lsSymbolKind parent_kind = lsSymbolKind::Unknown;
for (auto& def : entity.def)
if (def.spell) {
parent_kind = GetSymbolKind(db, sym);
break;
}
for (QueryId::LexicalRef ref : entity.uses)
fn(ref, parent_kind);
if (include_decl) {
for (auto& def : entity.def)
if (def.spell)
fn(*def.spell, parent_kind);
for (QueryId::LexicalRef ref : entity.declarations)
fn(ref, parent_kind);
}
});
}
template <typename Fn>
void EachDefinedType(QueryDatabase* db,
const std::vector<QueryId::Type>& ids,
Fn&& fn) {
for (QueryId::Type id : ids) {
QueryType& obj = db->GetType(id);
if (!obj.def.empty())
fn(obj);
}
}
template <typename Fn>
void EachDefinedFunc(QueryDatabase* db,
const std::vector<QueryId::Func>& ids,
Fn&& fn) {
for (QueryId::Func id : ids) {
QueryFunc& obj = db->GetFunc(id);
if (!obj.def.empty())
fn(obj);
}
}
template <typename Fn>
void EachDefinedVar(QueryDatabase* db,
const std::vector<QueryId::Var>& ids,
Fn&& fn) {
for (QueryId::Var id : ids) {
QueryVar& obj = db->GetVar(id);
if (!obj.def.empty())
fn(obj);
}
}