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 #[derive(Copy, Clone)]
55 /// #[xxx] pub async/const/extern "Abi" fn foo()
56 ItemFn(Name, &'a Generics, FnHeader, &'a Visibility, &'a [Attribute]),
59 Method(Ident, &'a MethodSig, Option<&'a Visibility>, &'a [Attribute]),
62 Closure(&'a [Attribute]),
66 pub fn attrs(&self) -> &'a [Attribute] {
68 FnKind::ItemFn(.., attrs) => attrs,
69 FnKind::Method(.., attrs) => attrs,
70 FnKind::Closure(attrs) => attrs,
75 /// Specifies what nested things a visitor wants to visit. The most
76 /// common choice is `OnlyBodies`, which will cause the visitor to
77 /// visit fn bodies for fns that it encounters, but skip over nested
80 /// See the comments on `ItemLikeVisitor` for more details on the overall
82 pub enum NestedVisitorMap<'this, 'tcx: 'this> {
83 /// Do not visit any nested things. When you add a new
84 /// "non-nested" thing, you will want to audit such uses to see if
85 /// they remain valid.
87 /// Use this if you are only walking some particular kind of tree
88 /// (i.e., a type, or fn signature) and you don't want to thread a
92 /// Do not visit nested item-like things, but visit nested things
93 /// that are inside of an item-like.
95 /// **This is the most common choice.** A very common pattern is
96 /// to use `visit_all_item_likes()` as an outer loop,
97 /// and to have the visitor that visits the contents of each item
98 /// using this setting.
99 OnlyBodies(&'this Map<'tcx>),
101 /// Visit all nested things, including item-likes.
103 /// **This is an unusual choice.** It is used when you want to
104 /// process everything within their lexical context. Typically you
105 /// kick off the visit by doing `walk_krate()`.
106 All(&'this Map<'tcx>),
109 impl<'this, 'tcx> NestedVisitorMap<'this, 'tcx> {
110 /// Returns the map to use for an "intra item-like" thing (if any).
111 /// e.g., function body.
112 pub fn intra(self) -> Option<&'this Map<'tcx>> {
114 NestedVisitorMap::None => None,
115 NestedVisitorMap::OnlyBodies(map) => Some(map),
116 NestedVisitorMap::All(map) => Some(map),
120 /// Returns the map to use for an "item-like" thing (if any).
121 /// e.g., item, impl-item.
122 pub fn inter(self) -> Option<&'this Map<'tcx>> {
124 NestedVisitorMap::None => None,
125 NestedVisitorMap::OnlyBodies(_) => None,
126 NestedVisitorMap::All(map) => Some(map),
131 /// Each method of the Visitor trait is a hook to be potentially
132 /// overridden. Each method's default implementation recursively visits
133 /// the substructure of the input via the corresponding `walk` method;
134 /// e.g., the `visit_mod` method by default calls `intravisit::walk_mod`.
136 /// Note that this visitor does NOT visit nested items by default
137 /// (this is why the module is called `intravisit`, to distinguish it
138 /// from the AST's `visit` module, which acts differently). If you
139 /// simply want to visit all items in the crate in some order, you
140 /// should call `Crate::visit_all_items`. Otherwise, see the comment
141 /// on `visit_nested_item` for details on how to visit nested items.
143 /// If you want to ensure that your code handles every variant
144 /// explicitly, you need to override each method. (And you also need
145 /// to monitor future changes to `Visitor` in case a new method with a
146 /// new default implementation gets introduced.)
147 pub trait Visitor<'v> : Sized {
148 ///////////////////////////////////////////////////////////////////////////
151 /// The default versions of the `visit_nested_XXX` routines invoke
152 /// this method to get a map to use. By selecting an enum variant,
153 /// you control which kinds of nested HIR are visited; see
154 /// `NestedVisitorMap` for details. By "nested HIR", we are
155 /// referring to bits of HIR that are not directly embedded within
156 /// one another but rather indirectly, through a table in the
157 /// crate. This is done to control dependencies during incremental
158 /// compilation: the non-inline bits of HIR can be tracked and
159 /// hashed separately.
161 /// **If for some reason you want the nested behavior, but don't
162 /// have a `Map` at your disposal:** then you should override the
163 /// `visit_nested_XXX` methods, and override this method to
164 /// `panic!()`. This way, if a new `visit_nested_XXX` variant is
165 /// added in the future, we will see the panic in your code and
166 /// fix it appropriately.
167 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v>;
169 /// Invoked when a nested item is encountered. By default does
170 /// nothing unless you override `nested_visit_map` to return
171 /// `Some(_)`, in which case it will walk the item. **You probably
172 /// don't want to override this method** -- instead, override
173 /// `nested_visit_map` or use the "shallow" or "deep" visit
174 /// patterns described on `itemlikevisit::ItemLikeVisitor`. The only
175 /// reason to override this method is if you want a nested pattern
176 /// but cannot supply a `Map`; see `nested_visit_map` for advice.
177 #[allow(unused_variables)]
178 fn visit_nested_item(&mut self, id: ItemId) {
179 let opt_item = self.nested_visit_map().inter().map(|map| map.expect_item(id.id));
180 if let Some(item) = opt_item {
181 self.visit_item(item);
185 /// Like `visit_nested_item()`, but for trait items. See
186 /// `visit_nested_item()` for advice on when to override this
188 #[allow(unused_variables)]
189 fn visit_nested_trait_item(&mut self, id: TraitItemId) {
190 let opt_item = self.nested_visit_map().inter().map(|map| map.trait_item(id));
191 if let Some(item) = opt_item {
192 self.visit_trait_item(item);
196 /// Like `visit_nested_item()`, but for impl items. See
197 /// `visit_nested_item()` for advice on when to override this
199 #[allow(unused_variables)]
200 fn visit_nested_impl_item(&mut self, id: ImplItemId) {
201 let opt_item = self.nested_visit_map().inter().map(|map| map.impl_item(id));
202 if let Some(item) = opt_item {
203 self.visit_impl_item(item);
207 /// Invoked to visit the body of a function, method or closure. Like
208 /// visit_nested_item, does nothing by default unless you override
209 /// `nested_visit_map` to return `Some(_)`, in which case it will walk the
211 fn visit_nested_body(&mut self, id: BodyId) {
212 let opt_body = self.nested_visit_map().intra().map(|map| map.body(id));
213 if let Some(body) = opt_body {
214 self.visit_body(body);
218 /// Visit the top-level item and (optionally) nested items / impl items. See
219 /// `visit_nested_item` for details.
220 fn visit_item(&mut self, i: &'v Item) {
224 fn visit_body(&mut self, b: &'v Body) {
228 /// When invoking `visit_all_item_likes()`, you need to supply an
229 /// item-like visitor. This method converts a "intra-visit"
230 /// visitor into an item-like visitor that walks the entire tree.
231 /// If you use this, you probably don't want to process the
232 /// contents of nested item-like things, since the outer loop will
233 /// visit them as well.
234 fn as_deep_visitor<'s>(&'s mut self) -> DeepVisitor<'s, Self> {
235 DeepVisitor::new(self)
238 ///////////////////////////////////////////////////////////////////////////
240 fn visit_id(&mut self, _node_id: NodeId) {
243 fn visit_def_mention(&mut self, _def: Def) {
246 fn visit_name(&mut self, _span: Span, _name: Name) {
249 fn visit_ident(&mut self, ident: Ident) {
250 walk_ident(self, ident)
252 fn visit_mod(&mut self, m: &'v Mod, _s: Span, n: NodeId) {
255 fn visit_foreign_item(&mut self, i: &'v ForeignItem) {
256 walk_foreign_item(self, i)
258 fn visit_local(&mut self, l: &'v Local) {
261 fn visit_block(&mut self, b: &'v Block) {
264 fn visit_stmt(&mut self, s: &'v Stmt) {
267 fn visit_arm(&mut self, a: &'v Arm) {
270 fn visit_pat(&mut self, p: &'v Pat) {
273 fn visit_decl(&mut self, d: &'v Decl) {
276 fn visit_anon_const(&mut self, c: &'v AnonConst) {
277 walk_anon_const(self, c)
279 fn visit_expr(&mut self, ex: &'v Expr) {
282 fn visit_ty(&mut self, t: &'v Ty) {
285 fn visit_generic_param(&mut self, p: &'v GenericParam) {
286 walk_generic_param(self, p)
288 fn visit_generics(&mut self, g: &'v Generics) {
289 walk_generics(self, g)
291 fn visit_where_predicate(&mut self, predicate: &'v WherePredicate) {
292 walk_where_predicate(self, predicate)
294 fn visit_fn_decl(&mut self, fd: &'v FnDecl) {
295 walk_fn_decl(self, fd)
297 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: BodyId, s: Span, id: NodeId) {
298 walk_fn(self, fk, fd, b, s, id)
300 fn visit_use(&mut self, path: &'v Path, id: NodeId, hir_id: HirId) {
301 walk_use(self, path, id, hir_id)
303 fn visit_trait_item(&mut self, ti: &'v TraitItem) {
304 walk_trait_item(self, ti)
306 fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
307 walk_trait_item_ref(self, ii)
309 fn visit_impl_item(&mut self, ii: &'v ImplItem) {
310 walk_impl_item(self, ii)
312 fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) {
313 walk_impl_item_ref(self, ii)
315 fn visit_trait_ref(&mut self, t: &'v TraitRef) {
316 walk_trait_ref(self, t)
318 fn visit_param_bound(&mut self, bounds: &'v GenericBound) {
319 walk_param_bound(self, bounds)
321 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: TraitBoundModifier) {
322 walk_poly_trait_ref(self, t, m)
324 fn visit_variant_data(&mut self,
330 walk_struct_def(self, s)
332 fn visit_struct_field(&mut self, s: &'v StructField) {
333 walk_struct_field(self, s)
335 fn visit_enum_def(&mut self,
336 enum_definition: &'v EnumDef,
337 generics: &'v Generics,
340 walk_enum_def(self, enum_definition, generics, item_id)
342 fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics, item_id: NodeId) {
343 walk_variant(self, v, g, item_id)
345 fn visit_label(&mut self, label: &'v Label) {
346 walk_label(self, label)
348 fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg) {
350 GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
351 GenericArg::Type(ty) => self.visit_ty(ty),
354 fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
355 walk_lifetime(self, lifetime)
357 fn visit_qpath(&mut self, qpath: &'v QPath, id: HirId, span: Span) {
358 walk_qpath(self, qpath, id, span)
360 fn visit_path(&mut self, path: &'v Path, _id: HirId) {
361 walk_path(self, path)
363 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
364 walk_path_segment(self, path_span, path_segment)
366 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs) {
367 walk_generic_args(self, path_span, generic_args)
369 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
370 walk_assoc_type_binding(self, type_binding)
372 fn visit_attribute(&mut self, _attr: &'v Attribute) {
374 fn visit_macro_def(&mut self, macro_def: &'v MacroDef) {
375 walk_macro_def(self, macro_def)
377 fn visit_vis(&mut self, vis: &'v Visibility) {
380 fn visit_associated_item_kind(&mut self, kind: &'v AssociatedItemKind) {
381 walk_associated_item_kind(self, kind);
383 fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
384 walk_defaultness(self, defaultness);
388 /// Walks the contents of a crate. See also `Crate::visit_all_items`.
389 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
390 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
391 walk_list!(visitor, visit_attribute, &krate.attrs);
392 walk_list!(visitor, visit_macro_def, &krate.exported_macros);
395 pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef) {
396 visitor.visit_id(macro_def.id);
397 visitor.visit_name(macro_def.span, macro_def.name);
398 walk_list!(visitor, visit_attribute, ¯o_def.attrs);
401 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod, mod_node_id: NodeId) {
402 visitor.visit_id(mod_node_id);
403 for &item_id in &module.item_ids {
404 visitor.visit_nested_item(item_id);
408 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body) {
409 for argument in &body.arguments {
410 visitor.visit_id(argument.id);
411 visitor.visit_pat(&argument.pat);
413 visitor.visit_expr(&body.value);
416 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
417 // Intentionally visiting the expr first - the initialization expr
418 // dominates the local's definition.
419 walk_list!(visitor, visit_expr, &local.init);
420 walk_list!(visitor, visit_attribute, local.attrs.iter());
421 visitor.visit_id(local.id);
422 visitor.visit_pat(&local.pat);
423 walk_list!(visitor, visit_ty, &local.ty);
426 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
427 visitor.visit_name(ident.span, ident.name);
430 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
431 visitor.visit_ident(label.ident);
434 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
435 visitor.visit_id(lifetime.id);
436 match lifetime.name {
437 LifetimeName::Param(ParamName::Plain(ident)) => {
438 visitor.visit_ident(ident);
440 LifetimeName::Param(ParamName::Fresh(_)) |
441 LifetimeName::Param(ParamName::Error) |
442 LifetimeName::Static |
443 LifetimeName::Error |
444 LifetimeName::Implicit |
445 LifetimeName::Underscore => {}
449 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
450 trait_ref: &'v PolyTraitRef,
451 _modifier: TraitBoundModifier)
454 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
455 visitor.visit_trait_ref(&trait_ref.trait_ref);
458 pub fn walk_trait_ref<'v, V>(visitor: &mut V, trait_ref: &'v TraitRef)
461 visitor.visit_id(trait_ref.ref_id);
462 visitor.visit_path(&trait_ref.path, trait_ref.hir_ref_id)
465 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
466 visitor.visit_vis(&item.vis);
467 visitor.visit_name(item.span, item.name);
469 ItemKind::ExternCrate(orig_name) => {
470 visitor.visit_id(item.id);
471 if let Some(orig_name) = orig_name {
472 visitor.visit_name(item.span, orig_name);
475 ItemKind::Use(ref path, _) => {
476 visitor.visit_use(path, item.id, item.hir_id);
478 ItemKind::Static(ref typ, _, body) |
479 ItemKind::Const(ref typ, body) => {
480 visitor.visit_id(item.id);
481 visitor.visit_ty(typ);
482 visitor.visit_nested_body(body);
484 ItemKind::Fn(ref declaration, header, ref generics, body_id) => {
485 visitor.visit_fn(FnKind::ItemFn(item.name,
495 ItemKind::Mod(ref module) => {
496 // `visit_mod()` takes care of visiting the `Item`'s `NodeId`.
497 visitor.visit_mod(module, item.span, item.id)
499 ItemKind::ForeignMod(ref foreign_module) => {
500 visitor.visit_id(item.id);
501 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
503 ItemKind::GlobalAsm(_) => {
504 visitor.visit_id(item.id);
506 ItemKind::Ty(ref typ, ref type_parameters) => {
507 visitor.visit_id(item.id);
508 visitor.visit_ty(typ);
509 visitor.visit_generics(type_parameters)
511 ItemKind::Existential(ExistTy {ref generics, ref bounds, impl_trait_fn}) => {
512 visitor.visit_id(item.id);
513 walk_generics(visitor, generics);
514 walk_list!(visitor, visit_param_bound, bounds);
515 if let Some(impl_trait_fn) = impl_trait_fn {
516 visitor.visit_def_mention(Def::Fn(impl_trait_fn))
519 ItemKind::Enum(ref enum_definition, ref type_parameters) => {
520 visitor.visit_generics(type_parameters);
521 // `visit_enum_def()` takes care of visiting the `Item`'s `NodeId`.
522 visitor.visit_enum_def(enum_definition, type_parameters, item.id, item.span)
527 ref opt_trait_reference,
531 visitor.visit_id(item.id);
532 visitor.visit_generics(type_parameters);
533 walk_list!(visitor, visit_trait_ref, opt_trait_reference);
534 visitor.visit_ty(typ);
535 walk_list!(visitor, visit_impl_item_ref, impl_item_refs);
537 ItemKind::Struct(ref struct_definition, ref generics) |
538 ItemKind::Union(ref struct_definition, ref generics) => {
539 visitor.visit_generics(generics);
540 visitor.visit_id(item.id);
541 visitor.visit_variant_data(struct_definition, item.name, generics, item.id, item.span);
543 ItemKind::Trait(.., ref generics, ref bounds, ref trait_item_refs) => {
544 visitor.visit_id(item.id);
545 visitor.visit_generics(generics);
546 walk_list!(visitor, visit_param_bound, bounds);
547 walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
549 ItemKind::TraitAlias(ref generics, ref bounds) => {
550 visitor.visit_id(item.id);
551 visitor.visit_generics(generics);
552 walk_list!(visitor, visit_param_bound, bounds);
555 walk_list!(visitor, visit_attribute, &item.attrs);
558 pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V,
562 visitor.visit_id(item_id);
563 visitor.visit_path(path, hir_id);
566 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
567 enum_definition: &'v EnumDef,
568 generics: &'v Generics,
570 visitor.visit_id(item_id);
573 &enum_definition.variants,
578 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
579 variant: &'v Variant,
580 generics: &'v Generics,
581 parent_item_id: NodeId) {
582 visitor.visit_name(variant.span, variant.node.name);
583 visitor.visit_variant_data(&variant.node.data,
588 walk_list!(visitor, visit_anon_const, &variant.node.disr_expr);
589 walk_list!(visitor, visit_attribute, &variant.node.attrs);
592 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
593 visitor.visit_id(typ.id);
596 TyKind::Slice(ref ty) => {
599 TyKind::Ptr(ref mutable_type) => {
600 visitor.visit_ty(&mutable_type.ty)
602 TyKind::Rptr(ref lifetime, ref mutable_type) => {
603 visitor.visit_lifetime(lifetime);
604 visitor.visit_ty(&mutable_type.ty)
607 TyKind::Tup(ref tuple_element_types) => {
608 walk_list!(visitor, visit_ty, tuple_element_types);
610 TyKind::BareFn(ref function_declaration) => {
611 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
612 visitor.visit_fn_decl(&function_declaration.decl);
614 TyKind::Path(ref qpath) => {
615 visitor.visit_qpath(qpath, typ.hir_id, typ.span);
617 TyKind::Def(item_id, ref lifetimes) => {
618 visitor.visit_nested_item(item_id);
619 walk_list!(visitor, visit_generic_arg, lifetimes);
621 TyKind::Array(ref ty, ref length) => {
622 visitor.visit_ty(ty);
623 visitor.visit_anon_const(length)
625 TyKind::TraitObject(ref bounds, ref lifetime) => {
626 for bound in bounds {
627 visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
629 visitor.visit_lifetime(lifetime);
631 TyKind::Typeof(ref expression) => {
632 visitor.visit_anon_const(expression)
634 TyKind::Infer | TyKind::Err => {}
638 pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath, id: HirId, span: Span) {
640 QPath::Resolved(ref maybe_qself, ref path) => {
641 if let Some(ref qself) = *maybe_qself {
642 visitor.visit_ty(qself);
644 visitor.visit_path(path, id)
646 QPath::TypeRelative(ref qself, ref segment) => {
647 visitor.visit_ty(qself);
648 visitor.visit_path_segment(span, segment);
653 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
654 visitor.visit_def_mention(path.def);
655 for segment in &path.segments {
656 visitor.visit_path_segment(path.span, segment);
660 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
662 segment: &'v PathSegment) {
663 visitor.visit_ident(segment.ident);
664 if let Some(id) = segment.id {
665 visitor.visit_id(id);
667 if let Some(ref args) = segment.args {
668 visitor.visit_generic_args(path_span, args);
672 pub fn walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V,
674 generic_args: &'v GenericArgs) {
675 walk_list!(visitor, visit_generic_arg, &generic_args.args);
676 walk_list!(visitor, visit_assoc_type_binding, &generic_args.bindings);
679 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
680 type_binding: &'v TypeBinding) {
681 visitor.visit_id(type_binding.id);
682 visitor.visit_ident(type_binding.ident);
683 visitor.visit_ty(&type_binding.ty);
686 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
687 visitor.visit_id(pattern.id);
689 PatKind::TupleStruct(ref qpath, ref children, _) => {
690 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
691 walk_list!(visitor, visit_pat, children);
693 PatKind::Path(ref qpath) => {
694 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
696 PatKind::Struct(ref qpath, ref fields, _) => {
697 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
698 for field in fields {
699 visitor.visit_id(field.node.id);
700 visitor.visit_ident(field.node.ident);
701 visitor.visit_pat(&field.node.pat)
704 PatKind::Tuple(ref tuple_elements, _) => {
705 walk_list!(visitor, visit_pat, tuple_elements);
707 PatKind::Box(ref subpattern) |
708 PatKind::Ref(ref subpattern, _) => {
709 visitor.visit_pat(subpattern)
711 PatKind::Binding(_, canonical_id, ident, ref optional_subpattern) => {
712 visitor.visit_def_mention(Def::Local(canonical_id));
713 visitor.visit_ident(ident);
714 walk_list!(visitor, visit_pat, optional_subpattern);
716 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
717 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
718 visitor.visit_expr(lower_bound);
719 visitor.visit_expr(upper_bound)
722 PatKind::Slice(ref prepatterns, ref slice_pattern, ref postpatterns) => {
723 walk_list!(visitor, visit_pat, prepatterns);
724 walk_list!(visitor, visit_pat, slice_pattern);
725 walk_list!(visitor, visit_pat, postpatterns);
730 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem) {
731 visitor.visit_id(foreign_item.id);
732 visitor.visit_vis(&foreign_item.vis);
733 visitor.visit_name(foreign_item.span, foreign_item.name);
735 match foreign_item.node {
736 ForeignItemKind::Fn(ref function_declaration, ref param_names, ref generics) => {
737 visitor.visit_generics(generics);
738 visitor.visit_fn_decl(function_declaration);
739 for ¶m_name in param_names {
740 visitor.visit_ident(param_name);
743 ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
744 ForeignItemKind::Type => (),
747 walk_list!(visitor, visit_attribute, &foreign_item.attrs);
750 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound) {
752 GenericBound::Trait(ref typ, modifier) => {
753 visitor.visit_poly_trait_ref(typ, modifier);
755 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
759 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam) {
760 visitor.visit_id(param.id);
761 walk_list!(visitor, visit_attribute, ¶m.attrs);
763 ParamName::Plain(ident) => visitor.visit_ident(ident),
764 ParamName::Error | ParamName::Fresh(_) => {}
767 GenericParamKind::Lifetime { .. } => {}
768 GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
770 walk_list!(visitor, visit_param_bound, ¶m.bounds);
773 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
774 walk_list!(visitor, visit_generic_param, &generics.params);
775 visitor.visit_id(generics.where_clause.id);
776 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
779 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
781 predicate: &'v WherePredicate)
784 &WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
786 ref bound_generic_params,
788 visitor.visit_ty(bounded_ty);
789 walk_list!(visitor, visit_param_bound, bounds);
790 walk_list!(visitor, visit_generic_param, bound_generic_params);
792 &WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
795 visitor.visit_lifetime(lifetime);
796 walk_list!(visitor, visit_param_bound, bounds);
798 &WherePredicate::EqPredicate(WhereEqPredicate{id,
802 visitor.visit_id(id);
803 visitor.visit_ty(lhs_ty);
804 visitor.visit_ty(rhs_ty);
809 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
810 if let Return(ref output_ty) = *ret_ty {
811 visitor.visit_ty(output_ty)
815 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
816 for ty in &function_declaration.inputs {
819 walk_fn_ret_ty(visitor, &function_declaration.output)
822 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
823 match function_kind {
824 FnKind::ItemFn(_, generics, ..) => {
825 visitor.visit_generics(generics);
828 FnKind::Closure(_) => {}
832 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
833 function_kind: FnKind<'v>,
834 function_declaration: &'v FnDecl,
838 visitor.visit_id(id);
839 visitor.visit_fn_decl(function_declaration);
840 walk_fn_kind(visitor, function_kind);
841 visitor.visit_nested_body(body_id)
844 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
845 visitor.visit_ident(trait_item.ident);
846 walk_list!(visitor, visit_attribute, &trait_item.attrs);
847 visitor.visit_generics(&trait_item.generics);
848 match trait_item.node {
849 TraitItemKind::Const(ref ty, default) => {
850 visitor.visit_id(trait_item.id);
851 visitor.visit_ty(ty);
852 walk_list!(visitor, visit_nested_body, default);
854 TraitItemKind::Method(ref sig, TraitMethod::Required(ref param_names)) => {
855 visitor.visit_id(trait_item.id);
856 visitor.visit_fn_decl(&sig.decl);
857 for ¶m_name in param_names {
858 visitor.visit_ident(param_name);
861 TraitItemKind::Method(ref sig, TraitMethod::Provided(body_id)) => {
862 visitor.visit_fn(FnKind::Method(trait_item.ident,
871 TraitItemKind::Type(ref bounds, ref default) => {
872 visitor.visit_id(trait_item.id);
873 walk_list!(visitor, visit_param_bound, bounds);
874 walk_list!(visitor, visit_ty, default);
879 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
880 // N.B., deliberately force a compilation error if/when new fields are added.
881 let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
882 visitor.visit_nested_trait_item(id);
883 visitor.visit_ident(ident);
884 visitor.visit_associated_item_kind(kind);
885 visitor.visit_defaultness(defaultness);
888 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
889 // N.B., deliberately force a compilation error if/when new fields are added.
902 visitor.visit_ident(ident);
903 visitor.visit_vis(vis);
904 visitor.visit_defaultness(defaultness);
905 walk_list!(visitor, visit_attribute, attrs);
906 visitor.visit_generics(generics);
908 ImplItemKind::Const(ref ty, body) => {
909 visitor.visit_id(impl_item.id);
910 visitor.visit_ty(ty);
911 visitor.visit_nested_body(body);
913 ImplItemKind::Method(ref sig, body_id) => {
914 visitor.visit_fn(FnKind::Method(impl_item.ident,
916 Some(&impl_item.vis),
923 ImplItemKind::Type(ref ty) => {
924 visitor.visit_id(impl_item.id);
925 visitor.visit_ty(ty);
927 ImplItemKind::Existential(ref bounds) => {
928 visitor.visit_id(impl_item.id);
929 walk_list!(visitor, visit_param_bound, bounds);
934 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
935 // N.B., deliberately force a compilation error if/when new fields are added.
936 let ImplItemRef { id, ident, ref kind, span: _, ref vis, ref defaultness } = *impl_item_ref;
937 visitor.visit_nested_impl_item(id);
938 visitor.visit_ident(ident);
939 visitor.visit_associated_item_kind(kind);
940 visitor.visit_vis(vis);
941 visitor.visit_defaultness(defaultness);
945 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &'v VariantData) {
946 visitor.visit_id(struct_definition.id());
947 walk_list!(visitor, visit_struct_field, struct_definition.fields());
950 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) {
951 visitor.visit_id(struct_field.id);
952 visitor.visit_vis(&struct_field.vis);
953 visitor.visit_ident(struct_field.ident);
954 visitor.visit_ty(&struct_field.ty);
955 walk_list!(visitor, visit_attribute, &struct_field.attrs);
958 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
959 visitor.visit_id(block.id);
960 walk_list!(visitor, visit_stmt, &block.stmts);
961 walk_list!(visitor, visit_expr, &block.expr);
964 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
965 match statement.node {
966 StmtKind::Decl(ref declaration, id) => {
967 visitor.visit_id(id);
968 visitor.visit_decl(declaration)
970 StmtKind::Expr(ref expression, id) |
971 StmtKind::Semi(ref expression, id) => {
972 visitor.visit_id(id);
973 visitor.visit_expr(expression)
978 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
979 match declaration.node {
980 DeclKind::Local(ref local) => visitor.visit_local(local),
981 DeclKind::Item(item) => visitor.visit_nested_item(item),
985 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
986 visitor.visit_id(constant.id);
987 visitor.visit_nested_body(constant.body);
990 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
991 visitor.visit_id(expression.id);
992 walk_list!(visitor, visit_attribute, expression.attrs.iter());
993 match expression.node {
994 ExprKind::Box(ref subexpression) => {
995 visitor.visit_expr(subexpression)
997 ExprKind::Array(ref subexpressions) => {
998 walk_list!(visitor, visit_expr, subexpressions);
1000 ExprKind::Repeat(ref element, ref count) => {
1001 visitor.visit_expr(element);
1002 visitor.visit_anon_const(count)
1004 ExprKind::Struct(ref qpath, ref fields, ref optional_base) => {
1005 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1006 for field in fields {
1007 visitor.visit_id(field.id);
1008 visitor.visit_ident(field.ident);
1009 visitor.visit_expr(&field.expr)
1011 walk_list!(visitor, visit_expr, optional_base);
1013 ExprKind::Tup(ref subexpressions) => {
1014 walk_list!(visitor, visit_expr, subexpressions);
1016 ExprKind::Call(ref callee_expression, ref arguments) => {
1017 visitor.visit_expr(callee_expression);
1018 walk_list!(visitor, visit_expr, arguments);
1020 ExprKind::MethodCall(ref segment, _, ref arguments) => {
1021 visitor.visit_path_segment(expression.span, segment);
1022 walk_list!(visitor, visit_expr, arguments);
1024 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
1025 visitor.visit_expr(left_expression);
1026 visitor.visit_expr(right_expression)
1028 ExprKind::AddrOf(_, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
1029 visitor.visit_expr(subexpression)
1031 ExprKind::Lit(_) => {}
1032 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
1033 visitor.visit_expr(subexpression);
1034 visitor.visit_ty(typ)
1036 ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
1037 visitor.visit_expr(head_expression);
1038 visitor.visit_expr(if_block);
1039 walk_list!(visitor, visit_expr, optional_else);
1041 ExprKind::While(ref subexpression, ref block, ref opt_label) => {
1042 walk_list!(visitor, visit_label, opt_label);
1043 visitor.visit_expr(subexpression);
1044 visitor.visit_block(block);
1046 ExprKind::Loop(ref block, ref opt_label, _) => {
1047 walk_list!(visitor, visit_label, opt_label);
1048 visitor.visit_block(block);
1050 ExprKind::Match(ref subexpression, ref arms, _) => {
1051 visitor.visit_expr(subexpression);
1052 walk_list!(visitor, visit_arm, arms);
1054 ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => {
1055 visitor.visit_fn(FnKind::Closure(&expression.attrs),
1056 function_declaration,
1061 ExprKind::Block(ref block, ref opt_label) => {
1062 walk_list!(visitor, visit_label, opt_label);
1063 visitor.visit_block(block);
1065 ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
1066 visitor.visit_expr(right_hand_expression);
1067 visitor.visit_expr(left_hand_expression)
1069 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
1070 visitor.visit_expr(right_expression);
1071 visitor.visit_expr(left_expression)
1073 ExprKind::Field(ref subexpression, ident) => {
1074 visitor.visit_expr(subexpression);
1075 visitor.visit_ident(ident);
1077 ExprKind::Index(ref main_expression, ref index_expression) => {
1078 visitor.visit_expr(main_expression);
1079 visitor.visit_expr(index_expression)
1081 ExprKind::Path(ref qpath) => {
1082 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1084 ExprKind::Break(ref destination, ref opt_expr) => {
1085 if let Some(ref label) = destination.label {
1086 visitor.visit_label(label);
1087 if let Ok(node_id) = destination.target_id {
1088 visitor.visit_def_mention(Def::Label(node_id))
1091 walk_list!(visitor, visit_expr, opt_expr);
1093 ExprKind::Continue(ref destination) => {
1094 if let Some(ref label) = destination.label {
1095 visitor.visit_label(label);
1096 if let Ok(node_id) = destination.target_id {
1097 visitor.visit_def_mention(Def::Label(node_id))
1101 ExprKind::Ret(ref optional_expression) => {
1102 walk_list!(visitor, visit_expr, optional_expression);
1104 ExprKind::InlineAsm(_, ref outputs, ref inputs) => {
1105 for expr in outputs.iter().chain(inputs.iter()) {
1106 visitor.visit_expr(expr)
1109 ExprKind::Yield(ref subexpression) => {
1110 visitor.visit_expr(subexpression);
1115 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
1116 walk_list!(visitor, visit_pat, &arm.pats);
1117 if let Some(ref g) = arm.guard {
1119 Guard::If(ref e) => visitor.visit_expr(e),
1122 visitor.visit_expr(&arm.body);
1123 walk_list!(visitor, visit_attribute, &arm.attrs);
1126 pub fn walk_vis<'v, V: Visitor<'v>>(visitor: &mut V, vis: &'v Visibility) {
1127 if let VisibilityKind::Restricted { ref path, id, hir_id } = vis.node {
1128 visitor.visit_id(id);
1129 visitor.visit_path(path, hir_id)
1133 pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssociatedItemKind) {
1134 // No visitable content here: this fn exists so you can call it if
1135 // the right thing to do, should content be added in the future,
1136 // would be to walk it.
1139 pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) {
1140 // No visitable content here: this fn exists so you can call it if
1141 // the right thing to do, should content be added in the future,
1142 // would be to walk it.
1145 #[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug)]
1146 pub struct IdRange {
1152 pub fn max() -> IdRange {
1155 max: NodeId::from_u32(0),
1159 pub fn empty(&self) -> bool {
1160 self.min >= self.max
1163 pub fn contains(&self, id: NodeId) -> bool {
1164 id >= self.min && id < self.max
1167 pub fn add(&mut self, id: NodeId) {
1168 self.min = cmp::min(self.min, id);
1169 self.max = cmp::max(self.max, NodeId::from_u32(id.as_u32() + 1));
1174 pub struct IdRangeComputingVisitor<'a, 'hir: 'a> {
1176 map: &'a map::Map<'hir>,
1179 impl<'a, 'hir> IdRangeComputingVisitor<'a, 'hir> {
1180 pub fn new(map: &'a map::Map<'hir>) -> IdRangeComputingVisitor<'a, 'hir> {
1181 IdRangeComputingVisitor { result: IdRange::max(), map: map }
1184 pub fn result(&self) -> IdRange {
1189 impl<'a, 'hir> Visitor<'hir> for IdRangeComputingVisitor<'a, 'hir> {
1190 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
1191 NestedVisitorMap::OnlyBodies(&self.map)
1194 fn visit_id(&mut self, id: NodeId) {
1195 self.result.add(id);