]> git.lizzy.rs Git - rust.git/blob - src/librustc_data_structures/control_flow_graph/mod.rs
deconstruct the `ControlFlowGraph` trait into more granular traits
[rust.git] / src / librustc_data_structures / control_flow_graph / mod.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use super::indexed_vec::Idx;
12
13 pub mod dominators;
14 pub mod iterate;
15 mod reference;
16
17 #[cfg(test)]
18 mod test;
19
20 pub trait DirectedGraph {
21     type Node: Idx;
22 }
23
24 pub trait WithNumNodes: DirectedGraph {
25     fn num_nodes(&self) -> usize;
26 }
27
28 pub trait WithSuccessors: DirectedGraph
29 where
30     Self: for<'graph> GraphSuccessors<'graph, Item = <Self as DirectedGraph>::Node>,
31 {
32     fn successors<'graph>(
33         &'graph self,
34         node: Self::Node,
35     ) -> <Self as GraphSuccessors<'graph>>::Iter;
36 }
37
38 pub trait GraphSuccessors<'graph> {
39     type Item;
40     type Iter: Iterator<Item = Self::Item>;
41 }
42
43 pub trait WithPredecessors: DirectedGraph
44 where
45     Self: for<'graph> GraphPredecessors<'graph, Item = <Self as DirectedGraph>::Node>,
46 {
47     fn predecessors<'graph>(
48         &'graph self,
49         node: Self::Node,
50     ) -> <Self as GraphPredecessors<'graph>>::Iter;
51 }
52
53 pub trait GraphPredecessors<'graph> {
54     type Item;
55     type Iter: Iterator<Item = Self::Item>;
56 }
57
58 pub trait WithStartNode: DirectedGraph {
59     fn start_node(&self) -> Self::Node;
60 }
61
62 pub trait ControlFlowGraph:
63     DirectedGraph + WithStartNode + WithPredecessors + WithStartNode + WithSuccessors + WithNumNodes
64 {
65     // convenient trait
66 }
67
68 impl<T> ControlFlowGraph for T
69 where
70     T: DirectedGraph
71         + WithStartNode
72         + WithPredecessors
73         + WithStartNode
74         + WithSuccessors
75         + WithNumNodes,
76 {
77 }