]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/visit.rs
syntax: Use `ast::MacArgs` for attributes
[rust.git] / src / libsyntax / visit.rs
1 //! AST walker. Each overridden visit method has full control over what
2 //! happens with its node, it can do its own traversal of the node's children,
3 //! call `visit::walk_*` to apply the default traversal algorithm, or prevent
4 //! deeper traversal by doing nothing.
5 //!
6 //! Note: it is an important invariant that the default visitor walks the body
7 //! of a function in "execution order" (more concretely, reverse post-order
8 //! with respect to the CFG implied by the AST), meaning that if AST node A may
9 //! execute before AST node B, then A is visited first. The borrow checker in
10 //! particular relies on this property.
11 //!
12 //! Note: walking an AST before macro expansion is probably a bad idea. For
13 //! instance, a walker looking for item names in a module will miss all of
14 //! those that are created by the expansion of a macro.
15
16 use crate::ast::*;
17 use crate::token::Token;
18 use crate::tokenstream::{TokenTree, TokenStream};
19
20 use syntax_pos::Span;
21
22 #[derive(Copy, Clone)]
23 pub enum FnKind<'a> {
24     /// E.g., `fn foo()` or `extern "Abi" fn foo()`.
25     ItemFn(Ident, &'a FnHeader, &'a Visibility, &'a Block),
26
27     /// E.g., `fn foo(&self)`.
28     Method(Ident, &'a FnSig, Option<&'a Visibility>, &'a Block),
29
30     /// E.g., `|x, y| body`.
31     Closure(&'a Expr),
32 }
33
34 impl<'a> FnKind<'a> {
35     pub fn header(&self) -> Option<&'a FnHeader> {
36         match *self {
37             FnKind::ItemFn(_, header, _, _) => Some(header),
38             FnKind::Method(_, sig, _, _) => Some(&sig.header),
39             FnKind::Closure(_) => None,
40         }
41     }
42 }
43
44 /// Each method of the `Visitor` trait is a hook to be potentially
45 /// overridden. Each method's default implementation recursively visits
46 /// the substructure of the input via the corresponding `walk` method;
47 /// e.g., the `visit_mod` method by default calls `visit::walk_mod`.
48 ///
49 /// If you want to ensure that your code handles every variant
50 /// explicitly, you need to override each method. (And you also need
51 /// to monitor future changes to `Visitor` in case a new method with a
52 /// new default implementation gets introduced.)
53 pub trait Visitor<'ast>: Sized {
54     fn visit_name(&mut self, _span: Span, _name: Name) {
55         // Nothing to do.
56     }
57     fn visit_ident(&mut self, ident: Ident) {
58         walk_ident(self, ident);
59     }
60     fn visit_mod(&mut self, m: &'ast Mod, _s: Span, _attrs: &[Attribute], _n: NodeId) {
61         walk_mod(self, m);
62     }
63     fn visit_foreign_item(&mut self, i: &'ast ForeignItem) { walk_foreign_item(self, i) }
64     fn visit_global_asm(&mut self, ga: &'ast GlobalAsm) { walk_global_asm(self, ga) }
65     fn visit_item(&mut self, i: &'ast Item) { walk_item(self, i) }
66     fn visit_local(&mut self, l: &'ast Local) { walk_local(self, l) }
67     fn visit_block(&mut self, b: &'ast Block) { walk_block(self, b) }
68     fn visit_stmt(&mut self, s: &'ast Stmt) { walk_stmt(self, s) }
69     fn visit_param(&mut self, param: &'ast Param) { walk_param(self, param) }
70     fn visit_arm(&mut self, a: &'ast Arm) { walk_arm(self, a) }
71     fn visit_pat(&mut self, p: &'ast Pat) { walk_pat(self, p) }
72     fn visit_anon_const(&mut self, c: &'ast AnonConst) { walk_anon_const(self, c) }
73     fn visit_expr(&mut self, ex: &'ast Expr) { walk_expr(self, ex) }
74     fn visit_expr_post(&mut self, _ex: &'ast Expr) { }
75     fn visit_ty(&mut self, t: &'ast Ty) { walk_ty(self, t) }
76     fn visit_generic_param(&mut self, param: &'ast GenericParam) {
77         walk_generic_param(self, param)
78     }
79     fn visit_generics(&mut self, g: &'ast Generics) { walk_generics(self, g) }
80     fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
81         walk_where_predicate(self, p)
82     }
83     fn visit_fn(&mut self, fk: FnKind<'ast>, fd: &'ast FnDecl, s: Span, _: NodeId) {
84         walk_fn(self, fk, fd, s)
85     }
86     fn visit_trait_item(&mut self, ti: &'ast TraitItem) { walk_trait_item(self, ti) }
87     fn visit_impl_item(&mut self, ii: &'ast ImplItem) { walk_impl_item(self, ii) }
88     fn visit_trait_ref(&mut self, t: &'ast TraitRef) { walk_trait_ref(self, t) }
89     fn visit_param_bound(&mut self, bounds: &'ast GenericBound) {
90         walk_param_bound(self, bounds)
91     }
92     fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
93         walk_poly_trait_ref(self, t, m)
94     }
95     fn visit_variant_data(&mut self, s: &'ast VariantData) {
96         walk_struct_def(self, s)
97     }
98     fn visit_struct_field(&mut self, s: &'ast StructField) { walk_struct_field(self, s) }
99     fn visit_enum_def(&mut self, enum_definition: &'ast EnumDef,
100                       generics: &'ast Generics, item_id: NodeId, _: Span) {
101         walk_enum_def(self, enum_definition, generics, item_id)
102     }
103     fn visit_variant(&mut self, v: &'ast Variant) {
104         walk_variant(self, v)
105     }
106     fn visit_label(&mut self, label: &'ast Label) {
107         walk_label(self, label)
108     }
109     fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
110         walk_lifetime(self, lifetime)
111     }
112     fn visit_mac(&mut self, _mac: &'ast Mac) {
113         panic!("visit_mac disabled by default");
114         // N.B., see note about macros above.
115         // if you really want a visitor that
116         // works on macros, use this
117         // definition in your trait impl:
118         // visit::walk_mac(self, _mac)
119     }
120     fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
121         // Nothing to do
122     }
123     fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
124         walk_path(self, path)
125     }
126     fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
127         walk_use_tree(self, use_tree, id)
128     }
129     fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
130         walk_path_segment(self, path_span, path_segment)
131     }
132     fn visit_generic_args(&mut self, path_span: Span, generic_args: &'ast GenericArgs) {
133         walk_generic_args(self, path_span, generic_args)
134     }
135     fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
136         match generic_arg {
137             GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
138             GenericArg::Type(ty) => self.visit_ty(ty),
139             GenericArg::Const(ct) => self.visit_anon_const(ct),
140         }
141     }
142     fn visit_assoc_ty_constraint(&mut self, constraint: &'ast AssocTyConstraint) {
143         walk_assoc_ty_constraint(self, constraint)
144     }
145     fn visit_attribute(&mut self, attr: &'ast Attribute) {
146         walk_attribute(self, attr)
147     }
148     fn visit_tt(&mut self, tt: TokenTree) {
149         walk_tt(self, tt)
150     }
151     fn visit_tts(&mut self, tts: TokenStream) {
152         walk_tts(self, tts)
153     }
154     fn visit_token(&mut self, _t: Token) {}
155     // FIXME: add `visit_interpolated` and `walk_interpolated`
156     fn visit_vis(&mut self, vis: &'ast Visibility) {
157         walk_vis(self, vis)
158     }
159     fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FunctionRetTy) {
160         walk_fn_ret_ty(self, ret_ty)
161     }
162     fn visit_fn_header(&mut self, _header: &'ast FnHeader) {
163         // Nothing to do
164     }
165     fn visit_field(&mut self, f: &'ast Field) {
166         walk_field(self, f)
167     }
168     fn visit_field_pattern(&mut self, fp: &'ast FieldPat) {
169         walk_field_pattern(self, fp)
170     }
171 }
172
173 #[macro_export]
174 macro_rules! walk_list {
175     ($visitor: expr, $method: ident, $list: expr) => {
176         for elem in $list {
177             $visitor.$method(elem)
178         }
179     };
180     ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
181         for elem in $list {
182             $visitor.$method(elem, $($extra_args,)*)
183         }
184     }
185 }
186
187 pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, ident: Ident) {
188     visitor.visit_name(ident.span, ident.name);
189 }
190
191 pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
192     visitor.visit_mod(&krate.module, krate.span, &krate.attrs, CRATE_NODE_ID);
193     walk_list!(visitor, visit_attribute, &krate.attrs);
194 }
195
196 pub fn walk_mod<'a, V: Visitor<'a>>(visitor: &mut V, module: &'a Mod) {
197     walk_list!(visitor, visit_item, &module.items);
198 }
199
200 pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
201     for attr in local.attrs.iter() {
202         visitor.visit_attribute(attr);
203     }
204     visitor.visit_pat(&local.pat);
205     walk_list!(visitor, visit_ty, &local.ty);
206     walk_list!(visitor, visit_expr, &local.init);
207 }
208
209 pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, label: &'a Label) {
210     visitor.visit_ident(label.ident);
211 }
212
213 pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
214     visitor.visit_ident(lifetime.ident);
215 }
216
217 pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V,
218                                   trait_ref: &'a PolyTraitRef,
219                                   _: &TraitBoundModifier)
220     where V: Visitor<'a>,
221 {
222     walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
223     visitor.visit_trait_ref(&trait_ref.trait_ref);
224 }
225
226 pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) {
227     visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
228 }
229
230 pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
231     visitor.visit_vis(&item.vis);
232     visitor.visit_ident(item.ident);
233     match item.kind {
234         ItemKind::ExternCrate(orig_name) => {
235             if let Some(orig_name) = orig_name {
236                 visitor.visit_name(item.span, orig_name);
237             }
238         }
239         ItemKind::Use(ref use_tree) => {
240             visitor.visit_use_tree(use_tree, item.id, false)
241         }
242         ItemKind::Static(ref typ, _, ref expr) |
243         ItemKind::Const(ref typ, ref expr) => {
244             visitor.visit_ty(typ);
245             visitor.visit_expr(expr);
246         }
247         ItemKind::Fn(ref sig, ref generics, ref body) => {
248             visitor.visit_generics(generics);
249             visitor.visit_fn_header(&sig.header);
250             visitor.visit_fn(FnKind::ItemFn(item.ident, &sig.header, &item.vis, body),
251                              &sig.decl,
252                              item.span,
253                              item.id)
254         }
255         ItemKind::Mod(ref module) => {
256             visitor.visit_mod(module, item.span, &item.attrs, item.id)
257         }
258         ItemKind::ForeignMod(ref foreign_module) => {
259             walk_list!(visitor, visit_foreign_item, &foreign_module.items);
260         }
261         ItemKind::GlobalAsm(ref ga) => visitor.visit_global_asm(ga),
262         ItemKind::TyAlias(ref typ, ref generics) => {
263             visitor.visit_ty(typ);
264             visitor.visit_generics(generics)
265         }
266         ItemKind::Enum(ref enum_definition, ref generics) => {
267             visitor.visit_generics(generics);
268             visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
269         }
270         ItemKind::Impl(_, _, _,
271                  ref generics,
272                  ref opt_trait_reference,
273                  ref typ,
274                  ref impl_items) => {
275             visitor.visit_generics(generics);
276             walk_list!(visitor, visit_trait_ref, opt_trait_reference);
277             visitor.visit_ty(typ);
278             walk_list!(visitor, visit_impl_item, impl_items);
279         }
280         ItemKind::Struct(ref struct_definition, ref generics) |
281         ItemKind::Union(ref struct_definition, ref generics) => {
282             visitor.visit_generics(generics);
283             visitor.visit_variant_data(struct_definition);
284         }
285         ItemKind::Trait(.., ref generics, ref bounds, ref methods) => {
286             visitor.visit_generics(generics);
287             walk_list!(visitor, visit_param_bound, bounds);
288             walk_list!(visitor, visit_trait_item, methods);
289         }
290         ItemKind::TraitAlias(ref generics, ref bounds) => {
291             visitor.visit_generics(generics);
292             walk_list!(visitor, visit_param_bound, bounds);
293         }
294         ItemKind::Mac(ref mac) => visitor.visit_mac(mac),
295         ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
296     }
297     walk_list!(visitor, visit_attribute, &item.attrs);
298 }
299
300 pub fn walk_enum_def<'a, V: Visitor<'a>>(
301     visitor: &mut V,
302     enum_definition: &'a EnumDef,
303     _: &'a Generics,
304     _: NodeId,
305 ) {
306     walk_list!(visitor, visit_variant, &enum_definition.variants);
307 }
308
309 pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant)
310     where V: Visitor<'a>,
311 {
312     visitor.visit_ident(variant.ident);
313     visitor.visit_vis(&variant.vis);
314     visitor.visit_variant_data(&variant.data);
315     walk_list!(visitor, visit_anon_const, &variant.disr_expr);
316     walk_list!(visitor, visit_attribute, &variant.attrs);
317 }
318
319 pub fn walk_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a Field) {
320     visitor.visit_expr(&f.expr);
321     visitor.visit_ident(f.ident);
322     walk_list!(visitor, visit_attribute, f.attrs.iter());
323 }
324
325 pub fn walk_field_pattern<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a FieldPat) {
326     visitor.visit_ident(fp.ident);
327     visitor.visit_pat(&fp.pat);
328     walk_list!(visitor, visit_attribute, fp.attrs.iter());
329 }
330
331 pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
332     match typ.kind {
333         TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => {
334             visitor.visit_ty(ty)
335         }
336         TyKind::Ptr(ref mutable_type) => {
337             visitor.visit_ty(&mutable_type.ty)
338         }
339         TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
340             walk_list!(visitor, visit_lifetime, opt_lifetime);
341             visitor.visit_ty(&mutable_type.ty)
342         }
343         TyKind::Tup(ref tuple_element_types) => {
344             walk_list!(visitor, visit_ty, tuple_element_types);
345         }
346         TyKind::BareFn(ref function_declaration) => {
347             walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
348             walk_fn_decl(visitor, &function_declaration.decl);
349         }
350         TyKind::Path(ref maybe_qself, ref path) => {
351             if let Some(ref qself) = *maybe_qself {
352                 visitor.visit_ty(&qself.ty);
353             }
354             visitor.visit_path(path, typ.id);
355         }
356         TyKind::Array(ref ty, ref length) => {
357             visitor.visit_ty(ty);
358             visitor.visit_anon_const(length)
359         }
360         TyKind::TraitObject(ref bounds, ..) |
361         TyKind::ImplTrait(_, ref bounds) => {
362             walk_list!(visitor, visit_param_bound, bounds);
363         }
364         TyKind::Typeof(ref expression) => {
365             visitor.visit_anon_const(expression)
366         }
367         TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
368         TyKind::Mac(ref mac) => {
369             visitor.visit_mac(mac)
370         }
371         TyKind::Never |
372         TyKind::CVarArgs => {}
373     }
374 }
375
376 pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
377     for segment in &path.segments {
378         visitor.visit_path_segment(path.span, segment);
379     }
380 }
381
382 pub fn walk_use_tree<'a, V: Visitor<'a>>(
383     visitor: &mut V, use_tree: &'a UseTree, id: NodeId,
384 ) {
385     visitor.visit_path(&use_tree.prefix, id);
386     match use_tree.kind {
387         UseTreeKind::Simple(rename, ..) => {
388             // The extra IDs are handled during HIR lowering.
389             if let Some(rename) = rename {
390                 visitor.visit_ident(rename);
391             }
392         }
393         UseTreeKind::Glob => {},
394         UseTreeKind::Nested(ref use_trees) => {
395             for &(ref nested_tree, nested_id) in use_trees {
396                 visitor.visit_use_tree(nested_tree, nested_id, true);
397             }
398         }
399     }
400 }
401
402 pub fn walk_path_segment<'a, V: Visitor<'a>>(visitor: &mut V,
403                                              path_span: Span,
404                                              segment: &'a PathSegment) {
405     visitor.visit_ident(segment.ident);
406     if let Some(ref args) = segment.args {
407         visitor.visit_generic_args(path_span, args);
408     }
409 }
410
411 pub fn walk_generic_args<'a, V>(visitor: &mut V,
412                                 _path_span: Span,
413                                 generic_args: &'a GenericArgs)
414     where V: Visitor<'a>,
415 {
416     match *generic_args {
417         GenericArgs::AngleBracketed(ref data) => {
418             walk_list!(visitor, visit_generic_arg, &data.args);
419             walk_list!(visitor, visit_assoc_ty_constraint, &data.constraints);
420         }
421         GenericArgs::Parenthesized(ref data) => {
422             walk_list!(visitor, visit_ty, &data.inputs);
423             walk_list!(visitor, visit_ty, &data.output);
424         }
425     }
426 }
427
428 pub fn walk_assoc_ty_constraint<'a, V: Visitor<'a>>(visitor: &mut V,
429                                                     constraint: &'a AssocTyConstraint) {
430     visitor.visit_ident(constraint.ident);
431     match constraint.kind {
432         AssocTyConstraintKind::Equality { ref ty } => {
433             visitor.visit_ty(ty);
434         }
435         AssocTyConstraintKind::Bound { ref bounds } => {
436             walk_list!(visitor, visit_param_bound, bounds);
437         }
438     }
439 }
440
441 pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
442     match pattern.kind {
443         PatKind::TupleStruct(ref path, ref elems) => {
444             visitor.visit_path(path, pattern.id);
445             walk_list!(visitor, visit_pat, elems);
446         }
447         PatKind::Path(ref opt_qself, ref path) => {
448             if let Some(ref qself) = *opt_qself {
449                 visitor.visit_ty(&qself.ty);
450             }
451             visitor.visit_path(path, pattern.id)
452         }
453         PatKind::Struct(ref path, ref fields, _) => {
454             visitor.visit_path(path, pattern.id);
455             walk_list!(visitor, visit_field_pattern, fields);
456         }
457         PatKind::Box(ref subpattern) |
458         PatKind::Ref(ref subpattern, _) |
459         PatKind::Paren(ref subpattern) => {
460             visitor.visit_pat(subpattern)
461         }
462         PatKind::Ident(_, ident, ref optional_subpattern) => {
463             visitor.visit_ident(ident);
464             walk_list!(visitor, visit_pat, optional_subpattern);
465         }
466         PatKind::Lit(ref expression) => visitor.visit_expr(expression),
467         PatKind::Range(ref lower_bound, ref upper_bound, _) => {
468             visitor.visit_expr(lower_bound);
469             visitor.visit_expr(upper_bound);
470         }
471         PatKind::Wild | PatKind::Rest => {},
472         PatKind::Tuple(ref elems)
473         | PatKind::Slice(ref elems)
474         | PatKind::Or(ref elems) => {
475             walk_list!(visitor, visit_pat, elems);
476         }
477         PatKind::Mac(ref mac) => visitor.visit_mac(mac),
478     }
479 }
480
481 pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, foreign_item: &'a ForeignItem) {
482     visitor.visit_vis(&foreign_item.vis);
483     visitor.visit_ident(foreign_item.ident);
484
485     match foreign_item.kind {
486         ForeignItemKind::Fn(ref function_declaration, ref generics) => {
487             walk_fn_decl(visitor, function_declaration);
488             visitor.visit_generics(generics)
489         }
490         ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
491         ForeignItemKind::Ty => (),
492         ForeignItemKind::Macro(ref mac) => visitor.visit_mac(mac),
493     }
494
495     walk_list!(visitor, visit_attribute, &foreign_item.attrs);
496 }
497
498 pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) {
499     // Empty!
500 }
501
502 pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
503     match *bound {
504         GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
505         GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
506     }
507 }
508
509 pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
510     visitor.visit_ident(param.ident);
511     walk_list!(visitor, visit_attribute, param.attrs.iter());
512     walk_list!(visitor, visit_param_bound, &param.bounds);
513     match param.kind {
514         GenericParamKind::Lifetime => (),
515         GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
516         GenericParamKind::Const { ref ty, .. } => visitor.visit_ty(ty),
517     }
518 }
519
520 pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
521     walk_list!(visitor, visit_generic_param, &generics.params);
522     walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
523 }
524
525 pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
526     match *predicate {
527         WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
528                                                            ref bounds,
529                                                            ref bound_generic_params,
530                                                            ..}) => {
531             visitor.visit_ty(bounded_ty);
532             walk_list!(visitor, visit_param_bound, bounds);
533             walk_list!(visitor, visit_generic_param, bound_generic_params);
534         }
535         WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
536                                                              ref bounds,
537                                                              ..}) => {
538             visitor.visit_lifetime(lifetime);
539             walk_list!(visitor, visit_param_bound, bounds);
540         }
541         WherePredicate::EqPredicate(WhereEqPredicate{ref lhs_ty,
542                                                      ref rhs_ty,
543                                                      ..}) => {
544             visitor.visit_ty(lhs_ty);
545             visitor.visit_ty(rhs_ty);
546         }
547     }
548 }
549
550 pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FunctionRetTy) {
551     if let FunctionRetTy::Ty(ref output_ty) = *ret_ty {
552         visitor.visit_ty(output_ty)
553     }
554 }
555
556 pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
557     for param in &function_declaration.inputs {
558         visitor.visit_param(param);
559     }
560     visitor.visit_fn_ret_ty(&function_declaration.output);
561 }
562
563 pub fn walk_fn<'a, V>(visitor: &mut V, kind: FnKind<'a>, declaration: &'a FnDecl, _span: Span)
564     where V: Visitor<'a>,
565 {
566     match kind {
567         FnKind::ItemFn(_, header, _, body) => {
568             visitor.visit_fn_header(header);
569             walk_fn_decl(visitor, declaration);
570             visitor.visit_block(body);
571         }
572         FnKind::Method(_, sig, _, body) => {
573             visitor.visit_fn_header(&sig.header);
574             walk_fn_decl(visitor, declaration);
575             visitor.visit_block(body);
576         }
577         FnKind::Closure(body) => {
578             walk_fn_decl(visitor, declaration);
579             visitor.visit_expr(body);
580         }
581     }
582 }
583
584 pub fn walk_trait_item<'a, V: Visitor<'a>>(visitor: &mut V, trait_item: &'a TraitItem) {
585     visitor.visit_vis(&trait_item.vis);
586     visitor.visit_ident(trait_item.ident);
587     walk_list!(visitor, visit_attribute, &trait_item.attrs);
588     visitor.visit_generics(&trait_item.generics);
589     match trait_item.kind {
590         TraitItemKind::Const(ref ty, ref default) => {
591             visitor.visit_ty(ty);
592             walk_list!(visitor, visit_expr, default);
593         }
594         TraitItemKind::Method(ref sig, None) => {
595             visitor.visit_fn_header(&sig.header);
596             walk_fn_decl(visitor, &sig.decl);
597         }
598         TraitItemKind::Method(ref sig, Some(ref body)) => {
599             visitor.visit_fn(FnKind::Method(trait_item.ident, sig, None, body),
600                              &sig.decl, trait_item.span, trait_item.id);
601         }
602         TraitItemKind::Type(ref bounds, ref default) => {
603             walk_list!(visitor, visit_param_bound, bounds);
604             walk_list!(visitor, visit_ty, default);
605         }
606         TraitItemKind::Macro(ref mac) => {
607             visitor.visit_mac(mac);
608         }
609     }
610 }
611
612 pub fn walk_impl_item<'a, V: Visitor<'a>>(visitor: &mut V, impl_item: &'a ImplItem) {
613     visitor.visit_vis(&impl_item.vis);
614     visitor.visit_ident(impl_item.ident);
615     walk_list!(visitor, visit_attribute, &impl_item.attrs);
616     visitor.visit_generics(&impl_item.generics);
617     match impl_item.kind {
618         ImplItemKind::Const(ref ty, ref expr) => {
619             visitor.visit_ty(ty);
620             visitor.visit_expr(expr);
621         }
622         ImplItemKind::Method(ref sig, ref body) => {
623             visitor.visit_fn(FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis), body),
624                              &sig.decl, impl_item.span, impl_item.id);
625         }
626         ImplItemKind::TyAlias(ref ty) => {
627             visitor.visit_ty(ty);
628         }
629         ImplItemKind::Macro(ref mac) => {
630             visitor.visit_mac(mac);
631         }
632     }
633 }
634
635 pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
636     walk_list!(visitor, visit_struct_field, struct_definition.fields());
637 }
638
639 pub fn walk_struct_field<'a, V: Visitor<'a>>(visitor: &mut V, struct_field: &'a StructField) {
640     visitor.visit_vis(&struct_field.vis);
641     if let Some(ident) = struct_field.ident {
642         visitor.visit_ident(ident);
643     }
644     visitor.visit_ty(&struct_field.ty);
645     walk_list!(visitor, visit_attribute, &struct_field.attrs);
646 }
647
648 pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
649     walk_list!(visitor, visit_stmt, &block.stmts);
650 }
651
652 pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
653     match statement.kind {
654         StmtKind::Local(ref local) => visitor.visit_local(local),
655         StmtKind::Item(ref item) => visitor.visit_item(item),
656         StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
657             visitor.visit_expr(expression)
658         }
659         StmtKind::Mac(ref mac) => {
660             let (ref mac, _, ref attrs) = **mac;
661             visitor.visit_mac(mac);
662             for attr in attrs.iter() {
663                 visitor.visit_attribute(attr);
664             }
665         }
666     }
667 }
668
669 pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a Mac) {
670     visitor.visit_path(&mac.path, DUMMY_NODE_ID);
671 }
672
673 pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
674     visitor.visit_expr(&constant.value);
675 }
676
677 pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
678     walk_list!(visitor, visit_attribute, expression.attrs.iter());
679
680     match expression.kind {
681         ExprKind::Box(ref subexpression) => {
682             visitor.visit_expr(subexpression)
683         }
684         ExprKind::Array(ref subexpressions) => {
685             walk_list!(visitor, visit_expr, subexpressions);
686         }
687         ExprKind::Repeat(ref element, ref count) => {
688             visitor.visit_expr(element);
689             visitor.visit_anon_const(count)
690         }
691         ExprKind::Struct(ref path, ref fields, ref optional_base) => {
692             visitor.visit_path(path, expression.id);
693             walk_list!(visitor, visit_field, fields);
694             walk_list!(visitor, visit_expr, optional_base);
695         }
696         ExprKind::Tup(ref subexpressions) => {
697             walk_list!(visitor, visit_expr, subexpressions);
698         }
699         ExprKind::Call(ref callee_expression, ref arguments) => {
700             visitor.visit_expr(callee_expression);
701             walk_list!(visitor, visit_expr, arguments);
702         }
703         ExprKind::MethodCall(ref segment, ref arguments) => {
704             visitor.visit_path_segment(expression.span, segment);
705             walk_list!(visitor, visit_expr, arguments);
706         }
707         ExprKind::Binary(_, ref left_expression, ref right_expression) => {
708             visitor.visit_expr(left_expression);
709             visitor.visit_expr(right_expression)
710         }
711         ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
712             visitor.visit_expr(subexpression)
713         }
714         ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
715             visitor.visit_expr(subexpression);
716             visitor.visit_ty(typ)
717         }
718         ExprKind::Let(ref pat, ref scrutinee) => {
719             visitor.visit_pat(pat);
720             visitor.visit_expr(scrutinee);
721         }
722         ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
723             visitor.visit_expr(head_expression);
724             visitor.visit_block(if_block);
725             walk_list!(visitor, visit_expr, optional_else);
726         }
727         ExprKind::While(ref subexpression, ref block, ref opt_label) => {
728             walk_list!(visitor, visit_label, opt_label);
729             visitor.visit_expr(subexpression);
730             visitor.visit_block(block);
731         }
732         ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
733             walk_list!(visitor, visit_label, opt_label);
734             visitor.visit_pat(pattern);
735             visitor.visit_expr(subexpression);
736             visitor.visit_block(block);
737         }
738         ExprKind::Loop(ref block, ref opt_label) => {
739             walk_list!(visitor, visit_label, opt_label);
740             visitor.visit_block(block);
741         }
742         ExprKind::Match(ref subexpression, ref arms) => {
743             visitor.visit_expr(subexpression);
744             walk_list!(visitor, visit_arm, arms);
745         }
746         ExprKind::Closure(_, _, _, ref function_declaration, ref body, _decl_span) => {
747             visitor.visit_fn(FnKind::Closure(body),
748                              function_declaration,
749                              expression.span,
750                              expression.id)
751         }
752         ExprKind::Block(ref block, ref opt_label) => {
753             walk_list!(visitor, visit_label, opt_label);
754             visitor.visit_block(block);
755         }
756         ExprKind::Async(_, _, ref body) => {
757             visitor.visit_block(body);
758         }
759         ExprKind::Await(ref expr) => visitor.visit_expr(expr),
760         ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
761             visitor.visit_expr(left_hand_expression);
762             visitor.visit_expr(right_hand_expression);
763         }
764         ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
765             visitor.visit_expr(left_expression);
766             visitor.visit_expr(right_expression);
767         }
768         ExprKind::Field(ref subexpression, ident) => {
769             visitor.visit_expr(subexpression);
770             visitor.visit_ident(ident);
771         }
772         ExprKind::Index(ref main_expression, ref index_expression) => {
773             visitor.visit_expr(main_expression);
774             visitor.visit_expr(index_expression)
775         }
776         ExprKind::Range(ref start, ref end, _) => {
777             walk_list!(visitor, visit_expr, start);
778             walk_list!(visitor, visit_expr, end);
779         }
780         ExprKind::Path(ref maybe_qself, ref path) => {
781             if let Some(ref qself) = *maybe_qself {
782                 visitor.visit_ty(&qself.ty);
783             }
784             visitor.visit_path(path, expression.id)
785         }
786         ExprKind::Break(ref opt_label, ref opt_expr) => {
787             walk_list!(visitor, visit_label, opt_label);
788             walk_list!(visitor, visit_expr, opt_expr);
789         }
790         ExprKind::Continue(ref opt_label) => {
791             walk_list!(visitor, visit_label, opt_label);
792         }
793         ExprKind::Ret(ref optional_expression) => {
794             walk_list!(visitor, visit_expr, optional_expression);
795         }
796         ExprKind::Mac(ref mac) => visitor.visit_mac(mac),
797         ExprKind::Paren(ref subexpression) => {
798             visitor.visit_expr(subexpression)
799         }
800         ExprKind::InlineAsm(ref ia) => {
801             for &(_, ref input) in &ia.inputs {
802                 visitor.visit_expr(input)
803             }
804             for output in &ia.outputs {
805                 visitor.visit_expr(&output.expr)
806             }
807         }
808         ExprKind::Yield(ref optional_expression) => {
809             walk_list!(visitor, visit_expr, optional_expression);
810         }
811         ExprKind::Try(ref subexpression) => {
812             visitor.visit_expr(subexpression)
813         }
814         ExprKind::TryBlock(ref body) => {
815             visitor.visit_block(body)
816         }
817         ExprKind::Lit(_) | ExprKind::Err => {}
818     }
819
820     visitor.visit_expr_post(expression)
821 }
822
823 pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {
824     walk_list!(visitor, visit_attribute, param.attrs.iter());
825     visitor.visit_pat(&param.pat);
826     visitor.visit_ty(&param.ty);
827 }
828
829 pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
830     visitor.visit_pat(&arm.pat);
831     walk_list!(visitor, visit_expr, &arm.guard);
832     visitor.visit_expr(&arm.body);
833     walk_list!(visitor, visit_attribute, &arm.attrs);
834 }
835
836 pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
837     if let VisibilityKind::Restricted { ref path, id } = vis.node {
838         visitor.visit_path(path, id);
839     }
840 }
841
842 pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
843     match attr.kind {
844         AttrKind::Normal(ref item) => walk_mac_args(visitor, &item.args),
845         AttrKind::DocComment(_) => {}
846     }
847 }
848
849 pub fn walk_mac_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a MacArgs) {
850     match args {
851         MacArgs::Empty => {}
852         MacArgs::Delimited(_dspan, _delim, tokens) => visitor.visit_tts(tokens.clone()),
853         MacArgs::Eq(_eq_span, tokens) => visitor.visit_tts(tokens.clone()),
854     }
855 }
856
857 pub fn walk_tt<'a, V: Visitor<'a>>(visitor: &mut V, tt: TokenTree) {
858     match tt {
859         TokenTree::Token(token) => visitor.visit_token(token),
860         TokenTree::Delimited(_, _, tts) => visitor.visit_tts(tts),
861     }
862 }
863
864 pub fn walk_tts<'a, V: Visitor<'a>>(visitor: &mut V, tts: TokenStream) {
865     for tt in tts.trees() {
866         visitor.visit_tt(tt);
867     }
868 }