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