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