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