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