-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
jltypes.c
2306 lines (2128 loc) · 85.6 KB
/
jltypes.c
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is a part of Julia. License is MIT: https://julialang.org/license
/*
Types
. type union, type cache, and instantiation
. builtin type definitions
*/
#include <stdlib.h>
#include <string.h>
#ifdef _OS_WINDOWS_
#include <malloc.h>
#endif
#include "julia.h"
#include "julia_internal.h"
#include "builtin_proto.h"
#include "julia_assert.h"
#ifdef __cplusplus
extern "C" {
#endif
jl_datatype_t *jl_any_type;
jl_unionall_t *jl_type_type;
jl_typename_t *jl_type_typename;
jl_methtable_t *jl_type_type_mt;
jl_datatype_t *jl_typename_type;
jl_datatype_t *jl_sym_type;
jl_datatype_t *jl_symbol_type;
jl_datatype_t *jl_ssavalue_type;
jl_datatype_t *jl_abstractslot_type;
jl_datatype_t *jl_slotnumber_type;
jl_datatype_t *jl_typedslot_type;
jl_datatype_t *jl_simplevector_type;
jl_typename_t *jl_tuple_typename;
jl_datatype_t *jl_anytuple_type;
jl_datatype_t *jl_emptytuple_type;
jl_unionall_t *jl_anytuple_type_type;
jl_typename_t *jl_vecelement_typename;
jl_unionall_t *jl_vararg_type;
jl_typename_t *jl_vararg_typename;
jl_datatype_t *jl_tvar_type;
jl_datatype_t *jl_uniontype_type;
jl_datatype_t *jl_unionall_type;
jl_datatype_t *jl_datatype_type;
jl_datatype_t *jl_function_type;
jl_datatype_t *jl_builtin_type;
jl_datatype_t *jl_typeofbottom_type;
jl_value_t *jl_bottom_type;
jl_unionall_t *jl_abstractarray_type;
jl_unionall_t *jl_densearray_type;
jl_datatype_t *jl_bool_type;
jl_datatype_t *jl_char_type;
jl_datatype_t *jl_int8_type;
jl_datatype_t *jl_uint8_type;
jl_datatype_t *jl_int16_type;
jl_datatype_t *jl_uint16_type;
jl_datatype_t *jl_int32_type;
jl_datatype_t *jl_uint32_type;
jl_datatype_t *jl_int64_type;
jl_datatype_t *jl_uint64_type;
jl_datatype_t *jl_float16_type;
jl_datatype_t *jl_float32_type;
jl_datatype_t *jl_float64_type;
jl_datatype_t *jl_floatingpoint_type;
jl_datatype_t *jl_number_type;
jl_datatype_t *jl_signed_type;
JL_DLLEXPORT jl_value_t *jl_emptytuple;
jl_svec_t *jl_emptysvec;
jl_value_t *jl_nothing;
JL_DLLEXPORT jl_value_t *jl_true;
JL_DLLEXPORT jl_value_t *jl_false;
jl_unionall_t *jl_typetype_type;
jl_unionall_t *jl_array_type;
jl_typename_t *jl_array_typename;
jl_value_t *jl_array_uint8_type;
jl_value_t *jl_array_any_type;
jl_value_t *jl_array_symbol_type;
jl_value_t *jl_array_int32_type;
jl_datatype_t *jl_weakref_type;
jl_datatype_t *jl_abstractstring_type;
jl_datatype_t *jl_string_type;
jl_datatype_t *jl_expr_type;
jl_datatype_t *jl_globalref_type;
jl_datatype_t *jl_linenumbernode_type;
jl_datatype_t *jl_gotonode_type;
jl_datatype_t *jl_pinode_type;
jl_datatype_t *jl_phinode_type;
jl_datatype_t *jl_phicnode_type;
jl_datatype_t *jl_upsilonnode_type;
jl_datatype_t *jl_quotenode_type;
jl_datatype_t *jl_newvarnode_type;
jl_datatype_t *jl_intrinsic_type;
jl_datatype_t *jl_method_type;
jl_datatype_t *jl_methtable_type;
jl_datatype_t *jl_typemap_entry_type;
jl_datatype_t *jl_typemap_level_type;
jl_datatype_t *jl_method_instance_type;
jl_datatype_t *jl_code_instance_type;
jl_datatype_t *jl_code_info_type;
jl_datatype_t *jl_module_type;
jl_datatype_t *jl_errorexception_type;
jl_datatype_t *jl_argumenterror_type;
jl_datatype_t *jl_typeerror_type;
jl_datatype_t *jl_methoderror_type;
jl_datatype_t *jl_loaderror_type;
jl_datatype_t *jl_initerror_type;
jl_datatype_t *jl_undefvarerror_type;
jl_datatype_t *jl_lineinfonode_type;
jl_unionall_t *jl_ref_type;
jl_unionall_t *jl_pointer_type;
jl_typename_t *jl_pointer_typename;
jl_datatype_t *jl_void_type;
jl_datatype_t *jl_voidpointer_type;
jl_typename_t *jl_namedtuple_typename;
jl_unionall_t *jl_namedtuple_type;
jl_value_t *jl_an_empty_vec_any;
jl_value_t *jl_an_empty_string;
jl_value_t *jl_stackovf_exception;
#ifdef SEGV_EXCEPTION
jl_value_t *jl_segv_exception;
#endif
JL_DLLEXPORT jl_value_t *jl_diverror_exception;
JL_DLLEXPORT jl_value_t *jl_domain_exception;
JL_DLLEXPORT jl_value_t *jl_overflow_exception;
JL_DLLEXPORT jl_value_t *jl_undefref_exception;
jl_value_t *jl_interrupt_exception;
jl_datatype_t *jl_boundserror_type;
jl_value_t *jl_memory_exception;
jl_value_t *jl_readonlymemory_exception;
jl_cgparams_t jl_default_cgparams = {1, 1, 1, 1, 0, NULL, NULL, NULL, NULL, NULL};
// --- type properties and predicates ---
static int typeenv_has(jl_typeenv_t *env, jl_tvar_t *v)
{
while (env != NULL) {
if (env->var == v)
return 1;
env = env->prev;
}
return 0;
}
static int has_free_typevars(jl_value_t *v, jl_typeenv_t *env)
{
if (jl_typeis(v, jl_tvar_type)) {
return !typeenv_has(env, (jl_tvar_t*)v);
}
if (jl_is_uniontype(v))
return has_free_typevars(((jl_uniontype_t*)v)->a, env) ||
has_free_typevars(((jl_uniontype_t*)v)->b, env);
if (jl_is_unionall(v)) {
jl_unionall_t *ua = (jl_unionall_t*)v;
jl_typeenv_t newenv = { ua->var, NULL, env };
return has_free_typevars(ua->var->lb, env) || has_free_typevars(ua->var->ub, env) ||
has_free_typevars(ua->body, &newenv);
}
if (jl_is_datatype(v)) {
int expect = ((jl_datatype_t*)v)->hasfreetypevars;
if (expect == 0 || env == NULL)
return expect;
size_t i;
for (i=0; i < jl_nparams(v); i++) {
if (has_free_typevars(jl_tparam(v,i), env)) {
assert(expect);
return 1;
}
}
}
return 0;
}
JL_DLLEXPORT int jl_has_free_typevars(jl_value_t *v)
{
return has_free_typevars(v, NULL);
}
static void find_free_typevars(jl_value_t *v, jl_typeenv_t *env, jl_array_t *out)
{
if (jl_typeis(v, jl_tvar_type)) {
if (!typeenv_has(env, (jl_tvar_t*)v))
jl_array_ptr_1d_push(out, v);
}
else if (jl_is_uniontype(v)) {
find_free_typevars(((jl_uniontype_t*)v)->a, env, out);
find_free_typevars(((jl_uniontype_t*)v)->b, env, out);
}
else if (jl_is_unionall(v)) {
jl_unionall_t *ua = (jl_unionall_t*)v;
jl_typeenv_t newenv = { ua->var, NULL, env };
find_free_typevars(ua->var->lb, env, out);
find_free_typevars(ua->var->ub, env, out);
find_free_typevars(ua->body, &newenv, out);
}
else if (jl_is_datatype(v)) {
if (!((jl_datatype_t*)v)->hasfreetypevars)
return;
size_t i;
for (i=0; i < jl_nparams(v); i++)
find_free_typevars(jl_tparam(v,i), env, out);
}
}
JL_DLLEXPORT jl_array_t *jl_find_free_typevars(jl_value_t *v)
{
jl_array_t *out = jl_alloc_vec_any(0);
JL_GC_PUSH1(&out);
find_free_typevars(v, NULL, out);
JL_GC_POP();
return out;
}
// test whether a type has vars bound by the given environment
static int jl_has_bound_typevars(jl_value_t *v, jl_typeenv_t *env)
{
if (jl_typeis(v, jl_tvar_type))
return typeenv_has(env, (jl_tvar_t*)v);
if (jl_is_uniontype(v))
return jl_has_bound_typevars(((jl_uniontype_t*)v)->a, env) ||
jl_has_bound_typevars(((jl_uniontype_t*)v)->b, env);
if (jl_is_unionall(v)) {
jl_unionall_t *ua = (jl_unionall_t*)v;
if (jl_has_bound_typevars(ua->var->lb, env) || jl_has_bound_typevars(ua->var->ub, env))
return 1;
jl_typeenv_t *te = env;
while (te != NULL) {
if (te->var == ua->var)
break;
te = te->prev;
}
if (te) te->var = NULL; // temporarily remove this var from env
int ans = jl_has_bound_typevars(ua->body, env);
if (te) te->var = ua->var;
return ans;
}
if (jl_is_datatype(v)) {
if (!((jl_datatype_t*)v)->hasfreetypevars)
return 0;
size_t i;
for (i=0; i < jl_nparams(v); i++) {
if (jl_has_bound_typevars(jl_tparam(v,i), env))
return 1;
}
}
return 0;
}
JL_DLLEXPORT int jl_has_typevar(jl_value_t *t, jl_tvar_t *v)
{
jl_typeenv_t env = { v, NULL, NULL };
return jl_has_bound_typevars(t, &env);
}
static int _jl_has_typevar_from_ua(jl_value_t *t, jl_unionall_t *ua, jl_typeenv_t *prev)
{
jl_typeenv_t env = { ua->var, NULL, prev };
if (jl_is_unionall(ua->body))
return _jl_has_typevar_from_ua(t, (jl_unionall_t*)ua->body, &env);
else
return jl_has_bound_typevars(t, &env);
}
JL_DLLEXPORT int jl_has_typevar_from_unionall(jl_value_t *t, jl_unionall_t *ua)
{
return _jl_has_typevar_from_ua(t, ua, NULL);
}
// Return true for any type (Integer or Unsigned) that can fit in a
// size_t and pass back value, else return false
JL_DLLEXPORT int jl_get_size(jl_value_t *val, size_t *pnt)
{
if (jl_is_long(val)) {
ssize_t slen = jl_unbox_long(val);
if (slen < 0)
jl_errorf("size or dimension is negative: %d", slen);
*pnt = slen;
return 1;
}
return 0;
}
// --- type union ---
static int count_union_components(jl_value_t **types, size_t n)
{
size_t i, c=0;
for(i=0; i < n; i++) {
jl_value_t *e = types[i];
if (jl_is_uniontype(e)) {
jl_uniontype_t *u = (jl_uniontype_t*)e;
c += count_union_components(&u->a, 1);
c += count_union_components(&u->b, 1);
}
else {
c++;
}
}
return c;
}
int jl_count_union_components(jl_value_t *v)
{
if (!jl_is_uniontype(v)) return 1;
jl_uniontype_t *u = (jl_uniontype_t*)v;
return jl_count_union_components(u->a) + jl_count_union_components(u->b);
}
// Return the `*pi`th element of a nested type union, according to a
// standard traversal order. Anything that is not itself a `Union` is
// considered an "element". `*pi` is destroyed in the process.
static jl_value_t *nth_union_component(jl_value_t *v, int *pi) JL_NOTSAFEPOINT
{
if (!jl_is_uniontype(v)) {
if (*pi == 0)
return v;
(*pi)--;
return NULL;
}
jl_uniontype_t *u = (jl_uniontype_t*)v;
jl_value_t *a = nth_union_component(u->a, pi);
if (a) return a;
return nth_union_component(u->b, pi);
}
jl_value_t *jl_nth_union_component(jl_value_t *v, int i) JL_NOTSAFEPOINT
{
return nth_union_component(v, &i);
}
// inverse of jl_nth_union_component
int jl_find_union_component(jl_value_t *haystack, jl_value_t *needle, unsigned *nth) JL_NOTSAFEPOINT
{
if (jl_is_uniontype(haystack)) {
if (jl_find_union_component(((jl_uniontype_t*)haystack)->a, needle, nth))
return 1;
if (jl_find_union_component(((jl_uniontype_t*)haystack)->b, needle, nth))
return 1;
return 0;
}
if (needle == haystack)
return 1;
(*nth)++;
return 0;
}
static void flatten_type_union(jl_value_t **types, size_t n, jl_value_t **out, size_t *idx) JL_NOTSAFEPOINT
{
size_t i;
for(i=0; i < n; i++) {
jl_value_t *e = types[i];
if (jl_is_uniontype(e)) {
jl_uniontype_t *u = (jl_uniontype_t*)e;
flatten_type_union(&u->a, 1, out, idx);
flatten_type_union(&u->b, 1, out, idx);
}
else {
out[*idx] = e;
(*idx)++;
}
}
}
STATIC_INLINE const char *datatype_module_name(jl_value_t *t) JL_NOTSAFEPOINT
{
if (((jl_datatype_t*)t)->name->module == NULL)
return NULL;
return jl_symbol_name(((jl_datatype_t*)t)->name->module->name);
}
STATIC_INLINE const char *str_(const char *s) JL_NOTSAFEPOINT
{
return s == NULL ? "" : s;
}
STATIC_INLINE int cmp_(int a, int b) JL_NOTSAFEPOINT
{
return a < b ? -1 : a > b;
}
// a/b are jl_datatype_t* & not NULL
int datatype_name_cmp(jl_value_t *a, jl_value_t *b) JL_NOTSAFEPOINT
{
if (!jl_is_datatype(a))
return jl_is_datatype(b) ? 1 : 0;
if (!jl_is_datatype(b))
return -1;
int cmp = strcmp(str_(datatype_module_name(a)), str_(datatype_module_name(b)));
if (cmp != 0)
return cmp;
cmp = strcmp(str_(jl_typename_str(a)), str_(jl_typename_str(b)));
if (cmp != 0)
return cmp;
cmp = cmp_(jl_nparams(a), jl_nparams(b));
if (cmp != 0)
return cmp;
// compare up to 3 type parameters
for (int i = 0; i < 3 && i < jl_nparams(a); i++) {
jl_value_t *ap = jl_tparam(a, i);
jl_value_t *bp = jl_tparam(b, i);
if (ap == bp) {
continue;
}
else if (jl_is_datatype(ap) && jl_is_datatype(bp)) {
cmp = datatype_name_cmp(ap, bp);
if (cmp != 0)
return cmp;
}
else if (jl_is_unionall(ap) && jl_is_unionall(bp)) {
cmp = datatype_name_cmp(jl_unwrap_unionall(ap), jl_unwrap_unionall(bp));
if (cmp != 0)
return cmp;
}
else {
// give up
cmp = 0;
}
}
return cmp;
}
// sort singletons first, then DataTypes, then UnionAlls,
// ties broken alphabetically including module name & type parameters
int union_sort_cmp(const void *ap, const void *bp) JL_NOTSAFEPOINT
{
jl_value_t *a = *(jl_value_t**)ap;
jl_value_t *b = *(jl_value_t**)bp;
if (a == NULL)
return b == NULL ? 0 : 1;
if (b == NULL)
return -1;
if (jl_is_datatype(a)) {
if (!jl_is_datatype(b))
return -1;
if (jl_is_datatype_singleton((jl_datatype_t*)a)) {
if (jl_is_datatype_singleton((jl_datatype_t*)b))
return datatype_name_cmp(a, b);
return -1;
}
else if (jl_is_datatype_singleton((jl_datatype_t*)b)) {
return 1;
}
else if (jl_isbits(a)) {
if (jl_isbits(b))
return datatype_name_cmp(a, b);
return -1;
}
else if (jl_isbits(b)) {
return 1;
}
else {
return datatype_name_cmp(a, b);
}
}
else {
if (jl_is_datatype(b))
return 1;
return datatype_name_cmp(jl_unwrap_unionall(a), jl_unwrap_unionall(b));
}
}
JL_DLLEXPORT jl_value_t *jl_type_union(jl_value_t **ts, size_t n)
{
if (n == 0) return (jl_value_t*)jl_bottom_type;
size_t i;
for(i=0; i < n; i++) {
jl_value_t *pi = ts[i];
if (!(jl_is_type(pi) || jl_is_typevar(pi)) || jl_is_vararg_type(pi))
jl_type_error("Union", (jl_value_t*)jl_type_type, pi);
}
if (n == 1) return ts[0];
size_t nt = count_union_components(ts, n);
jl_value_t **temp;
JL_GC_PUSHARGS(temp, nt+1);
size_t count = 0;
flatten_type_union(ts, n, temp, &count);
assert(count == nt);
size_t j;
for(i=0; i < nt; i++) {
int has_free = temp[i]!=NULL && jl_has_free_typevars(temp[i]);
for(j=0; j < nt; j++) {
if (j != i && temp[i] && temp[j]) {
if (temp[i] == temp[j] || temp[i] == jl_bottom_type ||
temp[j] == (jl_value_t*)jl_any_type ||
(!has_free && !jl_has_free_typevars(temp[j]) &&
jl_subtype(temp[i], temp[j]))) {
temp[i] = NULL;
}
}
}
}
qsort(temp, nt, sizeof(jl_value_t*), union_sort_cmp);
jl_value_t **ptu = &temp[nt];
*ptu = jl_bottom_type;
int k;
for (k = (int)nt-1; k >= 0; --k) {
if (temp[k] != NULL) {
if (*ptu == jl_bottom_type)
*ptu = temp[k];
else
*ptu = jl_new_struct(jl_uniontype_type, temp[k], *ptu);
}
}
assert(*ptu != NULL);
jl_value_t *tu = *ptu;
JL_GC_POP();
return tu;
}
// unionall types -------------------------------------------------------------
JL_DLLEXPORT jl_value_t *jl_type_unionall(jl_tvar_t *v, jl_value_t *body)
{
if (!jl_is_type(body) && !jl_is_typevar(body))
jl_type_error("UnionAll", (jl_value_t*)jl_type_type, body);
// normalize `T where T<:S` => S
if (body == (jl_value_t*)v)
return v->ub;
// where var doesn't occur in body just return body
if (!jl_has_typevar(body, v))
return body;
//if (v->lb == v->ub) // TODO maybe
// return jl_substitute_var(body, v, v->ub);
return jl_new_struct(jl_unionall_type, v, body);
}
// --- type instantiation and cache ---
static intptr_t wrapper_id(jl_value_t *t) JL_NOTSAFEPOINT
{
// DataType wrappers occur often, e.g. when called as constructors.
// make sure any type equal to a wrapper gets a consistent, ordered ID.
if (!jl_is_unionall(t)) return 0;
jl_value_t *u = jl_unwrap_unionall(t);
if (jl_is_datatype(u) && ((jl_datatype_t*)u)->name->wrapper == t)
return ((jl_datatype_t*)u)->name->hash;
return 0;
}
// this function determines whether a type is simple enough to form
// a total order based on UIDs and object_id.
static int is_typekey_ordered(jl_value_t **key, size_t n)
{
size_t i;
for(i=0; i < n; i++) {
jl_value_t *k = key[i];
if (jl_is_typevar(k))
return 0;
if (jl_is_datatype(k)) {
if (!((jl_datatype_t *) k)->typekeyordered)
return 0;
}
else {
if (jl_is_type(k) && k != jl_bottom_type && !wrapper_id(k))
return 0; // Union or UnionAll which is not a wrapper
}
}
return 1;
}
// ordered comparison of types
static int typekey_compare(jl_datatype_t *tt, jl_value_t **key, size_t n) JL_NOTSAFEPOINT
{
size_t j;
if (tt == NULL) return -1; // place NULLs at end to allow padding for fast growing
size_t tnp = jl_nparams(tt);
if (n < tnp) return -1;
if (n > tnp) return 1;
for(j=0; j < n; j++) {
jl_value_t *kj = key[j], *tj = jl_svecref(tt->parameters,j);
if (tj != kj) {
int dtk = jl_is_datatype(kj);
if (!jl_is_datatype(tj)) {
if (dtk) return 1;
uint32_t tid = wrapper_id(tj), kid = wrapper_id(kj);
if (kid != tid)
return kid < tid ? -1 : 1;
if (tid)
continue;
if (jl_egal(tj, kj))
continue;
return (jl_object_id(kj) < jl_object_id(tj) ? -1 : 1);
}
else if (!dtk) {
return -1;
}
assert(dtk && jl_is_datatype(tj));
jl_datatype_t *dt = (jl_datatype_t*)tj;
jl_datatype_t *dk = (jl_datatype_t*)kj;
if (dk->uid != dt->uid) {
return dk->uid < dt->uid ? -1 : 1;
}
else if (dk->uid != 0) {
assert(0);
}
else if (dk->name->hash != dt->name->hash) {
return dk->name->hash < dt->name->hash ? -1 : 1;
}
else {
int cmp = typekey_compare(dt, jl_svec_data(dk->parameters), jl_nparams(dk));
if (cmp != 0)
return cmp;
}
}
}
return 0;
}
static int dt_compare(const void *ap, const void *bp) JL_NOTSAFEPOINT
{
jl_datatype_t *a = *(jl_datatype_t**)ap;
jl_datatype_t *b = *(jl_datatype_t**)bp;
if (a == b) return 0;
if (b == NULL) return -1;
if (a == NULL) return 1;
return typekey_compare(b, jl_svec_data(a->parameters), jl_svec_len(a->parameters));
}
void jl_sort_types(jl_value_t **types, size_t length)
{
qsort(types, length, sizeof(jl_value_t*), dt_compare);
}
static int typekey_eq(jl_datatype_t *tt, jl_value_t **key, size_t n)
{
size_t j;
size_t tnp = jl_nparams(tt);
if (n != tnp) return 0;
if (tt->name == jl_type_typename) {
// for Type{T}, require `typeof(T)` to match also, to avoid incorrect
// dispatch from changing the type of something.
// this should work because `Type`s don't have uids, and aren't the
// direct tags of values so we don't rely on pointer equality.
jl_value_t *kj = key[0], *tj = jl_tparam0(tt);
return (kj == tj || (jl_typeof(tj) == jl_typeof(kj) && jl_types_equal(tj, kj)));
}
for(j=0; j < n; j++) {
jl_value_t *kj = key[j], *tj = jl_svecref(tt->parameters,j);
if (tj != kj) {
// require exact same Type{T}. see e.g. issue #22842
if (jl_is_type_type(tj) || jl_is_type_type(kj))
return 0;
if (!jl_types_equal(tj, kj))
return 0;
}
}
return 1;
}
// look up a type in a cache by binary or linear search.
// if found, returns the index of the found item. if not found, returns
// ~n, where n is the index where the type should be inserted.
static ssize_t lookup_type_idx(jl_typename_t *tn, jl_value_t **key, size_t n, int ordered)
{
if (n==0) return -1;
if (ordered) {
jl_svec_t *cache = tn->cache;
jl_datatype_t **data = (jl_datatype_t**)jl_svec_data(cache);
size_t cl = jl_svec_len(cache);
ssize_t lo = -1;
ssize_t hi = cl;
while (lo < hi-1) {
ssize_t m = ((size_t)(lo+hi))>>1;
int cmp = typekey_compare(data[m], key, n);
if (cmp > 0)
lo = m;
else
hi = m;
}
/*
When a module is replaced, the new versions of its types are different but
cannot be distinguished by typekey_compare, since the TypeNames can only
be distinguished by addresses, which don't have a reliable order. So we
need to allow sequences of typekey_compare-equal types in the ordered cache.
*/
while (hi < cl && typekey_compare(data[hi], key, n) == 0) {
if (typekey_eq(data[hi], key, n))
return hi;
hi++;
}
return ~hi;
}
else {
jl_svec_t *cache = tn->linearcache;
jl_datatype_t **data = (jl_datatype_t**)jl_svec_data(cache);
size_t cl = jl_svec_len(cache);
ssize_t i;
for(i=0; i < cl; i++) {
jl_datatype_t *tt = data[i];
if (tt == NULL) return ~i;
if (typekey_eq(tt, key, n))
return i;
}
return ~cl;
}
}
static jl_value_t *lookup_type(jl_typename_t *tn, jl_value_t **key, size_t n)
{
JL_TIMING(TYPE_CACHE_LOOKUP);
int ord = is_typekey_ordered(key, n);
ssize_t idx = lookup_type_idx(tn, key, n, ord);
jl_value_t *t = (idx < 0) ? NULL : jl_svecref(ord ? tn->cache : tn->linearcache, idx);
return t;
}
static volatile int t_uid_ctr = 1;
int jl_get_t_uid_ctr(void) { return t_uid_ctr; }
void jl_set_t_uid_ctr(int i) { t_uid_ctr=i; }
int jl_assign_type_uid(void)
{
assert(t_uid_ctr != 0);
return jl_atomic_fetch_add(&t_uid_ctr, 1);
}
static int is_cacheable(jl_datatype_t *type)
{
// only cache types whose behavior will not depend on the identities
// of contained TypeVars
assert(jl_is_datatype(type));
jl_svec_t *t = type->parameters;
if (jl_svec_len(t) == 0) return 0;
// cache abstract types with no free type vars
if (jl_is_abstracttype(type))
return !jl_has_free_typevars((jl_value_t*)type);
// ... or concrete types
return jl_is_concrete_type((jl_value_t*)type);
}
static void cache_insert_type(jl_value_t *type, ssize_t insert_at, int ordered)
{
assert(jl_is_datatype(type));
// assign uid if it hasn't been done already
if (!jl_is_abstracttype(type) && ((jl_datatype_t*)type)->uid==0)
((jl_datatype_t*)type)->uid = jl_assign_type_uid();
jl_svec_t *cache;
if (ordered)
cache = ((jl_datatype_t*)type)->name->cache;
else
cache = ((jl_datatype_t*)type)->name->linearcache;
assert(jl_is_svec(cache));
size_t n = jl_svec_len(cache);
if (n==0 || jl_svecref(cache,n-1) != NULL) {
jl_svec_t *nc = jl_alloc_svec(n < 8 ? 8 : (n*3)>>1);
memcpy(jl_svec_data(nc), jl_svec_data(cache), sizeof(void*) * n);
if (ordered)
((jl_datatype_t*)type)->name->cache = nc;
else
((jl_datatype_t*)type)->name->linearcache = nc;
jl_gc_wb(((jl_datatype_t*)type)->name, nc);
cache = nc;
n = jl_svec_len(nc);
}
jl_value_t **p = jl_svec_data(cache);
size_t i = insert_at;
jl_value_t *temp = p[i], *temp2;
jl_svecset(cache, insert_at, (jl_value_t*)type);
assert(i < n-1 || temp == NULL);
while (temp != NULL && i < n-1) {
i++;
temp2 = p[i];
p[i] = temp;
temp = temp2;
}
}
jl_value_t *jl_cache_type_(jl_datatype_t *type)
{
if (is_cacheable(type)) {
JL_TIMING(TYPE_CACHE_INSERT);
int ord = type->typekeyordered;
ssize_t idx = lookup_type_idx(type->name, jl_svec_data(type->parameters),
jl_svec_len(type->parameters), ord);
if (idx >= 0)
type = (jl_datatype_t*)jl_svecref(ord ? type->name->cache : type->name->linearcache, idx);
else
cache_insert_type((jl_value_t*)type, ~idx, ord);
}
return (jl_value_t*)type;
}
int jl_type_equality_is_identity(jl_value_t *t1, jl_value_t *t2)
{
if (t1 == t2)
return 1;
if (!jl_is_datatype(t1) || !jl_is_datatype(t2))
return 0;
jl_datatype_t *dt1 = (jl_datatype_t *) t1;
jl_datatype_t *dt2 = (jl_datatype_t *) t2;
if (!(is_cacheable(dt1) || jl_svec_len(dt1->parameters) == 0)) {
if (!(is_cacheable(dt2) || jl_svec_len(dt2->parameters) == 0))
return 0;
else
return 1;
}
if (!(is_cacheable(dt2) || jl_svec_len(dt2->parameters) == 0))
return 1;
return dt1->typekeyordered == dt2->typekeyordered;
}
// type instantiation
static int within_typevar(jl_value_t *t, jl_value_t *vlb, jl_value_t *vub)
{
jl_value_t *lb = t, *ub = t;
if (jl_is_typevar(t) || jl_has_free_typevars(t)) {
// TODO: automatically restrict typevars in method definitions based on
// types they are used in.
return 1;
//lb = ((jl_tvar_t*)t)->lb;
//ub = ((jl_tvar_t*)t)->ub;
}
else if (!jl_is_type(t)) {
return vlb == jl_bottom_type && vub == (jl_value_t*)jl_any_type;
}
return ((jl_has_free_typevars(vlb) || jl_subtype(vlb, lb)) &&
(jl_has_free_typevars(vub) || jl_subtype(ub, vub)));
}
struct _jl_typestack_t;
typedef struct _jl_typestack_t jl_typestack_t;
static jl_value_t *inst_datatype(jl_datatype_t *dt, jl_svec_t *p, jl_value_t **iparams, size_t ntp,
int cacheable, jl_typestack_t *stack);
jl_value_t *jl_apply_type(jl_value_t *tc, jl_value_t **params, size_t n)
{
if (tc == (jl_value_t*)jl_anytuple_type)
return (jl_value_t*)jl_apply_tuple_type_v(params, n);
if (tc == (jl_value_t*)jl_uniontype_type)
return (jl_value_t*)jl_type_union(params, n);
size_t i;
if (n > 1) {
// detect common case of applying a wrapper, where we know that all parameters will
// end up as direct parameters of a certain datatype, which can be optimized.
jl_value_t *u = jl_unwrap_unionall(tc);
if (jl_is_datatype(u) && n == jl_nparams((jl_datatype_t*)u) &&
((jl_datatype_t*)u)->name->wrapper == tc) {
int cacheable = 1;
for(i=0; i < n; i++) {
if (jl_has_free_typevars(params[i])) {
cacheable = 0; break;
}
}
return inst_datatype((jl_datatype_t*)u, NULL, params, n, cacheable, NULL);
}
}
JL_GC_PUSH1(&tc);
jl_value_t *tc0 = tc;
for (i=0; i < n; i++) {
if (!jl_is_unionall(tc0))
jl_error("too many parameters for type");
jl_value_t *pi = params[i];
tc0 = ((jl_unionall_t*)tc0)->body;
// doing a substitution can cause later UnionAlls to be dropped,
// as in `NTuple{0,T} where T` => `Tuple{}`. allow values to be
// substituted for these missing parameters.
// TODO: figure out how to get back a type error for e.g.
// S = Tuple{Vararg{T,N}} where T<:NTuple{N} where N
// S{0,Int}
if (!jl_is_unionall(tc)) continue;
jl_unionall_t *ua = (jl_unionall_t*)tc;
if (!jl_has_free_typevars(ua->var->lb) && !jl_has_free_typevars(ua->var->ub) &&
!within_typevar(pi, ua->var->lb, ua->var->ub)) {
jl_datatype_t *inner = (jl_datatype_t*)jl_unwrap_unionall(tc);
int iswrapper = 0;
if (jl_is_datatype(inner)) {
jl_value_t *temp = inner->name->wrapper;
while (jl_is_unionall(temp)) {
if (temp == tc) {
iswrapper = 1;
break;
}
temp = ((jl_unionall_t*)temp)->body;
}
}
// if this is a wrapper, let check_datatype_parameters give the error
if (!iswrapper)
jl_type_error_rt("Type", jl_symbol_name(ua->var->name), (jl_value_t*)ua->var, pi);
}
tc = jl_instantiate_unionall(ua, pi);
}
JL_GC_POP();
return tc;
}
JL_DLLEXPORT jl_value_t *jl_apply_type1(jl_value_t *tc, jl_value_t *p1)
{
JL_GC_PUSH1(&p1);
jl_value_t *t = jl_apply_type(tc, &p1, 1);
JL_GC_POP();
return t;
}
JL_DLLEXPORT jl_value_t *jl_apply_type2(jl_value_t *tc, jl_value_t *p1, jl_value_t *p2)
{
jl_value_t **args;
JL_GC_PUSHARGS(args, 2);
args[0] = p1; args[1] = p2;
jl_value_t *t = jl_apply_type(tc, args, 2);
JL_GC_POP();
return t;
}
JL_DLLEXPORT jl_value_t *jl_tupletype_fill(size_t n, jl_value_t *v)
{
// TODO: replace with just using NTuple
jl_value_t *p = NULL;
JL_GC_PUSH1(&p);
p = (jl_value_t*)jl_svec_fill(n, v);
p = (jl_value_t*)jl_apply_tuple_type((jl_svec_t*)p);
JL_GC_POP();
return p;
}
JL_EXTENSION struct _jl_typestack_t {
jl_datatype_t *tt;
struct _jl_typestack_t *prev;
};
static jl_value_t *inst_type_w_(jl_value_t *t, jl_typeenv_t *env, jl_typestack_t *stack, int check);
static jl_svec_t *inst_all(jl_svec_t *p, jl_typeenv_t *env, jl_typestack_t *stack, int check);
JL_DLLEXPORT jl_value_t *jl_instantiate_unionall(jl_unionall_t *u, jl_value_t *p)
{
jl_typeenv_t env = { u->var, p, NULL };
return inst_type_w_(u->body, &env, NULL, 1);
}
jl_value_t *jl_substitute_var(jl_value_t *t, jl_tvar_t *var, jl_value_t *val)
{
jl_typeenv_t env = { var, val, NULL };
return inst_type_w_(t, &env, NULL, 1);
}
jl_value_t *jl_unwrap_unionall(jl_value_t *v)
{
while (jl_is_unionall(v))
v = ((jl_unionall_t*)v)->body;
return v;
}
// wrap `t` in the same unionalls that surround `u`
jl_value_t *jl_rewrap_unionall(jl_value_t *t, jl_value_t *u)
{
if (!jl_is_unionall(u))
return t;
JL_GC_PUSH1(&t);
t = jl_rewrap_unionall(t, ((jl_unionall_t*)u)->body);
t = jl_new_struct(jl_unionall_type, ((jl_unionall_t*)u)->var, t);
JL_GC_POP();
return t;
}
static jl_value_t *lookup_type_stack(jl_typestack_t *stack, jl_datatype_t *tt, size_t ntp,
jl_value_t **iparams)
{
// if an identical instantiation is already in process somewhere up the
// stack, return it. this computes a fixed point for recursive types.
jl_typename_t *tn = tt->name;
while (stack != NULL) {
JL_GC_PROMISE_ROOTED(stack->tt);
if (stack->tt->name == tn &&
ntp == jl_svec_len(stack->tt->parameters) &&
typekey_eq(stack->tt, iparams, ntp)) {
return (jl_value_t*)stack->tt;
}
stack = stack->prev;
}
return NULL;
}
void jl_precompute_memoized_dt(jl_datatype_t *dt)
{
int istuple = (dt->name == jl_tuple_typename);
dt->hasfreetypevars = 0;
dt->isconcretetype = !dt->abstract;
dt->isdispatchtuple = istuple;
size_t i, l = jl_nparams(dt);
for (i = 0; i < l; i++) {
jl_value_t *p = jl_tparam(dt, i);
if (!dt->hasfreetypevars)
dt->hasfreetypevars = jl_has_free_typevars(p);
if (istuple && dt->isconcretetype)
dt->isconcretetype = (jl_is_datatype(p) && ((jl_datatype_t*)p)->isconcretetype) || p == jl_bottom_type;
if (dt->isdispatchtuple)