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::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};
22 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
31 use transform::{add_call_guards, 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: ty::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).as_str();
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 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);
123 tcx.alloc_mir(result)
126 #[derive(Copy, Clone, Debug, PartialEq)]
133 #[derive(Copy, Clone, Debug, PartialEq)]
139 fn temp_decl(mutability: Mutability, ty: Ty, span: Span) -> LocalDecl {
141 mutability, ty, name: None,
142 source_info: SourceInfo { scope: ARGUMENT_VISIBILITY_SCOPE, span },
144 is_user_variable: false
148 fn local_decls_for_sig<'tcx>(sig: &ty::FnSig<'tcx>, span: Span)
149 -> IndexVec<Local, LocalDecl<'tcx>>
151 iter::once(temp_decl(Mutability::Mut, sig.output(), span))
152 .chain(sig.inputs().iter().map(
153 |ity| temp_decl(Mutability::Not, ity, span)))
157 fn build_drop_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
159 ty: Option<Ty<'tcx>>)
162 debug!("build_drop_shim(def_id={:?}, ty={:?})", def_id, ty);
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);
170 let substs = if let Some(ty) = ty {
171 tcx.mk_substs(iter::once(Kind::from(ty)))
173 Substs::identity_for_item(tcx, def_id)
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);
179 let source_info = SourceInfo { span, scope: ARGUMENT_VISIBILITY_SCOPE };
181 let return_block = BasicBlock::new(1);
182 let mut blocks = IndexVec::new();
183 let block = |blocks: &mut IndexVec<_, _>, kind| {
184 blocks.push(BasicBlockData {
186 terminator: Some(Terminator { source_info, kind }),
190 block(&mut blocks, TerminatorKind::Goto { target: return_block });
191 block(&mut blocks, TerminatorKind::Return);
193 let mut mir = Mir::new(
195 IndexVec::from_elem_n(
196 VisibilityScopeData { span: span, parent_scope: None }, 1
201 local_decls_for_sig(&sig, span),
207 if let Some(..) = ty {
209 let param_env = tcx.param_env(def_id);
210 let mut elaborator = DropShimElaborator {
212 patch: MirPatch::new(&mir),
216 let dropee = Lvalue::Local(Local::new(1+0)).deref();
217 let resume_block = elaborator.patch.resume_block();
218 elaborate_drops::elaborate_drop(
224 elaborate_drops::Unwind::To(resume_block),
229 patch.apply(&mut mir);
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>,
242 impl<'a, 'tcx> fmt::Debug for DropShimElaborator<'a, 'tcx> {
243 fn fmt(&self, _f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
248 impl<'a, 'tcx> DropElaborator<'a, 'tcx> for DropShimElaborator<'a, 'tcx> {
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 }
256 fn drop_style(&self, _path: Self::Path, mode: DropFlagMode) -> DropStyle {
257 if let DropFlagMode::Shallow = mode {
264 fn get_drop_flag(&mut self, _path: Self::Path) -> Option<Operand<'tcx>> {
268 fn clear_drop_flag(&mut self, _location: Location, _path: Self::Path, _mode: DropFlagMode) {
271 fn field_subpath(&self, _path: Self::Path, _field: Field) -> Option<Self::Path> {
274 fn deref_subpath(&self, _path: Self::Path) -> Option<Self::Path> {
277 fn downcast_subpath(&self, _path: Self::Path, _variant: usize) -> Option<Self::Path> {
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>,
285 self_ty: ty::Ty<'tcx>)
288 debug!("build_clone_shim(def_id={:?})", def_id);
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);
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),
298 bug!("clone shim for `{:?}` which is not `Copy` and is not an aggregate", self_ty);
305 struct CloneShimBuilder<'a, 'tcx: 'a> {
306 tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
308 local_decls: IndexVec<Local, LocalDecl<'tcx>>,
309 blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
311 sig: ty::FnSig<'tcx>,
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);
323 local_decls: local_decls_for_sig(&sig, span),
324 blocks: IndexVec::new(),
330 fn into_mir(self) -> Mir<'tcx> {
333 IndexVec::from_elem_n(
334 VisibilityScopeData { span: self.span, parent_scope: None }, 1
340 self.sig.inputs().len(),
346 fn source_info(&self) -> SourceInfo {
347 SourceInfo { span: self.span, scope: ARGUMENT_VISIBILITY_SCOPE }
352 statements: Vec<Statement<'tcx>>,
353 kind: TerminatorKind<'tcx>,
356 let source_info = self.source_info();
357 self.blocks.push(BasicBlockData {
359 terminator: Some(Terminator { source_info, kind }),
364 fn make_statement(&self, kind: StatementKind<'tcx>) -> Statement<'tcx> {
366 source_info: self.source_info(),
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))
379 self.block(vec![ret_statement], TerminatorKind::Return, false);
382 fn make_lvalue(&mut self, mutability: Mutability, ty: ty::Ty<'tcx>) -> Lvalue<'tcx> {
383 let span = self.span;
385 self.local_decls.push(temp_decl(mutability, ty, span))
392 rcvr_field: Lvalue<'tcx>,
398 let substs = Substs::for_item(
401 |_, _| tcx.types.re_erased,
405 // `func == Clone::clone(&ty) -> ty`
406 let func = Operand::Constant(box Constant {
408 ty: tcx.mk_fn_def(self.def_id, substs),
409 literal: Literal::Value {
410 value: ConstVal::Function(self.def_id, substs),
414 let ref_loc = self.make_lvalue(
416 tcx.mk_ref(tcx.types.re_erased, ty::TypeAndMut {
418 mutbl: hir::Mutability::MutImmutable,
422 let loc = self.make_lvalue(Mutability::Not, ty);
424 // `let ref_loc: &ty = &rcvr_field;`
425 let statement = self.make_statement(
426 StatementKind::Assign(
428 Rvalue::Ref(tcx.types.re_erased, BorrowKind::Shared, rcvr_field)
432 // `let loc = Clone::clone(ref_loc);`
433 self.block(vec![statement], TerminatorKind::Call {
435 args: vec![Operand::Consume(ref_loc)],
436 destination: Some((loc.clone(), next)),
437 cleanup: Some(cleanup),
447 loop_body: BasicBlock,
448 loop_end: BasicBlock,
453 let cond = self.make_lvalue(Mutability::Mut, tcx.types.bool);
454 let compute_cond = self.make_statement(
455 StatementKind::Assign(
457 Rvalue::BinaryOp(BinOp::Ne, Operand::Consume(end), Operand::Consume(beg))
461 // `if end != beg { goto loop_body; } else { goto loop_end; }`
464 TerminatorKind::if_(tcx, Operand::Consume(cond), loop_body, loop_end),
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();
473 ty: self.tcx.types.usize,
474 literal: Literal::Value {
475 value: ConstVal::Integral(ConstInt::Usize(value))
480 fn array_shim(&mut self, ty: ty::Ty<'tcx>, len: usize) {
482 let rcvr = Lvalue::Local(Local::new(1+0)).deref();
484 let beg = self.make_lvalue(Mutability::Mut, tcx.types.usize);
485 let end = self.make_lvalue(Mutability::Not, tcx.types.usize);
486 let ret = self.make_lvalue(Mutability::Mut, tcx.mk_array(ty, len));
489 // `let mut beg = 0;`
494 StatementKind::Assign(
496 Rvalue::Use(Operand::Constant(self.make_usize(0)))
500 StatementKind::Assign(
502 Rvalue::Use(Operand::Constant(self.make_usize(len)))
506 self.block(inits, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
513 self.loop_header(beg.clone(), end, BasicBlock::new(2), BasicBlock::new(4), false);
516 // `let cloned = Clone::clone(rcvr[beg])`;
517 // Goto #3 if ok, #5 if unwinding happens.
518 let rcvr_field = rcvr.clone().index(Operand::Consume(beg.clone()));
519 let cloned = self.make_clone_call(ty, rcvr_field, BasicBlock::new(3), BasicBlock::new(5));
522 // `ret[beg] = cloned;`
525 let ret_field = ret.clone().index(Operand::Consume(beg.clone()));
526 let statements = vec![
528 StatementKind::Assign(
530 Rvalue::Use(Operand::Consume(cloned))
534 StatementKind::Assign(
538 Operand::Consume(beg.clone()),
539 Operand::Constant(self.make_usize(1))
544 self.block(statements, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
548 let ret_statement = self.make_statement(
549 StatementKind::Assign(
550 Lvalue::Local(RETURN_POINTER),
551 Rvalue::Use(Operand::Consume(ret.clone())),
554 self.block(vec![ret_statement], TerminatorKind::Return, false);
558 // `let mut beg = 0;`
561 let beg = self.make_lvalue(Mutability::Mut, tcx.types.usize);
562 let init = self.make_statement(
563 StatementKind::Assign(
565 Rvalue::Use(Operand::Constant(self.make_usize(0)))
568 self.block(vec![init], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
570 // BB #6 (cleanup): loop {
575 self.loop_header(beg.clone(), end, BasicBlock::new(7), BasicBlock::new(9), true);
579 self.block(vec![], TerminatorKind::Drop {
580 location: ret.index(Operand::Consume(beg.clone())),
581 target: BasicBlock::new(8),
588 let statement = self.make_statement(
589 StatementKind::Assign(
593 Operand::Consume(beg.clone()),
594 Operand::Constant(self.make_usize(1))
598 self.block(vec![statement], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
601 self.block(vec![], TerminatorKind::Resume, true);
604 fn tuple_shim(&mut self, tys: &ty::Slice<ty::Ty<'tcx>>) {
605 let rcvr = Lvalue::Local(Local::new(1+0)).deref();
607 let mut returns = Vec::new();
608 for (i, ity) in tys.iter().enumerate() {
609 let rcvr_field = rcvr.clone().field(Field::new(i), *ity);
612 // `returns[i] = Clone::clone(&rcvr.i);`
613 // Goto #(2i + 2) if ok, #(2i + 1) if unwinding happens.
615 self.make_clone_call(
618 BasicBlock::new(2 * i + 2),
619 BasicBlock::new(2 * i + 1),
623 // BB #(2i + 1) (cleanup)
625 // Nothing to drop, just resume.
626 self.block(vec![], TerminatorKind::Resume, true);
628 // Drop previous field and goto previous cleanup block.
629 self.block(vec![], TerminatorKind::Drop {
630 location: returns[i - 1].clone(),
631 target: BasicBlock::new(2 * i - 1),
637 // `return (returns[0], returns[1], ..., returns[tys.len() - 1]);`
638 let ret_statement = self.make_statement(
639 StatementKind::Assign(
640 Lvalue::Local(RETURN_POINTER),
642 box AggregateKind::Tuple,
643 returns.into_iter().map(Operand::Consume).collect()
647 self.block(vec![ret_statement], TerminatorKind::Return, false);
651 /// Build a "call" shim for `def_id`. The shim calls the
652 /// function specified by `call_kind`, first adjusting its first
653 /// argument according to `rcvr_adjustment`.
655 /// If `untuple_args` is a vec of types, the second argument of the
656 /// function will be untupled as these types.
657 fn build_call_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
659 rcvr_adjustment: Adjustment,
661 untuple_args: Option<&[Ty<'tcx>]>)
664 debug!("build_call_shim(def_id={:?}, rcvr_adjustment={:?}, \
665 call_kind={:?}, untuple_args={:?})",
666 def_id, rcvr_adjustment, call_kind, untuple_args);
668 let sig = tcx.fn_sig(def_id);
669 let sig = tcx.erase_late_bound_regions(&sig);
670 let span = tcx.def_span(def_id);
672 debug!("build_call_shim: sig={:?}", sig);
674 let mut local_decls = local_decls_for_sig(&sig, span);
675 let source_info = SourceInfo { span, scope: ARGUMENT_VISIBILITY_SCOPE };
677 let rcvr_arg = Local::new(1+0);
678 let rcvr_l = Lvalue::Local(rcvr_arg);
679 let mut statements = vec![];
681 let rcvr = match rcvr_adjustment {
682 Adjustment::Identity => Operand::Consume(rcvr_l),
683 Adjustment::Deref => Operand::Consume(rcvr_l.deref()),
684 Adjustment::RefMut => {
685 // let rcvr = &mut rcvr;
686 let ref_rcvr = local_decls.push(temp_decl(
688 tcx.mk_ref(tcx.types.re_erased, ty::TypeAndMut {
690 mutbl: hir::Mutability::MutMutable
694 statements.push(Statement {
696 kind: StatementKind::Assign(
697 Lvalue::Local(ref_rcvr),
698 Rvalue::Ref(tcx.types.re_erased, BorrowKind::Mut, rcvr_l)
701 Operand::Consume(Lvalue::Local(ref_rcvr))
705 let (callee, mut args) = match call_kind {
706 CallKind::Indirect => (rcvr, vec![]),
707 CallKind::Direct(def_id) => (
708 Operand::Constant(box Constant {
710 ty: tcx.type_of(def_id),
711 literal: Literal::Value {
712 value: ConstVal::Function(def_id,
713 Substs::identity_for_item(tcx, def_id)),
720 if let Some(untuple_args) = untuple_args {
721 args.extend(untuple_args.iter().enumerate().map(|(i, ity)| {
722 let arg_lv = Lvalue::Local(Local::new(1+1));
723 Operand::Consume(arg_lv.field(Field::new(i), *ity))
726 args.extend((1..sig.inputs().len()).map(|i| {
727 Operand::Consume(Lvalue::Local(Local::new(1+i)))
731 let mut blocks = IndexVec::new();
732 let block = |blocks: &mut IndexVec<_, _>, statements, kind, is_cleanup| {
733 blocks.push(BasicBlockData {
735 terminator: Some(Terminator { source_info, kind }),
741 block(&mut blocks, statements, TerminatorKind::Call {
744 destination: Some((Lvalue::Local(RETURN_POINTER),
745 BasicBlock::new(1))),
746 cleanup: if let Adjustment::RefMut = rcvr_adjustment {
747 Some(BasicBlock::new(3))
753 if let Adjustment::RefMut = rcvr_adjustment {
754 // BB #1 - drop for Self
755 block(&mut blocks, vec![], TerminatorKind::Drop {
756 location: Lvalue::Local(rcvr_arg),
757 target: BasicBlock::new(2),
762 block(&mut blocks, vec![], TerminatorKind::Return, false);
763 if let Adjustment::RefMut = rcvr_adjustment {
764 // BB #3 - drop if closure panics
765 block(&mut blocks, vec![], TerminatorKind::Drop {
766 location: Lvalue::Local(rcvr_arg),
767 target: BasicBlock::new(4),
772 block(&mut blocks, vec![], TerminatorKind::Resume, true);
775 let mut mir = Mir::new(
777 IndexVec::from_elem_n(
778 VisibilityScopeData { span: span, parent_scope: None }, 1
788 if let Abi::RustCall = sig.abi {
789 mir.spread_arg = Some(Local::new(sig.inputs().len()));
794 pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>,
795 ctor_id: ast::NodeId,
796 fields: &[hir::StructField],
798 -> (Mir<'tcx>, MirSource)
801 let def_id = tcx.hir.local_def_id(ctor_id);
802 let sig = tcx.no_late_bound_regions(&tcx.fn_sig(def_id))
803 .expect("LBR in ADT constructor signature");
804 let sig = tcx.erase_regions(&sig);
806 let (adt_def, substs) = match sig.output().sty {
807 ty::TyAdt(adt_def, substs) => (adt_def, substs),
808 _ => bug!("unexpected type for ADT ctor {:?}", sig.output())
811 debug!("build_ctor: def_id={:?} sig={:?} fields={:?}", def_id, sig, fields);
813 let local_decls = local_decls_for_sig(&sig, span);
815 let source_info = SourceInfo {
817 scope: ARGUMENT_VISIBILITY_SCOPE
820 let variant_no = if adt_def.is_enum() {
821 adt_def.variant_index_with_id(def_id)
826 // return = ADT(arg0, arg1, ...); return
827 let start_block = BasicBlockData {
828 statements: vec![Statement {
830 kind: StatementKind::Assign(
831 Lvalue::Local(RETURN_POINTER),
833 box AggregateKind::Adt(adt_def, variant_no, substs, None),
834 (1..sig.inputs().len()+1).map(|i| {
835 Operand::Consume(Lvalue::Local(Local::new(i)))
840 terminator: Some(Terminator {
842 kind: TerminatorKind::Return,
848 IndexVec::from_elem_n(start_block, 1),
849 IndexVec::from_elem_n(
850 VisibilityScopeData { span: span, parent_scope: None }, 1
860 (mir, MirSource::Fn(ctor_id))