]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/query/config.rs
Auto merge of #68030 - cuviper:llvm-9.0.1, r=nikic
[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::plumbing::CycleError;
4 use crate::ty::query::queries;
5 use crate::ty::query::{Query, QueryCache};
6 use crate::ty::TyCtxt;
7 use rustc_data_structures::profiling::ProfileCategory;
8 use rustc_hir::def_id::{CrateNum, DefId};
9
10 use crate::ich::StableHashingContext;
11 use rustc_data_structures::fingerprint::Fingerprint;
12 use rustc_data_structures::sharded::Sharded;
13 use std::borrow::Cow;
14 use std::fmt::Debug;
15 use std::hash::Hash;
16
17 // Query configuration and description traits.
18
19 // FIXME(eddyb) false positive, the lifetime parameter is used for `Key`/`Value`.
20 #[allow(unused_lifetimes)]
21 pub trait QueryConfig<'tcx> {
22     const NAME: &'static str;
23     const CATEGORY: ProfileCategory;
24
25     type Key: Eq + Hash + Clone + Debug;
26     type Value: Clone;
27 }
28
29 pub(crate) trait QueryAccessors<'tcx>: QueryConfig<'tcx> {
30     const ANON: bool;
31     const EVAL_ALWAYS: bool;
32
33     fn query(key: Self::Key) -> Query<'tcx>;
34
35     // Don't use this method to access query results, instead use the methods on TyCtxt
36     fn query_cache<'a>(tcx: TyCtxt<'tcx>) -> &'a Sharded<QueryCache<'tcx, Self>>;
37
38     fn to_dep_node(tcx: TyCtxt<'tcx>, key: &Self::Key) -> DepNode;
39
40     fn dep_kind() -> DepKind;
41
42     // Don't use this method to compute query results, instead use the methods on TyCtxt
43     fn compute(tcx: TyCtxt<'tcx>, 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: TyCtxt<'tcx>, error: CycleError<'tcx>) -> Self::Value;
49 }
50
51 pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'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: QueryAccessors<'tcx, Key = DefId>> QueryDescription<'tcx> for M {
65     default fn describe(tcx: TyCtxt<'_>, def_id: DefId) -> Cow<'static, str> {
66         if !tcx.sess.verbose() {
67             format!("processing `{}`", tcx.def_path_str(def_id)).into()
68         } else {
69             let name = ::std::any::type_name::<M>();
70             format!("processing {:?} with query `{}`", def_id, name).into()
71         }
72     }
73
74     default fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
75         false
76     }
77
78     default fn try_load_from_disk(
79         _: TyCtxt<'tcx>,
80         _: SerializedDepNodeIndex,
81     ) -> Option<Self::Value> {
82         bug!("QueryDescription::load_from_disk() called for an unsupported query.")
83     }
84 }
85
86 impl<'tcx> QueryDescription<'tcx> for queries::analysis<'tcx> {
87     fn describe(_tcx: TyCtxt<'_>, _: CrateNum) -> Cow<'static, str> {
88         "running analysis passes on this crate".into()
89     }
90 }