summary refs log tree commit diff
path: root/src/librustc/dep_graph/query.rs
blob: 74a054acb4fa05c98aabf3c5a4afab035626ce3e (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
64
65
66
67
68
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use rustc_data_structures::fnv::FnvHashMap;
use rustc_data_structures::graph::{Graph, NodeIndex};

use super::DepNode;

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

impl DepGraphQuery {
    pub fn new(nodes: &[DepNode], edges: &[(DepNode, DepNode)]) -> DepGraphQuery {
        let mut graph = Graph::new();
        let mut indices = FnvHashMap();
        for node in nodes {
            indices.insert(node.clone(), graph.next_node_index());
            graph.add_node(node.clone());
        }

        for &(ref source, ref target) in edges {
            let source = indices[source];
            let target = indices[target];
            graph.add_edge(source, target, ());
        }

        DepGraphQuery {
            graph: graph,
            indices: indices
        }
    }

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

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

    /// All nodes reachable from `node`. In other words, things that
    /// will have to be recomputed if `node` changes.
    pub fn dependents(&self, node: DepNode) -> Vec<DepNode> {
        if let Some(&index) = self.indices.get(&node) {
            self.graph.depth_traverse(index)
                      .map(|dependent_node| self.graph.node_data(dependent_node).clone())
                      .collect()
        } else {
            vec![]
        }
    }
}