]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_system/src/query/config.rs
Auto merge of #89587 - camelid:all-candidates, r=petrochenkov
[rust.git] / compiler / rustc_query_system / src / 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::ich::StableHashingContext;
6 use crate::query::caches::QueryCache;
7 use crate::query::{QueryCacheStore, QueryContext, QueryState};
8
9 use rustc_data_structures::fingerprint::Fingerprint;
10 use rustc_errors::DiagnosticBuilder;
11 use std::fmt::Debug;
12 use std::hash::Hash;
13
14 pub trait QueryConfig {
15     const NAME: &'static str;
16
17     type Key: Eq + Hash + Clone + Debug;
18     type Value;
19     type Stored: Clone;
20 }
21
22 pub(crate) struct QueryVtable<CTX: QueryContext, K, V> {
23     pub anon: bool,
24     pub dep_kind: CTX::DepKind,
25     pub eval_always: bool,
26
27     pub hash_result: fn(&mut StableHashingContext<'_>, &V) -> Option<Fingerprint>,
28     pub handle_cycle_error: fn(CTX, DiagnosticBuilder<'_>) -> V,
29     pub cache_on_disk: fn(CTX, &K, Option<&V>) -> bool,
30     pub try_load_from_disk: fn(CTX, SerializedDepNodeIndex) -> Option<V>,
31 }
32
33 impl<CTX: QueryContext, K, V> QueryVtable<CTX, K, V> {
34     pub(crate) fn to_dep_node(&self, tcx: CTX::DepContext, key: &K) -> DepNode<CTX::DepKind>
35     where
36         K: crate::dep_graph::DepNodeParams<CTX::DepContext>,
37     {
38         DepNode::construct(tcx, self.dep_kind, key)
39     }
40
41     pub(crate) fn hash_result(
42         &self,
43         hcx: &mut StableHashingContext<'_>,
44         value: &V,
45     ) -> Option<Fingerprint> {
46         (self.hash_result)(hcx, value)
47     }
48
49     pub(crate) fn cache_on_disk(&self, tcx: CTX, key: &K, value: Option<&V>) -> bool {
50         (self.cache_on_disk)(tcx, key, value)
51     }
52
53     pub(crate) fn try_load_from_disk(&self, tcx: CTX, index: SerializedDepNodeIndex) -> Option<V> {
54         (self.try_load_from_disk)(tcx, index)
55     }
56 }
57
58 pub trait QueryAccessors<CTX: QueryContext>: QueryConfig {
59     const ANON: bool;
60     const EVAL_ALWAYS: bool;
61     const DEP_KIND: CTX::DepKind;
62
63     type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
64
65     // Don't use this method to access query results, instead use the methods on TyCtxt
66     fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX::DepKind, Self::Key>
67     where
68         CTX: 'a;
69
70     // Don't use this method to access query results, instead use the methods on TyCtxt
71     fn query_cache<'a>(tcx: CTX) -> &'a QueryCacheStore<Self::Cache>
72     where
73         CTX: 'a;
74
75     // Don't use this method to compute query results, instead use the methods on TyCtxt
76     fn compute_fn(tcx: CTX, key: &Self::Key) -> fn(CTX::DepContext, Self::Key) -> Self::Value;
77
78     fn hash_result(hcx: &mut StableHashingContext<'_>, result: &Self::Value)
79     -> Option<Fingerprint>;
80
81     fn handle_cycle_error(tcx: CTX, diag: DiagnosticBuilder<'_>) -> Self::Value;
82 }
83
84 pub trait QueryDescription<CTX: QueryContext>: QueryAccessors<CTX> {
85     fn describe(tcx: CTX, key: Self::Key) -> String;
86
87     #[inline]
88     fn cache_on_disk(_: CTX, _: &Self::Key, _: Option<&Self::Value>) -> bool {
89         false
90     }
91
92     fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> {
93         panic!("QueryDescription::load_from_disk() called for an unsupported query.")
94     }
95 }
96
97 pub(crate) trait QueryVtableExt<CTX: QueryContext, K, V> {
98     const VTABLE: QueryVtable<CTX, K, V>;
99 }
100
101 impl<CTX, Q> QueryVtableExt<CTX, Q::Key, Q::Value> for Q
102 where
103     CTX: QueryContext,
104     Q: QueryDescription<CTX>,
105 {
106     const VTABLE: QueryVtable<CTX, Q::Key, Q::Value> = QueryVtable {
107         anon: Q::ANON,
108         dep_kind: Q::DEP_KIND,
109         eval_always: Q::EVAL_ALWAYS,
110         hash_result: Q::hash_result,
111         handle_cycle_error: Q::handle_cycle_error,
112         cache_on_disk: Q::cache_on_disk,
113         try_load_from_disk: Q::try_load_from_disk,
114     };
115 }