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