Skip to main content

gaia_assembler/types/
mod.rs

1//! Gaia Assembler Core Type Definitions
2
3use serde::{Deserialize, Serialize};
4
5/// Gaia Address Spaces
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7pub enum AddressSpace {
8    /// Generic or default address space.
9    Generic,
10    /// Stack or local memory.
11    Local,
12    /// Global memory.
13    Global,
14    /// Shared memory (e.g., GPU LDS).
15    Shared,
16    /// Constant memory.
17    Constant,
18    /// Managed heap (e.g., JVM/CLR).
19    Managed,
20}
21
22/// Gaia Type System
23#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
24pub enum GaiaType {
25    // --- Scalar Types ---
26    /// 1-bit boolean type.
27    Bool,
28    /// 8-bit signed integer.
29    I8,
30    /// 8-bit unsigned integer.
31    U8,
32    /// 16-bit signed integer.
33    I16,
34    /// 16-bit unsigned integer.
35    U16,
36    /// 32-bit signed integer.
37    I32,
38    /// 32-bit unsigned integer.
39    U32,
40    /// 64-bit signed integer.
41    I64,
42    /// 64-bit unsigned integer.
43    U64,
44    /// 16-bit half-precision floating point.
45    F16,
46    /// 32-bit single-precision floating point.
47    F32,
48    /// 64-bit double-precision floating point.
49    F64,
50
51    // --- Composite Types ---
52    /// Pointer type (pointee type, address space).
53    Pointer(Box<GaiaType>, AddressSpace),
54    /// Array type (element type, length).
55    Array(Box<GaiaType>, usize),
56    /// Vector type (element type, count).
57    Vector(Box<GaiaType>, usize),
58    /// Struct type (name/ID).
59    Struct(String),
60    /// String type (managed or raw).
61    String,
62
63    // --- Managed Types ---
64    /// Dynamic object type (e.g., Python/JS).
65    Object,
66    /// Managed class (e.g., JVM/CLR).
67    Class(String),
68    /// Interface or Trait.
69    Interface(String),
70    /// Dynamic or arbitrary type (Variant).
71    Any,
72
73    // --- Domain-Specific Types ---
74    /// Tensor type (element type, shape).
75    /// Shape uses -1 for dynamic dimensions.
76    Tensor(Box<GaiaType>, Vec<isize>),
77
78    // --- Special Types ---
79    /// Void type.
80    Void,
81    /// Opaque type for external references.
82    Opaque(String),
83    /// Function pointer.
84    FunctionPtr(Box<GaiaSignature>),
85}
86
87impl GaiaType {
88    /// Check if this is an integer type.
89    pub fn is_integer(&self) -> bool {
90        match self {
91            Self::I8 | Self::U8 | Self::I16 | Self::U16 | Self::I32 | Self::U32 | Self::I64 | Self::U64 => true,
92            _ => false,
93        }
94    }
95
96    /// Check if this is a floating point type.
97    pub fn is_float(&self) -> bool {
98        match self {
99            Self::F16 | Self::F32 | Self::F64 => true,
100            _ => false,
101        }
102    }
103}
104
105impl std::fmt::Display for GaiaType {
106    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
107        match self {
108            Self::Bool => write!(f, "bool"),
109            Self::I8 => write!(f, "i8"),
110            Self::U8 => write!(f, "u8"),
111            Self::I16 => write!(f, "i16"),
112            Self::U16 => write!(f, "u16"),
113            Self::I32 => write!(f, "i32"),
114            Self::U32 => write!(f, "u32"),
115            Self::I64 => write!(f, "i64"),
116            Self::U64 => write!(f, "u64"),
117            Self::F16 => write!(f, "f16"),
118            Self::F32 => write!(f, "f32"),
119            Self::F64 => write!(f, "f64"),
120            Self::Pointer(ty, _) => write!(f, "*{}", ty),
121            Self::Array(ty, len) => write!(f, "[{}; {}]", ty, len),
122            Self::Vector(ty, count) => write!(f, "vec{}<{}>", count, ty),
123            Self::Struct(name) => write!(f, "struct {}", name),
124            Self::String => write!(f, "string"),
125            Self::Object => write!(f, "object"),
126            Self::Class(name) => write!(f, "class {}", name),
127            Self::Interface(name) => write!(f, "interface {}", name),
128            Self::Any => write!(f, "any"),
129            Self::Tensor(ty, shape) => write!(f, "tensor<{}; {:?}>", ty, shape),
130            Self::Void => write!(f, "void"),
131            Self::Opaque(name) => write!(f, "opaque {}", name),
132            Self::FunctionPtr(sig) => write!(f, "fn({}) -> {}", sig.params.iter().map(|p| p.to_string()).collect::<Vec<_>>().join(", "), sig.return_type),
133        }
134    }
135}
136
137
138
139/// Function signature representing the type of a function.
140#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
141pub struct GaiaSignature {
142    /// The parameter types of the function.
143    pub params: Vec<GaiaType>,
144    /// The return type of the function.
145    pub return_type: GaiaType,
146}
147
148/// Type mapping utilities between UIR and Gaia IR
149pub mod mapping;