1 //! Query configuration and description traits.
3 use crate::dep_graph::DepNode;
4 use crate::dep_graph::SerializedDepNodeIndex;
5 use crate::query::caches::QueryCache;
6 use crate::query::{QueryCacheStore, QueryContext, QueryState};
8 use rustc_data_structures::fingerprint::Fingerprint;
9 use rustc_errors::DiagnosticBuilder;
13 pub trait QueryConfig {
14 const NAME: &'static str;
16 type Key: Eq + Hash + Clone + Debug;
21 pub(crate) struct QueryVtable<CTX: QueryContext, K, V> {
23 pub dep_kind: CTX::DepKind,
24 pub eval_always: bool,
26 // Don't use this method to compute query results, instead use the methods on TyCtxt
27 pub compute: fn(CTX, K) -> V,
29 pub hash_result: fn(&mut CTX::StableHashingContext, &V) -> Option<Fingerprint>,
30 pub handle_cycle_error: fn(CTX, DiagnosticBuilder<'_>) -> V,
31 pub cache_on_disk: fn(CTX, &K, Option<&V>) -> bool,
32 pub try_load_from_disk: fn(CTX, SerializedDepNodeIndex) -> Option<V>,
35 impl<CTX: QueryContext, K, V> QueryVtable<CTX, K, V> {
36 pub(crate) fn to_dep_node(&self, tcx: CTX::DepContext, key: &K) -> DepNode<CTX::DepKind>
38 K: crate::dep_graph::DepNodeParams<CTX::DepContext>,
40 DepNode::construct(tcx, self.dep_kind, key)
43 pub(crate) fn compute(&self, tcx: CTX, key: K) -> V {
44 (self.compute)(tcx, key)
47 pub(crate) fn hash_result(
49 hcx: &mut CTX::StableHashingContext,
51 ) -> Option<Fingerprint> {
52 (self.hash_result)(hcx, value)
55 pub(crate) fn cache_on_disk(&self, tcx: CTX, key: &K, value: Option<&V>) -> bool {
56 (self.cache_on_disk)(tcx, key, value)
59 pub(crate) fn try_load_from_disk(&self, tcx: CTX, index: SerializedDepNodeIndex) -> Option<V> {
60 (self.try_load_from_disk)(tcx, index)
64 pub trait QueryAccessors<CTX: QueryContext>: QueryConfig {
66 const EVAL_ALWAYS: bool;
67 const DEP_KIND: CTX::DepKind;
69 type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
71 // Don't use this method to access query results, instead use the methods on TyCtxt
72 fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX::DepKind, Self::Key>
76 // Don't use this method to access query results, instead use the methods on TyCtxt
77 fn query_cache<'a>(tcx: CTX) -> &'a QueryCacheStore<Self::Cache>
81 // Don't use this method to compute query results, instead use the methods on TyCtxt
82 fn compute(tcx: CTX, key: Self::Key) -> Self::Value;
85 hcx: &mut CTX::StableHashingContext,
87 ) -> Option<Fingerprint>;
89 fn handle_cycle_error(tcx: CTX, diag: DiagnosticBuilder<'_>) -> Self::Value;
92 pub trait QueryDescription<CTX: QueryContext>: QueryAccessors<CTX> {
93 fn describe(tcx: CTX, key: Self::Key) -> String;
96 fn cache_on_disk(_: CTX, _: &Self::Key, _: Option<&Self::Value>) -> bool {
100 fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> {
101 panic!("QueryDescription::load_from_disk() called for an unsupported query.")
105 pub(crate) trait QueryVtableExt<CTX: QueryContext, K, V> {
106 const VTABLE: QueryVtable<CTX, K, V>;
109 impl<CTX, Q> QueryVtableExt<CTX, Q::Key, Q::Value> for Q
112 Q: QueryDescription<CTX>,
114 const VTABLE: QueryVtable<CTX, Q::Key, Q::Value> = QueryVtable {
116 dep_kind: Q::DEP_KIND,
117 eval_always: Q::EVAL_ALWAYS,
119 hash_result: Q::hash_result,
120 handle_cycle_error: Q::handle_cycle_error,
121 cache_on_disk: Q::cache_on_disk,
122 try_load_from_disk: Q::try_load_from_disk,