]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/util/node_count.rs
Rollup merge of #34316 - jseyfried:refactor_ast_stmt, r=eddyb
[rust.git] / src / libsyntax / util / node_count.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Simply gives a rought count of the number of nodes in an AST.
12
13 use visit::*;
14 use ast::*;
15 use syntax_pos::Span;
16
17 pub struct NodeCounter {
18     pub count: usize,
19 }
20
21 impl NodeCounter {
22     pub fn new() -> NodeCounter {
23         NodeCounter {
24             count: 0,
25         }
26     }
27 }
28
29 impl Visitor for NodeCounter {
30     fn visit_ident(&mut self, span: Span, ident: Ident) {
31         self.count += 1;
32         walk_ident(self, span, ident);
33     }
34     fn visit_mod(&mut self, m: &Mod, _s: Span, _n: NodeId) {
35         self.count += 1;
36         walk_mod(self, m)
37     }
38     fn visit_foreign_item(&mut self, i: &ForeignItem) {
39         self.count += 1;
40         walk_foreign_item(self, i)
41     }
42     fn visit_item(&mut self, i: &Item) {
43         self.count += 1;
44         walk_item(self, i)
45     }
46     fn visit_local(&mut self, l: &Local) {
47         self.count += 1;
48         walk_local(self, l)
49     }
50     fn visit_block(&mut self, b: &Block) {
51         self.count += 1;
52         walk_block(self, b)
53     }
54     fn visit_stmt(&mut self, s: &Stmt) {
55         self.count += 1;
56         walk_stmt(self, s)
57     }
58     fn visit_arm(&mut self, a: &Arm) {
59         self.count += 1;
60         walk_arm(self, a)
61     }
62     fn visit_pat(&mut self, p: &Pat) {
63         self.count += 1;
64         walk_pat(self, p)
65     }
66     fn visit_expr(&mut self, ex: &Expr) {
67         self.count += 1;
68         walk_expr(self, ex)
69     }
70     fn visit_ty(&mut self, t: &Ty) {
71         self.count += 1;
72         walk_ty(self, t)
73     }
74     fn visit_generics(&mut self, g: &Generics) {
75         self.count += 1;
76         walk_generics(self, g)
77     }
78     fn visit_fn(&mut self, fk: FnKind, fd: &FnDecl, b: &Block, s: Span, _: NodeId) {
79         self.count += 1;
80         walk_fn(self, fk, fd, b, s)
81     }
82     fn visit_trait_item(&mut self, ti: &TraitItem) {
83         self.count += 1;
84         walk_trait_item(self, ti)
85     }
86     fn visit_impl_item(&mut self, ii: &ImplItem) {
87         self.count += 1;
88         walk_impl_item(self, ii)
89     }
90     fn visit_trait_ref(&mut self, t: &TraitRef) {
91         self.count += 1;
92         walk_trait_ref(self, t)
93     }
94     fn visit_ty_param_bound(&mut self, bounds: &TyParamBound) {
95         self.count += 1;
96         walk_ty_param_bound(self, bounds)
97     }
98     fn visit_poly_trait_ref(&mut self, t: &PolyTraitRef, m: &TraitBoundModifier) {
99         self.count += 1;
100         walk_poly_trait_ref(self, t, m)
101     }
102     fn visit_variant_data(&mut self, s: &VariantData, _: Ident,
103                           _: &Generics, _: NodeId, _: Span) {
104         self.count += 1;
105         walk_struct_def(self, s)
106     }
107     fn visit_struct_field(&mut self, s: &StructField) {
108         self.count += 1;
109         walk_struct_field(self, s)
110     }
111     fn visit_enum_def(&mut self, enum_definition: &EnumDef,
112                       generics: &Generics, item_id: NodeId, _: Span) {
113         self.count += 1;
114         walk_enum_def(self, enum_definition, generics, item_id)
115     }
116     fn visit_variant(&mut self, v: &Variant, g: &Generics, item_id: NodeId) {
117         self.count += 1;
118         walk_variant(self, v, g, item_id)
119     }
120     fn visit_lifetime(&mut self, lifetime: &Lifetime) {
121         self.count += 1;
122         walk_lifetime(self, lifetime)
123     }
124     fn visit_lifetime_def(&mut self, lifetime: &LifetimeDef) {
125         self.count += 1;
126         walk_lifetime_def(self, lifetime)
127     }
128     fn visit_mac(&mut self, _mac: &Mac) {
129         self.count += 1;
130         walk_mac(self, _mac)
131     }
132     fn visit_path(&mut self, path: &Path, _id: NodeId) {
133         self.count += 1;
134         walk_path(self, path)
135     }
136     fn visit_path_list_item(&mut self, prefix: &Path, item: &PathListItem) {
137         self.count += 1;
138         walk_path_list_item(self, prefix, item)
139     }
140     fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &PathParameters) {
141         self.count += 1;
142         walk_path_parameters(self, path_span, path_parameters)
143     }
144     fn visit_assoc_type_binding(&mut self, type_binding: &TypeBinding) {
145         self.count += 1;
146         walk_assoc_type_binding(self, type_binding)
147     }
148     fn visit_attribute(&mut self, _attr: &Attribute) {
149         self.count += 1;
150     }
151     fn visit_macro_def(&mut self, macro_def: &MacroDef) {
152         self.count += 1;
153         walk_macro_def(self, macro_def)
154     }
155
156 }