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.
34 use super::itemlikevisit::DeepVisitor;
36 use crate::hir::map::Map;
39 use syntax::ast::{Attribute, Ident, Name};
42 #[derive(Copy, Clone)]
44 /// `#[xxx] pub async/const/extern "Abi" fn foo()`
45 ItemFn(Ident, &'a Generics<'a>, FnHeader, &'a Visibility<'a>, &'a [Attribute]),
48 Method(Ident, &'a FnSig<'a>, Option<&'a Visibility<'a>>, &'a [Attribute]),
51 Closure(&'a [Attribute]),
55 pub fn attrs(&self) -> &'a [Attribute] {
57 FnKind::ItemFn(.., attrs) => attrs,
58 FnKind::Method(.., attrs) => attrs,
59 FnKind::Closure(attrs) => attrs,
63 pub fn header(&self) -> Option<&FnHeader> {
65 FnKind::ItemFn(_, _, ref header, _, _) => Some(header),
66 FnKind::Method(_, ref sig, _, _) => Some(&sig.header),
67 FnKind::Closure(_) => None,
72 /// Specifies what nested things a visitor wants to visit. The most
73 /// common choice is `OnlyBodies`, which will cause the visitor to
74 /// visit fn bodies for fns that it encounters, but skip over nested
77 /// See the comments on `ItemLikeVisitor` for more details on the overall
79 pub enum NestedVisitorMap<'this, 'tcx> {
80 /// Do not visit any nested things. When you add a new
81 /// "non-nested" thing, you will want to audit such uses to see if
82 /// they remain valid.
84 /// Use this if you are only walking some particular kind of tree
85 /// (i.e., a type, or fn signature) and you don't want to thread a
89 /// Do not visit nested item-like things, but visit nested things
90 /// that are inside of an item-like.
92 /// **This is the most common choice.** A very common pattern is
93 /// to use `visit_all_item_likes()` as an outer loop,
94 /// and to have the visitor that visits the contents of each item
95 /// using this setting.
96 OnlyBodies(&'this Map<'tcx>),
98 /// Visits all nested things, including item-likes.
100 /// **This is an unusual choice.** It is used when you want to
101 /// process everything within their lexical context. Typically you
102 /// kick off the visit by doing `walk_krate()`.
103 All(&'this Map<'tcx>),
106 impl<'this, 'tcx> NestedVisitorMap<'this, 'tcx> {
107 /// Returns the map to use for an "intra item-like" thing (if any).
108 /// E.g., function body.
109 pub fn intra(self) -> Option<&'this Map<'tcx>> {
111 NestedVisitorMap::None => None,
112 NestedVisitorMap::OnlyBodies(map) => Some(map),
113 NestedVisitorMap::All(map) => Some(map),
117 /// Returns the map to use for an "item-like" thing (if any).
118 /// E.g., item, impl-item.
119 pub fn inter(self) -> Option<&'this Map<'tcx>> {
121 NestedVisitorMap::None => None,
122 NestedVisitorMap::OnlyBodies(_) => None,
123 NestedVisitorMap::All(map) => Some(map),
128 /// Each method of the Visitor trait is a hook to be potentially
129 /// overridden. Each method's default implementation recursively visits
130 /// the substructure of the input via the corresponding `walk` method;
131 /// e.g., the `visit_mod` method by default calls `intravisit::walk_mod`.
133 /// Note that this visitor does NOT visit nested items by default
134 /// (this is why the module is called `intravisit`, to distinguish it
135 /// from the AST's `visit` module, which acts differently). If you
136 /// simply want to visit all items in the crate in some order, you
137 /// should call `Crate::visit_all_items`. Otherwise, see the comment
138 /// on `visit_nested_item` for details on how to visit nested items.
140 /// If you want to ensure that your code handles every variant
141 /// explicitly, you need to override each method. (And you also need
142 /// to monitor future changes to `Visitor` in case a new method with a
143 /// new default implementation gets introduced.)
144 pub trait Visitor<'v>: Sized {
145 ///////////////////////////////////////////////////////////////////////////
148 /// The default versions of the `visit_nested_XXX` routines invoke
149 /// this method to get a map to use. By selecting an enum variant,
150 /// you control which kinds of nested HIR are visited; see
151 /// `NestedVisitorMap` for details. By "nested HIR", we are
152 /// referring to bits of HIR that are not directly embedded within
153 /// one another but rather indirectly, through a table in the
154 /// crate. This is done to control dependencies during incremental
155 /// compilation: the non-inline bits of HIR can be tracked and
156 /// hashed separately.
158 /// **If for some reason you want the nested behavior, but don't
159 /// have a `Map` at your disposal:** then you should override the
160 /// `visit_nested_XXX` methods, and override this method to
161 /// `panic!()`. This way, if a new `visit_nested_XXX` variant is
162 /// added in the future, we will see the panic in your code and
163 /// fix it appropriately.
164 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v>;
166 /// Invoked when a nested item is encountered. By default does
167 /// nothing unless you override `nested_visit_map` to return other than
168 /// `None`, in which case it will walk the item. **You probably
169 /// don't want to override this method** -- instead, override
170 /// `nested_visit_map` or use the "shallow" or "deep" visit
171 /// patterns described on `itemlikevisit::ItemLikeVisitor`. The only
172 /// reason to override this method is if you want a nested pattern
173 /// but cannot supply a `Map`; see `nested_visit_map` for advice.
174 #[allow(unused_variables)]
175 fn visit_nested_item(&mut self, id: ItemId) {
176 let opt_item = self.nested_visit_map().inter().map(|map| map.expect_item(id.id));
177 if let Some(item) = opt_item {
178 self.visit_item(item);
182 /// Like `visit_nested_item()`, but for trait items. See
183 /// `visit_nested_item()` for advice on when to override this
185 #[allow(unused_variables)]
186 fn visit_nested_trait_item(&mut self, id: TraitItemId) {
187 let opt_item = self.nested_visit_map().inter().map(|map| map.trait_item(id));
188 if let Some(item) = opt_item {
189 self.visit_trait_item(item);
193 /// Like `visit_nested_item()`, but for impl items. See
194 /// `visit_nested_item()` for advice on when to override this
196 #[allow(unused_variables)]
197 fn visit_nested_impl_item(&mut self, id: ImplItemId) {
198 let opt_item = self.nested_visit_map().inter().map(|map| map.impl_item(id));
199 if let Some(item) = opt_item {
200 self.visit_impl_item(item);
204 /// Invoked to visit the body of a function, method or closure. Like
205 /// visit_nested_item, does nothing by default unless you override
206 /// `nested_visit_map` to return other than `None`, in which case it will walk
208 fn visit_nested_body(&mut self, id: BodyId) {
209 let opt_body = self.nested_visit_map().intra().map(|map| map.body(id));
210 if let Some(body) = opt_body {
211 self.visit_body(body);
215 fn visit_param(&mut self, param: &'v Param<'v>) {
216 walk_param(self, param)
219 /// Visits the top-level item and (optionally) nested items / impl items. See
220 /// `visit_nested_item` for details.
221 fn visit_item(&mut self, i: &'v Item<'v>) {
225 fn visit_body(&mut self, b: &'v Body<'v>) {
229 /// When invoking `visit_all_item_likes()`, you need to supply an
230 /// item-like visitor. This method converts a "intra-visit"
231 /// visitor into an item-like visitor that walks the entire tree.
232 /// If you use this, you probably don't want to process the
233 /// contents of nested item-like things, since the outer loop will
234 /// visit them as well.
235 fn as_deep_visitor<'s>(&'s mut self) -> DeepVisitor<'s, Self> {
236 DeepVisitor::new(self)
239 ///////////////////////////////////////////////////////////////////////////
241 fn visit_id(&mut self, _hir_id: HirId) {
244 fn visit_name(&mut self, _span: Span, _name: Name) {
247 fn visit_ident(&mut self, ident: Ident) {
248 walk_ident(self, ident)
250 fn visit_mod(&mut self, m: &'v Mod<'v>, _s: Span, n: HirId) {
253 fn visit_foreign_item(&mut self, i: &'v ForeignItem<'v>) {
254 walk_foreign_item(self, i)
256 fn visit_local(&mut self, l: &'v Local<'v>) {
259 fn visit_block(&mut self, b: &'v Block<'v>) {
262 fn visit_stmt(&mut self, s: &'v Stmt<'v>) {
265 fn visit_arm(&mut self, a: &'v Arm<'v>) {
268 fn visit_pat(&mut self, p: &'v Pat<'v>) {
271 fn visit_anon_const(&mut self, c: &'v AnonConst) {
272 walk_anon_const(self, c)
274 fn visit_expr(&mut self, ex: &'v Expr<'v>) {
277 fn visit_ty(&mut self, t: &'v Ty<'v>) {
280 fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) {
281 walk_generic_param(self, p)
283 fn visit_generics(&mut self, g: &'v Generics<'v>) {
284 walk_generics(self, g)
286 fn visit_where_predicate(&mut self, predicate: &'v WherePredicate<'v>) {
287 walk_where_predicate(self, predicate)
289 fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) {
290 walk_fn_decl(self, fd)
292 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, s: Span, id: HirId) {
293 walk_fn(self, fk, fd, b, s, id)
295 fn visit_use(&mut self, path: &'v Path<'v>, hir_id: HirId) {
296 walk_use(self, path, hir_id)
298 fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) {
299 walk_trait_item(self, ti)
301 fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
302 walk_trait_item_ref(self, ii)
304 fn visit_impl_item(&mut self, ii: &'v ImplItem<'v>) {
305 walk_impl_item(self, ii)
307 fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef<'v>) {
308 walk_impl_item_ref(self, ii)
310 fn visit_trait_ref(&mut self, t: &'v TraitRef<'v>) {
311 walk_trait_ref(self, t)
313 fn visit_param_bound(&mut self, bounds: &'v GenericBound<'v>) {
314 walk_param_bound(self, bounds)
316 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>, m: TraitBoundModifier) {
317 walk_poly_trait_ref(self, t, m)
319 fn visit_variant_data(
321 s: &'v VariantData<'v>,
327 walk_struct_def(self, s)
329 fn visit_struct_field(&mut self, s: &'v StructField<'v>) {
330 walk_struct_field(self, s)
334 enum_definition: &'v EnumDef<'v>,
335 generics: &'v Generics<'v>,
339 walk_enum_def(self, enum_definition, generics, item_id)
341 fn visit_variant(&mut self, v: &'v Variant<'v>, g: &'v Generics<'v>, item_id: HirId) {
342 walk_variant(self, v, g, item_id)
344 fn visit_label(&mut self, label: &'v Label) {
345 walk_label(self, label)
347 fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg<'v>) {
349 GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
350 GenericArg::Type(ty) => self.visit_ty(ty),
351 GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
354 fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
355 walk_lifetime(self, lifetime)
357 fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, span: Span) {
358 walk_qpath(self, qpath, id, span)
360 fn visit_path(&mut self, path: &'v Path<'v>, _id: HirId) {
361 walk_path(self, path)
363 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment<'v>) {
364 walk_path_segment(self, path_span, path_segment)
366 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs<'v>) {
367 walk_generic_args(self, path_span, generic_args)
369 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding<'v>) {
370 walk_assoc_type_binding(self, type_binding)
372 fn visit_attribute(&mut self, _attr: &'v Attribute) {}
373 fn visit_macro_def(&mut self, macro_def: &'v MacroDef<'v>) {
374 walk_macro_def(self, macro_def)
376 fn visit_vis(&mut self, vis: &'v Visibility<'v>) {
379 fn visit_associated_item_kind(&mut self, kind: &'v AssocItemKind) {
380 walk_associated_item_kind(self, kind);
382 fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
383 walk_defaultness(self, defaultness);
387 /// Walks the contents of a crate. See also `Crate::visit_all_items`.
388 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) {
389 visitor.visit_mod(&krate.module, krate.span, CRATE_HIR_ID);
390 walk_list!(visitor, visit_attribute, krate.attrs);
391 walk_list!(visitor, visit_macro_def, krate.exported_macros);
394 pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef<'v>) {
395 visitor.visit_id(macro_def.hir_id);
396 visitor.visit_name(macro_def.span, macro_def.name);
397 walk_list!(visitor, visit_attribute, macro_def.attrs);
400 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod<'v>, mod_hir_id: HirId) {
401 visitor.visit_id(mod_hir_id);
402 for &item_id in module.item_ids {
403 visitor.visit_nested_item(item_id);
407 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body<'v>) {
408 walk_list!(visitor, visit_param, body.params);
409 visitor.visit_expr(&body.value);
412 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local<'v>) {
413 // Intentionally visiting the expr first - the initialization expr
414 // dominates the local's definition.
415 walk_list!(visitor, visit_expr, &local.init);
416 walk_list!(visitor, visit_attribute, local.attrs.iter());
417 visitor.visit_id(local.hir_id);
418 visitor.visit_pat(&local.pat);
419 walk_list!(visitor, visit_ty, &local.ty);
422 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
423 visitor.visit_name(ident.span, ident.name);
426 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
427 visitor.visit_ident(label.ident);
430 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
431 visitor.visit_id(lifetime.hir_id);
432 match lifetime.name {
433 LifetimeName::Param(ParamName::Plain(ident)) => {
434 visitor.visit_ident(ident);
436 LifetimeName::Param(ParamName::Fresh(_))
437 | LifetimeName::Param(ParamName::Error)
438 | LifetimeName::Static
439 | LifetimeName::Error
440 | LifetimeName::Implicit
441 | LifetimeName::ImplicitObjectLifetimeDefault
442 | LifetimeName::Underscore => {}
446 pub fn walk_poly_trait_ref<'v, V>(
448 trait_ref: &'v PolyTraitRef<'v>,
449 _modifier: TraitBoundModifier,
453 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
454 visitor.visit_trait_ref(&trait_ref.trait_ref);
457 pub fn walk_trait_ref<'v, V>(visitor: &mut V, trait_ref: &'v TraitRef<'v>)
461 visitor.visit_id(trait_ref.hir_ref_id);
462 visitor.visit_path(&trait_ref.path, trait_ref.hir_ref_id)
465 pub fn walk_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Param<'v>) {
466 visitor.visit_id(param.hir_id);
467 visitor.visit_pat(¶m.pat);
468 walk_list!(visitor, visit_attribute, param.attrs);
471 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
472 visitor.visit_vis(&item.vis);
473 visitor.visit_ident(item.ident);
475 ItemKind::ExternCrate(orig_name) => {
476 visitor.visit_id(item.hir_id);
477 if let Some(orig_name) = orig_name {
478 visitor.visit_name(item.span, orig_name);
481 ItemKind::Use(ref path, _) => {
482 visitor.visit_use(path, item.hir_id);
484 ItemKind::Static(ref typ, _, body) | ItemKind::Const(ref typ, body) => {
485 visitor.visit_id(item.hir_id);
486 visitor.visit_ty(typ);
487 visitor.visit_nested_body(body);
489 ItemKind::Fn(ref sig, ref generics, body_id) => visitor.visit_fn(
490 FnKind::ItemFn(item.ident, generics, sig.header, &item.vis, &item.attrs),
496 ItemKind::Mod(ref module) => {
497 // `visit_mod()` takes care of visiting the `Item`'s `HirId`.
498 visitor.visit_mod(module, item.span, item.hir_id)
500 ItemKind::ForeignMod(ref foreign_module) => {
501 visitor.visit_id(item.hir_id);
502 walk_list!(visitor, visit_foreign_item, foreign_module.items);
504 ItemKind::GlobalAsm(_) => {
505 visitor.visit_id(item.hir_id);
507 ItemKind::TyAlias(ref ty, ref generics) => {
508 visitor.visit_id(item.hir_id);
509 visitor.visit_ty(ty);
510 visitor.visit_generics(generics)
512 ItemKind::OpaqueTy(OpaqueTy { ref generics, ref bounds, .. }) => {
513 visitor.visit_id(item.hir_id);
514 walk_generics(visitor, generics);
515 walk_list!(visitor, visit_param_bound, bounds);
517 ItemKind::Enum(ref enum_definition, ref generics) => {
518 visitor.visit_generics(generics);
519 // `visit_enum_def()` takes care of visiting the `Item`'s `HirId`.
520 visitor.visit_enum_def(enum_definition, generics, item.hir_id, item.span)
522 ItemKind::Impl(.., ref generics, ref opt_trait_reference, ref typ, impl_item_refs) => {
523 visitor.visit_id(item.hir_id);
524 visitor.visit_generics(generics);
525 walk_list!(visitor, visit_trait_ref, opt_trait_reference);
526 visitor.visit_ty(typ);
527 walk_list!(visitor, visit_impl_item_ref, impl_item_refs);
529 ItemKind::Struct(ref struct_definition, ref generics)
530 | ItemKind::Union(ref struct_definition, ref generics) => {
531 visitor.visit_generics(generics);
532 visitor.visit_id(item.hir_id);
533 visitor.visit_variant_data(
541 ItemKind::Trait(.., ref generics, ref bounds, trait_item_refs) => {
542 visitor.visit_id(item.hir_id);
543 visitor.visit_generics(generics);
544 walk_list!(visitor, visit_param_bound, bounds);
545 walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
547 ItemKind::TraitAlias(ref generics, ref bounds) => {
548 visitor.visit_id(item.hir_id);
549 visitor.visit_generics(generics);
550 walk_list!(visitor, visit_param_bound, bounds);
553 walk_list!(visitor, visit_attribute, item.attrs);
556 pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>, hir_id: HirId) {
557 visitor.visit_id(hir_id);
558 visitor.visit_path(path, hir_id);
561 pub fn walk_enum_def<'v, V: Visitor<'v>>(
563 enum_definition: &'v EnumDef<'v>,
564 generics: &'v Generics<'v>,
567 visitor.visit_id(item_id);
568 walk_list!(visitor, visit_variant, enum_definition.variants, generics, item_id);
571 pub fn walk_variant<'v, V: Visitor<'v>>(
573 variant: &'v Variant<'v>,
574 generics: &'v Generics<'v>,
575 parent_item_id: HirId,
577 visitor.visit_ident(variant.ident);
578 visitor.visit_id(variant.id);
579 visitor.visit_variant_data(
586 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
587 walk_list!(visitor, visit_attribute, variant.attrs);
590 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
591 visitor.visit_id(typ.hir_id);
594 TyKind::Slice(ref ty) => visitor.visit_ty(ty),
595 TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
596 TyKind::Rptr(ref lifetime, ref mutable_type) => {
597 visitor.visit_lifetime(lifetime);
598 visitor.visit_ty(&mutable_type.ty)
601 TyKind::Tup(ref tuple_element_types) => {
602 walk_list!(visitor, visit_ty, tuple_element_types);
604 TyKind::BareFn(ref function_declaration) => {
605 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
606 visitor.visit_fn_decl(&function_declaration.decl);
608 TyKind::Path(ref qpath) => {
609 visitor.visit_qpath(qpath, typ.hir_id, typ.span);
611 TyKind::Def(item_id, ref lifetimes) => {
612 visitor.visit_nested_item(item_id);
613 walk_list!(visitor, visit_generic_arg, lifetimes);
615 TyKind::Array(ref ty, ref length) => {
616 visitor.visit_ty(ty);
617 visitor.visit_anon_const(length)
619 TyKind::TraitObject(ref bounds, ref lifetime) => {
620 for bound in bounds {
621 visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
623 visitor.visit_lifetime(lifetime);
625 TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
626 TyKind::Infer | TyKind::Err => {}
630 pub fn walk_qpath<'v, V: Visitor<'v>>(
632 qpath: &'v QPath<'v>,
637 QPath::Resolved(ref maybe_qself, ref path) => {
638 if let Some(ref qself) = *maybe_qself {
639 visitor.visit_ty(qself);
641 visitor.visit_path(path, id)
643 QPath::TypeRelative(ref qself, ref segment) => {
644 visitor.visit_ty(qself);
645 visitor.visit_path_segment(span, segment);
650 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>) {
651 for segment in &path.segments {
652 visitor.visit_path_segment(path.span, segment);
656 pub fn walk_path_segment<'v, V: Visitor<'v>>(
659 segment: &'v PathSegment<'v>,
661 visitor.visit_ident(segment.ident);
662 if let Some(id) = segment.hir_id {
663 visitor.visit_id(id);
665 if let Some(ref args) = segment.args {
666 visitor.visit_generic_args(path_span, args);
670 pub fn walk_generic_args<'v, V: Visitor<'v>>(
673 generic_args: &'v GenericArgs<'v>,
675 walk_list!(visitor, visit_generic_arg, &generic_args.args);
676 walk_list!(visitor, visit_assoc_type_binding, &generic_args.bindings);
679 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(
681 type_binding: &'v TypeBinding<'v>,
683 visitor.visit_id(type_binding.hir_id);
684 visitor.visit_ident(type_binding.ident);
685 match type_binding.kind {
686 TypeBindingKind::Equality { ref ty } => {
687 visitor.visit_ty(ty);
689 TypeBindingKind::Constraint { ref bounds } => {
690 walk_list!(visitor, visit_param_bound, bounds);
695 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) {
696 visitor.visit_id(pattern.hir_id);
698 PatKind::TupleStruct(ref qpath, children, _) => {
699 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
700 walk_list!(visitor, visit_pat, children);
702 PatKind::Path(ref qpath) => {
703 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
705 PatKind::Struct(ref qpath, fields, _) => {
706 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
707 for field in fields {
708 visitor.visit_id(field.hir_id);
709 visitor.visit_ident(field.ident);
710 visitor.visit_pat(&field.pat)
713 PatKind::Or(pats) => walk_list!(visitor, visit_pat, pats),
714 PatKind::Tuple(tuple_elements, _) => {
715 walk_list!(visitor, visit_pat, tuple_elements);
717 PatKind::Box(ref subpattern) | PatKind::Ref(ref subpattern, _) => {
718 visitor.visit_pat(subpattern)
720 PatKind::Binding(_, _hir_id, ident, ref optional_subpattern) => {
721 visitor.visit_ident(ident);
722 walk_list!(visitor, visit_pat, optional_subpattern);
724 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
725 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
726 visitor.visit_expr(lower_bound);
727 visitor.visit_expr(upper_bound)
730 PatKind::Slice(prepatterns, ref slice_pattern, postpatterns) => {
731 walk_list!(visitor, visit_pat, prepatterns);
732 walk_list!(visitor, visit_pat, slice_pattern);
733 walk_list!(visitor, visit_pat, postpatterns);
738 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem<'v>) {
739 visitor.visit_id(foreign_item.hir_id);
740 visitor.visit_vis(&foreign_item.vis);
741 visitor.visit_ident(foreign_item.ident);
743 match foreign_item.kind {
744 ForeignItemKind::Fn(ref function_declaration, param_names, ref generics) => {
745 visitor.visit_generics(generics);
746 visitor.visit_fn_decl(function_declaration);
747 for ¶m_name in param_names {
748 visitor.visit_ident(param_name);
751 ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
752 ForeignItemKind::Type => (),
755 walk_list!(visitor, visit_attribute, foreign_item.attrs);
758 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound<'v>) {
760 GenericBound::Trait(ref typ, modifier) => {
761 visitor.visit_poly_trait_ref(typ, modifier);
763 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
767 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam<'v>) {
768 visitor.visit_id(param.hir_id);
769 walk_list!(visitor, visit_attribute, ¶m.attrs);
771 ParamName::Plain(ident) => visitor.visit_ident(ident),
772 ParamName::Error | ParamName::Fresh(_) => {}
775 GenericParamKind::Lifetime { .. } => {}
776 GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
777 GenericParamKind::Const { ref ty } => visitor.visit_ty(ty),
779 walk_list!(visitor, visit_param_bound, ¶m.bounds);
782 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) {
783 walk_list!(visitor, visit_generic_param, &generics.params);
784 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
787 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
789 predicate: &'v WherePredicate<'v>,
792 &WherePredicate::BoundPredicate(WhereBoundPredicate {
795 ref bound_generic_params,
798 visitor.visit_ty(bounded_ty);
799 walk_list!(visitor, visit_param_bound, bounds);
800 walk_list!(visitor, visit_generic_param, bound_generic_params);
802 &WherePredicate::RegionPredicate(WhereRegionPredicate {
803 ref lifetime, ref bounds, ..
805 visitor.visit_lifetime(lifetime);
806 walk_list!(visitor, visit_param_bound, bounds);
808 &WherePredicate::EqPredicate(WhereEqPredicate {
809 hir_id, ref lhs_ty, ref rhs_ty, ..
811 visitor.visit_id(hir_id);
812 visitor.visit_ty(lhs_ty);
813 visitor.visit_ty(rhs_ty);
818 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy<'v>) {
819 if let Return(ref output_ty) = *ret_ty {
820 visitor.visit_ty(output_ty)
824 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl<'v>) {
825 for ty in &function_declaration.inputs {
828 walk_fn_ret_ty(visitor, &function_declaration.output)
831 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
832 match function_kind {
833 FnKind::ItemFn(_, generics, ..) => {
834 visitor.visit_generics(generics);
836 FnKind::Method(..) | FnKind::Closure(_) => {}
840 pub fn walk_fn<'v, V: Visitor<'v>>(
842 function_kind: FnKind<'v>,
843 function_declaration: &'v FnDecl<'v>,
848 visitor.visit_id(id);
849 visitor.visit_fn_decl(function_declaration);
850 walk_fn_kind(visitor, function_kind);
851 visitor.visit_nested_body(body_id)
854 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem<'v>) {
855 visitor.visit_ident(trait_item.ident);
856 walk_list!(visitor, visit_attribute, trait_item.attrs);
857 visitor.visit_generics(&trait_item.generics);
858 match trait_item.kind {
859 TraitItemKind::Const(ref ty, default) => {
860 visitor.visit_id(trait_item.hir_id);
861 visitor.visit_ty(ty);
862 walk_list!(visitor, visit_nested_body, default);
864 TraitItemKind::Method(ref sig, TraitMethod::Required(param_names)) => {
865 visitor.visit_id(trait_item.hir_id);
866 visitor.visit_fn_decl(&sig.decl);
867 for ¶m_name in param_names {
868 visitor.visit_ident(param_name);
871 TraitItemKind::Method(ref sig, TraitMethod::Provided(body_id)) => {
873 FnKind::Method(trait_item.ident, sig, None, &trait_item.attrs),
880 TraitItemKind::Type(ref bounds, ref default) => {
881 visitor.visit_id(trait_item.hir_id);
882 walk_list!(visitor, visit_param_bound, bounds);
883 walk_list!(visitor, visit_ty, default);
888 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
889 // N.B., deliberately force a compilation error if/when new fields are added.
890 let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
891 visitor.visit_nested_trait_item(id);
892 visitor.visit_ident(ident);
893 visitor.visit_associated_item_kind(kind);
894 visitor.visit_defaultness(defaultness);
897 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem<'v>) {
898 // N.B., deliberately force a compilation error if/when new fields are added.
910 visitor.visit_ident(ident);
911 visitor.visit_vis(vis);
912 visitor.visit_defaultness(defaultness);
913 walk_list!(visitor, visit_attribute, attrs);
914 visitor.visit_generics(generics);
916 ImplItemKind::Const(ref ty, body) => {
917 visitor.visit_id(impl_item.hir_id);
918 visitor.visit_ty(ty);
919 visitor.visit_nested_body(body);
921 ImplItemKind::Method(ref sig, body_id) => {
923 FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis), &impl_item.attrs),
930 ImplItemKind::TyAlias(ref ty) => {
931 visitor.visit_id(impl_item.hir_id);
932 visitor.visit_ty(ty);
934 ImplItemKind::OpaqueTy(ref bounds) => {
935 visitor.visit_id(impl_item.hir_id);
936 walk_list!(visitor, visit_param_bound, bounds);
941 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef<'v>) {
942 // N.B., deliberately force a compilation error if/when new fields are added.
943 let ImplItemRef { id, ident, ref kind, span: _, ref vis, ref defaultness } = *impl_item_ref;
944 visitor.visit_nested_impl_item(id);
945 visitor.visit_ident(ident);
946 visitor.visit_associated_item_kind(kind);
947 visitor.visit_vis(vis);
948 visitor.visit_defaultness(defaultness);
951 pub fn walk_struct_def<'v, V: Visitor<'v>>(
953 struct_definition: &'v VariantData<'v>,
955 if let Some(ctor_hir_id) = struct_definition.ctor_hir_id() {
956 visitor.visit_id(ctor_hir_id);
958 walk_list!(visitor, visit_struct_field, struct_definition.fields());
961 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField<'v>) {
962 visitor.visit_id(struct_field.hir_id);
963 visitor.visit_vis(&struct_field.vis);
964 visitor.visit_ident(struct_field.ident);
965 visitor.visit_ty(&struct_field.ty);
966 walk_list!(visitor, visit_attribute, struct_field.attrs);
969 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block<'v>) {
970 visitor.visit_id(block.hir_id);
971 walk_list!(visitor, visit_stmt, block.stmts);
972 walk_list!(visitor, visit_expr, &block.expr);
975 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt<'v>) {
976 visitor.visit_id(statement.hir_id);
977 match statement.kind {
978 StmtKind::Local(ref local) => visitor.visit_local(local),
979 StmtKind::Item(item) => visitor.visit_nested_item(item),
980 StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
981 visitor.visit_expr(expression)
986 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
987 visitor.visit_id(constant.hir_id);
988 visitor.visit_nested_body(constant.body);
991 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) {
992 visitor.visit_id(expression.hir_id);
993 walk_list!(visitor, visit_attribute, expression.attrs.iter());
994 match expression.kind {
995 ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
996 ExprKind::Array(subexpressions) => {
997 walk_list!(visitor, visit_expr, subexpressions);
999 ExprKind::Repeat(ref element, ref count) => {
1000 visitor.visit_expr(element);
1001 visitor.visit_anon_const(count)
1003 ExprKind::Struct(ref qpath, fields, ref optional_base) => {
1004 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1005 for field in fields {
1006 visitor.visit_id(field.hir_id);
1007 visitor.visit_ident(field.ident);
1008 visitor.visit_expr(&field.expr)
1010 walk_list!(visitor, visit_expr, optional_base);
1012 ExprKind::Tup(subexpressions) => {
1013 walk_list!(visitor, visit_expr, subexpressions);
1015 ExprKind::Call(ref callee_expression, arguments) => {
1016 visitor.visit_expr(callee_expression);
1017 walk_list!(visitor, visit_expr, arguments);
1019 ExprKind::MethodCall(ref segment, _, arguments) => {
1020 visitor.visit_path_segment(expression.span, segment);
1021 walk_list!(visitor, visit_expr, arguments);
1023 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
1024 visitor.visit_expr(left_expression);
1025 visitor.visit_expr(right_expression)
1027 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
1028 visitor.visit_expr(subexpression)
1030 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
1031 visitor.visit_expr(subexpression);
1032 visitor.visit_ty(typ)
1034 ExprKind::DropTemps(ref subexpression) => {
1035 visitor.visit_expr(subexpression);
1037 ExprKind::Loop(ref block, ref opt_label, _) => {
1038 walk_list!(visitor, visit_label, opt_label);
1039 visitor.visit_block(block);
1041 ExprKind::Match(ref subexpression, arms, _) => {
1042 visitor.visit_expr(subexpression);
1043 walk_list!(visitor, visit_arm, arms);
1045 ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => visitor
1047 FnKind::Closure(&expression.attrs),
1048 function_declaration,
1053 ExprKind::Block(ref block, ref opt_label) => {
1054 walk_list!(visitor, visit_label, opt_label);
1055 visitor.visit_block(block);
1057 ExprKind::Assign(ref lhs, ref rhs, _) => {
1058 visitor.visit_expr(rhs);
1059 visitor.visit_expr(lhs)
1061 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
1062 visitor.visit_expr(right_expression);
1063 visitor.visit_expr(left_expression);
1065 ExprKind::Field(ref subexpression, ident) => {
1066 visitor.visit_expr(subexpression);
1067 visitor.visit_ident(ident);
1069 ExprKind::Index(ref main_expression, ref index_expression) => {
1070 visitor.visit_expr(main_expression);
1071 visitor.visit_expr(index_expression)
1073 ExprKind::Path(ref qpath) => {
1074 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1076 ExprKind::Break(ref destination, ref opt_expr) => {
1077 if let Some(ref label) = destination.label {
1078 visitor.visit_label(label);
1080 walk_list!(visitor, visit_expr, opt_expr);
1082 ExprKind::Continue(ref destination) => {
1083 if let Some(ref label) = destination.label {
1084 visitor.visit_label(label);
1087 ExprKind::Ret(ref optional_expression) => {
1088 walk_list!(visitor, visit_expr, optional_expression);
1090 ExprKind::InlineAsm(ref asm) => {
1091 walk_list!(visitor, visit_expr, asm.outputs_exprs);
1092 walk_list!(visitor, visit_expr, asm.inputs_exprs);
1094 ExprKind::Yield(ref subexpression, _) => {
1095 visitor.visit_expr(subexpression);
1097 ExprKind::Lit(_) | ExprKind::Err => {}
1101 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm<'v>) {
1102 visitor.visit_id(arm.hir_id);
1103 visitor.visit_pat(&arm.pat);
1104 if let Some(ref g) = arm.guard {
1106 Guard::If(ref e) => visitor.visit_expr(e),
1109 visitor.visit_expr(&arm.body);
1110 walk_list!(visitor, visit_attribute, arm.attrs);
1113 pub fn walk_vis<'v, V: Visitor<'v>>(visitor: &mut V, vis: &'v Visibility<'v>) {
1114 if let VisibilityKind::Restricted { ref path, hir_id } = vis.node {
1115 visitor.visit_id(hir_id);
1116 visitor.visit_path(path, hir_id)
1120 pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssocItemKind) {
1121 // No visitable content here: this fn exists so you can call it if
1122 // the right thing to do, should content be added in the future,
1123 // would be to walk it.
1126 pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) {
1127 // No visitable content here: this fn exists so you can call it if
1128 // the right thing to do, should content be added in the future,
1129 // would be to walk it.