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 //! Context-passing AST walker. Each overridden visit method has full control
12 //! over what happens with its node, it can do its own traversal of the node's
13 //! children (potentially passing in different contexts to each), call
14 //! `visit::visit_*` to apply the default traversal algorithm (again, it can
15 //! override the context), or prevent deeper traversal by doing nothing.
17 //! Note: it is an important invariant that the default visitor walks the body
18 //! of a function in "execution order" (more concretely, reverse post-order
19 //! with respect to the CFG implied by the AST), meaning that if AST node A may
20 //! execute before AST node B, then A is visited first. The borrow checker in
21 //! particular relies on this property.
23 //! Note: walking an AST before macro expansion is probably a bad idea. For
24 //! instance, a walker looking for item names in a module will miss all of
25 //! those that are created by the expansion of a macro.
33 use owned_slice::OwnedSlice;
38 /// fn foo() or extern "Abi" fn foo()
39 FkItemFn(Ident, &'a Generics, FnStyle, Abi),
42 FkMethod(Ident, &'a Generics, &'a Method),
49 pub fn name_of_fn(fk: &FnKind) -> Ident {
51 FkItemFn(name, _, _, _) | FkMethod(name, _, _) => name,
52 FkFnBlock(..) => parse::token::special_idents::invalid
56 pub fn generics_of_fn(fk: &FnKind) -> Generics {
58 FkItemFn(_, generics, _, _) |
59 FkMethod(_, generics, _) => {
62 FkFnBlock(..) => ast_util::empty_generics(),
66 /// Each method of the Visitor trait is a hook to be potentially
67 /// overridden. Each method's default implementation recursively visits
68 /// the substructure of the input via the corresponding `walk` method;
69 /// e.g. the `visit_mod` method by default calls `visit::walk_mod`.
71 /// If you want to ensure that your code handles every variant
72 /// explicitly, you need to override each method. (And you also need
73 /// to monitor future changes to `Visitor` in case a new method with a
74 /// new default implementation gets introduced.)
75 pub trait Visitor<E: Clone> {
77 fn visit_ident(&mut self, _sp: Span, _ident: Ident, _e: E) {
78 /*! Visit the idents */
80 fn visit_mod(&mut self, m: &Mod, _s: Span, _n: NodeId, e: E) { walk_mod(self, m, e) }
81 fn visit_view_item(&mut self, i: &ViewItem, e: E) { walk_view_item(self, i, e) }
82 fn visit_foreign_item(&mut self, i: &ForeignItem, e: E) { walk_foreign_item(self, i, e) }
83 fn visit_item(&mut self, i: &Item, e: E) { walk_item(self, i, e) }
84 fn visit_local(&mut self, l: &Local, e: E) { walk_local(self, l, e) }
85 fn visit_block(&mut self, b: &Block, e: E) { walk_block(self, b, e) }
86 fn visit_stmt(&mut self, s: &Stmt, e: E) { walk_stmt(self, s, e) }
87 fn visit_arm(&mut self, a: &Arm, e: E) { walk_arm(self, a, e) }
88 fn visit_pat(&mut self, p: &Pat, e: E) { walk_pat(self, p, e) }
89 fn visit_decl(&mut self, d: &Decl, e: E) { walk_decl(self, d, e) }
90 fn visit_expr(&mut self, ex: &Expr, e: E) { walk_expr(self, ex, e) }
91 fn visit_expr_post(&mut self, _ex: &Expr, _e: E) { }
92 fn visit_ty(&mut self, t: &Ty, e: E) { walk_ty(self, t, e) }
93 fn visit_generics(&mut self, g: &Generics, e: E) { walk_generics(self, g, e) }
94 fn visit_fn(&mut self, fk: &FnKind, fd: &FnDecl, b: &Block, s: Span, _: NodeId, e: E) {
95 walk_fn(self, fk, fd, b, s, e)
97 fn visit_ty_method(&mut self, t: &TypeMethod, e: E) { walk_ty_method(self, t, e) }
98 fn visit_trait_item(&mut self, t: &TraitItem, e: E) { walk_trait_item(self, t, e) }
99 fn visit_struct_def(&mut self, s: &StructDef, _: Ident, _: &Generics, _: NodeId, e: E) {
100 walk_struct_def(self, s, e)
102 fn visit_struct_field(&mut self, s: &StructField, e: E) { walk_struct_field(self, s, e) }
103 fn visit_variant(&mut self, v: &Variant, g: &Generics, e: E) { walk_variant(self, v, g, e) }
104 fn visit_opt_lifetime_ref(&mut self,
106 opt_lifetime: &Option<Lifetime>,
109 * Visits an optional reference to a lifetime. The `span` is
110 * the span of some surrounding reference should opt_lifetime
113 match *opt_lifetime {
114 Some(ref l) => self.visit_lifetime_ref(l, env),
118 fn visit_lifetime_ref(&mut self, _lifetime: &Lifetime, _e: E) {
119 /*! Visits a reference to a lifetime */
121 fn visit_lifetime_decl(&mut self, _lifetime: &LifetimeDef, _e: E) {
122 /*! Visits a declaration of a lifetime */
124 fn visit_explicit_self(&mut self, es: &ExplicitSelf, e: E) {
125 walk_explicit_self(self, es, e)
127 fn visit_mac(&mut self, _macro: &Mac, _e: E) {
128 fail!("visit_mac disabled by default");
129 // NB: see note about macros above.
130 // if you really want a visitor that
131 // works on macros, use this
132 // definition in your trait impl:
133 // visit::walk_mac(self, _macro, _e)
135 fn visit_path(&mut self, path: &Path, _id: ast::NodeId, e: E) {
136 walk_path(self, path, e)
138 fn visit_attribute(&mut self, _attr: &Attribute, _e: E) {}
141 pub fn walk_inlined_item<E: Clone, V: Visitor<E>>(visitor: &mut V,
142 item: &ast::InlinedItem,
145 IIItem(i) => visitor.visit_item(&*i, env),
146 IIForeign(i) => visitor.visit_foreign_item(&*i, env),
147 IITraitItem(_, iti) => {
149 ProvidedInlinedTraitItem(m) => {
150 walk_method_helper(visitor, &*m, env)
152 RequiredInlinedTraitItem(m) => {
153 walk_method_helper(visitor, &*m, env)
161 pub fn walk_crate<E: Clone, V: Visitor<E>>(visitor: &mut V, krate: &Crate, env: E) {
162 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID, env.clone());
163 for attr in krate.attrs.iter() {
164 visitor.visit_attribute(attr, env.clone());
168 pub fn walk_mod<E: Clone, V: Visitor<E>>(visitor: &mut V, module: &Mod, env: E) {
169 for view_item in module.view_items.iter() {
170 visitor.visit_view_item(view_item, env.clone())
173 for item in module.items.iter() {
174 visitor.visit_item(&**item, env.clone())
178 pub fn walk_view_item<E: Clone, V: Visitor<E>>(visitor: &mut V, vi: &ViewItem, env: E) {
180 ViewItemExternCrate(name, _, _) => {
181 visitor.visit_ident(vi.span, name, env.clone())
183 ViewItemUse(ref vp) => {
185 ViewPathSimple(ident, ref path, id) => {
186 visitor.visit_ident(vp.span, ident, env.clone());
187 visitor.visit_path(path, id, env.clone());
189 ViewPathGlob(ref path, id) => {
190 visitor.visit_path(path, id, env.clone());
192 ViewPathList(ref path, ref list, _) => {
193 for id in list.iter() {
195 PathListIdent { name, .. } => {
196 visitor.visit_ident(id.span, name, env.clone());
198 PathListMod { .. } => ()
201 walk_path(visitor, path, env.clone());
206 for attr in vi.attrs.iter() {
207 visitor.visit_attribute(attr, env.clone());
211 pub fn walk_local<E: Clone, V: Visitor<E>>(visitor: &mut V, local: &Local, env: E) {
212 visitor.visit_pat(&*local.pat, env.clone());
213 visitor.visit_ty(&*local.ty, env.clone());
216 Some(initializer) => visitor.visit_expr(&*initializer, env),
220 pub fn walk_explicit_self<E: Clone, V: Visitor<E>>(visitor: &mut V,
221 explicit_self: &ExplicitSelf,
223 match explicit_self.node {
224 SelfStatic | SelfValue(_) => {},
225 SelfRegion(ref lifetime, _, _) => {
226 visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime, env)
228 SelfExplicit(ref typ, _) => visitor.visit_ty(&**typ, env.clone()),
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_trait_ref_helper<E: Clone, V: Visitor<E>>(visitor: &mut V,
235 trait_ref: &TraitRef,
237 visitor.visit_path(&trait_ref.path, trait_ref.ref_id, env)
240 pub fn walk_item<E: Clone, V: Visitor<E>>(visitor: &mut V, item: &Item, env: E) {
241 visitor.visit_ident(item.span, item.ident, env.clone());
243 ItemStatic(ref typ, _, ref expr) => {
244 visitor.visit_ty(&**typ, env.clone());
245 visitor.visit_expr(&**expr, env.clone());
247 ItemFn(declaration, fn_style, abi, ref generics, body) => {
248 visitor.visit_fn(&FkItemFn(item.ident, generics, fn_style, abi),
255 ItemMod(ref module) => {
256 visitor.visit_mod(module, item.span, item.id, env.clone())
258 ItemForeignMod(ref foreign_module) => {
259 for view_item in foreign_module.view_items.iter() {
260 visitor.visit_view_item(view_item, env.clone())
262 for foreign_item in foreign_module.items.iter() {
263 visitor.visit_foreign_item(&**foreign_item, env.clone())
266 ItemTy(ref typ, ref type_parameters) => {
267 visitor.visit_ty(&**typ, env.clone());
268 visitor.visit_generics(type_parameters, env.clone())
270 ItemEnum(ref enum_definition, ref type_parameters) => {
271 visitor.visit_generics(type_parameters, env.clone());
272 walk_enum_def(visitor, enum_definition, type_parameters, env.clone())
274 ItemImpl(ref type_parameters,
278 visitor.visit_generics(type_parameters, env.clone());
279 match *trait_reference {
280 Some(ref trait_reference) => walk_trait_ref_helper(visitor,
281 trait_reference, env.clone()),
284 visitor.visit_ty(&*typ, env.clone());
285 for impl_item in impl_items.iter() {
287 MethodImplItem(method) => {
288 walk_method_helper(visitor, &*method, env.clone())
293 ItemStruct(ref struct_definition, ref generics) => {
294 visitor.visit_generics(generics, env.clone());
295 visitor.visit_struct_def(&**struct_definition,
301 ItemTrait(ref generics, _, ref bounds, ref methods) => {
302 visitor.visit_generics(generics, env.clone());
303 walk_ty_param_bounds(visitor, bounds, env.clone());
304 for method in methods.iter() {
305 visitor.visit_trait_item(method, env.clone())
308 ItemMac(ref macro) => visitor.visit_mac(macro, env.clone()),
310 for attr in item.attrs.iter() {
311 visitor.visit_attribute(attr, env.clone());
315 pub fn walk_enum_def<E: Clone, V:Visitor<E>>(visitor: &mut V,
316 enum_definition: &EnumDef,
319 for &variant in enum_definition.variants.iter() {
320 visitor.visit_variant(&*variant, generics, env.clone());
324 pub fn walk_variant<E: Clone, V: Visitor<E>>(visitor: &mut V,
328 visitor.visit_ident(variant.span, variant.node.name, env.clone());
330 match variant.node.kind {
331 TupleVariantKind(ref variant_arguments) => {
332 for variant_argument in variant_arguments.iter() {
333 visitor.visit_ty(&*variant_argument.ty, env.clone())
336 StructVariantKind(ref struct_definition) => {
337 visitor.visit_struct_def(&**struct_definition,
344 match variant.node.disr_expr {
345 Some(ref expr) => visitor.visit_expr(&**expr, env.clone()),
348 for attr in variant.node.attrs.iter() {
349 visitor.visit_attribute(attr, env.clone());
353 pub fn skip_ty<E, V: Visitor<E>>(_: &mut V, _: &Ty, _: E) {
357 pub fn walk_ty<E: Clone, V: Visitor<E>>(visitor: &mut V, typ: &Ty, env: E) {
359 TyUniq(ty) | TyVec(ty) | TyBox(ty) | TyParen(ty) => {
360 visitor.visit_ty(&*ty, env)
362 TyPtr(ref mutable_type) => {
363 visitor.visit_ty(&*mutable_type.ty, env)
365 TyRptr(ref lifetime, ref mutable_type) => {
366 visitor.visit_opt_lifetime_ref(typ.span, lifetime, env.clone());
367 visitor.visit_ty(&*mutable_type.ty, env)
369 TyTup(ref tuple_element_types) => {
370 for &tuple_element_type in tuple_element_types.iter() {
371 visitor.visit_ty(&*tuple_element_type, env.clone())
374 TyClosure(ref function_declaration) => {
375 for argument in function_declaration.decl.inputs.iter() {
376 visitor.visit_ty(&*argument.ty, env.clone())
378 visitor.visit_ty(&*function_declaration.decl.output, env.clone());
379 walk_ty_param_bounds(visitor, &function_declaration.bounds,
381 walk_lifetime_decls(visitor, &function_declaration.lifetimes,
384 TyProc(ref function_declaration) => {
385 for argument in function_declaration.decl.inputs.iter() {
386 visitor.visit_ty(&*argument.ty, env.clone())
388 visitor.visit_ty(&*function_declaration.decl.output, env.clone());
389 walk_ty_param_bounds(visitor, &function_declaration.bounds,
391 walk_lifetime_decls(visitor, &function_declaration.lifetimes,
394 TyBareFn(ref function_declaration) => {
395 for argument in function_declaration.decl.inputs.iter() {
396 visitor.visit_ty(&*argument.ty, env.clone())
398 visitor.visit_ty(&*function_declaration.decl.output, env.clone());
399 walk_lifetime_decls(visitor, &function_declaration.lifetimes,
402 TyUnboxedFn(ref function_declaration) => {
403 for argument in function_declaration.decl.inputs.iter() {
404 visitor.visit_ty(&*argument.ty, env.clone())
406 visitor.visit_ty(&*function_declaration.decl.output, env.clone());
408 TyPath(ref path, ref opt_bounds, id) => {
409 visitor.visit_path(path, id, env.clone());
411 Some(ref bounds) => {
412 walk_ty_param_bounds(visitor, bounds, env.clone());
417 TyFixedLengthVec(ref ty, ref expression) => {
418 visitor.visit_ty(&**ty, env.clone());
419 visitor.visit_expr(&**expression, env)
421 TyTypeof(ref expression) => {
422 visitor.visit_expr(&**expression, env)
424 TyNil | TyBot | TyInfer => {}
428 fn walk_lifetime_decls<E: Clone, V: Visitor<E>>(visitor: &mut V,
429 lifetimes: &Vec<LifetimeDef>,
431 for l in lifetimes.iter() {
432 visitor.visit_lifetime_decl(l, env.clone());
436 pub fn walk_path<E: Clone, V: Visitor<E>>(visitor: &mut V, path: &Path, env: E) {
437 for segment in path.segments.iter() {
438 visitor.visit_ident(path.span, segment.identifier, env.clone());
440 for typ in segment.types.iter() {
441 visitor.visit_ty(&**typ, env.clone());
443 for lifetime in segment.lifetimes.iter() {
444 visitor.visit_lifetime_ref(lifetime, env.clone());
449 pub fn walk_pat<E: Clone, V: Visitor<E>>(visitor: &mut V, pattern: &Pat, env: E) {
451 PatEnum(ref path, ref children) => {
452 visitor.visit_path(path, pattern.id, env.clone());
453 for children in children.iter() {
454 for child in children.iter() {
455 visitor.visit_pat(&**child, env.clone())
459 PatStruct(ref path, ref fields, _) => {
460 visitor.visit_path(path, pattern.id, env.clone());
461 for field in fields.iter() {
462 visitor.visit_pat(&*field.pat, env.clone())
465 PatTup(ref tuple_elements) => {
466 for tuple_element in tuple_elements.iter() {
467 visitor.visit_pat(&**tuple_element, env.clone())
470 PatBox(ref subpattern) |
471 PatRegion(ref subpattern) => {
472 visitor.visit_pat(&**subpattern, env)
474 PatIdent(_, ref pth1, ref optional_subpattern) => {
475 visitor.visit_ident(pth1.span, pth1.node, env.clone());
476 match *optional_subpattern {
478 Some(ref subpattern) => visitor.visit_pat(&**subpattern, env),
481 PatLit(ref expression) => visitor.visit_expr(&**expression, env),
482 PatRange(ref lower_bound, ref upper_bound) => {
483 visitor.visit_expr(&**lower_bound, env.clone());
484 visitor.visit_expr(&**upper_bound, env)
487 PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
488 for prepattern in prepattern.iter() {
489 visitor.visit_pat(&**prepattern, env.clone())
491 for slice_pattern in slice_pattern.iter() {
492 visitor.visit_pat(&**slice_pattern, env.clone())
494 for postpattern in postpatterns.iter() {
495 visitor.visit_pat(&**postpattern, env.clone())
498 PatMac(ref macro) => visitor.visit_mac(macro, env),
502 pub fn walk_foreign_item<E: Clone, V: Visitor<E>>(visitor: &mut V,
503 foreign_item: &ForeignItem,
505 visitor.visit_ident(foreign_item.span, foreign_item.ident, env.clone());
507 match foreign_item.node {
508 ForeignItemFn(ref function_declaration, ref generics) => {
509 walk_fn_decl(visitor, &**function_declaration, env.clone());
510 visitor.visit_generics(generics, env.clone())
512 ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ, env.clone()),
515 for attr in foreign_item.attrs.iter() {
516 visitor.visit_attribute(attr, env.clone());
520 pub fn walk_ty_param_bounds<E: Clone, V: Visitor<E>>(visitor: &mut V,
521 bounds: &OwnedSlice<TyParamBound>,
523 for bound in bounds.iter() {
525 TraitTyParamBound(ref typ) => {
526 walk_trait_ref_helper(visitor, typ, env.clone())
528 UnboxedFnTyParamBound(ref function_declaration) => {
529 for argument in function_declaration.decl.inputs.iter() {
530 visitor.visit_ty(&*argument.ty, env.clone())
532 visitor.visit_ty(&*function_declaration.decl.output,
535 RegionTyParamBound(ref lifetime) => {
536 visitor.visit_lifetime_ref(lifetime, env.clone());
542 pub fn walk_generics<E: Clone, V: Visitor<E>>(visitor: &mut V,
545 for type_parameter in generics.ty_params.iter() {
546 walk_ty_param_bounds(visitor, &type_parameter.bounds, env.clone());
547 match type_parameter.default {
548 Some(ref ty) => visitor.visit_ty(&**ty, env.clone()),
552 walk_lifetime_decls(visitor, &generics.lifetimes, env.clone());
553 for predicate in generics.where_clause.predicates.iter() {
554 visitor.visit_ident(predicate.span, predicate.ident, env.clone());
555 walk_ty_param_bounds(visitor, &predicate.bounds, env.clone());
559 pub fn walk_fn_decl<E: Clone, V: Visitor<E>>(visitor: &mut V,
560 function_declaration: &FnDecl,
562 for argument in function_declaration.inputs.iter() {
563 visitor.visit_pat(&*argument.pat, env.clone());
564 visitor.visit_ty(&*argument.ty, env.clone())
566 visitor.visit_ty(&*function_declaration.output, env)
569 // Note: there is no visit_method() method in the visitor, instead override
570 // visit_fn() and check for FkMethod(). I named this visit_method_helper()
571 // because it is not a default impl of any method, though I doubt that really
572 // clarifies anything. - Niko
573 pub fn walk_method_helper<E: Clone, V: Visitor<E>>(visitor: &mut V,
577 MethDecl(ident, ref generics, _, _, _, decl, body, _) => {
578 visitor.visit_ident(method.span, ident, env.clone());
579 visitor.visit_fn(&FkMethod(ident, generics, method),
585 for attr in method.attrs.iter() {
586 visitor.visit_attribute(attr, env.clone());
590 MethMac(ref mac) => visitor.visit_mac(mac, env.clone())
594 pub fn walk_fn<E: Clone, V: Visitor<E>>(visitor: &mut V,
595 function_kind: &FnKind,
596 function_declaration: &FnDecl,
597 function_body: &Block,
600 walk_fn_decl(visitor, function_declaration, env.clone());
602 match *function_kind {
603 FkItemFn(_, generics, _, _) => {
604 visitor.visit_generics(generics, env.clone());
606 FkMethod(_, generics, method) => {
607 visitor.visit_generics(generics, env.clone());
609 MethDecl(_, _, _, ref explicit_self, _, _, _, _) =>
610 visitor.visit_explicit_self(explicit_self, env.clone()),
612 visitor.visit_mac(mac, env.clone())
618 visitor.visit_block(function_body, env)
621 pub fn walk_ty_method<E: Clone, V: Visitor<E>>(visitor: &mut V,
622 method_type: &TypeMethod,
624 visitor.visit_ident(method_type.span, method_type.ident, env.clone());
625 visitor.visit_explicit_self(&method_type.explicit_self, env.clone());
626 for argument_type in method_type.decl.inputs.iter() {
627 visitor.visit_ty(&*argument_type.ty, env.clone())
629 visitor.visit_generics(&method_type.generics, env.clone());
630 visitor.visit_ty(&*method_type.decl.output, env.clone());
631 for attr in method_type.attrs.iter() {
632 visitor.visit_attribute(attr, env.clone());
636 pub fn walk_trait_item<E: Clone, V: Visitor<E>>(visitor: &mut V,
637 trait_method: &TraitItem,
639 match *trait_method {
640 RequiredMethod(ref method_type) => {
641 visitor.visit_ty_method(method_type, env)
643 ProvidedMethod(ref method) => walk_method_helper(visitor, &**method, env),
647 pub fn walk_struct_def<E: Clone, V: Visitor<E>>(visitor: &mut V,
648 struct_definition: &StructDef,
650 match struct_definition.super_struct {
651 Some(ref t) => visitor.visit_ty(&**t, env.clone()),
654 for field in struct_definition.fields.iter() {
655 visitor.visit_struct_field(field, env.clone())
659 pub fn walk_struct_field<E: Clone, V: Visitor<E>>(visitor: &mut V,
660 struct_field: &StructField,
662 match struct_field.node.kind {
663 NamedField(name, _) => {
664 visitor.visit_ident(struct_field.span, name, env.clone())
669 visitor.visit_ty(&*struct_field.node.ty, env.clone());
671 for attr in struct_field.node.attrs.iter() {
672 visitor.visit_attribute(attr, env.clone());
676 pub fn walk_block<E: Clone, V: Visitor<E>>(visitor: &mut V, block: &Block, env: E) {
677 for view_item in block.view_items.iter() {
678 visitor.visit_view_item(view_item, env.clone())
680 for statement in block.stmts.iter() {
681 visitor.visit_stmt(&**statement, env.clone())
683 walk_expr_opt(visitor, block.expr, env)
686 pub fn walk_stmt<E: Clone, V: Visitor<E>>(visitor: &mut V, statement: &Stmt, env: E) {
687 match statement.node {
688 StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration, env),
689 StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
690 visitor.visit_expr(&**expression, env)
692 StmtMac(ref macro, _) => visitor.visit_mac(macro, env),
696 pub fn walk_decl<E: Clone, V: Visitor<E>>(visitor: &mut V, declaration: &Decl, env: E) {
697 match declaration.node {
698 DeclLocal(ref local) => visitor.visit_local(&**local, env),
699 DeclItem(ref item) => visitor.visit_item(&**item, env),
703 pub fn walk_expr_opt<E: Clone, V: Visitor<E>>(visitor: &mut V,
704 optional_expression: Option<Gc<Expr>>,
706 match optional_expression {
708 Some(ref expression) => visitor.visit_expr(&**expression, env),
712 pub fn walk_exprs<E: Clone, V: Visitor<E>>(visitor: &mut V,
713 expressions: &[Gc<Expr>],
715 for expression in expressions.iter() {
716 visitor.visit_expr(&**expression, env.clone())
720 pub fn walk_mac<E, V: Visitor<E>>(_: &mut V, _: &Mac, _: E) {
724 pub fn walk_expr<E: Clone, V: Visitor<E>>(visitor: &mut V, expression: &Expr, env: E) {
725 match expression.node {
726 ExprBox(ref place, ref subexpression) => {
727 visitor.visit_expr(&**place, env.clone());
728 visitor.visit_expr(&**subexpression, env.clone())
730 ExprVec(ref subexpressions) => {
731 walk_exprs(visitor, subexpressions.as_slice(), env.clone())
733 ExprRepeat(ref element, ref count) => {
734 visitor.visit_expr(&**element, env.clone());
735 visitor.visit_expr(&**count, env.clone())
737 ExprStruct(ref path, ref fields, optional_base) => {
738 visitor.visit_path(path, expression.id, env.clone());
739 for field in fields.iter() {
740 visitor.visit_expr(&*field.expr, env.clone())
742 walk_expr_opt(visitor, optional_base, env.clone())
744 ExprTup(ref subexpressions) => {
745 for subexpression in subexpressions.iter() {
746 visitor.visit_expr(&**subexpression, env.clone())
749 ExprCall(ref callee_expression, ref arguments) => {
750 for argument in arguments.iter() {
751 visitor.visit_expr(&**argument, env.clone())
753 visitor.visit_expr(&**callee_expression, env.clone())
755 ExprMethodCall(_, ref types, ref arguments) => {
756 walk_exprs(visitor, arguments.as_slice(), env.clone());
757 for typ in types.iter() {
758 visitor.visit_ty(&**typ, env.clone())
761 ExprBinary(_, ref left_expression, ref right_expression) => {
762 visitor.visit_expr(&**left_expression, env.clone());
763 visitor.visit_expr(&**right_expression, env.clone())
765 ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
766 visitor.visit_expr(&**subexpression, env.clone())
769 ExprCast(ref subexpression, ref typ) => {
770 visitor.visit_expr(&**subexpression, env.clone());
771 visitor.visit_ty(&**typ, env.clone())
773 ExprIf(ref head_expression, ref if_block, optional_else) => {
774 visitor.visit_expr(&**head_expression, env.clone());
775 visitor.visit_block(&**if_block, env.clone());
776 walk_expr_opt(visitor, optional_else, env.clone())
778 ExprWhile(ref subexpression, ref block, _) => {
779 visitor.visit_expr(&**subexpression, env.clone());
780 visitor.visit_block(&**block, env.clone())
782 ExprForLoop(ref pattern, ref subexpression, ref block, _) => {
783 visitor.visit_pat(&**pattern, env.clone());
784 visitor.visit_expr(&**subexpression, env.clone());
785 visitor.visit_block(&**block, env.clone())
787 ExprLoop(ref block, _) => visitor.visit_block(&**block, env.clone()),
788 ExprMatch(ref subexpression, ref arms) => {
789 visitor.visit_expr(&**subexpression, env.clone());
790 for arm in arms.iter() {
791 visitor.visit_arm(arm, env.clone())
794 ExprFnBlock(_, ref function_declaration, ref body) => {
795 visitor.visit_fn(&FkFnBlock,
796 &**function_declaration,
802 ExprUnboxedFn(_, _, ref function_declaration, ref body) => {
803 visitor.visit_fn(&FkFnBlock,
804 &**function_declaration,
810 ExprProc(ref function_declaration, ref body) => {
811 visitor.visit_fn(&FkFnBlock,
812 &**function_declaration,
818 ExprBlock(ref block) => visitor.visit_block(&**block, env.clone()),
819 ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
820 visitor.visit_expr(&**right_hand_expression, env.clone());
821 visitor.visit_expr(&**left_hand_expression, env.clone())
823 ExprAssignOp(_, ref left_expression, ref right_expression) => {
824 visitor.visit_expr(&**right_expression, env.clone());
825 visitor.visit_expr(&**left_expression, env.clone())
827 ExprField(ref subexpression, _, ref types) => {
828 visitor.visit_expr(&**subexpression, env.clone());
829 for typ in types.iter() {
830 visitor.visit_ty(&**typ, env.clone())
833 ExprIndex(ref main_expression, ref index_expression) => {
834 visitor.visit_expr(&**main_expression, env.clone());
835 visitor.visit_expr(&**index_expression, env.clone())
837 ExprPath(ref path) => {
838 visitor.visit_path(path, expression.id, env.clone())
840 ExprBreak(_) | ExprAgain(_) => {}
841 ExprRet(optional_expression) => {
842 walk_expr_opt(visitor, optional_expression, env.clone())
844 ExprMac(ref macro) => visitor.visit_mac(macro, env.clone()),
845 ExprParen(ref subexpression) => {
846 visitor.visit_expr(&**subexpression, env.clone())
848 ExprInlineAsm(ref ia) => {
849 for &(_, ref input) in ia.inputs.iter() {
850 visitor.visit_expr(&**input, env.clone())
852 for &(_, ref output, _) in ia.outputs.iter() {
853 visitor.visit_expr(&**output, env.clone())
858 visitor.visit_expr_post(expression, env.clone())
861 pub fn walk_arm<E: Clone, V: Visitor<E>>(visitor: &mut V, arm: &Arm, env: E) {
862 for pattern in arm.pats.iter() {
863 visitor.visit_pat(&**pattern, env.clone())
865 walk_expr_opt(visitor, arm.guard, env.clone());
866 visitor.visit_expr(&*arm.body, env.clone());
867 for attr in arm.attrs.iter() {
868 visitor.visit_attribute(attr, env.clone());