]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_query_system/src/query/config.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / compiler / rustc_query_system / src / query / config.rs
1 //! Query configuration and description traits.
2
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};
7
8 use rustc_data_structures::fingerprint::Fingerprint;
9 use rustc_errors::DiagnosticBuilder;
10 use std::fmt::Debug;
11 use std::hash::Hash;
12
13 pub trait QueryConfig {
14 const NAME: &'static str;
15
16 type Key: Eq + Hash + Clone + Debug;
17 type Value;
18 type Stored: Clone;
19 }
20
21 pub(crate) struct QueryVtable<CTX: QueryContext, K, V> {
22 pub anon: bool,
23 pub dep_kind: CTX::DepKind,
24 pub eval_always: bool,
25
26 // Don't use this method to compute query results, instead use the methods on TyCtxt
27 pub compute: fn(CTX, K) -> V,
28
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>,
33 }
34
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>
37 where
38 K: crate::dep_graph::DepNodeParams<CTX::DepContext>,
39 {
40 DepNode::construct(tcx, self.dep_kind, key)
41 }
42
43 pub(crate) fn compute(&self, tcx: CTX, key: K) -> V {
44 (self.compute)(tcx, key)
45 }
46
47 pub(crate) fn hash_result(
48 &self,
49 hcx: &mut CTX::StableHashingContext,
50 value: &V,
51 ) -> Option<Fingerprint> {
52 (self.hash_result)(hcx, value)
53 }
54
55 pub(crate) fn handle_cycle_error(&self, tcx: CTX, diag: DiagnosticBuilder<'_>) -> V {
56 (self.handle_cycle_error)(tcx, diag)
57 }
58
59 pub(crate) fn cache_on_disk(&self, tcx: CTX, key: &K, value: Option<&V>) -> bool {
60 (self.cache_on_disk)(tcx, key, value)
61 }
62
63 pub(crate) fn try_load_from_disk(&self, tcx: CTX, index: SerializedDepNodeIndex) -> Option<V> {
64 (self.try_load_from_disk)(tcx, index)
65 }
66 }
67
68 pub trait QueryAccessors<CTX: QueryContext>: QueryConfig {
69 const ANON: bool;
70 const EVAL_ALWAYS: bool;
71 const DEP_KIND: CTX::DepKind;
72
73 type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
74
75 // Don't use this method to access query results, instead use the methods on TyCtxt
76 fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX::DepKind, Self::Key>
77 where
78 CTX: 'a;
79
80 // Don't use this method to access query results, instead use the methods on TyCtxt
81 fn query_cache<'a>(tcx: CTX) -> &'a QueryCacheStore<Self::Cache>
82 where
83 CTX: 'a;
84
85 // Don't use this method to compute query results, instead use the methods on TyCtxt
86 fn compute(tcx: CTX, key: Self::Key) -> Self::Value;
87
88 fn hash_result(
89 hcx: &mut CTX::StableHashingContext,
90 result: &Self::Value,
91 ) -> Option<Fingerprint>;
92
93 fn handle_cycle_error(tcx: CTX, diag: DiagnosticBuilder<'_>) -> Self::Value;
94 }
95
96 pub trait QueryDescription<CTX: QueryContext>: QueryAccessors<CTX> {
97 fn describe(tcx: CTX, key: Self::Key) -> String;
98
99 #[inline]
100 fn cache_on_disk(_: CTX, _: &Self::Key, _: Option<&Self::Value>) -> bool {
101 false
102 }
103
104 fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> {
105 panic!("QueryDescription::load_from_disk() called for an unsupported query.")
106 }
107 }
108
109 pub(crate) trait QueryVtableExt<CTX: QueryContext, K, V> {
110 const VTABLE: QueryVtable<CTX, K, V>;
111 }
112
113 impl<CTX, Q> QueryVtableExt<CTX, Q::Key, Q::Value> for Q
114 where
115 CTX: QueryContext,
116 Q: QueryDescription<CTX>,
117 {
118 const VTABLE: QueryVtable<CTX, Q::Key, Q::Value> = QueryVtable {
119 anon: Q::ANON,
120 dep_kind: Q::DEP_KIND,
121 eval_always: Q::EVAL_ALWAYS,
122 compute: Q::compute,
123 hash_result: Q::hash_result,
124 handle_cycle_error: Q::handle_cycle_error,
125 cache_on_disk: Q::cache_on_disk,
126 try_load_from_disk: Q::try_load_from_disk,
127 };
128 }