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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use std::marker::PhantomData;
use std::fmt;

use context::Context;
use context;
use object;
use object::{Object, ToObject};
use structs::{self, Struct};

use gccjit_sys::gcc_jit_types::*;

/// A representation of a type, as it is known to the JIT compiler.
/// Types can be created through the Typeable trait or they can
/// be created dynamically by composing Field types.
#[derive(Copy, Clone, Eq, Hash, PartialEq)]
pub struct Type<'ctx> {
    marker: PhantomData<&'ctx Context<'ctx>>,
    ptr: *mut gccjit_sys::gcc_jit_type
}

#[derive(Copy, Clone, Eq, Hash, PartialEq)]
pub struct VectorType<'ctx> {
    marker: PhantomData<&'ctx Context<'ctx>>,
    ptr: *mut gccjit_sys::gcc_jit_vector_type
}

impl<'ctx> VectorType<'ctx> {
    unsafe fn from_ptr(ptr: *mut gccjit_sys::gcc_jit_vector_type) -> VectorType<'ctx> {
        VectorType {
            marker: PhantomData,
            ptr
        }
    }

    pub fn get_element_type(&self) -> Type<'ctx> {
        unsafe {
            from_ptr(gccjit_sys::gcc_jit_vector_type_get_element_type(self.ptr))
        }
    }

    pub fn get_num_units(&self) -> usize {
        unsafe {
            gccjit_sys::gcc_jit_vector_type_get_num_units(self.ptr) as usize
        }
    }
}

#[derive(Copy, Clone, Eq, Hash, PartialEq)]
pub struct FunctionPtrType<'ctx> {
    marker: PhantomData<&'ctx Context<'ctx>>,
    ptr: *mut gccjit_sys::gcc_jit_function_type
}

impl<'ctx> fmt::Debug for FunctionPtrType<'ctx> {
    fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> {
        write!(fmt, "{:?} (", self.get_return_type())?;
        for i in 0..self.get_param_count() {
            write!(fmt, "{:?}, ", self.get_param_type(i))?;
        }
        write!(fmt, ")")
    }
}

impl<'ctx> FunctionPtrType<'ctx> {
    unsafe fn from_ptr(ptr: *mut gccjit_sys::gcc_jit_function_type) -> FunctionPtrType<'ctx> {
        FunctionPtrType {
            marker: PhantomData,
            ptr
        }
    }

    pub fn get_return_type(&self) -> Type<'ctx> {
        unsafe {
            from_ptr(gccjit_sys::gcc_jit_function_type_get_return_type(self.ptr))
        }
    }

    pub fn get_param_count(&self) -> usize {
        unsafe {
            gccjit_sys::gcc_jit_function_type_get_param_count(self.ptr) as usize
        }
    }

    pub fn get_param_type(&self, index: usize) -> Type<'ctx> {
        // TODO: return Option?
        unsafe {
            from_ptr(gccjit_sys::gcc_jit_function_type_get_param_type(self.ptr, index as _))
        }
    }
}

impl<'ctx> ToObject<'ctx> for Type<'ctx> {
    fn to_object(&self) -> Object<'ctx> {
        unsafe {
            let ptr = gccjit_sys::gcc_jit_type_as_object(self.ptr);
            object::from_ptr(ptr)
        }
    }
}

impl<'ctx> fmt::Debug for Type<'ctx> {
    fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> {
        let obj = self.to_object();
        obj.fmt(fmt)
    }
}

impl<'ctx> Type<'ctx> {
    /// Given a type T, creates a type to *T, a pointer to T.
    pub fn make_pointer(self) -> Type<'ctx> {
        unsafe {
            from_ptr(gccjit_sys::gcc_jit_type_get_pointer(self.ptr))
        }
    }

    #[cfg(feature="master")]
    pub fn set_packed(&self) {
        unsafe {
            gccjit_sys::gcc_jit_type_set_packed(self.ptr);
        }
    }

    /// Given a type T, creates a type of const T.
    pub fn make_const(self) -> Type<'ctx> {
        unsafe {
            from_ptr(gccjit_sys::gcc_jit_type_get_const(self.ptr))
        }
    }

    /// Given a type T, creates a new type of volatile T, which
    /// has the semantics of C's volatile.
    pub fn make_volatile(self) -> Type<'ctx> {
        unsafe {
            from_ptr(gccjit_sys::gcc_jit_type_get_volatile(self.ptr))
        }
    }

    /// Given a type T, creates a new type of restrict T, which
    /// has the semantics of C's restrict.
    #[cfg(feature="master")]
    pub fn make_restrict(self) -> Type<'ctx> {
        unsafe {
            from_ptr(gccjit_sys::gcc_jit_type_get_restrict(self.ptr))
        }
    }

    pub fn get_aligned(self, alignment_in_bytes: u64) -> Type<'ctx> {
        unsafe {
            from_ptr(gccjit_sys::gcc_jit_type_get_aligned(self.ptr, alignment_in_bytes as _))
        }
    }

    pub fn dyncast_array(self) -> Option<Type<'ctx>> {
        unsafe {
            let array_type = gccjit_sys::gcc_jit_type_dyncast_array(self.ptr);
            if array_type.is_null() {
                return None;
            }
            Some(from_ptr(array_type))
        }
    }

    pub fn is_bool(self) -> bool {
        unsafe {
            gccjit_sys::gcc_jit_type_is_bool(self.ptr) != 0
        }
    }

    pub fn is_integral(self) -> bool {
        unsafe {
            gccjit_sys::gcc_jit_type_is_integral(self.ptr) != 0
        }
    }

    pub fn dyncast_vector(self) -> Option<VectorType<'ctx>> {
        unsafe {
            let vector_type = gccjit_sys::gcc_jit_type_dyncast_vector(self.ptr);
            if vector_type.is_null() {
                return None;
            }
            Some(VectorType::from_ptr(vector_type))
        }
    }

    pub fn is_struct(self) -> Option<Struct<'ctx>> {
        unsafe {
            let struct_type = gccjit_sys::gcc_jit_type_is_struct(self.ptr);
            if struct_type.is_null() {
                return None;
            }
            Some(structs::from_ptr(struct_type))
        }
    }

    pub fn dyncast_function_ptr_type(self) -> Option<FunctionPtrType<'ctx>> {
        unsafe {
            let function_ptr_type = gccjit_sys::gcc_jit_type_dyncast_function_ptr_type(self.ptr);
            if function_ptr_type.is_null() {
                return None;
            }
            Some(FunctionPtrType::from_ptr(function_ptr_type))
        }
    }

    pub fn get_size(&self) -> u32 {
        unsafe {
            let size = gccjit_sys::gcc_jit_type_get_size(self.ptr);
            assert_ne!(size, -1, "called get_size of unsupported type: {:?}", self);
            size as u32
        }
    }

    pub fn unqualified(&self) -> Type<'ctx> {
        unsafe {
            from_ptr(gccjit_sys::gcc_jit_type_unqualified(self.ptr))
        }
    }

    pub fn get_pointee(&self) -> Option<Type<'ctx>> {
        unsafe {
            let value = gccjit_sys::gcc_jit_type_is_pointer(self.ptr);
            if value.is_null() {
                return None;
            }
            Some(from_ptr(value))
        }
    }

    #[cfg(feature="master")]
    pub fn is_const(&self) -> Option<Type<'ctx>> {
        unsafe {
            let value = gccjit_sys::gcc_jit_type_is_const(self.ptr);
            if value.is_null() {
                return None;
            }
            Some(from_ptr(value))
        }
    }

    pub fn is_compatible_with(&self, typ: Type<'ctx>) -> bool {
        unsafe {
            gccjit_sys::gcc_jit_compatible_types(self.ptr, typ.ptr)
        }
    }
}

/// Typeable is a trait for types that have a corresponding type within
/// gccjit. This library implements this type for a variety of primitive types,
/// but it's also possible to implement this trait for more complex types
/// that will use the API on Context to construct analagous struct/union types.
pub trait Typeable {
    fn get_type<'a, 'ctx>(ctx: &'a Context<'ctx>) -> Type<'a>;
}

macro_rules! typeable_def {
    ($ty:ty, $expr:expr) => {
        impl Typeable for $ty {
            fn get_type<'a, 'ctx>(ctx: &'a Context<'ctx>) -> Type<'a> {
                unsafe {
                    let ctx_ptr = context::get_ptr(ctx);
                    let ptr = gccjit_sys::gcc_jit_context_get_type(ctx_ptr, $expr);
                    #[cfg(debug_assertions)]
                    if let Ok(Some(error)) = ctx.get_last_error() {
                        panic!("{}", error);
                    }
                    from_ptr(ptr)
                }
            }
        }
    }
}

typeable_def!((), GCC_JIT_TYPE_VOID);
typeable_def!(bool, GCC_JIT_TYPE_BOOL);
typeable_def!(char, GCC_JIT_TYPE_CHAR);
typeable_def!(f32, GCC_JIT_TYPE_FLOAT);
typeable_def!(f64, GCC_JIT_TYPE_DOUBLE);
typeable_def!(usize, GCC_JIT_TYPE_SIZE_T);

macro_rules! typeable_int_def {
    ($ty:ty, $num_bytes:expr, $signed:expr) => {
        impl Typeable for $ty {
            fn get_type<'a, 'ctx>(ctx: &'a Context<'ctx>) -> Type<'a> {
                unsafe {
                    let ctx_ptr = context::get_ptr(ctx);
                    let ptr = gccjit_sys::gcc_jit_context_get_int_type(ctx_ptr, $num_bytes, $signed as i32);
                    from_ptr(ptr)
                }
            }
        }
    }
}

typeable_int_def!(i8, 1, true);
typeable_int_def!(u8, 1, false);
typeable_int_def!(i16, 2, true);
typeable_int_def!(u16, 2, false);
typeable_int_def!(i32, 4, true);
typeable_int_def!(u32, 4, false);
typeable_int_def!(i64, 8, true);
typeable_int_def!(u64, 8, false);
//typeable_int_def!(i128, 16, true); // FIXME: unsupported by libgccjit for now.
//typeable_int_def!(u128, 16, false); // FIXME: unsupported by libgccjit for now.

/// Specific implementations of Typeable for *mut T and *const T that
/// represent void* and const void*, respectively. These impls should
/// only be used to expose opaque pointers to gccjit, not to create
/// pointers that are not opaque to gcc. For that, the make_pointer
/// function should be used.
impl<T> Typeable for *mut T {
    fn get_type<'a, 'ctx>(ctx: &'a Context<'ctx>) -> Type<'a> {
        unsafe {
            let ctx_ptr = context::get_ptr(ctx);
            let ptr = gccjit_sys::gcc_jit_context_get_type(ctx_ptr, GCC_JIT_TYPE_VOID_PTR);
            #[cfg(debug_assertions)]
            if let Ok(Some(error)) = ctx.get_last_error() {
                panic!("{}", error);
            }
            from_ptr(ptr)
        }
    }
}

impl<T> Typeable for *const T {
    fn get_type<'a, 'ctx>(ctx: &'a Context<'ctx>) -> Type<'a> {
        unsafe {
            let ctx_ptr = context::get_ptr(ctx);
            let ptr = gccjit_sys::gcc_jit_context_get_type(ctx_ptr, GCC_JIT_TYPE_VOID_PTR);
            #[cfg(debug_assertions)]
            if let Ok(Some(error)) = ctx.get_last_error() {
                panic!("{}", error);
            }
            from_ptr(ptr).make_const()
        }
    }
}

pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_type) -> Type<'ctx> {
    Type {
        marker: PhantomData,
        ptr
    }
}

pub unsafe fn get_ptr<'ctx>(ty: &Type<'ctx>) -> *mut gccjit_sys::gcc_jit_type {
    ty.ptr
}