]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/value.rs
this is for tidy
[rust.git] / src / librustc / mir / interpret / value.rs
1 use std::fmt;
2 use rustc_macros::HashStable;
3
4 use crate::ty::{Ty, InferConst, ParamConst, layout::{HasDataLayout, Size}, subst::SubstsRef};
5 use crate::ty::PlaceholderConst;
6 use crate::hir::def_id::DefId;
7
8 use super::{EvalResult, Pointer, PointerArithmetic, Allocation, AllocId, sign_extend, truncate};
9
10 /// Represents the result of a raw const operation, pre-validation.
11 #[derive(Copy, Clone, Debug, Eq, PartialEq, RustcEncodable, RustcDecodable, Hash, HashStable)]
12 pub struct RawConst<'tcx> {
13     // the value lives here, at offset 0, and that allocation definitely is a `AllocKind::Memory`
14     // (so you can use `AllocMap::unwrap_memory`).
15     pub alloc_id: AllocId,
16     pub ty: Ty<'tcx>,
17 }
18
19 /// Represents a constant value in Rust. `Scalar` and `ScalarPair` are optimizations that
20 /// match the `LocalState` optimizations for easy conversions between `Value` and `ConstValue`.
21 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord,
22          RustcEncodable, RustcDecodable, Hash, HashStable)]
23 pub enum ConstValue<'tcx> {
24     /// A const generic parameter.
25     Param(ParamConst),
26
27     /// Infer the value of the const.
28     Infer(InferConst<'tcx>),
29
30     /// A placeholder const - universally quantified higher-ranked const.
31     Placeholder(PlaceholderConst),
32
33     /// Used only for types with `layout::abi::Scalar` ABI and ZSTs.
34     ///
35     /// Not using the enum `Value` to encode that this must not be `Undef`.
36     Scalar(Scalar),
37
38     /// Used only for slices and strings (`&[T]`, `&str`, `*const [T]`, `*mut str`, `Box<str>`,
39     /// etc.).
40     ///
41     /// Empty slices don't necessarily have an address backed by an `AllocId`, thus we also need to
42     /// enable integer pointers. The `Scalar` type covers exactly those two cases. While we could
43     /// create dummy-`AllocId`s, the additional code effort for the conversions doesn't seem worth
44     /// it.
45     Slice(Scalar, u64),
46
47     /// An allocation together with a pointer into the allocation.
48     /// Invariant: the pointer's `AllocId` resolves to the allocation.
49     ByRef(Pointer, &'tcx Allocation),
50
51     /// Used in the HIR by using `Unevaluated` everywhere and later normalizing to one of the other
52     /// variants when the code is monomorphic enough for that.
53     Unevaluated(DefId, SubstsRef<'tcx>),
54 }
55
56 #[cfg(target_arch = "x86_64")]
57 static_assert_size!(ConstValue<'_>, 40);
58
59 impl<'tcx> ConstValue<'tcx> {
60     #[inline]
61     pub fn try_to_scalar(&self) -> Option<Scalar> {
62         match *self {
63             ConstValue::Param(_) |
64             ConstValue::Infer(_) |
65             ConstValue::Placeholder(_) |
66             ConstValue::ByRef(..) |
67             ConstValue::Unevaluated(..) |
68             ConstValue::Slice(..) => None,
69             ConstValue::Scalar(val) => Some(val),
70         }
71     }
72
73     #[inline]
74     pub fn try_to_bits(&self, size: Size) -> Option<u128> {
75         self.try_to_scalar()?.to_bits(size).ok()
76     }
77
78     #[inline]
79     pub fn try_to_ptr(&self) -> Option<Pointer> {
80         self.try_to_scalar()?.to_ptr().ok()
81     }
82
83     #[inline]
84     pub fn new_slice(
85         val: Scalar,
86         len: u64,
87     ) -> Self {
88         ConstValue::Slice(val, len)
89     }
90 }
91
92 /// A `Scalar` represents an immediate, primitive value existing outside of a
93 /// `memory::Allocation`. It is in many ways like a small chunk of a `Allocation`, up to 8 bytes in
94 /// size. Like a range of bytes in an `Allocation`, a `Scalar` can either represent the raw bytes
95 /// of a simple value or a pointer into another `Allocation`
96 #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd,
97          RustcEncodable, RustcDecodable, Hash, HashStable)]
98 pub enum Scalar<Tag=(), Id=AllocId> {
99     /// The raw bytes of a simple value.
100     Bits {
101         /// The first `size` bytes are the value.
102         /// Do not try to read less or more bytes than that. The remaining bytes must be 0.
103         size: u8,
104         bits: u128,
105     },
106
107     /// A pointer into an `Allocation`. An `Allocation` in the `memory` module has a list of
108     /// relocations, but a `Scalar` is only large enough to contain one, so we just represent the
109     /// relocation and its associated offset together as a `Pointer` here.
110     Ptr(Pointer<Tag, Id>),
111 }
112
113 #[cfg(target_arch = "x86_64")]
114 static_assert_size!(Scalar, 24);
115
116 impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Scalar<Tag, Id> {
117     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118         match self {
119             Scalar::Ptr(ptr) =>
120                 write!(f, "{:?}", ptr),
121             &Scalar::Bits { bits, size } => {
122                 if size == 0 {
123                     assert_eq!(bits, 0, "ZST value must be 0");
124                     write!(f, "<ZST>")
125                 } else {
126                     assert_eq!(truncate(bits, Size::from_bytes(size as u64)), bits,
127                             "Scalar value {:#x} exceeds size of {} bytes", bits, size);
128                     // Format as hex number wide enough to fit any value of the given `size`.
129                     // So bits=20, size=1 will be "0x14", but with size=4 it'll be "0x00000014".
130                     write!(f, "0x{:>0width$x}", bits, width=(size*2) as usize)
131                 }
132             }
133         }
134     }
135 }
136
137 impl<Tag> fmt::Display for Scalar<Tag> {
138     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139         match self {
140             Scalar::Ptr(_) => write!(f, "a pointer"),
141             Scalar::Bits { bits, .. } => write!(f, "{}", bits),
142         }
143     }
144 }
145
146 impl<'tcx> Scalar<()> {
147     #[inline]
148     pub fn with_tag<Tag>(self, new_tag: Tag) -> Scalar<Tag> {
149         match self {
150             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.with_tag(new_tag)),
151             Scalar::Bits { bits, size } => Scalar::Bits { bits, size },
152         }
153     }
154
155     #[inline(always)]
156     pub fn with_default_tag<Tag>(self) -> Scalar<Tag>
157         where Tag: Default
158     {
159         self.with_tag(Tag::default())
160     }
161 }
162
163 impl<'tcx, Tag> Scalar<Tag> {
164     #[inline]
165     pub fn erase_tag(self) -> Scalar {
166         match self {
167             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.erase_tag()),
168             Scalar::Bits { bits, size } => Scalar::Bits { bits, size },
169         }
170     }
171
172     #[inline]
173     pub fn ptr_null(cx: &impl HasDataLayout) -> Self {
174         Scalar::Bits {
175             bits: 0,
176             size: cx.data_layout().pointer_size.bytes() as u8,
177         }
178     }
179
180     #[inline]
181     pub fn zst() -> Self {
182         Scalar::Bits { bits: 0, size: 0 }
183     }
184
185     #[inline]
186     pub fn ptr_offset(self, i: Size, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
187         let dl = cx.data_layout();
188         match self {
189             Scalar::Bits { bits, size } => {
190                 assert_eq!(size as u64, dl.pointer_size.bytes());
191                 Ok(Scalar::Bits {
192                     bits: dl.offset(bits as u64, i.bytes())? as u128,
193                     size,
194                 })
195             }
196             Scalar::Ptr(ptr) => ptr.offset(i, dl).map(Scalar::Ptr),
197         }
198     }
199
200     #[inline]
201     pub fn ptr_wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
202         let dl = cx.data_layout();
203         match self {
204             Scalar::Bits { bits, size } => {
205                 assert_eq!(size as u64, dl.pointer_size.bytes());
206                 Scalar::Bits {
207                     bits: dl.overflowing_offset(bits as u64, i.bytes()).0 as u128,
208                     size,
209                 }
210             }
211             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_offset(i, dl)),
212         }
213     }
214
215     #[inline]
216     pub fn ptr_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
217         let dl = cx.data_layout();
218         match self {
219             Scalar::Bits { bits, size } => {
220                 assert_eq!(size as u64, dl.pointer_size().bytes());
221                 Ok(Scalar::Bits {
222                     bits: dl.signed_offset(bits as u64, i)? as u128,
223                     size,
224                 })
225             }
226             Scalar::Ptr(ptr) => ptr.signed_offset(i, dl).map(Scalar::Ptr),
227         }
228     }
229
230     #[inline]
231     pub fn ptr_wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
232         let dl = cx.data_layout();
233         match self {
234             Scalar::Bits { bits, size } => {
235                 assert_eq!(size as u64, dl.pointer_size.bytes());
236                 Scalar::Bits {
237                     bits: dl.overflowing_signed_offset(bits as u64, i128::from(i)).0 as u128,
238                     size,
239                 }
240             }
241             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_signed_offset(i, dl)),
242         }
243     }
244
245     /// Returns this pointers offset from the allocation base, or from NULL (for
246     /// integer pointers).
247     #[inline]
248     pub fn get_ptr_offset(self, cx: &impl HasDataLayout) -> Size {
249         match self {
250             Scalar::Bits { bits, size } => {
251                 assert_eq!(size as u64, cx.pointer_size().bytes());
252                 Size::from_bytes(bits as u64)
253             }
254             Scalar::Ptr(ptr) => ptr.offset,
255         }
256     }
257
258     #[inline]
259     pub fn is_null_ptr(self, cx: &impl HasDataLayout) -> bool {
260         match self {
261             Scalar::Bits { bits, size } => {
262                 assert_eq!(size as u64, cx.data_layout().pointer_size.bytes());
263                 bits == 0
264             },
265             Scalar::Ptr(_) => false,
266         }
267     }
268
269     #[inline]
270     pub fn from_bool(b: bool) -> Self {
271         Scalar::Bits { bits: b as u128, size: 1 }
272     }
273
274     #[inline]
275     pub fn from_char(c: char) -> Self {
276         Scalar::Bits { bits: c as u128, size: 4 }
277     }
278
279     #[inline]
280     pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
281         let i = i.into();
282         debug_assert_eq!(truncate(i, size), i,
283                          "Unsigned value {} does not fit in {} bits", i, size.bits());
284         Scalar::Bits { bits: i, size: size.bytes() as u8 }
285     }
286
287     #[inline]
288     pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
289         let i = i.into();
290         // `into` performed sign extension, we have to truncate
291         let truncated = truncate(i as u128, size);
292         debug_assert_eq!(sign_extend(truncated, size) as i128, i,
293                          "Signed value {} does not fit in {} bits", i, size.bits());
294         Scalar::Bits { bits: truncated, size: size.bytes() as u8 }
295     }
296
297     #[inline]
298     pub fn from_f32(f: f32) -> Self {
299         Scalar::Bits { bits: f.to_bits() as u128, size: 4 }
300     }
301
302     #[inline]
303     pub fn from_f64(f: f64) -> Self {
304         Scalar::Bits { bits: f.to_bits() as u128, size: 8 }
305     }
306
307     #[inline]
308     pub fn to_bits(self, target_size: Size) -> EvalResult<'tcx, u128> {
309         match self {
310             Scalar::Bits { bits, size } => {
311                 assert_eq!(target_size.bytes(), size as u64);
312                 assert_ne!(size, 0, "to_bits cannot be used with zsts");
313                 Ok(bits)
314             }
315             Scalar::Ptr(_) => err!(ReadPointerAsBytes),
316         }
317     }
318
319     #[inline]
320     pub fn to_ptr(self) -> EvalResult<'tcx, Pointer<Tag>> {
321         match self {
322             Scalar::Bits { bits: 0, .. } => err!(InvalidNullPointerUsage),
323             Scalar::Bits { .. } => err!(ReadBytesAsPointer),
324             Scalar::Ptr(p) => Ok(p),
325         }
326     }
327
328     #[inline]
329     pub fn is_bits(self) -> bool {
330         match self {
331             Scalar::Bits { .. } => true,
332             _ => false,
333         }
334     }
335
336     #[inline]
337     pub fn is_ptr(self) -> bool {
338         match self {
339             Scalar::Ptr(_) => true,
340             _ => false,
341         }
342     }
343
344     pub fn to_bool(self) -> EvalResult<'tcx, bool> {
345         match self {
346             Scalar::Bits { bits: 0, size: 1 } => Ok(false),
347             Scalar::Bits { bits: 1, size: 1 } => Ok(true),
348             _ => err!(InvalidBool),
349         }
350     }
351
352     pub fn to_char(self) -> EvalResult<'tcx, char> {
353         let val = self.to_u32()?;
354         match ::std::char::from_u32(val) {
355             Some(c) => Ok(c),
356             None => err!(InvalidChar(val as u128)),
357         }
358     }
359
360     pub fn to_u8(self) -> EvalResult<'static, u8> {
361         let sz = Size::from_bits(8);
362         let b = self.to_bits(sz)?;
363         assert_eq!(b as u8 as u128, b);
364         Ok(b as u8)
365     }
366
367     pub fn to_u32(self) -> EvalResult<'static, u32> {
368         let sz = Size::from_bits(32);
369         let b = self.to_bits(sz)?;
370         assert_eq!(b as u32 as u128, b);
371         Ok(b as u32)
372     }
373
374     pub fn to_u64(self) -> EvalResult<'static, u64> {
375         let sz = Size::from_bits(64);
376         let b = self.to_bits(sz)?;
377         assert_eq!(b as u64 as u128, b);
378         Ok(b as u64)
379     }
380
381     pub fn to_usize(self, cx: &impl HasDataLayout) -> EvalResult<'static, u64> {
382         let b = self.to_bits(cx.data_layout().pointer_size)?;
383         assert_eq!(b as u64 as u128, b);
384         Ok(b as u64)
385     }
386
387     pub fn to_i8(self) -> EvalResult<'static, i8> {
388         let sz = Size::from_bits(8);
389         let b = self.to_bits(sz)?;
390         let b = sign_extend(b, sz) as i128;
391         assert_eq!(b as i8 as i128, b);
392         Ok(b as i8)
393     }
394
395     pub fn to_i32(self) -> EvalResult<'static, i32> {
396         let sz = Size::from_bits(32);
397         let b = self.to_bits(sz)?;
398         let b = sign_extend(b, sz) as i128;
399         assert_eq!(b as i32 as i128, b);
400         Ok(b as i32)
401     }
402
403     pub fn to_i64(self) -> EvalResult<'static, i64> {
404         let sz = Size::from_bits(64);
405         let b = self.to_bits(sz)?;
406         let b = sign_extend(b, sz) as i128;
407         assert_eq!(b as i64 as i128, b);
408         Ok(b as i64)
409     }
410
411     pub fn to_isize(self, cx: &impl HasDataLayout) -> EvalResult<'static, i64> {
412         let b = self.to_bits(cx.data_layout().pointer_size)?;
413         let b = sign_extend(b, cx.data_layout().pointer_size) as i128;
414         assert_eq!(b as i64 as i128, b);
415         Ok(b as i64)
416     }
417
418     #[inline]
419     pub fn to_f32(self) -> EvalResult<'static, f32> {
420         Ok(f32::from_bits(self.to_u32()?))
421     }
422
423     #[inline]
424     pub fn to_f64(self) -> EvalResult<'static, f64> {
425         Ok(f64::from_bits(self.to_u64()?))
426     }
427 }
428
429 impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
430     #[inline(always)]
431     fn from(ptr: Pointer<Tag>) -> Self {
432         Scalar::Ptr(ptr)
433     }
434 }
435
436 #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, RustcEncodable, RustcDecodable, Hash)]
437 pub enum ScalarMaybeUndef<Tag=(), Id=AllocId> {
438     Scalar(Scalar<Tag, Id>),
439     Undef,
440 }
441
442 impl<Tag> From<Scalar<Tag>> for ScalarMaybeUndef<Tag> {
443     #[inline(always)]
444     fn from(s: Scalar<Tag>) -> Self {
445         ScalarMaybeUndef::Scalar(s)
446     }
447 }
448
449 impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for ScalarMaybeUndef<Tag, Id> {
450     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
451         match self {
452             ScalarMaybeUndef::Undef => write!(f, "Undef"),
453             ScalarMaybeUndef::Scalar(s) => write!(f, "{:?}", s),
454         }
455     }
456 }
457
458 impl<Tag> fmt::Display for ScalarMaybeUndef<Tag> {
459     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
460         match self {
461             ScalarMaybeUndef::Undef => write!(f, "uninitialized bytes"),
462             ScalarMaybeUndef::Scalar(s) => write!(f, "{}", s),
463         }
464     }
465 }
466
467 impl<'tcx> ScalarMaybeUndef<()> {
468     #[inline]
469     pub fn with_tag<Tag>(self, new_tag: Tag) -> ScalarMaybeUndef<Tag> {
470         match self {
471             ScalarMaybeUndef::Scalar(s) => ScalarMaybeUndef::Scalar(s.with_tag(new_tag)),
472             ScalarMaybeUndef::Undef => ScalarMaybeUndef::Undef,
473         }
474     }
475
476     #[inline(always)]
477     pub fn with_default_tag<Tag>(self) -> ScalarMaybeUndef<Tag>
478         where Tag: Default
479     {
480         self.with_tag(Tag::default())
481     }
482 }
483
484 impl<'tcx, Tag> ScalarMaybeUndef<Tag> {
485     #[inline]
486     pub fn erase_tag(self) -> ScalarMaybeUndef
487     {
488         match self {
489             ScalarMaybeUndef::Scalar(s) => ScalarMaybeUndef::Scalar(s.erase_tag()),
490             ScalarMaybeUndef::Undef => ScalarMaybeUndef::Undef,
491         }
492     }
493
494     #[inline]
495     pub fn not_undef(self) -> EvalResult<'static, Scalar<Tag>> {
496         match self {
497             ScalarMaybeUndef::Scalar(scalar) => Ok(scalar),
498             ScalarMaybeUndef::Undef => err!(ReadUndefBytes(Size::from_bytes(0))),
499         }
500     }
501
502     #[inline(always)]
503     pub fn to_ptr(self) -> EvalResult<'tcx, Pointer<Tag>> {
504         self.not_undef()?.to_ptr()
505     }
506
507     #[inline(always)]
508     pub fn to_bits(self, target_size: Size) -> EvalResult<'tcx, u128> {
509         self.not_undef()?.to_bits(target_size)
510     }
511
512     #[inline(always)]
513     pub fn to_bool(self) -> EvalResult<'tcx, bool> {
514         self.not_undef()?.to_bool()
515     }
516
517     #[inline(always)]
518     pub fn to_char(self) -> EvalResult<'tcx, char> {
519         self.not_undef()?.to_char()
520     }
521
522     #[inline(always)]
523     pub fn to_f32(self) -> EvalResult<'tcx, f32> {
524         self.not_undef()?.to_f32()
525     }
526
527     #[inline(always)]
528     pub fn to_f64(self) -> EvalResult<'tcx, f64> {
529         self.not_undef()?.to_f64()
530     }
531
532     #[inline(always)]
533     pub fn to_u8(self) -> EvalResult<'tcx, u8> {
534         self.not_undef()?.to_u8()
535     }
536
537     #[inline(always)]
538     pub fn to_u32(self) -> EvalResult<'tcx, u32> {
539         self.not_undef()?.to_u32()
540     }
541
542     #[inline(always)]
543     pub fn to_u64(self) -> EvalResult<'tcx, u64> {
544         self.not_undef()?.to_u64()
545     }
546
547     #[inline(always)]
548     pub fn to_usize(self, cx: &impl HasDataLayout) -> EvalResult<'tcx, u64> {
549         self.not_undef()?.to_usize(cx)
550     }
551
552     #[inline(always)]
553     pub fn to_i8(self) -> EvalResult<'tcx, i8> {
554         self.not_undef()?.to_i8()
555     }
556
557     #[inline(always)]
558     pub fn to_i32(self) -> EvalResult<'tcx, i32> {
559         self.not_undef()?.to_i32()
560     }
561
562     #[inline(always)]
563     pub fn to_i64(self) -> EvalResult<'tcx, i64> {
564         self.not_undef()?.to_i64()
565     }
566
567     #[inline(always)]
568     pub fn to_isize(self, cx: &impl HasDataLayout) -> EvalResult<'tcx, i64> {
569         self.not_undef()?.to_isize(cx)
570     }
571 }
572
573 impl_stable_hash_for!(enum crate::mir::interpret::ScalarMaybeUndef {
574     Scalar(v),
575     Undef
576 });