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.
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.
11 //! HIR walker for walking the contents of nodes.
13 //! **For an overview of the visitor strategy, see the docs on the
14 //! `super::itemlikevisit::ItemLikeVisitor` trait.**
16 //! If you have decided to use this visitor, here are some general
17 //! notes on how to do it:
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.
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`
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
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).
41 //! This order consistency is required in a few places in rustc, for
42 //! example generator inference, and possibly also HIR borrowck.
44 use syntax::ast::{NodeId, CRATE_NODE_ID, Ident, Name, Attribute};
48 use hir::map::{self, Map};
49 use super::itemlikevisit::DeepVisitor;
53 use std::result::Result::Err;
55 #[derive(Copy, Clone)]
57 /// #[xxx] pub async/const/extern "Abi" fn foo()
58 ItemFn(Name, &'a Generics, FnHeader, &'a Visibility, &'a [Attribute]),
61 Method(Ident, &'a MethodSig, Option<&'a Visibility>, &'a [Attribute]),
64 Closure(&'a [Attribute]),
68 pub fn attrs(&self) -> &'a [Attribute] {
70 FnKind::ItemFn(.., attrs) => attrs,
71 FnKind::Method(.., attrs) => attrs,
72 FnKind::Closure(attrs) => attrs,
77 /// Specifies what nested things a visitor wants to visit. The most
78 /// common choice is `OnlyBodies`, which will cause the visitor to
79 /// visit fn bodies for fns that it encounters, but skip over nested
82 /// See the comments on `ItemLikeVisitor` for more details on the overall
84 pub enum NestedVisitorMap<'this, 'tcx: 'this> {
85 /// Do not visit any nested things. When you add a new
86 /// "non-nested" thing, you will want to audit such uses to see if
87 /// they remain valid.
89 /// Use this if you are only walking some particular kind of tree
90 /// (i.e., a type, or fn signature) and you don't want to thread a
94 /// Do not visit nested item-like things, but visit nested things
95 /// that are inside of an item-like.
97 /// **This is the most common choice.** A very common pattern is
98 /// to use `visit_all_item_likes()` as an outer loop,
99 /// and to have the visitor that visits the contents of each item
100 /// using this setting.
101 OnlyBodies(&'this Map<'tcx>),
103 /// Visit all nested things, including item-likes.
105 /// **This is an unusual choice.** It is used when you want to
106 /// process everything within their lexical context. Typically you
107 /// kick off the visit by doing `walk_krate()`.
108 All(&'this Map<'tcx>),
111 impl<'this, 'tcx> NestedVisitorMap<'this, 'tcx> {
112 /// Returns the map to use for an "intra item-like" thing (if any).
113 /// e.g., function body.
114 pub fn intra(self) -> Option<&'this Map<'tcx>> {
116 NestedVisitorMap::None => None,
117 NestedVisitorMap::OnlyBodies(map) => Some(map),
118 NestedVisitorMap::All(map) => Some(map),
122 /// Returns the map to use for an "item-like" thing (if any).
123 /// e.g., item, impl-item.
124 pub fn inter(self) -> Option<&'this Map<'tcx>> {
126 NestedVisitorMap::None => None,
127 NestedVisitorMap::OnlyBodies(_) => None,
128 NestedVisitorMap::All(map) => Some(map),
133 /// Each method of the Visitor trait is a hook to be potentially
134 /// overridden. Each method's default implementation recursively visits
135 /// the substructure of the input via the corresponding `walk` method;
136 /// e.g. the `visit_mod` method by default calls `intravisit::walk_mod`.
138 /// Note that this visitor does NOT visit nested items by default
139 /// (this is why the module is called `intravisit`, to distinguish it
140 /// from the AST's `visit` module, which acts differently). If you
141 /// simply want to visit all items in the crate in some order, you
142 /// should call `Crate::visit_all_items`. Otherwise, see the comment
143 /// on `visit_nested_item` for details on how to visit nested items.
145 /// If you want to ensure that your code handles every variant
146 /// explicitly, you need to override each method. (And you also need
147 /// to monitor future changes to `Visitor` in case a new method with a
148 /// new default implementation gets introduced.)
149 pub trait Visitor<'v> : Sized {
150 ///////////////////////////////////////////////////////////////////////////
153 /// The default versions of the `visit_nested_XXX` routines invoke
154 /// this method to get a map to use. By selecting an enum variant,
155 /// you control which kinds of nested HIR are visited; see
156 /// `NestedVisitorMap` for details. By "nested HIR", we are
157 /// referring to bits of HIR that are not directly embedded within
158 /// one another but rather indirectly, through a table in the
159 /// crate. This is done to control dependencies during incremental
160 /// compilation: the non-inline bits of HIR can be tracked and
161 /// hashed separately.
163 /// **If for some reason you want the nested behavior, but don't
164 /// have a `Map` at your disposal:** then you should override the
165 /// `visit_nested_XXX` methods, and override this method to
166 /// `panic!()`. This way, if a new `visit_nested_XXX` variant is
167 /// added in the future, we will see the panic in your code and
168 /// fix it appropriately.
169 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v>;
171 /// Invoked when a nested item is encountered. By default does
172 /// nothing unless you override `nested_visit_map` to return
173 /// `Some(_)`, in which case it will walk the item. **You probably
174 /// don't want to override this method** -- instead, override
175 /// `nested_visit_map` or use the "shallow" or "deep" visit
176 /// patterns described on `itemlikevisit::ItemLikeVisitor`. The only
177 /// reason to override this method is if you want a nested pattern
178 /// but cannot supply a `Map`; see `nested_visit_map` for advice.
179 #[allow(unused_variables)]
180 fn visit_nested_item(&mut self, id: ItemId) {
181 let opt_item = self.nested_visit_map().inter().map(|map| map.expect_item(id.id));
182 if let Some(item) = opt_item {
183 self.visit_item(item);
187 /// Like `visit_nested_item()`, but for trait items. See
188 /// `visit_nested_item()` for advice on when to override this
190 #[allow(unused_variables)]
191 fn visit_nested_trait_item(&mut self, id: TraitItemId) {
192 let opt_item = self.nested_visit_map().inter().map(|map| map.trait_item(id));
193 if let Some(item) = opt_item {
194 self.visit_trait_item(item);
198 /// Like `visit_nested_item()`, but for impl items. See
199 /// `visit_nested_item()` for advice on when to override this
201 #[allow(unused_variables)]
202 fn visit_nested_impl_item(&mut self, id: ImplItemId) {
203 let opt_item = self.nested_visit_map().inter().map(|map| map.impl_item(id));
204 if let Some(item) = opt_item {
205 self.visit_impl_item(item);
209 /// Invoked to visit the body of a function, method or closure. Like
210 /// visit_nested_item, does nothing by default unless you override
211 /// `nested_visit_map` to return `Some(_)`, in which case it will walk the
213 fn visit_nested_body(&mut self, id: BodyId) {
214 let opt_body = self.nested_visit_map().intra().map(|map| map.body(id));
215 if let Some(body) = opt_body {
216 self.visit_body(body);
220 /// Visit the top-level item and (optionally) nested items / impl items. See
221 /// `visit_nested_item` for details.
222 fn visit_item(&mut self, i: &'v Item) {
226 fn visit_body(&mut self, b: &'v Body) {
230 /// When invoking `visit_all_item_likes()`, you need to supply an
231 /// item-like visitor. This method converts a "intra-visit"
232 /// visitor into an item-like visitor that walks the entire tree.
233 /// If you use this, you probably don't want to process the
234 /// contents of nested item-like things, since the outer loop will
235 /// visit them as well.
236 fn as_deep_visitor<'s>(&'s mut self) -> DeepVisitor<'s, Self> {
237 DeepVisitor::new(self)
240 ///////////////////////////////////////////////////////////////////////////
242 fn visit_id(&mut self, _node_id: NodeId) {
245 fn visit_def_mention(&mut self, _def: Def) {
248 fn visit_name(&mut self, _span: Span, _name: Name) {
251 fn visit_ident(&mut self, ident: Ident) {
252 walk_ident(self, ident)
254 fn visit_mod(&mut self, m: &'v Mod, _s: Span, n: NodeId) {
257 fn visit_foreign_item(&mut self, i: &'v ForeignItem) {
258 walk_foreign_item(self, i)
260 fn visit_local(&mut self, l: &'v Local) {
263 fn visit_block(&mut self, b: &'v Block) {
266 fn visit_stmt(&mut self, s: &'v Stmt) {
269 fn visit_arm(&mut self, a: &'v Arm) {
272 fn visit_pat(&mut self, p: &'v Pat) {
275 fn visit_decl(&mut self, d: &'v Decl) {
278 fn visit_anon_const(&mut self, c: &'v AnonConst) {
279 walk_anon_const(self, c)
281 fn visit_expr(&mut self, ex: &'v Expr) {
284 fn visit_ty(&mut self, t: &'v Ty) {
287 fn visit_generic_param(&mut self, p: &'v GenericParam) {
288 walk_generic_param(self, p)
290 fn visit_generics(&mut self, g: &'v Generics) {
291 walk_generics(self, g)
293 fn visit_where_predicate(&mut self, predicate: &'v WherePredicate) {
294 walk_where_predicate(self, predicate)
296 fn visit_fn_decl(&mut self, fd: &'v FnDecl) {
297 walk_fn_decl(self, fd)
299 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: BodyId, s: Span, id: NodeId) {
300 walk_fn(self, fk, fd, b, s, id)
302 fn visit_trait_item(&mut self, ti: &'v TraitItem) {
303 walk_trait_item(self, ti)
305 fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
306 walk_trait_item_ref(self, ii)
308 fn visit_impl_item(&mut self, ii: &'v ImplItem) {
309 walk_impl_item(self, ii)
311 fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) {
312 walk_impl_item_ref(self, ii)
314 fn visit_trait_ref(&mut self, t: &'v TraitRef) {
315 walk_trait_ref(self, t)
317 fn visit_param_bound(&mut self, bounds: &'v GenericBound) {
318 walk_param_bound(self, bounds)
320 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: TraitBoundModifier) {
321 walk_poly_trait_ref(self, t, m)
323 fn visit_variant_data(&mut self,
329 walk_struct_def(self, s)
331 fn visit_struct_field(&mut self, s: &'v StructField) {
332 walk_struct_field(self, s)
334 fn visit_enum_def(&mut self,
335 enum_definition: &'v EnumDef,
336 generics: &'v Generics,
339 walk_enum_def(self, enum_definition, generics, item_id)
341 fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics, item_id: NodeId) {
342 walk_variant(self, v, g, item_id)
344 fn visit_label(&mut self, label: &'v Label) {
345 walk_label(self, label)
347 fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg) {
349 GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
350 GenericArg::Type(ty) => self.visit_ty(ty),
353 fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
354 walk_lifetime(self, lifetime)
356 fn visit_qpath(&mut self, qpath: &'v QPath, id: NodeId, span: Span) {
357 walk_qpath(self, qpath, id, span)
359 fn visit_path(&mut self, path: &'v Path, _id: NodeId) {
360 walk_path(self, path)
362 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
363 walk_path_segment(self, path_span, path_segment)
365 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs) {
366 walk_generic_args(self, path_span, generic_args)
368 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
369 walk_assoc_type_binding(self, type_binding)
371 fn visit_attribute(&mut self, _attr: &'v Attribute) {
373 fn visit_macro_def(&mut self, macro_def: &'v MacroDef) {
374 walk_macro_def(self, macro_def)
376 fn visit_vis(&mut self, vis: &'v Visibility) {
379 fn visit_associated_item_kind(&mut self, kind: &'v AssociatedItemKind) {
380 walk_associated_item_kind(self, kind);
382 fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
383 walk_defaultness(self, defaultness);
387 /// Walks the contents of a crate. See also `Crate::visit_all_items`.
388 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
389 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
390 walk_list!(visitor, visit_attribute, &krate.attrs);
391 walk_list!(visitor, visit_macro_def, &krate.exported_macros);
394 pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef) {
395 visitor.visit_id(macro_def.id);
396 visitor.visit_name(macro_def.span, macro_def.name);
397 walk_list!(visitor, visit_attribute, ¯o_def.attrs);
400 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod, mod_node_id: NodeId) {
401 visitor.visit_id(mod_node_id);
402 for &item_id in &module.item_ids {
403 visitor.visit_nested_item(item_id);
407 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body) {
408 for argument in &body.arguments {
409 visitor.visit_id(argument.id);
410 visitor.visit_pat(&argument.pat);
412 visitor.visit_expr(&body.value);
415 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
416 // Intentionally visiting the expr first - the initialization expr
417 // dominates the local's definition.
418 walk_list!(visitor, visit_expr, &local.init);
419 walk_list!(visitor, visit_attribute, local.attrs.iter());
420 visitor.visit_id(local.id);
421 visitor.visit_pat(&local.pat);
422 walk_list!(visitor, visit_ty, &local.ty);
425 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
426 visitor.visit_name(ident.span, ident.name);
429 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
430 visitor.visit_ident(label.ident);
433 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
434 visitor.visit_id(lifetime.id);
435 match lifetime.name {
436 LifetimeName::Param(ParamName::Plain(ident)) => {
437 visitor.visit_ident(ident);
439 LifetimeName::Param(ParamName::Fresh(_)) |
440 LifetimeName::Static |
441 LifetimeName::Implicit |
442 LifetimeName::Underscore => {}
446 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
447 trait_ref: &'v PolyTraitRef,
448 _modifier: TraitBoundModifier)
451 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
452 visitor.visit_trait_ref(&trait_ref.trait_ref);
455 pub fn walk_trait_ref<'v, V>(visitor: &mut V, trait_ref: &'v TraitRef)
458 visitor.visit_id(trait_ref.ref_id);
459 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
462 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
463 visitor.visit_vis(&item.vis);
464 visitor.visit_name(item.span, item.name);
466 ItemKind::ExternCrate(orig_name) => {
467 visitor.visit_id(item.id);
468 if let Some(orig_name) = orig_name {
469 visitor.visit_name(item.span, orig_name);
472 ItemKind::Use(ref path, _) => {
473 visitor.visit_id(item.id);
474 visitor.visit_path(path, item.id);
476 ItemKind::Static(ref typ, _, body) |
477 ItemKind::Const(ref typ, body) => {
478 visitor.visit_id(item.id);
479 visitor.visit_ty(typ);
480 visitor.visit_nested_body(body);
482 ItemKind::Fn(ref declaration, header, ref generics, body_id) => {
483 visitor.visit_fn(FnKind::ItemFn(item.name,
493 ItemKind::Mod(ref module) => {
494 // visit_mod() takes care of visiting the Item's NodeId
495 visitor.visit_mod(module, item.span, item.id)
497 ItemKind::ForeignMod(ref foreign_module) => {
498 visitor.visit_id(item.id);
499 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
501 ItemKind::GlobalAsm(_) => {
502 visitor.visit_id(item.id);
504 ItemKind::Ty(ref typ, ref type_parameters) => {
505 visitor.visit_id(item.id);
506 visitor.visit_ty(typ);
507 visitor.visit_generics(type_parameters)
509 ItemKind::Existential(ExistTy {ref generics, ref bounds, impl_trait_fn}) => {
510 visitor.visit_id(item.id);
511 walk_generics(visitor, generics);
512 walk_list!(visitor, visit_param_bound, bounds);
513 if let Some(impl_trait_fn) = impl_trait_fn {
514 visitor.visit_def_mention(Def::Fn(impl_trait_fn))
517 ItemKind::Enum(ref enum_definition, ref type_parameters) => {
518 visitor.visit_generics(type_parameters);
519 // visit_enum_def() takes care of visiting the Item's NodeId
520 visitor.visit_enum_def(enum_definition, type_parameters, item.id, item.span)
525 ref opt_trait_reference,
529 visitor.visit_id(item.id);
530 visitor.visit_generics(type_parameters);
531 walk_list!(visitor, visit_trait_ref, opt_trait_reference);
532 visitor.visit_ty(typ);
533 walk_list!(visitor, visit_impl_item_ref, impl_item_refs);
535 ItemKind::Struct(ref struct_definition, ref generics) |
536 ItemKind::Union(ref struct_definition, ref generics) => {
537 visitor.visit_generics(generics);
538 visitor.visit_id(item.id);
539 visitor.visit_variant_data(struct_definition, item.name, generics, item.id, item.span);
541 ItemKind::Trait(.., ref generics, ref bounds, ref trait_item_refs) => {
542 visitor.visit_id(item.id);
543 visitor.visit_generics(generics);
544 walk_list!(visitor, visit_param_bound, bounds);
545 walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
547 ItemKind::TraitAlias(ref generics, ref bounds) => {
548 visitor.visit_id(item.id);
549 visitor.visit_generics(generics);
550 walk_list!(visitor, visit_param_bound, bounds);
553 walk_list!(visitor, visit_attribute, &item.attrs);
556 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
557 enum_definition: &'v EnumDef,
558 generics: &'v Generics,
560 visitor.visit_id(item_id);
563 &enum_definition.variants,
568 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
569 variant: &'v Variant,
570 generics: &'v Generics,
571 parent_item_id: NodeId) {
572 visitor.visit_name(variant.span, variant.node.name);
573 visitor.visit_variant_data(&variant.node.data,
578 walk_list!(visitor, visit_anon_const, &variant.node.disr_expr);
579 walk_list!(visitor, visit_attribute, &variant.node.attrs);
582 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
583 visitor.visit_id(typ.id);
586 TyKind::Slice(ref ty) => {
589 TyKind::Ptr(ref mutable_type) => {
590 visitor.visit_ty(&mutable_type.ty)
592 TyKind::Rptr(ref lifetime, ref mutable_type) => {
593 visitor.visit_lifetime(lifetime);
594 visitor.visit_ty(&mutable_type.ty)
597 TyKind::Tup(ref tuple_element_types) => {
598 walk_list!(visitor, visit_ty, tuple_element_types);
600 TyKind::BareFn(ref function_declaration) => {
601 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
602 visitor.visit_fn_decl(&function_declaration.decl);
604 TyKind::Path(ref qpath) => {
605 visitor.visit_qpath(qpath, typ.id, typ.span);
607 TyKind::Array(ref ty, ref length) => {
608 visitor.visit_ty(ty);
609 visitor.visit_anon_const(length)
611 TyKind::TraitObject(ref bounds, ref lifetime) => {
612 for bound in bounds {
613 visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
615 visitor.visit_lifetime(lifetime);
617 TyKind::Typeof(ref expression) => {
618 visitor.visit_anon_const(expression)
620 TyKind::Infer | TyKind::Err => {}
624 pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath, id: NodeId, span: Span) {
626 QPath::Resolved(ref maybe_qself, ref path) => {
627 if let Some(ref qself) = *maybe_qself {
628 visitor.visit_ty(qself);
630 visitor.visit_path(path, id)
632 QPath::TypeRelative(ref qself, ref segment) => {
633 visitor.visit_ty(qself);
634 visitor.visit_path_segment(span, segment);
639 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
640 visitor.visit_def_mention(path.def);
641 for segment in &path.segments {
642 visitor.visit_path_segment(path.span, segment);
646 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
648 segment: &'v PathSegment) {
649 visitor.visit_ident(segment.ident);
650 if let Some(ref args) = segment.args {
651 visitor.visit_generic_args(path_span, args);
655 pub fn walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V,
657 generic_args: &'v GenericArgs) {
658 walk_list!(visitor, visit_generic_arg, &generic_args.args);
659 walk_list!(visitor, visit_assoc_type_binding, &generic_args.bindings);
662 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
663 type_binding: &'v TypeBinding) {
664 visitor.visit_id(type_binding.id);
665 visitor.visit_ident(type_binding.ident);
666 visitor.visit_ty(&type_binding.ty);
669 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
670 visitor.visit_id(pattern.id);
672 PatKind::TupleStruct(ref qpath, ref children, _) => {
673 visitor.visit_qpath(qpath, pattern.id, pattern.span);
674 walk_list!(visitor, visit_pat, children);
676 PatKind::Path(ref qpath) => {
677 visitor.visit_qpath(qpath, pattern.id, pattern.span);
679 PatKind::Struct(ref qpath, ref fields, _) => {
680 visitor.visit_qpath(qpath, pattern.id, pattern.span);
681 for field in fields {
682 visitor.visit_id(field.node.id);
683 visitor.visit_ident(field.node.ident);
684 visitor.visit_pat(&field.node.pat)
687 PatKind::Tuple(ref tuple_elements, _) => {
688 walk_list!(visitor, visit_pat, tuple_elements);
690 PatKind::Box(ref subpattern) |
691 PatKind::Ref(ref subpattern, _) => {
692 visitor.visit_pat(subpattern)
694 PatKind::Binding(_, canonical_id, ident, ref optional_subpattern) => {
695 visitor.visit_def_mention(Def::Local(canonical_id));
696 visitor.visit_ident(ident);
697 walk_list!(visitor, visit_pat, optional_subpattern);
699 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
700 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
701 visitor.visit_expr(lower_bound);
702 visitor.visit_expr(upper_bound)
705 PatKind::Slice(ref prepatterns, ref slice_pattern, ref postpatterns) => {
706 walk_list!(visitor, visit_pat, prepatterns);
707 walk_list!(visitor, visit_pat, slice_pattern);
708 walk_list!(visitor, visit_pat, postpatterns);
713 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem) {
714 visitor.visit_id(foreign_item.id);
715 visitor.visit_vis(&foreign_item.vis);
716 visitor.visit_name(foreign_item.span, foreign_item.name);
718 match foreign_item.node {
719 ForeignItemKind::Fn(ref function_declaration, ref param_names, ref generics) => {
720 visitor.visit_generics(generics);
721 visitor.visit_fn_decl(function_declaration);
722 for ¶m_name in param_names {
723 visitor.visit_ident(param_name);
726 ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
727 ForeignItemKind::Type => (),
730 walk_list!(visitor, visit_attribute, &foreign_item.attrs);
733 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound) {
735 GenericBound::Trait(ref typ, modifier) => {
736 visitor.visit_poly_trait_ref(typ, modifier);
738 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
742 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam) {
743 visitor.visit_id(param.id);
744 walk_list!(visitor, visit_attribute, ¶m.attrs);
746 ParamName::Plain(ident) => visitor.visit_ident(ident),
747 ParamName::Fresh(_) => {}
750 GenericParamKind::Lifetime { .. } => {}
751 GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
753 walk_list!(visitor, visit_param_bound, ¶m.bounds);
756 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
757 walk_list!(visitor, visit_generic_param, &generics.params);
758 visitor.visit_id(generics.where_clause.id);
759 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
762 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
764 predicate: &'v WherePredicate)
767 &WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
769 ref bound_generic_params,
771 visitor.visit_ty(bounded_ty);
772 walk_list!(visitor, visit_param_bound, bounds);
773 walk_list!(visitor, visit_generic_param, bound_generic_params);
775 &WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
778 visitor.visit_lifetime(lifetime);
779 walk_list!(visitor, visit_param_bound, bounds);
781 &WherePredicate::EqPredicate(WhereEqPredicate{id,
785 visitor.visit_id(id);
786 visitor.visit_ty(lhs_ty);
787 visitor.visit_ty(rhs_ty);
792 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
793 if let Return(ref output_ty) = *ret_ty {
794 visitor.visit_ty(output_ty)
798 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
799 for ty in &function_declaration.inputs {
802 walk_fn_ret_ty(visitor, &function_declaration.output)
805 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
806 match function_kind {
807 FnKind::ItemFn(_, generics, ..) => {
808 visitor.visit_generics(generics);
811 FnKind::Closure(_) => {}
815 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
816 function_kind: FnKind<'v>,
817 function_declaration: &'v FnDecl,
821 visitor.visit_id(id);
822 visitor.visit_fn_decl(function_declaration);
823 walk_fn_kind(visitor, function_kind);
824 visitor.visit_nested_body(body_id)
827 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
828 visitor.visit_ident(trait_item.ident);
829 walk_list!(visitor, visit_attribute, &trait_item.attrs);
830 visitor.visit_generics(&trait_item.generics);
831 match trait_item.node {
832 TraitItemKind::Const(ref ty, default) => {
833 visitor.visit_id(trait_item.id);
834 visitor.visit_ty(ty);
835 walk_list!(visitor, visit_nested_body, default);
837 TraitItemKind::Method(ref sig, TraitMethod::Required(ref param_names)) => {
838 visitor.visit_id(trait_item.id);
839 visitor.visit_fn_decl(&sig.decl);
840 for ¶m_name in param_names {
841 visitor.visit_ident(param_name);
844 TraitItemKind::Method(ref sig, TraitMethod::Provided(body_id)) => {
845 visitor.visit_fn(FnKind::Method(trait_item.ident,
854 TraitItemKind::Type(ref bounds, ref default) => {
855 visitor.visit_id(trait_item.id);
856 walk_list!(visitor, visit_param_bound, bounds);
857 walk_list!(visitor, visit_ty, default);
862 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
863 // NB: Deliberately force a compilation error if/when new fields are added.
864 let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
865 visitor.visit_nested_trait_item(id);
866 visitor.visit_ident(ident);
867 visitor.visit_associated_item_kind(kind);
868 visitor.visit_defaultness(defaultness);
871 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
872 // NB: Deliberately force a compilation error if/when new fields are added.
885 visitor.visit_ident(ident);
886 visitor.visit_vis(vis);
887 visitor.visit_defaultness(defaultness);
888 walk_list!(visitor, visit_attribute, attrs);
889 visitor.visit_generics(generics);
891 ImplItemKind::Const(ref ty, body) => {
892 visitor.visit_id(impl_item.id);
893 visitor.visit_ty(ty);
894 visitor.visit_nested_body(body);
896 ImplItemKind::Method(ref sig, body_id) => {
897 visitor.visit_fn(FnKind::Method(impl_item.ident,
899 Some(&impl_item.vis),
906 ImplItemKind::Type(ref ty) => {
907 visitor.visit_id(impl_item.id);
908 visitor.visit_ty(ty);
910 ImplItemKind::Existential(ref bounds) => {
911 visitor.visit_id(impl_item.id);
912 walk_list!(visitor, visit_param_bound, bounds);
917 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
918 // NB: Deliberately force a compilation error if/when new fields are added.
919 let ImplItemRef { id, ident, ref kind, span: _, ref vis, ref defaultness } = *impl_item_ref;
920 visitor.visit_nested_impl_item(id);
921 visitor.visit_ident(ident);
922 visitor.visit_associated_item_kind(kind);
923 visitor.visit_vis(vis);
924 visitor.visit_defaultness(defaultness);
928 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &'v VariantData) {
929 visitor.visit_id(struct_definition.id());
930 walk_list!(visitor, visit_struct_field, struct_definition.fields());
933 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) {
934 visitor.visit_id(struct_field.id);
935 visitor.visit_vis(&struct_field.vis);
936 visitor.visit_ident(struct_field.ident);
937 visitor.visit_ty(&struct_field.ty);
938 walk_list!(visitor, visit_attribute, &struct_field.attrs);
941 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
942 visitor.visit_id(block.id);
943 walk_list!(visitor, visit_stmt, &block.stmts);
944 walk_list!(visitor, visit_expr, &block.expr);
947 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
948 match statement.node {
949 StmtKind::Decl(ref declaration, id) => {
950 visitor.visit_id(id);
951 visitor.visit_decl(declaration)
953 StmtKind::Expr(ref expression, id) |
954 StmtKind::Semi(ref expression, id) => {
955 visitor.visit_id(id);
956 visitor.visit_expr(expression)
961 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
962 match declaration.node {
963 DeclKind::Local(ref local) => visitor.visit_local(local),
964 DeclKind::Item(item) => visitor.visit_nested_item(item),
968 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
969 visitor.visit_id(constant.id);
970 visitor.visit_nested_body(constant.body);
973 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
974 visitor.visit_id(expression.id);
975 walk_list!(visitor, visit_attribute, expression.attrs.iter());
976 match expression.node {
977 ExprKind::Box(ref subexpression) => {
978 visitor.visit_expr(subexpression)
980 ExprKind::Array(ref subexpressions) => {
981 walk_list!(visitor, visit_expr, subexpressions);
983 ExprKind::Repeat(ref element, ref count) => {
984 visitor.visit_expr(element);
985 visitor.visit_anon_const(count)
987 ExprKind::Struct(ref qpath, ref fields, ref optional_base) => {
988 visitor.visit_qpath(qpath, expression.id, expression.span);
989 for field in fields {
990 visitor.visit_id(field.id);
991 visitor.visit_ident(field.ident);
992 visitor.visit_expr(&field.expr)
994 walk_list!(visitor, visit_expr, optional_base);
996 ExprKind::Tup(ref subexpressions) => {
997 walk_list!(visitor, visit_expr, subexpressions);
999 ExprKind::Call(ref callee_expression, ref arguments) => {
1000 visitor.visit_expr(callee_expression);
1001 walk_list!(visitor, visit_expr, arguments);
1003 ExprKind::MethodCall(ref segment, _, ref arguments) => {
1004 visitor.visit_path_segment(expression.span, segment);
1005 walk_list!(visitor, visit_expr, arguments);
1007 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
1008 visitor.visit_expr(left_expression);
1009 visitor.visit_expr(right_expression)
1011 ExprKind::AddrOf(_, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
1012 visitor.visit_expr(subexpression)
1014 ExprKind::Lit(_) => {}
1015 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
1016 visitor.visit_expr(subexpression);
1017 visitor.visit_ty(typ)
1019 ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
1020 visitor.visit_expr(head_expression);
1021 visitor.visit_expr(if_block);
1022 walk_list!(visitor, visit_expr, optional_else);
1024 ExprKind::While(ref subexpression, ref block, ref opt_label) => {
1025 walk_list!(visitor, visit_label, opt_label);
1026 visitor.visit_expr(subexpression);
1027 visitor.visit_block(block);
1029 ExprKind::Loop(ref block, ref opt_label, _) => {
1030 walk_list!(visitor, visit_label, opt_label);
1031 visitor.visit_block(block);
1033 ExprKind::Match(ref subexpression, ref arms, _) => {
1034 visitor.visit_expr(subexpression);
1035 walk_list!(visitor, visit_arm, arms);
1037 ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => {
1038 visitor.visit_fn(FnKind::Closure(&expression.attrs),
1039 function_declaration,
1044 ExprKind::Block(ref block, ref opt_label) => {
1045 walk_list!(visitor, visit_label, opt_label);
1046 visitor.visit_block(block);
1048 ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
1049 visitor.visit_expr(right_hand_expression);
1050 visitor.visit_expr(left_hand_expression)
1052 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
1053 visitor.visit_expr(right_expression);
1054 visitor.visit_expr(left_expression)
1056 ExprKind::Field(ref subexpression, ident) => {
1057 visitor.visit_expr(subexpression);
1058 visitor.visit_ident(ident);
1060 ExprKind::Index(ref main_expression, ref index_expression) => {
1061 visitor.visit_expr(main_expression);
1062 visitor.visit_expr(index_expression)
1064 ExprKind::Path(ref qpath) => {
1065 visitor.visit_qpath(qpath, expression.id, expression.span);
1067 ExprKind::Break(ref destination, ref opt_expr) => {
1068 if let Some(ref label) = destination.label {
1069 visitor.visit_label(label);
1070 match destination.target_id {
1071 Ok(node_id) => visitor.visit_def_mention(Def::Label(node_id)),
1075 walk_list!(visitor, visit_expr, opt_expr);
1077 ExprKind::Continue(ref destination) => {
1078 if let Some(ref label) = destination.label {
1079 visitor.visit_label(label);
1080 match destination.target_id {
1081 Ok(node_id) => visitor.visit_def_mention(Def::Label(node_id)),
1086 ExprKind::Ret(ref optional_expression) => {
1087 walk_list!(visitor, visit_expr, optional_expression);
1089 ExprKind::InlineAsm(_, ref outputs, ref inputs) => {
1090 for output in outputs {
1091 visitor.visit_expr(output)
1093 for input in inputs {
1094 visitor.visit_expr(input)
1097 ExprKind::Yield(ref subexpression) => {
1098 visitor.visit_expr(subexpression);
1103 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
1104 walk_list!(visitor, visit_pat, &arm.pats);
1105 walk_list!(visitor, visit_expr, &arm.guard);
1106 visitor.visit_expr(&arm.body);
1107 walk_list!(visitor, visit_attribute, &arm.attrs);
1110 pub fn walk_vis<'v, V: Visitor<'v>>(visitor: &mut V, vis: &'v Visibility) {
1111 if let VisibilityKind::Restricted { ref path, id } = vis.node {
1112 visitor.visit_id(id);
1113 visitor.visit_path(path, id)
1117 pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssociatedItemKind) {
1118 // No visitable content here: this fn exists so you can call it if
1119 // the right thing to do, should content be added in the future,
1120 // would be to walk it.
1123 pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) {
1124 // No visitable content here: this fn exists so you can call it if
1125 // the right thing to do, should content be added in the future,
1126 // would be to walk it.
1129 #[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug)]
1130 pub struct IdRange {
1136 pub fn max() -> IdRange {
1138 min: NodeId::from_u32(u32::MAX),
1139 max: NodeId::from_u32(u32::MIN),
1143 pub fn empty(&self) -> bool {
1144 self.min >= self.max
1147 pub fn contains(&self, id: NodeId) -> bool {
1148 id >= self.min && id < self.max
1151 pub fn add(&mut self, id: NodeId) {
1152 self.min = cmp::min(self.min, id);
1153 self.max = cmp::max(self.max, NodeId::from_u32(id.as_u32() + 1));
1159 pub struct IdRangeComputingVisitor<'a, 'hir: 'a> {
1161 map: &'a map::Map<'hir>,
1164 impl<'a, 'hir> IdRangeComputingVisitor<'a, 'hir> {
1165 pub fn new(map: &'a map::Map<'hir>) -> IdRangeComputingVisitor<'a, 'hir> {
1166 IdRangeComputingVisitor { result: IdRange::max(), map: map }
1169 pub fn result(&self) -> IdRange {
1174 impl<'a, 'hir> Visitor<'hir> for IdRangeComputingVisitor<'a, 'hir> {
1175 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
1176 NestedVisitorMap::OnlyBodies(&self.map)
1179 fn visit_id(&mut self, id: NodeId) {
1180 self.result.add(id);