1 // Copyright 2015 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.
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.
14 //! Routines for manipulating the control-flow graph.
17 use rustc::mir::repr::*;
18 use syntax::codemap::Span;
20 impl<'tcx> CFG<'tcx> {
21 pub fn block_data(&self, blk: BasicBlock) -> &BasicBlockData<'tcx> {
22 &self.basic_blocks[blk.index()]
25 pub fn block_data_mut(&mut self, blk: BasicBlock) -> &mut BasicBlockData<'tcx> {
26 &mut self.basic_blocks[blk.index()]
29 pub fn start_new_block(&mut self) -> BasicBlock {
30 let node_index = self.basic_blocks.len();
31 self.basic_blocks.push(BasicBlockData::new(None));
32 BasicBlock::new(node_index)
35 pub fn start_new_cleanup_block(&mut self) -> BasicBlock {
36 let bb = self.start_new_block();
37 self.block_data_mut(bb).is_cleanup = true;
41 pub fn push(&mut self, block: BasicBlock, statement: Statement<'tcx>) {
42 debug!("push({:?}, {:?})", block, statement);
43 self.block_data_mut(block).statements.push(statement);
46 pub fn push_drop(&mut self, block: BasicBlock, span: Span, lvalue: &Lvalue<'tcx>) {
47 self.push(block, Statement {
49 kind: StatementKind::Drop(lvalue.clone())
53 pub fn push_assign(&mut self,
56 lvalue: &Lvalue<'tcx>,
57 rvalue: Rvalue<'tcx>) {
58 self.push(block, Statement {
60 kind: StatementKind::Assign(lvalue.clone(), rvalue)
64 pub fn push_assign_constant(&mut self,
68 constant: Constant<'tcx>) {
69 self.push_assign(block, span, temp, Rvalue::Use(Operand::Constant(constant)));
72 pub fn push_assign_unit(&mut self,
75 lvalue: &Lvalue<'tcx>) {
76 self.push_assign(block, span, lvalue, Rvalue::Aggregate(
77 AggregateKind::Tuple, vec![]
81 pub fn terminate(&mut self,
83 terminator: Terminator<'tcx>) {
84 debug_assert!(self.block_data(block).terminator.is_none(),
85 "terminate: block {:?} already has a terminator set", block);
86 self.block_data_mut(block).terminator = Some(terminator);