]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/visit.rs
596c5b46b986f2c82c9ad6204ea697c473f035a5
[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::parse::token::Token;
18 use crate::tokenstream::{TokenTree, TokenStream};
19
20 use syntax_pos::Span;
21
22 #[derive(Copy, Clone)]
23 pub enum FnKind<'a> {
24     /// fn foo() or extern "Abi" fn foo()
25     ItemFn(Ident, &'a FnHeader, &'a Visibility, &'a Block),
26
27     /// fn foo(&self)
28     Method(Ident, &'a MethodSig, Option<&'a Visibility>, &'a Block),
29
30     /// |x, y| body
31     Closure(&'a Expr),
32 }
33
34 impl<'a> FnKind<'a> {
35     pub fn header(&self) -> Option<&'a FnHeader> {
36         match *self {
37             FnKind::ItemFn(_, header, _, _) => Some(header),
38             FnKind::Method(_, sig, _, _) => Some(&sig.header),
39             FnKind::Closure(_) => None,
40         }
41     }
42 }
43
44 /// Each method of the Visitor trait is a hook to be potentially
45 /// overridden. Each method's default implementation recursively visits
46 /// the substructure of the input via the corresponding `walk` method;
47 /// e.g., the `visit_mod` method by default calls `visit::walk_mod`.
48 ///
49 /// If you want to ensure that your code handles every variant
50 /// explicitly, you need to override each method. (And you also need
51 /// to monitor future changes to `Visitor` in case a new method with a
52 /// new default implementation gets introduced.)
53 pub trait Visitor<'ast>: Sized {
54     fn visit_name(&mut self, _span: Span, _name: Name) {
55         // Nothing to do.
56     }
57     fn visit_ident(&mut self, ident: Ident) {
58         walk_ident(self, ident);
59     }
60     fn visit_mod(&mut self, m: &'ast Mod, _s: Span, _attrs: &[Attribute], _n: NodeId) {
61         walk_mod(self, m);
62     }
63     fn visit_foreign_item(&mut self, i: &'ast ForeignItem) { walk_foreign_item(self, i) }
64     fn visit_global_asm(&mut self, ga: &'ast GlobalAsm) { walk_global_asm(self, ga) }
65     fn visit_item(&mut self, i: &'ast Item) { walk_item(self, i) }
66     fn visit_local(&mut self, l: &'ast Local) { walk_local(self, l) }
67     fn visit_block(&mut self, b: &'ast Block) { walk_block(self, b) }
68     fn visit_stmt(&mut self, s: &'ast Stmt) { walk_stmt(self, s) }
69     fn visit_arg(&mut self, arg: &'ast Arg) { walk_arg(self, arg) }
70     fn visit_arm(&mut self, a: &'ast Arm) { walk_arm(self, a) }
71     fn visit_pat(&mut self, p: &'ast Pat) { walk_pat(self, p) }
72     fn visit_anon_const(&mut self, c: &'ast AnonConst) { walk_anon_const(self, c) }
73     fn visit_expr(&mut self, ex: &'ast Expr) { walk_expr(self, ex) }
74     fn visit_expr_post(&mut self, _ex: &'ast Expr) { }
75     fn visit_ty(&mut self, t: &'ast Ty) { walk_ty(self, t) }
76     fn visit_generic_param(&mut self, param: &'ast GenericParam) {
77         walk_generic_param(self, param)
78     }
79     fn visit_generics(&mut self, g: &'ast Generics) { walk_generics(self, g) }
80     fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
81         walk_where_predicate(self, p)
82     }
83     fn visit_fn(&mut self, fk: FnKind<'ast>, fd: &'ast FnDecl, s: Span, _: NodeId) {
84         walk_fn(self, fk, fd, s)
85     }
86     fn visit_trait_item(&mut self, ti: &'ast TraitItem) { walk_trait_item(self, ti) }
87     fn visit_impl_item(&mut self, ii: &'ast ImplItem) { walk_impl_item(self, ii) }
88     fn visit_trait_ref(&mut self, t: &'ast TraitRef) { walk_trait_ref(self, t) }
89     fn visit_param_bound(&mut self, bounds: &'ast GenericBound) {
90         walk_param_bound(self, bounds)
91     }
92     fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
93         walk_poly_trait_ref(self, t, m)
94     }
95     fn visit_variant_data(&mut self, s: &'ast VariantData, _: Ident,
96                           _: &'ast Generics, _: NodeId, _: Span) {
97         walk_struct_def(self, s)
98     }
99     fn visit_struct_field(&mut self, s: &'ast StructField) { walk_struct_field(self, s) }
100     fn visit_enum_def(&mut self, enum_definition: &'ast EnumDef,
101                       generics: &'ast Generics, item_id: NodeId, _: Span) {
102         walk_enum_def(self, enum_definition, generics, item_id)
103     }
104     fn visit_variant(&mut self, v: &'ast Variant, g: &'ast Generics, item_id: NodeId) {
105         walk_variant(self, v, g, item_id)
106     }
107     fn visit_label(&mut self, label: &'ast Label) {
108         walk_label(self, label)
109     }
110     fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
111         walk_lifetime(self, lifetime)
112     }
113     fn visit_mac(&mut self, _mac: &'ast Mac) {
114         panic!("visit_mac disabled by default");
115         // N.B., see note about macros above.
116         // if you really want a visitor that
117         // works on macros, use this
118         // definition in your trait impl:
119         // visit::walk_mac(self, _mac)
120     }
121     fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
122         // Nothing to do
123     }
124     fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
125         walk_path(self, path)
126     }
127     fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
128         walk_use_tree(self, use_tree, id)
129     }
130     fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
131         walk_path_segment(self, path_span, path_segment)
132     }
133     fn visit_generic_args(&mut self, path_span: Span, generic_args: &'ast GenericArgs) {
134         walk_generic_args(self, path_span, generic_args)
135     }
136     fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
137         match generic_arg {
138             GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
139             GenericArg::Type(ty) => self.visit_ty(ty),
140             GenericArg::Const(ct) => self.visit_anon_const(ct),
141         }
142     }
143     fn visit_assoc_ty_constraint(&mut self, constraint: &'ast AssocTyConstraint) {
144         walk_assoc_ty_constraint(self, constraint)
145     }
146     fn visit_attribute(&mut self, attr: &'ast Attribute) {
147         walk_attribute(self, attr)
148     }
149     fn visit_tt(&mut self, tt: TokenTree) {
150         walk_tt(self, tt)
151     }
152     fn visit_tts(&mut self, tts: TokenStream) {
153         walk_tts(self, tts)
154     }
155     fn visit_token(&mut self, _t: Token) {}
156     // FIXME: add `visit_interpolated` and `walk_interpolated`
157     fn visit_vis(&mut self, vis: &'ast Visibility) {
158         walk_vis(self, vis)
159     }
160     fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FunctionRetTy) {
161         walk_fn_ret_ty(self, ret_ty)
162     }
163     fn visit_fn_header(&mut self, _header: &'ast FnHeader) {
164         // Nothing to do
165     }
166 }
167
168 #[macro_export]
169 macro_rules! walk_list {
170     ($visitor: expr, $method: ident, $list: expr) => {
171         for elem in $list {
172             $visitor.$method(elem)
173         }
174     };
175     ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
176         for elem in $list {
177             $visitor.$method(elem, $($extra_args,)*)
178         }
179     }
180 }
181
182 pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, ident: Ident) {
183     visitor.visit_name(ident.span, ident.name);
184 }
185
186 pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
187     visitor.visit_mod(&krate.module, krate.span, &krate.attrs, CRATE_NODE_ID);
188     walk_list!(visitor, visit_attribute, &krate.attrs);
189 }
190
191 pub fn walk_mod<'a, V: Visitor<'a>>(visitor: &mut V, module: &'a Mod) {
192     walk_list!(visitor, visit_item, &module.items);
193 }
194
195 pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
196     for attr in local.attrs.iter() {
197         visitor.visit_attribute(attr);
198     }
199     visitor.visit_pat(&local.pat);
200     walk_list!(visitor, visit_ty, &local.ty);
201     walk_list!(visitor, visit_expr, &local.init);
202 }
203
204 pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, label: &'a Label) {
205     visitor.visit_ident(label.ident);
206 }
207
208 pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
209     visitor.visit_ident(lifetime.ident);
210 }
211
212 pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V,
213                                   trait_ref: &'a PolyTraitRef,
214                                   _: &TraitBoundModifier)
215     where V: Visitor<'a>,
216 {
217     walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
218     visitor.visit_trait_ref(&trait_ref.trait_ref);
219 }
220
221 pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) {
222     visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
223 }
224
225 pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
226     visitor.visit_vis(&item.vis);
227     visitor.visit_ident(item.ident);
228     match item.node {
229         ItemKind::ExternCrate(orig_name) => {
230             if let Some(orig_name) = orig_name {
231                 visitor.visit_name(item.span, orig_name);
232             }
233         }
234         ItemKind::Use(ref use_tree) => {
235             visitor.visit_use_tree(use_tree, item.id, false)
236         }
237         ItemKind::Static(ref typ, _, ref expr) |
238         ItemKind::Const(ref typ, ref expr) => {
239             visitor.visit_ty(typ);
240             visitor.visit_expr(expr);
241         }
242         ItemKind::Fn(ref declaration, ref header, ref generics, ref body) => {
243             visitor.visit_generics(generics);
244             visitor.visit_fn_header(header);
245             visitor.visit_fn(FnKind::ItemFn(item.ident, header,
246                                             &item.vis, body),
247                              declaration,
248                              item.span,
249                              item.id)
250         }
251         ItemKind::Mod(ref module) => {
252             visitor.visit_mod(module, item.span, &item.attrs, item.id)
253         }
254         ItemKind::ForeignMod(ref foreign_module) => {
255             walk_list!(visitor, visit_foreign_item, &foreign_module.items);
256         }
257         ItemKind::GlobalAsm(ref ga) => visitor.visit_global_asm(ga),
258         ItemKind::TyAlias(ref typ, ref generics) => {
259             visitor.visit_ty(typ);
260             visitor.visit_generics(generics)
261         }
262         ItemKind::OpaqueTy(ref bounds, ref generics) => {
263             walk_list!(visitor, visit_param_bound, bounds);
264             visitor.visit_generics(generics)
265         }
266         ItemKind::Enum(ref enum_definition, ref generics) => {
267             visitor.visit_generics(generics);
268             visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
269         }
270         ItemKind::Impl(_, _, _,
271                  ref generics,
272                  ref opt_trait_reference,
273                  ref typ,
274                  ref impl_items) => {
275             visitor.visit_generics(generics);
276             walk_list!(visitor, visit_trait_ref, opt_trait_reference);
277             visitor.visit_ty(typ);
278             walk_list!(visitor, visit_impl_item, impl_items);
279         }
280         ItemKind::Struct(ref struct_definition, ref generics) |
281         ItemKind::Union(ref struct_definition, ref generics) => {
282             visitor.visit_generics(generics);
283             visitor.visit_variant_data(struct_definition, item.ident,
284                                      generics, item.id, item.span);
285         }
286         ItemKind::Trait(.., ref generics, ref bounds, ref methods) => {
287             visitor.visit_generics(generics);
288             walk_list!(visitor, visit_param_bound, bounds);
289             walk_list!(visitor, visit_trait_item, methods);
290         }
291         ItemKind::TraitAlias(ref generics, ref bounds) => {
292             visitor.visit_generics(generics);
293             walk_list!(visitor, visit_param_bound, bounds);
294         }
295         ItemKind::Mac(ref mac) => visitor.visit_mac(mac),
296         ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
297     }
298     walk_list!(visitor, visit_attribute, &item.attrs);
299 }
300
301 pub fn walk_enum_def<'a, V: Visitor<'a>>(visitor: &mut V,
302                                  enum_definition: &'a EnumDef,
303                                  generics: &'a Generics,
304                                  item_id: NodeId) {
305     walk_list!(visitor, visit_variant, &enum_definition.variants, generics, item_id);
306 }
307
308 pub fn walk_variant<'a, V>(visitor: &mut V,
309                            variant: &'a Variant,
310                            generics: &'a Generics,
311                            item_id: NodeId)
312     where V: Visitor<'a>,
313 {
314     visitor.visit_ident(variant.node.ident);
315     visitor.visit_variant_data(&variant.node.data, variant.node.ident,
316                              generics, item_id, variant.span);
317     walk_list!(visitor, visit_anon_const, &variant.node.disr_expr);
318     walk_list!(visitor, visit_attribute, &variant.node.attrs);
319 }
320
321 pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
322     match typ.node {
323         TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => {
324             visitor.visit_ty(ty)
325         }
326         TyKind::Ptr(ref mutable_type) => {
327             visitor.visit_ty(&mutable_type.ty)
328         }
329         TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
330             walk_list!(visitor, visit_lifetime, opt_lifetime);
331             visitor.visit_ty(&mutable_type.ty)
332         }
333         TyKind::Never | TyKind::CVarArgs => {}
334         TyKind::Tup(ref tuple_element_types) => {
335             walk_list!(visitor, visit_ty, tuple_element_types);
336         }
337         TyKind::BareFn(ref function_declaration) => {
338             walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
339             walk_fn_decl(visitor, &function_declaration.decl);
340         }
341         TyKind::Path(ref maybe_qself, ref path) => {
342             if let Some(ref qself) = *maybe_qself {
343                 visitor.visit_ty(&qself.ty);
344             }
345             visitor.visit_path(path, typ.id);
346         }
347         TyKind::Array(ref ty, ref length) => {
348             visitor.visit_ty(ty);
349             visitor.visit_anon_const(length)
350         }
351         TyKind::TraitObject(ref bounds, ..) |
352         TyKind::ImplTrait(_, ref bounds) => {
353             walk_list!(visitor, visit_param_bound, bounds);
354         }
355         TyKind::Typeof(ref expression) => {
356             visitor.visit_anon_const(expression)
357         }
358         TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
359         TyKind::Mac(ref mac) => {
360             visitor.visit_mac(mac)
361         }
362     }
363 }
364
365 pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
366     for segment in &path.segments {
367         visitor.visit_path_segment(path.span, segment);
368     }
369 }
370
371 pub fn walk_use_tree<'a, V: Visitor<'a>>(
372     visitor: &mut V, use_tree: &'a UseTree, id: NodeId,
373 ) {
374     visitor.visit_path(&use_tree.prefix, id);
375     match use_tree.kind {
376         UseTreeKind::Simple(rename, ..) => {
377             // the extra IDs are handled during HIR lowering
378             if let Some(rename) = rename {
379                 visitor.visit_ident(rename);
380             }
381         }
382         UseTreeKind::Glob => {},
383         UseTreeKind::Nested(ref use_trees) => {
384             for &(ref nested_tree, nested_id) in use_trees {
385                 visitor.visit_use_tree(nested_tree, nested_id, true);
386             }
387         }
388     }
389 }
390
391 pub fn walk_path_segment<'a, V: Visitor<'a>>(visitor: &mut V,
392                                              path_span: Span,
393                                              segment: &'a PathSegment) {
394     visitor.visit_ident(segment.ident);
395     if let Some(ref args) = segment.args {
396         visitor.visit_generic_args(path_span, args);
397     }
398 }
399
400 pub fn walk_generic_args<'a, V>(visitor: &mut V,
401                                 _path_span: Span,
402                                 generic_args: &'a GenericArgs)
403     where V: Visitor<'a>,
404 {
405     match *generic_args {
406         GenericArgs::AngleBracketed(ref data) => {
407             walk_list!(visitor, visit_generic_arg, &data.args);
408             walk_list!(visitor, visit_assoc_ty_constraint, &data.constraints);
409         }
410         GenericArgs::Parenthesized(ref data) => {
411             walk_list!(visitor, visit_ty, &data.inputs);
412             walk_list!(visitor, visit_ty, &data.output);
413         }
414     }
415 }
416
417 pub fn walk_assoc_ty_constraint<'a, V: Visitor<'a>>(visitor: &mut V,
418                                                     constraint: &'a AssocTyConstraint) {
419     visitor.visit_ident(constraint.ident);
420     match constraint.kind {
421         AssocTyConstraintKind::Equality { ref ty } => {
422             visitor.visit_ty(ty);
423         }
424         AssocTyConstraintKind::Bound { ref bounds } => {
425             walk_list!(visitor, visit_param_bound, bounds);
426         }
427     }
428 }
429
430 pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
431     match pattern.node {
432         PatKind::TupleStruct(ref path, ref elems) => {
433             visitor.visit_path(path, pattern.id);
434             walk_list!(visitor, visit_pat, elems);
435         }
436         PatKind::Path(ref opt_qself, ref path) => {
437             if let Some(ref qself) = *opt_qself {
438                 visitor.visit_ty(&qself.ty);
439             }
440             visitor.visit_path(path, pattern.id)
441         }
442         PatKind::Struct(ref path, ref fields, _) => {
443             visitor.visit_path(path, pattern.id);
444             for field in fields {
445                 walk_list!(visitor, visit_attribute, field.node.attrs.iter());
446                 visitor.visit_ident(field.node.ident);
447                 visitor.visit_pat(&field.node.pat)
448             }
449         }
450         PatKind::Tuple(ref elems) => {
451             walk_list!(visitor, visit_pat, elems);
452         }
453         PatKind::Box(ref subpattern) |
454         PatKind::Ref(ref subpattern, _) |
455         PatKind::Paren(ref subpattern) => {
456             visitor.visit_pat(subpattern)
457         }
458         PatKind::Ident(_, ident, ref optional_subpattern) => {
459             visitor.visit_ident(ident);
460             walk_list!(visitor, visit_pat, optional_subpattern);
461         }
462         PatKind::Lit(ref expression) => visitor.visit_expr(expression),
463         PatKind::Range(ref lower_bound, ref upper_bound, _) => {
464             visitor.visit_expr(lower_bound);
465             visitor.visit_expr(upper_bound);
466         }
467         PatKind::Wild | PatKind::Rest => {},
468         PatKind::Slice(ref elems) => {
469             walk_list!(visitor, visit_pat, elems);
470         }
471         PatKind::Mac(ref mac) => visitor.visit_mac(mac),
472     }
473 }
474
475 pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, foreign_item: &'a ForeignItem) {
476     visitor.visit_vis(&foreign_item.vis);
477     visitor.visit_ident(foreign_item.ident);
478
479     match foreign_item.node {
480         ForeignItemKind::Fn(ref function_declaration, ref generics) => {
481             walk_fn_decl(visitor, function_declaration);
482             visitor.visit_generics(generics)
483         }
484         ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
485         ForeignItemKind::Ty => (),
486         ForeignItemKind::Macro(ref mac) => visitor.visit_mac(mac),
487     }
488
489     walk_list!(visitor, visit_attribute, &foreign_item.attrs);
490 }
491
492 pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) {
493     // Empty!
494 }
495
496 pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
497     match *bound {
498         GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
499         GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
500     }
501 }
502
503 pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
504     visitor.visit_ident(param.ident);
505     walk_list!(visitor, visit_attribute, param.attrs.iter());
506     walk_list!(visitor, visit_param_bound, &param.bounds);
507     match param.kind {
508         GenericParamKind::Lifetime => (),
509         GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
510         GenericParamKind::Const { ref ty, .. } => visitor.visit_ty(ty),
511     }
512 }
513
514 pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
515     walk_list!(visitor, visit_generic_param, &generics.params);
516     walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
517 }
518
519 pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
520     match *predicate {
521         WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
522                                                            ref bounds,
523                                                            ref bound_generic_params,
524                                                            ..}) => {
525             visitor.visit_ty(bounded_ty);
526             walk_list!(visitor, visit_param_bound, bounds);
527             walk_list!(visitor, visit_generic_param, bound_generic_params);
528         }
529         WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
530                                                              ref bounds,
531                                                              ..}) => {
532             visitor.visit_lifetime(lifetime);
533             walk_list!(visitor, visit_param_bound, bounds);
534         }
535         WherePredicate::EqPredicate(WhereEqPredicate{ref lhs_ty,
536                                                      ref rhs_ty,
537                                                      ..}) => {
538             visitor.visit_ty(lhs_ty);
539             visitor.visit_ty(rhs_ty);
540         }
541     }
542 }
543
544 pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FunctionRetTy) {
545     if let FunctionRetTy::Ty(ref output_ty) = *ret_ty {
546         visitor.visit_ty(output_ty)
547     }
548 }
549
550 pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
551     for arg in &function_declaration.inputs {
552         visitor.visit_arg(arg);
553     }
554     visitor.visit_fn_ret_ty(&function_declaration.output);
555 }
556
557 pub fn walk_fn<'a, V>(visitor: &mut V, kind: FnKind<'a>, declaration: &'a FnDecl, _span: Span)
558     where V: Visitor<'a>,
559 {
560     match kind {
561         FnKind::ItemFn(_, header, _, body) => {
562             visitor.visit_fn_header(header);
563             walk_fn_decl(visitor, declaration);
564             visitor.visit_block(body);
565         }
566         FnKind::Method(_, sig, _, body) => {
567             visitor.visit_fn_header(&sig.header);
568             walk_fn_decl(visitor, declaration);
569             visitor.visit_block(body);
570         }
571         FnKind::Closure(body) => {
572             walk_fn_decl(visitor, declaration);
573             visitor.visit_expr(body);
574         }
575     }
576 }
577
578 pub fn walk_trait_item<'a, V: Visitor<'a>>(visitor: &mut V, trait_item: &'a TraitItem) {
579     visitor.visit_ident(trait_item.ident);
580     walk_list!(visitor, visit_attribute, &trait_item.attrs);
581     visitor.visit_generics(&trait_item.generics);
582     match trait_item.node {
583         TraitItemKind::Const(ref ty, ref default) => {
584             visitor.visit_ty(ty);
585             walk_list!(visitor, visit_expr, default);
586         }
587         TraitItemKind::Method(ref sig, None) => {
588             visitor.visit_fn_header(&sig.header);
589             walk_fn_decl(visitor, &sig.decl);
590         }
591         TraitItemKind::Method(ref sig, Some(ref body)) => {
592             visitor.visit_fn(FnKind::Method(trait_item.ident, sig, None, body),
593                              &sig.decl, trait_item.span, trait_item.id);
594         }
595         TraitItemKind::Type(ref bounds, ref default) => {
596             walk_list!(visitor, visit_param_bound, bounds);
597             walk_list!(visitor, visit_ty, default);
598         }
599         TraitItemKind::Macro(ref mac) => {
600             visitor.visit_mac(mac);
601         }
602     }
603 }
604
605 pub fn walk_impl_item<'a, V: Visitor<'a>>(visitor: &mut V, impl_item: &'a ImplItem) {
606     visitor.visit_vis(&impl_item.vis);
607     visitor.visit_ident(impl_item.ident);
608     walk_list!(visitor, visit_attribute, &impl_item.attrs);
609     visitor.visit_generics(&impl_item.generics);
610     match impl_item.node {
611         ImplItemKind::Const(ref ty, ref expr) => {
612             visitor.visit_ty(ty);
613             visitor.visit_expr(expr);
614         }
615         ImplItemKind::Method(ref sig, ref body) => {
616             visitor.visit_fn(FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis), body),
617                              &sig.decl, impl_item.span, impl_item.id);
618         }
619         ImplItemKind::TyAlias(ref ty) => {
620             visitor.visit_ty(ty);
621         }
622         ImplItemKind::OpaqueTy(ref bounds) => {
623             walk_list!(visitor, visit_param_bound, bounds);
624         }
625         ImplItemKind::Macro(ref mac) => {
626             visitor.visit_mac(mac);
627         }
628     }
629 }
630
631 pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
632     walk_list!(visitor, visit_struct_field, struct_definition.fields());
633 }
634
635 pub fn walk_struct_field<'a, V: Visitor<'a>>(visitor: &mut V, struct_field: &'a StructField) {
636     visitor.visit_vis(&struct_field.vis);
637     if let Some(ident) = struct_field.ident {
638         visitor.visit_ident(ident);
639     }
640     visitor.visit_ty(&struct_field.ty);
641     walk_list!(visitor, visit_attribute, &struct_field.attrs);
642 }
643
644 pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
645     walk_list!(visitor, visit_stmt, &block.stmts);
646 }
647
648 pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
649     match statement.node {
650         StmtKind::Local(ref local) => visitor.visit_local(local),
651         StmtKind::Item(ref item) => visitor.visit_item(item),
652         StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
653             visitor.visit_expr(expression)
654         }
655         StmtKind::Mac(ref mac) => {
656             let (ref mac, _, ref attrs) = **mac;
657             visitor.visit_mac(mac);
658             for attr in attrs.iter() {
659                 visitor.visit_attribute(attr);
660             }
661         }
662     }
663 }
664
665 pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a Mac) {
666     visitor.visit_path(&mac.node.path, DUMMY_NODE_ID);
667 }
668
669 pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
670     visitor.visit_expr(&constant.value);
671 }
672
673 pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
674     for attr in expression.attrs.iter() {
675         visitor.visit_attribute(attr);
676     }
677     match expression.node {
678         ExprKind::Box(ref subexpression) => {
679             visitor.visit_expr(subexpression)
680         }
681         ExprKind::Array(ref subexpressions) => {
682             walk_list!(visitor, visit_expr, subexpressions);
683         }
684         ExprKind::Repeat(ref element, ref count) => {
685             visitor.visit_expr(element);
686             visitor.visit_anon_const(count)
687         }
688         ExprKind::Struct(ref path, ref fields, ref optional_base) => {
689             visitor.visit_path(path, expression.id);
690             for field in fields {
691                 walk_list!(visitor, visit_attribute, field.attrs.iter());
692                 visitor.visit_ident(field.ident);
693                 visitor.visit_expr(&field.expr)
694             }
695             walk_list!(visitor, visit_expr, optional_base);
696         }
697         ExprKind::Tup(ref subexpressions) => {
698             walk_list!(visitor, visit_expr, subexpressions);
699         }
700         ExprKind::Call(ref callee_expression, ref arguments) => {
701             visitor.visit_expr(callee_expression);
702             walk_list!(visitor, visit_expr, arguments);
703         }
704         ExprKind::MethodCall(ref segment, ref arguments) => {
705             visitor.visit_path_segment(expression.span, segment);
706             walk_list!(visitor, visit_expr, arguments);
707         }
708         ExprKind::Binary(_, ref left_expression, ref right_expression) => {
709             visitor.visit_expr(left_expression);
710             visitor.visit_expr(right_expression)
711         }
712         ExprKind::AddrOf(_, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
713             visitor.visit_expr(subexpression)
714         }
715         ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
716             visitor.visit_expr(subexpression);
717             visitor.visit_ty(typ)
718         }
719         ExprKind::Let(ref pats, ref scrutinee) => {
720             walk_list!(visitor, visit_pat, pats);
721             visitor.visit_expr(scrutinee);
722         }
723         ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
724             visitor.visit_expr(head_expression);
725             visitor.visit_block(if_block);
726             walk_list!(visitor, visit_expr, optional_else);
727         }
728         ExprKind::While(ref subexpression, ref block, ref opt_label) => {
729             walk_list!(visitor, visit_label, opt_label);
730             visitor.visit_expr(subexpression);
731             visitor.visit_block(block);
732         }
733         ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
734             walk_list!(visitor, visit_label, opt_label);
735             visitor.visit_pat(pattern);
736             visitor.visit_expr(subexpression);
737             visitor.visit_block(block);
738         }
739         ExprKind::Loop(ref block, ref opt_label) => {
740             walk_list!(visitor, visit_label, opt_label);
741             visitor.visit_block(block);
742         }
743         ExprKind::Match(ref subexpression, ref arms) => {
744             visitor.visit_expr(subexpression);
745             walk_list!(visitor, visit_arm, arms);
746         }
747         ExprKind::Closure(_, _, _, ref function_declaration, ref body, _decl_span) => {
748             visitor.visit_fn(FnKind::Closure(body),
749                              function_declaration,
750                              expression.span,
751                              expression.id)
752         }
753         ExprKind::Block(ref block, ref opt_label) => {
754             walk_list!(visitor, visit_label, opt_label);
755             visitor.visit_block(block);
756         }
757         ExprKind::Async(_, _, ref body) => {
758             visitor.visit_block(body);
759         }
760         ExprKind::Await(ref expr) => visitor.visit_expr(expr),
761         ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
762             visitor.visit_expr(left_hand_expression);
763             visitor.visit_expr(right_hand_expression);
764         }
765         ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
766             visitor.visit_expr(left_expression);
767             visitor.visit_expr(right_expression);
768         }
769         ExprKind::Field(ref subexpression, ident) => {
770             visitor.visit_expr(subexpression);
771             visitor.visit_ident(ident);
772         }
773         ExprKind::Index(ref main_expression, ref index_expression) => {
774             visitor.visit_expr(main_expression);
775             visitor.visit_expr(index_expression)
776         }
777         ExprKind::Range(ref start, ref end, _) => {
778             walk_list!(visitor, visit_expr, start);
779             walk_list!(visitor, visit_expr, end);
780         }
781         ExprKind::Path(ref maybe_qself, ref path) => {
782             if let Some(ref qself) = *maybe_qself {
783                 visitor.visit_ty(&qself.ty);
784             }
785             visitor.visit_path(path, expression.id)
786         }
787         ExprKind::Break(ref opt_label, ref opt_expr) => {
788             walk_list!(visitor, visit_label, opt_label);
789             walk_list!(visitor, visit_expr, opt_expr);
790         }
791         ExprKind::Continue(ref opt_label) => {
792             walk_list!(visitor, visit_label, opt_label);
793         }
794         ExprKind::Ret(ref optional_expression) => {
795             walk_list!(visitor, visit_expr, optional_expression);
796         }
797         ExprKind::Mac(ref mac) => visitor.visit_mac(mac),
798         ExprKind::Paren(ref subexpression) => {
799             visitor.visit_expr(subexpression)
800         }
801         ExprKind::InlineAsm(ref ia) => {
802             for &(_, ref input) in &ia.inputs {
803                 visitor.visit_expr(input)
804             }
805             for output in &ia.outputs {
806                 visitor.visit_expr(&output.expr)
807             }
808         }
809         ExprKind::Yield(ref optional_expression) => {
810             walk_list!(visitor, visit_expr, optional_expression);
811         }
812         ExprKind::Try(ref subexpression) => {
813             visitor.visit_expr(subexpression)
814         }
815         ExprKind::TryBlock(ref body) => {
816             visitor.visit_block(body)
817         }
818         ExprKind::Lit(_) | ExprKind::Err => {}
819     }
820
821     visitor.visit_expr_post(expression)
822 }
823
824 pub fn walk_arg<'a, V: Visitor<'a>>(visitor: &mut V, arg: &'a Arg) {
825     walk_list!(visitor, visit_attribute, arg.attrs.iter());
826     visitor.visit_pat(&arg.pat);
827     visitor.visit_ty(&arg.ty);
828 }
829
830 pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
831     walk_list!(visitor, visit_pat, &arm.pats);
832     if let Some(ref e) = &arm.guard {
833         visitor.visit_expr(e);
834     }
835     visitor.visit_expr(&arm.body);
836     walk_list!(visitor, visit_attribute, &arm.attrs);
837 }
838
839 pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
840     if let VisibilityKind::Restricted { ref path, id } = vis.node {
841         visitor.visit_path(path, id);
842     }
843 }
844
845 pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
846     visitor.visit_tts(attr.tokens.clone());
847 }
848
849 pub fn walk_tt<'a, V: Visitor<'a>>(visitor: &mut V, tt: TokenTree) {
850     match tt {
851         TokenTree::Token(token) => visitor.visit_token(token),
852         TokenTree::Delimited(_, _, tts) => visitor.visit_tts(tts),
853     }
854 }
855
856 pub fn walk_tts<'a, V: Visitor<'a>>(visitor: &mut V, tts: TokenStream) {
857     for tt in tts.trees() {
858         visitor.visit_tt(tt);
859     }
860 }