3 typedef struct Com Com;
10 int compar(Node*, int);
11 static void comma(Node*);
12 static Node* commas(Com*, Node*);
24 prtree(n, "pre complex");
31 prtree(n, "t complex");
35 prtree(n, "c complex");
39 prtree(n, "a complex");
43 prtree(n, "x complex");
48 * evaluate lvalues (addable == 1)
61 return tcomo(n, ADDROF);
81 diag(n, "unknown op in type complex: %O", n->op);
86 * tcom has already been called on this subtree
96 if(n->type->width == types[TLONG]->width) {
97 if(tcomo(l, ADDROF|CASTOF))
104 if(tcompat(n, l->type, n->type, tcast))
110 if(n->type->etype != TVOID)
111 warn(n, "null return of a typed function");
117 if(tcompat(n, n->type, l->type, tasign))
119 constas(n, n->type, l->type);
120 if(!sametype(n->type, l->type)) {
121 l = new1(OCAST, l, Z);
127 case OASI: /* same as as, but no test for const */
134 if(tlvalue(l) || tcompat(n, l->type, r->type, tasign))
136 if(!sametype(l->type, r->type)) {
137 r = new1(OCAST, r, Z);
153 if(tcompat(n, l->type, r->type, tasign))
155 constas(n, l->type, r->type);
156 if(!sametype(l->type, r->type)) {
157 r = new1(OCAST, r, Z);
173 typeext1(l->type, r);
174 if(tcompat(n, l->type, r->type, tasadd))
176 constas(n, l->type, r->type);
179 while(n->left->op == OCAST)
180 n->left = n->left->left;
181 if(!mixedasop(t, n->type)) {
182 if(!sametype(t, n->type)) {
183 r = new1(OCAST, n->right, Z);
203 typeext1(l->type, r);
204 if(tcompat(n, l->type, r->type, tmul))
206 constas(n, l->type, r->type);
209 while(n->left->op == OCAST)
210 n->left = n->left->left;
211 if(!mixedasop(t, n->type)) {
212 if(!sametype(t, n->type)) {
213 r = new1(OCAST, n->right, Z);
222 if(typeu[n->type->etype]) {
240 if(tcompat(n, l->type, r->type, tand))
243 n->right = new1(OCAST, r, Z);
244 n->right->type = types[TINT];
245 if(typeu[n->type->etype]) {
263 if(tcompat(n, l->type, r->type, tand))
267 while(n->left->op == OCAST)
268 n->left = n->left->left;
269 if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
270 r = new1(OCAST, n->right, Z);
275 if(typeu[n->type->etype]) {
291 if(tcompat(n, l->type, types[TINT], tadd))
294 if(n->type->etype == TIND)
295 if(n->type->link->width < 1) {
297 if(n->type->link->width < 1)
298 diag(n, "inc/dec of a void pointer");
311 if(tcompat(n, l->type, r->type, trel))
314 n->type = types[TINT];
326 typeext1(l->type, r);
327 typeext1(r->type, l);
328 if(tcompat(n, l->type, r->type, trel))
331 if(typeu[n->type->etype])
332 n->op = logrel[relindex(n->op)];
333 n->type = types[TINT];
339 if(o | tcom(r->right))
341 if(r->right->type->etype == TIND && vconst(r->left) == 0) {
342 r->left->type = r->right->type;
345 if(r->left->type->etype == TIND && vconst(r->right) == 0) {
346 r->right->type = r->left->type;
347 r->right->vconst = 0;
349 if(sametype(r->right->type, r->left->type)) {
350 r->type = r->right->type;
354 if(tcompat(r, r->left->type, r->right->type, trel))
366 if(tcompat(n, l->type, r->type, tadd))
377 if(tcompat(n, l->type, r->type, tsub))
391 if(tcompat(n, l->type, r->type, tmul))
394 if(typeu[n->type->etype]) {
410 if(tcompat(n, l->type, r->type, tand))
414 n->right = new1(OCAST, r, Z);
415 n->right->type = types[TINT];
416 if(typeu[n->type->etype])
429 if(tcompat(n, l->type, r->type, tand))
441 if(tcompat(n, l->type, r->type, tand))
444 if(typeu[n->type->etype])
455 l = new(OCONST, Z, Z);
457 l->type = types[TINT];
464 if(tcompat(n, l->type, r->type, tsub))
477 l = new(OCONST, Z, Z);
479 l->type = types[TINT];
486 if(tcompat(n, l->type, r->type, tsub))
500 l = new(OCONST, Z, Z);
502 l->type = types[TINT];
509 if(tcompat(n, l->type, r->type, tand))
520 if(tcompat(n, T, l->type, tnot))
522 n->type = types[TINT];
530 if(tcompat(n, T, l->type, tnot) |
531 tcompat(n, T, r->type, tnot))
533 n->type = types[TINT];
544 case OSIGN: /* extension signof(type) returns a hash */
546 if(l->op != OSTRING && l->op != OLSTRING)
550 diag(n, "signof bitfield");
557 if(n->type->width < 0) {
558 diag(n, "signof undefined type");
564 n->vconst = convvtox(signature(n->type), TULONG);
565 n->type = types[TULONG];
570 if(l->op != OSTRING && l->op != OLSTRING)
574 diag(n, "sizeof bitfield");
581 if(n->type->width <= 0) {
582 diag(n, "sizeof undefined type");
585 if(n->type->etype == TFUNC) {
586 diag(n, "sizeof function");
592 n->vconst = convvtox(n->type->width, TINT);
593 n->type = types[TINT];
600 if(l->type->etype == TIND && l->type->link->etype == TFUNC) {
601 l = new1(OIND, l, Z);
602 l->type = l->left->type->link;
605 if(tcompat(n, T, l->type, tfunct))
607 if(o | tcoma(l, r, l->type->down, 1))
609 n->type = l->type->link;
611 if(l->type->down == T){
614 diag(n, "function not declared: %F", l);
615 }else if(l->type->down->etype == TOLD) {
617 diag(n, "function args not checked: %F", l);
625 diag(n, "name not declared: %F", n);
628 if(n->type->etype == TENUM) {
630 n->type = n->sym->tenum;
631 if(!typefd[n->type->etype])
632 n->vconst = n->sym->vconst;
634 n->fconst = n->sym->fconst;
638 if(n->class == CEXREG) {
642 n->reg = n->sym->offset;
649 if(n->type->link != types[TRUNE]) {
653 outlstring(str, sizeof(Rune));
654 o = outlstring(0, 0);
658 n->xoffset = outlstring(n->rstring, n->type->width);
663 if(n->type->link != types[TCHAR]) {
671 n->xoffset = outstring(n->cstring, n->type->width);
681 if(tcompat(n, T, l->type, tdot))
693 diag(n, "address of a bit field");
696 if(l->op == OREGISTER) {
697 diag(n, "address of a register");
700 n->type = typ(TIND, l->type);
701 n->type->width = types[TIND]->width;
707 if(tcompat(n, T, l->type, tindir))
709 n->type = l->type->link;
724 if(typesu[t->etype] && t->tag)
725 diag(n, "structure not fully declared %s", t->tag->name);
727 diag(n, "structure not fully declared");
731 if(typeaf[t->etype]) {
735 warn(n, "address of array/func ignored");
742 l = new1(OXXX, Z, Z);
745 if(l->type->etype == TARRAY)
746 l->type = l->type->link;
750 n->type = typ(TIND, l->type);
751 n->type->width = types[TIND]->width;
760 tcoma(Node *l, Node *n, Type *t, int f)
766 if(t->etype == TOLD || t->etype == TDOT) /* .../old in prototype */
769 if(t != T && !sametype(t, types[TVOID])) {
770 diag(n, "not enough function arguments: %F", l);
776 o = tcoma(l, n->left, t, 0);
782 return o | tcoma(l, n->right, t, 1);
785 tcoma(l, Z, t->down, 0);
786 if(tcom(n) || tcompat(n, T, n->type, targ))
788 if(sametype(t, types[TVOID])) {
789 diag(n, "too many function arguments: %F", l);
794 if(stcompat(nodproto, t, n->type, tasign)) {
795 diag(l, "argument prototype mismatch \"%T\" for \"%T\": %F",
811 switch(n->type->etype)
826 if(t != T && !sametype(t, n->type)) {
827 n1 = new1(OXXX, Z, Z);
845 t = dotsearch(n->sym, n->left->type->link, n, &o);
847 diag(n, "not a member of struct/union: %F", n);
858 Node *l, *r, **ar, **al;
862 if(n->type->etype != TSTRUCT) {
863 diag(n, "constructor must be a structure");
869 for(t = n->type->link; t != T; t = t->down) {
871 diag(n, "constructor list too short");
887 if(tcompat(n, t, r->type, tasign))
889 constas(n, t, r->type);
890 if(!e && !sametype(t, r->type)) {
891 r = new1(OCAST, r, Z);
897 diag(n, "constructor list too long");
908 diag(n, "not an l-value");
915 * hoist comma operators out of expressions
916 * (a,b) OP c => (a, b OP c)
917 * OP(a,b) => (a, OP b)
918 * a OP (b,c) => (b, a OP c)
922 comargs(Com *com, Node *n)
924 if(n != Z && n->op == OLIST){
925 n->left = comargs(com, n->left);
926 n->right = comargs(com, n->right);
928 return commas(com, n);
932 commas(Com *com, Node *n)
948 t = commas(com, n->left);
949 if(com->n >= nelem(com->t))
950 fatal(n, "comma list overflow");
951 com->t[com->n++] = t;
952 return commas(com, n->right);
955 n->left = commas(com, n->left);
956 n->right = comargs(com, n->right);
960 n->left = commas(com, n->left);
961 comma(n->right->left);
962 comma(n->right->right);
967 n->left = commas(com, n->left);
975 n->left = commas(com, n->left);
977 n->right = commas(com, n->right);
988 nn = commas(&com, n);
990 if(debug['y'])print("n=%d\n", com.n);
991 if(debug['y']) prtree(nn, "res");
995 if(debug['y']) prtree(com.t[com.n-1], "tree");
996 nn = new1(OXXX, Z, Z);
1000 n->left = com.t[--com.n];
1002 n->lineno = n->left->lineno;
1004 if(debug['y']) prtree(n, "final");
1006 fatal(n, "odd tree");
1011 * (IND(ADDR x)) ==> x
1012 * (ADDR(IND x)) ==> x
1013 * remove some zero operands
1014 * remove no op casts
1015 * evaluate constants
1047 if(n->op == OASLSHR || n->op == OASASHR || n->op == OASASHL)
1048 if(r->op == OCONST) {
1049 t = n->type->width * 8; /* bits per byte */
1050 if(r->vconst >= t || r->vconst < 0)
1051 warn(n, "stupid shift: %lld", r->vconst);
1057 if(l->op == OCONST) {
1062 if(nocast(l->type, n->type) &&
1063 (!typefd[l->type->etype] || typeu[l->type->etype] && typeu[n->type->etype])) {
1089 l->left->type = n->type;
1097 if(l->op == OADDR) {
1098 l->left->type = n->type;
1118 if(compar(n, 0) || compar(n, 1))
1128 if(vconst(l) == 0 && !side(r)) {
1133 if(vconst(r) == 0) {
1137 if(r->op == OCONST) {
1138 t = n->type->width * 8; /* bits per byte */
1139 if(r->vconst >= t || r->vconst <= -t)
1140 warn(n, "stupid shift: %lld", r->vconst);
1148 if(t == 0 && !side(r)) {
1158 if(t == 0 && !side(l)) {
1171 if(vconst(l) == 0 && !side(r)) {
1178 diag(n, "divide check");
1190 if(r->op == OCONST) {
1191 if(typefd[r->type->etype]) {
1193 r->fconst = -r->fconst;
1197 r->vconst = -r->vconst;
1208 if(vconst(l) == 0) {
1213 if(vconst(r) == 0) {
1222 if(vconst(l) == 0 && !side(r)) {
1226 if(vconst(r) == 0 && !side(l)) {
1232 /* look for commutative constant */
1233 if(r->op == OCONST) {
1234 if(l->op == n->op) {
1235 if(l->left->op == OCONST) {
1236 n->right = l->right;
1240 if(l->right->op == OCONST) {
1247 if(l->op == OCONST) {
1248 if(r->op == n->op) {
1249 if(r->left->op == OCONST) {
1254 if(r->right->op == OCONST) {
1265 if(vconst(l) == 0) {
1274 if(l->op == OCONST && l->vconst != 0) {
1298 /* OEQ, ONE, OLE, OLS, OLT, OLO, OGE, OHS, OGT, OHI */
1299 static char *cmps[12] =
1301 "==", "!=", "<=", "<=", "<", "<", ">=", ">=", ">", ">",
1304 /* 128-bit numbers */
1305 typedef struct Big Big;
1333 if(y > 0 && x.b < ob)
1335 if(y < 0 && x.b > ob)
1341 big(vlong a, uvlong b)
1351 compar(Node *n, int reverse)
1355 char xbuf[40], cmpbuf[50];
1360 * The point of this function is to diagnose comparisons
1361 * that can never be true or that look misleading because
1362 * of the `usual arithmetic conversions'. As an example
1363 * of the latter, if x is a ulong, then if(x <= -1) really means
1364 * if(x <= 0xFFFFFFFF), while if(x <= -1LL) really means
1365 * what it says (but 8c compiles it wrong anyway).
1371 op = comrel[relindex(n->op)];
1379 * Skip over left casts to find out the original expression range.
1381 while(l->op == OCAST){
1384 if(lt == T || rt == T)
1386 if(lt->width < rt->width)
1388 if(lt->width == rt->width && ((lt->etype ^ rt->etype) & 1) != 0)
1395 if(lt == T || lt->etype == TXXX || lt->etype > TUVLONG)
1399 * Skip over the right casts to find the on-screen value.
1403 while(r->oldop == OCAST && !r->xcast)
1411 if((rt->etype&1) && r->vconst < 0) /* signed negative */
1416 lo = big(~0ULL, -(1LL<<(lt->width*8-1)));
1417 hi = big(0, (1LL<<(lt->width*8-1))-1);
1424 hi = big(0, (1LL<<(lt->width*8))-1);
1434 if(cmp(x, add(hi, 1)) >= 0)
1441 if(cmp(x, add(lo, -1)) <= 0)
1449 * Don't warn about comparisons if the expression
1450 * is as wide as the value: the compiler-supplied casts
1451 * will make both outcomes possible.
1453 if(lt->width >= rt->width && debug['w'] < 2)
1455 if(cmp(x, lo) < 0 || cmp(x, hi) > 0)
1462 if((x.a==0 && x.b<=9) || (x.a==~0LL && x.b >= -9ULL))
1463 snprint(xbuf, sizeof xbuf, "%lld", x.b);
1465 snprint(xbuf, sizeof xbuf, "%#llux", x.b);
1467 snprint(xbuf, sizeof xbuf, "%#llx", x.b);
1469 snprint(cmpbuf, sizeof cmpbuf, "%s %s %T",
1470 xbuf, cmps[relindex(n->op)], lt);
1472 snprint(cmpbuf, sizeof cmpbuf, "%T %s %s",
1473 lt, cmps[relindex(n->op)], xbuf);
1474 if(debug['y']) prtree(n, "strange");
1475 warn(n, "useless or misleading comparison: %s", cmpbuf);