1 // Copyright 2012-2014 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.
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.
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.
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.
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.
26 pub use self::FnKind::*;
33 use owned_slice::OwnedSlice;
35 #[derive(Copy, Clone)]
37 /// fn foo() or extern "Abi" fn foo()
38 FkItemFn(Ident, &'a Generics, Unsafety, Constness, Abi, Visibility),
41 FkMethod(Ident, &'a MethodSig, Option<Visibility>),
48 /// Each method of the Visitor trait is a hook to be potentially
49 /// overridden. Each method's default implementation recursively visits
50 /// the substructure of the input via the corresponding `walk` method;
51 /// e.g. the `visit_mod` method by default calls `visit::walk_mod`.
53 /// If you want to ensure that your code handles every variant
54 /// explicitly, you need to override each method. (And you also need
55 /// to monitor future changes to `Visitor` in case a new method with a
56 /// new default implementation gets introduced.)
57 pub trait Visitor<'v> : Sized {
58 fn visit_name(&mut self, _span: Span, _name: Name) {
61 fn visit_ident(&mut self, span: Span, ident: Ident) {
62 self.visit_name(span, ident.name);
64 fn visit_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) { walk_mod(self, m) }
65 fn visit_foreign_item(&mut self, i: &'v ForeignItem) { walk_foreign_item(self, i) }
66 fn visit_item(&mut self, i: &'v Item) { walk_item(self, i) }
67 fn visit_local(&mut self, l: &'v Local) { walk_local(self, l) }
68 fn visit_block(&mut self, b: &'v Block) { walk_block(self, b) }
69 fn visit_stmt(&mut self, s: &'v Stmt) { walk_stmt(self, s) }
70 fn visit_arm(&mut self, a: &'v Arm) { walk_arm(self, a) }
71 fn visit_pat(&mut self, p: &'v Pat) { walk_pat(self, p) }
72 fn visit_decl(&mut self, d: &'v Decl) { walk_decl(self, d) }
73 fn visit_expr(&mut self, ex: &'v Expr) { walk_expr(self, ex) }
74 fn visit_expr_post(&mut self, _ex: &'v Expr) { }
75 fn visit_ty(&mut self, t: &'v Ty) { walk_ty(self, t) }
76 fn visit_generics(&mut self, g: &'v Generics) { walk_generics(self, g) }
77 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: &'v Block, s: Span, _: NodeId) {
78 walk_fn(self, fk, fd, b, s)
80 fn visit_trait_item(&mut self, ti: &'v TraitItem) { walk_trait_item(self, ti) }
81 fn visit_impl_item(&mut self, ii: &'v ImplItem) { walk_impl_item(self, ii) }
82 fn visit_trait_ref(&mut self, t: &'v TraitRef) { walk_trait_ref(self, t) }
83 fn visit_ty_param_bound(&mut self, bounds: &'v TyParamBound) {
84 walk_ty_param_bound(self, bounds)
86 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: &'v TraitBoundModifier) {
87 walk_poly_trait_ref(self, t, m)
89 fn visit_struct_def(&mut self, s: &'v StructDef, _: Ident, _: &'v Generics, _: NodeId) {
90 walk_struct_def(self, s)
92 fn visit_struct_field(&mut self, s: &'v StructField) { walk_struct_field(self, s) }
93 fn visit_enum_def(&mut self, enum_definition: &'v EnumDef,
94 generics: &'v Generics) {
95 walk_enum_def(self, enum_definition, generics)
98 fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics) { walk_variant(self, v, g) }
100 /// Visits an optional reference to a lifetime. The `span` is the span of some surrounding
101 /// reference should opt_lifetime be None.
102 fn visit_opt_lifetime_ref(&mut self,
104 opt_lifetime: &'v Option<Lifetime>) {
105 match *opt_lifetime {
106 Some(ref l) => self.visit_lifetime_ref(l),
110 fn visit_lifetime_bound(&mut self, lifetime: &'v Lifetime) {
111 walk_lifetime_bound(self, lifetime)
113 fn visit_lifetime_ref(&mut self, lifetime: &'v Lifetime) {
114 walk_lifetime_ref(self, lifetime)
116 fn visit_lifetime_def(&mut self, lifetime: &'v LifetimeDef) {
117 walk_lifetime_def(self, lifetime)
119 fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
120 walk_explicit_self(self, es)
122 fn visit_mac(&mut self, _mac: &'v Mac) {
123 panic!("visit_mac disabled by default");
124 // NB: see note about macros above.
125 // if you really want a visitor that
126 // works on macros, use this
127 // definition in your trait impl:
128 // visit::walk_mac(self, _mac)
130 fn visit_path(&mut self, path: &'v Path, _id: ast::NodeId) {
131 walk_path(self, path)
133 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
134 walk_path_segment(self, path_span, path_segment)
136 fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'v PathParameters) {
137 walk_path_parameters(self, path_span, path_parameters)
139 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
140 walk_assoc_type_binding(self, type_binding)
142 fn visit_attribute(&mut self, _attr: &'v Attribute) {}
145 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
146 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
147 for attr in &krate.attrs {
148 visitor.visit_attribute(attr);
152 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
153 for item in &module.items {
154 visitor.visit_item(&**item)
158 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
159 visitor.visit_pat(&*local.pat);
160 walk_ty_opt(visitor, &local.ty);
161 walk_expr_opt(visitor, &local.init);
164 pub fn walk_lifetime_def<'v, V: Visitor<'v>>(visitor: &mut V,
165 lifetime_def: &'v LifetimeDef) {
166 visitor.visit_name(lifetime_def.lifetime.span, lifetime_def.lifetime.name);
167 for bound in &lifetime_def.bounds {
168 visitor.visit_lifetime_bound(bound);
172 pub fn walk_lifetime_bound<'v, V: Visitor<'v>>(visitor: &mut V,
173 lifetime_ref: &'v Lifetime) {
174 visitor.visit_lifetime_ref(lifetime_ref)
177 pub fn walk_lifetime_ref<'v, V: Visitor<'v>>(visitor: &mut V,
178 lifetime_ref: &'v Lifetime) {
179 visitor.visit_name(lifetime_ref.span, lifetime_ref.name)
182 pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V,
183 explicit_self: &'v ExplicitSelf) {
184 match explicit_self.node {
185 SelfStatic | SelfValue(_) => {},
186 SelfRegion(ref lifetime, _, _) => {
187 visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime)
189 SelfExplicit(ref typ, _) => visitor.visit_ty(&**typ),
193 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
194 trait_ref: &'v PolyTraitRef,
195 _modifier: &'v TraitBoundModifier)
198 walk_lifetime_decls_helper(visitor, &trait_ref.bound_lifetimes);
199 visitor.visit_trait_ref(&trait_ref.trait_ref);
202 pub fn walk_trait_ref<'v,V>(visitor: &mut V,
203 trait_ref: &'v TraitRef)
206 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
209 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
210 visitor.visit_ident(item.span, item.ident);
212 ItemExternCrate(..) => {}
215 ViewPathSimple(ident, ref path) => {
216 visitor.visit_ident(vp.span, ident);
217 visitor.visit_path(path, item.id);
219 ViewPathGlob(ref path) => {
220 visitor.visit_path(path, item.id);
222 ViewPathList(ref prefix, ref list) => {
225 PathListIdent { name, rename, .. } => {
226 visitor.visit_ident(id.span, name);
227 if let Some(ident) = rename {
228 visitor.visit_ident(id.span, ident);
231 PathListMod { rename, .. } => {
232 if let Some(ident) = rename {
233 visitor.visit_ident(id.span, ident);
239 // Note that the `prefix` here is not a complete
240 // path, so we don't use `visit_path`.
241 walk_path(visitor, prefix);
245 ItemStatic(ref typ, _, ref expr) |
246 ItemConst(ref typ, ref expr) => {
247 visitor.visit_ty(&**typ);
248 visitor.visit_expr(&**expr);
250 ItemFn(ref declaration, unsafety, constness, abi, ref generics, ref body) => {
251 visitor.visit_fn(FkItemFn(item.ident, generics, unsafety,
252 constness, abi, item.vis),
258 ItemMod(ref module) => {
259 visitor.visit_mod(module, item.span, item.id)
261 ItemForeignMod(ref foreign_module) => {
262 for foreign_item in &foreign_module.items {
263 visitor.visit_foreign_item(&**foreign_item)
266 ItemTy(ref typ, ref type_parameters) => {
267 visitor.visit_ty(&**typ);
268 visitor.visit_generics(type_parameters)
270 ItemEnum(ref enum_definition, ref type_parameters) => {
271 visitor.visit_generics(type_parameters);
272 visitor.visit_enum_def(enum_definition, type_parameters)
274 ItemDefaultImpl(_, ref trait_ref) => {
275 visitor.visit_trait_ref(trait_ref)
282 visitor.visit_generics(type_parameters);
283 match *trait_reference {
284 Some(ref trait_reference) => visitor.visit_trait_ref(trait_reference),
287 visitor.visit_ty(&**typ);
288 for impl_item in impl_items {
289 visitor.visit_impl_item(impl_item);
292 ItemStruct(ref struct_definition, ref generics) => {
293 visitor.visit_generics(generics);
294 visitor.visit_struct_def(&**struct_definition,
299 ItemTrait(_, ref generics, ref bounds, ref methods) => {
300 visitor.visit_generics(generics);
301 walk_ty_param_bounds_helper(visitor, bounds);
302 for method in methods {
303 visitor.visit_trait_item(method)
306 ItemMac(ref mac) => visitor.visit_mac(mac),
308 for attr in &item.attrs {
309 visitor.visit_attribute(attr);
313 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
314 enum_definition: &'v EnumDef,
315 generics: &'v Generics) {
316 for variant in &enum_definition.variants {
317 visitor.visit_variant(&**variant, generics);
321 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
322 variant: &'v Variant,
323 generics: &'v Generics) {
324 visitor.visit_ident(variant.span, variant.node.name);
326 match variant.node.kind {
327 TupleVariantKind(ref variant_arguments) => {
328 for variant_argument in variant_arguments {
329 visitor.visit_ty(&*variant_argument.ty)
332 StructVariantKind(ref struct_definition) => {
333 visitor.visit_struct_def(&**struct_definition,
339 match variant.node.disr_expr {
340 Some(ref expr) => visitor.visit_expr(&**expr),
343 for attr in &variant.node.attrs {
344 visitor.visit_attribute(attr);
348 pub fn skip_ty<'v, V: Visitor<'v>>(_: &mut V, _: &'v Ty) {
352 pub fn walk_ty_opt<'v, V: Visitor<'v>>(visitor: &mut V, optional_type: &'v Option<P<Ty>>) {
353 match *optional_type {
354 Some(ref ty) => visitor.visit_ty(&**ty),
359 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
361 TyVec(ref ty) | TyParen(ref ty) => {
362 visitor.visit_ty(&**ty)
364 TyPtr(ref mutable_type) => {
365 visitor.visit_ty(&*mutable_type.ty)
367 TyRptr(ref lifetime, ref mutable_type) => {
368 visitor.visit_opt_lifetime_ref(typ.span, lifetime);
369 visitor.visit_ty(&*mutable_type.ty)
371 TyTup(ref tuple_element_types) => {
372 for tuple_element_type in tuple_element_types {
373 visitor.visit_ty(&**tuple_element_type)
376 TyBareFn(ref function_declaration) => {
377 for argument in &function_declaration.decl.inputs {
378 visitor.visit_ty(&*argument.ty)
380 walk_fn_ret_ty(visitor, &function_declaration.decl.output);
381 walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
383 TyPath(ref maybe_qself, ref path) => {
384 if let Some(ref qself) = *maybe_qself {
385 visitor.visit_ty(&qself.ty);
387 visitor.visit_path(path, typ.id);
389 TyObjectSum(ref ty, ref bounds) => {
390 visitor.visit_ty(&**ty);
391 walk_ty_param_bounds_helper(visitor, bounds);
393 TyFixedLengthVec(ref ty, ref expression) => {
394 visitor.visit_ty(&**ty);
395 visitor.visit_expr(&**expression)
397 TyPolyTraitRef(ref bounds) => {
398 walk_ty_param_bounds_helper(visitor, bounds)
400 TyTypeof(ref expression) => {
401 visitor.visit_expr(&**expression)
405 visitor.visit_mac(mac)
410 pub fn walk_lifetime_decls_helper<'v, V: Visitor<'v>>(visitor: &mut V,
411 lifetimes: &'v Vec<LifetimeDef>) {
413 visitor.visit_lifetime_def(l);
417 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
418 for segment in &path.segments {
419 visitor.visit_path_segment(path.span, segment);
423 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
425 segment: &'v PathSegment) {
426 visitor.visit_ident(path_span, segment.identifier);
427 visitor.visit_path_parameters(path_span, &segment.parameters);
430 pub fn walk_path_parameters<'v, V: Visitor<'v>>(visitor: &mut V,
432 path_parameters: &'v PathParameters) {
433 match *path_parameters {
434 ast::AngleBracketedParameters(ref data) => {
435 for typ in data.types.iter() {
436 visitor.visit_ty(&**typ);
438 for lifetime in &data.lifetimes {
439 visitor.visit_lifetime_ref(lifetime);
441 for binding in data.bindings.iter() {
442 visitor.visit_assoc_type_binding(&**binding);
445 ast::ParenthesizedParameters(ref data) => {
446 for typ in &data.inputs {
447 visitor.visit_ty(&**typ);
449 if let Some(ref typ) = data.output {
450 visitor.visit_ty(&**typ);
456 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
457 type_binding: &'v TypeBinding) {
458 visitor.visit_ident(type_binding.span, type_binding.ident);
459 visitor.visit_ty(&*type_binding.ty);
462 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
464 PatEnum(ref path, ref children) => {
465 visitor.visit_path(path, pattern.id);
466 if let Some(ref children) = *children {
467 for child in children {
468 visitor.visit_pat(&*child)
472 PatQPath(ref qself, ref path) => {
473 visitor.visit_ty(&qself.ty);
474 visitor.visit_path(path, pattern.id)
476 PatStruct(ref path, ref fields, _) => {
477 visitor.visit_path(path, pattern.id);
478 for field in fields {
479 visitor.visit_pat(&*field.node.pat)
482 PatTup(ref tuple_elements) => {
483 for tuple_element in tuple_elements {
484 visitor.visit_pat(&**tuple_element)
487 PatBox(ref subpattern) |
488 PatRegion(ref subpattern, _) => {
489 visitor.visit_pat(&**subpattern)
491 PatIdent(_, ref pth1, ref optional_subpattern) => {
492 visitor.visit_ident(pth1.span, pth1.node);
493 match *optional_subpattern {
495 Some(ref subpattern) => visitor.visit_pat(&**subpattern),
498 PatLit(ref expression) => visitor.visit_expr(&**expression),
499 PatRange(ref lower_bound, ref upper_bound) => {
500 visitor.visit_expr(&**lower_bound);
501 visitor.visit_expr(&**upper_bound)
504 PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
505 for prepattern in prepattern {
506 visitor.visit_pat(&**prepattern)
508 if let Some(ref slice_pattern) = *slice_pattern {
509 visitor.visit_pat(&**slice_pattern)
511 for postpattern in postpatterns {
512 visitor.visit_pat(&**postpattern)
515 PatMac(ref mac) => visitor.visit_mac(mac),
519 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
520 foreign_item: &'v ForeignItem) {
521 visitor.visit_ident(foreign_item.span, foreign_item.ident);
523 match foreign_item.node {
524 ForeignItemFn(ref function_declaration, ref generics) => {
525 walk_fn_decl(visitor, &**function_declaration);
526 visitor.visit_generics(generics)
528 ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ),
531 for attr in &foreign_item.attrs {
532 visitor.visit_attribute(attr);
536 pub fn walk_ty_param_bounds_helper<'v, V: Visitor<'v>>(visitor: &mut V,
537 bounds: &'v OwnedSlice<TyParamBound>) {
538 for bound in bounds.iter() {
539 visitor.visit_ty_param_bound(bound)
543 pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
544 bound: &'v TyParamBound) {
546 TraitTyParamBound(ref typ, ref modifier) => {
547 visitor.visit_poly_trait_ref(typ, modifier);
549 RegionTyParamBound(ref lifetime) => {
550 visitor.visit_lifetime_bound(lifetime);
555 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
556 for param in generics.ty_params.iter() {
557 visitor.visit_ident(param.span, param.ident);
558 walk_ty_param_bounds_helper(visitor, ¶m.bounds);
559 walk_ty_opt(visitor, ¶m.default);
561 walk_lifetime_decls_helper(visitor, &generics.lifetimes);
562 for predicate in &generics.where_clause.predicates {
564 &ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bounded_ty,
567 visitor.visit_ty(&**bounded_ty);
568 walk_ty_param_bounds_helper(visitor, bounds);
570 &ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
573 visitor.visit_lifetime_ref(lifetime);
575 for bound in bounds {
576 visitor.visit_lifetime_ref(bound);
579 &ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{id,
583 visitor.visit_path(path, id);
584 visitor.visit_ty(&**ty);
590 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
591 if let Return(ref output_ty) = *ret_ty {
592 visitor.visit_ty(&**output_ty)
596 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
597 for argument in &function_declaration.inputs {
598 visitor.visit_pat(&*argument.pat);
599 visitor.visit_ty(&*argument.ty)
601 walk_fn_ret_ty(visitor, &function_declaration.output)
604 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
605 function_kind: FnKind<'v>,
606 function_declaration: &'v FnDecl,
607 function_body: &'v Block,
609 walk_fn_decl(visitor, function_declaration);
611 match function_kind {
612 FkItemFn(_, generics, _, _, _, _) => {
613 visitor.visit_generics(generics);
615 FkMethod(_, sig, _) => {
616 visitor.visit_generics(&sig.generics);
617 visitor.visit_explicit_self(&sig.explicit_self);
622 visitor.visit_block(function_body)
625 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
626 visitor.visit_ident(trait_item.span, trait_item.ident);
627 for attr in &trait_item.attrs {
628 visitor.visit_attribute(attr);
630 match trait_item.node {
631 ConstTraitItem(ref ty, ref default) => {
632 visitor.visit_ty(ty);
633 if let Some(ref expr) = *default {
634 visitor.visit_expr(expr);
637 MethodTraitItem(ref sig, None) => {
638 visitor.visit_explicit_self(&sig.explicit_self);
639 visitor.visit_generics(&sig.generics);
640 walk_fn_decl(visitor, &sig.decl);
642 MethodTraitItem(ref sig, Some(ref body)) => {
643 visitor.visit_fn(FkMethod(trait_item.ident, sig, None), &sig.decl,
644 body, trait_item.span, trait_item.id);
646 TypeTraitItem(ref bounds, ref default) => {
647 walk_ty_param_bounds_helper(visitor, bounds);
648 walk_ty_opt(visitor, default);
653 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
654 visitor.visit_ident(impl_item.span, impl_item.ident);
655 for attr in &impl_item.attrs {
656 visitor.visit_attribute(attr);
658 match impl_item.node {
659 ConstImplItem(ref ty, ref expr) => {
660 visitor.visit_ty(ty);
661 visitor.visit_expr(expr);
663 MethodImplItem(ref sig, ref body) => {
664 visitor.visit_fn(FkMethod(impl_item.ident, sig, Some(impl_item.vis)), &sig.decl,
665 body, impl_item.span, impl_item.id);
667 TypeImplItem(ref ty) => {
668 visitor.visit_ty(ty);
670 MacImplItem(ref mac) => {
671 visitor.visit_mac(mac);
676 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
677 struct_definition: &'v StructDef) {
678 for field in &struct_definition.fields {
679 visitor.visit_struct_field(field)
683 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
684 struct_field: &'v StructField) {
685 if let NamedField(name, _) = struct_field.node.kind {
686 visitor.visit_ident(struct_field.span, name);
689 visitor.visit_ty(&*struct_field.node.ty);
691 for attr in &struct_field.node.attrs {
692 visitor.visit_attribute(attr);
696 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
697 for statement in &block.stmts {
698 visitor.visit_stmt(&**statement)
700 walk_expr_opt(visitor, &block.expr)
703 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
704 match statement.node {
705 StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration),
706 StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
707 visitor.visit_expr(&**expression)
709 StmtMac(ref mac, _) => visitor.visit_mac(&**mac),
713 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
714 match declaration.node {
715 DeclLocal(ref local) => visitor.visit_local(&**local),
716 DeclItem(ref item) => visitor.visit_item(&**item),
720 pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V,
721 optional_expression: &'v Option<P<Expr>>) {
722 match *optional_expression {
724 Some(ref expression) => visitor.visit_expr(&**expression),
728 pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) {
729 for expression in expressions {
730 visitor.visit_expr(&**expression)
734 pub fn walk_mac<'v, V: Visitor<'v>>(_: &mut V, _: &'v Mac) {
738 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
739 match expression.node {
740 ExprBox(ref place, ref subexpression) => {
741 place.as_ref().map(|e|visitor.visit_expr(&**e));
742 visitor.visit_expr(&**subexpression)
744 ExprVec(ref subexpressions) => {
745 walk_exprs(visitor, subexpressions)
747 ExprRepeat(ref element, ref count) => {
748 visitor.visit_expr(&**element);
749 visitor.visit_expr(&**count)
751 ExprStruct(ref path, ref fields, ref optional_base) => {
752 visitor.visit_path(path, expression.id);
753 for field in fields {
754 visitor.visit_expr(&*field.expr)
756 walk_expr_opt(visitor, optional_base)
758 ExprTup(ref subexpressions) => {
759 for subexpression in subexpressions {
760 visitor.visit_expr(&**subexpression)
763 ExprCall(ref callee_expression, ref arguments) => {
764 for argument in arguments {
765 visitor.visit_expr(&**argument)
767 visitor.visit_expr(&**callee_expression)
769 ExprMethodCall(_, ref types, ref arguments) => {
770 walk_exprs(visitor, arguments);
772 visitor.visit_ty(&**typ)
775 ExprBinary(_, ref left_expression, ref right_expression) => {
776 visitor.visit_expr(&**left_expression);
777 visitor.visit_expr(&**right_expression)
779 ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
780 visitor.visit_expr(&**subexpression)
783 ExprCast(ref subexpression, ref typ) => {
784 visitor.visit_expr(&**subexpression);
785 visitor.visit_ty(&**typ)
787 ExprIf(ref head_expression, ref if_block, ref optional_else) => {
788 visitor.visit_expr(&**head_expression);
789 visitor.visit_block(&**if_block);
790 walk_expr_opt(visitor, optional_else)
792 ExprWhile(ref subexpression, ref block, _) => {
793 visitor.visit_expr(&**subexpression);
794 visitor.visit_block(&**block)
796 ExprIfLet(ref pattern, ref subexpression, ref if_block, ref optional_else) => {
797 visitor.visit_pat(&**pattern);
798 visitor.visit_expr(&**subexpression);
799 visitor.visit_block(&**if_block);
800 walk_expr_opt(visitor, optional_else);
802 ExprWhileLet(ref pattern, ref subexpression, ref block, _) => {
803 visitor.visit_pat(&**pattern);
804 visitor.visit_expr(&**subexpression);
805 visitor.visit_block(&**block);
807 ExprForLoop(ref pattern, ref subexpression, ref block, _) => {
808 visitor.visit_pat(&**pattern);
809 visitor.visit_expr(&**subexpression);
810 visitor.visit_block(&**block)
812 ExprLoop(ref block, _) => visitor.visit_block(&**block),
813 ExprMatch(ref subexpression, ref arms, _) => {
814 visitor.visit_expr(&**subexpression);
816 visitor.visit_arm(arm)
819 ExprClosure(_, ref function_declaration, ref body) => {
820 visitor.visit_fn(FkFnBlock,
821 &**function_declaration,
826 ExprBlock(ref block) => visitor.visit_block(&**block),
827 ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
828 visitor.visit_expr(&**right_hand_expression);
829 visitor.visit_expr(&**left_hand_expression)
831 ExprAssignOp(_, ref left_expression, ref right_expression) => {
832 visitor.visit_expr(&**right_expression);
833 visitor.visit_expr(&**left_expression)
835 ExprField(ref subexpression, _) => {
836 visitor.visit_expr(&**subexpression);
838 ExprTupField(ref subexpression, _) => {
839 visitor.visit_expr(&**subexpression);
841 ExprIndex(ref main_expression, ref index_expression) => {
842 visitor.visit_expr(&**main_expression);
843 visitor.visit_expr(&**index_expression)
845 ExprRange(ref start, ref end) => {
846 walk_expr_opt(visitor, start);
847 walk_expr_opt(visitor, end)
849 ExprPath(ref maybe_qself, ref path) => {
850 if let Some(ref qself) = *maybe_qself {
851 visitor.visit_ty(&qself.ty);
853 visitor.visit_path(path, expression.id)
855 ExprBreak(_) | ExprAgain(_) => {}
856 ExprRet(ref optional_expression) => {
857 walk_expr_opt(visitor, optional_expression)
859 ExprMac(ref mac) => visitor.visit_mac(mac),
860 ExprParen(ref subexpression) => {
861 visitor.visit_expr(&**subexpression)
863 ExprInlineAsm(ref ia) => {
864 for input in &ia.inputs {
865 let (_, ref input) = *input;
866 visitor.visit_expr(&**input)
868 for output in &ia.outputs {
869 let (_, ref output, _) = *output;
870 visitor.visit_expr(&**output)
875 visitor.visit_expr_post(expression)
878 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
879 for pattern in &arm.pats {
880 visitor.visit_pat(&**pattern)
882 walk_expr_opt(visitor, &arm.guard);
883 visitor.visit_expr(&*arm.body);
884 for attr in &arm.attrs {
885 visitor.visit_attribute(attr);