2019-10-09 06:20:23 -07:00
|
|
|
use crate::hir::CodegenFnAttrFlags;
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::hir::Unsafety;
|
2018-12-12 15:12:48 +02:00
|
|
|
use crate::hir::def::Namespace;
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::hir::def_id::DefId;
|
2019-02-09 22:11:53 +08:00
|
|
|
use crate::ty::{self, Ty, PolyFnSig, TypeFoldable, SubstsRef, TyCtxt};
|
2019-01-25 12:11:50 +02:00
|
|
|
use crate::ty::print::{FmtPrinter, Printer};
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::traits;
|
2019-05-23 12:45:22 -05:00
|
|
|
use crate::middle::lang_items::DropInPlaceFnLangItem;
|
2018-04-25 19:30:39 +03:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2018-12-03 01:14:35 +01:00
|
|
|
use rustc_macros::HashStable;
|
2017-02-08 18:31:03 +01:00
|
|
|
|
|
|
|
use std::fmt;
|
2018-10-06 23:00:35 +09:00
|
|
|
use std::iter;
|
2017-02-08 18:31:03 +01:00
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
2017-02-08 18:31:03 +01:00
|
|
|
pub struct Instance<'tcx> {
|
|
|
|
pub def: InstanceDef<'tcx>,
|
2019-02-09 22:11:53 +08:00
|
|
|
pub substs: SubstsRef<'tcx>,
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
2017-02-08 18:31:03 +01:00
|
|
|
pub enum InstanceDef<'tcx> {
|
|
|
|
Item(DefId),
|
2017-03-08 01:41:26 +02:00
|
|
|
Intrinsic(DefId),
|
2017-08-04 14:44:12 +02:00
|
|
|
|
2018-09-10 22:54:48 +09:00
|
|
|
/// `<T as Trait>::method` where `method` receives unsizeable `self: Self`.
|
|
|
|
VtableShim(DefId),
|
|
|
|
|
2019-10-09 21:02:54 -07:00
|
|
|
/// `fn()` pointer where the function itself cannot be turned into a pointer.
|
|
|
|
///
|
2019-10-10 07:25:54 -07:00
|
|
|
/// One example in the compiler today is functions annotated with `#[track_caller]`, which
|
|
|
|
/// must have their implicit caller location argument populated for a call. Because this is a
|
|
|
|
/// required part of the function's ABI but can't be tracked as a property of the function
|
|
|
|
/// pointer, we create a single "caller location" at the site where the function is reified.
|
2019-10-03 19:10:34 -07:00
|
|
|
ReifyShim(DefId),
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// `<fn() as FnTrait>::call_*`
|
|
|
|
/// `DefId` is `FnTrait::call_*`
|
2017-02-08 18:31:03 +01:00
|
|
|
FnPtrShim(DefId, Ty<'tcx>),
|
2017-08-04 14:44:12 +02:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// `<Trait as Trait>::fn`
|
2017-03-08 01:41:26 +02:00
|
|
|
Virtual(DefId, usize),
|
2017-08-04 14:44:12 +02:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// `<[mut closure] as FnOnce>::call_once`
|
2017-03-08 23:19:09 +02:00
|
|
|
ClosureOnceShim { call_once: DefId },
|
2017-08-04 14:44:12 +02:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// `drop_in_place::<T>; None` for empty drop glue.
|
2017-03-14 01:08:21 +02:00
|
|
|
DropGlue(DefId, Option<Ty<'tcx>>),
|
2017-08-04 14:44:12 +02:00
|
|
|
|
2017-09-13 22:40:48 +02:00
|
|
|
///`<T as Clone>::clone` shim.
|
2017-08-07 16:21:08 +02:00
|
|
|
CloneShim(DefId, Ty<'tcx>),
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 23:35:39 +03:00
|
|
|
impl<'tcx> Instance<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
|
2017-12-16 19:48:39 -06:00
|
|
|
let ty = tcx.type_of(self.def.def_id());
|
2018-03-09 12:53:17 -05:00
|
|
|
tcx.subst_and_normalize_erasing_regions(
|
|
|
|
self.substs,
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
&ty,
|
|
|
|
)
|
2017-10-25 17:27:27 +02:00
|
|
|
}
|
2018-10-06 23:00:35 +09:00
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
fn fn_sig_noadjust(&self, tcx: TyCtxt<'tcx>) -> PolyFnSig<'tcx> {
|
2018-10-06 23:00:35 +09:00
|
|
|
let ty = self.ty(tcx);
|
2019-09-16 19:08:35 +01:00
|
|
|
match ty.kind {
|
2018-10-06 23:00:35 +09:00
|
|
|
ty::FnDef(..) |
|
|
|
|
// Shims currently have type FnPtr. Not sure this should remain.
|
|
|
|
ty::FnPtr(_) => ty.fn_sig(tcx),
|
|
|
|
ty::Closure(def_id, substs) => {
|
2019-09-26 17:30:44 +00:00
|
|
|
let sig = substs.as_closure().sig(def_id, tcx);
|
2018-10-06 23:00:35 +09:00
|
|
|
|
|
|
|
let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
|
|
|
|
sig.map_bound(|sig| tcx.mk_fn_sig(
|
|
|
|
iter::once(*env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
|
|
|
|
sig.output(),
|
2019-02-08 17:30:42 +00:00
|
|
|
sig.c_variadic,
|
2018-10-06 23:00:35 +09:00
|
|
|
sig.unsafety,
|
|
|
|
sig.abi
|
|
|
|
))
|
|
|
|
}
|
|
|
|
ty::Generator(def_id, substs, _) => {
|
2019-10-03 21:21:28 +08:00
|
|
|
let sig = substs.as_generator().poly_sig(def_id, tcx);
|
2018-10-06 23:00:35 +09:00
|
|
|
|
|
|
|
let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
|
|
|
|
let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
|
|
|
|
|
2018-11-07 00:11:58 +01:00
|
|
|
let pin_did = tcx.lang_items().pin_type().unwrap();
|
|
|
|
let pin_adt_ref = tcx.adt_def(pin_did);
|
|
|
|
let pin_substs = tcx.intern_substs(&[env_ty.into()]);
|
|
|
|
let env_ty = tcx.mk_adt(pin_adt_ref, pin_substs);
|
|
|
|
|
2018-10-06 23:00:35 +09:00
|
|
|
sig.map_bound(|sig| {
|
|
|
|
let state_did = tcx.lang_items().gen_state().unwrap();
|
|
|
|
let state_adt_ref = tcx.adt_def(state_did);
|
|
|
|
let state_substs = tcx.intern_substs(&[
|
|
|
|
sig.yield_ty.into(),
|
|
|
|
sig.return_ty.into(),
|
|
|
|
]);
|
|
|
|
let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
|
|
|
|
|
|
|
|
tcx.mk_fn_sig(iter::once(env_ty),
|
|
|
|
ret_ty,
|
|
|
|
false,
|
|
|
|
Unsafety::Normal,
|
|
|
|
Abi::Rust
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => bug!("unexpected type {:?} in Instance::fn_sig_noadjust", ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn fn_sig(&self, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> {
|
2018-10-06 23:00:35 +09:00
|
|
|
let mut fn_sig = self.fn_sig_noadjust(tcx);
|
|
|
|
if let InstanceDef::VtableShim(..) = self.def {
|
|
|
|
// Modify fn(self, ...) to fn(self: *mut Self, ...)
|
|
|
|
fn_sig = fn_sig.map_bound(|mut fn_sig| {
|
|
|
|
let mut inputs_and_output = fn_sig.inputs_and_output.to_vec();
|
|
|
|
inputs_and_output[0] = tcx.mk_mut_ptr(inputs_and_output[0]);
|
|
|
|
fn_sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output);
|
|
|
|
fn_sig
|
|
|
|
});
|
|
|
|
}
|
|
|
|
fn_sig
|
|
|
|
}
|
2017-10-25 17:27:27 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 18:31:03 +01:00
|
|
|
impl<'tcx> InstanceDef<'tcx> {
|
|
|
|
#[inline]
|
|
|
|
pub fn def_id(&self) -> DefId {
|
|
|
|
match *self {
|
|
|
|
InstanceDef::Item(def_id) |
|
2018-09-10 22:54:48 +09:00
|
|
|
InstanceDef::VtableShim(def_id) |
|
2019-10-03 19:10:34 -07:00
|
|
|
InstanceDef::ReifyShim(def_id) |
|
2017-03-08 01:41:26 +02:00
|
|
|
InstanceDef::FnPtrShim(def_id, _) |
|
|
|
|
InstanceDef::Virtual(def_id, _) |
|
|
|
|
InstanceDef::Intrinsic(def_id, ) |
|
2017-08-04 14:44:12 +02:00
|
|
|
InstanceDef::ClosureOnceShim { call_once: def_id } |
|
|
|
|
InstanceDef::DropGlue(def_id, _) |
|
2017-08-07 16:21:08 +02:00
|
|
|
InstanceDef::CloneShim(def_id, _) => def_id
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn attrs(&self, tcx: TyCtxt<'tcx>) -> ty::Attributes<'tcx> {
|
2017-02-08 18:31:03 +01:00
|
|
|
tcx.get_attrs(self.def_id())
|
|
|
|
}
|
2017-10-27 11:27:05 +02:00
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn is_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::hir::map::DefPathData;
|
2017-10-27 11:27:05 +02:00
|
|
|
let def_id = match *self {
|
|
|
|
ty::InstanceDef::Item(def_id) => def_id,
|
|
|
|
ty::InstanceDef::DropGlue(_, Some(_)) => return false,
|
|
|
|
_ => return true
|
|
|
|
};
|
|
|
|
match tcx.def_key(def_id).disambiguated_data.data {
|
2019-03-24 17:49:58 +03:00
|
|
|
DefPathData::Ctor | DefPathData::ClosureExpr => true,
|
2017-10-27 11:27:05 +02:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn requires_local(&self, tcx: TyCtxt<'tcx>) -> bool {
|
2017-10-27 11:27:05 +02:00
|
|
|
if self.is_inline(tcx) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if let ty::InstanceDef::DropGlue(..) = *self {
|
2018-05-08 16:10:16 +03:00
|
|
|
// Drop glue wants to be instantiated at every codegen
|
2017-10-27 11:27:05 +02:00
|
|
|
// unit, but without an #[inline] hint. We should make this
|
|
|
|
// available to normal end-users.
|
|
|
|
return true
|
|
|
|
}
|
2018-11-17 15:36:06 +01:00
|
|
|
tcx.codegen_fn_attrs(self.def_id()).requests_inline()
|
2017-10-27 11:27:05 +02:00
|
|
|
}
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> fmt::Display for Instance<'tcx> {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::tls::with(|tcx| {
|
|
|
|
let substs = tcx.lift(&self.substs).expect("could not lift for printing");
|
|
|
|
FmtPrinter::new(tcx, &mut *f, Namespace::ValueNS)
|
2019-01-29 07:21:11 +02:00
|
|
|
.print_def_path(self.def_id(), substs)?;
|
2019-01-18 22:14:01 +02:00
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
2017-02-08 18:31:03 +01:00
|
|
|
match self.def {
|
2017-03-08 01:41:26 +02:00
|
|
|
InstanceDef::Item(_) => Ok(()),
|
2018-09-10 22:54:48 +09:00
|
|
|
InstanceDef::VtableShim(_) => {
|
|
|
|
write!(f, " - shim(vtable)")
|
|
|
|
}
|
2019-10-03 19:10:34 -07:00
|
|
|
InstanceDef::ReifyShim(_) => {
|
|
|
|
write!(f, " - shim(reify)")
|
|
|
|
}
|
2017-03-08 01:41:26 +02:00
|
|
|
InstanceDef::Intrinsic(_) => {
|
|
|
|
write!(f, " - intrinsic")
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
2017-03-08 01:41:26 +02:00
|
|
|
InstanceDef::Virtual(_, num) => {
|
|
|
|
write!(f, " - shim(#{})", num)
|
|
|
|
}
|
|
|
|
InstanceDef::FnPtrShim(_, ty) => {
|
2017-02-08 18:31:03 +01:00
|
|
|
write!(f, " - shim({:?})", ty)
|
|
|
|
}
|
2017-03-08 23:19:09 +02:00
|
|
|
InstanceDef::ClosureOnceShim { .. } => {
|
|
|
|
write!(f, " - shim")
|
2017-03-08 01:41:26 +02:00
|
|
|
}
|
2017-03-14 01:08:21 +02:00
|
|
|
InstanceDef::DropGlue(_, ty) => {
|
|
|
|
write!(f, " - shim({:?})", ty)
|
|
|
|
}
|
2017-08-07 16:21:08 +02:00
|
|
|
InstanceDef::CloneShim(_, ty) => {
|
2017-08-04 14:44:12 +02:00
|
|
|
write!(f, " - shim({:?})", ty)
|
|
|
|
}
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx> Instance<'tcx> {
|
2019-02-09 22:11:53 +08:00
|
|
|
pub fn new(def_id: DefId, substs: SubstsRef<'tcx>)
|
2017-02-08 18:31:03 +01:00
|
|
|
-> Instance<'tcx> {
|
2018-10-22 22:38:51 +02:00
|
|
|
assert!(!substs.has_escaping_bound_vars(),
|
2018-05-08 16:10:16 +03:00
|
|
|
"substs of instance {:?} not normalized for codegen: {:?}",
|
2017-02-08 18:31:03 +01:00
|
|
|
def_id, substs);
|
|
|
|
Instance { def: InstanceDef::Item(def_id), substs: substs }
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
|
2019-09-25 15:36:14 -04:00
|
|
|
Instance::new(def_id, tcx.empty_substs_for_def_id(def_id))
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn def_id(&self) -> DefId {
|
|
|
|
self.def.def_id()
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Resolves a `(def_id, substs)` pair to an (optional) instance -- most commonly,
|
2017-12-06 09:25:29 +01:00
|
|
|
/// this is used to find the precise code that will run for a trait method invocation,
|
|
|
|
/// if known.
|
|
|
|
///
|
|
|
|
/// Returns `None` if we cannot resolve `Instance` to a specific instance.
|
|
|
|
/// For example, in a context like this,
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn foo<T: Debug>(t: T) { ... }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// trying to resolve `Debug::fmt` applied to `T` will yield `None`, because we do not
|
|
|
|
/// know what code ought to run. (Note that this setting is also affected by the
|
|
|
|
/// `RevealMode` in the parameter environment.)
|
|
|
|
///
|
|
|
|
/// Presuming that coherence and type-check have succeeded, if this method is invoked
|
2018-05-08 16:10:16 +03:00
|
|
|
/// in a monomorphic context (i.e., like during codegen), then it is guaranteed to return
|
2017-12-06 09:25:29 +01:00
|
|
|
/// `Some`.
|
2019-06-12 00:11:55 +03:00
|
|
|
pub fn resolve(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: SubstsRef<'tcx>,
|
|
|
|
) -> Option<Instance<'tcx>> {
|
2017-12-06 09:25:29 +01:00
|
|
|
debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
|
|
|
|
let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
|
|
|
|
debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env);
|
|
|
|
let item = tcx.associated_item(def_id);
|
|
|
|
resolve_associated_item(tcx, &item, param_env, trait_def_id, substs)
|
|
|
|
} else {
|
|
|
|
let ty = tcx.type_of(def_id);
|
2018-03-09 12:53:17 -05:00
|
|
|
let item_type = tcx.subst_and_normalize_erasing_regions(
|
|
|
|
substs,
|
|
|
|
param_env,
|
|
|
|
&ty,
|
|
|
|
);
|
2017-12-06 09:25:29 +01:00
|
|
|
|
2019-09-16 19:08:35 +01:00
|
|
|
let def = match item_type.kind {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::FnDef(..) if {
|
2017-12-06 09:25:29 +01:00
|
|
|
let f = item_type.fn_sig(tcx);
|
|
|
|
f.abi() == Abi::RustIntrinsic ||
|
|
|
|
f.abi() == Abi::PlatformIntrinsic
|
|
|
|
} =>
|
|
|
|
{
|
|
|
|
debug!(" => intrinsic");
|
|
|
|
ty::InstanceDef::Intrinsic(def_id)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
if Some(def_id) == tcx.lang_items().drop_in_place_fn() {
|
|
|
|
let ty = substs.type_at(0);
|
2018-02-10 13:18:02 -05:00
|
|
|
if ty.needs_drop(tcx, ty::ParamEnv::reveal_all()) {
|
2017-12-06 09:25:29 +01:00
|
|
|
debug!(" => nontrivial drop glue");
|
|
|
|
ty::InstanceDef::DropGlue(def_id, Some(ty))
|
|
|
|
} else {
|
|
|
|
debug!(" => trivial drop glue");
|
|
|
|
ty::InstanceDef::DropGlue(def_id, None)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debug!(" => free item");
|
|
|
|
ty::InstanceDef::Item(def_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(Instance {
|
|
|
|
def: def,
|
|
|
|
substs: substs
|
|
|
|
})
|
|
|
|
};
|
|
|
|
debug!("resolve(def_id={:?}, substs={:?}) = {:?}", def_id, substs, result);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2019-10-05 14:24:07 -07:00
|
|
|
pub fn resolve_for_fn_ptr(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: SubstsRef<'tcx>,
|
|
|
|
) -> Option<Instance<'tcx>> {
|
|
|
|
debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
|
2019-10-09 06:20:23 -07:00
|
|
|
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::TRACK_CALLER) {
|
2019-10-09 06:05:49 -07:00
|
|
|
debug!(" => fn pointer created for function with #[track_caller]");
|
2019-10-05 14:24:07 -07:00
|
|
|
Some(Instance {
|
|
|
|
def: InstanceDef::ReifyShim(def_id),
|
|
|
|
substs,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Instance::resolve(tcx, param_env, def_id, substs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 00:11:55 +03:00
|
|
|
pub fn resolve_for_vtable(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: SubstsRef<'tcx>,
|
|
|
|
) -> Option<Instance<'tcx>> {
|
2018-09-10 23:00:50 +09:00
|
|
|
debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
|
|
|
|
let fn_sig = tcx.fn_sig(def_id);
|
2019-07-07 16:34:06 +01:00
|
|
|
let is_vtable_shim = fn_sig.inputs().skip_binder().len() > 0
|
|
|
|
&& fn_sig.input(0).skip_binder().is_param(0)
|
|
|
|
&& tcx.generics_of(def_id).has_self;
|
2018-09-10 23:00:50 +09:00
|
|
|
if is_vtable_shim {
|
|
|
|
debug!(" => associated item with unsizeable self: Self");
|
|
|
|
Some(Instance {
|
|
|
|
def: InstanceDef::VtableShim(def_id),
|
|
|
|
substs,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Instance::resolve(tcx, param_env, def_id, substs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
pub fn resolve_closure(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-10-02 10:52:43 +02:00
|
|
|
def_id: DefId,
|
2019-09-26 17:30:44 +00:00
|
|
|
substs: ty::SubstsRef<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
requested_kind: ty::ClosureKind,
|
|
|
|
) -> Instance<'tcx> {
|
2019-09-26 17:30:44 +00:00
|
|
|
let actual_kind = substs.as_closure().kind(def_id, tcx);
|
2017-12-06 09:25:29 +01:00
|
|
|
|
|
|
|
match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
|
2019-09-26 17:30:44 +00:00
|
|
|
Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, substs),
|
|
|
|
_ => Instance::new(def_id, substs)
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-10 23:00:50 +09:00
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
|
2019-08-28 06:17:58 +09:00
|
|
|
let def_id = tcx.require_lang_item(DropInPlaceFnLangItem, None);
|
2019-05-23 12:45:22 -05:00
|
|
|
let substs = tcx.intern_substs(&[ty.into()]);
|
|
|
|
Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fn_once_adapter_instance(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-05-23 12:45:22 -05:00
|
|
|
closure_did: DefId,
|
2019-09-26 16:09:51 +00:00
|
|
|
substs: ty::SubstsRef<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
) -> Instance<'tcx> {
|
2019-05-23 12:45:22 -05:00
|
|
|
debug!("fn_once_adapter_shim({:?}, {:?})",
|
|
|
|
closure_did,
|
|
|
|
substs);
|
|
|
|
let fn_once = tcx.lang_items().fn_once_trait().unwrap();
|
|
|
|
let call_once = tcx.associated_items(fn_once)
|
|
|
|
.find(|it| it.kind == ty::AssocKind::Method)
|
|
|
|
.unwrap().def_id;
|
|
|
|
let def = ty::InstanceDef::ClosureOnceShim { call_once };
|
|
|
|
|
|
|
|
let self_ty = tcx.mk_closure(closure_did, substs);
|
|
|
|
|
2019-09-26 17:30:44 +00:00
|
|
|
let sig = substs.as_closure().sig(closure_did, tcx);
|
2019-05-23 12:45:22 -05:00
|
|
|
let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
|
|
|
|
assert_eq!(sig.inputs().len(), 1);
|
|
|
|
let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
|
|
|
|
|
|
|
|
debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
|
|
|
|
Instance { def, substs }
|
|
|
|
}
|
|
|
|
|
2018-09-10 23:00:50 +09:00
|
|
|
pub fn is_vtable_shim(&self) -> bool {
|
|
|
|
if let InstanceDef::VtableShim(..) = self.def {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 23:35:39 +03:00
|
|
|
fn resolve_associated_item<'tcx>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-05-19 16:26:08 +08:00
|
|
|
trait_item: &ty::AssocItem,
|
2017-12-06 09:25:29 +01:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
trait_id: DefId,
|
2019-02-09 22:11:53 +08:00
|
|
|
rcvr_substs: SubstsRef<'tcx>,
|
2017-12-06 09:25:29 +01:00
|
|
|
) -> Option<Instance<'tcx>> {
|
|
|
|
let def_id = trait_item.def_id;
|
|
|
|
debug!("resolve_associated_item(trait_item={:?}, \
|
2018-12-03 00:49:12 +02:00
|
|
|
param_env={:?}, \
|
2018-10-02 10:52:43 +02:00
|
|
|
trait_id={:?}, \
|
|
|
|
rcvr_substs={:?})",
|
2018-12-03 00:49:12 +02:00
|
|
|
def_id, param_env, trait_id, rcvr_substs);
|
2017-12-06 09:25:29 +01:00
|
|
|
|
|
|
|
let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
|
2018-05-08 16:10:16 +03:00
|
|
|
let vtbl = tcx.codegen_fulfill_obligation((param_env, ty::Binder::bind(trait_ref)));
|
2017-12-06 09:25:29 +01:00
|
|
|
|
|
|
|
// Now that we know which impl is being used, we can dispatch to
|
|
|
|
// the actual function:
|
|
|
|
match vtbl {
|
|
|
|
traits::VtableImpl(impl_data) => {
|
|
|
|
let (def_id, substs) = traits::find_associated_item(
|
2018-12-03 00:49:12 +02:00
|
|
|
tcx, param_env, trait_item, rcvr_substs, &impl_data);
|
2017-12-06 09:25:29 +01:00
|
|
|
let substs = tcx.erase_regions(&substs);
|
|
|
|
Some(ty::Instance::new(def_id, substs))
|
|
|
|
}
|
2018-05-02 13:14:30 +02:00
|
|
|
traits::VtableGenerator(generator_data) => {
|
2017-12-06 09:25:29 +01:00
|
|
|
Some(Instance {
|
2018-05-02 13:14:30 +02:00
|
|
|
def: ty::InstanceDef::Item(generator_data.generator_def_id),
|
2019-10-03 21:30:26 +08:00
|
|
|
substs: generator_data.substs
|
2017-12-06 09:25:29 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
traits::VtableClosure(closure_data) => {
|
|
|
|
let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap();
|
|
|
|
Some(Instance::resolve_closure(tcx, closure_data.closure_def_id, closure_data.substs,
|
2018-10-02 10:52:43 +02:00
|
|
|
trait_closure_kind))
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
traits::VtableFnPointer(ref data) => {
|
|
|
|
Some(Instance {
|
|
|
|
def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty),
|
|
|
|
substs: rcvr_substs
|
|
|
|
})
|
|
|
|
}
|
|
|
|
traits::VtableObject(ref data) => {
|
|
|
|
let index = tcx.get_vtable_index_of_object_method(data, def_id);
|
|
|
|
Some(Instance {
|
|
|
|
def: ty::InstanceDef::Virtual(def_id, index),
|
|
|
|
substs: rcvr_substs
|
|
|
|
})
|
|
|
|
}
|
|
|
|
traits::VtableBuiltin(..) => {
|
2018-07-27 13:20:13 +02:00
|
|
|
if tcx.lang_items().clone_trait().is_some() {
|
2017-12-06 09:25:29 +01:00
|
|
|
Some(Instance {
|
|
|
|
def: ty::InstanceDef::CloneShim(def_id, trait_ref.self_ty()),
|
|
|
|
substs: rcvr_substs
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2018-10-12 01:50:03 +01:00
|
|
|
traits::VtableAutoImpl(..) |
|
|
|
|
traits::VtableParam(..) |
|
|
|
|
traits::VtableTraitAlias(..) => None
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 13:20:33 +03:00
|
|
|
fn needs_fn_once_adapter_shim(
|
|
|
|
actual_closure_kind: ty::ClosureKind,
|
|
|
|
trait_closure_kind: ty::ClosureKind,
|
|
|
|
) -> Result<bool, ()> {
|
2017-12-06 09:25:29 +01:00
|
|
|
match (actual_closure_kind, trait_closure_kind) {
|
|
|
|
(ty::ClosureKind::Fn, ty::ClosureKind::Fn) |
|
|
|
|
(ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) |
|
|
|
|
(ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
|
|
|
|
// No adapter needed.
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
(ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
|
|
|
|
// The closure fn `llfn` is a `fn(&self, ...)`. We want a
|
2018-05-08 16:10:16 +03:00
|
|
|
// `fn(&mut self, ...)`. In fact, at codegen time, these are
|
2017-12-06 09:25:29 +01:00
|
|
|
// basically the same thing, so we can just return llfn.
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
(ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) |
|
|
|
|
(ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
|
|
|
|
// The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
|
|
|
|
// self, ...)`. We want a `fn(self, ...)`. We can produce
|
|
|
|
// this by doing something like:
|
|
|
|
//
|
|
|
|
// fn call_once(self, ...) { call_mut(&self, ...) }
|
|
|
|
// fn call_once(mut self, ...) { call_mut(&mut self, ...) }
|
|
|
|
//
|
2018-05-08 16:10:16 +03:00
|
|
|
// These are both the same at codegen time.
|
2017-12-06 09:25:29 +01:00
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
(ty::ClosureKind::FnMut, _) |
|
|
|
|
(ty::ClosureKind::FnOnce, _) => Err(())
|
|
|
|
}
|
|
|
|
}
|