2022-07-17 16:02:49 -04:00
|
|
|
use rustc_middle::mir::interpret::{InterpResult, Pointer};
|
|
|
|
|
use rustc_middle::ty::layout::LayoutOf;
|
|
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
2021-06-20 17:43:25 +08:00
|
|
|
use rustc_target::abi::{Align, Size};
|
2018-06-08 03:47:26 +01:00
|
|
|
|
2020-07-24 13:16:54 +01:00
|
|
|
use super::util::ensure_monomorphic_enough;
|
2022-07-17 16:02:49 -04:00
|
|
|
use super::{InterpCx, Machine};
|
2020-03-21 13:49:02 +01:00
|
|
|
|
2020-03-16 15:12:42 -07:00
|
|
|
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
2016-09-11 03:06:44 -06:00
|
|
|
/// Creates a dynamic vtable for the given type and vtable origin. This is used only for
|
|
|
|
|
/// objects.
|
2016-09-09 12:51:14 +02:00
|
|
|
///
|
2022-07-19 19:50:04 -04: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 16:02:49 -04:00
|
|
|
pub fn get_vtable_ptr(
|
|
|
|
|
&self,
|
2017-08-10 08:48:38 -07:00
|
|
|
ty: Ty<'tcx>,
|
2018-12-04 13:28:06 +02:00
|
|
|
poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
|
2018-10-17 12:36:18 +02:00
|
|
|
trace!("get_vtable(trait_ref={:?})", poly_trait_ref);
|
2016-09-09 12:51:14 +02:00
|
|
|
|
2020-10-24 02:21:18 +02:00
|
|
|
let (ty, poly_trait_ref) = self.tcx.erase_regions((ty, poly_trait_ref));
|
2018-10-12 16:45:17 +02:00
|
|
|
|
2019-08-12 18:59:45 +03:00
|
|
|
// All vtables must be monomorphic, bail out otherwise.
|
2020-07-24 13:16:54 +01:00
|
|
|
ensure_monomorphic_enough(*self.tcx, ty)?;
|
|
|
|
|
ensure_monomorphic_enough(*self.tcx, poly_trait_ref)?;
|
2019-08-12 18:59:45 +03:00
|
|
|
|
2022-07-17 16:02:49 -04:00
|
|
|
let vtable_symbolic_allocation = self.tcx.create_vtable_alloc(ty, poly_trait_ref);
|
|
|
|
|
let vtable_ptr = self.global_base_pointer(Pointer::from(vtable_symbolic_allocation))?;
|
2021-08-01 20:09:22 +08:00
|
|
|
Ok(vtable_ptr.into())
|
2016-09-09 12:51:14 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-17 16:02:49 -04:00
|
|
|
/// Returns a high-level representation of the entires of the given vtable.
|
|
|
|
|
pub fn get_vtable_entries(
|
2019-10-28 19:09:54 -04:00
|
|
|
&self,
|
2022-07-18 18:47:31 -04:00
|
|
|
vtable: Pointer<Option<M::Provenance>>,
|
2022-07-17 16:02:49 -04:00
|
|
|
) -> InterpResult<'tcx, &'tcx [ty::VtblEntry<'tcx>]> {
|
|
|
|
|
let (ty, poly_trait_ref) = self.get_ptr_vtable(vtable)?;
|
|
|
|
|
Ok(if let Some(poly_trait_ref) = poly_trait_ref {
|
|
|
|
|
let trait_ref = poly_trait_ref.with_self_ty(*self.tcx, ty);
|
|
|
|
|
let trait_ref = self.tcx.erase_regions(trait_ref);
|
|
|
|
|
self.tcx.vtable_entries(trait_ref)
|
|
|
|
|
} else {
|
|
|
|
|
TyCtxt::COMMON_VTABLE_ENTRIES
|
|
|
|
|
})
|
2019-10-28 19:09:54 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-17 16:02:49 -04:00
|
|
|
pub fn get_vtable_size_and_align(
|
2017-08-10 08:48:38 -07:00
|
|
|
&self,
|
2022-07-18 18:47:31 -04:00
|
|
|
vtable: Pointer<Option<M::Provenance>>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, (Size, Align)> {
|
2022-07-17 16:02:49 -04:00
|
|
|
let (ty, _trait_ref) = self.get_ptr_vtable(vtable)?;
|
|
|
|
|
let layout = self.layout_of(ty)?;
|
|
|
|
|
assert!(!layout.is_unsized(), "there are no vtables for unsized types");
|
|
|
|
|
Ok((layout.size, layout.align.abi))
|
2021-07-31 22:46:23 +08:00
|
|
|
}
|
2016-09-09 12:51:14 +02:00
|
|
|
}
|