]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/shim.rs
Auto merge of #56842 - scottmcm:vecdeque-rotate, r=alexcrichton
[rust.git] / src / librustc_mir / shim.rs
1 // Copyright 2016 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 use rustc::hir;
12 use rustc::hir::def_id::DefId;
13 use rustc::infer;
14 use rustc::mir::*;
15 use rustc::ty::{self, Ty, TyCtxt, GenericParamDefKind};
16 use rustc::ty::layout::VariantIdx;
17 use rustc::ty::subst::{Subst, Substs};
18 use rustc::ty::query::Providers;
19
20 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
21
22 use rustc_target::spec::abi::Abi;
23 use syntax::ast;
24 use syntax_pos::Span;
25
26 use std::fmt;
27 use std::iter;
28
29 use transform::{add_moves_for_packed_drops, add_call_guards};
30 use transform::{remove_noop_landing_pads, no_landing_pads, simplify};
31 use util::elaborate_drops::{self, DropElaborator, DropStyle, DropFlagMode};
32 use util::patch::MirPatch;
33
34 pub fn provide(providers: &mut Providers) {
35     providers.mir_shims = make_shim;
36 }
37
38 fn make_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
39                        instance: ty::InstanceDef<'tcx>)
40                        -> &'tcx Mir<'tcx>
41 {
42     debug!("make_shim({:?})", instance);
43
44     let mut result = match instance {
45         ty::InstanceDef::Item(..) =>
46             bug!("item {:?} passed to make_shim", instance),
47         ty::InstanceDef::VtableShim(def_id) => {
48             build_call_shim(
49                 tcx,
50                 def_id,
51                 Adjustment::DerefMove,
52                 CallKind::Direct(def_id),
53                 None,
54             )
55         }
56         ty::InstanceDef::FnPtrShim(def_id, ty) => {
57             let trait_ = tcx.trait_of_item(def_id).unwrap();
58             let adjustment = match tcx.lang_items().fn_trait_kind(trait_) {
59                 Some(ty::ClosureKind::FnOnce) => Adjustment::Identity,
60                 Some(ty::ClosureKind::FnMut) |
61                 Some(ty::ClosureKind::Fn) => Adjustment::Deref,
62                 None => bug!("fn pointer {:?} is not an fn", ty)
63             };
64             // HACK: we need the "real" argument types for the MIR,
65             // but because our substs are (Self, Args), where Args
66             // is a tuple, we must include the *concrete* argument
67             // types in the MIR. They will be substituted again with
68             // the param-substs, but because they are concrete, this
69             // will not do any harm.
70             let sig = tcx.erase_late_bound_regions(&ty.fn_sig(tcx));
71             let arg_tys = sig.inputs();
72
73             build_call_shim(
74                 tcx,
75                 def_id,
76                 adjustment,
77                 CallKind::Indirect,
78                 Some(arg_tys)
79             )
80         }
81         ty::InstanceDef::Virtual(def_id, _) => {
82             // We are generating a call back to our def-id, which the
83             // codegen backend knows to turn to an actual virtual call.
84             build_call_shim(
85                 tcx,
86                 def_id,
87                 Adjustment::Identity,
88                 CallKind::Direct(def_id),
89                 None
90             )
91         }
92         ty::InstanceDef::ClosureOnceShim { call_once } => {
93             let fn_mut = tcx.lang_items().fn_mut_trait().unwrap();
94             let call_mut = tcx.global_tcx()
95                 .associated_items(fn_mut)
96                 .find(|it| it.kind == ty::AssociatedKind::Method)
97                 .unwrap().def_id;
98
99             build_call_shim(
100                 tcx,
101                 call_once,
102                 Adjustment::RefMut,
103                 CallKind::Direct(call_mut),
104                 None
105             )
106         }
107         ty::InstanceDef::DropGlue(def_id, ty) => {
108             build_drop_shim(tcx, def_id, ty)
109         }
110         ty::InstanceDef::CloneShim(def_id, ty) => {
111             let name = tcx.item_name(def_id);
112             if name == "clone" {
113                 build_clone_shim(tcx, def_id, ty)
114             } else if name == "clone_from" {
115                 debug!("make_shim({:?}: using default trait implementation", instance);
116                 return tcx.optimized_mir(def_id);
117             } else {
118                 bug!("builtin clone shim {:?} not supported", instance)
119             }
120         }
121         ty::InstanceDef::Intrinsic(_) => {
122             bug!("creating shims from intrinsics ({:?}) is unsupported", instance)
123         }
124     };
125     debug!("make_shim({:?}) = untransformed {:?}", instance, result);
126     add_moves_for_packed_drops::add_moves_for_packed_drops(
127         tcx, &mut result, instance.def_id());
128     no_landing_pads::no_landing_pads(tcx, &mut result);
129     remove_noop_landing_pads::remove_noop_landing_pads(tcx, &mut result);
130     simplify::simplify_cfg(&mut result);
131     add_call_guards::CriticalCallEdges.add_call_guards(&mut result);
132     debug!("make_shim({:?}) = {:?}", instance, result);
133
134     tcx.alloc_mir(result)
135 }
136
137 #[derive(Copy, Clone, Debug, PartialEq)]
138 enum Adjustment {
139     Identity,
140     Deref,
141     DerefMove,
142     RefMut,
143 }
144
145 #[derive(Copy, Clone, Debug, PartialEq)]
146 enum CallKind {
147     Indirect,
148     Direct(DefId),
149 }
150
151 fn temp_decl(mutability: Mutability, ty: Ty, span: Span) -> LocalDecl {
152     let source_info = SourceInfo { scope: OUTERMOST_SOURCE_SCOPE, span };
153     LocalDecl {
154         mutability,
155         ty,
156         user_ty: UserTypeProjections::none(),
157         name: None,
158         source_info,
159         visibility_scope: source_info.scope,
160         internal: false,
161         is_user_variable: None,
162         is_block_tail: None,
163     }
164 }
165
166 fn local_decls_for_sig<'tcx>(sig: &ty::FnSig<'tcx>, span: Span)
167     -> IndexVec<Local, LocalDecl<'tcx>>
168 {
169     iter::once(temp_decl(Mutability::Mut, sig.output(), span))
170         .chain(sig.inputs().iter().map(
171             |ity| temp_decl(Mutability::Not, ity, span)))
172         .collect()
173 }
174
175 fn build_drop_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
176                              def_id: DefId,
177                              ty: Option<Ty<'tcx>>)
178                              -> Mir<'tcx>
179 {
180     debug!("build_drop_shim(def_id={:?}, ty={:?})", def_id, ty);
181
182     // Check if this is a generator, if so, return the drop glue for it
183     if let Some(&ty::TyS { sty: ty::Generator(gen_def_id, substs, _), .. }) = ty {
184         let mir = &**tcx.optimized_mir(gen_def_id).generator_drop.as_ref().unwrap();
185         return mir.subst(tcx, substs.substs);
186     }
187
188     let substs = if let Some(ty) = ty {
189         tcx.intern_substs(&[ty.into()])
190     } else {
191         Substs::identity_for_item(tcx, def_id)
192     };
193     let sig = tcx.fn_sig(def_id).subst(tcx, substs);
194     let sig = tcx.erase_late_bound_regions(&sig);
195     let span = tcx.def_span(def_id);
196
197     let source_info = SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE };
198
199     let return_block = BasicBlock::new(1);
200     let mut blocks = IndexVec::with_capacity(2);
201     let block = |blocks: &mut IndexVec<_, _>, kind| {
202         blocks.push(BasicBlockData {
203             statements: vec![],
204             terminator: Some(Terminator { source_info, kind }),
205             is_cleanup: false
206         })
207     };
208     block(&mut blocks, TerminatorKind::Goto { target: return_block });
209     block(&mut blocks, TerminatorKind::Return);
210
211     let mut mir = Mir::new(
212         blocks,
213         IndexVec::from_elem_n(
214             SourceScopeData { span: span, parent_scope: None }, 1
215         ),
216         ClearCrossCrate::Clear,
217         IndexVec::new(),
218         None,
219         local_decls_for_sig(&sig, span),
220         sig.inputs().len(),
221         vec![],
222         span,
223         vec![],
224     );
225
226     if let Some(..) = ty {
227         // The first argument (index 0), but add 1 for the return value.
228         let dropee_ptr = Place::Local(Local::new(1+0));
229         if tcx.sess.opts.debugging_opts.mir_emit_retag {
230             // Function arguments should be retagged, and we make this one raw.
231             mir.basic_blocks_mut()[START_BLOCK].statements.insert(0, Statement {
232                 source_info,
233                 kind: StatementKind::Retag(RetagKind::Raw, dropee_ptr.clone()),
234             });
235         }
236         let patch = {
237             let param_env = tcx.param_env(def_id).with_reveal_all();
238             let mut elaborator = DropShimElaborator {
239                 mir: &mir,
240                 patch: MirPatch::new(&mir),
241                 tcx,
242                 param_env
243             };
244             let dropee = dropee_ptr.deref();
245             let resume_block = elaborator.patch.resume_block();
246             elaborate_drops::elaborate_drop(
247                 &mut elaborator,
248                 source_info,
249                 &dropee,
250                 (),
251                 return_block,
252                 elaborate_drops::Unwind::To(resume_block),
253                 START_BLOCK
254             );
255             elaborator.patch
256         };
257         patch.apply(&mut mir);
258     }
259
260     mir
261 }
262
263 pub struct DropShimElaborator<'a, 'tcx: 'a> {
264     pub mir: &'a Mir<'tcx>,
265     pub patch: MirPatch<'tcx>,
266     pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
267     pub param_env: ty::ParamEnv<'tcx>,
268 }
269
270 impl<'a, 'tcx> fmt::Debug for DropShimElaborator<'a, 'tcx> {
271     fn fmt(&self, _f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
272         Ok(())
273     }
274 }
275
276 impl<'a, 'tcx> DropElaborator<'a, 'tcx> for DropShimElaborator<'a, 'tcx> {
277     type Path = ();
278
279     fn patch(&mut self) -> &mut MirPatch<'tcx> { &mut self.patch }
280     fn mir(&self) -> &'a Mir<'tcx> { self.mir }
281     fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> { self.tcx }
282     fn param_env(&self) -> ty::ParamEnv<'tcx> { self.param_env }
283
284     fn drop_style(&self, _path: Self::Path, mode: DropFlagMode) -> DropStyle {
285         if let DropFlagMode::Shallow = mode {
286             DropStyle::Static
287         } else {
288             DropStyle::Open
289         }
290     }
291
292     fn get_drop_flag(&mut self, _path: Self::Path) -> Option<Operand<'tcx>> {
293         None
294     }
295
296     fn clear_drop_flag(&mut self, _location: Location, _path: Self::Path, _mode: DropFlagMode) {
297     }
298
299     fn field_subpath(&self, _path: Self::Path, _field: Field) -> Option<Self::Path> {
300         None
301     }
302     fn deref_subpath(&self, _path: Self::Path) -> Option<Self::Path> {
303         None
304     }
305     fn downcast_subpath(&self, _path: Self::Path, _variant: VariantIdx) -> Option<Self::Path> {
306         Some(())
307     }
308     fn array_subpath(&self, _path: Self::Path, _index: u32, _size: u32) -> Option<Self::Path> {
309         None
310     }
311 }
312
313 /// Build a `Clone::clone` shim for `self_ty`. Here, `def_id` is `Clone::clone`.
314 fn build_clone_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
315                               def_id: DefId,
316                               self_ty: Ty<'tcx>)
317                               -> Mir<'tcx>
318 {
319     debug!("build_clone_shim(def_id={:?})", def_id);
320
321     let mut builder = CloneShimBuilder::new(tcx, def_id, self_ty);
322     let is_copy = !self_ty.moves_by_default(tcx, tcx.param_env(def_id), builder.span);
323
324     let dest = Place::Local(RETURN_PLACE);
325     let src = Place::Local(Local::new(1+0)).deref();
326
327     match self_ty.sty {
328         _ if is_copy => builder.copy_shim(),
329         ty::Array(ty, len) => {
330             let len = len.unwrap_usize(tcx);
331             builder.array_shim(dest, src, ty, len)
332         }
333         ty::Closure(def_id, substs) => {
334             builder.tuple_like_shim(
335                 dest, src,
336                 substs.upvar_tys(def_id, tcx)
337             )
338         }
339         ty::Tuple(tys) => builder.tuple_like_shim(dest, src, tys.iter().cloned()),
340         _ => {
341             bug!("clone shim for `{:?}` which is not `Copy` and is not an aggregate", self_ty)
342         }
343     };
344
345     builder.into_mir()
346 }
347
348 struct CloneShimBuilder<'a, 'tcx: 'a> {
349     tcx: TyCtxt<'a, 'tcx, 'tcx>,
350     def_id: DefId,
351     local_decls: IndexVec<Local, LocalDecl<'tcx>>,
352     blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
353     span: Span,
354     sig: ty::FnSig<'tcx>,
355 }
356
357 impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
358     fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>,
359            def_id: DefId,
360            self_ty: Ty<'tcx>) -> Self {
361         // we must subst the self_ty because it's
362         // otherwise going to be TySelf and we can't index
363         // or access fields of a Place of type TySelf.
364         let substs = tcx.mk_substs_trait(self_ty, &[]);
365         let sig = tcx.fn_sig(def_id).subst(tcx, substs);
366         let sig = tcx.erase_late_bound_regions(&sig);
367         let span = tcx.def_span(def_id);
368
369         CloneShimBuilder {
370             tcx,
371             def_id,
372             local_decls: local_decls_for_sig(&sig, span),
373             blocks: IndexVec::new(),
374             span,
375             sig,
376         }
377     }
378
379     fn into_mir(self) -> Mir<'tcx> {
380         Mir::new(
381             self.blocks,
382             IndexVec::from_elem_n(
383                 SourceScopeData { span: self.span, parent_scope: None }, 1
384             ),
385             ClearCrossCrate::Clear,
386             IndexVec::new(),
387             None,
388             self.local_decls,
389             self.sig.inputs().len(),
390             vec![],
391             self.span,
392             vec![],
393         )
394     }
395
396     fn source_info(&self) -> SourceInfo {
397         SourceInfo { span: self.span, scope: OUTERMOST_SOURCE_SCOPE }
398     }
399
400     fn block(
401         &mut self,
402         statements: Vec<Statement<'tcx>>,
403         kind: TerminatorKind<'tcx>,
404         is_cleanup: bool
405     ) -> BasicBlock {
406         let source_info = self.source_info();
407         self.blocks.push(BasicBlockData {
408             statements,
409             terminator: Some(Terminator { source_info, kind }),
410             is_cleanup,
411         })
412     }
413
414     /// Gives the index of an upcoming BasicBlock, with an offset.
415     /// offset=0 will give you the index of the next BasicBlock,
416     /// offset=1 will give the index of the next-to-next block,
417     /// offset=-1 will give you the index of the last-created block
418     fn block_index_offset(&mut self, offset: usize) -> BasicBlock {
419         BasicBlock::new(self.blocks.len() + offset)
420     }
421
422     fn make_statement(&self, kind: StatementKind<'tcx>) -> Statement<'tcx> {
423         Statement {
424             source_info: self.source_info(),
425             kind,
426         }
427     }
428
429     fn copy_shim(&mut self) {
430         let rcvr = Place::Local(Local::new(1+0)).deref();
431         let ret_statement = self.make_statement(
432             StatementKind::Assign(
433                 Place::Local(RETURN_PLACE),
434                 box Rvalue::Use(Operand::Copy(rcvr))
435             )
436         );
437         self.block(vec![ret_statement], TerminatorKind::Return, false);
438     }
439
440     fn make_place(&mut self, mutability: Mutability, ty: Ty<'tcx>) -> Place<'tcx> {
441         let span = self.span;
442         Place::Local(
443             self.local_decls.push(temp_decl(mutability, ty, span))
444         )
445     }
446
447     fn make_clone_call(
448         &mut self,
449         dest: Place<'tcx>,
450         src: Place<'tcx>,
451         ty: Ty<'tcx>,
452         next: BasicBlock,
453         cleanup: BasicBlock
454     ) {
455         let tcx = self.tcx;
456
457         let substs = Substs::for_item(tcx, self.def_id, |param, _| {
458             match param.kind {
459                 GenericParamDefKind::Lifetime => tcx.types.re_erased.into(),
460                 GenericParamDefKind::Type {..} => ty.into(),
461             }
462         });
463
464         // `func == Clone::clone(&ty) -> ty`
465         let func_ty = tcx.mk_fn_def(self.def_id, substs);
466         let func = Operand::Constant(box Constant {
467             span: self.span,
468             ty: func_ty,
469             user_ty: None,
470             literal: ty::Const::zero_sized(self.tcx, func_ty),
471         });
472
473         let ref_loc = self.make_place(
474             Mutability::Not,
475             tcx.mk_ref(tcx.types.re_erased, ty::TypeAndMut {
476                 ty,
477                 mutbl: hir::Mutability::MutImmutable,
478             })
479         );
480
481         // `let ref_loc: &ty = &src;`
482         let statement = self.make_statement(
483             StatementKind::Assign(
484                 ref_loc.clone(),
485                 box Rvalue::Ref(tcx.types.re_erased, BorrowKind::Shared, src)
486             )
487         );
488
489         // `let loc = Clone::clone(ref_loc);`
490         self.block(vec![statement], TerminatorKind::Call {
491             func,
492             args: vec![Operand::Move(ref_loc)],
493             destination: Some((dest, next)),
494             cleanup: Some(cleanup),
495             from_hir_call: true,
496         }, false);
497     }
498
499     fn loop_header(
500         &mut self,
501         beg: Place<'tcx>,
502         end: Place<'tcx>,
503         loop_body: BasicBlock,
504         loop_end: BasicBlock,
505         is_cleanup: bool
506     ) {
507         let tcx = self.tcx;
508
509         let cond = self.make_place(Mutability::Mut, tcx.types.bool);
510         let compute_cond = self.make_statement(
511             StatementKind::Assign(
512                 cond.clone(),
513                 box Rvalue::BinaryOp(BinOp::Ne, Operand::Copy(end), Operand::Copy(beg))
514             )
515         );
516
517         // `if end != beg { goto loop_body; } else { goto loop_end; }`
518         self.block(
519             vec![compute_cond],
520             TerminatorKind::if_(tcx, Operand::Move(cond), loop_body, loop_end),
521             is_cleanup
522         );
523     }
524
525     fn make_usize(&self, value: u64) -> Box<Constant<'tcx>> {
526         box Constant {
527             span: self.span,
528             ty: self.tcx.types.usize,
529             user_ty: None,
530             literal: ty::Const::from_usize(self.tcx, value),
531         }
532     }
533
534     fn array_shim(&mut self, dest: Place<'tcx>, src: Place<'tcx>, ty: Ty<'tcx>, len: u64) {
535         let tcx = self.tcx;
536         let span = self.span;
537
538         let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
539         let end = self.make_place(Mutability::Not, tcx.types.usize);
540
541         // BB #0
542         // `let mut beg = 0;`
543         // `let end = len;`
544         // `goto #1;`
545         let inits = vec![
546             self.make_statement(
547                 StatementKind::Assign(
548                     Place::Local(beg),
549                     box Rvalue::Use(Operand::Constant(self.make_usize(0)))
550                 )
551             ),
552             self.make_statement(
553                 StatementKind::Assign(
554                     end.clone(),
555                     box Rvalue::Use(Operand::Constant(self.make_usize(len)))
556                 )
557             )
558         ];
559         self.block(inits, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
560
561         // BB #1: loop {
562         //     BB #2;
563         //     BB #3;
564         // }
565         // BB #4;
566         self.loop_header(Place::Local(beg), end, BasicBlock::new(2), BasicBlock::new(4), false);
567
568         // BB #2
569         // `dest[i] = Clone::clone(src[beg])`;
570         // Goto #3 if ok, #5 if unwinding happens.
571         let dest_field = dest.clone().index(beg);
572         let src_field = src.index(beg);
573         self.make_clone_call(dest_field, src_field, ty, BasicBlock::new(3),
574                              BasicBlock::new(5));
575
576         // BB #3
577         // `beg = beg + 1;`
578         // `goto #1`;
579         let statements = vec![
580             self.make_statement(
581                 StatementKind::Assign(
582                     Place::Local(beg),
583                     box Rvalue::BinaryOp(
584                         BinOp::Add,
585                         Operand::Copy(Place::Local(beg)),
586                         Operand::Constant(self.make_usize(1))
587                     )
588                 )
589             )
590         ];
591         self.block(statements, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
592
593         // BB #4
594         // `return dest;`
595         self.block(vec![], TerminatorKind::Return, false);
596
597         // BB #5 (cleanup)
598         // `let end = beg;`
599         // `let mut beg = 0;`
600         // goto #6;
601         let end = beg;
602         let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
603         let init = self.make_statement(
604             StatementKind::Assign(
605                 Place::Local(beg),
606                 box Rvalue::Use(Operand::Constant(self.make_usize(0)))
607             )
608         );
609         self.block(vec![init], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
610
611         // BB #6 (cleanup): loop {
612         //     BB #7;
613         //     BB #8;
614         // }
615         // BB #9;
616         self.loop_header(Place::Local(beg), Place::Local(end),
617                          BasicBlock::new(7), BasicBlock::new(9), true);
618
619         // BB #7 (cleanup)
620         // `drop(dest[beg])`;
621         self.block(vec![], TerminatorKind::Drop {
622             location: dest.index(beg),
623             target: BasicBlock::new(8),
624             unwind: None,
625         }, true);
626
627         // BB #8 (cleanup)
628         // `beg = beg + 1;`
629         // `goto #6;`
630         let statement = self.make_statement(
631             StatementKind::Assign(
632                 Place::Local(beg),
633                 box Rvalue::BinaryOp(
634                     BinOp::Add,
635                     Operand::Copy(Place::Local(beg)),
636                     Operand::Constant(self.make_usize(1))
637                 )
638             )
639         );
640         self.block(vec![statement], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
641
642         // BB #9 (resume)
643         self.block(vec![], TerminatorKind::Resume, true);
644     }
645
646     fn tuple_like_shim<I>(&mut self, dest: Place<'tcx>,
647                           src: Place<'tcx>, tys: I)
648             where I: Iterator<Item = ty::Ty<'tcx>> {
649         let mut previous_field = None;
650         for (i, ity) in tys.enumerate() {
651             let field = Field::new(i);
652             let src_field = src.clone().field(field, ity);
653
654             let dest_field = dest.clone().field(field, ity);
655
656             // #(2i + 1) is the cleanup block for the previous clone operation
657             let cleanup_block = self.block_index_offset(1);
658             // #(2i + 2) is the next cloning block
659             // (or the Return terminator if this is the last block)
660             let next_block = self.block_index_offset(2);
661
662             // BB #(2i)
663             // `dest.i = Clone::clone(&src.i);`
664             // Goto #(2i + 2) if ok, #(2i + 1) if unwinding happens.
665             self.make_clone_call(
666                 dest_field.clone(),
667                 src_field,
668                 ity,
669                 next_block,
670                 cleanup_block,
671             );
672
673             // BB #(2i + 1) (cleanup)
674             if let Some((previous_field, previous_cleanup)) = previous_field.take() {
675                 // Drop previous field and goto previous cleanup block.
676                 self.block(vec![], TerminatorKind::Drop {
677                     location: previous_field,
678                     target: previous_cleanup,
679                     unwind: None,
680                 }, true);
681             } else {
682                 // Nothing to drop, just resume.
683                 self.block(vec![], TerminatorKind::Resume, true);
684             }
685
686             previous_field = Some((dest_field, cleanup_block));
687         }
688
689         self.block(vec![], TerminatorKind::Return, false);
690     }
691 }
692
693 /// Build a "call" shim for `def_id`. The shim calls the
694 /// function specified by `call_kind`, first adjusting its first
695 /// argument according to `rcvr_adjustment`.
696 ///
697 /// If `untuple_args` is a vec of types, the second argument of the
698 /// function will be untupled as these types.
699 fn build_call_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
700                              def_id: DefId,
701                              rcvr_adjustment: Adjustment,
702                              call_kind: CallKind,
703                              untuple_args: Option<&[Ty<'tcx>]>)
704                              -> Mir<'tcx>
705 {
706     debug!("build_call_shim(def_id={:?}, rcvr_adjustment={:?}, \
707             call_kind={:?}, untuple_args={:?})",
708            def_id, rcvr_adjustment, call_kind, untuple_args);
709
710     let sig = tcx.fn_sig(def_id);
711     let sig = tcx.erase_late_bound_regions(&sig);
712     let span = tcx.def_span(def_id);
713
714     debug!("build_call_shim: sig={:?}", sig);
715
716     let mut local_decls = local_decls_for_sig(&sig, span);
717     let source_info = SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE };
718
719     let rcvr_arg = Local::new(1+0);
720     let rcvr_l = Place::Local(rcvr_arg);
721     let mut statements = vec![];
722
723     let rcvr = match rcvr_adjustment {
724         Adjustment::Identity => Operand::Move(rcvr_l),
725         Adjustment::Deref => Operand::Copy(rcvr_l.deref()),
726         Adjustment::DerefMove => {
727             // fn(Self, ...) -> fn(*mut Self, ...)
728             let arg_ty = local_decls[rcvr_arg].ty;
729             assert!(arg_ty.is_self());
730             local_decls[rcvr_arg].ty = tcx.mk_mut_ptr(arg_ty);
731
732             Operand::Move(rcvr_l.deref())
733         }
734         Adjustment::RefMut => {
735             // let rcvr = &mut rcvr;
736             let ref_rcvr = local_decls.push(temp_decl(
737                 Mutability::Not,
738                 tcx.mk_ref(tcx.types.re_erased, ty::TypeAndMut {
739                     ty: sig.inputs()[0],
740                     mutbl: hir::Mutability::MutMutable
741                 }),
742                 span
743             ));
744             let borrow_kind = BorrowKind::Mut {
745                 allow_two_phase_borrow: false,
746             };
747             statements.push(Statement {
748                 source_info,
749                 kind: StatementKind::Assign(
750                     Place::Local(ref_rcvr),
751                     box Rvalue::Ref(tcx.types.re_erased, borrow_kind, rcvr_l)
752                 )
753             });
754             Operand::Move(Place::Local(ref_rcvr))
755         }
756     };
757
758     let (callee, mut args) = match call_kind {
759         CallKind::Indirect => (rcvr, vec![]),
760         CallKind::Direct(def_id) => {
761             let ty = tcx.type_of(def_id);
762             (Operand::Constant(box Constant {
763                 span,
764                 ty,
765                 user_ty: None,
766                 literal: ty::Const::zero_sized(tcx, ty),
767              }),
768              vec![rcvr])
769         }
770     };
771
772     if let Some(untuple_args) = untuple_args {
773         args.extend(untuple_args.iter().enumerate().map(|(i, ity)| {
774             let arg_place = Place::Local(Local::new(1+1));
775             Operand::Move(arg_place.field(Field::new(i), *ity))
776         }));
777     } else {
778         args.extend((1..sig.inputs().len()).map(|i| {
779             Operand::Move(Place::Local(Local::new(1+i)))
780         }));
781     }
782
783     let n_blocks = if let Adjustment::RefMut = rcvr_adjustment { 5 } else { 2 };
784     let mut blocks = IndexVec::with_capacity(n_blocks);
785     let block = |blocks: &mut IndexVec<_, _>, statements, kind, is_cleanup| {
786         blocks.push(BasicBlockData {
787             statements,
788             terminator: Some(Terminator { source_info, kind }),
789             is_cleanup
790         })
791     };
792
793     // BB #0
794     block(&mut blocks, statements, TerminatorKind::Call {
795         func: callee,
796         args,
797         destination: Some((Place::Local(RETURN_PLACE),
798                            BasicBlock::new(1))),
799         cleanup: if let Adjustment::RefMut = rcvr_adjustment {
800             Some(BasicBlock::new(3))
801         } else {
802             None
803         },
804         from_hir_call: true,
805     }, false);
806
807     if let Adjustment::RefMut = rcvr_adjustment {
808         // BB #1 - drop for Self
809         block(&mut blocks, vec![], TerminatorKind::Drop {
810             location: Place::Local(rcvr_arg),
811             target: BasicBlock::new(2),
812             unwind: None
813         }, false);
814     }
815     // BB #1/#2 - return
816     block(&mut blocks, vec![], TerminatorKind::Return, false);
817     if let Adjustment::RefMut = rcvr_adjustment {
818         // BB #3 - drop if closure panics
819         block(&mut blocks, vec![], TerminatorKind::Drop {
820             location: Place::Local(rcvr_arg),
821             target: BasicBlock::new(4),
822             unwind: None
823         }, true);
824
825         // BB #4 - resume
826         block(&mut blocks, vec![], TerminatorKind::Resume, true);
827     }
828
829     let mut mir = Mir::new(
830         blocks,
831         IndexVec::from_elem_n(
832             SourceScopeData { span: span, parent_scope: None }, 1
833         ),
834         ClearCrossCrate::Clear,
835         IndexVec::new(),
836         None,
837         local_decls,
838         sig.inputs().len(),
839         vec![],
840         span,
841         vec![],
842     );
843     if let Abi::RustCall = sig.abi {
844         mir.spread_arg = Some(Local::new(sig.inputs().len()));
845     }
846     mir
847 }
848
849 pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>,
850                                       ctor_id: ast::NodeId,
851                                       fields: &[hir::StructField],
852                                       span: Span)
853                                       -> Mir<'tcx>
854 {
855     let tcx = infcx.tcx;
856     let gcx = tcx.global_tcx();
857     let def_id = tcx.hir().local_def_id(ctor_id);
858     let param_env = gcx.param_env(def_id);
859
860     // Normalize the sig.
861     let sig = gcx.fn_sig(def_id)
862         .no_bound_vars()
863         .expect("LBR in ADT constructor signature");
864     let sig = gcx.normalize_erasing_regions(param_env, sig);
865
866     let (adt_def, substs) = match sig.output().sty {
867         ty::Adt(adt_def, substs) => (adt_def, substs),
868         _ => bug!("unexpected type for ADT ctor {:?}", sig.output())
869     };
870
871     debug!("build_ctor: def_id={:?} sig={:?} fields={:?}", def_id, sig, fields);
872
873     let local_decls = local_decls_for_sig(&sig, span);
874
875     let source_info = SourceInfo {
876         span,
877         scope: OUTERMOST_SOURCE_SCOPE
878     };
879
880     let variant_no = if adt_def.is_enum() {
881         adt_def.variant_index_with_id(def_id)
882     } else {
883         VariantIdx::new(0)
884     };
885
886     // return = ADT(arg0, arg1, ...); return
887     let start_block = BasicBlockData {
888         statements: vec![Statement {
889             source_info,
890             kind: StatementKind::Assign(
891                 Place::Local(RETURN_PLACE),
892                 box Rvalue::Aggregate(
893                     box AggregateKind::Adt(adt_def, variant_no, substs, None, None),
894                     (1..sig.inputs().len()+1).map(|i| {
895                         Operand::Move(Place::Local(Local::new(i)))
896                     }).collect()
897                 )
898             )
899         }],
900         terminator: Some(Terminator {
901             source_info,
902             kind: TerminatorKind::Return,
903         }),
904         is_cleanup: false
905     };
906
907     Mir::new(
908         IndexVec::from_elem_n(start_block, 1),
909         IndexVec::from_elem_n(
910             SourceScopeData { span: span, parent_scope: None }, 1
911         ),
912         ClearCrossCrate::Clear,
913         IndexVec::new(),
914         None,
915         local_decls,
916         sig.inputs().len(),
917         vec![],
918         span,
919         vec![],
920     )
921 }