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 syntax::ast::{Ident, Name, Attribute};
37 use crate::hir::map::Map;
38 use super::itemlikevisit::DeepVisitor;
40 #[derive(Copy, Clone)]
42 /// `#[xxx] pub async/const/extern "Abi" fn foo()`
43 ItemFn(Ident, &'a Generics, FnHeader, &'a Visibility, &'a [Attribute]),
46 Method(Ident, &'a MethodSig, Option<&'a Visibility>, &'a [Attribute]),
49 Closure(&'a [Attribute]),
53 pub fn attrs(&self) -> &'a [Attribute] {
55 FnKind::ItemFn(.., attrs) => attrs,
56 FnKind::Method(.., attrs) => attrs,
57 FnKind::Closure(attrs) => attrs,
61 pub fn header(&self) -> Option<&FnHeader> {
63 FnKind::ItemFn(_, _, ref header, _, _) => Some(header),
64 FnKind::Method(_, ref sig, _, _) => Some(&sig.header),
65 FnKind::Closure(_) => None,
70 /// Specifies what nested things a visitor wants to visit. The most
71 /// common choice is `OnlyBodies`, which will cause the visitor to
72 /// visit fn bodies for fns that it encounters, but skip over nested
75 /// See the comments on `ItemLikeVisitor` for more details on the overall
77 pub enum NestedVisitorMap<'this, 'tcx: 'this> {
78 /// Do not visit any nested things. When you add a new
79 /// "non-nested" thing, you will want to audit such uses to see if
80 /// they remain valid.
82 /// Use this if you are only walking some particular kind of tree
83 /// (i.e., a type, or fn signature) and you don't want to thread a
87 /// Do not visit nested item-like things, but visit nested things
88 /// that are inside of an item-like.
90 /// **This is the most common choice.** A very common pattern is
91 /// to use `visit_all_item_likes()` as an outer loop,
92 /// and to have the visitor that visits the contents of each item
93 /// using this setting.
94 OnlyBodies(&'this Map<'tcx>),
96 /// Visits all nested things, including item-likes.
98 /// **This is an unusual choice.** It is used when you want to
99 /// process everything within their lexical context. Typically you
100 /// kick off the visit by doing `walk_krate()`.
101 All(&'this Map<'tcx>),
104 impl<'this, 'tcx> NestedVisitorMap<'this, 'tcx> {
105 /// Returns the map to use for an "intra item-like" thing (if any).
106 /// E.g., function body.
107 pub fn intra(self) -> Option<&'this Map<'tcx>> {
109 NestedVisitorMap::None => None,
110 NestedVisitorMap::OnlyBodies(map) => Some(map),
111 NestedVisitorMap::All(map) => Some(map),
115 /// Returns the map to use for an "item-like" thing (if any).
116 /// E.g., item, impl-item.
117 pub fn inter(self) -> Option<&'this Map<'tcx>> {
119 NestedVisitorMap::None => None,
120 NestedVisitorMap::OnlyBodies(_) => None,
121 NestedVisitorMap::All(map) => Some(map),
126 /// Each method of the Visitor trait is a hook to be potentially
127 /// overridden. Each method's default implementation recursively visits
128 /// the substructure of the input via the corresponding `walk` method;
129 /// e.g., the `visit_mod` method by default calls `intravisit::walk_mod`.
131 /// Note that this visitor does NOT visit nested items by default
132 /// (this is why the module is called `intravisit`, to distinguish it
133 /// from the AST's `visit` module, which acts differently). If you
134 /// simply want to visit all items in the crate in some order, you
135 /// should call `Crate::visit_all_items`. Otherwise, see the comment
136 /// on `visit_nested_item` for details on how to visit nested items.
138 /// If you want to ensure that your code handles every variant
139 /// explicitly, you need to override each method. (And you also need
140 /// to monitor future changes to `Visitor` in case a new method with a
141 /// new default implementation gets introduced.)
142 pub trait Visitor<'v> : Sized {
143 ///////////////////////////////////////////////////////////////////////////
146 /// The default versions of the `visit_nested_XXX` routines invoke
147 /// this method to get a map to use. By selecting an enum variant,
148 /// you control which kinds of nested HIR are visited; see
149 /// `NestedVisitorMap` for details. By "nested HIR", we are
150 /// referring to bits of HIR that are not directly embedded within
151 /// one another but rather indirectly, through a table in the
152 /// crate. This is done to control dependencies during incremental
153 /// compilation: the non-inline bits of HIR can be tracked and
154 /// hashed separately.
156 /// **If for some reason you want the nested behavior, but don't
157 /// have a `Map` at your disposal:** then you should override the
158 /// `visit_nested_XXX` methods, and override this method to
159 /// `panic!()`. This way, if a new `visit_nested_XXX` variant is
160 /// added in the future, we will see the panic in your code and
161 /// fix it appropriately.
162 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v>;
164 /// Invoked when a nested item is encountered. By default does
165 /// nothing unless you override `nested_visit_map` to return
166 /// `Some(_)`, in which case it will walk the item. **You probably
167 /// don't want to override this method** -- instead, override
168 /// `nested_visit_map` or use the "shallow" or "deep" visit
169 /// patterns described on `itemlikevisit::ItemLikeVisitor`. The only
170 /// reason to override this method is if you want a nested pattern
171 /// but cannot supply a `Map`; see `nested_visit_map` for advice.
172 #[allow(unused_variables)]
173 fn visit_nested_item(&mut self, id: ItemId) {
174 let opt_item = self.nested_visit_map().inter().map(|map| map.expect_item_by_hir_id(id.id));
175 if let Some(item) = opt_item {
176 self.visit_item(item);
180 /// Like `visit_nested_item()`, but for trait items. See
181 /// `visit_nested_item()` for advice on when to override this
183 #[allow(unused_variables)]
184 fn visit_nested_trait_item(&mut self, id: TraitItemId) {
185 let opt_item = self.nested_visit_map().inter().map(|map| map.trait_item(id));
186 if let Some(item) = opt_item {
187 self.visit_trait_item(item);
191 /// Like `visit_nested_item()`, but for impl items. See
192 /// `visit_nested_item()` for advice on when to override this
194 #[allow(unused_variables)]
195 fn visit_nested_impl_item(&mut self, id: ImplItemId) {
196 let opt_item = self.nested_visit_map().inter().map(|map| map.impl_item(id));
197 if let Some(item) = opt_item {
198 self.visit_impl_item(item);
202 /// Invoked to visit the body of a function, method or closure. Like
203 /// visit_nested_item, does nothing by default unless you override
204 /// `nested_visit_map` to return `Some(_)`, in which case it will walk the
206 fn visit_nested_body(&mut self, id: BodyId) {
207 let opt_body = self.nested_visit_map().intra().map(|map| map.body(id));
208 if let Some(body) = opt_body {
209 self.visit_body(body);
213 /// Visits the top-level item and (optionally) nested items / impl items. See
214 /// `visit_nested_item` for details.
215 fn visit_item(&mut self, i: &'v Item) {
219 fn visit_body(&mut self, b: &'v Body) {
223 /// When invoking `visit_all_item_likes()`, you need to supply an
224 /// item-like visitor. This method converts a "intra-visit"
225 /// visitor into an item-like visitor that walks the entire tree.
226 /// If you use this, you probably don't want to process the
227 /// contents of nested item-like things, since the outer loop will
228 /// visit them as well.
229 fn as_deep_visitor<'s>(&'s mut self) -> DeepVisitor<'s, Self> {
230 DeepVisitor::new(self)
233 ///////////////////////////////////////////////////////////////////////////
235 fn visit_id(&mut self, _hir_id: HirId) {
238 fn visit_name(&mut self, _span: Span, _name: Name) {
241 fn visit_ident(&mut self, ident: Ident) {
242 walk_ident(self, ident)
244 fn visit_mod(&mut self, m: &'v Mod, _s: Span, n: HirId) {
247 fn visit_foreign_item(&mut self, i: &'v ForeignItem) {
248 walk_foreign_item(self, i)
250 fn visit_local(&mut self, l: &'v Local) {
253 fn visit_block(&mut self, b: &'v Block) {
256 fn visit_stmt(&mut self, s: &'v Stmt) {
259 fn visit_arm(&mut self, a: &'v Arm) {
262 fn visit_pat(&mut self, p: &'v Pat) {
265 fn visit_argument_source(&mut self, s: &'v ArgSource) {
266 walk_argument_source(self, s)
268 fn visit_anon_const(&mut self, c: &'v AnonConst) {
269 walk_anon_const(self, c)
271 fn visit_expr(&mut self, ex: &'v Expr) {
274 fn visit_ty(&mut self, t: &'v Ty) {
277 fn visit_generic_param(&mut self, p: &'v GenericParam) {
278 walk_generic_param(self, p)
280 fn visit_generics(&mut self, g: &'v Generics) {
281 walk_generics(self, g)
283 fn visit_where_predicate(&mut self, predicate: &'v WherePredicate) {
284 walk_where_predicate(self, predicate)
286 fn visit_fn_decl(&mut self, fd: &'v FnDecl) {
287 walk_fn_decl(self, fd)
289 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: BodyId, s: Span, id: HirId) {
290 walk_fn(self, fk, fd, b, s, id)
292 fn visit_use(&mut self, path: &'v Path, hir_id: HirId) {
293 walk_use(self, path, hir_id)
295 fn visit_trait_item(&mut self, ti: &'v TraitItem) {
296 walk_trait_item(self, ti)
298 fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
299 walk_trait_item_ref(self, ii)
301 fn visit_impl_item(&mut self, ii: &'v ImplItem) {
302 walk_impl_item(self, ii)
304 fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) {
305 walk_impl_item_ref(self, ii)
307 fn visit_trait_ref(&mut self, t: &'v TraitRef) {
308 walk_trait_ref(self, t)
310 fn visit_param_bound(&mut self, bounds: &'v GenericBound) {
311 walk_param_bound(self, bounds)
313 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: TraitBoundModifier) {
314 walk_poly_trait_ref(self, t, m)
316 fn visit_variant_data(&mut self,
322 walk_struct_def(self, s)
324 fn visit_struct_field(&mut self, s: &'v StructField) {
325 walk_struct_field(self, s)
327 fn visit_enum_def(&mut self,
328 enum_definition: &'v EnumDef,
329 generics: &'v Generics,
332 walk_enum_def(self, enum_definition, generics, item_id)
334 fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics, item_id: HirId) {
335 walk_variant(self, v, g, item_id)
337 fn visit_label(&mut self, label: &'v Label) {
338 walk_label(self, label)
340 fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg) {
342 GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
343 GenericArg::Type(ty) => self.visit_ty(ty),
344 GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
347 fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
348 walk_lifetime(self, lifetime)
350 fn visit_qpath(&mut self, qpath: &'v QPath, id: HirId, span: Span) {
351 walk_qpath(self, qpath, id, span)
353 fn visit_path(&mut self, path: &'v Path, _id: HirId) {
354 walk_path(self, path)
356 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
357 walk_path_segment(self, path_span, path_segment)
359 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs) {
360 walk_generic_args(self, path_span, generic_args)
362 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
363 walk_assoc_type_binding(self, type_binding)
365 fn visit_attribute(&mut self, _attr: &'v Attribute) {
367 fn visit_macro_def(&mut self, macro_def: &'v MacroDef) {
368 walk_macro_def(self, macro_def)
370 fn visit_vis(&mut self, vis: &'v Visibility) {
373 fn visit_associated_item_kind(&mut self, kind: &'v AssociatedItemKind) {
374 walk_associated_item_kind(self, kind);
376 fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
377 walk_defaultness(self, defaultness);
381 /// Walks the contents of a crate. See also `Crate::visit_all_items`.
382 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
383 visitor.visit_mod(&krate.module, krate.span, CRATE_HIR_ID);
384 walk_list!(visitor, visit_attribute, &krate.attrs);
385 walk_list!(visitor, visit_macro_def, &krate.exported_macros);
388 pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef) {
389 visitor.visit_id(macro_def.hir_id);
390 visitor.visit_name(macro_def.span, macro_def.name);
391 walk_list!(visitor, visit_attribute, ¯o_def.attrs);
394 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod, mod_hir_id: HirId) {
395 visitor.visit_id(mod_hir_id);
396 for &item_id in &module.item_ids {
397 visitor.visit_nested_item(item_id);
401 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body) {
402 for argument in &body.arguments {
403 visitor.visit_id(argument.hir_id);
404 visitor.visit_pat(&argument.pat);
405 visitor.visit_argument_source(&argument.source);
407 visitor.visit_expr(&body.value);
410 pub fn walk_argument_source<'v, V: Visitor<'v>>(visitor: &mut V, source: &'v ArgSource) {
411 if let ArgSource::AsyncFn(pat) = source {
412 visitor.visit_pat(pat);
416 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
417 // Intentionally visiting the expr first - the initialization expr
418 // dominates the local's definition.
419 walk_list!(visitor, visit_expr, &local.init);
420 walk_list!(visitor, visit_attribute, local.attrs.iter());
421 visitor.visit_id(local.hir_id);
422 visitor.visit_pat(&local.pat);
423 walk_list!(visitor, visit_ty, &local.ty);
426 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
427 visitor.visit_name(ident.span, ident.name);
430 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
431 visitor.visit_ident(label.ident);
434 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
435 visitor.visit_id(lifetime.hir_id);
436 match lifetime.name {
437 LifetimeName::Param(ParamName::Plain(ident)) => {
438 visitor.visit_ident(ident);
440 LifetimeName::Param(ParamName::Fresh(_)) |
441 LifetimeName::Param(ParamName::Error) |
442 LifetimeName::Static |
443 LifetimeName::Error |
444 LifetimeName::Implicit |
445 LifetimeName::Underscore => {}
449 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
450 trait_ref: &'v PolyTraitRef,
451 _modifier: TraitBoundModifier)
454 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
455 visitor.visit_trait_ref(&trait_ref.trait_ref);
458 pub fn walk_trait_ref<'v, V>(visitor: &mut V, trait_ref: &'v TraitRef)
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_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
466 visitor.visit_vis(&item.vis);
467 visitor.visit_ident(item.ident);
469 ItemKind::ExternCrate(orig_name) => {
470 visitor.visit_id(item.hir_id);
471 if let Some(orig_name) = orig_name {
472 visitor.visit_name(item.span, orig_name);
475 ItemKind::Use(ref path, _) => {
476 visitor.visit_use(path, item.hir_id);
478 ItemKind::Static(ref typ, _, body) |
479 ItemKind::Const(ref typ, body) => {
480 visitor.visit_id(item.hir_id);
481 visitor.visit_ty(typ);
482 visitor.visit_nested_body(body);
484 ItemKind::Fn(ref declaration, header, ref generics, body_id) => {
485 visitor.visit_fn(FnKind::ItemFn(item.ident,
495 ItemKind::Mod(ref module) => {
496 // `visit_mod()` takes care of visiting the `Item`'s `HirId`.
497 visitor.visit_mod(module, item.span, item.hir_id)
499 ItemKind::ForeignMod(ref foreign_module) => {
500 visitor.visit_id(item.hir_id);
501 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
503 ItemKind::GlobalAsm(_) => {
504 visitor.visit_id(item.hir_id);
506 ItemKind::Ty(ref ty, ref generics) => {
507 visitor.visit_id(item.hir_id);
508 visitor.visit_ty(ty);
509 visitor.visit_generics(generics)
511 ItemKind::Existential(ExistTy {
516 visitor.visit_id(item.hir_id);
517 walk_generics(visitor, generics);
518 walk_list!(visitor, visit_param_bound, bounds);
520 ItemKind::Enum(ref enum_definition, ref generics) => {
521 visitor.visit_generics(generics);
522 // `visit_enum_def()` takes care of visiting the `Item`'s `HirId`.
523 visitor.visit_enum_def(enum_definition, generics, item.hir_id, item.span)
528 ref opt_trait_reference,
532 visitor.visit_id(item.hir_id);
533 visitor.visit_generics(generics);
534 walk_list!(visitor, visit_trait_ref, opt_trait_reference);
535 visitor.visit_ty(typ);
536 walk_list!(visitor, visit_impl_item_ref, impl_item_refs);
538 ItemKind::Struct(ref struct_definition, ref generics) |
539 ItemKind::Union(ref struct_definition, ref generics) => {
540 visitor.visit_generics(generics);
541 visitor.visit_id(item.hir_id);
542 visitor.visit_variant_data(struct_definition, item.ident.name, generics, item.hir_id,
545 ItemKind::Trait(.., ref generics, ref bounds, ref trait_item_refs) => {
546 visitor.visit_id(item.hir_id);
547 visitor.visit_generics(generics);
548 walk_list!(visitor, visit_param_bound, bounds);
549 walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
551 ItemKind::TraitAlias(ref generics, ref bounds) => {
552 visitor.visit_id(item.hir_id);
553 visitor.visit_generics(generics);
554 walk_list!(visitor, visit_param_bound, bounds);
557 walk_list!(visitor, visit_attribute, &item.attrs);
560 pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V,
563 visitor.visit_id(hir_id);
564 visitor.visit_path(path, hir_id);
567 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
568 enum_definition: &'v EnumDef,
569 generics: &'v Generics,
571 visitor.visit_id(item_id);
574 &enum_definition.variants,
579 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
580 variant: &'v Variant,
581 generics: &'v Generics,
582 parent_item_id: HirId) {
583 visitor.visit_ident(variant.node.ident);
584 visitor.visit_id(variant.node.id);
585 visitor.visit_variant_data(&variant.node.data,
586 variant.node.ident.name,
590 walk_list!(visitor, visit_anon_const, &variant.node.disr_expr);
591 walk_list!(visitor, visit_attribute, &variant.node.attrs);
594 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
595 visitor.visit_id(typ.hir_id);
598 TyKind::Slice(ref ty) => {
601 TyKind::Ptr(ref mutable_type) => {
602 visitor.visit_ty(&mutable_type.ty)
604 TyKind::Rptr(ref lifetime, ref mutable_type) => {
605 visitor.visit_lifetime(lifetime);
606 visitor.visit_ty(&mutable_type.ty)
609 TyKind::Tup(ref tuple_element_types) => {
610 walk_list!(visitor, visit_ty, tuple_element_types);
612 TyKind::BareFn(ref function_declaration) => {
613 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
614 visitor.visit_fn_decl(&function_declaration.decl);
616 TyKind::Path(ref qpath) => {
617 visitor.visit_qpath(qpath, typ.hir_id, typ.span);
619 TyKind::Def(item_id, ref lifetimes) => {
620 visitor.visit_nested_item(item_id);
621 walk_list!(visitor, visit_generic_arg, lifetimes);
623 TyKind::Array(ref ty, ref length) => {
624 visitor.visit_ty(ty);
625 visitor.visit_anon_const(length)
627 TyKind::TraitObject(ref bounds, ref lifetime) => {
628 for bound in bounds {
629 visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
631 visitor.visit_lifetime(lifetime);
633 TyKind::Typeof(ref expression) => {
634 visitor.visit_anon_const(expression)
636 TyKind::CVarArgs(ref lt) => {
637 visitor.visit_lifetime(lt)
639 TyKind::Infer | TyKind::Err => {}
643 pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath, id: HirId, span: Span) {
645 QPath::Resolved(ref maybe_qself, ref path) => {
646 if let Some(ref qself) = *maybe_qself {
647 visitor.visit_ty(qself);
649 visitor.visit_path(path, id)
651 QPath::TypeRelative(ref qself, ref segment) => {
652 visitor.visit_ty(qself);
653 visitor.visit_path_segment(span, segment);
658 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
659 for segment in &path.segments {
660 visitor.visit_path_segment(path.span, segment);
664 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
666 segment: &'v PathSegment) {
667 visitor.visit_ident(segment.ident);
668 if let Some(id) = segment.hir_id {
669 visitor.visit_id(id);
671 if let Some(ref args) = segment.args {
672 visitor.visit_generic_args(path_span, args);
676 pub fn walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V,
678 generic_args: &'v GenericArgs) {
679 walk_list!(visitor, visit_generic_arg, &generic_args.args);
680 walk_list!(visitor, visit_assoc_type_binding, &generic_args.bindings);
683 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
684 type_binding: &'v TypeBinding) {
685 visitor.visit_id(type_binding.hir_id);
686 visitor.visit_ident(type_binding.ident);
687 visitor.visit_ty(&type_binding.ty);
690 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
691 visitor.visit_id(pattern.hir_id);
693 PatKind::TupleStruct(ref qpath, ref children, _) => {
694 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
695 walk_list!(visitor, visit_pat, children);
697 PatKind::Path(ref qpath) => {
698 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
700 PatKind::Struct(ref qpath, ref fields, _) => {
701 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
702 for field in fields {
703 visitor.visit_id(field.node.hir_id);
704 visitor.visit_ident(field.node.ident);
705 visitor.visit_pat(&field.node.pat)
708 PatKind::Tuple(ref tuple_elements, _) => {
709 walk_list!(visitor, visit_pat, tuple_elements);
711 PatKind::Box(ref subpattern) |
712 PatKind::Ref(ref subpattern, _) => {
713 visitor.visit_pat(subpattern)
715 PatKind::Binding(_, _hir_id, ident, ref optional_subpattern) => {
716 visitor.visit_ident(ident);
717 walk_list!(visitor, visit_pat, optional_subpattern);
719 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
720 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
721 visitor.visit_expr(lower_bound);
722 visitor.visit_expr(upper_bound)
725 PatKind::Slice(ref prepatterns, ref slice_pattern, ref postpatterns) => {
726 walk_list!(visitor, visit_pat, prepatterns);
727 walk_list!(visitor, visit_pat, slice_pattern);
728 walk_list!(visitor, visit_pat, postpatterns);
733 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem) {
734 visitor.visit_id(foreign_item.hir_id);
735 visitor.visit_vis(&foreign_item.vis);
736 visitor.visit_ident(foreign_item.ident);
738 match foreign_item.node {
739 ForeignItemKind::Fn(ref function_declaration, ref param_names, ref generics) => {
740 visitor.visit_generics(generics);
741 visitor.visit_fn_decl(function_declaration);
742 for ¶m_name in param_names {
743 visitor.visit_ident(param_name);
746 ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
747 ForeignItemKind::Type => (),
750 walk_list!(visitor, visit_attribute, &foreign_item.attrs);
753 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound) {
755 GenericBound::Trait(ref typ, modifier) => {
756 visitor.visit_poly_trait_ref(typ, modifier);
758 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
762 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam) {
763 visitor.visit_id(param.hir_id);
764 walk_list!(visitor, visit_attribute, ¶m.attrs);
766 ParamName::Plain(ident) => visitor.visit_ident(ident),
767 ParamName::Error | ParamName::Fresh(_) => {}
770 GenericParamKind::Lifetime { .. } => {}
771 GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
772 GenericParamKind::Const { ref ty } => visitor.visit_ty(ty),
774 walk_list!(visitor, visit_param_bound, ¶m.bounds);
777 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
778 walk_list!(visitor, visit_generic_param, &generics.params);
779 visitor.visit_id(generics.where_clause.hir_id);
780 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
783 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
785 predicate: &'v WherePredicate)
788 &WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
790 ref bound_generic_params,
792 visitor.visit_ty(bounded_ty);
793 walk_list!(visitor, visit_param_bound, bounds);
794 walk_list!(visitor, visit_generic_param, bound_generic_params);
796 &WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
799 visitor.visit_lifetime(lifetime);
800 walk_list!(visitor, visit_param_bound, bounds);
802 &WherePredicate::EqPredicate(WhereEqPredicate{hir_id,
806 visitor.visit_id(hir_id);
807 visitor.visit_ty(lhs_ty);
808 visitor.visit_ty(rhs_ty);
813 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
814 if let Return(ref output_ty) = *ret_ty {
815 visitor.visit_ty(output_ty)
819 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
820 for ty in &function_declaration.inputs {
823 walk_fn_ret_ty(visitor, &function_declaration.output)
826 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
827 match function_kind {
828 FnKind::ItemFn(_, generics, ..) => {
829 visitor.visit_generics(generics);
832 FnKind::Closure(_) => {}
836 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
837 function_kind: FnKind<'v>,
838 function_declaration: &'v FnDecl,
842 visitor.visit_id(id);
843 visitor.visit_fn_decl(function_declaration);
844 walk_fn_kind(visitor, function_kind);
845 visitor.visit_nested_body(body_id)
848 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
849 visitor.visit_ident(trait_item.ident);
850 walk_list!(visitor, visit_attribute, &trait_item.attrs);
851 visitor.visit_generics(&trait_item.generics);
852 match trait_item.node {
853 TraitItemKind::Const(ref ty, default) => {
854 visitor.visit_id(trait_item.hir_id);
855 visitor.visit_ty(ty);
856 walk_list!(visitor, visit_nested_body, default);
858 TraitItemKind::Method(ref sig, TraitMethod::Required(ref param_names)) => {
859 visitor.visit_id(trait_item.hir_id);
860 visitor.visit_fn_decl(&sig.decl);
861 for ¶m_name in param_names {
862 visitor.visit_ident(param_name);
865 TraitItemKind::Method(ref sig, TraitMethod::Provided(body_id)) => {
866 visitor.visit_fn(FnKind::Method(trait_item.ident,
875 TraitItemKind::Type(ref bounds, ref default) => {
876 visitor.visit_id(trait_item.hir_id);
877 walk_list!(visitor, visit_param_bound, bounds);
878 walk_list!(visitor, visit_ty, default);
883 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
884 // N.B., deliberately force a compilation error if/when new fields are added.
885 let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
886 visitor.visit_nested_trait_item(id);
887 visitor.visit_ident(ident);
888 visitor.visit_associated_item_kind(kind);
889 visitor.visit_defaultness(defaultness);
892 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
893 // N.B., deliberately force a compilation error if/when new fields are added.
905 visitor.visit_ident(ident);
906 visitor.visit_vis(vis);
907 visitor.visit_defaultness(defaultness);
908 walk_list!(visitor, visit_attribute, attrs);
909 visitor.visit_generics(generics);
911 ImplItemKind::Const(ref ty, body) => {
912 visitor.visit_id(impl_item.hir_id);
913 visitor.visit_ty(ty);
914 visitor.visit_nested_body(body);
916 ImplItemKind::Method(ref sig, body_id) => {
917 visitor.visit_fn(FnKind::Method(impl_item.ident,
919 Some(&impl_item.vis),
926 ImplItemKind::Type(ref ty) => {
927 visitor.visit_id(impl_item.hir_id);
928 visitor.visit_ty(ty);
930 ImplItemKind::Existential(ref bounds) => {
931 visitor.visit_id(impl_item.hir_id);
932 walk_list!(visitor, visit_param_bound, bounds);
937 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
938 // N.B., deliberately force a compilation error if/when new fields are added.
939 let ImplItemRef { id, ident, ref kind, span: _, ref vis, ref defaultness } = *impl_item_ref;
940 visitor.visit_nested_impl_item(id);
941 visitor.visit_ident(ident);
942 visitor.visit_associated_item_kind(kind);
943 visitor.visit_vis(vis);
944 visitor.visit_defaultness(defaultness);
948 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &'v VariantData) {
949 if let Some(ctor_hir_id) = struct_definition.ctor_hir_id() {
950 visitor.visit_id(ctor_hir_id);
952 walk_list!(visitor, visit_struct_field, struct_definition.fields());
955 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) {
956 visitor.visit_id(struct_field.hir_id);
957 visitor.visit_vis(&struct_field.vis);
958 visitor.visit_ident(struct_field.ident);
959 visitor.visit_ty(&struct_field.ty);
960 walk_list!(visitor, visit_attribute, &struct_field.attrs);
963 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
964 visitor.visit_id(block.hir_id);
965 walk_list!(visitor, visit_stmt, &block.stmts);
966 walk_list!(visitor, visit_expr, &block.expr);
969 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
970 visitor.visit_id(statement.hir_id);
971 match statement.node {
972 StmtKind::Local(ref local) => visitor.visit_local(local),
973 StmtKind::Item(item) => visitor.visit_nested_item(item),
974 StmtKind::Expr(ref expression) |
975 StmtKind::Semi(ref expression) => {
976 visitor.visit_expr(expression)
981 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
982 visitor.visit_id(constant.hir_id);
983 visitor.visit_nested_body(constant.body);
986 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
987 visitor.visit_id(expression.hir_id);
988 walk_list!(visitor, visit_attribute, expression.attrs.iter());
989 match expression.node {
990 ExprKind::Box(ref subexpression) => {
991 visitor.visit_expr(subexpression)
993 ExprKind::Array(ref subexpressions) => {
994 walk_list!(visitor, visit_expr, subexpressions);
996 ExprKind::Repeat(ref element, ref count) => {
997 visitor.visit_expr(element);
998 visitor.visit_anon_const(count)
1000 ExprKind::Struct(ref qpath, ref fields, ref optional_base) => {
1001 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1002 for field in fields {
1003 visitor.visit_id(field.hir_id);
1004 visitor.visit_ident(field.ident);
1005 visitor.visit_expr(&field.expr)
1007 walk_list!(visitor, visit_expr, optional_base);
1009 ExprKind::Tup(ref subexpressions) => {
1010 walk_list!(visitor, visit_expr, subexpressions);
1012 ExprKind::Call(ref callee_expression, ref arguments) => {
1013 visitor.visit_expr(callee_expression);
1014 walk_list!(visitor, visit_expr, arguments);
1016 ExprKind::MethodCall(ref segment, _, ref arguments) => {
1017 visitor.visit_path_segment(expression.span, segment);
1018 walk_list!(visitor, visit_expr, arguments);
1020 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
1021 visitor.visit_expr(left_expression);
1022 visitor.visit_expr(right_expression)
1024 ExprKind::AddrOf(_, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
1025 visitor.visit_expr(subexpression)
1027 ExprKind::Lit(_) => {}
1028 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
1029 visitor.visit_expr(subexpression);
1030 visitor.visit_ty(typ)
1032 ExprKind::DropTemps(ref subexpression) => {
1033 visitor.visit_expr(subexpression);
1035 ExprKind::While(ref subexpression, ref block, ref opt_label) => {
1036 walk_list!(visitor, visit_label, opt_label);
1037 visitor.visit_expr(subexpression);
1038 visitor.visit_block(block);
1040 ExprKind::Loop(ref block, ref opt_label, _) => {
1041 walk_list!(visitor, visit_label, opt_label);
1042 visitor.visit_block(block);
1044 ExprKind::Match(ref subexpression, ref arms, _) => {
1045 visitor.visit_expr(subexpression);
1046 walk_list!(visitor, visit_arm, arms);
1048 ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => {
1049 visitor.visit_fn(FnKind::Closure(&expression.attrs),
1050 function_declaration,
1055 ExprKind::Block(ref block, ref opt_label) => {
1056 walk_list!(visitor, visit_label, opt_label);
1057 visitor.visit_block(block);
1059 ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
1060 visitor.visit_expr(right_hand_expression);
1061 visitor.visit_expr(left_hand_expression)
1063 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
1064 visitor.visit_expr(right_expression);
1065 visitor.visit_expr(left_expression)
1067 ExprKind::Field(ref subexpression, ident) => {
1068 visitor.visit_expr(subexpression);
1069 visitor.visit_ident(ident);
1071 ExprKind::Index(ref main_expression, ref index_expression) => {
1072 visitor.visit_expr(main_expression);
1073 visitor.visit_expr(index_expression)
1075 ExprKind::Path(ref qpath) => {
1076 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1078 ExprKind::Break(ref destination, ref opt_expr) => {
1079 if let Some(ref label) = destination.label {
1080 visitor.visit_label(label);
1082 walk_list!(visitor, visit_expr, opt_expr);
1084 ExprKind::Continue(ref destination) => {
1085 if let Some(ref label) = destination.label {
1086 visitor.visit_label(label);
1089 ExprKind::Ret(ref optional_expression) => {
1090 walk_list!(visitor, visit_expr, optional_expression);
1092 ExprKind::InlineAsm(_, ref outputs, ref inputs) => {
1093 for expr in outputs.iter().chain(inputs.iter()) {
1094 visitor.visit_expr(expr)
1097 ExprKind::Yield(ref subexpression) => {
1098 visitor.visit_expr(subexpression);
1104 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
1105 visitor.visit_id(arm.hir_id);
1106 walk_list!(visitor, visit_pat, &arm.pats);
1107 if let Some(ref g) = arm.guard {
1109 Guard::If(ref e) => visitor.visit_expr(e),
1112 visitor.visit_expr(&arm.body);
1113 walk_list!(visitor, visit_attribute, &arm.attrs);
1116 pub fn walk_vis<'v, V: Visitor<'v>>(visitor: &mut V, vis: &'v Visibility) {
1117 if let VisibilityKind::Restricted { ref path, hir_id } = vis.node {
1118 visitor.visit_id(hir_id);
1119 visitor.visit_path(path, hir_id)
1123 pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssociatedItemKind) {
1124 // No visitable content here: this fn exists so you can call it if
1125 // the right thing to do, should content be added in the future,
1126 // would be to walk it.
1129 pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) {
1130 // No visitable content here: this fn exists so you can call it if
1131 // the right thing to do, should content be added in the future,
1132 // would be to walk it.