]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/util/node_count.rs
Auto merge of #63692 - iluuu1994:issue-49660, r=sfackler
[rust.git] / src / libsyntax / util / node_count.rs
1 // Simply gives a rought count of the number of nodes in an AST.
2
3 use crate::visit::*;
4 use crate::ast::*;
5 use syntax_pos::Span;
6
7 pub struct NodeCounter {
8     pub count: usize,
9 }
10
11 impl NodeCounter {
12     pub fn new() -> NodeCounter {
13         NodeCounter {
14             count: 0,
15         }
16     }
17 }
18
19 impl<'ast> Visitor<'ast> for NodeCounter {
20     fn visit_ident(&mut self, ident: Ident) {
21         self.count += 1;
22         walk_ident(self, ident);
23     }
24     fn visit_mod(&mut self, m: &Mod, _s: Span, _a: &[Attribute], _n: NodeId) {
25         self.count += 1;
26         walk_mod(self, m)
27     }
28     fn visit_foreign_item(&mut self, i: &ForeignItem) {
29         self.count += 1;
30         walk_foreign_item(self, i)
31     }
32     fn visit_item(&mut self, i: &Item) {
33         self.count += 1;
34         walk_item(self, i)
35     }
36     fn visit_local(&mut self, l: &Local) {
37         self.count += 1;
38         walk_local(self, l)
39     }
40     fn visit_block(&mut self, b: &Block) {
41         self.count += 1;
42         walk_block(self, b)
43     }
44     fn visit_stmt(&mut self, s: &Stmt) {
45         self.count += 1;
46         walk_stmt(self, s)
47     }
48     fn visit_arm(&mut self, a: &Arm) {
49         self.count += 1;
50         walk_arm(self, a)
51     }
52     fn visit_pat(&mut self, p: &Pat) {
53         self.count += 1;
54         walk_pat(self, p)
55     }
56     fn visit_expr(&mut self, ex: &Expr) {
57         self.count += 1;
58         walk_expr(self, ex)
59     }
60     fn visit_ty(&mut self, t: &Ty) {
61         self.count += 1;
62         walk_ty(self, t)
63     }
64     fn visit_generic_param(&mut self, param: &GenericParam) {
65         self.count += 1;
66         walk_generic_param(self, param)
67     }
68     fn visit_generics(&mut self, g: &Generics) {
69         self.count += 1;
70         walk_generics(self, g)
71     }
72     fn visit_fn(&mut self, fk: FnKind<'_>, fd: &FnDecl, s: Span, _: NodeId) {
73         self.count += 1;
74         walk_fn(self, fk, fd, s)
75     }
76     fn visit_trait_item(&mut self, ti: &TraitItem) {
77         self.count += 1;
78         walk_trait_item(self, ti)
79     }
80     fn visit_impl_item(&mut self, ii: &ImplItem) {
81         self.count += 1;
82         walk_impl_item(self, ii)
83     }
84     fn visit_trait_ref(&mut self, t: &TraitRef) {
85         self.count += 1;
86         walk_trait_ref(self, t)
87     }
88     fn visit_param_bound(&mut self, bounds: &GenericBound) {
89         self.count += 1;
90         walk_param_bound(self, bounds)
91     }
92     fn visit_poly_trait_ref(&mut self, t: &PolyTraitRef, m: &TraitBoundModifier) {
93         self.count += 1;
94         walk_poly_trait_ref(self, t, m)
95     }
96     fn visit_variant_data(&mut self, s: &VariantData) {
97         self.count += 1;
98         walk_struct_def(self, s)
99     }
100     fn visit_struct_field(&mut self, s: &StructField) {
101         self.count += 1;
102         walk_struct_field(self, s)
103     }
104     fn visit_enum_def(&mut self, enum_definition: &EnumDef,
105                       generics: &Generics, item_id: NodeId, _: Span) {
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: &Mac) {
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 }