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