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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
use jl_sys::{
jl_code_info_type, jl_globalref_type, jl_gotonode_type, jl_intrinsic_type,
jl_linenumbernode_type, jl_namedtuple_typename, jl_newvarnode_type, jl_phicnode_type,
jl_phinode_type, jl_pinode_type, jl_quotenode_type, jl_slotnumber_type, jl_string_type,
jl_typedslot_type, jl_upsilonnode_type,
};
use crate::{
convert::into_julia::IntoJulia,
memory::global::Global,
private::Private,
wrappers::ptr::Wrapper,
wrappers::ptr::{
datatype::DataType, private::WrapperPriv, type_name::TypeName, union_all::UnionAll,
},
};
use std::ffi::c_void;
pub unsafe trait Typecheck {
#[doc(hidden)]
fn typecheck(t: DataType) -> bool;
}
#[doc(hidden)]
#[macro_export]
macro_rules! impl_julia_typecheck {
($type:ty, $jl_type:expr, $($lt:lifetime),+) => {
unsafe impl<$($lt),+> crate::layout::typecheck::Typecheck for $type {
#[inline(always)]
fn typecheck(t: $crate::wrappers::ptr::datatype::DataType) -> bool {
unsafe {
<$crate::wrappers::ptr::datatype::DataType as $crate::wrappers::ptr::private::WrapperPriv>::unwrap(t, crate::private::Private) == $jl_type
}
}
}
};
($type:ty, $jl_type:expr) => {
unsafe impl crate::layout::typecheck::Typecheck for $type {
#[inline(always)]
fn typecheck(t: $crate::wrappers::ptr::datatype::DataType) -> bool {
unsafe {
<$crate::wrappers::ptr::datatype::DataType as $crate::wrappers::ptr::private::WrapperPriv>::unwrap(t, crate::private::Private) == $jl_type
}
}
}
};
($type:ty) => {
unsafe impl crate::layout::typecheck::Typecheck for $type {
#[inline(always)]
fn typecheck(t: crate::wrappers::ptr::datatype::DataType) -> bool {
unsafe {
let global = $crate::memory::global::Global::new();
<$crate::wrappers::ptr::datatype::DataType as $crate::wrappers::ptr::private::WrapperPriv>::unwrap(t, crate::private::Private) == <$type as $crate::convert::into_julia::IntoJulia>::julia_type(global).ptr()
}
}
}
};
}
impl_julia_typecheck!(i8);
impl_julia_typecheck!(i16);
impl_julia_typecheck!(i32);
impl_julia_typecheck!(i64);
impl_julia_typecheck!(isize);
impl_julia_typecheck!(u8);
impl_julia_typecheck!(u16);
impl_julia_typecheck!(u32);
impl_julia_typecheck!(u64);
impl_julia_typecheck!(usize);
impl_julia_typecheck!(f32);
impl_julia_typecheck!(f64);
impl_julia_typecheck!(bool);
impl_julia_typecheck!(char);
impl_julia_typecheck!(*mut c_void);
unsafe impl<T: IntoJulia> Typecheck for *mut T {
fn typecheck(t: DataType) -> bool {
unsafe {
let global = Global::new();
let ptr_tname = TypeName::of_pointer(global);
if t.type_name().wrapper_unchecked() != ptr_tname {
return false;
}
let params = t
.parameters()
.wrapper_unchecked()
.unrestricted_data()
.as_slice();
let inner_ty = T::julia_type(global);
if params[0].value_unchecked() != inner_ty.value_unchecked() {
return false;
}
true
}
}
}
pub struct Type;
unsafe impl Typecheck for Type {
#[inline(always)]
fn typecheck(t: DataType) -> bool {
t.as_value().is_kind()
}
}
pub struct Bits;
unsafe impl Typecheck for Bits {
#[inline(always)]
fn typecheck(t: DataType) -> bool {
t.is_bits()
}
}
pub struct Abstract;
unsafe impl Typecheck for Abstract {
#[inline(always)]
fn typecheck(t: DataType) -> bool {
t.is_abstract()
}
}
pub struct AbstractRef;
unsafe impl Typecheck for AbstractRef {
fn typecheck(t: DataType) -> bool {
unsafe {
t.type_name().wrapper_unchecked()
== UnionAll::ref_type(Global::new())
.body()
.value_unchecked()
.cast_unchecked::<DataType>()
.type_name()
.wrapper_unchecked()
}
}
}
pub struct VecElement;
unsafe impl Typecheck for VecElement {
#[inline(always)]
fn typecheck(t: DataType) -> bool {
unsafe { t.type_name().wrapper_unchecked() == TypeName::of_vecelement(Global::new()) }
}
}
pub struct TypeType;
unsafe impl Typecheck for TypeType {
fn typecheck(t: DataType) -> bool {
unsafe {
t.type_name().wrapper_unchecked()
== UnionAll::type_type(Global::new())
.body()
.value_unchecked()
.cast_unchecked::<DataType>()
.type_name()
.wrapper_unchecked()
}
}
}
pub struct DispatchTuple;
unsafe impl Typecheck for DispatchTuple {
#[inline(always)]
fn typecheck(t: DataType) -> bool {
t.is_dispatch_tuple()
}
}
pub struct NamedTuple;
unsafe impl Typecheck for NamedTuple {
#[inline(always)]
fn typecheck(t: DataType) -> bool {
unsafe { t.unwrap_non_null(Private).as_ref().name == jl_namedtuple_typename }
}
}
pub struct Mutable;
unsafe impl Typecheck for Mutable {
#[inline(always)]
fn typecheck(t: DataType) -> bool {
t.mutable()
}
}
pub struct Immutable;
unsafe impl Typecheck for Immutable {
#[inline(always)]
fn typecheck(t: DataType) -> bool {
!t.mutable()
}
}
pub struct PrimitiveType;
unsafe impl Typecheck for PrimitiveType {
fn typecheck(t: DataType) -> bool {
unsafe {
t.is::<Immutable>()
&& !t.unwrap_non_null(Private).as_ref().layout.is_null()
&& t.n_fields() == 0
&& t.size() > 0
}
}
}
pub struct StructType;
unsafe impl Typecheck for StructType {
#[inline(always)]
fn typecheck(t: DataType) -> bool {
!t.is_abstract() && !t.is::<PrimitiveType>()
}
}
pub struct Singleton;
unsafe impl Typecheck for Singleton {
#[inline(always)]
fn typecheck(t: DataType) -> bool {
!t.instance().is_undefined()
}
}
pub struct Slot;
unsafe impl Typecheck for Slot {
#[inline(always)]
fn typecheck(t: DataType) -> bool {
unsafe { t.unwrap(Private) == jl_slotnumber_type || t.unwrap(Private) == jl_typedslot_type }
}
}
pub struct GlobalRef;
impl_julia_typecheck!(GlobalRef, jl_globalref_type);
pub struct GotoNode;
impl_julia_typecheck!(GotoNode, jl_gotonode_type);
pub struct PiNode;
impl_julia_typecheck!(PiNode, jl_pinode_type);
pub struct PhiNode;
impl_julia_typecheck!(PhiNode, jl_phinode_type);
pub struct PhiCNode;
impl_julia_typecheck!(PhiCNode, jl_phicnode_type);
pub struct UpsilonNode;
impl_julia_typecheck!(UpsilonNode, jl_upsilonnode_type);
pub struct QuoteNode;
impl_julia_typecheck!(QuoteNode, jl_quotenode_type);
pub struct NewVarNode;
impl_julia_typecheck!(NewVarNode, jl_newvarnode_type);
pub struct LineNode;
impl_julia_typecheck!(LineNode, jl_linenumbernode_type);
pub struct CodeInfo;
impl_julia_typecheck!(CodeInfo, jl_code_info_type);
impl_julia_typecheck!(String, jl_string_type);
pub struct Pointer;
unsafe impl Typecheck for Pointer {
#[inline(always)]
fn typecheck(t: DataType) -> bool {
unsafe { t.type_name().wrapper_unchecked() == TypeName::of_pointer(Global::new()) }
}
}
pub struct LLVMPointer;
unsafe impl Typecheck for LLVMPointer {
#[inline(always)]
fn typecheck(t: DataType) -> bool {
unsafe { t.type_name().wrapper_unchecked() == TypeName::of_llvmpointer(Global::new()) }
}
}
pub struct Intrinsic;
impl_julia_typecheck!(Intrinsic, jl_intrinsic_type);
pub struct Concrete;
unsafe impl Typecheck for Concrete {
#[inline(always)]
fn typecheck(t: DataType) -> bool {
t.is_concrete_type()
}
}