1 //! HIR walker for walking the contents of nodes.
3 //! **For an overview of the visitor strategy, see the docs on the
4 //! `super::itemlikevisit::ItemLikeVisitor` trait.**
6 //! If you have decided to use this visitor, here are some general
7 //! notes on how to do so:
9 //! Each overridden visit method has full control over what
10 //! happens with its node, it can do its own traversal of the node's children,
11 //! call `intravisit::walk_*` to apply the default traversal algorithm, or prevent
12 //! deeper traversal by doing nothing.
14 //! When visiting the HIR, the contents of nested items are NOT visited
15 //! by default. This is different from the AST visitor, which does a deep walk.
16 //! Hence this module is called `intravisit`; see the method `visit_nested_item`
19 //! Note: it is an important invariant that the default visitor walks
20 //! the body of a function in "execution order" - more concretely, if
21 //! we consider the reverse post-order (RPO) of the CFG implied by the HIR,
22 //! then a pre-order traversal of the HIR is consistent with the CFG RPO
23 //! on the *initial CFG point* of each HIR node, while a post-order traversal
24 //! of the HIR is consistent with the CFG RPO on each *final CFG point* of
27 //! One thing that follows is that if HIR node A always starts/ends executing
28 //! before HIR node B, then A appears in traversal pre/postorder before B,
29 //! respectively. (This follows from RPO respecting CFG domination).
31 //! This order consistency is required in a few places in rustc, for
32 //! example generator inference, and possibly also HIR borrowck.
35 use crate::hir_id::CRATE_HIR_ID;
36 use crate::itemlikevisit::{ItemLikeVisitor, ParItemLikeVisitor};
37 use rustc_ast::walk_list;
38 use rustc_ast::{Attribute, Label};
39 use rustc_span::symbol::{Ident, Symbol};
42 pub struct DeepVisitor<'v, V> {
46 impl<'v, V> DeepVisitor<'v, V> {
47 pub fn new(base: &'v mut V) -> Self {
48 DeepVisitor { visitor: base }
52 impl<'v, 'hir, V> ItemLikeVisitor<'hir> for DeepVisitor<'v, V>
56 fn visit_item(&mut self, item: &'hir Item<'hir>) {
57 self.visitor.visit_item(item);
60 fn visit_trait_item(&mut self, trait_item: &'hir TraitItem<'hir>) {
61 self.visitor.visit_trait_item(trait_item);
64 fn visit_impl_item(&mut self, impl_item: &'hir ImplItem<'hir>) {
65 self.visitor.visit_impl_item(impl_item);
68 fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem<'hir>) {
69 self.visitor.visit_foreign_item(foreign_item);
73 pub trait IntoVisitor<'hir> {
74 type Visitor: Visitor<'hir>;
75 fn into_visitor(&self) -> Self::Visitor;
78 pub struct ParDeepVisitor<V>(pub V);
80 impl<'hir, V> ParItemLikeVisitor<'hir> for ParDeepVisitor<V>
84 fn visit_item(&self, item: &'hir Item<'hir>) {
85 self.0.into_visitor().visit_item(item);
88 fn visit_trait_item(&self, trait_item: &'hir TraitItem<'hir>) {
89 self.0.into_visitor().visit_trait_item(trait_item);
92 fn visit_impl_item(&self, impl_item: &'hir ImplItem<'hir>) {
93 self.0.into_visitor().visit_impl_item(impl_item);
96 fn visit_foreign_item(&self, foreign_item: &'hir ForeignItem<'hir>) {
97 self.0.into_visitor().visit_foreign_item(foreign_item);
101 #[derive(Copy, Clone)]
102 pub enum FnKind<'a> {
103 /// `#[xxx] pub async/const/extern "Abi" fn foo()`
104 ItemFn(Ident, &'a Generics<'a>, FnHeader, &'a Visibility<'a>),
107 Method(Ident, &'a FnSig<'a>, Option<&'a Visibility<'a>>),
113 impl<'a> FnKind<'a> {
114 pub fn header(&self) -> Option<&FnHeader> {
116 FnKind::ItemFn(_, _, ref header, _) => Some(header),
117 FnKind::Method(_, ref sig, _) => Some(&sig.header),
118 FnKind::Closure => None,
123 /// An abstract representation of the HIR `rustc_middle::hir::map::Map`.
124 pub trait Map<'hir> {
125 /// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
126 fn find(&self, hir_id: HirId) -> Option<Node<'hir>>;
127 fn body(&self, id: BodyId) -> &'hir Body<'hir>;
128 fn item(&self, id: ItemId) -> &'hir Item<'hir>;
129 fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>;
130 fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>;
131 fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>;
134 /// An erased version of `Map<'hir>`, using dynamic dispatch.
135 /// NOTE: This type is effectively only usable with `NestedVisitorMap::None`.
136 pub struct ErasedMap<'hir>(&'hir dyn Map<'hir>);
138 impl<'hir> Map<'hir> for ErasedMap<'hir> {
139 fn find(&self, _: HirId) -> Option<Node<'hir>> {
142 fn body(&self, id: BodyId) -> &'hir Body<'hir> {
145 fn item(&self, id: ItemId) -> &'hir Item<'hir> {
148 fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> {
149 self.0.trait_item(id)
151 fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
154 fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> {
155 self.0.foreign_item(id)
159 /// Specifies what nested things a visitor wants to visit. The most
160 /// common choice is `OnlyBodies`, which will cause the visitor to
161 /// visit fn bodies for fns that it encounters, but skip over nested
162 /// item-like things.
164 /// See the comments on `ItemLikeVisitor` for more details on the overall
166 pub enum NestedVisitorMap<M> {
167 /// Do not visit any nested things. When you add a new
168 /// "non-nested" thing, you will want to audit such uses to see if
169 /// they remain valid.
171 /// Use this if you are only walking some particular kind of tree
172 /// (i.e., a type, or fn signature) and you don't want to thread a
176 /// Do not visit nested item-like things, but visit nested things
177 /// that are inside of an item-like.
179 /// **This is the most common choice.** A very common pattern is
180 /// to use `visit_all_item_likes()` as an outer loop,
181 /// and to have the visitor that visits the contents of each item
182 /// using this setting.
185 /// Visits all nested things, including item-likes.
187 /// **This is an unusual choice.** It is used when you want to
188 /// process everything within their lexical context. Typically you
189 /// kick off the visit by doing `walk_krate()`.
193 impl<M> NestedVisitorMap<M> {
194 /// Returns the map to use for an "intra item-like" thing (if any).
195 /// E.g., function body.
196 fn intra(self) -> Option<M> {
198 NestedVisitorMap::None => None,
199 NestedVisitorMap::OnlyBodies(map) => Some(map),
200 NestedVisitorMap::All(map) => Some(map),
204 /// Returns the map to use for an "item-like" thing (if any).
205 /// E.g., item, impl-item.
206 fn inter(self) -> Option<M> {
208 NestedVisitorMap::None => None,
209 NestedVisitorMap::OnlyBodies(_) => None,
210 NestedVisitorMap::All(map) => Some(map),
215 /// Each method of the Visitor trait is a hook to be potentially
216 /// overridden. Each method's default implementation recursively visits
217 /// the substructure of the input via the corresponding `walk` method;
218 /// e.g., the `visit_mod` method by default calls `intravisit::walk_mod`.
220 /// Note that this visitor does NOT visit nested items by default
221 /// (this is why the module is called `intravisit`, to distinguish it
222 /// from the AST's `visit` module, which acts differently). If you
223 /// simply want to visit all items in the crate in some order, you
224 /// should call `Crate::visit_all_items`. Otherwise, see the comment
225 /// on `visit_nested_item` for details on how to visit nested items.
227 /// If you want to ensure that your code handles every variant
228 /// explicitly, you need to override each method. (And you also need
229 /// to monitor future changes to `Visitor` in case a new method with a
230 /// new default implementation gets introduced.)
231 pub trait Visitor<'v>: Sized {
234 ///////////////////////////////////////////////////////////////////////////
237 /// The default versions of the `visit_nested_XXX` routines invoke
238 /// this method to get a map to use. By selecting an enum variant,
239 /// you control which kinds of nested HIR are visited; see
240 /// `NestedVisitorMap` for details. By "nested HIR", we are
241 /// referring to bits of HIR that are not directly embedded within
242 /// one another but rather indirectly, through a table in the
243 /// crate. This is done to control dependencies during incremental
244 /// compilation: the non-inline bits of HIR can be tracked and
245 /// hashed separately.
247 /// **If for some reason you want the nested behavior, but don't
248 /// have a `Map` at your disposal:** then you should override the
249 /// `visit_nested_XXX` methods, and override this method to
250 /// `panic!()`. This way, if a new `visit_nested_XXX` variant is
251 /// added in the future, we will see the panic in your code and
252 /// fix it appropriately.
253 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map>;
255 /// Invoked when a nested item is encountered. By default does
256 /// nothing unless you override `nested_visit_map` to return other than
257 /// `None`, in which case it will walk the item. **You probably
258 /// don't want to override this method** -- instead, override
259 /// `nested_visit_map` or use the "shallow" or "deep" visit
260 /// patterns described on `itemlikevisit::ItemLikeVisitor`. The only
261 /// reason to override this method is if you want a nested pattern
262 /// but cannot supply a `Map`; see `nested_visit_map` for advice.
263 fn visit_nested_item(&mut self, id: ItemId) {
264 let opt_item = self.nested_visit_map().inter().map(|map| map.item(id));
265 walk_list!(self, visit_item, opt_item);
268 /// Like `visit_nested_item()`, but for trait items. See
269 /// `visit_nested_item()` for advice on when to override this
271 fn visit_nested_trait_item(&mut self, id: TraitItemId) {
272 let opt_item = self.nested_visit_map().inter().map(|map| map.trait_item(id));
273 walk_list!(self, visit_trait_item, opt_item);
276 /// Like `visit_nested_item()`, but for impl items. See
277 /// `visit_nested_item()` for advice on when to override this
279 fn visit_nested_impl_item(&mut self, id: ImplItemId) {
280 let opt_item = self.nested_visit_map().inter().map(|map| map.impl_item(id));
281 walk_list!(self, visit_impl_item, opt_item);
284 /// Like `visit_nested_item()`, but for foreign items. See
285 /// `visit_nested_item()` for advice on when to override this
287 fn visit_nested_foreign_item(&mut self, id: ForeignItemId) {
288 let opt_item = self.nested_visit_map().inter().map(|map| map.foreign_item(id));
289 walk_list!(self, visit_foreign_item, opt_item);
292 /// Invoked to visit the body of a function, method or closure. Like
293 /// visit_nested_item, does nothing by default unless you override
294 /// `nested_visit_map` to return other than `None`, in which case it will walk
296 fn visit_nested_body(&mut self, id: BodyId) {
297 let opt_body = self.nested_visit_map().intra().map(|map| map.body(id));
298 walk_list!(self, visit_body, opt_body);
301 fn visit_param(&mut self, param: &'v Param<'v>) {
302 walk_param(self, param)
305 /// Visits the top-level item and (optionally) nested items / impl items. See
306 /// `visit_nested_item` for details.
307 fn visit_item(&mut self, i: &'v Item<'v>) {
311 fn visit_body(&mut self, b: &'v Body<'v>) {
315 /// When invoking `visit_all_item_likes()`, you need to supply an
316 /// item-like visitor. This method converts a "intra-visit"
317 /// visitor into an item-like visitor that walks the entire tree.
318 /// If you use this, you probably don't want to process the
319 /// contents of nested item-like things, since the outer loop will
320 /// visit them as well.
321 fn as_deep_visitor(&mut self) -> DeepVisitor<'_, Self> {
322 DeepVisitor::new(self)
325 ///////////////////////////////////////////////////////////////////////////
327 fn visit_id(&mut self, _hir_id: HirId) {
330 fn visit_name(&mut self, _span: Span, _name: Symbol) {
333 fn visit_ident(&mut self, ident: Ident) {
334 walk_ident(self, ident)
336 fn visit_mod(&mut self, m: &'v Mod<'v>, _s: Span, n: HirId) {
339 fn visit_foreign_item(&mut self, i: &'v ForeignItem<'v>) {
340 walk_foreign_item(self, i)
342 fn visit_local(&mut self, l: &'v Local<'v>) {
345 fn visit_block(&mut self, b: &'v Block<'v>) {
348 fn visit_stmt(&mut self, s: &'v Stmt<'v>) {
351 fn visit_arm(&mut self, a: &'v Arm<'v>) {
354 fn visit_pat(&mut self, p: &'v Pat<'v>) {
357 fn visit_anon_const(&mut self, c: &'v AnonConst) {
358 walk_anon_const(self, c)
360 fn visit_expr(&mut self, ex: &'v Expr<'v>) {
363 fn visit_ty(&mut self, t: &'v Ty<'v>) {
366 fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) {
367 walk_generic_param(self, p)
369 fn visit_generics(&mut self, g: &'v Generics<'v>) {
370 walk_generics(self, g)
372 fn visit_where_predicate(&mut self, predicate: &'v WherePredicate<'v>) {
373 walk_where_predicate(self, predicate)
375 fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) {
376 walk_fn_decl(self, fd)
378 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, s: Span, id: HirId) {
379 walk_fn(self, fk, fd, b, s, id)
381 fn visit_use(&mut self, path: &'v Path<'v>, hir_id: HirId) {
382 walk_use(self, path, hir_id)
384 fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) {
385 walk_trait_item(self, ti)
387 fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
388 walk_trait_item_ref(self, ii)
390 fn visit_impl_item(&mut self, ii: &'v ImplItem<'v>) {
391 walk_impl_item(self, ii)
393 fn visit_foreign_item_ref(&mut self, ii: &'v ForeignItemRef<'v>) {
394 walk_foreign_item_ref(self, ii)
396 fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef<'v>) {
397 walk_impl_item_ref(self, ii)
399 fn visit_trait_ref(&mut self, t: &'v TraitRef<'v>) {
400 walk_trait_ref(self, t)
402 fn visit_param_bound(&mut self, bounds: &'v GenericBound<'v>) {
403 walk_param_bound(self, bounds)
405 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>, m: TraitBoundModifier) {
406 walk_poly_trait_ref(self, t, m)
408 fn visit_variant_data(
410 s: &'v VariantData<'v>,
416 walk_struct_def(self, s)
418 fn visit_field_def(&mut self, s: &'v FieldDef<'v>) {
419 walk_field_def(self, s)
423 enum_definition: &'v EnumDef<'v>,
424 generics: &'v Generics<'v>,
428 walk_enum_def(self, enum_definition, generics, item_id)
430 fn visit_variant(&mut self, v: &'v Variant<'v>, g: &'v Generics<'v>, item_id: HirId) {
431 walk_variant(self, v, g, item_id)
433 fn visit_label(&mut self, label: &'v Label) {
434 walk_label(self, label)
436 fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg<'v>) {
438 GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
439 GenericArg::Type(ty) => self.visit_ty(ty),
440 GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
443 fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
444 walk_lifetime(self, lifetime)
446 fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, span: Span) {
447 walk_qpath(self, qpath, id, span)
449 fn visit_path(&mut self, path: &'v Path<'v>, _id: HirId) {
450 walk_path(self, path)
452 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment<'v>) {
453 walk_path_segment(self, path_span, path_segment)
455 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs<'v>) {
456 walk_generic_args(self, path_span, generic_args)
458 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding<'v>) {
459 walk_assoc_type_binding(self, type_binding)
461 fn visit_attribute(&mut self, _id: HirId, _attr: &'v Attribute) {}
462 fn visit_macro_def(&mut self, macro_def: &'v MacroDef<'v>) {
463 walk_macro_def(self, macro_def)
465 fn visit_vis(&mut self, vis: &'v Visibility<'v>) {
468 fn visit_associated_item_kind(&mut self, kind: &'v AssocItemKind) {
469 walk_associated_item_kind(self, kind);
471 fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
472 walk_defaultness(self, defaultness);
476 /// Walks the contents of a crate. See also `Crate::visit_all_items`.
477 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) {
478 visitor.visit_mod(&krate.item.module, krate.item.span, CRATE_HIR_ID);
479 walk_list!(visitor, visit_macro_def, krate.exported_macros);
480 for (&id, attrs) in krate.attrs.iter() {
482 visitor.visit_attribute(id, a)
487 pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef<'v>) {
488 visitor.visit_id(macro_def.hir_id());
489 visitor.visit_ident(macro_def.ident);
492 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod<'v>, mod_hir_id: HirId) {
493 visitor.visit_id(mod_hir_id);
494 for &item_id in module.item_ids {
495 visitor.visit_nested_item(item_id);
499 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body<'v>) {
500 walk_list!(visitor, visit_param, body.params);
501 visitor.visit_expr(&body.value);
504 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local<'v>) {
505 // Intentionally visiting the expr first - the initialization expr
506 // dominates the local's definition.
507 walk_list!(visitor, visit_expr, &local.init);
508 visitor.visit_id(local.hir_id);
509 visitor.visit_pat(&local.pat);
510 walk_list!(visitor, visit_ty, &local.ty);
513 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
514 visitor.visit_name(ident.span, ident.name);
517 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
518 visitor.visit_ident(label.ident);
521 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
522 visitor.visit_id(lifetime.hir_id);
523 match lifetime.name {
524 LifetimeName::Param(ParamName::Plain(ident)) => {
525 visitor.visit_ident(ident);
527 LifetimeName::Param(ParamName::Fresh(_))
528 | LifetimeName::Param(ParamName::Error)
529 | LifetimeName::Static
530 | LifetimeName::Error
531 | LifetimeName::Implicit
532 | LifetimeName::ImplicitObjectLifetimeDefault
533 | LifetimeName::Underscore => {}
537 pub fn walk_poly_trait_ref<'v, V: Visitor<'v>>(
539 trait_ref: &'v PolyTraitRef<'v>,
540 _modifier: TraitBoundModifier,
542 walk_list!(visitor, visit_generic_param, trait_ref.bound_generic_params);
543 visitor.visit_trait_ref(&trait_ref.trait_ref);
546 pub fn walk_trait_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_ref: &'v TraitRef<'v>) {
547 visitor.visit_id(trait_ref.hir_ref_id);
548 visitor.visit_path(&trait_ref.path, trait_ref.hir_ref_id)
551 pub fn walk_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Param<'v>) {
552 visitor.visit_id(param.hir_id);
553 visitor.visit_pat(¶m.pat);
556 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
557 visitor.visit_vis(&item.vis);
558 visitor.visit_ident(item.ident);
560 ItemKind::ExternCrate(orig_name) => {
561 visitor.visit_id(item.hir_id());
562 if let Some(orig_name) = orig_name {
563 visitor.visit_name(item.span, orig_name);
566 ItemKind::Use(ref path, _) => {
567 visitor.visit_use(path, item.hir_id());
569 ItemKind::Static(ref typ, _, body) | ItemKind::Const(ref typ, body) => {
570 visitor.visit_id(item.hir_id());
571 visitor.visit_ty(typ);
572 visitor.visit_nested_body(body);
574 ItemKind::Fn(ref sig, ref generics, body_id) => visitor.visit_fn(
575 FnKind::ItemFn(item.ident, generics, sig.header, &item.vis),
581 ItemKind::Mod(ref module) => {
582 // `visit_mod()` takes care of visiting the `Item`'s `HirId`.
583 visitor.visit_mod(module, item.span, item.hir_id())
585 ItemKind::ForeignMod { abi: _, items } => {
586 visitor.visit_id(item.hir_id());
587 walk_list!(visitor, visit_foreign_item_ref, items);
589 ItemKind::GlobalAsm(_) => {
590 visitor.visit_id(item.hir_id());
592 ItemKind::TyAlias(ref ty, ref generics) => {
593 visitor.visit_id(item.hir_id());
594 visitor.visit_ty(ty);
595 visitor.visit_generics(generics)
597 ItemKind::OpaqueTy(OpaqueTy { ref generics, bounds, .. }) => {
598 visitor.visit_id(item.hir_id());
599 walk_generics(visitor, generics);
600 walk_list!(visitor, visit_param_bound, bounds);
602 ItemKind::Enum(ref enum_definition, ref generics) => {
603 visitor.visit_generics(generics);
604 // `visit_enum_def()` takes care of visiting the `Item`'s `HirId`.
605 visitor.visit_enum_def(enum_definition, generics, item.hir_id(), item.span)
607 ItemKind::Impl(Impl {
618 visitor.visit_id(item.hir_id());
619 visitor.visit_generics(generics);
620 walk_list!(visitor, visit_trait_ref, of_trait);
621 visitor.visit_ty(self_ty);
622 walk_list!(visitor, visit_impl_item_ref, items);
624 ItemKind::Struct(ref struct_definition, ref generics)
625 | ItemKind::Union(ref struct_definition, ref generics) => {
626 visitor.visit_generics(generics);
627 visitor.visit_id(item.hir_id());
628 visitor.visit_variant_data(
636 ItemKind::Trait(.., ref generics, bounds, trait_item_refs) => {
637 visitor.visit_id(item.hir_id());
638 visitor.visit_generics(generics);
639 walk_list!(visitor, visit_param_bound, bounds);
640 walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
642 ItemKind::TraitAlias(ref generics, bounds) => {
643 visitor.visit_id(item.hir_id());
644 visitor.visit_generics(generics);
645 walk_list!(visitor, visit_param_bound, bounds);
650 pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>, hir_id: HirId) {
651 visitor.visit_id(hir_id);
652 visitor.visit_path(path, hir_id);
655 pub fn walk_enum_def<'v, V: Visitor<'v>>(
657 enum_definition: &'v EnumDef<'v>,
658 generics: &'v Generics<'v>,
661 visitor.visit_id(item_id);
662 walk_list!(visitor, visit_variant, enum_definition.variants, generics, item_id);
665 pub fn walk_variant<'v, V: Visitor<'v>>(
667 variant: &'v Variant<'v>,
668 generics: &'v Generics<'v>,
669 parent_item_id: HirId,
671 visitor.visit_ident(variant.ident);
672 visitor.visit_id(variant.id);
673 visitor.visit_variant_data(
680 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
683 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
684 visitor.visit_id(typ.hir_id);
687 TyKind::Slice(ref ty) => visitor.visit_ty(ty),
688 TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
689 TyKind::Rptr(ref lifetime, ref mutable_type) => {
690 visitor.visit_lifetime(lifetime);
691 visitor.visit_ty(&mutable_type.ty)
694 TyKind::Tup(tuple_element_types) => {
695 walk_list!(visitor, visit_ty, tuple_element_types);
697 TyKind::BareFn(ref function_declaration) => {
698 walk_list!(visitor, visit_generic_param, function_declaration.generic_params);
699 visitor.visit_fn_decl(&function_declaration.decl);
701 TyKind::Path(ref qpath) => {
702 visitor.visit_qpath(qpath, typ.hir_id, typ.span);
704 TyKind::OpaqueDef(item_id, lifetimes) => {
705 visitor.visit_nested_item(item_id);
706 walk_list!(visitor, visit_generic_arg, lifetimes);
708 TyKind::Array(ref ty, ref length) => {
709 visitor.visit_ty(ty);
710 visitor.visit_anon_const(length)
712 TyKind::TraitObject(bounds, ref lifetime) => {
713 for bound in bounds {
714 visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
716 visitor.visit_lifetime(lifetime);
718 TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
719 TyKind::Infer | TyKind::Err => {}
723 pub fn walk_qpath<'v, V: Visitor<'v>>(
725 qpath: &'v QPath<'v>,
730 QPath::Resolved(ref maybe_qself, ref path) => {
731 walk_list!(visitor, visit_ty, maybe_qself);
732 visitor.visit_path(path, id)
734 QPath::TypeRelative(ref qself, ref segment) => {
735 visitor.visit_ty(qself);
736 visitor.visit_path_segment(span, segment);
738 QPath::LangItem(..) => {}
742 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>) {
743 for segment in path.segments {
744 visitor.visit_path_segment(path.span, segment);
748 pub fn walk_path_segment<'v, V: Visitor<'v>>(
751 segment: &'v PathSegment<'v>,
753 visitor.visit_ident(segment.ident);
754 walk_list!(visitor, visit_id, segment.hir_id);
755 if let Some(ref args) = segment.args {
756 visitor.visit_generic_args(path_span, args);
760 pub fn walk_generic_args<'v, V: Visitor<'v>>(
763 generic_args: &'v GenericArgs<'v>,
765 walk_list!(visitor, visit_generic_arg, generic_args.args);
766 walk_list!(visitor, visit_assoc_type_binding, generic_args.bindings);
769 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(
771 type_binding: &'v TypeBinding<'v>,
773 visitor.visit_id(type_binding.hir_id);
774 visitor.visit_ident(type_binding.ident);
775 visitor.visit_generic_args(type_binding.span, type_binding.gen_args);
776 match type_binding.kind {
777 TypeBindingKind::Equality { ref ty } => {
778 visitor.visit_ty(ty);
780 TypeBindingKind::Constraint { bounds } => {
781 walk_list!(visitor, visit_param_bound, bounds);
786 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) {
787 visitor.visit_id(pattern.hir_id);
789 PatKind::TupleStruct(ref qpath, children, _) => {
790 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
791 walk_list!(visitor, visit_pat, children);
793 PatKind::Path(ref qpath) => {
794 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
796 PatKind::Struct(ref qpath, fields, _) => {
797 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
798 for field in fields {
799 visitor.visit_id(field.hir_id);
800 visitor.visit_ident(field.ident);
801 visitor.visit_pat(&field.pat)
804 PatKind::Or(pats) => walk_list!(visitor, visit_pat, pats),
805 PatKind::Tuple(tuple_elements, _) => {
806 walk_list!(visitor, visit_pat, tuple_elements);
808 PatKind::Box(ref subpattern) | PatKind::Ref(ref subpattern, _) => {
809 visitor.visit_pat(subpattern)
811 PatKind::Binding(_, _hir_id, ident, ref optional_subpattern) => {
812 visitor.visit_ident(ident);
813 walk_list!(visitor, visit_pat, optional_subpattern);
815 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
816 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
817 walk_list!(visitor, visit_expr, lower_bound);
818 walk_list!(visitor, visit_expr, upper_bound);
821 PatKind::Slice(prepatterns, ref slice_pattern, postpatterns) => {
822 walk_list!(visitor, visit_pat, prepatterns);
823 walk_list!(visitor, visit_pat, slice_pattern);
824 walk_list!(visitor, visit_pat, postpatterns);
829 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem<'v>) {
830 visitor.visit_id(foreign_item.hir_id());
831 visitor.visit_vis(&foreign_item.vis);
832 visitor.visit_ident(foreign_item.ident);
834 match foreign_item.kind {
835 ForeignItemKind::Fn(ref function_declaration, param_names, ref generics) => {
836 visitor.visit_generics(generics);
837 visitor.visit_fn_decl(function_declaration);
838 for ¶m_name in param_names {
839 visitor.visit_ident(param_name);
842 ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
843 ForeignItemKind::Type => (),
847 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound<'v>) {
849 GenericBound::Trait(ref typ, modifier) => {
850 visitor.visit_poly_trait_ref(typ, modifier);
852 GenericBound::LangItemTrait(_, span, hir_id, args) => {
853 visitor.visit_id(hir_id);
854 visitor.visit_generic_args(span, args);
856 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
860 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam<'v>) {
861 visitor.visit_id(param.hir_id);
863 ParamName::Plain(ident) => visitor.visit_ident(ident),
864 ParamName::Error | ParamName::Fresh(_) => {}
867 GenericParamKind::Lifetime { .. } => {}
868 GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
869 GenericParamKind::Const { ref ty, ref default } => {
870 visitor.visit_ty(ty);
871 if let Some(ref default) = default {
872 visitor.visit_anon_const(default);
876 walk_list!(visitor, visit_param_bound, param.bounds);
879 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) {
880 walk_list!(visitor, visit_generic_param, generics.params);
881 walk_list!(visitor, visit_where_predicate, generics.where_clause.predicates);
884 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
886 predicate: &'v WherePredicate<'v>,
889 WherePredicate::BoundPredicate(WhereBoundPredicate {
892 bound_generic_params,
895 visitor.visit_ty(bounded_ty);
896 walk_list!(visitor, visit_param_bound, bounds);
897 walk_list!(visitor, visit_generic_param, bound_generic_params);
899 WherePredicate::RegionPredicate(WhereRegionPredicate { ref lifetime, bounds, .. }) => {
900 visitor.visit_lifetime(lifetime);
901 walk_list!(visitor, visit_param_bound, bounds);
903 WherePredicate::EqPredicate(WhereEqPredicate {
904 hir_id, ref lhs_ty, ref rhs_ty, ..
906 visitor.visit_id(hir_id);
907 visitor.visit_ty(lhs_ty);
908 visitor.visit_ty(rhs_ty);
913 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FnRetTy<'v>) {
914 if let FnRetTy::Return(ref output_ty) = *ret_ty {
915 visitor.visit_ty(output_ty)
919 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl<'v>) {
920 for ty in function_declaration.inputs {
923 walk_fn_ret_ty(visitor, &function_declaration.output)
926 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
927 match function_kind {
928 FnKind::ItemFn(_, generics, ..) => {
929 visitor.visit_generics(generics);
931 FnKind::Method(..) | FnKind::Closure => {}
935 pub fn walk_fn<'v, V: Visitor<'v>>(
937 function_kind: FnKind<'v>,
938 function_declaration: &'v FnDecl<'v>,
943 visitor.visit_id(id);
944 visitor.visit_fn_decl(function_declaration);
945 walk_fn_kind(visitor, function_kind);
946 visitor.visit_nested_body(body_id)
949 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem<'v>) {
950 visitor.visit_ident(trait_item.ident);
951 visitor.visit_generics(&trait_item.generics);
952 match trait_item.kind {
953 TraitItemKind::Const(ref ty, default) => {
954 visitor.visit_id(trait_item.hir_id());
955 visitor.visit_ty(ty);
956 walk_list!(visitor, visit_nested_body, default);
958 TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => {
959 visitor.visit_id(trait_item.hir_id());
960 visitor.visit_fn_decl(&sig.decl);
961 for ¶m_name in param_names {
962 visitor.visit_ident(param_name);
965 TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => {
967 FnKind::Method(trait_item.ident, sig, None),
974 TraitItemKind::Type(bounds, ref default) => {
975 visitor.visit_id(trait_item.hir_id());
976 walk_list!(visitor, visit_param_bound, bounds);
977 walk_list!(visitor, visit_ty, default);
982 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
983 // N.B., deliberately force a compilation error if/when new fields are added.
984 let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
985 visitor.visit_nested_trait_item(id);
986 visitor.visit_ident(ident);
987 visitor.visit_associated_item_kind(kind);
988 visitor.visit_defaultness(defaultness);
991 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem<'v>) {
992 // N.B., deliberately force a compilation error if/when new fields are added.
993 let ImplItem { def_id: _, ident, ref vis, ref defaultness, ref generics, ref kind, span: _ } =
996 visitor.visit_ident(ident);
997 visitor.visit_vis(vis);
998 visitor.visit_defaultness(defaultness);
999 visitor.visit_generics(generics);
1001 ImplItemKind::Const(ref ty, body) => {
1002 visitor.visit_id(impl_item.hir_id());
1003 visitor.visit_ty(ty);
1004 visitor.visit_nested_body(body);
1006 ImplItemKind::Fn(ref sig, body_id) => {
1008 FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis)),
1015 ImplItemKind::TyAlias(ref ty) => {
1016 visitor.visit_id(impl_item.hir_id());
1017 visitor.visit_ty(ty);
1022 pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(
1024 foreign_item_ref: &'v ForeignItemRef<'v>,
1026 // N.B., deliberately force a compilation error if/when new fields are added.
1027 let ForeignItemRef { id, ident, span: _, ref vis } = *foreign_item_ref;
1028 visitor.visit_nested_foreign_item(id);
1029 visitor.visit_ident(ident);
1030 visitor.visit_vis(vis);
1033 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef<'v>) {
1034 // N.B., deliberately force a compilation error if/when new fields are added.
1035 let ImplItemRef { id, ident, ref kind, span: _, ref vis, ref defaultness } = *impl_item_ref;
1036 visitor.visit_nested_impl_item(id);
1037 visitor.visit_ident(ident);
1038 visitor.visit_associated_item_kind(kind);
1039 visitor.visit_vis(vis);
1040 visitor.visit_defaultness(defaultness);
1043 pub fn walk_struct_def<'v, V: Visitor<'v>>(
1045 struct_definition: &'v VariantData<'v>,
1047 walk_list!(visitor, visit_id, struct_definition.ctor_hir_id());
1048 walk_list!(visitor, visit_field_def, struct_definition.fields());
1051 pub fn walk_field_def<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v FieldDef<'v>) {
1052 visitor.visit_id(field.hir_id);
1053 visitor.visit_vis(&field.vis);
1054 visitor.visit_ident(field.ident);
1055 visitor.visit_ty(&field.ty);
1058 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block<'v>) {
1059 visitor.visit_id(block.hir_id);
1060 walk_list!(visitor, visit_stmt, block.stmts);
1061 walk_list!(visitor, visit_expr, &block.expr);
1064 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt<'v>) {
1065 visitor.visit_id(statement.hir_id);
1066 match statement.kind {
1067 StmtKind::Local(ref local) => visitor.visit_local(local),
1068 StmtKind::Item(item) => visitor.visit_nested_item(item),
1069 StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
1070 visitor.visit_expr(expression)
1075 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
1076 visitor.visit_id(constant.hir_id);
1077 visitor.visit_nested_body(constant.body);
1080 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) {
1081 visitor.visit_id(expression.hir_id);
1082 match expression.kind {
1083 ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
1084 ExprKind::Array(subexpressions) => {
1085 walk_list!(visitor, visit_expr, subexpressions);
1087 ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
1088 ExprKind::Repeat(ref element, ref count) => {
1089 visitor.visit_expr(element);
1090 visitor.visit_anon_const(count)
1092 ExprKind::Struct(ref qpath, fields, ref optional_base) => {
1093 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1094 for field in fields {
1095 visitor.visit_id(field.hir_id);
1096 visitor.visit_ident(field.ident);
1097 visitor.visit_expr(&field.expr)
1099 walk_list!(visitor, visit_expr, optional_base);
1101 ExprKind::Tup(subexpressions) => {
1102 walk_list!(visitor, visit_expr, subexpressions);
1104 ExprKind::Call(ref callee_expression, arguments) => {
1105 visitor.visit_expr(callee_expression);
1106 walk_list!(visitor, visit_expr, arguments);
1108 ExprKind::MethodCall(ref segment, _, arguments, _) => {
1109 visitor.visit_path_segment(expression.span, segment);
1110 walk_list!(visitor, visit_expr, arguments);
1112 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
1113 visitor.visit_expr(left_expression);
1114 visitor.visit_expr(right_expression)
1116 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
1117 visitor.visit_expr(subexpression)
1119 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
1120 visitor.visit_expr(subexpression);
1121 visitor.visit_ty(typ)
1123 ExprKind::DropTemps(ref subexpression) => {
1124 visitor.visit_expr(subexpression);
1126 ExprKind::If(ref cond, ref then, ref else_opt) => {
1127 visitor.visit_expr(cond);
1128 visitor.visit_expr(then);
1129 walk_list!(visitor, visit_expr, else_opt);
1131 ExprKind::Loop(ref block, ref opt_label, _, _) => {
1132 walk_list!(visitor, visit_label, opt_label);
1133 visitor.visit_block(block);
1135 ExprKind::Match(ref subexpression, arms, _) => {
1136 visitor.visit_expr(subexpression);
1137 walk_list!(visitor, visit_arm, arms);
1139 ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => visitor
1142 function_declaration,
1147 ExprKind::Block(ref block, ref opt_label) => {
1148 walk_list!(visitor, visit_label, opt_label);
1149 visitor.visit_block(block);
1151 ExprKind::Assign(ref lhs, ref rhs, _) => {
1152 visitor.visit_expr(rhs);
1153 visitor.visit_expr(lhs)
1155 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
1156 visitor.visit_expr(right_expression);
1157 visitor.visit_expr(left_expression);
1159 ExprKind::Field(ref subexpression, ident) => {
1160 visitor.visit_expr(subexpression);
1161 visitor.visit_ident(ident);
1163 ExprKind::Index(ref main_expression, ref index_expression) => {
1164 visitor.visit_expr(main_expression);
1165 visitor.visit_expr(index_expression)
1167 ExprKind::Path(ref qpath) => {
1168 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1170 ExprKind::Break(ref destination, ref opt_expr) => {
1171 walk_list!(visitor, visit_label, &destination.label);
1172 walk_list!(visitor, visit_expr, opt_expr);
1174 ExprKind::Continue(ref destination) => {
1175 walk_list!(visitor, visit_label, &destination.label);
1177 ExprKind::Ret(ref optional_expression) => {
1178 walk_list!(visitor, visit_expr, optional_expression);
1180 ExprKind::InlineAsm(ref asm) => {
1181 for (op, _op_sp) in asm.operands {
1183 InlineAsmOperand::In { expr, .. }
1184 | InlineAsmOperand::InOut { expr, .. }
1185 | InlineAsmOperand::Const { expr, .. }
1186 | InlineAsmOperand::Sym { expr, .. } => visitor.visit_expr(expr),
1187 InlineAsmOperand::Out { expr, .. } => {
1188 if let Some(expr) = expr {
1189 visitor.visit_expr(expr);
1192 InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
1193 visitor.visit_expr(in_expr);
1194 if let Some(out_expr) = out_expr {
1195 visitor.visit_expr(out_expr);
1201 ExprKind::LlvmInlineAsm(ref asm) => {
1202 walk_list!(visitor, visit_expr, asm.outputs_exprs);
1203 walk_list!(visitor, visit_expr, asm.inputs_exprs);
1205 ExprKind::Yield(ref subexpression, _) => {
1206 visitor.visit_expr(subexpression);
1208 ExprKind::Lit(_) | ExprKind::Err => {}
1212 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm<'v>) {
1213 visitor.visit_id(arm.hir_id);
1214 visitor.visit_pat(&arm.pat);
1215 if let Some(ref g) = arm.guard {
1217 Guard::If(ref e) => visitor.visit_expr(e),
1218 Guard::IfLet(ref pat, ref e) => {
1219 visitor.visit_pat(pat);
1220 visitor.visit_expr(e);
1224 visitor.visit_expr(&arm.body);
1227 pub fn walk_vis<'v, V: Visitor<'v>>(visitor: &mut V, vis: &'v Visibility<'v>) {
1228 if let VisibilityKind::Restricted { ref path, hir_id } = vis.node {
1229 visitor.visit_id(hir_id);
1230 visitor.visit_path(path, hir_id)
1234 pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssocItemKind) {
1235 // No visitable content here: this fn exists so you can call it if
1236 // the right thing to do, should content be added in the future,
1237 // would be to walk it.
1240 pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) {
1241 // No visitable content here: this fn exists so you can call it if
1242 // the right thing to do, should content be added in the future,
1243 // would be to walk it.