]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/cfg.rs
365fca23dda7018bbea484f78bbee5e6f03adf1c
[rust.git] / src / librustc_mir / build / cfg.rs
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;
17 use rustc::mir::repr::*;
18 use syntax::codemap::Span;
19
20 impl<'tcx> CFG<'tcx> {
21     pub fn block_data(&self, blk: BasicBlock) -> &BasicBlockData<'tcx> {
22         &self.basic_blocks[blk.index()]
23     }
24
25     pub fn block_data_mut(&mut self, blk: BasicBlock) -> &mut BasicBlockData<'tcx> {
26         &mut self.basic_blocks[blk.index()]
27     }
28
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)
33     }
34
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
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);
44     }
45
46     pub fn push_drop(&mut self, block: BasicBlock, span: Span, lvalue: &Lvalue<'tcx>) {
47         self.push(block, Statement {
48             span: span,
49             kind: StatementKind::Drop(lvalue.clone())
50         });
51     }
52
53     pub fn push_assign(&mut self,
54                        block: BasicBlock,
55                        span: Span,
56                        lvalue: &Lvalue<'tcx>,
57                        rvalue: Rvalue<'tcx>) {
58         self.push(block, Statement {
59             span: span,
60             kind: StatementKind::Assign(lvalue.clone(), rvalue)
61         });
62     }
63
64     pub fn push_assign_constant(&mut self,
65                                 block: BasicBlock,
66                                 span: Span,
67                                 temp: &Lvalue<'tcx>,
68                                 constant: Constant<'tcx>) {
69         self.push_assign(block, span, temp, Rvalue::Use(Operand::Constant(constant)));
70     }
71
72     pub fn push_assign_unit(&mut self,
73                             block: BasicBlock,
74                             span: Span,
75                             lvalue: &Lvalue<'tcx>) {
76         self.push_assign(block, span, lvalue, Rvalue::Aggregate(
77             AggregateKind::Tuple, vec![]
78         ));
79     }
80
81     pub fn terminate(&mut self,
82                      block: BasicBlock,
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);
87     }
88 }