Skip to main content

llvm_ir/
types.rs

1//! IR type system: integer, float, pointer, vector, struct, array, and function types.
2
3use crate::context::TypeId;
4
5/// Public API for `TypeData`.
6#[derive(Clone, Debug, PartialEq, Eq, Hash)]
7pub enum TypeData {
8    /// `Void` variant.
9    Void,
10    /// `Integer` variant.
11    Integer(u32),
12    /// `Float` variant.
13    Float(FloatKind),
14    /// `Pointer` variant.
15    Pointer,
16    /// `Array` variant.
17    Array {
18        element: TypeId,
19        len: u64,
20    },
21    /// `Vector` variant.
22    Vector {
23        element: TypeId,
24        len: u32,
25        scalable: bool,
26    },
27    /// `Struct` variant.
28    Struct(StructType),
29    /// `Function` variant.
30    Function(FunctionType),
31    /// `Label` variant.
32    Label,
33    /// `Metadata` variant.
34    Metadata,
35}
36
37/// Public API for `FloatKind`.
38#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
39pub enum FloatKind {
40    /// `Half` variant.
41    Half,
42    /// `BFloat` variant.
43    BFloat,
44    /// `Single` variant.
45    Single,
46    /// `Double` variant.
47    Double,
48    /// `Fp128` variant.
49    Fp128,
50    /// `X86Fp80` variant.
51    X86Fp80,
52}
53
54/// Public API for `StructType`.
55#[derive(Clone, Debug, PartialEq, Eq, Hash)]
56pub struct StructType {
57    /// Public API for `name`.
58    pub name: Option<String>,
59    /// Public API for `fields`.
60    pub fields: Vec<TypeId>,
61    /// Public API for `packed`.
62    pub packed: bool,
63}
64
65/// Public API for `FunctionType`.
66#[derive(Clone, Debug, PartialEq, Eq, Hash)]
67pub struct FunctionType {
68    /// Public API for `ret`.
69    pub ret: TypeId,
70    /// Public API for `params`.
71    pub params: Vec<TypeId>,
72    /// Public API for `variadic`.
73    pub variadic: bool,
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79    use crate::context::Context;
80
81    #[test]
82    fn type_data_eq() {
83        let a = TypeData::Integer(32);
84        let b = TypeData::Integer(32);
85        assert_eq!(a, b);
86        let c = TypeData::Integer(64);
87        assert_ne!(a, c);
88    }
89
90    #[test]
91    fn struct_type_eq() {
92        let ctx = Context::new();
93        let s1 = StructType {
94            name: None,
95            fields: vec![ctx.i32_ty, ctx.i64_ty],
96            packed: false,
97        };
98        let s2 = StructType {
99            name: None,
100            fields: vec![ctx.i32_ty, ctx.i64_ty],
101            packed: false,
102        };
103        assert_eq!(s1, s2);
104    }
105}