1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
use std::collections::HashSet;
use tree_sitter::Node;
use super::qualifiers::{
member_name_text, resolve_call_chain_tail, resolve_member_qualifier, Scope, MEMBER_SCOPE_NODES,
};
use super::receivers::type_fact;
use super::refs::{
invocation_arg_count, push_ctor_param_ref, push_member_ref, record_base_list,
record_single_type, registration_fact, type_parameter_names,
};
use super::text::{
declared_name, format_segments, named_children, namespace_level_types, new_parser, text,
type_kind_label, type_segment_parts, SegmentParts,
};
use super::type_defs::{record_enum_members, record_type_def, record_using};
use super::types::{Extraction, Fact};
// walk_list mutates its local `ns` mid-iteration for a FILE-SCOPED
// namespace (`namespace X;`): tree-sitter parses its declared name as the
// node's only child, and every member that follows is a SIBLING of that
// node under the same parent, not a descendant of it. So the namespace
// switch happens while iterating a sibling list, updating ns for the
// remaining siblings in that same list -- a per-node match arm can't
// express that, hence walk_list and walk staying separate functions.
fn walk_list<'a>(
nodes: Vec<Node<'a>>,
mut ns: String,
type_stack: &[String],
src: &[u8],
out: &mut Extraction,
scope: &Scope<'a>,
) {
for node in nodes {
if node.kind() == "file_scoped_namespace_declaration" {
let name = node
.child_by_field_name("name")
.map(|n| text(n, src))
.unwrap_or_default();
ns = if ns.is_empty() {
name
} else {
format!("{ns}.{name}")
};
continue;
}
walk(node, &ns, type_stack, src, out, scope);
}
}
// type_stack (see type_id) also doubles as the "am I inside a type" signal
// for nothing else -- ref extraction runs at every depth regardless of
// nesting.
#[allow(
clippy::too_many_lines,
reason = "one recursive descent over every node kind the extractor cares about, sharing the same ns/type_stack/scope state at each depth"
)]
fn walk<'a>(
node: Node<'a>,
ns: &str,
type_stack: &[String],
src: &[u8],
out: &mut Extraction,
scope: &Scope<'a>,
) {
// Entering a member declaration installs a fresh (lazily built)
// local/param table over the enclosing type's field table, for this
// subtree only.
let member_scope;
let scope: &Scope<'a> = if MEMBER_SCOPE_NODES.contains(&node.kind()) {
member_scope = scope.for_member(node);
&member_scope
} else {
scope
};
match node.kind() {
"namespace_declaration" => {
let name = node
.child_by_field_name("name")
.map(|n| text(n, src))
.unwrap_or_default();
let new_ns = if ns.is_empty() {
name
} else {
format!("{ns}.{name}")
};
walk_list(named_children(node), new_ns, type_stack, src, out, scope);
}
"using_directive" => {
record_using(node, src, &mut out.usings, &mut out.refs);
}
"class_declaration"
| "interface_declaration"
| "struct_declaration"
| "record_declaration" => {
let kind = type_kind_label(node.kind()).expect("matched TYPE_KINDS arm");
// Computed before record_type_def (Scope::for_type
// below recomputes its own copy for the type-facts table; cheap
// and kept separate rather than threading one instance through
// both call sites).
let type_params = type_parameter_names(node, src);
record_type_def(
node,
ns,
kind,
type_stack,
src,
&mut out.defs,
&mut out.names,
&type_params,
);
record_base_list(node, ns, type_stack, src, &mut out.refs);
let name = declared_name(node, src);
let new_stack: Vec<String> = if name.is_empty() {
type_stack.to_vec()
} else {
let mut s = type_stack.to_vec();
s.push(name);
s
};
// A type declaration opens a new field/primary-ctor table and
// closes any enclosing member scope (`node: None` -- a type body
// is not a member body).
let type_scope = Scope::for_type(node, src);
walk_list(
named_children(node),
ns.to_string(),
&new_stack,
src,
out,
&type_scope,
);
}
"enum_declaration" => {
record_type_def(
node,
ns,
"enum",
type_stack,
src,
&mut out.defs,
&mut out.names,
&HashSet::new(),
);
record_enum_members(node, ns, type_stack, src, &mut out.defs);
// No recursion into the enum body: enum member initializer
// expressions are not walked.
}
"delegate_declaration" => {
record_type_def(
node,
ns,
"delegate",
type_stack,
src,
&mut out.defs,
&mut out.names,
&HashSet::new(),
);
record_single_type(
node.child_by_field_name("type"),
"uses-type",
ns,
type_stack,
src,
&mut out.refs,
);
walk_list(
named_children(node),
ns.to_string(),
type_stack,
src,
out,
scope,
);
}
"field_declaration" => {
let vd = named_children(node)
.into_iter()
.find(|c| c.kind() == "variable_declaration");
record_single_type(
vd.and_then(|v| v.child_by_field_name("type")),
"uses-type",
ns,
type_stack,
src,
&mut out.refs,
);
walk_list(
named_children(node),
ns.to_string(),
type_stack,
src,
out,
scope,
);
}
"property_declaration" => {
record_single_type(
node.child_by_field_name("type"),
"uses-type",
ns,
type_stack,
src,
&mut out.refs,
);
walk_list(
named_children(node),
ns.to_string(),
type_stack,
src,
out,
scope,
);
}
"parameter" => {
record_single_type(
node.child_by_field_name("type"),
"uses-type",
ns,
type_stack,
src,
&mut out.refs,
);
walk_list(
named_children(node),
ns.to_string(),
type_stack,
src,
out,
scope,
);
}
// One 'ctor-param' fact per constructor parameter,
// ALONGSIDE (never instead of) the plain 'uses-type' ref the
// "parameter" arm above still emits as walk_list recurses into this
// node's own parameter_list below. Uses the same type_fact helper
// stage-2 field/local receiver facts use, so a parameter's generic
// arguments survive as descriptors instead of being stripped to a
// bare name the way the general 'uses-type' ladder strips them.
// scope.type_params is already the enclosing TYPE's own parameters
// here (MEMBER_SCOPE_NODES swapped in a member scope above with the
// SAME type_params -- a C# constructor cannot declare type
// parameters of its own).
"constructor_declaration" => {
if let Some(params) = node.child_by_field_name("parameters") {
for p in named_children(params) {
if p.kind() != "parameter" {
continue;
}
let type_node = p.child_by_field_name("type");
if let Some(fact) = type_fact(type_node, src, &scope.type_params) {
let line = type_node.map(|t| t.start_position().row + 1).unwrap_or(0);
push_ctor_param_ref(
&mut out.refs,
fact.type_name,
line,
ns.to_string(),
fact.args,
type_stack,
);
}
}
}
walk_list(
named_children(node),
ns.to_string(),
type_stack,
src,
out,
scope,
);
}
// `Method(out SomeEnum x)` / `obj is SomeEnum x` inline-declaration
// sites -- a `type` + `name` pair, same shape as `parameter`, just in
// expression position instead of a parameter list. Same
// record_single_type pipeline, so it goes through the normal ladder
// including ambiguous marking, exactly like an ordinary type usage.
"declaration_expression" => {
record_single_type(
node.child_by_field_name("type"),
"uses-type",
ns,
type_stack,
src,
&mut out.refs,
);
walk_list(
named_children(node),
ns.to_string(),
type_stack,
src,
out,
scope,
);
}
"method_declaration" => {
record_single_type(
node.child_by_field_name("returns"),
"uses-type",
ns,
type_stack,
src,
&mut out.refs,
);
walk_list(
named_children(node),
ns.to_string(),
type_stack,
src,
out,
scope,
);
}
"object_creation_expression" => {
record_single_type(
node.child_by_field_name("type"),
"uses-type",
ns,
type_stack,
src,
&mut out.refs,
);
walk_list(
named_children(node),
ns.to_string(),
type_stack,
src,
out,
scope,
);
}
"typeof_expression" => {
record_single_type(
node.child_by_field_name("type"),
"uses-type",
ns,
type_stack,
src,
&mut out.refs,
);
walk_list(
named_children(node),
ns.to_string(),
type_stack,
src,
out,
scope,
);
}
// Covers every position a member access can appear in -- cast-to-int
// values over a work-type enum, argument lists, switch/pattern arms,
// initializers -- because none of those container node types are
// special-cased above, so they all reach here via the default walk.
"member_access_expression" => {
// A registration fact needs only that this access is
// actually invoked -- the shape rule itself (name pattern, exactly
// two type arguments) lives in `registration_fact`. Independent of
// the uses-member candidate built below: the same call keeps
// recording its ordinary uses-member/uses-type refs unchanged.
if invocation_arg_count(node).is_some() {
if let Some(reg) = registration_fact(node, ns, src) {
out.registrations.push(reg);
}
}
let expr_field = node.child_by_field_name("expression");
// The member itself can be a generic_name too ("Foo.Bar<T>(...)"):
// normalize to the bare method name so the resolver's method-list
// membership check sees the name the def actually recorded.
let member = member_name_text(node.child_by_field_name("name"), src);
let qualifier = resolve_member_qualifier(expr_field, src, type_stack, scope);
if let (Some(q), Some(m)) = (&qualifier, &member) {
if !m.is_empty() {
push_member_ref(
&mut out.refs,
&q.text,
m.clone(),
node.start_position().row + 1,
ns.to_string(),
q.generic,
q.receiver.clone(),
// Asked of THIS node, so a chain window answers for its
// own call and never for the one wrapping it.
invocation_arg_count(node),
type_stack,
q.property_owner.clone(),
q.receiver_base,
q.receiver_local,
);
}
} else if let (Some(qn), Some(m)) = (expr_field, &member) {
// Unit C, chain tail: `resolve_member_qualifier` structurally
// NEVER succeeds here (`member_qualifier_info` has no arm
// for `invocation_expression`), so this is a true fallback,
// never a double emission for the same window.
if !m.is_empty() {
if let Some((owner, inner_member, head_is_base)) =
resolve_call_chain_tail(qn, src, type_stack, scope)
{
push_member_ref(
&mut out.refs,
// The qualifier's OWN source text ("a.B()",
// "Repo.Load()", ...) rather than a name that
// could coincidentally collide with a real
// type: an invocation_expression's span always
// ends at its own closing ')', a character no
// C# identifier or dotted type name can ever
// contain, so `push_member_ref`'s `name`/
// `qualified` split can never accidentally
// resolve as a real def. `receiver_type` stays
// `None` (a call-shaped Fact, not a resolved
// type) -- resolution goes through the same
// one-hop `method_returns` path a `var x =
// Q.M()` local already uses.
&text(qn, src),
m.clone(),
node.start_position().row + 1,
ns.to_string(),
false,
Some(Fact {
type_name: owner,
args: None,
call: Some(inner_member),
awaited: false,
is_array: false,
lambda: None,
}),
invocation_arg_count(node),
type_stack,
None,
// `base.Make().Validate()`: the hop must read
// Make's return type off the BASE that declares
// it, never off an enclosing type that hides
// Make with its own.
head_is_base,
false,
);
}
}
}
walk_list(
named_children(node),
ns.to_string(),
type_stack,
src,
out,
scope,
);
}
// `a?.B` / `a?.B(...)` -- a conditional-access window emits the
// SAME uses-member ref an unconditional `a.B` would, through the
// identical qualifier resolution (`resolve_member_qualifier`); only
// the line (this node's own start row) and the arg-count/binding
// shapes differ structurally. `member_binding_expression` is a
// CHILD of this node, not a field (confirmed against the shipped
// grammar's node-types), so it is found by kind rather than
// `child_by_field_name`. `element_binding_expression` (`a?[i]`) is
// the conditional_access_expression's other possible child and is
// not a member access at all -- no ref for it here.
"conditional_access_expression" => {
let condition = node.child_by_field_name("condition");
let binding = named_children(node)
.into_iter()
.find(|c| c.kind() == "member_binding_expression");
if let Some(binding) = binding {
let member = member_name_text(binding.child_by_field_name("name"), src);
let qualifier = resolve_member_qualifier(condition, src, type_stack, scope);
if let (Some(q), Some(m)) = (&qualifier, &member) {
if !m.is_empty() {
push_member_ref(
&mut out.refs,
&q.text,
m.clone(),
node.start_position().row + 1,
ns.to_string(),
q.generic,
q.receiver.clone(),
// Asked of the conditional_access_expression
// itself -- that is the node an enclosing
// `invocation_expression`'s `function` field
// names when the binding is invoked
// (`a?.B()`), never the inner binding.
invocation_arg_count(node),
type_stack,
q.property_owner.clone(),
q.receiver_base,
q.receiver_local,
);
}
}
}
walk_list(
named_children(node),
ns.to_string(),
type_stack,
src,
out,
scope,
);
}
"type_argument_list" => {
for arg in named_children(node) {
record_single_type(Some(arg), "uses-type", ns, type_stack, src, &mut out.refs);
}
walk_list(
named_children(node),
ns.to_string(),
type_stack,
src,
out,
scope,
);
}
_ => {
walk_list(
named_children(node),
ns.to_string(),
type_stack,
src,
out,
scope,
);
}
}
}
// ---------------------------------------------------------------------------
// Entry points.
// ---------------------------------------------------------------------------
/// Extract both outputs from one parse of C# `source`: the file's purpose
/// signature and its graph fragment (defs, usings, refs, names).
///
/// The source is first stripped of inactive conditional-compilation arms,
/// so only the arm that would actually compile is indexed.
pub fn extract(source: &str) -> Extraction {
let mut parser = new_parser();
// At most one arm of every `#if` group reaches the parser (see `preproc`).
let source = crate::preproc::strip_inactive(source);
let source = source.as_ref();
let units = crate::parse::utf16_units(source);
let utf16 = crate::parse::utf16_bytes(&units);
let tree = parser
.parse_utf16_le(&units, None)
.expect("parse returned no tree");
let root = tree.root_node();
let src = &utf16[..];
let purpose_parts: Vec<SegmentParts> = namespace_level_types(root)
.into_iter()
.filter_map(|n| type_segment_parts(n, src))
.collect();
let mut out = Extraction {
purpose: None,
defs: Vec::new(),
usings: Vec::new(),
refs: Vec::new(),
names: Vec::new(),
registrations: Vec::new(),
};
walk_list(
named_children(root),
String::new(),
&[],
src,
&mut out,
&Scope::root(),
);
out.purpose = format_segments(purpose_parts);
out
}