]> git.proxmox.com Git - rustc.git/blob - src/librustc/ty/query/config.rs
New upstream version 1.40.0+dfsg1
[rustc.git] / src / librustc / ty / query / config.rs
1 use crate::dep_graph::SerializedDepNodeIndex;
2 use crate::dep_graph::{DepKind, DepNode};
3 use crate::hir::def_id::{CrateNum, DefId};
4 use crate::ty::TyCtxt;
5 use crate::ty::query::queries;
6 use crate::ty::query::{Query, QueryName};
7 use crate::ty::query::QueryCache;
8 use crate::ty::query::plumbing::CycleError;
9 use crate::util::profiling::ProfileCategory;
10
11 use std::borrow::Cow;
12 use std::hash::Hash;
13 use std::fmt::Debug;
14 use rustc_data_structures::sharded::Sharded;
15 use rustc_data_structures::fingerprint::Fingerprint;
16 use crate::ich::StableHashingContext;
17
18 // Query configuration and description traits.
19
20 // FIXME(eddyb) false positive, the lifetime parameter is used for `Key`/`Value`.
21 #[allow(unused_lifetimes)]
22 pub trait QueryConfig<'tcx> {
23 const NAME: QueryName;
24 const CATEGORY: ProfileCategory;
25
26 type Key: Eq + Hash + Clone + Debug;
27 type Value: Clone;
28 }
29
30 pub(crate) trait QueryAccessors<'tcx>: QueryConfig<'tcx> {
31 const ANON: bool;
32 const EVAL_ALWAYS: bool;
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_cache<'a>(tcx: TyCtxt<'tcx>) -> &'a Sharded<QueryCache<'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(
47 hcx: &mut StableHashingContext<'_>,
48 result: &Self::Value
49 ) -> Option<Fingerprint>;
50
51 fn handle_cycle_error(tcx: TyCtxt<'tcx>, error: CycleError<'tcx>) -> Self::Value;
52 }
53
54 pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
55 fn describe(tcx: TyCtxt<'_>, key: Self::Key) -> Cow<'static, str>;
56
57 #[inline]
58 fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
59 false
60 }
61
62 fn try_load_from_disk(_: TyCtxt<'tcx>, _: SerializedDepNodeIndex) -> Option<Self::Value> {
63 bug!("QueryDescription::load_from_disk() called for an unsupported query.")
64 }
65 }
66
67 impl<'tcx, M: QueryAccessors<'tcx, Key = DefId>> QueryDescription<'tcx> for M {
68 default fn describe(tcx: TyCtxt<'_>, def_id: DefId) -> Cow<'static, str> {
69 if !tcx.sess.verbose() {
70 format!("processing `{}`", tcx.def_path_str(def_id)).into()
71 } else {
72 let name = ::std::any::type_name::<M>();
73 format!("processing {:?} with query `{}`", def_id, name).into()
74 }
75 }
76
77 default fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
78 false
79 }
80
81 default fn try_load_from_disk(
82 _: TyCtxt<'tcx>,
83 _: SerializedDepNodeIndex,
84 ) -> Option<Self::Value> {
85 bug!("QueryDescription::load_from_disk() called for an unsupported query.")
86 }
87 }
88
89 impl<'tcx> QueryDescription<'tcx> for queries::analysis<'tcx> {
90 fn describe(_tcx: TyCtxt<'_>, _: CrateNum) -> Cow<'static, str> {
91 "running analysis passes on this crate".into()
92 }
93 }