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