aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

expr.c (49428B)


      1 #include "util.h"
      2 #include "cc.h"
      3 #include <assert.h>
      4 #include <limits.h>
      5 #include <stdbool.h>
      6 #include <stdint.h>
      7 #include <stdlib.h>
      8 #include <string.h>
      9 
     10 #include "utf.h"
     11 
     12 static struct expr *
     13 mkexpr(enum exprkind k, struct type *t, struct expr *b)
     14 {
     15   struct expr *e;
     16 
     17   e          = xmalloc(sizeof(*e));
     18   e->qual    = QUALNONE;
     19   e->type    = t;
     20   e->lvalue  = false;
     21   e->decayed = false;
     22   e->kind    = k;
     23   e->base    = b;
     24   e->next    = NULL;
     25   e->toeval  = NULL;
     26 
     27   return e;
     28 }
     29 
     30 void
     31 delexpr(struct expr *e)
     32 {
     33   struct expr *sub;
     34 
     35   switch (e->kind) {
     36     case EXPRCALL:
     37       delexpr(e->base);
     38       while ((sub = e->u.call.args)) {
     39         e->u.call.args = sub->next;
     40         delexpr(sub);
     41       }
     42       break;
     43     case EXPRBITFIELD:
     44     case EXPRINCDEC:
     45     case EXPRUNARY:
     46     case EXPRCAST:
     47       delexpr(e->base);
     48       break;
     49     case EXPRBINARY:
     50       delexpr(e->u.binary.l);
     51       delexpr(e->u.binary.r);
     52       break;
     53     case EXPRCOND:
     54       delexpr(e->base);
     55       if (e->u.cond.t != e->base)
     56         delexpr(e->u.cond.t);
     57       delexpr(e->u.cond.f);
     58       break;
     59     /*
     60      * XXX: compound assignment causes some reuse of expressions,
     61      * so we cant free them without risk of a double-free
     62      *
     63      * case EXPRASSIGN:
     64      * delexpr(e->assign.l);
     65      * delexpr(e->assign.r);
     66      * break;
     67      */
     68     case EXPRCOMMA:
     69       while ((sub = e->base)) {
     70         e->base = sub->next;
     71         delexpr(sub);
     72       }
     73       break;
     74     default:
     75       break;
     76   }
     77   free(e);
     78 }
     79 
     80 static struct expr *
     81 mkconstexpr(struct type *t, unsigned long long n)
     82 {
     83   struct expr *e;
     84 
     85   e               = mkexpr(EXPRCONST, t, NULL);
     86   e->u.constant.u = n;
     87 
     88   return e;
     89 }
     90 
     91 static struct expr *mkunaryexpr(enum tokenkind, struct expr *);
     92 
     93 /* 6.3.2.1 conversion of arrays and function designators */
     94 static struct expr *
     95 decay(struct expr *e)
     96 {
     97   struct type  *t;
     98   enum typequal tq;
     99 
    100   /* XXX: combine with decl.c:adjust in some way? */
    101   t  = e->type;
    102   tq = e->qual;
    103   switch (t->kind) {
    104     case TYPEARRAY:
    105       /*
    106        * XXX: qualifiers should be applied to the element
    107        * type of array types, not the array type itself
    108        *
    109        * assert(tq == QUALNONE);
    110        */
    111       e          = mkunaryexpr(TBAND, e);
    112       e->type    = mkpointertype(t->base, t->qual | tq);
    113       e->decayed = true;
    114       break;
    115     case TYPEFUNC:
    116       e          = mkunaryexpr(TBAND, e);
    117       e->decayed = true;
    118       break;
    119     default:
    120       break;
    121   }
    122 
    123   return e;
    124 }
    125 
    126 static struct expr *
    127 mkunaryexpr(enum tokenkind op, struct expr *base)
    128 {
    129   struct expr *expr;
    130   struct type *type;
    131 
    132   switch (op) {
    133     case TBAND:
    134       if (base->decayed) {
    135         expr = base;
    136         base = base->base;
    137         free(expr);
    138       }
    139       /*
    140        * allow struct and union types even if they are not lvalues,
    141        * since we take their address when compiling member access
    142        */
    143       if (!base->lvalue && base->type->kind != TYPEFUNC && base->type->kind != TYPESTRUCT
    144           && base->type->kind != TYPEUNION)
    145         error(
    146             &tok.loc,
    147             "'&' operand is not an lvalue or "
    148             "function designator"
    149         );
    150       if (base->kind == EXPRBITFIELD)
    151         error(&tok.loc, "cannot take address of bit-field");
    152       expr     = mkexpr(EXPRUNARY, mkpointertype(base->type, base->qual), base);
    153       expr->op = op;
    154       return expr;
    155     case TMUL:
    156       if (base->type->kind != TYPEPOINTER)
    157         error(&tok.loc, "cannot dereference non-pointer");
    158       if (base->kind == EXPRUNARY && base->op == TBAND && base->base->kind != EXPRSTRING) {
    159         type       = base->type->base;
    160         expr       = base->base;
    161         expr->type = type;
    162       } else {
    163         expr         = mkexpr(EXPRUNARY, base->type->base, base);
    164         expr->qual   = base->type->qual;
    165         expr->lvalue = true;
    166         expr->op     = op;
    167       }
    168       return decay(expr);
    169     default:
    170       break;
    171   }
    172   /* other unary operators get compiled as equivalent binary ones */
    173   fatal("internal error: unknown unary operator %d", op);
    174   return NULL;
    175 }
    176 
    177 static unsigned
    178 bitfieldwidth(struct expr *e)
    179 {
    180   if (e->kind != EXPRBITFIELD)
    181     return -1;
    182   return e->type->size * 8 - e->u.bitfield.bits.before - e->u.bitfield.bits.after;
    183 }
    184 
    185 static struct expr *
    186 exprconvert(struct expr *e, struct type *t)
    187 {
    188   if (typecompatible(e->type, t))
    189     return e;
    190   return mkexpr(EXPRCAST, t, e);
    191 }
    192 
    193 static bool
    194 nullpointer(struct expr *e)
    195 {
    196   if (e->kind != EXPRCONST)
    197     return false;
    198   if (e->type->kind == TYPENULLPTR)
    199     return true;
    200   if (!(e->type->prop & PROPINT) && (e->type->kind != TYPEPOINTER || e->type->base != &typevoid))
    201     return false;
    202   return e->u.constant.u == 0;
    203 }
    204 
    205 struct expr *
    206 exprassign(struct expr *e, struct type *t)
    207 {
    208   struct type *et;
    209 
    210   et = e->type;
    211   switch (t->kind) {
    212     case TYPEBOOL:
    213       if (!(et->prop & PROPARITH) && et->kind != TYPEPOINTER && et->kind != TYPENULLPTR)
    214         error(
    215             &tok.loc,
    216             "assignment to bool must be from arithmetic, "
    217             "pointer, or nullptr_t type"
    218         );
    219       break;
    220     case TYPEPOINTER:
    221       if (nullpointer(e))
    222         break;
    223       if (et->kind != TYPEPOINTER)
    224         error(
    225             &tok.loc,
    226             "assignment to pointer must be from "
    227             "pointer or null pointer constant"
    228         );
    229       if (t->base != &typevoid && et->base != &typevoid && !typecompatible(t->base, et->base))
    230         error(
    231             &tok.loc,
    232             "base types of pointer assignment must "
    233             "be compatible or void"
    234         );
    235       if ((et->qual & t->qual) != et->qual)
    236         error(&tok.loc, "assignment to pointer discards qualifiers");
    237       break;
    238     case TYPENULLPTR:
    239       if (!nullpointer(e))
    240         error(
    241             &tok.loc,
    242             "assignment to nullptr_t must be from "
    243             "null pointer constant or expression "
    244             "with type nullptr_t"
    245         );
    246       break;
    247     case TYPESTRUCT:
    248     case TYPEUNION:
    249     case TYPECOMPLEX:
    250       if (!typecompatible(t, et))
    251         error(
    252             &tok.loc,
    253             "assignment to %s type must be from compatible "
    254             "type",
    255             t->kind == TYPEUNION     ? "union"
    256             : t->kind == TYPECOMPLEX ? "complex"
    257                                      : "struct"
    258         );
    259       break;
    260     default:
    261       assert(t->prop & PROPARITH);
    262       if (!(et->prop & PROPARITH))
    263         error(
    264             &tok.loc,
    265             "assignment to arithmetic type must be "
    266             "from arithmetic type"
    267         );
    268       break;
    269   }
    270   return exprconvert(e, t);
    271 }
    272 
    273 struct expr *
    274 exprpromote(struct expr *e)
    275 {
    276   struct type *t;
    277 
    278   t = typepromote(e->type, bitfieldwidth(e));
    279   return exprconvert(e, t);
    280 }
    281 
    282 static struct type *
    283 commonreal(struct expr **e1, struct expr **e2)
    284 {
    285   struct type *t;
    286 
    287   t   = typecommonreal((*e1)->type, bitfieldwidth(*e1), (*e2)->type, bitfieldwidth(*e2));
    288   *e1 = exprconvert(*e1, t);
    289   *e2 = exprconvert(*e2, t);
    290 
    291   return t;
    292 }
    293 
    294 static struct expr *
    295 mksizeofexpr(struct type *t)
    296 {
    297   struct expr *e;
    298 
    299   if (t->kind == TYPEARRAY && t->size == 0) {
    300     e              = mkexpr(EXPRSIZEOF, &typeulong, NULL);
    301     e->u.szof.type = t;
    302   } else {
    303     e = mkconstexpr(&typeulong, t->size);
    304   }
    305 
    306   return e;
    307 }
    308 
    309 /* complex has none of PROPARITH/PROPSCALAR/PROPREAL (see type.c), so
    310  * it never reaches the usual-arithmetic-conversion paths below on its
    311  * own; every operator that accepts it checks for it explicitly, up
    312  * front, and requires an exact type match on both sides (no mixed
    313  * float/double complex, no mixed real/complex, matching how far this
    314  * compiler's complex support currently goes) */
    315 static int
    316 iscomplexop(struct expr *l, struct expr *r)
    317 {
    318   return l->type->kind == TYPECOMPLEX || r->type->kind == TYPECOMPLEX;
    319 }
    320 
    321 static struct expr *
    322 mkbinaryexpr(struct location *loc, enum tokenkind op, struct expr *l, struct expr *r)
    323 {
    324   struct expr  *e;
    325   struct type  *t = NULL;
    326   enum typeprop lp, rp;
    327 
    328   lp = l->type->prop;
    329   rp = r->type->prop;
    330   switch (op) {
    331     case TLOR:
    332     case TLAND:
    333       if (!(lp & PROPSCALAR))
    334         error(loc, "left operand of '%s' operator must be scalar", tokenstr(op));
    335       if (!(rp & PROPSCALAR))
    336         error(loc, "right operand of '%s' operator must be scalar", tokenstr(op));
    337       t = &typeint;
    338       break;
    339     case TEQL:
    340     case TNEQ:
    341       t = &typeint;
    342       if (iscomplexop(l, r)) {
    343         if (l->type->kind != TYPECOMPLEX || r->type->kind != TYPECOMPLEX || l->type != r->type)
    344           error(
    345               loc,
    346               "'%s' between complex operands requires the same complex type on both sides",
    347               tokenstr(op)
    348           );
    349         break;
    350       }
    351       if (lp & PROPARITH && rp & PROPARITH) {
    352         commonreal(&l, &r);
    353         break;
    354       }
    355       if (l->type->kind != TYPEPOINTER)
    356         e = l, l = r, r = e;
    357       if (l->type->kind != TYPEPOINTER)
    358         error(loc, "invalid operands to '%s' operator", tokenstr(op));
    359       if (nullpointer(eval(r))) {
    360         r = exprconvert(r, l->type);
    361         break;
    362       }
    363       if (nullpointer(eval(l))) {
    364         l = exprconvert(l, r->type);
    365         break;
    366       }
    367       if (r->type->kind != TYPEPOINTER)
    368         error(loc, "invalid operands to '%s' operator", tokenstr(op));
    369       if (l->type->base->kind == TYPEVOID)
    370         e = l, l = r, r = e;
    371       if (r->type->base->kind == TYPEVOID && l->type->base->kind != TYPEFUNC)
    372         r = exprconvert(r, l->type);
    373       else if (!typecompatible(l->type->base, r->type->base))
    374         error(
    375             loc,
    376             "pointer operands to '%s' operator are to "
    377             "incompatible types",
    378             tokenstr(op)
    379         );
    380       break;
    381     case TLESS:
    382     case TGREATER:
    383     case TLEQ:
    384     case TGEQ:
    385       t = &typeint;
    386       if (lp & PROPREAL && rp & PROPREAL) {
    387         commonreal(&l, &r);
    388       } else if (l->type->kind == TYPEPOINTER && r->type->kind == TYPEPOINTER) {
    389         if (!typecompatible(l->type->base, r->type->base) || l->type->base->kind == TYPEFUNC)
    390           error(
    391               loc,
    392               "pointer operands to '%s' operator must "
    393               "be to compatible object types",
    394               tokenstr(op)
    395           );
    396       } else {
    397         error(loc, "invalid operands to '%s' operator", tokenstr(op));
    398       }
    399       break;
    400     case TBOR:
    401     case TXOR:
    402     case TBAND:
    403       t = commonreal(&l, &r);
    404       break;
    405     case TADD:
    406       if (iscomplexop(l, r)) {
    407         if (l->type->kind != TYPECOMPLEX || r->type->kind != TYPECOMPLEX || l->type != r->type)
    408           error(loc, "'+' between complex operands requires the same complex type on both sides");
    409         t = l->type;
    410         break;
    411       }
    412       if (lp & PROPARITH && rp & PROPARITH) {
    413         t = commonreal(&l, &r);
    414         break;
    415       }
    416       if (r->type->kind == TYPEPOINTER)
    417         e = l, l = r, r = e, rp = lp;
    418       if (l->type->kind != TYPEPOINTER || !(rp & PROPINT))
    419         error(loc, "invalid operands to '+' operator");
    420       t = l->type;
    421       if (t->base->incomplete || t->base->kind == TYPEFUNC)
    422         error(
    423             loc,
    424             "pointer operand to '+' must be to complete "
    425             "object type"
    426         );
    427       r = mkbinaryexpr(loc, TMUL, exprconvert(r, &typeulong), mksizeofexpr(t->base));
    428       break;
    429     case TSUB:
    430       if (iscomplexop(l, r)) {
    431         if (l->type->kind != TYPECOMPLEX || r->type->kind != TYPECOMPLEX || l->type != r->type)
    432           error(loc, "'-' between complex operands requires the same complex type on both sides");
    433         t = l->type;
    434         break;
    435       }
    436       if (lp & PROPARITH && rp & PROPARITH) {
    437         t = commonreal(&l, &r);
    438         break;
    439       }
    440       if (l->type->kind != TYPEPOINTER || (!(rp & PROPINT) && r->type->kind != TYPEPOINTER))
    441         error(loc, "invalid operands to '-' operator");
    442       if (l->type->base->incomplete || l->type->base->kind == TYPEFUNC)
    443         error(
    444             loc,
    445             "pointer operand to '-' must be to complete "
    446             "object type"
    447         );
    448       if (rp & PROPINT) {
    449         t = l->type;
    450         r = mkbinaryexpr(loc, TMUL, exprconvert(r, &typeulong), mksizeofexpr(t->base));
    451       } else {
    452         if (!typecompatible(l->type->base, r->type->base))
    453           error(
    454               &tok.loc,
    455               "pointer operands to '-' are "
    456               "to incompatible types"
    457           );
    458         op = TDIV;
    459         t  = &typelong;
    460         e  = mkbinaryexpr(loc, TSUB, exprconvert(l, &typelong), exprconvert(r, &typelong));
    461         r  = mksizeofexpr(l->type->base);
    462         l  = e;
    463       }
    464       break;
    465     case TMOD:
    466       if (!(lp & PROPINT) || !(rp & PROPINT))
    467         error(loc, "operands to '%%' operator must be integer");
    468       t = commonreal(&l, &r);
    469       break;
    470     case TMUL:
    471     case TDIV:
    472       if (iscomplexop(l, r)) {
    473         if (l->type->kind != TYPECOMPLEX || r->type->kind != TYPECOMPLEX || l->type != r->type)
    474           error(
    475               loc,
    476               "'%s' between complex operands requires the same complex type on both sides",
    477               tokenstr(op)
    478           );
    479         t = l->type;
    480         break;
    481       }
    482       if (!(lp & PROPARITH) || !(rp & PROPARITH))
    483         error(loc, "operands to '%s' operator must be arithmetic", tokenstr(op));
    484       t = commonreal(&l, &r);
    485       break;
    486     case TSHL:
    487     case TSHR:
    488       if (!(lp & PROPINT) || !(rp & PROPINT))
    489         error(loc, "operands to '%s' operator must be integer", tokenstr(op));
    490       l = exprpromote(l);
    491       r = exprpromote(r);
    492       t = l->type;
    493       break;
    494     default:
    495       fatal("internal error: unknown binary operator %d", op);
    496   }
    497   e             = mkexpr(EXPRBINARY, t, NULL);
    498   e->op         = op;
    499   e->u.binary.l = l;
    500   e->u.binary.r = r;
    501 
    502   return e;
    503 }
    504 
    505 static struct type *
    506 inttype(unsigned long long val, bool decimal, char *suffix)
    507 {
    508   enum {
    509     U  = 1,
    510     L  = 2,
    511     LL = 4,
    512     WB = 8,
    513   };
    514   static struct type *const types[] = {
    515       &typeint,
    516       &typeuint,   /* u */
    517       &typelong,   /* l */
    518       &typeulong,  /* ul */
    519       &typellong,  /* ll */
    520       &typeullong, /* ull */
    521   };
    522   int          flags, i, step;
    523   bool         sign;
    524   char        *s;
    525   struct type *t;
    526 
    527   /* parse suffix */
    528   flags = 0;
    529   for (s = suffix; *s; ++s) {
    530     int new;
    531 
    532     switch (*s) {
    533       case 'u':
    534       case 'U':
    535         new = U;
    536         break;
    537       case 'l':
    538       case 'L':
    539         new = s[1] == s[0] ? (++s, LL) : L;
    540         break;
    541       case 'w':
    542         if (s[1] != 'b')
    543           goto invalid;
    544         ++s, new = WB;
    545         break;
    546       case 'W':
    547         if (s[1] != 'B')
    548           goto invalid;
    549         ++s, new = WB;
    550         break;
    551       default:
    552       invalid:
    553         error(&tok.loc, "invalid integer constant suffix '%s'", suffix);
    554     }
    555     if (flags & new || (flags & ~U && new & ~U))
    556       goto invalid;
    557     flags |= new;
    558   }
    559 
    560   /* find integer type */
    561   sign = !(flags & U);
    562   if (flags & WB) {
    563     for (i = 1;; ++i) {
    564       if (i > 64 - sign)
    565         goto notype;
    566       if (val <= 0xffffffffffffffff >> (64 - i))
    567         break;
    568     }
    569     return mkbitinttype(i + sign, sign);
    570   }
    571   assert((size_t)flags < countof(types));
    572   step = !sign || decimal ? 2 : 1;
    573   for (i = flags; (size_t)i < countof(types); i += step) {
    574     t = types[i];
    575     if (typehasint(t, val, false))
    576       return t;
    577   }
    578 notype:
    579   error(&tok.loc, "no suitable type for constant '%s'", tok.lit);
    580 }
    581 
    582 static int
    583 octval(int c)
    584 {
    585   if ('0' <= c && c <= '7')
    586     return c - '0';
    587   return -1;
    588 }
    589 
    590 static int
    591 hexval(int c)
    592 {
    593   if ('0' <= c && c <= '9')
    594     return c - '0';
    595   if ('a' <= c && c <= 'f')
    596     return c - 'a' + 10;
    597   if ('A' <= c && c <= 'F')
    598     return c - 'A' + 10;
    599   return -1;
    600 }
    601 
    602 static size_t
    603 decodechar(
    604     const char *src, uint_least32_t *chr, bool *hexoct, const char *desc, struct location *loc
    605 )
    606 {
    607   uint_least32_t       c;
    608   size_t               n;
    609   int                  i, v;
    610   const unsigned char *s = (const unsigned char *)src;
    611 
    612   *hexoct = false;
    613   if (*s == '\\') {
    614     ++s;
    615     switch (*s) {
    616       case '\'':
    617       case '"':
    618       case '?':
    619       case '\\':
    620         c = *s;
    621         ++s;
    622         break;
    623       case 'a':
    624         c = '\a';
    625         ++s;
    626         break;
    627       case 'b':
    628         c = '\b';
    629         ++s;
    630         break;
    631       case 'f':
    632         c = '\f';
    633         ++s;
    634         break;
    635       case 'n':
    636         c = '\n';
    637         ++s;
    638         break;
    639       case 'r':
    640         c = '\r';
    641         ++s;
    642         break;
    643       case 't':
    644         c = '\t';
    645         ++s;
    646         break;
    647       case 'v':
    648         c = '\v';
    649         ++s;
    650         break;
    651       case 'u':
    652       case 'U':
    653         /* a universal character name names a code point, written as a
    654          * character of the literal's encoding would be */
    655         n = *s == 'u' ? 4 : 8;
    656         ++s;
    657         c = 0;
    658         while (n-- > 0) {
    659           v = hexval(*s++);
    660           assert(v >= 0);
    661           c = c * 16 + v;
    662         }
    663         if (c > 0x10ffff || (c >= 0xd800 && c <= 0xdfff))
    664           error(loc, "%s names %#lx, which is no character", desc, (unsigned long)c);
    665         break;
    666       case 'x':
    667         ++s;
    668         c = 0;
    669         v = hexval(*s);
    670         assert(v >= 0);
    671         do {
    672           if (c > 0xffffffff / 16)
    673             error(
    674                 &tok.loc,
    675                 "character constant escape is "
    676                 "out of range"
    677             );
    678           c = c * 16 + v;
    679           v = hexval(*++s);
    680         } while (v >= 0);
    681         *hexoct = true;
    682         break;
    683       default:
    684         c = 0;
    685         v = octval(*s);
    686         assert(v >= 0);
    687         i = 0;
    688         do {
    689           if (c > 0xffffffff / 8)
    690             error(
    691                 &tok.loc,
    692                 "character constant escape is "
    693                 "out of range"
    694             );
    695           c = c * 8 + v;
    696           v = octval(*++s);
    697         } while (v >= 0 && ++i < 3);
    698         *hexoct = true;
    699     }
    700   } else {
    701     n = utf8dec(&c, s, 4);
    702     if (n == (size_t)-1)
    703       error(loc, "%s contains invalid UTF-8", desc);
    704     s += n;
    705   }
    706   *chr = c;
    707   return s - (const unsigned char *)src;
    708 }
    709 
    710 static size_t
    711 encodechar8(void *dst, uint_least32_t chr, bool hexoct)
    712 {
    713   if (!hexoct)
    714     return utf8enc(dst, chr);
    715   *(unsigned char *)dst = chr;
    716   return 1;
    717 }
    718 
    719 static size_t
    720 encodechar16(void *dst, uint_least32_t chr, bool hexoct)
    721 {
    722   if (!hexoct)
    723     return utf16enc(dst, chr) * sizeof(uint_least16_t);
    724   *(uint_least16_t *)dst = chr;
    725   return sizeof(uint_least16_t);
    726 }
    727 
    728 static size_t
    729 encodechar32(void *dst, uint_least32_t chr, bool hexoct)
    730 {
    731   (void)hexoct;
    732   *(uint_least32_t *)dst = chr;
    733   return sizeof(uint_least32_t);
    734 }
    735 
    736 struct type *
    737 stringconcat(struct stringlit *str, bool forceutf8)
    738 {
    739   static struct array parts;
    740   struct {
    741     struct location loc;
    742     char           *str;
    743   }           *p;
    744   int          kind, newkind;
    745   struct type *t;
    746   size_t (*encodechar)(void *, uint_least32_t, bool);
    747   char          *src;
    748   unsigned char *buf, *dst;
    749   uint_least32_t chr;
    750   bool           hexoct;
    751   size_t         len, width;
    752 
    753   assert(tok.kind == TSTRINGLIT);
    754   parts.len = 0;
    755   len       = 0;
    756   kind      = 0;
    757   do {
    758     src = tok.lit;
    759     switch (*src) {
    760       case 'u':
    761         if (src[1] == '8')
    762           ++src; /* fallthrough */
    763       case 'L':
    764       case 'U':
    765         newkind = *src, ++src;
    766         break;
    767       case '"':
    768         newkind = 0;
    769         break;
    770       default:
    771         assert(0);
    772     }
    773     if (kind != newkind && kind && newkind)
    774       error(
    775           &tok.loc,
    776           "adjacent string literals have "
    777           "differing prefixes"
    778       );
    779     if (newkind)
    780       kind = newkind;
    781     p      = arrayadd(&parts, sizeof(*p));
    782     p->loc = tok.loc;
    783     p->str = src + 1;
    784     len += strlen(src) - 2;
    785     next();
    786   } while (tok.kind == TSTRINGLIT);
    787   if (forceutf8)
    788     kind = '8';
    789   ++len; /* null byte */
    790   switch (kind) {
    791     case 0:
    792       t = &typechar;
    793       break;
    794     case '8':
    795       t = &typeuchar;
    796       break;
    797     case 'u':
    798       t = &typeushort;
    799       break;
    800     case 'U':
    801       t = &typeuint;
    802       break;
    803     case 'L':
    804       t = targ->typewchar;
    805       break;
    806     default:
    807       assert(0);
    808   }
    809   switch (t->size) {
    810     case 1:
    811       width      = 1;
    812       encodechar = encodechar8;
    813       break;
    814     case 2:
    815       width      = sizeof(uint_least16_t);
    816       encodechar = encodechar16;
    817       break;
    818     case 4:
    819       width      = sizeof(uint_least32_t);
    820       encodechar = encodechar32;
    821       break;
    822     default:
    823       assert(0);
    824   }
    825   buf       = xreallocarray(NULL, len, width);
    826   str->data = buf;
    827   dst       = buf;
    828   arrayforeach(&parts, p)
    829   {
    830     src = p->str;
    831     while (*src != '"') {
    832       src += decodechar(src, &chr, &hexoct, "string literal", &p->loc);
    833       dst += encodechar(dst, chr, hexoct);
    834     }
    835   }
    836   dst += encodechar(dst, 0, false);
    837   str->size = (dst - buf) / width;
    838   return t;
    839 }
    840 
    841 static struct expr *
    842 generic(struct scope *s)
    843 {
    844   struct expr  *e, *match = NULL, *def = NULL;
    845   struct type  *t, *want;
    846   enum typequal qual;
    847 
    848   next();
    849   expect(TLPAREN, "after '_Generic'");
    850   e = assignexpr(s);
    851   expect(TCOMMA, "after generic selector expression");
    852   want = e->type;
    853   delexpr(e);
    854   do {
    855     if (consume(TDEFAULT)) {
    856       if (def)
    857         error(
    858             &tok.loc,
    859             "multiple default expressions "
    860             "in generic association list"
    861         );
    862       expect(TCOLON, "after 'default'");
    863       def = assignexpr(s);
    864     } else {
    865       qual = QUALNONE;
    866       t    = typename(s, &qual, NULL);
    867       if (!t)
    868         error(
    869             &tok.loc,
    870             "expected typename for generic "
    871             "association"
    872         );
    873       if (t->kind == TYPEFUNC)
    874         error(
    875             &tok.loc,
    876             "generic association must have "
    877             "object type"
    878         );
    879       if (t->incomplete)
    880         error(
    881             &tok.loc,
    882             "generic association must have "
    883             "complete type"
    884         );
    885       if (t->prop & PROPVM)
    886         error(
    887             &tok.loc,
    888             "generic association has "
    889             "variably modified type"
    890         );
    891       expect(TCOLON, "after type name");
    892       e = assignexpr(s);
    893       if (typecompatible(t, want) && qual == QUALNONE) {
    894         if (match)
    895           error(
    896               &tok.loc,
    897               "generic selector matches "
    898               "multiple associations"
    899           );
    900         match = e;
    901       } else {
    902         delexpr(e);
    903       }
    904     }
    905   } while (consume(TCOMMA));
    906   expect(TRPAREN, "after generic assocation list");
    907   if (!match) {
    908     if (!def)
    909       error(
    910           &tok.loc,
    911           "generic selector matches no associations and no "
    912           "default was specified"
    913       );
    914     match = def;
    915   } else if (def) {
    916     delexpr(def);
    917   }
    918   return match;
    919 }
    920 
    921 /* 6.5 expressions */
    922 static struct expr *
    923 primaryexpr(struct scope *s)
    924 {
    925   struct expr       *e;
    926   struct decl       *d;
    927   struct type       *t;
    928   char              *src, *end;
    929   uint_least32_t     chr;
    930   unsigned long long val;
    931   bool               hexoct, ordinary;
    932   int                base;
    933 
    934   switch (tok.kind) {
    935     case TSTRINGLIT:
    936       e         = mkexpr(EXPRSTRING, NULL, NULL);
    937       t         = stringconcat(&e->u.string, false);
    938       e->type   = mkarraytype(t, QUALNONE, e->u.string.size);
    939       e->lvalue = true;
    940       e         = decay(e);
    941       break;
    942     case TCHARCONST:
    943       src      = tok.lit;
    944       ordinary = false;
    945       switch (*src) {
    946         case 'L':
    947           ++src;
    948           t = targ->typewchar;
    949           break;
    950         case 'u':
    951           ++src;
    952           t = *src == '8' ? ++src, &typeuchar : &typeushort;
    953           break;
    954         case 'U':
    955           ++src;
    956           t = &typeuint;
    957           break;
    958         default:
    959           t = &typeuchar, ordinary = true;
    960       }
    961       assert(*src == '\'');
    962       ++src;
    963       src += decodechar(src, &chr, &hexoct, "character constant", &tok.loc);
    964       if (hexoct && !typehasint(t, chr, false))
    965         error(&tok.loc, "character constant escape is out of range");
    966       val = chr;
    967       if (ordinary) {
    968         if (typechar.u.arith.issigned)
    969           val = (val ^ 0x80) - 0x80;
    970         t = &typeint;
    971       }
    972       e = mkconstexpr(t, val);
    973       if (*src != '\'')
    974         error(
    975             &tok.loc,
    976             "character constant contains more than one "
    977             "character: %c",
    978             *src
    979         );
    980       next();
    981       break;
    982     case TNUMBER:
    983       e = mkexpr(EXPRCONST, NULL, NULL);
    984       if (tok.lit[0] == '0') {
    985         switch (tok.lit[1]) {
    986           case 'x':
    987           case 'X':
    988             base = 16;
    989             break;
    990           case 'b':
    991           case 'B':
    992             base = 2;
    993             break;
    994           default:
    995             base = 8;
    996             break;
    997         }
    998       } else {
    999         base = 10;
   1000       }
   1001       if (strpbrk(tok.lit, base == 16 ? ".pP" : ".eE")) {
   1002         /* floating constant */
   1003         e->u.constant.f = strtod(tok.lit, &end);
   1004         if (end == tok.lit)
   1005           error(&tok.loc, "invalid floating constant '%s'", tok.lit);
   1006         if (!end[0])
   1007           e->type = &typedouble;
   1008         else if ((end[0] == 'f' || end[0] == 'F') && !end[1])
   1009           e->type = &typefloat;
   1010         else if ((end[0] == 'l' || end[0] == 'L') && !end[1])
   1011           e->type = &typeldouble;
   1012         else
   1013           error(&tok.loc, "invalid floating constant suffix '%s'", end);
   1014       } else {
   1015         src = tok.lit;
   1016         if (base == 2)
   1017           src += 2;
   1018         /* integer constant */
   1019         e->u.constant.u = strtoull(src, &end, base);
   1020         if (end == src)
   1021           error(&tok.loc, "invalid integer constant '%s'", tok.lit);
   1022         e->type = inttype(e->u.constant.u, base == 10, end);
   1023       }
   1024       next();
   1025       break;
   1026     case TTRUE:
   1027     case TFALSE:
   1028       e               = mkexpr(EXPRCONST, &typebool, NULL);
   1029       e->u.constant.u = tok.kind == TTRUE;
   1030       next();
   1031       break;
   1032     case TNULLPTR:
   1033       e               = mkexpr(EXPRCONST, &typenullptr, NULL);
   1034       e->u.constant.u = 0;
   1035       next();
   1036       break;
   1037     case TLPAREN:
   1038       next();
   1039       e = expr(s);
   1040       expect(TRPAREN, "after expression");
   1041       break;
   1042     case T_GENERIC:
   1043       e = generic(s);
   1044       break;
   1045     default:
   1046       if (tok.kind >= TIDENT) {
   1047         d = scopegetdecl(s, tokenstr(tok.kind), 1);
   1048         if (!d)
   1049           error(&tok.loc, "undeclared identifier: %s", tokenstr(tok.kind));
   1050         e               = mkexpr(EXPRIDENT, d->type, NULL);
   1051         e->qual         = d->qual;
   1052         e->lvalue       = d->kind == DECLOBJECT;
   1053         e->u.ident.decl = d;
   1054         if (d->kind != DECLBUILTIN)
   1055           e = decay(e);
   1056         next();
   1057         break;
   1058       }
   1059       error(&tok.loc, "expected primary expression");
   1060   }
   1061 
   1062   return e;
   1063 }
   1064 
   1065 /* TODO: merge with init.c:designator() */
   1066 static void
   1067 designator(struct scope *s, struct type *t, unsigned long long *offset)
   1068 {
   1069   char              *name;
   1070   struct member     *m;
   1071   unsigned long long i;
   1072 
   1073   for (;;) {
   1074     switch (tok.kind) {
   1075       case TLBRACK:
   1076         if (t->kind != TYPEARRAY)
   1077           error(
   1078               &tok.loc,
   1079               "index designator is only "
   1080               "valid for array types"
   1081           );
   1082         next();
   1083         i = intconstexpr(s, false);
   1084         expect(TRBRACK, "for index designator");
   1085         t = t->base;
   1086         *offset += i * t->size;
   1087         break;
   1088       case TPERIOD:
   1089         if (t->kind != TYPESTRUCT && t->kind != TYPEUNION)
   1090           error(
   1091               &tok.loc,
   1092               "member designator only valid "
   1093               "for struct/union types"
   1094           );
   1095         next();
   1096         name = expect(TIDENT, "for member designator");
   1097         m    = typemember(t, name, offset);
   1098         if (!m)
   1099           error(
   1100               &tok.loc,
   1101               "%s has no member named '%s'",
   1102               t->kind == TYPEUNION ? "union" : "struct",
   1103               name
   1104           );
   1105         t = m->type;
   1106         break;
   1107       default:
   1108         return;
   1109     }
   1110   }
   1111 }
   1112 
   1113 /* every atomic builtin's first argument is a pointer whose pointee
   1114  * type drives the rest of the call, checked the same way regardless
   1115  * of which builtin is asking */
   1116 static struct expr *
   1117 atomicptrarg(struct scope *s, const char *msg)
   1118 {
   1119   struct expr *ptr = assignexpr(s);
   1120   if (ptr->type->kind != TYPEPOINTER)
   1121     error(&tok.loc, "%s must be a pointer", msg);
   1122   expect(TCOMMA, "after pointer argument");
   1123   return ptr;
   1124 }
   1125 
   1126 static struct expr *
   1127 builtinfunc(struct scope *s, enum builtinkind kind)
   1128 {
   1129   struct expr       *e, *toeval;
   1130   struct expr       *ptr, *val, *val2;
   1131   struct type       *t, *pointee;
   1132   struct member     *m;
   1133   char              *name;
   1134   unsigned long long offset;
   1135 
   1136   switch (kind) {
   1137     case BUILTINATOMICLOAD:
   1138       ptr     = atomicptrarg(s, "first argument to __atomic_load_n");
   1139       pointee = ptr->type->base;
   1140       delexpr(assignexpr(s)); /* memorder: every access is treated as seq_cst */
   1141       e                 = mkexpr(EXPRBUILTIN, pointee, ptr);
   1142       e->u.builtin.kind = BUILTINATOMICLOAD;
   1143       break;
   1144     case BUILTINATOMICSTORE:
   1145       ptr     = atomicptrarg(s, "first argument to __atomic_store_n");
   1146       pointee = ptr->type->base;
   1147       val     = exprassign(assignexpr(s), pointee);
   1148       expect(TCOMMA, "after value argument");
   1149       delexpr(assignexpr(s)); /* memorder */
   1150       e                 = mkexpr(EXPRBUILTIN, &typevoid, ptr);
   1151       e->u.builtin.kind = BUILTINATOMICSTORE;
   1152       e->u.builtin.val  = val;
   1153       break;
   1154     case BUILTINATOMICEXCHANGE:
   1155       ptr     = atomicptrarg(s, "first argument to __atomic_exchange_n");
   1156       pointee = ptr->type->base;
   1157       val     = exprassign(assignexpr(s), pointee);
   1158       expect(TCOMMA, "after value argument");
   1159       delexpr(assignexpr(s)); /* memorder */
   1160       e                 = mkexpr(EXPRBUILTIN, pointee, ptr);
   1161       e->u.builtin.kind = BUILTINATOMICEXCHANGE;
   1162       e->u.builtin.val  = val;
   1163       break;
   1164     case BUILTINATOMICCOMPAREEXCHANGE:
   1165       ptr     = atomicptrarg(s, "first argument to __atomic_compare_exchange_n");
   1166       pointee = ptr->type->base;
   1167       val2    = atomicptrarg(s, "second argument to __atomic_compare_exchange_n"); /* &expected */
   1168       val     = exprassign(assignexpr(s), pointee);                                /* desired */
   1169       expect(TCOMMA, "after desired-value argument");
   1170       delexpr(assignexpr(s)); /* weak */
   1171       expect(TCOMMA, "after weak argument");
   1172       delexpr(assignexpr(s)); /* success memorder */
   1173       expect(TCOMMA, "after success memorder argument");
   1174       delexpr(assignexpr(s)); /* failure memorder */
   1175       e                 = mkexpr(EXPRBUILTIN, &typebool, ptr);
   1176       e->u.builtin.kind = BUILTINATOMICCOMPAREEXCHANGE;
   1177       e->u.builtin.val  = val;
   1178       e->u.builtin.val2 = val2;
   1179       break;
   1180     case BUILTINATOMICFETCHADD:
   1181     case BUILTINATOMICFETCHSUB:
   1182     case BUILTINATOMICFETCHAND:
   1183     case BUILTINATOMICFETCHOR:
   1184     case BUILTINATOMICFETCHXOR:
   1185       ptr     = atomicptrarg(s, "first argument to atomic fetch builtin");
   1186       pointee = ptr->type->base;
   1187       val     = exprassign(assignexpr(s), pointee);
   1188       expect(TCOMMA, "after value argument");
   1189       delexpr(assignexpr(s)); /* memorder */
   1190       e                 = mkexpr(EXPRBUILTIN, pointee, ptr);
   1191       e->u.builtin.kind = kind;
   1192       e->u.builtin.val  = val;
   1193       break;
   1194     case BUILTINALLOCA:
   1195       e                 = exprassign(assignexpr(s), &typeulong);
   1196       e                 = mkexpr(EXPRBUILTIN, mkpointertype(&typevoid, QUALNONE), e);
   1197       e->u.builtin.kind = BUILTINALLOCA;
   1198       break;
   1199     case BUILTINCONSTANTP:
   1200       e = mkconstexpr(&typeint, eval(condexpr(s))->kind == EXPRCONST);
   1201       break;
   1202     case BUILTINEXPECT:
   1203       /* just a no-op for now */
   1204       /* TODO: check that the expression and the expected value have
   1205        * type 'long' */
   1206       e = assignexpr(s);
   1207       expect(TCOMMA, "after expression");
   1208       delexpr(assignexpr(s));
   1209       break;
   1210     case BUILTININFF:
   1211       e = mkexpr(EXPRCONST, &typefloat, NULL);
   1212       /* TODO: use INFINITY here when we can handle musl's math.h */
   1213       e->u.constant.f = strtod("inf", NULL);
   1214       break;
   1215     case BUILTINNANF:
   1216       e = assignexpr(s);
   1217       if (!e->decayed || e->base->kind != EXPRSTRING || e->base->u.string.size > 1)
   1218         error(
   1219             &tok.loc,
   1220             "__builtin_nanf currently only "
   1221             "supports empty string literals"
   1222         );
   1223       e = mkexpr(EXPRCONST, &typefloat, NULL);
   1224       /* TODO: use NAN here when we can handle musl's math.h */
   1225       e->u.constant.f = strtod("nan", NULL);
   1226       break;
   1227     case BUILTINOFFSETOF:
   1228       t = typename(s, NULL, NULL);
   1229       expect(TCOMMA, "after type name");
   1230       name = expect(TIDENT, "after ','");
   1231       if (t->kind != TYPESTRUCT && t->kind != TYPEUNION)
   1232         error(&tok.loc, "type is not a struct/union type");
   1233       offset = 0;
   1234       m      = typemember(t, name, &offset);
   1235       if (!m)
   1236         error(&tok.loc, "struct/union has no member named '%s'", name);
   1237       designator(s, m->type, &offset);
   1238       e = mkconstexpr(&typeulong, offset);
   1239       break;
   1240     case BUILTINTYPESCOMPATIBLEP:
   1241       t = typename(s, NULL, NULL);
   1242       expect(TCOMMA, "after type name");
   1243       e = mkconstexpr(&typeint, typecompatible(t, typename(s, NULL, NULL)));
   1244       break;
   1245     case BUILTINUNREACHABLE:
   1246       e                 = mkexpr(EXPRBUILTIN, &typevoid, NULL);
   1247       e->u.builtin.kind = BUILTINUNREACHABLE;
   1248       break;
   1249     case BUILTINVAARG:
   1250       e                 = mkexpr(EXPRBUILTIN, NULL, assignexpr(s));
   1251       e->u.builtin.kind = BUILTINVAARG;
   1252       if (!typesame(e->base->type, typeadjvalist))
   1253         error(&tok.loc, "va_arg argument must have type va_list");
   1254       if (typeadjvalist == targ->typevalist)
   1255         e->base = mkunaryexpr(TBAND, e->base);
   1256       expect(TCOMMA, "after va_list");
   1257       e->type   = typename(s, &e->qual, &toeval);
   1258       e->toeval = toeval;
   1259       break;
   1260     case BUILTINVACOPY:
   1261       e             = mkexpr(EXPRASSIGN, &typevoid, NULL);
   1262       e->u.assign.l = assignexpr(s);
   1263       if (!typesame(e->u.assign.l->type, typeadjvalist))
   1264         error(&tok.loc, "va_copy destination must have type va_list");
   1265       if (typeadjvalist != targ->typevalist)
   1266         e->u.assign.l = mkunaryexpr(TMUL, e->u.assign.l);
   1267       expect(TCOMMA, "after target va_list");
   1268       e->u.assign.r = assignexpr(s);
   1269       if (!typesame(e->u.assign.r->type, typeadjvalist))
   1270         error(&tok.loc, "va_copy source must have type va_list");
   1271       if (typeadjvalist != targ->typevalist)
   1272         e->u.assign.r = mkunaryexpr(TMUL, e->u.assign.r);
   1273       break;
   1274     case BUILTINVAEND:
   1275       e = assignexpr(s);
   1276       if (!typesame(e->type, typeadjvalist))
   1277         error(&tok.loc, "va_end argument must have type va_list");
   1278       e = mkexpr(EXPRCAST, &typevoid, e);
   1279       break;
   1280     case BUILTINVASTART:
   1281       e                 = mkexpr(EXPRBUILTIN, &typevoid, assignexpr(s));
   1282       e->u.builtin.kind = BUILTINVASTART;
   1283       if (!typesame(e->base->type, typeadjvalist))
   1284         error(&tok.loc, "va_start argument must have type va_list");
   1285       if (typeadjvalist == targ->typevalist)
   1286         e->base = mkunaryexpr(TBAND, e->base);
   1287       if (consume(TCOMMA))
   1288         delexpr(assignexpr(s));
   1289       break;
   1290     default:
   1291       fatal("internal error; unknown builtin");
   1292   }
   1293   return e;
   1294 }
   1295 
   1296 static struct expr *
   1297 mkincdecexpr(enum tokenkind op, struct expr *base, bool post)
   1298 {
   1299   struct expr *e;
   1300 
   1301   if (!base->lvalue)
   1302     error(&tok.loc, "operand of '%s' operator must be an lvalue", tokenstr(op));
   1303   if (base->qual & QUALCONST)
   1304     error(&tok.loc, "operand of '%s' operator is const qualified", tokenstr(op));
   1305   e                = mkexpr(EXPRINCDEC, base->type, base);
   1306   e->op            = op;
   1307   e->u.incdec.post = post;
   1308   return e;
   1309 }
   1310 
   1311 static struct expr *
   1312 postfixexpr(struct scope *s, struct expr *r)
   1313 {
   1314   struct expr       *e, *arr, *idx, *tmp, **end;
   1315   struct type       *t;
   1316   struct decl       *p;
   1317   struct member     *m;
   1318   unsigned long long offset;
   1319   enum typequal      tq;
   1320   enum tokenkind     op;
   1321   bool               lvalue;
   1322 
   1323   if (!r)
   1324     r = primaryexpr(s);
   1325   for (;;) {
   1326     switch (tok.kind) {
   1327       case TLBRACK: /* subscript */
   1328         next();
   1329         arr = r;
   1330         idx = expr(s);
   1331         if (arr->type->kind != TYPEPOINTER) {
   1332           if (idx->type->kind != TYPEPOINTER)
   1333             error(
   1334                 &tok.loc,
   1335                 "either array or index "
   1336                 "must be pointer type"
   1337             );
   1338           tmp = arr;
   1339           arr = idx;
   1340           idx = tmp;
   1341         }
   1342         if (arr->type->base->incomplete)
   1343           error(&tok.loc, "array is pointer to incomplete type");
   1344         if (!(idx->type->prop & PROPINT))
   1345           error(&tok.loc, "index is not an integer type");
   1346         e = mkunaryexpr(TMUL, mkbinaryexpr(&tok.loc, TADD, arr, idx));
   1347         expect(TRBRACK, "after array index");
   1348         break;
   1349       case TLPAREN: /* function call */
   1350         next();
   1351         if (r->kind == EXPRIDENT && r->u.ident.decl->kind == DECLBUILTIN) {
   1352           e = builtinfunc(s, r->u.ident.decl->u.builtin);
   1353           expect(TRPAREN, "after builtin parameters");
   1354           break;
   1355         }
   1356         if (r->type->kind != TYPEPOINTER || r->type->base->kind != TYPEFUNC)
   1357           error(&tok.loc, "called object is not a function");
   1358         t               = r->type->base;
   1359         e               = mkexpr(EXPRCALL, t->base, r);
   1360         e->u.call.args  = NULL;
   1361         e->u.call.nargs = 0;
   1362         p               = t->u.func.params;
   1363         end             = &e->u.call.args;
   1364         while (tok.kind != TRPAREN) {
   1365           if (e->u.call.args)
   1366             expect(
   1367                 TCOMMA,
   1368                 "or ')' after function "
   1369                 "call argument"
   1370             );
   1371           if (!p && !t->u.func.isvararg)
   1372             error(
   1373                 &tok.loc,
   1374                 "too many arguments "
   1375                 "for function call"
   1376             );
   1377           *end = assignexpr(s);
   1378           if (t->u.func.isvararg && !p)
   1379             *end = exprpromote(*end);
   1380           else
   1381             *end = exprassign(*end, p->type);
   1382           end = &(*end)->next;
   1383           ++e->u.call.nargs;
   1384           if (p)
   1385             p = p->next;
   1386         }
   1387         if (p && !t->u.func.isvararg)
   1388           error(&tok.loc, "not enough arguments for function call");
   1389         e = decay(e);
   1390         next();
   1391         break;
   1392       case TPERIOD:
   1393         r = mkunaryexpr(TBAND, r);
   1394         /* fallthrough */
   1395       case TARROW:
   1396         op = tok.kind;
   1397         if (r->type->kind != TYPEPOINTER)
   1398           error(
   1399               &tok.loc,
   1400               "'%s' operator must be applied to "
   1401               "pointer to struct/union",
   1402               tokenstr(op)
   1403           );
   1404         t  = r->type->base;
   1405         tq = r->type->qual;
   1406         if (t->kind != TYPESTRUCT && t->kind != TYPEUNION)
   1407           error(
   1408               &tok.loc,
   1409               "'%s' operator must be applied to "
   1410               "pointer to struct/union",
   1411               tokenstr(op)
   1412           );
   1413         next();
   1414         if (tok.kind < TIDENT)
   1415           error(&tok.loc, "expected identifier after '%s' operator", tokenstr(op));
   1416         lvalue = op == TARROW || r->base->lvalue;
   1417         offset = 0;
   1418         m      = typemember(t, tokenstr(tok.kind), &offset);
   1419         if (!m)
   1420           error(&tok.loc, "struct/union has no member named '%s'", tok.lit);
   1421         r = mkbinaryexpr(
   1422             &tok.loc, TADD, exprconvert(r, &typeulong), mkconstexpr(&typeulong, offset)
   1423         );
   1424         r->type   = mkpointertype(m->type, tq | m->qual);
   1425         r         = mkunaryexpr(TMUL, r);
   1426         r->lvalue = lvalue;
   1427         if (m->bits.before || m->bits.after) {
   1428           e                  = mkexpr(EXPRBITFIELD, r->type, r);
   1429           e->lvalue          = lvalue;
   1430           e->u.bitfield.bits = m->bits;
   1431         } else {
   1432           e = r;
   1433         }
   1434         next();
   1435         break;
   1436       case TINC:
   1437       case TDEC:
   1438         e = mkincdecexpr(tok.kind, r, true);
   1439         next();
   1440         break;
   1441       default:
   1442         return r;
   1443     }
   1444     r = e;
   1445   }
   1446 }
   1447 
   1448 static struct expr *castexpr(struct scope *);
   1449 
   1450 static struct expr *
   1451 unaryexpr(struct scope *s)
   1452 {
   1453   enum tokenkind op;
   1454   struct expr   *e, *l;
   1455   struct type   *t;
   1456 
   1457   op = tok.kind;
   1458   switch (op) {
   1459     case TINC:
   1460     case TDEC:
   1461       next();
   1462       l = unaryexpr(s);
   1463       e = mkincdecexpr(op, l, false);
   1464       break;
   1465     case TBAND:
   1466     case TMUL:
   1467       next();
   1468       return mkunaryexpr(op, castexpr(s));
   1469     case TADD:
   1470       next();
   1471       e = castexpr(s);
   1472       if (e->type->kind != TYPECOMPLEX) {
   1473         if (!(e->type->prop & PROPARITH))
   1474           error(
   1475               &tok.loc,
   1476               "operand of unary '+' operator must "
   1477               "have arithmetic type"
   1478           );
   1479         if (e->type->prop & PROPINT)
   1480           e = exprpromote(e);
   1481       }
   1482       break;
   1483     case TSUB:
   1484       next();
   1485       e = castexpr(s);
   1486       if (e->type->kind != TYPECOMPLEX) {
   1487         if (!(e->type->prop & PROPARITH))
   1488           error(
   1489               &tok.loc,
   1490               "operand of unary '-' operator must "
   1491               "have arithmetic type"
   1492           );
   1493         if (e->type->prop & PROPINT)
   1494           e = exprpromote(e);
   1495       }
   1496       e     = mkexpr(EXPRUNARY, e->type, e);
   1497       e->op = op;
   1498       break;
   1499     case TBNOT:
   1500       next();
   1501       e = castexpr(s);
   1502       if (!(e->type->prop & PROPINT))
   1503         error(&tok.loc, "operand of '~' operator must have integer type");
   1504       e = exprpromote(e);
   1505       e = mkbinaryexpr(&tok.loc, TXOR, e, mkconstexpr(e->type, -1));
   1506       break;
   1507     case TLNOT:
   1508       next();
   1509       e = castexpr(s);
   1510       if (!(e->type->prop & PROPSCALAR))
   1511         error(&tok.loc, "operator '!' must have scalar operand");
   1512       e = mkbinaryexpr(&tok.loc, TEQL, e, mkconstexpr(&typeint, 0));
   1513       break;
   1514     case TSIZEOF:
   1515     case TALIGNOF:
   1516     case T__ALIGNOF__:
   1517       next();
   1518       if (consume(TLPAREN)) {
   1519         t = typename(s, NULL, NULL);
   1520         if (t) {
   1521           expect(TRPAREN, "after type name");
   1522           /* might be part of a compound literal */
   1523           if (op == TSIZEOF && tok.kind == TLBRACE)
   1524             parseinit(s, t);
   1525           e = NULL;
   1526         } else {
   1527           e = expr(s);
   1528           expect(TRPAREN, "after expression");
   1529           if (op == TSIZEOF)
   1530             e = postfixexpr(s, e);
   1531         }
   1532       } else if (op == TSIZEOF) {
   1533         t = NULL;
   1534         e = unaryexpr(s);
   1535       } else {
   1536         error(&tok.loc, "expected ')' after 'alignof'");
   1537       }
   1538       if (!t) {
   1539         if (e->decayed)
   1540           e = e->base;
   1541         if (e->kind == EXPRBITFIELD)
   1542           error(
   1543               &tok.loc,
   1544               "%s operator applied to bitfield "
   1545               "expression",
   1546               tokenstr(op)
   1547           );
   1548         t = e->type;
   1549       }
   1550       if (t->incomplete)
   1551         error(&tok.loc, "%s operator applied to incomplete type", tokenstr(op));
   1552       if (t->kind == TYPEFUNC)
   1553         error(&tok.loc, "%s operator applied to function type", tokenstr(op));
   1554       if (t->kind == TYPEARRAY && t->size == 0 && op == TSIZEOF) {
   1555         e              = mkexpr(EXPRSIZEOF, &typeulong, e);
   1556         e->u.szof.type = t;
   1557       } else {
   1558         /*
   1559          * XXX: __alignof__ is not the same as alignof on 32-bit
   1560          * archs this needs to be considered if we gain support for
   1561          * those archs
   1562          */
   1563         e = mkconstexpr(&typeulong, op == TSIZEOF ? t->size : t->align);
   1564       }
   1565       break;
   1566     default:
   1567       e = postfixexpr(s, NULL);
   1568   }
   1569 
   1570   return e;
   1571 }
   1572 
   1573 static struct expr *
   1574 castexpr(struct scope *s)
   1575 {
   1576   struct type  *t, *ct;
   1577   struct decl  *d;
   1578   enum typequal tq;
   1579   struct expr  *r, *e, **end, *toeval;
   1580 
   1581   ct  = NULL;
   1582   end = &r;
   1583   while (consume(TLPAREN)) {
   1584     tq = QUALNONE;
   1585     t  = typename(s, &tq, &toeval);
   1586     if (!t) {
   1587       e = expr(s);
   1588       expect(TRPAREN, "after expression to match '('");
   1589       e = postfixexpr(s, e);
   1590       goto done;
   1591     }
   1592     expect(TRPAREN, "after type name");
   1593     if (tok.kind == TLBRACE) {
   1594       e                  = mkexpr(EXPRCOMPOUND, t, NULL);
   1595       e->toeval          = toeval;
   1596       e->qual            = tq;
   1597       e->lvalue          = true;
   1598       d                  = mkdecl(NULL, DECLOBJECT, t, tq, LINKNONE);
   1599       d->u.obj.storage   = s == &filescope ? SDSTATIC : SDAUTO;
   1600       e->u.compound.decl = d;
   1601       e->u.compound.init = parseinit(s, t);
   1602       e                  = postfixexpr(s, decay(e));
   1603       goto done;
   1604     }
   1605     if (t != &typevoid && t->kind != TYPECOMPLEX && !(t->prop & PROPSCALAR))
   1606       error(&tok.loc, "cast type must be scalar");
   1607     e         = mkexpr(EXPRCAST, t, NULL);
   1608     e->toeval = toeval;
   1609     *end      = e;
   1610     end       = &e->base;
   1611     ct        = t;
   1612   }
   1613   e = unaryexpr(s);
   1614 
   1615 done:
   1616   if (ct && ct != &typevoid && e->type->kind != TYPECOMPLEX && !(e->type->prop & PROPSCALAR))
   1617     error(&tok.loc, "cast operand must have scalar type");
   1618   *end = e;
   1619   return r;
   1620 }
   1621 
   1622 static int
   1623 precedence(enum tokenkind t)
   1624 {
   1625   switch (t) {
   1626     case TLOR:
   1627       return 0;
   1628     case TLAND:
   1629       return 1;
   1630     case TBOR:
   1631       return 2;
   1632     case TXOR:
   1633       return 3;
   1634     case TBAND:
   1635       return 4;
   1636     case TEQL:
   1637     case TNEQ:
   1638       return 5;
   1639     case TLESS:
   1640     case TGREATER:
   1641     case TLEQ:
   1642     case TGEQ:
   1643       return 6;
   1644     case TSHL:
   1645     case TSHR:
   1646       return 7;
   1647     case TADD:
   1648     case TSUB:
   1649       return 8;
   1650     case TMUL:
   1651     case TDIV:
   1652     case TMOD:
   1653       return 9;
   1654     default:
   1655       break;
   1656   }
   1657   return -1;
   1658 }
   1659 
   1660 static struct expr *
   1661 binaryexpr(struct scope *s, struct expr *l, int i)
   1662 {
   1663   struct expr    *r;
   1664   struct location loc;
   1665   enum tokenkind  op;
   1666   int             j, k;
   1667 
   1668   if (!l)
   1669     l = castexpr(s);
   1670   while ((j = precedence(tok.kind)) >= i) {
   1671     op  = tok.kind;
   1672     loc = tok.loc;
   1673     next();
   1674     r = castexpr(s);
   1675     while ((k = precedence(tok.kind)) > j)
   1676       r = binaryexpr(s, r, k);
   1677     l = mkbinaryexpr(&loc, op, l, r);
   1678   }
   1679   return l;
   1680 }
   1681 
   1682 struct expr *
   1683 condexpr(struct scope *s)
   1684 {
   1685   struct expr  *e, *l, *r;
   1686   struct type  *t, *lt, *rt;
   1687   enum typequal tq;
   1688 
   1689   e = binaryexpr(s, NULL, 0);
   1690   if (!consume(TQUESTION))
   1691     return e;
   1692   l = tok.kind == TCOLON ? e : expr(s);
   1693   expect(TCOLON, "in conditional expression");
   1694   r = condexpr(s);
   1695 
   1696   lt = l->type;
   1697   rt = r->type;
   1698   if (lt == rt) {
   1699     t = lt;
   1700   } else if (lt->prop & PROPARITH && rt->prop & PROPARITH) {
   1701     t = typecommonreal(lt, bitfieldwidth(l), rt, bitfieldwidth(r));
   1702   } else if (lt == &typevoid && rt == &typevoid) {
   1703     t = &typevoid;
   1704   } else {
   1705     l = eval(l);
   1706     r = eval(r);
   1707     if (nullpointer(l) && rt->kind == TYPEPOINTER) {
   1708       t = rt;
   1709     } else if (nullpointer(r) && lt->kind == TYPEPOINTER) {
   1710       t = lt;
   1711     } else if (lt->kind == TYPEPOINTER && rt->kind == TYPEPOINTER) {
   1712       tq = lt->qual | rt->qual;
   1713       lt = lt->base;
   1714       rt = rt->base;
   1715       if (lt == &typevoid || rt == &typevoid) {
   1716         t = &typevoid;
   1717       } else if (typecompatible(lt, rt)) {
   1718         t = typecomposite(lt, rt);
   1719       } else {
   1720         error(
   1721             &tok.loc,
   1722             "operands of conditional operator must "
   1723             "have compatible types"
   1724         );
   1725       }
   1726       t = mkpointertype(t, tq);
   1727     } else {
   1728       error(&tok.loc, "invalid operands to conditional operator");
   1729     }
   1730   }
   1731   e = eval(e);
   1732   if (e->kind == EXPRCONST && e->type->prop & PROPINT)
   1733     return exprconvert(e->u.constant.u ? l : r, t);
   1734   e           = mkexpr(EXPRCOND, t, e);
   1735   e->u.cond.t = l;
   1736   e->u.cond.f = r;
   1737   return e;
   1738 }
   1739 
   1740 unsigned long long
   1741 intconstexpr(struct scope *s, bool allowneg)
   1742 {
   1743   struct expr *e;
   1744 
   1745   e = eval(condexpr(s));
   1746   if (e->kind != EXPRCONST || !(e->type->prop & PROPINT))
   1747     error(&tok.loc, "not an integer constant expression");
   1748   if (!allowneg && e->type->u.arith.issigned && e->u.constant.u >> 63)
   1749     error(&tok.loc, "integer constant expression cannot be negative");
   1750   return e->u.constant.u;
   1751 }
   1752 
   1753 static struct expr *
   1754 mkassignexpr(struct expr *l, struct expr *r)
   1755 {
   1756   struct expr *e;
   1757 
   1758   e             = mkexpr(EXPRASSIGN, l->type, NULL);
   1759   e->u.assign.l = l;
   1760   e->u.assign.r = exprassign(r, l->type);
   1761   return e;
   1762 }
   1763 
   1764 struct expr *
   1765 assignexpr(struct scope *s)
   1766 {
   1767   struct expr   *e, *l, *r, *tmp, *bit;
   1768   enum tokenkind op;
   1769 
   1770   l = condexpr(s);
   1771   if (l->kind == EXPRBINARY || l->kind == EXPRCOMMA || l->kind == EXPRCAST)
   1772     return l;
   1773   switch (tok.kind) {
   1774     case TASSIGN:
   1775       op = TNONE;
   1776       break;
   1777     case TMULASSIGN:
   1778       op = TMUL;
   1779       break;
   1780     case TDIVASSIGN:
   1781       op = TDIV;
   1782       break;
   1783     case TMODASSIGN:
   1784       op = TMOD;
   1785       break;
   1786     case TADDASSIGN:
   1787       op = TADD;
   1788       break;
   1789     case TSUBASSIGN:
   1790       op = TSUB;
   1791       break;
   1792     case TSHLASSIGN:
   1793       op = TSHL;
   1794       break;
   1795     case TSHRASSIGN:
   1796       op = TSHR;
   1797       break;
   1798     case TBANDASSIGN:
   1799       op = TBAND;
   1800       break;
   1801     case TXORASSIGN:
   1802       op = TXOR;
   1803       break;
   1804     case TBORASSIGN:
   1805       op = TBOR;
   1806       break;
   1807     default:
   1808       return l;
   1809   }
   1810   if (!l->lvalue)
   1811     error(&tok.loc, "left side of assignment expression is not an lvalue");
   1812   next();
   1813   r = assignexpr(s);
   1814   if (!op)
   1815     return mkassignexpr(l, r);
   1816   /* rewrite `E1 OP= E2` as `t = &E1, *t = *t OP E2`, where t is a
   1817    * temporary slot */
   1818   if (l->kind == EXPRBITFIELD) {
   1819     bit = l;
   1820     l   = l->base;
   1821   } else {
   1822     bit = NULL;
   1823   }
   1824   tmp         = mkexpr(EXPRTEMP, mkpointertype(l->type, l->qual), NULL);
   1825   tmp->lvalue = true;
   1826   tmp->u.temp = NULL;
   1827   e           = mkassignexpr(tmp, mkunaryexpr(TBAND, l));
   1828   l           = mkunaryexpr(TMUL, tmp);
   1829   if (bit) {
   1830     bit->base = l;
   1831     l         = bit;
   1832   }
   1833   r       = mkbinaryexpr(&tok.loc, op, l, r);
   1834   e->next = mkassignexpr(l, r);
   1835   return mkexpr(EXPRCOMMA, l->type, e);
   1836 }
   1837 
   1838 struct expr *
   1839 expr(struct scope *s)
   1840 {
   1841   struct expr *r, *e, **end;
   1842 
   1843   end = &r;
   1844   for (;;) {
   1845     e    = assignexpr(s);
   1846     *end = e;
   1847     end  = &e->next;
   1848     if (tok.kind != TCOMMA)
   1849       break;
   1850     next();
   1851   }
   1852   if (!r->next)
   1853     return r;
   1854   return mkexpr(EXPRCOMMA, e->type, r);
   1855 }