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