]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_ast/src/visit.rs
New upstream version 1.64.0+dfsg1
[rustc.git] / compiler / rustc_ast / src / visit.rs
CommitLineData
1a4d82fc
JJ
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.
5//!
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
9fa01778 9//! execute before AST node B, then A is visited first. The borrow checker in
1a4d82fc
JJ
10//! particular relies on this property.
11//!
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.
15
9fa01778 16use crate::ast::*;
9fa01778 17
064997fb 18use rustc_span::symbol::Ident;
dfeec247 19use rustc_span::Span;
1a4d82fc 20
94222f64 21#[derive(Copy, Clone, Debug, PartialEq)]
74b04a01
XL
22pub enum AssocCtxt {
23 Trait,
24 Impl,
25}
26
94222f64 27#[derive(Copy, Clone, Debug, PartialEq)]
74b04a01
XL
28pub enum FnCtxt {
29 Free,
30 Foreign,
31 Assoc(AssocCtxt),
32}
33
04454e1e
FG
34#[derive(Copy, Clone, Debug)]
35pub enum BoundKind {
36 /// Trait bounds in generics bounds and type/trait alias.
37 /// E.g., `<T: Bound>`, `type A: Bound`, or `where T: Bound`.
38 Bound,
39
40 /// Trait bounds in `impl` type.
41 /// E.g., `type Foo = impl Bound1 + Bound2 + Bound3`.
42 Impl,
43
44 /// Trait bounds in trait object type.
45 /// E.g., `dyn Bound1 + Bound2 + Bound3`.
46 TraitObject,
47
48 /// Super traits of a trait.
49 /// E.g., `trait A: B`
50 SuperTraits,
51}
52
94222f64 53#[derive(Copy, Clone, Debug)]
1a4d82fc 54pub enum FnKind<'a> {
74b04a01 55 /// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
04454e1e 56 Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, &'a Generics, Option<&'a Block>),
1a4d82fc 57
e1599b0c 58 /// E.g., `|x, y| body`.
064997fb 59 Closure(&'a ClosureBinder, &'a FnDecl, &'a Expr),
1a4d82fc
JJ
60}
61
48663c56
XL
62impl<'a> FnKind<'a> {
63 pub fn header(&self) -> Option<&'a FnHeader> {
64 match *self {
04454e1e 65 FnKind::Fn(_, _, sig, _, _, _) => Some(&sig.header),
064997fb 66 FnKind::Closure(_, _, _) => None,
74b04a01
XL
67 }
68 }
69
3dfed10e
XL
70 pub fn ident(&self) -> Option<&Ident> {
71 match self {
72 FnKind::Fn(_, ident, ..) => Some(ident),
73 _ => None,
74 }
75 }
76
74b04a01
XL
77 pub fn decl(&self) -> &'a FnDecl {
78 match self {
04454e1e 79 FnKind::Fn(_, _, sig, _, _, _) => &sig.decl,
064997fb 80 FnKind::Closure(_, decl, _) => decl,
74b04a01
XL
81 }
82 }
83
84 pub fn ctxt(&self) -> Option<FnCtxt> {
85 match self {
86 FnKind::Fn(ctxt, ..) => Some(*ctxt),
87 FnKind::Closure(..) => None,
48663c56
XL
88 }
89 }
90}
91
923072b8
FG
92#[derive(Copy, Clone, Debug)]
93pub enum LifetimeCtxt {
94 /// Appears in a reference type.
95 Rptr,
96 /// Appears as a bound on a type or another lifetime.
97 Bound,
98 /// Appears as a generic argument.
99 GenericArg,
100}
101
e1599b0c 102/// Each method of the `Visitor` trait is a hook to be potentially
9fa01778 103/// overridden. Each method's default implementation recursively visits
1a4d82fc 104/// the substructure of the input via the corresponding `walk` method;
6a06907d 105/// e.g., the `visit_item` method by default calls `visit::walk_item`.
1a4d82fc
JJ
106///
107/// If you want to ensure that your code handles every variant
9fa01778 108/// explicitly, you need to override each method. (And you also need
1a4d82fc
JJ
109/// to monitor future changes to `Visitor` in case a new method with a
110/// new default implementation gets introduced.)
476ff2be 111pub trait Visitor<'ast>: Sized {
064997fb 112 fn visit_ident(&mut self, _ident: Ident) {}
dfeec247
XL
113 fn visit_foreign_item(&mut self, i: &'ast ForeignItem) {
114 walk_foreign_item(self, i)
115 }
dfeec247
XL
116 fn visit_item(&mut self, i: &'ast Item) {
117 walk_item(self, i)
118 }
119 fn visit_local(&mut self, l: &'ast Local) {
120 walk_local(self, l)
121 }
122 fn visit_block(&mut self, b: &'ast Block) {
123 walk_block(self, b)
124 }
125 fn visit_stmt(&mut self, s: &'ast Stmt) {
126 walk_stmt(self, s)
127 }
128 fn visit_param(&mut self, param: &'ast Param) {
129 walk_param(self, param)
130 }
131 fn visit_arm(&mut self, a: &'ast Arm) {
132 walk_arm(self, a)
133 }
134 fn visit_pat(&mut self, p: &'ast Pat) {
135 walk_pat(self, p)
136 }
137 fn visit_anon_const(&mut self, c: &'ast AnonConst) {
138 walk_anon_const(self, c)
139 }
140 fn visit_expr(&mut self, ex: &'ast Expr) {
141 walk_expr(self, ex)
142 }
143 fn visit_expr_post(&mut self, _ex: &'ast Expr) {}
144 fn visit_ty(&mut self, t: &'ast Ty) {
145 walk_ty(self, t)
146 }
8faf50e0
XL
147 fn visit_generic_param(&mut self, param: &'ast GenericParam) {
148 walk_generic_param(self, param)
149 }
dfeec247
XL
150 fn visit_generics(&mut self, g: &'ast Generics) {
151 walk_generics(self, g)
152 }
064997fb
FG
153 fn visit_closure_binder(&mut self, b: &'ast ClosureBinder) {
154 walk_closure_binder(self, b)
155 }
8bb4bdeb
XL
156 fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
157 walk_where_predicate(self, p)
158 }
74b04a01
XL
159 fn visit_fn(&mut self, fk: FnKind<'ast>, s: Span, _: NodeId) {
160 walk_fn(self, fk, s)
476ff2be 161 }
74b04a01
XL
162 fn visit_assoc_item(&mut self, i: &'ast AssocItem, ctxt: AssocCtxt) {
163 walk_assoc_item(self, i, ctxt)
dfeec247
XL
164 }
165 fn visit_trait_ref(&mut self, t: &'ast TraitRef) {
166 walk_trait_ref(self, t)
167 }
04454e1e 168 fn visit_param_bound(&mut self, bounds: &'ast GenericBound, _ctxt: BoundKind) {
8faf50e0 169 walk_param_bound(self, bounds)
1a4d82fc 170 }
476ff2be 171 fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
1a4d82fc
JJ
172 walk_poly_trait_ref(self, t, m)
173 }
e1599b0c 174 fn visit_variant_data(&mut self, s: &'ast VariantData) {
1a4d82fc
JJ
175 walk_struct_def(self, s)
176 }
6a06907d
XL
177 fn visit_field_def(&mut self, s: &'ast FieldDef) {
178 walk_field_def(self, s)
dfeec247
XL
179 }
180 fn visit_enum_def(
181 &mut self,
182 enum_definition: &'ast EnumDef,
183 generics: &'ast Generics,
184 item_id: NodeId,
185 _: Span,
186 ) {
b039eaaf 187 walk_enum_def(self, enum_definition, generics, item_id)
c1a9b12d 188 }
e1599b0c
XL
189 fn visit_variant(&mut self, v: &'ast Variant) {
190 walk_variant(self, v)
1a4d82fc 191 }
2c00a5a8
XL
192 fn visit_label(&mut self, label: &'ast Label) {
193 walk_label(self, label)
194 }
923072b8 195 fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, _: LifetimeCtxt) {
b039eaaf 196 walk_lifetime(self, lifetime)
1a4d82fc 197 }
29967ef6
XL
198 fn visit_mac_call(&mut self, mac: &'ast MacCall) {
199 walk_mac(self, mac)
1a4d82fc 200 }
7cac9316
XL
201 fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
202 // Nothing to do
203 }
476ff2be 204 fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
1a4d82fc
JJ
205 walk_path(self, path)
206 }
ff7c6d11
XL
207 fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
208 walk_use_tree(self, use_tree, id)
b039eaaf 209 }
476ff2be 210 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
1a4d82fc
JJ
211 walk_path_segment(self, path_span, path_segment)
212 }
8faf50e0
XL
213 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'ast GenericArgs) {
214 walk_generic_args(self, path_span, generic_args)
215 }
216 fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
29967ef6 217 walk_generic_arg(self, generic_arg)
1a4d82fc 218 }
5099ac24
FG
219 fn visit_assoc_constraint(&mut self, constraint: &'ast AssocConstraint) {
220 walk_assoc_constraint(self, constraint)
1a4d82fc 221 }
abe05a73
XL
222 fn visit_attribute(&mut self, attr: &'ast Attribute) {
223 walk_attribute(self, attr)
224 }
476ff2be 225 fn visit_vis(&mut self, vis: &'ast Visibility) {
54a0048b
SL
226 walk_vis(self, vis)
227 }
74b04a01 228 fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FnRetTy) {
5bcae85e
SL
229 walk_fn_ret_ty(self, ret_ty)
230 }
532ac7d7
XL
231 fn visit_fn_header(&mut self, _header: &'ast FnHeader) {
232 // Nothing to do
233 }
6a06907d
XL
234 fn visit_expr_field(&mut self, f: &'ast ExprField) {
235 walk_expr_field(self, f)
e1599b0c 236 }
6a06907d
XL
237 fn visit_pat_field(&mut self, fp: &'ast PatField) {
238 walk_pat_field(self, fp)
e1599b0c 239 }
a2a8927a
XL
240 fn visit_crate(&mut self, krate: &'ast Crate) {
241 walk_crate(self, krate)
242 }
04454e1e
FG
243 fn visit_inline_asm(&mut self, asm: &'ast InlineAsm) {
244 walk_inline_asm(self, asm)
245 }
246 fn visit_inline_asm_sym(&mut self, sym: &'ast InlineAsmSym) {
247 walk_inline_asm_sym(self, sym)
248 }
223e47cc
LB
249}
250
b039eaaf
SL
251#[macro_export]
252macro_rules! walk_list {
253 ($visitor: expr, $method: ident, $list: expr) => {
254 for elem in $list {
255 $visitor.$method(elem)
256 }
257 };
258 ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
259 for elem in $list {
260 $visitor.$method(elem, $($extra_args,)*)
261 }
1a4d82fc
JJ
262 }
263}
264
476ff2be 265pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
6a06907d 266 walk_list!(visitor, visit_item, &krate.items);
b039eaaf 267 walk_list!(visitor, visit_attribute, &krate.attrs);
1a4d82fc
JJ
268}
269
476ff2be 270pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
3157f602
XL
271 for attr in local.attrs.iter() {
272 visitor.visit_attribute(attr);
273 }
b039eaaf
SL
274 visitor.visit_pat(&local.pat);
275 walk_list!(visitor, visit_ty, &local.ty);
94222f64
XL
276 if let Some((init, els)) = local.kind.init_else_opt() {
277 visitor.visit_expr(init);
278 walk_list!(visitor, visit_block, els);
279 }
b039eaaf
SL
280}
281
2c00a5a8 282pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, label: &'a Label) {
83c7162d 283 visitor.visit_ident(label.ident);
2c00a5a8
XL
284}
285
476ff2be 286pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
83c7162d 287 visitor.visit_ident(lifetime.ident);
b039eaaf
SL
288}
289
dfeec247
XL
290pub fn walk_poly_trait_ref<'a, V>(
291 visitor: &mut V,
292 trait_ref: &'a PolyTraitRef,
293 _: &TraitBoundModifier,
294) where
295 V: Visitor<'a>,
1a4d82fc 296{
ff7c6d11 297 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
1a4d82fc
JJ
298 visitor.visit_trait_ref(&trait_ref.trait_ref);
299}
300
476ff2be 301pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) {
1a4d82fc
JJ
302 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
303}
304
476ff2be 305pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
a7813a04 306 visitor.visit_vis(&item.vis);
83c7162d 307 visitor.visit_ident(item.ident);
e74abb32 308 match item.kind {
064997fb 309 ItemKind::ExternCrate(_) => {}
dfeec247 310 ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
74b04a01 311 ItemKind::Static(ref typ, _, ref expr) | ItemKind::Const(_, ref typ, ref expr) => {
b039eaaf 312 visitor.visit_ty(typ);
74b04a01 313 walk_list!(visitor, visit_expr, expr);
1a4d82fc 314 }
3c0e092e 315 ItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
04454e1e
FG
316 let kind =
317 FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, body.as_deref());
74b04a01 318 visitor.visit_fn(kind, item.span, item.id)
dfeec247 319 }
6a06907d
XL
320 ItemKind::Mod(_unsafety, ref mod_kind) => match mod_kind {
321 ModKind::Loaded(items, _inline, _inner_span) => {
322 walk_list!(visitor, visit_item, items)
323 }
324 ModKind::Unloaded => {}
325 },
7453a54e 326 ItemKind::ForeignMod(ref foreign_module) => {
b039eaaf 327 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
223e47cc 328 }
923072b8 329 ItemKind::GlobalAsm(ref asm) => visitor.visit_inline_asm(asm),
5e7ed085 330 ItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. }) => {
74b04a01 331 visitor.visit_generics(generics);
04454e1e 332 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
74b04a01 333 walk_list!(visitor, visit_ty, ty);
1a4d82fc 334 }
532ac7d7
XL
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)
1a4d82fc 338 }
3c0e092e 339 ItemKind::Impl(box Impl {
dfeec247 340 defaultness: _,
3c0e092e 341 unsafety: _,
dfeec247 342 ref generics,
3c0e092e
XL
343 constness: _,
344 polarity: _,
dfeec247
XL
345 ref of_trait,
346 ref self_ty,
347 ref items,
5869c6ff 348 }) => {
532ac7d7 349 visitor.visit_generics(generics);
dfeec247
XL
350 walk_list!(visitor, visit_trait_ref, of_trait);
351 visitor.visit_ty(self_ty);
74b04a01 352 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Impl);
970d7e83 353 }
dfeec247
XL
354 ItemKind::Struct(ref struct_definition, ref generics)
355 | ItemKind::Union(ref struct_definition, ref generics) => {
1a4d82fc 356 visitor.visit_generics(generics);
e1599b0c 357 visitor.visit_variant_data(struct_definition);
1a4d82fc 358 }
3c0e092e
XL
359 ItemKind::Trait(box Trait {
360 unsafety: _,
361 is_auto: _,
362 ref generics,
363 ref bounds,
364 ref items,
365 }) => {
1a4d82fc 366 visitor.visit_generics(generics);
04454e1e 367 walk_list!(visitor, visit_param_bound, bounds, BoundKind::SuperTraits);
74b04a01 368 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
1a4d82fc 369 }
ff7c6d11
XL
370 ItemKind::TraitAlias(ref generics, ref bounds) => {
371 visitor.visit_generics(generics);
04454e1e 372 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
ff7c6d11 373 }
29967ef6 374 ItemKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
7cac9316 375 ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
1a4d82fc 376 }
b039eaaf 377 walk_list!(visitor, visit_attribute, &item.attrs);
223e47cc
LB
378}
379
e1599b0c
XL
380pub fn walk_enum_def<'a, V: Visitor<'a>>(
381 visitor: &mut V,
382 enum_definition: &'a EnumDef,
383 _: &'a Generics,
384 _: NodeId,
385) {
386 walk_list!(visitor, visit_variant, &enum_definition.variants);
1a4d82fc 387}
223e47cc 388
e1599b0c 389pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant)
dfeec247
XL
390where
391 V: Visitor<'a>,
3157f602 392{
e1599b0c 393 visitor.visit_ident(variant.ident);
60c5eb7d 394 visitor.visit_vis(&variant.vis);
e1599b0c
XL
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);
398}
399
6a06907d 400pub fn walk_expr_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a ExprField) {
e1599b0c
XL
401 visitor.visit_expr(&f.expr);
402 visitor.visit_ident(f.ident);
403 walk_list!(visitor, visit_attribute, f.attrs.iter());
404}
405
6a06907d 406pub fn walk_pat_field<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a PatField) {
e1599b0c
XL
407 visitor.visit_ident(fp.ident);
408 visitor.visit_pat(&fp.pat);
409 walk_list!(visitor, visit_attribute, fp.attrs.iter());
1a4d82fc
JJ
410}
411
476ff2be 412pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
e74abb32 413 match typ.kind {
dfeec247
XL
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),
7453a54e 416 TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
923072b8 417 walk_list!(visitor, visit_lifetime, opt_lifetime, LifetimeCtxt::Rptr);
b039eaaf 418 visitor.visit_ty(&mutable_type.ty)
1a4d82fc 419 }
7453a54e 420 TyKind::Tup(ref tuple_element_types) => {
b039eaaf 421 walk_list!(visitor, visit_ty, tuple_element_types);
1a4d82fc 422 }
7453a54e 423 TyKind::BareFn(ref function_declaration) => {
ff7c6d11 424 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
94b46f34 425 walk_fn_decl(visitor, &function_declaration.decl);
1a4d82fc 426 }
7453a54e 427 TyKind::Path(ref maybe_qself, ref path) => {
c34b1796
AL
428 if let Some(ref qself) = *maybe_qself {
429 visitor.visit_ty(&qself.ty);
430 }
431 visitor.visit_path(path, typ.id);
1a4d82fc 432 }
94b46f34 433 TyKind::Array(ref ty, ref length) => {
b039eaaf 434 visitor.visit_ty(ty);
94b46f34 435 visitor.visit_anon_const(length)
1a4d82fc 436 }
04454e1e
FG
437 TyKind::TraitObject(ref bounds, ..) => {
438 walk_list!(visitor, visit_param_bound, bounds, BoundKind::TraitObject);
439 }
440 TyKind::ImplTrait(_, ref bounds) => {
441 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Impl);
5bcae85e 442 }
dfeec247 443 TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
cc61c64b 444 TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
29967ef6 445 TyKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
dfeec247 446 TyKind::Never | TyKind::CVarArgs => {}
1a4d82fc
JJ
447 }
448}
449
476ff2be 450pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
85aaf69f 451 for segment in &path.segments {
1a4d82fc
JJ
452 visitor.visit_path_segment(path.span, segment);
453 }
454}
455
dfeec247 456pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree, id: NodeId) {
ff7c6d11 457 visitor.visit_path(&use_tree.prefix, id);
ff7c6d11 458 match use_tree.kind {
94b46f34 459 UseTreeKind::Simple(rename, ..) => {
e1599b0c 460 // The extra IDs are handled during HIR lowering.
0531ce1d 461 if let Some(rename) = rename {
83c7162d 462 visitor.visit_ident(rename);
0531ce1d 463 }
ff7c6d11 464 }
dfeec247 465 UseTreeKind::Glob => {}
ff7c6d11
XL
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);
469 }
470 }
471 }
b039eaaf
SL
472}
473
dfeec247
XL
474pub fn walk_path_segment<'a, V: Visitor<'a>>(
475 visitor: &mut V,
476 path_span: Span,
477 segment: &'a PathSegment,
478) {
83c7162d 479 visitor.visit_ident(segment.ident);
8faf50e0
XL
480 if let Some(ref args) = segment.args {
481 visitor.visit_generic_args(path_span, args);
32a655c1 482 }
1a4d82fc
JJ
483}
484
dfeec247
XL
485pub fn walk_generic_args<'a, V>(visitor: &mut V, _path_span: Span, generic_args: &'a GenericArgs)
486where
487 V: Visitor<'a>,
3157f602 488{
8faf50e0
XL
489 match *generic_args {
490 GenericArgs::AngleBracketed(ref data) => {
ba9703b0
XL
491 for arg in &data.args {
492 match arg {
493 AngleBracketedArg::Arg(a) => visitor.visit_generic_arg(a),
5099ac24 494 AngleBracketedArg::Constraint(c) => visitor.visit_assoc_constraint(c),
ba9703b0
XL
495 }
496 }
1a4d82fc 497 }
8faf50e0 498 GenericArgs::Parenthesized(ref data) => {
b039eaaf 499 walk_list!(visitor, visit_ty, &data.inputs);
dfeec247 500 walk_fn_ret_ty(visitor, &data.output);
1a4d82fc
JJ
501 }
502 }
503}
504
29967ef6
XL
505pub fn walk_generic_arg<'a, V>(visitor: &mut V, generic_arg: &'a GenericArg)
506where
507 V: Visitor<'a>,
508{
509 match generic_arg {
923072b8 510 GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt, LifetimeCtxt::GenericArg),
29967ef6
XL
511 GenericArg::Type(ty) => visitor.visit_ty(ty),
512 GenericArg::Const(ct) => visitor.visit_anon_const(ct),
513 }
514}
515
5099ac24 516pub fn walk_assoc_constraint<'a, V: Visitor<'a>>(visitor: &mut V, constraint: &'a AssocConstraint) {
dc9dc135 517 visitor.visit_ident(constraint.ident);
fc512014
XL
518 if let Some(ref gen_args) = constraint.gen_args {
519 visitor.visit_generic_args(gen_args.span(), gen_args);
520 }
dc9dc135 521 match constraint.kind {
5099ac24
FG
522 AssocConstraintKind::Equality { ref term } => match term {
523 Term::Ty(ty) => visitor.visit_ty(ty),
524 Term::Const(c) => visitor.visit_anon_const(c),
525 },
526 AssocConstraintKind::Bound { ref bounds } => {
04454e1e 527 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
dc9dc135
XL
528 }
529 }
1a4d82fc
JJ
530}
531
476ff2be 532pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
e74abb32 533 match pattern.kind {
17df50a5
XL
534 PatKind::TupleStruct(ref opt_qself, ref path, ref elems) => {
535 if let Some(ref qself) = *opt_qself {
536 visitor.visit_ty(&qself.ty);
537 }
7453a54e 538 visitor.visit_path(path, pattern.id);
416331ca 539 walk_list!(visitor, visit_pat, elems);
7453a54e 540 }
3157f602
XL
541 PatKind::Path(ref opt_qself, ref path) => {
542 if let Some(ref qself) = *opt_qself {
543 visitor.visit_ty(&qself.ty);
544 }
d9579d0f
AL
545 visitor.visit_path(path, pattern.id)
546 }
17df50a5
XL
547 PatKind::Struct(ref opt_qself, ref path, ref fields, _) => {
548 if let Some(ref qself) = *opt_qself {
549 visitor.visit_ty(&qself.ty);
550 }
1a4d82fc 551 visitor.visit_path(path, pattern.id);
6a06907d 552 walk_list!(visitor, visit_pat_field, fields);
1a4d82fc 553 }
dfeec247
XL
554 PatKind::Box(ref subpattern)
555 | PatKind::Ref(ref subpattern, _)
556 | PatKind::Paren(ref subpattern) => visitor.visit_pat(subpattern),
83c7162d
XL
557 PatKind::Ident(_, ident, ref optional_subpattern) => {
558 visitor.visit_ident(ident);
b039eaaf 559 walk_list!(visitor, visit_pat, optional_subpattern);
223e47cc 560 }
7453a54e 561 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
32a655c1 562 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
dfeec247
XL
563 walk_list!(visitor, visit_expr, lower_bound);
564 walk_list!(visitor, visit_expr, upper_bound);
223e47cc 565 }
dfeec247
XL
566 PatKind::Wild | PatKind::Rest => {}
567 PatKind::Tuple(ref elems) | PatKind::Slice(ref elems) | PatKind::Or(ref elems) => {
416331ca 568 walk_list!(visitor, visit_pat, elems);
223e47cc 569 }
29967ef6 570 PatKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
1a4d82fc
JJ
571 }
572}
573
74b04a01
XL
574pub 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);
579 match kind {
580 ForeignItemKind::Static(ty, _, expr) => {
581 visitor.visit_ty(ty);
582 walk_list!(visitor, visit_expr, expr);
583 }
3c0e092e 584 ForeignItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
04454e1e 585 let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body.as_deref());
74b04a01
XL
586 visitor.visit_fn(kind, span, id);
587 }
5e7ed085 588 ForeignItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
74b04a01 589 visitor.visit_generics(generics);
04454e1e 590 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
74b04a01
XL
591 walk_list!(visitor, visit_ty, ty);
592 }
ba9703b0 593 ForeignItemKind::MacCall(mac) => {
29967ef6 594 visitor.visit_mac_call(mac);
1a4d82fc 595 }
223e47cc 596 }
1a4d82fc
JJ
597}
598
8faf50e0 599pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
1a4d82fc 600 match *bound {
8faf50e0 601 GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
923072b8
FG
602 GenericBound::Outlives(ref lifetime) => {
603 visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound)
604 }
223e47cc
LB
605 }
606}
607
ff7c6d11 608pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
8faf50e0
XL
609 visitor.visit_ident(param.ident);
610 walk_list!(visitor, visit_attribute, param.attrs.iter());
04454e1e 611 walk_list!(visitor, visit_param_bound, &param.bounds, BoundKind::Bound);
8faf50e0 612 match param.kind {
dc9dc135 613 GenericParamKind::Lifetime => (),
8faf50e0 614 GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
5869c6ff
XL
615 GenericParamKind::Const { ref ty, ref default, .. } => {
616 visitor.visit_ty(ty);
617 if let Some(default) = default {
618 visitor.visit_anon_const(default);
619 }
620 }
1a4d82fc 621 }
ff7c6d11
XL
622}
623
624pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
625 walk_list!(visitor, visit_generic_param, &generics.params);
8bb4bdeb
XL
626 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
627}
628
064997fb
FG
629pub fn walk_closure_binder<'a, V: Visitor<'a>>(visitor: &mut V, binder: &'a ClosureBinder) {
630 match binder {
631 ClosureBinder::NotPresent => {}
632 ClosureBinder::For { generic_params, span: _ } => {
633 walk_list!(visitor, visit_generic_param, generic_params)
634 }
635 }
636}
637
8bb4bdeb
XL
638pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
639 match *predicate {
dfeec247
XL
640 WherePredicate::BoundPredicate(WhereBoundPredicate {
641 ref bounded_ty,
642 ref bounds,
643 ref bound_generic_params,
644 ..
645 }) => {
8bb4bdeb 646 visitor.visit_ty(bounded_ty);
04454e1e 647 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
ff7c6d11 648 walk_list!(visitor, visit_generic_param, bound_generic_params);
8bb4bdeb 649 }
dfeec247
XL
650 WherePredicate::RegionPredicate(WhereRegionPredicate {
651 ref lifetime, ref bounds, ..
652 }) => {
923072b8 653 visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound);
04454e1e 654 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
8bb4bdeb 655 }
dfeec247 656 WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }) => {
8bb4bdeb
XL
657 visitor.visit_ty(lhs_ty);
658 visitor.visit_ty(rhs_ty);
223e47cc
LB
659 }
660 }
661}
662
74b04a01
XL
663pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy) {
664 if let FnRetTy::Ty(ref output_ty) = *ret_ty {
b039eaaf 665 visitor.visit_ty(output_ty)
223e47cc
LB
666 }
667}
668
476ff2be 669pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
e1599b0c
XL
670 for param in &function_declaration.inputs {
671 visitor.visit_param(param);
223e47cc 672 }
416331ca 673 visitor.visit_fn_ret_ty(&function_declaration.output);
223e47cc
LB
674}
675
74b04a01 676pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Span) {
476ff2be 677 match kind {
04454e1e
FG
678 FnKind::Fn(_, _, sig, _, generics, body) => {
679 visitor.visit_generics(generics);
532ac7d7 680 visitor.visit_fn_header(&sig.header);
74b04a01
XL
681 walk_fn_decl(visitor, &sig.decl);
682 walk_list!(visitor, visit_block, body);
476ff2be 683 }
064997fb
FG
684 FnKind::Closure(binder, decl, body) => {
685 visitor.visit_closure_binder(binder);
74b04a01 686 walk_fn_decl(visitor, decl);
476ff2be 687 visitor.visit_expr(body);
1a4d82fc 688 }
1a4d82fc 689 }
b039eaaf 690}
1a4d82fc 691
74b04a01
XL
692pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, ctxt: AssocCtxt) {
693 let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = *item;
694 visitor.visit_vis(vis);
695 visitor.visit_ident(ident);
696 walk_list!(visitor, visit_attribute, attrs);
697 match kind {
698 AssocItemKind::Const(_, ty, expr) => {
d9579d0f 699 visitor.visit_ty(ty);
dfeec247 700 walk_list!(visitor, visit_expr, expr);
d9579d0f 701 }
3c0e092e 702 AssocItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
04454e1e 703 let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body.as_deref());
74b04a01 704 visitor.visit_fn(kind, span, id);
c34b1796 705 }
5e7ed085 706 AssocItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
74b04a01 707 visitor.visit_generics(generics);
04454e1e 708 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
dfeec247 709 walk_list!(visitor, visit_ty, ty);
d9579d0f 710 }
ba9703b0 711 AssocItemKind::MacCall(mac) => {
29967ef6 712 visitor.visit_mac_call(mac);
1a4d82fc 713 }
223e47cc
LB
714 }
715}
716
476ff2be 717pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
6a06907d 718 walk_list!(visitor, visit_field_def, struct_definition.fields());
223e47cc
LB
719}
720
6a06907d
XL
721pub fn walk_field_def<'a, V: Visitor<'a>>(visitor: &mut V, field: &'a FieldDef) {
722 visitor.visit_vis(&field.vis);
723 if let Some(ident) = field.ident {
83c7162d 724 visitor.visit_ident(ident);
2c00a5a8 725 }
6a06907d
XL
726 visitor.visit_ty(&field.ty);
727 walk_list!(visitor, visit_attribute, &field.attrs);
223e47cc
LB
728}
729
476ff2be 730pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
b039eaaf 731 walk_list!(visitor, visit_stmt, &block.stmts);
223e47cc
LB
732}
733
476ff2be 734pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
e74abb32 735 match statement.kind {
3157f602
XL
736 StmtKind::Local(ref local) => visitor.visit_local(local),
737 StmtKind::Item(ref item) => visitor.visit_item(item),
74b04a01
XL
738 StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => visitor.visit_expr(expr),
739 StmtKind::Empty => {}
ba9703b0 740 StmtKind::MacCall(ref mac) => {
fc512014 741 let MacCallStmt { ref mac, style: _, ref attrs, tokens: _ } = **mac;
29967ef6 742 visitor.visit_mac_call(mac);
3157f602 743 for attr in attrs.iter() {
92a42be0
SL
744 visitor.visit_attribute(attr);
745 }
746 }
223e47cc
LB
747 }
748}
749
ba9703b0 750pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a MacCall) {
e1599b0c 751 visitor.visit_path(&mac.path, DUMMY_NODE_ID);
223e47cc
LB
752}
753
94b46f34
XL
754pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
755 visitor.visit_expr(&constant.value);
756}
757
04454e1e 758pub fn walk_inline_asm<'a, V: Visitor<'a>>(visitor: &mut V, asm: &'a InlineAsm) {
17df50a5
XL
759 for (op, _) in &asm.operands {
760 match op {
761 InlineAsmOperand::In { expr, .. }
94222f64 762 | InlineAsmOperand::Out { expr: Some(expr), .. }
04454e1e 763 | InlineAsmOperand::InOut { expr, .. } => visitor.visit_expr(expr),
94222f64 764 InlineAsmOperand::Out { expr: None, .. } => {}
17df50a5
XL
765 InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
766 visitor.visit_expr(in_expr);
767 if let Some(out_expr) = out_expr {
768 visitor.visit_expr(out_expr);
769 }
770 }
771 InlineAsmOperand::Const { anon_const, .. } => visitor.visit_anon_const(anon_const),
04454e1e 772 InlineAsmOperand::Sym { sym } => visitor.visit_inline_asm_sym(sym),
17df50a5
XL
773 }
774 }
775}
776
04454e1e
FG
777pub fn walk_inline_asm_sym<'a, V: Visitor<'a>>(visitor: &mut V, sym: &'a InlineAsmSym) {
778 if let Some(ref qself) = sym.qself {
779 visitor.visit_ty(&qself.ty);
780 }
781 visitor.visit_path(&sym.path, sym.id);
782}
783
476ff2be 784pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
e1599b0c
XL
785 walk_list!(visitor, visit_attribute, expression.attrs.iter());
786
e74abb32 787 match expression.kind {
dfeec247 788 ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
32a655c1 789 ExprKind::Array(ref subexpressions) => {
b039eaaf 790 walk_list!(visitor, visit_expr, subexpressions);
1a4d82fc 791 }
29967ef6 792 ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
7453a54e 793 ExprKind::Repeat(ref element, ref count) => {
b039eaaf 794 visitor.visit_expr(element);
94b46f34 795 visitor.visit_anon_const(count)
1a4d82fc 796 }
6a06907d 797 ExprKind::Struct(ref se) => {
17df50a5
XL
798 if let Some(ref qself) = se.qself {
799 visitor.visit_ty(&qself.ty);
800 }
6a06907d
XL
801 visitor.visit_path(&se.path, expression.id);
802 walk_list!(visitor, visit_expr_field, &se.fields);
803 match &se.rest {
29967ef6
XL
804 StructRest::Base(expr) => visitor.visit_expr(expr),
805 StructRest::Rest(_span) => {}
806 StructRest::None => {}
807 }
223e47cc 808 }
7453a54e 809 ExprKind::Tup(ref subexpressions) => {
b039eaaf 810 walk_list!(visitor, visit_expr, subexpressions);
223e47cc 811 }
7453a54e 812 ExprKind::Call(ref callee_expression, ref arguments) => {
9e0c209e 813 visitor.visit_expr(callee_expression);
b039eaaf 814 walk_list!(visitor, visit_expr, arguments);
1a4d82fc 815 }
f035d41b 816 ExprKind::MethodCall(ref segment, ref arguments, _span) => {
041b39d2 817 visitor.visit_path_segment(expression.span, segment);
9e0c209e 818 walk_list!(visitor, visit_expr, arguments);
223e47cc 819 }
7453a54e 820 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
b039eaaf
SL
821 visitor.visit_expr(left_expression);
822 visitor.visit_expr(right_expression)
1a4d82fc 823 }
60c5eb7d 824 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
b039eaaf 825 visitor.visit_expr(subexpression)
1a4d82fc 826 }
7453a54e 827 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
b039eaaf
SL
828 visitor.visit_expr(subexpression);
829 visitor.visit_ty(typ)
1a4d82fc 830 }
94222f64 831 ExprKind::Let(ref pat, ref expr, _) => {
e1599b0c 832 visitor.visit_pat(pat);
94222f64 833 visitor.visit_expr(expr);
dc9dc135 834 }
7453a54e 835 ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
b039eaaf
SL
836 visitor.visit_expr(head_expression);
837 visitor.visit_block(if_block);
838 walk_list!(visitor, visit_expr, optional_else);
1a4d82fc 839 }
2c00a5a8
XL
840 ExprKind::While(ref subexpression, ref block, ref opt_label) => {
841 walk_list!(visitor, visit_label, opt_label);
b039eaaf
SL
842 visitor.visit_expr(subexpression);
843 visitor.visit_block(block);
1a4d82fc 844 }
2c00a5a8
XL
845 ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
846 walk_list!(visitor, visit_label, opt_label);
b039eaaf
SL
847 visitor.visit_pat(pattern);
848 visitor.visit_expr(subexpression);
849 visitor.visit_block(block);
b039eaaf 850 }
2c00a5a8
XL
851 ExprKind::Loop(ref block, ref opt_label) => {
852 walk_list!(visitor, visit_label, opt_label);
b039eaaf 853 visitor.visit_block(block);
b039eaaf 854 }
7453a54e 855 ExprKind::Match(ref subexpression, ref arms) => {
b039eaaf
SL
856 visitor.visit_expr(subexpression);
857 walk_list!(visitor, visit_arm, arms);
223e47cc 858 }
064997fb
FG
859 ExprKind::Closure(ref binder, _, _, _, ref decl, ref body, _decl_span) => {
860 visitor.visit_fn(FnKind::Closure(binder, decl, body), expression.span, expression.id)
74b04a01 861 }
94b46f34
XL
862 ExprKind::Block(ref block, ref opt_label) => {
863 walk_list!(visitor, visit_label, opt_label);
864 visitor.visit_block(block);
865 }
8faf50e0
XL
866 ExprKind::Async(_, _, ref body) => {
867 visitor.visit_block(body);
868 }
416331ca 869 ExprKind::Await(ref expr) => visitor.visit_expr(expr),
dfeec247
XL
870 ExprKind::Assign(ref lhs, ref rhs, _) => {
871 visitor.visit_expr(lhs);
872 visitor.visit_expr(rhs);
1a4d82fc 873 }
7453a54e 874 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
9e0c209e 875 visitor.visit_expr(left_expression);
b039eaaf 876 visitor.visit_expr(right_expression);
223e47cc 877 }
83c7162d 878 ExprKind::Field(ref subexpression, ident) => {
b039eaaf 879 visitor.visit_expr(subexpression);
83c7162d 880 visitor.visit_ident(ident);
223e47cc 881 }
7453a54e 882 ExprKind::Index(ref main_expression, ref index_expression) => {
b039eaaf
SL
883 visitor.visit_expr(main_expression);
884 visitor.visit_expr(index_expression)
223e47cc 885 }
54a0048b 886 ExprKind::Range(ref start, ref end, _) => {
b039eaaf
SL
887 walk_list!(visitor, visit_expr, start);
888 walk_list!(visitor, visit_expr, end);
223e47cc 889 }
fc512014 890 ExprKind::Underscore => {}
7453a54e 891 ExprKind::Path(ref maybe_qself, ref path) => {
c34b1796
AL
892 if let Some(ref qself) = *maybe_qself {
893 visitor.visit_ty(&qself.ty);
894 }
1a4d82fc 895 visitor.visit_path(path, expression.id)
223e47cc 896 }
2c00a5a8
XL
897 ExprKind::Break(ref opt_label, ref opt_expr) => {
898 walk_list!(visitor, visit_label, opt_label);
476ff2be
SL
899 walk_list!(visitor, visit_expr, opt_expr);
900 }
2c00a5a8
XL
901 ExprKind::Continue(ref opt_label) => {
902 walk_list!(visitor, visit_label, opt_label);
b039eaaf 903 }
7453a54e 904 ExprKind::Ret(ref optional_expression) => {
b039eaaf 905 walk_list!(visitor, visit_expr, optional_expression);
1a4d82fc 906 }
04454e1e
FG
907 ExprKind::Yeet(ref optional_expression) => {
908 walk_list!(visitor, visit_expr, optional_expression);
909 }
29967ef6 910 ExprKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
dfeec247 911 ExprKind::Paren(ref subexpression) => visitor.visit_expr(subexpression),
923072b8 912 ExprKind::InlineAsm(ref asm) => visitor.visit_inline_asm(asm),
ea8adc8c
XL
913 ExprKind::Yield(ref optional_expression) => {
914 walk_list!(visitor, visit_expr, optional_expression);
915 }
dfeec247
XL
916 ExprKind::Try(ref subexpression) => visitor.visit_expr(subexpression),
917 ExprKind::TryBlock(ref body) => visitor.visit_block(body),
dc9dc135 918 ExprKind::Lit(_) | ExprKind::Err => {}
1a4d82fc
JJ
919 }
920
921 visitor.visit_expr_post(expression)
922}
923
e1599b0c
XL
924pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {
925 walk_list!(visitor, visit_attribute, param.attrs.iter());
926 visitor.visit_pat(&param.pat);
927 visitor.visit_ty(&param.ty);
416331ca
XL
928}
929
476ff2be 930pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
e1599b0c 931 visitor.visit_pat(&arm.pat);
e1599b0c 932 walk_list!(visitor, visit_expr, &arm.guard);
b039eaaf
SL
933 visitor.visit_expr(&arm.body);
934 walk_list!(visitor, visit_attribute, &arm.attrs);
223e47cc 935}
54a0048b 936
476ff2be 937pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
1b1a35ee 938 if let VisibilityKind::Restricted { ref path, id } = vis.kind {
a7813a04 939 visitor.visit_path(path, id);
54a0048b
SL
940 }
941}
abe05a73
XL
942
943pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
60c5eb7d 944 match attr.kind {
29967ef6 945 AttrKind::Normal(ref item, ref _tokens) => walk_mac_args(visitor, &item.args),
3dfed10e 946 AttrKind::DocComment(..) => {}
60c5eb7d
XL
947 }
948}
949
950pub fn walk_mac_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a MacArgs) {
951 match args {
952 MacArgs::Empty => {}
29967ef6 953 MacArgs::Delimited(_dspan, _delim, _tokens) => {}
04454e1e
FG
954 MacArgs::Eq(_eq_span, MacArgsEq::Ast(expr)) => visitor.visit_expr(expr),
955 MacArgs::Eq(_, MacArgsEq::Hir(lit)) => {
956 unreachable!("in literal form when walking mac args eq: {:?}", lit)
957 }
abe05a73
XL
958 }
959}