2014-07-27 07:50:46 -04:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-05-10 13:10:35 -04:00
|
|
|
// 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.
|
|
|
|
|
2015-04-07 06:12:13 -04:00
|
|
|
use rustc_data_structures::graph;
|
2013-05-10 13:10:35 -04:00
|
|
|
use middle::cfg::*;
|
2014-05-14 15:31:30 -04:00
|
|
|
use middle::def;
|
2015-02-19 17:54:41 +01:00
|
|
|
use middle::pat_util;
|
2014-11-18 14:22:59 +01:00
|
|
|
use middle::region::CodeExtent;
|
2013-05-10 13:10:35 -04:00
|
|
|
use middle::ty;
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::ast_util;
|
2014-09-07 20:09:06 +03:00
|
|
|
use syntax::ptr::P;
|
2013-05-10 13:10:35 -04:00
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
struct CFGBuilder<'a, 'tcx: 'a> {
|
|
|
|
tcx: &'a ty::ctxt<'tcx>,
|
2013-05-10 13:10:35 -04:00
|
|
|
graph: CFGGraph,
|
2014-05-09 00:07:57 +02:00
|
|
|
fn_exit: CFGIndex,
|
|
|
|
loop_scopes: Vec<LoopScope>,
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2013-05-10 13:10:35 -04:00
|
|
|
struct LoopScope {
|
2013-07-27 10:25:59 +02:00
|
|
|
loop_id: ast::NodeId, // id of loop/while node
|
2013-05-10 13:10:35 -04:00
|
|
|
continue_index: CFGIndex, // where to go on a `loop`
|
|
|
|
break_index: CFGIndex, // where to go on a `break
|
|
|
|
}
|
|
|
|
|
2014-03-06 05:07:47 +02:00
|
|
|
pub fn construct(tcx: &ty::ctxt,
|
2013-07-19 07:38:55 +02:00
|
|
|
blk: &ast::Block) -> CFG {
|
2014-05-09 00:07:57 +02:00
|
|
|
let mut graph = graph::Graph::new();
|
2015-02-19 15:27:25 +01:00
|
|
|
let entry = graph.add_node(CFGNodeData::Entry);
|
2014-05-09 00:07:57 +02:00
|
|
|
|
|
|
|
// `fn_exit` is target of return exprs, which lies somewhere
|
|
|
|
// outside input `blk`. (Distinguishing `fn_exit` and `block_exit`
|
|
|
|
// also resolves chicken-and-egg problem that arises if you try to
|
|
|
|
// have return exprs jump to `block_exit` during construction.)
|
2015-02-19 15:27:25 +01:00
|
|
|
let fn_exit = graph.add_node(CFGNodeData::Exit);
|
2014-05-09 00:07:57 +02:00
|
|
|
let block_exit;
|
|
|
|
|
2013-05-10 13:10:35 -04:00
|
|
|
let mut cfg_builder = CFGBuilder {
|
2014-05-09 00:07:57 +02:00
|
|
|
graph: graph,
|
|
|
|
fn_exit: fn_exit,
|
2013-05-10 13:10:35 -04:00
|
|
|
tcx: tcx,
|
2014-03-04 10:02:49 -08:00
|
|
|
loop_scopes: Vec::new()
|
2013-05-10 13:10:35 -04:00
|
|
|
};
|
2014-05-09 00:07:57 +02:00
|
|
|
block_exit = cfg_builder.block(blk, entry);
|
|
|
|
cfg_builder.add_contained_edge(block_exit, fn_exit);
|
2015-02-19 15:33:53 +01:00
|
|
|
let CFGBuilder {graph, ..} = cfg_builder;
|
|
|
|
CFG {graph: graph,
|
2013-05-10 13:10:35 -04:00
|
|
|
entry: entry,
|
2014-05-09 00:07:57 +02:00
|
|
|
exit: fn_exit}
|
|
|
|
}
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
|
2013-07-19 07:38:55 +02:00
|
|
|
fn block(&mut self, blk: &ast::Block, pred: CFGIndex) -> CFGIndex {
|
2013-05-10 13:10:35 -04:00
|
|
|
let mut stmts_exit = pred;
|
2015-01-31 12:20:46 -05:00
|
|
|
for stmt in &blk.stmts {
|
2014-09-07 20:09:06 +03:00
|
|
|
stmts_exit = self.stmt(&**stmt, stmts_exit);
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-09-07 20:09:06 +03:00
|
|
|
let expr_exit = self.opt_expr(&blk.expr, stmts_exit);
|
2013-05-10 13:10:35 -04:00
|
|
|
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_ast_node(blk.id, &[expr_exit])
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-09-07 20:09:06 +03:00
|
|
|
fn stmt(&mut self, stmt: &ast::Stmt, pred: CFGIndex) -> CFGIndex {
|
2013-05-10 13:10:35 -04:00
|
|
|
match stmt.node {
|
2014-08-27 21:46:52 -04:00
|
|
|
ast::StmtDecl(ref decl, id) => {
|
|
|
|
let exit = self.decl(&**decl, pred);
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_ast_node(id, &[exit])
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-08-27 21:46:52 -04:00
|
|
|
ast::StmtExpr(ref expr, id) | ast::StmtSemi(ref expr, id) => {
|
2014-09-07 20:09:06 +03:00
|
|
|
let exit = self.expr(&**expr, pred);
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_ast_node(id, &[exit])
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2013-11-28 12:22:53 -08:00
|
|
|
ast::StmtMac(..) => {
|
2013-05-10 13:10:35 -04:00
|
|
|
self.tcx.sess.span_bug(stmt.span, "unexpanded macro");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 10:15:33 -07:00
|
|
|
fn decl(&mut self, decl: &ast::Decl, pred: CFGIndex) -> CFGIndex {
|
2013-05-10 13:10:35 -04:00
|
|
|
match decl.node {
|
2014-05-16 10:15:33 -07:00
|
|
|
ast::DeclLocal(ref local) => {
|
2014-09-07 20:09:06 +03:00
|
|
|
let init_exit = self.opt_expr(&local.init, pred);
|
2014-05-16 10:15:33 -07:00
|
|
|
self.pat(&*local.pat, init_exit)
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2013-09-02 03:45:37 +02:00
|
|
|
ast::DeclItem(_) => {
|
2013-05-10 13:10:35 -04:00
|
|
|
pred
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 10:15:33 -07:00
|
|
|
fn pat(&mut self, pat: &ast::Pat, pred: CFGIndex) -> CFGIndex {
|
2013-05-10 13:10:35 -04:00
|
|
|
match pat.node {
|
2013-09-02 03:45:37 +02:00
|
|
|
ast::PatIdent(_, _, None) |
|
|
|
|
ast::PatEnum(_, None) |
|
2015-03-25 10:53:28 -06:00
|
|
|
ast::PatQPath(..) |
|
2013-11-28 12:22:53 -08:00
|
|
|
ast::PatLit(..) |
|
|
|
|
ast::PatRange(..) |
|
2014-08-06 17:04:44 +02:00
|
|
|
ast::PatWild(_) => {
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_ast_node(pat.id, &[pred])
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-05-16 10:15:33 -07:00
|
|
|
ast::PatBox(ref subpat) |
|
2014-12-05 15:56:25 -08:00
|
|
|
ast::PatRegion(ref subpat, _) |
|
2014-05-16 10:15:33 -07:00
|
|
|
ast::PatIdent(_, _, Some(ref subpat)) => {
|
|
|
|
let subpat_exit = self.pat(&**subpat, pred);
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_ast_node(pat.id, &[subpat_exit])
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2013-09-02 03:45:37 +02:00
|
|
|
ast::PatEnum(_, Some(ref subpats)) |
|
|
|
|
ast::PatTup(ref subpats) => {
|
2014-09-07 20:09:06 +03:00
|
|
|
let pats_exit = self.pats_all(subpats.iter(), pred);
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_ast_node(pat.id, &[pats_exit])
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2013-09-02 03:45:37 +02:00
|
|
|
ast::PatStruct(_, ref subpats, _) => {
|
2013-05-10 13:10:35 -04:00
|
|
|
let pats_exit =
|
2014-10-06 13:36:53 +13:00
|
|
|
self.pats_all(subpats.iter().map(|f| &f.node.pat), pred);
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_ast_node(pat.id, &[pats_exit])
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2013-09-02 03:45:37 +02:00
|
|
|
ast::PatVec(ref pre, ref vec, ref post) => {
|
2014-09-07 20:09:06 +03:00
|
|
|
let pre_exit = self.pats_all(pre.iter(), pred);
|
|
|
|
let vec_exit = self.pats_all(vec.iter(), pre_exit);
|
|
|
|
let post_exit = self.pats_all(post.iter(), vec_exit);
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_ast_node(pat.id, &[post_exit])
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
2014-05-19 13:29:41 -07:00
|
|
|
|
|
|
|
ast::PatMac(_) => {
|
|
|
|
self.tcx.sess.span_bug(pat.span, "unexpanded macro");
|
|
|
|
}
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:26:38 -05:00
|
|
|
fn pats_all<'b, I: Iterator<Item=&'b P<ast::Pat>>>(&mut self,
|
2014-09-07 20:09:06 +03:00
|
|
|
pats: I,
|
|
|
|
pred: CFGIndex) -> CFGIndex {
|
2013-05-10 13:10:35 -04:00
|
|
|
//! Handles case where all of the patterns must match.
|
2014-09-07 20:09:06 +03:00
|
|
|
pats.fold(pred, |pred, pat| self.pat(&**pat, pred))
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-09-07 20:09:06 +03:00
|
|
|
fn expr(&mut self, expr: &ast::Expr, pred: CFGIndex) -> CFGIndex {
|
2013-05-10 13:10:35 -04:00
|
|
|
match expr.node {
|
2014-05-16 10:15:33 -07:00
|
|
|
ast::ExprBlock(ref blk) => {
|
|
|
|
let blk_exit = self.block(&**blk, pred);
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_ast_node(expr.id, &[blk_exit])
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-05-16 10:15:33 -07:00
|
|
|
ast::ExprIf(ref cond, ref then, None) => {
|
2013-05-10 13:10:35 -04:00
|
|
|
//
|
|
|
|
// [pred]
|
|
|
|
// |
|
|
|
|
// v 1
|
|
|
|
// [cond]
|
|
|
|
// |
|
|
|
|
// / \
|
|
|
|
// / \
|
|
|
|
// v 2 *
|
|
|
|
// [then] |
|
|
|
|
// | |
|
|
|
|
// v 3 v 4
|
|
|
|
// [..expr..]
|
|
|
|
//
|
2014-09-07 20:09:06 +03:00
|
|
|
let cond_exit = self.expr(&**cond, pred); // 1
|
2014-05-16 10:15:33 -07:00
|
|
|
let then_exit = self.block(&**then, cond_exit); // 2
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_ast_node(expr.id, &[cond_exit, then_exit]) // 3,4
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-05-16 10:15:33 -07:00
|
|
|
ast::ExprIf(ref cond, ref then, Some(ref otherwise)) => {
|
2013-05-10 13:10:35 -04:00
|
|
|
//
|
|
|
|
// [pred]
|
|
|
|
// |
|
|
|
|
// v 1
|
|
|
|
// [cond]
|
|
|
|
// |
|
|
|
|
// / \
|
|
|
|
// / \
|
|
|
|
// v 2 v 3
|
|
|
|
// [then][otherwise]
|
|
|
|
// | |
|
|
|
|
// v 4 v 5
|
|
|
|
// [..expr..]
|
|
|
|
//
|
2014-09-07 20:09:06 +03:00
|
|
|
let cond_exit = self.expr(&**cond, pred); // 1
|
2014-05-16 10:15:33 -07:00
|
|
|
let then_exit = self.block(&**then, cond_exit); // 2
|
2014-09-07 20:09:06 +03:00
|
|
|
let else_exit = self.expr(&**otherwise, cond_exit); // 3
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_ast_node(expr.id, &[then_exit, else_exit]) // 4, 5
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-08-27 21:34:03 -07:00
|
|
|
ast::ExprIfLet(..) => {
|
|
|
|
self.tcx.sess.span_bug(expr.span, "non-desugared ExprIfLet");
|
|
|
|
}
|
2014-08-24 19:08:48 -07:00
|
|
|
|
2014-07-25 20:12:51 -04:00
|
|
|
ast::ExprWhile(ref cond, ref body, _) => {
|
2013-05-10 13:10:35 -04:00
|
|
|
//
|
|
|
|
// [pred]
|
|
|
|
// |
|
|
|
|
// v 1
|
|
|
|
// [loopback] <--+ 5
|
|
|
|
// | |
|
|
|
|
// v 2 |
|
|
|
|
// +-----[cond] |
|
|
|
|
// | | |
|
|
|
|
// | v 4 |
|
|
|
|
// | [body] -----+
|
|
|
|
// v 3
|
|
|
|
// [expr]
|
|
|
|
//
|
2014-07-21 20:54:28 -07:00
|
|
|
// Note that `break` and `continue` statements
|
2013-05-10 13:10:35 -04:00
|
|
|
// may cause additional edges.
|
|
|
|
|
2013-07-30 16:47:22 -07:00
|
|
|
// Is the condition considered part of the loop?
|
2014-11-17 21:39:01 +13:00
|
|
|
let loopback = self.add_dummy_node(&[pred]); // 1
|
|
|
|
let cond_exit = self.expr(&**cond, loopback); // 2
|
2015-02-19 15:27:25 +01:00
|
|
|
let expr_exit = self.add_ast_node(expr.id, &[cond_exit]); // 3
|
2013-05-10 13:10:35 -04:00
|
|
|
self.loop_scopes.push(LoopScope {
|
|
|
|
loop_id: expr.id,
|
|
|
|
continue_index: loopback,
|
|
|
|
break_index: expr_exit
|
|
|
|
});
|
2014-05-16 10:15:33 -07:00
|
|
|
let body_exit = self.block(&**body, cond_exit); // 4
|
|
|
|
self.add_contained_edge(body_exit, loopback); // 5
|
2014-05-20 18:49:19 +02:00
|
|
|
self.loop_scopes.pop();
|
2013-05-10 13:10:35 -04:00
|
|
|
expr_exit
|
|
|
|
}
|
|
|
|
|
2014-10-03 00:41:24 -04:00
|
|
|
ast::ExprWhileLet(..) => {
|
|
|
|
self.tcx.sess.span_bug(expr.span, "non-desugared ExprWhileLet");
|
|
|
|
}
|
|
|
|
|
2015-01-22 16:59:23 -05:00
|
|
|
ast::ExprForLoop(..) => {
|
2015-01-08 18:04:26 -05:00
|
|
|
self.tcx.sess.span_bug(expr.span, "non-desugared ExprForLoop");
|
2014-07-21 20:54:28 -07:00
|
|
|
}
|
2013-07-29 17:25:00 -07:00
|
|
|
|
2014-05-16 10:15:33 -07:00
|
|
|
ast::ExprLoop(ref body, _) => {
|
2013-05-10 13:10:35 -04:00
|
|
|
//
|
|
|
|
// [pred]
|
|
|
|
// |
|
|
|
|
// v 1
|
|
|
|
// [loopback] <---+
|
|
|
|
// | 4 |
|
|
|
|
// v 3 |
|
|
|
|
// [body] ------+
|
|
|
|
//
|
|
|
|
// [expr] 2
|
|
|
|
//
|
|
|
|
// Note that `break` and `loop` statements
|
|
|
|
// may cause additional edges.
|
|
|
|
|
2014-11-17 21:39:01 +13:00
|
|
|
let loopback = self.add_dummy_node(&[pred]); // 1
|
2015-02-19 15:27:25 +01:00
|
|
|
let expr_exit = self.add_ast_node(expr.id, &[]); // 2
|
2013-05-10 13:10:35 -04:00
|
|
|
self.loop_scopes.push(LoopScope {
|
|
|
|
loop_id: expr.id,
|
|
|
|
continue_index: loopback,
|
|
|
|
break_index: expr_exit,
|
|
|
|
});
|
2014-05-16 10:15:33 -07:00
|
|
|
let body_exit = self.block(&**body, loopback); // 3
|
|
|
|
self.add_contained_edge(body_exit, loopback); // 4
|
2013-05-10 13:10:35 -04:00
|
|
|
self.loop_scopes.pop();
|
|
|
|
expr_exit
|
|
|
|
}
|
|
|
|
|
2014-08-25 14:55:00 -07:00
|
|
|
ast::ExprMatch(ref discr, ref arms, _) => {
|
2015-02-19 17:54:41 +01:00
|
|
|
self.match_(expr.id, &discr, &arms, pred)
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2015-01-13 14:24:37 +11:00
|
|
|
ast::ExprBinary(op, ref l, ref r) if ast_util::lazy_binop(op.node) => {
|
2013-05-10 13:10:35 -04:00
|
|
|
//
|
|
|
|
// [pred]
|
|
|
|
// |
|
|
|
|
// v 1
|
|
|
|
// [l]
|
|
|
|
// |
|
|
|
|
// / \
|
|
|
|
// / \
|
|
|
|
// v 2 *
|
|
|
|
// [r] |
|
|
|
|
// | |
|
|
|
|
// v 3 v 4
|
|
|
|
// [..exit..]
|
|
|
|
//
|
2014-09-07 20:09:06 +03:00
|
|
|
let l_exit = self.expr(&**l, pred); // 1
|
|
|
|
let r_exit = self.expr(&**r, l_exit); // 2
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_ast_node(expr.id, &[l_exit, r_exit]) // 3,4
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-05-16 10:15:33 -07:00
|
|
|
ast::ExprRet(ref v) => {
|
2014-09-07 20:09:06 +03:00
|
|
|
let v_exit = self.opt_expr(v, pred);
|
2015-02-19 15:27:25 +01:00
|
|
|
let b = self.add_ast_node(expr.id, &[v_exit]);
|
2014-05-09 00:07:57 +02:00
|
|
|
self.add_returning_edge(expr, b);
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_unreachable_node()
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2013-09-02 03:45:37 +02:00
|
|
|
ast::ExprBreak(label) => {
|
2013-05-10 13:10:35 -04:00
|
|
|
let loop_scope = self.find_scope(expr, label);
|
2015-02-19 15:27:25 +01:00
|
|
|
let b = self.add_ast_node(expr.id, &[pred]);
|
2014-05-09 00:07:57 +02:00
|
|
|
self.add_exiting_edge(expr, b,
|
2013-05-10 13:10:35 -04:00
|
|
|
loop_scope, loop_scope.break_index);
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_unreachable_node()
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2013-09-02 03:45:37 +02:00
|
|
|
ast::ExprAgain(label) => {
|
2013-05-10 13:10:35 -04:00
|
|
|
let loop_scope = self.find_scope(expr, label);
|
2015-02-19 15:27:25 +01:00
|
|
|
let a = self.add_ast_node(expr.id, &[pred]);
|
2014-05-09 00:07:57 +02:00
|
|
|
self.add_exiting_edge(expr, a,
|
2013-05-10 13:10:35 -04:00
|
|
|
loop_scope, loop_scope.continue_index);
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_unreachable_node()
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-04-04 13:12:18 +03:00
|
|
|
ast::ExprVec(ref elems) => {
|
2014-09-07 20:09:06 +03:00
|
|
|
self.straightline(expr, pred, elems.iter().map(|e| &**e))
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-05-16 10:15:33 -07:00
|
|
|
ast::ExprCall(ref func, ref args) => {
|
2014-09-07 20:09:06 +03:00
|
|
|
self.call(expr, pred, &**func, args.iter().map(|e| &**e))
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-02-26 16:06:45 +02:00
|
|
|
ast::ExprMethodCall(_, _, ref args) => {
|
2015-01-17 16:15:52 -08:00
|
|
|
self.call(expr, pred, &*args[0], args[1..].iter().map(|e| &**e))
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-05-16 10:15:33 -07:00
|
|
|
ast::ExprIndex(ref l, ref r) |
|
2014-09-07 20:09:06 +03:00
|
|
|
ast::ExprBinary(_, ref l, ref r) if self.is_method_call(expr) => {
|
2014-09-14 20:27:36 -07:00
|
|
|
self.call(expr, pred, &**l, Some(&**r).into_iter())
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-12-15 13:17:11 +13:00
|
|
|
ast::ExprRange(ref start, ref end) => {
|
2014-12-18 17:55:04 +13:00
|
|
|
let fields = start.as_ref().map(|e| &**e).into_iter()
|
2014-12-15 13:17:11 +13:00
|
|
|
.chain(end.as_ref().map(|e| &**e).into_iter());
|
|
|
|
self.straightline(expr, pred, fields)
|
2014-12-13 18:41:02 +13:00
|
|
|
}
|
|
|
|
|
2014-09-07 20:09:06 +03:00
|
|
|
ast::ExprUnary(_, ref e) if self.is_method_call(expr) => {
|
|
|
|
self.call(expr, pred, &**e, None::<ast::Expr>.iter())
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2013-09-02 03:45:37 +02:00
|
|
|
ast::ExprTup(ref exprs) => {
|
2014-09-07 20:09:06 +03:00
|
|
|
self.straightline(expr, pred, exprs.iter().map(|e| &**e))
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-09-07 20:09:06 +03:00
|
|
|
ast::ExprStruct(_, ref fields, ref base) => {
|
2014-11-05 15:05:01 -08:00
|
|
|
let field_cfg = self.straightline(expr, pred, fields.iter().map(|f| &*f.expr));
|
|
|
|
self.opt_expr(base, field_cfg)
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-09-07 20:09:06 +03:00
|
|
|
ast::ExprRepeat(ref elem, ref count) => {
|
|
|
|
self.straightline(expr, pred, [elem, count].iter().map(|&e| &**e))
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-09-07 20:09:06 +03:00
|
|
|
ast::ExprAssign(ref l, ref r) |
|
|
|
|
ast::ExprAssignOp(_, ref l, ref r) => {
|
|
|
|
self.straightline(expr, pred, [r, l].iter().map(|&e| &**e))
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-12-16 14:30:30 +01:00
|
|
|
ast::ExprBox(Some(ref l), ref r) |
|
2014-09-07 20:09:06 +03:00
|
|
|
ast::ExprIndex(ref l, ref r) |
|
|
|
|
ast::ExprBinary(_, ref l, ref r) => { // NB: && and || handled earlier
|
|
|
|
self.straightline(expr, pred, [l, r].iter().map(|&e| &**e))
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-12-16 14:30:30 +01:00
|
|
|
ast::ExprBox(None, ref e) |
|
2014-09-07 20:09:06 +03:00
|
|
|
ast::ExprAddrOf(_, ref e) |
|
|
|
|
ast::ExprCast(ref e, _) |
|
|
|
|
ast::ExprUnary(_, ref e) |
|
|
|
|
ast::ExprParen(ref e) |
|
2014-11-23 12:14:35 +01:00
|
|
|
ast::ExprField(ref e, _) |
|
|
|
|
ast::ExprTupField(ref e, _) => {
|
2014-09-14 20:27:36 -07:00
|
|
|
self.straightline(expr, pred, Some(&**e).into_iter())
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2014-05-20 18:49:19 +02:00
|
|
|
ast::ExprInlineAsm(ref inline_asm) => {
|
|
|
|
let inputs = inline_asm.inputs.iter();
|
|
|
|
let outputs = inline_asm.outputs.iter();
|
|
|
|
let post_inputs = self.exprs(inputs.map(|a| {
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("cfg::construct InlineAsm id:{} input:{:?}", expr.id, a);
|
2014-09-07 20:09:06 +03:00
|
|
|
let &(_, ref expr) = a;
|
|
|
|
&**expr
|
2014-05-20 18:49:19 +02:00
|
|
|
}), pred);
|
|
|
|
let post_outputs = self.exprs(outputs.map(|a| {
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("cfg::construct InlineAsm id:{} output:{:?}", expr.id, a);
|
2014-09-07 20:09:06 +03:00
|
|
|
let &(_, ref expr, _) = a;
|
|
|
|
&**expr
|
2014-05-20 18:49:19 +02:00
|
|
|
}), post_inputs);
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_ast_node(expr.id, &[post_outputs])
|
2014-05-20 18:49:19 +02:00
|
|
|
}
|
|
|
|
|
2013-11-28 12:22:53 -08:00
|
|
|
ast::ExprMac(..) |
|
2014-11-19 11:18:17 -05:00
|
|
|
ast::ExprClosure(..) |
|
2013-11-28 12:22:53 -08:00
|
|
|
ast::ExprLit(..) |
|
2015-02-17 19:29:13 +02:00
|
|
|
ast::ExprPath(..) => {
|
2014-09-07 20:09:06 +03:00
|
|
|
self.straightline(expr, pred, None::<ast::Expr>.iter())
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:26:38 -05:00
|
|
|
fn call<'b, I: Iterator<Item=&'b ast::Expr>>(&mut self,
|
2014-09-07 20:09:06 +03:00
|
|
|
call_expr: &ast::Expr,
|
2013-05-10 13:10:35 -04:00
|
|
|
pred: CFGIndex,
|
2014-09-07 20:09:06 +03:00
|
|
|
func_or_rcvr: &ast::Expr,
|
|
|
|
args: I) -> CFGIndex {
|
2014-11-25 14:21:20 -05:00
|
|
|
let method_call = ty::MethodCall::expr(call_expr.id);
|
2014-11-06 12:25:16 -05:00
|
|
|
let return_ty = ty::ty_fn_ret(match self.tcx.method_map.borrow().get(&method_call) {
|
2014-10-24 21:14:37 +02:00
|
|
|
Some(method) => method.ty,
|
2015-01-02 15:30:26 -05:00
|
|
|
None => ty::expr_ty_adjusted(self.tcx, func_or_rcvr)
|
2014-10-24 21:14:37 +02:00
|
|
|
});
|
|
|
|
|
2013-05-10 13:10:35 -04:00
|
|
|
let func_or_rcvr_exit = self.expr(func_or_rcvr, pred);
|
2014-05-20 18:49:19 +02:00
|
|
|
let ret = self.straightline(call_expr, func_or_rcvr_exit, args);
|
2015-01-06 05:03:42 -05:00
|
|
|
if return_ty.diverges() {
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_unreachable_node()
|
2014-05-20 18:49:19 +02:00
|
|
|
} else {
|
|
|
|
ret
|
|
|
|
}
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2015-01-01 23:26:38 -05:00
|
|
|
fn exprs<'b, I: Iterator<Item=&'b ast::Expr>>(&mut self,
|
2014-11-06 09:32:37 -08:00
|
|
|
exprs: I,
|
2014-09-07 20:09:06 +03:00
|
|
|
pred: CFGIndex) -> CFGIndex {
|
2013-05-10 13:10:35 -04:00
|
|
|
//! Constructs graph for `exprs` evaluated in order
|
2014-05-20 18:49:19 +02:00
|
|
|
exprs.fold(pred, |p, e| self.expr(e, p))
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn opt_expr(&mut self,
|
2014-09-07 20:09:06 +03:00
|
|
|
opt_expr: &Option<P<ast::Expr>>,
|
2013-05-10 13:10:35 -04:00
|
|
|
pred: CFGIndex) -> CFGIndex {
|
|
|
|
//! Constructs graph for `opt_expr` evaluated, if Some
|
2014-09-07 20:09:06 +03:00
|
|
|
opt_expr.iter().fold(pred, |p, e| self.expr(&**e, p))
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2015-01-01 23:26:38 -05:00
|
|
|
fn straightline<'b, I: Iterator<Item=&'b ast::Expr>>(&mut self,
|
2014-09-07 20:09:06 +03:00
|
|
|
expr: &ast::Expr,
|
2013-05-10 13:10:35 -04:00
|
|
|
pred: CFGIndex,
|
2014-09-07 20:09:06 +03:00
|
|
|
subexprs: I) -> CFGIndex {
|
2013-05-10 13:10:35 -04:00
|
|
|
//! Handles case of an expression that evaluates `subexprs` in order
|
|
|
|
|
2014-09-07 20:09:06 +03:00
|
|
|
let subexprs_exit = self.exprs(subexprs, pred);
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_ast_node(expr.id, &[subexprs_exit])
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2015-02-19 17:54:41 +01:00
|
|
|
fn match_(&mut self, id: ast::NodeId, discr: &ast::Expr,
|
|
|
|
arms: &[ast::Arm], pred: CFGIndex) -> CFGIndex {
|
|
|
|
// The CFG for match expression is quite complex, so no ASCII
|
|
|
|
// art for it (yet).
|
|
|
|
//
|
|
|
|
// The CFG generated below matches roughly what trans puts
|
|
|
|
// out. Each pattern and guard is visited in parallel, with
|
|
|
|
// arms containing multiple patterns generating multiple nodes
|
|
|
|
// for the same guard expression. The guard expressions chain
|
|
|
|
// into each other from top to bottom, with a specific
|
|
|
|
// exception to allow some additional valid programs
|
|
|
|
// (explained below). Trans differs slightly in that the
|
|
|
|
// pattern matching may continue after a guard but the visible
|
|
|
|
// behaviour should be the same.
|
|
|
|
//
|
|
|
|
// What is going on is explained in further comments.
|
|
|
|
|
|
|
|
// Visit the discriminant expression
|
|
|
|
let discr_exit = self.expr(discr, pred);
|
|
|
|
|
|
|
|
// Add a node for the exit of the match expression as a whole.
|
|
|
|
let expr_exit = self.add_ast_node(id, &[]);
|
|
|
|
|
|
|
|
// Keep track of the previous guard expressions
|
|
|
|
let mut prev_guards = Vec::new();
|
|
|
|
// Track if the previous pattern contained bindings or wildcards
|
|
|
|
let mut prev_has_bindings = false;
|
|
|
|
|
|
|
|
for arm in arms {
|
|
|
|
// Add an exit node for when we've visited all the
|
|
|
|
// patterns and the guard (if there is one) in the arm.
|
|
|
|
let arm_exit = self.add_dummy_node(&[]);
|
|
|
|
|
|
|
|
for pat in &arm.pats {
|
|
|
|
// Visit the pattern, coming from the discriminant exit
|
|
|
|
let mut pat_exit = self.pat(&**pat, discr_exit);
|
|
|
|
|
|
|
|
// If there is a guard expression, handle it here
|
|
|
|
if let Some(ref guard) = arm.guard {
|
|
|
|
// Add a dummy node for the previous guard
|
|
|
|
// expression to target
|
|
|
|
let guard_start = self.add_dummy_node(&[pat_exit]);
|
|
|
|
// Visit the guard expression
|
|
|
|
let guard_exit = self.expr(&**guard, guard_start);
|
|
|
|
|
|
|
|
let this_has_bindings = pat_util::pat_contains_bindings_or_wild(
|
|
|
|
&self.tcx.def_map, &**pat);
|
|
|
|
|
|
|
|
// If both this pattern and the previous pattern
|
|
|
|
// were free of bindings, they must consist only
|
|
|
|
// of "constant" patterns. Note we cannot match an
|
|
|
|
// all-constant pattern, fail the guard, and then
|
|
|
|
// match *another* all-constant pattern. This is
|
|
|
|
// because if the previous pattern matches, then
|
|
|
|
// we *cannot* match this one, unless all the
|
|
|
|
// constants are the same (which is rejected by
|
|
|
|
// `check_match`).
|
|
|
|
//
|
|
|
|
// We can use this to be smarter about the flow
|
|
|
|
// along guards. If the previous pattern matched,
|
|
|
|
// then we know we will not visit the guard in
|
|
|
|
// this one (whether or not the guard succeeded),
|
|
|
|
// if the previous pattern failed, then we know
|
|
|
|
// the guard for that pattern will not have been
|
|
|
|
// visited. Thus, it is not possible to visit both
|
|
|
|
// the previous guard and the current one when
|
|
|
|
// both patterns consist only of constant
|
|
|
|
// sub-patterns.
|
|
|
|
//
|
|
|
|
// However, if the above does not hold, then all
|
|
|
|
// previous guards need to be wired to visit the
|
|
|
|
// current guard pattern.
|
|
|
|
if prev_has_bindings || this_has_bindings {
|
|
|
|
while let Some(prev) = prev_guards.pop() {
|
|
|
|
self.add_contained_edge(prev, guard_start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_has_bindings = this_has_bindings;
|
|
|
|
|
|
|
|
// Push the guard onto the list of previous guards
|
|
|
|
prev_guards.push(guard_exit);
|
|
|
|
|
|
|
|
// Update the exit node for the pattern
|
|
|
|
pat_exit = guard_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add an edge from the exit of this pattern to the
|
|
|
|
// exit of the arm
|
|
|
|
self.add_contained_edge(pat_exit, arm_exit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit the body of this arm
|
|
|
|
let body_exit = self.expr(&arm.body, arm_exit);
|
|
|
|
|
|
|
|
// Link the body to the exit of the expression
|
|
|
|
self.add_contained_edge(body_exit, expr_exit);
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_exit
|
|
|
|
}
|
|
|
|
|
2013-05-10 13:10:35 -04:00
|
|
|
fn add_dummy_node(&mut self, preds: &[CFGIndex]) -> CFGIndex {
|
2015-02-19 15:27:25 +01:00
|
|
|
self.add_node(CFGNodeData::Dummy, preds)
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
|
2015-02-19 15:27:25 +01:00
|
|
|
fn add_ast_node(&mut self, id: ast::NodeId, preds: &[CFGIndex]) -> CFGIndex {
|
|
|
|
assert!(id != ast::DUMMY_NODE_ID);
|
|
|
|
self.add_node(CFGNodeData::AST(id), preds)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_unreachable_node(&mut self) -> CFGIndex {
|
|
|
|
self.add_node(CFGNodeData::Unreachable, &[])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_node(&mut self, data: CFGNodeData, preds: &[CFGIndex]) -> CFGIndex {
|
|
|
|
let node = self.graph.add_node(data);
|
2015-01-31 12:20:46 -05:00
|
|
|
for &pred in preds {
|
2013-05-10 13:10:35 -04:00
|
|
|
self.add_contained_edge(pred, node);
|
|
|
|
}
|
|
|
|
node
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_contained_edge(&mut self,
|
|
|
|
source: CFGIndex,
|
|
|
|
target: CFGIndex) {
|
2014-03-19 23:16:56 +11:00
|
|
|
let data = CFGEdgeData {exiting_scopes: vec!() };
|
2013-05-10 13:10:35 -04:00
|
|
|
self.graph.add_edge(source, target, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_exiting_edge(&mut self,
|
2014-09-07 20:09:06 +03:00
|
|
|
from_expr: &ast::Expr,
|
2013-05-10 13:10:35 -04:00
|
|
|
from_index: CFGIndex,
|
|
|
|
to_loop: LoopScope,
|
|
|
|
to_index: CFGIndex) {
|
2014-03-19 23:16:56 +11:00
|
|
|
let mut data = CFGEdgeData {exiting_scopes: vec!() };
|
2014-11-18 14:22:59 +01:00
|
|
|
let mut scope = CodeExtent::from_node_id(from_expr.id);
|
|
|
|
let target_scope = CodeExtent::from_node_id(to_loop.loop_id);
|
|
|
|
while scope != target_scope {
|
2014-03-19 23:16:56 +11:00
|
|
|
|
2014-11-18 14:22:59 +01:00
|
|
|
data.exiting_scopes.push(scope.node_id());
|
|
|
|
scope = self.tcx.region_maps.encl_scope(scope);
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
self.graph.add_edge(from_index, to_index, data);
|
|
|
|
}
|
|
|
|
|
2014-05-09 00:07:57 +02:00
|
|
|
fn add_returning_edge(&mut self,
|
2014-09-07 20:09:06 +03:00
|
|
|
_from_expr: &ast::Expr,
|
2014-05-09 00:07:57 +02:00
|
|
|
from_index: CFGIndex) {
|
2014-05-16 10:45:16 -07:00
|
|
|
let mut data = CFGEdgeData {
|
|
|
|
exiting_scopes: vec!(),
|
|
|
|
};
|
2014-05-09 00:07:57 +02:00
|
|
|
for &LoopScope { loop_id: id, .. } in self.loop_scopes.iter().rev() {
|
|
|
|
data.exiting_scopes.push(id);
|
|
|
|
}
|
|
|
|
self.graph.add_edge(from_index, self.fn_exit, data);
|
|
|
|
}
|
|
|
|
|
2013-05-10 13:10:35 -04:00
|
|
|
fn find_scope(&self,
|
2014-09-07 20:09:06 +03:00
|
|
|
expr: &ast::Expr,
|
2014-02-15 16:54:32 +08:00
|
|
|
label: Option<ast::Ident>) -> LoopScope {
|
2015-02-17 06:44:23 +02:00
|
|
|
if label.is_none() {
|
|
|
|
return *self.loop_scopes.last().unwrap();
|
|
|
|
}
|
2013-05-10 13:10:35 -04:00
|
|
|
|
2015-02-17 06:44:23 +02:00
|
|
|
match self.tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def()) {
|
|
|
|
Some(def::DefLabel(loop_id)) => {
|
|
|
|
for l in &self.loop_scopes {
|
|
|
|
if l.loop_id == loop_id {
|
|
|
|
return *l;
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
}
|
2015-02-17 06:44:23 +02:00
|
|
|
self.tcx.sess.span_bug(expr.span,
|
|
|
|
&format!("no loop scope for id {}", loop_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
r => {
|
|
|
|
self.tcx.sess.span_bug(expr.span,
|
|
|
|
&format!("bad entry `{:?}` in def_map for label", r));
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 03:45:37 +02:00
|
|
|
fn is_method_call(&self, expr: &ast::Expr) -> bool {
|
2014-11-25 14:21:20 -05:00
|
|
|
let method_call = ty::MethodCall::expr(expr.id);
|
2014-04-17 21:00:08 +02:00
|
|
|
self.tcx.method_map.borrow().contains_key(&method_call)
|
2013-05-10 13:10:35 -04:00
|
|
|
}
|
2013-07-09 19:32:09 -07:00
|
|
|
}
|