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;
37 /// fn foo() or extern "Abi" fn foo()
38 FkItemFn(Ident, &'a Generics, Unsafety, Abi),
41 FkMethod(Ident, &'a Generics, &'a Method),
48 /// Each method of the Visitor trait is a hook to be potentially
49 /// overridden. Each method's default implementation recursively visits
50 /// the substructure of the input via the corresponding `walk` method;
51 /// e.g. the `visit_mod` method by default calls `visit::walk_mod`.
53 /// If you want to ensure that your code handles every variant
54 /// explicitly, you need to override each method. (And you also need
55 /// to monitor future changes to `Visitor` in case a new method with a
56 /// new default implementation gets introduced.)
57 pub trait Visitor<'v> : Sized {
58 fn visit_name(&mut self, _span: Span, _name: Name) {
61 fn visit_ident(&mut self, span: Span, ident: Ident) {
62 self.visit_name(span, ident.name);
64 fn visit_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) { walk_mod(self, m) }
65 fn visit_foreign_item(&mut self, i: &'v ForeignItem) { walk_foreign_item(self, i) }
66 fn visit_item(&mut self, i: &'v Item) { walk_item(self, i) }
67 fn visit_local(&mut self, l: &'v Local) { walk_local(self, l) }
68 fn visit_block(&mut self, b: &'v Block) { walk_block(self, b) }
69 fn visit_stmt(&mut self, s: &'v Stmt) { walk_stmt(self, s) }
70 fn visit_arm(&mut self, a: &'v Arm) { walk_arm(self, a) }
71 fn visit_pat(&mut self, p: &'v Pat) { walk_pat(self, p) }
72 fn visit_decl(&mut self, d: &'v Decl) { walk_decl(self, d) }
73 fn visit_expr(&mut self, ex: &'v Expr) { walk_expr(self, ex) }
74 fn visit_expr_post(&mut self, _ex: &'v Expr) { }
75 fn visit_ty(&mut self, t: &'v Ty) { walk_ty(self, t) }
76 fn visit_generics(&mut self, g: &'v Generics) { walk_generics(self, g) }
77 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: &'v Block, s: Span, _: NodeId) {
78 walk_fn(self, fk, fd, b, s)
80 fn visit_ty_method(&mut self, t: &'v TypeMethod) { walk_ty_method(self, t) }
81 fn visit_trait_item(&mut self, t: &'v TraitItem) { walk_trait_item(self, t) }
82 fn visit_trait_ref(&mut self, t: &'v TraitRef) { walk_trait_ref(self, t) }
83 fn visit_ty_param_bound(&mut self, bounds: &'v TyParamBound) {
84 walk_ty_param_bound(self, bounds)
86 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: &'v TraitBoundModifier) {
87 walk_poly_trait_ref(self, t, m)
89 fn visit_struct_def(&mut self, s: &'v StructDef, _: Ident, _: &'v Generics, _: NodeId) {
90 walk_struct_def(self, s)
92 fn visit_struct_field(&mut self, s: &'v StructField) { walk_struct_field(self, s) }
93 fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics) { walk_variant(self, v, g) }
95 /// Visits an optional reference to a lifetime. The `span` is the span of some surrounding
96 /// reference should opt_lifetime be None.
97 fn visit_opt_lifetime_ref(&mut self,
99 opt_lifetime: &'v Option<Lifetime>) {
100 match *opt_lifetime {
101 Some(ref l) => self.visit_lifetime_ref(l),
105 fn visit_lifetime_bound(&mut self, lifetime: &'v Lifetime) {
106 walk_lifetime_bound(self, lifetime)
108 fn visit_lifetime_ref(&mut self, lifetime: &'v Lifetime) {
109 walk_lifetime_ref(self, lifetime)
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, _mac: &'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, _mac)
125 fn visit_path(&mut self, path: &'v Path, _id: ast::NodeId) {
126 walk_path(self, path)
128 fn visit_qpath(&mut self, qpath_span: Span, qpath: &'v QPath) {
129 walk_qpath(self, qpath_span, qpath)
131 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
132 walk_path_segment(self, path_span, path_segment)
134 fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'v PathParameters) {
135 walk_path_parameters(self, path_span, path_parameters)
137 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
138 walk_assoc_type_binding(self, type_binding)
140 fn visit_attribute(&mut self, _attr: &'v Attribute) {}
143 pub fn walk_inlined_item<'v,V>(visitor: &mut V, item: &'v InlinedItem)
144 where V: Visitor<'v> {
146 IIItem(ref i) => visitor.visit_item(&**i),
147 IIForeign(ref i) => visitor.visit_foreign_item(&**i),
148 IITraitItem(_, ref ti) => visitor.visit_trait_item(ti),
149 IIImplItem(_, MethodImplItem(ref m)) => {
150 walk_method_helper(visitor, &**m)
152 IIImplItem(_, TypeImplItem(ref typedef)) => {
153 visitor.visit_ident(typedef.span, typedef.ident);
154 visitor.visit_ty(&*typedef.typ);
160 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
161 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
162 for attr in &krate.attrs {
163 visitor.visit_attribute(attr);
167 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
168 for item in &module.items {
169 visitor.visit_item(&**item)
173 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
174 visitor.visit_pat(&*local.pat);
175 walk_ty_opt(visitor, &local.ty);
176 walk_expr_opt(visitor, &local.init);
179 pub fn walk_lifetime_def<'v, V: Visitor<'v>>(visitor: &mut V,
180 lifetime_def: &'v LifetimeDef) {
181 visitor.visit_name(lifetime_def.lifetime.span, lifetime_def.lifetime.name);
182 for bound in &lifetime_def.bounds {
183 visitor.visit_lifetime_bound(bound);
187 pub fn walk_lifetime_bound<'v, V: Visitor<'v>>(visitor: &mut V,
188 lifetime_ref: &'v Lifetime) {
189 visitor.visit_lifetime_ref(lifetime_ref)
192 pub fn walk_lifetime_ref<'v, V: Visitor<'v>>(visitor: &mut V,
193 lifetime_ref: &'v Lifetime) {
194 visitor.visit_name(lifetime_ref.span, lifetime_ref.name)
197 pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V,
198 explicit_self: &'v ExplicitSelf) {
199 match explicit_self.node {
200 SelfStatic | SelfValue(_) => {},
201 SelfRegion(ref lifetime, _, _) => {
202 visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime)
204 SelfExplicit(ref typ, _) => visitor.visit_ty(&**typ),
208 /// Like with walk_method_helper this doesn't correspond to a method
209 /// in Visitor, and so it gets a _helper suffix.
210 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
211 trait_ref: &'v PolyTraitRef,
212 _modifier: &'v TraitBoundModifier)
215 walk_lifetime_decls_helper(visitor, &trait_ref.bound_lifetimes);
216 visitor.visit_trait_ref(&trait_ref.trait_ref);
219 /// Like with walk_method_helper this doesn't correspond to a method
220 /// in Visitor, and so it gets a _helper suffix.
221 pub fn walk_trait_ref<'v,V>(visitor: &mut V,
222 trait_ref: &'v TraitRef)
225 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
228 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
229 visitor.visit_ident(item.span, item.ident);
231 ItemExternCrate(..) => {}
234 ViewPathSimple(ident, ref path) => {
235 visitor.visit_ident(vp.span, ident);
236 visitor.visit_path(path, item.id);
238 ViewPathGlob(ref path) => {
239 visitor.visit_path(path, item.id);
241 ViewPathList(ref prefix, ref list) => {
244 PathListIdent { name, .. } => {
245 visitor.visit_ident(id.span, name);
247 PathListMod { .. } => ()
251 // Note that the `prefix` here is not a complete
252 // path, so we don't use `visit_path`.
253 walk_path(visitor, prefix);
257 ItemStatic(ref typ, _, ref expr) |
258 ItemConst(ref typ, ref expr) => {
259 visitor.visit_ty(&**typ);
260 visitor.visit_expr(&**expr);
262 ItemFn(ref declaration, fn_style, abi, ref generics, ref body) => {
263 visitor.visit_fn(FkItemFn(item.ident, generics, fn_style, abi),
269 ItemMod(ref module) => {
270 visitor.visit_mod(module, item.span, item.id)
272 ItemForeignMod(ref foreign_module) => {
273 for foreign_item in &foreign_module.items {
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)
290 visitor.visit_generics(type_parameters);
291 match *trait_reference {
292 Some(ref trait_reference) => visitor.visit_trait_ref(trait_reference),
295 visitor.visit_ty(&**typ);
296 for impl_item in impl_items {
298 MethodImplItem(ref method) => {
299 walk_method_helper(visitor, &**method)
301 TypeImplItem(ref typedef) => {
302 visitor.visit_ident(typedef.span, typedef.ident);
303 visitor.visit_ty(&*typedef.typ);
308 ItemStruct(ref struct_definition, ref generics) => {
309 visitor.visit_generics(generics);
310 visitor.visit_struct_def(&**struct_definition,
315 ItemTrait(_, ref generics, ref bounds, ref methods) => {
316 visitor.visit_generics(generics);
317 walk_ty_param_bounds_helper(visitor, bounds);
318 for method in methods {
319 visitor.visit_trait_item(method)
322 ItemMac(ref mac) => visitor.visit_mac(mac),
324 for attr in &item.attrs {
325 visitor.visit_attribute(attr);
329 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
330 enum_definition: &'v EnumDef,
331 generics: &'v Generics) {
332 for variant in &enum_definition.variants {
333 visitor.visit_variant(&**variant, generics);
337 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
338 variant: &'v Variant,
339 generics: &'v Generics) {
340 visitor.visit_ident(variant.span, variant.node.name);
342 match variant.node.kind {
343 TupleVariantKind(ref variant_arguments) => {
344 for variant_argument in variant_arguments {
345 visitor.visit_ty(&*variant_argument.ty)
348 StructVariantKind(ref struct_definition) => {
349 visitor.visit_struct_def(&**struct_definition,
355 match variant.node.disr_expr {
356 Some(ref expr) => visitor.visit_expr(&**expr),
359 for attr in &variant.node.attrs {
360 visitor.visit_attribute(attr);
364 pub fn skip_ty<'v, V: Visitor<'v>>(_: &mut V, _: &'v Ty) {
368 pub fn walk_ty_opt<'v, V: Visitor<'v>>(visitor: &mut V, optional_type: &'v Option<P<Ty>>) {
369 match *optional_type {
370 Some(ref ty) => visitor.visit_ty(&**ty),
375 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
377 TyVec(ref ty) | TyParen(ref ty) => {
378 visitor.visit_ty(&**ty)
380 TyPtr(ref mutable_type) => {
381 visitor.visit_ty(&*mutable_type.ty)
383 TyRptr(ref lifetime, ref mutable_type) => {
384 visitor.visit_opt_lifetime_ref(typ.span, lifetime);
385 visitor.visit_ty(&*mutable_type.ty)
387 TyTup(ref tuple_element_types) => {
388 for tuple_element_type in tuple_element_types {
389 visitor.visit_ty(&**tuple_element_type)
392 TyBareFn(ref function_declaration) => {
393 for argument in &function_declaration.decl.inputs {
394 visitor.visit_ty(&*argument.ty)
396 walk_fn_ret_ty(visitor, &function_declaration.decl.output);
397 walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
399 TyPath(ref path, id) => {
400 visitor.visit_path(path, id);
402 TyObjectSum(ref ty, ref bounds) => {
403 visitor.visit_ty(&**ty);
404 walk_ty_param_bounds_helper(visitor, bounds);
406 TyQPath(ref qpath) => {
407 visitor.visit_qpath(typ.span, &**qpath);
409 TyFixedLengthVec(ref ty, ref expression) => {
410 visitor.visit_ty(&**ty);
411 visitor.visit_expr(&**expression)
413 TyPolyTraitRef(ref bounds) => {
414 walk_ty_param_bounds_helper(visitor, bounds)
416 TyTypeof(ref expression) => {
417 visitor.visit_expr(&**expression)
423 pub fn walk_lifetime_decls_helper<'v, V: Visitor<'v>>(visitor: &mut V,
424 lifetimes: &'v Vec<LifetimeDef>) {
426 visitor.visit_lifetime_def(l);
430 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
431 for segment in &path.segments {
432 visitor.visit_path_segment(path.span, segment);
436 pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V,
439 visitor.visit_ty(&*qpath.self_type);
440 visitor.visit_trait_ref(&*qpath.trait_ref);
441 visitor.visit_path_segment(qpath_span, &qpath.item_path);
444 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
446 segment: &'v PathSegment) {
447 visitor.visit_ident(path_span, segment.identifier);
448 visitor.visit_path_parameters(path_span, &segment.parameters);
451 pub fn walk_path_parameters<'v, V: Visitor<'v>>(visitor: &mut V,
453 path_parameters: &'v PathParameters) {
454 match *path_parameters {
455 ast::AngleBracketedParameters(ref data) => {
456 for typ in &*data.types {
457 visitor.visit_ty(&**typ);
459 for lifetime in &data.lifetimes {
460 visitor.visit_lifetime_ref(lifetime);
462 for binding in &*data.bindings {
463 visitor.visit_assoc_type_binding(&**binding);
466 ast::ParenthesizedParameters(ref data) => {
467 for typ in &data.inputs {
468 visitor.visit_ty(&**typ);
470 if let Some(ref typ) = data.output {
471 visitor.visit_ty(&**typ);
477 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
478 type_binding: &'v TypeBinding) {
479 visitor.visit_ident(type_binding.span, type_binding.ident);
480 visitor.visit_ty(&*type_binding.ty);
483 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
485 PatEnum(ref path, ref children) => {
486 visitor.visit_path(path, pattern.id);
487 if let Some(ref children) = *children {
488 for child in children {
489 visitor.visit_pat(&*child)
493 PatStruct(ref path, ref fields, _) => {
494 visitor.visit_path(path, pattern.id);
495 for field in fields {
496 visitor.visit_pat(&*field.node.pat)
499 PatTup(ref tuple_elements) => {
500 for tuple_element in tuple_elements {
501 visitor.visit_pat(&**tuple_element)
504 PatBox(ref subpattern) |
505 PatRegion(ref subpattern, _) => {
506 visitor.visit_pat(&**subpattern)
508 PatIdent(_, ref pth1, ref optional_subpattern) => {
509 visitor.visit_ident(pth1.span, pth1.node);
510 match *optional_subpattern {
512 Some(ref subpattern) => visitor.visit_pat(&**subpattern),
515 PatLit(ref expression) => visitor.visit_expr(&**expression),
516 PatRange(ref lower_bound, ref upper_bound) => {
517 visitor.visit_expr(&**lower_bound);
518 visitor.visit_expr(&**upper_bound)
521 PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
522 for prepattern in prepattern {
523 visitor.visit_pat(&**prepattern)
525 if let Some(ref slice_pattern) = *slice_pattern {
526 visitor.visit_pat(&**slice_pattern)
528 for postpattern in postpatterns {
529 visitor.visit_pat(&**postpattern)
532 PatMac(ref mac) => visitor.visit_mac(mac),
536 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
537 foreign_item: &'v ForeignItem) {
538 visitor.visit_ident(foreign_item.span, foreign_item.ident);
540 match foreign_item.node {
541 ForeignItemFn(ref function_declaration, ref generics) => {
542 walk_fn_decl(visitor, &**function_declaration);
543 visitor.visit_generics(generics)
545 ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ),
548 for attr in &foreign_item.attrs {
549 visitor.visit_attribute(attr);
553 pub fn walk_ty_param_bounds_helper<'v, V: Visitor<'v>>(visitor: &mut V,
554 bounds: &'v OwnedSlice<TyParamBound>) {
555 for bound in &**bounds {
556 visitor.visit_ty_param_bound(bound)
560 pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
561 bound: &'v TyParamBound) {
563 TraitTyParamBound(ref typ, ref modifier) => {
564 visitor.visit_poly_trait_ref(typ, modifier);
566 RegionTyParamBound(ref lifetime) => {
567 visitor.visit_lifetime_bound(lifetime);
572 pub fn walk_ty_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v TyParam) {
573 visitor.visit_ident(param.span, param.ident);
574 walk_ty_param_bounds_helper(visitor, ¶m.bounds);
575 walk_ty_opt(visitor, ¶m.default);
578 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
579 for type_parameter in &*generics.ty_params {
580 walk_ty_param(visitor, type_parameter);
582 walk_lifetime_decls_helper(visitor, &generics.lifetimes);
583 for predicate in &generics.where_clause.predicates {
585 &ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bounded_ty,
588 visitor.visit_ty(&**bounded_ty);
589 walk_ty_param_bounds_helper(visitor, bounds);
591 &ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
594 visitor.visit_lifetime_ref(lifetime);
596 for bound in bounds {
597 visitor.visit_lifetime_ref(bound);
600 &ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{id,
604 visitor.visit_path(path, id);
605 visitor.visit_ty(&**ty);
611 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
612 if let Return(ref output_ty) = *ret_ty {
613 visitor.visit_ty(&**output_ty)
617 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
618 for argument in &function_declaration.inputs {
619 visitor.visit_pat(&*argument.pat);
620 visitor.visit_ty(&*argument.ty)
622 walk_fn_ret_ty(visitor, &function_declaration.output)
625 // Note: there is no visit_method() method in the visitor, instead override
626 // visit_fn() and check for FkMethod(). I named this visit_method_helper()
627 // because it is not a default impl of any method, though I doubt that really
628 // clarifies anything. - Niko
629 pub fn walk_method_helper<'v, V: Visitor<'v>>(visitor: &mut V, method: &'v Method) {
631 MethDecl(ident, ref generics, _, _, _, ref decl, ref body, _) => {
632 visitor.visit_ident(method.span, ident);
633 visitor.visit_fn(FkMethod(ident, generics, method),
638 for attr in &method.attrs {
639 visitor.visit_attribute(attr);
643 MethMac(ref mac) => visitor.visit_mac(mac)
647 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
648 function_kind: FnKind<'v>,
649 function_declaration: &'v FnDecl,
650 function_body: &'v Block,
652 walk_fn_decl(visitor, function_declaration);
654 match function_kind {
655 FkItemFn(_, generics, _, _) => {
656 visitor.visit_generics(generics);
658 FkMethod(_, generics, method) => {
659 visitor.visit_generics(generics);
661 MethDecl(_, _, _, ref explicit_self, _, _, _, _) =>
662 visitor.visit_explicit_self(explicit_self),
664 visitor.visit_mac(mac)
670 visitor.visit_block(function_body)
673 pub fn walk_ty_method<'v, V: Visitor<'v>>(visitor: &mut V, method_type: &'v TypeMethod) {
674 visitor.visit_ident(method_type.span, method_type.ident);
675 visitor.visit_explicit_self(&method_type.explicit_self);
676 for argument_type in &method_type.decl.inputs {
677 visitor.visit_ty(&*argument_type.ty)
679 visitor.visit_generics(&method_type.generics);
680 walk_fn_ret_ty(visitor, &method_type.decl.output);
681 for attr in &method_type.attrs {
682 visitor.visit_attribute(attr);
686 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_method: &'v TraitItem) {
687 match *trait_method {
688 RequiredMethod(ref method_type) => visitor.visit_ty_method(method_type),
689 ProvidedMethod(ref method) => walk_method_helper(visitor, &**method),
690 TypeTraitItem(ref associated_type) => {
691 walk_ty_param(visitor, &associated_type.ty_param);
696 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
697 struct_definition: &'v StructDef) {
698 for field in &struct_definition.fields {
699 visitor.visit_struct_field(field)
703 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
704 struct_field: &'v StructField) {
705 if let NamedField(name, _) = struct_field.node.kind {
706 visitor.visit_ident(struct_field.span, name);
709 visitor.visit_ty(&*struct_field.node.ty);
711 for attr in &struct_field.node.attrs {
712 visitor.visit_attribute(attr);
716 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
717 for statement in &block.stmts {
718 visitor.visit_stmt(&**statement)
720 walk_expr_opt(visitor, &block.expr)
723 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
724 match statement.node {
725 StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration),
726 StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
727 visitor.visit_expr(&**expression)
729 StmtMac(ref mac, _) => visitor.visit_mac(&**mac),
733 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
734 match declaration.node {
735 DeclLocal(ref local) => visitor.visit_local(&**local),
736 DeclItem(ref item) => visitor.visit_item(&**item),
740 pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V,
741 optional_expression: &'v Option<P<Expr>>) {
742 match *optional_expression {
744 Some(ref expression) => visitor.visit_expr(&**expression),
748 pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) {
749 for expression in expressions {
750 visitor.visit_expr(&**expression)
754 pub fn walk_mac<'v, V: Visitor<'v>>(_: &mut V, _: &'v Mac) {
758 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
759 match expression.node {
760 ExprBox(ref place, ref subexpression) => {
761 place.as_ref().map(|e|visitor.visit_expr(&**e));
762 visitor.visit_expr(&**subexpression)
764 ExprVec(ref subexpressions) => {
765 walk_exprs(visitor, subexpressions)
767 ExprRepeat(ref element, ref count) => {
768 visitor.visit_expr(&**element);
769 visitor.visit_expr(&**count)
771 ExprStruct(ref path, ref fields, ref optional_base) => {
772 visitor.visit_path(path, expression.id);
773 for field in fields {
774 visitor.visit_expr(&*field.expr)
776 walk_expr_opt(visitor, optional_base)
778 ExprTup(ref subexpressions) => {
779 for subexpression in subexpressions {
780 visitor.visit_expr(&**subexpression)
783 ExprCall(ref callee_expression, ref arguments) => {
784 for argument in arguments {
785 visitor.visit_expr(&**argument)
787 visitor.visit_expr(&**callee_expression)
789 ExprMethodCall(_, ref types, ref arguments) => {
790 walk_exprs(visitor, arguments);
792 visitor.visit_ty(&**typ)
795 ExprBinary(_, ref left_expression, ref right_expression) => {
796 visitor.visit_expr(&**left_expression);
797 visitor.visit_expr(&**right_expression)
799 ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
800 visitor.visit_expr(&**subexpression)
803 ExprCast(ref subexpression, ref typ) => {
804 visitor.visit_expr(&**subexpression);
805 visitor.visit_ty(&**typ)
807 ExprIf(ref head_expression, ref if_block, ref optional_else) => {
808 visitor.visit_expr(&**head_expression);
809 visitor.visit_block(&**if_block);
810 walk_expr_opt(visitor, optional_else)
812 ExprWhile(ref subexpression, ref block, _) => {
813 visitor.visit_expr(&**subexpression);
814 visitor.visit_block(&**block)
816 ExprIfLet(ref pattern, ref subexpression, ref if_block, ref optional_else) => {
817 visitor.visit_pat(&**pattern);
818 visitor.visit_expr(&**subexpression);
819 visitor.visit_block(&**if_block);
820 walk_expr_opt(visitor, optional_else);
822 ExprWhileLet(ref pattern, ref subexpression, ref block, _) => {
823 visitor.visit_pat(&**pattern);
824 visitor.visit_expr(&**subexpression);
825 visitor.visit_block(&**block);
827 ExprForLoop(ref pattern, ref subexpression, ref block, _) => {
828 visitor.visit_pat(&**pattern);
829 visitor.visit_expr(&**subexpression);
830 visitor.visit_block(&**block)
832 ExprLoop(ref block, _) => visitor.visit_block(&**block),
833 ExprMatch(ref subexpression, ref arms, _) => {
834 visitor.visit_expr(&**subexpression);
836 visitor.visit_arm(arm)
839 ExprClosure(_, ref function_declaration, ref body) => {
840 visitor.visit_fn(FkFnBlock,
841 &**function_declaration,
846 ExprBlock(ref block) => visitor.visit_block(&**block),
847 ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
848 visitor.visit_expr(&**right_hand_expression);
849 visitor.visit_expr(&**left_hand_expression)
851 ExprAssignOp(_, ref left_expression, ref right_expression) => {
852 visitor.visit_expr(&**right_expression);
853 visitor.visit_expr(&**left_expression)
855 ExprField(ref subexpression, _) => {
856 visitor.visit_expr(&**subexpression);
858 ExprTupField(ref subexpression, _) => {
859 visitor.visit_expr(&**subexpression);
861 ExprIndex(ref main_expression, ref index_expression) => {
862 visitor.visit_expr(&**main_expression);
863 visitor.visit_expr(&**index_expression)
865 ExprRange(ref start, ref end) => {
866 walk_expr_opt(visitor, start);
867 walk_expr_opt(visitor, end)
869 ExprPath(ref path) => {
870 visitor.visit_path(path, expression.id)
872 ExprQPath(ref qpath) => {
873 visitor.visit_qpath(expression.span, &**qpath)
875 ExprBreak(_) | ExprAgain(_) => {}
876 ExprRet(ref optional_expression) => {
877 walk_expr_opt(visitor, optional_expression)
879 ExprMac(ref mac) => visitor.visit_mac(mac),
880 ExprParen(ref subexpression) => {
881 visitor.visit_expr(&**subexpression)
883 ExprInlineAsm(ref ia) => {
884 for input in &ia.inputs {
885 let (_, ref input) = *input;
886 visitor.visit_expr(&**input)
888 for output in &ia.outputs {
889 let (_, ref output, _) = *output;
890 visitor.visit_expr(&**output)
895 visitor.visit_expr_post(expression)
898 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
899 for pattern in &arm.pats {
900 visitor.visit_pat(&**pattern)
902 walk_expr_opt(visitor, &arm.guard);
903 visitor.visit_expr(&*arm.body);
904 for attr in &arm.attrs {
905 visitor.visit_attribute(attr);