1 /* $NetBSD: tree.c,v 1.82 2015/10/14 18:31:52 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1994, 1995 Jochen Pohl 5 * All Rights Reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Jochen Pohl for 18 * The NetBSD Project. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #if HAVE_NBTOOL_CONFIG_H 35 #include "nbtool_config.h" 36 #endif 37 38 #include <sys/cdefs.h> 39 #if defined(__RCSID) && !defined(lint) 40 __RCSID("$NetBSD: tree.c,v 1.82 2015/10/14 18:31:52 christos Exp $"); 41 #endif 42 43 #include <stdlib.h> 44 #include <string.h> 45 #include <float.h> 46 #include <limits.h> 47 #include <math.h> 48 #include <signal.h> 49 50 #include "lint1.h" 51 #include "cgram.h" 52 #include "externs1.h" 53 54 static tnode_t *getinode(tspec_t, int64_t); 55 static void ptrcmpok(op_t, tnode_t *, tnode_t *); 56 static int asgntypok(op_t, int, tnode_t *, tnode_t *); 57 static void chkbeop(op_t, tnode_t *, tnode_t *); 58 static void chkeop2(op_t, int, tnode_t *, tnode_t *); 59 static void chkeop1(op_t, int, tnode_t *, tnode_t *); 60 static tnode_t *mktnode(op_t, type_t *, tnode_t *, tnode_t *); 61 static void balance(op_t, tnode_t **, tnode_t **); 62 static void incompat(op_t, tspec_t, tspec_t); 63 static void illptrc(mod_t *, type_t *, type_t *); 64 static void mrgqual(type_t **, type_t *, type_t *); 65 static int conmemb(type_t *); 66 static void ptconv(int, tspec_t, tspec_t, type_t *, tnode_t *); 67 static void iiconv(op_t, int, tspec_t, tspec_t, type_t *, tnode_t *); 68 static void piconv(op_t, tspec_t, type_t *, tnode_t *); 69 static void ppconv(op_t, tnode_t *, type_t *); 70 static tnode_t *bldstr(op_t, tnode_t *, tnode_t *); 71 static tnode_t *bldincdec(op_t, tnode_t *); 72 static tnode_t *bldri(op_t, tnode_t *); 73 static tnode_t *bldamper(tnode_t *, int); 74 static tnode_t *bldplmi(op_t, tnode_t *, tnode_t *); 75 static tnode_t *bldshft(op_t, tnode_t *, tnode_t *); 76 static tnode_t *bldcol(tnode_t *, tnode_t *); 77 static tnode_t *bldasgn(op_t, tnode_t *, tnode_t *); 78 static tnode_t *plength(type_t *); 79 static tnode_t *fold(tnode_t *); 80 static tnode_t *foldtst(tnode_t *); 81 static tnode_t *foldflt(tnode_t *); 82 static tnode_t *chkfarg(type_t *, tnode_t *); 83 static tnode_t *parg(int, type_t *, tnode_t *); 84 static void nulleff(tnode_t *); 85 static void displexpr(tnode_t *, int); 86 static void chkaidx(tnode_t *, int); 87 static void chkcomp(op_t, tnode_t *, tnode_t *); 88 static void precconf(tnode_t *); 89 90 extern sig_atomic_t fpe; 91 92 #if 0 93 static char * 94 dumpnode(char *buf, size_t len, tnode_t *tn) { 95 const char *n = getopname(tn->tn_op); 96 const char *s; 97 char tbuf[256]; 98 99 switch (tn->tn_op) { 100 case NAME: 101 s = tn->tn_sym->s_name; 102 break; 103 case CON: 104 case STRING: 105 s = "*"; /* todo */ 106 break; 107 default: 108 s = NULL; 109 break; 110 } 111 char lb[1024]; 112 char rb[1024]; 113 114 if (s == NULL && tn->tn_left != NULL) 115 dumpnode(lb, sizeof(lb), tn->tn_left); 116 else 117 strcpy(lb, "(null)"); 118 119 if (s == NULL && tn->tn_right != NULL) 120 dumpnode(rb, sizeof(rb), tn->tn_right); 121 else 122 strcpy(rb, "(null)"); 123 124 125 snprintf(buf, len, "%s: (%s) = %s [%s, %s]", n, 126 tyname(tbuf, sizeof(tbuf), tn->tn_type), s, lb, rb); 127 return buf; 128 } 129 #endif 130 131 /* 132 * Increase degree of reference. 133 * This is most often used to change type "T" in type "pointer to T". 134 */ 135 type_t * 136 incref(type_t *tp, tspec_t t) 137 { 138 type_t *tp2; 139 140 tp2 = getblk(sizeof (type_t)); 141 tp2->t_tspec = t; 142 tp2->t_subt = tp; 143 return (tp2); 144 } 145 146 /* 147 * same for use in expressions 148 */ 149 type_t * 150 tincref(type_t *tp, tspec_t t) 151 { 152 type_t *tp2; 153 154 tp2 = tgetblk(sizeof (type_t)); 155 tp2->t_tspec = t; 156 tp2->t_subt = tp; 157 return (tp2); 158 } 159 160 /* 161 * Create a node for a constant. 162 */ 163 tnode_t * 164 getcnode(type_t *tp, val_t *v) 165 { 166 tnode_t *n; 167 168 n = getnode(); 169 n->tn_op = CON; 170 n->tn_type = tp; 171 n->tn_val = tgetblk(sizeof (val_t)); 172 n->tn_val->v_tspec = tp->t_tspec; 173 n->tn_val->v_ansiu = v->v_ansiu; 174 n->tn_val->v_u = v->v_u; 175 free(v); 176 return (n); 177 } 178 179 /* 180 * Create a node for a integer constant. 181 */ 182 static tnode_t * 183 getinode(tspec_t t, int64_t q) 184 { 185 tnode_t *n; 186 187 n = getnode(); 188 n->tn_op = CON; 189 n->tn_type = gettyp(t); 190 n->tn_val = tgetblk(sizeof (val_t)); 191 n->tn_val->v_tspec = t; 192 n->tn_val->v_quad = q; 193 return (n); 194 } 195 196 /* 197 * Create a node for a name (symbol table entry). 198 * ntok is the token which follows the name. 199 */ 200 tnode_t * 201 getnnode(sym_t *sym, int ntok) 202 { 203 tnode_t *n; 204 205 if (sym->s_scl == NOSCL) { 206 sym->s_scl = EXTERN; 207 sym->s_def = DECL; 208 if (ntok == T_LPARN) { 209 if (sflag) { 210 /* function implicitly declared to ... */ 211 warning(215); 212 } 213 /* 214 * XXX if tflag is set the symbol should be 215 * exported to level 0 216 */ 217 sym->s_type = incref(sym->s_type, FUNC); 218 } else { 219 if (!blklev) { 220 /* %s undefined */ 221 error(99, sym->s_name); 222 } else { 223 int fixtype; 224 if (strcmp(sym->s_name, "__FUNCTION__") == 0 || 225 strcmp(sym->s_name, "__PRETTY_FUNCTION__") 226 == 0) { 227 gnuism(316); 228 fixtype = 1; 229 } else if (strcmp(sym->s_name, "__func__") == 0) { 230 if (!Sflag) 231 warning(317); 232 fixtype = 1; 233 } else { 234 error(99, sym->s_name); 235 fixtype = 0; 236 } 237 if (fixtype) { 238 sym->s_type = incref(gettyp(CHAR), PTR); 239 sym->s_type->t_const = 1; 240 } 241 } 242 } 243 } 244 245 if (sym->s_kind != FVFT && sym->s_kind != FMOS) 246 LERROR("getnnode(%d)", sym->s_kind); 247 248 n = getnode(); 249 n->tn_type = sym->s_type; 250 if (sym->s_scl != ENUMCON) { 251 n->tn_op = NAME; 252 n->tn_sym = sym; 253 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC) 254 n->tn_lvalue = 1; 255 } else { 256 n->tn_op = CON; 257 n->tn_val = tgetblk(sizeof (val_t)); 258 *n->tn_val = sym->s_value; 259 } 260 261 return (n); 262 } 263 264 /* 265 * Create a node for a string. 266 */ 267 tnode_t * 268 getsnode(strg_t *strg) 269 { 270 size_t len; 271 tnode_t *n; 272 273 len = strg->st_len; 274 275 n = getnode(); 276 277 n->tn_op = STRING; 278 n->tn_type = tincref(gettyp(strg->st_tspec), ARRAY); 279 n->tn_type->t_dim = len + 1; 280 n->tn_lvalue = 1; 281 282 n->tn_strg = tgetblk(sizeof (strg_t)); 283 n->tn_strg->st_tspec = strg->st_tspec; 284 n->tn_strg->st_len = len; 285 286 if (strg->st_tspec == CHAR) { 287 n->tn_strg->st_cp = tgetblk(len + 1); 288 (void)memcpy(n->tn_strg->st_cp, strg->st_cp, len + 1); 289 free(strg->st_cp); 290 } else { 291 n->tn_strg->st_wcp = tgetblk((len + 1) * sizeof (wchar_t)); 292 (void)memcpy(n->tn_strg->st_wcp, strg->st_wcp, 293 (len + 1) * sizeof (wchar_t)); 294 free(strg->st_wcp); 295 } 296 free(strg); 297 298 return (n); 299 } 300 301 /* 302 * Returns a symbol which has the same name as the msym argument and is a 303 * member of the struct or union specified by the tn argument. 304 */ 305 sym_t * 306 strmemb(tnode_t *tn, op_t op, sym_t *msym) 307 { 308 str_t *str; 309 type_t *tp; 310 sym_t *sym, *csym; 311 int eq; 312 tspec_t t; 313 314 /* 315 * Remove the member if it was unknown until now (Which means 316 * that no defined struct or union has a member with the same name). 317 */ 318 if (msym->s_scl == NOSCL) { 319 /* undefined struct/union member: %s */ 320 error(101, msym->s_name); 321 rmsym(msym); 322 msym->s_kind = FMOS; 323 msym->s_scl = MOS; 324 msym->s_styp = tgetblk(sizeof (str_t)); 325 msym->s_styp->stag = tgetblk(sizeof (sym_t)); 326 msym->s_styp->stag->s_name = unnamed; 327 msym->s_value.v_tspec = INT; 328 return (msym); 329 } 330 331 /* Set str to the tag of which msym is expected to be a member. */ 332 str = NULL; 333 t = (tp = tn->tn_type)->t_tspec; 334 if (op == POINT) { 335 if (t == STRUCT || t == UNION) 336 str = tp->t_str; 337 } else if (op == ARROW && t == PTR) { 338 t = (tp = tp->t_subt)->t_tspec; 339 if (t == STRUCT || t == UNION) 340 str = tp->t_str; 341 } 342 343 /* 344 * If this struct/union has a member with the name of msym, return 345 * return this it. 346 */ 347 if (str != NULL) { 348 for (sym = msym; sym != NULL; sym = sym->s_link) { 349 if (sym->s_scl != MOS && sym->s_scl != MOU) 350 continue; 351 if (sym->s_styp != str) 352 continue; 353 if (strcmp(sym->s_name, msym->s_name) != 0) 354 continue; 355 return (sym); 356 } 357 } 358 359 /* 360 * Set eq to 0 if there are struct/union members with the same name 361 * and different types and/or offsets. 362 */ 363 eq = 1; 364 for (csym = msym; csym != NULL; csym = csym->s_link) { 365 if (csym->s_scl != MOS && csym->s_scl != MOU) 366 continue; 367 if (strcmp(msym->s_name, csym->s_name) != 0) 368 continue; 369 for (sym = csym->s_link ; sym != NULL; sym = sym->s_link) { 370 int w; 371 372 if (sym->s_scl != MOS && sym->s_scl != MOU) 373 continue; 374 if (strcmp(csym->s_name, sym->s_name) != 0) 375 continue; 376 if (csym->s_value.v_quad != sym->s_value.v_quad) { 377 eq = 0; 378 break; 379 } 380 w = 0; 381 eq = eqtype(csym->s_type, sym->s_type, 0, 0, &w) && !w; 382 if (!eq) 383 break; 384 if (csym->s_field != sym->s_field) { 385 eq = 0; 386 break; 387 } 388 if (csym->s_field) { 389 type_t *tp1, *tp2; 390 391 tp1 = csym->s_type; 392 tp2 = sym->s_type; 393 if (tp1->t_flen != tp2->t_flen) { 394 eq = 0; 395 break; 396 } 397 if (tp1->t_foffs != tp2->t_foffs) { 398 eq = 0; 399 break; 400 } 401 } 402 } 403 if (!eq) 404 break; 405 } 406 407 /* 408 * Now handle the case in which the left operand refers really 409 * to a struct/union, but the right operand is not member of it. 410 */ 411 if (str != NULL) { 412 /* illegal member use: %s */ 413 if (eq && tflag) { 414 warning(102, msym->s_name); 415 } else { 416 error(102, msym->s_name); 417 } 418 return (msym); 419 } 420 421 /* 422 * Now the left operand of ARROW does not point to a struct/union 423 * or the left operand of POINT is no struct/union. 424 */ 425 if (eq) { 426 if (op == POINT) { 427 /* left operand of "." must be struct/union object */ 428 if (tflag) { 429 warning(103); 430 } else { 431 error(103); 432 } 433 } else { 434 char buf[64]; 435 /* left operand of "->" must be pointer to ... */ 436 if (tflag && tn->tn_type->t_tspec == PTR) { 437 tyname(buf, sizeof(buf), tn->tn_type); 438 warning(104, buf); 439 } else { 440 tyname(buf, sizeof(buf), tn->tn_type); 441 error(104, buf); 442 } 443 } 444 } else { 445 if (tflag) { 446 /* non-unique member requires struct/union %s */ 447 error(105, op == POINT ? "object" : "pointer"); 448 } else { 449 /* unacceptable operand of %s */ 450 error(111, modtab[op].m_name); 451 } 452 } 453 454 return (msym); 455 } 456 457 /* 458 * Create a tree node. Called for most operands except function calls, 459 * sizeof and casts. 460 * 461 * op operator 462 * ln left operand 463 * rn if not NULL, right operand 464 */ 465 tnode_t * 466 build(op_t op, tnode_t *ln, tnode_t *rn) 467 { 468 mod_t *mp; 469 tnode_t *ntn; 470 type_t *rtp; 471 472 mp = &modtab[op]; 473 474 /* If there was an error in one of the operands, return. */ 475 if (ln == NULL || (mp->m_binary && rn == NULL)) 476 return (NULL); 477 478 /* 479 * Apply class conversions to the left operand, but only if its 480 * value is needed or it is compaired with null. 481 */ 482 if (mp->m_vctx || mp->m_tctx) 483 ln = cconv(ln); 484 /* 485 * The right operand is almost always in a test or value context, 486 * except if it is a struct or union member. 487 */ 488 if (mp->m_binary && op != ARROW && op != POINT) 489 rn = cconv(rn); 490 491 /* 492 * Print some warnings for comparisons of unsigned values with 493 * constants lower than or equal to null. This must be done 494 * before promote() because otherwise unsigned char and unsigned 495 * short would be promoted to int. Also types are tested to be 496 * CHAR, which would also become int. 497 */ 498 if (mp->m_comp) 499 chkcomp(op, ln, rn); 500 501 /* 502 * Promote the left operand if it is in a test or value context 503 */ 504 if (mp->m_vctx || mp->m_tctx) 505 ln = promote(op, 0, ln); 506 /* 507 * Promote the right operand, but only if it is no struct or 508 * union member, or if it is not to be assigned to the left operand 509 */ 510 if (mp->m_binary && op != ARROW && op != POINT && 511 op != ASSIGN && op != RETURN) { 512 rn = promote(op, 0, rn); 513 } 514 515 /* 516 * If the result of the operation is different for signed or 517 * unsigned operands and one of the operands is signed only in 518 * ANSI C, print a warning. 519 */ 520 if (mp->m_tlansiu && ln->tn_op == CON && ln->tn_val->v_ansiu) { 521 /* ANSI C treats constant as unsigned, op %s */ 522 warning(218, mp->m_name); 523 ln->tn_val->v_ansiu = 0; 524 } 525 if (mp->m_transiu && rn->tn_op == CON && rn->tn_val->v_ansiu) { 526 /* ANSI C treats constant as unsigned, op %s */ 527 warning(218, mp->m_name); 528 rn->tn_val->v_ansiu = 0; 529 } 530 531 /* Make sure both operands are of the same type */ 532 if (mp->m_balance || (tflag && (op == SHL || op == SHR))) 533 balance(op, &ln, &rn); 534 535 /* 536 * Check types for compatibility with the operation and mutual 537 * compatibility. Return if there are serios problems. 538 */ 539 if (!typeok(op, 0, ln, rn)) 540 return (NULL); 541 542 /* And now create the node. */ 543 switch (op) { 544 case POINT: 545 case ARROW: 546 ntn = bldstr(op, ln, rn); 547 break; 548 case INCAFT: 549 case DECAFT: 550 case INCBEF: 551 case DECBEF: 552 ntn = bldincdec(op, ln); 553 break; 554 case AMPER: 555 ntn = bldamper(ln, 0); 556 break; 557 case STAR: 558 ntn = mktnode(STAR, ln->tn_type->t_subt, ln, NULL); 559 break; 560 case PLUS: 561 case MINUS: 562 ntn = bldplmi(op, ln, rn); 563 break; 564 case SHL: 565 case SHR: 566 ntn = bldshft(op, ln, rn); 567 break; 568 case COLON: 569 ntn = bldcol(ln, rn); 570 break; 571 case ASSIGN: 572 case MULASS: 573 case DIVASS: 574 case MODASS: 575 case ADDASS: 576 case SUBASS: 577 case SHLASS: 578 case SHRASS: 579 case ANDASS: 580 case XORASS: 581 case ORASS: 582 case RETURN: 583 ntn = bldasgn(op, ln, rn); 584 break; 585 case COMMA: 586 case QUEST: 587 ntn = mktnode(op, rn->tn_type, ln, rn); 588 break; 589 case REAL: 590 case IMAG: 591 ntn = bldri(op, ln); 592 break; 593 default: 594 rtp = mp->m_logop ? gettyp(INT) : ln->tn_type; 595 if (!mp->m_binary && rn != NULL) 596 LERROR("build()"); 597 ntn = mktnode(op, rtp, ln, rn); 598 break; 599 } 600 601 /* Return if an error occurred. */ 602 if (ntn == NULL) 603 return (NULL); 604 605 /* Print a warning if precedence confusion is possible */ 606 if (mp->m_tpconf) 607 precconf(ntn); 608 609 /* 610 * Print a warning if one of the operands is in a context where 611 * it is compared with null and if this operand is a constant. 612 */ 613 if (mp->m_tctx) { 614 if (ln->tn_op == CON || 615 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) { 616 if (hflag && !ccflg) 617 /* constant in conditional context */ 618 warning(161); 619 } 620 } 621 622 /* Fold if the operator requires it */ 623 if (mp->m_fold) { 624 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) { 625 if (mp->m_tctx) { 626 ntn = foldtst(ntn); 627 } else if (isftyp(ntn->tn_type->t_tspec)) { 628 ntn = foldflt(ntn); 629 } else { 630 ntn = fold(ntn); 631 } 632 } else if (op == QUEST && ln->tn_op == CON) { 633 ntn = ln->tn_val->v_quad ? rn->tn_left : rn->tn_right; 634 } 635 } 636 637 return (ntn); 638 } 639 640 /* 641 * Perform class conversions. 642 * 643 * Arrays of type T are converted into pointers to type T. 644 * Functions are converted to pointers to functions. 645 * Lvalues are converted to rvalues. 646 */ 647 tnode_t * 648 cconv(tnode_t *tn) 649 { 650 type_t *tp; 651 652 /* 653 * Array-lvalue (array of type T) is converted into rvalue 654 * (pointer to type T) 655 */ 656 if (tn->tn_type->t_tspec == ARRAY) { 657 if (!tn->tn_lvalue) { 658 /* %soperand of '%s' must be lvalue */ 659 /* XXX print correct operator */ 660 (void)gnuism(114, "", modtab[AMPER].m_name); 661 } 662 tn = mktnode(AMPER, tincref(tn->tn_type->t_subt, PTR), 663 tn, NULL); 664 } 665 666 /* 667 * Expression of type function (function with return value of type T) 668 * in rvalue-expression (pointer to function with return value 669 * of type T) 670 */ 671 if (tn->tn_type->t_tspec == FUNC) 672 tn = bldamper(tn, 1); 673 674 /* lvalue to rvalue */ 675 if (tn->tn_lvalue) { 676 tp = tduptyp(tn->tn_type); 677 tp->t_const = tp->t_volatile = 0; 678 tn = mktnode(LOAD, tp, tn, NULL); 679 } 680 681 return (tn); 682 } 683 684 /* 685 * Perform most type checks. First the types are checked using 686 * informations from modtab[]. After that it is done by hand for 687 * more complicated operators and type combinations. 688 * 689 * If the types are ok, typeok() returns 1, otherwise 0. 690 */ 691 int 692 typeok(op_t op, int arg, tnode_t *ln, tnode_t *rn) 693 { 694 mod_t *mp; 695 tspec_t lt, rt = NOTSPEC, lst = NOTSPEC, rst = NOTSPEC, olt = NOTSPEC, 696 ort = NOTSPEC; 697 type_t *ltp, *rtp = NULL, *lstp = NULL, *rstp = NULL; 698 tnode_t *tn; 699 700 mp = &modtab[op]; 701 702 if ((ltp = ln->tn_type) == NULL) 703 LERROR("typeok()"); 704 705 if ((lt = ltp->t_tspec) == PTR) 706 lst = (lstp = ltp->t_subt)->t_tspec; 707 if (mp->m_binary) { 708 if ((rtp = rn->tn_type) == NULL) 709 LERROR("typeok()"); 710 if ((rt = rtp->t_tspec) == PTR) 711 rst = (rstp = rtp->t_subt)->t_tspec; 712 } 713 714 if (mp->m_rqint) { 715 /* integertypes required */ 716 if (!isityp(lt) || (mp->m_binary && !isityp(rt))) { 717 incompat(op, lt, rt); 718 return (0); 719 } 720 } else if (mp->m_rqintcomp) { 721 /* integertypes required */ 722 if ((!isityp(lt) && !isctyp(lt)) || 723 (mp->m_binary && (!isityp(rt) && !isctyp(rt)))) { 724 incompat(op, lt, rt); 725 return (0); 726 } 727 } else if (mp->m_rqsclt) { 728 /* scalar types required */ 729 if (!issclt(lt) || (mp->m_binary && !issclt(rt))) { 730 incompat(op, lt, rt); 731 return (0); 732 } 733 } else if (mp->m_rqatyp) { 734 /* arithmetic types required */ 735 if (!isatyp(lt) || (mp->m_binary && !isatyp(rt))) { 736 incompat(op, lt, rt); 737 return (0); 738 } 739 } 740 741 if (op == SHL || op == SHR || op == SHLASS || op == SHRASS) { 742 /* 743 * For these operations we need the types before promotion 744 * and balancing. 745 */ 746 for (tn=ln; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left) 747 continue; 748 olt = tn->tn_type->t_tspec; 749 for (tn=rn; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left) 750 continue; 751 ort = tn->tn_type->t_tspec; 752 } 753 754 switch (op) { 755 case POINT: 756 /* 757 * Most errors required by ANSI C are reported in strmemb(). 758 * Here we only must check for totaly wrong things. 759 */ 760 if (lt == FUNC || lt == VOID || ltp->t_isfield || 761 ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) { 762 /* Without tflag we got already an error */ 763 if (tflag) 764 /* unacceptable operand of %s */ 765 error(111, mp->m_name); 766 return (0); 767 } 768 /* Now we have an object we can create a pointer to */ 769 break; 770 case ARROW: 771 if (lt != PTR && !(tflag && isityp(lt))) { 772 /* Without tflag we got already an error */ 773 if (tflag) 774 /* unacceptabel operand of %s */ 775 error(111, mp->m_name); 776 return (0); 777 } 778 break; 779 case INCAFT: 780 case DECAFT: 781 case INCBEF: 782 case DECBEF: 783 /* operands have scalar types (checked above) */ 784 if (!ln->tn_lvalue) { 785 if (ln->tn_op == CVT && ln->tn_cast && 786 ln->tn_left->tn_op == LOAD) { 787 /* a cast to non-ptr does not yield an lvalue */ 788 if (ln->tn_type->t_tspec == PTR) 789 break; 790 error(163); 791 } 792 /* %soperand of %s must be lvalue */ 793 error(114, "", mp->m_name); 794 return (0); 795 } else if (ltp->t_const) { 796 /* %soperand of %s must be modifiable lvalue */ 797 if (!tflag) 798 warning(115, "", mp->m_name); 799 } 800 break; 801 case AMPER: 802 if (lt == ARRAY || lt == FUNC) { 803 /* ok, a warning comes later (in bldamper()) */ 804 } else if (!ln->tn_lvalue) { 805 if (ln->tn_op == CVT && ln->tn_cast && 806 ln->tn_left->tn_op == LOAD) { 807 /* a cast to non-ptr does not yield an lvalue */ 808 if (ln->tn_type->t_tspec == PTR) 809 break; 810 error(163); 811 } 812 /* %soperand of %s must be lvalue */ 813 error(114, "", mp->m_name); 814 return (0); 815 } else if (issclt(lt)) { 816 if (ltp->t_isfield) { 817 /* cannot take address of bit-field */ 818 error(112); 819 return (0); 820 } 821 } else if (lt != STRUCT && lt != UNION) { 822 /* unacceptable operand of %s */ 823 error(111, mp->m_name); 824 return (0); 825 } 826 if (ln->tn_op == NAME && ln->tn_sym->s_reg) { 827 /* cannot take address of register %s */ 828 error(113, ln->tn_sym->s_name); 829 return (0); 830 } 831 break; 832 case STAR: 833 /* until now there were no type checks for this operator */ 834 if (lt != PTR) { 835 /* cannot dereference non-pointer type */ 836 error(96); 837 return (0); 838 } 839 break; 840 case PLUS: 841 /* operands have scalar types (checked above) */ 842 if ((lt == PTR && !isityp(rt)) || (rt == PTR && !isityp(lt))) { 843 incompat(op, lt, rt); 844 return (0); 845 } 846 break; 847 case MINUS: 848 /* operands have scalar types (checked above) */ 849 if (lt == PTR && (!isityp(rt) && rt != PTR)) { 850 incompat(op, lt, rt); 851 return (0); 852 } else if (rt == PTR && lt != PTR) { 853 incompat(op, lt, rt); 854 return (0); 855 } 856 if (lt == PTR && rt == PTR) { 857 if (!eqtype(lstp, rstp, 1, 0, NULL)) { 858 /* illegal pointer subtraction */ 859 error(116); 860 } 861 } 862 break; 863 case SHR: 864 /* operands have integer types (checked above) */ 865 if (pflag && !isutyp(lt)) { 866 /* 867 * The left operand is signed. This means that 868 * the operation is (possibly) nonportable. 869 */ 870 /* bitwise operation on signed value nonportable */ 871 if (ln->tn_op != CON) { 872 /* possibly nonportable */ 873 warning(117); 874 } else if (ln->tn_val->v_quad < 0) { 875 warning(120); 876 } 877 } else if (!tflag && !sflag && !isutyp(olt) && isutyp(ort)) { 878 /* 879 * The left operand would become unsigned in 880 * traditional C. 881 */ 882 if (hflag && 883 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) { 884 /* semantics of %s change in ANSI C; use ... */ 885 warning(118, mp->m_name); 886 } 887 } else if (!tflag && !sflag && !isutyp(olt) && !isutyp(ort) && 888 psize(lt) < psize(rt)) { 889 /* 890 * In traditional C the left operand would be extended, 891 * possibly with 1, and then shifted. 892 */ 893 if (hflag && 894 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) { 895 /* semantics of %s change in ANSI C; use ... */ 896 warning(118, mp->m_name); 897 } 898 } 899 goto shift; 900 case SHL: 901 /* 902 * ANSI C does not perform balancing for shift operations, 903 * but traditional C does. If the width of the right operand 904 * is greather than the width of the left operand, than in 905 * traditional C the left operand would be extendet to the 906 * width of the right operand. For SHL this may result in 907 * different results. 908 */ 909 if (psize(lt) < psize(rt)) { 910 /* 911 * XXX If both operands are constant make sure 912 * that there is really a differencs between 913 * ANSI C and traditional C. 914 */ 915 if (hflag) 916 /* semantics of %s change in ANSI C; use ... */ 917 warning(118, mp->m_name); 918 } 919 shift: 920 if (rn->tn_op == CON) { 921 if (!isutyp(rt) && rn->tn_val->v_quad < 0) { 922 /* negative shift */ 923 warning(121); 924 } else if ((uint64_t)rn->tn_val->v_quad == (uint64_t)size(lt)) { 925 /* shift equal to size fo object */ 926 warning(267); 927 } else if ((uint64_t)rn->tn_val->v_quad > (uint64_t)size(lt)) { 928 /* shift greater than size of object */ 929 warning(122); 930 } 931 } 932 break; 933 case EQ: 934 case NE: 935 /* 936 * Accept some things which are allowed with EQ and NE, 937 * but not with ordered comparisons. 938 */ 939 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) { 940 if (rn->tn_op == CON && rn->tn_val->v_quad == 0) 941 break; 942 } 943 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) { 944 if (ln->tn_op == CON && ln->tn_val->v_quad == 0) 945 break; 946 } 947 /* FALLTHROUGH */ 948 case LT: 949 case GT: 950 case LE: 951 case GE: 952 if ((lt == PTR || rt == PTR) && lt != rt) { 953 if (isityp(lt) || isityp(rt)) { 954 /* illegal comb. of pointer and int., op %s */ 955 warning(123, mp->m_name); 956 } else { 957 incompat(op, lt, rt); 958 return (0); 959 } 960 } else if (lt == PTR && rt == PTR) { 961 ptrcmpok(op, ln, rn); 962 } 963 break; 964 case QUEST: 965 if (!issclt(lt)) { 966 /* first operand must have scalar type, op ? : */ 967 error(170); 968 return (0); 969 } 970 while (rn->tn_op == CVT) 971 rn = rn->tn_left; 972 if (rn->tn_op != COLON) 973 LERROR("typeok()"); 974 break; 975 case COLON: 976 977 if (isatyp(lt) && isatyp(rt)) 978 break; 979 980 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str) 981 break; 982 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str) 983 break; 984 985 /* combination of any pointer and 0, 0L or (void *)0 is ok */ 986 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) { 987 if (rn->tn_op == CON && rn->tn_val->v_quad == 0) 988 break; 989 } 990 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) { 991 if (ln->tn_op == CON && ln->tn_val->v_quad == 0) 992 break; 993 } 994 995 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) { 996 /* illegal comb. of ptr. and int., op %s */ 997 warning(123, mp->m_name); 998 break; 999 } 1000 1001 if (lt == VOID || rt == VOID) { 1002 if (lt != VOID || rt != VOID) 1003 /* incompatible types in conditional */ 1004 warning(126); 1005 break; 1006 } 1007 1008 if (lt == PTR && rt == PTR && ((lst == VOID && rst == FUNC) || 1009 (lst == FUNC && rst == VOID))) { 1010 /* (void *)0 handled above */ 1011 if (sflag) 1012 /* ANSI C forbids conv. of %s to %s, op %s */ 1013 warning(305, "function pointer", "'void *'", 1014 mp->m_name); 1015 break; 1016 } 1017 1018 if (rt == PTR && lt == PTR) { 1019 if (eqptrtype(lstp, rstp, 1)) 1020 break; 1021 if (!eqtype(lstp, rstp, 1, 0, NULL)) 1022 illptrc(mp, ltp, rtp); 1023 break; 1024 } 1025 1026 /* incompatible types in conditional */ 1027 error(126); 1028 return (0); 1029 1030 case ASSIGN: 1031 case INIT: 1032 case FARG: 1033 case RETURN: 1034 if (!asgntypok(op, arg, ln, rn)) 1035 return (0); 1036 goto assign; 1037 case MULASS: 1038 case DIVASS: 1039 case MODASS: 1040 goto assign; 1041 case ADDASS: 1042 case SUBASS: 1043 /* operands have scalar types (checked above) */ 1044 if ((lt == PTR && !isityp(rt)) || rt == PTR) { 1045 incompat(op, lt, rt); 1046 return (0); 1047 } 1048 goto assign; 1049 case SHLASS: 1050 goto assign; 1051 case SHRASS: 1052 if (pflag && !isutyp(lt) && !(tflag && isutyp(rt))) { 1053 /* bitwise operation on s.v. possibly nonportable */ 1054 warning(117); 1055 } 1056 goto assign; 1057 case ANDASS: 1058 case XORASS: 1059 case ORASS: 1060 goto assign; 1061 assign: 1062 if (!ln->tn_lvalue) { 1063 if (ln->tn_op == CVT && ln->tn_cast && 1064 ln->tn_left->tn_op == LOAD) { 1065 /* a cast to non-ptr does not yield an lvalue */ 1066 if (ln->tn_type->t_tspec == PTR) 1067 break; 1068 error(163); 1069 } 1070 /* %soperand of %s must be lvalue */ 1071 error(114, "left ", mp->m_name); 1072 return (0); 1073 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) && 1074 conmemb(ltp))) { 1075 /* %soperand of %s must be modifiable lvalue */ 1076 if (!tflag) 1077 warning(115, "left ", mp->m_name); 1078 } 1079 break; 1080 case COMMA: 1081 if (!modtab[ln->tn_op].m_sideeff) 1082 nulleff(ln); 1083 break; 1084 /* LINTED206: (enumeration values not handled in switch) */ 1085 case CON: 1086 case CASE: 1087 case PUSH: 1088 case LOAD: 1089 case ICALL: 1090 case CVT: 1091 case CALL: 1092 case FSEL: 1093 case STRING: 1094 case NAME: 1095 case LOGOR: 1096 case LOGAND: 1097 case OR: 1098 case XOR: 1099 case AND: 1100 case MOD: 1101 case DIV: 1102 case MULT: 1103 case UMINUS: 1104 case UPLUS: 1105 case DEC: 1106 case INC: 1107 case COMPL: 1108 case NOT: 1109 case NOOP: 1110 case REAL: 1111 case IMAG: 1112 break; 1113 } 1114 1115 if (mp->m_badeop && 1116 (ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) { 1117 chkbeop(op, ln, rn); 1118 } else if (mp->m_enumop && (ltp->t_isenum && rtp && rtp->t_isenum)) { 1119 chkeop2(op, arg, ln, rn); 1120 } else if (mp->m_enumop && (ltp->t_isenum || (rtp && rtp->t_isenum))) { 1121 chkeop1(op, arg, ln, rn); 1122 } 1123 1124 return (1); 1125 } 1126 1127 static void 1128 ptrcmpok(op_t op, tnode_t *ln, tnode_t *rn) 1129 { 1130 type_t *ltp, *rtp; 1131 tspec_t lt, rt; 1132 const char *lts, *rts; 1133 1134 lt = (ltp = ln->tn_type)->t_subt->t_tspec; 1135 rt = (rtp = rn->tn_type)->t_subt->t_tspec; 1136 1137 if (lt == VOID || rt == VOID) { 1138 if (sflag && (lt == FUNC || rt == FUNC)) { 1139 /* (void *)0 already handled in typeok() */ 1140 *(lt == FUNC ? <s : &rts) = "function pointer"; 1141 *(lt == VOID ? <s : &rts) = "'void *'"; 1142 /* ANSI C forbids comparison of %s with %s */ 1143 warning(274, lts, rts); 1144 } 1145 return; 1146 } 1147 1148 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) { 1149 illptrc(&modtab[op], ltp, rtp); 1150 return; 1151 } 1152 1153 if (lt == FUNC && rt == FUNC) { 1154 if (sflag && op != EQ && op != NE) 1155 /* ANSI C forbids ordered comp. of func ptr */ 1156 warning(125); 1157 } 1158 } 1159 1160 /* 1161 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN 1162 * and prints warnings/errors if necessary. 1163 * If the types are (almost) compatible, 1 is returned, otherwise 0. 1164 */ 1165 static int 1166 asgntypok(op_t op, int arg, tnode_t *ln, tnode_t *rn) 1167 { 1168 tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC; 1169 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL; 1170 mod_t *mp; 1171 const char *lts, *rts; 1172 char lbuf[128], rbuf[128]; 1173 1174 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR) 1175 lst = (lstp = ltp->t_subt)->t_tspec; 1176 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR) 1177 rst = (rstp = rtp->t_subt)->t_tspec; 1178 mp = &modtab[op]; 1179 1180 if (isatyp(lt) && isatyp(rt)) 1181 return (1); 1182 1183 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) 1184 /* both are struct or union */ 1185 return (ltp->t_str == rtp->t_str); 1186 1187 /* 0, 0L and (void *)0 may be assigned to any pointer */ 1188 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) { 1189 if (rn->tn_op == CON && rn->tn_val->v_quad == 0) 1190 return (1); 1191 } 1192 1193 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) { 1194 /* two pointers, at least one pointer to void */ 1195 if (sflag && (lst == FUNC || rst == FUNC)) { 1196 /* comb. of ptr to func and ptr to void */ 1197 *(lst == FUNC ? <s : &rts) = "function pointer"; 1198 *(lst == VOID ? <s : &rts) = "'void *'"; 1199 switch (op) { 1200 case INIT: 1201 case RETURN: 1202 /* ANSI C forbids conversion of %s to %s */ 1203 warning(303, rts, lts); 1204 break; 1205 case FARG: 1206 /* ANSI C forbids conv. of %s to %s, arg #%d */ 1207 warning(304, rts, lts, arg); 1208 break; 1209 default: 1210 /* ANSI C forbids conv. of %s to %s, op %s */ 1211 warning(305, rts, lts, mp->m_name); 1212 break; 1213 } 1214 } 1215 } 1216 1217 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID || 1218 eqtype(lstp, rstp, 1, 0, NULL))) { 1219 /* compatible pointer types (qualifiers ignored) */ 1220 if (!tflag && 1221 ((!lstp->t_const && rstp->t_const) || 1222 (!lstp->t_volatile && rstp->t_volatile))) { 1223 /* left side has not all qualifiers of right */ 1224 tyname(lbuf, sizeof(lbuf), lstp); 1225 tyname(rbuf, sizeof(rbuf), rstp); 1226 switch (op) { 1227 case INIT: 1228 case RETURN: 1229 /* incompatible pointer types */ 1230 warning(182, lbuf, rbuf); 1231 break; 1232 case FARG: 1233 /* argument has incompat. ptr. type, arg #%d */ 1234 warning(153, arg, lbuf, rbuf); 1235 break; 1236 default: 1237 /* operands have incompat. ptr. types, op %s */ 1238 warning(128, mp->m_name, lbuf, rbuf); 1239 break; 1240 } 1241 } 1242 return (1); 1243 } 1244 1245 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) { 1246 const char *lx = lt == PTR ? "pointer" : "integer"; 1247 const char *rx = rt == PTR ? "pointer" : "integer"; 1248 tyname(lbuf, sizeof(lbuf), ltp); 1249 tyname(rbuf, sizeof(rbuf), rtp); 1250 1251 switch (op) { 1252 case INIT: 1253 case RETURN: 1254 /* illegal combination of pointer and integer */ 1255 warning(183, lx, lbuf, rx, rbuf); 1256 break; 1257 case FARG: 1258 /* illegal comb. of ptr. and int., arg #%d */ 1259 warning(154, lx, lbuf, rx, rbuf, arg); 1260 break; 1261 default: 1262 /* illegal comb. of ptr. and int., op %s */ 1263 warning(123, lx, lbuf, rx, rbuf, mp->m_name); 1264 break; 1265 } 1266 return (1); 1267 } 1268 1269 if (lt == PTR && rt == PTR) { 1270 switch (op) { 1271 case INIT: 1272 case RETURN: 1273 illptrc(NULL, ltp, rtp); 1274 break; 1275 case FARG: 1276 /* argument has incompatible pointer type, arg #%d */ 1277 warning(153, arg, tyname(lbuf, sizeof(lbuf), ltp), 1278 tyname(rbuf, sizeof(rbuf), rtp)); 1279 break; 1280 default: 1281 illptrc(mp, ltp, rtp); 1282 break; 1283 } 1284 return (1); 1285 } 1286 1287 switch (op) { 1288 case INIT: 1289 /* initialisation type mismatch */ 1290 error(185); 1291 break; 1292 case RETURN: 1293 /* return value type mismatch */ 1294 error(211); 1295 break; 1296 case FARG: 1297 /* argument is incompatible with prototype, arg #%d */ 1298 warning(155, arg); 1299 break; 1300 default: 1301 incompat(op, lt, rt); 1302 break; 1303 } 1304 1305 return (0); 1306 } 1307 1308 /* 1309 * Prints a warning if an operator, which should be senseless for an 1310 * enum type, is applied to an enum type. 1311 */ 1312 static void 1313 chkbeop(op_t op, tnode_t *ln, tnode_t *rn) 1314 { 1315 mod_t *mp; 1316 1317 if (!eflag) 1318 return; 1319 1320 mp = &modtab[op]; 1321 1322 if (!(ln->tn_type->t_isenum || 1323 (mp->m_binary && rn->tn_type->t_isenum))) { 1324 return; 1325 } 1326 1327 /* 1328 * Enum as offset to a pointer is an exception (otherwise enums 1329 * could not be used as array indizes). 1330 */ 1331 if (op == PLUS && 1332 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) || 1333 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) { 1334 return; 1335 } 1336 1337 /* dubious operation on enum, op %s */ 1338 warning(241, mp->m_name); 1339 1340 } 1341 1342 /* 1343 * Prints a warning if an operator is applied to two different enum types. 1344 */ 1345 static void 1346 chkeop2(op_t op, int arg, tnode_t *ln, tnode_t *rn) 1347 { 1348 mod_t *mp; 1349 1350 mp = &modtab[op]; 1351 1352 if (ln->tn_type->t_enum != rn->tn_type->t_enum) { 1353 switch (op) { 1354 case INIT: 1355 /* enum type mismatch in initialisation */ 1356 warning(210); 1357 break; 1358 case FARG: 1359 /* enum type mismatch, arg #%d */ 1360 warning(156, arg); 1361 break; 1362 case RETURN: 1363 /* return value type mismatch */ 1364 warning(211); 1365 break; 1366 default: 1367 /* enum type mismatch, op %s */ 1368 warning(130, mp->m_name); 1369 break; 1370 } 1371 } else if (Pflag && mp->m_comp && op != EQ && op != NE) { 1372 if (eflag) 1373 /* dubious comparisons of enums */ 1374 warning(243, mp->m_name); 1375 } 1376 } 1377 1378 /* 1379 * Prints a warning if an operator has both enum end other integer 1380 * types. 1381 */ 1382 static void 1383 chkeop1(op_t op, int arg, tnode_t *ln, tnode_t *rn) 1384 { 1385 char lbuf[64], rbuf[64]; 1386 1387 if (!eflag) 1388 return; 1389 1390 switch (op) { 1391 case INIT: 1392 /* 1393 * Initializations with 0 should be allowed. Otherwise, 1394 * we should complain about all uninitialized enums, 1395 * consequently. 1396 */ 1397 if (!rn->tn_type->t_isenum && rn->tn_op == CON && 1398 isityp(rn->tn_type->t_tspec) && rn->tn_val->v_quad == 0) { 1399 return; 1400 } 1401 /* initialisation of '%s' with '%s' */ 1402 warning(277, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1403 tyname(rbuf, sizeof(rbuf), rn->tn_type)); 1404 break; 1405 case FARG: 1406 /* combination of '%s' and '%s', arg #%d */ 1407 warning(278, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1408 tyname(rbuf, sizeof(rbuf), rn->tn_type), arg); 1409 break; 1410 case RETURN: 1411 /* combination of '%s' and '%s' in return */ 1412 warning(279, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1413 tyname(rbuf, sizeof(rbuf), rn->tn_type)); 1414 break; 1415 default: 1416 /* combination of '%s' and %s, op %s */ 1417 warning(242, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1418 tyname(rbuf, sizeof(rbuf), rn->tn_type), 1419 modtab[op].m_name); 1420 break; 1421 } 1422 } 1423 1424 /* 1425 * Build and initialize a new node. 1426 */ 1427 static tnode_t * 1428 mktnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn) 1429 { 1430 tnode_t *ntn; 1431 tspec_t t; 1432 #ifdef notyet 1433 size_t l; 1434 uint64_t rnum; 1435 #endif 1436 1437 ntn = getnode(); 1438 1439 ntn->tn_op = op; 1440 ntn->tn_type = type; 1441 ntn->tn_left = ln; 1442 ntn->tn_right = rn; 1443 1444 switch (op) { 1445 #ifdef notyet 1446 case SHR: 1447 if (rn->tn_op != CON) 1448 break; 1449 rnum = rn->tn_val->v_quad; 1450 l = tsize(ln->tn_type) / CHAR_BIT; 1451 t = ln->tn_type->t_tspec; 1452 switch (l) { 1453 case 8: 1454 if (rnum >= 56) 1455 t = UCHAR; 1456 else if (rnum >= 48) 1457 t = USHORT; 1458 else if (rnum >= 32) 1459 t = UINT; 1460 break; 1461 case 4: 1462 if (rnum >= 24) 1463 t = UCHAR; 1464 else if (rnum >= 16) 1465 t = USHORT; 1466 break; 1467 case 2: 1468 if (rnum >= 8) 1469 t = UCHAR; 1470 break; 1471 default: 1472 break; 1473 } 1474 if (t != ln->tn_type->t_tspec) 1475 ntn->tn_type->t_tspec = t; 1476 break; 1477 #endif 1478 case STAR: 1479 case FSEL: 1480 if (ln->tn_type->t_tspec == PTR) { 1481 t = ln->tn_type->t_subt->t_tspec; 1482 if (t != FUNC && t != VOID) 1483 ntn->tn_lvalue = 1; 1484 } else { 1485 LERROR("mktnode()"); 1486 } 1487 break; 1488 default: 1489 break; 1490 } 1491 1492 return ntn; 1493 } 1494 1495 /* 1496 * Performs usual conversion of operands to (unsigned) int. 1497 * 1498 * If tflag is set or the operand is a function argument with no 1499 * type information (no prototype or variable # of args), convert 1500 * float to double. 1501 */ 1502 tnode_t * 1503 promote(op_t op, int farg, tnode_t *tn) 1504 { 1505 tspec_t t; 1506 type_t *ntp; 1507 u_int len; 1508 1509 t = tn->tn_type->t_tspec; 1510 1511 if (!isatyp(t)) 1512 return (tn); 1513 1514 if (!tflag) { 1515 /* 1516 * ANSI C requires that the result is always of type INT 1517 * if INT can represent all possible values of the previous 1518 * type. 1519 */ 1520 if (tn->tn_type->t_isfield) { 1521 len = tn->tn_type->t_flen; 1522 if (size(INT) > len) { 1523 t = INT; 1524 } else { 1525 if (size(INT) != len) 1526 LERROR("promote()"); 1527 if (isutyp(t)) { 1528 t = UINT; 1529 } else { 1530 t = INT; 1531 } 1532 } 1533 } else if (t == CHAR || t == UCHAR || t == SCHAR) { 1534 t = (size(CHAR) < size(INT) || t != UCHAR) ? 1535 INT : UINT; 1536 } else if (t == SHORT || t == USHORT) { 1537 t = (size(SHORT) < size(INT) || t == SHORT) ? 1538 INT : UINT; 1539 } else if (t == ENUM) { 1540 t = INT; 1541 } else if (farg && t == FLOAT) { 1542 t = DOUBLE; 1543 } 1544 } else { 1545 /* 1546 * In traditional C, keep unsigned and promote FLOAT 1547 * to DOUBLE. 1548 */ 1549 if (t == UCHAR || t == USHORT) { 1550 t = UINT; 1551 } else if (t == CHAR || t == SCHAR || t == SHORT) { 1552 t = INT; 1553 } else if (t == FLOAT) { 1554 t = DOUBLE; 1555 } else if (t == ENUM) { 1556 t = INT; 1557 } 1558 } 1559 1560 if (t != tn->tn_type->t_tspec) { 1561 ntp = tduptyp(tn->tn_type); 1562 ntp->t_tspec = t; 1563 /* 1564 * Keep t_isenum so we are later able to check compatibility 1565 * of enum types. 1566 */ 1567 tn = convert(op, 0, ntp, tn); 1568 } 1569 1570 return (tn); 1571 } 1572 1573 /* 1574 * Insert conversions which are necessary to give both operands the same 1575 * type. This is done in different ways for traditional C and ANIS C. 1576 */ 1577 static void 1578 balance(op_t op, tnode_t **lnp, tnode_t **rnp) 1579 { 1580 tspec_t lt, rt, t; 1581 int i, u; 1582 type_t *ntp; 1583 static tspec_t tl[] = { 1584 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT, 1585 }; 1586 1587 lt = (*lnp)->tn_type->t_tspec; 1588 rt = (*rnp)->tn_type->t_tspec; 1589 1590 if (!isatyp(lt) || !isatyp(rt)) 1591 return; 1592 1593 if (!tflag) { 1594 if (lt == rt) { 1595 t = lt; 1596 } else if (lt == LCOMPLEX || rt == LCOMPLEX) { 1597 t = LCOMPLEX; 1598 } else if (lt == DCOMPLEX || rt == DCOMPLEX) { 1599 t = DCOMPLEX; 1600 } else if (lt == COMPLEX || rt == COMPLEX) { 1601 t = COMPLEX; 1602 } else if (lt == FCOMPLEX || rt == FCOMPLEX) { 1603 t = FCOMPLEX; 1604 } else if (lt == LDOUBLE || rt == LDOUBLE) { 1605 t = LDOUBLE; 1606 } else if (lt == DOUBLE || rt == DOUBLE) { 1607 t = DOUBLE; 1608 } else if (lt == FLOAT || rt == FLOAT) { 1609 t = FLOAT; 1610 } else { 1611 /* 1612 * If type A has more bits than type B it should 1613 * be able to hold all possible values of type B. 1614 */ 1615 if (size(lt) > size(rt)) { 1616 t = lt; 1617 } else if (size(lt) < size(rt)) { 1618 t = rt; 1619 } else { 1620 for (i = 3; tl[i] != INT; i++) { 1621 if (tl[i] == lt || tl[i] == rt) 1622 break; 1623 } 1624 if ((isutyp(lt) || isutyp(rt)) && 1625 !isutyp(tl[i])) { 1626 i--; 1627 } 1628 t = tl[i]; 1629 } 1630 } 1631 } else { 1632 /* Keep unsigned in traditional C */ 1633 u = isutyp(lt) || isutyp(rt); 1634 for (i = 0; tl[i] != INT; i++) { 1635 if (lt == tl[i] || rt == tl[i]) 1636 break; 1637 } 1638 t = tl[i]; 1639 if (u && isityp(t) && !isutyp(t)) 1640 t = utyp(t); 1641 } 1642 1643 if (t != lt) { 1644 ntp = tduptyp((*lnp)->tn_type); 1645 ntp->t_tspec = t; 1646 *lnp = convert(op, 0, ntp, *lnp); 1647 } 1648 if (t != rt) { 1649 ntp = tduptyp((*rnp)->tn_type); 1650 ntp->t_tspec = t; 1651 *rnp = convert(op, 0, ntp, *rnp); 1652 } 1653 } 1654 1655 /* 1656 * Insert a conversion operator, which converts the type of the node 1657 * to another given type. 1658 * If op is FARG, arg is the number of the argument (used for warnings). 1659 */ 1660 tnode_t * 1661 convert(op_t op, int arg, type_t *tp, tnode_t *tn) 1662 { 1663 tnode_t *ntn; 1664 tspec_t nt, ot, ost = NOTSPEC; 1665 1666 nt = tp->t_tspec; 1667 if ((ot = tn->tn_type->t_tspec) == PTR) 1668 ost = tn->tn_type->t_subt->t_tspec; 1669 1670 if (!tflag && !sflag && op == FARG) 1671 ptconv(arg, nt, ot, tp, tn); 1672 if (isityp(nt) && isityp(ot)) { 1673 iiconv(op, arg, nt, ot, tp, tn); 1674 } else if (nt == PTR && ((ot == PTR && ost == VOID) || isityp(ot)) && 1675 tn->tn_op == CON && tn->tn_val->v_quad == 0) { 1676 /* 0, 0L and (void *)0 may be assigned to any pointer. */ 1677 } else if (isityp(nt) && ot == PTR) { 1678 piconv(op, nt, tp, tn); 1679 } else if (nt == PTR && ot == PTR) { 1680 ppconv(op, tn, tp); 1681 } 1682 1683 ntn = getnode(); 1684 ntn->tn_op = CVT; 1685 ntn->tn_type = tp; 1686 ntn->tn_cast = op == CVT; 1687 ntn->tn_right = NULL; 1688 if (tn->tn_op != CON || nt == VOID) { 1689 ntn->tn_left = tn; 1690 } else { 1691 ntn->tn_op = CON; 1692 ntn->tn_val = tgetblk(sizeof (val_t)); 1693 cvtcon(op, arg, ntn->tn_type, ntn->tn_val, tn->tn_val); 1694 } 1695 1696 return (ntn); 1697 } 1698 1699 /* 1700 * Print a warning if a prototype causes a type conversion that is 1701 * different from what would happen to the same argument in the 1702 * absence of a prototype. 1703 * 1704 * Errors/Warnings about illegal type combinations are already printed 1705 * in asgntypok(). 1706 */ 1707 static void 1708 ptconv(int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn) 1709 { 1710 tnode_t *ptn; 1711 char buf[64]; 1712 1713 if (!isatyp(nt) || !isatyp(ot)) 1714 return; 1715 1716 /* 1717 * If the type of the formal parameter is char/short, a warning 1718 * would be useless, because functions declared the old style 1719 * can't expect char/short arguments. 1720 */ 1721 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT) 1722 return; 1723 1724 /* get default promotion */ 1725 ptn = promote(NOOP, 1, tn); 1726 ot = ptn->tn_type->t_tspec; 1727 1728 /* return if types are the same with and without prototype */ 1729 if (nt == ot || (nt == ENUM && ot == INT)) 1730 return; 1731 1732 if (isftyp(nt) != isftyp(ot) || psize(nt) != psize(ot)) { 1733 /* representation and/or width change */ 1734 if (!isityp(ot) || psize(ot) > psize(INT)) { 1735 /* conversion to '%s' due to prototype, arg #%d */ 1736 warning(259, tyname(buf, sizeof(buf), tp), arg); 1737 } 1738 } else if (hflag) { 1739 /* 1740 * they differ in sign or base type (char, short, int, 1741 * long, long long, float, double, long double) 1742 * 1743 * if they differ only in sign and the argument is a constant 1744 * and the msb of the argument is not set, print no warning 1745 */ 1746 if (ptn->tn_op == CON && isityp(nt) && styp(nt) == styp(ot) && 1747 msb(ptn->tn_val->v_quad, ot, -1) == 0) { 1748 /* ok */ 1749 } else { 1750 /* conversion to '%s' due to prototype, arg #%d */ 1751 warning(259, tyname(buf, sizeof(buf), tp), arg); 1752 } 1753 } 1754 } 1755 1756 /* 1757 * Print warnings for conversions of integer types which my cause 1758 * problems. 1759 */ 1760 /* ARGSUSED */ 1761 static void 1762 iiconv(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn) 1763 { 1764 char lbuf[64], rbuf[64], opbuf[16]; 1765 if (tn->tn_op == CON) 1766 return; 1767 1768 if (op == CVT) 1769 return; 1770 1771 if (Pflag && psize(nt) > psize(ot) && isutyp(nt) != isutyp(ot)) { 1772 /* conversion to %s may sign-extend incorrectly (, arg #%d) */ 1773 if (aflag && pflag) { 1774 if (op == FARG) { 1775 warning(297, tyname(lbuf, sizeof(lbuf), tp), 1776 arg); 1777 } else { 1778 warning(131, tyname(lbuf, sizeof(lbuf), tp)); 1779 } 1780 } 1781 } 1782 1783 if (Pflag && psize(nt) > psize(ot)) { 1784 switch (tn->tn_op) { 1785 case PLUS: 1786 case MINUS: 1787 case MULT: 1788 case SHL: 1789 warning(324, 1790 tyname(rbuf, sizeof(rbuf), gettyp(ot)), 1791 tyname(lbuf, sizeof(lbuf), tp), 1792 prtnode(opbuf, sizeof(opbuf), tn)); 1793 break; 1794 default: 1795 break; 1796 } 1797 } 1798 1799 if (psize(nt) < psize(ot) && 1800 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD || 1801 aflag > 1)) { 1802 /* conversion from '%s' may lose accuracy */ 1803 if (aflag) { 1804 if (op == FARG) { 1805 warning(298, 1806 tyname(rbuf, sizeof(rbuf), tn->tn_type), 1807 tyname(lbuf, sizeof(lbuf), tp), 1808 arg); 1809 } else { 1810 warning(132, 1811 tyname(rbuf, sizeof(rbuf), tn->tn_type), 1812 tyname(lbuf, sizeof(lbuf), tp)); 1813 } 1814 } 1815 } 1816 } 1817 1818 /* 1819 * Print warnings for dubious conversions of pointer to integer. 1820 */ 1821 static void 1822 piconv(op_t op, tspec_t nt, type_t *tp, tnode_t *tn) 1823 { 1824 char buf[64]; 1825 1826 if (tn->tn_op == CON) 1827 return; 1828 1829 if (op != CVT) { 1830 /* We got already an error. */ 1831 return; 1832 } 1833 1834 if (psize(nt) < psize(PTR)) { 1835 if (pflag && size(nt) >= size(PTR)) { 1836 /* conv. of pointer to %s may lose bits */ 1837 warning(134, tyname(buf, sizeof(buf), tp)); 1838 } else { 1839 /* conv. of pointer to %s loses bits */ 1840 warning(133, tyname(buf, sizeof(buf), tp)); 1841 } 1842 } 1843 } 1844 1845 /* 1846 * Print warnings for questionable pointer conversions. 1847 */ 1848 static void 1849 ppconv(op_t op, tnode_t *tn, type_t *tp) 1850 { 1851 tspec_t nt, ot; 1852 const char *nts, *ots; 1853 1854 /* 1855 * We got already an error (pointers of different types 1856 * without a cast) or we will not get a warning. 1857 */ 1858 if (op != CVT) 1859 return; 1860 1861 nt = tp->t_subt->t_tspec; 1862 ot = tn->tn_type->t_subt->t_tspec; 1863 1864 if (nt == VOID || ot == VOID) { 1865 if (sflag && (nt == FUNC || ot == FUNC)) { 1866 /* (void *)0 already handled in convert() */ 1867 *(nt == FUNC ? &nts : &ots) = "function pointer"; 1868 *(nt == VOID ? &nts : &ots) = "'void *'"; 1869 /* ANSI C forbids conversion of %s to %s */ 1870 warning(303, ots, nts); 1871 } 1872 return; 1873 } else if (nt == FUNC && ot == FUNC) { 1874 return; 1875 } else if (nt == FUNC || ot == FUNC) { 1876 /* questionable conversion of function pointer */ 1877 warning(229); 1878 return; 1879 } 1880 1881 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) { 1882 if (hflag) 1883 /* possible pointer alignment problem */ 1884 warning(135); 1885 } 1886 if (((nt == STRUCT || nt == UNION) && 1887 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) || 1888 psize(nt) != psize(ot)) { 1889 if (cflag) { 1890 /* pointer casts may be troublesome */ 1891 warning(247); 1892 } 1893 } 1894 } 1895 1896 /* 1897 * Converts a typed constant in a constant of another type. 1898 * 1899 * op operator which requires conversion 1900 * arg if op is FARG, # of argument 1901 * tp type in which to convert the constant 1902 * nv new constant 1903 * v old constant 1904 */ 1905 void 1906 cvtcon(op_t op, int arg, type_t *tp, val_t *nv, val_t *v) 1907 { 1908 char lbuf[64], rbuf[64]; 1909 tspec_t ot, nt; 1910 ldbl_t max = 0.0, min = 0.0; 1911 int sz, rchk; 1912 int64_t xmask, xmsk1; 1913 int osz, nsz; 1914 1915 ot = v->v_tspec; 1916 nt = nv->v_tspec = tp->t_tspec; 1917 rchk = 0; 1918 1919 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) { 1920 switch (nt) { 1921 case BOOL: 1922 max = 1; min = 0; break; 1923 case CHAR: 1924 max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break; 1925 case UCHAR: 1926 max = TARG_UCHAR_MAX; min = 0; break; 1927 case SCHAR: 1928 max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break; 1929 case SHORT: 1930 max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break; 1931 case USHORT: 1932 max = TARG_USHRT_MAX; min = 0; break; 1933 case ENUM: 1934 case INT: 1935 max = TARG_INT_MAX; min = TARG_INT_MIN; break; 1936 case UINT: 1937 max = (u_int)TARG_UINT_MAX;min = 0; break; 1938 case LONG: 1939 max = TARG_LONG_MAX; min = TARG_LONG_MIN; break; 1940 case ULONG: 1941 max = (u_long)TARG_ULONG_MAX; min = 0; break; 1942 case QUAD: 1943 max = QUAD_MAX; min = QUAD_MIN; break; 1944 case UQUAD: 1945 max = (uint64_t)UQUAD_MAX; min = 0; break; 1946 case FLOAT: 1947 case FCOMPLEX: 1948 max = FLT_MAX; min = -FLT_MAX; break; 1949 case DOUBLE: 1950 case DCOMPLEX: 1951 max = DBL_MAX; min = -DBL_MAX; break; 1952 case PTR: 1953 /* Got already an error because of float --> ptr */ 1954 case LDOUBLE: 1955 case LCOMPLEX: 1956 max = LDBL_MAX; min = -LDBL_MAX; break; 1957 default: 1958 LERROR("cvtcon()"); 1959 } 1960 if (v->v_ldbl > max || v->v_ldbl < min) { 1961 if (nt == LDOUBLE) 1962 LERROR("cvtcon()"); 1963 if (op == FARG) { 1964 /* conv. of %s to %s is out of rng., arg #%d */ 1965 warning(295, tyname(lbuf, sizeof(lbuf), 1966 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 1967 arg); 1968 } else { 1969 /* conversion of %s to %s is out of range */ 1970 warning(119, tyname(lbuf, sizeof(lbuf), 1971 gettyp(ot)), 1972 tyname(rbuf, sizeof(rbuf), tp)); 1973 } 1974 v->v_ldbl = v->v_ldbl > 0 ? max : min; 1975 } 1976 if (nt == FLOAT) { 1977 nv->v_ldbl = (float)v->v_ldbl; 1978 } else if (nt == DOUBLE) { 1979 nv->v_ldbl = (double)v->v_ldbl; 1980 } else if (nt == LDOUBLE) { 1981 nv->v_ldbl = v->v_ldbl; 1982 } else { 1983 nv->v_quad = (nt == PTR || isutyp(nt)) ? 1984 (int64_t)v->v_ldbl : (int64_t)v->v_ldbl; 1985 } 1986 } else { 1987 if (nt == FLOAT) { 1988 nv->v_ldbl = (ot == PTR || isutyp(ot)) ? 1989 (float)(uint64_t)v->v_quad : (float)v->v_quad; 1990 } else if (nt == DOUBLE) { 1991 nv->v_ldbl = (ot == PTR || isutyp(ot)) ? 1992 (double)(uint64_t)v->v_quad : (double)v->v_quad; 1993 } else if (nt == LDOUBLE) { 1994 nv->v_ldbl = (ot == PTR || isutyp(ot)) ? 1995 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad; 1996 } else { 1997 rchk = 1; /* Check for lost precision. */ 1998 nv->v_quad = v->v_quad; 1999 } 2000 } 2001 2002 if (v->v_ansiu && isftyp(nt)) { 2003 /* ANSI C treats constant as unsigned */ 2004 warning(157); 2005 v->v_ansiu = 0; 2006 } else if (v->v_ansiu && (isityp(nt) && !isutyp(nt) && 2007 psize(nt) > psize(ot))) { 2008 /* ANSI C treats constant as unsigned */ 2009 warning(157); 2010 v->v_ansiu = 0; 2011 } 2012 2013 switch (nt) { 2014 case FLOAT: 2015 case FCOMPLEX: 2016 case DOUBLE: 2017 case DCOMPLEX: 2018 case LDOUBLE: 2019 case LCOMPLEX: 2020 break; 2021 default: 2022 sz = tp->t_isfield ? tp->t_flen : size(nt); 2023 nv->v_quad = xsign(nv->v_quad, nt, sz); 2024 break; 2025 } 2026 2027 if (rchk && op != CVT) { 2028 osz = size(ot); 2029 nsz = tp->t_isfield ? tp->t_flen : size(nt); 2030 xmask = qlmasks[nsz] ^ qlmasks[osz]; 2031 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1]; 2032 /* 2033 * For bitwise operations we are not interested in the 2034 * value, but in the bits itself. 2035 */ 2036 if (op == ORASS || op == OR || op == XOR) { 2037 /* 2038 * Print a warning if bits which were set are 2039 * lost due to the conversion. 2040 * This can happen with operator ORASS only. 2041 */ 2042 if (nsz < osz && (v->v_quad & xmask) != 0) { 2043 /* constant truncated by conv., op %s */ 2044 warning(306, modtab[op].m_name); 2045 } 2046 } else if (op == ANDASS || op == AND) { 2047 /* 2048 * Print a warning if additional bits are not all 1 2049 * and the most significant bit of the old value is 1, 2050 * or if at least one (but not all) removed bit was 0. 2051 */ 2052 if (nsz > osz && 2053 (nv->v_quad & qbmasks[osz - 1]) != 0 && 2054 (nv->v_quad & xmask) != xmask) { 2055 /* 2056 * extra bits set to 0 in conversion 2057 * of '%s' to '%s', op %s 2058 */ 2059 warning(309, tyname(lbuf, sizeof(lbuf), 2060 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 2061 modtab[op].m_name); 2062 } else if (nsz < osz && 2063 (v->v_quad & xmask) != xmask && 2064 (v->v_quad & xmask) != 0) { 2065 /* const. truncated by conv., op %s */ 2066 warning(306, modtab[op].m_name); 2067 } 2068 } else if ((nt != PTR && isutyp(nt)) && 2069 (ot != PTR && !isutyp(ot)) && v->v_quad < 0) { 2070 if (op == ASSIGN) { 2071 /* assignment of negative constant to ... */ 2072 warning(164); 2073 } else if (op == INIT) { 2074 /* initialisation of unsigned with neg. ... */ 2075 warning(221); 2076 } else if (op == FARG) { 2077 /* conversion of neg. const. to ..., arg #%d */ 2078 warning(296, arg); 2079 } else if (modtab[op].m_comp) { 2080 /* we get this warning already in chkcomp() */ 2081 } else { 2082 /* conversion of negative constant to ... */ 2083 warning(222); 2084 } 2085 } else if (nv->v_quad != v->v_quad && nsz <= osz && 2086 (v->v_quad & xmask) != 0 && 2087 (isutyp(ot) || (v->v_quad & xmsk1) != xmsk1)) { 2088 /* 2089 * Loss of significant bit(s). All truncated bits 2090 * of unsigned types or all truncated bits plus the 2091 * msb of the target for signed types are considered 2092 * to be significant bits. Loss of significant bits 2093 * means that at least on of the bits was set in an 2094 * unsigned type or that at least one, but not all of 2095 * the bits was set in an signed type. 2096 * Loss of significant bits means that it is not 2097 * possible, also not with necessary casts, to convert 2098 * back to the original type. A example for a 2099 * necessary cast is: 2100 * char c; int i; c = 128; 2101 * i = c; ** yields -128 ** 2102 * i = (unsigned char)c; ** yields 128 ** 2103 */ 2104 if (op == ASSIGN && tp->t_isfield) { 2105 /* precision lost in bit-field assignment */ 2106 warning(166); 2107 } else if (op == ASSIGN) { 2108 /* constant truncated by assignment */ 2109 warning(165); 2110 } else if (op == INIT && tp->t_isfield) { 2111 /* bit-field initializer does not fit */ 2112 warning(180); 2113 } else if (op == INIT) { 2114 /* initializer does not fit */ 2115 warning(178); 2116 } else if (op == CASE) { 2117 /* case label affected by conversion */ 2118 warning(196); 2119 } else if (op == FARG) { 2120 /* conv. of %s to %s is out of rng., arg #%d */ 2121 warning(295, tyname(lbuf, sizeof(lbuf), 2122 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 2123 arg); 2124 } else { 2125 /* conversion of %s to %s is out of range */ 2126 warning(119, tyname(lbuf, sizeof(lbuf), 2127 gettyp(ot)), 2128 tyname(rbuf, sizeof(rbuf), tp)); 2129 } 2130 } else if (nv->v_quad != v->v_quad) { 2131 if (op == ASSIGN && tp->t_isfield) { 2132 /* precision lost in bit-field assignment */ 2133 warning(166); 2134 } else if (op == INIT && tp->t_isfield) { 2135 /* bit-field initializer out of range */ 2136 warning(11); 2137 } else if (op == CASE) { 2138 /* case label affected by conversion */ 2139 warning(196); 2140 } else if (op == FARG) { 2141 /* conv. of %s to %s is out of rng., arg #%d */ 2142 warning(295, tyname(lbuf, sizeof(lbuf), 2143 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 2144 arg); 2145 } else { 2146 /* conversion of %s to %s is out of range */ 2147 warning(119, tyname(lbuf, sizeof(lbuf), 2148 gettyp(ot)), 2149 tyname(rbuf, sizeof(rbuf), tp)); 2150 } 2151 } 2152 } 2153 } 2154 2155 /* 2156 * Called if incompatible types were detected. 2157 * Prints a appropriate warning. 2158 */ 2159 static void 2160 incompat(op_t op, tspec_t lt, tspec_t rt) 2161 { 2162 mod_t *mp; 2163 int e = 0; 2164 2165 mp = &modtab[op]; 2166 2167 if (lt == VOID || (mp->m_binary && rt == VOID)) { 2168 /* void type illegal in expression */ 2169 e = 109; 2170 } else if (op == ASSIGN) { 2171 if ((lt == STRUCT || lt == UNION) && 2172 (rt == STRUCT || rt == UNION)) { 2173 /* assignment of different structures */ 2174 e = 240; 2175 } else { 2176 /* assignment type mismatch */ 2177 e = 171; 2178 } 2179 } else if (mp->m_binary) { 2180 /* operands of %s have incompatible types */ 2181 e = 107; 2182 } else { 2183 /* operand of %s has incompatible type */ 2184 e = 108; 2185 } 2186 switch (e) { 2187 case 0: 2188 return; 2189 case 109: 2190 error(e); 2191 return; 2192 case 108: 2193 case 107: 2194 error(e, mp->m_name, basictyname(lt), basictyname(rt)); 2195 return; 2196 default: 2197 error(e, basictyname(lt), basictyname(rt)); 2198 return; 2199 } 2200 } 2201 2202 /* 2203 * Called if incompatible pointer types are detected. 2204 * Print an appropriate warning. 2205 */ 2206 static void 2207 illptrc(mod_t *mp, type_t *ltp, type_t *rtp) 2208 { 2209 tspec_t lt, rt; 2210 2211 if (ltp->t_tspec != PTR || rtp->t_tspec != PTR) 2212 LERROR("illptrc()"); 2213 2214 lt = ltp->t_subt->t_tspec; 2215 rt = rtp->t_subt->t_tspec; 2216 2217 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) { 2218 if (mp == NULL) { 2219 /* illegal structure pointer combination */ 2220 warning(244); 2221 } else { 2222 /* illegal structure pointer combination, op %s */ 2223 warning(245, mp->m_name); 2224 } 2225 } else { 2226 if (mp == NULL) { 2227 /* illegal pointer combination */ 2228 warning(184); 2229 } else { 2230 /* illegal pointer combination, op %s */ 2231 warning(124, mp->m_name); 2232 } 2233 } 2234 } 2235 2236 /* 2237 * Make sure type (*tpp)->t_subt has at least the qualifiers 2238 * of tp1->t_subt and tp2->t_subt. 2239 */ 2240 static void 2241 mrgqual(type_t **tpp, type_t *tp1, type_t *tp2) 2242 { 2243 2244 if ((*tpp)->t_tspec != PTR || 2245 tp1->t_tspec != PTR || tp2->t_tspec != PTR) { 2246 LERROR("mrgqual()"); 2247 } 2248 2249 if ((*tpp)->t_subt->t_const == 2250 (tp1->t_subt->t_const | tp2->t_subt->t_const) && 2251 (*tpp)->t_subt->t_volatile == 2252 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) { 2253 return; 2254 } 2255 2256 *tpp = tduptyp(*tpp); 2257 (*tpp)->t_subt = tduptyp((*tpp)->t_subt); 2258 (*tpp)->t_subt->t_const = 2259 tp1->t_subt->t_const | tp2->t_subt->t_const; 2260 (*tpp)->t_subt->t_volatile = 2261 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile; 2262 } 2263 2264 /* 2265 * Returns 1 if the given structure or union has a constant member 2266 * (maybe recursively). 2267 */ 2268 static int 2269 conmemb(type_t *tp) 2270 { 2271 sym_t *m; 2272 tspec_t t; 2273 2274 if ((t = tp->t_tspec) != STRUCT && t != UNION) 2275 LERROR("conmemb()"); 2276 for (m = tp->t_str->memb; m != NULL; m = m->s_nxt) { 2277 tp = m->s_type; 2278 if (tp->t_const) 2279 return (1); 2280 if ((t = tp->t_tspec) == STRUCT || t == UNION) { 2281 if (conmemb(m->s_type)) 2282 return (1); 2283 } 2284 } 2285 return (0); 2286 } 2287 2288 /* 2289 * Create a new node for one of the operators POINT and ARROW. 2290 */ 2291 static tnode_t * 2292 bldstr(op_t op, tnode_t *ln, tnode_t *rn) 2293 { 2294 tnode_t *ntn, *ctn; 2295 int nolval; 2296 2297 if (rn->tn_op != NAME) 2298 LERROR("bldstr()"); 2299 if (rn->tn_sym->s_value.v_tspec != INT) 2300 LERROR("bldstr()"); 2301 if (rn->tn_sym->s_scl != MOS && rn->tn_sym->s_scl != MOU) 2302 LERROR("bldstr()"); 2303 2304 /* 2305 * Remember if the left operand is an lvalue (structure members 2306 * are lvalues if and only if the structure itself is an lvalue). 2307 */ 2308 nolval = op == POINT && !ln->tn_lvalue; 2309 2310 if (op == POINT) { 2311 ln = bldamper(ln, 1); 2312 } else if (ln->tn_type->t_tspec != PTR) { 2313 if (!tflag || !isityp(ln->tn_type->t_tspec)) 2314 LERROR("bldstr()"); 2315 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln); 2316 } 2317 2318 #if PTRDIFF_IS_LONG 2319 ctn = getinode(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT); 2320 #else 2321 ctn = getinode(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT); 2322 #endif 2323 2324 ntn = mktnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn); 2325 if (ln->tn_op == CON) 2326 ntn = fold(ntn); 2327 2328 if (rn->tn_type->t_isfield) { 2329 ntn = mktnode(FSEL, ntn->tn_type->t_subt, ntn, NULL); 2330 } else { 2331 ntn = mktnode(STAR, ntn->tn_type->t_subt, ntn, NULL); 2332 } 2333 2334 if (nolval) 2335 ntn->tn_lvalue = 0; 2336 2337 return (ntn); 2338 } 2339 2340 /* 2341 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF. 2342 */ 2343 static tnode_t * 2344 bldincdec(op_t op, tnode_t *ln) 2345 { 2346 tnode_t *cn, *ntn; 2347 2348 if (ln == NULL) 2349 LERROR("bldincdec()"); 2350 2351 if (ln->tn_type->t_tspec == PTR) { 2352 cn = plength(ln->tn_type); 2353 } else { 2354 cn = getinode(INT, (int64_t)1); 2355 } 2356 ntn = mktnode(op, ln->tn_type, ln, cn); 2357 2358 return (ntn); 2359 } 2360 2361 /* 2362 * Create a node for REAL, IMAG 2363 */ 2364 static tnode_t * 2365 bldri(op_t op, tnode_t *ln) 2366 { 2367 tnode_t *cn, *ntn; 2368 char buf[64]; 2369 2370 if (ln == NULL) 2371 LERROR("bldincdec()"); 2372 2373 switch (ln->tn_type->t_tspec) { 2374 case LCOMPLEX: 2375 cn = getinode(LDOUBLE, (int64_t)1); 2376 break; 2377 case DCOMPLEX: 2378 cn = getinode(DOUBLE, (int64_t)1); 2379 break; 2380 case FCOMPLEX: 2381 cn = getinode(FLOAT, (int64_t)1); 2382 break; 2383 default: 2384 error(276, op == REAL ? "real" : "imag", 2385 tyname(buf, sizeof(buf), ln->tn_type)); 2386 return NULL; 2387 } 2388 ntn = mktnode(op, cn->tn_type, ln, cn); 2389 ntn->tn_lvalue = 1; 2390 2391 return (ntn); 2392 } 2393 /* 2394 * Create a tree node for the & operator 2395 */ 2396 static tnode_t * 2397 bldamper(tnode_t *tn, int noign) 2398 { 2399 tnode_t *ntn; 2400 tspec_t t; 2401 2402 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) { 2403 /* & before array or function: ignored */ 2404 if (tflag) 2405 warning(127); 2406 return (tn); 2407 } 2408 2409 /* eliminate &* */ 2410 if (tn->tn_op == STAR && 2411 tn->tn_left->tn_type->t_tspec == PTR && 2412 tn->tn_left->tn_type->t_subt == tn->tn_type) { 2413 return (tn->tn_left); 2414 } 2415 2416 ntn = mktnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL); 2417 2418 return (ntn); 2419 } 2420 2421 /* 2422 * Create a node for operators PLUS and MINUS. 2423 */ 2424 static tnode_t * 2425 bldplmi(op_t op, tnode_t *ln, tnode_t *rn) 2426 { 2427 tnode_t *ntn, *ctn; 2428 type_t *tp; 2429 2430 /* If pointer and integer, then pointer to the lhs. */ 2431 if (rn->tn_type->t_tspec == PTR && isityp(ln->tn_type->t_tspec)) { 2432 ntn = ln; 2433 ln = rn; 2434 rn = ntn; 2435 } 2436 2437 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) { 2438 2439 if (!isityp(rn->tn_type->t_tspec)) 2440 LERROR("bldplmi()"); 2441 2442 ctn = plength(ln->tn_type); 2443 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec) 2444 rn = convert(NOOP, 0, ctn->tn_type, rn); 2445 rn = mktnode(MULT, rn->tn_type, rn, ctn); 2446 if (rn->tn_left->tn_op == CON) 2447 rn = fold(rn); 2448 ntn = mktnode(op, ln->tn_type, ln, rn); 2449 2450 } else if (rn->tn_type->t_tspec == PTR) { 2451 2452 if (ln->tn_type->t_tspec != PTR || op != MINUS) 2453 LERROR("bldplmi()"); 2454 #if PTRDIFF_IS_LONG 2455 tp = gettyp(LONG); 2456 #else 2457 tp = gettyp(INT); 2458 #endif 2459 ntn = mktnode(op, tp, ln, rn); 2460 if (ln->tn_op == CON && rn->tn_op == CON) 2461 ntn = fold(ntn); 2462 ctn = plength(ln->tn_type); 2463 balance(NOOP, &ntn, &ctn); 2464 ntn = mktnode(DIV, tp, ntn, ctn); 2465 2466 } else { 2467 2468 ntn = mktnode(op, ln->tn_type, ln, rn); 2469 2470 } 2471 return (ntn); 2472 } 2473 2474 /* 2475 * Create a node for operators SHL and SHR. 2476 */ 2477 static tnode_t * 2478 bldshft(op_t op, tnode_t *ln, tnode_t *rn) 2479 { 2480 tspec_t t; 2481 tnode_t *ntn; 2482 2483 if ((t = rn->tn_type->t_tspec) != INT && t != UINT) 2484 rn = convert(CVT, 0, gettyp(INT), rn); 2485 ntn = mktnode(op, ln->tn_type, ln, rn); 2486 return (ntn); 2487 } 2488 2489 /* 2490 * Create a node for COLON. 2491 */ 2492 static tnode_t * 2493 bldcol(tnode_t *ln, tnode_t *rn) 2494 { 2495 tspec_t lt, rt, pdt; 2496 type_t *rtp; 2497 tnode_t *ntn; 2498 2499 lt = ln->tn_type->t_tspec; 2500 rt = rn->tn_type->t_tspec; 2501 #if PTRDIFF_IS_LONG 2502 pdt = LONG; 2503 #else 2504 pdt = INT; 2505 #endif 2506 2507 /* 2508 * Arithmetic types are balanced, all other type combinations 2509 * still need to be handled. 2510 */ 2511 if (isatyp(lt) && isatyp(rt)) { 2512 rtp = ln->tn_type; 2513 } else if (lt == VOID || rt == VOID) { 2514 rtp = gettyp(VOID); 2515 } else if (lt == STRUCT || lt == UNION) { 2516 /* Both types must be identical. */ 2517 if (rt != STRUCT && rt != UNION) 2518 LERROR("bldcol()"); 2519 if (ln->tn_type->t_str != rn->tn_type->t_str) 2520 LERROR("bldcol()"); 2521 if (incompl(ln->tn_type)) { 2522 /* unknown operand size, op %s */ 2523 error(138, modtab[COLON].m_name); 2524 return (NULL); 2525 } 2526 rtp = ln->tn_type; 2527 } else if (lt == PTR && isityp(rt)) { 2528 if (rt != pdt) { 2529 rn = convert(NOOP, 0, gettyp(pdt), rn); 2530 rt = pdt; 2531 } 2532 rtp = ln->tn_type; 2533 } else if (rt == PTR && isityp(lt)) { 2534 if (lt != pdt) { 2535 ln = convert(NOOP, 0, gettyp(pdt), ln); 2536 lt = pdt; 2537 } 2538 rtp = rn->tn_type; 2539 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) { 2540 if (rt != PTR) 2541 LERROR("bldcol()"); 2542 rtp = rn->tn_type; 2543 mrgqual(&rtp, ln->tn_type, rn->tn_type); 2544 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) { 2545 if (lt != PTR) 2546 LERROR("bldcol()"); 2547 rtp = ln->tn_type; 2548 mrgqual(&rtp, ln->tn_type, rn->tn_type); 2549 } else { 2550 if (lt != PTR || rt != PTR) 2551 LERROR("bldcol()"); 2552 /* 2553 * XXX For now we simply take the left type. This is 2554 * probably wrong, if one type contains a functionprototype 2555 * and the other one, at the same place, only an old style 2556 * declaration. 2557 */ 2558 rtp = ln->tn_type; 2559 mrgqual(&rtp, ln->tn_type, rn->tn_type); 2560 } 2561 2562 ntn = mktnode(COLON, rtp, ln, rn); 2563 2564 return (ntn); 2565 } 2566 2567 /* 2568 * Create a node for an assignment operator (both = and op= ). 2569 */ 2570 static tnode_t * 2571 bldasgn(op_t op, tnode_t *ln, tnode_t *rn) 2572 { 2573 tspec_t lt, rt; 2574 tnode_t *ntn, *ctn; 2575 2576 if (ln == NULL || rn == NULL) 2577 LERROR("bldasgn()"); 2578 2579 lt = ln->tn_type->t_tspec; 2580 rt = rn->tn_type->t_tspec; 2581 2582 if ((op == ADDASS || op == SUBASS) && lt == PTR) { 2583 if (!isityp(rt)) 2584 LERROR("bldasgn()"); 2585 ctn = plength(ln->tn_type); 2586 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec) 2587 rn = convert(NOOP, 0, ctn->tn_type, rn); 2588 rn = mktnode(MULT, rn->tn_type, rn, ctn); 2589 if (rn->tn_left->tn_op == CON) 2590 rn = fold(rn); 2591 } 2592 2593 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) { 2594 if (rt != lt || ln->tn_type->t_str != rn->tn_type->t_str) 2595 LERROR("bldasgn()"); 2596 if (incompl(ln->tn_type)) { 2597 if (op == RETURN) { 2598 /* cannot return incomplete type */ 2599 error(212); 2600 } else { 2601 /* unknown operand size, op %s */ 2602 error(138, modtab[op].m_name); 2603 } 2604 return (NULL); 2605 } 2606 } 2607 2608 if (op == SHLASS) { 2609 if (psize(lt) < psize(rt)) { 2610 if (hflag) 2611 /* semantics of %s change in ANSI C; use ... */ 2612 warning(118, "<<="); 2613 } 2614 } else if (op != SHRASS) { 2615 if (op == ASSIGN || lt != PTR) { 2616 if (lt != rt || 2617 (ln->tn_type->t_isfield && rn->tn_op == CON)) { 2618 rn = convert(op, 0, ln->tn_type, rn); 2619 rt = lt; 2620 } 2621 } 2622 } 2623 2624 ntn = mktnode(op, ln->tn_type, ln, rn); 2625 2626 return (ntn); 2627 } 2628 2629 /* 2630 * Get length of type tp->t_subt. 2631 */ 2632 static tnode_t * 2633 plength(type_t *tp) 2634 { 2635 int elem, elsz; 2636 tspec_t st; 2637 2638 if (tp->t_tspec != PTR) 2639 LERROR("plength()"); 2640 tp = tp->t_subt; 2641 2642 elem = 1; 2643 elsz = 0; 2644 2645 while (tp->t_tspec == ARRAY) { 2646 elem *= tp->t_dim; 2647 tp = tp->t_subt; 2648 } 2649 2650 switch (tp->t_tspec) { 2651 case FUNC: 2652 /* pointer to function is not allowed here */ 2653 error(110); 2654 break; 2655 case VOID: 2656 /* cannot do pointer arithmetic on operand of ... */ 2657 (void)gnuism(136); 2658 break; 2659 case STRUCT: 2660 case UNION: 2661 if ((elsz = tp->t_str->size) == 0) 2662 /* cannot do pointer arithmetic on operand of ... */ 2663 error(136); 2664 break; 2665 case ENUM: 2666 if (incompl(tp)) { 2667 /* cannot do pointer arithmetic on operand of ... */ 2668 warning(136); 2669 } 2670 /* FALLTHROUGH */ 2671 default: 2672 if ((elsz = size(tp->t_tspec)) == 0) { 2673 /* cannot do pointer arithmetic on operand of ... */ 2674 error(136); 2675 } else if (elsz == -1) { 2676 LERROR("plength()"); 2677 } 2678 break; 2679 } 2680 2681 if (elem == 0 && elsz != 0) { 2682 /* cannot do pointer arithmetic on operand of ... */ 2683 error(136); 2684 } 2685 2686 if (elsz == 0) 2687 elsz = CHAR_BIT; 2688 2689 #if PTRDIFF_IS_LONG 2690 st = LONG; 2691 #else 2692 st = INT; 2693 #endif 2694 2695 return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT))); 2696 } 2697 2698 /* 2699 * XXX 2700 * Note: There appear to be a number of bugs in detecting overflow in 2701 * this function. An audit and a set of proper regression tests are needed. 2702 * --Perry Metzger, Nov. 16, 2001 2703 */ 2704 /* 2705 * Do only as much as necessary to compute constant expressions. 2706 * Called only if the operator allows folding and (both) operands 2707 * are constants. 2708 */ 2709 static tnode_t * 2710 fold(tnode_t *tn) 2711 { 2712 val_t *v; 2713 tspec_t t; 2714 int utyp, ovfl; 2715 int64_t sl, sr = 0, q = 0, mask; 2716 uint64_t ul, ur = 0; 2717 tnode_t *cn; 2718 2719 v = xcalloc(1, sizeof (val_t)); 2720 v->v_tspec = t = tn->tn_type->t_tspec; 2721 2722 utyp = t == PTR || isutyp(t); 2723 ul = sl = tn->tn_left->tn_val->v_quad; 2724 if (modtab[tn->tn_op].m_binary) 2725 ur = sr = tn->tn_right->tn_val->v_quad; 2726 2727 mask = qlmasks[size(t)]; 2728 ovfl = 0; 2729 2730 switch (tn->tn_op) { 2731 case UPLUS: 2732 q = sl; 2733 break; 2734 case UMINUS: 2735 q = -sl; 2736 if (sl != 0 && msb(q, t, -1) == msb(sl, t, -1)) 2737 ovfl = 1; 2738 break; 2739 case COMPL: 2740 q = ~sl; 2741 break; 2742 case MULT: 2743 if (utyp) { 2744 q = ul * ur; 2745 if (q != (q & mask)) 2746 ovfl = 1; 2747 else if ((ul != 0) && ((q / ul) != ur)) 2748 ovfl = 1; 2749 } else { 2750 q = sl * sr; 2751 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1))) 2752 ovfl = 1; 2753 } 2754 break; 2755 case DIV: 2756 if (sr == 0) { 2757 /* division by 0 */ 2758 error(139); 2759 q = utyp ? UQUAD_MAX : QUAD_MAX; 2760 } else { 2761 q = utyp ? (int64_t)(ul / ur) : sl / sr; 2762 } 2763 break; 2764 case MOD: 2765 if (sr == 0) { 2766 /* modulus by 0 */ 2767 error(140); 2768 q = 0; 2769 } else { 2770 q = utyp ? (int64_t)(ul % ur) : sl % sr; 2771 } 2772 break; 2773 case PLUS: 2774 q = utyp ? (int64_t)(ul + ur) : sl + sr; 2775 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) { 2776 if (msb(q, t, -1) == 0) 2777 ovfl = 1; 2778 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) { 2779 if (msb(q, t, -1) != 0) 2780 ovfl = 1; 2781 } 2782 break; 2783 case MINUS: 2784 q = utyp ? (int64_t)(ul - ur) : sl - sr; 2785 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) { 2786 if (msb(q, t, -1) == 0) 2787 ovfl = 1; 2788 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) { 2789 if (msb(q, t, -1) != 0) 2790 ovfl = 1; 2791 } 2792 break; 2793 case SHL: 2794 q = utyp ? (int64_t)(ul << sr) : sl << sr; 2795 break; 2796 case SHR: 2797 /* 2798 * The sign must be explicitly extended because 2799 * shifts of signed values are implementation dependent. 2800 */ 2801 q = ul >> sr; 2802 q = xsign(q, t, size(t) - (int)sr); 2803 break; 2804 case LT: 2805 q = utyp ? ul < ur : sl < sr; 2806 break; 2807 case LE: 2808 q = utyp ? ul <= ur : sl <= sr; 2809 break; 2810 case GE: 2811 q = utyp ? ul >= ur : sl >= sr; 2812 break; 2813 case GT: 2814 q = utyp ? ul > ur : sl > sr; 2815 break; 2816 case EQ: 2817 q = utyp ? ul == ur : sl == sr; 2818 break; 2819 case NE: 2820 q = utyp ? ul != ur : sl != sr; 2821 break; 2822 case AND: 2823 q = utyp ? (int64_t)(ul & ur) : sl & sr; 2824 break; 2825 case XOR: 2826 q = utyp ? (int64_t)(ul ^ ur) : sl ^ sr; 2827 break; 2828 case OR: 2829 q = utyp ? (int64_t)(ul | ur) : sl | sr; 2830 break; 2831 default: 2832 LERROR("fold()"); 2833 } 2834 2835 /* XXX does not work for quads. */ 2836 if (ovfl || ((uint64_t)(q | mask) != ~(uint64_t)0 && 2837 (q & ~mask) != 0)) { 2838 if (hflag) 2839 /* integer overflow detected, op %s */ 2840 warning(141, modtab[tn->tn_op].m_name); 2841 } 2842 2843 v->v_quad = xsign(q, t, -1); 2844 2845 cn = getcnode(tn->tn_type, v); 2846 2847 return (cn); 2848 } 2849 2850 /* 2851 * Same for operators whose operands are compared with 0 (test context). 2852 */ 2853 static tnode_t * 2854 foldtst(tnode_t *tn) 2855 { 2856 int l, r = 0; 2857 val_t *v; 2858 2859 v = xcalloc(1, sizeof (val_t)); 2860 v->v_tspec = tn->tn_type->t_tspec; 2861 if (tn->tn_type->t_tspec != INT) 2862 LERROR("foldtst()"); 2863 2864 if (isftyp(tn->tn_left->tn_type->t_tspec)) { 2865 l = tn->tn_left->tn_val->v_ldbl != 0.0; 2866 } else { 2867 l = tn->tn_left->tn_val->v_quad != 0; 2868 } 2869 2870 if (modtab[tn->tn_op].m_binary) { 2871 if (isftyp(tn->tn_right->tn_type->t_tspec)) { 2872 r = tn->tn_right->tn_val->v_ldbl != 0.0; 2873 } else { 2874 r = tn->tn_right->tn_val->v_quad != 0; 2875 } 2876 } 2877 2878 switch (tn->tn_op) { 2879 case NOT: 2880 if (hflag && !ccflg) 2881 /* constant argument to NOT */ 2882 warning(239); 2883 v->v_quad = !l; 2884 break; 2885 case LOGAND: 2886 v->v_quad = l && r; 2887 break; 2888 case LOGOR: 2889 v->v_quad = l || r; 2890 break; 2891 default: 2892 LERROR("foldtst()"); 2893 } 2894 2895 return (getcnode(tn->tn_type, v)); 2896 } 2897 2898 /* 2899 * Same for operands with floating point type. 2900 */ 2901 static tnode_t * 2902 foldflt(tnode_t *tn) 2903 { 2904 val_t *v; 2905 tspec_t t; 2906 ldbl_t l, r = 0; 2907 2908 fpe = 0; 2909 v = xcalloc(1, sizeof (val_t)); 2910 v->v_tspec = t = tn->tn_type->t_tspec; 2911 2912 if (!isftyp(t)) 2913 LERROR("foldflt()"); 2914 2915 if (t != tn->tn_left->tn_type->t_tspec) 2916 LERROR("foldflt()"); 2917 if (modtab[tn->tn_op].m_binary && t != tn->tn_right->tn_type->t_tspec) 2918 LERROR("foldflt()"); 2919 2920 l = tn->tn_left->tn_val->v_ldbl; 2921 if (modtab[tn->tn_op].m_binary) 2922 r = tn->tn_right->tn_val->v_ldbl; 2923 2924 switch (tn->tn_op) { 2925 case UPLUS: 2926 v->v_ldbl = l; 2927 break; 2928 case UMINUS: 2929 v->v_ldbl = -l; 2930 break; 2931 case MULT: 2932 v->v_ldbl = l * r; 2933 break; 2934 case DIV: 2935 if (r == 0.0) { 2936 /* division by 0 */ 2937 error(139); 2938 if (t == FLOAT) { 2939 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX; 2940 } else if (t == DOUBLE) { 2941 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX; 2942 } else { 2943 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX; 2944 } 2945 } else { 2946 v->v_ldbl = l / r; 2947 } 2948 break; 2949 case PLUS: 2950 v->v_ldbl = l + r; 2951 break; 2952 case MINUS: 2953 v->v_ldbl = l - r; 2954 break; 2955 case LT: 2956 v->v_quad = l < r; 2957 break; 2958 case LE: 2959 v->v_quad = l <= r; 2960 break; 2961 case GE: 2962 v->v_quad = l >= r; 2963 break; 2964 case GT: 2965 v->v_quad = l > r; 2966 break; 2967 case EQ: 2968 v->v_quad = l == r; 2969 break; 2970 case NE: 2971 v->v_quad = l != r; 2972 break; 2973 default: 2974 LERROR("foldflt()"); 2975 } 2976 2977 if (!fpe && isnan((double)v->v_ldbl)) 2978 LERROR("foldflt()"); 2979 if (fpe || !finite((double)v->v_ldbl) || 2980 (t == FLOAT && 2981 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) || 2982 (t == DOUBLE && 2983 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) { 2984 /* floating point overflow detected, op %s */ 2985 warning(142, modtab[tn->tn_op].m_name); 2986 if (t == FLOAT) { 2987 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX; 2988 } else if (t == DOUBLE) { 2989 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX; 2990 } else { 2991 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX; 2992 } 2993 fpe = 0; 2994 } 2995 2996 return (getcnode(tn->tn_type, v)); 2997 } 2998 2999 3000 /* 3001 * Create a constant node for sizeof. 3002 */ 3003 tnode_t * 3004 bldszof(type_t *tp) 3005 { 3006 tspec_t st; 3007 #if SIZEOF_IS_ULONG 3008 st = ULONG; 3009 #else 3010 st = UINT; 3011 #endif 3012 return getinode(st, tsize(tp) / CHAR_BIT); 3013 } 3014 3015 int64_t 3016 tsize(type_t *tp) 3017 { 3018 int elem, elsz, flex; 3019 3020 elem = 1; 3021 flex = 0; 3022 while (tp->t_tspec == ARRAY) { 3023 flex = 1; /* allow c99 flex arrays [] [0] */ 3024 elem *= tp->t_dim; 3025 tp = tp->t_subt; 3026 } 3027 if (elem == 0) { 3028 if (!flex) { 3029 /* cannot take size of incomplete type */ 3030 error(143); 3031 elem = 1; 3032 } 3033 } 3034 switch (tp->t_tspec) { 3035 case FUNC: 3036 /* cannot take size of function */ 3037 error(144); 3038 elsz = 1; 3039 break; 3040 case STRUCT: 3041 case UNION: 3042 if (incompl(tp)) { 3043 /* cannot take size of incomplete type */ 3044 error(143); 3045 elsz = 1; 3046 } else { 3047 elsz = tp->t_str->size; 3048 } 3049 break; 3050 case ENUM: 3051 if (incompl(tp)) { 3052 /* cannot take size of incomplete type */ 3053 warning(143); 3054 } 3055 /* FALLTHROUGH */ 3056 default: 3057 if (tp->t_isfield) { 3058 /* cannot take size of bit-field */ 3059 error(145); 3060 } 3061 if (tp->t_tspec == VOID) { 3062 /* cannot take size of void */ 3063 error(146); 3064 elsz = 1; 3065 } else { 3066 elsz = size(tp->t_tspec); 3067 if (elsz <= 0) 3068 LERROR("bldszof()"); 3069 } 3070 break; 3071 } 3072 3073 return (int64_t)(elem * elsz); 3074 } 3075 3076 /* 3077 */ 3078 tnode_t * 3079 bldalof(type_t *tp) 3080 { 3081 tspec_t st; 3082 3083 switch (tp->t_tspec) { 3084 case ARRAY: 3085 break; 3086 3087 case FUNC: 3088 /* cannot take align of function */ 3089 error(144); 3090 return 0; 3091 3092 case STRUCT: 3093 case UNION: 3094 if (incompl(tp)) { 3095 /* cannot take align of incomplete type */ 3096 error(143); 3097 return 0; 3098 } 3099 break; 3100 case ENUM: 3101 break; 3102 default: 3103 if (tp->t_isfield) { 3104 /* cannot take align of bit-field */ 3105 error(145); 3106 return 0; 3107 } 3108 if (tp->t_tspec == VOID) { 3109 /* cannot take alignsize of void */ 3110 error(146); 3111 return 0; 3112 } 3113 break; 3114 } 3115 3116 #if SIZEOF_IS_ULONG 3117 st = ULONG; 3118 #else 3119 st = UINT; 3120 #endif 3121 3122 return getinode(st, (int64_t)getbound(tp)); 3123 } 3124 3125 /* 3126 * Type casts. 3127 */ 3128 tnode_t * 3129 cast(tnode_t *tn, type_t *tp) 3130 { 3131 tspec_t nt, ot; 3132 3133 if (tn == NULL) 3134 return (NULL); 3135 3136 tn = cconv(tn); 3137 3138 nt = tp->t_tspec; 3139 ot = tn->tn_type->t_tspec; 3140 3141 if (nt == VOID) { 3142 /* 3143 * XXX ANSI C requires scalar types or void (Plauger&Brodie). 3144 * But this seams really questionable. 3145 */ 3146 } else if (nt == STRUCT || nt == UNION || nt == ARRAY || nt == FUNC) { 3147 /* invalid cast expression */ 3148 error(147); 3149 return (NULL); 3150 } else if (ot == STRUCT || ot == UNION) { 3151 /* invalid cast expression */ 3152 error(147); 3153 return (NULL); 3154 } else if (ot == VOID) { 3155 /* improper cast of void expression */ 3156 error(148); 3157 return (NULL); 3158 } else if (isityp(nt) && issclt(ot)) { 3159 /* ok */ 3160 } else if (isftyp(nt) && isatyp(ot)) { 3161 /* ok */ 3162 } else if (nt == PTR && isityp(ot)) { 3163 /* ok */ 3164 } else if (nt == PTR && ot == PTR) { 3165 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) { 3166 if (hflag) 3167 /* cast discards 'const' from ... */ 3168 warning(275); 3169 } 3170 } else { 3171 /* invalid cast expression */ 3172 error(147); 3173 return (NULL); 3174 } 3175 3176 tn = convert(CVT, 0, tp, tn); 3177 tn->tn_cast = 1; 3178 3179 return (tn); 3180 } 3181 3182 /* 3183 * Create the node for a function argument. 3184 * All necessary conversions and type checks are done in funccall(), because 3185 * in funcarg() we have no information about expected argument types. 3186 */ 3187 tnode_t * 3188 funcarg(tnode_t *args, tnode_t *arg) 3189 { 3190 tnode_t *ntn; 3191 3192 /* 3193 * If there was a serious error in the expression for the argument, 3194 * create a dummy argument so the positions of the remaining arguments 3195 * will not change. 3196 */ 3197 if (arg == NULL) 3198 arg = getinode(INT, (int64_t)0); 3199 3200 ntn = mktnode(PUSH, arg->tn_type, arg, args); 3201 3202 return (ntn); 3203 } 3204 3205 /* 3206 * Create the node for a function call. Also check types of 3207 * function arguments and insert conversions, if necessary. 3208 */ 3209 tnode_t * 3210 funccall(tnode_t *func, tnode_t *args) 3211 { 3212 tnode_t *ntn; 3213 op_t fcop; 3214 3215 if (func == NULL) 3216 return (NULL); 3217 3218 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) { 3219 fcop = CALL; 3220 } else { 3221 fcop = ICALL; 3222 } 3223 3224 /* 3225 * after cconv() func will always be a pointer to a function 3226 * if it is a valid function designator. 3227 */ 3228 func = cconv(func); 3229 3230 if (func->tn_type->t_tspec != PTR || 3231 func->tn_type->t_subt->t_tspec != FUNC) { 3232 char buf[256]; 3233 /* illegal function */ 3234 error(149, tyname(buf, sizeof(buf), func->tn_type)); 3235 return (NULL); 3236 } 3237 3238 args = chkfarg(func->tn_type->t_subt, args); 3239 3240 ntn = mktnode(fcop, func->tn_type->t_subt->t_subt, func, args); 3241 3242 return (ntn); 3243 } 3244 3245 /* 3246 * Check types of all function arguments and insert conversions, 3247 * if necessary. 3248 */ 3249 static tnode_t * 3250 chkfarg(type_t *ftp, tnode_t *args) 3251 { 3252 tnode_t *arg; 3253 sym_t *asym; 3254 tspec_t at; 3255 int narg, npar, n, i; 3256 3257 /* get # of args in the prototype */ 3258 npar = 0; 3259 for (asym = ftp->t_args; asym != NULL; asym = asym->s_nxt) 3260 npar++; 3261 3262 /* get # of args in function call */ 3263 narg = 0; 3264 for (arg = args; arg != NULL; arg = arg->tn_right) 3265 narg++; 3266 3267 asym = ftp->t_args; 3268 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) { 3269 /* argument mismatch: %d arg%s passed, %d expected */ 3270 error(150, narg, narg > 1 ? "s" : "", npar); 3271 asym = NULL; 3272 } 3273 3274 for (n = 1; n <= narg; n++) { 3275 3276 /* 3277 * The rightmost argument is at the top of the argument 3278 * subtree. 3279 */ 3280 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) 3281 continue; 3282 3283 /* some things which are always not allowd */ 3284 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) { 3285 /* void expressions may not be arguments, arg #%d */ 3286 error(151, n); 3287 return (NULL); 3288 } else if ((at == STRUCT || at == UNION) && 3289 incompl(arg->tn_left->tn_type)) { 3290 /* argument cannot have unknown size, arg #%d */ 3291 error(152, n); 3292 return (NULL); 3293 } else if (isityp(at) && arg->tn_left->tn_type->t_isenum && 3294 incompl(arg->tn_left->tn_type)) { 3295 /* argument cannot have unknown size, arg #%d */ 3296 warning(152, n); 3297 } 3298 3299 /* class conversions (arg in value context) */ 3300 arg->tn_left = cconv(arg->tn_left); 3301 3302 if (asym != NULL) { 3303 arg->tn_left = parg(n, asym->s_type, arg->tn_left); 3304 } else { 3305 arg->tn_left = promote(NOOP, 1, arg->tn_left); 3306 } 3307 arg->tn_type = arg->tn_left->tn_type; 3308 3309 if (asym != NULL) 3310 asym = asym->s_nxt; 3311 } 3312 3313 return (args); 3314 } 3315 3316 /* 3317 * Compare the type of an argument with the corresponding type of a 3318 * prototype parameter. If it is a valid combination, but both types 3319 * are not the same, insert a conversion to convert the argument into 3320 * the type of the parameter. 3321 */ 3322 static tnode_t * 3323 parg( int n, /* pos of arg */ 3324 type_t *tp, /* expected type (from prototype) */ 3325 tnode_t *tn) /* argument */ 3326 { 3327 tnode_t *ln; 3328 int dowarn; 3329 3330 ln = xcalloc(1, sizeof (tnode_t)); 3331 ln->tn_type = tduptyp(tp); 3332 ln->tn_type->t_const = 0; 3333 ln->tn_lvalue = 1; 3334 if (typeok(FARG, n, ln, tn)) { 3335 if (!eqtype(tp, tn->tn_type, 1, 0, (dowarn = 0, &dowarn)) || dowarn) 3336 tn = convert(FARG, n, tp, tn); 3337 } 3338 free(ln); 3339 return (tn); 3340 } 3341 3342 /* 3343 * Return the value of an integral constant expression. 3344 * If the expression is not constant or its type is not an integer 3345 * type, an error message is printed. 3346 */ 3347 val_t * 3348 constant(tnode_t *tn, int required) 3349 { 3350 val_t *v; 3351 3352 if (tn != NULL) 3353 tn = cconv(tn); 3354 if (tn != NULL) 3355 tn = promote(NOOP, 0, tn); 3356 3357 v = xcalloc(1, sizeof (val_t)); 3358 3359 if (tn == NULL) { 3360 if (nerr == 0) 3361 LERROR("constant()"); 3362 v->v_tspec = INT; 3363 v->v_quad = 1; 3364 return (v); 3365 } 3366 3367 v->v_tspec = tn->tn_type->t_tspec; 3368 3369 if (tn->tn_op == CON) { 3370 if (tn->tn_type->t_tspec != tn->tn_val->v_tspec) 3371 LERROR("constant()"); 3372 if (isityp(tn->tn_val->v_tspec)) { 3373 v->v_ansiu = tn->tn_val->v_ansiu; 3374 v->v_quad = tn->tn_val->v_quad; 3375 return (v); 3376 } 3377 v->v_quad = tn->tn_val->v_ldbl; 3378 } else { 3379 v->v_quad = 1; 3380 } 3381 3382 /* integral constant expression expected */ 3383 if (required) 3384 error(55); 3385 else 3386 c99ism(318); 3387 3388 if (!isityp(v->v_tspec)) 3389 v->v_tspec = INT; 3390 3391 return (v); 3392 } 3393 3394 /* 3395 * Perform some tests on expressions which can't be done in build() and 3396 * functions called by build(). These tests must be done here because 3397 * we need some information about the context in which the operations 3398 * are performed. 3399 * After all tests are performed, expr() frees the memory which is used 3400 * for the expression. 3401 */ 3402 void 3403 expr(tnode_t *tn, int vctx, int tctx, int dofreeblk) 3404 { 3405 3406 if (tn == NULL && nerr == 0) 3407 LERROR("expr()"); 3408 3409 if (tn == NULL) { 3410 tfreeblk(); 3411 return; 3412 } 3413 3414 /* expr() is also called in global initialisations */ 3415 if (dcs->d_ctx != EXTERN) 3416 chkreach(); 3417 3418 chkmisc(tn, vctx, tctx, !tctx, 0, 0, 0); 3419 if (tn->tn_op == ASSIGN) { 3420 if (hflag && tctx) 3421 /* assignment in conditional context */ 3422 warning(159); 3423 } else if (tn->tn_op == CON) { 3424 if (hflag && tctx && !ccflg) 3425 /* constant in conditional context */ 3426 warning(161); 3427 } 3428 if (!modtab[tn->tn_op].m_sideeff) { 3429 /* 3430 * for left operands of COMMA this warning is already 3431 * printed 3432 */ 3433 if (tn->tn_op != COMMA && !vctx && !tctx) 3434 nulleff(tn); 3435 } 3436 if (dflag) 3437 displexpr(tn, 0); 3438 3439 /* free the tree memory */ 3440 if (dofreeblk) 3441 tfreeblk(); 3442 } 3443 3444 static void 3445 nulleff(tnode_t *tn) 3446 { 3447 3448 if (!hflag) 3449 return; 3450 3451 while (!modtab[tn->tn_op].m_sideeff) { 3452 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) { 3453 tn = tn->tn_left; 3454 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) { 3455 /* 3456 * && and || have a side effect if the right operand 3457 * has a side effect. 3458 */ 3459 tn = tn->tn_right; 3460 } else if (tn->tn_op == QUEST) { 3461 /* 3462 * ? has a side effect if at least one of its right 3463 * operands has a side effect 3464 */ 3465 tn = tn->tn_right; 3466 } else if (tn->tn_op == COLON || tn->tn_op == COMMA) { 3467 /* 3468 * : has a side effect if at least one of its operands 3469 * has a side effect 3470 */ 3471 if (modtab[tn->tn_left->tn_op].m_sideeff) { 3472 tn = tn->tn_left; 3473 } else if (modtab[tn->tn_right->tn_op].m_sideeff) { 3474 tn = tn->tn_right; 3475 } else { 3476 break; 3477 } 3478 } else { 3479 break; 3480 } 3481 } 3482 if (!modtab[tn->tn_op].m_sideeff) 3483 /* expression has null effect */ 3484 warning(129); 3485 } 3486 3487 /* 3488 * Dump an expression to stdout 3489 * only used for debugging 3490 */ 3491 static void 3492 displexpr(tnode_t *tn, int offs) 3493 { 3494 uint64_t uq; 3495 3496 if (tn == NULL) { 3497 (void)printf("%*s%s\n", offs, "", "NULL"); 3498 return; 3499 } 3500 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name); 3501 3502 if (tn->tn_op == NAME) { 3503 (void)printf("%s: %s ", 3504 tn->tn_sym->s_name, scltoa(tn->tn_sym->s_scl)); 3505 } else if (tn->tn_op == CON && isftyp(tn->tn_type->t_tspec)) { 3506 (void)printf("%#g ", (double)tn->tn_val->v_ldbl); 3507 } else if (tn->tn_op == CON && isityp(tn->tn_type->t_tspec)) { 3508 uq = tn->tn_val->v_quad; 3509 (void)printf("0x %08lx %08lx ", (long)(uq >> 32) & 0xffffffffl, 3510 (long)uq & 0xffffffffl); 3511 } else if (tn->tn_op == CON) { 3512 if (tn->tn_type->t_tspec != PTR) 3513 LERROR("displexpr()"); 3514 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4), 3515 (u_long)tn->tn_val->v_quad); 3516 } else if (tn->tn_op == STRING) { 3517 if (tn->tn_strg->st_tspec == CHAR) { 3518 (void)printf("\"%s\"", tn->tn_strg->st_cp); 3519 } else { 3520 char *s; 3521 size_t n; 3522 n = MB_CUR_MAX * (tn->tn_strg->st_len + 1); 3523 s = xmalloc(n); 3524 (void)wcstombs(s, tn->tn_strg->st_wcp, n); 3525 (void)printf("L\"%s\"", s); 3526 free(s); 3527 } 3528 (void)printf(" "); 3529 } else if (tn->tn_op == FSEL) { 3530 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs, 3531 tn->tn_type->t_flen); 3532 } 3533 (void)printf("%s\n", ttos(tn->tn_type)); 3534 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING) 3535 return; 3536 displexpr(tn->tn_left, offs + 2); 3537 if (modtab[tn->tn_op].m_binary || 3538 (tn->tn_op == PUSH && tn->tn_right != NULL)) { 3539 displexpr(tn->tn_right, offs + 2); 3540 } 3541 } 3542 3543 /* 3544 * Called by expr() to recursively perform some tests. 3545 */ 3546 /* ARGSUSED */ 3547 void 3548 chkmisc(tnode_t *tn, int vctx, int tctx, int eqwarn, int fcall, int rvdisc, 3549 int szof) 3550 { 3551 tnode_t *ln, *rn; 3552 mod_t *mp; 3553 int nrvdisc, cvctx, ctctx; 3554 op_t op; 3555 scl_t sc; 3556 dinfo_t *di; 3557 3558 if (tn == NULL) 3559 return; 3560 3561 ln = tn->tn_left; 3562 rn = tn->tn_right; 3563 mp = &modtab[op = tn->tn_op]; 3564 3565 switch (op) { 3566 case AMPER: 3567 if (ln->tn_op == NAME && (reached || rchflg)) { 3568 if (!szof) 3569 setsflg(ln->tn_sym); 3570 setuflg(ln->tn_sym, fcall, szof); 3571 } 3572 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3573 /* check the range of array indices */ 3574 chkaidx(ln->tn_left, 1); 3575 break; 3576 case LOAD: 3577 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3578 /* check the range of array indices */ 3579 chkaidx(ln->tn_left, 0); 3580 /* FALLTHROUGH */ 3581 case PUSH: 3582 case INCBEF: 3583 case DECBEF: 3584 case INCAFT: 3585 case DECAFT: 3586 case ADDASS: 3587 case SUBASS: 3588 case MULASS: 3589 case DIVASS: 3590 case MODASS: 3591 case ANDASS: 3592 case ORASS: 3593 case XORASS: 3594 case SHLASS: 3595 case SHRASS: 3596 case REAL: 3597 case IMAG: 3598 if (ln->tn_op == NAME && (reached || rchflg)) { 3599 sc = ln->tn_sym->s_scl; 3600 /* 3601 * Look if there was a asm statement in one of the 3602 * compound statements we are in. If not, we don't 3603 * print a warning. 3604 */ 3605 for (di = dcs; di != NULL; di = di->d_nxt) { 3606 if (di->d_asm) 3607 break; 3608 } 3609 if (sc != EXTERN && sc != STATIC && 3610 !ln->tn_sym->s_set && !szof && di == NULL) { 3611 /* %s may be used before set */ 3612 warning(158, ln->tn_sym->s_name); 3613 setsflg(ln->tn_sym); 3614 } 3615 setuflg(ln->tn_sym, 0, 0); 3616 } 3617 break; 3618 case ASSIGN: 3619 if (ln->tn_op == NAME && !szof && (reached || rchflg)) { 3620 setsflg(ln->tn_sym); 3621 if (ln->tn_sym->s_scl == EXTERN) 3622 outusg(ln->tn_sym); 3623 } 3624 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3625 /* check the range of array indices */ 3626 chkaidx(ln->tn_left, 0); 3627 break; 3628 case CALL: 3629 if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME) 3630 LERROR("chkmisc(op=%s != %s || %s != %s)", 3631 getopname(ln->tn_op), getopname(AMPER), 3632 getopname(ln->tn_left->tn_op), getopname(NAME)); 3633 if (!szof) 3634 outcall(tn, vctx || tctx, rvdisc); 3635 break; 3636 case EQ: 3637 /* equality operator "==" found where "=" was exp. */ 3638 if (hflag && eqwarn) 3639 warning(160); 3640 break; 3641 case CON: 3642 case NAME: 3643 case STRING: 3644 return; 3645 /* LINTED206: (enumeration values not handled in switch) */ 3646 case OR: 3647 case XOR: 3648 case NE: 3649 case GE: 3650 case GT: 3651 case LE: 3652 case LT: 3653 case SHR: 3654 case SHL: 3655 case MINUS: 3656 case PLUS: 3657 case MOD: 3658 case DIV: 3659 case MULT: 3660 case STAR: 3661 case UMINUS: 3662 case UPLUS: 3663 case DEC: 3664 case INC: 3665 case COMPL: 3666 case NOT: 3667 case POINT: 3668 case ARROW: 3669 case NOOP: 3670 case AND: 3671 case FARG: 3672 case CASE: 3673 case INIT: 3674 case RETURN: 3675 case ICALL: 3676 case CVT: 3677 case COMMA: 3678 case FSEL: 3679 case COLON: 3680 case QUEST: 3681 case LOGOR: 3682 case LOGAND: 3683 break; 3684 } 3685 3686 cvctx = mp->m_vctx; 3687 ctctx = mp->m_tctx; 3688 /* 3689 * values of operands of ':' are not used if the type of at least 3690 * one of the operands (for gcc compatibility) is void 3691 * XXX test/value context of QUEST should probably be used as 3692 * context for both operands of COLON 3693 */ 3694 if (op == COLON && tn->tn_type->t_tspec == VOID) 3695 cvctx = ctctx = 0; 3696 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID; 3697 chkmisc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof); 3698 3699 switch (op) { 3700 case PUSH: 3701 if (rn != NULL) 3702 chkmisc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof); 3703 break; 3704 case LOGAND: 3705 case LOGOR: 3706 chkmisc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof); 3707 break; 3708 case COLON: 3709 chkmisc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof); 3710 break; 3711 case COMMA: 3712 chkmisc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof); 3713 break; 3714 default: 3715 if (mp->m_binary) 3716 chkmisc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof); 3717 break; 3718 } 3719 3720 } 3721 3722 /* 3723 * Checks the range of array indices, if possible. 3724 * amper is set if only the address of the element is used. This 3725 * means that the index is allowd to refere to the first element 3726 * after the array. 3727 */ 3728 static void 3729 chkaidx(tnode_t *tn, int amper) 3730 { 3731 int dim; 3732 tnode_t *ln, *rn; 3733 int elsz; 3734 int64_t con; 3735 3736 ln = tn->tn_left; 3737 rn = tn->tn_right; 3738 3739 /* We can only check constant indices. */ 3740 if (rn->tn_op != CON) 3741 return; 3742 3743 /* Return if the left node does not stem from an array. */ 3744 if (ln->tn_op != AMPER) 3745 return; 3746 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME) 3747 return; 3748 if (ln->tn_left->tn_type->t_tspec != ARRAY) 3749 return; 3750 3751 /* 3752 * For incomplete array types, we can print a warning only if 3753 * the index is negative. 3754 */ 3755 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0) 3756 return; 3757 3758 /* Get the size of one array element */ 3759 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0) 3760 return; 3761 elsz /= CHAR_BIT; 3762 3763 /* Change the unit of the index from bytes to element size. */ 3764 if (isutyp(rn->tn_type->t_tspec)) { 3765 con = (uint64_t)rn->tn_val->v_quad / elsz; 3766 } else { 3767 con = rn->tn_val->v_quad / elsz; 3768 } 3769 3770 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0); 3771 3772 if (!isutyp(rn->tn_type->t_tspec) && con < 0) { 3773 /* array subscript cannot be negative: %ld */ 3774 warning(167, (long)con); 3775 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) { 3776 /* array subscript cannot be > %d: %ld */ 3777 warning(168, dim - 1, (long)con); 3778 } 3779 } 3780 3781 /* 3782 * Check for ordered comparisons of unsigned values with 0. 3783 */ 3784 static void 3785 chkcomp(op_t op, tnode_t *ln, tnode_t *rn) 3786 { 3787 char buf[64]; 3788 tspec_t lt, rt; 3789 mod_t *mp; 3790 3791 lt = ln->tn_type->t_tspec; 3792 rt = rn->tn_type->t_tspec; 3793 mp = &modtab[op]; 3794 3795 if (ln->tn_op != CON && rn->tn_op != CON) 3796 return; 3797 3798 if (!isityp(lt) || !isityp(rt)) 3799 return; 3800 3801 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON && 3802 (rn->tn_val->v_quad < 0 || 3803 rn->tn_val->v_quad > (int)~(~0U << (CHAR_BIT - 1)))) { 3804 /* nonportable character comparison, op %s */ 3805 warning(230, mp->m_name); 3806 return; 3807 } 3808 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON && 3809 (ln->tn_val->v_quad < 0 || 3810 ln->tn_val->v_quad > (int)~(~0U << (CHAR_BIT - 1)))) { 3811 /* nonportable character comparison, op %s */ 3812 warning(230, mp->m_name); 3813 return; 3814 } 3815 if (isutyp(lt) && !isutyp(rt) && 3816 rn->tn_op == CON && rn->tn_val->v_quad <= 0) { 3817 if (rn->tn_val->v_quad < 0) { 3818 /* comparison of %s with %s, op %s */ 3819 warning(162, tyname(buf, sizeof(buf), ln->tn_type), 3820 "negative constant", mp->m_name); 3821 } else if (op == LT || op == GE || (hflag && op == LE)) { 3822 /* comparison of %s with %s, op %s */ 3823 warning(162, tyname(buf, sizeof(buf), ln->tn_type), 3824 "0", mp->m_name); 3825 } 3826 return; 3827 } 3828 if (isutyp(rt) && !isutyp(lt) && 3829 ln->tn_op == CON && ln->tn_val->v_quad <= 0) { 3830 if (ln->tn_val->v_quad < 0) { 3831 /* comparison of %s with %s, op %s */ 3832 warning(162, "negative constant", 3833 tyname(buf, sizeof(buf), rn->tn_type), mp->m_name); 3834 } else if (op == GT || op == LE || (hflag && op == GE)) { 3835 /* comparison of %s with %s, op %s */ 3836 warning(162, "0", tyname(buf, sizeof(buf), rn->tn_type), 3837 mp->m_name); 3838 } 3839 return; 3840 } 3841 } 3842 3843 /* 3844 * Takes an expression an returns 0 if this expression can be used 3845 * for static initialisation, otherwise -1. 3846 * 3847 * Constant initialisation expressions must be constant or an address 3848 * of a static object with an optional offset. In the first case, 3849 * the result is returned in *offsp. In the second case, the static 3850 * object is returned in *symp and the offset in *offsp. 3851 * 3852 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and 3853 * CON. Type conversions are allowed if they do not change binary 3854 * representation (including width). 3855 */ 3856 int 3857 conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp) 3858 { 3859 sym_t *sym; 3860 ptrdiff_t offs1, offs2; 3861 tspec_t t, ot; 3862 3863 switch (tn->tn_op) { 3864 case MINUS: 3865 if (tn->tn_right->tn_op == CVT) 3866 return conaddr(tn->tn_right, symp, offsp); 3867 else if (tn->tn_right->tn_op != CON) 3868 return (-1); 3869 /* FALLTHROUGH */ 3870 case PLUS: 3871 offs1 = offs2 = 0; 3872 if (tn->tn_left->tn_op == CON) { 3873 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad; 3874 if (conaddr(tn->tn_right, &sym, &offs2) == -1) 3875 return (-1); 3876 } else if (tn->tn_right->tn_op == CON) { 3877 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad; 3878 if (tn->tn_op == MINUS) 3879 offs2 = -offs2; 3880 if (conaddr(tn->tn_left, &sym, &offs1) == -1) 3881 return (-1); 3882 } else { 3883 return (-1); 3884 } 3885 *symp = sym; 3886 *offsp = offs1 + offs2; 3887 break; 3888 case AMPER: 3889 if (tn->tn_left->tn_op == NAME) { 3890 *symp = tn->tn_left->tn_sym; 3891 *offsp = 0; 3892 } else if (tn->tn_left->tn_op == STRING) { 3893 /* 3894 * If this would be the front end of a compiler we 3895 * would return a label instead of 0. 3896 */ 3897 *offsp = 0; 3898 } 3899 break; 3900 case CVT: 3901 t = tn->tn_type->t_tspec; 3902 ot = tn->tn_left->tn_type->t_tspec; 3903 if ((!isityp(t) && t != PTR) || (!isityp(ot) && ot != PTR)) 3904 return (-1); 3905 #ifdef notdef 3906 /* 3907 * consider: 3908 * struct foo { 3909 * unsigned char a; 3910 * } f = { 3911 * (u_char)(u_long)(&(((struct foo *)0)->a)) 3912 * }; 3913 * since psize(u_long) != psize(u_char) this fails. 3914 */ 3915 else if (psize(t) != psize(ot)) 3916 return (-1); 3917 #endif 3918 if (conaddr(tn->tn_left, symp, offsp) == -1) 3919 return (-1); 3920 break; 3921 default: 3922 return (-1); 3923 } 3924 return (0); 3925 } 3926 3927 /* 3928 * Concatenate two string constants. 3929 */ 3930 strg_t * 3931 catstrg(strg_t *strg1, strg_t *strg2) 3932 { 3933 size_t len1, len2, len; 3934 3935 if (strg1->st_tspec != strg2->st_tspec) { 3936 /* cannot concatenate wide and regular string literals */ 3937 error(292); 3938 return (strg1); 3939 } 3940 3941 len1 = strg1->st_len; 3942 len2 = strg2->st_len + 1; /* + NUL */ 3943 len = len1 + len2; 3944 3945 #define COPY(F) \ 3946 do { \ 3947 strg1->F = xrealloc(strg1->F, len * sizeof(*strg1->F)); \ 3948 (void)memcpy(strg1->F + len1, strg2->F, len2 * sizeof(*strg1->F)); \ 3949 free(strg2->F); \ 3950 } while (/*CONSTCOND*/0) 3951 3952 if (strg1->st_tspec == CHAR) 3953 COPY(st_cp); 3954 else 3955 COPY(st_wcp); 3956 3957 strg1->st_len = len - 1; /* - NUL */; 3958 free(strg2); 3959 3960 return (strg1); 3961 } 3962 3963 /* 3964 * Print a warning if the given node has operands which should be 3965 * parenthesized. 3966 * 3967 * XXX Does not work if an operand is a constant expression. Constant 3968 * expressions are already folded. 3969 */ 3970 static void 3971 precconf(tnode_t *tn) 3972 { 3973 tnode_t *ln, *rn; 3974 op_t lop, rop = NOOP; 3975 int lparn, rparn = 0; 3976 mod_t *mp; 3977 int dowarn; 3978 3979 if (!hflag) 3980 return; 3981 3982 mp = &modtab[tn->tn_op]; 3983 3984 lparn = 0; 3985 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left) 3986 lparn |= ln->tn_parn; 3987 lparn |= ln->tn_parn; 3988 lop = ln->tn_op; 3989 3990 if (mp->m_binary) { 3991 rparn = 0; 3992 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left) 3993 rparn |= rn->tn_parn; 3994 rparn |= rn->tn_parn; 3995 rop = rn->tn_op; 3996 } 3997 3998 dowarn = 0; 3999 4000 switch (tn->tn_op) { 4001 case SHL: 4002 case SHR: 4003 if (!lparn && (lop == PLUS || lop == MINUS)) { 4004 dowarn = 1; 4005 } else if (!rparn && (rop == PLUS || rop == MINUS)) { 4006 dowarn = 1; 4007 } 4008 break; 4009 case LOGOR: 4010 if (!lparn && lop == LOGAND) { 4011 dowarn = 1; 4012 } else if (!rparn && rop == LOGAND) { 4013 dowarn = 1; 4014 } 4015 break; 4016 case AND: 4017 case XOR: 4018 case OR: 4019 if (!lparn && lop != tn->tn_op) { 4020 if (lop == PLUS || lop == MINUS) { 4021 dowarn = 1; 4022 } else if (lop == AND || lop == XOR) { 4023 dowarn = 1; 4024 } 4025 } 4026 if (!dowarn && !rparn && rop != tn->tn_op) { 4027 if (rop == PLUS || rop == MINUS) { 4028 dowarn = 1; 4029 } else if (rop == AND || rop == XOR) { 4030 dowarn = 1; 4031 } 4032 } 4033 break; 4034 /* LINTED206: (enumeration values not handled in switch) */ 4035 case DECAFT: 4036 case XORASS: 4037 case SHLASS: 4038 case NOOP: 4039 case ARROW: 4040 case ORASS: 4041 case POINT: 4042 case NAME: 4043 case NOT: 4044 case COMPL: 4045 case CON: 4046 case INC: 4047 case STRING: 4048 case DEC: 4049 case INCBEF: 4050 case DECBEF: 4051 case INCAFT: 4052 case FSEL: 4053 case CALL: 4054 case COMMA: 4055 case CVT: 4056 case ICALL: 4057 case LOAD: 4058 case PUSH: 4059 case RETURN: 4060 case INIT: 4061 case CASE: 4062 case FARG: 4063 case SUBASS: 4064 case ADDASS: 4065 case MODASS: 4066 case DIVASS: 4067 case MULASS: 4068 case ASSIGN: 4069 case COLON: 4070 case QUEST: 4071 case LOGAND: 4072 case NE: 4073 case EQ: 4074 case GE: 4075 case GT: 4076 case LE: 4077 case LT: 4078 case MINUS: 4079 case PLUS: 4080 case MOD: 4081 case DIV: 4082 case MULT: 4083 case AMPER: 4084 case STAR: 4085 case UMINUS: 4086 case SHRASS: 4087 case UPLUS: 4088 case ANDASS: 4089 case REAL: 4090 case IMAG: 4091 break; 4092 } 4093 4094 if (dowarn) { 4095 /* precedence confusion possible: parenthesize! */ 4096 warning(169); 4097 } 4098 4099 } 4100