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