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