]> git.lizzy.rs Git - rust.git/blob - src/librustc_front/visit.rs
Use ast attributes every where (remove HIR attributes).
[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_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_ident(item.span, item.ident);
202     match item.node {
203         ItemExternCrate(..) => {}
204         ItemUse(ref vp) => {
205             match vp.node {
206                 ViewPathSimple(ident, ref path) => {
207                     visitor.visit_ident(vp.span, ident);
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                     for id in list {
215                         match id.node {
216                             PathListIdent { name, .. } => {
217                                 visitor.visit_ident(id.span, name);
218                             }
219                             PathListMod { .. } => ()
220                         }
221                     }
222
223                     // Note that the `prefix` here is not a complete
224                     // path, so we don't use `visit_path`.
225                     walk_path(visitor, prefix);
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.ident, 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.ident,
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_ident(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_segment<'v, V: Visitor<'v>>(visitor: &mut V,
404                                              path_span: Span,
405                                              segment: &'v PathSegment) {
406     visitor.visit_ident(path_span, segment.identifier);
407     visitor.visit_path_parameters(path_span, &segment.parameters);
408 }
409
410 pub fn walk_path_parameters<'v, V: Visitor<'v>>(visitor: &mut V,
411                                                 _path_span: Span,
412                                                 path_parameters: &'v PathParameters) {
413     match *path_parameters {
414         hir::AngleBracketedParameters(ref data) => {
415             for typ in data.types.iter() {
416                 visitor.visit_ty(&**typ);
417             }
418             for lifetime in &data.lifetimes {
419                 visitor.visit_lifetime_ref(lifetime);
420             }
421             for binding in data.bindings.iter() {
422                 visitor.visit_assoc_type_binding(&**binding);
423             }
424         }
425         hir::ParenthesizedParameters(ref data) => {
426             for typ in &data.inputs {
427                 visitor.visit_ty(&**typ);
428             }
429             if let Some(ref typ) = data.output {
430                 visitor.visit_ty(&**typ);
431             }
432         }
433     }
434 }
435
436 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
437                                                    type_binding: &'v TypeBinding) {
438     visitor.visit_ident(type_binding.span, type_binding.ident);
439     visitor.visit_ty(&*type_binding.ty);
440 }
441
442 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
443     match pattern.node {
444         PatEnum(ref path, ref children) => {
445             visitor.visit_path(path, pattern.id);
446             if let Some(ref children) = *children {
447                 for child in children {
448                     visitor.visit_pat(&*child)
449                 }
450             }
451         }
452         PatQPath(ref qself, ref path) => {
453             visitor.visit_ty(&qself.ty);
454             visitor.visit_path(path, pattern.id)
455         }
456         PatStruct(ref path, ref fields, _) => {
457             visitor.visit_path(path, pattern.id);
458             for field in fields {
459                 visitor.visit_pat(&*field.node.pat)
460             }
461         }
462         PatTup(ref tuple_elements) => {
463             for tuple_element in tuple_elements {
464                 visitor.visit_pat(&**tuple_element)
465             }
466         }
467         PatBox(ref subpattern) |
468         PatRegion(ref subpattern, _) => {
469             visitor.visit_pat(&**subpattern)
470         }
471         PatIdent(_, ref pth1, ref optional_subpattern) => {
472             visitor.visit_ident(pth1.span, pth1.node);
473             match *optional_subpattern {
474                 None => {}
475                 Some(ref subpattern) => visitor.visit_pat(&**subpattern),
476             }
477         }
478         PatLit(ref expression) => visitor.visit_expr(&**expression),
479         PatRange(ref lower_bound, ref upper_bound) => {
480             visitor.visit_expr(&**lower_bound);
481             visitor.visit_expr(&**upper_bound)
482         }
483         PatWild(_) => (),
484         PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
485             for prepattern in prepattern {
486                 visitor.visit_pat(&**prepattern)
487             }
488             if let Some(ref slice_pattern) = *slice_pattern {
489                 visitor.visit_pat(&**slice_pattern)
490             }
491             for postpattern in postpatterns {
492                 visitor.visit_pat(&**postpattern)
493             }
494         }
495     }
496 }
497
498 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
499                                              foreign_item: &'v ForeignItem) {
500     visitor.visit_ident(foreign_item.span, foreign_item.ident);
501
502     match foreign_item.node {
503         ForeignItemFn(ref function_declaration, ref generics) => {
504             walk_fn_decl(visitor, &**function_declaration);
505             visitor.visit_generics(generics)
506         }
507         ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ),
508     }
509
510     for attr in &foreign_item.attrs {
511         visitor.visit_attribute(attr);
512     }
513 }
514
515 pub fn walk_ty_param_bounds_helper<'v, V: Visitor<'v>>(visitor: &mut V,
516                                                        bounds: &'v OwnedSlice<TyParamBound>) {
517     for bound in bounds.iter() {
518         visitor.visit_ty_param_bound(bound)
519     }
520 }
521
522 pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
523                                                bound: &'v TyParamBound) {
524     match *bound {
525         TraitTyParamBound(ref typ, ref modifier) => {
526             visitor.visit_poly_trait_ref(typ, modifier);
527         }
528         RegionTyParamBound(ref lifetime) => {
529             visitor.visit_lifetime_bound(lifetime);
530         }
531     }
532 }
533
534 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
535     for param in generics.ty_params.iter() {
536         visitor.visit_ident(param.span, param.ident);
537         walk_ty_param_bounds_helper(visitor, &param.bounds);
538         walk_ty_opt(visitor, &param.default);
539     }
540     walk_lifetime_decls_helper(visitor, &generics.lifetimes);
541     for predicate in &generics.where_clause.predicates {
542         match predicate {
543             &hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate{ref bounded_ty,
544                                                                           ref bounds,
545                                                                           ..}) => {
546                 visitor.visit_ty(&**bounded_ty);
547                 walk_ty_param_bounds_helper(visitor, bounds);
548             }
549             &hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate{ref lifetime,
550                                                                             ref bounds,
551                                                                             ..}) => {
552                 visitor.visit_lifetime_ref(lifetime);
553
554                 for bound in bounds {
555                     visitor.visit_lifetime_ref(bound);
556                 }
557             }
558             &hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{id,
559                                                                     ref path,
560                                                                     ref ty,
561                                                                     ..}) => {
562                 visitor.visit_path(path, id);
563                 visitor.visit_ty(&**ty);
564             }
565         }
566     }
567 }
568
569 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
570     if let Return(ref output_ty) = *ret_ty {
571         visitor.visit_ty(&**output_ty)
572     }
573 }
574
575 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
576     for argument in &function_declaration.inputs {
577         visitor.visit_pat(&*argument.pat);
578         visitor.visit_ty(&*argument.ty)
579     }
580     walk_fn_ret_ty(visitor, &function_declaration.output)
581 }
582
583 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V,
584                                         function_kind: FnKind<'v>) {
585     match function_kind {
586         FnKind::ItemFn(_, generics, _, _, _, _) => {
587             visitor.visit_generics(generics);
588         }
589         FnKind::Method(_, sig, _) => {
590             visitor.visit_generics(&sig.generics);
591             visitor.visit_explicit_self(&sig.explicit_self);
592         }
593         FnKind::Closure(..) => {}
594     }
595 }
596
597 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
598                                    function_kind: FnKind<'v>,
599                                    function_declaration: &'v FnDecl,
600                                    function_body: &'v Block,
601                                    _span: Span) {
602     walk_fn_decl(visitor, function_declaration);
603     walk_fn_kind(visitor, function_kind);
604     visitor.visit_block(function_body)
605 }
606
607 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
608     visitor.visit_ident(trait_item.span, trait_item.ident);
609     for attr in &trait_item.attrs {
610         visitor.visit_attribute(attr);
611     }
612     match trait_item.node {
613         ConstTraitItem(ref ty, ref default) => {
614             visitor.visit_ty(ty);
615             if let Some(ref expr) = *default {
616                 visitor.visit_expr(expr);
617             }
618         }
619         MethodTraitItem(ref sig, None) => {
620             visitor.visit_explicit_self(&sig.explicit_self);
621             visitor.visit_generics(&sig.generics);
622             walk_fn_decl(visitor, &sig.decl);
623         }
624         MethodTraitItem(ref sig, Some(ref body)) => {
625             visitor.visit_fn(FnKind::Method(trait_item.ident, sig, None), &sig.decl,
626                              body, trait_item.span, trait_item.id);
627         }
628         TypeTraitItem(ref bounds, ref default) => {
629             walk_ty_param_bounds_helper(visitor, bounds);
630             walk_ty_opt(visitor, default);
631         }
632     }
633 }
634
635 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
636     visitor.visit_ident(impl_item.span, impl_item.ident);
637     for attr in &impl_item.attrs {
638         visitor.visit_attribute(attr);
639     }
640     match impl_item.node {
641         ConstImplItem(ref ty, ref expr) => {
642             visitor.visit_ty(ty);
643             visitor.visit_expr(expr);
644         }
645         MethodImplItem(ref sig, ref body) => {
646             visitor.visit_fn(FnKind::Method(impl_item.ident, sig, Some(impl_item.vis)), &sig.decl,
647                              body, impl_item.span, impl_item.id);
648         }
649         TypeImplItem(ref ty) => {
650             visitor.visit_ty(ty);
651         }
652     }
653 }
654
655 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
656                                            struct_definition: &'v StructDef) {
657     for field in &struct_definition.fields {
658         visitor.visit_struct_field(field)
659     }
660 }
661
662 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
663                                              struct_field: &'v StructField) {
664     if let NamedField(name, _) = struct_field.node.kind {
665         visitor.visit_ident(struct_field.span, name);
666     }
667
668     visitor.visit_ty(&*struct_field.node.ty);
669
670     for attr in &struct_field.node.attrs {
671         visitor.visit_attribute(attr);
672     }
673 }
674
675 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
676     for statement in &block.stmts {
677         visitor.visit_stmt(&**statement)
678     }
679     walk_expr_opt(visitor, &block.expr)
680 }
681
682 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
683     match statement.node {
684         StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration),
685         StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
686             visitor.visit_expr(&**expression)
687         }
688     }
689 }
690
691 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
692     match declaration.node {
693         DeclLocal(ref local) => visitor.visit_local(&**local),
694         DeclItem(ref item) => visitor.visit_item(&**item),
695     }
696 }
697
698 pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V,
699                                          optional_expression: &'v Option<P<Expr>>) {
700     match *optional_expression {
701         None => {}
702         Some(ref expression) => visitor.visit_expr(&**expression),
703     }
704 }
705
706 pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) {
707     for expression in expressions {
708         visitor.visit_expr(&**expression)
709     }
710 }
711
712 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
713     match expression.node {
714         ExprBox(ref place, ref subexpression) => {
715             place.as_ref().map(|e|visitor.visit_expr(&**e));
716             visitor.visit_expr(&**subexpression)
717         }
718         ExprVec(ref subexpressions) => {
719             walk_exprs(visitor, subexpressions)
720         }
721         ExprRepeat(ref element, ref count) => {
722             visitor.visit_expr(&**element);
723             visitor.visit_expr(&**count)
724         }
725         ExprStruct(ref path, ref fields, ref optional_base) => {
726             visitor.visit_path(path, expression.id);
727             for field in fields {
728                 visitor.visit_expr(&*field.expr)
729             }
730             walk_expr_opt(visitor, optional_base)
731         }
732         ExprTup(ref subexpressions) => {
733             for subexpression in subexpressions {
734                 visitor.visit_expr(&**subexpression)
735             }
736         }
737         ExprCall(ref callee_expression, ref arguments) => {
738             for argument in arguments {
739                 visitor.visit_expr(&**argument)
740             }
741             visitor.visit_expr(&**callee_expression)
742         }
743         ExprMethodCall(_, ref types, ref arguments) => {
744             walk_exprs(visitor, arguments);
745             for typ in types {
746                 visitor.visit_ty(&**typ)
747             }
748         }
749         ExprBinary(_, ref left_expression, ref right_expression) => {
750             visitor.visit_expr(&**left_expression);
751             visitor.visit_expr(&**right_expression)
752         }
753         ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
754             visitor.visit_expr(&**subexpression)
755         }
756         ExprLit(_) => {}
757         ExprCast(ref subexpression, ref typ) => {
758             visitor.visit_expr(&**subexpression);
759             visitor.visit_ty(&**typ)
760         }
761         ExprIf(ref head_expression, ref if_block, ref optional_else) => {
762             visitor.visit_expr(&**head_expression);
763             visitor.visit_block(&**if_block);
764             walk_expr_opt(visitor, optional_else)
765         }
766         ExprWhile(ref subexpression, ref block, _) => {
767             visitor.visit_expr(&**subexpression);
768             visitor.visit_block(&**block)
769         }
770         ExprLoop(ref block, _) => visitor.visit_block(&**block),
771         ExprMatch(ref subexpression, ref arms, _) => {
772             visitor.visit_expr(&**subexpression);
773             for arm in arms {
774                 visitor.visit_arm(arm)
775             }
776         }
777         ExprClosure(_, ref function_declaration, ref body) => {
778             visitor.visit_fn(FnKind::Closure,
779                              &**function_declaration,
780                              &**body,
781                              expression.span,
782                              expression.id)
783         }
784         ExprBlock(ref block) => visitor.visit_block(&**block),
785         ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
786             visitor.visit_expr(&**right_hand_expression);
787             visitor.visit_expr(&**left_hand_expression)
788         }
789         ExprAssignOp(_, ref left_expression, ref right_expression) => {
790             visitor.visit_expr(&**right_expression);
791             visitor.visit_expr(&**left_expression)
792         }
793         ExprField(ref subexpression, _) => {
794             visitor.visit_expr(&**subexpression);
795         }
796         ExprTupField(ref subexpression, _) => {
797             visitor.visit_expr(&**subexpression);
798         }
799         ExprIndex(ref main_expression, ref index_expression) => {
800             visitor.visit_expr(&**main_expression);
801             visitor.visit_expr(&**index_expression)
802         }
803         ExprRange(ref start, ref end) => {
804             walk_expr_opt(visitor, start);
805             walk_expr_opt(visitor, end)
806         }
807         ExprPath(ref maybe_qself, ref path) => {
808             if let Some(ref qself) = *maybe_qself {
809                 visitor.visit_ty(&qself.ty);
810             }
811             visitor.visit_path(path, expression.id)
812         }
813         ExprBreak(_) | ExprAgain(_) => {}
814         ExprRet(ref optional_expression) => {
815             walk_expr_opt(visitor, optional_expression)
816         }
817         ExprParen(ref subexpression) => {
818             visitor.visit_expr(&**subexpression)
819         }
820         ExprInlineAsm(ref ia) => {
821             for input in &ia.inputs {
822                 let (_, ref input) = *input;
823                 visitor.visit_expr(&**input)
824             }
825             for output in &ia.outputs {
826                 let (_, ref output, _) = *output;
827                 visitor.visit_expr(&**output)
828             }
829         }
830     }
831
832     visitor.visit_expr_post(expression)
833 }
834
835 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
836     for pattern in &arm.pats {
837         visitor.visit_pat(&**pattern)
838     }
839     walk_expr_opt(visitor, &arm.guard);
840     visitor.visit_expr(&*arm.body);
841     for attr in &arm.attrs {
842         visitor.visit_attribute(attr);
843     }
844 }