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_view_item(&mut self, i: &'v ViewItem) { walk_view_item(self, i) }
66 fn visit_foreign_item(&mut self, i: &'v ForeignItem) { walk_foreign_item(self, i) }
67 fn visit_item(&mut self, i: &'v Item) { walk_item(self, i) }
68 fn visit_local(&mut self, l: &'v Local) { walk_local(self, l) }
69 fn visit_block(&mut self, b: &'v Block) { walk_block(self, b) }
70 fn visit_stmt(&mut self, s: &'v Stmt) { walk_stmt(self, s) }
71 fn visit_arm(&mut self, a: &'v Arm) { walk_arm(self, a) }
72 fn visit_pat(&mut self, p: &'v Pat) { walk_pat(self, p) }
73 fn visit_decl(&mut self, d: &'v Decl) { walk_decl(self, d) }
74 fn visit_expr(&mut self, ex: &'v Expr) { walk_expr(self, ex) }
75 fn visit_expr_post(&mut self, _ex: &'v Expr) { }
76 fn visit_ty(&mut self, t: &'v Ty) { walk_ty(self, t) }
77 fn visit_generics(&mut self, g: &'v Generics) { walk_generics(self, g) }
78 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: &'v Block, s: Span, _: NodeId) {
79 walk_fn(self, fk, fd, b, s)
81 fn visit_ty_method(&mut self, t: &'v TypeMethod) { walk_ty_method(self, t) }
82 fn visit_trait_item(&mut self, t: &'v TraitItem) { walk_trait_item(self, t) }
83 fn visit_trait_ref(&mut self, t: &'v TraitRef) { walk_trait_ref(self, t) }
84 fn visit_ty_param_bound(&mut self, bounds: &'v TyParamBound) {
85 walk_ty_param_bound(self, bounds)
87 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: &'v TraitBoundModifier) {
88 walk_poly_trait_ref(self, t, m)
90 fn visit_struct_def(&mut self, s: &'v StructDef, _: Ident, _: &'v Generics, _: NodeId) {
91 walk_struct_def(self, s)
93 fn visit_struct_field(&mut self, s: &'v StructField) { walk_struct_field(self, s) }
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_mac(&mut self, _macro: &'v Mac) {
119 panic!("visit_mac disabled by default");
120 // NB: see note about macros above.
121 // if you really want a visitor that
122 // works on macros, use this
123 // definition in your trait impl:
124 // visit::walk_mac(self, _macro)
126 fn visit_path(&mut self, path: &'v Path, _id: ast::NodeId) {
127 walk_path(self, path)
129 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
130 walk_path_segment(self, path_span, path_segment)
132 fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'v PathParameters) {
133 walk_path_parameters(self, path_span, path_parameters)
135 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
136 walk_assoc_type_binding(self, type_binding)
138 fn visit_attribute(&mut self, _attr: &'v Attribute) {}
141 pub fn walk_inlined_item<'v,V>(visitor: &mut V, item: &'v InlinedItem)
142 where V: Visitor<'v> {
144 IIItem(ref i) => visitor.visit_item(&**i),
145 IIForeign(ref i) => visitor.visit_foreign_item(&**i),
146 IITraitItem(_, ref ti) => visitor.visit_trait_item(ti),
147 IIImplItem(_, MethodImplItem(ref m)) => {
148 walk_method_helper(visitor, &**m)
150 IIImplItem(_, TypeImplItem(ref typedef)) => {
151 visitor.visit_ident(typedef.span, typedef.ident);
152 visitor.visit_ty(&*typedef.typ);
158 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
159 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
160 for attr in krate.attrs.iter() {
161 visitor.visit_attribute(attr);
165 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
166 for view_item in module.view_items.iter() {
167 visitor.visit_view_item(view_item)
170 for item in module.items.iter() {
171 visitor.visit_item(&**item)
175 pub fn walk_view_item<'v, V: Visitor<'v>>(visitor: &mut V, vi: &'v ViewItem) {
177 ViewItemExternCrate(name, _, _) => {
178 visitor.visit_ident(vi.span, name)
180 ViewItemUse(ref vp) => {
182 ViewPathSimple(ident, ref path, id) => {
183 visitor.visit_ident(vp.span, ident);
184 visitor.visit_path(path, id);
186 ViewPathGlob(ref path, id) => {
187 visitor.visit_path(path, id);
189 ViewPathList(ref prefix, ref list, _) => {
190 for id in list.iter() {
192 PathListIdent { name, .. } => {
193 visitor.visit_ident(id.span, name);
195 PathListMod { .. } => ()
199 // Note that the `prefix` here is not a complete
200 // path, so we don't use `visit_path`.
201 walk_path(visitor, prefix);
206 for attr in vi.attrs.iter() {
207 visitor.visit_attribute(attr);
211 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
212 visitor.visit_pat(&*local.pat);
213 walk_ty_opt(visitor, &local.ty);
214 walk_expr_opt(visitor, &local.init);
217 pub fn walk_lifetime_def<'v, V: Visitor<'v>>(visitor: &mut V,
218 lifetime_def: &'v LifetimeDef) {
219 visitor.visit_name(lifetime_def.lifetime.span, lifetime_def.lifetime.name);
220 for bound in lifetime_def.bounds.iter() {
221 visitor.visit_lifetime_bound(bound);
225 pub fn walk_lifetime_bound<'v, V: Visitor<'v>>(visitor: &mut V,
226 lifetime_ref: &'v Lifetime) {
227 visitor.visit_lifetime_ref(lifetime_ref)
230 pub fn walk_lifetime_ref<'v, V: Visitor<'v>>(visitor: &mut V,
231 lifetime_ref: &'v Lifetime) {
232 visitor.visit_name(lifetime_ref.span, lifetime_ref.name)
235 pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V,
236 explicit_self: &'v ExplicitSelf) {
237 match explicit_self.node {
238 SelfStatic | SelfValue(_) => {},
239 SelfRegion(ref lifetime, _, _) => {
240 visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime)
242 SelfExplicit(ref typ, _) => visitor.visit_ty(&**typ),
246 /// Like with walk_method_helper this doesn't correspond to a method
247 /// in Visitor, and so it gets a _helper suffix.
248 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
249 trait_ref: &'v PolyTraitRef,
250 _modifier: &'v TraitBoundModifier)
253 walk_lifetime_decls_helper(visitor, &trait_ref.bound_lifetimes);
254 visitor.visit_trait_ref(&trait_ref.trait_ref);
257 /// Like with walk_method_helper this doesn't correspond to a method
258 /// in Visitor, and so it gets a _helper suffix.
259 pub fn walk_trait_ref<'v,V>(visitor: &mut V,
260 trait_ref: &'v TraitRef)
263 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
266 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
267 visitor.visit_ident(item.span, item.ident);
269 ItemStatic(ref typ, _, ref expr) |
270 ItemConst(ref typ, ref expr) => {
271 visitor.visit_ty(&**typ);
272 visitor.visit_expr(&**expr);
274 ItemFn(ref declaration, fn_style, abi, ref generics, ref body) => {
275 visitor.visit_fn(FkItemFn(item.ident, generics, fn_style, abi),
281 ItemMod(ref module) => {
282 visitor.visit_mod(module, item.span, item.id)
284 ItemForeignMod(ref foreign_module) => {
285 for view_item in foreign_module.view_items.iter() {
286 visitor.visit_view_item(view_item)
288 for foreign_item in foreign_module.items.iter() {
289 visitor.visit_foreign_item(&**foreign_item)
292 ItemTy(ref typ, ref type_parameters) => {
293 visitor.visit_ty(&**typ);
294 visitor.visit_generics(type_parameters)
296 ItemEnum(ref enum_definition, ref type_parameters) => {
297 visitor.visit_generics(type_parameters);
298 walk_enum_def(visitor, enum_definition, type_parameters)
305 visitor.visit_generics(type_parameters);
306 match *trait_reference {
307 Some(ref trait_reference) => visitor.visit_trait_ref(trait_reference),
310 visitor.visit_ty(&**typ);
311 for impl_item in impl_items.iter() {
313 MethodImplItem(ref method) => {
314 walk_method_helper(visitor, &**method)
316 TypeImplItem(ref typedef) => {
317 visitor.visit_ident(typedef.span, typedef.ident);
318 visitor.visit_ty(&*typedef.typ);
323 ItemStruct(ref struct_definition, ref generics) => {
324 visitor.visit_generics(generics);
325 visitor.visit_struct_def(&**struct_definition,
330 ItemTrait(_, ref generics, ref bounds, ref methods) => {
331 visitor.visit_generics(generics);
332 walk_ty_param_bounds_helper(visitor, bounds);
333 for method in methods.iter() {
334 visitor.visit_trait_item(method)
337 ItemMac(ref macro) => visitor.visit_mac(macro),
339 for attr in item.attrs.iter() {
340 visitor.visit_attribute(attr);
344 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
345 enum_definition: &'v EnumDef,
346 generics: &'v Generics) {
347 for variant in enum_definition.variants.iter() {
348 visitor.visit_variant(&**variant, generics);
352 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
353 variant: &'v Variant,
354 generics: &'v Generics) {
355 visitor.visit_ident(variant.span, variant.node.name);
357 match variant.node.kind {
358 TupleVariantKind(ref variant_arguments) => {
359 for variant_argument in variant_arguments.iter() {
360 visitor.visit_ty(&*variant_argument.ty)
363 StructVariantKind(ref struct_definition) => {
364 visitor.visit_struct_def(&**struct_definition,
370 match variant.node.disr_expr {
371 Some(ref expr) => visitor.visit_expr(&**expr),
374 for attr in variant.node.attrs.iter() {
375 visitor.visit_attribute(attr);
379 pub fn skip_ty<'v, V: Visitor<'v>>(_: &mut V, _: &'v Ty) {
383 pub fn walk_ty_opt<'v, V: Visitor<'v>>(visitor: &mut V, optional_type: &'v Option<P<Ty>>) {
384 match *optional_type {
385 Some(ref ty) => visitor.visit_ty(&**ty),
390 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
392 TyVec(ref ty) | TyParen(ref ty) => {
393 visitor.visit_ty(&**ty)
395 TyPtr(ref mutable_type) => {
396 visitor.visit_ty(&*mutable_type.ty)
398 TyRptr(ref lifetime, ref mutable_type) => {
399 visitor.visit_opt_lifetime_ref(typ.span, lifetime);
400 visitor.visit_ty(&*mutable_type.ty)
402 TyTup(ref tuple_element_types) => {
403 for tuple_element_type in tuple_element_types.iter() {
404 visitor.visit_ty(&**tuple_element_type)
407 TyClosure(ref function_declaration) => {
408 for argument in function_declaration.decl.inputs.iter() {
409 visitor.visit_ty(&*argument.ty)
411 walk_fn_ret_ty(visitor, &function_declaration.decl.output);
412 walk_ty_param_bounds_helper(visitor, &function_declaration.bounds);
413 walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
415 TyBareFn(ref function_declaration) => {
416 for argument in function_declaration.decl.inputs.iter() {
417 visitor.visit_ty(&*argument.ty)
419 walk_fn_ret_ty(visitor, &function_declaration.decl.output);
420 walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
422 TyPath(ref path, id) => {
423 visitor.visit_path(path, id);
425 TyObjectSum(ref ty, ref bounds) => {
426 visitor.visit_ty(&**ty);
427 walk_ty_param_bounds_helper(visitor, bounds);
429 TyQPath(ref qpath) => {
430 visitor.visit_ty(&*qpath.self_type);
431 visitor.visit_trait_ref(&*qpath.trait_ref);
432 visitor.visit_ident(typ.span, qpath.item_name);
434 TyFixedLengthVec(ref ty, ref expression) => {
435 visitor.visit_ty(&**ty);
436 visitor.visit_expr(&**expression)
438 TyPolyTraitRef(ref bounds) => {
439 walk_ty_param_bounds_helper(visitor, bounds)
441 TyTypeof(ref expression) => {
442 visitor.visit_expr(&**expression)
448 pub fn walk_lifetime_decls_helper<'v, V: Visitor<'v>>(visitor: &mut V,
449 lifetimes: &'v Vec<LifetimeDef>) {
450 for l in lifetimes.iter() {
451 visitor.visit_lifetime_def(l);
455 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
456 for segment in path.segments.iter() {
457 visitor.visit_path_segment(path.span, segment);
461 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
463 segment: &'v PathSegment) {
464 visitor.visit_ident(path_span, segment.identifier);
465 visitor.visit_path_parameters(path_span, &segment.parameters);
468 pub fn walk_path_parameters<'v, V: Visitor<'v>>(visitor: &mut V,
470 path_parameters: &'v PathParameters) {
471 match *path_parameters {
472 ast::AngleBracketedParameters(ref data) => {
473 for typ in data.types.iter() {
474 visitor.visit_ty(&**typ);
476 for lifetime in data.lifetimes.iter() {
477 visitor.visit_lifetime_ref(lifetime);
479 for binding in data.bindings.iter() {
480 visitor.visit_assoc_type_binding(&**binding);
483 ast::ParenthesizedParameters(ref data) => {
484 for typ in data.inputs.iter() {
485 visitor.visit_ty(&**typ);
487 for typ in data.output.iter() {
488 visitor.visit_ty(&**typ);
494 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
495 type_binding: &'v TypeBinding) {
496 visitor.visit_ident(type_binding.span, type_binding.ident);
497 visitor.visit_ty(&*type_binding.ty);
500 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
502 PatEnum(ref path, ref children) => {
503 visitor.visit_path(path, pattern.id);
504 for children in children.iter() {
505 for child in children.iter() {
506 visitor.visit_pat(&**child)
510 PatStruct(ref path, ref fields, _) => {
511 visitor.visit_path(path, pattern.id);
512 for field in fields.iter() {
513 visitor.visit_pat(&*field.node.pat)
516 PatTup(ref tuple_elements) => {
517 for tuple_element in tuple_elements.iter() {
518 visitor.visit_pat(&**tuple_element)
521 PatBox(ref subpattern) |
522 PatRegion(ref subpattern) => {
523 visitor.visit_pat(&**subpattern)
525 PatIdent(_, ref pth1, ref optional_subpattern) => {
526 visitor.visit_ident(pth1.span, pth1.node);
527 match *optional_subpattern {
529 Some(ref subpattern) => visitor.visit_pat(&**subpattern),
532 PatLit(ref expression) => visitor.visit_expr(&**expression),
533 PatRange(ref lower_bound, ref upper_bound) => {
534 visitor.visit_expr(&**lower_bound);
535 visitor.visit_expr(&**upper_bound)
538 PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
539 for prepattern in prepattern.iter() {
540 visitor.visit_pat(&**prepattern)
542 for slice_pattern in slice_pattern.iter() {
543 visitor.visit_pat(&**slice_pattern)
545 for postpattern in postpatterns.iter() {
546 visitor.visit_pat(&**postpattern)
549 PatMac(ref macro) => visitor.visit_mac(macro),
553 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
554 foreign_item: &'v ForeignItem) {
555 visitor.visit_ident(foreign_item.span, foreign_item.ident);
557 match foreign_item.node {
558 ForeignItemFn(ref function_declaration, ref generics) => {
559 walk_fn_decl(visitor, &**function_declaration);
560 visitor.visit_generics(generics)
562 ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ),
565 for attr in foreign_item.attrs.iter() {
566 visitor.visit_attribute(attr);
570 pub fn walk_ty_param_bounds_helper<'v, V: Visitor<'v>>(visitor: &mut V,
571 bounds: &'v OwnedSlice<TyParamBound>) {
572 for bound in bounds.iter() {
573 visitor.visit_ty_param_bound(bound)
577 pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
578 bound: &'v TyParamBound) {
580 TraitTyParamBound(ref typ, ref modifier) => {
581 visitor.visit_poly_trait_ref(typ, modifier);
583 RegionTyParamBound(ref lifetime) => {
584 visitor.visit_lifetime_bound(lifetime);
589 pub fn walk_ty_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v TyParam) {
590 visitor.visit_ident(param.span, param.ident);
591 walk_ty_param_bounds_helper(visitor, ¶m.bounds);
592 walk_ty_opt(visitor, ¶m.default);
595 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
596 for type_parameter in generics.ty_params.iter() {
597 walk_ty_param(visitor, type_parameter);
599 walk_lifetime_decls_helper(visitor, &generics.lifetimes);
600 for predicate in generics.where_clause.predicates.iter() {
602 &ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bounded_ty,
605 visitor.visit_ty(&**bounded_ty);
606 walk_ty_param_bounds_helper(visitor, bounds);
608 &ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
611 visitor.visit_lifetime_ref(lifetime);
613 for bound in bounds.iter() {
614 visitor.visit_lifetime_ref(bound);
617 &ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{id,
621 visitor.visit_path(path, id);
622 visitor.visit_ty(&**ty);
628 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
629 if let Return(ref output_ty) = *ret_ty {
630 visitor.visit_ty(&**output_ty)
634 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
635 for argument in function_declaration.inputs.iter() {
636 visitor.visit_pat(&*argument.pat);
637 visitor.visit_ty(&*argument.ty)
639 walk_fn_ret_ty(visitor, &function_declaration.output)
642 // Note: there is no visit_method() method in the visitor, instead override
643 // visit_fn() and check for FkMethod(). I named this visit_method_helper()
644 // because it is not a default impl of any method, though I doubt that really
645 // clarifies anything. - Niko
646 pub fn walk_method_helper<'v, V: Visitor<'v>>(visitor: &mut V, method: &'v Method) {
648 MethDecl(ident, ref generics, _, _, _, ref decl, ref body, _) => {
649 visitor.visit_ident(method.span, ident);
650 visitor.visit_fn(FkMethod(ident, generics, method),
655 for attr in method.attrs.iter() {
656 visitor.visit_attribute(attr);
660 MethMac(ref mac) => visitor.visit_mac(mac)
664 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
665 function_kind: FnKind<'v>,
666 function_declaration: &'v FnDecl,
667 function_body: &'v Block,
669 walk_fn_decl(visitor, function_declaration);
671 match function_kind {
672 FkItemFn(_, generics, _, _) => {
673 visitor.visit_generics(generics);
675 FkMethod(_, generics, method) => {
676 visitor.visit_generics(generics);
678 MethDecl(_, _, _, ref explicit_self, _, _, _, _) =>
679 visitor.visit_explicit_self(explicit_self),
681 visitor.visit_mac(mac)
687 visitor.visit_block(function_body)
690 pub fn walk_ty_method<'v, V: Visitor<'v>>(visitor: &mut V, method_type: &'v TypeMethod) {
691 visitor.visit_ident(method_type.span, method_type.ident);
692 visitor.visit_explicit_self(&method_type.explicit_self);
693 for argument_type in method_type.decl.inputs.iter() {
694 visitor.visit_ty(&*argument_type.ty)
696 visitor.visit_generics(&method_type.generics);
697 walk_fn_ret_ty(visitor, &method_type.decl.output);
698 for attr in method_type.attrs.iter() {
699 visitor.visit_attribute(attr);
703 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_method: &'v TraitItem) {
704 match *trait_method {
705 RequiredMethod(ref method_type) => visitor.visit_ty_method(method_type),
706 ProvidedMethod(ref method) => walk_method_helper(visitor, &**method),
707 TypeTraitItem(ref associated_type) => {
708 walk_ty_param(visitor, &associated_type.ty_param);
713 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
714 struct_definition: &'v StructDef) {
715 for field in struct_definition.fields.iter() {
716 visitor.visit_struct_field(field)
720 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
721 struct_field: &'v StructField) {
722 if let NamedField(name, _) = struct_field.node.kind {
723 visitor.visit_ident(struct_field.span, name);
726 visitor.visit_ty(&*struct_field.node.ty);
728 for attr in struct_field.node.attrs.iter() {
729 visitor.visit_attribute(attr);
733 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
734 for view_item in block.view_items.iter() {
735 visitor.visit_view_item(view_item)
737 for statement in block.stmts.iter() {
738 visitor.visit_stmt(&**statement)
740 walk_expr_opt(visitor, &block.expr)
743 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
744 match statement.node {
745 StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration),
746 StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
747 visitor.visit_expr(&**expression)
749 StmtMac(ref macro, _) => visitor.visit_mac(&**macro),
753 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
754 match declaration.node {
755 DeclLocal(ref local) => visitor.visit_local(&**local),
756 DeclItem(ref item) => visitor.visit_item(&**item),
760 pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V,
761 optional_expression: &'v Option<P<Expr>>) {
762 match *optional_expression {
764 Some(ref expression) => visitor.visit_expr(&**expression),
768 pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) {
769 for expression in expressions.iter() {
770 visitor.visit_expr(&**expression)
774 pub fn walk_mac<'v, V: Visitor<'v>>(_: &mut V, _: &'v Mac) {
778 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
779 match expression.node {
780 ExprBox(ref place, ref subexpression) => {
781 place.as_ref().map(|e|visitor.visit_expr(&**e));
782 visitor.visit_expr(&**subexpression)
784 ExprVec(ref subexpressions) => {
785 walk_exprs(visitor, subexpressions.as_slice())
787 ExprRepeat(ref element, ref count) => {
788 visitor.visit_expr(&**element);
789 visitor.visit_expr(&**count)
791 ExprStruct(ref path, ref fields, ref optional_base) => {
792 visitor.visit_path(path, expression.id);
793 for field in fields.iter() {
794 visitor.visit_expr(&*field.expr)
796 walk_expr_opt(visitor, optional_base)
798 ExprTup(ref subexpressions) => {
799 for subexpression in subexpressions.iter() {
800 visitor.visit_expr(&**subexpression)
803 ExprCall(ref callee_expression, ref arguments) => {
804 for argument in arguments.iter() {
805 visitor.visit_expr(&**argument)
807 visitor.visit_expr(&**callee_expression)
809 ExprMethodCall(_, ref types, ref arguments) => {
810 walk_exprs(visitor, arguments.as_slice());
811 for typ in types.iter() {
812 visitor.visit_ty(&**typ)
815 ExprBinary(_, ref left_expression, ref right_expression) => {
816 visitor.visit_expr(&**left_expression);
817 visitor.visit_expr(&**right_expression)
819 ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
820 visitor.visit_expr(&**subexpression)
823 ExprCast(ref subexpression, ref typ) => {
824 visitor.visit_expr(&**subexpression);
825 visitor.visit_ty(&**typ)
827 ExprIf(ref head_expression, ref if_block, ref optional_else) => {
828 visitor.visit_expr(&**head_expression);
829 visitor.visit_block(&**if_block);
830 walk_expr_opt(visitor, optional_else)
832 ExprWhile(ref subexpression, ref block, _) => {
833 visitor.visit_expr(&**subexpression);
834 visitor.visit_block(&**block)
836 ExprIfLet(ref pattern, ref subexpression, ref if_block, ref optional_else) => {
837 visitor.visit_pat(&**pattern);
838 visitor.visit_expr(&**subexpression);
839 visitor.visit_block(&**if_block);
840 walk_expr_opt(visitor, optional_else);
842 ExprWhileLet(ref pattern, ref subexpression, ref block, _) => {
843 visitor.visit_pat(&**pattern);
844 visitor.visit_expr(&**subexpression);
845 visitor.visit_block(&**block);
847 ExprForLoop(ref pattern, ref subexpression, ref block, _) => {
848 visitor.visit_pat(&**pattern);
849 visitor.visit_expr(&**subexpression);
850 visitor.visit_block(&**block)
852 ExprLoop(ref block, _) => visitor.visit_block(&**block),
853 ExprMatch(ref subexpression, ref arms, _) => {
854 visitor.visit_expr(&**subexpression);
855 for arm in arms.iter() {
856 visitor.visit_arm(arm)
859 ExprClosure(_, _, ref function_declaration, ref body) => {
860 visitor.visit_fn(FkFnBlock,
861 &**function_declaration,
866 ExprBlock(ref block) => visitor.visit_block(&**block),
867 ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
868 visitor.visit_expr(&**right_hand_expression);
869 visitor.visit_expr(&**left_hand_expression)
871 ExprAssignOp(_, ref left_expression, ref right_expression) => {
872 visitor.visit_expr(&**right_expression);
873 visitor.visit_expr(&**left_expression)
875 ExprField(ref subexpression, _) => {
876 visitor.visit_expr(&**subexpression);
878 ExprTupField(ref subexpression, _) => {
879 visitor.visit_expr(&**subexpression);
881 ExprIndex(ref main_expression, ref index_expression) => {
882 visitor.visit_expr(&**main_expression);
883 visitor.visit_expr(&**index_expression)
885 ExprRange(ref start, ref end) => {
886 walk_expr_opt(visitor, start);
887 walk_expr_opt(visitor, end)
889 ExprPath(ref path) => {
890 visitor.visit_path(path, expression.id)
892 ExprBreak(_) | ExprAgain(_) => {}
893 ExprRet(ref optional_expression) => {
894 walk_expr_opt(visitor, optional_expression)
896 ExprMac(ref macro) => visitor.visit_mac(macro),
897 ExprParen(ref subexpression) => {
898 visitor.visit_expr(&**subexpression)
900 ExprInlineAsm(ref ia) => {
901 for input in ia.inputs.iter() {
902 let (_, ref input) = *input;
903 visitor.visit_expr(&**input)
905 for output in ia.outputs.iter() {
906 let (_, ref output, _) = *output;
907 visitor.visit_expr(&**output)
912 visitor.visit_expr_post(expression)
915 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
916 for pattern in arm.pats.iter() {
917 visitor.visit_pat(&**pattern)
919 walk_expr_opt(visitor, &arm.guard);
920 visitor.visit_expr(&*arm.body);
921 for attr in arm.attrs.iter() {
922 visitor.visit_attribute(attr);