]>
Commit | Line | Data |
---|---|---|
ba9703b0 XL |
1 | //! Query configuration and description traits. |
2 | ||
3 | use crate::dep_graph::DepNode; | |
4 | use crate::dep_graph::SerializedDepNodeIndex; | |
c295e0f8 | 5 | use crate::ich::StableHashingContext; |
ba9703b0 | 6 | use crate::query::caches::QueryCache; |
6a06907d | 7 | use crate::query::{QueryCacheStore, QueryContext, QueryState}; |
ba9703b0 XL |
8 | |
9 | use rustc_data_structures::fingerprint::Fingerprint; | |
6a06907d | 10 | use rustc_errors::DiagnosticBuilder; |
ba9703b0 XL |
11 | use std::fmt::Debug; |
12 | use std::hash::Hash; | |
13 | ||
29967ef6 | 14 | pub trait QueryConfig { |
ba9703b0 | 15 | const NAME: &'static str; |
ba9703b0 XL |
16 | |
17 | type Key: Eq + Hash + Clone + Debug; | |
f9f354fc XL |
18 | type Value; |
19 | type Stored: Clone; | |
20 | } | |
21 | ||
22 | pub(crate) struct QueryVtable<CTX: QueryContext, K, V> { | |
23 | pub anon: bool, | |
24 | pub dep_kind: CTX::DepKind, | |
25 | pub eval_always: bool, | |
26 | ||
c295e0f8 | 27 | pub hash_result: fn(&mut StableHashingContext<'_>, &V) -> Option<Fingerprint>, |
6a06907d | 28 | pub handle_cycle_error: fn(CTX, DiagnosticBuilder<'_>) -> V, |
f9f354fc XL |
29 | pub cache_on_disk: fn(CTX, &K, Option<&V>) -> bool, |
30 | pub try_load_from_disk: fn(CTX, SerializedDepNodeIndex) -> Option<V>, | |
31 | } | |
32 | ||
33 | impl<CTX: QueryContext, K, V> QueryVtable<CTX, K, V> { | |
6a06907d | 34 | pub(crate) fn to_dep_node(&self, tcx: CTX::DepContext, key: &K) -> DepNode<CTX::DepKind> |
f9f354fc | 35 | where |
6a06907d | 36 | K: crate::dep_graph::DepNodeParams<CTX::DepContext>, |
f9f354fc XL |
37 | { |
38 | DepNode::construct(tcx, self.dep_kind, key) | |
39 | } | |
40 | ||
f9f354fc XL |
41 | pub(crate) fn hash_result( |
42 | &self, | |
c295e0f8 | 43 | hcx: &mut StableHashingContext<'_>, |
f9f354fc XL |
44 | value: &V, |
45 | ) -> Option<Fingerprint> { | |
46 | (self.hash_result)(hcx, value) | |
47 | } | |
48 | ||
f9f354fc XL |
49 | pub(crate) fn cache_on_disk(&self, tcx: CTX, key: &K, value: Option<&V>) -> bool { |
50 | (self.cache_on_disk)(tcx, key, value) | |
51 | } | |
52 | ||
53 | pub(crate) fn try_load_from_disk(&self, tcx: CTX, index: SerializedDepNodeIndex) -> Option<V> { | |
54 | (self.try_load_from_disk)(tcx, index) | |
55 | } | |
ba9703b0 XL |
56 | } |
57 | ||
29967ef6 | 58 | pub trait QueryAccessors<CTX: QueryContext>: QueryConfig { |
ba9703b0 XL |
59 | const ANON: bool; |
60 | const EVAL_ALWAYS: bool; | |
61 | const DEP_KIND: CTX::DepKind; | |
62 | ||
f9f354fc | 63 | type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>; |
ba9703b0 XL |
64 | |
65 | // Don't use this method to access query results, instead use the methods on TyCtxt | |
6a06907d XL |
66 | fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX::DepKind, Self::Key> |
67 | where | |
68 | CTX: 'a; | |
ba9703b0 | 69 | |
6a06907d XL |
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> | |
f9f354fc | 72 | where |
6a06907d | 73 | CTX: 'a; |
ba9703b0 XL |
74 | |
75 | // Don't use this method to compute query results, instead use the methods on TyCtxt | |
136023e0 | 76 | fn compute_fn(tcx: CTX, key: &Self::Key) -> fn(CTX::DepContext, Self::Key) -> Self::Value; |
ba9703b0 | 77 | |
c295e0f8 XL |
78 | fn hash_result(hcx: &mut StableHashingContext<'_>, result: &Self::Value) |
79 | -> Option<Fingerprint>; | |
ba9703b0 | 80 | |
6a06907d | 81 | fn handle_cycle_error(tcx: CTX, diag: DiagnosticBuilder<'_>) -> Self::Value; |
ba9703b0 XL |
82 | } |
83 | ||
84 | pub trait QueryDescription<CTX: QueryContext>: QueryAccessors<CTX> { | |
6a06907d | 85 | fn describe(tcx: CTX, key: Self::Key) -> String; |
ba9703b0 XL |
86 | |
87 | #[inline] | |
f9f354fc | 88 | fn cache_on_disk(_: CTX, _: &Self::Key, _: Option<&Self::Value>) -> bool { |
ba9703b0 XL |
89 | false |
90 | } | |
91 | ||
92 | fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> { | |
93 | panic!("QueryDescription::load_from_disk() called for an unsupported query.") | |
94 | } | |
95 | } | |
96 | ||
f9f354fc XL |
97 | pub(crate) trait QueryVtableExt<CTX: QueryContext, K, V> { |
98 | const VTABLE: QueryVtable<CTX, K, V>; | |
99 | } | |
100 | ||
101 | impl<CTX, Q> QueryVtableExt<CTX, Q::Key, Q::Value> for Q | |
ba9703b0 | 102 | where |
f9f354fc XL |
103 | CTX: QueryContext, |
104 | Q: QueryDescription<CTX>, | |
ba9703b0 | 105 | { |
f9f354fc XL |
106 | const VTABLE: QueryVtable<CTX, Q::Key, Q::Value> = QueryVtable { |
107 | anon: Q::ANON, | |
108 | dep_kind: Q::DEP_KIND, | |
109 | eval_always: Q::EVAL_ALWAYS, | |
f9f354fc XL |
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, | |
114 | }; | |
ba9703b0 | 115 | } |