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