]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/cfg/mod.rs
Merge pull request #20510 from tshepang/patch-6
[rust.git] / src / librustc / middle / cfg / mod.rs
1 // Copyright 2012 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 //! Module that constructs a control-flow graph representing an item.
12 //! Uses `Graph` as the underlying representation.
13
14 use middle::graph;
15 use middle::ty;
16 use syntax::ast;
17 use util::nodemap::NodeMap;
18
19 mod construct;
20 pub mod graphviz;
21
22 pub struct CFG {
23     pub exit_map: NodeMap<CFGIndex>,
24     pub graph: CFGGraph,
25     pub entry: CFGIndex,
26     pub exit: CFGIndex,
27 }
28
29 #[derive(Copy)]
30 pub struct CFGNodeData {
31     pub id: ast::NodeId
32 }
33
34 pub struct CFGEdgeData {
35     pub exiting_scopes: Vec<ast::NodeId>
36 }
37
38 pub type CFGIndex = graph::NodeIndex;
39
40 pub type CFGGraph = graph::Graph<CFGNodeData, CFGEdgeData>;
41
42 pub type CFGNode = graph::Node<CFGNodeData>;
43
44 pub type CFGEdge = graph::Edge<CFGEdgeData>;
45
46 impl CFG {
47     pub fn new(tcx: &ty::ctxt,
48                blk: &ast::Block) -> CFG {
49         construct::construct(tcx, blk)
50     }
51
52     pub fn node_is_reachable(&self, id: ast::NodeId) -> bool {
53         self.graph.depth_traverse(self.entry).any(|node| node.id == id)
54     }
55 }