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::plumbing::CycleError;
7 use crate::query::{QueryContext, QueryState};
8 use rustc_data_structures::profiling::ProfileCategory;
9 use rustc_span::def_id::DefId;
11 use rustc_data_structures::fingerprint::Fingerprint;
16 // The parameter `CTX` is required in librustc_middle:
17 // implementations may need to access the `'tcx` lifetime in `CTX = TyCtxt<'tcx>`.
18 pub trait QueryConfig<CTX> {
19 const NAME: &'static str;
20 const CATEGORY: ProfileCategory;
22 type Key: Eq + Hash + Clone + Debug;
27 pub(crate) struct QueryVtable<CTX: QueryContext, K, V> {
28 pub eval_always: bool,
30 // Don't use this method to compute query results, instead use the methods on TyCtxt
31 pub compute: fn(CTX, K) -> V,
33 pub hash_result: fn(&mut CTX::StableHashingContext, &V) -> Option<Fingerprint>,
34 pub cache_on_disk: fn(CTX, &K, Option<&V>) -> bool,
35 pub try_load_from_disk: fn(CTX, SerializedDepNodeIndex) -> Option<V>,
38 impl<CTX: QueryContext, K, V> QueryVtable<CTX, K, V> {
39 pub(crate) fn compute(&self, tcx: CTX, key: K) -> V {
40 (self.compute)(tcx, key)
43 pub(crate) fn hash_result(
45 hcx: &mut CTX::StableHashingContext,
47 ) -> Option<Fingerprint> {
48 (self.hash_result)(hcx, value)
51 pub(crate) fn cache_on_disk(&self, tcx: CTX, key: &K, value: Option<&V>) -> bool {
52 (self.cache_on_disk)(tcx, key, value)
55 pub(crate) fn try_load_from_disk(&self, tcx: CTX, index: SerializedDepNodeIndex) -> Option<V> {
56 (self.try_load_from_disk)(tcx, index)
60 pub trait QueryAccessors<CTX: QueryContext>: QueryConfig<CTX> {
62 const EVAL_ALWAYS: bool;
63 const DEP_KIND: CTX::DepKind;
65 type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
67 // Don't use this method to access query results, instead use the methods on TyCtxt
68 fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX, Self::Cache>;
70 fn to_dep_node(tcx: CTX, key: &Self::Key) -> DepNode<CTX::DepKind>;
72 // Don't use this method to compute query results, instead use the methods on TyCtxt
73 fn compute(tcx: CTX, key: Self::Key) -> Self::Value;
76 hcx: &mut CTX::StableHashingContext,
78 ) -> Option<Fingerprint>;
80 fn handle_cycle_error(tcx: CTX, error: CycleError<CTX::Query>) -> Self::Value;
83 pub trait QueryDescription<CTX: QueryContext>: QueryAccessors<CTX> {
84 fn describe(tcx: CTX, key: Self::Key) -> Cow<'static, str>;
87 fn cache_on_disk(_: CTX, _: &Self::Key, _: Option<&Self::Value>) -> bool {
91 fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> {
92 panic!("QueryDescription::load_from_disk() called for an unsupported query.")
96 pub(crate) trait QueryVtableExt<CTX: QueryContext, K, V> {
97 const VTABLE: QueryVtable<CTX, K, V>;
100 impl<CTX, Q> QueryVtableExt<CTX, Q::Key, Q::Value> for Q
103 Q: QueryDescription<CTX>,
105 const VTABLE: QueryVtable<CTX, Q::Key, Q::Value> = QueryVtable {
106 eval_always: Q::EVAL_ALWAYS,
108 hash_result: Q::hash_result,
109 cache_on_disk: Q::cache_on_disk,
110 try_load_from_disk: Q::try_load_from_disk,
114 impl<CTX: QueryContext, M> QueryDescription<CTX> for M
116 M: QueryAccessors<CTX, Key = DefId>,
118 default fn describe(tcx: CTX, def_id: DefId) -> Cow<'static, str> {
120 format!("processing `{}`", tcx.def_path_str(def_id)).into()
122 let name = ::std::any::type_name::<M>();
123 format!("processing {:?} with query `{}`", def_id, name).into()
127 default fn cache_on_disk(_: CTX, _: &Self::Key, _: Option<&Self::Value>) -> bool {
131 default fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> {
132 panic!("QueryDescription::load_from_disk() called for an unsupported query.")