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.
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 //! HIR 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.
27 use syntax::ast::{Ident, NodeId, CRATE_NODE_ID, Name, Attribute};
30 use syntax::codemap::Span;
32 use syntax::owned_slice::OwnedSlice;
34 #[derive(Copy, Clone, PartialEq, Eq)]
36 /// fn foo() or extern "Abi" fn foo()
37 ItemFn(Name, &'a Generics, Unsafety, Constness, Abi, Visibility),
40 Method(Name, &'a MethodSig, Option<Visibility>),
47 /// Each method of the Visitor trait is a hook to be potentially
48 /// overridden. Each method's default implementation recursively visits
49 /// the substructure of the input via the corresponding `walk` method;
50 /// e.g. the `visit_mod` method by default calls `visit::walk_mod`.
52 /// If you want to ensure that your code handles every variant
53 /// explicitly, you need to override each method. (And you also need
54 /// to monitor future changes to `Visitor` in case a new method with a
55 /// new default implementation gets introduced.)
56 pub trait Visitor<'v> : Sized {
57 fn visit_name(&mut self, _span: Span, _name: Name) {
60 fn visit_ident(&mut self, span: Span, ident: Ident) { walk_ident(self, span, ident) }
61 fn visit_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) { walk_mod(self, m) }
62 fn visit_foreign_item(&mut self, i: &'v ForeignItem) { walk_foreign_item(self, i) }
63 fn visit_item(&mut self, i: &'v Item) { walk_item(self, i) }
64 fn visit_local(&mut self, l: &'v Local) { walk_local(self, l) }
65 fn visit_block(&mut self, b: &'v Block) { walk_block(self, b) }
66 fn visit_stmt(&mut self, s: &'v Stmt) { walk_stmt(self, s) }
67 fn visit_arm(&mut self, a: &'v Arm) { walk_arm(self, a) }
68 fn visit_pat(&mut self, p: &'v Pat) { walk_pat(self, p) }
69 fn visit_decl(&mut self, d: &'v Decl) { walk_decl(self, d) }
70 fn visit_expr(&mut self, ex: &'v Expr) { walk_expr(self, ex) }
71 fn visit_expr_post(&mut self, _ex: &'v Expr) { }
72 fn visit_ty(&mut self, t: &'v Ty) { walk_ty(self, t) }
73 fn visit_generics(&mut self, g: &'v Generics) { walk_generics(self, g) }
74 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: &'v Block, s: Span, _: NodeId) {
75 walk_fn(self, fk, fd, b, s)
77 fn visit_trait_item(&mut self, ti: &'v TraitItem) { walk_trait_item(self, ti) }
78 fn visit_impl_item(&mut self, ii: &'v ImplItem) { walk_impl_item(self, ii) }
79 fn visit_trait_ref(&mut self, t: &'v TraitRef) { walk_trait_ref(self, t) }
80 fn visit_ty_param_bound(&mut self, bounds: &'v TyParamBound) {
81 walk_ty_param_bound(self, bounds)
83 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: &'v TraitBoundModifier) {
84 walk_poly_trait_ref(self, t, m)
86 fn visit_struct_def(&mut self, s: &'v StructDef, _: Name, _: &'v Generics, _: NodeId) {
87 walk_struct_def(self, s)
89 fn visit_struct_field(&mut self, s: &'v StructField) { walk_struct_field(self, s) }
90 fn visit_enum_def(&mut self, enum_definition: &'v EnumDef,
91 generics: &'v Generics) {
92 walk_enum_def(self, enum_definition, generics)
95 fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics) { walk_variant(self, v, g) }
97 /// Visits an optional reference to a lifetime. The `span` is the span of some surrounding
98 /// reference should opt_lifetime be None.
99 fn visit_opt_lifetime_ref(&mut self,
101 opt_lifetime: &'v Option<Lifetime>) {
102 match *opt_lifetime {
103 Some(ref l) => self.visit_lifetime_ref(l),
107 fn visit_lifetime_bound(&mut self, lifetime: &'v Lifetime) {
108 walk_lifetime_bound(self, lifetime)
110 fn visit_lifetime_ref(&mut self, lifetime: &'v Lifetime) {
111 walk_lifetime_ref(self, lifetime)
113 fn visit_lifetime_def(&mut self, lifetime: &'v LifetimeDef) {
114 walk_lifetime_def(self, lifetime)
116 fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
117 walk_explicit_self(self, es)
119 fn visit_path(&mut self, path: &'v Path, _id: NodeId) {
120 walk_path(self, path)
122 fn visit_path_list_item(&mut self, prefix: &'v Path, item: &'v PathListItem) {
123 walk_path_list_item(self, prefix, item)
125 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
126 walk_path_segment(self, path_span, path_segment)
128 fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'v PathParameters) {
129 walk_path_parameters(self, path_span, path_parameters)
131 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
132 walk_assoc_type_binding(self, type_binding)
134 fn visit_attribute(&mut self, _attr: &'v Attribute) {}
137 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, span: Span, ident: Ident) {
138 visitor.visit_name(span, ident.name);
141 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
142 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
143 for attr in &krate.attrs {
144 visitor.visit_attribute(attr);
148 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
149 for item in &module.items {
150 visitor.visit_item(&**item)
154 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
155 visitor.visit_pat(&*local.pat);
156 walk_ty_opt(visitor, &local.ty);
157 walk_expr_opt(visitor, &local.init);
160 pub fn walk_lifetime_def<'v, V: Visitor<'v>>(visitor: &mut V,
161 lifetime_def: &'v LifetimeDef) {
162 visitor.visit_name(lifetime_def.lifetime.span, lifetime_def.lifetime.name);
163 for bound in &lifetime_def.bounds {
164 visitor.visit_lifetime_bound(bound);
168 pub fn walk_lifetime_bound<'v, V: Visitor<'v>>(visitor: &mut V,
169 lifetime_ref: &'v Lifetime) {
170 visitor.visit_lifetime_ref(lifetime_ref)
173 pub fn walk_lifetime_ref<'v, V: Visitor<'v>>(visitor: &mut V,
174 lifetime_ref: &'v Lifetime) {
175 visitor.visit_name(lifetime_ref.span, lifetime_ref.name)
178 pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V,
179 explicit_self: &'v ExplicitSelf) {
180 match explicit_self.node {
181 SelfStatic | SelfValue(_) => {},
182 SelfRegion(ref lifetime, _, _) => {
183 visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime)
185 SelfExplicit(ref typ, _) => visitor.visit_ty(&**typ),
189 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
190 trait_ref: &'v PolyTraitRef,
191 _modifier: &'v TraitBoundModifier)
194 walk_lifetime_decls_helper(visitor, &trait_ref.bound_lifetimes);
195 visitor.visit_trait_ref(&trait_ref.trait_ref);
198 pub fn walk_trait_ref<'v,V>(visitor: &mut V,
199 trait_ref: &'v TraitRef)
202 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
205 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
206 visitor.visit_name(item.span, item.name);
208 ItemExternCrate(..) => {}
211 ViewPathSimple(name, ref path) => {
212 visitor.visit_name(vp.span, name);
213 visitor.visit_path(path, item.id);
215 ViewPathGlob(ref path) => {
216 visitor.visit_path(path, item.id);
218 ViewPathList(ref prefix, ref list) => {
219 if !list.is_empty() {
221 visitor.visit_path_list_item(prefix, item)
224 visitor.visit_path(prefix, item.id);
229 ItemStatic(ref typ, _, ref expr) |
230 ItemConst(ref typ, ref expr) => {
231 visitor.visit_ty(&**typ);
232 visitor.visit_expr(&**expr);
234 ItemFn(ref declaration, unsafety, constness, abi, ref generics, ref body) => {
235 visitor.visit_fn(FnKind::ItemFn(item.name, generics, unsafety,
236 constness, abi, item.vis),
242 ItemMod(ref module) => {
243 visitor.visit_mod(module, item.span, item.id)
245 ItemForeignMod(ref foreign_module) => {
246 for foreign_item in &foreign_module.items {
247 visitor.visit_foreign_item(&**foreign_item)
250 ItemTy(ref typ, ref type_parameters) => {
251 visitor.visit_ty(&**typ);
252 visitor.visit_generics(type_parameters)
254 ItemEnum(ref enum_definition, ref type_parameters) => {
255 visitor.visit_generics(type_parameters);
256 visitor.visit_enum_def(enum_definition, type_parameters)
258 ItemDefaultImpl(_, ref trait_ref) => {
259 visitor.visit_trait_ref(trait_ref)
266 visitor.visit_generics(type_parameters);
267 match *trait_reference {
268 Some(ref trait_reference) => visitor.visit_trait_ref(trait_reference),
271 visitor.visit_ty(&**typ);
272 for impl_item in impl_items {
273 visitor.visit_impl_item(impl_item);
276 ItemStruct(ref struct_definition, ref generics) => {
277 visitor.visit_generics(generics);
278 visitor.visit_struct_def(&**struct_definition,
283 ItemTrait(_, ref generics, ref bounds, ref methods) => {
284 visitor.visit_generics(generics);
285 walk_ty_param_bounds_helper(visitor, bounds);
286 for method in methods {
287 visitor.visit_trait_item(method)
291 for attr in &item.attrs {
292 visitor.visit_attribute(attr);
296 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
297 enum_definition: &'v EnumDef,
298 generics: &'v Generics) {
299 for variant in &enum_definition.variants {
300 visitor.visit_variant(&**variant, generics);
304 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
305 variant: &'v Variant,
306 generics: &'v Generics) {
307 visitor.visit_name(variant.span, variant.node.name);
309 match variant.node.kind {
310 TupleVariantKind(ref variant_arguments) => {
311 for variant_argument in variant_arguments {
312 visitor.visit_ty(&*variant_argument.ty)
315 StructVariantKind(ref struct_definition) => {
316 visitor.visit_struct_def(&**struct_definition,
322 match variant.node.disr_expr {
323 Some(ref expr) => visitor.visit_expr(&**expr),
326 for attr in &variant.node.attrs {
327 visitor.visit_attribute(attr);
331 pub fn skip_ty<'v, V: Visitor<'v>>(_: &mut V, _: &'v Ty) {
335 pub fn walk_ty_opt<'v, V: Visitor<'v>>(visitor: &mut V, optional_type: &'v Option<P<Ty>>) {
336 match *optional_type {
337 Some(ref ty) => visitor.visit_ty(&**ty),
342 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
344 TyVec(ref ty) | TyParen(ref ty) => {
345 visitor.visit_ty(&**ty)
347 TyPtr(ref mutable_type) => {
348 visitor.visit_ty(&*mutable_type.ty)
350 TyRptr(ref lifetime, ref mutable_type) => {
351 visitor.visit_opt_lifetime_ref(typ.span, lifetime);
352 visitor.visit_ty(&*mutable_type.ty)
354 TyTup(ref tuple_element_types) => {
355 for tuple_element_type in tuple_element_types {
356 visitor.visit_ty(&**tuple_element_type)
359 TyBareFn(ref function_declaration) => {
360 for argument in &function_declaration.decl.inputs {
361 visitor.visit_ty(&*argument.ty)
363 walk_fn_ret_ty(visitor, &function_declaration.decl.output);
364 walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
366 TyPath(ref maybe_qself, ref path) => {
367 if let Some(ref qself) = *maybe_qself {
368 visitor.visit_ty(&qself.ty);
370 visitor.visit_path(path, typ.id);
372 TyObjectSum(ref ty, ref bounds) => {
373 visitor.visit_ty(&**ty);
374 walk_ty_param_bounds_helper(visitor, bounds);
376 TyFixedLengthVec(ref ty, ref expression) => {
377 visitor.visit_ty(&**ty);
378 visitor.visit_expr(&**expression)
380 TyPolyTraitRef(ref bounds) => {
381 walk_ty_param_bounds_helper(visitor, bounds)
383 TyTypeof(ref expression) => {
384 visitor.visit_expr(&**expression)
390 pub fn walk_lifetime_decls_helper<'v, V: Visitor<'v>>(visitor: &mut V,
391 lifetimes: &'v Vec<LifetimeDef>) {
393 visitor.visit_lifetime_def(l);
397 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
398 for segment in &path.segments {
399 visitor.visit_path_segment(path.span, segment);
403 pub fn walk_path_list_item<'v, V: Visitor<'v>>(visitor: &mut V, prefix: &'v Path,
404 item: &'v PathListItem) {
405 for segment in &prefix.segments {
406 visitor.visit_path_segment(prefix.span, segment);
409 if let PathListIdent { name, .. } = item.node {
410 visitor.visit_name(item.span, name);
414 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
416 segment: &'v PathSegment) {
417 visitor.visit_ident(path_span, segment.identifier);
418 visitor.visit_path_parameters(path_span, &segment.parameters);
421 pub fn walk_path_parameters<'v, V: Visitor<'v>>(visitor: &mut V,
423 path_parameters: &'v PathParameters) {
424 match *path_parameters {
425 hir::AngleBracketedParameters(ref data) => {
426 for typ in data.types.iter() {
427 visitor.visit_ty(&**typ);
429 for lifetime in &data.lifetimes {
430 visitor.visit_lifetime_ref(lifetime);
432 for binding in data.bindings.iter() {
433 visitor.visit_assoc_type_binding(&**binding);
436 hir::ParenthesizedParameters(ref data) => {
437 for typ in &data.inputs {
438 visitor.visit_ty(&**typ);
440 if let Some(ref typ) = data.output {
441 visitor.visit_ty(&**typ);
447 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
448 type_binding: &'v TypeBinding) {
449 visitor.visit_name(type_binding.span, type_binding.name);
450 visitor.visit_ty(&*type_binding.ty);
453 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
455 PatEnum(ref path, ref children) => {
456 visitor.visit_path(path, pattern.id);
457 if let Some(ref children) = *children {
458 for child in children {
459 visitor.visit_pat(&*child)
463 PatQPath(ref qself, ref path) => {
464 visitor.visit_ty(&qself.ty);
465 visitor.visit_path(path, pattern.id)
467 PatStruct(ref path, ref fields, _) => {
468 visitor.visit_path(path, pattern.id);
469 for field in fields {
470 visitor.visit_pat(&*field.node.pat)
473 PatTup(ref tuple_elements) => {
474 for tuple_element in tuple_elements {
475 visitor.visit_pat(&**tuple_element)
478 PatBox(ref subpattern) |
479 PatRegion(ref subpattern, _) => {
480 visitor.visit_pat(&**subpattern)
482 PatIdent(_, ref pth1, ref optional_subpattern) => {
483 visitor.visit_ident(pth1.span, pth1.node);
484 match *optional_subpattern {
486 Some(ref subpattern) => visitor.visit_pat(&**subpattern),
489 PatLit(ref expression) => visitor.visit_expr(&**expression),
490 PatRange(ref lower_bound, ref upper_bound) => {
491 visitor.visit_expr(&**lower_bound);
492 visitor.visit_expr(&**upper_bound)
495 PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
496 for prepattern in prepattern {
497 visitor.visit_pat(&**prepattern)
499 if let Some(ref slice_pattern) = *slice_pattern {
500 visitor.visit_pat(&**slice_pattern)
502 for postpattern in postpatterns {
503 visitor.visit_pat(&**postpattern)
509 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
510 foreign_item: &'v ForeignItem) {
511 visitor.visit_name(foreign_item.span, foreign_item.name);
513 match foreign_item.node {
514 ForeignItemFn(ref function_declaration, ref generics) => {
515 walk_fn_decl(visitor, &**function_declaration);
516 visitor.visit_generics(generics)
518 ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ),
521 for attr in &foreign_item.attrs {
522 visitor.visit_attribute(attr);
526 pub fn walk_ty_param_bounds_helper<'v, V: Visitor<'v>>(visitor: &mut V,
527 bounds: &'v OwnedSlice<TyParamBound>) {
528 for bound in bounds.iter() {
529 visitor.visit_ty_param_bound(bound)
533 pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
534 bound: &'v TyParamBound) {
536 TraitTyParamBound(ref typ, ref modifier) => {
537 visitor.visit_poly_trait_ref(typ, modifier);
539 RegionTyParamBound(ref lifetime) => {
540 visitor.visit_lifetime_bound(lifetime);
545 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
546 for param in generics.ty_params.iter() {
547 visitor.visit_name(param.span, param.name);
548 walk_ty_param_bounds_helper(visitor, ¶m.bounds);
549 walk_ty_opt(visitor, ¶m.default);
551 walk_lifetime_decls_helper(visitor, &generics.lifetimes);
552 for predicate in &generics.where_clause.predicates {
554 &hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate{ref bounded_ty,
557 visitor.visit_ty(&**bounded_ty);
558 walk_ty_param_bounds_helper(visitor, bounds);
560 &hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate{ref lifetime,
563 visitor.visit_lifetime_ref(lifetime);
565 for bound in bounds {
566 visitor.visit_lifetime_ref(bound);
569 &hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{id,
573 visitor.visit_path(path, id);
574 visitor.visit_ty(&**ty);
580 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
581 if let Return(ref output_ty) = *ret_ty {
582 visitor.visit_ty(&**output_ty)
586 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
587 for argument in &function_declaration.inputs {
588 visitor.visit_pat(&*argument.pat);
589 visitor.visit_ty(&*argument.ty)
591 walk_fn_ret_ty(visitor, &function_declaration.output)
594 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V,
595 function_kind: FnKind<'v>) {
596 match function_kind {
597 FnKind::ItemFn(_, generics, _, _, _, _) => {
598 visitor.visit_generics(generics);
600 FnKind::Method(_, sig, _) => {
601 visitor.visit_generics(&sig.generics);
602 visitor.visit_explicit_self(&sig.explicit_self);
604 FnKind::Closure(..) => {}
608 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
609 function_kind: FnKind<'v>,
610 function_declaration: &'v FnDecl,
611 function_body: &'v Block,
613 walk_fn_decl(visitor, function_declaration);
614 walk_fn_kind(visitor, function_kind);
615 visitor.visit_block(function_body)
618 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
619 visitor.visit_name(trait_item.span, trait_item.name);
620 for attr in &trait_item.attrs {
621 visitor.visit_attribute(attr);
623 match trait_item.node {
624 ConstTraitItem(ref ty, ref default) => {
625 visitor.visit_ty(ty);
626 if let Some(ref expr) = *default {
627 visitor.visit_expr(expr);
630 MethodTraitItem(ref sig, None) => {
631 visitor.visit_explicit_self(&sig.explicit_self);
632 visitor.visit_generics(&sig.generics);
633 walk_fn_decl(visitor, &sig.decl);
635 MethodTraitItem(ref sig, Some(ref body)) => {
636 visitor.visit_fn(FnKind::Method(trait_item.name, sig, None), &sig.decl,
637 body, trait_item.span, trait_item.id);
639 TypeTraitItem(ref bounds, ref default) => {
640 walk_ty_param_bounds_helper(visitor, bounds);
641 walk_ty_opt(visitor, default);
646 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
647 visitor.visit_name(impl_item.span, impl_item.name);
648 for attr in &impl_item.attrs {
649 visitor.visit_attribute(attr);
651 match impl_item.node {
652 ConstImplItem(ref ty, ref expr) => {
653 visitor.visit_ty(ty);
654 visitor.visit_expr(expr);
656 MethodImplItem(ref sig, ref body) => {
657 visitor.visit_fn(FnKind::Method(impl_item.name, sig, Some(impl_item.vis)), &sig.decl,
658 body, impl_item.span, impl_item.id);
660 TypeImplItem(ref ty) => {
661 visitor.visit_ty(ty);
666 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
667 struct_definition: &'v StructDef) {
668 for field in &struct_definition.fields {
669 visitor.visit_struct_field(field)
673 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
674 struct_field: &'v StructField) {
675 if let NamedField(name, _) = struct_field.node.kind {
676 visitor.visit_name(struct_field.span, name);
679 visitor.visit_ty(&*struct_field.node.ty);
681 for attr in &struct_field.node.attrs {
682 visitor.visit_attribute(attr);
686 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
687 for statement in &block.stmts {
688 visitor.visit_stmt(&**statement)
690 walk_expr_opt(visitor, &block.expr)
693 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
694 match statement.node {
695 StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration),
696 StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
697 visitor.visit_expr(&**expression)
702 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
703 match declaration.node {
704 DeclLocal(ref local) => visitor.visit_local(&**local),
705 DeclItem(ref item) => visitor.visit_item(&**item),
709 pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V,
710 optional_expression: &'v Option<P<Expr>>) {
711 match *optional_expression {
713 Some(ref expression) => visitor.visit_expr(&**expression),
717 pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) {
718 for expression in expressions {
719 visitor.visit_expr(&**expression)
723 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
724 match expression.node {
725 ExprBox(ref subexpression) => {
726 visitor.visit_expr(&**subexpression)
728 ExprVec(ref subexpressions) => {
729 walk_exprs(visitor, subexpressions)
731 ExprRepeat(ref element, ref count) => {
732 visitor.visit_expr(&**element);
733 visitor.visit_expr(&**count)
735 ExprStruct(ref path, ref fields, ref optional_base) => {
736 visitor.visit_path(path, expression.id);
737 for field in fields {
738 visitor.visit_expr(&*field.expr)
740 walk_expr_opt(visitor, optional_base)
742 ExprTup(ref subexpressions) => {
743 for subexpression in subexpressions {
744 visitor.visit_expr(&**subexpression)
747 ExprCall(ref callee_expression, ref arguments) => {
748 for argument in arguments {
749 visitor.visit_expr(&**argument)
751 visitor.visit_expr(&**callee_expression)
753 ExprMethodCall(_, ref types, ref arguments) => {
754 walk_exprs(visitor, arguments);
756 visitor.visit_ty(&**typ)
759 ExprBinary(_, ref left_expression, ref right_expression) => {
760 visitor.visit_expr(&**left_expression);
761 visitor.visit_expr(&**right_expression)
763 ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
764 visitor.visit_expr(&**subexpression)
767 ExprCast(ref subexpression, ref typ) => {
768 visitor.visit_expr(&**subexpression);
769 visitor.visit_ty(&**typ)
771 ExprIf(ref head_expression, ref if_block, ref optional_else) => {
772 visitor.visit_expr(&**head_expression);
773 visitor.visit_block(&**if_block);
774 walk_expr_opt(visitor, optional_else)
776 ExprWhile(ref subexpression, ref block, _) => {
777 visitor.visit_expr(&**subexpression);
778 visitor.visit_block(&**block)
780 ExprLoop(ref block, _) => visitor.visit_block(&**block),
781 ExprMatch(ref subexpression, ref arms, _) => {
782 visitor.visit_expr(&**subexpression);
784 visitor.visit_arm(arm)
787 ExprClosure(_, ref function_declaration, ref body) => {
788 visitor.visit_fn(FnKind::Closure,
789 &**function_declaration,
794 ExprBlock(ref block) => visitor.visit_block(&**block),
795 ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
796 visitor.visit_expr(&**right_hand_expression);
797 visitor.visit_expr(&**left_hand_expression)
799 ExprAssignOp(_, ref left_expression, ref right_expression) => {
800 visitor.visit_expr(&**right_expression);
801 visitor.visit_expr(&**left_expression)
803 ExprField(ref subexpression, _) => {
804 visitor.visit_expr(&**subexpression);
806 ExprTupField(ref subexpression, _) => {
807 visitor.visit_expr(&**subexpression);
809 ExprIndex(ref main_expression, ref index_expression) => {
810 visitor.visit_expr(&**main_expression);
811 visitor.visit_expr(&**index_expression)
813 ExprRange(ref start, ref end) => {
814 walk_expr_opt(visitor, start);
815 walk_expr_opt(visitor, end)
817 ExprPath(ref maybe_qself, ref path) => {
818 if let Some(ref qself) = *maybe_qself {
819 visitor.visit_ty(&qself.ty);
821 visitor.visit_path(path, expression.id)
823 ExprBreak(_) | ExprAgain(_) => {}
824 ExprRet(ref optional_expression) => {
825 walk_expr_opt(visitor, optional_expression)
827 ExprInlineAsm(ref ia) => {
828 for input in &ia.inputs {
829 let (_, ref input) = *input;
830 visitor.visit_expr(&**input)
832 for output in &ia.outputs {
833 let (_, ref output, _) = *output;
834 visitor.visit_expr(&**output)
839 visitor.visit_expr_post(expression)
842 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
843 for pattern in &arm.pats {
844 visitor.visit_pat(&**pattern)
846 walk_expr_opt(visitor, &arm.guard);
847 visitor.visit_expr(&*arm.body);
848 for attr in &arm.attrs {
849 visitor.visit_attribute(attr);