]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_system/src/dep_graph/mod.rs
Use a dedicated DepKind for the forever-red node.
[rust.git] / compiler / rustc_query_system / src / dep_graph / mod.rs
1 pub mod debug;
2 mod dep_node;
3 mod graph;
4 mod query;
5 mod serialized;
6
7 pub use dep_node::{DepNode, DepNodeParams, WorkProductId};
8 pub use graph::{
9     hash_result, DepGraph, DepNodeColor, DepNodeIndex, TaskDeps, TaskDepsRef, WorkProduct,
10 };
11 pub use query::DepGraphQuery;
12 pub use serialized::{SerializedDepGraph, SerializedDepNodeIndex};
13
14 use crate::ich::StableHashingContext;
15 use rustc_data_structures::profiling::SelfProfilerRef;
16 use rustc_serialize::{opaque::FileEncoder, Encodable};
17 use rustc_session::Session;
18
19 use std::fmt;
20 use std::hash::Hash;
21
22 pub trait DepContext: Copy {
23     type DepKind: self::DepKind;
24
25     /// Create a hashing context for hashing new results.
26     fn with_stable_hashing_context<R>(&self, f: impl FnOnce(StableHashingContext<'_>) -> R) -> R;
27
28     /// Access the DepGraph.
29     fn dep_graph(&self) -> &DepGraph<Self::DepKind>;
30
31     /// Access the profiler.
32     fn profiler(&self) -> &SelfProfilerRef;
33
34     /// Access the compiler session.
35     fn sess(&self) -> &Session;
36
37     /// Return whether this kind always require evaluation.
38     fn is_eval_always(&self, kind: Self::DepKind) -> bool;
39
40     fn fingerprint_style(&self, kind: Self::DepKind) -> FingerprintStyle;
41
42     /// Try to force a dep node to execute and see if it's green.
43     fn try_force_from_dep_node(&self, dep_node: DepNode<Self::DepKind>) -> bool;
44
45     /// Load data from the on-disk cache.
46     fn try_load_from_on_disk_cache(&self, dep_node: DepNode<Self::DepKind>);
47 }
48
49 pub trait HasDepContext: Copy {
50     type DepKind: self::DepKind;
51     type DepContext: self::DepContext<DepKind = Self::DepKind>;
52
53     fn dep_context(&self) -> &Self::DepContext;
54 }
55
56 impl<T: DepContext> HasDepContext for T {
57     type DepKind = T::DepKind;
58     type DepContext = Self;
59
60     fn dep_context(&self) -> &Self::DepContext {
61         self
62     }
63 }
64
65 /// Describes the contents of the fingerprint generated by a given query.
66 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
67 pub enum FingerprintStyle {
68     /// The fingerprint is actually a DefPathHash.
69     DefPathHash,
70     /// Query key was `()` or equivalent, so fingerprint is just zero.
71     Unit,
72     /// Some opaque hash.
73     Opaque,
74 }
75
76 impl FingerprintStyle {
77     #[inline]
78     pub fn reconstructible(self) -> bool {
79         match self {
80             FingerprintStyle::DefPathHash | FingerprintStyle::Unit => true,
81             FingerprintStyle::Opaque => false,
82         }
83     }
84 }
85
86 /// Describe the different families of dependency nodes.
87 pub trait DepKind: Copy + fmt::Debug + Eq + Hash + Send + Encodable<FileEncoder> + 'static {
88     /// DepKind to use when incr. comp. is turned off.
89     const NULL: Self;
90
91     /// DepKind to use to create the initial forever-red node.
92     const RED: Self;
93
94     /// Implementation of `std::fmt::Debug` for `DepNode`.
95     fn debug_node(node: &DepNode<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result;
96
97     /// Execute the operation with provided dependencies.
98     fn with_deps<OP, R>(deps: TaskDepsRef<'_, Self>, op: OP) -> R
99     where
100         OP: FnOnce() -> R;
101
102     /// Access dependencies from current implicit context.
103     fn read_deps<OP>(op: OP)
104     where
105         OP: for<'a> FnOnce(TaskDepsRef<'a, Self>);
106 }