1 //! Visitor for a run-time value with a given layout: Traverse enums, structs and other compound
2 //! types until we arrive at the leaves, with custom handling for primitive types.
4 use rustc_middle
::mir
::interpret
::InterpResult
;
6 use rustc_middle
::ty
::layout
::TyAndLayout
;
7 use rustc_target
::abi
::{FieldsShape, VariantIdx, Variants}
;
9 use std
::num
::NonZeroUsize
;
11 use super::{InterpCx, MPlaceTy, Machine, OpTy}
;
13 // A thing that we can project into, and that has a layout.
14 // This wouldn't have to depend on `Machine` but with the current type inference,
15 // that's just more convenient to work with (avoids repeating all the `Machine` bounds).
16 pub trait Value
<'mir
, 'tcx
, M
: Machine
<'mir
, 'tcx
>>: Copy
{
17 /// Gets this value's layout.
18 fn layout(&self) -> TyAndLayout
<'tcx
>;
20 /// Makes this into an `OpTy`.
21 fn to_op(self, ecx
: &InterpCx
<'mir
, 'tcx
, M
>) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::PointerTag
>>;
23 /// Creates this from an `MPlaceTy`.
24 fn from_mem_place(mplace
: MPlaceTy
<'tcx
, M
::PointerTag
>) -> Self;
26 /// Projects to the given enum variant.
29 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
31 ) -> InterpResult
<'tcx
, Self>;
33 /// Projects to the n-th field.
34 fn project_field(self, ecx
: &InterpCx
<'mir
, 'tcx
, M
>, field
: usize)
35 -> InterpResult
<'tcx
, Self>;
38 // Operands and memory-places are both values.
39 // Places in general are not due to `place_field` having to do `force_allocation`.
40 impl<'mir
, 'tcx
: 'mir
, M
: Machine
<'mir
, 'tcx
>> Value
<'mir
, 'tcx
, M
> for OpTy
<'tcx
, M
::PointerTag
> {
42 fn layout(&self) -> TyAndLayout
<'tcx
> {
49 _ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
50 ) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::PointerTag
>> {
55 fn from_mem_place(mplace
: MPlaceTy
<'tcx
, M
::PointerTag
>) -> Self {
62 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
64 ) -> InterpResult
<'tcx
, Self> {
65 ecx
.operand_downcast(self, variant
)
71 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
73 ) -> InterpResult
<'tcx
, Self> {
74 ecx
.operand_field(self, field
)
78 impl<'mir
, 'tcx
: 'mir
, M
: Machine
<'mir
, 'tcx
>> Value
<'mir
, 'tcx
, M
>
79 for MPlaceTy
<'tcx
, M
::PointerTag
>
82 fn layout(&self) -> TyAndLayout
<'tcx
> {
89 _ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
90 ) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::PointerTag
>> {
95 fn from_mem_place(mplace
: MPlaceTy
<'tcx
, M
::PointerTag
>) -> Self {
102 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
104 ) -> InterpResult
<'tcx
, Self> {
105 ecx
.mplace_downcast(self, variant
)
111 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
113 ) -> InterpResult
<'tcx
, Self> {
114 ecx
.mplace_field(self, field
)
118 macro_rules
! make_value_visitor
{
119 ($visitor_trait_name
:ident
, $
($mutability
:ident
)?
) => {
120 // How to traverse a value and what to do when we are at the leaves.
121 pub trait $visitor_trait_name
<'mir
, 'tcx
: 'mir
, M
: Machine
<'mir
, 'tcx
>>: Sized
{
122 type V
: Value
<'mir
, 'tcx
, M
>;
124 /// The visitor must have an `InterpCx` in it.
125 fn ecx(&$
($mutability
)?
self)
126 -> &$
($mutability
)? InterpCx
<'mir
, 'tcx
, M
>;
128 /// `read_discriminant` can be hooked for better error messages.
130 fn read_discriminant(
132 op
: OpTy
<'tcx
, M
::PointerTag
>,
133 ) -> InterpResult
<'tcx
, VariantIdx
> {
134 Ok(self.ecx().read_discriminant(op
)?
.1)
137 // Recursive actions, ready to be overloaded.
138 /// Visits the given value, dispatching as appropriate to more specialized visitors.
140 fn visit_value(&mut self, v
: Self::V
) -> InterpResult
<'tcx
>
144 /// Visits the given value as a union. No automatic recursion can happen here.
146 fn visit_union(&mut self, _v
: Self::V
, _fields
: NonZeroUsize
) -> InterpResult
<'tcx
>
150 /// Visits this value as an aggregate, you are getting an iterator yielding
151 /// all the fields (still in an `InterpResult`, you have to do error handling yourself).
152 /// Recurses into the fields.
157 fields
: impl Iterator
<Item
=InterpResult
<'tcx
, Self::V
>>,
158 ) -> InterpResult
<'tcx
> {
159 self.walk_aggregate(v
, fields
)
162 /// Called each time we recurse down to a field of a "product-like" aggregate
163 /// (structs, tuples, arrays and the like, but not enums), passing in old (outer)
164 /// and new (inner) value.
165 /// This gives the visitor the chance to track the stack of nested fields that
166 /// we are descending through.
173 ) -> InterpResult
<'tcx
> {
174 self.visit_value(new_val
)
176 /// Called when recursing into an enum variant.
177 /// This gives the visitor the chance to track the stack of nested fields that
178 /// we are descending through.
183 _variant
: VariantIdx
,
185 ) -> InterpResult
<'tcx
> {
186 self.visit_value(new_val
)
189 // Default recursors. Not meant to be overloaded.
193 fields
: impl Iterator
<Item
=InterpResult
<'tcx
, Self::V
>>,
194 ) -> InterpResult
<'tcx
> {
195 // Now iterate over it.
196 for (idx
, field_val
) in fields
.enumerate() {
197 self.visit_field(v
, idx
, field_val?
)?
;
201 fn walk_value(&mut self, v
: Self::V
) -> InterpResult
<'tcx
>
203 trace
!("walk_value: type: {}", v
.layout().ty
);
205 // Special treatment for special types, where the (static) layout is not sufficient.
206 match *v
.layout().ty
.kind() {
207 // If it is a trait object, switch to the real type that was used to create it.
209 // immediate trait objects are not a thing
210 let dest
= v
.to_op(self.ecx())?
.assert_mem_place(self.ecx());
211 let inner
= self.ecx().unpack_dyn_trait(dest
)?
.1;
212 trace
!("walk_value: dyn object layout: {:#?}", inner
.layout
);
213 // recurse with the inner type
214 return self.visit_field(v
, 0, Value
::from_mem_place(inner
));
216 // Slices do not need special handling here: they have `Array` field
217 // placement with length 0, so we enter the `Array` case below which
218 // indirectly uses the metadata to determine the actual length.
222 // Visit the fields of this value.
223 match v
.layout().fields
{
224 FieldsShape
::Primitive
=> {}
,
225 FieldsShape
::Union(fields
) => {
226 self.visit_union(v
, fields
)?
;
228 FieldsShape
::Arbitrary { ref offsets, .. }
=> {
229 // FIXME: We collect in a vec because otherwise there are lifetime
230 // errors: Projecting to a field needs access to `ecx`.
231 let fields
: Vec
<InterpResult
<'tcx
, Self::V
>> =
232 (0..offsets
.len()).map(|i
| {
233 v
.project_field(self.ecx(), i
)
236 self.visit_aggregate(v
, fields
.into_iter())?
;
238 FieldsShape
::Array { .. }
=> {
239 // Let's get an mplace first.
240 let mplace
= v
.to_op(self.ecx())?
.assert_mem_place(self.ecx());
241 // Now we can go over all the fields.
242 // This uses the *run-time length*, i.e., if we are a slice,
243 // the dynamic info from the metadata is used.
244 let iter
= self.ecx().mplace_array_fields(mplace
)?
245 .map(|f
| f
.and_then(|f
| {
246 Ok(Value
::from_mem_place(f
))
248 self.visit_aggregate(v
, iter
)?
;
252 match v
.layout().variants
{
253 // If this is a multi-variant layout, find the right variant and proceed
254 // with *its* fields.
255 Variants
::Multiple { .. }
=> {
256 let op
= v
.to_op(self.ecx())?
;
257 let idx
= self.read_discriminant(op
)?
;
258 let inner
= v
.project_downcast(self.ecx(), idx
)?
;
259 trace
!("walk_value: variant layout: {:#?}", inner
.layout());
260 // recurse with the inner type
261 self.visit_variant(v
, idx
, inner
)
263 // For single-variant layouts, we already did anything there is to do.
264 Variants
::Single { .. }
=> Ok(())
271 make_value_visitor
!(ValueVisitor
,);
272 make_value_visitor
!(MutValueVisitor
, mut);