4 dodecl(void (*f)(int,Type*,Sym*), int c, Type *t, Node *n)
17 diag(n, "unknown declarator: %O", n->op);
31 diag(n, "array size must be a positive constant");
34 t->width = v * t->link->width;
55 lastfield = n1->vconst;
57 diag(n, "field width must be non-negative constant");
64 diag(n, "zero width named field");
68 if(!typei[t->etype]) {
69 diag(n, "field type must be int-like");
73 if(lastfield > tfield->width*8) {
74 diag(n, "field width larger than field unit");
78 if(lastbit > tfield->width*8) {
90 if(s->class == CLOCAL)
95 n->xoffset = s->offset;
99 n->etype = n->type->etype;
103 s->varlineno = lineno;
115 if(s->class != CLOCAL)
117 snprint(symb, NSYMB, "%s$%d", s->name, s->block);
119 if(s1->class != CSTATIC) {
121 s1->offset = s->offset;
122 s1->block = s->block;
129 * make a copy of a typedef
130 * the problem is to split out incomplete
131 * arrays so that it is in the variable
132 * rather than the typedef.
147 (et == TARRAY && t->width == 0)) {
156 doinit(Sym *s, Type *t, long o, Node *a)
162 if(s->class == CEXTERN) {
168 print("t = %T; o = %ld; n = %s\n", t, o, s->name);
169 prtree(a, "doinit value");
178 a = init1(s, t, o, 0);
180 diag(initlist, "more initializers than structure: %s",
188 * get next major operator,
189 * dont advance initlist.
209 * consume and return next element on
210 * initlist. expand strings.
228 b = new(OCONST, Z, Z);
229 b->type = a->type->link;
230 if(a->op == OSTRING) {
231 b->vconst = convvtox(*a->cstring, TCHAR);
234 if(a->op == OLSTRING) {
235 b->vconst = convvtox(*a->rstring, TUSHORT);
238 a->type->width -= b->type->width;
239 if(a->type->width <= 0)
248 isstruct(Node *a, Type *t)
255 if(n && n->type && sametype(n->type, t))
265 n = new(ODOTDOT, Z, Z);
269 * ODOTDOT is a flag for tcom
270 * a second tcom will not be performed
279 if(sametype(n->type, t))
285 init1(Sym *s, Type *t, long o, int exflag)
287 Node *a, *l, *r, nod;
296 print("t = %T; o = %ld; n = %s\n", t, o, s->name);
297 prtree(a, "init1 value");
300 if(exflag && a->op == OINIT)
301 return doinit(s, t, o, nextinit());
305 diag(Z, "unknown type in initialization: %T to: %s", t, s->name);
322 if(a->op == OARRAY || a->op == OELEM)
330 diag(Z, "cannot initialize bitfields");
331 if(s->class == CAUTO) {
332 l = new(ONAME, Z, Z);
337 l->etype = s->type->etype;
338 l->xoffset = s->offset + o;
349 if(a->op == OCONST) {
350 if(vconst(a) && t->etype == TIND && a->type && a->type->etype != TIND){
351 diag(a, "initialize pointer to an integer: %s", s->name);
354 if(!sametype(a->type, t)) {
355 /* hoop jumping to save malloc */
357 nodcast = new(OCAST, Z, Z);
361 nod.lineno = a->lineno;
366 if(a->op != OCONST) {
367 diag(a, "initializer is not a constant: %s",
375 if(t->etype == TIND) {
376 while(a->op == OCAST) {
377 warn(a, "CAST in initialization ignored");
380 if(!sametype(t, a->type)) {
381 diag(a, "initialization of incompatible pointers: %s\n%T and %T",
382 s->name, t, a->type);
389 while(a->op == OCAST)
392 warn(a, "initialize pointer to an integer: %s", s->name);
396 diag(a, "initializer is not a constant: %s", s->name);
400 gextern(s, a, o, t->width);
406 if(a->op == OSTRING || a->op == OLSTRING)
407 if(typei[t->link->etype]) {
409 * get rid of null if sizes match exactly
413 so = a->type->width/a->type->link->width;
416 diag(a, "string initialization larger than array");
417 a->type->width -= a->type->link->width;
421 * arrange strings to be expanded
422 * inside OINIT braces.
424 a = new(OUSED, a, Z);
425 return doinit(s, t, o, a);
432 * peek ahead for element initializer
437 if(a->op == OELEM && t->link->etype != TSTRUCT)
439 if(a->op == OARRAY) {
445 if(r->op != OCONST) {
446 diag(r, "initializer subscript must be constant");
451 if(e < 0 || e*w >= t->width) {
452 diag(a, "initialization index out of range: %ld", e);
463 r = init1(s, t->link, o+so, 1);
474 * peek ahead to find type of rhs.
475 * if its a structure, then treat
476 * this element as a variable
477 * rather than an aggregate.
483 diag(Z, "incomplete structure: %s", s->name);
489 for(t1 = t->link; t1 != T; t1 = t1->down) {
490 if(a->op == OARRAY && t1->etype != TARRAY)
493 if(t1->sym != a->sym)
497 r = init1(s, t1, o+t1->offset, 1);
505 if(a && a->op == OELEM)
506 diag(a, "structure element not found %F", a);
512 newlist(Node *l, Node *r)
518 return new(OLIST, l, r);
533 for(l = t->link; l != T; l = l->down) {
536 l->shift = -l->shift;
537 w = round(w, tfield->width);
544 l->width == 0 && l->down != T)
546 diag(Z, "incomplete structure element: %s",
549 diag(Z, "incomplete structure element");
550 w = align(w, l, Ael1);
552 w = align(w, l, Ael2);
555 w = align(w, t, Asu2);
564 for(l = t->link; l != T; l = l->down) {
567 diag(Z, "incomplete union element: %s",
570 diag(Z, "incomplete union element");
573 o = align(align(0, l, Ael1), l, Ael2);
577 w = align(w, t, Asu2);
584 diag(Z, "unknown type in sualign: %T", t);
594 if(w <= 0 || w > 8) {
595 diag(Z, "rounding by %d", w);
613 tl = ofnproto(n->left);
614 tr = ofnproto(n->right);
621 t = copytyp(n->sym->type);
632 argmark(Node *n, int pass)
636 autoffset = align(0, thisfn->link, Aarg0);
638 for(; n->left != Z; n = n->left) {
639 if(n->op != OFUNC || n->left->op != ONAME)
641 walkparam(n->right, pass);
642 if(pass != 0 && anyproto(n->right) == OLDPROTO) {
643 t = typ(TFUNC, n->left->sym->type->link);
644 t->down = typ(TOLD, T);
645 t->down->down = ofnproto(n->right);
646 tmerge(t, n->left->sym);
647 n->left->sym->type = t;
656 walkparam(Node *n, int pass)
661 if(n != Z && n->op == OPROTO && n->left == Z && n->type == types[TVOID])
669 diag(n, "argument not a name/prototype: %O", n->op);
673 walkparam(n->left, pass);
678 for(n1 = n; n1 != Z; n1=n1->left)
679 if(n1->op == ONAME) {
686 dodecl(pdecl, CPARAM, n->type, n->left);
694 * allow no name in argument declaration
695 diag(Z, "no name in argument declaration");
699 dodecl(NODECL, CPARAM, n->type, n->left);
700 pdecl(CPARAM, lastdcl, S);
713 if(s->offset != -1) {
716 firstargtype = s->type;
718 autoffset = align(autoffset, s->type, Aarg1);
719 s->offset = autoffset;
720 autoffset = align(autoffset, s->type, Aarg2);
722 dodecl(pdecl, CXXX, types[TINT], n);
735 d->offset = autoffset;
751 diag(Z, "pop off dcl stack");
758 autoffset = d->offset;
764 print("revert1 \"%s\"\n", s->name);
766 nearln = s->varlineno;
767 if(s->class == CAUTO)
768 warn(Z, "auto declared and not used: %s", s->name);
769 if(s->class == CPARAM)
770 warn(Z, "param declared and not used: %s", s->name);
772 if(s->type && (s->type->garb & GVOLATILE)) {
773 n1 = new(ONAME, Z, Z);
778 n1->etype = n1->type->etype;
779 n1->xoffset = s->offset;
780 n1->class = s->class;
782 n1 = new(OADDR, n1, Z);
783 n1 = new(OUSED, n1, Z);
787 n = new(OLIST, n1, n);
791 s->offset = d->offset;
793 s->varlineno = d->varlineno;
799 print("revert2 \"%s\"\n", s->name);
801 s->sueblock = d->block;
806 print("revert3 \"%s\"\n", s->name);
807 if(s->label && s->label->addable == 0)
808 warn(s->label, "label declared and not used \"%s\"", s->name);
821 r = anyproto(n->right);
822 if(r == 0 || (r & OLDPROTO)) {
824 diag(n, "mixed ansi/old function declaration: %F", n->left);
827 return fnproto1(n->right);
842 r |= anyproto(n->left);
848 return r | ANSIPROTO;
862 t = fnproto1(n->left);
864 t->down = fnproto1(n->right);
869 dodecl(NODECL, CXXX, n->type, n->left);
872 *t = *paramconv(lastdcl, 1);
876 diag(n, "incomplete argument prototype");
882 diag(n, "unknown op in fnproto");
889 print("decl \"%s\": C=%s [B=%d:O=%ld] T=%T\n",
890 s->name, cnames[s->class], s->block, s->offset, s->type);
898 d = alloc(sizeof(*d));
914 d->offset = s->offset;
916 d->varlineno = s->varlineno;
922 sametype(Type *t1, Type *t2)
927 return rsametype(t1, t2, 5, 1);
931 rsametype(Type *t1, Type *t2, int n, int f)
939 if(t1 == T || t2 == T)
947 if(!rsametype(t1->link, t2->link, n, 0))
951 while(t1 != T && t2 != T) {
952 if(t1->etype == TOLD) {
956 if(t2->etype == TOLD) {
960 while(t1 != T || t2 != T) {
961 if(!rsametype(t1, t2, n, 0))
971 if(t1->width != t2->width && t1->width != 0 && t2->width != 0)
978 if(t1 != t2 && t1->link == T && t2->link == T){
979 /* structs with missing or different tag names aren't considered equal */
980 if(t1->tag == nil || t2->tag == nil ||
981 strcmp(t1->tag->name, t2->tag->name) != 0)
989 if(!rsametype(t1, t2, n, 0))
997 if((f || !debug['V']) && et == TIND) {
998 if(t1 != T && t1->etype == TVOID)
1000 if(t2 != T && t2->etype == TVOID)
1006 typedef struct Typetab Typetab;
1014 sigind(Type *t, Typetab *tt)
1017 Type **a, **na, **p, **e;
1022 /* linear search seems ok */
1023 for(p = a ; p < e; p++)
1027 na = malloc((n+16)*sizeof(Type*));
1028 memmove(na, a, n*sizeof(Type*));
1037 signat(Type *t, Typetab *tt)
1044 for(; t; t=t->link) {
1045 s = s*thash1 + thash[t->etype];
1046 if(t->garb&GINCOMPLETE)
1052 s = s*thash2 + 0; /* was t->width */
1055 for(t1=t->down; t1; t1=t1->down)
1056 s = s*thash3 + signat(t1, tt);
1060 if((i = sigind(t, tt)) >= 0){
1064 for(t1=t->link; t1; t1=t1->down)
1065 s = s*thash3 + signat(t1, tt);
1093 if(s->sig == SIGINTERN)
1095 if((t = s->type) == T)
1106 if(typesu[t->etype])
1107 if(t->link == T && t->tag && t->tag->suetag) {
1108 t->link = t->tag->suetag->link;
1109 t->width = t->tag->suetag->width;
1114 dotag(Sym *s, int et, int bn)
1118 if(bn != 0 && bn != s->sueblock) {
1122 d->type = s->suetag;
1123 d->block = s->sueblock;
1126 if(s->suetag == T) {
1127 s->suetag = typ(et, T);
1128 s->sueblock = autobn;
1130 if(s->suetag->etype != et)
1131 diag(Z, "tag used for more than one type: %s",
1133 if(s->suetag->tag == S)
1139 dcllabel(Sym *s, int f)
1148 diag(Z, "label reused: %s", s->name);
1149 n->complex = 1; // declared
1151 n->addable = 1; // used
1167 n = new(OXXX, Z, Z);
1179 paramconv(Type *t, int f)
1184 t = typ(TIND, t->link);
1185 t->width = types[TIND]->width;
1190 t->width = types[TIND]->width;
1214 adecl(int c, Type *t, Sym *s)
1219 if(t->etype == TFUNC) {
1224 if(c == CAUTO || c == CEXREG)
1225 diag(Z, "function cannot be %s %s", cnames[c], s->name);
1230 if(s->class == CSTATIC)
1231 if(c == CEXTERN || c == CGLOBL) {
1232 warn(Z, "just say static: %s", s->name);
1235 if(s->class == CAUTO || s->class == CPARAM || s->class == CLOCAL)
1236 if(s->block == autobn)
1237 diag(Z, "auto redeclaration of: %s", s->name);
1248 autoffset = align(autoffset, t, Aaut3);
1249 stkoff = maxround(stkoff, autoffset);
1250 s->offset = -autoffset;
1254 if(autoffset == 0) {
1258 autoffset = align(autoffset, t, Aarg1);
1260 s->offset = autoffset;
1261 autoffset = align(autoffset, t, Aarg2);
1267 pdecl(int c, Type *t, Sym *s)
1269 if(s && s->offset != -1) {
1270 diag(Z, "not a parameter: %s", s->name);
1273 t = paramconv(t, c==CPARAM);
1277 diag(Z, "parameter cannot have class: %s", s->name);
1280 if(typesu[t->etype] && t->width <= 0)
1281 diag(Z, "incomplete structure: %s", t->tag->name);
1286 xdecl(int c, Type *t, Sym *s)
1296 if(s->class == CGLOBL)
1301 if(s->class == CGLOBL)
1307 if(s->class == CEXTERN)
1312 diag(Z, "overspecified class: %s %s %s", s->name, cnames[c], cnames[s->class]);
1317 if(!typesuv[t->etype]) {
1318 diag(Z, "typestr must be struct/union: %s", s->name);
1325 if(s->class == CSTATIC)
1326 if(c == CEXTERN || c == CGLOBL) {
1327 warn(Z, "overspecified class: %s %s %s", s->name, cnames[c], cnames[s->class]);
1331 if(s->class != c || !sametype(t, s->type) || t->etype == TENUM) {
1332 diag(Z, "external redeclaration of: %s", s->name);
1333 Bprint(&diagbuf, " %s %T %L\n", cnames[c], t, nearln);
1334 Bprint(&diagbuf, " %s %T %L\n", cnames[s->class], s->type, s->varlineno);
1344 tmerge(Type *t1, Sym *s)
1349 /*print("merge %T; %T\n", t1, t2);/**/
1351 if(t1 == T || t2 == T || t1 == t2)
1353 if(t1->etype != t2->etype)
1365 while(ta != T && tb != T) {
1368 /* ignore old-style flag */
1369 if(ta->etype == TOLD) {
1373 if(tb->etype == TOLD) {
1377 /* checking terminated by ... */
1378 if(ta->etype == TDOT && tb->etype == TDOT) {
1383 if(!sametype(ta, tb))
1389 diag(Z, "function inconsistently declared: %s", s->name);
1391 /* take new-style over old-style */
1394 if(ta != T && ta->etype == TOLD)
1395 if(tb != T && tb->etype != TOLD)
1400 /* should we check array size change? */
1401 if(t2->width > t1->width)
1402 t1->width = t2->width;
1415 edecl(int c, Type *t, Sym *s)
1420 if(!typesu[t->etype])
1421 diag(Z, "unnamed structure element must be struct/union");
1423 diag(Z, "unnamed structure element cannot have class");
1426 diag(Z, "structure element cannot have class: %s", s->name);
1432 t->shift = lastbit - lastfield;
1433 t->nbits = lastfield;
1435 t->shift = -t->shift;
1437 t->etype = tufield->etype;
1439 t->etype = tfield->etype;
1449 * this routine is very suspect.
1450 * ansi requires the enum type to
1451 * be represented as an 'int'
1452 * this means that 0x81234567
1453 * would be illegal. this routine
1454 * makes signed and unsigned go
1458 maxtype(Type *t1, Type *t2)
1465 if(t1->etype > t2->etype)
1471 doenum(Sym *s, Node *n)
1476 if(n->op != OCONST) {
1477 diag(n, "enum not a constant: %s", s->name);
1481 en.tenum = maxtype(en.cenum, en.tenum);
1483 if(!typefd[en.cenum->etype])
1484 en.lastenum = n->vconst;
1486 en.floatenum = n->fconst;
1490 xdecl(CXXX, types[TENUM], s);
1493 en.tenum = types[TINT];
1494 en.cenum = types[TINT];
1497 s->tenum = en.cenum;
1499 if(!typefd[s->tenum->etype]) {
1500 s->vconst = convvtox(en.lastenum, s->tenum->etype);
1503 s->fconst = en.floatenum;
1513 symadjust(Sym *s, Node *n, long del)
1519 symadjust(s, n->left, del);
1521 symadjust(s, n->right, del);
1539 contig(Sym *s, Node *n, long v)
1541 Node *p, *r, *q, *m;
1546 print("contig v = %ld; s = %s\n", v, s->name);
1547 prtree(n, "doinit value");
1555 * nightmare: an automatic array whose size
1556 * increases when it is initialized
1560 diag(n, "automatic adjustable array: %s", s->name);
1562 autoffset = align(autoffset, s->type, Aaut3);
1563 s->offset = -autoffset;
1564 stkoff = maxround(stkoff, autoffset);
1565 symadjust(s, n, v - s->offset);
1567 if(w <= ewidth[TIND])
1570 diag(Z, "oops in contig");
1571 /*ZZZ this appears incorrect
1572 need to check if the list completely covers the data.
1579 if(n->left->type->width == w)
1581 while(w & (ewidth[TIND]-1))
1584 * insert the following code, where long becomes vlong if pointers are fat
1586 *(long**)&X = (long*)((char*)X + sizeof(X));
1590 } while(*(long**)&X);
1593 for(q=n; q->op != ONAME; q=q->left)
1596 zt = ewidth[TIND] > ewidth[TLONG]? types[TVLONG]: types[TLONG];
1598 p = new(ONAME, Z, Z);
1600 p->type = typ(TIND, zt);
1601 p->xoffset = s->offset;
1603 r = new(ONAME, Z, Z);
1605 r = new(OPOSTDEC, r, Z);
1607 q = new(ONAME, Z, Z);
1609 q = new(OIND, q, Z);
1611 m = new(OCONST, Z, Z);
1617 r = new(OLIST, r, q);
1619 q = new(ONAME, Z, Z);
1621 r = new(ODWHILE, q, r);
1623 q = new(ONAME, Z, Z);
1625 q->type = q->type->link;
1627 q = new(OADDR, q, 0);
1629 q = new(OASI, p, q);
1630 r = new(OLIST, q, r);
1632 n = new(OLIST, r, n);