2022-07-17 20:02:49 +00:00
|
|
|
use rustc_middle::mir::interpret::{InterpResult, Pointer};
|
|
|
|
use rustc_middle::ty::layout::LayoutOf;
|
2024-06-10 15:24:36 +00:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
2021-06-20 09:43:25 +00:00
|
|
|
use rustc_target::abi::{Align, Size};
|
2024-05-22 04:20:23 +00:00
|
|
|
use tracing::trace;
|
2018-06-08 02:47:26 +00:00
|
|
|
|
2020-07-24 12:16:54 +00:00
|
|
|
use super::util::ensure_monomorphic_enough;
|
2024-06-10 15:24:36 +00:00
|
|
|
use super::{InterpCx, MPlaceTy, Machine, MemPlaceMeta, OffsetMode, Projectable};
|
2020-03-21 12:49:02 +00:00
|
|
|
|
2024-05-27 06:24:23 +00:00
|
|
|
impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
|
2016-09-11 09:06:44 +00:00
|
|
|
/// Creates a dynamic vtable for the given type and vtable origin. This is used only for
|
|
|
|
/// objects.
|
2016-09-09 10:51:14 +00:00
|
|
|
///
|
2022-07-19 23:50:04 +00:00
|
|
|
/// The `trait_ref` encodes the erased self type. Hence, if we are making an object `Foo<Trait>`
|
|
|
|
/// from a value of type `Foo<T>`, then `trait_ref` would map `T: Trait`. `None` here means that
|
|
|
|
/// this is an auto trait without any methods, so we only need the basic vtable (drop, size,
|
|
|
|
/// align).
|
2022-07-17 20:02:49 +00:00
|
|
|
pub fn get_vtable_ptr(
|
|
|
|
&self,
|
2017-08-10 15:48:38 +00:00
|
|
|
ty: Ty<'tcx>,
|
2018-12-04 11:28:06 +00:00
|
|
|
poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
|
2022-07-18 22:47:31 +00:00
|
|
|
) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
|
2018-10-17 10:36:18 +00:00
|
|
|
trace!("get_vtable(trait_ref={:?})", poly_trait_ref);
|
2016-09-09 10:51:14 +00:00
|
|
|
|
2020-10-24 00:21:18 +00:00
|
|
|
let (ty, poly_trait_ref) = self.tcx.erase_regions((ty, poly_trait_ref));
|
2018-10-12 14:45:17 +00:00
|
|
|
|
2019-08-12 15:59:45 +00:00
|
|
|
// All vtables must be monomorphic, bail out otherwise.
|
2020-07-24 12:16:54 +00:00
|
|
|
ensure_monomorphic_enough(*self.tcx, ty)?;
|
|
|
|
ensure_monomorphic_enough(*self.tcx, poly_trait_ref)?;
|
2019-08-12 15:59:45 +00:00
|
|
|
|
2023-09-12 06:42:36 +00:00
|
|
|
let vtable_symbolic_allocation = self.tcx.reserve_and_set_vtable_alloc(ty, poly_trait_ref);
|
2024-04-16 16:37:56 +00:00
|
|
|
let vtable_ptr = self.global_root_pointer(Pointer::from(vtable_symbolic_allocation))?;
|
2021-08-01 12:09:22 +00:00
|
|
|
Ok(vtable_ptr.into())
|
2016-09-09 10:51:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 20:02:49 +00:00
|
|
|
pub fn get_vtable_size_and_align(
|
2017-08-10 15:48:38 +00:00
|
|
|
&self,
|
2022-07-18 22:47:31 +00:00
|
|
|
vtable: Pointer<Option<M::Provenance>>,
|
2024-06-11 05:47:58 +00:00
|
|
|
expected_trait: Option<&'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>>,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx, (Size, Align)> {
|
2024-06-11 05:47:58 +00:00
|
|
|
let ty = self.get_ptr_vtable_ty(vtable, expected_trait)?;
|
2022-07-17 20:02:49 +00:00
|
|
|
let layout = self.layout_of(ty)?;
|
2022-11-13 11:14:59 +00:00
|
|
|
assert!(layout.is_sized(), "there are no vtables for unsized types");
|
2022-07-17 20:02:49 +00:00
|
|
|
Ok((layout.size, layout.align.abi))
|
2021-07-31 14:46:23 +00:00
|
|
|
}
|
2024-06-10 15:24:36 +00:00
|
|
|
|
|
|
|
/// Turn a place with a `dyn Trait` type into a place with the actual dynamic type.
|
|
|
|
pub(super) fn unpack_dyn_trait(
|
|
|
|
&self,
|
|
|
|
mplace: &MPlaceTy<'tcx, M::Provenance>,
|
|
|
|
expected_trait: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
|
|
|
|
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
|
|
|
|
assert!(
|
|
|
|
matches!(mplace.layout.ty.kind(), ty::Dynamic(_, _, ty::Dyn)),
|
|
|
|
"`unpack_dyn_trait` only makes sense on `dyn*` types"
|
|
|
|
);
|
|
|
|
let vtable = mplace.meta().unwrap_meta().to_pointer(self)?;
|
|
|
|
let ty = self.get_ptr_vtable_ty(vtable, Some(expected_trait))?;
|
|
|
|
// This is a kind of transmute, from a place with unsized type and metadata to
|
|
|
|
// a place with sized type and no metadata.
|
|
|
|
let layout = self.layout_of(ty)?;
|
|
|
|
let mplace = mplace.offset_with_meta(
|
|
|
|
Size::ZERO,
|
|
|
|
OffsetMode::Wrapping,
|
|
|
|
MemPlaceMeta::None,
|
|
|
|
layout,
|
|
|
|
self,
|
|
|
|
)?;
|
|
|
|
Ok(mplace)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Turn a `dyn* Trait` type into an value with the actual dynamic type.
|
|
|
|
pub(super) fn unpack_dyn_star<P: Projectable<'tcx, M::Provenance>>(
|
|
|
|
&self,
|
|
|
|
val: &P,
|
|
|
|
expected_trait: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
|
|
|
|
) -> InterpResult<'tcx, P> {
|
|
|
|
assert!(
|
|
|
|
matches!(val.layout().ty.kind(), ty::Dynamic(_, _, ty::DynStar)),
|
|
|
|
"`unpack_dyn_star` only makes sense on `dyn*` types"
|
|
|
|
);
|
|
|
|
let data = self.project_field(val, 0)?;
|
|
|
|
let vtable = self.project_field(val, 1)?;
|
|
|
|
let vtable = self.read_pointer(&vtable.to_op(self)?)?;
|
|
|
|
let ty = self.get_ptr_vtable_ty(vtable, Some(expected_trait))?;
|
|
|
|
// `data` is already the right thing but has the wrong type. So we transmute it.
|
|
|
|
let layout = self.layout_of(ty)?;
|
|
|
|
let data = data.transmute(layout, self)?;
|
|
|
|
Ok(data)
|
|
|
|
}
|
2016-09-09 10:51:14 +00:00
|
|
|
}
|