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.
16 use build
::{CFG, Location}
;
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 current_location(&mut self, block
: BasicBlock
) -> Location
{
47 let index
= self.block_data(block
).statements
.len();
48 Location { block: block, statement_index: index }
51 pub fn push_assign(&mut self,
55 lvalue
: &Lvalue
<'tcx
>,
56 rvalue
: Rvalue
<'tcx
>) {
57 self.push(block
, Statement
{
60 kind
: StatementKind
::Assign(lvalue
.clone(), rvalue
)
64 pub fn push_assign_constant(&mut self,
69 constant
: Constant
<'tcx
>) {
70 self.push_assign(block
, scope
, span
, temp
,
71 Rvalue
::Use(Operand
::Constant(constant
)));
74 pub fn push_assign_unit(&mut self,
78 lvalue
: &Lvalue
<'tcx
>) {
79 self.push_assign(block
, scope
, span
, lvalue
, Rvalue
::Aggregate(
80 AggregateKind
::Tuple
, vec
![]
84 pub fn terminate(&mut self,
88 kind
: TerminatorKind
<'tcx
>) {
89 debug_assert
!(self.block_data(block
).terminator
.is_none(),
90 "terminate: block {:?} already has a terminator set", block
);
91 self.block_data_mut(block
).terminator
= Some(Terminator
{