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.
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.
12 use rustc::hir::def_id::DefId;
14 use rustc::middle::const_val::ConstVal;
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};
21 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
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;
35 pub fn provide(providers: &mut Providers) {
36 providers.mir_shims = make_shim;
39 fn make_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
40 instance: ty::InstanceDef<'tcx>)
43 debug!("make_shim({:?})", instance);
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)
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();
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.
80 CallKind::Direct(def_id),
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)
95 CallKind::Direct(call_mut),
99 ty::InstanceDef::DropGlue(def_id, ty) => {
100 build_drop_shim(tcx, def_id, ty)
102 ty::InstanceDef::CloneShim(def_id, ty) => {
103 let name = tcx.item_name(def_id);
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);
110 bug!("builtin clone shim {:?} not supported", instance)
113 ty::InstanceDef::Intrinsic(_) => {
114 bug!("creating shims from intrinsics ({:?}) is unsupported", instance)
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);
125 tcx.alloc_mir(result)
128 #[derive(Copy, Clone, Debug, PartialEq)]
135 #[derive(Copy, Clone, Debug, PartialEq)]
141 fn temp_decl(mutability: Mutability, ty: Ty, span: Span) -> LocalDecl {
143 mutability, ty, name: None,
144 source_info: SourceInfo { scope: ARGUMENT_VISIBILITY_SCOPE, span },
145 lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
147 is_user_variable: false
151 fn local_decls_for_sig<'tcx>(sig: &ty::FnSig<'tcx>, span: Span)
152 -> IndexVec<Local, LocalDecl<'tcx>>
154 iter::once(temp_decl(Mutability::Mut, sig.output(), span))
155 .chain(sig.inputs().iter().map(
156 |ity| temp_decl(Mutability::Not, ity, span)))
160 fn build_drop_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
162 ty: Option<Ty<'tcx>>)
165 debug!("build_drop_shim(def_id={:?}, ty={:?})", def_id, ty);
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);
173 let substs = if let Some(ty) = ty {
174 tcx.mk_substs(iter::once(Kind::from(ty)))
176 Substs::identity_for_item(tcx, def_id)
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);
182 let source_info = SourceInfo { span, scope: ARGUMENT_VISIBILITY_SCOPE };
184 let return_block = BasicBlock::new(1);
185 let mut blocks = IndexVec::new();
186 let block = |blocks: &mut IndexVec<_, _>, kind| {
187 blocks.push(BasicBlockData {
189 terminator: Some(Terminator { source_info, kind }),
193 block(&mut blocks, TerminatorKind::Goto { target: return_block });
194 block(&mut blocks, TerminatorKind::Return);
196 let mut mir = Mir::new(
198 IndexVec::from_elem_n(
199 VisibilityScopeData { span: span, parent_scope: None }, 1
201 ClearOnDecode::Clear,
204 local_decls_for_sig(&sig, span),
210 if let Some(..) = ty {
212 let param_env = tcx.param_env(def_id);
213 let mut elaborator = DropShimElaborator {
215 patch: MirPatch::new(&mir),
219 let dropee = Lvalue::Local(Local::new(1+0)).deref();
220 let resume_block = elaborator.patch.resume_block();
221 elaborate_drops::elaborate_drop(
227 elaborate_drops::Unwind::To(resume_block),
232 patch.apply(&mut mir);
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>,
245 impl<'a, 'tcx> fmt::Debug for DropShimElaborator<'a, 'tcx> {
246 fn fmt(&self, _f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
251 impl<'a, 'tcx> DropElaborator<'a, 'tcx> for DropShimElaborator<'a, 'tcx> {
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 }
259 fn drop_style(&self, _path: Self::Path, mode: DropFlagMode) -> DropStyle {
260 if let DropFlagMode::Shallow = mode {
267 fn get_drop_flag(&mut self, _path: Self::Path) -> Option<Operand<'tcx>> {
271 fn clear_drop_flag(&mut self, _location: Location, _path: Self::Path, _mode: DropFlagMode) {
274 fn field_subpath(&self, _path: Self::Path, _field: Field) -> Option<Self::Path> {
277 fn deref_subpath(&self, _path: Self::Path) -> Option<Self::Path> {
280 fn downcast_subpath(&self, _path: Self::Path, _variant: usize) -> Option<Self::Path> {
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>,
291 debug!("build_clone_shim(def_id={:?})", def_id);
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);
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)
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)
308 ty::TyTuple(tys, _) => builder.tuple_like_shim(&**tys, AggregateKind::Tuple),
310 bug!("clone shim for `{:?}` which is not `Copy` and is not an aggregate", self_ty)
317 struct CloneShimBuilder<'a, 'tcx: 'a> {
318 tcx: TyCtxt<'a, 'tcx, 'tcx>,
320 local_decls: IndexVec<Local, LocalDecl<'tcx>>,
321 blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
323 sig: ty::FnSig<'tcx>,
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);
335 local_decls: local_decls_for_sig(&sig, span),
336 blocks: IndexVec::new(),
342 fn into_mir(self) -> Mir<'tcx> {
345 IndexVec::from_elem_n(
346 VisibilityScopeData { span: self.span, parent_scope: None }, 1
348 ClearOnDecode::Clear,
352 self.sig.inputs().len(),
358 fn source_info(&self) -> SourceInfo {
359 SourceInfo { span: self.span, scope: ARGUMENT_VISIBILITY_SCOPE }
364 statements: Vec<Statement<'tcx>>,
365 kind: TerminatorKind<'tcx>,
368 let source_info = self.source_info();
369 self.blocks.push(BasicBlockData {
371 terminator: Some(Terminator { source_info, kind }),
376 fn make_statement(&self, kind: StatementKind<'tcx>) -> Statement<'tcx> {
378 source_info: self.source_info(),
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))
391 self.block(vec![ret_statement], TerminatorKind::Return, false);
394 fn make_lvalue(&mut self, mutability: Mutability, ty: Ty<'tcx>) -> Lvalue<'tcx> {
395 let span = self.span;
397 self.local_decls.push(temp_decl(mutability, ty, span))
404 rcvr_field: Lvalue<'tcx>,
410 let substs = Substs::for_item(
413 |_, _| tcx.types.re_erased,
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 {
422 literal: Literal::Value {
423 value: tcx.mk_const(ty::Const {
424 val: ConstVal::Function(self.def_id, substs),
430 let ref_loc = self.make_lvalue(
432 tcx.mk_ref(tcx.types.re_erased, ty::TypeAndMut {
434 mutbl: hir::Mutability::MutImmutable,
438 let loc = self.make_lvalue(Mutability::Not, ty);
440 // `let ref_loc: &ty = &rcvr_field;`
441 let statement = self.make_statement(
442 StatementKind::Assign(
444 Rvalue::Ref(tcx.types.re_erased, BorrowKind::Shared, rcvr_field)
448 // `let loc = Clone::clone(ref_loc);`
449 self.block(vec![statement], TerminatorKind::Call {
451 args: vec![Operand::Consume(ref_loc)],
452 destination: Some((loc.clone(), next)),
453 cleanup: Some(cleanup),
463 loop_body: BasicBlock,
464 loop_end: BasicBlock,
469 let cond = self.make_lvalue(Mutability::Mut, tcx.types.bool);
470 let compute_cond = self.make_statement(
471 StatementKind::Assign(
473 Rvalue::BinaryOp(BinOp::Ne, Operand::Consume(end), Operand::Consume(beg))
477 // `if end != beg { goto loop_body; } else { goto loop_end; }`
480 TerminatorKind::if_(tcx, Operand::Consume(cond), loop_body, loop_end),
485 fn make_usize(&self, value: u64) -> Box<Constant<'tcx>> {
486 let value = ConstUsize::new(value, self.tcx.sess.target.usize_ty).unwrap();
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,
499 fn array_shim(&mut self, ty: Ty<'tcx>, len: u64) {
501 let span = self.span;
502 let rcvr = Lvalue::Local(Local::new(1+0)).deref();
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));
509 // `let mut beg = 0;`
514 StatementKind::Assign(
516 Rvalue::Use(Operand::Constant(self.make_usize(0)))
520 StatementKind::Assign(
522 Rvalue::Use(Operand::Constant(self.make_usize(len)))
526 self.block(inits, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
533 self.loop_header(Lvalue::Local(beg), end, BasicBlock::new(2), BasicBlock::new(4), false);
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));
542 // `ret[beg] = cloned;`
545 let ret_field = ret.clone().index(beg);
546 let statements = vec![
548 StatementKind::Assign(
550 Rvalue::Use(Operand::Consume(cloned))
554 StatementKind::Assign(
558 Operand::Consume(Lvalue::Local(beg)),
559 Operand::Constant(self.make_usize(1))
564 self.block(statements, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
568 let ret_statement = self.make_statement(
569 StatementKind::Assign(
570 Lvalue::Local(RETURN_POINTER),
571 Rvalue::Use(Operand::Consume(ret.clone())),
574 self.block(vec![ret_statement], TerminatorKind::Return, false);
578 // `let mut beg = 0;`
581 let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
582 let init = self.make_statement(
583 StatementKind::Assign(
585 Rvalue::Use(Operand::Constant(self.make_usize(0)))
588 self.block(vec![init], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
590 // BB #6 (cleanup): loop {
595 self.loop_header(Lvalue::Local(beg), Lvalue::Local(end),
596 BasicBlock::new(7), BasicBlock::new(9), true);
600 self.block(vec![], TerminatorKind::Drop {
601 location: ret.index(beg),
602 target: BasicBlock::new(8),
609 let statement = self.make_statement(
610 StatementKind::Assign(
614 Operand::Consume(Lvalue::Local(beg)),
615 Operand::Constant(self.make_usize(1))
619 self.block(vec![statement], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
622 self.block(vec![], TerminatorKind::Resume, true);
625 fn tuple_like_shim(&mut self, tys: &[ty::Ty<'tcx>], kind: AggregateKind<'tcx>) {
627 AggregateKind::Tuple | AggregateKind::Closure(..) => (),
628 _ => bug!("only tuples and closures are accepted"),
631 let rcvr = Lvalue::Local(Local::new(1+0)).deref();
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);
638 // `returns[i] = Clone::clone(&rcvr.i);`
639 // Goto #(2i + 2) if ok, #(2i + 1) if unwinding happens.
641 self.make_clone_call(
644 BasicBlock::new(2 * i + 2),
645 BasicBlock::new(2 * i + 1),
649 // BB #(2i + 1) (cleanup)
651 // Nothing to drop, just resume.
652 self.block(vec![], TerminatorKind::Resume, true);
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),
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),
669 returns.into_iter().map(Operand::Consume).collect()
673 self.block(vec![ret_statement], TerminatorKind::Return, false);
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`.
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>,
685 rcvr_adjustment: Adjustment,
687 untuple_args: Option<&[Ty<'tcx>]>)
690 debug!("build_call_shim(def_id={:?}, rcvr_adjustment={:?}, \
691 call_kind={:?}, untuple_args={:?})",
692 def_id, rcvr_adjustment, call_kind, untuple_args);
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);
698 debug!("build_call_shim: sig={:?}", sig);
700 let mut local_decls = local_decls_for_sig(&sig, span);
701 let source_info = SourceInfo { span, scope: ARGUMENT_VISIBILITY_SCOPE };
703 let rcvr_arg = Local::new(1+0);
704 let rcvr_l = Lvalue::Local(rcvr_arg);
705 let mut statements = vec![];
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(
714 tcx.mk_ref(tcx.types.re_erased, ty::TypeAndMut {
716 mutbl: hir::Mutability::MutMutable
720 statements.push(Statement {
722 kind: StatementKind::Assign(
723 Lvalue::Local(ref_rcvr),
724 Rvalue::Ref(tcx.types.re_erased, BorrowKind::Mut, rcvr_l)
727 Operand::Consume(Lvalue::Local(ref_rcvr))
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 {
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)),
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))
756 args.extend((1..sig.inputs().len()).map(|i| {
757 Operand::Consume(Lvalue::Local(Local::new(1+i)))
761 let mut blocks = IndexVec::new();
762 let block = |blocks: &mut IndexVec<_, _>, statements, kind, is_cleanup| {
763 blocks.push(BasicBlockData {
765 terminator: Some(Terminator { source_info, kind }),
771 block(&mut blocks, statements, TerminatorKind::Call {
774 destination: Some((Lvalue::Local(RETURN_POINTER),
775 BasicBlock::new(1))),
776 cleanup: if let Adjustment::RefMut = rcvr_adjustment {
777 Some(BasicBlock::new(3))
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),
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),
802 block(&mut blocks, vec![], TerminatorKind::Resume, true);
805 let mut mir = Mir::new(
807 IndexVec::from_elem_n(
808 VisibilityScopeData { span: span, parent_scope: None }, 1
810 ClearOnDecode::Clear,
818 if let Abi::RustCall = sig.abi {
819 mir.spread_arg = Some(Local::new(sig.inputs().len()));
824 pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>,
825 ctor_id: ast::NodeId,
826 fields: &[hir::StructField],
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);
838 // Normalize the sig now that we have liberated the late-bound
840 let sig = gcx.normalize_associated_type_in_env(&sig, param_env);
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())
847 debug!("build_ctor: def_id={:?} sig={:?} fields={:?}", def_id, sig, fields);
849 let local_decls = local_decls_for_sig(&sig, span);
851 let source_info = SourceInfo {
853 scope: ARGUMENT_VISIBILITY_SCOPE
856 let variant_no = if adt_def.is_enum() {
857 adt_def.variant_index_with_id(def_id)
862 // return = ADT(arg0, arg1, ...); return
863 let start_block = BasicBlockData {
864 statements: vec![Statement {
866 kind: StatementKind::Assign(
867 Lvalue::Local(RETURN_POINTER),
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)))
876 terminator: Some(Terminator {
878 kind: TerminatorKind::Return,
884 IndexVec::from_elem_n(start_block, 1),
885 IndexVec::from_elem_n(
886 VisibilityScopeData { span: span, parent_scope: None }, 1
888 ClearOnDecode::Clear,