@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
@@ -34,6 +34,7 @@ local M = {}
34
34
  M.EDGE = 3
35
35
  M.MAX_TIES = 3
36
36
  M.MAX_CANDIDATES = 200
37
+ M.MAX_INCLUDES = 2000
37
38
 
38
39
  -- Node kinds a call / `new` / bare reference may bind to.
39
40
  local CALLABLE = {
@@ -42,12 +43,14 @@ local CALLABLE = {
42
43
  }
43
44
  local NEWABLE = { class = true, record = true, constructor = true, enum = true }
44
45
  local VALUE = { ['function'] = true, method = true }
46
+ local NO_MACRO = {}
47
+ for kind in pairs(CALLABLE) do if kind ~= 'macro' then NO_MACRO[kind] = true end end
45
48
 
46
49
  -- C and C++ call each other (extern "C", shared headers); others don't mix.
47
50
  local C_FAMILY = { c = true, cpp = true }
48
51
  local HEADER_EXT = { h = true, hpp = true, hh = true, hxx = true }
49
52
  local SOURCE_EXT = { c = true, cc = true, cpp = true, cxx = true }
50
- local FAMILY = { c = 'c', cpp = 'c', javascript = 'js', typescript = 'js' }
53
+ local FAMILY = { c = 'c', cpp = 'c', javascript = 'js', typescript = 'js', arkts = 'js' }
51
54
  local function family(lang) return FAMILY[lang] or lang end
52
55
 
53
56
  local function dirname(p) return p:match('^(.*)/[^/]*$') or '' end
@@ -68,29 +71,16 @@ local function push3(t, k, a, b, c)
68
71
  end
69
72
 
70
73
  -- Files a file depends on, resolved from its #include / import records.
71
- local function resolve_deps(G, f, js_context)
74
+ local function resolve_deps(G, f, js_context, imports_only)
72
75
  local rec = G.files[f]
76
+ if C_FAMILY[rec.language] then js_context = js_context or {} end
73
77
  local deps = {}
74
78
  local function add(g) if g and g ~= f then deps[g] = true end end
75
79
  local here = dirname(rec.path)
76
80
  for _, imp in ipairs(rec.imports) do
77
81
  local path = imp.path
78
82
  if C_FAMILY[rec.language] then
79
- path = path:gsub('\\', '/')
80
- if path:find('^%.%.?/') then
81
- -- "../dir/x.h": relative to the including file first
82
- local parts = {}
83
- for segment in ((here ~= '' and here .. '/' or '') .. path):gmatch('[^/]+') do
84
- if segment == '..' then parts[#parts] = nil
85
- elseif segment ~= '.' then parts[#parts + 1] = segment end
86
- end
87
- add(G.file_index[table.concat(parts, '/')])
88
- end
89
- -- "dir/x.h": files whose path ends with it, preferring the closest
90
- for _, g in ipairs(G.by_base[basename(path)] or {}) do
91
- local p = G.files[g].path
92
- if p == path or p:sub(-(#path + 1)) == '/' .. path then add(g) end
93
- end
83
+ for _, g in ipairs(require('codeoutline.cpp_includes').resolve(G, f, imp, js_context)) do add(g) end
94
84
  elseif rec.language == 'python' then
95
85
  local rel = path
96
86
  local base = here
@@ -129,26 +119,16 @@ local function resolve_deps(G, f, js_context)
129
119
  -- dofile/loadfile take a path.
130
120
  for _, g in ipairs(lua_bindings.modules(G, imp, js_context and js_context.lua)) do add(g) end
131
121
  elseif rec.language == 'go' then
132
- -- import "github.com/org/repo/pkg/sub": the package is the directory
133
- -- whose path ends with the longest matching suffix of the import
134
- local segs = {}
135
- for s in path:gmatch('[^/]+') do segs[#segs + 1] = s end
136
- for k = 1, #segs do
137
- local suffix = table.concat(segs, '/', k)
138
- local hit = false
139
- for _, d in ipairs(G.dir_by_base[segs[#segs]] or {}) do
140
- if d == suffix or d:sub(-(#suffix + 1)) == '/' .. suffix then
141
- for _, g in ipairs(G.dir_files[d]) do add(g) end
142
- hit = true
143
- end
144
- end
145
- if hit then break end
146
- end
147
- elseif rec.language == 'javascript' or rec.language == 'typescript' then
148
- add(js_resolution.module(G, f, path, js_context))
122
+ for _, g in ipairs(require('codeoutline.go_modules').files(G, f, path, js_context)) do add(g) end
123
+ elseif rec.language == 'javascript' or rec.language == 'typescript' or rec.language == 'arkts' then
124
+ add(js_resolution.module(G, f, path, js_context, imp.kind))
149
125
  elseif rec.language == 'csharp' then
150
126
  for _, g in ipairs(G.ns_files[path] or {}) do add(g) end
151
127
  elseif rec.language == 'rust' then
128
+ if js_context then
129
+ local files = require('codeoutline.rust_resolution').imports(G, f, imp, js_context)
130
+ if files then for _, g in ipairs(files) do add(g) end; goto next_import end
131
+ end
152
132
  if imp.kind == 'mod' then
153
133
  -- mod x; -> x.rs / x/mod.rs next to this file (or under its stem dir)
154
134
  local st = stem(rec.path)
@@ -187,13 +167,16 @@ local function resolve_deps(G, f, js_context)
187
167
  for _, id in ipairs(G.by_qualified[path] or {}) do add(G.node_file[id]) end
188
168
  end
189
169
  end
170
+ ::next_import::
190
171
  end
191
172
  -- Java: the whole package is visible without imports; Go: the whole
192
173
  -- directory is one package; C#: the file's own namespaces.
193
174
  if (rec.language == 'java' or rec.language == 'kotlin') and rec.package then
194
175
  for _, g in ipairs(G.pkg_files[rec.package] or {}) do add(g) end
195
176
  elseif rec.language == 'go' then
196
- for _, g in ipairs(G.dir_files[here] or {}) do add(g) end
177
+ if not imports_only then
178
+ for _, g in ipairs(require('codeoutline.go_modules').peers(G, f, js_context)) do add(g) end
179
+ end
197
180
  elseif rec.language == 'csharp' then
198
181
  for _, node in ipairs(rec.nodes) do
199
182
  if node.kind == 'namespace' then
@@ -204,12 +187,27 @@ local function resolve_deps(G, f, js_context)
204
187
  -- C: x.c usually implements what x.h declares, so a file that includes
205
188
  -- x.h also "depends on" x.c for resolution purposes.
206
189
  local paired = {}
207
- if C_FAMILY[rec.language] then
190
+ if C_FAMILY[rec.language] and not imports_only then
191
+ local strict = require('codeoutline.cpp_includes').policy(G, f, js_context).configured
192
+ local function can_pair(source, header)
193
+ if not strict then return true end
194
+ return M.directly_includes(G, source, header, js_context)
195
+ end
208
196
  for g in pairs(deps) do
209
197
  local p = G.files[g].path
210
- if HEADER_EXT[p:match('%.(%w+)$') or ''] then
198
+ if HEADER_EXT[p:match('%.(%w+)$') or ''] or G.files[g].on_demand or G.files[g].cpp_fragment then
199
+ local local_sources, has_local = {}, false
211
200
  for _, h in ipairs(G.by_stem[stem(p)] or {}) do
212
- if h ~= f and SOURCE_EXT[G.files[h].path:match('%.(%w+)$') or ''] then paired[h] = true end
201
+ if SOURCE_EXT[G.files[h].path:match('%.(%w+)$') or ''] and dirname(G.files[h].path) == dirname(p) then
202
+ has_local = true
203
+ if h ~= f and can_pair(h, g) then local_sources[#local_sources + 1] = h end
204
+ end
205
+ end
206
+ if has_local then for _, h in ipairs(local_sources) do paired[h] = true end
207
+ else
208
+ for _, h in ipairs(G.by_stem[stem(p)] or {}) do
209
+ if h ~= f and SOURCE_EXT[G.files[h].path:match('%.(%w+)$') or ''] and can_pair(h, g) then paired[h] = true end
210
+ end
213
211
  end
214
212
  end
215
213
  end
@@ -217,6 +215,52 @@ local function resolve_deps(G, f, js_context)
217
215
  return deps, paired
218
216
  end
219
217
 
218
+ function M.directly_includes(G, source, header, ctx)
219
+ ctx = ctx or {}
220
+ local cache = ctx.cpp_source_dependencies
221
+ if not cache then cache = {}; ctx.cpp_source_dependencies = cache end
222
+ if not cache[source] then cache[source] = resolve_deps(G, source, ctx, true) end
223
+ return cache[source][header] or false
224
+ end
225
+
226
+ -- One bounded include closure for graph queries, navigation and type lookup.
227
+ -- Paired implementations may supply definitions, never declarations/imports.
228
+ function M.visible_dependencies(G, f, ctx)
229
+ if C_FAMILY[G.files[f].language] then ctx = ctx or {} end
230
+ local cache = ctx and ctx.include_dependencies
231
+ if not cache and ctx then cache = {}; ctx.include_dependencies = cache end
232
+ local old = cache and cache[f]
233
+ if old then return old[1], old[2], old[3] end
234
+ local direct, paired = resolve_deps(G, f, ctx)
235
+ local incomplete = false
236
+ if C_FAMILY[G.files[f].language] then
237
+ local visible, queue = {}, {}
238
+ local function add(files)
239
+ local ordered = {}
240
+ for g in pairs(files) do ordered[#ordered + 1] = g end
241
+ table.sort(ordered, function(a, b) return G.files[a].path < G.files[b].path end)
242
+ for _, g in ipairs(ordered) do
243
+ if g ~= f and not visible[g] then
244
+ if #queue >= M.MAX_INCLUDES then incomplete = true
245
+ else visible[g] = true; queue[#queue + 1] = g end
246
+ end
247
+ end
248
+ end
249
+ add(direct)
250
+ local i = 1
251
+ while i <= #queue do
252
+ control.check()
253
+ local more, sources = resolve_deps(G, queue[i], ctx)
254
+ for g in pairs(sources) do if g ~= f then paired[g] = true end end
255
+ add(more); i = i + 1
256
+ end
257
+ direct = visible
258
+ end
259
+ if cache then cache[f] = { direct, paired, incomplete } end
260
+ if incomplete and ctx then ctx.include_incomplete = true end
261
+ return direct, paired, incomplete
262
+ end
263
+
220
264
  -- Innermost class-like container of a node (for self./this. calls).
221
265
  local function owner_class(G, id)
222
266
  local f = G.node_file[id]
@@ -239,7 +283,7 @@ local function locality(G, f, cf, c, deps, paired)
239
283
  if cf == f then return 100 end
240
284
  if c.static then
241
285
  -- `static inline` helpers in an included header serve every includer.
242
- if deps[cf] and HEADER_EXT[G.files[cf].path:match('%.(%w+)$') or ''] then return 50 end
286
+ if deps[cf] and (HEADER_EXT[G.files[cf].path:match('%.(%w+)$') or ''] or G.files[cf].on_demand or G.files[cf].cpp_fragment) then return 50 end
243
287
  return nil
244
288
  end
245
289
  if deps[cf] then return 50 end
@@ -283,25 +327,62 @@ local function is_decl(node) return node.kind == 'prototype' or node.decl end
283
327
  local scored_ids, scored_vals = {}, {}
284
328
 
285
329
  -- accept overrides the node kinds the ref kind may bind to (navigation only).
286
- local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired, accept, evidence, js_context)
287
- if kind == 'annotation' then return nil end
330
+ local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired, accept, evidence, js_context, line)
331
+ if kind == 'annotation' or kind == 'type' then return nil end
332
+ local macro_state
333
+ if kind == 'call' and C_FAMILY[G.files[f].language] and (not src_id or G.nodes[src_id].kind ~= 'macro') then
334
+ local macro, _, state = require('codeoutline.cpp_macros').resolve(G, f, name, line, js_context)
335
+ macro_state = state
336
+ if macro then return macro, #macro > 0 and 'visible macro' or 'unknown or unsupported macro visibility', macro_state end
337
+ if macro == false then accept = accept or NO_MACRO end
338
+ end
288
339
  if evidence and evidence:sub(1, 7) == 'cs:new:' then kind = 'new' end
289
340
  accept = accept or (kind == 'new' and NEWABLE) or (kind == 'ref' and VALUE) or CALLABLE
341
+ -- A method value needs the same receiver checks as a method call; its
342
+ -- stored edge remains a reference and its target remains callable-only.
343
+ local function_value = kind == 'ref' and (G.files[f].language == 'javascript'
344
+ or G.files[f].language == 'typescript' or G.files[f].language == 'arkts'
345
+ or G.files[f].language == 'python' or G.files[f].language == 'java' or G.files[f].language == 'kotlin')
346
+ if function_value and recv then kind = 'member_call' end
290
347
  if evidence == '' then evidence = nil end
291
348
  local strategy = 'js/ts binding'
292
- local exact = js_resolution.resolve(G, f, name, kind, recv, evidence, js_context)
349
+ local exact, binding_reason = js_resolution.resolve(G, f, name, kind, recv, evidence, js_context)
350
+ if G.files[f].language == 'go' then
351
+ strategy = 'go method set'
352
+ exact, binding_reason = require('codeoutline.go_methods').resolve(G, f, name, recv, evidence, js_context)
353
+ if exact == nil then
354
+ strategy = 'go package binding'
355
+ exact, binding_reason = require('codeoutline.go_modules').resolve(G, f, name, recv, js_context)
356
+ end
357
+ end
358
+ if G.files[f].language == 'rust' and js_context then
359
+ strategy = 'cargo crate/module binding'
360
+ exact, binding_reason = require('codeoutline.rust_resolution').resolve(G, f, src_id, name, recv, evidence, js_context)
361
+ end
293
362
  if exact == nil and G.files[f].language == 'python' then
294
363
  strategy = 'python binding'
295
364
  exact = require('codeoutline.python_bindings').resolve(G, f, evidence, js_context)
296
365
  end
366
+ if exact == nil and C_FAMILY[G.files[f].language] and evidence and evidence:match('^cxx:expr:') then
367
+ strategy = 'c/c++ call-result receiver'
368
+ exact, binding_reason = require('codeoutline.cpp_receivers').context(G, js_context).resolve(f, src_id, name, evidence)
369
+ end
297
370
  if exact == nil and G.files[f].language == 'cpp' then
298
- strategy = 'c++ declared receiver'
299
- exact = require('codeoutline.cpp_bindings').resolve(G, f, src_id, name, evidence, deps, js_context)
371
+ strategy = evidence and evidence:match('^cpp:name:') and 'c++ namespace/import' or 'c++ declared receiver'
372
+ exact, binding_reason = require('codeoutline.cpp_bindings').resolve(G, f, src_id, name, evidence, deps, js_context, accept)
300
373
  end
301
374
  if exact == nil and G.files[f].language == 'csharp' then
302
375
  strategy = 'c# binding'
303
376
  exact = require('codeoutline.csharp_bindings').resolve(G, f, src_id, name, kind, recv, evidence, js_context)
304
377
  end
378
+ if exact == nil and (G.files[f].language == 'java' or G.files[f].language == 'kotlin') then
379
+ strategy = 'declared JVM receiver'
380
+ exact = require('codeoutline.implementations').resolve(G, f, src_id, name, evidence, js_context)
381
+ if function_value and recv and exact == nil then
382
+ exact = require('codeoutline.implementations').resolve(G, f, src_id, name,
383
+ 'jvm:type:' .. recv .. '\0' .. '0', js_context)
384
+ end
385
+ end
305
386
  if exact == nil then
306
387
  strategy = 'lua binding'
307
388
  exact = lua_bindings.resolve(G, f, name, evidence, js_context and js_context.lua, recv)
@@ -309,7 +390,10 @@ local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired, accept,
309
390
  if exact ~= nil then
310
391
  local result = {}
311
392
  for _, id in ipairs(exact) do if accept[G.nodes[id].kind] then result[#result + 1] = id end end
312
- return result, #result == 0 and 'binding target unresolved' or strategy
393
+ return result, #result == 0 and (binding_reason or 'binding target unresolved') or strategy, macro_state
394
+ end
395
+ if function_value and recv and recv ~= 'self' and recv ~= 'cls' and recv ~= 'this' then
396
+ return {}, 'unresolved function-value receiver'
313
397
  end
314
398
  local cands = G.by_name[name]
315
399
  if G.files[f].language == 'kotlin' and not recv then
@@ -325,7 +409,7 @@ local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired, accept,
325
409
  end
326
410
  end
327
411
  end
328
- if not cands then return nil, 'no indexed target' end
412
+ if not cands then return nil, 'no indexed target', macro_state end
329
413
  -- Score first: score() rejects other languages and statics of other
330
414
  -- files, so "is there a real definition?" is only asked among the
331
415
  -- candidates this call could actually reach. (Asking it over all
@@ -342,6 +426,8 @@ local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired, accept,
342
426
  local cid = cands[i]
343
427
  local node = G.nodes[cid]
344
428
  if cid ~= src_id and accept[node.kind]
429
+ and (G.files[f].language ~= 'lua' or not node['local'] or G.node_file[cid] == f
430
+ or lua_bindings.exported(node, name, G.lua_global_export_tables))
345
431
  and (not local_only or (locality(G, f, G.node_file[cid], node, deps, paired) or 0) > 0) then
346
432
  local s = score(G, f, src_id, kind, recv, cid, deps, paired, owner)
347
433
  if s then
@@ -369,12 +455,12 @@ local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired, accept,
369
455
  -- A lone candidate far away (score 0) is still a match for a unique
370
456
  -- name; many far candidates with nothing to separate them is a guess.
371
457
  if best and best <= 0 and #ties > 1 then
372
- return nil, 'ambiguous name candidates' .. (#cands > M.MAX_CANDIDATES and '; candidate scan limit reached' or '')
458
+ return nil, 'ambiguous name candidates' .. (#cands > M.MAX_CANDIDATES and '; candidate scan limit reached' or ''), macro_state
373
459
  end
374
460
  local reason = #ties == 0 and 'no eligible target' or #ties > 1 and 'multiple name/locality candidates' or 'name/locality match'
375
461
  if #cands > M.MAX_CANDIDATES then reason = reason .. '; candidate scan limit reached' end
376
462
  if tie_count > M.MAX_TIES then reason = reason .. '; tied candidate limit reached' end
377
- return ties, reason
463
+ return ties, reason, macro_state
378
464
  end
379
465
 
380
466
  -- Files compare by path; nodes by file path, then node order (ids ascend
@@ -536,34 +622,93 @@ function M.query(symbols, opts)
536
622
  'max_expanded must be a positive integer')
537
623
  local Q = setmetatable({ out = {}, inn = {},
538
624
  stats = { expanded = 0, incoming_scans = 0, resolved_refs = 0 }, incomplete = false }, { __index = symbols })
625
+ local reasons = {}; Q.stats.incomplete_reasons = reasons
626
+ local function incomplete(reason) Q.incomplete = true; reasons[reason] = true end
627
+ local resolution, observe = require('codeoutline.resolution').new()
628
+ Q.stats.resolution = resolution
539
629
  local dependencies, references = {}, {}
540
630
  local js_context = js_resolution.context(symbols)
631
+ js_context.include_paths = opts.include_paths
541
632
  js_context.lua = lua_bindings.context(symbols)
542
633
  js_context.ordinary = require('codeoutline.ordinary_bindings').context(symbols, js_context)
634
+ js_context.lua.ordinary = js_context.ordinary
543
635
  Q.stats.ordinary = js_context.ordinary.stats
636
+ local macros = require('codeoutline.cpp_macros').context(symbols, js_context)
637
+ Q.stats.macros = macros.stats
638
+ Q.stats.workspace = js_context.workspace_stats
639
+ Q.stats.go = js_context.go_stats
640
+ Q.stats.rust = js_context.rust_stats
544
641
  local report = opts.explain and require('codeoutline.query_report').new() or nil
545
642
  Q.report = report
643
+ if report then
644
+ local add = report.add
645
+ function report:add(entry)
646
+ -- Ordinary sites carry a reference index and are counted below.
647
+ -- Dispatch collectors can instead report an unresolved synthetic site.
648
+ if entry.reason and not entry.target and not entry.reference then
649
+ observe('dispatch:' .. entry.source, (entry.line or 0) .. ':' .. (entry.call or ''), 'call', nil, entry.reason)
650
+ end
651
+ return add(self, entry)
652
+ end
653
+ end
546
654
  local indirect = require('codeoutline.indirect').query(symbols, opts.dispatch_rules, report)
547
- local static_dispatch = require('codeoutline.static_dispatch').query(symbols, report)
655
+ local static_dispatch = require('codeoutline.static_dispatch').query(symbols, report, js_context)
656
+ local implementations = require('codeoutline.implementations').context(symbols, js_context, report)
657
+ Q.stats.implementations = implementations.stats
658
+ local events = require('codeoutline.automatic_events').context(symbols, js_context, report)
659
+ Q.stats.events = events.stats
660
+ local callback_loops = require('codeoutline.lua_callback_loops').context(symbols, js_context, report)
661
+ Q.stats.callback_loops = callback_loops.stats
662
+ local function context_limits()
663
+ if js_context.include_incomplete then incomplete('include_closure_limit') end
664
+ if js_context.workspace_incomplete then incomplete('js_workspace_analysis_incomplete') end
665
+ if js_context.go_incomplete then incomplete('go_module_analysis_incomplete') end
666
+ if js_context.go_methods_incomplete then incomplete('go_method_set_analysis_incomplete') end
667
+ if js_context.cpp_receivers_incomplete then incomplete('cpp_receiver_analysis_incomplete') end
668
+ if js_context.rust_incomplete then incomplete('rust_resolution_incomplete') end
669
+ end
548
670
  local function deps(f)
549
671
  local entry = dependencies[f]
550
672
  if not entry then
551
- local direct, paired = resolve_deps(symbols, f, js_context)
673
+ local direct, paired, incomplete = M.visible_dependencies(symbols, f, js_context)
674
+ if incomplete then Q.incomplete = true; reasons.include_closure_limit = true end
552
675
  entry = { direct, paired }; dependencies[f] = entry
553
676
  end
554
677
  return entry[1], entry[2]
555
678
  end
556
679
  local function targets(f, i, explain)
557
- if static_dispatch.handles(f, i) then return {} end
680
+ if symbols.files[f].refs.kind[i] == 'type' then return {} end
681
+ if static_dispatch.handles(f, i) or symbols.files[f].language == 'lua'
682
+ and symbols.files[f].refs.kind[i] == 'call' and callback_loops.handles(f, i) then
683
+ if callback_loops.incomplete then incomplete('lua_callback_analysis_incomplete') end
684
+ return {}
685
+ end
558
686
  local rec, ids = symbols.files[f], symbols.id_of[f]
559
687
  local refs = rec.refs
560
688
  local direct, paired = deps(f)
561
689
  Q.stats.resolved_refs = Q.stats.resolved_refs + 1
562
- local found, reason = resolve_ref(symbols, f, ids[refs.from[i]], refs.name[i], refs.kind[i],
563
- refs.recv[i] or nil, direct, paired, nil, js_context.ordinary.evidence(f, i), js_context)
690
+ local evidence = js_context.ordinary.evidence(f, i)
691
+ local found, reason, macro_state = resolve_ref(symbols, f, ids[refs.from[i]], refs.name[i], refs.kind[i],
692
+ refs.recv[i] or nil, direct, paired, nil, evidence, js_context, refs.line[i])
693
+ if refs.kind[i] == 'call' and found then
694
+ local nominal = false
695
+ for _, id in ipairs(found) do
696
+ local kind = symbols.nodes[id].kind
697
+ if kind == 'class' or kind == 'struct' or kind == 'interface' then nominal = true; break end
698
+ end
699
+ if nominal then
700
+ if (js_context.ordinary.data(f).type_exclusions or {})[i] then return {} end
701
+ end
702
+ end
703
+ observe(f, i, refs.kind[i], found, reason)
704
+ if reason and reason:find('candidate scan limit reached', 1, true) then incomplete('candidate_scan_limit') end
705
+ if reason and reason:find('tied candidate limit reached', 1, true) then incomplete('tied_candidate_limit') end
706
+ if macros.incomplete then incomplete('cpp_macro_analysis_incomplete') end
707
+ context_limits()
564
708
  if report and explain and (refs.kind[i] == 'call' or refs.kind[i] == 'member_call') then
565
709
  if (not found or #found == 0) and explain == true then
566
- report:add({ source = ids[refs.from[i]], line = refs.line[i], call = refs.name[i], reason = reason or 'unresolved reference' })
710
+ report:add({ source = ids[refs.from[i]], line = refs.line[i], call = refs.name[i], reason = reason or 'unresolved reference',
711
+ reference = i, binding = evidence, macro_state = macro_state })
567
712
  else
568
713
  for _, dst in ipairs(found or {}) do
569
714
  if explain == true or explain[dst] then
@@ -574,9 +719,14 @@ function M.query(symbols, opts)
574
719
  end
575
720
  return found
576
721
  end
722
+ -- Opt-in inspection of already reported failures. Suggestions never load
723
+ -- outgoing/incoming edges or change the execution resolver's results.
724
+ function Q:definition_candidates()
725
+ return require('codeoutline.definition_candidates').suggest(self, report, js_context)
726
+ end
577
727
  setmetatable(Q.out, { __index = function(cache, id)
578
728
  control.check()
579
- if Q.stats.expanded >= limit then Q.incomplete = true; return nil end
729
+ if Q.stats.expanded >= limit then incomplete('graph_expansion_limit'); return nil end
580
730
  Q.stats.expanded = Q.stats.expanded + 1
581
731
  local f = assert(symbols.node_file[id], 'unknown symbol')
582
732
  local rec, ids = symbols.files[f], symbols.id_of[f]
@@ -590,6 +740,12 @@ function M.query(symbols, opts)
590
740
  references[f] = grouped
591
741
  end
592
742
  local edges = {}
743
+ for _, dst in ipairs(implementations.outgoing(id)) do
744
+ edges[#edges + 1] = dst
745
+ edges[#edges + 1] = symbols.nodes[id].line
746
+ edges[#edges + 1] = 'implementation'
747
+ end
748
+ if implementations.incomplete then incomplete('implementation_analysis_incomplete') end
593
749
  for _, edge in ipairs(indirect.outgoing(id)) do
594
750
  edges[#edges + 1] = edge.target
595
751
  edges[#edges + 1] = edge.line
@@ -600,6 +756,18 @@ function M.query(symbols, opts)
600
756
  edges[#edges + 1] = edge.line
601
757
  edges[#edges + 1] = edge.kind
602
758
  end
759
+ for _, edge in ipairs(events.outgoing(id)) do
760
+ edges[#edges + 1] = edge.target
761
+ edges[#edges + 1] = edge.line
762
+ edges[#edges + 1] = edge.kind
763
+ end
764
+ if events.incomplete then incomplete('automatic_event_analysis_incomplete') end
765
+ for _, edge in ipairs(callback_loops.outgoing(id)) do
766
+ edges[#edges + 1] = edge.target
767
+ edges[#edges + 1] = edge.line
768
+ edges[#edges + 1] = edge.kind
769
+ end
770
+ if callback_loops.incomplete then incomplete('lua_callback_analysis_incomplete') end
603
771
  for _, i in ipairs(grouped[id] or {}) do
604
772
  control.check()
605
773
  for _, dst in ipairs(targets(f, i, true) or {}) do
@@ -612,51 +780,136 @@ function M.query(symbols, opts)
612
780
  return edges
613
781
  end })
614
782
  -- Resolve all requested callers with a single ordered multi-name scan.
615
- function Q:load_incoming(ids)
616
- local wanted, names, found = {}, {}, false
783
+ function Q:load_incoming(ids, progress)
784
+ local wanted, names, family_names, local_names, found = {}, {}, {}, {}, false
617
785
  for _, id in ipairs(ids) do
618
786
  if rawget(self.inn, id) == nil then
619
787
  wanted[id], names[symbols.nodes[id].name], found = true, true, true
620
- for _, alias in ipairs(lua_bindings.aliases(symbols.nodes[id], symbols.lua_global_export_tables)) do names[alias] = true end
788
+ local f = symbols.node_file[id]
789
+ local rec, node = symbols.files[f], symbols.nodes[id]
790
+ local lang = family(rec.language)
791
+ -- The generic resolver cannot reach a translation-unit static
792
+ -- from another source file. Header statics remain visible.
793
+ local private = C_FAMILY[rec.language] and node.static
794
+ and not HEADER_EXT[rec.path:match('%.(%w+)$') or '']
795
+ local bucket = private and local_names or family_names
796
+ local key = private and f or lang
797
+ bucket[key] = bucket[key] or {}
798
+ bucket[key][node.name] = true
799
+ for _, alias in ipairs(lua_bindings.aliases(symbols.nodes[id], symbols.lua_global_export_tables)) do
800
+ names[alias], bucket[key][alias] = true, true
801
+ end
621
802
  end
622
803
  end
623
804
  if not found then return end
624
805
  local incoming = {}
625
- for id in pairs(wanted) do incoming[id] = {} end
806
+ for id in pairs(wanted) do
807
+ incoming[id] = {}
808
+ for _, source in ipairs(implementations.incoming(id)) do
809
+ push3(incoming, id, source, symbols.nodes[source].line, 'implementation')
810
+ end
811
+ end
812
+ -- A bounded impact query may retain verified edges from a completed
813
+ -- prefix of the scan. They never enter the normal incoming cache.
814
+ if progress then progress(incoming) end
815
+ if implementations.incomplete then incomplete('implementation_analysis_incomplete') end
626
816
  local indirect_incoming = indirect.incoming(wanted)
817
+ for src, edges in pairs(callback_loops.incoming(wanted)) do
818
+ for _, edge in ipairs(edges) do push3(incoming, edge.target, src, edge.line, edge.kind) end
819
+ end
820
+ if callback_loops.incomplete then incomplete('lua_callback_analysis_incomplete') end
821
+ for src, edges in pairs(events.incoming(wanted)) do
822
+ for _, edge in ipairs(edges) do push3(incoming, edge.target, src, edge.line, edge.kind) end
823
+ end
824
+ if events.incomplete then incomplete('automatic_event_analysis_incomplete') end
627
825
  for src, edges in pairs(static_dispatch.incoming(wanted)) do
628
826
  for _, edge in ipairs(edges) do push3(incoming, edge.target, src, edge.line, edge.kind) end
629
827
  end
630
- for _, f in ipairs(symbols.file_order) do
631
- for _, src in ipairs(symbols.id_of[f]) do
632
- for _, edge in ipairs(indirect_incoming[src] or {}) do
633
- if wanted[edge.target] then push3(incoming, edge.target, src, edge.line, edge.kind) end
634
- end
828
+ local indirect_sources = {}
829
+ for src in pairs(indirect_incoming) do
830
+ if #indirect_sources % 128 == 0 then control.check() end
831
+ indirect_sources[#indirect_sources + 1] = src
832
+ end
833
+ table.sort(indirect_sources, function(a, b) return M.before(symbols, a, b) end)
834
+ for _, src in ipairs(indirect_sources) do
835
+ control.check()
836
+ for _, edge in ipairs(indirect_incoming[src]) do
837
+ if wanted[edge.target] then push3(incoming, edge.target, src, edge.line, edge.kind) end
635
838
  end
636
839
  end
637
840
  self.stats.incoming_scans = self.stats.incoming_scans + 1
841
+ -- Lua modules can target Lua files or native files identified by
842
+ -- luaopen_* (the same resident evidence used by foreign.modules).
843
+ -- Other targets cannot be reached through Lua import/local bindings.
844
+ local native_targets, checked_native = false, {}
845
+ local lua_names = {}
846
+ for id in pairs(wanted) do
847
+ local f = symbols.node_file[id]
848
+ local rec = symbols.files[f]
849
+ if rec.language == 'lua' then
850
+ local local_index = id - symbols.id_of[f][1] + 1
851
+ for name, n in pairs(rec.lua_exports or {}) do if n == local_index then lua_names[name] = true end end
852
+ local locals = rec.lua_locals or rec.lua_binding_names and js_context.ordinary.data(f).lua_locals or {}
853
+ for _, members in ipairs(locals) do
854
+ for name, n in pairs(members) do if n == local_index then lua_names[name] = true end end
855
+ end
856
+ end
857
+ if C_FAMILY[rec.language] and not checked_native[f] then
858
+ checked_native[f] = true
859
+ for _, node in ipairs(rec.nodes) do
860
+ if node.name:match('^luaopen_(.+)$') then native_targets = true; break end
861
+ end
862
+ end
863
+ end
638
864
  -- Hot loop over every reference: numeric loops, hoisted fields, and the
639
865
  -- selective name test first. Checkpoints run per file and per chunk.
640
866
  local files = symbols.files
641
867
  local candidates = js_context.ordinary.candidates(wanted)
868
+ context_limits()
642
869
  for _, f in ipairs(symbols.file_order) do
643
870
  control.check()
871
+ local language = files[f].language
872
+ local file_names = family_names[family(language)]
873
+ local private_names = local_names[f]
874
+ if language == 'lua' and native_targets then file_names = names end
875
+ if not file_names and not private_names then goto next_incoming_file end
876
+ file_names = file_names or {}
644
877
  local alias_refs = js_context.ordinary.aliases(f, wanted, candidates)
878
+ if files[f].language == 'rust' then
879
+ for i in pairs(require('codeoutline.rust_resolution').alias_refs(symbols, f, wanted, js_context)) do alias_refs[i] = true end
880
+ end
645
881
  local aliases = {}
646
882
  if files[f].language == 'kotlin' then
647
883
  for _, imp in ipairs(files[f].imports) do
648
- if imp.alias and names[imp.path:match('([^.]+)$')] then aliases[imp.alias] = true end
884
+ if imp.alias and file_names[imp.path:match('([^.]+)$')] then aliases[imp.alias] = true end
649
885
  end
650
886
  end
651
887
  local refs = files[f].refs
888
+ local lua_values = {}
889
+ for index, imp in ipairs(files[f].lua_imports or {}) do
890
+ if imp.member and (native_targets or lua_names[imp.member]) then lua_values['lua-value:' .. index] = true end
891
+ end
892
+ local lua_evidence = refs.lua_binding
893
+ local imported_native = false
894
+ if native_targets and not lua_evidence then
895
+ for _, imp in ipairs(files[f].lua_imports or {}) do
896
+ for _, target in ipairs(lua_bindings.modules(symbols, imp, js_context.lua)) do
897
+ if checked_native[target] then imported_native = true; break end
898
+ end
899
+ if imported_native then break end
900
+ end
901
+ end
902
+ if (next(lua_values) or imported_native) and not lua_evidence then lua_evidence = js_context.ordinary.data(f).refs.lua_binding end
652
903
  local ref_names, froms = refs.name, refs.from
653
904
  local count = #ref_names
654
905
  for first = 1, count, 4096 do
655
906
  if first > 1 then control.check() end
656
907
  for i = first, math.min(count, first + 4095) do
657
- if (names[ref_names[i]] or aliases[ref_names[i]] or aliases[refs.recv[i]]
908
+ if (file_names[ref_names[i]] or private_names and private_names[ref_names[i]]
909
+ or aliases[ref_names[i]] or aliases[refs.recv[i]]
658
910
  or alias_refs[i]
659
- or (refs.lua_binding and refs.lua_binding[i] ~= '')) and froms[i] > 0 then
911
+ or (lua_evidence and lua_evidence[i] ~= '' and
912
+ (native_targets or lua_names[ref_names[i]] or lua_values[lua_evidence[i]]))) and froms[i] > 0 then
660
913
  for _, dst in ipairs(targets(f, i, wanted) or {}) do
661
914
  if wanted[dst] then
662
915
  push3(incoming, dst, symbols.id_of[f][froms[i]], refs.line[i], refs.kind[i])
@@ -665,8 +918,10 @@ function M.query(symbols, opts)
665
918
  end
666
919
  end
667
920
  end
921
+ ::next_incoming_file::
668
922
  end
669
923
  -- Interrupted scans never publish partial caller lists.
924
+ context_limits()
670
925
  for id, edges in pairs(incoming) do rawset(self.inn, id, edges) end
671
926
  end
672
927
  setmetatable(Q.inn, { __index = function(cache, id)