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