1 //! HIR walker for walking the contents of nodes.
2 //!
3 //! Here are the three available patterns for the visitor strategy,
4 //! in roughly the order of desirability:
5 //!
6 //! 1. **Shallow visit**: Get a simple callback for every item (or item-like thing) in the HIR.
7 //! - Example: find all items with a `#[foo]` attribute on them.
8 //! - How: Use the `hir_crate_items` or `hir_module_items` query to traverse over item-like ids
9 //! (ItemId, TraitItemId, etc.) and use tcx.def_kind and `tcx.hir().item*(id)` to filter and
10 //! access actual item-like thing, respectively.
11 //! - Pro: Efficient; just walks the lists of item ids and gives users control whether to access
12 //! the hir_owners themselves or not.
13 //! - Con: Don't get information about nesting
14 //! - Con: Don't have methods for specific bits of HIR, like "on
15 //! every expr, do this".
16 //! 2. **Deep visit**: Want to scan for specific kinds of HIR nodes within
17 //! an item, but don't care about how item-like things are nested
18 //! within one another.
19 //! - Example: Examine each expression to look for its type and do some check or other.
20 //! - How: Implement `intravisit::Visitor` and override the `NestedFilter` type to
21 //! `nested_filter::OnlyBodies` (and implement `nested_visit_map`), and use
22 //! `tcx.hir().visit_all_item_likes_in_crate(&mut visitor)`. Within your
23 //! `intravisit::Visitor` impl, implement methods like `visit_expr()` (don't forget to invoke
24 //! `intravisit::walk_expr()` to keep walking the subparts).
25 //! - Pro: Visitor methods for any kind of HIR node, not just item-like things.
26 //! - Pro: Integrates well into dependency tracking.
27 //! - Con: Don't get information about nesting between items
28 //! 3. **Nested visit**: Want to visit the whole HIR and you care about the nesting between
29 //! item-like things.
30 //! - Example: Lifetime resolution, which wants to bring lifetimes declared on the
31 //! impl into scope while visiting the impl-items, and then back out again.
32 //! - How: Implement `intravisit::Visitor` and override the `NestedFilter` type to
33 //! `nested_filter::All` (and implement `nested_visit_map`). Walk your crate with
34 //! `tcx.hir().walk_toplevel_module(visitor)` invoked on `tcx.hir().krate()`.
35 //! - Pro: Visitor methods for any kind of HIR node, not just item-like things.
36 //! - Pro: Preserves nesting information
37 //! - Con: Does not integrate well into dependency tracking.
38 //!
39 //! If you have decided to use this visitor, here are some general
40 //! notes on how to do so:
41 //!
42 //! Each overridden visit method has full control over what
43 //! happens with its node, it can do its own traversal of the node's children,
44 //! call `intravisit::walk_*` to apply the default traversal algorithm, or prevent
45 //! deeper traversal by doing nothing.
46 //!
47 //! When visiting the HIR, the contents of nested items are NOT visited
48 //! by default. This is different from the AST visitor, which does a deep walk.
49 //! Hence this module is called `intravisit`; see the method `visit_nested_item`
50 //! for more details.
51 //!
52 //! Note: it is an important invariant that the default visitor walks
53 //! the body of a function in "execution order" - more concretely, if
54 //! we consider the reverse post-order (RPO) of the CFG implied by the HIR,
55 //! then a pre-order traversal of the HIR is consistent with the CFG RPO
56 //! on the *initial CFG point* of each HIR node, while a post-order traversal
57 //! of the HIR is consistent with the CFG RPO on each *final CFG point* of
58 //! each CFG node.
59 //!
60 //! One thing that follows is that if HIR node A always starts/ends executing
61 //! before HIR node B, then A appears in traversal pre/postorder before B,
62 //! respectively. (This follows from RPO respecting CFG domination).
63 //!
64 //! This order consistency is required in a few places in rustc, for
65 //! example generator inference, and possibly also HIR borrowck.
66
67 use crate::hir::*;
68 use rustc_ast::walk_list;
69 use rustc_ast::{Attribute, Label};
70 use rustc_span::def_id::LocalDefId;
71 use rustc_span::symbol::{Ident, Symbol};
72 use rustc_span::Span;
73
74 pub trait IntoVisitor<'hir> {
75 type Visitor: Visitor<'hir>;
into_visitor(&self) -> Self::Visitor76 fn into_visitor(&self) -> Self::Visitor;
77 }
78
79 #[derive(Copy, Clone, Debug)]
80 pub enum FnKind<'a> {
81 /// `#[xxx] pub async/const/extern "Abi" fn foo()`
82 ItemFn(Ident, &'a Generics<'a>, FnHeader),
83
84 /// `fn foo(&self)`
85 Method(Ident, &'a FnSig<'a>),
86
87 /// `|x, y| {}`
88 Closure,
89 }
90
91 impl<'a> FnKind<'a> {
header(&self) -> Option<&FnHeader>92 pub fn header(&self) -> Option<&FnHeader> {
93 match *self {
94 FnKind::ItemFn(_, _, ref header) => Some(header),
95 FnKind::Method(_, ref sig) => Some(&sig.header),
96 FnKind::Closure => None,
97 }
98 }
99
constness(self) -> Constness100 pub fn constness(self) -> Constness {
101 self.header().map_or(Constness::NotConst, |header| header.constness)
102 }
103
asyncness(self) -> IsAsync104 pub fn asyncness(self) -> IsAsync {
105 self.header().map_or(IsAsync::NotAsync, |header| header.asyncness)
106 }
107 }
108
109 /// An abstract representation of the HIR `rustc_middle::hir::map::Map`.
110 pub trait Map<'hir> {
111 /// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
find(&self, hir_id: HirId) -> Option<Node<'hir>>112 fn find(&self, hir_id: HirId) -> Option<Node<'hir>>;
body(&self, id: BodyId) -> &'hir Body<'hir>113 fn body(&self, id: BodyId) -> &'hir Body<'hir>;
item(&self, id: ItemId) -> &'hir Item<'hir>114 fn item(&self, id: ItemId) -> &'hir Item<'hir>;
trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>115 fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>;
impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>116 fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>;
foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>117 fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>;
118 }
119
120 // Used when no map is actually available, forcing manual implementation of nested visitors.
121 impl<'hir> Map<'hir> for ! {
find(&self, _: HirId) -> Option<Node<'hir>>122 fn find(&self, _: HirId) -> Option<Node<'hir>> {
123 *self;
124 }
body(&self, _: BodyId) -> &'hir Body<'hir>125 fn body(&self, _: BodyId) -> &'hir Body<'hir> {
126 *self;
127 }
item(&self, _: ItemId) -> &'hir Item<'hir>128 fn item(&self, _: ItemId) -> &'hir Item<'hir> {
129 *self;
130 }
trait_item(&self, _: TraitItemId) -> &'hir TraitItem<'hir>131 fn trait_item(&self, _: TraitItemId) -> &'hir TraitItem<'hir> {
132 *self;
133 }
impl_item(&self, _: ImplItemId) -> &'hir ImplItem<'hir>134 fn impl_item(&self, _: ImplItemId) -> &'hir ImplItem<'hir> {
135 *self;
136 }
foreign_item(&self, _: ForeignItemId) -> &'hir ForeignItem<'hir>137 fn foreign_item(&self, _: ForeignItemId) -> &'hir ForeignItem<'hir> {
138 *self;
139 }
140 }
141
142 pub mod nested_filter {
143 use super::Map;
144
145 /// Specifies what nested things a visitor wants to visit. By "nested
146 /// things", we are referring to bits of HIR that are not directly embedded
147 /// within one another but rather indirectly, through a table in the crate.
148 /// This is done to control dependencies during incremental compilation: the
149 /// non-inline bits of HIR can be tracked and hashed separately.
150 ///
151 /// The most common choice is `OnlyBodies`, which will cause the visitor to
152 /// visit fn bodies for fns that it encounters, and closure bodies, but
153 /// skip over nested item-like things.
154 ///
155 /// See the comments on `ItemLikeVisitor` for more details on the overall
156 /// visit strategy.
157 pub trait NestedFilter<'hir> {
158 type Map: Map<'hir>;
159
160 /// Whether the visitor visits nested "item-like" things.
161 /// E.g., item, impl-item.
162 const INTER: bool;
163 /// Whether the visitor visits "intra item-like" things.
164 /// E.g., function body, closure, `AnonConst`
165 const INTRA: bool;
166 }
167
168 /// Do not visit any nested things. When you add a new
169 /// "non-nested" thing, you will want to audit such uses to see if
170 /// they remain valid.
171 ///
172 /// Use this if you are only walking some particular kind of tree
173 /// (i.e., a type, or fn signature) and you don't want to thread a
174 /// HIR map around.
175 pub struct None(());
176 impl NestedFilter<'_> for None {
177 type Map = !;
178 const INTER: bool = false;
179 const INTRA: bool = false;
180 }
181 }
182
183 use nested_filter::NestedFilter;
184
185 /// Each method of the Visitor trait is a hook to be potentially
186 /// overridden. Each method's default implementation recursively visits
187 /// the substructure of the input via the corresponding `walk` method;
188 /// e.g., the `visit_mod` method by default calls `intravisit::walk_mod`.
189 ///
190 /// Note that this visitor does NOT visit nested items by default
191 /// (this is why the module is called `intravisit`, to distinguish it
192 /// from the AST's `visit` module, which acts differently). If you
193 /// simply want to visit all items in the crate in some order, you
194 /// should call `tcx.hir().visit_all_item_likes_in_crate`. Otherwise, see the comment
195 /// on `visit_nested_item` for details on how to visit nested items.
196 ///
197 /// If you want to ensure that your code handles every variant
198 /// explicitly, you need to override each method. (And you also need
199 /// to monitor future changes to `Visitor` in case a new method with a
200 /// new default implementation gets introduced.)
201 pub trait Visitor<'v>: Sized {
202 // this type should not be overridden, it exists for convenient usage as `Self::Map`
203 type Map: Map<'v> = <Self::NestedFilter as NestedFilter<'v>>::Map;
204
205 ///////////////////////////////////////////////////////////////////////////
206 // Nested items.
207
208 /// Override this type to control which nested HIR are visited; see
209 /// [`NestedFilter`] for details. If you override this type, you
210 /// must also override [`nested_visit_map`](Self::nested_visit_map).
211 ///
212 /// **If for some reason you want the nested behavior, but don't
213 /// have a `Map` at your disposal:** then override the
214 /// `visit_nested_XXX` methods. If a new `visit_nested_XXX` variant is
215 /// added in the future, it will cause a panic which can be detected
216 /// and fixed appropriately.
217 type NestedFilter: NestedFilter<'v> = nested_filter::None;
218
219 /// If `type NestedFilter` is set to visit nested items, this method
220 /// must also be overridden to provide a map to retrieve nested items.
nested_visit_map(&mut self) -> Self::Map221 fn nested_visit_map(&mut self) -> Self::Map {
222 panic!(
223 "nested_visit_map must be implemented or consider using \
224 `type NestedFilter = nested_filter::None` (the default)"
225 );
226 }
227
228 /// Invoked when a nested item is encountered. By default, when
229 /// `Self::NestedFilter` is `nested_filter::None`, this method does
230 /// nothing. **You probably don't want to override this method** --
231 /// instead, override [`Self::NestedFilter`] or use the "shallow" or
232 /// "deep" visit patterns described on
233 /// `itemlikevisit::ItemLikeVisitor`. The only reason to override
234 /// this method is if you want a nested pattern but cannot supply a
235 /// [`Map`]; see `nested_visit_map` for advice.
visit_nested_item(&mut self, id: ItemId)236 fn visit_nested_item(&mut self, id: ItemId) {
237 if Self::NestedFilter::INTER {
238 let item = self.nested_visit_map().item(id);
239 self.visit_item(item);
240 }
241 }
242
243 /// Like `visit_nested_item()`, but for trait items. See
244 /// `visit_nested_item()` for advice on when to override this
245 /// method.
visit_nested_trait_item(&mut self, id: TraitItemId)246 fn visit_nested_trait_item(&mut self, id: TraitItemId) {
247 if Self::NestedFilter::INTER {
248 let item = self.nested_visit_map().trait_item(id);
249 self.visit_trait_item(item);
250 }
251 }
252
253 /// Like `visit_nested_item()`, but for impl items. See
254 /// `visit_nested_item()` for advice on when to override this
255 /// method.
visit_nested_impl_item(&mut self, id: ImplItemId)256 fn visit_nested_impl_item(&mut self, id: ImplItemId) {
257 if Self::NestedFilter::INTER {
258 let item = self.nested_visit_map().impl_item(id);
259 self.visit_impl_item(item);
260 }
261 }
262
263 /// Like `visit_nested_item()`, but for foreign items. See
264 /// `visit_nested_item()` for advice on when to override this
265 /// method.
visit_nested_foreign_item(&mut self, id: ForeignItemId)266 fn visit_nested_foreign_item(&mut self, id: ForeignItemId) {
267 if Self::NestedFilter::INTER {
268 let item = self.nested_visit_map().foreign_item(id);
269 self.visit_foreign_item(item);
270 }
271 }
272
273 /// Invoked to visit the body of a function, method or closure. Like
274 /// `visit_nested_item`, does nothing by default unless you override
275 /// `Self::NestedFilter`.
visit_nested_body(&mut self, id: BodyId)276 fn visit_nested_body(&mut self, id: BodyId) {
277 if Self::NestedFilter::INTRA {
278 let body = self.nested_visit_map().body(id);
279 self.visit_body(body);
280 }
281 }
282
visit_param(&mut self, param: &'v Param<'v>)283 fn visit_param(&mut self, param: &'v Param<'v>) {
284 walk_param(self, param)
285 }
286
287 /// Visits the top-level item and (optionally) nested items / impl items. See
288 /// `visit_nested_item` for details.
visit_item(&mut self, i: &'v Item<'v>)289 fn visit_item(&mut self, i: &'v Item<'v>) {
290 walk_item(self, i)
291 }
292
visit_body(&mut self, b: &'v Body<'v>)293 fn visit_body(&mut self, b: &'v Body<'v>) {
294 walk_body(self, b);
295 }
296
297 ///////////////////////////////////////////////////////////////////////////
298
visit_id(&mut self, _hir_id: HirId)299 fn visit_id(&mut self, _hir_id: HirId) {
300 // Nothing to do.
301 }
visit_name(&mut self, _name: Symbol)302 fn visit_name(&mut self, _name: Symbol) {
303 // Nothing to do.
304 }
visit_ident(&mut self, ident: Ident)305 fn visit_ident(&mut self, ident: Ident) {
306 walk_ident(self, ident)
307 }
visit_mod(&mut self, m: &'v Mod<'v>, _s: Span, n: HirId)308 fn visit_mod(&mut self, m: &'v Mod<'v>, _s: Span, n: HirId) {
309 walk_mod(self, m, n)
310 }
visit_foreign_item(&mut self, i: &'v ForeignItem<'v>)311 fn visit_foreign_item(&mut self, i: &'v ForeignItem<'v>) {
312 walk_foreign_item(self, i)
313 }
visit_local(&mut self, l: &'v Local<'v>)314 fn visit_local(&mut self, l: &'v Local<'v>) {
315 walk_local(self, l)
316 }
visit_block(&mut self, b: &'v Block<'v>)317 fn visit_block(&mut self, b: &'v Block<'v>) {
318 walk_block(self, b)
319 }
visit_stmt(&mut self, s: &'v Stmt<'v>)320 fn visit_stmt(&mut self, s: &'v Stmt<'v>) {
321 walk_stmt(self, s)
322 }
visit_arm(&mut self, a: &'v Arm<'v>)323 fn visit_arm(&mut self, a: &'v Arm<'v>) {
324 walk_arm(self, a)
325 }
visit_pat(&mut self, p: &'v Pat<'v>)326 fn visit_pat(&mut self, p: &'v Pat<'v>) {
327 walk_pat(self, p)
328 }
visit_pat_field(&mut self, f: &'v PatField<'v>)329 fn visit_pat_field(&mut self, f: &'v PatField<'v>) {
330 walk_pat_field(self, f)
331 }
visit_array_length(&mut self, len: &'v ArrayLen)332 fn visit_array_length(&mut self, len: &'v ArrayLen) {
333 walk_array_len(self, len)
334 }
visit_anon_const(&mut self, c: &'v AnonConst)335 fn visit_anon_const(&mut self, c: &'v AnonConst) {
336 walk_anon_const(self, c)
337 }
visit_inline_const(&mut self, c: &'v ConstBlock)338 fn visit_inline_const(&mut self, c: &'v ConstBlock) {
339 walk_inline_const(self, c)
340 }
visit_expr(&mut self, ex: &'v Expr<'v>)341 fn visit_expr(&mut self, ex: &'v Expr<'v>) {
342 walk_expr(self, ex)
343 }
visit_let_expr(&mut self, lex: &'v Let<'v>)344 fn visit_let_expr(&mut self, lex: &'v Let<'v>) {
345 walk_let_expr(self, lex)
346 }
visit_expr_field(&mut self, field: &'v ExprField<'v>)347 fn visit_expr_field(&mut self, field: &'v ExprField<'v>) {
348 walk_expr_field(self, field)
349 }
visit_ty(&mut self, t: &'v Ty<'v>)350 fn visit_ty(&mut self, t: &'v Ty<'v>) {
351 walk_ty(self, t)
352 }
visit_generic_param(&mut self, p: &'v GenericParam<'v>)353 fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) {
354 walk_generic_param(self, p)
355 }
visit_const_param_default(&mut self, _param: HirId, ct: &'v AnonConst)356 fn visit_const_param_default(&mut self, _param: HirId, ct: &'v AnonConst) {
357 walk_const_param_default(self, ct)
358 }
visit_generics(&mut self, g: &'v Generics<'v>)359 fn visit_generics(&mut self, g: &'v Generics<'v>) {
360 walk_generics(self, g)
361 }
visit_where_predicate(&mut self, predicate: &'v WherePredicate<'v>)362 fn visit_where_predicate(&mut self, predicate: &'v WherePredicate<'v>) {
363 walk_where_predicate(self, predicate)
364 }
visit_fn_ret_ty(&mut self, ret_ty: &'v FnRetTy<'v>)365 fn visit_fn_ret_ty(&mut self, ret_ty: &'v FnRetTy<'v>) {
366 walk_fn_ret_ty(self, ret_ty)
367 }
visit_fn_decl(&mut self, fd: &'v FnDecl<'v>)368 fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) {
369 walk_fn_decl(self, fd)
370 }
visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, _: Span, id: LocalDefId)371 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, _: Span, id: LocalDefId) {
372 walk_fn(self, fk, fd, b, id)
373 }
visit_use(&mut self, path: &'v UsePath<'v>, hir_id: HirId)374 fn visit_use(&mut self, path: &'v UsePath<'v>, hir_id: HirId) {
375 walk_use(self, path, hir_id)
376 }
visit_trait_item(&mut self, ti: &'v TraitItem<'v>)377 fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) {
378 walk_trait_item(self, ti)
379 }
visit_trait_item_ref(&mut self, ii: &'v TraitItemRef)380 fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
381 walk_trait_item_ref(self, ii)
382 }
visit_impl_item(&mut self, ii: &'v ImplItem<'v>)383 fn visit_impl_item(&mut self, ii: &'v ImplItem<'v>) {
384 walk_impl_item(self, ii)
385 }
visit_foreign_item_ref(&mut self, ii: &'v ForeignItemRef)386 fn visit_foreign_item_ref(&mut self, ii: &'v ForeignItemRef) {
387 walk_foreign_item_ref(self, ii)
388 }
visit_impl_item_ref(&mut self, ii: &'v ImplItemRef)389 fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) {
390 walk_impl_item_ref(self, ii)
391 }
visit_trait_ref(&mut self, t: &'v TraitRef<'v>)392 fn visit_trait_ref(&mut self, t: &'v TraitRef<'v>) {
393 walk_trait_ref(self, t)
394 }
visit_param_bound(&mut self, bounds: &'v GenericBound<'v>)395 fn visit_param_bound(&mut self, bounds: &'v GenericBound<'v>) {
396 walk_param_bound(self, bounds)
397 }
visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>)398 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>) {
399 walk_poly_trait_ref(self, t)
400 }
visit_variant_data(&mut self, s: &'v VariantData<'v>)401 fn visit_variant_data(&mut self, s: &'v VariantData<'v>) {
402 walk_struct_def(self, s)
403 }
visit_field_def(&mut self, s: &'v FieldDef<'v>)404 fn visit_field_def(&mut self, s: &'v FieldDef<'v>) {
405 walk_field_def(self, s)
406 }
visit_enum_def(&mut self, enum_definition: &'v EnumDef<'v>, item_id: HirId)407 fn visit_enum_def(&mut self, enum_definition: &'v EnumDef<'v>, item_id: HirId) {
408 walk_enum_def(self, enum_definition, item_id)
409 }
visit_variant(&mut self, v: &'v Variant<'v>)410 fn visit_variant(&mut self, v: &'v Variant<'v>) {
411 walk_variant(self, v)
412 }
visit_label(&mut self, label: &'v Label)413 fn visit_label(&mut self, label: &'v Label) {
414 walk_label(self, label)
415 }
visit_infer(&mut self, inf: &'v InferArg)416 fn visit_infer(&mut self, inf: &'v InferArg) {
417 walk_inf(self, inf);
418 }
visit_generic_arg(&mut self, generic_arg: &'v GenericArg<'v>)419 fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg<'v>) {
420 walk_generic_arg(self, generic_arg);
421 }
visit_lifetime(&mut self, lifetime: &'v Lifetime)422 fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
423 walk_lifetime(self, lifetime)
424 }
425 // The span is that of the surrounding type/pattern/expr/whatever.
visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, _span: Span)426 fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, _span: Span) {
427 walk_qpath(self, qpath, id)
428 }
visit_path(&mut self, path: &Path<'v>, _id: HirId)429 fn visit_path(&mut self, path: &Path<'v>, _id: HirId) {
430 walk_path(self, path)
431 }
visit_path_segment(&mut self, path_segment: &'v PathSegment<'v>)432 fn visit_path_segment(&mut self, path_segment: &'v PathSegment<'v>) {
433 walk_path_segment(self, path_segment)
434 }
visit_generic_args(&mut self, generic_args: &'v GenericArgs<'v>)435 fn visit_generic_args(&mut self, generic_args: &'v GenericArgs<'v>) {
436 walk_generic_args(self, generic_args)
437 }
visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding<'v>)438 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding<'v>) {
439 walk_assoc_type_binding(self, type_binding)
440 }
visit_attribute(&mut self, _attr: &'v Attribute)441 fn visit_attribute(&mut self, _attr: &'v Attribute) {}
visit_associated_item_kind(&mut self, kind: &'v AssocItemKind)442 fn visit_associated_item_kind(&mut self, kind: &'v AssocItemKind) {
443 walk_associated_item_kind(self, kind);
444 }
visit_defaultness(&mut self, defaultness: &'v Defaultness)445 fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
446 walk_defaultness(self, defaultness);
447 }
visit_inline_asm(&mut self, asm: &'v InlineAsm<'v>, id: HirId)448 fn visit_inline_asm(&mut self, asm: &'v InlineAsm<'v>, id: HirId) {
449 walk_inline_asm(self, asm, id);
450 }
451 }
452
walk_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Param<'v>)453 pub fn walk_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Param<'v>) {
454 visitor.visit_id(param.hir_id);
455 visitor.visit_pat(param.pat);
456 }
457
walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>)458 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
459 visitor.visit_ident(item.ident);
460 match item.kind {
461 ItemKind::ExternCrate(orig_name) => {
462 visitor.visit_id(item.hir_id());
463 if let Some(orig_name) = orig_name {
464 visitor.visit_name(orig_name);
465 }
466 }
467 ItemKind::Use(ref path, _) => {
468 visitor.visit_use(path, item.hir_id());
469 }
470 ItemKind::Static(ref typ, _, body) | ItemKind::Const(ref typ, body) => {
471 visitor.visit_id(item.hir_id());
472 visitor.visit_ty(typ);
473 visitor.visit_nested_body(body);
474 }
475 ItemKind::Fn(ref sig, ref generics, body_id) => {
476 visitor.visit_id(item.hir_id());
477 visitor.visit_fn(
478 FnKind::ItemFn(item.ident, generics, sig.header),
479 sig.decl,
480 body_id,
481 item.span,
482 item.owner_id.def_id,
483 )
484 }
485 ItemKind::Macro(..) => {
486 visitor.visit_id(item.hir_id());
487 }
488 ItemKind::Mod(ref module) => {
489 // `visit_mod()` takes care of visiting the `Item`'s `HirId`.
490 visitor.visit_mod(module, item.span, item.hir_id())
491 }
492 ItemKind::ForeignMod { abi: _, items } => {
493 visitor.visit_id(item.hir_id());
494 walk_list!(visitor, visit_foreign_item_ref, items);
495 }
496 ItemKind::GlobalAsm(asm) => {
497 visitor.visit_id(item.hir_id());
498 visitor.visit_inline_asm(asm, item.hir_id());
499 }
500 ItemKind::TyAlias(ref ty, ref generics) => {
501 visitor.visit_id(item.hir_id());
502 visitor.visit_ty(ty);
503 visitor.visit_generics(generics)
504 }
505 ItemKind::OpaqueTy(&OpaqueTy { generics, bounds, .. }) => {
506 visitor.visit_id(item.hir_id());
507 walk_generics(visitor, generics);
508 walk_list!(visitor, visit_param_bound, bounds);
509 }
510 ItemKind::Enum(ref enum_definition, ref generics) => {
511 visitor.visit_generics(generics);
512 // `visit_enum_def()` takes care of visiting the `Item`'s `HirId`.
513 visitor.visit_enum_def(enum_definition, item.hir_id())
514 }
515 ItemKind::Impl(Impl {
516 unsafety: _,
517 defaultness: _,
518 polarity: _,
519 constness: _,
520 defaultness_span: _,
521 ref generics,
522 ref of_trait,
523 ref self_ty,
524 items,
525 }) => {
526 visitor.visit_id(item.hir_id());
527 visitor.visit_generics(generics);
528 walk_list!(visitor, visit_trait_ref, of_trait);
529 visitor.visit_ty(self_ty);
530 walk_list!(visitor, visit_impl_item_ref, *items);
531 }
532 ItemKind::Struct(ref struct_definition, ref generics)
533 | ItemKind::Union(ref struct_definition, ref generics) => {
534 visitor.visit_generics(generics);
535 visitor.visit_id(item.hir_id());
536 visitor.visit_variant_data(struct_definition);
537 }
538 ItemKind::Trait(.., ref generics, bounds, trait_item_refs) => {
539 visitor.visit_id(item.hir_id());
540 visitor.visit_generics(generics);
541 walk_list!(visitor, visit_param_bound, bounds);
542 walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
543 }
544 ItemKind::TraitAlias(ref generics, bounds) => {
545 visitor.visit_id(item.hir_id());
546 visitor.visit_generics(generics);
547 walk_list!(visitor, visit_param_bound, bounds);
548 }
549 }
550 }
551
walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body<'v>)552 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body<'v>) {
553 walk_list!(visitor, visit_param, body.params);
554 visitor.visit_expr(body.value);
555 }
556
walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident)557 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
558 visitor.visit_name(ident.name);
559 }
560
walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod<'v>, mod_hir_id: HirId)561 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod<'v>, mod_hir_id: HirId) {
562 visitor.visit_id(mod_hir_id);
563 for &item_id in module.item_ids {
564 visitor.visit_nested_item(item_id);
565 }
566 }
567
walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem<'v>)568 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem<'v>) {
569 visitor.visit_id(foreign_item.hir_id());
570 visitor.visit_ident(foreign_item.ident);
571
572 match foreign_item.kind {
573 ForeignItemKind::Fn(ref function_declaration, param_names, ref generics) => {
574 visitor.visit_generics(generics);
575 visitor.visit_fn_decl(function_declaration);
576 for ¶m_name in param_names {
577 visitor.visit_ident(param_name);
578 }
579 }
580 ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
581 ForeignItemKind::Type => (),
582 }
583 }
584
walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local<'v>)585 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local<'v>) {
586 // Intentionally visiting the expr first - the initialization expr
587 // dominates the local's definition.
588 walk_list!(visitor, visit_expr, &local.init);
589 visitor.visit_id(local.hir_id);
590 visitor.visit_pat(local.pat);
591 if let Some(els) = local.els {
592 visitor.visit_block(els);
593 }
594 walk_list!(visitor, visit_ty, &local.ty);
595 }
596
walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block<'v>)597 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block<'v>) {
598 visitor.visit_id(block.hir_id);
599 walk_list!(visitor, visit_stmt, block.stmts);
600 walk_list!(visitor, visit_expr, &block.expr);
601 }
602
walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt<'v>)603 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt<'v>) {
604 visitor.visit_id(statement.hir_id);
605 match statement.kind {
606 StmtKind::Local(ref local) => visitor.visit_local(local),
607 StmtKind::Item(item) => visitor.visit_nested_item(item),
608 StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
609 visitor.visit_expr(expression)
610 }
611 }
612 }
613
walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm<'v>)614 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm<'v>) {
615 visitor.visit_id(arm.hir_id);
616 visitor.visit_pat(arm.pat);
617 if let Some(ref g) = arm.guard {
618 match g {
619 Guard::If(ref e) => visitor.visit_expr(e),
620 Guard::IfLet(ref l) => {
621 visitor.visit_let_expr(l);
622 }
623 }
624 }
625 visitor.visit_expr(arm.body);
626 }
627
walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>)628 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) {
629 visitor.visit_id(pattern.hir_id);
630 match pattern.kind {
631 PatKind::TupleStruct(ref qpath, children, _) => {
632 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
633 walk_list!(visitor, visit_pat, children);
634 }
635 PatKind::Path(ref qpath) => {
636 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
637 }
638 PatKind::Struct(ref qpath, fields, _) => {
639 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
640 walk_list!(visitor, visit_pat_field, fields);
641 }
642 PatKind::Or(pats) => walk_list!(visitor, visit_pat, pats),
643 PatKind::Tuple(tuple_elements, _) => {
644 walk_list!(visitor, visit_pat, tuple_elements);
645 }
646 PatKind::Box(ref subpattern) | PatKind::Ref(ref subpattern, _) => {
647 visitor.visit_pat(subpattern)
648 }
649 PatKind::Binding(_, _hir_id, ident, ref optional_subpattern) => {
650 visitor.visit_ident(ident);
651 walk_list!(visitor, visit_pat, optional_subpattern);
652 }
653 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
654 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
655 walk_list!(visitor, visit_expr, lower_bound);
656 walk_list!(visitor, visit_expr, upper_bound);
657 }
658 PatKind::Wild => (),
659 PatKind::Slice(prepatterns, ref slice_pattern, postpatterns) => {
660 walk_list!(visitor, visit_pat, prepatterns);
661 walk_list!(visitor, visit_pat, slice_pattern);
662 walk_list!(visitor, visit_pat, postpatterns);
663 }
664 }
665 }
666
walk_pat_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v PatField<'v>)667 pub fn walk_pat_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v PatField<'v>) {
668 visitor.visit_id(field.hir_id);
669 visitor.visit_ident(field.ident);
670 visitor.visit_pat(field.pat)
671 }
672
walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen)673 pub fn walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen) {
674 match len {
675 &ArrayLen::Infer(hir_id, _span) => visitor.visit_id(hir_id),
676 ArrayLen::Body(c) => visitor.visit_anon_const(c),
677 }
678 }
679
walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst)680 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
681 visitor.visit_id(constant.hir_id);
682 visitor.visit_nested_body(constant.body);
683 }
684
walk_inline_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v ConstBlock)685 pub fn walk_inline_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v ConstBlock) {
686 visitor.visit_id(constant.hir_id);
687 visitor.visit_nested_body(constant.body);
688 }
689
walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)690 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) {
691 visitor.visit_id(expression.hir_id);
692 match expression.kind {
693 ExprKind::Array(subexpressions) => {
694 walk_list!(visitor, visit_expr, subexpressions);
695 }
696 ExprKind::ConstBlock(ref const_block) => visitor.visit_inline_const(const_block),
697 ExprKind::Repeat(ref element, ref count) => {
698 visitor.visit_expr(element);
699 visitor.visit_array_length(count)
700 }
701 ExprKind::Struct(ref qpath, fields, ref optional_base) => {
702 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
703 walk_list!(visitor, visit_expr_field, fields);
704 walk_list!(visitor, visit_expr, optional_base);
705 }
706 ExprKind::Tup(subexpressions) => {
707 walk_list!(visitor, visit_expr, subexpressions);
708 }
709 ExprKind::Call(ref callee_expression, arguments) => {
710 visitor.visit_expr(callee_expression);
711 walk_list!(visitor, visit_expr, arguments);
712 }
713 ExprKind::MethodCall(ref segment, receiver, arguments, _) => {
714 visitor.visit_path_segment(segment);
715 visitor.visit_expr(receiver);
716 walk_list!(visitor, visit_expr, arguments);
717 }
718 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
719 visitor.visit_expr(left_expression);
720 visitor.visit_expr(right_expression)
721 }
722 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
723 visitor.visit_expr(subexpression)
724 }
725 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
726 visitor.visit_expr(subexpression);
727 visitor.visit_ty(typ)
728 }
729 ExprKind::DropTemps(ref subexpression) => {
730 visitor.visit_expr(subexpression);
731 }
732 ExprKind::Let(ref let_expr) => visitor.visit_let_expr(let_expr),
733 ExprKind::If(ref cond, ref then, ref else_opt) => {
734 visitor.visit_expr(cond);
735 visitor.visit_expr(then);
736 walk_list!(visitor, visit_expr, else_opt);
737 }
738 ExprKind::Loop(ref block, ref opt_label, _, _) => {
739 walk_list!(visitor, visit_label, opt_label);
740 visitor.visit_block(block);
741 }
742 ExprKind::Match(ref subexpression, arms, _) => {
743 visitor.visit_expr(subexpression);
744 walk_list!(visitor, visit_arm, arms);
745 }
746 ExprKind::Closure(&Closure {
747 def_id,
748 binder: _,
749 bound_generic_params,
750 fn_decl,
751 body,
752 capture_clause: _,
753 fn_decl_span: _,
754 fn_arg_span: _,
755 movability: _,
756 constness: _,
757 }) => {
758 walk_list!(visitor, visit_generic_param, bound_generic_params);
759 visitor.visit_fn(FnKind::Closure, fn_decl, body, expression.span, def_id)
760 }
761 ExprKind::Block(ref block, ref opt_label) => {
762 walk_list!(visitor, visit_label, opt_label);
763 visitor.visit_block(block);
764 }
765 ExprKind::Assign(ref lhs, ref rhs, _) => {
766 visitor.visit_expr(rhs);
767 visitor.visit_expr(lhs)
768 }
769 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
770 visitor.visit_expr(right_expression);
771 visitor.visit_expr(left_expression);
772 }
773 ExprKind::Field(ref subexpression, ident) => {
774 visitor.visit_expr(subexpression);
775 visitor.visit_ident(ident);
776 }
777 ExprKind::Index(ref main_expression, ref index_expression) => {
778 visitor.visit_expr(main_expression);
779 visitor.visit_expr(index_expression)
780 }
781 ExprKind::Path(ref qpath) => {
782 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
783 }
784 ExprKind::Break(ref destination, ref opt_expr) => {
785 walk_list!(visitor, visit_label, &destination.label);
786 walk_list!(visitor, visit_expr, opt_expr);
787 }
788 ExprKind::Continue(ref destination) => {
789 walk_list!(visitor, visit_label, &destination.label);
790 }
791 ExprKind::Ret(ref optional_expression) => {
792 walk_list!(visitor, visit_expr, optional_expression);
793 }
794 ExprKind::Become(ref expr) => visitor.visit_expr(expr),
795 ExprKind::InlineAsm(ref asm) => {
796 visitor.visit_inline_asm(asm, expression.hir_id);
797 }
798 ExprKind::OffsetOf(ref container, ref fields) => {
799 visitor.visit_ty(container);
800 walk_list!(visitor, visit_ident, fields.iter().copied());
801 }
802 ExprKind::Yield(ref subexpression, _) => {
803 visitor.visit_expr(subexpression);
804 }
805 ExprKind::Lit(_) | ExprKind::Err(_) => {}
806 }
807 }
808
walk_let_expr<'v, V: Visitor<'v>>(visitor: &mut V, let_expr: &'v Let<'v>)809 pub fn walk_let_expr<'v, V: Visitor<'v>>(visitor: &mut V, let_expr: &'v Let<'v>) {
810 // match the visit order in walk_local
811 visitor.visit_expr(let_expr.init);
812 visitor.visit_id(let_expr.hir_id);
813 visitor.visit_pat(let_expr.pat);
814 walk_list!(visitor, visit_ty, let_expr.ty);
815 }
816
walk_expr_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v ExprField<'v>)817 pub fn walk_expr_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v ExprField<'v>) {
818 visitor.visit_id(field.hir_id);
819 visitor.visit_ident(field.ident);
820 visitor.visit_expr(field.expr)
821 }
822
walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>)823 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
824 visitor.visit_id(typ.hir_id);
825
826 match typ.kind {
827 TyKind::Slice(ref ty) => visitor.visit_ty(ty),
828 TyKind::Ptr(ref mutable_type) => visitor.visit_ty(mutable_type.ty),
829 TyKind::Ref(ref lifetime, ref mutable_type) => {
830 visitor.visit_lifetime(lifetime);
831 visitor.visit_ty(mutable_type.ty)
832 }
833 TyKind::Never => {}
834 TyKind::Tup(tuple_element_types) => {
835 walk_list!(visitor, visit_ty, tuple_element_types);
836 }
837 TyKind::BareFn(ref function_declaration) => {
838 walk_list!(visitor, visit_generic_param, function_declaration.generic_params);
839 visitor.visit_fn_decl(function_declaration.decl);
840 }
841 TyKind::Path(ref qpath) => {
842 visitor.visit_qpath(qpath, typ.hir_id, typ.span);
843 }
844 TyKind::OpaqueDef(item_id, lifetimes, _in_trait) => {
845 visitor.visit_nested_item(item_id);
846 walk_list!(visitor, visit_generic_arg, lifetimes);
847 }
848 TyKind::Array(ref ty, ref length) => {
849 visitor.visit_ty(ty);
850 visitor.visit_array_length(length)
851 }
852 TyKind::TraitObject(bounds, ref lifetime, _syntax) => {
853 for bound in bounds {
854 visitor.visit_poly_trait_ref(bound);
855 }
856 visitor.visit_lifetime(lifetime);
857 }
858 TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
859 TyKind::Infer | TyKind::Err(_) => {}
860 }
861 }
862
walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam<'v>)863 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam<'v>) {
864 visitor.visit_id(param.hir_id);
865 match param.name {
866 ParamName::Plain(ident) => visitor.visit_ident(ident),
867 ParamName::Error | ParamName::Fresh => {}
868 }
869 match param.kind {
870 GenericParamKind::Lifetime { .. } => {}
871 GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
872 GenericParamKind::Const { ref ty, ref default } => {
873 visitor.visit_ty(ty);
874 if let Some(ref default) = default {
875 visitor.visit_const_param_default(param.hir_id, default);
876 }
877 }
878 }
879 }
880
walk_const_param_default<'v, V: Visitor<'v>>(visitor: &mut V, ct: &'v AnonConst)881 pub fn walk_const_param_default<'v, V: Visitor<'v>>(visitor: &mut V, ct: &'v AnonConst) {
882 visitor.visit_anon_const(ct)
883 }
884
walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>)885 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) {
886 walk_list!(visitor, visit_generic_param, generics.params);
887 walk_list!(visitor, visit_where_predicate, generics.predicates);
888 }
889
walk_where_predicate<'v, V: Visitor<'v>>( visitor: &mut V, predicate: &'v WherePredicate<'v>, )890 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
891 visitor: &mut V,
892 predicate: &'v WherePredicate<'v>,
893 ) {
894 match *predicate {
895 WherePredicate::BoundPredicate(WhereBoundPredicate {
896 hir_id,
897 ref bounded_ty,
898 bounds,
899 bound_generic_params,
900 origin: _,
901 span: _,
902 }) => {
903 visitor.visit_id(hir_id);
904 visitor.visit_ty(bounded_ty);
905 walk_list!(visitor, visit_param_bound, bounds);
906 walk_list!(visitor, visit_generic_param, bound_generic_params);
907 }
908 WherePredicate::RegionPredicate(WhereRegionPredicate {
909 ref lifetime,
910 bounds,
911 span: _,
912 in_where_clause: _,
913 }) => {
914 visitor.visit_lifetime(lifetime);
915 walk_list!(visitor, visit_param_bound, bounds);
916 }
917 WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, span: _ }) => {
918 visitor.visit_ty(lhs_ty);
919 visitor.visit_ty(rhs_ty);
920 }
921 }
922 }
923
walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl<'v>)924 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl<'v>) {
925 for ty in function_declaration.inputs {
926 visitor.visit_ty(ty)
927 }
928 visitor.visit_fn_ret_ty(&function_declaration.output)
929 }
930
walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FnRetTy<'v>)931 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FnRetTy<'v>) {
932 if let FnRetTy::Return(ref output_ty) = *ret_ty {
933 visitor.visit_ty(output_ty)
934 }
935 }
936
walk_fn<'v, V: Visitor<'v>>( visitor: &mut V, function_kind: FnKind<'v>, function_declaration: &'v FnDecl<'v>, body_id: BodyId, _: LocalDefId, )937 pub fn walk_fn<'v, V: Visitor<'v>>(
938 visitor: &mut V,
939 function_kind: FnKind<'v>,
940 function_declaration: &'v FnDecl<'v>,
941 body_id: BodyId,
942 _: LocalDefId,
943 ) {
944 visitor.visit_fn_decl(function_declaration);
945 walk_fn_kind(visitor, function_kind);
946 visitor.visit_nested_body(body_id)
947 }
948
walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>)949 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
950 match function_kind {
951 FnKind::ItemFn(_, generics, ..) => {
952 visitor.visit_generics(generics);
953 }
954 FnKind::Closure | FnKind::Method(..) => {}
955 }
956 }
957
walk_use<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v UsePath<'v>, hir_id: HirId)958 pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v UsePath<'v>, hir_id: HirId) {
959 visitor.visit_id(hir_id);
960 let UsePath { segments, ref res, span } = *path;
961 for &res in res {
962 visitor.visit_path(&Path { segments, res, span }, hir_id);
963 }
964 }
965
walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem<'v>)966 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem<'v>) {
967 // N.B., deliberately force a compilation error if/when new fields are added.
968 let TraitItem { ident, generics, ref defaultness, ref kind, span, owner_id: _ } = *trait_item;
969 let hir_id = trait_item.hir_id();
970 visitor.visit_ident(ident);
971 visitor.visit_generics(&generics);
972 visitor.visit_defaultness(&defaultness);
973 visitor.visit_id(hir_id);
974 match *kind {
975 TraitItemKind::Const(ref ty, default) => {
976 visitor.visit_ty(ty);
977 walk_list!(visitor, visit_nested_body, default);
978 }
979 TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => {
980 visitor.visit_fn_decl(sig.decl);
981 for ¶m_name in param_names {
982 visitor.visit_ident(param_name);
983 }
984 }
985 TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => {
986 visitor.visit_fn(
987 FnKind::Method(ident, sig),
988 sig.decl,
989 body_id,
990 span,
991 trait_item.owner_id.def_id,
992 );
993 }
994 TraitItemKind::Type(bounds, ref default) => {
995 walk_list!(visitor, visit_param_bound, bounds);
996 walk_list!(visitor, visit_ty, default);
997 }
998 }
999 }
1000
walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef)1001 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
1002 // N.B., deliberately force a compilation error if/when new fields are added.
1003 let TraitItemRef { id, ident, ref kind, span: _ } = *trait_item_ref;
1004 visitor.visit_nested_trait_item(id);
1005 visitor.visit_ident(ident);
1006 visitor.visit_associated_item_kind(kind);
1007 }
1008
walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem<'v>)1009 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem<'v>) {
1010 // N.B., deliberately force a compilation error if/when new fields are added.
1011 let ImplItem {
1012 owner_id: _,
1013 ident,
1014 ref generics,
1015 ref kind,
1016 ref defaultness,
1017 span: _,
1018 vis_span: _,
1019 } = *impl_item;
1020
1021 visitor.visit_ident(ident);
1022 visitor.visit_generics(generics);
1023 visitor.visit_defaultness(defaultness);
1024 visitor.visit_id(impl_item.hir_id());
1025 match *kind {
1026 ImplItemKind::Const(ref ty, body) => {
1027 visitor.visit_ty(ty);
1028 visitor.visit_nested_body(body);
1029 }
1030 ImplItemKind::Fn(ref sig, body_id) => {
1031 visitor.visit_fn(
1032 FnKind::Method(impl_item.ident, sig),
1033 sig.decl,
1034 body_id,
1035 impl_item.span,
1036 impl_item.owner_id.def_id,
1037 );
1038 }
1039 ImplItemKind::Type(ref ty) => {
1040 visitor.visit_ty(ty);
1041 }
1042 }
1043 }
1044
walk_foreign_item_ref<'v, V: Visitor<'v>>( visitor: &mut V, foreign_item_ref: &'v ForeignItemRef, )1045 pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(
1046 visitor: &mut V,
1047 foreign_item_ref: &'v ForeignItemRef,
1048 ) {
1049 // N.B., deliberately force a compilation error if/when new fields are added.
1050 let ForeignItemRef { id, ident, span: _ } = *foreign_item_ref;
1051 visitor.visit_nested_foreign_item(id);
1052 visitor.visit_ident(ident);
1053 }
1054
walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef)1055 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
1056 // N.B., deliberately force a compilation error if/when new fields are added.
1057 let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref;
1058 visitor.visit_nested_impl_item(id);
1059 visitor.visit_ident(ident);
1060 visitor.visit_associated_item_kind(kind);
1061 }
1062
walk_trait_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_ref: &'v TraitRef<'v>)1063 pub fn walk_trait_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_ref: &'v TraitRef<'v>) {
1064 visitor.visit_id(trait_ref.hir_ref_id);
1065 visitor.visit_path(trait_ref.path, trait_ref.hir_ref_id)
1066 }
1067
walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound<'v>)1068 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound<'v>) {
1069 match *bound {
1070 GenericBound::Trait(ref typ, _modifier) => {
1071 visitor.visit_poly_trait_ref(typ);
1072 }
1073 GenericBound::LangItemTrait(_, _span, hir_id, args) => {
1074 visitor.visit_id(hir_id);
1075 visitor.visit_generic_args(args);
1076 }
1077 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
1078 }
1079 }
1080
walk_poly_trait_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_ref: &'v PolyTraitRef<'v>)1081 pub fn walk_poly_trait_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_ref: &'v PolyTraitRef<'v>) {
1082 walk_list!(visitor, visit_generic_param, trait_ref.bound_generic_params);
1083 visitor.visit_trait_ref(&trait_ref.trait_ref);
1084 }
1085
walk_struct_def<'v, V: Visitor<'v>>( visitor: &mut V, struct_definition: &'v VariantData<'v>, )1086 pub fn walk_struct_def<'v, V: Visitor<'v>>(
1087 visitor: &mut V,
1088 struct_definition: &'v VariantData<'v>,
1089 ) {
1090 walk_list!(visitor, visit_id, struct_definition.ctor_hir_id());
1091 walk_list!(visitor, visit_field_def, struct_definition.fields());
1092 }
1093
walk_field_def<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v FieldDef<'v>)1094 pub fn walk_field_def<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v FieldDef<'v>) {
1095 visitor.visit_id(field.hir_id);
1096 visitor.visit_ident(field.ident);
1097 visitor.visit_ty(field.ty);
1098 }
1099
walk_enum_def<'v, V: Visitor<'v>>( visitor: &mut V, enum_definition: &'v EnumDef<'v>, item_id: HirId, )1100 pub fn walk_enum_def<'v, V: Visitor<'v>>(
1101 visitor: &mut V,
1102 enum_definition: &'v EnumDef<'v>,
1103 item_id: HirId,
1104 ) {
1105 visitor.visit_id(item_id);
1106 walk_list!(visitor, visit_variant, enum_definition.variants);
1107 }
1108
walk_variant<'v, V: Visitor<'v>>(visitor: &mut V, variant: &'v Variant<'v>)1109 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V, variant: &'v Variant<'v>) {
1110 visitor.visit_ident(variant.ident);
1111 visitor.visit_id(variant.hir_id);
1112 visitor.visit_variant_data(&variant.data);
1113 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
1114 }
1115
walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label)1116 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
1117 visitor.visit_ident(label.ident);
1118 }
1119
walk_inf<'v, V: Visitor<'v>>(visitor: &mut V, inf: &'v InferArg)1120 pub fn walk_inf<'v, V: Visitor<'v>>(visitor: &mut V, inf: &'v InferArg) {
1121 visitor.visit_id(inf.hir_id);
1122 }
1123
walk_generic_arg<'v, V: Visitor<'v>>(visitor: &mut V, generic_arg: &'v GenericArg<'v>)1124 pub fn walk_generic_arg<'v, V: Visitor<'v>>(visitor: &mut V, generic_arg: &'v GenericArg<'v>) {
1125 match generic_arg {
1126 GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt),
1127 GenericArg::Type(ty) => visitor.visit_ty(ty),
1128 GenericArg::Const(ct) => visitor.visit_anon_const(&ct.value),
1129 GenericArg::Infer(inf) => visitor.visit_infer(inf),
1130 }
1131 }
1132
walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime)1133 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
1134 visitor.visit_id(lifetime.hir_id);
1135 visitor.visit_ident(lifetime.ident);
1136 }
1137
walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath<'v>, id: HirId)1138 pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath<'v>, id: HirId) {
1139 match *qpath {
1140 QPath::Resolved(ref maybe_qself, ref path) => {
1141 walk_list!(visitor, visit_ty, maybe_qself);
1142 visitor.visit_path(path, id)
1143 }
1144 QPath::TypeRelative(ref qself, ref segment) => {
1145 visitor.visit_ty(qself);
1146 visitor.visit_path_segment(segment);
1147 }
1148 QPath::LangItem(..) => {}
1149 }
1150 }
1151
walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &Path<'v>)1152 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &Path<'v>) {
1153 for segment in path.segments {
1154 visitor.visit_path_segment(segment);
1155 }
1156 }
1157
walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V, segment: &'v PathSegment<'v>)1158 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V, segment: &'v PathSegment<'v>) {
1159 visitor.visit_ident(segment.ident);
1160 visitor.visit_id(segment.hir_id);
1161 if let Some(ref args) = segment.args {
1162 visitor.visit_generic_args(args);
1163 }
1164 }
1165
walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V, generic_args: &'v GenericArgs<'v>)1166 pub fn walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V, generic_args: &'v GenericArgs<'v>) {
1167 walk_list!(visitor, visit_generic_arg, generic_args.args);
1168 walk_list!(visitor, visit_assoc_type_binding, generic_args.bindings);
1169 }
1170
walk_assoc_type_binding<'v, V: Visitor<'v>>( visitor: &mut V, type_binding: &'v TypeBinding<'v>, )1171 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(
1172 visitor: &mut V,
1173 type_binding: &'v TypeBinding<'v>,
1174 ) {
1175 visitor.visit_id(type_binding.hir_id);
1176 visitor.visit_ident(type_binding.ident);
1177 visitor.visit_generic_args(type_binding.gen_args);
1178 match type_binding.kind {
1179 TypeBindingKind::Equality { ref term } => match term {
1180 Term::Ty(ref ty) => visitor.visit_ty(ty),
1181 Term::Const(ref c) => visitor.visit_anon_const(c),
1182 },
1183 TypeBindingKind::Constraint { bounds } => walk_list!(visitor, visit_param_bound, bounds),
1184 }
1185 }
1186
walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssocItemKind)1187 pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssocItemKind) {
1188 // No visitable content here: this fn exists so you can call it if
1189 // the right thing to do, should content be added in the future,
1190 // would be to walk it.
1191 }
1192
walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness)1193 pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) {
1194 // No visitable content here: this fn exists so you can call it if
1195 // the right thing to do, should content be added in the future,
1196 // would be to walk it.
1197 }
1198
walk_inline_asm<'v, V: Visitor<'v>>(visitor: &mut V, asm: &'v InlineAsm<'v>, id: HirId)1199 pub fn walk_inline_asm<'v, V: Visitor<'v>>(visitor: &mut V, asm: &'v InlineAsm<'v>, id: HirId) {
1200 for (op, op_sp) in asm.operands {
1201 match op {
1202 InlineAsmOperand::In { expr, .. } | InlineAsmOperand::InOut { expr, .. } => {
1203 visitor.visit_expr(expr)
1204 }
1205 InlineAsmOperand::Out { expr, .. } => {
1206 if let Some(expr) = expr {
1207 visitor.visit_expr(expr);
1208 }
1209 }
1210 InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
1211 visitor.visit_expr(in_expr);
1212 if let Some(out_expr) = out_expr {
1213 visitor.visit_expr(out_expr);
1214 }
1215 }
1216 InlineAsmOperand::Const { anon_const, .. }
1217 | InlineAsmOperand::SymFn { anon_const, .. } => visitor.visit_anon_const(anon_const),
1218 InlineAsmOperand::SymStatic { path, .. } => visitor.visit_qpath(path, id, *op_sp),
1219 }
1220 }
1221 }
1222