-
Notifications
You must be signed in to change notification settings - Fork 0
/
typechk.c
3512 lines (2968 loc) · 102 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 "parse.h"
#include "var_table.h"
#include "vector.h"
#include <math.h>
#include <stdatomic.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_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(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(FLOAT);
MAKE_WKT(DOUBLE);
MAKE_WKT(LONG_DOUBLE);
#undef MAKE_WKT
struct typechk {
struct arena_allocator *arena;
struct parser *parser;
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;
};
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_QUOT:
return false;
}
}
bool 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_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:
return false;
}
}
static 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;
}
// FIXME: consider qualifiers, and "compatible" types rather than just equal
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;
}
goto pointer;
pointer:
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_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 !strcmp(l_agg.name, r_agg.name);
}
case TD_VAR_TY_TY_INCOMPLETE_AGGREGATE:
bug("can't check incomplete");
}
}
static bool is_cnst_ty_integral(enum td_cnst_ty ty) {
switch (ty) {
case TD_CNST_TY_CHAR:
case TD_CNST_TY_WIDE_CHAR:
case TD_CNST_TY_SIGNED_INT:
case TD_CNST_TY_UNSIGNED_INT:
case TD_CNST_TY_SIGNED_LONG:
case TD_CNST_TY_UNSIGNED_LONG:
case TD_CNST_TY_SIGNED_LONG_LONG:
case TD_CNST_TY_UNSIGNED_LONG_LONG:
return true;
case TD_CNST_TY_FLOAT:
case TD_CNST_TY_DOUBLE:
case TD_CNST_TY_LONG_DOUBLE:
case TD_CNST_TY_STR_LITERAL:
case TD_CNST_TY_WIDE_STR_LITERAL:
return false;
}
}
static bool is_cnst_ty_fp(enum td_cnst_ty ty) {
switch (ty) {
case TD_CNST_TY_FLOAT:
case TD_CNST_TY_DOUBLE:
case TD_CNST_TY_LONG_DOUBLE:
return true;
case TD_CNST_TY_CHAR:
case TD_CNST_TY_WIDE_CHAR:
case TD_CNST_TY_SIGNED_INT:
case TD_CNST_TY_UNSIGNED_INT:
case TD_CNST_TY_SIGNED_LONG:
case TD_CNST_TY_UNSIGNED_LONG:
case TD_CNST_TY_SIGNED_LONG_LONG:
case TD_CNST_TY_UNSIGNED_LONG_LONG:
case TD_CNST_TY_STR_LITERAL:
case TD_CNST_TY_WIDE_STR_LITERAL:
return false;
}
}
bool 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_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:
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;
}
}
struct td_var_ty td_var_ty_pointer_sized_int(UNUSED_ARG(struct typechk *tchk),
bool is_signed) {
// returns the type for `size_t` effectively
// TODO: generalise - either we should have a special ptr-sized int type, or
// tchk should have a field for ptr size
return (struct td_var_ty){.ty = TD_VAR_TY_TY_WELL_KNOWN,
.well_known =
is_signed ? WELL_KNOWN_TY_SIGNED_LONG_LONG
: WELL_KNOWN_TY_UNSIGNED_LONG_LONG};
}
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 = arena_alloc(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_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 %d)", 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 = arena_alloc(tchk->arena, sizeof(*td_expr.unary_op.expr)),
.cast = (struct td_cast){.var_ty = target_ty}}};
*td_expr.unary_op.expr = expr;
return td_expr;
}
static struct td_expr add_cast_if_needed(struct typechk *tchk,
struct td_expr expr,
struct td_var_ty target_ty) {
if (!td_var_ty_eq(tchk, &expr.var_ty, &target_ty)) {
return add_cast_expr(tchk, expr, 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;
}
// TODO:
#define WARN(s) bug(s)
UNUSED static struct td_var_ty
resolve_ternary_ty(UNUSED_ARG(struct typechk *tchk),
const struct td_ternary *ternary) {
// FIXME: do logic
return ternary->false_expr->var_ty;
}
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) {
// it is expected integer promotion has already been performed
debug_assert(lhs_ty->ty != TD_VAR_TY_TY_UNKNOWN &&
rhs_ty->ty != TD_VAR_TY_TY_UNKNOWN,
"unknown ty in call to `%s`", __func__);
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_POINTER && lhs_ty->pointer.underlying->ty == TD_VAR_TY_TY_VOID)
|| 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) || is_integral_ty(rhs_ty)) {
return *lhs_ty;
} else if (td_var_ty_eq(tchk, lhs_ty, rhs_ty)) {
return *lhs_ty;
} else {
WARN("incompatible pointer types");
}
}
if (lhs_ty->ty != TD_VAR_TY_TY_WELL_KNOWN ||
rhs_ty->ty != TD_VAR_TY_TY_WELL_KNOWN) {
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;
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);
if (signed_lhs != signed_rhs) {
// one is bigger than other
// type of expression is simply the larger type
result_ty.well_known = MAX(signed_lhs, signed_rhs);
} else {
// they are the same size
// the unsigned one is chosen (C spec dictates)
result_ty.well_known = WKT_MAKE_UNSIGNED(signed_lhs);
}
}
return result_ty;
}
struct td_binary_op_tys {
struct td_var_ty lhs_op_ty;
struct td_var_ty rhs_op_ty;
struct td_var_ty result_ty;
};
static struct td_binary_op_tys
resolve_binary_op_types(struct typechk *tchk,
const struct td_binary_op *binary_op) {
// it is expected integer promotion has already been performed
enum td_binary_op_ty ty = binary_op->ty;
const struct td_expr *lhs_expr = binary_op->lhs;
const struct td_expr *rhs_expr = binary_op->rhs;
const struct td_var_ty *lhs = &lhs_expr->var_ty;
const struct td_var_ty *rhs = &rhs_expr->var_ty;
struct td_var_ty lhs_op_ty, rhs_op_ty, result_ty;
if (lhs->ty == TD_VAR_TY_TY_POINTER && rhs->ty == TD_VAR_TY_TY_POINTER) {
if (ty == TD_BINARY_OP_TY_SUB) {
if (!td_var_ty_eq(tchk, lhs, rhs)) {
WARN("subtraction on pointers of different kinds is forbidden");
}
lhs_op_ty = rhs_op_ty = *lhs;
result_ty = td_var_ty_pointer_sized_int(tchk, true);
} else if (td_binary_op_is_comparison(ty)) {
lhs_op_ty = rhs_op_ty = *lhs;
result_ty = TD_VAR_TY_WELL_KNOWN_SIGNED_INT;
} else {
WARN("binary operations where both types are pointer only makes sense "
"for subtraction or comparisons");
}
} else if (lhs->ty == TD_VAR_TY_TY_POINTER) {
lhs_op_ty = *lhs;
rhs_op_ty = *lhs;
result_ty = *lhs;
} else if (rhs->ty == TD_VAR_TY_TY_POINTER) {
lhs_op_ty = *rhs;
rhs_op_ty = *rhs;
result_ty = *rhs;
} else if (ty == TD_BINARY_OP_TY_LSHIFT || ty == TD_BINARY_OP_TY_RSHIFT) {
lhs_op_ty = *lhs;
rhs_op_ty = *rhs;
result_ty = *lhs;
} else {
lhs_op_ty = rhs_op_ty =
resolve_usual_arithmetic_conversions(tchk, lhs, rhs);
if (td_binary_op_is_comparison(ty)) {
result_ty = TD_VAR_TY_WELL_KNOWN_SIGNED_INT;
} else {
result_ty = lhs_op_ty;
}
}
return (struct td_binary_op_tys){
.lhs_op_ty = lhs_op_ty, .rhs_op_ty = rhs_op_ty, .result_ty = result_ty};
}
struct assg_ty_map {
enum lex_token_ty token_ty;
enum ast_assg_ty assg_ty;
enum ast_binary_op_ty binary_op_ty;
};
struct td_specifiers {
enum td_storage_class_specifier storage;
enum td_function_specifier function;
enum td_type_qualifier_flags qualifier_flags;
struct td_var_ty type_specifier;
};
enum td_specifier_allow {
TD_SPECIFIER_ALLOW_TYPE_QUALIFIERS = 1,
TD_SPECIFIER_ALLOW_STORAGE_CLASS_SPECIFIERS = 2,
TD_SPECIFIER_ALLOW_FUNCTION_SPECIFIERS = 4,
TD_SPECIFIER_ALLOW_TYPE_SPECIFIERS = 4,
};
static unsigned long long
type_constant_integral_expr(UNUSED struct typechk *tchk,
const struct ast_expr *expr);
// static unsigned long long type_constant_expr(UNUSED struct typechk *tchk,
// const struct ast_expr *expr);
static struct td_declaration
type_declaration(struct typechk *tchk,
const struct ast_declaration *declaration);
static struct td_declaration
type_struct_declaration(struct typechk *tchk,
const struct ast_declaration *declaration);
static struct td_specifiers
type_specifiers(struct typechk *tchk,
const struct ast_declaration_specifier_list *list,
enum td_specifier_allow allow);
enum sign_state { SIGN_STATE_NONE, SIGN_STATE_SIGNED, SIGN_STATE_UNSIGNED };
static char *anonymous_name(struct typechk *tchk) {
size_t id = tchk->next_anonymous_type_name_id++;
size_t char_size = num_digits(id);
size_t len_prefix = strlen("<anonymous>");
size_t len = len_prefix + char_size + 1;
char *buff = arena_alloc(tchk->arena, sizeof(*buff) * len);
snprintf(buff, len, "<anonymous>%zu", id);
buff[len] = '\0';
return buff;
}
static struct td_var_ty
td_var_ty_for_enum(struct typechk *tchk,
const struct ast_enum_specifier *specifier) {
if (!specifier->enumerator_list) {
if (!specifier->identifier) {
WARN("enum without identifier or decl list");
}
} else {
unsigned long last_value;
for (size_t i = 0; i < specifier->enumerator_list->num_enumerators; i++) {
struct ast_enumerator *enumerator =
&specifier->enumerator_list->enumerators[i];
unsigned long long value;
if (enumerator->value) {
value = type_constant_integral_expr(tchk, enumerator->value);
} else {
value = i ? last_value + 1 : 0;
}
const char *enum_name =
identifier_str(tchk->parser, &enumerator->identifier);
struct td_var var = {.ty = TD_VAR_VAR_TY_ENUMERATOR,
.identifier = enum_name,
.scope = cur_scope(&tchk->var_table),
.enumerator = value};
struct var_table_entry *entry =
var_table_create_entry(&tchk->var_table, enum_name);
entry->var = arena_alloc(tchk->arena, sizeof(*entry->var));
*entry->var = var;
entry->var_ty = arena_alloc(tchk->arena, sizeof(*entry->var_ty));
*entry->var_ty = TD_VAR_TY_WELL_KNOWN_SIGNED_INT;
last_value = value;
}
}
return TD_VAR_TY_WELL_KNOWN_SIGNED_INT;
}
static struct td_var_ty td_var_ty_for_struct_or_union(
struct typechk *tchk,
const struct ast_struct_or_union_specifier *specifier) {
struct td_var_ty var_ty = {
.ty = TD_VAR_TY_TY_AGGREGATE,
};
switch (specifier->ty) {
case AST_STRUCT_OR_UNION_SPECIFIER_TY_STRUCT:
var_ty.aggregate.ty = TD_TY_AGGREGATE_TY_STRUCT;
break;
case AST_STRUCT_OR_UNION_SPECIFIER_TY_UNION:
var_ty.aggregate.ty = TD_TY_AGGREGATE_TY_UNION;
break;
}
const char *name;
if (specifier->identifier) {
name = identifier_str(tchk->parser, specifier->identifier);
} else {
name = anonymous_name(tchk);
}
if (!specifier->decl_list) {
if (!specifier->identifier) {
WARN("struct/union without identifier or decl list");
}
struct var_table_entry *entry = var_table_get_entry(&tchk->ty_table, name);
// FIXME: check scope too
// if (entry && entry->scope == scope of this decl)
if (entry) {
return *entry->var_ty;
} else {
return (struct td_var_ty){
.ty = TD_VAR_TY_TY_INCOMPLETE_AGGREGATE,
.incomplete_aggregate = {.ty = var_ty.aggregate.ty, .name = name}};
}
}
var_ty.aggregate.name = name;
struct vector *var_decls = vector_create(sizeof(struct td_var_declaration));
for (size_t i = 0; i < specifier->decl_list->num_declarations; i++) {
const struct ast_declaration *declaration =
&specifier->decl_list->declarations[i];
struct td_declaration td_decl = type_struct_declaration(tchk, declaration);
vector_extend(var_decls, td_decl.var_declarations,
td_decl.num_var_declarations);
}
size_t num_var_decls = vector_length(var_decls);
var_ty.aggregate.num_fields = num_var_decls;
var_ty.aggregate.fields = arena_alloc(
tchk->arena, sizeof(*var_ty.aggregate.fields) * num_var_decls);
for (size_t i = 0; i < num_var_decls; i++) {
struct td_var_declaration *var_decl = vector_get(var_decls, i);
if (var_decl->init) {
WARN("initializer not supported in struct");
}
var_ty.aggregate.fields[i] = (struct td_struct_field){
.identifier = var_decl->var.identifier, .var_ty = var_decl->var_ty};
}
struct td_var var = {.ty = TD_VAR_VAR_TY_VAR,
.identifier = name,
.scope = cur_scope(&tchk->ty_table)};
struct var_table_entry *entry = var_table_create_entry(&tchk->ty_table, name);
entry->var = arena_alloc(tchk->arena, sizeof(*entry->var));
*entry->var = var;
entry->var_ty = arena_alloc(tchk->arena, sizeof(*entry->var_ty));
*entry->var_ty = var_ty;
return var_ty;
}
static struct td_var_declaration
type_declarator(struct typechk *tchk, const struct td_specifiers *specifiers,
const struct ast_declarator *declarator,
const struct ast_init *init);
static struct td_var_ty type_abstract_declarator(
struct typechk *tchk, const struct td_specifiers *specifiers,
const struct ast_abstract_declarator *abstract_declarator);
static struct td_var_ty
type_array_declarator(struct typechk *tchk, struct td_var_ty var_ty,
const struct ast_array_declarator *array_declarator,
const struct ast_init *init) {
struct td_var_ty array_ty = {
.ty = TD_VAR_TY_TY_ARRAY,
};
switch (array_declarator->ty) {
case AST_ARRAY_DECLARATOR_TY_STAR:
todo("star arrays");
case AST_ARRAY_DECLARATOR_TY_STATIC_SIZED:
case AST_ARRAY_DECLARATOR_TY_SIZED:
array_ty.array.size =
type_constant_integral_expr(tchk, array_declarator->size);
break;
case AST_ARRAY_DECLARATOR_TY_UNSIZED:
switch (init->ty) {
case AST_INIT_TY_EXPR:
WARN("cannot initialize unsized array with expression");
case AST_INIT_TY_INIT_LIST: {
const struct ast_init_list *init_list = &init->init_list;
size_t max_size = 0;
size_t size = 0;
for (size_t i = 0; i < init_list->num_inits; i++) {
const struct ast_init_list_init *init_list_init = &init_list->inits[i];
if (init_list_init->designator_list &&
init_list_init->designator_list->num_designators) {
const struct ast_designator *designator =
&init_list_init->designator_list->designators[0];
switch (designator->ty) {
case AST_DESIGNATOR_TY_FIELD:
WARN("field designator in array init");
case AST_DESIGNATOR_TY_INDEX:
size = type_constant_integral_expr(tchk, designator->index) + 1;
break;
}
} else {
size++;
}
max_size = MAX(max_size, size);
}
array_ty.array.size = max_size;
break;
}
}
break;
}
array_ty.array.underlying =
arena_alloc(tchk->arena, sizeof(*array_ty.array.underlying));
*array_ty.array.underlying = var_ty;
return array_ty;
}
static struct td_var_ty
type_func_declarator(struct typechk *tchk, struct td_var_ty var_ty,
struct ast_func_declarator *func_declarator) {
struct td_var_ty func_ty = {
.ty = TD_VAR_TY_TY_FUNC,
};
func_ty.func.ret = arena_alloc(tchk->arena, sizeof(*func_ty.func.ret));
*func_ty.func.ret = var_ty;
struct ast_paramlist *param_list = func_declarator->param_list;
size_t num_params;
if (param_list->num_params &&
param_list->params[param_list->num_params - 1].ty ==
AST_PARAM_TY_VARIADIC) {
num_params = param_list->num_params - 1;
func_ty.func.ty = TD_TY_FUNC_TY_VARIADIC;
} else if (param_list->num_params &&
param_list->params[0].ty == AST_PARAM_TY_VOID) {
num_params = 0;
func_ty.func.ty = TD_TY_FUNC_TY_KNOWN_ARGS;
} else {
num_params = param_list->num_params;
func_ty.func.ty = param_list->num_params ? TD_TY_FUNC_TY_KNOWN_ARGS
: TD_TY_FUNC_TY_UNKNOWN_ARGS;
}
func_ty.func.num_params = num_params;
func_ty.func.params =
arena_alloc(tchk->arena, sizeof(*func_ty.func.params) * num_params);
for (size_t j = 0; j < num_params; j++) {
struct ast_param *param = ¶m_list->params[j];
struct td_ty_param *td_param = &func_ty.func.params[j];
struct td_specifiers param_specifiers =
type_specifiers(tchk, ¶m->specifier_list,
TD_SPECIFIER_ALLOW_TYPE_QUALIFIERS |
TD_SPECIFIER_ALLOW_TYPE_SPECIFIERS);
switch (param->ty) {
case AST_PARAM_TY_VOID:
case AST_PARAM_TY_VARIADIC:
continue;
case AST_PARAM_TY_DECL: {
struct td_var_declaration param_decl =
type_declarator(tchk, ¶m_specifiers, ¶m->declarator, NULL);
*td_param = (struct td_ty_param){.identifier = param_decl.var.identifier,
.var_ty = param_decl.var_ty};
break;
}
case AST_PARAM_TY_ABSTRACT_DECL: {
struct td_var_ty param_var_ty = type_abstract_declarator(
tchk, ¶m_specifiers, ¶m->abstract_declarator);
*td_param =
(struct td_ty_param){.identifier = NULL, .var_ty = param_var_ty};
break;
}
}
}
return func_ty;
}
static struct td_var_ty type_abstract_declarator_inner(
struct typechk *tchk, const struct td_var_ty *outer_var_ty,
const struct ast_abstract_declarator *abstract_declarator) {
struct ast_pointer_list pointer_list = abstract_declarator->pointer_list;
struct ast_direct_abstract_declarator_list decl_list =
abstract_declarator->direct_abstract_declarator_list;
struct td_var_ty var_ty = *outer_var_ty;
for (size_t i = 0; i < pointer_list.num_pointers; i++) {
struct ast_pointer *pointer = &pointer_list.pointers[i];
struct td_specifiers ptr_specifiers = type_specifiers(
tchk, &pointer->specifier_list, TD_SPECIFIER_ALLOW_TYPE_QUALIFIERS);
var_ty =
td_var_ty_make_pointer(tchk, &var_ty, ptr_specifiers.qualifier_flags);
}
ssize_t inner_idx = -1;
for (size_t i = decl_list.num_direct_abstract_declarators; i; i--) {
struct ast_direct_abstract_declarator *direct_declarator =
&decl_list.direct_abstract_declarators[i - 1];
switch (direct_declarator->ty) {
case AST_DIRECT_ABSTRACT_DECLARATOR_TY_PAREN_DECLARATOR:
if (inner_idx != -1) {
WARN("declarator has multiple sub-declarators");
}
inner_idx = i - 1;
break;
case AST_DIRECT_ABSTRACT_DECLARATOR_TY_ARRAY_DECLARATOR: {
var_ty = type_array_declarator(tchk, var_ty,
direct_declarator->array_declarator, NULL);
break;
}
case AST_DIRECT_ABSTRACT_DECLARATOR_TY_FUNC_DECLARATOR: {
var_ty = type_func_declarator(tchk, var_ty,
direct_declarator->func_declarator);
break;
}
}
}
if (inner_idx != -1) {
struct ast_direct_abstract_declarator *direct_declarator =
&decl_list.direct_abstract_declarators[inner_idx];
return type_abstract_declarator_inner(tchk, &var_ty,
direct_declarator->paren_declarator);
} else {
return var_ty;
}
}
static struct td_var_ty type_abstract_declarator(
struct typechk *tchk, const struct td_specifiers *specifiers,
const struct ast_abstract_declarator *abstract_declarator) {
// TODO: handle storage class/qualifier/function specifiers
return type_abstract_declarator_inner(tchk, &specifiers->type_specifier,
abstract_declarator);
}
static struct td_var_ty td_var_ty_for_typedef(struct typechk *tchk,
const struct lex_token *identifier) {
struct var_table_entry *entry = var_table_get_entry(
&tchk->ty_table, identifier_str(tchk->parser, identifier));
if (!entry) {
WARN("typedef not recognised");
}
return *entry->var_ty;
}
struct td_declarator {
struct td_var_ty var_ty;
const char *identifier;
};
static struct td_var_declaration type_declarator_inner(
struct typechk *tchk, const struct td_var_ty *outer_var_ty,
const struct ast_declarator *declarator, const struct ast_init *init) {
struct td_var_declaration var_decl;
struct td_var_ty var_ty = *outer_var_ty;
struct ast_pointer_list pointer_list = declarator->pointer_list;
struct ast_direct_declarator_list decl_list =
declarator->direct_declarator_list;
for (size_t i = 0; i < pointer_list.num_pointers; i++) {
struct ast_pointer *pointer = &pointer_list.pointers[i];
struct td_specifiers ptr_specifiers = type_specifiers(
tchk, &pointer->specifier_list, TD_SPECIFIER_ALLOW_TYPE_QUALIFIERS);
var_ty =
td_var_ty_make_pointer(tchk, &var_ty, ptr_specifiers.qualifier_flags);
}
bool found_ident = false;
ssize_t inner_idx = -1;
for (size_t i = decl_list.num_direct_declarators; i; i--) {
struct ast_direct_declarator *direct_declarator =
&decl_list.direct_declarators[i - 1];
switch (direct_declarator->ty) {
case AST_DIRECT_DECLARATOR_TY_IDENTIFIER:
var_decl.var = (struct td_var){
// FIXME: other fields
.ty = TD_VAR_VAR_TY_VAR,
.scope = cur_scope(&tchk->var_table),
.identifier =
identifier_str(tchk->parser, &direct_declarator->identifier),
};
found_ident = true;
break;
case AST_DIRECT_DECLARATOR_TY_PAREN_DECLARATOR: {
if (inner_idx != -1) {
WARN("declarator has multiple sub-declarators");
}
inner_idx = i - 1;
found_ident = true;
break;
}
case AST_DIRECT_DECLARATOR_TY_ARRAY_DECLARATOR: {
var_ty = type_array_declarator(tchk, var_ty,
direct_declarator->array_declarator, init);
break;
}
case AST_DIRECT_DECLARATOR_TY_FUNC_DECLARATOR: {
var_ty = type_func_declarator(tchk, var_ty,
direct_declarator->func_declarator);
break;
}
}
}
debug_assert(found_ident, "decl without identifier?");
if (inner_idx != -1) {
struct ast_direct_declarator *direct_declarator =
&decl_list.direct_declarators[inner_idx];
return type_declarator_inner(tchk, &var_ty,
direct_declarator->paren_declarator, init);
} else {
var_decl.init = NULL;
var_decl.var_ty = var_ty;
return var_decl;
}
}
static struct td_var_declaration
type_declarator(struct typechk *tchk, const struct td_specifiers *specifiers,
const struct ast_declarator *declarator,
const struct ast_init *init) {
// TODO: handle storage class/qualifier/function specifiers
return type_declarator_inner(tchk, &specifiers->type_specifier, declarator,
init);
}
static struct td_specifiers
type_specifiers(struct typechk *tchk,
const struct ast_declaration_specifier_list *list,
enum td_specifier_allow allow) {
struct td_specifiers specifiers = {.storage = TD_STORAGE_CLASS_SPECIFIER_NONE,
.function = TD_FUNCTION_SPECIFIER_NONE,
.qualifier_flags =
TD_TYPE_QUALIFIER_FLAG_NONE,
.type_specifier = TD_VAR_TY_UNKNOWN};
int long_count = 0, int_count = 0, signed_count = 0, unsigned_count = 0;
int type_specifier_count = 0;
struct ast_type_specifier last_specifier;
for (size_t i = 0; i < list->num_decl_specifiers; i++) {
struct ast_declaration_specifier specifier = list->decl_specifiers[i];
switch (specifier.ty) {
case AST_DECL_SPECIFIER_TY_STORAGE_CLASS_SPECIFIER:
if (!(allow & TD_SPECIFIER_ALLOW_STORAGE_CLASS_SPECIFIERS)) {
WARN("storage class specifier not valid");
}
if (specifiers.storage != TD_STORAGE_CLASS_SPECIFIER_NONE) {
WARN("multiple storage specifiers");
}