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
//! `Npa` implementation for C#.
#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
#![allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss
)]
use super::*;
impl Npa for CsharpCode {
fn compute<'a>(node: &Node<'a>, _code: &'a [u8], stats: &mut Stats) {
use Csharp::*;
if Self::is_func_space(node) && stats.is_disabled() {
stats.is_class_space = true;
}
// Class / struct / record / interface bodies all share
// `DeclarationList`; the parent kind disambiguates.
if !matches!(node.kind_id().into(), DeclarationList) {
return;
}
let Some(parent_kind) = node.parent().map(|p| p.kind_id().into()) else {
return;
};
match parent_kind {
// For `RecordDeclaration`, only explicit body fields are
// counted. The implicit `parameter_list` of a positional
// record (`record Person(string Name, int Age);`) is not
// walked here — its parameters become auto-generated public
// properties at the IL level, but modelling them would
// require synthesizing nodes that don't appear in the AST.
ClassDeclaration | StructDeclaration | RecordDeclaration => {
for declaration in node
.children()
.filter(|c| matches!(c.kind_id().into(), FieldDeclaration))
{
let attributes = csharp_count_field_declarators(&declaration);
stats.class_na += attributes;
if csharp_is_explicit_public(&declaration) {
stats.class_npa += attributes;
}
}
}
// C# 8+ interfaces can declare fields with explicit modifiers
// (rare); members declared without an explicit modifier default
// to public, mirroring Java's interface convention.
InterfaceDeclaration => {
for declaration in node
.children()
.filter(|c| matches!(c.kind_id().into(), FieldDeclaration))
{
let attributes = csharp_count_field_declarators(&declaration);
stats.interface_na += attributes;
// The modifier applies to every declarator of the field,
// so the public/private split is per-declaration: count
// all declarators as public unless the field is explicitly
// private/protected.
if csharp_interface_member_is_public(&declaration) {
stats.interface_npa += attributes;
}
}
}
_ => {}
}
}
}
// Count `VariableDeclarator`s nested under any aliased `VariableDeclaration`
// inside a C# `FieldDeclaration`. Both kinds emit two aliased `kind_id`s
// each; the macros centralize the alias union (lesson #2).
fn csharp_count_field_declarators(field_decl: &Node) -> usize {
field_decl
.children()
.filter(|c| matches!(c.kind_id().into(), csharp_var_decl_kinds!()))
.flat_map(|c| c.children())
.filter(|c| matches!(c.kind_id().into(), csharp_var_declarator_kinds!()))
.count()
}