1
Fork 0
rust/src/librustc_query_system/query/config.rs

151 lines
4.8 KiB
Rust
Raw Normal View History

2020-03-08 17:24:56 +01:00
//! Query configuration and description traits.
use crate::dep_graph::DepNode;
2019-02-05 11:20:45 -06:00
use crate::dep_graph::SerializedDepNodeIndex;
2020-03-19 14:13:31 +01:00
use crate::query::caches::QueryCache;
use crate::query::plumbing::CycleError;
use crate::query::{QueryContext, QueryState};
use rustc_data_structures::profiling::ProfileCategory;
2020-03-24 20:37:19 +01:00
use rustc_span::def_id::DefId;
2017-09-18 05:40:13 -04:00
2019-12-22 17:42:04 -05:00
use rustc_data_structures::fingerprint::Fingerprint;
use std::borrow::Cow;
use std::fmt::Debug;
2019-12-22 17:42:04 -05:00
use std::hash::Hash;
2017-09-18 05:40:13 -04:00
2020-03-29 15:24:45 +02:00
// The parameter `CTX` is required in librustc_middle:
// implementations may need to access the `'tcx` lifetime in `CTX = TyCtxt<'tcx>`.
2020-03-08 17:24:56 +01:00
pub trait QueryConfig<CTX> {
const NAME: &'static str;
2018-05-19 13:50:58 -04:00
const CATEGORY: ProfileCategory;
type Key: Eq + Hash + Clone + Debug;
type Value;
type Stored: Clone;
2018-06-13 16:44:43 +03:00
}
2020-03-06 22:15:46 +01:00
pub(crate) struct QueryVtable<CTX: QueryContext, K, V> {
2020-03-06 22:56:05 +01:00
pub anon: bool,
pub dep_kind: CTX::DepKind,
2020-03-06 22:15:46 +01:00
pub eval_always: bool,
2020-03-28 13:12:20 +01:00
pub to_dep_node: fn(CTX, &K) -> DepNode<CTX::DepKind>,
2020-03-06 22:15:46 +01:00
// Don't use this method to compute query results, instead use the methods on TyCtxt
pub compute: fn(CTX, K) -> V,
pub hash_result: fn(&mut CTX::StableHashingContext, &V) -> Option<Fingerprint>,
2020-03-07 17:32:07 +01:00
pub handle_cycle_error: fn(CTX, CycleError<CTX::Query>) -> V,
pub cache_on_disk: fn(CTX, &K, Option<&V>) -> bool,
pub try_load_from_disk: fn(CTX, SerializedDepNodeIndex) -> Option<V>,
}
impl<CTX: QueryContext, K, V> QueryVtable<CTX, K, V> {
2020-03-28 13:12:20 +01:00
pub(crate) fn to_dep_node(&self, tcx: CTX, key: &K) -> DepNode<CTX::DepKind> {
(self.to_dep_node)(tcx, key)
}
pub(crate) fn compute(&self, tcx: CTX, key: K) -> V {
(self.compute)(tcx, key)
}
pub(crate) fn hash_result(
&self,
hcx: &mut CTX::StableHashingContext,
value: &V,
) -> Option<Fingerprint> {
(self.hash_result)(hcx, value)
}
2020-03-07 17:32:07 +01:00
pub(crate) fn handle_cycle_error(&self, tcx: CTX, error: CycleError<CTX::Query>) -> V {
(self.handle_cycle_error)(tcx, error)
}
pub(crate) fn cache_on_disk(&self, tcx: CTX, key: &K, value: Option<&V>) -> bool {
(self.cache_on_disk)(tcx, key, value)
}
pub(crate) fn try_load_from_disk(&self, tcx: CTX, index: SerializedDepNodeIndex) -> Option<V> {
(self.try_load_from_disk)(tcx, index)
}
2020-03-06 22:15:46 +01:00
}
2020-03-19 14:13:31 +01:00
pub trait QueryAccessors<CTX: QueryContext>: QueryConfig<CTX> {
const ANON: bool;
const EVAL_ALWAYS: bool;
const DEP_KIND: CTX::DepKind;
type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
2018-04-27 12:08:54 +02:00
// Don't use this method to access query results, instead use the methods on TyCtxt
2020-03-08 18:20:18 +01:00
fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX, Self::Cache>;
fn to_dep_node(tcx: CTX, key: &Self::Key) -> DepNode<CTX::DepKind>;
2018-04-27 12:08:54 +02:00
// Don't use this method to compute query results, instead use the methods on TyCtxt
2020-03-08 18:20:18 +01:00
fn compute(tcx: CTX, key: Self::Key) -> Self::Value;
fn hash_result(
hcx: &mut CTX::StableHashingContext,
result: &Self::Value,
) -> Option<Fingerprint>;
fn handle_cycle_error(tcx: CTX, error: CycleError<CTX::Query>) -> Self::Value;
2017-09-18 05:40:13 -04:00
}
2020-03-19 14:13:31 +01:00
pub trait QueryDescription<CTX: QueryContext>: QueryAccessors<CTX> {
fn describe(tcx: CTX, key: Self::Key) -> Cow<'static, str>;
#[inline]
fn cache_on_disk(_: CTX, _: &Self::Key, _: Option<&Self::Value>) -> bool {
false
}
fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> {
2020-03-19 14:13:31 +01:00
panic!("QueryDescription::load_from_disk() called for an unsupported query.")
}
2017-09-18 05:40:13 -04:00
}
2020-03-06 22:15:46 +01:00
pub(crate) trait QueryVtableExt<CTX: QueryContext, K, V> {
const VTABLE: QueryVtable<CTX, K, V>;
}
impl<CTX, Q> QueryVtableExt<CTX, Q::Key, Q::Value> for Q
where
CTX: QueryContext,
Q: QueryDescription<CTX>,
{
const VTABLE: QueryVtable<CTX, Q::Key, Q::Value> = QueryVtable {
2020-03-06 22:56:05 +01:00
anon: Q::ANON,
dep_kind: Q::DEP_KIND,
2020-03-28 13:12:20 +01:00
to_dep_node: Q::to_dep_node,
2020-03-06 22:15:46 +01:00
eval_always: Q::EVAL_ALWAYS,
compute: Q::compute,
hash_result: Q::hash_result,
2020-03-07 17:32:07 +01:00
handle_cycle_error: Q::handle_cycle_error,
cache_on_disk: Q::cache_on_disk,
try_load_from_disk: Q::try_load_from_disk,
2020-03-06 22:15:46 +01:00
};
}
impl<CTX: QueryContext, M> QueryDescription<CTX> for M
2020-03-08 18:20:18 +01:00
where
M: QueryAccessors<CTX, Key = DefId>,
2020-03-08 18:20:18 +01:00
{
default fn describe(tcx: CTX, def_id: DefId) -> Cow<'static, str> {
2020-03-25 08:02:27 +01:00
if !tcx.verbose() {
format!("processing `{}`", tcx.def_path_str(def_id)).into()
} else {
let name = ::std::any::type_name::<M>();
2018-06-06 22:13:52 +02:00
format!("processing {:?} with query `{}`", def_id, name).into()
}
2017-09-18 05:40:13 -04:00
}
2019-09-19 00:14:02 +02:00
default fn cache_on_disk(_: CTX, _: &Self::Key, _: Option<&Self::Value>) -> bool {
2019-09-19 00:14:02 +02:00
false
}
default fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> {
2020-03-19 14:13:31 +01:00
panic!("QueryDescription::load_from_disk() called for an unsupported query.")
2019-09-19 00:14:02 +02:00
}
2017-09-18 05:40:13 -04:00
}