]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/query/config.rs
178c2362def6e9db554cbdef6f8c67c63c2048f5
[rust.git] / src / librustc / ty / query / config.rs
1 use crate::dep_graph::SerializedDepNodeIndex;
2 use crate::dep_graph::{DepKind, DepNode};
3 use crate::ty::query::caches::QueryCache;
4 use crate::ty::query::plumbing::CycleError;
5 use crate::ty::query::{Query, QueryState};
6 use crate::ty::TyCtxt;
7 use rustc_data_structures::profiling::ProfileCategory;
8 use rustc_hir::def_id::DefId;
9
10 use crate::ich::StableHashingContext;
11 use rustc_data_structures::fingerprint::Fingerprint;
12 use std::borrow::Cow;
13 use std::fmt::Debug;
14 use std::hash::Hash;
15
16 // Query configuration and description traits.
17
18 // FIXME(eddyb) false positive, the lifetime parameter is used for `Key`/`Value`.
19 #[allow(unused_lifetimes)]
20 pub trait QueryConfig<'tcx> {
21     const NAME: &'static str;
22     const CATEGORY: ProfileCategory;
23
24     type Key: Eq + Hash + Clone + Debug;
25     type Value: Clone;
26 }
27
28 pub(crate) trait QueryAccessors<'tcx>: QueryConfig<'tcx> {
29     const ANON: bool;
30     const EVAL_ALWAYS: bool;
31
32     type Cache: QueryCache<Self::Key, Self::Value>;
33
34     fn query(key: Self::Key) -> Query<'tcx>;
35
36     // Don't use this method to access query results, instead use the methods on TyCtxt
37     fn query_state<'a>(tcx: TyCtxt<'tcx>) -> &'a QueryState<'tcx, Self>;
38
39     fn to_dep_node(tcx: TyCtxt<'tcx>, key: &Self::Key) -> DepNode;
40
41     fn dep_kind() -> DepKind;
42
43     // Don't use this method to compute query results, instead use the methods on TyCtxt
44     fn compute(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value;
45
46     fn hash_result(hcx: &mut StableHashingContext<'_>, result: &Self::Value)
47     -> Option<Fingerprint>;
48
49     fn handle_cycle_error(tcx: TyCtxt<'tcx>, error: CycleError<'tcx>) -> Self::Value;
50 }
51
52 pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
53     fn describe(tcx: TyCtxt<'_>, key: Self::Key) -> Cow<'static, str>;
54
55     #[inline]
56     fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
57         false
58     }
59
60     fn try_load_from_disk(_: TyCtxt<'tcx>, _: SerializedDepNodeIndex) -> Option<Self::Value> {
61         bug!("QueryDescription::load_from_disk() called for an unsupported query.")
62     }
63 }
64
65 impl<'tcx, M: QueryAccessors<'tcx, Key = DefId>> QueryDescription<'tcx> for M
66 where
67     <M as QueryAccessors<'tcx>>::Cache: QueryCache<DefId, <M as QueryConfig<'tcx>>::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 }