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.
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 use hir::def_id::CrateNum;
15 macro_rules! try_opt {
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`.
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.
43 // Represents the HIR node with the given node-id
46 // Represents the body of a function or method. The def-id is that of the
50 // Represents the metadata for a given HIR node, typically found
51 // in an extern crate.
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>),
58 // Represents different phases in the compiler.
62 CoherenceCheckTrait(D),
63 CoherenceCheckImpl(D),
64 CoherenceOverlapCheck(D),
65 CoherenceOverlapCheckSpecial(D),
67 PrivacyAccessLevels(CrateNum),
69 // Represents the MIR for a fn; also used as the task node for
70 // things read/modify that MIR.
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`).
92 TypeParamPredicates((D, D)),
96 AssociatedItemDefIds(D),
104 // The set of impls for a given trait. Ultimately, it would be
105 // nice to get more fine-grained here (e.g., to include a
106 // simplified type), but we can't do that until we restructure the
107 // HIR to distinguish the *header* of an impl from its body. This
108 // is because changes to the header may change the self-type of
109 // the impl and hence would require us to be more conservative
110 // than changes in the impl body.
113 // Nodes representing caches. To properly handle a true cache, we
114 // don't use a DepTrackingMap, but rather we push a task node.
115 // Otherwise the write into the map would be incorrectly
116 // attributed to the first task that happened to fill the cache,
117 // which would yield an overly conservative dep-graph.
121 // Trait selection cache is a little funny. Given a trait
122 // reference like `Foo: SomeTrait<Bar>`, there could be
123 // arbitrarily many def-ids to map on in there (e.g., `Foo`,
124 // `SomeTrait`, `Bar`). We could have a vector of them, but it
125 // requires heap-allocation, and trait sel in general can be a
126 // surprisingly hot path. So instead we pick two def-ids: the
127 // trait def-id, and the first def-id in the input types. If there
128 // is no def-id in the input types, then we use the trait def-id
129 // again. So for example:
131 // - `i32: Clone` -> `TraitSelect { trait_def_id: Clone, self_def_id: Clone }`
132 // - `u32: Clone` -> `TraitSelect { trait_def_id: Clone, self_def_id: Clone }`
133 // - `Clone: Clone` -> `TraitSelect { trait_def_id: Clone, self_def_id: Clone }`
134 // - `Vec<i32>: Clone` -> `TraitSelect { trait_def_id: Clone, self_def_id: Vec }`
135 // - `String: Clone` -> `TraitSelect { trait_def_id: Clone, self_def_id: String }`
136 // - `Foo: Trait<Bar>` -> `TraitSelect { trait_def_id: Trait, self_def_id: Foo }`
137 // - `Foo: Trait<i32>` -> `TraitSelect { trait_def_id: Trait, self_def_id: Foo }`
138 // - `(Foo, Bar): Trait` -> `TraitSelect { trait_def_id: Trait, self_def_id: Foo }`
139 // - `i32: Trait<Foo>` -> `TraitSelect { trait_def_id: Trait, self_def_id: Foo }`
141 // You can see that we map many trait refs to the same
142 // trait-select node. This is not a problem, it just means
143 // imprecision in our dep-graph tracking. The important thing is
144 // that for any given trait-ref, we always map to the **same**
145 // trait-select node.
146 TraitSelect { trait_def_id: D, input_def_id: D },
148 // For proj. cache, we just keep a list of all def-ids, since it is
150 ProjectionCache { def_ids: Vec<D> },
156 impl<D: Clone + Debug> DepNode<D> {
158 pub fn from_label_string(label: &str, data: D) -> Result<DepNode<D>, ()> {
160 ($($name:ident,)*) => {
162 $(stringify!($name) => Ok(DepNode::$name(data)),)*
168 if label == "Krate" {
170 return Ok(DepNode::Krate);
181 AssociatedItemDefIds,
190 pub fn map_def<E, OP>(&self, mut op: OP) -> Option<DepNode<E>>
191 where OP: FnMut(&D) -> Option<E>, E: Clone + Debug
193 use self::DepNode::*;
196 Krate => Some(Krate),
197 BorrowCheckKrate => Some(BorrowCheckKrate),
198 MirKrate => Some(MirKrate),
199 TypeckBodiesKrate => Some(TypeckBodiesKrate),
200 Coherence => Some(Coherence),
201 Resolve => Some(Resolve),
202 Variance => Some(Variance),
203 PrivacyAccessLevels(k) => Some(PrivacyAccessLevels(k)),
204 Reachability => Some(Reachability),
205 LateLintCheck => Some(LateLintCheck),
206 TransWriteMetadata => Some(TransWriteMetadata),
208 // work product names do not need to be mapped, because
209 // they are always absolute.
210 WorkProduct(ref id) => Some(WorkProduct(id.clone())),
212 Hir(ref d) => op(d).map(Hir),
213 HirBody(ref d) => op(d).map(HirBody),
214 MetaData(ref d) => op(d).map(MetaData),
215 CoherenceCheckTrait(ref d) => op(d).map(CoherenceCheckTrait),
216 CoherenceCheckImpl(ref d) => op(d).map(CoherenceCheckImpl),
217 CoherenceOverlapCheck(ref d) => op(d).map(CoherenceOverlapCheck),
218 CoherenceOverlapCheckSpecial(ref d) => op(d).map(CoherenceOverlapCheckSpecial),
219 Mir(ref d) => op(d).map(Mir),
220 MirShim(ref def_ids) => {
221 let def_ids: Option<Vec<E>> = def_ids.iter().map(op).collect();
224 BorrowCheck(ref d) => op(d).map(BorrowCheck),
225 RegionMaps(ref d) => op(d).map(RegionMaps),
226 RvalueCheck(ref d) => op(d).map(RvalueCheck),
227 TransCrateItem(ref d) => op(d).map(TransCrateItem),
228 TransInlinedItem(ref d) => op(d).map(TransInlinedItem),
229 AssociatedItems(ref d) => op(d).map(AssociatedItems),
230 ItemSignature(ref d) => op(d).map(ItemSignature),
231 IsForeignItem(ref d) => op(d).map(IsForeignItem),
232 TypeParamPredicates((ref item, ref param)) => {
233 Some(TypeParamPredicates((try_opt!(op(item)), try_opt!(op(param)))))
235 SizedConstraint(ref d) => op(d).map(SizedConstraint),
236 DtorckConstraint(ref d) => op(d).map(DtorckConstraint),
237 AdtDestructor(ref d) => op(d).map(AdtDestructor),
238 AssociatedItemDefIds(ref d) => op(d).map(AssociatedItemDefIds),
239 InherentImpls(ref d) => op(d).map(InherentImpls),
240 TypeckTables(ref d) => op(d).map(TypeckTables),
241 UsedTraitImports(ref d) => op(d).map(UsedTraitImports),
242 ConstEval(ref d) => op(d).map(ConstEval),
243 SymbolName(ref d) => op(d).map(SymbolName),
244 TraitImpls(ref d) => op(d).map(TraitImpls),
245 TraitItems(ref d) => op(d).map(TraitItems),
246 ReprHints(ref d) => op(d).map(ReprHints),
247 TraitSelect { ref trait_def_id, ref input_def_id } => {
248 op(trait_def_id).and_then(|trait_def_id| {
249 op(input_def_id).and_then(|input_def_id| {
250 Some(TraitSelect { trait_def_id: trait_def_id,
251 input_def_id: input_def_id })
255 ProjectionCache { ref def_ids } => {
256 let def_ids: Option<Vec<E>> = def_ids.iter().map(op).collect();
257 def_ids.map(|d| ProjectionCache { def_ids: d })
259 DescribeDef(ref d) => op(d).map(DescribeDef),
260 DefSpan(ref d) => op(d).map(DefSpan),
265 /// A "work product" corresponds to a `.o` (or other) file that we
266 /// save in between runs. These ids do not have a DefId but rather
267 /// some independent path or string that persists between runs without
268 /// the need to be mapped or unmapped. (This ensures we can serialize
269 /// them even in the absence of a tcx.)
270 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
271 pub struct WorkProductId(pub String);