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 // Recursive actions, ready to be overloaded.
129 /// Visits the given value, dispatching as appropriate to more specialized visitors.
131 fn visit_value(&mut self, v: Self::V) -> InterpResult<'tcx>
135 /// Visits the given value as a union. No automatic recursion can happen here.
137 fn visit_union(&mut self, _v: Self::V, _fields: NonZeroUsize) -> InterpResult<'tcx>
141 /// Visits this value as an aggregate, you are getting an iterator yielding
142 /// all the fields (still in an `InterpResult`, you have to do error handling yourself).
143 /// Recurses into the fields.
148 fields: impl Iterator<Item=InterpResult<'tcx, Self::V>>,
149 ) -> InterpResult<'tcx> {
150 self.walk_aggregate(v, fields)
153 /// Called each time we recurse down to a field of a "product-like" aggregate
154 /// (structs, tuples, arrays and the like, but not enums), passing in old (outer)
155 /// and new (inner) value.
156 /// This gives the visitor the chance to track the stack of nested fields that
157 /// we are descending through.
164 ) -> InterpResult<'tcx> {
165 self.visit_value(new_val)
167 /// Called when recursing into an enum variant.
168 /// This gives the visitor the chance to track the stack of nested fields that
169 /// we are descending through.
174 _variant: VariantIdx,
176 ) -> InterpResult<'tcx> {
177 self.visit_value(new_val)
180 // Default recursors. Not meant to be overloaded.
184 fields: impl Iterator<Item=InterpResult<'tcx, Self::V>>,
185 ) -> InterpResult<'tcx> {
186 // Now iterate over it.
187 for (idx, field_val) in fields.enumerate() {
188 self.visit_field(v, idx, field_val?)?;
192 fn walk_value(&mut self, v: Self::V) -> InterpResult<'tcx>
194 trace!("walk_value: type: {}", v.layout().ty);
196 // Special treatment for special types, where the (static) layout is not sufficient.
197 match v.layout().ty.kind {
198 // If it is a trait object, switch to the real type that was used to create it.
200 // immediate trait objects are not a thing
201 let dest = v.to_op(self.ecx())?.assert_mem_place(self.ecx());
202 let inner = self.ecx().unpack_dyn_trait(dest)?.1;
203 trace!("walk_value: dyn object layout: {:#?}", inner.layout);
204 // recurse with the inner type
205 return self.visit_field(v, 0, Value::from_mem_place(inner));
207 // Slices do not need special handling here: they have `Array` field
208 // placement with length 0, so we enter the `Array` case below which
209 // indirectly uses the metadata to determine the actual length.
213 // Visit the fields of this value.
214 match v.layout().fields {
215 FieldsShape::Primitive => {},
216 FieldsShape::Union(fields) => {
217 self.visit_union(v, fields)?;
219 FieldsShape::Arbitrary { ref offsets, .. } => {
220 // FIXME: We collect in a vec because otherwise there are lifetime
221 // errors: Projecting to a field needs access to `ecx`.
222 let fields: Vec<InterpResult<'tcx, Self::V>> =
223 (0..offsets.len()).map(|i| {
224 v.project_field(self.ecx(), i)
227 self.visit_aggregate(v, fields.into_iter())?;
229 FieldsShape::Array { .. } => {
230 // Let's get an mplace first.
231 let mplace = v.to_op(self.ecx())?.assert_mem_place(self.ecx());
232 // Now we can go over all the fields.
233 // This uses the *run-time length*, i.e., if we are a slice,
234 // the dynamic info from the metadata is used.
235 let iter = self.ecx().mplace_array_fields(mplace)?
236 .map(|f| f.and_then(|f| {
237 Ok(Value::from_mem_place(f))
239 self.visit_aggregate(v, iter)?;
243 match v.layout().variants {
244 // If this is a multi-variant layout, find the right variant and proceed
245 // with *its* fields.
246 Variants::Multiple { .. } => {
247 let op = v.to_op(self.ecx())?;
248 let idx = self.ecx().read_discriminant(op)?.1;
249 let inner = v.project_downcast(self.ecx(), idx)?;
250 trace!("walk_value: variant layout: {:#?}", inner.layout());
251 // recurse with the inner type
252 self.visit_variant(v, idx, inner)
254 // For single-variant layouts, we already did anything there is to do.
255 Variants::Single { .. } => Ok(())
262 make_value_visitor!(ValueVisitor,);
263 make_value_visitor!(MutValueVisitor, mut);