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)]
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_use(&mut self, path: &'v Path, id: NodeId, hir_id: HirId) {
302 walk_use(self, path, id, hir_id)
304 fn visit_trait_item(&mut self, ti: &'v TraitItem) {
305 walk_trait_item(self, ti)
307 fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
308 walk_trait_item_ref(self, ii)
310 fn visit_impl_item(&mut self, ii: &'v ImplItem) {
311 walk_impl_item(self, ii)
313 fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) {
314 walk_impl_item_ref(self, ii)
316 fn visit_trait_ref(&mut self, t: &'v TraitRef) {
317 walk_trait_ref(self, t)
319 fn visit_param_bound(&mut self, bounds: &'v GenericBound) {
320 walk_param_bound(self, bounds)
322 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: TraitBoundModifier) {
323 walk_poly_trait_ref(self, t, m)
325 fn visit_variant_data(&mut self,
331 walk_struct_def(self, s)
333 fn visit_struct_field(&mut self, s: &'v StructField) {
334 walk_struct_field(self, s)
336 fn visit_enum_def(&mut self,
337 enum_definition: &'v EnumDef,
338 generics: &'v Generics,
341 walk_enum_def(self, enum_definition, generics, item_id)
343 fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics, item_id: NodeId) {
344 walk_variant(self, v, g, item_id)
346 fn visit_label(&mut self, label: &'v Label) {
347 walk_label(self, label)
349 fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg) {
351 GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
352 GenericArg::Type(ty) => self.visit_ty(ty),
355 fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
356 walk_lifetime(self, lifetime)
358 fn visit_qpath(&mut self, qpath: &'v QPath, id: HirId, span: Span) {
359 walk_qpath(self, qpath, id, span)
361 fn visit_path(&mut self, path: &'v Path, _id: HirId) {
362 walk_path(self, path)
364 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
365 walk_path_segment(self, path_span, path_segment)
367 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs) {
368 walk_generic_args(self, path_span, generic_args)
370 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
371 walk_assoc_type_binding(self, type_binding)
373 fn visit_attribute(&mut self, _attr: &'v Attribute) {
375 fn visit_macro_def(&mut self, macro_def: &'v MacroDef) {
376 walk_macro_def(self, macro_def)
378 fn visit_vis(&mut self, vis: &'v Visibility) {
381 fn visit_associated_item_kind(&mut self, kind: &'v AssociatedItemKind) {
382 walk_associated_item_kind(self, kind);
384 fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
385 walk_defaultness(self, defaultness);
389 /// Walks the contents of a crate. See also `Crate::visit_all_items`.
390 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
391 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
392 walk_list!(visitor, visit_attribute, &krate.attrs);
393 walk_list!(visitor, visit_macro_def, &krate.exported_macros);
396 pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef) {
397 visitor.visit_id(macro_def.id);
398 visitor.visit_name(macro_def.span, macro_def.name);
399 walk_list!(visitor, visit_attribute, ¯o_def.attrs);
402 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod, mod_node_id: NodeId) {
403 visitor.visit_id(mod_node_id);
404 for &item_id in &module.item_ids {
405 visitor.visit_nested_item(item_id);
409 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body) {
410 for argument in &body.arguments {
411 visitor.visit_id(argument.id);
412 visitor.visit_pat(&argument.pat);
414 visitor.visit_expr(&body.value);
417 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
418 // Intentionally visiting the expr first - the initialization expr
419 // dominates the local's definition.
420 walk_list!(visitor, visit_expr, &local.init);
421 walk_list!(visitor, visit_attribute, local.attrs.iter());
422 visitor.visit_id(local.id);
423 visitor.visit_pat(&local.pat);
424 walk_list!(visitor, visit_ty, &local.ty);
427 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
428 visitor.visit_name(ident.span, ident.name);
431 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
432 visitor.visit_ident(label.ident);
435 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
436 visitor.visit_id(lifetime.id);
437 match lifetime.name {
438 LifetimeName::Param(ParamName::Plain(ident)) => {
439 visitor.visit_ident(ident);
441 LifetimeName::Param(ParamName::Fresh(_)) |
442 LifetimeName::Param(ParamName::Error) |
443 LifetimeName::Static |
444 LifetimeName::Error |
445 LifetimeName::Implicit |
446 LifetimeName::Underscore => {}
450 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
451 trait_ref: &'v PolyTraitRef,
452 _modifier: TraitBoundModifier)
455 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
456 visitor.visit_trait_ref(&trait_ref.trait_ref);
459 pub fn walk_trait_ref<'v, V>(visitor: &mut V, trait_ref: &'v TraitRef)
462 visitor.visit_id(trait_ref.ref_id);
463 visitor.visit_path(&trait_ref.path, trait_ref.hir_ref_id)
466 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
467 visitor.visit_vis(&item.vis);
468 visitor.visit_name(item.span, item.name);
470 ItemKind::ExternCrate(orig_name) => {
471 visitor.visit_id(item.id);
472 if let Some(orig_name) = orig_name {
473 visitor.visit_name(item.span, orig_name);
476 ItemKind::Use(ref path, _) => {
477 visitor.visit_use(path, item.id, item.hir_id);
479 ItemKind::Static(ref typ, _, body) |
480 ItemKind::Const(ref typ, body) => {
481 visitor.visit_id(item.id);
482 visitor.visit_ty(typ);
483 visitor.visit_nested_body(body);
485 ItemKind::Fn(ref declaration, header, ref generics, body_id) => {
486 visitor.visit_fn(FnKind::ItemFn(item.name,
496 ItemKind::Mod(ref module) => {
497 // visit_mod() takes care of visiting the Item's NodeId
498 visitor.visit_mod(module, item.span, item.id)
500 ItemKind::ForeignMod(ref foreign_module) => {
501 visitor.visit_id(item.id);
502 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
504 ItemKind::GlobalAsm(_) => {
505 visitor.visit_id(item.id);
507 ItemKind::Ty(ref typ, ref type_parameters) => {
508 visitor.visit_id(item.id);
509 visitor.visit_ty(typ);
510 visitor.visit_generics(type_parameters)
512 ItemKind::Existential(ExistTy {ref generics, ref bounds, impl_trait_fn}) => {
513 visitor.visit_id(item.id);
514 walk_generics(visitor, generics);
515 walk_list!(visitor, visit_param_bound, bounds);
516 if let Some(impl_trait_fn) = impl_trait_fn {
517 visitor.visit_def_mention(Def::Fn(impl_trait_fn))
520 ItemKind::Enum(ref enum_definition, ref type_parameters) => {
521 visitor.visit_generics(type_parameters);
522 // visit_enum_def() takes care of visiting the Item's NodeId
523 visitor.visit_enum_def(enum_definition, type_parameters, item.id, item.span)
528 ref opt_trait_reference,
532 visitor.visit_id(item.id);
533 visitor.visit_generics(type_parameters);
534 walk_list!(visitor, visit_trait_ref, opt_trait_reference);
535 visitor.visit_ty(typ);
536 walk_list!(visitor, visit_impl_item_ref, impl_item_refs);
538 ItemKind::Struct(ref struct_definition, ref generics) |
539 ItemKind::Union(ref struct_definition, ref generics) => {
540 visitor.visit_generics(generics);
541 visitor.visit_id(item.id);
542 visitor.visit_variant_data(struct_definition, item.name, generics, item.id, item.span);
544 ItemKind::Trait(.., ref generics, ref bounds, ref trait_item_refs) => {
545 visitor.visit_id(item.id);
546 visitor.visit_generics(generics);
547 walk_list!(visitor, visit_param_bound, bounds);
548 walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
550 ItemKind::TraitAlias(ref generics, ref bounds) => {
551 visitor.visit_id(item.id);
552 visitor.visit_generics(generics);
553 walk_list!(visitor, visit_param_bound, bounds);
556 walk_list!(visitor, visit_attribute, &item.attrs);
559 pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V,
563 visitor.visit_id(item_id);
564 visitor.visit_path(path, hir_id);
567 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
568 enum_definition: &'v EnumDef,
569 generics: &'v Generics,
571 visitor.visit_id(item_id);
574 &enum_definition.variants,
579 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
580 variant: &'v Variant,
581 generics: &'v Generics,
582 parent_item_id: NodeId) {
583 visitor.visit_name(variant.span, variant.node.name);
584 visitor.visit_variant_data(&variant.node.data,
589 walk_list!(visitor, visit_anon_const, &variant.node.disr_expr);
590 walk_list!(visitor, visit_attribute, &variant.node.attrs);
593 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
594 visitor.visit_id(typ.id);
597 TyKind::Slice(ref ty) => {
600 TyKind::Ptr(ref mutable_type) => {
601 visitor.visit_ty(&mutable_type.ty)
603 TyKind::Rptr(ref lifetime, ref mutable_type) => {
604 visitor.visit_lifetime(lifetime);
605 visitor.visit_ty(&mutable_type.ty)
608 TyKind::Tup(ref tuple_element_types) => {
609 walk_list!(visitor, visit_ty, tuple_element_types);
611 TyKind::BareFn(ref function_declaration) => {
612 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
613 visitor.visit_fn_decl(&function_declaration.decl);
615 TyKind::Path(ref qpath) => {
616 visitor.visit_qpath(qpath, typ.hir_id, typ.span);
618 TyKind::Def(item_id, ref lifetimes) => {
619 visitor.visit_nested_item(item_id);
620 walk_list!(visitor, visit_generic_arg, lifetimes);
622 TyKind::Array(ref ty, ref length) => {
623 visitor.visit_ty(ty);
624 visitor.visit_anon_const(length)
626 TyKind::TraitObject(ref bounds, ref lifetime) => {
627 for bound in bounds {
628 visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
630 visitor.visit_lifetime(lifetime);
632 TyKind::Typeof(ref expression) => {
633 visitor.visit_anon_const(expression)
635 TyKind::Infer | TyKind::Err => {}
639 pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath, id: HirId, span: Span) {
641 QPath::Resolved(ref maybe_qself, ref path) => {
642 if let Some(ref qself) = *maybe_qself {
643 visitor.visit_ty(qself);
645 visitor.visit_path(path, id)
647 QPath::TypeRelative(ref qself, ref segment) => {
648 visitor.visit_ty(qself);
649 visitor.visit_path_segment(span, segment);
654 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
655 visitor.visit_def_mention(path.def);
656 for segment in &path.segments {
657 visitor.visit_path_segment(path.span, segment);
661 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
663 segment: &'v PathSegment) {
664 visitor.visit_ident(segment.ident);
665 if let Some(id) = segment.id {
666 visitor.visit_id(id);
668 if let Some(ref args) = segment.args {
669 visitor.visit_generic_args(path_span, args);
673 pub fn walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V,
675 generic_args: &'v GenericArgs) {
676 walk_list!(visitor, visit_generic_arg, &generic_args.args);
677 walk_list!(visitor, visit_assoc_type_binding, &generic_args.bindings);
680 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
681 type_binding: &'v TypeBinding) {
682 visitor.visit_id(type_binding.id);
683 visitor.visit_ident(type_binding.ident);
684 visitor.visit_ty(&type_binding.ty);
687 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
688 visitor.visit_id(pattern.id);
690 PatKind::TupleStruct(ref qpath, ref children, _) => {
691 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
692 walk_list!(visitor, visit_pat, children);
694 PatKind::Path(ref qpath) => {
695 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
697 PatKind::Struct(ref qpath, ref fields, _) => {
698 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
699 for field in fields {
700 visitor.visit_id(field.node.id);
701 visitor.visit_ident(field.node.ident);
702 visitor.visit_pat(&field.node.pat)
705 PatKind::Tuple(ref tuple_elements, _) => {
706 walk_list!(visitor, visit_pat, tuple_elements);
708 PatKind::Box(ref subpattern) |
709 PatKind::Ref(ref subpattern, _) => {
710 visitor.visit_pat(subpattern)
712 PatKind::Binding(_, canonical_id, ident, ref optional_subpattern) => {
713 visitor.visit_def_mention(Def::Local(canonical_id));
714 visitor.visit_ident(ident);
715 walk_list!(visitor, visit_pat, optional_subpattern);
717 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
718 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
719 visitor.visit_expr(lower_bound);
720 visitor.visit_expr(upper_bound)
723 PatKind::Slice(ref prepatterns, ref slice_pattern, ref postpatterns) => {
724 walk_list!(visitor, visit_pat, prepatterns);
725 walk_list!(visitor, visit_pat, slice_pattern);
726 walk_list!(visitor, visit_pat, postpatterns);
731 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem) {
732 visitor.visit_id(foreign_item.id);
733 visitor.visit_vis(&foreign_item.vis);
734 visitor.visit_name(foreign_item.span, foreign_item.name);
736 match foreign_item.node {
737 ForeignItemKind::Fn(ref function_declaration, ref param_names, ref generics) => {
738 visitor.visit_generics(generics);
739 visitor.visit_fn_decl(function_declaration);
740 for ¶m_name in param_names {
741 visitor.visit_ident(param_name);
744 ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
745 ForeignItemKind::Type => (),
748 walk_list!(visitor, visit_attribute, &foreign_item.attrs);
751 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound) {
753 GenericBound::Trait(ref typ, modifier) => {
754 visitor.visit_poly_trait_ref(typ, modifier);
756 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
760 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam) {
761 visitor.visit_id(param.id);
762 walk_list!(visitor, visit_attribute, ¶m.attrs);
764 ParamName::Plain(ident) => visitor.visit_ident(ident),
765 ParamName::Error | ParamName::Fresh(_) => {}
768 GenericParamKind::Lifetime { .. } => {}
769 GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
771 walk_list!(visitor, visit_param_bound, ¶m.bounds);
774 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
775 walk_list!(visitor, visit_generic_param, &generics.params);
776 visitor.visit_id(generics.where_clause.id);
777 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
780 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
782 predicate: &'v WherePredicate)
785 &WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
787 ref bound_generic_params,
789 visitor.visit_ty(bounded_ty);
790 walk_list!(visitor, visit_param_bound, bounds);
791 walk_list!(visitor, visit_generic_param, bound_generic_params);
793 &WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
796 visitor.visit_lifetime(lifetime);
797 walk_list!(visitor, visit_param_bound, bounds);
799 &WherePredicate::EqPredicate(WhereEqPredicate{id,
803 visitor.visit_id(id);
804 visitor.visit_ty(lhs_ty);
805 visitor.visit_ty(rhs_ty);
810 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
811 if let Return(ref output_ty) = *ret_ty {
812 visitor.visit_ty(output_ty)
816 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
817 for ty in &function_declaration.inputs {
820 walk_fn_ret_ty(visitor, &function_declaration.output)
823 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
824 match function_kind {
825 FnKind::ItemFn(_, generics, ..) => {
826 visitor.visit_generics(generics);
829 FnKind::Closure(_) => {}
833 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
834 function_kind: FnKind<'v>,
835 function_declaration: &'v FnDecl,
839 visitor.visit_id(id);
840 visitor.visit_fn_decl(function_declaration);
841 walk_fn_kind(visitor, function_kind);
842 visitor.visit_nested_body(body_id)
845 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
846 visitor.visit_ident(trait_item.ident);
847 walk_list!(visitor, visit_attribute, &trait_item.attrs);
848 visitor.visit_generics(&trait_item.generics);
849 match trait_item.node {
850 TraitItemKind::Const(ref ty, default) => {
851 visitor.visit_id(trait_item.id);
852 visitor.visit_ty(ty);
853 walk_list!(visitor, visit_nested_body, default);
855 TraitItemKind::Method(ref sig, TraitMethod::Required(ref param_names)) => {
856 visitor.visit_id(trait_item.id);
857 visitor.visit_fn_decl(&sig.decl);
858 for ¶m_name in param_names {
859 visitor.visit_ident(param_name);
862 TraitItemKind::Method(ref sig, TraitMethod::Provided(body_id)) => {
863 visitor.visit_fn(FnKind::Method(trait_item.ident,
872 TraitItemKind::Type(ref bounds, ref default) => {
873 visitor.visit_id(trait_item.id);
874 walk_list!(visitor, visit_param_bound, bounds);
875 walk_list!(visitor, visit_ty, default);
880 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
881 // NB: Deliberately force a compilation error if/when new fields are added.
882 let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
883 visitor.visit_nested_trait_item(id);
884 visitor.visit_ident(ident);
885 visitor.visit_associated_item_kind(kind);
886 visitor.visit_defaultness(defaultness);
889 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
890 // NB: Deliberately force a compilation error if/when new fields are added.
903 visitor.visit_ident(ident);
904 visitor.visit_vis(vis);
905 visitor.visit_defaultness(defaultness);
906 walk_list!(visitor, visit_attribute, attrs);
907 visitor.visit_generics(generics);
909 ImplItemKind::Const(ref ty, body) => {
910 visitor.visit_id(impl_item.id);
911 visitor.visit_ty(ty);
912 visitor.visit_nested_body(body);
914 ImplItemKind::Method(ref sig, body_id) => {
915 visitor.visit_fn(FnKind::Method(impl_item.ident,
917 Some(&impl_item.vis),
924 ImplItemKind::Type(ref ty) => {
925 visitor.visit_id(impl_item.id);
926 visitor.visit_ty(ty);
928 ImplItemKind::Existential(ref bounds) => {
929 visitor.visit_id(impl_item.id);
930 walk_list!(visitor, visit_param_bound, bounds);
935 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
936 // NB: Deliberately force a compilation error if/when new fields are added.
937 let ImplItemRef { id, ident, ref kind, span: _, ref vis, ref defaultness } = *impl_item_ref;
938 visitor.visit_nested_impl_item(id);
939 visitor.visit_ident(ident);
940 visitor.visit_associated_item_kind(kind);
941 visitor.visit_vis(vis);
942 visitor.visit_defaultness(defaultness);
946 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &'v VariantData) {
947 visitor.visit_id(struct_definition.id());
948 walk_list!(visitor, visit_struct_field, struct_definition.fields());
951 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) {
952 visitor.visit_id(struct_field.id);
953 visitor.visit_vis(&struct_field.vis);
954 visitor.visit_ident(struct_field.ident);
955 visitor.visit_ty(&struct_field.ty);
956 walk_list!(visitor, visit_attribute, &struct_field.attrs);
959 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
960 visitor.visit_id(block.id);
961 walk_list!(visitor, visit_stmt, &block.stmts);
962 walk_list!(visitor, visit_expr, &block.expr);
965 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
966 match statement.node {
967 StmtKind::Decl(ref declaration, id) => {
968 visitor.visit_id(id);
969 visitor.visit_decl(declaration)
971 StmtKind::Expr(ref expression, id) |
972 StmtKind::Semi(ref expression, id) => {
973 visitor.visit_id(id);
974 visitor.visit_expr(expression)
979 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
980 match declaration.node {
981 DeclKind::Local(ref local) => visitor.visit_local(local),
982 DeclKind::Item(item) => visitor.visit_nested_item(item),
986 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
987 visitor.visit_id(constant.id);
988 visitor.visit_nested_body(constant.body);
991 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
992 visitor.visit_id(expression.id);
993 walk_list!(visitor, visit_attribute, expression.attrs.iter());
994 match expression.node {
995 ExprKind::Box(ref subexpression) => {
996 visitor.visit_expr(subexpression)
998 ExprKind::Array(ref subexpressions) => {
999 walk_list!(visitor, visit_expr, subexpressions);
1001 ExprKind::Repeat(ref element, ref count) => {
1002 visitor.visit_expr(element);
1003 visitor.visit_anon_const(count)
1005 ExprKind::Struct(ref qpath, ref fields, ref optional_base) => {
1006 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1007 for field in fields {
1008 visitor.visit_id(field.id);
1009 visitor.visit_ident(field.ident);
1010 visitor.visit_expr(&field.expr)
1012 walk_list!(visitor, visit_expr, optional_base);
1014 ExprKind::Tup(ref subexpressions) => {
1015 walk_list!(visitor, visit_expr, subexpressions);
1017 ExprKind::Call(ref callee_expression, ref arguments) => {
1018 visitor.visit_expr(callee_expression);
1019 walk_list!(visitor, visit_expr, arguments);
1021 ExprKind::MethodCall(ref segment, _, ref arguments) => {
1022 visitor.visit_path_segment(expression.span, segment);
1023 walk_list!(visitor, visit_expr, arguments);
1025 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
1026 visitor.visit_expr(left_expression);
1027 visitor.visit_expr(right_expression)
1029 ExprKind::AddrOf(_, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
1030 visitor.visit_expr(subexpression)
1032 ExprKind::Lit(_) => {}
1033 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
1034 visitor.visit_expr(subexpression);
1035 visitor.visit_ty(typ)
1037 ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
1038 visitor.visit_expr(head_expression);
1039 visitor.visit_expr(if_block);
1040 walk_list!(visitor, visit_expr, optional_else);
1042 ExprKind::While(ref subexpression, ref block, ref opt_label) => {
1043 walk_list!(visitor, visit_label, opt_label);
1044 visitor.visit_expr(subexpression);
1045 visitor.visit_block(block);
1047 ExprKind::Loop(ref block, ref opt_label, _) => {
1048 walk_list!(visitor, visit_label, opt_label);
1049 visitor.visit_block(block);
1051 ExprKind::Match(ref subexpression, ref arms, _) => {
1052 visitor.visit_expr(subexpression);
1053 walk_list!(visitor, visit_arm, arms);
1055 ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => {
1056 visitor.visit_fn(FnKind::Closure(&expression.attrs),
1057 function_declaration,
1062 ExprKind::Block(ref block, ref opt_label) => {
1063 walk_list!(visitor, visit_label, opt_label);
1064 visitor.visit_block(block);
1066 ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
1067 visitor.visit_expr(right_hand_expression);
1068 visitor.visit_expr(left_hand_expression)
1070 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
1071 visitor.visit_expr(right_expression);
1072 visitor.visit_expr(left_expression)
1074 ExprKind::Field(ref subexpression, ident) => {
1075 visitor.visit_expr(subexpression);
1076 visitor.visit_ident(ident);
1078 ExprKind::Index(ref main_expression, ref index_expression) => {
1079 visitor.visit_expr(main_expression);
1080 visitor.visit_expr(index_expression)
1082 ExprKind::Path(ref qpath) => {
1083 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1085 ExprKind::Break(ref destination, ref opt_expr) => {
1086 if let Some(ref label) = destination.label {
1087 visitor.visit_label(label);
1088 if let Ok(node_id) = destination.target_id {
1089 visitor.visit_def_mention(Def::Label(node_id))
1092 walk_list!(visitor, visit_expr, opt_expr);
1094 ExprKind::Continue(ref destination) => {
1095 if let Some(ref label) = destination.label {
1096 visitor.visit_label(label);
1097 if let Ok(node_id) = destination.target_id {
1098 visitor.visit_def_mention(Def::Label(node_id))
1102 ExprKind::Ret(ref optional_expression) => {
1103 walk_list!(visitor, visit_expr, optional_expression);
1105 ExprKind::InlineAsm(_, ref outputs, ref inputs) => {
1106 for expr in outputs.iter().chain(inputs.iter()) {
1107 visitor.visit_expr(expr)
1110 ExprKind::Yield(ref subexpression) => {
1111 visitor.visit_expr(subexpression);
1116 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
1117 walk_list!(visitor, visit_pat, &arm.pats);
1118 if let Some(ref g) = arm.guard {
1120 Guard::If(ref e) => visitor.visit_expr(e),
1123 visitor.visit_expr(&arm.body);
1124 walk_list!(visitor, visit_attribute, &arm.attrs);
1127 pub fn walk_vis<'v, V: Visitor<'v>>(visitor: &mut V, vis: &'v Visibility) {
1128 if let VisibilityKind::Restricted { ref path, id, hir_id } = vis.node {
1129 visitor.visit_id(id);
1130 visitor.visit_path(path, hir_id)
1134 pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssociatedItemKind) {
1135 // No visitable content here: this fn exists so you can call it if
1136 // the right thing to do, should content be added in the future,
1137 // would be to walk it.
1140 pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) {
1141 // No visitable content here: this fn exists so you can call it if
1142 // the right thing to do, should content be added in the future,
1143 // would be to walk it.
1146 #[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug)]
1147 pub struct IdRange {
1153 pub fn max() -> IdRange {
1155 min: NodeId::from_u32(u32::MAX),
1156 max: NodeId::from_u32(u32::MIN),
1160 pub fn empty(&self) -> bool {
1161 self.min >= self.max
1164 pub fn contains(&self, id: NodeId) -> bool {
1165 id >= self.min && id < self.max
1168 pub fn add(&mut self, id: NodeId) {
1169 self.min = cmp::min(self.min, id);
1170 self.max = cmp::max(self.max, NodeId::from_u32(id.as_u32() + 1));
1175 pub struct IdRangeComputingVisitor<'a, 'hir: 'a> {
1177 map: &'a map::Map<'hir>,
1180 impl<'a, 'hir> IdRangeComputingVisitor<'a, 'hir> {
1181 pub fn new(map: &'a map::Map<'hir>) -> IdRangeComputingVisitor<'a, 'hir> {
1182 IdRangeComputingVisitor { result: IdRange::max(), map: map }
1185 pub fn result(&self) -> IdRange {
1190 impl<'a, 'hir> Visitor<'hir> for IdRangeComputingVisitor<'a, 'hir> {
1191 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
1192 NestedVisitorMap::OnlyBodies(&self.map)
1195 fn visit_id(&mut self, id: NodeId) {
1196 self.result.add(id);