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