@codua/codeoutline-win32-x64 0.2.6 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +205 -3
- package/README.zh-CN.md +40 -2
- package/bin/xnet.exe +0 -0
- package/build-info.json +1 -1
- package/package.json +1 -1
- package/scripts/codeoutline/affected.lua +145 -0
- package/scripts/codeoutline/automatic_events.lua +202 -0
- package/scripts/codeoutline/cache_repairs.lua +47 -0
- package/scripts/codeoutline/callers.lua +56 -16
- package/scripts/codeoutline/cargo_toml.lua +125 -0
- package/scripts/codeoutline/command.lua +26 -4
- package/scripts/codeoutline/common.lua +48 -7
- package/scripts/codeoutline/completion.lua +126 -22
- package/scripts/codeoutline/coverage.lua +63 -0
- package/scripts/codeoutline/cpp_bindings.lua +46 -11
- package/scripts/codeoutline/cpp_fragments.lua +143 -0
- package/scripts/codeoutline/cpp_implementations.lua +340 -0
- package/scripts/codeoutline/cpp_includes.lua +156 -0
- package/scripts/codeoutline/cpp_macros.lua +279 -0
- package/scripts/codeoutline/cpp_receivers.lua +342 -0
- package/scripts/codeoutline/cpp_types.lua +275 -28
- package/scripts/codeoutline/definition_candidates.lua +180 -0
- package/scripts/codeoutline/dispatch_aliases.lua +10 -5
- package/scripts/codeoutline/documents.lua +22 -9
- package/scripts/codeoutline/explore.lua +243 -122
- package/scripts/codeoutline/external_headers.lua +171 -0
- package/scripts/codeoutline/function_values.lua +69 -0
- package/scripts/codeoutline/go_methods.lua +931 -0
- package/scripts/codeoutline/go_modules.lua +329 -0
- package/scripts/codeoutline/graph.lua +322 -67
- package/scripts/codeoutline/implementations.lua +403 -0
- package/scripts/codeoutline/index.lua +161 -28
- package/scripts/codeoutline/index_worker.lua +11 -4
- package/scripts/codeoutline/indirect.lua +4 -2
- package/scripts/codeoutline/js_evidence.lua +2 -1
- package/scripts/codeoutline/js_resolution.lua +40 -12
- package/scripts/codeoutline/js_workspace.lua +325 -0
- package/scripts/codeoutline/lang/c.lua +15 -2
- package/scripts/codeoutline/lang/cpp.lua +7 -4
- package/scripts/codeoutline/lang/csharp.lua +1 -1
- package/scripts/codeoutline/lang/js.lua +56 -20
- package/scripts/codeoutline/lang/rust.lua +9 -1
- package/scripts/codeoutline/language_detect.lua +100 -0
- package/scripts/codeoutline/lsp.lua +173 -23
- package/scripts/codeoutline/lua_bindings.lua +151 -47
- package/scripts/codeoutline/lua_callback_loops.lua +210 -0
- package/scripts/codeoutline/main.lua +32 -7
- package/scripts/codeoutline/mcp.lua +52 -9
- package/scripts/codeoutline/navigation.lua +284 -64
- package/scripts/codeoutline/on_demand.lua +93 -0
- package/scripts/codeoutline/ordinary_bindings.lua +210 -34
- package/scripts/codeoutline/parse.lua +18 -2
- package/scripts/codeoutline/parse_pool.lua +1 -1
- package/scripts/codeoutline/parse_worker.lua +1 -1
- package/scripts/codeoutline/python_bindings.lua +2 -2
- package/scripts/codeoutline/query_filters.lua +65 -0
- package/scripts/codeoutline/query_report.lua +56 -19
- package/scripts/codeoutline/resolution.lua +23 -0
- package/scripts/codeoutline/rust_packages.lua +203 -0
- package/scripts/codeoutline/rust_resolution.lua +301 -0
- package/scripts/codeoutline/search_ranking.lua +70 -0
- package/scripts/codeoutline/service.lua +47 -7
- package/scripts/codeoutline/shared_events.lua +369 -0
- package/scripts/codeoutline/static_dispatch.lua +46 -11
- package/scripts/codeoutline/text.lua +3 -2
- package/scripts/codeoutline/ts_implementations.lua +81 -0
- package/scripts/codeoutline/type_bases.lua +61 -0
- package/scripts/codeoutline/type_hierarchy.lua +324 -0
- package/scripts/codeoutline/type_references.lua +290 -0
- package/scripts/codeoutline/type_uses.lua +260 -0
- package/scripts/codeoutline/update_sequence.lua +1 -1
- package/scripts/codeoutline/version.lua +1 -1
|
@@ -0,0 +1,931 @@
|
|
|
1
|
+
-- Query-owned Go signatures, method sets and declared receiver bindings.
|
|
2
|
+
local control = require('codeoutline.control')
|
|
3
|
+
local M = { MAX_FILES = 16, MAX_BYTES = 8388608, MAX_TYPES = 512, MAX_NODES = 8192,
|
|
4
|
+
MAX_DEPTH = 16, MAX_RESULTS = 64, MAX_CANDIDATES = 512, MAX_TIME_MS = 500 }
|
|
5
|
+
local kinds = { struct = true, interface = true, typedef = true }
|
|
6
|
+
|
|
7
|
+
local function statement_end(T, a, b)
|
|
8
|
+
local i = a
|
|
9
|
+
while i <= b do
|
|
10
|
+
if T.m[i] and T.m[i] > i then i = T.m[i] end
|
|
11
|
+
if i >= b or T:text(i + 1) == ';' or T.l[i + 1] > T.el[i] then return i end
|
|
12
|
+
i = i + 1
|
|
13
|
+
end
|
|
14
|
+
return b
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
local type_expr, parameters
|
|
18
|
+
type_expr = function(T, a, b, depth)
|
|
19
|
+
depth = (depth or 0) + 1
|
|
20
|
+
if a > b or depth > 16 then return nil end
|
|
21
|
+
local first = T:text(a)
|
|
22
|
+
local function wrap(kind, start, extra)
|
|
23
|
+
local child = type_expr(T, start, b, depth)
|
|
24
|
+
return child and { kind = kind, child = child, extra = extra } or nil
|
|
25
|
+
end
|
|
26
|
+
if first == '*' then return wrap('pointer', a + 1) end
|
|
27
|
+
if first == '...' then return wrap('variadic', a + 1) end
|
|
28
|
+
if first == '(' and T.m[a] == b then return type_expr(T, a + 1, b - 1, depth) end
|
|
29
|
+
if first == '[' and T.m[a] then
|
|
30
|
+
local close = T.m[a]
|
|
31
|
+
if close == a + 1 then return wrap('slice', close + 1) end
|
|
32
|
+
local size = close == a + 2 and tonumber(T:text(a + 1))
|
|
33
|
+
if size and size >= 0 and size <= 9007199254740991 and size == math.floor(size) then return wrap('array', close + 1, size) end
|
|
34
|
+
return nil
|
|
35
|
+
end
|
|
36
|
+
if first == 'map' and T:text(a + 1) == '[' and T.m[a + 1] then
|
|
37
|
+
local close = T.m[a + 1]
|
|
38
|
+
local key, value = type_expr(T, a + 2, close - 1, depth), type_expr(T, close + 1, b, depth)
|
|
39
|
+
return key and value and { kind = 'map', key = key, child = value } or nil
|
|
40
|
+
end
|
|
41
|
+
if first == 'chan' then return wrap('channel', T:text(a + 1) == '<-' and a + 2 or a + 1,
|
|
42
|
+
T:text(a + 1) == '<-' and 'send' or 'both') end
|
|
43
|
+
if first == '<-' and T:text(a + 1) == 'chan' then return wrap('channel', a + 2, 'receive') end
|
|
44
|
+
if first == 'interface' and T:text(a + 1) == '{' and T.m[a + 1] == a + 2 and b == a + 2 then return { kind = 'any' } end
|
|
45
|
+
if first == 'func' and T:text(a + 1) == '(' and T.m[a + 1] then
|
|
46
|
+
local close = T.m[a + 1]
|
|
47
|
+
local params = parameters(T, a + 2, close - 1)
|
|
48
|
+
local results = {}
|
|
49
|
+
if close < b then
|
|
50
|
+
if T:text(close + 1) == '(' and T.m[close + 1] == b then results = parameters(T, close + 2, b - 1)
|
|
51
|
+
else local ty = type_expr(T, close + 1, b, depth); results = ty and { ty } or nil end
|
|
52
|
+
end
|
|
53
|
+
return params and results and { kind = 'function', params = params, results = results } or nil
|
|
54
|
+
end
|
|
55
|
+
if T.k[a] == 'id' and a == b then return { kind = 'name', name = first, at = T.s[a] - 1 } end
|
|
56
|
+
if T.k[a] == 'id' and T:text(a + 1) == '.' and T.k[a + 2] == 'id' and b == a + 2 then
|
|
57
|
+
return { kind = 'name', name = first .. '.' .. T:text(a + 2), at = T.s[a] - 1 }
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
parameters = function(T, a, b)
|
|
62
|
+
local spans, start, i = {}, a, a
|
|
63
|
+
while i <= b + 1 do
|
|
64
|
+
if i == b + 1 or T:text(i) == ',' then
|
|
65
|
+
if start < i then spans[#spans + 1] = { start, i - 1 } end
|
|
66
|
+
start = i + 1
|
|
67
|
+
elseif T.m[i] and T.m[i] > i then i = T.m[i] end
|
|
68
|
+
i = i + 1
|
|
69
|
+
end
|
|
70
|
+
local types, names, pending, named = {}, {}, {}, false
|
|
71
|
+
local function add(ty, name)
|
|
72
|
+
types[#types + 1], names[#names + 1] = ty, name or false
|
|
73
|
+
end
|
|
74
|
+
for _, span in ipairs(spans) do
|
|
75
|
+
local x, y = span[1], span[2]
|
|
76
|
+
local ty = T.k[x] == 'id' and type_expr(T, x + 1, y)
|
|
77
|
+
local named_form = T.k[x] == 'id' and x < y and T:text(x + 1) ~= '.'
|
|
78
|
+
and not (T:text(x + 1) == '[' and T.m[x + 1] == y)
|
|
79
|
+
if ty or named_form then
|
|
80
|
+
named = true
|
|
81
|
+
for _, token in ipairs(pending) do add(ty or false, { name = T:text(token), token = token }) end
|
|
82
|
+
pending = {}; add(ty or false, { name = T:text(x), token = x })
|
|
83
|
+
elseif x == y and T.k[x] == 'id' then pending[#pending + 1] = x
|
|
84
|
+
else
|
|
85
|
+
if named then return nil end
|
|
86
|
+
for _, token in ipairs(pending) do add(type_expr(T, token, token)) end
|
|
87
|
+
pending = {}; ty = type_expr(T, x, y)
|
|
88
|
+
add(ty or false)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
if named and #pending > 0 then return nil end
|
|
92
|
+
for _, token in ipairs(pending) do add(type_expr(T, token, token)) end
|
|
93
|
+
for index, ty in ipairs(types) do if ty and ty.kind == 'variadic' and index ~= #types then return nil end end
|
|
94
|
+
return types, names
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
local function signature(T, open, last)
|
|
98
|
+
local close = T.m[open]
|
|
99
|
+
if not close then return nil end
|
|
100
|
+
local params, names = parameters(T, open + 1, close - 1)
|
|
101
|
+
if not params then return nil end
|
|
102
|
+
local after, results, result_names = close + 1, {}, {}
|
|
103
|
+
if T:text(after) == '(' and T.m[after] and T.m[after] <= last then
|
|
104
|
+
results, result_names = parameters(T, after + 1, T.m[after] - 1)
|
|
105
|
+
after = T.m[after] + 1
|
|
106
|
+
elseif after <= last and T:text(after) ~= '{' and T:text(after) ~= ';' then
|
|
107
|
+
local ending = after
|
|
108
|
+
while ending < last and T:text(ending + 1) ~= '{' and T:text(ending + 1) ~= ';'
|
|
109
|
+
and T.l[ending + 1] == T.el[ending] do
|
|
110
|
+
if T.m[ending] and T.m[ending] > ending then ending = T.m[ending] end
|
|
111
|
+
if ending >= last then break end
|
|
112
|
+
ending = ending + 1
|
|
113
|
+
end
|
|
114
|
+
local ty = type_expr(T, after, ending)
|
|
115
|
+
if not ty then return nil end
|
|
116
|
+
results, after = { ty }, ending + 1
|
|
117
|
+
end
|
|
118
|
+
if not results then return nil end
|
|
119
|
+
return { params = params, results = results }, names, result_names, after
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
local function function_body(T, close, last)
|
|
123
|
+
local at = close + 1
|
|
124
|
+
while at <= last do
|
|
125
|
+
local word = T:text(at)
|
|
126
|
+
if word == '{' then
|
|
127
|
+
if T:text(at - 1) ~= 'struct' and T:text(at - 1) ~= 'interface' then return at end
|
|
128
|
+
at = (T.m[at] or at) + 1
|
|
129
|
+
elseif (word == '(' or word == '[') and T.m[at] then at = T.m[at] + 1
|
|
130
|
+
elseif word == ';' or T.l[at] > T.el[at - 1] then return nil
|
|
131
|
+
else at = at + 1 end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
-- Only identifiers, direct calls and parenthesized versions have a proven
|
|
136
|
+
-- receiver path here. Fields, indexing, assertions and generic calls stay unknown.
|
|
137
|
+
local function expression(T, last, depth)
|
|
138
|
+
depth = (depth or 0) + 1
|
|
139
|
+
if depth > 16 then return nil end
|
|
140
|
+
if T.k[last] == 'id' and T:text(last - 1) ~= '.' then
|
|
141
|
+
return { kind = 'binding_value', name = T:text(last), at = T.s[last] - 1 }, last, 1
|
|
142
|
+
end
|
|
143
|
+
if T:text(last) ~= ')' or not T.m[last] then return nil end
|
|
144
|
+
local open = T.m[last]
|
|
145
|
+
if T.k[open - 1] == 'id' then
|
|
146
|
+
local name_at, first, count = open - 1, open - 1, 1
|
|
147
|
+
local call = { kind = 'call_result', name = T:text(name_at), at = T.s[name_at] - 1, result = 1, arity = 1 }
|
|
148
|
+
if T:text(name_at - 1) == '.' then
|
|
149
|
+
local receiver, start, size = expression(T, name_at - 2, depth)
|
|
150
|
+
if not receiver then return nil end
|
|
151
|
+
first, count = start, count + size
|
|
152
|
+
if receiver.kind == 'binding_value' then call.recv = receiver.name
|
|
153
|
+
else call.receiver = receiver end
|
|
154
|
+
end
|
|
155
|
+
return call, first, count
|
|
156
|
+
end
|
|
157
|
+
local value, first, count = expression(T, last - 1, depth)
|
|
158
|
+
if first == open + 1 then return value, open, count end
|
|
159
|
+
end
|
|
160
|
+
M.expression = expression
|
|
161
|
+
|
|
162
|
+
function M.collect(r, T)
|
|
163
|
+
if r.language ~= 'go' then return end
|
|
164
|
+
local data = { nodes = {}, bindings = {}, shadows = {}, evidence = {}, receivers = {}, receiver_weight = 0 }
|
|
165
|
+
r.go_methods = data
|
|
166
|
+
local children, scopes, stack, functions, named_functions = {}, {}, {}, {}, {}
|
|
167
|
+
for i = 1, T.n do
|
|
168
|
+
if T:text(i) == '{' and T.m[i] then stack[#stack + 1] = i end
|
|
169
|
+
scopes[i] = stack[#stack] or 0
|
|
170
|
+
if stack[#stack] and T.m[stack[#stack]] == i then stack[#stack] = nil end
|
|
171
|
+
end
|
|
172
|
+
for id, n in ipairs(r.nodes) do
|
|
173
|
+
if n.parent then children[n.parent] = children[n.parent] or {}; children[n.parent][#children[n.parent] + 1] = id end
|
|
174
|
+
end
|
|
175
|
+
local function binding(name, ty, token, first, last, generic)
|
|
176
|
+
if name == '_' then return end
|
|
177
|
+
data.bindings[#data.bindings + 1] = { name = name, type = ty or false, at = T.s[token] - 1,
|
|
178
|
+
first = first, last = last, generic = generic or nil }
|
|
179
|
+
end
|
|
180
|
+
for id, n in ipairs(r.nodes) do
|
|
181
|
+
if id % 128 == 0 then control.check() end
|
|
182
|
+
local d = {}; data.nodes[id] = d
|
|
183
|
+
if kinds[n.kind] then
|
|
184
|
+
local name_at = n.ti
|
|
185
|
+
if T:text(name_at) == 'type' then name_at = name_at + 1 end
|
|
186
|
+
local bracket = name_at + 1
|
|
187
|
+
d.generic = T:text(bracket) == '[' and T.m[bracket] and T.m[bracket] > bracket + 2
|
|
188
|
+
and T.k[bracket + 1] == 'id' or nil
|
|
189
|
+
local after = name_at + 1
|
|
190
|
+
if d.generic then after = (T.m[after] or after) + 1 end
|
|
191
|
+
d.alias = T:text(after) == '=' or nil
|
|
192
|
+
if d.alias then after = after + 1 end
|
|
193
|
+
d.underlying = type_expr(T, after, n.tj)
|
|
194
|
+
d.embeds, d.fields = {}, {}
|
|
195
|
+
if n.kind == 'interface' then
|
|
196
|
+
local open = after + 1
|
|
197
|
+
if T:text(after) ~= 'interface' or T:text(open) ~= '{' or not T.m[open] then d.unsupported = true
|
|
198
|
+
else
|
|
199
|
+
local i, last = open + 1, T.m[open] - 1
|
|
200
|
+
while i <= last do
|
|
201
|
+
if T:text(i) == ';' then i = i + 1
|
|
202
|
+
else
|
|
203
|
+
local finish = statement_end(T, i, last)
|
|
204
|
+
if T:text(i + 1) ~= '(' then
|
|
205
|
+
local ty = type_expr(T, i, finish)
|
|
206
|
+
if not ty or ty.kind ~= 'name' then d.unsupported = true else d.embeds[#d.embeds + 1] = ty end
|
|
207
|
+
end
|
|
208
|
+
i = finish + 1
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
elseif n.kind == 'struct' then
|
|
213
|
+
for _, child in ipairs(children[id] or {}) do
|
|
214
|
+
local field = r.nodes[child]
|
|
215
|
+
if field.kind == 'field' then
|
|
216
|
+
d.fields[#d.fields + 1] = child
|
|
217
|
+
local last = field.tj
|
|
218
|
+
if T.k[last] == 'str' then last = last - 1 end
|
|
219
|
+
if not field.embedded and T:text(field.ti + 1) == '[' and T.m[field.ti + 1] == last then d.unsupported = true end
|
|
220
|
+
if field.embedded then
|
|
221
|
+
local ty = type_expr(T, field.ti, last)
|
|
222
|
+
if ty then d.embeds[#d.embeds + 1] = ty else d.unsupported = true end
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
elseif not d.alias and not d.underlying then d.unsupported = true end
|
|
227
|
+
elseif n.kind == 'method' or n.kind == 'function' then
|
|
228
|
+
named_functions[n.ti] = true
|
|
229
|
+
local at, receiver, recv_name = n.ti, nil, nil
|
|
230
|
+
if T:text(at) == 'func' then
|
|
231
|
+
at = at + 1
|
|
232
|
+
if T:text(at) == '(' and T.m[at] then
|
|
233
|
+
local close = T.m[at]
|
|
234
|
+
receiver = type_expr(T, at + 1, close - 1)
|
|
235
|
+
if not receiver and T.k[at + 1] == 'id' then
|
|
236
|
+
recv_name, receiver = T:text(at + 1), type_expr(T, at + 2, close - 1)
|
|
237
|
+
end
|
|
238
|
+
at = close + 1
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
local open = at + 1
|
|
242
|
+
d.generic = T:text(open) == '[' or (n.recv and not receiver) or nil
|
|
243
|
+
if T:text(open) == '[' then open = (T.m[open] or open) + 1 end
|
|
244
|
+
if T:text(open) == '(' then
|
|
245
|
+
local sig, names, result_names, body = signature(T, open, n.tj)
|
|
246
|
+
d.signature, d.receiver = sig, receiver
|
|
247
|
+
local params = sig and sig.params
|
|
248
|
+
if not body and T.m[open] then
|
|
249
|
+
body = function_body(T, T.m[open], n.tj)
|
|
250
|
+
params, names = parameters(T, open + 1, T.m[open] - 1)
|
|
251
|
+
end
|
|
252
|
+
if body and T:text(body) == '{' and T.m[body] then
|
|
253
|
+
local first, last = T.s[body] - 1, T.e[T.m[body]]
|
|
254
|
+
if d.generic then functions[#functions + 1] = { first = first, last = last, generic = true } end
|
|
255
|
+
for _, list in ipairs({ names or {}, result_names or {} }) do
|
|
256
|
+
for index, entry in ipairs(list) do
|
|
257
|
+
if entry then binding(entry.name, (list == names and params or sig.results)[index], entry.token, first, last, d.generic) end
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
if recv_name then binding(recv_name, receiver, n.ti, first, last, d.generic) end
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
local function declaration(name_at, ending, ty, token)
|
|
266
|
+
local scope = scopes[name_at]
|
|
267
|
+
local first, last = scope > 0 and T.s[scope] - 1 or 0, scope > 0 and T.e[T.m[scope]] or #T.src
|
|
268
|
+
local generic = false
|
|
269
|
+
for _, fn in ipairs(functions) do if fn.generic and fn.first <= first and last <= fn.last then generic = true; break end end
|
|
270
|
+
for i = name_at - 1, math.max(1, name_at - 64), -1 do
|
|
271
|
+
local word = T:text(i)
|
|
272
|
+
if word == ';' or word == '{' or word == '}' then break end
|
|
273
|
+
if word == 'if' or word == 'for' or word == 'switch' then generic = true; break end
|
|
274
|
+
end
|
|
275
|
+
binding(T:text(name_at), ty, token or name_at, first, last, generic)
|
|
276
|
+
local entry = data.bindings[#data.bindings]
|
|
277
|
+
if entry and entry.name == T:text(name_at) then entry.at = scope == 0 and 0 or T.e[ending] end
|
|
278
|
+
end
|
|
279
|
+
local function initializer(a, b, depth)
|
|
280
|
+
depth = (depth or 0) + 1
|
|
281
|
+
if depth > 16 then return nil end
|
|
282
|
+
if T:text(a) == '(' and T.m[a] == b then return initializer(a + 1, b - 1, depth) end
|
|
283
|
+
if a == b and T.k[a] == 'id' then
|
|
284
|
+
return { kind = 'binding_value', name = T:text(a), at = T.s[a] - 1 }
|
|
285
|
+
end
|
|
286
|
+
local address = T:text(a) == '&'
|
|
287
|
+
if address then a = a + 1 end
|
|
288
|
+
if T:text(a) == 'new' and T:text(a + 1) == '(' and T.m[a + 1] == b then
|
|
289
|
+
local ty = type_expr(T, a + 2, b - 1)
|
|
290
|
+
return ty and { kind = 'pointer', child = ty, builtin_new = true, at = T.s[a] - 1 } or nil
|
|
291
|
+
end
|
|
292
|
+
if not address then
|
|
293
|
+
local value, first = expression(T, b)
|
|
294
|
+
if first == a then return value end
|
|
295
|
+
end
|
|
296
|
+
local open = a
|
|
297
|
+
while open <= b and T:text(open) ~= '{' do open = open + 1 end
|
|
298
|
+
if T:text(open) ~= '{' or T.m[open] ~= b then return nil end
|
|
299
|
+
local ty = type_expr(T, a, open - 1)
|
|
300
|
+
return ty and (address and { kind = 'pointer', child = ty } or ty) or nil
|
|
301
|
+
end
|
|
302
|
+
local function initializers(a, b, count)
|
|
303
|
+
local values, start, i = {}, a, a
|
|
304
|
+
while i <= b + 1 do
|
|
305
|
+
if i == b + 1 or T:text(i) == ',' then
|
|
306
|
+
values[#values + 1] = initializer(start, i - 1) or false; start = i + 1
|
|
307
|
+
elseif T.m[i] and T.m[i] > i then i = T.m[i] end
|
|
308
|
+
i = i + 1
|
|
309
|
+
end
|
|
310
|
+
if #values == 1 and values[1] and values[1].kind == 'call_result' then
|
|
311
|
+
local call = values[1]; values = {}
|
|
312
|
+
for index = 1, count do values[index] = { kind = call.kind, name = call.name, recv = call.recv,
|
|
313
|
+
receiver = call.receiver, at = call.at, result = index, arity = count } end
|
|
314
|
+
elseif #values ~= count then return {} end
|
|
315
|
+
return values
|
|
316
|
+
end
|
|
317
|
+
local function variable(first, finish)
|
|
318
|
+
local after = first + 1
|
|
319
|
+
while T:text(after) == ',' and T.k[after + 1] == 'id' do after = after + 2 end
|
|
320
|
+
local equal = after
|
|
321
|
+
while equal <= finish and T:text(equal) ~= '=' do equal = equal + 1 end
|
|
322
|
+
local ty = type_expr(T, after, equal - 1)
|
|
323
|
+
local values = not ty and after == equal and initializers(equal + 1, finish, (after - first + 1) / 2) or {}
|
|
324
|
+
for x = first, after - 1, 2 do declaration(x, finish, ty or values[(x - first) / 2 + 1]) end
|
|
325
|
+
end
|
|
326
|
+
for i = 1, T.n do
|
|
327
|
+
if i % 4096 == 0 then control.check() end
|
|
328
|
+
if T:text(i) == 'var' and T.k[i + 1] == 'id' then
|
|
329
|
+
local finish = statement_end(T, i + 1, T.n)
|
|
330
|
+
variable(i + 1, finish)
|
|
331
|
+
elseif T:text(i) == 'var' and T:text(i + 1) == '(' and T.m[i + 1] then
|
|
332
|
+
local at, last = i + 2, T.m[i + 1] - 1
|
|
333
|
+
while at <= last do
|
|
334
|
+
if T:text(at) == ';' then at = at + 1
|
|
335
|
+
else local ending = statement_end(T, at, last); if T.k[at] == 'id' then variable(at, ending) end; at = ending + 1 end
|
|
336
|
+
end
|
|
337
|
+
elseif T:text(i) == 'type' and T.k[i + 1] == 'id' and scopes[i] > 0 then
|
|
338
|
+
local scope = scopes[i]
|
|
339
|
+
data.shadows[#data.shadows + 1] = { name = T:text(i + 1), first = T.s[i] - 1, last = T.e[T.m[scope]] }
|
|
340
|
+
elseif T:text(i) == 'type' and T:text(i + 1) == '(' and T.m[i + 1] and scopes[i] > 0 then
|
|
341
|
+
local at, last, scope = i + 2, T.m[i + 1] - 1, scopes[i]
|
|
342
|
+
while at <= last do
|
|
343
|
+
if T:text(at) == ';' then at = at + 1
|
|
344
|
+
else
|
|
345
|
+
if T.k[at] == 'id' then data.shadows[#data.shadows + 1] = {
|
|
346
|
+
name = T:text(at), first = T.s[i] - 1, last = T.e[T.m[scope]] } end
|
|
347
|
+
at = statement_end(T, at, last) + 1
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
elseif T:text(i) == 'func' and not named_functions[i] and T:text(i + 1) == '(' then
|
|
351
|
+
local sig, names, results, body = signature(T, i + 1, T.n)
|
|
352
|
+
local params = sig and sig.params
|
|
353
|
+
if not body and T.m[i + 1] then
|
|
354
|
+
body = function_body(T, T.m[i + 1], T.n)
|
|
355
|
+
params, names = parameters(T, i + 2, T.m[i + 1] - 1)
|
|
356
|
+
end
|
|
357
|
+
if body and T:text(body) == '{' and T.m[body] then
|
|
358
|
+
for _, list in ipairs({ names or {}, results or {} }) do
|
|
359
|
+
for index, entry in ipairs(list) do
|
|
360
|
+
if entry then binding(entry.name, (list == names and params or sig.results)[index], entry.token,
|
|
361
|
+
T.s[body] - 1, T.e[T.m[body]]) end
|
|
362
|
+
end
|
|
363
|
+
end
|
|
364
|
+
end
|
|
365
|
+
elseif T:text(i) == ':=' and T.k[i - 1] == 'id' then
|
|
366
|
+
local first, finish = i - 1, statement_end(T, i + 1, T.n)
|
|
367
|
+
while T:text(first - 1) == ',' and T.k[first - 2] == 'id' do first = first - 2 end
|
|
368
|
+
if T:text(i + 1) == 'range' and T:text(first - 1) == 'for' then
|
|
369
|
+
local open = i + 2
|
|
370
|
+
while open <= T.n and T:text(open) ~= '{' and T:text(open) ~= ';' do
|
|
371
|
+
if T.m[open] and T.m[open] > open then open = T.m[open] end
|
|
372
|
+
open = open + 1
|
|
373
|
+
end
|
|
374
|
+
local value = T:text(open) == '{' and T.m[open] and initializer(i + 2, open - 1)
|
|
375
|
+
if T:text(open) == '{' and T.m[open] then
|
|
376
|
+
for x = first, i - 1, 2 do binding(T:text(x), value and { kind = 'range_value', child = value,
|
|
377
|
+
result = (x - first) / 2 + 1, arity = (i - first + 1) / 2 }, x, T.s[open] - 1, T.e[T.m[open]]) end
|
|
378
|
+
else for x = first, i - 1, 2 do declaration(x, finish) end end
|
|
379
|
+
else
|
|
380
|
+
local values = initializers(i + 1, finish, (i - first + 1) / 2)
|
|
381
|
+
for x = first, i - 1, 2 do declaration(x, finish, values[(x - first) / 2 + 1]) end
|
|
382
|
+
end
|
|
383
|
+
end
|
|
384
|
+
end
|
|
385
|
+
table.sort(functions, function(a, b) return a.first < b.first end)
|
|
386
|
+
local function generic_at(at)
|
|
387
|
+
local lo, hi = 1, #functions
|
|
388
|
+
while lo <= hi do
|
|
389
|
+
local mid = math.floor((lo + hi) / 2)
|
|
390
|
+
if functions[mid].first <= at then lo = mid + 1 else hi = mid - 1 end
|
|
391
|
+
end
|
|
392
|
+
return hi > 0 and at <= functions[hi].last
|
|
393
|
+
end
|
|
394
|
+
for index, ref in ipairs(r.refs) do
|
|
395
|
+
local at = T.s[ref.tok] - 1
|
|
396
|
+
local receiver, _, size
|
|
397
|
+
if T:text(ref.tok - 1) == '.' then receiver, _, size = expression(T, ref.tok - 2) end
|
|
398
|
+
if generic_at(at) then
|
|
399
|
+
ref.binding = 'go:?'; data.evidence[index] = ref.binding; r.has_bindings = true
|
|
400
|
+
elseif receiver and receiver.kind ~= 'binding_value' then
|
|
401
|
+
data.receivers[at] = receiver; data.receiver_weight = data.receiver_weight + size * 192
|
|
402
|
+
ref.binding = 'go:expr:' .. at; data.evidence[index] = ref.binding; r.has_bindings = true
|
|
403
|
+
elseif ref.recv then
|
|
404
|
+
ref.binding = 'go:recv:' .. (T.s[ref.tok] - 1); r.has_bindings = true
|
|
405
|
+
if T.el[ref.tok - 3] == T.l[ref.tok - 2] and (T:text(ref.tok - 3) == '.'
|
|
406
|
+
or T:text(ref.tok - 3) == ')' or T:text(ref.tok - 3) == ']') then ref.binding = 'go:?' end
|
|
407
|
+
data.evidence[index] = ref.binding
|
|
408
|
+
elseif ref.kind == 'call' then
|
|
409
|
+
ref.binding = 'go:call:' .. (T.s[ref.tok] - 1)
|
|
410
|
+
data.evidence[index] = ref.binding; r.has_bindings = true
|
|
411
|
+
elseif ref.kind == 'member_call' then
|
|
412
|
+
ref.binding = 'go:?'; data.evidence[index] = ref.binding; r.has_bindings = true
|
|
413
|
+
end
|
|
414
|
+
end
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
local primitive = {}
|
|
418
|
+
for word in ('bool string int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr float32 float64 complex64 complex128 error any'):gmatch('%S+') do primitive[word] = word end
|
|
419
|
+
primitive.byte, primitive.rune = 'uint8', 'int32'
|
|
420
|
+
|
|
421
|
+
function M.context(G, ctx, report)
|
|
422
|
+
ctx = ctx or {}
|
|
423
|
+
if ctx.go_methods then
|
|
424
|
+
if report then ctx.go_methods.report = report end
|
|
425
|
+
return ctx.go_methods
|
|
426
|
+
end
|
|
427
|
+
local C = { incomplete = false, report = report, stats = { files = 0, bytes = 0, sets = 0, candidates = 0, links = 0 } }
|
|
428
|
+
local deadline = xtimer.now_ms() + M.MAX_TIME_MS
|
|
429
|
+
ctx.go_methods = C
|
|
430
|
+
local data, packages, owners, sets, signatures, outgoing, incoming = {}, {}, {}, {}, {}, {}, {}
|
|
431
|
+
local modules = require('codeoutline.go_modules')
|
|
432
|
+
local function limited() C.incomplete, ctx.go_methods_incomplete = true, true end
|
|
433
|
+
local function expired()
|
|
434
|
+
control.check()
|
|
435
|
+
if xtimer.now_ms() > deadline then limited(); return true end
|
|
436
|
+
return false
|
|
437
|
+
end
|
|
438
|
+
function C.data(f)
|
|
439
|
+
if data[f] ~= nil then return data[f] or nil end
|
|
440
|
+
local rec = G.files[f]
|
|
441
|
+
if ctx.ordinary and not ctx.ordinary.available(f) then data[f] = false; return nil end
|
|
442
|
+
local size = (ctx.ordinary and ctx.ordinary.source_size(f)) or rec.size or #rec.nodes * 128
|
|
443
|
+
if expired() or C.stats.files >= M.MAX_FILES or C.stats.bytes + size > M.MAX_BYTES then limited(); data[f] = false; return nil end
|
|
444
|
+
C.stats.files, C.stats.bytes = C.stats.files + 1, C.stats.bytes + size
|
|
445
|
+
local parsed = ctx.ordinary and ctx.ordinary.data(f) or rec
|
|
446
|
+
data[f] = parsed.go_methods or false
|
|
447
|
+
return data[f] or nil
|
|
448
|
+
end
|
|
449
|
+
local function info(id)
|
|
450
|
+
local f = G.node_file[id]
|
|
451
|
+
if not G.id_of[f][1] or id < G.id_of[f][1] or id > G.id_of[f][#G.id_of[f]] then return nil end
|
|
452
|
+
local d = C.data(f)
|
|
453
|
+
return d and d.nodes[id - G.id_of[f][1] + 1] or nil
|
|
454
|
+
end
|
|
455
|
+
local function peers(f)
|
|
456
|
+
if not packages[f] then
|
|
457
|
+
local files = modules.peers(G, f, ctx) or {}
|
|
458
|
+
local members = {}; for _, g in ipairs(files) do members[g] = true end
|
|
459
|
+
packages[f] = { files = files, members = members,
|
|
460
|
+
key = (G.files[f].path:match('^(.*)/') or '') .. '\0' .. (G.files[f].package or '') }
|
|
461
|
+
end
|
|
462
|
+
return packages[f]
|
|
463
|
+
end
|
|
464
|
+
function C.bound(f, name, at)
|
|
465
|
+
local d, best = C.data(f), nil
|
|
466
|
+
if not d then return false end
|
|
467
|
+
for _, entry in ipairs(d.bindings) do
|
|
468
|
+
if entry.name == name and entry.first <= at and at <= entry.last and entry.at <= at
|
|
469
|
+
and (not best or entry.first > best.first or entry.first == best.first and entry.at > best.at) then best = entry end
|
|
470
|
+
end
|
|
471
|
+
return best
|
|
472
|
+
end
|
|
473
|
+
local function shadowed(f, name, at)
|
|
474
|
+
local d = C.data(f)
|
|
475
|
+
if not d then return true end
|
|
476
|
+
for _, entry in ipairs(d.shadows) do
|
|
477
|
+
if entry.name == name and entry.first <= at and at <= entry.last then return true end
|
|
478
|
+
end
|
|
479
|
+
return false
|
|
480
|
+
end
|
|
481
|
+
local function named(f, name, at)
|
|
482
|
+
local alias, member = name:match('^([%w_]+)%.([%w_]+)$')
|
|
483
|
+
local files
|
|
484
|
+
if at then
|
|
485
|
+
if shadowed(f, alias or name, at) then return {}, true end
|
|
486
|
+
if C.bound(f, alias or name, at) then return {}, true end
|
|
487
|
+
end
|
|
488
|
+
if alias then
|
|
489
|
+
if not member:match('^%u') then return {}, true end
|
|
490
|
+
local imports = modules.aliases(G, f, ctx)
|
|
491
|
+
local entry = imports[alias]
|
|
492
|
+
if not entry then return {}, true end
|
|
493
|
+
files, name = {}, member
|
|
494
|
+
for _, g in ipairs(entry.files) do files[g] = true end
|
|
495
|
+
else files = peers(f).members end
|
|
496
|
+
local ids, found = {}, false
|
|
497
|
+
for i, id in ipairs(G.by_name[name] or {}) do
|
|
498
|
+
if i > M.MAX_NODES then limited(); return {}, true end
|
|
499
|
+
local n, g = G.nodes[id], G.node_file[id]
|
|
500
|
+
if files[g] and not n.parent and n.kind ~= 'method' then
|
|
501
|
+
found = true
|
|
502
|
+
if kinds[n.kind] then ids[#ids + 1] = id end
|
|
503
|
+
end
|
|
504
|
+
end
|
|
505
|
+
return ids, found or alias ~= nil
|
|
506
|
+
end
|
|
507
|
+
local canonical, resolve_type
|
|
508
|
+
resolve_type = function(f, ty, seen, depth)
|
|
509
|
+
depth, seen = (depth or 0) + 1, seen or {}
|
|
510
|
+
if not ty or ty.kind ~= 'name' or depth > M.MAX_DEPTH then return nil end
|
|
511
|
+
local ids = named(f, ty.name, ty.at)
|
|
512
|
+
if #ids ~= 1 then return nil end
|
|
513
|
+
local id, d = ids[1], info(ids[1])
|
|
514
|
+
if not d or d.generic or seen[id] then return nil end
|
|
515
|
+
if d.alias then
|
|
516
|
+
seen[id] = true
|
|
517
|
+
return resolve_type(G.node_file[id], d.underlying, seen, depth)
|
|
518
|
+
end
|
|
519
|
+
return id
|
|
520
|
+
end
|
|
521
|
+
canonical = function(f, ty, path, depth)
|
|
522
|
+
path, depth = path or {}, (depth or 0) + 1
|
|
523
|
+
if not ty or depth > M.MAX_DEPTH then return nil end
|
|
524
|
+
if ty.kind == 'name' then
|
|
525
|
+
local ids, known = named(f, ty.name)
|
|
526
|
+
if #ids == 0 and not known then return primitive[ty.name] and 'builtin:' .. primitive[ty.name] or nil end
|
|
527
|
+
if #ids ~= 1 then return nil end
|
|
528
|
+
local id, d = ids[1], info(ids[1])
|
|
529
|
+
if not d or d.generic or path[id] then return nil end
|
|
530
|
+
if d.alias then
|
|
531
|
+
path[id] = true; local result = canonical(G.node_file[id], d.underlying, path, depth); path[id] = nil
|
|
532
|
+
return result
|
|
533
|
+
end
|
|
534
|
+
return 'type:' .. id
|
|
535
|
+
elseif ty.kind == 'any' then return 'builtin:any' end
|
|
536
|
+
if ty.kind == 'function' then
|
|
537
|
+
local parts = {}
|
|
538
|
+
for _, list in ipairs({ ty.params, ty.results }) do
|
|
539
|
+
local items = {}
|
|
540
|
+
for _, item in ipairs(list) do
|
|
541
|
+
local value = canonical(f, item, path, depth)
|
|
542
|
+
if not value then return nil end
|
|
543
|
+
items[#items + 1] = value
|
|
544
|
+
end
|
|
545
|
+
parts[#parts + 1] = table.concat(items, ',')
|
|
546
|
+
end
|
|
547
|
+
return 'func(' .. table.concat(parts, ')->(') .. ')'
|
|
548
|
+
end
|
|
549
|
+
local child = canonical(f, ty.child, path, depth)
|
|
550
|
+
if not child then return nil end
|
|
551
|
+
if ty.kind == 'map' then
|
|
552
|
+
local key = canonical(f, ty.key, path, depth)
|
|
553
|
+
return key and 'map[' .. key .. ']' .. child or nil
|
|
554
|
+
end
|
|
555
|
+
return ty.kind .. ':' .. tostring(ty.extra or '') .. '[' .. child .. ']'
|
|
556
|
+
end
|
|
557
|
+
function C.signature(id)
|
|
558
|
+
if signatures[id] ~= nil then return signatures[id] or nil end
|
|
559
|
+
local d, f = info(id), G.node_file[id]
|
|
560
|
+
if not d or d.generic or not d.signature then signatures[id] = false; return nil end
|
|
561
|
+
local result = {}
|
|
562
|
+
for _, list in ipairs({ d.signature.params, d.signature.results }) do
|
|
563
|
+
local items = {}
|
|
564
|
+
for _, ty in ipairs(list) do
|
|
565
|
+
local value = canonical(f, ty)
|
|
566
|
+
if not value then signatures[id] = false; return nil end
|
|
567
|
+
items[#items + 1] = value
|
|
568
|
+
end
|
|
569
|
+
result[#result + 1] = table.concat(items, ',')
|
|
570
|
+
end
|
|
571
|
+
signatures[id] = table.concat(result, ' -> ')
|
|
572
|
+
return signatures[id]
|
|
573
|
+
end
|
|
574
|
+
local function method_key(f, name)
|
|
575
|
+
return name:match('^%u') and name or peers(f).key .. '\0' .. name
|
|
576
|
+
end
|
|
577
|
+
function C.owner(id)
|
|
578
|
+
if owners[id] ~= nil then return owners[id] or nil end
|
|
579
|
+
local n, f = G.nodes[id], G.node_file[id]
|
|
580
|
+
local owner
|
|
581
|
+
if n.parent and G.files[f].nodes[n.parent].kind == 'interface' then owner = G.id_of[f][n.parent]
|
|
582
|
+
elseif n.kind == 'method' and n.recv then
|
|
583
|
+
local ids = named(f, n.recv)
|
|
584
|
+
if #ids == 1 and G.nodes[ids[1]].kind ~= 'typedef' then owner = ids[1]
|
|
585
|
+
else owner = resolve_type(f, { kind = 'name', name = n.recv }) end
|
|
586
|
+
end
|
|
587
|
+
owners[id] = owner or false
|
|
588
|
+
return owner
|
|
589
|
+
end
|
|
590
|
+
local function declarations(id, filter)
|
|
591
|
+
local n, f, out, seen = G.nodes[id], G.node_file[id], {}, {}
|
|
592
|
+
local function add(mid)
|
|
593
|
+
local g = G.node_file[mid]
|
|
594
|
+
if not G.id_of[g][1] or mid < G.id_of[g][1] or mid > G.id_of[g][#G.id_of[g]] then return end
|
|
595
|
+
if filter and not filter[G.nodes[mid].name] then return end
|
|
596
|
+
if not seen[mid] and peers(f).members[g] and G.nodes[mid].kind == 'method' and C.owner(mid) == id then
|
|
597
|
+
seen[mid] = true; out[#out + 1] = mid
|
|
598
|
+
end
|
|
599
|
+
end
|
|
600
|
+
for i, mid in ipairs(G.by_owner[n.qualified] or {}) do
|
|
601
|
+
if i > M.MAX_NODES then limited(); break end
|
|
602
|
+
add(mid)
|
|
603
|
+
end
|
|
604
|
+
for g in pairs(G.drafts or {}) do
|
|
605
|
+
if peers(f).members[g] then for _, mid in ipairs(G.id_of[g]) do add(mid) end end
|
|
606
|
+
end
|
|
607
|
+
return out
|
|
608
|
+
end
|
|
609
|
+
local function set(id, pointer, path, depth, filter)
|
|
610
|
+
local names = {}
|
|
611
|
+
for name in pairs(filter or {}) do names[#names + 1] = name end
|
|
612
|
+
table.sort(names)
|
|
613
|
+
local key = id .. (pointer and '*' or '') .. ':' .. (filter and table.concat(names, '\0') or '*')
|
|
614
|
+
if sets[key] then return sets[key] end
|
|
615
|
+
depth, path = depth or 0, path or {}
|
|
616
|
+
if expired() or depth > M.MAX_DEPTH or C.stats.sets >= M.MAX_TYPES then limited(); return { entries = {}, valid = false } end
|
|
617
|
+
if path[id] then return { entries = {}, valid = false } end
|
|
618
|
+
C.stats.sets = C.stats.sets + 1
|
|
619
|
+
local d, n, f = info(id), G.nodes[id], G.node_file[id]
|
|
620
|
+
local result = { entries = {}, valid = d ~= nil and not d.generic and not d.unsupported }
|
|
621
|
+
if not d or not result.valid then sets[key] = result; return result end
|
|
622
|
+
if n.kind == 'interface' and pointer then result.valid = false; sets[key] = result; return result end
|
|
623
|
+
path[id] = true
|
|
624
|
+
local function add(name, entry)
|
|
625
|
+
if entry.ids then
|
|
626
|
+
local ids = {}; for _, mid in ipairs(entry.ids) do ids[#ids + 1] = mid end
|
|
627
|
+
entry.ids = ids
|
|
628
|
+
end
|
|
629
|
+
local old = result.entries[name]
|
|
630
|
+
if old and n.kind == 'interface' then
|
|
631
|
+
if old.signature and old.signature == entry.signature then
|
|
632
|
+
local seen = {}; for _, mid in ipairs(old.ids or {}) do seen[mid] = true end
|
|
633
|
+
for _, mid in ipairs(entry.ids or {}) do if not seen[mid] then old.ids[#old.ids + 1] = mid end end
|
|
634
|
+
else result.valid = false; result.entries[name] = { depth = 0, blocked = true } end
|
|
635
|
+
elseif not old or entry.depth < old.depth then result.entries[name] = entry
|
|
636
|
+
elseif old.depth == entry.depth then
|
|
637
|
+
result.entries[name] = { depth = old.depth, blocked = true }
|
|
638
|
+
end
|
|
639
|
+
end
|
|
640
|
+
for _, mid in ipairs(declarations(id, filter)) do
|
|
641
|
+
local md, mf = info(mid), G.node_file[mid]
|
|
642
|
+
local usable = md and not md.generic and (n.kind == 'interface' or md.receiver)
|
|
643
|
+
local is_pointer = usable and md.receiver and md.receiver.kind == 'pointer'
|
|
644
|
+
local sig = usable and C.signature(mid)
|
|
645
|
+
add(method_key(mf, G.nodes[mid].name), { depth = 0, ids = { mid }, signature = sig,
|
|
646
|
+
blocked = not usable or is_pointer and not pointer })
|
|
647
|
+
end
|
|
648
|
+
for _, field in ipairs(d.fields or {}) do
|
|
649
|
+
local mid = G.id_of[f][field]
|
|
650
|
+
if not filter or filter[G.nodes[mid].name] then add(method_key(f, G.nodes[mid].name), { depth = 0, field = mid, blocked = true }) end
|
|
651
|
+
end
|
|
652
|
+
if n.kind == 'typedef' and not d.alias then
|
|
653
|
+
local ty = d.underlying
|
|
654
|
+
if not ty or ty.kind == 'pointer' or ty.kind == 'name' and not primitive[ty.name] then result.valid = false end
|
|
655
|
+
if ty and ty.kind == 'pointer' then result.invalid = true end
|
|
656
|
+
end
|
|
657
|
+
for _, embedded in ipairs(d.embeds or {}) do
|
|
658
|
+
local ptr, ty = embedded.kind == 'pointer', embedded.kind == 'pointer' and embedded.child or embedded
|
|
659
|
+
local base = resolve_type(f, ty)
|
|
660
|
+
if not base or ptr and G.nodes[base].kind == 'interface'
|
|
661
|
+
or n.kind == 'interface' and G.nodes[base].kind ~= 'interface' then
|
|
662
|
+
result.valid = false
|
|
663
|
+
if base and ptr and G.nodes[base].kind == 'interface' then result.invalid = true end
|
|
664
|
+
else
|
|
665
|
+
local inherited = set(base, G.nodes[base].kind ~= 'interface' and (pointer or ptr), path, depth + 1, filter)
|
|
666
|
+
if not inherited.valid then result.valid = false end
|
|
667
|
+
if inherited.invalid then result.invalid = true end
|
|
668
|
+
for name, entry in pairs(inherited.entries) do
|
|
669
|
+
add(name, { depth = entry.depth + 1, ids = entry.ids, signature = entry.signature,
|
|
670
|
+
field = entry.field, blocked = entry.blocked })
|
|
671
|
+
end
|
|
672
|
+
end
|
|
673
|
+
end
|
|
674
|
+
path[id] = nil; sets[key] = result
|
|
675
|
+
return result
|
|
676
|
+
end
|
|
677
|
+
C.method_set = set
|
|
678
|
+
local value_type
|
|
679
|
+
value_type = function(f, ty, path, depth)
|
|
680
|
+
path, depth = path or {}, (depth or 0) + 1
|
|
681
|
+
if not ty or depth > M.MAX_DEPTH or expired() then return nil end
|
|
682
|
+
if ty.kind == 'binding_value' then
|
|
683
|
+
local entry = C.bound(f, ty.name, ty.at)
|
|
684
|
+
if not entry or entry.generic or path[entry] then return nil end
|
|
685
|
+
path[entry] = true
|
|
686
|
+
local result, origin = value_type(f, entry.type, path, depth)
|
|
687
|
+
path[entry] = nil; return result, origin
|
|
688
|
+
elseif ty.kind == 'call_result' then
|
|
689
|
+
local ids
|
|
690
|
+
if ty.receiver or ty.recv then
|
|
691
|
+
local receiver
|
|
692
|
+
if ty.receiver then receiver = C.expression_receiver(f, ty.receiver, path, depth)
|
|
693
|
+
else receiver = C.receiver(f, ty.recv, ty.at, path, depth) end
|
|
694
|
+
if receiver == false or receiver and receiver.type_expr then return nil end
|
|
695
|
+
if receiver then
|
|
696
|
+
ids = {}; for _, member in ipairs(C.members(receiver, f, ty.name)) do
|
|
697
|
+
if member.kind == 'method' then ids[#ids + 1] = member.id end
|
|
698
|
+
end
|
|
699
|
+
else ids = modules.resolve(G, f, ty.name, ty.recv, ctx) end
|
|
700
|
+
elseif not C.bound(f, ty.name, ty.at) and not shadowed(f, ty.name, ty.at) then
|
|
701
|
+
ids = modules.resolve(G, f, ty.name, nil, ctx)
|
|
702
|
+
end
|
|
703
|
+
if not ids or #ids ~= 1 then return nil end
|
|
704
|
+
local d, origin = info(ids[1]), G.node_file[ids[1]]
|
|
705
|
+
if not d or d.generic or not d.signature or #d.signature.results ~= ty.arity then return nil end
|
|
706
|
+
return value_type(origin, d.signature.results[ty.result], path, depth)
|
|
707
|
+
elseif ty.kind == 'range_value' then
|
|
708
|
+
local collection, origin = value_type(f, ty.child, path, depth)
|
|
709
|
+
local visited = {}
|
|
710
|
+
for _ = 1, M.MAX_DEPTH do
|
|
711
|
+
if not collection then return nil end
|
|
712
|
+
if collection.kind ~= 'name' then break end
|
|
713
|
+
local ids, known = named(origin, collection.name, collection.at)
|
|
714
|
+
if #ids == 0 and not known and collection.name == 'string' then
|
|
715
|
+
if ty.arity > 2 then return nil end
|
|
716
|
+
return { kind = 'builtin', name = ty.result == 1 and 'int' or 'int32' }, origin
|
|
717
|
+
end
|
|
718
|
+
if #ids ~= 1 or visited[ids[1]] then return nil end
|
|
719
|
+
local d = info(ids[1]); visited[ids[1]] = true
|
|
720
|
+
if not d or d.generic then return nil end
|
|
721
|
+
collection, origin = d.underlying, G.node_file[ids[1]]
|
|
722
|
+
end
|
|
723
|
+
if not collection then return nil end
|
|
724
|
+
if collection.kind == 'pointer' and collection.child.kind == 'array' then collection = collection.child end
|
|
725
|
+
if collection.kind == 'channel' then
|
|
726
|
+
if collection.extra == 'send' or ty.arity ~= 1 then return nil end
|
|
727
|
+
return collection.child, origin
|
|
728
|
+
elseif collection.kind == 'slice' or collection.kind == 'array' or collection.kind == 'map' then
|
|
729
|
+
if ty.arity > 2 then return nil end
|
|
730
|
+
return ty.result == 1 and (collection.kind == 'map' and collection.key or { kind = 'builtin', name = 'int' })
|
|
731
|
+
or collection.child, origin
|
|
732
|
+
end
|
|
733
|
+
return nil
|
|
734
|
+
end
|
|
735
|
+
return ty, f
|
|
736
|
+
end
|
|
737
|
+
function C.expression_receiver(f, expression, path, depth)
|
|
738
|
+
local ty, origin = value_type(f, expression, path, depth)
|
|
739
|
+
if not ty then return false end
|
|
740
|
+
local pointer = ty.kind == 'pointer'
|
|
741
|
+
local id = resolve_type(origin, pointer and ty.child or ty)
|
|
742
|
+
return id and { id = id, pointer = pointer, addressable = false, type_expr = false } or false
|
|
743
|
+
end
|
|
744
|
+
function C.receiver(f, name, at, path, depth)
|
|
745
|
+
local entry = C.bound(f, name, at)
|
|
746
|
+
if entry == false then return false end
|
|
747
|
+
if not entry and shadowed(f, name, at) then return false end
|
|
748
|
+
local ty, addressable
|
|
749
|
+
if entry then
|
|
750
|
+
if entry.generic or not entry.type then return false end
|
|
751
|
+
ty, addressable = entry.type, true
|
|
752
|
+
if ty.builtin_new then
|
|
753
|
+
local _, shadowed = named(f, 'new', ty.at)
|
|
754
|
+
if shadowed then return false end
|
|
755
|
+
end
|
|
756
|
+
else ty = { kind = 'name', name = name, at = at } end
|
|
757
|
+
local origin
|
|
758
|
+
ty, origin = value_type(f, ty, path, depth)
|
|
759
|
+
if not ty then return false end
|
|
760
|
+
local pointer = ty.kind == 'pointer'
|
|
761
|
+
local id = resolve_type(origin, pointer and ty.child or ty)
|
|
762
|
+
if not id then
|
|
763
|
+
if not entry and modules.aliases(G, f, ctx)[name] then return nil end
|
|
764
|
+
return false
|
|
765
|
+
end
|
|
766
|
+
return { id = id, pointer = pointer, addressable = addressable or false, type_expr = not entry }
|
|
767
|
+
end
|
|
768
|
+
function C.members(receiver, requesting, name)
|
|
769
|
+
local pointer = receiver.pointer or receiver.addressable and G.nodes[receiver.id].kind ~= 'interface'
|
|
770
|
+
local methods, result = set(receiver.id, pointer, nil, nil, name and { [name] = true }), {}
|
|
771
|
+
for name, entry in pairs(methods.entries) do
|
|
772
|
+
local exported = name:match('^%u')
|
|
773
|
+
local visible = exported or name:sub(1, #peers(requesting).key + 1) == peers(requesting).key .. '\0'
|
|
774
|
+
if visible and (methods.valid or entry.depth == 0) then
|
|
775
|
+
if entry.field and not receiver.type_expr then
|
|
776
|
+
local id = entry.field
|
|
777
|
+
result[#result + 1] = { name = G.nodes[id].name, kind = 'field', node = G.nodes[id], file = G.node_file[id], id = id }
|
|
778
|
+
elseif not entry.blocked then
|
|
779
|
+
for _, id in ipairs(entry.ids or {}) do result[#result + 1] = { name = G.nodes[id].name,
|
|
780
|
+
kind = 'method', node = G.nodes[id], file = G.node_file[id], id = id } end
|
|
781
|
+
end
|
|
782
|
+
end
|
|
783
|
+
end
|
|
784
|
+
table.sort(result, function(a, b) return a.name < b.name or a.name == b.name and a.file < b.file end)
|
|
785
|
+
return result
|
|
786
|
+
end
|
|
787
|
+
function C.resolve(f, name, recv, evidence)
|
|
788
|
+
if evidence == 'go:?' then return {}, 'unsupported Go receiver chain' end
|
|
789
|
+
local expression_at = evidence and tonumber(evidence:match('^go:expr:(%d+)$'))
|
|
790
|
+
if expression_at then
|
|
791
|
+
local d = C.data(f)
|
|
792
|
+
local expression = d and d.receivers[expression_at]
|
|
793
|
+
local receiver = expression and C.expression_receiver(f, expression)
|
|
794
|
+
if not receiver then return {}, 'unknown Go receiver expression' end
|
|
795
|
+
local result = {}
|
|
796
|
+
for _, entry in ipairs(C.members(receiver, f, name)) do if entry.id then result[#result + 1] = entry.id end end
|
|
797
|
+
return result, 'Go call-result method set'
|
|
798
|
+
end
|
|
799
|
+
if not recv then
|
|
800
|
+
local at = evidence and tonumber(evidence:match('^go:call:(%d+)$'))
|
|
801
|
+
if at and C.bound(f, name, at) ~= nil then return {}, 'unresolved local Go callable' end
|
|
802
|
+
return nil
|
|
803
|
+
end
|
|
804
|
+
local at = evidence and tonumber(evidence:match('^go:recv:(%d+)$'))
|
|
805
|
+
if not at then
|
|
806
|
+
if modules.aliases(G, f, ctx)[recv] then return nil end
|
|
807
|
+
return {}, 'Go receiver has no lexical binding evidence'
|
|
808
|
+
end
|
|
809
|
+
local receiver = C.receiver(f, recv, at)
|
|
810
|
+
if receiver == false then return {}, 'unknown or shadowed Go receiver' end
|
|
811
|
+
if not receiver then return nil end
|
|
812
|
+
local result = {}
|
|
813
|
+
for _, entry in ipairs(C.members(receiver, f, name)) do if entry.name == name and entry.id then result[#result + 1] = entry.id end end
|
|
814
|
+
return result, 'Go method set'
|
|
815
|
+
end
|
|
816
|
+
local function satisfies(id, contract, pointer)
|
|
817
|
+
if G.nodes[id].kind == 'interface' then return false end
|
|
818
|
+
local wanted, count, filter = set(contract, false), 0, {}
|
|
819
|
+
for _, entry in pairs(wanted.entries) do
|
|
820
|
+
if entry.ids then filter[G.nodes[entry.ids[1]].name] = true end
|
|
821
|
+
end
|
|
822
|
+
local got = set(id, pointer, nil, nil, filter)
|
|
823
|
+
if not wanted.valid or got.invalid then return false end
|
|
824
|
+
for name, entry in pairs(wanted.entries) do
|
|
825
|
+
if entry.blocked or not entry.signature then return false end
|
|
826
|
+
count = count + 1
|
|
827
|
+
local actual = got.entries[name]
|
|
828
|
+
if not actual or actual.blocked or actual.signature ~= entry.signature then return false end
|
|
829
|
+
if not got.valid and actual.depth ~= 0 then return false end
|
|
830
|
+
end
|
|
831
|
+
return count > 0, got
|
|
832
|
+
end
|
|
833
|
+
C.satisfies = satisfies
|
|
834
|
+
function C.types(f, written)
|
|
835
|
+
local pointer = written:sub(1, 1) == '*'
|
|
836
|
+
local id = resolve_type(f, { kind = 'name', name = pointer and written:sub(2) or written })
|
|
837
|
+
return id and { id } or {}
|
|
838
|
+
end
|
|
839
|
+
local function implementers(contract)
|
|
840
|
+
local wanted, seed, shortest = set(contract, false), nil, math.huge
|
|
841
|
+
if not wanted.valid then return {} end
|
|
842
|
+
for _, entry in pairs(wanted.entries) do
|
|
843
|
+
if entry.blocked or not entry.ids or not entry.signature then return {} end
|
|
844
|
+
local list = G.by_name[G.nodes[entry.ids[1]].name] or {}
|
|
845
|
+
if #list < shortest then seed, shortest = list, #list end
|
|
846
|
+
end
|
|
847
|
+
if not seed then return {} end
|
|
848
|
+
-- Embedders need not declare the wanted method. Follow only indexed
|
|
849
|
+
-- embedded-field names, never a global type/interface Cartesian product.
|
|
850
|
+
local candidates, seen, queue = {}, {}, {}
|
|
851
|
+
local function add(id)
|
|
852
|
+
if not id or seen[id] then return end
|
|
853
|
+
seen[id] = true
|
|
854
|
+
if #queue >= M.MAX_CANDIDATES then limited(); return end
|
|
855
|
+
queue[#queue + 1] = id
|
|
856
|
+
end
|
|
857
|
+
for i, mid in ipairs(seed) do
|
|
858
|
+
if i > M.MAX_CANDIDATES then limited(); break end
|
|
859
|
+
if G.files[G.node_file[mid]].language == 'go' then add(C.owner(mid)) end
|
|
860
|
+
end
|
|
861
|
+
local i = 1
|
|
862
|
+
while i <= #queue do
|
|
863
|
+
if expired() then break end
|
|
864
|
+
local id = queue[i]; i = i + 1
|
|
865
|
+
control.check(); C.stats.candidates = C.stats.candidates + 1
|
|
866
|
+
if satisfies(id, contract, false) or satisfies(id, contract, true) then candidates[#candidates + 1] = id end
|
|
867
|
+
if #candidates >= M.MAX_RESULTS then limited(); break end
|
|
868
|
+
for index, field in ipairs(G.by_name[G.nodes[id].name] or {}) do
|
|
869
|
+
if index > M.MAX_CANDIDATES then limited(); break end
|
|
870
|
+
local n, f = G.nodes[field], G.node_file[field]
|
|
871
|
+
if G.files[f].language == 'go' and n.embedded and n.parent then
|
|
872
|
+
local parent = G.id_of[f][n.parent]
|
|
873
|
+
local d = info(parent)
|
|
874
|
+
if d then
|
|
875
|
+
for _, ty in ipairs(d.embeds) do
|
|
876
|
+
if resolve_type(f, ty.kind == 'pointer' and ty.child or ty) == id then add(parent) end
|
|
877
|
+
end
|
|
878
|
+
end
|
|
879
|
+
end
|
|
880
|
+
end
|
|
881
|
+
end
|
|
882
|
+
return candidates
|
|
883
|
+
end
|
|
884
|
+
function C.outgoing(id)
|
|
885
|
+
if outgoing[id] then return outgoing[id] end
|
|
886
|
+
local owner = G.nodes[id].kind == 'interface' and id or C.owner(id)
|
|
887
|
+
local result, seen = {}, {}
|
|
888
|
+
if owner and G.nodes[owner].kind == 'interface' then
|
|
889
|
+
for _, candidate in ipairs(implementers(owner)) do
|
|
890
|
+
if id == owner then result[#result + 1] = candidate
|
|
891
|
+
else
|
|
892
|
+
local key = method_key(G.node_file[id], G.nodes[id].name)
|
|
893
|
+
local matched, methods = satisfies(candidate, owner, false)
|
|
894
|
+
if not matched then matched, methods = satisfies(candidate, owner, true) end
|
|
895
|
+
local entry = matched and methods.entries[key]
|
|
896
|
+
for _, mid in ipairs(entry and entry.ids or {}) do
|
|
897
|
+
if mid ~= id and not G.nodes[mid].abstract and not seen[mid] then
|
|
898
|
+
seen[mid] = true; result[#result + 1] = mid; C.stats.links = C.stats.links + 1
|
|
899
|
+
if C.report then C.report:add({ source = id, target = mid, line = G.nodes[id].line,
|
|
900
|
+
kind = 'possible Go implementation (method set and exact signature)' }) end
|
|
901
|
+
end
|
|
902
|
+
end
|
|
903
|
+
end
|
|
904
|
+
end
|
|
905
|
+
end
|
|
906
|
+
outgoing[id] = result; return result
|
|
907
|
+
end
|
|
908
|
+
function C.incoming(id)
|
|
909
|
+
if incoming[id] then return incoming[id] end
|
|
910
|
+
local result, seen = {}, {}
|
|
911
|
+
for i, base in ipairs(G.by_name[G.nodes[id].name] or {}) do
|
|
912
|
+
if i > M.MAX_CANDIDATES then limited(); break end
|
|
913
|
+
if G.files[G.node_file[base]].language == 'go' and G.nodes[base].kind == 'method' then
|
|
914
|
+
local owner = C.owner(base)
|
|
915
|
+
if owner and G.nodes[owner].kind == 'interface' then
|
|
916
|
+
for _, mid in ipairs(C.outgoing(base)) do
|
|
917
|
+
if mid == id and not seen[base] then seen[base] = true; result[#result + 1] = base end
|
|
918
|
+
end
|
|
919
|
+
end
|
|
920
|
+
end
|
|
921
|
+
end
|
|
922
|
+
incoming[id] = result; return result
|
|
923
|
+
end
|
|
924
|
+
return C
|
|
925
|
+
end
|
|
926
|
+
|
|
927
|
+
function M.resolve(G, f, name, recv, evidence, ctx)
|
|
928
|
+
return M.context(G, ctx).resolve(f, name, recv, evidence)
|
|
929
|
+
end
|
|
930
|
+
|
|
931
|
+
return M
|