rust/src/librustc_query_system/query/config.rs

118 lines
3.8 KiB
Rust
Raw Normal View History

2020-03-08 17:24:56 +01:00
//! Query configuration and description traits.
2019-02-05 11:20:45 -06:00
use crate::dep_graph::SerializedDepNodeIndex;
2020-03-19 14:13:31 +01:00
use crate::dep_graph::{DepContext, DepGraph, DepNode};
use crate::query::caches::QueryCache;
use crate::query::job::{QueryJobId, QueryJobInfo};
use crate::query::plumbing::CycleError;
use crate::query::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 rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::HashStable;
2020-03-19 18:31:17 +01:00
use rustc_data_structures::sync::Lock;
use rustc_data_structures::thin_vec::ThinVec;
use rustc_errors::Diagnostic;
use rustc_session::Session;
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-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: Clone;
2018-06-13 16:44:43 +03:00
}
2020-03-18 21:02:02 +01:00
pub trait QueryContext: DepContext {
type Query: Clone + HashStable<Self::StableHashingContext>;
/// Access the session.
fn session(&self) -> &Session;
/// Get string representation from DefPath.
fn def_path_str(&self, def_id: DefId) -> String;
2020-03-18 23:58:19 +01:00
2020-03-19 18:31:17 +01:00
/// Access the DepGraph.
fn dep_graph(&self) -> &DepGraph<Self::DepKind>;
2020-03-18 23:58:19 +01:00
/// Get the query information from the TLS context.
fn read_query_job<R>(&self, op: impl FnOnce(Option<QueryJobId<Self::DepKind>>) -> R) -> R;
fn try_collect_active_jobs(
&self,
) -> Option<FxHashMap<QueryJobId<Self::DepKind>, QueryJobInfo<Self>>>;
2020-03-19 18:31:17 +01:00
/// Executes a job by changing the `ImplicitCtxt` to point to the
/// new query job while it executes. It returns the diagnostics
/// captured during execution and the actual result.
fn start_query<R>(
&self,
token: QueryJobId<Self::DepKind>,
diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
compute: impl FnOnce(Self) -> R,
) -> R;
2020-03-08 18:20:18 +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;
2020-03-24 23:46:47 +01:00
type Cache: QueryCache<Key = Self::Key, 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
}
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> {
if !tcx.session().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
}