]>
Commit | Line | Data |
---|---|---|
e9174d1e SL |
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. | |
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 | ||
12 | ||
13 | ||
14 | //! Routines for manipulating the control-flow graph. | |
15 | ||
16 | use build::CFG; | |
92a42be0 | 17 | use rustc::mir::repr::*; |
b039eaaf | 18 | use syntax::codemap::Span; |
e9174d1e | 19 | |
b039eaaf SL |
20 | impl<'tcx> CFG<'tcx> { |
21 | pub fn block_data(&self, blk: BasicBlock) -> &BasicBlockData<'tcx> { | |
e9174d1e SL |
22 | &self.basic_blocks[blk.index()] |
23 | } | |
24 | ||
b039eaaf | 25 | pub fn block_data_mut(&mut self, blk: BasicBlock) -> &mut BasicBlockData<'tcx> { |
e9174d1e SL |
26 | &mut self.basic_blocks[blk.index()] |
27 | } | |
28 | ||
e9174d1e SL |
29 | pub fn start_new_block(&mut self) -> BasicBlock { |
30 | let node_index = self.basic_blocks.len(); | |
9cc50fc6 | 31 | self.basic_blocks.push(BasicBlockData::new(None)); |
e9174d1e SL |
32 | BasicBlock::new(node_index) |
33 | } | |
34 | ||
7453a54e SL |
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; | |
38 | bb | |
39 | } | |
40 | ||
b039eaaf | 41 | pub fn push(&mut self, block: BasicBlock, statement: Statement<'tcx>) { |
e9174d1e SL |
42 | debug!("push({:?}, {:?})", block, statement); |
43 | self.block_data_mut(block).statements.push(statement); | |
44 | } | |
45 | ||
e9174d1e SL |
46 | pub fn push_assign(&mut self, |
47 | block: BasicBlock, | |
b039eaaf SL |
48 | span: Span, |
49 | lvalue: &Lvalue<'tcx>, | |
50 | rvalue: Rvalue<'tcx>) { | |
e9174d1e SL |
51 | self.push(block, Statement { |
52 | span: span, | |
53 | kind: StatementKind::Assign(lvalue.clone(), rvalue) | |
54 | }); | |
55 | } | |
56 | ||
9cc50fc6 SL |
57 | pub fn push_assign_constant(&mut self, |
58 | block: BasicBlock, | |
59 | span: Span, | |
60 | temp: &Lvalue<'tcx>, | |
61 | constant: Constant<'tcx>) { | |
62 | self.push_assign(block, span, temp, Rvalue::Use(Operand::Constant(constant))); | |
63 | } | |
64 | ||
65 | pub fn push_assign_unit(&mut self, | |
66 | block: BasicBlock, | |
67 | span: Span, | |
68 | lvalue: &Lvalue<'tcx>) { | |
69 | self.push_assign(block, span, lvalue, Rvalue::Aggregate( | |
70 | AggregateKind::Tuple, vec![] | |
71 | )); | |
72 | } | |
73 | ||
e9174d1e SL |
74 | pub fn terminate(&mut self, |
75 | block: BasicBlock, | |
b039eaaf | 76 | terminator: Terminator<'tcx>) { |
9cc50fc6 | 77 | debug_assert!(self.block_data(block).terminator.is_none(), |
e9174d1e | 78 | "terminate: block {:?} already has a terminator set", block); |
9cc50fc6 | 79 | self.block_data_mut(block).terminator = Some(terminator); |
e9174d1e SL |
80 | } |
81 | } |