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::itemlikevisit::{ItemLikeVisitor, ParItemLikeVisitor};
36 use rustc_ast::walk_list;
37 use rustc_ast::{Attribute, Label};
38 use rustc_span::symbol::{Ident, Symbol};
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);
67 fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem<'hir>) {
68 self.visitor.visit_foreign_item(foreign_item);
72 pub trait IntoVisitor<'hir> {
73 type Visitor: Visitor<'hir>;
74 fn into_visitor(&self) -> Self::Visitor;
77 pub struct ParDeepVisitor<V>(pub V);
79 impl<'hir, V> ParItemLikeVisitor<'hir> for ParDeepVisitor<V>
83 fn visit_item(&self, item: &'hir Item<'hir>) {
84 self.0.into_visitor().visit_item(item);
87 fn visit_trait_item(&self, trait_item: &'hir TraitItem<'hir>) {
88 self.0.into_visitor().visit_trait_item(trait_item);
91 fn visit_impl_item(&self, impl_item: &'hir ImplItem<'hir>) {
92 self.0.into_visitor().visit_impl_item(impl_item);
95 fn visit_foreign_item(&self, foreign_item: &'hir ForeignItem<'hir>) {
96 self.0.into_visitor().visit_foreign_item(foreign_item);
100 #[derive(Copy, Clone, Debug)]
101 pub enum FnKind<'a> {
102 /// `#[xxx] pub async/const/extern "Abi" fn foo()`
103 ItemFn(Ident, &'a Generics<'a>, FnHeader, &'a Visibility<'a>),
106 Method(Ident, &'a FnSig<'a>, Option<&'a Visibility<'a>>),
112 impl<'a> FnKind<'a> {
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_middle::hir::map::Map`.
123 pub trait Map<'hir> {
124 /// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
125 fn find(&self, hir_id: HirId) -> Option<Node<'hir>>;
126 fn body(&self, id: BodyId) -> &'hir Body<'hir>;
127 fn item(&self, id: ItemId) -> &'hir Item<'hir>;
128 fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>;
129 fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>;
130 fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>;
133 // Used when no map is actually available, forcing manual implementation of nested visitors.
134 impl Map<'hir> for ! {
135 fn find(&self, _: HirId) -> Option<Node<'hir>> {
138 fn body(&self, _: BodyId) -> &'hir Body<'hir> {
141 fn item(&self, _: ItemId) -> &'hir Item<'hir> {
144 fn trait_item(&self, _: TraitItemId) -> &'hir TraitItem<'hir> {
147 fn impl_item(&self, _: ImplItemId) -> &'hir ImplItem<'hir> {
150 fn foreign_item(&self, _: ForeignItemId) -> &'hir ForeignItem<'hir> {
155 /// An erased version of `Map<'hir>`, using dynamic dispatch.
156 /// NOTE: This type is effectively only usable with `NestedVisitorMap::None`.
157 pub struct ErasedMap<'hir>(&'hir dyn Map<'hir>);
159 impl<'hir> Map<'hir> for ErasedMap<'hir> {
160 fn find(&self, _: HirId) -> Option<Node<'hir>> {
163 fn body(&self, id: BodyId) -> &'hir Body<'hir> {
166 fn item(&self, id: ItemId) -> &'hir Item<'hir> {
169 fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> {
170 self.0.trait_item(id)
172 fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
175 fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> {
176 self.0.foreign_item(id)
180 /// Specifies what nested things a visitor wants to visit. The most
181 /// common choice is `OnlyBodies`, which will cause the visitor to
182 /// visit fn bodies for fns that it encounters, but skip over nested
183 /// item-like things.
185 /// See the comments on `ItemLikeVisitor` for more details on the overall
187 pub enum NestedVisitorMap<M> {
188 /// Do not visit any nested things. When you add a new
189 /// "non-nested" thing, you will want to audit such uses to see if
190 /// they remain valid.
192 /// Use this if you are only walking some particular kind of tree
193 /// (i.e., a type, or fn signature) and you don't want to thread a
197 /// Do not visit nested item-like things, but visit nested things
198 /// that are inside of an item-like.
200 /// **This is the most common choice.** A very common pattern is
201 /// to use `visit_all_item_likes()` as an outer loop,
202 /// and to have the visitor that visits the contents of each item
203 /// using this setting.
206 /// Visits all nested things, including item-likes.
208 /// **This is an unusual choice.** It is used when you want to
209 /// process everything within their lexical context. Typically you
210 /// kick off the visit by doing `walk_krate()`.
214 impl<M> NestedVisitorMap<M> {
215 /// Returns the map to use for an "intra item-like" thing (if any).
216 /// E.g., function body.
217 fn intra(self) -> Option<M> {
219 NestedVisitorMap::None => None,
220 NestedVisitorMap::OnlyBodies(map) => Some(map),
221 NestedVisitorMap::All(map) => Some(map),
225 /// Returns the map to use for an "item-like" thing (if any).
226 /// E.g., item, impl-item.
227 fn inter(self) -> Option<M> {
229 NestedVisitorMap::None => None,
230 NestedVisitorMap::OnlyBodies(_) => None,
231 NestedVisitorMap::All(map) => Some(map),
236 /// Each method of the Visitor trait is a hook to be potentially
237 /// overridden. Each method's default implementation recursively visits
238 /// the substructure of the input via the corresponding `walk` method;
239 /// e.g., the `visit_mod` method by default calls `intravisit::walk_mod`.
241 /// Note that this visitor does NOT visit nested items by default
242 /// (this is why the module is called `intravisit`, to distinguish it
243 /// from the AST's `visit` module, which acts differently). If you
244 /// simply want to visit all items in the crate in some order, you
245 /// should call `Crate::visit_all_items`. Otherwise, see the comment
246 /// on `visit_nested_item` for details on how to visit nested items.
248 /// If you want to ensure that your code handles every variant
249 /// explicitly, you need to override each method. (And you also need
250 /// to monitor future changes to `Visitor` in case a new method with a
251 /// new default implementation gets introduced.)
252 pub trait Visitor<'v>: Sized {
255 ///////////////////////////////////////////////////////////////////////////
258 /// The default versions of the `visit_nested_XXX` routines invoke
259 /// this method to get a map to use. By selecting an enum variant,
260 /// you control which kinds of nested HIR are visited; see
261 /// `NestedVisitorMap` for details. By "nested HIR", we are
262 /// referring to bits of HIR that are not directly embedded within
263 /// one another but rather indirectly, through a table in the
264 /// crate. This is done to control dependencies during incremental
265 /// compilation: the non-inline bits of HIR can be tracked and
266 /// hashed separately.
268 /// **If for some reason you want the nested behavior, but don't
269 /// have a `Map` at your disposal:** then you should override the
270 /// `visit_nested_XXX` methods, and override this method to
271 /// `panic!()`. This way, if a new `visit_nested_XXX` variant is
272 /// added in the future, we will see the panic in your code and
273 /// fix it appropriately.
274 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map>;
276 /// Invoked when a nested item is encountered. By default does
277 /// nothing unless you override `nested_visit_map` to return other than
278 /// `None`, in which case it will walk the item. **You probably
279 /// don't want to override this method** -- instead, override
280 /// `nested_visit_map` or use the "shallow" or "deep" visit
281 /// patterns described on `itemlikevisit::ItemLikeVisitor`. The only
282 /// reason to override this method is if you want a nested pattern
283 /// but cannot supply a `Map`; see `nested_visit_map` for advice.
284 fn visit_nested_item(&mut self, id: ItemId) {
285 let opt_item = self.nested_visit_map().inter().map(|map| map.item(id));
286 walk_list!(self, visit_item, opt_item);
289 /// Like `visit_nested_item()`, but for trait items. See
290 /// `visit_nested_item()` for advice on when to override this
292 fn visit_nested_trait_item(&mut self, id: TraitItemId) {
293 let opt_item = self.nested_visit_map().inter().map(|map| map.trait_item(id));
294 walk_list!(self, visit_trait_item, opt_item);
297 /// Like `visit_nested_item()`, but for impl items. See
298 /// `visit_nested_item()` for advice on when to override this
300 fn visit_nested_impl_item(&mut self, id: ImplItemId) {
301 let opt_item = self.nested_visit_map().inter().map(|map| map.impl_item(id));
302 walk_list!(self, visit_impl_item, opt_item);
305 /// Like `visit_nested_item()`, but for foreign items. See
306 /// `visit_nested_item()` for advice on when to override this
308 fn visit_nested_foreign_item(&mut self, id: ForeignItemId) {
309 let opt_item = self.nested_visit_map().inter().map(|map| map.foreign_item(id));
310 walk_list!(self, visit_foreign_item, opt_item);
313 /// Invoked to visit the body of a function, method or closure. Like
314 /// visit_nested_item, does nothing by default unless you override
315 /// `nested_visit_map` to return other than `None`, in which case it will walk
317 fn visit_nested_body(&mut self, id: BodyId) {
318 let opt_body = self.nested_visit_map().intra().map(|map| map.body(id));
319 walk_list!(self, visit_body, opt_body);
322 fn visit_param(&mut self, param: &'v Param<'v>) {
323 walk_param(self, param)
326 /// Visits the top-level item and (optionally) nested items / impl items. See
327 /// `visit_nested_item` for details.
328 fn visit_item(&mut self, i: &'v Item<'v>) {
332 fn visit_body(&mut self, b: &'v Body<'v>) {
336 /// When invoking `visit_all_item_likes()`, you need to supply an
337 /// item-like visitor. This method converts an "intra-visit"
338 /// visitor into an item-like visitor that walks the entire tree.
339 /// If you use this, you probably don't want to process the
340 /// contents of nested item-like things, since the outer loop will
341 /// visit them as well.
342 fn as_deep_visitor(&mut self) -> DeepVisitor<'_, Self> {
343 DeepVisitor::new(self)
346 ///////////////////////////////////////////////////////////////////////////
348 fn visit_id(&mut self, _hir_id: HirId) {
351 fn visit_name(&mut self, _span: Span, _name: Symbol) {
354 fn visit_ident(&mut self, ident: Ident) {
355 walk_ident(self, ident)
357 fn visit_mod(&mut self, m: &'v Mod<'v>, _s: Span, n: HirId) {
360 fn visit_foreign_item(&mut self, i: &'v ForeignItem<'v>) {
361 walk_foreign_item(self, i)
363 fn visit_local(&mut self, l: &'v Local<'v>) {
366 fn visit_block(&mut self, b: &'v Block<'v>) {
369 fn visit_stmt(&mut self, s: &'v Stmt<'v>) {
372 fn visit_arm(&mut self, a: &'v Arm<'v>) {
375 fn visit_pat(&mut self, p: &'v Pat<'v>) {
378 fn visit_anon_const(&mut self, c: &'v AnonConst) {
379 walk_anon_const(self, c)
381 fn visit_expr(&mut self, ex: &'v Expr<'v>) {
384 fn visit_ty(&mut self, t: &'v Ty<'v>) {
387 fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) {
388 walk_generic_param(self, p)
390 fn visit_const_param_default(&mut self, _param: HirId, ct: &'v AnonConst) {
391 walk_const_param_default(self, ct)
393 fn visit_generics(&mut self, g: &'v Generics<'v>) {
394 walk_generics(self, g)
396 fn visit_where_predicate(&mut self, predicate: &'v WherePredicate<'v>) {
397 walk_where_predicate(self, predicate)
399 fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) {
400 walk_fn_decl(self, fd)
402 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, s: Span, id: HirId) {
403 walk_fn(self, fk, fd, b, s, id)
405 fn visit_use(&mut self, path: &'v Path<'v>, hir_id: HirId) {
406 walk_use(self, path, hir_id)
408 fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) {
409 walk_trait_item(self, ti)
411 fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
412 walk_trait_item_ref(self, ii)
414 fn visit_impl_item(&mut self, ii: &'v ImplItem<'v>) {
415 walk_impl_item(self, ii)
417 fn visit_foreign_item_ref(&mut self, ii: &'v ForeignItemRef) {
418 walk_foreign_item_ref(self, ii)
420 fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) {
421 walk_impl_item_ref(self, ii)
423 fn visit_trait_ref(&mut self, t: &'v TraitRef<'v>) {
424 walk_trait_ref(self, t)
426 fn visit_param_bound(&mut self, bounds: &'v GenericBound<'v>) {
427 walk_param_bound(self, bounds)
429 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>, m: TraitBoundModifier) {
430 walk_poly_trait_ref(self, t, m)
432 fn visit_variant_data(
434 s: &'v VariantData<'v>,
440 walk_struct_def(self, s)
442 fn visit_field_def(&mut self, s: &'v FieldDef<'v>) {
443 walk_field_def(self, s)
447 enum_definition: &'v EnumDef<'v>,
448 generics: &'v Generics<'v>,
452 walk_enum_def(self, enum_definition, generics, item_id)
454 fn visit_variant(&mut self, v: &'v Variant<'v>, g: &'v Generics<'v>, item_id: HirId) {
455 walk_variant(self, v, g, item_id)
457 fn visit_label(&mut self, label: &'v Label) {
458 walk_label(self, label)
460 fn visit_infer(&mut self, inf: &'v InferArg) {
463 fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg<'v>) {
465 GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
466 GenericArg::Type(ty) => self.visit_ty(ty),
467 GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
468 GenericArg::Infer(inf) => self.visit_infer(inf),
471 fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
472 walk_lifetime(self, lifetime)
474 fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, span: Span) {
475 walk_qpath(self, qpath, id, span)
477 fn visit_path(&mut self, path: &'v Path<'v>, _id: HirId) {
478 walk_path(self, path)
480 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment<'v>) {
481 walk_path_segment(self, path_span, path_segment)
483 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs<'v>) {
484 walk_generic_args(self, path_span, generic_args)
486 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding<'v>) {
487 walk_assoc_type_binding(self, type_binding)
489 fn visit_attribute(&mut self, _id: HirId, _attr: &'v Attribute) {}
490 fn visit_vis(&mut self, vis: &'v Visibility<'v>) {
493 fn visit_associated_item_kind(&mut self, kind: &'v AssocItemKind) {
494 walk_associated_item_kind(self, kind);
496 fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
497 walk_defaultness(self, defaultness);
501 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod<'v>, mod_hir_id: HirId) {
502 visitor.visit_id(mod_hir_id);
503 for &item_id in module.item_ids {
504 visitor.visit_nested_item(item_id);
508 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body<'v>) {
509 walk_list!(visitor, visit_param, body.params);
510 visitor.visit_expr(&body.value);
513 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local<'v>) {
514 // Intentionally visiting the expr first - the initialization expr
515 // dominates the local's definition.
516 walk_list!(visitor, visit_expr, &local.init);
517 visitor.visit_id(local.hir_id);
518 visitor.visit_pat(&local.pat);
519 walk_list!(visitor, visit_ty, &local.ty);
522 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
523 visitor.visit_name(ident.span, ident.name);
526 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
527 visitor.visit_ident(label.ident);
530 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
531 visitor.visit_id(lifetime.hir_id);
532 match lifetime.name {
533 LifetimeName::Param(ParamName::Plain(ident)) => {
534 visitor.visit_ident(ident);
536 LifetimeName::Param(ParamName::Fresh(_))
537 | LifetimeName::Param(ParamName::Error)
538 | LifetimeName::Static
539 | LifetimeName::Error
540 | LifetimeName::Implicit
541 | LifetimeName::ImplicitObjectLifetimeDefault
542 | LifetimeName::Underscore => {}
546 pub fn walk_poly_trait_ref<'v, V: Visitor<'v>>(
548 trait_ref: &'v PolyTraitRef<'v>,
549 _modifier: TraitBoundModifier,
551 walk_list!(visitor, visit_generic_param, trait_ref.bound_generic_params);
552 visitor.visit_trait_ref(&trait_ref.trait_ref);
555 pub fn walk_trait_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_ref: &'v TraitRef<'v>) {
556 visitor.visit_id(trait_ref.hir_ref_id);
557 visitor.visit_path(&trait_ref.path, trait_ref.hir_ref_id)
560 pub fn walk_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Param<'v>) {
561 visitor.visit_id(param.hir_id);
562 visitor.visit_pat(¶m.pat);
565 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
566 visitor.visit_vis(&item.vis);
567 visitor.visit_ident(item.ident);
569 ItemKind::ExternCrate(orig_name) => {
570 visitor.visit_id(item.hir_id());
571 if let Some(orig_name) = orig_name {
572 visitor.visit_name(item.span, orig_name);
575 ItemKind::Use(ref path, _) => {
576 visitor.visit_use(path, item.hir_id());
578 ItemKind::Static(ref typ, _, body) | ItemKind::Const(ref typ, body) => {
579 visitor.visit_id(item.hir_id());
580 visitor.visit_ty(typ);
581 visitor.visit_nested_body(body);
583 ItemKind::Fn(ref sig, ref generics, body_id) => visitor.visit_fn(
584 FnKind::ItemFn(item.ident, generics, sig.header, &item.vis),
590 ItemKind::Macro(_) => {
591 visitor.visit_id(item.hir_id());
593 ItemKind::Mod(ref module) => {
594 // `visit_mod()` takes care of visiting the `Item`'s `HirId`.
595 visitor.visit_mod(module, item.span, item.hir_id())
597 ItemKind::ForeignMod { abi: _, items } => {
598 visitor.visit_id(item.hir_id());
599 walk_list!(visitor, visit_foreign_item_ref, items);
601 ItemKind::GlobalAsm(asm) => {
602 visitor.visit_id(item.hir_id());
603 walk_inline_asm(visitor, asm);
605 ItemKind::TyAlias(ref ty, ref generics) => {
606 visitor.visit_id(item.hir_id());
607 visitor.visit_ty(ty);
608 visitor.visit_generics(generics)
610 ItemKind::OpaqueTy(OpaqueTy { ref generics, bounds, .. }) => {
611 visitor.visit_id(item.hir_id());
612 walk_generics(visitor, generics);
613 walk_list!(visitor, visit_param_bound, bounds);
615 ItemKind::Enum(ref enum_definition, ref generics) => {
616 visitor.visit_generics(generics);
617 // `visit_enum_def()` takes care of visiting the `Item`'s `HirId`.
618 visitor.visit_enum_def(enum_definition, generics, item.hir_id(), item.span)
620 ItemKind::Impl(Impl {
631 visitor.visit_id(item.hir_id());
632 visitor.visit_generics(generics);
633 walk_list!(visitor, visit_trait_ref, of_trait);
634 visitor.visit_ty(self_ty);
635 walk_list!(visitor, visit_impl_item_ref, items);
637 ItemKind::Struct(ref struct_definition, ref generics)
638 | ItemKind::Union(ref struct_definition, ref generics) => {
639 visitor.visit_generics(generics);
640 visitor.visit_id(item.hir_id());
641 visitor.visit_variant_data(
649 ItemKind::Trait(.., ref generics, bounds, trait_item_refs) => {
650 visitor.visit_id(item.hir_id());
651 visitor.visit_generics(generics);
652 walk_list!(visitor, visit_param_bound, bounds);
653 walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
655 ItemKind::TraitAlias(ref generics, bounds) => {
656 visitor.visit_id(item.hir_id());
657 visitor.visit_generics(generics);
658 walk_list!(visitor, visit_param_bound, bounds);
663 fn walk_inline_asm<'v, V: Visitor<'v>>(visitor: &mut V, asm: &'v InlineAsm<'v>) {
664 for (op, _op_sp) in asm.operands {
666 InlineAsmOperand::In { expr, .. }
667 | InlineAsmOperand::InOut { expr, .. }
668 | InlineAsmOperand::Sym { expr, .. } => visitor.visit_expr(expr),
669 InlineAsmOperand::Out { expr, .. } => {
670 if let Some(expr) = expr {
671 visitor.visit_expr(expr);
674 InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
675 visitor.visit_expr(in_expr);
676 if let Some(out_expr) = out_expr {
677 visitor.visit_expr(out_expr);
680 InlineAsmOperand::Const { anon_const } => visitor.visit_anon_const(anon_const),
685 pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>, hir_id: HirId) {
686 visitor.visit_id(hir_id);
687 visitor.visit_path(path, hir_id);
690 pub fn walk_enum_def<'v, V: Visitor<'v>>(
692 enum_definition: &'v EnumDef<'v>,
693 generics: &'v Generics<'v>,
696 visitor.visit_id(item_id);
697 walk_list!(visitor, visit_variant, enum_definition.variants, generics, item_id);
700 pub fn walk_variant<'v, V: Visitor<'v>>(
702 variant: &'v Variant<'v>,
703 generics: &'v Generics<'v>,
704 parent_item_id: HirId,
706 visitor.visit_ident(variant.ident);
707 visitor.visit_id(variant.id);
708 visitor.visit_variant_data(
715 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
718 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
719 visitor.visit_id(typ.hir_id);
722 TyKind::Slice(ref ty) => visitor.visit_ty(ty),
723 TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
724 TyKind::Rptr(ref lifetime, ref mutable_type) => {
725 visitor.visit_lifetime(lifetime);
726 visitor.visit_ty(&mutable_type.ty)
729 TyKind::Tup(tuple_element_types) => {
730 walk_list!(visitor, visit_ty, tuple_element_types);
732 TyKind::BareFn(ref function_declaration) => {
733 walk_list!(visitor, visit_generic_param, function_declaration.generic_params);
734 visitor.visit_fn_decl(&function_declaration.decl);
736 TyKind::Path(ref qpath) => {
737 visitor.visit_qpath(qpath, typ.hir_id, typ.span);
739 TyKind::OpaqueDef(item_id, lifetimes) => {
740 visitor.visit_nested_item(item_id);
741 walk_list!(visitor, visit_generic_arg, lifetimes);
743 TyKind::Array(ref ty, ref length) => {
744 visitor.visit_ty(ty);
745 visitor.visit_anon_const(length)
747 TyKind::TraitObject(bounds, ref lifetime, _syntax) => {
748 for bound in bounds {
749 visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
751 visitor.visit_lifetime(lifetime);
753 TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
754 TyKind::Infer | TyKind::Err => {}
758 pub fn walk_inf<'v, V: Visitor<'v>>(visitor: &mut V, inf: &'v InferArg) {
759 visitor.visit_id(inf.hir_id);
762 pub fn walk_qpath<'v, V: Visitor<'v>>(
764 qpath: &'v QPath<'v>,
769 QPath::Resolved(ref maybe_qself, ref path) => {
770 walk_list!(visitor, visit_ty, maybe_qself);
771 visitor.visit_path(path, id)
773 QPath::TypeRelative(ref qself, ref segment) => {
774 visitor.visit_ty(qself);
775 visitor.visit_path_segment(span, segment);
777 QPath::LangItem(..) => {}
781 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>) {
782 for segment in path.segments {
783 visitor.visit_path_segment(path.span, segment);
787 pub fn walk_path_segment<'v, V: Visitor<'v>>(
790 segment: &'v PathSegment<'v>,
792 visitor.visit_ident(segment.ident);
793 walk_list!(visitor, visit_id, segment.hir_id);
794 if let Some(ref args) = segment.args {
795 visitor.visit_generic_args(path_span, args);
799 pub fn walk_generic_args<'v, V: Visitor<'v>>(
802 generic_args: &'v GenericArgs<'v>,
804 walk_list!(visitor, visit_generic_arg, generic_args.args);
805 walk_list!(visitor, visit_assoc_type_binding, generic_args.bindings);
808 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(
810 type_binding: &'v TypeBinding<'v>,
812 visitor.visit_id(type_binding.hir_id);
813 visitor.visit_ident(type_binding.ident);
814 visitor.visit_generic_args(type_binding.span, type_binding.gen_args);
815 match type_binding.kind {
816 TypeBindingKind::Equality { ref ty } => {
817 visitor.visit_ty(ty);
819 TypeBindingKind::Constraint { bounds } => {
820 walk_list!(visitor, visit_param_bound, bounds);
825 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) {
826 visitor.visit_id(pattern.hir_id);
828 PatKind::TupleStruct(ref qpath, children, _) => {
829 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
830 walk_list!(visitor, visit_pat, children);
832 PatKind::Path(ref qpath) => {
833 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
835 PatKind::Struct(ref qpath, fields, _) => {
836 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
837 for field in fields {
838 visitor.visit_id(field.hir_id);
839 visitor.visit_ident(field.ident);
840 visitor.visit_pat(&field.pat)
843 PatKind::Or(pats) => walk_list!(visitor, visit_pat, pats),
844 PatKind::Tuple(tuple_elements, _) => {
845 walk_list!(visitor, visit_pat, tuple_elements);
847 PatKind::Box(ref subpattern) | PatKind::Ref(ref subpattern, _) => {
848 visitor.visit_pat(subpattern)
850 PatKind::Binding(_, _hir_id, ident, ref optional_subpattern) => {
851 visitor.visit_ident(ident);
852 walk_list!(visitor, visit_pat, optional_subpattern);
854 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
855 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
856 walk_list!(visitor, visit_expr, lower_bound);
857 walk_list!(visitor, visit_expr, upper_bound);
860 PatKind::Slice(prepatterns, ref slice_pattern, postpatterns) => {
861 walk_list!(visitor, visit_pat, prepatterns);
862 walk_list!(visitor, visit_pat, slice_pattern);
863 walk_list!(visitor, visit_pat, postpatterns);
868 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem<'v>) {
869 visitor.visit_id(foreign_item.hir_id());
870 visitor.visit_vis(&foreign_item.vis);
871 visitor.visit_ident(foreign_item.ident);
873 match foreign_item.kind {
874 ForeignItemKind::Fn(ref function_declaration, param_names, ref generics) => {
875 visitor.visit_generics(generics);
876 visitor.visit_fn_decl(function_declaration);
877 for ¶m_name in param_names {
878 visitor.visit_ident(param_name);
881 ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
882 ForeignItemKind::Type => (),
886 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound<'v>) {
888 GenericBound::Trait(ref typ, modifier) => {
889 visitor.visit_poly_trait_ref(typ, modifier);
891 GenericBound::LangItemTrait(_, span, hir_id, args) => {
892 visitor.visit_id(hir_id);
893 visitor.visit_generic_args(span, args);
895 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
899 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam<'v>) {
900 visitor.visit_id(param.hir_id);
902 ParamName::Plain(ident) => visitor.visit_ident(ident),
903 ParamName::Error | ParamName::Fresh(_) => {}
906 GenericParamKind::Lifetime { .. } => {}
907 GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
908 GenericParamKind::Const { ref ty, ref default } => {
909 visitor.visit_ty(ty);
910 if let Some(ref default) = default {
911 visitor.visit_const_param_default(param.hir_id, default);
915 walk_list!(visitor, visit_param_bound, param.bounds);
918 pub fn walk_const_param_default<'v, V: Visitor<'v>>(visitor: &mut V, ct: &'v AnonConst) {
919 visitor.visit_anon_const(ct)
922 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) {
923 walk_list!(visitor, visit_generic_param, generics.params);
924 walk_list!(visitor, visit_where_predicate, generics.where_clause.predicates);
927 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
929 predicate: &'v WherePredicate<'v>,
932 WherePredicate::BoundPredicate(WhereBoundPredicate {
935 bound_generic_params,
938 visitor.visit_ty(bounded_ty);
939 walk_list!(visitor, visit_param_bound, bounds);
940 walk_list!(visitor, visit_generic_param, bound_generic_params);
942 WherePredicate::RegionPredicate(WhereRegionPredicate { ref lifetime, bounds, .. }) => {
943 visitor.visit_lifetime(lifetime);
944 walk_list!(visitor, visit_param_bound, bounds);
946 WherePredicate::EqPredicate(WhereEqPredicate {
947 hir_id, ref lhs_ty, ref rhs_ty, ..
949 visitor.visit_id(hir_id);
950 visitor.visit_ty(lhs_ty);
951 visitor.visit_ty(rhs_ty);
956 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FnRetTy<'v>) {
957 if let FnRetTy::Return(ref output_ty) = *ret_ty {
958 visitor.visit_ty(output_ty)
962 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl<'v>) {
963 for ty in function_declaration.inputs {
966 walk_fn_ret_ty(visitor, &function_declaration.output)
969 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
970 match function_kind {
971 FnKind::ItemFn(_, generics, ..) => {
972 visitor.visit_generics(generics);
974 FnKind::Method(..) | FnKind::Closure => {}
978 pub fn walk_fn<'v, V: Visitor<'v>>(
980 function_kind: FnKind<'v>,
981 function_declaration: &'v FnDecl<'v>,
986 visitor.visit_id(id);
987 visitor.visit_fn_decl(function_declaration);
988 walk_fn_kind(visitor, function_kind);
989 visitor.visit_nested_body(body_id)
992 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem<'v>) {
993 visitor.visit_ident(trait_item.ident);
994 visitor.visit_generics(&trait_item.generics);
995 match trait_item.kind {
996 TraitItemKind::Const(ref ty, default) => {
997 visitor.visit_id(trait_item.hir_id());
998 visitor.visit_ty(ty);
999 walk_list!(visitor, visit_nested_body, default);
1001 TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => {
1002 visitor.visit_id(trait_item.hir_id());
1003 visitor.visit_fn_decl(&sig.decl);
1004 for ¶m_name in param_names {
1005 visitor.visit_ident(param_name);
1008 TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => {
1010 FnKind::Method(trait_item.ident, sig, None),
1014 trait_item.hir_id(),
1017 TraitItemKind::Type(bounds, ref default) => {
1018 visitor.visit_id(trait_item.hir_id());
1019 walk_list!(visitor, visit_param_bound, bounds);
1020 walk_list!(visitor, visit_ty, default);
1025 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
1026 // N.B., deliberately force a compilation error if/when new fields are added.
1027 let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
1028 visitor.visit_nested_trait_item(id);
1029 visitor.visit_ident(ident);
1030 visitor.visit_associated_item_kind(kind);
1031 visitor.visit_defaultness(defaultness);
1034 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem<'v>) {
1035 // N.B., deliberately force a compilation error if/when new fields are added.
1036 let ImplItem { def_id: _, ident, ref vis, ref defaultness, ref generics, ref kind, span: _ } =
1039 visitor.visit_ident(ident);
1040 visitor.visit_vis(vis);
1041 visitor.visit_defaultness(defaultness);
1042 visitor.visit_generics(generics);
1044 ImplItemKind::Const(ref ty, body) => {
1045 visitor.visit_id(impl_item.hir_id());
1046 visitor.visit_ty(ty);
1047 visitor.visit_nested_body(body);
1049 ImplItemKind::Fn(ref sig, body_id) => {
1051 FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis)),
1058 ImplItemKind::TyAlias(ref ty) => {
1059 visitor.visit_id(impl_item.hir_id());
1060 visitor.visit_ty(ty);
1065 pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(
1067 foreign_item_ref: &'v ForeignItemRef,
1069 // N.B., deliberately force a compilation error if/when new fields are added.
1070 let ForeignItemRef { id, ident, span: _ } = *foreign_item_ref;
1071 visitor.visit_nested_foreign_item(id);
1072 visitor.visit_ident(ident);
1075 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
1076 // N.B., deliberately force a compilation error if/when new fields are added.
1077 let ImplItemRef { id, ident, ref kind, span: _, ref defaultness } = *impl_item_ref;
1078 visitor.visit_nested_impl_item(id);
1079 visitor.visit_ident(ident);
1080 visitor.visit_associated_item_kind(kind);
1081 visitor.visit_defaultness(defaultness);
1084 pub fn walk_struct_def<'v, V: Visitor<'v>>(
1086 struct_definition: &'v VariantData<'v>,
1088 walk_list!(visitor, visit_id, struct_definition.ctor_hir_id());
1089 walk_list!(visitor, visit_field_def, struct_definition.fields());
1092 pub fn walk_field_def<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v FieldDef<'v>) {
1093 visitor.visit_id(field.hir_id);
1094 visitor.visit_vis(&field.vis);
1095 visitor.visit_ident(field.ident);
1096 visitor.visit_ty(&field.ty);
1099 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block<'v>) {
1100 visitor.visit_id(block.hir_id);
1101 walk_list!(visitor, visit_stmt, block.stmts);
1102 walk_list!(visitor, visit_expr, &block.expr);
1105 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt<'v>) {
1106 visitor.visit_id(statement.hir_id);
1107 match statement.kind {
1108 StmtKind::Local(ref local) => visitor.visit_local(local),
1109 StmtKind::Item(item) => visitor.visit_nested_item(item),
1110 StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
1111 visitor.visit_expr(expression)
1116 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
1117 visitor.visit_id(constant.hir_id);
1118 visitor.visit_nested_body(constant.body);
1121 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) {
1122 visitor.visit_id(expression.hir_id);
1123 match expression.kind {
1124 ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
1125 ExprKind::Array(subexpressions) => {
1126 walk_list!(visitor, visit_expr, subexpressions);
1128 ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
1129 ExprKind::Repeat(ref element, ref count) => {
1130 visitor.visit_expr(element);
1131 visitor.visit_anon_const(count)
1133 ExprKind::Struct(ref qpath, fields, ref optional_base) => {
1134 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1135 for field in fields {
1136 visitor.visit_id(field.hir_id);
1137 visitor.visit_ident(field.ident);
1138 visitor.visit_expr(&field.expr)
1140 walk_list!(visitor, visit_expr, optional_base);
1142 ExprKind::Tup(subexpressions) => {
1143 walk_list!(visitor, visit_expr, subexpressions);
1145 ExprKind::Call(ref callee_expression, arguments) => {
1146 visitor.visit_expr(callee_expression);
1147 walk_list!(visitor, visit_expr, arguments);
1149 ExprKind::MethodCall(ref segment, _, arguments, _) => {
1150 visitor.visit_path_segment(expression.span, segment);
1151 walk_list!(visitor, visit_expr, arguments);
1153 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
1154 visitor.visit_expr(left_expression);
1155 visitor.visit_expr(right_expression)
1157 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
1158 visitor.visit_expr(subexpression)
1160 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
1161 visitor.visit_expr(subexpression);
1162 visitor.visit_ty(typ)
1164 ExprKind::DropTemps(ref subexpression) => {
1165 visitor.visit_expr(subexpression);
1167 ExprKind::Let(ref pat, ref expr, _) => {
1168 visitor.visit_expr(expr);
1169 visitor.visit_pat(pat);
1171 ExprKind::If(ref cond, ref then, ref else_opt) => {
1172 visitor.visit_expr(cond);
1173 visitor.visit_expr(then);
1174 walk_list!(visitor, visit_expr, else_opt);
1176 ExprKind::Loop(ref block, ref opt_label, _, _) => {
1177 walk_list!(visitor, visit_label, opt_label);
1178 visitor.visit_block(block);
1180 ExprKind::Match(ref subexpression, arms, _) => {
1181 visitor.visit_expr(subexpression);
1182 walk_list!(visitor, visit_arm, arms);
1184 ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => visitor
1187 function_declaration,
1192 ExprKind::Block(ref block, ref opt_label) => {
1193 walk_list!(visitor, visit_label, opt_label);
1194 visitor.visit_block(block);
1196 ExprKind::Assign(ref lhs, ref rhs, _) => {
1197 visitor.visit_expr(rhs);
1198 visitor.visit_expr(lhs)
1200 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
1201 visitor.visit_expr(right_expression);
1202 visitor.visit_expr(left_expression);
1204 ExprKind::Field(ref subexpression, ident) => {
1205 visitor.visit_expr(subexpression);
1206 visitor.visit_ident(ident);
1208 ExprKind::Index(ref main_expression, ref index_expression) => {
1209 visitor.visit_expr(main_expression);
1210 visitor.visit_expr(index_expression)
1212 ExprKind::Path(ref qpath) => {
1213 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1215 ExprKind::Break(ref destination, ref opt_expr) => {
1216 walk_list!(visitor, visit_label, &destination.label);
1217 walk_list!(visitor, visit_expr, opt_expr);
1219 ExprKind::Continue(ref destination) => {
1220 walk_list!(visitor, visit_label, &destination.label);
1222 ExprKind::Ret(ref optional_expression) => {
1223 walk_list!(visitor, visit_expr, optional_expression);
1225 ExprKind::InlineAsm(ref asm) => {
1226 walk_inline_asm(visitor, asm);
1228 ExprKind::LlvmInlineAsm(ref asm) => {
1229 walk_list!(visitor, visit_expr, asm.outputs_exprs);
1230 walk_list!(visitor, visit_expr, asm.inputs_exprs);
1232 ExprKind::Yield(ref subexpression, _) => {
1233 visitor.visit_expr(subexpression);
1235 ExprKind::Lit(_) | ExprKind::Err => {}
1239 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm<'v>) {
1240 visitor.visit_id(arm.hir_id);
1241 visitor.visit_pat(&arm.pat);
1242 if let Some(ref g) = arm.guard {
1244 Guard::If(ref e) => visitor.visit_expr(e),
1245 Guard::IfLet(ref pat, ref e) => {
1246 visitor.visit_pat(pat);
1247 visitor.visit_expr(e);
1251 visitor.visit_expr(&arm.body);
1254 pub fn walk_vis<'v, V: Visitor<'v>>(visitor: &mut V, vis: &'v Visibility<'v>) {
1255 if let VisibilityKind::Restricted { ref path, hir_id } = vis.node {
1256 visitor.visit_id(hir_id);
1257 visitor.visit_path(path, hir_id)
1261 pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssocItemKind) {
1262 // No visitable content here: this fn exists so you can call it if
1263 // the right thing to do, should content be added in the future,
1264 // would be to walk it.
1267 pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) {
1268 // No visitable content here: this fn exists so you can call it if
1269 // the right thing to do, should content be added in the future,
1270 // would be to walk it.