1 //! Query configuration and description traits.
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};
9 use rustc_data_structures::fingerprint::Fingerprint;
10 use rustc_errors::DiagnosticBuilder;
14 pub trait QueryConfig {
15 const NAME: &'static str;
17 type Key: Eq + Hash + Clone + Debug;
22 pub(crate) struct QueryVtable<CTX: QueryContext, K, V> {
24 pub dep_kind: CTX::DepKind,
25 pub eval_always: bool,
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>,
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>
36 K: crate::dep_graph::DepNodeParams<CTX::DepContext>,
38 DepNode::construct(tcx, self.dep_kind, key)
41 pub(crate) fn hash_result(
43 hcx: &mut StableHashingContext<'_>,
45 ) -> Option<Fingerprint> {
46 (self.hash_result)(hcx, value)
49 pub(crate) fn cache_on_disk(&self, tcx: CTX, key: &K, value: Option<&V>) -> bool {
50 (self.cache_on_disk)(tcx, key, value)
53 pub(crate) fn try_load_from_disk(&self, tcx: CTX, index: SerializedDepNodeIndex) -> Option<V> {
54 (self.try_load_from_disk)(tcx, index)
58 pub trait QueryAccessors<CTX: QueryContext>: QueryConfig {
60 const EVAL_ALWAYS: bool;
61 const DEP_KIND: CTX::DepKind;
63 type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
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>
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>
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;
78 fn hash_result(hcx: &mut StableHashingContext<'_>, result: &Self::Value)
79 -> Option<Fingerprint>;
81 fn handle_cycle_error(tcx: CTX, diag: DiagnosticBuilder<'_>) -> Self::Value;
84 pub trait QueryDescription<CTX: QueryContext>: QueryAccessors<CTX> {
85 fn describe(tcx: CTX, key: Self::Key) -> String;
88 fn cache_on_disk(_: CTX, _: &Self::Key, _: Option<&Self::Value>) -> bool {
92 fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> {
93 panic!("QueryDescription::load_from_disk() called for an unsupported query.")
97 pub(crate) trait QueryVtableExt<CTX: QueryContext, K, V> {
98 const VTABLE: QueryVtable<CTX, K, V>;
101 impl<CTX, Q> QueryVtableExt<CTX, Q::Key, Q::Value> for Q
104 Q: QueryDescription<CTX>,
106 const VTABLE: QueryVtable<CTX, Q::Key, Q::Value> = QueryVtable {
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,