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