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