3 int compar(Node*, int);
15 prtree(n, "pre complex");
20 prtree(n, "t complex");
24 prtree(n, "c complex");
28 prtree(n, "a complex");
32 prtree(n, "x complex");
37 * evaluate lvalues (addable == 1)
50 return tcomo(n, ADDROF);
70 diag(n, "unknown op in type complex: %O", n->op);
75 * tcom has already been called on this subtree
85 if(n->type->width == types[TLONG]->width) {
86 if(tcomo(l, ADDROF|CASTOF))
93 if(tcompat(n, l->type, n->type, tcast))
99 if(n->type->etype != TVOID)
100 warn(n, "null return of a typed function");
106 if(tcompat(n, n->type, l->type, tasign))
108 constas(n, n->type, l->type);
109 if(!sametype(n->type, l->type)) {
110 l = new1(OCAST, l, Z);
116 case OASI: /* same as as, but no test for const */
123 if(tlvalue(l) || tcompat(n, l->type, r->type, tasign))
125 if(!sametype(l->type, r->type)) {
126 r = new1(OCAST, r, Z);
142 if(tcompat(n, l->type, r->type, tasign))
144 constas(n, l->type, r->type);
145 if(!sametype(l->type, r->type)) {
146 r = new1(OCAST, r, Z);
162 typeext1(l->type, r);
163 if(tcompat(n, l->type, r->type, tasadd))
165 constas(n, l->type, r->type);
168 while(n->left->op == OCAST)
169 n->left = n->left->left;
170 if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
171 r = new1(OCAST, n->right, Z);
189 typeext1(l->type, r);
190 if(tcompat(n, l->type, r->type, tmul))
192 constas(n, l->type, r->type);
195 while(n->left->op == OCAST)
196 n->left = n->left->left;
197 if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
198 r = new1(OCAST, n->right, Z);
203 if(typeu[n->type->etype]) {
221 if(tcompat(n, l->type, r->type, tand))
224 n->right = new1(OCAST, r, Z);
225 n->right->type = types[TINT];
226 if(typeu[n->type->etype]) {
244 if(tcompat(n, l->type, r->type, tand))
248 while(n->left->op == OCAST)
249 n->left = n->left->left;
250 if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
251 r = new1(OCAST, n->right, Z);
256 if(typeu[n->type->etype]) {
272 if(tcompat(n, l->type, types[TINT], tadd))
275 if(n->type->etype == TIND)
276 if(n->type->link->width < 1)
277 diag(n, "inc/dec of a void pointer");
289 if(tcompat(n, l->type, r->type, trel))
292 n->type = types[TINT];
304 typeext1(l->type, r);
305 typeext1(r->type, l);
306 if(tcompat(n, l->type, r->type, trel))
309 if(typeu[n->type->etype])
310 n->op = logrel[relindex(n->op)];
311 n->type = types[TINT];
317 if(o | tcom(r->right))
319 if(r->right->type->etype == TIND && vconst(r->left) == 0) {
320 r->left->type = r->right->type;
323 if(r->left->type->etype == TIND && vconst(r->right) == 0) {
324 r->right->type = r->left->type;
325 r->right->vconst = 0;
327 if(sametype(r->right->type, r->left->type)) {
328 r->type = r->right->type;
332 if(tcompat(r, r->left->type, r->right->type, trel))
344 if(tcompat(n, l->type, r->type, tadd))
355 if(tcompat(n, l->type, r->type, tsub))
369 if(tcompat(n, l->type, r->type, tmul))
372 if(typeu[n->type->etype]) {
388 if(tcompat(n, l->type, r->type, tand))
392 n->right = new1(OCAST, r, Z);
393 n->right->type = types[TINT];
394 if(typeu[n->type->etype])
407 if(tcompat(n, l->type, r->type, tand))
419 if(tcompat(n, l->type, r->type, tand))
422 if(typeu[n->type->etype])
433 l = new(OCONST, Z, Z);
435 l->type = types[TINT];
442 if(tcompat(n, l->type, r->type, tsub))
455 l = new(OCONST, Z, Z);
457 l->type = types[TINT];
464 if(tcompat(n, l->type, r->type, tsub))
478 l = new(OCONST, Z, Z);
480 l->type = types[TINT];
487 if(tcompat(n, l->type, r->type, tand))
498 if(tcompat(n, T, l->type, tnot))
500 n->type = types[TINT];
508 if(tcompat(n, T, l->type, tnot) |
509 tcompat(n, T, r->type, tnot))
511 n->type = types[TINT];
522 case OSIGN: /* extension signof(type) returns a hash */
524 if(l->op != OSTRING && l->op != OLSTRING)
528 diag(n, "signof bitfield");
535 if(n->type->width < 0) {
536 diag(n, "signof undefined type");
542 n->vconst = convvtox(signature(n->type), TULONG);
543 n->type = types[TULONG];
548 if(l->op != OSTRING && l->op != OLSTRING)
552 diag(n, "sizeof bitfield");
559 if(n->type->width <= 0) {
560 diag(n, "sizeof undefined type");
563 if(n->type->etype == TFUNC) {
564 diag(n, "sizeof function");
570 n->vconst = convvtox(n->type->width, TINT);
571 n->type = types[TINT];
578 if(l->type->etype == TIND && l->type->link->etype == TFUNC) {
579 l = new1(OIND, l, Z);
580 l->type = l->left->type->link;
583 if(tcompat(n, T, l->type, tfunct))
585 if(o | tcoma(l, r, l->type->down, 1))
587 n->type = l->type->link;
589 if(l->type->down == T || l->type->down->etype == TOLD) {
591 diag(n, "function args not checked: %F", l);
598 diag(n, "name not declared: %F", n);
601 if(n->type->etype == TENUM) {
603 n->type = n->sym->tenum;
604 if(!typefd[n->type->etype])
605 n->vconst = n->sym->vconst;
607 n->fconst = n->sym->fconst;
611 if(n->class == CEXREG) {
613 n->reg = n->sym->offset;
620 if(n->type->link != types[TUSHORT]) {
623 outlstring(L"", sizeof(ushort));
624 o = outlstring(0, 0);
628 n->xoffset = outlstring(n->rstring, n->type->width);
633 if(n->type->link != types[TCHAR]) {
641 n->xoffset = outstring(n->cstring, n->type->width);
651 if(tcompat(n, T, l->type, tdot))
663 diag(n, "address of a bit field");
666 if(l->op == OREGISTER) {
667 diag(n, "address of a register");
670 n->type = typ(TIND, l->type);
671 n->type->width = types[TIND]->width;
677 if(tcompat(n, T, l->type, tindir))
679 n->type = l->type->link;
694 if(typesu[t->etype] && t->tag)
695 diag(n, "structure not fully declared %s", t->tag->name);
697 diag(n, "structure not fully declared");
701 if(typeaf[t->etype]) {
705 warn(n, "address of array/func ignored");
712 l = new1(OXXX, Z, Z);
715 if(l->type->etype == TARRAY)
716 l->type = l->type->link;
720 n->type = typ(TIND, l->type);
721 n->type->width = types[TIND]->width;
730 tcoma(Node *l, Node *n, Type *t, int f)
736 if(t->etype == TOLD || t->etype == TDOT) /* .../old in prototype */
739 if(t != T && !sametype(t, types[TVOID])) {
740 diag(n, "not enough function arguments: %F", l);
746 o = tcoma(l, n->left, t, 0);
752 return o | tcoma(l, n->right, t, 1);
755 tcoma(l, Z, t->down, 0);
756 if(tcom(n) || tcompat(n, T, n->type, targ))
758 if(sametype(t, types[TVOID])) {
759 diag(n, "too many function arguments: %F", l);
764 if(stcompat(nodproto, t, n->type, tasign)) {
765 diag(l, "argument prototype mismatch \"%T\" for \"%T\": %F",
781 switch(n->type->etype)
796 if(t != T && !sametype(t, n->type)) {
797 n1 = new1(OXXX, Z, Z);
815 t = dotsearch(n->sym, n->left->type->link, n, &o);
817 diag(n, "not a member of struct/union: %F", n);
828 Node *l, *r, **ar, **al;
832 if(n->type->etype != TSTRUCT) {
833 diag(n, "constructor must be a structure");
839 for(t = n->type->link; t != T; t = t->down) {
841 diag(n, "constructor list too short");
857 if(tcompat(n, t, r->type, tasign))
859 constas(n, t, r->type);
860 if(!e && !sametype(t, r->type)) {
861 r = new1(OCAST, r, Z);
867 diag(n, "constructor list too long");
878 diag(n, "not an l-value");
886 * (IND(ADDR x)) ==> x
887 * (ADDR(IND x)) ==> x
888 * remove some zero operands
922 if(n->op == OASLSHR || n->op == OASASHR || n->op == OASASHL)
923 if(r->op == OCONST) {
924 t = n->type->width * 8; /* bits per byte */
925 if(r->vconst >= t || r->vconst < 0)
926 warn(n, "stupid shift: %lld", r->vconst);
932 if(l->op == OCONST) {
937 if(nocast(l->type, n->type)) {
963 l->left->type = n->type;
972 l->left->type = n->type;
992 if(compar(n, 0) || compar(n, 1))
1002 if(vconst(l) == 0 && !side(r)) {
1007 if(vconst(r) == 0) {
1011 if(r->op == OCONST) {
1012 t = n->type->width * 8; /* bits per byte */
1013 if(r->vconst >= t || r->vconst <= -t)
1014 warn(n, "stupid shift: %lld", r->vconst);
1022 if(t == 0 && !side(r)) {
1032 if(t == 0 && !side(l)) {
1045 if(vconst(l) == 0 && !side(r)) {
1052 diag(n, "divide check");
1064 if(r->op == OCONST) {
1065 if(typefd[r->type->etype]) {
1067 r->fconst = -r->fconst;
1071 r->vconst = -r->vconst;
1082 if(vconst(l) == 0) {
1087 if(vconst(r) == 0) {
1096 if(vconst(l) == 0 && !side(r)) {
1100 if(vconst(r) == 0 && !side(l)) {
1106 /* look for commutative constant */
1107 if(r->op == OCONST) {
1108 if(l->op == n->op) {
1109 if(l->left->op == OCONST) {
1110 n->right = l->right;
1114 if(l->right->op == OCONST) {
1121 if(l->op == OCONST) {
1122 if(r->op == n->op) {
1123 if(r->left->op == OCONST) {
1128 if(r->right->op == OCONST) {
1139 if(vconst(l) == 0) {
1148 if(l->op == OCONST && l->vconst != 0) {
1172 /* OEQ, ONE, OLE, OLS, OLT, OLO, OGE, OHS, OGT, OHI */
1173 static char *cmps[12] =
1175 "==", "!=", "<=", "<=", "<", "<", ">=", ">=", ">", ">",
1178 /* 128-bit numbers */
1179 typedef struct Big Big;
1207 if(y > 0 && x.b < ob)
1209 if(y < 0 && x.b > ob)
1215 big(vlong a, uvlong b)
1225 compar(Node *n, int reverse)
1229 char xbuf[40], cmpbuf[50];
1234 * The point of this function is to diagnose comparisons
1235 * that can never be true or that look misleading because
1236 * of the `usual arithmetic conversions'. As an example
1237 * of the latter, if x is a ulong, then if(x <= -1) really means
1238 * if(x <= 0xFFFFFFFF), while if(x <= -1LL) really means
1239 * what it says (but 8c compiles it wrong anyway).
1245 op = comrel[relindex(n->op)];
1253 * Skip over left casts to find out the original expression range.
1255 while(l->op == OCAST)
1260 if(l->op == ONAME && l->sym->type){
1262 if(lt->etype == TARRAY)
1267 if(lt->etype == TXXX || lt->etype > TUVLONG)
1271 * Skip over the right casts to find the on-screen value.
1275 while(r->oldop == OCAST && !r->xcast)
1283 if((rt->etype&1) && r->vconst < 0) /* signed negative */
1286 if((lt->etype&1)==0){
1292 hi = big(0, (1LL<<(l->type->width*8))-1);
1294 lo = big(~0ULL, -(1LL<<(l->type->width*8-1)));
1295 hi = big(0, (1LL<<(l->type->width*8-1))-1);
1305 if(cmp(x, add(hi, 1)) >= 0)
1312 if(cmp(x, add(lo, -1)) <= 0)
1320 * Don't warn about comparisons if the expression
1321 * is as wide as the value: the compiler-supplied casts
1322 * will make both outcomes possible.
1324 if(lt->width >= rt->width && debug['w'] < 2)
1326 if(cmp(x, lo) < 0 || cmp(x, hi) > 0)
1333 if((x.a==0 && x.b<=9) || (x.a==~0LL && x.b >= -9ULL))
1334 snprint(xbuf, sizeof xbuf, "%lld", x.b);
1336 snprint(xbuf, sizeof xbuf, "%#llux", x.b);
1338 snprint(xbuf, sizeof xbuf, "%#llx", x.b);
1340 snprint(cmpbuf, sizeof cmpbuf, "%s %s %T",
1341 xbuf, cmps[relindex(n->op)], lt);
1343 snprint(cmpbuf, sizeof cmpbuf, "%T %s %s",
1344 lt, cmps[relindex(n->op)], xbuf);
1345 warn(n, "useless or misleading comparison: %s", cmpbuf);