@codua/codeoutline-win32-x64 0.2.6 → 0.3.0

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.
Files changed (73) hide show
  1. package/README.md +205 -3
  2. package/README.zh-CN.md +40 -2
  3. package/bin/xnet.exe +0 -0
  4. package/build-info.json +1 -1
  5. package/package.json +1 -1
  6. package/scripts/codeoutline/affected.lua +145 -0
  7. package/scripts/codeoutline/automatic_events.lua +202 -0
  8. package/scripts/codeoutline/cache_repairs.lua +47 -0
  9. package/scripts/codeoutline/callers.lua +56 -16
  10. package/scripts/codeoutline/cargo_toml.lua +125 -0
  11. package/scripts/codeoutline/command.lua +26 -4
  12. package/scripts/codeoutline/common.lua +48 -7
  13. package/scripts/codeoutline/completion.lua +126 -22
  14. package/scripts/codeoutline/coverage.lua +63 -0
  15. package/scripts/codeoutline/cpp_bindings.lua +46 -11
  16. package/scripts/codeoutline/cpp_fragments.lua +143 -0
  17. package/scripts/codeoutline/cpp_implementations.lua +340 -0
  18. package/scripts/codeoutline/cpp_includes.lua +156 -0
  19. package/scripts/codeoutline/cpp_macros.lua +279 -0
  20. package/scripts/codeoutline/cpp_receivers.lua +342 -0
  21. package/scripts/codeoutline/cpp_types.lua +285 -29
  22. package/scripts/codeoutline/definition_candidates.lua +186 -0
  23. package/scripts/codeoutline/dispatch_aliases.lua +10 -5
  24. package/scripts/codeoutline/documents.lua +22 -9
  25. package/scripts/codeoutline/explore.lua +243 -122
  26. package/scripts/codeoutline/external_headers.lua +176 -0
  27. package/scripts/codeoutline/foreign.lua +23 -3
  28. package/scripts/codeoutline/function_values.lua +69 -0
  29. package/scripts/codeoutline/go_methods.lua +931 -0
  30. package/scripts/codeoutline/go_modules.lua +329 -0
  31. package/scripts/codeoutline/graph.lua +322 -67
  32. package/scripts/codeoutline/implementations.lua +403 -0
  33. package/scripts/codeoutline/index.lua +161 -28
  34. package/scripts/codeoutline/index_worker.lua +11 -4
  35. package/scripts/codeoutline/indirect.lua +4 -2
  36. package/scripts/codeoutline/js_evidence.lua +2 -1
  37. package/scripts/codeoutline/js_resolution.lua +40 -12
  38. package/scripts/codeoutline/js_workspace.lua +325 -0
  39. package/scripts/codeoutline/lang/c.lua +15 -2
  40. package/scripts/codeoutline/lang/cpp.lua +32 -18
  41. package/scripts/codeoutline/lang/csharp.lua +1 -1
  42. package/scripts/codeoutline/lang/js.lua +56 -20
  43. package/scripts/codeoutline/lang/rust.lua +9 -1
  44. package/scripts/codeoutline/language_detect.lua +100 -0
  45. package/scripts/codeoutline/lsp.lua +173 -23
  46. package/scripts/codeoutline/lua_bindings.lua +151 -47
  47. package/scripts/codeoutline/lua_callback_loops.lua +210 -0
  48. package/scripts/codeoutline/main.lua +32 -7
  49. package/scripts/codeoutline/mcp.lua +52 -9
  50. package/scripts/codeoutline/navigation.lua +351 -65
  51. package/scripts/codeoutline/on_demand.lua +93 -0
  52. package/scripts/codeoutline/ordinary_bindings.lua +210 -34
  53. package/scripts/codeoutline/parse.lua +18 -2
  54. package/scripts/codeoutline/parse_pool.lua +1 -1
  55. package/scripts/codeoutline/parse_worker.lua +1 -1
  56. package/scripts/codeoutline/python_bindings.lua +2 -2
  57. package/scripts/codeoutline/query_filters.lua +65 -0
  58. package/scripts/codeoutline/query_report.lua +56 -19
  59. package/scripts/codeoutline/resolution.lua +23 -0
  60. package/scripts/codeoutline/rust_packages.lua +203 -0
  61. package/scripts/codeoutline/rust_resolution.lua +301 -0
  62. package/scripts/codeoutline/search_ranking.lua +70 -0
  63. package/scripts/codeoutline/service.lua +47 -7
  64. package/scripts/codeoutline/shared_events.lua +369 -0
  65. package/scripts/codeoutline/static_dispatch.lua +46 -11
  66. package/scripts/codeoutline/text.lua +3 -2
  67. package/scripts/codeoutline/ts_implementations.lua +81 -0
  68. package/scripts/codeoutline/type_bases.lua +61 -0
  69. package/scripts/codeoutline/type_hierarchy.lua +324 -0
  70. package/scripts/codeoutline/type_references.lua +291 -0
  71. package/scripts/codeoutline/type_uses.lua +260 -0
  72. package/scripts/codeoutline/update_sequence.lua +1 -1
  73. package/scripts/codeoutline/version.lua +1 -1
@@ -0,0 +1,279 @@
1
+ -- Query-owned project macro visibility. No expansion or compiler execution.
2
+ local M = { MAX_FILES = 128, MAX_BYTES = 8 * 1024 * 1024, MAX_SUMMARY_BYTES = 1024 * 1024, MAX_EVENTS = 20000,
3
+ MAX_DIRECTIVES = 4096, MAX_DEPTH = 32, MAX_RESULTS = 4096 }
4
+ local control = require('codeoutline.control')
5
+ local scanners = {}
6
+ local UNKNOWN = {}
7
+ local function trim(s) return (s:gsub('^%s+', ''):gsub('%s+$', '')) end
8
+ local function conjunction(a, b) if a == false or b == false then return false end; if a == true and b == true then return true end end
9
+ local function disjunction(a, b) if a == true or b == true then return true end; if a == false and b == false then return false end end
10
+ local function negate(a) if a ~= nil then return not a end end
11
+ local function defined(env, name)
12
+ local value = env[name]
13
+ if value == false then return false end
14
+ if value and value ~= UNKNOWN then return true end
15
+ end
16
+
17
+ local function condition(expr, env, depth)
18
+ depth = (depth or 0) + 1
19
+ if depth > 16 then return nil end
20
+ expr = trim(expr)
21
+ local level, split_or, split_and = 0
22
+ for i = 1, #expr do
23
+ local c = expr:sub(i, i)
24
+ if c == '(' then level = level + 1 elseif c == ')' then level = level - 1 end
25
+ if level == 0 then
26
+ local op = expr:sub(i, i + 1)
27
+ if op == '||' then split_or = i elseif op == '&&' then split_and = i end
28
+ end
29
+ if level < 0 then return nil end
30
+ end
31
+ if level ~= 0 then return nil end
32
+ local at = split_or or split_and
33
+ if at then
34
+ local a, b = condition(expr:sub(1, at - 1), env, depth), condition(expr:sub(at + 2), env, depth)
35
+ if split_or then return disjunction(a, b) end
36
+ return conjunction(a, b)
37
+ end
38
+ if expr:sub(1, 1) == '(' and expr:sub(-1) == ')' then return condition(expr:sub(2, -2), env, depth) end
39
+ if expr:sub(1, 1) == '!' then return negate(condition(expr:sub(2), env, depth)) end
40
+ local name = expr:match('^defined%s*%(%s*([%a_][%w_]*)%s*%)$') or expr:match('^defined%s+([%a_][%w_]*)$')
41
+ if name then return defined(env, name) end
42
+ local number = tonumber((expr:gsub('[uUlL]+$', '')))
43
+ if number then return number ~= 0 end
44
+ if expr:match('^[%a_][%w_]*$') then
45
+ local value = env[expr]
46
+ if value == false then return false end
47
+ if value and value ~= UNKNOWN and value.number then return value.number ~= 0 end
48
+ end
49
+ end
50
+
51
+ local function scanner_for(language)
52
+ local scanner = scanners[language]
53
+ if not scanner then
54
+ local config = {}
55
+ for k, v in pairs(require('codeoutline.lang.' .. language).config) do config[k] = v end
56
+ config.pp_first_branch = false
57
+ scanner = require('codeoutline.xscan').lang(config); scanners[language] = scanner
58
+ end
59
+ return scanner
60
+ end
61
+
62
+ function M.collect(r, T, all_branches)
63
+ if r.language ~= 'c' and r.language ~= 'cpp' then return end
64
+ local result = {}; r.cpp_macros = result
65
+ if not T.src:find('#', 1, true) then return end
66
+ local scanner = scanner_for(r.language)
67
+ local source = T.src
68
+ if r.language == 'cpp' and source:find('R"', 1, true) then
69
+ local starts = {}
70
+ for i = 1, T.n do
71
+ if T.k[i] == 'str' then
72
+ local prefix = T:text(i):match('^([LuU8]*R)"')
73
+ if prefix then starts[T.s[i] + #prefix - 1] = true end
74
+ elseif T.k[i] == 'id' and T:text(i) == 'u8R' and T.s[i + 1] == T.e[i] + 1 and T:text(i + 1):sub(1, 1) == '"' then
75
+ starts[T.e[i]] = true -- scanners accepting at most two prefix characters
76
+ end
77
+ end
78
+ local parts, at = {}, 1
79
+ while true do
80
+ local first, last, delimiter = source:find('R"([^%s%(%)"\\]*)%(', at)
81
+ if not first then break end
82
+ if not starts[first] or #delimiter > 16 then at = last + 1
83
+ else
84
+ local close = source:find(')' .. delimiter .. '"', last + 1, true)
85
+ if not close then result.incomplete = true; return end
86
+ parts[#parts + 1] = { first = first, last = close + #delimiter + 1 }
87
+ at = close + #delimiter + 2
88
+ end
89
+ end
90
+ local pieces, previous = {}, 1
91
+ for _, span in ipairs(parts) do
92
+ pieces[#pieces + 1] = source:sub(previous, span.first - 1)
93
+ pieces[#pieces + 1] = source:sub(span.first, span.last):gsub('[^\r\n]', ' ')
94
+ previous = span.last + 1
95
+ end
96
+ pieces[#pieces + 1] = source:sub(previous)
97
+ source = table.concat(pieces)
98
+ end
99
+ local full = source == T.src and (all_branches or not source:find('#%s*if')) and T or scanner:tokenize(source)
100
+ local nodes = {}
101
+ for i, n in ipairs(r.nodes) do
102
+ if n.kind == 'macro' then nodes[n.line .. '\0' .. n.name] = i end
103
+ end
104
+ for i = 1, full.n do
105
+ if i % 4096 == 0 then control.check() end
106
+ if full.k[i] == 'dir' then
107
+ if #result >= M.MAX_DIRECTIVES then result.incomplete = true; break end
108
+ local raw = full:text(i):gsub('\\\r?\n', ' ')
109
+ local kind, rest = raw:match('^#%s*(%a+)%s*(.*)$')
110
+ local e = { kind = kind, line = full:line(i) }
111
+ if kind == 'include' then
112
+ e.path = rest:match('^"([^"]+)"') or rest:match('^<([^>]+)>')
113
+ e.system = rest:sub(1, 1) == '<'
114
+ else
115
+ rest = trim(rest:gsub('/%*.-%*/', ' '):gsub('//[^\n]*', ''))
116
+ e.name = rest:match('^([%a_][%w_]*)')
117
+ if kind == 'define' and e.name then
118
+ local body = rest:sub(#e.name + 1)
119
+ e.callable = body:sub(1, 1) == '('
120
+ e.number = not e.callable and tonumber((trim(body):gsub('[uUlL]+$', ''))) or nil
121
+ e.node = nodes[e.line .. '\0' .. e.name]
122
+ -- Recursive wrapper expansion needs expansion semantics.
123
+ e.recursive = e.callable and body:find('%f[%w_]' .. e.name .. '%f[^%w_]') ~= nil
124
+ elseif kind == 'if' or kind == 'elif' then e.expression = rest
125
+ elseif kind == 'pragma' then e.once = trim(rest) == 'once' end
126
+ end
127
+ result[#result + 1] = e
128
+ end
129
+ end
130
+ if result[1] and result[2] and result[1].kind == 'ifndef' and result[2].kind == 'define'
131
+ and result[1].name == result[2].name then result.guard = result[1].name end
132
+ end
133
+
134
+ function M.summary(rec, source)
135
+ if source:sub(1, 3) == '\239\187\191' then source = source:sub(4) end
136
+ local r = { language = rec.language, nodes = rec.nodes }
137
+ M.collect(r, scanner_for(rec.language):tokenize(source), true)
138
+ return r.cpp_macros
139
+ end
140
+
141
+ function M.context(G, ctx)
142
+ if ctx.cpp_macro_context then return ctx.cpp_macro_context end
143
+ local C = { stats = { files = 0, bytes = 0, summary_bytes = 0, events = 0, checks = 0 }, incomplete = false }
144
+ ctx.cpp_macro_context = C
145
+ local summaries, results, result_count = {}, {}, 0
146
+ local function limited() C.incomplete = true; return nil end
147
+ local function data(f)
148
+ if summaries[f] then return summaries[f] end
149
+ local rec = G.files[f]
150
+ if C.stats.files >= M.MAX_FILES or C.stats.bytes + (rec.size or 0) > M.MAX_BYTES then return limited() end
151
+ local value = ctx.ordinary.macros(f)
152
+ if not value or value.incomplete then return limited() end
153
+ local weight = #value * 192
154
+ for _, e in ipairs(value) do weight = weight + #(e.name or '') + #(e.expression or '') + #(e.path or '') end
155
+ if C.stats.summary_bytes + weight > M.MAX_SUMMARY_BYTES then return limited() end
156
+ C.stats.summary_bytes = C.stats.summary_bytes + weight
157
+ summaries[f] = value; C.stats.files = C.stats.files + 1; C.stats.bytes = C.stats.bytes + (rec.size or 0)
158
+ return value
159
+ end
160
+ -- Only definition navigation opts into ambiguous include alternatives;
161
+ -- execution queries keep their own strict, separately cached result.
162
+ function C.resolve(f, name, line, declarations)
163
+ local key = f .. '\0' .. name .. '\0' .. line .. (declarations and '\0declarations' or '')
164
+ if results[key] then return results[key][1], results[key][2], results[key][3], results[key][4] end
165
+ if result_count >= M.MAX_RESULTS then limited(); return {}, true, nil, 'limited' end
166
+ C.stats.checks = C.stats.checks + 1
167
+ local env, once, active_files, definitions = {}, {}, {}, {}
168
+ local failed = false
169
+ local function walk(file, cutoff, parent, depth)
170
+ control.check()
171
+ if depth > M.MAX_DEPTH then limited(); failed = true; return end
172
+ if active_files[file] or once[file] then return end
173
+ local events = data(file)
174
+ if not events then failed = true; return end
175
+ -- Conventional include guards are initially absent unless a source
176
+ -- directive established their state. Arbitrary build flags stay unknown.
177
+ if events.guard and env[events.guard] == nil and parent == true then env[events.guard] = false end
178
+ active_files[file] = true
179
+ local stack, active = {}, parent
180
+ for _, e in ipairs(events) do
181
+ if e.line > cutoff then break end
182
+ C.stats.events = C.stats.events + 1
183
+ if C.stats.events % 128 == 0 then control.check() end
184
+ if C.stats.events > M.MAX_EVENTS then limited(); failed = true; break end
185
+ local kind = e.kind
186
+ if kind == 'if' or kind == 'ifdef' or kind == 'ifndef' then
187
+ local test
188
+ if kind == 'if' then test = condition(e.expression, env)
189
+ else test = defined(env, e.name); if kind == 'ifndef' then test = negate(test) end end
190
+ stack[#stack + 1] = { parent = active, taken = test }
191
+ active = conjunction(active, test)
192
+ elseif kind == 'elif' or kind == 'else' then
193
+ local frame = stack[#stack]
194
+ if not frame then failed = true; break end
195
+ local test = kind == 'else' and true or condition(e.expression, env)
196
+ active = conjunction(frame.parent, conjunction(negate(frame.taken), test))
197
+ frame.taken = disjunction(frame.taken, test)
198
+ elseif kind == 'endif' then
199
+ local frame = table.remove(stack)
200
+ if not frame then failed = true; break end
201
+ active = frame.parent
202
+ elseif active ~= false then
203
+ if (kind == 'define' or kind == 'undef') and e.name then
204
+ if e.name == name then
205
+ if active == true then definitions = {} end
206
+ if kind == 'define' and e.node then definitions[G.id_of[file][e.node]] = true end
207
+ end
208
+ if active == nil then env[e.name] = UNKNOWN
209
+ elseif kind == 'undef' then env[e.name] = false
210
+ else env[e.name] = { callable = e.callable, number = e.number, node = e.node,
211
+ file = file, recursive = e.recursive } end
212
+ elseif kind == 'pragma' and e.once and active == true then once[file] = true
213
+ elseif kind == 'include' then
214
+ if not e.path then env[name] = UNKNOWN
215
+ else
216
+ local targets = require('codeoutline.cpp_includes').resolve(G, file, e, ctx)
217
+ if #targets == 1 then walk(targets[1], math.huge, active, depth + 1)
218
+ elseif #targets > 1 then
219
+ if declarations then
220
+ -- F12 may show alternatives without choosing a
221
+ -- build's include path. Evaluate each header
222
+ -- independently: sibling include guards, undef
223
+ -- and pragma once must not affect each other.
224
+ local original_env, original_once, original_definitions = env, once, definitions
225
+ local candidates = {}
226
+ local function copy(values)
227
+ local result = {}; for k, v in pairs(values) do result[k] = v end; return result
228
+ end
229
+ for _, target in ipairs(targets) do
230
+ env, once, definitions = copy(original_env), copy(original_once), copy(original_definitions)
231
+ walk(target, math.huge, active, depth + 1)
232
+ for id in pairs(definitions) do candidates[id] = true end
233
+ if failed then break end
234
+ end
235
+ env, once, definitions = original_env, original_once, candidates
236
+ end
237
+ env[name] = UNKNOWN
238
+ end
239
+ -- External/unindexed headers are not interpreted.
240
+ end
241
+ end
242
+ end
243
+ end
244
+ active_files[file] = nil
245
+ end
246
+ walk(f, line, true, 1)
247
+ local value, ids, handled = env[name], {}, true
248
+ if not failed and (value == false or value == nil) then handled = false
249
+ elseif not failed and value ~= UNKNOWN and value and value.callable and value.node and not value.recursive then
250
+ ids[1] = G.id_of[value.file][value.node]
251
+ end
252
+ -- Definition navigation may inspect the declarations reached on an
253
+ -- unknown branch without asserting an execution edge. An unconditional
254
+ -- redefine/undef clears older candidates; directives after the call and
255
+ -- known-false branches never contribute. Graph resolution stays strict.
256
+ local sites = {}
257
+ if not failed then for id in pairs(definitions) do sites[#sites + 1] = id end end
258
+ table.sort(sites)
259
+ local state = failed and 'limited' or value == nil and 'unseen' or value == false and 'undefined'
260
+ or value == UNKNOWN and 'uncertain' or 'defined'
261
+ result_count = result_count + 1; results[key] = { ids, handled, sites, state }
262
+ return ids, handled, sites, state
263
+ end
264
+ return C
265
+ end
266
+
267
+ function M.resolve(G, f, name, line, ctx, declarations)
268
+ if not line or not ctx or (G.files[f].language ~= 'c' and G.files[f].language ~= 'cpp') then return nil end
269
+ local candidate = false
270
+ for i, id in ipairs(G.by_name[name] or {}) do
271
+ if i % 128 == 0 then control.check() end
272
+ if G.nodes[id].kind == 'macro' then candidate = true; break end
273
+ end
274
+ if not candidate then return nil end
275
+ local ids, handled, definitions, state = M.context(G, ctx).resolve(f, name, line, declarations)
276
+ return handled and ids or false, definitions, state
277
+ end
278
+
279
+ return M
@@ -0,0 +1,342 @@
1
+ -- Query-owned C/C++ call-result receivers. Never infer a type from a member name.
2
+ local control = require('codeoutline.control')
3
+ local M = { MAX_FILES = 16, MAX_BYTES = 8388608, MAX_DEPTH = 12, MAX_CANDIDATES = 256, MAX_TIME_MS = 400 }
4
+ local family = { c = true, cpp = true }
5
+ local aggregates = { class = true, struct = true, union = true }
6
+ local callable = { ['function'] = true, method = true, prototype = true }
7
+ local modifiers = { static = true, inline = true, virtual = true, constexpr = true, extern = true,
8
+ friend = true, __inline = true, __inline__ = true, register = true, typedef = true }
9
+ local cv = { const = true, volatile = true, restrict = true, __restrict = true }
10
+
11
+ local function declared(T, a, b)
12
+ while modifiers[T:text(a)] do a = a + 1 end
13
+ local names, pointer, reference, tag, is_const = {}, 0, false, nil, false
14
+ for i = a, b do
15
+ local s = T:text(i)
16
+ if cv[s] then if s == 'const' and pointer == 0 then is_const = true end
17
+ elseif aggregates[s] then if #names > 0 then return nil end; tag = s
18
+ elseif s == '*' then pointer = pointer + 1
19
+ elseif s == '&' or s == '&&' then reference = true
20
+ elseif s == '::' and pointer == 0 and not reference then names[#names + 1] = s
21
+ elseif T.k[i] == 'id' and pointer == 0 and not reference then
22
+ if #names > 0 and names[#names] ~= '::' then return nil end
23
+ names[#names + 1] = s
24
+ else return nil end
25
+ end
26
+ local name = table.concat(names)
27
+ if name == '' or name == 'auto' or name == 'decltype' or name:sub(-2) == '::' then return nil end
28
+ return { name = name, pointer = pointer, reference = reference, const = is_const, tag = tag, at = T.s[a] - 1 }
29
+ end
30
+
31
+ local expression
32
+ expression = function(T, last, depth)
33
+ depth = (depth or 0) + 1
34
+ if depth > M.MAX_DEPTH then return nil end
35
+ if T.k[last] == 'id' then
36
+ local op = T:text(last - 1)
37
+ if op == '.' or op == '->' then
38
+ local receiver, first, size, call = expression(T, last - 2, depth)
39
+ if receiver then return { kind = 'field', receiver = receiver, name = T:text(last), op = op }, first, size + 1, call end
40
+ return nil
41
+ end
42
+ local first = last
43
+ while T:text(first - 1) == '::' and T.k[first - 2] == 'id' do first = first - 2 end
44
+ if T:text(first - 1) == '::' then first = first - 1 end
45
+ return { kind = 'name', name = T.src:sub(T.s[first], T.e[last]):gsub('%s+', ''),
46
+ at = T.s[last] - 1 }, first, 1, false
47
+ end
48
+ if T:text(last) ~= ')' or not T.m[last] then return nil end
49
+ local open = T.m[last]
50
+ if T.k[open - 1] == 'id' then
51
+ local target, first, size = expression(T, open - 1, depth)
52
+ if target then return { kind = 'call', target = target }, first, size + 1, true end
53
+ else
54
+ local previous = T:text(open - 1)
55
+ if previous == '>' or previous == '>>' or previous == ']' or previous == ')' then return nil end
56
+ local value, first, size, call = expression(T, last - 1, depth)
57
+ if first == open + 1 then return value, open, size, call end
58
+ end
59
+ end
60
+ M.expression = expression
61
+
62
+ function M.evidence(T, t)
63
+ local op = T:text(t - 1)
64
+ if op ~= '.' and op ~= '->' then return nil end
65
+ local e, _, _, call = expression(T, t - 2)
66
+ if call or not e and (T:text(t - 2) == ')' or T:text(t - 2) == ']') then return 'cxx:expr:' .. (T.s[t] - 1) end
67
+ end
68
+
69
+ function M.collect(r, T)
70
+ if not family[r.language] then return end
71
+ local d = { nodes = {}, expressions = {}, bindings = {}, calls = {}, weight = 0 }
72
+ r.cpp_receivers = d
73
+ local wanted = {}
74
+ local function roots(e)
75
+ if e.kind == 'name' then wanted[e.name] = true
76
+ elseif e.target then roots(e.target)
77
+ elseif e.receiver then roots(e.receiver) end
78
+ end
79
+ for t = 1, T.n do
80
+ if t % 4096 == 0 then control.check() end
81
+ local op = T:text(t - 1)
82
+ if T.k[t] == 'id' and (op == '.' or op == '->') then
83
+ local e, _, size, call = expression(T, t - 2)
84
+ if call or not e and (T:text(t - 2) == ')' or T:text(t - 2) == ']') then
85
+ local at = T.s[t] - 1
86
+ d.expressions[at] = { receiver = e or false, op = op }
87
+ d.weight = d.weight + (size or 1) * 192
88
+ if e then roots(e) end
89
+ end
90
+ end
91
+ end
92
+ local function prefix(ni)
93
+ local a = ni - 1
94
+ while a > 0 do
95
+ local s = T:text(a)
96
+ if T.k[a] ~= 'id' and T.k[a] ~= 'kw' and s ~= '*' and s ~= '&' and s ~= '&&' and s ~= '::' then break end
97
+ a = a - 1
98
+ end
99
+ return a + 1
100
+ end
101
+ local functions = {}
102
+ for id, n in ipairs(r.nodes) do
103
+ if id % 128 == 0 then control.check() end
104
+ local ni, a, b = r.cpp_declaration_tokens and r.cpp_declaration_tokens[id], n.ti, nil
105
+ local const_method = false
106
+ if callable[n.kind] and ni then
107
+ functions[#functions + 1] = n
108
+ local first = ni
109
+ while T:text(first - 1) == '::' and T.k[first - 2] == 'id' do first = first - 2 end
110
+ if T:text(first - 1) == '::' then first = first - 1 end
111
+ b = first - 1
112
+ local open = ni + 1
113
+ if T:text(open) == ')' and T.m[open] == ni - 1 then open, b = ni + 2, ni - 2 end
114
+ if T:text(open) == '(' and T.m[open] then
115
+ local after = T.m[open] + 1
116
+ const_method = T:text(after) == 'const'
117
+ while cv[T:text(after)] or T:text(after) == 'noexcept' do
118
+ after = after + 1
119
+ if T:text(after) == '(' and T.m[after] then after = T.m[after] + 1 end
120
+ end
121
+ if T:text(after) == '->' then
122
+ a, b = after + 1, after + 1
123
+ while b <= n.tj and T:text(b) ~= '{' and T:text(b) ~= ';' and T:text(b) ~= '=' do b = b + 1 end
124
+ b = b - 1
125
+ end
126
+ end
127
+ elseif n.kind == 'field' and ni then
128
+ a, b = prefix(ni), ni - 1
129
+ if T:text(ni + 1) == '[' or T:text(ni + 1) == ')' then b = a - 1 end
130
+ elseif n.kind == 'typedef' and ni then
131
+ if T:text(ni + 1) == '=' then a, b = ni + 2, n.tj - 1
132
+ elseif T:text(n.ti) == 'typedef' then a, b = n.ti + 1, ni - 1 end
133
+ end
134
+ d.nodes[id] = { type = b and declared(T, a, b) or false, const_method = const_method }
135
+ end
136
+ local scopes, stack = {}, {}
137
+ for t = 1, T.n do
138
+ if T:text(t) == '{' and T.m[t] then stack[#stack + 1] = t end
139
+ scopes[t] = stack[#stack] or 0
140
+ if stack[#stack] and T.m[stack[#stack]] == t then stack[#stack] = nil end
141
+ end
142
+ for t = 1, T.n do
143
+ if wanted[T:text(t)] then
144
+ local after, a = T:text(t + 1), prefix(t)
145
+ local pointer_callable = T:text(t - 1) == '*' and T:text(t - 2) == '('
146
+ local ty = declared(T, a, t - 1)
147
+ if (ty or pointer_callable or T:text(a) == 'auto' or T:text(t - 1) == '>')
148
+ and (after == ')' or after == ',' or after == ';' or after == '=' or after == '[') then
149
+ local scope = scopes[t]
150
+ local first, last = scope > 0 and T.s[scope] - 1 or 0, scope > 0 and T.e[T.m[scope]] or #T.src
151
+ for _, fn in ipairs(functions) do
152
+ if fn.ti < t and t < fn.tj and T.s[fn.ti] - 1 > first then first, last = T.s[fn.ti] - 1, T.e[fn.tj] end
153
+ end
154
+ d.bindings[#d.bindings + 1] = { name = T:text(t), type = ty or false, at = T.s[t] - 1, first = first, last = last }
155
+ end
156
+ end
157
+ end
158
+ d.evidence = {}
159
+ for index, ref in ipairs(r.refs) do
160
+ local at = T.s[ref.tok] - 1
161
+ if ref.kind == 'call' then d.calls[at] = ref.binding == 'cpp:?' or false end
162
+ if d.expressions[at] then ref.binding = 'cxx:expr:' .. at; d.evidence[index] = ref.binding; r.has_bindings = true end
163
+ end
164
+ d.weight = d.weight + #r.nodes * 160 + #d.bindings * 192
165
+ r.cpp_declaration_tokens = nil
166
+ end
167
+
168
+ function M.context(G, ctx)
169
+ ctx = ctx or {}
170
+ if ctx.cpp_receivers then return ctx.cpp_receivers end
171
+ local C = { stats = { files = 0, bytes = 0, candidates = 0 }, incomplete = false }
172
+ ctx.cpp_receivers = C
173
+ local deadline, records, visibility = xtimer.now_ms() + M.MAX_TIME_MS, {}, {}
174
+ local function limited() C.incomplete, ctx.cpp_receivers_incomplete = true, true end
175
+ local function expired()
176
+ control.check()
177
+ if xtimer.now_ms() > deadline then limited(); return true end
178
+ return false
179
+ end
180
+ function C.data(f)
181
+ if records[f] ~= nil then return records[f] or nil end
182
+ local rec = G.files[f]
183
+ if not ctx.ordinary or not ctx.ordinary.available(f) then records[f] = false; return nil end
184
+ local size = ctx.ordinary.source_size(f) or rec.size or #rec.nodes * 128
185
+ if expired() or C.stats.files >= M.MAX_FILES or C.stats.bytes + size > M.MAX_BYTES then
186
+ limited(); records[f] = false; return nil
187
+ end
188
+ C.stats.files, C.stats.bytes = C.stats.files + 1, C.stats.bytes + size
189
+ records[f] = ctx.ordinary.data(f).cpp_receivers or false
190
+ return records[f] or nil
191
+ end
192
+ local function active(id)
193
+ local f = G.node_file[id]; local ids = f and G.id_of[f]
194
+ return ids and ids[1] and ids[1] <= id and id <= ids[#ids]
195
+ end
196
+ local function info(id)
197
+ if not active(id) then return nil end
198
+ local f = G.node_file[id]; local d = C.data(f)
199
+ return d and d.nodes[id - G.id_of[f][1] + 1]
200
+ end
201
+ local function visible(f, g)
202
+ if f == g then return true end
203
+ if not visibility[f] then visibility[f] = require('codeoutline.graph').visible_dependencies(G, f, ctx) end
204
+ return visibility[f][g]
205
+ end
206
+ local function bound(f, name, at)
207
+ local d, best = C.data(f), nil
208
+ for _, b in ipairs(d and d.bindings or {}) do
209
+ if b.name == name and b.at < at and b.first <= at and at <= b.last
210
+ and (not best or b.first > best.first or b.first == best.first and b.at > best.at) then best = b end
211
+ end
212
+ return best
213
+ end
214
+ local function scope(id)
215
+ return id and G.nodes[id].qualified:match('^(.*)::[^:]+$') or nil
216
+ end
217
+ local type_of
218
+ type_of = function(f, ty, owner, seen, depth)
219
+ depth, seen = (depth or 0) + 1, seen or {}
220
+ if not ty or depth > M.MAX_DEPTH or expired() then return nil end
221
+ local ids
222
+ if G.files[f].language == 'cpp' then
223
+ -- Decorated aliases must retain indirection. The namespace resolver
224
+ -- intentionally normalizes it away, so leave those aliases unknown.
225
+ for i, alias in ipairs(G.by_name[ty.name:match('([^:]+)$')] or {}) do
226
+ if i > M.MAX_CANDIDATES then limited(); return nil end
227
+ if active(alias) and G.nodes[alias].kind == 'typedef' and visible(f, G.node_file[alias]) then
228
+ for _, a in ipairs(require('codeoutline.cpp_types').aliases(G, G.node_file[alias], ctx)) do
229
+ if a.name == G.nodes[alias].name and a.decorated then return nil end
230
+ end
231
+ end
232
+ end
233
+ ids = require('codeoutline.cpp_types').resolve(G, f, ty.name, { context = ctx, scope = owner, at = ty.at })
234
+ else
235
+ ids = {}
236
+ for i, id in ipairs(G.by_name[ty.name] or {}) do
237
+ if i > M.MAX_CANDIDATES then limited(); return nil end
238
+ local n, g = G.nodes[id], G.node_file[id]
239
+ if active(id) and family[G.files[g].language] and visible(f, g) and not n.parent then
240
+ if aggregates[n.kind] then ids[#ids + 1] = id
241
+ elseif n.kind == 'typedef' and not ty.tag then
242
+ if seen[id] then return nil end
243
+ local d = info(id); seen[id] = true
244
+ local value = d and type_of(g, d.type, nil, seen, depth)
245
+ seen[id] = nil
246
+ if not value then return nil end
247
+ return { id = value.id, pointer = value.pointer + ty.pointer, const = value.const or ty.const }
248
+ end
249
+ end
250
+ end
251
+ end
252
+ if #ids ~= 1 then return nil end
253
+ return { id = ids[1], pointer = ty.pointer, const = ty.const }
254
+ end
255
+ local function members(receiver, name, op, declarations)
256
+ if not receiver or (op == '->' and receiver.pointer ~= 1 or op == '.' and receiver.pointer ~= 0) then return {} end
257
+ local id, result = receiver.id, {}
258
+ if G.files[G.node_file[id]].language == 'cpp' then
259
+ local candidates
260
+ if declarations then
261
+ local known
262
+ candidates, known = require('codeoutline.cpp_types').resolve(G, G.node_file[id], '::' .. G.nodes[id].qualified .. '::' .. name,
263
+ { context = ctx, accept = { ['function'] = true, method = true, prototype = true, field = true } })
264
+ if known and #candidates == 0 then return {} end
265
+ end
266
+ if not candidates or #candidates == 0 then
267
+ candidates = require('codeoutline.cpp_bindings').resolve(G, G.node_file[id], nil, name,
268
+ 'cpp:type:::' .. G.nodes[id].qualified, nil, ctx) or {}
269
+ end
270
+ for _, mid in ipairs(candidates) do
271
+ local d = receiver.const and callable[G.nodes[mid].kind] and info(mid)
272
+ if not receiver.const or not callable[G.nodes[mid].kind] or d and d.const_method then result[#result + 1] = mid end
273
+ end
274
+ return result
275
+ end
276
+ for i, mid in ipairs(G.by_name[name] or {}) do
277
+ if i > M.MAX_CANDIDATES then limited(); break end
278
+ local n, g = G.nodes[mid], G.node_file[mid]
279
+ if active(mid) and g == G.node_file[id] and n.parent and G.id_of[g][n.parent] == id then result[#result + 1] = mid end
280
+ end
281
+ return result
282
+ end
283
+ C.members = members
284
+ local value
285
+ value = function(f, e, owner, depth)
286
+ depth = (depth or 0) + 1
287
+ if not e or depth > M.MAX_DEPTH or expired() then return nil end
288
+ if e.kind == 'name' then
289
+ local b = bound(f, e.name, e.at)
290
+ return b and type_of(f, b.type, scope(owner)) or nil
291
+ end
292
+ local candidates
293
+ local parent
294
+ if e.kind == 'field' then
295
+ parent = value(f, e.receiver, owner, depth)
296
+ candidates = members(parent, e.name, e.op, true)
297
+ elseif e.kind == 'call' then
298
+ local target = e.target
299
+ if target.kind == 'field' then
300
+ candidates = members(value(f, target.receiver, owner, depth), target.name, target.op, true)
301
+ elseif target.kind == 'name' then
302
+ local d = C.data(f)
303
+ if not d or d.calls[target.at] or bound(f, target.name, target.at) then return nil end
304
+ if G.files[f].language == 'cpp' then
305
+ candidates = require('codeoutline.cpp_types').resolve(G, f, target.name,
306
+ { context = ctx, scope = scope(owner), at = target.at, accept = callable })
307
+ else
308
+ candidates = {}
309
+ for i, id in ipairs(G.by_name[target.name] or {}) do
310
+ if i > M.MAX_CANDIDATES then limited(); return nil end
311
+ local n, g = G.nodes[id], G.node_file[id]
312
+ if active(id) and callable[n.kind] and not n.parent and visible(f, g)
313
+ and not (n.static and f ~= g) then candidates[#candidates + 1] = id end
314
+ end
315
+ end
316
+ end
317
+ end
318
+ local agreed
319
+ for i, id in ipairs(candidates or {}) do
320
+ if i > M.MAX_CANDIDATES then limited(); return nil end
321
+ C.stats.candidates = C.stats.candidates + 1
322
+ if e.kind == 'call' and not callable[G.nodes[id].kind] or e.kind == 'field' and G.nodes[id].kind ~= 'field' then return nil end
323
+ local d = info(id)
324
+ local actual = d and type_of(G.node_file[id], d.type, scope(id))
325
+ if actual and parent and parent.const and actual.pointer == 0 then actual.const = true end
326
+ if not actual or agreed and (agreed.id ~= actual.id or agreed.pointer ~= actual.pointer or agreed.const ~= actual.const) then return nil end
327
+ agreed = actual
328
+ end
329
+ return agreed
330
+ end
331
+ C.receiver = value
332
+ function C.resolve(f, src, name, evidence)
333
+ local at = evidence and tonumber(evidence:match('^cxx:expr:(%d+)$'))
334
+ if not at then return nil end
335
+ local d = C.data(f); local site = d and d.expressions[at]
336
+ if not site then return {}, 'missing C/C++ receiver evidence' end
337
+ return members(value(f, site.receiver, src), name, site.op), 'C/C++ declared call-result receiver'
338
+ end
339
+ return C
340
+ end
341
+
342
+ return M