-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
jltypes.c
3464 lines (3224 loc) · 118 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
/*
Types
. type predicates (subtype) and type matching
. type union and intersection
. builtin type definitions
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef _OS_WINDOWS_
#include <malloc.h>
#endif
#include "julia.h"
#include "julia_internal.h"
#include "builtin_proto.h"
#ifdef __cplusplus
extern "C" {
#endif
jl_datatype_t *jl_any_type;
jl_datatype_t *jl_type_type;
jl_datatype_t *jl_typename_type;
jl_datatype_t *jl_sym_type;
jl_datatype_t *jl_symbol_type;
jl_datatype_t *jl_gensym_type;
jl_datatype_t *jl_simplevector_type;
jl_typename_t *jl_tuple_typename;
jl_tupletype_t *jl_anytuple_type;
jl_datatype_t *jl_ntuple_type;
jl_typename_t *jl_ntuple_typename;
jl_datatype_t *jl_vararg_type;
jl_datatype_t *jl_tvar_type;
jl_datatype_t *jl_uniontype_type;
jl_datatype_t *jl_datatype_type;
jl_value_t *jl_bottom_type;
jl_datatype_t *jl_abstractarray_type;
jl_datatype_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_float32_type;
jl_datatype_t *jl_float64_type;
jl_datatype_t *jl_floatingpoint_type;
jl_datatype_t *jl_number_type;
jl_datatype_t *jl_complex_type;
jl_datatype_t *jl_signed_type;
jl_value_t *jl_emptytuple=NULL;
jl_svec_t *jl_emptysvec;
jl_value_t *jl_nothing;
// --- type properties and predicates ---
int jl_is_type(jl_value_t *v)
{
jl_value_t *t = jl_typeof(v);
return (t == (jl_value_t*)jl_datatype_type || t == (jl_value_t*)jl_uniontype_type ||
t == (jl_value_t*)jl_typector_type);
}
STATIC_INLINE int is_unspec(jl_datatype_t *dt)
{
return (jl_datatype_t*)dt->name->primary == dt;
}
static int jl_has_typevars__(jl_value_t *v, int incl_wildcard, jl_svec_t *p)
{
size_t i;
if (jl_typeis(v, jl_tvar_type)) {
if (jl_has_typevars__(((jl_tvar_t*)v)->ub, incl_wildcard, p) ||
jl_has_typevars__(((jl_tvar_t*)v)->lb, incl_wildcard, p))
return 1;
if (p != NULL) {
size_t l = jl_svec_len(p);
for(i=0; i < l; i++) {
if (v == jl_svecref(p, i))
return 1;
}
return 0;
}
if (!((jl_tvar_t*)v)->bound)
return incl_wildcard;
return 1;
}
if (jl_is_typector(v))
return incl_wildcard;
jl_svec_t *t;
if (jl_is_uniontype(v)) {
t = ((jl_uniontype_t*)v)->types;
}
else if (jl_is_datatype(v)) {
if (is_unspec((jl_datatype_t*)v))
return 0;
t = ((jl_datatype_t*)v)->parameters;
}
else {
return 0;
}
size_t l = jl_svec_len(t);
for(i=0; i < l; i++) {
jl_value_t *elt = jl_svecref(t, i);
if (elt != v) {
if (jl_has_typevars__(elt, incl_wildcard, p))
return 1;
}
}
// probably not necessary; no reason to use match() instead of subtype()
// on the unconstrained version of a type
//if (jl_is_typector(v))
// return jl_svec_len((((jl_typector_t*)v)->parameters) > 0);
return 0;
}
static int jl_has_typevars_(jl_value_t *v, int incl_wildcard)
{
if (jl_is_typevar(v)) return 1;
return jl_has_typevars__(v, incl_wildcard, NULL);
}
static int jl_has_typevars_from(jl_value_t *v, jl_svec_t *p)
{
if (jl_svec_len(p) == 0)
return 0;
return jl_has_typevars__(v, 0, p);
}
int jl_has_typevars(jl_value_t *v)
{
if (jl_is_typevar(v)) return 1;
return jl_has_typevars__(v, 0, NULL);
}
int jl_is_leaf_type(jl_value_t *v)
{
if (jl_is_datatype(v)) {
if (((jl_datatype_t*)v)->abstract) {
if (jl_is_type_type(v))
return !jl_is_typevar(jl_tparam0(v));
return 0;
}
jl_svec_t *t = ((jl_datatype_t*)v)->parameters;
size_t l = jl_svec_len(t);
if (((jl_datatype_t*)v)->name == jl_tuple_typename) {
for(int i=0; i < l; i++) {
if (!jl_is_leaf_type(jl_svecref(t,i)))
return 0;
}
}
else {
for(int i=0; i < l; i++) {
if (jl_is_typevar(jl_svecref(t,i)))
return 0;
}
}
return 1;
}
return 0;
}
static int type_eqv_(jl_value_t *a, jl_value_t *b);
// --- type union ---
static int count_union_components(jl_svec_t *types)
{
size_t i, c=0;
for(i=0; i < jl_svec_len(types); i++) {
jl_value_t *e = jl_svecref(types,i);
if (jl_is_uniontype(e)) {
c += count_union_components(((jl_uniontype_t*)e)->types);
}
else {
c++;
}
}
return c;
}
static void flatten_type_union(jl_svec_t *types, jl_value_t **out, size_t *idx)
{
size_t i;
for(i=0; i < jl_svec_len(types); i++) {
jl_value_t *e = jl_svecref(types,i);
if (jl_is_uniontype(e)) {
flatten_type_union(((jl_uniontype_t*)e)->types, out, idx);
}
else {
out[*idx] = e;
(*idx)++;
}
}
}
static int union_elt_morespecific(const void *a, const void *b)
{
jl_value_t *va = *(jl_value_t**)a;
jl_value_t *vb = *(jl_value_t**)b;
if (jl_args_morespecific(va, vb))
return -1;
// impose a partially-arbitrary ordering on Union elements, to make it more
// likely that many Unions will be identical and can be merged.
// NOTE: we know !(a <: b) && !(b <: a), since otherwise one would have
// been eliminated from the Union.
return jl_object_id(va) < jl_object_id(vb) ? -1 : 1;
}
// NOTE: this is a hack to avoid simplifying type unions too early inside
// type definitions. (issue #2365)
int inside_typedef = 0;
jl_svec_t *jl_compute_type_union(jl_svec_t *types)
{
size_t n = count_union_components(types);
jl_value_t **temp;
JL_GC_PUSHARGS(temp, n+1);
size_t idx=0;
flatten_type_union(types, temp, &idx);
assert(idx == n);
size_t i, j, ndel=0;
for(i=0; i < n; i++) {
for(j=0; j < n; j++) {
if (j != i && temp[i] && temp[j]) {
if (temp[i] == temp[j] ||
(!jl_has_typevars(temp[i]) && !jl_has_typevars(temp[j]) &&
!(inside_typedef && (jl_is_typevar(temp[i]) ||
jl_is_typevar(temp[j]))) &&
(type_eqv_(temp[i], temp[j]) ||
jl_subtype(temp[i], temp[j], 0)))) {
temp[i] = NULL;
ndel++;
}
}
}
}
temp[n] = NULL;
jl_svec_t *result = jl_alloc_svec_uninit(n - ndel);
temp[n] = (jl_value_t*)result; // root result tuple while sorting
j=0;
for(i=0; i < n; i++) {
if (temp[i] != NULL) {
jl_svecset(result, j, temp[i]);
j++;
}
}
assert(j == n-ndel);
// sort Union components by specificity, so "complex" type Unions work as
// long as there are no ambiguities (see e.g. issue #126).
// TODO: maybe warn about ambiguities
qsort(result->data, j, sizeof(jl_value_t*), union_elt_morespecific);
JL_GC_POP();
return result;
}
jl_value_t *jl_type_union(jl_svec_t *types)
{
types = jl_compute_type_union(types);
if (jl_svec_len(types) == 1)
return jl_svecref(types, 0);
if (jl_svec_len(types) == 0)
return (jl_value_t*)jl_bottom_type;
JL_GC_PUSH1(&types);
jl_value_t *tu = (jl_value_t*)jl_new_uniontype(types);
JL_GC_POP();
return tu;
}
// --- type intersection ---
typedef enum {invariant, covariant} variance_t;
#define MAX_CENV_SIZE 128
typedef struct {
jl_value_t **data;
size_t n;
jl_svec_t *tvars;
} cenv_t;
STATIC_INLINE int is_bnd(jl_tvar_t *tv, cenv_t *env)
{
if (jl_is_typevar(env->tvars))
return (jl_tvar_t*)env->tvars == tv;
for(size_t i=0; i < jl_svec_len(env->tvars); i++) {
if ((jl_tvar_t*)jl_svecref(env->tvars,i) == tv)
return 1;
}
return 0;
}
STATIC_INLINE int is_btv(jl_value_t *v)
{
return jl_is_typevar(v) && ((jl_tvar_t*)v)->bound;
}
static void extend_(jl_value_t *var, jl_value_t *val, cenv_t *soln, int allowself)
{
if (!allowself && var == val)
return;
for(int i=0; i < soln->n; i+=2) {
if (soln->data[i]==var &&
(soln->data[i+1]==val || (!jl_is_typevar(val) &&
type_eqv_(soln->data[i+1],val))))
return;
if (soln->data[i]==val && soln->data[i+1]==var)
return;
}
if (soln->n >= MAX_CENV_SIZE)
jl_error("type too large");
soln->data[soln->n++] = var;
soln->data[soln->n++] = val;
}
static void extend(jl_value_t *var, jl_value_t *val, cenv_t *soln)
{
extend_(var, val, soln, 0);
}
static jl_value_t *jl_type_intersect(jl_value_t *a, jl_value_t *b,
cenv_t *penv, cenv_t *eqc, variance_t var);
static jl_value_t *intersect_union(jl_uniontype_t *a, jl_value_t *b,
cenv_t *penv, cenv_t *eqc, variance_t var)
{
int eq0 = eqc->n, co0 = penv->n;
jl_svec_t *t = jl_alloc_svec(jl_svec_len(a->types));
JL_GC_PUSH1(&t);
size_t i, l=jl_svec_len(t);
for(i=0; i < l; i++) {
int eq_l = eqc->n, co_l = penv->n;
jl_value_t *ti = jl_type_intersect(jl_svecref(a->types,i), b,
penv, eqc, var);
if (ti == (jl_value_t*)jl_bottom_type) {
eqc->n = eq0; penv->n = co0;
ti = jl_type_intersect(jl_svecref(a->types,i), b,
penv, eqc, var);
if (ti != (jl_value_t*)jl_bottom_type) {
// tvar conflict among union elements; keep the conflicting
// constraints rolled back
eqc->n = eq0; penv->n = co0;
}
else {
// union element doesn't overlap no matter what.
// so remove only its constraints.
eqc->n = eq_l; penv->n = co_l;
}
}
jl_svecset(t, i, ti);
}
// problem: an intermediate union type we make here might be too
// complex, even though the final type after typevars are replaced
// might be ok.
jl_value_t *tu = jl_type_union(t);
JL_GC_POP();
return tu;
}
// if returns with *bot!=0, then intersection is Union()
static size_t tuple_intersect_size(jl_svec_t *a, jl_svec_t *b, int *bot)
{
size_t al = jl_svec_len(a);
size_t bl = jl_svec_len(b);
*bot = 0;
if (al == bl) return al;
if (al > bl) return tuple_intersect_size(b, a, bot);
assert(al < bl);
if (jl_is_vararg_type(jl_svecref(b,bl-1))) {
if (al > 0 && jl_is_vararg_type(jl_svecref(a,al-1))) {
return bl;
}
else {
if (bl == al+1)
return al;
*bot=1;
return 0;
}
}
if (al > 0 && jl_is_vararg_type(jl_svecref(a,al-1)))
return bl;
*bot=1;
return 0;
}
jl_datatype_t *jl_wrap_vararg(jl_value_t *t)
{
jl_value_t *env[2];
env[0] = jl_tparam0(jl_vararg_type);
env[1] = t;
return (jl_datatype_t*)jl_instantiate_type_with((jl_value_t*)jl_vararg_type, env, 1);
}
static jl_value_t *intersect_tuple(jl_datatype_t *a, jl_datatype_t *b,
cenv_t *penv, cenv_t *eqc, variance_t var)
{
jl_svec_t *ap = a->parameters, *bp = b->parameters;
size_t al = jl_svec_len(ap), bl = jl_svec_len(bp);
int bot=0;
size_t n = tuple_intersect_size(ap, bp, &bot);
if (bot) return (jl_value_t*)jl_bottom_type;
if (n == 0) return jl_typeof(jl_emptytuple);
jl_svec_t *tc = jl_alloc_svec(n);
jl_value_t *result = (jl_value_t*)tc;
jl_value_t *ce = NULL;
JL_GC_PUSH2(&tc, &ce);
size_t ai=0, bi=0, ci;
jl_value_t *ae=NULL, *be=NULL;
int aseq=0, bseq=0;
for(ci=0; ci < n; ci++) {
if (ai < al) {
ae = jl_svecref(ap,ai);
if (jl_is_vararg_type(ae)) {
aseq=1;
ae = jl_tparam0(ae);
}
ai++;
}
if (bi < bl) {
be = jl_svecref(bp,bi);
if (jl_is_vararg_type(be)) {
bseq=1;
be = jl_tparam0(be);
}
bi++;
}
assert(ae!=NULL && be!=NULL);
ce = jl_type_intersect(ae,be,penv,eqc,var);
if (ce == (jl_value_t*)jl_bottom_type) {
if (var!=invariant && aseq && bseq) {
// (X∩Y)==∅ → (X...)∩(Y...) == ()
if (n == 1) {
JL_GC_POP();
return (jl_value_t*)jl_typeof(jl_emptytuple);
}
jl_svec_set_len_unsafe(tc,jl_svec_len(tc)-1);
goto done_intersect_tuple;
}
JL_GC_POP();
return (jl_value_t*)jl_bottom_type;
}
if (aseq && bseq)
ce = (jl_value_t*)jl_wrap_vararg(ce);
jl_svecset(tc, ci, ce);
}
done_intersect_tuple:
result = (jl_value_t*)jl_apply_tuple_type(tc);
JL_GC_POP();
return result;
}
static jl_value_t *intersect_tag(jl_datatype_t *a, jl_datatype_t *b,
cenv_t *penv, cenv_t *eqc, variance_t var)
{
assert(a->name == b->name);
assert(jl_svec_len(a->parameters) == jl_svec_len(b->parameters));
jl_svec_t *p = jl_alloc_svec(jl_svec_len(a->parameters));
JL_GC_PUSH1(&p);
jl_value_t *ti;
size_t i;
if (a->name == jl_ntuple_typename) {
assert(jl_svec_len(p) == 2);
// NOTE: tuples are covariant, so NTuple element type is too
ti = jl_type_intersect(jl_tparam0(a),jl_tparam0(b),penv,eqc,invariant);
jl_svecset(p, 0, ti);
ti = jl_type_intersect(jl_tparam1(a),jl_tparam1(b),penv,eqc,var);
if (ti==(jl_value_t*)jl_bottom_type ||
jl_svecref(p,0)==(jl_value_t*)jl_bottom_type) {
JL_GC_POP();
return (jl_value_t*)jl_bottom_type;
}
jl_svecset(p, 1, ti);
}
else {
for(i=0; i < jl_svec_len(p); i++) {
jl_value_t *ap = jl_svecref(a->parameters,i);
jl_value_t *bp = jl_svecref(b->parameters,i);
if (jl_is_typevar(ap)) {
if (var==invariant && jl_is_typevar(bp)) {
if (((jl_tvar_t*)ap)->bound != ((jl_tvar_t*)bp)->bound) {
JL_GC_POP();
return (jl_value_t*)jl_bottom_type;
}
if ((is_unspec(a) && is_bnd((jl_tvar_t*)bp,penv)) ||
(is_bnd((jl_tvar_t*)ap,penv) && is_unspec(b))) {
// Foo{T} and Foo can never be equal since the former
// is always a subtype of the latter
JL_GC_POP();
return (jl_value_t*)jl_bottom_type;
}
}
ti = jl_type_intersect(ap,bp,penv,eqc,invariant);
if (bp == (jl_value_t*)jl_bottom_type &&
!((jl_tvar_t*)ap)->bound) {
// "Union()" as a type parameter
jl_svecset(p, i, ti);
continue;
}
}
else if (jl_is_typevar(bp)) {
ti = jl_type_intersect(ap,bp,penv,eqc,invariant);
if (ap == (jl_value_t*)jl_bottom_type &&
!((jl_tvar_t*)bp)->bound) {
// "Union()" as a type parameter
jl_svecset(p, i, ti);
continue;
}
}
else {
int tva = jl_has_typevars_(ap,0);
int tvb = jl_has_typevars_(bp,0);
if (tva || tvb) {
if (jl_subtype_invariant(ap,bp,0) ||
jl_subtype_invariant(bp,ap,0)) {
ti = jl_type_intersect(ap,bp,penv,eqc,invariant);
}
else {
ti = (jl_value_t*)jl_bottom_type;
}
}
else if (type_eqv_(ap,bp)) {
ti = ap;
if (ti == (jl_value_t*)jl_bottom_type) {
// "Union()" as a type parameter
jl_svecset(p, i, ti);
continue;
}
}
else {
ti = (jl_value_t*)jl_bottom_type;
}
}
if (ti == (jl_value_t*)jl_bottom_type) {
JL_GC_POP();
return (jl_value_t*)jl_bottom_type;
}
jl_svecset(p, i, ti);
}
}
if (a->name->primary != NULL) {
jl_value_t *res = (jl_value_t*)jl_apply_type(a->name->primary, p);
JL_GC_POP();
return res;
}
assert(0 && "not yet implemented");
return NULL;
}
static long meet_tuple_lengths(long bv, long vv, int *bot)
{
/*
do a meet over the lattice of tuple lengths:
>=0
| \
| 0
>=1
| \
| 1
>=2
| \
| 2
...
">=N" is represented as ~N
*/
if (bv < 0) {
if (vv < 0) {
if (bv < vv)
return bv;
else
return vv;
}
else {
if (~bv > vv) {
*bot = 1;
return 0;
}
}
}
else {
if (vv < 0) {
if (~vv > bv) {
*bot = 1;
return 0;
}
return bv;
}
else {
if (bv != vv) {
*bot = 1;
return 0;
}
}
}
return vv;
}
// convert a type to the value it would have if assigned to a static parameter
// in covariant context.
// example: {Type{Int},} => {DataType,}
// calling f{T}(x::T) as f({Int,}) should give T == {DataType,}, but we
// might temporarily represent this type as {Type{Int},} for more precision.
static jl_value_t *type_to_static_parameter_value(jl_value_t *t)
{
if (jl_is_type_type(t) && !jl_is_typevar(jl_tparam0(t)))
return jl_typeof(jl_tparam0(t));
if (jl_is_tuple_type(t)) {
jl_svec_t *p = ((jl_datatype_t*)t)->parameters;
size_t l = jl_svec_len(p);
int changed = 0;
jl_svec_t *np = jl_alloc_svec(l);
JL_GC_PUSH1(&np);
for(size_t i=0; i < l; i++) {
jl_value_t *el = type_to_static_parameter_value(jl_svecref(p,i));
jl_svecset(np, i, el);
if (el != jl_svecref(p,i))
changed = 1;
}
jl_value_t *result = changed ? (jl_value_t*)jl_apply_tuple_type(np) : t;
JL_GC_POP();
return result;
}
return t;
}
static int match_intersection_mode = 0;
static jl_value_t *meet_tvars(jl_tvar_t *a, jl_tvar_t *b);
static jl_value_t *intersect_typevar(jl_tvar_t *a, jl_value_t *b,
cenv_t *penv, cenv_t *eqc, variance_t var)
{
jl_value_t *both=NULL;
jl_tvar_t *new_b=NULL;
JL_GC_PUSH3(&b, &both, &new_b);
if (var == covariant) {
// matching T to Type{S} in covariant context
b = type_to_static_parameter_value(b);
}
if (jl_subtype(b, (jl_value_t*)a, 0)) {
if (!a->bound) {
JL_GC_POP();
return b;
}
}
else if (var==invariant && !jl_has_typevars_(b,0)) {
// for typevar a and non-typevar type b, b must be within a's bounds
// in invariant contexts.
JL_GC_POP();
return (jl_value_t*)jl_bottom_type;
}
else if (jl_subtype((jl_value_t*)a, b, 0)) {
/*
TODO: get sharper types when the overlap between a typevar and
a type is not simple. Ex:
tintersect(Type{Array{T,n}}, Type{typevar(:_,Vector)})
should give Type{_<:Vector}
*/
if (jl_is_typevar(b)) {
if (!((jl_tvar_t*)b)->bound){
JL_GC_POP();
return (jl_value_t*)a;
}
}
else {
if (!a->bound) {
JL_GC_POP();
return (jl_value_t*)a;
}
}
}
else {
b = jl_type_intersect(a->ub, b, penv, eqc, covariant);
if (b == jl_bottom_type) {
JL_GC_POP();
return b;
}
}
if ((jl_value_t*)a == b) {
JL_GC_POP();
return b;
}
if (var == invariant) {
if (!jl_has_typevars_(b,0) && !jl_is_typevar(b)) {
int i;
for(i=0; i < eqc->n; i+=2) {
if (eqc->data[i] == (jl_value_t*)a) {
jl_value_t *v = eqc->data[i+1];
if (jl_is_typevar(v))
continue;
if (!jl_types_equal(v, b)) {
JL_GC_POP();
return (jl_value_t*)jl_bottom_type;
}
break;
}
}
if (i >= eqc->n)
extend((jl_value_t*)a, b, eqc);
JL_GC_POP();
return (jl_value_t*)a;
}
if (jl_is_typevar(b)) {
both = meet_tvars(a, (jl_tvar_t*)b);
if (both == jl_bottom_type) {
JL_GC_POP();
return both;
}
if (!jl_is_typevar(both))
both = (jl_value_t*)jl_new_typevar(underscore_sym, jl_bottom_type, both);
extend((jl_value_t*)a, both, penv);
extend((jl_value_t*)b, both, penv);
}
if (is_btv(b))
extend(b, (jl_value_t*)a, eqc);
else
extend((jl_value_t*)a, b, eqc);
}
else {
int i;
for(i=0; i < penv->n; i+=2) {
if (penv->data[i] == (jl_value_t*)a && !jl_is_typevar(penv->data[i+1])) {
if (jl_types_equal(b, penv->data[i+1])) {
JL_GC_POP();
return (jl_value_t*)a;
}
jl_value_t *ti = jl_type_intersection(b, penv->data[i+1]);
if (ti == (jl_value_t*)jl_bottom_type) {
JL_GC_POP();
return ti;
}
break;
}
}
extend((jl_value_t*)a, b, penv);
if (jl_is_typevar(b)) {
JL_GC_POP();
return (jl_value_t*)a;
}
else {
new_b = jl_new_typevar(underscore_sym, jl_bottom_type, b);
extend((jl_value_t*)new_b, b, penv);
extend((jl_value_t*)new_b, (jl_value_t*)a, penv);
JL_GC_POP();
return (jl_value_t*)new_b;
}
}
JL_GC_POP();
return (jl_value_t*)a;
}
static jl_value_t *approxify_type(jl_datatype_t *dt, jl_svec_t *pp)
{
size_t i, l = jl_svec_len(dt->parameters);
jl_svec_t *p = jl_alloc_svec(l);
JL_GC_PUSH1(&p);
for(i=0; i < l; i++) {
jl_value_t *el = jl_svecref(dt->parameters, i);
if (jl_has_typevars_from(el, pp))
jl_svecset(p, i, jl_new_typevar(underscore_sym, jl_bottom_type, el));
else
jl_svecset(p, i, el);
}
jl_value_t *nt = jl_apply_type(dt->name->primary, p);
JL_GC_POP();
return nt;
}
static int has_ntuple_intersect_tuple = 0;
static jl_value_t *jl_type_intersect(jl_value_t *a, jl_value_t *b,
cenv_t *penv, cenv_t *eqc, variance_t var)
{
if (jl_is_typector(a))
a = (jl_value_t*)((jl_typector_t*)a)->body;
if (jl_is_typector(b))
b = (jl_value_t*)((jl_typector_t*)b)->body;
if (a == b) return a;
if (jl_is_typevar(a)) {
if (var == covariant && !((jl_tvar_t*)a)->bound)
a = ((jl_tvar_t*)a)->ub;
else if (a != jl_ANY_flag)
return intersect_typevar((jl_tvar_t*)a, b, penv, eqc, var);
}
if (jl_is_typevar(b)) {
if (var == covariant && !((jl_tvar_t*)b)->bound)
b = ((jl_tvar_t*)b)->ub;
else if (b != jl_ANY_flag)
return intersect_typevar((jl_tvar_t*)b, a, penv, eqc, var);
}
if (a == (jl_value_t*)jl_bottom_type || b == (jl_value_t*)jl_bottom_type)
return (jl_value_t*)jl_bottom_type;
if (!jl_has_typevars(a) && !jl_has_typevars(b)) {
if (jl_subtype(a, b, 0))
return a;
if (jl_subtype(b, a, 0))
return b;
}
// union
if (jl_is_uniontype(a))
return intersect_union((jl_uniontype_t*)a, b, penv, eqc, var);
if (jl_is_uniontype(b))
return intersect_union((jl_uniontype_t*)b, a, penv, eqc, var);
if (a == (jl_value_t*)jl_any_type || a == jl_ANY_flag) return b;
if (b == (jl_value_t*)jl_any_type || b == jl_ANY_flag) return a;
// tuple
if (jl_is_tuple_type(a)) {
long alen = (long)jl_nparams(a);
jl_value_t *temp=NULL;
JL_GC_PUSH2(&b, &temp);
if (jl_is_ntuple_type(b)) {
has_ntuple_intersect_tuple = 1;
jl_value_t *lenvar = jl_tparam0(b);
jl_value_t *elty = jl_tparam1(b);
int i;
for(i=0; i < eqc->n; i+=2) {
if (eqc->data[i] == lenvar) {
jl_value_t *v = eqc->data[i+1];
if (jl_is_long(v)) {
// N is already known in NTuple{N,...}
alen = jl_unbox_long(v);
break;
}
}
}
b = (jl_value_t*)jl_tupletype_fill(alen, elty);
if (i >= eqc->n) {
// don't know N yet, so add a constraint for it based on
// the length of the other tuple
if (jl_is_va_tuple((jl_datatype_t*)a)) {
temp = (jl_value_t*)jl_svec_copy(((jl_datatype_t*)b)->parameters);
jl_svecset(temp, alen-1, jl_wrap_vararg(elty));
b = (jl_value_t*)jl_apply_tuple_type((jl_svec_t*)temp);
if (jl_is_typevar(lenvar)) {
// store "at least N" constraints in the <: env
for(i=0; i < penv->n; i+=2) {
if (penv->data[i] == lenvar) {
jl_value_t *v = penv->data[i+1];
if (jl_is_long(v)) {
int bot = 0;
long met = meet_tuple_lengths(~jl_unbox_long(v),
~(alen-1), &bot);
if (bot) {
JL_GC_POP();
return (jl_value_t*)jl_bottom_type;
}
penv->data[i+1] = jl_box_long(~met);
break;
}
}
}
if (i >= penv->n) {
temp = jl_box_long(alen-1);
extend(lenvar, temp, penv);
}
}
}
else {
if (jl_is_typevar(lenvar)) {
// store "== N" constraints in the == env
temp = jl_box_long(alen);
if (intersect_typevar((jl_tvar_t*)lenvar,temp,penv,eqc,
invariant) ==
(jl_value_t*)jl_bottom_type) {
JL_GC_POP();
return (jl_value_t*)jl_bottom_type;
}
}
}
}
}
if (jl_is_type_type(b)) {
jl_value_t *btp0v = jl_tparam0(b);
if (jl_is_typevar(btp0v)) {
jl_tvar_t *btp0 = (jl_tvar_t*)btp0v;
if (jl_subtype(btp0->ub, a, 1)) {
JL_GC_POP();
return b;
}
}
}
if (jl_is_tuple_type(b)) {
a = intersect_tuple((jl_datatype_t*)a, (jl_datatype_t*)b, penv,eqc,var);
JL_GC_POP();
return a;
}
JL_GC_POP();
}
if (jl_is_tuple_type(b)) {
return jl_type_intersect(b, a, penv,eqc,var);
}
if (jl_is_ntuple_type(a) && jl_is_type_type(b)) {
jl_value_t *temp = a;
a = b;
b = temp;
}
// tag
if (!jl_is_datatype(a) || !jl_is_datatype(b))
return (jl_value_t*)jl_bottom_type;
jl_datatype_t *tta = (jl_datatype_t*)a;
jl_datatype_t *ttb = (jl_datatype_t*)b;
if (tta->name == ttb->name)
return (jl_value_t*)intersect_tag(tta, ttb, penv, eqc, var);
jl_datatype_t *super = NULL;
jl_datatype_t *sub = NULL;
jl_value_t *env = NULL;
jl_value_t *p = NULL;
jl_value_t *temp3 = NULL;
JL_GC_PUSH5(&super, &sub, &env, &p, &temp3);
while (tta != jl_any_type) {
if (tta->name == ttb->name) {
sub = (jl_datatype_t*)a;
super = (jl_datatype_t*)b;
break;
}
tta = tta->super;
}
if (sub == NULL) {
tta = (jl_datatype_t*)a;
while (ttb != jl_any_type) {
if (tta->name == ttb->name) {
sub = (jl_datatype_t*)b;
super = (jl_datatype_t*)a;
break;
}
ttb = ttb->super;
}
if (sub == NULL) {
JL_GC_POP();
return (jl_value_t*)jl_bottom_type;
}
}
if (sub->super == jl_type_type && jl_is_type_type((jl_value_t*)super)) {
// subtypes of Type like DataType do not constrain the type
// parameter, and yet contain Type instances with a more specific
// parameter (like Type{Int}). This is a special case.
jl_value_t *tp0 = jl_tparam0(super);
if (jl_is_typevar(tp0) || (jl_value_t*)sub == jl_typeof(tp0)) {
JL_GC_POP();
return (jl_value_t*)super;
}
JL_GC_POP();
return (jl_value_t*)jl_bottom_type;
}
/*
issue #6387
Say we have
type DateRange{C} <: Range{Date{C}}; end
and
vcat{T}(r::Range{T}) = ...
Then inferring vcat(::DateRange) concludes that T==Date{C}, but it should
conclude T<:Date{C}. The core problem seems to be that in moving from a
type to its supertype, we drop the environment that binds C --- we
forget that C is a variable in Range{Date{C}}. For now I work around this
by rewriting this type to Range{_<:Date{C}}, effectively tagging type
parameters that are variable due to the extra (dropped) environment.
*/
if (var == covariant &&
sub == (jl_datatype_t*)sub->name->primary &&
jl_has_typevars_from((jl_value_t*)sub->super, ((jl_datatype_t*)sub->name->primary)->parameters))
env = approxify_type((jl_datatype_t*)sub->super, ((jl_datatype_t*)sub->name->primary)->parameters);
else
env = (jl_value_t*)sub->super;
super = (jl_datatype_t*)jl_type_intersect((jl_value_t*)env, (jl_value_t*)super, penv, eqc, var);
if ((jl_value_t*)super == jl_bottom_type) {
JL_GC_POP();
return (jl_value_t*)jl_bottom_type;
}
// super needs to be instantiated so the matching below finds actual types
// and doesn't fail due to the presence of extra typevars.
super = (jl_datatype_t*)jl_instantiate_type_with((jl_value_t*)super, eqc->data, eqc->n/2);
size_t n = jl_svec_len(sub->parameters);
assert(sub->name->primary != NULL);
jl_value_t *tc = sub->name->primary;
jl_svec_t *tc_params = ((jl_datatype_t*)tc)->parameters;
// compute what constraints the supertype imposes on the subtype
jl_svec_t *subs_sup_params =
((jl_datatype_t*)((jl_datatype_t*)tc)->super)->parameters;
// match the intersected supertype against the pattern this subtype
// uses to instantiate its supertype. this tells us what subtype parameter
// values are implied by the intersected supertype, or that the
// intersected supertype cannot come from this subtype (in which case
// our final answer is Union()).
size_t i;
// hack: we need type_match to find assignments for all typevars