Skip to main content

luaur_code_gen/records/
ir_const.rs

1use crate::enums::ir_const_kind::IrConstKind;
2
3#[derive(Clone, Copy)]
4#[repr(C)]
5pub struct IrConst {
6    pub kind: IrConstKind,
7    pub value: IrConstValue,
8}
9
10#[derive(Clone, Copy)]
11#[repr(C)]
12pub union IrConstValue {
13    pub value_int: i32,
14    pub value_int64: i64,
15    pub value_uint: u32,
16    pub value_double: f64,
17    pub value_tag: u8,
18}
19
20impl Default for IrConst {
21    fn default() -> Self {
22        Self {
23            kind: IrConstKind::Int,
24            value: IrConstValue { value_int: 0 },
25        }
26    }
27}
28
29impl core::fmt::Debug for IrConst {
30    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
31        let mut s = f.debug_struct("IrConst");
32        s.field("kind", &self.kind);
33        unsafe {
34            match self.kind {
35                IrConstKind::Int => s.field("value_int", &self.value.value_int),
36                IrConstKind::Int64 => s.field("value_int64", &self.value.value_int64),
37                IrConstKind::Uint | IrConstKind::Import => {
38                    s.field("value_uint", &self.value.value_uint)
39                }
40                IrConstKind::Double => s.field("value_double", &self.value.value_double),
41                IrConstKind::Tag => s.field("value_tag", &self.value.value_tag),
42            };
43        }
44        s.finish()
45    }
46}
47
48impl core::fmt::Debug for IrConstValue {
49    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
50        f.debug_struct("IrConstValue")
51            .field("bits", unsafe { &self.value_int64 })
52            .finish()
53    }
54}