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