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