auryn-cpp 0.1.0__py3-none-any.whl
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.
- auryn/ingestors/cpp/__init__.py +0 -0
- auryn/ingestors/cpp/file_parser.py +511 -0
- auryn/ingestors/cpp/git_sync.py +212 -0
- auryn/ingestors/cpp/grammar.py +14 -0
- auryn/ingestors/cpp/ids.py +80 -0
- auryn/ingestors/cpp/incremental.py +332 -0
- auryn/ingestors/cpp/plugin.py +43 -0
- auryn/ingestors/cpp/raw_model.py +136 -0
- auryn/ingestors/cpp/resolution.py +893 -0
- auryn/ingestors/cpp/schema.py +72 -0
- auryn_cpp-0.1.0.dist-info/METADATA +11 -0
- auryn_cpp-0.1.0.dist-info/RECORD +15 -0
- auryn_cpp-0.1.0.dist-info/WHEEL +4 -0
- auryn_cpp-0.1.0.dist-info/entry_points.txt +2 -0
- auryn_cpp-0.1.0.dist-info/licenses/LICENSE +557 -0
|
@@ -0,0 +1,893 @@
|
|
|
1
|
+
"""Project-wide pass (phase 2): turn the per-file RawFile list into graph nodes/edges, applying
|
|
2
|
+
the cross-file decisions a single file can't -- materializing the namespace tier, wiring each
|
|
3
|
+
out-of-line `Class::method` definition back to the class it belongs to (the key C++-specific
|
|
4
|
+
challenge), resolving EXTENDS/DEPENDS_ON to project types, and best-effort call resolution.
|
|
5
|
+
|
|
6
|
+
C++ is the structural union of the C and Java ingestors, so this pass is too: it carries Java's
|
|
7
|
+
namespace/type/member machinery (the tier here is `Namespace`, chained by HAS_NAMESPACE/
|
|
8
|
+
HAS_SUB_NAMESPACE) *and* C's free-function/macro/#include layer. Two C++-specific twists:
|
|
9
|
+
|
|
10
|
+
* out-of-line definitions -- `double geo::Shape::area() const {..}` is written at namespace or
|
|
11
|
+
global scope, physically apart from `class Shape`. We split its `qualified_identifier`, match
|
|
12
|
+
the owning type by simple name (preferring the enclosing namespace), and emit its Method
|
|
13
|
+
under that class -- crucially with the *same* method id as the in-class declaration, so the
|
|
14
|
+
declaration and the out-of-line definition merge into one node (as C merges a prototype with
|
|
15
|
+
its definition). A qualified def whose owner isn't a project type degrades to a free function.
|
|
16
|
+
* templates -- a class/method template's arity is folded into the type id (via ids.py), so
|
|
17
|
+
`Box` and `Box<T>` can't collide (mirroring Java's generics handling).
|
|
18
|
+
|
|
19
|
+
C++ has no interfaces, so there is **no IMPLEMENTS** edge: an abstract/pure-virtual "interface"
|
|
20
|
+
class is just a Class, and deriving from it is still EXTENDS.
|
|
21
|
+
|
|
22
|
+
Name-based only -- this is the fast tree-sitter engine, so every CALLS/DEPENDS_ON edge is stamped
|
|
23
|
+
`resolution="name"`. A future libclang deep pass (template instantiation, overload resolution,
|
|
24
|
+
ADL, precise header linking) would re-emit the same node ids with a stronger provenance (the
|
|
25
|
+
two-phase model). Ambiguous or unresolved references produce no edge rather than a wrong guess --
|
|
26
|
+
the same trade-off as the C/Java/C# ingestors."""
|
|
27
|
+
|
|
28
|
+
from __future__ import annotations
|
|
29
|
+
|
|
30
|
+
import posixpath
|
|
31
|
+
import re
|
|
32
|
+
from collections import defaultdict
|
|
33
|
+
from collections.abc import Iterator
|
|
34
|
+
from dataclasses import dataclass, field
|
|
35
|
+
|
|
36
|
+
from auryn.core.graph_model import GraphEdge, GraphNode
|
|
37
|
+
from auryn.ingestors.cpp import ids
|
|
38
|
+
from auryn.ingestors.cpp.raw_model import RawCall, RawFile, RawFunction, RawMethod, RawType
|
|
39
|
+
|
|
40
|
+
_IDENTIFIER_RE = re.compile(r"[A-Za-z_][A-Za-z0-9_]*")
|
|
41
|
+
# Type tokens that are never a project type -- C++ primitives/keywords appearing in type text.
|
|
42
|
+
_NON_TYPE_TOKENS = frozenset(
|
|
43
|
+
{"void", "int", "long", "short", "char", "bool", "float", "double", "signed", "unsigned",
|
|
44
|
+
"const", "volatile", "auto", "wchar_t", "char8_t", "char16_t", "char32_t", "size_t",
|
|
45
|
+
"struct", "class", "enum", "union", "typename", "static", "mutable", "constexpr"}
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass
|
|
50
|
+
class _Entry:
|
|
51
|
+
type_id: str
|
|
52
|
+
kind: str # "Class" | "Struct" | "Union" | "Enum"
|
|
53
|
+
name: str
|
|
54
|
+
namespace: str
|
|
55
|
+
raw_type: RawType
|
|
56
|
+
file_id: str
|
|
57
|
+
parent_type_id: str | None
|
|
58
|
+
scope_id: str
|
|
59
|
+
scope_label: str | None # "Namespace" | "Project" (top-level only; None if nested)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@dataclass
|
|
63
|
+
class _MethodRec:
|
|
64
|
+
"""One method after merging an (optional) in-class declaration with an (optional) out-of-line
|
|
65
|
+
definition into a single node (definition wins for body/source/calls)."""
|
|
66
|
+
|
|
67
|
+
method_id: str
|
|
68
|
+
name: str
|
|
69
|
+
kind: str
|
|
70
|
+
return_type: str | None
|
|
71
|
+
params: list # RawParameter
|
|
72
|
+
is_static: bool
|
|
73
|
+
is_virtual: bool
|
|
74
|
+
is_const: bool
|
|
75
|
+
defined: bool
|
|
76
|
+
calls: list # RawCall
|
|
77
|
+
source: str
|
|
78
|
+
doc: str
|
|
79
|
+
line_start: int
|
|
80
|
+
line_end: int
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@dataclass
|
|
84
|
+
class _FuncRec:
|
|
85
|
+
function_id: str
|
|
86
|
+
scope_id: str
|
|
87
|
+
scope_label: str
|
|
88
|
+
namespace: str
|
|
89
|
+
raw: RawFunction
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@dataclass
|
|
93
|
+
class _MethodIndexEntry:
|
|
94
|
+
owner_type_id: str
|
|
95
|
+
method_id: str
|
|
96
|
+
kind: str
|
|
97
|
+
namespace: str
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@dataclass
|
|
101
|
+
class _TypeStub:
|
|
102
|
+
"""A non-emit type seeded from the graph for incremental resolution -- duck-compatible with
|
|
103
|
+
`_Entry` everywhere `_resolve_type` is used (only .type_id/.kind/.name/.namespace are read)."""
|
|
104
|
+
type_id: str
|
|
105
|
+
kind: str
|
|
106
|
+
name: str
|
|
107
|
+
namespace: str
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@dataclass
|
|
111
|
+
class _FuncStub:
|
|
112
|
+
"""A non-emit free function seeded from the graph -- duck-compatible with `_FuncRec` everywhere
|
|
113
|
+
`_pick_function`/call resolution reads it (only .function_id/.namespace)."""
|
|
114
|
+
function_id: str
|
|
115
|
+
namespace: str
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@dataclass
|
|
119
|
+
class CppGraphIndex:
|
|
120
|
+
"""Resolution tables seeded from the GRAPH for the NON-emit files (ADR 0005), so
|
|
121
|
+
`resolve_incremental` resolves an emit file's cross-file EXTENDS/DEPENDS_ON/CALLS/#include against
|
|
122
|
+
the surviving project without re-parsing it. Reconstructed by `cpp.incremental.read_cpp_index`."""
|
|
123
|
+
type_index: dict = field(default_factory=dict) # simple name -> [_TypeStub]
|
|
124
|
+
method_index: dict = field(default_factory=dict) # method name -> [_MethodIndexEntry] (incl ctors)
|
|
125
|
+
function_index: dict = field(default_factory=dict) # function name -> [_FuncStub]
|
|
126
|
+
include_fullpaths: dict = field(default_factory=dict) # normalized full path -> File id (NO basename keys;
|
|
127
|
+
# basename collapse happens in resolve_incremental over the seed+emit UNION, so a basename shared by a
|
|
128
|
+
# surviving and an emit file is correctly seen as ambiguous -- matching the full path, see _collapse_basenames)
|
|
129
|
+
# ids DEFINED by a surviving (non-emit) file: a forward decl / prototype in the emit set for one of
|
|
130
|
+
# these emits only its DECLARES provenance, never a sparse node that would clobber the real definition.
|
|
131
|
+
defined_type_ids: frozenset = frozenset()
|
|
132
|
+
defined_function_ids: frozenset = frozenset()
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def resolve(raw_files: list[RawFile], project_id: str) -> Iterator[GraphNode | GraphEdge]:
|
|
136
|
+
# Stable path order so a symbol DEFINED in several files (multi-program dirs: three `class Benchmark`
|
|
137
|
+
# benchmark mains, several `main`) reduces to an order-INDEPENDENT winner -- identical here and in
|
|
138
|
+
# resolve_incremental's emit subset, the precondition for incremental == full (ADR 0005, as C/Java do).
|
|
139
|
+
raw_files = sorted(raw_files, key=lambda rf: _norm(rf.path))
|
|
140
|
+
file_ids: dict[str, str] = {}
|
|
141
|
+
namespaces_seen: set[str] = set()
|
|
142
|
+
# Per-entity file provenance (ADR 0005): entity_id -> {file_id -> is_definition}. A file that
|
|
143
|
+
# DEFINES an entity (`True`, class body / method or function body) emits File-DEFINES; one that only
|
|
144
|
+
# DECLARES it (`False`, forward decl / in-class method decl whose body is out-of-line / prototype)
|
|
145
|
+
# emits File-DECLARES. Namespaces have no decl/def split -> a plain set of contributing files.
|
|
146
|
+
type_prov: dict[str, tuple[str, dict[str, bool]]] = {}
|
|
147
|
+
method_prov: dict[str, tuple[str, dict[str, bool]]] = {}
|
|
148
|
+
func_prov: dict[str, tuple[str, dict[str, bool]]] = {}
|
|
149
|
+
ns_prov: dict[str, set[str]] = defaultdict(set)
|
|
150
|
+
|
|
151
|
+
def note_ns(ns: str, fid: str) -> None:
|
|
152
|
+
namespaces_seen.add(ns)
|
|
153
|
+
prefix = ""
|
|
154
|
+
for segment in ns.split("::"):
|
|
155
|
+
prefix = f"{prefix}::{segment}" if prefix else segment
|
|
156
|
+
ns_prov[ids.namespace_id(project_id, prefix)].add(fid)
|
|
157
|
+
|
|
158
|
+
for rf in raw_files:
|
|
159
|
+
fid = ids.file_id(project_id, rf.path)
|
|
160
|
+
file_ids[rf.path] = fid
|
|
161
|
+
yield GraphNode("File", {"id": fid}, {"name": _basename(rf.path), "path": _norm(rf.path)})
|
|
162
|
+
yield GraphEdge("HAS_FILE", "Project", {"id": project_id}, "File", {"id": fid})
|
|
163
|
+
for t in rf.types:
|
|
164
|
+
if t.namespace:
|
|
165
|
+
note_ns(t.namespace, fid)
|
|
166
|
+
for f in rf.functions:
|
|
167
|
+
if f.namespace:
|
|
168
|
+
note_ns(f.namespace, fid)
|
|
169
|
+
yield from _emit_namespaces(namespaces_seen, project_id)
|
|
170
|
+
|
|
171
|
+
# Flatten every (possibly nested) type into a scoped entry.
|
|
172
|
+
entries: list[_Entry] = []
|
|
173
|
+
for rf in raw_files:
|
|
174
|
+
for rt in rf.types:
|
|
175
|
+
_flatten(rt, file_ids[rf.path], project_id, None, entries)
|
|
176
|
+
|
|
177
|
+
type_index: dict[str, list[_Entry]] = defaultdict(list)
|
|
178
|
+
for e in entries:
|
|
179
|
+
type_index[e.name].append(e)
|
|
180
|
+
_note(type_prov, e.type_id, e.kind, e.file_id, e.raw_type.is_definition)
|
|
181
|
+
# In-class method declarations/inline-definitions are provenanced to the type's file.
|
|
182
|
+
for m in e.raw_type.methods:
|
|
183
|
+
mid = ids.method_id(e.type_id, m.name, [p.type for p in m.params])
|
|
184
|
+
_note(method_prov, mid, "Method", e.file_id, m.is_definition)
|
|
185
|
+
|
|
186
|
+
# Wire out-of-line member definitions back to their owning class (or degrade to free fns).
|
|
187
|
+
defs_by_type: dict[str, list[tuple[str, RawMethod]]] = defaultdict(list)
|
|
188
|
+
extra_functions: list[tuple[str, RawFunction]] = [] # (rel_path, converted free function)
|
|
189
|
+
for rf in raw_files:
|
|
190
|
+
for md in rf.member_defs:
|
|
191
|
+
inferred_ns = _join_ns([md.namespace, *md.owner_scope[:-1]])
|
|
192
|
+
owner = _resolve_type(md.owner_name or "", inferred_ns, type_index)
|
|
193
|
+
if owner is not None:
|
|
194
|
+
defs_by_type[owner.type_id].append((file_ids[rf.path], md))
|
|
195
|
+
mid = ids.method_id(owner.type_id, md.name, [p.type for p in md.params])
|
|
196
|
+
_note(method_prov, mid, "Method", file_ids[rf.path], md.is_definition)
|
|
197
|
+
else: # e.g. `int myns::helper()` -- a namespace-qualified free function definition
|
|
198
|
+
extra_functions.append((rf.path, _method_to_function(md, inferred_ns)))
|
|
199
|
+
|
|
200
|
+
# Merge in-class declarations with out-of-line definitions -> one _MethodRec per method.
|
|
201
|
+
methods_by_type: dict[str, list[_MethodRec]] = {}
|
|
202
|
+
method_index: dict[str, list[_MethodIndexEntry]] = defaultdict(list)
|
|
203
|
+
for e in entries:
|
|
204
|
+
recs = _merge_methods(e, [md for _f, md in defs_by_type.get(e.type_id, [])])
|
|
205
|
+
methods_by_type[e.type_id] = recs
|
|
206
|
+
for r in recs:
|
|
207
|
+
method_index[r.name].append(_MethodIndexEntry(e.type_id, r.method_id, r.kind, e.namespace))
|
|
208
|
+
|
|
209
|
+
# Free functions (from files + degraded qualified defs), scoped to a namespace/project.
|
|
210
|
+
func_recs: list[_FuncRec] = []
|
|
211
|
+
function_index: dict[str, list[_FuncRec]] = defaultdict(list)
|
|
212
|
+
for rf in raw_files:
|
|
213
|
+
for fn in rf.functions:
|
|
214
|
+
fr = _make_func_rec(fn, project_id)
|
|
215
|
+
func_recs.append(fr)
|
|
216
|
+
_note(func_prov, fr.function_id, "Function", file_ids[rf.path], fn.is_definition)
|
|
217
|
+
for path, fn in extra_functions:
|
|
218
|
+
fr = _make_func_rec(fn, project_id)
|
|
219
|
+
func_recs.append(fr)
|
|
220
|
+
_note(func_prov, fr.function_id, "Function", file_ids[path], fn.is_definition)
|
|
221
|
+
# Deduplicate free functions sharing an id (a prototype + its definition), definition wins.
|
|
222
|
+
fr_by_id: dict[str, _FuncRec] = {}
|
|
223
|
+
for fr in func_recs:
|
|
224
|
+
prev = fr_by_id.get(fr.function_id)
|
|
225
|
+
if prev is None or (fr.raw.is_definition and not prev.raw.is_definition):
|
|
226
|
+
fr_by_id[fr.function_id] = fr
|
|
227
|
+
func_recs = list(fr_by_id.values())
|
|
228
|
+
for fr in func_recs:
|
|
229
|
+
function_index[fr.raw.name].append(fr)
|
|
230
|
+
|
|
231
|
+
# Pass 1: nodes + structural edges (so pass 2's cross-refs always have a landing node).
|
|
232
|
+
for e in entries:
|
|
233
|
+
yield from _emit_type_and_members(e, methods_by_type[e.type_id])
|
|
234
|
+
for fr in func_recs:
|
|
235
|
+
yield from _emit_function(fr)
|
|
236
|
+
|
|
237
|
+
# File provenance (ADR 0005): File-DEFINES/DECLARES -> every program-global entity + namespace.
|
|
238
|
+
yield from _emit_provenance(type_prov, method_prov, func_prov, ns_prov)
|
|
239
|
+
|
|
240
|
+
# Pass 2: hierarchy, type references, calls. `externs` dedups placeholder Function nodes for
|
|
241
|
+
# unresolved (std/system/other-repo) calls across the whole project (ADR 0004).
|
|
242
|
+
externs: set[str] = set()
|
|
243
|
+
for e in entries:
|
|
244
|
+
yield from _emit_hierarchy(e, type_index)
|
|
245
|
+
yield from _emit_type_references(e, methods_by_type[e.type_id], type_index)
|
|
246
|
+
yield from _emit_calls_for_type(e, methods_by_type[e.type_id], type_index, method_index, function_index, externs)
|
|
247
|
+
for fr in func_recs:
|
|
248
|
+
yield from _emit_function_depends_on(fr, type_index)
|
|
249
|
+
yield from _emit_function_calls(fr, type_index, method_index, function_index, externs)
|
|
250
|
+
|
|
251
|
+
# C-family: macros (file/preprocessor-scoped, mirrors the C ingestor) + resolved #includes.
|
|
252
|
+
yield from _emit_macros(raw_files, project_id)
|
|
253
|
+
yield from _emit_includes(raw_files, file_ids, _build_include_index(raw_files, file_ids))
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def resolve_incremental(emit_raws: list[RawFile], project_id: str, index: CppGraphIndex) -> Iterator[GraphNode | GraphEdge]:
|
|
257
|
+
"""Emit graph items for ONLY `emit_raws`, resolving cross-file references against `index` (the
|
|
258
|
+
graph-seeded non-emit symbols) merged with the emit files' own. The blast-radius analogue of
|
|
259
|
+
`resolve()` (ADR 0005) -- SAME emitters, scope seeded rather than whole-project. The engine's
|
|
260
|
+
closures guarantee every file co-authoring a shared node an emit file touches is in `emit_raws`:
|
|
261
|
+
for C++'s out-of-line methods that means BOTH the class-body file (.h) AND the definition file (.cc)
|
|
262
|
+
are always present together (forward co-member + reverse member-owner closures), so a method is never
|
|
263
|
+
sparsely re-emitted. A forward-declared type / free-function prototype whose DEFINITION is in a
|
|
264
|
+
surviving file emits its DECLARES provenance only (never a sparse node that would clobber the real
|
|
265
|
+
definition) -- mirrors C."""
|
|
266
|
+
emit_raws = sorted(emit_raws, key=lambda rf: _norm(rf.path)) # same stable order as resolve() (winner determinism)
|
|
267
|
+
file_ids = {rf.path: ids.file_id(project_id, rf.path) for rf in emit_raws}
|
|
268
|
+
type_prov: dict[str, tuple[str, dict[str, bool]]] = {}
|
|
269
|
+
method_prov: dict[str, tuple[str, dict[str, bool]]] = {}
|
|
270
|
+
func_prov: dict[str, tuple[str, dict[str, bool]]] = {}
|
|
271
|
+
ns_prov: dict[str, set[str]] = defaultdict(set)
|
|
272
|
+
namespaces_seen: set[str] = set()
|
|
273
|
+
|
|
274
|
+
def note_ns(ns: str, fid: str) -> None:
|
|
275
|
+
namespaces_seen.add(ns)
|
|
276
|
+
prefix = ""
|
|
277
|
+
for segment in ns.split("::"):
|
|
278
|
+
prefix = f"{prefix}::{segment}" if prefix else segment
|
|
279
|
+
ns_prov[ids.namespace_id(project_id, prefix)].add(fid)
|
|
280
|
+
|
|
281
|
+
for rf in emit_raws:
|
|
282
|
+
fid = file_ids[rf.path]
|
|
283
|
+
yield GraphNode("File", {"id": fid}, {"name": _basename(rf.path), "path": _norm(rf.path)})
|
|
284
|
+
yield GraphEdge("HAS_FILE", "Project", {"id": project_id}, "File", {"id": fid})
|
|
285
|
+
for t in rf.types:
|
|
286
|
+
if t.namespace:
|
|
287
|
+
note_ns(t.namespace, fid)
|
|
288
|
+
for f in rf.functions:
|
|
289
|
+
if f.namespace:
|
|
290
|
+
note_ns(f.namespace, fid)
|
|
291
|
+
yield from _emit_namespaces(namespaces_seen, project_id)
|
|
292
|
+
|
|
293
|
+
# Flatten the emit set's types, recording provenance.
|
|
294
|
+
entries: list[_Entry] = []
|
|
295
|
+
for rf in emit_raws:
|
|
296
|
+
for rt in rf.types:
|
|
297
|
+
_flatten(rt, file_ids[rf.path], project_id, None, entries)
|
|
298
|
+
emit_type_ids = {e.type_id for e in entries}
|
|
299
|
+
|
|
300
|
+
# Merged type index: seed stubs (non-emit) + the emit set's own entries. Both expose
|
|
301
|
+
# .type_id/.kind/.name/.namespace, which is all `_resolve_type` reads.
|
|
302
|
+
type_index: dict[str, list] = defaultdict(list)
|
|
303
|
+
for name, stubs in index.type_index.items():
|
|
304
|
+
type_index[name].extend(stubs)
|
|
305
|
+
for e in entries:
|
|
306
|
+
type_index[e.name].append(e)
|
|
307
|
+
_note(type_prov, e.type_id, e.kind, e.file_id, e.raw_type.is_definition)
|
|
308
|
+
for m in e.raw_type.methods:
|
|
309
|
+
mid = ids.method_id(e.type_id, m.name, [p.type for p in m.params])
|
|
310
|
+
_note(method_prov, mid, "Method", e.file_id, m.is_definition)
|
|
311
|
+
|
|
312
|
+
# Wire out-of-line member defs to their owning type via the MERGED index (so an emit .cc's method
|
|
313
|
+
# def binds to a type whether the class body is in the emit set or seeded).
|
|
314
|
+
defs_by_type: dict[str, list[tuple[str, RawMethod]]] = defaultdict(list)
|
|
315
|
+
extra_functions: list[tuple[str, RawFunction]] = []
|
|
316
|
+
for rf in emit_raws:
|
|
317
|
+
for md in rf.member_defs:
|
|
318
|
+
inferred_ns = _join_ns([md.namespace, *md.owner_scope[:-1]])
|
|
319
|
+
owner = _resolve_type(md.owner_name or "", inferred_ns, type_index)
|
|
320
|
+
if owner is not None:
|
|
321
|
+
defs_by_type[owner.type_id].append((file_ids[rf.path], md))
|
|
322
|
+
mid = ids.method_id(owner.type_id, md.name, [p.type for p in md.params])
|
|
323
|
+
_note(method_prov, mid, "Method", file_ids[rf.path], md.is_definition)
|
|
324
|
+
else:
|
|
325
|
+
extra_functions.append((rf.path, _method_to_function(md, inferred_ns)))
|
|
326
|
+
|
|
327
|
+
# Merge in-class decls with out-of-line defs per emit type; build the merged method index.
|
|
328
|
+
methods_by_type: dict[str, list[_MethodRec]] = {}
|
|
329
|
+
method_index: dict[str, list[_MethodIndexEntry]] = defaultdict(list)
|
|
330
|
+
for name, es in index.method_index.items():
|
|
331
|
+
method_index[name].extend(es)
|
|
332
|
+
for e in entries:
|
|
333
|
+
recs = _merge_methods(e, [md for _f, md in defs_by_type.get(e.type_id, [])])
|
|
334
|
+
methods_by_type[e.type_id] = recs
|
|
335
|
+
for r in recs:
|
|
336
|
+
method_index[r.name].append(_MethodIndexEntry(e.type_id, r.method_id, r.kind, e.namespace))
|
|
337
|
+
|
|
338
|
+
# Free functions (emit files + degraded qualified defs); merged function index.
|
|
339
|
+
func_recs: list[_FuncRec] = []
|
|
340
|
+
function_index: dict[str, list] = defaultdict(list)
|
|
341
|
+
for name, stubs in index.function_index.items():
|
|
342
|
+
function_index[name].extend(stubs)
|
|
343
|
+
for rf in emit_raws:
|
|
344
|
+
for fn in rf.functions:
|
|
345
|
+
fr = _make_func_rec(fn, project_id)
|
|
346
|
+
func_recs.append(fr)
|
|
347
|
+
_note(func_prov, fr.function_id, "Function", file_ids[rf.path], fn.is_definition)
|
|
348
|
+
for path, fn in extra_functions:
|
|
349
|
+
fr = _make_func_rec(fn, project_id)
|
|
350
|
+
func_recs.append(fr)
|
|
351
|
+
_note(func_prov, fr.function_id, "Function", file_ids[path], fn.is_definition)
|
|
352
|
+
fr_by_id: dict[str, _FuncRec] = {}
|
|
353
|
+
for fr in func_recs:
|
|
354
|
+
prev = fr_by_id.get(fr.function_id)
|
|
355
|
+
if prev is None or (fr.raw.is_definition and not prev.raw.is_definition):
|
|
356
|
+
fr_by_id[fr.function_id] = fr
|
|
357
|
+
func_recs = list(fr_by_id.values())
|
|
358
|
+
for fr in func_recs:
|
|
359
|
+
function_index[fr.raw.name].append(fr)
|
|
360
|
+
|
|
361
|
+
# Pass 1: nodes + structural edges. A forward-decl of a type DEFINED in a surviving file emits no
|
|
362
|
+
# node (DECLARES provenance only, below) -- a sparse re-emit would clobber the real definition.
|
|
363
|
+
for e in entries:
|
|
364
|
+
if not e.raw_type.is_definition and e.type_id in index.defined_type_ids:
|
|
365
|
+
# Forward decl of a type DEFINED in a surviving file: emit no (sparse) node that would clobber
|
|
366
|
+
# the real definition, but still emit this file's DEFINED_IN edge -- a full resolve emits one
|
|
367
|
+
# per type occurrence (incl. forward decls), so omitting it would diverge (PR #18 review).
|
|
368
|
+
yield GraphEdge("DEFINED_IN", e.kind, {"id": e.type_id}, "File", {"id": e.file_id})
|
|
369
|
+
continue
|
|
370
|
+
yield from _emit_type_and_members(e, methods_by_type[e.type_id])
|
|
371
|
+
for fr in func_recs:
|
|
372
|
+
if not fr.raw.is_definition and fr.function_id in index.defined_function_ids:
|
|
373
|
+
continue # prototype of a function defined elsewhere -> DECLARES provenance only
|
|
374
|
+
yield from _emit_function(fr)
|
|
375
|
+
|
|
376
|
+
# Pass 2: hierarchy, type refs, calls -- resolved against the merged indexes.
|
|
377
|
+
externs: set[str] = set()
|
|
378
|
+
for e in entries:
|
|
379
|
+
yield from _emit_hierarchy(e, type_index)
|
|
380
|
+
yield from _emit_type_references(e, methods_by_type[e.type_id], type_index)
|
|
381
|
+
yield from _emit_calls_for_type(e, methods_by_type[e.type_id], type_index, method_index, function_index, externs)
|
|
382
|
+
for fr in func_recs:
|
|
383
|
+
yield from _emit_function_depends_on(fr, type_index)
|
|
384
|
+
yield from _emit_function_calls(fr, type_index, method_index, function_index, externs)
|
|
385
|
+
|
|
386
|
+
yield from _emit_macros(emit_raws, project_id)
|
|
387
|
+
# Includes resolve against the WHOLE project. Union the seed's full-path map with the emit files', THEN
|
|
388
|
+
# collapse basenames over that union -- so a basename shared by a surviving file and an emit file is seen
|
|
389
|
+
# as ambiguous (no bare-basename key), exactly as the full path sees it. Collapsing each half separately
|
|
390
|
+
# would resolve an #include the full recompute leaves dangling (incremental != full).
|
|
391
|
+
union_fullpaths = {**index.include_fullpaths, **_include_fullpaths(emit_raws, file_ids)}
|
|
392
|
+
yield from _emit_includes(emit_raws, file_ids, _collapse_basenames(union_fullpaths))
|
|
393
|
+
yield from _emit_provenance(type_prov, method_prov, func_prov, ns_prov)
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
# -- namespaces ----------------------------------------------------------------------------
|
|
397
|
+
|
|
398
|
+
def _emit_namespaces(namespaces: set[str], project_id: str) -> Iterator[GraphNode | GraphEdge]:
|
|
399
|
+
"""Materialize every prefix of every declared namespace (as Java does for packages / C# for
|
|
400
|
+
namespaces): the first segment hangs off the Project via HAS_NAMESPACE, deeper segments chain
|
|
401
|
+
via HAS_SUB_NAMESPACE. `a::b::c` -> Namespace(a) -> Namespace(a::b) -> Namespace(a::b::c)."""
|
|
402
|
+
emitted: set[str] = set()
|
|
403
|
+
for ns in namespaces:
|
|
404
|
+
prefix = ""
|
|
405
|
+
parent_id, parent_label, parent_rel = project_id, "Project", "HAS_NAMESPACE"
|
|
406
|
+
for segment in ns.split("::"):
|
|
407
|
+
prefix = f"{prefix}::{segment}" if prefix else segment
|
|
408
|
+
nid = ids.namespace_id(project_id, prefix)
|
|
409
|
+
if nid not in emitted:
|
|
410
|
+
emitted.add(nid)
|
|
411
|
+
yield GraphNode("Namespace", {"id": nid}, {"name": segment, "qualified_name": prefix})
|
|
412
|
+
yield GraphEdge(parent_rel, parent_label, {"id": parent_id}, "Namespace", {"id": nid})
|
|
413
|
+
parent_id, parent_label, parent_rel = nid, "Namespace", "HAS_SUB_NAMESPACE"
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
# -- flattening ----------------------------------------------------------------------------
|
|
417
|
+
|
|
418
|
+
def _flatten(rt: RawType, file_id: str, project_id: str, parent_type_id: str | None, entries: list[_Entry]) -> None:
|
|
419
|
+
if parent_type_id is not None:
|
|
420
|
+
tid = ids.type_id(parent_type_id, rt.kind, rt.name, rt.type_parameters)
|
|
421
|
+
scope_id, scope_label = parent_type_id, None
|
|
422
|
+
elif rt.namespace:
|
|
423
|
+
scope_id = ids.namespace_id(project_id, rt.namespace)
|
|
424
|
+
scope_label = "Namespace"
|
|
425
|
+
tid = ids.type_id(scope_id, rt.kind, rt.name, rt.type_parameters)
|
|
426
|
+
else:
|
|
427
|
+
scope_id, scope_label = project_id, "Project"
|
|
428
|
+
tid = ids.type_id(scope_id, rt.kind, rt.name, rt.type_parameters)
|
|
429
|
+
|
|
430
|
+
entries.append(_Entry(tid, rt.kind, rt.name, rt.namespace, rt, file_id, parent_type_id, scope_id, scope_label))
|
|
431
|
+
for nested in rt.nested_types:
|
|
432
|
+
_flatten(nested, file_id, project_id, tid, entries)
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
# -- method merging ------------------------------------------------------------------------
|
|
436
|
+
|
|
437
|
+
def _merge_methods(e: _Entry, out_of_line: list[RawMethod]) -> list[_MethodRec]:
|
|
438
|
+
"""One _MethodRec per method id: an in-class declaration and its out-of-line definition
|
|
439
|
+
(matched by owner + name + parameter *types*) collapse into a single node, the definition
|
|
440
|
+
supplying the body/source/calls (as C merges a function prototype with its definition)."""
|
|
441
|
+
recs: dict[str, _MethodRec] = {}
|
|
442
|
+
order: list[str] = []
|
|
443
|
+
|
|
444
|
+
def upsert(m: RawMethod) -> None:
|
|
445
|
+
mid = ids.method_id(e.type_id, m.name, [p.type for p in m.params])
|
|
446
|
+
cur = recs.get(mid)
|
|
447
|
+
if cur is None:
|
|
448
|
+
recs[mid] = _MethodRec(
|
|
449
|
+
method_id=mid, name=m.name, kind=m.kind, return_type=m.return_type,
|
|
450
|
+
params=list(m.params), is_static=m.is_static, is_virtual=m.is_virtual,
|
|
451
|
+
is_const=m.is_const, defined=m.is_definition, calls=list(m.calls),
|
|
452
|
+
source=m.source, doc=m.doc, line_start=m.line_start, line_end=m.line_end,
|
|
453
|
+
)
|
|
454
|
+
order.append(mid)
|
|
455
|
+
return
|
|
456
|
+
# Merge: the definition wins for body-derived fields; keep declaration flags where richer.
|
|
457
|
+
if m.is_definition and not cur.defined:
|
|
458
|
+
cur.defined = True
|
|
459
|
+
cur.calls = list(m.calls)
|
|
460
|
+
cur.source = m.source
|
|
461
|
+
cur.line_start, cur.line_end = m.line_start, m.line_end
|
|
462
|
+
cur.params = list(m.params) # the definition's params carry names
|
|
463
|
+
cur.is_virtual = cur.is_virtual or m.is_virtual
|
|
464
|
+
cur.is_static = cur.is_static or m.is_static
|
|
465
|
+
if cur.return_type is None and m.return_type is not None:
|
|
466
|
+
cur.return_type = m.return_type
|
|
467
|
+
if m.doc and not cur.doc: # keep the first non-empty doc (decl usually carries the API doc)
|
|
468
|
+
cur.doc = m.doc
|
|
469
|
+
|
|
470
|
+
for m in e.raw_type.methods:
|
|
471
|
+
upsert(m)
|
|
472
|
+
for m in out_of_line:
|
|
473
|
+
upsert(m)
|
|
474
|
+
return [recs[mid] for mid in order]
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
# -- pass 1: nodes + structural edges ------------------------------------------------------
|
|
478
|
+
|
|
479
|
+
def _type_properties(rt: RawType) -> dict:
|
|
480
|
+
props: dict = {
|
|
481
|
+
"name": rt.name,
|
|
482
|
+
"qualified_name": f"{rt.namespace}::{rt.name}" if rt.namespace else rt.name,
|
|
483
|
+
"type_parameters": list(rt.type_parameters),
|
|
484
|
+
"is_template": bool(rt.type_parameters),
|
|
485
|
+
"source": rt.source,
|
|
486
|
+
"doc": rt.doc,
|
|
487
|
+
"line_start": rt.line_start,
|
|
488
|
+
"line_end": rt.line_end,
|
|
489
|
+
}
|
|
490
|
+
if rt.kind == "Enum":
|
|
491
|
+
props["is_scoped"] = rt.is_scoped # `enum class` / `enum struct`
|
|
492
|
+
props["members"] = list(rt.enum_members)
|
|
493
|
+
return props
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
def _emit_type_and_members(e: _Entry, methods: list[_MethodRec]) -> Iterator[GraphNode | GraphEdge]:
|
|
497
|
+
yield GraphNode(e.kind, {"id": e.type_id}, _type_properties(e.raw_type))
|
|
498
|
+
if e.parent_type_id is not None:
|
|
499
|
+
yield GraphEdge("HAS_NESTED_TYPE", _parent_kind(e), {"id": e.parent_type_id}, e.kind, {"id": e.type_id})
|
|
500
|
+
else:
|
|
501
|
+
yield GraphEdge("HAS_TYPE", e.scope_label, {"id": e.scope_id}, e.kind, {"id": e.type_id})
|
|
502
|
+
yield GraphEdge("DEFINED_IN", e.kind, {"id": e.type_id}, "File", {"id": e.file_id})
|
|
503
|
+
|
|
504
|
+
for r in methods:
|
|
505
|
+
yield from _emit_method(e.type_id, e.kind, r)
|
|
506
|
+
for f in e.raw_type.fields:
|
|
507
|
+
fid = ids.field_id(e.type_id, f.name)
|
|
508
|
+
yield GraphNode("Field", {"id": fid}, {
|
|
509
|
+
"name": f.name, "type": f.type, "is_static": f.is_static,
|
|
510
|
+
"line_start": f.line_start, "line_end": f.line_end,
|
|
511
|
+
})
|
|
512
|
+
yield GraphEdge("HAS_FIELD", e.kind, {"id": e.type_id}, "Field", {"id": fid})
|
|
513
|
+
for member in e.raw_type.enum_members:
|
|
514
|
+
mid = ids.enum_member_id(e.type_id, member)
|
|
515
|
+
yield GraphNode("EnumMember", {"id": mid}, {"name": member})
|
|
516
|
+
yield GraphEdge("HAS_ENUM_MEMBER", "Enum", {"id": e.type_id}, "EnumMember", {"id": mid})
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
_KIND_BY_SLUG = {"class": "Class", "struct": "Struct", "union": "Union", "enum": "Enum"}
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
def _parent_kind(e: _Entry) -> str:
|
|
523
|
+
# parent_type_id ends with ".<slug>.<name>[`arity]"; pull the slug.
|
|
524
|
+
tail = e.parent_type_id.rsplit(".", 2)[-2] if e.parent_type_id else "class"
|
|
525
|
+
return _KIND_BY_SLUG.get(tail, "Class")
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
def _emit_method(type_id: str, type_label: str, r: _MethodRec) -> Iterator[GraphNode | GraphEdge]:
|
|
529
|
+
yield GraphNode("Method", {"id": r.method_id}, {
|
|
530
|
+
"name": r.name,
|
|
531
|
+
"kind": r.kind,
|
|
532
|
+
"is_static": r.is_static,
|
|
533
|
+
"is_virtual": r.is_virtual,
|
|
534
|
+
"is_const": r.is_const,
|
|
535
|
+
"defined": r.defined,
|
|
536
|
+
"return_type": r.return_type,
|
|
537
|
+
"source": r.source,
|
|
538
|
+
"doc": r.doc,
|
|
539
|
+
"line_start": r.line_start,
|
|
540
|
+
"line_end": r.line_end,
|
|
541
|
+
})
|
|
542
|
+
yield GraphEdge("HAS_METHOD", type_label, {"id": type_id}, "Method", {"id": r.method_id})
|
|
543
|
+
for i, p in enumerate(r.params):
|
|
544
|
+
yield from _emit_parameter(r.method_id, "Method", p, i)
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
def _emit_parameter(parent_id: str, parent_label: str, param, position: int) -> Iterator[GraphNode | GraphEdge]:
|
|
548
|
+
pname = param.name or f"arg{position}"
|
|
549
|
+
pid = ids.parameter_id(parent_id, position, pname)
|
|
550
|
+
yield GraphNode("Parameter", {"id": pid}, {"name": pname, "type": param.type, "position": position})
|
|
551
|
+
yield GraphEdge("HAS_PARAMETER", parent_label, {"id": parent_id}, "Parameter", {"id": pid})
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
def _make_func_rec(fn: RawFunction, project_id: str) -> _FuncRec:
|
|
555
|
+
if fn.namespace:
|
|
556
|
+
scope_id, scope_label = ids.namespace_id(project_id, fn.namespace), "Namespace"
|
|
557
|
+
else:
|
|
558
|
+
scope_id, scope_label = project_id, "Project"
|
|
559
|
+
fid = ids.function_id(scope_id, fn.name, [p.type for p in fn.params])
|
|
560
|
+
return _FuncRec(function_id=fid, scope_id=scope_id, scope_label=scope_label, namespace=fn.namespace, raw=fn)
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
def _cpp_qualified(namespace: str | None, name: str) -> str:
|
|
564
|
+
return f"{namespace}::{name}" if namespace else name
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
def _cpp_origin(qualified: str) -> str:
|
|
568
|
+
head = qualified.split("::", 1)[0]
|
|
569
|
+
if head in ("std", "__gnu_cxx"):
|
|
570
|
+
return "stdlib"
|
|
571
|
+
return "external"
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
def _external_free_call(call: RawCall) -> str | None:
|
|
575
|
+
"""The qualified free-function name for an unresolved call we can name across repos (ADR 0004),
|
|
576
|
+
or None for a constructor / receiver-typed member call we can't attribute to a free function."""
|
|
577
|
+
if call.is_new or (call.receiver is not None and call.receiver != "this"):
|
|
578
|
+
return None
|
|
579
|
+
return f"{call.scope}::{call.name}" if call.scope else call.name
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
def _external_function_node(qualified: str) -> GraphNode:
|
|
583
|
+
eid = ids.extern_function_key(qualified)
|
|
584
|
+
return GraphNode("Function", {"id": eid}, {
|
|
585
|
+
"name": qualified.rsplit("::", 1)[-1], "qualified_name": qualified,
|
|
586
|
+
"resolved": False, "origin": _cpp_origin(qualified), "linkage_key": eid,
|
|
587
|
+
})
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
def _emit_function(fr: _FuncRec) -> Iterator[GraphNode | GraphEdge]:
|
|
591
|
+
fn = fr.raw
|
|
592
|
+
qualified = _cpp_qualified(fn.namespace, fn.name)
|
|
593
|
+
yield GraphNode("Function", {"id": fr.function_id}, {
|
|
594
|
+
"name": fn.name,
|
|
595
|
+
"qualified_name": qualified,
|
|
596
|
+
"linkage_key": ids.extern_function_key(qualified), # cross-repo key (ADR 0004)
|
|
597
|
+
"return_type": fn.return_type,
|
|
598
|
+
"signature": fn.signature,
|
|
599
|
+
"defined": fn.is_definition,
|
|
600
|
+
"source": fn.source,
|
|
601
|
+
"doc": fn.doc,
|
|
602
|
+
"line_start": fn.line_start,
|
|
603
|
+
"line_end": fn.line_end,
|
|
604
|
+
})
|
|
605
|
+
yield GraphEdge("HAS_FUNCTION", fr.scope_label, {"id": fr.scope_id}, "Function", {"id": fr.function_id})
|
|
606
|
+
for i, p in enumerate(fn.params):
|
|
607
|
+
yield from _emit_parameter(fr.function_id, "Function", p, i)
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
# -- pass 2: hierarchy, type references, calls ---------------------------------------------
|
|
611
|
+
|
|
612
|
+
def _resolve_type(name: str, from_namespace: str, type_index: dict[str, list[_Entry]]) -> _Entry | None:
|
|
613
|
+
matches = type_index.get(name)
|
|
614
|
+
if not matches:
|
|
615
|
+
return None
|
|
616
|
+
same = [m for m in matches if m.namespace == from_namespace]
|
|
617
|
+
pool = same or matches
|
|
618
|
+
return sorted(pool, key=lambda m: m.type_id)[0] # deterministic on ambiguity
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
def _emit_hierarchy(e: _Entry, type_index: dict[str, list[_Entry]]) -> Iterator[GraphEdge]:
|
|
622
|
+
# C++ has no interfaces -> EXTENDS only, one edge per base (multiple inheritance), carrying
|
|
623
|
+
# the access specifier. A pure-virtual "interface" base is just another Class -> still EXTENDS.
|
|
624
|
+
emitted: set[str] = set()
|
|
625
|
+
for base in e.raw_type.bases:
|
|
626
|
+
target = _resolve_type(base.name, e.namespace, type_index)
|
|
627
|
+
if target is None or target.type_id == e.type_id or target.type_id in emitted:
|
|
628
|
+
continue
|
|
629
|
+
emitted.add(target.type_id)
|
|
630
|
+
yield GraphEdge("EXTENDS", e.kind, {"id": e.type_id}, target.kind, {"id": target.type_id},
|
|
631
|
+
{"access": base.access})
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
def _referenced_names(type_text: str | None) -> list[str]:
|
|
635
|
+
if not type_text:
|
|
636
|
+
return []
|
|
637
|
+
return [n for n in _IDENTIFIER_RE.findall(type_text) if n not in _NON_TYPE_TOKENS]
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
def _depends_on(from_label: str, from_id: str, names: set[str], from_namespace: str,
|
|
641
|
+
self_type_id: str | None, type_index) -> Iterator[GraphEdge]:
|
|
642
|
+
emitted: set[str] = set()
|
|
643
|
+
for name in names:
|
|
644
|
+
target = _resolve_type(name, from_namespace, type_index)
|
|
645
|
+
if target is None or target.type_id == self_type_id or target.type_id in emitted:
|
|
646
|
+
continue
|
|
647
|
+
emitted.add(target.type_id)
|
|
648
|
+
yield GraphEdge("DEPENDS_ON", from_label, {"id": from_id}, target.kind, {"id": target.type_id})
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
def _emit_type_references(e: _Entry, methods: list[_MethodRec], type_index) -> Iterator[GraphEdge]:
|
|
652
|
+
for r in methods:
|
|
653
|
+
names = set(_referenced_names(r.return_type))
|
|
654
|
+
for p in r.params:
|
|
655
|
+
names.update(_referenced_names(p.type))
|
|
656
|
+
yield from _depends_on("Method", r.method_id, names, e.namespace, e.type_id, type_index)
|
|
657
|
+
for f in e.raw_type.fields:
|
|
658
|
+
fid = ids.field_id(e.type_id, f.name)
|
|
659
|
+
yield from _depends_on("Field", fid, set(_referenced_names(f.type)), e.namespace, e.type_id, type_index)
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
def _emit_function_depends_on(fr: _FuncRec, type_index) -> Iterator[GraphEdge]:
|
|
663
|
+
fn = fr.raw
|
|
664
|
+
names = set(_referenced_names(fn.return_type))
|
|
665
|
+
for p in fn.params:
|
|
666
|
+
names.update(_referenced_names(p.type))
|
|
667
|
+
yield from _depends_on("Function", fr.function_id, names, fr.namespace, None, type_index)
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
def _pick_method(candidates: list[_MethodIndexEntry]) -> str:
|
|
671
|
+
return sorted(candidates, key=lambda c: c.method_id)[0].method_id
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
def _pick_function(candidates: list[_FuncRec], from_namespace: str) -> str:
|
|
675
|
+
same = [c for c in candidates if c.namespace == from_namespace]
|
|
676
|
+
pool = same or candidates
|
|
677
|
+
return sorted(pool, key=lambda c: c.function_id)[0].function_id
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
def _resolve_call(call: RawCall, from_namespace: str, current_type_id: str | None, member_types: dict[str, str],
|
|
681
|
+
type_index, method_index: dict[str, list[_MethodIndexEntry]],
|
|
682
|
+
function_index: dict[str, list[_FuncRec]]):
|
|
683
|
+
"""(target_label, target_id, extra_props) or None. `new X()` -> X's constructor; a
|
|
684
|
+
`Class::x()` qualified call -> that class's method (else a namespace free function); a
|
|
685
|
+
receiver naming a known field/param -> that type's method; a bare/`this` call -> the same
|
|
686
|
+
type's method (implicit this), else a free function."""
|
|
687
|
+
if call.is_new:
|
|
688
|
+
target = _resolve_type(call.name, from_namespace, type_index)
|
|
689
|
+
if target is not None:
|
|
690
|
+
ctors = [c for c in method_index.get(call.name, [])
|
|
691
|
+
if c.owner_type_id == target.type_id and c.kind == "CONSTRUCTOR"]
|
|
692
|
+
if ctors:
|
|
693
|
+
return "Method", _pick_method(ctors), {}
|
|
694
|
+
return None
|
|
695
|
+
|
|
696
|
+
if call.scope:
|
|
697
|
+
target = _resolve_type(call.scope, from_namespace, type_index)
|
|
698
|
+
if target is not None:
|
|
699
|
+
matches = [c for c in method_index.get(call.name, []) if c.owner_type_id == target.type_id]
|
|
700
|
+
if matches:
|
|
701
|
+
return "Method", _pick_method(matches), {"receiver_type": target.name}
|
|
702
|
+
fns = function_index.get(call.name) # `ns::free_function()`
|
|
703
|
+
if fns:
|
|
704
|
+
return "Function", _pick_function(fns, from_namespace), {}
|
|
705
|
+
return None
|
|
706
|
+
|
|
707
|
+
if call.receiver is not None and call.receiver != "this":
|
|
708
|
+
rtype = member_types.get(call.receiver)
|
|
709
|
+
if rtype is None:
|
|
710
|
+
return None # unknown receiver (a local variable we don't track) -> no guess
|
|
711
|
+
for type_name in _referenced_names(rtype):
|
|
712
|
+
target = _resolve_type(type_name, from_namespace, type_index)
|
|
713
|
+
if target is None:
|
|
714
|
+
continue
|
|
715
|
+
matches = [c for c in method_index.get(call.name, []) if c.owner_type_id == target.type_id]
|
|
716
|
+
if matches:
|
|
717
|
+
return "Method", _pick_method(matches), {"receiver_type": target.name}
|
|
718
|
+
return None
|
|
719
|
+
|
|
720
|
+
# bare or `this` call: prefer the same type's method (implicit this), then a free function.
|
|
721
|
+
if current_type_id is not None:
|
|
722
|
+
same = [c for c in method_index.get(call.name, []) if c.owner_type_id == current_type_id]
|
|
723
|
+
if same:
|
|
724
|
+
return "Method", _pick_method(same), {}
|
|
725
|
+
fns = function_index.get(call.name)
|
|
726
|
+
if fns:
|
|
727
|
+
return "Function", _pick_function(fns, from_namespace), {}
|
|
728
|
+
return None
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
def _emit_calls_for_type(e: _Entry, methods: list[_MethodRec], type_index,
|
|
732
|
+
method_index, function_index, externs: set[str]) -> Iterator[GraphNode | GraphEdge]:
|
|
733
|
+
field_types = {f.name: f.type for f in e.raw_type.fields if f.type}
|
|
734
|
+
for r in methods:
|
|
735
|
+
member_types = dict(field_types)
|
|
736
|
+
member_types.update({p.name: p.type for p in r.params if p.name and p.type})
|
|
737
|
+
emitted: set[str] = set()
|
|
738
|
+
for call in r.calls:
|
|
739
|
+
hit = _resolve_call(call, e.namespace, e.type_id, member_types, type_index, method_index, function_index)
|
|
740
|
+
if hit is None:
|
|
741
|
+
yield from _emit_external_call("Method", r.method_id, call, emitted, externs)
|
|
742
|
+
continue
|
|
743
|
+
target_label, target_id, extra = hit
|
|
744
|
+
if target_id in emitted:
|
|
745
|
+
continue
|
|
746
|
+
emitted.add(target_id)
|
|
747
|
+
yield GraphEdge("CALLS", "Method", {"id": r.method_id}, target_label, {"id": target_id},
|
|
748
|
+
{"resolution": "name", **extra})
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
def _emit_function_calls(fr: _FuncRec, type_index, method_index, function_index,
|
|
752
|
+
externs: set[str]) -> Iterator[GraphNode | GraphEdge]:
|
|
753
|
+
member_types = {p.name: p.type for p in fr.raw.params if p.name and p.type}
|
|
754
|
+
emitted: set[str] = set()
|
|
755
|
+
for call in fr.raw.calls:
|
|
756
|
+
hit = _resolve_call(call, fr.namespace, None, member_types, type_index, method_index, function_index)
|
|
757
|
+
if hit is None:
|
|
758
|
+
yield from _emit_external_call("Function", fr.function_id, call, emitted, externs)
|
|
759
|
+
continue
|
|
760
|
+
target_label, target_id, extra = hit
|
|
761
|
+
if target_id in emitted:
|
|
762
|
+
continue
|
|
763
|
+
emitted.add(target_id)
|
|
764
|
+
yield GraphEdge("CALLS", "Function", {"id": fr.function_id}, target_label, {"id": target_id},
|
|
765
|
+
{"resolution": "name", **extra})
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
def _emit_external_call(from_label: str, from_id: str, call: RawCall, emitted: set[str],
|
|
769
|
+
externs: set[str]) -> Iterator[GraphNode | GraphEdge]:
|
|
770
|
+
"""An unresolved call the resolver could not bind in-project -> a placeholder Function keyed by
|
|
771
|
+
its cross-repo linkage (std/system or a not-yet-ingested sibling repo), ADR 0004. Only
|
|
772
|
+
free/namespaced calls are nameable this way; constructors / receiver-typed member calls skip."""
|
|
773
|
+
qualified = _external_free_call(call)
|
|
774
|
+
if qualified is None:
|
|
775
|
+
return
|
|
776
|
+
eid = ids.extern_function_key(qualified)
|
|
777
|
+
if eid in emitted:
|
|
778
|
+
return
|
|
779
|
+
emitted.add(eid)
|
|
780
|
+
if eid not in externs:
|
|
781
|
+
externs.add(eid)
|
|
782
|
+
yield _external_function_node(qualified)
|
|
783
|
+
yield GraphEdge("CALLS", from_label, {"id": from_id}, "Function", {"id": eid}, {"resolution": "external"})
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
# -- C-family: macros + #includes ----------------------------------------------------------
|
|
787
|
+
|
|
788
|
+
def _emit_macros(raw_files: list[RawFile], project_id: str) -> Iterator[GraphNode | GraphEdge]:
|
|
789
|
+
for rf in raw_files:
|
|
790
|
+
fid = ids.file_id(project_id, rf.path)
|
|
791
|
+
norm = _norm(rf.path)
|
|
792
|
+
for m in rf.macros:
|
|
793
|
+
mid = ids.macro_id(fid, m.name) # macros are file/preprocessor-scoped (as in C)
|
|
794
|
+
yield GraphNode("Macro", {"id": mid}, {
|
|
795
|
+
"name": m.name, "qualified_name": f"{norm}:{m.name}", "function_like": m.is_function_like,
|
|
796
|
+
"source": m.source, "line_start": m.line_start, "line_end": m.line_end,
|
|
797
|
+
})
|
|
798
|
+
yield GraphEdge("DEFINES", "File", {"id": fid}, "Macro", {"id": mid})
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
def _include_fullpaths(raw_files: list[RawFile], file_ids: dict[str, str]) -> dict[str, str]:
|
|
802
|
+
"""{normalized full path -> File id} -- one entry per file, never ambiguous. The basename shortcuts
|
|
803
|
+
are added separately by `_collapse_basenames`, which must see the WHOLE project at once."""
|
|
804
|
+
return {_norm(rf.path): file_ids[rf.path] for rf in raw_files}
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
def _collapse_basenames(fullpaths: dict[str, str]) -> dict[str, str]:
|
|
808
|
+
"""Build the include-resolution index from a full-path map: keep every full-path key, and add a bare
|
|
809
|
+
basename key ONLY for a GLOBALLY-unique basename (so `#include "x.h"` resolves by basename when the
|
|
810
|
+
project has exactly one `x.h`). The uniqueness count MUST be over the whole project at once -- counting
|
|
811
|
+
within a partition (seed vs emit) would call a basename unique in each half and wrongly resolve an
|
|
812
|
+
#include that the full path leaves dangling (incremental != full)."""
|
|
813
|
+
idx = dict(fullpaths)
|
|
814
|
+
basename_count: dict[str, int] = defaultdict(int)
|
|
815
|
+
for norm in fullpaths:
|
|
816
|
+
basename_count[posixpath.basename(norm)] += 1
|
|
817
|
+
for norm, fid in fullpaths.items():
|
|
818
|
+
base = posixpath.basename(norm)
|
|
819
|
+
if basename_count[base] == 1:
|
|
820
|
+
idx.setdefault(base, fid)
|
|
821
|
+
return idx
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
def _build_include_index(raw_files: list[RawFile], file_ids: dict[str, str]) -> dict[str, str]:
|
|
825
|
+
"""{normalized path AND unique basename -> File id}, so an #include resolves by exact relative path
|
|
826
|
+
first, else by unique basename (mirrors the C ingestor). Used by the full `resolve`, which sees every
|
|
827
|
+
file at once; the incremental path collapses basenames over the seed+emit union instead."""
|
|
828
|
+
return _collapse_basenames(_include_fullpaths(raw_files, file_ids))
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
def _emit_includes(raw_files: list[RawFile], file_ids: dict[str, str],
|
|
832
|
+
include_index: dict[str, str]) -> Iterator[GraphEdge]:
|
|
833
|
+
"""Resolve each `#include "..."`/`<...>` to a project File via `include_index` (exact relative path,
|
|
834
|
+
else unique basename). The index spans the WHOLE project (seed non-emit files + the emit set on an
|
|
835
|
+
incremental resync), so an emit file's include of an untouched header still resolves -- otherwise the
|
|
836
|
+
INCLUDES edge would vanish on resync (incremental != full). Out-of-project system headers -> no edge."""
|
|
837
|
+
for rf in raw_files:
|
|
838
|
+
fid = file_ids[rf.path]
|
|
839
|
+
for inc in rf.includes:
|
|
840
|
+
target = include_index.get(_norm(inc.path)) or include_index.get(posixpath.basename(inc.path))
|
|
841
|
+
if target is not None and target != fid:
|
|
842
|
+
yield GraphEdge("INCLUDES", "File", {"id": fid}, "File", {"id": target}, {"resolution": "name"})
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
# -- file provenance (ADR 0005) ------------------------------------------------------------
|
|
846
|
+
|
|
847
|
+
def _note(prov: dict[str, tuple[str, dict[str, bool]]], entity_id: str, label: str,
|
|
848
|
+
file_id: str, is_def: bool) -> None:
|
|
849
|
+
"""Record that `file_id` DEFINES (is_def=True, has a body) or merely DECLARES (False) `entity_id`.
|
|
850
|
+
A file that both declares and inline-defines the same entity counts as a definition (OR of flags)."""
|
|
851
|
+
entry = prov.get(entity_id)
|
|
852
|
+
if entry is None:
|
|
853
|
+
prov[entity_id] = (label, {file_id: is_def})
|
|
854
|
+
else:
|
|
855
|
+
files = entry[1]
|
|
856
|
+
files[file_id] = files.get(file_id, False) or is_def
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
def _emit_provenance(type_prov, method_prov, func_prov, ns_prov) -> Iterator[GraphEdge]:
|
|
860
|
+
"""File-[:DEFINES|DECLARES]->entity for every program-global type/method/function + the namespaces a
|
|
861
|
+
file contributes to -- the per-file ownership handle the authored-shared incremental engine reads and
|
|
862
|
+
diffs (uniform with C/Go/Java). DEFINES where the file carries the body, DECLARES where it only
|
|
863
|
+
declares (a header). Emitted IDENTICALLY by resolve() and resolve_incremental() (shared helper), so a
|
|
864
|
+
full recompute and an incremental resync agree edge-for-edge (the incremental==full invariant)."""
|
|
865
|
+
for prov in (type_prov, method_prov, func_prov):
|
|
866
|
+
for eid, (label, files) in prov.items():
|
|
867
|
+
for fid, is_def in files.items():
|
|
868
|
+
yield GraphEdge("DEFINES" if is_def else "DECLARES", "File", {"id": fid}, label, {"id": eid})
|
|
869
|
+
for nid, fids in ns_prov.items():
|
|
870
|
+
for fid in fids: # a namespace has no decl/def split -- every contributing file DEFINES it
|
|
871
|
+
yield GraphEdge("DEFINES", "File", {"id": fid}, "Namespace", {"id": nid})
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
# -- helpers -------------------------------------------------------------------------------
|
|
875
|
+
|
|
876
|
+
def _method_to_function(md: RawMethod, namespace: str) -> RawFunction:
|
|
877
|
+
return RawFunction(
|
|
878
|
+
name=md.name, namespace=namespace, return_type=md.return_type or "",
|
|
879
|
+
params=list(md.params), is_definition=md.is_definition, calls=list(md.calls),
|
|
880
|
+
signature="", source=md.source, doc=md.doc, line_start=md.line_start, line_end=md.line_end,
|
|
881
|
+
)
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
def _join_ns(parts: list[str]) -> str:
|
|
885
|
+
return "::".join([p for p in parts if p])
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
def _norm(path: str) -> str:
|
|
889
|
+
return path.replace("\\", "/").lstrip("./")
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
def _basename(path: str) -> str:
|
|
893
|
+
return _norm(path).rsplit("/", 1)[-1]
|