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 `intravisit::walk_*` to apply the default traversal algorithm, or prevent
14 //! deeper traversal by doing nothing.
16 //! When visiting the HIR, the contents of nested items are NOT visited
17 //! by default. This is different from the AST visitor, which does a deep walk.
18 //! Hence this module is called `intravisit`; see the method `visit_nested_item`
21 //! Note: it is an important invariant that the default visitor walks
22 //! the body of a function in "execution order" (more concretely,
23 //! reverse post-order with respect to the CFG implied by the AST),
24 //! meaning that if AST node A may execute before AST node B, then A
25 //! is visited first. The borrow checker in particular relies on this
29 use syntax::ast::{NodeId, CRATE_NODE_ID, Name, Attribute};
30 use syntax::codemap::Span;
33 #[derive(Copy, Clone, PartialEq, Eq)]
35 /// fn foo() or extern "Abi" fn foo()
36 ItemFn(Name, &'a Generics, Unsafety, Constness, Abi, Visibility),
39 Method(Name, &'a MethodSig, Option<Visibility>),
45 /// Each method of the Visitor trait is a hook to be potentially
46 /// overridden. Each method's default implementation recursively visits
47 /// the substructure of the input via the corresponding `walk` method;
48 /// e.g. the `visit_mod` method by default calls `intravisit::walk_mod`.
50 /// Note that this visitor does NOT visit nested items by default
51 /// (this is why the module is called `intravisit`, to distinguish it
52 /// from the AST's `visit` module, which acts differently). If you
53 /// simply want to visit all items in the crate in some order, you
54 /// should call `Crate::visit_all_items`. Otherwise, see the comment
55 /// on `visit_nested_item` for details on how to visit nested items.
57 /// If you want to ensure that your code handles every variant
58 /// explicitly, you need to override each method. (And you also need
59 /// to monitor future changes to `Visitor` in case a new method with a
60 /// new default implementation gets introduced.)
61 pub trait Visitor<'v> : Sized {
62 ///////////////////////////////////////////////////////////////////////////
65 /// Invoked when a nested item is encountered. By default, does
66 /// nothing. If you want a deep walk, you need to override to
67 /// fetch the item contents. But most of the time, it is easier
68 /// (and better) to invoke `Crate::visit_all_items`, which visits
69 /// all items in the crate in some order (but doesn't respect
71 #[allow(unused_variables)]
72 fn visit_nested_item(&mut self, id: ItemId) {
75 /// Visit the top-level item and (optionally) nested items. See
76 /// `visit_nested_item` for details.
77 fn visit_item(&mut self, i: &'v Item) {
81 ///////////////////////////////////////////////////////////////////////////
83 fn visit_name(&mut self, _span: Span, _name: Name) {
86 fn visit_ident(&mut self, span: Span, ident: Ident) {
87 walk_ident(self, span, ident);
89 fn visit_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) {
92 fn visit_foreign_item(&mut self, i: &'v ForeignItem) {
93 walk_foreign_item(self, i)
95 fn visit_local(&mut self, l: &'v Local) {
98 fn visit_block(&mut self, b: &'v Block) {
101 fn visit_stmt(&mut self, s: &'v Stmt) {
104 fn visit_arm(&mut self, a: &'v Arm) {
107 fn visit_pat(&mut self, p: &'v Pat) {
110 fn visit_decl(&mut self, d: &'v Decl) {
113 fn visit_expr(&mut self, ex: &'v Expr) {
116 fn visit_expr_post(&mut self, _ex: &'v Expr) {
118 fn visit_ty(&mut self, t: &'v Ty) {
121 fn visit_generics(&mut self, g: &'v Generics) {
122 walk_generics(self, g)
124 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: &'v Block, s: Span, _: NodeId) {
125 walk_fn(self, fk, fd, b, s)
127 fn visit_trait_item(&mut self, ti: &'v TraitItem) {
128 walk_trait_item(self, ti)
130 fn visit_impl_item(&mut self, ii: &'v ImplItem) {
131 walk_impl_item(self, ii)
133 fn visit_trait_ref(&mut self, t: &'v TraitRef) {
134 walk_trait_ref(self, t)
136 fn visit_ty_param_bound(&mut self, bounds: &'v TyParamBound) {
137 walk_ty_param_bound(self, bounds)
139 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: &'v TraitBoundModifier) {
140 walk_poly_trait_ref(self, t, m)
142 fn visit_variant_data(&mut self,
148 walk_struct_def(self, s)
150 fn visit_struct_field(&mut self, s: &'v StructField) {
151 walk_struct_field(self, s)
153 fn visit_enum_def(&mut self,
154 enum_definition: &'v EnumDef,
155 generics: &'v Generics,
158 walk_enum_def(self, enum_definition, generics, item_id)
160 fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics, item_id: NodeId) {
161 walk_variant(self, v, g, item_id)
163 fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
164 walk_lifetime(self, lifetime)
166 fn visit_lifetime_def(&mut self, lifetime: &'v LifetimeDef) {
167 walk_lifetime_def(self, lifetime)
169 fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
170 walk_explicit_self(self, es)
172 fn visit_path(&mut self, path: &'v Path, _id: NodeId) {
173 walk_path(self, path)
175 fn visit_path_list_item(&mut self, prefix: &'v Path, item: &'v PathListItem) {
176 walk_path_list_item(self, prefix, item)
178 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
179 walk_path_segment(self, path_span, path_segment)
181 fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'v PathParameters) {
182 walk_path_parameters(self, path_span, path_parameters)
184 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
185 walk_assoc_type_binding(self, type_binding)
187 fn visit_attribute(&mut self, _attr: &'v Attribute) {
189 fn visit_macro_def(&mut self, macro_def: &'v MacroDef) {
190 walk_macro_def(self, macro_def)
194 pub fn walk_opt_name<'v, V: Visitor<'v>>(visitor: &mut V, span: Span, opt_name: Option<Name>) {
195 for name in opt_name {
196 visitor.visit_name(span, name);
200 pub fn walk_opt_ident<'v, V: Visitor<'v>>(visitor: &mut V, span: Span, opt_ident: Option<Ident>) {
201 for ident in opt_ident {
202 visitor.visit_ident(span, ident);
206 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, span: Span, ident: Ident) {
207 visitor.visit_name(span, ident.name);
210 /// Walks the contents of a crate. See also `Crate::visit_all_items`.
211 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
212 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
213 walk_list!(visitor, visit_attribute, &krate.attrs);
214 walk_list!(visitor, visit_macro_def, &krate.exported_macros);
217 pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef) {
218 visitor.visit_name(macro_def.span, macro_def.name);
219 walk_opt_name(visitor, macro_def.span, macro_def.imported_from);
220 walk_list!(visitor, visit_attribute, ¯o_def.attrs);
223 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
224 for &item_id in &module.item_ids {
225 visitor.visit_nested_item(item_id);
229 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
230 visitor.visit_pat(&local.pat);
231 walk_list!(visitor, visit_ty, &local.ty);
232 walk_list!(visitor, visit_expr, &local.init);
235 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
236 visitor.visit_name(lifetime.span, lifetime.name);
239 pub fn walk_lifetime_def<'v, V: Visitor<'v>>(visitor: &mut V, lifetime_def: &'v LifetimeDef) {
240 visitor.visit_lifetime(&lifetime_def.lifetime);
241 walk_list!(visitor, visit_lifetime, &lifetime_def.bounds);
244 pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V, explicit_self: &'v ExplicitSelf) {
245 match explicit_self.node {
248 visitor.visit_name(explicit_self.span, name)
250 SelfRegion(ref opt_lifetime, _, name) => {
251 visitor.visit_name(explicit_self.span, name);
252 walk_list!(visitor, visit_lifetime, opt_lifetime);
254 SelfExplicit(ref typ, name) => {
255 visitor.visit_name(explicit_self.span, name);
256 visitor.visit_ty(typ)
261 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
262 trait_ref: &'v PolyTraitRef,
263 _modifier: &'v TraitBoundModifier)
266 walk_list!(visitor, visit_lifetime_def, &trait_ref.bound_lifetimes);
267 visitor.visit_trait_ref(&trait_ref.trait_ref);
270 pub fn walk_trait_ref<'v, V>(visitor: &mut V, trait_ref: &'v TraitRef)
273 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
276 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
277 visitor.visit_name(item.span, item.name);
279 ItemExternCrate(opt_name) => {
280 walk_opt_name(visitor, item.span, opt_name)
284 ViewPathSimple(name, ref path) => {
285 visitor.visit_name(vp.span, name);
286 visitor.visit_path(path, item.id);
288 ViewPathGlob(ref path) => {
289 visitor.visit_path(path, item.id);
291 ViewPathList(ref prefix, ref list) => {
292 if !list.is_empty() {
294 visitor.visit_path_list_item(prefix, item)
297 visitor.visit_path(prefix, item.id);
302 ItemStatic(ref typ, _, ref expr) |
303 ItemConst(ref typ, ref expr) => {
304 visitor.visit_ty(typ);
305 visitor.visit_expr(expr);
307 ItemFn(ref declaration, unsafety, constness, abi, ref generics, ref body) => {
308 visitor.visit_fn(FnKind::ItemFn(item.name,
319 ItemMod(ref module) => {
320 visitor.visit_mod(module, item.span, item.id)
322 ItemForeignMod(ref foreign_module) => {
323 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
325 ItemTy(ref typ, ref type_parameters) => {
326 visitor.visit_ty(typ);
327 visitor.visit_generics(type_parameters)
329 ItemEnum(ref enum_definition, ref type_parameters) => {
330 visitor.visit_generics(type_parameters);
331 visitor.visit_enum_def(enum_definition, type_parameters, item.id, item.span)
333 ItemDefaultImpl(_, ref trait_ref) => {
334 visitor.visit_trait_ref(trait_ref)
336 ItemImpl(_, _, ref type_parameters, ref opt_trait_reference, ref typ, ref impl_items) => {
337 visitor.visit_generics(type_parameters);
338 walk_list!(visitor, visit_trait_ref, opt_trait_reference);
339 visitor.visit_ty(typ);
340 walk_list!(visitor, visit_impl_item, impl_items);
342 ItemStruct(ref struct_definition, ref generics) => {
343 visitor.visit_generics(generics);
344 visitor.visit_variant_data(struct_definition, item.name, generics, item.id, item.span);
346 ItemTrait(_, ref generics, ref bounds, ref methods) => {
347 visitor.visit_generics(generics);
348 walk_list!(visitor, visit_ty_param_bound, bounds);
349 walk_list!(visitor, visit_trait_item, methods);
352 walk_list!(visitor, visit_attribute, &item.attrs);
355 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
356 enum_definition: &'v EnumDef,
357 generics: &'v Generics,
361 &enum_definition.variants,
366 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
367 variant: &'v Variant,
368 generics: &'v Generics,
370 visitor.visit_name(variant.span, variant.node.name);
371 visitor.visit_variant_data(&variant.node.data,
376 walk_list!(visitor, visit_expr, &variant.node.disr_expr);
377 walk_list!(visitor, visit_attribute, &variant.node.attrs);
380 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
385 TyPtr(ref mutable_type) => {
386 visitor.visit_ty(&mutable_type.ty)
388 TyRptr(ref opt_lifetime, ref mutable_type) => {
389 walk_list!(visitor, visit_lifetime, opt_lifetime);
390 visitor.visit_ty(&mutable_type.ty)
392 TyTup(ref tuple_element_types) => {
393 walk_list!(visitor, visit_ty, tuple_element_types);
395 TyBareFn(ref function_declaration) => {
396 walk_fn_decl(visitor, &function_declaration.decl);
397 walk_list!(visitor, visit_lifetime_def, &function_declaration.lifetimes);
399 TyPath(ref maybe_qself, ref path) => {
400 if let Some(ref qself) = *maybe_qself {
401 visitor.visit_ty(&qself.ty);
403 visitor.visit_path(path, typ.id);
405 TyObjectSum(ref ty, ref bounds) => {
406 visitor.visit_ty(ty);
407 walk_list!(visitor, visit_ty_param_bound, bounds);
409 TyFixedLengthVec(ref ty, ref expression) => {
410 visitor.visit_ty(ty);
411 visitor.visit_expr(expression)
413 TyPolyTraitRef(ref bounds) => {
414 walk_list!(visitor, visit_ty_param_bound, bounds);
416 TyTypeof(ref expression) => {
417 visitor.visit_expr(expression)
423 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
424 for segment in &path.segments {
425 visitor.visit_path_segment(path.span, segment);
429 pub fn walk_path_list_item<'v, V: Visitor<'v>>(visitor: &mut V,
431 item: &'v PathListItem) {
432 for segment in &prefix.segments {
433 visitor.visit_path_segment(prefix.span, segment);
436 walk_opt_name(visitor, item.span, item.node.name());
437 walk_opt_name(visitor, item.span, item.node.rename());
440 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
442 segment: &'v PathSegment) {
443 visitor.visit_ident(path_span, segment.identifier);
444 visitor.visit_path_parameters(path_span, &segment.parameters);
447 pub fn walk_path_parameters<'v, V: Visitor<'v>>(visitor: &mut V,
449 path_parameters: &'v PathParameters) {
450 match *path_parameters {
451 AngleBracketedParameters(ref data) => {
452 walk_list!(visitor, visit_ty, &data.types);
453 walk_list!(visitor, visit_lifetime, &data.lifetimes);
454 walk_list!(visitor, visit_assoc_type_binding, &data.bindings);
456 ParenthesizedParameters(ref data) => {
457 walk_list!(visitor, visit_ty, &data.inputs);
458 walk_list!(visitor, visit_ty, &data.output);
463 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
464 type_binding: &'v TypeBinding) {
465 visitor.visit_name(type_binding.span, type_binding.name);
466 visitor.visit_ty(&type_binding.ty);
469 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
471 PatKind::TupleStruct(ref path, ref opt_children) => {
472 visitor.visit_path(path, pattern.id);
473 if let Some(ref children) = *opt_children {
474 walk_list!(visitor, visit_pat, children);
477 PatKind::Path(ref path) => {
478 visitor.visit_path(path, pattern.id);
480 PatKind::QPath(ref qself, ref path) => {
481 visitor.visit_ty(&qself.ty);
482 visitor.visit_path(path, pattern.id)
484 PatKind::Struct(ref path, ref fields, _) => {
485 visitor.visit_path(path, pattern.id);
486 for field in fields {
487 visitor.visit_name(field.span, field.node.name);
488 visitor.visit_pat(&field.node.pat)
491 PatKind::Tup(ref tuple_elements) => {
492 walk_list!(visitor, visit_pat, tuple_elements);
494 PatKind::Box(ref subpattern) |
495 PatKind::Ref(ref subpattern, _) => {
496 visitor.visit_pat(subpattern)
498 PatKind::Ident(_, ref pth1, ref optional_subpattern) => {
499 visitor.visit_ident(pth1.span, pth1.node);
500 walk_list!(visitor, visit_pat, optional_subpattern);
502 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
503 PatKind::Range(ref lower_bound, ref upper_bound) => {
504 visitor.visit_expr(lower_bound);
505 visitor.visit_expr(upper_bound)
508 PatKind::Vec(ref prepatterns, ref slice_pattern, ref postpatterns) => {
509 walk_list!(visitor, visit_pat, prepatterns);
510 walk_list!(visitor, visit_pat, slice_pattern);
511 walk_list!(visitor, visit_pat, postpatterns);
516 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem) {
517 visitor.visit_name(foreign_item.span, foreign_item.name);
519 match foreign_item.node {
520 ForeignItemFn(ref function_declaration, ref generics) => {
521 walk_fn_decl(visitor, function_declaration);
522 visitor.visit_generics(generics)
524 ForeignItemStatic(ref typ, _) => visitor.visit_ty(typ),
527 walk_list!(visitor, visit_attribute, &foreign_item.attrs);
530 pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v TyParamBound) {
532 TraitTyParamBound(ref typ, ref modifier) => {
533 visitor.visit_poly_trait_ref(typ, modifier);
535 RegionTyParamBound(ref lifetime) => {
536 visitor.visit_lifetime(lifetime);
541 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
542 for param in &generics.ty_params {
543 visitor.visit_name(param.span, param.name);
544 walk_list!(visitor, visit_ty_param_bound, ¶m.bounds);
545 walk_list!(visitor, visit_ty, ¶m.default);
547 walk_list!(visitor, visit_lifetime_def, &generics.lifetimes);
548 for predicate in &generics.where_clause.predicates {
550 &WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
554 visitor.visit_ty(bounded_ty);
555 walk_list!(visitor, visit_ty_param_bound, bounds);
556 walk_list!(visitor, visit_lifetime_def, bound_lifetimes);
558 &WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
561 visitor.visit_lifetime(lifetime);
562 walk_list!(visitor, visit_lifetime, bounds);
564 &WherePredicate::EqPredicate(WhereEqPredicate{id,
568 visitor.visit_path(path, id);
569 visitor.visit_ty(ty);
575 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
576 if let Return(ref output_ty) = *ret_ty {
577 visitor.visit_ty(output_ty)
581 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
582 for argument in &function_declaration.inputs {
583 visitor.visit_pat(&argument.pat);
584 visitor.visit_ty(&argument.ty)
586 walk_fn_ret_ty(visitor, &function_declaration.output)
589 pub fn walk_fn_decl_nopat<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
590 for argument in &function_declaration.inputs {
591 visitor.visit_ty(&argument.ty)
593 walk_fn_ret_ty(visitor, &function_declaration.output)
596 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
597 match function_kind {
598 FnKind::ItemFn(_, generics, _, _, _, _) => {
599 visitor.visit_generics(generics);
601 FnKind::Method(_, sig, _) => {
602 visitor.visit_generics(&sig.generics);
603 visitor.visit_explicit_self(&sig.explicit_self);
605 FnKind::Closure => {}
609 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
610 function_kind: FnKind<'v>,
611 function_declaration: &'v FnDecl,
612 function_body: &'v Block,
614 walk_fn_decl(visitor, function_declaration);
615 walk_fn_kind(visitor, function_kind);
616 visitor.visit_block(function_body)
619 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
620 visitor.visit_name(trait_item.span, trait_item.name);
621 walk_list!(visitor, visit_attribute, &trait_item.attrs);
622 match trait_item.node {
623 ConstTraitItem(ref ty, ref default) => {
624 visitor.visit_ty(ty);
625 walk_list!(visitor, visit_expr, default);
627 MethodTraitItem(ref sig, None) => {
628 visitor.visit_explicit_self(&sig.explicit_self);
629 visitor.visit_generics(&sig.generics);
630 walk_fn_decl(visitor, &sig.decl);
632 MethodTraitItem(ref sig, Some(ref body)) => {
633 visitor.visit_fn(FnKind::Method(trait_item.name, sig, None),
639 TypeTraitItem(ref bounds, ref default) => {
640 walk_list!(visitor, visit_ty_param_bound, bounds);
641 walk_list!(visitor, visit_ty, default);
646 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
647 visitor.visit_name(impl_item.span, impl_item.name);
648 walk_list!(visitor, visit_attribute, &impl_item.attrs);
649 match impl_item.node {
650 ImplItemKind::Const(ref ty, ref expr) => {
651 visitor.visit_ty(ty);
652 visitor.visit_expr(expr);
654 ImplItemKind::Method(ref sig, ref body) => {
655 visitor.visit_fn(FnKind::Method(impl_item.name, sig, Some(impl_item.vis)),
661 ImplItemKind::Type(ref ty) => {
662 visitor.visit_ty(ty);
667 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &'v VariantData) {
668 walk_list!(visitor, visit_struct_field, struct_definition.fields());
671 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) {
672 visitor.visit_name(struct_field.span, struct_field.name);
673 visitor.visit_ty(&struct_field.ty);
674 walk_list!(visitor, visit_attribute, &struct_field.attrs);
677 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
678 walk_list!(visitor, visit_stmt, &block.stmts);
679 walk_list!(visitor, visit_expr, &block.expr);
682 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
683 match statement.node {
684 StmtDecl(ref declaration, _) => visitor.visit_decl(declaration),
685 StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
686 visitor.visit_expr(expression)
691 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
692 match declaration.node {
693 DeclLocal(ref local) => visitor.visit_local(local),
694 DeclItem(item) => visitor.visit_nested_item(item),
698 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
699 match expression.node {
700 ExprBox(ref subexpression) => {
701 visitor.visit_expr(subexpression)
703 ExprVec(ref subexpressions) => {
704 walk_list!(visitor, visit_expr, subexpressions);
706 ExprRepeat(ref element, ref count) => {
707 visitor.visit_expr(element);
708 visitor.visit_expr(count)
710 ExprStruct(ref path, ref fields, ref optional_base) => {
711 visitor.visit_path(path, expression.id);
712 for field in fields {
713 visitor.visit_name(field.name.span, field.name.node);
714 visitor.visit_expr(&field.expr)
716 walk_list!(visitor, visit_expr, optional_base);
718 ExprTup(ref subexpressions) => {
719 walk_list!(visitor, visit_expr, subexpressions);
721 ExprCall(ref callee_expression, ref arguments) => {
722 walk_list!(visitor, visit_expr, arguments);
723 visitor.visit_expr(callee_expression)
725 ExprMethodCall(ref name, ref types, ref arguments) => {
726 visitor.visit_name(name.span, name.node);
727 walk_list!(visitor, visit_expr, arguments);
728 walk_list!(visitor, visit_ty, types);
730 ExprBinary(_, ref left_expression, ref right_expression) => {
731 visitor.visit_expr(left_expression);
732 visitor.visit_expr(right_expression)
734 ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
735 visitor.visit_expr(subexpression)
738 ExprCast(ref subexpression, ref typ) | ExprType(ref subexpression, ref typ) => {
739 visitor.visit_expr(subexpression);
740 visitor.visit_ty(typ)
742 ExprIf(ref head_expression, ref if_block, ref optional_else) => {
743 visitor.visit_expr(head_expression);
744 visitor.visit_block(if_block);
745 walk_list!(visitor, visit_expr, optional_else);
747 ExprWhile(ref subexpression, ref block, opt_ident) => {
748 visitor.visit_expr(subexpression);
749 visitor.visit_block(block);
750 walk_opt_ident(visitor, expression.span, opt_ident)
752 ExprLoop(ref block, opt_ident) => {
753 visitor.visit_block(block);
754 walk_opt_ident(visitor, expression.span, opt_ident)
756 ExprMatch(ref subexpression, ref arms, _) => {
757 visitor.visit_expr(subexpression);
758 walk_list!(visitor, visit_arm, arms);
760 ExprClosure(_, ref function_declaration, ref body) => {
761 visitor.visit_fn(FnKind::Closure,
762 function_declaration,
767 ExprBlock(ref block) => visitor.visit_block(block),
768 ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
769 visitor.visit_expr(right_hand_expression);
770 visitor.visit_expr(left_hand_expression)
772 ExprAssignOp(_, ref left_expression, ref right_expression) => {
773 visitor.visit_expr(right_expression);
774 visitor.visit_expr(left_expression)
776 ExprField(ref subexpression, ref name) => {
777 visitor.visit_expr(subexpression);
778 visitor.visit_name(name.span, name.node);
780 ExprTupField(ref subexpression, _) => {
781 visitor.visit_expr(subexpression);
783 ExprIndex(ref main_expression, ref index_expression) => {
784 visitor.visit_expr(main_expression);
785 visitor.visit_expr(index_expression)
787 ExprPath(ref maybe_qself, ref path) => {
788 if let Some(ref qself) = *maybe_qself {
789 visitor.visit_ty(&qself.ty);
791 visitor.visit_path(path, expression.id)
793 ExprBreak(ref opt_sp_ident) | ExprAgain(ref opt_sp_ident) => {
794 for sp_ident in opt_sp_ident {
795 visitor.visit_ident(sp_ident.span, sp_ident.node);
798 ExprRet(ref optional_expression) => {
799 walk_list!(visitor, visit_expr, optional_expression);
801 ExprInlineAsm(_, ref outputs, ref inputs) => {
802 for output in outputs {
803 visitor.visit_expr(output)
805 for input in inputs {
806 visitor.visit_expr(input)
811 visitor.visit_expr_post(expression)
814 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
815 walk_list!(visitor, visit_pat, &arm.pats);
816 walk_list!(visitor, visit_expr, &arm.guard);
817 visitor.visit_expr(&arm.body);
818 walk_list!(visitor, visit_attribute, &arm.attrs);