-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypechk.c
8131 lines (7003 loc) · 261 KB
/
typechk.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
#include "typechk.h"
#include "alloc.h"
#include "ap_val.h"
#include "builtins.h"
#include "compiler.h"
#include "diagnostics.h"
#include "hashtbl.h"
#include "lex.h"
#include "log.h"
#include "parse.h"
#include "program.h"
#include "target.h"
#include "util.h"
#include "var_table.h"
#include "vector.h"
#include <ctype.h>
struct td_var_ty TD_VAR_TY_UNKNOWN = {.ty = TD_VAR_TY_TY_UNKNOWN};
struct td_var_ty TD_VAR_TY_VOID = {.ty = TD_VAR_TY_TY_VOID};
struct td_var_ty TD_VAR_TY_VOID_POINTER = {
.ty = TD_VAR_TY_TY_POINTER, .pointer = {.underlying = &TD_VAR_TY_VOID}};
struct td_var_ty TD_VAR_TY_CHAR_POINTER = {
.ty = TD_VAR_TY_TY_POINTER,
.pointer = {.underlying = &TD_VAR_TY_WELL_KNOWN_CHAR}};
struct td_var_ty TD_VAR_TY_INT_POINTER = {
.ty = TD_VAR_TY_TY_POINTER,
.pointer = {.underlying = &TD_VAR_TY_WELL_KNOWN_SIGNED_INT}};
struct td_var_ty TD_VAR_TY_CONST_CHAR_POINTER = {
.ty = TD_VAR_TY_TY_POINTER,
.type_qualifiers = TD_TYPE_QUALIFIER_FLAG_CONST,
.pointer = {.underlying = &TD_VAR_TY_WELL_KNOWN_CHAR}};
#define MAKE_WKT(name) \
struct td_var_ty TD_VAR_TY_WELL_KNOWN_##name = { \
.ty = TD_VAR_TY_TY_WELL_KNOWN, .well_known = WELL_KNOWN_TY_##name}
MAKE_WKT(BOOL);
MAKE_WKT(CHAR);
MAKE_WKT(SIGNED_CHAR);
MAKE_WKT(UNSIGNED_CHAR);
MAKE_WKT(SIGNED_SHORT);
MAKE_WKT(UNSIGNED_SHORT);
MAKE_WKT(SIGNED_INT);
MAKE_WKT(UNSIGNED_INT);
MAKE_WKT(SIGNED_LONG);
MAKE_WKT(UNSIGNED_LONG);
MAKE_WKT(SIGNED_LONG_LONG);
MAKE_WKT(UNSIGNED_LONG_LONG);
MAKE_WKT(HALF);
MAKE_WKT(FLOAT);
MAKE_WKT(DOUBLE);
MAKE_WKT(LONG_DOUBLE);
#undef MAKE_WKT
struct typechk {
struct arena_allocator *arena;
struct parser *parser;
const struct target *target;
const struct compile_args *args;
size_t next_anonymous_type_name_id;
// `returns` need to know what they coerce to
struct td_var_ty ret_ty;
// `value` contains a `struct td_var_ty *` to the type of the variable
// or NULL if the variable has been used without a declaration
struct var_table var_table;
// types (e.g declared structs)
struct var_table ty_table;
struct hashtbl *builtin_fns;
enum typechk_result_ty result_ty;
struct compiler_diagnostics *diagnostics;
};
static struct td_var_ty
get_target_for_variadic(const struct td_var_ty *ty_ref) {
// floats are promoted to doubles and types smaller than int are promoted to
// int
if (ty_ref->ty != TD_VAR_TY_TY_WELL_KNOWN) {
return *ty_ref;
}
if (ty_ref->well_known == WELL_KNOWN_TY_FLOAT) {
return TD_VAR_TY_WELL_KNOWN_DOUBLE;
} else if (ty_ref->well_known < WELL_KNOWN_TY_SIGNED_INT) {
return TD_VAR_TY_WELL_KNOWN_SIGNED_INT;
}
return *ty_ref;
}
bool td_binary_op_is_comparison(enum td_binary_op_ty ty) {
switch (ty) {
case TD_BINARY_OP_TY_EQ:
case TD_BINARY_OP_TY_NEQ:
case TD_BINARY_OP_TY_GT:
case TD_BINARY_OP_TY_GTEQ:
case TD_BINARY_OP_TY_LT:
case TD_BINARY_OP_TY_LTEQ:
case TD_BINARY_OP_TY_LOGICAL_OR:
case TD_BINARY_OP_TY_LOGICAL_AND:
return true;
case TD_BINARY_OP_TY_OR:
case TD_BINARY_OP_TY_AND:
case TD_BINARY_OP_TY_XOR:
case TD_BINARY_OP_TY_LSHIFT:
case TD_BINARY_OP_TY_RSHIFT:
case TD_BINARY_OP_TY_ADD:
case TD_BINARY_OP_TY_SUB:
case TD_BINARY_OP_TY_MUL:
case TD_BINARY_OP_TY_DIV:
case TD_BINARY_OP_TY_MOD:
return false;
}
}
bool td_var_ty_is_fp_ty(const struct td_var_ty *ty) {
if (ty->ty != TD_VAR_TY_TY_WELL_KNOWN) {
return false;
}
switch (ty->well_known) {
case WELL_KNOWN_TY_HALF:
case WELL_KNOWN_TY_FLOAT:
case WELL_KNOWN_TY_DOUBLE:
case WELL_KNOWN_TY_LONG_DOUBLE:
return true;
case WELL_KNOWN_TY_BOOL:
case WELL_KNOWN_TY_CHAR:
case WELL_KNOWN_TY_SIGNED_CHAR:
case WELL_KNOWN_TY_UNSIGNED_CHAR:
case WELL_KNOWN_TY_SIGNED_SHORT:
case WELL_KNOWN_TY_UNSIGNED_SHORT:
case WELL_KNOWN_TY_SIGNED_INT:
case WELL_KNOWN_TY_UNSIGNED_INT:
case WELL_KNOWN_TY_SIGNED_LONG:
case WELL_KNOWN_TY_UNSIGNED_LONG:
case WELL_KNOWN_TY_SIGNED_LONG_LONG:
case WELL_KNOWN_TY_UNSIGNED_LONG_LONG:
case WELL_KNOWN_TY_UINT128:
case WELL_KNOWN_TY_INT128:
return false;
}
}
static bool try_get_completed_aggregate(struct typechk *tchk,
const struct td_var_ty *var_ty,
struct td_var_ty *complete);
enum FLAG_ENUM td_var_ty_compatible_flags {
TD_VAR_TY_COMPATIBLE_FLAG_NONE = 0,
TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_LHS = 1 << 0,
TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_RHS = 1 << 1,
TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_RECURSIVE = 1 << 2,
TD_VAR_TY_COMPATIBLE_FLAG_CANONICALIZE = 1 << 3,
TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_ALL =
TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_LHS |
TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_RHS
};
static struct td_var_ty
td_var_ty_lvalue_convert(struct typechk *tchk, const struct td_var_ty *var_ty) {
// arrays and functions decay to pointers
// const/volatile/restrict qualifiers stripped
struct td_var_ty lvalue_cvt = *var_ty;
lvalue_cvt.type_qualifiers = TD_TYPE_QUALIFIER_FLAG_NONE;
if (lvalue_cvt.ty == TD_VAR_TY_TY_ARRAY) {
lvalue_cvt = td_var_ty_make_pointer(tchk, lvalue_cvt.array.underlying,
TD_TYPE_QUALIFIER_FLAG_NONE);
} else if (lvalue_cvt.ty == TD_VAR_TY_TY_FUNC) {
lvalue_cvt =
td_var_ty_make_pointer(tchk, &lvalue_cvt, TD_TYPE_QUALIFIER_FLAG_NONE);
}
return lvalue_cvt;
}
// turns long long to long on LP64, and long to int on LP32
// for easier comparison
static enum well_known_ty canonicalize_wkt(struct typechk *tchk,
enum well_known_ty wkt) {
switch (tchk->target->lp_sz) {
case TARGET_LP_SZ_LP32:
switch (wkt) {
case WELL_KNOWN_TY_SIGNED_LONG:
return WELL_KNOWN_TY_SIGNED_INT;
case WELL_KNOWN_TY_UNSIGNED_LONG:
return WELL_KNOWN_TY_UNSIGNED_INT;
default:
return wkt;
}
case TARGET_LP_SZ_LP64:
switch (wkt) {
case WELL_KNOWN_TY_SIGNED_LONG_LONG:
return WELL_KNOWN_TY_SIGNED_LONG;
case WELL_KNOWN_TY_UNSIGNED_LONG_LONG:
return WELL_KNOWN_TY_UNSIGNED_LONG;
default:
return wkt;
}
}
}
static bool td_var_ty_compatible(struct typechk *tchk,
const struct td_var_ty *l0,
const struct td_var_ty *r0,
enum td_var_ty_compatible_flags flags) {
struct td_var_ty l = *l0;
struct td_var_ty r = *r0;
if (l.ty == TD_VAR_TY_TY_INCOMPLETE_AGGREGATE) {
struct td_var_ty complete;
if (try_get_completed_aggregate(tchk, &l, &complete)) {
l = complete;
}
}
if (r.ty == TD_VAR_TY_TY_INCOMPLETE_AGGREGATE) {
struct td_var_ty complete;
if (try_get_completed_aggregate(tchk, &r, &complete)) {
r = complete;
}
}
if (flags & TD_VAR_TY_COMPATIBLE_FLAG_CANONICALIZE) {
flags &= ~TD_VAR_TY_COMPATIBLE_FLAG_CANONICALIZE;
if (l.ty == TD_VAR_TY_TY_WELL_KNOWN) {
l.well_known = canonicalize_wkt(tchk, l.well_known);
}
if (r.ty == TD_VAR_TY_TY_WELL_KNOWN) {
r.well_known = canonicalize_wkt(tchk, r.well_known);
}
}
if (flags & TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_LHS) {
if (!(flags & TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_RECURSIVE)) {
flags &= ~TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_LHS;
}
l = td_var_ty_lvalue_convert(tchk, &l);
}
if (flags & TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_RHS) {
if (!(flags & TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_RECURSIVE)) {
flags &= ~TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_RHS;
}
r = td_var_ty_lvalue_convert(tchk, &r);
}
if (l.type_qualifiers != r.type_qualifiers) {
return false;
}
if (l.ty != r.ty) {
// possible bug source: what if one is incomplete aggregate and one is
// complete aggregate? this will return false but maybe shouldn't
return false;
}
switch (l.ty) {
case TD_VAR_TY_TY_UNKNOWN:
case TD_VAR_TY_TY_VOID:
return true;
case TD_VAR_TY_TY_VARIADIC:
BUG("doesn't make sense");
case TD_VAR_TY_TY_WELL_KNOWN:
return l.well_known == r.well_known;
case TD_VAR_TY_TY_FUNC: {
struct td_ty_func l_func = l.func;
struct td_ty_func r_func = r.func;
if (!td_var_ty_compatible(tchk, l_func.ret, r_func.ret, flags)) {
return false;
}
if (l_func.ty == TD_TY_FUNC_TY_UNKNOWN_ARGS ||
r_func.ty == TD_TY_FUNC_TY_UNKNOWN_ARGS) {
// TODO: absolute minefield. from C spec
//
// * one is an old-style (parameter-less) definition, the other has a
// parameter list,
// the parameter list does not use an ellipsis and each parameter is
// compatible (after function parameter type adjustment) with the
// corresponding old-style parameter after default argument promotions
//
// * one is an old-style (parameter-less) declaration, the other has a
// parameter list, the parameter list does not use an ellipsis,
// and all parameters (after function parameter type adjustment) are
// unaffected by default argument promotions
return true;
}
if (l_func.ty != r_func.ty) {
return false;
}
if (l_func.num_params != r_func.num_params) {
return false;
}
size_t num_params = l_func.num_params;
for (size_t i = 0; i < num_params; i++) {
const struct td_var_ty *lp = &l_func.params[i].var_ty;
const struct td_var_ty *rp = &r_func.params[i].var_ty;
if (lp->ty == TD_VAR_TY_TY_VARIADIC || rp->ty == TD_VAR_TY_TY_VARIADIC) {
continue;
}
if (!td_var_ty_compatible(
tchk, lp, rp,
TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_LHS |
TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_RHS)) {
return false;
}
}
return true;
}
case TD_VAR_TY_TY_POINTER: {
struct td_var_ty l_underlying = td_var_ty_get_underlying(tchk, &l);
struct td_var_ty r_underlying = td_var_ty_get_underlying(tchk, &r);
return td_var_ty_compatible(tchk, &l_underlying, &r_underlying, flags);
}
case TD_VAR_TY_TY_ARRAY: {
// unknown size array type is turned to pointer, but we don't know how to
// distinguish that from normal pointer, so this is wrong there as it should
// do `if (one_is_pointer) { return true }`
if (l.array.size != r.array.size) {
return false;
}
struct td_var_ty l_underlying = td_var_ty_get_underlying(tchk, &l);
struct td_var_ty r_underlying = td_var_ty_get_underlying(tchk, &r);
return td_var_ty_compatible(tchk, &l_underlying, &r_underlying, flags);
}
case TD_VAR_TY_TY_AGGREGATE: {
struct td_ty_aggregate l_agg = l.aggregate;
struct td_ty_aggregate r_agg = r.aggregate;
if (l_agg.ty != r_agg.ty) {
return false;
}
// aggregate types are the same iff they have the same name or come from the
// same declaration we give anonymous types a name per-declaration
return ustr_eq(l_agg.name, r_agg.name);
}
case TD_VAR_TY_TY_INCOMPLETE_AGGREGATE: {
// same logic as for aggregate
struct td_ty_incomplete_aggregate l_agg = l.incomplete_aggregate;
struct td_ty_incomplete_aggregate r_agg = r.incomplete_aggregate;
if (l_agg.ty != r_agg.ty) {
return false;
}
return ustr_eq(l_agg.name, r_agg.name);
}
}
}
// FIXME: consider qualifiers, and "compatible" types rather than just equal
// this method likely still needed, but most consumers of it care about
// "compatible"
bool td_var_ty_eq(struct typechk *tchk, const struct td_var_ty *l,
const struct td_var_ty *r) {
if (l->ty != r->ty) {
return false;
}
switch (l->ty) {
case TD_VAR_TY_TY_UNKNOWN:
BUG("comparing unknown types");
case TD_VAR_TY_TY_VOID:
return true;
case TD_VAR_TY_TY_WELL_KNOWN:
return l->well_known == r->well_known;
case TD_VAR_TY_TY_FUNC: {
struct td_ty_func l_func = l->func;
struct td_ty_func r_func = r->func;
if (!td_var_ty_eq(tchk, l_func.ret, r_func.ret)) {
return false;
}
if (l_func.ty == TD_TY_FUNC_TY_UNKNOWN_ARGS ||
r_func.ty == TD_TY_FUNC_TY_UNKNOWN_ARGS) {
return true;
}
if (l_func.ty != r_func.ty) {
return false;
}
if (l_func.num_params != r_func.num_params) {
return false;
}
size_t num_params = l_func.num_params;
for (size_t i = 0; i < num_params; i++) {
if (!td_var_ty_eq(tchk, &l_func.params[i].var_ty,
&r_func.params[i].var_ty)) {
return false;
}
}
return true;
}
case TD_VAR_TY_TY_ARRAY:
if (l->array.size != r->array.size) {
return false;
}
// cast never needed for actual array?
return true;
case TD_VAR_TY_TY_POINTER: {
// FIXME: why did i change this to this
return r->ty == TD_VAR_TY_TY_POINTER;
// struct td_var_ty l_underlying = td_var_ty_get_underlying(tchk, l);
// struct td_var_ty r_underlying = td_var_ty_get_underlying(tchk, r);
// return td_var_ty_eq(tchk, &l_underlying, &r_underlying);
}
case TD_VAR_TY_TY_VARIADIC:
return true;
case TD_VAR_TY_TY_AGGREGATE: {
struct td_ty_aggregate l_agg = l->aggregate;
struct td_ty_aggregate r_agg = r->aggregate;
if (l_agg.ty != r_agg.ty) {
return false;
}
// aggregate types are the same iff they have the same name or come from the
// same declaration we give anonymous types a name per-declaration
return ustr_eq(l_agg.name, r_agg.name);
}
case TD_VAR_TY_TY_INCOMPLETE_AGGREGATE: {
struct td_ty_incomplete_aggregate l_agg = l->incomplete_aggregate;
struct td_ty_incomplete_aggregate r_agg = r->incomplete_aggregate;
if (l_agg.ty != r_agg.ty) {
return false;
}
// aggregate types are the same iff they have the same name or come from the
// same declaration we give anonymous types a name per-declaration
return ustr_eq(l_agg.name, r_agg.name);
}
}
}
bool td_var_ty_is_integral_ty(const struct td_var_ty *ty) {
if (ty->ty != TD_VAR_TY_TY_WELL_KNOWN) {
return false;
}
switch (ty->well_known) {
case WELL_KNOWN_TY_BOOL:
case WELL_KNOWN_TY_CHAR:
case WELL_KNOWN_TY_SIGNED_CHAR:
case WELL_KNOWN_TY_UNSIGNED_CHAR:
case WELL_KNOWN_TY_SIGNED_SHORT:
case WELL_KNOWN_TY_UNSIGNED_SHORT:
case WELL_KNOWN_TY_SIGNED_INT:
case WELL_KNOWN_TY_UNSIGNED_INT:
case WELL_KNOWN_TY_SIGNED_LONG:
case WELL_KNOWN_TY_UNSIGNED_LONG:
case WELL_KNOWN_TY_SIGNED_LONG_LONG:
case WELL_KNOWN_TY_UNSIGNED_LONG_LONG:
case WELL_KNOWN_TY_INT128:
case WELL_KNOWN_TY_UINT128:
return true;
case WELL_KNOWN_TY_HALF:
case WELL_KNOWN_TY_FLOAT:
case WELL_KNOWN_TY_DOUBLE:
case WELL_KNOWN_TY_LONG_DOUBLE:
return false;
}
}
bool td_var_ty_is_scalar_ty(const struct td_var_ty *ty) {
// FIXME: also nullptr_t in C23
return td_var_ty_is_integral_ty(ty) || td_var_ty_is_fp_ty(ty) ||
ty->ty == TD_VAR_TY_TY_POINTER;
}
struct td_var_ty td_var_ty_pointer_sized_int(struct typechk *tchk,
bool is_signed) {
// returns the type for `size_t` effectively
// TODO: generalise - we should have a special ptr-sized int type
enum well_known_ty wkt;
switch (tchk->target->lp_sz) {
case TARGET_LP_SZ_LP32:
wkt = is_signed ? WELL_KNOWN_TY_SIGNED_INT : WELL_KNOWN_TY_UNSIGNED_INT;
break;
case TARGET_LP_SZ_LP64:
wkt = is_signed ? WELL_KNOWN_TY_SIGNED_LONG_LONG
: WELL_KNOWN_TY_UNSIGNED_LONG_LONG;
break;
}
return (struct td_var_ty){.ty = TD_VAR_TY_TY_WELL_KNOWN, .well_known = wkt};
}
struct td_var_ty
td_var_ty_make_pointer(struct typechk *tchk, const struct td_var_ty *var_ty,
enum td_type_qualifier_flags qualifiers) {
// we don't know lifetime of the other one so need to copy it
// TODO: cache types
struct td_var_ty *copied = aralloc(tchk->arena, sizeof(*copied));
*copied = *var_ty;
return (struct td_var_ty){.ty = TD_VAR_TY_TY_POINTER,
.type_qualifiers = qualifiers,
.pointer =
(struct td_ty_pointer){.underlying = copied}};
}
struct td_var_ty td_var_ty_get_underlying(UNUSED_ARG(struct typechk *tchk),
const struct td_var_ty *ty_ref) {
switch (ty_ref->ty) {
case TD_VAR_TY_TY_UNKNOWN:
return TD_VAR_TY_UNKNOWN;
case TD_VAR_TY_TY_POINTER:
return *ty_ref->pointer.underlying;
case TD_VAR_TY_TY_ARRAY:
return *ty_ref->array.underlying;
default:
BUG("non pointer/array/tagged passed (type %u)", ty_ref->ty);
}
}
UNUSED static struct td_var_ty
td_var_ty_promote_integer(UNUSED_ARG(struct typechk *tchk),
const struct td_var_ty *ty_ref) {
DEBUG_ASSERT(ty_ref->ty != TD_VAR_TY_TY_UNKNOWN, "unknown ty in call to `%s`",
__func__);
if (ty_ref->ty != TD_VAR_TY_TY_WELL_KNOWN ||
ty_ref->well_known >= WELL_KNOWN_TY_SIGNED_INT) {
return *ty_ref;
}
// all values smaller than int are promoted to int
// FIXME: wrong on EEP, as unsigned short should promote to unsigned int due
// to both being 16 bits
return TD_VAR_TY_WELL_KNOWN_SIGNED_INT;
}
static struct td_expr add_cast_expr(struct typechk *tchk, struct td_expr expr,
struct td_var_ty target_ty) {
struct td_expr td_expr = (struct td_expr){
.ty = TD_EXPR_TY_UNARY_OP,
.var_ty = target_ty,
.unary_op =
(struct td_unary_op){
.ty = TD_UNARY_OP_TY_CAST,
.expr = aralloc(tchk->arena, sizeof(*td_expr.unary_op.expr)),
.cast = (struct td_cast){.var_ty = target_ty}},
.span = expr.span};
*td_expr.unary_op.expr = expr;
return td_expr;
}
static char *tchk_base_type_name(struct typechk *tchk,
const struct td_var_ty *var_ty) {
const char *kw;
ustr_t name;
switch (var_ty->ty) {
case TD_VAR_TY_TY_UNKNOWN:
return "<err-ty>";
case TD_VAR_TY_TY_VOID:
return "void";
case TD_VAR_TY_TY_WELL_KNOWN:
switch (var_ty->well_known) {
case WELL_KNOWN_TY_CHAR:
return "char";
case WELL_KNOWN_TY_SIGNED_CHAR:
return "signed char";
case WELL_KNOWN_TY_UNSIGNED_CHAR:
return "unsigned char";
case WELL_KNOWN_TY_SIGNED_SHORT:
return "short";
case WELL_KNOWN_TY_UNSIGNED_SHORT:
return "unsigned short";
case WELL_KNOWN_TY_SIGNED_INT:
return "int";
case WELL_KNOWN_TY_UNSIGNED_INT:
return "unsigned int";
case WELL_KNOWN_TY_SIGNED_LONG:
return "long";
case WELL_KNOWN_TY_UNSIGNED_LONG:
return "unsigned long";
case WELL_KNOWN_TY_SIGNED_LONG_LONG:
return "long long";
case WELL_KNOWN_TY_UNSIGNED_LONG_LONG:
return "unsigned long long";
case WELL_KNOWN_TY_INT128:
return "int128";
case WELL_KNOWN_TY_UINT128:
return "uint128";
case WELL_KNOWN_TY_BOOL:
return "bool";
case WELL_KNOWN_TY_HALF:
// hmm, we probably want to show `__Fp16` or what is actually used
// (same for typedefs)
return "half";
case WELL_KNOWN_TY_FLOAT:
return "float";
case WELL_KNOWN_TY_DOUBLE:
return "double";
case WELL_KNOWN_TY_LONG_DOUBLE:
return "long double";
}
case TD_VAR_TY_TY_FUNC: {
// FIXME: this is wrong, e.g it will do `**(char bar())` instead of `char
// (**bar)`
const char *ret_name = tchk_type_name(tchk, var_ty->func.ret);
char *base = aralloc_snprintf(tchk->arena, "%s (*)(", ret_name);
for (size_t i = 0; i < var_ty->func.num_params; i++) {
if (i + 1 == var_ty->func.num_params) {
base = aralloc_snprintf(
tchk->arena, "%s%s)", base,
tchk_type_name(tchk, &var_ty->func.params[i].var_ty));
} else {
base = aralloc_snprintf(
tchk->arena, "%s%s, ", base,
tchk_type_name(tchk, &var_ty->func.params[i].var_ty));
}
}
return base;
}
case TD_VAR_TY_TY_POINTER: {
const char *el_name = tchk_type_name(tchk, var_ty->array.underlying);
return aralloc_snprintf(tchk->arena, "%s *", el_name);
}
case TD_VAR_TY_TY_ARRAY: {
const char *el_name = tchk_type_name(tchk, var_ty->array.underlying);
return aralloc_snprintf(tchk->arena, "%s[%zu]", el_name,
var_ty->array.size);
}
case TD_VAR_TY_TY_VARIADIC:
return "...";
case TD_VAR_TY_TY_AGGREGATE:
switch (var_ty->aggregate.ty) {
case TD_TY_AGGREGATE_TY_STRUCT:
kw = "struct";
break;
case TD_TY_AGGREGATE_TY_UNION:
kw = "union";
break;
}
// FIXME: will print <anonymous> type name for anonymous types
name = var_ty->aggregate.name;
goto named;
case TD_VAR_TY_TY_INCOMPLETE_AGGREGATE:
switch (var_ty->incomplete_aggregate.ty) {
case TD_TY_AGGREGATE_TY_STRUCT:
kw = "struct";
break;
case TD_TY_AGGREGATE_TY_UNION:
kw = "union";
break;
}
name = var_ty->incomplete_aggregate.name;
goto named;
named: {
return aralloc_snprintf(tchk->arena, "%s %.*s", kw, (int)name.len,
name.str);
}
}
}
// returns qualifiers _with a trailing space_
static char *tchk_qualifiers_name(struct typechk *tchk,
enum td_type_qualifier_flags flags) {
if (!flags) {
return "";
}
size_t len = 0;
#define TQUAL(name, str) \
if ((flags & (name))) { \
len += strlen((str)) + 1; \
}
TQUAL(TD_TYPE_QUALIFIER_FLAG_CONST, "const")
TQUAL(TD_TYPE_QUALIFIER_FLAG_VOLATILE, "volatile")
TQUAL(TD_TYPE_QUALIFIER_FLAG_RESTRICT, "restrict")
TQUAL(TD_TYPE_QUALIFIER_FLAG_NULLABLE, "_Optional")
#undef TQUAL
char *buf = aralloc(tchk->arena, len + 1);
size_t head = 0;
#define TQUAL(name, str) \
if ((flags & (name))) { \
strcpy(&buf[head], str " "); \
head += strlen((str)) + 1; \
}
TQUAL(TD_TYPE_QUALIFIER_FLAG_CONST, "const")
TQUAL(TD_TYPE_QUALIFIER_FLAG_VOLATILE, "volatile")
TQUAL(TD_TYPE_QUALIFIER_FLAG_RESTRICT, "restrict")
TQUAL(TD_TYPE_QUALIFIER_FLAG_NULLABLE, "_Optional")
#undef TQUAL
buf[head++] = '\0';
DEBUG_ASSERT(head == len + 1,
"str buf sizing went wrong (head %zu expected %zu)", head,
len + 1);
return buf;
}
char *tchk_type_name(struct typechk *tchk, const struct td_var_ty *var_ty) {
return aralloc_snprintf(
tchk->arena, "%s%s", tchk_qualifiers_name(tchk, var_ty->type_qualifiers),
tchk_base_type_name(tchk, var_ty));
}
enum cast_ty { CAST_TY_NONE, CAST_TY_CAST, CAST_TY_ERR };
static enum cast_ty is_cast_needed(struct typechk *tchk,
const struct td_var_ty *var_ty,
const struct td_var_ty *target_ty) {
if (target_ty->ty == TD_VAR_TY_TY_UNKNOWN ||
var_ty->ty == TD_VAR_TY_TY_UNKNOWN) {
return CAST_TY_NONE;
}
if (target_ty->ty == TD_VAR_TY_TY_VOID) {
return CAST_TY_NONE;
}
if (td_var_ty_compatible(tchk, var_ty, target_ty,
TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_LHS |
TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_RHS)) {
return CAST_TY_NONE;
}
if (td_var_ty_is_integral_ty(target_ty)) {
if (td_var_ty_is_integral_ty(var_ty) || td_var_ty_is_fp_ty(var_ty) ||
var_ty->ty == TD_VAR_TY_TY_POINTER) {
return CAST_TY_CAST;
}
return CAST_TY_ERR;
}
if (td_var_ty_is_fp_ty(target_ty)) {
if (td_var_ty_is_integral_ty(var_ty) || td_var_ty_is_fp_ty(var_ty)) {
return CAST_TY_CAST;
}
return CAST_TY_ERR;
}
if (target_ty->ty == TD_VAR_TY_TY_POINTER) {
if (td_var_ty_is_integral_ty(var_ty) ||
var_ty->ty == TD_VAR_TY_TY_POINTER ||
var_ty->ty == TD_VAR_TY_TY_ARRAY || var_ty->ty == TD_VAR_TY_TY_FUNC) {
return CAST_TY_CAST;
}
return CAST_TY_ERR;
}
return CAST_TY_ERR;
}
static struct td_expr add_cast_if_needed(struct typechk *tchk,
struct text_span context,
struct td_expr expr,
struct td_var_ty target_ty) {
if (expr.var_ty.ty == TD_VAR_TY_TY_FUNC) {
// decay to ptr
expr.var_ty =
td_var_ty_make_pointer(tchk, &expr.var_ty, TD_TYPE_QUALIFIER_FLAG_NONE);
}
if (expr.ty == TD_EXPR_TY_CNST && expr.var_ty.ty == TD_VAR_TY_TY_POINTER &&
target_ty.ty == TD_VAR_TY_TY_ARRAY) {
// FIXME: should validate rhs here
// HACK: change the string literal type to array so ir build knows to have
// it inline
// do we need to do this in explicit casts (`type_cast`) too?
expr.var_ty = target_ty;
return expr;
}
switch (is_cast_needed(tchk, &expr.var_ty, &target_ty)) {
case CAST_TY_NONE:
return expr;
case CAST_TY_CAST:
return add_cast_expr(tchk, expr, target_ty);
case CAST_TY_ERR:
tchk->result_ty = TYPECHK_RESULT_TY_FAILURE;
compiler_diagnostics_add(
tchk->diagnostics,
MK_SEMANTIC_DIAGNOSTIC(
CAST, cast, context, MK_INVALID_TEXT_POS(0),
aralloc_snprintf(tchk->arena,
"cast is not legal (from '%s' to '%s')",
tchk_type_name(tchk, &expr.var_ty),
tchk_type_name(tchk, &target_ty))));
return expr;
}
}
static struct td_expr perform_integer_promotion(struct typechk *tchk,
struct td_expr expr) {
if (expr.var_ty.ty == TD_VAR_TY_TY_WELL_KNOWN &&
expr.var_ty.well_known < WELL_KNOWN_TY_SIGNED_INT) {
struct td_var_ty target_ty = TD_VAR_TY_WELL_KNOWN_SIGNED_INT;
return add_cast_expr(tchk, expr, target_ty);
}
return expr;
}
static struct td_var_ty resolve_usual_arithmetic_conversions(
struct typechk *tchk, const struct td_var_ty *lhs_ty,
const struct td_var_ty *rhs_ty, struct text_span context) {
// it is expected integer promotion has already been performed
if (lhs_ty->ty == TD_VAR_TY_TY_UNKNOWN ||
rhs_ty->ty == TD_VAR_TY_TY_UNKNOWN) {
return TD_VAR_TY_UNKNOWN;
}
if (lhs_ty->ty == TD_VAR_TY_TY_INCOMPLETE_AGGREGATE ||
rhs_ty->ty == TD_VAR_TY_TY_INCOMPLETE_AGGREGATE) {
return TD_VAR_TY_UNKNOWN;
}
if (lhs_ty->ty == TD_VAR_TY_TY_ARRAY) {
struct td_var_ty lhs_ptr = td_var_ty_make_pointer(
tchk, lhs_ty->array.underlying, TD_TYPE_QUALIFIER_FLAG_NONE);
return resolve_usual_arithmetic_conversions(tchk, &lhs_ptr, rhs_ty,
context);
}
if (lhs_ty->ty == TD_VAR_TY_TY_FUNC) {
struct td_var_ty lhs_ptr =
td_var_ty_make_pointer(tchk, lhs_ty, TD_TYPE_QUALIFIER_FLAG_NONE);
return resolve_usual_arithmetic_conversions(tchk, &lhs_ptr, rhs_ty,
context);
}
if (rhs_ty->ty == TD_VAR_TY_TY_ARRAY) {
struct td_var_ty rhs_ptr = td_var_ty_make_pointer(
tchk, rhs_ty->array.underlying, TD_TYPE_QUALIFIER_FLAG_NONE);
return resolve_usual_arithmetic_conversions(tchk, lhs_ty, &rhs_ptr,
context);
}
if (rhs_ty->ty == TD_VAR_TY_TY_FUNC) {
struct td_var_ty rhs_ptr =
td_var_ty_make_pointer(tchk, rhs_ty, TD_TYPE_QUALIFIER_FLAG_NONE);
return resolve_usual_arithmetic_conversions(tchk, lhs_ty, &rhs_ptr,
context);
}
if (lhs_ty->ty == TD_VAR_TY_TY_POINTER ||
rhs_ty->ty == TD_VAR_TY_TY_POINTER) {
if (lhs_ty->ty == TD_VAR_TY_TY_WELL_KNOWN) {
return *rhs_ty;
} else if (rhs_ty->ty == TD_VAR_TY_TY_WELL_KNOWN) {
return *lhs_ty;
}
if ((lhs_ty->ty == TD_VAR_TY_TY_POINTER &&
lhs_ty->pointer.underlying->ty == TD_VAR_TY_TY_VOID) ||
td_var_ty_is_integral_ty(lhs_ty)) {
return *rhs_ty;
} else if ((rhs_ty->ty == TD_VAR_TY_TY_POINTER &&
rhs_ty->pointer.underlying->ty == TD_VAR_TY_TY_VOID) ||
td_var_ty_is_integral_ty(rhs_ty)) {
return *lhs_ty;
} else if (lhs_ty->ty == TD_VAR_TY_TY_POINTER &&
rhs_ty->ty == TD_VAR_TY_TY_POINTER &&
td_var_ty_compatible(
tchk, lhs_ty->pointer.underlying, rhs_ty->pointer.underlying,
TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_LHS |
TD_VAR_TY_COMPATIBLE_FLAG_LVALUE_CONVERT_RHS)) {
return *lhs_ty;
} else {
// compiler_diagnostics_add(
// tchk->diagnostics,
// MK_SEMANTIC_DIAGNOSTIC(POINTER_TYPE_MISMATCH,
// pointer_type_mismatch,
// context, MK_INVALID_TEXT_POS(0),
// "pointer type mismatch"));
return *lhs_ty;
}
}
if (lhs_ty->ty != TD_VAR_TY_TY_WELL_KNOWN ||
rhs_ty->ty != TD_VAR_TY_TY_WELL_KNOWN) {
return *lhs_ty;
// TODO("`%s` for types other than well known", __func__);
}
DEBUG_ASSERT(lhs_ty->well_known >= WELL_KNOWN_TY_SIGNED_INT &&
rhs_ty->well_known >= WELL_KNOWN_TY_SIGNED_INT,
"integer promotion should have occurred");
struct td_var_ty result_ty = {.ty = TD_VAR_TY_TY_WELL_KNOWN};
if (lhs_ty->well_known == rhs_ty->well_known) {
// they are the same type
result_ty.well_known = lhs_ty->well_known;
} else {
enum well_known_ty signed_lhs = WKT_MAKE_SIGNED(lhs_ty->well_known);
enum well_known_ty signed_rhs = WKT_MAKE_SIGNED(rhs_ty->well_known);
/* Otherwise, if both operands have signed integer types or both have
** unsigned integer types, the operand with the type of lesser integer
** conversion rank is converted to the type of the operand with greater
** rank.
*/
if (WKT_IS_SIGNED(lhs_ty->well_known) ==
WKT_IS_SIGNED(rhs_ty->well_known)) {
result_ty.well_known = MAX(lhs_ty->well_known, rhs_ty->well_known);
/* Otherwise, if the operand that has unsigned integer type has rank
** greater or equal to the rank of the type of the other operand, then the
** operand with signed integer type is converted to the type of the
** operand with unsigned integer type.
*/
} else if (!WKT_IS_SIGNED(lhs_ty->well_known) && signed_lhs >= signed_rhs) {
result_ty.well_known = lhs_ty->well_known;
} else if (!WKT_IS_SIGNED(rhs_ty->well_known) && signed_rhs >= signed_lhs) {
result_ty.well_known = rhs_ty->well_known;
/* Otherwise, if the type of the operand with signed integer type can
** represent all of the values of the type of the operand with unsigned
** integer type, then the operand with unsigned integer type is converted
** to the type of the operand with signed integer type.
*/
} else if (WKT_IS_SIGNED(lhs_ty->well_known) &&
(canonicalize_wkt(tchk, lhs_ty->well_known) >
canonicalize_wkt(tchk, rhs_ty->well_known))) {
// one is bigger than other
// type of expression is simply the larger type
// FIXME: is this correct
result_ty.well_known =
signed_lhs > signed_rhs ? lhs_ty->well_known : rhs_ty->well_known;
}
/* Otherwise, both operands are converted to the unsigned integer type
** corresponding to the type of the operand with signed integer type.
*/
else {
result_ty.well_known = WKT_IS_SIGNED(lhs_ty->well_known)
? WKT_MAKE_UNSIGNED(lhs_ty->well_known)
: WKT_MAKE_UNSIGNED(rhs_ty->well_known);
}
}
return result_ty;
}