2 use crate::mir::interpret::{AllocId, Scalar};
3 use crate::ty::{self, Ty, TyCtxt};
4 use rustc_macros::{HashStable, TyDecodable, TyEncodable};
6 #[derive(Copy, Clone, Debug, Hash, TyEncodable, TyDecodable, Eq, PartialEq, Ord, PartialOrd)]
8 /// This datastructure is used to represent the value of constants used in the type system.
10 /// We explicitly choose a different datastructure from the way values are processed within
11 /// CTFE, as in the type system equal values (according to their `PartialEq`) must also have
12 /// equal representation (`==` on the rustc data structure, e.g. `ValTree`) and vice versa.
13 /// Since CTFE uses `AllocId` to represent pointers, it often happens that two different
14 /// `AllocId`s point to equal values. So we may end up with different representations for
15 /// two constants whose value is `&42`. Furthermore any kind of struct that has padding will
16 /// have arbitrary values within that padding, even if the values of the struct are the same.
18 /// `ValTree` does not have this problem with representation, as it only contains integers or
19 /// lists of (nested) `ValTree`.
20 pub enum ValTree<'tcx> {
21 /// integers, `bool`, `char` are represented as scalars.
22 /// See the `ScalarInt` documentation for how `ScalarInt` guarantees that equal values
23 /// of these types have the same representation.
26 //SliceOrStr(ValSlice<'tcx>),
27 // dont use SliceOrStr for now
28 /// The fields of any kind of aggregate. Structs, tuples and arrays are represented by
29 /// listing their fields' values in order.
31 /// Enums are represented by storing their discriminant as a field, followed by all
32 /// the fields of the variant.
34 /// ZST types are represented as an empty slice.
35 Branch(&'tcx [ValTree<'tcx>]),
38 impl<'tcx> ValTree<'tcx> {
39 pub fn zst() -> Self {
44 pub fn unwrap_leaf(self) -> ScalarInt {
47 _ => bug!("expected leaf, got {:?}", self),
52 pub fn unwrap_branch(self) -> &'tcx [Self] {
54 Self::Branch(branch) => branch,
55 _ => bug!("expected branch, got {:?}", self),
59 pub fn from_raw_bytes<'a>(tcx: TyCtxt<'tcx>, bytes: &'a [u8]) -> Self {
60 let branches = bytes.iter().map(|b| Self::Leaf(ScalarInt::from(*b)));
61 let interned = tcx.arena.alloc_from_iter(branches);
63 Self::Branch(interned)
66 pub fn from_scalar_int(i: ScalarInt) -> Self {
70 pub fn try_to_scalar(self) -> Option<Scalar<AllocId>> {
71 self.try_to_scalar_int().map(Scalar::Int)
74 pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
76 Self::Leaf(s) => Some(s),
77 Self::Branch(_) => None,
81 pub fn try_to_machine_usize(self, tcx: TyCtxt<'tcx>) -> Option<u64> {
82 self.try_to_scalar_int().map(|s| s.try_to_machine_usize(tcx).ok()).flatten()
85 /// Get the values inside the ValTree as a slice of bytes. This only works for
86 /// constants with types &str, &[u8], or [u8; _].
87 pub fn try_to_raw_bytes(self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<&'tcx [u8]> {
89 ty::Ref(_, inner_ty, _) => match inner_ty.kind() {
90 // `&str` can be interpreted as raw bytes
92 // `&[u8]` can be interpreted as raw bytes
93 ty::Slice(slice_ty) if *slice_ty == tcx.types.u8 => {}
94 // other `&_` can't be interpreted as raw bytes
97 // `[u8; N]` can be interpreted as raw bytes
98 ty::Array(array_ty, _) if *array_ty == tcx.types.u8 => {}
99 // Otherwise, type cannot be interpreted as raw bytes
103 Some(tcx.arena.alloc_from_iter(
104 self.unwrap_branch().into_iter().map(|v| v.unwrap_leaf().try_to_u8().unwrap()),