]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_passes/src/node_count.rs
Auto merge of #104679 - dvdhrm:rw/dso, r=petrochenkov
[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     }
22     fn visit_foreign_item(&mut self, i: &ForeignItem) {
23         self.count += 1;
24         walk_foreign_item(self, i)
25     }
26     fn visit_item(&mut self, i: &Item) {
27         self.count += 1;
28         walk_item(self, i)
29     }
30     fn visit_local(&mut self, l: &Local) {
31         self.count += 1;
32         walk_local(self, l)
33     }
34     fn visit_block(&mut self, b: &Block) {
35         self.count += 1;
36         walk_block(self, b)
37     }
38     fn visit_stmt(&mut self, s: &Stmt) {
39         self.count += 1;
40         walk_stmt(self, s)
41     }
42     fn visit_arm(&mut self, a: &Arm) {
43         self.count += 1;
44         walk_arm(self, a)
45     }
46     fn visit_pat(&mut self, p: &Pat) {
47         self.count += 1;
48         walk_pat(self, p)
49     }
50     fn visit_expr(&mut self, ex: &Expr) {
51         self.count += 1;
52         walk_expr(self, ex)
53     }
54     fn visit_ty(&mut self, t: &Ty) {
55         self.count += 1;
56         walk_ty(self, t)
57     }
58     fn visit_generic_param(&mut self, param: &GenericParam) {
59         self.count += 1;
60         walk_generic_param(self, param)
61     }
62     fn visit_generics(&mut self, g: &Generics) {
63         self.count += 1;
64         walk_generics(self, g)
65     }
66     fn visit_fn(&mut self, fk: visit::FnKind<'_>, _: Span, _: NodeId) {
67         self.count += 1;
68         walk_fn(self, fk)
69     }
70     fn visit_assoc_item(&mut self, ti: &AssocItem, ctxt: AssocCtxt) {
71         self.count += 1;
72         walk_assoc_item(self, ti, ctxt);
73     }
74     fn visit_trait_ref(&mut self, t: &TraitRef) {
75         self.count += 1;
76         walk_trait_ref(self, t)
77     }
78     fn visit_param_bound(&mut self, bounds: &GenericBound, _ctxt: BoundKind) {
79         self.count += 1;
80         walk_param_bound(self, bounds)
81     }
82     fn visit_poly_trait_ref(&mut self, t: &PolyTraitRef) {
83         self.count += 1;
84         walk_poly_trait_ref(self, t)
85     }
86     fn visit_variant_data(&mut self, s: &VariantData) {
87         self.count += 1;
88         walk_struct_def(self, s)
89     }
90     fn visit_field_def(&mut self, s: &FieldDef) {
91         self.count += 1;
92         walk_field_def(self, s)
93     }
94     fn visit_enum_def(&mut self, enum_definition: &EnumDef) {
95         self.count += 1;
96         walk_enum_def(self, enum_definition)
97     }
98     fn visit_variant(&mut self, v: &Variant) {
99         self.count += 1;
100         walk_variant(self, v)
101     }
102     fn visit_lifetime(&mut self, lifetime: &Lifetime, _: visit::LifetimeCtxt) {
103         self.count += 1;
104         walk_lifetime(self, lifetime)
105     }
106     fn visit_mac_call(&mut self, mac: &MacCall) {
107         self.count += 1;
108         walk_mac(self, mac)
109     }
110     fn visit_path(&mut self, path: &Path, _id: NodeId) {
111         self.count += 1;
112         walk_path(self, path)
113     }
114     fn visit_use_tree(&mut self, use_tree: &UseTree, id: NodeId, _nested: bool) {
115         self.count += 1;
116         walk_use_tree(self, use_tree, id)
117     }
118     fn visit_generic_args(&mut self, generic_args: &GenericArgs) {
119         self.count += 1;
120         walk_generic_args(self, generic_args)
121     }
122     fn visit_assoc_constraint(&mut self, constraint: &AssocConstraint) {
123         self.count += 1;
124         walk_assoc_constraint(self, constraint)
125     }
126     fn visit_attribute(&mut self, _attr: &Attribute) {
127         self.count += 1;
128     }
129 }