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