1 //! Query configuration and description traits.
3 use crate::dep_graph::DepNode;
4 use crate::dep_graph::SerializedDepNodeIndex;
5 use crate::error::HandleCycleError;
6 use crate::ich::StableHashingContext;
7 use crate::query::caches::QueryCache;
8 use crate::query::{QueryContext, QueryState};
10 use rustc_data_structures::fingerprint::Fingerprint;
14 pub trait QueryConfig<CTX: QueryContext> {
15 const NAME: &'static str;
17 type Key: Eq + Hash + Clone + Debug;
21 type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
23 // Don't use this method to access query results, instead use the methods on TyCtxt
24 fn query_state<'a>(tcx: CTX) -> &'a QueryState<Self::Key>
28 // Don't use this method to access query results, instead use the methods on TyCtxt
29 fn query_cache<'a>(tcx: CTX) -> &'a Self::Cache
33 // Don't use this method to compute query results, instead use the methods on TyCtxt
34 fn make_vtable(tcx: CTX, key: &Self::Key) -> QueryVTable<CTX, Self::Key, Self::Value>;
36 fn cache_on_disk(tcx: CTX::DepContext, key: &Self::Key) -> bool;
38 // Don't use this method to compute query results, instead use the methods on TyCtxt
39 fn execute_query(tcx: CTX::DepContext, k: Self::Key) -> Self::Stored;
42 #[derive(Copy, Clone)]
43 pub struct QueryVTable<CTX: QueryContext, K, V> {
45 pub dep_kind: CTX::DepKind,
46 pub eval_always: bool,
47 pub depth_limit: bool,
49 pub compute: fn(CTX::DepContext, K) -> V,
50 pub hash_result: Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>,
51 pub handle_cycle_error: HandleCycleError,
52 // NOTE: this is also `None` if `cache_on_disk()` returns false, not just if it's unsupported by the query
53 pub try_load_from_disk: Option<fn(CTX, SerializedDepNodeIndex) -> Option<V>>,
56 impl<CTX: QueryContext, K, V> QueryVTable<CTX, K, V> {
57 pub(crate) fn to_dep_node(&self, tcx: CTX::DepContext, key: &K) -> DepNode<CTX::DepKind>
59 K: crate::dep_graph::DepNodeParams<CTX::DepContext>,
61 DepNode::construct(tcx, self.dep_kind, key)
64 pub(crate) fn compute(&self, tcx: CTX::DepContext, key: K) -> V {
65 (self.compute)(tcx, key)