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.
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.
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.
18 use crate::tokenstream::TokenTree;
20 use rustc_span::symbol::{Ident, Symbol};
23 #[derive(Copy, Clone, PartialEq)]
29 #[derive(Copy, Clone, PartialEq)]
36 #[derive(Copy, Clone)]
38 /// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
39 Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, Option<&'a Block>),
41 /// E.g., `|x, y| body`.
42 Closure(&'a FnDecl, &'a Expr),
46 pub fn header(&self) -> Option<&'a FnHeader> {
48 FnKind::Fn(_, _, sig, _, _) => Some(&sig.header),
49 FnKind::Closure(_, _) => None,
53 pub fn ident(&self) -> Option<&Ident> {
55 FnKind::Fn(_, ident, ..) => Some(ident),
60 pub fn decl(&self) -> &'a FnDecl {
62 FnKind::Fn(_, _, sig, _, _) => &sig.decl,
63 FnKind::Closure(decl, _) => decl,
67 pub fn ctxt(&self) -> Option<FnCtxt> {
69 FnKind::Fn(ctxt, ..) => Some(*ctxt),
70 FnKind::Closure(..) => None,
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`.
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) {
88 fn visit_ident(&mut self, ident: Ident) {
89 walk_ident(self, ident);
91 fn visit_mod(&mut self, m: &'ast Mod, _s: Span, _attrs: &[Attribute], _n: NodeId) {
94 fn visit_foreign_item(&mut self, i: &'ast ForeignItem) {
95 walk_foreign_item(self, i)
97 fn visit_global_asm(&mut self, ga: &'ast GlobalAsm) {
98 walk_global_asm(self, ga)
100 fn visit_item(&mut self, i: &'ast Item) {
103 fn visit_local(&mut self, l: &'ast Local) {
106 fn visit_block(&mut self, b: &'ast Block) {
109 fn visit_stmt(&mut self, s: &'ast Stmt) {
112 fn visit_param(&mut self, param: &'ast Param) {
113 walk_param(self, param)
115 fn visit_arm(&mut self, a: &'ast Arm) {
118 fn visit_pat(&mut self, p: &'ast Pat) {
121 fn visit_anon_const(&mut self, c: &'ast AnonConst) {
122 walk_anon_const(self, c)
124 fn visit_expr(&mut self, ex: &'ast Expr) {
127 fn visit_expr_post(&mut self, _ex: &'ast Expr) {}
128 fn visit_ty(&mut self, t: &'ast Ty) {
131 fn visit_generic_param(&mut self, param: &'ast GenericParam) {
132 walk_generic_param(self, param)
134 fn visit_generics(&mut self, g: &'ast Generics) {
135 walk_generics(self, g)
137 fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
138 walk_where_predicate(self, p)
140 fn visit_fn(&mut self, fk: FnKind<'ast>, s: Span, _: NodeId) {
143 fn visit_assoc_item(&mut self, i: &'ast AssocItem, ctxt: AssocCtxt) {
144 walk_assoc_item(self, i, ctxt)
146 fn visit_trait_ref(&mut self, t: &'ast TraitRef) {
147 walk_trait_ref(self, t)
149 fn visit_param_bound(&mut self, bounds: &'ast GenericBound) {
150 walk_param_bound(self, bounds)
152 fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
153 walk_poly_trait_ref(self, t, m)
155 fn visit_variant_data(&mut self, s: &'ast VariantData) {
156 walk_struct_def(self, s)
158 fn visit_struct_field(&mut self, s: &'ast StructField) {
159 walk_struct_field(self, s)
163 enum_definition: &'ast EnumDef,
164 generics: &'ast Generics,
168 walk_enum_def(self, enum_definition, generics, item_id)
170 fn visit_variant(&mut self, v: &'ast Variant) {
171 walk_variant(self, v)
173 fn visit_label(&mut self, label: &'ast Label) {
174 walk_label(self, label)
176 fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
177 walk_lifetime(self, lifetime)
179 fn visit_mac_call(&mut self, mac: &'ast MacCall) {
182 fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
185 fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
186 walk_path(self, path)
188 fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
189 walk_use_tree(self, use_tree, id)
191 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
192 walk_path_segment(self, path_span, path_segment)
194 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'ast GenericArgs) {
195 walk_generic_args(self, path_span, generic_args)
197 fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
198 walk_generic_arg(self, generic_arg)
200 fn visit_assoc_ty_constraint(&mut self, constraint: &'ast AssocTyConstraint) {
201 walk_assoc_ty_constraint(self, constraint)
203 fn visit_attribute(&mut self, attr: &'ast Attribute) {
204 walk_attribute(self, attr)
206 fn visit_vis(&mut self, vis: &'ast Visibility) {
209 fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FnRetTy) {
210 walk_fn_ret_ty(self, ret_ty)
212 fn visit_fn_header(&mut self, _header: &'ast FnHeader) {
215 fn visit_field(&mut self, f: &'ast Field) {
218 fn visit_field_pattern(&mut self, fp: &'ast FieldPat) {
219 walk_field_pattern(self, fp)
224 macro_rules! walk_list {
225 ($visitor: expr, $method: ident, $list: expr) => {
227 $visitor.$method(elem)
230 ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
232 $visitor.$method(elem, $($extra_args,)*)
237 pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, ident: Ident) {
238 visitor.visit_name(ident.span, ident.name);
241 pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
242 visitor.visit_mod(&krate.module, krate.span, &krate.attrs, CRATE_NODE_ID);
243 walk_list!(visitor, visit_attribute, &krate.attrs);
246 pub fn walk_mod<'a, V: Visitor<'a>>(visitor: &mut V, module: &'a Mod) {
247 walk_list!(visitor, visit_item, &module.items);
250 pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
251 for attr in local.attrs.iter() {
252 visitor.visit_attribute(attr);
254 visitor.visit_pat(&local.pat);
255 walk_list!(visitor, visit_ty, &local.ty);
256 walk_list!(visitor, visit_expr, &local.init);
259 pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, label: &'a Label) {
260 visitor.visit_ident(label.ident);
263 pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
264 visitor.visit_ident(lifetime.ident);
267 pub fn walk_poly_trait_ref<'a, V>(
269 trait_ref: &'a PolyTraitRef,
270 _: &TraitBoundModifier,
274 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
275 visitor.visit_trait_ref(&trait_ref.trait_ref);
278 pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) {
279 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
282 pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
283 visitor.visit_vis(&item.vis);
284 visitor.visit_ident(item.ident);
286 ItemKind::ExternCrate(orig_name) => {
287 if let Some(orig_name) = orig_name {
288 visitor.visit_name(item.span, orig_name);
291 ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
292 ItemKind::Static(ref typ, _, ref expr) | ItemKind::Const(_, ref typ, ref expr) => {
293 visitor.visit_ty(typ);
294 walk_list!(visitor, visit_expr, expr);
296 ItemKind::Fn(_, ref sig, ref generics, ref body) => {
297 visitor.visit_generics(generics);
298 let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref());
299 visitor.visit_fn(kind, item.span, item.id)
301 ItemKind::Mod(ref module) => visitor.visit_mod(module, item.span, &item.attrs, item.id),
302 ItemKind::ForeignMod(ref foreign_module) => {
303 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
305 ItemKind::GlobalAsm(ref ga) => visitor.visit_global_asm(ga),
306 ItemKind::TyAlias(_, 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);
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)
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);
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);
335 ItemKind::Trait(.., ref generics, ref bounds, ref items) => {
336 visitor.visit_generics(generics);
337 walk_list!(visitor, visit_param_bound, bounds);
338 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
340 ItemKind::TraitAlias(ref generics, ref bounds) => {
341 visitor.visit_generics(generics);
342 walk_list!(visitor, visit_param_bound, bounds);
344 ItemKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
345 ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
347 walk_list!(visitor, visit_attribute, &item.attrs);
350 pub fn walk_enum_def<'a, V: Visitor<'a>>(
352 enum_definition: &'a EnumDef,
356 walk_list!(visitor, visit_variant, &enum_definition.variants);
359 pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant)
363 visitor.visit_ident(variant.ident);
364 visitor.visit_vis(&variant.vis);
365 visitor.visit_variant_data(&variant.data);
366 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
367 walk_list!(visitor, visit_attribute, &variant.attrs);
370 pub fn walk_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a Field) {
371 visitor.visit_expr(&f.expr);
372 visitor.visit_ident(f.ident);
373 walk_list!(visitor, visit_attribute, f.attrs.iter());
376 pub fn walk_field_pattern<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a FieldPat) {
377 visitor.visit_ident(fp.ident);
378 visitor.visit_pat(&fp.pat);
379 walk_list!(visitor, visit_attribute, fp.attrs.iter());
382 pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
384 TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => visitor.visit_ty(ty),
385 TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
386 TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
387 walk_list!(visitor, visit_lifetime, opt_lifetime);
388 visitor.visit_ty(&mutable_type.ty)
390 TyKind::Tup(ref tuple_element_types) => {
391 walk_list!(visitor, visit_ty, tuple_element_types);
393 TyKind::BareFn(ref function_declaration) => {
394 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
395 walk_fn_decl(visitor, &function_declaration.decl);
397 TyKind::Path(ref maybe_qself, ref path) => {
398 if let Some(ref qself) = *maybe_qself {
399 visitor.visit_ty(&qself.ty);
401 visitor.visit_path(path, typ.id);
403 TyKind::Array(ref ty, ref length) => {
404 visitor.visit_ty(ty);
405 visitor.visit_anon_const(length)
407 TyKind::TraitObject(ref bounds, ..) | TyKind::ImplTrait(_, ref bounds) => {
408 walk_list!(visitor, visit_param_bound, bounds);
410 TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
411 TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
412 TyKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
413 TyKind::Never | TyKind::CVarArgs => {}
417 pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
418 for segment in &path.segments {
419 visitor.visit_path_segment(path.span, segment);
423 pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree, id: NodeId) {
424 visitor.visit_path(&use_tree.prefix, id);
425 match use_tree.kind {
426 UseTreeKind::Simple(rename, ..) => {
427 // The extra IDs are handled during HIR lowering.
428 if let Some(rename) = rename {
429 visitor.visit_ident(rename);
432 UseTreeKind::Glob => {}
433 UseTreeKind::Nested(ref use_trees) => {
434 for &(ref nested_tree, nested_id) in use_trees {
435 visitor.visit_use_tree(nested_tree, nested_id, true);
441 pub fn walk_path_segment<'a, V: Visitor<'a>>(
444 segment: &'a PathSegment,
446 visitor.visit_ident(segment.ident);
447 if let Some(ref args) = segment.args {
448 visitor.visit_generic_args(path_span, args);
452 pub fn walk_generic_args<'a, V>(visitor: &mut V, _path_span: Span, generic_args: &'a GenericArgs)
456 match *generic_args {
457 GenericArgs::AngleBracketed(ref data) => {
458 for arg in &data.args {
460 AngleBracketedArg::Arg(a) => visitor.visit_generic_arg(a),
461 AngleBracketedArg::Constraint(c) => visitor.visit_assoc_ty_constraint(c),
465 GenericArgs::Parenthesized(ref data) => {
466 walk_list!(visitor, visit_ty, &data.inputs);
467 walk_fn_ret_ty(visitor, &data.output);
472 pub fn walk_generic_arg<'a, V>(visitor: &mut V, generic_arg: &'a GenericArg)
477 GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt),
478 GenericArg::Type(ty) => visitor.visit_ty(ty),
479 GenericArg::Const(ct) => visitor.visit_anon_const(ct),
483 pub fn walk_assoc_ty_constraint<'a, V: Visitor<'a>>(
485 constraint: &'a AssocTyConstraint,
487 visitor.visit_ident(constraint.ident);
488 match constraint.kind {
489 AssocTyConstraintKind::Equality { ref ty } => {
490 visitor.visit_ty(ty);
492 AssocTyConstraintKind::Bound { ref bounds } => {
493 walk_list!(visitor, visit_param_bound, bounds);
498 pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
500 PatKind::TupleStruct(ref path, ref elems) => {
501 visitor.visit_path(path, pattern.id);
502 walk_list!(visitor, visit_pat, elems);
504 PatKind::Path(ref opt_qself, ref path) => {
505 if let Some(ref qself) = *opt_qself {
506 visitor.visit_ty(&qself.ty);
508 visitor.visit_path(path, pattern.id)
510 PatKind::Struct(ref path, ref fields, _) => {
511 visitor.visit_path(path, pattern.id);
512 walk_list!(visitor, visit_field_pattern, fields);
514 PatKind::Box(ref subpattern)
515 | PatKind::Ref(ref subpattern, _)
516 | PatKind::Paren(ref subpattern) => visitor.visit_pat(subpattern),
517 PatKind::Ident(_, ident, ref optional_subpattern) => {
518 visitor.visit_ident(ident);
519 walk_list!(visitor, visit_pat, optional_subpattern);
521 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
522 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
523 walk_list!(visitor, visit_expr, lower_bound);
524 walk_list!(visitor, visit_expr, upper_bound);
526 PatKind::Wild | PatKind::Rest => {}
527 PatKind::Tuple(ref elems) | PatKind::Slice(ref elems) | PatKind::Or(ref elems) => {
528 walk_list!(visitor, visit_pat, elems);
530 PatKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
534 pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignItem) {
535 let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = *item;
536 visitor.visit_vis(vis);
537 visitor.visit_ident(ident);
538 walk_list!(visitor, visit_attribute, attrs);
540 ForeignItemKind::Static(ty, _, expr) => {
541 visitor.visit_ty(ty);
542 walk_list!(visitor, visit_expr, expr);
544 ForeignItemKind::Fn(_, sig, generics, body) => {
545 visitor.visit_generics(generics);
546 let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, body.as_deref());
547 visitor.visit_fn(kind, span, id);
549 ForeignItemKind::TyAlias(_, generics, bounds, ty) => {
550 visitor.visit_generics(generics);
551 walk_list!(visitor, visit_param_bound, bounds);
552 walk_list!(visitor, visit_ty, ty);
554 ForeignItemKind::MacCall(mac) => {
555 visitor.visit_mac_call(mac);
560 pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) {
564 pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
566 GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
567 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
571 pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
572 visitor.visit_ident(param.ident);
573 walk_list!(visitor, visit_attribute, param.attrs.iter());
574 walk_list!(visitor, visit_param_bound, ¶m.bounds);
576 GenericParamKind::Lifetime => (),
577 GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
578 GenericParamKind::Const { ref ty, .. } => visitor.visit_ty(ty),
582 pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
583 walk_list!(visitor, visit_generic_param, &generics.params);
584 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
587 pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
589 WherePredicate::BoundPredicate(WhereBoundPredicate {
592 ref bound_generic_params,
595 visitor.visit_ty(bounded_ty);
596 walk_list!(visitor, visit_param_bound, bounds);
597 walk_list!(visitor, visit_generic_param, bound_generic_params);
599 WherePredicate::RegionPredicate(WhereRegionPredicate {
600 ref lifetime, ref bounds, ..
602 visitor.visit_lifetime(lifetime);
603 walk_list!(visitor, visit_param_bound, bounds);
605 WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }) => {
606 visitor.visit_ty(lhs_ty);
607 visitor.visit_ty(rhs_ty);
612 pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy) {
613 if let FnRetTy::Ty(ref output_ty) = *ret_ty {
614 visitor.visit_ty(output_ty)
618 pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
619 for param in &function_declaration.inputs {
620 visitor.visit_param(param);
622 visitor.visit_fn_ret_ty(&function_declaration.output);
625 pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Span) {
627 FnKind::Fn(_, _, sig, _, body) => {
628 visitor.visit_fn_header(&sig.header);
629 walk_fn_decl(visitor, &sig.decl);
630 walk_list!(visitor, visit_block, body);
632 FnKind::Closure(decl, body) => {
633 walk_fn_decl(visitor, decl);
634 visitor.visit_expr(body);
639 pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, ctxt: AssocCtxt) {
640 let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = *item;
641 visitor.visit_vis(vis);
642 visitor.visit_ident(ident);
643 walk_list!(visitor, visit_attribute, attrs);
645 AssocItemKind::Const(_, ty, expr) => {
646 visitor.visit_ty(ty);
647 walk_list!(visitor, visit_expr, expr);
649 AssocItemKind::Fn(_, sig, generics, body) => {
650 visitor.visit_generics(generics);
651 let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, body.as_deref());
652 visitor.visit_fn(kind, span, id);
654 AssocItemKind::TyAlias(_, generics, bounds, ty) => {
655 visitor.visit_generics(generics);
656 walk_list!(visitor, visit_param_bound, bounds);
657 walk_list!(visitor, visit_ty, ty);
659 AssocItemKind::MacCall(mac) => {
660 visitor.visit_mac_call(mac);
665 pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
666 walk_list!(visitor, visit_struct_field, struct_definition.fields());
669 pub fn walk_struct_field<'a, V: Visitor<'a>>(visitor: &mut V, struct_field: &'a StructField) {
670 visitor.visit_vis(&struct_field.vis);
671 if let Some(ident) = struct_field.ident {
672 visitor.visit_ident(ident);
674 visitor.visit_ty(&struct_field.ty);
675 walk_list!(visitor, visit_attribute, &struct_field.attrs);
678 pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
679 walk_list!(visitor, visit_stmt, &block.stmts);
682 pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
683 match statement.kind {
684 StmtKind::Local(ref local) => visitor.visit_local(local),
685 StmtKind::Item(ref item) => visitor.visit_item(item),
686 StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => visitor.visit_expr(expr),
687 StmtKind::Empty => {}
688 StmtKind::MacCall(ref mac) => {
689 let MacCallStmt { ref mac, style: _, ref attrs } = **mac;
690 visitor.visit_mac_call(mac);
691 for attr in attrs.iter() {
692 visitor.visit_attribute(attr);
698 pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a MacCall) {
699 visitor.visit_path(&mac.path, DUMMY_NODE_ID);
702 pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
703 visitor.visit_expr(&constant.value);
706 pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
707 walk_list!(visitor, visit_attribute, expression.attrs.iter());
709 match expression.kind {
710 ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
711 ExprKind::Array(ref subexpressions) => {
712 walk_list!(visitor, visit_expr, subexpressions);
714 ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
715 ExprKind::Repeat(ref element, ref count) => {
716 visitor.visit_expr(element);
717 visitor.visit_anon_const(count)
719 ExprKind::Struct(ref path, ref fields, ref optional_base) => {
720 visitor.visit_path(path, expression.id);
721 walk_list!(visitor, visit_field, fields);
722 match optional_base {
723 StructRest::Base(expr) => visitor.visit_expr(expr),
724 StructRest::Rest(_span) => {}
725 StructRest::None => {}
728 ExprKind::Tup(ref subexpressions) => {
729 walk_list!(visitor, visit_expr, subexpressions);
731 ExprKind::Call(ref callee_expression, ref arguments) => {
732 visitor.visit_expr(callee_expression);
733 walk_list!(visitor, visit_expr, arguments);
735 ExprKind::MethodCall(ref segment, ref arguments, _span) => {
736 visitor.visit_path_segment(expression.span, segment);
737 walk_list!(visitor, visit_expr, arguments);
739 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
740 visitor.visit_expr(left_expression);
741 visitor.visit_expr(right_expression)
743 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
744 visitor.visit_expr(subexpression)
746 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
747 visitor.visit_expr(subexpression);
748 visitor.visit_ty(typ)
750 ExprKind::Let(ref pat, ref scrutinee) => {
751 visitor.visit_pat(pat);
752 visitor.visit_expr(scrutinee);
754 ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
755 visitor.visit_expr(head_expression);
756 visitor.visit_block(if_block);
757 walk_list!(visitor, visit_expr, optional_else);
759 ExprKind::While(ref subexpression, ref block, ref opt_label) => {
760 walk_list!(visitor, visit_label, opt_label);
761 visitor.visit_expr(subexpression);
762 visitor.visit_block(block);
764 ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
765 walk_list!(visitor, visit_label, opt_label);
766 visitor.visit_pat(pattern);
767 visitor.visit_expr(subexpression);
768 visitor.visit_block(block);
770 ExprKind::Loop(ref block, ref opt_label) => {
771 walk_list!(visitor, visit_label, opt_label);
772 visitor.visit_block(block);
774 ExprKind::Match(ref subexpression, ref arms) => {
775 visitor.visit_expr(subexpression);
776 walk_list!(visitor, visit_arm, arms);
778 ExprKind::Closure(_, _, _, ref decl, ref body, _decl_span) => {
779 visitor.visit_fn(FnKind::Closure(decl, body), expression.span, expression.id)
781 ExprKind::Block(ref block, ref opt_label) => {
782 walk_list!(visitor, visit_label, opt_label);
783 visitor.visit_block(block);
785 ExprKind::Async(_, _, ref body) => {
786 visitor.visit_block(body);
788 ExprKind::Await(ref expr) => visitor.visit_expr(expr),
789 ExprKind::Assign(ref lhs, ref rhs, _) => {
790 visitor.visit_expr(lhs);
791 visitor.visit_expr(rhs);
793 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
794 visitor.visit_expr(left_expression);
795 visitor.visit_expr(right_expression);
797 ExprKind::Field(ref subexpression, ident) => {
798 visitor.visit_expr(subexpression);
799 visitor.visit_ident(ident);
801 ExprKind::Index(ref main_expression, ref index_expression) => {
802 visitor.visit_expr(main_expression);
803 visitor.visit_expr(index_expression)
805 ExprKind::Range(ref start, ref end, _) => {
806 walk_list!(visitor, visit_expr, start);
807 walk_list!(visitor, visit_expr, end);
809 ExprKind::Underscore => {}
810 ExprKind::Path(ref maybe_qself, ref path) => {
811 if let Some(ref qself) = *maybe_qself {
812 visitor.visit_ty(&qself.ty);
814 visitor.visit_path(path, expression.id)
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);
820 ExprKind::Continue(ref opt_label) => {
821 walk_list!(visitor, visit_label, opt_label);
823 ExprKind::Ret(ref optional_expression) => {
824 walk_list!(visitor, visit_expr, optional_expression);
826 ExprKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
827 ExprKind::Paren(ref subexpression) => visitor.visit_expr(subexpression),
828 ExprKind::InlineAsm(ref ia) => {
829 for (op, _) in &ia.operands {
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);
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);
849 ExprKind::LlvmInlineAsm(ref ia) => {
850 for &(_, ref input) in &ia.inputs {
851 visitor.visit_expr(input)
853 for output in &ia.outputs {
854 visitor.visit_expr(&output.expr)
857 ExprKind::Yield(ref optional_expression) => {
858 walk_list!(visitor, visit_expr, optional_expression);
860 ExprKind::Try(ref subexpression) => visitor.visit_expr(subexpression),
861 ExprKind::TryBlock(ref body) => visitor.visit_block(body),
862 ExprKind::Lit(_) | ExprKind::Err => {}
865 visitor.visit_expr_post(expression)
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(¶m.pat);
871 visitor.visit_ty(¶m.ty);
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);
881 pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
882 if let VisibilityKind::Restricted { ref path, id } = vis.kind {
883 visitor.visit_path(path, id);
887 pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
889 AttrKind::Normal(ref item, ref _tokens) => walk_mac_args(visitor, &item.args),
890 AttrKind::DocComment(..) => {}
894 pub fn walk_mac_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a MacArgs) {
897 MacArgs::Delimited(_dspan, _delim, _tokens) => {}
898 // The value in `#[key = VALUE]` must be visited as an expression for backward
899 // compatibility, so that macros can be expanded in that position.
900 MacArgs::Eq(_eq_span, tokens) => match tokens.trees_ref().next() {
901 Some(TokenTree::Token(token)) => match &token.kind {
902 token::Interpolated(nt) => match &**nt {
903 token::NtExpr(expr) => visitor.visit_expr(expr),
904 t => panic!("unexpected token in key-value attribute: {:?}", t),
906 token::Literal(..) | token::Ident(..) => {}
907 t => panic!("unexpected token in key-value attribute: {:?}", t),
909 t => panic!("unexpected token in key-value attribute: {:?}", t),