]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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; | |
1a4d82fc JJ |
17 | |
18 | mod construct; | |
19 | pub mod graphviz; | |
20 | ||
21 | pub struct CFG { | |
1a4d82fc JJ |
22 | pub graph: CFGGraph, |
23 | pub entry: CFGIndex, | |
24 | pub exit: CFGIndex, | |
25 | } | |
26 | ||
c34b1796 AL |
27 | #[derive(Copy, Clone, PartialEq)] |
28 | pub enum CFGNodeData { | |
29 | AST(ast::NodeId), | |
30 | Entry, | |
31 | Exit, | |
32 | Dummy, | |
33 | Unreachable, | |
34 | } | |
35 | ||
36 | impl CFGNodeData { | |
37 | pub fn id(&self) -> ast::NodeId { | |
38 | if let CFGNodeData::AST(id) = *self { | |
39 | id | |
40 | } else { | |
41 | ast::DUMMY_NODE_ID | |
42 | } | |
43 | } | |
1a4d82fc JJ |
44 | } |
45 | ||
46 | pub struct CFGEdgeData { | |
47 | pub exiting_scopes: Vec<ast::NodeId> | |
48 | } | |
49 | ||
50 | pub type CFGIndex = graph::NodeIndex; | |
51 | ||
52 | pub type CFGGraph = graph::Graph<CFGNodeData, CFGEdgeData>; | |
53 | ||
54 | pub type CFGNode = graph::Node<CFGNodeData>; | |
55 | ||
56 | pub type CFGEdge = graph::Edge<CFGEdgeData>; | |
57 | ||
58 | impl CFG { | |
59 | pub fn new(tcx: &ty::ctxt, | |
60 | blk: &ast::Block) -> CFG { | |
61 | construct::construct(tcx, blk) | |
62 | } | |
63 | ||
64 | pub fn node_is_reachable(&self, id: ast::NodeId) -> bool { | |
c34b1796 | 65 | self.graph.depth_traverse(self.entry).any(|node| node.id() == id) |
1a4d82fc JJ |
66 | } |
67 | } |