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