scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

decl.c (17770B)


      1 static char sccsid[] = "@(#) ./cc1/decl.c";
      2 #include <stdarg.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 #include <scc/cstd.h>
      7 #include <scc/scc.h>
      8 #include "cc1.h"
      9 
     10 #define NOSCLASS  0
     11 
     12 #define NOREP 0
     13 #define REP 1
     14 #define QUIET   1
     15 #define NOQUIET 0
     16 
     17 #define NR_DCL_TYP (NR_DECLARATORS+NR_FUNPARAM)
     18 #define NR_DCL_SYM (NR_DECLARATORS+NR_FUNPARAM+1)
     19 
     20 struct declarators {
     21 	unsigned nr;
     22 	unsigned ns;
     23 	struct decl *dcl;
     24 	unsigned nr_types;
     25 	Type **tpars;
     26 	Symbol **pars;
     27 	struct declarator {
     28 		unsigned char op;
     29 		TINT  nelem;
     30 		Symbol *sym;
     31 		Type **tpars;
     32 		Symbol **pars;
     33 	} d [NR_DECLARATORS];
     34 };
     35 
     36 struct decl {
     37 	unsigned ns;
     38 	int sclass;
     39 	int qualifier;
     40 	Symbol *sym;
     41 	Type *type;
     42 	Type *parent;
     43 	Symbol **pars;
     44 	Symbol *bufpars[NR_DCL_SYM];
     45 	Type *buftpars[NR_DCL_TYP];
     46 };
     47 
     48 
     49 static void
     50 endfundcl(Type *tp, Symbol **pars)
     51 {
     52 	if (tp->prop&TK_R && *pars)
     53 		warn("parameter names (without types) in function declaration");
     54 	/*
     55 	 * avoid non used warnings in prototypes
     56 	 */
     57 	while (*pars)
     58 		(*pars++)->flags |= SUSED;
     59 	popctx();
     60 }
     61 
     62 static void
     63 push(struct declarators *dp, int op, ...)
     64 {
     65 	va_list va;
     66 	unsigned n;
     67 	struct declarator *p;
     68 
     69 	va_start(va, op);
     70 	if ((n = dp->nr++) == NR_DECLARATORS)
     71 		error("too many declarators");
     72 
     73 	p = &dp->d[n];
     74 	p->op = op;
     75 	p->tpars = NULL;
     76 
     77 	switch (op) {
     78 	case ARY:
     79 		p->nelem = va_arg(va, TINT);
     80 		break;
     81 	case KRFTN:
     82 	case FTN:
     83 		p->nelem = va_arg(va, unsigned);
     84 		p->tpars = va_arg(va, Type **);
     85 		p->pars = va_arg(va, Symbol **);
     86 		break;
     87 	case IDEN:
     88 		p->sym = va_arg(va, Symbol *);
     89 		break;
     90 	}
     91 	va_end(va);
     92 }
     93 
     94 static int
     95 pop(struct declarators *dp, struct decl *dcl)
     96 {
     97 	struct declarator *p;
     98 
     99 	if (dp->nr == 0)
    100 		return 0;
    101 
    102 	p = &dp->d[--dp->nr];
    103 	if (p->op == IDEN) {
    104 		dcl->sym = p->sym;
    105 		return 1;
    106 	}
    107 
    108 	if (dcl->type->op == FTN)
    109 		endfundcl(dcl->type, dcl->pars);
    110 	dcl->pars = p->pars;
    111 
    112 	dcl->type = mktype(dcl->type, p->op, p->nelem, p->tpars);
    113 	return 1;
    114 }
    115 
    116 static void
    117 arydcl(struct declarators *dp)
    118 {
    119 	Node *np = NULL;
    120 	TINT n = 0;
    121 
    122 	expect('[');
    123 	if (yytoken != ']') {
    124 		if ((np = constexpr()) == NULL) {
    125 			errorp("invalid storage size");
    126 		} else {
    127 			if ((n = np->sym->u.i) <= 0) {
    128 				errorp("array size is not a positive number");
    129 				n = 1;
    130 			}
    131 			freetree(np);
    132 		}
    133 	}
    134 	expect(']');
    135 
    136 	push(dp, ARY, n);
    137 }
    138 
    139 static int
    140 empty(Symbol *sym, Type *tp, int param)
    141 {
    142 	if (!sym->name) {
    143 		sym->type = tp;
    144 		switch (tp->op) {
    145 		default:
    146 			 /* warn if it is not a parameter */
    147 			if (!param)
    148 				warn("empty declaration");
    149 		case STRUCT:
    150 		case UNION:
    151 		case ENUM:
    152 			return 1;
    153 		}
    154 	}
    155 	return 0;
    156 }
    157 
    158 static void
    159 bad_storage(Type *tp, char *name)
    160 {
    161 	if (tp->op != FTN)
    162 		errorp("incorrect storage class for file-scope declaration");
    163 	else
    164 		errorp("invalid storage class for function '%s'", name);
    165 }
    166 
    167 static Symbol *
    168 redcl(Symbol *sym, Type *tp, int sclass)
    169 {
    170 	int flags;
    171 	char *name = sym->name;
    172 
    173 	if (!eqtype(sym->type, tp, 1)) {
    174 		errorp("conflicting types for '%s'", name);
    175 		return sym;
    176 	}
    177 
    178 	if (sym->token == TYPEIDEN && sclass != TYPEDEF ||
    179 	    sym->token != TYPEIDEN && sclass == TYPEDEF) {
    180 		goto redeclaration;
    181 	}
    182 	if (curctx != GLOBALCTX && tp->op != FTN) {
    183 		/* is it the redeclaration of a local variable? */
    184 		if ((sym->flags & SEXTERN) && sclass == EXTERN)
    185 			return sym;
    186 		goto redeclaration;
    187 	}
    188 
    189 	flags = sym->flags;
    190 	switch (sclass) {
    191 	case REGISTER:
    192 	case AUTO:
    193 		bad_storage(tp, name);
    194 		break;
    195 	case NOSCLASS:
    196 		if ((flags & SPRIVATE) == 0) {
    197 			if (flags & SEXTERN)
    198 				flags &= ~(SEXTERN|SEMITTED);
    199 			flags |= SGLOBAL;
    200 			break;
    201 		}
    202 		errorp("non-static declaration of '%s' follows static declaration",
    203 		       name);
    204 		break;
    205 	case TYPEDEF:
    206 		/* Only C11 allows multiple definitions of a typedef. */
    207 		goto redeclaration;
    208 	case EXTERN:
    209 		break;
    210 	case STATIC:
    211 		if ((flags & (SGLOBAL|SEXTERN)) == 0) {
    212 			flags |= SPRIVATE;
    213 			break;
    214 		}
    215 		errorp("static declaration of '%s' follows non-static declaration",
    216 		       name);
    217 		break;
    218 	}
    219 	sym->flags = flags;
    220 
    221 	return sym;
    222 
    223 redeclaration:
    224 	errorp("redeclaration of '%s'", name);
    225 	return sym;
    226 }
    227 
    228 static Symbol *
    229 identifier(struct decl *dcl)
    230 {
    231 	Symbol *sym = dcl->sym;
    232 	Type *tp = dcl->type;
    233 	int sclass = dcl->sclass;
    234 	char *name = sym->name;
    235 
    236 	if (empty(sym, tp, 0))
    237 		return sym;
    238 
    239 	/* TODO: Add warning about ANSI limits */
    240 	if (!(tp->prop & TDEFINED)                &&
    241 	    sclass != EXTERN && sclass != TYPEDEF &&
    242 	    !(tp->op == ARY && yytoken == '=')) {
    243 		errorp("declared variable '%s' of incomplete type", name);
    244 	}
    245 
    246 	if (tp->op == FTN) {
    247 		if (sclass == NOSCLASS)
    248 			sclass = EXTERN;
    249 		if (!strcmp(name, "main") && tp->type != inttype) {
    250 			errorp("main shall be defined with a return type of int");
    251 			errorp("please contact __20h__ on irc.freenode.net (#bitreich-en) via IRC");
    252 		}
    253 	}
    254 
    255 	if (sym->flags & SDECLARED) {
    256 		sym = redcl(dcl->sym, tp, sclass);
    257 	} else {
    258 		int flags = sym->flags | SDECLARED;
    259 
    260 		sym->type = tp;
    261 
    262 		switch (sclass) {
    263 		case REGISTER:
    264 		case AUTO:
    265 			if (curctx != GLOBALCTX && tp->op != FTN) {
    266 				flags |= (sclass == REGISTER) ? SREGISTER : SAUTO;
    267 				break;
    268 			}
    269 			bad_storage(tp, name);
    270 		case NOSCLASS:
    271 			if (tp->op == FTN)
    272 				flags |= SEXTERN;
    273 			else
    274 				flags |= (curctx == GLOBALCTX) ? SGLOBAL : SAUTO;
    275 			break;
    276 		case EXTERN:
    277 			flags |= SEXTERN;
    278 			break;
    279 		case STATIC:
    280 			flags |= (curctx == GLOBALCTX) ? SPRIVATE : SLOCAL;
    281 			break;
    282 		case TYPEDEF:
    283 			flags |= STYPEDEF;
    284 			sym->u.token = sym->token = TYPEIDEN;
    285 			break;
    286 		}
    287 		sym->flags = flags;
    288 	}
    289 
    290 	if (accept('='))
    291 		initializer(sym, sym->type);
    292 	if (!(sym->flags & (SGLOBAL|SEXTERN)) && tp->op != FTN)
    293 		sym->flags |= SDEFINED;
    294 	if (sym->token == IDEN && tp->op != FTN)
    295 		emit(ODECL, sym);
    296 	return sym;
    297 }
    298 
    299 static Symbol *
    300 parameter(struct decl *dcl)
    301 {
    302 	Symbol *sym = dcl->sym;
    303 	Type *funtp = dcl->parent, *tp = dcl->type;
    304 	char *name = sym->name;
    305 	int flags;
    306 
    307 	flags = 0;
    308 	switch (dcl->sclass) {
    309 	case STATIC:
    310 	case EXTERN:
    311 	case AUTO:
    312 		errorp("bad storage class in function parameter");
    313 		break;
    314 	case REGISTER:
    315 		flags |= SREGISTER;
    316 		break;
    317 	case NOSCLASS:
    318 		flags |= SAUTO;
    319 		break;
    320 	}
    321 
    322 	switch (tp->op) {
    323 	case VOID:
    324 		funtp->n.elem = 1;
    325 		if (dcl->sclass)
    326 			errorp("void as unique parameter may not be qualified");
    327 		return NULL;
    328 	case ARY:
    329 		tp = mktype(tp->type, PTR, 0, NULL);
    330 		break;
    331 	case FTN:
    332 		errorp("incorrect function type for a function parameter");
    333 		return NULL;
    334 	}
    335 	if (!empty(sym, tp, 1)) {
    336 		int isdcl = sym->flags&SDECLARED, isk_r = funtp->prop & TK_R;
    337 		if (isdcl && !isk_r) {
    338 			errorp("redefinition of parameter '%s'", name);
    339 			return NULL;
    340 		}
    341 		if (!isdcl && isk_r) {
    342 			errorp("declaration for parameter '%s' but no such parameter",
    343 			       sym->name);
    344 			return NULL;
    345 		}
    346 		sym->flags |= SDECLARED;
    347 	}
    348 
    349 	sym->type = tp;
    350 	sym->flags &= ~(SAUTO|SREGISTER);
    351 	sym->flags |= flags;
    352 	return sym;
    353 }
    354 
    355 static Symbol *dodcl(int rep,
    356                      Symbol *(*fun)(struct decl *),
    357                      unsigned ns,
    358                      Type *type);
    359 
    360 static int
    361 krpars(struct declarators *dp)
    362 {
    363 	Symbol *sym;
    364 	int toomany = 0;
    365 	unsigned npars = 0;
    366 
    367 	do {
    368 		sym = yylval.sym;
    369 		expect(IDEN);
    370 		sym->flags |= SAUTO;
    371 		if ((sym = install(NS_IDEN, sym)) == NULL) {
    372 			errorp("redefinition of parameter '%s'",
    373 			       yylval.sym->name);
    374 			continue;
    375 		}
    376 		if (npars < NR_FUNPARAM) {
    377 			++npars;
    378 			*dp->pars++ = sym;
    379 			continue;
    380 		}
    381 		if (!toomany)
    382 		toomany = 1;
    383 	} while (accept(','));
    384 
    385 	return toomany;
    386 }
    387 
    388 static unsigned
    389 krfun(struct declarators *dp)
    390 {
    391 	int toomany = 0;
    392 
    393 
    394 	if (yytoken != ')')
    395 		toomany = krpars(dp);
    396 
    397 	if (dp->nr_types == NR_DCL_TYP) {
    398 		toomany = 1;
    399 	} else {
    400 		++dp->nr_types;
    401 		*dp->tpars++ = ellipsistype;
    402 	}
    403 
    404 	if (toomany)
    405 		errorp("too many parameters in function definition");
    406 	return 1;
    407 }
    408 
    409 static unsigned
    410 ansifun(struct declarators *dp)
    411 {
    412 	Symbol *sym;
    413 	unsigned npars, ntype, toomany, distoomany, voidpar;
    414 	Type type, *tp;
    415 
    416 	type.n.elem = 0;
    417 	type.prop = 0;
    418 	npars = ntype = toomany = distoomany = voidpar = 0;
    419 
    420 	do {
    421 		if (accept(ELLIPSIS)) {
    422 			if (ntype < 1)
    423 				errorp("a named argument is requiered before '...'");
    424 			if (yytoken != ')')
    425 				errorp("... must be the last parameter");
    426 			sym = NULL;
    427 			tp = ellipsistype;
    428 		} else if ((sym = dodcl(NOREP, parameter, NS_IDEN, &type)) == NULL) {
    429 			if (type.n.elem == 1 && ntype > 1)
    430 				voidpar = 1;
    431 			sym = NULL;
    432 			tp = NULL;
    433 		} else {
    434 			tp = sym->type;
    435 		}
    436 
    437 		if (sym) {
    438 			if (npars == NR_FUNPARAM) {
    439 				toomany = 1;
    440 			} else {
    441 				npars++;
    442 				*dp->pars++ = sym;
    443 			}
    444 		}
    445 
    446 		if (tp) {
    447 			if (dp->nr_types == NR_DCL_TYP) {
    448 				toomany = 1;
    449 			} else {
    450 				ntype++;
    451 				dp->nr_types++;
    452 				*dp->tpars++ = tp;
    453 			}
    454 		}
    455 
    456 	} while (accept(','));
    457 
    458 	if (toomany == 1)
    459 		errorp("too many parameters in function definition");
    460 	if (voidpar && ntype > 1)
    461 		errorp("'void' must be the only parameter");
    462 	return ntype;
    463 }
    464 
    465 static int
    466 funbody(Symbol *sym, Symbol *pars[])
    467 {
    468 	Type *tp;
    469 	Symbol **bp, *p;
    470 
    471 	if (!sym)
    472 		return 0;
    473 	tp = sym->type;
    474 	if (tp->op != FTN)
    475 		return 0;
    476 
    477 	switch (yytoken) {
    478 	case '{':
    479 	case TYPE:
    480 	case TYPEIDEN:
    481 		if (curctx != PARAMCTX)
    482 			errorp("nested function declaration");
    483 		if (sym && sym->ns == NS_IDEN)
    484 			break;
    485 	default:
    486 		emit(ODECL, sym);
    487 		endfundcl(tp, pars);
    488 		return  0;
    489 	}
    490 
    491 	tp->prop |= TFUNDEF;
    492 	curfun = sym;
    493 	if (sym->type->prop & TK_R) {
    494 		while (yytoken != '{') {
    495 			dodcl(REP, parameter, NS_IDEN, sym->type);
    496 			expect(';');
    497 		}
    498 		for (bp = pars; p = *bp; ++bp) {
    499 			if (p->type == NULL) {
    500 				warn("type of '%s' defaults to int", p->name);
    501 				p->type = inttype;
    502 			}
    503 		}
    504 	}
    505 	if (sym->flags & STYPEDEF)
    506 		errorp("function definition declared 'typedef'");
    507 	if (sym->flags & SDEFINED)
    508 		errorp("redefinition of '%s'", sym->name);
    509 	if (sym->flags & SEXTERN) {
    510 		sym->flags &= ~SEXTERN;
    511 		sym->flags |= SGLOBAL;
    512 	}
    513 	sym->flags |= SDEFINED;
    514 	sym->flags &= ~SEMITTED;
    515 	sym->u.pars = pars;
    516 	emit(OFUN, sym);
    517 	compound(NULL, NULL, NULL);
    518 	emit(OEFUN, NULL);
    519 	popctx();
    520 	flushtypes();
    521 	curfun = NULL;
    522 	return 1;
    523 }
    524 
    525 static void
    526 fundcl(struct declarators *dp)
    527 {
    528 	Type **types = dp->tpars;
    529 	unsigned ntypes, typefun;
    530 	Symbol **pars = dp->pars;
    531 	unsigned (*fun)(struct declarators *);
    532 
    533 	pushctx();
    534 	expect('(');
    535 	if (yytoken == ')' || yytoken == IDEN) {
    536 		typefun = KRFTN;
    537 		fun = krfun;
    538 	} else {
    539 		typefun = FTN;
    540 		fun = ansifun;
    541 	}
    542 	ntypes = (*fun)(dp);
    543 	*dp->pars++= NULL;
    544 	expect(')');
    545 
    546 	push(dp, typefun, ntypes, types, pars);
    547 }
    548 
    549 static void declarator(struct declarators *dp);
    550 
    551 static void
    552 directdcl(struct declarators *dp)
    553 {
    554 	Symbol *p, *sym;
    555 	static int nested;
    556 
    557 	if (accept('(')) {
    558 		if (nested == NR_SUBTYPE)
    559 			error("too many declarators nested by parentheses");
    560 		++nested;
    561 		declarator(dp);
    562 		--nested;
    563 		expect(')');
    564 	} else {
    565 		if (yytoken == IDEN || yytoken == TYPEIDEN) {
    566 			sym = yylval.sym;
    567 			if (p = install(dp->ns, sym)) {
    568 				sym = p;
    569 				sym->flags &= ~SDECLARED;
    570 			}
    571 			next();
    572 		} else {
    573 			sym = newsym(dp->ns, NULL);
    574 		}
    575 		push(dp, IDEN, sym);
    576 	}
    577 
    578 	for (;;) {
    579 		switch (yytoken) {
    580 		case '(':  fundcl(dp); break;
    581 		case '[':  arydcl(dp); break;
    582 		default:   return;
    583 		}
    584 	}
    585 }
    586 
    587 static void
    588 declarator(struct declarators *dp)
    589 {
    590 	unsigned  n;
    591 
    592 	for (n = 0; accept('*'); ++n) {
    593 		while (accept(TQUALIFIER))
    594 			;
    595 	}
    596 
    597 	directdcl(dp);
    598 
    599 	while (n--)
    600 		push(dp, PTR);
    601 }
    602 
    603 static Type *structdcl(void), *enumdcl(void);
    604 
    605 static Type *
    606 specifier(int *sclass, int *qualifier)
    607 {
    608 	Type *tp = NULL;
    609 	unsigned spec, qlf, sign, type, cls, size;
    610 
    611 	spec = qlf = sign = type = cls = size = 0;
    612 
    613 	for (;;) {
    614 		unsigned *p = NULL;
    615 		Type *(*dcl)(void) = NULL;
    616 
    617 		switch (yytoken) {
    618 		case SCLASS:
    619 			p = &cls;
    620 			break;
    621 		case TQUALIFIER:
    622 			qlf |= yylval.token;
    623 			next();
    624 			continue;
    625 		case TYPEIDEN:
    626 			if (type)
    627 				goto return_type;
    628 			tp = yylval.sym->type;
    629 			p = &type;
    630 			break;
    631 		case TYPE:
    632 			switch (yylval.token) {
    633 			case ENUM:
    634 				dcl = enumdcl;
    635 				p = &type;
    636 				break;
    637 			case STRUCT:
    638 			case UNION:
    639 				dcl = structdcl;
    640 				p = &type;
    641 				break;
    642 			case VA_LIST:
    643 			case VOID:
    644 			case BOOL:
    645 			case CHAR:
    646 			case INT:
    647 			case FLOAT:
    648 			case DOUBLE:
    649 				p = &type;
    650 				break;
    651 			case SIGNED:
    652 			case UNSIGNED:
    653 				p = &sign;
    654 				break;
    655 			case LONG:
    656 				if (size == LONG) {
    657 					yylval.token = LLONG;
    658 					size = 0;
    659 				}
    660 			case SHORT:
    661 				p = &size;
    662 				break;
    663 			}
    664 			break;
    665 		default:
    666 			goto return_type;
    667 		}
    668 		if (*p)
    669 			errorp("invalid type specification");
    670 		*p = yylval.token;
    671 		if (dcl) {
    672 			if (size || sign)
    673 				errorp("invalid type specification");
    674 			tp = (*dcl)();
    675 			goto return_type;
    676 		} else {
    677 			next();
    678 		}
    679 		spec = 1;
    680 	}
    681 
    682 return_type:
    683 	*sclass = cls;
    684 	*qualifier = qlf;
    685 	if (!tp) {
    686 		if (spec) {
    687 			tp = ctype(type, sign, size);
    688 		} else {
    689 			if (curctx != GLOBALCTX)
    690 				unexpected();
    691 			warn("type defaults to 'int' in declaration");
    692 			tp = inttype;
    693 		}
    694 	}
    695 	return tp;
    696 }
    697 
    698 static Symbol *
    699 newtag(void)
    700 {
    701 	Symbol *sym;
    702 	int ns, op, tag = yylval.token;
    703 	static unsigned tpns = NS_STRUCTS;
    704 
    705 	ns = namespace;
    706 	namespace = NS_TAG;
    707 	next();
    708 	namespace = ns;
    709 
    710 	switch (yytoken) {
    711 	case IDEN:
    712 	case TYPEIDEN:
    713 		sym = yylval.sym;
    714 		if ((sym->flags & SDECLARED) == 0)
    715 			install(NS_TAG, yylval.sym);
    716 		next();
    717 		break;
    718 	default:
    719 		sym = newsym(NS_TAG, NULL);
    720 		break;
    721 	}
    722 	if (!sym->type) {
    723 		Type *tp;
    724 
    725 		if (tpns == NS_STRUCTS + NR_MAXSTRUCTS)
    726 			error("too many tags declared");
    727 		tp = mktype(NULL, tag, 0, NULL);
    728 		tp->ns = tpns++;
    729 		sym->type = tp;
    730 		tp->tag = sym;
    731 		DBG("declared tag '%s' with ns = %d\n",
    732 		    (sym->name) ? sym->name : "anonymous", tp->ns);
    733 	}
    734 
    735 	if ((op = sym->type->op) != tag &&  op != INT)
    736 		error("'%s' defined as wrong kind of tag", sym->name);
    737 	return sym;
    738 }
    739 
    740 static void fieldlist(Type *tp);
    741 
    742 static Type *
    743 structdcl(void)
    744 {
    745 	Symbol *sym;
    746 	Type *tp;
    747 	static int nested;
    748 	int ns;
    749 
    750 	sym = newtag();
    751 	tp = sym->type;
    752 
    753 	if (!accept('{'))
    754 		return tp;
    755 
    756 	ns = namespace;
    757 	namespace = tp->ns;
    758 
    759 	if (tp->prop & TDEFINED && sym->ctx == curctx)
    760 		error("redefinition of struct/union '%s'", sym->name);
    761 
    762 	if (nested == NR_STRUCT_LEVEL)
    763 		error("too many levels of nested structure or union definitions");
    764 
    765 	++nested;
    766 	while (yytoken != '}') {
    767 		fieldlist(tp);
    768 	}
    769 	--nested;
    770 
    771 	deftype(tp);
    772 	namespace = ns;
    773 	expect('}');
    774 	return tp;
    775 }
    776 
    777 static Type *
    778 enumdcl(void)
    779 {
    780 	Type *tp;
    781 	Symbol *sym, *tagsym;
    782 	int ns, val, toomany;
    783 	unsigned nctes;
    784 
    785 	ns = namespace;
    786 	tagsym = newtag();
    787 	tp = tagsym->type;
    788 
    789 	if (!accept('{'))
    790 		goto restore_name;
    791 	if (tp->prop & TDEFINED)
    792 		errorp("redefinition of enumeration '%s'", tagsym->name);
    793 	deftype(tp);
    794 	namespace = NS_IDEN;
    795 
    796 	/* TODO: check incorrect values in val */
    797 	for (nctes = val = 0; yytoken != '}'; ++nctes, ++val) {
    798 		if (yytoken != IDEN)
    799 			unexpected();
    800 		sym = yylval.sym;
    801 		next();
    802 		if (nctes == NR_ENUM_CTES && !toomany) {
    803 			errorp("too many enum constants in a single enum");
    804 			toomany = 1;
    805 		}
    806 		if (accept('=')) {
    807 			Node *np = constexpr();
    808 
    809 			if (np == NULL)
    810 				errorp("invalid enumeration value");
    811 			else
    812 				val = np->sym->u.i;
    813 			freetree(np);
    814 		}
    815 		if ((sym = install(NS_IDEN, sym)) == NULL) {
    816 			errorp("'%s' redeclared as different kind of symbol",
    817 			       yytext);
    818 		} else {
    819 			sym->u.i = val;
    820 			sym->flags |= SCONSTANT;
    821 			sym->type = inttype;
    822 		}
    823 		if (!accept(','))
    824 			break;
    825 	}
    826 	expect('}');
    827 
    828 restore_name:
    829 	namespace = ns;
    830 	return tp;
    831 }
    832 
    833 static Symbol *
    834 type(struct decl *dcl)
    835 {
    836 	Symbol *sym = dcl->sym;
    837 
    838 	if (dcl->sclass)
    839 		error("class storage in type name");
    840 	if (sym->name)
    841 		error("unexpected identifier in type name");
    842 	sym->type = dcl->type;
    843 
    844 	return sym;
    845 }
    846 
    847 static Symbol *
    848 field(struct decl *dcl)
    849 {
    850 	static char *anon = "<anonymous>";
    851 	Symbol *sym = dcl->sym;
    852 	char *name = (sym->name) ? sym->name : anon;
    853 	Type *structp = dcl->parent, *tp = dcl->type;
    854 	TINT n = structp->n.elem;
    855 	int err = 0;
    856 
    857 	if (accept(':')) {
    858 		Node *np;
    859 		TINT n;
    860 
    861 		if ((np = constexpr()) == NULL) {
    862 			unexpected();
    863 			n = 0;
    864 		} else {
    865 			n = np->sym->u.i;
    866 			freetree(np);
    867 		}
    868 		if (n == 0 && name != anon)
    869 			errorp("zero width for bit-field '%s'", name);
    870 		if (tp != booltype && tp != inttype && tp != uinttype)
    871 			errorp("bit-field '%s' has invalid type", name);
    872 		if (n < 0)
    873 			errorp("negative width in bit-field '%s'", name);
    874 		else if (n > tp->size*8)
    875 			errorp("width of '%s' exceeds its type", name);
    876 	} else if (empty(sym, tp, 0)) {
    877 		return sym;
    878 	}
    879 
    880 	if (tp->op == FTN) {
    881 		errorp("invalid type '%s' in struct/union", name);
    882 		err = 1;
    883 	}
    884 	if (dcl->sclass) {
    885 		errorp("storage class in struct/union field '%s'", name);
    886 		err = 1;
    887 	}
    888 	if (!(tp->prop & TDEFINED)) {
    889 		error("field '%s' has incomplete type", name);
    890 		err = 1;
    891 	}
    892 	if (err)
    893 		return sym;
    894 
    895 	if (sym->flags & SDECLARED)
    896 		error("duplicated member '%s'", name);
    897 	sym->flags |= SFIELD|SDECLARED;
    898 	sym->type = tp;
    899 
    900 	if (n == NR_FIELDS)
    901 		error("too many fields in struct/union");
    902 	DBG("New field '%s' in namespace %d\n", name, structp->ns);
    903 	structp->p.fields = xrealloc(structp->p.fields, ++n * sizeof(*sym));
    904 	structp->p.fields[n-1] = sym;
    905 	structp->n.elem = n;
    906 
    907 	return sym;
    908 }
    909 
    910 static Symbol *
    911 dodcl(int rep, Symbol *(*fun)(struct decl *), unsigned ns, Type *parent)
    912 {
    913 	Symbol *sym;
    914 	Type *base;
    915 	struct decl dcl;
    916 	struct declarators stack;
    917 
    918 	dcl.ns = ns;
    919 	dcl.parent = parent;
    920 	base = specifier(&dcl.sclass, &dcl.qualifier);
    921 
    922 	do {
    923 		dcl.type = base;
    924 		stack.nr_types = stack.nr = 0;
    925 		stack.tpars = dcl.buftpars;
    926 		stack.pars = dcl.bufpars;
    927 		stack.dcl = &dcl;
    928 		stack.ns = ns;
    929 
    930 		declarator(&stack);
    931 
    932 		while (pop(&stack, &dcl))
    933 			;
    934 		sym = (*fun)(&dcl);
    935 		if (funbody(sym, dcl.pars))
    936 			return sym;
    937 	} while (rep && accept(','));
    938 
    939 	return sym;
    940 }
    941 
    942 void
    943 decl(void)
    944 {
    945 	Symbol *sym;
    946 
    947 	if (accept(';'))
    948 		return;
    949 	sym = dodcl(REP, identifier, NS_IDEN, NULL);
    950 	if (sym->type->prop & TFUNDEF)
    951 		return;
    952 	expect(';');
    953 }
    954 
    955 static void
    956 fieldlist(Type *tp)
    957 {
    958 	if (yytoken != ';')
    959 		dodcl(REP, field, tp->ns, tp);
    960 	expect(';');
    961 }
    962 
    963 Type *
    964 typename(void)
    965 {
    966 	return dodcl(NOREP, type, NS_DUMMY, NULL)->type;
    967 }