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