2 use rustc_macros::HashStable;
4 use crate::ty::{Ty, InferConst, ParamConst, layout::{HasDataLayout, Size}, subst::SubstsRef};
5 use crate::ty::PlaceholderConst;
6 use crate::hir::def_id::DefId;
8 use super::{EvalResult, Pointer, PointerArithmetic, Allocation, AllocId, sign_extend, truncate};
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,
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.
27 /// Infer the value of the const.
28 Infer(InferConst<'tcx>),
30 /// A placeholder const - universally quantified higher-ranked const.
31 Placeholder(PlaceholderConst),
33 /// Used only for types with `layout::abi::Scalar` ABI and ZSTs.
35 /// Not using the enum `Value` to encode that this must not be `Undef`.
38 /// Used only for slices and strings (`&[T]`, `&str`, `*const [T]`, `*mut str`, `Box<str>`,
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
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),
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>),
56 #[cfg(target_arch = "x86_64")]
57 static_assert_size!(ConstValue<'_>, 40);
59 impl<'tcx> ConstValue<'tcx> {
61 pub fn try_to_scalar(&self) -> Option<Scalar> {
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),
74 pub fn try_to_bits(&self, size: Size) -> Option<u128> {
75 self.try_to_scalar()?.to_bits(size).ok()
79 pub fn try_to_ptr(&self) -> Option<Pointer> {
80 self.try_to_scalar()?.to_ptr().ok()
88 ConstValue::Slice(val, len)
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.
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.
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>),
113 #[cfg(target_arch = "x86_64")]
114 static_assert_size!(Scalar, 24);
116 impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Scalar<Tag, Id> {
117 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120 write!(f, "{:?}", ptr),
121 &Scalar::Bits { bits, size } => {
123 assert_eq!(bits, 0, "ZST value must be 0");
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)
137 impl<Tag> fmt::Display for Scalar<Tag> {
138 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140 Scalar::Ptr(_) => write!(f, "a pointer"),
141 Scalar::Bits { bits, .. } => write!(f, "{}", bits),
146 impl<'tcx> Scalar<()> {
148 pub fn with_tag<Tag>(self, new_tag: Tag) -> Scalar<Tag> {
150 Scalar::Ptr(ptr) => Scalar::Ptr(ptr.with_tag(new_tag)),
151 Scalar::Bits { bits, size } => Scalar::Bits { bits, size },
156 pub fn with_default_tag<Tag>(self) -> Scalar<Tag>
159 self.with_tag(Tag::default())
163 impl<'tcx, Tag> Scalar<Tag> {
165 pub fn erase_tag(self) -> Scalar {
167 Scalar::Ptr(ptr) => Scalar::Ptr(ptr.erase_tag()),
168 Scalar::Bits { bits, size } => Scalar::Bits { bits, size },
173 pub fn ptr_null(cx: &impl HasDataLayout) -> Self {
176 size: cx.data_layout().pointer_size.bytes() as u8,
181 pub fn zst() -> Self {
182 Scalar::Bits { bits: 0, size: 0 }
186 pub fn ptr_offset(self, i: Size, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
187 let dl = cx.data_layout();
189 Scalar::Bits { bits, size } => {
190 assert_eq!(size as u64, dl.pointer_size.bytes());
192 bits: dl.offset(bits as u64, i.bytes())? as u128,
196 Scalar::Ptr(ptr) => ptr.offset(i, dl).map(Scalar::Ptr),
201 pub fn ptr_wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
202 let dl = cx.data_layout();
204 Scalar::Bits { bits, size } => {
205 assert_eq!(size as u64, dl.pointer_size.bytes());
207 bits: dl.overflowing_offset(bits as u64, i.bytes()).0 as u128,
211 Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_offset(i, dl)),
216 pub fn ptr_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
217 let dl = cx.data_layout();
219 Scalar::Bits { bits, size } => {
220 assert_eq!(size as u64, dl.pointer_size().bytes());
222 bits: dl.signed_offset(bits as u64, i)? as u128,
226 Scalar::Ptr(ptr) => ptr.signed_offset(i, dl).map(Scalar::Ptr),
231 pub fn ptr_wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
232 let dl = cx.data_layout();
234 Scalar::Bits { bits, size } => {
235 assert_eq!(size as u64, dl.pointer_size.bytes());
237 bits: dl.overflowing_signed_offset(bits as u64, i128::from(i)).0 as u128,
241 Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_signed_offset(i, dl)),
245 /// Returns this pointers offset from the allocation base, or from NULL (for
246 /// integer pointers).
248 pub fn get_ptr_offset(self, cx: &impl HasDataLayout) -> Size {
250 Scalar::Bits { bits, size } => {
251 assert_eq!(size as u64, cx.pointer_size().bytes());
252 Size::from_bytes(bits as u64)
254 Scalar::Ptr(ptr) => ptr.offset,
259 pub fn is_null_ptr(self, cx: &impl HasDataLayout) -> bool {
261 Scalar::Bits { bits, size } => {
262 assert_eq!(size as u64, cx.data_layout().pointer_size.bytes());
265 Scalar::Ptr(_) => false,
270 pub fn from_bool(b: bool) -> Self {
271 Scalar::Bits { bits: b as u128, size: 1 }
275 pub fn from_char(c: char) -> Self {
276 Scalar::Bits { bits: c as u128, size: 4 }
280 pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
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 }
288 pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
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 }
298 pub fn from_f32(f: f32) -> Self {
299 Scalar::Bits { bits: f.to_bits() as u128, size: 4 }
303 pub fn from_f64(f: f64) -> Self {
304 Scalar::Bits { bits: f.to_bits() as u128, size: 8 }
308 pub fn to_bits(self, target_size: Size) -> EvalResult<'tcx, u128> {
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");
315 Scalar::Ptr(_) => err!(ReadPointerAsBytes),
320 pub fn to_ptr(self) -> EvalResult<'tcx, Pointer<Tag>> {
322 Scalar::Bits { bits: 0, .. } => err!(InvalidNullPointerUsage),
323 Scalar::Bits { .. } => err!(ReadBytesAsPointer),
324 Scalar::Ptr(p) => Ok(p),
329 pub fn is_bits(self) -> bool {
331 Scalar::Bits { .. } => true,
337 pub fn is_ptr(self) -> bool {
339 Scalar::Ptr(_) => true,
344 pub fn to_bool(self) -> EvalResult<'tcx, bool> {
346 Scalar::Bits { bits: 0, size: 1 } => Ok(false),
347 Scalar::Bits { bits: 1, size: 1 } => Ok(true),
348 _ => err!(InvalidBool),
352 pub fn to_char(self) -> EvalResult<'tcx, char> {
353 let val = self.to_u32()?;
354 match ::std::char::from_u32(val) {
356 None => err!(InvalidChar(val as u128)),
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);
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);
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);
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);
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);
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);
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);
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);
419 pub fn to_f32(self) -> EvalResult<'static, f32> {
420 Ok(f32::from_bits(self.to_u32()?))
424 pub fn to_f64(self) -> EvalResult<'static, f64> {
425 Ok(f64::from_bits(self.to_u64()?))
429 impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
431 fn from(ptr: Pointer<Tag>) -> Self {
436 #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, RustcEncodable, RustcDecodable, Hash)]
437 pub enum ScalarMaybeUndef<Tag=(), Id=AllocId> {
438 Scalar(Scalar<Tag, Id>),
442 impl<Tag> From<Scalar<Tag>> for ScalarMaybeUndef<Tag> {
444 fn from(s: Scalar<Tag>) -> Self {
445 ScalarMaybeUndef::Scalar(s)
449 impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for ScalarMaybeUndef<Tag, Id> {
450 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
452 ScalarMaybeUndef::Undef => write!(f, "Undef"),
453 ScalarMaybeUndef::Scalar(s) => write!(f, "{:?}", s),
458 impl<Tag> fmt::Display for ScalarMaybeUndef<Tag> {
459 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
461 ScalarMaybeUndef::Undef => write!(f, "uninitialized bytes"),
462 ScalarMaybeUndef::Scalar(s) => write!(f, "{}", s),
467 impl<'tcx> ScalarMaybeUndef<()> {
469 pub fn with_tag<Tag>(self, new_tag: Tag) -> ScalarMaybeUndef<Tag> {
471 ScalarMaybeUndef::Scalar(s) => ScalarMaybeUndef::Scalar(s.with_tag(new_tag)),
472 ScalarMaybeUndef::Undef => ScalarMaybeUndef::Undef,
477 pub fn with_default_tag<Tag>(self) -> ScalarMaybeUndef<Tag>
480 self.with_tag(Tag::default())
484 impl<'tcx, Tag> ScalarMaybeUndef<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) -> EvalResult<'static, Scalar<Tag>> {
497 ScalarMaybeUndef::Scalar(scalar) => Ok(scalar),
498 ScalarMaybeUndef::Undef => err!(ReadUndefBytes(Size::from_bytes(0))),
503 pub fn to_ptr(self) -> EvalResult<'tcx, Pointer<Tag>> {
504 self.not_undef()?.to_ptr()
508 pub fn to_bits(self, target_size: Size) -> EvalResult<'tcx, u128> {
509 self.not_undef()?.to_bits(target_size)
513 pub fn to_bool(self) -> EvalResult<'tcx, bool> {
514 self.not_undef()?.to_bool()
518 pub fn to_char(self) -> EvalResult<'tcx, char> {
519 self.not_undef()?.to_char()
523 pub fn to_f32(self) -> EvalResult<'tcx, f32> {
524 self.not_undef()?.to_f32()
528 pub fn to_f64(self) -> EvalResult<'tcx, f64> {
529 self.not_undef()?.to_f64()
533 pub fn to_u8(self) -> EvalResult<'tcx, u8> {
534 self.not_undef()?.to_u8()
538 pub fn to_u32(self) -> EvalResult<'tcx, u32> {
539 self.not_undef()?.to_u32()
543 pub fn to_u64(self) -> EvalResult<'tcx, u64> {
544 self.not_undef()?.to_u64()
548 pub fn to_usize(self, cx: &impl HasDataLayout) -> EvalResult<'tcx, u64> {
549 self.not_undef()?.to_usize(cx)
553 pub fn to_i8(self) -> EvalResult<'tcx, i8> {
554 self.not_undef()?.to_i8()
558 pub fn to_i32(self) -> EvalResult<'tcx, i32> {
559 self.not_undef()?.to_i32()
563 pub fn to_i64(self) -> EvalResult<'tcx, i64> {
564 self.not_undef()?.to_i64()
568 pub fn to_isize(self, cx: &impl HasDataLayout) -> EvalResult<'tcx, i64> {
569 self.not_undef()?.to_isize(cx)
573 impl_stable_hash_for!(enum crate::mir::interpret::ScalarMaybeUndef {