1 // Copyright 2012-2014 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.
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.
12 use rustc::middle::region::CodeExtent;
13 use rustc::middle::ty::{FnOutput, Ty};
14 use rustc::mir::repr::*;
15 use rustc_data_structures::fnv::FnvHashMap;
19 use syntax::codemap::Span;
21 pub struct Builder<'a, 'tcx: 'a> {
25 // the current set of scopes, updated as we traverse;
26 // see the `scope` module for more details
27 scopes: Vec<scope::Scope<'tcx>>,
29 // for each scope, a span of blocks that defines it;
30 // we track these for use in region and borrow checking,
31 // but these are liable to get out of date once optimization
32 // begins. They are also hopefully temporary, and will be
33 // no longer needed when we adopt graph-based regions.
34 scope_auxiliary: Vec<ScopeAuxiliary>,
36 // the current set of loops; see the `scope` module for more
38 loop_scopes: Vec<scope::LoopScope>,
40 // the vector of all scopes that we have created thus far;
41 // we track this for debuginfo later
42 scope_data_vec: ScopeDataVec,
44 var_decls: Vec<VarDecl<'tcx>>,
45 var_indices: FnvHashMap<ast::NodeId, u32>,
46 temp_decls: Vec<TempDecl<'tcx>>,
47 unit_temp: Option<Lvalue<'tcx>>,
51 basic_blocks: Vec<BasicBlockData<'tcx>>,
54 /// For each scope, we track the extent (from the HIR) and a
55 /// single-entry-multiple-exit subgraph that contains all the
56 /// statements/terminators within it.
58 /// This information is separated out from the main `ScopeData`
59 /// because it is short-lived. First, the extent contains node-ids,
60 /// so it cannot be saved and re-loaded. Second, any optimization will mess up
61 /// the dominator/postdominator information.
63 /// The intention is basically to use this information to do
64 /// regionck/borrowck and then throw it away once we are done.
65 pub struct ScopeAuxiliary {
66 /// extent of this scope from the MIR.
67 pub extent: CodeExtent,
69 /// "entry point": dominator of all nodes in the scope
72 /// "exit points": mutual postdominators of all nodes in the scope
73 pub postdoms: Vec<Location>,
76 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
78 /// the location is within this block
79 pub block: BasicBlock,
81 /// the location is the start of the this statement; or, if `statement_index`
82 /// == num-statements, then the start of the terminator.
83 pub statement_index: usize,
86 pub struct MirPlusPlus<'tcx> {
88 pub scope_auxiliary: Vec<ScopeAuxiliary>,
91 ///////////////////////////////////////////////////////////////////////////
92 /// The `BlockAnd` "monad" packages up the new basic block along with a
93 /// produced value (sometimes just unit, of course). The `unpack!`
94 /// macro (and methods below) makes working with `BlockAnd` much more
97 #[must_use] // if you don't use one of these results, you're leaving a dangling edge
98 pub struct BlockAnd<T>(BasicBlock, T);
100 trait BlockAndExtension {
101 fn and<T>(self, v: T) -> BlockAnd<T>;
102 fn unit(self) -> BlockAnd<()>;
105 impl BlockAndExtension for BasicBlock {
106 fn and<T>(self, v: T) -> BlockAnd<T> {
110 fn unit(self) -> BlockAnd<()> {
115 /// Update a block pointer and return the value.
116 /// Use it like `let x = unpack!(block = self.foo(block, foo))`.
117 macro_rules! unpack {
118 ($x:ident = $c:expr) => {
120 let BlockAnd(b, v) = $c;
128 let BlockAnd(b, ()) = $c;
134 ///////////////////////////////////////////////////////////////////////////
135 /// the main entry point for building MIR for a function
137 pub fn construct<'a,'tcx>(hir: Cx<'a,'tcx>,
139 implicit_arguments: Vec<Ty<'tcx>>,
140 explicit_arguments: Vec<(Ty<'tcx>, &'tcx hir::Pat)>,
141 argument_extent: CodeExtent,
142 return_ty: FnOutput<'tcx>,
143 ast_block: &'tcx hir::Block)
144 -> MirPlusPlus<'tcx> {
145 let cfg = CFG { basic_blocks: vec![] };
147 let mut builder = Builder {
151 scope_data_vec: ScopeDataVec::new(),
152 scope_auxiliary: vec![],
156 var_indices: FnvHashMap(),
160 assert_eq!(builder.cfg.start_new_block(), START_BLOCK);
161 assert_eq!(builder.cfg.start_new_block(), END_BLOCK);
163 let mut block = START_BLOCK;
164 let (arg_decls, arg_scope_id) =
165 unpack!(block = builder.args_and_body(block,
171 builder.cfg.terminate(block, arg_scope_id, span,
172 TerminatorKind::Goto { target: END_BLOCK });
173 builder.cfg.terminate(END_BLOCK, arg_scope_id, span,
174 TerminatorKind::Return);
177 builder.cfg.basic_blocks
180 .all(|(index, block)| {
181 if block.terminator.is_none() {
182 panic!("no terminator on block {:?} in {:?}",
183 index, argument_extent)
190 basic_blocks: builder.cfg.basic_blocks,
191 scopes: builder.scope_data_vec,
192 var_decls: builder.var_decls,
193 arg_decls: arg_decls,
194 temp_decls: builder.temp_decls,
195 return_ty: return_ty,
198 scope_auxiliary: builder.scope_auxiliary,
202 impl<'a,'tcx> Builder<'a,'tcx> {
203 fn args_and_body(&mut self,
204 mut block: BasicBlock,
205 implicit_arguments: Vec<Ty<'tcx>>,
206 explicit_arguments: Vec<(Ty<'tcx>, &'tcx hir::Pat)>,
207 argument_extent: CodeExtent,
208 ast_block: &'tcx hir::Block)
209 -> BlockAnd<(Vec<ArgDecl<'tcx>>, ScopeId)>
211 self.in_scope(argument_extent, block, |this, argument_scope_id| {
212 // to start, translate the argument patterns and collect the argument types.
213 let implicits = implicit_arguments.into_iter().map(|ty| (ty, None));
214 let explicits = explicit_arguments.into_iter().map(|(ty, pat)| (ty, Some(pat)));
219 .map(|(index, (ty, pattern))| {
220 let lvalue = Lvalue::Arg(index as u32);
221 if let Some(pattern) = pattern {
222 let pattern = this.hir.irrefutable_pat(pattern);
223 unpack!(block = this.lvalue_into_pattern(block,
228 // Make sure we drop (parts of) the argument even when not matched on.
229 this.schedule_drop(pattern.as_ref().map_or(ast_block.span, |pat| pat.span),
230 argument_extent, &lvalue, ty);
231 ArgDecl { ty: ty, spread: false }
235 // start the first basic block and translate the body
236 unpack!(block = this.ast_block(&Lvalue::ReturnPointer, block, ast_block));
238 block.and((arg_decls, argument_scope_id))
242 fn get_unit_temp(&mut self) -> Lvalue<'tcx> {
243 match self.unit_temp {
244 Some(ref tmp) => tmp.clone(),
246 let ty = self.hir.unit_ty();
247 let tmp = self.temp(ty);
248 self.unit_temp = Some(tmp.clone());
255 ///////////////////////////////////////////////////////////////////////////
256 // Builder methods are broken up into modules, depending on what kind
257 // of thing is being translated. Note that they use the `unpack` macro
258 // above extensively.