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::{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}
48
49/// Signedness of a numeric type
50#[derive_group_for_ast]
51pub enum Signedness {
52 /// Signed type (`i32`, `i64`, ...)
53 Signed,
54 /// Unsigned type (`u32`, `u64`, ...)
55 Unsigned,
56}
57
58/// Describes a Rust integer type (`u64`, `i32`, ...)
59#[derive_group_for_ast]
60pub struct IntKind {
61 /// Size of this integer type
62 pub size: IntSize,
63 /// Whether this integer type is signed or unsigned
64 pub signedness: Signedness,
65}
66
67/// Float types
68#[derive_group_for_ast]
69pub enum FloatKind {
70 /// 16 bits float
71 F16,
72 /// 32 bits float
73 F32,
74 /// 64 bits float
75 F64,
76 /// 128 bits float
77 F128,
78}
79
80/// Rust literal
81#[derive_group_for_ast]
82pub enum Literal {
83 /// String literal
84 String(Symbol),
85 /// Character literal
86 Char(char),
87 /// Boolean literal
88 Bool(bool),
89 /// Integer literal
90 Int {
91 /// Value as u128
92 value: Symbol,
93 /// True if `-`
94 negative: bool,
95 /// Rust int type description (size + signedness)
96 kind: IntKind,
97 },
98 /// Float literal
99 Float {
100 /// Value as a string
101 value: Symbol,
102 /// True if `-`
103 negative: bool,
104 /// Size
105 kind: FloatKind,
106 },
107}