]> git.lizzy.rs Git - rust.git/blob - src/librustc_ast_passes/node_count.rs
Rollup merge of #74074 - sunfishcode:windows-openoptionsext-return-type, r=LukasKalbe...
[rust.git] / src / librustc_ast_passes / node_count.rs
1 // Simply gives a rough count of the number of nodes in an AST.
2
3 use rustc_ast::ast::*;
4 use rustc_ast::visit::*;
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_mod(&mut self, m: &Mod, _s: Span, _a: &[Attribute], _n: NodeId) {
24         self.count += 1;
25         walk_mod(self, m)
26     }
27     fn visit_foreign_item(&mut self, i: &ForeignItem) {
28         self.count += 1;
29         walk_foreign_item(self, i)
30     }
31     fn visit_item(&mut self, i: &Item) {
32         self.count += 1;
33         walk_item(self, i)
34     }
35     fn visit_local(&mut self, l: &Local) {
36         self.count += 1;
37         walk_local(self, l)
38     }
39     fn visit_block(&mut self, b: &Block) {
40         self.count += 1;
41         walk_block(self, b)
42     }
43     fn visit_stmt(&mut self, s: &Stmt) {
44         self.count += 1;
45         walk_stmt(self, s)
46     }
47     fn visit_arm(&mut self, a: &Arm) {
48         self.count += 1;
49         walk_arm(self, a)
50     }
51     fn visit_pat(&mut self, p: &Pat) {
52         self.count += 1;
53         walk_pat(self, p)
54     }
55     fn visit_expr(&mut self, ex: &Expr) {
56         self.count += 1;
57         walk_expr(self, ex)
58     }
59     fn visit_ty(&mut self, t: &Ty) {
60         self.count += 1;
61         walk_ty(self, t)
62     }
63     fn visit_generic_param(&mut self, param: &GenericParam) {
64         self.count += 1;
65         walk_generic_param(self, param)
66     }
67     fn visit_generics(&mut self, g: &Generics) {
68         self.count += 1;
69         walk_generics(self, g)
70     }
71     fn visit_fn(&mut self, fk: FnKind<'_>, s: Span, _: NodeId) {
72         self.count += 1;
73         walk_fn(self, fk, s)
74     }
75     fn visit_assoc_item(&mut self, ti: &AssocItem, ctxt: AssocCtxt) {
76         self.count += 1;
77         walk_assoc_item(self, ti, ctxt);
78     }
79     fn visit_trait_ref(&mut self, t: &TraitRef) {
80         self.count += 1;
81         walk_trait_ref(self, t)
82     }
83     fn visit_param_bound(&mut self, bounds: &GenericBound) {
84         self.count += 1;
85         walk_param_bound(self, bounds)
86     }
87     fn visit_poly_trait_ref(&mut self, t: &PolyTraitRef, m: &TraitBoundModifier) {
88         self.count += 1;
89         walk_poly_trait_ref(self, t, m)
90     }
91     fn visit_variant_data(&mut self, s: &VariantData) {
92         self.count += 1;
93         walk_struct_def(self, s)
94     }
95     fn visit_struct_field(&mut self, s: &StructField) {
96         self.count += 1;
97         walk_struct_field(self, s)
98     }
99     fn visit_enum_def(
100         &mut self,
101         enum_definition: &EnumDef,
102         generics: &Generics,
103         item_id: NodeId,
104         _: Span,
105     ) {
106         self.count += 1;
107         walk_enum_def(self, enum_definition, generics, item_id)
108     }
109     fn visit_variant(&mut self, v: &Variant) {
110         self.count += 1;
111         walk_variant(self, v)
112     }
113     fn visit_lifetime(&mut self, lifetime: &Lifetime) {
114         self.count += 1;
115         walk_lifetime(self, lifetime)
116     }
117     fn visit_mac(&mut self, _mac: &MacCall) {
118         self.count += 1;
119         walk_mac(self, _mac)
120     }
121     fn visit_path(&mut self, path: &Path, _id: NodeId) {
122         self.count += 1;
123         walk_path(self, path)
124     }
125     fn visit_use_tree(&mut self, use_tree: &UseTree, id: NodeId, _nested: bool) {
126         self.count += 1;
127         walk_use_tree(self, use_tree, id)
128     }
129     fn visit_generic_args(&mut self, path_span: Span, generic_args: &GenericArgs) {
130         self.count += 1;
131         walk_generic_args(self, path_span, generic_args)
132     }
133     fn visit_assoc_ty_constraint(&mut self, constraint: &AssocTyConstraint) {
134         self.count += 1;
135         walk_assoc_ty_constraint(self, constraint)
136     }
137     fn visit_attribute(&mut self, _attr: &Attribute) {
138         self.count += 1;
139     }
140 }