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