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