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