aruu

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

decl.c (40727B)


      1 #include <assert.h>
      2 #include <limits.h>
      3 #include <stdbool.h>
      4 #include <stddef.h>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <string.h>
      8 #include "util.h"
      9 #include "cc.h"
     10 
     11 static struct decl *tentativedefns, **tentativedefnsend = &tentativedefns;
     12 
     13 struct qualtype {
     14   struct type  *type;
     15   enum typequal qual;
     16   struct expr  *expr;
     17 };
     18 
     19 enum storageclass {
     20   SCNONE,
     21 
     22   SCTYPEDEF     = 1 << 1,
     23   SCEXTERN      = 1 << 2,
     24   SCSTATIC      = 1 << 3,
     25   SCAUTO        = 1 << 4,
     26   SCREGISTER    = 1 << 5,
     27   SCTHREADLOCAL = 1 << 6,
     28 };
     29 
     30 enum typespec {
     31   SPECNONE,
     32 
     33   SPECVOID     = 1 << 1,
     34   SPECCHAR     = 1 << 2,
     35   SPECBOOL     = 1 << 3,
     36   SPECINT      = 1 << 4,
     37   SPECFLOAT    = 1 << 5,
     38   SPECDOUBLE   = 1 << 6,
     39   SPECSHORT    = 1 << 7,
     40   SPECLONG     = 1 << 8,
     41   SPECLONG2    = 1 << 9,
     42   SPECSIGNED   = 1 << 10,
     43   SPECUNSIGNED = 1 << 11,
     44   SPECCOMPLEX  = 1 << 12,
     45   SPECBITINT   = 1 << 13,
     46   SPECINT128   = 1 << 14,
     47 
     48   SPECLONGLONG = SPECLONG | SPECLONG2,
     49 };
     50 
     51 enum funcspec {
     52   FUNCNONE,
     53 
     54   FUNCINLINE   = 1 << 1,
     55   FUNCNORETURN = 1 << 2,
     56 };
     57 
     58 struct structbuilder {
     59   struct type    *type;
     60   struct member **last;
     61   unsigned        bits; /* number of bits remaining in the last byte */
     62   bool            pack;
     63 };
     64 
     65 struct decl *
     66 mkdecl(char *name, enum declkind k, struct type *t, enum typequal tq, enum linkage linkage)
     67 {
     68   struct decl *d;
     69 
     70   d = xmalloc(sizeof(*d));
     71   memset(d, 0, sizeof(*d));
     72   d->name    = name;
     73   d->kind    = k;
     74   d->linkage = linkage;
     75   d->type    = t;
     76   d->qual    = tq;
     77   if (k == DECLOBJECT && t)
     78     d->u.obj.align = t->align;
     79 
     80   return d;
     81 }
     82 
     83 /* 6.7.1 storage-class specifiers */
     84 static int
     85 storageclass(enum storageclass *sc)
     86 {
     87   enum storageclass allowed, new;
     88 
     89   switch (tok.kind) {
     90     case TTYPEDEF:
     91       new = SCTYPEDEF;
     92       break;
     93     case TEXTERN:
     94       new = SCEXTERN;
     95       break;
     96     case TSTATIC:
     97       new = SCSTATIC;
     98       break;
     99     case TTHREAD_LOCAL:
    100       new = SCTHREADLOCAL;
    101       break;
    102     case TAUTO:
    103       new = SCAUTO;
    104       break;
    105     case TREGISTER:
    106       new = SCREGISTER;
    107       break;
    108     default:
    109       return 0;
    110   }
    111   if (!sc)
    112     error(&tok.loc, "storage class not allowed in this declaration");
    113   switch (*sc) {
    114     case SCNONE:
    115       allowed = ~SCNONE;
    116       break;
    117     case SCTHREADLOCAL:
    118       allowed = SCSTATIC | SCEXTERN;
    119       break;
    120     case SCSTATIC:
    121     case SCEXTERN:
    122       allowed = SCTHREADLOCAL;
    123       break;
    124     default:
    125       allowed = SCNONE;
    126       break;
    127   }
    128   if (new & ~allowed)
    129     error(&tok.loc, "invalid combination of storage class specifiers");
    130   *sc |= new;
    131   next();
    132 
    133   return 1;
    134 }
    135 
    136 /* 6.7.3 type qualifiers */
    137 static int
    138 typequal(enum typequal *tq)
    139 {
    140   switch (tok.kind) {
    141     case TCONST:
    142       *tq |= QUALCONST;
    143       break;
    144     case TVOLATILE:
    145       *tq |= QUALVOLATILE;
    146       break;
    147     case TRESTRICT:
    148       *tq |= QUALRESTRICT;
    149       break;
    150     case T_ATOMIC:
    151       /* only the qualifier form (_Atomic int x;), not the specifier
    152        * form (_Atomic(int) x;): atomicity is achieved entirely
    153        * through the __atomic_* builtins operating on an ordinary
    154        * pointer to the object (see qbe.c's mkatomicfn), so the
    155        * qualifier itself carries no codegen weight beyond being
    156        * recorded, the same as const/volatile */
    157       *tq |= QUALATOMIC;
    158       break;
    159     default:
    160       return 0;
    161   }
    162   next();
    163 
    164   return 1;
    165 }
    166 
    167 /* 6.7.4 function specifiers */
    168 static int
    169 funcspec(enum funcspec *fs)
    170 {
    171   enum funcspec new;
    172 
    173   switch (tok.kind) {
    174     case TINLINE:
    175       new = FUNCINLINE;
    176       break;
    177     case T_NORETURN:
    178       new = FUNCNORETURN;
    179       break;
    180     default:
    181       return 0;
    182   }
    183   if (!fs)
    184     error(&tok.loc, "function specifier not allowed in this declaration");
    185   *fs |= new;
    186   next();
    187 
    188   return 1;
    189 }
    190 
    191 static void            structdecl(struct scope *, struct structbuilder *);
    192 static struct qualtype declspecs(struct scope *, enum storageclass *, enum funcspec *, int *);
    193 
    194 static struct type *
    195 tagspec(struct scope *s)
    196 {
    197   static struct type *const inttypes[][2] = {
    198       {&typeuint, &typeint},
    199       {&typeulong, &typelong},
    200       {&typeullong, &typellong},
    201   };
    202   struct type         *t, *et;
    203   char                *tag, *name;
    204   enum typekind        kind;
    205   struct decl         *d, *enumconsts;
    206   struct expr         *e;
    207   struct attr          a;
    208   enum attrkind        allowedattr;
    209   struct structbuilder b;
    210   unsigned long long   value, max, min;
    211   bool                 sign;
    212   int                  i;
    213 
    214   allowedattr = 0;
    215   switch (tok.kind) {
    216     case TSTRUCT:
    217       kind = TYPESTRUCT, allowedattr |= ATTRPACKED;
    218       break;
    219     case TUNION:
    220       kind = TYPEUNION, allowedattr |= ATTRPACKED;
    221       break;
    222     case TENUM:
    223       kind = TYPEENUM;
    224       break;
    225     default:
    226       fatal("internal error: unknown tag kind");
    227   }
    228   next();
    229   a.kind = 0;
    230   attr(&a, allowedattr);
    231   gnuattr(&a, allowedattr);
    232   tag = 0;
    233   t   = NULL;
    234   et  = NULL;
    235   if (tok.kind >= TIDENT) {
    236     tag = tokenstr(tok.kind);
    237     next();
    238   }
    239   if (kind == TYPEENUM && consume(TCOLON)) {
    240     et = declspecs(s, NULL, NULL, NULL).type;
    241     if (!et)
    242       error(&tok.loc, "no type in enum type specifier");
    243   }
    244   if (tag)
    245     t = scopegettag(s, tag, tok.kind != TLBRACE && tok.kind != TSEMICOLON);
    246   if (t) {
    247     if (t->kind != kind)
    248       error(&tok.loc, "redeclaration of tag '%s' with different kind", tag);
    249   } else {
    250     if (kind == TYPEENUM) {
    251       t       = mktype(kind, PROPSCALAR | PROPARITH | PROPREAL | PROPINT);
    252       t->base = et;
    253     } else {
    254       t                        = mktype(kind, 0);
    255       t->size                  = 0;
    256       t->align                 = 0;
    257       t->u.structunion.tag     = tag;
    258       t->u.structunion.members = NULL;
    259     }
    260     t->incomplete = true;
    261     if (tag)
    262       scopeputtag(s, tag, t);
    263   }
    264   if (tok.kind != TLBRACE)
    265     return t;
    266   if (!t->incomplete)
    267     error(&tok.loc, "redefinition of tag '%s'", tag);
    268   next();
    269   switch (t->kind) {
    270     case TYPESTRUCT:
    271     case TYPEUNION:
    272       b.type = t;
    273       b.last = &t->u.structunion.members;
    274       b.bits = 0;
    275       b.pack = a.kind & ATTRPACKED;
    276       do
    277         structdecl(s, &b);
    278       while (tok.kind != TRBRACE);
    279       if (!t->u.structunion.members)
    280         error(&tok.loc, "struct/union has no members");
    281       next();
    282       if (!b.pack)
    283         t->size = ALIGNUP(t->size, t->align);
    284       break;
    285     case TYPEENUM:
    286       enumconsts = NULL;
    287       if (et) {
    288         t->size             = t->base->size;
    289         t->align            = t->base->align;
    290         t->u.arith.issigned = t->base->u.arith.issigned;
    291         t->u.arith.width    = t->base->u.arith.width;
    292         t->incomplete       = false;
    293         et                  = t;
    294       } else {
    295         et = &typeint;
    296       }
    297       max = 0;
    298       min = 0;
    299       for (value = 0; tok.kind >= TIDENT; ++value) {
    300         name = tokenstr(tok.kind);
    301         next();
    302         attr(NULL, 0);
    303         if (consume(TASSIGN)) {
    304           e = eval(condexpr(s));
    305           if (e->kind != EXPRCONST || !(e->type->prop & PROPINT))
    306             error(
    307                 &tok.loc,
    308                 "expected integer "
    309                 "constant expression"
    310             );
    311           value = e->u.constant.u;
    312           if (!t->base)
    313             et = typehasint(&typeint, value, e->type->u.arith.issigned) ? &typeint : e->type;
    314           else if (!typehasint(et, value, e->type->u.arith.issigned))
    315             goto invalid;
    316         } else if (
    317             (value == 0 && !et->u.arith.issigned) || (value == 1ull << 63 && et->u.arith.issigned)
    318         ) {
    319           error(
    320               &tok.loc,
    321               "no %ssigned integer type can represent "
    322               "enumerator value",
    323               et->u.arith.issigned ? "" : "un"
    324           );
    325         } else if (!typehasint(et, value, et->u.arith.issigned)) {
    326           if (t->base) {
    327           invalid:
    328             /* fixed underlying type */
    329             error(
    330                 &tok.loc,
    331                 "enumerator '%s' value cannot be "
    332                 "represented in underlying type",
    333                 name
    334             );
    335           }
    336           sign = et->u.arith.issigned;
    337           for (i = 0; (size_t)i < countof(inttypes); ++i) {
    338             et = inttypes[i][sign];
    339             if (typehasint(et, value, sign))
    340               break;
    341           }
    342           assert((size_t)i < countof(inttypes));
    343         }
    344         d              = mkdecl(name, DECLCONST, et, QUALNONE, LINKNONE);
    345         d->u.enumconst = value;
    346         d->value       = mkintconst(value);
    347         d->next        = enumconsts;
    348         enumconsts     = d;
    349         if (et->u.arith.issigned && value >= 1ull << 63) {
    350           if (-value > min)
    351             min = -value;
    352         } else if (value > max) {
    353           max = value;
    354         }
    355         scopeputdecl(s, d);
    356         if (!consume(TCOMMA))
    357           break;
    358       }
    359       expect(TRBRACE, "to close enum specifier");
    360       if (!t->base) {
    361         if (min <= 0x80000000 && max <= 0x7fffffff) {
    362           t->base = min ? &typeint : &typeuint;
    363         } else {
    364           sign = min > 0;
    365           for (i = 0; (size_t)i < countof(inttypes); ++i) {
    366             et = inttypes[i][sign];
    367             if (typehasint(et, max, false) && typehasint(et, -min, true))
    368               break;
    369           }
    370           if (i == countof(inttypes))
    371             error(
    372                 &tok.loc,
    373                 "no integer type can represent "
    374                 "all enumerator values"
    375             );
    376           t->base = et;
    377           for (d = enumconsts; d; d = d->next)
    378             d->type = t;
    379         }
    380         t->size             = t->base->size;
    381         t->align            = t->base->align;
    382         t->u.arith.issigned = t->base->u.arith.issigned;
    383         t->u.arith.width    = t->base->u.arith.width;
    384       }
    385     default:
    386       break;
    387   }
    388   t->incomplete = false;
    389 
    390   return t;
    391 }
    392 
    393 /* 6.7 declarations */
    394 static struct qualtype
    395 declspecs(struct scope *s, enum storageclass *sc, enum funcspec *fs, int *align)
    396 {
    397   struct type       *t, *other;
    398   struct decl       *d;
    399   struct expr       *e;
    400   enum typespec      ts = SPECNONE;
    401   enum typequal      tq = QUALNONE;
    402   enum tokenkind     op;
    403   int                ntypes = 0;
    404   unsigned long long i, bits;
    405   struct expr       *typeofexpr = NULL;
    406 
    407   t = NULL;
    408   if (sc)
    409     *sc = SCNONE;
    410   if (fs)
    411     *fs = FUNCNONE;
    412   if (align)
    413     *align = 0;
    414   for (;;) {
    415     if (typequal(&tq) || storageclass(sc) || funcspec(fs))
    416       continue;
    417     op = tok.kind;
    418     switch (op) {
    419       /* 6.7.2 type specifiers */
    420       case TVOID:
    421         t = &typevoid;
    422         ++ntypes;
    423         next();
    424         break;
    425       case TCHAR:
    426         ts |= SPECCHAR;
    427         ++ntypes;
    428         next();
    429         break;
    430       case TSHORT:
    431         if (ts & SPECSHORT)
    432           error(&tok.loc, "duplicate 'short'");
    433         ts |= SPECSHORT;
    434         next();
    435         break;
    436       case TINT:
    437         ts |= SPECINT;
    438         ++ntypes;
    439         next();
    440         break;
    441       case TLONG:
    442         if (ts & SPECLONG2)
    443           error(&tok.loc, "too many 'long'");
    444         if (ts & SPECLONG)
    445           ts |= SPECLONG2;
    446         ts |= SPECLONG;
    447         next();
    448         break;
    449       case TFLOAT:
    450         ts |= SPECFLOAT;
    451         ++ntypes;
    452         next();
    453         break;
    454       case TDOUBLE:
    455         ts |= SPECDOUBLE;
    456         ++ntypes;
    457         next();
    458         break;
    459       case TSIGNED:
    460         if (ts & SPECSIGNED)
    461           error(&tok.loc, "duplicate 'signed'");
    462         ts |= SPECSIGNED;
    463         next();
    464         break;
    465       case TUNSIGNED:
    466         if (ts & SPECUNSIGNED)
    467           error(&tok.loc, "duplicate 'unsigned'");
    468         ts |= SPECUNSIGNED;
    469         next();
    470         break;
    471       case T_BITINT:
    472         ts |= SPECBITINT;
    473         ++ntypes;
    474         next();
    475         expect(TLPAREN, "after _BitInt");
    476         bits = intconstexpr(s, false);
    477         expect(TRPAREN, "after _BitInt width");
    478         break;
    479       case TBOOL:
    480         t = &typebool;
    481         ++ntypes;
    482         next();
    483         break;
    484       case T_INT128:
    485         ts |= SPECINT128;
    486         ++ntypes;
    487         next();
    488         break;
    489       case T_COMPLEX:
    490         if (ts & SPECCOMPLEX)
    491           error(&tok.loc, "duplicate '_Complex'");
    492         ts |= SPECCOMPLEX;
    493         next();
    494         break;
    495       case T_ATOMIC:
    496         error(&tok.loc, "_Atomic is not yet supported");
    497         break;
    498       case TSTRUCT:
    499       case TUNION:
    500       case TENUM:
    501         t = tagspec(s);
    502         ++ntypes;
    503         break;
    504       case TTYPEOF:
    505       case TTYPEOF_UNQUAL:
    506         next();
    507         expect(TLPAREN, "after 'typeof'");
    508         t = typename(s, &tq, &typeofexpr);
    509         if (!t) {
    510           e = expr(s);
    511           if (e->decayed)
    512             e = e->base;
    513           t = e->type;
    514           if (op == TTYPEOF)
    515             tq |= e->qual;
    516           if (t->prop & PROPVM)
    517             typeofexpr = e;
    518         }
    519         ++ntypes;
    520         expect(TRPAREN, "to close 'typeof'");
    521         break;
    522 
    523       /* 6.7.5 alignment specifier */
    524       case TALIGNAS:
    525         if (!align)
    526           error(
    527               &tok.loc,
    528               "alignment specifier not "
    529               "allowed in this declaration"
    530           );
    531         next();
    532         expect(TLPAREN, "after 'alignas'");
    533         other = typename(s, NULL, NULL);
    534         i     = other ? other->align : intconstexpr(s, false);
    535         if (i & i - 1 || i > INT_MAX)
    536           error(&tok.loc, "invalid alignment: %llu", i);
    537         if (i > (unsigned long long)*align)
    538           *align = i;
    539         expect(TRPAREN, "to close 'alignas' specifier");
    540         break;
    541 
    542       case T__ATTRIBUTE__:
    543         gnuattr(NULL, 0);
    544         break;
    545 
    546       default:
    547         if (op < TIDENT || t || ts)
    548           goto done;
    549         d = scopegetdecl(s, tokenstr(tok.kind), 1);
    550         if (!d || d->kind != DECLTYPE)
    551           goto done;
    552         t = d->type;
    553         tq |= d->qual;
    554         ++ntypes;
    555         next();
    556         break;
    557     }
    558     if (ntypes > 1 || (t && ts))
    559       error(&tok.loc, "multiple types in declaration specifiers");
    560   }
    561 done:
    562   switch ((int)ts) {
    563     case SPECNONE:
    564       break;
    565     case SPECCHAR:
    566       t = &typechar;
    567       break;
    568     case SPECSIGNED | SPECCHAR:
    569       t = &typeschar;
    570       break;
    571     case SPECUNSIGNED | SPECCHAR:
    572       t = &typeuchar;
    573       break;
    574     case SPECSHORT:
    575     case SPECSHORT | SPECINT:
    576     case SPECSIGNED | SPECSHORT:
    577     case SPECSIGNED | SPECSHORT | SPECINT:
    578       t = &typeshort;
    579       break;
    580     case SPECUNSIGNED | SPECSHORT:
    581     case SPECUNSIGNED | SPECSHORT | SPECINT:
    582       t = &typeushort;
    583       break;
    584     case SPECINT:
    585     case SPECSIGNED:
    586     case SPECSIGNED | SPECINT:
    587       t = &typeint;
    588       break;
    589     case SPECUNSIGNED:
    590     case SPECUNSIGNED | SPECINT:
    591       t = &typeuint;
    592       break;
    593     case SPECLONG:
    594     case SPECLONG | SPECINT:
    595     case SPECSIGNED | SPECLONG:
    596     case SPECSIGNED | SPECLONG | SPECINT:
    597       t = &typelong;
    598       break;
    599     case SPECUNSIGNED | SPECLONG:
    600     case SPECUNSIGNED | SPECLONG | SPECINT:
    601       t = &typeulong;
    602       break;
    603     case SPECLONGLONG:
    604     case SPECLONGLONG | SPECINT:
    605     case SPECSIGNED | SPECLONGLONG:
    606     case SPECSIGNED | SPECLONGLONG | SPECINT:
    607       t = &typellong;
    608       break;
    609     case SPECUNSIGNED | SPECLONGLONG:
    610     case SPECUNSIGNED | SPECLONGLONG | SPECINT:
    611       t = &typeullong;
    612       break;
    613     case SPECBITINT:
    614     case SPECBITINT | SPECSIGNED:
    615       t = mkbitinttype(bits, true);
    616       break;
    617     case SPECBITINT | SPECUNSIGNED:
    618       t = mkbitinttype(bits, false);
    619       break;
    620     case SPECINT128:
    621     case SPECINT128 | SPECSIGNED:
    622       t = &typeint128;
    623       break;
    624     case SPECINT128 | SPECUNSIGNED:
    625       t = &typeuint128;
    626       break;
    627     case SPECFLOAT:
    628       t = &typefloat;
    629       break;
    630     case SPECDOUBLE:
    631       t = &typedouble;
    632       break;
    633     case SPECLONG | SPECDOUBLE:
    634       t = &typeldouble;
    635       break;
    636     case SPECFLOAT | SPECCOMPLEX:
    637       t = &typefloatcomplex;
    638       break;
    639     case SPECDOUBLE | SPECCOMPLEX:
    640       t = &typedoublecomplex;
    641       break;
    642     case SPECLONG | SPECDOUBLE | SPECCOMPLEX:
    643       error(&tok.loc, "long double _Complex is not yet supported");
    644       break;
    645     default:
    646       error(&tok.loc, "invalid combination of type specifiers");
    647   }
    648   if (tq & QUALRESTRICT) {
    649     /* C23 6.7.4.1p2 */
    650     other = t;
    651     while (other->kind == TYPEARRAY)
    652       other = other->base;
    653     if (!other || other->kind != TYPEPOINTER)
    654       error(&tok.loc, "'restrict' applied to non-pointer type");
    655     if (other->base->kind == TYPEFUNC)
    656       error(&tok.loc, "'restrict' applied to function pointer");
    657   }
    658   if (!t && (tq || (sc && *sc) || (fs && *fs)))
    659     error(&tok.loc, "declaration has no type specifier");
    660   /*
    661    * TODO: consider delaying attribute parsing to declarator(),
    662    * so we can tell the difference between the start of an
    663    * attribute and an array declarator
    664    */
    665   attr(NULL, 0);
    666 
    667   return (struct qualtype){t, tq, typeofexpr};
    668 }
    669 
    670 /* 6.7.6 declarators */
    671 static struct decl *parameter(struct scope *);
    672 
    673 static bool
    674 istypename(struct scope *s, const char *name)
    675 {
    676   struct decl *d;
    677 
    678   d = scopegetdecl(s, name, 1);
    679   return d && d->kind == DECLTYPE;
    680 }
    681 
    682 /*
    683  * when parsing a declarator, qualifiers for derived types are temporarily
    684  * stored in the `qual` field of the type itself (elsewhere this field
    685  * is used for the qualifiers of the base type). this is corrected in
    686  * declarator()
    687  */
    688 static void
    689 declaratortypes(
    690     struct scope  *s,
    691     struct list   *result,
    692     char         **name,
    693     int           *align,
    694     struct scope **funcscope,
    695     bool           allowabstract
    696 )
    697 {
    698   struct list  *ptr;
    699   struct type  *t;
    700   struct decl  *d, **paramend;
    701   struct expr  *e;
    702   struct attr   a;
    703   enum typequal tq;
    704   int           allowedattr;
    705 
    706   while (consume(TMUL)) {
    707     attr(NULL, 0);
    708     tq = QUALNONE;
    709     while (typequal(&tq))
    710       ;
    711     t = mkpointertype(NULL, tq);
    712     listinsert(result, &t->link);
    713   }
    714   if (name)
    715     *name = NULL;
    716   a.kind = 0;
    717   ptr    = result->next;
    718   switch (tok.kind) {
    719     case TLPAREN:
    720       next();
    721       if (allowabstract) {
    722         switch (tok.kind) {
    723           case TMUL:
    724           case TLPAREN:
    725             break;
    726           default:
    727             if (tok.kind >= TIDENT && !istypename(s, tokenstr(tok.kind)))
    728               break;
    729             goto func;
    730         }
    731       }
    732       declaratortypes(s, result, name, align, funcscope, allowabstract);
    733       expect(TRPAREN, "after parenthesized declarator");
    734       allowedattr = -1;
    735       break;
    736     default:
    737       if (tok.kind >= TIDENT) {
    738         if (!name)
    739           error(
    740               &tok.loc,
    741               "identifier not allowed in "
    742               "abstract declarator"
    743           );
    744         *name = tokenstr(tok.kind);
    745         next();
    746       } else if (!allowabstract) {
    747         error(&tok.loc, "expected '(' or identifier");
    748       }
    749       /*
    750        * the aligned attribute is used in the definition of
    751        * max_align_t from gcc/clang (used by other Linux libcs),
    752        * dragonflybsd, freebsd, and openbsd
    753        */
    754       allowedattr = align ? ATTRALIGNED : 0;
    755   }
    756   for (;;) {
    757     switch (tok.kind) {
    758       case TLPAREN: /* function declarator */
    759         next();
    760       func:
    761         t                  = mktype(TYPEFUNC, 0);
    762         t->qual            = QUALNONE;
    763         t->u.func.isvararg = false;
    764         t->u.func.params   = NULL;
    765         t->u.func.nparam   = 0;
    766         paramend           = &t->u.func.params;
    767         s                  = mkscope(s);
    768         d                  = NULL;
    769         do {
    770           if (consume(TELLIPSIS)) {
    771             t->u.func.isvararg = true;
    772             break;
    773           }
    774           if (tok.kind == TRPAREN)
    775             break;
    776           d = parameter(s);
    777           if (d->name)
    778             scopeputdecl(s, d);
    779           *paramend = d;
    780           paramend  = &d->next;
    781           ++t->u.func.nparam;
    782         } while (consume(TCOMMA));
    783         expect(TRPAREN, "to close function declarator");
    784         if (funcscope && ptr->prev == result) {
    785           /* we may need to re-open the scope later if
    786            * this is a function definition */
    787           *funcscope = s;
    788           s          = s->parent;
    789         } else {
    790           s = delscope(s);
    791         }
    792         if (t->u.func.nparam == 1 && !t->u.func.isvararg && d->type->kind == TYPEVOID && !d->name) {
    793           t->u.func.params = NULL;
    794           t->u.func.nparam = 0;
    795         }
    796         listinsert(ptr->prev, &t->link);
    797         allowedattr = 0;
    798         break;
    799       case TLBRACK: /* array declarator */
    800         if (allowedattr != -1 && attr(&a, allowedattr))
    801           goto attr;
    802         next();
    803         t = mkarraytype(NULL, QUALNONE, 0);
    804         while (consume(TSTATIC) || typequal(&t->u.array.ptrqual))
    805           ;
    806         if (tok.kind == TMUL && peek(TRBRACK)) {
    807           t->prop |= PROPVM;
    808           t->incomplete = false;
    809         } else if (!consume(TRBRACK)) {
    810           e = assignexpr(s);
    811           if (!(e->type->prop & PROPINT))
    812             error(
    813                 &tok.loc,
    814                 "array length expression must "
    815                 "have integer type"
    816             );
    817           t->u.array.length = e;
    818           t->incomplete     = false;
    819           expect(TRBRACK, "after array length");
    820         }
    821         listinsert(ptr->prev, &t->link);
    822         allowedattr = 0;
    823         break;
    824       case T__ATTRIBUTE__:
    825         if (allowedattr == -1)
    826           error(
    827               &tok.loc,
    828               "attribute not allowed after "
    829               "parenthesized declarator"
    830           );
    831         gnuattr(&a, allowedattr);
    832       attr:
    833         /* attribute applies to identifier if ptr->prev ==
    834          * result, otherwise type ptr->prev */
    835         if (ptr->prev == result) {
    836           if (a.kind & ATTRALIGNED && a.align > *align)
    837             *align = a.align;
    838         }
    839         break;
    840       default:
    841         return;
    842     }
    843   }
    844 }
    845 
    846 static struct qualtype
    847 declarator(
    848     struct scope   *s,
    849     struct qualtype base,
    850     char          **name,
    851     int            *align,
    852     struct scope  **funcscope,
    853     bool            allowabstract
    854 )
    855 {
    856   struct type  *t;
    857   enum typequal tq;
    858   struct expr  *e;
    859   struct list   result = {&result, &result}, *l, *prev;
    860 
    861   if (funcscope)
    862     *funcscope = NULL;
    863   declaratortypes(s, &result, name, align, funcscope, allowabstract);
    864   for (l = result.prev; l != &result; l = prev) {
    865     prev    = l->prev;
    866     t       = listelement(l, struct type, link);
    867     tq      = t->qual;
    868     t->base = base.type;
    869     t->qual = base.qual;
    870     t->prop |= base.type->prop & PROPVM;
    871     switch (t->kind) {
    872       case TYPEFUNC:
    873         if (base.type->kind == TYPEFUNC)
    874           error(
    875               &tok.loc,
    876               "function declarator specifies "
    877               "function return type"
    878           );
    879         if (base.type->kind == TYPEARRAY)
    880           error(
    881               &tok.loc,
    882               "function declarator specifies "
    883               "array return type"
    884           );
    885         break;
    886       case TYPEARRAY:
    887         if (base.type->incomplete)
    888           error(&tok.loc, "array element has incomplete type");
    889         if (base.type->kind == TYPEFUNC)
    890           error(&tok.loc, "array element has function type");
    891         if (t->u.array.ptrqual) {
    892           /* TODO: check if we are in a function prototype
    893            * if (?)
    894            * error(&tok.loc, "array type has
    895            * qualifiers outside of a function prototype");
    896            */
    897           if (prev != &result)
    898             error(
    899                 &tok.loc,
    900                 "nested array type has "
    901                 "qualifiers"
    902             );
    903         }
    904         t->align = base.type->align;
    905         t->size  = 0;
    906         if (t->u.array.length) {
    907           e = eval(t->u.array.length);
    908           if (e->kind == EXPRCONST && base.type->size) {
    909             if (e->type->u.arith.issigned && e->u.constant.u >> 63)
    910               error(
    911                   &tok.loc,
    912                   "array length must be "
    913                   "non-negative"
    914               );
    915             if (e->u.constant.u > ULLONG_MAX / base.type->size)
    916               error(
    917                   &tok.loc,
    918                   "array length "
    919                   "is too large"
    920               );
    921             t->size = base.type->size * e->u.constant.u;
    922           } else {
    923             t->prop |= PROPVM;
    924             t->u.array.length = e;
    925             t->u.array.size   = NULL;
    926           }
    927         }
    928         break;
    929       default:
    930         break;
    931     }
    932     base.type = t;
    933     base.qual = tq;
    934   }
    935 
    936   return base;
    937 }
    938 
    939 static struct decl *
    940 parameter(struct scope *s)
    941 {
    942   struct decl      *d;
    943   char             *name;
    944   struct qualtype   t;
    945   enum storageclass sc;
    946 
    947   attr(NULL, 0);
    948   t = declspecs(s, &sc, NULL, NULL);
    949   if (!t.type)
    950     error(&tok.loc, "no type in parameter declaration");
    951   if (sc && sc != SCREGISTER)
    952     error(
    953         &tok.loc,
    954         "parameter declaration has invalid "
    955         "storage-class specifier"
    956     );
    957   t                = declarator(s, t, &name, NULL, NULL, true);
    958   t.type           = typeadjust(t.type, &t.qual);
    959   d                = mkdecl(name, DECLOBJECT, t.type, t.qual, LINKNONE);
    960   d->u.obj.storage = SDAUTO;
    961   return d;
    962 }
    963 
    964 static void
    965 addmember(
    966     struct structbuilder *b, struct qualtype mt, char *name, int align, unsigned long long width
    967 )
    968 {
    969   struct type   *t = b->type;
    970   struct member *m;
    971   size_t         end;
    972 
    973   if (t->kind == TYPESTRUCT && t->prop & PROPFLEX)
    974     error(&tok.loc, "struct has member '%s' after flexible array member", name);
    975   if (mt.type->incomplete) {
    976     if (mt.type->kind != TYPEARRAY)
    977       error(&tok.loc, "struct member '%s' has incomplete type", name);
    978     t->prop |= PROPFLEX;
    979   }
    980   if (mt.type->prop & PROPFLEX) {
    981     /*
    982      * ISO c doesnt allow structures with members containing
    983      * fams, but allow it as an extension
    984      */
    985     t->prop |= PROPFLEX;
    986   }
    987   if (mt.type->kind == TYPEFUNC)
    988     error(&tok.loc, "struct member '%s' has function type", name);
    989   if (mt.type->prop & PROPVM)
    990     error(&tok.loc, "struct member '%s' has variably modified type", name);
    991   assert(mt.type->align > 0);
    992   if (name || width == -1ull) {
    993     m        = xmalloc(sizeof(*m));
    994     m->type  = mt.type;
    995     m->qual  = mt.qual;
    996     m->name  = name;
    997     m->next  = NULL;
    998     *b->last = m;
    999     b->last  = &m->next;
   1000   } else {
   1001     m = NULL;
   1002   }
   1003   if (width == -1ull) {
   1004     m->bits.before = 0;
   1005     m->bits.after  = 0;
   1006     if (align < mt.type->align) {
   1007       if (align)
   1008         error(
   1009             &tok.loc,
   1010             "specified alignment of struct member "
   1011             "'%s' is less strict than is required by "
   1012             "type",
   1013             name
   1014         );
   1015       align = b->pack ? 1 : mt.type->align;
   1016     }
   1017     if (t->kind == TYPESTRUCT) {
   1018       m->offset = ALIGNUP(t->size, align);
   1019       t->size   = m->offset + mt.type->size;
   1020     } else {
   1021       m->offset = 0;
   1022       if (t->size < mt.type->size)
   1023         t->size = mt.type->size;
   1024     }
   1025     b->bits = 0;
   1026   } else { /* bit-field */
   1027     if (!(mt.type->prop & PROPINT))
   1028       error(&tok.loc, "bit-field '%s' has invalid type", name);
   1029     if (align)
   1030       error(&tok.loc, "alignment specified for bit-field '%s'", name);
   1031     if (b->pack)
   1032       error(
   1033           &tok.loc,
   1034           "bit-field '%s' in packed struct is not "
   1035           "supported",
   1036           name
   1037       );
   1038     if (!width && name)
   1039       error(
   1040           &tok.loc,
   1041           "bit-field '%s' with zero width must not have "
   1042           "declarator",
   1043           name
   1044       );
   1045     if (width > (unsigned long long)mt.type->u.arith.width)
   1046       error(&tok.loc, "bit-field '%s' exceeds width of underlying type", name);
   1047     align = mt.type->align;
   1048     if (t->kind == TYPESTRUCT) {
   1049       /* calculate end of the storage-unit for this bit-field
   1050        */
   1051       end = ALIGNUP(t->size, mt.type->size);
   1052       if (!width || width > (end - t->size) * 8 + b->bits) {
   1053         /* no room, allocate a new storage-unit */
   1054         t->size = end;
   1055         b->bits = 0;
   1056       }
   1057       if (m) {
   1058         m->offset      = ALIGNDOWN(t->size - !!b->bits, mt.type->size);
   1059         m->bits.before = (t->size - m->offset) * 8 - b->bits;
   1060         m->bits.after  = mt.type->size * 8 - width - m->bits.before;
   1061       }
   1062       t->size += (width - b->bits + 7) / 8;
   1063       b->bits = (b->bits - width) % 8;
   1064     } else if (m) {
   1065       m->offset      = 0;
   1066       m->bits.before = 0;
   1067       m->bits.after  = mt.type->size * 8 - width;
   1068       if (t->size < mt.type->size)
   1069         t->size = mt.type->size;
   1070     }
   1071   }
   1072   if (m && t->align < align)
   1073     t->align = align;
   1074 }
   1075 
   1076 static bool
   1077 staticassert(struct scope *s)
   1078 {
   1079   struct stringlit   msg;
   1080   unsigned long long c;
   1081 
   1082   if (!consume(TSTATIC_ASSERT))
   1083     return false;
   1084   expect(TLPAREN, "after static_assert");
   1085   c = intconstexpr(s, true);
   1086   if (consume(TCOMMA)) {
   1087     tokencheck(&tok, TSTRINGLIT, "after static assertion expression");
   1088     stringconcat(&msg, true);
   1089     if (!c)
   1090       error(&tok.loc, "static assertion failed: %.*s", (int)(msg.size - 1), (char *)msg.data);
   1091   } else if (!c) {
   1092     error(&tok.loc, "static assertion failed");
   1093   }
   1094   expect(TRPAREN, "after static assertion");
   1095   expect(TSEMICOLON, "after static assertion");
   1096   return true;
   1097 }
   1098 
   1099 static void
   1100 structdecl(struct scope *s, struct structbuilder *b)
   1101 {
   1102   struct qualtype    base, mt;
   1103   char              *name;
   1104   unsigned long long width;
   1105   int                align;
   1106 
   1107   if (staticassert(s))
   1108     return;
   1109   attr(NULL, 0);
   1110   base = declspecs(s, NULL, NULL, &align);
   1111   if (!base.type)
   1112     error(&tok.loc, "no type in struct member declaration");
   1113   if (tok.kind == TSEMICOLON) {
   1114     if ((base.type->kind != TYPESTRUCT && base.type->kind != TYPEUNION)
   1115         || base.type->u.structunion.tag)
   1116       error(
   1117           &tok.loc,
   1118           "struct declaration must declare at "
   1119           "least one member"
   1120       );
   1121     next();
   1122     addmember(b, base, NULL, align, -1);
   1123     return;
   1124   }
   1125   for (;;) {
   1126     if (consume(TCOLON)) {
   1127       width = intconstexpr(s, false);
   1128       addmember(b, base, NULL, 0, width);
   1129     } else {
   1130       mt    = declarator(s, base, &name, &align, NULL, false);
   1131       width = consume(TCOLON) ? intconstexpr(s, false) : -1;
   1132       addmember(b, mt, name, align, width);
   1133     }
   1134     if (tok.kind == TSEMICOLON)
   1135       break;
   1136     expect(TCOMMA, "or ';' after declarator");
   1137   }
   1138   next();
   1139 }
   1140 
   1141 /* 6.7.7 type names */
   1142 struct type *typename(struct scope *s, enum typequal *tq, struct expr **toeval)
   1143 {
   1144   struct qualtype t;
   1145 
   1146   t = declspecs(s, NULL, NULL, NULL);
   1147   if (t.type) {
   1148     t = declarator(s, t, NULL, NULL, NULL, true);
   1149     if (tq)
   1150       *tq |= t.qual;
   1151     if (toeval)
   1152       *toeval = t.expr;
   1153   }
   1154   return t.type;
   1155 }
   1156 
   1157 static enum linkage
   1158 getlinkage(enum declkind kind, enum storageclass sc, struct decl *prior, bool filescope)
   1159 {
   1160   if (sc & SCSTATIC)
   1161     return filescope ? LINKINTERN : LINKNONE;
   1162   if (sc & SCEXTERN || kind == DECLFUNC)
   1163     return prior ? prior->linkage : LINKEXTERN;
   1164   return filescope ? LINKEXTERN : LINKNONE;
   1165 }
   1166 
   1167 static struct decl *
   1168 declcommon(
   1169     struct scope     *s,
   1170     enum declkind     kind,
   1171     char             *name,
   1172     char             *asmname,
   1173     struct type      *t,
   1174     enum typequal     tq,
   1175     enum storageclass sc,
   1176     struct decl      *prior
   1177 )
   1178 {
   1179   struct decl *d;
   1180   enum linkage linkage;
   1181   const char  *kindstr = kind == DECLFUNC ? "function" : "object";
   1182 
   1183   if (prior) {
   1184     if (prior->linkage == LINKNONE)
   1185       error(&tok.loc, "%s '%s' with no linkage redeclared", kindstr, name);
   1186     linkage = getlinkage(kind, sc, prior, s == &filescope);
   1187     if (prior->linkage != linkage)
   1188       error(&tok.loc, "%s '%s' redeclared with different linkage", kindstr, name);
   1189     if (!typecompatible(t, prior->type) || tq != prior->qual)
   1190       error(&tok.loc, "%s '%s' redeclared with incompatible type", kindstr, name);
   1191     if (asmname && (!prior->asmname || strcmp(prior->asmname, asmname) != 0))
   1192       error(
   1193           &tok.loc,
   1194           "%s '%s' redeclared with different assembler "
   1195           "name",
   1196           kindstr,
   1197           name
   1198       );
   1199     prior->type = typecomposite(t, prior->type);
   1200     return prior;
   1201   }
   1202   if (s->parent)
   1203     prior = scopegetdecl(s->parent, name, true);
   1204   linkage = getlinkage(kind, sc, prior, s == &filescope);
   1205   if (linkage != LINKNONE && s->parent) {
   1206     /* XXX: should maintain map of identifiers with linkage to their
   1207      * declaration, and use that */
   1208     if (s->parent != &filescope)
   1209       prior = scopegetdecl(&filescope, name, false);
   1210     if (prior && prior->linkage != LINKNONE) {
   1211       if (prior->kind != kind)
   1212         error(&tok.loc, "'%s' redeclared with different kind", name);
   1213       if (prior->linkage != linkage)
   1214         error(
   1215             &tok.loc,
   1216             "%s '%s' redeclared with different "
   1217             "linkage",
   1218             kindstr,
   1219             name
   1220         );
   1221       if (!typecompatible(t, prior->type) || tq != prior->qual)
   1222         error(
   1223             &tok.loc,
   1224             "%s '%s' redeclared with incompatible "
   1225             "type",
   1226             kindstr,
   1227             name
   1228         );
   1229       if (!asmname)
   1230         asmname = prior->asmname;
   1231       else if (!prior->asmname || strcmp(prior->asmname, asmname) != 0)
   1232         error(
   1233             &tok.loc,
   1234             "%s '%s' redeclared with different "
   1235             "assembler name",
   1236             kindstr,
   1237             name
   1238         );
   1239       t = typecomposite(t, prior->type);
   1240     }
   1241   }
   1242   d          = mkdecl(name, kind, t, tq, linkage);
   1243   d->asmname = asmname;
   1244   scopeputdecl(s, d);
   1245   return d;
   1246 }
   1247 
   1248 static void
   1249 defineobj(struct decl *d, struct init *init, bool hasinit, struct func *f)
   1250 {
   1251   if (d->type->incomplete)
   1252     error(&tok.loc, "object '%s' has incomplete type", d->name);
   1253   if (d->u.obj.align < d->type->align)
   1254     d->u.obj.align = d->type->align;
   1255   if (d->u.obj.storage == SDAUTO)
   1256     funcinit(f, d, init, hasinit);
   1257   else
   1258     emitdata(d, init);
   1259   d->defined = true;
   1260 }
   1261 
   1262 bool
   1263 decl(struct scope *s, struct func *f)
   1264 {
   1265   struct qualtype   base, qt;
   1266   struct type      *t;
   1267   enum typequal     tq;
   1268   enum storageclass sc;
   1269   enum funcspec     fs;
   1270   struct attr       a;
   1271   struct init      *init;
   1272   bool              hasinit;
   1273   char             *name, *asmname;
   1274   int               allowfunc = !f;
   1275   struct decl      *d, *prior;
   1276   enum declkind     kind;
   1277   struct scope     *funcscope;
   1278   int               align;
   1279 
   1280   if (staticassert(s))
   1281     return true;
   1282   a.kind = 0;
   1283   if (attr(&a, ATTRNORETURN) && consume(TSEMICOLON))
   1284     return true;
   1285   base = declspecs(s, &sc, &fs, &align);
   1286   if (!base.type)
   1287     return false;
   1288   if (f) {
   1289     if (sc == SCTHREADLOCAL)
   1290       error(
   1291           &tok.loc,
   1292           "block scope declaration containing "
   1293           "'thread_local' must contain 'static' "
   1294           "or 'extern'"
   1295       );
   1296   } else {
   1297     /* 6.9p2 */
   1298     if (sc & SCAUTO)
   1299       error(&tok.loc, "external declaration must not contain 'auto'");
   1300     if (sc & SCREGISTER)
   1301       error(
   1302           &tok.loc,
   1303           "external declaration must not contain "
   1304           "'register'"
   1305       );
   1306   }
   1307   if (consume(TSEMICOLON)) {
   1308     /* XXX 6.7p2 error unless in function parameter/struct/union, or
   1309      * tag/enum members are declared */
   1310     return true;
   1311   }
   1312   for (;;) {
   1313     qt = declarator(s, base, &name, NULL, &funcscope, false);
   1314     t  = qt.type;
   1315     tq = qt.qual;
   1316     if (consume(T__ASM__)) {
   1317       struct stringlit lit;
   1318 
   1319       expect(TLPAREN, "after __asm__");
   1320       tokencheck(&tok, TSTRINGLIT, "after __asm__");
   1321       stringconcat(&lit, true);
   1322       asmname = lit.data;
   1323       expect(TRPAREN, "after assembler name");
   1324       allowfunc = 0;
   1325     } else {
   1326       asmname = NULL;
   1327     }
   1328     gnuattr(&a, 0); /* appertains to identifier */
   1329     kind  = sc & SCTYPEDEF ? DECLTYPE : t->kind == TYPEFUNC ? DECLFUNC : DECLOBJECT;
   1330     prior = scopegetdecl(s, name, false);
   1331     if (prior && prior->kind != kind)
   1332       error(&tok.loc, "'%s' redeclared with different kind", name);
   1333     switch (kind) {
   1334       case DECLTYPE:
   1335         if (align)
   1336           error(
   1337               &tok.loc,
   1338               "typedef '%s' declared with alignment "
   1339               "specifier",
   1340               name
   1341           );
   1342         if (asmname)
   1343           error(
   1344               &tok.loc,
   1345               "typedef '%s' declared with assembler "
   1346               "label",
   1347               name
   1348           );
   1349         if (!prior)
   1350           scopeputdecl(s, mkdecl(name, DECLTYPE, t, tq, LINKNONE));
   1351         else if (!typesame(prior->type, t) || prior->qual != tq)
   1352           error(
   1353               &tok.loc,
   1354               "typedef '%s' redefined with different "
   1355               "type",
   1356               name
   1357           );
   1358         break;
   1359       case DECLOBJECT:
   1360         if (align && align < t->align)
   1361           error(
   1362               &tok.loc,
   1363               "object '%s' requires alignment %d, "
   1364               "which is stricter than specified "
   1365               "alignment %d",
   1366               name,
   1367               t->align,
   1368               align
   1369           );
   1370         d = declcommon(s, kind, name, asmname, t, tq, sc, prior);
   1371         if (d->u.obj.align < align)
   1372           d->u.obj.align = align;
   1373         if (d->linkage == LINKNONE && !(sc & SCSTATIC)) {
   1374           d->u.obj.storage = SDAUTO;
   1375         } else {
   1376           d->u.obj.storage = sc & SCTHREADLOCAL ? SDTHREAD : SDSTATIC;
   1377           if (t->prop & PROPVM)
   1378             error(
   1379                 &tok.loc,
   1380                 "object '%s' with %s storage "
   1381                 "duration cannot have variably "
   1382                 "modified type",
   1383                 name,
   1384                 d->u.obj.storage == SDSTATIC ? "static" : "thread"
   1385             );
   1386           d->value = mkglobal(d);
   1387         }
   1388 
   1389         if (base.expr)
   1390           funcexpr(f, base.expr);
   1391         init    = NULL;
   1392         hasinit = false;
   1393         if (consume(TASSIGN)) {
   1394           if (f && d->linkage != LINKNONE)
   1395             error(
   1396                 &tok.loc,
   1397                 "object '%s' with block scope "
   1398                 "and %s linkage cannot have "
   1399                 "initializer",
   1400                 name,
   1401                 d->linkage == LINKEXTERN ? "external" : "internal"
   1402             );
   1403           if (d->defined)
   1404             error(&tok.loc, "object '%s' redefined", name);
   1405           init    = parseinit(s, d->type);
   1406           hasinit = true;
   1407         } else if (sc & SCEXTERN) {
   1408           break;
   1409         } else if (d->linkage != LINKNONE && d->u.obj.storage == SDSTATIC) {
   1410           if (!d->defined && !d->tentative) {
   1411             d->tentative       = true;
   1412             *tentativedefnsend = d;
   1413             tentativedefnsend  = &d->next;
   1414           }
   1415           break;
   1416         }
   1417         defineobj(d, init, hasinit, f);
   1418         break;
   1419       case DECLFUNC:
   1420         if (align)
   1421           error(
   1422               &tok.loc,
   1423               "function '%s' declared with alignment "
   1424               "specifier",
   1425               name
   1426           );
   1427         if (f && sc && sc != SCEXTERN) /* 6.7.1p7 */
   1428           error(
   1429               &tok.loc,
   1430               "function '%s' with block scope may only "
   1431               "have storage class 'extern'",
   1432               name
   1433           );
   1434         d                    = declcommon(s, kind, name, asmname, t, tq, sc, prior);
   1435         d->value             = mkglobal(d);
   1436         d->u.func.inlinedefn = d->linkage == LINKEXTERN && fs & FUNCINLINE && !(sc & SCEXTERN)
   1437                                && (!prior || prior->u.func.inlinedefn);
   1438         d->u.func.isnoreturn = fs & FUNCNORETURN || a.kind & ATTRNORETURN;
   1439         if (tok.kind == TLBRACE) {
   1440           if (!allowfunc)
   1441             error(
   1442                 &tok.loc,
   1443                 "function definition "
   1444                 "not allowed"
   1445             );
   1446           if (d->defined)
   1447             error(&tok.loc, "function '%s' redefined", name);
   1448           /* re-open scope from function declarator */
   1449           assert(funcscope);
   1450           s = funcscope;
   1451           f = mkfunc(d, name, t, s);
   1452           stmt(f, s);
   1453           if (d->u.func.isnoreturn)
   1454             funchlt(f);
   1455           /* XXX: need to keep track of function in case a
   1456            * later declaration specifies extern */
   1457           if (!d->u.func.inlinedefn)
   1458             emitfunc(f, d->linkage == LINKEXTERN);
   1459           s = delscope(s);
   1460           delfunc(f);
   1461           d->defined = true;
   1462           return true;
   1463         } else if (funcscope) {
   1464           delscope(funcscope);
   1465         }
   1466         break;
   1467       default:
   1468         break;
   1469     }
   1470     if (consume(TSEMICOLON))
   1471       return true;
   1472     expect(TCOMMA, "or ';' after declarator");
   1473     allowfunc = 0;
   1474   }
   1475 }
   1476 
   1477 struct decl *
   1478 stringdecl(struct expr *expr)
   1479 {
   1480   static struct map strings;
   1481   struct mapkey     key;
   1482   struct decl      *d;
   1483   size_t            i;
   1484 
   1485   if (!strings.len)
   1486     mapinit(&strings, 64);
   1487   assert(expr->kind == EXPRSTRING);
   1488   mapkey(&key, expr->u.string.data, expr->u.string.size);
   1489   if (mapput(&strings, &key, &i)) {
   1490     d        = mkdecl("string", DECLOBJECT, expr->type, QUALNONE, LINKNONE);
   1491     d->value = mkglobal(d);
   1492     emitdata(d, mkinit(0, expr->type->size, (struct bitfield){0}, expr));
   1493     strings.vals[i].p = d;
   1494   } else {
   1495     d = strings.vals[i].p;
   1496   }
   1497   return d;
   1498 }
   1499 
   1500 void
   1501 emittentativedefns(void)
   1502 {
   1503   struct decl *d;
   1504 
   1505   for (d = tentativedefns; d; d = d->next) {
   1506     if (!d->defined)
   1507       defineobj(d, NULL, false, NULL);
   1508   }
   1509 }