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