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(Ident, &'a Generics, Unsafety, Constness, Abi, Visibility),
40 Method(Ident, &'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) {
61 self.visit_name(span, ident.name);
63 fn visit_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) { walk_mod(self, m) }
64 fn visit_foreign_item(&mut self, i: &'v ForeignItem) { walk_foreign_item(self, i) }
65 fn visit_item(&mut self, i: &'v Item) { walk_item(self, i) }
66 fn visit_local(&mut self, l: &'v Local) { walk_local(self, l) }
67 fn visit_block(&mut self, b: &'v Block) { walk_block(self, b) }
68 fn visit_stmt(&mut self, s: &'v Stmt) { walk_stmt(self, s) }
69 fn visit_arm(&mut self, a: &'v Arm) { walk_arm(self, a) }
70 fn visit_pat(&mut self, p: &'v Pat) { walk_pat(self, p) }
71 fn visit_decl(&mut self, d: &'v Decl) { walk_decl(self, d) }
72 fn visit_expr(&mut self, ex: &'v Expr) { walk_expr(self, ex) }
73 fn visit_expr_post(&mut self, _ex: &'v Expr) { }
74 fn visit_ty(&mut self, t: &'v Ty) { walk_ty(self, t) }
75 fn visit_generics(&mut self, g: &'v Generics) { walk_generics(self, g) }
76 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: &'v Block, s: Span, _: NodeId) {
77 walk_fn(self, fk, fd, b, s)
79 fn visit_trait_item(&mut self, ti: &'v TraitItem) { walk_trait_item(self, ti) }
80 fn visit_impl_item(&mut self, ii: &'v ImplItem) { walk_impl_item(self, ii) }
81 fn visit_trait_ref(&mut self, t: &'v TraitRef) { walk_trait_ref(self, t) }
82 fn visit_ty_param_bound(&mut self, bounds: &'v TyParamBound) {
83 walk_ty_param_bound(self, bounds)
85 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: &'v TraitBoundModifier) {
86 walk_poly_trait_ref(self, t, m)
88 fn visit_struct_def(&mut self, s: &'v StructDef, _: Ident, _: &'v Generics, _: NodeId) {
89 walk_struct_def(self, s)
91 fn visit_struct_field(&mut self, s: &'v StructField) { walk_struct_field(self, s) }
92 fn visit_enum_def(&mut self, enum_definition: &'v EnumDef,
93 generics: &'v Generics) {
94 walk_enum_def(self, enum_definition, generics)
97 fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics) { walk_variant(self, v, g) }
99 /// Visits an optional reference to a lifetime. The `span` is the span of some surrounding
100 /// reference should opt_lifetime be None.
101 fn visit_opt_lifetime_ref(&mut self,
103 opt_lifetime: &'v Option<Lifetime>) {
104 match *opt_lifetime {
105 Some(ref l) => self.visit_lifetime_ref(l),
109 fn visit_lifetime_bound(&mut self, lifetime: &'v Lifetime) {
110 walk_lifetime_bound(self, lifetime)
112 fn visit_lifetime_ref(&mut self, lifetime: &'v Lifetime) {
113 walk_lifetime_ref(self, lifetime)
115 fn visit_lifetime_def(&mut self, lifetime: &'v LifetimeDef) {
116 walk_lifetime_def(self, lifetime)
118 fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
119 walk_explicit_self(self, es)
121 fn visit_path(&mut self, path: &'v Path, _id: NodeId) {
122 walk_path(self, path)
124 fn visit_path_list_item(&mut self, prefix: &'v Path, item: &'v PathListItem) {
125 walk_path_list_item(self, prefix, item)
127 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
128 walk_path_segment(self, path_span, path_segment)
130 fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'v PathParameters) {
131 walk_path_parameters(self, path_span, path_parameters)
133 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
134 walk_assoc_type_binding(self, type_binding)
136 fn visit_attribute(&mut self, _attr: &'v Attribute) {}
139 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
140 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
141 for attr in &krate.attrs {
142 visitor.visit_attribute(attr);
146 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
147 for item in &module.items {
148 visitor.visit_item(&**item)
152 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
153 visitor.visit_pat(&*local.pat);
154 walk_ty_opt(visitor, &local.ty);
155 walk_expr_opt(visitor, &local.init);
158 pub fn walk_lifetime_def<'v, V: Visitor<'v>>(visitor: &mut V,
159 lifetime_def: &'v LifetimeDef) {
160 visitor.visit_name(lifetime_def.lifetime.span, lifetime_def.lifetime.name);
161 for bound in &lifetime_def.bounds {
162 visitor.visit_lifetime_bound(bound);
166 pub fn walk_lifetime_bound<'v, V: Visitor<'v>>(visitor: &mut V,
167 lifetime_ref: &'v Lifetime) {
168 visitor.visit_lifetime_ref(lifetime_ref)
171 pub fn walk_lifetime_ref<'v, V: Visitor<'v>>(visitor: &mut V,
172 lifetime_ref: &'v Lifetime) {
173 visitor.visit_name(lifetime_ref.span, lifetime_ref.name)
176 pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V,
177 explicit_self: &'v ExplicitSelf) {
178 match explicit_self.node {
179 SelfStatic | SelfValue(_) => {},
180 SelfRegion(ref lifetime, _, _) => {
181 visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime)
183 SelfExplicit(ref typ, _) => visitor.visit_ty(&**typ),
187 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
188 trait_ref: &'v PolyTraitRef,
189 _modifier: &'v TraitBoundModifier)
192 walk_lifetime_decls_helper(visitor, &trait_ref.bound_lifetimes);
193 visitor.visit_trait_ref(&trait_ref.trait_ref);
196 pub fn walk_trait_ref<'v,V>(visitor: &mut V,
197 trait_ref: &'v TraitRef)
200 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
203 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
204 visitor.visit_ident(item.span, item.ident);
206 ItemExternCrate(..) => {}
209 ViewPathSimple(_ident, ref path) => {
210 visitor.visit_path(path, item.id);
212 ViewPathGlob(ref path) => {
213 visitor.visit_path(path, item.id);
215 ViewPathList(ref prefix, ref list) => {
216 if !list.is_empty() {
218 visitor.visit_path_list_item(prefix, item)
221 visitor.visit_path(prefix, item.id);
226 ItemStatic(ref typ, _, ref expr) |
227 ItemConst(ref typ, ref expr) => {
228 visitor.visit_ty(&**typ);
229 visitor.visit_expr(&**expr);
231 ItemFn(ref declaration, unsafety, constness, abi, ref generics, ref body) => {
232 visitor.visit_fn(FnKind::ItemFn(item.ident, generics, unsafety,
233 constness, abi, item.vis),
239 ItemMod(ref module) => {
240 visitor.visit_mod(module, item.span, item.id)
242 ItemForeignMod(ref foreign_module) => {
243 for foreign_item in &foreign_module.items {
244 visitor.visit_foreign_item(&**foreign_item)
247 ItemTy(ref typ, ref type_parameters) => {
248 visitor.visit_ty(&**typ);
249 visitor.visit_generics(type_parameters)
251 ItemEnum(ref enum_definition, ref type_parameters) => {
252 visitor.visit_generics(type_parameters);
253 visitor.visit_enum_def(enum_definition, type_parameters)
255 ItemDefaultImpl(_, ref trait_ref) => {
256 visitor.visit_trait_ref(trait_ref)
263 visitor.visit_generics(type_parameters);
264 match *trait_reference {
265 Some(ref trait_reference) => visitor.visit_trait_ref(trait_reference),
268 visitor.visit_ty(&**typ);
269 for impl_item in impl_items {
270 visitor.visit_impl_item(impl_item);
273 ItemStruct(ref struct_definition, ref generics) => {
274 visitor.visit_generics(generics);
275 visitor.visit_struct_def(&**struct_definition,
280 ItemTrait(_, ref generics, ref bounds, ref methods) => {
281 visitor.visit_generics(generics);
282 walk_ty_param_bounds_helper(visitor, bounds);
283 for method in methods {
284 visitor.visit_trait_item(method)
288 for attr in &item.attrs {
289 visitor.visit_attribute(attr);
293 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
294 enum_definition: &'v EnumDef,
295 generics: &'v Generics) {
296 for variant in &enum_definition.variants {
297 visitor.visit_variant(&**variant, generics);
301 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
302 variant: &'v Variant,
303 generics: &'v Generics) {
304 visitor.visit_ident(variant.span, variant.node.name);
306 match variant.node.kind {
307 TupleVariantKind(ref variant_arguments) => {
308 for variant_argument in variant_arguments {
309 visitor.visit_ty(&*variant_argument.ty)
312 StructVariantKind(ref struct_definition) => {
313 visitor.visit_struct_def(&**struct_definition,
319 match variant.node.disr_expr {
320 Some(ref expr) => visitor.visit_expr(&**expr),
323 for attr in &variant.node.attrs {
324 visitor.visit_attribute(attr);
328 pub fn skip_ty<'v, V: Visitor<'v>>(_: &mut V, _: &'v Ty) {
332 pub fn walk_ty_opt<'v, V: Visitor<'v>>(visitor: &mut V, optional_type: &'v Option<P<Ty>>) {
333 match *optional_type {
334 Some(ref ty) => visitor.visit_ty(&**ty),
339 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
341 TyVec(ref ty) | TyParen(ref ty) => {
342 visitor.visit_ty(&**ty)
344 TyPtr(ref mutable_type) => {
345 visitor.visit_ty(&*mutable_type.ty)
347 TyRptr(ref lifetime, ref mutable_type) => {
348 visitor.visit_opt_lifetime_ref(typ.span, lifetime);
349 visitor.visit_ty(&*mutable_type.ty)
351 TyTup(ref tuple_element_types) => {
352 for tuple_element_type in tuple_element_types {
353 visitor.visit_ty(&**tuple_element_type)
356 TyBareFn(ref function_declaration) => {
357 for argument in &function_declaration.decl.inputs {
358 visitor.visit_ty(&*argument.ty)
360 walk_fn_ret_ty(visitor, &function_declaration.decl.output);
361 walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
363 TyPath(ref maybe_qself, ref path) => {
364 if let Some(ref qself) = *maybe_qself {
365 visitor.visit_ty(&qself.ty);
367 visitor.visit_path(path, typ.id);
369 TyObjectSum(ref ty, ref bounds) => {
370 visitor.visit_ty(&**ty);
371 walk_ty_param_bounds_helper(visitor, bounds);
373 TyFixedLengthVec(ref ty, ref expression) => {
374 visitor.visit_ty(&**ty);
375 visitor.visit_expr(&**expression)
377 TyPolyTraitRef(ref bounds) => {
378 walk_ty_param_bounds_helper(visitor, bounds)
380 TyTypeof(ref expression) => {
381 visitor.visit_expr(&**expression)
387 pub fn walk_lifetime_decls_helper<'v, V: Visitor<'v>>(visitor: &mut V,
388 lifetimes: &'v Vec<LifetimeDef>) {
390 visitor.visit_lifetime_def(l);
394 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
395 for segment in &path.segments {
396 visitor.visit_path_segment(path.span, segment);
400 pub fn walk_path_list_item<'v, V: Visitor<'v>>(visitor: &mut V, prefix: &'v Path,
401 item: &'v PathListItem) {
402 for segment in &prefix.segments {
403 visitor.visit_path_segment(prefix.span, segment);
406 if let PathListIdent { name, .. } = item.node {
407 visitor.visit_ident(item.span, name);
411 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
413 segment: &'v PathSegment) {
414 visitor.visit_ident(path_span, segment.identifier);
415 visitor.visit_path_parameters(path_span, &segment.parameters);
418 pub fn walk_path_parameters<'v, V: Visitor<'v>>(visitor: &mut V,
420 path_parameters: &'v PathParameters) {
421 match *path_parameters {
422 hir::AngleBracketedParameters(ref data) => {
423 for typ in data.types.iter() {
424 visitor.visit_ty(&**typ);
426 for lifetime in &data.lifetimes {
427 visitor.visit_lifetime_ref(lifetime);
429 for binding in data.bindings.iter() {
430 visitor.visit_assoc_type_binding(&**binding);
433 hir::ParenthesizedParameters(ref data) => {
434 for typ in &data.inputs {
435 visitor.visit_ty(&**typ);
437 if let Some(ref typ) = data.output {
438 visitor.visit_ty(&**typ);
444 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
445 type_binding: &'v TypeBinding) {
446 visitor.visit_ident(type_binding.span, type_binding.ident);
447 visitor.visit_ty(&*type_binding.ty);
450 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
452 PatEnum(ref path, ref children) => {
453 visitor.visit_path(path, pattern.id);
454 if let Some(ref children) = *children {
455 for child in children {
456 visitor.visit_pat(&*child)
460 PatQPath(ref qself, ref path) => {
461 visitor.visit_ty(&qself.ty);
462 visitor.visit_path(path, pattern.id)
464 PatStruct(ref path, ref fields, _) => {
465 visitor.visit_path(path, pattern.id);
466 for field in fields {
467 visitor.visit_pat(&*field.node.pat)
470 PatTup(ref tuple_elements) => {
471 for tuple_element in tuple_elements {
472 visitor.visit_pat(&**tuple_element)
475 PatBox(ref subpattern) |
476 PatRegion(ref subpattern, _) => {
477 visitor.visit_pat(&**subpattern)
479 PatIdent(_, ref pth1, ref optional_subpattern) => {
480 visitor.visit_ident(pth1.span, pth1.node);
481 match *optional_subpattern {
483 Some(ref subpattern) => visitor.visit_pat(&**subpattern),
486 PatLit(ref expression) => visitor.visit_expr(&**expression),
487 PatRange(ref lower_bound, ref upper_bound) => {
488 visitor.visit_expr(&**lower_bound);
489 visitor.visit_expr(&**upper_bound)
492 PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
493 for prepattern in prepattern {
494 visitor.visit_pat(&**prepattern)
496 if let Some(ref slice_pattern) = *slice_pattern {
497 visitor.visit_pat(&**slice_pattern)
499 for postpattern in postpatterns {
500 visitor.visit_pat(&**postpattern)
506 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
507 foreign_item: &'v ForeignItem) {
508 visitor.visit_ident(foreign_item.span, foreign_item.ident);
510 match foreign_item.node {
511 ForeignItemFn(ref function_declaration, ref generics) => {
512 walk_fn_decl(visitor, &**function_declaration);
513 visitor.visit_generics(generics)
515 ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ),
518 for attr in &foreign_item.attrs {
519 visitor.visit_attribute(attr);
523 pub fn walk_ty_param_bounds_helper<'v, V: Visitor<'v>>(visitor: &mut V,
524 bounds: &'v OwnedSlice<TyParamBound>) {
525 for bound in bounds.iter() {
526 visitor.visit_ty_param_bound(bound)
530 pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
531 bound: &'v TyParamBound) {
533 TraitTyParamBound(ref typ, ref modifier) => {
534 visitor.visit_poly_trait_ref(typ, modifier);
536 RegionTyParamBound(ref lifetime) => {
537 visitor.visit_lifetime_bound(lifetime);
542 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
543 for param in generics.ty_params.iter() {
544 visitor.visit_ident(param.span, param.ident);
545 walk_ty_param_bounds_helper(visitor, ¶m.bounds);
546 walk_ty_opt(visitor, ¶m.default);
548 walk_lifetime_decls_helper(visitor, &generics.lifetimes);
549 for predicate in &generics.where_clause.predicates {
551 &hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate{ref bounded_ty,
554 visitor.visit_ty(&**bounded_ty);
555 walk_ty_param_bounds_helper(visitor, bounds);
557 &hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate{ref lifetime,
560 visitor.visit_lifetime_ref(lifetime);
562 for bound in bounds {
563 visitor.visit_lifetime_ref(bound);
566 &hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{id,
570 visitor.visit_path(path, id);
571 visitor.visit_ty(&**ty);
577 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
578 if let Return(ref output_ty) = *ret_ty {
579 visitor.visit_ty(&**output_ty)
583 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
584 for argument in &function_declaration.inputs {
585 visitor.visit_pat(&*argument.pat);
586 visitor.visit_ty(&*argument.ty)
588 walk_fn_ret_ty(visitor, &function_declaration.output)
591 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V,
592 function_kind: FnKind<'v>) {
593 match function_kind {
594 FnKind::ItemFn(_, generics, _, _, _, _) => {
595 visitor.visit_generics(generics);
597 FnKind::Method(_, sig, _) => {
598 visitor.visit_generics(&sig.generics);
599 visitor.visit_explicit_self(&sig.explicit_self);
601 FnKind::Closure(..) => {}
605 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
606 function_kind: FnKind<'v>,
607 function_declaration: &'v FnDecl,
608 function_body: &'v Block,
610 walk_fn_decl(visitor, function_declaration);
611 walk_fn_kind(visitor, function_kind);
612 visitor.visit_block(function_body)
615 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
616 visitor.visit_ident(trait_item.span, trait_item.ident);
617 for attr in &trait_item.attrs {
618 visitor.visit_attribute(attr);
620 match trait_item.node {
621 ConstTraitItem(ref ty, ref default) => {
622 visitor.visit_ty(ty);
623 if let Some(ref expr) = *default {
624 visitor.visit_expr(expr);
627 MethodTraitItem(ref sig, None) => {
628 visitor.visit_explicit_self(&sig.explicit_self);
629 visitor.visit_generics(&sig.generics);
630 walk_fn_decl(visitor, &sig.decl);
632 MethodTraitItem(ref sig, Some(ref body)) => {
633 visitor.visit_fn(FnKind::Method(trait_item.ident, sig, None), &sig.decl,
634 body, trait_item.span, trait_item.id);
636 TypeTraitItem(ref bounds, ref default) => {
637 walk_ty_param_bounds_helper(visitor, bounds);
638 walk_ty_opt(visitor, default);
643 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
644 visitor.visit_ident(impl_item.span, impl_item.ident);
645 for attr in &impl_item.attrs {
646 visitor.visit_attribute(attr);
648 match impl_item.node {
649 ConstImplItem(ref ty, ref expr) => {
650 visitor.visit_ty(ty);
651 visitor.visit_expr(expr);
653 MethodImplItem(ref sig, ref body) => {
654 visitor.visit_fn(FnKind::Method(impl_item.ident, sig, Some(impl_item.vis)), &sig.decl,
655 body, impl_item.span, impl_item.id);
657 TypeImplItem(ref ty) => {
658 visitor.visit_ty(ty);
663 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
664 struct_definition: &'v StructDef) {
665 for field in &struct_definition.fields {
666 visitor.visit_struct_field(field)
670 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
671 struct_field: &'v StructField) {
672 if let NamedField(name, _) = struct_field.node.kind {
673 visitor.visit_ident(struct_field.span, name);
676 visitor.visit_ty(&*struct_field.node.ty);
678 for attr in &struct_field.node.attrs {
679 visitor.visit_attribute(attr);
683 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
684 for statement in &block.stmts {
685 visitor.visit_stmt(&**statement)
687 walk_expr_opt(visitor, &block.expr)
690 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
691 match statement.node {
692 StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration),
693 StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
694 visitor.visit_expr(&**expression)
699 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
700 match declaration.node {
701 DeclLocal(ref local) => visitor.visit_local(&**local),
702 DeclItem(ref item) => visitor.visit_item(&**item),
706 pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V,
707 optional_expression: &'v Option<P<Expr>>) {
708 match *optional_expression {
710 Some(ref expression) => visitor.visit_expr(&**expression),
714 pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) {
715 for expression in expressions {
716 visitor.visit_expr(&**expression)
720 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
721 match expression.node {
722 ExprBox(ref place, ref subexpression) => {
723 place.as_ref().map(|e|visitor.visit_expr(&**e));
724 visitor.visit_expr(&**subexpression)
726 ExprVec(ref subexpressions) => {
727 walk_exprs(visitor, subexpressions)
729 ExprRepeat(ref element, ref count) => {
730 visitor.visit_expr(&**element);
731 visitor.visit_expr(&**count)
733 ExprStruct(ref path, ref fields, ref optional_base) => {
734 visitor.visit_path(path, expression.id);
735 for field in fields {
736 visitor.visit_expr(&*field.expr)
738 walk_expr_opt(visitor, optional_base)
740 ExprTup(ref subexpressions) => {
741 for subexpression in subexpressions {
742 visitor.visit_expr(&**subexpression)
745 ExprCall(ref callee_expression, ref arguments) => {
746 for argument in arguments {
747 visitor.visit_expr(&**argument)
749 visitor.visit_expr(&**callee_expression)
751 ExprMethodCall(_, ref types, ref arguments) => {
752 walk_exprs(visitor, arguments);
754 visitor.visit_ty(&**typ)
757 ExprBinary(_, ref left_expression, ref right_expression) => {
758 visitor.visit_expr(&**left_expression);
759 visitor.visit_expr(&**right_expression)
761 ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
762 visitor.visit_expr(&**subexpression)
765 ExprCast(ref subexpression, ref typ) => {
766 visitor.visit_expr(&**subexpression);
767 visitor.visit_ty(&**typ)
769 ExprIf(ref head_expression, ref if_block, ref optional_else) => {
770 visitor.visit_expr(&**head_expression);
771 visitor.visit_block(&**if_block);
772 walk_expr_opt(visitor, optional_else)
774 ExprWhile(ref subexpression, ref block, _) => {
775 visitor.visit_expr(&**subexpression);
776 visitor.visit_block(&**block)
778 ExprLoop(ref block, _) => visitor.visit_block(&**block),
779 ExprMatch(ref subexpression, ref arms, _) => {
780 visitor.visit_expr(&**subexpression);
782 visitor.visit_arm(arm)
785 ExprClosure(_, ref function_declaration, ref body) => {
786 visitor.visit_fn(FnKind::Closure,
787 &**function_declaration,
792 ExprBlock(ref block) => visitor.visit_block(&**block),
793 ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
794 visitor.visit_expr(&**right_hand_expression);
795 visitor.visit_expr(&**left_hand_expression)
797 ExprAssignOp(_, ref left_expression, ref right_expression) => {
798 visitor.visit_expr(&**right_expression);
799 visitor.visit_expr(&**left_expression)
801 ExprField(ref subexpression, _) => {
802 visitor.visit_expr(&**subexpression);
804 ExprTupField(ref subexpression, _) => {
805 visitor.visit_expr(&**subexpression);
807 ExprIndex(ref main_expression, ref index_expression) => {
808 visitor.visit_expr(&**main_expression);
809 visitor.visit_expr(&**index_expression)
811 ExprRange(ref start, ref end) => {
812 walk_expr_opt(visitor, start);
813 walk_expr_opt(visitor, end)
815 ExprPath(ref maybe_qself, ref path) => {
816 if let Some(ref qself) = *maybe_qself {
817 visitor.visit_ty(&qself.ty);
819 visitor.visit_path(path, expression.id)
821 ExprBreak(_) | ExprAgain(_) => {}
822 ExprRet(ref optional_expression) => {
823 walk_expr_opt(visitor, optional_expression)
825 ExprInlineAsm(ref ia) => {
826 for input in &ia.inputs {
827 let (_, ref input) = *input;
828 visitor.visit_expr(&**input)
830 for output in &ia.outputs {
831 let (_, ref output, _) = *output;
832 visitor.visit_expr(&**output)
837 visitor.visit_expr_post(expression)
840 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
841 for pattern in &arm.pats {
842 visitor.visit_pat(&**pattern)
844 walk_expr_opt(visitor, &arm.guard);
845 visitor.visit_expr(&*arm.body);
846 for attr in &arm.attrs {
847 visitor.visit_attribute(attr);