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
//! Construct Julia type objects from Rust types.

use std::{any::TypeId, ffi::c_void, marker::PhantomData, ptr::NonNull};

use fnv::FnvHashMap;
use jl_sys::{
    jl_array_typename, jl_bool_type, jl_bottom_type, jl_char_type, jl_float32_type,
    jl_float64_type, jl_int16_type, jl_int32_type, jl_int64_type, jl_int8_type, jl_pointer_type,
    jl_uint16_type, jl_uint32_type, jl_uint64_type, jl_uint8_type, jl_uniontype_type, jl_value_t,
    jl_voidpointer_type,
};

use super::abstract_types::AnyType;
use crate::{
    convert::to_symbol::ToSymbol,
    data::managed::{
        datatype::DataType,
        type_var::TypeVar,
        union::Union,
        union_all::UnionAll,
        value::{Value, ValueData},
        Managed,
    },
    gc_safe::{GcSafeOnceLock, GcSafeRwLock},
    memory::target::Target,
    prelude::Tuple,
    private::Private,
};

static CONSTRUCTED_TYPE_CACHE: GcSafeOnceLock<ConstructedTypes> = GcSafeOnceLock::new();

pub(crate) unsafe fn init_constructed_type_cache() {
    CONSTRUCTED_TYPE_CACHE.set(ConstructedTypes::new()).ok();
}

/// Associate a Julia type object with a Rust type.
///
/// Safety:
///
/// `ConstructType::construct_type` must either return a valid type object, or an instance of an
/// isbits type which is immediately used as a type parameter of another constructed type.
pub unsafe trait ConstructType: Sized {
    /// `Self`, but with all lifetimes set to `'static`.
    type Static: 'static + ConstructType;

    /// Indicates whether the type might be cacheable.
    ///
    /// If set to `false`, `construct_type` will never try to cache or look up the
    /// constructed type. It should be set to `false` if the constructed type isn't a `DataType`.
    const CACHEABLE: bool = true;

    /// Returns the `TypeId` of this type.
    #[inline]
    fn type_id() -> TypeId {
        TypeId::of::<Self::Static>()
    }

    /// Construct the type object and try to cache the result. If a cached entry is available, it
    /// is returned.
    #[inline]
    fn construct_type<'target, T>(target: T) -> ValueData<'target, 'static, T>
    where
        T: Target<'target>,
    {
        if Self::CACHEABLE {
            unsafe {
                CONSTRUCTED_TYPE_CACHE
                    .get_unchecked()
                    .find_or_construct::<Self, _>(target)
            }
        } else {
            Self::construct_type_uncached(target)
        }
    }

    /// Constructs the type object associated with this type.
    fn construct_type_uncached<'target, T>(target: T) -> ValueData<'target, 'static, T>
    where
        T: Target<'target>;

    /// Returns the base type object associated with this type.
    ///
    /// The base type object is the type object without any types applied to it. If there is no
    /// such type object, e.g. when `Self` is a value type, `None` is returned. The base type must
    /// assumed to be globally rooted.
    fn base_type<'target, T>(target: &T) -> Option<Value<'target, 'static>>
    where
        T: Target<'target>;
}

macro_rules! impl_construct_julia_type_constant {
    ($ty:ty, $const_ty:ty) => {
        unsafe impl<const N: $const_ty> ConstructType for $ty {
            type Static = $ty;

            const CACHEABLE: bool = false;

            #[inline]
            fn construct_type_uncached<'target, T>(target: T) -> ValueData<'target, 'static, T>
            where
                T: Target<'target>,
            {
                Value::new(target, N)
            }

            #[inline]
            fn base_type<'target, T>(_target: &T) -> Option<Value<'target, 'static>>
            where
                T: Target<'target>,
            {
                None
            }
        }
    };
}

macro_rules! impl_construct_julia_type_primitive {
    ($ty:ty, $jl_ty:ident) => {
        unsafe impl ConstructType for $ty {
            type Static = $ty;

            const CACHEABLE: bool = false;

            #[inline]
            fn construct_type_uncached<'target, T>(target: T) -> ValueData<'target, 'static, T>
            where
                T: Target<'target>,
            {
                unsafe {
                    let ptr = NonNull::new_unchecked($jl_ty.cast::<jl_value_t>());
                    target.data_from_ptr(ptr, Private)
                }
            }

            #[inline]
            fn base_type<'target, T>(_target: &T) -> Option<Value<'target, 'static>>
            where
                T: Target<'target>,
            {
                unsafe {
                    let ptr = NonNull::new_unchecked($jl_ty.cast::<jl_value_t>());
                    Some(
                        <Value as $crate::data::managed::private::ManagedPriv>::wrap_non_null(
                            ptr,
                            $crate::private::Private,
                        ),
                    )
                }
            }
        }
    };
}

/// Constant `i8`.
pub struct ConstantI8<const N: i8>;
impl_construct_julia_type_constant!(ConstantI8<N>, i8);

/// Constant `i16`.
pub struct ConstantI16<const N: i16>;
impl_construct_julia_type_constant!(ConstantI16<N>, i16);

/// Constant `i32`.
pub struct ConstantI32<const N: i32>;
impl_construct_julia_type_constant!(ConstantI32<N>, i32);

/// Constant `i64`.
pub struct ConstantI64<const N: i64>;
impl_construct_julia_type_constant!(ConstantI64<N>, i64);

/// Constant `isize`.
pub struct ConstantIsize<const N: isize>;
impl_construct_julia_type_constant!(ConstantIsize<N>, isize);

/// Constant `isize`.
pub struct ConstantSize<const N: usize>;
unsafe impl<const N: usize> ConstructType for ConstantSize<N> {
    type Static = ConstantSize<N>;

    const CACHEABLE: bool = false;

    #[inline]
    fn construct_type_uncached<'target, T>(target: T) -> ValueData<'target, 'static, T>
    where
        T: Target<'target>,
    {
        Value::new(target, N as isize)
    }

    #[inline]
    fn base_type<'target, T>(_target: &T) -> Option<Value<'target, 'static>>
    where
        T: Target<'target>,
    {
        None
    }
}

// TODO: UInt8 and/or Int8 objects are statically allocated in Julia and can be cached.
/// Constant `u8`.
pub struct ConstantU8<const N: u8>;
impl_construct_julia_type_constant!(ConstantU8<N>, u8);

/// Constant `u16`.
pub struct ConstantU16<const N: u16>;
impl_construct_julia_type_constant!(ConstantU16<N>, u16);

/// Constant `u32`.
pub struct ConstantU32<const N: u32>;
impl_construct_julia_type_constant!(ConstantU32<N>, u32);

/// Constant `u64`.
pub struct ConstantU64<const N: u64>;
impl_construct_julia_type_constant!(ConstantU64<N>, u64);

/// Constant `usize`.
pub struct ConstantUsize<const N: usize>;
impl_construct_julia_type_constant!(ConstantUsize<N>, usize);

/// Constant `bool`.
pub struct ConstantBool<const N: bool>;
impl_construct_julia_type_constant!(ConstantBool<N>, bool);

/// Constant `char`.
pub struct ConstantChar<const N: char>;
impl_construct_julia_type_constant!(ConstantChar<N>, char);

/// Constant string. Not a type constructor, but can be used to implement multi-character type
/// vars.
pub trait ConstantStr: 'static {
    /// The string constant.
    const STR: &'static str;
}

/// The name of a `TypeVar`.
pub struct Name<const N: char>;

/// Trait to set the name of`TypeVar`.
///
/// Implemented by [`Name`], [`ConstantChar`], and implementations of [`ConstantStr`].
pub trait TypeVarName: 'static {
    /// The type returned by `name`.
    type Sym: ToSymbol;

    // Returns the name.
    fn name() -> Self::Sym;
}

impl<const N: char> TypeVarName for Name<N> {
    type Sym = String;

    #[inline]
    fn name() -> Self::Sym {
        String::from(N)
    }
}

impl<const N: char> TypeVarName for ConstantChar<N> {
    type Sym = String;

    #[inline]
    fn name() -> Self::Sym {
        String::from(N)
    }
}

impl<T: ConstantStr> TypeVarName for T {
    type Sym = &'static str;

    #[inline]
    fn name() -> Self::Sym {
        Self::STR
    }
}

/// Construct a new `TypeVar` from the provided type parameters.
pub struct TypeVarConstructor<
    N: TypeVarName,
    U: ConstructType = AnyType,
    L: ConstructType = BottomType,
> {
    _name: PhantomData<N>,
    _upper: PhantomData<U>,
    _lower: PhantomData<L>,
}

unsafe impl<N: TypeVarName, U: ConstructType, L: ConstructType> ConstructType
    for TypeVarConstructor<N, U, L>
{
    type Static = TypeVarConstructor<N, U::Static, L::Static>;

    fn construct_type_uncached<'target, T>(target: T) -> ValueData<'target, 'static, T>
    where
        T: Target<'target>,
    {
        target
            .with_local_scope::<_, _, 2>(|target, mut frame| {
                let upper_bound = U::construct_type(&mut frame);
                let lower_bound = L::construct_type(&mut frame);
                unsafe {
                    Ok(TypeVar::new_unchecked(
                        &target,
                        N::name(),
                        Some(lower_bound),
                        Some(upper_bound),
                    )
                    .as_value()
                    .root(target))
                }
            })
            .unwrap()
    }

    #[inline]
    fn base_type<'target, T>(_target: &T) -> Option<Value<'target, 'static>>
    where
        T: Target<'target>,
    {
        None
    }
}

/// Construct a new `Array` type from the provided type parameters.
pub struct ArrayTypeConstructor<T: ConstructType, N: ConstructType> {
    _type: PhantomData<T>,
    _rank: PhantomData<N>,
}

unsafe impl<T: ConstructType, N: ConstructType> ConstructType for ArrayTypeConstructor<T, N> {
    type Static = ArrayTypeConstructor<T::Static, N::Static>;

    fn construct_type_uncached<'target, Tgt>(target: Tgt) -> ValueData<'target, 'static, Tgt>
    where
        Tgt: Target<'target>,
    {
        unsafe {
            target
                .with_local_scope::<_, _, 3>(|target, mut frame| {
                    let ty_param = T::construct_type(&mut frame);
                    let rank_param = N::construct_type(&mut frame);
                    let params = [ty_param, rank_param];
                    let applied = Self::base_type(&frame)
                        .unwrap_unchecked()
                        .apply_type_unchecked(&mut frame, params);

                    Ok(UnionAll::rewrap(
                        target,
                        applied.cast_unchecked::<DataType>(),
                    ))
                })
                .unwrap_unchecked()
        }
    }

    #[inline]
    fn base_type<'target, Tgt>(_target: &Tgt) -> Option<Value<'target, 'static>>
    where
        Tgt: Target<'target>,
    {
        unsafe {
            let wrapper =
                NonNull::new_unchecked(NonNull::new_unchecked(jl_array_typename).as_ref().wrapper);
            // let ptr = NonNull::new_unchecked(jl_array_type.cast::<jl_value_t>());
            Some(
                <Value as crate::data::managed::private::ManagedPriv>::wrap_non_null(
                    wrapper,
                    crate::private::Private,
                ),
            )
        }
    }
}

pub type RankedArrayType<T, const N: isize> = ArrayTypeConstructor<T, ConstantIsize<N>>;

/// Construct a new `Union` type from the provided type parameters. Larger unions can be built
/// by nesting `UnionTypeConstructor`.
pub struct UnionTypeConstructor<L: ConstructType, R: ConstructType> {
    _l: PhantomData<L>,
    _r: PhantomData<R>,
}

unsafe impl<L: ConstructType, R: ConstructType> ConstructType for UnionTypeConstructor<L, R> {
    type Static = UnionTypeConstructor<L::Static, R::Static>;

    fn construct_type_uncached<'target, Tgt>(target: Tgt) -> ValueData<'target, 'static, Tgt>
    where
        Tgt: Target<'target>,
    {
        target
            .with_local_scope::<_, _, 2>(|target, mut frame| {
                let l = L::construct_type(&mut frame);
                let r = R::construct_type(&mut frame);

                unsafe { Ok(Union::new_unchecked(target, [l, r])) }
            })
            .unwrap()
    }

    #[inline]
    fn base_type<'target, Tgt>(_target: &Tgt) -> Option<Value<'target, 'static>>
    where
        Tgt: Target<'target>,
    {
        unsafe {
            let ptr = NonNull::new_unchecked(jl_uniontype_type.cast::<jl_value_t>());
            Some(
                <Value as crate::data::managed::private::ManagedPriv>::wrap_non_null(
                    ptr,
                    crate::private::Private,
                ),
            )
        }
    }
}

pub struct BottomType;

unsafe impl ConstructType for BottomType {
    type Static = BottomType;

    fn construct_type_uncached<'target, Tgt>(target: Tgt) -> ValueData<'target, 'static, Tgt>
    where
        Tgt: Target<'target>,
    {
        unsafe {
            let ptr = NonNull::new_unchecked(jl_bottom_type.cast::<jl_value_t>());
            target.data_from_ptr(ptr, Private)
        }
    }

    #[inline]
    fn base_type<'target, Tgt>(_target: &Tgt) -> Option<Value<'target, 'static>>
    where
        Tgt: Target<'target>,
    {
        unsafe {
            let ptr = NonNull::new_unchecked(jl_bottom_type.cast::<jl_value_t>());
            Some(
                <Value as crate::data::managed::private::ManagedPriv>::wrap_non_null(
                    ptr,
                    crate::private::Private,
                ),
            )
        }
    }
}

impl_construct_julia_type_primitive!(u8, jl_uint8_type);
impl_construct_julia_type_primitive!(u16, jl_uint16_type);
impl_construct_julia_type_primitive!(u32, jl_uint32_type);
impl_construct_julia_type_primitive!(u64, jl_uint64_type);

#[cfg(target_pointer_width = "64")]
impl_construct_julia_type_primitive!(usize, jl_uint64_type);
#[cfg(target_pointer_width = "32")]
impl_construct_julia_type_primitive!(usize, jl_uint32_type);

impl_construct_julia_type_primitive!(i8, jl_int8_type);
impl_construct_julia_type_primitive!(i16, jl_int16_type);
impl_construct_julia_type_primitive!(i32, jl_int32_type);
impl_construct_julia_type_primitive!(i64, jl_int64_type);

#[cfg(target_pointer_width = "64")]
impl_construct_julia_type_primitive!(isize, jl_int64_type);
#[cfg(target_pointer_width = "32")]
impl_construct_julia_type_primitive!(isize, jl_int32_type);

impl_construct_julia_type_primitive!(f32, jl_float32_type);
impl_construct_julia_type_primitive!(f64, jl_float64_type);

impl_construct_julia_type_primitive!(bool, jl_bool_type);
impl_construct_julia_type_primitive!(char, jl_char_type);

impl_construct_julia_type_primitive!(*mut c_void, jl_voidpointer_type);

unsafe impl<U: ConstructType> ConstructType for *mut U {
    type Static = *mut U::Static;

    fn construct_type_uncached<'target, Tgt>(target: Tgt) -> ValueData<'target, 'static, Tgt>
    where
        Tgt: Target<'target>,
    {
        target
            .with_local_scope::<_, _, 1>(|target, mut frame| {
                let ty = U::construct_type(&mut frame);
                unsafe {
                    Ok(UnionAll::pointer_type(&frame)
                        .as_value()
                        .apply_type_unchecked(target, [ty]))
                }
            })
            .unwrap()
    }

    #[inline]
    fn base_type<'target, Tgt>(_target: &Tgt) -> Option<Value<'target, 'static>>
    where
        Tgt: Target<'target>,
    {
        unsafe {
            let ptr = NonNull::new_unchecked(jl_pointer_type.cast::<jl_value_t>());
            Some(
                <Value as crate::data::managed::private::ManagedPriv>::wrap_non_null(
                    ptr,
                    crate::private::Private,
                ),
            )
        }
    }
}

struct ConstructedTypes {
    data: GcSafeRwLock<FnvHashMap<TypeId, Value<'static, 'static>>>,
}

impl ConstructedTypes {
    fn new() -> Self {
        ConstructedTypes {
            data: GcSafeRwLock::new(FnvHashMap::default()),
        }
    }

    #[inline]
    fn find_or_construct<'target, T: ConstructType, Tgt: Target<'target>>(
        &self,
        target: Tgt,
    ) -> ValueData<'target, 'static, Tgt> {
        let tid = T::type_id();

        {
            if let Some(res) = self.data.read().get(&tid).copied() {
                return res.root(target);
            }
        }

        do_construct::<T, _>(target, self, tid)
    }
}

#[inline(never)]
fn do_construct<'target, T: ConstructType, Tgt: Target<'target>>(
    target: Tgt,
    ct: &ConstructedTypes,
    tid: TypeId,
) -> ValueData<'target, 'static, Tgt> {
    unsafe {
        target
            .with_local_scope::<_, _, 1>(|target, mut frame| {
                let ty = T::construct_type_uncached(&mut frame);

                if ty.is::<DataType>() {
                    let dt = ty.cast_unchecked::<DataType>();
                    if !dt.has_free_type_vars() && (!dt.is::<Tuple>() || dt.is_concrete_type()) {
                        ct.data.write().insert(tid, ty.leak().as_value());
                    }
                }

                Ok(ty.root(target))
            })
            .unwrap_unchecked()
    }
}

unsafe impl Sync for ConstructedTypes {}
unsafe impl Send for ConstructedTypes {}