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 span = self.span;
483 let rcvr = Lvalue::Local(Local::new(1+0)).deref();
485 let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
486 let end = self.make_lvalue(Mutability::Not, tcx.types.usize);
487 let ret = self.make_lvalue(Mutability::Mut, tcx.mk_array(ty, len));
490 // `let mut beg = 0;`
495 StatementKind::Assign(
497 Rvalue::Use(Operand::Constant(self.make_usize(0)))
501 StatementKind::Assign(
503 Rvalue::Use(Operand::Constant(self.make_usize(len)))
507 self.block(inits, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
514 self.loop_header(Lvalue::Local(beg), end, BasicBlock::new(2), BasicBlock::new(4), false);
517 // `let cloned = Clone::clone(rcvr[beg])`;
518 // Goto #3 if ok, #5 if unwinding happens.
519 let rcvr_field = rcvr.clone().index(beg);
520 let cloned = self.make_clone_call(ty, rcvr_field, BasicBlock::new(3), BasicBlock::new(5));
523 // `ret[beg] = cloned;`
526 let ret_field = ret.clone().index(beg);
527 let statements = vec![
529 StatementKind::Assign(
531 Rvalue::Use(Operand::Consume(cloned))
535 StatementKind::Assign(
539 Operand::Consume(Lvalue::Local(beg)),
540 Operand::Constant(self.make_usize(1))
545 self.block(statements, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
549 let ret_statement = self.make_statement(
550 StatementKind::Assign(
551 Lvalue::Local(RETURN_POINTER),
552 Rvalue::Use(Operand::Consume(ret.clone())),
555 self.block(vec![ret_statement], TerminatorKind::Return, false);
559 // `let mut beg = 0;`
562 let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
563 let init = self.make_statement(
564 StatementKind::Assign(
566 Rvalue::Use(Operand::Constant(self.make_usize(0)))
569 self.block(vec![init], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
571 // BB #6 (cleanup): loop {
576 self.loop_header(Lvalue::Local(beg), Lvalue::Local(end),
577 BasicBlock::new(7), BasicBlock::new(9), true);
581 self.block(vec![], TerminatorKind::Drop {
582 location: ret.index(beg),
583 target: BasicBlock::new(8),
590 let statement = self.make_statement(
591 StatementKind::Assign(
595 Operand::Consume(Lvalue::Local(beg)),
596 Operand::Constant(self.make_usize(1))
600 self.block(vec![statement], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
603 self.block(vec![], TerminatorKind::Resume, true);
606 fn tuple_shim(&mut self, tys: &ty::Slice<ty::Ty<'tcx>>) {
607 let rcvr = Lvalue::Local(Local::new(1+0)).deref();
609 let mut returns = Vec::new();
610 for (i, ity) in tys.iter().enumerate() {
611 let rcvr_field = rcvr.clone().field(Field::new(i), *ity);
614 // `returns[i] = Clone::clone(&rcvr.i);`
615 // Goto #(2i + 2) if ok, #(2i + 1) if unwinding happens.
617 self.make_clone_call(
620 BasicBlock::new(2 * i + 2),
621 BasicBlock::new(2 * i + 1),
625 // BB #(2i + 1) (cleanup)
627 // Nothing to drop, just resume.
628 self.block(vec![], TerminatorKind::Resume, true);
630 // Drop previous field and goto previous cleanup block.
631 self.block(vec![], TerminatorKind::Drop {
632 location: returns[i - 1].clone(),
633 target: BasicBlock::new(2 * i - 1),
639 // `return (returns[0], returns[1], ..., returns[tys.len() - 1]);`
640 let ret_statement = self.make_statement(
641 StatementKind::Assign(
642 Lvalue::Local(RETURN_POINTER),
644 box AggregateKind::Tuple,
645 returns.into_iter().map(Operand::Consume).collect()
649 self.block(vec![ret_statement], TerminatorKind::Return, false);
653 /// Build a "call" shim for `def_id`. The shim calls the
654 /// function specified by `call_kind`, first adjusting its first
655 /// argument according to `rcvr_adjustment`.
657 /// If `untuple_args` is a vec of types, the second argument of the
658 /// function will be untupled as these types.
659 fn build_call_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
661 rcvr_adjustment: Adjustment,
663 untuple_args: Option<&[Ty<'tcx>]>)
666 debug!("build_call_shim(def_id={:?}, rcvr_adjustment={:?}, \
667 call_kind={:?}, untuple_args={:?})",
668 def_id, rcvr_adjustment, call_kind, untuple_args);
670 let sig = tcx.fn_sig(def_id);
671 let sig = tcx.erase_late_bound_regions(&sig);
672 let span = tcx.def_span(def_id);
674 debug!("build_call_shim: sig={:?}", sig);
676 let mut local_decls = local_decls_for_sig(&sig, span);
677 let source_info = SourceInfo { span, scope: ARGUMENT_VISIBILITY_SCOPE };
679 let rcvr_arg = Local::new(1+0);
680 let rcvr_l = Lvalue::Local(rcvr_arg);
681 let mut statements = vec![];
683 let rcvr = match rcvr_adjustment {
684 Adjustment::Identity => Operand::Consume(rcvr_l),
685 Adjustment::Deref => Operand::Consume(rcvr_l.deref()),
686 Adjustment::RefMut => {
687 // let rcvr = &mut rcvr;
688 let ref_rcvr = local_decls.push(temp_decl(
690 tcx.mk_ref(tcx.types.re_erased, ty::TypeAndMut {
692 mutbl: hir::Mutability::MutMutable
696 statements.push(Statement {
698 kind: StatementKind::Assign(
699 Lvalue::Local(ref_rcvr),
700 Rvalue::Ref(tcx.types.re_erased, BorrowKind::Mut, rcvr_l)
703 Operand::Consume(Lvalue::Local(ref_rcvr))
707 let (callee, mut args) = match call_kind {
708 CallKind::Indirect => (rcvr, vec![]),
709 CallKind::Direct(def_id) => (
710 Operand::Constant(box Constant {
712 ty: tcx.type_of(def_id),
713 literal: Literal::Value {
714 value: ConstVal::Function(def_id,
715 Substs::identity_for_item(tcx, def_id)),
722 if let Some(untuple_args) = untuple_args {
723 args.extend(untuple_args.iter().enumerate().map(|(i, ity)| {
724 let arg_lv = Lvalue::Local(Local::new(1+1));
725 Operand::Consume(arg_lv.field(Field::new(i), *ity))
728 args.extend((1..sig.inputs().len()).map(|i| {
729 Operand::Consume(Lvalue::Local(Local::new(1+i)))
733 let mut blocks = IndexVec::new();
734 let block = |blocks: &mut IndexVec<_, _>, statements, kind, is_cleanup| {
735 blocks.push(BasicBlockData {
737 terminator: Some(Terminator { source_info, kind }),
743 block(&mut blocks, statements, TerminatorKind::Call {
746 destination: Some((Lvalue::Local(RETURN_POINTER),
747 BasicBlock::new(1))),
748 cleanup: if let Adjustment::RefMut = rcvr_adjustment {
749 Some(BasicBlock::new(3))
755 if let Adjustment::RefMut = rcvr_adjustment {
756 // BB #1 - drop for Self
757 block(&mut blocks, vec![], TerminatorKind::Drop {
758 location: Lvalue::Local(rcvr_arg),
759 target: BasicBlock::new(2),
764 block(&mut blocks, vec![], TerminatorKind::Return, false);
765 if let Adjustment::RefMut = rcvr_adjustment {
766 // BB #3 - drop if closure panics
767 block(&mut blocks, vec![], TerminatorKind::Drop {
768 location: Lvalue::Local(rcvr_arg),
769 target: BasicBlock::new(4),
774 block(&mut blocks, vec![], TerminatorKind::Resume, true);
777 let mut mir = Mir::new(
779 IndexVec::from_elem_n(
780 VisibilityScopeData { span: span, parent_scope: None }, 1
790 if let Abi::RustCall = sig.abi {
791 mir.spread_arg = Some(Local::new(sig.inputs().len()));
796 pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>,
797 ctor_id: ast::NodeId,
798 fields: &[hir::StructField],
800 -> (Mir<'tcx>, MirSource)
803 let def_id = tcx.hir.local_def_id(ctor_id);
804 let sig = tcx.no_late_bound_regions(&tcx.fn_sig(def_id))
805 .expect("LBR in ADT constructor signature");
806 let sig = tcx.erase_regions(&sig);
808 let (adt_def, substs) = match sig.output().sty {
809 ty::TyAdt(adt_def, substs) => (adt_def, substs),
810 _ => bug!("unexpected type for ADT ctor {:?}", sig.output())
813 debug!("build_ctor: def_id={:?} sig={:?} fields={:?}", def_id, sig, fields);
815 let local_decls = local_decls_for_sig(&sig, span);
817 let source_info = SourceInfo {
819 scope: ARGUMENT_VISIBILITY_SCOPE
822 let variant_no = if adt_def.is_enum() {
823 adt_def.variant_index_with_id(def_id)
828 // return = ADT(arg0, arg1, ...); return
829 let start_block = BasicBlockData {
830 statements: vec![Statement {
832 kind: StatementKind::Assign(
833 Lvalue::Local(RETURN_POINTER),
835 box AggregateKind::Adt(adt_def, variant_no, substs, None),
836 (1..sig.inputs().len()+1).map(|i| {
837 Operand::Consume(Lvalue::Local(Local::new(i)))
842 terminator: Some(Terminator {
844 kind: TerminatorKind::Return,
850 IndexVec::from_elem_n(start_block, 1),
851 IndexVec::from_elem_n(
852 VisibilityScopeData { span: span, parent_scope: None }, 1
862 (mir, MirSource::Fn(ctor_id))