]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/visit.rs
73e731397c329ca7aec8092769d18057d27e4412
[rust.git] / src / libsyntax / visit.rs
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.
5 //!
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.
11 //!
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.
15
16 use crate::ast::*;
17 use crate::token::Token;
18 use crate::tokenstream::{TokenStream, TokenTree};
19
20 use rustc_span::Span;
21
22 #[derive(Copy, Clone, PartialEq)]
23 pub enum AssocCtxt {
24     Trait,
25     Impl,
26 }
27
28 #[derive(Copy, Clone, PartialEq)]
29 pub enum FnCtxt {
30     Free,
31     Foreign,
32     Assoc(AssocCtxt),
33 }
34
35 #[derive(Copy, Clone)]
36 pub enum FnKind<'a> {
37     /// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
38     Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, Option<&'a Block>),
39
40     /// E.g., `|x, y| body`.
41     Closure(&'a FnDecl, &'a Expr),
42 }
43
44 impl<'a> FnKind<'a> {
45     pub fn header(&self) -> Option<&'a FnHeader> {
46         match *self {
47             FnKind::Fn(_, _, sig, _, _) => Some(&sig.header),
48             FnKind::Closure(_, _) => None,
49         }
50     }
51
52     pub fn decl(&self) -> &'a FnDecl {
53         match self {
54             FnKind::Fn(_, _, sig, _, _) => &sig.decl,
55             FnKind::Closure(decl, _) => decl,
56         }
57     }
58
59     pub fn ctxt(&self) -> Option<FnCtxt> {
60         match self {
61             FnKind::Fn(ctxt, ..) => Some(*ctxt),
62             FnKind::Closure(..) => None,
63         }
64     }
65 }
66
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`.
71 ///
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) {
78         // Nothing to do.
79     }
80     fn visit_ident(&mut self, ident: Ident) {
81         walk_ident(self, ident);
82     }
83     fn visit_mod(&mut self, m: &'ast Mod, _s: Span, _attrs: &[Attribute], _n: NodeId) {
84         walk_mod(self, m);
85     }
86     fn visit_foreign_item(&mut self, i: &'ast ForeignItem) {
87         walk_foreign_item(self, i)
88     }
89     fn visit_global_asm(&mut self, ga: &'ast GlobalAsm) {
90         walk_global_asm(self, ga)
91     }
92     fn visit_item(&mut self, i: &'ast Item) {
93         walk_item(self, i)
94     }
95     fn visit_local(&mut self, l: &'ast Local) {
96         walk_local(self, l)
97     }
98     fn visit_block(&mut self, b: &'ast Block) {
99         walk_block(self, b)
100     }
101     fn visit_stmt(&mut self, s: &'ast Stmt) {
102         walk_stmt(self, s)
103     }
104     fn visit_param(&mut self, param: &'ast Param) {
105         walk_param(self, param)
106     }
107     fn visit_arm(&mut self, a: &'ast Arm) {
108         walk_arm(self, a)
109     }
110     fn visit_pat(&mut self, p: &'ast Pat) {
111         walk_pat(self, p)
112     }
113     fn visit_anon_const(&mut self, c: &'ast AnonConst) {
114         walk_anon_const(self, c)
115     }
116     fn visit_expr(&mut self, ex: &'ast Expr) {
117         walk_expr(self, ex)
118     }
119     fn visit_expr_post(&mut self, _ex: &'ast Expr) {}
120     fn visit_ty(&mut self, t: &'ast Ty) {
121         walk_ty(self, t)
122     }
123     fn visit_generic_param(&mut self, param: &'ast GenericParam) {
124         walk_generic_param(self, param)
125     }
126     fn visit_generics(&mut self, g: &'ast Generics) {
127         walk_generics(self, g)
128     }
129     fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
130         walk_where_predicate(self, p)
131     }
132     fn visit_fn(&mut self, fk: FnKind<'ast>, s: Span, _: NodeId) {
133         walk_fn(self, fk, s)
134     }
135     fn visit_assoc_item(&mut self, i: &'ast AssocItem, ctxt: AssocCtxt) {
136         walk_assoc_item(self, i, ctxt)
137     }
138     fn visit_trait_ref(&mut self, t: &'ast TraitRef) {
139         walk_trait_ref(self, t)
140     }
141     fn visit_param_bound(&mut self, bounds: &'ast GenericBound) {
142         walk_param_bound(self, bounds)
143     }
144     fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
145         walk_poly_trait_ref(self, t, m)
146     }
147     fn visit_variant_data(&mut self, s: &'ast VariantData) {
148         walk_struct_def(self, s)
149     }
150     fn visit_struct_field(&mut self, s: &'ast StructField) {
151         walk_struct_field(self, s)
152     }
153     fn visit_enum_def(
154         &mut self,
155         enum_definition: &'ast EnumDef,
156         generics: &'ast Generics,
157         item_id: NodeId,
158         _: Span,
159     ) {
160         walk_enum_def(self, enum_definition, generics, item_id)
161     }
162     fn visit_variant(&mut self, v: &'ast Variant) {
163         walk_variant(self, v)
164     }
165     fn visit_label(&mut self, label: &'ast Label) {
166         walk_label(self, label)
167     }
168     fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
169         walk_lifetime(self, lifetime)
170     }
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)
178     }
179     fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
180         // Nothing to do
181     }
182     fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
183         walk_path(self, path)
184     }
185     fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
186         walk_use_tree(self, use_tree, id)
187     }
188     fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
189         walk_path_segment(self, path_span, path_segment)
190     }
191     fn visit_generic_args(&mut self, path_span: Span, generic_args: &'ast GenericArgs) {
192         walk_generic_args(self, path_span, generic_args)
193     }
194     fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
195         match generic_arg {
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),
199         }
200     }
201     fn visit_assoc_ty_constraint(&mut self, constraint: &'ast AssocTyConstraint) {
202         walk_assoc_ty_constraint(self, constraint)
203     }
204     fn visit_attribute(&mut self, attr: &'ast Attribute) {
205         walk_attribute(self, attr)
206     }
207     fn visit_tt(&mut self, tt: TokenTree) {
208         walk_tt(self, tt)
209     }
210     fn visit_tts(&mut self, tts: TokenStream) {
211         walk_tts(self, tts)
212     }
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) {
216         walk_vis(self, vis)
217     }
218     fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FunctionRetTy) {
219         walk_fn_ret_ty(self, ret_ty)
220     }
221     fn visit_fn_header(&mut self, _header: &'ast FnHeader) {
222         // Nothing to do
223     }
224     fn visit_field(&mut self, f: &'ast Field) {
225         walk_field(self, f)
226     }
227     fn visit_field_pattern(&mut self, fp: &'ast FieldPat) {
228         walk_field_pattern(self, fp)
229     }
230 }
231
232 #[macro_export]
233 macro_rules! walk_list {
234     ($visitor: expr, $method: ident, $list: expr) => {
235         for elem in $list {
236             $visitor.$method(elem)
237         }
238     };
239     ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
240         for elem in $list {
241             $visitor.$method(elem, $($extra_args,)*)
242         }
243     }
244 }
245
246 pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, ident: Ident) {
247     visitor.visit_name(ident.span, ident.name);
248 }
249
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);
253 }
254
255 pub fn walk_mod<'a, V: Visitor<'a>>(visitor: &mut V, module: &'a Mod) {
256     walk_list!(visitor, visit_item, &module.items);
257 }
258
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);
262     }
263     visitor.visit_pat(&local.pat);
264     walk_list!(visitor, visit_ty, &local.ty);
265     walk_list!(visitor, visit_expr, &local.init);
266 }
267
268 pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, label: &'a Label) {
269     visitor.visit_ident(label.ident);
270 }
271
272 pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
273     visitor.visit_ident(lifetime.ident);
274 }
275
276 pub fn walk_poly_trait_ref<'a, V>(
277     visitor: &mut V,
278     trait_ref: &'a PolyTraitRef,
279     _: &TraitBoundModifier,
280 ) where
281     V: Visitor<'a>,
282 {
283     walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
284     visitor.visit_trait_ref(&trait_ref.trait_ref);
285 }
286
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)
289 }
290
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);
294     match item.kind {
295         ItemKind::ExternCrate(orig_name) => {
296             if let Some(orig_name) = orig_name {
297                 visitor.visit_name(item.span, orig_name);
298             }
299         }
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             visitor.visit_expr(expr);
304         }
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)
309         }
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);
313         }
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)
318         }
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)
322         }
323         ItemKind::Impl {
324             unsafety: _,
325             polarity: _,
326             defaultness: _,
327             constness: _,
328             ref generics,
329             ref of_trait,
330             ref self_ty,
331             ref items,
332         } => {
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);
337         }
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);
342         }
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);
347         }
348         ItemKind::TraitAlias(ref generics, ref bounds) => {
349             visitor.visit_generics(generics);
350             walk_list!(visitor, visit_param_bound, bounds);
351         }
352         ItemKind::Mac(ref mac) => visitor.visit_mac(mac),
353         ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
354     }
355     walk_list!(visitor, visit_attribute, &item.attrs);
356 }
357
358 pub fn walk_enum_def<'a, V: Visitor<'a>>(
359     visitor: &mut V,
360     enum_definition: &'a EnumDef,
361     _: &'a Generics,
362     _: NodeId,
363 ) {
364     walk_list!(visitor, visit_variant, &enum_definition.variants);
365 }
366
367 pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant)
368 where
369     V: Visitor<'a>,
370 {
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);
376 }
377
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());
382 }
383
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());
388 }
389
390 pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
391     match typ.kind {
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)
397         }
398         TyKind::Tup(ref tuple_element_types) => {
399             walk_list!(visitor, visit_ty, tuple_element_types);
400         }
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);
404         }
405         TyKind::Path(ref maybe_qself, ref path) => {
406             if let Some(ref qself) = *maybe_qself {
407                 visitor.visit_ty(&qself.ty);
408             }
409             visitor.visit_path(path, typ.id);
410         }
411         TyKind::Array(ref ty, ref length) => {
412             visitor.visit_ty(ty);
413             visitor.visit_anon_const(length)
414         }
415         TyKind::TraitObject(ref bounds, ..) | TyKind::ImplTrait(_, ref bounds) => {
416             walk_list!(visitor, visit_param_bound, bounds);
417         }
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 => {}
422     }
423 }
424
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);
428     }
429 }
430
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);
438             }
439         }
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);
444             }
445         }
446     }
447 }
448
449 pub fn walk_path_segment<'a, V: Visitor<'a>>(
450     visitor: &mut V,
451     path_span: Span,
452     segment: &'a PathSegment,
453 ) {
454     visitor.visit_ident(segment.ident);
455     if let Some(ref args) = segment.args {
456         visitor.visit_generic_args(path_span, args);
457     }
458 }
459
460 pub fn walk_generic_args<'a, V>(visitor: &mut V, _path_span: Span, generic_args: &'a GenericArgs)
461 where
462     V: Visitor<'a>,
463 {
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);
468         }
469         GenericArgs::Parenthesized(ref data) => {
470             walk_list!(visitor, visit_ty, &data.inputs);
471             walk_fn_ret_ty(visitor, &data.output);
472         }
473     }
474 }
475
476 pub fn walk_assoc_ty_constraint<'a, V: Visitor<'a>>(
477     visitor: &mut V,
478     constraint: &'a AssocTyConstraint,
479 ) {
480     visitor.visit_ident(constraint.ident);
481     match constraint.kind {
482         AssocTyConstraintKind::Equality { ref ty } => {
483             visitor.visit_ty(ty);
484         }
485         AssocTyConstraintKind::Bound { ref bounds } => {
486             walk_list!(visitor, visit_param_bound, bounds);
487         }
488     }
489 }
490
491 pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
492     match pattern.kind {
493         PatKind::TupleStruct(ref path, ref elems) => {
494             visitor.visit_path(path, pattern.id);
495             walk_list!(visitor, visit_pat, elems);
496         }
497         PatKind::Path(ref opt_qself, ref path) => {
498             if let Some(ref qself) = *opt_qself {
499                 visitor.visit_ty(&qself.ty);
500             }
501             visitor.visit_path(path, pattern.id)
502         }
503         PatKind::Struct(ref path, ref fields, _) => {
504             visitor.visit_path(path, pattern.id);
505             walk_list!(visitor, visit_field_pattern, fields);
506         }
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);
513         }
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);
518         }
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);
522         }
523         PatKind::Mac(ref mac) => visitor.visit_mac(mac),
524     }
525 }
526
527 pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignItem) {
528     visitor.visit_vis(&item.vis);
529     visitor.visit_ident(item.ident);
530
531     match item.kind {
532         ForeignItemKind::Fn(ref sig, ref generics, ref body) => {
533             visitor.visit_generics(generics);
534             let kind = FnKind::Fn(FnCtxt::Foreign, item.ident, sig, &item.vis, body.as_deref());
535             visitor.visit_fn(kind, item.span, item.id);
536         }
537         ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
538         ForeignItemKind::Ty => (),
539         ForeignItemKind::Macro(ref mac) => visitor.visit_mac(mac),
540     }
541
542     walk_list!(visitor, visit_attribute, &item.attrs);
543 }
544
545 pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) {
546     // Empty!
547 }
548
549 pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
550     match *bound {
551         GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
552         GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
553     }
554 }
555
556 pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
557     visitor.visit_ident(param.ident);
558     walk_list!(visitor, visit_attribute, param.attrs.iter());
559     walk_list!(visitor, visit_param_bound, &param.bounds);
560     match param.kind {
561         GenericParamKind::Lifetime => (),
562         GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
563         GenericParamKind::Const { ref ty, .. } => visitor.visit_ty(ty),
564     }
565 }
566
567 pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
568     walk_list!(visitor, visit_generic_param, &generics.params);
569     walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
570 }
571
572 pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
573     match *predicate {
574         WherePredicate::BoundPredicate(WhereBoundPredicate {
575             ref bounded_ty,
576             ref bounds,
577             ref bound_generic_params,
578             ..
579         }) => {
580             visitor.visit_ty(bounded_ty);
581             walk_list!(visitor, visit_param_bound, bounds);
582             walk_list!(visitor, visit_generic_param, bound_generic_params);
583         }
584         WherePredicate::RegionPredicate(WhereRegionPredicate {
585             ref lifetime, ref bounds, ..
586         }) => {
587             visitor.visit_lifetime(lifetime);
588             walk_list!(visitor, visit_param_bound, bounds);
589         }
590         WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }) => {
591             visitor.visit_ty(lhs_ty);
592             visitor.visit_ty(rhs_ty);
593         }
594     }
595 }
596
597 pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FunctionRetTy) {
598     if let FunctionRetTy::Ty(ref output_ty) = *ret_ty {
599         visitor.visit_ty(output_ty)
600     }
601 }
602
603 pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
604     for param in &function_declaration.inputs {
605         visitor.visit_param(param);
606     }
607     visitor.visit_fn_ret_ty(&function_declaration.output);
608 }
609
610 pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Span) {
611     match kind {
612         FnKind::Fn(_, _, sig, _, body) => {
613             visitor.visit_fn_header(&sig.header);
614             walk_fn_decl(visitor, &sig.decl);
615             walk_list!(visitor, visit_block, body);
616         }
617         FnKind::Closure(decl, body) => {
618             walk_fn_decl(visitor, decl);
619             visitor.visit_expr(body);
620         }
621     }
622 }
623
624 pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, ctxt: AssocCtxt) {
625     visitor.visit_vis(&item.vis);
626     visitor.visit_ident(item.ident);
627     walk_list!(visitor, visit_attribute, &item.attrs);
628     visitor.visit_generics(&item.generics);
629     match item.kind {
630         AssocItemKind::Const(ref ty, ref expr) => {
631             visitor.visit_ty(ty);
632             walk_list!(visitor, visit_expr, expr);
633         }
634         AssocItemKind::Fn(ref sig, ref body) => {
635             let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), item.ident, sig, &item.vis, body.as_deref());
636             visitor.visit_fn(kind, item.span, item.id);
637         }
638         AssocItemKind::TyAlias(ref bounds, ref ty) => {
639             walk_list!(visitor, visit_param_bound, bounds);
640             walk_list!(visitor, visit_ty, ty);
641         }
642         AssocItemKind::Macro(ref mac) => {
643             visitor.visit_mac(mac);
644         }
645     }
646 }
647
648 pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
649     walk_list!(visitor, visit_struct_field, struct_definition.fields());
650 }
651
652 pub fn walk_struct_field<'a, V: Visitor<'a>>(visitor: &mut V, struct_field: &'a StructField) {
653     visitor.visit_vis(&struct_field.vis);
654     if let Some(ident) = struct_field.ident {
655         visitor.visit_ident(ident);
656     }
657     visitor.visit_ty(&struct_field.ty);
658     walk_list!(visitor, visit_attribute, &struct_field.attrs);
659 }
660
661 pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
662     walk_list!(visitor, visit_stmt, &block.stmts);
663 }
664
665 pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
666     match statement.kind {
667         StmtKind::Local(ref local) => visitor.visit_local(local),
668         StmtKind::Item(ref item) => visitor.visit_item(item),
669         StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
670             visitor.visit_expr(expression)
671         }
672         StmtKind::Mac(ref mac) => {
673             let (ref mac, _, ref attrs) = **mac;
674             visitor.visit_mac(mac);
675             for attr in attrs.iter() {
676                 visitor.visit_attribute(attr);
677             }
678         }
679     }
680 }
681
682 pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a Mac) {
683     visitor.visit_path(&mac.path, DUMMY_NODE_ID);
684 }
685
686 pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
687     visitor.visit_expr(&constant.value);
688 }
689
690 pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
691     walk_list!(visitor, visit_attribute, expression.attrs.iter());
692
693     match expression.kind {
694         ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
695         ExprKind::Array(ref subexpressions) => {
696             walk_list!(visitor, visit_expr, subexpressions);
697         }
698         ExprKind::Repeat(ref element, ref count) => {
699             visitor.visit_expr(element);
700             visitor.visit_anon_const(count)
701         }
702         ExprKind::Struct(ref path, ref fields, ref optional_base) => {
703             visitor.visit_path(path, expression.id);
704             walk_list!(visitor, visit_field, fields);
705             walk_list!(visitor, visit_expr, optional_base);
706         }
707         ExprKind::Tup(ref subexpressions) => {
708             walk_list!(visitor, visit_expr, subexpressions);
709         }
710         ExprKind::Call(ref callee_expression, ref arguments) => {
711             visitor.visit_expr(callee_expression);
712             walk_list!(visitor, visit_expr, arguments);
713         }
714         ExprKind::MethodCall(ref segment, ref arguments) => {
715             visitor.visit_path_segment(expression.span, segment);
716             walk_list!(visitor, visit_expr, arguments);
717         }
718         ExprKind::Binary(_, ref left_expression, ref right_expression) => {
719             visitor.visit_expr(left_expression);
720             visitor.visit_expr(right_expression)
721         }
722         ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
723             visitor.visit_expr(subexpression)
724         }
725         ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
726             visitor.visit_expr(subexpression);
727             visitor.visit_ty(typ)
728         }
729         ExprKind::Let(ref pat, ref scrutinee) => {
730             visitor.visit_pat(pat);
731             visitor.visit_expr(scrutinee);
732         }
733         ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
734             visitor.visit_expr(head_expression);
735             visitor.visit_block(if_block);
736             walk_list!(visitor, visit_expr, optional_else);
737         }
738         ExprKind::While(ref subexpression, ref block, ref opt_label) => {
739             walk_list!(visitor, visit_label, opt_label);
740             visitor.visit_expr(subexpression);
741             visitor.visit_block(block);
742         }
743         ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
744             walk_list!(visitor, visit_label, opt_label);
745             visitor.visit_pat(pattern);
746             visitor.visit_expr(subexpression);
747             visitor.visit_block(block);
748         }
749         ExprKind::Loop(ref block, ref opt_label) => {
750             walk_list!(visitor, visit_label, opt_label);
751             visitor.visit_block(block);
752         }
753         ExprKind::Match(ref subexpression, ref arms) => {
754             visitor.visit_expr(subexpression);
755             walk_list!(visitor, visit_arm, arms);
756         }
757         ExprKind::Closure(_, _, _, ref decl, ref body, _decl_span) => {
758             visitor.visit_fn(FnKind::Closure(decl, body), expression.span, expression.id)
759         }
760         ExprKind::Block(ref block, ref opt_label) => {
761             walk_list!(visitor, visit_label, opt_label);
762             visitor.visit_block(block);
763         }
764         ExprKind::Async(_, _, ref body) => {
765             visitor.visit_block(body);
766         }
767         ExprKind::Await(ref expr) => visitor.visit_expr(expr),
768         ExprKind::Assign(ref lhs, ref rhs, _) => {
769             visitor.visit_expr(lhs);
770             visitor.visit_expr(rhs);
771         }
772         ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
773             visitor.visit_expr(left_expression);
774             visitor.visit_expr(right_expression);
775         }
776         ExprKind::Field(ref subexpression, ident) => {
777             visitor.visit_expr(subexpression);
778             visitor.visit_ident(ident);
779         }
780         ExprKind::Index(ref main_expression, ref index_expression) => {
781             visitor.visit_expr(main_expression);
782             visitor.visit_expr(index_expression)
783         }
784         ExprKind::Range(ref start, ref end, _) => {
785             walk_list!(visitor, visit_expr, start);
786             walk_list!(visitor, visit_expr, end);
787         }
788         ExprKind::Path(ref maybe_qself, ref path) => {
789             if let Some(ref qself) = *maybe_qself {
790                 visitor.visit_ty(&qself.ty);
791             }
792             visitor.visit_path(path, expression.id)
793         }
794         ExprKind::Break(ref opt_label, ref opt_expr) => {
795             walk_list!(visitor, visit_label, opt_label);
796             walk_list!(visitor, visit_expr, opt_expr);
797         }
798         ExprKind::Continue(ref opt_label) => {
799             walk_list!(visitor, visit_label, opt_label);
800         }
801         ExprKind::Ret(ref optional_expression) => {
802             walk_list!(visitor, visit_expr, optional_expression);
803         }
804         ExprKind::Mac(ref mac) => visitor.visit_mac(mac),
805         ExprKind::Paren(ref subexpression) => visitor.visit_expr(subexpression),
806         ExprKind::InlineAsm(ref ia) => {
807             for &(_, ref input) in &ia.inputs {
808                 visitor.visit_expr(input)
809             }
810             for output in &ia.outputs {
811                 visitor.visit_expr(&output.expr)
812             }
813         }
814         ExprKind::Yield(ref optional_expression) => {
815             walk_list!(visitor, visit_expr, optional_expression);
816         }
817         ExprKind::Try(ref subexpression) => visitor.visit_expr(subexpression),
818         ExprKind::TryBlock(ref body) => visitor.visit_block(body),
819         ExprKind::Lit(_) | ExprKind::Err => {}
820     }
821
822     visitor.visit_expr_post(expression)
823 }
824
825 pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {
826     walk_list!(visitor, visit_attribute, param.attrs.iter());
827     visitor.visit_pat(&param.pat);
828     visitor.visit_ty(&param.ty);
829 }
830
831 pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
832     visitor.visit_pat(&arm.pat);
833     walk_list!(visitor, visit_expr, &arm.guard);
834     visitor.visit_expr(&arm.body);
835     walk_list!(visitor, visit_attribute, &arm.attrs);
836 }
837
838 pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
839     if let VisibilityKind::Restricted { ref path, id } = vis.node {
840         visitor.visit_path(path, id);
841     }
842 }
843
844 pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
845     match attr.kind {
846         AttrKind::Normal(ref item) => walk_mac_args(visitor, &item.args),
847         AttrKind::DocComment(_) => {}
848     }
849 }
850
851 pub fn walk_mac_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a MacArgs) {
852     match args {
853         MacArgs::Empty => {}
854         MacArgs::Delimited(_dspan, _delim, tokens) => visitor.visit_tts(tokens.clone()),
855         MacArgs::Eq(_eq_span, tokens) => visitor.visit_tts(tokens.clone()),
856     }
857 }
858
859 pub fn walk_tt<'a, V: Visitor<'a>>(visitor: &mut V, tt: TokenTree) {
860     match tt {
861         TokenTree::Token(token) => visitor.visit_token(token),
862         TokenTree::Delimited(_, _, tts) => visitor.visit_tts(tts),
863     }
864 }
865
866 pub fn walk_tts<'a, V: Visitor<'a>>(visitor: &mut V, tts: TokenStream) {
867     for tt in tts.trees() {
868         visitor.visit_tt(tt);
869     }
870 }