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