]> git.proxmox.com Git - rustc.git/blob - src/librustc_query_system/query/config.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / src / librustc_query_system / 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::plumbing::CycleError;
7 use crate::query::{QueryContext, QueryState};
8 use rustc_data_structures::profiling::ProfileCategory;
9 use rustc_span::def_id::DefId;
10
11 use rustc_data_structures::fingerprint::Fingerprint;
12 use std::borrow::Cow;
13 use std::fmt::Debug;
14 use std::hash::Hash;
15
16 // The parameter `CTX` is required in librustc_middle:
17 // implementations may need to access the `'tcx` lifetime in `CTX = TyCtxt<'tcx>`.
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 QueryAccessors<CTX: QueryContext>: QueryConfig<CTX> {
27 const ANON: bool;
28 const EVAL_ALWAYS: bool;
29 const DEP_KIND: CTX::DepKind;
30
31 type Cache: QueryCache<Key = Self::Key, Value = Self::Value>;
32
33 // Don't use this method to access query results, instead use the methods on TyCtxt
34 fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX, Self::Cache>;
35
36 fn to_dep_node(tcx: CTX, key: &Self::Key) -> DepNode<CTX::DepKind>;
37
38 // Don't use this method to compute query results, instead use the methods on TyCtxt
39 fn compute(tcx: CTX, key: Self::Key) -> Self::Value;
40
41 fn hash_result(
42 hcx: &mut CTX::StableHashingContext,
43 result: &Self::Value,
44 ) -> Option<Fingerprint>;
45
46 fn handle_cycle_error(tcx: CTX, error: CycleError<CTX::Query>) -> Self::Value;
47 }
48
49 pub trait QueryDescription<CTX: QueryContext>: QueryAccessors<CTX> {
50 fn describe(tcx: CTX, key: Self::Key) -> Cow<'static, str>;
51
52 #[inline]
53 fn cache_on_disk(_: CTX, _: Self::Key, _: Option<&Self::Value>) -> bool {
54 false
55 }
56
57 fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> {
58 panic!("QueryDescription::load_from_disk() called for an unsupported query.")
59 }
60 }
61
62 impl<CTX: QueryContext, M> QueryDescription<CTX> for M
63 where
64 M: QueryAccessors<CTX, Key = DefId>,
65 {
66 default fn describe(tcx: CTX, def_id: DefId) -> Cow<'static, str> {
67 if !tcx.verbose() {
68 format!("processing `{}`", tcx.def_path_str(def_id)).into()
69 } else {
70 let name = ::std::any::type_name::<M>();
71 format!("processing {:?} with query `{}`", def_id, name).into()
72 }
73 }
74
75 default fn cache_on_disk(_: CTX, _: Self::Key, _: Option<&Self::Value>) -> bool {
76 false
77 }
78
79 default fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> {
80 panic!("QueryDescription::load_from_disk() called for an unsupported query.")
81 }
82 }