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 {
15 const NAME: &'static str;
17 type Key: Eq + Hash + Clone + Debug;
22 #[derive(Copy, Clone)]
23 pub struct QueryVTable<CTX: QueryContext, K, V> {
25 pub dep_kind: CTX::DepKind,
26 pub eval_always: bool,
27 pub depth_limit: bool,
29 pub compute: fn(CTX::DepContext, K) -> V,
30 pub hash_result: Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>,
31 pub handle_cycle_error: HandleCycleError,
32 // NOTE: this is also `None` if `cache_on_disk()` returns false, not just if it's unsupported by the query
33 pub try_load_from_disk: Option<fn(CTX, SerializedDepNodeIndex) -> Option<V>>,
36 impl<CTX: QueryContext, K, V> QueryVTable<CTX, K, V> {
37 pub(crate) fn to_dep_node(&self, tcx: CTX::DepContext, key: &K) -> DepNode<CTX::DepKind>
39 K: crate::dep_graph::DepNodeParams<CTX::DepContext>,
41 DepNode::construct(tcx, self.dep_kind, key)
44 pub(crate) fn compute(&self, tcx: CTX::DepContext, key: K) -> V {
45 (self.compute)(tcx, key)
49 pub trait QueryDescription<CTX: QueryContext>: QueryConfig {
50 type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
52 // Don't use this method to access query results, instead use the methods on TyCtxt
53 fn query_state<'a>(tcx: CTX) -> &'a QueryState<Self::Key>
57 // Don't use this method to access query results, instead use the methods on TyCtxt
58 fn query_cache<'a>(tcx: CTX) -> &'a Self::Cache
62 // Don't use this method to compute query results, instead use the methods on TyCtxt
63 fn make_vtable(tcx: CTX, key: &Self::Key) -> QueryVTable<CTX, Self::Key, Self::Value>;
65 fn cache_on_disk(tcx: CTX::DepContext, key: &Self::Key) -> bool;
67 // Don't use this method to compute query results, instead use the methods on TyCtxt
68 fn execute_query(tcx: CTX::DepContext, k: Self::Key) -> Self::Stored;