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::mir::interpret::InterpResult;
6 use rustc::ty::layout::{self, TyLayout, VariantIdx};
8 use super::{InterpCx, MPlaceTy, Machine, OpTy};
10 // A thing that we can project into, and that has a layout.
11 // This wouldn't have to depend on `Machine` but with the current type inference,
12 // that's just more convenient to work with (avoids repeating all the `Machine` bounds).
13 pub trait Value<'mir, 'tcx, M: Machine<'mir, 'tcx>>: Copy {
14 /// Gets this value's layout.
15 fn layout(&self) -> TyLayout<'tcx>;
17 /// Makes this into an `OpTy`.
18 fn to_op(self, ecx: &InterpCx<'mir, 'tcx, M>) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>>;
20 /// Creates this from an `MPlaceTy`.
21 fn from_mem_place(mplace: MPlaceTy<'tcx, M::PointerTag>) -> Self;
23 /// Projects to the given enum variant.
26 ecx: &InterpCx<'mir, 'tcx, M>,
28 ) -> InterpResult<'tcx, Self>;
30 /// Projects to the n-th field.
31 fn project_field(self, ecx: &InterpCx<'mir, 'tcx, M>, field: u64) -> InterpResult<'tcx, Self>;
34 // Operands and memory-places are both values.
35 // Places in general are not due to `place_field` having to do `force_allocation`.
36 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Value<'mir, 'tcx, M> for OpTy<'tcx, M::PointerTag> {
38 fn layout(&self) -> TyLayout<'tcx> {
45 _ecx: &InterpCx<'mir, 'tcx, M>,
46 ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
51 fn from_mem_place(mplace: MPlaceTy<'tcx, M::PointerTag>) -> Self {
58 ecx: &InterpCx<'mir, 'tcx, M>,
60 ) -> InterpResult<'tcx, Self> {
61 ecx.operand_downcast(self, variant)
65 fn project_field(self, ecx: &InterpCx<'mir, 'tcx, M>, field: u64) -> InterpResult<'tcx, Self> {
66 ecx.operand_field(self, field)
70 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Value<'mir, 'tcx, M> for MPlaceTy<'tcx, M::PointerTag> {
72 fn layout(&self) -> TyLayout<'tcx> {
79 _ecx: &InterpCx<'mir, 'tcx, M>,
80 ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
85 fn from_mem_place(mplace: MPlaceTy<'tcx, M::PointerTag>) -> Self {
92 ecx: &InterpCx<'mir, 'tcx, M>,
94 ) -> InterpResult<'tcx, Self> {
95 ecx.mplace_downcast(self, variant)
99 fn project_field(self, ecx: &InterpCx<'mir, 'tcx, M>, field: u64) -> InterpResult<'tcx, Self> {
100 ecx.mplace_field(self, field)
104 macro_rules! make_value_visitor {
105 ($visitor_trait_name:ident, $($mutability:ident)?) => {
106 // How to traverse a value and what to do when we are at the leaves.
107 pub trait $visitor_trait_name<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>>: Sized {
108 type V: Value<'mir, 'tcx, M>;
110 /// The visitor must have an `InterpCx` in it.
111 fn ecx(&$($mutability)? self)
112 -> &$($mutability)? InterpCx<'mir, 'tcx, M>;
114 // Recursive actions, ready to be overloaded.
115 /// Visits the given value, dispatching as appropriate to more specialized visitors.
117 fn visit_value(&mut self, v: Self::V) -> InterpResult<'tcx>
121 /// Visits the given value as a union. No automatic recursion can happen here.
123 fn visit_union(&mut self, _v: Self::V, _fields: usize) -> InterpResult<'tcx>
127 /// Visits this value as an aggregate, you are getting an iterator yielding
128 /// all the fields (still in an `InterpResult`, you have to do error handling yourself).
129 /// Recurses into the fields.
134 fields: impl Iterator<Item=InterpResult<'tcx, Self::V>>,
135 ) -> InterpResult<'tcx> {
136 self.walk_aggregate(v, fields)
139 /// Called each time we recurse down to a field of a "product-like" aggregate
140 /// (structs, tuples, arrays and the like, but not enums), passing in old (outer)
141 /// and new (inner) value.
142 /// This gives the visitor the chance to track the stack of nested fields that
143 /// we are descending through.
150 ) -> InterpResult<'tcx> {
151 self.visit_value(new_val)
153 /// Called when recursing into an enum variant.
154 /// This gives the visitor the chance to track the stack of nested fields that
155 /// we are descending through.
160 _variant: VariantIdx,
162 ) -> InterpResult<'tcx> {
163 self.visit_value(new_val)
166 // Default recursors. Not meant to be overloaded.
170 fields: impl Iterator<Item=InterpResult<'tcx, Self::V>>,
171 ) -> InterpResult<'tcx> {
172 // Now iterate over it.
173 for (idx, field_val) in fields.enumerate() {
174 self.visit_field(v, idx, field_val?)?;
178 fn walk_value(&mut self, v: Self::V) -> InterpResult<'tcx>
180 trace!("walk_value: type: {}", v.layout().ty);
182 // Special treatment for special types, where the (static) layout is not sufficient.
183 match v.layout().ty.kind {
184 // If it is a trait object, switch to the real type that was used to create it.
186 // immediate trait objects are not a thing
187 let dest = v.to_op(self.ecx())?.assert_mem_place(self.ecx());
188 let inner = self.ecx().unpack_dyn_trait(dest)?.1;
189 trace!("walk_value: dyn object layout: {:#?}", inner.layout);
190 // recurse with the inner type
191 return self.visit_field(v, 0, Value::from_mem_place(inner));
193 // Slices do not need special handling here: they have `Array` field
194 // placement with length 0, so we enter the `Array` case below which
195 // indirectly uses the metadata to determine the actual length.
199 // Visit the fields of this value.
200 match v.layout().fields {
201 layout::FieldPlacement::Union(fields) => {
202 self.visit_union(v, fields)?;
204 layout::FieldPlacement::Arbitrary { ref offsets, .. } => {
205 // FIXME: We collect in a vec because otherwise there are lifetime
206 // errors: Projecting to a field needs access to `ecx`.
207 let fields: Vec<InterpResult<'tcx, Self::V>> =
208 (0..offsets.len()).map(|i| {
209 v.project_field(self.ecx(), i as u64)
212 self.visit_aggregate(v, fields.into_iter())?;
214 layout::FieldPlacement::Array { .. } => {
215 // Let's get an mplace first.
216 let mplace = v.to_op(self.ecx())?.assert_mem_place(self.ecx());
217 // Now we can go over all the fields.
218 // This uses the *run-time length*, i.e., if we are a slice,
219 // the dynamic info from the metadata is used.
220 let iter = self.ecx().mplace_array_fields(mplace)?
221 .map(|f| f.and_then(|f| {
222 Ok(Value::from_mem_place(f))
224 self.visit_aggregate(v, iter)?;
228 match v.layout().variants {
229 // If this is a multi-variant layout, find the right variant and proceed
230 // with *its* fields.
231 layout::Variants::Multiple { .. } => {
232 let op = v.to_op(self.ecx())?;
233 let idx = self.ecx().read_discriminant(op)?.1;
234 let inner = v.project_downcast(self.ecx(), idx)?;
235 trace!("walk_value: variant layout: {:#?}", inner.layout());
236 // recurse with the inner type
237 self.visit_variant(v, idx, inner)
239 // For single-variant layouts, we already did anything there is to do.
240 layout::Variants::Single { .. } => Ok(())
247 make_value_visitor!(ValueVisitor,);
248 make_value_visitor!(MutValueVisitor, mut);