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.
19 use rustc_span::symbol::{Ident, Symbol};
22 #[derive(Copy, Clone, Debug, PartialEq)]
28 #[derive(Copy, Clone, Debug, PartialEq)]
35 #[derive(Copy, Clone, Debug)]
37 /// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
38 Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, Option<&'a Block>),
40 /// E.g., `|x, y| body`.
41 Closure(&'a FnDecl, &'a Expr),
45 pub fn header(&self) -> Option<&'a FnHeader> {
47 FnKind::Fn(_, _, sig, _, _) => Some(&sig.header),
48 FnKind::Closure(_, _) => None,
52 pub fn ident(&self) -> Option<&Ident> {
54 FnKind::Fn(_, ident, ..) => Some(ident),
59 pub fn decl(&self) -> &'a FnDecl {
61 FnKind::Fn(_, _, sig, _, _) => &sig.decl,
62 FnKind::Closure(decl, _) => decl,
66 pub fn ctxt(&self) -> Option<FnCtxt> {
68 FnKind::Fn(ctxt, ..) => Some(*ctxt),
69 FnKind::Closure(..) => None,
74 /// Each method of the `Visitor` trait is a hook to be potentially
75 /// overridden. Each method's default implementation recursively visits
76 /// the substructure of the input via the corresponding `walk` method;
77 /// e.g., the `visit_item` method by default calls `visit::walk_item`.
79 /// If you want to ensure that your code handles every variant
80 /// explicitly, you need to override each method. (And you also need
81 /// to monitor future changes to `Visitor` in case a new method with a
82 /// new default implementation gets introduced.)
83 pub trait Visitor<'ast>: Sized {
84 fn visit_name(&mut self, _span: Span, _name: Symbol) {
87 fn visit_ident(&mut self, ident: Ident) {
88 walk_ident(self, ident);
90 fn visit_foreign_item(&mut self, i: &'ast ForeignItem) {
91 walk_foreign_item(self, i)
93 fn visit_item(&mut self, i: &'ast Item) {
96 fn visit_local(&mut self, l: &'ast Local) {
99 fn visit_block(&mut self, b: &'ast Block) {
102 fn visit_stmt(&mut self, s: &'ast Stmt) {
105 fn visit_param(&mut self, param: &'ast Param) {
106 walk_param(self, param)
108 fn visit_arm(&mut self, a: &'ast Arm) {
111 fn visit_pat(&mut self, p: &'ast Pat) {
114 fn visit_anon_const(&mut self, c: &'ast AnonConst) {
115 walk_anon_const(self, c)
117 fn visit_expr(&mut self, ex: &'ast Expr) {
120 fn visit_expr_post(&mut self, _ex: &'ast Expr) {}
121 fn visit_ty(&mut self, t: &'ast Ty) {
124 fn visit_generic_param(&mut self, param: &'ast GenericParam) {
125 walk_generic_param(self, param)
127 fn visit_generics(&mut self, g: &'ast Generics) {
128 walk_generics(self, g)
130 fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
131 walk_where_predicate(self, p)
133 fn visit_fn(&mut self, fk: FnKind<'ast>, s: Span, _: NodeId) {
136 fn visit_assoc_item(&mut self, i: &'ast AssocItem, ctxt: AssocCtxt) {
137 walk_assoc_item(self, i, ctxt)
139 fn visit_trait_ref(&mut self, t: &'ast TraitRef) {
140 walk_trait_ref(self, t)
142 fn visit_param_bound(&mut self, bounds: &'ast GenericBound) {
143 walk_param_bound(self, bounds)
145 fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
146 walk_poly_trait_ref(self, t, m)
148 fn visit_variant_data(&mut self, s: &'ast VariantData) {
149 walk_struct_def(self, s)
151 fn visit_field_def(&mut self, s: &'ast FieldDef) {
152 walk_field_def(self, s)
156 enum_definition: &'ast EnumDef,
157 generics: &'ast Generics,
161 walk_enum_def(self, enum_definition, generics, item_id)
163 fn visit_variant(&mut self, v: &'ast Variant) {
164 walk_variant(self, v)
166 fn visit_label(&mut self, label: &'ast Label) {
167 walk_label(self, label)
169 fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
170 walk_lifetime(self, lifetime)
172 fn visit_mac_call(&mut self, mac: &'ast MacCall) {
175 fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
178 fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
179 walk_path(self, path)
181 fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
182 walk_use_tree(self, use_tree, id)
184 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
185 walk_path_segment(self, path_span, path_segment)
187 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'ast GenericArgs) {
188 walk_generic_args(self, path_span, generic_args)
190 fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
191 walk_generic_arg(self, generic_arg)
193 fn visit_assoc_ty_constraint(&mut self, constraint: &'ast AssocTyConstraint) {
194 walk_assoc_ty_constraint(self, constraint)
196 fn visit_attribute(&mut self, attr: &'ast Attribute) {
197 walk_attribute(self, attr)
199 fn visit_vis(&mut self, vis: &'ast Visibility) {
202 fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FnRetTy) {
203 walk_fn_ret_ty(self, ret_ty)
205 fn visit_fn_header(&mut self, _header: &'ast FnHeader) {
208 fn visit_expr_field(&mut self, f: &'ast ExprField) {
209 walk_expr_field(self, f)
211 fn visit_pat_field(&mut self, fp: &'ast PatField) {
212 walk_pat_field(self, fp)
217 macro_rules! walk_list {
218 ($visitor: expr, $method: ident, $list: expr) => {
220 $visitor.$method(elem)
223 ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
225 $visitor.$method(elem, $($extra_args,)*)
230 pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, ident: Ident) {
231 visitor.visit_name(ident.span, ident.name);
234 pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
235 walk_list!(visitor, visit_item, &krate.items);
236 walk_list!(visitor, visit_attribute, &krate.attrs);
239 pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
240 for attr in local.attrs.iter() {
241 visitor.visit_attribute(attr);
243 visitor.visit_pat(&local.pat);
244 walk_list!(visitor, visit_ty, &local.ty);
245 if let Some((init, els)) = local.kind.init_else_opt() {
246 visitor.visit_expr(init);
247 walk_list!(visitor, visit_block, els);
251 pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, label: &'a Label) {
252 visitor.visit_ident(label.ident);
255 pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
256 visitor.visit_ident(lifetime.ident);
259 pub fn walk_poly_trait_ref<'a, V>(
261 trait_ref: &'a PolyTraitRef,
262 _: &TraitBoundModifier,
266 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
267 visitor.visit_trait_ref(&trait_ref.trait_ref);
270 pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) {
271 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
274 pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
275 visitor.visit_vis(&item.vis);
276 visitor.visit_ident(item.ident);
278 ItemKind::ExternCrate(orig_name) => {
279 if let Some(orig_name) = orig_name {
280 visitor.visit_name(item.span, orig_name);
283 ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
284 ItemKind::Static(ref typ, _, ref expr) | ItemKind::Const(_, ref typ, ref expr) => {
285 visitor.visit_ty(typ);
286 walk_list!(visitor, visit_expr, expr);
288 ItemKind::Fn(box FnKind(_, ref sig, ref generics, ref body)) => {
289 visitor.visit_generics(generics);
290 let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref());
291 visitor.visit_fn(kind, item.span, item.id)
293 ItemKind::Mod(_unsafety, ref mod_kind) => match mod_kind {
294 ModKind::Loaded(items, _inline, _inner_span) => {
295 walk_list!(visitor, visit_item, items)
297 ModKind::Unloaded => {}
299 ItemKind::ForeignMod(ref foreign_module) => {
300 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
302 ItemKind::GlobalAsm(ref asm) => walk_inline_asm(visitor, asm),
303 ItemKind::TyAlias(box TyAliasKind(_, ref generics, ref bounds, ref ty)) => {
304 visitor.visit_generics(generics);
305 walk_list!(visitor, visit_param_bound, bounds);
306 walk_list!(visitor, visit_ty, ty);
308 ItemKind::Enum(ref enum_definition, ref generics) => {
309 visitor.visit_generics(generics);
310 visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
312 ItemKind::Impl(box ImplKind {
322 visitor.visit_generics(generics);
323 walk_list!(visitor, visit_trait_ref, of_trait);
324 visitor.visit_ty(self_ty);
325 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Impl);
327 ItemKind::Struct(ref struct_definition, ref generics)
328 | ItemKind::Union(ref struct_definition, ref generics) => {
329 visitor.visit_generics(generics);
330 visitor.visit_variant_data(struct_definition);
332 ItemKind::Trait(box TraitKind(.., ref generics, ref bounds, ref items)) => {
333 visitor.visit_generics(generics);
334 walk_list!(visitor, visit_param_bound, bounds);
335 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
337 ItemKind::TraitAlias(ref generics, ref bounds) => {
338 visitor.visit_generics(generics);
339 walk_list!(visitor, visit_param_bound, bounds);
341 ItemKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
342 ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
344 walk_list!(visitor, visit_attribute, &item.attrs);
347 pub fn walk_enum_def<'a, V: Visitor<'a>>(
349 enum_definition: &'a EnumDef,
353 walk_list!(visitor, visit_variant, &enum_definition.variants);
356 pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant)
360 visitor.visit_ident(variant.ident);
361 visitor.visit_vis(&variant.vis);
362 visitor.visit_variant_data(&variant.data);
363 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
364 walk_list!(visitor, visit_attribute, &variant.attrs);
367 pub fn walk_expr_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a ExprField) {
368 visitor.visit_expr(&f.expr);
369 visitor.visit_ident(f.ident);
370 walk_list!(visitor, visit_attribute, f.attrs.iter());
373 pub fn walk_pat_field<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a PatField) {
374 visitor.visit_ident(fp.ident);
375 visitor.visit_pat(&fp.pat);
376 walk_list!(visitor, visit_attribute, fp.attrs.iter());
379 pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
381 TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => visitor.visit_ty(ty),
382 TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
383 TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
384 walk_list!(visitor, visit_lifetime, opt_lifetime);
385 visitor.visit_ty(&mutable_type.ty)
387 TyKind::Tup(ref tuple_element_types) => {
388 walk_list!(visitor, visit_ty, tuple_element_types);
390 TyKind::BareFn(ref function_declaration) => {
391 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
392 walk_fn_decl(visitor, &function_declaration.decl);
394 TyKind::Path(ref maybe_qself, ref path) => {
395 if let Some(ref qself) = *maybe_qself {
396 visitor.visit_ty(&qself.ty);
398 visitor.visit_path(path, typ.id);
400 TyKind::Array(ref ty, ref length) => {
401 visitor.visit_ty(ty);
402 visitor.visit_anon_const(length)
404 TyKind::TraitObject(ref bounds, ..) | TyKind::ImplTrait(_, ref bounds) => {
405 walk_list!(visitor, visit_param_bound, bounds);
407 TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
408 TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
409 TyKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
410 TyKind::AnonymousStruct(ref fields, ..) | TyKind::AnonymousUnion(ref fields, ..) => {
411 walk_list!(visitor, visit_field_def, fields)
413 TyKind::Never | TyKind::CVarArgs => {}
417 pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
418 for segment in &path.segments {
419 visitor.visit_path_segment(path.span, segment);
423 pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree, id: NodeId) {
424 visitor.visit_path(&use_tree.prefix, id);
425 match use_tree.kind {
426 UseTreeKind::Simple(rename, ..) => {
427 // The extra IDs are handled during HIR lowering.
428 if let Some(rename) = rename {
429 visitor.visit_ident(rename);
432 UseTreeKind::Glob => {}
433 UseTreeKind::Nested(ref use_trees) => {
434 for &(ref nested_tree, nested_id) in use_trees {
435 visitor.visit_use_tree(nested_tree, nested_id, true);
441 pub fn walk_path_segment<'a, V: Visitor<'a>>(
444 segment: &'a PathSegment,
446 visitor.visit_ident(segment.ident);
447 if let Some(ref args) = segment.args {
448 visitor.visit_generic_args(path_span, args);
452 pub fn walk_generic_args<'a, V>(visitor: &mut V, _path_span: Span, generic_args: &'a GenericArgs)
456 match *generic_args {
457 GenericArgs::AngleBracketed(ref data) => {
458 for arg in &data.args {
460 AngleBracketedArg::Arg(a) => visitor.visit_generic_arg(a),
461 AngleBracketedArg::Constraint(c) => visitor.visit_assoc_ty_constraint(c),
465 GenericArgs::Parenthesized(ref data) => {
466 walk_list!(visitor, visit_ty, &data.inputs);
467 walk_fn_ret_ty(visitor, &data.output);
472 pub fn walk_generic_arg<'a, V>(visitor: &mut V, generic_arg: &'a GenericArg)
477 GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt),
478 GenericArg::Type(ty) => visitor.visit_ty(ty),
479 GenericArg::Const(ct) => visitor.visit_anon_const(ct),
483 pub fn walk_assoc_ty_constraint<'a, V: Visitor<'a>>(
485 constraint: &'a AssocTyConstraint,
487 visitor.visit_ident(constraint.ident);
488 if let Some(ref gen_args) = constraint.gen_args {
489 visitor.visit_generic_args(gen_args.span(), gen_args);
491 match constraint.kind {
492 AssocTyConstraintKind::Equality { ref ty } => {
493 visitor.visit_ty(ty);
495 AssocTyConstraintKind::Bound { ref bounds } => {
496 walk_list!(visitor, visit_param_bound, bounds);
501 pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
503 PatKind::TupleStruct(ref opt_qself, ref path, ref elems) => {
504 if let Some(ref qself) = *opt_qself {
505 visitor.visit_ty(&qself.ty);
507 visitor.visit_path(path, pattern.id);
508 walk_list!(visitor, visit_pat, elems);
510 PatKind::Path(ref opt_qself, ref path) => {
511 if let Some(ref qself) = *opt_qself {
512 visitor.visit_ty(&qself.ty);
514 visitor.visit_path(path, pattern.id)
516 PatKind::Struct(ref opt_qself, ref path, ref fields, _) => {
517 if let Some(ref qself) = *opt_qself {
518 visitor.visit_ty(&qself.ty);
520 visitor.visit_path(path, pattern.id);
521 walk_list!(visitor, visit_pat_field, fields);
523 PatKind::Box(ref subpattern)
524 | PatKind::Ref(ref subpattern, _)
525 | PatKind::Paren(ref subpattern) => visitor.visit_pat(subpattern),
526 PatKind::Ident(_, ident, ref optional_subpattern) => {
527 visitor.visit_ident(ident);
528 walk_list!(visitor, visit_pat, optional_subpattern);
530 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
531 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
532 walk_list!(visitor, visit_expr, lower_bound);
533 walk_list!(visitor, visit_expr, upper_bound);
535 PatKind::Wild | PatKind::Rest => {}
536 PatKind::Tuple(ref elems) | PatKind::Slice(ref elems) | PatKind::Or(ref elems) => {
537 walk_list!(visitor, visit_pat, elems);
539 PatKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
543 pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignItem) {
544 let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = *item;
545 visitor.visit_vis(vis);
546 visitor.visit_ident(ident);
547 walk_list!(visitor, visit_attribute, attrs);
549 ForeignItemKind::Static(ty, _, expr) => {
550 visitor.visit_ty(ty);
551 walk_list!(visitor, visit_expr, expr);
553 ForeignItemKind::Fn(box FnKind(_, sig, generics, body)) => {
554 visitor.visit_generics(generics);
555 let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, body.as_deref());
556 visitor.visit_fn(kind, span, id);
558 ForeignItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => {
559 visitor.visit_generics(generics);
560 walk_list!(visitor, visit_param_bound, bounds);
561 walk_list!(visitor, visit_ty, ty);
563 ForeignItemKind::MacCall(mac) => {
564 visitor.visit_mac_call(mac);
569 pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
571 GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
572 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
576 pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
577 visitor.visit_ident(param.ident);
578 walk_list!(visitor, visit_attribute, param.attrs.iter());
579 walk_list!(visitor, visit_param_bound, ¶m.bounds);
581 GenericParamKind::Lifetime => (),
582 GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
583 GenericParamKind::Const { ref ty, ref default, .. } => {
584 visitor.visit_ty(ty);
585 if let Some(default) = default {
586 visitor.visit_anon_const(default);
592 pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
593 walk_list!(visitor, visit_generic_param, &generics.params);
594 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
597 pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
599 WherePredicate::BoundPredicate(WhereBoundPredicate {
602 ref bound_generic_params,
605 visitor.visit_ty(bounded_ty);
606 walk_list!(visitor, visit_param_bound, bounds);
607 walk_list!(visitor, visit_generic_param, bound_generic_params);
609 WherePredicate::RegionPredicate(WhereRegionPredicate {
610 ref lifetime, ref bounds, ..
612 visitor.visit_lifetime(lifetime);
613 walk_list!(visitor, visit_param_bound, bounds);
615 WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }) => {
616 visitor.visit_ty(lhs_ty);
617 visitor.visit_ty(rhs_ty);
622 pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy) {
623 if let FnRetTy::Ty(ref output_ty) = *ret_ty {
624 visitor.visit_ty(output_ty)
628 pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
629 for param in &function_declaration.inputs {
630 visitor.visit_param(param);
632 visitor.visit_fn_ret_ty(&function_declaration.output);
635 pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Span) {
637 FnKind::Fn(_, _, sig, _, body) => {
638 visitor.visit_fn_header(&sig.header);
639 walk_fn_decl(visitor, &sig.decl);
640 walk_list!(visitor, visit_block, body);
642 FnKind::Closure(decl, body) => {
643 walk_fn_decl(visitor, decl);
644 visitor.visit_expr(body);
649 pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, ctxt: AssocCtxt) {
650 let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = *item;
651 visitor.visit_vis(vis);
652 visitor.visit_ident(ident);
653 walk_list!(visitor, visit_attribute, attrs);
655 AssocItemKind::Const(_, ty, expr) => {
656 visitor.visit_ty(ty);
657 walk_list!(visitor, visit_expr, expr);
659 AssocItemKind::Fn(box FnKind(_, sig, generics, body)) => {
660 visitor.visit_generics(generics);
661 let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, body.as_deref());
662 visitor.visit_fn(kind, span, id);
664 AssocItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => {
665 visitor.visit_generics(generics);
666 walk_list!(visitor, visit_param_bound, bounds);
667 walk_list!(visitor, visit_ty, ty);
669 AssocItemKind::MacCall(mac) => {
670 visitor.visit_mac_call(mac);
675 pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
676 walk_list!(visitor, visit_field_def, struct_definition.fields());
679 pub fn walk_field_def<'a, V: Visitor<'a>>(visitor: &mut V, field: &'a FieldDef) {
680 visitor.visit_vis(&field.vis);
681 if let Some(ident) = field.ident {
682 visitor.visit_ident(ident);
684 visitor.visit_ty(&field.ty);
685 walk_list!(visitor, visit_attribute, &field.attrs);
688 pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
689 walk_list!(visitor, visit_stmt, &block.stmts);
692 pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
693 match statement.kind {
694 StmtKind::Local(ref local) => visitor.visit_local(local),
695 StmtKind::Item(ref item) => visitor.visit_item(item),
696 StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => visitor.visit_expr(expr),
697 StmtKind::Empty => {}
698 StmtKind::MacCall(ref mac) => {
699 let MacCallStmt { ref mac, style: _, ref attrs, tokens: _ } = **mac;
700 visitor.visit_mac_call(mac);
701 for attr in attrs.iter() {
702 visitor.visit_attribute(attr);
708 pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a MacCall) {
709 visitor.visit_path(&mac.path, DUMMY_NODE_ID);
712 pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
713 visitor.visit_expr(&constant.value);
716 fn walk_inline_asm<'a, V: Visitor<'a>>(visitor: &mut V, asm: &'a InlineAsm) {
717 for (op, _) in &asm.operands {
719 InlineAsmOperand::In { expr, .. }
720 | InlineAsmOperand::Out { expr: Some(expr), .. }
721 | InlineAsmOperand::InOut { expr, .. }
722 | InlineAsmOperand::Sym { expr, .. } => visitor.visit_expr(expr),
723 InlineAsmOperand::Out { expr: None, .. } => {}
724 InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
725 visitor.visit_expr(in_expr);
726 if let Some(out_expr) = out_expr {
727 visitor.visit_expr(out_expr);
730 InlineAsmOperand::Const { anon_const, .. } => visitor.visit_anon_const(anon_const),
735 pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
736 walk_list!(visitor, visit_attribute, expression.attrs.iter());
738 match expression.kind {
739 ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
740 ExprKind::Array(ref subexpressions) => {
741 walk_list!(visitor, visit_expr, subexpressions);
743 ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
744 ExprKind::Repeat(ref element, ref count) => {
745 visitor.visit_expr(element);
746 visitor.visit_anon_const(count)
748 ExprKind::Struct(ref se) => {
749 if let Some(ref qself) = se.qself {
750 visitor.visit_ty(&qself.ty);
752 visitor.visit_path(&se.path, expression.id);
753 walk_list!(visitor, visit_expr_field, &se.fields);
755 StructRest::Base(expr) => visitor.visit_expr(expr),
756 StructRest::Rest(_span) => {}
757 StructRest::None => {}
760 ExprKind::Tup(ref subexpressions) => {
761 walk_list!(visitor, visit_expr, subexpressions);
763 ExprKind::Call(ref callee_expression, ref arguments) => {
764 visitor.visit_expr(callee_expression);
765 walk_list!(visitor, visit_expr, arguments);
767 ExprKind::MethodCall(ref segment, ref arguments, _span) => {
768 visitor.visit_path_segment(expression.span, segment);
769 walk_list!(visitor, visit_expr, arguments);
771 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
772 visitor.visit_expr(left_expression);
773 visitor.visit_expr(right_expression)
775 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
776 visitor.visit_expr(subexpression)
778 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
779 visitor.visit_expr(subexpression);
780 visitor.visit_ty(typ)
782 ExprKind::Let(ref pat, ref expr, _) => {
783 visitor.visit_pat(pat);
784 visitor.visit_expr(expr);
786 ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
787 visitor.visit_expr(head_expression);
788 visitor.visit_block(if_block);
789 walk_list!(visitor, visit_expr, optional_else);
791 ExprKind::While(ref subexpression, ref block, ref opt_label) => {
792 walk_list!(visitor, visit_label, opt_label);
793 visitor.visit_expr(subexpression);
794 visitor.visit_block(block);
796 ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
797 walk_list!(visitor, visit_label, opt_label);
798 visitor.visit_pat(pattern);
799 visitor.visit_expr(subexpression);
800 visitor.visit_block(block);
802 ExprKind::Loop(ref block, ref opt_label) => {
803 walk_list!(visitor, visit_label, opt_label);
804 visitor.visit_block(block);
806 ExprKind::Match(ref subexpression, ref arms) => {
807 visitor.visit_expr(subexpression);
808 walk_list!(visitor, visit_arm, arms);
810 ExprKind::Closure(_, _, _, ref decl, ref body, _decl_span) => {
811 visitor.visit_fn(FnKind::Closure(decl, body), expression.span, expression.id)
813 ExprKind::Block(ref block, ref opt_label) => {
814 walk_list!(visitor, visit_label, opt_label);
815 visitor.visit_block(block);
817 ExprKind::Async(_, _, ref body) => {
818 visitor.visit_block(body);
820 ExprKind::Await(ref expr) => visitor.visit_expr(expr),
821 ExprKind::Assign(ref lhs, ref rhs, _) => {
822 visitor.visit_expr(lhs);
823 visitor.visit_expr(rhs);
825 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
826 visitor.visit_expr(left_expression);
827 visitor.visit_expr(right_expression);
829 ExprKind::Field(ref subexpression, ident) => {
830 visitor.visit_expr(subexpression);
831 visitor.visit_ident(ident);
833 ExprKind::Index(ref main_expression, ref index_expression) => {
834 visitor.visit_expr(main_expression);
835 visitor.visit_expr(index_expression)
837 ExprKind::Range(ref start, ref end, _) => {
838 walk_list!(visitor, visit_expr, start);
839 walk_list!(visitor, visit_expr, end);
841 ExprKind::Underscore => {}
842 ExprKind::Path(ref maybe_qself, ref path) => {
843 if let Some(ref qself) = *maybe_qself {
844 visitor.visit_ty(&qself.ty);
846 visitor.visit_path(path, expression.id)
848 ExprKind::Break(ref opt_label, ref opt_expr) => {
849 walk_list!(visitor, visit_label, opt_label);
850 walk_list!(visitor, visit_expr, opt_expr);
852 ExprKind::Continue(ref opt_label) => {
853 walk_list!(visitor, visit_label, opt_label);
855 ExprKind::Ret(ref optional_expression) => {
856 walk_list!(visitor, visit_expr, optional_expression);
858 ExprKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
859 ExprKind::Paren(ref subexpression) => visitor.visit_expr(subexpression),
860 ExprKind::InlineAsm(ref asm) => walk_inline_asm(visitor, asm),
861 ExprKind::LlvmInlineAsm(ref ia) => {
862 for &(_, ref input) in &ia.inputs {
863 visitor.visit_expr(input)
865 for output in &ia.outputs {
866 visitor.visit_expr(&output.expr)
869 ExprKind::Yield(ref optional_expression) => {
870 walk_list!(visitor, visit_expr, optional_expression);
872 ExprKind::Try(ref subexpression) => visitor.visit_expr(subexpression),
873 ExprKind::TryBlock(ref body) => visitor.visit_block(body),
874 ExprKind::Lit(_) | ExprKind::Err => {}
877 visitor.visit_expr_post(expression)
880 pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {
881 walk_list!(visitor, visit_attribute, param.attrs.iter());
882 visitor.visit_pat(¶m.pat);
883 visitor.visit_ty(¶m.ty);
886 pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
887 visitor.visit_pat(&arm.pat);
888 walk_list!(visitor, visit_expr, &arm.guard);
889 visitor.visit_expr(&arm.body);
890 walk_list!(visitor, visit_attribute, &arm.attrs);
893 pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
894 if let VisibilityKind::Restricted { ref path, id } = vis.kind {
895 visitor.visit_path(path, id);
899 pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
901 AttrKind::Normal(ref item, ref _tokens) => walk_mac_args(visitor, &item.args),
902 AttrKind::DocComment(..) => {}
906 pub fn walk_mac_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a MacArgs) {
909 MacArgs::Delimited(_dspan, _delim, _tokens) => {}
910 // The value in `#[key = VALUE]` must be visited as an expression for backward
911 // compatibility, so that macros can be expanded in that position.
912 MacArgs::Eq(_eq_span, token) => match &token.kind {
913 token::Interpolated(nt) => match &**nt {
914 token::NtExpr(expr) => visitor.visit_expr(expr),
915 t => panic!("unexpected token in key-value attribute: {:?}", t),
917 t => panic!("unexpected token in key-value attribute: {:?}", t),