summary refs log tree commit diff
path: root/compiler/rustc_query_system/src/dep_graph/query.rs
blob: cc25d08cb54f43797fe4f526ed772db7b3e8c833 (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
63
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::graph::implementation::{Direction, Graph, NodeIndex, INCOMING};

use super::{DepKind, DepNode};

pub struct DepGraphQuery<K> {
    pub graph: Graph<DepNode<K>, ()>,
    pub indices: FxHashMap<DepNode<K>, NodeIndex>,
}

impl<K: DepKind> DepGraphQuery<K> {
    pub fn new(
        nodes: &[DepNode<K>],
        edge_list_indices: &[(usize, usize)],
        edge_list_data: &[usize],
    ) -> DepGraphQuery<K> {
        let mut graph = Graph::with_capacity(nodes.len(), edge_list_data.len());
        let mut indices = FxHashMap::default();
        for node in nodes {
            indices.insert(*node, graph.add_node(*node));
        }

        for (source, &(start, end)) in edge_list_indices.iter().enumerate() {
            for &target in &edge_list_data[start..end] {
                let source = indices[&nodes[source]];
                let target = indices[&nodes[target]];
                graph.add_edge(source, target, ());
            }
        }

        DepGraphQuery { graph, indices }
    }

    pub fn contains_node(&self, node: &DepNode<K>) -> bool {
        self.indices.contains_key(&node)
    }

    pub fn nodes(&self) -> Vec<&DepNode<K>> {
        self.graph.all_nodes().iter().map(|n| &n.data).collect()
    }

    pub fn edges(&self) -> Vec<(&DepNode<K>, &DepNode<K>)> {
        self.graph
            .all_edges()
            .iter()
            .map(|edge| (edge.source(), edge.target()))
            .map(|(s, t)| (self.graph.node_data(s), self.graph.node_data(t)))
            .collect()
    }

    fn reachable_nodes(&self, node: &DepNode<K>, direction: Direction) -> Vec<&DepNode<K>> {
        if let Some(&index) = self.indices.get(node) {
            self.graph.depth_traverse(index, direction).map(|s| self.graph.node_data(s)).collect()
        } else {
            vec![]
        }
    }

    /// All nodes that can reach `node`.
    pub fn transitive_predecessors(&self, node: &DepNode<K>) -> Vec<&DepNode<K>> {
        self.reachable_nodes(node, INCOMING)
    }
}