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