]> git.lizzy.rs Git - rust.git/blob - src/librustc_query_system/dep_graph/dep_node.rs
Make librustc_query_system compile.
[rust.git] / src / librustc_query_system / dep_graph / dep_node.rs
1 //! This module defines the `DepNode` type which the compiler uses to represent
2 //! nodes in the dependency graph. A `DepNode` consists of a `DepKind` (which
3 //! specifies the kind of thing it represents, like a piece of HIR, MIR, etc)
4 //! and a `Fingerprint`, a 128 bit hash value the exact meaning of which
5 //! depends on the node's `DepKind`. Together, the kind and the fingerprint
6 //! fully identify a dependency node, even across multiple compilation sessions.
7 //! In other words, the value of the fingerprint does not depend on anything
8 //! that is specific to a given compilation session, like an unpredictable
9 //! interning key (e.g., NodeId, DefId, Symbol) or the numeric value of a
10 //! pointer. The concept behind this could be compared to how git commit hashes
11 //! uniquely identify a given commit and has a few advantages:
12 //!
13 //! * A `DepNode` can simply be serialized to disk and loaded in another session
14 //!   without the need to do any "rebasing (like we have to do for Spans and
15 //!   NodeIds) or "retracing" like we had to do for `DefId` in earlier
16 //!   implementations of the dependency graph.
17 //! * A `Fingerprint` is just a bunch of bits, which allows `DepNode` to
18 //!   implement `Copy`, `Sync`, `Send`, `Freeze`, etc.
19 //! * Since we just have a bit pattern, `DepNode` can be mapped from disk into
20 //!   memory without any post-processing (e.g., "abomination-style" pointer
21 //!   reconstruction).
22 //! * Because a `DepNode` is self-contained, we can instantiate `DepNodes` that
23 //!   refer to things that do not exist anymore. In previous implementations
24 //!   `DepNode` contained a `DefId`. A `DepNode` referring to something that
25 //!   had been removed between the previous and the current compilation session
26 //!   could not be instantiated because the current compilation session
27 //!   contained no `DefId` for thing that had been removed.
28 //!
29 //! `DepNode` definition happens in `librustc` with the `define_dep_nodes!()` macro.
30 //! This macro defines the `DepKind` enum and a corresponding `DepConstructor` enum. The
31 //! `DepConstructor` enum links a `DepKind` to the parameters that are needed at runtime in order
32 //! to construct a valid `DepNode` fingerprint.
33 //!
34 //! Because the macro sees what parameters a given `DepKind` requires, it can
35 //! "infer" some properties for each kind of `DepNode`:
36 //!
37 //! * Whether a `DepNode` of a given kind has any parameters at all. Some
38 //!   `DepNode`s could represent global concepts with only one value.
39 //! * Whether it is possible, in principle, to reconstruct a query key from a
40 //!   given `DepNode`. Many `DepKind`s only require a single `DefId` parameter,
41 //!   in which case it is possible to map the node's fingerprint back to the
42 //!   `DefId` it was computed from. In other cases, too much information gets
43 //!   lost during fingerprint computation.
44
45 use super::{DepContext, DepKind};
46
47 use rustc_data_structures::fingerprint::Fingerprint;
48 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
49 use rustc_macros::HashStable_Generic;
50
51 use std::fmt;
52 use std::hash::Hash;
53
54 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
55 pub struct DepNode<K> {
56     pub kind: K,
57     pub hash: Fingerprint,
58 }
59
60 impl<K: DepKind> DepNode<K> {
61     /// Creates a new, parameterless DepNode. This method will assert
62     /// that the DepNode corresponding to the given DepKind actually
63     /// does not require any parameters.
64     pub fn new_no_params(kind: K) -> DepNode<K> {
65         debug_assert!(!kind.has_params());
66         DepNode { kind, hash: Fingerprint::ZERO }
67     }
68 }
69
70 impl<K: DepKind> fmt::Debug for DepNode<K> {
71     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72         K::debug_node(self, f)
73     }
74 }
75
76 pub trait DepNodeParams<Ctxt: DepContext>: fmt::Debug + Sized {
77     const CAN_RECONSTRUCT_QUERY_KEY: bool;
78
79     /// This method turns the parameters of a DepNodeConstructor into an opaque
80     /// Fingerprint to be used in DepNode.
81     /// Not all DepNodeParams support being turned into a Fingerprint (they
82     /// don't need to if the corresponding DepNode is anonymous).
83     fn to_fingerprint(&self, _: Ctxt) -> Fingerprint {
84         panic!("Not implemented. Accidentally called on anonymous node?")
85     }
86
87     fn to_debug_str(&self, _: Ctxt) -> String {
88         format!("{:?}", self)
89     }
90
91     /// This method tries to recover the query key from the given `DepNode`,
92     /// something which is needed when forcing `DepNode`s during red-green
93     /// evaluation. The query system will only call this method if
94     /// `CAN_RECONSTRUCT_QUERY_KEY` is `true`.
95     /// It is always valid to return `None` here, in which case incremental
96     /// compilation will treat the query as having changed instead of forcing it.
97     fn recover(tcx: Ctxt, dep_node: &DepNode<Ctxt::DepKind>) -> Option<Self>;
98 }
99
100 impl<Ctxt: DepContext, T> DepNodeParams<Ctxt> for T
101 where
102     T: HashStable<Ctxt::StableHashingContext> + fmt::Debug,
103 {
104     default const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
105
106     default fn to_fingerprint(&self, tcx: Ctxt) -> Fingerprint {
107         let mut hcx = tcx.create_stable_hashing_context();
108         let mut hasher = StableHasher::new();
109
110         self.hash_stable(&mut hcx, &mut hasher);
111
112         hasher.finish()
113     }
114
115     default fn to_debug_str(&self, _: Ctxt) -> String {
116         format!("{:?}", *self)
117     }
118
119     default fn recover(_: Ctxt, _: &DepNode<Ctxt::DepKind>) -> Option<Self> {
120         None
121     }
122 }
123
124 /// A "work product" corresponds to a `.o` (or other) file that we
125 /// save in between runs. These IDs do not have a `DefId` but rather
126 /// some independent path or string that persists between runs without
127 /// the need to be mapped or unmapped. (This ensures we can serialize
128 /// them even in the absence of a tcx.)
129 #[derive(
130     Clone,
131     Copy,
132     Debug,
133     PartialEq,
134     Eq,
135     PartialOrd,
136     Ord,
137     Hash,
138     RustcEncodable,
139     RustcDecodable,
140     HashStable_Generic
141 )]
142 pub struct WorkProductId {
143     hash: Fingerprint,
144 }
145
146 impl WorkProductId {
147     pub fn from_cgu_name(cgu_name: &str) -> WorkProductId {
148         let mut hasher = StableHasher::new();
149         cgu_name.len().hash(&mut hasher);
150         cgu_name.hash(&mut hasher);
151         WorkProductId { hash: hasher.finish() }
152     }
153
154     pub fn from_fingerprint(fingerprint: Fingerprint) -> WorkProductId {
155         WorkProductId { hash: fingerprint }
156     }
157 }