rust/src/librustc/ty/query/config.rs

94 lines
3 KiB
Rust
Raw Normal View History

2019-02-05 11:20:45 -06:00
use crate::dep_graph::SerializedDepNodeIndex;
use crate::dep_graph::{DepKind, DepNode};
use crate::hir::def_id::{CrateNum, DefId};
use crate::ty::TyCtxt;
2019-02-05 11:20:45 -06:00
use crate::ty::query::queries;
use crate::ty::query::{Query, QueryName};
2019-02-05 11:20:45 -06:00
use crate::ty::query::QueryCache;
2019-01-24 20:05:19 +01:00
use crate::ty::query::plumbing::CycleError;
2019-02-05 11:20:45 -06:00
use crate::util::profiling::ProfileCategory;
2017-09-18 05:40:13 -04:00
use std::borrow::Cow;
2017-09-18 05:40:13 -04:00
use std::hash::Hash;
use std::fmt::Debug;
2019-06-12 14:39:12 +02:00
use rustc_data_structures::sharded::Sharded;
use rustc_data_structures::fingerprint::Fingerprint;
2019-02-05 11:20:45 -06:00
use crate::ich::StableHashingContext;
2017-09-18 05:40:13 -04:00
2018-06-13 16:44:43 +03:00
// Query configuration and description traits.
2017-09-18 05:40:13 -04:00
2019-06-11 12:21:38 +03:00
// FIXME(eddyb) false positive, the lifetime parameter is used for `Key`/`Value`.
#[allow(unused_lifetimes)]
pub trait QueryConfig<'tcx> {
const NAME: QueryName;
2018-05-19 13:50:58 -04:00
const CATEGORY: ProfileCategory;
type Key: Eq + Hash + Clone + Debug;
type Value: Clone;
2018-06-13 16:44:43 +03:00
}
2018-12-03 01:14:35 +01:00
pub(crate) trait QueryAccessors<'tcx>: QueryConfig<'tcx> {
const ANON: bool;
const EVAL_ALWAYS: bool;
fn query(key: Self::Key) -> Query<'tcx>;
2018-04-27 12:08:54 +02:00
// Don't use this method to access query results, instead use the methods on TyCtxt
2019-06-12 14:39:12 +02:00
fn query_cache<'a>(tcx: TyCtxt<'tcx>) -> &'a Sharded<QueryCache<'tcx, Self>>;
2019-06-14 00:48:52 +03:00
fn to_dep_node(tcx: TyCtxt<'tcx>, key: &Self::Key) -> DepNode;
fn dep_kind() -> DepKind;
2018-04-27 12:08:54 +02:00
// Don't use this method to compute query results, instead use the methods on TyCtxt
2019-06-14 00:48:52 +03:00
fn compute(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value;
fn hash_result(
hcx: &mut StableHashingContext<'_>,
result: &Self::Value
) -> Option<Fingerprint>;
2019-06-14 00:48:52 +03:00
fn handle_cycle_error(tcx: TyCtxt<'tcx>, error: CycleError<'tcx>) -> Self::Value;
2017-09-18 05:40:13 -04:00
}
2018-12-03 01:14:35 +01:00
pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
2019-06-14 00:48:52 +03:00
fn describe(tcx: TyCtxt<'_>, key: Self::Key) -> Cow<'static, str>;
#[inline]
2019-04-05 13:11:44 +02:00
fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
false
}
2019-06-14 00:48:52 +03:00
fn try_load_from_disk(_: TyCtxt<'tcx>, _: SerializedDepNodeIndex) -> Option<Self::Value> {
bug!("QueryDescription::load_from_disk() called for an unsupported query.")
}
2017-09-18 05:40:13 -04:00
}
impl<'tcx, M: QueryAccessors<'tcx, Key = DefId>> QueryDescription<'tcx> for M {
2019-06-14 00:48:52 +03:00
default fn describe(tcx: TyCtxt<'_>, def_id: DefId) -> Cow<'static, str> {
if !tcx.sess.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(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
false
}
2019-09-19 00:42:50 +02:00
default fn try_load_from_disk(
_: TyCtxt<'tcx>,
_: SerializedDepNodeIndex,
) -> Option<Self::Value> {
2019-09-19 00:14:02 +02:00
bug!("QueryDescription::load_from_disk() called for an unsupported query.")
}
2017-09-18 05:40:13 -04:00
}
impl<'tcx> QueryDescription<'tcx> for queries::analysis<'tcx> {
2019-06-14 00:48:52 +03:00
fn describe(_tcx: TyCtxt<'_>, _: CrateNum) -> Cow<'static, str> {
"running analysis passes on this crate".into()
}
}