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.
18 use rustc_span::symbol::{Ident, Symbol};
21 #[derive(Copy, Clone, Debug, PartialEq)]
27 #[derive(Copy, Clone, Debug, PartialEq)]
34 #[derive(Copy, Clone, Debug)]
36 /// Trait bounds in generics bounds and type/trait alias.
37 /// E.g., `<T: Bound>`, `type A: Bound`, or `where T: Bound`.
40 /// Trait bounds in `impl` type.
41 /// E.g., `type Foo = impl Bound1 + Bound2 + Bound3`.
44 /// Trait bounds in trait object type.
45 /// E.g., `dyn Bound1 + Bound2 + Bound3`.
48 /// Super traits of a trait.
49 /// E.g., `trait A: B`
53 #[derive(Copy, Clone, Debug)]
55 /// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
56 Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, &'a Generics, Option<&'a Block>),
58 /// E.g., `|x, y| body`.
59 Closure(&'a FnDecl, &'a Expr),
63 pub fn header(&self) -> Option<&'a FnHeader> {
65 FnKind::Fn(_, _, sig, _, _, _) => Some(&sig.header),
66 FnKind::Closure(_, _) => None,
70 pub fn ident(&self) -> Option<&Ident> {
72 FnKind::Fn(_, ident, ..) => Some(ident),
77 pub fn decl(&self) -> &'a FnDecl {
79 FnKind::Fn(_, _, sig, _, _, _) => &sig.decl,
80 FnKind::Closure(decl, _) => decl,
84 pub fn ctxt(&self) -> Option<FnCtxt> {
86 FnKind::Fn(ctxt, ..) => Some(*ctxt),
87 FnKind::Closure(..) => None,
92 /// Each method of the `Visitor` trait is a hook to be potentially
93 /// overridden. Each method's default implementation recursively visits
94 /// the substructure of the input via the corresponding `walk` method;
95 /// e.g., the `visit_item` method by default calls `visit::walk_item`.
97 /// If you want to ensure that your code handles every variant
98 /// explicitly, you need to override each method. (And you also need
99 /// to monitor future changes to `Visitor` in case a new method with a
100 /// new default implementation gets introduced.)
101 pub trait Visitor<'ast>: Sized {
102 fn visit_name(&mut self, _span: Span, _name: Symbol) {
105 fn visit_ident(&mut self, ident: Ident) {
106 walk_ident(self, ident);
108 fn visit_foreign_item(&mut self, i: &'ast ForeignItem) {
109 walk_foreign_item(self, i)
111 fn visit_item(&mut self, i: &'ast Item) {
114 fn visit_local(&mut self, l: &'ast Local) {
117 fn visit_block(&mut self, b: &'ast Block) {
120 fn visit_stmt(&mut self, s: &'ast Stmt) {
123 fn visit_param(&mut self, param: &'ast Param) {
124 walk_param(self, param)
126 fn visit_arm(&mut self, a: &'ast Arm) {
129 fn visit_pat(&mut self, p: &'ast Pat) {
132 fn visit_anon_const(&mut self, c: &'ast AnonConst) {
133 walk_anon_const(self, c)
135 fn visit_expr(&mut self, ex: &'ast Expr) {
138 fn visit_expr_post(&mut self, _ex: &'ast Expr) {}
139 fn visit_ty(&mut self, t: &'ast Ty) {
142 fn visit_generic_param(&mut self, param: &'ast GenericParam) {
143 walk_generic_param(self, param)
145 fn visit_generics(&mut self, g: &'ast Generics) {
146 walk_generics(self, g)
148 fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
149 walk_where_predicate(self, p)
151 fn visit_fn(&mut self, fk: FnKind<'ast>, s: Span, _: NodeId) {
154 fn visit_assoc_item(&mut self, i: &'ast AssocItem, ctxt: AssocCtxt) {
155 walk_assoc_item(self, i, ctxt)
157 fn visit_trait_ref(&mut self, t: &'ast TraitRef) {
158 walk_trait_ref(self, t)
160 fn visit_param_bound(&mut self, bounds: &'ast GenericBound, _ctxt: BoundKind) {
161 walk_param_bound(self, bounds)
163 fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
164 walk_poly_trait_ref(self, t, m)
166 fn visit_variant_data(&mut self, s: &'ast VariantData) {
167 walk_struct_def(self, s)
169 fn visit_field_def(&mut self, s: &'ast FieldDef) {
170 walk_field_def(self, s)
174 enum_definition: &'ast EnumDef,
175 generics: &'ast Generics,
179 walk_enum_def(self, enum_definition, generics, item_id)
181 fn visit_variant(&mut self, v: &'ast Variant) {
182 walk_variant(self, v)
184 fn visit_label(&mut self, label: &'ast Label) {
185 walk_label(self, label)
187 fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
188 walk_lifetime(self, lifetime)
190 fn visit_mac_call(&mut self, mac: &'ast MacCall) {
193 fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
196 fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
197 walk_path(self, path)
199 fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
200 walk_use_tree(self, use_tree, id)
202 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
203 walk_path_segment(self, path_span, path_segment)
205 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'ast GenericArgs) {
206 walk_generic_args(self, path_span, generic_args)
208 fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
209 walk_generic_arg(self, generic_arg)
211 fn visit_assoc_constraint(&mut self, constraint: &'ast AssocConstraint) {
212 walk_assoc_constraint(self, constraint)
214 fn visit_attribute(&mut self, attr: &'ast Attribute) {
215 walk_attribute(self, attr)
217 fn visit_vis(&mut self, vis: &'ast Visibility) {
220 fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FnRetTy) {
221 walk_fn_ret_ty(self, ret_ty)
223 fn visit_fn_header(&mut self, _header: &'ast FnHeader) {
226 fn visit_expr_field(&mut self, f: &'ast ExprField) {
227 walk_expr_field(self, f)
229 fn visit_pat_field(&mut self, fp: &'ast PatField) {
230 walk_pat_field(self, fp)
232 fn visit_crate(&mut self, krate: &'ast Crate) {
233 walk_crate(self, krate)
235 fn visit_inline_asm(&mut self, asm: &'ast InlineAsm) {
236 walk_inline_asm(self, asm)
238 fn visit_inline_asm_sym(&mut self, sym: &'ast InlineAsmSym) {
239 walk_inline_asm_sym(self, sym)
244 macro_rules! walk_list {
245 ($visitor: expr, $method: ident, $list: expr) => {
247 $visitor.$method(elem)
250 ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
252 $visitor.$method(elem, $($extra_args,)*)
257 pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, ident: Ident) {
258 visitor.visit_name(ident.span, ident.name);
261 pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
262 walk_list!(visitor, visit_item, &krate.items);
263 walk_list!(visitor, visit_attribute, &krate.attrs);
266 pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
267 for attr in local.attrs.iter() {
268 visitor.visit_attribute(attr);
270 visitor.visit_pat(&local.pat);
271 walk_list!(visitor, visit_ty, &local.ty);
272 if let Some((init, els)) = local.kind.init_else_opt() {
273 visitor.visit_expr(init);
274 walk_list!(visitor, visit_block, els);
278 pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, label: &'a Label) {
279 visitor.visit_ident(label.ident);
282 pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
283 visitor.visit_ident(lifetime.ident);
286 pub fn walk_poly_trait_ref<'a, V>(
288 trait_ref: &'a PolyTraitRef,
289 _: &TraitBoundModifier,
293 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
294 visitor.visit_trait_ref(&trait_ref.trait_ref);
297 pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) {
298 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
301 pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
302 visitor.visit_vis(&item.vis);
303 visitor.visit_ident(item.ident);
305 ItemKind::ExternCrate(orig_name) => {
306 if let Some(orig_name) = orig_name {
307 visitor.visit_name(item.span, orig_name);
310 ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
311 ItemKind::Static(ref typ, _, ref expr) | ItemKind::Const(_, ref typ, ref expr) => {
312 visitor.visit_ty(typ);
313 walk_list!(visitor, visit_expr, expr);
315 ItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
317 FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, body.as_deref());
318 visitor.visit_fn(kind, item.span, item.id)
320 ItemKind::Mod(_unsafety, ref mod_kind) => match mod_kind {
321 ModKind::Loaded(items, _inline, _inner_span) => {
322 walk_list!(visitor, visit_item, items)
324 ModKind::Unloaded => {}
326 ItemKind::ForeignMod(ref foreign_module) => {
327 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
329 ItemKind::GlobalAsm(ref asm) => visitor.visit_inline_asm(asm),
330 ItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. }) => {
331 visitor.visit_generics(generics);
332 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
333 walk_list!(visitor, visit_ty, ty);
335 ItemKind::Enum(ref enum_definition, ref generics) => {
336 visitor.visit_generics(generics);
337 visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
339 ItemKind::Impl(box Impl {
349 visitor.visit_generics(generics);
350 walk_list!(visitor, visit_trait_ref, of_trait);
351 visitor.visit_ty(self_ty);
352 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Impl);
354 ItemKind::Struct(ref struct_definition, ref generics)
355 | ItemKind::Union(ref struct_definition, ref generics) => {
356 visitor.visit_generics(generics);
357 visitor.visit_variant_data(struct_definition);
359 ItemKind::Trait(box Trait {
366 visitor.visit_generics(generics);
367 walk_list!(visitor, visit_param_bound, bounds, BoundKind::SuperTraits);
368 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
370 ItemKind::TraitAlias(ref generics, ref bounds) => {
371 visitor.visit_generics(generics);
372 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
374 ItemKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
375 ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
377 walk_list!(visitor, visit_attribute, &item.attrs);
380 pub fn walk_enum_def<'a, V: Visitor<'a>>(
382 enum_definition: &'a EnumDef,
386 walk_list!(visitor, visit_variant, &enum_definition.variants);
389 pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant)
393 visitor.visit_ident(variant.ident);
394 visitor.visit_vis(&variant.vis);
395 visitor.visit_variant_data(&variant.data);
396 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
397 walk_list!(visitor, visit_attribute, &variant.attrs);
400 pub fn walk_expr_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a ExprField) {
401 visitor.visit_expr(&f.expr);
402 visitor.visit_ident(f.ident);
403 walk_list!(visitor, visit_attribute, f.attrs.iter());
406 pub fn walk_pat_field<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a PatField) {
407 visitor.visit_ident(fp.ident);
408 visitor.visit_pat(&fp.pat);
409 walk_list!(visitor, visit_attribute, fp.attrs.iter());
412 pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
414 TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => visitor.visit_ty(ty),
415 TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
416 TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
417 walk_list!(visitor, visit_lifetime, opt_lifetime);
418 visitor.visit_ty(&mutable_type.ty)
420 TyKind::Tup(ref tuple_element_types) => {
421 walk_list!(visitor, visit_ty, tuple_element_types);
423 TyKind::BareFn(ref function_declaration) => {
424 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
425 walk_fn_decl(visitor, &function_declaration.decl);
427 TyKind::Path(ref maybe_qself, ref path) => {
428 if let Some(ref qself) = *maybe_qself {
429 visitor.visit_ty(&qself.ty);
431 visitor.visit_path(path, typ.id);
433 TyKind::Array(ref ty, ref length) => {
434 visitor.visit_ty(ty);
435 visitor.visit_anon_const(length)
437 TyKind::TraitObject(ref bounds, ..) => {
438 walk_list!(visitor, visit_param_bound, bounds, BoundKind::TraitObject);
440 TyKind::ImplTrait(_, ref bounds) => {
441 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Impl);
443 TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
444 TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
445 TyKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
446 TyKind::Never | TyKind::CVarArgs => {}
450 pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
451 for segment in &path.segments {
452 visitor.visit_path_segment(path.span, segment);
456 pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree, id: NodeId) {
457 visitor.visit_path(&use_tree.prefix, id);
458 match use_tree.kind {
459 UseTreeKind::Simple(rename, ..) => {
460 // The extra IDs are handled during HIR lowering.
461 if let Some(rename) = rename {
462 visitor.visit_ident(rename);
465 UseTreeKind::Glob => {}
466 UseTreeKind::Nested(ref use_trees) => {
467 for &(ref nested_tree, nested_id) in use_trees {
468 visitor.visit_use_tree(nested_tree, nested_id, true);
474 pub fn walk_path_segment<'a, V: Visitor<'a>>(
477 segment: &'a PathSegment,
479 visitor.visit_ident(segment.ident);
480 if let Some(ref args) = segment.args {
481 visitor.visit_generic_args(path_span, args);
485 pub fn walk_generic_args<'a, V>(visitor: &mut V, _path_span: Span, generic_args: &'a GenericArgs)
489 match *generic_args {
490 GenericArgs::AngleBracketed(ref data) => {
491 for arg in &data.args {
493 AngleBracketedArg::Arg(a) => visitor.visit_generic_arg(a),
494 AngleBracketedArg::Constraint(c) => visitor.visit_assoc_constraint(c),
498 GenericArgs::Parenthesized(ref data) => {
499 walk_list!(visitor, visit_ty, &data.inputs);
500 walk_fn_ret_ty(visitor, &data.output);
505 pub fn walk_generic_arg<'a, V>(visitor: &mut V, generic_arg: &'a GenericArg)
510 GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt),
511 GenericArg::Type(ty) => visitor.visit_ty(ty),
512 GenericArg::Const(ct) => visitor.visit_anon_const(ct),
516 pub fn walk_assoc_constraint<'a, V: Visitor<'a>>(visitor: &mut V, constraint: &'a AssocConstraint) {
517 visitor.visit_ident(constraint.ident);
518 if let Some(ref gen_args) = constraint.gen_args {
519 visitor.visit_generic_args(gen_args.span(), gen_args);
521 match constraint.kind {
522 AssocConstraintKind::Equality { ref term } => match term {
523 Term::Ty(ty) => visitor.visit_ty(ty),
524 Term::Const(c) => visitor.visit_anon_const(c),
526 AssocConstraintKind::Bound { ref bounds } => {
527 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
532 pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
534 PatKind::TupleStruct(ref opt_qself, ref path, ref elems) => {
535 if let Some(ref qself) = *opt_qself {
536 visitor.visit_ty(&qself.ty);
538 visitor.visit_path(path, pattern.id);
539 walk_list!(visitor, visit_pat, elems);
541 PatKind::Path(ref opt_qself, ref path) => {
542 if let Some(ref qself) = *opt_qself {
543 visitor.visit_ty(&qself.ty);
545 visitor.visit_path(path, pattern.id)
547 PatKind::Struct(ref opt_qself, ref path, ref fields, _) => {
548 if let Some(ref qself) = *opt_qself {
549 visitor.visit_ty(&qself.ty);
551 visitor.visit_path(path, pattern.id);
552 walk_list!(visitor, visit_pat_field, fields);
554 PatKind::Box(ref subpattern)
555 | PatKind::Ref(ref subpattern, _)
556 | PatKind::Paren(ref subpattern) => visitor.visit_pat(subpattern),
557 PatKind::Ident(_, ident, ref optional_subpattern) => {
558 visitor.visit_ident(ident);
559 walk_list!(visitor, visit_pat, optional_subpattern);
561 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
562 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
563 walk_list!(visitor, visit_expr, lower_bound);
564 walk_list!(visitor, visit_expr, upper_bound);
566 PatKind::Wild | PatKind::Rest => {}
567 PatKind::Tuple(ref elems) | PatKind::Slice(ref elems) | PatKind::Or(ref elems) => {
568 walk_list!(visitor, visit_pat, elems);
570 PatKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
574 pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignItem) {
575 let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = *item;
576 visitor.visit_vis(vis);
577 visitor.visit_ident(ident);
578 walk_list!(visitor, visit_attribute, attrs);
580 ForeignItemKind::Static(ty, _, expr) => {
581 visitor.visit_ty(ty);
582 walk_list!(visitor, visit_expr, expr);
584 ForeignItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
585 let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body.as_deref());
586 visitor.visit_fn(kind, span, id);
588 ForeignItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
589 visitor.visit_generics(generics);
590 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
591 walk_list!(visitor, visit_ty, ty);
593 ForeignItemKind::MacCall(mac) => {
594 visitor.visit_mac_call(mac);
599 pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
601 GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
602 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
606 pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
607 visitor.visit_ident(param.ident);
608 walk_list!(visitor, visit_attribute, param.attrs.iter());
609 walk_list!(visitor, visit_param_bound, ¶m.bounds, BoundKind::Bound);
611 GenericParamKind::Lifetime => (),
612 GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
613 GenericParamKind::Const { ref ty, ref default, .. } => {
614 visitor.visit_ty(ty);
615 if let Some(default) = default {
616 visitor.visit_anon_const(default);
622 pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
623 walk_list!(visitor, visit_generic_param, &generics.params);
624 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
627 pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
629 WherePredicate::BoundPredicate(WhereBoundPredicate {
632 ref bound_generic_params,
635 visitor.visit_ty(bounded_ty);
636 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
637 walk_list!(visitor, visit_generic_param, bound_generic_params);
639 WherePredicate::RegionPredicate(WhereRegionPredicate {
640 ref lifetime, ref bounds, ..
642 visitor.visit_lifetime(lifetime);
643 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
645 WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }) => {
646 visitor.visit_ty(lhs_ty);
647 visitor.visit_ty(rhs_ty);
652 pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy) {
653 if let FnRetTy::Ty(ref output_ty) = *ret_ty {
654 visitor.visit_ty(output_ty)
658 pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
659 for param in &function_declaration.inputs {
660 visitor.visit_param(param);
662 visitor.visit_fn_ret_ty(&function_declaration.output);
665 pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Span) {
667 FnKind::Fn(_, _, sig, _, generics, body) => {
668 visitor.visit_generics(generics);
669 visitor.visit_fn_header(&sig.header);
670 walk_fn_decl(visitor, &sig.decl);
671 walk_list!(visitor, visit_block, body);
673 FnKind::Closure(decl, body) => {
674 walk_fn_decl(visitor, decl);
675 visitor.visit_expr(body);
680 pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, ctxt: AssocCtxt) {
681 let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = *item;
682 visitor.visit_vis(vis);
683 visitor.visit_ident(ident);
684 walk_list!(visitor, visit_attribute, attrs);
686 AssocItemKind::Const(_, ty, expr) => {
687 visitor.visit_ty(ty);
688 walk_list!(visitor, visit_expr, expr);
690 AssocItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
691 let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body.as_deref());
692 visitor.visit_fn(kind, span, id);
694 AssocItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
695 visitor.visit_generics(generics);
696 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
697 walk_list!(visitor, visit_ty, ty);
699 AssocItemKind::MacCall(mac) => {
700 visitor.visit_mac_call(mac);
705 pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
706 walk_list!(visitor, visit_field_def, struct_definition.fields());
709 pub fn walk_field_def<'a, V: Visitor<'a>>(visitor: &mut V, field: &'a FieldDef) {
710 visitor.visit_vis(&field.vis);
711 if let Some(ident) = field.ident {
712 visitor.visit_ident(ident);
714 visitor.visit_ty(&field.ty);
715 walk_list!(visitor, visit_attribute, &field.attrs);
718 pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
719 walk_list!(visitor, visit_stmt, &block.stmts);
722 pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
723 match statement.kind {
724 StmtKind::Local(ref local) => visitor.visit_local(local),
725 StmtKind::Item(ref item) => visitor.visit_item(item),
726 StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => visitor.visit_expr(expr),
727 StmtKind::Empty => {}
728 StmtKind::MacCall(ref mac) => {
729 let MacCallStmt { ref mac, style: _, ref attrs, tokens: _ } = **mac;
730 visitor.visit_mac_call(mac);
731 for attr in attrs.iter() {
732 visitor.visit_attribute(attr);
738 pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a MacCall) {
739 visitor.visit_path(&mac.path, DUMMY_NODE_ID);
742 pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
743 visitor.visit_expr(&constant.value);
746 pub fn walk_inline_asm<'a, V: Visitor<'a>>(visitor: &mut V, asm: &'a InlineAsm) {
747 for (op, _) in &asm.operands {
749 InlineAsmOperand::In { expr, .. }
750 | InlineAsmOperand::Out { expr: Some(expr), .. }
751 | InlineAsmOperand::InOut { expr, .. } => visitor.visit_expr(expr),
752 InlineAsmOperand::Out { expr: None, .. } => {}
753 InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
754 visitor.visit_expr(in_expr);
755 if let Some(out_expr) = out_expr {
756 visitor.visit_expr(out_expr);
759 InlineAsmOperand::Const { anon_const, .. } => visitor.visit_anon_const(anon_const),
760 InlineAsmOperand::Sym { sym } => visitor.visit_inline_asm_sym(sym),
765 pub fn walk_inline_asm_sym<'a, V: Visitor<'a>>(visitor: &mut V, sym: &'a InlineAsmSym) {
766 if let Some(ref qself) = sym.qself {
767 visitor.visit_ty(&qself.ty);
769 visitor.visit_path(&sym.path, sym.id);
772 pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
773 walk_list!(visitor, visit_attribute, expression.attrs.iter());
775 match expression.kind {
776 ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
777 ExprKind::Array(ref subexpressions) => {
778 walk_list!(visitor, visit_expr, subexpressions);
780 ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
781 ExprKind::Repeat(ref element, ref count) => {
782 visitor.visit_expr(element);
783 visitor.visit_anon_const(count)
785 ExprKind::Struct(ref se) => {
786 if let Some(ref qself) = se.qself {
787 visitor.visit_ty(&qself.ty);
789 visitor.visit_path(&se.path, expression.id);
790 walk_list!(visitor, visit_expr_field, &se.fields);
792 StructRest::Base(expr) => visitor.visit_expr(expr),
793 StructRest::Rest(_span) => {}
794 StructRest::None => {}
797 ExprKind::Tup(ref subexpressions) => {
798 walk_list!(visitor, visit_expr, subexpressions);
800 ExprKind::Call(ref callee_expression, ref arguments) => {
801 visitor.visit_expr(callee_expression);
802 walk_list!(visitor, visit_expr, arguments);
804 ExprKind::MethodCall(ref segment, ref arguments, _span) => {
805 visitor.visit_path_segment(expression.span, segment);
806 walk_list!(visitor, visit_expr, arguments);
808 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
809 visitor.visit_expr(left_expression);
810 visitor.visit_expr(right_expression)
812 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
813 visitor.visit_expr(subexpression)
815 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
816 visitor.visit_expr(subexpression);
817 visitor.visit_ty(typ)
819 ExprKind::Let(ref pat, ref expr, _) => {
820 visitor.visit_pat(pat);
821 visitor.visit_expr(expr);
823 ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
824 visitor.visit_expr(head_expression);
825 visitor.visit_block(if_block);
826 walk_list!(visitor, visit_expr, optional_else);
828 ExprKind::While(ref subexpression, ref block, ref opt_label) => {
829 walk_list!(visitor, visit_label, opt_label);
830 visitor.visit_expr(subexpression);
831 visitor.visit_block(block);
833 ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
834 walk_list!(visitor, visit_label, opt_label);
835 visitor.visit_pat(pattern);
836 visitor.visit_expr(subexpression);
837 visitor.visit_block(block);
839 ExprKind::Loop(ref block, ref opt_label) => {
840 walk_list!(visitor, visit_label, opt_label);
841 visitor.visit_block(block);
843 ExprKind::Match(ref subexpression, ref arms) => {
844 visitor.visit_expr(subexpression);
845 walk_list!(visitor, visit_arm, arms);
847 ExprKind::Closure(_, _, _, ref decl, ref body, _decl_span) => {
848 visitor.visit_fn(FnKind::Closure(decl, body), expression.span, expression.id)
850 ExprKind::Block(ref block, ref opt_label) => {
851 walk_list!(visitor, visit_label, opt_label);
852 visitor.visit_block(block);
854 ExprKind::Async(_, _, ref body) => {
855 visitor.visit_block(body);
857 ExprKind::Await(ref expr) => visitor.visit_expr(expr),
858 ExprKind::Assign(ref lhs, ref rhs, _) => {
859 visitor.visit_expr(lhs);
860 visitor.visit_expr(rhs);
862 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
863 visitor.visit_expr(left_expression);
864 visitor.visit_expr(right_expression);
866 ExprKind::Field(ref subexpression, ident) => {
867 visitor.visit_expr(subexpression);
868 visitor.visit_ident(ident);
870 ExprKind::Index(ref main_expression, ref index_expression) => {
871 visitor.visit_expr(main_expression);
872 visitor.visit_expr(index_expression)
874 ExprKind::Range(ref start, ref end, _) => {
875 walk_list!(visitor, visit_expr, start);
876 walk_list!(visitor, visit_expr, end);
878 ExprKind::Underscore => {}
879 ExprKind::Path(ref maybe_qself, ref path) => {
880 if let Some(ref qself) = *maybe_qself {
881 visitor.visit_ty(&qself.ty);
883 visitor.visit_path(path, expression.id)
885 ExprKind::Break(ref opt_label, ref opt_expr) => {
886 walk_list!(visitor, visit_label, opt_label);
887 walk_list!(visitor, visit_expr, opt_expr);
889 ExprKind::Continue(ref opt_label) => {
890 walk_list!(visitor, visit_label, opt_label);
892 ExprKind::Ret(ref optional_expression) => {
893 walk_list!(visitor, visit_expr, optional_expression);
895 ExprKind::Yeet(ref optional_expression) => {
896 walk_list!(visitor, visit_expr, optional_expression);
898 ExprKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
899 ExprKind::Paren(ref subexpression) => visitor.visit_expr(subexpression),
900 ExprKind::InlineAsm(ref asm) => visitor.visit_inline_asm(asm),
901 ExprKind::Yield(ref optional_expression) => {
902 walk_list!(visitor, visit_expr, optional_expression);
904 ExprKind::Try(ref subexpression) => visitor.visit_expr(subexpression),
905 ExprKind::TryBlock(ref body) => visitor.visit_block(body),
906 ExprKind::Lit(_) | ExprKind::Err => {}
909 visitor.visit_expr_post(expression)
912 pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {
913 walk_list!(visitor, visit_attribute, param.attrs.iter());
914 visitor.visit_pat(¶m.pat);
915 visitor.visit_ty(¶m.ty);
918 pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
919 visitor.visit_pat(&arm.pat);
920 walk_list!(visitor, visit_expr, &arm.guard);
921 visitor.visit_expr(&arm.body);
922 walk_list!(visitor, visit_attribute, &arm.attrs);
925 pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
926 if let VisibilityKind::Restricted { ref path, id } = vis.kind {
927 visitor.visit_path(path, id);
931 pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
933 AttrKind::Normal(ref item, ref _tokens) => walk_mac_args(visitor, &item.args),
934 AttrKind::DocComment(..) => {}
938 pub fn walk_mac_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a MacArgs) {
941 MacArgs::Delimited(_dspan, _delim, _tokens) => {}
942 MacArgs::Eq(_eq_span, MacArgsEq::Ast(expr)) => visitor.visit_expr(expr),
943 MacArgs::Eq(_, MacArgsEq::Hir(lit)) => {
944 unreachable!("in literal form when walking mac args eq: {:?}", lit)