1 //! AST walker. Each overridden visit method has full control over what
2 //! happens with its node, it can do its own traversal of the node's children,
3 //! call `visit::walk_*` to apply the default traversal algorithm, or prevent
4 //! deeper traversal by doing nothing.
6 //! Note: it is an important invariant that the default visitor walks the body
7 //! of a function in "execution order" (more concretely, reverse post-order
8 //! with respect to the CFG implied by the AST), meaning that if AST node A may
9 //! execute before AST node B, then A is visited first. The borrow checker in
10 //! particular relies on this property.
12 //! Note: walking an AST before macro expansion is probably a bad idea. For
13 //! instance, a walker looking for item names in a module will miss all of
14 //! those that are created by the expansion of a macro.
17 use crate::token::Token;
18 use crate::tokenstream::{TokenTree, TokenStream};
22 #[derive(Copy, Clone)]
24 /// E.g., `fn foo()` or `extern "Abi" fn foo()`.
25 ItemFn(Ident, &'a FnHeader, &'a Visibility, &'a Block),
27 /// E.g., `fn foo(&self)`.
28 Method(Ident, &'a FnSig, &'a Visibility, &'a Block),
30 /// E.g., `|x, y| body`.
35 pub fn header(&self) -> Option<&'a FnHeader> {
37 FnKind::ItemFn(_, header, _, _) => Some(header),
38 FnKind::Method(_, sig, _, _) => Some(&sig.header),
39 FnKind::Closure(_) => None,
44 /// Each method of the `Visitor` trait is a hook to be potentially
45 /// overridden. Each method's default implementation recursively visits
46 /// the substructure of the input via the corresponding `walk` method;
47 /// e.g., the `visit_mod` method by default calls `visit::walk_mod`.
49 /// If you want to ensure that your code handles every variant
50 /// explicitly, you need to override each method. (And you also need
51 /// to monitor future changes to `Visitor` in case a new method with a
52 /// new default implementation gets introduced.)
53 pub trait Visitor<'ast>: Sized {
54 fn visit_name(&mut self, _span: Span, _name: Name) {
57 fn visit_ident(&mut self, ident: Ident) {
58 walk_ident(self, ident);
60 fn visit_mod(&mut self, m: &'ast Mod, _s: Span, _attrs: &[Attribute], _n: NodeId) {
63 fn visit_foreign_item(&mut self, i: &'ast ForeignItem) { walk_foreign_item(self, i) }
64 fn visit_global_asm(&mut self, ga: &'ast GlobalAsm) { walk_global_asm(self, ga) }
65 fn visit_item(&mut self, i: &'ast Item) { walk_item(self, i) }
66 fn visit_local(&mut self, l: &'ast Local) { walk_local(self, l) }
67 fn visit_block(&mut self, b: &'ast Block) { walk_block(self, b) }
68 fn visit_stmt(&mut self, s: &'ast Stmt) { walk_stmt(self, s) }
69 fn visit_param(&mut self, param: &'ast Param) { walk_param(self, param) }
70 fn visit_arm(&mut self, a: &'ast Arm) { walk_arm(self, a) }
71 fn visit_pat(&mut self, p: &'ast Pat) { walk_pat(self, p) }
72 fn visit_anon_const(&mut self, c: &'ast AnonConst) { walk_anon_const(self, c) }
73 fn visit_expr(&mut self, ex: &'ast Expr) { walk_expr(self, ex) }
74 fn visit_expr_post(&mut self, _ex: &'ast Expr) { }
75 fn visit_ty(&mut self, t: &'ast Ty) { walk_ty(self, t) }
76 fn visit_generic_param(&mut self, param: &'ast GenericParam) {
77 walk_generic_param(self, param)
79 fn visit_generics(&mut self, g: &'ast Generics) { walk_generics(self, g) }
80 fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
81 walk_where_predicate(self, p)
83 fn visit_fn(&mut self, fk: FnKind<'ast>, fd: &'ast FnDecl, s: Span, _: NodeId) {
84 walk_fn(self, fk, fd, s)
86 fn visit_trait_item(&mut self, i: &'ast AssocItem) { walk_trait_item(self, i) }
87 fn visit_impl_item(&mut self, i: &'ast AssocItem) { walk_impl_item(self, i) }
88 fn visit_assoc_item(&mut self, i: &'ast AssocItem) { walk_assoc_item(self, i) }
89 fn visit_trait_ref(&mut self, t: &'ast TraitRef) { walk_trait_ref(self, t) }
90 fn visit_param_bound(&mut self, bounds: &'ast GenericBound) {
91 walk_param_bound(self, bounds)
93 fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
94 walk_poly_trait_ref(self, t, m)
96 fn visit_variant_data(&mut self, s: &'ast VariantData) {
97 walk_struct_def(self, s)
99 fn visit_struct_field(&mut self, s: &'ast StructField) { walk_struct_field(self, s) }
100 fn visit_enum_def(&mut self, enum_definition: &'ast EnumDef,
101 generics: &'ast Generics, item_id: NodeId, _: Span) {
102 walk_enum_def(self, enum_definition, generics, item_id)
104 fn visit_variant(&mut self, v: &'ast Variant) {
105 walk_variant(self, v)
107 fn visit_label(&mut self, label: &'ast Label) {
108 walk_label(self, label)
110 fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
111 walk_lifetime(self, lifetime)
113 fn visit_mac(&mut self, _mac: &'ast Mac) {
114 panic!("visit_mac disabled by default");
115 // N.B., see note about macros above.
116 // if you really want a visitor that
117 // works on macros, use this
118 // definition in your trait impl:
119 // visit::walk_mac(self, _mac)
121 fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
124 fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
125 walk_path(self, path)
127 fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
128 walk_use_tree(self, use_tree, id)
130 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
131 walk_path_segment(self, path_span, path_segment)
133 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'ast GenericArgs) {
134 walk_generic_args(self, path_span, generic_args)
136 fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
138 GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
139 GenericArg::Type(ty) => self.visit_ty(ty),
140 GenericArg::Const(ct) => self.visit_anon_const(ct),
143 fn visit_assoc_ty_constraint(&mut self, constraint: &'ast AssocTyConstraint) {
144 walk_assoc_ty_constraint(self, constraint)
146 fn visit_attribute(&mut self, attr: &'ast Attribute) {
147 walk_attribute(self, attr)
149 fn visit_tt(&mut self, tt: TokenTree) {
152 fn visit_tts(&mut self, tts: TokenStream) {
155 fn visit_token(&mut self, _t: Token) {}
156 // FIXME: add `visit_interpolated` and `walk_interpolated`
157 fn visit_vis(&mut self, vis: &'ast Visibility) {
160 fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FunctionRetTy) {
161 walk_fn_ret_ty(self, ret_ty)
163 fn visit_fn_header(&mut self, _header: &'ast FnHeader) {
166 fn visit_field(&mut self, f: &'ast Field) {
169 fn visit_field_pattern(&mut self, fp: &'ast FieldPat) {
170 walk_field_pattern(self, fp)
175 macro_rules! walk_list {
176 ($visitor: expr, $method: ident, $list: expr) => {
178 $visitor.$method(elem)
181 ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
183 $visitor.$method(elem, $($extra_args,)*)
188 pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, ident: Ident) {
189 visitor.visit_name(ident.span, ident.name);
192 pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
193 visitor.visit_mod(&krate.module, krate.span, &krate.attrs, CRATE_NODE_ID);
194 walk_list!(visitor, visit_attribute, &krate.attrs);
197 pub fn walk_mod<'a, V: Visitor<'a>>(visitor: &mut V, module: &'a Mod) {
198 walk_list!(visitor, visit_item, &module.items);
201 pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
202 for attr in local.attrs.iter() {
203 visitor.visit_attribute(attr);
205 visitor.visit_pat(&local.pat);
206 walk_list!(visitor, visit_ty, &local.ty);
207 walk_list!(visitor, visit_expr, &local.init);
210 pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, label: &'a Label) {
211 visitor.visit_ident(label.ident);
214 pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
215 visitor.visit_ident(lifetime.ident);
218 pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V,
219 trait_ref: &'a PolyTraitRef,
220 _: &TraitBoundModifier)
221 where V: Visitor<'a>,
223 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
224 visitor.visit_trait_ref(&trait_ref.trait_ref);
227 pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) {
228 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
231 pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
232 visitor.visit_vis(&item.vis);
233 visitor.visit_ident(item.ident);
235 ItemKind::ExternCrate(orig_name) => {
236 if let Some(orig_name) = orig_name {
237 visitor.visit_name(item.span, orig_name);
240 ItemKind::Use(ref use_tree) => {
241 visitor.visit_use_tree(use_tree, item.id, false)
243 ItemKind::Static(ref typ, _, ref expr) |
244 ItemKind::Const(ref typ, ref expr) => {
245 visitor.visit_ty(typ);
246 visitor.visit_expr(expr);
248 ItemKind::Fn(ref sig, ref generics, ref body) => {
249 visitor.visit_generics(generics);
250 visitor.visit_fn_header(&sig.header);
251 visitor.visit_fn(FnKind::ItemFn(item.ident, &sig.header, &item.vis, body),
256 ItemKind::Mod(ref module) => {
257 visitor.visit_mod(module, item.span, &item.attrs, item.id)
259 ItemKind::ForeignMod(ref foreign_module) => {
260 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
262 ItemKind::GlobalAsm(ref ga) => visitor.visit_global_asm(ga),
263 ItemKind::TyAlias(ref typ, ref generics) => {
264 visitor.visit_ty(typ);
265 visitor.visit_generics(generics)
267 ItemKind::Enum(ref enum_definition, ref generics) => {
268 visitor.visit_generics(generics);
269 visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
271 ItemKind::Impl(_, _, _,
273 ref opt_trait_reference,
276 visitor.visit_generics(generics);
277 walk_list!(visitor, visit_trait_ref, opt_trait_reference);
278 visitor.visit_ty(typ);
279 walk_list!(visitor, visit_impl_item, impl_items);
281 ItemKind::Struct(ref struct_definition, ref generics) |
282 ItemKind::Union(ref struct_definition, ref generics) => {
283 visitor.visit_generics(generics);
284 visitor.visit_variant_data(struct_definition);
286 ItemKind::Trait(.., ref generics, ref bounds, ref methods) => {
287 visitor.visit_generics(generics);
288 walk_list!(visitor, visit_param_bound, bounds);
289 walk_list!(visitor, visit_trait_item, methods);
291 ItemKind::TraitAlias(ref generics, ref bounds) => {
292 visitor.visit_generics(generics);
293 walk_list!(visitor, visit_param_bound, bounds);
295 ItemKind::Mac(ref mac) => visitor.visit_mac(mac),
296 ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
298 walk_list!(visitor, visit_attribute, &item.attrs);
301 pub fn walk_enum_def<'a, V: Visitor<'a>>(
303 enum_definition: &'a EnumDef,
307 walk_list!(visitor, visit_variant, &enum_definition.variants);
310 pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant)
311 where V: Visitor<'a>,
313 visitor.visit_ident(variant.ident);
314 visitor.visit_vis(&variant.vis);
315 visitor.visit_variant_data(&variant.data);
316 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
317 walk_list!(visitor, visit_attribute, &variant.attrs);
320 pub fn walk_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a Field) {
321 visitor.visit_expr(&f.expr);
322 visitor.visit_ident(f.ident);
323 walk_list!(visitor, visit_attribute, f.attrs.iter());
326 pub fn walk_field_pattern<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a FieldPat) {
327 visitor.visit_ident(fp.ident);
328 visitor.visit_pat(&fp.pat);
329 walk_list!(visitor, visit_attribute, fp.attrs.iter());
332 pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
334 TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => {
337 TyKind::Ptr(ref mutable_type) => {
338 visitor.visit_ty(&mutable_type.ty)
340 TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
341 walk_list!(visitor, visit_lifetime, opt_lifetime);
342 visitor.visit_ty(&mutable_type.ty)
344 TyKind::Tup(ref tuple_element_types) => {
345 walk_list!(visitor, visit_ty, tuple_element_types);
347 TyKind::BareFn(ref function_declaration) => {
348 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
349 walk_fn_decl(visitor, &function_declaration.decl);
351 TyKind::Path(ref maybe_qself, ref path) => {
352 if let Some(ref qself) = *maybe_qself {
353 visitor.visit_ty(&qself.ty);
355 visitor.visit_path(path, typ.id);
357 TyKind::Array(ref ty, ref length) => {
358 visitor.visit_ty(ty);
359 visitor.visit_anon_const(length)
361 TyKind::TraitObject(ref bounds, ..) |
362 TyKind::ImplTrait(_, ref bounds) => {
363 walk_list!(visitor, visit_param_bound, bounds);
365 TyKind::Typeof(ref expression) => {
366 visitor.visit_anon_const(expression)
368 TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
369 TyKind::Mac(ref mac) => {
370 visitor.visit_mac(mac)
373 TyKind::CVarArgs => {}
377 pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
378 for segment in &path.segments {
379 visitor.visit_path_segment(path.span, segment);
383 pub fn walk_use_tree<'a, V: Visitor<'a>>(
384 visitor: &mut V, use_tree: &'a UseTree, id: NodeId,
386 visitor.visit_path(&use_tree.prefix, id);
387 match use_tree.kind {
388 UseTreeKind::Simple(rename, ..) => {
389 // The extra IDs are handled during HIR lowering.
390 if let Some(rename) = rename {
391 visitor.visit_ident(rename);
394 UseTreeKind::Glob => {},
395 UseTreeKind::Nested(ref use_trees) => {
396 for &(ref nested_tree, nested_id) in use_trees {
397 visitor.visit_use_tree(nested_tree, nested_id, true);
403 pub fn walk_path_segment<'a, V: Visitor<'a>>(visitor: &mut V,
405 segment: &'a PathSegment) {
406 visitor.visit_ident(segment.ident);
407 if let Some(ref args) = segment.args {
408 visitor.visit_generic_args(path_span, args);
412 pub fn walk_generic_args<'a, V>(visitor: &mut V,
414 generic_args: &'a GenericArgs)
415 where V: Visitor<'a>,
417 match *generic_args {
418 GenericArgs::AngleBracketed(ref data) => {
419 walk_list!(visitor, visit_generic_arg, &data.args);
420 walk_list!(visitor, visit_assoc_ty_constraint, &data.constraints);
422 GenericArgs::Parenthesized(ref data) => {
423 walk_list!(visitor, visit_ty, &data.inputs);
424 walk_fn_ret_ty(visitor, &data.output);
429 pub fn walk_assoc_ty_constraint<'a, V: Visitor<'a>>(visitor: &mut V,
430 constraint: &'a AssocTyConstraint) {
431 visitor.visit_ident(constraint.ident);
432 match constraint.kind {
433 AssocTyConstraintKind::Equality { ref ty } => {
434 visitor.visit_ty(ty);
436 AssocTyConstraintKind::Bound { ref bounds } => {
437 walk_list!(visitor, visit_param_bound, bounds);
442 pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
444 PatKind::TupleStruct(ref path, ref elems) => {
445 visitor.visit_path(path, pattern.id);
446 walk_list!(visitor, visit_pat, elems);
448 PatKind::Path(ref opt_qself, ref path) => {
449 if let Some(ref qself) = *opt_qself {
450 visitor.visit_ty(&qself.ty);
452 visitor.visit_path(path, pattern.id)
454 PatKind::Struct(ref path, ref fields, _) => {
455 visitor.visit_path(path, pattern.id);
456 walk_list!(visitor, visit_field_pattern, fields);
458 PatKind::Box(ref subpattern) |
459 PatKind::Ref(ref subpattern, _) |
460 PatKind::Paren(ref subpattern) => {
461 visitor.visit_pat(subpattern)
463 PatKind::Ident(_, ident, ref optional_subpattern) => {
464 visitor.visit_ident(ident);
465 walk_list!(visitor, visit_pat, optional_subpattern);
467 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
468 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
469 visitor.visit_expr(lower_bound);
470 visitor.visit_expr(upper_bound);
472 PatKind::Wild | PatKind::Rest => {},
473 PatKind::Tuple(ref elems)
474 | PatKind::Slice(ref elems)
475 | PatKind::Or(ref elems) => {
476 walk_list!(visitor, visit_pat, elems);
478 PatKind::Mac(ref mac) => visitor.visit_mac(mac),
482 pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, foreign_item: &'a ForeignItem) {
483 visitor.visit_vis(&foreign_item.vis);
484 visitor.visit_ident(foreign_item.ident);
486 match foreign_item.kind {
487 ForeignItemKind::Fn(ref function_declaration, ref generics) => {
488 walk_fn_decl(visitor, function_declaration);
489 visitor.visit_generics(generics)
491 ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
492 ForeignItemKind::Ty => (),
493 ForeignItemKind::Macro(ref mac) => visitor.visit_mac(mac),
496 walk_list!(visitor, visit_attribute, &foreign_item.attrs);
499 pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) {
503 pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
505 GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
506 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
510 pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
511 visitor.visit_ident(param.ident);
512 walk_list!(visitor, visit_attribute, param.attrs.iter());
513 walk_list!(visitor, visit_param_bound, ¶m.bounds);
515 GenericParamKind::Lifetime => (),
516 GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
517 GenericParamKind::Const { ref ty, .. } => visitor.visit_ty(ty),
521 pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
522 walk_list!(visitor, visit_generic_param, &generics.params);
523 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
526 pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
528 WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
530 ref bound_generic_params,
532 visitor.visit_ty(bounded_ty);
533 walk_list!(visitor, visit_param_bound, bounds);
534 walk_list!(visitor, visit_generic_param, bound_generic_params);
536 WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
539 visitor.visit_lifetime(lifetime);
540 walk_list!(visitor, visit_param_bound, bounds);
542 WherePredicate::EqPredicate(WhereEqPredicate{ref lhs_ty,
545 visitor.visit_ty(lhs_ty);
546 visitor.visit_ty(rhs_ty);
551 pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FunctionRetTy) {
552 if let FunctionRetTy::Ty(ref output_ty) = *ret_ty {
553 visitor.visit_ty(output_ty)
557 pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
558 for param in &function_declaration.inputs {
559 visitor.visit_param(param);
561 visitor.visit_fn_ret_ty(&function_declaration.output);
564 pub fn walk_fn<'a, V>(visitor: &mut V, kind: FnKind<'a>, declaration: &'a FnDecl, _span: Span)
565 where V: Visitor<'a>,
568 FnKind::ItemFn(_, header, _, body) => {
569 visitor.visit_fn_header(header);
570 walk_fn_decl(visitor, declaration);
571 visitor.visit_block(body);
573 FnKind::Method(_, sig, _, body) => {
574 visitor.visit_fn_header(&sig.header);
575 walk_fn_decl(visitor, declaration);
576 visitor.visit_block(body);
578 FnKind::Closure(body) => {
579 walk_fn_decl(visitor, declaration);
580 visitor.visit_expr(body);
585 pub fn walk_impl_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem) {
586 visitor.visit_assoc_item(item);
589 pub fn walk_trait_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem) {
590 visitor.visit_assoc_item(item);
593 pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem) {
594 visitor.visit_vis(&item.vis);
595 visitor.visit_ident(item.ident);
596 walk_list!(visitor, visit_attribute, &item.attrs);
597 visitor.visit_generics(&item.generics);
599 AssocItemKind::Const(ref ty, ref expr) => {
600 visitor.visit_ty(ty);
601 walk_list!(visitor, visit_expr, expr);
603 AssocItemKind::Fn(ref sig, None) => {
604 visitor.visit_fn_header(&sig.header);
605 walk_fn_decl(visitor, &sig.decl);
607 AssocItemKind::Fn(ref sig, Some(ref body)) => {
608 visitor.visit_fn(FnKind::Method(item.ident, sig, &item.vis, body),
609 &sig.decl, item.span, item.id);
611 AssocItemKind::TyAlias(ref bounds, ref ty) => {
612 walk_list!(visitor, visit_param_bound, bounds);
613 walk_list!(visitor, visit_ty, ty);
615 AssocItemKind::Macro(ref mac) => {
616 visitor.visit_mac(mac);
621 pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
622 walk_list!(visitor, visit_struct_field, struct_definition.fields());
625 pub fn walk_struct_field<'a, V: Visitor<'a>>(visitor: &mut V, struct_field: &'a StructField) {
626 visitor.visit_vis(&struct_field.vis);
627 if let Some(ident) = struct_field.ident {
628 visitor.visit_ident(ident);
630 visitor.visit_ty(&struct_field.ty);
631 walk_list!(visitor, visit_attribute, &struct_field.attrs);
634 pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
635 walk_list!(visitor, visit_stmt, &block.stmts);
638 pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
639 match statement.kind {
640 StmtKind::Local(ref local) => visitor.visit_local(local),
641 StmtKind::Item(ref item) => visitor.visit_item(item),
642 StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
643 visitor.visit_expr(expression)
645 StmtKind::Mac(ref mac) => {
646 let (ref mac, _, ref attrs) = **mac;
647 visitor.visit_mac(mac);
648 for attr in attrs.iter() {
649 visitor.visit_attribute(attr);
655 pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a Mac) {
656 visitor.visit_path(&mac.path, DUMMY_NODE_ID);
659 pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
660 visitor.visit_expr(&constant.value);
663 pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
664 walk_list!(visitor, visit_attribute, expression.attrs.iter());
666 match expression.kind {
667 ExprKind::Box(ref subexpression) => {
668 visitor.visit_expr(subexpression)
670 ExprKind::Array(ref subexpressions) => {
671 walk_list!(visitor, visit_expr, subexpressions);
673 ExprKind::Repeat(ref element, ref count) => {
674 visitor.visit_expr(element);
675 visitor.visit_anon_const(count)
677 ExprKind::Struct(ref path, ref fields, ref optional_base) => {
678 visitor.visit_path(path, expression.id);
679 walk_list!(visitor, visit_field, fields);
680 walk_list!(visitor, visit_expr, optional_base);
682 ExprKind::Tup(ref subexpressions) => {
683 walk_list!(visitor, visit_expr, subexpressions);
685 ExprKind::Call(ref callee_expression, ref arguments) => {
686 visitor.visit_expr(callee_expression);
687 walk_list!(visitor, visit_expr, arguments);
689 ExprKind::MethodCall(ref segment, ref arguments) => {
690 visitor.visit_path_segment(expression.span, segment);
691 walk_list!(visitor, visit_expr, arguments);
693 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
694 visitor.visit_expr(left_expression);
695 visitor.visit_expr(right_expression)
697 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
698 visitor.visit_expr(subexpression)
700 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
701 visitor.visit_expr(subexpression);
702 visitor.visit_ty(typ)
704 ExprKind::Let(ref pat, ref scrutinee) => {
705 visitor.visit_pat(pat);
706 visitor.visit_expr(scrutinee);
708 ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
709 visitor.visit_expr(head_expression);
710 visitor.visit_block(if_block);
711 walk_list!(visitor, visit_expr, optional_else);
713 ExprKind::While(ref subexpression, ref block, ref opt_label) => {
714 walk_list!(visitor, visit_label, opt_label);
715 visitor.visit_expr(subexpression);
716 visitor.visit_block(block);
718 ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
719 walk_list!(visitor, visit_label, opt_label);
720 visitor.visit_pat(pattern);
721 visitor.visit_expr(subexpression);
722 visitor.visit_block(block);
724 ExprKind::Loop(ref block, ref opt_label) => {
725 walk_list!(visitor, visit_label, opt_label);
726 visitor.visit_block(block);
728 ExprKind::Match(ref subexpression, ref arms) => {
729 visitor.visit_expr(subexpression);
730 walk_list!(visitor, visit_arm, arms);
732 ExprKind::Closure(_, _, _, ref function_declaration, ref body, _decl_span) => {
733 visitor.visit_fn(FnKind::Closure(body),
734 function_declaration,
738 ExprKind::Block(ref block, ref opt_label) => {
739 walk_list!(visitor, visit_label, opt_label);
740 visitor.visit_block(block);
742 ExprKind::Async(_, _, ref body) => {
743 visitor.visit_block(body);
745 ExprKind::Await(ref expr) => visitor.visit_expr(expr),
746 ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
747 visitor.visit_expr(left_hand_expression);
748 visitor.visit_expr(right_hand_expression);
750 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
751 visitor.visit_expr(left_expression);
752 visitor.visit_expr(right_expression);
754 ExprKind::Field(ref subexpression, ident) => {
755 visitor.visit_expr(subexpression);
756 visitor.visit_ident(ident);
758 ExprKind::Index(ref main_expression, ref index_expression) => {
759 visitor.visit_expr(main_expression);
760 visitor.visit_expr(index_expression)
762 ExprKind::Range(ref start, ref end, _) => {
763 walk_list!(visitor, visit_expr, start);
764 walk_list!(visitor, visit_expr, end);
766 ExprKind::Path(ref maybe_qself, ref path) => {
767 if let Some(ref qself) = *maybe_qself {
768 visitor.visit_ty(&qself.ty);
770 visitor.visit_path(path, expression.id)
772 ExprKind::Break(ref opt_label, ref opt_expr) => {
773 walk_list!(visitor, visit_label, opt_label);
774 walk_list!(visitor, visit_expr, opt_expr);
776 ExprKind::Continue(ref opt_label) => {
777 walk_list!(visitor, visit_label, opt_label);
779 ExprKind::Ret(ref optional_expression) => {
780 walk_list!(visitor, visit_expr, optional_expression);
782 ExprKind::Mac(ref mac) => visitor.visit_mac(mac),
783 ExprKind::Paren(ref subexpression) => {
784 visitor.visit_expr(subexpression)
786 ExprKind::InlineAsm(ref ia) => {
787 for &(_, ref input) in &ia.inputs {
788 visitor.visit_expr(input)
790 for output in &ia.outputs {
791 visitor.visit_expr(&output.expr)
794 ExprKind::Yield(ref optional_expression) => {
795 walk_list!(visitor, visit_expr, optional_expression);
797 ExprKind::Try(ref subexpression) => {
798 visitor.visit_expr(subexpression)
800 ExprKind::TryBlock(ref body) => {
801 visitor.visit_block(body)
803 ExprKind::Lit(_) | ExprKind::Err => {}
806 visitor.visit_expr_post(expression)
809 pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {
810 walk_list!(visitor, visit_attribute, param.attrs.iter());
811 visitor.visit_pat(¶m.pat);
812 visitor.visit_ty(¶m.ty);
815 pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
816 visitor.visit_pat(&arm.pat);
817 walk_list!(visitor, visit_expr, &arm.guard);
818 visitor.visit_expr(&arm.body);
819 walk_list!(visitor, visit_attribute, &arm.attrs);
822 pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
823 if let VisibilityKind::Restricted { ref path, id } = vis.node {
824 visitor.visit_path(path, id);
828 pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
830 AttrKind::Normal(ref item) => walk_mac_args(visitor, &item.args),
831 AttrKind::DocComment(_) => {}
835 pub fn walk_mac_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a MacArgs) {
838 MacArgs::Delimited(_dspan, _delim, tokens) => visitor.visit_tts(tokens.clone()),
839 MacArgs::Eq(_eq_span, tokens) => visitor.visit_tts(tokens.clone()),
843 pub fn walk_tt<'a, V: Visitor<'a>>(visitor: &mut V, tt: TokenTree) {
845 TokenTree::Token(token) => visitor.visit_token(token),
846 TokenTree::Delimited(_, _, tts) => visitor.visit_tts(tts),
850 pub fn walk_tts<'a, V: Visitor<'a>>(visitor: &mut V, tts: TokenStream) {
851 for tt in tts.trees() {
852 visitor.visit_tt(tt);