2013-05-10 13:10:35 -04:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
//! Module that constructs a control-flow graph representing an item.
|
|
|
|
//! Uses `Graph` as the underlying representation.
|
2013-05-10 13:10:35 -04:00
|
|
|
|
|
|
|
use middle::graph;
|
|
|
|
use middle::ty;
|
|
|
|
use syntax::ast;
|
2014-02-28 14:34:26 -08:00
|
|
|
use util::nodemap::NodeMap;
|
2013-05-10 13:10:35 -04:00
|
|
|
|
|
|
|
mod construct;
|
2014-04-17 21:00:08 +02:00
|
|
|
pub mod graphviz;
|
2013-05-10 13:10:35 -04:00
|
|
|
|
|
|
|
pub struct CFG {
|
2014-04-17 21:00:08 +02:00
|
|
|
pub exit_map: NodeMap<CFGIndex>,
|
|
|
|
pub graph: CFGGraph,
|
|
|
|
pub entry: CFGIndex,
|
|
|
|
pub exit: CFGIndex,
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Copy)]
|
2013-05-10 13:10:35 -04:00
|
|
|
pub struct CFGNodeData {
|
2014-04-17 21:00:08 +02:00
|
|
|
pub id: ast::NodeId
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CFGEdgeData {
|
2014-04-17 21:00:08 +02:00
|
|
|
pub exiting_scopes: Vec<ast::NodeId>
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type CFGIndex = graph::NodeIndex;
|
|
|
|
|
|
|
|
pub type CFGGraph = graph::Graph<CFGNodeData, CFGEdgeData>;
|
|
|
|
|
|
|
|
pub type CFGNode = graph::Node<CFGNodeData>;
|
|
|
|
|
|
|
|
pub type CFGEdge = graph::Edge<CFGEdgeData>;
|
|
|
|
|
|
|
|
impl CFG {
|
2014-03-06 05:07:47 +02:00
|
|
|
pub fn new(tcx: &ty::ctxt,
|
2013-07-19 07:38:55 +02:00
|
|
|
blk: &ast::Block) -> CFG {
|
2014-04-17 21:00:08 +02:00
|
|
|
construct::construct(tcx, blk)
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
2014-12-16 12:21:08 +13:00
|
|
|
|
|
|
|
pub fn node_is_reachable(&self, id: ast::NodeId) -> bool {
|
2014-12-17 13:24:35 +13:00
|
|
|
self.graph.depth_traverse(self.entry).any(|node| node.id == id)
|
2014-12-16 12:21:08 +13:00
|
|
|
}
|
2013-07-27 10:25:59 +02:00
|
|
|
}
|