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