]> git.lizzy.rs Git - rust.git/blob - src/librustc_query_system/query/config.rs
7a13bbf22999d0fd78fc8135636c5f462e4c546c
[rust.git] / src / librustc_query_system / query / config.rs
1 //! Query configuration and description traits.
2
3 use crate::dep_graph::DepNode;
4 use crate::dep_graph::SerializedDepNodeIndex;
5 use crate::query::caches::QueryCache;
6 use crate::query::plumbing::CycleError;
7 use crate::query::{QueryContext, QueryState};
8 use rustc_data_structures::profiling::ProfileCategory;
9 use rustc_span::def_id::DefId;
10
11 use rustc_data_structures::fingerprint::Fingerprint;
12 use std::borrow::Cow;
13 use std::fmt::Debug;
14 use std::hash::Hash;
15
16 // The parameter `CTX` is required in librustc_middle:
17 // implementations may need to access the `'tcx` lifetime in `CTX = TyCtxt<'tcx>`.
18 pub trait QueryConfig<CTX> {
19     const NAME: &'static str;
20     const CATEGORY: ProfileCategory;
21
22     type Key: Eq + Hash + Clone + Debug;
23     type Value;
24     type Stored: Clone;
25 }
26
27 pub(crate) struct QueryVtable<CTX: QueryContext, K, V> {
28     pub eval_always: bool,
29
30     // Don't use this method to compute query results, instead use the methods on TyCtxt
31     pub compute: fn(CTX, K) -> V,
32
33     pub hash_result: fn(&mut CTX::StableHashingContext, &V) -> Option<Fingerprint>,
34     pub cache_on_disk: fn(CTX, &K, Option<&V>) -> bool,
35     pub try_load_from_disk: fn(CTX, SerializedDepNodeIndex) -> Option<V>,
36 }
37
38 impl<CTX: QueryContext, K, V> QueryVtable<CTX, K, V> {
39     pub(crate) fn compute(&self, tcx: CTX, key: K) -> V {
40         (self.compute)(tcx, key)
41     }
42
43     pub(crate) fn hash_result(
44         &self,
45         hcx: &mut CTX::StableHashingContext,
46         value: &V,
47     ) -> Option<Fingerprint> {
48         (self.hash_result)(hcx, value)
49     }
50
51     pub(crate) fn cache_on_disk(&self, tcx: CTX, key: &K, value: Option<&V>) -> bool {
52         (self.cache_on_disk)(tcx, key, value)
53     }
54
55     pub(crate) fn try_load_from_disk(&self, tcx: CTX, index: SerializedDepNodeIndex) -> Option<V> {
56         (self.try_load_from_disk)(tcx, index)
57     }
58 }
59
60 pub trait QueryAccessors<CTX: QueryContext>: QueryConfig<CTX> {
61     const ANON: bool;
62     const EVAL_ALWAYS: bool;
63     const DEP_KIND: CTX::DepKind;
64
65     type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
66
67     // Don't use this method to access query results, instead use the methods on TyCtxt
68     fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX, Self::Cache>;
69
70     fn to_dep_node(tcx: CTX, key: &Self::Key) -> DepNode<CTX::DepKind>;
71
72     // Don't use this method to compute query results, instead use the methods on TyCtxt
73     fn compute(tcx: CTX, key: Self::Key) -> Self::Value;
74
75     fn hash_result(
76         hcx: &mut CTX::StableHashingContext,
77         result: &Self::Value,
78     ) -> Option<Fingerprint>;
79
80     fn handle_cycle_error(tcx: CTX, error: CycleError<CTX::Query>) -> Self::Value;
81 }
82
83 pub trait QueryDescription<CTX: QueryContext>: QueryAccessors<CTX> {
84     fn describe(tcx: CTX, key: Self::Key) -> Cow<'static, str>;
85
86     #[inline]
87     fn cache_on_disk(_: CTX, _: &Self::Key, _: Option<&Self::Value>) -> bool {
88         false
89     }
90
91     fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> {
92         panic!("QueryDescription::load_from_disk() called for an unsupported query.")
93     }
94 }
95
96 pub(crate) trait QueryVtableExt<CTX: QueryContext, K, V> {
97     const VTABLE: QueryVtable<CTX, K, V>;
98 }
99
100 impl<CTX, Q> QueryVtableExt<CTX, Q::Key, Q::Value> for Q
101 where
102     CTX: QueryContext,
103     Q: QueryDescription<CTX>,
104 {
105     const VTABLE: QueryVtable<CTX, Q::Key, Q::Value> = QueryVtable {
106         eval_always: Q::EVAL_ALWAYS,
107         compute: Q::compute,
108         hash_result: Q::hash_result,
109         cache_on_disk: Q::cache_on_disk,
110         try_load_from_disk: Q::try_load_from_disk,
111     };
112 }
113
114 impl<CTX: QueryContext, M> QueryDescription<CTX> for M
115 where
116     M: QueryAccessors<CTX, Key = DefId>,
117 {
118     default fn describe(tcx: CTX, def_id: DefId) -> Cow<'static, str> {
119         if !tcx.verbose() {
120             format!("processing `{}`", tcx.def_path_str(def_id)).into()
121         } else {
122             let name = ::std::any::type_name::<M>();
123             format!("processing {:?} with query `{}`", def_id, name).into()
124         }
125     }
126
127     default fn cache_on_disk(_: CTX, _: &Self::Key, _: Option<&Self::Value>) -> bool {
128         false
129     }
130
131     default fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> {
132         panic!("QueryDescription::load_from_disk() called for an unsupported query.")
133     }
134 }