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