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