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;
54 #[derive(Copy, Clone, PartialEq, Eq)]
56 /// #[xxx] pub async/const/extern "Abi" fn foo()
57 ItemFn(Name, &'a Generics, FnHeader, &'a Visibility, &'a [Attribute]),
60 Method(Ident, &'a MethodSig, Option<&'a Visibility>, &'a [Attribute]),
63 Closure(&'a [Attribute]),
67 pub fn attrs(&self) -> &'a [Attribute] {
69 FnKind::ItemFn(.., attrs) => attrs,
70 FnKind::Method(.., attrs) => attrs,
71 FnKind::Closure(attrs) => attrs,
76 /// Specifies what nested things a visitor wants to visit. The most
77 /// common choice is `OnlyBodies`, which will cause the visitor to
78 /// visit fn bodies for fns that it encounters, but skip over nested
81 /// See the comments on `ItemLikeVisitor` for more details on the overall
83 pub enum NestedVisitorMap<'this, 'tcx: 'this> {
84 /// Do not visit any nested things. When you add a new
85 /// "non-nested" thing, you will want to audit such uses to see if
86 /// they remain valid.
88 /// Use this if you are only walking some particular kind of tree
89 /// (i.e., a type, or fn signature) and you don't want to thread a
93 /// Do not visit nested item-like things, but visit nested things
94 /// that are inside of an item-like.
96 /// **This is the most common choice.** A very common pattern is
97 /// to use `visit_all_item_likes()` as an outer loop,
98 /// and to have the visitor that visits the contents of each item
99 /// using this setting.
100 OnlyBodies(&'this Map<'tcx>),
102 /// Visit all nested things, including item-likes.
104 /// **This is an unusual choice.** It is used when you want to
105 /// process everything within their lexical context. Typically you
106 /// kick off the visit by doing `walk_krate()`.
107 All(&'this Map<'tcx>),
110 impl<'this, 'tcx> NestedVisitorMap<'this, 'tcx> {
111 /// Returns the map to use for an "intra item-like" thing (if any).
112 /// e.g., function body.
113 pub fn intra(self) -> Option<&'this Map<'tcx>> {
115 NestedVisitorMap::None => None,
116 NestedVisitorMap::OnlyBodies(map) => Some(map),
117 NestedVisitorMap::All(map) => Some(map),
121 /// Returns the map to use for an "item-like" thing (if any).
122 /// e.g., item, impl-item.
123 pub fn inter(self) -> Option<&'this Map<'tcx>> {
125 NestedVisitorMap::None => None,
126 NestedVisitorMap::OnlyBodies(_) => None,
127 NestedVisitorMap::All(map) => Some(map),
132 /// Each method of the Visitor trait is a hook to be potentially
133 /// overridden. Each method's default implementation recursively visits
134 /// the substructure of the input via the corresponding `walk` method;
135 /// e.g. the `visit_mod` method by default calls `intravisit::walk_mod`.
137 /// Note that this visitor does NOT visit nested items by default
138 /// (this is why the module is called `intravisit`, to distinguish it
139 /// from the AST's `visit` module, which acts differently). If you
140 /// simply want to visit all items in the crate in some order, you
141 /// should call `Crate::visit_all_items`. Otherwise, see the comment
142 /// on `visit_nested_item` for details on how to visit nested items.
144 /// If you want to ensure that your code handles every variant
145 /// explicitly, you need to override each method. (And you also need
146 /// to monitor future changes to `Visitor` in case a new method with a
147 /// new default implementation gets introduced.)
148 pub trait Visitor<'v> : Sized {
149 ///////////////////////////////////////////////////////////////////////////
152 /// The default versions of the `visit_nested_XXX` routines invoke
153 /// this method to get a map to use. By selecting an enum variant,
154 /// you control which kinds of nested HIR are visited; see
155 /// `NestedVisitorMap` for details. By "nested HIR", we are
156 /// referring to bits of HIR that are not directly embedded within
157 /// one another but rather indirectly, through a table in the
158 /// crate. This is done to control dependencies during incremental
159 /// compilation: the non-inline bits of HIR can be tracked and
160 /// hashed separately.
162 /// **If for some reason you want the nested behavior, but don't
163 /// have a `Map` at your disposal:** then you should override the
164 /// `visit_nested_XXX` methods, and override this method to
165 /// `panic!()`. This way, if a new `visit_nested_XXX` variant is
166 /// added in the future, we will see the panic in your code and
167 /// fix it appropriately.
168 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v>;
170 /// Invoked when a nested item is encountered. By default does
171 /// nothing unless you override `nested_visit_map` to return
172 /// `Some(_)`, in which case it will walk the item. **You probably
173 /// don't want to override this method** -- instead, override
174 /// `nested_visit_map` or use the "shallow" or "deep" visit
175 /// patterns described on `itemlikevisit::ItemLikeVisitor`. The only
176 /// reason to override this method is if you want a nested pattern
177 /// but cannot supply a `Map`; see `nested_visit_map` for advice.
178 #[allow(unused_variables)]
179 fn visit_nested_item(&mut self, id: ItemId) {
180 let opt_item = self.nested_visit_map().inter().map(|map| map.expect_item(id.id));
181 if let Some(item) = opt_item {
182 self.visit_item(item);
186 /// Like `visit_nested_item()`, but for trait items. See
187 /// `visit_nested_item()` for advice on when to override this
189 #[allow(unused_variables)]
190 fn visit_nested_trait_item(&mut self, id: TraitItemId) {
191 let opt_item = self.nested_visit_map().inter().map(|map| map.trait_item(id));
192 if let Some(item) = opt_item {
193 self.visit_trait_item(item);
197 /// Like `visit_nested_item()`, but for impl items. See
198 /// `visit_nested_item()` for advice on when to override this
200 #[allow(unused_variables)]
201 fn visit_nested_impl_item(&mut self, id: ImplItemId) {
202 let opt_item = self.nested_visit_map().inter().map(|map| map.impl_item(id));
203 if let Some(item) = opt_item {
204 self.visit_impl_item(item);
208 /// Invoked to visit the body of a function, method or closure. Like
209 /// visit_nested_item, does nothing by default unless you override
210 /// `nested_visit_map` to return `Some(_)`, in which case it will walk the
212 fn visit_nested_body(&mut self, id: BodyId) {
213 let opt_body = self.nested_visit_map().intra().map(|map| map.body(id));
214 if let Some(body) = opt_body {
215 self.visit_body(body);
219 /// Visit the top-level item and (optionally) nested items / impl items. See
220 /// `visit_nested_item` for details.
221 fn visit_item(&mut self, i: &'v Item) {
225 fn visit_body(&mut self, b: &'v Body) {
229 /// When invoking `visit_all_item_likes()`, you need to supply an
230 /// item-like visitor. This method converts a "intra-visit"
231 /// visitor into an item-like visitor that walks the entire tree.
232 /// If you use this, you probably don't want to process the
233 /// contents of nested item-like things, since the outer loop will
234 /// visit them as well.
235 fn as_deep_visitor<'s>(&'s mut self) -> DeepVisitor<'s, Self> {
236 DeepVisitor::new(self)
239 ///////////////////////////////////////////////////////////////////////////
241 fn visit_id(&mut self, _node_id: NodeId) {
244 fn visit_def_mention(&mut self, _def: Def) {
247 fn visit_name(&mut self, _span: Span, _name: Name) {
250 fn visit_ident(&mut self, ident: Ident) {
251 walk_ident(self, ident)
253 fn visit_mod(&mut self, m: &'v Mod, _s: Span, n: NodeId) {
256 fn visit_foreign_item(&mut self, i: &'v ForeignItem) {
257 walk_foreign_item(self, i)
259 fn visit_local(&mut self, l: &'v Local) {
262 fn visit_block(&mut self, b: &'v Block) {
265 fn visit_stmt(&mut self, s: &'v Stmt) {
268 fn visit_arm(&mut self, a: &'v Arm) {
271 fn visit_pat(&mut self, p: &'v Pat) {
274 fn visit_decl(&mut self, d: &'v Decl) {
277 fn visit_anon_const(&mut self, c: &'v AnonConst) {
278 walk_anon_const(self, c)
280 fn visit_expr(&mut self, ex: &'v Expr) {
283 fn visit_ty(&mut self, t: &'v Ty) {
286 fn visit_generic_param(&mut self, p: &'v GenericParam) {
287 walk_generic_param(self, p)
289 fn visit_generics(&mut self, g: &'v Generics) {
290 walk_generics(self, g)
292 fn visit_where_predicate(&mut self, predicate: &'v WherePredicate) {
293 walk_where_predicate(self, predicate)
295 fn visit_fn_decl(&mut self, fd: &'v FnDecl) {
296 walk_fn_decl(self, fd)
298 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: BodyId, s: Span, id: NodeId) {
299 walk_fn(self, fk, fd, b, s, id)
301 fn visit_trait_item(&mut self, ti: &'v TraitItem) {
302 walk_trait_item(self, ti)
304 fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
305 walk_trait_item_ref(self, ii)
307 fn visit_impl_item(&mut self, ii: &'v ImplItem) {
308 walk_impl_item(self, ii)
310 fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) {
311 walk_impl_item_ref(self, ii)
313 fn visit_trait_ref(&mut self, t: &'v TraitRef) {
314 walk_trait_ref(self, t)
316 fn visit_param_bound(&mut self, bounds: &'v GenericBound) {
317 walk_param_bound(self, bounds)
319 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: TraitBoundModifier) {
320 walk_poly_trait_ref(self, t, m)
322 fn visit_variant_data(&mut self,
328 walk_struct_def(self, s)
330 fn visit_struct_field(&mut self, s: &'v StructField) {
331 walk_struct_field(self, s)
333 fn visit_enum_def(&mut self,
334 enum_definition: &'v EnumDef,
335 generics: &'v Generics,
338 walk_enum_def(self, enum_definition, generics, item_id)
340 fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics, item_id: NodeId) {
341 walk_variant(self, v, g, item_id)
343 fn visit_label(&mut self, label: &'v Label) {
344 walk_label(self, label)
346 fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg) {
348 GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
349 GenericArg::Type(ty) => self.visit_ty(ty),
352 fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
353 walk_lifetime(self, lifetime)
355 fn visit_qpath(&mut self, qpath: &'v QPath, id: NodeId, span: Span) {
356 walk_qpath(self, qpath, id, span)
358 fn visit_path(&mut self, path: &'v Path, _id: NodeId) {
359 walk_path(self, path)
361 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
362 walk_path_segment(self, path_span, path_segment)
364 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs) {
365 walk_generic_args(self, path_span, generic_args)
367 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
368 walk_assoc_type_binding(self, type_binding)
370 fn visit_attribute(&mut self, _attr: &'v Attribute) {
372 fn visit_macro_def(&mut self, macro_def: &'v MacroDef) {
373 walk_macro_def(self, macro_def)
375 fn visit_vis(&mut self, vis: &'v Visibility) {
378 fn visit_associated_item_kind(&mut self, kind: &'v AssociatedItemKind) {
379 walk_associated_item_kind(self, kind);
381 fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
382 walk_defaultness(self, defaultness);
386 /// Walks the contents of a crate. See also `Crate::visit_all_items`.
387 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
388 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
389 walk_list!(visitor, visit_attribute, &krate.attrs);
390 walk_list!(visitor, visit_macro_def, &krate.exported_macros);
393 pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef) {
394 visitor.visit_id(macro_def.id);
395 visitor.visit_name(macro_def.span, macro_def.name);
396 walk_list!(visitor, visit_attribute, ¯o_def.attrs);
399 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod, mod_node_id: NodeId) {
400 visitor.visit_id(mod_node_id);
401 for &item_id in &module.item_ids {
402 visitor.visit_nested_item(item_id);
406 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body) {
407 for argument in &body.arguments {
408 visitor.visit_id(argument.id);
409 visitor.visit_pat(&argument.pat);
411 visitor.visit_expr(&body.value);
414 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
415 // Intentionally visiting the expr first - the initialization expr
416 // dominates the local's definition.
417 walk_list!(visitor, visit_expr, &local.init);
418 walk_list!(visitor, visit_attribute, local.attrs.iter());
419 visitor.visit_id(local.id);
420 visitor.visit_pat(&local.pat);
421 walk_list!(visitor, visit_ty, &local.ty);
424 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
425 visitor.visit_name(ident.span, ident.name);
428 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
429 visitor.visit_ident(label.ident);
432 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
433 visitor.visit_id(lifetime.id);
434 match lifetime.name {
435 LifetimeName::Param(ParamName::Plain(ident)) => {
436 visitor.visit_ident(ident);
438 LifetimeName::Param(ParamName::Fresh(_)) |
439 LifetimeName::Static |
440 LifetimeName::Implicit |
441 LifetimeName::Underscore => {}
445 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
446 trait_ref: &'v PolyTraitRef,
447 _modifier: TraitBoundModifier)
450 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
451 visitor.visit_trait_ref(&trait_ref.trait_ref);
454 pub fn walk_trait_ref<'v, V>(visitor: &mut V, trait_ref: &'v TraitRef)
457 visitor.visit_id(trait_ref.ref_id);
458 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
461 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
462 visitor.visit_vis(&item.vis);
463 visitor.visit_name(item.span, item.name);
465 ItemExternCrate(orig_name) => {
466 visitor.visit_id(item.id);
467 if let Some(orig_name) = orig_name {
468 visitor.visit_name(item.span, orig_name);
471 ItemUse(ref path, _) => {
472 visitor.visit_id(item.id);
473 visitor.visit_path(path, item.id);
475 ItemStatic(ref typ, _, body) |
476 ItemConst(ref typ, body) => {
477 visitor.visit_id(item.id);
478 visitor.visit_ty(typ);
479 visitor.visit_nested_body(body);
481 ItemFn(ref declaration, header, ref generics, body_id) => {
482 visitor.visit_fn(FnKind::ItemFn(item.name,
492 ItemMod(ref module) => {
493 // visit_mod() takes care of visiting the Item's NodeId
494 visitor.visit_mod(module, item.span, item.id)
496 ItemForeignMod(ref foreign_module) => {
497 visitor.visit_id(item.id);
498 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
500 ItemGlobalAsm(_) => {
501 visitor.visit_id(item.id);
503 ItemTy(ref typ, ref type_parameters) => {
504 visitor.visit_id(item.id);
505 visitor.visit_ty(typ);
506 visitor.visit_generics(type_parameters)
508 ItemExistential(ExistTy {ref generics, ref bounds, impl_trait_fn}) => {
509 visitor.visit_id(item.id);
510 walk_generics(visitor, generics);
511 walk_list!(visitor, visit_param_bound, bounds);
512 if let Some(impl_trait_fn) = impl_trait_fn {
513 visitor.visit_def_mention(Def::Fn(impl_trait_fn))
516 ItemEnum(ref enum_definition, ref type_parameters) => {
517 visitor.visit_generics(type_parameters);
518 // visit_enum_def() takes care of visiting the Item's NodeId
519 visitor.visit_enum_def(enum_definition, type_parameters, item.id, item.span)
521 ItemImpl(.., ref type_parameters, ref opt_trait_reference, ref typ, ref impl_item_refs) => {
522 visitor.visit_id(item.id);
523 visitor.visit_generics(type_parameters);
524 walk_list!(visitor, visit_trait_ref, opt_trait_reference);
525 visitor.visit_ty(typ);
526 walk_list!(visitor, visit_impl_item_ref, impl_item_refs);
528 ItemStruct(ref struct_definition, ref generics) |
529 ItemUnion(ref struct_definition, ref generics) => {
530 visitor.visit_generics(generics);
531 visitor.visit_id(item.id);
532 visitor.visit_variant_data(struct_definition, item.name, generics, item.id, item.span);
534 ItemTrait(.., ref generics, ref bounds, ref trait_item_refs) => {
535 visitor.visit_id(item.id);
536 visitor.visit_generics(generics);
537 walk_list!(visitor, visit_param_bound, bounds);
538 walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
540 ItemTraitAlias(ref generics, ref bounds) => {
541 visitor.visit_id(item.id);
542 visitor.visit_generics(generics);
543 walk_list!(visitor, visit_param_bound, bounds);
546 walk_list!(visitor, visit_attribute, &item.attrs);
549 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
550 enum_definition: &'v EnumDef,
551 generics: &'v Generics,
553 visitor.visit_id(item_id);
556 &enum_definition.variants,
561 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
562 variant: &'v Variant,
563 generics: &'v Generics,
564 parent_item_id: NodeId) {
565 visitor.visit_name(variant.span, variant.node.name);
566 visitor.visit_variant_data(&variant.node.data,
571 walk_list!(visitor, visit_anon_const, &variant.node.disr_expr);
572 walk_list!(visitor, visit_attribute, &variant.node.attrs);
575 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
576 visitor.visit_id(typ.id);
582 TyPtr(ref mutable_type) => {
583 visitor.visit_ty(&mutable_type.ty)
585 TyRptr(ref lifetime, ref mutable_type) => {
586 visitor.visit_lifetime(lifetime);
587 visitor.visit_ty(&mutable_type.ty)
590 TyTup(ref tuple_element_types) => {
591 walk_list!(visitor, visit_ty, tuple_element_types);
593 TyBareFn(ref function_declaration) => {
594 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
595 visitor.visit_fn_decl(&function_declaration.decl);
597 TyPath(ref qpath) => {
598 visitor.visit_qpath(qpath, typ.id, typ.span);
600 TyArray(ref ty, ref length) => {
601 visitor.visit_ty(ty);
602 visitor.visit_anon_const(length)
604 TyTraitObject(ref bounds, ref lifetime) => {
605 for bound in bounds {
606 visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
608 visitor.visit_lifetime(lifetime);
610 TyImplTraitExistential(_, def_id, ref lifetimes) => {
611 // we are not recursing into the `existential` item, because it is already being visited
612 // as part of the surrounding module. The `NodeId` just exists so we don't have to look
613 // it up everywhere else in the compiler
614 visitor.visit_def_mention(Def::Existential(def_id));
615 walk_list!(visitor, visit_lifetime, lifetimes);
617 TyTypeof(ref expression) => {
618 visitor.visit_anon_const(expression)
620 TyInfer | TyErr => {}
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 ForeignItemFn(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 ForeignItemStatic(ref typ, _) => visitor.visit_ty(typ),
727 ForeignItemType => (),
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);
913 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
914 // NB: Deliberately force a compilation error if/when new fields are added.
915 let ImplItemRef { id, ident, ref kind, span: _, ref vis, ref defaultness } = *impl_item_ref;
916 visitor.visit_nested_impl_item(id);
917 visitor.visit_ident(ident);
918 visitor.visit_associated_item_kind(kind);
919 visitor.visit_vis(vis);
920 visitor.visit_defaultness(defaultness);
924 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &'v VariantData) {
925 visitor.visit_id(struct_definition.id());
926 walk_list!(visitor, visit_struct_field, struct_definition.fields());
929 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) {
930 visitor.visit_id(struct_field.id);
931 visitor.visit_vis(&struct_field.vis);
932 visitor.visit_ident(struct_field.ident);
933 visitor.visit_ty(&struct_field.ty);
934 walk_list!(visitor, visit_attribute, &struct_field.attrs);
937 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
938 visitor.visit_id(block.id);
939 walk_list!(visitor, visit_stmt, &block.stmts);
940 walk_list!(visitor, visit_expr, &block.expr);
943 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
944 match statement.node {
945 StmtDecl(ref declaration, id) => {
946 visitor.visit_id(id);
947 visitor.visit_decl(declaration)
949 StmtExpr(ref expression, id) |
950 StmtSemi(ref expression, id) => {
951 visitor.visit_id(id);
952 visitor.visit_expr(expression)
957 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
958 match declaration.node {
959 DeclLocal(ref local) => visitor.visit_local(local),
960 DeclItem(item) => visitor.visit_nested_item(item),
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);
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 ExprBox(ref subexpression) => {
974 visitor.visit_expr(subexpression)
976 ExprArray(ref subexpressions) => {
977 walk_list!(visitor, visit_expr, subexpressions);
979 ExprRepeat(ref element, ref count) => {
980 visitor.visit_expr(element);
981 visitor.visit_anon_const(count)
983 ExprStruct(ref qpath, ref fields, ref optional_base) => {
984 visitor.visit_qpath(qpath, expression.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)
990 walk_list!(visitor, visit_expr, optional_base);
992 ExprTup(ref subexpressions) => {
993 walk_list!(visitor, visit_expr, subexpressions);
995 ExprCall(ref callee_expression, ref arguments) => {
996 visitor.visit_expr(callee_expression);
997 walk_list!(visitor, visit_expr, arguments);
999 ExprMethodCall(ref segment, _, ref arguments) => {
1000 visitor.visit_path_segment(expression.span, segment);
1001 walk_list!(visitor, visit_expr, arguments);
1003 ExprBinary(_, ref left_expression, ref right_expression) => {
1004 visitor.visit_expr(left_expression);
1005 visitor.visit_expr(right_expression)
1007 ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
1008 visitor.visit_expr(subexpression)
1011 ExprCast(ref subexpression, ref typ) | ExprType(ref subexpression, ref typ) => {
1012 visitor.visit_expr(subexpression);
1013 visitor.visit_ty(typ)
1015 ExprIf(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);
1020 ExprWhile(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);
1025 ExprLoop(ref block, ref opt_label, _) => {
1026 walk_list!(visitor, visit_label, opt_label);
1027 visitor.visit_block(block);
1029 ExprMatch(ref subexpression, ref arms, _) => {
1030 visitor.visit_expr(subexpression);
1031 walk_list!(visitor, visit_arm, arms);
1033 ExprClosure(_, ref function_declaration, body, _fn_decl_span, _gen) => {
1034 visitor.visit_fn(FnKind::Closure(&expression.attrs),
1035 function_declaration,
1040 ExprBlock(ref block, ref opt_label) => {
1041 walk_list!(visitor, visit_label, opt_label);
1042 visitor.visit_block(block);
1044 ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
1045 visitor.visit_expr(right_hand_expression);
1046 visitor.visit_expr(left_hand_expression)
1048 ExprAssignOp(_, ref left_expression, ref right_expression) => {
1049 visitor.visit_expr(right_expression);
1050 visitor.visit_expr(left_expression)
1052 ExprField(ref subexpression, ident) => {
1053 visitor.visit_expr(subexpression);
1054 visitor.visit_ident(ident);
1056 ExprIndex(ref main_expression, ref index_expression) => {
1057 visitor.visit_expr(main_expression);
1058 visitor.visit_expr(index_expression)
1060 ExprPath(ref qpath) => {
1061 visitor.visit_qpath(qpath, expression.id, expression.span);
1063 ExprBreak(ref destination, ref opt_expr) => {
1064 if let Some(ref label) = destination.label {
1065 visitor.visit_label(label);
1066 match destination.target_id {
1067 Ok(node_id) => visitor.visit_def_mention(Def::Label(node_id)),
1071 walk_list!(visitor, visit_expr, opt_expr);
1073 ExprContinue(ref destination) => {
1074 if let Some(ref label) = destination.label {
1075 visitor.visit_label(label);
1076 match destination.target_id {
1077 Ok(node_id) => visitor.visit_def_mention(Def::Label(node_id)),
1082 ExprRet(ref optional_expression) => {
1083 walk_list!(visitor, visit_expr, optional_expression);
1085 ExprInlineAsm(_, ref outputs, ref inputs) => {
1086 for output in outputs {
1087 visitor.visit_expr(output)
1089 for input in inputs {
1090 visitor.visit_expr(input)
1093 ExprYield(ref subexpression) => {
1094 visitor.visit_expr(subexpression);
1099 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
1100 walk_list!(visitor, visit_pat, &arm.pats);
1101 walk_list!(visitor, visit_expr, &arm.guard);
1102 visitor.visit_expr(&arm.body);
1103 walk_list!(visitor, visit_attribute, &arm.attrs);
1106 pub fn walk_vis<'v, V: Visitor<'v>>(visitor: &mut V, vis: &'v Visibility) {
1107 if let VisibilityKind::Restricted { ref path, id } = vis.node {
1108 visitor.visit_id(id);
1109 visitor.visit_path(path, id)
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.
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.
1125 #[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug, PartialEq, Eq)]
1126 pub struct IdRange {
1132 pub fn max() -> IdRange {
1134 min: NodeId::from_u32(u32::MAX),
1135 max: NodeId::from_u32(u32::MIN),
1139 pub fn empty(&self) -> bool {
1140 self.min >= self.max
1143 pub fn contains(&self, id: NodeId) -> bool {
1144 id >= self.min && id < self.max
1147 pub fn add(&mut self, id: NodeId) {
1148 self.min = cmp::min(self.min, id);
1149 self.max = cmp::max(self.max, NodeId::from_u32(id.as_u32() + 1));
1155 pub struct IdRangeComputingVisitor<'a, 'hir: 'a> {
1157 map: &'a map::Map<'hir>,
1160 impl<'a, 'hir> IdRangeComputingVisitor<'a, 'hir> {
1161 pub fn new(map: &'a map::Map<'hir>) -> IdRangeComputingVisitor<'a, 'hir> {
1162 IdRangeComputingVisitor { result: IdRange::max(), map: map }
1165 pub fn result(&self) -> IdRange {
1170 impl<'a, 'hir> Visitor<'hir> for IdRangeComputingVisitor<'a, 'hir> {
1171 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
1172 NestedVisitorMap::OnlyBodies(&self.map)
1175 fn visit_id(&mut self, id: NodeId) {
1176 self.result.add(id);