]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/cfg.rs
ed99364a0f230c0af02c60d6ea1a7a8e275d294b
[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, Location};
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 current_location(&mut self, block: BasicBlock) -> Location {
47         let index = self.block_data(block).statements.len();
48         Location { block: block, statement_index: index }
49     }
50
51     pub fn push_assign(&mut self,
52                        block: BasicBlock,
53                        scope: ScopeId,
54                        span: Span,
55                        lvalue: &Lvalue<'tcx>,
56                        rvalue: Rvalue<'tcx>) {
57         self.push(block, Statement {
58             scope: scope,
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                                 scope: ScopeId,
67                                 span: Span,
68                                 temp: &Lvalue<'tcx>,
69                                 constant: Constant<'tcx>) {
70         self.push_assign(block, scope, span, temp,
71                          Rvalue::Use(Operand::Constant(constant)));
72     }
73
74     pub fn push_assign_unit(&mut self,
75                             block: BasicBlock,
76                             scope: ScopeId,
77                             span: Span,
78                             lvalue: &Lvalue<'tcx>) {
79         self.push_assign(block, scope, span, lvalue, Rvalue::Aggregate(
80             AggregateKind::Tuple, vec![]
81         ));
82     }
83
84     pub fn terminate(&mut self,
85                      block: BasicBlock,
86                      kind: TerminatorKind<'tcx>) {
87         debug_assert!(self.block_data(block).terminator.is_none(),
88                       "terminate: block {:?} already has a terminator set", block);
89         self.block_data_mut(block).terminator = Some(Terminator {
90             kind: kind
91         });
92     }
93 }