2 use rustc_macros::HashStable;
3 use rustc_apfloat::{Float, ieee::{Double, Single}};
5 use crate::ty::{Ty, InferConst, ParamConst, layout::{HasDataLayout, Size}, subst::SubstsRef};
6 use crate::ty::PlaceholderConst;
7 use crate::hir::def_id::DefId;
9 use super::{InterpResult, Pointer, PointerArithmetic, Allocation, AllocId, sign_extend, truncate};
11 /// Represents the result of a raw const operation, pre-validation.
12 #[derive(Copy, Clone, Debug, Eq, PartialEq, RustcEncodable, RustcDecodable, Hash, HashStable)]
13 pub struct RawConst<'tcx> {
14 // the value lives here, at offset 0, and that allocation definitely is a `AllocKind::Memory`
15 // (so you can use `AllocMap::unwrap_memory`).
16 pub alloc_id: AllocId,
20 /// Represents a constant value in Rust. `Scalar` and `ScalarPair` are optimizations that
21 /// match the `LocalState` optimizations for easy conversions between `Value` and `ConstValue`.
22 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord,
23 RustcEncodable, RustcDecodable, Hash, HashStable)]
24 pub enum ConstValue<'tcx> {
25 /// A const generic parameter.
28 /// Infer the value of the const.
29 Infer(InferConst<'tcx>),
31 /// A placeholder const - universally quantified higher-ranked const.
32 Placeholder(PlaceholderConst),
34 /// Used only for types with `layout::abi::Scalar` ABI and ZSTs.
36 /// Not using the enum `Value` to encode that this must not be `Undef`.
39 /// Used only for `&[u8]` and `&str`
41 data: &'tcx Allocation,
46 /// An allocation together with a pointer into the allocation.
47 /// Invariant: the pointer's `AllocId` resolves to the allocation.
48 ByRef(Pointer, &'tcx Allocation),
50 /// Used in the HIR by using `Unevaluated` everywhere and later normalizing to one of the other
51 /// variants when the code is monomorphic enough for that.
52 Unevaluated(DefId, SubstsRef<'tcx>),
55 #[cfg(target_arch = "x86_64")]
56 static_assert_size!(ConstValue<'_>, 32);
58 impl<'tcx> ConstValue<'tcx> {
60 pub fn try_to_scalar(&self) -> Option<Scalar> {
62 ConstValue::Param(_) |
63 ConstValue::Infer(_) |
64 ConstValue::Placeholder(_) |
65 ConstValue::ByRef(..) |
66 ConstValue::Unevaluated(..) |
67 ConstValue::Slice { .. } => None,
68 ConstValue::Scalar(val) => Some(val),
73 pub fn try_to_bits(&self, size: Size) -> Option<u128> {
74 self.try_to_scalar()?.to_bits(size).ok()
78 pub fn try_to_ptr(&self) -> Option<Pointer> {
79 self.try_to_scalar()?.to_ptr().ok()
83 /// A `Scalar` represents an immediate, primitive value existing outside of a
84 /// `memory::Allocation`. It is in many ways like a small chunk of a `Allocation`, up to 8 bytes in
85 /// size. Like a range of bytes in an `Allocation`, a `Scalar` can either represent the raw bytes
86 /// of a simple value or a pointer into another `Allocation`
87 #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd,
88 RustcEncodable, RustcDecodable, Hash, HashStable)]
89 pub enum Scalar<Tag=(), Id=AllocId> {
90 /// The raw bytes of a simple value.
92 /// The first `size` bytes of `data` are the value.
93 /// Do not try to read less or more bytes than that. The remaining bytes must be 0.
98 /// A pointer into an `Allocation`. An `Allocation` in the `memory` module has a list of
99 /// relocations, but a `Scalar` is only large enough to contain one, so we just represent the
100 /// relocation and its associated offset together as a `Pointer` here.
101 Ptr(Pointer<Tag, Id>),
104 #[cfg(target_arch = "x86_64")]
105 static_assert_size!(Scalar, 24);
107 impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Scalar<Tag, Id> {
108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111 write!(f, "{:?}", ptr),
112 &Scalar::Raw { data, size } => {
113 Scalar::check_data(data, size);
117 // Format as hex number wide enough to fit any value of the given `size`.
118 // So data=20, size=1 will be "0x14", but with size=4 it'll be "0x00000014".
119 write!(f, "0x{:>0width$x}", data, width=(size*2) as usize)
126 impl<Tag> fmt::Display for Scalar<Tag> {
127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129 Scalar::Ptr(_) => write!(f, "a pointer"),
130 Scalar::Raw { data, .. } => write!(f, "{}", data),
135 impl<'tcx> Scalar<()> {
137 fn check_data(data: u128, size: u8) {
138 debug_assert_eq!(truncate(data, Size::from_bytes(size as u64)), data,
139 "Scalar value {:#x} exceeds size of {} bytes", data, size);
142 /// Tag this scalar with `new_tag` if it is a pointer, leave it unchanged otherwise.
144 /// Used by `MemPlace::replace_tag`.
146 pub fn with_tag<Tag>(self, new_tag: Tag) -> Scalar<Tag> {
148 Scalar::Ptr(ptr) => Scalar::Ptr(ptr.with_tag(new_tag)),
149 Scalar::Raw { data, size } => Scalar::Raw { data, size },
154 impl<'tcx, Tag> Scalar<Tag> {
155 /// Erase the tag from the scalar, if any.
157 /// Used by error reporting code to avoid having the error type depend on `Tag`.
159 pub fn erase_tag(self) -> Scalar {
161 Scalar::Ptr(ptr) => Scalar::Ptr(ptr.erase_tag()),
162 Scalar::Raw { data, size } => Scalar::Raw { data, size },
167 pub fn ptr_null(cx: &impl HasDataLayout) -> Self {
170 size: cx.data_layout().pointer_size.bytes() as u8,
175 pub fn zst() -> Self {
176 Scalar::Raw { data: 0, size: 0 }
180 pub fn ptr_offset(self, i: Size, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
181 let dl = cx.data_layout();
183 Scalar::Raw { data, size } => {
184 assert_eq!(size as u64, dl.pointer_size.bytes());
186 data: dl.offset(data as u64, i.bytes())? as u128,
190 Scalar::Ptr(ptr) => ptr.offset(i, dl).map(Scalar::Ptr),
195 pub fn ptr_wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
196 let dl = cx.data_layout();
198 Scalar::Raw { data, size } => {
199 assert_eq!(size as u64, dl.pointer_size.bytes());
201 data: dl.overflowing_offset(data as u64, i.bytes()).0 as u128,
205 Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_offset(i, dl)),
210 pub fn ptr_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
211 let dl = cx.data_layout();
213 Scalar::Raw { data, size } => {
214 assert_eq!(size as u64, dl.pointer_size().bytes());
216 data: dl.signed_offset(data as u64, i)? as u128,
220 Scalar::Ptr(ptr) => ptr.signed_offset(i, dl).map(Scalar::Ptr),
225 pub fn ptr_wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
226 let dl = cx.data_layout();
228 Scalar::Raw { data, size } => {
229 assert_eq!(size as u64, dl.pointer_size.bytes());
231 data: dl.overflowing_signed_offset(data as u64, i128::from(i)).0 as u128,
235 Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_signed_offset(i, dl)),
239 /// Returns this pointer's offset from the allocation base, or from NULL (for
240 /// integer pointers).
242 pub fn get_ptr_offset(self, cx: &impl HasDataLayout) -> Size {
244 Scalar::Raw { data, size } => {
245 assert_eq!(size as u64, cx.pointer_size().bytes());
246 Size::from_bytes(data as u64)
248 Scalar::Ptr(ptr) => ptr.offset,
253 pub fn is_null_ptr(self, cx: &impl HasDataLayout) -> bool {
255 Scalar::Raw { data, size } => {
256 assert_eq!(size as u64, cx.data_layout().pointer_size.bytes());
259 Scalar::Ptr(_) => false,
264 pub fn from_bool(b: bool) -> Self {
265 Scalar::Raw { data: b as u128, size: 1 }
269 pub fn from_char(c: char) -> Self {
270 Scalar::Raw { data: c as u128, size: 4 }
274 pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
277 truncate(i, size), i,
278 "Unsigned value {:#x} does not fit in {} bits", i, size.bits()
280 Scalar::Raw { data: i, size: size.bytes() as u8 }
284 pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
286 // `into` performed sign extension, we have to truncate
287 let truncated = truncate(i as u128, size);
289 sign_extend(truncated, size) as i128, i,
290 "Signed value {:#x} does not fit in {} bits", i, size.bits()
292 Scalar::Raw { data: truncated, size: size.bytes() as u8 }
296 pub fn from_f32(f: Single) -> Self {
297 Scalar::Raw { data: f.to_bits() as u128, size: 4 }
301 pub fn from_f64(f: Double) -> Self {
302 Scalar::Raw { data: f.to_bits() as u128, size: 8 }
306 pub fn to_bits_or_ptr(
309 cx: &impl HasDataLayout,
310 ) -> Result<u128, Pointer<Tag>> {
312 Scalar::Raw { data, size } => {
313 assert_eq!(target_size.bytes(), size as u64);
314 assert_ne!(size, 0, "you should never look at the bits of a ZST");
315 Scalar::check_data(data, size);
318 Scalar::Ptr(ptr) => {
319 assert_eq!(target_size, cx.data_layout().pointer_size);
326 pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
328 Scalar::Raw { data, size } => {
329 assert_eq!(target_size.bytes(), size as u64);
330 assert_ne!(size, 0, "you should never look at the bits of a ZST");
331 Scalar::check_data(data, size);
334 Scalar::Ptr(_) => err!(ReadPointerAsBytes),
339 pub fn to_ptr(self) -> InterpResult<'tcx, Pointer<Tag>> {
341 Scalar::Raw { data: 0, .. } => err!(InvalidNullPointerUsage),
342 Scalar::Raw { .. } => err!(ReadBytesAsPointer),
343 Scalar::Ptr(p) => Ok(p),
348 pub fn is_bits(self) -> bool {
350 Scalar::Raw { .. } => true,
356 pub fn is_ptr(self) -> bool {
358 Scalar::Ptr(_) => true,
363 pub fn to_bool(self) -> InterpResult<'tcx, bool> {
365 Scalar::Raw { data: 0, size: 1 } => Ok(false),
366 Scalar::Raw { data: 1, size: 1 } => Ok(true),
367 _ => err!(InvalidBool),
371 pub fn to_char(self) -> InterpResult<'tcx, char> {
372 let val = self.to_u32()?;
373 match ::std::char::from_u32(val) {
375 None => err!(InvalidChar(val as u128)),
379 pub fn to_u8(self) -> InterpResult<'static, u8> {
380 let sz = Size::from_bits(8);
381 let b = self.to_bits(sz)?;
385 pub fn to_u32(self) -> InterpResult<'static, u32> {
386 let sz = Size::from_bits(32);
387 let b = self.to_bits(sz)?;
391 pub fn to_u64(self) -> InterpResult<'static, u64> {
392 let sz = Size::from_bits(64);
393 let b = self.to_bits(sz)?;
397 pub fn to_usize(self, cx: &impl HasDataLayout) -> InterpResult<'static, u64> {
398 let b = self.to_bits(cx.data_layout().pointer_size)?;
402 pub fn to_i8(self) -> InterpResult<'static, i8> {
403 let sz = Size::from_bits(8);
404 let b = self.to_bits(sz)?;
405 let b = sign_extend(b, sz) as i128;
409 pub fn to_i32(self) -> InterpResult<'static, i32> {
410 let sz = Size::from_bits(32);
411 let b = self.to_bits(sz)?;
412 let b = sign_extend(b, sz) as i128;
416 pub fn to_i64(self) -> InterpResult<'static, i64> {
417 let sz = Size::from_bits(64);
418 let b = self.to_bits(sz)?;
419 let b = sign_extend(b, sz) as i128;
423 pub fn to_isize(self, cx: &impl HasDataLayout) -> InterpResult<'static, i64> {
424 let sz = cx.data_layout().pointer_size;
425 let b = self.to_bits(sz)?;
426 let b = sign_extend(b, sz) as i128;
431 pub fn to_f32(self) -> InterpResult<'static, Single> {
432 // Going through `u32` to check size and truncation.
433 Ok(Single::from_bits(self.to_u32()? as u128))
437 pub fn to_f64(self) -> InterpResult<'static, Double> {
438 // Going through `u64` to check size and truncation.
439 Ok(Double::from_bits(self.to_u64()? as u128))
443 impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
445 fn from(ptr: Pointer<Tag>) -> Self {
450 #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, RustcEncodable, RustcDecodable, Hash)]
451 pub enum ScalarMaybeUndef<Tag=(), Id=AllocId> {
452 Scalar(Scalar<Tag, Id>),
456 impl<Tag> From<Scalar<Tag>> for ScalarMaybeUndef<Tag> {
458 fn from(s: Scalar<Tag>) -> Self {
459 ScalarMaybeUndef::Scalar(s)
463 impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for ScalarMaybeUndef<Tag, Id> {
464 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
466 ScalarMaybeUndef::Undef => write!(f, "Undef"),
467 ScalarMaybeUndef::Scalar(s) => write!(f, "{:?}", s),
472 impl<Tag> fmt::Display for ScalarMaybeUndef<Tag> {
473 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
475 ScalarMaybeUndef::Undef => write!(f, "uninitialized bytes"),
476 ScalarMaybeUndef::Scalar(s) => write!(f, "{}", s),
481 impl<'tcx, Tag> ScalarMaybeUndef<Tag> {
482 /// Erase the tag from the scalar, if any.
484 /// Used by error reporting code to avoid having the error type depend on `Tag`.
486 pub fn erase_tag(self) -> ScalarMaybeUndef
489 ScalarMaybeUndef::Scalar(s) => ScalarMaybeUndef::Scalar(s.erase_tag()),
490 ScalarMaybeUndef::Undef => ScalarMaybeUndef::Undef,
495 pub fn not_undef(self) -> InterpResult<'static, Scalar<Tag>> {
497 ScalarMaybeUndef::Scalar(scalar) => Ok(scalar),
498 ScalarMaybeUndef::Undef => err!(ReadUndefBytes(Size::from_bytes(0))),
503 pub fn to_ptr(self) -> InterpResult<'tcx, Pointer<Tag>> {
504 self.not_undef()?.to_ptr()
508 pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
509 self.not_undef()?.to_bits(target_size)
513 pub fn to_bool(self) -> InterpResult<'tcx, bool> {
514 self.not_undef()?.to_bool()
518 pub fn to_char(self) -> InterpResult<'tcx, char> {
519 self.not_undef()?.to_char()
523 pub fn to_f32(self) -> InterpResult<'tcx, Single> {
524 self.not_undef()?.to_f32()
528 pub fn to_f64(self) -> InterpResult<'tcx, Double> {
529 self.not_undef()?.to_f64()
533 pub fn to_u8(self) -> InterpResult<'tcx, u8> {
534 self.not_undef()?.to_u8()
538 pub fn to_u32(self) -> InterpResult<'tcx, u32> {
539 self.not_undef()?.to_u32()
543 pub fn to_u64(self) -> InterpResult<'tcx, u64> {
544 self.not_undef()?.to_u64()
548 pub fn to_usize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
549 self.not_undef()?.to_usize(cx)
553 pub fn to_i8(self) -> InterpResult<'tcx, i8> {
554 self.not_undef()?.to_i8()
558 pub fn to_i32(self) -> InterpResult<'tcx, i32> {
559 self.not_undef()?.to_i32()
563 pub fn to_i64(self) -> InterpResult<'tcx, i64> {
564 self.not_undef()?.to_i64()
568 pub fn to_isize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, i64> {
569 self.not_undef()?.to_isize(cx)
573 impl_stable_hash_for!(enum crate::mir::interpret::ScalarMaybeUndef {