@codua/codeoutline-win32-x64 0.2.4 → 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.
Files changed (77) 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 +65 -9
  13. package/scripts/codeoutline/completion.lua +221 -65
  14. package/scripts/codeoutline/coverage.lua +63 -0
  15. package/scripts/codeoutline/cpp_bindings.lua +151 -0
  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 +456 -0
  22. package/scripts/codeoutline/csharp_bindings.lua +498 -0
  23. package/scripts/codeoutline/definition_candidates.lua +180 -0
  24. package/scripts/codeoutline/dispatch_aliases.lua +479 -0
  25. package/scripts/codeoutline/documents.lua +32 -9
  26. package/scripts/codeoutline/explore.lua +274 -119
  27. package/scripts/codeoutline/external_headers.lua +171 -0
  28. package/scripts/codeoutline/foreign.lua +79 -14
  29. package/scripts/codeoutline/foreign_lua.lua +146 -19
  30. package/scripts/codeoutline/function_values.lua +69 -0
  31. package/scripts/codeoutline/go_methods.lua +931 -0
  32. package/scripts/codeoutline/go_modules.lua +329 -0
  33. package/scripts/codeoutline/graph.lua +346 -63
  34. package/scripts/codeoutline/implementations.lua +403 -0
  35. package/scripts/codeoutline/index.lua +165 -27
  36. package/scripts/codeoutline/index_worker.lua +11 -4
  37. package/scripts/codeoutline/indirect.lua +116 -40
  38. package/scripts/codeoutline/js_evidence.lua +32 -3
  39. package/scripts/codeoutline/js_resolution.lua +72 -10
  40. package/scripts/codeoutline/js_workspace.lua +325 -0
  41. package/scripts/codeoutline/lang/c.lua +15 -2
  42. package/scripts/codeoutline/lang/cpp.lua +7 -4
  43. package/scripts/codeoutline/lang/csharp.lua +15 -3
  44. package/scripts/codeoutline/lang/js.lua +56 -20
  45. package/scripts/codeoutline/lang/rust.lua +9 -1
  46. package/scripts/codeoutline/language_detect.lua +100 -0
  47. package/scripts/codeoutline/lsp.lua +173 -23
  48. package/scripts/codeoutline/lua_bindings.lua +228 -54
  49. package/scripts/codeoutline/lua_callback_loops.lua +210 -0
  50. package/scripts/codeoutline/lua_literal.lua +38 -0
  51. package/scripts/codeoutline/main.lua +32 -7
  52. package/scripts/codeoutline/mcp.lua +52 -9
  53. package/scripts/codeoutline/navigation.lua +593 -147
  54. package/scripts/codeoutline/on_demand.lua +93 -0
  55. package/scripts/codeoutline/ordinary_bindings.lua +396 -0
  56. package/scripts/codeoutline/parse.lua +18 -2
  57. package/scripts/codeoutline/parse_pool.lua +1 -1
  58. package/scripts/codeoutline/parse_worker.lua +1 -1
  59. package/scripts/codeoutline/python_bindings.lua +112 -0
  60. package/scripts/codeoutline/query_filters.lua +65 -0
  61. package/scripts/codeoutline/query_report.lua +135 -8
  62. package/scripts/codeoutline/resolution.lua +23 -0
  63. package/scripts/codeoutline/rules.lua +127 -0
  64. package/scripts/codeoutline/rust_packages.lua +203 -0
  65. package/scripts/codeoutline/rust_resolution.lua +301 -0
  66. package/scripts/codeoutline/search_ranking.lua +70 -0
  67. package/scripts/codeoutline/service.lua +47 -7
  68. package/scripts/codeoutline/shared_events.lua +369 -0
  69. package/scripts/codeoutline/static_dispatch.lua +569 -0
  70. package/scripts/codeoutline/text.lua +3 -2
  71. package/scripts/codeoutline/ts_implementations.lua +81 -0
  72. package/scripts/codeoutline/type_bases.lua +61 -0
  73. package/scripts/codeoutline/type_hierarchy.lua +324 -0
  74. package/scripts/codeoutline/type_references.lua +290 -0
  75. package/scripts/codeoutline/type_uses.lua +260 -0
  76. package/scripts/codeoutline/update_sequence.lua +1 -1
  77. package/scripts/codeoutline/version.lua +1 -1
@@ -0,0 +1,151 @@
1
+ -- Bounded declared receiver types. Source evidence is compact; lookup uses the
2
+ -- same symbol tables and include visibility as ordinary graph queries.
3
+ local M = {}
4
+ local control = require('codeoutline.control')
5
+ local types = { class = true, struct = true, union = true }
6
+ local qualifiers = { const = true, volatile = true, static = true, mutable = true, struct = true, class = true }
7
+ local boundary = { [';'] = true, ['{'] = true, ['}'] = true, ['('] = true, [','] = true, [':'] = true }
8
+ local primitive = { auto = true, int = true, double = true, float = true, char = true, bool = true,
9
+ long = true, short = true, unsigned = true, signed = true, void = true }
10
+
11
+ function M.collect(r, T, type_refs)
12
+ if r.language ~= 'cpp' and r.language ~= 'c' then return end
13
+ require('codeoutline.cpp_types').collect(r, T)
14
+ local refs = r.refs
15
+ if type_refs and #type_refs > 0 then
16
+ refs = {}; for _, ref in ipairs(r.refs) do refs[#refs + 1] = ref end
17
+ for _, ref in ipairs(type_refs) do refs[#refs + 1] = ref end
18
+ end
19
+ local wanted = {}
20
+ for _, ref in ipairs(refs) do
21
+ if ref.kind == 'type' and not ref.recv then wanted[ref.name] = true
22
+ elseif r.language == 'c' then -- ordinary C call resolution is unchanged
23
+ elseif ref.kind == 'call' or ref.kind == 'ref' or ref.kind == 'member_call' and T:text(ref.tok - 1) == '::' then
24
+ if T:text(ref.tok) == ref.name then
25
+ ref.binding = require('codeoutline.cpp_types').evidence(T, ref.tok)
26
+ r.has_bindings = true
27
+ if ref.kind == 'call' or ref.kind == 'ref' then wanted[ref.name] = true end
28
+ end
29
+ elseif ref.kind == 'member_call' and ref.recv and ref.recv ~= 'this' then wanted[ref.recv] = true end
30
+ end
31
+ if not next(wanted) then return end
32
+ r.has_bindings = true
33
+ local scopes, stack = {}, {}
34
+ for i = 1, T.n do
35
+ if T:text(i) == '{' and T.m[i] then stack[#stack + 1] = i end
36
+ scopes[i] = stack[#stack] or 0
37
+ if stack[#stack] and T.m[stack[#stack]] == i then stack[#stack] = nil end
38
+ end
39
+ local bindings = {}
40
+ for i = 1, T.n do
41
+ if i % 4096 == 0 then control.check() end
42
+ if T.k[i] == 'id' and wanted[T:text(i)] then
43
+ local after = T:text(i + 1)
44
+ if after == ')' or after == ',' or after == ';' or after == '=' or after == '{' then
45
+ local j = i - 1
46
+ while T:text(j) == '*' or T:text(j) == '&' or T:text(j) == '&&' or qualifiers[T:text(j)] do j = j - 1 end
47
+ if T.k[j] == 'id' or primitive[T:text(j)] then
48
+ local ty = T:text(j)
49
+ j = j - 1
50
+ while T:text(j) == '::' and T.k[j - 1] == 'id' do
51
+ ty = T:text(j - 1) .. '::' .. ty; j = j - 2
52
+ end
53
+ if T:text(j) == '::' then ty = '::' .. ty; j = j - 1 end
54
+ while qualifiers[T:text(j)] do j = j - 1 end
55
+ while primitive[T:text(j)] do j = j - 1 end
56
+ if j == 0 or boundary[T:text(j)] then
57
+ local a = scopes[i]
58
+ local b = a > 0 and T.m[a] or T.n
59
+ for _, n in ipairs(r.nodes) do
60
+ if (n.kind == 'function' or n.kind == 'method' or n.kind == 'constructor')
61
+ and n.ti <= i and i <= n.tj and n.ti > a then a, b = n.ti, n.tj end
62
+ end
63
+ bindings[T:text(i)] = bindings[T:text(i)] or {}
64
+ bindings[T:text(i)][#bindings[T:text(i)] + 1] = { token = i, a = a, b = b, type = ty }
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ for _, ref in ipairs(refs) do
71
+ local bare = (r.language == 'cpp' and (ref.kind == 'call' or ref.kind == 'ref')) or ref.kind == 'type' and not ref.recv
72
+ if bare or ref.kind == 'member_call' and ref.recv and T:text(ref.tok - 1) ~= '::' then
73
+ local best
74
+ for _, b in ipairs(bindings[bare and ref.name or ref.recv] or {}) do
75
+ if b.a <= ref.tok and ref.tok <= b.b and b.token < ref.tok
76
+ and (not best or b.a > best.a or b.a == best.a and b.token > best.token) then best = b end
77
+ end
78
+ if best then
79
+ if ref.kind == 'type' then ref.type_use.value_shadowed = true
80
+ else ref.binding = bare and 'cpp:?' or 'cpp:type:' .. best.type .. '\0' .. (T.s[best.token] - 1) end
81
+ -- A chain's tail is not the simple local sharing that name.
82
+ if T:text(ref.tok - 3) == '.' or T:text(ref.tok - 3) == '->' then ref.binding = 'cpp:?' end
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ function M.resolve(G, f, src, name, evidence, deps, ctx, accept)
89
+ if G.files[f].language ~= 'cpp' or not evidence or evidence:sub(1, 4) ~= 'cpp:' then return nil end
90
+ local written, position = evidence:match('^cpp:name:(.-)%z(%d+)$')
91
+ if written then
92
+ local scope = src and G.nodes[src].qualified
93
+ if src and (G.nodes[src].kind == 'function' or G.nodes[src].kind == 'method') then scope = scope:match('^(.*)::[^:]+$') end
94
+ local ids, known = require('codeoutline.cpp_types').resolve(G, f, written,
95
+ { scope = scope, at = tonumber(position), deps = deps, context = ctx, accept = accept, definitions = true })
96
+ return ids, not known and 'unresolved c++ name' or nil
97
+ end
98
+ local ty = evidence:match('^cpp:type:(.+)$')
99
+ if not ty then return {} end
100
+ local written, at = ty:match('^(.-)%z(%d+)$')
101
+ ty = written or ty
102
+ local function classes(written, scope, file, position)
103
+ return require('codeoutline.cpp_types').resolve(G, file or f, written,
104
+ { scope = scope, at = position, deps = deps, context = ctx })
105
+ end
106
+ local scope = src and G.nodes[src].qualified:match('^(.*)::[^:]+$')
107
+ local result, seen, emitted = {}, {}, {}
108
+ local definition_owners = {}
109
+ local configured = require('codeoutline.cpp_includes').policy(G, f, ctx).configured
110
+ local function belongs(id, cls)
111
+ if definition_owners[id] == nil then
112
+ definition_owners[id] = require('codeoutline.cpp_types').owner(G, G.node_file[id], G.nodes[id], ctx) or false
113
+ end
114
+ if definition_owners[id] == cls then return true end
115
+ -- Keep legacy class-outside hints when there is no configured policy
116
+ -- and the defining file provides no class evidence (e.g. forced includes).
117
+ local node = G.nodes[id]
118
+ local parent = node.parent and G.files[G.node_file[id]].nodes[node.parent]
119
+ return not configured and definition_owners[id] == false and (not parent or not types[parent.kind])
120
+ end
121
+ local function members(cls, depth)
122
+ control.check()
123
+ if seen[cls] or depth > 8 then return end
124
+ seen[cls] = true
125
+ local q = G.nodes[cls].qualified .. '::' .. name
126
+ local matches, has_definition = {}, false
127
+ for _, id in ipairs(G.by_name[name] or {}) do
128
+ local n = G.nodes[id]
129
+ if n.qualified == q and G.files[G.node_file[id]].language == 'cpp'
130
+ and belongs(id, cls) then
131
+ matches[#matches + 1] = id
132
+ if not n.decl and n.kind ~= 'prototype' then has_definition = true end
133
+ end
134
+ end
135
+ for _, id in ipairs(matches) do
136
+ local n = G.nodes[id]
137
+ if not emitted[id] and (not has_definition or not n.decl and n.kind ~= 'prototype') then
138
+ emitted[id] = true; result[#result + 1] = id
139
+ end
140
+ end
141
+ if #matches == 0 then
142
+ for _, base in ipairs(G.nodes[cls].bases or {}) do
143
+ for _, id in ipairs(classes(base, G.nodes[cls].qualified:match('^(.*)::[^:]+$'), G.node_file[cls])) do members(id, depth + 1) end
144
+ end
145
+ end
146
+ end
147
+ for _, cls in ipairs(classes(ty, scope, f, tonumber(at))) do members(cls, 0) end
148
+ return result
149
+ end
150
+
151
+ return M
@@ -0,0 +1,143 @@
1
+ -- Include-driven admission of unknown suffixes from the normal file listing.
2
+ local paths = require('codeoutline.path')
3
+ local parse = require('codeoutline.parse')
4
+ local includes = require('codeoutline.cpp_includes')
5
+ local control = require('codeoutline.control')
6
+ local M = { MAX_FILES = 4096, MAX_BYTES = 67108864, MAX_DEPTH = 32 }
7
+ local function c_family(rec) return rec and (rec.language == 'c' or rec.language == 'cpp') end
8
+
9
+ function M.inventory(root)
10
+ return { root = root, files = {}, file_index = {}, by_base = {}, count = 0 }
11
+ end
12
+
13
+ function M.policy_key(root)
14
+ local policy = includes.context({ root = root })
15
+ local parts = { tostring(policy.configured) }
16
+ for _, dir in ipairs(policy.defaults) do parts[#parts + 1] = 'path:' .. dir end
17
+ for _, rule in ipairs(policy.rules) do
18
+ parts[#parts + 1] = 'rule:' .. rule.directory
19
+ for _, dir in ipairs(rule.paths) do parts[#parts + 1] = 'path:' .. dir end
20
+ end
21
+ return table.concat(parts, '\0')
22
+ end
23
+
24
+ function M.add(G, rel)
25
+ if parse.language(rel) then return end
26
+ local key = paths.key(rel)
27
+ if G.file_index[key] then return end
28
+ G.count = G.count + 1
29
+ G.files[rel] = { path = rel }
30
+ G.file_index[rel], G.file_index[key] = rel, rel
31
+ local base = rel:match('([^/]*)$')
32
+ local key_base = paths.key(base)
33
+ G.by_base[key_base] = G.by_base[key_base] or {}
34
+ G.by_base[key_base][#G.by_base[key_base] + 1] = rel
35
+ G.by_base[base] = G.by_base[key_base]
36
+ end
37
+
38
+ -- sync(batch, seen) uses the regular index loader and parse worker path.
39
+ -- Parents, macro environments and detailed bindings stay query-owned.
40
+ function M.refresh(idx, stats, seen, sync, changed)
41
+ local G = idx.fragment_inventory
42
+ if not G or G.count == 0 then return end
43
+ local ctx, frontier = {}, {}
44
+ for rel, rec in pairs(idx.files) do
45
+ if seen[rel] and not rec.cpp_fragment and c_family(rec) then frontier[#frontier + 1] = rel end
46
+ end
47
+ table.sort(frontier)
48
+ if #frontier == 0 then return end
49
+ local admitted, canonical, count, bytes = {}, {}, 0, 0
50
+ local function target(rel)
51
+ local old = canonical[rel]
52
+ if old ~= nil then return old or nil end
53
+ local real = xutils.realpath(idx:abs(rel))
54
+ local found
55
+ if real and paths.contains(idx.root, real) then
56
+ real = paths.normalize(real)
57
+ local relative = real:sub(#idx.root + (idx.root:sub(-1) == '/' and 1 or 2))
58
+ found = G.file_index[paths.key(relative)]
59
+ end
60
+ canonical[rel] = found or false
61
+ return found
62
+ end
63
+ local function dependency(rel, imp)
64
+ if parse.language(imp.path) then return end
65
+ local ids = includes.resolve(G, rel, imp, ctx)
66
+ if #ids == 1 then return target(ids[1]) end
67
+ end
68
+ local metadata = {}
69
+ local function stat(rel)
70
+ if metadata[rel] == nil then metadata[rel] = xutils.stat(idx:abs(rel)) or false end
71
+ return metadata[rel] or nil
72
+ end
73
+ -- Settle C++ precedence through unchanged cached includes before loading a
74
+ -- batch. Otherwise a shared fragment can flip C -> C++ on every refresh
75
+ -- when the C++ includer is reached a round later than the C includer.
76
+ local cpp_hints, queue, queued = {}, {}, {}
77
+ for _, rel in ipairs(frontier) do
78
+ queued[rel] = idx.files[rel].language == 'cpp'
79
+ queue[#queue + 1] = { rel, queued[rel] }
80
+ end
81
+ local at, limit = 1, #queue + 2 * M.MAX_FILES
82
+ while at <= #queue and at <= limit do
83
+ control.check()
84
+ local rel, cpp = queue[at][1], queue[at][2]
85
+ local rec = idx.files[rel]; G.files[rel] = rec
86
+ cpp = cpp or rec.cpp_fragment_cpp or false
87
+ for _, imp in ipairs(rec.imports) do
88
+ local p = dependency(rel, imp)
89
+ if p then
90
+ if cpp then cpp_hints[p] = true end
91
+ local cached, st = idx.files[p], stat(p)
92
+ if cached and cached.cpp_fragment and not (changed and changed[p]) and st
93
+ and cached.size == st.size and cached.mtime == st.mtime then
94
+ local mode = cpp or cached.cpp_fragment_cpp or false
95
+ if queued[p] == nil or mode and not queued[p] then
96
+ queued[p] = mode; queue[#queue + 1] = { p, mode }
97
+ end
98
+ end
99
+ end
100
+ end
101
+ at = at + 1
102
+ end
103
+ for depth = 1, M.MAX_DEPTH do
104
+ local pending = {}
105
+ for _, rel in ipairs(frontier) do
106
+ control.check()
107
+ local rec = idx.files[rel]
108
+ G.files[rel] = rec
109
+ for _, imp in ipairs(rec.imports) do
110
+ local p = dependency(rel, imp)
111
+ if p then
112
+ local desired = cpp_hints[p] and 'cpp' or rec.language
113
+ local language = pending[p] or admitted[p]
114
+ if not language or language == 'c' and desired == 'cpp' then pending[p] = desired end
115
+ end
116
+ end
117
+ end
118
+ local ordered = {}; for p in pairs(pending) do ordered[#ordered + 1] = p end
119
+ table.sort(ordered)
120
+ local batch = {}
121
+ for _, p in ipairs(ordered) do
122
+ local st = stat(p)
123
+ if st and st.type == 'file' and st.size <= idx.max_bytes then
124
+ if not admitted[p] and (count >= M.MAX_FILES or bytes + st.size > M.MAX_BYTES) then stats.fragment_limited = true
125
+ else
126
+ if not admitted[p] then count, bytes = count + 1, bytes + st.size end
127
+ admitted[p] = pending[p]
128
+ batch[#batch + 1] = { rel = p, language = pending[p], stat = st }
129
+ end
130
+ end
131
+ end
132
+ if #batch == 0 then break end
133
+ frontier = sync(batch, seen)
134
+ table.sort(frontier)
135
+ if #frontier == 0 then break end
136
+ if depth == M.MAX_DEPTH then stats.fragment_limited = true end
137
+ end
138
+ local retained = 0
139
+ for rel in pairs(admitted) do if seen[rel] then retained = retained + 1 end end
140
+ stats.fragment_files = retained
141
+ end
142
+
143
+ return M
@@ -0,0 +1,340 @@
1
+ -- C++ override evidence belongs to a query, never the resident index.
2
+ local M = { MAX_ALIAS_FILES = 128, MAX_ALIAS_BYTES = 1024 * 1024 }
3
+ local control = require('codeoutline.control')
4
+ local lookup = require('codeoutline.cpp_types')
5
+ local classes = { class = true, struct = true }
6
+ local cv = { const = true, volatile = true }
7
+ local builtin = { void = true, bool = true, char = true, wchar_t = true, char8_t = true,
8
+ char16_t = true, char32_t = true, short = true, int = true, long = true,
9
+ signed = true, unsigned = true, float = true, double = true }
10
+ local modifiers = { virtual = true, static = true, inline = true, constexpr = true,
11
+ consteval = true, explicit = true, friend = true, extern = true }
12
+
13
+ local function tokens(T, a, b)
14
+ local out = {}
15
+ for i = a, b do out[#out + 1] = T:text(i) end
16
+ return out
17
+ end
18
+
19
+ local function parameter(T, a, b)
20
+ for i = a, b do if T:text(i) == '=' then b = i - 1; break end end
21
+ -- A trailing identifier is a name only when a type precedes it. Preserve
22
+ -- unnamed `const Item`, qualified types, and all pointer/cv decoration.
23
+ if T.k[b] == 'id' and T:text(b - 1) ~= '::' then
24
+ local typed = false
25
+ for i = a, b - 1 do
26
+ local s = T:text(i)
27
+ if not cv[s] and s ~= 'struct' and s ~= 'class' and s ~= '::' then typed = true end
28
+ end
29
+ if typed then b = b - 1 end
30
+ end
31
+ return tokens(T, a, b)
32
+ end
33
+
34
+ function M.collect(r, T)
35
+ local evidence = {}
36
+ for id, n in ipairs(r.nodes) do
37
+ if id % 128 == 0 then control.check() end
38
+ if classes[n.kind] or n.kind == 'method' then
39
+ local d = { at = T.s[n.ti] - 1 }
40
+ if classes[n.kind] then d.hierarchy_bases = require('codeoutline.type_bases').collect(T, n, r.language) end
41
+ local ni, opening
42
+ for i = n.ti, n.tj do
43
+ local s = T:text(i)
44
+ if s == '{' then break end
45
+ if s == 'template' or s == '<' or s == '>' then d.unsupported = true end
46
+ if s == 'virtual' or s == 'static' or s == 'final' or s == 'friend' then d[s] = true end
47
+ if s == n.name and T:text(i + 1) == '(' and n.kind == 'method' then
48
+ ni, opening = i, i + 1; break
49
+ end
50
+ end
51
+ if n.kind == 'method' and opening and T.m[opening] then
52
+ local close, params, first = T.m[opening], {}, opening + 1
53
+ local i = first
54
+ while i <= close do
55
+ local s = T:text(i)
56
+ if i == close or s == ',' then
57
+ if first < i then params[#params + 1] = parameter(T, first, i - 1) end
58
+ first = i + 1
59
+ elseif T.m[i] and T.m[i] > i then i = T.m[i] end
60
+ i = i + 1
61
+ end
62
+ if #params == 1 and #params[1] == 1 and params[1][1] == 'void' then params = {} end
63
+ d.params = params
64
+ local head = ni
65
+ while T:text(head - 1) == '::' and T.k[head - 2] == 'id' do head = head - 2 end
66
+ if T:text(head - 1) == '::' then head = head - 1 end
67
+ local ret = {}
68
+ for j = n.ti, head - 1 do
69
+ local s = T:text(j)
70
+ if not modifiers[s] then ret[#ret + 1] = s end
71
+ end
72
+ d.returns = ret
73
+ i = close + 1
74
+ while i <= n.tj do
75
+ local s = T:text(i)
76
+ if s == '{' or s == ';' then break end
77
+ if cv[s] or s == 'final' or s == 'override' then d[s] = true
78
+ elseif s == '&' or s == '&&' then d.ref = s
79
+ elseif s == '=' and T:text(i + 1) == '0' then d.pure = true; i = i + 1
80
+ elseif s == 'noexcept' then
81
+ if T:text(i + 1) == '(' and T.m[i + 1] then i = T.m[i + 1] end
82
+ else d.unsupported = true end
83
+ i = i + 1
84
+ end
85
+ elseif n.kind == 'method' then d.unsupported = true end
86
+ evidence[id] = d
87
+ end
88
+ end
89
+ r.implementation_evidence = evidence
90
+ end
91
+
92
+ function M.context(G, ctx, report, limits, C)
93
+ local details, owners, chains, lists, outgoing, incoming = {}, {}, {}, {}, {}, {}
94
+ local alias_records, alias_count, alias_bytes, alias_limited = {}, 0, 0, false
95
+ local function aliases(f)
96
+ if alias_records[f] then return alias_records[f] end
97
+ if alias_limited then return {} end
98
+ local data = lookup.aliases(G, f, ctx)
99
+ local weight = 0
100
+ for _, alias in ipairs(data) do
101
+ weight = weight + 128
102
+ for _, value in pairs(alias) do if type(value) == 'string' then weight = weight + #value end end
103
+ end
104
+ if alias_count >= M.MAX_ALIAS_FILES or alias_bytes + weight > M.MAX_ALIAS_BYTES then
105
+ alias_limited, C.incomplete = true, true; return {}
106
+ end
107
+ -- Retain only the alias arrays already produced by the shared loader.
108
+ -- Repeated parameter lookups must not thrash its 16-file source FIFO.
109
+ alias_count, alias_bytes = alias_count + 1, alias_bytes + weight
110
+ alias_records[f] = data
111
+ C.stats.cpp_alias_files, C.stats.cpp_alias_bytes = alias_count, alias_bytes
112
+ return data
113
+ end
114
+ local function info(id)
115
+ if not details[id] then
116
+ local f = G.node_file[id]
117
+ local data = ctx.ordinary and ctx.ordinary.data(f) or G.files[f]
118
+ details[id] = (data.implementation_evidence or {})[id - G.id_of[f][1] + 1] or {}
119
+ end
120
+ return details[id]
121
+ end
122
+ local function resolve(id, text, accept)
123
+ local ids = lookup.resolve(G, G.node_file[id], text, { context = ctx,
124
+ scope = G.nodes[id].qualified, at = info(id).at, accept = accept or classes,
125
+ undecorated = true, aliases = aliases })
126
+ if ctx.include_incomplete then C.incomplete = true end
127
+ return #ids == 1 and ids[1] or nil
128
+ end
129
+ local function owner(id)
130
+ if owners[id] == nil then
131
+ local n, f = G.nodes[id], G.node_file[id]
132
+ local parent = n.parent and G.id_of[f][n.parent]
133
+ owners[id] = parent and classes[G.nodes[parent].kind] and parent
134
+ or resolve(id, n.qualified:match('^(.*)::[^:]+$')) or false
135
+ end
136
+ return owners[id] or nil
137
+ end
138
+ local function ancestry(id)
139
+ if chains[id] then return chains[id] end
140
+ local out, visited, current = {}, {}, id
141
+ while current do
142
+ control.check()
143
+ if visited[current] or #out > limits.MAX_DEPTH or C.stats.hierarchy >= limits.MAX_HIERARCHY then
144
+ C.incomplete = true; out = {}; break
145
+ end
146
+ visited[current] = true; C.stats.hierarchy = C.stats.hierarchy + 1
147
+ local n, d = G.nodes[current], info(current)
148
+ local bases = n.bases or {}
149
+ -- Multiple/virtual inheritance, templates, and unresolved bases
150
+ -- need a final-overrider model; do not approximate by class name.
151
+ if d.unsupported or d.virtual or current ~= id and d.final or #bases > 1 then out = {}; break end
152
+ if #bases == 0 then break end
153
+ local parent = resolve(current, bases[1])
154
+ if not parent then out = {}; break end
155
+ out[#out + 1] = parent; current = parent
156
+ end
157
+ chains[id] = out; return out
158
+ end
159
+ local function type_key(id, words, value)
160
+ if not words or #words == 0 then return nil end
161
+ local base, layers, layer = {}, {}, {}
162
+ layers[1] = layer
163
+ for _, s in ipairs(words) do
164
+ if cv[s] then layer[s] = true
165
+ elseif s == '*' or s == '&' or s == '&&' then
166
+ if layer.op == '&' or layer.op == '&&' then return nil end
167
+ layer = { op = s }; layers[#layers + 1] = layer
168
+ elseif s == '::' or s:match('^[%a_][%w_]*$') then
169
+ if #layers ~= 1 then return nil end
170
+ if s ~= 'struct' and s ~= 'class' and s ~= 'enum' then base[#base + 1] = s end
171
+ else return nil end
172
+ end
173
+ if #base == 0 then return nil end
174
+ local primitive = true
175
+ for _, s in ipairs(base) do if not builtin[s] then primitive = false end end
176
+ local canonical
177
+ if primitive then
178
+ canonical = table.concat(base, ' ')
179
+ canonical = canonical:gsub('^signed int$', 'int'):gsub('^signed$', 'int')
180
+ :gsub('^unsigned$', 'unsigned int'):gsub('short int$', 'short'):gsub('long int$', 'long')
181
+ else
182
+ local written = table.concat(base):gsub('::', ':')
183
+ if written:find('[^%w_:]') or not written:match('^:?[A-Za-z_][%w_:]*$') then return nil end
184
+ -- Adjacent identifiers are not a qualified named type.
185
+ for i = 2, #base do if base[i] ~= '::' and base[i - 1] ~= '::' then return nil end end
186
+ local ty = resolve(id, table.concat(base), { class = true, struct = true, enum = true })
187
+ if not ty then return nil end
188
+ canonical = '#' .. ty
189
+ end
190
+ local result = { canonical }
191
+ for i, part in ipairs(layers) do
192
+ local ignore = value and i == #layers and part.op ~= '&' and part.op ~= '&&'
193
+ result[#result + 1] = (part.op or '') .. (not ignore and part.const and ' const' or '')
194
+ .. (not ignore and part.volatile and ' volatile' or '')
195
+ end
196
+ return table.concat(result)
197
+ end
198
+ local function signature(id)
199
+ local d, cls = info(id), owner(id)
200
+ if not cls or not d.params or d.unsupported or d.friend or info(cls).unsupported
201
+ or info(cls).virtual or #(G.nodes[cls].bases or {}) > 1 then return nil end
202
+ local enclosing, depth = cls, 0
203
+ while enclosing do
204
+ if info(enclosing).unsupported then return nil end
205
+ depth = depth + 1
206
+ if depth > limits.MAX_DEPTH + 1 then C.incomplete = true; return nil end
207
+ local n, f = G.nodes[enclosing], G.node_file[enclosing]
208
+ enclosing = n.parent and G.id_of[f][n.parent]
209
+ if enclosing and not classes[G.nodes[enclosing].kind] then enclosing = nil end
210
+ end
211
+ local params = {}
212
+ for _, p in ipairs(d.params) do
213
+ local key = type_key(id, p, true)
214
+ if not key then return nil end
215
+ params[#params + 1] = key
216
+ end
217
+ local ret = type_key(id, d.returns, false)
218
+ if not ret then return nil end
219
+ return table.concat(params, ',') .. '|' .. (d.const and 'const' or '')
220
+ .. (d.volatile and 'volatile' or '') .. (d.ref or ''), ret
221
+ end
222
+ local function methods(name)
223
+ if lists[name] then return lists[name] end
224
+ local groups, by_id, ordered, checked = {}, {}, {}, 0
225
+ for _, id in ipairs(G.by_name[name] or {}) do
226
+ control.check()
227
+ if G.nodes[id].kind == 'method' and G.files[G.node_file[id]].language == 'cpp' then
228
+ if checked >= limits.MAX_CANDIDATES then C.incomplete = true; break end
229
+ checked = checked + 1; C.stats.candidates = C.stats.candidates + 1
230
+ local sig, ret = signature(id)
231
+ if sig then
232
+ local cls, d = owner(id), info(id)
233
+ groups[cls] = groups[cls] or {}
234
+ local group = groups[cls][sig]
235
+ if not group then
236
+ group = { cls = cls, sig = sig, ret = ret, ids = {} }; groups[cls][sig] = group
237
+ ordered[#ordered + 1] = group
238
+ end
239
+ if group.ret ~= ret then group.invalid = true end
240
+ group.ids[#group.ids + 1] = id; by_id[id] = group
241
+ local n, f = G.nodes[id], G.node_file[id]
242
+ if n.parent and G.id_of[f][n.parent] == cls then group.declared = true end
243
+ for _, flag in ipairs({ 'virtual', 'pure', 'static', 'final' }) do if d[flag] then group[flag] = true end end
244
+ if not G.nodes[id].decl then
245
+ if group.body then group.invalid = true end
246
+ group.body = id
247
+ end
248
+ end
249
+ end
250
+ end
251
+ for _, members in pairs(groups) do
252
+ for _, group in pairs(members) do if not group.declared then group.invalid = true end end
253
+ end
254
+ local result = { groups = groups, by_id = by_id, ordered = ordered }; lists[name] = result; return result
255
+ end
256
+ local function inherited(list, group)
257
+ local result = {}
258
+ for _, cls in ipairs(ancestry(group.cls)) do
259
+ local parent = list.groups[cls] and list.groups[cls][group.sig]
260
+ if parent then result[#result + 1] = parent end
261
+ end
262
+ return result
263
+ end
264
+ local function contract(list, group)
265
+ if group.invalid or group.static then return false end
266
+ local virtual = group.virtual
267
+ for _, parent in ipairs(inherited(list, group)) do
268
+ if parent.invalid or parent.static or parent.final or parent.ret ~= group.ret then return false end
269
+ virtual = virtual or parent.virtual
270
+ end
271
+ return virtual
272
+ end
273
+ local function matches(list, base, candidate)
274
+ if candidate.invalid or candidate.pure or candidate.static or candidate.ret ~= base.ret or base.final then return false end
275
+ for _, parent in ipairs(inherited(list, candidate)) do
276
+ if parent == base then return true end
277
+ if parent.invalid or parent.static or parent.final or parent.ret ~= candidate.ret then return false end
278
+ end
279
+ return false
280
+ end
281
+ local function link(source, target)
282
+ C.stats.links = C.stats.links + 1
283
+ if report then report:add({ source = source, target = target, line = G.nodes[source].line,
284
+ kind = 'possible implementation (C++ virtual declaration and exact signature)' }) end
285
+ end
286
+ local function potential(id)
287
+ local cls = owner(id)
288
+ if not cls or info(cls).final then return false end
289
+ if #(G.nodes[cls].bases or {}) > 0 or info(id).virtual then return true end
290
+ -- A class-outside body inherits the virtual marker from its declaration.
291
+ -- A plain root method must not expand hundreds of same-name candidates.
292
+ for _, declaration in ipairs(G.by_qualified[G.nodes[id].qualified] or {}) do
293
+ if G.files[G.node_file[declaration]].language == 'cpp' and G.nodes[declaration].kind == 'method'
294
+ and owner(declaration) == cls and info(declaration).virtual then return true end
295
+ end
296
+ return false
297
+ end
298
+ local D = {}
299
+ function D.outgoing(id)
300
+ if outgoing[id] then return outgoing[id] end
301
+ if not potential(id) then outgoing[id] = {}; return outgoing[id] end
302
+ local result, list = {}, methods(G.nodes[id].name)
303
+ local base = list.by_id[id]
304
+ if base and contract(list, base) and not base.final then
305
+ for _, group in ipairs(list.ordered) do
306
+ if group.sig == base.sig and matches(list, base, group) then
307
+ if #result >= limits.MAX_TARGETS then C.incomplete = true; break end
308
+ local target = group.body or group.ids[1]
309
+ result[#result + 1] = target
310
+ end
311
+ end
312
+ end
313
+ if alias_limited then result = {} end
314
+ for _, target in ipairs(result) do link(id, target) end
315
+ table.sort(result); outgoing[id] = result; return result
316
+ end
317
+ function D.incoming(id)
318
+ if incoming[id] then return incoming[id] end
319
+ local cls = owner(id)
320
+ if not cls or #(G.nodes[cls].bases or {}) == 0 then incoming[id] = {}; return incoming[id] end
321
+ local result, list = {}, methods(G.nodes[id].name)
322
+ local candidate = list.by_id[id]
323
+ if candidate then
324
+ for _, base in ipairs(inherited(list, candidate)) do
325
+ if contract(list, base) and matches(list, base, candidate) then
326
+ for _, source in ipairs(base.ids) do
327
+ if #result >= limits.MAX_TARGETS then C.incomplete = true; break end
328
+ result[#result + 1] = source
329
+ end
330
+ end
331
+ end
332
+ end
333
+ if alias_limited then result = {} end
334
+ for _, source in ipairs(result) do link(source, id) end
335
+ table.sort(result); incoming[id] = result; return result
336
+ end
337
+ return D
338
+ end
339
+
340
+ return M