1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! LLVM types

use crate::{
    context::Context,
    ir::{Type, TypeLike},
};
use mlir_sys::{
    mlirLLVMArrayTypeGet, mlirLLVMFunctionTypeGet, mlirLLVMPointerTypeGet,
    mlirLLVMStructTypeLiteralGet, mlirLLVMVoidTypeGet,
};

// TODO Check if the `llvm` dialect is loaded on use of those functions.

/// Creates an LLVM array type.
pub fn array(r#type: Type, len: u32) -> Type {
    unsafe { Type::from_raw(mlirLLVMArrayTypeGet(r#type.to_raw(), len)) }
}

/// Creates an LLVM function type.
pub fn function<'c>(
    result: Type<'c>,
    arguments: &[Type<'c>],
    variadic_arguments: bool,
) -> Type<'c> {
    unsafe {
        Type::from_raw(mlirLLVMFunctionTypeGet(
            result.to_raw(),
            arguments.len() as isize,
            arguments as *const _ as *const _,
            variadic_arguments,
        ))
    }
}

/// Creates an LLVM opaque pointer type at address space 0.
#[deprecated(
    since = "0.11.0",
    note = "please use the pointer method, all pointers are opaque in LLVM 18"
)]
pub fn opaque_pointer(context: &Context) -> Type {
    pointer(context, 0)
}

/// Creates an LLVM pointer type in the given address space.
pub fn pointer(context: &Context, address_space: u32) -> Type {
    unsafe { Type::from_raw(mlirLLVMPointerTypeGet(context.to_raw(), address_space)) }
}

/// Creates an LLVM struct type.
pub fn r#struct<'c>(context: &'c Context, fields: &[Type<'c>], packed: bool) -> Type<'c> {
    unsafe {
        Type::from_raw(mlirLLVMStructTypeLiteralGet(
            context.to_raw(),
            fields.len() as isize,
            fields as *const _ as *const _,
            packed,
        ))
    }
}

/// Creates an LLVM void type.
pub fn void(context: &Context) -> Type {
    unsafe { Type::from_raw(mlirLLVMVoidTypeGet(context.to_raw())) }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{dialect, ir::r#type::IntegerType};

    fn create_context() -> Context {
        let context = Context::new();

        dialect::DialectHandle::llvm().register_dialect(&context);
        context.get_or_load_dialect("llvm");

        context
    }

    #[test]
    fn pointer() {
        let context = create_context();

        assert_eq!(
            super::pointer(&context, 0),
            Type::parse(&context, "!llvm.ptr").unwrap()
        );
    }

    #[test]
    fn pointer_with_address_space() {
        let context = create_context();

        assert_eq!(
            super::pointer(&context, 4),
            Type::parse(&context, "!llvm.ptr<4>").unwrap()
        );
    }

    #[test]
    fn void() {
        let context = create_context();

        assert_eq!(
            super::void(&context),
            Type::parse(&context, "!llvm.void").unwrap()
        );
    }

    #[test]
    fn array() {
        let context = create_context();
        let i32 = IntegerType::new(&context, 32).into();

        assert_eq!(
            super::array(i32, 4),
            Type::parse(&context, "!llvm.array<4 x i32>").unwrap()
        );
    }

    #[test]
    fn function() {
        let context = create_context();
        let i8 = IntegerType::new(&context, 8).into();
        let i32 = IntegerType::new(&context, 32).into();
        let i64 = IntegerType::new(&context, 64).into();

        assert_eq!(
            super::function(i8, &[i32, i64], false),
            Type::parse(&context, "!llvm.func<i8 (i32, i64)>").unwrap()
        );
    }

    #[test]
    fn r#struct() {
        let context = create_context();
        let i32 = IntegerType::new(&context, 32).into();
        let i64 = IntegerType::new(&context, 64).into();

        assert_eq!(
            super::r#struct(&context, &[i32, i64], false),
            Type::parse(&context, "!llvm.struct<(i32, i64)>").unwrap()
        );
    }

    #[test]
    fn packed_struct() {
        let context = create_context();
        let i32 = IntegerType::new(&context, 32).into();
        let i64 = IntegerType::new(&context, 64).into();

        assert_eq!(
            super::r#struct(&context, &[i32, i64], true),
            Type::parse(&context, "!llvm.struct<packed (i32, i64)>").unwrap()
        );
    }
}