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