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