]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/visit.rs
0fa0753b22c8298276987aa44eff0d5d6c39f5bd
[rust.git] / src / libsyntax / visit.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! AST walker. Each overridden visit method has full control over what
12 //! happens with its node, it can do its own traversal of the node's children,
13 //! call `visit::walk_*` to apply the default traversal algorithm, or prevent
14 //! deeper traversal by doing nothing.
15 //!
16 //! Note: it is an important invariant that the default visitor walks the body
17 //! of a function in "execution order" (more concretely, reverse post-order
18 //! with respect to the CFG implied by the AST), meaning that if AST node A may
19 //! execute before AST node B, then A is visited first.  The borrow checker in
20 //! particular relies on this property.
21 //!
22 //! Note: walking an AST before macro expansion is probably a bad idea. For
23 //! instance, a walker looking for item names in a module will miss all of
24 //! those that are created by the expansion of a macro.
25
26 use abi::Abi;
27 use ast::*;
28 use syntax_pos::Span;
29 use codemap::Spanned;
30 use tokenstream::ThinTokenStream;
31
32 #[derive(Copy, Clone, PartialEq, Eq)]
33 pub enum FnKind<'a> {
34     /// fn foo() or extern "Abi" fn foo()
35     ItemFn(Ident, &'a Generics, Unsafety, Spanned<Constness>, Abi, &'a Visibility, &'a Block),
36
37     /// fn foo(&self)
38     Method(Ident, &'a MethodSig, Option<&'a Visibility>, &'a Block),
39
40     /// |x, y| body
41     Closure(&'a Expr),
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, span: Span, ident: Ident) {
58         walk_ident(self, span, 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_expr(&mut self, ex: &'ast Expr) { walk_expr(self, ex) }
72     fn visit_expr_post(&mut self, _ex: &'ast Expr) { }
73     fn visit_ty(&mut self, t: &'ast Ty) { walk_ty(self, t) }
74     fn visit_generics(&mut self, g: &'ast Generics) { walk_generics(self, g) }
75     fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
76         walk_where_predicate(self, p)
77     }
78     fn visit_fn(&mut self, fk: FnKind<'ast>, fd: &'ast FnDecl, s: Span, _: NodeId) {
79         walk_fn(self, fk, fd, s)
80     }
81     fn visit_trait_item(&mut self, ti: &'ast TraitItem) { walk_trait_item(self, ti) }
82     fn visit_impl_item(&mut self, ii: &'ast ImplItem) { walk_impl_item(self, ii) }
83     fn visit_trait_ref(&mut self, t: &'ast TraitRef) { walk_trait_ref(self, t) }
84     fn visit_ty_param_bound(&mut self, bounds: &'ast TyParamBound) {
85         walk_ty_param_bound(self, bounds)
86     }
87     fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
88         walk_poly_trait_ref(self, t, m)
89     }
90     fn visit_variant_data(&mut self, s: &'ast VariantData, _: Ident,
91                           _: &'ast Generics, _: NodeId, _: Span) {
92         walk_struct_def(self, s)
93     }
94     fn visit_struct_field(&mut self, s: &'ast StructField) { walk_struct_field(self, s) }
95     fn visit_enum_def(&mut self, enum_definition: &'ast EnumDef,
96                       generics: &'ast Generics, item_id: NodeId, _: Span) {
97         walk_enum_def(self, enum_definition, generics, item_id)
98     }
99     fn visit_variant(&mut self, v: &'ast Variant, g: &'ast Generics, item_id: NodeId) {
100         walk_variant(self, v, g, item_id)
101     }
102     fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
103         walk_lifetime(self, lifetime)
104     }
105     fn visit_lifetime_def(&mut self, lifetime: &'ast LifetimeDef) {
106         walk_lifetime_def(self, lifetime)
107     }
108     fn visit_mac(&mut self, _mac: &'ast Mac) {
109         panic!("visit_mac disabled by default");
110         // NB: see note about macros above.
111         // if you really want a visitor that
112         // works on macros, use this
113         // definition in your trait impl:
114         // visit::walk_mac(self, _mac)
115     }
116     fn visit_mac_def(&mut self, _mac: &'ast ThinTokenStream, _id: NodeId) {
117         // Nothing to do
118     }
119     fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
120         walk_path(self, path)
121     }
122     fn visit_path_list_item(&mut self, prefix: &'ast Path, item: &'ast PathListItem) {
123         walk_path_list_item(self, prefix, item)
124     }
125     fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
126         walk_path_segment(self, path_span, path_segment)
127     }
128     fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'ast PathParameters) {
129         walk_path_parameters(self, path_span, path_parameters)
130     }
131     fn visit_assoc_type_binding(&mut self, type_binding: &'ast TypeBinding) {
132         walk_assoc_type_binding(self, type_binding)
133     }
134     fn visit_attribute(&mut self, _attr: &'ast Attribute) {}
135     fn visit_vis(&mut self, vis: &'ast Visibility) {
136         walk_vis(self, vis)
137     }
138     fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FunctionRetTy) {
139         walk_fn_ret_ty(self, ret_ty)
140     }
141 }
142
143 #[macro_export]
144 macro_rules! walk_list {
145     ($visitor: expr, $method: ident, $list: expr) => {
146         for elem in $list {
147             $visitor.$method(elem)
148         }
149     };
150     ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
151         for elem in $list {
152             $visitor.$method(elem, $($extra_args,)*)
153         }
154     }
155 }
156
157 pub fn walk_opt_name<'a, V: Visitor<'a>>(visitor: &mut V, span: Span, opt_name: Option<Name>) {
158     if let Some(name) = opt_name {
159         visitor.visit_name(span, name);
160     }
161 }
162
163 pub fn walk_opt_ident<'a, V: Visitor<'a>>(visitor: &mut V, span: Span, opt_ident: Option<Ident>) {
164     if let Some(ident) = opt_ident {
165         visitor.visit_ident(span, ident);
166     }
167 }
168
169 pub fn walk_opt_sp_ident<'a, V: Visitor<'a>>(visitor: &mut V,
170                                              opt_sp_ident: &Option<Spanned<Ident>>) {
171     if let Some(ref sp_ident) = *opt_sp_ident {
172         visitor.visit_ident(sp_ident.span, sp_ident.node);
173     }
174 }
175
176 pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, span: Span, ident: Ident) {
177     visitor.visit_name(span, ident.name);
178 }
179
180 pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
181     visitor.visit_mod(&krate.module, krate.span, &krate.attrs, CRATE_NODE_ID);
182     walk_list!(visitor, visit_attribute, &krate.attrs);
183 }
184
185 pub fn walk_mod<'a, V: Visitor<'a>>(visitor: &mut V, module: &'a Mod) {
186     walk_list!(visitor, visit_item, &module.items);
187 }
188
189 pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
190     for attr in local.attrs.iter() {
191         visitor.visit_attribute(attr);
192     }
193     visitor.visit_pat(&local.pat);
194     walk_list!(visitor, visit_ty, &local.ty);
195     walk_list!(visitor, visit_expr, &local.init);
196 }
197
198 pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
199     visitor.visit_name(lifetime.span, lifetime.name);
200 }
201
202 pub fn walk_lifetime_def<'a, V: Visitor<'a>>(visitor: &mut V, lifetime_def: &'a LifetimeDef) {
203     visitor.visit_lifetime(&lifetime_def.lifetime);
204     walk_list!(visitor, visit_lifetime, &lifetime_def.bounds);
205     walk_list!(visitor, visit_attribute, &*lifetime_def.attrs);
206 }
207
208 pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V,
209                                   trait_ref: &'a PolyTraitRef,
210                                   _: &TraitBoundModifier)
211     where V: Visitor<'a>,
212 {
213     walk_list!(visitor, visit_lifetime_def, &trait_ref.bound_lifetimes);
214     visitor.visit_trait_ref(&trait_ref.trait_ref);
215 }
216
217 pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) {
218     visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
219 }
220
221 pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
222     visitor.visit_vis(&item.vis);
223     visitor.visit_ident(item.span, item.ident);
224     match item.node {
225         ItemKind::ExternCrate(opt_name) => {
226             walk_opt_name(visitor, item.span, opt_name)
227         }
228         ItemKind::Use(ref vp) => {
229             match vp.node {
230                 ViewPathSimple(ident, ref path) => {
231                     visitor.visit_ident(vp.span, ident);
232                     visitor.visit_path(path, item.id);
233                 }
234                 ViewPathGlob(ref path) => {
235                     visitor.visit_path(path, item.id);
236                 }
237                 ViewPathList(ref prefix, ref list) => {
238                     visitor.visit_path(prefix, item.id);
239                     for item in list {
240                         visitor.visit_path_list_item(prefix, item)
241                     }
242                 }
243             }
244         }
245         ItemKind::Static(ref typ, _, ref expr) |
246         ItemKind::Const(ref typ, ref expr) => {
247             visitor.visit_ty(typ);
248             visitor.visit_expr(expr);
249         }
250         ItemKind::Fn(ref declaration, unsafety, constness, abi, ref generics, ref body) => {
251             visitor.visit_fn(FnKind::ItemFn(item.ident, generics, unsafety,
252                                             constness, abi, &item.vis, body),
253                              declaration,
254                              item.span,
255                              item.id)
256         }
257         ItemKind::Mod(ref module) => {
258             visitor.visit_mod(module, item.span, &item.attrs, item.id)
259         }
260         ItemKind::ForeignMod(ref foreign_module) => {
261             walk_list!(visitor, visit_foreign_item, &foreign_module.items);
262         }
263         ItemKind::GlobalAsm(ref ga) => visitor.visit_global_asm(ga),
264         ItemKind::Ty(ref typ, ref type_parameters) => {
265             visitor.visit_ty(typ);
266             visitor.visit_generics(type_parameters)
267         }
268         ItemKind::Enum(ref enum_definition, ref type_parameters) => {
269             visitor.visit_generics(type_parameters);
270             visitor.visit_enum_def(enum_definition, type_parameters, item.id, item.span)
271         }
272         ItemKind::DefaultImpl(_, ref trait_ref) => {
273             visitor.visit_trait_ref(trait_ref)
274         }
275         ItemKind::Impl(_, _, _,
276                  ref type_parameters,
277                  ref opt_trait_reference,
278                  ref typ,
279                  ref impl_items) => {
280             visitor.visit_generics(type_parameters);
281             walk_list!(visitor, visit_trait_ref, opt_trait_reference);
282             visitor.visit_ty(typ);
283             walk_list!(visitor, visit_impl_item, impl_items);
284         }
285         ItemKind::Struct(ref struct_definition, ref generics) |
286         ItemKind::Union(ref struct_definition, ref generics) => {
287             visitor.visit_generics(generics);
288             visitor.visit_variant_data(struct_definition, item.ident,
289                                      generics, item.id, item.span);
290         }
291         ItemKind::Trait(_, ref generics, ref bounds, ref methods) => {
292             visitor.visit_generics(generics);
293             walk_list!(visitor, visit_ty_param_bound, bounds);
294             walk_list!(visitor, visit_trait_item, methods);
295         }
296         ItemKind::Mac(ref mac) => visitor.visit_mac(mac),
297         ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
298     }
299     walk_list!(visitor, visit_attribute, &item.attrs);
300 }
301
302 pub fn walk_enum_def<'a, V: Visitor<'a>>(visitor: &mut V,
303                                  enum_definition: &'a EnumDef,
304                                  generics: &'a Generics,
305                                  item_id: NodeId) {
306     walk_list!(visitor, visit_variant, &enum_definition.variants, generics, item_id);
307 }
308
309 pub fn walk_variant<'a, V>(visitor: &mut V,
310                            variant: &'a Variant,
311                            generics: &'a Generics,
312                            item_id: NodeId)
313     where V: Visitor<'a>,
314 {
315     visitor.visit_ident(variant.span, variant.node.name);
316     visitor.visit_variant_data(&variant.node.data, variant.node.name,
317                              generics, item_id, variant.span);
318     walk_list!(visitor, visit_expr, &variant.node.disr_expr);
319     walk_list!(visitor, visit_attribute, &variant.node.attrs);
320 }
321
322 pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
323     match typ.node {
324         TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => {
325             visitor.visit_ty(ty)
326         }
327         TyKind::Ptr(ref mutable_type) => {
328             visitor.visit_ty(&mutable_type.ty)
329         }
330         TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
331             walk_list!(visitor, visit_lifetime, opt_lifetime);
332             visitor.visit_ty(&mutable_type.ty)
333         }
334         TyKind::Never => {},
335         TyKind::Tup(ref tuple_element_types) => {
336             walk_list!(visitor, visit_ty, tuple_element_types);
337         }
338         TyKind::BareFn(ref function_declaration) => {
339             walk_fn_decl(visitor, &function_declaration.decl);
340             walk_list!(visitor, visit_lifetime_def, &function_declaration.lifetimes);
341         }
342         TyKind::Path(ref maybe_qself, ref path) => {
343             if let Some(ref qself) = *maybe_qself {
344                 visitor.visit_ty(&qself.ty);
345             }
346             visitor.visit_path(path, typ.id);
347         }
348         TyKind::Array(ref ty, ref expression) => {
349             visitor.visit_ty(ty);
350             visitor.visit_expr(expression)
351         }
352         TyKind::TraitObject(ref bounds) |
353         TyKind::ImplTrait(ref bounds) => {
354             walk_list!(visitor, visit_ty_param_bound, bounds);
355         }
356         TyKind::Typeof(ref expression) => {
357             visitor.visit_expr(expression)
358         }
359         TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
360         TyKind::Mac(ref mac) => {
361             visitor.visit_mac(mac)
362         }
363     }
364 }
365
366 pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
367     for segment in &path.segments {
368         visitor.visit_path_segment(path.span, segment);
369     }
370 }
371
372 pub fn walk_path_list_item<'a, V: Visitor<'a>>(visitor: &mut V,
373                                                _prefix: &Path,
374                                                item: &'a PathListItem) {
375     visitor.visit_ident(item.span, item.node.name);
376     walk_opt_ident(visitor, item.span, item.node.rename);
377 }
378
379 pub fn walk_path_segment<'a, V: Visitor<'a>>(visitor: &mut V,
380                                              path_span: Span,
381                                              segment: &'a PathSegment) {
382     visitor.visit_ident(path_span, segment.identifier);
383     if let Some(ref parameters) = segment.parameters {
384         visitor.visit_path_parameters(path_span, parameters);
385     }
386 }
387
388 pub fn walk_path_parameters<'a, V>(visitor: &mut V,
389                                    _path_span: Span,
390                                    path_parameters: &'a PathParameters)
391     where V: Visitor<'a>,
392 {
393     match *path_parameters {
394         PathParameters::AngleBracketed(ref data) => {
395             walk_list!(visitor, visit_ty, &data.types);
396             walk_list!(visitor, visit_lifetime, &data.lifetimes);
397             walk_list!(visitor, visit_assoc_type_binding, &data.bindings);
398         }
399         PathParameters::Parenthesized(ref data) => {
400             walk_list!(visitor, visit_ty, &data.inputs);
401             walk_list!(visitor, visit_ty, &data.output);
402         }
403     }
404 }
405
406 pub fn walk_assoc_type_binding<'a, V: Visitor<'a>>(visitor: &mut V,
407                                                    type_binding: &'a TypeBinding) {
408     visitor.visit_ident(type_binding.span, type_binding.ident);
409     visitor.visit_ty(&type_binding.ty);
410 }
411
412 pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
413     match pattern.node {
414         PatKind::TupleStruct(ref path, ref children, _) => {
415             visitor.visit_path(path, pattern.id);
416             walk_list!(visitor, visit_pat, children);
417         }
418         PatKind::Path(ref opt_qself, ref path) => {
419             if let Some(ref qself) = *opt_qself {
420                 visitor.visit_ty(&qself.ty);
421             }
422             visitor.visit_path(path, pattern.id)
423         }
424         PatKind::Struct(ref path, ref fields, _) => {
425             visitor.visit_path(path, pattern.id);
426             for field in fields {
427                 walk_list!(visitor, visit_attribute, field.node.attrs.iter());
428                 visitor.visit_ident(field.span, field.node.ident);
429                 visitor.visit_pat(&field.node.pat)
430             }
431         }
432         PatKind::Tuple(ref tuple_elements, _) => {
433             walk_list!(visitor, visit_pat, tuple_elements);
434         }
435         PatKind::Box(ref subpattern) |
436         PatKind::Ref(ref subpattern, _) => {
437             visitor.visit_pat(subpattern)
438         }
439         PatKind::Ident(_, ref pth1, ref optional_subpattern) => {
440             visitor.visit_ident(pth1.span, pth1.node);
441             walk_list!(visitor, visit_pat, optional_subpattern);
442         }
443         PatKind::Lit(ref expression) => visitor.visit_expr(expression),
444         PatKind::Range(ref lower_bound, ref upper_bound, _) => {
445             visitor.visit_expr(lower_bound);
446             visitor.visit_expr(upper_bound);
447         }
448         PatKind::Wild => (),
449         PatKind::Slice(ref prepatterns, ref slice_pattern, ref postpatterns) => {
450             walk_list!(visitor, visit_pat, prepatterns);
451             walk_list!(visitor, visit_pat, slice_pattern);
452             walk_list!(visitor, visit_pat, postpatterns);
453         }
454         PatKind::Mac(ref mac) => visitor.visit_mac(mac),
455     }
456 }
457
458 pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, foreign_item: &'a ForeignItem) {
459     visitor.visit_vis(&foreign_item.vis);
460     visitor.visit_ident(foreign_item.span, foreign_item.ident);
461
462     match foreign_item.node {
463         ForeignItemKind::Fn(ref function_declaration, ref generics) => {
464             walk_fn_decl(visitor, function_declaration);
465             visitor.visit_generics(generics)
466         }
467         ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
468     }
469
470     walk_list!(visitor, visit_attribute, &foreign_item.attrs);
471 }
472
473 pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) {
474     // Empty!
475 }
476
477 pub fn walk_ty_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a TyParamBound) {
478     match *bound {
479         TraitTyParamBound(ref typ, ref modifier) => {
480             visitor.visit_poly_trait_ref(typ, modifier);
481         }
482         RegionTyParamBound(ref lifetime) => {
483             visitor.visit_lifetime(lifetime);
484         }
485     }
486 }
487
488 pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
489     for param in &generics.ty_params {
490         visitor.visit_ident(param.span, param.ident);
491         walk_list!(visitor, visit_ty_param_bound, &param.bounds);
492         walk_list!(visitor, visit_ty, &param.default);
493         walk_list!(visitor, visit_attribute, &*param.attrs);
494     }
495     walk_list!(visitor, visit_lifetime_def, &generics.lifetimes);
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_lifetimes,
504                                                            ..}) => {
505             visitor.visit_ty(bounded_ty);
506             walk_list!(visitor, visit_ty_param_bound, bounds);
507             walk_list!(visitor, visit_lifetime_def, bound_lifetimes);
508         }
509         WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
510                                                              ref bounds,
511                                                              ..}) => {
512             visitor.visit_lifetime(lifetime);
513             walk_list!(visitor, visit_lifetime, 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(_, generics, _, _, _, _, body) => {
543             visitor.visit_generics(generics);
544             walk_fn_decl(visitor, declaration);
545             visitor.visit_block(body);
546         }
547         FnKind::Method(_, sig, _, body) => {
548             visitor.visit_generics(&sig.generics);
549             walk_fn_decl(visitor, declaration);
550             visitor.visit_block(body);
551         }
552         FnKind::Closure(body) => {
553             walk_fn_decl(visitor, declaration);
554             visitor.visit_expr(body);
555         }
556     }
557 }
558
559 pub fn walk_trait_item<'a, V: Visitor<'a>>(visitor: &mut V, trait_item: &'a TraitItem) {
560     visitor.visit_ident(trait_item.span, trait_item.ident);
561     walk_list!(visitor, visit_attribute, &trait_item.attrs);
562     match trait_item.node {
563         TraitItemKind::Const(ref ty, ref default) => {
564             visitor.visit_ty(ty);
565             walk_list!(visitor, visit_expr, default);
566         }
567         TraitItemKind::Method(ref sig, None) => {
568             visitor.visit_generics(&sig.generics);
569             walk_fn_decl(visitor, &sig.decl);
570         }
571         TraitItemKind::Method(ref sig, Some(ref body)) => {
572             visitor.visit_fn(FnKind::Method(trait_item.ident, sig, None, body),
573                              &sig.decl, trait_item.span, trait_item.id);
574         }
575         TraitItemKind::Type(ref bounds, ref default) => {
576             walk_list!(visitor, visit_ty_param_bound, bounds);
577             walk_list!(visitor, visit_ty, default);
578         }
579         TraitItemKind::Macro(ref mac) => {
580             visitor.visit_mac(mac);
581         }
582     }
583 }
584
585 pub fn walk_impl_item<'a, V: Visitor<'a>>(visitor: &mut V, impl_item: &'a ImplItem) {
586     visitor.visit_vis(&impl_item.vis);
587     visitor.visit_ident(impl_item.span, impl_item.ident);
588     walk_list!(visitor, visit_attribute, &impl_item.attrs);
589     match impl_item.node {
590         ImplItemKind::Const(ref ty, ref expr) => {
591             visitor.visit_ty(ty);
592             visitor.visit_expr(expr);
593         }
594         ImplItemKind::Method(ref sig, ref body) => {
595             visitor.visit_fn(FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis), body),
596                              &sig.decl, impl_item.span, impl_item.id);
597         }
598         ImplItemKind::Type(ref ty) => {
599             visitor.visit_ty(ty);
600         }
601         ImplItemKind::Macro(ref mac) => {
602             visitor.visit_mac(mac);
603         }
604     }
605 }
606
607 pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
608     walk_list!(visitor, visit_struct_field, struct_definition.fields());
609 }
610
611 pub fn walk_struct_field<'a, V: Visitor<'a>>(visitor: &mut V, struct_field: &'a StructField) {
612     visitor.visit_vis(&struct_field.vis);
613     walk_opt_ident(visitor, struct_field.span, struct_field.ident);
614     visitor.visit_ty(&struct_field.ty);
615     walk_list!(visitor, visit_attribute, &struct_field.attrs);
616 }
617
618 pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
619     walk_list!(visitor, visit_stmt, &block.stmts);
620 }
621
622 pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
623     match statement.node {
624         StmtKind::Local(ref local) => visitor.visit_local(local),
625         StmtKind::Item(ref item) => visitor.visit_item(item),
626         StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
627             visitor.visit_expr(expression)
628         }
629         StmtKind::Mac(ref mac) => {
630             let (ref mac, _, ref attrs) = **mac;
631             visitor.visit_mac(mac);
632             for attr in attrs.iter() {
633                 visitor.visit_attribute(attr);
634             }
635         }
636     }
637 }
638
639 pub fn walk_mac<'a, V: Visitor<'a>>(_: &mut V, _: &Mac) {
640     // Empty!
641 }
642
643 pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
644     for attr in expression.attrs.iter() {
645         visitor.visit_attribute(attr);
646     }
647     match expression.node {
648         ExprKind::Box(ref subexpression) => {
649             visitor.visit_expr(subexpression)
650         }
651         ExprKind::InPlace(ref place, ref subexpression) => {
652             visitor.visit_expr(place);
653             visitor.visit_expr(subexpression)
654         }
655         ExprKind::Array(ref subexpressions) => {
656             walk_list!(visitor, visit_expr, subexpressions);
657         }
658         ExprKind::Repeat(ref element, ref count) => {
659             visitor.visit_expr(element);
660             visitor.visit_expr(count)
661         }
662         ExprKind::Struct(ref path, ref fields, ref optional_base) => {
663             visitor.visit_path(path, expression.id);
664             for field in fields {
665                 walk_list!(visitor, visit_attribute, field.attrs.iter());
666                 visitor.visit_ident(field.ident.span, field.ident.node);
667                 visitor.visit_expr(&field.expr)
668             }
669             walk_list!(visitor, visit_expr, optional_base);
670         }
671         ExprKind::Tup(ref subexpressions) => {
672             walk_list!(visitor, visit_expr, subexpressions);
673         }
674         ExprKind::Call(ref callee_expression, ref arguments) => {
675             visitor.visit_expr(callee_expression);
676             walk_list!(visitor, visit_expr, arguments);
677         }
678         ExprKind::MethodCall(ref ident, ref types, ref arguments) => {
679             visitor.visit_ident(ident.span, ident.node);
680             walk_list!(visitor, visit_ty, types);
681             walk_list!(visitor, visit_expr, arguments);
682         }
683         ExprKind::Binary(_, ref left_expression, ref right_expression) => {
684             visitor.visit_expr(left_expression);
685             visitor.visit_expr(right_expression)
686         }
687         ExprKind::AddrOf(_, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
688             visitor.visit_expr(subexpression)
689         }
690         ExprKind::Lit(_) => {}
691         ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
692             visitor.visit_expr(subexpression);
693             visitor.visit_ty(typ)
694         }
695         ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
696             visitor.visit_expr(head_expression);
697             visitor.visit_block(if_block);
698             walk_list!(visitor, visit_expr, optional_else);
699         }
700         ExprKind::While(ref subexpression, ref block, ref opt_sp_ident) => {
701             visitor.visit_expr(subexpression);
702             visitor.visit_block(block);
703             walk_opt_sp_ident(visitor, opt_sp_ident);
704         }
705         ExprKind::IfLet(ref pattern, ref subexpression, ref if_block, ref optional_else) => {
706             visitor.visit_pat(pattern);
707             visitor.visit_expr(subexpression);
708             visitor.visit_block(if_block);
709             walk_list!(visitor, visit_expr, optional_else);
710         }
711         ExprKind::WhileLet(ref pattern, ref subexpression, ref block, ref opt_sp_ident) => {
712             visitor.visit_pat(pattern);
713             visitor.visit_expr(subexpression);
714             visitor.visit_block(block);
715             walk_opt_sp_ident(visitor, opt_sp_ident);
716         }
717         ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_sp_ident) => {
718             visitor.visit_pat(pattern);
719             visitor.visit_expr(subexpression);
720             visitor.visit_block(block);
721             walk_opt_sp_ident(visitor, opt_sp_ident);
722         }
723         ExprKind::Loop(ref block, ref opt_sp_ident) => {
724             visitor.visit_block(block);
725             walk_opt_sp_ident(visitor, opt_sp_ident);
726         }
727         ExprKind::Match(ref subexpression, ref arms) => {
728             visitor.visit_expr(subexpression);
729             walk_list!(visitor, visit_arm, arms);
730         }
731         ExprKind::Closure(_, ref function_declaration, ref body, _decl_span) => {
732             visitor.visit_fn(FnKind::Closure(body),
733                              function_declaration,
734                              expression.span,
735                              expression.id)
736         }
737         ExprKind::Block(ref block) => visitor.visit_block(block),
738         ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
739             visitor.visit_expr(left_hand_expression);
740             visitor.visit_expr(right_hand_expression);
741         }
742         ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
743             visitor.visit_expr(left_expression);
744             visitor.visit_expr(right_expression);
745         }
746         ExprKind::Field(ref subexpression, ref ident) => {
747             visitor.visit_expr(subexpression);
748             visitor.visit_ident(ident.span, ident.node);
749         }
750         ExprKind::TupField(ref subexpression, _) => {
751             visitor.visit_expr(subexpression);
752         }
753         ExprKind::Index(ref main_expression, ref index_expression) => {
754             visitor.visit_expr(main_expression);
755             visitor.visit_expr(index_expression)
756         }
757         ExprKind::Range(ref start, ref end, _) => {
758             walk_list!(visitor, visit_expr, start);
759             walk_list!(visitor, visit_expr, end);
760         }
761         ExprKind::Path(ref maybe_qself, ref path) => {
762             if let Some(ref qself) = *maybe_qself {
763                 visitor.visit_ty(&qself.ty);
764             }
765             visitor.visit_path(path, expression.id)
766         }
767         ExprKind::Break(ref opt_sp_ident, ref opt_expr) => {
768             walk_opt_sp_ident(visitor, opt_sp_ident);
769             walk_list!(visitor, visit_expr, opt_expr);
770         }
771         ExprKind::Continue(ref opt_sp_ident) => {
772             walk_opt_sp_ident(visitor, opt_sp_ident);
773         }
774         ExprKind::Ret(ref optional_expression) => {
775             walk_list!(visitor, visit_expr, optional_expression);
776         }
777         ExprKind::Mac(ref mac) => visitor.visit_mac(mac),
778         ExprKind::Paren(ref subexpression) => {
779             visitor.visit_expr(subexpression)
780         }
781         ExprKind::InlineAsm(ref ia) => {
782             for &(_, ref input) in &ia.inputs {
783                 visitor.visit_expr(input)
784             }
785             for output in &ia.outputs {
786                 visitor.visit_expr(&output.expr)
787             }
788         }
789         ExprKind::Try(ref subexpression) => {
790             visitor.visit_expr(subexpression)
791         }
792         ExprKind::Catch(ref body) => {
793             visitor.visit_block(body)
794         }
795     }
796
797     visitor.visit_expr_post(expression)
798 }
799
800 pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
801     walk_list!(visitor, visit_pat, &arm.pats);
802     walk_list!(visitor, visit_expr, &arm.guard);
803     visitor.visit_expr(&arm.body);
804     walk_list!(visitor, visit_attribute, &arm.attrs);
805 }
806
807 pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
808     if let Visibility::Restricted { ref path, id } = *vis {
809         visitor.visit_path(path, id);
810     }
811 }