]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/intravisit.rs
rustc: async fn drop order lowering in HIR
[rust.git] / src / librustc / hir / intravisit.rs
1 //! HIR walker for walking the contents of nodes.
2 //!
3 //! **For an overview of the visitor strategy, see the docs on the
4 //! `super::itemlikevisit::ItemLikeVisitor` trait.**
5 //!
6 //! If you have decided to use this visitor, here are some general
7 //! notes on how to do so:
8 //!
9 //! Each overridden visit method has full control over what
10 //! happens with its node, it can do its own traversal of the node's children,
11 //! call `intravisit::walk_*` to apply the default traversal algorithm, or prevent
12 //! deeper traversal by doing nothing.
13 //!
14 //! When visiting the HIR, the contents of nested items are NOT visited
15 //! by default. This is different from the AST visitor, which does a deep walk.
16 //! Hence this module is called `intravisit`; see the method `visit_nested_item`
17 //! for more details.
18 //!
19 //! Note: it is an important invariant that the default visitor walks
20 //! the body of a function in "execution order" - more concretely, if
21 //! we consider the reverse post-order (RPO) of the CFG implied by the HIR,
22 //! then a pre-order traversal of the HIR is consistent with the CFG RPO
23 //! on the *initial CFG point* of each HIR node, while a post-order traversal
24 //! of the HIR is consistent with the CFG RPO on each *final CFG point* of
25 //! each CFG node.
26 //!
27 //! One thing that follows is that if HIR node A always starts/ends executing
28 //! before HIR node B, then A appears in traversal pre/postorder before B,
29 //! respectively. (This follows from RPO respecting CFG domination).
30 //!
31 //! This order consistency is required in a few places in rustc, for
32 //! example generator inference, and possibly also HIR borrowck.
33
34 use syntax::ast::{Ident, Name, Attribute};
35 use syntax_pos::Span;
36 use crate::hir::*;
37 use crate::hir::map::Map;
38 use super::itemlikevisit::DeepVisitor;
39
40 #[derive(Copy, Clone)]
41 pub enum FnKind<'a> {
42     /// `#[xxx] pub async/const/extern "Abi" fn foo()`
43     ItemFn(Ident, &'a Generics, FnHeader, &'a Visibility, &'a [Attribute]),
44
45     /// `fn foo(&self)`
46     Method(Ident, &'a MethodSig, Option<&'a Visibility>, &'a [Attribute]),
47
48     /// `|x, y| {}`
49     Closure(&'a [Attribute]),
50 }
51
52 impl<'a> FnKind<'a> {
53     pub fn attrs(&self) -> &'a [Attribute] {
54         match *self {
55             FnKind::ItemFn(.., attrs) => attrs,
56             FnKind::Method(.., attrs) => attrs,
57             FnKind::Closure(attrs) => attrs,
58         }
59     }
60
61     pub fn header(&self) -> Option<&FnHeader> {
62         match *self {
63             FnKind::ItemFn(_, _, ref header, _, _) => Some(header),
64             FnKind::Method(_, ref sig, _, _) => Some(&sig.header),
65             FnKind::Closure(_) => None,
66         }
67     }
68 }
69
70 /// Specifies what nested things a visitor wants to visit. The most
71 /// common choice is `OnlyBodies`, which will cause the visitor to
72 /// visit fn bodies for fns that it encounters, but skip over nested
73 /// item-like things.
74 ///
75 /// See the comments on `ItemLikeVisitor` for more details on the overall
76 /// visit strategy.
77 pub enum NestedVisitorMap<'this, 'tcx: 'this> {
78     /// Do not visit any nested things. When you add a new
79     /// "non-nested" thing, you will want to audit such uses to see if
80     /// they remain valid.
81     ///
82     /// Use this if you are only walking some particular kind of tree
83     /// (i.e., a type, or fn signature) and you don't want to thread a
84     /// HIR map around.
85     None,
86
87     /// Do not visit nested item-like things, but visit nested things
88     /// that are inside of an item-like.
89     ///
90     /// **This is the most common choice.** A very common pattern is
91     /// to use `visit_all_item_likes()` as an outer loop,
92     /// and to have the visitor that visits the contents of each item
93     /// using this setting.
94     OnlyBodies(&'this Map<'tcx>),
95
96     /// Visits all nested things, including item-likes.
97     ///
98     /// **This is an unusual choice.** It is used when you want to
99     /// process everything within their lexical context. Typically you
100     /// kick off the visit by doing `walk_krate()`.
101     All(&'this Map<'tcx>),
102 }
103
104 impl<'this, 'tcx> NestedVisitorMap<'this, 'tcx> {
105     /// Returns the map to use for an "intra item-like" thing (if any).
106     /// E.g., function body.
107     pub fn intra(self) -> Option<&'this Map<'tcx>> {
108         match self {
109             NestedVisitorMap::None => None,
110             NestedVisitorMap::OnlyBodies(map) => Some(map),
111             NestedVisitorMap::All(map) => Some(map),
112         }
113     }
114
115     /// Returns the map to use for an "item-like" thing (if any).
116     /// E.g., item, impl-item.
117     pub fn inter(self) -> Option<&'this Map<'tcx>> {
118         match self {
119             NestedVisitorMap::None => None,
120             NestedVisitorMap::OnlyBodies(_) => None,
121             NestedVisitorMap::All(map) => Some(map),
122         }
123     }
124 }
125
126 /// Each method of the Visitor trait is a hook to be potentially
127 /// overridden. Each method's default implementation recursively visits
128 /// the substructure of the input via the corresponding `walk` method;
129 /// e.g., the `visit_mod` method by default calls `intravisit::walk_mod`.
130 ///
131 /// Note that this visitor does NOT visit nested items by default
132 /// (this is why the module is called `intravisit`, to distinguish it
133 /// from the AST's `visit` module, which acts differently). If you
134 /// simply want to visit all items in the crate in some order, you
135 /// should call `Crate::visit_all_items`. Otherwise, see the comment
136 /// on `visit_nested_item` for details on how to visit nested items.
137 ///
138 /// If you want to ensure that your code handles every variant
139 /// explicitly, you need to override each method. (And you also need
140 /// to monitor future changes to `Visitor` in case a new method with a
141 /// new default implementation gets introduced.)
142 pub trait Visitor<'v> : Sized {
143     ///////////////////////////////////////////////////////////////////////////
144     // Nested items.
145
146     /// The default versions of the `visit_nested_XXX` routines invoke
147     /// this method to get a map to use. By selecting an enum variant,
148     /// you control which kinds of nested HIR are visited; see
149     /// `NestedVisitorMap` for details. By "nested HIR", we are
150     /// referring to bits of HIR that are not directly embedded within
151     /// one another but rather indirectly, through a table in the
152     /// crate. This is done to control dependencies during incremental
153     /// compilation: the non-inline bits of HIR can be tracked and
154     /// hashed separately.
155     ///
156     /// **If for some reason you want the nested behavior, but don't
157     /// have a `Map` at your disposal:** then you should override the
158     /// `visit_nested_XXX` methods, and override this method to
159     /// `panic!()`. This way, if a new `visit_nested_XXX` variant is
160     /// added in the future, we will see the panic in your code and
161     /// fix it appropriately.
162     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v>;
163
164     /// Invoked when a nested item is encountered. By default does
165     /// nothing unless you override `nested_visit_map` to return
166     /// `Some(_)`, in which case it will walk the item. **You probably
167     /// don't want to override this method** -- instead, override
168     /// `nested_visit_map` or use the "shallow" or "deep" visit
169     /// patterns described on `itemlikevisit::ItemLikeVisitor`. The only
170     /// reason to override this method is if you want a nested pattern
171     /// but cannot supply a `Map`; see `nested_visit_map` for advice.
172     #[allow(unused_variables)]
173     fn visit_nested_item(&mut self, id: ItemId) {
174         let opt_item = self.nested_visit_map().inter().map(|map| map.expect_item_by_hir_id(id.id));
175         if let Some(item) = opt_item {
176             self.visit_item(item);
177         }
178     }
179
180     /// Like `visit_nested_item()`, but for trait items. See
181     /// `visit_nested_item()` for advice on when to override this
182     /// method.
183     #[allow(unused_variables)]
184     fn visit_nested_trait_item(&mut self, id: TraitItemId) {
185         let opt_item = self.nested_visit_map().inter().map(|map| map.trait_item(id));
186         if let Some(item) = opt_item {
187             self.visit_trait_item(item);
188         }
189     }
190
191     /// Like `visit_nested_item()`, but for impl items. See
192     /// `visit_nested_item()` for advice on when to override this
193     /// method.
194     #[allow(unused_variables)]
195     fn visit_nested_impl_item(&mut self, id: ImplItemId) {
196         let opt_item = self.nested_visit_map().inter().map(|map| map.impl_item(id));
197         if let Some(item) = opt_item {
198             self.visit_impl_item(item);
199         }
200     }
201
202     /// Invoked to visit the body of a function, method or closure. Like
203     /// visit_nested_item, does nothing by default unless you override
204     /// `nested_visit_map` to return `Some(_)`, in which case it will walk the
205     /// body.
206     fn visit_nested_body(&mut self, id: BodyId) {
207         let opt_body = self.nested_visit_map().intra().map(|map| map.body(id));
208         if let Some(body) = opt_body {
209             self.visit_body(body);
210         }
211     }
212
213     /// Visits the top-level item and (optionally) nested items / impl items. See
214     /// `visit_nested_item` for details.
215     fn visit_item(&mut self, i: &'v Item) {
216         walk_item(self, i)
217     }
218
219     fn visit_body(&mut self, b: &'v Body) {
220         walk_body(self, b);
221     }
222
223     /// When invoking `visit_all_item_likes()`, you need to supply an
224     /// item-like visitor. This method converts a "intra-visit"
225     /// visitor into an item-like visitor that walks the entire tree.
226     /// If you use this, you probably don't want to process the
227     /// contents of nested item-like things, since the outer loop will
228     /// visit them as well.
229     fn as_deep_visitor<'s>(&'s mut self) -> DeepVisitor<'s, Self> {
230         DeepVisitor::new(self)
231     }
232
233     ///////////////////////////////////////////////////////////////////////////
234
235     fn visit_id(&mut self, _hir_id: HirId) {
236         // Nothing to do.
237     }
238     fn visit_name(&mut self, _span: Span, _name: Name) {
239         // Nothing to do.
240     }
241     fn visit_ident(&mut self, ident: Ident) {
242         walk_ident(self, ident)
243     }
244     fn visit_mod(&mut self, m: &'v Mod, _s: Span, n: HirId) {
245         walk_mod(self, m, n)
246     }
247     fn visit_foreign_item(&mut self, i: &'v ForeignItem) {
248         walk_foreign_item(self, i)
249     }
250     fn visit_local(&mut self, l: &'v Local) {
251         walk_local(self, l)
252     }
253     fn visit_block(&mut self, b: &'v Block) {
254         walk_block(self, b)
255     }
256     fn visit_stmt(&mut self, s: &'v Stmt) {
257         walk_stmt(self, s)
258     }
259     fn visit_arm(&mut self, a: &'v Arm) {
260         walk_arm(self, a)
261     }
262     fn visit_pat(&mut self, p: &'v Pat) {
263         walk_pat(self, p)
264     }
265     fn visit_anon_const(&mut self, c: &'v AnonConst) {
266         walk_anon_const(self, c)
267     }
268     fn visit_expr(&mut self, ex: &'v Expr) {
269         walk_expr(self, ex)
270     }
271     fn visit_ty(&mut self, t: &'v Ty) {
272         walk_ty(self, t)
273     }
274     fn visit_generic_param(&mut self, p: &'v GenericParam) {
275         walk_generic_param(self, p)
276     }
277     fn visit_generics(&mut self, g: &'v Generics) {
278         walk_generics(self, g)
279     }
280     fn visit_where_predicate(&mut self, predicate: &'v WherePredicate) {
281         walk_where_predicate(self, predicate)
282     }
283     fn visit_fn_decl(&mut self, fd: &'v FnDecl) {
284         walk_fn_decl(self, fd)
285     }
286     fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: BodyId, s: Span, id: HirId) {
287         walk_fn(self, fk, fd, b, s, id)
288     }
289     fn visit_use(&mut self, path: &'v Path, hir_id: HirId) {
290         walk_use(self, path, hir_id)
291     }
292     fn visit_trait_item(&mut self, ti: &'v TraitItem) {
293         walk_trait_item(self, ti)
294     }
295     fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
296         walk_trait_item_ref(self, ii)
297     }
298     fn visit_impl_item(&mut self, ii: &'v ImplItem) {
299         walk_impl_item(self, ii)
300     }
301     fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) {
302         walk_impl_item_ref(self, ii)
303     }
304     fn visit_trait_ref(&mut self, t: &'v TraitRef) {
305         walk_trait_ref(self, t)
306     }
307     fn visit_param_bound(&mut self, bounds: &'v GenericBound) {
308         walk_param_bound(self, bounds)
309     }
310     fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: TraitBoundModifier) {
311         walk_poly_trait_ref(self, t, m)
312     }
313     fn visit_variant_data(&mut self,
314                           s: &'v VariantData,
315                           _: Name,
316                           _: &'v Generics,
317                           _parent_id: HirId,
318                           _: Span) {
319         walk_struct_def(self, s)
320     }
321     fn visit_struct_field(&mut self, s: &'v StructField) {
322         walk_struct_field(self, s)
323     }
324     fn visit_enum_def(&mut self,
325                       enum_definition: &'v EnumDef,
326                       generics: &'v Generics,
327                       item_id: HirId,
328                       _: Span) {
329         walk_enum_def(self, enum_definition, generics, item_id)
330     }
331     fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics, item_id: HirId) {
332         walk_variant(self, v, g, item_id)
333     }
334     fn visit_label(&mut self, label: &'v Label) {
335         walk_label(self, label)
336     }
337     fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg) {
338         match generic_arg {
339             GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
340             GenericArg::Type(ty) => self.visit_ty(ty),
341             GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
342         }
343     }
344     fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
345         walk_lifetime(self, lifetime)
346     }
347     fn visit_qpath(&mut self, qpath: &'v QPath, id: HirId, span: Span) {
348         walk_qpath(self, qpath, id, span)
349     }
350     fn visit_path(&mut self, path: &'v Path, _id: HirId) {
351         walk_path(self, path)
352     }
353     fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
354         walk_path_segment(self, path_span, path_segment)
355     }
356     fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs) {
357         walk_generic_args(self, path_span, generic_args)
358     }
359     fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
360         walk_assoc_type_binding(self, type_binding)
361     }
362     fn visit_attribute(&mut self, _attr: &'v Attribute) {
363     }
364     fn visit_macro_def(&mut self, macro_def: &'v MacroDef) {
365         walk_macro_def(self, macro_def)
366     }
367     fn visit_vis(&mut self, vis: &'v Visibility) {
368         walk_vis(self, vis)
369     }
370     fn visit_associated_item_kind(&mut self, kind: &'v AssocItemKind) {
371         walk_associated_item_kind(self, kind);
372     }
373     fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
374         walk_defaultness(self, defaultness);
375     }
376 }
377
378 /// Walks the contents of a crate. See also `Crate::visit_all_items`.
379 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
380     visitor.visit_mod(&krate.module, krate.span, CRATE_HIR_ID);
381     walk_list!(visitor, visit_attribute, &krate.attrs);
382     walk_list!(visitor, visit_macro_def, &krate.exported_macros);
383 }
384
385 pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef) {
386     visitor.visit_id(macro_def.hir_id);
387     visitor.visit_name(macro_def.span, macro_def.name);
388     walk_list!(visitor, visit_attribute, &macro_def.attrs);
389 }
390
391 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod, mod_hir_id: HirId) {
392     visitor.visit_id(mod_hir_id);
393     for &item_id in &module.item_ids {
394         visitor.visit_nested_item(item_id);
395     }
396 }
397
398 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body) {
399     for argument in &body.arguments {
400         visitor.visit_id(argument.hir_id);
401         visitor.visit_pat(&argument.pat);
402     }
403     visitor.visit_expr(&body.value);
404 }
405
406 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
407     // Intentionally visiting the expr first - the initialization expr
408     // dominates the local's definition.
409     walk_list!(visitor, visit_expr, &local.init);
410     walk_list!(visitor, visit_attribute, local.attrs.iter());
411     visitor.visit_id(local.hir_id);
412     visitor.visit_pat(&local.pat);
413     walk_list!(visitor, visit_ty, &local.ty);
414 }
415
416 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
417     visitor.visit_name(ident.span, ident.name);
418 }
419
420 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
421     visitor.visit_ident(label.ident);
422 }
423
424 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
425     visitor.visit_id(lifetime.hir_id);
426     match lifetime.name {
427         LifetimeName::Param(ParamName::Plain(ident)) => {
428             visitor.visit_ident(ident);
429         }
430         LifetimeName::Param(ParamName::Fresh(_)) |
431         LifetimeName::Param(ParamName::Error) |
432         LifetimeName::Static |
433         LifetimeName::Error |
434         LifetimeName::Implicit |
435         LifetimeName::Underscore => {}
436     }
437 }
438
439 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
440                                   trait_ref: &'v PolyTraitRef,
441                                   _modifier: TraitBoundModifier)
442     where V: Visitor<'v>
443 {
444     walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
445     visitor.visit_trait_ref(&trait_ref.trait_ref);
446 }
447
448 pub fn walk_trait_ref<'v, V>(visitor: &mut V, trait_ref: &'v TraitRef)
449     where V: Visitor<'v>
450 {
451     visitor.visit_id(trait_ref.hir_ref_id);
452     visitor.visit_path(&trait_ref.path, trait_ref.hir_ref_id)
453 }
454
455 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
456     visitor.visit_vis(&item.vis);
457     visitor.visit_ident(item.ident);
458     match item.node {
459         ItemKind::ExternCrate(orig_name) => {
460             visitor.visit_id(item.hir_id);
461             if let Some(orig_name) = orig_name {
462                 visitor.visit_name(item.span, orig_name);
463             }
464         }
465         ItemKind::Use(ref path, _) => {
466             visitor.visit_use(path, item.hir_id);
467         }
468         ItemKind::Static(ref typ, _, body) |
469         ItemKind::Const(ref typ, body) => {
470             visitor.visit_id(item.hir_id);
471             visitor.visit_ty(typ);
472             visitor.visit_nested_body(body);
473         }
474         ItemKind::Fn(ref declaration, header, ref generics, body_id) => {
475             visitor.visit_fn(FnKind::ItemFn(item.ident,
476                                             generics,
477                                             header,
478                                             &item.vis,
479                                             &item.attrs),
480                              declaration,
481                              body_id,
482                              item.span,
483                              item.hir_id)
484         }
485         ItemKind::Mod(ref module) => {
486             // `visit_mod()` takes care of visiting the `Item`'s `HirId`.
487             visitor.visit_mod(module, item.span, item.hir_id)
488         }
489         ItemKind::ForeignMod(ref foreign_module) => {
490             visitor.visit_id(item.hir_id);
491             walk_list!(visitor, visit_foreign_item, &foreign_module.items);
492         }
493         ItemKind::GlobalAsm(_) => {
494             visitor.visit_id(item.hir_id);
495         }
496         ItemKind::Ty(ref ty, ref generics) => {
497             visitor.visit_id(item.hir_id);
498             visitor.visit_ty(ty);
499             visitor.visit_generics(generics)
500         }
501         ItemKind::Existential(ExistTy {
502             ref generics,
503             ref bounds,
504             ..
505         }) => {
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, generics, item.hir_id, item.span)
514         }
515         ItemKind::Impl(
516             ..,
517             ref generics,
518             ref opt_trait_reference,
519             ref typ,
520             ref impl_item_refs
521         ) => {
522             visitor.visit_id(item.hir_id);
523             visitor.visit_generics(generics);
524             walk_list!(visitor, visit_trait_ref, opt_trait_reference);
525             visitor.visit_ty(typ);
526             walk_list!(visitor, visit_impl_item_ref, impl_item_refs);
527         }
528         ItemKind::Struct(ref struct_definition, ref generics) |
529         ItemKind::Union(ref struct_definition, ref generics) => {
530             visitor.visit_generics(generics);
531             visitor.visit_id(item.hir_id);
532             visitor.visit_variant_data(struct_definition, item.ident.name, generics, item.hir_id,
533                                        item.span);
534         }
535         ItemKind::Trait(.., ref generics, ref bounds, ref trait_item_refs) => {
536             visitor.visit_id(item.hir_id);
537             visitor.visit_generics(generics);
538             walk_list!(visitor, visit_param_bound, bounds);
539             walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
540         }
541         ItemKind::TraitAlias(ref generics, ref bounds) => {
542             visitor.visit_id(item.hir_id);
543             visitor.visit_generics(generics);
544             walk_list!(visitor, visit_param_bound, bounds);
545         }
546     }
547     walk_list!(visitor, visit_attribute, &item.attrs);
548 }
549
550 pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V,
551                                     path: &'v Path,
552                                     hir_id: HirId) {
553     visitor.visit_id(hir_id);
554     visitor.visit_path(path, hir_id);
555 }
556
557 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
558                                          enum_definition: &'v EnumDef,
559                                          generics: &'v Generics,
560                                          item_id: HirId) {
561     visitor.visit_id(item_id);
562     walk_list!(visitor,
563                visit_variant,
564                &enum_definition.variants,
565                generics,
566                item_id);
567 }
568
569 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
570                                         variant: &'v Variant,
571                                         generics: &'v Generics,
572                                         parent_item_id: HirId) {
573     visitor.visit_ident(variant.node.ident);
574     visitor.visit_id(variant.node.id);
575     visitor.visit_variant_data(&variant.node.data,
576                                variant.node.ident.name,
577                                generics,
578                                parent_item_id,
579                                variant.span);
580     walk_list!(visitor, visit_anon_const, &variant.node.disr_expr);
581     walk_list!(visitor, visit_attribute, &variant.node.attrs);
582 }
583
584 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
585     visitor.visit_id(typ.hir_id);
586
587     match typ.node {
588         TyKind::Slice(ref ty) => {
589             visitor.visit_ty(ty)
590         }
591         TyKind::Ptr(ref mutable_type) => {
592             visitor.visit_ty(&mutable_type.ty)
593         }
594         TyKind::Rptr(ref lifetime, ref mutable_type) => {
595             visitor.visit_lifetime(lifetime);
596             visitor.visit_ty(&mutable_type.ty)
597         }
598         TyKind::Never => {},
599         TyKind::Tup(ref tuple_element_types) => {
600             walk_list!(visitor, visit_ty, tuple_element_types);
601         }
602         TyKind::BareFn(ref function_declaration) => {
603             walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
604             visitor.visit_fn_decl(&function_declaration.decl);
605         }
606         TyKind::Path(ref qpath) => {
607             visitor.visit_qpath(qpath, typ.hir_id, typ.span);
608         }
609         TyKind::Def(item_id, ref lifetimes) => {
610             visitor.visit_nested_item(item_id);
611             walk_list!(visitor, visit_generic_arg, lifetimes);
612         }
613         TyKind::Array(ref ty, ref length) => {
614             visitor.visit_ty(ty);
615             visitor.visit_anon_const(length)
616         }
617         TyKind::TraitObject(ref bounds, ref lifetime) => {
618             for bound in bounds {
619                 visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
620             }
621             visitor.visit_lifetime(lifetime);
622         }
623         TyKind::Typeof(ref expression) => {
624             visitor.visit_anon_const(expression)
625         }
626         TyKind::CVarArgs(ref lt) => {
627             visitor.visit_lifetime(lt)
628         }
629         TyKind::Infer | TyKind::Err => {}
630     }
631 }
632
633 pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath, id: HirId, span: Span) {
634     match *qpath {
635         QPath::Resolved(ref maybe_qself, ref path) => {
636             if let Some(ref qself) = *maybe_qself {
637                 visitor.visit_ty(qself);
638             }
639             visitor.visit_path(path, id)
640         }
641         QPath::TypeRelative(ref qself, ref segment) => {
642             visitor.visit_ty(qself);
643             visitor.visit_path_segment(span, segment);
644         }
645     }
646 }
647
648 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
649     for segment in &path.segments {
650         visitor.visit_path_segment(path.span, segment);
651     }
652 }
653
654 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
655                                              path_span: Span,
656                                              segment: &'v PathSegment) {
657     visitor.visit_ident(segment.ident);
658     if let Some(id) = segment.hir_id {
659         visitor.visit_id(id);
660     }
661     if let Some(ref args) = segment.args {
662         visitor.visit_generic_args(path_span, args);
663     }
664 }
665
666 pub fn walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V,
667                                              _path_span: Span,
668                                              generic_args: &'v GenericArgs) {
669     walk_list!(visitor, visit_generic_arg, &generic_args.args);
670     walk_list!(visitor, visit_assoc_type_binding, &generic_args.bindings);
671 }
672
673 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
674                                                    type_binding: &'v TypeBinding) {
675     visitor.visit_id(type_binding.hir_id);
676     visitor.visit_ident(type_binding.ident);
677     visitor.visit_ty(&type_binding.ty);
678 }
679
680 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
681     visitor.visit_id(pattern.hir_id);
682     match pattern.node {
683         PatKind::TupleStruct(ref qpath, ref children, _) => {
684             visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
685             walk_list!(visitor, visit_pat, children);
686         }
687         PatKind::Path(ref qpath) => {
688             visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
689         }
690         PatKind::Struct(ref qpath, ref fields, _) => {
691             visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
692             for field in fields {
693                 visitor.visit_id(field.node.hir_id);
694                 visitor.visit_ident(field.node.ident);
695                 visitor.visit_pat(&field.node.pat)
696             }
697         }
698         PatKind::Tuple(ref tuple_elements, _) => {
699             walk_list!(visitor, visit_pat, tuple_elements);
700         }
701         PatKind::Box(ref subpattern) |
702         PatKind::Ref(ref subpattern, _) => {
703             visitor.visit_pat(subpattern)
704         }
705         PatKind::Binding(_, _hir_id, ident, ref optional_subpattern) => {
706             visitor.visit_ident(ident);
707             walk_list!(visitor, visit_pat, optional_subpattern);
708         }
709         PatKind::Lit(ref expression) => visitor.visit_expr(expression),
710         PatKind::Range(ref lower_bound, ref upper_bound, _) => {
711             visitor.visit_expr(lower_bound);
712             visitor.visit_expr(upper_bound)
713         }
714         PatKind::Wild => (),
715         PatKind::Slice(ref prepatterns, ref slice_pattern, ref postpatterns) => {
716             walk_list!(visitor, visit_pat, prepatterns);
717             walk_list!(visitor, visit_pat, slice_pattern);
718             walk_list!(visitor, visit_pat, postpatterns);
719         }
720     }
721 }
722
723 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem) {
724     visitor.visit_id(foreign_item.hir_id);
725     visitor.visit_vis(&foreign_item.vis);
726     visitor.visit_ident(foreign_item.ident);
727
728     match foreign_item.node {
729         ForeignItemKind::Fn(ref function_declaration, ref param_names, ref generics) => {
730             visitor.visit_generics(generics);
731             visitor.visit_fn_decl(function_declaration);
732             for &param_name in param_names {
733                 visitor.visit_ident(param_name);
734             }
735         }
736         ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
737         ForeignItemKind::Type => (),
738     }
739
740     walk_list!(visitor, visit_attribute, &foreign_item.attrs);
741 }
742
743 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound) {
744     match *bound {
745         GenericBound::Trait(ref typ, modifier) => {
746             visitor.visit_poly_trait_ref(typ, modifier);
747         }
748         GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
749     }
750 }
751
752 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam) {
753     visitor.visit_id(param.hir_id);
754     walk_list!(visitor, visit_attribute, &param.attrs);
755     match param.name {
756         ParamName::Plain(ident) => visitor.visit_ident(ident),
757         ParamName::Error | ParamName::Fresh(_) => {}
758     }
759     match param.kind {
760         GenericParamKind::Lifetime { .. } => {}
761         GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
762         GenericParamKind::Const { ref ty } => visitor.visit_ty(ty),
763     }
764     walk_list!(visitor, visit_param_bound, &param.bounds);
765 }
766
767 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
768     walk_list!(visitor, visit_generic_param, &generics.params);
769     visitor.visit_id(generics.where_clause.hir_id);
770     walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
771 }
772
773 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
774     visitor: &mut V,
775     predicate: &'v WherePredicate)
776 {
777     match predicate {
778         &WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
779                                                             ref bounds,
780                                                             ref bound_generic_params,
781                                                             ..}) => {
782             visitor.visit_ty(bounded_ty);
783             walk_list!(visitor, visit_param_bound, bounds);
784             walk_list!(visitor, visit_generic_param, bound_generic_params);
785         }
786         &WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
787                                                               ref bounds,
788                                                               ..}) => {
789             visitor.visit_lifetime(lifetime);
790             walk_list!(visitor, visit_param_bound, bounds);
791         }
792         &WherePredicate::EqPredicate(WhereEqPredicate{hir_id,
793                                                       ref lhs_ty,
794                                                       ref rhs_ty,
795                                                       ..}) => {
796             visitor.visit_id(hir_id);
797             visitor.visit_ty(lhs_ty);
798             visitor.visit_ty(rhs_ty);
799         }
800     }
801 }
802
803 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
804     if let Return(ref output_ty) = *ret_ty {
805         visitor.visit_ty(output_ty)
806     }
807 }
808
809 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
810     for ty in &function_declaration.inputs {
811         visitor.visit_ty(ty)
812     }
813     walk_fn_ret_ty(visitor, &function_declaration.output)
814 }
815
816 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
817     match function_kind {
818         FnKind::ItemFn(_, generics, ..) => {
819             visitor.visit_generics(generics);
820         }
821         FnKind::Method(..) |
822         FnKind::Closure(_) => {}
823     }
824 }
825
826 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
827                                    function_kind: FnKind<'v>,
828                                    function_declaration: &'v FnDecl,
829                                    body_id: BodyId,
830                                    _span: Span,
831                                    id: HirId) {
832     visitor.visit_id(id);
833     visitor.visit_fn_decl(function_declaration);
834     walk_fn_kind(visitor, function_kind);
835     visitor.visit_nested_body(body_id)
836 }
837
838 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
839     visitor.visit_ident(trait_item.ident);
840     walk_list!(visitor, visit_attribute, &trait_item.attrs);
841     visitor.visit_generics(&trait_item.generics);
842     match trait_item.node {
843         TraitItemKind::Const(ref ty, default) => {
844             visitor.visit_id(trait_item.hir_id);
845             visitor.visit_ty(ty);
846             walk_list!(visitor, visit_nested_body, default);
847         }
848         TraitItemKind::Method(ref sig, TraitMethod::Required(ref param_names)) => {
849             visitor.visit_id(trait_item.hir_id);
850             visitor.visit_fn_decl(&sig.decl);
851             for &param_name in param_names {
852                 visitor.visit_ident(param_name);
853             }
854         }
855         TraitItemKind::Method(ref sig, TraitMethod::Provided(body_id)) => {
856             visitor.visit_fn(FnKind::Method(trait_item.ident,
857                                             sig,
858                                             None,
859                                             &trait_item.attrs),
860                              &sig.decl,
861                              body_id,
862                              trait_item.span,
863                              trait_item.hir_id);
864         }
865         TraitItemKind::Type(ref bounds, ref default) => {
866             visitor.visit_id(trait_item.hir_id);
867             walk_list!(visitor, visit_param_bound, bounds);
868             walk_list!(visitor, visit_ty, default);
869         }
870     }
871 }
872
873 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
874     // N.B., deliberately force a compilation error if/when new fields are added.
875     let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
876     visitor.visit_nested_trait_item(id);
877     visitor.visit_ident(ident);
878     visitor.visit_associated_item_kind(kind);
879     visitor.visit_defaultness(defaultness);
880 }
881
882 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
883     // N.B., deliberately force a compilation error if/when new fields are added.
884     let ImplItem {
885         hir_id: _,
886         ident,
887         ref vis,
888         ref defaultness,
889         ref attrs,
890         ref generics,
891         ref node,
892         span: _,
893     } = *impl_item;
894
895     visitor.visit_ident(ident);
896     visitor.visit_vis(vis);
897     visitor.visit_defaultness(defaultness);
898     walk_list!(visitor, visit_attribute, attrs);
899     visitor.visit_generics(generics);
900     match *node {
901         ImplItemKind::Const(ref ty, body) => {
902             visitor.visit_id(impl_item.hir_id);
903             visitor.visit_ty(ty);
904             visitor.visit_nested_body(body);
905         }
906         ImplItemKind::Method(ref sig, body_id) => {
907             visitor.visit_fn(FnKind::Method(impl_item.ident,
908                                             sig,
909                                             Some(&impl_item.vis),
910                                             &impl_item.attrs),
911                              &sig.decl,
912                              body_id,
913                              impl_item.span,
914                              impl_item.hir_id);
915         }
916         ImplItemKind::Type(ref ty) => {
917             visitor.visit_id(impl_item.hir_id);
918             visitor.visit_ty(ty);
919         }
920         ImplItemKind::Existential(ref bounds) => {
921             visitor.visit_id(impl_item.hir_id);
922             walk_list!(visitor, visit_param_bound, bounds);
923         }
924     }
925 }
926
927 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
928     // N.B., deliberately force a compilation error if/when new fields are added.
929     let ImplItemRef { id, ident, ref kind, span: _, ref vis, ref defaultness } = *impl_item_ref;
930     visitor.visit_nested_impl_item(id);
931     visitor.visit_ident(ident);
932     visitor.visit_associated_item_kind(kind);
933     visitor.visit_vis(vis);
934     visitor.visit_defaultness(defaultness);
935 }
936
937
938 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &'v VariantData) {
939     if let Some(ctor_hir_id) = struct_definition.ctor_hir_id() {
940         visitor.visit_id(ctor_hir_id);
941     }
942     walk_list!(visitor, visit_struct_field, struct_definition.fields());
943 }
944
945 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) {
946     visitor.visit_id(struct_field.hir_id);
947     visitor.visit_vis(&struct_field.vis);
948     visitor.visit_ident(struct_field.ident);
949     visitor.visit_ty(&struct_field.ty);
950     walk_list!(visitor, visit_attribute, &struct_field.attrs);
951 }
952
953 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
954     visitor.visit_id(block.hir_id);
955     walk_list!(visitor, visit_stmt, &block.stmts);
956     walk_list!(visitor, visit_expr, &block.expr);
957 }
958
959 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
960     visitor.visit_id(statement.hir_id);
961     match statement.node {
962         StmtKind::Local(ref local) => visitor.visit_local(local),
963         StmtKind::Item(item) => visitor.visit_nested_item(item),
964         StmtKind::Expr(ref expression) |
965         StmtKind::Semi(ref expression) => {
966             visitor.visit_expr(expression)
967         }
968     }
969 }
970
971 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
972     visitor.visit_id(constant.hir_id);
973     visitor.visit_nested_body(constant.body);
974 }
975
976 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
977     visitor.visit_id(expression.hir_id);
978     walk_list!(visitor, visit_attribute, expression.attrs.iter());
979     match expression.node {
980         ExprKind::Box(ref subexpression) => {
981             visitor.visit_expr(subexpression)
982         }
983         ExprKind::Array(ref subexpressions) => {
984             walk_list!(visitor, visit_expr, subexpressions);
985         }
986         ExprKind::Repeat(ref element, ref count) => {
987             visitor.visit_expr(element);
988             visitor.visit_anon_const(count)
989         }
990         ExprKind::Struct(ref qpath, ref fields, ref optional_base) => {
991             visitor.visit_qpath(qpath, expression.hir_id, expression.span);
992             for field in fields {
993                 visitor.visit_id(field.hir_id);
994                 visitor.visit_ident(field.ident);
995                 visitor.visit_expr(&field.expr)
996             }
997             walk_list!(visitor, visit_expr, optional_base);
998         }
999         ExprKind::Tup(ref subexpressions) => {
1000             walk_list!(visitor, visit_expr, subexpressions);
1001         }
1002         ExprKind::Call(ref callee_expression, ref arguments) => {
1003             visitor.visit_expr(callee_expression);
1004             walk_list!(visitor, visit_expr, arguments);
1005         }
1006         ExprKind::MethodCall(ref segment, _, ref arguments) => {
1007             visitor.visit_path_segment(expression.span, segment);
1008             walk_list!(visitor, visit_expr, arguments);
1009         }
1010         ExprKind::Binary(_, ref left_expression, ref right_expression) => {
1011             visitor.visit_expr(left_expression);
1012             visitor.visit_expr(right_expression)
1013         }
1014         ExprKind::AddrOf(_, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
1015             visitor.visit_expr(subexpression)
1016         }
1017         ExprKind::Lit(_) => {}
1018         ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
1019             visitor.visit_expr(subexpression);
1020             visitor.visit_ty(typ)
1021         }
1022         ExprKind::DropTemps(ref subexpression) => {
1023             visitor.visit_expr(subexpression);
1024         }
1025         ExprKind::While(ref subexpression, ref block, ref opt_label) => {
1026             walk_list!(visitor, visit_label, opt_label);
1027             visitor.visit_expr(subexpression);
1028             visitor.visit_block(block);
1029         }
1030         ExprKind::Loop(ref block, ref opt_label, _) => {
1031             walk_list!(visitor, visit_label, opt_label);
1032             visitor.visit_block(block);
1033         }
1034         ExprKind::Match(ref subexpression, ref arms, _) => {
1035             visitor.visit_expr(subexpression);
1036             walk_list!(visitor, visit_arm, arms);
1037         }
1038         ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => {
1039             visitor.visit_fn(FnKind::Closure(&expression.attrs),
1040                              function_declaration,
1041                              body,
1042                              expression.span,
1043                              expression.hir_id)
1044         }
1045         ExprKind::Block(ref block, ref opt_label) => {
1046             walk_list!(visitor, visit_label, opt_label);
1047             visitor.visit_block(block);
1048         }
1049         ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
1050             visitor.visit_expr(right_hand_expression);
1051             visitor.visit_expr(left_hand_expression)
1052         }
1053         ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
1054             visitor.visit_expr(right_expression);
1055             visitor.visit_expr(left_expression)
1056         }
1057         ExprKind::Field(ref subexpression, ident) => {
1058             visitor.visit_expr(subexpression);
1059             visitor.visit_ident(ident);
1060         }
1061         ExprKind::Index(ref main_expression, ref index_expression) => {
1062             visitor.visit_expr(main_expression);
1063             visitor.visit_expr(index_expression)
1064         }
1065         ExprKind::Path(ref qpath) => {
1066             visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1067         }
1068         ExprKind::Break(ref destination, ref opt_expr) => {
1069             if let Some(ref label) = destination.label {
1070                 visitor.visit_label(label);
1071             }
1072             walk_list!(visitor, visit_expr, opt_expr);
1073         }
1074         ExprKind::Continue(ref destination) => {
1075             if let Some(ref label) = destination.label {
1076                 visitor.visit_label(label);
1077             }
1078         }
1079         ExprKind::Ret(ref optional_expression) => {
1080             walk_list!(visitor, visit_expr, optional_expression);
1081         }
1082         ExprKind::InlineAsm(_, ref outputs, ref inputs) => {
1083             for expr in outputs.iter().chain(inputs.iter()) {
1084                 visitor.visit_expr(expr)
1085             }
1086         }
1087         ExprKind::Yield(ref subexpression) => {
1088             visitor.visit_expr(subexpression);
1089         }
1090         ExprKind::Err => {}
1091     }
1092 }
1093
1094 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
1095     visitor.visit_id(arm.hir_id);
1096     walk_list!(visitor, visit_pat, &arm.pats);
1097     if let Some(ref g) = arm.guard {
1098         match g {
1099             Guard::If(ref e) => visitor.visit_expr(e),
1100         }
1101     }
1102     visitor.visit_expr(&arm.body);
1103     walk_list!(visitor, visit_attribute, &arm.attrs);
1104 }
1105
1106 pub fn walk_vis<'v, V: Visitor<'v>>(visitor: &mut V, vis: &'v Visibility) {
1107     if let VisibilityKind::Restricted { ref path, hir_id } = vis.node {
1108         visitor.visit_id(hir_id);
1109         visitor.visit_path(path, hir_id)
1110     }
1111 }
1112
1113 pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssocItemKind) {
1114     // No visitable content here: this fn exists so you can call it if
1115     // the right thing to do, should content be added in the future,
1116     // would be to walk it.
1117 }
1118
1119 pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) {
1120     // No visitable content here: this fn exists so you can call it if
1121     // the right thing to do, should content be added in the future,
1122     // would be to walk it.
1123 }