]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/query/config.rs
Make QueryAccessor argument a type.
[rust.git] / src / librustc / ty / query / config.rs
1 //! Query configuration and description traits.
2
3 use crate::dep_graph::SerializedDepNodeIndex;
4 use crate::dep_graph::{DepKind, DepNode};
5 use crate::ty::query::caches::QueryCache;
6 use crate::ty::query::plumbing::CycleError;
7 use crate::ty::query::QueryState;
8 use crate::ty::TyCtxt;
9 use rustc_data_structures::profiling::ProfileCategory;
10 use rustc_hir::def_id::DefId;
11
12 use crate::ich::StableHashingContext;
13 use rustc_data_structures::fingerprint::Fingerprint;
14 use std::borrow::Cow;
15 use std::fmt::Debug;
16 use std::hash::Hash;
17
18 pub trait QueryConfig<CTX> {
19     const NAME: &'static str;
20     const CATEGORY: ProfileCategory;
21
22     type Key: Eq + Hash + Clone + Debug;
23     type Value: Clone;
24 }
25
26 pub trait QueryContext: Copy {
27     type Query;
28 }
29
30 pub(crate) trait QueryAccessors<CTX: QueryContext>: QueryConfig<CTX> {
31     const ANON: bool;
32     const EVAL_ALWAYS: bool;
33     const DEP_KIND: DepKind;
34
35     type Cache: QueryCache<Key = Self::Key, Value = Self::Value>;
36
37     // Don't use this method to access query results, instead use the methods on TyCtxt
38     fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX, Self::Cache>;
39
40     fn to_dep_node(tcx: CTX, key: &Self::Key) -> DepNode;
41
42     // Don't use this method to compute query results, instead use the methods on TyCtxt
43     fn compute(tcx: CTX, key: Self::Key) -> Self::Value;
44
45     fn hash_result(hcx: &mut StableHashingContext<'_>, result: &Self::Value)
46     -> Option<Fingerprint>;
47
48     fn handle_cycle_error(tcx: CTX, error: CycleError<CTX>) -> Self::Value;
49 }
50
51 pub(crate) trait QueryDescription<'tcx>: QueryAccessors<TyCtxt<'tcx>> {
52     fn describe(tcx: TyCtxt<'_>, key: Self::Key) -> Cow<'static, str>;
53
54     #[inline]
55     fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
56         false
57     }
58
59     fn try_load_from_disk(_: TyCtxt<'tcx>, _: SerializedDepNodeIndex) -> Option<Self::Value> {
60         bug!("QueryDescription::load_from_disk() called for an unsupported query.")
61     }
62 }
63
64 impl<'tcx, M> QueryDescription<'tcx> for M
65 where
66     M: QueryAccessors<TyCtxt<'tcx>, Key = DefId>,
67     //M::Cache: QueryCache<DefId, M::Value>,
68 {
69     default fn describe(tcx: TyCtxt<'_>, def_id: DefId) -> Cow<'static, str> {
70         if !tcx.sess.verbose() {
71             format!("processing `{}`", tcx.def_path_str(def_id)).into()
72         } else {
73             let name = ::std::any::type_name::<M>();
74             format!("processing {:?} with query `{}`", def_id, name).into()
75         }
76     }
77
78     default fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
79         false
80     }
81
82     default fn try_load_from_disk(
83         _: TyCtxt<'tcx>,
84         _: SerializedDepNodeIndex,
85     ) -> Option<Self::Value> {
86         bug!("QueryDescription::load_from_disk() called for an unsupported query.")
87     }
88 }