]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/shim.rs
rustdoc: pretty-print Unevaluated expressions in types.
[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) => {
296             let len = len.val.to_const_int().unwrap().to_u64().unwrap();
297             builder.array_shim(ty, len)
298         }
299         ty::TyTuple(tys, _) => builder.tuple_shim(tys),
300         _ => {
301             bug!("clone shim for `{:?}` which is not `Copy` and is not an aggregate", self_ty);
302         }
303     };
304
305     builder.into_mir()
306 }
307
308 struct CloneShimBuilder<'a, 'tcx: 'a> {
309     tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
310     def_id: DefId,
311     local_decls: IndexVec<Local, LocalDecl<'tcx>>,
312     blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
313     span: Span,
314     sig: ty::FnSig<'tcx>,
315 }
316
317 impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
318     fn new(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Self {
319         let sig = tcx.fn_sig(def_id);
320         let sig = tcx.erase_late_bound_regions(&sig);
321         let span = tcx.def_span(def_id);
322
323         CloneShimBuilder {
324             tcx,
325             def_id,
326             local_decls: local_decls_for_sig(&sig, span),
327             blocks: IndexVec::new(),
328             span,
329             sig,
330         }
331     }
332
333     fn into_mir(self) -> Mir<'tcx> {
334         Mir::new(
335             self.blocks,
336             IndexVec::from_elem_n(
337                 VisibilityScopeData { span: self.span, parent_scope: None }, 1
338             ),
339             IndexVec::new(),
340             self.sig.output(),
341             None,
342             self.local_decls,
343             self.sig.inputs().len(),
344             vec![],
345             self.span
346         )
347     }
348
349     fn source_info(&self) -> SourceInfo {
350         SourceInfo { span: self.span, scope: ARGUMENT_VISIBILITY_SCOPE }
351     }
352
353     fn block(
354         &mut self,
355         statements: Vec<Statement<'tcx>>,
356         kind: TerminatorKind<'tcx>,
357         is_cleanup: bool
358     ) -> BasicBlock {
359         let source_info = self.source_info();
360         self.blocks.push(BasicBlockData {
361             statements,
362             terminator: Some(Terminator { source_info, kind }),
363             is_cleanup,
364         })
365     }
366
367     fn make_statement(&self, kind: StatementKind<'tcx>) -> Statement<'tcx> {
368         Statement {
369             source_info: self.source_info(),
370             kind,
371         }
372     }
373
374     fn copy_shim(&mut self) {
375         let rcvr = Lvalue::Local(Local::new(1+0)).deref();
376         let ret_statement = self.make_statement(
377             StatementKind::Assign(
378                 Lvalue::Local(RETURN_POINTER),
379                 Rvalue::Use(Operand::Consume(rcvr))
380             )
381         );
382         self.block(vec![ret_statement], TerminatorKind::Return, false);
383     }
384
385     fn make_lvalue(&mut self, mutability: Mutability, ty: ty::Ty<'tcx>) -> Lvalue<'tcx> {
386         let span = self.span;
387         Lvalue::Local(
388             self.local_decls.push(temp_decl(mutability, ty, span))
389         )
390     }
391
392     fn make_clone_call(
393         &mut self,
394         ty: ty::Ty<'tcx>,
395         rcvr_field: Lvalue<'tcx>,
396         next: BasicBlock,
397         cleanup: BasicBlock
398     ) -> Lvalue<'tcx> {
399         let tcx = self.tcx;
400
401         let substs = Substs::for_item(
402             tcx,
403             self.def_id,
404             |_, _| tcx.types.re_erased,
405             |_, _| ty
406         );
407
408         // `func == Clone::clone(&ty) -> ty`
409         let func_ty = tcx.mk_fn_def(self.def_id, substs);
410         let func = Operand::Constant(box Constant {
411             span: self.span,
412             ty: func_ty,
413             literal: Literal::Value {
414                 value: tcx.mk_const(ty::Const {
415                     val: ConstVal::Function(self.def_id, substs),
416                     ty: func_ty
417                 }),
418             },
419         });
420
421         let ref_loc = self.make_lvalue(
422             Mutability::Not,
423             tcx.mk_ref(tcx.types.re_erased, ty::TypeAndMut {
424                 ty,
425                 mutbl: hir::Mutability::MutImmutable,
426             })
427         );
428
429         let loc = self.make_lvalue(Mutability::Not, ty);
430
431         // `let ref_loc: &ty = &rcvr_field;`
432         let statement = self.make_statement(
433             StatementKind::Assign(
434                 ref_loc.clone(),
435                 Rvalue::Ref(tcx.types.re_erased, BorrowKind::Shared, rcvr_field)
436             )
437         );
438
439         // `let loc = Clone::clone(ref_loc);`
440         self.block(vec![statement], TerminatorKind::Call {
441             func,
442             args: vec![Operand::Consume(ref_loc)],
443             destination: Some((loc.clone(), next)),
444             cleanup: Some(cleanup),
445         }, false);
446
447         loc
448     }
449
450     fn loop_header(
451         &mut self,
452         beg: Lvalue<'tcx>,
453         end: Lvalue<'tcx>,
454         loop_body: BasicBlock,
455         loop_end: BasicBlock,
456         is_cleanup: bool
457     ) {
458         let tcx = self.tcx;
459
460         let cond = self.make_lvalue(Mutability::Mut, tcx.types.bool);
461         let compute_cond = self.make_statement(
462             StatementKind::Assign(
463                 cond.clone(),
464                 Rvalue::BinaryOp(BinOp::Ne, Operand::Consume(end), Operand::Consume(beg))
465             )
466         );
467
468         // `if end != beg { goto loop_body; } else { goto loop_end; }`
469         self.block(
470             vec![compute_cond],
471             TerminatorKind::if_(tcx, Operand::Consume(cond), loop_body, loop_end),
472             is_cleanup
473         );
474     }
475
476     fn make_usize(&self, value: u64) -> Box<Constant<'tcx>> {
477         let value = ConstUsize::new(value, self.tcx.sess.target.usize_ty).unwrap();
478         box Constant {
479             span: self.span,
480             ty: self.tcx.types.usize,
481             literal: Literal::Value {
482                 value: self.tcx.mk_const(ty::Const {
483                     val: ConstVal::Integral(ConstInt::Usize(value)),
484                     ty: self.tcx.types.usize,
485                 })
486             }
487         }
488     }
489
490     fn array_shim(&mut self, ty: ty::Ty<'tcx>, len: u64) {
491         let tcx = self.tcx;
492         let span = self.span;
493         let rcvr = Lvalue::Local(Local::new(1+0)).deref();
494
495         let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
496         let end = self.make_lvalue(Mutability::Not, tcx.types.usize);
497         let ret = self.make_lvalue(Mutability::Mut, tcx.mk_array(ty, len));
498
499         // BB #0
500         // `let mut beg = 0;`
501         // `let end = len;`
502         // `goto #1;`
503         let inits = vec![
504             self.make_statement(
505                 StatementKind::Assign(
506                     Lvalue::Local(beg),
507                     Rvalue::Use(Operand::Constant(self.make_usize(0)))
508                 )
509             ),
510             self.make_statement(
511                 StatementKind::Assign(
512                     end.clone(),
513                     Rvalue::Use(Operand::Constant(self.make_usize(len)))
514                 )
515             )
516         ];
517         self.block(inits, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
518
519         // BB #1: loop {
520         //     BB #2;
521         //     BB #3;
522         // }
523         // BB #4;
524         self.loop_header(Lvalue::Local(beg), end, BasicBlock::new(2), BasicBlock::new(4), false);
525
526         // BB #2
527         // `let cloned = Clone::clone(rcvr[beg])`;
528         // Goto #3 if ok, #5 if unwinding happens.
529         let rcvr_field = rcvr.clone().index(beg);
530         let cloned = self.make_clone_call(ty, rcvr_field, BasicBlock::new(3), BasicBlock::new(5));
531
532         // BB #3
533         // `ret[beg] = cloned;`
534         // `beg = beg + 1;`
535         // `goto #1`;
536         let ret_field = ret.clone().index(beg);
537         let statements = vec![
538             self.make_statement(
539                 StatementKind::Assign(
540                     ret_field,
541                     Rvalue::Use(Operand::Consume(cloned))
542                 )
543             ),
544             self.make_statement(
545                 StatementKind::Assign(
546                     Lvalue::Local(beg),
547                     Rvalue::BinaryOp(
548                         BinOp::Add,
549                         Operand::Consume(Lvalue::Local(beg)),
550                         Operand::Constant(self.make_usize(1))
551                     )
552                 )
553             )
554         ];
555         self.block(statements, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
556
557         // BB #4
558         // `return ret;`
559         let ret_statement = self.make_statement(
560             StatementKind::Assign(
561                 Lvalue::Local(RETURN_POINTER),
562                 Rvalue::Use(Operand::Consume(ret.clone())),
563             )
564         );
565         self.block(vec![ret_statement], TerminatorKind::Return, false);
566
567         // BB #5 (cleanup)
568         // `let end = beg;`
569         // `let mut beg = 0;`
570         // goto #6;
571         let end = beg;
572         let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
573         let init = self.make_statement(
574             StatementKind::Assign(
575                 Lvalue::Local(beg),
576                 Rvalue::Use(Operand::Constant(self.make_usize(0)))
577             )
578         );
579         self.block(vec![init], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
580
581         // BB #6 (cleanup): loop {
582         //     BB #7;
583         //     BB #8;
584         // }
585         // BB #9;
586         self.loop_header(Lvalue::Local(beg), Lvalue::Local(end),
587                          BasicBlock::new(7), BasicBlock::new(9), true);
588
589         // BB #7 (cleanup)
590         // `drop(ret[beg])`;
591         self.block(vec![], TerminatorKind::Drop {
592             location: ret.index(beg),
593             target: BasicBlock::new(8),
594             unwind: None,
595         }, true);
596
597         // BB #8 (cleanup)
598         // `beg = beg + 1;`
599         // `goto #6;`
600         let statement = self.make_statement(
601             StatementKind::Assign(
602                 Lvalue::Local(beg),
603                 Rvalue::BinaryOp(
604                     BinOp::Add,
605                     Operand::Consume(Lvalue::Local(beg)),
606                     Operand::Constant(self.make_usize(1))
607                 )
608             )
609         );
610         self.block(vec![statement], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
611
612         // BB #9 (resume)
613         self.block(vec![], TerminatorKind::Resume, true);
614     }
615
616     fn tuple_shim(&mut self, tys: &ty::Slice<ty::Ty<'tcx>>) {
617         let rcvr = Lvalue::Local(Local::new(1+0)).deref();
618
619         let mut returns = Vec::new();
620         for (i, ity) in tys.iter().enumerate() {
621             let rcvr_field = rcvr.clone().field(Field::new(i), *ity);
622
623             // BB #(2i)
624             // `returns[i] = Clone::clone(&rcvr.i);`
625             // Goto #(2i + 2) if ok, #(2i + 1) if unwinding happens.
626             returns.push(
627                 self.make_clone_call(
628                     *ity,
629                     rcvr_field,
630                     BasicBlock::new(2 * i + 2),
631                     BasicBlock::new(2 * i + 1),
632                 )
633             );
634
635             // BB #(2i + 1) (cleanup)
636             if i == 0 {
637                 // Nothing to drop, just resume.
638                 self.block(vec![], TerminatorKind::Resume, true);
639             } else {
640                 // Drop previous field and goto previous cleanup block.
641                 self.block(vec![], TerminatorKind::Drop {
642                     location: returns[i - 1].clone(),
643                     target: BasicBlock::new(2 * i - 1),
644                     unwind: None,
645                 }, true);
646             }
647         }
648
649         // `return (returns[0], returns[1], ..., returns[tys.len() - 1]);`
650         let ret_statement = self.make_statement(
651             StatementKind::Assign(
652                 Lvalue::Local(RETURN_POINTER),
653                 Rvalue::Aggregate(
654                     box AggregateKind::Tuple,
655                     returns.into_iter().map(Operand::Consume).collect()
656                 )
657             )
658         );
659        self.block(vec![ret_statement], TerminatorKind::Return, false);
660     }
661 }
662
663 /// Build a "call" shim for `def_id`. The shim calls the
664 /// function specified by `call_kind`, first adjusting its first
665 /// argument according to `rcvr_adjustment`.
666 ///
667 /// If `untuple_args` is a vec of types, the second argument of the
668 /// function will be untupled as these types.
669 fn build_call_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
670                              def_id: DefId,
671                              rcvr_adjustment: Adjustment,
672                              call_kind: CallKind,
673                              untuple_args: Option<&[Ty<'tcx>]>)
674                              -> Mir<'tcx>
675 {
676     debug!("build_call_shim(def_id={:?}, rcvr_adjustment={:?}, \
677             call_kind={:?}, untuple_args={:?})",
678            def_id, rcvr_adjustment, call_kind, untuple_args);
679
680     let sig = tcx.fn_sig(def_id);
681     let sig = tcx.erase_late_bound_regions(&sig);
682     let span = tcx.def_span(def_id);
683
684     debug!("build_call_shim: sig={:?}", sig);
685
686     let mut local_decls = local_decls_for_sig(&sig, span);
687     let source_info = SourceInfo { span, scope: ARGUMENT_VISIBILITY_SCOPE };
688
689     let rcvr_arg = Local::new(1+0);
690     let rcvr_l = Lvalue::Local(rcvr_arg);
691     let mut statements = vec![];
692
693     let rcvr = match rcvr_adjustment {
694         Adjustment::Identity => Operand::Consume(rcvr_l),
695         Adjustment::Deref => Operand::Consume(rcvr_l.deref()),
696         Adjustment::RefMut => {
697             // let rcvr = &mut rcvr;
698             let ref_rcvr = local_decls.push(temp_decl(
699                 Mutability::Not,
700                 tcx.mk_ref(tcx.types.re_erased, ty::TypeAndMut {
701                     ty: sig.inputs()[0],
702                     mutbl: hir::Mutability::MutMutable
703                 }),
704                 span
705             ));
706             statements.push(Statement {
707                 source_info,
708                 kind: StatementKind::Assign(
709                     Lvalue::Local(ref_rcvr),
710                     Rvalue::Ref(tcx.types.re_erased, BorrowKind::Mut, rcvr_l)
711                 )
712             });
713             Operand::Consume(Lvalue::Local(ref_rcvr))
714         }
715     };
716
717     let (callee, mut args) = match call_kind {
718         CallKind::Indirect => (rcvr, vec![]),
719         CallKind::Direct(def_id) => {
720             let ty = tcx.type_of(def_id);
721             (Operand::Constant(box Constant {
722                 span,
723                 ty,
724                 literal: Literal::Value {
725                     value: tcx.mk_const(ty::Const {
726                         val: ConstVal::Function(def_id,
727                             Substs::identity_for_item(tcx, def_id)),
728                         ty
729                     }),
730                 },
731              }),
732              vec![rcvr])
733         }
734     };
735
736     if let Some(untuple_args) = untuple_args {
737         args.extend(untuple_args.iter().enumerate().map(|(i, ity)| {
738             let arg_lv = Lvalue::Local(Local::new(1+1));
739             Operand::Consume(arg_lv.field(Field::new(i), *ity))
740         }));
741     } else {
742         args.extend((1..sig.inputs().len()).map(|i| {
743             Operand::Consume(Lvalue::Local(Local::new(1+i)))
744         }));
745     }
746
747     let mut blocks = IndexVec::new();
748     let block = |blocks: &mut IndexVec<_, _>, statements, kind, is_cleanup| {
749         blocks.push(BasicBlockData {
750             statements,
751             terminator: Some(Terminator { source_info, kind }),
752             is_cleanup
753         })
754     };
755
756     // BB #0
757     block(&mut blocks, statements, TerminatorKind::Call {
758         func: callee,
759         args,
760         destination: Some((Lvalue::Local(RETURN_POINTER),
761                            BasicBlock::new(1))),
762         cleanup: if let Adjustment::RefMut = rcvr_adjustment {
763             Some(BasicBlock::new(3))
764         } else {
765             None
766         }
767     }, false);
768
769     if let Adjustment::RefMut = rcvr_adjustment {
770         // BB #1 - drop for Self
771         block(&mut blocks, vec![], TerminatorKind::Drop {
772             location: Lvalue::Local(rcvr_arg),
773             target: BasicBlock::new(2),
774             unwind: None
775         }, false);
776     }
777     // BB #1/#2 - return
778     block(&mut blocks, vec![], TerminatorKind::Return, false);
779     if let Adjustment::RefMut = rcvr_adjustment {
780         // BB #3 - drop if closure panics
781         block(&mut blocks, vec![], TerminatorKind::Drop {
782             location: Lvalue::Local(rcvr_arg),
783             target: BasicBlock::new(4),
784             unwind: None
785         }, true);
786
787         // BB #4 - resume
788         block(&mut blocks, vec![], TerminatorKind::Resume, true);
789     }
790
791     let mut mir = Mir::new(
792         blocks,
793         IndexVec::from_elem_n(
794             VisibilityScopeData { span: span, parent_scope: None }, 1
795         ),
796         IndexVec::new(),
797         sig.output(),
798         None,
799         local_decls,
800         sig.inputs().len(),
801         vec![],
802         span
803     );
804     if let Abi::RustCall = sig.abi {
805         mir.spread_arg = Some(Local::new(sig.inputs().len()));
806     }
807     mir
808 }
809
810 pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>,
811                                       ctor_id: ast::NodeId,
812                                       fields: &[hir::StructField],
813                                       span: Span)
814                                       -> (Mir<'tcx>, MirSource)
815 {
816     let tcx = infcx.tcx;
817     let def_id = tcx.hir.local_def_id(ctor_id);
818     let sig = tcx.no_late_bound_regions(&tcx.fn_sig(def_id))
819         .expect("LBR in ADT constructor signature");
820     let sig = tcx.erase_regions(&sig);
821
822     let (adt_def, substs) = match sig.output().sty {
823         ty::TyAdt(adt_def, substs) => (adt_def, substs),
824         _ => bug!("unexpected type for ADT ctor {:?}", sig.output())
825     };
826
827     debug!("build_ctor: def_id={:?} sig={:?} fields={:?}", def_id, sig, fields);
828
829     let local_decls = local_decls_for_sig(&sig, span);
830
831     let source_info = SourceInfo {
832         span,
833         scope: ARGUMENT_VISIBILITY_SCOPE
834     };
835
836     let variant_no = if adt_def.is_enum() {
837         adt_def.variant_index_with_id(def_id)
838     } else {
839         0
840     };
841
842     // return = ADT(arg0, arg1, ...); return
843     let start_block = BasicBlockData {
844         statements: vec![Statement {
845             source_info,
846             kind: StatementKind::Assign(
847                 Lvalue::Local(RETURN_POINTER),
848                 Rvalue::Aggregate(
849                     box AggregateKind::Adt(adt_def, variant_no, substs, None),
850                     (1..sig.inputs().len()+1).map(|i| {
851                         Operand::Consume(Lvalue::Local(Local::new(i)))
852                     }).collect()
853                 )
854             )
855         }],
856         terminator: Some(Terminator {
857             source_info,
858             kind: TerminatorKind::Return,
859         }),
860         is_cleanup: false
861     };
862
863     let mir = Mir::new(
864         IndexVec::from_elem_n(start_block, 1),
865         IndexVec::from_elem_n(
866             VisibilityScopeData { span: span, parent_scope: None }, 1
867         ),
868         IndexVec::new(),
869         sig.output(),
870         None,
871         local_decls,
872         sig.inputs().len(),
873         vec![],
874         span
875     );
876     (mir, MirSource::Fn(ctor_id))
877 }