Skip to main content

hax_rust_engine/ast/
literals.rs

1//! Literal and numeric type kinds used in constant expressions.
2
3use crate::symbol::Symbol;
4use hax_rust_engine_macros::*;
5
6/// Size of an integer type
7#[derive_group_for_ast]
8pub enum IntSize {
9    /// 8 bits integer type
10    S8,
11    /// 16 bits integer type
12    S16,
13    /// 32 bits integer type
14    S32,
15    /// 64 bits integer type
16    S64,
17    /// 128 bits integer type
18    S128,
19    /// Pointer-sized integer type
20    SSize,
21}
22
23use hax_frontend_exporter::{FloatTy, IntTy, UintTy};
24impl From<&IntTy> for IntSize {
25    fn from(value: &IntTy) -> Self {
26        match value {
27            IntTy::I128 => Self::S128,
28            IntTy::I64 => Self::S64,
29            IntTy::I32 => Self::S32,
30            IntTy::I16 => Self::S16,
31            IntTy::I8 => Self::S8,
32            IntTy::Isize => Self::SSize,
33        }
34    }
35}
36impl From<&UintTy> for IntSize {
37    fn from(value: &UintTy) -> Self {
38        match value {
39            UintTy::U128 => Self::S128,
40            UintTy::U64 => Self::S64,
41            UintTy::U32 => Self::S32,
42            UintTy::U16 => Self::S16,
43            UintTy::U8 => Self::S8,
44            UintTy::Usize => Self::SSize,
45        }
46    }
47}
48impl From<&IntTy> for IntKind {
49    fn from(value: &IntTy) -> Self {
50        IntKind {
51            size: value.into(),
52            signedness: Signedness::Signed,
53        }
54    }
55}
56impl From<&UintTy> for IntKind {
57    fn from(value: &UintTy) -> Self {
58        IntKind {
59            size: value.into(),
60            signedness: Signedness::Unsigned,
61        }
62    }
63}
64impl From<&FloatTy> for FloatKind {
65    fn from(value: &FloatTy) -> Self {
66        match value {
67            FloatTy::F128 => Self::F128,
68            FloatTy::F64 => Self::F64,
69            FloatTy::F32 => Self::F32,
70            FloatTy::F16 => Self::F16,
71        }
72    }
73}
74
75/// Signedness of a numeric type
76#[derive_group_for_ast]
77pub enum Signedness {
78    /// Signed type (`i32`, `i64`, ...)
79    Signed,
80    /// Unsigned type (`u32`, `u64`, ...)
81    Unsigned,
82}
83
84/// Describes a Rust integer type (`u64`, `i32`, ...)
85#[derive_group_for_ast]
86pub struct IntKind {
87    /// Size of this integer type
88    pub size: IntSize,
89    /// Whether this integer type is signed or unsigned
90    pub signedness: Signedness,
91}
92
93/// Float types
94#[derive_group_for_ast]
95pub enum FloatKind {
96    /// 16 bits float
97    F16,
98    /// 32 bits float
99    F32,
100    /// 64 bits float
101    F64,
102    /// 128 bits float
103    F128,
104}
105
106/// Rust literal
107#[derive_group_for_ast]
108pub enum Literal {
109    /// String literal
110    String(Symbol),
111    /// Character literal
112    Char(char),
113    /// Boolean literal
114    Bool(bool),
115    /// Integer literal
116    Int {
117        /// Value as u128
118        value: Symbol,
119        /// True if `-`
120        negative: bool,
121        /// Rust int type description (size + signedness)
122        kind: IntKind,
123    },
124    /// Float literal
125    Float {
126        /// Value as a string
127        value: Symbol,
128        /// True if `-`
129        negative: bool,
130        /// Size
131        kind: FloatKind,
132    },
133}