]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/visit.rs
mk: The beta channel produces things called 'beta'
[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, _mac: &'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, _mac)
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 mac) => visitor.visit_mac(mac),
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         TyBareFn(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_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
413         }
414         TyPath(ref path, id) => {
415             visitor.visit_path(path, id);
416         }
417         TyObjectSum(ref ty, ref bounds) => {
418             visitor.visit_ty(&**ty);
419             walk_ty_param_bounds_helper(visitor, bounds);
420         }
421         TyQPath(ref qpath) => {
422             visitor.visit_ty(&*qpath.self_type);
423             visitor.visit_trait_ref(&*qpath.trait_ref);
424             visitor.visit_ident(typ.span, qpath.item_name);
425         }
426         TyFixedLengthVec(ref ty, ref expression) => {
427             visitor.visit_ty(&**ty);
428             visitor.visit_expr(&**expression)
429         }
430         TyPolyTraitRef(ref bounds) => {
431             walk_ty_param_bounds_helper(visitor, bounds)
432         }
433         TyTypeof(ref expression) => {
434             visitor.visit_expr(&**expression)
435         }
436         TyInfer => {}
437     }
438 }
439
440 pub fn walk_lifetime_decls_helper<'v, V: Visitor<'v>>(visitor: &mut V,
441                                                       lifetimes: &'v Vec<LifetimeDef>) {
442     for l in lifetimes.iter() {
443         visitor.visit_lifetime_def(l);
444     }
445 }
446
447 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
448     for segment in path.segments.iter() {
449         visitor.visit_path_segment(path.span, segment);
450     }
451 }
452
453 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
454                                              path_span: Span,
455                                              segment: &'v PathSegment) {
456     visitor.visit_ident(path_span, segment.identifier);
457     visitor.visit_path_parameters(path_span, &segment.parameters);
458 }
459
460 pub fn walk_path_parameters<'v, V: Visitor<'v>>(visitor: &mut V,
461                                                 _path_span: Span,
462                                                 path_parameters: &'v PathParameters) {
463     match *path_parameters {
464         ast::AngleBracketedParameters(ref data) => {
465             for typ in data.types.iter() {
466                 visitor.visit_ty(&**typ);
467             }
468             for lifetime in data.lifetimes.iter() {
469                 visitor.visit_lifetime_ref(lifetime);
470             }
471             for binding in data.bindings.iter() {
472                 visitor.visit_assoc_type_binding(&**binding);
473             }
474         }
475         ast::ParenthesizedParameters(ref data) => {
476             for typ in data.inputs.iter() {
477                 visitor.visit_ty(&**typ);
478             }
479             for typ in data.output.iter() {
480                 visitor.visit_ty(&**typ);
481             }
482         }
483     }
484 }
485
486 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
487                                                    type_binding: &'v TypeBinding) {
488     visitor.visit_ident(type_binding.span, type_binding.ident);
489     visitor.visit_ty(&*type_binding.ty);
490 }
491
492 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
493     match pattern.node {
494         PatEnum(ref path, ref children) => {
495             visitor.visit_path(path, pattern.id);
496             for children in children.iter() {
497                 for child in children.iter() {
498                     visitor.visit_pat(&**child)
499                 }
500             }
501         }
502         PatStruct(ref path, ref fields, _) => {
503             visitor.visit_path(path, pattern.id);
504             for field in fields.iter() {
505                 visitor.visit_pat(&*field.node.pat)
506             }
507         }
508         PatTup(ref tuple_elements) => {
509             for tuple_element in tuple_elements.iter() {
510                 visitor.visit_pat(&**tuple_element)
511             }
512         }
513         PatBox(ref subpattern) |
514         PatRegion(ref subpattern, _) => {
515             visitor.visit_pat(&**subpattern)
516         }
517         PatIdent(_, ref pth1, ref optional_subpattern) => {
518             visitor.visit_ident(pth1.span, pth1.node);
519             match *optional_subpattern {
520                 None => {}
521                 Some(ref subpattern) => visitor.visit_pat(&**subpattern),
522             }
523         }
524         PatLit(ref expression) => visitor.visit_expr(&**expression),
525         PatRange(ref lower_bound, ref upper_bound) => {
526             visitor.visit_expr(&**lower_bound);
527             visitor.visit_expr(&**upper_bound)
528         }
529         PatWild(_) => (),
530         PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
531             for prepattern in prepattern.iter() {
532                 visitor.visit_pat(&**prepattern)
533             }
534             for slice_pattern in slice_pattern.iter() {
535                 visitor.visit_pat(&**slice_pattern)
536             }
537             for postpattern in postpatterns.iter() {
538                 visitor.visit_pat(&**postpattern)
539             }
540         }
541         PatMac(ref mac) => visitor.visit_mac(mac),
542     }
543 }
544
545 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
546                                              foreign_item: &'v ForeignItem) {
547     visitor.visit_ident(foreign_item.span, foreign_item.ident);
548
549     match foreign_item.node {
550         ForeignItemFn(ref function_declaration, ref generics) => {
551             walk_fn_decl(visitor, &**function_declaration);
552             visitor.visit_generics(generics)
553         }
554         ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ),
555     }
556
557     for attr in foreign_item.attrs.iter() {
558         visitor.visit_attribute(attr);
559     }
560 }
561
562 pub fn walk_ty_param_bounds_helper<'v, V: Visitor<'v>>(visitor: &mut V,
563                                                        bounds: &'v OwnedSlice<TyParamBound>) {
564     for bound in bounds.iter() {
565         visitor.visit_ty_param_bound(bound)
566     }
567 }
568
569 pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
570                                                bound: &'v TyParamBound) {
571     match *bound {
572         TraitTyParamBound(ref typ, ref modifier) => {
573             visitor.visit_poly_trait_ref(typ, modifier);
574         }
575         RegionTyParamBound(ref lifetime) => {
576             visitor.visit_lifetime_bound(lifetime);
577         }
578     }
579 }
580
581 pub fn walk_ty_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v TyParam) {
582     visitor.visit_ident(param.span, param.ident);
583     walk_ty_param_bounds_helper(visitor, &param.bounds);
584     walk_ty_opt(visitor, &param.default);
585 }
586
587 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
588     for type_parameter in generics.ty_params.iter() {
589         walk_ty_param(visitor, type_parameter);
590     }
591     walk_lifetime_decls_helper(visitor, &generics.lifetimes);
592     for predicate in generics.where_clause.predicates.iter() {
593         match predicate {
594             &ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bounded_ty,
595                                                                           ref bounds,
596                                                                           ..}) => {
597                 visitor.visit_ty(&**bounded_ty);
598                 walk_ty_param_bounds_helper(visitor, bounds);
599             }
600             &ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
601                                                                             ref bounds,
602                                                                             ..}) => {
603                 visitor.visit_lifetime_ref(lifetime);
604
605                 for bound in bounds.iter() {
606                     visitor.visit_lifetime_ref(bound);
607                 }
608             }
609             &ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{id,
610                                                                     ref path,
611                                                                     ref ty,
612                                                                     ..}) => {
613                 visitor.visit_path(path, id);
614                 visitor.visit_ty(&**ty);
615             }
616         }
617     }
618 }
619
620 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
621     if let Return(ref output_ty) = *ret_ty {
622         visitor.visit_ty(&**output_ty)
623     }
624 }
625
626 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
627     for argument in function_declaration.inputs.iter() {
628         visitor.visit_pat(&*argument.pat);
629         visitor.visit_ty(&*argument.ty)
630     }
631     walk_fn_ret_ty(visitor, &function_declaration.output)
632 }
633
634 // Note: there is no visit_method() method in the visitor, instead override
635 // visit_fn() and check for FkMethod().  I named this visit_method_helper()
636 // because it is not a default impl of any method, though I doubt that really
637 // clarifies anything. - Niko
638 pub fn walk_method_helper<'v, V: Visitor<'v>>(visitor: &mut V, method: &'v Method) {
639     match method.node {
640         MethDecl(ident, ref generics, _, _, _, ref decl, ref body, _) => {
641             visitor.visit_ident(method.span, ident);
642             visitor.visit_fn(FkMethod(ident, generics, method),
643                              &**decl,
644                              &**body,
645                              method.span,
646                              method.id);
647             for attr in method.attrs.iter() {
648                 visitor.visit_attribute(attr);
649             }
650
651         },
652         MethMac(ref mac) => visitor.visit_mac(mac)
653     }
654 }
655
656 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
657                                    function_kind: FnKind<'v>,
658                                    function_declaration: &'v FnDecl,
659                                    function_body: &'v Block,
660                                    _span: Span) {
661     walk_fn_decl(visitor, function_declaration);
662
663     match function_kind {
664         FkItemFn(_, generics, _, _) => {
665             visitor.visit_generics(generics);
666         }
667         FkMethod(_, generics, method) => {
668             visitor.visit_generics(generics);
669             match method.node {
670                 MethDecl(_, _, _, ref explicit_self, _, _, _, _) =>
671                     visitor.visit_explicit_self(explicit_self),
672                 MethMac(ref mac) =>
673                     visitor.visit_mac(mac)
674             }
675         }
676         FkFnBlock(..) => {}
677     }
678
679     visitor.visit_block(function_body)
680 }
681
682 pub fn walk_ty_method<'v, V: Visitor<'v>>(visitor: &mut V, method_type: &'v TypeMethod) {
683     visitor.visit_ident(method_type.span, method_type.ident);
684     visitor.visit_explicit_self(&method_type.explicit_self);
685     for argument_type in method_type.decl.inputs.iter() {
686         visitor.visit_ty(&*argument_type.ty)
687     }
688     visitor.visit_generics(&method_type.generics);
689     walk_fn_ret_ty(visitor, &method_type.decl.output);
690     for attr in method_type.attrs.iter() {
691         visitor.visit_attribute(attr);
692     }
693 }
694
695 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_method: &'v TraitItem) {
696     match *trait_method {
697         RequiredMethod(ref method_type) => visitor.visit_ty_method(method_type),
698         ProvidedMethod(ref method) => walk_method_helper(visitor, &**method),
699         TypeTraitItem(ref associated_type) => {
700             walk_ty_param(visitor, &associated_type.ty_param);
701         }
702     }
703 }
704
705 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
706                                            struct_definition: &'v StructDef) {
707     for field in struct_definition.fields.iter() {
708         visitor.visit_struct_field(field)
709     }
710 }
711
712 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
713                                              struct_field: &'v StructField) {
714     if let NamedField(name, _) = struct_field.node.kind {
715         visitor.visit_ident(struct_field.span, name);
716     }
717
718     visitor.visit_ty(&*struct_field.node.ty);
719
720     for attr in struct_field.node.attrs.iter() {
721         visitor.visit_attribute(attr);
722     }
723 }
724
725 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
726     for view_item in block.view_items.iter() {
727         visitor.visit_view_item(view_item)
728     }
729     for statement in block.stmts.iter() {
730         visitor.visit_stmt(&**statement)
731     }
732     walk_expr_opt(visitor, &block.expr)
733 }
734
735 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
736     match statement.node {
737         StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration),
738         StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
739             visitor.visit_expr(&**expression)
740         }
741         StmtMac(ref mac, _) => visitor.visit_mac(&**mac),
742     }
743 }
744
745 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
746     match declaration.node {
747         DeclLocal(ref local) => visitor.visit_local(&**local),
748         DeclItem(ref item) => visitor.visit_item(&**item),
749     }
750 }
751
752 pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V,
753                                          optional_expression: &'v Option<P<Expr>>) {
754     match *optional_expression {
755         None => {}
756         Some(ref expression) => visitor.visit_expr(&**expression),
757     }
758 }
759
760 pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) {
761     for expression in expressions.iter() {
762         visitor.visit_expr(&**expression)
763     }
764 }
765
766 pub fn walk_mac<'v, V: Visitor<'v>>(_: &mut V, _: &'v Mac) {
767     // Empty!
768 }
769
770 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
771     match expression.node {
772         ExprBox(ref place, ref subexpression) => {
773             place.as_ref().map(|e|visitor.visit_expr(&**e));
774             visitor.visit_expr(&**subexpression)
775         }
776         ExprVec(ref subexpressions) => {
777             walk_exprs(visitor, subexpressions.as_slice())
778         }
779         ExprRepeat(ref element, ref count) => {
780             visitor.visit_expr(&**element);
781             visitor.visit_expr(&**count)
782         }
783         ExprStruct(ref path, ref fields, ref optional_base) => {
784             visitor.visit_path(path, expression.id);
785             for field in fields.iter() {
786                 visitor.visit_expr(&*field.expr)
787             }
788             walk_expr_opt(visitor, optional_base)
789         }
790         ExprTup(ref subexpressions) => {
791             for subexpression in subexpressions.iter() {
792                 visitor.visit_expr(&**subexpression)
793             }
794         }
795         ExprCall(ref callee_expression, ref arguments) => {
796             for argument in arguments.iter() {
797                 visitor.visit_expr(&**argument)
798             }
799             visitor.visit_expr(&**callee_expression)
800         }
801         ExprMethodCall(_, ref types, ref arguments) => {
802             walk_exprs(visitor, arguments.as_slice());
803             for typ in types.iter() {
804                 visitor.visit_ty(&**typ)
805             }
806         }
807         ExprBinary(_, ref left_expression, ref right_expression) => {
808             visitor.visit_expr(&**left_expression);
809             visitor.visit_expr(&**right_expression)
810         }
811         ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
812             visitor.visit_expr(&**subexpression)
813         }
814         ExprLit(_) => {}
815         ExprCast(ref subexpression, ref typ) => {
816             visitor.visit_expr(&**subexpression);
817             visitor.visit_ty(&**typ)
818         }
819         ExprIf(ref head_expression, ref if_block, ref optional_else) => {
820             visitor.visit_expr(&**head_expression);
821             visitor.visit_block(&**if_block);
822             walk_expr_opt(visitor, optional_else)
823         }
824         ExprWhile(ref subexpression, ref block, _) => {
825             visitor.visit_expr(&**subexpression);
826             visitor.visit_block(&**block)
827         }
828         ExprIfLet(ref pattern, ref subexpression, ref if_block, ref optional_else) => {
829             visitor.visit_pat(&**pattern);
830             visitor.visit_expr(&**subexpression);
831             visitor.visit_block(&**if_block);
832             walk_expr_opt(visitor, optional_else);
833         }
834         ExprWhileLet(ref pattern, ref subexpression, ref block, _) => {
835             visitor.visit_pat(&**pattern);
836             visitor.visit_expr(&**subexpression);
837             visitor.visit_block(&**block);
838         }
839         ExprForLoop(ref pattern, ref subexpression, ref block, _) => {
840             visitor.visit_pat(&**pattern);
841             visitor.visit_expr(&**subexpression);
842             visitor.visit_block(&**block)
843         }
844         ExprLoop(ref block, _) => visitor.visit_block(&**block),
845         ExprMatch(ref subexpression, ref arms, _) => {
846             visitor.visit_expr(&**subexpression);
847             for arm in arms.iter() {
848                 visitor.visit_arm(arm)
849             }
850         }
851         ExprClosure(_, _, ref function_declaration, ref body) => {
852             visitor.visit_fn(FkFnBlock,
853                              &**function_declaration,
854                              &**body,
855                              expression.span,
856                              expression.id)
857         }
858         ExprBlock(ref block) => visitor.visit_block(&**block),
859         ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
860             visitor.visit_expr(&**right_hand_expression);
861             visitor.visit_expr(&**left_hand_expression)
862         }
863         ExprAssignOp(_, ref left_expression, ref right_expression) => {
864             visitor.visit_expr(&**right_expression);
865             visitor.visit_expr(&**left_expression)
866         }
867         ExprField(ref subexpression, _) => {
868             visitor.visit_expr(&**subexpression);
869         }
870         ExprTupField(ref subexpression, _) => {
871             visitor.visit_expr(&**subexpression);
872         }
873         ExprIndex(ref main_expression, ref index_expression) => {
874             visitor.visit_expr(&**main_expression);
875             visitor.visit_expr(&**index_expression)
876         }
877         ExprRange(ref start, ref end) => {
878             walk_expr_opt(visitor, start);
879             walk_expr_opt(visitor, end)
880         }
881         ExprPath(ref path) => {
882             visitor.visit_path(path, expression.id)
883         }
884         ExprBreak(_) | ExprAgain(_) => {}
885         ExprRet(ref optional_expression) => {
886             walk_expr_opt(visitor, optional_expression)
887         }
888         ExprMac(ref mac) => visitor.visit_mac(mac),
889         ExprParen(ref subexpression) => {
890             visitor.visit_expr(&**subexpression)
891         }
892         ExprInlineAsm(ref ia) => {
893             for input in ia.inputs.iter() {
894                 let (_, ref input) = *input;
895                 visitor.visit_expr(&**input)
896             }
897             for output in ia.outputs.iter() {
898                 let (_, ref output, _) = *output;
899                 visitor.visit_expr(&**output)
900             }
901         }
902     }
903
904     visitor.visit_expr_post(expression)
905 }
906
907 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
908     for pattern in arm.pats.iter() {
909         visitor.visit_pat(&**pattern)
910     }
911     walk_expr_opt(visitor, &arm.guard);
912     visitor.visit_expr(&*arm.body);
913     for attr in arm.attrs.iter() {
914         visitor.visit_attribute(attr);
915     }
916 }