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 typ, ref generics) => {
316 visitor.visit_ty(typ);
317 visitor.visit_generics(generics)
319 ItemKind::Enum(ref enum_definition, ref generics) => {
320 visitor.visit_generics(generics);
321 visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
333 visitor.visit_generics(generics);
334 walk_list!(visitor, visit_trait_ref, of_trait);
335 visitor.visit_ty(self_ty);
336 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Impl);
338 ItemKind::Struct(ref struct_definition, ref generics)
339 | ItemKind::Union(ref struct_definition, ref generics) => {
340 visitor.visit_generics(generics);
341 visitor.visit_variant_data(struct_definition);
343 ItemKind::Trait(.., ref generics, ref bounds, ref items) => {
344 visitor.visit_generics(generics);
345 walk_list!(visitor, visit_param_bound, bounds);
346 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
348 ItemKind::TraitAlias(ref generics, ref bounds) => {
349 visitor.visit_generics(generics);
350 walk_list!(visitor, visit_param_bound, bounds);
352 ItemKind::Mac(ref mac) => visitor.visit_mac(mac),
353 ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
355 walk_list!(visitor, visit_attribute, &item.attrs);
358 pub fn walk_enum_def<'a, V: Visitor<'a>>(
360 enum_definition: &'a EnumDef,
364 walk_list!(visitor, visit_variant, &enum_definition.variants);
367 pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant)
371 visitor.visit_ident(variant.ident);
372 visitor.visit_vis(&variant.vis);
373 visitor.visit_variant_data(&variant.data);
374 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
375 walk_list!(visitor, visit_attribute, &variant.attrs);
378 pub fn walk_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a Field) {
379 visitor.visit_expr(&f.expr);
380 visitor.visit_ident(f.ident);
381 walk_list!(visitor, visit_attribute, f.attrs.iter());
384 pub fn walk_field_pattern<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a FieldPat) {
385 visitor.visit_ident(fp.ident);
386 visitor.visit_pat(&fp.pat);
387 walk_list!(visitor, visit_attribute, fp.attrs.iter());
390 pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
392 TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => visitor.visit_ty(ty),
393 TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
394 TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
395 walk_list!(visitor, visit_lifetime, opt_lifetime);
396 visitor.visit_ty(&mutable_type.ty)
398 TyKind::Tup(ref tuple_element_types) => {
399 walk_list!(visitor, visit_ty, tuple_element_types);
401 TyKind::BareFn(ref function_declaration) => {
402 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
403 walk_fn_decl(visitor, &function_declaration.decl);
405 TyKind::Path(ref maybe_qself, ref path) => {
406 if let Some(ref qself) = *maybe_qself {
407 visitor.visit_ty(&qself.ty);
409 visitor.visit_path(path, typ.id);
411 TyKind::Array(ref ty, ref length) => {
412 visitor.visit_ty(ty);
413 visitor.visit_anon_const(length)
415 TyKind::TraitObject(ref bounds, ..) | TyKind::ImplTrait(_, ref bounds) => {
416 walk_list!(visitor, visit_param_bound, bounds);
418 TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
419 TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
420 TyKind::Mac(ref mac) => visitor.visit_mac(mac),
421 TyKind::Never | TyKind::CVarArgs => {}
425 pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
426 for segment in &path.segments {
427 visitor.visit_path_segment(path.span, segment);
431 pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree, id: NodeId) {
432 visitor.visit_path(&use_tree.prefix, id);
433 match use_tree.kind {
434 UseTreeKind::Simple(rename, ..) => {
435 // The extra IDs are handled during HIR lowering.
436 if let Some(rename) = rename {
437 visitor.visit_ident(rename);
440 UseTreeKind::Glob => {}
441 UseTreeKind::Nested(ref use_trees) => {
442 for &(ref nested_tree, nested_id) in use_trees {
443 visitor.visit_use_tree(nested_tree, nested_id, true);
449 pub fn walk_path_segment<'a, V: Visitor<'a>>(
452 segment: &'a PathSegment,
454 visitor.visit_ident(segment.ident);
455 if let Some(ref args) = segment.args {
456 visitor.visit_generic_args(path_span, args);
460 pub fn walk_generic_args<'a, V>(visitor: &mut V, _path_span: Span, generic_args: &'a GenericArgs)
464 match *generic_args {
465 GenericArgs::AngleBracketed(ref data) => {
466 walk_list!(visitor, visit_generic_arg, &data.args);
467 walk_list!(visitor, visit_assoc_ty_constraint, &data.constraints);
469 GenericArgs::Parenthesized(ref data) => {
470 walk_list!(visitor, visit_ty, &data.inputs);
471 walk_fn_ret_ty(visitor, &data.output);
476 pub fn walk_assoc_ty_constraint<'a, V: Visitor<'a>>(
478 constraint: &'a AssocTyConstraint,
480 visitor.visit_ident(constraint.ident);
481 match constraint.kind {
482 AssocTyConstraintKind::Equality { ref ty } => {
483 visitor.visit_ty(ty);
485 AssocTyConstraintKind::Bound { ref bounds } => {
486 walk_list!(visitor, visit_param_bound, bounds);
491 pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
493 PatKind::TupleStruct(ref path, ref elems) => {
494 visitor.visit_path(path, pattern.id);
495 walk_list!(visitor, visit_pat, elems);
497 PatKind::Path(ref opt_qself, ref path) => {
498 if let Some(ref qself) = *opt_qself {
499 visitor.visit_ty(&qself.ty);
501 visitor.visit_path(path, pattern.id)
503 PatKind::Struct(ref path, ref fields, _) => {
504 visitor.visit_path(path, pattern.id);
505 walk_list!(visitor, visit_field_pattern, fields);
507 PatKind::Box(ref subpattern)
508 | PatKind::Ref(ref subpattern, _)
509 | PatKind::Paren(ref subpattern) => visitor.visit_pat(subpattern),
510 PatKind::Ident(_, ident, ref optional_subpattern) => {
511 visitor.visit_ident(ident);
512 walk_list!(visitor, visit_pat, optional_subpattern);
514 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
515 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
516 walk_list!(visitor, visit_expr, lower_bound);
517 walk_list!(visitor, visit_expr, upper_bound);
519 PatKind::Wild | PatKind::Rest => {}
520 PatKind::Tuple(ref elems) | PatKind::Slice(ref elems) | PatKind::Or(ref elems) => {
521 walk_list!(visitor, visit_pat, elems);
523 PatKind::Mac(ref mac) => visitor.visit_mac(mac),
527 pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignItem) {
528 let ForeignItem { id, span, ident, vis, attrs, kind, tokens: _ } = item;
529 walk_nested_item(visitor, *id, *span, *ident, vis, attrs, kind, FnCtxt::Foreign);
532 pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) {
536 pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
538 GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
539 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
543 pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
544 visitor.visit_ident(param.ident);
545 walk_list!(visitor, visit_attribute, param.attrs.iter());
546 walk_list!(visitor, visit_param_bound, ¶m.bounds);
548 GenericParamKind::Lifetime => (),
549 GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
550 GenericParamKind::Const { ref ty, .. } => visitor.visit_ty(ty),
554 pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
555 walk_list!(visitor, visit_generic_param, &generics.params);
556 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
559 pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
561 WherePredicate::BoundPredicate(WhereBoundPredicate {
564 ref bound_generic_params,
567 visitor.visit_ty(bounded_ty);
568 walk_list!(visitor, visit_param_bound, bounds);
569 walk_list!(visitor, visit_generic_param, bound_generic_params);
571 WherePredicate::RegionPredicate(WhereRegionPredicate {
572 ref lifetime, ref bounds, ..
574 visitor.visit_lifetime(lifetime);
575 walk_list!(visitor, visit_param_bound, bounds);
577 WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }) => {
578 visitor.visit_ty(lhs_ty);
579 visitor.visit_ty(rhs_ty);
584 pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy) {
585 if let FnRetTy::Ty(ref output_ty) = *ret_ty {
586 visitor.visit_ty(output_ty)
590 pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
591 for param in &function_declaration.inputs {
592 visitor.visit_param(param);
594 visitor.visit_fn_ret_ty(&function_declaration.output);
597 pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Span) {
599 FnKind::Fn(_, _, sig, _, body) => {
600 visitor.visit_fn_header(&sig.header);
601 walk_fn_decl(visitor, &sig.decl);
602 walk_list!(visitor, visit_block, body);
604 FnKind::Closure(decl, body) => {
605 walk_fn_decl(visitor, decl);
606 visitor.visit_expr(body);
611 pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, ctxt: AssocCtxt) {
612 let AssocItem { id, span, ident, vis, attrs, kind, tokens: _, defaultness: _ } = item;
613 walk_nested_item(visitor, *id, *span, *ident, vis, attrs, kind, FnCtxt::Assoc(ctxt));
616 fn walk_nested_item<'a, V: Visitor<'a>>(
622 attrs: &'a [Attribute],
623 kind: &'a AssocItemKind,
626 visitor.visit_vis(vis);
627 visitor.visit_ident(ident);
628 walk_list!(visitor, visit_attribute, attrs);
630 AssocItemKind::Const(ty, expr) | AssocItemKind::Static(ty, _, expr) => {
631 visitor.visit_ty(ty);
632 walk_list!(visitor, visit_expr, expr);
634 AssocItemKind::Fn(sig, generics, body) => {
635 visitor.visit_generics(generics);
636 let kind = FnKind::Fn(ctxt, ident, sig, vis, body.as_deref());
637 visitor.visit_fn(kind, span, id);
639 AssocItemKind::TyAlias(generics, bounds, ty) => {
640 visitor.visit_generics(generics);
641 walk_list!(visitor, visit_param_bound, bounds);
642 walk_list!(visitor, visit_ty, ty);
644 AssocItemKind::Macro(mac) => {
645 visitor.visit_mac(mac);
650 pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
651 walk_list!(visitor, visit_struct_field, struct_definition.fields());
654 pub fn walk_struct_field<'a, V: Visitor<'a>>(visitor: &mut V, struct_field: &'a StructField) {
655 visitor.visit_vis(&struct_field.vis);
656 if let Some(ident) = struct_field.ident {
657 visitor.visit_ident(ident);
659 visitor.visit_ty(&struct_field.ty);
660 walk_list!(visitor, visit_attribute, &struct_field.attrs);
663 pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
664 walk_list!(visitor, visit_stmt, &block.stmts);
667 pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
668 match statement.kind {
669 StmtKind::Local(ref local) => visitor.visit_local(local),
670 StmtKind::Item(ref item) => visitor.visit_item(item),
671 StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
672 visitor.visit_expr(expression)
674 StmtKind::Mac(ref mac) => {
675 let (ref mac, _, ref attrs) = **mac;
676 visitor.visit_mac(mac);
677 for attr in attrs.iter() {
678 visitor.visit_attribute(attr);
684 pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a Mac) {
685 visitor.visit_path(&mac.path, DUMMY_NODE_ID);
688 pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
689 visitor.visit_expr(&constant.value);
692 pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
693 walk_list!(visitor, visit_attribute, expression.attrs.iter());
695 match expression.kind {
696 ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
697 ExprKind::Array(ref subexpressions) => {
698 walk_list!(visitor, visit_expr, subexpressions);
700 ExprKind::Repeat(ref element, ref count) => {
701 visitor.visit_expr(element);
702 visitor.visit_anon_const(count)
704 ExprKind::Struct(ref path, ref fields, ref optional_base) => {
705 visitor.visit_path(path, expression.id);
706 walk_list!(visitor, visit_field, fields);
707 walk_list!(visitor, visit_expr, optional_base);
709 ExprKind::Tup(ref subexpressions) => {
710 walk_list!(visitor, visit_expr, subexpressions);
712 ExprKind::Call(ref callee_expression, ref arguments) => {
713 visitor.visit_expr(callee_expression);
714 walk_list!(visitor, visit_expr, arguments);
716 ExprKind::MethodCall(ref segment, ref arguments) => {
717 visitor.visit_path_segment(expression.span, segment);
718 walk_list!(visitor, visit_expr, arguments);
720 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
721 visitor.visit_expr(left_expression);
722 visitor.visit_expr(right_expression)
724 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
725 visitor.visit_expr(subexpression)
727 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
728 visitor.visit_expr(subexpression);
729 visitor.visit_ty(typ)
731 ExprKind::Let(ref pat, ref scrutinee) => {
732 visitor.visit_pat(pat);
733 visitor.visit_expr(scrutinee);
735 ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
736 visitor.visit_expr(head_expression);
737 visitor.visit_block(if_block);
738 walk_list!(visitor, visit_expr, optional_else);
740 ExprKind::While(ref subexpression, ref block, ref opt_label) => {
741 walk_list!(visitor, visit_label, opt_label);
742 visitor.visit_expr(subexpression);
743 visitor.visit_block(block);
745 ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
746 walk_list!(visitor, visit_label, opt_label);
747 visitor.visit_pat(pattern);
748 visitor.visit_expr(subexpression);
749 visitor.visit_block(block);
751 ExprKind::Loop(ref block, ref opt_label) => {
752 walk_list!(visitor, visit_label, opt_label);
753 visitor.visit_block(block);
755 ExprKind::Match(ref subexpression, ref arms) => {
756 visitor.visit_expr(subexpression);
757 walk_list!(visitor, visit_arm, arms);
759 ExprKind::Closure(_, _, _, ref decl, ref body, _decl_span) => {
760 visitor.visit_fn(FnKind::Closure(decl, body), expression.span, expression.id)
762 ExprKind::Block(ref block, ref opt_label) => {
763 walk_list!(visitor, visit_label, opt_label);
764 visitor.visit_block(block);
766 ExprKind::Async(_, _, ref body) => {
767 visitor.visit_block(body);
769 ExprKind::Await(ref expr) => visitor.visit_expr(expr),
770 ExprKind::Assign(ref lhs, ref rhs, _) => {
771 visitor.visit_expr(lhs);
772 visitor.visit_expr(rhs);
774 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
775 visitor.visit_expr(left_expression);
776 visitor.visit_expr(right_expression);
778 ExprKind::Field(ref subexpression, ident) => {
779 visitor.visit_expr(subexpression);
780 visitor.visit_ident(ident);
782 ExprKind::Index(ref main_expression, ref index_expression) => {
783 visitor.visit_expr(main_expression);
784 visitor.visit_expr(index_expression)
786 ExprKind::Range(ref start, ref end, _) => {
787 walk_list!(visitor, visit_expr, start);
788 walk_list!(visitor, visit_expr, end);
790 ExprKind::Path(ref maybe_qself, ref path) => {
791 if let Some(ref qself) = *maybe_qself {
792 visitor.visit_ty(&qself.ty);
794 visitor.visit_path(path, expression.id)
796 ExprKind::Break(ref opt_label, ref opt_expr) => {
797 walk_list!(visitor, visit_label, opt_label);
798 walk_list!(visitor, visit_expr, opt_expr);
800 ExprKind::Continue(ref opt_label) => {
801 walk_list!(visitor, visit_label, opt_label);
803 ExprKind::Ret(ref optional_expression) => {
804 walk_list!(visitor, visit_expr, optional_expression);
806 ExprKind::Mac(ref mac) => visitor.visit_mac(mac),
807 ExprKind::Paren(ref subexpression) => visitor.visit_expr(subexpression),
808 ExprKind::InlineAsm(ref ia) => {
809 for &(_, ref input) in &ia.inputs {
810 visitor.visit_expr(input)
812 for output in &ia.outputs {
813 visitor.visit_expr(&output.expr)
816 ExprKind::Yield(ref optional_expression) => {
817 walk_list!(visitor, visit_expr, optional_expression);
819 ExprKind::Try(ref subexpression) => visitor.visit_expr(subexpression),
820 ExprKind::TryBlock(ref body) => visitor.visit_block(body),
821 ExprKind::Lit(_) | ExprKind::Err => {}
824 visitor.visit_expr_post(expression)
827 pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {
828 walk_list!(visitor, visit_attribute, param.attrs.iter());
829 visitor.visit_pat(¶m.pat);
830 visitor.visit_ty(¶m.ty);
833 pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
834 visitor.visit_pat(&arm.pat);
835 walk_list!(visitor, visit_expr, &arm.guard);
836 visitor.visit_expr(&arm.body);
837 walk_list!(visitor, visit_attribute, &arm.attrs);
840 pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
841 if let VisibilityKind::Restricted { ref path, id } = vis.node {
842 visitor.visit_path(path, id);
846 pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
848 AttrKind::Normal(ref item) => walk_mac_args(visitor, &item.args),
849 AttrKind::DocComment(_) => {}
853 pub fn walk_mac_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a MacArgs) {
856 MacArgs::Delimited(_dspan, _delim, tokens) => visitor.visit_tts(tokens.clone()),
857 MacArgs::Eq(_eq_span, tokens) => visitor.visit_tts(tokens.clone()),
861 pub fn walk_tt<'a, V: Visitor<'a>>(visitor: &mut V, tt: TokenTree) {
863 TokenTree::Token(token) => visitor.visit_token(token),
864 TokenTree::Delimited(_, _, tts) => visitor.visit_tts(tts),
868 pub fn walk_tts<'a, V: Visitor<'a>>(visitor: &mut V, tts: TokenStream) {
869 for tt in tts.trees() {
870 visitor.visit_tt(tt);