1 /* $NetBSD: tree.c,v 1.81 2015/08/28 09:42:07 joerg 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.81 2015/08/28 09:42:07 joerg 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 switch (op) { 1247 case INIT: 1248 case RETURN: 1249 /* illegal combination of pointer and integer */ 1250 warning(183); 1251 break; 1252 case FARG: 1253 /* illegal comb. of ptr. and int., arg #%d */ 1254 warning(154, arg); 1255 break; 1256 default: 1257 /* illegal comb. of ptr. and int., op %s */ 1258 warning(123, mp->m_name); 1259 break; 1260 } 1261 return (1); 1262 } 1263 1264 if (lt == PTR && rt == PTR) { 1265 switch (op) { 1266 case INIT: 1267 case RETURN: 1268 illptrc(NULL, ltp, rtp); 1269 break; 1270 case FARG: 1271 /* argument has incompatible pointer type, arg #%d */ 1272 warning(153, arg, tyname(lbuf, sizeof(lbuf), ltp), 1273 tyname(rbuf, sizeof(rbuf), rtp)); 1274 break; 1275 default: 1276 illptrc(mp, ltp, rtp); 1277 break; 1278 } 1279 return (1); 1280 } 1281 1282 switch (op) { 1283 case INIT: 1284 /* initialisation type mismatch */ 1285 error(185); 1286 break; 1287 case RETURN: 1288 /* return value type mismatch */ 1289 error(211); 1290 break; 1291 case FARG: 1292 /* argument is incompatible with prototype, arg #%d */ 1293 warning(155, arg); 1294 break; 1295 default: 1296 incompat(op, lt, rt); 1297 break; 1298 } 1299 1300 return (0); 1301 } 1302 1303 /* 1304 * Prints a warning if an operator, which should be senseless for an 1305 * enum type, is applied to an enum type. 1306 */ 1307 static void 1308 chkbeop(op_t op, tnode_t *ln, tnode_t *rn) 1309 { 1310 mod_t *mp; 1311 1312 if (!eflag) 1313 return; 1314 1315 mp = &modtab[op]; 1316 1317 if (!(ln->tn_type->t_isenum || 1318 (mp->m_binary && rn->tn_type->t_isenum))) { 1319 return; 1320 } 1321 1322 /* 1323 * Enum as offset to a pointer is an exception (otherwise enums 1324 * could not be used as array indizes). 1325 */ 1326 if (op == PLUS && 1327 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) || 1328 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) { 1329 return; 1330 } 1331 1332 /* dubious operation on enum, op %s */ 1333 warning(241, mp->m_name); 1334 1335 } 1336 1337 /* 1338 * Prints a warning if an operator is applied to two different enum types. 1339 */ 1340 static void 1341 chkeop2(op_t op, int arg, tnode_t *ln, tnode_t *rn) 1342 { 1343 mod_t *mp; 1344 1345 mp = &modtab[op]; 1346 1347 if (ln->tn_type->t_enum != rn->tn_type->t_enum) { 1348 switch (op) { 1349 case INIT: 1350 /* enum type mismatch in initialisation */ 1351 warning(210); 1352 break; 1353 case FARG: 1354 /* enum type mismatch, arg #%d */ 1355 warning(156, arg); 1356 break; 1357 case RETURN: 1358 /* return value type mismatch */ 1359 warning(211); 1360 break; 1361 default: 1362 /* enum type mismatch, op %s */ 1363 warning(130, mp->m_name); 1364 break; 1365 } 1366 } else if (Pflag && mp->m_comp && op != EQ && op != NE) { 1367 if (eflag) 1368 /* dubious comparisons of enums */ 1369 warning(243, mp->m_name); 1370 } 1371 } 1372 1373 /* 1374 * Prints a warning if an operator has both enum end other integer 1375 * types. 1376 */ 1377 static void 1378 chkeop1(op_t op, int arg, tnode_t *ln, tnode_t *rn) 1379 { 1380 char lbuf[64], rbuf[64]; 1381 1382 if (!eflag) 1383 return; 1384 1385 switch (op) { 1386 case INIT: 1387 /* 1388 * Initializations with 0 should be allowed. Otherwise, 1389 * we should complain about all uninitialized enums, 1390 * consequently. 1391 */ 1392 if (!rn->tn_type->t_isenum && rn->tn_op == CON && 1393 isityp(rn->tn_type->t_tspec) && rn->tn_val->v_quad == 0) { 1394 return; 1395 } 1396 /* initialisation of '%s' with '%s' */ 1397 warning(277, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1398 tyname(rbuf, sizeof(rbuf), rn->tn_type)); 1399 break; 1400 case FARG: 1401 /* combination of '%s' and '%s', arg #%d */ 1402 warning(278, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1403 tyname(rbuf, sizeof(rbuf), rn->tn_type), arg); 1404 break; 1405 case RETURN: 1406 /* combination of '%s' and '%s' in return */ 1407 warning(279, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1408 tyname(rbuf, sizeof(rbuf), rn->tn_type)); 1409 break; 1410 default: 1411 /* combination of '%s' and %s, op %s */ 1412 warning(242, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1413 tyname(rbuf, sizeof(rbuf), rn->tn_type), 1414 modtab[op].m_name); 1415 break; 1416 } 1417 } 1418 1419 /* 1420 * Build and initialize a new node. 1421 */ 1422 static tnode_t * 1423 mktnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn) 1424 { 1425 tnode_t *ntn; 1426 tspec_t t; 1427 #ifdef notyet 1428 size_t l; 1429 uint64_t rnum; 1430 #endif 1431 1432 ntn = getnode(); 1433 1434 ntn->tn_op = op; 1435 ntn->tn_type = type; 1436 ntn->tn_left = ln; 1437 ntn->tn_right = rn; 1438 1439 switch (op) { 1440 #ifdef notyet 1441 case SHR: 1442 if (rn->tn_op != CON) 1443 break; 1444 rnum = rn->tn_val->v_quad; 1445 l = tsize(ln->tn_type) / CHAR_BIT; 1446 t = ln->tn_type->t_tspec; 1447 switch (l) { 1448 case 8: 1449 if (rnum >= 56) 1450 t = UCHAR; 1451 else if (rnum >= 48) 1452 t = USHORT; 1453 else if (rnum >= 32) 1454 t = UINT; 1455 break; 1456 case 4: 1457 if (rnum >= 24) 1458 t = UCHAR; 1459 else if (rnum >= 16) 1460 t = USHORT; 1461 break; 1462 case 2: 1463 if (rnum >= 8) 1464 t = UCHAR; 1465 break; 1466 default: 1467 break; 1468 } 1469 if (t != ln->tn_type->t_tspec) 1470 ntn->tn_type->t_tspec = t; 1471 break; 1472 #endif 1473 case STAR: 1474 case FSEL: 1475 if (ln->tn_type->t_tspec == PTR) { 1476 t = ln->tn_type->t_subt->t_tspec; 1477 if (t != FUNC && t != VOID) 1478 ntn->tn_lvalue = 1; 1479 } else { 1480 LERROR("mktnode()"); 1481 } 1482 break; 1483 default: 1484 break; 1485 } 1486 1487 return ntn; 1488 } 1489 1490 /* 1491 * Performs usual conversion of operands to (unsigned) int. 1492 * 1493 * If tflag is set or the operand is a function argument with no 1494 * type information (no prototype or variable # of args), convert 1495 * float to double. 1496 */ 1497 tnode_t * 1498 promote(op_t op, int farg, tnode_t *tn) 1499 { 1500 tspec_t t; 1501 type_t *ntp; 1502 u_int len; 1503 1504 t = tn->tn_type->t_tspec; 1505 1506 if (!isatyp(t)) 1507 return (tn); 1508 1509 if (!tflag) { 1510 /* 1511 * ANSI C requires that the result is always of type INT 1512 * if INT can represent all possible values of the previous 1513 * type. 1514 */ 1515 if (tn->tn_type->t_isfield) { 1516 len = tn->tn_type->t_flen; 1517 if (size(INT) > len) { 1518 t = INT; 1519 } else { 1520 if (size(INT) != len) 1521 LERROR("promote()"); 1522 if (isutyp(t)) { 1523 t = UINT; 1524 } else { 1525 t = INT; 1526 } 1527 } 1528 } else if (t == CHAR || t == UCHAR || t == SCHAR) { 1529 t = (size(CHAR) < size(INT) || t != UCHAR) ? 1530 INT : UINT; 1531 } else if (t == SHORT || t == USHORT) { 1532 t = (size(SHORT) < size(INT) || t == SHORT) ? 1533 INT : UINT; 1534 } else if (t == ENUM) { 1535 t = INT; 1536 } else if (farg && t == FLOAT) { 1537 t = DOUBLE; 1538 } 1539 } else { 1540 /* 1541 * In traditional C, keep unsigned and promote FLOAT 1542 * to DOUBLE. 1543 */ 1544 if (t == UCHAR || t == USHORT) { 1545 t = UINT; 1546 } else if (t == CHAR || t == SCHAR || t == SHORT) { 1547 t = INT; 1548 } else if (t == FLOAT) { 1549 t = DOUBLE; 1550 } else if (t == ENUM) { 1551 t = INT; 1552 } 1553 } 1554 1555 if (t != tn->tn_type->t_tspec) { 1556 ntp = tduptyp(tn->tn_type); 1557 ntp->t_tspec = t; 1558 /* 1559 * Keep t_isenum so we are later able to check compatibility 1560 * of enum types. 1561 */ 1562 tn = convert(op, 0, ntp, tn); 1563 } 1564 1565 return (tn); 1566 } 1567 1568 /* 1569 * Insert conversions which are necessary to give both operands the same 1570 * type. This is done in different ways for traditional C and ANIS C. 1571 */ 1572 static void 1573 balance(op_t op, tnode_t **lnp, tnode_t **rnp) 1574 { 1575 tspec_t lt, rt, t; 1576 int i, u; 1577 type_t *ntp; 1578 static tspec_t tl[] = { 1579 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT, 1580 }; 1581 1582 lt = (*lnp)->tn_type->t_tspec; 1583 rt = (*rnp)->tn_type->t_tspec; 1584 1585 if (!isatyp(lt) || !isatyp(rt)) 1586 return; 1587 1588 if (!tflag) { 1589 if (lt == rt) { 1590 t = lt; 1591 } else if (lt == LCOMPLEX || rt == LCOMPLEX) { 1592 t = LCOMPLEX; 1593 } else if (lt == DCOMPLEX || rt == DCOMPLEX) { 1594 t = DCOMPLEX; 1595 } else if (lt == COMPLEX || rt == COMPLEX) { 1596 t = COMPLEX; 1597 } else if (lt == FCOMPLEX || rt == FCOMPLEX) { 1598 t = FCOMPLEX; 1599 } else if (lt == LDOUBLE || rt == LDOUBLE) { 1600 t = LDOUBLE; 1601 } else if (lt == DOUBLE || rt == DOUBLE) { 1602 t = DOUBLE; 1603 } else if (lt == FLOAT || rt == FLOAT) { 1604 t = FLOAT; 1605 } else { 1606 /* 1607 * If type A has more bits than type B it should 1608 * be able to hold all possible values of type B. 1609 */ 1610 if (size(lt) > size(rt)) { 1611 t = lt; 1612 } else if (size(lt) < size(rt)) { 1613 t = rt; 1614 } else { 1615 for (i = 3; tl[i] != INT; i++) { 1616 if (tl[i] == lt || tl[i] == rt) 1617 break; 1618 } 1619 if ((isutyp(lt) || isutyp(rt)) && 1620 !isutyp(tl[i])) { 1621 i--; 1622 } 1623 t = tl[i]; 1624 } 1625 } 1626 } else { 1627 /* Keep unsigned in traditional C */ 1628 u = isutyp(lt) || isutyp(rt); 1629 for (i = 0; tl[i] != INT; i++) { 1630 if (lt == tl[i] || rt == tl[i]) 1631 break; 1632 } 1633 t = tl[i]; 1634 if (u && isityp(t) && !isutyp(t)) 1635 t = utyp(t); 1636 } 1637 1638 if (t != lt) { 1639 ntp = tduptyp((*lnp)->tn_type); 1640 ntp->t_tspec = t; 1641 *lnp = convert(op, 0, ntp, *lnp); 1642 } 1643 if (t != rt) { 1644 ntp = tduptyp((*rnp)->tn_type); 1645 ntp->t_tspec = t; 1646 *rnp = convert(op, 0, ntp, *rnp); 1647 } 1648 } 1649 1650 /* 1651 * Insert a conversion operator, which converts the type of the node 1652 * to another given type. 1653 * If op is FARG, arg is the number of the argument (used for warnings). 1654 */ 1655 tnode_t * 1656 convert(op_t op, int arg, type_t *tp, tnode_t *tn) 1657 { 1658 tnode_t *ntn; 1659 tspec_t nt, ot, ost = NOTSPEC; 1660 1661 nt = tp->t_tspec; 1662 if ((ot = tn->tn_type->t_tspec) == PTR) 1663 ost = tn->tn_type->t_subt->t_tspec; 1664 1665 if (!tflag && !sflag && op == FARG) 1666 ptconv(arg, nt, ot, tp, tn); 1667 if (isityp(nt) && isityp(ot)) { 1668 iiconv(op, arg, nt, ot, tp, tn); 1669 } else if (nt == PTR && ((ot == PTR && ost == VOID) || isityp(ot)) && 1670 tn->tn_op == CON && tn->tn_val->v_quad == 0) { 1671 /* 0, 0L and (void *)0 may be assigned to any pointer. */ 1672 } else if (isityp(nt) && ot == PTR) { 1673 piconv(op, nt, tp, tn); 1674 } else if (nt == PTR && ot == PTR) { 1675 ppconv(op, tn, tp); 1676 } 1677 1678 ntn = getnode(); 1679 ntn->tn_op = CVT; 1680 ntn->tn_type = tp; 1681 ntn->tn_cast = op == CVT; 1682 ntn->tn_right = NULL; 1683 if (tn->tn_op != CON || nt == VOID) { 1684 ntn->tn_left = tn; 1685 } else { 1686 ntn->tn_op = CON; 1687 ntn->tn_val = tgetblk(sizeof (val_t)); 1688 cvtcon(op, arg, ntn->tn_type, ntn->tn_val, tn->tn_val); 1689 } 1690 1691 return (ntn); 1692 } 1693 1694 /* 1695 * Print a warning if a prototype causes a type conversion that is 1696 * different from what would happen to the same argument in the 1697 * absence of a prototype. 1698 * 1699 * Errors/Warnings about illegal type combinations are already printed 1700 * in asgntypok(). 1701 */ 1702 static void 1703 ptconv(int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn) 1704 { 1705 tnode_t *ptn; 1706 char buf[64]; 1707 1708 if (!isatyp(nt) || !isatyp(ot)) 1709 return; 1710 1711 /* 1712 * If the type of the formal parameter is char/short, a warning 1713 * would be useless, because functions declared the old style 1714 * can't expect char/short arguments. 1715 */ 1716 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT) 1717 return; 1718 1719 /* get default promotion */ 1720 ptn = promote(NOOP, 1, tn); 1721 ot = ptn->tn_type->t_tspec; 1722 1723 /* return if types are the same with and without prototype */ 1724 if (nt == ot || (nt == ENUM && ot == INT)) 1725 return; 1726 1727 if (isftyp(nt) != isftyp(ot) || psize(nt) != psize(ot)) { 1728 /* representation and/or width change */ 1729 if (!isityp(ot) || psize(ot) > psize(INT)) { 1730 /* conversion to '%s' due to prototype, arg #%d */ 1731 warning(259, tyname(buf, sizeof(buf), tp), arg); 1732 } 1733 } else if (hflag) { 1734 /* 1735 * they differ in sign or base type (char, short, int, 1736 * long, long long, float, double, long double) 1737 * 1738 * if they differ only in sign and the argument is a constant 1739 * and the msb of the argument is not set, print no warning 1740 */ 1741 if (ptn->tn_op == CON && isityp(nt) && styp(nt) == styp(ot) && 1742 msb(ptn->tn_val->v_quad, ot, -1) == 0) { 1743 /* ok */ 1744 } else { 1745 /* conversion to '%s' due to prototype, arg #%d */ 1746 warning(259, tyname(buf, sizeof(buf), tp), arg); 1747 } 1748 } 1749 } 1750 1751 /* 1752 * Print warnings for conversions of integer types which my cause 1753 * problems. 1754 */ 1755 /* ARGSUSED */ 1756 static void 1757 iiconv(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn) 1758 { 1759 char lbuf[64], rbuf[64], opbuf[16]; 1760 if (tn->tn_op == CON) 1761 return; 1762 1763 if (op == CVT) 1764 return; 1765 1766 if (Pflag && psize(nt) > psize(ot) && isutyp(nt) != isutyp(ot)) { 1767 /* conversion to %s may sign-extend incorrectly (, arg #%d) */ 1768 if (aflag && pflag) { 1769 if (op == FARG) { 1770 warning(297, tyname(lbuf, sizeof(lbuf), tp), 1771 arg); 1772 } else { 1773 warning(131, tyname(lbuf, sizeof(lbuf), tp)); 1774 } 1775 } 1776 } 1777 1778 if (Pflag && psize(nt) > psize(ot)) { 1779 switch (tn->tn_op) { 1780 case PLUS: 1781 case MINUS: 1782 case MULT: 1783 case SHL: 1784 warning(324, 1785 tyname(rbuf, sizeof(rbuf), gettyp(ot)), 1786 tyname(lbuf, sizeof(lbuf), tp), 1787 prtnode(opbuf, sizeof(opbuf), tn)); 1788 break; 1789 default: 1790 break; 1791 } 1792 } 1793 1794 if (psize(nt) < psize(ot) && 1795 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD || 1796 aflag > 1)) { 1797 /* conversion from '%s' may lose accuracy */ 1798 if (aflag) { 1799 if (op == FARG) { 1800 warning(298, 1801 tyname(rbuf, sizeof(rbuf), tn->tn_type), 1802 tyname(lbuf, sizeof(lbuf), tp), 1803 arg); 1804 } else { 1805 warning(132, 1806 tyname(rbuf, sizeof(rbuf), tn->tn_type), 1807 tyname(lbuf, sizeof(lbuf), tp)); 1808 } 1809 } 1810 } 1811 } 1812 1813 /* 1814 * Print warnings for dubious conversions of pointer to integer. 1815 */ 1816 static void 1817 piconv(op_t op, tspec_t nt, type_t *tp, tnode_t *tn) 1818 { 1819 char buf[64]; 1820 1821 if (tn->tn_op == CON) 1822 return; 1823 1824 if (op != CVT) { 1825 /* We got already an error. */ 1826 return; 1827 } 1828 1829 if (psize(nt) < psize(PTR)) { 1830 if (pflag && size(nt) >= size(PTR)) { 1831 /* conv. of pointer to %s may lose bits */ 1832 warning(134, tyname(buf, sizeof(buf), tp)); 1833 } else { 1834 /* conv. of pointer to %s loses bits */ 1835 warning(133, tyname(buf, sizeof(buf), tp)); 1836 } 1837 } 1838 } 1839 1840 /* 1841 * Print warnings for questionable pointer conversions. 1842 */ 1843 static void 1844 ppconv(op_t op, tnode_t *tn, type_t *tp) 1845 { 1846 tspec_t nt, ot; 1847 const char *nts, *ots; 1848 1849 /* 1850 * We got already an error (pointers of different types 1851 * without a cast) or we will not get a warning. 1852 */ 1853 if (op != CVT) 1854 return; 1855 1856 nt = tp->t_subt->t_tspec; 1857 ot = tn->tn_type->t_subt->t_tspec; 1858 1859 if (nt == VOID || ot == VOID) { 1860 if (sflag && (nt == FUNC || ot == FUNC)) { 1861 /* (void *)0 already handled in convert() */ 1862 *(nt == FUNC ? &nts : &ots) = "function pointer"; 1863 *(nt == VOID ? &nts : &ots) = "'void *'"; 1864 /* ANSI C forbids conversion of %s to %s */ 1865 warning(303, ots, nts); 1866 } 1867 return; 1868 } else if (nt == FUNC && ot == FUNC) { 1869 return; 1870 } else if (nt == FUNC || ot == FUNC) { 1871 /* questionable conversion of function pointer */ 1872 warning(229); 1873 return; 1874 } 1875 1876 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) { 1877 if (hflag) 1878 /* possible pointer alignment problem */ 1879 warning(135); 1880 } 1881 if (((nt == STRUCT || nt == UNION) && 1882 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) || 1883 psize(nt) != psize(ot)) { 1884 if (cflag) { 1885 /* pointer casts may be troublesome */ 1886 warning(247); 1887 } 1888 } 1889 } 1890 1891 /* 1892 * Converts a typed constant in a constant of another type. 1893 * 1894 * op operator which requires conversion 1895 * arg if op is FARG, # of argument 1896 * tp type in which to convert the constant 1897 * nv new constant 1898 * v old constant 1899 */ 1900 void 1901 cvtcon(op_t op, int arg, type_t *tp, val_t *nv, val_t *v) 1902 { 1903 char lbuf[64], rbuf[64]; 1904 tspec_t ot, nt; 1905 ldbl_t max = 0.0, min = 0.0; 1906 int sz, rchk; 1907 int64_t xmask, xmsk1; 1908 int osz, nsz; 1909 1910 ot = v->v_tspec; 1911 nt = nv->v_tspec = tp->t_tspec; 1912 rchk = 0; 1913 1914 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) { 1915 switch (nt) { 1916 case BOOL: 1917 max = 1; min = 0; break; 1918 case CHAR: 1919 max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break; 1920 case UCHAR: 1921 max = TARG_UCHAR_MAX; min = 0; break; 1922 case SCHAR: 1923 max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break; 1924 case SHORT: 1925 max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break; 1926 case USHORT: 1927 max = TARG_USHRT_MAX; min = 0; break; 1928 case ENUM: 1929 case INT: 1930 max = TARG_INT_MAX; min = TARG_INT_MIN; break; 1931 case UINT: 1932 max = (u_int)TARG_UINT_MAX;min = 0; break; 1933 case LONG: 1934 max = TARG_LONG_MAX; min = TARG_LONG_MIN; break; 1935 case ULONG: 1936 max = (u_long)TARG_ULONG_MAX; min = 0; break; 1937 case QUAD: 1938 max = QUAD_MAX; min = QUAD_MIN; break; 1939 case UQUAD: 1940 max = (uint64_t)UQUAD_MAX; min = 0; break; 1941 case FLOAT: 1942 case FCOMPLEX: 1943 max = FLT_MAX; min = -FLT_MAX; break; 1944 case DOUBLE: 1945 case DCOMPLEX: 1946 max = DBL_MAX; min = -DBL_MAX; break; 1947 case PTR: 1948 /* Got already an error because of float --> ptr */ 1949 case LDOUBLE: 1950 case LCOMPLEX: 1951 max = LDBL_MAX; min = -LDBL_MAX; break; 1952 default: 1953 LERROR("cvtcon()"); 1954 } 1955 if (v->v_ldbl > max || v->v_ldbl < min) { 1956 if (nt == LDOUBLE) 1957 LERROR("cvtcon()"); 1958 if (op == FARG) { 1959 /* conv. of %s to %s is out of rng., arg #%d */ 1960 warning(295, tyname(lbuf, sizeof(lbuf), 1961 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 1962 arg); 1963 } else { 1964 /* conversion of %s to %s is out of range */ 1965 warning(119, tyname(lbuf, sizeof(lbuf), 1966 gettyp(ot)), 1967 tyname(rbuf, sizeof(rbuf), tp)); 1968 } 1969 v->v_ldbl = v->v_ldbl > 0 ? max : min; 1970 } 1971 if (nt == FLOAT) { 1972 nv->v_ldbl = (float)v->v_ldbl; 1973 } else if (nt == DOUBLE) { 1974 nv->v_ldbl = (double)v->v_ldbl; 1975 } else if (nt == LDOUBLE) { 1976 nv->v_ldbl = v->v_ldbl; 1977 } else { 1978 nv->v_quad = (nt == PTR || isutyp(nt)) ? 1979 (int64_t)v->v_ldbl : (int64_t)v->v_ldbl; 1980 } 1981 } else { 1982 if (nt == FLOAT) { 1983 nv->v_ldbl = (ot == PTR || isutyp(ot)) ? 1984 (float)(uint64_t)v->v_quad : (float)v->v_quad; 1985 } else if (nt == DOUBLE) { 1986 nv->v_ldbl = (ot == PTR || isutyp(ot)) ? 1987 (double)(uint64_t)v->v_quad : (double)v->v_quad; 1988 } else if (nt == LDOUBLE) { 1989 nv->v_ldbl = (ot == PTR || isutyp(ot)) ? 1990 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad; 1991 } else { 1992 rchk = 1; /* Check for lost precision. */ 1993 nv->v_quad = v->v_quad; 1994 } 1995 } 1996 1997 if (v->v_ansiu && isftyp(nt)) { 1998 /* ANSI C treats constant as unsigned */ 1999 warning(157); 2000 v->v_ansiu = 0; 2001 } else if (v->v_ansiu && (isityp(nt) && !isutyp(nt) && 2002 psize(nt) > psize(ot))) { 2003 /* ANSI C treats constant as unsigned */ 2004 warning(157); 2005 v->v_ansiu = 0; 2006 } 2007 2008 switch (nt) { 2009 case FLOAT: 2010 case FCOMPLEX: 2011 case DOUBLE: 2012 case DCOMPLEX: 2013 case LDOUBLE: 2014 case LCOMPLEX: 2015 break; 2016 default: 2017 sz = tp->t_isfield ? tp->t_flen : size(nt); 2018 nv->v_quad = xsign(nv->v_quad, nt, sz); 2019 break; 2020 } 2021 2022 if (rchk && op != CVT) { 2023 osz = size(ot); 2024 nsz = tp->t_isfield ? tp->t_flen : size(nt); 2025 xmask = qlmasks[nsz] ^ qlmasks[osz]; 2026 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1]; 2027 /* 2028 * For bitwise operations we are not interested in the 2029 * value, but in the bits itself. 2030 */ 2031 if (op == ORASS || op == OR || op == XOR) { 2032 /* 2033 * Print a warning if bits which were set are 2034 * lost due to the conversion. 2035 * This can happen with operator ORASS only. 2036 */ 2037 if (nsz < osz && (v->v_quad & xmask) != 0) { 2038 /* constant truncated by conv., op %s */ 2039 warning(306, modtab[op].m_name); 2040 } 2041 } else if (op == ANDASS || op == AND) { 2042 /* 2043 * Print a warning if additional bits are not all 1 2044 * and the most significant bit of the old value is 1, 2045 * or if at least one (but not all) removed bit was 0. 2046 */ 2047 if (nsz > osz && 2048 (nv->v_quad & qbmasks[osz - 1]) != 0 && 2049 (nv->v_quad & xmask) != xmask) { 2050 /* 2051 * extra bits set to 0 in conversion 2052 * of '%s' to '%s', op %s 2053 */ 2054 warning(309, tyname(lbuf, sizeof(lbuf), 2055 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 2056 modtab[op].m_name); 2057 } else if (nsz < osz && 2058 (v->v_quad & xmask) != xmask && 2059 (v->v_quad & xmask) != 0) { 2060 /* const. truncated by conv., op %s */ 2061 warning(306, modtab[op].m_name); 2062 } 2063 } else if ((nt != PTR && isutyp(nt)) && 2064 (ot != PTR && !isutyp(ot)) && v->v_quad < 0) { 2065 if (op == ASSIGN) { 2066 /* assignment of negative constant to ... */ 2067 warning(164); 2068 } else if (op == INIT) { 2069 /* initialisation of unsigned with neg. ... */ 2070 warning(221); 2071 } else if (op == FARG) { 2072 /* conversion of neg. const. to ..., arg #%d */ 2073 warning(296, arg); 2074 } else if (modtab[op].m_comp) { 2075 /* we get this warning already in chkcomp() */ 2076 } else { 2077 /* conversion of negative constant to ... */ 2078 warning(222); 2079 } 2080 } else if (nv->v_quad != v->v_quad && nsz <= osz && 2081 (v->v_quad & xmask) != 0 && 2082 (isutyp(ot) || (v->v_quad & xmsk1) != xmsk1)) { 2083 /* 2084 * Loss of significant bit(s). All truncated bits 2085 * of unsigned types or all truncated bits plus the 2086 * msb of the target for signed types are considered 2087 * to be significant bits. Loss of significant bits 2088 * means that at least on of the bits was set in an 2089 * unsigned type or that at least one, but not all of 2090 * the bits was set in an signed type. 2091 * Loss of significant bits means that it is not 2092 * possible, also not with necessary casts, to convert 2093 * back to the original type. A example for a 2094 * necessary cast is: 2095 * char c; int i; c = 128; 2096 * i = c; ** yields -128 ** 2097 * i = (unsigned char)c; ** yields 128 ** 2098 */ 2099 if (op == ASSIGN && tp->t_isfield) { 2100 /* precision lost in bit-field assignment */ 2101 warning(166); 2102 } else if (op == ASSIGN) { 2103 /* constant truncated by assignment */ 2104 warning(165); 2105 } else if (op == INIT && tp->t_isfield) { 2106 /* bit-field initializer does not fit */ 2107 warning(180); 2108 } else if (op == INIT) { 2109 /* initializer does not fit */ 2110 warning(178); 2111 } else if (op == CASE) { 2112 /* case label affected by conversion */ 2113 warning(196); 2114 } else if (op == FARG) { 2115 /* conv. of %s to %s is out of rng., arg #%d */ 2116 warning(295, tyname(lbuf, sizeof(lbuf), 2117 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 2118 arg); 2119 } else { 2120 /* conversion of %s to %s is out of range */ 2121 warning(119, tyname(lbuf, sizeof(lbuf), 2122 gettyp(ot)), 2123 tyname(rbuf, sizeof(rbuf), tp)); 2124 } 2125 } else if (nv->v_quad != v->v_quad) { 2126 if (op == ASSIGN && tp->t_isfield) { 2127 /* precision lost in bit-field assignment */ 2128 warning(166); 2129 } else if (op == INIT && tp->t_isfield) { 2130 /* bit-field initializer out of range */ 2131 warning(11); 2132 } else if (op == CASE) { 2133 /* case label affected by conversion */ 2134 warning(196); 2135 } else if (op == FARG) { 2136 /* conv. of %s to %s is out of rng., arg #%d */ 2137 warning(295, tyname(lbuf, sizeof(lbuf), 2138 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 2139 arg); 2140 } else { 2141 /* conversion of %s to %s is out of range */ 2142 warning(119, tyname(lbuf, sizeof(lbuf), 2143 gettyp(ot)), 2144 tyname(rbuf, sizeof(rbuf), tp)); 2145 } 2146 } 2147 } 2148 } 2149 2150 /* 2151 * Called if incompatible types were detected. 2152 * Prints a appropriate warning. 2153 */ 2154 static void 2155 incompat(op_t op, tspec_t lt, tspec_t rt) 2156 { 2157 mod_t *mp; 2158 int e = 0; 2159 2160 mp = &modtab[op]; 2161 2162 if (lt == VOID || (mp->m_binary && rt == VOID)) { 2163 /* void type illegal in expression */ 2164 e = 109; 2165 } else if (op == ASSIGN) { 2166 if ((lt == STRUCT || lt == UNION) && 2167 (rt == STRUCT || rt == UNION)) { 2168 /* assignment of different structures */ 2169 e = 240; 2170 } else { 2171 /* assignment type mismatch */ 2172 e = 171; 2173 } 2174 } else if (mp->m_binary) { 2175 /* operands of %s have incompatible types */ 2176 e = 107; 2177 } else { 2178 /* operand of %s has incompatible type */ 2179 e = 108; 2180 } 2181 switch (e) { 2182 case 0: 2183 return; 2184 case 109: 2185 error(e); 2186 return; 2187 case 108: 2188 case 107: 2189 error(e, mp->m_name, basictyname(lt), basictyname(rt)); 2190 return; 2191 default: 2192 error(e, basictyname(lt), basictyname(rt)); 2193 return; 2194 } 2195 } 2196 2197 /* 2198 * Called if incompatible pointer types are detected. 2199 * Print an appropriate warning. 2200 */ 2201 static void 2202 illptrc(mod_t *mp, type_t *ltp, type_t *rtp) 2203 { 2204 tspec_t lt, rt; 2205 2206 if (ltp->t_tspec != PTR || rtp->t_tspec != PTR) 2207 LERROR("illptrc()"); 2208 2209 lt = ltp->t_subt->t_tspec; 2210 rt = rtp->t_subt->t_tspec; 2211 2212 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) { 2213 if (mp == NULL) { 2214 /* illegal structure pointer combination */ 2215 warning(244); 2216 } else { 2217 /* illegal structure pointer combination, op %s */ 2218 warning(245, mp->m_name); 2219 } 2220 } else { 2221 if (mp == NULL) { 2222 /* illegal pointer combination */ 2223 warning(184); 2224 } else { 2225 /* illegal pointer combination, op %s */ 2226 warning(124, mp->m_name); 2227 } 2228 } 2229 } 2230 2231 /* 2232 * Make sure type (*tpp)->t_subt has at least the qualifiers 2233 * of tp1->t_subt and tp2->t_subt. 2234 */ 2235 static void 2236 mrgqual(type_t **tpp, type_t *tp1, type_t *tp2) 2237 { 2238 2239 if ((*tpp)->t_tspec != PTR || 2240 tp1->t_tspec != PTR || tp2->t_tspec != PTR) { 2241 LERROR("mrgqual()"); 2242 } 2243 2244 if ((*tpp)->t_subt->t_const == 2245 (tp1->t_subt->t_const | tp2->t_subt->t_const) && 2246 (*tpp)->t_subt->t_volatile == 2247 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) { 2248 return; 2249 } 2250 2251 *tpp = tduptyp(*tpp); 2252 (*tpp)->t_subt = tduptyp((*tpp)->t_subt); 2253 (*tpp)->t_subt->t_const = 2254 tp1->t_subt->t_const | tp2->t_subt->t_const; 2255 (*tpp)->t_subt->t_volatile = 2256 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile; 2257 } 2258 2259 /* 2260 * Returns 1 if the given structure or union has a constant member 2261 * (maybe recursively). 2262 */ 2263 static int 2264 conmemb(type_t *tp) 2265 { 2266 sym_t *m; 2267 tspec_t t; 2268 2269 if ((t = tp->t_tspec) != STRUCT && t != UNION) 2270 LERROR("conmemb()"); 2271 for (m = tp->t_str->memb; m != NULL; m = m->s_nxt) { 2272 tp = m->s_type; 2273 if (tp->t_const) 2274 return (1); 2275 if ((t = tp->t_tspec) == STRUCT || t == UNION) { 2276 if (conmemb(m->s_type)) 2277 return (1); 2278 } 2279 } 2280 return (0); 2281 } 2282 2283 /* 2284 * Create a new node for one of the operators POINT and ARROW. 2285 */ 2286 static tnode_t * 2287 bldstr(op_t op, tnode_t *ln, tnode_t *rn) 2288 { 2289 tnode_t *ntn, *ctn; 2290 int nolval; 2291 2292 if (rn->tn_op != NAME) 2293 LERROR("bldstr()"); 2294 if (rn->tn_sym->s_value.v_tspec != INT) 2295 LERROR("bldstr()"); 2296 if (rn->tn_sym->s_scl != MOS && rn->tn_sym->s_scl != MOU) 2297 LERROR("bldstr()"); 2298 2299 /* 2300 * Remember if the left operand is an lvalue (structure members 2301 * are lvalues if and only if the structure itself is an lvalue). 2302 */ 2303 nolval = op == POINT && !ln->tn_lvalue; 2304 2305 if (op == POINT) { 2306 ln = bldamper(ln, 1); 2307 } else if (ln->tn_type->t_tspec != PTR) { 2308 if (!tflag || !isityp(ln->tn_type->t_tspec)) 2309 LERROR("bldstr()"); 2310 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln); 2311 } 2312 2313 #if PTRDIFF_IS_LONG 2314 ctn = getinode(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT); 2315 #else 2316 ctn = getinode(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT); 2317 #endif 2318 2319 ntn = mktnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn); 2320 if (ln->tn_op == CON) 2321 ntn = fold(ntn); 2322 2323 if (rn->tn_type->t_isfield) { 2324 ntn = mktnode(FSEL, ntn->tn_type->t_subt, ntn, NULL); 2325 } else { 2326 ntn = mktnode(STAR, ntn->tn_type->t_subt, ntn, NULL); 2327 } 2328 2329 if (nolval) 2330 ntn->tn_lvalue = 0; 2331 2332 return (ntn); 2333 } 2334 2335 /* 2336 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF. 2337 */ 2338 static tnode_t * 2339 bldincdec(op_t op, tnode_t *ln) 2340 { 2341 tnode_t *cn, *ntn; 2342 2343 if (ln == NULL) 2344 LERROR("bldincdec()"); 2345 2346 if (ln->tn_type->t_tspec == PTR) { 2347 cn = plength(ln->tn_type); 2348 } else { 2349 cn = getinode(INT, (int64_t)1); 2350 } 2351 ntn = mktnode(op, ln->tn_type, ln, cn); 2352 2353 return (ntn); 2354 } 2355 2356 /* 2357 * Create a node for REAL, IMAG 2358 */ 2359 static tnode_t * 2360 bldri(op_t op, tnode_t *ln) 2361 { 2362 tnode_t *cn, *ntn; 2363 char buf[64]; 2364 2365 if (ln == NULL) 2366 LERROR("bldincdec()"); 2367 2368 switch (ln->tn_type->t_tspec) { 2369 case LCOMPLEX: 2370 cn = getinode(LDOUBLE, (int64_t)1); 2371 break; 2372 case DCOMPLEX: 2373 cn = getinode(DOUBLE, (int64_t)1); 2374 break; 2375 case FCOMPLEX: 2376 cn = getinode(FLOAT, (int64_t)1); 2377 break; 2378 default: 2379 error(276, op == REAL ? "real" : "imag", 2380 tyname(buf, sizeof(buf), ln->tn_type)); 2381 return NULL; 2382 } 2383 ntn = mktnode(op, cn->tn_type, ln, cn); 2384 ntn->tn_lvalue = 1; 2385 2386 return (ntn); 2387 } 2388 /* 2389 * Create a tree node for the & operator 2390 */ 2391 static tnode_t * 2392 bldamper(tnode_t *tn, int noign) 2393 { 2394 tnode_t *ntn; 2395 tspec_t t; 2396 2397 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) { 2398 /* & before array or function: ignored */ 2399 if (tflag) 2400 warning(127); 2401 return (tn); 2402 } 2403 2404 /* eliminate &* */ 2405 if (tn->tn_op == STAR && 2406 tn->tn_left->tn_type->t_tspec == PTR && 2407 tn->tn_left->tn_type->t_subt == tn->tn_type) { 2408 return (tn->tn_left); 2409 } 2410 2411 ntn = mktnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL); 2412 2413 return (ntn); 2414 } 2415 2416 /* 2417 * Create a node for operators PLUS and MINUS. 2418 */ 2419 static tnode_t * 2420 bldplmi(op_t op, tnode_t *ln, tnode_t *rn) 2421 { 2422 tnode_t *ntn, *ctn; 2423 type_t *tp; 2424 2425 /* If pointer and integer, then pointer to the lhs. */ 2426 if (rn->tn_type->t_tspec == PTR && isityp(ln->tn_type->t_tspec)) { 2427 ntn = ln; 2428 ln = rn; 2429 rn = ntn; 2430 } 2431 2432 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) { 2433 2434 if (!isityp(rn->tn_type->t_tspec)) 2435 LERROR("bldplmi()"); 2436 2437 ctn = plength(ln->tn_type); 2438 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec) 2439 rn = convert(NOOP, 0, ctn->tn_type, rn); 2440 rn = mktnode(MULT, rn->tn_type, rn, ctn); 2441 if (rn->tn_left->tn_op == CON) 2442 rn = fold(rn); 2443 ntn = mktnode(op, ln->tn_type, ln, rn); 2444 2445 } else if (rn->tn_type->t_tspec == PTR) { 2446 2447 if (ln->tn_type->t_tspec != PTR || op != MINUS) 2448 LERROR("bldplmi()"); 2449 #if PTRDIFF_IS_LONG 2450 tp = gettyp(LONG); 2451 #else 2452 tp = gettyp(INT); 2453 #endif 2454 ntn = mktnode(op, tp, ln, rn); 2455 if (ln->tn_op == CON && rn->tn_op == CON) 2456 ntn = fold(ntn); 2457 ctn = plength(ln->tn_type); 2458 balance(NOOP, &ntn, &ctn); 2459 ntn = mktnode(DIV, tp, ntn, ctn); 2460 2461 } else { 2462 2463 ntn = mktnode(op, ln->tn_type, ln, rn); 2464 2465 } 2466 return (ntn); 2467 } 2468 2469 /* 2470 * Create a node for operators SHL and SHR. 2471 */ 2472 static tnode_t * 2473 bldshft(op_t op, tnode_t *ln, tnode_t *rn) 2474 { 2475 tspec_t t; 2476 tnode_t *ntn; 2477 2478 if ((t = rn->tn_type->t_tspec) != INT && t != UINT) 2479 rn = convert(CVT, 0, gettyp(INT), rn); 2480 ntn = mktnode(op, ln->tn_type, ln, rn); 2481 return (ntn); 2482 } 2483 2484 /* 2485 * Create a node for COLON. 2486 */ 2487 static tnode_t * 2488 bldcol(tnode_t *ln, tnode_t *rn) 2489 { 2490 tspec_t lt, rt, pdt; 2491 type_t *rtp; 2492 tnode_t *ntn; 2493 2494 lt = ln->tn_type->t_tspec; 2495 rt = rn->tn_type->t_tspec; 2496 #if PTRDIFF_IS_LONG 2497 pdt = LONG; 2498 #else 2499 pdt = INT; 2500 #endif 2501 2502 /* 2503 * Arithmetic types are balanced, all other type combinations 2504 * still need to be handled. 2505 */ 2506 if (isatyp(lt) && isatyp(rt)) { 2507 rtp = ln->tn_type; 2508 } else if (lt == VOID || rt == VOID) { 2509 rtp = gettyp(VOID); 2510 } else if (lt == STRUCT || lt == UNION) { 2511 /* Both types must be identical. */ 2512 if (rt != STRUCT && rt != UNION) 2513 LERROR("bldcol()"); 2514 if (ln->tn_type->t_str != rn->tn_type->t_str) 2515 LERROR("bldcol()"); 2516 if (incompl(ln->tn_type)) { 2517 /* unknown operand size, op %s */ 2518 error(138, modtab[COLON].m_name); 2519 return (NULL); 2520 } 2521 rtp = ln->tn_type; 2522 } else if (lt == PTR && isityp(rt)) { 2523 if (rt != pdt) { 2524 rn = convert(NOOP, 0, gettyp(pdt), rn); 2525 rt = pdt; 2526 } 2527 rtp = ln->tn_type; 2528 } else if (rt == PTR && isityp(lt)) { 2529 if (lt != pdt) { 2530 ln = convert(NOOP, 0, gettyp(pdt), ln); 2531 lt = pdt; 2532 } 2533 rtp = rn->tn_type; 2534 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) { 2535 if (rt != PTR) 2536 LERROR("bldcol()"); 2537 rtp = rn->tn_type; 2538 mrgqual(&rtp, ln->tn_type, rn->tn_type); 2539 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) { 2540 if (lt != PTR) 2541 LERROR("bldcol()"); 2542 rtp = ln->tn_type; 2543 mrgqual(&rtp, ln->tn_type, rn->tn_type); 2544 } else { 2545 if (lt != PTR || rt != PTR) 2546 LERROR("bldcol()"); 2547 /* 2548 * XXX For now we simply take the left type. This is 2549 * probably wrong, if one type contains a functionprototype 2550 * and the other one, at the same place, only an old style 2551 * declaration. 2552 */ 2553 rtp = ln->tn_type; 2554 mrgqual(&rtp, ln->tn_type, rn->tn_type); 2555 } 2556 2557 ntn = mktnode(COLON, rtp, ln, rn); 2558 2559 return (ntn); 2560 } 2561 2562 /* 2563 * Create a node for an assignment operator (both = and op= ). 2564 */ 2565 static tnode_t * 2566 bldasgn(op_t op, tnode_t *ln, tnode_t *rn) 2567 { 2568 tspec_t lt, rt; 2569 tnode_t *ntn, *ctn; 2570 2571 if (ln == NULL || rn == NULL) 2572 LERROR("bldasgn()"); 2573 2574 lt = ln->tn_type->t_tspec; 2575 rt = rn->tn_type->t_tspec; 2576 2577 if ((op == ADDASS || op == SUBASS) && lt == PTR) { 2578 if (!isityp(rt)) 2579 LERROR("bldasgn()"); 2580 ctn = plength(ln->tn_type); 2581 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec) 2582 rn = convert(NOOP, 0, ctn->tn_type, rn); 2583 rn = mktnode(MULT, rn->tn_type, rn, ctn); 2584 if (rn->tn_left->tn_op == CON) 2585 rn = fold(rn); 2586 } 2587 2588 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) { 2589 if (rt != lt || ln->tn_type->t_str != rn->tn_type->t_str) 2590 LERROR("bldasgn()"); 2591 if (incompl(ln->tn_type)) { 2592 if (op == RETURN) { 2593 /* cannot return incomplete type */ 2594 error(212); 2595 } else { 2596 /* unknown operand size, op %s */ 2597 error(138, modtab[op].m_name); 2598 } 2599 return (NULL); 2600 } 2601 } 2602 2603 if (op == SHLASS) { 2604 if (psize(lt) < psize(rt)) { 2605 if (hflag) 2606 /* semantics of %s change in ANSI C; use ... */ 2607 warning(118, "<<="); 2608 } 2609 } else if (op != SHRASS) { 2610 if (op == ASSIGN || lt != PTR) { 2611 if (lt != rt || 2612 (ln->tn_type->t_isfield && rn->tn_op == CON)) { 2613 rn = convert(op, 0, ln->tn_type, rn); 2614 rt = lt; 2615 } 2616 } 2617 } 2618 2619 ntn = mktnode(op, ln->tn_type, ln, rn); 2620 2621 return (ntn); 2622 } 2623 2624 /* 2625 * Get length of type tp->t_subt. 2626 */ 2627 static tnode_t * 2628 plength(type_t *tp) 2629 { 2630 int elem, elsz; 2631 tspec_t st; 2632 2633 if (tp->t_tspec != PTR) 2634 LERROR("plength()"); 2635 tp = tp->t_subt; 2636 2637 elem = 1; 2638 elsz = 0; 2639 2640 while (tp->t_tspec == ARRAY) { 2641 elem *= tp->t_dim; 2642 tp = tp->t_subt; 2643 } 2644 2645 switch (tp->t_tspec) { 2646 case FUNC: 2647 /* pointer to function is not allowed here */ 2648 error(110); 2649 break; 2650 case VOID: 2651 /* cannot do pointer arithmetic on operand of ... */ 2652 (void)gnuism(136); 2653 break; 2654 case STRUCT: 2655 case UNION: 2656 if ((elsz = tp->t_str->size) == 0) 2657 /* cannot do pointer arithmetic on operand of ... */ 2658 error(136); 2659 break; 2660 case ENUM: 2661 if (incompl(tp)) { 2662 /* cannot do pointer arithmetic on operand of ... */ 2663 warning(136); 2664 } 2665 /* FALLTHROUGH */ 2666 default: 2667 if ((elsz = size(tp->t_tspec)) == 0) { 2668 /* cannot do pointer arithmetic on operand of ... */ 2669 error(136); 2670 } else if (elsz == -1) { 2671 LERROR("plength()"); 2672 } 2673 break; 2674 } 2675 2676 if (elem == 0 && elsz != 0) { 2677 /* cannot do pointer arithmetic on operand of ... */ 2678 error(136); 2679 } 2680 2681 if (elsz == 0) 2682 elsz = CHAR_BIT; 2683 2684 #if PTRDIFF_IS_LONG 2685 st = LONG; 2686 #else 2687 st = INT; 2688 #endif 2689 2690 return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT))); 2691 } 2692 2693 /* 2694 * XXX 2695 * Note: There appear to be a number of bugs in detecting overflow in 2696 * this function. An audit and a set of proper regression tests are needed. 2697 * --Perry Metzger, Nov. 16, 2001 2698 */ 2699 /* 2700 * Do only as much as necessary to compute constant expressions. 2701 * Called only if the operator allows folding and (both) operands 2702 * are constants. 2703 */ 2704 static tnode_t * 2705 fold(tnode_t *tn) 2706 { 2707 val_t *v; 2708 tspec_t t; 2709 int utyp, ovfl; 2710 int64_t sl, sr = 0, q = 0, mask; 2711 uint64_t ul, ur = 0; 2712 tnode_t *cn; 2713 2714 v = xcalloc(1, sizeof (val_t)); 2715 v->v_tspec = t = tn->tn_type->t_tspec; 2716 2717 utyp = t == PTR || isutyp(t); 2718 ul = sl = tn->tn_left->tn_val->v_quad; 2719 if (modtab[tn->tn_op].m_binary) 2720 ur = sr = tn->tn_right->tn_val->v_quad; 2721 2722 mask = qlmasks[size(t)]; 2723 ovfl = 0; 2724 2725 switch (tn->tn_op) { 2726 case UPLUS: 2727 q = sl; 2728 break; 2729 case UMINUS: 2730 q = -sl; 2731 if (sl != 0 && msb(q, t, -1) == msb(sl, t, -1)) 2732 ovfl = 1; 2733 break; 2734 case COMPL: 2735 q = ~sl; 2736 break; 2737 case MULT: 2738 if (utyp) { 2739 q = ul * ur; 2740 if (q != (q & mask)) 2741 ovfl = 1; 2742 else if ((ul != 0) && ((q / ul) != ur)) 2743 ovfl = 1; 2744 } else { 2745 q = sl * sr; 2746 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1))) 2747 ovfl = 1; 2748 } 2749 break; 2750 case DIV: 2751 if (sr == 0) { 2752 /* division by 0 */ 2753 error(139); 2754 q = utyp ? UQUAD_MAX : QUAD_MAX; 2755 } else { 2756 q = utyp ? (int64_t)(ul / ur) : sl / sr; 2757 } 2758 break; 2759 case MOD: 2760 if (sr == 0) { 2761 /* modulus by 0 */ 2762 error(140); 2763 q = 0; 2764 } else { 2765 q = utyp ? (int64_t)(ul % ur) : sl % sr; 2766 } 2767 break; 2768 case PLUS: 2769 q = utyp ? (int64_t)(ul + ur) : sl + sr; 2770 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) { 2771 if (msb(q, t, -1) == 0) 2772 ovfl = 1; 2773 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) { 2774 if (msb(q, t, -1) != 0) 2775 ovfl = 1; 2776 } 2777 break; 2778 case MINUS: 2779 q = utyp ? (int64_t)(ul - ur) : sl - sr; 2780 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) { 2781 if (msb(q, t, -1) == 0) 2782 ovfl = 1; 2783 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) { 2784 if (msb(q, t, -1) != 0) 2785 ovfl = 1; 2786 } 2787 break; 2788 case SHL: 2789 q = utyp ? (int64_t)(ul << sr) : sl << sr; 2790 break; 2791 case SHR: 2792 /* 2793 * The sign must be explicitly extended because 2794 * shifts of signed values are implementation dependent. 2795 */ 2796 q = ul >> sr; 2797 q = xsign(q, t, size(t) - (int)sr); 2798 break; 2799 case LT: 2800 q = utyp ? ul < ur : sl < sr; 2801 break; 2802 case LE: 2803 q = utyp ? ul <= ur : sl <= sr; 2804 break; 2805 case GE: 2806 q = utyp ? ul >= ur : sl >= sr; 2807 break; 2808 case GT: 2809 q = utyp ? ul > ur : sl > sr; 2810 break; 2811 case EQ: 2812 q = utyp ? ul == ur : sl == sr; 2813 break; 2814 case NE: 2815 q = utyp ? ul != ur : sl != sr; 2816 break; 2817 case AND: 2818 q = utyp ? (int64_t)(ul & ur) : sl & sr; 2819 break; 2820 case XOR: 2821 q = utyp ? (int64_t)(ul ^ ur) : sl ^ sr; 2822 break; 2823 case OR: 2824 q = utyp ? (int64_t)(ul | ur) : sl | sr; 2825 break; 2826 default: 2827 LERROR("fold()"); 2828 } 2829 2830 /* XXX does not work for quads. */ 2831 if (ovfl || ((uint64_t)(q | mask) != ~(uint64_t)0 && 2832 (q & ~mask) != 0)) { 2833 if (hflag) 2834 /* integer overflow detected, op %s */ 2835 warning(141, modtab[tn->tn_op].m_name); 2836 } 2837 2838 v->v_quad = xsign(q, t, -1); 2839 2840 cn = getcnode(tn->tn_type, v); 2841 2842 return (cn); 2843 } 2844 2845 /* 2846 * Same for operators whose operands are compared with 0 (test context). 2847 */ 2848 static tnode_t * 2849 foldtst(tnode_t *tn) 2850 { 2851 int l, r = 0; 2852 val_t *v; 2853 2854 v = xcalloc(1, sizeof (val_t)); 2855 v->v_tspec = tn->tn_type->t_tspec; 2856 if (tn->tn_type->t_tspec != INT) 2857 LERROR("foldtst()"); 2858 2859 if (isftyp(tn->tn_left->tn_type->t_tspec)) { 2860 l = tn->tn_left->tn_val->v_ldbl != 0.0; 2861 } else { 2862 l = tn->tn_left->tn_val->v_quad != 0; 2863 } 2864 2865 if (modtab[tn->tn_op].m_binary) { 2866 if (isftyp(tn->tn_right->tn_type->t_tspec)) { 2867 r = tn->tn_right->tn_val->v_ldbl != 0.0; 2868 } else { 2869 r = tn->tn_right->tn_val->v_quad != 0; 2870 } 2871 } 2872 2873 switch (tn->tn_op) { 2874 case NOT: 2875 if (hflag && !ccflg) 2876 /* constant argument to NOT */ 2877 warning(239); 2878 v->v_quad = !l; 2879 break; 2880 case LOGAND: 2881 v->v_quad = l && r; 2882 break; 2883 case LOGOR: 2884 v->v_quad = l || r; 2885 break; 2886 default: 2887 LERROR("foldtst()"); 2888 } 2889 2890 return (getcnode(tn->tn_type, v)); 2891 } 2892 2893 /* 2894 * Same for operands with floating point type. 2895 */ 2896 static tnode_t * 2897 foldflt(tnode_t *tn) 2898 { 2899 val_t *v; 2900 tspec_t t; 2901 ldbl_t l, r = 0; 2902 2903 fpe = 0; 2904 v = xcalloc(1, sizeof (val_t)); 2905 v->v_tspec = t = tn->tn_type->t_tspec; 2906 2907 if (!isftyp(t)) 2908 LERROR("foldflt()"); 2909 2910 if (t != tn->tn_left->tn_type->t_tspec) 2911 LERROR("foldflt()"); 2912 if (modtab[tn->tn_op].m_binary && t != tn->tn_right->tn_type->t_tspec) 2913 LERROR("foldflt()"); 2914 2915 l = tn->tn_left->tn_val->v_ldbl; 2916 if (modtab[tn->tn_op].m_binary) 2917 r = tn->tn_right->tn_val->v_ldbl; 2918 2919 switch (tn->tn_op) { 2920 case UPLUS: 2921 v->v_ldbl = l; 2922 break; 2923 case UMINUS: 2924 v->v_ldbl = -l; 2925 break; 2926 case MULT: 2927 v->v_ldbl = l * r; 2928 break; 2929 case DIV: 2930 if (r == 0.0) { 2931 /* division by 0 */ 2932 error(139); 2933 if (t == FLOAT) { 2934 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX; 2935 } else if (t == DOUBLE) { 2936 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX; 2937 } else { 2938 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX; 2939 } 2940 } else { 2941 v->v_ldbl = l / r; 2942 } 2943 break; 2944 case PLUS: 2945 v->v_ldbl = l + r; 2946 break; 2947 case MINUS: 2948 v->v_ldbl = l - r; 2949 break; 2950 case LT: 2951 v->v_quad = l < r; 2952 break; 2953 case LE: 2954 v->v_quad = l <= r; 2955 break; 2956 case GE: 2957 v->v_quad = l >= r; 2958 break; 2959 case GT: 2960 v->v_quad = l > r; 2961 break; 2962 case EQ: 2963 v->v_quad = l == r; 2964 break; 2965 case NE: 2966 v->v_quad = l != r; 2967 break; 2968 default: 2969 LERROR("foldflt()"); 2970 } 2971 2972 if (!fpe && isnan((double)v->v_ldbl)) 2973 LERROR("foldflt()"); 2974 if (fpe || !finite((double)v->v_ldbl) || 2975 (t == FLOAT && 2976 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) || 2977 (t == DOUBLE && 2978 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) { 2979 /* floating point overflow detected, op %s */ 2980 warning(142, modtab[tn->tn_op].m_name); 2981 if (t == FLOAT) { 2982 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX; 2983 } else if (t == DOUBLE) { 2984 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX; 2985 } else { 2986 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX; 2987 } 2988 fpe = 0; 2989 } 2990 2991 return (getcnode(tn->tn_type, v)); 2992 } 2993 2994 2995 /* 2996 * Create a constant node for sizeof. 2997 */ 2998 tnode_t * 2999 bldszof(type_t *tp) 3000 { 3001 tspec_t st; 3002 #if SIZEOF_IS_ULONG 3003 st = ULONG; 3004 #else 3005 st = UINT; 3006 #endif 3007 return getinode(st, tsize(tp) / CHAR_BIT); 3008 } 3009 3010 int64_t 3011 tsize(type_t *tp) 3012 { 3013 int elem, elsz, flex; 3014 3015 elem = 1; 3016 flex = 0; 3017 while (tp->t_tspec == ARRAY) { 3018 flex = 1; /* allow c99 flex arrays [] [0] */ 3019 elem *= tp->t_dim; 3020 tp = tp->t_subt; 3021 } 3022 if (elem == 0) { 3023 if (!flex) { 3024 /* cannot take size of incomplete type */ 3025 error(143); 3026 elem = 1; 3027 } 3028 } 3029 switch (tp->t_tspec) { 3030 case FUNC: 3031 /* cannot take size of function */ 3032 error(144); 3033 elsz = 1; 3034 break; 3035 case STRUCT: 3036 case UNION: 3037 if (incompl(tp)) { 3038 /* cannot take size of incomplete type */ 3039 error(143); 3040 elsz = 1; 3041 } else { 3042 elsz = tp->t_str->size; 3043 } 3044 break; 3045 case ENUM: 3046 if (incompl(tp)) { 3047 /* cannot take size of incomplete type */ 3048 warning(143); 3049 } 3050 /* FALLTHROUGH */ 3051 default: 3052 if (tp->t_isfield) { 3053 /* cannot take size of bit-field */ 3054 error(145); 3055 } 3056 if (tp->t_tspec == VOID) { 3057 /* cannot take size of void */ 3058 error(146); 3059 elsz = 1; 3060 } else { 3061 elsz = size(tp->t_tspec); 3062 if (elsz <= 0) 3063 LERROR("bldszof()"); 3064 } 3065 break; 3066 } 3067 3068 return (int64_t)(elem * elsz); 3069 } 3070 3071 /* 3072 */ 3073 tnode_t * 3074 bldalof(type_t *tp) 3075 { 3076 tspec_t st; 3077 3078 switch (tp->t_tspec) { 3079 case ARRAY: 3080 break; 3081 3082 case FUNC: 3083 /* cannot take align of function */ 3084 error(144); 3085 return 0; 3086 3087 case STRUCT: 3088 case UNION: 3089 if (incompl(tp)) { 3090 /* cannot take align of incomplete type */ 3091 error(143); 3092 return 0; 3093 } 3094 break; 3095 case ENUM: 3096 break; 3097 default: 3098 if (tp->t_isfield) { 3099 /* cannot take align of bit-field */ 3100 error(145); 3101 return 0; 3102 } 3103 if (tp->t_tspec == VOID) { 3104 /* cannot take alignsize of void */ 3105 error(146); 3106 return 0; 3107 } 3108 break; 3109 } 3110 3111 #if SIZEOF_IS_ULONG 3112 st = ULONG; 3113 #else 3114 st = UINT; 3115 #endif 3116 3117 return getinode(st, (int64_t)getbound(tp)); 3118 } 3119 3120 /* 3121 * Type casts. 3122 */ 3123 tnode_t * 3124 cast(tnode_t *tn, type_t *tp) 3125 { 3126 tspec_t nt, ot; 3127 3128 if (tn == NULL) 3129 return (NULL); 3130 3131 tn = cconv(tn); 3132 3133 nt = tp->t_tspec; 3134 ot = tn->tn_type->t_tspec; 3135 3136 if (nt == VOID) { 3137 /* 3138 * XXX ANSI C requires scalar types or void (Plauger&Brodie). 3139 * But this seams really questionable. 3140 */ 3141 } else if (nt == STRUCT || nt == UNION || nt == ARRAY || nt == FUNC) { 3142 /* invalid cast expression */ 3143 error(147); 3144 return (NULL); 3145 } else if (ot == STRUCT || ot == UNION) { 3146 /* invalid cast expression */ 3147 error(147); 3148 return (NULL); 3149 } else if (ot == VOID) { 3150 /* improper cast of void expression */ 3151 error(148); 3152 return (NULL); 3153 } else if (isityp(nt) && issclt(ot)) { 3154 /* ok */ 3155 } else if (isftyp(nt) && isatyp(ot)) { 3156 /* ok */ 3157 } else if (nt == PTR && isityp(ot)) { 3158 /* ok */ 3159 } else if (nt == PTR && ot == PTR) { 3160 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) { 3161 if (hflag) 3162 /* cast discards 'const' from ... */ 3163 warning(275); 3164 } 3165 } else { 3166 /* invalid cast expression */ 3167 error(147); 3168 return (NULL); 3169 } 3170 3171 tn = convert(CVT, 0, tp, tn); 3172 tn->tn_cast = 1; 3173 3174 return (tn); 3175 } 3176 3177 /* 3178 * Create the node for a function argument. 3179 * All necessary conversions and type checks are done in funccall(), because 3180 * in funcarg() we have no information about expected argument types. 3181 */ 3182 tnode_t * 3183 funcarg(tnode_t *args, tnode_t *arg) 3184 { 3185 tnode_t *ntn; 3186 3187 /* 3188 * If there was a serious error in the expression for the argument, 3189 * create a dummy argument so the positions of the remaining arguments 3190 * will not change. 3191 */ 3192 if (arg == NULL) 3193 arg = getinode(INT, (int64_t)0); 3194 3195 ntn = mktnode(PUSH, arg->tn_type, arg, args); 3196 3197 return (ntn); 3198 } 3199 3200 /* 3201 * Create the node for a function call. Also check types of 3202 * function arguments and insert conversions, if necessary. 3203 */ 3204 tnode_t * 3205 funccall(tnode_t *func, tnode_t *args) 3206 { 3207 tnode_t *ntn; 3208 op_t fcop; 3209 3210 if (func == NULL) 3211 return (NULL); 3212 3213 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) { 3214 fcop = CALL; 3215 } else { 3216 fcop = ICALL; 3217 } 3218 3219 /* 3220 * after cconv() func will always be a pointer to a function 3221 * if it is a valid function designator. 3222 */ 3223 func = cconv(func); 3224 3225 if (func->tn_type->t_tspec != PTR || 3226 func->tn_type->t_subt->t_tspec != FUNC) { 3227 char buf[256]; 3228 /* illegal function */ 3229 error(149, tyname(buf, sizeof(buf), func->tn_type)); 3230 return (NULL); 3231 } 3232 3233 args = chkfarg(func->tn_type->t_subt, args); 3234 3235 ntn = mktnode(fcop, func->tn_type->t_subt->t_subt, func, args); 3236 3237 return (ntn); 3238 } 3239 3240 /* 3241 * Check types of all function arguments and insert conversions, 3242 * if necessary. 3243 */ 3244 static tnode_t * 3245 chkfarg(type_t *ftp, tnode_t *args) 3246 { 3247 tnode_t *arg; 3248 sym_t *asym; 3249 tspec_t at; 3250 int narg, npar, n, i; 3251 3252 /* get # of args in the prototype */ 3253 npar = 0; 3254 for (asym = ftp->t_args; asym != NULL; asym = asym->s_nxt) 3255 npar++; 3256 3257 /* get # of args in function call */ 3258 narg = 0; 3259 for (arg = args; arg != NULL; arg = arg->tn_right) 3260 narg++; 3261 3262 asym = ftp->t_args; 3263 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) { 3264 /* argument mismatch: %d arg%s passed, %d expected */ 3265 error(150, narg, narg > 1 ? "s" : "", npar); 3266 asym = NULL; 3267 } 3268 3269 for (n = 1; n <= narg; n++) { 3270 3271 /* 3272 * The rightmost argument is at the top of the argument 3273 * subtree. 3274 */ 3275 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) 3276 continue; 3277 3278 /* some things which are always not allowd */ 3279 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) { 3280 /* void expressions may not be arguments, arg #%d */ 3281 error(151, n); 3282 return (NULL); 3283 } else if ((at == STRUCT || at == UNION) && 3284 incompl(arg->tn_left->tn_type)) { 3285 /* argument cannot have unknown size, arg #%d */ 3286 error(152, n); 3287 return (NULL); 3288 } else if (isityp(at) && arg->tn_left->tn_type->t_isenum && 3289 incompl(arg->tn_left->tn_type)) { 3290 /* argument cannot have unknown size, arg #%d */ 3291 warning(152, n); 3292 } 3293 3294 /* class conversions (arg in value context) */ 3295 arg->tn_left = cconv(arg->tn_left); 3296 3297 if (asym != NULL) { 3298 arg->tn_left = parg(n, asym->s_type, arg->tn_left); 3299 } else { 3300 arg->tn_left = promote(NOOP, 1, arg->tn_left); 3301 } 3302 arg->tn_type = arg->tn_left->tn_type; 3303 3304 if (asym != NULL) 3305 asym = asym->s_nxt; 3306 } 3307 3308 return (args); 3309 } 3310 3311 /* 3312 * Compare the type of an argument with the corresponding type of a 3313 * prototype parameter. If it is a valid combination, but both types 3314 * are not the same, insert a conversion to convert the argument into 3315 * the type of the parameter. 3316 */ 3317 static tnode_t * 3318 parg( int n, /* pos of arg */ 3319 type_t *tp, /* expected type (from prototype) */ 3320 tnode_t *tn) /* argument */ 3321 { 3322 tnode_t *ln; 3323 int dowarn; 3324 3325 ln = xcalloc(1, sizeof (tnode_t)); 3326 ln->tn_type = tduptyp(tp); 3327 ln->tn_type->t_const = 0; 3328 ln->tn_lvalue = 1; 3329 if (typeok(FARG, n, ln, tn)) { 3330 if (!eqtype(tp, tn->tn_type, 1, 0, (dowarn = 0, &dowarn)) || dowarn) 3331 tn = convert(FARG, n, tp, tn); 3332 } 3333 free(ln); 3334 return (tn); 3335 } 3336 3337 /* 3338 * Return the value of an integral constant expression. 3339 * If the expression is not constant or its type is not an integer 3340 * type, an error message is printed. 3341 */ 3342 val_t * 3343 constant(tnode_t *tn, int required) 3344 { 3345 val_t *v; 3346 3347 if (tn != NULL) 3348 tn = cconv(tn); 3349 if (tn != NULL) 3350 tn = promote(NOOP, 0, tn); 3351 3352 v = xcalloc(1, sizeof (val_t)); 3353 3354 if (tn == NULL) { 3355 if (nerr == 0) 3356 LERROR("constant()"); 3357 v->v_tspec = INT; 3358 v->v_quad = 1; 3359 return (v); 3360 } 3361 3362 v->v_tspec = tn->tn_type->t_tspec; 3363 3364 if (tn->tn_op == CON) { 3365 if (tn->tn_type->t_tspec != tn->tn_val->v_tspec) 3366 LERROR("constant()"); 3367 if (isityp(tn->tn_val->v_tspec)) { 3368 v->v_ansiu = tn->tn_val->v_ansiu; 3369 v->v_quad = tn->tn_val->v_quad; 3370 return (v); 3371 } 3372 v->v_quad = tn->tn_val->v_ldbl; 3373 } else { 3374 v->v_quad = 1; 3375 } 3376 3377 /* integral constant expression expected */ 3378 if (required) 3379 error(55); 3380 else 3381 c99ism(318); 3382 3383 if (!isityp(v->v_tspec)) 3384 v->v_tspec = INT; 3385 3386 return (v); 3387 } 3388 3389 /* 3390 * Perform some tests on expressions which can't be done in build() and 3391 * functions called by build(). These tests must be done here because 3392 * we need some information about the context in which the operations 3393 * are performed. 3394 * After all tests are performed, expr() frees the memory which is used 3395 * for the expression. 3396 */ 3397 void 3398 expr(tnode_t *tn, int vctx, int tctx, int dofreeblk) 3399 { 3400 3401 if (tn == NULL && nerr == 0) 3402 LERROR("expr()"); 3403 3404 if (tn == NULL) { 3405 tfreeblk(); 3406 return; 3407 } 3408 3409 /* expr() is also called in global initialisations */ 3410 if (dcs->d_ctx != EXTERN) 3411 chkreach(); 3412 3413 chkmisc(tn, vctx, tctx, !tctx, 0, 0, 0); 3414 if (tn->tn_op == ASSIGN) { 3415 if (hflag && tctx) 3416 /* assignment in conditional context */ 3417 warning(159); 3418 } else if (tn->tn_op == CON) { 3419 if (hflag && tctx && !ccflg) 3420 /* constant in conditional context */ 3421 warning(161); 3422 } 3423 if (!modtab[tn->tn_op].m_sideeff) { 3424 /* 3425 * for left operands of COMMA this warning is already 3426 * printed 3427 */ 3428 if (tn->tn_op != COMMA && !vctx && !tctx) 3429 nulleff(tn); 3430 } 3431 if (dflag) 3432 displexpr(tn, 0); 3433 3434 /* free the tree memory */ 3435 if (dofreeblk) 3436 tfreeblk(); 3437 } 3438 3439 static void 3440 nulleff(tnode_t *tn) 3441 { 3442 3443 if (!hflag) 3444 return; 3445 3446 while (!modtab[tn->tn_op].m_sideeff) { 3447 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) { 3448 tn = tn->tn_left; 3449 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) { 3450 /* 3451 * && and || have a side effect if the right operand 3452 * has a side effect. 3453 */ 3454 tn = tn->tn_right; 3455 } else if (tn->tn_op == QUEST) { 3456 /* 3457 * ? has a side effect if at least one of its right 3458 * operands has a side effect 3459 */ 3460 tn = tn->tn_right; 3461 } else if (tn->tn_op == COLON || tn->tn_op == COMMA) { 3462 /* 3463 * : has a side effect if at least one of its operands 3464 * has a side effect 3465 */ 3466 if (modtab[tn->tn_left->tn_op].m_sideeff) { 3467 tn = tn->tn_left; 3468 } else if (modtab[tn->tn_right->tn_op].m_sideeff) { 3469 tn = tn->tn_right; 3470 } else { 3471 break; 3472 } 3473 } else { 3474 break; 3475 } 3476 } 3477 if (!modtab[tn->tn_op].m_sideeff) 3478 /* expression has null effect */ 3479 warning(129); 3480 } 3481 3482 /* 3483 * Dump an expression to stdout 3484 * only used for debugging 3485 */ 3486 static void 3487 displexpr(tnode_t *tn, int offs) 3488 { 3489 uint64_t uq; 3490 3491 if (tn == NULL) { 3492 (void)printf("%*s%s\n", offs, "", "NULL"); 3493 return; 3494 } 3495 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name); 3496 3497 if (tn->tn_op == NAME) { 3498 (void)printf("%s: %s ", 3499 tn->tn_sym->s_name, scltoa(tn->tn_sym->s_scl)); 3500 } else if (tn->tn_op == CON && isftyp(tn->tn_type->t_tspec)) { 3501 (void)printf("%#g ", (double)tn->tn_val->v_ldbl); 3502 } else if (tn->tn_op == CON && isityp(tn->tn_type->t_tspec)) { 3503 uq = tn->tn_val->v_quad; 3504 (void)printf("0x %08lx %08lx ", (long)(uq >> 32) & 0xffffffffl, 3505 (long)uq & 0xffffffffl); 3506 } else if (tn->tn_op == CON) { 3507 if (tn->tn_type->t_tspec != PTR) 3508 LERROR("displexpr()"); 3509 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4), 3510 (u_long)tn->tn_val->v_quad); 3511 } else if (tn->tn_op == STRING) { 3512 if (tn->tn_strg->st_tspec == CHAR) { 3513 (void)printf("\"%s\"", tn->tn_strg->st_cp); 3514 } else { 3515 char *s; 3516 size_t n; 3517 n = MB_CUR_MAX * (tn->tn_strg->st_len + 1); 3518 s = xmalloc(n); 3519 (void)wcstombs(s, tn->tn_strg->st_wcp, n); 3520 (void)printf("L\"%s\"", s); 3521 free(s); 3522 } 3523 (void)printf(" "); 3524 } else if (tn->tn_op == FSEL) { 3525 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs, 3526 tn->tn_type->t_flen); 3527 } 3528 (void)printf("%s\n", ttos(tn->tn_type)); 3529 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING) 3530 return; 3531 displexpr(tn->tn_left, offs + 2); 3532 if (modtab[tn->tn_op].m_binary || 3533 (tn->tn_op == PUSH && tn->tn_right != NULL)) { 3534 displexpr(tn->tn_right, offs + 2); 3535 } 3536 } 3537 3538 /* 3539 * Called by expr() to recursively perform some tests. 3540 */ 3541 /* ARGSUSED */ 3542 void 3543 chkmisc(tnode_t *tn, int vctx, int tctx, int eqwarn, int fcall, int rvdisc, 3544 int szof) 3545 { 3546 tnode_t *ln, *rn; 3547 mod_t *mp; 3548 int nrvdisc, cvctx, ctctx; 3549 op_t op; 3550 scl_t sc; 3551 dinfo_t *di; 3552 3553 if (tn == NULL) 3554 return; 3555 3556 ln = tn->tn_left; 3557 rn = tn->tn_right; 3558 mp = &modtab[op = tn->tn_op]; 3559 3560 switch (op) { 3561 case AMPER: 3562 if (ln->tn_op == NAME && (reached || rchflg)) { 3563 if (!szof) 3564 setsflg(ln->tn_sym); 3565 setuflg(ln->tn_sym, fcall, szof); 3566 } 3567 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3568 /* check the range of array indices */ 3569 chkaidx(ln->tn_left, 1); 3570 break; 3571 case LOAD: 3572 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3573 /* check the range of array indices */ 3574 chkaidx(ln->tn_left, 0); 3575 /* FALLTHROUGH */ 3576 case PUSH: 3577 case INCBEF: 3578 case DECBEF: 3579 case INCAFT: 3580 case DECAFT: 3581 case ADDASS: 3582 case SUBASS: 3583 case MULASS: 3584 case DIVASS: 3585 case MODASS: 3586 case ANDASS: 3587 case ORASS: 3588 case XORASS: 3589 case SHLASS: 3590 case SHRASS: 3591 case REAL: 3592 case IMAG: 3593 if (ln->tn_op == NAME && (reached || rchflg)) { 3594 sc = ln->tn_sym->s_scl; 3595 /* 3596 * Look if there was a asm statement in one of the 3597 * compound statements we are in. If not, we don't 3598 * print a warning. 3599 */ 3600 for (di = dcs; di != NULL; di = di->d_nxt) { 3601 if (di->d_asm) 3602 break; 3603 } 3604 if (sc != EXTERN && sc != STATIC && 3605 !ln->tn_sym->s_set && !szof && di == NULL) { 3606 /* %s may be used before set */ 3607 warning(158, ln->tn_sym->s_name); 3608 setsflg(ln->tn_sym); 3609 } 3610 setuflg(ln->tn_sym, 0, 0); 3611 } 3612 break; 3613 case ASSIGN: 3614 if (ln->tn_op == NAME && !szof && (reached || rchflg)) { 3615 setsflg(ln->tn_sym); 3616 if (ln->tn_sym->s_scl == EXTERN) 3617 outusg(ln->tn_sym); 3618 } 3619 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3620 /* check the range of array indices */ 3621 chkaidx(ln->tn_left, 0); 3622 break; 3623 case CALL: 3624 if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME) 3625 LERROR("chkmisc(op=%s != %s || %s != %s)", 3626 getopname(ln->tn_op), getopname(AMPER), 3627 getopname(ln->tn_left->tn_op), getopname(NAME)); 3628 if (!szof) 3629 outcall(tn, vctx || tctx, rvdisc); 3630 break; 3631 case EQ: 3632 /* equality operator "==" found where "=" was exp. */ 3633 if (hflag && eqwarn) 3634 warning(160); 3635 break; 3636 case CON: 3637 case NAME: 3638 case STRING: 3639 return; 3640 /* LINTED206: (enumeration values not handled in switch) */ 3641 case OR: 3642 case XOR: 3643 case NE: 3644 case GE: 3645 case GT: 3646 case LE: 3647 case LT: 3648 case SHR: 3649 case SHL: 3650 case MINUS: 3651 case PLUS: 3652 case MOD: 3653 case DIV: 3654 case MULT: 3655 case STAR: 3656 case UMINUS: 3657 case UPLUS: 3658 case DEC: 3659 case INC: 3660 case COMPL: 3661 case NOT: 3662 case POINT: 3663 case ARROW: 3664 case NOOP: 3665 case AND: 3666 case FARG: 3667 case CASE: 3668 case INIT: 3669 case RETURN: 3670 case ICALL: 3671 case CVT: 3672 case COMMA: 3673 case FSEL: 3674 case COLON: 3675 case QUEST: 3676 case LOGOR: 3677 case LOGAND: 3678 break; 3679 } 3680 3681 cvctx = mp->m_vctx; 3682 ctctx = mp->m_tctx; 3683 /* 3684 * values of operands of ':' are not used if the type of at least 3685 * one of the operands (for gcc compatibility) is void 3686 * XXX test/value context of QUEST should probably be used as 3687 * context for both operands of COLON 3688 */ 3689 if (op == COLON && tn->tn_type->t_tspec == VOID) 3690 cvctx = ctctx = 0; 3691 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID; 3692 chkmisc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof); 3693 3694 switch (op) { 3695 case PUSH: 3696 if (rn != NULL) 3697 chkmisc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof); 3698 break; 3699 case LOGAND: 3700 case LOGOR: 3701 chkmisc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof); 3702 break; 3703 case COLON: 3704 chkmisc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof); 3705 break; 3706 case COMMA: 3707 chkmisc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof); 3708 break; 3709 default: 3710 if (mp->m_binary) 3711 chkmisc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof); 3712 break; 3713 } 3714 3715 } 3716 3717 /* 3718 * Checks the range of array indices, if possible. 3719 * amper is set if only the address of the element is used. This 3720 * means that the index is allowd to refere to the first element 3721 * after the array. 3722 */ 3723 static void 3724 chkaidx(tnode_t *tn, int amper) 3725 { 3726 int dim; 3727 tnode_t *ln, *rn; 3728 int elsz; 3729 int64_t con; 3730 3731 ln = tn->tn_left; 3732 rn = tn->tn_right; 3733 3734 /* We can only check constant indices. */ 3735 if (rn->tn_op != CON) 3736 return; 3737 3738 /* Return if the left node does not stem from an array. */ 3739 if (ln->tn_op != AMPER) 3740 return; 3741 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME) 3742 return; 3743 if (ln->tn_left->tn_type->t_tspec != ARRAY) 3744 return; 3745 3746 /* 3747 * For incomplete array types, we can print a warning only if 3748 * the index is negative. 3749 */ 3750 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0) 3751 return; 3752 3753 /* Get the size of one array element */ 3754 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0) 3755 return; 3756 elsz /= CHAR_BIT; 3757 3758 /* Change the unit of the index from bytes to element size. */ 3759 if (isutyp(rn->tn_type->t_tspec)) { 3760 con = (uint64_t)rn->tn_val->v_quad / elsz; 3761 } else { 3762 con = rn->tn_val->v_quad / elsz; 3763 } 3764 3765 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0); 3766 3767 if (!isutyp(rn->tn_type->t_tspec) && con < 0) { 3768 /* array subscript cannot be negative: %ld */ 3769 warning(167, (long)con); 3770 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) { 3771 /* array subscript cannot be > %d: %ld */ 3772 warning(168, dim - 1, (long)con); 3773 } 3774 } 3775 3776 /* 3777 * Check for ordered comparisons of unsigned values with 0. 3778 */ 3779 static void 3780 chkcomp(op_t op, tnode_t *ln, tnode_t *rn) 3781 { 3782 char buf[64]; 3783 tspec_t lt, rt; 3784 mod_t *mp; 3785 3786 lt = ln->tn_type->t_tspec; 3787 rt = rn->tn_type->t_tspec; 3788 mp = &modtab[op]; 3789 3790 if (ln->tn_op != CON && rn->tn_op != CON) 3791 return; 3792 3793 if (!isityp(lt) || !isityp(rt)) 3794 return; 3795 3796 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON && 3797 (rn->tn_val->v_quad < 0 || 3798 rn->tn_val->v_quad > (int)~(~0U << (CHAR_BIT - 1)))) { 3799 /* nonportable character comparison, op %s */ 3800 warning(230, mp->m_name); 3801 return; 3802 } 3803 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON && 3804 (ln->tn_val->v_quad < 0 || 3805 ln->tn_val->v_quad > (int)~(~0U << (CHAR_BIT - 1)))) { 3806 /* nonportable character comparison, op %s */ 3807 warning(230, mp->m_name); 3808 return; 3809 } 3810 if (isutyp(lt) && !isutyp(rt) && 3811 rn->tn_op == CON && rn->tn_val->v_quad <= 0) { 3812 if (rn->tn_val->v_quad < 0) { 3813 /* comparison of %s with %s, op %s */ 3814 warning(162, tyname(buf, sizeof(buf), ln->tn_type), 3815 "negative constant", mp->m_name); 3816 } else if (op == LT || op == GE || (hflag && op == LE)) { 3817 /* comparison of %s with %s, op %s */ 3818 warning(162, tyname(buf, sizeof(buf), ln->tn_type), 3819 "0", mp->m_name); 3820 } 3821 return; 3822 } 3823 if (isutyp(rt) && !isutyp(lt) && 3824 ln->tn_op == CON && ln->tn_val->v_quad <= 0) { 3825 if (ln->tn_val->v_quad < 0) { 3826 /* comparison of %s with %s, op %s */ 3827 warning(162, "negative constant", 3828 tyname(buf, sizeof(buf), rn->tn_type), mp->m_name); 3829 } else if (op == GT || op == LE || (hflag && op == GE)) { 3830 /* comparison of %s with %s, op %s */ 3831 warning(162, "0", tyname(buf, sizeof(buf), rn->tn_type), 3832 mp->m_name); 3833 } 3834 return; 3835 } 3836 } 3837 3838 /* 3839 * Takes an expression an returns 0 if this expression can be used 3840 * for static initialisation, otherwise -1. 3841 * 3842 * Constant initialisation expressions must be constant or an address 3843 * of a static object with an optional offset. In the first case, 3844 * the result is returned in *offsp. In the second case, the static 3845 * object is returned in *symp and the offset in *offsp. 3846 * 3847 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and 3848 * CON. Type conversions are allowed if they do not change binary 3849 * representation (including width). 3850 */ 3851 int 3852 conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp) 3853 { 3854 sym_t *sym; 3855 ptrdiff_t offs1, offs2; 3856 tspec_t t, ot; 3857 3858 switch (tn->tn_op) { 3859 case MINUS: 3860 if (tn->tn_right->tn_op == CVT) 3861 return conaddr(tn->tn_right, symp, offsp); 3862 else if (tn->tn_right->tn_op != CON) 3863 return (-1); 3864 /* FALLTHROUGH */ 3865 case PLUS: 3866 offs1 = offs2 = 0; 3867 if (tn->tn_left->tn_op == CON) { 3868 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad; 3869 if (conaddr(tn->tn_right, &sym, &offs2) == -1) 3870 return (-1); 3871 } else if (tn->tn_right->tn_op == CON) { 3872 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad; 3873 if (tn->tn_op == MINUS) 3874 offs2 = -offs2; 3875 if (conaddr(tn->tn_left, &sym, &offs1) == -1) 3876 return (-1); 3877 } else { 3878 return (-1); 3879 } 3880 *symp = sym; 3881 *offsp = offs1 + offs2; 3882 break; 3883 case AMPER: 3884 if (tn->tn_left->tn_op == NAME) { 3885 *symp = tn->tn_left->tn_sym; 3886 *offsp = 0; 3887 } else if (tn->tn_left->tn_op == STRING) { 3888 /* 3889 * If this would be the front end of a compiler we 3890 * would return a label instead of 0. 3891 */ 3892 *offsp = 0; 3893 } 3894 break; 3895 case CVT: 3896 t = tn->tn_type->t_tspec; 3897 ot = tn->tn_left->tn_type->t_tspec; 3898 if ((!isityp(t) && t != PTR) || (!isityp(ot) && ot != PTR)) 3899 return (-1); 3900 #ifdef notdef 3901 /* 3902 * consider: 3903 * struct foo { 3904 * unsigned char a; 3905 * } f = { 3906 * (u_char)(u_long)(&(((struct foo *)0)->a)) 3907 * }; 3908 * since psize(u_long) != psize(u_char) this fails. 3909 */ 3910 else if (psize(t) != psize(ot)) 3911 return (-1); 3912 #endif 3913 if (conaddr(tn->tn_left, symp, offsp) == -1) 3914 return (-1); 3915 break; 3916 default: 3917 return (-1); 3918 } 3919 return (0); 3920 } 3921 3922 /* 3923 * Concatenate two string constants. 3924 */ 3925 strg_t * 3926 catstrg(strg_t *strg1, strg_t *strg2) 3927 { 3928 size_t len1, len2, len; 3929 3930 if (strg1->st_tspec != strg2->st_tspec) { 3931 /* cannot concatenate wide and regular string literals */ 3932 error(292); 3933 return (strg1); 3934 } 3935 3936 len1 = strg1->st_len; 3937 len2 = strg2->st_len + 1; /* + NUL */ 3938 len = len1 + len2; 3939 3940 #define COPY(F) \ 3941 do { \ 3942 strg1->F = xrealloc(strg1->F, len * sizeof(*strg1->F)); \ 3943 (void)memcpy(strg1->F + len1, strg2->F, len2 * sizeof(*strg1->F)); \ 3944 free(strg2->F); \ 3945 } while (/*CONSTCOND*/0) 3946 3947 if (strg1->st_tspec == CHAR) 3948 COPY(st_cp); 3949 else 3950 COPY(st_wcp); 3951 3952 strg1->st_len = len - 1; /* - NUL */; 3953 free(strg2); 3954 3955 return (strg1); 3956 } 3957 3958 /* 3959 * Print a warning if the given node has operands which should be 3960 * parenthesized. 3961 * 3962 * XXX Does not work if an operand is a constant expression. Constant 3963 * expressions are already folded. 3964 */ 3965 static void 3966 precconf(tnode_t *tn) 3967 { 3968 tnode_t *ln, *rn; 3969 op_t lop, rop = NOOP; 3970 int lparn, rparn = 0; 3971 mod_t *mp; 3972 int dowarn; 3973 3974 if (!hflag) 3975 return; 3976 3977 mp = &modtab[tn->tn_op]; 3978 3979 lparn = 0; 3980 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left) 3981 lparn |= ln->tn_parn; 3982 lparn |= ln->tn_parn; 3983 lop = ln->tn_op; 3984 3985 if (mp->m_binary) { 3986 rparn = 0; 3987 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left) 3988 rparn |= rn->tn_parn; 3989 rparn |= rn->tn_parn; 3990 rop = rn->tn_op; 3991 } 3992 3993 dowarn = 0; 3994 3995 switch (tn->tn_op) { 3996 case SHL: 3997 case SHR: 3998 if (!lparn && (lop == PLUS || lop == MINUS)) { 3999 dowarn = 1; 4000 } else if (!rparn && (rop == PLUS || rop == MINUS)) { 4001 dowarn = 1; 4002 } 4003 break; 4004 case LOGOR: 4005 if (!lparn && lop == LOGAND) { 4006 dowarn = 1; 4007 } else if (!rparn && rop == LOGAND) { 4008 dowarn = 1; 4009 } 4010 break; 4011 case AND: 4012 case XOR: 4013 case OR: 4014 if (!lparn && lop != tn->tn_op) { 4015 if (lop == PLUS || lop == MINUS) { 4016 dowarn = 1; 4017 } else if (lop == AND || lop == XOR) { 4018 dowarn = 1; 4019 } 4020 } 4021 if (!dowarn && !rparn && rop != tn->tn_op) { 4022 if (rop == PLUS || rop == MINUS) { 4023 dowarn = 1; 4024 } else if (rop == AND || rop == XOR) { 4025 dowarn = 1; 4026 } 4027 } 4028 break; 4029 /* LINTED206: (enumeration values not handled in switch) */ 4030 case DECAFT: 4031 case XORASS: 4032 case SHLASS: 4033 case NOOP: 4034 case ARROW: 4035 case ORASS: 4036 case POINT: 4037 case NAME: 4038 case NOT: 4039 case COMPL: 4040 case CON: 4041 case INC: 4042 case STRING: 4043 case DEC: 4044 case INCBEF: 4045 case DECBEF: 4046 case INCAFT: 4047 case FSEL: 4048 case CALL: 4049 case COMMA: 4050 case CVT: 4051 case ICALL: 4052 case LOAD: 4053 case PUSH: 4054 case RETURN: 4055 case INIT: 4056 case CASE: 4057 case FARG: 4058 case SUBASS: 4059 case ADDASS: 4060 case MODASS: 4061 case DIVASS: 4062 case MULASS: 4063 case ASSIGN: 4064 case COLON: 4065 case QUEST: 4066 case LOGAND: 4067 case NE: 4068 case EQ: 4069 case GE: 4070 case GT: 4071 case LE: 4072 case LT: 4073 case MINUS: 4074 case PLUS: 4075 case MOD: 4076 case DIV: 4077 case MULT: 4078 case AMPER: 4079 case STAR: 4080 case UMINUS: 4081 case SHRASS: 4082 case UPLUS: 4083 case ANDASS: 4084 case REAL: 4085 case IMAG: 4086 break; 4087 } 4088 4089 if (dowarn) { 4090 /* precedence confusion possible: parenthesize! */ 4091 warning(169); 4092 } 4093 4094 } 4095