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
|
File without changes
|
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
"""parse_file(path) -> RawFile: the parallelizable phase-1 unit (pure function, no shared
|
|
2
|
+
state -- safe under a ProcessPoolExecutor; only picklable raw_model dataclasses are returned,
|
|
3
|
+
no tree-sitter node crosses the boundary). parse_source(source, path_label) holds the logic
|
|
4
|
+
and takes raw bytes, so a git blob can be parsed without touching disk.
|
|
5
|
+
|
|
6
|
+
Every node type / field name below was confirmed empirically against tree-sitter-cpp 0.23.4
|
|
7
|
+
(see the package's grammar probe), not assumed:
|
|
8
|
+
|
|
9
|
+
- top level / namespace scope: `namespace_definition` (`name` = `namespace_identifier` or
|
|
10
|
+
`nested_namespace_specifier` for `a::b`; anonymous = no `name`; `body` = `declaration_list`),
|
|
11
|
+
`preproc_include` (`path` = `system_lib_string` | `string_literal`), `preproc_def` /
|
|
12
|
+
`preproc_function_def` (`name`), and the header-guard/`extern "C"` wrappers
|
|
13
|
+
`preproc_ifdef`/`preproc_if`/.../`linkage_specification` (flattened, as in the C ingestor).
|
|
14
|
+
- types: `class_specifier` / `struct_specifier` / `union_specifier` (`name` = type_identifier,
|
|
15
|
+
child `base_class_clause` [access_specifier + type], `body` = field_declaration_list) and
|
|
16
|
+
`enum_specifier` (optional `class`/`struct` token = scoped; `name`; `body` = enumerator_list
|
|
17
|
+
of `enumerator` with `name`).
|
|
18
|
+
- members (inside field_declaration_list, with `access_specifier` toggling current access):
|
|
19
|
+
`field_declaration` (a member var, OR a method *declaration* when its declarator descends to
|
|
20
|
+
a `function_declarator`), `function_definition` (an inline member method with a body), and
|
|
21
|
+
`declaration` (a ctor/dtor prototype -- no return type -> parsed as declaration).
|
|
22
|
+
- functions: `function_definition` / `declaration` whose `function_declarator`'s inner
|
|
23
|
+
declarator is a plain `identifier` = free function; a `qualified_identifier`
|
|
24
|
+
(`scope`::`name`, possibly nested) = an out-of-line member definition `Owner::method`.
|
|
25
|
+
- templates: `template_declaration` (`parameters` = template_parameter_list of
|
|
26
|
+
`type_parameter_declaration`) wrapping a class/function.
|
|
27
|
+
- calls: `call_expression` (`function` = `identifier` | `field_expression`
|
|
28
|
+
[`argument`/`operator` `.`|`->` /`field`] | `qualified_identifier`) and `new_expression`
|
|
29
|
+
(`type`).
|
|
30
|
+
|
|
31
|
+
Names sit at the bottom of possibly pointer/reference/array/function-wrapped declarators, so
|
|
32
|
+
the descent helpers step through the `declarator` field (and the unlabelled child of a
|
|
33
|
+
`reference_declarator`) to the leaf identifier/field_identifier/destructor_name/qualified id."""
|
|
34
|
+
|
|
35
|
+
from __future__ import annotations
|
|
36
|
+
|
|
37
|
+
from pathlib import Path
|
|
38
|
+
|
|
39
|
+
from auryn.core.docstrings import leading_comment
|
|
40
|
+
from auryn.ingestors.cpp.grammar import new_parser
|
|
41
|
+
from auryn.ingestors.cpp.raw_model import (
|
|
42
|
+
RawBase,
|
|
43
|
+
RawCall,
|
|
44
|
+
RawField,
|
|
45
|
+
RawFile,
|
|
46
|
+
RawFunction,
|
|
47
|
+
RawInclude,
|
|
48
|
+
RawMacro,
|
|
49
|
+
RawMethod,
|
|
50
|
+
RawParameter,
|
|
51
|
+
RawType,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
_TYPE_SPECIFIERS = {"class_specifier": "Class", "struct_specifier": "Struct", "union_specifier": "Union"}
|
|
55
|
+
_NAME_LEAVES = {"identifier", "field_identifier", "type_identifier", "destructor_name"}
|
|
56
|
+
_DECL_WRAPPERS = {
|
|
57
|
+
"pointer_declarator", "reference_declarator", "array_declarator",
|
|
58
|
+
"init_declarator", "parenthesized_declarator",
|
|
59
|
+
}
|
|
60
|
+
# Wrappers that hold ordinary declarations as children rather than being declarations
|
|
61
|
+
# themselves -- header guards / feature #ifs and `extern "C"` blocks. We flatten them so a
|
|
62
|
+
# guarded header (`#ifndef FOO_HPP ... #endif`, i.e. almost every real header) isn't lost.
|
|
63
|
+
_TRANSPARENT = {
|
|
64
|
+
"preproc_ifdef", "preproc_if", "preproc_elif", "preproc_else", "preproc_elifdef",
|
|
65
|
+
"preproc_elifndef", "linkage_specification",
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def parse_file(path: str) -> RawFile:
|
|
70
|
+
return parse_source(Path(path).read_bytes(), path)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def parse_source(source: bytes, path_label: str, parser=None) -> RawFile:
|
|
74
|
+
# `parser` lets a sibling ingestor reuse this full C++ extraction with a different grammar.
|
|
75
|
+
# tree-sitter-cuda is a strict superset of tree-sitter-cpp (identical node types/fields for
|
|
76
|
+
# every C++ construct -- verified), so the auryn-cuda plugin passes its own parser here and
|
|
77
|
+
# gets the whole C++ graph for free, then layers on the CUDA-only extras (see auryn.ingestors.cuda).
|
|
78
|
+
tree = (parser or new_parser()).parse(source)
|
|
79
|
+
rf = RawFile(path=path_label)
|
|
80
|
+
|
|
81
|
+
def txt(node) -> str:
|
|
82
|
+
return source[node.start_byte : node.end_byte].decode("utf-8", "replace") if node else ""
|
|
83
|
+
|
|
84
|
+
def lines(node) -> tuple[int, int]:
|
|
85
|
+
# tree-sitter points are 0-based (row, col); expose 1-based inclusive line numbers.
|
|
86
|
+
return node.start_point[0] + 1, node.end_point[0] + 1
|
|
87
|
+
|
|
88
|
+
def doc_of(inner, outer=None) -> str:
|
|
89
|
+
"""The cleaned leading comment for a declaration. A doc comment is a *preceding sibling*
|
|
90
|
+
of the node it documents; when a class/function is wrapped in a `template_declaration`,
|
|
91
|
+
the comment sits above the wrapper (`outer`), not the inner specifier (`inner`)."""
|
|
92
|
+
return leading_comment(inner, source) or (leading_comment(outer, source) if outer is not None else "")
|
|
93
|
+
|
|
94
|
+
def type_text(type_node) -> str:
|
|
95
|
+
return " ".join(txt(type_node).split()) if type_node else ""
|
|
96
|
+
|
|
97
|
+
def first_type_name(node) -> str | None:
|
|
98
|
+
"""The bare simple name a type/base node denotes: `Base` for `Base`, `Base<T>`
|
|
99
|
+
(template_type), `ns::Base` (qualified) -- generic args and scopes stripped."""
|
|
100
|
+
if node is None:
|
|
101
|
+
return None
|
|
102
|
+
if node.type in ("type_identifier", "namespace_identifier"):
|
|
103
|
+
return txt(node)
|
|
104
|
+
if node.type == "template_type":
|
|
105
|
+
return first_type_name(node.child_by_field_name("name"))
|
|
106
|
+
if node.type == "qualified_identifier":
|
|
107
|
+
segs, final = split_qualified(node)
|
|
108
|
+
return final or (segs[-1] if segs else None)
|
|
109
|
+
# descend to the first type_identifier we can find
|
|
110
|
+
stack = list(node.children)
|
|
111
|
+
while stack:
|
|
112
|
+
n = stack.pop(0)
|
|
113
|
+
if n.type == "type_identifier":
|
|
114
|
+
return txt(n)
|
|
115
|
+
stack.extend(n.children)
|
|
116
|
+
return None
|
|
117
|
+
|
|
118
|
+
def split_qualified(qid) -> tuple[list[str], str]:
|
|
119
|
+
"""`A::B::deep` -> (["A", "B"], "deep"); `Foo::bar` -> (["Foo"], "bar"). The scope
|
|
120
|
+
segments are the leading names; the final segment is the simple name (which may be a
|
|
121
|
+
destructor_name, kept verbatim as "~Foo")."""
|
|
122
|
+
segs: list[str] = []
|
|
123
|
+
node = qid
|
|
124
|
+
while node is not None and node.type == "qualified_identifier":
|
|
125
|
+
scope = node.child_by_field_name("scope")
|
|
126
|
+
if scope is not None:
|
|
127
|
+
segs.append(txt(scope))
|
|
128
|
+
node = node.child_by_field_name("name")
|
|
129
|
+
final = txt(node) if node is not None else ""
|
|
130
|
+
return segs, final
|
|
131
|
+
|
|
132
|
+
def name_node_of(decl):
|
|
133
|
+
"""Descend a (possibly pointer/reference/array/init/function-wrapped) declarator to the
|
|
134
|
+
leaf name node: an identifier/field_identifier/destructor_name, or a qualified_identifier
|
|
135
|
+
(an out-of-line `Owner::method`). None for an abstract (unnamed) declarator."""
|
|
136
|
+
node = decl
|
|
137
|
+
seen = 0
|
|
138
|
+
while node is not None and seen < 64:
|
|
139
|
+
if node.type in _NAME_LEAVES or node.type == "qualified_identifier":
|
|
140
|
+
return node
|
|
141
|
+
nxt = node.child_by_field_name("declarator")
|
|
142
|
+
if nxt is None: # e.g. reference_declarator: the inner node isn't a `declarator` field
|
|
143
|
+
nxt = next((c for c in node.children
|
|
144
|
+
if c.type in _DECL_WRAPPERS or c.type == "function_declarator"), None)
|
|
145
|
+
node = nxt
|
|
146
|
+
seen += 1
|
|
147
|
+
return None
|
|
148
|
+
|
|
149
|
+
def find_function_declarator(decl):
|
|
150
|
+
"""A function/method name lives in a function_declarator, itself possibly wrapped by
|
|
151
|
+
pointer/reference/array declarators (`const Point& ref() const`). Descend to it."""
|
|
152
|
+
node = decl
|
|
153
|
+
seen = 0
|
|
154
|
+
while node is not None and seen < 64:
|
|
155
|
+
if node.type == "function_declarator":
|
|
156
|
+
return node
|
|
157
|
+
nxt = node.child_by_field_name("declarator")
|
|
158
|
+
if nxt is None:
|
|
159
|
+
nxt = next((c for c in node.children
|
|
160
|
+
if c.type in _DECL_WRAPPERS or c.type == "function_declarator"), None)
|
|
161
|
+
node = nxt
|
|
162
|
+
seen += 1
|
|
163
|
+
return None
|
|
164
|
+
|
|
165
|
+
def written_type(container, name_node, upto=None) -> str:
|
|
166
|
+
"""The type exactly as written, minus the identifier being declared and any storage-class
|
|
167
|
+
keyword: the container text with the name excised. Keeps `const`, `*`, `&`, `[]`, qualifiers that
|
|
168
|
+
live in the declarator -- e.g. `const Shape *s` -> `const Shape *`. `upto` bounds the text to
|
|
169
|
+
`[start, upto)` -- used for a defaulted parameter to cut at the `=` token, so the declaration and
|
|
170
|
+
its out-of-line definition run the SAME normalization over the SAME type text and thus share an
|
|
171
|
+
id (the default value never reaches the type), whatever the type contains."""
|
|
172
|
+
a = container.start_byte
|
|
173
|
+
b = upto if upto is not None else container.end_byte
|
|
174
|
+
if name_node is not None and a <= name_node.start_byte and name_node.end_byte <= b:
|
|
175
|
+
piece = source[a : name_node.start_byte] + source[name_node.end_byte : b]
|
|
176
|
+
else:
|
|
177
|
+
piece = source[a:b]
|
|
178
|
+
text = " ".join(piece.decode("utf-8", "replace").split()).strip().rstrip(";").strip()
|
|
179
|
+
for kw in ("static ", "mutable ", "inline ", "constexpr "):
|
|
180
|
+
while text.startswith(kw):
|
|
181
|
+
text = text[len(kw):].strip()
|
|
182
|
+
if ":" in text and "::" not in text: # bitfield `int x : 3` -- keep type, drop width
|
|
183
|
+
text = text.split(":", 1)[0].strip()
|
|
184
|
+
return text
|
|
185
|
+
|
|
186
|
+
def signature_of(node, top_declarator) -> str:
|
|
187
|
+
"""The header exactly as written -- from the declaration start through the `)` of the
|
|
188
|
+
parameter list, body excluded (top_declarator ends at the `)` for def and proto alike)."""
|
|
189
|
+
if top_declarator is None:
|
|
190
|
+
return ""
|
|
191
|
+
return " ".join(source[node.start_byte : top_declarator.end_byte].decode("utf-8", "replace").split()).strip()
|
|
192
|
+
|
|
193
|
+
def parameters(func_declarator) -> list[RawParameter]:
|
|
194
|
+
plist = func_declarator.child_by_field_name("parameters")
|
|
195
|
+
out: list[RawParameter] = []
|
|
196
|
+
if plist is None:
|
|
197
|
+
return out
|
|
198
|
+
pos = 0
|
|
199
|
+
for p in plist.children:
|
|
200
|
+
# `optional_parameter_declaration` is a parameter WITH a default value (`T x = y`). It MUST be
|
|
201
|
+
# treated as a parameter -- skipping it (as the old `!= "parameter_declaration"` did) drops the
|
|
202
|
+
# param, so a declaration with a default gets a DIFFERENT signature/id than its out-of-line
|
|
203
|
+
# definition (which has no default), the two never merge, and the decl-only node goes stale on
|
|
204
|
+
# resync (incremental != full; also a duplicate-node bug in a plain full ingest). Seen on Catch2's
|
|
205
|
+
# `Equals(std::string, CaseSensitive = CaseSensitive::Yes)`.
|
|
206
|
+
if p.type not in ("parameter_declaration", "optional_parameter_declaration"):
|
|
207
|
+
continue # skips '(', ')', ',', and variadic '...'
|
|
208
|
+
d = p.child_by_field_name("declarator")
|
|
209
|
+
nnode = name_node_of(d) if d is not None else None
|
|
210
|
+
pname = txt(nnode) if nnode is not None and nnode.type != "qualified_identifier" else ""
|
|
211
|
+
if p.type == "optional_parameter_declaration":
|
|
212
|
+
# A defaulted param (`T x = default`): bound written_type at the `=` TOKEN (a direct child --
|
|
213
|
+
# the `==` inside `decltype(a==b)` is nested, not a child) so the default value and any comment
|
|
214
|
+
# after `=` are dropped. Running it through the SAME written_type (same normalization) as a
|
|
215
|
+
# plain param is what makes the decl and its out-of-line def type IDENTICALLY -- whatever the
|
|
216
|
+
# type contains (ternary, bitfield-looking `:`, etc.) -- so they share an id and merge (else
|
|
217
|
+
# the decl-only node is a duplicate that goes stale on resync; Catch2 finding).
|
|
218
|
+
eq = next((c for c in p.children if not c.is_named and txt(c) == "="), None)
|
|
219
|
+
t_written = written_type(p, nnode, upto=eq.start_byte if eq is not None else None)
|
|
220
|
+
else:
|
|
221
|
+
t_written = written_type(p, nnode)
|
|
222
|
+
if t_written == "void" and d is None:
|
|
223
|
+
continue # `(void)` is "no parameters"
|
|
224
|
+
out.append(RawParameter(name=pname, type=t_written, position=pos))
|
|
225
|
+
pos += 1
|
|
226
|
+
return out
|
|
227
|
+
|
|
228
|
+
def has_child_token(node, token: str) -> bool:
|
|
229
|
+
return any(not c.is_named and txt(c) == token for c in node.children) or \
|
|
230
|
+
any(c.type == token for c in node.children)
|
|
231
|
+
|
|
232
|
+
def is_static(node) -> bool:
|
|
233
|
+
return any(c.type == "storage_class_specifier" and txt(c) == "static" for c in node.children)
|
|
234
|
+
|
|
235
|
+
def is_virtual(node) -> bool:
|
|
236
|
+
return any((not c.is_named and txt(c) == "virtual") for c in node.children)
|
|
237
|
+
|
|
238
|
+
def is_const_method(func_declarator) -> bool:
|
|
239
|
+
return any(c.type == "type_qualifier" and txt(c) == "const" for c in func_declarator.children)
|
|
240
|
+
|
|
241
|
+
def collect_calls(body) -> list[RawCall]:
|
|
242
|
+
if body is None:
|
|
243
|
+
return []
|
|
244
|
+
out: list[RawCall] = []
|
|
245
|
+
seen: set = set()
|
|
246
|
+
stack = list(body.children)
|
|
247
|
+
while stack:
|
|
248
|
+
n = stack.pop()
|
|
249
|
+
if n.type == "call_expression":
|
|
250
|
+
fn = n.child_by_field_name("function")
|
|
251
|
+
call = None
|
|
252
|
+
if fn is not None and fn.type == "identifier":
|
|
253
|
+
call = RawCall(name=txt(fn))
|
|
254
|
+
elif fn is not None and fn.type == "field_expression":
|
|
255
|
+
arg = fn.child_by_field_name("argument")
|
|
256
|
+
fld = fn.child_by_field_name("field")
|
|
257
|
+
recv = txt(arg) if arg is not None and arg.type in ("identifier", "this") else None
|
|
258
|
+
if fld is not None:
|
|
259
|
+
call = RawCall(name=txt(fld), receiver=recv)
|
|
260
|
+
elif fn is not None and fn.type == "qualified_identifier":
|
|
261
|
+
segs, final = split_qualified(fn)
|
|
262
|
+
if final:
|
|
263
|
+
call = RawCall(name=final, scope=segs[-1] if segs else None)
|
|
264
|
+
if call is not None and call.name:
|
|
265
|
+
key = (call.name, call.receiver, call.scope, call.is_new)
|
|
266
|
+
if key not in seen:
|
|
267
|
+
seen.add(key)
|
|
268
|
+
out.append(call)
|
|
269
|
+
elif n.type == "new_expression":
|
|
270
|
+
nm = first_type_name(n.child_by_field_name("type"))
|
|
271
|
+
if nm:
|
|
272
|
+
key = (nm, None, None, True)
|
|
273
|
+
if key not in seen:
|
|
274
|
+
seen.add(key)
|
|
275
|
+
out.append(RawCall(name=nm, is_new=True))
|
|
276
|
+
stack.extend(n.children)
|
|
277
|
+
return out
|
|
278
|
+
|
|
279
|
+
# -- members -------------------------------------------------------------------------
|
|
280
|
+
|
|
281
|
+
def method_from_declarator(node, fdecl, class_name: str, kind_hint: str | None,
|
|
282
|
+
is_def: bool) -> RawMethod | None:
|
|
283
|
+
name_node = fdecl.child_by_field_name("declarator")
|
|
284
|
+
leaf = name_node_of(name_node) if name_node is not None else None
|
|
285
|
+
if leaf is None:
|
|
286
|
+
return None
|
|
287
|
+
if leaf.type == "destructor_name":
|
|
288
|
+
name, kind = txt(leaf), "DESTRUCTOR"
|
|
289
|
+
elif leaf.type == "identifier" and txt(leaf) == class_name:
|
|
290
|
+
name, kind = txt(leaf), "CONSTRUCTOR"
|
|
291
|
+
else:
|
|
292
|
+
name, kind = txt(leaf), (kind_hint or "ORDINARY")
|
|
293
|
+
ret = None if kind in ("CONSTRUCTOR", "DESTRUCTOR") else type_text(node.child_by_field_name("type"))
|
|
294
|
+
body = node.child_by_field_name("body")
|
|
295
|
+
ls, le = lines(node)
|
|
296
|
+
return RawMethod(
|
|
297
|
+
name=name, kind=kind, return_type=ret, params=parameters(fdecl),
|
|
298
|
+
is_static=is_static(node), is_virtual=is_virtual(node), is_const=is_const_method(fdecl),
|
|
299
|
+
is_definition=is_def, calls=collect_calls(body) if body is not None else [],
|
|
300
|
+
source=txt(node), doc=doc_of(node), line_start=ls, line_end=le,
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
def build_members(field_list, rt: RawType) -> None:
|
|
304
|
+
for m in field_list.children:
|
|
305
|
+
if m.type == "access_specifier":
|
|
306
|
+
continue # (access tracking is not currently exposed; kept simple)
|
|
307
|
+
if m.type in _TYPE_SPECIFIERS or m.type == "enum_specifier":
|
|
308
|
+
nested = build_type(m, rt.namespace)
|
|
309
|
+
if nested is not None:
|
|
310
|
+
rt.nested_types.append(nested)
|
|
311
|
+
elif m.type == "template_declaration":
|
|
312
|
+
inner, tparams = _unwrap_template(m)
|
|
313
|
+
if inner is not None and (inner.type in _TYPE_SPECIFIERS or inner.type == "enum_specifier"):
|
|
314
|
+
nested = build_type(inner, rt.namespace, outer=m)
|
|
315
|
+
if nested is not None:
|
|
316
|
+
nested.type_parameters = tparams
|
|
317
|
+
rt.nested_types.append(nested)
|
|
318
|
+
elif m.type == "function_definition":
|
|
319
|
+
fdecl = find_function_declarator(m.child_by_field_name("declarator"))
|
|
320
|
+
if fdecl is not None:
|
|
321
|
+
meth = method_from_declarator(m, fdecl, rt.name, None, is_def=True)
|
|
322
|
+
if meth is not None:
|
|
323
|
+
rt.methods.append(meth)
|
|
324
|
+
elif m.type == "field_declaration":
|
|
325
|
+
decl = m.child_by_field_name("declarator")
|
|
326
|
+
fdecl = find_function_declarator(decl) if decl is not None else None
|
|
327
|
+
if fdecl is not None: # a method declaration (`int f() const;`, `static int g();`)
|
|
328
|
+
meth = method_from_declarator(m, fdecl, rt.name, "ORDINARY", is_def=False)
|
|
329
|
+
if meth is not None:
|
|
330
|
+
rt.methods.append(meth)
|
|
331
|
+
else: # a member variable
|
|
332
|
+
nnode = name_node_of(decl) if decl is not None else None
|
|
333
|
+
fname = txt(nnode) if nnode is not None and nnode.type in _NAME_LEAVES else ""
|
|
334
|
+
if fname:
|
|
335
|
+
ls, le = lines(m)
|
|
336
|
+
rt.fields.append(RawField(name=fname, type=written_type(m, nnode),
|
|
337
|
+
is_static=is_static(m), line_start=ls, line_end=le))
|
|
338
|
+
elif m.type == "declaration": # ctor/dtor prototype (no return type)
|
|
339
|
+
decl = m.child_by_field_name("declarator")
|
|
340
|
+
fdecl = find_function_declarator(decl) if decl is not None else None
|
|
341
|
+
if fdecl is not None:
|
|
342
|
+
meth = method_from_declarator(m, fdecl, rt.name, "ORDINARY", is_def=False)
|
|
343
|
+
if meth is not None:
|
|
344
|
+
rt.methods.append(meth)
|
|
345
|
+
|
|
346
|
+
def build_type(spec, namespace: str, outer=None) -> RawType | None:
|
|
347
|
+
if spec.type == "enum_specifier":
|
|
348
|
+
return build_enum(spec, namespace, outer)
|
|
349
|
+
kind = _TYPE_SPECIFIERS[spec.type]
|
|
350
|
+
name_node = spec.child_by_field_name("name")
|
|
351
|
+
if name_node is None:
|
|
352
|
+
return None # anonymous struct/union/class -- no stable identity, skip
|
|
353
|
+
body = spec.child_by_field_name("body")
|
|
354
|
+
ls, le = lines(spec)
|
|
355
|
+
rt = RawType(kind=kind, name=txt(name_node), namespace=namespace,
|
|
356
|
+
is_definition=body is not None, source=txt(spec), doc=doc_of(spec, outer),
|
|
357
|
+
line_start=ls, line_end=le)
|
|
358
|
+
rt.bases = base_classes(spec, default_access="public" if kind in ("Struct", "Union") else "private")
|
|
359
|
+
if body is not None:
|
|
360
|
+
build_members(body, rt)
|
|
361
|
+
return rt
|
|
362
|
+
|
|
363
|
+
def base_classes(spec, default_access: str) -> list[RawBase]:
|
|
364
|
+
clause = next((c for c in spec.children if c.type == "base_class_clause"), None)
|
|
365
|
+
if clause is None:
|
|
366
|
+
return []
|
|
367
|
+
out: list[RawBase] = []
|
|
368
|
+
access = default_access
|
|
369
|
+
for c in clause.children:
|
|
370
|
+
if c.type == "access_specifier":
|
|
371
|
+
access = txt(c)
|
|
372
|
+
elif c.type in ("type_identifier", "template_type", "qualified_identifier"):
|
|
373
|
+
nm = first_type_name(c)
|
|
374
|
+
if nm:
|
|
375
|
+
out.append(RawBase(name=nm, access=access))
|
|
376
|
+
access = default_access # reset for the next base
|
|
377
|
+
return out
|
|
378
|
+
|
|
379
|
+
def build_enum(spec, namespace: str, outer=None) -> RawType:
|
|
380
|
+
name_node = spec.child_by_field_name("name")
|
|
381
|
+
body = spec.child_by_field_name("body")
|
|
382
|
+
ls, le = lines(spec)
|
|
383
|
+
rt = RawType(kind="Enum", name=txt(name_node) if name_node is not None else "",
|
|
384
|
+
namespace=namespace, is_scoped=has_child_token(spec, "class") or has_child_token(spec, "struct"),
|
|
385
|
+
is_definition=body is not None, source=txt(spec), doc=doc_of(spec, outer),
|
|
386
|
+
line_start=ls, line_end=le)
|
|
387
|
+
if body is not None:
|
|
388
|
+
for e in body.children:
|
|
389
|
+
if e.type == "enumerator":
|
|
390
|
+
rt.enum_members.append(txt(e.child_by_field_name("name")))
|
|
391
|
+
return rt
|
|
392
|
+
|
|
393
|
+
# -- free functions / out-of-line member defs ----------------------------------------
|
|
394
|
+
|
|
395
|
+
def build_function_or_memberdef(node, namespace: str, is_def: bool, outer=None) -> None:
|
|
396
|
+
decl = node.child_by_field_name("declarator")
|
|
397
|
+
fdecl = find_function_declarator(decl) if decl is not None else None
|
|
398
|
+
if fdecl is None:
|
|
399
|
+
return # a variable declaration at namespace scope -- not modelled (no globals tier)
|
|
400
|
+
name_node = fdecl.child_by_field_name("declarator")
|
|
401
|
+
leaf = name_node_of(name_node) if name_node is not None else None
|
|
402
|
+
if leaf is None:
|
|
403
|
+
return
|
|
404
|
+
body = node.child_by_field_name("body")
|
|
405
|
+
ls, le = lines(node)
|
|
406
|
+
if leaf.type == "qualified_identifier":
|
|
407
|
+
segs, final = split_qualified(leaf)
|
|
408
|
+
owner = segs[-1] if segs else None
|
|
409
|
+
if final.startswith("~"):
|
|
410
|
+
kind = "DESTRUCTOR"
|
|
411
|
+
elif owner is not None and final == owner:
|
|
412
|
+
kind = "CONSTRUCTOR"
|
|
413
|
+
else:
|
|
414
|
+
kind = "ORDINARY"
|
|
415
|
+
ret = None if kind in ("CONSTRUCTOR", "DESTRUCTOR") else type_text(node.child_by_field_name("type"))
|
|
416
|
+
rf.member_defs.append(RawMethod(
|
|
417
|
+
name=final, kind=kind, return_type=ret, params=parameters(fdecl),
|
|
418
|
+
is_static=is_static(node), is_const=is_const_method(fdecl),
|
|
419
|
+
is_definition=body is not None, calls=collect_calls(body) if body is not None else [],
|
|
420
|
+
owner_name=owner, owner_scope=segs, namespace=namespace,
|
|
421
|
+
source=txt(node), doc=doc_of(node, outer), line_start=ls, line_end=le,
|
|
422
|
+
))
|
|
423
|
+
else: # plain identifier -> a free function
|
|
424
|
+
name = txt(leaf)
|
|
425
|
+
if not name:
|
|
426
|
+
return
|
|
427
|
+
rf.functions.append(RawFunction(
|
|
428
|
+
name=name, namespace=namespace,
|
|
429
|
+
return_type=type_text(node.child_by_field_name("type")),
|
|
430
|
+
params=parameters(fdecl), is_definition=is_def,
|
|
431
|
+
calls=collect_calls(body) if body is not None else [],
|
|
432
|
+
signature=signature_of(node, decl), source=txt(node), doc=doc_of(node, outer),
|
|
433
|
+
line_start=ls, line_end=le,
|
|
434
|
+
))
|
|
435
|
+
|
|
436
|
+
# -- template unwrapping -------------------------------------------------------------
|
|
437
|
+
|
|
438
|
+
def _unwrap_template(node):
|
|
439
|
+
"""(inner declaration node, template type-parameter names). Handles a single template
|
|
440
|
+
prefix; nested `template<>` layers are unwrapped recursively."""
|
|
441
|
+
tparams: list[str] = []
|
|
442
|
+
tpl = node.child_by_field_name("parameters")
|
|
443
|
+
if tpl is not None:
|
|
444
|
+
for c in tpl.named_children:
|
|
445
|
+
if c.type in ("type_parameter_declaration", "template_template_parameter_declaration"):
|
|
446
|
+
ident = next((g for g in c.children if g.type == "type_identifier"), None)
|
|
447
|
+
if ident is not None:
|
|
448
|
+
tparams.append(txt(ident))
|
|
449
|
+
inner = next((c for c in node.children
|
|
450
|
+
if c.type in _TYPE_SPECIFIERS or c.type == "enum_specifier"
|
|
451
|
+
or c.type in ("function_definition", "declaration", "template_declaration")), None)
|
|
452
|
+
if inner is not None and inner.type == "template_declaration":
|
|
453
|
+
inner, more = _unwrap_template(inner)
|
|
454
|
+
tparams.extend(more)
|
|
455
|
+
return inner, tparams
|
|
456
|
+
|
|
457
|
+
# -- top-level / namespace walk ------------------------------------------------------
|
|
458
|
+
|
|
459
|
+
def walk(node, ns_path: list[str]) -> None:
|
|
460
|
+
for child in node.children:
|
|
461
|
+
kind = child.type
|
|
462
|
+
if kind == "namespace_definition":
|
|
463
|
+
name_node = child.child_by_field_name("name")
|
|
464
|
+
body = child.child_by_field_name("body")
|
|
465
|
+
if name_node is None: # anonymous namespace -- attribute contents to the parent
|
|
466
|
+
new_ns = ns_path
|
|
467
|
+
elif name_node.type == "nested_namespace_specifier":
|
|
468
|
+
new_ns = ns_path + [txt(g) for g in name_node.children if g.type == "namespace_identifier"]
|
|
469
|
+
else:
|
|
470
|
+
new_ns = ns_path + [txt(name_node)]
|
|
471
|
+
if body is not None:
|
|
472
|
+
walk(body, new_ns)
|
|
473
|
+
elif kind in _TRANSPARENT or kind == "declaration_list":
|
|
474
|
+
walk(child, ns_path)
|
|
475
|
+
elif kind == "preproc_include":
|
|
476
|
+
p = child.child_by_field_name("path")
|
|
477
|
+
if p is not None:
|
|
478
|
+
rf.includes.append(RawInclude(path=txt(p).strip('<>"'),
|
|
479
|
+
is_system=p.type == "system_lib_string"))
|
|
480
|
+
elif kind == "preproc_def":
|
|
481
|
+
ls, le = lines(child)
|
|
482
|
+
rf.macros.append(RawMacro(name=txt(child.child_by_field_name("name")),
|
|
483
|
+
is_function_like=False, source=txt(child).rstrip(),
|
|
484
|
+
line_start=ls, line_end=le))
|
|
485
|
+
elif kind == "preproc_function_def":
|
|
486
|
+
ls, le = lines(child)
|
|
487
|
+
rf.macros.append(RawMacro(name=txt(child.child_by_field_name("name")),
|
|
488
|
+
is_function_like=True, source=txt(child).rstrip(),
|
|
489
|
+
line_start=ls, line_end=le))
|
|
490
|
+
elif kind in _TYPE_SPECIFIERS or kind == "enum_specifier":
|
|
491
|
+
rt = build_type(child, "::".join(ns_path))
|
|
492
|
+
if rt is not None:
|
|
493
|
+
rf.types.append(rt)
|
|
494
|
+
elif kind == "template_declaration":
|
|
495
|
+
inner, tparams = _unwrap_template(child)
|
|
496
|
+
if inner is None:
|
|
497
|
+
continue
|
|
498
|
+
if inner.type in _TYPE_SPECIFIERS or inner.type == "enum_specifier":
|
|
499
|
+
rt = build_type(inner, "::".join(ns_path), outer=child)
|
|
500
|
+
if rt is not None:
|
|
501
|
+
rt.type_parameters = tparams
|
|
502
|
+
rf.types.append(rt)
|
|
503
|
+
elif inner.type in ("function_definition", "declaration"):
|
|
504
|
+
build_function_or_memberdef(inner, "::".join(ns_path), is_def=inner.type == "function_definition", outer=child)
|
|
505
|
+
elif kind == "function_definition":
|
|
506
|
+
build_function_or_memberdef(child, "::".join(ns_path), is_def=True)
|
|
507
|
+
elif kind == "declaration":
|
|
508
|
+
build_function_or_memberdef(child, "::".join(ns_path), is_def=False)
|
|
509
|
+
|
|
510
|
+
walk(tree.root_node, [])
|
|
511
|
+
return rf
|