]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/dep_graph/mod.rs
Auto merge of #82183 - michaelwoerister:lazier-defpathhash-loading2, r=wesleywiser
[rust.git] / compiler / rustc_middle / src / dep_graph / mod.rs
1 use crate::ich::StableHashingContext;
2 use crate::ty::{self, TyCtxt};
3 use rustc_data_structures::profiling::SelfProfilerRef;
4 use rustc_data_structures::sync::Lock;
5 use rustc_session::Session;
6
7 #[macro_use]
8 mod dep_node;
9
10 pub use rustc_query_system::dep_graph::{
11     debug::DepNodeFilter, hash_result, DepContext, DepNodeColor, DepNodeIndex,
12     SerializedDepNodeIndex, WorkProduct, WorkProductId,
13 };
14
15 pub use dep_node::{label_strs, DepKind, DepNode, DepNodeExt};
16 crate use dep_node::{make_compile_codegen_unit, make_compile_mono_item};
17
18 pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepKind>;
19 pub type TaskDeps = rustc_query_system::dep_graph::TaskDeps<DepKind>;
20 pub type DepGraphQuery = rustc_query_system::dep_graph::DepGraphQuery<DepKind>;
21 pub type SerializedDepGraph = rustc_query_system::dep_graph::SerializedDepGraph<DepKind>;
22 pub type EdgeFilter = rustc_query_system::dep_graph::debug::EdgeFilter<DepKind>;
23
24 impl rustc_query_system::dep_graph::DepKind for DepKind {
25     const NULL: Self = DepKind::Null;
26
27     #[inline(always)]
28     fn can_reconstruct_query_key(&self) -> bool {
29         DepKind::can_reconstruct_query_key(self)
30     }
31
32     #[inline(always)]
33     fn is_eval_always(&self) -> bool {
34         self.is_eval_always
35     }
36
37     #[inline(always)]
38     fn has_params(&self) -> bool {
39         self.has_params
40     }
41
42     fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43         write!(f, "{:?}", node.kind)?;
44
45         if !node.kind.has_params && !node.kind.is_anon {
46             return Ok(());
47         }
48
49         write!(f, "(")?;
50
51         ty::tls::with_opt(|opt_tcx| {
52             if let Some(tcx) = opt_tcx {
53                 if let Some(def_id) = node.extract_def_id(tcx) {
54                     write!(f, "{}", tcx.def_path_debug_str(def_id))?;
55                 } else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*node) {
56                     write!(f, "{}", s)?;
57                 } else {
58                     write!(f, "{}", node.hash)?;
59                 }
60             } else {
61                 write!(f, "{}", node.hash)?;
62             }
63             Ok(())
64         })?;
65
66         write!(f, ")")
67     }
68
69     fn with_deps<OP, R>(task_deps: Option<&Lock<TaskDeps>>, op: OP) -> R
70     where
71         OP: FnOnce() -> R,
72     {
73         ty::tls::with_context(|icx| {
74             let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
75
76             ty::tls::enter_context(&icx, |_| op())
77         })
78     }
79
80     fn read_deps<OP>(op: OP)
81     where
82         OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps>>),
83     {
84         ty::tls::with_context_opt(|icx| {
85             let icx = if let Some(icx) = icx { icx } else { return };
86             op(icx.task_deps)
87         })
88     }
89 }
90
91 impl<'tcx> DepContext for TyCtxt<'tcx> {
92     type DepKind = DepKind;
93     type StableHashingContext = StableHashingContext<'tcx>;
94
95     #[inline]
96     fn create_stable_hashing_context(&self) -> Self::StableHashingContext {
97         TyCtxt::create_stable_hashing_context(*self)
98     }
99
100     #[inline]
101     fn dep_graph(&self) -> &DepGraph {
102         &self.dep_graph
103     }
104
105     #[inline(always)]
106     fn profiler(&self) -> &SelfProfilerRef {
107         &self.prof
108     }
109
110     #[inline(always)]
111     fn sess(&self) -> &Session {
112         self.sess
113     }
114 }