]> git.lizzy.rs Git - rust.git/blob - src/librustc/dep_graph/dep_node.rs
Auto merge of #41258 - clarcharr:str_box_extras, r=Kimundi
[rust.git] / src / librustc / dep_graph / dep_node.rs
1 // Copyright 2014 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.
4 //
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.
10
11 use hir::def_id::CrateNum;
12 use std::fmt::Debug;
13 use std::sync::Arc;
14
15 macro_rules! try_opt {
16     ($e:expr) => (
17         match $e {
18             Some(r) => r,
19             None => return None,
20         }
21     )
22 }
23
24 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
25 pub enum DepNode<D: Clone + Debug> {
26     // The `D` type is "how definitions are identified".
27     // During compilation, it is always `DefId`, but when serializing
28     // it is mapped to `DefPath`.
29
30     // Represents the `Krate` as a whole (the `hir::Krate` value) (as
31     // distinct from the krate module). This is basically a hash of
32     // the entire krate, so if you read from `Krate` (e.g., by calling
33     // `tcx.hir.krate()`), we will have to assume that any change
34     // means that you need to be recompiled. This is because the
35     // `Krate` value gives you access to all other items. To avoid
36     // this fate, do not call `tcx.hir.krate()`; instead, prefer
37     // wrappers like `tcx.visit_all_items_in_krate()`.  If there is no
38     // suitable wrapper, you can use `tcx.dep_graph.ignore()` to gain
39     // access to the krate, but you must remember to add suitable
40     // edges yourself for the individual items that you read.
41     Krate,
42
43     // Represents the HIR node with the given node-id
44     Hir(D),
45
46     // Represents the body of a function or method. The def-id is that of the
47     // function/method.
48     HirBody(D),
49
50     // Represents the metadata for a given HIR node, typically found
51     // in an extern crate.
52     MetaData(D),
53
54     // Represents some artifact that we save to disk. Note that these
55     // do not have a def-id as part of their identifier.
56     WorkProduct(Arc<WorkProductId>),
57
58     // Represents different phases in the compiler.
59     RegionResolveCrate,
60     Coherence,
61     Resolve,
62     CoherenceCheckTrait(D),
63     CoherenceCheckImpl(D),
64     CoherenceOverlapCheck(D),
65     CoherenceOverlapCheckSpecial(D),
66     Variance,
67     PrivacyAccessLevels(CrateNum),
68
69     // Represents the MIR for a fn; also used as the task node for
70     // things read/modify that MIR.
71     MirKrate,
72     Mir(D),
73     MirShim(Vec<D>),
74
75     BorrowCheckKrate,
76     BorrowCheck(D),
77     RvalueCheck(D),
78     Reachability,
79     LateLintCheck,
80     TransCrateItem(D),
81     TransInlinedItem(D),
82     TransWriteMetadata,
83
84     // Nodes representing bits of computed IR in the tcx. Each shared
85     // table in the tcx (or elsewhere) maps to one of these
86     // nodes. Often we map multiple tables to the same node if there
87     // is no point in distinguishing them (e.g., both the type and
88     // predicates for an item wind up in `ItemSignature`).
89     AssociatedItems(D),
90     ItemSignature(D),
91     IsForeignItem(D),
92     TypeParamPredicates((D, D)),
93     SizedConstraint(D),
94     DtorckConstraint(D),
95     AdtDestructor(D),
96     AssociatedItemDefIds(D),
97     InherentImpls(D),
98     TypeckBodiesKrate,
99     TypeckTables(D),
100     UsedTraitImports(D),
101     ConstEval(D),
102
103     // The set of impls for a given trait. Ultimately, it would be
104     // nice to get more fine-grained here (e.g., to include a
105     // simplified type), but we can't do that until we restructure the
106     // HIR to distinguish the *header* of an impl from its body.  This
107     // is because changes to the header may change the self-type of
108     // the impl and hence would require us to be more conservative
109     // than changes in the impl body.
110     TraitImpls(D),
111
112     // Nodes representing caches. To properly handle a true cache, we
113     // don't use a DepTrackingMap, but rather we push a task node.
114     // Otherwise the write into the map would be incorrectly
115     // attributed to the first task that happened to fill the cache,
116     // which would yield an overly conservative dep-graph.
117     TraitItems(D),
118     ReprHints(D),
119
120     // Trait selection cache is a little funny. Given a trait
121     // reference like `Foo: SomeTrait<Bar>`, there could be
122     // arbitrarily many def-ids to map on in there (e.g., `Foo`,
123     // `SomeTrait`, `Bar`). We could have a vector of them, but it
124     // requires heap-allocation, and trait sel in general can be a
125     // surprisingly hot path. So instead we pick two def-ids: the
126     // trait def-id, and the first def-id in the input types. If there
127     // is no def-id in the input types, then we use the trait def-id
128     // again. So for example:
129     //
130     // - `i32: Clone` -> `TraitSelect { trait_def_id: Clone, self_def_id: Clone }`
131     // - `u32: Clone` -> `TraitSelect { trait_def_id: Clone, self_def_id: Clone }`
132     // - `Clone: Clone` -> `TraitSelect { trait_def_id: Clone, self_def_id: Clone }`
133     // - `Vec<i32>: Clone` -> `TraitSelect { trait_def_id: Clone, self_def_id: Vec }`
134     // - `String: Clone` -> `TraitSelect { trait_def_id: Clone, self_def_id: String }`
135     // - `Foo: Trait<Bar>` -> `TraitSelect { trait_def_id: Trait, self_def_id: Foo }`
136     // - `Foo: Trait<i32>` -> `TraitSelect { trait_def_id: Trait, self_def_id: Foo }`
137     // - `(Foo, Bar): Trait` -> `TraitSelect { trait_def_id: Trait, self_def_id: Foo }`
138     // - `i32: Trait<Foo>` -> `TraitSelect { trait_def_id: Trait, self_def_id: Foo }`
139     //
140     // You can see that we map many trait refs to the same
141     // trait-select node.  This is not a problem, it just means
142     // imprecision in our dep-graph tracking.  The important thing is
143     // that for any given trait-ref, we always map to the **same**
144     // trait-select node.
145     TraitSelect { trait_def_id: D, input_def_id: D },
146
147     // For proj. cache, we just keep a list of all def-ids, since it is
148     // not a hotspot.
149     ProjectionCache { def_ids: Vec<D> },
150 }
151
152 impl<D: Clone + Debug> DepNode<D> {
153     /// Used in testing
154     pub fn from_label_string(label: &str, data: D) -> Result<DepNode<D>, ()> {
155         macro_rules! check {
156             ($($name:ident,)*) => {
157                 match label {
158                     $(stringify!($name) => Ok(DepNode::$name(data)),)*
159                     _ => Err(())
160                 }
161             }
162         }
163
164         if label == "Krate" {
165             // special case
166             return Ok(DepNode::Krate);
167         }
168
169         check! {
170             BorrowCheck,
171             Hir,
172             HirBody,
173             TransCrateItem,
174             AssociatedItems,
175             ItemSignature,
176             IsForeignItem,
177             AssociatedItemDefIds,
178             InherentImpls,
179             TypeckTables,
180             UsedTraitImports,
181             TraitImpls,
182             ReprHints,
183         }
184     }
185
186     pub fn map_def<E, OP>(&self, mut op: OP) -> Option<DepNode<E>>
187         where OP: FnMut(&D) -> Option<E>, E: Clone + Debug
188     {
189         use self::DepNode::*;
190
191         match *self {
192             Krate => Some(Krate),
193             BorrowCheckKrate => Some(BorrowCheckKrate),
194             MirKrate => Some(MirKrate),
195             TypeckBodiesKrate => Some(TypeckBodiesKrate),
196             RegionResolveCrate => Some(RegionResolveCrate),
197             Coherence => Some(Coherence),
198             Resolve => Some(Resolve),
199             Variance => Some(Variance),
200             PrivacyAccessLevels(k) => Some(PrivacyAccessLevels(k)),
201             Reachability => Some(Reachability),
202             LateLintCheck => Some(LateLintCheck),
203             TransWriteMetadata => Some(TransWriteMetadata),
204
205             // work product names do not need to be mapped, because
206             // they are always absolute.
207             WorkProduct(ref id) => Some(WorkProduct(id.clone())),
208
209             Hir(ref d) => op(d).map(Hir),
210             HirBody(ref d) => op(d).map(HirBody),
211             MetaData(ref d) => op(d).map(MetaData),
212             CoherenceCheckTrait(ref d) => op(d).map(CoherenceCheckTrait),
213             CoherenceCheckImpl(ref d) => op(d).map(CoherenceCheckImpl),
214             CoherenceOverlapCheck(ref d) => op(d).map(CoherenceOverlapCheck),
215             CoherenceOverlapCheckSpecial(ref d) => op(d).map(CoherenceOverlapCheckSpecial),
216             Mir(ref d) => op(d).map(Mir),
217             MirShim(ref def_ids) => {
218                 let def_ids: Option<Vec<E>> = def_ids.iter().map(op).collect();
219                 def_ids.map(MirShim)
220             }
221             BorrowCheck(ref d) => op(d).map(BorrowCheck),
222             RvalueCheck(ref d) => op(d).map(RvalueCheck),
223             TransCrateItem(ref d) => op(d).map(TransCrateItem),
224             TransInlinedItem(ref d) => op(d).map(TransInlinedItem),
225             AssociatedItems(ref d) => op(d).map(AssociatedItems),
226             ItemSignature(ref d) => op(d).map(ItemSignature),
227             IsForeignItem(ref d) => op(d).map(IsForeignItem),
228             TypeParamPredicates((ref item, ref param)) => {
229                 Some(TypeParamPredicates((try_opt!(op(item)), try_opt!(op(param)))))
230             }
231             SizedConstraint(ref d) => op(d).map(SizedConstraint),
232             DtorckConstraint(ref d) => op(d).map(DtorckConstraint),
233             AdtDestructor(ref d) => op(d).map(AdtDestructor),
234             AssociatedItemDefIds(ref d) => op(d).map(AssociatedItemDefIds),
235             InherentImpls(ref d) => op(d).map(InherentImpls),
236             TypeckTables(ref d) => op(d).map(TypeckTables),
237             UsedTraitImports(ref d) => op(d).map(UsedTraitImports),
238             ConstEval(ref d) => op(d).map(ConstEval),
239             TraitImpls(ref d) => op(d).map(TraitImpls),
240             TraitItems(ref d) => op(d).map(TraitItems),
241             ReprHints(ref d) => op(d).map(ReprHints),
242             TraitSelect { ref trait_def_id, ref input_def_id } => {
243                 op(trait_def_id).and_then(|trait_def_id| {
244                     op(input_def_id).and_then(|input_def_id| {
245                         Some(TraitSelect { trait_def_id: trait_def_id,
246                                            input_def_id: input_def_id })
247                     })
248                 })
249             }
250             ProjectionCache { ref def_ids } => {
251                 let def_ids: Option<Vec<E>> = def_ids.iter().map(op).collect();
252                 def_ids.map(|d| ProjectionCache { def_ids: d })
253             }
254         }
255     }
256 }
257
258 /// A "work product" corresponds to a `.o` (or other) file that we
259 /// save in between runs. These ids do not have a DefId but rather
260 /// some independent path or string that persists between runs without
261 /// the need to be mapped or unmapped. (This ensures we can serialize
262 /// them even in the absence of a tcx.)
263 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
264 pub struct WorkProductId(pub String);