-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypecheck_expression.c
More file actions
1335 lines (1151 loc) · 38.6 KB
/
typecheck_expression.c
File metadata and controls
1335 lines (1151 loc) · 38.6 KB
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 "analyzer.h"
#include "std.h"
#include "std_io.h"
static const struct EnumeratorAndValue *
get_global_enumerator(const struct Vector /*<EnumeratorAndValue>*/ *ref_list,
const char *name);
static int is_local_var(const struct ScopeChain *t, const char *str);
int typecheck_constant_integral_expression(struct AnalyzerState *ptr_ps,
const struct UntypedExpr *ref_uexpr,
const char *context)
{
const struct UntypedExpr uexpr = *ref_uexpr;
switch (uexpr.category) {
case INT_LITERAL_:
return uexpr.int_value;
case VAR: {
const char *name = uexpr.var_name;
if (is_local_var(&ptr_ps->scope_chain, name)) {
break;
}
const struct EnumeratorAndValue *ptr_enum_and_value =
get_global_enumerator(&ptr_ps->global_enumerator_list, name);
if (!ptr_enum_and_value) {
break;
}
return ptr_enum_and_value->value;
}
default:
break;
}
fprintf(stderr, "Expected const expression, but did not get one.\n");
fprintf(stderr, "context: %s\n", context);
exit(EXIT_FAILURE);
}
static int is_compatible(const struct AnalyzerState *ptr_ps,
const struct Type *ref_t1, const struct Type *ref_t2);
void expect_type(const struct AnalyzerState *ptr_ps,
const struct Type *ref_actual_type,
const struct Type *ref_expected_type, const char *message)
{
if (!is_compatible(ptr_ps, ref_actual_type, ref_expected_type)) {
fprintf(stderr, "Unmatched type: expected `");
debug_print_type(ref_expected_type);
fprintf(stderr, "`, but got `");
debug_print_type(ref_actual_type);
fprintf(stderr, "`.\n");
fprintf(stderr, "context: %s\n", message);
exit(EXIT_FAILURE);
}
}
static int is_strictly_equal(const struct AnalyzerState *ptr_ps,
const struct Type *ref_t1,
const struct Type *ref_t2)
{
const struct Type t1 = *ref_t1;
const struct Type t2 = *ref_t2;
if (t1.type_category == INT_ && t2.type_category == INT_) {
return 1;
}
if (t1.type_category == CHAR_ && t2.type_category == CHAR_) {
return 1;
}
if (t1.type_category == VOID_ && t2.type_category == VOID_) {
return 1;
}
if (t1.type_category == PTR_ && t2.type_category == PTR_) {
/* pointer to incomplete types */
if (t1.derived_from->type_category == STRUCT_NOT_UNION &&
t2.derived_from->type_category == STRUCT_NOT_UNION &&
strcmp(t1.derived_from->s.struct_or_union_tag,
t2.derived_from->s.struct_or_union_tag) == 0) {
return 1;
}
/* pointer to incomplete types */
if (t1.derived_from->type_category == UNION &&
t2.derived_from->type_category == UNION &&
strcmp(t1.derived_from->s.struct_or_union_tag,
t2.derived_from->s.struct_or_union_tag) == 0) {
return 1;
}
return is_strictly_equal(ptr_ps, t1.derived_from, t2.derived_from);
}
if (t1.type_category == ARRAY && t2.type_category == ARRAY) {
return is_strictly_equal(ptr_ps, t1.derived_from, t2.derived_from) &&
(t1.array_length == t2.array_length);
}
if (t1.type_category == STRUCT_NOT_UNION &&
t2.type_category == STRUCT_NOT_UNION) {
if ((0)) { /* both are local */
unsupported("struct type declared locally");
}
return strcmp(t1.s.struct_or_union_tag, t2.s.struct_or_union_tag) ==
0 &&
lookup(ptr_ps->global_struct_or_union_tag_map,
t1.s.struct_or_union_tag);
}
if (t1.type_category == UNION && t2.type_category == UNION) {
if ((0)) { /* both are local */
unsupported("union type declared locally");
}
return strcmp(t1.s.struct_or_union_tag, t2.s.struct_or_union_tag) ==
0 &&
lookup(ptr_ps->global_struct_or_union_tag_map,
t1.s.struct_or_union_tag);
}
if (t1.type_category == ENUM_ && t2.type_category == ENUM_) {
if ((0)) { /* both are local */
unsupported("enum type declared locally");
}
return strcmp(t1.e.enum_tag, t2.e.enum_tag) == 0;
}
if (t1.type_category == FN && t2.type_category == FN) {
if (!is_strictly_equal(ptr_ps, t1.derived_from, t2.derived_from)) {
return 0;
}
if (t1.param_infos_validity == VALID &&
t2.param_infos_validity == VALID) {
if (t1.param_infos.length != t2.param_infos.length) {
return 0;
}
for (int i = 0; i < t1.param_infos.length; i++) {
if (!is_strictly_equal(ptr_ps, t1.param_infos.vector[i],
t2.param_infos.vector[i])) {
return 0;
}
}
return 1;
} else if (t1.param_infos_validity == INVALID ||
t2.param_infos_validity ==
INVALID) { /* when invalid, it matches no matter what */
return 1;
} else { /* one is VA_ARGS, the other is valid */
#ifdef __STDC__
#warning should typecheck
#endif
return 1;
}
}
return 0;
}
static int is_integral(const struct Type *ref_t1)
{
return ref_t1->type_category == INT_ || ref_t1->type_category == CHAR_ ||
ref_t1->type_category == ENUM_;
}
static int is_scalar(const struct Type *ref_t1)
{
return ref_t1->type_category == PTR_ || is_integral(ref_t1);
}
void expect_scalar(const struct Type *ref_type, const char *context)
{
if (!is_scalar(ref_type)) {
fprintf(stderr, "Expected a scalar type, but got a non-scalar type `");
debug_print_type(ref_type);
fprintf(stderr, "`.\ncontext: %s\n", context);
exit(EXIT_FAILURE);
}
}
void expect_integral(const struct Type *ref_actual_type, const char *message)
{
if (!is_integral(ref_actual_type)) {
fprintf(stderr, "Unmatched type: expected an integral type, but got a "
"non-integral type `");
debug_print_type(ref_actual_type);
fprintf(stderr, "`.\n");
fprintf(stderr, "context: %s\n", message);
exit(EXIT_FAILURE);
}
}
static int is_compatible(const struct AnalyzerState *ptr_ps,
const struct Type *ref_t1, const struct Type *ref_t2)
{
if (is_strictly_equal(ptr_ps, ref_t1, ref_t2)) {
return 1;
}
if (is_integral(ref_t1) && is_integral(ref_t2)) {
return 1;
}
const struct Type t1 = *ref_t1;
const struct Type t2 = *ref_t2;
if (t1.type_category == PTR_ && t2.type_category == PTR_) {
return t1.derived_from->type_category == VOID_ ||
t2.derived_from->type_category == VOID_;
}
if (t1.type_category == PTR_ && t2.type_category == ARRAY) {
struct Type t3 = t2; /* copy of t2 */
t3.type_category = PTR_;
return is_compatible(ptr_ps, &t1, &t3);
}
if (t1.type_category == ARRAY && t2.type_category == PTR_) {
return is_compatible(ptr_ps, &t2, &t1);
}
if (t1.type_category == FN && t2.type_category == FN) {
if (!is_strictly_equal(ptr_ps, t1.derived_from, t2.derived_from)) {
return 0;
}
if (t1.param_infos_validity == INVALID ||
t2.param_infos_validity == INVALID) {
return 1;
}
if (t1.param_infos_validity == VA_ARGS ||
t2.param_infos_validity == VA_ARGS) {
#ifdef __STDC__
#warning should typecheck
#endif
return 1;
}
if (t1.param_infos.length != t2.param_infos.length) {
return 0;
}
for (int i = 0; i < t1.param_infos.length; i++) {
const struct TypeAndIdent *ptr_ti1 = t1.param_infos.vector[i];
const struct TypeAndIdent *ptr_ti2 = t2.param_infos.vector[i];
if (!is_strictly_equal(ptr_ps, &ptr_ti1->type, &ptr_ti2->type)) {
return 0;
}
}
return 1;
}
return 0;
}
static enum SimpleBinOp to_simplebinop(enum TokenKind token_kind)
{
switch (token_kind) {
case OP_PLUS:
return SIMPLE_BIN_OP_PLUS;
case OP_MINUS:
return SIMPLE_BIN_OP_MINUS;
case OP_ASTERISK:
return SIMPLE_BIN_OP_ASTERISK;
case OP_SLASH:
return SIMPLE_BIN_OP_SLASH;
case OP_PERCENT:
return SIMPLE_BIN_OP_PERCENT;
case OP_LT:
return SIMPLE_BIN_OP_LT;
case OP_LT_EQ:
return SIMPLE_BIN_OP_LT_EQ;
case OP_LSHIFT:
return SIMPLE_BIN_OP_LSHIFT;
case OP_GT:
return SIMPLE_BIN_OP_GT;
case OP_GT_EQ:
return SIMPLE_BIN_OP_GT_EQ;
case OP_RSHIFT:
return SIMPLE_BIN_OP_RSHIFT;
case OP_AND:
return SIMPLE_BIN_OP_AND;
case OP_OR:
return SIMPLE_BIN_OP_OR;
case OP_EQ_EQ:
return SIMPLE_BIN_OP_EQ_EQ;
case OP_NOT_EQ:
return SIMPLE_BIN_OP_NOT_EQ;
case OP_HAT:
return SIMPLE_BIN_OP_HAT;
default:
fprintf(stderr,
"****************************\n"
"* INTERNAL COMPILER ERROR @ %s\n"
"* Unexpected value of TokenKind: a binary operator is needed, "
"but `token_kind` is `%d`\n"
"****************************\n",
__func__, token_kind);
exit(EXIT_FAILURE);
}
}
static struct Expr binary_op(const struct Expr *ref_expr,
const struct Expr *ref_expr2,
enum ExprCategory cat, const struct Type *ref_type)
{
struct Expr *ptr_expr1 = calloc(1, sizeof(struct Expr));
struct Expr *ptr_expr2 = calloc(1, sizeof(struct Expr));
*ptr_expr1 = *ref_expr;
*ptr_expr2 = *ref_expr2;
struct Expr new_expr;
new_expr.details.type = *ref_type;
new_expr.details.true_type = *ref_type;
new_expr.category = cat;
new_expr.ptr1 = ptr_expr1;
new_expr.ptr2 = ptr_expr2;
new_expr.ptr3 = 0;
return new_expr;
}
static struct Expr simple_binary_op(const struct Expr *ref_expr,
const struct Expr *ref_expr2,
enum TokenKind kind,
const struct Type *ref_type)
{
struct Expr *ptr_expr1 = calloc(1, sizeof(struct Expr));
struct Expr *ptr_expr2 = calloc(1, sizeof(struct Expr));
*ptr_expr1 = *ref_expr;
*ptr_expr2 = *ref_expr2;
struct Expr new_expr;
new_expr.details.type = *ref_type;
new_expr.details.true_type = *ref_type;
new_expr.category = SIMPLE_BINARY_EXPR;
new_expr.simple_binary_operator = to_simplebinop(kind);
new_expr.ptr1 = ptr_expr1;
new_expr.ptr2 = ptr_expr2;
new_expr.ptr3 = 0;
return new_expr;
}
static struct Expr comma_op(const struct Expr *ref_expr,
const struct Expr *ref_expr2,
const struct Type *ref_type)
{
struct Expr *ptr_expr1 = calloc(1, sizeof(struct Expr));
struct Expr *ptr_expr2 = calloc(1, sizeof(struct Expr));
*ptr_expr1 = *ref_expr;
*ptr_expr2 = *ref_expr2;
struct Expr new_expr;
new_expr.details.type = *ref_type;
new_expr.details.true_type = *ref_type;
new_expr.category = COMMA_EXPR;
new_expr.ptr1 = ptr_expr1;
new_expr.ptr2 = ptr_expr2;
new_expr.ptr3 = 0;
return new_expr;
}
static struct Expr pointer_plusorminus_int(const struct AnalyzerState *ptr_ps,
const struct Expr *ref_expr,
const struct Expr *ref_expr2,
enum TokenKind kind)
{
struct Expr *ptr_expr1 = calloc(1, sizeof(struct Expr));
struct Expr *ptr_expr2 = calloc(1, sizeof(struct Expr));
*ptr_expr1 = *ref_expr;
*ptr_expr2 = *ref_expr2;
struct Expr new_expr;
new_expr.details = ref_expr->details;
// array must decay into a pointer once pointer arithmetic happens
if (new_expr.details.true_type.type_category == ARRAY) {
new_expr.details.true_type.type_category = PTR_;
}
new_expr.category = kind == OP_PLUS ? POINTER_PLUS_INT : POINTER_MINUS_INT;
new_expr.ptr1 = ptr_expr1;
new_expr.ptr2 = ptr_expr2;
new_expr.ptr3 = 0;
const struct Type deref = deref_type(&ref_expr->details.type);
new_expr.size_info_for_pointer_arith = size_of(ptr_ps, &deref);
return new_expr;
}
static int is_local_var(const struct ScopeChain *ref_t, const char *str)
{
if (isElem(ref_t->var_table, str)) {
return 1;
} else if (ref_t->outer == 0) {
/* most outer, but cannot be found */
return 0;
} else {
return is_local_var(ref_t->outer, str);
}
}
static struct LocalVarInfo resolve_name_locally(const struct ScopeChain *ref_t,
const char *str)
{
if (isElem(ref_t->var_table, str)) {
struct LocalVarInfo *ptr_varinfo = lookup(ref_t->var_table, str);
return *ptr_varinfo;
} else if (ref_t->outer == 0) {
/* most outer, but cannot be found */
fprintf(stderr, "%s is not declared locally\n", str);
exit(EXIT_FAILURE);
} else {
return resolve_name_locally(ref_t->outer, str);
}
}
int isAssign(enum TokenKind opkind)
{
return (opkind == OP_EQ || opkind == OP_PLUS_EQ || opkind == OP_MINUS_EQ ||
opkind == OP_ASTERISK_EQ || opkind == OP_SLASH_EQ ||
opkind == OP_PERCENT_EQ || opkind == OP_LSHIFT_EQ ||
opkind == OP_RSHIFT_EQ || opkind == OP_AND_EQ ||
opkind == OP_HAT_EQ || opkind == OP_OR_EQ);
}
static const struct EnumeratorAndValue *
get_global_enumerator(const struct Vector /*<EnumeratorAndValue>*/ *ref_list,
const char *name)
{
for (int i = 0; i < ref_list->length; i++) {
const struct EnumeratorAndValue *ptr_vec_i = ref_list->vector[i];
if (strcmp(ptr_vec_i->ident, name) == 0) {
return ptr_vec_i;
}
}
return 0;
}
static enum UnaryOp to_unaryop(enum TokenKind token_kind)
{
switch (token_kind) {
case OP_NOT:
return UNARY_OP_NOT;
case OP_TILDA:
return UNARY_OP_TILDA;
case OP_PLUS:
return UNARY_OP_PLUS;
case OP_MINUS:
return UNARY_OP_MINUS;
case OP_PLUS_PLUS:
return UNARY_OP_PLUS_PLUS;
case OP_MINUS_MINUS:
return UNARY_OP_MINUS_MINUS;
case OP_AND:
return UNARY_OP_AND;
case OP_ASTERISK:
return UNARY_OP_ASTERISK;
default:
fprintf(stderr,
"****************************\n"
"* INTERNAL COMPILER ERROR @ %s\n"
"* Unexpected value of TokenKind: a unary operator is needed, "
"but `token_kind` is `%d`\n"
"****************************\n",
__func__, token_kind);
exit(EXIT_FAILURE);
}
}
static struct Expr unary_op_(const struct Expr *ref_expr, enum TokenKind kind,
const struct Type *ref_type)
{
struct Expr *ptr_expr1 = calloc(1, sizeof(struct Expr));
*ptr_expr1 = *ref_expr;
struct Expr new_expr;
new_expr.details.type = *ref_type;
new_expr.category = UNARY_OP_EXPR;
new_expr.unary_operator = to_unaryop(kind);
new_expr.ptr1 = ptr_expr1;
new_expr.ptr2 = 0;
new_expr.ptr3 = 0;
return new_expr;
}
static enum SimpleBinOp op_before_assign(enum TokenKind token_kind)
{
switch (token_kind) {
case OP_PLUS_EQ:
return SIMPLE_BIN_OP_PLUS;
case OP_MINUS_EQ:
return SIMPLE_BIN_OP_MINUS;
case OP_ASTERISK_EQ:
return SIMPLE_BIN_OP_ASTERISK;
case OP_SLASH_EQ:
return SIMPLE_BIN_OP_SLASH;
case OP_PERCENT_EQ:
return SIMPLE_BIN_OP_PERCENT;
case OP_LSHIFT_EQ:
return SIMPLE_BIN_OP_LSHIFT;
case OP_RSHIFT_EQ:
return SIMPLE_BIN_OP_RSHIFT;
case OP_AND_EQ:
return SIMPLE_BIN_OP_AND;
case OP_HAT_EQ:
return SIMPLE_BIN_OP_HAT;
case OP_OR_EQ:
return SIMPLE_BIN_OP_OR;
default:
fprintf(stderr,
"****************************\n"
"* INTERNAL COMPILER ERROR @ %s\n"
"* Unexpected value of TokenKind: a compound assignment "
"operator is needed, but `token_kind` is `%d`\n"
"****************************\n",
__func__, token_kind);
exit(EXIT_FAILURE);
}
}
void cast_to_null_pointer_if_possible(struct Expr *ref_e,
const struct TypePair *ref_details)
{
if (ref_e->category == INT_VALUE && ref_e->int_value == 0) {
ref_e->category = NULLPTR;
ref_e->details = *ref_details;
}
}
void if_function_cast_to_pointer(struct Expr *ptr_expr)
{
if (ptr_expr->details.type.type_category == FN) {
struct Type type = ptr_expr->details.type;
const struct Type ptr_to_type_ = ptr_to_type(&type);
struct Expr e = unary_op_(ptr_expr, OP_AND, &ptr_to_type_);
*ptr_expr = e;
}
}
struct Expr typecheck_binary_expression(const struct AnalyzerState *ptr_ps,
const struct Expr *ref_expr,
const struct Expr *ref_expr2,
enum TokenKind op);
struct Expr typecheck_unary_expression(const struct AnalyzerState *ptr_ps,
const struct Expr *ref_expr,
enum TokenKind kind)
{
const struct Expr expr = *ref_expr;
switch (kind) {
case OP_NOT: {
expect_scalar(&expr.details.type, "operand of logical not");
struct Type t = INT_TYPE();
return unary_op_(&expr, kind, &t);
}
case OP_TILDA:
case OP_PLUS:
case OP_MINUS: {
expect_integral(&expr.details.type,
"operand of bitnot, unary plus or unary minus");
/* integral promotion */
struct Type t = INT_TYPE();
return unary_op_(&expr, kind, &t);
}
case OP_PLUS_PLUS:
case OP_MINUS_MINUS: {
struct Expr new_expr = unary_op_(&expr, kind, &expr.details.type);
if (expr.details.type.type_category == PTR_) {
const struct Type deref = deref_type(&expr.details.type);
new_expr.size_info_for_pointer_arith = size_of(ptr_ps, &deref);
}
return new_expr;
}
case OP_AND: {
struct Type type = expr.details.type;
if (type.type_category == PTR_ &&
expr.details.true_type.type_category == ARRAY) {
type = expr.details.true_type;
}
const struct Type ptr_to_type_ = ptr_to_type(&type);
return unary_op_(&expr, OP_AND, &ptr_to_type_);
}
case OP_ASTERISK: {
/* function type, automatically converted to a function pointer,
* gets dereferenced to become a function type, which is again
* automatically converted to a function pointer
*/
if (expr.details.type.type_category == FN) {
struct Expr e = expr;
if_function_cast_to_pointer(&e);
return e;
}
const struct Type type = deref_type(&expr.details.type);
if (type.type_category ==
FN) { /* function reverts to function pointer */
return expr;
}
struct Type t2 = type;
if_array_convert_to_ptr_(&t2);
struct Expr new_expr = unary_op_(&expr, OP_ASTERISK, &t2);
new_expr.details.true_type = type;
return new_expr;
}
case RES_SIZEOF: {
struct Expr new_expr;
new_expr.details.type = INT_TYPE();
new_expr.int_value = size_of(ptr_ps, &expr.details.true_type);
new_expr.category = INT_VALUE;
return new_expr;
}
default: {
fprintf(stderr, "FAILURE::::::: INVALID TOKEN %d in unary\n", kind);
exit(EXIT_FAILURE);
}
}
}
struct Expr builtin_func_call_expr(
struct AnalyzerState *ptr_ps,
const struct Vector /* <UntypedExpr> */ *ref_arg_exprs_vec)
{
struct Type ret_type;
ret_type.type_category = VOID_;
struct Expr expr;
expr.args = init_vector();
expr.category = BUILTIN_FUNCCALL_EXPR;
for (int counter = 0; counter < ref_arg_exprs_vec->length; counter++) {
const struct UntypedExpr *ptr = ref_arg_exprs_vec->vector[counter];
struct Expr *ptr_arg = calloc(1, sizeof(struct Expr));
*ptr_arg = typecheck_expression(ptr_ps, ptr);
push_vector(&expr.args, ptr_arg);
}
expr.details.type = ret_type;
return expr;
}
struct Expr
func_call_expr(struct AnalyzerState *ptr_ps, const struct Type *ref_ret_type,
const struct Vector /*<TypeAndIdent>*/ *nullable_ref_param_infos,
const struct Vector /* <UntypedExpr> */ *ref_arg_exprs_vec,
int is_fp_call)
{
const struct Type ret_type = *ref_ret_type;
struct Expr expr;
expr.args = init_vector();
if (is_struct_or_union(&ret_type)) {
expr.size_info_for_struct_or_union_assign = size_of(ptr_ps, &ret_type);
char *str = calloc(20, sizeof(char));
sprintf(str, "@anon%d", -ptr_ps->newest_offset);
int offset = add_local_var_to_scope(ptr_ps, &ret_type, str);
enum SystemVAbiClass abi_class =
system_v_abi_class_of(ptr_ps, &ret_type);
expr.local_var_offset = offset;
if (abi_class == INTEGER_CLASS) {
expr.category = is_fp_call ? FPCALL_EXPR_RETURNING_INTEGER_CLASS
: FUNCCALL_EXPR_RETURNING_INTEGER_CLASS;
} else {
expr.category = is_fp_call ? FPCALL_EXPR_RETURNING_MEMORY_CLASS
: FUNCCALL_EXPR_RETURNING_MEMORY_CLASS;
struct Expr *ptr_arg = calloc(1, sizeof(struct Expr));
/* push pointer to anon as the first argument */
{
struct Expr anon_var_expr;
anon_var_expr.details.type = ret_type;
anon_var_expr.details.true_type = ret_type;
anon_var_expr.category = LOCAL_VAR_;
anon_var_expr.local_var_offset = offset;
const struct Type ptr_to_type_ = ptr_to_type(&ret_type);
*ptr_arg = unary_op_(&anon_var_expr, OP_AND, &ptr_to_type_);
}
push_vector(&expr.args, ptr_arg);
}
} else {
expr.category = is_fp_call ? FPCALL_EXPR : FUNCCALL_EXPR;
}
if (nullable_ref_param_infos &&
nullable_ref_param_infos->length > ref_arg_exprs_vec->length) {
simple_error("the number of argument that is passed is less than the "
"expected number of parameters");
}
for (int counter = 0; counter < ref_arg_exprs_vec->length; counter++) {
const struct UntypedExpr *ptr = ref_arg_exprs_vec->vector[counter];
struct Expr *ptr_arg = calloc(1, sizeof(struct Expr));
*ptr_arg = typecheck_expression(ptr_ps, ptr);
/* Note that `nullable_ref_param_infos->length` can be smaller than
* `ref_arg_exprs_vec->length` if you are calling a variable-argument
* function. In that case we ignore the typecheck. */
if (nullable_ref_param_infos &&
counter < nullable_ref_param_infos->length) {
const struct TypeAndIdent *pti =
nullable_ref_param_infos->vector[counter];
if (!is_compatible(ptr_ps, &pti->type, &ptr_arg->details.type)) {
fprintf(stderr, "Expected type `");
debug_print_type(&pti->type);
fprintf(stderr, "` for parameter `%s`, but got `",
pti->ident_str);
debug_print_type(&ptr_arg->details.type);
fprintf(stderr, "`.\n");
exit(EXIT_FAILURE);
}
}
push_vector(&expr.args, ptr_arg);
}
expr.details.type = ret_type;
return expr;
}
static struct Expr string_literal(const char *string);
/* returns null pointer if the name is not found */
static struct Expr * /* nullable */
from_name_to_expr(struct AnalyzerState *ptr_ps, const char *name)
{
if (strcmp(name, "__func__") == 0) {
struct Expr *p_expr = calloc(1, sizeof(struct Expr));
*p_expr = string_literal(ptr_ps->current_function_name);
return p_expr;
}
if (!is_local_var(&ptr_ps->scope_chain, name)) {
const struct EnumeratorAndValue *ptr_enum_and_value =
get_global_enumerator(&ptr_ps->global_enumerator_list, name);
if (ptr_enum_and_value) {
struct Expr expr;
expr.details.type = INT_TYPE();
expr.details.true_type = INT_TYPE();
expr.category = INT_VALUE;
expr.int_value = ptr_enum_and_value->value;
struct Expr *p_expr = calloc(1, sizeof(struct Expr));
*p_expr = expr;
return p_expr;
}
struct Type type;
if (isElem(ptr_ps->global_vars_type_map, name)) {
struct Type *ptr_type = lookup(ptr_ps->global_vars_type_map, name);
type = *ptr_type;
} else if (isElem(ptr_ps->func_info_map, name)) {
struct Type *ptr_type = lookup(ptr_ps->func_info_map, name);
type = *ptr_type;
} else {
return 0;
}
struct Expr expr;
struct Type t2 = type;
if_array_convert_to_ptr_(&t2);
expr.details.type = t2;
expr.details.true_type = type;
expr.category = GLOBAL_VAR_;
expr.global_var_name = name;
struct Expr *p_expr = calloc(1, sizeof(struct Expr));
*p_expr = expr;
return p_expr;
} else {
struct LocalVarInfo info =
resolve_name_locally(&ptr_ps->scope_chain, name);
struct Expr expr;
struct Type t2 = info.type;
if_array_convert_to_ptr_(&t2);
expr.details.type = t2;
expr.details.true_type = info.type;
expr.category = LOCAL_VAR_;
expr.local_var_offset = info.offset;
struct Expr *p_expr = calloc(1, sizeof(struct Expr));
*p_expr = expr;
return p_expr;
}
}
static struct Expr string_literal(const char *string)
{
const struct Type char_type = CHAR_TYPE();
struct Expr expr;
expr.details.type = ptr_to_type(&char_type);
expr.details.true_type = arr_of_type(&char_type, strlen(string) + 1);
expr.category = STRING_LITERAL;
expr.literal_string = string;
return expr;
}
struct Expr typecheck_expression(struct AnalyzerState *ptr_ps,
const struct UntypedExpr *ref_uexpr)
{
const struct UntypedExpr uexpr = *ref_uexpr;
switch (uexpr.category) {
case AMPERSAND_DOT: {
struct Expr struct_or_union_expr =
typecheck_expression(ptr_ps, uexpr.ptr1);
const char *ident_after_dot = uexpr.ident_after_dot;
if (!is_struct_or_union(&struct_or_union_expr.details.type)) {
fprintf(stderr, "member is requested but the left operand is "
"neither a struct nor a union\n");
exit(EXIT_FAILURE);
}
const char *tag =
struct_or_union_expr.details.type.s.struct_or_union_tag;
const struct StructOrUnionInternalCompleteInfo *ptr_info =
lookup(ptr_ps->global_struct_or_union_tag_map, tag);
if (!ptr_info) {
fprintf(stderr,
"tried to use a member of incomplete type `struct %s` / "
"`union %s`\n",
tag, tag);
exit(EXIT_FAILURE);
}
struct Vector /* <TypeAndIdent> */ vec =
*ptr_info->info.ptr_types_and_idents;
int nth_member = -1;
struct Expr expr;
for (int i = 0; i < vec.length; i++) {
const struct TypeAndIdent *ptr_vec_i = vec.vector[i];
if (strcmp(ptr_vec_i->ident_str, ident_after_dot) == 0) {
nth_member = i;
struct Type t2 = ptr_vec_i->type;
struct Type t3 = ptr_to_type(&t2);
expr.details.type = t3;
expr.details.true_type = t3;
break;
}
}
if (nth_member == -1) {
fprintf(stderr, "member `%s` does not belong to struct `%s`\n",
ident_after_dot, tag);
exit(EXIT_FAILURE);
}
int offset = ptr_info->offset_vec[nth_member];
struct Expr *ptr_struct_or_union_expr = calloc(1, sizeof(struct Expr));
*ptr_struct_or_union_expr = struct_or_union_expr;
expr.category = PTR_STRUCT_AND_OFFSET;
expr.struct_offset = offset;
expr.ptr1 = ptr_struct_or_union_expr;
expr.ptr2 = 0;
expr.ptr3 = 0;
return expr;
}
case SIZEOF_TYPE: {
struct Expr expr;
expr.details.type = INT_TYPE();
expr.int_value = size_of(ptr_ps, &uexpr.operand_of_sizeof_or_alignof);
expr.category = INT_VALUE;
return expr;
}
case ALIGNOF_TYPE: {
struct Expr expr;
expr.details.type = INT_TYPE();
expr.int_value = align_of(ptr_ps, &uexpr.operand_of_sizeof_or_alignof);
expr.category = INT_VALUE;
return expr;
}
case UNARY_EXPR: {
enum TokenKind kind = uexpr.operator_;
const struct Expr expr = typecheck_expression(ptr_ps, uexpr.ptr1);
return typecheck_unary_expression(ptr_ps, &expr, kind);
}
case FUNC_PTR_CALL: {
struct Expr fp_expr = typecheck_expression(ptr_ps, uexpr.ptr1);
struct Type fn_type;
if (fp_expr.details.type.type_category == FN) {
fn_type = fp_expr.details.type;
} else if (fp_expr.details.type.type_category == PTR_ &&
fp_expr.details.type.derived_from->type_category == FN) {
fn_type = *(fp_expr.details.type.derived_from);
} else {
fprintf(
stderr,
"function call operator was applied to something of type `");
debug_print_type(&fp_expr.details.type);
fprintf(stderr,
"`, which is neither a function nor a function pointer\n");
exit(EXIT_FAILURE);
}
int param_infos_validity = fn_type.param_infos_validity;
struct Vector /*<TypeAndIdent>*/ param_infos;
if (param_infos_validity != INVALID) {
param_infos = fn_type.param_infos;
}
struct Expr expr =
func_call_expr(ptr_ps, fn_type.derived_from,
param_infos_validity != INVALID ? ¶m_infos : 0,
&uexpr.arg_exprs_vec, 1 /* is_fp_call */);
struct Expr *ptr_fp_expr = calloc(1, sizeof(struct Expr));
*ptr_fp_expr = fp_expr;
expr.ptr1 = ptr_fp_expr;
return expr;
}
case BUILTIN_FUNCCALL: {
const char *ident_str = uexpr.var_name;
struct Expr expr = builtin_func_call_expr(ptr_ps, &uexpr.arg_exprs_vec);
expr.global_var_name = ident_str;
return expr;
}
case FUNCCALL: {
const char *ident_str = uexpr.var_name;
struct Type ret_type;
int param_infos_validity;
struct Vector /*<TypeAndIdent>*/ param_infos;
if (!isElem(ptr_ps->func_info_map, ident_str)) {
if (from_name_to_expr(ptr_ps, ident_str)) {
struct UntypedExpr *p_fp_u =
calloc(1, sizeof(struct UntypedExpr));
p_fp_u->category = VAR;
p_fp_u->var_name = ident_str;
struct UntypedExpr u = uexpr;
u.category = FUNC_PTR_CALL;
u.ptr1 = p_fp_u;
return typecheck_expression(ptr_ps, &u);
}
fprintf(stderr, "Undeclared function `%s()` detected.\n",
ident_str);
fprintf(stderr, "Assumes that `%s()` returns `int`\n", ident_str);
ret_type = INT_TYPE();
param_infos_validity = INVALID;
} else {
struct Type *ptr_func_info =
lookup(ptr_ps->func_info_map, ident_str);
ret_type = *(ptr_func_info->derived_from);
param_infos_validity = ptr_func_info->param_infos_validity;
if (param_infos_validity != INVALID) {
param_infos = ptr_func_info->param_infos;
}
}
struct Expr expr =
func_call_expr(ptr_ps, &ret_type,
param_infos_validity != INVALID ? ¶m_infos : 0,