]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/cfg.rs
rustdoc: pretty-print Unevaluated expressions in types.
[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::middle::region;
18 use rustc::mir::*;
19 use rustc::ty;
20
21 impl<'tcx> CFG<'tcx> {
22     pub fn block_data(&self, blk: BasicBlock) -> &BasicBlockData<'tcx> {
23         &self.basic_blocks[blk]
24     }
25
26     pub fn block_data_mut(&mut self, blk: BasicBlock) -> &mut BasicBlockData<'tcx> {
27         &mut self.basic_blocks[blk]
28     }
29
30     // llvm.org/PR32488 makes this function use an excess of stack space. Mark
31     // it as #[inline(never)] to keep rustc's stack use in check.
32     #[inline(never)]
33     pub fn start_new_block(&mut self) -> BasicBlock {
34         self.basic_blocks.push(BasicBlockData::new(None))
35     }
36
37     pub fn start_new_cleanup_block(&mut self) -> BasicBlock {
38         let bb = self.start_new_block();
39         self.block_data_mut(bb).is_cleanup = true;
40         bb
41     }
42
43     pub fn push(&mut self, block: BasicBlock, statement: Statement<'tcx>) {
44         debug!("push({:?}, {:?})", block, statement);
45         self.block_data_mut(block).statements.push(statement);
46     }
47
48     pub fn push_end_region<'a, 'gcx:'a+'tcx>(&mut self,
49                                              tcx: ty::TyCtxt<'a, 'gcx, 'tcx>,
50                                              block: BasicBlock,
51                                              source_info: SourceInfo,
52                                              region_scope: region::Scope) {
53         if tcx.sess.emit_end_regions() {
54             self.push(block, Statement {
55                 source_info,
56                 kind: StatementKind::EndRegion(region_scope),
57             });
58         }
59     }
60
61     pub fn push_assign(&mut self,
62                        block: BasicBlock,
63                        source_info: SourceInfo,
64                        lvalue: &Lvalue<'tcx>,
65                        rvalue: Rvalue<'tcx>) {
66         self.push(block, Statement {
67             source_info,
68             kind: StatementKind::Assign(lvalue.clone(), rvalue)
69         });
70     }
71
72     pub fn push_assign_constant(&mut self,
73                                 block: BasicBlock,
74                                 source_info: SourceInfo,
75                                 temp: &Lvalue<'tcx>,
76                                 constant: Constant<'tcx>) {
77         self.push_assign(block, source_info, temp,
78                          Rvalue::Use(Operand::Constant(box constant)));
79     }
80
81     pub fn push_assign_unit(&mut self,
82                             block: BasicBlock,
83                             source_info: SourceInfo,
84                             lvalue: &Lvalue<'tcx>) {
85         self.push_assign(block, source_info, lvalue, Rvalue::Aggregate(
86             box AggregateKind::Tuple, vec![]
87         ));
88     }
89
90     pub fn terminate(&mut self,
91                      block: BasicBlock,
92                      source_info: SourceInfo,
93                      kind: TerminatorKind<'tcx>) {
94         debug!("terminating block {:?} <- {:?}", block, kind);
95         debug_assert!(self.block_data(block).terminator.is_none(),
96                       "terminate: block {:?}={:?} already has a terminator set",
97                       block,
98                       self.block_data(block));
99         self.block_data_mut(block).terminator = Some(Terminator {
100             source_info,
101             kind,
102         });
103     }
104 }