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