-
Notifications
You must be signed in to change notification settings - Fork 46
/
expr.c
1368 lines (1295 loc) · 33.6 KB
/
expr.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 <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "utf.h"
#include "cc.h"
static struct expr *
mkexpr(enum exprkind k, struct type *t, struct expr *b)
{
struct expr *e;
e = xmalloc(sizeof(*e));
e->qual = QUALNONE;
e->type = t;
e->lvalue = false;
e->decayed = false;
e->kind = k;
e->base = b;
e->next = NULL;
e->toeval = NULL;
return e;
}
void
delexpr(struct expr *e)
{
struct expr *sub;
switch (e->kind) {
case EXPRCALL:
delexpr(e->base);
while (sub = e->u.call.args) {
e->u.call.args = sub->next;
delexpr(sub);
}
break;
case EXPRBITFIELD:
case EXPRINCDEC:
case EXPRUNARY:
case EXPRCAST:
delexpr(e->base);
break;
case EXPRBINARY:
delexpr(e->u.binary.l);
delexpr(e->u.binary.r);
break;
case EXPRCOND:
delexpr(e->base);
delexpr(e->u.cond.t);
delexpr(e->u.cond.f);
break;
/*
XXX: compound assignment causes some reuse of expressions,
so we can't free them without risk of a double-free
case EXPRASSIGN:
delexpr(e->assign.l);
delexpr(e->assign.r);
break;
*/
case EXPRCOMMA:
while (sub = e->base) {
e->base = sub->next;
delexpr(sub);
}
break;
}
free(e);
}
static struct expr *
mkconstexpr(struct type *t, unsigned long long n)
{
struct expr *e;
e = mkexpr(EXPRCONST, t, NULL);
e->u.constant.u = n;
return e;
}
static struct expr *mkunaryexpr(enum tokenkind, struct expr *);
/* 6.3.2.1 Conversion of arrays and function designators */
static struct expr *
decay(struct expr *e)
{
struct type *t;
enum typequal tq;
/* XXX: combine with decl.c:adjust in some way? */
t = e->type;
tq = e->qual;
switch (t->kind) {
case TYPEARRAY:
/*
XXX: qualifiers should be applied to the element
type of array types, not the array type itself
assert(tq == QUALNONE);
*/
e = mkunaryexpr(TBAND, e);
e->type = mkpointertype(t->base, t->qual | tq);
e->decayed = true;
break;
case TYPEFUNC:
e = mkunaryexpr(TBAND, e);
e->decayed = true;
break;
}
return e;
}
static struct expr *
mkunaryexpr(enum tokenkind op, struct expr *base)
{
struct expr *expr;
struct type *type;
switch (op) {
case TBAND:
if (base->decayed) {
expr = base;
base = base->base;
free(expr);
}
/*
Allow struct and union types even if they are not lvalues,
since we take their address when compiling member access.
*/
if (!base->lvalue && base->type->kind != TYPEFUNC && base->type->kind != TYPESTRUCT && base->type->kind != TYPEUNION)
error(&tok.loc, "'&' operand is not an lvalue or function designator");
if (base->kind == EXPRBITFIELD)
error(&tok.loc, "cannot take address of bit-field");
expr = mkexpr(EXPRUNARY, mkpointertype(base->type, base->qual), base);
expr->op = op;
return expr;
case TMUL:
if (base->type->kind != TYPEPOINTER)
error(&tok.loc, "cannot dereference non-pointer");
if (base->kind == EXPRUNARY && base->op == TBAND) {
type = base->type->base;
expr = base->base;
expr->type = type;
} else {
expr = mkexpr(EXPRUNARY, base->type->base, base);
expr->qual = base->type->qual;
expr->lvalue = true;
expr->op = op;
}
return decay(expr);
}
/* other unary operators get compiled as equivalent binary ones */
fatal("internal error: unknown unary operator %d", op);
return NULL;
}
static unsigned
bitfieldwidth(struct expr *e)
{
if (e->kind != EXPRBITFIELD)
return -1;
return e->type->size * 8 - e->u.bitfield.bits.before - e->u.bitfield.bits.after;
}
static struct expr *
exprconvert(struct expr *e, struct type *t)
{
if (typecompatible(e->type, t))
return e;
return mkexpr(EXPRCAST, t, e);
}
static bool
nullpointer(struct expr *e)
{
if (e->kind != EXPRCONST)
return false;
if (e->type->kind == TYPENULLPTR)
return true;
if (!(e->type->prop & PROPINT) && (e->type->kind != TYPEPOINTER || e->type->base != &typevoid))
return false;
return e->u.constant.u == 0;
}
struct expr *
exprassign(struct expr *e, struct type *t)
{
struct type *et;
et = e->type;
switch (t->kind) {
case TYPEBOOL:
if (!(et->prop & PROPARITH) && et->kind != TYPEPOINTER && et->kind != TYPENULLPTR)
error(&tok.loc, "assignment to bool must be from arithmetic, pointer, or nullptr_t type");
break;
case TYPEPOINTER:
if (nullpointer(e))
break;
if (et->kind != TYPEPOINTER)
error(&tok.loc, "assignment to pointer must be from pointer or null pointer constant");
if (t->base != &typevoid && et->base != &typevoid && !typecompatible(t->base, et->base))
error(&tok.loc, "base types of pointer assignment must be compatible or void");
if ((et->qual & t->qual) != et->qual)
error(&tok.loc, "assignment to pointer discards qualifiers");
break;
case TYPENULLPTR:
if (!nullpointer(e))
error(&tok.loc, "assignment to nullptr_t must be from null pointer constant or expression with type nullptr_t");
break;
case TYPESTRUCT:
case TYPEUNION:
if (!typecompatible(t, et))
error(&tok.loc, "assignment to %s type must be from compatible type", tokstr[t->kind]);
break;
default:
assert(t->prop & PROPARITH);
if (!(et->prop & PROPARITH))
error(&tok.loc, "assignment to arithmetic type must be from arithmetic type");
break;
}
return exprconvert(e, t);
}
struct expr *
exprpromote(struct expr *e)
{
struct type *t;
t = typepromote(e->type, bitfieldwidth(e));
return exprconvert(e, t);
}
static struct type *
commonreal(struct expr **e1, struct expr **e2)
{
struct type *t;
t = typecommonreal((*e1)->type, bitfieldwidth(*e1), (*e2)->type, bitfieldwidth(*e2));
*e1 = exprconvert(*e1, t);
*e2 = exprconvert(*e2, t);
return t;
}
static struct expr *
mkbinaryexpr(struct location *loc, enum tokenkind op, struct expr *l, struct expr *r)
{
struct expr *e;
struct type *t = NULL;
enum typeprop lp, rp;
lp = l->type->prop;
rp = r->type->prop;
switch (op) {
case TLOR:
case TLAND:
if (!(lp & PROPSCALAR))
error(loc, "left operand of '%s' operator must be scalar", tokstr[op]);
if (!(rp & PROPSCALAR))
error(loc, "right operand of '%s' operator must be scalar", tokstr[op]);
t = &typeint;
break;
case TEQL:
case TNEQ:
t = &typeint;
if (lp & PROPARITH && rp & PROPARITH) {
commonreal(&l, &r);
break;
}
if (l->type->kind != TYPEPOINTER)
e = l, l = r, r = e;
if (l->type->kind != TYPEPOINTER)
error(loc, "invalid operands to '%s' operator", tokstr[op]);
if (nullpointer(eval(r))) {
r = exprconvert(r, l->type);
break;
}
if (nullpointer(eval(l))) {
l = exprconvert(l, r->type);
break;
}
if (r->type->kind != TYPEPOINTER)
error(loc, "invalid operands to '%s' operator", tokstr[op]);
if (l->type->base->kind == TYPEVOID)
e = l, l = r, r = e;
if (r->type->base->kind == TYPEVOID && l->type->base->kind != TYPEFUNC)
r = exprconvert(r, l->type);
else if (!typecompatible(l->type->base, r->type->base))
error(loc, "pointer operands to '%s' operator are to incompatible types", tokstr[op]);
break;
case TLESS:
case TGREATER:
case TLEQ:
case TGEQ:
t = &typeint;
if (lp & PROPREAL && rp & PROPREAL) {
commonreal(&l, &r);
} else if (l->type->kind == TYPEPOINTER && r->type->kind == TYPEPOINTER) {
if (!typecompatible(l->type->base, r->type->base) || l->type->base->kind == TYPEFUNC)
error(loc, "pointer operands to '%s' operator must be to compatible object types", tokstr[op]);
} else {
error(loc, "invalid operands to '%s' operator", tokstr[op]);
}
break;
case TBOR:
case TXOR:
case TBAND:
t = commonreal(&l, &r);
break;
case TADD:
if (lp & PROPARITH && rp & PROPARITH) {
t = commonreal(&l, &r);
break;
}
if (r->type->kind == TYPEPOINTER)
e = l, l = r, r = e, rp = lp;
if (l->type->kind != TYPEPOINTER || !(rp & PROPINT))
error(loc, "invalid operands to '+' operator");
t = l->type;
if (t->base->incomplete || t->base->kind == TYPEFUNC)
error(loc, "pointer operand to '+' must be to complete object type");
r = mkbinaryexpr(loc, TMUL, exprconvert(r, &typeulong), mkconstexpr(&typeulong, t->base->size));
break;
case TSUB:
if (lp & PROPARITH && rp & PROPARITH) {
t = commonreal(&l, &r);
break;
}
if (l->type->kind != TYPEPOINTER || !(rp & PROPINT) && r->type->kind != TYPEPOINTER)
error(loc, "invalid operands to '-' operator");
if (l->type->base->incomplete || l->type->base->kind == TYPEFUNC)
error(loc, "pointer operand to '-' must be to complete object type");
if (rp & PROPINT) {
t = l->type;
r = mkbinaryexpr(loc, TMUL, exprconvert(r, &typeulong), mkconstexpr(&typeulong, t->base->size));
} else {
if (!typecompatible(l->type->base, r->type->base))
error(&tok.loc, "pointer operands to '-' are to incompatible types");
op = TDIV;
t = &typelong;
e = mkbinaryexpr(loc, TSUB, exprconvert(l, &typelong), exprconvert(r, &typelong));
r = mkconstexpr(&typelong, l->type->base->size);
l = e;
}
break;
case TMOD:
if (!(lp & PROPINT) || !(rp & PROPINT))
error(loc, "operands to '%%' operator must be integer");
t = commonreal(&l, &r);
break;
case TMUL:
case TDIV:
if (!(lp & PROPARITH) || !(rp & PROPARITH))
error(loc, "operands to '%s' operator must be arithmetic", tokstr[op]);
t = commonreal(&l, &r);
break;
case TSHL:
case TSHR:
if (!(lp & PROPINT) || !(rp & PROPINT))
error(loc, "operands to '%s' operator must be integer", tokstr[op]);
l = exprpromote(l);
r = exprpromote(r);
t = l->type;
break;
default:
fatal("internal error: unknown binary operator %d", op);
}
e = mkexpr(EXPRBINARY, t, NULL);
e->op = op;
e->u.binary.l = l;
e->u.binary.r = r;
return e;
}
static struct type *
inttype(unsigned long long val, bool decimal, char *end)
{
static struct {
struct type *type;
const char *end1, *end2;
} limits[] = {
{&typeint, "", NULL},
{&typeuint, "u", NULL},
{&typelong, "l", NULL},
{&typeulong, "ul", "lu"},
{&typellong, "ll", NULL},
{&typeullong, "ull", "llu"},
};
struct type *t;
size_t i, step;
for (i = 0; end[i]; ++i)
end[i] = tolower(end[i]);
for (i = 0; i < LEN(limits); ++i) {
if (strcmp(end, limits[i].end1) == 0)
break;
if (limits[i].end2 && strcmp(end, limits[i].end2) == 0)
break;
}
if (i == LEN(limits))
error(&tok.loc, "invalid integer constant suffix '%s'", end);
step = i % 2 || decimal ? 2 : 1;
for (; i < LEN(limits); i += step) {
t = limits[i].type;
if (typehasint(t, val, false))
return t;
}
error(&tok.loc, "no suitable type for constant '%s'", tok.lit);
return NULL;
}
static int
isodigit(int c)
{
return '0' <= c && c <= '8';
}
static size_t
decodechar(const char *src, uint_least32_t *chr, bool *hexoct, const char *desc, struct location *loc)
{
uint_least32_t c;
size_t n;
int i;
const unsigned char *s = (const unsigned char *)src;
if (*s == '\\') {
++s;
switch (*s) {
case '\'':
case '"':
case '?':
case '\\': c = *s; ++s; break;
case 'a': c = '\a'; ++s; break;
case 'b': c = '\b'; ++s; break;
case 'f': c = '\f'; ++s; break;
case 'n': c = '\n'; ++s; break;
case 'r': c = '\r'; ++s; break;
case 't': c = '\t'; ++s; break;
case 'v': c = '\v'; ++s; break;
case 'x':
++s;
assert(isxdigit(*s));
c = 0;
do c = c * 16 + (*s > '9' ? 10 + tolower(*s) - 'a' : *s - '0');
while (isxdigit(*++s));
if (hexoct)
*hexoct = true;
break;
default:
assert(isodigit(*s));
c = 0;
i = 0;
do c = c * 8 + (*s++ - '0');
while (++i < 3 && isodigit(*s));
if (hexoct)
*hexoct = true;
}
} else {
n = utf8dec(&c, s, 4);
if (n == (size_t)-1)
error(loc, "%s contains invalid UTF-8", desc);
s += n;
}
*chr = c;
return s - (const unsigned char *)src;
}
static size_t
encodechar8(void *dst, uint_least32_t chr, bool hexoct)
{
if (!hexoct)
return utf8enc(dst, chr);
*(unsigned char *)dst = chr;
return 1;
}
static size_t
encodechar16(void *dst, uint_least32_t chr, bool hexoct)
{
if (!hexoct)
return utf16enc(dst, chr) * sizeof(uint_least16_t);
*(uint_least16_t *)dst = chr;
return sizeof(uint_least16_t);
}
static size_t
encodechar32(void *dst, uint_least32_t chr, bool hexoct)
{
*(uint_least32_t *)dst = chr;
return sizeof(uint_least32_t);
}
struct type *
stringconcat(struct stringlit *str, bool forceutf8)
{
static struct array parts;
struct {
struct location loc;
char *str;
} *p;
int kind, newkind;
struct type *t;
size_t (*encodechar)(void *, uint_least32_t, bool);
char *src;
unsigned char *buf, *dst;
uint_least32_t chr;
bool hexoct;
size_t len, width;
assert(tok.kind == TSTRINGLIT);
parts.len = 0;
len = 0;
kind = 0;
do {
src = tok.lit;
switch (*src) {
case 'u': if (src[1] == '8') ++src; /* fallthrough */
case 'L':
case 'U': newkind = *src, ++src; break;
case '"': newkind = 0; break;
default: assert(0);
}
if (kind != newkind && kind && newkind)
error(&tok.loc, "adjacent string literals have differing prefixes");
if (newkind)
kind = newkind;
p = arrayadd(&parts, sizeof(*p));
p->loc = tok.loc;
p->str = src + 1;
len += strlen(src) - 2;
next();
} while (tok.kind == TSTRINGLIT);
if (forceutf8)
kind = '8';
++len; /* null byte */
switch (kind) {
case 0: t = &typechar; break;
case '8': t = &typeuchar; break;
case 'u': t = &typeushort; break;
case 'U': t = &typeuint; break;
case 'L': t = targ->typewchar; break;
default: assert(0);
}
switch (t->size) {
case 1:
width = 1;
encodechar = encodechar8;
break;
case 2:
width = sizeof(uint_least16_t);
encodechar = encodechar16;
break;
case 4:
width = sizeof(uint_least32_t);
encodechar = encodechar32;
break;
default:
assert(0);
}
buf = xreallocarray(NULL, len, width);
str->data = buf;
dst = buf;
arrayforeach(&parts, p) {
src = p->str;
while (*src != '"') {
hexoct = false;
src += decodechar(src, &chr, &hexoct, "string literal", &p->loc);
dst += encodechar(dst, chr, hexoct);
}
}
dst += encodechar(dst, 0, false);
str->size = (dst - buf) / width;
return t;
}
static struct expr *
generic(struct scope *s)
{
struct expr *e, *match = NULL, *def = NULL;
struct type *t, *want;
enum typequal qual;
next();
expect(TLPAREN, "after '_Generic'");
e = assignexpr(s);
expect(TCOMMA, "after generic selector expression");
want = e->type;
delexpr(e);
do {
if (consume(TDEFAULT)) {
if (def)
error(&tok.loc, "multiple default expressions in generic association list");
expect(TCOLON, "after 'default'");
def = assignexpr(s);
} else {
qual = QUALNONE;
t = typename(s, &qual, NULL);
if (!t)
error(&tok.loc, "expected typename for generic association");
if (t->kind == TYPEFUNC)
error(&tok.loc, "generic association must have object type");
if (t->incomplete)
error(&tok.loc, "generic association must have complete type");
if (t->prop & PROPVM)
error(&tok.loc, "generic association has variably modified type");
expect(TCOLON, "after type name");
e = assignexpr(s);
if (typecompatible(t, want) && qual == QUALNONE) {
if (match)
error(&tok.loc, "generic selector matches multiple associations");
match = e;
} else {
delexpr(e);
}
}
} while (consume(TCOMMA));
expect(TRPAREN, "after generic assocation list");
if (!match) {
if (!def)
error(&tok.loc, "generic selector matches no associations and no default was specified");
match = def;
} else if (def) {
delexpr(def);
}
return match;
}
/* 6.5 Expressions */
static struct expr *
primaryexpr(struct scope *s)
{
struct expr *e;
struct decl *d;
struct type *t;
char *src, *end;
uint_least32_t chr;
int base;
switch (tok.kind) {
case TIDENT:
d = scopegetdecl(s, tok.lit, 1);
if (!d)
error(&tok.loc, "undeclared identifier: %s", tok.lit);
e = mkexpr(EXPRIDENT, d->type, NULL);
e->qual = d->qual;
e->lvalue = d->kind == DECLOBJECT;
e->u.ident.decl = d;
if (d->kind != DECLBUILTIN)
e = decay(e);
next();
break;
case TSTRINGLIT:
e = mkexpr(EXPRSTRING, NULL, NULL);
t = stringconcat(&e->u.string, false);
e->type = mkarraytype(t, QUALNONE, e->u.string.size);
e->lvalue = true;
e = decay(e);
break;
case TCHARCONST:
src = tok.lit;
switch (*src) {
case 'L': ++src; t = targ->typewchar; break;
case 'u': ++src; t = *src == '8' ? ++src, &typeuchar : &typeushort; break;
case 'U': ++src; t = &typeuint; break;
default: t = &typeint;
}
assert(*src == '\'');
++src;
src += decodechar(src, &chr, NULL, "character constant", &tok.loc);
e = mkconstexpr(t, chr);
if (*src != '\'')
error(&tok.loc, "character constant contains more than one character: %c", *src);
next();
break;
case TNUMBER:
e = mkexpr(EXPRCONST, NULL, NULL);
if (tok.lit[0] == '0') {
switch (tolower(tok.lit[1])) {
case 'x': base = 16; break;
case 'b': base = 2; break;
default: base = 8; break;
}
} else {
base = 10;
}
if (strpbrk(tok.lit, base == 16 ? ".pP" : ".eE")) {
/* floating constant */
e->u.constant.f = strtod(tok.lit, &end);
if (end == tok.lit)
error(&tok.loc, "invalid floating constant '%s'", tok.lit);
if (!end[0])
e->type = &typedouble;
else if (tolower(end[0]) == 'f' && !end[1])
e->type = &typefloat;
else if (tolower(end[0]) == 'l' && !end[1])
e->type = &typeldouble;
else
error(&tok.loc, "invalid floating constant suffix '%s'", end);
} else {
src = tok.lit;
if (base == 2)
src += 2;
/* integer constant */
e->u.constant.u = strtoull(src, &end, base);
if (end == src)
error(&tok.loc, "invalid integer constant '%s'", tok.lit);
e->type = inttype(e->u.constant.u, base == 10, end);
}
next();
break;
case TTRUE:
case TFALSE:
e = mkexpr(EXPRCONST, &typebool, NULL);
e->u.constant.u = tok.kind == TTRUE;
next();
break;
case TNULLPTR:
e = mkexpr(EXPRCONST, &typenullptr, NULL);
e->u.constant.u = 0;
next();
break;
case TLPAREN:
next();
e = expr(s);
expect(TRPAREN, "after expression");
break;
case T_GENERIC:
e = generic(s);
break;
default:
error(&tok.loc, "expected primary expression");
return NULL; /* unreachable */
}
return e;
}
/* TODO: merge with init.c:designator() */
static void
designator(struct scope *s, struct type *t, unsigned long long *offset)
{
char *name;
struct member *m;
unsigned long long i;
for (;;) {
switch (tok.kind) {
case TLBRACK:
if (t->kind != TYPEARRAY)
error(&tok.loc, "index designator is only valid for array types");
next();
i = intconstexpr(s, false);
expect(TRBRACK, "for index designator");
t = t->base;
*offset += i * t->size;
break;
case TPERIOD:
if (t->kind != TYPESTRUCT && t->kind != TYPEUNION)
error(&tok.loc, "member designator only valid for struct/union types");
next();
name = expect(TIDENT, "for member designator");
m = typemember(t, name, offset);
if (!m)
error(&tok.loc, "%s has no member named '%s'", t->kind == TYPEUNION ? "union" : "struct", name);
free(name);
t = m->type;
break;
default:
return;
}
}
}
static struct expr *
builtinfunc(struct scope *s, enum builtinkind kind)
{
struct expr *e, *toeval;
struct type *t;
struct member *m;
char *name;
unsigned long long offset;
switch (kind) {
case BUILTINALLOCA:
e = exprassign(assignexpr(s), &typeulong);
e = mkexpr(EXPRBUILTIN, mkpointertype(&typevoid, QUALNONE), e);
e->u.builtin.kind = BUILTINALLOCA;
break;
case BUILTINCONSTANTP:
e = mkconstexpr(&typeint, eval(condexpr(s))->kind == EXPRCONST);
break;
case BUILTINEXPECT:
/* just a no-op for now */
/* TODO: check that the expression and the expected value have type 'long' */
e = assignexpr(s);
expect(TCOMMA, "after expression");
delexpr(assignexpr(s));
break;
case BUILTININFF:
e = mkexpr(EXPRCONST, &typefloat, NULL);
/* TODO: use INFINITY here when we can handle musl's math.h */
e->u.constant.f = strtod("inf", NULL);
break;
case BUILTINNANF:
e = assignexpr(s);
if (!e->decayed || e->base->kind != EXPRSTRING || e->base->u.string.size > 1)
error(&tok.loc, "__builtin_nanf currently only supports empty string literals");
e = mkexpr(EXPRCONST, &typefloat, NULL);
/* TODO: use NAN here when we can handle musl's math.h */
e->u.constant.f = strtod("nan", NULL);
break;
case BUILTINOFFSETOF:
t = typename(s, NULL, NULL);
expect(TCOMMA, "after type name");
name = expect(TIDENT, "after ','");
if (t->kind != TYPESTRUCT && t->kind != TYPEUNION)
error(&tok.loc, "type is not a struct/union type");
offset = 0;
m = typemember(t, name, &offset);
if (!m)
error(&tok.loc, "struct/union has no member named '%s'", name);
designator(s, m->type, &offset);
e = mkconstexpr(&typeulong, offset);
free(name);
break;
case BUILTINTYPESCOMPATIBLEP:
t = typename(s, NULL, NULL);
expect(TCOMMA, "after type name");
e = mkconstexpr(&typeint, typecompatible(t, typename(s, NULL, NULL)));
break;
case BUILTINUNREACHABLE:
e = mkexpr(EXPRBUILTIN, &typevoid, NULL);
e->u.builtin.kind = BUILTINUNREACHABLE;
break;
case BUILTINVAARG:
e = mkexpr(EXPRBUILTIN, NULL, assignexpr(s));
e->u.builtin.kind = BUILTINVAARG;
if (!typesame(e->base->type, typeadjvalist))
error(&tok.loc, "va_arg argument must have type va_list");
if (typeadjvalist == targ->typevalist)
e->base = mkunaryexpr(TBAND, e->base);
expect(TCOMMA, "after va_list");
e->type = typename(s, &e->qual, &toeval);
e->toeval = toeval;
break;
case BUILTINVACOPY:
e = mkexpr(EXPRASSIGN, &typevoid, NULL);
e->u.assign.l = assignexpr(s);
if (!typesame(e->u.assign.l->type, typeadjvalist))
error(&tok.loc, "va_copy destination must have type va_list");
if (typeadjvalist != targ->typevalist)
e->u.assign.l = mkunaryexpr(TMUL, e->u.assign.l);
expect(TCOMMA, "after target va_list");
e->u.assign.r = assignexpr(s);
if (!typesame(e->u.assign.r->type, typeadjvalist))
error(&tok.loc, "va_copy source must have type va_list");
if (typeadjvalist != targ->typevalist)
e->u.assign.r = mkunaryexpr(TMUL, e->u.assign.r);
break;
case BUILTINVAEND:
e = assignexpr(s);
if (!typesame(e->type, typeadjvalist))
error(&tok.loc, "va_end argument must have type va_list");
e = mkexpr(EXPRCAST, &typevoid, e);
break;
case BUILTINVASTART:
e = mkexpr(EXPRBUILTIN, &typevoid, assignexpr(s));
e->u.builtin.kind = BUILTINVASTART;
if (!typesame(e->base->type, typeadjvalist))
error(&tok.loc, "va_start argument must have type va_list");
if (typeadjvalist == targ->typevalist)
e->base = mkunaryexpr(TBAND, e->base);
if (consume(TCOMMA))
delexpr(assignexpr(s));
break;
default:
fatal("internal error; unknown builtin");
return NULL; /* unreachable */
}
return e;
}
static struct expr *
mkincdecexpr(enum tokenkind op, struct expr *base, bool post)
{
struct expr *e;
if (!base->lvalue)
error(&tok.loc, "operand of '%s' operator must be an lvalue", tokstr[op]);
if (base->qual & QUALCONST)
error(&tok.loc, "operand of '%s' operator is const qualified", tokstr[op]);
e = mkexpr(EXPRINCDEC, base->type, base);
e->op = op;
e->u.incdec.post = post;
return e;
}
static struct expr *
postfixexpr(struct scope *s, struct expr *r)
{
struct expr *e, *arr, *idx, *tmp, **end;
struct type *t;
struct decl *p;
struct member *m;
unsigned long long offset;
enum typequal tq;
enum tokenkind op;
bool lvalue;
if (!r)
r = primaryexpr(s);
for (;;) {
switch (tok.kind) {
case TLBRACK: /* subscript */
next();
arr = r;
idx = expr(s);
if (arr->type->kind != TYPEPOINTER) {
if (idx->type->kind != TYPEPOINTER)
error(&tok.loc, "either array or index must be pointer type");
tmp = arr;
arr = idx;
idx = tmp;
}
if (arr->type->base->incomplete)
error(&tok.loc, "array is pointer to incomplete type");
if (!(idx->type->prop & PROPINT))
error(&tok.loc, "index is not an integer type");
e = mkunaryexpr(TMUL, mkbinaryexpr(&tok.loc, TADD, arr, idx));
expect(TRBRACK, "after array index");
break;
case TLPAREN: /* function call */
next();
if (r->kind == EXPRIDENT && r->u.ident.decl->kind == DECLBUILTIN) {
e = builtinfunc(s, r->u.ident.decl->u.builtin);
expect(TRPAREN, "after builtin parameters");
break;
}
if (r->type->kind != TYPEPOINTER || r->type->base->kind != TYPEFUNC)
error(&tok.loc, "called object is not a function");
t = r->type->base;
e = mkexpr(EXPRCALL, t->base, r);
e->u.call.args = NULL;
e->u.call.nargs = 0;
p = t->u.func.params;
end = &e->u.call.args;
while (tok.kind != TRPAREN) {
if (e->u.call.args)
expect(TCOMMA, "or ')' after function call argument");
if (!p && !t->u.func.isvararg)
error(&tok.loc, "too many arguments for function call");
*end = assignexpr(s);
if (t->u.func.isvararg && !p)
*end = exprpromote(*end);
else
*end = exprassign(*end, p->type);
end = &(*end)->next;
++e->u.call.nargs;
if (p)
p = p->next;
}
if (p && !t->u.func.isvararg)
error(&tok.loc, "not enough arguments for function call");
e = decay(e);
next();
break;
case TPERIOD:
r = mkunaryexpr(TBAND, r);
/* fallthrough */
case TARROW:
op = tok.kind;
if (r->type->kind != TYPEPOINTER)
error(&tok.loc, "'%s' operator must be applied to pointer to struct/union", tokstr[op]);
t = r->type->base;
tq = r->type->qual;
if (t->kind != TYPESTRUCT && t->kind != TYPEUNION)
error(&tok.loc, "'%s' operator must be applied to pointer to struct/union", tokstr[op]);
next();
if (tok.kind != TIDENT)
error(&tok.loc, "expected identifier after '%s' operator", tokstr[op]);
lvalue = op == TARROW || r->base->lvalue;
offset = 0;
m = typemember(t, tok.lit, &offset);
if (!m)
error(&tok.loc, "struct/union has no member named '%s'", tok.lit);
r = mkbinaryexpr(&tok.loc, TADD, exprconvert(r, &typeulong), mkconstexpr(&typeulong, offset));
r->type = mkpointertype(m->type, tq | m->qual);
r = mkunaryexpr(TMUL, r);
r->lvalue = lvalue;
if (m->bits.before || m->bits.after) {
e = mkexpr(EXPRBITFIELD, r->type, r);