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