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::ast::{Attribute, Ident, Label, Name};
38 use rustc_ast::walk_list;
41 pub struct DeepVisitor<'v, V> {
45 impl<'v, V> DeepVisitor<'v, V> {
46 pub fn new(base: &'v mut V) -> Self {
47 DeepVisitor { visitor: base }
51 impl<'v, 'hir, V> ItemLikeVisitor<'hir> for DeepVisitor<'v, V>
55 fn visit_item(&mut self, item: &'hir Item<'hir>) {
56 self.visitor.visit_item(item);
59 fn visit_trait_item(&mut self, trait_item: &'hir TraitItem<'hir>) {
60 self.visitor.visit_trait_item(trait_item);
63 fn visit_impl_item(&mut self, impl_item: &'hir ImplItem<'hir>) {
64 self.visitor.visit_impl_item(impl_item);
68 pub trait IntoVisitor<'hir> {
69 type Visitor: Visitor<'hir>;
70 fn into_visitor(&self) -> Self::Visitor;
73 pub struct ParDeepVisitor<V>(pub V);
75 impl<'hir, V> ParItemLikeVisitor<'hir> for ParDeepVisitor<V>
79 fn visit_item(&self, item: &'hir Item<'hir>) {
80 self.0.into_visitor().visit_item(item);
83 fn visit_trait_item(&self, trait_item: &'hir TraitItem<'hir>) {
84 self.0.into_visitor().visit_trait_item(trait_item);
87 fn visit_impl_item(&self, impl_item: &'hir ImplItem<'hir>) {
88 self.0.into_visitor().visit_impl_item(impl_item);
92 #[derive(Copy, Clone)]
94 /// `#[xxx] pub async/const/extern "Abi" fn foo()`
95 ItemFn(Ident, &'a Generics<'a>, FnHeader, &'a Visibility<'a>, &'a [Attribute]),
98 Method(Ident, &'a FnSig<'a>, Option<&'a Visibility<'a>>, &'a [Attribute]),
101 Closure(&'a [Attribute]),
104 impl<'a> FnKind<'a> {
105 pub fn attrs(&self) -> &'a [Attribute] {
107 FnKind::ItemFn(.., attrs) => attrs,
108 FnKind::Method(.., attrs) => attrs,
109 FnKind::Closure(attrs) => attrs,
113 pub fn header(&self) -> Option<&FnHeader> {
115 FnKind::ItemFn(_, _, ref header, _, _) => Some(header),
116 FnKind::Method(_, ref sig, _, _) => Some(&sig.header),
117 FnKind::Closure(_) => None,
122 /// An abstract representation of the HIR `rustc::hir::map::Map`.
123 pub trait Map<'hir> {
124 fn body(&self, id: BodyId) -> &'hir Body<'hir>;
125 fn item(&self, id: HirId) -> &'hir Item<'hir>;
126 fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>;
127 fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>;
130 /// An erased version of `Map<'hir>`, using dynamic dispatch.
131 /// NOTE: This type is effectively only usable with `NestedVisitorMap::None`.
132 pub struct ErasedMap<'hir>(&'hir dyn Map<'hir>);
134 impl<'hir> Map<'hir> for ErasedMap<'hir> {
135 fn body(&self, id: BodyId) -> &'hir Body<'hir> {
138 fn item(&self, id: HirId) -> &'hir Item<'hir> {
141 fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> {
142 self.0.trait_item(id)
144 fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
149 /// Specifies what nested things a visitor wants to visit. The most
150 /// common choice is `OnlyBodies`, which will cause the visitor to
151 /// visit fn bodies for fns that it encounters, but skip over nested
152 /// item-like things.
154 /// See the comments on `ItemLikeVisitor` for more details on the overall
156 pub enum NestedVisitorMap<M> {
157 /// Do not visit any nested things. When you add a new
158 /// "non-nested" thing, you will want to audit such uses to see if
159 /// they remain valid.
161 /// Use this if you are only walking some particular kind of tree
162 /// (i.e., a type, or fn signature) and you don't want to thread a
166 /// Do not visit nested item-like things, but visit nested things
167 /// that are inside of an item-like.
169 /// **This is the most common choice.** A very common pattern is
170 /// to use `visit_all_item_likes()` as an outer loop,
171 /// and to have the visitor that visits the contents of each item
172 /// using this setting.
175 /// Visits all nested things, including item-likes.
177 /// **This is an unusual choice.** It is used when you want to
178 /// process everything within their lexical context. Typically you
179 /// kick off the visit by doing `walk_krate()`.
183 impl<M> NestedVisitorMap<M> {
184 /// Returns the map to use for an "intra item-like" thing (if any).
185 /// E.g., function body.
186 fn intra(self) -> Option<M> {
188 NestedVisitorMap::None => None,
189 NestedVisitorMap::OnlyBodies(map) => Some(map),
190 NestedVisitorMap::All(map) => Some(map),
194 /// Returns the map to use for an "item-like" thing (if any).
195 /// E.g., item, impl-item.
196 fn inter(self) -> Option<M> {
198 NestedVisitorMap::None => None,
199 NestedVisitorMap::OnlyBodies(_) => None,
200 NestedVisitorMap::All(map) => Some(map),
205 /// Each method of the Visitor trait is a hook to be potentially
206 /// overridden. Each method's default implementation recursively visits
207 /// the substructure of the input via the corresponding `walk` method;
208 /// e.g., the `visit_mod` method by default calls `intravisit::walk_mod`.
210 /// Note that this visitor does NOT visit nested items by default
211 /// (this is why the module is called `intravisit`, to distinguish it
212 /// from the AST's `visit` module, which acts differently). If you
213 /// simply want to visit all items in the crate in some order, you
214 /// should call `Crate::visit_all_items`. Otherwise, see the comment
215 /// on `visit_nested_item` for details on how to visit nested items.
217 /// If you want to ensure that your code handles every variant
218 /// explicitly, you need to override each method. (And you also need
219 /// to monitor future changes to `Visitor` in case a new method with a
220 /// new default implementation gets introduced.)
221 pub trait Visitor<'v>: Sized {
224 ///////////////////////////////////////////////////////////////////////////
227 /// The default versions of the `visit_nested_XXX` routines invoke
228 /// this method to get a map to use. By selecting an enum variant,
229 /// you control which kinds of nested HIR are visited; see
230 /// `NestedVisitorMap` for details. By "nested HIR", we are
231 /// referring to bits of HIR that are not directly embedded within
232 /// one another but rather indirectly, through a table in the
233 /// crate. This is done to control dependencies during incremental
234 /// compilation: the non-inline bits of HIR can be tracked and
235 /// hashed separately.
237 /// **If for some reason you want the nested behavior, but don't
238 /// have a `Map` at your disposal:** then you should override the
239 /// `visit_nested_XXX` methods, and override this method to
240 /// `panic!()`. This way, if a new `visit_nested_XXX` variant is
241 /// added in the future, we will see the panic in your code and
242 /// fix it appropriately.
243 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map>;
245 /// Invoked when a nested item is encountered. By default does
246 /// nothing unless you override `nested_visit_map` to return other than
247 /// `None`, in which case it will walk the item. **You probably
248 /// don't want to override this method** -- instead, override
249 /// `nested_visit_map` or use the "shallow" or "deep" visit
250 /// patterns described on `itemlikevisit::ItemLikeVisitor`. The only
251 /// reason to override this method is if you want a nested pattern
252 /// but cannot supply a `Map`; see `nested_visit_map` for advice.
253 #[allow(unused_variables)]
254 fn visit_nested_item(&mut self, id: ItemId) {
255 let opt_item = self.nested_visit_map().inter().map(|map| map.item(id.id));
256 walk_list!(self, visit_item, opt_item);
259 /// Like `visit_nested_item()`, but for trait items. See
260 /// `visit_nested_item()` for advice on when to override this
262 #[allow(unused_variables)]
263 fn visit_nested_trait_item(&mut self, id: TraitItemId) {
264 let opt_item = self.nested_visit_map().inter().map(|map| map.trait_item(id));
265 walk_list!(self, visit_trait_item, opt_item);
268 /// Like `visit_nested_item()`, but for impl items. See
269 /// `visit_nested_item()` for advice on when to override this
271 #[allow(unused_variables)]
272 fn visit_nested_impl_item(&mut self, id: ImplItemId) {
273 let opt_item = self.nested_visit_map().inter().map(|map| map.impl_item(id));
274 walk_list!(self, visit_impl_item, opt_item);
277 /// Invoked to visit the body of a function, method or closure. Like
278 /// visit_nested_item, does nothing by default unless you override
279 /// `nested_visit_map` to return other than `None`, in which case it will walk
281 fn visit_nested_body(&mut self, id: BodyId) {
282 let opt_body = self.nested_visit_map().intra().map(|map| map.body(id));
283 walk_list!(self, visit_body, opt_body);
286 fn visit_param(&mut self, param: &'v Param<'v>) {
287 walk_param(self, param)
290 /// Visits the top-level item and (optionally) nested items / impl items. See
291 /// `visit_nested_item` for details.
292 fn visit_item(&mut self, i: &'v Item<'v>) {
296 fn visit_body(&mut self, b: &'v Body<'v>) {
300 /// When invoking `visit_all_item_likes()`, you need to supply an
301 /// item-like visitor. This method converts a "intra-visit"
302 /// visitor into an item-like visitor that walks the entire tree.
303 /// If you use this, you probably don't want to process the
304 /// contents of nested item-like things, since the outer loop will
305 /// visit them as well.
306 fn as_deep_visitor(&mut self) -> DeepVisitor<'_, Self> {
307 DeepVisitor::new(self)
310 ///////////////////////////////////////////////////////////////////////////
312 fn visit_id(&mut self, _hir_id: HirId) {
315 fn visit_name(&mut self, _span: Span, _name: Name) {
318 fn visit_ident(&mut self, ident: Ident) {
319 walk_ident(self, ident)
321 fn visit_mod(&mut self, m: &'v Mod<'v>, _s: Span, n: HirId) {
324 fn visit_foreign_item(&mut self, i: &'v ForeignItem<'v>) {
325 walk_foreign_item(self, i)
327 fn visit_local(&mut self, l: &'v Local<'v>) {
330 fn visit_block(&mut self, b: &'v Block<'v>) {
333 fn visit_stmt(&mut self, s: &'v Stmt<'v>) {
336 fn visit_arm(&mut self, a: &'v Arm<'v>) {
339 fn visit_pat(&mut self, p: &'v Pat<'v>) {
342 fn visit_anon_const(&mut self, c: &'v AnonConst) {
343 walk_anon_const(self, c)
345 fn visit_expr(&mut self, ex: &'v Expr<'v>) {
348 fn visit_ty(&mut self, t: &'v Ty<'v>) {
351 fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) {
352 walk_generic_param(self, p)
354 fn visit_generics(&mut self, g: &'v Generics<'v>) {
355 walk_generics(self, g)
357 fn visit_where_predicate(&mut self, predicate: &'v WherePredicate<'v>) {
358 walk_where_predicate(self, predicate)
360 fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) {
361 walk_fn_decl(self, fd)
363 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, s: Span, id: HirId) {
364 walk_fn(self, fk, fd, b, s, id)
366 fn visit_use(&mut self, path: &'v Path<'v>, hir_id: HirId) {
367 walk_use(self, path, hir_id)
369 fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) {
370 walk_trait_item(self, ti)
372 fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
373 walk_trait_item_ref(self, ii)
375 fn visit_impl_item(&mut self, ii: &'v ImplItem<'v>) {
376 walk_impl_item(self, ii)
378 fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef<'v>) {
379 walk_impl_item_ref(self, ii)
381 fn visit_trait_ref(&mut self, t: &'v TraitRef<'v>) {
382 walk_trait_ref(self, t)
384 fn visit_param_bound(&mut self, bounds: &'v GenericBound<'v>) {
385 walk_param_bound(self, bounds)
387 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>, m: TraitBoundModifier) {
388 walk_poly_trait_ref(self, t, m)
390 fn visit_variant_data(
392 s: &'v VariantData<'v>,
398 walk_struct_def(self, s)
400 fn visit_struct_field(&mut self, s: &'v StructField<'v>) {
401 walk_struct_field(self, s)
405 enum_definition: &'v EnumDef<'v>,
406 generics: &'v Generics<'v>,
410 walk_enum_def(self, enum_definition, generics, item_id)
412 fn visit_variant(&mut self, v: &'v Variant<'v>, g: &'v Generics<'v>, item_id: HirId) {
413 walk_variant(self, v, g, item_id)
415 fn visit_label(&mut self, label: &'v Label) {
416 walk_label(self, label)
418 fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg<'v>) {
420 GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
421 GenericArg::Type(ty) => self.visit_ty(ty),
422 GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
425 fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
426 walk_lifetime(self, lifetime)
428 fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, span: Span) {
429 walk_qpath(self, qpath, id, span)
431 fn visit_path(&mut self, path: &'v Path<'v>, _id: HirId) {
432 walk_path(self, path)
434 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment<'v>) {
435 walk_path_segment(self, path_span, path_segment)
437 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs<'v>) {
438 walk_generic_args(self, path_span, generic_args)
440 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding<'v>) {
441 walk_assoc_type_binding(self, type_binding)
443 fn visit_attribute(&mut self, _attr: &'v Attribute) {}
444 fn visit_macro_def(&mut self, macro_def: &'v MacroDef<'v>) {
445 walk_macro_def(self, macro_def)
447 fn visit_vis(&mut self, vis: &'v Visibility<'v>) {
450 fn visit_associated_item_kind(&mut self, kind: &'v AssocItemKind) {
451 walk_associated_item_kind(self, kind);
453 fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
454 walk_defaultness(self, defaultness);
458 /// Walks the contents of a crate. See also `Crate::visit_all_items`.
459 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) {
460 visitor.visit_mod(&krate.item.module, krate.item.span, CRATE_HIR_ID);
461 walk_list!(visitor, visit_attribute, krate.item.attrs);
462 walk_list!(visitor, visit_macro_def, krate.exported_macros);
465 pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef<'v>) {
466 visitor.visit_id(macro_def.hir_id);
467 visitor.visit_ident(macro_def.ident);
468 walk_list!(visitor, visit_attribute, macro_def.attrs);
471 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod<'v>, mod_hir_id: HirId) {
472 visitor.visit_id(mod_hir_id);
473 for &item_id in module.item_ids {
474 visitor.visit_nested_item(item_id);
478 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body<'v>) {
479 walk_list!(visitor, visit_param, body.params);
480 visitor.visit_expr(&body.value);
483 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local<'v>) {
484 // Intentionally visiting the expr first - the initialization expr
485 // dominates the local's definition.
486 walk_list!(visitor, visit_expr, &local.init);
487 walk_list!(visitor, visit_attribute, local.attrs.iter());
488 visitor.visit_id(local.hir_id);
489 visitor.visit_pat(&local.pat);
490 walk_list!(visitor, visit_ty, &local.ty);
493 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
494 visitor.visit_name(ident.span, ident.name);
497 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
498 visitor.visit_ident(label.ident);
501 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
502 visitor.visit_id(lifetime.hir_id);
503 match lifetime.name {
504 LifetimeName::Param(ParamName::Plain(ident)) => {
505 visitor.visit_ident(ident);
507 LifetimeName::Param(ParamName::Fresh(_))
508 | LifetimeName::Param(ParamName::Error)
509 | LifetimeName::Static
510 | LifetimeName::Error
511 | LifetimeName::Implicit
512 | LifetimeName::ImplicitObjectLifetimeDefault
513 | LifetimeName::Underscore => {}
517 pub fn walk_poly_trait_ref<'v, V: Visitor<'v>>(
519 trait_ref: &'v PolyTraitRef<'v>,
520 _modifier: TraitBoundModifier,
522 walk_list!(visitor, visit_generic_param, trait_ref.bound_generic_params);
523 visitor.visit_trait_ref(&trait_ref.trait_ref);
526 pub fn walk_trait_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_ref: &'v TraitRef<'v>) {
527 visitor.visit_id(trait_ref.hir_ref_id);
528 visitor.visit_path(&trait_ref.path, trait_ref.hir_ref_id)
531 pub fn walk_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Param<'v>) {
532 visitor.visit_id(param.hir_id);
533 visitor.visit_pat(¶m.pat);
534 walk_list!(visitor, visit_attribute, param.attrs);
537 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
538 visitor.visit_vis(&item.vis);
539 visitor.visit_ident(item.ident);
541 ItemKind::ExternCrate(orig_name) => {
542 visitor.visit_id(item.hir_id);
543 if let Some(orig_name) = orig_name {
544 visitor.visit_name(item.span, orig_name);
547 ItemKind::Use(ref path, _) => {
548 visitor.visit_use(path, item.hir_id);
550 ItemKind::Static(ref typ, _, body) | ItemKind::Const(ref typ, body) => {
551 visitor.visit_id(item.hir_id);
552 visitor.visit_ty(typ);
553 visitor.visit_nested_body(body);
555 ItemKind::Fn(ref sig, ref generics, body_id) => visitor.visit_fn(
556 FnKind::ItemFn(item.ident, generics, sig.header, &item.vis, &item.attrs),
562 ItemKind::Mod(ref module) => {
563 // `visit_mod()` takes care of visiting the `Item`'s `HirId`.
564 visitor.visit_mod(module, item.span, item.hir_id)
566 ItemKind::ForeignMod(ref foreign_module) => {
567 visitor.visit_id(item.hir_id);
568 walk_list!(visitor, visit_foreign_item, foreign_module.items);
570 ItemKind::GlobalAsm(_) => {
571 visitor.visit_id(item.hir_id);
573 ItemKind::TyAlias(ref ty, ref generics) => {
574 visitor.visit_id(item.hir_id);
575 visitor.visit_ty(ty);
576 visitor.visit_generics(generics)
578 ItemKind::OpaqueTy(OpaqueTy { ref generics, bounds, .. }) => {
579 visitor.visit_id(item.hir_id);
580 walk_generics(visitor, generics);
581 walk_list!(visitor, visit_param_bound, bounds);
583 ItemKind::Enum(ref enum_definition, ref generics) => {
584 visitor.visit_generics(generics);
585 // `visit_enum_def()` takes care of visiting the `Item`'s `HirId`.
586 visitor.visit_enum_def(enum_definition, generics, item.hir_id, item.span)
598 visitor.visit_id(item.hir_id);
599 visitor.visit_generics(generics);
600 walk_list!(visitor, visit_trait_ref, of_trait);
601 visitor.visit_ty(self_ty);
602 walk_list!(visitor, visit_impl_item_ref, items);
604 ItemKind::Struct(ref struct_definition, ref generics)
605 | ItemKind::Union(ref struct_definition, ref generics) => {
606 visitor.visit_generics(generics);
607 visitor.visit_id(item.hir_id);
608 visitor.visit_variant_data(
616 ItemKind::Trait(.., ref generics, bounds, trait_item_refs) => {
617 visitor.visit_id(item.hir_id);
618 visitor.visit_generics(generics);
619 walk_list!(visitor, visit_param_bound, bounds);
620 walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
622 ItemKind::TraitAlias(ref generics, bounds) => {
623 visitor.visit_id(item.hir_id);
624 visitor.visit_generics(generics);
625 walk_list!(visitor, visit_param_bound, bounds);
628 walk_list!(visitor, visit_attribute, item.attrs);
631 pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>, hir_id: HirId) {
632 visitor.visit_id(hir_id);
633 visitor.visit_path(path, hir_id);
636 pub fn walk_enum_def<'v, V: Visitor<'v>>(
638 enum_definition: &'v EnumDef<'v>,
639 generics: &'v Generics<'v>,
642 visitor.visit_id(item_id);
643 walk_list!(visitor, visit_variant, enum_definition.variants, generics, item_id);
646 pub fn walk_variant<'v, V: Visitor<'v>>(
648 variant: &'v Variant<'v>,
649 generics: &'v Generics<'v>,
650 parent_item_id: HirId,
652 visitor.visit_ident(variant.ident);
653 visitor.visit_id(variant.id);
654 visitor.visit_variant_data(
661 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
662 walk_list!(visitor, visit_attribute, variant.attrs);
665 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
666 visitor.visit_id(typ.hir_id);
669 TyKind::Slice(ref ty) => visitor.visit_ty(ty),
670 TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
671 TyKind::Rptr(ref lifetime, ref mutable_type) => {
672 visitor.visit_lifetime(lifetime);
673 visitor.visit_ty(&mutable_type.ty)
676 TyKind::Tup(tuple_element_types) => {
677 walk_list!(visitor, visit_ty, tuple_element_types);
679 TyKind::BareFn(ref function_declaration) => {
680 walk_list!(visitor, visit_generic_param, function_declaration.generic_params);
681 visitor.visit_fn_decl(&function_declaration.decl);
683 TyKind::Path(ref qpath) => {
684 visitor.visit_qpath(qpath, typ.hir_id, typ.span);
686 TyKind::Def(item_id, lifetimes) => {
687 visitor.visit_nested_item(item_id);
688 walk_list!(visitor, visit_generic_arg, lifetimes);
690 TyKind::Array(ref ty, ref length) => {
691 visitor.visit_ty(ty);
692 visitor.visit_anon_const(length)
694 TyKind::TraitObject(bounds, ref lifetime) => {
695 for bound in bounds {
696 visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
698 visitor.visit_lifetime(lifetime);
700 TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
701 TyKind::Infer | TyKind::Err => {}
705 pub fn walk_qpath<'v, V: Visitor<'v>>(
707 qpath: &'v QPath<'v>,
712 QPath::Resolved(ref maybe_qself, ref path) => {
713 walk_list!(visitor, visit_ty, maybe_qself);
714 visitor.visit_path(path, id)
716 QPath::TypeRelative(ref qself, ref segment) => {
717 visitor.visit_ty(qself);
718 visitor.visit_path_segment(span, segment);
723 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>) {
724 for segment in path.segments {
725 visitor.visit_path_segment(path.span, segment);
729 pub fn walk_path_segment<'v, V: Visitor<'v>>(
732 segment: &'v PathSegment<'v>,
734 visitor.visit_ident(segment.ident);
735 walk_list!(visitor, visit_id, segment.hir_id);
736 if let Some(ref args) = segment.args {
737 visitor.visit_generic_args(path_span, args);
741 pub fn walk_generic_args<'v, V: Visitor<'v>>(
744 generic_args: &'v GenericArgs<'v>,
746 walk_list!(visitor, visit_generic_arg, generic_args.args);
747 walk_list!(visitor, visit_assoc_type_binding, generic_args.bindings);
750 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(
752 type_binding: &'v TypeBinding<'v>,
754 visitor.visit_id(type_binding.hir_id);
755 visitor.visit_ident(type_binding.ident);
756 match type_binding.kind {
757 TypeBindingKind::Equality { ref ty } => {
758 visitor.visit_ty(ty);
760 TypeBindingKind::Constraint { bounds } => {
761 walk_list!(visitor, visit_param_bound, bounds);
766 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) {
767 visitor.visit_id(pattern.hir_id);
769 PatKind::TupleStruct(ref qpath, children, _) => {
770 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
771 walk_list!(visitor, visit_pat, children);
773 PatKind::Path(ref qpath) => {
774 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
776 PatKind::Struct(ref qpath, fields, _) => {
777 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
778 for field in fields {
779 visitor.visit_id(field.hir_id);
780 visitor.visit_ident(field.ident);
781 visitor.visit_pat(&field.pat)
784 PatKind::Or(pats) => walk_list!(visitor, visit_pat, pats),
785 PatKind::Tuple(tuple_elements, _) => {
786 walk_list!(visitor, visit_pat, tuple_elements);
788 PatKind::Box(ref subpattern) | PatKind::Ref(ref subpattern, _) => {
789 visitor.visit_pat(subpattern)
791 PatKind::Binding(_, _hir_id, ident, ref optional_subpattern) => {
792 visitor.visit_ident(ident);
793 walk_list!(visitor, visit_pat, optional_subpattern);
795 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
796 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
797 walk_list!(visitor, visit_expr, lower_bound);
798 walk_list!(visitor, visit_expr, upper_bound);
801 PatKind::Slice(prepatterns, ref slice_pattern, postpatterns) => {
802 walk_list!(visitor, visit_pat, prepatterns);
803 walk_list!(visitor, visit_pat, slice_pattern);
804 walk_list!(visitor, visit_pat, postpatterns);
809 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem<'v>) {
810 visitor.visit_id(foreign_item.hir_id);
811 visitor.visit_vis(&foreign_item.vis);
812 visitor.visit_ident(foreign_item.ident);
814 match foreign_item.kind {
815 ForeignItemKind::Fn(ref function_declaration, param_names, ref generics) => {
816 visitor.visit_generics(generics);
817 visitor.visit_fn_decl(function_declaration);
818 for ¶m_name in param_names {
819 visitor.visit_ident(param_name);
822 ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
823 ForeignItemKind::Type => (),
826 walk_list!(visitor, visit_attribute, foreign_item.attrs);
829 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound<'v>) {
831 GenericBound::Trait(ref typ, modifier) => {
832 visitor.visit_poly_trait_ref(typ, modifier);
834 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
838 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam<'v>) {
839 visitor.visit_id(param.hir_id);
840 walk_list!(visitor, visit_attribute, param.attrs);
842 ParamName::Plain(ident) => visitor.visit_ident(ident),
843 ParamName::Error | ParamName::Fresh(_) => {}
846 GenericParamKind::Lifetime { .. } => {}
847 GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
848 GenericParamKind::Const { ref ty } => visitor.visit_ty(ty),
850 walk_list!(visitor, visit_param_bound, param.bounds);
853 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) {
854 walk_list!(visitor, visit_generic_param, generics.params);
855 walk_list!(visitor, visit_where_predicate, generics.where_clause.predicates);
858 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
860 predicate: &'v WherePredicate<'v>,
863 &WherePredicate::BoundPredicate(WhereBoundPredicate {
866 bound_generic_params,
869 visitor.visit_ty(bounded_ty);
870 walk_list!(visitor, visit_param_bound, bounds);
871 walk_list!(visitor, visit_generic_param, bound_generic_params);
873 &WherePredicate::RegionPredicate(WhereRegionPredicate { ref lifetime, bounds, .. }) => {
874 visitor.visit_lifetime(lifetime);
875 walk_list!(visitor, visit_param_bound, bounds);
877 &WherePredicate::EqPredicate(WhereEqPredicate {
878 hir_id, ref lhs_ty, ref rhs_ty, ..
880 visitor.visit_id(hir_id);
881 visitor.visit_ty(lhs_ty);
882 visitor.visit_ty(rhs_ty);
887 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FnRetTy<'v>) {
888 if let FnRetTy::Return(ref output_ty) = *ret_ty {
889 visitor.visit_ty(output_ty)
893 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl<'v>) {
894 for ty in function_declaration.inputs {
897 walk_fn_ret_ty(visitor, &function_declaration.output)
900 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
901 match function_kind {
902 FnKind::ItemFn(_, generics, ..) => {
903 visitor.visit_generics(generics);
905 FnKind::Method(..) | FnKind::Closure(_) => {}
909 pub fn walk_fn<'v, V: Visitor<'v>>(
911 function_kind: FnKind<'v>,
912 function_declaration: &'v FnDecl<'v>,
917 visitor.visit_id(id);
918 visitor.visit_fn_decl(function_declaration);
919 walk_fn_kind(visitor, function_kind);
920 visitor.visit_nested_body(body_id)
923 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem<'v>) {
924 visitor.visit_ident(trait_item.ident);
925 walk_list!(visitor, visit_attribute, trait_item.attrs);
926 visitor.visit_generics(&trait_item.generics);
927 match trait_item.kind {
928 TraitItemKind::Const(ref ty, default) => {
929 visitor.visit_id(trait_item.hir_id);
930 visitor.visit_ty(ty);
931 walk_list!(visitor, visit_nested_body, default);
933 TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => {
934 visitor.visit_id(trait_item.hir_id);
935 visitor.visit_fn_decl(&sig.decl);
936 for ¶m_name in param_names {
937 visitor.visit_ident(param_name);
940 TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => {
942 FnKind::Method(trait_item.ident, sig, None, &trait_item.attrs),
949 TraitItemKind::Type(bounds, ref default) => {
950 visitor.visit_id(trait_item.hir_id);
951 walk_list!(visitor, visit_param_bound, bounds);
952 walk_list!(visitor, visit_ty, default);
957 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
958 // N.B., deliberately force a compilation error if/when new fields are added.
959 let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
960 visitor.visit_nested_trait_item(id);
961 visitor.visit_ident(ident);
962 visitor.visit_associated_item_kind(kind);
963 visitor.visit_defaultness(defaultness);
966 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem<'v>) {
967 // N.B., deliberately force a compilation error if/when new fields are added.
979 visitor.visit_ident(ident);
980 visitor.visit_vis(vis);
981 visitor.visit_defaultness(defaultness);
982 walk_list!(visitor, visit_attribute, attrs);
983 visitor.visit_generics(generics);
985 ImplItemKind::Const(ref ty, body) => {
986 visitor.visit_id(impl_item.hir_id);
987 visitor.visit_ty(ty);
988 visitor.visit_nested_body(body);
990 ImplItemKind::Fn(ref sig, body_id) => {
992 FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis), &impl_item.attrs),
999 ImplItemKind::TyAlias(ref ty) => {
1000 visitor.visit_id(impl_item.hir_id);
1001 visitor.visit_ty(ty);
1003 ImplItemKind::OpaqueTy(bounds) => {
1004 visitor.visit_id(impl_item.hir_id);
1005 walk_list!(visitor, visit_param_bound, bounds);
1010 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef<'v>) {
1011 // N.B., deliberately force a compilation error if/when new fields are added.
1012 let ImplItemRef { id, ident, ref kind, span: _, ref vis, ref defaultness } = *impl_item_ref;
1013 visitor.visit_nested_impl_item(id);
1014 visitor.visit_ident(ident);
1015 visitor.visit_associated_item_kind(kind);
1016 visitor.visit_vis(vis);
1017 visitor.visit_defaultness(defaultness);
1020 pub fn walk_struct_def<'v, V: Visitor<'v>>(
1022 struct_definition: &'v VariantData<'v>,
1024 walk_list!(visitor, visit_id, struct_definition.ctor_hir_id());
1025 walk_list!(visitor, visit_struct_field, struct_definition.fields());
1028 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField<'v>) {
1029 visitor.visit_id(struct_field.hir_id);
1030 visitor.visit_vis(&struct_field.vis);
1031 visitor.visit_ident(struct_field.ident);
1032 visitor.visit_ty(&struct_field.ty);
1033 walk_list!(visitor, visit_attribute, struct_field.attrs);
1036 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block<'v>) {
1037 visitor.visit_id(block.hir_id);
1038 walk_list!(visitor, visit_stmt, block.stmts);
1039 walk_list!(visitor, visit_expr, &block.expr);
1042 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt<'v>) {
1043 visitor.visit_id(statement.hir_id);
1044 match statement.kind {
1045 StmtKind::Local(ref local) => visitor.visit_local(local),
1046 StmtKind::Item(item) => visitor.visit_nested_item(item),
1047 StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
1048 visitor.visit_expr(expression)
1053 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
1054 visitor.visit_id(constant.hir_id);
1055 visitor.visit_nested_body(constant.body);
1058 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) {
1059 visitor.visit_id(expression.hir_id);
1060 walk_list!(visitor, visit_attribute, expression.attrs.iter());
1061 match expression.kind {
1062 ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
1063 ExprKind::Array(subexpressions) => {
1064 walk_list!(visitor, visit_expr, subexpressions);
1066 ExprKind::Repeat(ref element, ref count) => {
1067 visitor.visit_expr(element);
1068 visitor.visit_anon_const(count)
1070 ExprKind::Struct(ref qpath, fields, ref optional_base) => {
1071 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1072 for field in fields {
1073 visitor.visit_id(field.hir_id);
1074 visitor.visit_ident(field.ident);
1075 visitor.visit_expr(&field.expr)
1077 walk_list!(visitor, visit_expr, optional_base);
1079 ExprKind::Tup(subexpressions) => {
1080 walk_list!(visitor, visit_expr, subexpressions);
1082 ExprKind::Call(ref callee_expression, arguments) => {
1083 visitor.visit_expr(callee_expression);
1084 walk_list!(visitor, visit_expr, arguments);
1086 ExprKind::MethodCall(ref segment, _, arguments) => {
1087 visitor.visit_path_segment(expression.span, segment);
1088 walk_list!(visitor, visit_expr, arguments);
1090 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
1091 visitor.visit_expr(left_expression);
1092 visitor.visit_expr(right_expression)
1094 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
1095 visitor.visit_expr(subexpression)
1097 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
1098 visitor.visit_expr(subexpression);
1099 visitor.visit_ty(typ)
1101 ExprKind::DropTemps(ref subexpression) => {
1102 visitor.visit_expr(subexpression);
1104 ExprKind::Loop(ref block, ref opt_label, _) => {
1105 walk_list!(visitor, visit_label, opt_label);
1106 visitor.visit_block(block);
1108 ExprKind::Match(ref subexpression, arms, _) => {
1109 visitor.visit_expr(subexpression);
1110 walk_list!(visitor, visit_arm, arms);
1112 ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => visitor
1114 FnKind::Closure(&expression.attrs),
1115 function_declaration,
1120 ExprKind::Block(ref block, ref opt_label) => {
1121 walk_list!(visitor, visit_label, opt_label);
1122 visitor.visit_block(block);
1124 ExprKind::Assign(ref lhs, ref rhs, _) => {
1125 visitor.visit_expr(rhs);
1126 visitor.visit_expr(lhs)
1128 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
1129 visitor.visit_expr(right_expression);
1130 visitor.visit_expr(left_expression);
1132 ExprKind::Field(ref subexpression, ident) => {
1133 visitor.visit_expr(subexpression);
1134 visitor.visit_ident(ident);
1136 ExprKind::Index(ref main_expression, ref index_expression) => {
1137 visitor.visit_expr(main_expression);
1138 visitor.visit_expr(index_expression)
1140 ExprKind::Path(ref qpath) => {
1141 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1143 ExprKind::Break(ref destination, ref opt_expr) => {
1144 walk_list!(visitor, visit_label, &destination.label);
1145 walk_list!(visitor, visit_expr, opt_expr);
1147 ExprKind::Continue(ref destination) => {
1148 walk_list!(visitor, visit_label, &destination.label);
1150 ExprKind::Ret(ref optional_expression) => {
1151 walk_list!(visitor, visit_expr, optional_expression);
1153 ExprKind::InlineAsm(ref asm) => {
1154 walk_list!(visitor, visit_expr, asm.outputs_exprs);
1155 walk_list!(visitor, visit_expr, asm.inputs_exprs);
1157 ExprKind::Yield(ref subexpression, _) => {
1158 visitor.visit_expr(subexpression);
1160 ExprKind::Lit(_) | ExprKind::Err => {}
1164 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm<'v>) {
1165 visitor.visit_id(arm.hir_id);
1166 visitor.visit_pat(&arm.pat);
1167 if let Some(ref g) = arm.guard {
1169 Guard::If(ref e) => visitor.visit_expr(e),
1172 visitor.visit_expr(&arm.body);
1173 walk_list!(visitor, visit_attribute, arm.attrs);
1176 pub fn walk_vis<'v, V: Visitor<'v>>(visitor: &mut V, vis: &'v Visibility<'v>) {
1177 if let VisibilityKind::Restricted { ref path, hir_id } = vis.node {
1178 visitor.visit_id(hir_id);
1179 visitor.visit_path(path, hir_id)
1183 pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssocItemKind) {
1184 // No visitable content here: this fn exists so you can call it if
1185 // the right thing to do, should content be added in the future,
1186 // would be to walk it.
1189 pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) {
1190 // No visitable content here: this fn exists so you can call it if
1191 // the right thing to do, should content be added in the future,
1192 // would be to walk it.