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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
//! Datatypes and properties.
//!
//! Julia has an optional typing system. The type information of a [`Value`] is available at
//! runtime. Additionally, a value can hold type information as its contents. For example:
//!
//! ```julia
//! truth = true
//! truthtype = typeof(truth)
//! @assert(truthtype == Bool)
//! @assert(truthtype isa DataType)
//! ```
//!
//! In this module you'll find the [`DataType`] struct which provides access to the properties
//! of its counterpart in Julia and lets you perform a large set of checks to find out its
//! properties. Many of these checks are handled through implementations of the trait
//! [`JuliaTypecheck`]. Some of these checks can be found in this module.
//!
//! [`Value`]: ../struct.Value.html
//! [`DataType`]: struct.DataType.html
//! [`JuliaTypecheck`]: ../../traits/trait.JuliaTypecheck.html

use crate::error::{JlrsError, JlrsResult};
use crate::frame::Output;
use crate::global::Global;
use crate::traits::{private::Internal, Cast, Frame, 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_abstractslot_type, jl_abstractstring_type, jl_any_type, jl_anytuple_type,
    jl_argumenterror_type, jl_bool_type, jl_boundserror_type, jl_builtin_type, jl_char_type,
    jl_code_info_type, jl_code_instance_type, jl_datatype_align, jl_datatype_isinlinealloc,
    jl_datatype_nbits, jl_datatype_nfields, jl_datatype_size, jl_datatype_t, jl_datatype_type,
    jl_emptytuple_type, jl_errorexception_type, jl_expr_type, jl_field_isptr, jl_field_names,
    jl_field_offset, jl_field_size, jl_float16_type, jl_float32_type, jl_float64_type,
    jl_floatingpoint_type, jl_function_type, jl_get_fieldtypes, jl_globalref_type,
    jl_gotonode_type, jl_initerror_type, jl_int16_type, jl_int32_type, jl_int64_type, jl_int8_type,
    jl_intrinsic_type, jl_is_cpointer_type, jl_isbits, jl_lineinfonode_type,
    jl_linenumbernode_type, jl_loaderror_type, jl_method_instance_type, jl_method_type,
    jl_methoderror_type, jl_methtable_type, jl_module_type, jl_namedtuple_typename, jl_new_structv,
    jl_newvarnode_type, jl_nothing_type, jl_number_type, jl_phicnode_type, jl_phinode_type,
    jl_pinode_type, jl_quotenode_type, jl_signed_type, jl_simplevector_type, jl_slotnumber_type,
    jl_ssavalue_type, jl_string_type, jl_svec_data, jl_svec_len, jl_symbol_type, jl_task_type,
    jl_tvar_type, jl_typedslot_type, jl_typeerror_type, jl_typemap_entry_type,
    jl_typemap_level_type, jl_typename_str, jl_typename_type, jl_typeofbottom_type, jl_uint16_type,
    jl_uint32_type, jl_uint64_type, jl_uint8_type, jl_undefvarerror_type, jl_unionall_type,
    jl_uniontype_type, jl_upsilonnode_type, jl_voidpointer_type, jl_weakref_type,
};
use std::ffi::CStr;
use std::fmt::{Debug, Formatter, Result as FmtResult};
use std::marker::PhantomData;
/// Julia type information. You can acquire a [`Value`]'s datatype by by calling
/// [`Value::datatype`]. This struct implements [`JuliaTypecheck`] and [`Cast`]. It can be used in
/// combination with [`DataType::is`] and [`Value::is`]; if the check returns `true` the [`Value`]
///  can be cast to `DataType`:
///
/// ```
/// # use jlrs::prelude::*;
/// # use jlrs::util::JULIA;
/// # fn main() {
/// # JULIA.with(|j| {
/// # let mut julia = j.borrow_mut();
/// julia.frame(2, |global, frame| {
///     let val = Value::new(frame, 1u8)?;
///     let typeof_func = Module::core(global).function("typeof")?;
///     let ty_val = typeof_func.call1(frame, val)?.unwrap();
///     assert!(ty_val.is::<DataType>());
///     assert!(ty_val.cast::<DataType>().is_ok());
///     Ok(())
/// }).unwrap();
/// # });
/// # }
/// ```
///
/// [`JuliaTypecheck`]: ../../traits/trait.JuliaTypecheck.html
/// [`Cast`]: ../../traits/trait.Cast.html
/// [`DataType::is`]: ../datatype/struct.DataType.html#method.is
/// [`Value::is`]: ../struct.Value.html#method.is
/// [`Value`]: ../struct.Value.html
/// [`Value::datatype`]: ../struct.Value.html#method.datatype
/// [`Value::cast`]: ../struct.Value.html#method.cast
/// [`JuliaTypecheck`]: ../../traits/trait.JuliaTypecheck.html
/// [`DataType::is`]: struct.Datatype.html#method.is
/// [`Value::is`]: struct.Datatype.html#method.is
#[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
    }

    /// Performs the given typecheck.
    pub fn is<T: JuliaTypecheck>(self) -> bool {
        unsafe { T::julia_typecheck(self) }
    }

    /// Returns the size of a value of this type in bytes.
    pub fn size(self) -> i32 {
        unsafe { jl_datatype_size(self.0) }
    }

    /// Returns the alignment of a value of this type in bytes.
    pub fn align(self) -> u16 {
        unsafe { jl_datatype_align(self.0) }
    }

    /// Returns the size of a value of this type in bits.
    pub fn nbits(self) -> i32 {
        unsafe { jl_datatype_nbits(self.0) }
    }

    /// Returns the number of fields of a value of this type.
    pub fn nfields(self) -> u32 {
        unsafe { jl_datatype_nfields(self.0) }
    }

    /// Returns true if a value of this type stores its data inline.
    pub fn isinlinealloc(self) -> bool {
        unsafe { jl_datatype_isinlinealloc(self.0) != 0 }
    }

    /// Returns the name of this type.
    pub fn name(self) -> &'frame str {
        unsafe {
            let name = jl_typename_str(self.ptr().cast());
            CStr::from_ptr(name).to_str().unwrap()
        }
    }

    /// Returns the `TypeName` of this type.
    pub fn type_name(self) -> TypeName<'frame> {
        unsafe { TypeName::wrap((&*self.ptr()).name) }
    }

    /// Returns the field names of this type as a slice of `Symbol`s. These symbols can be used
    /// to access their fields with [`Value::get_field`].
    ///
    /// [`Value::get_field`]: struct.Value.html#method.get_field
    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)
        }
    }

    /// Returns the field types of this type.
    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)
        }
    }

    /// Returns the size of the field at position `idx` in this type.
    pub fn field_size(self, idx: usize) -> u32 {
        unsafe { jl_field_size(self.ptr(), idx as _) }
    }

    /// Returns the offset where the field at position `idx` is stored.
    pub fn field_offset(self, idx: usize) -> u32 {
        unsafe { jl_field_offset(self.ptr(), idx as _) }
    }

    /// Returns true if the field at position `idx` is a pointer.
    pub fn is_pointer_field(self, idx: usize) -> bool {
        unsafe { jl_field_isptr(self.ptr(), idx as _) }
    }

    /// Returns true if this type is a bits-type.
    pub fn isbits(self) -> bool {
        unsafe { jl_isbits(self.ptr().cast()) }
    }

    /// Returns the supertype of this type.
    pub fn super_type(self) -> Option<Self> {
        unsafe {
            let sup = (&*self.ptr()).super_;
            if sup.is_null() {
                None
            } else {
                Some(DataType::wrap(sup))
            }
        }
    }

    /// Returns the type parameters of this type.
    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))
        }
    }

    /// Returns the instance if this type is a singleton.
    pub fn instance(self) -> Option<Value<'frame, 'static>> {
        unsafe {
            let instance = (&*self.ptr()).instance;
            if instance.is_null() {
                None
            } else {
                Some(Value::wrap(instance))
            }
        }
    }

    /// Returns the number of initialized fields.
    pub fn n_initialized(self) -> i32 {
        unsafe { (&*self.ptr()).ninitialized }
    }

    /// Returns the hash of this type.
    pub fn hash(self) -> u32 {
        unsafe { (&*self.ptr()).hash }
    }

    /// Returns true if this is an abstract type.
    pub fn is_abstract(self) -> bool {
        unsafe { (&*self.ptr()).abstract_ != 0 }
    }

    /// Returns true if this is a mutable type.
    pub fn mutable(self) -> bool {
        unsafe { (&*self.ptr()).mutabl != 0 }
    }

    /// Returns true if one or more of the type parameters has not been set.
    pub fn has_free_type_vars(self) -> bool {
        unsafe { (&*self.ptr()).hasfreetypevars != 0 }
    }

    /// Returns true if this type can have instances
    pub fn is_concrete_type(self) -> bool {
        unsafe { (&*self.ptr()).isconcretetype != 0 }
    }

    /// Returns true if this type is a dispatch, or leaf, tuple type.
    pub fn is_dispatch_tuple(self) -> bool {
        unsafe { (&*self.ptr()).isdispatchtuple != 0 }
    }

    /// Returns true if one or more fields require zero-initialization.
    pub fn zeroinit(self) -> bool {
        unsafe { (&*self.ptr()).zeroinit != 0 }
    }

    /// If false, no value will have this type.
    pub fn has_concrete_subtype(self) -> bool {
        unsafe { (&*self.ptr()).has_concrete_subtype != 0 }
    }

    /// Convert `self` to a `Value`.
    /// Convert `self` to a `Value`.
    pub fn as_value(self) -> Value<'frame, 'static> {
        self.into()
    }

    /// Intantiate this `DataType` with the given values. The type must be concrete. One free slot
    /// on the GC stack is required for this function to succeed, returns an error if no slot is
    /// available or if the type is not concrete.
    pub fn instantiate<'fr, 'value, 'borrow, F, V>(
        self,
        frame: &mut F,
        mut values: V,
    ) -> JlrsResult<Value<'fr, 'borrow>>
    where
        F: Frame<'fr>,
        V: AsMut<[Value<'value, 'borrow>]>,
    {
        unsafe {
            if !self.is::<Concrete>() {
                Err(JlrsError::NotConcrete(self.name().into()))?;
            }

            let values = values.as_mut();
            let value = jl_new_structv(self.ptr(), values.as_mut_ptr().cast(), values.len() as _);
            frame.protect(value, Internal).map_err(Into::into)
        }
    }

    /// Intantiate this `DataType` with the given values using the given output. The type must be
    /// concrete, returns an error if the type is not concrete.
    pub fn instantiate_output<'output, 'fr, 'value, 'borrow, F, V>(
        self,
        frame: &mut F,
        output: Output<'output>,
        values: V,
    ) -> JlrsResult<Value<'output, 'borrow>>
    where
        F: Frame<'fr>,
        V: AsMut<[Value<'value, 'borrow>]>,
    {
        Value::instantiate_output(frame, output, self, values)
    }
}

impl<'base> DataType<'base> {
    /// The type of the bottom type, `Union{}`.
    pub fn typeofbottom_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_typeofbottom_type) }
    }

    /// The type `DataType`.
    pub fn datatype_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_datatype_type) }
    }

    /// The type `Union`.
    pub fn uniontype_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_uniontype_type) }
    }

    /// The type `UnionAll`.
    pub fn unionall_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_unionall_type) }
    }

    /// The type `TypeVar`.
    pub fn tvar_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_tvar_type) }
    }

    /// The type `Any`.
    pub fn any_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_any_type) }
    }

    /// The type `TypeName`.
    pub fn typename_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_typename_type) }
    }

    /// The type `Symbol`.
    pub fn symbol_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_symbol_type) }
    }

    /// The type `Core.SSAValue`.
    pub fn ssavalue_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_ssavalue_type) }
    }

    /// The type `Slot`.
    pub fn abstractslot_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_abstractslot_type) }
    }

    /// The type `SlotNumber`.
    pub fn slotnumber_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_slotnumber_type) }
    }

    /// The type `TypedSlot`.
    pub fn typedslot_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_typedslot_type) }
    }

    /// The type `SimpleVector`, or `SVec`.
    pub fn simplevector_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_simplevector_type) }
    }

    /// The type `Tuple`.
    pub fn anytuple_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_anytuple_type) }
    }

    /// The type `Tuple`.
    pub fn tuple_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_anytuple_type) }
    }

    /// The type of an empty tuple.
    pub fn emptytuple_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_emptytuple_type) }
    }

    /// The type `Function`.
    pub fn function_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_function_type) }
    }

    /// The type `Builtin`.
    pub fn builtin_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_builtin_type) }
    }

    /// The type `MethodInstance`.
    pub fn method_instance_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_method_instance_type) }
    }

    /// The type `CodeInstance`.
    pub fn code_instance_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_code_instance_type) }
    }

    /// The type `CodeInfo`.
    pub fn code_info_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_code_info_type) }
    }

    /// The type `Method`.
    pub fn method_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_method_type) }
    }

    /// The type `Module`.
    pub fn module_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_module_type) }
    }

    /// The type `WeakRef`.
    pub fn weakref_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_weakref_type) }
    }

    /// The type `AbstractString`.
    pub fn abstractstring_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_abstractstring_type) }
    }

    /// The type `String`.
    pub fn string_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_string_type) }
    }

    /// The type `ErrorException`.
    pub fn errorexception_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_errorexception_type) }
    }

    /// The type `ArgumentError`.
    pub fn argumenterror_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_argumenterror_type) }
    }

    /// The type `LoadError`.
    pub fn loaderror_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_loaderror_type) }
    }

    /// The type `InitError`.
    pub fn initerror_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_initerror_type) }
    }

    /// The type `TypeError`.
    pub fn typeerror_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_typeerror_type) }
    }

    /// The type `MethodError`.
    pub fn methoderror_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_methoderror_type) }
    }

    /// The type `UndefVarError`.
    pub fn undefvarerror_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_undefvarerror_type) }
    }

    /// The type `LineInfoNode`.
    pub fn lineinfonode_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_lineinfonode_type) }
    }

    /// The type `BoundsError`.
    pub fn boundserror_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_boundserror_type) }
    }

    /// The type `Bool`.
    pub fn bool_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_bool_type) }
    }

    /// The type `Char`.
    pub fn char_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_char_type) }
    }

    /// The type `Int8`.
    pub fn int8_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_int8_type) }
    }

    /// The type `UInt8`.
    pub fn uint8_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_uint8_type) }
    }

    /// The type `Int16`.
    pub fn int16_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_int16_type) }
    }

    /// The type `UInt16`.
    pub fn uint16_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_uint16_type) }
    }

    /// The type `Int32`.
    pub fn int32_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_int32_type) }
    }

    /// The type `UInt32`.
    pub fn uint32_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_uint32_type) }
    }

    /// The type `Int64`.
    pub fn int64_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_int64_type) }
    }

    /// The type `UInt64`.
    pub fn uint64_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_uint64_type) }
    }

    /// The type `Float16`.
    pub fn float16_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_float16_type) }
    }

    /// The type `Float32`.
    pub fn float32_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_float32_type) }
    }

    /// The type `Float64`.
    pub fn float64_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_float64_type) }
    }

    /// The type `AbstractFloat`.
    pub fn floatingpoint_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_floatingpoint_type) }
    }

    /// The type `Number`.
    pub fn number_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_number_type) }
    }

    /// The type `Nothing`.
    pub fn nothing_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_nothing_type) }
    }

    /// The type `Signed`.
    pub fn signed_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_signed_type) }
    }

    /// The type `Ptr{Nothing}`.
    pub fn voidpointer_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_voidpointer_type) }
    }

    /// The type `Task`.
    pub fn task_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_task_type) }
    }

    /// The type `Expr`.
    pub fn expr_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_expr_type) }
    }

    /// The type `GlobalRef`.
    pub fn globalref_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_globalref_type) }
    }

    /// The type `LineNumberNode`.
    pub fn linenumbernode_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_linenumbernode_type) }
    }

    /// The type `GotoNode`.
    pub fn gotonode_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_gotonode_type) }
    }

    /// The type `PhiNode`.
    pub fn phinode_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_phinode_type) }
    }

    /// The type `PiNode`.
    pub fn pinode_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_pinode_type) }
    }

    /// The type `PhiCNode`.
    pub fn phicnode_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_phicnode_type) }
    }

    /// The type `UpsilonNode`.
    pub fn upsilonnode_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_upsilonnode_type) }
    }

    /// The type `QuoteNode`.
    pub fn quotenode_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_quotenode_type) }
    }

    /// The type `NewVarNode`.
    pub fn newvarnode_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_newvarnode_type) }
    }

    /// The type `Intrinsic`.
    pub fn intrinsic_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_intrinsic_type) }
    }

    /// The type `MethodTable`.
    pub fn methtable_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_methtable_type) }
    }

    /// The type `TypeMapLevel`.
    pub fn typemap_level_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_typemap_level_type) }
    }

    /// The type `TypeMapEntry`.
    pub fn typemap_entry_type(_: Global<'base>) -> Self {
        unsafe { Self::wrap(jl_typemap_entry_type) }
    }
}

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);

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is a tuple.
pub struct Any;
impl_julia_typecheck!(Any, jl_any_type);

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is a named tuple.
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);

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// the fields of a value of this type can be modified.
pub struct Mutable;

unsafe impl JuliaTypecheck for Mutable {
    unsafe fn julia_typecheck(t: DataType) -> bool {
        (&*t.ptr()).mutabl != 0
    }
}

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// the datatype is a mutable datatype.
pub struct MutableDatatype;

unsafe impl JuliaTypecheck for MutableDatatype {
    unsafe fn julia_typecheck(t: DataType) -> bool {
        DataType::julia_typecheck(t) && (&*t.ptr()).mutabl != 0
    }
}

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// the fields of a value of this type cannot be modified.
pub struct Immutable;

unsafe impl JuliaTypecheck for Immutable {
    unsafe fn julia_typecheck(t: DataType) -> bool {
        (&*t.ptr()).mutabl == 0
    }
}

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// the datatype is an immutable datatype.
pub struct ImmutableDatatype;

unsafe impl JuliaTypecheck for ImmutableDatatype {
    unsafe fn julia_typecheck(t: DataType) -> bool {
        DataType::julia_typecheck(t) && (&*t.ptr()).mutabl == 0
    }
}

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is a primitive type.
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
    }
}

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is a struct type.
pub struct StructType;

unsafe impl JuliaTypecheck for StructType {
    unsafe fn julia_typecheck(t: DataType) -> bool {
        !t.is_abstract() && !t.is::<PrimitiveType>()
    }
}

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is a struct type.
pub struct Singleton;

unsafe impl JuliaTypecheck for Singleton {
    unsafe fn julia_typecheck(t: DataType) -> bool {
        t.instance().is_some()
    }
}

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is a slot.
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
    }
}

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is a global reference.
pub struct GlobalRef;
impl_julia_typecheck!(GlobalRef, jl_globalref_type);

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is a Goto node.
pub struct GotoNode;
impl_julia_typecheck!(GotoNode, jl_gotonode_type);

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is a Pi node.
pub struct PiNode;
impl_julia_typecheck!(PiNode, jl_pinode_type);

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is a Phi node.
pub struct PhiNode;
impl_julia_typecheck!(PhiNode, jl_phinode_type);

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is a PhiC node.
pub struct PhiCNode;
impl_julia_typecheck!(PhiCNode, jl_phicnode_type);

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is an Upsilon node.
pub struct UpsilonNode;
impl_julia_typecheck!(UpsilonNode, jl_upsilonnode_type);

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is a Quote node.
pub struct QuoteNode;
impl_julia_typecheck!(QuoteNode, jl_quotenode_type);

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is an NewVar node.
pub struct NewVarNode;
impl_julia_typecheck!(NewVarNode, jl_newvarnode_type);

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is a Line node.
pub struct LineNode;
impl_julia_typecheck!(LineNode, jl_linenumbernode_type);

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is code info.
pub struct CodeInfo;
impl_julia_typecheck!(CodeInfo, jl_code_info_type);

impl_julia_typecheck!(String, jl_string_type);

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is a pointer.
pub struct Pointer;
unsafe impl JuliaTypecheck for Pointer {
    unsafe fn julia_typecheck(t: DataType) -> bool {
        jl_is_cpointer_type(t.ptr().cast())
    }
}

/// A typecheck that can be used in combination with `DataType::is`. This method returns true if
/// a value of this type is an intrinsic.
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
    }
}