]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/intravisit.rs
Take Const into account 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::{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     /// Visits 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     /// Visits 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             GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
338         }
339     }
340     fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
341         walk_lifetime(self, lifetime)
342     }
343     fn visit_qpath(&mut self, qpath: &'v QPath, id: HirId, span: Span) {
344         walk_qpath(self, qpath, id, span)
345     }
346     fn visit_path(&mut self, path: &'v Path, _id: HirId) {
347         walk_path(self, path)
348     }
349     fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
350         walk_path_segment(self, path_span, path_segment)
351     }
352     fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs) {
353         walk_generic_args(self, path_span, generic_args)
354     }
355     fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
356         walk_assoc_type_binding(self, type_binding)
357     }
358     fn visit_attribute(&mut self, _attr: &'v Attribute) {
359     }
360     fn visit_macro_def(&mut self, macro_def: &'v MacroDef) {
361         walk_macro_def(self, macro_def)
362     }
363     fn visit_vis(&mut self, vis: &'v Visibility) {
364         walk_vis(self, vis)
365     }
366     fn visit_associated_item_kind(&mut self, kind: &'v AssociatedItemKind) {
367         walk_associated_item_kind(self, kind);
368     }
369     fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
370         walk_defaultness(self, defaultness);
371     }
372 }
373
374 /// Walks the contents of a crate. See also `Crate::visit_all_items`.
375 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
376     visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
377     walk_list!(visitor, visit_attribute, &krate.attrs);
378     walk_list!(visitor, visit_macro_def, &krate.exported_macros);
379 }
380
381 pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef) {
382     visitor.visit_id(macro_def.id);
383     visitor.visit_name(macro_def.span, macro_def.name);
384     walk_list!(visitor, visit_attribute, &macro_def.attrs);
385 }
386
387 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod, mod_node_id: NodeId) {
388     visitor.visit_id(mod_node_id);
389     for &item_id in &module.item_ids {
390         visitor.visit_nested_item(item_id);
391     }
392 }
393
394 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body) {
395     for argument in &body.arguments {
396         visitor.visit_id(argument.id);
397         visitor.visit_pat(&argument.pat);
398     }
399     visitor.visit_expr(&body.value);
400 }
401
402 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
403     // Intentionally visiting the expr first - the initialization expr
404     // dominates the local's definition.
405     walk_list!(visitor, visit_expr, &local.init);
406     walk_list!(visitor, visit_attribute, local.attrs.iter());
407     visitor.visit_id(local.id);
408     visitor.visit_pat(&local.pat);
409     walk_list!(visitor, visit_ty, &local.ty);
410 }
411
412 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
413     visitor.visit_name(ident.span, ident.name);
414 }
415
416 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
417     visitor.visit_ident(label.ident);
418 }
419
420 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
421     visitor.visit_id(lifetime.id);
422     match lifetime.name {
423         LifetimeName::Param(ParamName::Plain(ident)) => {
424             visitor.visit_ident(ident);
425         }
426         LifetimeName::Param(ParamName::Fresh(_)) |
427         LifetimeName::Param(ParamName::Error) |
428         LifetimeName::Static |
429         LifetimeName::Error |
430         LifetimeName::Implicit |
431         LifetimeName::Underscore => {}
432     }
433 }
434
435 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
436                                   trait_ref: &'v PolyTraitRef,
437                                   _modifier: TraitBoundModifier)
438     where V: Visitor<'v>
439 {
440     walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
441     visitor.visit_trait_ref(&trait_ref.trait_ref);
442 }
443
444 pub fn walk_trait_ref<'v, V>(visitor: &mut V, trait_ref: &'v TraitRef)
445     where V: Visitor<'v>
446 {
447     visitor.visit_id(trait_ref.ref_id);
448     visitor.visit_path(&trait_ref.path, trait_ref.hir_ref_id)
449 }
450
451 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
452     visitor.visit_vis(&item.vis);
453     visitor.visit_ident(item.ident);
454     match item.node {
455         ItemKind::ExternCrate(orig_name) => {
456             visitor.visit_id(item.id);
457             if let Some(orig_name) = orig_name {
458                 visitor.visit_name(item.span, orig_name);
459             }
460         }
461         ItemKind::Use(ref path, _) => {
462             visitor.visit_use(path, item.id, item.hir_id);
463         }
464         ItemKind::Static(ref typ, _, body) |
465         ItemKind::Const(ref typ, body) => {
466             visitor.visit_id(item.id);
467             visitor.visit_ty(typ);
468             visitor.visit_nested_body(body);
469         }
470         ItemKind::Fn(ref declaration, header, ref generics, body_id) => {
471             visitor.visit_fn(FnKind::ItemFn(item.ident,
472                                             generics,
473                                             header,
474                                             &item.vis,
475                                             &item.attrs),
476                              declaration,
477                              body_id,
478                              item.span,
479                              item.id)
480         }
481         ItemKind::Mod(ref module) => {
482             // `visit_mod()` takes care of visiting the `Item`'s `NodeId`.
483             visitor.visit_mod(module, item.span, item.id)
484         }
485         ItemKind::ForeignMod(ref foreign_module) => {
486             visitor.visit_id(item.id);
487             walk_list!(visitor, visit_foreign_item, &foreign_module.items);
488         }
489         ItemKind::GlobalAsm(_) => {
490             visitor.visit_id(item.id);
491         }
492         ItemKind::Ty(ref typ, ref type_parameters) => {
493             visitor.visit_id(item.id);
494             visitor.visit_ty(typ);
495             visitor.visit_generics(type_parameters)
496         }
497         ItemKind::Existential(ExistTy {ref generics, ref bounds, impl_trait_fn}) => {
498             visitor.visit_id(item.id);
499             walk_generics(visitor, generics);
500             walk_list!(visitor, visit_param_bound, bounds);
501             if let Some(impl_trait_fn) = impl_trait_fn {
502                 visitor.visit_def_mention(Def::Fn(impl_trait_fn))
503             }
504         }
505         ItemKind::Enum(ref enum_definition, ref type_parameters) => {
506             visitor.visit_generics(type_parameters);
507             // `visit_enum_def()` takes care of visiting the `Item`'s `NodeId`.
508             visitor.visit_enum_def(enum_definition, type_parameters, item.id, item.span)
509         }
510         ItemKind::Impl(
511             ..,
512             ref type_parameters,
513             ref opt_trait_reference,
514             ref typ,
515             ref impl_item_refs
516         ) => {
517             visitor.visit_id(item.id);
518             visitor.visit_generics(type_parameters);
519             walk_list!(visitor, visit_trait_ref, opt_trait_reference);
520             visitor.visit_ty(typ);
521             walk_list!(visitor, visit_impl_item_ref, impl_item_refs);
522         }
523         ItemKind::Struct(ref struct_definition, ref generics) |
524         ItemKind::Union(ref struct_definition, ref generics) => {
525             visitor.visit_generics(generics);
526             visitor.visit_id(item.id);
527             visitor.visit_variant_data(struct_definition, item.ident.name, generics, item.id,
528                                        item.span);
529         }
530         ItemKind::Trait(.., ref generics, ref bounds, ref trait_item_refs) => {
531             visitor.visit_id(item.id);
532             visitor.visit_generics(generics);
533             walk_list!(visitor, visit_param_bound, bounds);
534             walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
535         }
536         ItemKind::TraitAlias(ref generics, ref bounds) => {
537             visitor.visit_id(item.id);
538             visitor.visit_generics(generics);
539             walk_list!(visitor, visit_param_bound, bounds);
540         }
541     }
542     walk_list!(visitor, visit_attribute, &item.attrs);
543 }
544
545 pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V,
546                                     path: &'v Path,
547                                     item_id: NodeId,
548                                     hir_id: HirId) {
549     visitor.visit_id(item_id);
550     visitor.visit_path(path, hir_id);
551 }
552
553 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
554                                          enum_definition: &'v EnumDef,
555                                          generics: &'v Generics,
556                                          item_id: NodeId) {
557     visitor.visit_id(item_id);
558     walk_list!(visitor,
559                visit_variant,
560                &enum_definition.variants,
561                generics,
562                item_id);
563 }
564
565 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
566                                         variant: &'v Variant,
567                                         generics: &'v Generics,
568                                         parent_item_id: NodeId) {
569     visitor.visit_ident(variant.node.ident);
570     visitor.visit_variant_data(&variant.node.data,
571                                variant.node.ident.name,
572                                generics,
573                                parent_item_id,
574                                variant.span);
575     walk_list!(visitor, visit_anon_const, &variant.node.disr_expr);
576     walk_list!(visitor, visit_attribute, &variant.node.attrs);
577 }
578
579 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
580     visitor.visit_id(typ.id);
581
582     match typ.node {
583         TyKind::Slice(ref ty) => {
584             visitor.visit_ty(ty)
585         }
586         TyKind::Ptr(ref mutable_type) => {
587             visitor.visit_ty(&mutable_type.ty)
588         }
589         TyKind::Rptr(ref lifetime, ref mutable_type) => {
590             visitor.visit_lifetime(lifetime);
591             visitor.visit_ty(&mutable_type.ty)
592         }
593         TyKind::Never => {},
594         TyKind::Tup(ref tuple_element_types) => {
595             walk_list!(visitor, visit_ty, tuple_element_types);
596         }
597         TyKind::BareFn(ref function_declaration) => {
598             walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
599             visitor.visit_fn_decl(&function_declaration.decl);
600         }
601         TyKind::Path(ref qpath) => {
602             visitor.visit_qpath(qpath, typ.hir_id, typ.span);
603         }
604         TyKind::Def(item_id, ref lifetimes) => {
605             visitor.visit_nested_item(item_id);
606             walk_list!(visitor, visit_generic_arg, lifetimes);
607         }
608         TyKind::Array(ref ty, ref length) => {
609             visitor.visit_ty(ty);
610             visitor.visit_anon_const(length)
611         }
612         TyKind::TraitObject(ref bounds, ref lifetime) => {
613             for bound in bounds {
614                 visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
615             }
616             visitor.visit_lifetime(lifetime);
617         }
618         TyKind::Typeof(ref expression) => {
619             visitor.visit_anon_const(expression)
620         }
621         TyKind::Infer | TyKind::Err => {}
622     }
623 }
624
625 pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath, id: HirId, span: Span) {
626     match *qpath {
627         QPath::Resolved(ref maybe_qself, ref path) => {
628             if let Some(ref qself) = *maybe_qself {
629                 visitor.visit_ty(qself);
630             }
631             visitor.visit_path(path, id)
632         }
633         QPath::TypeRelative(ref qself, ref segment) => {
634             visitor.visit_ty(qself);
635             visitor.visit_path_segment(span, segment);
636         }
637     }
638 }
639
640 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
641     visitor.visit_def_mention(path.def);
642     for segment in &path.segments {
643         visitor.visit_path_segment(path.span, segment);
644     }
645 }
646
647 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
648                                              path_span: Span,
649                                              segment: &'v PathSegment) {
650     visitor.visit_ident(segment.ident);
651     if let Some(id) = segment.id {
652         visitor.visit_id(id);
653     }
654     if let Some(ref args) = segment.args {
655         visitor.visit_generic_args(path_span, args);
656     }
657 }
658
659 pub fn walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V,
660                                              _path_span: Span,
661                                              generic_args: &'v GenericArgs) {
662     walk_list!(visitor, visit_generic_arg, &generic_args.args);
663     walk_list!(visitor, visit_assoc_type_binding, &generic_args.bindings);
664 }
665
666 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
667                                                    type_binding: &'v TypeBinding) {
668     visitor.visit_id(type_binding.id);
669     visitor.visit_ident(type_binding.ident);
670     visitor.visit_ty(&type_binding.ty);
671 }
672
673 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
674     visitor.visit_id(pattern.id);
675     match pattern.node {
676         PatKind::TupleStruct(ref qpath, ref children, _) => {
677             visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
678             walk_list!(visitor, visit_pat, children);
679         }
680         PatKind::Path(ref qpath) => {
681             visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
682         }
683         PatKind::Struct(ref qpath, ref fields, _) => {
684             visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
685             for field in fields {
686                 visitor.visit_id(field.node.id);
687                 visitor.visit_ident(field.node.ident);
688                 visitor.visit_pat(&field.node.pat)
689             }
690         }
691         PatKind::Tuple(ref tuple_elements, _) => {
692             walk_list!(visitor, visit_pat, tuple_elements);
693         }
694         PatKind::Box(ref subpattern) |
695         PatKind::Ref(ref subpattern, _) => {
696             visitor.visit_pat(subpattern)
697         }
698         PatKind::Binding(_, canonical_id, _hir_id, ident, ref optional_subpattern) => {
699             visitor.visit_def_mention(Def::Local(canonical_id));
700             visitor.visit_ident(ident);
701             walk_list!(visitor, visit_pat, optional_subpattern);
702         }
703         PatKind::Lit(ref expression) => visitor.visit_expr(expression),
704         PatKind::Range(ref lower_bound, ref upper_bound, _) => {
705             visitor.visit_expr(lower_bound);
706             visitor.visit_expr(upper_bound)
707         }
708         PatKind::Wild => (),
709         PatKind::Slice(ref prepatterns, ref slice_pattern, ref postpatterns) => {
710             walk_list!(visitor, visit_pat, prepatterns);
711             walk_list!(visitor, visit_pat, slice_pattern);
712             walk_list!(visitor, visit_pat, postpatterns);
713         }
714     }
715 }
716
717 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem) {
718     visitor.visit_id(foreign_item.id);
719     visitor.visit_vis(&foreign_item.vis);
720     visitor.visit_ident(foreign_item.ident);
721
722     match foreign_item.node {
723         ForeignItemKind::Fn(ref function_declaration, ref param_names, ref generics) => {
724             visitor.visit_generics(generics);
725             visitor.visit_fn_decl(function_declaration);
726             for &param_name in param_names {
727                 visitor.visit_ident(param_name);
728             }
729         }
730         ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
731         ForeignItemKind::Type => (),
732     }
733
734     walk_list!(visitor, visit_attribute, &foreign_item.attrs);
735 }
736
737 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound) {
738     match *bound {
739         GenericBound::Trait(ref typ, modifier) => {
740             visitor.visit_poly_trait_ref(typ, modifier);
741         }
742         GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
743     }
744 }
745
746 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam) {
747     visitor.visit_id(param.id);
748     walk_list!(visitor, visit_attribute, &param.attrs);
749     match param.name {
750         ParamName::Plain(ident) => visitor.visit_ident(ident),
751         ParamName::Error | ParamName::Fresh(_) => {}
752     }
753     match param.kind {
754         GenericParamKind::Lifetime { .. } => {}
755         GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
756         GenericParamKind::Const { ref ty } => visitor.visit_ty(ty),
757     }
758     walk_list!(visitor, visit_param_bound, &param.bounds);
759 }
760
761 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
762     walk_list!(visitor, visit_generic_param, &generics.params);
763     visitor.visit_id(generics.where_clause.id);
764     walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
765 }
766
767 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
768     visitor: &mut V,
769     predicate: &'v WherePredicate)
770 {
771     match predicate {
772         &WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
773                                                             ref bounds,
774                                                             ref bound_generic_params,
775                                                             ..}) => {
776             visitor.visit_ty(bounded_ty);
777             walk_list!(visitor, visit_param_bound, bounds);
778             walk_list!(visitor, visit_generic_param, bound_generic_params);
779         }
780         &WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
781                                                               ref bounds,
782                                                               ..}) => {
783             visitor.visit_lifetime(lifetime);
784             walk_list!(visitor, visit_param_bound, bounds);
785         }
786         &WherePredicate::EqPredicate(WhereEqPredicate{id,
787                                                       ref lhs_ty,
788                                                       ref rhs_ty,
789                                                       ..}) => {
790             visitor.visit_id(id);
791             visitor.visit_ty(lhs_ty);
792             visitor.visit_ty(rhs_ty);
793         }
794     }
795 }
796
797 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
798     if let Return(ref output_ty) = *ret_ty {
799         visitor.visit_ty(output_ty)
800     }
801 }
802
803 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
804     for ty in &function_declaration.inputs {
805         visitor.visit_ty(ty)
806     }
807     walk_fn_ret_ty(visitor, &function_declaration.output)
808 }
809
810 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
811     match function_kind {
812         FnKind::ItemFn(_, generics, ..) => {
813             visitor.visit_generics(generics);
814         }
815         FnKind::Method(..) |
816         FnKind::Closure(_) => {}
817     }
818 }
819
820 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
821                                    function_kind: FnKind<'v>,
822                                    function_declaration: &'v FnDecl,
823                                    body_id: BodyId,
824                                    _span: Span,
825                                    id: NodeId) {
826     visitor.visit_id(id);
827     visitor.visit_fn_decl(function_declaration);
828     walk_fn_kind(visitor, function_kind);
829     visitor.visit_nested_body(body_id)
830 }
831
832 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
833     visitor.visit_ident(trait_item.ident);
834     walk_list!(visitor, visit_attribute, &trait_item.attrs);
835     visitor.visit_generics(&trait_item.generics);
836     match trait_item.node {
837         TraitItemKind::Const(ref ty, default) => {
838             visitor.visit_id(trait_item.id);
839             visitor.visit_ty(ty);
840             walk_list!(visitor, visit_nested_body, default);
841         }
842         TraitItemKind::Method(ref sig, TraitMethod::Required(ref param_names)) => {
843             visitor.visit_id(trait_item.id);
844             visitor.visit_fn_decl(&sig.decl);
845             for &param_name in param_names {
846                 visitor.visit_ident(param_name);
847             }
848         }
849         TraitItemKind::Method(ref sig, TraitMethod::Provided(body_id)) => {
850             visitor.visit_fn(FnKind::Method(trait_item.ident,
851                                             sig,
852                                             None,
853                                             &trait_item.attrs),
854                              &sig.decl,
855                              body_id,
856                              trait_item.span,
857                              trait_item.id);
858         }
859         TraitItemKind::Type(ref bounds, ref default) => {
860             visitor.visit_id(trait_item.id);
861             walk_list!(visitor, visit_param_bound, bounds);
862             walk_list!(visitor, visit_ty, default);
863         }
864     }
865 }
866
867 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
868     // N.B., deliberately force a compilation error if/when new fields are added.
869     let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
870     visitor.visit_nested_trait_item(id);
871     visitor.visit_ident(ident);
872     visitor.visit_associated_item_kind(kind);
873     visitor.visit_defaultness(defaultness);
874 }
875
876 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
877     // N.B., deliberately force a compilation error if/when new fields are added.
878     let ImplItem {
879         id: _,
880         hir_id: _,
881         ident,
882         ref vis,
883         ref defaultness,
884         ref attrs,
885         ref generics,
886         ref node,
887         span: _,
888     } = *impl_item;
889
890     visitor.visit_ident(ident);
891     visitor.visit_vis(vis);
892     visitor.visit_defaultness(defaultness);
893     walk_list!(visitor, visit_attribute, attrs);
894     visitor.visit_generics(generics);
895     match *node {
896         ImplItemKind::Const(ref ty, body) => {
897             visitor.visit_id(impl_item.id);
898             visitor.visit_ty(ty);
899             visitor.visit_nested_body(body);
900         }
901         ImplItemKind::Method(ref sig, body_id) => {
902             visitor.visit_fn(FnKind::Method(impl_item.ident,
903                                             sig,
904                                             Some(&impl_item.vis),
905                                             &impl_item.attrs),
906                              &sig.decl,
907                              body_id,
908                              impl_item.span,
909                              impl_item.id);
910         }
911         ImplItemKind::Type(ref ty) => {
912             visitor.visit_id(impl_item.id);
913             visitor.visit_ty(ty);
914         }
915         ImplItemKind::Existential(ref bounds) => {
916             visitor.visit_id(impl_item.id);
917             walk_list!(visitor, visit_param_bound, bounds);
918         }
919     }
920 }
921
922 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
923     // N.B., deliberately force a compilation error if/when new fields are added.
924     let ImplItemRef { id, ident, ref kind, span: _, ref vis, ref defaultness } = *impl_item_ref;
925     visitor.visit_nested_impl_item(id);
926     visitor.visit_ident(ident);
927     visitor.visit_associated_item_kind(kind);
928     visitor.visit_vis(vis);
929     visitor.visit_defaultness(defaultness);
930 }
931
932
933 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &'v VariantData) {
934     visitor.visit_id(struct_definition.id());
935     walk_list!(visitor, visit_struct_field, struct_definition.fields());
936 }
937
938 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) {
939     visitor.visit_id(struct_field.id);
940     visitor.visit_vis(&struct_field.vis);
941     visitor.visit_ident(struct_field.ident);
942     visitor.visit_ty(&struct_field.ty);
943     walk_list!(visitor, visit_attribute, &struct_field.attrs);
944 }
945
946 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
947     visitor.visit_id(block.id);
948     walk_list!(visitor, visit_stmt, &block.stmts);
949     walk_list!(visitor, visit_expr, &block.expr);
950 }
951
952 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
953     visitor.visit_id(statement.id);
954     match statement.node {
955         StmtKind::Local(ref local) => visitor.visit_local(local),
956         StmtKind::Item(ref item) => visitor.visit_nested_item(**item),
957         StmtKind::Expr(ref expression) |
958         StmtKind::Semi(ref expression) => {
959             visitor.visit_expr(expression)
960         }
961     }
962 }
963
964 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
965     visitor.visit_id(constant.id);
966     visitor.visit_nested_body(constant.body);
967 }
968
969 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
970     visitor.visit_id(expression.id);
971     walk_list!(visitor, visit_attribute, expression.attrs.iter());
972     match expression.node {
973         ExprKind::Box(ref subexpression) => {
974             visitor.visit_expr(subexpression)
975         }
976         ExprKind::Array(ref subexpressions) => {
977             walk_list!(visitor, visit_expr, subexpressions);
978         }
979         ExprKind::Repeat(ref element, ref count) => {
980             visitor.visit_expr(element);
981             visitor.visit_anon_const(count)
982         }
983         ExprKind::Struct(ref qpath, ref fields, ref optional_base) => {
984             visitor.visit_qpath(qpath, expression.hir_id, expression.span);
985             for field in fields {
986                 visitor.visit_id(field.id);
987                 visitor.visit_ident(field.ident);
988                 visitor.visit_expr(&field.expr)
989             }
990             walk_list!(visitor, visit_expr, optional_base);
991         }
992         ExprKind::Tup(ref subexpressions) => {
993             walk_list!(visitor, visit_expr, subexpressions);
994         }
995         ExprKind::Call(ref callee_expression, ref arguments) => {
996             visitor.visit_expr(callee_expression);
997             walk_list!(visitor, visit_expr, arguments);
998         }
999         ExprKind::MethodCall(ref segment, _, ref arguments) => {
1000             visitor.visit_path_segment(expression.span, segment);
1001             walk_list!(visitor, visit_expr, arguments);
1002         }
1003         ExprKind::Binary(_, ref left_expression, ref right_expression) => {
1004             visitor.visit_expr(left_expression);
1005             visitor.visit_expr(right_expression)
1006         }
1007         ExprKind::AddrOf(_, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
1008             visitor.visit_expr(subexpression)
1009         }
1010         ExprKind::Lit(_) => {}
1011         ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
1012             visitor.visit_expr(subexpression);
1013             visitor.visit_ty(typ)
1014         }
1015         ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
1016             visitor.visit_expr(head_expression);
1017             visitor.visit_expr(if_block);
1018             walk_list!(visitor, visit_expr, optional_else);
1019         }
1020         ExprKind::While(ref subexpression, ref block, ref opt_label) => {
1021             walk_list!(visitor, visit_label, opt_label);
1022             visitor.visit_expr(subexpression);
1023             visitor.visit_block(block);
1024         }
1025         ExprKind::Loop(ref block, ref opt_label, _) => {
1026             walk_list!(visitor, visit_label, opt_label);
1027             visitor.visit_block(block);
1028         }
1029         ExprKind::Match(ref subexpression, ref arms, _) => {
1030             visitor.visit_expr(subexpression);
1031             walk_list!(visitor, visit_arm, arms);
1032         }
1033         ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => {
1034             visitor.visit_fn(FnKind::Closure(&expression.attrs),
1035                              function_declaration,
1036                              body,
1037                              expression.span,
1038                              expression.id)
1039         }
1040         ExprKind::Block(ref block, ref opt_label) => {
1041             walk_list!(visitor, visit_label, opt_label);
1042             visitor.visit_block(block);
1043         }
1044         ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
1045             visitor.visit_expr(right_hand_expression);
1046             visitor.visit_expr(left_hand_expression)
1047         }
1048         ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
1049             visitor.visit_expr(right_expression);
1050             visitor.visit_expr(left_expression)
1051         }
1052         ExprKind::Field(ref subexpression, ident) => {
1053             visitor.visit_expr(subexpression);
1054             visitor.visit_ident(ident);
1055         }
1056         ExprKind::Index(ref main_expression, ref index_expression) => {
1057             visitor.visit_expr(main_expression);
1058             visitor.visit_expr(index_expression)
1059         }
1060         ExprKind::Path(ref qpath) => {
1061             visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1062         }
1063         ExprKind::Break(ref destination, ref opt_expr) => {
1064             if let Some(ref label) = destination.label {
1065                 visitor.visit_label(label);
1066                 if let Ok(node_id) = destination.target_id {
1067                     visitor.visit_def_mention(Def::Label(node_id))
1068                 }
1069             }
1070             walk_list!(visitor, visit_expr, opt_expr);
1071         }
1072         ExprKind::Continue(ref destination) => {
1073             if let Some(ref label) = destination.label {
1074                 visitor.visit_label(label);
1075                 if let Ok(node_id) = destination.target_id {
1076                     visitor.visit_def_mention(Def::Label(node_id))
1077                 }
1078             }
1079         }
1080         ExprKind::Ret(ref optional_expression) => {
1081             walk_list!(visitor, visit_expr, optional_expression);
1082         }
1083         ExprKind::InlineAsm(_, ref outputs, ref inputs) => {
1084             for expr in outputs.iter().chain(inputs.iter()) {
1085                 visitor.visit_expr(expr)
1086             }
1087         }
1088         ExprKind::Yield(ref subexpression) => {
1089             visitor.visit_expr(subexpression);
1090         }
1091         ExprKind::Err => {}
1092     }
1093 }
1094
1095 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
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, id, hir_id } = vis.node {
1108         visitor.visit_id(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 AssociatedItemKind) {
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 }