]> git.proxmox.com Git - rustc.git/blame - src/librustc/middle/cfg/mod.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / librustc / middle / cfg / mod.rs
CommitLineData
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
d9579d0f 14use rustc_data_structures::graph;
1a4d82fc
JJ
15use middle::ty;
16use syntax::ast;
e9174d1e 17use rustc_front::hir;
1a4d82fc
JJ
18
19mod construct;
20pub mod graphviz;
21
22pub struct CFG {
1a4d82fc
JJ
23 pub graph: CFGGraph,
24 pub entry: CFGIndex,
25 pub exit: CFGIndex,
26}
27
d9579d0f 28#[derive(Copy, Clone, Debug, PartialEq)]
c34b1796
AL
29pub enum CFGNodeData {
30 AST(ast::NodeId),
31 Entry,
32 Exit,
33 Dummy,
34 Unreachable,
35}
36
37impl CFGNodeData {
38 pub fn id(&self) -> ast::NodeId {
39 if let CFGNodeData::AST(id) = *self {
40 id
41 } else {
42 ast::DUMMY_NODE_ID
43 }
44 }
1a4d82fc
JJ
45}
46
d9579d0f 47#[derive(Debug)]
1a4d82fc
JJ
48pub struct CFGEdgeData {
49 pub exiting_scopes: Vec<ast::NodeId>
50}
51
52pub type CFGIndex = graph::NodeIndex;
53
54pub type CFGGraph = graph::Graph<CFGNodeData, CFGEdgeData>;
55
56pub type CFGNode = graph::Node<CFGNodeData>;
57
58pub type CFGEdge = graph::Edge<CFGEdgeData>;
59
60impl CFG {
61 pub fn new(tcx: &ty::ctxt,
e9174d1e 62 blk: &hir::Block) -> CFG {
1a4d82fc
JJ
63 construct::construct(tcx, blk)
64 }
65
66 pub fn node_is_reachable(&self, id: ast::NodeId) -> bool {
62682a34
SL
67 self.graph.depth_traverse(self.entry)
68 .any(|idx| self.graph.node_data(idx).id() == id)
1a4d82fc
JJ
69 }
70}