1 //! AST walker. Each overridden visit method has full control over what
2 //! happens with its node, it can do its own traversal of the node's children,
3 //! call `visit::walk_*` to apply the default traversal algorithm, or prevent
4 //! deeper traversal by doing nothing.
6 //! Note: it is an important invariant that the default visitor walks the body
7 //! of a function in "execution order" (more concretely, reverse post-order
8 //! with respect to the CFG implied by the AST), meaning that if AST node A may
9 //! execute before AST node B, then A is visited first. The borrow checker in
10 //! particular relies on this property.
12 //! Note: walking an AST before macro expansion is probably a bad idea. For
13 //! instance, a walker looking for item names in a module will miss all of
14 //! those that are created by the expansion of a macro.
17 use crate::token::Token;
18 use crate::tokenstream::{TokenStream, TokenTree};
22 #[derive(Copy, Clone, PartialEq)]
28 #[derive(Copy, Clone, PartialEq)]
35 #[derive(Copy, Clone)]
37 /// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
38 Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, Option<&'a Block>),
40 /// E.g., `|x, y| body`.
41 Closure(&'a FnDecl, &'a Expr),
45 pub fn header(&self) -> Option<&'a FnHeader> {
47 FnKind::Fn(_, _, sig, _, _) => Some(&sig.header),
48 FnKind::Closure(_, _) => None,
52 pub fn decl(&self) -> &'a FnDecl {
54 FnKind::Fn(_, _, sig, _, _) => &sig.decl,
55 FnKind::Closure(decl, _) => decl,
59 pub fn ctxt(&self) -> Option<FnCtxt> {
61 FnKind::Fn(ctxt, ..) => Some(*ctxt),
62 FnKind::Closure(..) => None,
67 /// Each method of the `Visitor` trait is a hook to be potentially
68 /// overridden. Each method's default implementation recursively visits
69 /// the substructure of the input via the corresponding `walk` method;
70 /// e.g., the `visit_mod` method by default calls `visit::walk_mod`.
72 /// If you want to ensure that your code handles every variant
73 /// explicitly, you need to override each method. (And you also need
74 /// to monitor future changes to `Visitor` in case a new method with a
75 /// new default implementation gets introduced.)
76 pub trait Visitor<'ast>: Sized {
77 fn visit_name(&mut self, _span: Span, _name: Name) {
80 fn visit_ident(&mut self, ident: Ident) {
81 walk_ident(self, ident);
83 fn visit_mod(&mut self, m: &'ast Mod, _s: Span, _attrs: &[Attribute], _n: NodeId) {
86 fn visit_foreign_item(&mut self, i: &'ast ForeignItem) {
87 walk_foreign_item(self, i)
89 fn visit_global_asm(&mut self, ga: &'ast GlobalAsm) {
90 walk_global_asm(self, ga)
92 fn visit_item(&mut self, i: &'ast Item) {
95 fn visit_local(&mut self, l: &'ast Local) {
98 fn visit_block(&mut self, b: &'ast Block) {
101 fn visit_stmt(&mut self, s: &'ast Stmt) {
104 fn visit_param(&mut self, param: &'ast Param) {
105 walk_param(self, param)
107 fn visit_arm(&mut self, a: &'ast Arm) {
110 fn visit_pat(&mut self, p: &'ast Pat) {
113 fn visit_anon_const(&mut self, c: &'ast AnonConst) {
114 walk_anon_const(self, c)
116 fn visit_expr(&mut self, ex: &'ast Expr) {
119 fn visit_expr_post(&mut self, _ex: &'ast Expr) {}
120 fn visit_ty(&mut self, t: &'ast Ty) {
123 fn visit_generic_param(&mut self, param: &'ast GenericParam) {
124 walk_generic_param(self, param)
126 fn visit_generics(&mut self, g: &'ast Generics) {
127 walk_generics(self, g)
129 fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
130 walk_where_predicate(self, p)
132 fn visit_fn(&mut self, fk: FnKind<'ast>, s: Span, _: NodeId) {
135 fn visit_assoc_item(&mut self, i: &'ast AssocItem, ctxt: AssocCtxt) {
136 walk_assoc_item(self, i, ctxt)
138 fn visit_trait_ref(&mut self, t: &'ast TraitRef) {
139 walk_trait_ref(self, t)
141 fn visit_param_bound(&mut self, bounds: &'ast GenericBound) {
142 walk_param_bound(self, bounds)
144 fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
145 walk_poly_trait_ref(self, t, m)
147 fn visit_variant_data(&mut self, s: &'ast VariantData) {
148 walk_struct_def(self, s)
150 fn visit_struct_field(&mut self, s: &'ast StructField) {
151 walk_struct_field(self, s)
155 enum_definition: &'ast EnumDef,
156 generics: &'ast Generics,
160 walk_enum_def(self, enum_definition, generics, item_id)
162 fn visit_variant(&mut self, v: &'ast Variant) {
163 walk_variant(self, v)
165 fn visit_label(&mut self, label: &'ast Label) {
166 walk_label(self, label)
168 fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
169 walk_lifetime(self, lifetime)
171 fn visit_mac(&mut self, _mac: &'ast Mac) {
172 panic!("visit_mac disabled by default");
173 // N.B., see note about macros above.
174 // if you really want a visitor that
175 // works on macros, use this
176 // definition in your trait impl:
177 // visit::walk_mac(self, _mac)
179 fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
182 fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
183 walk_path(self, path)
185 fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
186 walk_use_tree(self, use_tree, id)
188 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
189 walk_path_segment(self, path_span, path_segment)
191 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'ast GenericArgs) {
192 walk_generic_args(self, path_span, generic_args)
194 fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
196 GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
197 GenericArg::Type(ty) => self.visit_ty(ty),
198 GenericArg::Const(ct) => self.visit_anon_const(ct),
201 fn visit_assoc_ty_constraint(&mut self, constraint: &'ast AssocTyConstraint) {
202 walk_assoc_ty_constraint(self, constraint)
204 fn visit_attribute(&mut self, attr: &'ast Attribute) {
205 walk_attribute(self, attr)
207 fn visit_tt(&mut self, tt: TokenTree) {
210 fn visit_tts(&mut self, tts: TokenStream) {
213 fn visit_token(&mut self, _t: Token) {}
214 // FIXME: add `visit_interpolated` and `walk_interpolated`
215 fn visit_vis(&mut self, vis: &'ast Visibility) {
218 fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FnRetTy) {
219 walk_fn_ret_ty(self, ret_ty)
221 fn visit_fn_header(&mut self, _header: &'ast FnHeader) {
224 fn visit_field(&mut self, f: &'ast Field) {
227 fn visit_field_pattern(&mut self, fp: &'ast FieldPat) {
228 walk_field_pattern(self, fp)
233 macro_rules! walk_list {
234 ($visitor: expr, $method: ident, $list: expr) => {
236 $visitor.$method(elem)
239 ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
241 $visitor.$method(elem, $($extra_args,)*)
246 pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, ident: Ident) {
247 visitor.visit_name(ident.span, ident.name);
250 pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
251 visitor.visit_mod(&krate.module, krate.span, &krate.attrs, CRATE_NODE_ID);
252 walk_list!(visitor, visit_attribute, &krate.attrs);
255 pub fn walk_mod<'a, V: Visitor<'a>>(visitor: &mut V, module: &'a Mod) {
256 walk_list!(visitor, visit_item, &module.items);
259 pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
260 for attr in local.attrs.iter() {
261 visitor.visit_attribute(attr);
263 visitor.visit_pat(&local.pat);
264 walk_list!(visitor, visit_ty, &local.ty);
265 walk_list!(visitor, visit_expr, &local.init);
268 pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, label: &'a Label) {
269 visitor.visit_ident(label.ident);
272 pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
273 visitor.visit_ident(lifetime.ident);
276 pub fn walk_poly_trait_ref<'a, V>(
278 trait_ref: &'a PolyTraitRef,
279 _: &TraitBoundModifier,
283 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
284 visitor.visit_trait_ref(&trait_ref.trait_ref);
287 pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) {
288 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
291 pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
292 visitor.visit_vis(&item.vis);
293 visitor.visit_ident(item.ident);
295 ItemKind::ExternCrate(orig_name) => {
296 if let Some(orig_name) = orig_name {
297 visitor.visit_name(item.span, orig_name);
300 ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
301 ItemKind::Static(ref typ, _, ref expr) | ItemKind::Const(ref typ, ref expr) => {
302 visitor.visit_ty(typ);
303 walk_list!(visitor, visit_expr, expr);
305 ItemKind::Fn(ref sig, ref generics, ref body) => {
306 visitor.visit_generics(generics);
307 let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref());
308 visitor.visit_fn(kind, item.span, item.id)
310 ItemKind::Mod(ref module) => visitor.visit_mod(module, item.span, &item.attrs, item.id),
311 ItemKind::ForeignMod(ref foreign_module) => {
312 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
314 ItemKind::GlobalAsm(ref ga) => visitor.visit_global_asm(ga),
315 ItemKind::TyAlias(ref generics, ref bounds, ref ty) => {
316 visitor.visit_generics(generics);
317 walk_list!(visitor, visit_param_bound, bounds);
318 walk_list!(visitor, visit_ty, ty);
320 ItemKind::Enum(ref enum_definition, ref generics) => {
321 visitor.visit_generics(generics);
322 visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
334 visitor.visit_generics(generics);
335 walk_list!(visitor, visit_trait_ref, of_trait);
336 visitor.visit_ty(self_ty);
337 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Impl);
339 ItemKind::Struct(ref struct_definition, ref generics)
340 | ItemKind::Union(ref struct_definition, ref generics) => {
341 visitor.visit_generics(generics);
342 visitor.visit_variant_data(struct_definition);
344 ItemKind::Trait(.., ref generics, ref bounds, ref items) => {
345 visitor.visit_generics(generics);
346 walk_list!(visitor, visit_param_bound, bounds);
347 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
349 ItemKind::TraitAlias(ref generics, ref bounds) => {
350 visitor.visit_generics(generics);
351 walk_list!(visitor, visit_param_bound, bounds);
353 ItemKind::Mac(ref mac) => visitor.visit_mac(mac),
354 ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
356 walk_list!(visitor, visit_attribute, &item.attrs);
359 pub fn walk_enum_def<'a, V: Visitor<'a>>(
361 enum_definition: &'a EnumDef,
365 walk_list!(visitor, visit_variant, &enum_definition.variants);
368 pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant)
372 visitor.visit_ident(variant.ident);
373 visitor.visit_vis(&variant.vis);
374 visitor.visit_variant_data(&variant.data);
375 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
376 walk_list!(visitor, visit_attribute, &variant.attrs);
379 pub fn walk_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a Field) {
380 visitor.visit_expr(&f.expr);
381 visitor.visit_ident(f.ident);
382 walk_list!(visitor, visit_attribute, f.attrs.iter());
385 pub fn walk_field_pattern<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a FieldPat) {
386 visitor.visit_ident(fp.ident);
387 visitor.visit_pat(&fp.pat);
388 walk_list!(visitor, visit_attribute, fp.attrs.iter());
391 pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
393 TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => visitor.visit_ty(ty),
394 TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
395 TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
396 walk_list!(visitor, visit_lifetime, opt_lifetime);
397 visitor.visit_ty(&mutable_type.ty)
399 TyKind::Tup(ref tuple_element_types) => {
400 walk_list!(visitor, visit_ty, tuple_element_types);
402 TyKind::BareFn(ref function_declaration) => {
403 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
404 walk_fn_decl(visitor, &function_declaration.decl);
406 TyKind::Path(ref maybe_qself, ref path) => {
407 if let Some(ref qself) = *maybe_qself {
408 visitor.visit_ty(&qself.ty);
410 visitor.visit_path(path, typ.id);
412 TyKind::Array(ref ty, ref length) => {
413 visitor.visit_ty(ty);
414 visitor.visit_anon_const(length)
416 TyKind::TraitObject(ref bounds, ..) | TyKind::ImplTrait(_, ref bounds) => {
417 walk_list!(visitor, visit_param_bound, bounds);
419 TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
420 TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
421 TyKind::Mac(ref mac) => visitor.visit_mac(mac),
422 TyKind::Never | TyKind::CVarArgs => {}
426 pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
427 for segment in &path.segments {
428 visitor.visit_path_segment(path.span, segment);
432 pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree, id: NodeId) {
433 visitor.visit_path(&use_tree.prefix, id);
434 match use_tree.kind {
435 UseTreeKind::Simple(rename, ..) => {
436 // The extra IDs are handled during HIR lowering.
437 if let Some(rename) = rename {
438 visitor.visit_ident(rename);
441 UseTreeKind::Glob => {}
442 UseTreeKind::Nested(ref use_trees) => {
443 for &(ref nested_tree, nested_id) in use_trees {
444 visitor.visit_use_tree(nested_tree, nested_id, true);
450 pub fn walk_path_segment<'a, V: Visitor<'a>>(
453 segment: &'a PathSegment,
455 visitor.visit_ident(segment.ident);
456 if let Some(ref args) = segment.args {
457 visitor.visit_generic_args(path_span, args);
461 pub fn walk_generic_args<'a, V>(visitor: &mut V, _path_span: Span, generic_args: &'a GenericArgs)
465 match *generic_args {
466 GenericArgs::AngleBracketed(ref data) => {
467 walk_list!(visitor, visit_generic_arg, &data.args);
468 walk_list!(visitor, visit_assoc_ty_constraint, &data.constraints);
470 GenericArgs::Parenthesized(ref data) => {
471 walk_list!(visitor, visit_ty, &data.inputs);
472 walk_fn_ret_ty(visitor, &data.output);
477 pub fn walk_assoc_ty_constraint<'a, V: Visitor<'a>>(
479 constraint: &'a AssocTyConstraint,
481 visitor.visit_ident(constraint.ident);
482 match constraint.kind {
483 AssocTyConstraintKind::Equality { ref ty } => {
484 visitor.visit_ty(ty);
486 AssocTyConstraintKind::Bound { ref bounds } => {
487 walk_list!(visitor, visit_param_bound, bounds);
492 pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
494 PatKind::TupleStruct(ref path, ref elems) => {
495 visitor.visit_path(path, pattern.id);
496 walk_list!(visitor, visit_pat, elems);
498 PatKind::Path(ref opt_qself, ref path) => {
499 if let Some(ref qself) = *opt_qself {
500 visitor.visit_ty(&qself.ty);
502 visitor.visit_path(path, pattern.id)
504 PatKind::Struct(ref path, ref fields, _) => {
505 visitor.visit_path(path, pattern.id);
506 walk_list!(visitor, visit_field_pattern, fields);
508 PatKind::Box(ref subpattern)
509 | PatKind::Ref(ref subpattern, _)
510 | PatKind::Paren(ref subpattern) => visitor.visit_pat(subpattern),
511 PatKind::Ident(_, ident, ref optional_subpattern) => {
512 visitor.visit_ident(ident);
513 walk_list!(visitor, visit_pat, optional_subpattern);
515 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
516 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
517 walk_list!(visitor, visit_expr, lower_bound);
518 walk_list!(visitor, visit_expr, upper_bound);
520 PatKind::Wild | PatKind::Rest => {}
521 PatKind::Tuple(ref elems) | PatKind::Slice(ref elems) | PatKind::Or(ref elems) => {
522 walk_list!(visitor, visit_pat, elems);
524 PatKind::Mac(ref mac) => visitor.visit_mac(mac),
528 pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignItem) {
529 let ForeignItem { id, span, ident, vis, attrs, kind, tokens: _ } = item;
530 walk_nested_item(visitor, *id, *span, *ident, vis, attrs, kind, FnCtxt::Foreign);
533 pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) {
537 pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
539 GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
540 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
544 pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
545 visitor.visit_ident(param.ident);
546 walk_list!(visitor, visit_attribute, param.attrs.iter());
547 walk_list!(visitor, visit_param_bound, ¶m.bounds);
549 GenericParamKind::Lifetime => (),
550 GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
551 GenericParamKind::Const { ref ty, .. } => visitor.visit_ty(ty),
555 pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
556 walk_list!(visitor, visit_generic_param, &generics.params);
557 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
560 pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
562 WherePredicate::BoundPredicate(WhereBoundPredicate {
565 ref bound_generic_params,
568 visitor.visit_ty(bounded_ty);
569 walk_list!(visitor, visit_param_bound, bounds);
570 walk_list!(visitor, visit_generic_param, bound_generic_params);
572 WherePredicate::RegionPredicate(WhereRegionPredicate {
573 ref lifetime, ref bounds, ..
575 visitor.visit_lifetime(lifetime);
576 walk_list!(visitor, visit_param_bound, bounds);
578 WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }) => {
579 visitor.visit_ty(lhs_ty);
580 visitor.visit_ty(rhs_ty);
585 pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy) {
586 if let FnRetTy::Ty(ref output_ty) = *ret_ty {
587 visitor.visit_ty(output_ty)
591 pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
592 for param in &function_declaration.inputs {
593 visitor.visit_param(param);
595 visitor.visit_fn_ret_ty(&function_declaration.output);
598 pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Span) {
600 FnKind::Fn(_, _, sig, _, body) => {
601 visitor.visit_fn_header(&sig.header);
602 walk_fn_decl(visitor, &sig.decl);
603 walk_list!(visitor, visit_block, body);
605 FnKind::Closure(decl, body) => {
606 walk_fn_decl(visitor, decl);
607 visitor.visit_expr(body);
612 pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, ctxt: AssocCtxt) {
613 let AssocItem { id, span, ident, vis, attrs, kind, tokens: _, defaultness: _ } = item;
614 walk_nested_item(visitor, *id, *span, *ident, vis, attrs, kind, FnCtxt::Assoc(ctxt));
617 fn walk_nested_item<'a, V: Visitor<'a>>(
623 attrs: &'a [Attribute],
624 kind: &'a AssocItemKind,
627 visitor.visit_vis(vis);
628 visitor.visit_ident(ident);
629 walk_list!(visitor, visit_attribute, attrs);
631 AssocItemKind::Const(ty, expr) | AssocItemKind::Static(ty, _, expr) => {
632 visitor.visit_ty(ty);
633 walk_list!(visitor, visit_expr, expr);
635 AssocItemKind::Fn(sig, generics, body) => {
636 visitor.visit_generics(generics);
637 let kind = FnKind::Fn(ctxt, ident, sig, vis, body.as_deref());
638 visitor.visit_fn(kind, span, id);
640 AssocItemKind::TyAlias(generics, bounds, ty) => {
641 visitor.visit_generics(generics);
642 walk_list!(visitor, visit_param_bound, bounds);
643 walk_list!(visitor, visit_ty, ty);
645 AssocItemKind::Macro(mac) => {
646 visitor.visit_mac(mac);
651 pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
652 walk_list!(visitor, visit_struct_field, struct_definition.fields());
655 pub fn walk_struct_field<'a, V: Visitor<'a>>(visitor: &mut V, struct_field: &'a StructField) {
656 visitor.visit_vis(&struct_field.vis);
657 if let Some(ident) = struct_field.ident {
658 visitor.visit_ident(ident);
660 visitor.visit_ty(&struct_field.ty);
661 walk_list!(visitor, visit_attribute, &struct_field.attrs);
664 pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
665 walk_list!(visitor, visit_stmt, &block.stmts);
668 pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
669 match statement.kind {
670 StmtKind::Local(ref local) => visitor.visit_local(local),
671 StmtKind::Item(ref item) => visitor.visit_item(item),
672 StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
673 visitor.visit_expr(expression)
675 StmtKind::Mac(ref mac) => {
676 let (ref mac, _, ref attrs) = **mac;
677 visitor.visit_mac(mac);
678 for attr in attrs.iter() {
679 visitor.visit_attribute(attr);
685 pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a Mac) {
686 visitor.visit_path(&mac.path, DUMMY_NODE_ID);
689 pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
690 visitor.visit_expr(&constant.value);
693 pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
694 walk_list!(visitor, visit_attribute, expression.attrs.iter());
696 match expression.kind {
697 ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
698 ExprKind::Array(ref subexpressions) => {
699 walk_list!(visitor, visit_expr, subexpressions);
701 ExprKind::Repeat(ref element, ref count) => {
702 visitor.visit_expr(element);
703 visitor.visit_anon_const(count)
705 ExprKind::Struct(ref path, ref fields, ref optional_base) => {
706 visitor.visit_path(path, expression.id);
707 walk_list!(visitor, visit_field, fields);
708 walk_list!(visitor, visit_expr, optional_base);
710 ExprKind::Tup(ref subexpressions) => {
711 walk_list!(visitor, visit_expr, subexpressions);
713 ExprKind::Call(ref callee_expression, ref arguments) => {
714 visitor.visit_expr(callee_expression);
715 walk_list!(visitor, visit_expr, arguments);
717 ExprKind::MethodCall(ref segment, ref arguments) => {
718 visitor.visit_path_segment(expression.span, segment);
719 walk_list!(visitor, visit_expr, arguments);
721 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
722 visitor.visit_expr(left_expression);
723 visitor.visit_expr(right_expression)
725 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
726 visitor.visit_expr(subexpression)
728 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
729 visitor.visit_expr(subexpression);
730 visitor.visit_ty(typ)
732 ExprKind::Let(ref pat, ref scrutinee) => {
733 visitor.visit_pat(pat);
734 visitor.visit_expr(scrutinee);
736 ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
737 visitor.visit_expr(head_expression);
738 visitor.visit_block(if_block);
739 walk_list!(visitor, visit_expr, optional_else);
741 ExprKind::While(ref subexpression, ref block, ref opt_label) => {
742 walk_list!(visitor, visit_label, opt_label);
743 visitor.visit_expr(subexpression);
744 visitor.visit_block(block);
746 ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
747 walk_list!(visitor, visit_label, opt_label);
748 visitor.visit_pat(pattern);
749 visitor.visit_expr(subexpression);
750 visitor.visit_block(block);
752 ExprKind::Loop(ref block, ref opt_label) => {
753 walk_list!(visitor, visit_label, opt_label);
754 visitor.visit_block(block);
756 ExprKind::Match(ref subexpression, ref arms) => {
757 visitor.visit_expr(subexpression);
758 walk_list!(visitor, visit_arm, arms);
760 ExprKind::Closure(_, _, _, ref decl, ref body, _decl_span) => {
761 visitor.visit_fn(FnKind::Closure(decl, body), expression.span, expression.id)
763 ExprKind::Block(ref block, ref opt_label) => {
764 walk_list!(visitor, visit_label, opt_label);
765 visitor.visit_block(block);
767 ExprKind::Async(_, _, ref body) => {
768 visitor.visit_block(body);
770 ExprKind::Await(ref expr) => visitor.visit_expr(expr),
771 ExprKind::Assign(ref lhs, ref rhs, _) => {
772 visitor.visit_expr(lhs);
773 visitor.visit_expr(rhs);
775 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
776 visitor.visit_expr(left_expression);
777 visitor.visit_expr(right_expression);
779 ExprKind::Field(ref subexpression, ident) => {
780 visitor.visit_expr(subexpression);
781 visitor.visit_ident(ident);
783 ExprKind::Index(ref main_expression, ref index_expression) => {
784 visitor.visit_expr(main_expression);
785 visitor.visit_expr(index_expression)
787 ExprKind::Range(ref start, ref end, _) => {
788 walk_list!(visitor, visit_expr, start);
789 walk_list!(visitor, visit_expr, end);
791 ExprKind::Path(ref maybe_qself, ref path) => {
792 if let Some(ref qself) = *maybe_qself {
793 visitor.visit_ty(&qself.ty);
795 visitor.visit_path(path, expression.id)
797 ExprKind::Break(ref opt_label, ref opt_expr) => {
798 walk_list!(visitor, visit_label, opt_label);
799 walk_list!(visitor, visit_expr, opt_expr);
801 ExprKind::Continue(ref opt_label) => {
802 walk_list!(visitor, visit_label, opt_label);
804 ExprKind::Ret(ref optional_expression) => {
805 walk_list!(visitor, visit_expr, optional_expression);
807 ExprKind::Mac(ref mac) => visitor.visit_mac(mac),
808 ExprKind::Paren(ref subexpression) => visitor.visit_expr(subexpression),
809 ExprKind::InlineAsm(ref ia) => {
810 for &(_, ref input) in &ia.inputs {
811 visitor.visit_expr(input)
813 for output in &ia.outputs {
814 visitor.visit_expr(&output.expr)
817 ExprKind::Yield(ref optional_expression) => {
818 walk_list!(visitor, visit_expr, optional_expression);
820 ExprKind::Try(ref subexpression) => visitor.visit_expr(subexpression),
821 ExprKind::TryBlock(ref body) => visitor.visit_block(body),
822 ExprKind::Lit(_) | ExprKind::Err => {}
825 visitor.visit_expr_post(expression)
828 pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {
829 walk_list!(visitor, visit_attribute, param.attrs.iter());
830 visitor.visit_pat(¶m.pat);
831 visitor.visit_ty(¶m.ty);
834 pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
835 visitor.visit_pat(&arm.pat);
836 walk_list!(visitor, visit_expr, &arm.guard);
837 visitor.visit_expr(&arm.body);
838 walk_list!(visitor, visit_attribute, &arm.attrs);
841 pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
842 if let VisibilityKind::Restricted { ref path, id } = vis.node {
843 visitor.visit_path(path, id);
847 pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
849 AttrKind::Normal(ref item) => walk_mac_args(visitor, &item.args),
850 AttrKind::DocComment(_) => {}
854 pub fn walk_mac_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a MacArgs) {
857 MacArgs::Delimited(_dspan, _delim, tokens) => visitor.visit_tts(tokens.clone()),
858 MacArgs::Eq(_eq_span, tokens) => visitor.visit_tts(tokens.clone()),
862 pub fn walk_tt<'a, V: Visitor<'a>>(visitor: &mut V, tt: TokenTree) {
864 TokenTree::Token(token) => visitor.visit_token(token),
865 TokenTree::Delimited(_, _, tts) => visitor.visit_tts(tts),
869 pub fn walk_tts<'a, V: Visitor<'a>>(visitor: &mut V, tts: TokenStream) {
870 for tt in tts.trees() {
871 visitor.visit_tt(tt);