4 #define add(x,n) (x > inttype->u.sym->u.limits.max.i-(n) ? (overflow=1,x) : x+(n))
5 #define chkoverflow(x,n) ((void)add(x,n))
6 #define bits2bytes(n) (((n) + 7)/8)
9 static List autos, registers;
10 Symbol cfunc; /* current function */
11 Symbol retv; /* return value location for structs */
13 static void checkref(Symbol, void *);
14 static Symbol dclglobal(int, char *, Type, Coordinate *);
15 static Symbol dcllocal(int, char *, Type, Coordinate *);
16 static Symbol dclparam(int, char *, Type, Coordinate *);
17 static Type dclr(Type, char **, Symbol **, int);
18 static Type dclr1(char **, Symbol **, int);
19 static void decl(Symbol (*)(int, char *, Type, Coordinate *));
20 extern void doconst(Symbol, void *);
21 static void doglobal(Symbol, void *);
22 static void doextern(Symbol, void *);
23 static void exitparams(Symbol []);
24 static void fields(Type);
25 static void funcdefn(int, char *, Type, Symbol [], Coordinate);
26 static void initglobal(Symbol, int);
27 static void oldparam(Symbol, void *);
28 static Symbol *parameters(Type);
29 static Type specifier(int *);
30 static Type structdcl(int);
31 static Type tnode(int, Type);
36 for (n = 0; t != EOI; n++)
37 if (kind[t] == CHAR || kind[t] == STATIC
38 || t == ID || t == '*' || t == '(') {
41 if (!(glevel >= 3 || xref))
43 } else if (t == ';') {
44 warning("empty declaration\n");
47 error("unrecognized declaration\n");
51 warning("empty input file\n");
53 static Type specifier(int *sclass) {
54 int cls, cons, sign, size, type, vol;
57 cls = vol = cons = sign = size = type = 0;
64 case REGISTER: if (level <= GLOBAL && cls == 0)
65 error("invalid use of `%k'\n", t);
66 p = &cls; t = gettok(); break;
67 case STATIC: case EXTERN:
68 case TYPEDEF: p = &cls; t = gettok(); break;
69 case CONST: p = &cons; t = gettok(); break;
70 case VOLATILE: p = &vol; t = gettok(); break;
72 case UNSIGNED: p = &sign; t = gettok(); break;
73 case LONG: if (size == LONG) {
77 p = &size; t = gettok(); break;
78 case SHORT: p = &size; t = gettok(); break;
79 case VOID: case CHAR: case INT: case FLOAT:
80 case DOUBLE: p = &type; ty = tsym->type;
82 case ENUM: p = &type; ty = enumdcl(); break;
84 case UNION: p = &type; ty = structdcl(t); break;
86 if (istypename(t, tsym) && type == 0
87 && sign == 0 && size == 0) {
91 && ty->size != ty->type->size) {
93 if (isconst(tsym->type))
95 if (isvolatile(tsym->type))
96 ty = qual(VOLATILE, ty);
109 error("invalid use of `%k'\n", tt);
118 if ((size == SHORT && type != INT)
119 || (size == LONG+LONG && type != INT)
120 || (size == LONG && type != INT && type != DOUBLE)
121 || (sign && type != INT && type != CHAR))
122 error("invalid type specification\n");
123 if (type == CHAR && sign)
124 ty = sign == UNSIGNED ? unsignedchar : signedchar;
125 else if (size == SHORT)
126 ty = sign == UNSIGNED ? unsignedshort : shorttype;
127 else if (size == LONG && type == DOUBLE)
129 else if (size == LONG+LONG) {
130 ty = sign == UNSIGNED ? unsignedlonglong : longlong;
132 warning("`%t' is a non-ANSI type\n", ty);
133 } else if (size == LONG)
134 ty = sign == UNSIGNED ? unsignedlong : longtype;
135 else if (sign == UNSIGNED && type == INT)
138 ty = qual(CONST, ty);
140 ty = qual(VOLATILE, ty);
143 static void decl(Symbol (*dcl)(int, char *, Type, Coordinate *)) {
146 static char stop[] = { CHAR, STATIC, ID, 0 };
148 ty = specifier(&sclass);
149 if (t == ID || t == '*' || t == '(' || t == '[') {
154 if (level == GLOBAL) {
155 Symbol *params = NULL;
156 ty1 = dclr(ty, &id, ¶ms, 0);
157 if (params && id && isfunc(ty1)
158 && (t == '{' || istypename(t, tsym)
159 || (kind[t] == STATIC && t != TYPEDEF))) {
160 if (sclass == TYPEDEF) {
161 error("invalid use of `typedef'\n");
164 if (ty1->u.f.oldstyle)
166 funcdefn(sclass, id, ty1, params, pos);
171 ty1 = dclr(ty, &id, NULL, 0);
173 if (Aflag >= 1 && !hasproto(ty1))
174 warning("missing prototype\n");
176 error("missing identifier\n");
177 else if (sclass == TYPEDEF)
179 Symbol p = lookup(id, identifiers);
180 if (p && p->scope == level)
181 error("redeclaration of `%s'\n", id);
182 p = install(id, &identifiers, level,
183 level < LOCAL ? PERM : FUNC);
189 (void)(*dcl)(sclass, id, ty1, &pos);
195 ty1 = dclr(ty, &id, NULL, 0);
197 } else if (ty == NULL
199 (isstruct(ty) && (*unqual(ty)->u.sym->name < '1' || *unqual(ty)->u.sym->name > '9'))))
200 error("empty declaration\n");
203 static Symbol dclglobal(int sclass, char *id, Type ty, Coordinate *pos) {
208 else if (sclass != EXTERN && sclass != STATIC) {
209 error("invalid storage class `%k' for `%t %s'\n",
213 p = lookup(id, identifiers);
214 if (p && p->scope == GLOBAL) {
215 if (p->sclass != TYPEDEF && eqtype(ty, p->type, 1))
216 ty = compose(ty, p->type);
218 error("redeclaration of `%s' previously declared at %w\n", p->name, &p->src);
220 if (!isfunc(ty) && p->defined && t == '=')
221 error("redefinition of `%s' previously defined at %w\n", p->name, &p->src);
223 if ((p->sclass == EXTERN && sclass == STATIC)
224 || (p->sclass == STATIC && sclass == AUTO)
225 || (p->sclass == AUTO && sclass == STATIC))
226 warning("inconsistent linkage for `%s' previously declared at %w\n", p->name, &p->src);
229 if (p == NULL || p->scope != GLOBAL) {
230 Symbol q = lookup(id, externals);
232 if (sclass == STATIC || !eqtype(ty, q->type, 1))
233 warning("declaration of `%s' does not match previous declaration at %w\n", id, &q->src);
235 p = relocate(id, externals, globals);
238 p = install(id, &globals, GLOBAL, PERM);
242 if (p->sclass != STATIC) {
245 if (Aflag >= 2 && nglobals == 512)
246 warning("more than 511 external identifiers\n");
248 } else if (p->sclass == EXTERN)
252 if (t == '=' && isfunc(p->type)) {
253 error("illegal initialization for `%s'\n", p->name);
255 initializer(p->type, 0);
256 } else if (t == '=') {
258 if (glevel > 0 && IR->stabsym) {
259 (*IR->stabsym)(p); swtoseg(p->u.seg); }
260 } else if (p->sclass == STATIC && !isfunc(p->type)
261 && p->type->size == 0)
262 error("undefined size for `%t %s'\n", p->type, p->name);
265 static void initglobal(Symbol p, int flag) {
268 if (t == '=' || flag) {
269 if (p->sclass == STATIC) {
270 for (ty = p->type; isarray(ty); ty = ty->type)
272 defglobal(p, isconst(ty) ? LIT : DATA);
277 ty = initializer(p->type, 0);
278 if (isarray(p->type) && p->type->size == 0)
280 if (p->sclass == EXTERN)
284 void defglobal(Symbol p, int seg) {
287 if (p->sclass != STATIC)
293 static Type dclr(Type basety, char **id, Symbol **params, int abstract) {
294 Type ty = dclr1(id, params, abstract);
296 for ( ; ty; ty = ty->type)
299 basety = ptr(basety);
302 basety = func(basety, ty->u.f.proto,
306 basety = array(basety, ty->size, 0);
308 case CONST: case VOLATILE:
309 basety = qual(ty->op, basety);
313 if (Aflag >= 2 && basety->size > 32767)
314 warning("more than 32767 bytes in `%t'\n", basety);
317 static Type tnode(int op, Type type) {
325 static Type dclr1(char **id, Symbol **params, int abstract) {
332 error("extraneous identifier `%s'\n", token);
334 case '*': t = gettok(); if (t == CONST || t == VOLATILE) {
336 ty1 = ty = tnode(t, NULL);
337 while ((t = gettok()) == CONST || t == VOLATILE)
339 ty->type = dclr1(id, params, abstract);
342 ty = dclr1(id, params, abstract);
343 ty = tnode(POINTER, ty); break;
344 case '(': t = gettok(); if (abstract
345 && (t == REGISTER || istypename(t, tsym) || t == ')')) {
347 ty = tnode(FUNCTION, ty);
351 args = parameters(ty);
354 ty = dclr1(id, params, abstract);
356 if (abstract && ty == NULL
357 && (id == NULL || *id == NULL))
358 return tnode(FUNCTION, NULL);
363 while (t == '(' || t == '[')
365 case '(': t = gettok(); { Symbol *args;
366 ty = tnode(FUNCTION, ty);
370 args = parameters(ty);
371 if (params && *params == NULL)
377 case '[': t = gettok(); { int n = 0;
381 error("`%d' is an illegal array size\n", n);
386 ty = tnode(ARRAY, ty);
387 ty->size = n; } break;
392 static Symbol *parameters(Type fty) {
396 if (kind[t] == STATIC || istypename(t, tsym)) {
403 if (ty1 && t == ELLIPSIS) {
404 static struct symbol sentinel;
405 if (sentinel.type == NULL) {
406 sentinel.type = voidtype;
407 sentinel.defined = 1;
410 error("illegal formal parameter types\n");
411 list = append(&sentinel, list);
415 if (!istypename(t, tsym) && t != REGISTER)
416 error("missing parameter type\n");
418 ty = dclr(specifier(&sclass), &id, NULL, 1);
419 if ( (ty == voidtype && (ty1 || id))
421 error("illegal formal parameter types\n");
425 list = append(dclparam(sclass, id, ty, &src), list);
426 if (Aflag >= 1 && !hasproto(ty))
427 warning("missing prototype\n");
434 fty->u.f.proto = newarray(length(list) + 1,
435 sizeof (Type *), PERM);
436 params = ltov(&list, FUNC);
437 for (n = 0; params[n]; n++)
438 fty->u.f.proto[n] = params[n]->type;
439 fty->u.f.proto[n] = NULL;
440 fty->u.f.oldstyle = 0;
446 error("expecting an identifier\n");
449 p = dclparam(0, token, inttype, &src);
451 list = append(p, list);
457 params = ltov(&list, FUNC);
458 fty->u.f.proto = NULL;
459 fty->u.f.oldstyle = 1;
462 static char stop[] = { CHAR, STATIC, IF, ')', 0 };
470 static void exitparams(Symbol params[]) {
472 if (params[0] && !params[0]->defined)
473 error("extraneous old-style parameter list\n");
479 static Symbol dclparam(int sclass, char *id, Type ty, Coordinate *pos) {
484 else if (isarray(ty))
488 else if (sclass != REGISTER) {
489 error("invalid storage class `%k' for `%t%s\n",
490 sclass, ty, stringf(id ? " %s'" : "' parameter", id));
492 } else if (isvolatile(ty) || isstruct(ty)) {
493 warning("register declaration ignored for `%t%s\n",
494 ty, stringf(id ? " %s'" : "' parameter", id));
498 p = lookup(id, identifiers);
499 if (p && p->scope == level)
500 error("duplicate declaration for `%s' previously declared at %w\n", id, &p->src);
503 p = install(id, &identifiers, level, FUNC);
509 error("illegal initialization for parameter `%s'\n", id);
515 static Type structdcl(int op) {
529 static char stop[] = { IF, ',', 0 };
530 ty = newstruct(op, tag);
531 ty->u.sym->src = pos;
532 ty->u.sym->defined = 1;
534 if (istypename(t, tsym))
537 error("invalid %k field declarations\n", op);
540 else if (*tag && (p = lookup(tag, types)) != NULL
541 && p->type->op == op) {
543 if (t == ';' && p->scope < level)
544 ty = newstruct(op, tag);
548 error("missing %k tag\n", op);
549 ty = newstruct(op, tag);
555 static void fields(Type ty) {
557 while (istypename(t, tsym)) {
558 static char stop[] = { IF, CHAR, '}', 0 };
559 Type ty1 = specifier(NULL);
563 Type fty = dclr(ty1, &id, NULL, 0);
564 p = newfield(id, ty, fty);
565 if (Aflag >= 1 && !hasproto(p->type))
566 warning("missing prototype\n");
568 if (unqual(p->type) != inttype
569 && unqual(p->type) != unsignedtype) {
570 error("`%t' is an illegal bit-field type\n",
575 p->bitsize = intexpr(0, 0);
576 if (p->bitsize > 8*inttype->size || p->bitsize < 0) {
577 error("`%d' is an illegal bit-field size\n",
579 p->bitsize = 8*inttype->size;
580 } else if (p->bitsize == 0 && id) {
581 warning("extraneous 0-width bit field `%t %s' ignored\n", p->type, id);
583 p->name = stringd(genlabel(1));
589 error("field name missing\n");
590 else if (isfunc(p->type))
591 error("`%t' is an illegal field type\n", p->type);
592 else if (p->type->size == 0)
593 error("undefined size for field `%t %s'\n",
596 if (isconst(p->type))
597 ty->u.sym->u.s.cfields = 1;
598 if (isvolatile(p->type))
599 ty->u.sym->u.s.vfields = 1;
601 if (Aflag >= 2 && n == 128)
602 warning("more than 127 fields in `%t'\n", ty);
609 { int bits = 0, off = 0, overflow = 0;
610 Field p, *q = &ty->u.sym->u.s.flist;
611 ty->align = IR->structmetric.align;
612 for (p = *q; p; p = p->link) {
613 int a = p->type->align ? p->type->align : 1;
615 a = unsignedtype->align;
618 else if (p->bitsize == 0 || bits == 0
619 || bits - 1 + p->bitsize > 8*unsignedtype->size) {
620 off = add(off, bits2bytes(bits-1));
622 chkoverflow(off, a - 1);
623 off = roundup(off, a);
632 if (IR->little_endian)
635 p->lsb = 8*unsignedtype->size - bits + 1
639 off = add(off, p->type->size);
640 if (off + bits2bytes(bits-1) > ty->size)
641 ty->size = off + bits2bytes(bits-1);
643 || !('1' <= *p->name && *p->name <= '9')) {
649 chkoverflow(ty->size, ty->align - 1);
650 ty->size = roundup(ty->size, ty->align);
652 error("size of `%t' exceeds %d bytes\n", ty, inttype->u.sym->u.limits.max.i);
653 ty->size = inttype->u.sym->u.limits.max.i&(~(ty->align - 1));
656 static void funcdefn(int sclass, char *id, Type ty, Symbol params[], Coordinate pt) {
658 Symbol *callee, *caller, p;
659 Type rty = freturn(ty);
661 if (isstruct(rty) && rty->size == 0)
662 error("illegal use of incomplete type `%t'\n", rty);
663 for (n = 0; params[n]; n++)
665 if (n > 0 && params[n-1]->name == NULL)
667 if (Aflag >= 2 && n > 31)
668 warning("more than 31 parameters in function `%s'\n", id);
669 if (ty->u.f.oldstyle) {
671 warning("old-style function definition for `%s'\n", id);
673 callee = newarray(n + 1, sizeof *callee, FUNC);
674 memcpy(callee, caller, (n+1)*sizeof *callee);
676 assert(level == PARAM);
677 while (kind[t] == STATIC || istypename(t, tsym))
679 foreach(identifiers, PARAM, oldparam, callee);
681 for (i = 0; (p = callee[i]) != NULL; i++) {
683 callee[i] = dclparam(0, p->name, inttype, &p->src);
685 caller[i]->sclass = AUTO;
686 caller[i]->type = promote(p->type);
688 p = lookup(id, identifiers);
689 if (p && p->scope == GLOBAL && isfunc(p->type)
690 && p->type->u.f.proto) {
691 Type *proto = p->type->u.f.proto;
692 for (i = 0; caller[i] && proto[i]; i++) {
693 Type ty = unqual(proto[i]);
694 if (eqtype(isenum(ty) ? ty->type : ty,
695 unqual(caller[i]->type), 1) == 0)
697 else if (isenum(ty) && !isenum(unqual(caller[i]->type)))
698 warning("compatibility of `%t' and `%t' is compiler dependent\n",
699 proto[i], caller[i]->type);
701 if (proto[i] || caller[i])
702 error("conflicting argument declarations for function `%s'\n", id);
706 Type *proto = newarray(n + 1, sizeof *proto, PERM);
708 warning("missing prototype for `%s'\n", id);
709 for (i = 0; i < n; i++)
710 proto[i] = caller[i]->type;
712 ty = func(rty, proto, 1);
716 caller = newarray(n + 1, sizeof *caller, FUNC);
717 for (i = 0; (p = callee[i]) != NULL && p->name; i++) {
718 NEW(caller[i], FUNC);
721 caller[i]->type = promote(p->type);
722 caller[i]->sclass = AUTO;
723 if ('1' <= *p->name && *p->name <= '9')
724 error("missing name for parameter %d to function `%s'\n", i + 1, id);
729 for (i = 0; (p = callee[i]) != NULL; i++)
730 if (p->type->size == 0) {
731 error("undefined size for parameter `%t %s'\n",
733 caller[i]->type = p->type = inttype;
735 if (Aflag >= 2 && sclass != STATIC && strcmp(id, "main") == 0) {
736 if (ty->u.f.oldstyle)
737 warning("`%t %s()' is a non-ANSI definition\n", rty, id);
738 else if (!(rty == inttype
739 && ((n == 0 && callee[0] == NULL)
740 || (n == 2 && callee[0]->type == inttype
741 && isptr(callee[1]->type) && callee[1]->type->type == charptype
743 warning("`%s' is a non-ANSI definition\n", typestring(ty, id));
745 p = lookup(id, identifiers);
746 if (p && isfunc(p->type) && p->defined)
747 error("redefinition of `%s' previously defined at %w\n",
749 cfunc = dclglobal(sclass, id, ty, &pt);
750 cfunc->u.f.label = genlabel(1);
751 cfunc->u.f.callee = callee;
755 use(cfunc, cfunc->src);
757 printproto(cfunc, cfunc->u.f.callee);
759 ncalled = findfunc(cfunc->name, pt.file);
760 labels = table(NULL, LABELS);
761 stmtlabs = table(NULL, LABELS);
764 codelist = &codehead;
765 codelist->next = NULL;
766 if (!IR->wants_callb && isstruct(rty))
767 retv = genident(AUTO, ptr(rty), PARAM);
768 compound(0, NULL, 0);
772 for (cp = codelist; cp->kind < Label; cp = cp->prev)
774 if (cp->kind != Jump) {
775 if (rty != voidtype) {
776 warning("missing return value\n");
777 retcode(cnsttree(inttype, 0L));
782 definelab(cfunc->u.f.label);
784 apply(events.exit, cfunc, NULL);
787 assert(level == PARAM);
788 foreach(identifiers, level, checkref, NULL);
789 if (!IR->wants_callb && isstruct(rty)) {
791 a = newarray(n + 2, sizeof *a, FUNC);
793 memcpy(&a[1], callee, (n+1)*sizeof *callee);
795 a = newarray(n + 2, sizeof *a, FUNC);
798 memcpy(&a[1], caller, (n+1)*sizeof *callee);
802 for (i = 0; caller[i]; i++)
803 if (isstruct(caller[i]->type)) {
804 caller[i]->type = ptr(caller[i]->type);
805 callee[i]->type = ptr(callee[i]->type);
806 caller[i]->structarg = callee[i]->structarg = 1;
808 if (glevel > 1) for (i = 0; callee[i]; i++) callee[i]->sclass = AUTO;
809 if (cfunc->sclass != STATIC)
810 (*IR->export)(cfunc);
811 if (glevel && IR->stabsym) {
812 swtoseg(CODE); (*IR->stabsym)(cfunc); }
814 (*IR->function)(cfunc, caller, callee, cfunc->u.f.ncalls);
815 if (glevel && IR->stabfend)
816 (*IR->stabfend)(cfunc, lineno);
817 foreach(stmtlabs, LABELS, checklab, NULL);
820 labels = stmtlabs = NULL;
824 static void oldparam(Symbol p, void *cl) {
828 for (i = 0; callee[i]; i++)
829 if (p->name == callee[i]->name) {
833 error("declared parameter `%s' is missing\n", p->name);
835 void compound(int loop, struct swtch *swp, int lev) {
842 assert(level >= LOCAL);
843 if (level == LOCAL && events.entry)
844 apply(events.entry, cfunc, NULL);
847 autos = registers = NULL;
848 if (level == LOCAL && IR->wants_callb
849 && isstruct(freturn(cfunc->type))) {
850 retv = genident(AUTO, ptr(freturn(cfunc->type)), level);
853 registers = append(retv, registers);
855 while (kind[t] == CHAR || kind[t] == STATIC
856 || (istypename(t, tsym) && getchr() != ':'))
860 Symbol *a = ltov(&autos, STMT);
861 nregs = length(registers);
862 for (i = 0; a[i]; i++)
863 registers = append(a[i], registers);
864 cp->u.block.locals = ltov(®isters, FUNC);
866 if (events.blockentry)
867 apply(events.blockentry, cp->u.block.locals, NULL);
868 while (kind[t] == IF || kind[t] == ID)
869 statement(loop, swp, lev);
871 foreach(identifiers, level, checkref, NULL);
875 for ( ; (p = cp->u.block.locals[i]) != NULL; i++) {
876 for (j = i; j > nregs
877 && cp->u.block.locals[j-1]->ref < p->ref; j--)
878 cp->u.block.locals[j] = cp->u.block.locals[j-1];
879 cp->u.block.locals[j] = p;
882 if (events.blockexit)
883 apply(events.blockexit, cp->u.block.locals, NULL);
884 cp->u.block.level = level;
885 cp->u.block.identifiers = identifiers;
886 cp->u.block.types = types;
887 code(Blockend)->u.begin = cp;
895 static void checkref(Symbol p, void *cl) {
896 if (p->scope >= PARAM
897 && (isvolatile(p->type) || isfunc(p->type)))
899 if (Aflag >= 2 && p->defined && p->ref == 0) {
900 if (p->sclass == STATIC)
901 warning("static `%t %s' is not referenced\n",
903 else if (p->scope == PARAM)
904 warning("parameter `%t %s' is not referenced\n",
906 else if (p->scope >= LOCAL && p->sclass != EXTERN)
907 warning("local `%t %s' is not referenced\n",
910 if (p->sclass == AUTO
911 && ((p->scope == PARAM && regcount == 0)
912 || p->scope >= LOCAL)
913 && !p->addressed && isscalar(p->type) && p->ref >= 3.0)
914 p->sclass = REGISTER;
915 if (level == GLOBAL && p->sclass == STATIC && !p->defined
916 && isfunc(p->type) && p->ref)
917 error("undefined static `%t %s'\n", p->type, p->name);
918 assert(!(level == GLOBAL && p->sclass == STATIC && !p->defined && !isfunc(p->type)));
920 static Symbol dcllocal(int sclass, char *id, Type ty, Coordinate *pos) {
924 sclass = isfunc(ty) ? EXTERN : AUTO;
925 else if (isfunc(ty) && sclass != EXTERN) {
926 error("invalid storage class `%k' for `%t %s'\n",
929 } else if (sclass == REGISTER
930 && (isvolatile(ty) || isstruct(ty) || isarray(ty))) {
931 warning("register declaration ignored for `%t %s'\n",
935 q = lookup(id, identifiers);
936 if ((q && q->scope >= level)
937 || (q && q->scope == PARAM && level == LOCAL)) {
938 if (sclass == EXTERN && q->sclass == EXTERN
939 && eqtype(q->type, ty, 1))
940 ty = compose(ty, q->type);
942 error("redeclaration of `%s' previously declared at %w\n", q->name, &q->src);
945 assert(level >= LOCAL);
946 p = install(id, &identifiers, level, sclass == STATIC || sclass == EXTERN ? PERM : FUNC);
951 case EXTERN: q = lookup(id, globals);
952 if (q == NULL || q->sclass == TYPEDEF || q->sclass == ENUM) {
953 q = lookup(id, externals);
955 q = install(p->name, &externals, GLOBAL, PERM);
962 if (!eqtype(p->type, q->type, 1))
963 warning("declaration of `%s' does not match previous declaration at %w\n", q->name, &q->src);
965 p->u.alias = q; break;
966 case STATIC: (*IR->defsymbol)(p);
969 if (p->type->size > 0) {
971 (*IR->space)(p->type->size);
973 error("undefined size for `%t %s'\n",
976 p->defined = 1; break;
977 case REGISTER: registers = append(p, registers);
981 case AUTO: autos = append(p, autos);
982 p->defined = 1; break;
987 if (sclass == EXTERN)
988 error("illegal initialization of `extern %s'\n", id);
991 if (isscalar(p->type)
992 || (isstruct(p->type) && t != '{')) {
1001 Type ty = p->type, ty1 = ty;
1002 while (isarray(ty1))
1004 if (!isconst(ty) && (!isarray(ty) || !isconst(ty1)))
1005 ty = qual(CONST, ty);
1006 t1 = genident(STATIC, ty, GLOBAL);
1008 if (isarray(p->type) && p->type->size == 0
1009 && t1->type->size > 0)
1010 p->type = array(p->type->type,
1011 t1->type->size/t1->type->type->size, 0);
1014 walk(root(asgn(p, e)), 0, 0);
1017 if (!isfunc(p->type) && p->defined && p->type->size <= 0)
1018 error("undefined size for `%t %s'\n", p->type, id);
1021 void finalize(void) {
1022 foreach(externals, GLOBAL, doextern, NULL);
1023 foreach(identifiers, GLOBAL, doglobal, NULL);
1024 foreach(identifiers, GLOBAL, checkref, NULL);
1025 foreach(constants, CONSTANTS, doconst, NULL);
1027 static void doextern(Symbol p, void *cl) {
1030 static void doglobal(Symbol p, void *cl) {
1031 if (!p->defined && (p->sclass == EXTERN
1032 || (isfunc(p->type) && p->sclass == AUTO)))
1034 else if (!p->defined && !isfunc(p->type)
1035 && (p->sclass == AUTO || p->sclass == STATIC)) {
1036 if (isarray(p->type)
1037 && p->type->size == 0 && p->type->type->size > 0)
1038 p->type = array(p->type->type, 1, 0);
1039 if (p->type->size > 0) {
1041 (*IR->space)(p->type->size);
1042 if (glevel > 0 && IR->stabsym)
1045 error("undefined size for `%t %s'\n",
1051 && !p->generated && p->sclass != EXTERN)
1052 printdecl(p, p->type);
1054 void doconst(Symbol p, void *cl) {
1056 assert(p->u.c.loc->u.seg == 0);
1057 defglobal(p->u.c.loc, LIT);
1058 if (isarray(p->type) && p->type->type == widechar) {
1059 unsigned int *s = p->u.c.v.p;
1060 int n = p->type->size/widechar->size;
1064 (*IR->defconst)(widechar->op, widechar->size, v);
1066 } else if (isarray(p->type))
1067 (*IR->defstring)(p->type->size, p->u.c.v.p);
1069 (*IR->defconst)(p->type->op, p->type->size, p->u.c.v);
1073 void checklab(Symbol p, void *cl) {
1075 error("undefined label `%s'\n", p->name);
1079 Type enumdcl(void) {
1093 static char follow[] = { IF, 0 };
1097 ty = newstruct(ENUM, tag);
1100 error("expecting an enumerator identifier\n");
1104 if (tsym && tsym->scope == level)
1105 error("redeclaration of `%s' previously declared at %w\n",
1113 if (k == inttype->u.sym->u.limits.max.i)
1114 error("overflow in value for enumeration constant `%s'\n", id);
1117 p = install(id, &identifiers, level, level < LOCAL ? PERM : FUNC);
1122 idlist = append(p, idlist);
1124 if (Aflag >= 2 && n == 128)
1125 warning("more than 127 enumeration constants in `%t'\n", ty);
1129 if (Aflag >= 2 && t == '}')
1130 warning("non-ANSI trailing comma in enumerator list\n");
1134 ty->size = ty->type->size;
1135 ty->align = ty->type->align;
1136 ty->u.sym->u.idlist = ltov(&idlist, PERM);
1137 ty->u.sym->defined = 1;
1138 } else if ((p = lookup(tag, types)) != NULL && p->type->op == ENUM) {
1141 error("empty declaration\n");
1143 error("unknown enumeration `%s'\n", tag);
1144 ty = newstruct(ENUM, tag);
1152 Type typename(void) {
1153 Type ty = specifier(NULL);
1155 if (t == '*' || t == '(' || t == '[') {
1156 ty = dclr(ty, NULL, NULL, 1);
1157 if (Aflag >= 1 && !hasproto(ty))
1158 warning("missing prototype\n");