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::{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_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) { walk_mod(self, m) }
61 fn visit_foreign_item(&mut self, i: &'v ForeignItem) { walk_foreign_item(self, i) }
62 fn visit_item(&mut self, i: &'v Item) { walk_item(self, i) }
63 fn visit_local(&mut self, l: &'v Local) { walk_local(self, l) }
64 fn visit_block(&mut self, b: &'v Block) { walk_block(self, b) }
65 fn visit_stmt(&mut self, s: &'v Stmt) { walk_stmt(self, s) }
66 fn visit_arm(&mut self, a: &'v Arm) { walk_arm(self, a) }
67 fn visit_pat(&mut self, p: &'v Pat) { walk_pat(self, p) }
68 fn visit_decl(&mut self, d: &'v Decl) { walk_decl(self, d) }
69 fn visit_expr(&mut self, ex: &'v Expr) { walk_expr(self, ex) }
70 fn visit_expr_post(&mut self, _ex: &'v Expr) { }
71 fn visit_ty(&mut self, t: &'v Ty) { walk_ty(self, t) }
72 fn visit_generics(&mut self, g: &'v Generics) { walk_generics(self, g) }
73 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: &'v Block, s: Span, _: NodeId) {
74 walk_fn(self, fk, fd, b, s)
76 fn visit_trait_item(&mut self, ti: &'v TraitItem) { walk_trait_item(self, ti) }
77 fn visit_impl_item(&mut self, ii: &'v ImplItem) { walk_impl_item(self, ii) }
78 fn visit_trait_ref(&mut self, t: &'v TraitRef) { walk_trait_ref(self, t) }
79 fn visit_ty_param_bound(&mut self, bounds: &'v TyParamBound) {
80 walk_ty_param_bound(self, bounds)
82 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: &'v TraitBoundModifier) {
83 walk_poly_trait_ref(self, t, m)
85 fn visit_struct_def(&mut self, s: &'v StructDef, _: Name, _: &'v Generics, _: NodeId) {
86 walk_struct_def(self, s)
88 fn visit_struct_field(&mut self, s: &'v StructField) { walk_struct_field(self, s) }
89 fn visit_enum_def(&mut self, enum_definition: &'v EnumDef,
90 generics: &'v Generics) {
91 walk_enum_def(self, enum_definition, generics)
94 fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics) { walk_variant(self, v, g) }
96 /// Visits an optional reference to a lifetime. The `span` is the span of some surrounding
97 /// reference should opt_lifetime be None.
98 fn visit_opt_lifetime_ref(&mut self,
100 opt_lifetime: &'v Option<Lifetime>) {
101 match *opt_lifetime {
102 Some(ref l) => self.visit_lifetime_ref(l),
106 fn visit_lifetime_bound(&mut self, lifetime: &'v Lifetime) {
107 walk_lifetime_bound(self, lifetime)
109 fn visit_lifetime_ref(&mut self, lifetime: &'v Lifetime) {
110 walk_lifetime_ref(self, lifetime)
112 fn visit_lifetime_def(&mut self, lifetime: &'v LifetimeDef) {
113 walk_lifetime_def(self, lifetime)
115 fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
116 walk_explicit_self(self, es)
118 fn visit_path(&mut self, path: &'v Path, _id: NodeId) {
119 walk_path(self, path)
121 fn visit_path_list_item(&mut self, prefix: &'v Path, item: &'v PathListItem) {
122 walk_path_list_item(self, prefix, item)
124 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
125 walk_path_segment(self, path_span, path_segment)
127 fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'v PathParameters) {
128 walk_path_parameters(self, path_span, path_parameters)
130 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
131 walk_assoc_type_binding(self, type_binding)
133 fn visit_attribute(&mut self, _attr: &'v Attribute) {}
136 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
137 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
138 for attr in &krate.attrs {
139 visitor.visit_attribute(attr);
143 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
144 for item in &module.items {
145 visitor.visit_item(&**item)
149 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
150 visitor.visit_pat(&*local.pat);
151 walk_ty_opt(visitor, &local.ty);
152 walk_expr_opt(visitor, &local.init);
155 pub fn walk_lifetime_def<'v, V: Visitor<'v>>(visitor: &mut V,
156 lifetime_def: &'v LifetimeDef) {
157 visitor.visit_name(lifetime_def.lifetime.span, lifetime_def.lifetime.name);
158 for bound in &lifetime_def.bounds {
159 visitor.visit_lifetime_bound(bound);
163 pub fn walk_lifetime_bound<'v, V: Visitor<'v>>(visitor: &mut V,
164 lifetime_ref: &'v Lifetime) {
165 visitor.visit_lifetime_ref(lifetime_ref)
168 pub fn walk_lifetime_ref<'v, V: Visitor<'v>>(visitor: &mut V,
169 lifetime_ref: &'v Lifetime) {
170 visitor.visit_name(lifetime_ref.span, lifetime_ref.name)
173 pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V,
174 explicit_self: &'v ExplicitSelf) {
175 match explicit_self.node {
176 SelfStatic | SelfValue(_) => {},
177 SelfRegion(ref lifetime, _, _) => {
178 visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime)
180 SelfExplicit(ref typ, _) => visitor.visit_ty(&**typ),
184 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
185 trait_ref: &'v PolyTraitRef,
186 _modifier: &'v TraitBoundModifier)
189 walk_lifetime_decls_helper(visitor, &trait_ref.bound_lifetimes);
190 visitor.visit_trait_ref(&trait_ref.trait_ref);
193 pub fn walk_trait_ref<'v,V>(visitor: &mut V,
194 trait_ref: &'v TraitRef)
197 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
200 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
201 visitor.visit_name(item.span, item.name);
203 ItemExternCrate(..) => {}
206 ViewPathSimple(name, ref path) => {
207 visitor.visit_name(vp.span, name);
208 visitor.visit_path(path, item.id);
210 ViewPathGlob(ref path) => {
211 visitor.visit_path(path, item.id);
213 ViewPathList(ref prefix, ref list) => {
214 if !list.is_empty() {
216 visitor.visit_path_list_item(prefix, item)
219 visitor.visit_path(prefix, item.id);
224 ItemStatic(ref typ, _, ref expr) |
225 ItemConst(ref typ, ref expr) => {
226 visitor.visit_ty(&**typ);
227 visitor.visit_expr(&**expr);
229 ItemFn(ref declaration, unsafety, constness, abi, ref generics, ref body) => {
230 visitor.visit_fn(FnKind::ItemFn(item.name, generics, unsafety,
231 constness, abi, item.vis),
237 ItemMod(ref module) => {
238 visitor.visit_mod(module, item.span, item.id)
240 ItemForeignMod(ref foreign_module) => {
241 for foreign_item in &foreign_module.items {
242 visitor.visit_foreign_item(&**foreign_item)
245 ItemTy(ref typ, ref type_parameters) => {
246 visitor.visit_ty(&**typ);
247 visitor.visit_generics(type_parameters)
249 ItemEnum(ref enum_definition, ref type_parameters) => {
250 visitor.visit_generics(type_parameters);
251 visitor.visit_enum_def(enum_definition, type_parameters)
253 ItemDefaultImpl(_, ref trait_ref) => {
254 visitor.visit_trait_ref(trait_ref)
261 visitor.visit_generics(type_parameters);
262 match *trait_reference {
263 Some(ref trait_reference) => visitor.visit_trait_ref(trait_reference),
266 visitor.visit_ty(&**typ);
267 for impl_item in impl_items {
268 visitor.visit_impl_item(impl_item);
271 ItemStruct(ref struct_definition, ref generics) => {
272 visitor.visit_generics(generics);
273 visitor.visit_struct_def(&**struct_definition,
278 ItemTrait(_, ref generics, ref bounds, ref methods) => {
279 visitor.visit_generics(generics);
280 walk_ty_param_bounds_helper(visitor, bounds);
281 for method in methods {
282 visitor.visit_trait_item(method)
286 for attr in &item.attrs {
287 visitor.visit_attribute(attr);
291 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
292 enum_definition: &'v EnumDef,
293 generics: &'v Generics) {
294 for variant in &enum_definition.variants {
295 visitor.visit_variant(&**variant, generics);
299 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
300 variant: &'v Variant,
301 generics: &'v Generics) {
302 visitor.visit_name(variant.span, variant.node.name);
304 match variant.node.kind {
305 TupleVariantKind(ref variant_arguments) => {
306 for variant_argument in variant_arguments {
307 visitor.visit_ty(&*variant_argument.ty)
310 StructVariantKind(ref struct_definition) => {
311 visitor.visit_struct_def(&**struct_definition,
317 match variant.node.disr_expr {
318 Some(ref expr) => visitor.visit_expr(&**expr),
321 for attr in &variant.node.attrs {
322 visitor.visit_attribute(attr);
326 pub fn skip_ty<'v, V: Visitor<'v>>(_: &mut V, _: &'v Ty) {
330 pub fn walk_ty_opt<'v, V: Visitor<'v>>(visitor: &mut V, optional_type: &'v Option<P<Ty>>) {
331 match *optional_type {
332 Some(ref ty) => visitor.visit_ty(&**ty),
337 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
339 TyVec(ref ty) | TyParen(ref ty) => {
340 visitor.visit_ty(&**ty)
342 TyPtr(ref mutable_type) => {
343 visitor.visit_ty(&*mutable_type.ty)
345 TyRptr(ref lifetime, ref mutable_type) => {
346 visitor.visit_opt_lifetime_ref(typ.span, lifetime);
347 visitor.visit_ty(&*mutable_type.ty)
349 TyTup(ref tuple_element_types) => {
350 for tuple_element_type in tuple_element_types {
351 visitor.visit_ty(&**tuple_element_type)
354 TyBareFn(ref function_declaration) => {
355 for argument in &function_declaration.decl.inputs {
356 visitor.visit_ty(&*argument.ty)
358 walk_fn_ret_ty(visitor, &function_declaration.decl.output);
359 walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
361 TyPath(ref maybe_qself, ref path) => {
362 if let Some(ref qself) = *maybe_qself {
363 visitor.visit_ty(&qself.ty);
365 visitor.visit_path(path, typ.id);
367 TyObjectSum(ref ty, ref bounds) => {
368 visitor.visit_ty(&**ty);
369 walk_ty_param_bounds_helper(visitor, bounds);
371 TyFixedLengthVec(ref ty, ref expression) => {
372 visitor.visit_ty(&**ty);
373 visitor.visit_expr(&**expression)
375 TyPolyTraitRef(ref bounds) => {
376 walk_ty_param_bounds_helper(visitor, bounds)
378 TyTypeof(ref expression) => {
379 visitor.visit_expr(&**expression)
385 pub fn walk_lifetime_decls_helper<'v, V: Visitor<'v>>(visitor: &mut V,
386 lifetimes: &'v Vec<LifetimeDef>) {
388 visitor.visit_lifetime_def(l);
392 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
393 for segment in &path.segments {
394 visitor.visit_path_segment(path.span, segment);
398 pub fn walk_path_list_item<'v, V: Visitor<'v>>(visitor: &mut V, prefix: &'v Path,
399 item: &'v PathListItem) {
400 for segment in &prefix.segments {
401 visitor.visit_path_segment(prefix.span, segment);
404 if let PathListIdent { name, .. } = item.node {
405 visitor.visit_ident(item.span, name);
409 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
411 segment: &'v PathSegment) {
412 visitor.visit_name(path_span, segment.identifier.name);
413 visitor.visit_path_parameters(path_span, &segment.parameters);
416 pub fn walk_path_parameters<'v, V: Visitor<'v>>(visitor: &mut V,
418 path_parameters: &'v PathParameters) {
419 match *path_parameters {
420 hir::AngleBracketedParameters(ref data) => {
421 for typ in data.types.iter() {
422 visitor.visit_ty(&**typ);
424 for lifetime in &data.lifetimes {
425 visitor.visit_lifetime_ref(lifetime);
427 for binding in data.bindings.iter() {
428 visitor.visit_assoc_type_binding(&**binding);
431 hir::ParenthesizedParameters(ref data) => {
432 for typ in &data.inputs {
433 visitor.visit_ty(&**typ);
435 if let Some(ref typ) = data.output {
436 visitor.visit_ty(&**typ);
442 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
443 type_binding: &'v TypeBinding) {
444 visitor.visit_name(type_binding.span, type_binding.name);
445 visitor.visit_ty(&*type_binding.ty);
448 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
450 PatEnum(ref path, ref children) => {
451 visitor.visit_path(path, pattern.id);
452 if let Some(ref children) = *children {
453 for child in children {
454 visitor.visit_pat(&*child)
458 PatQPath(ref qself, ref path) => {
459 visitor.visit_ty(&qself.ty);
460 visitor.visit_path(path, pattern.id)
462 PatStruct(ref path, ref fields, _) => {
463 visitor.visit_path(path, pattern.id);
464 for field in fields {
465 visitor.visit_pat(&*field.node.pat)
468 PatTup(ref tuple_elements) => {
469 for tuple_element in tuple_elements {
470 visitor.visit_pat(&**tuple_element)
473 PatBox(ref subpattern) |
474 PatRegion(ref subpattern, _) => {
475 visitor.visit_pat(&**subpattern)
477 PatIdent(_, ref pth1, ref optional_subpattern) => {
478 visitor.visit_name(pth1.span, pth1.node.name);
479 match *optional_subpattern {
481 Some(ref subpattern) => visitor.visit_pat(&**subpattern),
484 PatLit(ref expression) => visitor.visit_expr(&**expression),
485 PatRange(ref lower_bound, ref upper_bound) => {
486 visitor.visit_expr(&**lower_bound);
487 visitor.visit_expr(&**upper_bound)
490 PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
491 for prepattern in prepattern {
492 visitor.visit_pat(&**prepattern)
494 if let Some(ref slice_pattern) = *slice_pattern {
495 visitor.visit_pat(&**slice_pattern)
497 for postpattern in postpatterns {
498 visitor.visit_pat(&**postpattern)
504 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
505 foreign_item: &'v ForeignItem) {
506 visitor.visit_name(foreign_item.span, foreign_item.name);
508 match foreign_item.node {
509 ForeignItemFn(ref function_declaration, ref generics) => {
510 walk_fn_decl(visitor, &**function_declaration);
511 visitor.visit_generics(generics)
513 ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ),
516 for attr in &foreign_item.attrs {
517 visitor.visit_attribute(attr);
521 pub fn walk_ty_param_bounds_helper<'v, V: Visitor<'v>>(visitor: &mut V,
522 bounds: &'v OwnedSlice<TyParamBound>) {
523 for bound in bounds.iter() {
524 visitor.visit_ty_param_bound(bound)
528 pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
529 bound: &'v TyParamBound) {
531 TraitTyParamBound(ref typ, ref modifier) => {
532 visitor.visit_poly_trait_ref(typ, modifier);
534 RegionTyParamBound(ref lifetime) => {
535 visitor.visit_lifetime_bound(lifetime);
540 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
541 for param in generics.ty_params.iter() {
542 visitor.visit_name(param.span, param.name);
543 walk_ty_param_bounds_helper(visitor, ¶m.bounds);
544 walk_ty_opt(visitor, ¶m.default);
546 walk_lifetime_decls_helper(visitor, &generics.lifetimes);
547 for predicate in &generics.where_clause.predicates {
549 &hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate{ref bounded_ty,
552 visitor.visit_ty(&**bounded_ty);
553 walk_ty_param_bounds_helper(visitor, bounds);
555 &hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate{ref lifetime,
558 visitor.visit_lifetime_ref(lifetime);
560 for bound in bounds {
561 visitor.visit_lifetime_ref(bound);
564 &hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{id,
568 visitor.visit_path(path, id);
569 visitor.visit_ty(&**ty);
575 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
576 if let Return(ref output_ty) = *ret_ty {
577 visitor.visit_ty(&**output_ty)
581 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
582 for argument in &function_declaration.inputs {
583 visitor.visit_pat(&*argument.pat);
584 visitor.visit_ty(&*argument.ty)
586 walk_fn_ret_ty(visitor, &function_declaration.output)
589 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V,
590 function_kind: FnKind<'v>) {
591 match function_kind {
592 FnKind::ItemFn(_, generics, _, _, _, _) => {
593 visitor.visit_generics(generics);
595 FnKind::Method(_, sig, _) => {
596 visitor.visit_generics(&sig.generics);
597 visitor.visit_explicit_self(&sig.explicit_self);
599 FnKind::Closure(..) => {}
603 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
604 function_kind: FnKind<'v>,
605 function_declaration: &'v FnDecl,
606 function_body: &'v Block,
608 walk_fn_decl(visitor, function_declaration);
609 walk_fn_kind(visitor, function_kind);
610 visitor.visit_block(function_body)
613 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
614 visitor.visit_name(trait_item.span, trait_item.name);
615 for attr in &trait_item.attrs {
616 visitor.visit_attribute(attr);
618 match trait_item.node {
619 ConstTraitItem(ref ty, ref default) => {
620 visitor.visit_ty(ty);
621 if let Some(ref expr) = *default {
622 visitor.visit_expr(expr);
625 MethodTraitItem(ref sig, None) => {
626 visitor.visit_explicit_self(&sig.explicit_self);
627 visitor.visit_generics(&sig.generics);
628 walk_fn_decl(visitor, &sig.decl);
630 MethodTraitItem(ref sig, Some(ref body)) => {
631 visitor.visit_fn(FnKind::Method(trait_item.name, sig, None), &sig.decl,
632 body, trait_item.span, trait_item.id);
634 TypeTraitItem(ref bounds, ref default) => {
635 walk_ty_param_bounds_helper(visitor, bounds);
636 walk_ty_opt(visitor, default);
641 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
642 visitor.visit_name(impl_item.span, impl_item.name);
643 for attr in &impl_item.attrs {
644 visitor.visit_attribute(attr);
646 match impl_item.node {
647 ConstImplItem(ref ty, ref expr) => {
648 visitor.visit_ty(ty);
649 visitor.visit_expr(expr);
651 MethodImplItem(ref sig, ref body) => {
652 visitor.visit_fn(FnKind::Method(impl_item.name, sig, Some(impl_item.vis)), &sig.decl,
653 body, impl_item.span, impl_item.id);
655 TypeImplItem(ref ty) => {
656 visitor.visit_ty(ty);
661 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
662 struct_definition: &'v StructDef) {
663 for field in &struct_definition.fields {
664 visitor.visit_struct_field(field)
668 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
669 struct_field: &'v StructField) {
670 if let NamedField(name, _) = struct_field.node.kind {
671 visitor.visit_name(struct_field.span, name);
674 visitor.visit_ty(&*struct_field.node.ty);
676 for attr in &struct_field.node.attrs {
677 visitor.visit_attribute(attr);
681 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
682 for statement in &block.stmts {
683 visitor.visit_stmt(&**statement)
685 walk_expr_opt(visitor, &block.expr)
688 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
689 match statement.node {
690 StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration),
691 StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
692 visitor.visit_expr(&**expression)
697 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
698 match declaration.node {
699 DeclLocal(ref local) => visitor.visit_local(&**local),
700 DeclItem(ref item) => visitor.visit_item(&**item),
704 pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V,
705 optional_expression: &'v Option<P<Expr>>) {
706 match *optional_expression {
708 Some(ref expression) => visitor.visit_expr(&**expression),
712 pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) {
713 for expression in expressions {
714 visitor.visit_expr(&**expression)
718 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
719 match expression.node {
720 ExprBox(ref place, ref subexpression) => {
721 place.as_ref().map(|e|visitor.visit_expr(&**e));
722 visitor.visit_expr(&**subexpression)
724 ExprVec(ref subexpressions) => {
725 walk_exprs(visitor, subexpressions)
727 ExprRepeat(ref element, ref count) => {
728 visitor.visit_expr(&**element);
729 visitor.visit_expr(&**count)
731 ExprStruct(ref path, ref fields, ref optional_base) => {
732 visitor.visit_path(path, expression.id);
733 for field in fields {
734 visitor.visit_expr(&*field.expr)
736 walk_expr_opt(visitor, optional_base)
738 ExprTup(ref subexpressions) => {
739 for subexpression in subexpressions {
740 visitor.visit_expr(&**subexpression)
743 ExprCall(ref callee_expression, ref arguments) => {
744 for argument in arguments {
745 visitor.visit_expr(&**argument)
747 visitor.visit_expr(&**callee_expression)
749 ExprMethodCall(_, ref types, ref arguments) => {
750 walk_exprs(visitor, arguments);
752 visitor.visit_ty(&**typ)
755 ExprBinary(_, ref left_expression, ref right_expression) => {
756 visitor.visit_expr(&**left_expression);
757 visitor.visit_expr(&**right_expression)
759 ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
760 visitor.visit_expr(&**subexpression)
763 ExprCast(ref subexpression, ref typ) => {
764 visitor.visit_expr(&**subexpression);
765 visitor.visit_ty(&**typ)
767 ExprIf(ref head_expression, ref if_block, ref optional_else) => {
768 visitor.visit_expr(&**head_expression);
769 visitor.visit_block(&**if_block);
770 walk_expr_opt(visitor, optional_else)
772 ExprWhile(ref subexpression, ref block, _) => {
773 visitor.visit_expr(&**subexpression);
774 visitor.visit_block(&**block)
776 ExprLoop(ref block, _) => visitor.visit_block(&**block),
777 ExprMatch(ref subexpression, ref arms, _) => {
778 visitor.visit_expr(&**subexpression);
780 visitor.visit_arm(arm)
783 ExprClosure(_, ref function_declaration, ref body) => {
784 visitor.visit_fn(FnKind::Closure,
785 &**function_declaration,
790 ExprBlock(ref block) => visitor.visit_block(&**block),
791 ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
792 visitor.visit_expr(&**right_hand_expression);
793 visitor.visit_expr(&**left_hand_expression)
795 ExprAssignOp(_, ref left_expression, ref right_expression) => {
796 visitor.visit_expr(&**right_expression);
797 visitor.visit_expr(&**left_expression)
799 ExprField(ref subexpression, _) => {
800 visitor.visit_expr(&**subexpression);
802 ExprTupField(ref subexpression, _) => {
803 visitor.visit_expr(&**subexpression);
805 ExprIndex(ref main_expression, ref index_expression) => {
806 visitor.visit_expr(&**main_expression);
807 visitor.visit_expr(&**index_expression)
809 ExprRange(ref start, ref end) => {
810 walk_expr_opt(visitor, start);
811 walk_expr_opt(visitor, end)
813 ExprPath(ref maybe_qself, ref path) => {
814 if let Some(ref qself) = *maybe_qself {
815 visitor.visit_ty(&qself.ty);
817 visitor.visit_path(path, expression.id)
819 ExprBreak(_) | ExprAgain(_) => {}
820 ExprRet(ref optional_expression) => {
821 walk_expr_opt(visitor, optional_expression)
823 ExprInlineAsm(ref ia) => {
824 for input in &ia.inputs {
825 let (_, ref input) = *input;
826 visitor.visit_expr(&**input)
828 for output in &ia.outputs {
829 let (_, ref output, _) = *output;
830 visitor.visit_expr(&**output)
835 visitor.visit_expr_post(expression)
838 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
839 for pattern in &arm.pats {
840 visitor.visit_pat(&**pattern)
842 walk_expr_opt(visitor, &arm.guard);
843 visitor.visit_expr(&*arm.body);
844 for attr in &arm.attrs {
845 visitor.visit_attribute(attr);