]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_system/src/query/config.rs
UPDATE - rename DiagnosticHandler trait to IntoDiagnostic
[rust.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::error::HandleCycleError;
6 use crate::ich::StableHashingContext;
7 use crate::query::caches::QueryCache;
8 use crate::query::{QueryContext, QueryState};
9
10 use rustc_data_structures::fingerprint::Fingerprint;
11 use std::fmt::Debug;
12 use std::hash::Hash;
13
14 pub trait QueryConfig {
15     const NAME: &'static str;
16
17     type Key: Eq + Hash + Clone + Debug;
18     type Value;
19     type Stored: Clone;
20 }
21
22 #[derive(Copy, Clone)]
23 pub struct QueryVTable<CTX: QueryContext, K, V> {
24     pub anon: bool,
25     pub dep_kind: CTX::DepKind,
26     pub eval_always: bool,
27     pub depth_limit: bool,
28
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>>,
34 }
35
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>
38     where
39         K: crate::dep_graph::DepNodeParams<CTX::DepContext>,
40     {
41         DepNode::construct(tcx, self.dep_kind, key)
42     }
43
44     pub(crate) fn compute(&self, tcx: CTX::DepContext, key: K) -> V {
45         (self.compute)(tcx, key)
46     }
47 }
48
49 pub trait QueryDescription<CTX: QueryContext>: QueryConfig {
50     type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
51
52     fn describe(tcx: CTX, key: Self::Key) -> String;
53
54     // Don't use this method to access query results, instead use the methods on TyCtxt
55     fn query_state<'a>(tcx: CTX) -> &'a QueryState<Self::Key>
56     where
57         CTX: 'a;
58
59     // Don't use this method to access query results, instead use the methods on TyCtxt
60     fn query_cache<'a>(tcx: CTX) -> &'a Self::Cache
61     where
62         CTX: 'a;
63
64     // Don't use this method to compute query results, instead use the methods on TyCtxt
65     fn make_vtable(tcx: CTX, key: &Self::Key) -> QueryVTable<CTX, Self::Key, Self::Value>;
66
67     fn cache_on_disk(tcx: CTX::DepContext, key: &Self::Key) -> bool;
68
69     // Don't use this method to compute query results, instead use the methods on TyCtxt
70     fn execute_query(tcx: CTX::DepContext, k: Self::Key) -> Self::Stored;
71 }