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,
121 pub fn constness(self) -> Constness {
122 self.header().map_or(Constness::NotConst, |header| header.constness)
125 pub fn asyncness(self) -> IsAsync {
126 self.header().map_or(IsAsync::NotAsync, |header| header.asyncness)
130 /// An abstract representation of the HIR `rustc_middle::hir::map::Map`.
131 pub trait Map<'hir> {
132 /// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
133 fn find(&self, hir_id: HirId) -> Option<Node<'hir>>;
134 fn body(&self, id: BodyId) -> &'hir Body<'hir>;
135 fn item(&self, id: ItemId) -> &'hir Item<'hir>;
136 fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>;
137 fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>;
138 fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>;
141 // Used when no map is actually available, forcing manual implementation of nested visitors.
142 impl<'hir> Map<'hir> for ! {
143 fn find(&self, _: HirId) -> Option<Node<'hir>> {
146 fn body(&self, _: BodyId) -> &'hir Body<'hir> {
149 fn item(&self, _: ItemId) -> &'hir Item<'hir> {
152 fn trait_item(&self, _: TraitItemId) -> &'hir TraitItem<'hir> {
155 fn impl_item(&self, _: ImplItemId) -> &'hir ImplItem<'hir> {
158 fn foreign_item(&self, _: ForeignItemId) -> &'hir ForeignItem<'hir> {
163 /// An erased version of `Map<'hir>`, using dynamic dispatch.
164 /// NOTE: This type is effectively only usable with `NestedVisitorMap::None`.
165 pub struct ErasedMap<'hir>(&'hir dyn Map<'hir>);
167 impl<'hir> Map<'hir> for ErasedMap<'hir> {
168 fn find(&self, _: HirId) -> Option<Node<'hir>> {
171 fn body(&self, id: BodyId) -> &'hir Body<'hir> {
174 fn item(&self, id: ItemId) -> &'hir Item<'hir> {
177 fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> {
178 self.0.trait_item(id)
180 fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
183 fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> {
184 self.0.foreign_item(id)
188 /// Specifies what nested things a visitor wants to visit. The most
189 /// common choice is `OnlyBodies`, which will cause the visitor to
190 /// visit fn bodies for fns that it encounters, but skip over nested
191 /// item-like things.
193 /// See the comments on `ItemLikeVisitor` for more details on the overall
195 pub enum NestedVisitorMap<M> {
196 /// Do not visit any nested things. When you add a new
197 /// "non-nested" thing, you will want to audit such uses to see if
198 /// they remain valid.
200 /// Use this if you are only walking some particular kind of tree
201 /// (i.e., a type, or fn signature) and you don't want to thread a
205 /// Do not visit nested item-like things, but visit nested things
206 /// that are inside of an item-like.
208 /// **This is the most common choice.** A very common pattern is
209 /// to use `visit_all_item_likes()` as an outer loop,
210 /// and to have the visitor that visits the contents of each item
211 /// using this setting.
214 /// Visits all nested things, including item-likes.
216 /// **This is an unusual choice.** It is used when you want to
217 /// process everything within their lexical context. Typically you
218 /// kick off the visit by doing `walk_krate()`.
222 impl<M> NestedVisitorMap<M> {
223 /// Returns the map to use for an "intra item-like" thing (if any).
224 /// E.g., function body.
225 fn intra(self) -> Option<M> {
227 NestedVisitorMap::None => None,
228 NestedVisitorMap::OnlyBodies(map) => Some(map),
229 NestedVisitorMap::All(map) => Some(map),
233 /// Returns the map to use for an "item-like" thing (if any).
234 /// E.g., item, impl-item.
235 fn inter(self) -> Option<M> {
237 NestedVisitorMap::None => None,
238 NestedVisitorMap::OnlyBodies(_) => None,
239 NestedVisitorMap::All(map) => Some(map),
244 /// Each method of the Visitor trait is a hook to be potentially
245 /// overridden. Each method's default implementation recursively visits
246 /// the substructure of the input via the corresponding `walk` method;
247 /// e.g., the `visit_mod` method by default calls `intravisit::walk_mod`.
249 /// Note that this visitor does NOT visit nested items by default
250 /// (this is why the module is called `intravisit`, to distinguish it
251 /// from the AST's `visit` module, which acts differently). If you
252 /// simply want to visit all items in the crate in some order, you
253 /// should call `Crate::visit_all_items`. Otherwise, see the comment
254 /// on `visit_nested_item` for details on how to visit nested items.
256 /// If you want to ensure that your code handles every variant
257 /// explicitly, you need to override each method. (And you also need
258 /// to monitor future changes to `Visitor` in case a new method with a
259 /// new default implementation gets introduced.)
260 pub trait Visitor<'v>: Sized {
263 ///////////////////////////////////////////////////////////////////////////
266 /// The default versions of the `visit_nested_XXX` routines invoke
267 /// this method to get a map to use. By selecting an enum variant,
268 /// you control which kinds of nested HIR are visited; see
269 /// `NestedVisitorMap` for details. By "nested HIR", we are
270 /// referring to bits of HIR that are not directly embedded within
271 /// one another but rather indirectly, through a table in the
272 /// crate. This is done to control dependencies during incremental
273 /// compilation: the non-inline bits of HIR can be tracked and
274 /// hashed separately.
276 /// **If for some reason you want the nested behavior, but don't
277 /// have a `Map` at your disposal:** then you should override the
278 /// `visit_nested_XXX` methods, and override this method to
279 /// `panic!()`. This way, if a new `visit_nested_XXX` variant is
280 /// added in the future, we will see the panic in your code and
281 /// fix it appropriately.
282 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map>;
284 /// Invoked when a nested item is encountered. By default does
285 /// nothing unless you override `nested_visit_map` to return other than
286 /// `None`, in which case it will walk the item. **You probably
287 /// don't want to override this method** -- instead, override
288 /// `nested_visit_map` or use the "shallow" or "deep" visit
289 /// patterns described on `itemlikevisit::ItemLikeVisitor`. The only
290 /// reason to override this method is if you want a nested pattern
291 /// but cannot supply a `Map`; see `nested_visit_map` for advice.
292 fn visit_nested_item(&mut self, id: ItemId) {
293 let opt_item = self.nested_visit_map().inter().map(|map| map.item(id));
294 walk_list!(self, visit_item, opt_item);
297 /// Like `visit_nested_item()`, but for trait items. See
298 /// `visit_nested_item()` for advice on when to override this
300 fn visit_nested_trait_item(&mut self, id: TraitItemId) {
301 let opt_item = self.nested_visit_map().inter().map(|map| map.trait_item(id));
302 walk_list!(self, visit_trait_item, opt_item);
305 /// Like `visit_nested_item()`, but for impl items. See
306 /// `visit_nested_item()` for advice on when to override this
308 fn visit_nested_impl_item(&mut self, id: ImplItemId) {
309 let opt_item = self.nested_visit_map().inter().map(|map| map.impl_item(id));
310 walk_list!(self, visit_impl_item, opt_item);
313 /// Like `visit_nested_item()`, but for foreign items. See
314 /// `visit_nested_item()` for advice on when to override this
316 fn visit_nested_foreign_item(&mut self, id: ForeignItemId) {
317 let opt_item = self.nested_visit_map().inter().map(|map| map.foreign_item(id));
318 walk_list!(self, visit_foreign_item, opt_item);
321 /// Invoked to visit the body of a function, method or closure. Like
322 /// visit_nested_item, does nothing by default unless you override
323 /// `nested_visit_map` to return other than `None`, in which case it will walk
325 fn visit_nested_body(&mut self, id: BodyId) {
326 let opt_body = self.nested_visit_map().intra().map(|map| map.body(id));
327 walk_list!(self, visit_body, opt_body);
330 fn visit_param(&mut self, param: &'v Param<'v>) {
331 walk_param(self, param)
334 /// Visits the top-level item and (optionally) nested items / impl items. See
335 /// `visit_nested_item` for details.
336 fn visit_item(&mut self, i: &'v Item<'v>) {
340 fn visit_body(&mut self, b: &'v Body<'v>) {
344 /// When invoking `visit_all_item_likes()`, you need to supply an
345 /// item-like visitor. This method converts an "intra-visit"
346 /// visitor into an item-like visitor that walks the entire tree.
347 /// If you use this, you probably don't want to process the
348 /// contents of nested item-like things, since the outer loop will
349 /// visit them as well.
350 fn as_deep_visitor(&mut self) -> DeepVisitor<'_, Self> {
351 DeepVisitor::new(self)
354 ///////////////////////////////////////////////////////////////////////////
356 fn visit_id(&mut self, _hir_id: HirId) {
359 fn visit_name(&mut self, _span: Span, _name: Symbol) {
362 fn visit_ident(&mut self, ident: Ident) {
363 walk_ident(self, ident)
365 fn visit_mod(&mut self, m: &'v Mod<'v>, _s: Span, n: HirId) {
368 fn visit_foreign_item(&mut self, i: &'v ForeignItem<'v>) {
369 walk_foreign_item(self, i)
371 fn visit_local(&mut self, l: &'v Local<'v>) {
374 fn visit_block(&mut self, b: &'v Block<'v>) {
377 fn visit_stmt(&mut self, s: &'v Stmt<'v>) {
380 fn visit_arm(&mut self, a: &'v Arm<'v>) {
383 fn visit_pat(&mut self, p: &'v Pat<'v>) {
386 fn visit_anon_const(&mut self, c: &'v AnonConst) {
387 walk_anon_const(self, c)
389 fn visit_expr(&mut self, ex: &'v Expr<'v>) {
392 fn visit_ty(&mut self, t: &'v Ty<'v>) {
395 fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) {
396 walk_generic_param(self, p)
398 fn visit_const_param_default(&mut self, _param: HirId, ct: &'v AnonConst) {
399 walk_const_param_default(self, ct)
401 fn visit_generics(&mut self, g: &'v Generics<'v>) {
402 walk_generics(self, g)
404 fn visit_where_predicate(&mut self, predicate: &'v WherePredicate<'v>) {
405 walk_where_predicate(self, predicate)
407 fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) {
408 walk_fn_decl(self, fd)
410 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, s: Span, id: HirId) {
411 walk_fn(self, fk, fd, b, s, id)
413 fn visit_use(&mut self, path: &'v Path<'v>, hir_id: HirId) {
414 walk_use(self, path, hir_id)
416 fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) {
417 walk_trait_item(self, ti)
419 fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
420 walk_trait_item_ref(self, ii)
422 fn visit_impl_item(&mut self, ii: &'v ImplItem<'v>) {
423 walk_impl_item(self, ii)
425 fn visit_foreign_item_ref(&mut self, ii: &'v ForeignItemRef) {
426 walk_foreign_item_ref(self, ii)
428 fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) {
429 walk_impl_item_ref(self, ii)
431 fn visit_trait_ref(&mut self, t: &'v TraitRef<'v>) {
432 walk_trait_ref(self, t)
434 fn visit_param_bound(&mut self, bounds: &'v GenericBound<'v>) {
435 walk_param_bound(self, bounds)
437 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>, m: TraitBoundModifier) {
438 walk_poly_trait_ref(self, t, m)
440 fn visit_variant_data(
442 s: &'v VariantData<'v>,
448 walk_struct_def(self, s)
450 fn visit_field_def(&mut self, s: &'v FieldDef<'v>) {
451 walk_field_def(self, s)
455 enum_definition: &'v EnumDef<'v>,
456 generics: &'v Generics<'v>,
460 walk_enum_def(self, enum_definition, generics, item_id)
462 fn visit_variant(&mut self, v: &'v Variant<'v>, g: &'v Generics<'v>, item_id: HirId) {
463 walk_variant(self, v, g, item_id)
465 fn visit_label(&mut self, label: &'v Label) {
466 walk_label(self, label)
468 fn visit_infer(&mut self, inf: &'v InferArg) {
471 fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg<'v>) {
473 GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
474 GenericArg::Type(ty) => self.visit_ty(ty),
475 GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
476 GenericArg::Infer(inf) => self.visit_infer(inf),
479 fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
480 walk_lifetime(self, lifetime)
482 fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, span: Span) {
483 walk_qpath(self, qpath, id, span)
485 fn visit_path(&mut self, path: &'v Path<'v>, _id: HirId) {
486 walk_path(self, path)
488 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment<'v>) {
489 walk_path_segment(self, path_span, path_segment)
491 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs<'v>) {
492 walk_generic_args(self, path_span, generic_args)
494 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding<'v>) {
495 walk_assoc_type_binding(self, type_binding)
497 fn visit_attribute(&mut self, _id: HirId, _attr: &'v Attribute) {}
498 fn visit_vis(&mut self, vis: &'v Visibility<'v>) {
501 fn visit_associated_item_kind(&mut self, kind: &'v AssocItemKind) {
502 walk_associated_item_kind(self, kind);
504 fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
505 walk_defaultness(self, defaultness);
509 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod<'v>, mod_hir_id: HirId) {
510 visitor.visit_id(mod_hir_id);
511 for &item_id in module.item_ids {
512 visitor.visit_nested_item(item_id);
516 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body<'v>) {
517 walk_list!(visitor, visit_param, body.params);
518 visitor.visit_expr(&body.value);
521 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local<'v>) {
522 // Intentionally visiting the expr first - the initialization expr
523 // dominates the local's definition.
524 walk_list!(visitor, visit_expr, &local.init);
525 visitor.visit_id(local.hir_id);
526 visitor.visit_pat(&local.pat);
527 walk_list!(visitor, visit_ty, &local.ty);
530 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
531 visitor.visit_name(ident.span, ident.name);
534 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
535 visitor.visit_ident(label.ident);
538 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
539 visitor.visit_id(lifetime.hir_id);
540 match lifetime.name {
541 LifetimeName::Param(ParamName::Plain(ident)) => {
542 visitor.visit_ident(ident);
544 LifetimeName::Param(ParamName::Fresh(_))
545 | LifetimeName::Param(ParamName::Error)
546 | LifetimeName::Static
547 | LifetimeName::Error
548 | LifetimeName::Implicit(_)
549 | LifetimeName::ImplicitObjectLifetimeDefault
550 | LifetimeName::Underscore => {}
554 pub fn walk_poly_trait_ref<'v, V: Visitor<'v>>(
556 trait_ref: &'v PolyTraitRef<'v>,
557 _modifier: TraitBoundModifier,
559 walk_list!(visitor, visit_generic_param, trait_ref.bound_generic_params);
560 visitor.visit_trait_ref(&trait_ref.trait_ref);
563 pub fn walk_trait_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_ref: &'v TraitRef<'v>) {
564 visitor.visit_id(trait_ref.hir_ref_id);
565 visitor.visit_path(&trait_ref.path, trait_ref.hir_ref_id)
568 pub fn walk_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Param<'v>) {
569 visitor.visit_id(param.hir_id);
570 visitor.visit_pat(¶m.pat);
573 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
574 visitor.visit_vis(&item.vis);
575 visitor.visit_ident(item.ident);
577 ItemKind::ExternCrate(orig_name) => {
578 visitor.visit_id(item.hir_id());
579 if let Some(orig_name) = orig_name {
580 visitor.visit_name(item.span, orig_name);
583 ItemKind::Use(ref path, _) => {
584 visitor.visit_use(path, item.hir_id());
586 ItemKind::Static(ref typ, _, body) | ItemKind::Const(ref typ, body) => {
587 visitor.visit_id(item.hir_id());
588 visitor.visit_ty(typ);
589 visitor.visit_nested_body(body);
591 ItemKind::Fn(ref sig, ref generics, body_id) => visitor.visit_fn(
592 FnKind::ItemFn(item.ident, generics, sig.header, &item.vis),
598 ItemKind::Macro(_) => {
599 visitor.visit_id(item.hir_id());
601 ItemKind::Mod(ref module) => {
602 // `visit_mod()` takes care of visiting the `Item`'s `HirId`.
603 visitor.visit_mod(module, item.span, item.hir_id())
605 ItemKind::ForeignMod { abi: _, items } => {
606 visitor.visit_id(item.hir_id());
607 walk_list!(visitor, visit_foreign_item_ref, items);
609 ItemKind::GlobalAsm(asm) => {
610 visitor.visit_id(item.hir_id());
611 walk_inline_asm(visitor, asm);
613 ItemKind::TyAlias(ref ty, ref generics) => {
614 visitor.visit_id(item.hir_id());
615 visitor.visit_ty(ty);
616 visitor.visit_generics(generics)
618 ItemKind::OpaqueTy(OpaqueTy { ref generics, bounds, .. }) => {
619 visitor.visit_id(item.hir_id());
620 walk_generics(visitor, generics);
621 walk_list!(visitor, visit_param_bound, bounds);
623 ItemKind::Enum(ref enum_definition, ref generics) => {
624 visitor.visit_generics(generics);
625 // `visit_enum_def()` takes care of visiting the `Item`'s `HirId`.
626 visitor.visit_enum_def(enum_definition, generics, item.hir_id(), item.span)
628 ItemKind::Impl(Impl {
639 visitor.visit_id(item.hir_id());
640 visitor.visit_generics(generics);
641 walk_list!(visitor, visit_trait_ref, of_trait);
642 visitor.visit_ty(self_ty);
643 walk_list!(visitor, visit_impl_item_ref, items);
645 ItemKind::Struct(ref struct_definition, ref generics)
646 | ItemKind::Union(ref struct_definition, ref generics) => {
647 visitor.visit_generics(generics);
648 visitor.visit_id(item.hir_id());
649 visitor.visit_variant_data(
657 ItemKind::Trait(.., ref generics, bounds, trait_item_refs) => {
658 visitor.visit_id(item.hir_id());
659 visitor.visit_generics(generics);
660 walk_list!(visitor, visit_param_bound, bounds);
661 walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
663 ItemKind::TraitAlias(ref generics, bounds) => {
664 visitor.visit_id(item.hir_id());
665 visitor.visit_generics(generics);
666 walk_list!(visitor, visit_param_bound, bounds);
671 fn walk_inline_asm<'v, V: Visitor<'v>>(visitor: &mut V, asm: &'v InlineAsm<'v>) {
672 for (op, _op_sp) in asm.operands {
674 InlineAsmOperand::In { expr, .. }
675 | InlineAsmOperand::InOut { expr, .. }
676 | InlineAsmOperand::Sym { expr, .. } => visitor.visit_expr(expr),
677 InlineAsmOperand::Out { expr, .. } => {
678 if let Some(expr) = expr {
679 visitor.visit_expr(expr);
682 InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
683 visitor.visit_expr(in_expr);
684 if let Some(out_expr) = out_expr {
685 visitor.visit_expr(out_expr);
688 InlineAsmOperand::Const { anon_const } => visitor.visit_anon_const(anon_const),
693 pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>, hir_id: HirId) {
694 visitor.visit_id(hir_id);
695 visitor.visit_path(path, hir_id);
698 pub fn walk_enum_def<'v, V: Visitor<'v>>(
700 enum_definition: &'v EnumDef<'v>,
701 generics: &'v Generics<'v>,
704 visitor.visit_id(item_id);
705 walk_list!(visitor, visit_variant, enum_definition.variants, generics, item_id);
708 pub fn walk_variant<'v, V: Visitor<'v>>(
710 variant: &'v Variant<'v>,
711 generics: &'v Generics<'v>,
712 parent_item_id: HirId,
714 visitor.visit_ident(variant.ident);
715 visitor.visit_id(variant.id);
716 visitor.visit_variant_data(
723 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
726 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
727 visitor.visit_id(typ.hir_id);
730 TyKind::Slice(ref ty) => visitor.visit_ty(ty),
731 TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
732 TyKind::Rptr(ref lifetime, ref mutable_type) => {
733 visitor.visit_lifetime(lifetime);
734 visitor.visit_ty(&mutable_type.ty)
737 TyKind::Tup(tuple_element_types) => {
738 walk_list!(visitor, visit_ty, tuple_element_types);
740 TyKind::BareFn(ref function_declaration) => {
741 walk_list!(visitor, visit_generic_param, function_declaration.generic_params);
742 visitor.visit_fn_decl(&function_declaration.decl);
744 TyKind::Path(ref qpath) => {
745 visitor.visit_qpath(qpath, typ.hir_id, typ.span);
747 TyKind::OpaqueDef(item_id, lifetimes) => {
748 visitor.visit_nested_item(item_id);
749 walk_list!(visitor, visit_generic_arg, lifetimes);
751 TyKind::Array(ref ty, ref length) => {
752 visitor.visit_ty(ty);
753 visitor.visit_anon_const(length)
755 TyKind::TraitObject(bounds, ref lifetime, _syntax) => {
756 for bound in bounds {
757 visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
759 visitor.visit_lifetime(lifetime);
761 TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
762 TyKind::Infer | TyKind::Err => {}
766 pub fn walk_inf<'v, V: Visitor<'v>>(visitor: &mut V, inf: &'v InferArg) {
767 visitor.visit_id(inf.hir_id);
770 pub fn walk_qpath<'v, V: Visitor<'v>>(
772 qpath: &'v QPath<'v>,
777 QPath::Resolved(ref maybe_qself, ref path) => {
778 walk_list!(visitor, visit_ty, maybe_qself);
779 visitor.visit_path(path, id)
781 QPath::TypeRelative(ref qself, ref segment) => {
782 visitor.visit_ty(qself);
783 visitor.visit_path_segment(span, segment);
785 QPath::LangItem(..) => {}
789 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>) {
790 for segment in path.segments {
791 visitor.visit_path_segment(path.span, segment);
795 pub fn walk_path_segment<'v, V: Visitor<'v>>(
798 segment: &'v PathSegment<'v>,
800 visitor.visit_ident(segment.ident);
801 walk_list!(visitor, visit_id, segment.hir_id);
802 if let Some(ref args) = segment.args {
803 visitor.visit_generic_args(path_span, args);
807 pub fn walk_generic_args<'v, V: Visitor<'v>>(
810 generic_args: &'v GenericArgs<'v>,
812 walk_list!(visitor, visit_generic_arg, generic_args.args);
813 walk_list!(visitor, visit_assoc_type_binding, generic_args.bindings);
816 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(
818 type_binding: &'v TypeBinding<'v>,
820 visitor.visit_id(type_binding.hir_id);
821 visitor.visit_ident(type_binding.ident);
822 visitor.visit_generic_args(type_binding.span, type_binding.gen_args);
823 match type_binding.kind {
824 TypeBindingKind::Equality { ref ty } => {
825 visitor.visit_ty(ty);
827 TypeBindingKind::Constraint { bounds } => {
828 walk_list!(visitor, visit_param_bound, bounds);
833 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) {
834 visitor.visit_id(pattern.hir_id);
836 PatKind::TupleStruct(ref qpath, children, _) => {
837 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
838 walk_list!(visitor, visit_pat, children);
840 PatKind::Path(ref qpath) => {
841 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
843 PatKind::Struct(ref qpath, fields, _) => {
844 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
845 for field in fields {
846 visitor.visit_id(field.hir_id);
847 visitor.visit_ident(field.ident);
848 visitor.visit_pat(&field.pat)
851 PatKind::Or(pats) => walk_list!(visitor, visit_pat, pats),
852 PatKind::Tuple(tuple_elements, _) => {
853 walk_list!(visitor, visit_pat, tuple_elements);
855 PatKind::Box(ref subpattern) | PatKind::Ref(ref subpattern, _) => {
856 visitor.visit_pat(subpattern)
858 PatKind::Binding(_, _hir_id, ident, ref optional_subpattern) => {
859 visitor.visit_ident(ident);
860 walk_list!(visitor, visit_pat, optional_subpattern);
862 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
863 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
864 walk_list!(visitor, visit_expr, lower_bound);
865 walk_list!(visitor, visit_expr, upper_bound);
868 PatKind::Slice(prepatterns, ref slice_pattern, postpatterns) => {
869 walk_list!(visitor, visit_pat, prepatterns);
870 walk_list!(visitor, visit_pat, slice_pattern);
871 walk_list!(visitor, visit_pat, postpatterns);
876 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem<'v>) {
877 visitor.visit_id(foreign_item.hir_id());
878 visitor.visit_vis(&foreign_item.vis);
879 visitor.visit_ident(foreign_item.ident);
881 match foreign_item.kind {
882 ForeignItemKind::Fn(ref function_declaration, param_names, ref generics) => {
883 visitor.visit_generics(generics);
884 visitor.visit_fn_decl(function_declaration);
885 for ¶m_name in param_names {
886 visitor.visit_ident(param_name);
889 ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
890 ForeignItemKind::Type => (),
894 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound<'v>) {
896 GenericBound::Trait(ref typ, modifier) => {
897 visitor.visit_poly_trait_ref(typ, modifier);
899 GenericBound::LangItemTrait(_, span, hir_id, args) => {
900 visitor.visit_id(hir_id);
901 visitor.visit_generic_args(span, args);
903 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
907 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam<'v>) {
908 visitor.visit_id(param.hir_id);
910 ParamName::Plain(ident) => visitor.visit_ident(ident),
911 ParamName::Error | ParamName::Fresh(_) => {}
914 GenericParamKind::Lifetime { .. } => {}
915 GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
916 GenericParamKind::Const { ref ty, ref default } => {
917 visitor.visit_ty(ty);
918 if let Some(ref default) = default {
919 visitor.visit_const_param_default(param.hir_id, default);
923 walk_list!(visitor, visit_param_bound, param.bounds);
926 pub fn walk_const_param_default<'v, V: Visitor<'v>>(visitor: &mut V, ct: &'v AnonConst) {
927 visitor.visit_anon_const(ct)
930 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) {
931 walk_list!(visitor, visit_generic_param, generics.params);
932 walk_list!(visitor, visit_where_predicate, generics.where_clause.predicates);
935 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
937 predicate: &'v WherePredicate<'v>,
940 WherePredicate::BoundPredicate(WhereBoundPredicate {
943 bound_generic_params,
946 visitor.visit_ty(bounded_ty);
947 walk_list!(visitor, visit_param_bound, bounds);
948 walk_list!(visitor, visit_generic_param, bound_generic_params);
950 WherePredicate::RegionPredicate(WhereRegionPredicate { ref lifetime, bounds, .. }) => {
951 visitor.visit_lifetime(lifetime);
952 walk_list!(visitor, visit_param_bound, bounds);
954 WherePredicate::EqPredicate(WhereEqPredicate {
955 hir_id, ref lhs_ty, ref rhs_ty, ..
957 visitor.visit_id(hir_id);
958 visitor.visit_ty(lhs_ty);
959 visitor.visit_ty(rhs_ty);
964 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FnRetTy<'v>) {
965 if let FnRetTy::Return(ref output_ty) = *ret_ty {
966 visitor.visit_ty(output_ty)
970 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl<'v>) {
971 for ty in function_declaration.inputs {
974 walk_fn_ret_ty(visitor, &function_declaration.output)
977 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
978 match function_kind {
979 FnKind::ItemFn(_, generics, ..) => {
980 visitor.visit_generics(generics);
982 FnKind::Method(..) | FnKind::Closure => {}
986 pub fn walk_fn<'v, V: Visitor<'v>>(
988 function_kind: FnKind<'v>,
989 function_declaration: &'v FnDecl<'v>,
994 visitor.visit_id(id);
995 visitor.visit_fn_decl(function_declaration);
996 walk_fn_kind(visitor, function_kind);
997 visitor.visit_nested_body(body_id)
1000 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem<'v>) {
1001 visitor.visit_ident(trait_item.ident);
1002 visitor.visit_generics(&trait_item.generics);
1003 match trait_item.kind {
1004 TraitItemKind::Const(ref ty, default) => {
1005 visitor.visit_id(trait_item.hir_id());
1006 visitor.visit_ty(ty);
1007 walk_list!(visitor, visit_nested_body, default);
1009 TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => {
1010 visitor.visit_id(trait_item.hir_id());
1011 visitor.visit_fn_decl(&sig.decl);
1012 for ¶m_name in param_names {
1013 visitor.visit_ident(param_name);
1016 TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => {
1018 FnKind::Method(trait_item.ident, sig, None),
1022 trait_item.hir_id(),
1025 TraitItemKind::Type(bounds, ref default) => {
1026 visitor.visit_id(trait_item.hir_id());
1027 walk_list!(visitor, visit_param_bound, bounds);
1028 walk_list!(visitor, visit_ty, default);
1033 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
1034 // N.B., deliberately force a compilation error if/when new fields are added.
1035 let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
1036 visitor.visit_nested_trait_item(id);
1037 visitor.visit_ident(ident);
1038 visitor.visit_associated_item_kind(kind);
1039 visitor.visit_defaultness(defaultness);
1042 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem<'v>) {
1043 // N.B., deliberately force a compilation error if/when new fields are added.
1044 let ImplItem { def_id: _, ident, ref vis, ref defaultness, ref generics, ref kind, span: _ } =
1047 visitor.visit_ident(ident);
1048 visitor.visit_vis(vis);
1049 visitor.visit_defaultness(defaultness);
1050 visitor.visit_generics(generics);
1052 ImplItemKind::Const(ref ty, body) => {
1053 visitor.visit_id(impl_item.hir_id());
1054 visitor.visit_ty(ty);
1055 visitor.visit_nested_body(body);
1057 ImplItemKind::Fn(ref sig, body_id) => {
1059 FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis)),
1066 ImplItemKind::TyAlias(ref ty) => {
1067 visitor.visit_id(impl_item.hir_id());
1068 visitor.visit_ty(ty);
1073 pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(
1075 foreign_item_ref: &'v ForeignItemRef,
1077 // N.B., deliberately force a compilation error if/when new fields are added.
1078 let ForeignItemRef { id, ident, span: _ } = *foreign_item_ref;
1079 visitor.visit_nested_foreign_item(id);
1080 visitor.visit_ident(ident);
1083 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
1084 // N.B., deliberately force a compilation error if/when new fields are added.
1085 let ImplItemRef { id, ident, ref kind, span: _, ref defaultness } = *impl_item_ref;
1086 visitor.visit_nested_impl_item(id);
1087 visitor.visit_ident(ident);
1088 visitor.visit_associated_item_kind(kind);
1089 visitor.visit_defaultness(defaultness);
1092 pub fn walk_struct_def<'v, V: Visitor<'v>>(
1094 struct_definition: &'v VariantData<'v>,
1096 walk_list!(visitor, visit_id, struct_definition.ctor_hir_id());
1097 walk_list!(visitor, visit_field_def, struct_definition.fields());
1100 pub fn walk_field_def<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v FieldDef<'v>) {
1101 visitor.visit_id(field.hir_id);
1102 visitor.visit_vis(&field.vis);
1103 visitor.visit_ident(field.ident);
1104 visitor.visit_ty(&field.ty);
1107 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block<'v>) {
1108 visitor.visit_id(block.hir_id);
1109 walk_list!(visitor, visit_stmt, block.stmts);
1110 walk_list!(visitor, visit_expr, &block.expr);
1113 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt<'v>) {
1114 visitor.visit_id(statement.hir_id);
1115 match statement.kind {
1116 StmtKind::Local(ref local) => visitor.visit_local(local),
1117 StmtKind::Item(item) => visitor.visit_nested_item(item),
1118 StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
1119 visitor.visit_expr(expression)
1124 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
1125 visitor.visit_id(constant.hir_id);
1126 visitor.visit_nested_body(constant.body);
1129 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) {
1130 visitor.visit_id(expression.hir_id);
1131 match expression.kind {
1132 ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
1133 ExprKind::Array(subexpressions) => {
1134 walk_list!(visitor, visit_expr, subexpressions);
1136 ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
1137 ExprKind::Repeat(ref element, ref count) => {
1138 visitor.visit_expr(element);
1139 visitor.visit_anon_const(count)
1141 ExprKind::Struct(ref qpath, fields, ref optional_base) => {
1142 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1143 for field in fields {
1144 visitor.visit_id(field.hir_id);
1145 visitor.visit_ident(field.ident);
1146 visitor.visit_expr(&field.expr)
1148 walk_list!(visitor, visit_expr, optional_base);
1150 ExprKind::Tup(subexpressions) => {
1151 walk_list!(visitor, visit_expr, subexpressions);
1153 ExprKind::Call(ref callee_expression, arguments) => {
1154 visitor.visit_expr(callee_expression);
1155 walk_list!(visitor, visit_expr, arguments);
1157 ExprKind::MethodCall(ref segment, _, arguments, _) => {
1158 visitor.visit_path_segment(expression.span, segment);
1159 walk_list!(visitor, visit_expr, arguments);
1161 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
1162 visitor.visit_expr(left_expression);
1163 visitor.visit_expr(right_expression)
1165 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
1166 visitor.visit_expr(subexpression)
1168 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
1169 visitor.visit_expr(subexpression);
1170 visitor.visit_ty(typ)
1172 ExprKind::DropTemps(ref subexpression) => {
1173 visitor.visit_expr(subexpression);
1175 ExprKind::Let(ref pat, ref expr, _) => {
1176 visitor.visit_expr(expr);
1177 visitor.visit_pat(pat);
1179 ExprKind::If(ref cond, ref then, ref else_opt) => {
1180 visitor.visit_expr(cond);
1181 visitor.visit_expr(then);
1182 walk_list!(visitor, visit_expr, else_opt);
1184 ExprKind::Loop(ref block, ref opt_label, _, _) => {
1185 walk_list!(visitor, visit_label, opt_label);
1186 visitor.visit_block(block);
1188 ExprKind::Match(ref subexpression, arms, _) => {
1189 visitor.visit_expr(subexpression);
1190 walk_list!(visitor, visit_arm, arms);
1192 ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => visitor
1195 function_declaration,
1200 ExprKind::Block(ref block, ref opt_label) => {
1201 walk_list!(visitor, visit_label, opt_label);
1202 visitor.visit_block(block);
1204 ExprKind::Assign(ref lhs, ref rhs, _) => {
1205 visitor.visit_expr(rhs);
1206 visitor.visit_expr(lhs)
1208 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
1209 visitor.visit_expr(right_expression);
1210 visitor.visit_expr(left_expression);
1212 ExprKind::Field(ref subexpression, ident) => {
1213 visitor.visit_expr(subexpression);
1214 visitor.visit_ident(ident);
1216 ExprKind::Index(ref main_expression, ref index_expression) => {
1217 visitor.visit_expr(main_expression);
1218 visitor.visit_expr(index_expression)
1220 ExprKind::Path(ref qpath) => {
1221 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1223 ExprKind::Break(ref destination, ref opt_expr) => {
1224 walk_list!(visitor, visit_label, &destination.label);
1225 walk_list!(visitor, visit_expr, opt_expr);
1227 ExprKind::Continue(ref destination) => {
1228 walk_list!(visitor, visit_label, &destination.label);
1230 ExprKind::Ret(ref optional_expression) => {
1231 walk_list!(visitor, visit_expr, optional_expression);
1233 ExprKind::InlineAsm(ref asm) => {
1234 walk_inline_asm(visitor, asm);
1236 ExprKind::LlvmInlineAsm(ref asm) => {
1237 walk_list!(visitor, visit_expr, asm.outputs_exprs);
1238 walk_list!(visitor, visit_expr, asm.inputs_exprs);
1240 ExprKind::Yield(ref subexpression, _) => {
1241 visitor.visit_expr(subexpression);
1243 ExprKind::Lit(_) | ExprKind::Err => {}
1247 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm<'v>) {
1248 visitor.visit_id(arm.hir_id);
1249 visitor.visit_pat(&arm.pat);
1250 if let Some(ref g) = arm.guard {
1252 Guard::If(ref e) => visitor.visit_expr(e),
1253 Guard::IfLet(ref pat, ref e) => {
1254 visitor.visit_pat(pat);
1255 visitor.visit_expr(e);
1259 visitor.visit_expr(&arm.body);
1262 pub fn walk_vis<'v, V: Visitor<'v>>(visitor: &mut V, vis: &'v Visibility<'v>) {
1263 if let VisibilityKind::Restricted { ref path, hir_id } = vis.node {
1264 visitor.visit_id(hir_id);
1265 visitor.visit_path(path, hir_id)
1269 pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssocItemKind) {
1270 // No visitable content here: this fn exists so you can call it if
1271 // the right thing to do, should content be added in the future,
1272 // would be to walk it.
1275 pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) {
1276 // No visitable content here: this fn exists so you can call it if
1277 // the right thing to do, should content be added in the future,
1278 // would be to walk it.