1 /* $NetBSD: tree.c,v 1.506 2023/02/24 19:18:50 rillig 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) 40 __RCSID("$NetBSD: tree.c,v 1.506 2023/02/24 19:18:50 rillig Exp $"); 41 #endif 42 43 #include <float.h> 44 #include <limits.h> 45 #include <math.h> 46 #include <signal.h> 47 #include <stdlib.h> 48 #include <string.h> 49 50 #include "lint1.h" 51 52 53 typedef struct integer_constraints { 54 int64_t smin; /* signed minimum */ 55 int64_t smax; /* signed maximum */ 56 uint64_t umin; /* unsigned minimum */ 57 uint64_t umax; /* unsigned maximum */ 58 uint64_t bset; /* bits that are definitely set */ 59 uint64_t bclr; /* bits that are definitely clear */ 60 } integer_constraints; 61 62 63 extern sig_atomic_t fpe; 64 65 66 static uint64_t 67 u64_fill_right(uint64_t x) 68 { 69 x |= x >> 1; 70 x |= x >> 2; 71 x |= x >> 4; 72 x |= x >> 8; 73 x |= x >> 16; 74 x |= x >> 32; 75 return x; 76 } 77 78 static bool 79 str_endswith(const char *haystack, const char *needle) 80 { 81 size_t hlen = strlen(haystack); 82 size_t nlen = strlen(needle); 83 84 return nlen <= hlen && 85 memcmp(haystack + hlen - nlen, needle, nlen) == 0; 86 } 87 static const char * 88 op_name(op_t op) 89 { 90 return modtab[op].m_name; 91 } 92 93 static unsigned 94 width_in_bits(const type_t *tp) 95 { 96 97 lint_assert(is_integer(tp->t_tspec)); 98 return tp->t_bitfield ? tp->t_flen : size_in_bits(tp->t_tspec); 99 } 100 101 static bool 102 ic_maybe_signed(const type_t *tp, const integer_constraints *ic) 103 { 104 105 return !is_uinteger(tp->t_tspec) && 106 (ic->bclr & ((uint64_t)1 << 63)) == 0; 107 } 108 109 static integer_constraints 110 ic_any(const type_t *tp) 111 { 112 integer_constraints c; 113 114 uint64_t vbits = value_bits(width_in_bits(tp)); 115 if (is_uinteger(tp->t_tspec)) { 116 c.smin = INT64_MIN; 117 c.smax = INT64_MAX; 118 c.umin = 0; 119 c.umax = vbits; 120 c.bset = 0; 121 c.bclr = ~c.umax; 122 } else { 123 c.smin = (int64_t)-1 - (int64_t)(vbits >> 1); 124 c.smax = (int64_t)(vbits >> 1); 125 c.umin = 0; 126 c.umax = UINT64_MAX; 127 c.bset = 0; 128 c.bclr = 0; 129 } 130 return c; 131 } 132 133 static integer_constraints 134 ic_con(const type_t *tp, const val_t *v) 135 { 136 integer_constraints c; 137 138 lint_assert(is_integer(tp->t_tspec)); 139 int64_t s = v->v_quad; 140 uint64_t u = (uint64_t)s; 141 c.smin = s; 142 c.smax = s; 143 c.umin = u; 144 c.umax = u; 145 c.bset = u; 146 c.bclr = ~u; 147 return c; 148 } 149 150 static integer_constraints 151 ic_cvt(const type_t *ntp, const type_t *otp, integer_constraints a) 152 { 153 154 if (width_in_bits(ntp) > width_in_bits(otp) && 155 is_uinteger(otp->t_tspec)) 156 return a; 157 return ic_any(ntp); 158 } 159 160 static integer_constraints 161 ic_bitand(integer_constraints a, integer_constraints b) 162 { 163 integer_constraints c; 164 165 c.smin = INT64_MIN; 166 c.smax = INT64_MAX; 167 c.umin = 0; 168 c.umax = UINT64_MAX; 169 c.bset = a.bset & b.bset; 170 c.bclr = a.bclr | b.bclr; 171 return c; 172 } 173 174 static integer_constraints 175 ic_bitor(integer_constraints a, integer_constraints b) 176 { 177 integer_constraints c; 178 179 c.smin = INT64_MIN; 180 c.smax = INT64_MAX; 181 c.umin = 0; 182 c.umax = UINT64_MAX; 183 c.bset = a.bset | b.bset; 184 c.bclr = a.bclr & b.bclr; 185 return c; 186 } 187 188 static integer_constraints 189 ic_mod(const type_t *tp, integer_constraints a, integer_constraints b) 190 { 191 integer_constraints c; 192 193 if (ic_maybe_signed(tp, &a) || ic_maybe_signed(tp, &b)) 194 return ic_any(tp); 195 196 c.smin = INT64_MIN; 197 c.smax = INT64_MAX; 198 c.umin = 0; 199 c.umax = b.umax - 1; 200 c.bset = 0; 201 c.bclr = ~u64_fill_right(c.umax); 202 return c; 203 } 204 205 static integer_constraints 206 ic_shl(const type_t *tp, integer_constraints a, integer_constraints b) 207 { 208 integer_constraints c; 209 unsigned int amount; 210 211 if (ic_maybe_signed(tp, &a)) 212 return ic_any(tp); 213 214 if (b.smin == b.smax && b.smin >= 0 && b.smin < 64) 215 amount = (unsigned int)b.smin; 216 else if (b.umin == b.umax && b.umin < 64) 217 amount = (unsigned int)b.umin; 218 else 219 return ic_any(tp); 220 221 c.smin = INT64_MIN; 222 c.smax = INT64_MAX; 223 c.umin = 0; 224 c.umax = UINT64_MAX; 225 c.bset = a.bset << amount; 226 c.bclr = a.bclr << amount | (((uint64_t)1 << amount) - 1); 227 return c; 228 } 229 230 static integer_constraints 231 ic_shr(const type_t *tp, integer_constraints a, integer_constraints b) 232 { 233 integer_constraints c; 234 unsigned int amount; 235 236 if (ic_maybe_signed(tp, &a)) 237 return ic_any(tp); 238 239 if (b.smin == b.smax && b.smin >= 0 && b.smin < 64) 240 amount = (unsigned int)b.smin; 241 else if (b.umin == b.umax && b.umin < 64) 242 amount = (unsigned int)b.umin; 243 else 244 return ic_any(tp); 245 246 c.smin = INT64_MIN; 247 c.smax = INT64_MAX; 248 c.umin = 0; 249 c.umax = UINT64_MAX; 250 c.bset = a.bset >> amount; 251 c.bclr = a.bclr >> amount | ~(~(uint64_t)0 >> amount); 252 return c; 253 } 254 255 static integer_constraints 256 ic_expr(const tnode_t *tn) 257 { 258 integer_constraints lc, rc; 259 260 lint_assert(is_integer(tn->tn_type->t_tspec)); 261 262 switch (tn->tn_op) { 263 case CON: 264 return ic_con(tn->tn_type, tn->tn_val); 265 case CVT: 266 if (!is_integer(tn->tn_left->tn_type->t_tspec)) 267 return ic_any(tn->tn_type); 268 lc = ic_expr(tn->tn_left); 269 return ic_cvt(tn->tn_type, tn->tn_left->tn_type, lc); 270 case MOD: 271 lc = ic_expr(before_conversion(tn->tn_left)); 272 rc = ic_expr(before_conversion(tn->tn_right)); 273 return ic_mod(tn->tn_type, lc, rc); 274 case SHL: 275 lc = ic_expr(tn->tn_left); 276 rc = ic_expr(tn->tn_right); 277 return ic_shl(tn->tn_type, lc, rc); 278 case SHR: 279 lc = ic_expr(tn->tn_left); 280 rc = ic_expr(tn->tn_right); 281 return ic_shr(tn->tn_type, lc, rc); 282 case BITAND: 283 lc = ic_expr(tn->tn_left); 284 rc = ic_expr(tn->tn_right); 285 return ic_bitand(lc, rc); 286 case BITOR: 287 lc = ic_expr(tn->tn_left); 288 rc = ic_expr(tn->tn_right); 289 return ic_bitor(lc, rc); 290 default: 291 return ic_any(tn->tn_type); 292 } 293 } 294 295 /* Build 'pointer to tp', 'array of tp' or 'function returning tp'. */ 296 type_t * 297 block_derive_type(type_t *tp, tspec_t t) 298 { 299 type_t *tp2; 300 301 tp2 = block_zero_alloc(sizeof(*tp2)); 302 tp2->t_tspec = t; 303 tp2->t_subt = tp; 304 return tp2; 305 } 306 307 /* 308 * Derive 'pointer to tp' or 'function returning tp'. 309 * The memory is freed at the end of the current expression. 310 */ 311 type_t * 312 expr_derive_type(type_t *tp, tspec_t t) 313 { 314 type_t *tp2; 315 316 tp2 = expr_zero_alloc(sizeof(*tp2)); 317 tp2->t_tspec = t; 318 tp2->t_subt = tp; 319 return tp2; 320 } 321 322 /* 323 * Build and initialize a new node. 324 */ 325 static tnode_t * 326 new_tnode(op_t op, bool sys, type_t *type, tnode_t *ln, tnode_t *rn) 327 { 328 329 tnode_t *ntn = expr_alloc_tnode(); 330 ntn->tn_op = op; 331 ntn->tn_type = type; 332 ntn->tn_sys = sys; 333 ntn->tn_left = ln; 334 ntn->tn_right = rn; 335 336 if (op == INDIR || op == FSEL) { 337 lint_assert(ln->tn_type->t_tspec == PTR); 338 tspec_t t = ln->tn_type->t_subt->t_tspec; 339 if (t != FUNC && t != VOID) 340 ntn->tn_lvalue = true; 341 } 342 343 return ntn; 344 } 345 346 /* 347 * Create a node for a constant. 348 */ 349 tnode_t * 350 build_constant(type_t *tp, val_t *v) 351 { 352 tnode_t *n; 353 354 n = expr_alloc_tnode(); 355 n->tn_op = CON; 356 n->tn_type = tp; 357 n->tn_val = expr_zero_alloc(sizeof(*n->tn_val)); 358 n->tn_val->v_tspec = tp->t_tspec; 359 n->tn_val->v_unsigned_since_c90 = v->v_unsigned_since_c90; 360 n->tn_val->v_u = v->v_u; 361 free(v); 362 return n; 363 } 364 365 static tnode_t * 366 build_integer_constant(tspec_t t, int64_t q) 367 { 368 tnode_t *n; 369 370 n = expr_alloc_tnode(); 371 n->tn_op = CON; 372 n->tn_type = gettyp(t); 373 n->tn_val = expr_zero_alloc(sizeof(*n->tn_val)); 374 n->tn_val->v_tspec = t; 375 n->tn_val->v_quad = q; 376 return n; 377 } 378 379 static void 380 fallback_symbol(sym_t *sym) 381 { 382 383 if (Tflag && fallback_symbol_strict_bool(sym)) 384 return; 385 386 if (block_level > 0 && (strcmp(sym->s_name, "__FUNCTION__") == 0 || 387 strcmp(sym->s_name, "__PRETTY_FUNCTION__") == 0)) { 388 /* __FUNCTION__/__PRETTY_FUNCTION__ is a GCC extension */ 389 gnuism(316); 390 sym->s_type = block_derive_type(gettyp(CHAR), PTR); 391 sym->s_type->t_const = true; 392 return; 393 } 394 395 if (block_level > 0 && strcmp(sym->s_name, "__func__") == 0) { 396 if (!allow_c99) 397 /* __func__ is a C99 feature */ 398 warning(317); 399 /* C11 6.4.2.2 */ 400 sym->s_type = block_derive_type(gettyp(CHAR), ARRAY); 401 sym->s_type->t_const = true; 402 sym->s_type->t_dim = (int)strlen(funcsym->s_name) + 1; 403 return; 404 } 405 406 /* '%s' undefined */ 407 error(99, sym->s_name); 408 } 409 410 /* 411 * Functions that are predeclared by GCC or other compilers can be called 412 * with arbitrary arguments. Since lint usually runs after a successful 413 * compilation, it's the compiler's job to catch any errors. 414 */ 415 bool 416 is_compiler_builtin(const char *name) 417 { 418 /* https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html */ 419 if (allow_gcc) { 420 if (strncmp(name, "__atomic_", 9) == 0 || 421 strncmp(name, "__builtin_", 10) == 0 || 422 strcmp(name, "alloca") == 0 || 423 /* obsolete but still in use, as of 2021 */ 424 strncmp(name, "__sync_", 7) == 0) 425 return true; 426 } 427 428 /* https://software.intel.com/sites/landingpage/IntrinsicsGuide/ */ 429 if (strncmp(name, "_mm_", 4) == 0) 430 return true; 431 432 return false; 433 } 434 435 /* https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html */ 436 static bool 437 is_gcc_bool_builtin(const char *name) 438 { 439 return strncmp(name, "__builtin_", 10) == 0 && 440 (str_endswith(name, "_overflow") || 441 str_endswith(name, "_overflow_p")); 442 } 443 444 static void 445 build_name_call(sym_t *sym) 446 { 447 448 if (is_compiler_builtin(sym->s_name)) { 449 /* 450 * Do not warn about these, just assume that 451 * they are regular functions compatible with 452 * non-prototype calling conventions. 453 */ 454 if (allow_gcc && is_gcc_bool_builtin(sym->s_name)) 455 sym->s_type = gettyp(BOOL); 456 457 } else if (allow_c99) { 458 /* function '%s' implicitly declared to return int */ 459 error(215, sym->s_name); 460 } else if (!allow_trad) { 461 /* function '%s' implicitly declared to return int */ 462 warning(215, sym->s_name); 463 } 464 465 /* XXX if !allow_c90, the symbol should be exported to level 0 */ 466 sym->s_type = block_derive_type(sym->s_type, FUNC); 467 } 468 469 /* Create a node for a name (symbol table entry). */ 470 tnode_t * 471 build_name(sym_t *sym, bool is_funcname) 472 { 473 tnode_t *n; 474 475 if (sym->s_scl == NOSCL && !in_gcc_attribute) { 476 sym->s_scl = EXTERN; 477 sym->s_def = DECL; 478 if (is_funcname) 479 build_name_call(sym); 480 else 481 fallback_symbol(sym); 482 } 483 484 lint_assert(sym->s_kind == FVFT || sym->s_kind == FMEMBER); 485 486 n = expr_alloc_tnode(); 487 n->tn_type = sym->s_type; 488 if (sym->s_scl == BOOL_CONST) { 489 n->tn_op = CON; 490 n->tn_val = expr_zero_alloc(sizeof(*n->tn_val)); 491 n->tn_val->v_tspec = BOOL; 492 n->tn_val->v_quad = sym->u.s_bool_constant ? 1 : 0; 493 } else if (sym->s_scl == ENUM_CONST) { 494 n->tn_op = CON; 495 n->tn_val = expr_zero_alloc(sizeof(*n->tn_val)); 496 n->tn_val->v_tspec = INT; /* ENUM is in n->tn_type */ 497 n->tn_val->v_quad = sym->u.s_enum_constant; 498 } else { 499 n->tn_op = NAME; 500 n->tn_sym = sym; 501 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC) 502 n->tn_lvalue = true; 503 } 504 505 return n; 506 } 507 508 tnode_t * 509 build_string(strg_t *strg) 510 { 511 size_t len; 512 tnode_t *n; 513 type_t *tp; 514 515 len = strg->st_len; 516 517 n = expr_alloc_tnode(); 518 519 tp = expr_zero_alloc(sizeof(*tp)); 520 tp->t_tspec = ARRAY; 521 tp->t_subt = gettyp(strg->st_char ? CHAR : WCHAR); 522 tp->t_dim = (int)(len + 1); 523 524 n->tn_op = STRING; 525 n->tn_type = tp; 526 n->tn_lvalue = true; 527 528 n->tn_string = expr_zero_alloc(sizeof(*n->tn_string)); 529 n->tn_string->st_char = strg->st_char; 530 n->tn_string->st_len = len; 531 532 size_t chsize = strg->st_char ? sizeof(char) : sizeof(wchar_t); 533 size_t size = (len + 1) * chsize; 534 n->tn_string->st_mem = expr_zero_alloc(size); 535 (void)memcpy(n->tn_string->st_mem, strg->st_mem, size); 536 free(strg->st_mem); 537 free(strg); 538 539 return n; 540 } 541 542 tnode_t * 543 build_generic_selection(const tnode_t *expr, 544 struct generic_association *sel) 545 { 546 tnode_t *default_result = NULL; 547 548 for (; sel != NULL; sel = sel->ga_prev) { 549 if (expr != NULL && 550 types_compatible(sel->ga_arg, expr->tn_type, 551 false, false, NULL)) 552 return sel->ga_result; 553 else if (sel->ga_arg == NULL) 554 default_result = sel->ga_result; 555 } 556 return default_result; 557 } 558 559 static bool 560 is_out_of_char_range(const tnode_t *tn) 561 { 562 return tn->tn_op == CON && 563 !(0 <= tn->tn_val->v_quad && 564 tn->tn_val->v_quad < 1 << (CHAR_SIZE - 1)); 565 } 566 567 /* 568 * Check for ordered comparisons of unsigned values with 0. 569 */ 570 static void 571 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn) 572 { 573 tspec_t lt, rt; 574 575 lt = ln->tn_type->t_tspec; 576 rt = rn->tn_type->t_tspec; 577 578 if (ln->tn_op != CON && rn->tn_op != CON) 579 return; 580 581 if (!is_integer(lt) || !is_integer(rt)) 582 return; 583 584 if (hflag || pflag) { 585 if (lt == CHAR && is_out_of_char_range(rn)) { 586 char buf[128]; 587 (void)snprintf(buf, sizeof(buf), "%s %d", 588 op_name(op), (int)rn->tn_val->v_quad); 589 /* nonportable character comparison '%s' */ 590 warning(230, buf); 591 return; 592 } 593 if (rt == CHAR && is_out_of_char_range(ln)) { 594 char buf[128]; 595 (void)snprintf(buf, sizeof(buf), "%d %s ?", 596 (int)ln->tn_val->v_quad, op_name(op)); 597 /* nonportable character comparison '%s' */ 598 warning(230, buf); 599 return; 600 } 601 } 602 603 if (is_uinteger(lt) && !is_uinteger(rt) && 604 rn->tn_op == CON && rn->tn_val->v_quad <= 0) { 605 if (rn->tn_val->v_quad < 0) { 606 /* operator '%s' compares '%s' with '%s' */ 607 warning(162, op_name(op), 608 type_name(ln->tn_type), "negative constant"); 609 } else if (op == LT || op == GE) { 610 /* operator '%s' compares '%s' with '%s' */ 611 warning(162, op_name(op), type_name(ln->tn_type), "0"); 612 } 613 return; 614 } 615 if (is_uinteger(rt) && !is_uinteger(lt) && 616 ln->tn_op == CON && ln->tn_val->v_quad <= 0) { 617 if (ln->tn_val->v_quad < 0) { 618 /* operator '%s' compares '%s' with '%s' */ 619 warning(162, op_name(op), 620 "negative constant", type_name(rn->tn_type)); 621 } else if (op == GT || op == LE) { 622 /* operator '%s' compares '%s' with '%s' */ 623 warning(162, op_name(op), "0", type_name(rn->tn_type)); 624 } 625 return; 626 } 627 } 628 629 static const tspec_t arith_rank[] = { 630 LDOUBLE, DOUBLE, FLOAT, 631 #ifdef INT128_SIZE 632 UINT128, INT128, 633 #endif 634 UQUAD, QUAD, 635 ULONG, LONG, 636 UINT, INT, 637 }; 638 639 /* Keep unsigned in traditional C */ 640 static tspec_t 641 usual_arithmetic_conversion_trad(tspec_t lt, tspec_t rt) 642 { 643 644 size_t i; 645 for (i = 0; arith_rank[i] != INT; i++) 646 if (lt == arith_rank[i] || rt == arith_rank[i]) 647 break; 648 649 tspec_t t = arith_rank[i]; 650 if (is_uinteger(lt) || is_uinteger(rt)) 651 if (is_integer(t) && !is_uinteger(t)) 652 return unsigned_type(t); 653 return t; 654 } 655 656 static tspec_t 657 usual_arithmetic_conversion_c90(tspec_t lt, tspec_t rt) 658 { 659 660 if (lt == rt) 661 return lt; 662 663 if (lt == LCOMPLEX || rt == LCOMPLEX) 664 return LCOMPLEX; 665 if (lt == DCOMPLEX || rt == DCOMPLEX) 666 return DCOMPLEX; 667 if (lt == FCOMPLEX || rt == FCOMPLEX) 668 return FCOMPLEX; 669 if (lt == LDOUBLE || rt == LDOUBLE) 670 return LDOUBLE; 671 if (lt == DOUBLE || rt == DOUBLE) 672 return DOUBLE; 673 if (lt == FLOAT || rt == FLOAT) 674 return FLOAT; 675 676 /* 677 * If type A has more bits than type B, it should be able to hold all 678 * possible values of type B. 679 */ 680 if (size_in_bits(lt) > size_in_bits(rt)) 681 return lt; 682 if (size_in_bits(lt) < size_in_bits(rt)) 683 return rt; 684 685 size_t i; 686 for (i = 3; arith_rank[i] != INT; i++) 687 if (arith_rank[i] == lt || arith_rank[i] == rt) 688 break; 689 if ((is_uinteger(lt) || is_uinteger(rt)) && 690 !is_uinteger(arith_rank[i])) 691 i--; 692 return arith_rank[i]; 693 } 694 695 static tnode_t * 696 apply_usual_arithmetic_conversions(op_t op, tnode_t *tn, tspec_t t) 697 { 698 type_t *ntp = expr_dup_type(tn->tn_type); 699 ntp->t_tspec = t; 700 if (tn->tn_op != CON) { 701 /* usual arithmetic conversion for '%s' from '%s' to '%s' */ 702 query_message(4, op_name(op), 703 type_name(tn->tn_type), type_name(ntp)); 704 } 705 return convert(op, 0, ntp, tn); 706 } 707 708 /* 709 * Apply the "usual arithmetic conversions" (C99 6.3.1.8), which gives both 710 * operands the same type. 711 */ 712 static void 713 balance(op_t op, tnode_t **lnp, tnode_t **rnp) 714 { 715 716 tspec_t lt = (*lnp)->tn_type->t_tspec; 717 tspec_t rt = (*rnp)->tn_type->t_tspec; 718 if (!is_arithmetic(lt) || !is_arithmetic(rt)) 719 return; 720 721 tspec_t t = allow_c90 722 ? usual_arithmetic_conversion_c90(lt, rt) 723 : usual_arithmetic_conversion_trad(lt, rt); 724 725 if (t != lt) 726 *lnp = apply_usual_arithmetic_conversions(op, *lnp, t); 727 if (t != rt) 728 *rnp = apply_usual_arithmetic_conversions(op, *rnp, t); 729 } 730 731 /* 732 * Create a tree node for the unary & operator 733 */ 734 static tnode_t * 735 build_address(bool sys, tnode_t *tn, bool noign) 736 { 737 tspec_t t; 738 739 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) { 740 if (!allow_c90) 741 /* '&' before array or function: ignored */ 742 warning(127); 743 return tn; 744 } 745 746 /* eliminate &* */ 747 if (tn->tn_op == INDIR && 748 tn->tn_left->tn_type->t_tspec == PTR && 749 tn->tn_left->tn_type->t_subt == tn->tn_type) { 750 return tn->tn_left; 751 } 752 753 return new_tnode(ADDR, sys, expr_derive_type(tn->tn_type, PTR), 754 tn, NULL); 755 } 756 757 /* 758 * XXX 759 * Note: There appear to be a number of bugs in detecting overflow in 760 * this function. An audit and a set of proper regression tests are needed. 761 * --Perry Metzger, Nov. 16, 2001 762 */ 763 /* 764 * Do only as much as necessary to compute constant expressions. 765 * Called only if the operator allows folding and all operands are constants. 766 */ 767 static tnode_t * 768 fold(tnode_t *tn) 769 { 770 val_t *v; 771 tspec_t t; 772 bool utyp, ovfl; 773 int64_t sl, sr = 0, q = 0, mask; 774 uint64_t ul, ur = 0; 775 tnode_t *cn; 776 777 v = xcalloc(1, sizeof(*v)); 778 v->v_tspec = tn->tn_type->t_tspec; 779 780 t = tn->tn_left->tn_type->t_tspec; 781 utyp = !is_integer(t) || is_uinteger(t); 782 ul = sl = tn->tn_left->tn_val->v_quad; 783 if (is_binary(tn)) 784 ur = sr = tn->tn_right->tn_val->v_quad; 785 786 mask = value_bits(size_in_bits(t)); 787 ovfl = false; 788 789 switch (tn->tn_op) { 790 case UPLUS: 791 q = sl; 792 break; 793 case UMINUS: 794 q = sl == INT64_MIN ? sl : -sl; 795 if (sl != 0 && msb(q, t) == msb(sl, t)) 796 ovfl = true; 797 break; 798 case COMPL: 799 q = ~sl; 800 break; 801 case MULT: 802 if (utyp) { 803 q = ul * ur; 804 if (q != (q & mask)) 805 ovfl = true; 806 else if ((ul != 0) && ((q / ul) != ur)) 807 ovfl = true; 808 } else { 809 q = sl * sr; 810 if (msb(q, t) != (msb(sl, t) ^ msb(sr, t))) 811 ovfl = true; 812 } 813 break; 814 case DIV: 815 if (sr == 0) { 816 /* division by 0 */ 817 error(139); 818 q = utyp ? -1 : INT64_MAX; 819 } else { 820 q = utyp ? (int64_t)(ul / ur) : sl / sr; 821 } 822 break; 823 case MOD: 824 if (sr == 0) { 825 /* modulus by 0 */ 826 error(140); 827 q = 0; 828 } else { 829 q = utyp ? (int64_t)(ul % ur) : sl % sr; 830 } 831 break; 832 case PLUS: 833 q = utyp ? (int64_t)(ul + ur) : sl + sr; 834 if (msb(sl, t) && msb(sr, t) && !msb(q, t)) 835 ovfl = true; 836 if (!utyp && !msb(sl, t) && !msb(sr, t) && msb(q, t)) 837 ovfl = true; 838 break; 839 case MINUS: 840 q = utyp ? (int64_t)(ul - ur) : sl - sr; 841 if (!utyp && msb(sl, t) && !msb(sr, t) && !msb(q, t)) 842 ovfl = true; 843 if (!msb(sl, t) && msb(sr, t) && msb(q, t)) 844 ovfl = true; 845 break; 846 case SHL: 847 /* TODO: warn about out-of-bounds 'sr'. */ 848 /* TODO: warn about overflow in signed '<<'. */ 849 q = utyp ? (int64_t)(ul << (sr & 63)) : sl << (sr & 63); 850 break; 851 case SHR: 852 /* 853 * The sign must be explicitly extended because 854 * shifts of signed values are implementation dependent. 855 */ 856 /* TODO: warn about out-of-bounds 'sr'. */ 857 q = ul >> (sr & 63); 858 q = convert_integer(q, t, size_in_bits(t) - (int)sr); 859 break; 860 case LT: 861 q = (utyp ? ul < ur : sl < sr) ? 1 : 0; 862 break; 863 case LE: 864 q = (utyp ? ul <= ur : sl <= sr) ? 1 : 0; 865 break; 866 case GE: 867 q = (utyp ? ul >= ur : sl >= sr) ? 1 : 0; 868 break; 869 case GT: 870 q = (utyp ? ul > ur : sl > sr) ? 1 : 0; 871 break; 872 case EQ: 873 q = (utyp ? ul == ur : sl == sr) ? 1 : 0; 874 break; 875 case NE: 876 q = (utyp ? ul != ur : sl != sr) ? 1 : 0; 877 break; 878 case BITAND: 879 q = utyp ? (int64_t)(ul & ur) : sl & sr; 880 break; 881 case BITXOR: 882 q = utyp ? (int64_t)(ul ^ ur) : sl ^ sr; 883 break; 884 case BITOR: 885 q = utyp ? (int64_t)(ul | ur) : sl | sr; 886 break; 887 default: 888 lint_assert(/*CONSTCOND*/false); 889 } 890 891 /* XXX does not work for quads. */ 892 if (ovfl || 893 ((uint64_t)(q | mask) != ~(uint64_t)0 && (q & ~mask) != 0)) { 894 if (hflag) 895 /* integer overflow detected, op '%s' */ 896 warning(141, op_name(tn->tn_op)); 897 } 898 899 v->v_quad = convert_integer(q, t, 0); 900 901 cn = build_constant(tn->tn_type, v); 902 if (tn->tn_left->tn_system_dependent) 903 cn->tn_system_dependent = true; 904 if (is_binary(tn) && tn->tn_right->tn_system_dependent) 905 cn->tn_system_dependent = true; 906 907 return cn; 908 } 909 910 /* 911 * Create a new node for one of the operators POINT and ARROW. 912 */ 913 static tnode_t * 914 build_struct_access(op_t op, bool sys, tnode_t *ln, tnode_t *rn) 915 { 916 tnode_t *ntn, *ctn; 917 bool nolval; 918 919 lint_assert(rn->tn_op == NAME); 920 lint_assert(is_member(rn->tn_sym)); 921 922 /* 923 * Remember if the left operand is an lvalue (structure members 924 * are lvalues if and only if the structure itself is an lvalue). 925 */ 926 nolval = op == POINT && !ln->tn_lvalue; 927 928 if (op == POINT) { 929 ln = build_address(sys, ln, true); 930 } else if (ln->tn_type->t_tspec != PTR) { 931 lint_assert(!allow_c90); 932 lint_assert(is_integer(ln->tn_type->t_tspec)); 933 ln = convert(NOOP, 0, expr_derive_type(gettyp(VOID), PTR), ln); 934 } 935 936 ctn = build_integer_constant(PTRDIFF_TSPEC, 937 rn->tn_sym->u.s_member.sm_offset_in_bits / CHAR_SIZE); 938 939 ntn = new_tnode(PLUS, sys, expr_derive_type(rn->tn_type, PTR), 940 ln, ctn); 941 if (ln->tn_op == CON) 942 ntn = fold(ntn); 943 944 if (rn->tn_type->t_bitfield) { 945 ntn = new_tnode(FSEL, sys, ntn->tn_type->t_subt, ntn, NULL); 946 } else { 947 ntn = new_tnode(INDIR, sys, ntn->tn_type->t_subt, ntn, NULL); 948 } 949 950 if (nolval) 951 ntn->tn_lvalue = false; 952 953 return ntn; 954 } 955 956 /* 957 * Get the size in bytes of type tp->t_subt, as a constant expression of type 958 * ptrdiff_t as seen from the target platform. 959 */ 960 static tnode_t * 961 subt_size_in_bytes(type_t *tp) 962 { 963 int elem, elsz_in_bits; 964 965 lint_assert(tp->t_tspec == PTR); 966 tp = tp->t_subt; 967 968 elem = 1; 969 elsz_in_bits = 0; 970 971 while (tp->t_tspec == ARRAY) { 972 elem *= tp->t_dim; 973 tp = tp->t_subt; 974 } 975 976 switch (tp->t_tspec) { 977 case FUNC: 978 /* pointer to function is not allowed here */ 979 error(110); 980 break; 981 case VOID: 982 /* cannot do pointer arithmetic on operand of unknown size */ 983 gnuism(136); 984 break; 985 case STRUCT: 986 case UNION: 987 if ((elsz_in_bits = tp->t_str->sou_size_in_bits) == 0) 988 /* cannot do pointer arithmetic on operand of ... */ 989 error(136); 990 break; 991 case ENUM: 992 if (is_incomplete(tp)) { 993 /* cannot do pointer arithmetic on operand of ... */ 994 warning(136); 995 } 996 /* FALLTHROUGH */ 997 default: 998 if ((elsz_in_bits = size_in_bits(tp->t_tspec)) == 0) { 999 /* cannot do pointer arithmetic on operand of ... */ 1000 error(136); 1001 } else { 1002 lint_assert(elsz_in_bits != -1); 1003 } 1004 break; 1005 } 1006 1007 if (elem == 0 && elsz_in_bits != 0) { 1008 /* cannot do pointer arithmetic on operand of unknown size */ 1009 error(136); 1010 } 1011 1012 if (elsz_in_bits == 0) 1013 elsz_in_bits = CHAR_SIZE; 1014 1015 return build_integer_constant(PTRDIFF_TSPEC, 1016 (int64_t)(elem * elsz_in_bits / CHAR_SIZE)); 1017 } 1018 1019 /* 1020 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF. 1021 */ 1022 static tnode_t * 1023 build_prepost_incdec(op_t op, bool sys, tnode_t *ln) 1024 { 1025 tnode_t *cn, *ntn; 1026 1027 lint_assert(ln != NULL); 1028 1029 if (ln->tn_type->t_tspec == PTR) { 1030 cn = subt_size_in_bytes(ln->tn_type); 1031 } else { 1032 cn = build_integer_constant(INT, (int64_t)1); 1033 } 1034 ntn = new_tnode(op, sys, ln->tn_type, ln, cn); 1035 1036 return ntn; 1037 } 1038 1039 static void 1040 check_enum_array_index(const tnode_t *ln, const tnode_t *rn) 1041 { 1042 int max_array_index; 1043 int64_t max_enum_value; 1044 const struct sym *ec, *max_ec; 1045 const type_t *lt, *rt; 1046 1047 if (ln->tn_op != ADDR || ln->tn_left->tn_op != NAME) 1048 return; 1049 1050 lt = ln->tn_left->tn_type; 1051 if (lt->t_tspec != ARRAY || lt->t_incomplete_array) 1052 return; 1053 1054 if (rn->tn_op != CVT || !rn->tn_type->t_is_enum) 1055 return; 1056 if (rn->tn_left->tn_op != LOAD) 1057 return; 1058 1059 rt = rn->tn_left->tn_type; 1060 ec = rt->t_enum->en_first_enumerator; 1061 max_ec = ec; 1062 lint_assert(ec != NULL); 1063 for (ec = ec->s_next; ec != NULL; ec = ec->s_next) 1064 if (ec->u.s_enum_constant > max_ec->u.s_enum_constant) 1065 max_ec = ec; 1066 1067 max_enum_value = max_ec->u.s_enum_constant; 1068 lint_assert(INT_MIN <= max_enum_value && max_enum_value <= INT_MAX); 1069 1070 max_array_index = lt->t_dim - 1; 1071 if (max_enum_value == max_array_index) 1072 return; 1073 1074 /* 1075 * If the name of the largest enum constant contains 'MAX' or 'NUM', 1076 * that constant is typically not part of the allowed enum values but 1077 * a marker for the number of actual enum values. 1078 */ 1079 if (max_enum_value == max_array_index + 1 && 1080 (strstr(max_ec->s_name, "MAX") != NULL || 1081 strstr(max_ec->s_name, "max") != NULL || 1082 strstr(max_ec->s_name, "NUM") != NULL || 1083 strstr(max_ec->s_name, "num") != NULL)) 1084 return; 1085 1086 /* maximum value %d of '%s' does not match maximum array index %d */ 1087 warning(348, (int)max_enum_value, type_name(rt), max_array_index); 1088 print_previous_declaration(max_ec); 1089 } 1090 1091 /* 1092 * Create a node for operators PLUS and MINUS. 1093 */ 1094 static tnode_t * 1095 build_plus_minus(op_t op, bool sys, tnode_t *ln, tnode_t *rn) 1096 { 1097 1098 /* If pointer and integer, then pointer to the lhs. */ 1099 if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) { 1100 tnode_t *tmp = ln; 1101 ln = rn; 1102 rn = tmp; 1103 /* pointer addition has integer on the left-hand side */ 1104 query_message(5); 1105 } 1106 1107 /* pointer +- integer */ 1108 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) { 1109 lint_assert(is_integer(rn->tn_type->t_tspec)); 1110 1111 check_ctype_macro_invocation(ln, rn); 1112 check_enum_array_index(ln, rn); 1113 1114 tnode_t *elsz = subt_size_in_bytes(ln->tn_type); 1115 if (rn->tn_type->t_tspec != elsz->tn_type->t_tspec) 1116 rn = convert(NOOP, 0, elsz->tn_type, rn); 1117 1118 tnode_t *prod = new_tnode(MULT, sys, rn->tn_type, rn, elsz); 1119 if (rn->tn_op == CON) 1120 prod = fold(prod); 1121 1122 return new_tnode(op, sys, ln->tn_type, ln, prod); 1123 } 1124 1125 /* pointer - pointer */ 1126 if (rn->tn_type->t_tspec == PTR) { 1127 lint_assert(ln->tn_type->t_tspec == PTR); 1128 lint_assert(op == MINUS); 1129 1130 type_t *ptrdiff = gettyp(PTRDIFF_TSPEC); 1131 tnode_t *raw_diff = new_tnode(op, sys, ptrdiff, ln, rn); 1132 if (ln->tn_op == CON && rn->tn_op == CON) 1133 raw_diff = fold(raw_diff); 1134 1135 tnode_t *elsz = subt_size_in_bytes(ln->tn_type); 1136 balance(NOOP, &raw_diff, &elsz); 1137 1138 return new_tnode(DIV, sys, ptrdiff, raw_diff, elsz); 1139 } 1140 1141 return new_tnode(op, sys, ln->tn_type, ln, rn); 1142 } 1143 1144 /* 1145 * Create a node for operators SHL and SHR. 1146 */ 1147 static tnode_t * 1148 build_bit_shift(op_t op, bool sys, tnode_t *ln, tnode_t *rn) 1149 { 1150 1151 if (!allow_c90 && rn->tn_type->t_tspec != INT) 1152 rn = convert(NOOP, 0, gettyp(INT), rn); 1153 return new_tnode(op, sys, ln->tn_type, ln, rn); 1154 } 1155 1156 static bool 1157 is_null_pointer(const tnode_t *tn) 1158 { 1159 tspec_t t = tn->tn_type->t_tspec; 1160 1161 return ((t == PTR && tn->tn_type->t_subt->t_tspec == VOID) || 1162 is_integer(t)) 1163 && (tn->tn_op == CON && tn->tn_val->v_quad == 0); 1164 } 1165 1166 /* Return a type based on tp1, with added qualifiers from tp2. */ 1167 static type_t * 1168 merge_qualifiers(type_t *tp1, const type_t *tp2) 1169 { 1170 type_t *ntp, *nstp; 1171 bool c1, c2, v1, v2; 1172 1173 lint_assert(tp1->t_tspec == PTR); 1174 lint_assert(tp2->t_tspec == PTR); 1175 1176 c1 = tp1->t_subt->t_const; 1177 c2 = tp2->t_subt->t_const; 1178 v1 = tp1->t_subt->t_volatile; 1179 v2 = tp2->t_subt->t_volatile; 1180 1181 if (c1 == (c1 | c2) && v1 == (v1 | v2)) 1182 return tp1; 1183 1184 nstp = expr_dup_type(tp1->t_subt); 1185 nstp->t_const |= c2; 1186 nstp->t_volatile |= v2; 1187 1188 ntp = expr_dup_type(tp1); 1189 ntp->t_subt = nstp; 1190 return ntp; 1191 } 1192 1193 /* See C99 6.5.15 "Conditional operator". */ 1194 static tnode_t * 1195 build_colon(bool sys, tnode_t *ln, tnode_t *rn) 1196 { 1197 tspec_t lt, rt; 1198 type_t *tp; 1199 1200 lt = ln->tn_type->t_tspec; 1201 rt = rn->tn_type->t_tspec; 1202 1203 if (is_arithmetic(lt) && is_arithmetic(rt)) { 1204 /* The operands were already balanced in build_binary. */ 1205 tp = ln->tn_type; 1206 } else if (lt == BOOL && rt == BOOL) { 1207 tp = ln->tn_type; 1208 } else if (lt == VOID || rt == VOID) { 1209 tp = gettyp(VOID); 1210 } else if (is_struct_or_union(lt)) { 1211 /* Both types must be identical. */ 1212 lint_assert(is_struct_or_union(rt)); 1213 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str); 1214 if (is_incomplete(ln->tn_type)) { 1215 /* unknown operand size, op '%s' */ 1216 error(138, op_name(COLON)); 1217 return NULL; 1218 } 1219 tp = ln->tn_type; 1220 } else if (lt == PTR && is_integer(rt)) { 1221 if (rt != PTRDIFF_TSPEC) 1222 rn = convert(NOOP, 0, gettyp(PTRDIFF_TSPEC), rn); 1223 tp = ln->tn_type; 1224 } else if (rt == PTR && is_integer(lt)) { 1225 if (lt != PTRDIFF_TSPEC) 1226 ln = convert(NOOP, 0, gettyp(PTRDIFF_TSPEC), ln); 1227 tp = rn->tn_type; 1228 } else if (lt == PTR && is_null_pointer(rn)) { 1229 tp = merge_qualifiers(ln->tn_type, rn->tn_type); 1230 } else if (rt == PTR && is_null_pointer(ln)) { 1231 tp = merge_qualifiers(rn->tn_type, ln->tn_type); 1232 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) { 1233 tp = merge_qualifiers(ln->tn_type, rn->tn_type); 1234 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) { 1235 tp = merge_qualifiers(rn->tn_type, ln->tn_type); 1236 } else { 1237 /* 1238 * XXX For now we simply take the left type. This is 1239 * probably wrong, if one type contains a function prototype 1240 * and the other one, at the same place, only an old-style 1241 * declaration. 1242 */ 1243 tp = merge_qualifiers(ln->tn_type, rn->tn_type); 1244 } 1245 1246 return new_tnode(COLON, sys, tp, ln, rn); 1247 } 1248 1249 /* TODO: check for varargs */ 1250 static bool 1251 is_cast_redundant(const tnode_t *tn) 1252 { 1253 const type_t *ntp = tn->tn_type, *otp = tn->tn_left->tn_type; 1254 tspec_t nt = ntp->t_tspec, ot = otp->t_tspec; 1255 1256 if (nt == BOOL || ot == BOOL) 1257 return nt == BOOL && ot == BOOL; 1258 1259 if (is_integer(nt) && is_integer(ot)) { 1260 unsigned int nw = width_in_bits(ntp), ow = width_in_bits(otp); 1261 if (is_uinteger(nt) == is_uinteger(ot)) 1262 return nw >= ow; 1263 return is_uinteger(ot) && nw > ow; 1264 } 1265 1266 if (is_complex(nt) || is_complex(ot)) 1267 return is_complex(nt) && is_complex(ot) && 1268 size_in_bits(nt) >= size_in_bits(ot); 1269 1270 if (is_floating(nt) && is_floating(ot)) 1271 return size_in_bits(nt) >= size_in_bits(ot); 1272 1273 if (nt == PTR && ot == PTR) { 1274 if (!ntp->t_subt->t_const && otp->t_subt->t_const) 1275 return false; 1276 if (!ntp->t_subt->t_volatile && otp->t_subt->t_volatile) 1277 return false; 1278 1279 if (ntp->t_subt->t_tspec == VOID || 1280 otp->t_subt->t_tspec == VOID || 1281 types_compatible(ntp->t_subt, otp->t_subt, 1282 false, false, NULL)) 1283 return true; 1284 } 1285 1286 return false; 1287 } 1288 1289 /* 1290 * Create a node for an assignment operator (both = and op= ). 1291 */ 1292 static tnode_t * 1293 build_assignment(op_t op, bool sys, tnode_t *ln, tnode_t *rn) 1294 { 1295 tspec_t lt, rt; 1296 tnode_t *ntn, *ctn; 1297 1298 lint_assert(ln != NULL); 1299 lint_assert(rn != NULL); 1300 1301 lt = ln->tn_type->t_tspec; 1302 rt = rn->tn_type->t_tspec; 1303 1304 if ((op == ADDASS || op == SUBASS) && lt == PTR) { 1305 lint_assert(is_integer(rt)); 1306 ctn = subt_size_in_bytes(ln->tn_type); 1307 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec) 1308 rn = convert(NOOP, 0, ctn->tn_type, rn); 1309 rn = new_tnode(MULT, sys, rn->tn_type, rn, ctn); 1310 if (rn->tn_left->tn_op == CON) 1311 rn = fold(rn); 1312 } 1313 1314 if ((op == ASSIGN || op == RETURN || op == INIT) && 1315 (lt == STRUCT || rt == STRUCT)) { 1316 lint_assert(lt == rt); 1317 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str); 1318 if (is_incomplete(ln->tn_type)) { 1319 if (op == RETURN) { 1320 /* cannot return incomplete type */ 1321 error(212); 1322 } else { 1323 /* unknown operand size, op '%s' */ 1324 error(138, op_name(op)); 1325 } 1326 return NULL; 1327 } 1328 } 1329 1330 if (op == SHLASS) { 1331 if (portable_size_in_bits(lt) < portable_size_in_bits(rt)) { 1332 if (hflag) 1333 /* semantics of '%s' change in ANSI C; ... */ 1334 warning(118, "<<="); 1335 } 1336 } else if (op != SHRASS) { 1337 if (op == ASSIGN || lt != PTR) { 1338 if (lt != rt || 1339 (ln->tn_type->t_bitfield && rn->tn_op == CON)) { 1340 rn = convert(op, 0, ln->tn_type, rn); 1341 rt = lt; 1342 } 1343 } 1344 } 1345 1346 if (any_query_enabled && rn->tn_op == CVT && rn->tn_cast && 1347 types_compatible(ln->tn_type, rn->tn_type, false, false, NULL) && 1348 is_cast_redundant(rn)) { 1349 /* redundant cast from '%s' to '%s' before assignment */ 1350 query_message(7, 1351 type_name(rn->tn_left->tn_type), type_name(rn->tn_type)); 1352 } 1353 1354 ntn = new_tnode(op, sys, ln->tn_type, ln, rn); 1355 1356 return ntn; 1357 } 1358 1359 /* 1360 * Create a node for REAL, IMAG 1361 */ 1362 static tnode_t * 1363 build_real_imag(op_t op, bool sys, tnode_t *ln) 1364 { 1365 tnode_t *cn, *ntn; 1366 1367 lint_assert(ln != NULL); 1368 1369 if (ln->tn_op == NAME) { 1370 /* 1371 * This may be too much, but it avoids wrong warnings. 1372 * See d_c99_complex_split.c. 1373 */ 1374 mark_as_used(ln->tn_sym, false, false); 1375 mark_as_set(ln->tn_sym); 1376 } 1377 1378 switch (ln->tn_type->t_tspec) { 1379 case LCOMPLEX: 1380 /* XXX: integer and LDOUBLE don't match. */ 1381 cn = build_integer_constant(LDOUBLE, (int64_t)1); 1382 break; 1383 case DCOMPLEX: 1384 /* XXX: integer and DOUBLE don't match. */ 1385 cn = build_integer_constant(DOUBLE, (int64_t)1); 1386 break; 1387 case FCOMPLEX: 1388 /* XXX: integer and FLOAT don't match. */ 1389 cn = build_integer_constant(FLOAT, (int64_t)1); 1390 break; 1391 default: 1392 /* '__%s__' is illegal for type '%s' */ 1393 error(276, op == REAL ? "real" : "imag", 1394 type_name(ln->tn_type)); 1395 return NULL; 1396 } 1397 ntn = new_tnode(op, sys, cn->tn_type, ln, cn); 1398 ntn->tn_lvalue = true; 1399 1400 return ntn; 1401 } 1402 1403 static bool 1404 is_confusing_precedence(op_t op, op_t lop, bool lparen, op_t rop, bool rparen) 1405 { 1406 1407 if (op == SHL || op == SHR) { 1408 if (!lparen && (lop == PLUS || lop == MINUS)) 1409 return true; 1410 if (!rparen && (rop == PLUS || rop == MINUS)) 1411 return true; 1412 return false; 1413 } 1414 1415 if (op == LOGOR) { 1416 if (!lparen && lop == LOGAND) 1417 return true; 1418 if (!rparen && rop == LOGAND) 1419 return true; 1420 return false; 1421 } 1422 1423 lint_assert(op == BITAND || op == BITXOR || op == BITOR); 1424 if (!lparen && lop != op) { 1425 if (lop == PLUS || lop == MINUS) 1426 return true; 1427 if (lop == BITAND || lop == BITXOR) 1428 return true; 1429 } 1430 if (!rparen && rop != op) { 1431 if (rop == PLUS || rop == MINUS) 1432 return true; 1433 if (rop == BITAND || rop == BITXOR) 1434 return true; 1435 } 1436 return false; 1437 } 1438 1439 /* 1440 * Print a warning if the given node has operands which should be 1441 * parenthesized. 1442 * 1443 * XXX Does not work if an operand is a constant expression. Constant 1444 * expressions are already folded. 1445 */ 1446 static void 1447 check_precedence_confusion(tnode_t *tn) 1448 { 1449 tnode_t *ln, *rn; 1450 1451 if (!hflag) 1452 return; 1453 1454 debug_node(tn); 1455 1456 lint_assert(is_binary(tn)); 1457 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left) 1458 continue; 1459 for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left) 1460 continue; 1461 1462 if (is_confusing_precedence(tn->tn_op, 1463 ln->tn_op, ln->tn_parenthesized, 1464 rn->tn_op, rn->tn_parenthesized)) { 1465 /* precedence confusion possible: parenthesize! */ 1466 warning(169); 1467 } 1468 } 1469 1470 /* 1471 * Fold constant nodes, as much as is needed for comparing the value with 0. 1472 */ 1473 static tnode_t * 1474 fold_bool(tnode_t *tn) 1475 { 1476 bool l, r; 1477 val_t *v; 1478 1479 v = xcalloc(1, sizeof(*v)); 1480 v->v_tspec = tn->tn_type->t_tspec; 1481 lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL)); 1482 1483 l = constant_is_nonzero(tn->tn_left); 1484 r = is_binary(tn) && constant_is_nonzero(tn->tn_right); 1485 1486 switch (tn->tn_op) { 1487 case NOT: 1488 if (hflag && !constcond_flag) 1489 /* constant argument to '!' */ 1490 warning(239); 1491 v->v_quad = !l ? 1 : 0; 1492 break; 1493 case LOGAND: 1494 v->v_quad = l && r ? 1 : 0; 1495 break; 1496 case LOGOR: 1497 v->v_quad = l || r ? 1 : 0; 1498 break; 1499 default: 1500 lint_assert(/*CONSTCOND*/false); 1501 } 1502 1503 return build_constant(tn->tn_type, v); 1504 } 1505 1506 static ldbl_t 1507 floating_error_value(tspec_t t, ldbl_t lv) 1508 { 1509 if (t == FLOAT) { 1510 return lv < 0 ? -FLT_MAX : FLT_MAX; 1511 } else if (t == DOUBLE) { 1512 return lv < 0 ? -DBL_MAX : DBL_MAX; 1513 } else { 1514 /* LINTED 248: floating-point constant out of range */ 1515 ldbl_t max = LDBL_MAX; 1516 return lv < 0 ? -max : max; 1517 } 1518 } 1519 1520 /* 1521 * Fold constant nodes having operands with floating point type. 1522 */ 1523 static tnode_t * 1524 fold_float(tnode_t *tn) 1525 { 1526 val_t *v; 1527 tspec_t t; 1528 ldbl_t lv, rv = 0; 1529 1530 fpe = 0; 1531 v = xcalloc(1, sizeof(*v)); 1532 v->v_tspec = t = tn->tn_type->t_tspec; 1533 1534 lint_assert(is_floating(t)); 1535 lint_assert(t == tn->tn_left->tn_type->t_tspec); 1536 lint_assert(!is_binary(tn) || t == tn->tn_right->tn_type->t_tspec); 1537 1538 lv = tn->tn_left->tn_val->v_ldbl; 1539 if (is_binary(tn)) 1540 rv = tn->tn_right->tn_val->v_ldbl; 1541 1542 switch (tn->tn_op) { 1543 case UPLUS: 1544 v->v_ldbl = lv; 1545 break; 1546 case UMINUS: 1547 v->v_ldbl = -lv; 1548 break; 1549 case MULT: 1550 v->v_ldbl = lv * rv; 1551 break; 1552 case DIV: 1553 if (rv == 0.0) { 1554 /* division by 0 */ 1555 error(139); 1556 v->v_ldbl = floating_error_value(t, lv); 1557 } else { 1558 v->v_ldbl = lv / rv; 1559 } 1560 break; 1561 case PLUS: 1562 v->v_ldbl = lv + rv; 1563 break; 1564 case MINUS: 1565 v->v_ldbl = lv - rv; 1566 break; 1567 case LT: 1568 v->v_quad = lv < rv ? 1 : 0; 1569 break; 1570 case LE: 1571 v->v_quad = lv <= rv ? 1 : 0; 1572 break; 1573 case GE: 1574 v->v_quad = lv >= rv ? 1 : 0; 1575 break; 1576 case GT: 1577 v->v_quad = lv > rv ? 1 : 0; 1578 break; 1579 case EQ: 1580 v->v_quad = lv == rv ? 1 : 0; 1581 break; 1582 case NE: 1583 v->v_quad = lv != rv ? 1 : 0; 1584 break; 1585 default: 1586 lint_assert(/*CONSTCOND*/false); 1587 } 1588 1589 lint_assert(fpe != 0 || isnan((double)v->v_ldbl) == 0); 1590 if (fpe != 0 || isfinite((double)v->v_ldbl) == 0 || 1591 (t == FLOAT && 1592 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) || 1593 (t == DOUBLE && 1594 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) { 1595 /* floating point overflow on operator '%s' */ 1596 warning(142, op_name(tn->tn_op)); 1597 v->v_ldbl = floating_error_value(t, v->v_ldbl); 1598 fpe = 0; 1599 } 1600 1601 return build_constant(tn->tn_type, v); 1602 } 1603 1604 /* 1605 * Create a tree node for a binary operator and its two operands. Also called 1606 * for unary operators; in that case rn is NULL. 1607 * 1608 * Function calls, sizeof and casts are handled elsewhere. 1609 */ 1610 tnode_t * 1611 build_binary(tnode_t *ln, op_t op, bool sys, tnode_t *rn) 1612 { 1613 const mod_t *mp; 1614 tnode_t *ntn; 1615 type_t *rettp; 1616 1617 mp = &modtab[op]; 1618 1619 /* If there was an error in one of the operands, return. */ 1620 if (ln == NULL || (mp->m_binary && rn == NULL)) 1621 return NULL; 1622 1623 /* 1624 * Apply class conversions to the left operand, but only if its 1625 * value is needed or it is compared with zero. 1626 */ 1627 if (mp->m_value_context || mp->m_compares_with_zero) 1628 ln = cconv(ln); 1629 /* 1630 * The right operand is almost always in a test or value context, 1631 * except if it is a struct or union member. 1632 */ 1633 if (mp->m_binary && op != ARROW && op != POINT) 1634 rn = cconv(rn); 1635 1636 /* 1637 * Print some warnings for comparisons of unsigned values with 1638 * constants lower than or equal to null. This must be done 1639 * before promote() because otherwise unsigned char and unsigned 1640 * short would be promoted to int. Types are also tested to be 1641 * CHAR, which would also become int. 1642 */ 1643 if (mp->m_comparison) 1644 check_integer_comparison(op, ln, rn); 1645 1646 if (mp->m_value_context || mp->m_compares_with_zero) 1647 ln = promote(op, false, ln); 1648 if (mp->m_binary && op != ARROW && op != POINT && 1649 op != ASSIGN && op != RETURN && op != INIT) { 1650 rn = promote(op, false, rn); 1651 } 1652 1653 /* 1654 * If the result of the operation is different for signed or 1655 * unsigned operands and one of the operands is signed only in 1656 * ANSI C, print a warning. 1657 */ 1658 if (mp->m_warn_if_left_unsigned_in_c90 && 1659 ln->tn_op == CON && ln->tn_val->v_unsigned_since_c90) { 1660 /* ANSI C treats constant as unsigned, op '%s' */ 1661 warning(218, mp->m_name); 1662 ln->tn_val->v_unsigned_since_c90 = false; 1663 } 1664 if (mp->m_warn_if_right_unsigned_in_c90 && 1665 rn->tn_op == CON && rn->tn_val->v_unsigned_since_c90) { 1666 /* ANSI C treats constant as unsigned, op '%s' */ 1667 warning(218, mp->m_name); 1668 rn->tn_val->v_unsigned_since_c90 = false; 1669 } 1670 1671 /* Make sure both operands are of the same type */ 1672 if (mp->m_balance_operands || (!allow_c90 && (op == SHL || op == SHR))) 1673 balance(op, &ln, &rn); 1674 1675 /* 1676 * Check types for compatibility with the operation and mutual 1677 * compatibility. Return if there are serious problems. 1678 */ 1679 if (!typeok(op, 0, ln, rn)) 1680 return NULL; 1681 1682 /* And now create the node. */ 1683 switch (op) { 1684 case POINT: 1685 case ARROW: 1686 ntn = build_struct_access(op, sys, ln, rn); 1687 break; 1688 case INCAFT: 1689 case DECAFT: 1690 case INCBEF: 1691 case DECBEF: 1692 ntn = build_prepost_incdec(op, sys, ln); 1693 break; 1694 case ADDR: 1695 ntn = build_address(sys, ln, false); 1696 break; 1697 case INDIR: 1698 ntn = new_tnode(INDIR, sys, ln->tn_type->t_subt, ln, NULL); 1699 break; 1700 case PLUS: 1701 case MINUS: 1702 ntn = build_plus_minus(op, sys, ln, rn); 1703 break; 1704 case SHL: 1705 case SHR: 1706 ntn = build_bit_shift(op, sys, ln, rn); 1707 break; 1708 case COLON: 1709 ntn = build_colon(sys, ln, rn); 1710 break; 1711 case ASSIGN: 1712 case MULASS: 1713 case DIVASS: 1714 case MODASS: 1715 case ADDASS: 1716 case SUBASS: 1717 case SHLASS: 1718 case SHRASS: 1719 case ANDASS: 1720 case XORASS: 1721 case ORASS: 1722 case RETURN: 1723 case INIT: 1724 ntn = build_assignment(op, sys, ln, rn); 1725 break; 1726 case COMMA: 1727 case QUEST: 1728 ntn = new_tnode(op, sys, rn->tn_type, ln, rn); 1729 break; 1730 case REAL: 1731 case IMAG: 1732 ntn = build_real_imag(op, sys, ln); 1733 break; 1734 default: 1735 rettp = mp->m_returns_bool 1736 ? gettyp(Tflag ? BOOL : INT) : ln->tn_type; 1737 lint_assert(mp->m_binary == (rn != NULL)); 1738 ntn = new_tnode(op, sys, rettp, ln, rn); 1739 break; 1740 } 1741 1742 /* Return if an error occurred. */ 1743 if (ntn == NULL) 1744 return NULL; 1745 1746 /* Print a warning if precedence confusion is possible */ 1747 if (mp->m_possible_precedence_confusion) 1748 check_precedence_confusion(ntn); 1749 1750 /* 1751 * Print a warning if one of the operands is in a context where 1752 * it is compared with zero and if this operand is a constant. 1753 */ 1754 if (hflag && !constcond_flag && 1755 mp->m_compares_with_zero && 1756 (ln->tn_op == CON || 1757 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) && 1758 /* XXX: rn->tn_system_dependent should be checked as well */ 1759 !ln->tn_system_dependent) { 1760 /* constant in conditional context */ 1761 warning(161); 1762 } 1763 1764 /* Fold if the operator requires it */ 1765 if (mp->m_fold_constant_operands) { 1766 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) { 1767 if (mp->m_compares_with_zero) { 1768 ntn = fold_bool(ntn); 1769 } else if (is_floating(ntn->tn_type->t_tspec)) { 1770 ntn = fold_float(ntn); 1771 } else { 1772 ntn = fold(ntn); 1773 } 1774 } else if (op == QUEST && ln->tn_op == CON) { 1775 ntn = ln->tn_val->v_quad != 0 1776 ? rn->tn_left : rn->tn_right; 1777 } 1778 } 1779 1780 return ntn; 1781 } 1782 1783 tnode_t * 1784 build_unary(op_t op, bool sys, tnode_t *tn) 1785 { 1786 return build_binary(tn, op, sys, NULL); 1787 } 1788 1789 /* 1790 * Return whether all struct/union members with the same name have the same 1791 * type and offset. 1792 */ 1793 static bool 1794 all_members_compatible(const sym_t *msym) 1795 { 1796 for (const sym_t *csym = msym; 1797 csym != NULL; csym = csym->s_symtab_next) { 1798 if (!is_member(csym)) 1799 continue; 1800 if (strcmp(msym->s_name, csym->s_name) != 0) 1801 continue; 1802 1803 for (const sym_t *sym = csym->s_symtab_next; 1804 sym != NULL; sym = sym->s_symtab_next) { 1805 1806 if (!is_member(sym)) 1807 continue; 1808 if (strcmp(csym->s_name, sym->s_name) != 0) 1809 continue; 1810 if (csym->u.s_member.sm_offset_in_bits != 1811 sym->u.s_member.sm_offset_in_bits) 1812 return false; 1813 1814 bool w = false; 1815 if (!types_compatible(csym->s_type, sym->s_type, 1816 false, false, &w) && !w) 1817 return false; 1818 if (csym->s_bitfield != sym->s_bitfield) 1819 return false; 1820 if (csym->s_bitfield) { 1821 type_t *tp1 = csym->s_type; 1822 type_t *tp2 = sym->s_type; 1823 if (tp1->t_flen != tp2->t_flen) 1824 return false; 1825 if (tp1->t_foffs != tp2->t_foffs) 1826 return false; 1827 } 1828 } 1829 } 1830 return true; 1831 } 1832 1833 /* 1834 * Returns a symbol which has the same name as the msym argument and is a 1835 * member of the struct or union specified by the tn argument. 1836 */ 1837 static sym_t * 1838 struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym) 1839 { 1840 struct_or_union *str; 1841 type_t *tp; 1842 tspec_t t; 1843 1844 /* 1845 * Remove the member if it was unknown until now, which means 1846 * that no defined struct or union has a member with the same name. 1847 */ 1848 if (msym->s_scl == NOSCL) { 1849 /* type '%s' does not have member '%s' */ 1850 error(101, type_name(tn->tn_type), msym->s_name); 1851 rmsym(msym); 1852 msym->s_kind = FMEMBER; 1853 msym->s_scl = STRUCT_MEMBER; 1854 1855 struct_or_union *sou = expr_zero_alloc(sizeof(*sou)); 1856 sou->sou_tag = expr_zero_alloc(sizeof(*sou->sou_tag)); 1857 sou->sou_tag->s_name = unnamed; 1858 1859 msym->u.s_member.sm_sou_type = sou; 1860 /* 1861 * The member sm_offset_in_bits is not needed here since this 1862 * symbol can only be used for error reporting. 1863 */ 1864 return msym; 1865 } 1866 1867 /* Set str to the tag of which msym is expected to be a member. */ 1868 str = NULL; 1869 t = (tp = tn->tn_type)->t_tspec; 1870 if (op == POINT) { 1871 if (is_struct_or_union(t)) 1872 str = tp->t_str; 1873 } else if (op == ARROW && t == PTR) { 1874 t = (tp = tp->t_subt)->t_tspec; 1875 if (is_struct_or_union(t)) 1876 str = tp->t_str; 1877 } 1878 1879 /* 1880 * If this struct/union has a member with the name of msym, return it. 1881 */ 1882 if (str != NULL) { 1883 for (sym_t *sym = msym; 1884 sym != NULL; sym = sym->s_symtab_next) { 1885 if (!is_member(sym)) 1886 continue; 1887 if (sym->u.s_member.sm_sou_type != str) 1888 continue; 1889 if (strcmp(sym->s_name, msym->s_name) != 0) 1890 continue; 1891 return sym; 1892 } 1893 } 1894 1895 bool eq = all_members_compatible(msym); 1896 1897 /* 1898 * Now handle the case in which the left operand refers really 1899 * to a struct/union, but the right operand is not member of it. 1900 */ 1901 if (str != NULL) { 1902 if (eq && !allow_c90) { 1903 /* illegal use of member '%s' */ 1904 warning(102, msym->s_name); 1905 } else { 1906 /* illegal use of member '%s' */ 1907 error(102, msym->s_name); 1908 } 1909 return msym; 1910 } 1911 1912 /* 1913 * Now the left operand of ARROW does not point to a struct/union 1914 * or the left operand of POINT is no struct/union. 1915 */ 1916 if (eq) { 1917 if (op == POINT) { 1918 if (!allow_c90) { 1919 /* left operand of '.' must be struct ... */ 1920 warning(103, type_name(tn->tn_type)); 1921 } else { 1922 /* left operand of '.' must be struct ... */ 1923 error(103, type_name(tn->tn_type)); 1924 } 1925 } else { 1926 if (!allow_c90 && tn->tn_type->t_tspec == PTR) { 1927 /* left operand of '->' must be pointer ... */ 1928 warning(104, type_name(tn->tn_type)); 1929 } else { 1930 /* left operand of '->' must be pointer ... */ 1931 error(104, type_name(tn->tn_type)); 1932 } 1933 } 1934 } else { 1935 if (!allow_c90) { 1936 /* non-unique member requires struct/union %s */ 1937 error(105, op == POINT ? "object" : "pointer"); 1938 } else { 1939 /* unacceptable operand of '%s' */ 1940 error(111, op_name(op)); 1941 } 1942 } 1943 1944 return msym; 1945 } 1946 1947 tnode_t * 1948 build_member_access(tnode_t *ln, op_t op, bool sys, sbuf_t *member) 1949 { 1950 sym_t *msym; 1951 1952 if (ln == NULL) 1953 return NULL; 1954 1955 if (op == ARROW) { 1956 /* must do this before struct_or_union_member is called */ 1957 ln = cconv(ln); 1958 } 1959 msym = struct_or_union_member(ln, op, getsym(member)); 1960 return build_binary(ln, op, sys, build_name(msym, false)); 1961 } 1962 1963 /* 1964 * Perform class conversions. 1965 * 1966 * Arrays of type T are converted into pointers to type T. 1967 * Functions are converted to pointers to functions. 1968 * Lvalues are converted to rvalues. 1969 * 1970 * C99 6.3 "Conversions" 1971 * C99 6.3.2 "Other operands" 1972 * C99 6.3.2.1 "Lvalues, arrays, and function designators" 1973 */ 1974 tnode_t * 1975 cconv(tnode_t *tn) 1976 { 1977 type_t *tp; 1978 1979 /* 1980 * Array-lvalue (array of type T) is converted into rvalue 1981 * (pointer to type T) 1982 */ 1983 if (tn->tn_type->t_tspec == ARRAY) { 1984 if (!tn->tn_lvalue) { 1985 /* XXX print correct operator */ 1986 /* %soperand of '%s' must be lvalue */ 1987 gnuism(114, "", op_name(ADDR)); 1988 } 1989 tn = new_tnode(ADDR, tn->tn_sys, 1990 expr_derive_type(tn->tn_type->t_subt, PTR), tn, NULL); 1991 } 1992 1993 /* 1994 * Expression of type function (function with return value of type T) 1995 * in rvalue-expression (pointer to function with return value 1996 * of type T) 1997 */ 1998 if (tn->tn_type->t_tspec == FUNC) 1999 tn = build_address(tn->tn_sys, tn, true); 2000 2001 /* lvalue to rvalue */ 2002 if (tn->tn_lvalue) { 2003 tp = expr_dup_type(tn->tn_type); 2004 /* C99 6.3.2.1p2 sentence 2 says to remove the qualifiers. */ 2005 tp->t_const = tp->t_volatile = false; 2006 tn = new_tnode(LOAD, tn->tn_sys, tp, tn, NULL); 2007 } 2008 2009 return tn; 2010 } 2011 2012 const tnode_t * 2013 before_conversion(const tnode_t *tn) 2014 { 2015 while (tn->tn_op == CVT && !tn->tn_cast) 2016 tn = tn->tn_left; 2017 return tn; 2018 } 2019 2020 /* 2021 * Most errors required by ANSI C are reported in struct_or_union_member(). 2022 * Here we only check for totally wrong things. 2023 */ 2024 static bool 2025 typeok_point(const tnode_t *ln, const type_t *ltp, tspec_t lt) 2026 { 2027 if (is_struct_or_union(lt)) 2028 return true; 2029 2030 if (lt == FUNC || lt == VOID || ltp->t_bitfield) 2031 goto wrong; 2032 2033 /* 2034 * Some C dialects from before C90 tolerated any lvalue on the 2035 * left-hand side of the '.' operator, allowing things like 2036 * char st[100]; st.st_mtime, assuming that the member 'st_mtime' 2037 * only occurred in a single struct; see typeok_arrow. 2038 */ 2039 if (ln->tn_lvalue) 2040 return true; 2041 2042 wrong: 2043 /* With allow_c90 we already got an error */ 2044 if (!allow_c90) 2045 /* unacceptable operand of '%s' */ 2046 error(111, op_name(POINT)); 2047 2048 return false; 2049 } 2050 2051 static bool 2052 typeok_arrow(tspec_t lt) 2053 { 2054 /* 2055 * C1978 Appendix A 14.1 says: <quote>In fact, any lvalue is allowed 2056 * before '.', and that lvalue is then assumed to have the form of 2057 * the structure of which the name of the right is a member. [...] 2058 * Such constructions are non-portable.</quote> 2059 */ 2060 if (lt == PTR || (!allow_c90 && is_integer(lt))) 2061 return true; 2062 2063 /* With allow_c90 we already got an error */ 2064 if (!allow_c90) 2065 /* unacceptable operand of '%s' */ 2066 error(111, op_name(ARROW)); 2067 return false; 2068 } 2069 2070 static bool 2071 typeok_incdec(op_t op, const tnode_t *tn, const type_t *tp) 2072 { 2073 /* operand has scalar type (checked in typeok) */ 2074 if (!tn->tn_lvalue) { 2075 if (tn->tn_op == CVT && tn->tn_cast && 2076 tn->tn_left->tn_op == LOAD) { 2077 /* a cast does not yield an lvalue */ 2078 error(163); 2079 } 2080 /* %soperand of '%s' must be lvalue */ 2081 error(114, "", op_name(op)); 2082 return false; 2083 } else if (tp->t_const) { 2084 if (allow_c90) 2085 /* %soperand of '%s' must be modifiable lvalue */ 2086 warning(115, "", op_name(op)); 2087 } 2088 return true; 2089 } 2090 2091 static bool 2092 typeok_address(const mod_t *mp, 2093 const tnode_t *tn, const type_t *tp, tspec_t t) 2094 { 2095 if (t == ARRAY || t == FUNC) { 2096 /* ok, a warning comes later (in build_address()) */ 2097 } else if (!tn->tn_lvalue) { 2098 if (tn->tn_op == CVT && tn->tn_cast && 2099 tn->tn_left->tn_op == LOAD) { 2100 /* a cast does not yield an lvalue */ 2101 error(163); 2102 } 2103 /* %soperand of '%s' must be lvalue */ 2104 error(114, "", mp->m_name); 2105 return false; 2106 } else if (is_scalar(t)) { 2107 if (tp->t_bitfield) { 2108 /* cannot take address of bit-field */ 2109 error(112); 2110 return false; 2111 } 2112 } else if (t != STRUCT && t != UNION) { 2113 /* unacceptable operand of '%s' */ 2114 error(111, mp->m_name); 2115 return false; 2116 } 2117 if (tn->tn_op == NAME && tn->tn_sym->s_register) { 2118 /* cannot take address of register '%s' */ 2119 error(113, tn->tn_sym->s_name); 2120 return false; 2121 } 2122 return true; 2123 } 2124 2125 static bool 2126 typeok_indir(const type_t *tp, tspec_t t) 2127 { 2128 2129 if (t != PTR) { 2130 /* cannot dereference non-pointer type '%s' */ 2131 error(96, type_name(tp)); 2132 return false; 2133 } 2134 return true; 2135 } 2136 2137 /* 2138 * Called if incompatible types were detected. 2139 * Prints a appropriate warning. 2140 */ 2141 static void 2142 warn_incompatible_types(op_t op, 2143 const type_t *ltp, tspec_t lt, 2144 const type_t *rtp, tspec_t rt) 2145 { 2146 const mod_t *mp; 2147 2148 mp = &modtab[op]; 2149 2150 if (lt == VOID || (mp->m_binary && rt == VOID)) { 2151 /* void type illegal in expression */ 2152 error(109); 2153 } else if (op == ASSIGN) { 2154 /* cannot assign to '%s' from '%s' */ 2155 error(171, type_name(ltp), type_name(rtp)); 2156 } else if (mp->m_binary) { 2157 /* operands of '%s' have incompatible types '%s' and '%s' */ 2158 error(107, mp->m_name, tspec_name(lt), tspec_name(rt)); 2159 } else { 2160 lint_assert(rt == NOTSPEC); 2161 /* operand of '%s' has invalid type '%s' */ 2162 error(108, mp->m_name, type_name(ltp)); 2163 } 2164 } 2165 2166 static bool 2167 typeok_plus(op_t op, 2168 const type_t *ltp, tspec_t lt, 2169 const type_t *rtp, tspec_t rt) 2170 { 2171 /* operands have scalar types (checked in typeok) */ 2172 if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) { 2173 warn_incompatible_types(op, ltp, lt, rtp, rt); 2174 return false; 2175 } 2176 return true; 2177 } 2178 2179 static bool 2180 typeok_minus(op_t op, 2181 const type_t *ltp, tspec_t lt, 2182 const type_t *rtp, tspec_t rt) 2183 { 2184 /* operands have scalar types (checked in typeok) */ 2185 if ((lt == PTR && rt != PTR && !is_integer(rt)) || 2186 (lt != PTR && rt == PTR)) { 2187 warn_incompatible_types(op, ltp, lt, rtp, rt); 2188 return false; 2189 } 2190 if (lt == PTR && rt == PTR) { 2191 if (!types_compatible(ltp->t_subt, rtp->t_subt, 2192 true, false, NULL)) { 2193 /* illegal pointer subtraction */ 2194 error(116); 2195 } 2196 } 2197 return true; 2198 } 2199 2200 static void 2201 typeok_shr(const mod_t *mp, 2202 const tnode_t *ln, tspec_t lt, 2203 const tnode_t *rn, tspec_t rt) 2204 { 2205 tspec_t olt, ort; 2206 2207 olt = before_conversion(ln)->tn_type->t_tspec; 2208 ort = before_conversion(rn)->tn_type->t_tspec; 2209 2210 /* operands have integer types (checked in typeok) */ 2211 if (pflag && !is_uinteger(olt)) { 2212 integer_constraints lc = ic_expr(ln); 2213 if (!ic_maybe_signed(ln->tn_type, &lc)) 2214 return; 2215 2216 /* 2217 * The left operand is signed. This means that 2218 * the operation is (possibly) nonportable. 2219 */ 2220 if (ln->tn_op != CON) { 2221 /* bitwise '%s' on signed value possibly nonportable */ 2222 warning(117, mp->m_name); 2223 } else if (ln->tn_val->v_quad < 0) { 2224 /* bitwise '%s' on signed value nonportable */ 2225 warning(120, mp->m_name); 2226 } 2227 } else if (allow_trad && allow_c90 && 2228 !is_uinteger(olt) && is_uinteger(ort)) { 2229 /* 2230 * The left operand would become unsigned in 2231 * traditional C. 2232 */ 2233 if (hflag && (ln->tn_op != CON || ln->tn_val->v_quad < 0)) { 2234 /* semantics of '%s' change in ANSI C; use ... */ 2235 warning(118, mp->m_name); 2236 } 2237 } else if (allow_trad && allow_c90 && 2238 !is_uinteger(olt) && !is_uinteger(ort) && 2239 portable_size_in_bits(lt) < portable_size_in_bits(rt)) { 2240 /* 2241 * In traditional C the left operand would be extended 2242 * (possibly sign-extended) and then shifted. 2243 */ 2244 if (hflag && (ln->tn_op != CON || ln->tn_val->v_quad < 0)) { 2245 /* semantics of '%s' change in ANSI C; use ... */ 2246 warning(118, mp->m_name); 2247 } 2248 } 2249 } 2250 2251 static void 2252 typeok_shl(const mod_t *mp, tspec_t lt, tspec_t rt) 2253 { 2254 /* 2255 * C90 does not perform balancing for shift operations, 2256 * but traditional C does. If the width of the right operand 2257 * is greater than the width of the left operand, then in 2258 * traditional C the left operand would be extended to the 2259 * width of the right operand. For SHL this may result in 2260 * different results. 2261 */ 2262 if (portable_size_in_bits(lt) < portable_size_in_bits(rt)) { 2263 /* 2264 * XXX If both operands are constant, make sure 2265 * that there is really a difference between 2266 * ANSI C and traditional C. 2267 */ 2268 if (hflag && !allow_c99) 2269 /* semantics of '%s' change in ANSI C; use ... */ 2270 warning(118, mp->m_name); 2271 } 2272 } 2273 2274 static void 2275 typeok_shift(const type_t *ltp, tspec_t lt, const tnode_t *rn, tspec_t rt) 2276 { 2277 if (rn->tn_op != CON) 2278 return; 2279 2280 if (!is_uinteger(rt) && rn->tn_val->v_quad < 0) { 2281 /* negative shift */ 2282 warning(121); 2283 } else if ((uint64_t)rn->tn_val->v_quad == 2284 (uint64_t)size_in_bits(lt)) { 2285 /* shift amount %u equals bit-size of '%s' */ 2286 warning(267, (unsigned)rn->tn_val->v_quad, type_name(ltp)); 2287 } else if ((uint64_t)rn->tn_val->v_quad > (uint64_t)size_in_bits(lt)) { 2288 /* shift amount %llu is greater than bit-size %llu of '%s' */ 2289 warning(122, (unsigned long long)rn->tn_val->v_quad, 2290 (unsigned long long)size_in_bits(lt), 2291 tspec_name(lt)); 2292 } 2293 } 2294 2295 static bool 2296 is_typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt) 2297 { 2298 if (lt == PTR && is_null_pointer(rn)) 2299 return true; 2300 if (rt == PTR && is_null_pointer(ln)) 2301 return true; 2302 return false; 2303 } 2304 2305 /* 2306 * Called if incompatible pointer types are detected. 2307 * Print an appropriate warning. 2308 */ 2309 static void 2310 warn_incompatible_pointers(const mod_t *mp, 2311 const type_t *ltp, const type_t *rtp) 2312 { 2313 tspec_t lt, rt; 2314 2315 lint_assert(ltp->t_tspec == PTR); 2316 lint_assert(rtp->t_tspec == PTR); 2317 2318 lt = ltp->t_subt->t_tspec; 2319 rt = rtp->t_subt->t_tspec; 2320 2321 if (is_struct_or_union(lt) && is_struct_or_union(rt)) { 2322 if (mp == NULL) { 2323 /* illegal structure pointer combination */ 2324 warning(244); 2325 } else { 2326 /* incompatible structure pointers: '%s' '%s' '%s' */ 2327 warning(245, type_name(ltp), mp->m_name, type_name(rtp)); 2328 } 2329 } else { 2330 if (mp == NULL) { 2331 /* illegal combination of '%s' and '%s' */ 2332 warning(184, type_name(ltp), type_name(rtp)); 2333 } else { 2334 /* illegal combination of '%s' and '%s', op '%s' */ 2335 warning(124, 2336 type_name(ltp), type_name(rtp), mp->m_name); 2337 } 2338 } 2339 } 2340 2341 static void 2342 check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn) 2343 { 2344 type_t *ltp, *rtp; 2345 tspec_t lst, rst; 2346 const char *lsts, *rsts; 2347 2348 lst = (ltp = ln->tn_type)->t_subt->t_tspec; 2349 rst = (rtp = rn->tn_type)->t_subt->t_tspec; 2350 2351 if (lst == VOID || rst == VOID) { 2352 /* TODO: C99 behaves like C90 here. */ 2353 if ((!allow_trad && !allow_c99) && 2354 (lst == FUNC || rst == FUNC)) { 2355 /* (void *)0 already handled in typeok() */ 2356 *(lst == FUNC ? &lsts : &rsts) = "function pointer"; 2357 *(lst == VOID ? &lsts : &rsts) = "'void *'"; 2358 /* ANSI C forbids comparison of %s with %s */ 2359 warning(274, lsts, rsts); 2360 } 2361 return; 2362 } 2363 2364 if (!types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) { 2365 warn_incompatible_pointers(&modtab[op], ltp, rtp); 2366 return; 2367 } 2368 2369 if (lst == FUNC && rst == FUNC) { 2370 /* TODO: C99 behaves like C90 here, see C99 6.5.8p2. */ 2371 if ((!allow_trad && !allow_c99) && op != EQ && op != NE) 2372 /* ANSI C forbids ordered comparisons of ... */ 2373 warning(125); 2374 } 2375 } 2376 2377 static bool 2378 typeok_compare(op_t op, 2379 const tnode_t *ln, const type_t *ltp, tspec_t lt, 2380 const tnode_t *rn, const type_t *rtp, tspec_t rt) 2381 { 2382 const char *lx, *rx; 2383 2384 if (lt == PTR && rt == PTR) { 2385 check_pointer_comparison(op, ln, rn); 2386 return true; 2387 } 2388 2389 if (lt != PTR && rt != PTR) 2390 return true; 2391 2392 if (!is_integer(lt) && !is_integer(rt)) { 2393 warn_incompatible_types(op, ltp, lt, rtp, rt); 2394 return false; 2395 } 2396 2397 lx = lt == PTR ? "pointer" : "integer"; 2398 rx = rt == PTR ? "pointer" : "integer"; 2399 /* illegal combination of %s '%s' and %s '%s', op '%s' */ 2400 warning(123, lx, type_name(ltp), rx, type_name(rtp), op_name(op)); 2401 return true; 2402 } 2403 2404 static bool 2405 typeok_quest(tspec_t lt, const tnode_t *rn) 2406 { 2407 if (!is_scalar(lt)) { 2408 /* first operand must have scalar type, op ? : */ 2409 error(170); 2410 return false; 2411 } 2412 lint_assert(before_conversion(rn)->tn_op == COLON); 2413 return true; 2414 } 2415 2416 static void 2417 typeok_colon_pointer(const mod_t *mp, const type_t *ltp, const type_t *rtp) 2418 { 2419 type_t *lstp = ltp->t_subt; 2420 type_t *rstp = rtp->t_subt; 2421 tspec_t lst = lstp->t_tspec; 2422 tspec_t rst = rstp->t_tspec; 2423 2424 if ((lst == VOID && rst == FUNC) || (lst == FUNC && rst == VOID)) { 2425 /* (void *)0 is handled in typeok_colon */ 2426 /* TODO: C99 behaves like C90 here. */ 2427 if (!allow_trad && !allow_c99) 2428 /* ANSI C forbids conversion of %s to %s, op %s */ 2429 warning(305, "function pointer", "'void *'", 2430 mp->m_name); 2431 return; 2432 } 2433 2434 if (pointer_types_are_compatible(lstp, rstp, true)) 2435 return; 2436 if (!types_compatible(lstp, rstp, true, false, NULL)) 2437 warn_incompatible_pointers(mp, ltp, rtp); 2438 } 2439 2440 static bool 2441 typeok_colon(const mod_t *mp, 2442 const tnode_t *ln, const type_t *ltp, tspec_t lt, 2443 const tnode_t *rn, const type_t *rtp, tspec_t rt) 2444 { 2445 2446 if (is_arithmetic(lt) && is_arithmetic(rt)) 2447 return true; 2448 if (lt == BOOL && rt == BOOL) 2449 return true; 2450 2451 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str) 2452 return true; 2453 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str) 2454 return true; 2455 2456 if (lt == PTR && is_null_pointer(rn)) 2457 return true; 2458 if (rt == PTR && is_null_pointer(ln)) 2459 return true; 2460 2461 if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) { 2462 const char *lx = lt == PTR ? "pointer" : "integer"; 2463 const char *rx = rt == PTR ? "pointer" : "integer"; 2464 /* illegal combination of %s '%s' and %s '%s', op '%s' */ 2465 warning(123, lx, type_name(ltp), 2466 rx, type_name(rtp), mp->m_name); 2467 return true; 2468 } 2469 2470 if (lt == VOID || rt == VOID) { 2471 if (lt != VOID || rt != VOID) 2472 /* incompatible types '%s' and '%s' in conditional */ 2473 warning(126, type_name(ltp), type_name(rtp)); 2474 return true; 2475 } 2476 2477 if (lt == PTR && rt == PTR) { 2478 typeok_colon_pointer(mp, ltp, rtp); 2479 return true; 2480 } 2481 2482 /* incompatible types '%s' and '%s' in conditional */ 2483 error(126, type_name(ltp), type_name(rtp)); 2484 return false; 2485 } 2486 2487 /* 2488 * Returns true if the given structure or union has a constant member 2489 * (maybe recursively). 2490 */ 2491 static bool 2492 has_constant_member(const type_t *tp) 2493 { 2494 sym_t *m; 2495 2496 lint_assert(is_struct_or_union(tp->t_tspec)); 2497 2498 for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next) { 2499 const type_t *mtp = m->s_type; 2500 if (mtp->t_const) 2501 return true; 2502 if (is_struct_or_union(mtp->t_tspec) && 2503 has_constant_member(mtp)) 2504 return true; 2505 } 2506 return false; 2507 } 2508 2509 static bool 2510 typeok_assign(op_t op, const tnode_t *ln, const type_t *ltp, tspec_t lt) 2511 { 2512 if (op == RETURN || op == INIT || op == FARG) 2513 return true; 2514 2515 if (!ln->tn_lvalue) { 2516 if (ln->tn_op == CVT && ln->tn_cast && 2517 ln->tn_left->tn_op == LOAD) { 2518 /* a cast does not yield an lvalue */ 2519 error(163); 2520 } 2521 /* %soperand of '%s' must be lvalue */ 2522 error(114, "left ", op_name(op)); 2523 return false; 2524 } else if (ltp->t_const || (is_struct_or_union(lt) && 2525 has_constant_member(ltp))) { 2526 if (allow_c90) 2527 /* %soperand of '%s' must be modifiable lvalue */ 2528 warning(115, "left ", op_name(op)); 2529 } 2530 return true; 2531 } 2532 2533 /* Check the types using the information from modtab[]. */ 2534 static bool 2535 typeok_scalar(op_t op, const mod_t *mp, 2536 const type_t *ltp, tspec_t lt, 2537 const type_t *rtp, tspec_t rt) 2538 { 2539 if (mp->m_takes_bool && lt == BOOL && rt == BOOL) 2540 return true; 2541 if (mp->m_requires_integer) { 2542 if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) { 2543 warn_incompatible_types(op, ltp, lt, rtp, rt); 2544 return false; 2545 } 2546 } else if (mp->m_requires_integer_or_complex) { 2547 if ((!is_integer(lt) && !is_complex(lt)) || 2548 (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) { 2549 warn_incompatible_types(op, ltp, lt, rtp, rt); 2550 return false; 2551 } 2552 } else if (mp->m_requires_scalar) { 2553 if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) { 2554 warn_incompatible_types(op, ltp, lt, rtp, rt); 2555 return false; 2556 } 2557 } else if (mp->m_requires_arith) { 2558 if (!is_arithmetic(lt) || 2559 (mp->m_binary && !is_arithmetic(rt))) { 2560 warn_incompatible_types(op, ltp, lt, rtp, rt); 2561 return false; 2562 } 2563 } 2564 return true; 2565 } 2566 2567 static void 2568 check_assign_void_pointer(op_t op, int arg, 2569 tspec_t lt, tspec_t lst, 2570 tspec_t rt, tspec_t rst) 2571 { 2572 const char *lts, *rts; 2573 2574 if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID))) 2575 return; 2576 /* two pointers, at least one pointer to void */ 2577 2578 /* TODO: C99 behaves like C90 here. */ 2579 if (!((!allow_trad && !allow_c99) && (lst == FUNC || rst == FUNC))) 2580 return; 2581 /* comb. of ptr to func and ptr to void */ 2582 2583 *(lst == FUNC ? <s : &rts) = "function pointer"; 2584 *(lst == VOID ? <s : &rts) = "'void *'"; 2585 2586 switch (op) { 2587 case INIT: 2588 case RETURN: 2589 /* ANSI C forbids conversion of %s to %s */ 2590 warning(303, rts, lts); 2591 break; 2592 case FARG: 2593 /* ANSI C forbids conversion of %s to %s, arg #%d */ 2594 warning(304, rts, lts, arg); 2595 break; 2596 default: 2597 /* ANSI C forbids conversion of %s to %s, op %s */ 2598 warning(305, rts, lts, op_name(op)); 2599 break; 2600 } 2601 } 2602 2603 static bool 2604 is_direct_function_call(const tnode_t *tn, const char **out_name) 2605 { 2606 2607 if (!(tn->tn_op == CALL && 2608 tn->tn_left->tn_op == ADDR && 2609 tn->tn_left->tn_left->tn_op == NAME)) 2610 return false; 2611 2612 *out_name = tn->tn_left->tn_left->tn_sym->s_name; 2613 return true; 2614 } 2615 2616 static bool 2617 is_unconst_function(const char *name) 2618 { 2619 2620 return strcmp(name, "memchr") == 0 || 2621 strcmp(name, "strchr") == 0 || 2622 strcmp(name, "strpbrk") == 0 || 2623 strcmp(name, "strrchr") == 0 || 2624 strcmp(name, "strstr") == 0; 2625 } 2626 2627 static bool 2628 is_const_char_pointer(const tnode_t *tn) 2629 { 2630 const type_t *tp; 2631 2632 /* 2633 * For traditional reasons, C99 6.4.5p5 defines that string literals 2634 * have type 'char[]'. They are often implicitly converted to 2635 * 'char *', for example when they are passed as function arguments. 2636 * 2637 * C99 6.4.5p6 further defines that modifying a string that is 2638 * constructed from a string literal invokes undefined behavior. 2639 * 2640 * Out of these reasons, string literals are treated as 'effectively 2641 * const' here. 2642 */ 2643 if (tn->tn_op == CVT && 2644 tn->tn_left->tn_op == ADDR && 2645 tn->tn_left->tn_left->tn_op == STRING) 2646 return true; 2647 2648 tp = before_conversion(tn)->tn_type; 2649 return tp->t_tspec == PTR && 2650 tp->t_subt->t_tspec == CHAR && 2651 tp->t_subt->t_const; 2652 } 2653 2654 static bool 2655 is_first_arg_const_char_pointer(const tnode_t *tn) 2656 { 2657 const tnode_t *an; 2658 2659 an = tn->tn_right; 2660 if (an == NULL) 2661 return false; 2662 2663 while (an->tn_right != NULL) 2664 an = an->tn_right; 2665 return is_const_char_pointer(an->tn_left); 2666 } 2667 2668 static bool 2669 is_const_pointer(const tnode_t *tn) 2670 { 2671 const type_t *tp; 2672 2673 tp = before_conversion(tn)->tn_type; 2674 return tp->t_tspec == PTR && tp->t_subt->t_const; 2675 } 2676 2677 static bool 2678 is_second_arg_const_pointer(const tnode_t *tn) 2679 { 2680 const tnode_t *an; 2681 2682 an = tn->tn_right; 2683 if (an == NULL || an->tn_right == NULL) 2684 return false; 2685 2686 while (an->tn_right->tn_right != NULL) 2687 an = an->tn_right; 2688 return is_const_pointer(an->tn_left); 2689 } 2690 2691 static void 2692 check_unconst_function(const type_t *lstp, const tnode_t *rn) 2693 { 2694 const char *function_name; 2695 2696 if (lstp->t_tspec == CHAR && !lstp->t_const && 2697 is_direct_function_call(rn, &function_name) && 2698 is_unconst_function(function_name) && 2699 is_first_arg_const_char_pointer(rn)) { 2700 /* call to '%s' effectively discards 'const' from argument */ 2701 warning(346, function_name); 2702 } 2703 2704 if (!lstp->t_const && 2705 is_direct_function_call(rn, &function_name) && 2706 strcmp(function_name, "bsearch") == 0 && 2707 is_second_arg_const_pointer(rn)) { 2708 /* call to '%s' effectively discards 'const' from argument */ 2709 warning(346, function_name); 2710 } 2711 } 2712 2713 static bool 2714 check_assign_void_pointer_compat(op_t op, int arg, 2715 const type_t *const ltp, tspec_t const lt, 2716 const type_t *const lstp, tspec_t const lst, 2717 const tnode_t *const rn, 2718 const type_t *const rtp, tspec_t const rt, 2719 const type_t *const rstp, tspec_t const rst) 2720 { 2721 if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID || 2722 types_compatible(lstp, rstp, 2723 true, false, NULL)))) 2724 return false; 2725 2726 /* compatible pointer types (qualifiers ignored) */ 2727 if (allow_c90 && 2728 ((!lstp->t_const && rstp->t_const) || 2729 (!lstp->t_volatile && rstp->t_volatile))) { 2730 /* left side has not all qualifiers of right */ 2731 switch (op) { 2732 case INIT: 2733 case RETURN: 2734 /* incompatible pointer types to '%s' and '%s' */ 2735 warning(182, type_name(lstp), type_name(rstp)); 2736 break; 2737 case FARG: 2738 /* converting '%s' to incompatible '%s' ... */ 2739 warning(153, 2740 type_name(rtp), type_name(ltp), arg); 2741 break; 2742 default: 2743 /* operands of '%s' have incompatible pointer ... */ 2744 warning(128, op_name(op), 2745 type_name(lstp), type_name(rstp)); 2746 break; 2747 } 2748 } 2749 2750 if (allow_c90) 2751 check_unconst_function(lstp, rn); 2752 2753 return true; 2754 } 2755 2756 static bool 2757 check_assign_pointer_integer(op_t op, int arg, 2758 const type_t *const ltp, tspec_t const lt, 2759 const type_t *const rtp, tspec_t const rt) 2760 { 2761 const char *lx, *rx; 2762 2763 if (!((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR))) 2764 return false; 2765 2766 lx = lt == PTR ? "pointer" : "integer"; 2767 rx = rt == PTR ? "pointer" : "integer"; 2768 2769 switch (op) { 2770 case INIT: 2771 case RETURN: 2772 /* illegal combination of %s '%s' and %s '%s' */ 2773 warning(183, lx, type_name(ltp), rx, type_name(rtp)); 2774 break; 2775 case FARG: 2776 /* illegal combination of %s '%s' and %s '%s', arg #%d */ 2777 warning(154, 2778 lx, type_name(ltp), rx, type_name(rtp), arg); 2779 break; 2780 default: 2781 /* illegal combination of %s '%s' and %s '%s', op '%s' */ 2782 warning(123, 2783 lx, type_name(ltp), rx, type_name(rtp), op_name(op)); 2784 break; 2785 } 2786 return true; 2787 } 2788 2789 static bool 2790 check_assign_pointer(op_t op, int arg, 2791 const type_t *ltp, tspec_t lt, 2792 const type_t *rtp, tspec_t rt) 2793 { 2794 if (!(lt == PTR && rt == PTR)) 2795 return false; 2796 2797 switch (op) { 2798 case RETURN: 2799 warn_incompatible_pointers(NULL, ltp, rtp); 2800 break; 2801 case FARG: 2802 /* converting '%s' to incompatible '%s' for ... */ 2803 warning(153, type_name(rtp), type_name(ltp), arg); 2804 break; 2805 default: 2806 warn_incompatible_pointers(&modtab[op], ltp, rtp); 2807 break; 2808 } 2809 return true; 2810 } 2811 2812 static void 2813 warn_assign(op_t op, int arg, 2814 const type_t *ltp, tspec_t lt, 2815 const type_t *rtp, tspec_t rt) 2816 { 2817 switch (op) { 2818 case INIT: 2819 /* cannot initialize '%s' from '%s' */ 2820 error(185, type_name(ltp), type_name(rtp)); 2821 break; 2822 case RETURN: 2823 /* function has return type '%s' but returns '%s' */ 2824 error(211, type_name(ltp), type_name(rtp)); 2825 break; 2826 case FARG: 2827 /* passing '%s' to incompatible '%s', arg #%d */ 2828 warning(155, type_name(rtp), type_name(ltp), arg); 2829 break; 2830 default: 2831 warn_incompatible_types(op, ltp, lt, rtp, rt); 2832 break; 2833 } 2834 } 2835 2836 /* 2837 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN 2838 * and prints warnings/errors if necessary. 2839 * Returns whether the types are (almost) compatible. 2840 */ 2841 static bool 2842 check_assign_types_compatible(op_t op, int arg, 2843 const tnode_t *ln, const tnode_t *rn) 2844 { 2845 tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC; 2846 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL; 2847 2848 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR) 2849 lst = (lstp = ltp->t_subt)->t_tspec; 2850 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR) 2851 rst = (rstp = rtp->t_subt)->t_tspec; 2852 2853 if (lt == BOOL && is_scalar(rt)) /* C99 6.3.1.2 */ 2854 return true; 2855 2856 if (is_arithmetic(lt) && (is_arithmetic(rt) || rt == BOOL)) 2857 return true; 2858 2859 if (is_struct_or_union(lt) && is_struct_or_union(rt)) 2860 /* both are struct or union */ 2861 return ltp->t_str == rtp->t_str; 2862 2863 /* a null pointer may be assigned to any pointer */ 2864 if (lt == PTR && is_null_pointer(rn)) 2865 return true; 2866 2867 check_assign_void_pointer(op, arg, lt, lst, rt, rst); 2868 2869 if (check_assign_void_pointer_compat(op, arg, 2870 ltp, lt, lstp, lst, rn, rtp, rt, rstp, rst)) 2871 return true; 2872 2873 if (check_assign_pointer_integer(op, arg, ltp, lt, rtp, rt)) 2874 return true; 2875 2876 if (check_assign_pointer(op, arg, ltp, lt, rtp, rt)) 2877 return true; 2878 2879 warn_assign(op, arg, ltp, lt, rtp, rt); 2880 return false; 2881 } 2882 2883 static bool 2884 has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */ 2885 { 2886 op_t op = tn->tn_op; 2887 2888 if (modtab[op].m_has_side_effect) 2889 return true; 2890 2891 if (op == CVT && tn->tn_type->t_tspec == VOID) 2892 return has_side_effect(tn->tn_left); 2893 2894 /* XXX: Why not has_side_effect(tn->tn_left) as well? */ 2895 if (op == LOGAND || op == LOGOR) 2896 return has_side_effect(tn->tn_right); 2897 2898 /* XXX: Why not has_side_effect(tn->tn_left) as well? */ 2899 if (op == QUEST) 2900 return has_side_effect(tn->tn_right); 2901 2902 if (op == COLON || op == COMMA) { 2903 return has_side_effect(tn->tn_left) || 2904 has_side_effect(tn->tn_right); 2905 } 2906 2907 return false; 2908 } 2909 2910 static bool 2911 is_void_cast(const tnode_t *tn) 2912 { 2913 2914 return tn->tn_op == CVT && tn->tn_cast && 2915 tn->tn_type->t_tspec == VOID; 2916 } 2917 2918 static bool 2919 is_local_symbol(const tnode_t *tn) 2920 { 2921 2922 return tn->tn_op == LOAD && 2923 tn->tn_left->tn_op == NAME && 2924 tn->tn_left->tn_sym->s_scl == AUTO; 2925 } 2926 2927 static bool 2928 is_int_constant_zero(const tnode_t *tn) 2929 { 2930 2931 return tn->tn_op == CON && 2932 tn->tn_type->t_tspec == INT && 2933 tn->tn_val->v_quad == 0; 2934 } 2935 2936 static void 2937 check_null_effect(const tnode_t *tn) 2938 { 2939 2940 if (!hflag) 2941 return; 2942 if (has_side_effect(tn)) 2943 return; 2944 if (is_void_cast(tn) && is_local_symbol(tn->tn_left)) 2945 return; 2946 if (is_void_cast(tn) && is_int_constant_zero(tn->tn_left)) 2947 return; 2948 2949 /* expression has null effect */ 2950 warning(129); 2951 } 2952 2953 /* 2954 * Check the types for specific operators and type combinations. 2955 * 2956 * At this point, the operands already conform to the type requirements of 2957 * the operator, such as being integer, floating or scalar. 2958 */ 2959 static bool 2960 typeok_op(op_t op, const mod_t *mp, int arg, 2961 const tnode_t *ln, const type_t *ltp, tspec_t lt, 2962 const tnode_t *rn, const type_t *rtp, tspec_t rt) 2963 { 2964 switch (op) { 2965 case ARROW: 2966 return typeok_arrow(lt); 2967 case POINT: 2968 return typeok_point(ln, ltp, lt); 2969 case INCBEF: 2970 case DECBEF: 2971 case INCAFT: 2972 case DECAFT: 2973 return typeok_incdec(op, ln, ltp); 2974 case INDIR: 2975 return typeok_indir(ltp, lt); 2976 case ADDR: 2977 return typeok_address(mp, ln, ltp, lt); 2978 case PLUS: 2979 return typeok_plus(op, ltp, lt, rtp, rt); 2980 case MINUS: 2981 return typeok_minus(op, ltp, lt, rtp, rt); 2982 case SHL: 2983 typeok_shl(mp, lt, rt); 2984 goto shift; 2985 case SHR: 2986 typeok_shr(mp, ln, lt, rn, rt); 2987 shift: 2988 typeok_shift(ltp, lt, rn, rt); 2989 break; 2990 case LT: 2991 case LE: 2992 case GT: 2993 case GE: 2994 compare: 2995 return typeok_compare(op, ln, ltp, lt, rn, rtp, rt); 2996 case EQ: 2997 case NE: 2998 if (is_typeok_eq(ln, lt, rn, rt)) 2999 break; 3000 goto compare; 3001 case QUEST: 3002 return typeok_quest(lt, rn); 3003 case COLON: 3004 return typeok_colon(mp, ln, ltp, lt, rn, rtp, rt); 3005 case ASSIGN: 3006 case INIT: 3007 case FARG: 3008 case RETURN: 3009 if (!check_assign_types_compatible(op, arg, ln, rn)) 3010 return false; 3011 goto assign; 3012 case MULASS: 3013 case DIVASS: 3014 case MODASS: 3015 goto assign; 3016 case ADDASS: 3017 case SUBASS: 3018 if ((lt == PTR && !is_integer(rt)) || rt == PTR) { 3019 warn_incompatible_types(op, ltp, lt, rtp, rt); 3020 return false; 3021 } 3022 goto assign; 3023 case SHLASS: 3024 goto assign; 3025 case SHRASS: 3026 if (pflag && !is_uinteger(lt) && 3027 !(!allow_c90 && is_uinteger(rt))) { 3028 /* bitwise '%s' on signed value possibly nonportable */ 3029 warning(117, mp->m_name); 3030 } 3031 goto assign; 3032 case ANDASS: 3033 case XORASS: 3034 case ORASS: 3035 assign: 3036 return typeok_assign(op, ln, ltp, lt); 3037 case COMMA: 3038 if (!modtab[ln->tn_op].m_has_side_effect) 3039 check_null_effect(ln); 3040 break; 3041 default: 3042 break; 3043 } 3044 return true; 3045 } 3046 3047 /* Prints a warning if a strange operator is used on an enum type. */ 3048 static void 3049 check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn) 3050 { 3051 3052 if (!eflag) 3053 return; 3054 3055 /* 3056 * Enum as offset to a pointer is an exception (otherwise enums 3057 * could not be used as array indices). 3058 */ 3059 if (op == PLUS && 3060 ((ln->tn_type->t_is_enum && rn->tn_type->t_tspec == PTR) || 3061 (rn->tn_type->t_is_enum && ln->tn_type->t_tspec == PTR))) { 3062 return; 3063 } 3064 3065 /* dubious operation on enum, op '%s' */ 3066 warning(241, op_name(op)); 3067 } 3068 3069 /* 3070 * Prints a warning if an operator is applied to two different enum types. 3071 */ 3072 static void 3073 check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn) 3074 { 3075 const mod_t *mp; 3076 3077 mp = &modtab[op]; 3078 3079 if (ln->tn_type->t_enum != rn->tn_type->t_enum) { 3080 switch (op) { 3081 case INIT: 3082 /* enum type mismatch between '%s' and '%s' in ... */ 3083 warning(210, 3084 type_name(ln->tn_type), type_name(rn->tn_type)); 3085 break; 3086 case FARG: 3087 /* function expects '%s', passing '%s' for arg #%d */ 3088 warning(156, 3089 type_name(ln->tn_type), type_name(rn->tn_type), 3090 arg); 3091 break; 3092 case RETURN: 3093 /* function has return type '%s' but returns '%s' */ 3094 warning(211, 3095 type_name(ln->tn_type), type_name(rn->tn_type)); 3096 break; 3097 default: 3098 /* enum type mismatch: '%s' '%s' '%s' */ 3099 warning(130, type_name(ln->tn_type), mp->m_name, 3100 type_name(rn->tn_type)); 3101 break; 3102 } 3103 } else if (Pflag && mp->m_comparison && op != EQ && op != NE) { 3104 if (eflag) 3105 /* dubious comparison of enums, op '%s' */ 3106 warning(243, mp->m_name); 3107 } 3108 } 3109 3110 /* Prints a warning if the operands mix between enum and integer. */ 3111 static void 3112 check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn) 3113 { 3114 3115 if (!eflag) 3116 return; 3117 3118 switch (op) { 3119 case INIT: 3120 /* 3121 * Initialization with 0 is allowed. Otherwise, all implicit 3122 * initializations would need to be warned upon as well. 3123 */ 3124 if (!rn->tn_type->t_is_enum && rn->tn_op == CON && 3125 is_integer(rn->tn_type->t_tspec) && 3126 rn->tn_val->v_quad == 0) { 3127 return; 3128 } 3129 /* initialization of '%s' with '%s' */ 3130 warning(277, type_name(ln->tn_type), type_name(rn->tn_type)); 3131 break; 3132 case FARG: 3133 /* combination of '%s' and '%s', arg #%d */ 3134 warning(278, 3135 type_name(ln->tn_type), type_name(rn->tn_type), arg); 3136 break; 3137 case RETURN: 3138 /* combination of '%s' and '%s' in return */ 3139 warning(279, type_name(ln->tn_type), type_name(rn->tn_type)); 3140 break; 3141 default: 3142 /* combination of '%s' and '%s', op '%s' */ 3143 warning(242, type_name(ln->tn_type), type_name(rn->tn_type), 3144 op_name(op)); 3145 break; 3146 } 3147 } 3148 3149 static void 3150 typeok_enum(op_t op, const mod_t *mp, int arg, 3151 const tnode_t *ln, const type_t *ltp, 3152 const tnode_t *rn, const type_t *rtp) 3153 { 3154 if (mp->m_bad_on_enum && 3155 (ltp->t_is_enum || (mp->m_binary && rtp->t_is_enum))) { 3156 check_bad_enum_operation(op, ln, rn); 3157 } else if (mp->m_valid_on_enum && 3158 (ltp->t_is_enum && rtp != NULL && rtp->t_is_enum)) { 3159 check_enum_type_mismatch(op, arg, ln, rn); 3160 } else if (mp->m_valid_on_enum && 3161 (ltp->t_is_enum || (rtp != NULL && rtp->t_is_enum))) { 3162 check_enum_int_mismatch(op, arg, ln, rn); 3163 } 3164 } 3165 3166 /* Perform most type checks. Return whether the types are ok. */ 3167 bool 3168 typeok(op_t op, int arg, const tnode_t *ln, const tnode_t *rn) 3169 { 3170 const mod_t *mp; 3171 tspec_t lt, rt; 3172 type_t *ltp, *rtp; 3173 3174 mp = &modtab[op]; 3175 3176 lint_assert((ltp = ln->tn_type) != NULL); 3177 lt = ltp->t_tspec; 3178 3179 if (mp->m_binary) { 3180 lint_assert((rtp = rn->tn_type) != NULL); 3181 rt = rtp->t_tspec; 3182 } else { 3183 rtp = NULL; 3184 rt = NOTSPEC; 3185 } 3186 3187 if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn)) 3188 return false; 3189 if (!typeok_scalar(op, mp, ltp, lt, rtp, rt)) 3190 return false; 3191 3192 if (!typeok_op(op, mp, arg, ln, ltp, lt, rn, rtp, rt)) 3193 return false; 3194 3195 typeok_enum(op, mp, arg, ln, ltp, rn, rtp); 3196 return true; 3197 } 3198 3199 /* In traditional C, keep unsigned and promote FLOAT to DOUBLE. */ 3200 static tspec_t 3201 promote_trad(tspec_t t) 3202 { 3203 3204 if (t == UCHAR || t == USHORT) 3205 return UINT; 3206 if (t == CHAR || t == SCHAR || t == SHORT) 3207 return INT; 3208 if (t == FLOAT) 3209 return DOUBLE; 3210 if (t == ENUM) 3211 return INT; 3212 return t; 3213 } 3214 3215 /* 3216 * C99 6.3.1.1p2 requires for types with lower rank than int that "If an int 3217 * can represent all the values of the original type, the value is converted 3218 * to an int; otherwise it is converted to an unsigned int", and that "All 3219 * other types are unchanged by the integer promotions". 3220 */ 3221 static tspec_t 3222 promote_c90(const tnode_t *tn, tspec_t t, bool farg) 3223 { 3224 if (tn->tn_type->t_bitfield) { 3225 unsigned int len = tn->tn_type->t_flen; 3226 if (len < size_in_bits(INT)) 3227 return INT; 3228 if (len == size_in_bits(INT)) 3229 return is_uinteger(t) ? UINT : INT; 3230 return t; 3231 } 3232 3233 if (t == CHAR || t == SCHAR) 3234 return INT; 3235 if (t == UCHAR) 3236 return size_in_bits(CHAR) < size_in_bits(INT) ? INT : UINT; 3237 if (t == SHORT) 3238 return INT; 3239 if (t == USHORT) 3240 return size_in_bits(SHORT) < size_in_bits(INT) ? INT : UINT; 3241 if (t == ENUM) 3242 return INT; 3243 if (farg && t == FLOAT) 3244 return DOUBLE; 3245 return t; 3246 } 3247 3248 /* 3249 * Performs the "integer promotions" (C99 6.3.1.1p2), which convert small 3250 * integer types to either int or unsigned int. 3251 * 3252 * If allow_c90 is unset or the operand is a function argument with no type 3253 * information (no prototype or variable # of args), converts float to double. 3254 */ 3255 tnode_t * 3256 promote(op_t op, bool farg, tnode_t *tn) 3257 { 3258 3259 tspec_t ot = tn->tn_type->t_tspec; 3260 if (!is_arithmetic(ot)) 3261 return tn; 3262 3263 tspec_t nt = allow_c90 ? promote_c90(tn, ot, farg) : promote_trad(ot); 3264 if (nt == ot) 3265 return tn; 3266 3267 type_t *ntp = expr_dup_type(tn->tn_type); 3268 ntp->t_tspec = nt; 3269 /* 3270 * Keep t_is_enum even though t_tspec gets converted from 3271 * ENUM to INT, so we are later able to check compatibility 3272 * of enum types. 3273 */ 3274 return convert(op, 0, ntp, tn); 3275 } 3276 3277 static void 3278 convert_integer_from_floating(op_t op, const type_t *tp, const tnode_t *tn) 3279 { 3280 3281 if (op == CVT) 3282 /* cast from floating point '%s' to integer '%s' */ 3283 query_message(2, type_name(tn->tn_type), type_name(tp)); 3284 else 3285 /* implicit conversion from floating point '%s' to ... */ 3286 query_message(1, type_name(tn->tn_type), type_name(tp)); 3287 } 3288 3289 static bool 3290 should_warn_about_prototype_conversion(tspec_t nt, 3291 tspec_t ot, const tnode_t *ptn) 3292 { 3293 3294 if (nt == ot) 3295 return false; 3296 3297 if (nt == ENUM && ot == INT) 3298 return false; 3299 3300 if (is_floating(nt) != is_floating(ot) || 3301 portable_size_in_bits(nt) != portable_size_in_bits(ot)) { 3302 /* representation and/or width change */ 3303 if (!is_integer(ot)) 3304 return true; 3305 /* 3306 * XXX: Investigate whether this rule makes sense; see 3307 * tests/usr.bin/xlint/lint1/platform_long.c. 3308 */ 3309 return portable_size_in_bits(ot) > portable_size_in_bits(INT); 3310 } 3311 3312 if (!hflag) 3313 return false; 3314 3315 /* 3316 * If the types differ only in sign and the argument has the same 3317 * representation in both types, print no warning. 3318 */ 3319 if (ptn->tn_op == CON && is_integer(nt) && 3320 signed_type(nt) == signed_type(ot) && 3321 !msb(ptn->tn_val->v_quad, ot)) 3322 return false; 3323 3324 return true; 3325 } 3326 3327 /* 3328 * Warn if a prototype causes a type conversion that is different from what 3329 * would happen to the same argument in the absence of a prototype. This 3330 * check is intended for code that needs to stay compatible with pre-C90 C. 3331 * 3332 * Errors/warnings about illegal type combinations are already printed 3333 * in check_assign_types_compatible(). 3334 */ 3335 static void 3336 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp, 3337 tnode_t *tn) 3338 { 3339 tnode_t *ptn; 3340 3341 if (!is_arithmetic(nt) || !is_arithmetic(ot)) 3342 return; 3343 3344 /* 3345 * If the type of the formal parameter is char/short, a warning 3346 * would be useless, because functions declared the old style 3347 * can't expect char/short arguments. 3348 */ 3349 if (nt == CHAR || nt == SCHAR || nt == UCHAR || 3350 nt == SHORT || nt == USHORT) 3351 return; 3352 3353 /* apply the default promotion */ 3354 ptn = promote(NOOP, true, tn); 3355 ot = ptn->tn_type->t_tspec; 3356 3357 if (should_warn_about_prototype_conversion(nt, ot, ptn)) { 3358 /* argument #%d is converted from '%s' to '%s' ... */ 3359 warning(259, arg, type_name(tn->tn_type), type_name(tp)); 3360 } 3361 } 3362 3363 /* 3364 * When converting a large integer type to a small integer type, in some 3365 * cases the value of the actual expression is further restricted than the 3366 * type bounds, such as in (expr & 0xFF) or (expr % 100) or (expr >> 24). 3367 */ 3368 static bool 3369 can_represent(const type_t *tp, const tnode_t *tn) 3370 { 3371 3372 debug_step("%s: type '%s'", __func__, type_name(tp)); 3373 debug_node(tn); 3374 3375 uint64_t nmask = value_bits(width_in_bits(tp)); 3376 if (!is_uinteger(tp->t_tspec)) 3377 nmask >>= 1; 3378 3379 integer_constraints c = ic_expr(tn); 3380 if ((~c.bclr & ~nmask) == 0) 3381 return true; 3382 3383 return false; 3384 } 3385 3386 static void 3387 convert_integer_from_integer(op_t op, int arg, tspec_t nt, tspec_t ot, 3388 type_t *tp, tnode_t *tn) 3389 { 3390 3391 if (tn->tn_op == CON) 3392 return; 3393 3394 if (op == CVT) 3395 return; 3396 3397 if (Pflag && pflag && aflag > 0 && 3398 portable_size_in_bits(nt) > portable_size_in_bits(ot) && 3399 is_uinteger(nt) != is_uinteger(ot)) { 3400 if (op == FARG) { 3401 /* conversion to '%s' may sign-extend ... */ 3402 warning(297, type_name(tp), arg); 3403 } else { 3404 /* conversion to '%s' may sign-extend ... */ 3405 warning(131, type_name(tp)); 3406 } 3407 } 3408 3409 if (Pflag && portable_size_in_bits(nt) > portable_size_in_bits(ot) && 3410 (tn->tn_op == PLUS || tn->tn_op == MINUS || tn->tn_op == MULT || 3411 tn->tn_op == SHL)) { 3412 /* suggest cast from '%s' to '%s' on op '%s' to ... */ 3413 warning(324, type_name(gettyp(ot)), type_name(tp), 3414 op_name(tn->tn_op)); 3415 } 3416 3417 if (aflag > 0 && 3418 portable_size_in_bits(nt) < portable_size_in_bits(ot) && 3419 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD || 3420 aflag > 1) && 3421 !can_represent(tp, tn)) { 3422 if (op == FARG) { 3423 /* conversion from '%s' to '%s' may lose ... */ 3424 warning(298, 3425 type_name(tn->tn_type), type_name(tp), arg); 3426 } else { 3427 /* conversion from '%s' to '%s' may lose accuracy */ 3428 warning(132, 3429 type_name(tn->tn_type), type_name(tp)); 3430 } 3431 } 3432 3433 if (is_uinteger(nt) != is_uinteger(ot)) 3434 /* implicit conversion changes sign from '%s' to '%s' */ 3435 query_message(3, type_name(tn->tn_type), type_name(tp)); 3436 } 3437 3438 static void 3439 convert_integer_from_pointer(op_t op, tspec_t nt, type_t *tp, tnode_t *tn) 3440 { 3441 3442 if (tn->tn_op == CON) 3443 return; 3444 if (op != CVT) 3445 return; /* We got already an error. */ 3446 if (portable_size_in_bits(nt) >= portable_size_in_bits(PTR)) 3447 return; 3448 3449 if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) { 3450 /* conversion of pointer to '%s' may lose bits */ 3451 warning(134, type_name(tp)); 3452 } else { 3453 /* conversion of pointer to '%s' loses bits */ 3454 warning(133, type_name(tp)); 3455 } 3456 } 3457 3458 static bool 3459 struct_starts_with(const type_t *struct_tp, const type_t *member_tp) 3460 { 3461 3462 return struct_tp->t_str->sou_first_member != NULL && 3463 types_compatible(struct_tp->t_str->sou_first_member->s_type, 3464 member_tp, true, false, NULL); 3465 } 3466 3467 static bool 3468 is_byte_array(const type_t *tp) 3469 { 3470 3471 return tp->t_tspec == ARRAY && 3472 (tp->t_subt->t_tspec == CHAR || tp->t_subt->t_tspec == UCHAR); 3473 } 3474 3475 static bool 3476 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst, 3477 const type_t *ostp, tspec_t ost) 3478 { 3479 3480 while (nst == ARRAY) 3481 nstp = nstp->t_subt, nst = nstp->t_tspec; 3482 while (ost == ARRAY) 3483 ostp = ostp->t_subt, ost = ostp->t_tspec; 3484 3485 if (nst == STRUCT && ost == STRUCT && 3486 (struct_starts_with(nstp, ostp) || 3487 struct_starts_with(ostp, nstp))) 3488 return false; 3489 3490 if (is_incomplete(nstp) || is_incomplete(ostp)) 3491 return false; 3492 3493 if (nst == CHAR || nst == UCHAR) 3494 return false; /* for the sake of traditional C code */ 3495 if (ost == CHAR || ost == UCHAR) 3496 return false; /* for the sake of traditional C code */ 3497 3498 /* Allow cast between pointers to sockaddr variants. */ 3499 if (nst == STRUCT && ost == STRUCT) { 3500 debug_type(nstp); 3501 debug_type(ostp); 3502 const sym_t *nmem = nstp->t_str->sou_first_member; 3503 const sym_t *omem = ostp->t_str->sou_first_member; 3504 while (nmem != NULL && omem != NULL && 3505 types_compatible(nmem->s_type, omem->s_type, 3506 true, false, NULL)) 3507 nmem = nmem->s_next, omem = omem->s_next; 3508 if (nmem != NULL && is_byte_array(nmem->s_type)) 3509 return false; 3510 if (omem != NULL && is_byte_array(omem->s_type)) 3511 return false; 3512 if (nmem == NULL && omem == NULL) 3513 return false; 3514 } 3515 3516 if (is_struct_or_union(nst) && nstp->t_str != ostp->t_str) 3517 return true; 3518 3519 return portable_size_in_bits(nst) != portable_size_in_bits(ost); 3520 } 3521 3522 static void 3523 convert_pointer_from_pointer(type_t *ntp, tnode_t *tn) 3524 { 3525 const type_t *nstp, *otp, *ostp; 3526 tspec_t nst, ost; 3527 const char *nts, *ots; 3528 3529 nstp = ntp->t_subt; 3530 otp = tn->tn_type; 3531 ostp = otp->t_subt; 3532 nst = nstp->t_tspec; 3533 ost = ostp->t_tspec; 3534 3535 if (nst == VOID || ost == VOID) { 3536 /* TODO: C99 behaves like C90 here. */ 3537 if ((!allow_trad && !allow_c99) && (nst == FUNC || ost == FUNC)) { 3538 /* null pointers are already handled in convert() */ 3539 *(nst == FUNC ? &nts : &ots) = "function pointer"; 3540 *(nst == VOID ? &nts : &ots) = "'void *'"; 3541 /* ANSI C forbids conversion of %s to %s */ 3542 warning(303, ots, nts); 3543 } 3544 return; 3545 } else if (nst == FUNC && ost == FUNC) { 3546 return; 3547 } else if (nst == FUNC || ost == FUNC) { 3548 /* converting '%s' to '%s' is questionable */ 3549 warning(229, type_name(otp), type_name(ntp)); 3550 return; 3551 } 3552 3553 if (hflag && alignment_in_bits(nstp) > alignment_in_bits(ostp) && 3554 ost != CHAR && ost != UCHAR && 3555 !is_incomplete(ostp)) { 3556 /* converting '%s' to '%s' increases alignment ... */ 3557 warning(135, type_name(otp), type_name(ntp), 3558 alignment_in_bits(ostp) / CHAR_SIZE, 3559 alignment_in_bits(nstp) / CHAR_SIZE); 3560 } 3561 3562 if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) { 3563 /* pointer cast from '%s' to '%s' may be troublesome */ 3564 warning(247, type_name(otp), type_name(ntp)); 3565 } 3566 } 3567 3568 /* 3569 * Insert a conversion operator, which converts the type of the node 3570 * to another given type. 3571 * 3572 * Possible values for 'op': 3573 * CVT a cast-expression 3574 * binary integer promotion for one of the operands, or a usual 3575 * arithmetic conversion 3576 * binary plain or compound assignments to bit-fields 3577 * FARG 'arg' is the number of the argument (used for warnings) 3578 * NOOP several other implicit conversions 3579 * ... 3580 */ 3581 tnode_t * 3582 convert(op_t op, int arg, type_t *tp, tnode_t *tn) 3583 { 3584 tnode_t *ntn; 3585 tspec_t nt, ot; 3586 3587 nt = tp->t_tspec; 3588 ot = tn->tn_type->t_tspec; 3589 3590 if (allow_trad && allow_c90 && op == FARG) 3591 check_prototype_conversion(arg, nt, ot, tp, tn); 3592 3593 if (nt == BOOL) { 3594 /* No further checks. */ 3595 3596 } else if (is_integer(nt)) { 3597 if (ot == BOOL) { 3598 /* No further checks. */ 3599 } else if (is_integer(ot)) { 3600 convert_integer_from_integer(op, arg, nt, ot, tp, tn); 3601 } else if (is_floating(ot)) { 3602 convert_integer_from_floating(op, tp, tn); 3603 } else if (ot == PTR) { 3604 convert_integer_from_pointer(op, nt, tp, tn); 3605 } 3606 3607 } else if (is_floating(nt)) { 3608 /* No further checks. */ 3609 3610 } else if (nt == PTR) { 3611 if (is_null_pointer(tn)) { 3612 /* a null pointer may be assigned to any pointer. */ 3613 } else if (ot == PTR && op == CVT) { 3614 convert_pointer_from_pointer(tp, tn); 3615 } 3616 } 3617 3618 ntn = expr_alloc_tnode(); 3619 ntn->tn_op = CVT; 3620 ntn->tn_type = tp; 3621 ntn->tn_cast = op == CVT; 3622 ntn->tn_sys |= tn->tn_sys; 3623 ntn->tn_right = NULL; 3624 if (tn->tn_op != CON || nt == VOID) { 3625 ntn->tn_left = tn; 3626 } else { 3627 ntn->tn_op = CON; 3628 ntn->tn_val = expr_zero_alloc(sizeof(*ntn->tn_val)); 3629 convert_constant(op, arg, ntn->tn_type, ntn->tn_val, 3630 tn->tn_val); 3631 } 3632 3633 return ntn; 3634 } 3635 3636 static void 3637 convert_constant_floating(op_t op, int arg, tspec_t ot, const type_t *tp, 3638 tspec_t nt, val_t *v, val_t *nv) 3639 { 3640 ldbl_t max = 0.0, min = 0.0; 3641 3642 switch (nt) { 3643 case CHAR: 3644 max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break; 3645 case UCHAR: 3646 max = TARG_UCHAR_MAX; min = 0; break; 3647 case SCHAR: 3648 max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break; 3649 case SHORT: 3650 max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break; 3651 case USHORT: 3652 max = TARG_USHRT_MAX; min = 0; break; 3653 case ENUM: 3654 case INT: 3655 max = TARG_INT_MAX; min = TARG_INT_MIN; break; 3656 case UINT: 3657 max = TARG_UINT_MAX; min = 0; break; 3658 case LONG: 3659 max = TARG_LONG_MAX; min = TARG_LONG_MIN; break; 3660 case ULONG: 3661 max = TARG_ULONG_MAX; min = 0; break; 3662 case QUAD: 3663 max = QUAD_MAX; min = QUAD_MIN; break; 3664 case UQUAD: 3665 max = UQUAD_MAX; min = 0; break; 3666 case FLOAT: 3667 case FCOMPLEX: 3668 max = FLT_MAX; min = -FLT_MAX; break; 3669 case DOUBLE: 3670 case DCOMPLEX: 3671 max = DBL_MAX; min = -DBL_MAX; break; 3672 case PTR: 3673 /* Got already an error because of float --> ptr */ 3674 case LDOUBLE: 3675 case LCOMPLEX: 3676 /* LINTED 248 */ 3677 max = LDBL_MAX; min = -max; break; 3678 default: 3679 lint_assert(/*CONSTCOND*/false); 3680 } 3681 if (v->v_ldbl > max || v->v_ldbl < min) { 3682 lint_assert(nt != LDOUBLE); 3683 if (op == FARG) { 3684 /* conversion of '%s' to '%s' is out of range, ... */ 3685 warning(295, 3686 type_name(gettyp(ot)), type_name(tp), arg); 3687 } else { 3688 /* conversion of '%s' to '%s' is out of range */ 3689 warning(119, 3690 type_name(gettyp(ot)), type_name(tp)); 3691 } 3692 v->v_ldbl = v->v_ldbl > 0 ? max : min; 3693 } 3694 3695 if (nt == FLOAT) { 3696 nv->v_ldbl = (float)v->v_ldbl; 3697 } else if (nt == DOUBLE) { 3698 nv->v_ldbl = (double)v->v_ldbl; 3699 } else if (nt == LDOUBLE) { 3700 nv->v_ldbl = v->v_ldbl; 3701 } else { 3702 nv->v_quad = (int64_t)v->v_ldbl; 3703 } 3704 } 3705 3706 static bool 3707 convert_constant_to_floating(tspec_t nt, val_t *nv, 3708 tspec_t ot, const val_t *v) 3709 { 3710 if (nt == FLOAT) { 3711 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ? 3712 (float)(uint64_t)v->v_quad : (float)v->v_quad; 3713 } else if (nt == DOUBLE) { 3714 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ? 3715 (double)(uint64_t)v->v_quad : (double)v->v_quad; 3716 } else if (nt == LDOUBLE) { 3717 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ? 3718 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad; 3719 } else 3720 return false; 3721 return true; 3722 } 3723 3724 /* 3725 * Print a warning if bits which were set are lost due to the conversion. 3726 * This can happen with operator ORASS only. 3727 */ 3728 static void 3729 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v, 3730 uint64_t xmask, op_t op) 3731 { 3732 if (nsz < osz && (v->v_quad & xmask) != 0) { 3733 /* constant truncated by conversion, op '%s' */ 3734 warning(306, op_name(op)); 3735 } 3736 } 3737 3738 /* 3739 * Print a warning if additional bits are not all 1 3740 * and the most significant bit of the old value is 1, 3741 * or if at least one (but not all) removed bit was 0. 3742 */ 3743 static void 3744 convert_constant_check_range_bitand(size_t nsz, size_t osz, 3745 uint64_t xmask, const val_t *nv, 3746 tspec_t ot, const val_t *v, 3747 const type_t *tp, op_t op) 3748 { 3749 if (nsz > osz && 3750 (nv->v_quad & bit((unsigned int)(osz - 1))) != 0 && 3751 (nv->v_quad & xmask) != xmask) { 3752 /* extra bits set to 0 in conversion of '%s' to '%s', ... */ 3753 warning(309, type_name(gettyp(ot)), 3754 type_name(tp), op_name(op)); 3755 } else if (nsz < osz && 3756 (v->v_quad & xmask) != xmask && 3757 (v->v_quad & xmask) != 0) { 3758 /* constant truncated by conversion, op '%s' */ 3759 warning(306, op_name(op)); 3760 } 3761 } 3762 3763 static void 3764 convert_constant_check_range_signed(op_t op, int arg) 3765 { 3766 if (op == ASSIGN) { 3767 /* assignment of negative constant to unsigned type */ 3768 warning(164); 3769 } else if (op == INIT) { 3770 /* initialization of unsigned with negative constant */ 3771 warning(221); 3772 } else if (op == FARG) { 3773 /* conversion of negative constant to unsigned type, ... */ 3774 warning(296, arg); 3775 } else if (modtab[op].m_comparison) { 3776 /* handled by check_integer_comparison() */ 3777 } else { 3778 /* conversion of negative constant to unsigned type */ 3779 warning(222); 3780 } 3781 } 3782 3783 /* 3784 * Loss of significant bit(s). All truncated bits 3785 * of unsigned types or all truncated bits plus the 3786 * msb of the target for signed types are considered 3787 * to be significant bits. Loss of significant bits 3788 * means that at least one of the bits was set in an 3789 * unsigned type or that at least one but not all of 3790 * the bits was set in a signed type. 3791 * Loss of significant bits means that it is not 3792 * possible, also not with necessary casts, to convert 3793 * back to the original type. A example for a 3794 * necessary cast is: 3795 * char c; int i; c = 128; 3796 * i = c; ** yields -128 ** 3797 * i = (unsigned char)c; ** yields 128 ** 3798 */ 3799 static void 3800 convert_constant_check_range_truncated(op_t op, int arg, const type_t *tp, 3801 tspec_t ot) 3802 { 3803 if (op == ASSIGN && tp->t_bitfield) { 3804 /* precision lost in bit-field assignment */ 3805 warning(166); 3806 } else if (op == ASSIGN) { 3807 /* constant truncated by assignment */ 3808 warning(165); 3809 } else if (op == INIT && tp->t_bitfield) { 3810 /* bit-field initializer does not fit */ 3811 warning(180); 3812 } else if (op == INIT) { 3813 /* initializer does not fit */ 3814 warning(178); 3815 } else if (op == CASE) { 3816 /* case label affected by conversion */ 3817 warning(196); 3818 } else if (op == FARG) { 3819 /* conversion of '%s' to '%s' is out of range, arg #%d */ 3820 warning(295, 3821 type_name(gettyp(ot)), type_name(tp), arg); 3822 } else { 3823 /* conversion of '%s' to '%s' is out of range */ 3824 warning(119, 3825 type_name(gettyp(ot)), type_name(tp)); 3826 } 3827 } 3828 3829 static void 3830 convert_constant_check_range_loss(op_t op, int arg, const type_t *tp, 3831 tspec_t ot) 3832 { 3833 if (op == ASSIGN && tp->t_bitfield) { 3834 /* precision lost in bit-field assignment */ 3835 warning(166); 3836 } else if (op == INIT && tp->t_bitfield) { 3837 /* bit-field initializer out of range */ 3838 warning(11); 3839 } else if (op == CASE) { 3840 /* case label affected by conversion */ 3841 warning(196); 3842 } else if (op == FARG) { 3843 /* conversion of '%s' to '%s' is out of range, arg #%d */ 3844 warning(295, 3845 type_name(gettyp(ot)), type_name(tp), arg); 3846 } else { 3847 /* conversion of '%s' to '%s' is out of range */ 3848 warning(119, 3849 type_name(gettyp(ot)), type_name(tp)); 3850 } 3851 } 3852 3853 static void 3854 convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt, 3855 op_t op, int arg, const val_t *v, val_t *nv) 3856 { 3857 unsigned int obitsz, nbitsz; 3858 uint64_t xmask, xmsk1; 3859 3860 obitsz = size_in_bits(ot); 3861 nbitsz = tp->t_bitfield ? tp->t_flen : size_in_bits(nt); 3862 xmask = value_bits(nbitsz) ^ value_bits(obitsz); 3863 xmsk1 = value_bits(nbitsz) ^ value_bits(obitsz - 1); 3864 /* 3865 * For bitwise operations we are not interested in the arithmetic 3866 * value, but in the bits itself. 3867 */ 3868 if (op == ORASS || op == BITOR || op == BITXOR) { 3869 convert_constant_check_range_bitor( 3870 nbitsz, obitsz, v, xmask, op); 3871 } else if (op == ANDASS || op == BITAND) { 3872 convert_constant_check_range_bitand( 3873 nbitsz, obitsz, xmask, nv, ot, v, tp, op); 3874 } else if ((nt != PTR && is_uinteger(nt)) && 3875 (ot != PTR && !is_uinteger(ot)) && 3876 v->v_quad < 0) { 3877 convert_constant_check_range_signed(op, arg); 3878 } else if (nv->v_quad != v->v_quad && nbitsz <= obitsz && 3879 (v->v_quad & xmask) != 0 && 3880 (is_uinteger(ot) || (v->v_quad & xmsk1) != xmsk1)) { 3881 convert_constant_check_range_truncated(op, arg, tp, ot); 3882 } else if (nv->v_quad != v->v_quad) { 3883 convert_constant_check_range_loss(op, arg, tp, ot); 3884 } 3885 } 3886 3887 /* 3888 * Converts a typed constant to a constant of another type. 3889 * 3890 * op operator which requires conversion 3891 * arg if op is FARG, # of argument 3892 * tp type in which to convert the constant 3893 * nv new constant 3894 * v old constant 3895 */ 3896 void 3897 convert_constant(op_t op, int arg, const type_t *tp, val_t *nv, val_t *v) 3898 { 3899 tspec_t ot, nt; 3900 unsigned int sz; 3901 bool range_check; 3902 3903 /* 3904 * TODO: make 'v' const; the name of this function does not suggest 3905 * that it modifies 'v'. 3906 */ 3907 ot = v->v_tspec; 3908 nt = nv->v_tspec = tp->t_tspec; 3909 range_check = false; 3910 3911 if (nt == BOOL) { /* C99 6.3.1.2 */ 3912 nv->v_unsigned_since_c90 = false; 3913 nv->v_quad = is_nonzero_val(v) ? 1 : 0; 3914 return; 3915 } 3916 3917 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) { 3918 convert_constant_floating(op, arg, ot, tp, nt, v, nv); 3919 } else if (!convert_constant_to_floating(nt, nv, ot, v)) { 3920 range_check = true; /* Check for lost precision. */ 3921 nv->v_quad = v->v_quad; 3922 } 3923 3924 if (allow_trad && allow_c90 && v->v_unsigned_since_c90 && 3925 (is_floating(nt) || ( 3926 (is_integer(nt) && !is_uinteger(nt) && 3927 portable_size_in_bits(nt) > portable_size_in_bits(ot))))) { 3928 /* ANSI C treats constant as unsigned */ 3929 warning(157); 3930 v->v_unsigned_since_c90 = false; 3931 } 3932 3933 if (is_integer(nt)) { 3934 sz = tp->t_bitfield ? tp->t_flen : size_in_bits(nt); 3935 nv->v_quad = convert_integer(nv->v_quad, nt, sz); 3936 } 3937 3938 if (range_check && op != CVT) 3939 convert_constant_check_range(ot, tp, nt, op, arg, v, nv); 3940 } 3941 3942 /* 3943 * Create a constant node for sizeof. 3944 */ 3945 tnode_t * 3946 build_sizeof(const type_t *tp) 3947 { 3948 unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE; 3949 tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes); 3950 tn->tn_system_dependent = true; 3951 debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes); 3952 return tn; 3953 } 3954 3955 /* 3956 * Create a constant node for offsetof. 3957 */ 3958 /* ARGSUSED */ /* FIXME: See implementation comments. */ 3959 tnode_t * 3960 build_offsetof(const type_t *tp, const sym_t *sym) 3961 { 3962 unsigned int offset_in_bytes; 3963 tnode_t *tn; 3964 3965 if (!is_struct_or_union(tp->t_tspec)) 3966 /* unacceptable operand of '%s' */ 3967 error(111, "offsetof"); 3968 3969 /* FIXME: Don't wrongly use the size of the whole type, use sym. */ 3970 offset_in_bytes = type_size_in_bits(tp) / CHAR_SIZE; 3971 tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes); 3972 tn->tn_system_dependent = true; 3973 return tn; 3974 } 3975 3976 unsigned int 3977 type_size_in_bits(const type_t *tp) 3978 { 3979 unsigned int elem, elsz; 3980 bool flex; 3981 3982 elem = 1; 3983 flex = false; 3984 lint_assert(tp != NULL); 3985 while (tp->t_tspec == ARRAY) { 3986 flex = true; /* allow c99 flex arrays [] [0] */ 3987 elem *= tp->t_dim; 3988 tp = tp->t_subt; 3989 } 3990 if (elem == 0) { 3991 if (!flex) { 3992 /* cannot take size/alignment of incomplete type */ 3993 error(143); 3994 elem = 1; 3995 } 3996 } 3997 switch (tp->t_tspec) { 3998 case FUNC: 3999 /* cannot take size/alignment of function type '%s' */ 4000 error(144, type_name(tp)); 4001 elsz = 1; 4002 break; 4003 case STRUCT: 4004 case UNION: 4005 if (is_incomplete(tp)) { 4006 /* cannot take size/alignment of incomplete type */ 4007 error(143); 4008 elsz = 1; 4009 } else { 4010 elsz = tp->t_str->sou_size_in_bits; 4011 } 4012 break; 4013 case ENUM: 4014 if (is_incomplete(tp)) { 4015 /* cannot take size/alignment of incomplete type */ 4016 warning(143); 4017 } 4018 /* FALLTHROUGH */ 4019 default: 4020 if (tp->t_bitfield) { 4021 /* cannot take size/alignment of bit-field */ 4022 error(145); 4023 } 4024 if (tp->t_tspec == VOID) { 4025 /* cannot take size/alignment of void */ 4026 error(146); 4027 elsz = 1; 4028 } else { 4029 elsz = size_in_bits(tp->t_tspec); 4030 lint_assert(elsz > 0); 4031 } 4032 break; 4033 } 4034 4035 return elem * elsz; 4036 } 4037 4038 tnode_t * 4039 build_alignof(const type_t *tp) 4040 { 4041 switch (tp->t_tspec) { 4042 case ARRAY: 4043 break; 4044 4045 case FUNC: 4046 /* cannot take size/alignment of function type '%s' */ 4047 error(144, type_name(tp)); 4048 return 0; 4049 4050 case STRUCT: 4051 case UNION: 4052 if (is_incomplete(tp)) { 4053 /* cannot take size/alignment of incomplete type */ 4054 error(143); 4055 return 0; 4056 } 4057 break; 4058 case ENUM: 4059 break; 4060 default: 4061 if (tp->t_bitfield) { 4062 /* cannot take size/alignment of bit-field */ 4063 error(145); 4064 return 0; 4065 } 4066 if (tp->t_tspec == VOID) { 4067 /* cannot take size/alignment of void */ 4068 error(146); 4069 return 0; 4070 } 4071 break; 4072 } 4073 4074 return build_integer_constant(SIZEOF_TSPEC, 4075 (int64_t)alignment_in_bits(tp) / CHAR_SIZE); 4076 } 4077 4078 static tnode_t * 4079 cast_to_union(const tnode_t *otn, type_t *ntp) 4080 { 4081 4082 if (!allow_gcc) { 4083 /* union cast is a GCC extension */ 4084 error(328); 4085 return NULL; 4086 } 4087 4088 for (const sym_t *m = ntp->t_str->sou_first_member; 4089 m != NULL; m = m->s_next) { 4090 if (types_compatible(m->s_type, otn->tn_type, 4091 false, false, NULL)) { 4092 tnode_t *ntn = expr_alloc_tnode(); 4093 ntn->tn_op = CVT; 4094 ntn->tn_type = ntp; 4095 ntn->tn_cast = true; 4096 ntn->tn_right = NULL; 4097 return ntn; 4098 } 4099 } 4100 4101 /* type '%s' is not a member of '%s' */ 4102 error(329, type_name(otn->tn_type), type_name(ntp)); 4103 return NULL; 4104 } 4105 4106 /* 4107 * Type casts. 4108 */ 4109 tnode_t * 4110 cast(tnode_t *tn, type_t *tp) 4111 { 4112 tspec_t nt, ot; 4113 4114 if (tn == NULL) 4115 return NULL; 4116 4117 tn = cconv(tn); 4118 4119 lint_assert(tp != NULL); 4120 nt = tp->t_tspec; 4121 ot = tn->tn_type->t_tspec; 4122 4123 if (nt == VOID) { 4124 /* 4125 * C90 6.3.4, C99 6.5.4p2 and C11 6.5.4p2 allow any type to 4126 * be cast to void. The only other allowed casts are from a 4127 * scalar type to a scalar type. 4128 */ 4129 } else if (nt == UNION) { 4130 return cast_to_union(tn, tp); 4131 } else if (nt == STRUCT || nt == ARRAY || nt == FUNC) { 4132 /* Casting to a struct is an undocumented GCC extension. */ 4133 if (!(allow_gcc && nt == STRUCT)) 4134 goto invalid_cast; 4135 } else if (is_struct_or_union(ot)) { 4136 goto invalid_cast; 4137 } else if (ot == VOID) { 4138 /* improper cast of void expression */ 4139 error(148); 4140 return NULL; 4141 } else if (is_integer(nt) && is_scalar(ot)) { 4142 /* ok */ 4143 } else if (is_floating(nt) && is_arithmetic(ot)) { 4144 /* ok */ 4145 } else if (nt == PTR && is_integer(ot)) { 4146 /* ok */ 4147 } else if (nt == PTR && ot == PTR) { 4148 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) { 4149 if (hflag) 4150 /* cast discards 'const' from type '%s' */ 4151 warning(275, type_name(tn->tn_type)); 4152 } 4153 } else 4154 goto invalid_cast; 4155 4156 if (any_query_enabled && types_compatible(tp, tn->tn_type, 4157 false, false, NULL)) { 4158 /* no-op cast from '%s' to '%s' */ 4159 query_message(6, type_name(tn->tn_type), type_name(tp)); 4160 } 4161 4162 tn = convert(CVT, 0, tp, tn); 4163 tn->tn_cast = true; 4164 4165 return tn; 4166 4167 invalid_cast: 4168 /* invalid cast from '%s' to '%s' */ 4169 error(147, type_name(tn->tn_type), type_name(tp)); 4170 return NULL; 4171 } 4172 4173 /* 4174 * Create the node for a function argument. 4175 * All necessary conversions and type checks are done in 4176 * build_function_call because build_function_argument has no 4177 * information about expected argument types. 4178 */ 4179 tnode_t * 4180 build_function_argument(tnode_t *args, tnode_t *arg) 4181 { 4182 /* 4183 * If there was a serious error in the expression for the argument, 4184 * create a dummy argument so the positions of the remaining arguments 4185 * will not change. 4186 */ 4187 if (arg == NULL) 4188 arg = build_integer_constant(INT, 0); 4189 4190 return new_tnode(PUSH, arg->tn_sys, arg->tn_type, arg, args); 4191 } 4192 4193 /* 4194 * Compare the type of an argument with the corresponding type of a 4195 * prototype parameter. If it is a valid combination, but both types 4196 * are not the same, insert a conversion to convert the argument into 4197 * the type of the parameter. 4198 */ 4199 static tnode_t * 4200 check_prototype_argument( 4201 int n, /* pos of arg */ 4202 type_t *tp, /* expected type (from prototype) */ 4203 tnode_t *tn) /* argument */ 4204 { 4205 tnode_t *ln; 4206 bool dowarn; 4207 4208 ln = xcalloc(1, sizeof(*ln)); 4209 ln->tn_type = expr_unqualified_type(tp); 4210 ln->tn_lvalue = true; 4211 if (typeok(FARG, n, ln, tn)) { 4212 if (!types_compatible(tp, tn->tn_type, 4213 true, false, (dowarn = false, &dowarn)) || dowarn) 4214 tn = convert(FARG, n, tp, tn); 4215 } 4216 free(ln); 4217 return tn; 4218 } 4219 4220 /* 4221 * Check types of all function arguments and insert conversions, 4222 * if necessary. 4223 */ 4224 static tnode_t * 4225 check_function_arguments(type_t *ftp, tnode_t *args) 4226 { 4227 tnode_t *arg; 4228 sym_t *asym; 4229 tspec_t at; 4230 int narg, npar, n, i; 4231 4232 /* get # of args in the prototype */ 4233 npar = 0; 4234 for (asym = ftp->t_args; asym != NULL; asym = asym->s_next) 4235 npar++; 4236 4237 /* get # of args in function call */ 4238 narg = 0; 4239 for (arg = args; arg != NULL; arg = arg->tn_right) 4240 narg++; 4241 4242 asym = ftp->t_args; 4243 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) { 4244 /* argument mismatch: %d %s passed, %d expected */ 4245 error(150, narg, narg > 1 ? "arguments" : "argument", npar); 4246 asym = NULL; 4247 } 4248 4249 for (n = 1; n <= narg; n++) { 4250 4251 /* 4252 * The rightmost argument is at the top of the argument 4253 * subtree. 4254 */ 4255 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) 4256 continue; 4257 4258 /* some things which are always not allowed */ 4259 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) { 4260 /* void expressions may not be arguments, arg #%d */ 4261 error(151, n); 4262 return NULL; 4263 } else if (is_struct_or_union(at) && 4264 is_incomplete(arg->tn_left->tn_type)) { 4265 /* argument cannot have unknown size, arg #%d */ 4266 error(152, n); 4267 return NULL; 4268 } else if (is_integer(at) && 4269 arg->tn_left->tn_type->t_is_enum && 4270 is_incomplete(arg->tn_left->tn_type)) { 4271 /* argument cannot have unknown size, arg #%d */ 4272 warning(152, n); 4273 } 4274 4275 /* class conversions (arg in value context) */ 4276 arg->tn_left = cconv(arg->tn_left); 4277 4278 if (asym != NULL) { 4279 arg->tn_left = check_prototype_argument( 4280 n, asym->s_type, arg->tn_left); 4281 } else { 4282 arg->tn_left = promote(NOOP, true, arg->tn_left); 4283 } 4284 arg->tn_type = arg->tn_left->tn_type; 4285 4286 if (asym != NULL) 4287 asym = asym->s_next; 4288 } 4289 4290 return args; 4291 } 4292 4293 /* 4294 * Create the node for a function call. Also check types of 4295 * function arguments and insert conversions, if necessary. 4296 */ 4297 tnode_t * 4298 build_function_call(tnode_t *func, bool sys, tnode_t *args) 4299 { 4300 tnode_t *ntn; 4301 op_t fcop; 4302 4303 if (func == NULL) 4304 return NULL; 4305 4306 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) { 4307 fcop = CALL; 4308 } else { 4309 fcop = ICALL; 4310 } 4311 4312 check_ctype_function_call(func, args); 4313 4314 /* 4315 * after cconv() func will always be a pointer to a function 4316 * if it is a valid function designator. 4317 */ 4318 func = cconv(func); 4319 4320 if (func->tn_type->t_tspec != PTR || 4321 func->tn_type->t_subt->t_tspec != FUNC) { 4322 /* cannot call '%s', must be a function */ 4323 error(149, type_name(func->tn_type)); 4324 return NULL; 4325 } 4326 4327 args = check_function_arguments(func->tn_type->t_subt, args); 4328 4329 ntn = new_tnode(fcop, sys, func->tn_type->t_subt->t_subt, func, args); 4330 4331 return ntn; 4332 } 4333 4334 /* 4335 * Return the value of an integral constant expression. 4336 * If the expression is not constant or its type is not an integer 4337 * type, an error message is printed. 4338 */ 4339 val_t * 4340 constant(tnode_t *tn, bool required) 4341 { 4342 val_t *v; 4343 4344 if (tn != NULL) 4345 tn = cconv(tn); 4346 if (tn != NULL) 4347 tn = promote(NOOP, false, tn); 4348 4349 v = xcalloc(1, sizeof(*v)); 4350 4351 if (tn == NULL) { 4352 lint_assert(nerr != 0); 4353 debug_step("constant node is null; returning 1 instead"); 4354 v->v_tspec = INT; 4355 v->v_quad = 1; 4356 return v; 4357 } 4358 4359 v->v_tspec = tn->tn_type->t_tspec; 4360 4361 if (tn->tn_op == CON) { 4362 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec); 4363 if (is_integer(tn->tn_val->v_tspec)) { 4364 v->v_unsigned_since_c90 = 4365 tn->tn_val->v_unsigned_since_c90; 4366 v->v_quad = tn->tn_val->v_quad; 4367 return v; 4368 } 4369 v->v_quad = tn->tn_val->v_ldbl; 4370 } else { 4371 v->v_quad = 1; 4372 } 4373 4374 if (required) 4375 /* integral constant expression expected */ 4376 error(55); 4377 else 4378 /* variable array dimension is a C99/GCC extension */ 4379 c99ism(318); 4380 4381 if (!is_integer(v->v_tspec)) 4382 v->v_tspec = INT; 4383 4384 return v; 4385 } 4386 4387 static bool 4388 is_constcond_false(const tnode_t *tn, tspec_t t) 4389 { 4390 return (t == BOOL || t == INT) && 4391 tn->tn_op == CON && tn->tn_val->v_quad == 0; 4392 } 4393 4394 /* 4395 * Perform some tests on expressions which can't be done in build_binary() 4396 * and functions called by build_binary(). These tests must be done here 4397 * because we need some information about the context in which the operations 4398 * are performed. 4399 * After all tests are performed and dofreeblk is true, expr() frees the 4400 * memory which is used for the expression. 4401 */ 4402 void 4403 expr(tnode_t *tn, bool vctx, bool cond, bool dofreeblk, bool is_do_while) 4404 { 4405 4406 if (tn == NULL) { /* in case of errors */ 4407 expr_free_all(); 4408 return; 4409 } 4410 4411 /* expr() is also called in global initializations */ 4412 if (dcs->d_kind != DK_EXTERN && !is_do_while) 4413 check_statement_reachable(); 4414 4415 check_expr_misc(tn, vctx, cond, !cond, false, false, false); 4416 if (tn->tn_op == ASSIGN) { 4417 if (hflag && cond) 4418 /* assignment in conditional context */ 4419 warning(159); 4420 } else if (tn->tn_op == CON) { 4421 if (hflag && cond && !constcond_flag && 4422 !tn->tn_system_dependent && 4423 !(is_do_while && 4424 is_constcond_false(tn, tn->tn_type->t_tspec))) 4425 /* constant in conditional context */ 4426 warning(161); 4427 } 4428 if (!modtab[tn->tn_op].m_has_side_effect) { 4429 /* 4430 * for left operands of COMMA this warning is already 4431 * printed 4432 */ 4433 if (tn->tn_op != COMMA && !vctx && !cond) 4434 check_null_effect(tn); 4435 } 4436 debug_node(tn); 4437 4438 /* free the tree memory */ 4439 if (dofreeblk) 4440 expr_free_all(); 4441 } 4442 4443 /* 4444 * Checks the range of array indices, if possible. 4445 * amper is set if only the address of the element is used. This 4446 * means that the index is allowed to refer to the first element 4447 * after the array. 4448 */ 4449 static void 4450 check_array_index(tnode_t *tn, bool amper) 4451 { 4452 int dim; 4453 tnode_t *ln, *rn; 4454 int elsz; 4455 int64_t con; 4456 4457 ln = tn->tn_left; 4458 rn = tn->tn_right; 4459 4460 /* We can only check constant indices. */ 4461 if (rn->tn_op != CON) 4462 return; 4463 4464 /* Return if the left node does not stem from an array. */ 4465 if (ln->tn_op != ADDR) 4466 return; 4467 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME) 4468 return; 4469 if (ln->tn_left->tn_type->t_tspec != ARRAY) 4470 return; 4471 4472 /* 4473 * For incomplete array types, we can print a warning only if 4474 * the index is negative. 4475 */ 4476 if (is_incomplete(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0) 4477 return; 4478 4479 /* Get the size of one array element */ 4480 if ((elsz = length_in_bits(ln->tn_type->t_subt, NULL)) == 0) 4481 return; 4482 elsz /= CHAR_SIZE; 4483 4484 /* Change the unit of the index from bytes to element size. */ 4485 if (is_uinteger(rn->tn_type->t_tspec)) { 4486 con = (uint64_t)rn->tn_val->v_quad / elsz; 4487 } else { 4488 con = rn->tn_val->v_quad / elsz; 4489 } 4490 4491 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0); 4492 4493 if (!is_uinteger(rn->tn_type->t_tspec) && con < 0) { 4494 /* array subscript cannot be negative: %ld */ 4495 warning(167, (long)con); 4496 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) { 4497 /* array subscript cannot be > %d: %ld */ 4498 warning(168, dim - 1, (long)con); 4499 } 4500 } 4501 4502 static void 4503 check_expr_addr(const tnode_t *ln, bool szof, bool fcall) 4504 { 4505 /* XXX: Taking warn_about_unreachable into account here feels wrong. */ 4506 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) { 4507 if (!szof) 4508 mark_as_set(ln->tn_sym); 4509 mark_as_used(ln->tn_sym, fcall, szof); 4510 } 4511 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS) 4512 /* check the range of array indices */ 4513 check_array_index(ln->tn_left, true); 4514 } 4515 4516 static void 4517 check_expr_load(const tnode_t *ln) 4518 { 4519 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS) 4520 /* check the range of array indices */ 4521 check_array_index(ln->tn_left, false); 4522 } 4523 4524 static void 4525 check_expr_side_effect(const tnode_t *ln, bool szof) 4526 { 4527 scl_t sc; 4528 dinfo_t *di; 4529 4530 /* XXX: Taking warn_about_unreachable into account here feels wrong. */ 4531 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) { 4532 sc = ln->tn_sym->s_scl; 4533 /* 4534 * Look if there was a asm statement in one of the 4535 * compound statements we are in. If not, we don't 4536 * print a warning. 4537 */ 4538 for (di = dcs; di != NULL; di = di->d_enclosing) { 4539 if (di->d_asm) 4540 break; 4541 } 4542 if (sc != EXTERN && sc != STATIC && 4543 !ln->tn_sym->s_set && !szof && di == NULL) { 4544 /* '%s' may be used before set */ 4545 warning(158, ln->tn_sym->s_name); 4546 mark_as_set(ln->tn_sym); 4547 } 4548 mark_as_used(ln->tn_sym, false, false); 4549 } 4550 } 4551 4552 static void 4553 check_expr_assign(const tnode_t *ln, bool szof) 4554 { 4555 /* XXX: Taking warn_about_unreachable into account here feels wrong. */ 4556 if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) { 4557 mark_as_set(ln->tn_sym); 4558 if (ln->tn_sym->s_scl == EXTERN) 4559 outusg(ln->tn_sym); 4560 } 4561 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS) 4562 /* check the range of array indices */ 4563 check_array_index(ln->tn_left, false); 4564 } 4565 4566 static void 4567 check_expr_call(const tnode_t *tn, const tnode_t *ln, 4568 bool szof, bool vctx, bool cond, bool retval_discarded) 4569 { 4570 lint_assert(ln->tn_op == ADDR); 4571 lint_assert(ln->tn_left->tn_op == NAME); 4572 if (!szof && 4573 !is_compiler_builtin(ln->tn_left->tn_sym->s_name)) 4574 outcall(tn, vctx || cond, retval_discarded); 4575 } 4576 4577 static bool 4578 check_expr_op(const tnode_t *tn, op_t op, const tnode_t *ln, 4579 bool szof, bool fcall, bool vctx, bool cond, 4580 bool retval_discarded, bool eqwarn) 4581 { 4582 switch (op) { 4583 case ADDR: 4584 check_expr_addr(ln, szof, fcall); 4585 break; 4586 case LOAD: 4587 check_expr_load(ln); 4588 /* FALLTHROUGH */ 4589 case PUSH: 4590 case INCBEF: 4591 case DECBEF: 4592 case INCAFT: 4593 case DECAFT: 4594 case ADDASS: 4595 case SUBASS: 4596 case MULASS: 4597 case DIVASS: 4598 case MODASS: 4599 case ANDASS: 4600 case ORASS: 4601 case XORASS: 4602 case SHLASS: 4603 case SHRASS: 4604 case REAL: 4605 case IMAG: 4606 check_expr_side_effect(ln, szof); 4607 break; 4608 case ASSIGN: 4609 check_expr_assign(ln, szof); 4610 break; 4611 case CALL: 4612 check_expr_call(tn, ln, szof, vctx, cond, retval_discarded); 4613 break; 4614 case EQ: 4615 if (hflag && eqwarn) 4616 /* operator '==' found where '=' was expected */ 4617 warning(160); 4618 break; 4619 case CON: 4620 case NAME: 4621 case STRING: 4622 return false; 4623 default: 4624 break; 4625 } 4626 return true; 4627 } 4628 4629 /* 4630 * vctx ??? 4631 * cond whether the expression is a condition that 4632 * will be compared with 0 4633 * eqwarn whether the operator '==' might be a 4634 * misspelled '=' 4635 * fcall whether the expression is a function call 4636 * retval_discarded whether the return value of a function call 4637 * is discarded; such calls will be analyzed by 4638 * lint2 in messages 4, 8 and 9 4639 * szof whether the expression is part of a sizeof 4640 * expression, which means that its value is 4641 * discarded since only the type is relevant 4642 */ 4643 void 4644 check_expr_misc(const tnode_t *tn, bool vctx, bool cond, 4645 bool eqwarn, bool fcall, bool retval_discarded, bool szof) 4646 { 4647 tnode_t *ln, *rn; 4648 const mod_t *mp; 4649 op_t op; 4650 bool cvctx, ccond, eq, discard; 4651 4652 if (tn == NULL) 4653 return; 4654 4655 ln = tn->tn_left; 4656 rn = tn->tn_right; 4657 mp = &modtab[op = tn->tn_op]; 4658 4659 if (!check_expr_op(tn, op, ln, 4660 szof, fcall, vctx, cond, retval_discarded, eqwarn)) 4661 return; 4662 4663 cvctx = mp->m_value_context; 4664 ccond = mp->m_compares_with_zero; 4665 eq = mp->m_warn_if_operand_eq && 4666 !ln->tn_parenthesized && 4667 rn != NULL && !rn->tn_parenthesized; 4668 4669 /* 4670 * values of operands of ':' are not used if the type of at least 4671 * one of the operands (for gcc compatibility) is void 4672 * XXX test/value context of QUEST should probably be used as 4673 * context for both operands of COLON 4674 */ 4675 if (op == COLON && tn->tn_type->t_tspec == VOID) 4676 cvctx = ccond = false; 4677 discard = op == CVT && tn->tn_type->t_tspec == VOID; 4678 check_expr_misc(ln, cvctx, ccond, eq, op == CALL, discard, szof); 4679 4680 switch (op) { 4681 case PUSH: 4682 if (rn != NULL) 4683 check_expr_misc(rn, false, false, eq, false, false, 4684 szof); 4685 break; 4686 case LOGAND: 4687 case LOGOR: 4688 check_expr_misc(rn, false, true, eq, false, false, szof); 4689 break; 4690 case COLON: 4691 check_expr_misc(rn, cvctx, ccond, eq, false, false, szof); 4692 break; 4693 case COMMA: 4694 check_expr_misc(rn, vctx, cond, false, false, false, szof); 4695 break; 4696 default: 4697 if (mp->m_binary) 4698 check_expr_misc(rn, true, false, eq, false, false, 4699 szof); 4700 break; 4701 } 4702 } 4703 4704 /* 4705 * Return whether the expression can be used for static initialization. 4706 * 4707 * Constant initialization expressions must be constant or an address 4708 * of a static object with an optional offset. In the first case, 4709 * the result is returned in *offsp. In the second case, the static 4710 * object is returned in *symp and the offset in *offsp. 4711 * 4712 * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and 4713 * CON. Type conversions are allowed if they do not change binary 4714 * representation (including width). 4715 * 4716 * C99 6.6 "Constant expressions" 4717 * C99 6.7.8p4 restricts initializers for static storage duration 4718 */ 4719 bool 4720 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp) 4721 { 4722 const sym_t *sym; 4723 ptrdiff_t offs1, offs2; 4724 tspec_t t, ot; 4725 4726 switch (tn->tn_op) { 4727 case MINUS: 4728 if (tn->tn_right->tn_op == CVT) 4729 return constant_addr(tn->tn_right, symp, offsp); 4730 else if (tn->tn_right->tn_op != CON) 4731 return false; 4732 /* FALLTHROUGH */ 4733 case PLUS: 4734 offs1 = offs2 = 0; 4735 if (tn->tn_left->tn_op == CON) { 4736 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad; 4737 if (!constant_addr(tn->tn_right, &sym, &offs2)) 4738 return false; 4739 } else if (tn->tn_right->tn_op == CON) { 4740 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad; 4741 if (tn->tn_op == MINUS) 4742 offs2 = -offs2; 4743 if (!constant_addr(tn->tn_left, &sym, &offs1)) 4744 return false; 4745 } else { 4746 return false; 4747 } 4748 *symp = sym; 4749 *offsp = offs1 + offs2; 4750 return true; 4751 case ADDR: 4752 if (tn->tn_left->tn_op == NAME) { 4753 *symp = tn->tn_left->tn_sym; 4754 *offsp = 0; 4755 return true; 4756 } else { 4757 /* 4758 * If this would be the front end of a compiler we 4759 * would return a label instead of 0, at least if 4760 * 'tn->tn_left->tn_op == STRING'. 4761 */ 4762 *symp = NULL; 4763 *offsp = 0; 4764 return true; 4765 } 4766 case CVT: 4767 t = tn->tn_type->t_tspec; 4768 ot = tn->tn_left->tn_type->t_tspec; 4769 if ((!is_integer(t) && t != PTR) || 4770 (!is_integer(ot) && ot != PTR)) { 4771 return false; 4772 } 4773 #if 0 4774 /* 4775 * consider: 4776 * struct foo { 4777 * unsigned char a; 4778 * } f = { 4779 * (unsigned char)(unsigned long) 4780 * (&(((struct foo *)0)->a)) 4781 * }; 4782 * since psize(unsigned long) != psize(unsigned char), 4783 * this fails. 4784 */ 4785 else if (psize(t) != psize(ot)) 4786 return -1; 4787 #endif 4788 return constant_addr(tn->tn_left, symp, offsp); 4789 default: 4790 return false; 4791 } 4792 } 4793 4794 /* Append s2 to s1, then free s2. */ 4795 strg_t * 4796 cat_strings(strg_t *s1, strg_t *s2) 4797 { 4798 4799 if (s1->st_char != s2->st_char) { 4800 /* cannot concatenate wide and regular string literals */ 4801 error(292); 4802 return s1; 4803 } 4804 4805 size_t len1 = s1->st_len; 4806 size_t len2 = s2->st_len; 4807 size_t chsize = s1->st_char ? sizeof(char) : sizeof(wchar_t); 4808 size_t size1 = len1 * chsize; 4809 size_t size2 = (len2 + 1) * chsize; 4810 s1->st_mem = xrealloc(s1->st_mem, size1 + size2); 4811 memcpy((char *)s1->st_mem + size1, s2->st_mem, size2); 4812 free(s2->st_mem); 4813 4814 s1->st_len = len1 + len2; 4815 free(s2); 4816 4817 return s1; 4818 } 4819 4820 4821 typedef struct stmt_expr { 4822 memory_pool se_mem; 4823 sym_t *se_sym; 4824 struct stmt_expr *se_enclosing; 4825 } stmt_expr; 4826 4827 static stmt_expr *stmt_exprs; 4828 4829 void 4830 begin_statement_expr(void) 4831 { 4832 stmt_expr *se = xmalloc(sizeof(*se)); 4833 se->se_mem = expr_save_memory(); 4834 se->se_sym = NULL; 4835 se->se_enclosing = stmt_exprs; 4836 stmt_exprs = se; 4837 } 4838 4839 void 4840 do_statement_expr(tnode_t *tn) 4841 { 4842 block_level--; 4843 mem_block_level--; 4844 stmt_exprs->se_sym = tn != NULL 4845 ? mktempsym(block_dup_type(tn->tn_type)) 4846 : NULL; /* after a syntax error */ 4847 mem_block_level++; 4848 block_level++; 4849 /* ({ }) is a GCC extension */ 4850 gnuism(320); 4851 } 4852 4853 tnode_t * 4854 end_statement_expr(void) 4855 { 4856 stmt_expr *se = stmt_exprs; 4857 if (se->se_sym == NULL) 4858 return NULL; /* after a syntax error */ 4859 tnode_t *tn = build_name(se->se_sym, false); 4860 (void)expr_save_memory(); /* leak */ 4861 expr_restore_memory(se->se_mem); 4862 stmt_exprs = se->se_enclosing; 4863 free(se); 4864 return tn; 4865 } 4866