summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_query_system/src/query/config.rs
blob: d563738734657e3c64171cde5bf1f23608780888 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Query configuration and description traits.

use crate::dep_graph::{DepNode, DepNodeParams, SerializedDepNodeIndex};
use crate::error::HandleCycleError;
use crate::ich::StableHashingContext;
use crate::query::caches::QueryCache;
use crate::query::{QueryContext, QueryState};

use rustc_data_structures::fingerprint::Fingerprint;
use std::fmt::Debug;
use std::hash::Hash;

pub type HashResult<Qcx, Q> =
    Option<fn(&mut StableHashingContext<'_>, &<Q as QueryConfig<Qcx>>::Value) -> Fingerprint>;

pub type TryLoadFromDisk<Qcx, Q> =
    Option<fn(Qcx, SerializedDepNodeIndex) -> Option<<Q as QueryConfig<Qcx>>::Value>>;

pub trait QueryConfig<Qcx: QueryContext> {
    const NAME: &'static str;

    // `Key` and `Value` are `Copy` instead of `Clone` to ensure copying them stays cheap,
    // but it isn't necessary.
    type Key: DepNodeParams<Qcx::DepContext> + Eq + Hash + Copy + Debug;
    type Value: Debug + Copy;

    type Cache: QueryCache<Key = Self::Key, Value = Self::Value>;

    // Don't use this method to access query results, instead use the methods on TyCtxt
    fn query_state<'a>(tcx: Qcx) -> &'a QueryState<Self::Key, Qcx::DepKind>
    where
        Qcx: 'a;

    // Don't use this method to access query results, instead use the methods on TyCtxt
    fn query_cache<'a>(tcx: Qcx) -> &'a Self::Cache
    where
        Qcx: 'a;

    fn cache_on_disk(tcx: Qcx::DepContext, key: &Self::Key) -> bool;

    // Don't use this method to compute query results, instead use the methods on TyCtxt
    fn execute_query(tcx: Qcx::DepContext, k: Self::Key) -> Self::Value;

    fn compute(tcx: Qcx, key: Self::Key) -> Self::Value;

    fn try_load_from_disk(qcx: Qcx, idx: &Self::Key) -> TryLoadFromDisk<Qcx, Self>;

    const ANON: bool;
    const EVAL_ALWAYS: bool;
    const DEPTH_LIMIT: bool;
    const FEEDABLE: bool;

    const DEP_KIND: Qcx::DepKind;
    const HANDLE_CYCLE_ERROR: HandleCycleError;

    const HASH_RESULT: HashResult<Qcx, Self>;

    // Just here for convernience and checking that the key matches the kind, don't override this.
    fn construct_dep_node(tcx: Qcx::DepContext, key: &Self::Key) -> DepNode<Qcx::DepKind> {
        DepNode::construct(tcx, Self::DEP_KIND, key)
    }
}