]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/util/node_count.rs
Rollup merge of #56363 - Lucretiel:patch-3, r=shepmaster
[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<'ast> Visitor<'ast> for NodeCounter {
30     fn visit_ident(&mut self, ident: Ident) {
31         self.count += 1;
32         walk_ident(self, ident);
33     }
34     fn visit_mod(&mut self, m: &Mod, _s: Span, _a: &[Attribute], _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_generic_param(&mut self, param: &GenericParam) {
75         self.count += 1;
76         walk_generic_param(self, param)
77     }
78     fn visit_generics(&mut self, g: &Generics) {
79         self.count += 1;
80         walk_generics(self, g)
81     }
82     fn visit_fn(&mut self, fk: FnKind, fd: &FnDecl, s: Span, _: NodeId) {
83         self.count += 1;
84         walk_fn(self, fk, fd, s)
85     }
86     fn visit_trait_item(&mut self, ti: &TraitItem) {
87         self.count += 1;
88         walk_trait_item(self, ti)
89     }
90     fn visit_impl_item(&mut self, ii: &ImplItem) {
91         self.count += 1;
92         walk_impl_item(self, ii)
93     }
94     fn visit_trait_ref(&mut self, t: &TraitRef) {
95         self.count += 1;
96         walk_trait_ref(self, t)
97     }
98     fn visit_param_bound(&mut self, bounds: &GenericBound) {
99         self.count += 1;
100         walk_param_bound(self, bounds)
101     }
102     fn visit_poly_trait_ref(&mut self, t: &PolyTraitRef, m: &TraitBoundModifier) {
103         self.count += 1;
104         walk_poly_trait_ref(self, t, m)
105     }
106     fn visit_variant_data(&mut self, s: &VariantData, _: Ident,
107                           _: &Generics, _: NodeId, _: Span) {
108         self.count += 1;
109         walk_struct_def(self, s)
110     }
111     fn visit_struct_field(&mut self, s: &StructField) {
112         self.count += 1;
113         walk_struct_field(self, s)
114     }
115     fn visit_enum_def(&mut self, enum_definition: &EnumDef,
116                       generics: &Generics, item_id: NodeId, _: Span) {
117         self.count += 1;
118         walk_enum_def(self, enum_definition, generics, item_id)
119     }
120     fn visit_variant(&mut self, v: &Variant, g: &Generics, item_id: NodeId) {
121         self.count += 1;
122         walk_variant(self, v, g, item_id)
123     }
124     fn visit_lifetime(&mut self, lifetime: &Lifetime) {
125         self.count += 1;
126         walk_lifetime(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_use_tree(&mut self, use_tree: &UseTree, id: NodeId, _nested: bool) {
137         self.count += 1;
138         walk_use_tree(self, use_tree, id)
139     }
140     fn visit_generic_args(&mut self, path_span: Span, generic_args: &GenericArgs) {
141         self.count += 1;
142         walk_generic_args(self, path_span, generic_args)
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 }