]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/visit.rs
Auto merge of #27474 - bluss:twoway-reverse, r=brson
[rust.git] / src / libsyntax / visit.rs
1 // Copyright 2012-2014 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 pub use self::FnKind::*;
27
28 use abi::Abi;
29 use ast::*;
30 use ast;
31 use codemap::Span;
32 use ptr::P;
33 use owned_slice::OwnedSlice;
34
35 #[derive(Copy, Clone)]
36 pub enum FnKind<'a> {
37     /// fn foo() or extern "Abi" fn foo()
38     FkItemFn(Ident, &'a Generics, Unsafety, Constness, Abi, Visibility),
39
40     /// fn foo(&self)
41     FkMethod(Ident, &'a MethodSig, Option<Visibility>),
42
43     /// |x, y| ...
44     /// proc(x, y) ...
45     FkFnBlock,
46 }
47
48 /// Each method of the Visitor trait is a hook to be potentially
49 /// overridden.  Each method's default implementation recursively visits
50 /// the substructure of the input via the corresponding `walk` method;
51 /// e.g. the `visit_mod` method by default calls `visit::walk_mod`.
52 ///
53 /// If you want to ensure that your code handles every variant
54 /// explicitly, you need to override each method.  (And you also need
55 /// to monitor future changes to `Visitor` in case a new method with a
56 /// new default implementation gets introduced.)
57 pub trait Visitor<'v> : Sized {
58     fn visit_name(&mut self, _span: Span, _name: Name) {
59         // Nothing to do.
60     }
61     fn visit_ident(&mut self, span: Span, ident: Ident) {
62         self.visit_name(span, ident.name);
63     }
64     fn visit_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) { walk_mod(self, m) }
65     fn visit_foreign_item(&mut self, i: &'v ForeignItem) { walk_foreign_item(self, i) }
66     fn visit_item(&mut self, i: &'v Item) { walk_item(self, i) }
67     fn visit_local(&mut self, l: &'v Local) { walk_local(self, l) }
68     fn visit_block(&mut self, b: &'v Block) { walk_block(self, b) }
69     fn visit_stmt(&mut self, s: &'v Stmt) { walk_stmt(self, s) }
70     fn visit_arm(&mut self, a: &'v Arm) { walk_arm(self, a) }
71     fn visit_pat(&mut self, p: &'v Pat) { walk_pat(self, p) }
72     fn visit_decl(&mut self, d: &'v Decl) { walk_decl(self, d) }
73     fn visit_expr(&mut self, ex: &'v Expr) { walk_expr(self, ex) }
74     fn visit_expr_post(&mut self, _ex: &'v Expr) { }
75     fn visit_ty(&mut self, t: &'v Ty) { walk_ty(self, t) }
76     fn visit_generics(&mut self, g: &'v Generics) { walk_generics(self, g) }
77     fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: &'v Block, s: Span, _: NodeId) {
78         walk_fn(self, fk, fd, b, s)
79     }
80     fn visit_trait_item(&mut self, ti: &'v TraitItem) { walk_trait_item(self, ti) }
81     fn visit_impl_item(&mut self, ii: &'v ImplItem) { walk_impl_item(self, ii) }
82     fn visit_trait_ref(&mut self, t: &'v TraitRef) { walk_trait_ref(self, t) }
83     fn visit_ty_param_bound(&mut self, bounds: &'v TyParamBound) {
84         walk_ty_param_bound(self, bounds)
85     }
86     fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: &'v TraitBoundModifier) {
87         walk_poly_trait_ref(self, t, m)
88     }
89     fn visit_struct_def(&mut self, s: &'v StructDef, _: Ident, _: &'v Generics, _: NodeId) {
90         walk_struct_def(self, s)
91     }
92     fn visit_struct_field(&mut self, s: &'v StructField) { walk_struct_field(self, s) }
93     fn visit_enum_def(&mut self, enum_definition: &'v EnumDef,
94                       generics: &'v Generics) {
95         walk_enum_def(self, enum_definition, generics)
96     }
97
98     fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics) { walk_variant(self, v, g) }
99
100     /// Visits an optional reference to a lifetime. The `span` is the span of some surrounding
101     /// reference should opt_lifetime be None.
102     fn visit_opt_lifetime_ref(&mut self,
103                               _span: Span,
104                               opt_lifetime: &'v Option<Lifetime>) {
105         match *opt_lifetime {
106             Some(ref l) => self.visit_lifetime_ref(l),
107             None => ()
108         }
109     }
110     fn visit_lifetime_bound(&mut self, lifetime: &'v Lifetime) {
111         walk_lifetime_bound(self, lifetime)
112     }
113     fn visit_lifetime_ref(&mut self, lifetime: &'v Lifetime) {
114         walk_lifetime_ref(self, lifetime)
115     }
116     fn visit_lifetime_def(&mut self, lifetime: &'v LifetimeDef) {
117         walk_lifetime_def(self, lifetime)
118     }
119     fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
120         walk_explicit_self(self, es)
121     }
122     fn visit_mac(&mut self, _mac: &'v Mac) {
123         panic!("visit_mac disabled by default");
124         // NB: see note about macros above.
125         // if you really want a visitor that
126         // works on macros, use this
127         // definition in your trait impl:
128         // visit::walk_mac(self, _mac)
129     }
130     fn visit_path(&mut self, path: &'v Path, _id: ast::NodeId) {
131         walk_path(self, path)
132     }
133     fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
134         walk_path_segment(self, path_span, path_segment)
135     }
136     fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'v PathParameters) {
137         walk_path_parameters(self, path_span, path_parameters)
138     }
139     fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
140         walk_assoc_type_binding(self, type_binding)
141     }
142     fn visit_attribute(&mut self, _attr: &'v Attribute) {}
143 }
144
145 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
146     visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
147     for attr in &krate.attrs {
148         visitor.visit_attribute(attr);
149     }
150 }
151
152 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
153     for item in &module.items {
154         visitor.visit_item(&**item)
155     }
156 }
157
158 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
159     visitor.visit_pat(&*local.pat);
160     walk_ty_opt(visitor, &local.ty);
161     walk_expr_opt(visitor, &local.init);
162 }
163
164 pub fn walk_lifetime_def<'v, V: Visitor<'v>>(visitor: &mut V,
165                                               lifetime_def: &'v LifetimeDef) {
166     visitor.visit_name(lifetime_def.lifetime.span, lifetime_def.lifetime.name);
167     for bound in &lifetime_def.bounds {
168         visitor.visit_lifetime_bound(bound);
169     }
170 }
171
172 pub fn walk_lifetime_bound<'v, V: Visitor<'v>>(visitor: &mut V,
173                                                lifetime_ref: &'v Lifetime) {
174     visitor.visit_lifetime_ref(lifetime_ref)
175 }
176
177 pub fn walk_lifetime_ref<'v, V: Visitor<'v>>(visitor: &mut V,
178                                              lifetime_ref: &'v Lifetime) {
179     visitor.visit_name(lifetime_ref.span, lifetime_ref.name)
180 }
181
182 pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V,
183                                               explicit_self: &'v ExplicitSelf) {
184     match explicit_self.node {
185         SelfStatic | SelfValue(_) => {},
186         SelfRegion(ref lifetime, _, _) => {
187             visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime)
188         }
189         SelfExplicit(ref typ, _) => visitor.visit_ty(&**typ),
190     }
191 }
192
193 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
194                                   trait_ref: &'v PolyTraitRef,
195                                   _modifier: &'v TraitBoundModifier)
196     where V: Visitor<'v>
197 {
198     walk_lifetime_decls_helper(visitor, &trait_ref.bound_lifetimes);
199     visitor.visit_trait_ref(&trait_ref.trait_ref);
200 }
201
202 pub fn walk_trait_ref<'v,V>(visitor: &mut V,
203                                    trait_ref: &'v TraitRef)
204     where V: Visitor<'v>
205 {
206     visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
207 }
208
209 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
210     visitor.visit_ident(item.span, item.ident);
211     match item.node {
212         ItemExternCrate(..) => {}
213         ItemUse(ref vp) => {
214             match vp.node {
215                 ViewPathSimple(ident, ref path) => {
216                     visitor.visit_ident(vp.span, ident);
217                     visitor.visit_path(path, item.id);
218                 }
219                 ViewPathGlob(ref path) => {
220                     visitor.visit_path(path, item.id);
221                 }
222                 ViewPathList(ref prefix, ref list) => {
223                     for id in list {
224                         match id.node {
225                             PathListIdent { name, rename, .. } => {
226                                 visitor.visit_ident(id.span, name);
227                                 if let Some(ident) = rename {
228                                     visitor.visit_ident(id.span, ident);
229                                 }
230                             }
231                             PathListMod { rename, .. } => {
232                                 if let Some(ident) = rename {
233                                     visitor.visit_ident(id.span, ident);
234                                 }
235                             }
236                         }
237                     }
238
239                     // Note that the `prefix` here is not a complete
240                     // path, so we don't use `visit_path`.
241                     walk_path(visitor, prefix);
242                 }
243             }
244         }
245         ItemStatic(ref typ, _, ref expr) |
246         ItemConst(ref typ, ref expr) => {
247             visitor.visit_ty(&**typ);
248             visitor.visit_expr(&**expr);
249         }
250         ItemFn(ref declaration, unsafety, constness, abi, ref generics, ref body) => {
251             visitor.visit_fn(FkItemFn(item.ident, generics, unsafety,
252                                       constness, abi, item.vis),
253                              &**declaration,
254                              &**body,
255                              item.span,
256                              item.id)
257         }
258         ItemMod(ref module) => {
259             visitor.visit_mod(module, item.span, item.id)
260         }
261         ItemForeignMod(ref foreign_module) => {
262             for foreign_item in &foreign_module.items {
263                 visitor.visit_foreign_item(&**foreign_item)
264             }
265         }
266         ItemTy(ref typ, ref type_parameters) => {
267             visitor.visit_ty(&**typ);
268             visitor.visit_generics(type_parameters)
269         }
270         ItemEnum(ref enum_definition, ref type_parameters) => {
271             visitor.visit_generics(type_parameters);
272             visitor.visit_enum_def(enum_definition, type_parameters)
273         }
274         ItemDefaultImpl(_, ref trait_ref) => {
275             visitor.visit_trait_ref(trait_ref)
276         }
277         ItemImpl(_, _,
278                  ref type_parameters,
279                  ref trait_reference,
280                  ref typ,
281                  ref impl_items) => {
282             visitor.visit_generics(type_parameters);
283             match *trait_reference {
284                 Some(ref trait_reference) => visitor.visit_trait_ref(trait_reference),
285                 None => ()
286             }
287             visitor.visit_ty(&**typ);
288             for impl_item in impl_items {
289                 visitor.visit_impl_item(impl_item);
290             }
291         }
292         ItemStruct(ref struct_definition, ref generics) => {
293             visitor.visit_generics(generics);
294             visitor.visit_struct_def(&**struct_definition,
295                                      item.ident,
296                                      generics,
297                                      item.id)
298         }
299         ItemTrait(_, ref generics, ref bounds, ref methods) => {
300             visitor.visit_generics(generics);
301             walk_ty_param_bounds_helper(visitor, bounds);
302             for method in methods {
303                 visitor.visit_trait_item(method)
304             }
305         }
306         ItemMac(ref mac) => visitor.visit_mac(mac),
307     }
308     for attr in &item.attrs {
309         visitor.visit_attribute(attr);
310     }
311 }
312
313 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
314                                          enum_definition: &'v EnumDef,
315                                          generics: &'v Generics) {
316     for variant in &enum_definition.variants {
317         visitor.visit_variant(&**variant, generics);
318     }
319 }
320
321 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
322                                         variant: &'v Variant,
323                                         generics: &'v Generics) {
324     visitor.visit_ident(variant.span, variant.node.name);
325
326     match variant.node.kind {
327         TupleVariantKind(ref variant_arguments) => {
328             for variant_argument in variant_arguments {
329                 visitor.visit_ty(&*variant_argument.ty)
330             }
331         }
332         StructVariantKind(ref struct_definition) => {
333             visitor.visit_struct_def(&**struct_definition,
334                                      variant.node.name,
335                                      generics,
336                                      variant.node.id)
337         }
338     }
339     match variant.node.disr_expr {
340         Some(ref expr) => visitor.visit_expr(&**expr),
341         None => ()
342     }
343     for attr in &variant.node.attrs {
344         visitor.visit_attribute(attr);
345     }
346 }
347
348 pub fn skip_ty<'v, V: Visitor<'v>>(_: &mut V, _: &'v Ty) {
349     // Empty!
350 }
351
352 pub fn walk_ty_opt<'v, V: Visitor<'v>>(visitor: &mut V, optional_type: &'v Option<P<Ty>>) {
353     match *optional_type {
354         Some(ref ty) => visitor.visit_ty(&**ty),
355         None => ()
356     }
357 }
358
359 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
360     match typ.node {
361         TyVec(ref ty) | TyParen(ref ty) => {
362             visitor.visit_ty(&**ty)
363         }
364         TyPtr(ref mutable_type) => {
365             visitor.visit_ty(&*mutable_type.ty)
366         }
367         TyRptr(ref lifetime, ref mutable_type) => {
368             visitor.visit_opt_lifetime_ref(typ.span, lifetime);
369             visitor.visit_ty(&*mutable_type.ty)
370         }
371         TyTup(ref tuple_element_types) => {
372             for tuple_element_type in tuple_element_types {
373                 visitor.visit_ty(&**tuple_element_type)
374             }
375         }
376         TyBareFn(ref function_declaration) => {
377             for argument in &function_declaration.decl.inputs {
378                 visitor.visit_ty(&*argument.ty)
379             }
380             walk_fn_ret_ty(visitor, &function_declaration.decl.output);
381             walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
382         }
383         TyPath(ref maybe_qself, ref path) => {
384             if let Some(ref qself) = *maybe_qself {
385                 visitor.visit_ty(&qself.ty);
386             }
387             visitor.visit_path(path, typ.id);
388         }
389         TyObjectSum(ref ty, ref bounds) => {
390             visitor.visit_ty(&**ty);
391             walk_ty_param_bounds_helper(visitor, bounds);
392         }
393         TyFixedLengthVec(ref ty, ref expression) => {
394             visitor.visit_ty(&**ty);
395             visitor.visit_expr(&**expression)
396         }
397         TyPolyTraitRef(ref bounds) => {
398             walk_ty_param_bounds_helper(visitor, bounds)
399         }
400         TyTypeof(ref expression) => {
401             visitor.visit_expr(&**expression)
402         }
403         TyInfer => {}
404         TyMac(ref mac) => {
405             visitor.visit_mac(mac)
406         }
407     }
408 }
409
410 pub fn walk_lifetime_decls_helper<'v, V: Visitor<'v>>(visitor: &mut V,
411                                                       lifetimes: &'v Vec<LifetimeDef>) {
412     for l in lifetimes {
413         visitor.visit_lifetime_def(l);
414     }
415 }
416
417 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
418     for segment in &path.segments {
419         visitor.visit_path_segment(path.span, segment);
420     }
421 }
422
423 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
424                                              path_span: Span,
425                                              segment: &'v PathSegment) {
426     visitor.visit_ident(path_span, segment.identifier);
427     visitor.visit_path_parameters(path_span, &segment.parameters);
428 }
429
430 pub fn walk_path_parameters<'v, V: Visitor<'v>>(visitor: &mut V,
431                                                 _path_span: Span,
432                                                 path_parameters: &'v PathParameters) {
433     match *path_parameters {
434         ast::AngleBracketedParameters(ref data) => {
435             for typ in data.types.iter() {
436                 visitor.visit_ty(&**typ);
437             }
438             for lifetime in &data.lifetimes {
439                 visitor.visit_lifetime_ref(lifetime);
440             }
441             for binding in data.bindings.iter() {
442                 visitor.visit_assoc_type_binding(&**binding);
443             }
444         }
445         ast::ParenthesizedParameters(ref data) => {
446             for typ in &data.inputs {
447                 visitor.visit_ty(&**typ);
448             }
449             if let Some(ref typ) = data.output {
450                 visitor.visit_ty(&**typ);
451             }
452         }
453     }
454 }
455
456 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
457                                                    type_binding: &'v TypeBinding) {
458     visitor.visit_ident(type_binding.span, type_binding.ident);
459     visitor.visit_ty(&*type_binding.ty);
460 }
461
462 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
463     match pattern.node {
464         PatEnum(ref path, ref children) => {
465             visitor.visit_path(path, pattern.id);
466             if let Some(ref children) = *children {
467                 for child in children {
468                     visitor.visit_pat(&*child)
469                 }
470             }
471         }
472         PatQPath(ref qself, ref path) => {
473             visitor.visit_ty(&qself.ty);
474             visitor.visit_path(path, pattern.id)
475         }
476         PatStruct(ref path, ref fields, _) => {
477             visitor.visit_path(path, pattern.id);
478             for field in fields {
479                 visitor.visit_pat(&*field.node.pat)
480             }
481         }
482         PatTup(ref tuple_elements) => {
483             for tuple_element in tuple_elements {
484                 visitor.visit_pat(&**tuple_element)
485             }
486         }
487         PatBox(ref subpattern) |
488         PatRegion(ref subpattern, _) => {
489             visitor.visit_pat(&**subpattern)
490         }
491         PatIdent(_, ref pth1, ref optional_subpattern) => {
492             visitor.visit_ident(pth1.span, pth1.node);
493             match *optional_subpattern {
494                 None => {}
495                 Some(ref subpattern) => visitor.visit_pat(&**subpattern),
496             }
497         }
498         PatLit(ref expression) => visitor.visit_expr(&**expression),
499         PatRange(ref lower_bound, ref upper_bound) => {
500             visitor.visit_expr(&**lower_bound);
501             visitor.visit_expr(&**upper_bound)
502         }
503         PatWild(_) => (),
504         PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
505             for prepattern in prepattern {
506                 visitor.visit_pat(&**prepattern)
507             }
508             if let Some(ref slice_pattern) = *slice_pattern {
509                 visitor.visit_pat(&**slice_pattern)
510             }
511             for postpattern in postpatterns {
512                 visitor.visit_pat(&**postpattern)
513             }
514         }
515         PatMac(ref mac) => visitor.visit_mac(mac),
516     }
517 }
518
519 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
520                                              foreign_item: &'v ForeignItem) {
521     visitor.visit_ident(foreign_item.span, foreign_item.ident);
522
523     match foreign_item.node {
524         ForeignItemFn(ref function_declaration, ref generics) => {
525             walk_fn_decl(visitor, &**function_declaration);
526             visitor.visit_generics(generics)
527         }
528         ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ),
529     }
530
531     for attr in &foreign_item.attrs {
532         visitor.visit_attribute(attr);
533     }
534 }
535
536 pub fn walk_ty_param_bounds_helper<'v, V: Visitor<'v>>(visitor: &mut V,
537                                                        bounds: &'v OwnedSlice<TyParamBound>) {
538     for bound in bounds.iter() {
539         visitor.visit_ty_param_bound(bound)
540     }
541 }
542
543 pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
544                                                bound: &'v TyParamBound) {
545     match *bound {
546         TraitTyParamBound(ref typ, ref modifier) => {
547             visitor.visit_poly_trait_ref(typ, modifier);
548         }
549         RegionTyParamBound(ref lifetime) => {
550             visitor.visit_lifetime_bound(lifetime);
551         }
552     }
553 }
554
555 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
556     for param in generics.ty_params.iter() {
557         visitor.visit_ident(param.span, param.ident);
558         walk_ty_param_bounds_helper(visitor, &param.bounds);
559         walk_ty_opt(visitor, &param.default);
560     }
561     walk_lifetime_decls_helper(visitor, &generics.lifetimes);
562     for predicate in &generics.where_clause.predicates {
563         match predicate {
564             &ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bounded_ty,
565                                                                           ref bounds,
566                                                                           ..}) => {
567                 visitor.visit_ty(&**bounded_ty);
568                 walk_ty_param_bounds_helper(visitor, bounds);
569             }
570             &ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
571                                                                             ref bounds,
572                                                                             ..}) => {
573                 visitor.visit_lifetime_ref(lifetime);
574
575                 for bound in bounds {
576                     visitor.visit_lifetime_ref(bound);
577                 }
578             }
579             &ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{id,
580                                                                     ref path,
581                                                                     ref ty,
582                                                                     ..}) => {
583                 visitor.visit_path(path, id);
584                 visitor.visit_ty(&**ty);
585             }
586         }
587     }
588 }
589
590 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
591     if let Return(ref output_ty) = *ret_ty {
592         visitor.visit_ty(&**output_ty)
593     }
594 }
595
596 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
597     for argument in &function_declaration.inputs {
598         visitor.visit_pat(&*argument.pat);
599         visitor.visit_ty(&*argument.ty)
600     }
601     walk_fn_ret_ty(visitor, &function_declaration.output)
602 }
603
604 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
605                                    function_kind: FnKind<'v>,
606                                    function_declaration: &'v FnDecl,
607                                    function_body: &'v Block,
608                                    _span: Span) {
609     walk_fn_decl(visitor, function_declaration);
610
611     match function_kind {
612         FkItemFn(_, generics, _, _, _, _) => {
613             visitor.visit_generics(generics);
614         }
615         FkMethod(_, sig, _) => {
616             visitor.visit_generics(&sig.generics);
617             visitor.visit_explicit_self(&sig.explicit_self);
618         }
619         FkFnBlock(..) => {}
620     }
621
622     visitor.visit_block(function_body)
623 }
624
625 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
626     visitor.visit_ident(trait_item.span, trait_item.ident);
627     for attr in &trait_item.attrs {
628         visitor.visit_attribute(attr);
629     }
630     match trait_item.node {
631         ConstTraitItem(ref ty, ref default) => {
632             visitor.visit_ty(ty);
633             if let Some(ref expr) = *default {
634                 visitor.visit_expr(expr);
635             }
636         }
637         MethodTraitItem(ref sig, None) => {
638             visitor.visit_explicit_self(&sig.explicit_self);
639             visitor.visit_generics(&sig.generics);
640             walk_fn_decl(visitor, &sig.decl);
641         }
642         MethodTraitItem(ref sig, Some(ref body)) => {
643             visitor.visit_fn(FkMethod(trait_item.ident, sig, None), &sig.decl,
644                              body, trait_item.span, trait_item.id);
645         }
646         TypeTraitItem(ref bounds, ref default) => {
647             walk_ty_param_bounds_helper(visitor, bounds);
648             walk_ty_opt(visitor, default);
649         }
650     }
651 }
652
653 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
654     visitor.visit_ident(impl_item.span, impl_item.ident);
655     for attr in &impl_item.attrs {
656         visitor.visit_attribute(attr);
657     }
658     match impl_item.node {
659         ConstImplItem(ref ty, ref expr) => {
660             visitor.visit_ty(ty);
661             visitor.visit_expr(expr);
662         }
663         MethodImplItem(ref sig, ref body) => {
664             visitor.visit_fn(FkMethod(impl_item.ident, sig, Some(impl_item.vis)), &sig.decl,
665                              body, impl_item.span, impl_item.id);
666         }
667         TypeImplItem(ref ty) => {
668             visitor.visit_ty(ty);
669         }
670         MacImplItem(ref mac) => {
671             visitor.visit_mac(mac);
672         }
673     }
674 }
675
676 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
677                                            struct_definition: &'v StructDef) {
678     for field in &struct_definition.fields {
679         visitor.visit_struct_field(field)
680     }
681 }
682
683 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
684                                              struct_field: &'v StructField) {
685     if let NamedField(name, _) = struct_field.node.kind {
686         visitor.visit_ident(struct_field.span, name);
687     }
688
689     visitor.visit_ty(&*struct_field.node.ty);
690
691     for attr in &struct_field.node.attrs {
692         visitor.visit_attribute(attr);
693     }
694 }
695
696 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
697     for statement in &block.stmts {
698         visitor.visit_stmt(&**statement)
699     }
700     walk_expr_opt(visitor, &block.expr)
701 }
702
703 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
704     match statement.node {
705         StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration),
706         StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
707             visitor.visit_expr(&**expression)
708         }
709         StmtMac(ref mac, _) => visitor.visit_mac(&**mac),
710     }
711 }
712
713 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
714     match declaration.node {
715         DeclLocal(ref local) => visitor.visit_local(&**local),
716         DeclItem(ref item) => visitor.visit_item(&**item),
717     }
718 }
719
720 pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V,
721                                          optional_expression: &'v Option<P<Expr>>) {
722     match *optional_expression {
723         None => {}
724         Some(ref expression) => visitor.visit_expr(&**expression),
725     }
726 }
727
728 pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) {
729     for expression in expressions {
730         visitor.visit_expr(&**expression)
731     }
732 }
733
734 pub fn walk_mac<'v, V: Visitor<'v>>(_: &mut V, _: &'v Mac) {
735     // Empty!
736 }
737
738 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
739     match expression.node {
740         ExprBox(ref place, ref subexpression) => {
741             place.as_ref().map(|e|visitor.visit_expr(&**e));
742             visitor.visit_expr(&**subexpression)
743         }
744         ExprVec(ref subexpressions) => {
745             walk_exprs(visitor, subexpressions)
746         }
747         ExprRepeat(ref element, ref count) => {
748             visitor.visit_expr(&**element);
749             visitor.visit_expr(&**count)
750         }
751         ExprStruct(ref path, ref fields, ref optional_base) => {
752             visitor.visit_path(path, expression.id);
753             for field in fields {
754                 visitor.visit_expr(&*field.expr)
755             }
756             walk_expr_opt(visitor, optional_base)
757         }
758         ExprTup(ref subexpressions) => {
759             for subexpression in subexpressions {
760                 visitor.visit_expr(&**subexpression)
761             }
762         }
763         ExprCall(ref callee_expression, ref arguments) => {
764             for argument in arguments {
765                 visitor.visit_expr(&**argument)
766             }
767             visitor.visit_expr(&**callee_expression)
768         }
769         ExprMethodCall(_, ref types, ref arguments) => {
770             walk_exprs(visitor, arguments);
771             for typ in types {
772                 visitor.visit_ty(&**typ)
773             }
774         }
775         ExprBinary(_, ref left_expression, ref right_expression) => {
776             visitor.visit_expr(&**left_expression);
777             visitor.visit_expr(&**right_expression)
778         }
779         ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
780             visitor.visit_expr(&**subexpression)
781         }
782         ExprLit(_) => {}
783         ExprCast(ref subexpression, ref typ) => {
784             visitor.visit_expr(&**subexpression);
785             visitor.visit_ty(&**typ)
786         }
787         ExprIf(ref head_expression, ref if_block, ref optional_else) => {
788             visitor.visit_expr(&**head_expression);
789             visitor.visit_block(&**if_block);
790             walk_expr_opt(visitor, optional_else)
791         }
792         ExprWhile(ref subexpression, ref block, _) => {
793             visitor.visit_expr(&**subexpression);
794             visitor.visit_block(&**block)
795         }
796         ExprIfLet(ref pattern, ref subexpression, ref if_block, ref optional_else) => {
797             visitor.visit_pat(&**pattern);
798             visitor.visit_expr(&**subexpression);
799             visitor.visit_block(&**if_block);
800             walk_expr_opt(visitor, optional_else);
801         }
802         ExprWhileLet(ref pattern, ref subexpression, ref block, _) => {
803             visitor.visit_pat(&**pattern);
804             visitor.visit_expr(&**subexpression);
805             visitor.visit_block(&**block);
806         }
807         ExprForLoop(ref pattern, ref subexpression, ref block, _) => {
808             visitor.visit_pat(&**pattern);
809             visitor.visit_expr(&**subexpression);
810             visitor.visit_block(&**block)
811         }
812         ExprLoop(ref block, _) => visitor.visit_block(&**block),
813         ExprMatch(ref subexpression, ref arms, _) => {
814             visitor.visit_expr(&**subexpression);
815             for arm in arms {
816                 visitor.visit_arm(arm)
817             }
818         }
819         ExprClosure(_, ref function_declaration, ref body) => {
820             visitor.visit_fn(FkFnBlock,
821                              &**function_declaration,
822                              &**body,
823                              expression.span,
824                              expression.id)
825         }
826         ExprBlock(ref block) => visitor.visit_block(&**block),
827         ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
828             visitor.visit_expr(&**right_hand_expression);
829             visitor.visit_expr(&**left_hand_expression)
830         }
831         ExprAssignOp(_, ref left_expression, ref right_expression) => {
832             visitor.visit_expr(&**right_expression);
833             visitor.visit_expr(&**left_expression)
834         }
835         ExprField(ref subexpression, _) => {
836             visitor.visit_expr(&**subexpression);
837         }
838         ExprTupField(ref subexpression, _) => {
839             visitor.visit_expr(&**subexpression);
840         }
841         ExprIndex(ref main_expression, ref index_expression) => {
842             visitor.visit_expr(&**main_expression);
843             visitor.visit_expr(&**index_expression)
844         }
845         ExprRange(ref start, ref end) => {
846             walk_expr_opt(visitor, start);
847             walk_expr_opt(visitor, end)
848         }
849         ExprPath(ref maybe_qself, ref path) => {
850             if let Some(ref qself) = *maybe_qself {
851                 visitor.visit_ty(&qself.ty);
852             }
853             visitor.visit_path(path, expression.id)
854         }
855         ExprBreak(_) | ExprAgain(_) => {}
856         ExprRet(ref optional_expression) => {
857             walk_expr_opt(visitor, optional_expression)
858         }
859         ExprMac(ref mac) => visitor.visit_mac(mac),
860         ExprParen(ref subexpression) => {
861             visitor.visit_expr(&**subexpression)
862         }
863         ExprInlineAsm(ref ia) => {
864             for input in &ia.inputs {
865                 let (_, ref input) = *input;
866                 visitor.visit_expr(&**input)
867             }
868             for output in &ia.outputs {
869                 let (_, ref output, _) = *output;
870                 visitor.visit_expr(&**output)
871             }
872         }
873     }
874
875     visitor.visit_expr_post(expression)
876 }
877
878 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
879     for pattern in &arm.pats {
880         visitor.visit_pat(&**pattern)
881     }
882     walk_expr_opt(visitor, &arm.guard);
883     visitor.visit_expr(&*arm.body);
884     for attr in &arm.attrs {
885         visitor.visit_attribute(attr);
886     }
887 }