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