279 lines
10 KiB
Rust
Raw Normal View History

//! 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;
use rustc_middle::ty::layout::TyAndLayout;
use rustc_target::abi::{FieldsShape, VariantIdx, Variants};
use std::num::NonZeroUsize;
2019-12-22 17:42:04 -05:00
use super::{InterpCx, MPlaceTy, Machine, OpTy};
// 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).
pub trait Value<'mir, 'tcx, M: Machine<'mir, 'tcx>>: Copy {
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>;
2019-02-08 14:53:55 +01:00
/// Makes this into an `OpTy`.
2021-02-15 00:00:00 +00:00
fn to_op(&self, ecx: &InterpCx<'mir, 'tcx, M>)
-> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>>;
2019-02-08 14:53:55 +01:00
/// Creates this from an `MPlaceTy`.
2019-02-08 06:28:15 +09:00
fn from_mem_place(mplace: MPlaceTy<'tcx, M::PointerTag>) -> Self;
2019-02-08 14:53:55 +01:00
/// Projects to the given enum variant.
fn project_downcast(
2021-02-15 00:00:00 +00:00
&self,
ecx: &InterpCx<'mir, 'tcx, M>,
variant: VariantIdx,
) -> InterpResult<'tcx, Self>;
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-11-02 14:06:43 +01:00
// Operands and memory-places are both values.
// Places in general are not due to `place_field` having to do `force_allocation`.
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> Value<'mir, 'tcx, M> for OpTy<'tcx, M::PointerTag> {
#[inline(always)]
2020-03-04 14:50:21 +00:00
fn layout(&self) -> TyAndLayout<'tcx> {
self.layout
}
#[inline(always)]
2018-11-02 09:52:17 +01:00
fn to_op(
2021-02-15 00:00:00 +00:00
&self,
_ecx: &InterpCx<'mir, 'tcx, M>,
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
2021-02-15 00:00:00 +00:00
Ok(*self)
}
#[inline(always)]
fn from_mem_place(mplace: MPlaceTy<'tcx, M::PointerTag>) -> Self {
mplace.into()
}
#[inline(always)]
fn project_downcast(
2021-02-15 00:00:00 +00:00
&self,
ecx: &InterpCx<'mir, 'tcx, M>,
variant: VariantIdx,
) -> InterpResult<'tcx, Self> {
2018-11-02 09:52:17 +01:00
ecx.operand_downcast(self, variant)
}
#[inline(always)]
fn project_field(
2021-02-15 00:00:00 +00:00
&self,
ecx: &InterpCx<'mir, 'tcx, M>,
field: usize,
) -> InterpResult<'tcx, Self> {
2018-11-02 09:52:17 +01:00
ecx.operand_field(self, field)
}
}
2019-02-05 20:44:59 +01:00
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> Value<'mir, 'tcx, M>
for MPlaceTy<'tcx, M::PointerTag>
{
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)]
2018-11-02 09:52:17 +01:00
fn to_op(
2021-02-15 00:00:00 +00:00
&self,
_ecx: &InterpCx<'mir, 'tcx, M>,
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
2021-02-15 00:00:00 +00:00
Ok((*self).into())
2018-10-31 19:52:10 +01:00
}
#[inline(always)]
fn from_mem_place(mplace: MPlaceTy<'tcx, M::PointerTag>) -> Self {
mplace
}
#[inline(always)]
fn project_downcast(
2021-02-15 00:00:00 +00:00
&self,
ecx: &InterpCx<'mir, 'tcx, M>,
variant: VariantIdx,
) -> InterpResult<'tcx, Self> {
2021-02-15 00:00:00 +00:00
ecx.mplace_downcast(self, variant)
}
2018-10-31 19:52:10 +01:00
#[inline(always)]
fn project_field(
2021-02-15 00:00:00 +00:00
&self,
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-11-02 14:06:43 +01:00
macro_rules! make_value_visitor {
2019-02-10 15:16:25 +01:00
($visitor_trait_name:ident, $($mutability:ident)?) => {
2018-11-02 14:06:43 +01:00
// How to traverse a value and what to do when we are at the leaves.
pub trait $visitor_trait_name<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>>: Sized {
type V: Value<'mir, 'tcx, M>;
/// The visitor must have an `InterpCx` in it.
2019-02-10 15:16:25 +01:00
fn ecx(&$($mutability)? self)
-> &$($mutability)? InterpCx<'mir, 'tcx, M>;
/// `read_discriminant` can be hooked for better error messages.
#[inline(always)]
fn read_discriminant(
&mut self,
2021-02-15 00:00:00 +00:00
op: &OpTy<'tcx, M::PointerTag>,
) -> 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(())
}
2019-05-04 13:14:56 +02:00
/// Visits this value as an aggregate, you are getting an iterator yielding
/// 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,
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,
) -> 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.
/// This gives the visitor the chance to track the stack of nested fields that
/// we are descending through.
#[inline(always)]
fn visit_variant(
&mut self,
2021-02-15 00:00:00 +00:00
_old_val: &Self::V,
_variant: VariantIdx,
2021-02-15 00:00:00 +00:00
new_val: &Self::V,
) -> InterpResult<'tcx> {
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,
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(())
}
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
{
2018-11-05 14:34:43 +01:00
trace!("walk_value: type: {}", v.layout().ty);
// Special treatment for special types, where the (static) layout is not sufficient.
2020-08-03 00:49:11 +02:00
match *v.layout().ty.kind() {
// 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(..) => {
// immediate trait objects are not a thing
let op = v.to_op(self.ecx())?;
let dest = op.assert_mem_place();
2021-02-15 00:00:00 +00:00
let inner = self.ecx().unpack_dyn_trait(&dest)?.1;
2018-11-02 14:06:43 +01:00
trace!("walk_value: dyn object layout: {:#?}", inner.layout);
// recurse with the inner type
2021-02-15 00:00:00 +00:00
return self.visit_field(&v, 0, &Value::from_mem_place(inner));
2018-11-02 14:06:43 +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.
2018-11-02 14:06:43 +01:00
_ => {},
};
// Visit the fields of this value.
2018-11-02 14:06:43 +01:00
match v.layout().fields {
FieldsShape::Primitive => {},
FieldsShape::Union(fields) => {
self.visit_union(v, fields)?;
2018-11-02 14:06:43 +01: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`.
let fields: Vec<InterpResult<'tcx, Self::V>> =
2019-05-04 13:14:56 +02:00
(0..offsets.len()).map(|i| {
v.project_field(self.ecx(), i)
2019-05-04 13:14:56 +02:00
})
.collect();
self.visit_aggregate(v, fields.into_iter())?;
2018-11-02 14:06:43 +01:00
},
FieldsShape::Array { .. } => {
2018-11-02 14:06:43 +01:00
// Let's get an mplace first.
let op = v.to_op(self.ecx())?;
let mplace = op.assert_mem_place();
2018-11-02 14:06:43 +01:00
// Now we can go over all the fields.
// This uses the *run-time length*, i.e., if we are a slice,
// the dynamic info from the metadata is used.
2021-02-15 00:00:00 +00:00
let iter = self.ecx().mplace_array_fields(&mplace)?
2018-11-02 14:06:43 +01:00
.map(|f| f.and_then(|f| {
Ok(Value::from_mem_place(f))
}));
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.
Variants::Multiple { .. } => {
let op = v.to_op(self.ecx())?;
2021-02-15 00:00:00 +00:00
let idx = self.read_discriminant(&op)?;
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
}
// For single-variant layouts, we already did anything there is to do.
Variants::Single { .. } => Ok(())
2018-11-02 14:06:43 +01:00
}
}
}
}
}
2018-11-02 14:06:43 +01:00
make_value_visitor!(ValueVisitor,);
2019-12-22 17:42:04 -05:00
make_value_visitor!(MutValueVisitor, mut);