]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/itemlikevisit.rs
Rollup merge of #66094 - ArturKovacs:fix-count-doc, r=Dylan-DPC
[rust.git] / src / librustc / hir / itemlikevisit.rs
1 use super::{Item, ImplItem, TraitItem};
2 use super::intravisit::Visitor;
3
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:
9 ///
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
30 ///    item-like things.
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.
39 ///
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
46 /// needed.
47 pub trait ItemLikeVisitor<'hir> {
48     fn visit_item(&mut self, item: &'hir Item);
49     fn visit_trait_item(&mut self, trait_item: &'hir TraitItem);
50     fn visit_impl_item(&mut self, impl_item: &'hir ImplItem);
51 }
52
53 pub struct DeepVisitor<'v, V> {
54     visitor: &'v mut V,
55 }
56
57 impl<'v, 'hir, V> DeepVisitor<'v, V>
58     where V: Visitor<'hir> + 'v
59 {
60     pub fn new(base: &'v mut V) -> Self {
61         DeepVisitor { visitor: base }
62     }
63 }
64
65 impl<'v, 'hir, V> ItemLikeVisitor<'hir> for DeepVisitor<'v, V>
66     where V: Visitor<'hir>
67 {
68     fn visit_item(&mut self, item: &'hir Item) {
69         self.visitor.visit_item(item);
70     }
71
72     fn visit_trait_item(&mut self, trait_item: &'hir TraitItem) {
73         self.visitor.visit_trait_item(trait_item);
74     }
75
76     fn visit_impl_item(&mut self, impl_item: &'hir ImplItem) {
77         self.visitor.visit_impl_item(impl_item);
78     }
79 }
80
81 /// A parallel variant of `ItemLikeVisitor`.
82 pub trait ParItemLikeVisitor<'hir> {
83     fn visit_item(&self, item: &'hir Item);
84     fn visit_trait_item(&self, trait_item: &'hir TraitItem);
85     fn visit_impl_item(&self, impl_item: &'hir ImplItem);
86 }
87
88 pub trait IntoVisitor<'hir> {
89     type Visitor: Visitor<'hir>;
90     fn into_visitor(&self) -> Self::Visitor;
91 }
92
93 pub struct ParDeepVisitor<V>(pub V);
94
95 impl<'hir, V> ParItemLikeVisitor<'hir> for ParDeepVisitor<V>
96     where V: IntoVisitor<'hir>
97 {
98     fn visit_item(&self, item: &'hir Item) {
99         self.0.into_visitor().visit_item(item);
100     }
101
102     fn visit_trait_item(&self, trait_item: &'hir TraitItem) {
103         self.0.into_visitor().visit_trait_item(trait_item);
104     }
105
106     fn visit_impl_item(&self, impl_item: &'hir ImplItem) {
107         self.0.into_visitor().visit_impl_item(impl_item);
108     }
109 }