1 use super::intravisit::Visitor;
2 use super::{ImplItem, Item, TraitItem};
4 /// The "item-like visitor" defines only the top-level methods
5 /// that can be invoked by `Crate::visit_all_item_likes()`. Whether
6 /// this trait is the right one to implement will depend on the
7 /// overall pattern you need. Here are the three available patterns,
8 /// in roughly the order of desirability:
10 /// 1. **Shallow visit**: Get a simple callback for every item (or item-like thing) in the HIR.
11 /// - Example: find all items with a `#[foo]` attribute on them.
12 /// - How: Implement `ItemLikeVisitor` and call `tcx.hir().krate().visit_all_item_likes()`.
13 /// - Pro: Efficient; just walks the lists of item-like things, not the nodes themselves.
14 /// - Con: Don't get information about nesting
15 /// - Con: Don't have methods for specific bits of HIR, like "on
16 /// every expr, do this".
17 /// 2. **Deep visit**: Want to scan for specific kinds of HIR nodes within
18 /// an item, but don't care about how item-like things are nested
19 /// within one another.
20 /// - Example: Examine each expression to look for its type and do some check or other.
21 /// - How: Implement `intravisit::Visitor` and override the `nested_visit_map()` method
22 /// to return `NestedVisitorMap::OnlyBodies` and use
23 /// `tcx.hir().krate().visit_all_item_likes(&mut visitor.as_deep_visitor())`. Within
24 /// your `intravisit::Visitor` impl, implement methods like `visit_expr()` (don't forget
25 /// to invoke `intravisit::walk_expr()` to keep walking the subparts).
26 /// - Pro: Visitor methods for any kind of HIR node, not just item-like things.
27 /// - Pro: Integrates well into dependency tracking.
28 /// - Con: Don't get information about nesting between items
29 /// 3. **Nested visit**: Want to visit the whole HIR and you care about the nesting between
31 /// - Example: Lifetime resolution, which wants to bring lifetimes declared on the
32 /// impl into scope while visiting the impl-items, and then back out again.
33 /// - How: Implement `intravisit::Visitor` and override the `nested_visit_map()` method
34 /// to return `NestedVisitorMap::All`. Walk your crate with `intravisit::walk_crate()`
35 /// invoked on `tcx.hir().krate()`.
36 /// - Pro: Visitor methods for any kind of HIR node, not just item-like things.
37 /// - Pro: Preserves nesting information
38 /// - Con: Does not integrate well into dependency tracking.
40 /// Note: the methods of `ItemLikeVisitor` intentionally have no
41 /// defaults, so that as we expand the list of item-like things, we
42 /// revisit the various visitors to see if they need to change. This
43 /// is harder to do with `intravisit::Visitor`, so when you add a new
44 /// `visit_nested_foo()` method, it is recommended that you search for
45 /// existing `fn visit_nested` methods to see where changes are
47 pub trait ItemLikeVisitor<'hir> {
48 fn visit_item(&mut self, item: &'hir Item<'hir>);
49 fn visit_trait_item(&mut self, trait_item: &'hir TraitItem<'hir>);
50 fn visit_impl_item(&mut self, impl_item: &'hir ImplItem<'hir>);
53 pub struct DeepVisitor<'v, V> {
57 impl<'v, 'hir, V> DeepVisitor<'v, V>
59 V: Visitor<'hir> + 'v,
61 pub fn new(base: &'v mut V) -> Self {
62 DeepVisitor { visitor: base }
66 impl<'v, 'hir, V> ItemLikeVisitor<'hir> for DeepVisitor<'v, V>
70 fn visit_item(&mut self, item: &'hir Item<'hir>) {
71 self.visitor.visit_item(item);
74 fn visit_trait_item(&mut self, trait_item: &'hir TraitItem<'hir>) {
75 self.visitor.visit_trait_item(trait_item);
78 fn visit_impl_item(&mut self, impl_item: &'hir ImplItem<'hir>) {
79 self.visitor.visit_impl_item(impl_item);
83 /// A parallel variant of `ItemLikeVisitor`.
84 pub trait ParItemLikeVisitor<'hir> {
85 fn visit_item(&self, item: &'hir Item<'hir>);
86 fn visit_trait_item(&self, trait_item: &'hir TraitItem<'hir>);
87 fn visit_impl_item(&self, impl_item: &'hir ImplItem<'hir>);
90 pub trait IntoVisitor<'hir> {
91 type Visitor: Visitor<'hir>;
92 fn into_visitor(&self) -> Self::Visitor;
95 pub struct ParDeepVisitor<V>(pub V);
97 impl<'hir, V> ParItemLikeVisitor<'hir> for ParDeepVisitor<V>
101 fn visit_item(&self, item: &'hir Item<'hir>) {
102 self.0.into_visitor().visit_item(item);
105 fn visit_trait_item(&self, trait_item: &'hir TraitItem<'hir>) {
106 self.0.into_visitor().visit_trait_item(trait_item);
109 fn visit_impl_item(&self, impl_item: &'hir ImplItem<'hir>) {
110 self.0.into_visitor().visit_impl_item(impl_item);