scc

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

decl.c (17726B)


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