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