1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! HIR walker. Each overridden visit method has full control over what
12 //! happens with its node, it can do its own traversal of the node's children,
13 //! call `visit::walk_*` to apply the default traversal algorithm, or prevent
14 //! deeper traversal by doing nothing.
16 //! Note: it is an important invariant that the default visitor walks the body
17 //! of a function in "execution order" (more concretely, reverse post-order
18 //! with respect to the CFG implied by the AST), meaning that if AST node A may
19 //! execute before AST node B, then A is visited first. The borrow checker in
20 //! particular relies on this property.
22 //! Note: walking an AST before macro expansion is probably a bad idea. For
23 //! instance, a walker looking for item names in a module will miss all of
24 //! those that are created by the expansion of a macro.
27 use syntax::ast::{Ident, NodeId, CRATE_NODE_ID, Name, Attribute};
28 use syntax::codemap::Span;
31 #[derive(Copy, Clone, PartialEq, Eq)]
33 /// fn foo() or extern "Abi" fn foo()
34 ItemFn(Name, &'a Generics, Unsafety, Constness, Abi, Visibility),
37 Method(Name, &'a MethodSig, Option<Visibility>),
43 /// Each method of the Visitor trait is a hook to be potentially
44 /// overridden. Each method's default implementation recursively visits
45 /// the substructure of the input via the corresponding `walk` method;
46 /// e.g. the `visit_mod` method by default calls `visit::walk_mod`.
48 /// If you want to ensure that your code handles every variant
49 /// explicitly, you need to override each method. (And you also need
50 /// to monitor future changes to `Visitor` in case a new method with a
51 /// new default implementation gets introduced.)
52 pub trait Visitor<'v> : Sized {
53 fn visit_name(&mut self, _span: Span, _name: Name) {
56 fn visit_ident(&mut self, span: Span, ident: Ident) {
57 walk_ident(self, span, ident);
59 fn visit_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) { walk_mod(self, m) }
60 fn visit_foreign_item(&mut self, i: &'v ForeignItem) { walk_foreign_item(self, i) }
61 fn visit_item(&mut self, i: &'v Item) { walk_item(self, i) }
62 fn visit_local(&mut self, l: &'v Local) { walk_local(self, l) }
63 fn visit_block(&mut self, b: &'v Block) { walk_block(self, b) }
64 fn visit_stmt(&mut self, s: &'v Stmt) { walk_stmt(self, s) }
65 fn visit_arm(&mut self, a: &'v Arm) { walk_arm(self, a) }
66 fn visit_pat(&mut self, p: &'v Pat) { walk_pat(self, p) }
67 fn visit_decl(&mut self, d: &'v Decl) { walk_decl(self, d) }
68 fn visit_expr(&mut self, ex: &'v Expr) { walk_expr(self, ex) }
69 fn visit_expr_post(&mut self, _ex: &'v Expr) { }
70 fn visit_ty(&mut self, t: &'v Ty) { walk_ty(self, t) }
71 fn visit_generics(&mut self, g: &'v Generics) { walk_generics(self, g) }
72 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: &'v Block, s: Span, _: NodeId) {
73 walk_fn(self, fk, fd, b, s)
75 fn visit_trait_item(&mut self, ti: &'v TraitItem) { walk_trait_item(self, ti) }
76 fn visit_impl_item(&mut self, ii: &'v ImplItem) { walk_impl_item(self, ii) }
77 fn visit_trait_ref(&mut self, t: &'v TraitRef) { walk_trait_ref(self, t) }
78 fn visit_ty_param_bound(&mut self, bounds: &'v TyParamBound) {
79 walk_ty_param_bound(self, bounds)
81 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: &'v TraitBoundModifier) {
82 walk_poly_trait_ref(self, t, m)
84 fn visit_struct_def(&mut self, s: &'v StructDef, _: Name, _: &'v Generics, _: NodeId) {
85 walk_struct_def(self, s)
87 fn visit_struct_field(&mut self, s: &'v StructField) { walk_struct_field(self, s) }
88 fn visit_enum_def(&mut self, enum_definition: &'v EnumDef,
89 generics: &'v Generics) {
90 walk_enum_def(self, enum_definition, generics)
93 fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics) { walk_variant(self, v, g) }
95 fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
96 walk_lifetime(self, lifetime)
98 fn visit_lifetime_def(&mut self, lifetime: &'v LifetimeDef) {
99 walk_lifetime_def(self, lifetime)
101 fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
102 walk_explicit_self(self, es)
104 fn visit_path(&mut self, path: &'v Path, _id: NodeId) {
105 walk_path(self, path)
107 fn visit_path_list_item(&mut self, prefix: &'v Path, item: &'v PathListItem) {
108 walk_path_list_item(self, prefix, item)
110 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
111 walk_path_segment(self, path_span, path_segment)
113 fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'v PathParameters) {
114 walk_path_parameters(self, path_span, path_parameters)
116 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
117 walk_assoc_type_binding(self, type_binding)
119 fn visit_attribute(&mut self, _attr: &'v Attribute) {}
120 fn visit_macro_def(&mut self, macro_def: &'v MacroDef) {
121 walk_macro_def(self, macro_def)
125 pub fn walk_opt_name<'v, V: Visitor<'v>>(visitor: &mut V, span: Span, opt_name: Option<Name>) {
126 for name in opt_name {
127 visitor.visit_name(span, name);
131 pub fn walk_opt_ident<'v, V: Visitor<'v>>(visitor: &mut V, span: Span, opt_ident: Option<Ident>) {
132 for ident in opt_ident {
133 visitor.visit_ident(span, ident);
137 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, span: Span, ident: Ident) {
138 visitor.visit_name(span, ident.name);
141 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
142 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
143 walk_list!(visitor, visit_attribute, &krate.attrs);
144 walk_list!(visitor, visit_macro_def, &krate.exported_macros);
147 pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef) {
148 visitor.visit_name(macro_def.span, macro_def.name);
149 walk_opt_name(visitor, macro_def.span, macro_def.imported_from);
150 walk_list!(visitor, visit_attribute, ¯o_def.attrs);
153 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
154 walk_list!(visitor, visit_item, &module.items);
157 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
158 visitor.visit_pat(&local.pat);
159 walk_list!(visitor, visit_ty, &local.ty);
160 walk_list!(visitor, visit_expr, &local.init);
163 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
164 visitor.visit_name(lifetime.span, lifetime.name);
167 pub fn walk_lifetime_def<'v, V: Visitor<'v>>(visitor: &mut V,
168 lifetime_def: &'v LifetimeDef) {
169 visitor.visit_lifetime(&lifetime_def.lifetime);
170 walk_list!(visitor, visit_lifetime, &lifetime_def.bounds);
173 pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V,
174 explicit_self: &'v ExplicitSelf) {
175 match explicit_self.node {
178 visitor.visit_name(explicit_self.span, name)
180 SelfRegion(ref opt_lifetime, _, name) => {
181 visitor.visit_name(explicit_self.span, name);
182 walk_list!(visitor, visit_lifetime, opt_lifetime);
184 SelfExplicit(ref typ, name) => {
185 visitor.visit_name(explicit_self.span, name);
186 visitor.visit_ty(typ)
191 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
192 trait_ref: &'v PolyTraitRef,
193 _modifier: &'v TraitBoundModifier)
196 walk_list!(visitor, visit_lifetime_def, &trait_ref.bound_lifetimes);
197 visitor.visit_trait_ref(&trait_ref.trait_ref);
200 pub fn walk_trait_ref<'v,V>(visitor: &mut V,
201 trait_ref: &'v TraitRef)
204 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
207 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
208 visitor.visit_name(item.span, item.name);
210 ItemExternCrate(opt_name) => {
211 walk_opt_name(visitor, item.span, opt_name)
215 ViewPathSimple(name, ref path) => {
216 visitor.visit_name(vp.span, name);
217 visitor.visit_path(path, item.id);
219 ViewPathGlob(ref path) => {
220 visitor.visit_path(path, item.id);
222 ViewPathList(ref prefix, ref list) => {
223 if !list.is_empty() {
225 visitor.visit_path_list_item(prefix, item)
228 visitor.visit_path(prefix, item.id);
233 ItemStatic(ref typ, _, ref expr) |
234 ItemConst(ref typ, ref expr) => {
235 visitor.visit_ty(typ);
236 visitor.visit_expr(expr);
238 ItemFn(ref declaration, unsafety, constness, abi, ref generics, ref body) => {
239 visitor.visit_fn(FnKind::ItemFn(item.name, generics, unsafety,
240 constness, abi, item.vis),
246 ItemMod(ref module) => {
247 visitor.visit_mod(module, item.span, item.id)
249 ItemForeignMod(ref foreign_module) => {
250 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
252 ItemTy(ref typ, ref type_parameters) => {
253 visitor.visit_ty(typ);
254 visitor.visit_generics(type_parameters)
256 ItemEnum(ref enum_definition, ref type_parameters) => {
257 visitor.visit_generics(type_parameters);
258 visitor.visit_enum_def(enum_definition, type_parameters)
260 ItemDefaultImpl(_, ref trait_ref) => {
261 visitor.visit_trait_ref(trait_ref)
265 ref opt_trait_reference,
268 visitor.visit_generics(type_parameters);
269 walk_list!(visitor, visit_trait_ref, opt_trait_reference);
270 visitor.visit_ty(typ);
271 walk_list!(visitor, visit_impl_item, impl_items);
273 ItemStruct(ref struct_definition, ref generics) => {
274 visitor.visit_generics(generics);
275 visitor.visit_struct_def(struct_definition,
280 ItemTrait(_, ref generics, ref bounds, ref methods) => {
281 visitor.visit_generics(generics);
282 walk_list!(visitor, visit_ty_param_bound, bounds);
283 walk_list!(visitor, visit_trait_item, methods);
286 walk_list!(visitor, visit_attribute, &item.attrs);
289 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
290 enum_definition: &'v EnumDef,
291 generics: &'v Generics) {
292 for variant in &enum_definition.variants {
293 visitor.visit_variant(variant, generics);
297 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
298 variant: &'v Variant,
299 generics: &'v Generics) {
300 visitor.visit_name(variant.span, variant.node.name);
302 match variant.node.kind {
303 TupleVariantKind(ref variant_arguments) => {
304 for variant_argument in variant_arguments {
305 visitor.visit_ty(&variant_argument.ty)
308 StructVariantKind(ref struct_definition) => {
309 visitor.visit_struct_def(struct_definition,
315 walk_list!(visitor, visit_expr, &variant.node.disr_expr);
316 walk_list!(visitor, visit_attribute, &variant.node.attrs);
319 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
321 TyVec(ref ty) | TyParen(ref ty) => {
324 TyPtr(ref mutable_type) => {
325 visitor.visit_ty(&mutable_type.ty)
327 TyRptr(ref opt_lifetime, ref mutable_type) => {
328 walk_list!(visitor, visit_lifetime, opt_lifetime);
329 visitor.visit_ty(&mutable_type.ty)
331 TyTup(ref tuple_element_types) => {
332 walk_list!(visitor, visit_ty, tuple_element_types);
334 TyBareFn(ref function_declaration) => {
335 walk_fn_decl(visitor, &function_declaration.decl);
336 walk_list!(visitor, visit_lifetime_def, &function_declaration.lifetimes);
338 TyPath(ref maybe_qself, ref path) => {
339 for qself in maybe_qself {
340 visitor.visit_ty(&qself.ty);
342 visitor.visit_path(path, typ.id);
344 TyObjectSum(ref ty, ref bounds) => {
345 visitor.visit_ty(ty);
346 walk_list!(visitor, visit_ty_param_bound, bounds);
348 TyFixedLengthVec(ref ty, ref expression) => {
349 visitor.visit_ty(ty);
350 visitor.visit_expr(expression)
352 TyPolyTraitRef(ref bounds) => {
353 walk_list!(visitor, visit_ty_param_bound, bounds);
355 TyTypeof(ref expression) => {
356 visitor.visit_expr(expression)
362 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
363 for segment in &path.segments {
364 visitor.visit_path_segment(path.span, segment);
368 pub fn walk_path_list_item<'v, V: Visitor<'v>>(visitor: &mut V, prefix: &'v Path,
369 item: &'v PathListItem) {
370 for segment in &prefix.segments {
371 visitor.visit_path_segment(prefix.span, segment);
374 walk_opt_name(visitor, item.span, item.node.name());
375 walk_opt_name(visitor, item.span, item.node.rename());
378 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
380 segment: &'v PathSegment) {
381 visitor.visit_ident(path_span, segment.identifier);
382 visitor.visit_path_parameters(path_span, &segment.parameters);
385 pub fn walk_path_parameters<'v, V: Visitor<'v>>(visitor: &mut V,
387 path_parameters: &'v PathParameters) {
388 match *path_parameters {
389 AngleBracketedParameters(ref data) => {
390 walk_list!(visitor, visit_ty, &data.types);
391 walk_list!(visitor, visit_lifetime, &data.lifetimes);
392 walk_list!(visitor, visit_assoc_type_binding, &data.bindings);
394 ParenthesizedParameters(ref data) => {
395 walk_list!(visitor, visit_ty, &data.inputs);
396 walk_list!(visitor, visit_ty, &data.output);
401 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
402 type_binding: &'v TypeBinding) {
403 visitor.visit_name(type_binding.span, type_binding.name);
404 visitor.visit_ty(&type_binding.ty);
407 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
409 PatEnum(ref path, ref opt_children) => {
410 visitor.visit_path(path, pattern.id);
411 for children in opt_children {
412 walk_list!(visitor, visit_pat, children);
415 PatQPath(ref qself, ref path) => {
416 visitor.visit_ty(&qself.ty);
417 visitor.visit_path(path, pattern.id)
419 PatStruct(ref path, ref fields, _) => {
420 visitor.visit_path(path, pattern.id);
421 for field in fields {
422 visitor.visit_name(field.span, field.node.name);
423 visitor.visit_pat(&field.node.pat)
426 PatTup(ref tuple_elements) => {
427 walk_list!(visitor, visit_pat, tuple_elements);
429 PatBox(ref subpattern) |
430 PatRegion(ref subpattern, _) => {
431 visitor.visit_pat(subpattern)
433 PatIdent(_, ref pth1, ref optional_subpattern) => {
434 visitor.visit_ident(pth1.span, pth1.node);
435 walk_list!(visitor, visit_pat, optional_subpattern);
437 PatLit(ref expression) => visitor.visit_expr(expression),
438 PatRange(ref lower_bound, ref upper_bound) => {
439 visitor.visit_expr(lower_bound);
440 visitor.visit_expr(upper_bound)
443 PatVec(ref prepatterns, ref slice_pattern, ref postpatterns) => {
444 walk_list!(visitor, visit_pat, prepatterns);
445 walk_list!(visitor, visit_pat, slice_pattern);
446 walk_list!(visitor, visit_pat, postpatterns);
451 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
452 foreign_item: &'v ForeignItem) {
453 visitor.visit_name(foreign_item.span, foreign_item.name);
455 match foreign_item.node {
456 ForeignItemFn(ref function_declaration, ref generics) => {
457 walk_fn_decl(visitor, function_declaration);
458 visitor.visit_generics(generics)
460 ForeignItemStatic(ref typ, _) => visitor.visit_ty(typ),
463 walk_list!(visitor, visit_attribute, &foreign_item.attrs);
466 pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
467 bound: &'v TyParamBound) {
469 TraitTyParamBound(ref typ, ref modifier) => {
470 visitor.visit_poly_trait_ref(typ, modifier);
472 RegionTyParamBound(ref lifetime) => {
473 visitor.visit_lifetime(lifetime);
478 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
479 for param in &generics.ty_params {
480 visitor.visit_name(param.span, param.name);
481 walk_list!(visitor, visit_ty_param_bound, ¶m.bounds);
482 walk_list!(visitor, visit_ty, ¶m.default);
484 walk_list!(visitor, visit_lifetime_def, &generics.lifetimes);
485 for predicate in &generics.where_clause.predicates {
487 &WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
491 visitor.visit_ty(bounded_ty);
492 walk_list!(visitor, visit_ty_param_bound, bounds);
493 walk_list!(visitor, visit_lifetime_def, bound_lifetimes);
495 &WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
498 visitor.visit_lifetime(lifetime);
499 walk_list!(visitor, visit_lifetime, bounds);
501 &WherePredicate::EqPredicate(WhereEqPredicate{id,
505 visitor.visit_path(path, id);
506 visitor.visit_ty(ty);
512 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
513 if let Return(ref output_ty) = *ret_ty {
514 visitor.visit_ty(output_ty)
518 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
519 for argument in &function_declaration.inputs {
520 visitor.visit_pat(&argument.pat);
521 visitor.visit_ty(&argument.ty)
523 walk_fn_ret_ty(visitor, &function_declaration.output)
526 pub fn walk_fn_decl_nopat<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
527 for argument in &function_declaration.inputs {
528 visitor.visit_ty(&argument.ty)
530 walk_fn_ret_ty(visitor, &function_declaration.output)
533 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V,
534 function_kind: FnKind<'v>) {
535 match function_kind {
536 FnKind::ItemFn(_, generics, _, _, _, _) => {
537 visitor.visit_generics(generics);
539 FnKind::Method(_, sig, _) => {
540 visitor.visit_generics(&sig.generics);
541 visitor.visit_explicit_self(&sig.explicit_self);
543 FnKind::Closure => {}
547 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
548 function_kind: FnKind<'v>,
549 function_declaration: &'v FnDecl,
550 function_body: &'v Block,
552 walk_fn_decl(visitor, function_declaration);
553 walk_fn_kind(visitor, function_kind);
554 visitor.visit_block(function_body)
557 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
558 visitor.visit_name(trait_item.span, trait_item.name);
559 walk_list!(visitor, visit_attribute, &trait_item.attrs);
560 match trait_item.node {
561 ConstTraitItem(ref ty, ref default) => {
562 visitor.visit_ty(ty);
563 walk_list!(visitor, visit_expr, default);
565 MethodTraitItem(ref sig, None) => {
566 visitor.visit_explicit_self(&sig.explicit_self);
567 visitor.visit_generics(&sig.generics);
568 walk_fn_decl(visitor, &sig.decl);
570 MethodTraitItem(ref sig, Some(ref body)) => {
571 visitor.visit_fn(FnKind::Method(trait_item.name, sig, None), &sig.decl,
572 body, trait_item.span, trait_item.id);
574 TypeTraitItem(ref bounds, ref default) => {
575 walk_list!(visitor, visit_ty_param_bound, bounds);
576 walk_list!(visitor, visit_ty, default);
581 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
582 visitor.visit_name(impl_item.span, impl_item.name);
583 walk_list!(visitor, visit_attribute, &impl_item.attrs);
584 match impl_item.node {
585 ConstImplItem(ref ty, ref expr) => {
586 visitor.visit_ty(ty);
587 visitor.visit_expr(expr);
589 MethodImplItem(ref sig, ref body) => {
590 visitor.visit_fn(FnKind::Method(impl_item.name, sig, Some(impl_item.vis)), &sig.decl,
591 body, impl_item.span, impl_item.id);
593 TypeImplItem(ref ty) => {
594 visitor.visit_ty(ty);
599 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
600 struct_definition: &'v StructDef) {
601 walk_list!(visitor, visit_struct_field, &struct_definition.fields);
604 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
605 struct_field: &'v StructField) {
606 walk_opt_name(visitor, struct_field.span, struct_field.node.name());
607 visitor.visit_ty(&struct_field.node.ty);
608 walk_list!(visitor, visit_attribute, &struct_field.node.attrs);
611 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
612 walk_list!(visitor, visit_stmt, &block.stmts);
613 walk_list!(visitor, visit_expr, &block.expr);
616 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
617 match statement.node {
618 StmtDecl(ref declaration, _) => visitor.visit_decl(declaration),
619 StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
620 visitor.visit_expr(expression)
625 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
626 match declaration.node {
627 DeclLocal(ref local) => visitor.visit_local(local),
628 DeclItem(ref item) => visitor.visit_item(item),
632 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
633 match expression.node {
634 ExprBox(ref subexpression) => {
635 visitor.visit_expr(subexpression)
637 ExprVec(ref subexpressions) => {
638 walk_list!(visitor, visit_expr, subexpressions);
640 ExprRepeat(ref element, ref count) => {
641 visitor.visit_expr(element);
642 visitor.visit_expr(count)
644 ExprStruct(ref path, ref fields, ref optional_base) => {
645 visitor.visit_path(path, expression.id);
646 for field in fields {
647 visitor.visit_name(field.name.span, field.name.node);
648 visitor.visit_expr(&field.expr)
650 walk_list!(visitor, visit_expr, optional_base);
652 ExprTup(ref subexpressions) => {
653 walk_list!(visitor, visit_expr, subexpressions);
655 ExprCall(ref callee_expression, ref arguments) => {
656 walk_list!(visitor, visit_expr, arguments);
657 visitor.visit_expr(callee_expression)
659 ExprMethodCall(ref name, ref types, ref arguments) => {
660 visitor.visit_name(name.span, name.node);
661 walk_list!(visitor, visit_expr, arguments);
662 walk_list!(visitor, visit_ty, types);
664 ExprBinary(_, ref left_expression, ref right_expression) => {
665 visitor.visit_expr(left_expression);
666 visitor.visit_expr(right_expression)
668 ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
669 visitor.visit_expr(subexpression)
672 ExprCast(ref subexpression, ref typ) => {
673 visitor.visit_expr(subexpression);
674 visitor.visit_ty(typ)
676 ExprIf(ref head_expression, ref if_block, ref optional_else) => {
677 visitor.visit_expr(head_expression);
678 visitor.visit_block(if_block);
679 walk_list!(visitor, visit_expr, optional_else);
681 ExprWhile(ref subexpression, ref block, opt_ident) => {
682 visitor.visit_expr(subexpression);
683 visitor.visit_block(block);
684 walk_opt_ident(visitor, expression.span, opt_ident)
686 ExprLoop(ref block, opt_ident) => {
687 visitor.visit_block(block);
688 walk_opt_ident(visitor, expression.span, opt_ident)
690 ExprMatch(ref subexpression, ref arms, _) => {
691 visitor.visit_expr(subexpression);
692 walk_list!(visitor, visit_arm, arms);
694 ExprClosure(_, ref function_declaration, ref body) => {
695 visitor.visit_fn(FnKind::Closure,
696 function_declaration,
701 ExprBlock(ref block) => visitor.visit_block(block),
702 ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
703 visitor.visit_expr(right_hand_expression);
704 visitor.visit_expr(left_hand_expression)
706 ExprAssignOp(_, ref left_expression, ref right_expression) => {
707 visitor.visit_expr(right_expression);
708 visitor.visit_expr(left_expression)
710 ExprField(ref subexpression, ref name) => {
711 visitor.visit_expr(subexpression);
712 visitor.visit_name(name.span, name.node);
714 ExprTupField(ref subexpression, _) => {
715 visitor.visit_expr(subexpression);
717 ExprIndex(ref main_expression, ref index_expression) => {
718 visitor.visit_expr(main_expression);
719 visitor.visit_expr(index_expression)
721 ExprRange(ref start, ref end) => {
722 walk_list!(visitor, visit_expr, start);
723 walk_list!(visitor, visit_expr, end);
725 ExprPath(ref maybe_qself, ref path) => {
726 for qself in maybe_qself {
727 visitor.visit_ty(&qself.ty);
729 visitor.visit_path(path, expression.id)
731 ExprBreak(ref opt_sp_ident) | ExprAgain(ref opt_sp_ident) => {
732 for sp_ident in opt_sp_ident {
733 visitor.visit_ident(sp_ident.span, sp_ident.node);
736 ExprRet(ref optional_expression) => {
737 walk_list!(visitor, visit_expr, optional_expression);
739 ExprInlineAsm(ref ia) => {
740 for &(_, ref input) in &ia.inputs {
741 visitor.visit_expr(&input)
743 for &(_, ref output, _) in &ia.outputs {
744 visitor.visit_expr(&output)
749 visitor.visit_expr_post(expression)
752 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
753 walk_list!(visitor, visit_pat, &arm.pats);
754 walk_list!(visitor, visit_expr, &arm.guard);
755 visitor.visit_expr(&arm.body);
756 walk_list!(visitor, visit_attribute, &arm.attrs);