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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use crate::error::{JlrsError, JlrsResult};
use crate::traits::{Cast, JuliaTypecheck};
use crate::value::symbol::Symbol;
use crate::value::type_name::TypeName;
use crate::value::Value;
use crate::{impl_julia_type, impl_julia_typecheck, impl_valid_layout};
use jl_sys::{
jl_any_type, jl_code_info_type, jl_datatype_align, jl_datatype_isinlinealloc,
jl_datatype_nbits, jl_datatype_nfields, jl_datatype_size, jl_datatype_t, jl_datatype_type,
jl_field_isptr, jl_field_names, jl_field_offset, jl_field_size, jl_get_fieldtypes,
jl_globalref_type, jl_gotonode_type, jl_intrinsic_type, jl_is_cpointer_type, jl_isbits,
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_svec_data, jl_svec_len, jl_typedslot_type, jl_typename_str, jl_upsilonnode_type,
};
use std::ffi::CStr;
use std::fmt::{Debug, Formatter, Result as FmtResult};
use std::marker::PhantomData;
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
#[repr(transparent)]
pub struct DataType<'frame>(*mut jl_datatype_t, PhantomData<&'frame ()>);
impl<'frame> DataType<'frame> {
pub(crate) unsafe fn wrap(datatype: *mut jl_datatype_t) -> Self {
DataType(datatype, PhantomData)
}
#[doc(hidden)]
pub unsafe fn ptr(self) -> *mut jl_datatype_t {
self.0
}
pub fn is<T: JuliaTypecheck>(self) -> bool {
unsafe { T::julia_typecheck(self) }
}
pub fn size(self) -> i32 {
unsafe { jl_datatype_size(self.0) }
}
pub fn align(self) -> u16 {
unsafe { jl_datatype_align(self.0) }
}
pub fn nbits(self) -> i32 {
unsafe { jl_datatype_nbits(self.0) }
}
pub fn nfields(self) -> u32 {
unsafe { jl_datatype_nfields(self.0) }
}
pub fn isinlinealloc(self) -> bool {
unsafe { jl_datatype_isinlinealloc(self.0) != 0 }
}
pub fn name(self) -> &'frame str {
unsafe {
let name = jl_typename_str(self.ptr().cast());
CStr::from_ptr(name).to_str().unwrap()
}
}
pub fn type_name(self) -> TypeName<'frame> {
unsafe { TypeName::wrap((&*self.ptr()).name) }
}
pub fn field_names(self) -> &'frame [Symbol<'frame>] {
unsafe {
let field_names = jl_field_names(self.ptr().cast());
let len = jl_svec_len(field_names);
let items = jl_svec_data(field_names);
std::slice::from_raw_parts(items.cast(), len)
}
}
pub fn field_types(self) -> &'frame [Value<'frame, 'static>] {
unsafe {
let field_types = jl_get_fieldtypes(self.ptr());
let len = jl_svec_len(field_types);
let items = jl_svec_data(field_types);
std::slice::from_raw_parts(items.cast(), len)
}
}
pub fn field_size(self, idx: usize) -> u32 {
unsafe { jl_field_size(self.ptr(), idx as _) }
}
pub fn field_offset(self, idx: usize) -> u32 {
unsafe { jl_field_offset(self.ptr(), idx as _) }
}
pub fn is_pointer_field(self, idx: usize) -> bool {
unsafe { jl_field_isptr(self.ptr(), idx as _) }
}
pub fn isbits(self) -> bool {
unsafe { jl_isbits(self.ptr().cast()) }
}
pub fn super_type(self) -> Option<Self> {
unsafe {
let sup = (&*self.ptr()).super_;
if sup.is_null() {
None
} else {
Some(DataType::wrap(sup))
}
}
}
pub fn parameters(self) -> &'frame [Value<'frame, 'static>] {
unsafe {
let params = (&*self.ptr()).parameters;
std::slice::from_raw_parts(jl_svec_data(params).cast(), jl_svec_len(params))
}
}
pub fn instance(self) -> Option<Value<'frame, 'static>> {
unsafe {
let instance = (&*self.ptr()).instance;
if instance.is_null() {
None
} else {
Some(Value::wrap(instance))
}
}
}
pub fn n_initialized(self) -> i32 {
unsafe { (&*self.ptr()).ninitialized }
}
pub fn hash(self) -> u32 {
unsafe { (&*self.ptr()).hash }
}
pub fn is_abstract(self) -> bool {
unsafe { (&*self.ptr()).abstract_ != 0 }
}
pub fn mutable(self) -> bool {
unsafe { (&*self.ptr()).mutabl != 0 }
}
pub fn has_free_type_vars(self) -> bool {
unsafe { (&*self.ptr()).hasfreetypevars != 0 }
}
pub fn is_concrete_type(self) -> bool {
unsafe { (&*self.ptr()).isconcretetype != 0 }
}
pub fn is_dispatch_tuple(self) -> bool {
unsafe { (&*self.ptr()).isdispatchtuple != 0 }
}
pub fn zeroinit(self) -> bool {
unsafe { (&*self.ptr()).zeroinit != 0 }
}
pub fn has_concrete_subtype(self) -> bool {
unsafe { (&*self.ptr()).has_concrete_subtype != 0 }
}
}
impl<'frame> Into<Value<'frame, 'static>> for DataType<'frame> {
fn into(self) -> Value<'frame, 'static> {
unsafe { Value::wrap(self.ptr().cast()) }
}
}
impl<'frame, 'data> Debug for DataType<'frame> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.debug_tuple("DataType").field(&self.name()).finish()
}
}
unsafe impl<'frame, 'data> Cast<'frame, 'data> for DataType<'frame> {
type Output = Self;
fn cast(value: Value<'frame, 'data>) -> JlrsResult<Self::Output> {
if value.is::<Self::Output>() {
return unsafe { Ok(Self::cast_unchecked(value)) };
}
Err(JlrsError::NotADataType)?
}
unsafe fn cast_unchecked(value: Value<'frame, 'data>) -> Self::Output {
DataType::wrap(value.ptr().cast())
}
}
impl_julia_type!(DataType<'frame>, jl_datatype_type, 'frame);
impl_valid_layout!(DataType<'frame>, 'frame);
pub struct Any;
impl_julia_typecheck!(Any, jl_any_type);
pub struct NamedTuple;
unsafe impl JuliaTypecheck for NamedTuple {
unsafe fn julia_typecheck(t: DataType) -> bool {
(&*t.ptr()).name == jl_namedtuple_typename
}
}
impl_julia_typecheck!(DataType<'frame>, jl_datatype_type, 'frame);
pub struct Mutable;
unsafe impl JuliaTypecheck for Mutable {
unsafe fn julia_typecheck(t: DataType) -> bool {
(&*t.ptr()).mutabl != 0
}
}
pub struct MutableDatatype;
unsafe impl JuliaTypecheck for MutableDatatype {
unsafe fn julia_typecheck(t: DataType) -> bool {
DataType::julia_typecheck(t) && (&*t.ptr()).mutabl != 0
}
}
pub struct Immutable;
unsafe impl JuliaTypecheck for Immutable {
unsafe fn julia_typecheck(t: DataType) -> bool {
(&*t.ptr()).mutabl == 0
}
}
pub struct ImmutableDatatype;
unsafe impl JuliaTypecheck for ImmutableDatatype {
unsafe fn julia_typecheck(t: DataType) -> bool {
DataType::julia_typecheck(t) && (&*t.ptr()).mutabl == 0
}
}
pub struct PrimitiveType;
unsafe impl JuliaTypecheck for PrimitiveType {
unsafe fn julia_typecheck(t: DataType) -> bool {
t.is::<Immutable>() && !(&*t.ptr()).layout.is_null() && t.nfields() == 0 && t.size() > 0
}
}
pub struct StructType;
unsafe impl JuliaTypecheck for StructType {
unsafe fn julia_typecheck(t: DataType) -> bool {
!t.is_abstract() && !t.is::<PrimitiveType>()
}
}
pub struct Singleton;
unsafe impl JuliaTypecheck for Singleton {
unsafe fn julia_typecheck(t: DataType) -> bool {
t.instance().is_some()
}
}
pub struct Slot;
unsafe impl JuliaTypecheck for Slot {
unsafe fn julia_typecheck(t: DataType) -> bool {
t.ptr() == jl_slotnumber_type || t.ptr() == 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 JuliaTypecheck for Pointer {
unsafe fn julia_typecheck(t: DataType) -> bool {
jl_is_cpointer_type(t.ptr().cast())
}
}
pub struct Intrinsic;
impl_julia_typecheck!(Intrinsic, jl_intrinsic_type);
pub struct Concrete;
unsafe impl JuliaTypecheck for Concrete {
unsafe fn julia_typecheck(t: DataType) -> bool {
(&*t.ptr()).isconcretetype != 0
}
}