]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/shim.rs
Auto merge of #44438 - Xaeroxe:clamp, r=Mark-Simulacrum
[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);
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 span = self.span;
483         let rcvr = Lvalue::Local(Local::new(1+0)).deref();
484
485         let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
486         let end = self.make_lvalue(Mutability::Not, tcx.types.usize);
487         let ret = self.make_lvalue(Mutability::Mut, tcx.mk_array(ty, len));
488
489         // BB #0
490         // `let mut beg = 0;`
491         // `let end = len;`
492         // `goto #1;`
493         let inits = vec![
494             self.make_statement(
495                 StatementKind::Assign(
496                     Lvalue::Local(beg),
497                     Rvalue::Use(Operand::Constant(self.make_usize(0)))
498                 )
499             ),
500             self.make_statement(
501                 StatementKind::Assign(
502                     end.clone(),
503                     Rvalue::Use(Operand::Constant(self.make_usize(len)))
504                 )
505             )
506         ];
507         self.block(inits, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
508
509         // BB #1: loop {
510         //     BB #2;
511         //     BB #3;
512         // }
513         // BB #4;
514         self.loop_header(Lvalue::Local(beg), end, BasicBlock::new(2), BasicBlock::new(4), false);
515
516         // BB #2
517         // `let cloned = Clone::clone(rcvr[beg])`;
518         // Goto #3 if ok, #5 if unwinding happens.
519         let rcvr_field = rcvr.clone().index(beg);
520         let cloned = self.make_clone_call(ty, rcvr_field, BasicBlock::new(3), BasicBlock::new(5));
521
522         // BB #3
523         // `ret[beg] = cloned;`
524         // `beg = beg + 1;`
525         // `goto #1`;
526         let ret_field = ret.clone().index(beg);
527         let statements = vec![
528             self.make_statement(
529                 StatementKind::Assign(
530                     ret_field,
531                     Rvalue::Use(Operand::Consume(cloned))
532                 )
533             ),
534             self.make_statement(
535                 StatementKind::Assign(
536                     Lvalue::Local(beg),
537                     Rvalue::BinaryOp(
538                         BinOp::Add,
539                         Operand::Consume(Lvalue::Local(beg)),
540                         Operand::Constant(self.make_usize(1))
541                     )
542                 )
543             )
544         ];
545         self.block(statements, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
546
547         // BB #4
548         // `return ret;`
549         let ret_statement = self.make_statement(
550             StatementKind::Assign(
551                 Lvalue::Local(RETURN_POINTER),
552                 Rvalue::Use(Operand::Consume(ret.clone())),
553             )
554         );
555         self.block(vec![ret_statement], TerminatorKind::Return, false);
556
557         // BB #5 (cleanup)
558         // `let end = beg;`
559         // `let mut beg = 0;`
560         // goto #6;
561         let end = beg;
562         let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
563         let init = self.make_statement(
564             StatementKind::Assign(
565                 Lvalue::Local(beg),
566                 Rvalue::Use(Operand::Constant(self.make_usize(0)))
567             )
568         );
569         self.block(vec![init], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
570
571         // BB #6 (cleanup): loop {
572         //     BB #7;
573         //     BB #8;
574         // }
575         // BB #9;
576         self.loop_header(Lvalue::Local(beg), Lvalue::Local(end),
577                          BasicBlock::new(7), BasicBlock::new(9), true);
578
579         // BB #7 (cleanup)
580         // `drop(ret[beg])`;
581         self.block(vec![], TerminatorKind::Drop {
582             location: ret.index(beg),
583             target: BasicBlock::new(8),
584             unwind: None,
585         }, true);
586
587         // BB #8 (cleanup)
588         // `beg = beg + 1;`
589         // `goto #6;`
590         let statement = self.make_statement(
591             StatementKind::Assign(
592                 Lvalue::Local(beg),
593                 Rvalue::BinaryOp(
594                     BinOp::Add,
595                     Operand::Consume(Lvalue::Local(beg)),
596                     Operand::Constant(self.make_usize(1))
597                 )
598             )
599         );
600         self.block(vec![statement], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
601
602         // BB #9 (resume)
603         self.block(vec![], TerminatorKind::Resume, true);
604     }
605
606     fn tuple_shim(&mut self, tys: &ty::Slice<ty::Ty<'tcx>>) {
607         let rcvr = Lvalue::Local(Local::new(1+0)).deref();
608
609         let mut returns = Vec::new();
610         for (i, ity) in tys.iter().enumerate() {
611             let rcvr_field = rcvr.clone().field(Field::new(i), *ity);
612
613             // BB #(2i)
614             // `returns[i] = Clone::clone(&rcvr.i);`
615             // Goto #(2i + 2) if ok, #(2i + 1) if unwinding happens.
616             returns.push(
617                 self.make_clone_call(
618                     *ity,
619                     rcvr_field,
620                     BasicBlock::new(2 * i + 2),
621                     BasicBlock::new(2 * i + 1),
622                 )
623             );
624
625             // BB #(2i + 1) (cleanup)
626             if i == 0 {
627                 // Nothing to drop, just resume.
628                 self.block(vec![], TerminatorKind::Resume, true);
629             } else {
630                 // Drop previous field and goto previous cleanup block.
631                 self.block(vec![], TerminatorKind::Drop {
632                     location: returns[i - 1].clone(),
633                     target: BasicBlock::new(2 * i - 1),
634                     unwind: None,
635                 }, true);
636             }
637         }
638
639         // `return (returns[0], returns[1], ..., returns[tys.len() - 1]);`
640         let ret_statement = self.make_statement(
641             StatementKind::Assign(
642                 Lvalue::Local(RETURN_POINTER),
643                 Rvalue::Aggregate(
644                     box AggregateKind::Tuple,
645                     returns.into_iter().map(Operand::Consume).collect()
646                 )
647             )
648         );
649        self.block(vec![ret_statement], TerminatorKind::Return, false);
650     }
651 }
652
653 /// Build a "call" shim for `def_id`. The shim calls the
654 /// function specified by `call_kind`, first adjusting its first
655 /// argument according to `rcvr_adjustment`.
656 ///
657 /// If `untuple_args` is a vec of types, the second argument of the
658 /// function will be untupled as these types.
659 fn build_call_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
660                              def_id: DefId,
661                              rcvr_adjustment: Adjustment,
662                              call_kind: CallKind,
663                              untuple_args: Option<&[Ty<'tcx>]>)
664                              -> Mir<'tcx>
665 {
666     debug!("build_call_shim(def_id={:?}, rcvr_adjustment={:?}, \
667             call_kind={:?}, untuple_args={:?})",
668            def_id, rcvr_adjustment, call_kind, untuple_args);
669
670     let sig = tcx.fn_sig(def_id);
671     let sig = tcx.erase_late_bound_regions(&sig);
672     let span = tcx.def_span(def_id);
673
674     debug!("build_call_shim: sig={:?}", sig);
675
676     let mut local_decls = local_decls_for_sig(&sig, span);
677     let source_info = SourceInfo { span, scope: ARGUMENT_VISIBILITY_SCOPE };
678
679     let rcvr_arg = Local::new(1+0);
680     let rcvr_l = Lvalue::Local(rcvr_arg);
681     let mut statements = vec![];
682
683     let rcvr = match rcvr_adjustment {
684         Adjustment::Identity => Operand::Consume(rcvr_l),
685         Adjustment::Deref => Operand::Consume(rcvr_l.deref()),
686         Adjustment::RefMut => {
687             // let rcvr = &mut rcvr;
688             let ref_rcvr = local_decls.push(temp_decl(
689                 Mutability::Not,
690                 tcx.mk_ref(tcx.types.re_erased, ty::TypeAndMut {
691                     ty: sig.inputs()[0],
692                     mutbl: hir::Mutability::MutMutable
693                 }),
694                 span
695             ));
696             statements.push(Statement {
697                 source_info,
698                 kind: StatementKind::Assign(
699                     Lvalue::Local(ref_rcvr),
700                     Rvalue::Ref(tcx.types.re_erased, BorrowKind::Mut, rcvr_l)
701                 )
702             });
703             Operand::Consume(Lvalue::Local(ref_rcvr))
704         }
705     };
706
707     let (callee, mut args) = match call_kind {
708         CallKind::Indirect => (rcvr, vec![]),
709         CallKind::Direct(def_id) => (
710             Operand::Constant(box Constant {
711                 span,
712                 ty: tcx.type_of(def_id),
713                 literal: Literal::Value {
714                     value: ConstVal::Function(def_id,
715                         Substs::identity_for_item(tcx, def_id)),
716                 },
717             }),
718             vec![rcvr]
719         )
720     };
721
722     if let Some(untuple_args) = untuple_args {
723         args.extend(untuple_args.iter().enumerate().map(|(i, ity)| {
724             let arg_lv = Lvalue::Local(Local::new(1+1));
725             Operand::Consume(arg_lv.field(Field::new(i), *ity))
726         }));
727     } else {
728         args.extend((1..sig.inputs().len()).map(|i| {
729             Operand::Consume(Lvalue::Local(Local::new(1+i)))
730         }));
731     }
732
733     let mut blocks = IndexVec::new();
734     let block = |blocks: &mut IndexVec<_, _>, statements, kind, is_cleanup| {
735         blocks.push(BasicBlockData {
736             statements,
737             terminator: Some(Terminator { source_info, kind }),
738             is_cleanup
739         })
740     };
741
742     // BB #0
743     block(&mut blocks, statements, TerminatorKind::Call {
744         func: callee,
745         args,
746         destination: Some((Lvalue::Local(RETURN_POINTER),
747                            BasicBlock::new(1))),
748         cleanup: if let Adjustment::RefMut = rcvr_adjustment {
749             Some(BasicBlock::new(3))
750         } else {
751             None
752         }
753     }, false);
754
755     if let Adjustment::RefMut = rcvr_adjustment {
756         // BB #1 - drop for Self
757         block(&mut blocks, vec![], TerminatorKind::Drop {
758             location: Lvalue::Local(rcvr_arg),
759             target: BasicBlock::new(2),
760             unwind: None
761         }, false);
762     }
763     // BB #1/#2 - return
764     block(&mut blocks, vec![], TerminatorKind::Return, false);
765     if let Adjustment::RefMut = rcvr_adjustment {
766         // BB #3 - drop if closure panics
767         block(&mut blocks, vec![], TerminatorKind::Drop {
768             location: Lvalue::Local(rcvr_arg),
769             target: BasicBlock::new(4),
770             unwind: None
771         }, true);
772
773         // BB #4 - resume
774         block(&mut blocks, vec![], TerminatorKind::Resume, true);
775     }
776
777     let mut mir = Mir::new(
778         blocks,
779         IndexVec::from_elem_n(
780             VisibilityScopeData { span: span, parent_scope: None }, 1
781         ),
782         IndexVec::new(),
783         sig.output(),
784         None,
785         local_decls,
786         sig.inputs().len(),
787         vec![],
788         span
789     );
790     if let Abi::RustCall = sig.abi {
791         mir.spread_arg = Some(Local::new(sig.inputs().len()));
792     }
793     mir
794 }
795
796 pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>,
797                                       ctor_id: ast::NodeId,
798                                       fields: &[hir::StructField],
799                                       span: Span)
800                                       -> (Mir<'tcx>, MirSource)
801 {
802     let tcx = infcx.tcx;
803     let def_id = tcx.hir.local_def_id(ctor_id);
804     let sig = tcx.no_late_bound_regions(&tcx.fn_sig(def_id))
805         .expect("LBR in ADT constructor signature");
806     let sig = tcx.erase_regions(&sig);
807
808     let (adt_def, substs) = match sig.output().sty {
809         ty::TyAdt(adt_def, substs) => (adt_def, substs),
810         _ => bug!("unexpected type for ADT ctor {:?}", sig.output())
811     };
812
813     debug!("build_ctor: def_id={:?} sig={:?} fields={:?}", def_id, sig, fields);
814
815     let local_decls = local_decls_for_sig(&sig, span);
816
817     let source_info = SourceInfo {
818         span,
819         scope: ARGUMENT_VISIBILITY_SCOPE
820     };
821
822     let variant_no = if adt_def.is_enum() {
823         adt_def.variant_index_with_id(def_id)
824     } else {
825         0
826     };
827
828     // return = ADT(arg0, arg1, ...); return
829     let start_block = BasicBlockData {
830         statements: vec![Statement {
831             source_info,
832             kind: StatementKind::Assign(
833                 Lvalue::Local(RETURN_POINTER),
834                 Rvalue::Aggregate(
835                     box AggregateKind::Adt(adt_def, variant_no, substs, None),
836                     (1..sig.inputs().len()+1).map(|i| {
837                         Operand::Consume(Lvalue::Local(Local::new(i)))
838                     }).collect()
839                 )
840             )
841         }],
842         terminator: Some(Terminator {
843             source_info,
844             kind: TerminatorKind::Return,
845         }),
846         is_cleanup: false
847     };
848
849     let mir = Mir::new(
850         IndexVec::from_elem_n(start_block, 1),
851         IndexVec::from_elem_n(
852             VisibilityScopeData { span: span, parent_scope: None }, 1
853         ),
854         IndexVec::new(),
855         sig.output(),
856         None,
857         local_decls,
858         sig.inputs().len(),
859         vec![],
860         span
861     );
862     (mir, MirSource::Fn(ctor_id))
863 }