]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_passes/src/node_count.rs
Auto merge of #98961 - zeevm:issue-98958-fix, r=oli-obk
[rust.git] / compiler / rustc_ast_passes / src / node_count.rs
1 // Simply gives a rough count of the number of nodes in an AST.
2
3 use rustc_ast::visit::*;
4 use rustc_ast::*;
5 use rustc_span::symbol::Ident;
6 use rustc_span::Span;
7
8 pub struct NodeCounter {
9     pub count: usize,
10 }
11
12 impl NodeCounter {
13     pub fn new() -> NodeCounter {
14         NodeCounter { count: 0 }
15     }
16 }
17
18 impl<'ast> Visitor<'ast> for NodeCounter {
19     fn visit_ident(&mut self, ident: Ident) {
20         self.count += 1;
21         walk_ident(self, ident);
22     }
23     fn visit_foreign_item(&mut self, i: &ForeignItem) {
24         self.count += 1;
25         walk_foreign_item(self, i)
26     }
27     fn visit_item(&mut self, i: &Item) {
28         self.count += 1;
29         walk_item(self, i)
30     }
31     fn visit_local(&mut self, l: &Local) {
32         self.count += 1;
33         walk_local(self, l)
34     }
35     fn visit_block(&mut self, b: &Block) {
36         self.count += 1;
37         walk_block(self, b)
38     }
39     fn visit_stmt(&mut self, s: &Stmt) {
40         self.count += 1;
41         walk_stmt(self, s)
42     }
43     fn visit_arm(&mut self, a: &Arm) {
44         self.count += 1;
45         walk_arm(self, a)
46     }
47     fn visit_pat(&mut self, p: &Pat) {
48         self.count += 1;
49         walk_pat(self, p)
50     }
51     fn visit_expr(&mut self, ex: &Expr) {
52         self.count += 1;
53         walk_expr(self, ex)
54     }
55     fn visit_ty(&mut self, t: &Ty) {
56         self.count += 1;
57         walk_ty(self, t)
58     }
59     fn visit_generic_param(&mut self, param: &GenericParam) {
60         self.count += 1;
61         walk_generic_param(self, param)
62     }
63     fn visit_generics(&mut self, g: &Generics) {
64         self.count += 1;
65         walk_generics(self, g)
66     }
67     fn visit_fn(&mut self, fk: visit::FnKind<'_>, s: Span, _: NodeId) {
68         self.count += 1;
69         walk_fn(self, fk, s)
70     }
71     fn visit_assoc_item(&mut self, ti: &AssocItem, ctxt: AssocCtxt) {
72         self.count += 1;
73         walk_assoc_item(self, ti, ctxt);
74     }
75     fn visit_trait_ref(&mut self, t: &TraitRef) {
76         self.count += 1;
77         walk_trait_ref(self, t)
78     }
79     fn visit_param_bound(&mut self, bounds: &GenericBound, _ctxt: BoundKind) {
80         self.count += 1;
81         walk_param_bound(self, bounds)
82     }
83     fn visit_poly_trait_ref(&mut self, t: &PolyTraitRef, m: &TraitBoundModifier) {
84         self.count += 1;
85         walk_poly_trait_ref(self, t, m)
86     }
87     fn visit_variant_data(&mut self, s: &VariantData) {
88         self.count += 1;
89         walk_struct_def(self, s)
90     }
91     fn visit_field_def(&mut self, s: &FieldDef) {
92         self.count += 1;
93         walk_field_def(self, s)
94     }
95     fn visit_enum_def(
96         &mut self,
97         enum_definition: &EnumDef,
98         generics: &Generics,
99         item_id: NodeId,
100         _: Span,
101     ) {
102         self.count += 1;
103         walk_enum_def(self, enum_definition, generics, item_id)
104     }
105     fn visit_variant(&mut self, v: &Variant) {
106         self.count += 1;
107         walk_variant(self, v)
108     }
109     fn visit_lifetime(&mut self, lifetime: &Lifetime, _: visit::LifetimeCtxt) {
110         self.count += 1;
111         walk_lifetime(self, lifetime)
112     }
113     fn visit_mac_call(&mut self, mac: &MacCall) {
114         self.count += 1;
115         walk_mac(self, mac)
116     }
117     fn visit_path(&mut self, path: &Path, _id: NodeId) {
118         self.count += 1;
119         walk_path(self, path)
120     }
121     fn visit_use_tree(&mut self, use_tree: &UseTree, id: NodeId, _nested: bool) {
122         self.count += 1;
123         walk_use_tree(self, use_tree, id)
124     }
125     fn visit_generic_args(&mut self, path_span: Span, generic_args: &GenericArgs) {
126         self.count += 1;
127         walk_generic_args(self, path_span, generic_args)
128     }
129     fn visit_assoc_constraint(&mut self, constraint: &AssocConstraint) {
130         self.count += 1;
131         walk_assoc_constraint(self, constraint)
132     }
133     fn visit_attribute(&mut self, _attr: &Attribute) {
134         self.count += 1;
135     }
136 }