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