1 //! Query configuration and description traits.
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
;
11 use rustc_data_structures
::fingerprint
::Fingerprint
;
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
;
22 type Key
: Eq
+ Hash
+ Clone
+ Debug
;
26 pub trait QueryAccessors
<CTX
: QueryContext
>: QueryConfig
<CTX
> {
28 const EVAL_ALWAYS
: bool
;
29 const DEP_KIND
: CTX
::DepKind
;
31 type Cache
: QueryCache
<Key
= Self::Key
, Value
= Self::Value
>;
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
>;
36 fn to_dep_node(tcx
: CTX
, key
: &Self::Key
) -> DepNode
<CTX
::DepKind
>;
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
;
42 hcx
: &mut CTX
::StableHashingContext
,
44 ) -> Option
<Fingerprint
>;
46 fn handle_cycle_error(tcx
: CTX
, error
: CycleError
<CTX
::Query
>) -> Self::Value
;
49 pub trait QueryDescription
<CTX
: QueryContext
>: QueryAccessors
<CTX
> {
50 fn describe(tcx
: CTX
, key
: Self::Key
) -> Cow
<'
static, str>;
53 fn cache_on_disk(_
: CTX
, _
: Self::Key
, _
: Option
<&Self::Value
>) -> bool
{
57 fn try_load_from_disk(_
: CTX
, _
: SerializedDepNodeIndex
) -> Option
<Self::Value
> {
58 panic
!("QueryDescription::load_from_disk() called for an unsupported query.")
62 impl<CTX
: QueryContext
, M
> QueryDescription
<CTX
> for M
64 M
: QueryAccessors
<CTX
, Key
= DefId
>,
66 default fn describe(tcx
: CTX
, def_id
: DefId
) -> Cow
<'
static, str> {
68 format
!("processing `{}`", tcx
.def_path_str(def_id
)).into()
70 let name
= ::std
::any
::type_name
::<M
>();
71 format
!("processing {:?} with query `{}`", def_id
, name
).into()
75 default fn cache_on_disk(_
: CTX
, _
: Self::Key
, _
: Option
<&Self::Value
>) -> bool
{
79 default fn try_load_from_disk(_
: CTX
, _
: SerializedDepNodeIndex
) -> Option
<Self::Value
> {
80 panic
!("QueryDescription::load_from_disk() called for an unsupported query.")