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;
36 /// fn foo() or extern "Abi" fn foo()
37 FkItemFn(Ident, &'a Generics, FnStyle, Abi),
40 FkMethod(Ident, &'a Generics, &'a Method),
47 impl<'a> Copy for FnKind<'a> {}
49 /// Each method of the Visitor trait is a hook to be potentially
50 /// overridden. Each method's default implementation recursively visits
51 /// the substructure of the input via the corresponding `walk` method;
52 /// e.g. the `visit_mod` method by default calls `visit::walk_mod`.
54 /// If you want to ensure that your code handles every variant
55 /// explicitly, you need to override each method. (And you also need
56 /// to monitor future changes to `Visitor` in case a new method with a
57 /// new default implementation gets introduced.)
58 pub trait Visitor<'v> {
60 fn visit_name(&mut self, _span: Span, _name: Name) {
63 fn visit_ident(&mut self, span: Span, ident: Ident) {
64 self.visit_name(span, ident.name);
66 fn visit_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) { walk_mod(self, m) }
67 fn visit_view_item(&mut self, i: &'v ViewItem) { walk_view_item(self, i) }
68 fn visit_foreign_item(&mut self, i: &'v ForeignItem) { walk_foreign_item(self, i) }
69 fn visit_item(&mut self, i: &'v Item) { walk_item(self, i) }
70 fn visit_local(&mut self, l: &'v Local) { walk_local(self, l) }
71 fn visit_block(&mut self, b: &'v Block) { walk_block(self, b) }
72 fn visit_stmt(&mut self, s: &'v Stmt) { walk_stmt(self, s) }
73 fn visit_arm(&mut self, a: &'v Arm) { walk_arm(self, a) }
74 fn visit_pat(&mut self, p: &'v Pat) { walk_pat(self, p) }
75 fn visit_decl(&mut self, d: &'v Decl) { walk_decl(self, d) }
76 fn visit_expr(&mut self, ex: &'v Expr) { walk_expr(self, ex) }
77 fn visit_expr_post(&mut self, _ex: &'v Expr) { }
78 fn visit_ty(&mut self, t: &'v Ty) { walk_ty(self, t) }
79 fn visit_generics(&mut self, g: &'v Generics) { walk_generics(self, g) }
80 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: &'v Block, s: Span, _: NodeId) {
81 walk_fn(self, fk, fd, b, s)
83 fn visit_ty_method(&mut self, t: &'v TypeMethod) { walk_ty_method(self, t) }
84 fn visit_trait_item(&mut self, t: &'v TraitItem) { walk_trait_item(self, t) }
85 fn visit_trait_ref(&mut self, t: &'v TraitRef) { walk_trait_ref(self, t) }
86 fn visit_ty_param_bound(&mut self, bounds: &'v TyParamBound) {
87 walk_ty_param_bound(self, bounds)
89 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef) {
90 walk_poly_trait_ref(self, t)
92 fn visit_struct_def(&mut self, s: &'v StructDef, _: Ident, _: &'v Generics, _: NodeId) {
93 walk_struct_def(self, s)
95 fn visit_struct_field(&mut self, s: &'v StructField) { walk_struct_field(self, s) }
96 fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics) { walk_variant(self, v, g) }
98 /// Visits an optional reference to a lifetime. The `span` is the span of some surrounding
99 /// reference should opt_lifetime be None.
100 fn visit_opt_lifetime_ref(&mut self,
102 opt_lifetime: &'v Option<Lifetime>) {
103 match *opt_lifetime {
104 Some(ref l) => self.visit_lifetime_ref(l),
108 fn visit_lifetime_ref(&mut self, lifetime: &'v Lifetime) {
109 self.visit_name(lifetime.span, lifetime.name)
111 fn visit_lifetime_def(&mut self, lifetime: &'v LifetimeDef) {
112 walk_lifetime_def(self, lifetime)
114 fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
115 walk_explicit_self(self, es)
117 fn visit_mac(&mut self, _macro: &'v Mac) {
118 panic!("visit_mac disabled by default");
119 // NB: see note about macros above.
120 // if you really want a visitor that
121 // works on macros, use this
122 // definition in your trait impl:
123 // visit::walk_mac(self, _macro)
125 fn visit_path(&mut self, path: &'v Path, _id: ast::NodeId) {
126 walk_path(self, path)
128 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
129 walk_path_segment(self, path_span, path_segment)
131 fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'v PathParameters) {
132 walk_path_parameters(self, path_span, path_parameters)
134 fn visit_attribute(&mut self, _attr: &'v Attribute) {}
137 pub fn walk_inlined_item<'v,V>(visitor: &mut V, item: &'v InlinedItem)
138 where V: Visitor<'v> {
140 IIItem(ref i) => visitor.visit_item(&**i),
141 IIForeign(ref i) => visitor.visit_foreign_item(&**i),
142 IITraitItem(_, ref ti) => visitor.visit_trait_item(ti),
143 IIImplItem(_, MethodImplItem(ref m)) => {
144 walk_method_helper(visitor, &**m)
146 IIImplItem(_, TypeImplItem(ref typedef)) => {
147 visitor.visit_ident(typedef.span, typedef.ident);
148 visitor.visit_ty(&*typedef.typ);
154 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
155 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
156 for attr in krate.attrs.iter() {
157 visitor.visit_attribute(attr);
161 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
162 for view_item in module.view_items.iter() {
163 visitor.visit_view_item(view_item)
166 for item in module.items.iter() {
167 visitor.visit_item(&**item)
171 pub fn walk_view_item<'v, V: Visitor<'v>>(visitor: &mut V, vi: &'v ViewItem) {
173 ViewItemExternCrate(name, _, _) => {
174 visitor.visit_ident(vi.span, name)
176 ViewItemUse(ref vp) => {
178 ViewPathSimple(ident, ref path, id) => {
179 visitor.visit_ident(vp.span, ident);
180 visitor.visit_path(path, id);
182 ViewPathGlob(ref path, id) => {
183 visitor.visit_path(path, id);
185 ViewPathList(ref prefix, ref list, _) => {
186 for id in list.iter() {
188 PathListIdent { name, .. } => {
189 visitor.visit_ident(id.span, name);
191 PathListMod { .. } => ()
195 // Note that the `prefix` here is not a complete
196 // path, so we don't use `visit_path`.
197 walk_path(visitor, prefix);
202 for attr in vi.attrs.iter() {
203 visitor.visit_attribute(attr);
207 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
208 visitor.visit_pat(&*local.pat);
209 visitor.visit_ty(&*local.ty);
210 walk_expr_opt(visitor, &local.init);
213 pub fn walk_lifetime_def<'v, V: Visitor<'v>>(visitor: &mut V,
214 lifetime_def: &'v LifetimeDef) {
215 visitor.visit_name(lifetime_def.lifetime.span, lifetime_def.lifetime.name);
216 for bound in lifetime_def.bounds.iter() {
217 visitor.visit_lifetime_ref(bound);
221 pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V,
222 explicit_self: &'v ExplicitSelf) {
223 match explicit_self.node {
224 SelfStatic | SelfValue(_) => {},
225 SelfRegion(ref lifetime, _, _) => {
226 visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime)
228 SelfExplicit(ref typ, _) => visitor.visit_ty(&**typ),
232 /// Like with walk_method_helper this doesn't correspond to a method
233 /// in Visitor, and so it gets a _helper suffix.
234 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
235 trait_ref: &'v PolyTraitRef)
238 walk_lifetime_decls_helper(visitor, &trait_ref.bound_lifetimes);
239 visitor.visit_trait_ref(&trait_ref.trait_ref);
242 /// Like with walk_method_helper this doesn't correspond to a method
243 /// in Visitor, and so it gets a _helper suffix.
244 pub fn walk_trait_ref<'v,V>(visitor: &mut V,
245 trait_ref: &'v TraitRef)
248 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
251 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
252 visitor.visit_ident(item.span, item.ident);
254 ItemStatic(ref typ, _, ref expr) |
255 ItemConst(ref typ, ref expr) => {
256 visitor.visit_ty(&**typ);
257 visitor.visit_expr(&**expr);
259 ItemFn(ref declaration, fn_style, abi, ref generics, ref body) => {
260 visitor.visit_fn(FkItemFn(item.ident, generics, fn_style, abi),
266 ItemMod(ref module) => {
267 visitor.visit_mod(module, item.span, item.id)
269 ItemForeignMod(ref foreign_module) => {
270 for view_item in foreign_module.view_items.iter() {
271 visitor.visit_view_item(view_item)
273 for foreign_item in foreign_module.items.iter() {
274 visitor.visit_foreign_item(&**foreign_item)
277 ItemTy(ref typ, ref type_parameters) => {
278 visitor.visit_ty(&**typ);
279 visitor.visit_generics(type_parameters)
281 ItemEnum(ref enum_definition, ref type_parameters) => {
282 visitor.visit_generics(type_parameters);
283 walk_enum_def(visitor, enum_definition, type_parameters)
285 ItemImpl(ref type_parameters,
289 visitor.visit_generics(type_parameters);
290 match *trait_reference {
291 Some(ref trait_reference) => visitor.visit_trait_ref(trait_reference),
294 visitor.visit_ty(&**typ);
295 for impl_item in impl_items.iter() {
297 MethodImplItem(ref method) => {
298 walk_method_helper(visitor, &**method)
300 TypeImplItem(ref typedef) => {
301 visitor.visit_ident(typedef.span, typedef.ident);
302 visitor.visit_ty(&*typedef.typ);
307 ItemStruct(ref struct_definition, ref generics) => {
308 visitor.visit_generics(generics);
309 visitor.visit_struct_def(&**struct_definition,
314 ItemTrait(ref generics, _, ref bounds, ref methods) => {
315 visitor.visit_generics(generics);
316 walk_ty_param_bounds_helper(visitor, bounds);
317 for method in methods.iter() {
318 visitor.visit_trait_item(method)
321 ItemMac(ref macro) => visitor.visit_mac(macro),
323 for attr in item.attrs.iter() {
324 visitor.visit_attribute(attr);
328 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
329 enum_definition: &'v EnumDef,
330 generics: &'v Generics) {
331 for variant in enum_definition.variants.iter() {
332 visitor.visit_variant(&**variant, generics);
336 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
337 variant: &'v Variant,
338 generics: &'v Generics) {
339 visitor.visit_ident(variant.span, variant.node.name);
341 match variant.node.kind {
342 TupleVariantKind(ref variant_arguments) => {
343 for variant_argument in variant_arguments.iter() {
344 visitor.visit_ty(&*variant_argument.ty)
347 StructVariantKind(ref struct_definition) => {
348 visitor.visit_struct_def(&**struct_definition,
354 match variant.node.disr_expr {
355 Some(ref expr) => visitor.visit_expr(&**expr),
358 for attr in variant.node.attrs.iter() {
359 visitor.visit_attribute(attr);
363 pub fn skip_ty<'v, V: Visitor<'v>>(_: &mut V, _: &'v Ty) {
367 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
369 TyVec(ref ty) | TyParen(ref ty) => {
370 visitor.visit_ty(&**ty)
372 TyPtr(ref mutable_type) => {
373 visitor.visit_ty(&*mutable_type.ty)
375 TyRptr(ref lifetime, ref mutable_type) => {
376 visitor.visit_opt_lifetime_ref(typ.span, lifetime);
377 visitor.visit_ty(&*mutable_type.ty)
379 TyTup(ref tuple_element_types) => {
380 for tuple_element_type in tuple_element_types.iter() {
381 visitor.visit_ty(&**tuple_element_type)
384 TyClosure(ref function_declaration) => {
385 for argument in function_declaration.decl.inputs.iter() {
386 visitor.visit_ty(&*argument.ty)
388 walk_fn_ret_ty(visitor, &function_declaration.decl.output);
389 walk_ty_param_bounds_helper(visitor, &function_declaration.bounds);
390 walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
392 TyProc(ref function_declaration) => {
393 for argument in function_declaration.decl.inputs.iter() {
394 visitor.visit_ty(&*argument.ty)
396 walk_fn_ret_ty(visitor, &function_declaration.decl.output);
397 walk_ty_param_bounds_helper(visitor, &function_declaration.bounds);
398 walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
400 TyBareFn(ref function_declaration) => {
401 for argument in function_declaration.decl.inputs.iter() {
402 visitor.visit_ty(&*argument.ty)
404 walk_fn_ret_ty(visitor, &function_declaration.decl.output);
405 walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
407 TyPath(ref path, id) => {
408 visitor.visit_path(path, id);
410 TyObjectSum(ref ty, ref bounds) => {
411 visitor.visit_ty(&**ty);
412 walk_ty_param_bounds_helper(visitor, bounds);
414 TyQPath(ref qpath) => {
415 visitor.visit_ty(&*qpath.self_type);
416 visitor.visit_trait_ref(&*qpath.trait_ref);
417 visitor.visit_ident(typ.span, qpath.item_name);
419 TyFixedLengthVec(ref ty, ref expression) => {
420 visitor.visit_ty(&**ty);
421 visitor.visit_expr(&**expression)
423 TyPolyTraitRef(ref bounds) => {
424 walk_ty_param_bounds_helper(visitor, bounds)
426 TyTypeof(ref expression) => {
427 visitor.visit_expr(&**expression)
433 pub fn walk_lifetime_decls_helper<'v, V: Visitor<'v>>(visitor: &mut V,
434 lifetimes: &'v Vec<LifetimeDef>) {
435 for l in lifetimes.iter() {
436 visitor.visit_lifetime_def(l);
440 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
441 for segment in path.segments.iter() {
442 visitor.visit_path_segment(path.span, segment);
446 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
448 segment: &'v PathSegment) {
449 visitor.visit_ident(path_span, segment.identifier);
450 visitor.visit_path_parameters(path_span, &segment.parameters);
453 pub fn walk_path_parameters<'v, V: Visitor<'v>>(visitor: &mut V,
455 path_parameters: &'v PathParameters) {
456 match *path_parameters {
457 ast::AngleBracketedParameters(ref data) => {
458 for typ in data.types.iter() {
459 visitor.visit_ty(&**typ);
461 for lifetime in data.lifetimes.iter() {
462 visitor.visit_lifetime_ref(lifetime);
465 ast::ParenthesizedParameters(ref data) => {
466 for typ in data.inputs.iter() {
467 visitor.visit_ty(&**typ);
469 for typ in data.output.iter() {
470 visitor.visit_ty(&**typ);
476 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
478 PatEnum(ref path, ref children) => {
479 visitor.visit_path(path, pattern.id);
480 for children in children.iter() {
481 for child in children.iter() {
482 visitor.visit_pat(&**child)
486 PatStruct(ref path, ref fields, _) => {
487 visitor.visit_path(path, pattern.id);
488 for field in fields.iter() {
489 visitor.visit_pat(&*field.node.pat)
492 PatTup(ref tuple_elements) => {
493 for tuple_element in tuple_elements.iter() {
494 visitor.visit_pat(&**tuple_element)
497 PatBox(ref subpattern) |
498 PatRegion(ref subpattern) => {
499 visitor.visit_pat(&**subpattern)
501 PatIdent(_, ref pth1, ref optional_subpattern) => {
502 visitor.visit_ident(pth1.span, pth1.node);
503 match *optional_subpattern {
505 Some(ref subpattern) => visitor.visit_pat(&**subpattern),
508 PatLit(ref expression) => visitor.visit_expr(&**expression),
509 PatRange(ref lower_bound, ref upper_bound) => {
510 visitor.visit_expr(&**lower_bound);
511 visitor.visit_expr(&**upper_bound)
514 PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
515 for prepattern in prepattern.iter() {
516 visitor.visit_pat(&**prepattern)
518 for slice_pattern in slice_pattern.iter() {
519 visitor.visit_pat(&**slice_pattern)
521 for postpattern in postpatterns.iter() {
522 visitor.visit_pat(&**postpattern)
525 PatMac(ref macro) => visitor.visit_mac(macro),
529 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
530 foreign_item: &'v ForeignItem) {
531 visitor.visit_ident(foreign_item.span, foreign_item.ident);
533 match foreign_item.node {
534 ForeignItemFn(ref function_declaration, ref generics) => {
535 walk_fn_decl(visitor, &**function_declaration);
536 visitor.visit_generics(generics)
538 ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ),
541 for attr in foreign_item.attrs.iter() {
542 visitor.visit_attribute(attr);
546 pub fn walk_ty_param_bounds_helper<'v, V: Visitor<'v>>(visitor: &mut V,
547 bounds: &'v OwnedSlice<TyParamBound>) {
548 for bound in bounds.iter() {
549 visitor.visit_ty_param_bound(bound)
553 pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
554 bound: &'v TyParamBound) {
556 TraitTyParamBound(ref typ) => {
557 visitor.visit_poly_trait_ref(typ);
559 RegionTyParamBound(ref lifetime) => {
560 visitor.visit_lifetime_ref(lifetime);
565 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
566 for type_parameter in generics.ty_params.iter() {
567 visitor.visit_ident(type_parameter.span, type_parameter.ident);
568 walk_ty_param_bounds_helper(visitor, &type_parameter.bounds);
569 match type_parameter.default {
570 Some(ref ty) => visitor.visit_ty(&**ty),
574 walk_lifetime_decls_helper(visitor, &generics.lifetimes);
575 for predicate in generics.where_clause.predicates.iter() {
576 visitor.visit_ident(predicate.span, predicate.ident);
577 walk_ty_param_bounds_helper(visitor, &predicate.bounds);
581 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
582 if let Return(ref output_ty) = *ret_ty {
583 visitor.visit_ty(&**output_ty)
587 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
588 for argument in function_declaration.inputs.iter() {
589 visitor.visit_pat(&*argument.pat);
590 visitor.visit_ty(&*argument.ty)
592 walk_fn_ret_ty(visitor, &function_declaration.output)
595 // Note: there is no visit_method() method in the visitor, instead override
596 // visit_fn() and check for FkMethod(). I named this visit_method_helper()
597 // because it is not a default impl of any method, though I doubt that really
598 // clarifies anything. - Niko
599 pub fn walk_method_helper<'v, V: Visitor<'v>>(visitor: &mut V, method: &'v Method) {
601 MethDecl(ident, ref generics, _, _, _, ref decl, ref body, _) => {
602 visitor.visit_ident(method.span, ident);
603 visitor.visit_fn(FkMethod(ident, generics, method),
608 for attr in method.attrs.iter() {
609 visitor.visit_attribute(attr);
613 MethMac(ref mac) => visitor.visit_mac(mac)
617 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
618 function_kind: FnKind<'v>,
619 function_declaration: &'v FnDecl,
620 function_body: &'v Block,
622 walk_fn_decl(visitor, function_declaration);
624 match function_kind {
625 FkItemFn(_, generics, _, _) => {
626 visitor.visit_generics(generics);
628 FkMethod(_, generics, method) => {
629 visitor.visit_generics(generics);
631 MethDecl(_, _, _, ref explicit_self, _, _, _, _) =>
632 visitor.visit_explicit_self(explicit_self),
634 visitor.visit_mac(mac)
640 visitor.visit_block(function_body)
643 pub fn walk_ty_method<'v, V: Visitor<'v>>(visitor: &mut V, method_type: &'v TypeMethod) {
644 visitor.visit_ident(method_type.span, method_type.ident);
645 visitor.visit_explicit_self(&method_type.explicit_self);
646 for argument_type in method_type.decl.inputs.iter() {
647 visitor.visit_ty(&*argument_type.ty)
649 visitor.visit_generics(&method_type.generics);
650 walk_fn_ret_ty(visitor, &method_type.decl.output);
651 for attr in method_type.attrs.iter() {
652 visitor.visit_attribute(attr);
656 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_method: &'v TraitItem) {
657 match *trait_method {
658 RequiredMethod(ref method_type) => visitor.visit_ty_method(method_type),
659 ProvidedMethod(ref method) => walk_method_helper(visitor, &**method),
660 TypeTraitItem(ref associated_type) => {
661 visitor.visit_ident(associated_type.ty_param.span,
662 associated_type.ty_param.ident)
667 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
668 struct_definition: &'v StructDef) {
669 for field in struct_definition.fields.iter() {
670 visitor.visit_struct_field(field)
674 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
675 struct_field: &'v StructField) {
676 if let NamedField(name, _) = struct_field.node.kind {
677 visitor.visit_ident(struct_field.span, name);
680 visitor.visit_ty(&*struct_field.node.ty);
682 for attr in struct_field.node.attrs.iter() {
683 visitor.visit_attribute(attr);
687 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
688 for view_item in block.view_items.iter() {
689 visitor.visit_view_item(view_item)
691 for statement in block.stmts.iter() {
692 visitor.visit_stmt(&**statement)
694 walk_expr_opt(visitor, &block.expr)
697 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
698 match statement.node {
699 StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration),
700 StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
701 visitor.visit_expr(&**expression)
703 StmtMac(ref macro, _) => visitor.visit_mac(macro),
707 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
708 match declaration.node {
709 DeclLocal(ref local) => visitor.visit_local(&**local),
710 DeclItem(ref item) => visitor.visit_item(&**item),
714 pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V,
715 optional_expression: &'v Option<P<Expr>>) {
716 match *optional_expression {
718 Some(ref expression) => visitor.visit_expr(&**expression),
722 pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) {
723 for expression in expressions.iter() {
724 visitor.visit_expr(&**expression)
728 pub fn walk_mac<'v, V: Visitor<'v>>(_: &mut V, _: &'v Mac) {
732 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
733 match expression.node {
734 ExprBox(ref place, ref subexpression) => {
735 visitor.visit_expr(&**place);
736 visitor.visit_expr(&**subexpression)
738 ExprVec(ref subexpressions) => {
739 walk_exprs(visitor, subexpressions.as_slice())
741 ExprRepeat(ref element, ref count) => {
742 visitor.visit_expr(&**element);
743 visitor.visit_expr(&**count)
745 ExprStruct(ref path, ref fields, ref optional_base) => {
746 visitor.visit_path(path, expression.id);
747 for field in fields.iter() {
748 visitor.visit_expr(&*field.expr)
750 walk_expr_opt(visitor, optional_base)
752 ExprTup(ref subexpressions) => {
753 for subexpression in subexpressions.iter() {
754 visitor.visit_expr(&**subexpression)
757 ExprCall(ref callee_expression, ref arguments) => {
758 for argument in arguments.iter() {
759 visitor.visit_expr(&**argument)
761 visitor.visit_expr(&**callee_expression)
763 ExprMethodCall(_, ref types, ref arguments) => {
764 walk_exprs(visitor, arguments.as_slice());
765 for typ in types.iter() {
766 visitor.visit_ty(&**typ)
769 ExprBinary(_, ref left_expression, ref right_expression) => {
770 visitor.visit_expr(&**left_expression);
771 visitor.visit_expr(&**right_expression)
773 ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
774 visitor.visit_expr(&**subexpression)
777 ExprCast(ref subexpression, ref typ) => {
778 visitor.visit_expr(&**subexpression);
779 visitor.visit_ty(&**typ)
781 ExprIf(ref head_expression, ref if_block, ref optional_else) => {
782 visitor.visit_expr(&**head_expression);
783 visitor.visit_block(&**if_block);
784 walk_expr_opt(visitor, optional_else)
786 ExprWhile(ref subexpression, ref block, _) => {
787 visitor.visit_expr(&**subexpression);
788 visitor.visit_block(&**block)
790 ExprIfLet(ref pattern, ref subexpression, ref if_block, ref optional_else) => {
791 visitor.visit_pat(&**pattern);
792 visitor.visit_expr(&**subexpression);
793 visitor.visit_block(&**if_block);
794 walk_expr_opt(visitor, optional_else);
796 ExprWhileLet(ref pattern, ref subexpression, ref block, _) => {
797 visitor.visit_pat(&**pattern);
798 visitor.visit_expr(&**subexpression);
799 visitor.visit_block(&**block);
801 ExprForLoop(ref pattern, ref subexpression, ref block, _) => {
802 visitor.visit_pat(&**pattern);
803 visitor.visit_expr(&**subexpression);
804 visitor.visit_block(&**block)
806 ExprLoop(ref block, _) => visitor.visit_block(&**block),
807 ExprMatch(ref subexpression, ref arms, _) => {
808 visitor.visit_expr(&**subexpression);
809 for arm in arms.iter() {
810 visitor.visit_arm(arm)
813 ExprClosure(_, _, ref function_declaration, ref body) => {
814 visitor.visit_fn(FkFnBlock,
815 &**function_declaration,
820 ExprProc(ref function_declaration, ref body) => {
821 visitor.visit_fn(FkFnBlock,
822 &**function_declaration,
827 ExprBlock(ref block) => visitor.visit_block(&**block),
828 ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
829 visitor.visit_expr(&**right_hand_expression);
830 visitor.visit_expr(&**left_hand_expression)
832 ExprAssignOp(_, ref left_expression, ref right_expression) => {
833 visitor.visit_expr(&**right_expression);
834 visitor.visit_expr(&**left_expression)
836 ExprField(ref subexpression, _) => {
837 visitor.visit_expr(&**subexpression);
839 ExprTupField(ref subexpression, _) => {
840 visitor.visit_expr(&**subexpression);
842 ExprIndex(ref main_expression, ref index_expression) => {
843 visitor.visit_expr(&**main_expression);
844 visitor.visit_expr(&**index_expression)
846 ExprSlice(ref main_expression, ref start, ref end, _) => {
847 visitor.visit_expr(&**main_expression);
848 walk_expr_opt(visitor, start);
849 walk_expr_opt(visitor, end)
851 ExprPath(ref path) => {
852 visitor.visit_path(path, expression.id)
854 ExprBreak(_) | ExprAgain(_) => {}
855 ExprRet(ref optional_expression) => {
856 walk_expr_opt(visitor, optional_expression)
858 ExprMac(ref macro) => visitor.visit_mac(macro),
859 ExprParen(ref subexpression) => {
860 visitor.visit_expr(&**subexpression)
862 ExprInlineAsm(ref ia) => {
863 for input in ia.inputs.iter() {
864 let (_, ref input) = *input;
865 visitor.visit_expr(&**input)
867 for output in ia.outputs.iter() {
868 let (_, ref output, _) = *output;
869 visitor.visit_expr(&**output)
874 visitor.visit_expr_post(expression)
877 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
878 for pattern in arm.pats.iter() {
879 visitor.visit_pat(&**pattern)
881 walk_expr_opt(visitor, &arm.guard);
882 visitor.visit_expr(&*arm.body);
883 for attr in arm.attrs.iter() {
884 visitor.visit_attribute(attr);