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