]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/cfg.rs
Merge branch 'refactor-select' of https://github.com/aravind-pg/rust into update...
[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::TyCtxt;
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: TyCtxt<'a, 'gcx, 'tcx>,
50                                              block: BasicBlock,
51                                              source_info: SourceInfo,
52                                              region_scope: region::Scope) {
53         if tcx.emit_end_regions() {
54             if let region::ScopeData::CallSite(_) = region_scope.data() {
55                 // The CallSite scope (aka the root scope) is sort of weird, in that it is
56                 // supposed to "separate" the "interior" and "exterior" of a closure. Being
57                 // that, it is not really a part of the region hierarchy, but for some
58                 // reason it *is* considered a part of it.
59                 //
60                 // It should die a hopefully painful death with NLL, so let's leave this hack
61                 // for now so that nobody can complain about soundness.
62                 return
63             }
64
65             self.push(block, Statement {
66                 source_info,
67                 kind: StatementKind::EndRegion(region_scope),
68             });
69         }
70     }
71
72     pub fn push_assign(&mut self,
73                        block: BasicBlock,
74                        source_info: SourceInfo,
75                        place: &Place<'tcx>,
76                        rvalue: Rvalue<'tcx>) {
77         self.push(block, Statement {
78             source_info,
79             kind: StatementKind::Assign(place.clone(), rvalue)
80         });
81     }
82
83     pub fn push_assign_constant(&mut self,
84                                 block: BasicBlock,
85                                 source_info: SourceInfo,
86                                 temp: &Place<'tcx>,
87                                 constant: Constant<'tcx>) {
88         self.push_assign(block, source_info, temp,
89                          Rvalue::Use(Operand::Constant(box constant)));
90     }
91
92     pub fn push_assign_unit(&mut self,
93                             block: BasicBlock,
94                             source_info: SourceInfo,
95                             place: &Place<'tcx>) {
96         self.push_assign(block, source_info, place, Rvalue::Aggregate(
97             box AggregateKind::Tuple, vec![]
98         ));
99     }
100
101     pub fn terminate(&mut self,
102                      block: BasicBlock,
103                      source_info: SourceInfo,
104                      kind: TerminatorKind<'tcx>) {
105         debug!("terminating block {:?} <- {:?}", block, kind);
106         debug_assert!(self.block_data(block).terminator.is_none(),
107                       "terminate: block {:?}={:?} already has a terminator set",
108                       block,
109                       self.block_data(block));
110         self.block_data_mut(block).terminator = Some(Terminator {
111             source_info,
112             kind,
113         });
114     }
115 }