2020-05-14 20:39:36 +00:00
|
|
|
// Simply gives a rough count of the number of nodes in an AST.
|
2015-11-11 05:26:14 +00:00
|
|
|
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::visit::*;
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast::*;
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::Ident;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2015-11-11 05:26:14 +00:00
|
|
|
|
|
|
|
pub struct NodeCounter {
|
|
|
|
pub count: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeCounter {
|
|
|
|
pub fn new() -> NodeCounter {
|
2019-12-22 22:42:04 +00:00
|
|
|
NodeCounter { count: 0 }
|
2015-11-11 05:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
impl<'ast> Visitor<'ast> for NodeCounter {
|
2018-03-19 00:54:56 +00:00
|
|
|
fn visit_ident(&mut self, ident: Ident) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
2018-03-19 00:54:56 +00:00
|
|
|
walk_ident(self, ident);
|
2015-11-11 05:26:14 +00:00
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_foreign_item(&mut self, i: &ForeignItem) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_foreign_item(self, i)
|
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_item(&mut self, i: &Item) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_item(self, i)
|
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_local(&mut self, l: &Local) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_local(self, l)
|
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_block(&mut self, b: &Block) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_block(self, b)
|
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_stmt(&mut self, s: &Stmt) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_stmt(self, s)
|
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_arm(&mut self, a: &Arm) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_arm(self, a)
|
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_pat(&mut self, p: &Pat) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_pat(self, p)
|
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_expr(&mut self, ex: &Expr) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_expr(self, ex)
|
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_ty(&mut self, t: &Ty) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_ty(self, t)
|
|
|
|
}
|
2018-05-27 19:07:09 +00:00
|
|
|
fn visit_generic_param(&mut self, param: &GenericParam) {
|
2017-10-16 19:07:26 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_generic_param(self, param)
|
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_generics(&mut self, g: &Generics) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_generics(self, g)
|
|
|
|
}
|
2021-01-29 07:31:08 +00:00
|
|
|
fn visit_fn(&mut self, fk: visit::FnKind<'_>, s: Span, _: NodeId) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
2020-01-29 23:18:54 +00:00
|
|
|
walk_fn(self, fk, s)
|
2015-11-11 05:26:14 +00:00
|
|
|
}
|
2020-01-29 23:18:54 +00:00
|
|
|
fn visit_assoc_item(&mut self, ti: &AssocItem, ctxt: AssocCtxt) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
2020-01-29 23:18:54 +00:00
|
|
|
walk_assoc_item(self, ti, ctxt);
|
2015-11-11 05:26:14 +00:00
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_trait_ref(&mut self, t: &TraitRef) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_trait_ref(self, t)
|
|
|
|
}
|
2018-06-14 11:08:58 +00:00
|
|
|
fn visit_param_bound(&mut self, bounds: &GenericBound) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
2018-05-28 14:23:16 +00:00
|
|
|
walk_param_bound(self, bounds)
|
2015-11-11 05:26:14 +00:00
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_poly_trait_ref(&mut self, t: &PolyTraitRef, m: &TraitBoundModifier) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_poly_trait_ref(self, t, m)
|
|
|
|
}
|
2019-08-24 16:54:40 +00:00
|
|
|
fn visit_variant_data(&mut self, s: &VariantData) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_struct_def(self, s)
|
|
|
|
}
|
2021-03-15 21:36:07 +00:00
|
|
|
fn visit_field_def(&mut self, s: &FieldDef) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
2021-03-15 21:36:07 +00:00
|
|
|
walk_field_def(self, s)
|
2015-11-11 05:26:14 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
fn visit_enum_def(
|
|
|
|
&mut self,
|
|
|
|
enum_definition: &EnumDef,
|
|
|
|
generics: &Generics,
|
|
|
|
item_id: NodeId,
|
|
|
|
_: Span,
|
|
|
|
) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_enum_def(self, enum_definition, generics, item_id)
|
|
|
|
}
|
2019-08-24 16:54:40 +00:00
|
|
|
fn visit_variant(&mut self, v: &Variant) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
2019-08-24 16:54:40 +00:00
|
|
|
walk_variant(self, v)
|
2015-11-11 05:26:14 +00:00
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_lifetime(&mut self, lifetime: &Lifetime) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_lifetime(self, lifetime)
|
|
|
|
}
|
2020-11-03 17:34:57 +00:00
|
|
|
fn visit_mac_call(&mut self, mac: &MacCall) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
2020-11-03 17:26:17 +00:00
|
|
|
walk_mac(self, mac)
|
2015-11-11 05:26:14 +00:00
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_path(&mut self, path: &Path, _id: NodeId) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
walk_path(self, path)
|
|
|
|
}
|
2017-09-26 21:04:00 +00:00
|
|
|
fn visit_use_tree(&mut self, use_tree: &UseTree, id: NodeId, _nested: bool) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
2017-09-26 21:04:00 +00:00
|
|
|
walk_use_tree(self, use_tree, id)
|
2015-11-11 05:26:14 +00:00
|
|
|
}
|
2018-02-13 11:32:37 +00:00
|
|
|
fn visit_generic_args(&mut self, path_span: Span, generic_args: &GenericArgs) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
2018-02-13 11:32:37 +00:00
|
|
|
walk_generic_args(self, path_span, generic_args)
|
2015-11-11 05:26:14 +00:00
|
|
|
}
|
2019-02-28 22:43:53 +00:00
|
|
|
fn visit_assoc_ty_constraint(&mut self, constraint: &AssocTyConstraint) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
2019-02-28 22:43:53 +00:00
|
|
|
walk_assoc_ty_constraint(self, constraint)
|
2015-11-11 05:26:14 +00:00
|
|
|
}
|
2016-06-12 07:51:31 +00:00
|
|
|
fn visit_attribute(&mut self, _attr: &Attribute) {
|
2015-11-11 05:26:14 +00:00
|
|
|
self.count += 1;
|
|
|
|
}
|
|
|
|
}
|