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