2017-02-07 15:46:21 -06:00
|
|
|
use rustc::hir;
|
2017-03-08 10:33:21 -06:00
|
|
|
use rustc::hir::def_id::DefId;
|
2017-02-07 15:46:21 -06:00
|
|
|
use rustc::mir::*;
|
2019-02-19 19:19:13 -06:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
2018-11-01 13:03:38 -05:00
|
|
|
use rustc::ty::layout::VariantIdx;
|
2019-02-25 19:30:34 -06:00
|
|
|
use rustc::ty::subst::{Subst, InternalSubsts};
|
2018-06-13 08:44:43 -05:00
|
|
|
use rustc::ty::query::Providers;
|
2017-02-07 15:46:21 -06:00
|
|
|
|
|
|
|
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
|
|
|
|
|
2018-04-25 11:30:39 -05:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2019-05-14 15:32:44 -05:00
|
|
|
use syntax_pos::{Span, sym};
|
2017-02-07 15:46:21 -06:00
|
|
|
|
2017-03-13 18:08:21 -05:00
|
|
|
use std::fmt;
|
2017-02-07 15:46:21 -06:00
|
|
|
use std::iter;
|
|
|
|
|
2018-11-22 10:17:45 -06:00
|
|
|
use crate::transform::{
|
|
|
|
add_moves_for_packed_drops, add_call_guards,
|
|
|
|
remove_noop_landing_pads, no_landing_pads, simplify, run_passes
|
|
|
|
};
|
2019-02-07 15:28:15 -06:00
|
|
|
use crate::util::elaborate_drops::{self, DropElaborator, DropStyle, DropFlagMode};
|
|
|
|
use crate::util::patch::MirPatch;
|
2019-05-26 03:55:50 -05:00
|
|
|
use crate::util::expand_aggregate;
|
2017-03-09 12:36:01 -06:00
|
|
|
|
2019-02-07 15:28:15 -06:00
|
|
|
pub fn provide(providers: &mut Providers<'_>) {
|
2017-02-08 11:31:03 -06:00
|
|
|
providers.mir_shims = make_shim;
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:13:36 -05:00
|
|
|
fn make_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-02-08 11:31:03 -06:00
|
|
|
instance: ty::InstanceDef<'tcx>)
|
2019-05-17 16:55:04 -05:00
|
|
|
-> &'tcx Body<'tcx>
|
2017-02-08 11:31:03 -06:00
|
|
|
{
|
2017-03-06 04:58:51 -06:00
|
|
|
debug!("make_shim({:?})", instance);
|
2017-03-08 10:33:21 -06:00
|
|
|
|
2017-03-09 12:36:01 -06:00
|
|
|
let mut result = match instance {
|
2017-02-08 11:31:03 -06:00
|
|
|
ty::InstanceDef::Item(..) =>
|
|
|
|
bug!("item {:?} passed to make_shim", instance),
|
2018-09-11 09:31:48 -05:00
|
|
|
ty::InstanceDef::VtableShim(def_id) => {
|
|
|
|
build_call_shim(
|
|
|
|
tcx,
|
|
|
|
def_id,
|
|
|
|
Adjustment::DerefMove,
|
|
|
|
CallKind::Direct(def_id),
|
|
|
|
None,
|
|
|
|
)
|
2018-09-10 08:54:48 -05:00
|
|
|
}
|
2017-03-08 10:33:21 -06:00
|
|
|
ty::InstanceDef::FnPtrShim(def_id, ty) => {
|
|
|
|
let trait_ = tcx.trait_of_item(def_id).unwrap();
|
2017-08-31 10:57:41 -05:00
|
|
|
let adjustment = match tcx.lang_items().fn_trait_kind(trait_) {
|
2017-03-08 10:33:21 -06:00
|
|
|
Some(ty::ClosureKind::FnOnce) => Adjustment::Identity,
|
|
|
|
Some(ty::ClosureKind::FnMut) |
|
|
|
|
Some(ty::ClosureKind::Fn) => Adjustment::Deref,
|
|
|
|
None => bug!("fn pointer {:?} is not an fn", ty)
|
|
|
|
};
|
|
|
|
// HACK: we need the "real" argument types for the MIR,
|
|
|
|
// but because our substs are (Self, Args), where Args
|
|
|
|
// is a tuple, we must include the *concrete* argument
|
|
|
|
// types in the MIR. They will be substituted again with
|
|
|
|
// the param-substs, but because they are concrete, this
|
|
|
|
// will not do any harm.
|
2017-05-13 09:11:52 -05:00
|
|
|
let sig = tcx.erase_late_bound_regions(&ty.fn_sig(tcx));
|
2017-03-08 10:33:21 -06:00
|
|
|
let arg_tys = sig.inputs();
|
|
|
|
|
|
|
|
build_call_shim(
|
|
|
|
tcx,
|
|
|
|
def_id,
|
|
|
|
adjustment,
|
|
|
|
CallKind::Indirect,
|
|
|
|
Some(arg_tys)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ty::InstanceDef::Virtual(def_id, _) => {
|
2018-05-08 08:10:16 -05:00
|
|
|
// We are generating a call back to our def-id, which the
|
|
|
|
// codegen backend knows to turn to an actual virtual call.
|
2017-03-08 10:33:21 -06:00
|
|
|
build_call_shim(
|
|
|
|
tcx,
|
|
|
|
def_id,
|
|
|
|
Adjustment::Identity,
|
|
|
|
CallKind::Direct(def_id),
|
|
|
|
None
|
|
|
|
)
|
2017-03-06 04:58:51 -06:00
|
|
|
}
|
2017-03-08 15:19:09 -06:00
|
|
|
ty::InstanceDef::ClosureOnceShim { call_once } => {
|
2017-08-31 10:57:41 -05:00
|
|
|
let fn_mut = tcx.lang_items().fn_mut_trait().unwrap();
|
2017-03-08 15:19:09 -06:00
|
|
|
let call_mut = tcx.global_tcx()
|
|
|
|
.associated_items(fn_mut)
|
2019-05-19 03:26:08 -05:00
|
|
|
.find(|it| it.kind == ty::AssocKind::Method)
|
2017-03-08 15:19:09 -06:00
|
|
|
.unwrap().def_id;
|
|
|
|
|
|
|
|
build_call_shim(
|
|
|
|
tcx,
|
|
|
|
call_once,
|
|
|
|
Adjustment::RefMut,
|
|
|
|
CallKind::Direct(call_mut),
|
|
|
|
None
|
|
|
|
)
|
|
|
|
}
|
2017-03-13 18:08:21 -05:00
|
|
|
ty::InstanceDef::DropGlue(def_id, ty) => {
|
2017-05-10 09:28:06 -05:00
|
|
|
build_drop_shim(tcx, def_id, ty)
|
2017-03-13 18:08:21 -05:00
|
|
|
}
|
2017-08-07 09:21:08 -05:00
|
|
|
ty::InstanceDef::CloneShim(def_id, ty) => {
|
2017-09-01 11:24:02 -05:00
|
|
|
let name = tcx.item_name(def_id);
|
2019-05-14 15:32:44 -05:00
|
|
|
if name == sym::clone {
|
2017-08-04 07:44:12 -05:00
|
|
|
build_clone_shim(tcx, def_id, ty)
|
2019-05-14 15:32:44 -05:00
|
|
|
} else if name == sym::clone_from {
|
2017-08-04 07:44:12 -05:00
|
|
|
debug!("make_shim({:?}: using default trait implementation", instance);
|
|
|
|
return tcx.optimized_mir(def_id);
|
|
|
|
} else {
|
2017-08-07 09:21:08 -05:00
|
|
|
bug!("builtin clone shim {:?} not supported", instance)
|
2017-08-04 07:44:12 -05:00
|
|
|
}
|
|
|
|
}
|
2017-03-08 15:19:09 -06:00
|
|
|
ty::InstanceDef::Intrinsic(_) => {
|
|
|
|
bug!("creating shims from intrinsics ({:?}) is unsupported", instance)
|
|
|
|
}
|
2017-03-06 04:58:51 -06:00
|
|
|
};
|
2017-08-04 07:44:12 -05:00
|
|
|
debug!("make_shim({:?}) = untransformed {:?}", instance, result);
|
2018-11-22 10:17:45 -06:00
|
|
|
|
2019-02-03 04:51:07 -06:00
|
|
|
run_passes(tcx, &mut result, instance, MirPhase::Const, &[
|
2018-11-22 10:17:45 -06:00
|
|
|
&add_moves_for_packed_drops::AddMovesForPackedDrops,
|
|
|
|
&no_landing_pads::NoLandingPads,
|
|
|
|
&remove_noop_landing_pads::RemoveNoopLandingPads,
|
|
|
|
&simplify::SimplifyCfg::new("make_shim"),
|
|
|
|
&add_call_guards::CriticalCallEdges,
|
|
|
|
]);
|
|
|
|
|
2017-03-06 04:58:51 -06:00
|
|
|
debug!("make_shim({:?}) = {:?}", instance, result);
|
|
|
|
|
2019-05-31 02:25:34 -05:00
|
|
|
tcx.arena.alloc(result)
|
2017-02-08 11:31:03 -06:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:33:21 -06:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
enum Adjustment {
|
|
|
|
Identity,
|
|
|
|
Deref,
|
2018-09-11 09:31:48 -05:00
|
|
|
DerefMove,
|
2017-03-08 15:19:09 -06:00
|
|
|
RefMut,
|
2017-03-08 10:33:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
enum CallKind {
|
|
|
|
Indirect,
|
|
|
|
Direct(DefId),
|
|
|
|
}
|
|
|
|
|
2019-02-07 15:28:15 -06:00
|
|
|
fn temp_decl(mutability: Mutability, ty: Ty<'_>, span: Span) -> LocalDecl<'_> {
|
2018-05-29 04:24:53 -05:00
|
|
|
let source_info = SourceInfo { scope: OUTERMOST_SOURCE_SCOPE, span };
|
2017-04-11 15:52:51 -05:00
|
|
|
LocalDecl {
|
2018-10-03 08:14:11 -05:00
|
|
|
mutability,
|
|
|
|
ty,
|
2018-10-22 07:23:44 -05:00
|
|
|
user_ty: UserTypeProjections::none(),
|
2018-09-10 09:54:31 -05:00
|
|
|
name: None,
|
2018-05-29 13:31:33 -05:00
|
|
|
source_info,
|
2018-05-29 05:55:21 -05:00
|
|
|
visibility_scope: source_info.scope,
|
2017-07-15 15:41:33 -05:00
|
|
|
internal: false,
|
2018-06-07 08:25:08 -05:00
|
|
|
is_user_variable: None,
|
2018-09-21 17:51:48 -05:00
|
|
|
is_block_tail: None,
|
2017-04-11 15:52:51 -05:00
|
|
|
}
|
2017-03-08 10:33:21 -06:00
|
|
|
}
|
|
|
|
|
2017-04-11 15:52:51 -05:00
|
|
|
fn local_decls_for_sig<'tcx>(sig: &ty::FnSig<'tcx>, span: Span)
|
2017-02-07 15:46:21 -06:00
|
|
|
-> IndexVec<Local, LocalDecl<'tcx>>
|
|
|
|
{
|
2017-04-11 15:52:51 -05:00
|
|
|
iter::once(temp_decl(Mutability::Mut, sig.output(), span))
|
2017-03-08 10:33:21 -06:00
|
|
|
.chain(sig.inputs().iter().map(
|
2017-04-11 15:52:51 -05:00
|
|
|
|ity| temp_decl(Mutability::Not, ity, span)))
|
2017-03-08 10:33:21 -06:00
|
|
|
.collect()
|
2017-02-07 15:46:21 -06:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:13:36 -05:00
|
|
|
fn build_drop_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-03-13 18:08:21 -05:00
|
|
|
def_id: DefId,
|
|
|
|
ty: Option<Ty<'tcx>>)
|
2019-05-17 16:55:04 -05:00
|
|
|
-> Body<'tcx>
|
2017-03-13 18:08:21 -05:00
|
|
|
{
|
|
|
|
debug!("build_drop_shim(def_id={:?}, ty={:?})", def_id, ty);
|
|
|
|
|
2016-12-26 07:34:03 -06:00
|
|
|
// Check if this is a generator, if so, return the drop glue for it
|
2018-08-21 19:35:02 -05:00
|
|
|
if let Some(&ty::TyS { sty: ty::Generator(gen_def_id, substs, _), .. }) = ty {
|
2019-06-03 17:26:48 -05:00
|
|
|
let body = &**tcx.optimized_mir(gen_def_id).generator_drop.as_ref().unwrap();
|
|
|
|
return body.subst(tcx, substs.substs);
|
2016-12-26 07:34:03 -06:00
|
|
|
}
|
|
|
|
|
2017-03-13 18:08:21 -05:00
|
|
|
let substs = if let Some(ty) = ty {
|
2018-05-16 02:43:24 -05:00
|
|
|
tcx.intern_substs(&[ty.into()])
|
2017-03-13 18:08:21 -05:00
|
|
|
} else {
|
2019-02-25 19:30:34 -06:00
|
|
|
InternalSubsts::identity_for_item(tcx, def_id)
|
2017-03-13 18:08:21 -05:00
|
|
|
};
|
2017-05-13 09:11:52 -05:00
|
|
|
let sig = tcx.fn_sig(def_id).subst(tcx, substs);
|
|
|
|
let sig = tcx.erase_late_bound_regions(&sig);
|
2017-03-13 18:08:21 -05:00
|
|
|
let span = tcx.def_span(def_id);
|
|
|
|
|
2018-05-28 06:16:09 -05:00
|
|
|
let source_info = SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE };
|
2017-03-13 18:08:21 -05:00
|
|
|
|
|
|
|
let return_block = BasicBlock::new(1);
|
2018-11-07 08:38:06 -06:00
|
|
|
let mut blocks = IndexVec::with_capacity(2);
|
2017-03-13 18:08:21 -05:00
|
|
|
let block = |blocks: &mut IndexVec<_, _>, kind| {
|
|
|
|
blocks.push(BasicBlockData {
|
|
|
|
statements: vec![],
|
|
|
|
terminator: Some(Terminator { source_info, kind }),
|
|
|
|
is_cleanup: false
|
|
|
|
})
|
|
|
|
};
|
|
|
|
block(&mut blocks, TerminatorKind::Goto { target: return_block });
|
|
|
|
block(&mut blocks, TerminatorKind::Return);
|
|
|
|
|
2019-06-03 17:26:48 -05:00
|
|
|
let mut body = Body::new(
|
2017-03-13 18:08:21 -05:00
|
|
|
blocks,
|
|
|
|
IndexVec::from_elem_n(
|
2018-05-28 06:16:09 -05:00
|
|
|
SourceScopeData { span: span, parent_scope: None }, 1
|
2017-03-13 18:08:21 -05:00
|
|
|
),
|
2017-11-22 06:47:50 -06:00
|
|
|
ClearCrossCrate::Clear,
|
2017-03-13 18:08:21 -05:00
|
|
|
IndexVec::new(),
|
2016-12-26 07:34:03 -06:00
|
|
|
None,
|
2017-04-11 15:52:51 -05:00
|
|
|
local_decls_for_sig(&sig, span),
|
2018-11-16 15:56:18 -06:00
|
|
|
IndexVec::new(),
|
2017-03-13 18:08:21 -05:00
|
|
|
sig.inputs().len(),
|
|
|
|
vec![],
|
2018-11-24 07:38:31 -06:00
|
|
|
span,
|
2018-11-26 10:30:19 -06:00
|
|
|
vec![],
|
2017-03-13 18:08:21 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(..) = ty {
|
2018-11-07 08:36:36 -06:00
|
|
|
// The first argument (index 0), but add 1 for the return value.
|
2019-02-21 22:24:03 -06:00
|
|
|
let dropee_ptr = Place::Base(PlaceBase::Local(Local::new(1+0)));
|
2018-11-07 05:21:28 -06:00
|
|
|
if tcx.sess.opts.debugging_opts.mir_emit_retag {
|
2018-12-11 12:54:38 -06:00
|
|
|
// Function arguments should be retagged, and we make this one raw.
|
2019-06-03 17:26:48 -05:00
|
|
|
body.basic_blocks_mut()[START_BLOCK].statements.insert(0, Statement {
|
2018-11-22 06:43:05 -06:00
|
|
|
source_info,
|
2018-12-11 12:54:38 -06:00
|
|
|
kind: StatementKind::Retag(RetagKind::Raw, dropee_ptr.clone()),
|
2018-11-22 06:43:05 -06:00
|
|
|
});
|
2018-11-07 05:21:28 -06:00
|
|
|
}
|
2017-03-13 18:08:21 -05:00
|
|
|
let patch = {
|
2018-04-26 12:31:31 -05:00
|
|
|
let param_env = tcx.param_env(def_id).with_reveal_all();
|
2017-03-13 18:08:21 -05:00
|
|
|
let mut elaborator = DropShimElaborator {
|
2019-06-03 17:26:48 -05:00
|
|
|
body: &body,
|
|
|
|
patch: MirPatch::new(&body),
|
2017-05-10 09:28:06 -05:00
|
|
|
tcx,
|
|
|
|
param_env
|
2017-03-13 18:08:21 -05:00
|
|
|
};
|
2018-11-07 05:21:28 -06:00
|
|
|
let dropee = dropee_ptr.deref();
|
2017-03-13 18:08:21 -05:00
|
|
|
let resume_block = elaborator.patch.resume_block();
|
|
|
|
elaborate_drops::elaborate_drop(
|
|
|
|
&mut elaborator,
|
|
|
|
source_info,
|
|
|
|
&dropee,
|
|
|
|
(),
|
|
|
|
return_block,
|
2017-05-12 07:00:36 -05:00
|
|
|
elaborate_drops::Unwind::To(resume_block),
|
2017-03-13 18:08:21 -05:00
|
|
|
START_BLOCK
|
|
|
|
);
|
|
|
|
elaborator.patch
|
|
|
|
};
|
2019-06-03 17:26:48 -05:00
|
|
|
patch.apply(&mut body);
|
2017-03-13 18:08:21 -05:00
|
|
|
}
|
|
|
|
|
2019-06-03 17:26:48 -05:00
|
|
|
body
|
2017-03-13 18:08:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DropShimElaborator<'a, 'tcx: 'a> {
|
2019-06-03 17:26:48 -05:00
|
|
|
pub body: &'a Body<'tcx>,
|
2016-12-26 07:34:03 -06:00
|
|
|
pub patch: MirPatch<'tcx>,
|
2017-09-14 20:13:36 -05:00
|
|
|
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-12-26 07:34:03 -06:00
|
|
|
pub param_env: ty::ParamEnv<'tcx>,
|
2017-03-13 18:08:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> fmt::Debug for DropShimElaborator<'a, 'tcx> {
|
2019-02-07 15:28:15 -06:00
|
|
|
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
2017-03-13 18:08:21 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> DropElaborator<'a, 'tcx> for DropShimElaborator<'a, 'tcx> {
|
|
|
|
type Path = ();
|
|
|
|
|
|
|
|
fn patch(&mut self) -> &mut MirPatch<'tcx> { &mut self.patch }
|
2019-06-03 17:26:48 -05:00
|
|
|
fn body(&self) -> &'a Body<'tcx> { self.body }
|
2017-09-14 20:13:36 -05:00
|
|
|
fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> { self.tcx }
|
2017-05-15 16:57:30 -05:00
|
|
|
fn param_env(&self) -> ty::ParamEnv<'tcx> { self.param_env }
|
2017-03-13 18:08:21 -05:00
|
|
|
|
|
|
|
fn drop_style(&self, _path: Self::Path, mode: DropFlagMode) -> DropStyle {
|
|
|
|
if let DropFlagMode::Shallow = mode {
|
|
|
|
DropStyle::Static
|
|
|
|
} else {
|
|
|
|
DropStyle::Open
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_drop_flag(&mut self, _path: Self::Path) -> Option<Operand<'tcx>> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_drop_flag(&mut self, _location: Location, _path: Self::Path, _mode: DropFlagMode) {
|
|
|
|
}
|
|
|
|
|
|
|
|
fn field_subpath(&self, _path: Self::Path, _field: Field) -> Option<Self::Path> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
fn deref_subpath(&self, _path: Self::Path) -> Option<Self::Path> {
|
|
|
|
None
|
|
|
|
}
|
2018-11-01 13:03:38 -05:00
|
|
|
fn downcast_subpath(&self, _path: Self::Path, _variant: VariantIdx) -> Option<Self::Path> {
|
2017-03-13 18:08:21 -05:00
|
|
|
Some(())
|
|
|
|
}
|
2017-11-28 06:48:23 -06:00
|
|
|
fn array_subpath(&self, _path: Self::Path, _index: u32, _size: u32) -> Option<Self::Path> {
|
|
|
|
None
|
|
|
|
}
|
2017-03-13 18:08:21 -05:00
|
|
|
}
|
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Builds a `Clone::clone` shim for `self_ty`. Here, `def_id` is `Clone::clone`.
|
2017-09-14 20:13:36 -05:00
|
|
|
fn build_clone_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-08-04 07:44:12 -05:00
|
|
|
def_id: DefId,
|
2017-09-14 20:44:23 -05:00
|
|
|
self_ty: Ty<'tcx>)
|
2019-05-17 16:55:04 -05:00
|
|
|
-> Body<'tcx>
|
2017-08-04 07:44:12 -05:00
|
|
|
{
|
|
|
|
debug!("build_clone_shim(def_id={:?})", def_id);
|
|
|
|
|
2018-01-30 03:17:25 -06:00
|
|
|
let mut builder = CloneShimBuilder::new(tcx, def_id, self_ty);
|
2018-11-20 10:59:06 -06:00
|
|
|
let is_copy = self_ty.is_copy_modulo_regions(tcx, tcx.param_env(def_id), builder.span);
|
2017-08-09 06:55:27 -05:00
|
|
|
|
2019-02-21 22:24:03 -06:00
|
|
|
let dest = Place::RETURN_PLACE;
|
|
|
|
let src = Place::Base(PlaceBase::Local(Local::new(1+0))).deref();
|
2018-01-30 06:06:19 -06:00
|
|
|
|
2017-08-09 06:55:27 -05:00
|
|
|
match self_ty.sty {
|
|
|
|
_ if is_copy => builder.copy_shim(),
|
2018-08-21 19:35:02 -05:00
|
|
|
ty::Array(ty, len) => {
|
2018-04-26 02:18:19 -05:00
|
|
|
let len = len.unwrap_usize(tcx);
|
2018-01-30 06:06:19 -06:00
|
|
|
builder.array_shim(dest, src, ty, len)
|
2017-08-05 08:11:24 -05:00
|
|
|
}
|
2018-08-21 19:35:02 -05:00
|
|
|
ty::Closure(def_id, substs) => {
|
2017-09-13 15:40:48 -05:00
|
|
|
builder.tuple_like_shim(
|
2018-01-30 06:06:19 -06:00
|
|
|
dest, src,
|
|
|
|
substs.upvar_tys(def_id, tcx)
|
2017-09-13 15:40:48 -05:00
|
|
|
)
|
|
|
|
}
|
2019-04-25 18:27:33 -05:00
|
|
|
ty::Tuple(tys) => builder.tuple_like_shim(dest, src, tys.iter().map(|k| k.expect_ty())),
|
2017-08-09 06:55:27 -05:00
|
|
|
_ => {
|
2017-09-13 15:40:48 -05:00
|
|
|
bug!("clone shim for `{:?}` which is not `Copy` and is not an aggregate", self_ty)
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
|
|
|
};
|
2017-08-04 07:44:12 -05:00
|
|
|
|
2017-08-09 06:55:27 -05:00
|
|
|
builder.into_mir()
|
|
|
|
}
|
2017-08-08 10:13:12 -05:00
|
|
|
|
2017-08-09 06:55:27 -05:00
|
|
|
struct CloneShimBuilder<'a, 'tcx: 'a> {
|
2017-09-14 20:13:36 -05:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-08-09 06:55:27 -05:00
|
|
|
def_id: DefId,
|
|
|
|
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
|
|
|
|
blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
|
|
|
|
span: Span,
|
|
|
|
sig: ty::FnSig<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
|
2018-01-30 03:17:25 -06:00
|
|
|
fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
self_ty: Ty<'tcx>) -> Self {
|
|
|
|
// we must subst the self_ty because it's
|
|
|
|
// otherwise going to be TySelf and we can't index
|
|
|
|
// or access fields of a Place of type TySelf.
|
|
|
|
let substs = tcx.mk_substs_trait(self_ty, &[]);
|
|
|
|
let sig = tcx.fn_sig(def_id).subst(tcx, substs);
|
2017-08-09 06:55:27 -05:00
|
|
|
let sig = tcx.erase_late_bound_regions(&sig);
|
|
|
|
let span = tcx.def_span(def_id);
|
|
|
|
|
|
|
|
CloneShimBuilder {
|
|
|
|
tcx,
|
|
|
|
def_id,
|
|
|
|
local_decls: local_decls_for_sig(&sig, span),
|
|
|
|
blocks: IndexVec::new(),
|
|
|
|
span,
|
|
|
|
sig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 16:55:04 -05:00
|
|
|
fn into_mir(self) -> Body<'tcx> {
|
|
|
|
Body::new(
|
2017-08-09 06:55:27 -05:00
|
|
|
self.blocks,
|
|
|
|
IndexVec::from_elem_n(
|
2018-05-28 06:16:09 -05:00
|
|
|
SourceScopeData { span: self.span, parent_scope: None }, 1
|
2017-08-09 06:55:27 -05:00
|
|
|
),
|
2017-11-22 06:47:50 -06:00
|
|
|
ClearCrossCrate::Clear,
|
2017-08-09 06:55:27 -05:00
|
|
|
IndexVec::new(),
|
2017-08-22 00:09:50 -05:00
|
|
|
None,
|
2017-08-09 06:55:27 -05:00
|
|
|
self.local_decls,
|
2018-11-16 15:56:18 -06:00
|
|
|
IndexVec::new(),
|
2017-08-09 06:55:27 -05:00
|
|
|
self.sig.inputs().len(),
|
|
|
|
vec![],
|
2018-11-24 07:38:31 -06:00
|
|
|
self.span,
|
2018-11-26 10:30:19 -06:00
|
|
|
vec![],
|
2017-08-09 06:55:27 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn source_info(&self) -> SourceInfo {
|
2018-05-28 06:16:09 -05:00
|
|
|
SourceInfo { span: self.span, scope: OUTERMOST_SOURCE_SCOPE }
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn block(
|
|
|
|
&mut self,
|
|
|
|
statements: Vec<Statement<'tcx>>,
|
|
|
|
kind: TerminatorKind<'tcx>,
|
|
|
|
is_cleanup: bool
|
|
|
|
) -> BasicBlock {
|
|
|
|
let source_info = self.source_info();
|
|
|
|
self.blocks.push(BasicBlockData {
|
2017-08-08 10:13:12 -05:00
|
|
|
statements,
|
2017-08-04 07:44:12 -05:00
|
|
|
terminator: Some(Terminator { source_info, kind }),
|
2017-08-08 10:13:12 -05:00
|
|
|
is_cleanup,
|
2017-08-04 07:44:12 -05:00
|
|
|
})
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
|
|
|
|
2018-01-30 06:06:19 -06:00
|
|
|
/// Gives the index of an upcoming BasicBlock, with an offset.
|
|
|
|
/// offset=0 will give you the index of the next BasicBlock,
|
|
|
|
/// offset=1 will give the index of the next-to-next block,
|
|
|
|
/// offset=-1 will give you the index of the last-created block
|
|
|
|
fn block_index_offset(&mut self, offset: usize) -> BasicBlock {
|
|
|
|
BasicBlock::new(self.blocks.len() + offset)
|
|
|
|
}
|
|
|
|
|
2017-08-09 06:55:27 -05:00
|
|
|
fn make_statement(&self, kind: StatementKind<'tcx>) -> Statement<'tcx> {
|
|
|
|
Statement {
|
|
|
|
source_info: self.source_info(),
|
|
|
|
kind,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn copy_shim(&mut self) {
|
2019-02-21 22:24:03 -06:00
|
|
|
let rcvr = Place::Base(PlaceBase::Local(Local::new(1+0))).deref();
|
2017-08-09 06:55:27 -05:00
|
|
|
let ret_statement = self.make_statement(
|
|
|
|
StatementKind::Assign(
|
2019-02-21 22:24:03 -06:00
|
|
|
Place::RETURN_PLACE,
|
2018-09-23 20:32:31 -05:00
|
|
|
box Rvalue::Use(Operand::Copy(rcvr))
|
2017-08-09 06:55:27 -05:00
|
|
|
)
|
|
|
|
);
|
|
|
|
self.block(vec![ret_statement], TerminatorKind::Return, false);
|
|
|
|
}
|
2017-08-04 07:44:12 -05:00
|
|
|
|
2017-12-01 06:39:51 -06:00
|
|
|
fn make_place(&mut self, mutability: Mutability, ty: Ty<'tcx>) -> Place<'tcx> {
|
2017-08-09 06:55:27 -05:00
|
|
|
let span = self.span;
|
2019-02-21 22:24:03 -06:00
|
|
|
Place::Base(PlaceBase::Local(
|
2017-08-09 06:55:27 -05:00
|
|
|
self.local_decls.push(temp_decl(mutability, ty, span))
|
2019-02-21 22:24:03 -06:00
|
|
|
))
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_clone_call(
|
|
|
|
&mut self,
|
2018-01-30 05:54:56 -06:00
|
|
|
dest: Place<'tcx>,
|
|
|
|
src: Place<'tcx>,
|
2017-09-14 20:44:23 -05:00
|
|
|
ty: Ty<'tcx>,
|
2017-08-09 06:55:27 -05:00
|
|
|
next: BasicBlock,
|
2018-01-30 05:54:56 -06:00
|
|
|
cleanup: BasicBlock
|
2018-01-29 23:08:54 -06:00
|
|
|
) {
|
2017-08-09 06:55:27 -05:00
|
|
|
let tcx = self.tcx;
|
|
|
|
|
2019-02-19 19:19:13 -06:00
|
|
|
let substs = tcx.mk_substs_trait(ty, &[]);
|
2017-08-04 07:44:12 -05:00
|
|
|
|
|
|
|
// `func == Clone::clone(&ty) -> ty`
|
2017-08-04 03:25:13 -05:00
|
|
|
let func_ty = tcx.mk_fn_def(self.def_id, substs);
|
2017-08-04 07:44:12 -05:00
|
|
|
let func = Operand::Constant(box Constant {
|
2017-08-09 06:55:27 -05:00
|
|
|
span: self.span,
|
2017-08-04 03:25:13 -05:00
|
|
|
ty: func_ty,
|
2018-08-09 05:18:00 -05:00
|
|
|
user_ty: None,
|
2019-04-03 08:29:31 -05:00
|
|
|
literal: ty::Const::zero_sized(tcx, func_ty),
|
2017-08-04 07:44:12 -05:00
|
|
|
});
|
|
|
|
|
2017-12-01 06:39:51 -06:00
|
|
|
let ref_loc = self.make_place(
|
2017-08-08 10:13:12 -05:00
|
|
|
Mutability::Not,
|
2019-04-25 16:05:04 -05:00
|
|
|
tcx.mk_ref(tcx.lifetimes.re_erased, ty::TypeAndMut {
|
2017-08-04 07:44:12 -05:00
|
|
|
ty,
|
2017-08-08 10:13:12 -05:00
|
|
|
mutbl: hir::Mutability::MutImmutable,
|
2017-08-09 06:55:27 -05:00
|
|
|
})
|
2017-08-04 07:44:12 -05:00
|
|
|
);
|
|
|
|
|
2018-01-30 05:54:56 -06:00
|
|
|
// `let ref_loc: &ty = &src;`
|
2017-08-09 06:55:27 -05:00
|
|
|
let statement = self.make_statement(
|
|
|
|
StatementKind::Assign(
|
2017-08-04 07:44:12 -05:00
|
|
|
ref_loc.clone(),
|
2019-04-25 16:05:04 -05:00
|
|
|
box Rvalue::Ref(tcx.lifetimes.re_erased, BorrowKind::Shared, src)
|
2017-08-04 07:44:12 -05:00
|
|
|
)
|
2017-08-09 06:55:27 -05:00
|
|
|
);
|
2017-08-04 07:44:12 -05:00
|
|
|
|
|
|
|
// `let loc = Clone::clone(ref_loc);`
|
2017-08-09 06:55:27 -05:00
|
|
|
self.block(vec![statement], TerminatorKind::Call {
|
2017-08-04 07:44:12 -05:00
|
|
|
func,
|
2017-11-17 09:19:57 -06:00
|
|
|
args: vec![Operand::Move(ref_loc)],
|
2018-01-29 23:21:48 -06:00
|
|
|
destination: Some((dest, next)),
|
2017-08-08 10:13:12 -05:00
|
|
|
cleanup: Some(cleanup),
|
2018-09-29 04:34:12 -05:00
|
|
|
from_hir_call: true,
|
2017-08-08 10:13:12 -05:00
|
|
|
}, false);
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
2017-08-04 07:44:12 -05:00
|
|
|
|
2017-08-09 06:55:27 -05:00
|
|
|
fn loop_header(
|
|
|
|
&mut self,
|
2017-12-01 06:31:47 -06:00
|
|
|
beg: Place<'tcx>,
|
|
|
|
end: Place<'tcx>,
|
2017-08-09 06:55:27 -05:00
|
|
|
loop_body: BasicBlock,
|
|
|
|
loop_end: BasicBlock,
|
|
|
|
is_cleanup: bool
|
|
|
|
) {
|
|
|
|
let tcx = self.tcx;
|
|
|
|
|
2017-12-01 06:39:51 -06:00
|
|
|
let cond = self.make_place(Mutability::Mut, tcx.types.bool);
|
2017-08-09 06:55:27 -05:00
|
|
|
let compute_cond = self.make_statement(
|
|
|
|
StatementKind::Assign(
|
|
|
|
cond.clone(),
|
2018-09-23 20:32:31 -05:00
|
|
|
box Rvalue::BinaryOp(BinOp::Ne, Operand::Copy(end), Operand::Copy(beg))
|
2017-08-09 06:55:27 -05:00
|
|
|
)
|
|
|
|
);
|
2017-08-08 10:13:12 -05:00
|
|
|
|
2017-08-09 06:55:27 -05:00
|
|
|
// `if end != beg { goto loop_body; } else { goto loop_end; }`
|
|
|
|
self.block(
|
|
|
|
vec![compute_cond],
|
2017-11-17 09:19:57 -06:00
|
|
|
TerminatorKind::if_(tcx, Operand::Move(cond), loop_body, loop_end),
|
2017-08-09 06:55:27 -05:00
|
|
|
is_cleanup
|
|
|
|
);
|
|
|
|
}
|
2017-08-08 10:13:12 -05:00
|
|
|
|
2017-08-05 04:27:28 -05:00
|
|
|
fn make_usize(&self, value: u64) -> Box<Constant<'tcx>> {
|
2017-08-09 06:55:27 -05:00
|
|
|
box Constant {
|
|
|
|
span: self.span,
|
|
|
|
ty: self.tcx.types.usize,
|
2018-08-09 05:18:00 -05:00
|
|
|
user_ty: None,
|
2019-04-03 08:29:31 -05:00
|
|
|
literal: ty::Const::from_usize(self.tcx, value),
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
|
|
|
}
|
2017-08-08 10:13:12 -05:00
|
|
|
|
2018-01-30 06:06:19 -06:00
|
|
|
fn array_shim(&mut self, dest: Place<'tcx>, src: Place<'tcx>, ty: Ty<'tcx>, len: u64) {
|
2017-08-09 06:55:27 -05:00
|
|
|
let tcx = self.tcx;
|
2017-09-03 13:55:41 -05:00
|
|
|
let span = self.span;
|
2017-08-09 06:55:27 -05:00
|
|
|
|
2017-09-03 13:55:41 -05:00
|
|
|
let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
|
2017-12-01 06:39:51 -06:00
|
|
|
let end = self.make_place(Mutability::Not, tcx.types.usize);
|
2017-08-09 06:55:27 -05:00
|
|
|
|
|
|
|
// BB #0
|
|
|
|
// `let mut beg = 0;`
|
|
|
|
// `let end = len;`
|
|
|
|
// `goto #1;`
|
|
|
|
let inits = vec![
|
|
|
|
self.make_statement(
|
|
|
|
StatementKind::Assign(
|
2019-02-21 22:24:03 -06:00
|
|
|
Place::Base(PlaceBase::Local(beg)),
|
2018-09-23 20:32:31 -05:00
|
|
|
box Rvalue::Use(Operand::Constant(self.make_usize(0)))
|
2017-08-08 10:13:12 -05:00
|
|
|
)
|
2017-08-09 06:55:27 -05:00
|
|
|
),
|
|
|
|
self.make_statement(
|
|
|
|
StatementKind::Assign(
|
|
|
|
end.clone(),
|
2018-09-23 20:32:31 -05:00
|
|
|
box Rvalue::Use(Operand::Constant(self.make_usize(len)))
|
2017-08-09 06:55:27 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
];
|
|
|
|
self.block(inits, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
|
|
|
|
|
|
|
|
// BB #1: loop {
|
|
|
|
// BB #2;
|
|
|
|
// BB #3;
|
|
|
|
// }
|
|
|
|
// BB #4;
|
2019-02-21 22:24:03 -06:00
|
|
|
self.loop_header(Place::Base(PlaceBase::Local(beg)),
|
|
|
|
end,
|
|
|
|
BasicBlock::new(2),
|
|
|
|
BasicBlock::new(4),
|
|
|
|
false);
|
2017-08-09 06:55:27 -05:00
|
|
|
|
|
|
|
// BB #2
|
2018-01-30 06:00:15 -06:00
|
|
|
// `dest[i] = Clone::clone(src[beg])`;
|
2017-08-09 06:55:27 -05:00
|
|
|
// Goto #3 if ok, #5 if unwinding happens.
|
2018-01-30 06:00:15 -06:00
|
|
|
let dest_field = dest.clone().index(beg);
|
2018-10-25 13:11:11 -05:00
|
|
|
let src_field = src.index(beg);
|
2018-01-30 06:00:15 -06:00
|
|
|
self.make_clone_call(dest_field, src_field, ty, BasicBlock::new(3),
|
2018-01-30 05:54:56 -06:00
|
|
|
BasicBlock::new(5));
|
2017-08-09 06:55:27 -05:00
|
|
|
|
|
|
|
// BB #3
|
|
|
|
// `beg = beg + 1;`
|
|
|
|
// `goto #1`;
|
|
|
|
let statements = vec![
|
|
|
|
self.make_statement(
|
|
|
|
StatementKind::Assign(
|
2019-02-21 22:24:03 -06:00
|
|
|
Place::Base(PlaceBase::Local(beg)),
|
2018-09-23 20:32:31 -05:00
|
|
|
box Rvalue::BinaryOp(
|
2017-08-08 10:13:12 -05:00
|
|
|
BinOp::Add,
|
2019-02-21 22:24:03 -06:00
|
|
|
Operand::Copy(Place::Base(PlaceBase::Local(beg))),
|
2017-08-09 06:55:27 -05:00
|
|
|
Operand::Constant(self.make_usize(1))
|
2017-08-04 07:44:12 -05:00
|
|
|
)
|
|
|
|
)
|
2017-08-09 06:55:27 -05:00
|
|
|
)
|
|
|
|
];
|
|
|
|
self.block(statements, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
|
|
|
|
|
|
|
|
// BB #4
|
2018-01-30 06:00:15 -06:00
|
|
|
// `return dest;`
|
|
|
|
self.block(vec![], TerminatorKind::Return, false);
|
2017-08-09 06:55:27 -05:00
|
|
|
|
|
|
|
// BB #5 (cleanup)
|
|
|
|
// `let end = beg;`
|
|
|
|
// `let mut beg = 0;`
|
|
|
|
// goto #6;
|
|
|
|
let end = beg;
|
2017-09-03 13:55:41 -05:00
|
|
|
let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
|
2017-08-09 06:55:27 -05:00
|
|
|
let init = self.make_statement(
|
|
|
|
StatementKind::Assign(
|
2019-02-21 22:24:03 -06:00
|
|
|
Place::Base(PlaceBase::Local(beg)),
|
2018-09-23 20:32:31 -05:00
|
|
|
box Rvalue::Use(Operand::Constant(self.make_usize(0)))
|
2017-08-09 06:55:27 -05:00
|
|
|
)
|
|
|
|
);
|
|
|
|
self.block(vec![init], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
|
|
|
|
|
|
|
|
// BB #6 (cleanup): loop {
|
|
|
|
// BB #7;
|
|
|
|
// BB #8;
|
|
|
|
// }
|
|
|
|
// BB #9;
|
2019-02-21 22:24:03 -06:00
|
|
|
self.loop_header(Place::Base(PlaceBase::Local(beg)), Place::Base(PlaceBase::Local(end)),
|
2017-09-03 13:55:41 -05:00
|
|
|
BasicBlock::new(7), BasicBlock::new(9), true);
|
2017-08-09 06:55:27 -05:00
|
|
|
|
|
|
|
// BB #7 (cleanup)
|
2018-01-30 06:00:15 -06:00
|
|
|
// `drop(dest[beg])`;
|
2017-08-09 06:55:27 -05:00
|
|
|
self.block(vec![], TerminatorKind::Drop {
|
2018-01-30 06:00:15 -06:00
|
|
|
location: dest.index(beg),
|
2017-08-09 06:55:27 -05:00
|
|
|
target: BasicBlock::new(8),
|
|
|
|
unwind: None,
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
// BB #8 (cleanup)
|
|
|
|
// `beg = beg + 1;`
|
|
|
|
// `goto #6;`
|
|
|
|
let statement = self.make_statement(
|
|
|
|
StatementKind::Assign(
|
2019-02-21 22:24:03 -06:00
|
|
|
Place::Base(PlaceBase::Local(beg)),
|
2018-09-23 20:32:31 -05:00
|
|
|
box Rvalue::BinaryOp(
|
2017-08-09 06:55:27 -05:00
|
|
|
BinOp::Add,
|
2019-02-21 22:24:03 -06:00
|
|
|
Operand::Copy(Place::Base(PlaceBase::Local(beg))),
|
2017-08-09 06:55:27 -05:00
|
|
|
Operand::Constant(self.make_usize(1))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
self.block(vec![statement], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
|
2017-08-08 10:13:12 -05:00
|
|
|
|
2017-08-09 06:55:27 -05:00
|
|
|
// BB #9 (resume)
|
|
|
|
self.block(vec![], TerminatorKind::Resume, true);
|
|
|
|
}
|
|
|
|
|
2018-01-30 06:06:19 -06:00
|
|
|
fn tuple_like_shim<I>(&mut self, dest: Place<'tcx>,
|
|
|
|
src: Place<'tcx>, tys: I)
|
2019-04-26 07:26:49 -05:00
|
|
|
where I: Iterator<Item = Ty<'tcx>> {
|
2018-01-30 06:06:19 -06:00
|
|
|
let mut previous_field = None;
|
|
|
|
for (i, ity) in tys.enumerate() {
|
2018-01-29 23:21:48 -06:00
|
|
|
let field = Field::new(i);
|
2018-01-30 06:06:19 -06:00
|
|
|
let src_field = src.clone().field(field, ity);
|
|
|
|
|
|
|
|
let dest_field = dest.clone().field(field, ity);
|
2017-08-09 06:55:27 -05:00
|
|
|
|
2018-01-30 06:06:19 -06:00
|
|
|
// #(2i + 1) is the cleanup block for the previous clone operation
|
|
|
|
let cleanup_block = self.block_index_offset(1);
|
|
|
|
// #(2i + 2) is the next cloning block
|
|
|
|
// (or the Return terminator if this is the last block)
|
|
|
|
let next_block = self.block_index_offset(2);
|
2018-01-29 23:08:54 -06:00
|
|
|
|
2017-08-09 06:55:27 -05:00
|
|
|
// BB #(2i)
|
2018-01-30 06:06:19 -06:00
|
|
|
// `dest.i = Clone::clone(&src.i);`
|
2017-08-09 06:55:27 -05:00
|
|
|
// Goto #(2i + 2) if ok, #(2i + 1) if unwinding happens.
|
2018-01-29 23:08:54 -06:00
|
|
|
self.make_clone_call(
|
2018-01-30 06:06:19 -06:00
|
|
|
dest_field.clone(),
|
|
|
|
src_field,
|
|
|
|
ity,
|
|
|
|
next_block,
|
|
|
|
cleanup_block,
|
2017-08-09 06:55:27 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// BB #(2i + 1) (cleanup)
|
2018-01-30 06:06:19 -06:00
|
|
|
if let Some((previous_field, previous_cleanup)) = previous_field.take() {
|
2017-08-09 06:55:27 -05:00
|
|
|
// Drop previous field and goto previous cleanup block.
|
|
|
|
self.block(vec![], TerminatorKind::Drop {
|
2018-01-30 06:06:19 -06:00
|
|
|
location: previous_field,
|
|
|
|
target: previous_cleanup,
|
2017-08-09 06:55:27 -05:00
|
|
|
unwind: None,
|
|
|
|
}, true);
|
2018-01-29 23:21:48 -06:00
|
|
|
} else {
|
|
|
|
// Nothing to drop, just resume.
|
|
|
|
self.block(vec![], TerminatorKind::Resume, true);
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
2018-01-29 23:21:48 -06:00
|
|
|
|
2018-01-30 06:06:19 -06:00
|
|
|
previous_field = Some((dest_field, cleanup_block));
|
2017-08-04 07:44:12 -05:00
|
|
|
}
|
|
|
|
|
2018-01-29 23:21:48 -06:00
|
|
|
self.block(vec![], TerminatorKind::Return, false);
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
2017-08-04 07:44:12 -05:00
|
|
|
}
|
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Builds a "call" shim for `def_id`. The shim calls the
|
2017-03-08 10:33:21 -06:00
|
|
|
/// function specified by `call_kind`, first adjusting its first
|
|
|
|
/// argument according to `rcvr_adjustment`.
|
|
|
|
///
|
|
|
|
/// If `untuple_args` is a vec of types, the second argument of the
|
|
|
|
/// function will be untupled as these types.
|
2017-09-14 20:13:36 -05:00
|
|
|
fn build_call_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-03-08 10:33:21 -06:00
|
|
|
def_id: DefId,
|
|
|
|
rcvr_adjustment: Adjustment,
|
|
|
|
call_kind: CallKind,
|
|
|
|
untuple_args: Option<&[Ty<'tcx>]>)
|
2019-05-17 16:55:04 -05:00
|
|
|
-> Body<'tcx>
|
2017-03-06 04:58:51 -06:00
|
|
|
{
|
2017-03-08 10:33:21 -06:00
|
|
|
debug!("build_call_shim(def_id={:?}, rcvr_adjustment={:?}, \
|
|
|
|
call_kind={:?}, untuple_args={:?})",
|
|
|
|
def_id, rcvr_adjustment, call_kind, untuple_args);
|
2017-03-06 04:58:51 -06:00
|
|
|
|
2017-05-13 09:11:52 -05:00
|
|
|
let sig = tcx.fn_sig(def_id);
|
|
|
|
let sig = tcx.erase_late_bound_regions(&sig);
|
2017-03-08 10:33:21 -06:00
|
|
|
let span = tcx.def_span(def_id);
|
2017-03-06 04:58:51 -06:00
|
|
|
|
2017-03-08 10:33:21 -06:00
|
|
|
debug!("build_call_shim: sig={:?}", sig);
|
2017-03-06 04:58:51 -06:00
|
|
|
|
2017-04-11 15:52:51 -05:00
|
|
|
let mut local_decls = local_decls_for_sig(&sig, span);
|
2018-05-28 06:16:09 -05:00
|
|
|
let source_info = SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE };
|
2017-03-06 04:58:51 -06:00
|
|
|
|
2017-03-08 15:19:09 -06:00
|
|
|
let rcvr_arg = Local::new(1+0);
|
2019-02-21 22:24:03 -06:00
|
|
|
let rcvr_l = Place::Base(PlaceBase::Local(rcvr_arg));
|
2017-03-08 15:19:09 -06:00
|
|
|
let mut statements = vec![];
|
2017-03-06 04:58:51 -06:00
|
|
|
|
2017-03-08 10:33:21 -06:00
|
|
|
let rcvr = match rcvr_adjustment {
|
2017-11-17 09:19:57 -06:00
|
|
|
Adjustment::Identity => Operand::Move(rcvr_l),
|
|
|
|
Adjustment::Deref => Operand::Copy(rcvr_l.deref()),
|
2018-09-11 09:31:48 -05:00
|
|
|
Adjustment::DerefMove => {
|
|
|
|
// fn(Self, ...) -> fn(*mut Self, ...)
|
|
|
|
let arg_ty = local_decls[rcvr_arg].ty;
|
|
|
|
assert!(arg_ty.is_self());
|
|
|
|
local_decls[rcvr_arg].ty = tcx.mk_mut_ptr(arg_ty);
|
|
|
|
|
2018-09-26 09:03:09 -05:00
|
|
|
Operand::Move(rcvr_l.deref())
|
2018-09-11 09:31:48 -05:00
|
|
|
}
|
2017-03-08 15:19:09 -06:00
|
|
|
Adjustment::RefMut => {
|
|
|
|
// let rcvr = &mut rcvr;
|
|
|
|
let ref_rcvr = local_decls.push(temp_decl(
|
|
|
|
Mutability::Not,
|
2019-04-25 16:05:04 -05:00
|
|
|
tcx.mk_ref(tcx.lifetimes.re_erased, ty::TypeAndMut {
|
2017-03-08 15:19:09 -06:00
|
|
|
ty: sig.inputs()[0],
|
|
|
|
mutbl: hir::Mutability::MutMutable
|
2017-04-11 15:52:51 -05:00
|
|
|
}),
|
|
|
|
span
|
2017-03-08 15:19:09 -06:00
|
|
|
));
|
2018-01-15 05:47:26 -06:00
|
|
|
let borrow_kind = BorrowKind::Mut {
|
|
|
|
allow_two_phase_borrow: false,
|
|
|
|
};
|
2017-03-08 15:19:09 -06:00
|
|
|
statements.push(Statement {
|
2017-08-07 00:54:09 -05:00
|
|
|
source_info,
|
2017-03-08 15:19:09 -06:00
|
|
|
kind: StatementKind::Assign(
|
2019-02-21 22:24:03 -06:00
|
|
|
Place::Base(PlaceBase::Local(ref_rcvr)),
|
2019-04-25 16:05:04 -05:00
|
|
|
box Rvalue::Ref(tcx.lifetimes.re_erased, borrow_kind, rcvr_l)
|
2017-03-08 15:19:09 -06:00
|
|
|
)
|
|
|
|
});
|
2019-02-21 22:24:03 -06:00
|
|
|
Operand::Move(Place::Base(PlaceBase::Local(ref_rcvr)))
|
2017-03-08 15:19:09 -06:00
|
|
|
}
|
2017-03-08 10:33:21 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let (callee, mut args) = match call_kind {
|
|
|
|
CallKind::Indirect => (rcvr, vec![]),
|
2017-08-04 03:25:13 -05:00
|
|
|
CallKind::Direct(def_id) => {
|
|
|
|
let ty = tcx.type_of(def_id);
|
|
|
|
(Operand::Constant(box Constant {
|
2017-08-07 00:54:09 -05:00
|
|
|
span,
|
2017-08-04 03:25:13 -05:00
|
|
|
ty,
|
2018-08-09 05:18:00 -05:00
|
|
|
user_ty: None,
|
2019-04-03 08:29:31 -05:00
|
|
|
literal: ty::Const::zero_sized(tcx, ty),
|
2017-08-04 03:25:13 -05:00
|
|
|
}),
|
|
|
|
vec![rcvr])
|
|
|
|
}
|
2017-03-08 10:33:21 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(untuple_args) = untuple_args {
|
|
|
|
args.extend(untuple_args.iter().enumerate().map(|(i, ity)| {
|
2019-02-21 22:24:03 -06:00
|
|
|
let arg_place = Place::Base(PlaceBase::Local(Local::new(1+1)));
|
2017-12-01 06:39:51 -06:00
|
|
|
Operand::Move(arg_place.field(Field::new(i), *ity))
|
2017-03-08 10:33:21 -06:00
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
args.extend((1..sig.inputs().len()).map(|i| {
|
2019-02-21 22:24:03 -06:00
|
|
|
Operand::Move(Place::Base(PlaceBase::Local(Local::new(1+i))))
|
2017-03-08 10:33:21 -06:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2018-11-07 08:38:06 -06:00
|
|
|
let n_blocks = if let Adjustment::RefMut = rcvr_adjustment { 5 } else { 2 };
|
|
|
|
let mut blocks = IndexVec::with_capacity(n_blocks);
|
2017-03-08 15:19:09 -06:00
|
|
|
let block = |blocks: &mut IndexVec<_, _>, statements, kind, is_cleanup| {
|
|
|
|
blocks.push(BasicBlockData {
|
|
|
|
statements,
|
|
|
|
terminator: Some(Terminator { source_info, kind }),
|
|
|
|
is_cleanup
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
// BB #0
|
|
|
|
block(&mut blocks, statements, TerminatorKind::Call {
|
|
|
|
func: callee,
|
2017-08-07 00:54:09 -05:00
|
|
|
args,
|
2019-02-21 22:24:03 -06:00
|
|
|
destination: Some((Place::RETURN_PLACE,
|
2017-03-08 15:19:09 -06:00
|
|
|
BasicBlock::new(1))),
|
2017-03-09 12:36:01 -06:00
|
|
|
cleanup: if let Adjustment::RefMut = rcvr_adjustment {
|
2017-03-08 15:19:09 -06:00
|
|
|
Some(BasicBlock::new(3))
|
|
|
|
} else {
|
|
|
|
None
|
2018-09-29 04:34:12 -05:00
|
|
|
},
|
|
|
|
from_hir_call: true,
|
2017-03-08 15:19:09 -06:00
|
|
|
}, false);
|
|
|
|
|
|
|
|
if let Adjustment::RefMut = rcvr_adjustment {
|
|
|
|
// BB #1 - drop for Self
|
|
|
|
block(&mut blocks, vec![], TerminatorKind::Drop {
|
2019-02-21 22:24:03 -06:00
|
|
|
location: Place::Base(PlaceBase::Local(rcvr_arg)),
|
2017-03-08 15:19:09 -06:00
|
|
|
target: BasicBlock::new(2),
|
2017-03-09 12:36:01 -06:00
|
|
|
unwind: None
|
2017-03-08 15:19:09 -06:00
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
// BB #1/#2 - return
|
|
|
|
block(&mut blocks, vec![], TerminatorKind::Return, false);
|
2017-03-09 12:36:01 -06:00
|
|
|
if let Adjustment::RefMut = rcvr_adjustment {
|
2017-03-08 15:19:09 -06:00
|
|
|
// BB #3 - drop if closure panics
|
|
|
|
block(&mut blocks, vec![], TerminatorKind::Drop {
|
2019-02-21 22:24:03 -06:00
|
|
|
location: Place::Base(PlaceBase::Local(rcvr_arg)),
|
2017-03-08 15:19:09 -06:00
|
|
|
target: BasicBlock::new(4),
|
|
|
|
unwind: None
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
// BB #4 - resume
|
|
|
|
block(&mut blocks, vec![], TerminatorKind::Resume, true);
|
|
|
|
}
|
2017-03-06 04:58:51 -06:00
|
|
|
|
2019-06-03 17:26:48 -05:00
|
|
|
let mut body = Body::new(
|
2017-03-08 10:33:21 -06:00
|
|
|
blocks,
|
2017-03-06 04:58:51 -06:00
|
|
|
IndexVec::from_elem_n(
|
2018-05-28 06:16:09 -05:00
|
|
|
SourceScopeData { span: span, parent_scope: None }, 1
|
2017-03-06 04:58:51 -06:00
|
|
|
),
|
2017-11-22 06:47:50 -06:00
|
|
|
ClearCrossCrate::Clear,
|
2017-03-06 04:58:51 -06:00
|
|
|
IndexVec::new(),
|
2016-12-26 07:34:03 -06:00
|
|
|
None,
|
2017-03-06 04:58:51 -06:00
|
|
|
local_decls,
|
2018-11-16 15:56:18 -06:00
|
|
|
IndexVec::new(),
|
2017-03-06 04:58:51 -06:00
|
|
|
sig.inputs().len(),
|
|
|
|
vec![],
|
2018-11-24 07:38:31 -06:00
|
|
|
span,
|
2018-11-26 10:30:19 -06:00
|
|
|
vec![],
|
2017-03-06 04:58:51 -06:00
|
|
|
);
|
2017-03-08 10:33:21 -06:00
|
|
|
if let Abi::RustCall = sig.abi {
|
2019-06-03 17:26:48 -05:00
|
|
|
body.spread_arg = Some(Local::new(sig.inputs().len()));
|
2017-03-08 10:33:21 -06:00
|
|
|
}
|
2019-06-03 17:26:48 -05:00
|
|
|
body
|
2017-03-06 04:58:51 -06:00
|
|
|
}
|
|
|
|
|
2019-05-26 03:55:50 -05:00
|
|
|
pub fn build_adt_ctor<'gcx>(tcx: TyCtxt<'_, 'gcx, 'gcx>, ctor_id: DefId) -> &'gcx Body<'gcx> {
|
|
|
|
debug_assert!(tcx.is_constructor(ctor_id));
|
|
|
|
|
|
|
|
let span = tcx.hir().span_if_local(ctor_id)
|
|
|
|
.unwrap_or_else(|| bug!("no span for ctor {:?}", ctor_id));
|
|
|
|
|
|
|
|
let param_env = tcx.param_env(ctor_id);
|
2017-11-20 12:08:52 -06:00
|
|
|
|
2018-03-03 07:23:28 -06:00
|
|
|
// Normalize the sig.
|
2019-05-26 03:55:50 -05:00
|
|
|
let sig = tcx.fn_sig(ctor_id)
|
2018-10-24 15:30:34 -05:00
|
|
|
.no_bound_vars()
|
|
|
|
.expect("LBR in ADT constructor signature");
|
2019-05-26 03:55:50 -05:00
|
|
|
let sig = tcx.normalize_erasing_regions(param_env, sig);
|
2017-02-07 15:46:21 -06:00
|
|
|
|
|
|
|
let (adt_def, substs) = match sig.output().sty {
|
2018-08-21 19:35:02 -05:00
|
|
|
ty::Adt(adt_def, substs) => (adt_def, substs),
|
2017-02-07 15:46:21 -06:00
|
|
|
_ => bug!("unexpected type for ADT ctor {:?}", sig.output())
|
|
|
|
};
|
|
|
|
|
2019-05-26 03:55:50 -05:00
|
|
|
debug!("build_ctor: ctor_id={:?} sig={:?}", ctor_id, sig);
|
2017-02-07 15:46:21 -06:00
|
|
|
|
2017-04-11 15:52:51 -05:00
|
|
|
let local_decls = local_decls_for_sig(&sig, span);
|
2017-02-07 15:46:21 -06:00
|
|
|
|
|
|
|
let source_info = SourceInfo {
|
2017-08-07 00:54:09 -05:00
|
|
|
span,
|
2018-05-28 06:16:09 -05:00
|
|
|
scope: OUTERMOST_SOURCE_SCOPE
|
2017-02-07 15:46:21 -06:00
|
|
|
};
|
|
|
|
|
2019-05-26 03:55:50 -05:00
|
|
|
let variant_index = if adt_def.is_enum() {
|
|
|
|
adt_def.variant_index_with_ctor_id(ctor_id)
|
2017-02-07 15:46:21 -06:00
|
|
|
} else {
|
2018-11-01 13:03:38 -05:00
|
|
|
VariantIdx::new(0)
|
2017-02-07 15:46:21 -06:00
|
|
|
};
|
|
|
|
|
2019-05-26 03:55:50 -05:00
|
|
|
// Generate the following MIR:
|
|
|
|
//
|
|
|
|
// (return as Variant).field0 = arg0;
|
|
|
|
// (return as Variant).field1 = arg1;
|
|
|
|
//
|
|
|
|
// return;
|
|
|
|
debug!("build_ctor: variant_index={:?}", variant_index);
|
|
|
|
|
|
|
|
let statements = expand_aggregate(
|
|
|
|
Place::RETURN_PLACE,
|
|
|
|
adt_def
|
|
|
|
.variants[variant_index]
|
|
|
|
.fields
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(idx, field_def)| (
|
|
|
|
Operand::Move(Place::Base(PlaceBase::Local(Local::new(idx + 1)))),
|
|
|
|
field_def.ty(tcx, substs),
|
|
|
|
)),
|
|
|
|
AggregateKind::Adt(adt_def, variant_index, substs, None, None),
|
|
|
|
source_info,
|
|
|
|
).collect();
|
|
|
|
|
2017-02-07 15:46:21 -06:00
|
|
|
let start_block = BasicBlockData {
|
2019-05-26 03:55:50 -05:00
|
|
|
statements,
|
2017-02-07 15:46:21 -06:00
|
|
|
terminator: Some(Terminator {
|
2017-08-07 00:54:09 -05:00
|
|
|
source_info,
|
2017-02-07 15:46:21 -06:00
|
|
|
kind: TerminatorKind::Return,
|
|
|
|
}),
|
|
|
|
is_cleanup: false
|
|
|
|
};
|
|
|
|
|
2019-05-26 03:55:50 -05:00
|
|
|
let body = Body::new(
|
2017-02-07 15:46:21 -06:00
|
|
|
IndexVec::from_elem_n(start_block, 1),
|
|
|
|
IndexVec::from_elem_n(
|
2018-05-28 06:16:09 -05:00
|
|
|
SourceScopeData { span: span, parent_scope: None }, 1
|
2017-02-07 15:46:21 -06:00
|
|
|
),
|
2017-11-22 06:47:50 -06:00
|
|
|
ClearCrossCrate::Clear,
|
2017-02-07 15:46:21 -06:00
|
|
|
IndexVec::new(),
|
2016-12-26 07:34:03 -06:00
|
|
|
None,
|
2017-02-07 15:46:21 -06:00
|
|
|
local_decls,
|
2018-11-16 15:56:18 -06:00
|
|
|
IndexVec::new(),
|
2017-02-07 15:46:21 -06:00
|
|
|
sig.inputs().len(),
|
|
|
|
vec![],
|
2018-11-24 07:38:31 -06:00
|
|
|
span,
|
2018-11-26 10:30:19 -06:00
|
|
|
vec![],
|
2019-05-26 03:55:50 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
crate::util::dump_mir(
|
|
|
|
tcx,
|
|
|
|
None,
|
|
|
|
"mir_map",
|
|
|
|
&0,
|
|
|
|
crate::transform::MirSource::item(ctor_id),
|
|
|
|
&body,
|
|
|
|
|_, _| Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
tcx.arena.alloc(body)
|
2017-02-07 15:46:21 -06:00
|
|
|
}
|