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