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 PatEnum(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 PatQPath(ref qself, ref path) => {
478 visitor.visit_ty(&qself.ty);
479 visitor.visit_path(path, pattern.id)
481 PatStruct(ref path, ref fields, _) => {
482 visitor.visit_path(path, pattern.id);
483 for field in fields {
484 visitor.visit_name(field.span, field.node.name);
485 visitor.visit_pat(&field.node.pat)
488 PatTup(ref tuple_elements) => {
489 walk_list!(visitor, visit_pat, tuple_elements);
491 PatBox(ref subpattern) |
492 PatRegion(ref subpattern, _) => {
493 visitor.visit_pat(subpattern)
495 PatIdent(_, ref pth1, ref optional_subpattern) => {
496 visitor.visit_ident(pth1.span, pth1.node);
497 walk_list!(visitor, visit_pat, optional_subpattern);
499 PatLit(ref expression) => visitor.visit_expr(expression),
500 PatRange(ref lower_bound, ref upper_bound) => {
501 visitor.visit_expr(lower_bound);
502 visitor.visit_expr(upper_bound)
505 PatVec(ref prepatterns, ref slice_pattern, ref postpatterns) => {
506 walk_list!(visitor, visit_pat, prepatterns);
507 walk_list!(visitor, visit_pat, slice_pattern);
508 walk_list!(visitor, visit_pat, postpatterns);
513 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem) {
514 visitor.visit_name(foreign_item.span, foreign_item.name);
516 match foreign_item.node {
517 ForeignItemFn(ref function_declaration, ref generics) => {
518 walk_fn_decl(visitor, function_declaration);
519 visitor.visit_generics(generics)
521 ForeignItemStatic(ref typ, _) => visitor.visit_ty(typ),
524 walk_list!(visitor, visit_attribute, &foreign_item.attrs);
527 pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v TyParamBound) {
529 TraitTyParamBound(ref typ, ref modifier) => {
530 visitor.visit_poly_trait_ref(typ, modifier);
532 RegionTyParamBound(ref lifetime) => {
533 visitor.visit_lifetime(lifetime);
538 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
539 for param in &generics.ty_params {
540 visitor.visit_name(param.span, param.name);
541 walk_list!(visitor, visit_ty_param_bound, ¶m.bounds);
542 walk_list!(visitor, visit_ty, ¶m.default);
544 walk_list!(visitor, visit_lifetime_def, &generics.lifetimes);
545 for predicate in &generics.where_clause.predicates {
547 &WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
551 visitor.visit_ty(bounded_ty);
552 walk_list!(visitor, visit_ty_param_bound, bounds);
553 walk_list!(visitor, visit_lifetime_def, bound_lifetimes);
555 &WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
558 visitor.visit_lifetime(lifetime);
559 walk_list!(visitor, visit_lifetime, bounds);
561 &WherePredicate::EqPredicate(WhereEqPredicate{id,
565 visitor.visit_path(path, id);
566 visitor.visit_ty(ty);
572 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
573 if let Return(ref output_ty) = *ret_ty {
574 visitor.visit_ty(output_ty)
578 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
579 for argument in &function_declaration.inputs {
580 visitor.visit_pat(&argument.pat);
581 visitor.visit_ty(&argument.ty)
583 walk_fn_ret_ty(visitor, &function_declaration.output)
586 pub fn walk_fn_decl_nopat<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
587 for argument in &function_declaration.inputs {
588 visitor.visit_ty(&argument.ty)
590 walk_fn_ret_ty(visitor, &function_declaration.output)
593 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
594 match function_kind {
595 FnKind::ItemFn(_, generics, _, _, _, _) => {
596 visitor.visit_generics(generics);
598 FnKind::Method(_, sig, _) => {
599 visitor.visit_generics(&sig.generics);
600 visitor.visit_explicit_self(&sig.explicit_self);
602 FnKind::Closure => {}
606 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
607 function_kind: FnKind<'v>,
608 function_declaration: &'v FnDecl,
609 function_body: &'v Block,
611 walk_fn_decl(visitor, function_declaration);
612 walk_fn_kind(visitor, function_kind);
613 visitor.visit_block(function_body)
616 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
617 visitor.visit_name(trait_item.span, trait_item.name);
618 walk_list!(visitor, visit_attribute, &trait_item.attrs);
619 match trait_item.node {
620 ConstTraitItem(ref ty, ref default) => {
621 visitor.visit_ty(ty);
622 walk_list!(visitor, visit_expr, default);
624 MethodTraitItem(ref sig, None) => {
625 visitor.visit_explicit_self(&sig.explicit_self);
626 visitor.visit_generics(&sig.generics);
627 walk_fn_decl(visitor, &sig.decl);
629 MethodTraitItem(ref sig, Some(ref body)) => {
630 visitor.visit_fn(FnKind::Method(trait_item.name, sig, None),
636 TypeTraitItem(ref bounds, ref default) => {
637 walk_list!(visitor, visit_ty_param_bound, bounds);
638 walk_list!(visitor, visit_ty, default);
643 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
644 visitor.visit_name(impl_item.span, impl_item.name);
645 walk_list!(visitor, visit_attribute, &impl_item.attrs);
646 match impl_item.node {
647 ImplItemKind::Const(ref ty, ref expr) => {
648 visitor.visit_ty(ty);
649 visitor.visit_expr(expr);
651 ImplItemKind::Method(ref sig, ref body) => {
652 visitor.visit_fn(FnKind::Method(impl_item.name, sig, Some(impl_item.vis)),
658 ImplItemKind::Type(ref ty) => {
659 visitor.visit_ty(ty);
664 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &'v VariantData) {
665 walk_list!(visitor, visit_struct_field, struct_definition.fields());
668 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) {
669 walk_opt_name(visitor, struct_field.span, struct_field.node.name());
670 visitor.visit_ty(&struct_field.node.ty);
671 walk_list!(visitor, visit_attribute, &struct_field.node.attrs);
674 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
675 walk_list!(visitor, visit_stmt, &block.stmts);
676 walk_list!(visitor, visit_expr, &block.expr);
679 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
680 match statement.node {
681 StmtDecl(ref declaration, _) => visitor.visit_decl(declaration),
682 StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
683 visitor.visit_expr(expression)
688 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
689 match declaration.node {
690 DeclLocal(ref local) => visitor.visit_local(local),
691 DeclItem(item) => visitor.visit_nested_item(item),
695 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
696 match expression.node {
697 ExprBox(ref subexpression) => {
698 visitor.visit_expr(subexpression)
700 ExprVec(ref subexpressions) => {
701 walk_list!(visitor, visit_expr, subexpressions);
703 ExprRepeat(ref element, ref count) => {
704 visitor.visit_expr(element);
705 visitor.visit_expr(count)
707 ExprStruct(ref path, ref fields, ref optional_base) => {
708 visitor.visit_path(path, expression.id);
709 for field in fields {
710 visitor.visit_name(field.name.span, field.name.node);
711 visitor.visit_expr(&field.expr)
713 walk_list!(visitor, visit_expr, optional_base);
715 ExprTup(ref subexpressions) => {
716 walk_list!(visitor, visit_expr, subexpressions);
718 ExprCall(ref callee_expression, ref arguments) => {
719 walk_list!(visitor, visit_expr, arguments);
720 visitor.visit_expr(callee_expression)
722 ExprMethodCall(ref name, ref types, ref arguments) => {
723 visitor.visit_name(name.span, name.node);
724 walk_list!(visitor, visit_expr, arguments);
725 walk_list!(visitor, visit_ty, types);
727 ExprBinary(_, ref left_expression, ref right_expression) => {
728 visitor.visit_expr(left_expression);
729 visitor.visit_expr(right_expression)
731 ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
732 visitor.visit_expr(subexpression)
735 ExprCast(ref subexpression, ref typ) | ExprType(ref subexpression, ref typ) => {
736 visitor.visit_expr(subexpression);
737 visitor.visit_ty(typ)
739 ExprIf(ref head_expression, ref if_block, ref optional_else) => {
740 visitor.visit_expr(head_expression);
741 visitor.visit_block(if_block);
742 walk_list!(visitor, visit_expr, optional_else);
744 ExprWhile(ref subexpression, ref block, opt_ident) => {
745 visitor.visit_expr(subexpression);
746 visitor.visit_block(block);
747 walk_opt_ident(visitor, expression.span, opt_ident)
749 ExprLoop(ref block, opt_ident) => {
750 visitor.visit_block(block);
751 walk_opt_ident(visitor, expression.span, opt_ident)
753 ExprMatch(ref subexpression, ref arms, _) => {
754 visitor.visit_expr(subexpression);
755 walk_list!(visitor, visit_arm, arms);
757 ExprClosure(_, ref function_declaration, ref body) => {
758 visitor.visit_fn(FnKind::Closure,
759 function_declaration,
764 ExprBlock(ref block) => visitor.visit_block(block),
765 ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
766 visitor.visit_expr(right_hand_expression);
767 visitor.visit_expr(left_hand_expression)
769 ExprAssignOp(_, ref left_expression, ref right_expression) => {
770 visitor.visit_expr(right_expression);
771 visitor.visit_expr(left_expression)
773 ExprField(ref subexpression, ref name) => {
774 visitor.visit_expr(subexpression);
775 visitor.visit_name(name.span, name.node);
777 ExprTupField(ref subexpression, _) => {
778 visitor.visit_expr(subexpression);
780 ExprIndex(ref main_expression, ref index_expression) => {
781 visitor.visit_expr(main_expression);
782 visitor.visit_expr(index_expression)
784 ExprRange(ref start, ref end) => {
785 walk_list!(visitor, visit_expr, start);
786 walk_list!(visitor, visit_expr, end);
788 ExprPath(ref maybe_qself, ref path) => {
789 if let Some(ref qself) = *maybe_qself {
790 visitor.visit_ty(&qself.ty);
792 visitor.visit_path(path, expression.id)
794 ExprBreak(ref opt_sp_ident) | ExprAgain(ref opt_sp_ident) => {
795 for sp_ident in opt_sp_ident {
796 visitor.visit_ident(sp_ident.span, sp_ident.node);
799 ExprRet(ref optional_expression) => {
800 walk_list!(visitor, visit_expr, optional_expression);
802 ExprInlineAsm(ref ia) => {
803 for &(_, ref input) in &ia.inputs {
804 visitor.visit_expr(&input)
806 for output in &ia.outputs {
807 visitor.visit_expr(&output.expr)
812 visitor.visit_expr_post(expression)
815 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
816 walk_list!(visitor, visit_pat, &arm.pats);
817 walk_list!(visitor, visit_expr, &arm.guard);
818 visitor.visit_expr(&arm.body);
819 walk_list!(visitor, visit_attribute, &arm.attrs);