2018-10-31 16:46:33 +01:00
|
|
|
//! Visitor for a run-time value with a given layout: Traverse enums, structs and other compound
|
|
|
|
|
//! types until we arrive at the leaves, with custom handling for primitive types.
|
|
|
|
|
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::interpret::InterpResult;
|
|
|
|
|
use rustc_middle::ty;
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_middle::ty::layout::TyAndLayout;
|
|
|
|
|
use rustc_target::abi::{FieldsShape, VariantIdx, Variants};
|
2018-10-31 16:46:33 +01:00
|
|
|
|
2020-04-16 15:15:46 +00:00
|
|
|
use std::num::NonZeroUsize;
|
|
|
|
|
|
2022-07-14 20:32:45 -04:00
|
|
|
use super::{InterpCx, MPlaceTy, Machine, OpTy, PlaceTy};
|
2018-10-31 16:46:33 +01:00
|
|
|
|
2022-07-14 20:32:45 -04:00
|
|
|
/// A thing that we can project into, and that has a layout.
|
|
|
|
|
/// This wouldn't have to depend on `Machine` but with the current type inference,
|
|
|
|
|
/// that's just more convenient to work with (avoids repeating all the `Machine` bounds).
|
2022-07-15 22:58:20 -04:00
|
|
|
pub trait Value<'mir, 'tcx, M: Machine<'mir, 'tcx>>: Sized {
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Gets this value's layout.
|
2020-03-04 14:50:21 +00:00
|
|
|
fn layout(&self) -> TyAndLayout<'tcx>;
|
2018-10-31 16:46:33 +01:00
|
|
|
|
2022-07-14 20:32:45 -04:00
|
|
|
/// Makes this into an `OpTy`, in a cheap way that is good for reading.
|
|
|
|
|
fn to_op_for_read(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &InterpCx<'mir, 'tcx, M>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>>;
|
2022-07-14 20:32:45 -04:00
|
|
|
|
|
|
|
|
/// Makes this into an `OpTy`, in a potentially more expensive way that is good for projections.
|
|
|
|
|
fn to_op_for_proj(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &InterpCx<'mir, 'tcx, M>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2022-07-14 20:32:45 -04:00
|
|
|
self.to_op_for_read(ecx)
|
|
|
|
|
}
|
2018-10-31 18:44:00 +01:00
|
|
|
|
2022-07-04 08:48:05 -04:00
|
|
|
/// Creates this from an `OpTy`.
|
|
|
|
|
///
|
2022-07-14 20:32:45 -04:00
|
|
|
/// If `to_op_for_proj` only ever produces `Indirect` operands, then this one is definitely `Indirect`.
|
2022-07-18 18:47:31 -04:00
|
|
|
fn from_op(op: &OpTy<'tcx, M::Provenance>) -> Self;
|
2018-10-31 18:44:00 +01:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Projects to the given enum variant.
|
2018-11-01 13:53:21 +01:00
|
|
|
fn project_downcast(
|
2021-02-15 00:00:00 +00:00
|
|
|
&self,
|
2019-06-27 11:36:01 +02:00
|
|
|
ecx: &InterpCx<'mir, 'tcx, M>,
|
2018-11-07 16:45:07 +01:00
|
|
|
variant: VariantIdx,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, Self>;
|
2018-11-01 13:53:21 +01:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Projects to the n-th field.
|
2021-02-15 00:00:00 +00:00
|
|
|
fn project_field(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &InterpCx<'mir, 'tcx, M>,
|
|
|
|
|
field: usize,
|
|
|
|
|
) -> InterpResult<'tcx, Self>;
|
2018-10-31 18:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-14 20:32:45 -04:00
|
|
|
/// A thing that we can project into given *mutable* access to `ecx`, and that has a layout.
|
|
|
|
|
/// This wouldn't have to depend on `Machine` but with the current type inference,
|
|
|
|
|
/// that's just more convenient to work with (avoids repeating all the `Machine` bounds).
|
2022-07-15 22:58:20 -04:00
|
|
|
pub trait ValueMut<'mir, 'tcx, M: Machine<'mir, 'tcx>>: Sized {
|
2022-07-14 20:32:45 -04:00
|
|
|
/// Gets this value's layout.
|
|
|
|
|
fn layout(&self) -> TyAndLayout<'tcx>;
|
|
|
|
|
|
|
|
|
|
/// Makes this into an `OpTy`, in a cheap way that is good for reading.
|
|
|
|
|
fn to_op_for_read(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &InterpCx<'mir, 'tcx, M>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>>;
|
2022-07-14 20:32:45 -04:00
|
|
|
|
|
|
|
|
/// Makes this into an `OpTy`, in a potentially more expensive way that is good for projections.
|
|
|
|
|
fn to_op_for_proj(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, M>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>>;
|
2022-07-14 20:32:45 -04:00
|
|
|
|
|
|
|
|
/// Creates this from an `OpTy`.
|
|
|
|
|
///
|
|
|
|
|
/// If `to_op_for_proj` only ever produces `Indirect` operands, then this one is definitely `Indirect`.
|
2022-07-18 18:47:31 -04:00
|
|
|
fn from_op(op: &OpTy<'tcx, M::Provenance>) -> Self;
|
2022-07-14 20:32:45 -04:00
|
|
|
|
|
|
|
|
/// Projects to the given enum variant.
|
|
|
|
|
fn project_downcast(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, M>,
|
|
|
|
|
variant: VariantIdx,
|
|
|
|
|
) -> InterpResult<'tcx, Self>;
|
|
|
|
|
|
|
|
|
|
/// Projects to the n-th field.
|
|
|
|
|
fn project_field(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, M>,
|
|
|
|
|
field: usize,
|
|
|
|
|
) -> InterpResult<'tcx, Self>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We cannot have a general impl which shows that Value implies ValueMut. (When we do, it says we
|
|
|
|
|
// cannot `impl ValueMut for PlaceTy` because some downstream crate could `impl Value for PlaceTy`.)
|
|
|
|
|
// So we have some copy-paste here. (We could have a macro but since we only have 2 types with this
|
|
|
|
|
// double-impl, that would barely make the code shorter, if at all.)
|
|
|
|
|
|
2022-07-18 18:47:31 -04:00
|
|
|
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> Value<'mir, 'tcx, M> for OpTy<'tcx, M::Provenance> {
|
2018-10-31 18:44:00 +01:00
|
|
|
#[inline(always)]
|
2020-03-04 14:50:21 +00:00
|
|
|
fn layout(&self) -> TyAndLayout<'tcx> {
|
2018-10-31 18:44:00 +01:00
|
|
|
self.layout
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2022-07-14 20:32:45 -04:00
|
|
|
fn to_op_for_read(
|
2021-02-15 00:00:00 +00:00
|
|
|
&self,
|
2019-06-27 11:36:01 +02:00
|
|
|
_ecx: &InterpCx<'mir, 'tcx, M>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2022-07-15 22:58:20 -04:00
|
|
|
Ok(self.clone())
|
2018-10-31 18:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2022-07-18 18:47:31 -04:00
|
|
|
fn from_op(op: &OpTy<'tcx, M::Provenance>) -> Self {
|
2022-07-15 22:58:20 -04:00
|
|
|
op.clone()
|
2018-10-31 18:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-01 13:53:21 +01:00
|
|
|
#[inline(always)]
|
|
|
|
|
fn project_downcast(
|
2021-02-15 00:00:00 +00:00
|
|
|
&self,
|
2019-06-27 11:36:01 +02:00
|
|
|
ecx: &InterpCx<'mir, 'tcx, M>,
|
2018-11-07 16:45:07 +01:00
|
|
|
variant: VariantIdx,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, Self> {
|
2018-11-02 09:52:17 +01:00
|
|
|
ecx.operand_downcast(self, variant)
|
2018-11-01 13:53:21 +01:00
|
|
|
}
|
|
|
|
|
|
2018-10-31 18:44:00 +01:00
|
|
|
#[inline(always)]
|
2020-03-21 17:17:01 +01:00
|
|
|
fn project_field(
|
2021-02-15 00:00:00 +00:00
|
|
|
&self,
|
2020-03-21 17:17:01 +01:00
|
|
|
ecx: &InterpCx<'mir, 'tcx, M>,
|
|
|
|
|
field: usize,
|
|
|
|
|
) -> InterpResult<'tcx, Self> {
|
2018-11-02 09:52:17 +01:00
|
|
|
ecx.operand_field(self, field)
|
2018-10-31 18:44:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-02-05 20:44:59 +01:00
|
|
|
|
2022-07-14 20:32:45 -04:00
|
|
|
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueMut<'mir, 'tcx, M>
|
2022-07-18 18:47:31 -04:00
|
|
|
for OpTy<'tcx, M::Provenance>
|
2022-07-14 20:32:45 -04:00
|
|
|
{
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn layout(&self) -> TyAndLayout<'tcx> {
|
|
|
|
|
self.layout
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn to_op_for_read(
|
|
|
|
|
&self,
|
|
|
|
|
_ecx: &InterpCx<'mir, 'tcx, M>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2022-07-15 22:58:20 -04:00
|
|
|
Ok(self.clone())
|
2022-07-14 20:32:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn to_op_for_proj(
|
|
|
|
|
&self,
|
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, M>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2022-07-15 22:58:20 -04:00
|
|
|
Ok(self.clone())
|
2022-07-14 20:32:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2022-07-18 18:47:31 -04:00
|
|
|
fn from_op(op: &OpTy<'tcx, M::Provenance>) -> Self {
|
2022-07-15 22:58:20 -04:00
|
|
|
op.clone()
|
2022-07-14 20:32:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn project_downcast(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, M>,
|
|
|
|
|
variant: VariantIdx,
|
|
|
|
|
) -> InterpResult<'tcx, Self> {
|
|
|
|
|
ecx.operand_downcast(self, variant)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn project_field(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, M>,
|
|
|
|
|
field: usize,
|
|
|
|
|
) -> InterpResult<'tcx, Self> {
|
|
|
|
|
ecx.operand_field(self, field)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 15:12:42 -07:00
|
|
|
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> Value<'mir, 'tcx, M>
|
2022-07-18 18:47:31 -04:00
|
|
|
for MPlaceTy<'tcx, M::Provenance>
|
2020-03-16 15:12:42 -07:00
|
|
|
{
|
2018-10-31 19:52:10 +01:00
|
|
|
#[inline(always)]
|
2020-03-04 14:50:21 +00:00
|
|
|
fn layout(&self) -> TyAndLayout<'tcx> {
|
2018-10-31 19:52:10 +01:00
|
|
|
self.layout
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2022-07-14 20:32:45 -04:00
|
|
|
fn to_op_for_read(
|
2021-02-15 00:00:00 +00:00
|
|
|
&self,
|
2019-06-27 11:36:01 +02:00
|
|
|
_ecx: &InterpCx<'mir, 'tcx, M>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2022-06-29 17:07:24 -04:00
|
|
|
Ok(self.into())
|
2018-10-31 19:52:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2022-07-18 18:47:31 -04:00
|
|
|
fn from_op(op: &OpTy<'tcx, M::Provenance>) -> Self {
|
2022-07-14 20:32:45 -04:00
|
|
|
// assert is justified because our `to_op_for_read` only ever produces `Indirect` operands.
|
2022-07-04 08:48:05 -04:00
|
|
|
op.assert_mem_place()
|
2018-10-31 19:52:10 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-01 13:53:21 +01:00
|
|
|
#[inline(always)]
|
|
|
|
|
fn project_downcast(
|
2021-02-15 00:00:00 +00:00
|
|
|
&self,
|
2019-06-27 11:36:01 +02:00
|
|
|
ecx: &InterpCx<'mir, 'tcx, M>,
|
2018-11-07 16:45:07 +01:00
|
|
|
variant: VariantIdx,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, Self> {
|
2021-02-15 00:00:00 +00:00
|
|
|
ecx.mplace_downcast(self, variant)
|
2018-11-01 13:53:21 +01:00
|
|
|
}
|
|
|
|
|
|
2018-10-31 19:52:10 +01:00
|
|
|
#[inline(always)]
|
2020-03-21 17:17:01 +01:00
|
|
|
fn project_field(
|
2021-02-15 00:00:00 +00:00
|
|
|
&self,
|
2020-03-21 17:17:01 +01:00
|
|
|
ecx: &InterpCx<'mir, 'tcx, M>,
|
|
|
|
|
field: usize,
|
|
|
|
|
) -> InterpResult<'tcx, Self> {
|
2021-02-15 00:00:00 +00:00
|
|
|
ecx.mplace_field(self, field)
|
2018-10-31 19:52:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-10-31 18:44:00 +01:00
|
|
|
|
2022-07-14 20:32:45 -04:00
|
|
|
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueMut<'mir, 'tcx, M>
|
2022-07-18 18:47:31 -04:00
|
|
|
for MPlaceTy<'tcx, M::Provenance>
|
2022-07-14 20:32:45 -04:00
|
|
|
{
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn layout(&self) -> TyAndLayout<'tcx> {
|
|
|
|
|
self.layout
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn to_op_for_read(
|
|
|
|
|
&self,
|
|
|
|
|
_ecx: &InterpCx<'mir, 'tcx, M>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2022-07-14 20:32:45 -04:00
|
|
|
Ok(self.into())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn to_op_for_proj(
|
|
|
|
|
&self,
|
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, M>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2022-07-14 20:32:45 -04:00
|
|
|
Ok(self.into())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2022-07-18 18:47:31 -04:00
|
|
|
fn from_op(op: &OpTy<'tcx, M::Provenance>) -> Self {
|
2022-07-14 20:32:45 -04:00
|
|
|
// assert is justified because our `to_op_for_proj` only ever produces `Indirect` operands.
|
|
|
|
|
op.assert_mem_place()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn project_downcast(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, M>,
|
|
|
|
|
variant: VariantIdx,
|
|
|
|
|
) -> InterpResult<'tcx, Self> {
|
|
|
|
|
ecx.mplace_downcast(self, variant)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn project_field(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, M>,
|
|
|
|
|
field: usize,
|
|
|
|
|
) -> InterpResult<'tcx, Self> {
|
|
|
|
|
ecx.mplace_field(self, field)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueMut<'mir, 'tcx, M>
|
2022-07-18 18:47:31 -04:00
|
|
|
for PlaceTy<'tcx, M::Provenance>
|
2022-07-14 20:32:45 -04:00
|
|
|
{
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn layout(&self) -> TyAndLayout<'tcx> {
|
|
|
|
|
self.layout
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn to_op_for_read(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &InterpCx<'mir, 'tcx, M>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2022-07-14 20:32:45 -04:00
|
|
|
// We `force_allocation` here so that `from_op` below can work.
|
|
|
|
|
ecx.place_to_op(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn to_op_for_proj(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, M>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2022-07-14 20:32:45 -04:00
|
|
|
// We `force_allocation` here so that `from_op` below can work.
|
|
|
|
|
Ok(ecx.force_allocation(self)?.into())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2022-07-18 18:47:31 -04:00
|
|
|
fn from_op(op: &OpTy<'tcx, M::Provenance>) -> Self {
|
2022-07-14 20:32:45 -04:00
|
|
|
// assert is justified because our `to_op` only ever produces `Indirect` operands.
|
|
|
|
|
op.assert_mem_place().into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn project_downcast(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, M>,
|
|
|
|
|
variant: VariantIdx,
|
|
|
|
|
) -> InterpResult<'tcx, Self> {
|
|
|
|
|
ecx.place_downcast(self, variant)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn project_field(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, M>,
|
|
|
|
|
field: usize,
|
|
|
|
|
) -> InterpResult<'tcx, Self> {
|
|
|
|
|
ecx.place_field(self, field)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-02 14:06:43 +01:00
|
|
|
macro_rules! make_value_visitor {
|
2022-07-14 20:32:45 -04:00
|
|
|
($visitor_trait:ident, $value_trait:ident, $($mutability:ident)?) => {
|
2022-11-27 11:15:06 +00:00
|
|
|
/// How to traverse a value and what to do when we are at the leaves.
|
2022-07-14 20:32:45 -04:00
|
|
|
pub trait $visitor_trait<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>>: Sized {
|
|
|
|
|
type V: $value_trait<'mir, 'tcx, M>;
|
2018-10-31 18:44:00 +01:00
|
|
|
|
2019-06-27 11:36:01 +02:00
|
|
|
/// The visitor must have an `InterpCx` in it.
|
2019-02-10 15:16:25 +01:00
|
|
|
fn ecx(&$($mutability)? self)
|
2019-06-27 11:36:01 +02:00
|
|
|
-> &$($mutability)? InterpCx<'mir, 'tcx, M>;
|
2018-10-31 18:44:00 +01:00
|
|
|
|
2020-07-05 13:40:27 +02:00
|
|
|
/// `read_discriminant` can be hooked for better error messages.
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn read_discriminant(
|
|
|
|
|
&mut self,
|
2022-07-18 18:47:31 -04:00
|
|
|
op: &OpTy<'tcx, M::Provenance>,
|
2020-07-05 13:40:27 +02:00
|
|
|
) -> InterpResult<'tcx, VariantIdx> {
|
|
|
|
|
Ok(self.ecx().read_discriminant(op)?.1)
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-02 14:06:43 +01:00
|
|
|
// Recursive actions, ready to be overloaded.
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Visits the given value, dispatching as appropriate to more specialized visitors.
|
2018-11-02 14:06:43 +01:00
|
|
|
#[inline(always)]
|
2021-02-15 00:00:00 +00:00
|
|
|
fn visit_value(&mut self, v: &Self::V) -> InterpResult<'tcx>
|
2018-11-02 14:06:43 +01:00
|
|
|
{
|
|
|
|
|
self.walk_value(v)
|
|
|
|
|
}
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Visits the given value as a union. No automatic recursion can happen here.
|
2018-11-02 14:06:43 +01:00
|
|
|
#[inline(always)]
|
2021-02-15 00:00:00 +00:00
|
|
|
fn visit_union(&mut self, _v: &Self::V, _fields: NonZeroUsize) -> InterpResult<'tcx>
|
2018-11-02 14:06:43 +01:00
|
|
|
{
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2022-07-03 22:55:25 -04:00
|
|
|
/// Visits the given value as the pointer of a `Box`. There is nothing to recurse into.
|
|
|
|
|
/// The type of `v` will be a raw pointer, but this is a field of `Box<T>` and the
|
|
|
|
|
/// pointee type is the actual `T`.
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn visit_box(&mut self, _v: &Self::V) -> InterpResult<'tcx>
|
|
|
|
|
{
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2019-05-04 13:14:56 +02:00
|
|
|
/// Visits this value as an aggregate, you are getting an iterator yielding
|
2019-06-07 18:56:27 +02:00
|
|
|
/// all the fields (still in an `InterpResult`, you have to do error handling yourself).
|
2018-11-02 14:06:43 +01:00
|
|
|
/// Recurses into the fields.
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn visit_aggregate(
|
|
|
|
|
&mut self,
|
2021-02-15 00:00:00 +00:00
|
|
|
v: &Self::V,
|
2019-06-07 18:56:27 +02:00
|
|
|
fields: impl Iterator<Item=InterpResult<'tcx, Self::V>>,
|
|
|
|
|
) -> InterpResult<'tcx> {
|
2018-11-02 14:06:43 +01:00
|
|
|
self.walk_aggregate(v, fields)
|
|
|
|
|
}
|
2018-11-20 16:19:24 +01:00
|
|
|
|
|
|
|
|
/// Called each time we recurse down to a field of a "product-like" aggregate
|
2019-05-04 13:14:56 +02:00
|
|
|
/// (structs, tuples, arrays and the like, but not enums), passing in old (outer)
|
|
|
|
|
/// and new (inner) value.
|
2018-11-02 14:06:43 +01:00
|
|
|
/// This gives the visitor the chance to track the stack of nested fields that
|
|
|
|
|
/// we are descending through.
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn visit_field(
|
|
|
|
|
&mut self,
|
2021-02-15 00:00:00 +00:00
|
|
|
_old_val: &Self::V,
|
2018-11-02 14:06:43 +01:00
|
|
|
_field: usize,
|
2021-02-15 00:00:00 +00:00
|
|
|
new_val: &Self::V,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-11-02 14:06:43 +01:00
|
|
|
self.visit_value(new_val)
|
|
|
|
|
}
|
2018-11-20 16:19:24 +01:00
|
|
|
/// Called when recursing into an enum variant.
|
2020-02-17 22:59:16 +01:00
|
|
|
/// This gives the visitor the chance to track the stack of nested fields that
|
|
|
|
|
/// we are descending through.
|
2018-11-07 16:45:07 +01:00
|
|
|
#[inline(always)]
|
|
|
|
|
fn visit_variant(
|
|
|
|
|
&mut self,
|
2021-02-15 00:00:00 +00:00
|
|
|
_old_val: &Self::V,
|
2018-11-07 16:45:07 +01:00
|
|
|
_variant: VariantIdx,
|
2021-02-15 00:00:00 +00:00
|
|
|
new_val: &Self::V,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-11-07 16:45:07 +01:00
|
|
|
self.visit_value(new_val)
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-02 14:06:43 +01:00
|
|
|
// Default recursors. Not meant to be overloaded.
|
|
|
|
|
fn walk_aggregate(
|
|
|
|
|
&mut self,
|
2021-02-15 00:00:00 +00:00
|
|
|
v: &Self::V,
|
2019-06-07 18:56:27 +02:00
|
|
|
fields: impl Iterator<Item=InterpResult<'tcx, Self::V>>,
|
|
|
|
|
) -> InterpResult<'tcx> {
|
2018-11-02 14:06:43 +01:00
|
|
|
// Now iterate over it.
|
|
|
|
|
for (idx, field_val) in fields.enumerate() {
|
2021-02-15 00:00:00 +00:00
|
|
|
self.visit_field(v, idx, &field_val?)?;
|
2018-11-02 14:06:43 +01:00
|
|
|
}
|
|
|
|
|
Ok(())
|
2018-10-31 16:46:33 +01:00
|
|
|
}
|
2021-02-15 00:00:00 +00:00
|
|
|
fn walk_value(&mut self, v: &Self::V) -> InterpResult<'tcx>
|
2018-11-02 14:06:43 +01:00
|
|
|
{
|
2022-07-14 20:32:45 -04:00
|
|
|
let ty = v.layout().ty;
|
|
|
|
|
trace!("walk_value: type: {ty}");
|
2018-10-31 16:46:33 +01:00
|
|
|
|
2020-02-17 22:59:16 +01:00
|
|
|
// Special treatment for special types, where the (static) layout is not sufficient.
|
2022-07-14 20:32:45 -04:00
|
|
|
match *ty.kind() {
|
2020-02-17 22:59:16 +01:00
|
|
|
// If it is a trait object, switch to the real type that was used to create it.
|
2018-11-02 14:06:43 +01:00
|
|
|
ty::Dynamic(..) => {
|
2022-07-04 08:48:05 -04:00
|
|
|
// unsized values are never immediate, so we can assert_mem_place
|
2022-07-14 20:32:45 -04:00
|
|
|
let op = v.to_op_for_read(self.ecx())?;
|
2021-07-12 18:22:15 +02:00
|
|
|
let dest = op.assert_mem_place();
|
2022-07-17 16:02:49 -04:00
|
|
|
let inner_mplace = self.ecx().unpack_dyn_trait(&dest)?;
|
2022-07-14 20:32:45 -04:00
|
|
|
trace!("walk_value: dyn object layout: {:#?}", inner_mplace.layout);
|
2018-11-02 14:06:43 +01:00
|
|
|
// recurse with the inner type
|
2022-07-14 20:32:45 -04:00
|
|
|
return self.visit_field(&v, 0, &$value_trait::from_op(&inner_mplace.into()));
|
2018-11-02 14:06:43 +01:00
|
|
|
},
|
2020-02-17 22:59:16 +01:00
|
|
|
// Slices do not need special handling here: they have `Array` field
|
|
|
|
|
// placement with length 0, so we enter the `Array` case below which
|
|
|
|
|
// indirectly uses the metadata to determine the actual length.
|
2022-07-03 22:55:25 -04:00
|
|
|
|
|
|
|
|
// However, `Box`... let's talk about `Box`.
|
|
|
|
|
ty::Adt(def, ..) if def.is_box() => {
|
|
|
|
|
// `Box` is a hybrid primitive-library-defined type that one the one hand is
|
|
|
|
|
// a dereferenceable pointer, on the other hand has *basically arbitrary
|
|
|
|
|
// user-defined layout* since the user controls the 'allocator' field. So it
|
|
|
|
|
// cannot be treated like a normal pointer, since it does not fit into an
|
|
|
|
|
// `Immediate`. Yeah, it is quite terrible. But many visitors want to do
|
|
|
|
|
// something with "all boxed pointers", so we handle this mess for them.
|
|
|
|
|
//
|
|
|
|
|
// When we hit a `Box`, we do not do the usual `visit_aggregate`; instead,
|
|
|
|
|
// we (a) call `visit_box` on the pointer value, and (b) recurse on the
|
|
|
|
|
// allocator field. We also assert tons of things to ensure we do not miss
|
|
|
|
|
// any other fields.
|
|
|
|
|
|
|
|
|
|
// `Box` has two fields: the pointer we care about, and the allocator.
|
|
|
|
|
assert_eq!(v.layout().fields.count(), 2, "`Box` must have exactly 2 fields");
|
|
|
|
|
let (unique_ptr, alloc) =
|
|
|
|
|
(v.project_field(self.ecx(), 0)?, v.project_field(self.ecx(), 1)?);
|
|
|
|
|
// Unfortunately there is some type junk in the way here: `unique_ptr` is a `Unique`...
|
|
|
|
|
// (which means another 2 fields, the second of which is a `PhantomData`)
|
|
|
|
|
assert_eq!(unique_ptr.layout().fields.count(), 2);
|
|
|
|
|
let (nonnull_ptr, phantom) = (
|
|
|
|
|
unique_ptr.project_field(self.ecx(), 0)?,
|
|
|
|
|
unique_ptr.project_field(self.ecx(), 1)?,
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
phantom.layout().ty.ty_adt_def().is_some_and(|adt| adt.is_phantom_data()),
|
|
|
|
|
"2nd field of `Unique` should be PhantomData but is {:?}",
|
|
|
|
|
phantom.layout().ty,
|
|
|
|
|
);
|
|
|
|
|
// ... that contains a `NonNull`... (gladly, only a single field here)
|
|
|
|
|
assert_eq!(nonnull_ptr.layout().fields.count(), 1);
|
|
|
|
|
let raw_ptr = nonnull_ptr.project_field(self.ecx(), 0)?; // the actual raw ptr
|
|
|
|
|
// ... whose only field finally is a raw ptr we can dereference.
|
|
|
|
|
self.visit_box(&raw_ptr)?;
|
|
|
|
|
|
|
|
|
|
// The second `Box` field is the allocator, which we recursively check for validity
|
|
|
|
|
// like in regular structs.
|
|
|
|
|
self.visit_field(v, 1, &alloc)?;
|
2022-07-17 10:47:34 -04:00
|
|
|
|
|
|
|
|
// We visited all parts of this one.
|
|
|
|
|
return Ok(());
|
2022-07-03 22:55:25 -04:00
|
|
|
}
|
2018-11-02 14:06:43 +01:00
|
|
|
_ => {},
|
|
|
|
|
};
|
2018-10-31 16:46:33 +01:00
|
|
|
|
2020-02-17 22:59:16 +01:00
|
|
|
// Visit the fields of this value.
|
2018-11-02 14:06:43 +01:00
|
|
|
match v.layout().fields {
|
2022-07-14 20:32:45 -04:00
|
|
|
FieldsShape::Primitive => {}
|
2020-03-31 18:16:47 +02:00
|
|
|
FieldsShape::Union(fields) => {
|
2020-02-17 22:59:16 +01:00
|
|
|
self.visit_union(v, fields)?;
|
2022-07-14 20:32:45 -04:00
|
|
|
}
|
2020-03-31 18:16:47 +02:00
|
|
|
FieldsShape::Arbitrary { ref offsets, .. } => {
|
2019-05-04 13:14:56 +02:00
|
|
|
// FIXME: We collect in a vec because otherwise there are lifetime
|
|
|
|
|
// errors: Projecting to a field needs access to `ecx`.
|
2019-06-07 18:56:27 +02:00
|
|
|
let fields: Vec<InterpResult<'tcx, Self::V>> =
|
2019-05-04 13:14:56 +02:00
|
|
|
(0..offsets.len()).map(|i| {
|
2020-03-21 17:17:01 +01:00
|
|
|
v.project_field(self.ecx(), i)
|
2019-05-04 13:14:56 +02:00
|
|
|
})
|
|
|
|
|
.collect();
|
2020-02-17 22:59:16 +01:00
|
|
|
self.visit_aggregate(v, fields.into_iter())?;
|
2022-07-14 20:32:45 -04:00
|
|
|
}
|
2020-03-31 18:16:47 +02:00
|
|
|
FieldsShape::Array { .. } => {
|
2022-07-14 20:32:45 -04:00
|
|
|
// Let's get an mplace (or immediate) first.
|
|
|
|
|
// This might `force_allocate` if `v` is a `PlaceTy`, but `place_index` does that anyway.
|
|
|
|
|
let op = v.to_op_for_proj(self.ecx())?;
|
2018-11-02 14:06:43 +01:00
|
|
|
// Now we can go over all the fields.
|
2020-02-17 22:59:16 +01:00
|
|
|
// This uses the *run-time length*, i.e., if we are a slice,
|
|
|
|
|
// the dynamic info from the metadata is used.
|
2022-07-04 08:48:05 -04:00
|
|
|
let iter = self.ecx().operand_array_fields(&op)?
|
2018-11-02 14:06:43 +01:00
|
|
|
.map(|f| f.and_then(|f| {
|
2022-07-14 20:32:45 -04:00
|
|
|
Ok($value_trait::from_op(&f))
|
2018-11-02 14:06:43 +01:00
|
|
|
}));
|
2020-02-17 22:59:16 +01:00
|
|
|
self.visit_aggregate(v, iter)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match v.layout().variants {
|
|
|
|
|
// If this is a multi-variant layout, find the right variant and proceed
|
|
|
|
|
// with *its* fields.
|
2020-03-31 18:16:47 +02:00
|
|
|
Variants::Multiple { .. } => {
|
2022-07-14 20:32:45 -04:00
|
|
|
let op = v.to_op_for_read(self.ecx())?;
|
2021-02-15 00:00:00 +00:00
|
|
|
let idx = self.read_discriminant(&op)?;
|
2020-02-17 22:59:16 +01:00
|
|
|
let inner = v.project_downcast(self.ecx(), idx)?;
|
|
|
|
|
trace!("walk_value: variant layout: {:#?}", inner.layout());
|
|
|
|
|
// recurse with the inner type
|
2021-02-15 00:00:00 +00:00
|
|
|
self.visit_variant(v, idx, &inner)
|
2018-11-02 14:06:43 +01:00
|
|
|
}
|
2020-02-17 22:59:16 +01:00
|
|
|
// For single-variant layouts, we already did anything there is to do.
|
2020-03-31 18:16:47 +02:00
|
|
|
Variants::Single { .. } => Ok(())
|
2018-11-02 14:06:43 +01:00
|
|
|
}
|
2018-10-31 16:46:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-02 14:06:43 +01:00
|
|
|
|
2022-07-14 20:32:45 -04:00
|
|
|
make_value_visitor!(ValueVisitor, Value,);
|
|
|
|
|
make_value_visitor!(MutValueVisitor, ValueMut, mut);
|