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