1 /* Build expressions with type checking for C compiler. 2 Copyright (C) 1987-2024 Free Software Foundation, Inc. 3 4 This file is part of GCC. 5 6 GCC is free software; you can redistribute it and/or modify it under 7 the terms of the GNU General Public License as published by the Free 8 Software Foundation; either version 3, or (at your option) any later 9 version. 10 11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12 WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GCC; see the file COPYING3. If not see 18 <http://www.gnu.org/licenses/>. */ 19 20 21 /* This file is part of the C front end. 22 It contains routines to build C expressions given their operands, 23 including computing the types of the result, C-specific error checks, 24 and some optimization. */ 25 26 #include "config.h" 27 #include "system.h" 28 #include "coretypes.h" 29 #include "memmodel.h" 30 #include "target.h" 31 #include "function.h" 32 #include "bitmap.h" 33 #include "c-tree.h" 34 #include "gimple-expr.h" 35 #include "predict.h" 36 #include "stor-layout.h" 37 #include "trans-mem.h" 38 #include "varasm.h" 39 #include "stmt.h" 40 #include "langhooks.h" 41 #include "c-lang.h" 42 #include "intl.h" 43 #include "tree-iterator.h" 44 #include "gimplify.h" 45 #include "tree-inline.h" 46 #include "omp-general.h" 47 #include "c-family/c-objc.h" 48 #include "c-family/c-ubsan.h" 49 #include "gomp-constants.h" 50 #include "spellcheck-tree.h" 51 #include "gcc-rich-location.h" 52 #include "stringpool.h" 53 #include "attribs.h" 54 #include "asan.h" 55 #include "realmpfr.h" 56 57 /* Possible cases of implicit conversions. Used to select diagnostic messages 58 and control folding initializers in convert_for_assignment. */ 59 enum impl_conv { 60 ic_argpass, 61 ic_assign, 62 ic_init, 63 ic_init_const, 64 ic_return 65 }; 66 67 /* The level of nesting inside "__alignof__". */ 68 int in_alignof; 69 70 /* The level of nesting inside "sizeof". */ 71 int in_sizeof; 72 73 /* The level of nesting inside "typeof". */ 74 int in_typeof; 75 76 /* True when parsing OpenMP loop expressions. */ 77 bool c_in_omp_for; 78 79 /* True when parsing OpenMP map clause. */ 80 bool c_omp_array_section_p; 81 82 /* The argument of last parsed sizeof expression, only to be tested 83 if expr.original_code == SIZEOF_EXPR. */ 84 tree c_last_sizeof_arg; 85 location_t c_last_sizeof_loc; 86 87 /* Nonzero if we might need to print a "missing braces around 88 initializer" message within this initializer. */ 89 static int found_missing_braces; 90 91 static bool require_constant_value; 92 static bool require_constant_elements; 93 static bool require_constexpr_value; 94 95 static tree qualify_type (tree, tree); 96 struct comptypes_data; 97 static bool tagged_types_tu_compatible_p (const_tree, const_tree, 98 struct comptypes_data *); 99 static bool comp_target_types (location_t, tree, tree); 100 static bool function_types_compatible_p (const_tree, const_tree, 101 struct comptypes_data *); 102 static bool type_lists_compatible_p (const_tree, const_tree, 103 struct comptypes_data *); 104 static tree lookup_field (tree, tree); 105 static int convert_arguments (location_t, vec<location_t>, tree, 106 vec<tree, va_gc> *, vec<tree, va_gc> *, tree, 107 tree); 108 static tree pointer_diff (location_t, tree, tree, tree *); 109 static tree convert_for_assignment (location_t, location_t, tree, tree, tree, 110 enum impl_conv, bool, tree, tree, int, 111 int = 0); 112 static tree valid_compound_expr_initializer (tree, tree); 113 static void push_string (const char *); 114 static void push_member_name (tree); 115 static int spelling_length (void); 116 static char *print_spelling (char *); 117 static void warning_init (location_t, int, const char *); 118 static tree digest_init (location_t, tree, tree, tree, bool, bool, bool, bool, 119 bool, bool); 120 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool, 121 bool, struct obstack *); 122 static void output_pending_init_elements (int, struct obstack *); 123 static bool set_designator (location_t, bool, struct obstack *); 124 static void push_range_stack (tree, struct obstack *); 125 static void add_pending_init (location_t, tree, tree, tree, bool, 126 struct obstack *); 127 static void set_nonincremental_init (struct obstack *); 128 static void set_nonincremental_init_from_string (tree, struct obstack *); 129 static tree find_init_member (tree, struct obstack *); 130 static void readonly_warning (tree, enum lvalue_use); 131 static int lvalue_or_else (location_t, const_tree, enum lvalue_use); 132 static void record_maybe_used_decl (tree); 133 static bool comptypes_internal (const_tree, const_tree, 134 struct comptypes_data *data); 135 136 /* Return true if EXP is a null pointer constant, false otherwise. */ 138 139 bool 140 null_pointer_constant_p (const_tree expr) 141 { 142 /* This should really operate on c_expr structures, but they aren't 143 yet available everywhere required. */ 144 tree type = TREE_TYPE (expr); 145 146 /* An integer constant expression with the value 0, such an expression 147 cast to type void*, or the predefined constant nullptr, are a null 148 pointer constant. */ 149 if (expr == nullptr_node) 150 return true; 151 152 return (TREE_CODE (expr) == INTEGER_CST 153 && !TREE_OVERFLOW (expr) 154 && integer_zerop (expr) 155 && (INTEGRAL_TYPE_P (type) 156 || (TREE_CODE (type) == POINTER_TYPE 157 && VOID_TYPE_P (TREE_TYPE (type)) 158 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED))); 159 } 160 161 /* EXPR may appear in an unevaluated part of an integer constant 162 expression, but not in an evaluated part. Wrap it in a 163 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an 164 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */ 165 166 static tree 167 note_integer_operands (tree expr) 168 { 169 tree ret; 170 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op) 171 { 172 ret = copy_node (expr); 173 TREE_OVERFLOW (ret) = 1; 174 } 175 else 176 { 177 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr); 178 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1; 179 } 180 return ret; 181 } 182 183 /* Having checked whether EXPR may appear in an unevaluated part of an 184 integer constant expression and found that it may, remove any 185 C_MAYBE_CONST_EXPR noting this fact and return the resulting 186 expression. */ 187 188 static inline tree 189 remove_c_maybe_const_expr (tree expr) 190 { 191 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) 192 return C_MAYBE_CONST_EXPR_EXPR (expr); 193 else 194 return expr; 195 } 196 197 /* This is a cache to hold if two types are seen. */ 199 200 struct tagged_tu_seen_cache { 201 const struct tagged_tu_seen_cache * next; 202 const_tree t1; 203 const_tree t2; 204 }; 205 206 /* Do `exp = require_complete_type (loc, exp);' to make sure exp 207 does not have an incomplete type. (That includes void types.) 208 LOC is the location of the use. */ 209 210 tree 211 require_complete_type (location_t loc, tree value) 212 { 213 tree type = TREE_TYPE (value); 214 215 if (error_operand_p (value)) 216 return error_mark_node; 217 218 /* First, detect a valid value with a complete type. */ 219 if (COMPLETE_TYPE_P (type)) 220 return value; 221 222 c_incomplete_type_error (loc, value, type); 223 return error_mark_node; 224 } 225 226 /* Print an error message for invalid use of an incomplete type. 227 VALUE is the expression that was used (or 0 if that isn't known) 228 and TYPE is the type that was invalid. LOC is the location for 229 the error. */ 230 231 void 232 c_incomplete_type_error (location_t loc, const_tree value, const_tree type) 233 { 234 /* Avoid duplicate error message. */ 235 if (TREE_CODE (type) == ERROR_MARK) 236 return; 237 238 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL)) 239 error_at (loc, "%qD has an incomplete type %qT", value, type); 240 else 241 { 242 retry: 243 /* We must print an error message. Be clever about what it says. */ 244 245 switch (TREE_CODE (type)) 246 { 247 case RECORD_TYPE: 248 case UNION_TYPE: 249 case ENUMERAL_TYPE: 250 break; 251 252 case VOID_TYPE: 253 error_at (loc, "invalid use of void expression"); 254 return; 255 256 case ARRAY_TYPE: 257 if (TYPE_DOMAIN (type)) 258 { 259 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL) 260 { 261 error_at (loc, "invalid use of flexible array member"); 262 return; 263 } 264 type = TREE_TYPE (type); 265 goto retry; 266 } 267 error_at (loc, "invalid use of array with unspecified bounds"); 268 return; 269 270 default: 271 gcc_unreachable (); 272 } 273 274 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) 275 error_at (loc, "invalid use of undefined type %qT", type); 276 else 277 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ 278 error_at (loc, "invalid use of incomplete typedef %qT", type); 279 } 280 } 281 282 /* Given a type, apply default promotions wrt unnamed function 283 arguments and return the new type. */ 284 285 tree 286 c_type_promotes_to (tree type) 287 { 288 tree ret = NULL_TREE; 289 290 if (TYPE_MAIN_VARIANT (type) == float_type_node) 291 ret = double_type_node; 292 else if (c_promoting_integer_type_p (type)) 293 { 294 /* Preserve unsignedness if not really getting any wider. */ 295 if (TYPE_UNSIGNED (type) 296 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) 297 ret = unsigned_type_node; 298 else 299 ret = integer_type_node; 300 } 301 302 if (ret != NULL_TREE) 303 return (TYPE_ATOMIC (type) 304 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC) 305 : ret); 306 307 return type; 308 } 309 310 /* Return true if between two named address spaces, whether there is a superset 311 named address space that encompasses both address spaces. If there is a 312 superset, return which address space is the superset. */ 313 314 static bool 315 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common) 316 { 317 if (as1 == as2) 318 { 319 *common = as1; 320 return true; 321 } 322 else if (targetm.addr_space.subset_p (as1, as2)) 323 { 324 *common = as2; 325 return true; 326 } 327 else if (targetm.addr_space.subset_p (as2, as1)) 328 { 329 *common = as1; 330 return true; 331 } 332 else 333 return false; 334 } 335 336 /* Return a variant of TYPE which has all the type qualifiers of LIKE 337 as well as those of TYPE. */ 338 339 static tree 340 qualify_type (tree type, tree like) 341 { 342 addr_space_t as_type = TYPE_ADDR_SPACE (type); 343 addr_space_t as_like = TYPE_ADDR_SPACE (like); 344 addr_space_t as_common; 345 346 /* If the two named address spaces are different, determine the common 347 superset address space. If there isn't one, raise an error. */ 348 if (!addr_space_superset (as_type, as_like, &as_common)) 349 { 350 as_common = as_type; 351 error ("%qT and %qT are in disjoint named address spaces", 352 type, like); 353 } 354 355 return c_build_qualified_type (type, 356 TYPE_QUALS_NO_ADDR_SPACE (type) 357 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like) 358 | ENCODE_QUAL_ADDR_SPACE (as_common)); 359 } 360 361 362 /* Check consistency of type TYPE. For derived types, we test that 364 C_TYPE_VARIABLE_SIZE and C_TYPE_VARIABLY_MODIFIED are consistent with 365 the requirements of the base type. We also check that arrays with a 366 non-constant length are marked with C_TYPE_VARIABLE_SIZE. If any 367 inconsistency is detected false is returned and true otherwise. */ 368 369 static bool 370 c_verify_type (tree type) 371 { 372 switch (TREE_CODE (type)) 373 { 374 case POINTER_TYPE: 375 case FUNCTION_TYPE: 376 /* Pointer and funcions can not have variable size. */ 377 if (C_TYPE_VARIABLE_SIZE (type)) 378 return false; 379 /* Pointer and funcions are variably modified if and only if the 380 return / target type is variably modified. */ 381 if (C_TYPE_VARIABLY_MODIFIED (type) 382 != C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (type))) 383 return false; 384 break; 385 case ARRAY_TYPE: 386 /* An array has variable size if and only if it has a non-constant 387 dimensions or its element type has variable size. */ 388 if ((C_TYPE_VARIABLE_SIZE (TREE_TYPE (type)) 389 || (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) 390 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) 391 != INTEGER_CST)) 392 != C_TYPE_VARIABLE_SIZE (type)) 393 return false; 394 /* If the element type or the array has variable size, then the 395 array has variable size and is variably modified. */ 396 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (type)) 397 || C_TYPE_VARIABLE_SIZE (type)) 398 { 399 if (!C_TYPE_VARIABLE_SIZE (type)) 400 return false; 401 if (!C_TYPE_VARIABLY_MODIFIED (type)) 402 return false; 403 } 404 /* If the element type is variably modified, then also the array. */ 405 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (type)) 406 && !C_TYPE_VARIABLY_MODIFIED (type)) 407 return false; 408 break; 409 default: 410 break; 411 } 412 return true; 413 } 414 415 /* Propagate C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE 416 from a base type to a newly built derived or qualified type. */ 417 418 static tree 419 c_set_type_bits (tree new_type, tree old_type) 420 { 421 gcc_checking_assert (c_verify_type (old_type)); 422 423 if (C_TYPE_VARIABLY_MODIFIED (old_type)) 424 C_TYPE_VARIABLY_MODIFIED (new_type) = true; 425 426 if (TREE_CODE (new_type) == ARRAY_TYPE && C_TYPE_VARIABLE_SIZE (old_type)) 427 { 428 C_TYPE_VARIABLY_MODIFIED (new_type) = true; 429 C_TYPE_VARIABLE_SIZE (new_type) = true; 430 } 431 return new_type; 432 } 433 434 /* Build a pointer type using the default pointer mode. */ 435 436 tree 437 c_build_pointer_type (tree to_type) 438 { 439 addr_space_t as = to_type == error_mark_node ? ADDR_SPACE_GENERIC 440 : TYPE_ADDR_SPACE (to_type); 441 machine_mode pointer_mode; 442 443 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode) 444 pointer_mode = targetm.addr_space.pointer_mode (as); 445 else 446 pointer_mode = c_default_pointer_mode; 447 448 return c_build_pointer_type_for_mode (to_type, pointer_mode, false); 449 } 450 451 /* Build a pointer type using the given mode. */ 452 453 tree 454 c_build_pointer_type_for_mode (tree type, machine_mode mode, bool m) 455 { 456 tree ret = build_pointer_type_for_mode (type, mode, m); 457 return c_set_type_bits (ret, type); 458 } 459 460 /* Build a function type. */ 461 462 tree 463 c_build_function_type (tree type, tree args, bool no) 464 { 465 tree ret = build_function_type (type, args, no); 466 return c_set_type_bits (ret, type); 467 } 468 469 /* Build an array type. This sets typeless storage as required 470 by C2Y and C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE 471 based on the element type and domain. */ 472 473 tree 474 c_build_array_type (tree type, tree domain) 475 { 476 int type_quals = TYPE_QUALS (type); 477 478 /* Identify typeless storage as introduced in C2Y 479 and supported also in earlier language modes. */ 480 bool typeless = (char_type_p (type) && !(type_quals & TYPE_QUAL_ATOMIC)) 481 || (AGGREGATE_TYPE_P (type) && TYPE_TYPELESS_STORAGE (type)); 482 483 tree ret = build_array_type (type, domain, typeless); 484 485 if (domain && TYPE_MAX_VALUE (domain) 486 && TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST) 487 { 488 C_TYPE_VARIABLE_SIZE (ret) = 1; 489 C_TYPE_VARIABLY_MODIFIED (ret) = 1; 490 } 491 492 return c_set_type_bits (ret, type); 493 } 494 495 496 /* Build an array type of unspecified size. */ 497 tree 498 c_build_array_type_unspecified (tree type) 499 { 500 tree upper = build2 (COMPOUND_EXPR, TREE_TYPE (size_zero_node), 501 integer_zero_node, size_zero_node); 502 return c_build_array_type (type, build_index_type (upper)); 503 } 504 505 506 tree 507 c_build_type_attribute_qual_variant (tree type, tree attrs, int quals) 508 { 509 tree ret = build_type_attribute_qual_variant (type, attrs, quals); 510 return c_set_type_bits (ret, type); 511 } 512 513 tree 514 c_build_type_attribute_variant (tree type, tree attrs) 515 { 516 return c_build_type_attribute_qual_variant (type, attrs, TYPE_QUALS (type)); 517 } 518 519 /* Reconstruct a complex derived type. This is used to re-construct types 520 with the vector attribute. It is called via a langhook. */ 521 522 tree 523 c_reconstruct_complex_type (tree type, tree bottom) 524 { 525 tree inner, outer; 526 527 if (TREE_CODE (type) == POINTER_TYPE) 528 { 529 inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom); 530 outer = c_build_pointer_type_for_mode (inner, TYPE_MODE (type), 531 TYPE_REF_CAN_ALIAS_ALL (type)); 532 } 533 else if (TREE_CODE (type) == ARRAY_TYPE) 534 { 535 inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom); 536 outer = c_build_array_type (inner, TYPE_DOMAIN (type)); 537 538 gcc_checking_assert (C_TYPE_VARIABLE_SIZE (type) 539 == C_TYPE_VARIABLE_SIZE (outer)); 540 gcc_checking_assert (C_TYPE_VARIABLY_MODIFIED (outer) 541 == C_TYPE_VARIABLY_MODIFIED (type)); 542 } 543 else if (TREE_CODE (type) == FUNCTION_TYPE) 544 { 545 inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom); 546 outer = c_build_function_type (inner, TYPE_ARG_TYPES (type), 547 TYPE_NO_NAMED_ARGS_STDARG_P (type)); 548 } 549 else if (TREE_CODE (type) == REFERENCE_TYPE 550 || TREE_CODE (type) == OFFSET_TYPE) 551 gcc_unreachable (); 552 else 553 return bottom; 554 555 return c_build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type), 556 TYPE_QUALS (type)); 557 } 558 559 /* If NTYPE is a type of a non-variadic function with a prototype 560 and OTYPE is a type of a function without a prototype and ATTRS 561 contains attribute format, diagnosess and removes it from ATTRS. 562 Returns the result of build_type_attribute_variant of NTYPE and 563 the (possibly) modified ATTRS. */ 564 565 static tree 566 c_build_functype_attribute_variant (tree ntype, tree otype, tree attrs) 567 { 568 if (!prototype_p (otype) 569 && prototype_p (ntype) 570 && lookup_attribute ("format", attrs)) 571 { 572 warning_at (input_location, OPT_Wattributes, 573 "%qs attribute cannot be applied to a function that " 574 "does not take variable arguments", "format"); 575 attrs = remove_attribute ("format", attrs); 576 } 577 return c_build_type_attribute_variant (ntype, attrs); 578 579 } 580 581 582 /* Return the composite type of two compatible types. 584 585 We assume that comptypes has already been done and returned 586 nonzero; if that isn't so, this may crash. In particular, we 587 assume that qualifiers match. */ 588 589 struct composite_cache { 590 tree t1; 591 tree t2; 592 tree composite; 593 struct composite_cache* next; 594 }; 595 596 tree 597 composite_type_internal (tree t1, tree t2, struct composite_cache* cache) 598 { 599 enum tree_code code1; 600 enum tree_code code2; 601 tree attributes; 602 603 /* Save time if the two types are the same. */ 604 605 if (t1 == t2) return t1; 606 607 /* If one type is nonsense, use the other. */ 608 if (t1 == error_mark_node) 609 return t2; 610 if (t2 == error_mark_node) 611 return t1; 612 613 code1 = TREE_CODE (t1); 614 code2 = TREE_CODE (t2); 615 616 /* Merge the attributes. */ 617 attributes = targetm.merge_type_attributes (t1, t2); 618 619 /* If one is an enumerated type and the other is the compatible 620 integer type, the composite type might be either of the two 621 (DR#013 question 3). For consistency, use the enumerated type as 622 the composite type. */ 623 624 if (code1 == ENUMERAL_TYPE 625 && (code2 == INTEGER_TYPE 626 || code2 == BOOLEAN_TYPE)) 627 return t1; 628 if (code2 == ENUMERAL_TYPE 629 && (code1 == INTEGER_TYPE 630 || code1 == BOOLEAN_TYPE)) 631 return t2; 632 633 gcc_assert (code1 == code2); 634 635 switch (code1) 636 { 637 case POINTER_TYPE: 638 /* For two pointers, do this recursively on the target type. */ 639 { 640 tree pointed_to_1 = TREE_TYPE (t1); 641 tree pointed_to_2 = TREE_TYPE (t2); 642 tree target = composite_type_internal (pointed_to_1, 643 pointed_to_2, cache); 644 t1 = c_build_pointer_type_for_mode (target, TYPE_MODE (t1), false); 645 t1 = c_build_type_attribute_variant (t1, attributes); 646 return qualify_type (t1, t2); 647 } 648 649 case ARRAY_TYPE: 650 { 651 tree elt = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2), 652 cache); 653 int quals; 654 tree unqual_elt; 655 tree d1 = TYPE_DOMAIN (t1); 656 tree d2 = TYPE_DOMAIN (t2); 657 bool d1_variable, d2_variable; 658 bool d1_zero, d2_zero; 659 bool t1_complete, t2_complete; 660 661 /* We should not have any type quals on arrays at all. */ 662 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1) 663 && !TYPE_QUALS_NO_ADDR_SPACE (t2)); 664 665 t1_complete = COMPLETE_TYPE_P (t1); 666 t2_complete = COMPLETE_TYPE_P (t2); 667 668 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1); 669 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2); 670 671 d1_variable = (!d1_zero 672 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST 673 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); 674 d2_variable = (!d2_zero 675 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST 676 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); 677 678 bool use1 = TYPE_DOMAIN (t1) 679 && (d2_variable || d2_zero || !d1_variable); 680 bool use2 = TYPE_DOMAIN (t2) 681 && (d1_variable || d1_zero || !d2_variable); 682 683 /* If the first is an unspecified size pick the other one. */ 684 if (d2_variable && c_type_unspecified_p (t1)) 685 { 686 gcc_assert (use1 && use2); 687 use1 = false; 688 } 689 690 /* Save space: see if the result is identical to one of the args. */ 691 if (elt == TREE_TYPE (t1) && use1) 692 return c_build_type_attribute_variant (t1, attributes); 693 if (elt == TREE_TYPE (t2) && use2) 694 return c_build_type_attribute_variant (t2, attributes); 695 696 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) 697 return c_build_type_attribute_variant (t1, attributes); 698 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) 699 return c_build_type_attribute_variant (t2, attributes); 700 701 /* Merge the element types, and have a size if either arg has 702 one. We may have qualifiers on the element types. To set 703 up TYPE_MAIN_VARIANT correctly, we need to form the 704 composite of the unqualified types and add the qualifiers 705 back at the end. */ 706 quals = TYPE_QUALS (strip_array_types (elt)); 707 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED); 708 t1 = c_build_array_type (unqual_elt, TYPE_DOMAIN (use1 ? t1 : t2)); 709 710 /* Check that a type which has a varying outermost dimension 711 got marked has having a variable size. */ 712 bool varsize = (d1_variable && d2_variable) 713 || (d1_variable && !t2_complete) 714 || (d2_variable && !t1_complete); 715 gcc_checking_assert (!varsize || C_TYPE_VARIABLE_SIZE (t1)); 716 717 /* Ensure a composite type involving a zero-length array type 718 is a zero-length type not an incomplete type. */ 719 if (d1_zero && d2_zero 720 && (t1_complete || t2_complete) 721 && !COMPLETE_TYPE_P (t1)) 722 { 723 TYPE_SIZE (t1) = bitsize_zero_node; 724 TYPE_SIZE_UNIT (t1) = size_zero_node; 725 } 726 t1 = c_build_qualified_type (t1, quals); 727 return c_build_type_attribute_variant (t1, attributes); 728 } 729 730 case RECORD_TYPE: 731 case UNION_TYPE: 732 if (flag_isoc23 && !comptypes_same_p (t1, t2)) 733 { 734 gcc_checking_assert (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)); 735 gcc_checking_assert (!TYPE_NAME (t1) || comptypes (t1, t2)); 736 737 /* If a composite type for these two types is already under 738 construction, return it. */ 739 740 for (struct composite_cache *c = cache; c != NULL; c = c->next) 741 if (c->t1 == t1 && c->t2 == t2) 742 return c->composite; 743 744 /* Otherwise, create a new type node and link it into the cache. */ 745 746 tree n = make_node (code1); 747 SET_TYPE_STRUCTURAL_EQUALITY (n); 748 TYPE_NAME (n) = TYPE_NAME (t1); 749 750 struct composite_cache cache2 = { t1, t2, n, cache }; 751 cache = &cache2; 752 753 tree f1 = TYPE_FIELDS (t1); 754 tree f2 = TYPE_FIELDS (t2); 755 tree fields = NULL_TREE; 756 757 for (tree a = f1, b = f2; a && b; 758 a = DECL_CHAIN (a), b = DECL_CHAIN (b)) 759 { 760 tree ta = TREE_TYPE (a); 761 tree tb = TREE_TYPE (b); 762 763 if (DECL_C_BIT_FIELD (a)) 764 { 765 ta = DECL_BIT_FIELD_TYPE (a); 766 tb = DECL_BIT_FIELD_TYPE (b); 767 } 768 769 gcc_assert (DECL_NAME (a) == DECL_NAME (b)); 770 gcc_checking_assert (!DECL_NAME (a) || comptypes (ta, tb)); 771 772 tree t = composite_type_internal (ta, tb, cache); 773 tree f = build_decl (input_location, FIELD_DECL, DECL_NAME (a), t); 774 775 DECL_PACKED (f) = DECL_PACKED (a); 776 SET_DECL_ALIGN (f, DECL_ALIGN (a)); 777 DECL_ATTRIBUTES (f) = DECL_ATTRIBUTES (a); 778 C_DECL_VARIABLE_SIZE (f) = C_TYPE_VARIABLE_SIZE (t); 779 780 decl_attributes (&f, DECL_ATTRIBUTES (f), 0); 781 782 finish_decl (f, input_location, NULL, NULL, NULL); 783 784 if (DECL_C_BIT_FIELD (a)) 785 { 786 /* This will be processed by finish_struct. */ 787 SET_DECL_C_BIT_FIELD (f); 788 DECL_INITIAL (f) = build_int_cst (integer_type_node, 789 tree_to_uhwi (DECL_SIZE (a))); 790 DECL_NONADDRESSABLE_P (f) = true; 791 DECL_PADDING_P (f) = !DECL_NAME (a); 792 } 793 794 DECL_CHAIN (f) = fields; 795 fields = f; 796 } 797 798 fields = nreverse (fields); 799 800 /* Setup the struct/union type. Because we inherit all variably 801 modified components, we can ignore the size expression. */ 802 tree expr = NULL_TREE; 803 804 /* Set TYPE_STUB_DECL for debugging symbols. */ 805 TYPE_STUB_DECL (n) = pushdecl (build_decl (input_location, TYPE_DECL, 806 NULL_TREE, n)); 807 808 n = finish_struct (input_location, n, fields, attributes, NULL, 809 &expr); 810 811 n = qualify_type (n, t1); 812 813 gcc_checking_assert (!TYPE_NAME (n) || comptypes (n, t1)); 814 gcc_checking_assert (!TYPE_NAME (n) || comptypes (n, t2)); 815 816 return n; 817 } 818 /* FALLTHRU */ 819 case ENUMERAL_TYPE: 820 if (attributes != NULL) 821 { 822 /* Try harder not to create a new aggregate type. */ 823 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) 824 return t1; 825 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) 826 return t2; 827 } 828 return c_build_type_attribute_variant (t1, attributes); 829 830 case FUNCTION_TYPE: 831 /* Function types: prefer the one that specified arg types. 832 If both do, merge the arg types. Also merge the return types. */ 833 { 834 tree valtype = composite_type_internal (TREE_TYPE (t1), 835 TREE_TYPE (t2), cache); 836 tree p1 = TYPE_ARG_TYPES (t1); 837 tree p2 = TYPE_ARG_TYPES (t2); 838 int len; 839 tree newargs, n; 840 int i; 841 842 /* Save space: see if the result is identical to one of the args. */ 843 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2)) 844 return c_build_functype_attribute_variant (t1, t2, attributes); 845 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1)) 846 return c_build_functype_attribute_variant (t2, t1, attributes); 847 848 /* Simple way if one arg fails to specify argument types. */ 849 if (TYPE_ARG_TYPES (t1) == NULL_TREE) 850 { 851 t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t2), 852 TYPE_NO_NAMED_ARGS_STDARG_P (t2)); 853 t1 = c_build_type_attribute_variant (t1, attributes); 854 return qualify_type (t1, t2); 855 } 856 if (TYPE_ARG_TYPES (t2) == NULL_TREE) 857 { 858 t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t1), 859 TYPE_NO_NAMED_ARGS_STDARG_P (t1)); 860 t1 = c_build_type_attribute_variant (t1, attributes); 861 return qualify_type (t1, t2); 862 } 863 864 /* If both args specify argument types, we must merge the two 865 lists, argument by argument. */ 866 867 for (len = 0, newargs = p1; 868 newargs && newargs != void_list_node; 869 len++, newargs = TREE_CHAIN (newargs)) 870 ; 871 872 for (i = 0; i < len; i++) 873 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); 874 875 n = newargs; 876 877 for (; p1 && p1 != void_list_node; 878 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) 879 { 880 tree mv1 = TREE_VALUE (p1); 881 if (mv1 && mv1 != error_mark_node 882 && TREE_CODE (mv1) != ARRAY_TYPE) 883 mv1 = TYPE_MAIN_VARIANT (mv1); 884 885 tree mv2 = TREE_VALUE (p2); 886 if (mv2 && mv2 != error_mark_node 887 && TREE_CODE (mv2) != ARRAY_TYPE) 888 mv2 = TYPE_MAIN_VARIANT (mv2); 889 890 /* A null type means arg type is not specified. 891 Take whatever the other function type has. */ 892 if (TREE_VALUE (p1) == NULL_TREE) 893 { 894 TREE_VALUE (n) = TREE_VALUE (p2); 895 goto parm_done; 896 } 897 if (TREE_VALUE (p2) == NULL_TREE) 898 { 899 TREE_VALUE (n) = TREE_VALUE (p1); 900 goto parm_done; 901 } 902 903 /* Given wait (union {union wait *u; int *i} *) 904 and wait (union wait *), 905 prefer union wait * as type of parm. */ 906 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE 907 && TREE_VALUE (p1) != TREE_VALUE (p2)) 908 { 909 tree memb; 910 for (memb = TYPE_FIELDS (TREE_VALUE (p1)); 911 memb; memb = DECL_CHAIN (memb)) 912 { 913 tree mv3 = TREE_TYPE (memb); 914 if (mv3 && mv3 != error_mark_node 915 && TREE_CODE (mv3) != ARRAY_TYPE) 916 mv3 = TYPE_MAIN_VARIANT (mv3); 917 if (comptypes (mv3, mv2)) 918 { 919 TREE_VALUE (n) = composite_type_internal (TREE_TYPE (memb), 920 TREE_VALUE (p2), 921 cache); 922 pedwarn (input_location, OPT_Wpedantic, 923 "function types not truly compatible in ISO C"); 924 goto parm_done; 925 } 926 } 927 } 928 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE 929 && TREE_VALUE (p2) != TREE_VALUE (p1)) 930 { 931 tree memb; 932 for (memb = TYPE_FIELDS (TREE_VALUE (p2)); 933 memb; memb = DECL_CHAIN (memb)) 934 { 935 tree mv3 = TREE_TYPE (memb); 936 if (mv3 && mv3 != error_mark_node 937 && TREE_CODE (mv3) != ARRAY_TYPE) 938 mv3 = TYPE_MAIN_VARIANT (mv3); 939 if (comptypes (mv3, mv1)) 940 { 941 TREE_VALUE (n) 942 = composite_type_internal (TREE_TYPE (memb), 943 TREE_VALUE (p1), 944 cache); 945 pedwarn (input_location, OPT_Wpedantic, 946 "function types not truly compatible in ISO C"); 947 goto parm_done; 948 } 949 } 950 } 951 TREE_VALUE (n) = composite_type_internal (mv1, mv2, cache); 952 parm_done: ; 953 } 954 955 t1 = c_build_function_type (valtype, newargs); 956 t1 = qualify_type (t1, t2); 957 } 958 /* FALLTHRU */ 959 960 default: 961 return c_build_type_attribute_variant (t1, attributes); 962 } 963 } 964 965 tree 966 composite_type (tree t1, tree t2) 967 { 968 struct composite_cache cache = { }; 969 return composite_type_internal (t1, t2, &cache); 970 } 971 972 /* Return the type of a conditional expression between pointers to 973 possibly differently qualified versions of compatible types. 974 975 We assume that comp_target_types has already been done and returned 976 true; if that isn't so, this may crash. */ 977 978 static tree 979 common_pointer_type (tree t1, tree t2) 980 { 981 tree attributes; 982 tree pointed_to_1, mv1; 983 tree pointed_to_2, mv2; 984 tree target; 985 unsigned target_quals; 986 addr_space_t as1, as2, as_common; 987 int quals1, quals2; 988 989 /* Save time if the two types are the same. */ 990 991 if (t1 == t2) return t1; 992 993 /* If one type is nonsense, use the other. */ 994 if (t1 == error_mark_node) 995 return t2; 996 if (t2 == error_mark_node) 997 return t1; 998 999 gcc_assert (TREE_CODE (t1) == POINTER_TYPE 1000 && TREE_CODE (t2) == POINTER_TYPE); 1001 1002 /* Merge the attributes. */ 1003 attributes = targetm.merge_type_attributes (t1, t2); 1004 1005 /* Find the composite type of the target types, and combine the 1006 qualifiers of the two types' targets. Do not lose qualifiers on 1007 array element types by taking the TYPE_MAIN_VARIANT. */ 1008 mv1 = pointed_to_1 = TREE_TYPE (t1); 1009 mv2 = pointed_to_2 = TREE_TYPE (t2); 1010 if (TREE_CODE (mv1) != ARRAY_TYPE) 1011 mv1 = TYPE_MAIN_VARIANT (pointed_to_1); 1012 if (TREE_CODE (mv2) != ARRAY_TYPE) 1013 mv2 = TYPE_MAIN_VARIANT (pointed_to_2); 1014 target = composite_type (mv1, mv2); 1015 1016 /* Strip array types to get correct qualifier for pointers to arrays */ 1017 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1)); 1018 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2)); 1019 1020 /* For function types do not merge const qualifiers, but drop them 1021 if used inconsistently. The middle-end uses these to mark const 1022 and noreturn functions. */ 1023 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE) 1024 target_quals = (quals1 & quals2); 1025 else 1026 target_quals = (quals1 | quals2); 1027 1028 /* If the two named address spaces are different, determine the common 1029 superset address space. This is guaranteed to exist due to the 1030 assumption that comp_target_type returned true. */ 1031 as1 = TYPE_ADDR_SPACE (pointed_to_1); 1032 as2 = TYPE_ADDR_SPACE (pointed_to_2); 1033 if (!addr_space_superset (as1, as2, &as_common)) 1034 gcc_unreachable (); 1035 1036 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common); 1037 1038 t1 = c_build_pointer_type (c_build_qualified_type (target, target_quals)); 1039 return c_build_type_attribute_variant (t1, attributes); 1040 } 1041 1042 /* Return the common type for two arithmetic types under the usual 1043 arithmetic conversions. The default conversions have already been 1044 applied, and enumerated types converted to their compatible integer 1045 types. The resulting type is unqualified and has no attributes. 1046 1047 This is the type for the result of most arithmetic operations 1048 if the operands have the given two types. */ 1049 1050 static tree 1051 c_common_type (tree t1, tree t2) 1052 { 1053 enum tree_code code1; 1054 enum tree_code code2; 1055 1056 /* If one type is nonsense, use the other. */ 1057 if (t1 == error_mark_node) 1058 return t2; 1059 if (t2 == error_mark_node) 1060 return t1; 1061 1062 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED) 1063 t1 = TYPE_MAIN_VARIANT (t1); 1064 1065 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED) 1066 t2 = TYPE_MAIN_VARIANT (t2); 1067 1068 if (TYPE_ATTRIBUTES (t1) != NULL_TREE) 1069 { 1070 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1)); 1071 t1 = c_build_type_attribute_variant (t1, attrs); 1072 } 1073 1074 if (TYPE_ATTRIBUTES (t2) != NULL_TREE) 1075 { 1076 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2)); 1077 t2 = c_build_type_attribute_variant (t2, attrs); 1078 } 1079 1080 /* Save time if the two types are the same. */ 1081 1082 if (t1 == t2) return t1; 1083 1084 code1 = TREE_CODE (t1); 1085 code2 = TREE_CODE (t2); 1086 1087 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE 1088 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE 1089 || code1 == INTEGER_TYPE || code1 == BITINT_TYPE); 1090 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE 1091 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE 1092 || code2 == INTEGER_TYPE || code2 == BITINT_TYPE); 1093 1094 /* When one operand is a decimal float type, the other operand cannot be 1095 a generic float type or a complex type. We also disallow vector types 1096 here. */ 1097 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2)) 1098 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2))) 1099 { 1100 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE) 1101 { 1102 error ("cannot mix operands of decimal floating and vector types"); 1103 return error_mark_node; 1104 } 1105 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) 1106 { 1107 error ("cannot mix operands of decimal floating and complex types"); 1108 return error_mark_node; 1109 } 1110 if (code1 == REAL_TYPE && code2 == REAL_TYPE) 1111 { 1112 error ("cannot mix operands of decimal floating " 1113 "and other floating types"); 1114 return error_mark_node; 1115 } 1116 } 1117 1118 /* If one type is a vector type, return that type. (How the usual 1119 arithmetic conversions apply to the vector types extension is not 1120 precisely specified.) */ 1121 if (code1 == VECTOR_TYPE) 1122 return t1; 1123 1124 if (code2 == VECTOR_TYPE) 1125 return t2; 1126 1127 /* If one type is complex, form the common type of the non-complex 1128 components, then make that complex. Use T1 or T2 if it is the 1129 required type. */ 1130 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) 1131 { 1132 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; 1133 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; 1134 tree subtype = c_common_type (subtype1, subtype2); 1135 1136 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) 1137 return t1; 1138 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) 1139 return t2; 1140 else if (TREE_CODE (subtype) == BITINT_TYPE) 1141 { 1142 sorry ("%<_Complex _BitInt(%d)%> unsupported", 1143 TYPE_PRECISION (subtype)); 1144 return code1 == COMPLEX_TYPE ? t1 : t2; 1145 } 1146 else 1147 return build_complex_type (subtype); 1148 } 1149 1150 /* If only one is real, use it as the result. */ 1151 1152 if (code1 == REAL_TYPE && code2 != REAL_TYPE) 1153 return t1; 1154 1155 if (code2 == REAL_TYPE && code1 != REAL_TYPE) 1156 return t2; 1157 1158 /* If both are real and either are decimal floating point types, use 1159 the decimal floating point type with the greater precision. */ 1160 1161 if (code1 == REAL_TYPE && code2 == REAL_TYPE) 1162 { 1163 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node 1164 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node) 1165 return dfloat128_type_node; 1166 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node 1167 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node) 1168 return dfloat64_type_node; 1169 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node 1170 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node) 1171 return dfloat32_type_node; 1172 } 1173 1174 /* Deal with fixed-point types. */ 1175 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE) 1176 { 1177 unsigned int unsignedp = 0, satp = 0; 1178 scalar_mode m1, m2; 1179 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit; 1180 1181 m1 = SCALAR_TYPE_MODE (t1); 1182 m2 = SCALAR_TYPE_MODE (t2); 1183 1184 /* If one input type is saturating, the result type is saturating. */ 1185 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2)) 1186 satp = 1; 1187 1188 /* If both fixed-point types are unsigned, the result type is unsigned. 1189 When mixing fixed-point and integer types, follow the sign of the 1190 fixed-point type. 1191 Otherwise, the result type is signed. */ 1192 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2) 1193 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE) 1194 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE 1195 && TYPE_UNSIGNED (t1)) 1196 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE 1197 && TYPE_UNSIGNED (t2))) 1198 unsignedp = 1; 1199 1200 /* The result type is signed. */ 1201 if (unsignedp == 0) 1202 { 1203 /* If the input type is unsigned, we need to convert to the 1204 signed type. */ 1205 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1)) 1206 { 1207 enum mode_class mclass = (enum mode_class) 0; 1208 if (GET_MODE_CLASS (m1) == MODE_UFRACT) 1209 mclass = MODE_FRACT; 1210 else if (GET_MODE_CLASS (m1) == MODE_UACCUM) 1211 mclass = MODE_ACCUM; 1212 else 1213 gcc_unreachable (); 1214 m1 = as_a <scalar_mode> 1215 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0)); 1216 } 1217 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2)) 1218 { 1219 enum mode_class mclass = (enum mode_class) 0; 1220 if (GET_MODE_CLASS (m2) == MODE_UFRACT) 1221 mclass = MODE_FRACT; 1222 else if (GET_MODE_CLASS (m2) == MODE_UACCUM) 1223 mclass = MODE_ACCUM; 1224 else 1225 gcc_unreachable (); 1226 m2 = as_a <scalar_mode> 1227 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0)); 1228 } 1229 } 1230 1231 if (code1 == FIXED_POINT_TYPE) 1232 { 1233 fbit1 = GET_MODE_FBIT (m1); 1234 ibit1 = GET_MODE_IBIT (m1); 1235 } 1236 else 1237 { 1238 fbit1 = 0; 1239 /* Signed integers need to subtract one sign bit. */ 1240 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1)); 1241 } 1242 1243 if (code2 == FIXED_POINT_TYPE) 1244 { 1245 fbit2 = GET_MODE_FBIT (m2); 1246 ibit2 = GET_MODE_IBIT (m2); 1247 } 1248 else 1249 { 1250 fbit2 = 0; 1251 /* Signed integers need to subtract one sign bit. */ 1252 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2)); 1253 } 1254 1255 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2; 1256 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2; 1257 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp, 1258 satp); 1259 } 1260 1261 /* Both real or both integers; use the one with greater precision. */ 1262 1263 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) 1264 return t1; 1265 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) 1266 return t2; 1267 1268 /* Same precision. Prefer long longs to longs to ints when the 1269 same precision, following the C99 rules on integer type rank 1270 (which are equivalent to the C90 rules for C90 types). */ 1271 1272 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node 1273 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node) 1274 return long_long_unsigned_type_node; 1275 1276 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node 1277 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node) 1278 { 1279 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) 1280 return long_long_unsigned_type_node; 1281 else 1282 return long_long_integer_type_node; 1283 } 1284 1285 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node 1286 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) 1287 return long_unsigned_type_node; 1288 1289 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node 1290 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) 1291 { 1292 /* But preserve unsignedness from the other type, 1293 since long cannot hold all the values of an unsigned int. */ 1294 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) 1295 return long_unsigned_type_node; 1296 else 1297 return long_integer_type_node; 1298 } 1299 1300 /* For floating types of the same TYPE_PRECISION (which we here 1301 assume means either the same set of values, or sets of values 1302 neither a subset of the other, with behavior being undefined in 1303 the latter case), follow the rules from TS 18661-3: prefer 1304 interchange types _FloatN, then standard types long double, 1305 double, float, then extended types _FloatNx. For extended types, 1306 check them starting with _Float128x as that seems most consistent 1307 in spirit with preferring long double to double; for interchange 1308 types, also check in that order for consistency although it's not 1309 possible for more than one of them to have the same 1310 precision. */ 1311 tree mv1 = TYPE_MAIN_VARIANT (t1); 1312 tree mv2 = TYPE_MAIN_VARIANT (t2); 1313 1314 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--) 1315 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i)) 1316 return FLOATN_TYPE_NODE (i); 1317 1318 /* Likewise, prefer long double to double even if same size. */ 1319 if (mv1 == long_double_type_node || mv2 == long_double_type_node) 1320 return long_double_type_node; 1321 1322 /* Likewise, prefer double to float even if same size. 1323 We got a couple of embedded targets with 32 bit doubles, and the 1324 pdp11 might have 64 bit floats. */ 1325 if (mv1 == double_type_node || mv2 == double_type_node) 1326 return double_type_node; 1327 1328 if (mv1 == float_type_node || mv2 == float_type_node) 1329 return float_type_node; 1330 1331 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--) 1332 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i)) 1333 return FLOATNX_TYPE_NODE (i); 1334 1335 if ((code1 == BITINT_TYPE || code2 == BITINT_TYPE) && code1 != code2) 1336 { 1337 /* Prefer any other integral types over bit-precise integer types. */ 1338 if (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)) 1339 return code1 == BITINT_TYPE ? t2 : t1; 1340 /* If BITINT_TYPE is unsigned and the other type is signed 1341 non-BITINT_TYPE with the same precision, the latter has higher rank. 1342 In that case: 1343 Otherwise, both operands are converted to the unsigned integer type 1344 corresponding to the type of the operand with signed integer type. */ 1345 if (TYPE_UNSIGNED (code1 == BITINT_TYPE ? t1 : t2)) 1346 return c_common_unsigned_type (code1 == BITINT_TYPE ? t2 : t1); 1347 } 1348 1349 /* Otherwise prefer the unsigned one. */ 1350 1351 if (TYPE_UNSIGNED (t1)) 1352 return t1; 1353 else 1354 return t2; 1355 } 1356 1357 /* Wrapper around c_common_type that is used by c-common.cc and other 1359 front end optimizations that remove promotions. ENUMERAL_TYPEs 1360 are allowed here and are converted to their compatible integer types. 1361 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or 1362 preferably a non-Boolean type as the common type. */ 1363 tree 1364 common_type (tree t1, tree t2) 1365 { 1366 if (TREE_CODE (t1) == ENUMERAL_TYPE) 1367 t1 = ENUM_UNDERLYING_TYPE (t1); 1368 if (TREE_CODE (t2) == ENUMERAL_TYPE) 1369 t2 = ENUM_UNDERLYING_TYPE (t2); 1370 1371 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */ 1372 if (TREE_CODE (t1) == BOOLEAN_TYPE 1373 && TREE_CODE (t2) == BOOLEAN_TYPE) 1374 return boolean_type_node; 1375 1376 /* If either type is BOOLEAN_TYPE, then return the other. */ 1377 if (TREE_CODE (t1) == BOOLEAN_TYPE) 1378 return t2; 1379 if (TREE_CODE (t2) == BOOLEAN_TYPE) 1380 return t1; 1381 1382 return c_common_type (t1, t2); 1383 } 1384 1385 1386 1387 /* Helper function for comptypes. For two compatible types, return 1 1388 if they pass consistency checks. In particular we test that 1389 TYPE_CANONICAL is set correctly, i.e. the two types can alias. */ 1390 1391 static bool 1392 comptypes_verify (tree type1, tree type2) 1393 { 1394 if (type1 == type2 || !type1 || !type2 1395 || TREE_CODE (type1) == ERROR_MARK || TREE_CODE (type2) == ERROR_MARK) 1396 return true; 1397 1398 gcc_checking_assert (c_verify_type (type1)); 1399 gcc_checking_assert (c_verify_type (type2)); 1400 1401 if (TYPE_CANONICAL (type1) != TYPE_CANONICAL (type2) 1402 && !TYPE_STRUCTURAL_EQUALITY_P (type1) 1403 && !TYPE_STRUCTURAL_EQUALITY_P (type2)) 1404 { 1405 /* FIXME: check other types. */ 1406 if (RECORD_OR_UNION_TYPE_P (type1) 1407 || TREE_CODE (type1) == ENUMERAL_TYPE 1408 || TREE_CODE (type2) == ENUMERAL_TYPE) 1409 return false; 1410 } 1411 return true; 1412 } 1413 1414 struct comptypes_data { 1415 bool enum_and_int_p; 1416 bool different_types_p; 1417 bool warning_needed; 1418 bool anon_field; 1419 bool equiv; 1420 1421 const struct tagged_tu_seen_cache* cache; 1422 }; 1423 1424 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment 1425 or various other operations. Return 2 if they are compatible 1426 but a warning may be needed if you use them together. */ 1427 1428 int 1429 comptypes (tree type1, tree type2) 1430 { 1431 struct comptypes_data data = { }; 1432 bool ret = comptypes_internal (type1, type2, &data); 1433 1434 return ret ? (data.warning_needed ? 2 : 1) : 0; 1435 } 1436 1437 1438 /* Like comptypes, but it returns non-zero only for identical 1439 types. */ 1440 1441 bool 1442 comptypes_same_p (tree type1, tree type2) 1443 { 1444 struct comptypes_data data = { }; 1445 bool ret = comptypes_internal (type1, type2, &data); 1446 1447 if (data.different_types_p) 1448 return false; 1449 1450 return ret; 1451 } 1452 1453 1454 /* Like comptypes, but if it returns non-zero because enum and int are 1455 compatible, it sets *ENUM_AND_INT_P to true. */ 1456 1457 int 1458 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p) 1459 { 1460 struct comptypes_data data = { }; 1461 bool ret = comptypes_internal (type1, type2, &data); 1462 *enum_and_int_p = data.enum_and_int_p; 1463 1464 return ret ? (data.warning_needed ? 2 : 1) : 0; 1465 } 1466 1467 /* Like comptypes, but if it returns nonzero for different types, it 1468 sets *DIFFERENT_TYPES_P to true. */ 1469 1470 int 1471 comptypes_check_different_types (tree type1, tree type2, 1472 bool *different_types_p) 1473 { 1474 struct comptypes_data data = { }; 1475 bool ret = comptypes_internal (type1, type2, &data); 1476 *different_types_p = data.different_types_p; 1477 1478 return ret ? (data.warning_needed ? 2 : 1) : 0; 1479 } 1480 1481 1482 /* Like comptypes, but if it returns nonzero for struct and union 1483 types considered equivalent for aliasing purposes. */ 1484 1485 bool 1486 comptypes_equiv_p (tree type1, tree type2) 1487 { 1488 struct comptypes_data data = { }; 1489 data.equiv = true; 1490 bool ret = comptypes_internal (type1, type2, &data); 1491 1492 return ret; 1493 } 1494 1495 1496 /* Return true if TYPE1 and TYPE2 are compatible types for assignment 1498 or various other operations. If they are compatible but a warning may 1499 be needed if you use them together, 'warning_needed' in DATA is set. 1500 If one type is an enum and the other a compatible integer type, then 1501 this sets 'enum_and_int_p' in DATA to true (it is never set to 1502 false). If the types are compatible but different enough not to be 1503 permitted in C11 typedef redeclarations, then this sets 1504 'different_types_p' in DATA to true; it is never set to 1505 false, but may or may not be set if the types are incompatible. 1506 This differs from comptypes, in that we don't free the seen 1507 types. */ 1508 1509 static bool 1510 comptypes_internal (const_tree type1, const_tree type2, 1511 struct comptypes_data *data) 1512 { 1513 const_tree t1 = type1; 1514 const_tree t2 = type2; 1515 1516 /* Suppress errors caused by previously reported errors. */ 1517 1518 if (t1 == t2 || !t1 || !t2 1519 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) 1520 return true; 1521 1522 /* Enumerated types are compatible with integer types, but this is 1523 not transitive: two enumerated types in the same translation unit 1524 are compatible with each other only if they are the same type. */ 1525 1526 if (TREE_CODE (t1) == ENUMERAL_TYPE 1527 && COMPLETE_TYPE_P (t1) 1528 && TREE_CODE (t2) != ENUMERAL_TYPE) 1529 { 1530 t1 = ENUM_UNDERLYING_TYPE (t1); 1531 if (TREE_CODE (t2) != VOID_TYPE) 1532 { 1533 data->enum_and_int_p = true; 1534 data->different_types_p = true; 1535 } 1536 } 1537 else if (TREE_CODE (t2) == ENUMERAL_TYPE 1538 && COMPLETE_TYPE_P (t2) 1539 && TREE_CODE (t1) != ENUMERAL_TYPE) 1540 { 1541 t2 = ENUM_UNDERLYING_TYPE (t2); 1542 if (TREE_CODE (t1) != VOID_TYPE) 1543 { 1544 data->enum_and_int_p = true; 1545 data->different_types_p = true; 1546 } 1547 } 1548 1549 if (t1 == t2) 1550 return true; 1551 1552 /* Different classes of types can't be compatible. */ 1553 1554 if (TREE_CODE (t1) != TREE_CODE (t2)) 1555 return false; 1556 1557 /* Qualifiers must match. C99 6.7.3p9 */ 1558 1559 if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) 1560 return false; 1561 1562 /* Allow for two different type nodes which have essentially the same 1563 definition. Note that we already checked for equality of the type 1564 qualifiers (just above). */ 1565 1566 if (TREE_CODE (t1) != ARRAY_TYPE 1567 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) 1568 return true; 1569 1570 int attrval; 1571 1572 /* 1 if no need for warning yet, 2 if warning cause has been seen. */ 1573 if (!(attrval = comp_type_attributes (t1, t2))) 1574 return false; 1575 1576 if (2 == attrval) 1577 data->warning_needed = true; 1578 1579 switch (TREE_CODE (t1)) 1580 { 1581 case INTEGER_TYPE: 1582 case FIXED_POINT_TYPE: 1583 case REAL_TYPE: 1584 case BITINT_TYPE: 1585 /* With these nodes, we can't determine type equivalence by 1586 looking at what is stored in the nodes themselves, because 1587 two nodes might have different TYPE_MAIN_VARIANTs but still 1588 represent the same type. For example, wchar_t and int could 1589 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE, 1590 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs 1591 and are distinct types. On the other hand, int and the 1592 following typedef 1593 1594 typedef int INT __attribute((may_alias)); 1595 1596 have identical properties, different TYPE_MAIN_VARIANTs, but 1597 represent the same type. The canonical type system keeps 1598 track of equivalence in this case, so we fall back on it. */ 1599 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2); 1600 1601 case POINTER_TYPE: 1602 /* Do not remove mode information. */ 1603 if (TYPE_MODE (t1) != TYPE_MODE (t2)) 1604 return false; 1605 return comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data); 1606 1607 case FUNCTION_TYPE: 1608 return function_types_compatible_p (t1, t2, data); 1609 1610 case ARRAY_TYPE: 1611 { 1612 tree d1 = TYPE_DOMAIN (t1); 1613 tree d2 = TYPE_DOMAIN (t2); 1614 bool d1_variable, d2_variable; 1615 bool d1_zero, d2_zero; 1616 1617 /* Target types must match incl. qualifiers. */ 1618 if (!comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data)) 1619 return false; 1620 1621 if ((d1 == NULL_TREE) != (d2 == NULL_TREE)) 1622 data->different_types_p = true; 1623 /* Ignore size mismatches. */ 1624 if (data->equiv) 1625 return true; 1626 /* Sizes must match unless one is missing or variable. */ 1627 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2) 1628 return true; 1629 1630 d1_zero = !TYPE_MAX_VALUE (d1); 1631 d2_zero = !TYPE_MAX_VALUE (d2); 1632 1633 d1_variable = (!d1_zero 1634 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST 1635 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); 1636 d2_variable = (!d2_zero 1637 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST 1638 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); 1639 1640 if (d1_variable != d2_variable) 1641 data->different_types_p = true; 1642 if (d1_variable || d2_variable) 1643 return true; 1644 if (d1_zero && d2_zero) 1645 return true; 1646 if (d1_zero || d2_zero 1647 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)) 1648 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2))) 1649 return false; 1650 1651 return true; 1652 } 1653 1654 case ENUMERAL_TYPE: 1655 case RECORD_TYPE: 1656 case UNION_TYPE: 1657 1658 if (!flag_isoc23) 1659 return false; 1660 1661 return tagged_types_tu_compatible_p (t1, t2, data); 1662 1663 case VECTOR_TYPE: 1664 return known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2)) 1665 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data); 1666 1667 default: 1668 return false; 1669 } 1670 gcc_unreachable (); 1671 } 1672 1673 /* Return true if TTL and TTR are pointers to types that are equivalent, ignoring 1674 their qualifiers, except for named address spaces. If the pointers point to 1675 different named addresses, then we must determine if one address space is a 1676 subset of the other. */ 1677 1678 static bool 1679 comp_target_types (location_t location, tree ttl, tree ttr) 1680 { 1681 int val; 1682 int val_ped; 1683 tree mvl = TREE_TYPE (ttl); 1684 tree mvr = TREE_TYPE (ttr); 1685 addr_space_t asl = TYPE_ADDR_SPACE (mvl); 1686 addr_space_t asr = TYPE_ADDR_SPACE (mvr); 1687 addr_space_t as_common; 1688 bool enum_and_int_p; 1689 1690 /* Fail if pointers point to incompatible address spaces. */ 1691 if (!addr_space_superset (asl, asr, &as_common)) 1692 return 0; 1693 1694 /* For pedantic record result of comptypes on arrays before losing 1695 qualifiers on the element type below. */ 1696 val_ped = 1; 1697 1698 if (TREE_CODE (mvl) == ARRAY_TYPE 1699 && TREE_CODE (mvr) == ARRAY_TYPE) 1700 val_ped = comptypes (mvl, mvr); 1701 1702 /* Qualifiers on element types of array types that are 1703 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */ 1704 1705 mvl = (TYPE_ATOMIC (strip_array_types (mvl)) 1706 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC) 1707 : TYPE_MAIN_VARIANT (mvl)); 1708 1709 mvr = (TYPE_ATOMIC (strip_array_types (mvr)) 1710 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC) 1711 : TYPE_MAIN_VARIANT (mvr)); 1712 1713 enum_and_int_p = false; 1714 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p); 1715 1716 if (val == 1 && val_ped != 1) 1717 pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers " 1718 "in ISO C before C23"); 1719 1720 if (val == 2) 1721 pedwarn (location, OPT_Wpedantic, "types are not quite compatible"); 1722 1723 if (val == 1 && enum_and_int_p && warn_cxx_compat) 1724 warning_at (location, OPT_Wc___compat, 1725 "pointer target types incompatible in C++"); 1726 1727 return val; 1728 } 1729 1730 /* Subroutines of `comptypes'. */ 1732 1733 /* Return true if two 'struct', 'union', or 'enum' types T1 and T2 are 1734 compatible. The two types are not the same (which has been 1735 checked earlier in comptypes_internal). */ 1736 1737 static bool 1738 tagged_types_tu_compatible_p (const_tree t1, const_tree t2, 1739 struct comptypes_data *data) 1740 { 1741 tree s1, s2; 1742 1743 /* We have to verify that the tags of the types are the same. This 1744 is harder than it looks because this may be a typedef, so we have 1745 to go look at the original type. It may even be a typedef of a 1746 typedef... 1747 In the case of compiler-created builtin structs the TYPE_DECL 1748 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */ 1749 while (TYPE_NAME (t1) 1750 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL 1751 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1))) 1752 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1)); 1753 1754 while (TYPE_NAME (t2) 1755 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL 1756 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2))) 1757 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2)); 1758 1759 if (TYPE_NAME (t1) != TYPE_NAME (t2)) 1760 return false; 1761 1762 if (!data->anon_field && NULL_TREE == TYPE_NAME (t1)) 1763 return false; 1764 1765 if (!data->anon_field && TYPE_STUB_DECL (t1) != TYPE_STUB_DECL (t2)) 1766 data->different_types_p = true; 1767 1768 /* Incomplete types are incompatible inside a TU. */ 1769 if (TYPE_SIZE (t1) == NULL || TYPE_SIZE (t2) == NULL) 1770 return false; 1771 1772 if (ENUMERAL_TYPE != TREE_CODE (t1) 1773 && (TYPE_REVERSE_STORAGE_ORDER (t1) 1774 != TYPE_REVERSE_STORAGE_ORDER (t2))) 1775 return false; 1776 1777 /* For types already being looked at in some active 1778 invocation of this function, assume compatibility. 1779 The cache is built as a linked list on the stack 1780 with the head of the list passed downwards. */ 1781 for (const struct tagged_tu_seen_cache *t = data->cache; 1782 t != NULL; t = t->next) 1783 if (t->t1 == t1 && t->t2 == t2) 1784 return true; 1785 1786 const struct tagged_tu_seen_cache entry = { data->cache, t1, t2 }; 1787 1788 switch (TREE_CODE (t1)) 1789 { 1790 case ENUMERAL_TYPE: 1791 { 1792 if (!comptypes (ENUM_UNDERLYING_TYPE (t1), ENUM_UNDERLYING_TYPE (t2))) 1793 return false; 1794 1795 /* Speed up the case where the type values are in the same order. */ 1796 tree tv1 = TYPE_VALUES (t1); 1797 tree tv2 = TYPE_VALUES (t2); 1798 1799 if (tv1 == tv2) 1800 return true; 1801 1802 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2)) 1803 { 1804 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2)) 1805 break; 1806 1807 if (simple_cst_equal (DECL_INITIAL (TREE_VALUE (tv1)), 1808 DECL_INITIAL (TREE_VALUE (tv2))) != 1) 1809 break; 1810 } 1811 1812 if (tv1 == NULL_TREE && tv2 == NULL_TREE) 1813 return true; 1814 1815 if (tv1 == NULL_TREE || tv2 == NULL_TREE) 1816 return false; 1817 1818 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2))) 1819 return false; 1820 1821 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1)) 1822 { 1823 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2)); 1824 1825 if (s2 == NULL 1826 || simple_cst_equal (DECL_INITIAL (TREE_VALUE (s1)), 1827 DECL_INITIAL (TREE_VALUE (s2))) != 1) 1828 return false; 1829 } 1830 1831 return true; 1832 } 1833 1834 case UNION_TYPE: 1835 case RECORD_TYPE: 1836 1837 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2))) 1838 return false; 1839 1840 if (data->equiv && (C_TYPE_VARIABLE_SIZE (t1) || C_TYPE_VARIABLE_SIZE (t2))) 1841 return false; 1842 1843 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); 1844 s1 && s2; 1845 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) 1846 { 1847 gcc_assert (TREE_CODE (s1) == FIELD_DECL); 1848 gcc_assert (TREE_CODE (s2) == FIELD_DECL); 1849 1850 if (DECL_NAME (s1) != DECL_NAME (s2)) 1851 return false; 1852 1853 if (DECL_ALIGN (s1) != DECL_ALIGN (s2)) 1854 return false; 1855 1856 data->anon_field = !DECL_NAME (s1); 1857 1858 const struct tagged_tu_seen_cache *cache = data->cache; 1859 data->cache = &entry; 1860 bool ret = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), data); 1861 data->cache = cache; 1862 if (!ret) 1863 return false; 1864 1865 tree st1 = TYPE_SIZE (TREE_TYPE (s1)); 1866 tree st2 = TYPE_SIZE (TREE_TYPE (s2)); 1867 1868 if (data->equiv 1869 && st1 && TREE_CODE (st1) == INTEGER_CST 1870 && st2 && TREE_CODE (st2) == INTEGER_CST 1871 && !tree_int_cst_equal (st1, st2)) 1872 return false; 1873 } 1874 return true; 1875 1876 default: 1877 gcc_unreachable (); 1878 } 1879 } 1880 1881 /* Return true if two function types F1 and F2 are compatible. 1882 If either type specifies no argument types, 1883 the other must specify a fixed number of self-promoting arg types. 1884 Otherwise, if one type specifies only the number of arguments, 1885 the other must specify that number of self-promoting arg types. 1886 Otherwise, the argument types must match. */ 1887 1888 static bool 1889 function_types_compatible_p (const_tree f1, const_tree f2, 1890 struct comptypes_data *data) 1891 { 1892 tree args1, args2; 1893 /* 1 if no need for warning yet, 2 if warning cause has been seen. */ 1894 int val = 1; 1895 int val1; 1896 tree ret1, ret2; 1897 1898 ret1 = TREE_TYPE (f1); 1899 ret2 = TREE_TYPE (f2); 1900 1901 /* 'volatile' qualifiers on a function's return type used to mean 1902 the function is noreturn. */ 1903 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2)) 1904 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>"); 1905 if (TYPE_VOLATILE (ret1)) 1906 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1), 1907 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE); 1908 if (TYPE_VOLATILE (ret2)) 1909 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2), 1910 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE); 1911 val = comptypes_internal (ret1, ret2, data); 1912 if (val == 0) 1913 return 0; 1914 1915 args1 = TYPE_ARG_TYPES (f1); 1916 args2 = TYPE_ARG_TYPES (f2); 1917 1918 if ((args1 == NULL_TREE) != (args2 == NULL_TREE)) 1919 data->different_types_p = true; 1920 1921 /* An unspecified parmlist matches any specified parmlist 1922 whose argument types don't need default promotions. */ 1923 1924 if (args1 == NULL_TREE) 1925 { 1926 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2)) 1927 return 0; 1928 if (!self_promoting_args_p (args2)) 1929 return 0; 1930 /* If one of these types comes from a non-prototype fn definition, 1931 compare that with the other type's arglist. 1932 If they don't match, ask for a warning (but no error). */ 1933 if (TYPE_ACTUAL_ARG_TYPES (f1) 1934 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), 1935 data) != 1) 1936 { 1937 val = 1; 1938 data->warning_needed = true; 1939 } 1940 return val; 1941 } 1942 if (args2 == NULL_TREE) 1943 { 1944 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2)) 1945 return 0; 1946 if (!self_promoting_args_p (args1)) 1947 return 0; 1948 if (TYPE_ACTUAL_ARG_TYPES (f2) 1949 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), 1950 data) != 1) 1951 { 1952 val = 1; 1953 data->warning_needed = true; 1954 } 1955 return val; 1956 } 1957 1958 /* Both types have argument lists: compare them and propagate results. */ 1959 val1 = type_lists_compatible_p (args1, args2, data); 1960 return val1; 1961 } 1962 1963 /* Check two lists of types for compatibility, returning false for 1964 incompatible, true for compatible. */ 1965 1966 static bool 1967 type_lists_compatible_p (const_tree args1, const_tree args2, 1968 struct comptypes_data *data) 1969 { 1970 while (1) 1971 { 1972 tree a1, mv1, a2, mv2; 1973 if (args1 == NULL_TREE && args2 == NULL_TREE) 1974 return true; 1975 /* If one list is shorter than the other, 1976 they fail to match. */ 1977 if (args1 == NULL_TREE || args2 == NULL_TREE) 1978 return 0; 1979 mv1 = a1 = TREE_VALUE (args1); 1980 mv2 = a2 = TREE_VALUE (args2); 1981 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE) 1982 mv1 = (TYPE_ATOMIC (mv1) 1983 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1), 1984 TYPE_QUAL_ATOMIC) 1985 : TYPE_MAIN_VARIANT (mv1)); 1986 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE) 1987 mv2 = (TYPE_ATOMIC (mv2) 1988 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2), 1989 TYPE_QUAL_ATOMIC) 1990 : TYPE_MAIN_VARIANT (mv2)); 1991 /* A null pointer instead of a type 1992 means there is supposed to be an argument 1993 but nothing is specified about what type it has. 1994 So match anything that self-promotes. */ 1995 if ((a1 == NULL_TREE) != (a2 == NULL_TREE)) 1996 data->different_types_p = true; 1997 if (a1 == NULL_TREE) 1998 { 1999 if (c_type_promotes_to (a2) != a2) 2000 return 0; 2001 } 2002 else if (a2 == NULL_TREE) 2003 { 2004 if (c_type_promotes_to (a1) != a1) 2005 return 0; 2006 } 2007 /* If one of the lists has an error marker, ignore this arg. */ 2008 else if (TREE_CODE (a1) == ERROR_MARK 2009 || TREE_CODE (a2) == ERROR_MARK) 2010 ; 2011 else if (!comptypes_internal (mv1, mv2, data)) 2012 { 2013 data->different_types_p = true; 2014 /* Allow wait (union {union wait *u; int *i} *) 2015 and wait (union wait *) to be compatible. */ 2016 if (TREE_CODE (a1) == UNION_TYPE 2017 && (TYPE_NAME (a1) == NULL_TREE 2018 || TYPE_TRANSPARENT_AGGR (a1)) 2019 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST 2020 && tree_int_cst_equal (TYPE_SIZE (a1), 2021 TYPE_SIZE (a2))) 2022 { 2023 tree memb; 2024 for (memb = TYPE_FIELDS (a1); 2025 memb; memb = DECL_CHAIN (memb)) 2026 { 2027 tree mv3 = TREE_TYPE (memb); 2028 if (mv3 && mv3 != error_mark_node 2029 && TREE_CODE (mv3) != ARRAY_TYPE) 2030 mv3 = (TYPE_ATOMIC (mv3) 2031 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3), 2032 TYPE_QUAL_ATOMIC) 2033 : TYPE_MAIN_VARIANT (mv3)); 2034 if (comptypes_internal (mv3, mv2, data)) 2035 break; 2036 } 2037 if (memb == NULL_TREE) 2038 return 0; 2039 } 2040 else if (TREE_CODE (a2) == UNION_TYPE 2041 && (TYPE_NAME (a2) == NULL_TREE 2042 || TYPE_TRANSPARENT_AGGR (a2)) 2043 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST 2044 && tree_int_cst_equal (TYPE_SIZE (a2), 2045 TYPE_SIZE (a1))) 2046 { 2047 tree memb; 2048 for (memb = TYPE_FIELDS (a2); 2049 memb; memb = DECL_CHAIN (memb)) 2050 { 2051 tree mv3 = TREE_TYPE (memb); 2052 if (mv3 && mv3 != error_mark_node 2053 && TREE_CODE (mv3) != ARRAY_TYPE) 2054 mv3 = (TYPE_ATOMIC (mv3) 2055 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3), 2056 TYPE_QUAL_ATOMIC) 2057 : TYPE_MAIN_VARIANT (mv3)); 2058 if (comptypes_internal (mv3, mv1, data)) 2059 break; 2060 } 2061 if (memb == NULL_TREE) 2062 return 0; 2063 } 2064 else 2065 return 0; 2066 } 2067 2068 args1 = TREE_CHAIN (args1); 2069 args2 = TREE_CHAIN (args2); 2070 } 2071 } 2072 2073 /* Compute the size to increment a pointer by. When a function type or void 2075 type or incomplete type is passed, size_one_node is returned. 2076 This function does not emit any diagnostics; the caller is responsible 2077 for that. */ 2078 2079 static tree 2080 c_size_in_bytes (const_tree type) 2081 { 2082 enum tree_code code = TREE_CODE (type); 2083 2084 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK 2085 || !COMPLETE_TYPE_P (type)) 2086 return size_one_node; 2087 2088 /* Convert in case a char is more than one unit. */ 2089 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), 2090 size_int (TYPE_PRECISION (char_type_node) 2091 / BITS_PER_UNIT)); 2092 } 2093 2094 /* Return either DECL or its known constant value (if it has one). */ 2096 2097 tree 2098 decl_constant_value_1 (tree decl, bool in_init) 2099 { 2100 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */ 2101 TREE_CODE (decl) != PARM_DECL 2102 && !TREE_THIS_VOLATILE (decl) 2103 && TREE_READONLY (decl) 2104 && DECL_INITIAL (decl) != NULL_TREE 2105 && !error_operand_p (DECL_INITIAL (decl)) 2106 /* This is invalid if initial value is not constant. 2107 If it has either a function call, a memory reference, 2108 or a variable, then re-evaluating it could give different results. */ 2109 && TREE_CONSTANT (DECL_INITIAL (decl)) 2110 /* Check for cases where this is sub-optimal, even though valid. */ 2111 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)) 2112 return DECL_INITIAL (decl); 2113 return decl; 2114 } 2115 2116 /* Return either DECL or its known constant value (if it has one). 2117 Like the above, but always return decl outside of functions. */ 2118 2119 tree 2120 decl_constant_value (tree decl) 2121 { 2122 /* Don't change a variable array bound or initial value to a constant 2123 in a place where a variable is invalid. */ 2124 return current_function_decl ? decl_constant_value_1 (decl, false) : decl; 2125 } 2126 2127 /* Convert the array expression EXP to a pointer. */ 2128 static tree 2129 array_to_pointer_conversion (location_t loc, tree exp) 2130 { 2131 tree orig_exp = exp; 2132 tree type = TREE_TYPE (exp); 2133 tree adr; 2134 tree restype = TREE_TYPE (type); 2135 tree ptrtype; 2136 2137 gcc_assert (TREE_CODE (type) == ARRAY_TYPE); 2138 2139 STRIP_TYPE_NOPS (exp); 2140 2141 copy_warning (exp, orig_exp); 2142 2143 ptrtype = c_build_pointer_type (restype); 2144 2145 if (INDIRECT_REF_P (exp)) 2146 return convert (ptrtype, TREE_OPERAND (exp, 0)); 2147 2148 /* In C++ array compound literals are temporary objects unless they are 2149 const or appear in namespace scope, so they are destroyed too soon 2150 to use them for much of anything (c++/53220). */ 2151 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR) 2152 { 2153 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); 2154 if (!TREE_READONLY (decl) && !TREE_STATIC (decl)) 2155 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat, 2156 "converting an array compound literal to a pointer " 2157 "leads to a dangling pointer in C++"); 2158 } 2159 2160 adr = build_unary_op (loc, ADDR_EXPR, exp, true); 2161 return convert (ptrtype, adr); 2162 } 2163 2164 /* Convert the function expression EXP to a pointer. */ 2165 static tree 2166 function_to_pointer_conversion (location_t loc, tree exp) 2167 { 2168 tree orig_exp = exp; 2169 2170 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE); 2171 2172 STRIP_TYPE_NOPS (exp); 2173 2174 copy_warning (exp, orig_exp); 2175 2176 return build_unary_op (loc, ADDR_EXPR, exp, false); 2177 } 2178 2179 /* Mark EXP as read, not just set, for set but not used -Wunused 2180 warning purposes. */ 2181 2182 void 2183 mark_exp_read (tree exp) 2184 { 2185 switch (TREE_CODE (exp)) 2186 { 2187 case VAR_DECL: 2188 case PARM_DECL: 2189 DECL_READ_P (exp) = 1; 2190 break; 2191 case ARRAY_REF: 2192 case COMPONENT_REF: 2193 case MODIFY_EXPR: 2194 case REALPART_EXPR: 2195 case IMAGPART_EXPR: 2196 CASE_CONVERT: 2197 case ADDR_EXPR: 2198 case VIEW_CONVERT_EXPR: 2199 mark_exp_read (TREE_OPERAND (exp, 0)); 2200 break; 2201 case COMPOUND_EXPR: 2202 /* Pattern match what build_atomic_assign produces with modifycode 2203 NOP_EXPR. */ 2204 if (VAR_P (TREE_OPERAND (exp, 1)) 2205 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1)) 2206 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR) 2207 { 2208 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); 2209 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1); 2210 if (TREE_CODE (t1) == TARGET_EXPR 2211 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1) 2212 && TREE_CODE (t2) == CALL_EXPR) 2213 { 2214 tree fndecl = get_callee_fndecl (t2); 2215 tree arg = NULL_TREE; 2216 if (fndecl 2217 && TREE_CODE (fndecl) == FUNCTION_DECL 2218 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL) 2219 && call_expr_nargs (t2) >= 2) 2220 switch (DECL_FUNCTION_CODE (fndecl)) 2221 { 2222 case BUILT_IN_ATOMIC_STORE: 2223 arg = CALL_EXPR_ARG (t2, 1); 2224 break; 2225 case BUILT_IN_ATOMIC_STORE_1: 2226 case BUILT_IN_ATOMIC_STORE_2: 2227 case BUILT_IN_ATOMIC_STORE_4: 2228 case BUILT_IN_ATOMIC_STORE_8: 2229 case BUILT_IN_ATOMIC_STORE_16: 2230 arg = CALL_EXPR_ARG (t2, 0); 2231 break; 2232 default: 2233 break; 2234 } 2235 if (arg) 2236 { 2237 STRIP_NOPS (arg); 2238 if (TREE_CODE (arg) == ADDR_EXPR 2239 && DECL_P (TREE_OPERAND (arg, 0)) 2240 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0)))) 2241 mark_exp_read (TREE_OPERAND (arg, 0)); 2242 } 2243 } 2244 } 2245 /* FALLTHRU */ 2246 case C_MAYBE_CONST_EXPR: 2247 mark_exp_read (TREE_OPERAND (exp, 1)); 2248 break; 2249 case OMP_ARRAY_SECTION: 2250 mark_exp_read (TREE_OPERAND (exp, 0)); 2251 if (TREE_OPERAND (exp, 1)) 2252 mark_exp_read (TREE_OPERAND (exp, 1)); 2253 if (TREE_OPERAND (exp, 2)) 2254 mark_exp_read (TREE_OPERAND (exp, 2)); 2255 break; 2256 default: 2257 break; 2258 } 2259 } 2260 2261 /* Perform the default conversion of arrays and functions to pointers. 2262 Return the result of converting EXP. For any other expression, just 2263 return EXP. 2264 2265 LOC is the location of the expression. */ 2266 2267 struct c_expr 2268 default_function_array_conversion (location_t loc, struct c_expr exp) 2269 { 2270 tree orig_exp = exp.value; 2271 tree type = TREE_TYPE (exp.value); 2272 enum tree_code code = TREE_CODE (type); 2273 2274 switch (code) 2275 { 2276 case ARRAY_TYPE: 2277 { 2278 bool not_lvalue = false; 2279 bool lvalue_array_p; 2280 2281 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR 2282 || CONVERT_EXPR_P (exp.value)) 2283 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type) 2284 { 2285 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR) 2286 not_lvalue = true; 2287 exp.value = TREE_OPERAND (exp.value, 0); 2288 } 2289 2290 copy_warning (exp.value, orig_exp); 2291 2292 lvalue_array_p = !not_lvalue && lvalue_p (exp.value); 2293 if (!flag_isoc99 && !lvalue_array_p) 2294 { 2295 /* Before C99, non-lvalue arrays do not decay to pointers. 2296 Normally, using such an array would be invalid; but it can 2297 be used correctly inside sizeof or as a statement expression. 2298 Thus, do not give an error here; an error will result later. */ 2299 return exp; 2300 } 2301 2302 exp.value = array_to_pointer_conversion (loc, exp.value); 2303 } 2304 break; 2305 case FUNCTION_TYPE: 2306 exp.value = function_to_pointer_conversion (loc, exp.value); 2307 break; 2308 default: 2309 break; 2310 } 2311 2312 return exp; 2313 } 2314 2315 struct c_expr 2316 default_function_array_read_conversion (location_t loc, struct c_expr exp) 2317 { 2318 mark_exp_read (exp.value); 2319 return default_function_array_conversion (loc, exp); 2320 } 2321 2322 /* Return whether EXPR should be treated as an atomic lvalue for the 2323 purposes of load and store handling. */ 2324 2325 static bool 2326 really_atomic_lvalue (tree expr) 2327 { 2328 if (error_operand_p (expr)) 2329 return false; 2330 if (!TYPE_ATOMIC (TREE_TYPE (expr))) 2331 return false; 2332 if (!lvalue_p (expr)) 2333 return false; 2334 2335 /* Ignore _Atomic on register variables, since their addresses can't 2336 be taken so (a) atomicity is irrelevant and (b) the normal atomic 2337 sequences wouldn't work. Ignore _Atomic on structures containing 2338 bit-fields, since accessing elements of atomic structures or 2339 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if 2340 it's undefined at translation time or execution time, and the 2341 normal atomic sequences again wouldn't work. */ 2342 while (handled_component_p (expr)) 2343 { 2344 if (TREE_CODE (expr) == COMPONENT_REF 2345 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) 2346 return false; 2347 expr = TREE_OPERAND (expr, 0); 2348 } 2349 if (DECL_P (expr) && C_DECL_REGISTER (expr)) 2350 return false; 2351 return true; 2352 } 2353 2354 /* If EXPR is a named constant (C23) derived from a constexpr variable 2355 - that is, a reference to such a variable, or a member extracted by 2356 a sequence of structure and union (but not array) member accesses 2357 (where union member accesses must access the same member as 2358 initialized) - then return the corresponding initializer; 2359 otherwise, return NULL_TREE. */ 2360 2361 static tree 2362 maybe_get_constexpr_init (tree expr) 2363 { 2364 tree decl = NULL_TREE; 2365 if (TREE_CODE (expr) == VAR_DECL) 2366 decl = expr; 2367 else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR) 2368 decl = COMPOUND_LITERAL_EXPR_DECL (expr); 2369 if (decl 2370 && C_DECL_DECLARED_CONSTEXPR (decl) 2371 && DECL_INITIAL (decl) != NULL_TREE 2372 && !error_operand_p (DECL_INITIAL (decl))) 2373 return DECL_INITIAL (decl); 2374 if (TREE_CODE (expr) != COMPONENT_REF) 2375 return NULL_TREE; 2376 tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0)); 2377 if (inner == NULL_TREE) 2378 return NULL_TREE; 2379 while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR) 2380 && !error_operand_p (inner) 2381 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner)) 2382 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0))))) 2383 inner = TREE_OPERAND (inner, 0); 2384 if (TREE_CODE (inner) != CONSTRUCTOR) 2385 return NULL_TREE; 2386 tree field = TREE_OPERAND (expr, 1); 2387 unsigned HOST_WIDE_INT cidx; 2388 tree cfield, cvalue; 2389 bool have_other_init = false; 2390 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue) 2391 { 2392 if (cfield == field) 2393 return cvalue; 2394 have_other_init = true; 2395 } 2396 if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE 2397 && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner)))) 2398 return NULL_TREE; 2399 /* Return a default initializer. */ 2400 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr))) 2401 return build_constructor (TREE_TYPE (expr), NULL); 2402 return build_zero_cst (TREE_TYPE (expr)); 2403 } 2404 2405 /* Convert expression EXP (location LOC) from lvalue to rvalue, 2406 including converting functions and arrays to pointers if CONVERT_P. 2407 If READ_P, also mark the expression as having been read. If 2408 FOR_INIT, constexpr expressions of structure and union type should 2409 be replaced by the corresponding CONSTRUCTOR; otherwise, only 2410 constexpr scalars (including elements of structures and unions) are 2411 replaced by their initializers. */ 2412 2413 struct c_expr 2414 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp, 2415 bool convert_p, bool read_p, bool for_init) 2416 { 2417 bool force_non_npc = false; 2418 if (read_p) 2419 mark_exp_read (exp.value); 2420 if (convert_p) 2421 exp = default_function_array_conversion (loc, exp); 2422 if (!VOID_TYPE_P (TREE_TYPE (exp.value))) 2423 exp.value = require_complete_type (loc, exp.value); 2424 if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value))) 2425 { 2426 tree init = maybe_get_constexpr_init (exp.value); 2427 if (init != NULL_TREE) 2428 { 2429 /* A named constant of pointer type or type nullptr_t is not 2430 a null pointer constant even if the initializer is 2431 one. */ 2432 if (TREE_CODE (init) == INTEGER_CST 2433 && !INTEGRAL_TYPE_P (TREE_TYPE (init)) 2434 && integer_zerop (init)) 2435 force_non_npc = true; 2436 exp.value = init; 2437 } 2438 } 2439 if (really_atomic_lvalue (exp.value)) 2440 { 2441 vec<tree, va_gc> *params; 2442 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call; 2443 tree expr_type = TREE_TYPE (exp.value); 2444 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false); 2445 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST); 2446 2447 gcc_assert (TYPE_ATOMIC (expr_type)); 2448 2449 /* Expansion of a generic atomic load may require an addition 2450 element, so allocate enough to prevent a resize. */ 2451 vec_alloc (params, 4); 2452 2453 /* Remove the qualifiers for the rest of the expressions and 2454 create the VAL temp variable to hold the RHS. */ 2455 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED); 2456 tmp = create_tmp_var_raw (nonatomic_type); 2457 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false); 2458 TREE_ADDRESSABLE (tmp) = 1; 2459 /* Do not disable warnings for TMP even though it's artificial. 2460 -Winvalid-memory-model depends on it. */ 2461 2462 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */ 2463 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD); 2464 params->quick_push (expr_addr); 2465 params->quick_push (tmp_addr); 2466 params->quick_push (seq_cst); 2467 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); 2468 2469 /* EXPR is always read. */ 2470 mark_exp_read (exp.value); 2471 2472 /* Return tmp which contains the value loaded. */ 2473 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call, 2474 NULL_TREE, NULL_TREE); 2475 } 2476 if (convert_p && !error_operand_p (exp.value) 2477 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE)) 2478 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value); 2479 if (force_non_npc) 2480 exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value); 2481 2482 { 2483 tree false_value, true_value; 2484 if (convert_p && !error_operand_p (exp.value) 2485 && c_hardbool_type_attr (TREE_TYPE (exp.value), 2486 &false_value, &true_value)) 2487 { 2488 tree t = save_expr (exp.value); 2489 2490 mark_exp_read (exp.value); 2491 2492 tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP); 2493 tree expr = build_call_expr_loc (loc, trapfn, 0); 2494 expr = build_compound_expr (loc, expr, boolean_true_node); 2495 expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node, 2496 fold_build2_loc (loc, NE_EXPR, 2497 boolean_type_node, 2498 t, true_value), 2499 expr, boolean_true_node); 2500 expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node, 2501 fold_build2_loc (loc, NE_EXPR, 2502 boolean_type_node, 2503 t, false_value), 2504 expr, boolean_false_node); 2505 2506 exp.value = expr; 2507 } 2508 } 2509 2510 return exp; 2511 } 2512 2513 /* EXP is an expression of integer type. Apply the integer promotions 2514 to it and return the promoted value. */ 2515 2516 tree 2517 perform_integral_promotions (tree exp) 2518 { 2519 tree type = TREE_TYPE (exp); 2520 enum tree_code code = TREE_CODE (type); 2521 2522 gcc_assert (INTEGRAL_TYPE_P (type)); 2523 2524 /* Convert enums to the result of applying the integer promotions to 2525 their underlying type. */ 2526 if (code == ENUMERAL_TYPE) 2527 { 2528 type = ENUM_UNDERLYING_TYPE (type); 2529 if (c_promoting_integer_type_p (type)) 2530 { 2531 if (TYPE_UNSIGNED (type) 2532 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) 2533 type = unsigned_type_node; 2534 else 2535 type = integer_type_node; 2536 } 2537 2538 return convert (type, exp); 2539 } 2540 2541 /* ??? This should no longer be needed now bit-fields have their 2542 proper types. */ 2543 if (TREE_CODE (exp) == COMPONENT_REF 2544 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))) 2545 { 2546 if (TREE_CODE (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))) 2547 == BITINT_TYPE) 2548 return convert (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)), exp); 2549 /* If it's thinner than an int, promote it like a 2550 c_promoting_integer_type_p, otherwise leave it alone. */ 2551 if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), 2552 TYPE_PRECISION (integer_type_node)) < 0) 2553 return convert (integer_type_node, exp); 2554 } 2555 2556 if (c_promoting_integer_type_p (type)) 2557 { 2558 /* Preserve unsignedness if not really getting any wider. */ 2559 if (TYPE_UNSIGNED (type) 2560 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) 2561 return convert (unsigned_type_node, exp); 2562 2563 return convert (integer_type_node, exp); 2564 } 2565 2566 return exp; 2567 } 2568 2569 2570 /* Perform default promotions for C data used in expressions. 2571 Enumeral types or short or char are converted to int. 2572 In addition, manifest constants symbols are replaced by their values. */ 2573 2574 tree 2575 default_conversion (tree exp) 2576 { 2577 tree orig_exp; 2578 tree type = TREE_TYPE (exp); 2579 enum tree_code code = TREE_CODE (type); 2580 tree promoted_type; 2581 2582 mark_exp_read (exp); 2583 2584 /* Functions and arrays have been converted during parsing. */ 2585 gcc_assert (code != FUNCTION_TYPE); 2586 if (code == ARRAY_TYPE) 2587 return exp; 2588 2589 /* Constants can be used directly unless they're not loadable. */ 2590 if (TREE_CODE (exp) == CONST_DECL) 2591 exp = DECL_INITIAL (exp); 2592 2593 /* Strip no-op conversions. */ 2594 orig_exp = exp; 2595 STRIP_TYPE_NOPS (exp); 2596 2597 copy_warning (exp, orig_exp); 2598 2599 if (code == VOID_TYPE) 2600 { 2601 error_at (EXPR_LOC_OR_LOC (exp, input_location), 2602 "void value not ignored as it ought to be"); 2603 return error_mark_node; 2604 } 2605 2606 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp); 2607 if (exp == error_mark_node) 2608 return error_mark_node; 2609 2610 promoted_type = targetm.promoted_type (type); 2611 if (promoted_type) 2612 return convert (promoted_type, exp); 2613 2614 if (INTEGRAL_TYPE_P (type)) 2615 return perform_integral_promotions (exp); 2616 2617 return exp; 2618 } 2619 2620 /* Look up COMPONENT in a structure or union TYPE. 2622 2623 If the component name is not found, returns NULL_TREE. Otherwise, 2624 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL 2625 stepping down the chain to the component, which is in the last 2626 TREE_VALUE of the list. Normally the list is of length one, but if 2627 the component is embedded within (nested) anonymous structures or 2628 unions, the list steps down the chain to the component. */ 2629 2630 static tree 2631 lookup_field (tree type, tree component) 2632 { 2633 tree field; 2634 2635 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers 2636 to the field elements. Use a binary search on this array to quickly 2637 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC 2638 will always be set for structures which have many elements. 2639 2640 Duplicate field checking replaces duplicates with NULL_TREE so 2641 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that 2642 case just iterate using DECL_CHAIN. */ 2643 2644 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s 2645 && !seen_error ()) 2646 { 2647 int bot, top, half; 2648 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0]; 2649 2650 field = TYPE_FIELDS (type); 2651 bot = 0; 2652 top = TYPE_LANG_SPECIFIC (type)->s->len; 2653 while (top - bot > 1) 2654 { 2655 half = (top - bot + 1) >> 1; 2656 field = field_array[bot+half]; 2657 2658 if (DECL_NAME (field) == NULL_TREE) 2659 { 2660 /* Step through all anon unions in linear fashion. */ 2661 while (DECL_NAME (field_array[bot]) == NULL_TREE) 2662 { 2663 field = field_array[bot++]; 2664 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))) 2665 { 2666 tree anon = lookup_field (TREE_TYPE (field), component); 2667 2668 if (anon) 2669 return tree_cons (NULL_TREE, field, anon); 2670 2671 /* The Plan 9 compiler permits referring 2672 directly to an anonymous struct/union field 2673 using a typedef name. */ 2674 if (flag_plan9_extensions 2675 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE 2676 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field))) 2677 == TYPE_DECL) 2678 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) 2679 == component)) 2680 break; 2681 } 2682 } 2683 2684 /* Entire record is only anon unions. */ 2685 if (bot > top) 2686 return NULL_TREE; 2687 2688 /* Restart the binary search, with new lower bound. */ 2689 continue; 2690 } 2691 2692 if (DECL_NAME (field) == component) 2693 break; 2694 if (DECL_NAME (field) < component) 2695 bot += half; 2696 else 2697 top = bot + half; 2698 } 2699 2700 if (DECL_NAME (field_array[bot]) == component) 2701 field = field_array[bot]; 2702 else if (DECL_NAME (field) != component) 2703 return NULL_TREE; 2704 } 2705 else 2706 { 2707 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 2708 { 2709 if (DECL_NAME (field) == NULL_TREE 2710 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))) 2711 { 2712 tree anon = lookup_field (TREE_TYPE (field), component); 2713 2714 if (anon) 2715 return tree_cons (NULL_TREE, field, anon); 2716 2717 /* The Plan 9 compiler permits referring directly to an 2718 anonymous struct/union field using a typedef 2719 name. */ 2720 if (flag_plan9_extensions 2721 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE 2722 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL 2723 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) 2724 == component)) 2725 break; 2726 } 2727 2728 if (DECL_NAME (field) == component) 2729 break; 2730 } 2731 2732 if (field == NULL_TREE) 2733 return NULL_TREE; 2734 } 2735 2736 return tree_cons (NULL_TREE, field, NULL_TREE); 2737 } 2738 2739 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */ 2740 2741 static void 2742 lookup_field_fuzzy_find_candidates (tree type, tree component, 2743 vec<tree> *candidates) 2744 { 2745 tree field; 2746 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 2747 { 2748 if (DECL_NAME (field) == NULL_TREE 2749 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))) 2750 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component, 2751 candidates); 2752 2753 if (DECL_NAME (field)) 2754 candidates->safe_push (DECL_NAME (field)); 2755 } 2756 } 2757 2758 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE, 2759 rather than returning a TREE_LIST for an exact match. */ 2760 2761 static tree 2762 lookup_field_fuzzy (tree type, tree component) 2763 { 2764 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE); 2765 2766 /* First, gather a list of candidates. */ 2767 auto_vec <tree> candidates; 2768 2769 lookup_field_fuzzy_find_candidates (type, component, 2770 &candidates); 2771 2772 return find_closest_identifier (component, &candidates); 2773 } 2774 2775 /* Support function for build_component_ref's error-handling. 2776 2777 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a 2778 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */ 2779 2780 static bool 2781 should_suggest_deref_p (tree datum_type) 2782 { 2783 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax 2784 allows "." for ptrs; we could be handling a failed attempt 2785 to access a property. */ 2786 if (c_dialect_objc ()) 2787 return false; 2788 2789 /* Only suggest it for pointers... */ 2790 if (TREE_CODE (datum_type) != POINTER_TYPE) 2791 return false; 2792 2793 /* ...to structs/unions. */ 2794 tree underlying_type = TREE_TYPE (datum_type); 2795 enum tree_code code = TREE_CODE (underlying_type); 2796 if (code == RECORD_TYPE || code == UNION_TYPE) 2797 return true; 2798 else 2799 return false; 2800 } 2801 2802 /* Make an expression to refer to the COMPONENT field of structure or 2803 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the 2804 location of the COMPONENT_REF. COMPONENT_LOC is the location 2805 of COMPONENT. ARROW_LOC is the location of the first -> operand if 2806 it is from -> operator. */ 2807 2808 tree 2809 build_component_ref (location_t loc, tree datum, tree component, 2810 location_t component_loc, location_t arrow_loc) 2811 { 2812 tree type = TREE_TYPE (datum); 2813 enum tree_code code = TREE_CODE (type); 2814 tree field = NULL; 2815 tree ref; 2816 bool datum_lvalue = lvalue_p (datum); 2817 2818 if (!objc_is_public (datum, component)) 2819 return error_mark_node; 2820 2821 /* Detect Objective-C property syntax object.property. */ 2822 if (c_dialect_objc () 2823 && (ref = objc_maybe_build_component_ref (datum, component))) 2824 return ref; 2825 2826 /* See if there is a field or component with name COMPONENT. */ 2827 2828 if (code == RECORD_TYPE || code == UNION_TYPE) 2829 { 2830 if (!COMPLETE_TYPE_P (type)) 2831 { 2832 c_incomplete_type_error (loc, NULL_TREE, type); 2833 return error_mark_node; 2834 } 2835 2836 field = lookup_field (type, component); 2837 2838 if (!field) 2839 { 2840 tree guessed_id = lookup_field_fuzzy (type, component); 2841 if (guessed_id) 2842 { 2843 /* Attempt to provide a fixit replacement hint, if 2844 we have a valid range for the component. */ 2845 location_t reported_loc 2846 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc; 2847 gcc_rich_location rich_loc (reported_loc); 2848 if (component_loc != UNKNOWN_LOCATION) 2849 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id); 2850 error_at (&rich_loc, 2851 "%qT has no member named %qE; did you mean %qE?", 2852 type, component, guessed_id); 2853 } 2854 else 2855 error_at (loc, "%qT has no member named %qE", type, component); 2856 return error_mark_node; 2857 } 2858 2859 /* Accessing elements of atomic structures or unions is undefined 2860 behavior (C11 6.5.2.3#5). */ 2861 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0) 2862 { 2863 if (code == RECORD_TYPE) 2864 warning_at (loc, 0, "accessing a member %qE of an atomic " 2865 "structure %qE", component, datum); 2866 else 2867 warning_at (loc, 0, "accessing a member %qE of an atomic " 2868 "union %qE", component, datum); 2869 } 2870 2871 /* Chain the COMPONENT_REFs if necessary down to the FIELD. 2872 This might be better solved in future the way the C++ front 2873 end does it - by giving the anonymous entities each a 2874 separate name and type, and then have build_component_ref 2875 recursively call itself. We can't do that here. */ 2876 do 2877 { 2878 tree subdatum = TREE_VALUE (field); 2879 int quals; 2880 tree subtype; 2881 bool use_datum_quals; 2882 2883 if (TREE_TYPE (subdatum) == error_mark_node) 2884 return error_mark_node; 2885 2886 /* If this is an rvalue, it does not have qualifiers in C 2887 standard terms and we must avoid propagating such 2888 qualifiers down to a non-lvalue array that is then 2889 converted to a pointer. */ 2890 use_datum_quals = (datum_lvalue 2891 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE); 2892 2893 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum))); 2894 if (use_datum_quals) 2895 quals |= TYPE_QUALS (TREE_TYPE (datum)); 2896 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals); 2897 2898 ref = build3 (COMPONENT_REF, subtype, datum, subdatum, 2899 NULL_TREE); 2900 SET_EXPR_LOCATION (ref, loc); 2901 if (TREE_READONLY (subdatum) 2902 || (use_datum_quals && TREE_READONLY (datum))) 2903 TREE_READONLY (ref) = 1; 2904 if (TREE_THIS_VOLATILE (subdatum) 2905 || (use_datum_quals && TREE_THIS_VOLATILE (datum))) 2906 TREE_THIS_VOLATILE (ref) = 1; 2907 2908 if (TREE_UNAVAILABLE (subdatum)) 2909 error_unavailable_use (subdatum, NULL_TREE); 2910 else if (TREE_DEPRECATED (subdatum)) 2911 warn_deprecated_use (subdatum, NULL_TREE); 2912 2913 datum = ref; 2914 2915 field = TREE_CHAIN (field); 2916 } 2917 while (field); 2918 2919 return ref; 2920 } 2921 else if (should_suggest_deref_p (type)) 2922 { 2923 /* Special-case the error message for "ptr.field" for the case 2924 where the user has confused "." vs "->". */ 2925 rich_location richloc (line_table, loc); 2926 if (INDIRECT_REF_P (datum) && arrow_loc != UNKNOWN_LOCATION) 2927 { 2928 richloc.add_fixit_insert_before (arrow_loc, "(*"); 2929 richloc.add_fixit_insert_after (arrow_loc, ")"); 2930 error_at (&richloc, 2931 "%qE is a pointer to pointer; did you mean to dereference " 2932 "it before applying %<->%> to it?", 2933 TREE_OPERAND (datum, 0)); 2934 } 2935 else 2936 { 2937 /* "loc" should be the "." token. */ 2938 richloc.add_fixit_replace ("->"); 2939 error_at (&richloc, 2940 "%qE is a pointer; did you mean to use %<->%>?", 2941 datum); 2942 } 2943 return error_mark_node; 2944 } 2945 else if (code != ERROR_MARK) 2946 error_at (loc, 2947 "request for member %qE in something not a structure or union", 2948 component); 2949 2950 return error_mark_node; 2951 } 2952 2953 /* Given an expression PTR for a pointer, return an expression 2955 for the value pointed to. 2956 ERRORSTRING is the name of the operator to appear in error messages. 2957 2958 LOC is the location to use for the generated tree. */ 2959 2960 tree 2961 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring) 2962 { 2963 tree pointer = default_conversion (ptr); 2964 tree type = TREE_TYPE (pointer); 2965 tree ref; 2966 2967 if (TREE_CODE (type) == POINTER_TYPE) 2968 { 2969 if (CONVERT_EXPR_P (pointer) 2970 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR) 2971 { 2972 /* If a warning is issued, mark it to avoid duplicates from 2973 the backend. This only needs to be done at 2974 warn_strict_aliasing > 2. */ 2975 if (warn_strict_aliasing > 2) 2976 if (strict_aliasing_warning (EXPR_LOCATION (pointer), 2977 type, TREE_OPERAND (pointer, 0))) 2978 suppress_warning (pointer, OPT_Wstrict_aliasing_); 2979 } 2980 2981 if (TREE_CODE (pointer) == ADDR_EXPR 2982 && (TREE_TYPE (TREE_OPERAND (pointer, 0)) 2983 == TREE_TYPE (type))) 2984 { 2985 ref = TREE_OPERAND (pointer, 0); 2986 protected_set_expr_location (ref, loc); 2987 return ref; 2988 } 2989 else 2990 { 2991 tree t = TREE_TYPE (type); 2992 2993 ref = build1 (INDIRECT_REF, t, pointer); 2994 2995 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0) 2996 warning_at (loc, 0, "dereferencing %<void *%> pointer"); 2997 2998 /* We *must* set TREE_READONLY when dereferencing a pointer to const, 2999 so that we get the proper error message if the result is used 3000 to assign to. Also, &* is supposed to be a no-op. 3001 And ANSI C seems to specify that the type of the result 3002 should be the const type. */ 3003 /* A de-reference of a pointer to const is not a const. It is valid 3004 to change it via some other pointer. */ 3005 TREE_READONLY (ref) = TYPE_READONLY (t); 3006 TREE_SIDE_EFFECTS (ref) 3007 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer); 3008 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); 3009 protected_set_expr_location (ref, loc); 3010 return ref; 3011 } 3012 } 3013 else if (TREE_CODE (pointer) != ERROR_MARK) 3014 invalid_indirection_error (loc, type, errstring); 3015 3016 return error_mark_node; 3017 } 3018 3019 /* This handles expressions of the form "a[i]", which denotes 3020 an array reference. 3021 3022 This is logically equivalent in C to *(a+i), but we may do it differently. 3023 If A is a variable or a member, we generate a primitive ARRAY_REF. 3024 This avoids forcing the array out of registers, and can work on 3025 arrays that are not lvalues (for example, members of structures returned 3026 by functions). 3027 3028 For vector types, allow vector[i] but not i[vector], and create 3029 *(((type*)&vectortype) + i) for the expression. 3030 3031 LOC is the location to use for the returned expression. */ 3032 3033 tree 3034 build_array_ref (location_t loc, tree array, tree index) 3035 { 3036 tree ret; 3037 bool swapped = false; 3038 if (TREE_TYPE (array) == error_mark_node 3039 || TREE_TYPE (index) == error_mark_node) 3040 return error_mark_node; 3041 3042 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE 3043 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE 3044 /* Allow vector[index] but not index[vector]. */ 3045 && !gnu_vector_type_p (TREE_TYPE (array))) 3046 { 3047 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE 3048 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE) 3049 { 3050 error_at (loc, 3051 "subscripted value is neither array nor pointer nor vector"); 3052 3053 return error_mark_node; 3054 } 3055 std::swap (array, index); 3056 swapped = true; 3057 } 3058 3059 if (!INTEGRAL_TYPE_P (TREE_TYPE (index))) 3060 { 3061 error_at (loc, "array subscript is not an integer"); 3062 return error_mark_node; 3063 } 3064 3065 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE) 3066 { 3067 error_at (loc, "subscripted value is pointer to function"); 3068 return error_mark_node; 3069 } 3070 3071 /* ??? Existing practice has been to warn only when the char 3072 index is syntactically the index, not for char[array]. */ 3073 if (!swapped) 3074 warn_array_subscript_with_type_char (loc, index); 3075 3076 /* Apply default promotions *after* noticing character types. */ 3077 index = default_conversion (index); 3078 if (index == error_mark_node) 3079 return error_mark_node; 3080 3081 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE 3082 || TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE); 3083 3084 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array)); 3085 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index); 3086 3087 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) 3088 { 3089 tree rval, type; 3090 3091 /* An array that is indexed by a non-constant 3092 cannot be stored in a register; we must be able to do 3093 address arithmetic on its address. 3094 Likewise an array of elements of variable size. */ 3095 if (TREE_CODE (index) != INTEGER_CST 3096 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) 3097 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) 3098 { 3099 if (!c_mark_addressable (array, true)) 3100 return error_mark_node; 3101 } 3102 /* An array that is indexed by a constant value which is not within 3103 the array bounds cannot be stored in a register either; because we 3104 would get a crash in store_bit_field/extract_bit_field when trying 3105 to access a non-existent part of the register. */ 3106 if (TREE_CODE (index) == INTEGER_CST 3107 && TYPE_DOMAIN (TREE_TYPE (array)) 3108 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array)))) 3109 { 3110 if (!c_mark_addressable (array)) 3111 return error_mark_node; 3112 } 3113 3114 if ((pedantic || warn_c90_c99_compat) 3115 && ! was_vector) 3116 { 3117 tree foo = array; 3118 while (TREE_CODE (foo) == COMPONENT_REF) 3119 foo = TREE_OPERAND (foo, 0); 3120 if (VAR_P (foo) && C_DECL_REGISTER (foo)) 3121 pedwarn (loc, OPT_Wpedantic, 3122 "ISO C forbids subscripting %<register%> array"); 3123 else if (!lvalue_p (foo)) 3124 pedwarn_c90 (loc, OPT_Wpedantic, 3125 "ISO C90 forbids subscripting non-lvalue " 3126 "array"); 3127 } 3128 3129 if (TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE 3130 && TYPE_PRECISION (TREE_TYPE (index)) > TYPE_PRECISION (sizetype)) 3131 index = fold_convert (sizetype, index); 3132 3133 type = TREE_TYPE (TREE_TYPE (array)); 3134 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE); 3135 /* Array ref is const/volatile if the array elements are 3136 or if the array is. */ 3137 TREE_READONLY (rval) 3138 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) 3139 | TREE_READONLY (array)); 3140 TREE_SIDE_EFFECTS (rval) 3141 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) 3142 | TREE_SIDE_EFFECTS (array)); 3143 TREE_THIS_VOLATILE (rval) 3144 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) 3145 /* This was added by rms on 16 Nov 91. 3146 It fixes vol struct foo *a; a->elts[1] 3147 in an inline function. 3148 Hope it doesn't break something else. */ 3149 | TREE_THIS_VOLATILE (array)); 3150 ret = require_complete_type (loc, rval); 3151 protected_set_expr_location (ret, loc); 3152 if (non_lvalue) 3153 ret = non_lvalue_loc (loc, ret); 3154 return ret; 3155 } 3156 else 3157 { 3158 tree ar = default_conversion (array); 3159 3160 if (ar == error_mark_node) 3161 return ar; 3162 3163 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE); 3164 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE); 3165 3166 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar, 3167 index, false), 3168 RO_ARRAY_INDEXING); 3169 if (non_lvalue) 3170 ret = non_lvalue_loc (loc, ret); 3171 return ret; 3172 } 3173 } 3174 3175 /* Build an OpenMP array section reference, creating an exact type for the 3176 resulting expression based on the element type and bounds if possible. If 3177 we have variable bounds, create an incomplete array type for the result 3178 instead. */ 3179 3180 tree 3181 build_omp_array_section (location_t loc, tree array, tree index, tree length) 3182 { 3183 tree type = TREE_TYPE (array); 3184 gcc_assert (type); 3185 3186 tree sectype, eltype = TREE_TYPE (type); 3187 3188 /* It's not an array or pointer type. Just reuse the type of the original 3189 expression as the type of the array section (an error will be raised 3190 anyway, later). */ 3191 if (eltype == NULL_TREE || error_operand_p (eltype)) 3192 sectype = TREE_TYPE (array); 3193 else 3194 { 3195 tree idxtype = NULL_TREE; 3196 3197 if (index != NULL_TREE 3198 && length != NULL_TREE 3199 && INTEGRAL_TYPE_P (TREE_TYPE (index)) 3200 && INTEGRAL_TYPE_P (TREE_TYPE (length))) 3201 { 3202 tree low = fold_convert (sizetype, index); 3203 tree high = fold_convert (sizetype, length); 3204 high = size_binop (PLUS_EXPR, low, high); 3205 high = size_binop (MINUS_EXPR, high, size_one_node); 3206 idxtype = build_range_type (sizetype, low, high); 3207 } 3208 else if ((index == NULL_TREE || integer_zerop (index)) 3209 && length != NULL_TREE 3210 && INTEGRAL_TYPE_P (TREE_TYPE (length))) 3211 idxtype = build_index_type (length); 3212 3213 gcc_assert (!error_operand_p (idxtype)); 3214 3215 sectype = c_build_array_type (eltype, idxtype); 3216 } 3217 3218 return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array, index, length); 3219 } 3220 3221 3222 /* Build an external reference to identifier ID. FUN indicates 3224 whether this will be used for a function call. LOC is the source 3225 location of the identifier. This sets *TYPE to the type of the 3226 identifier, which is not the same as the type of the returned value 3227 for CONST_DECLs defined as enum constants. If the type of the 3228 identifier is not available, *TYPE is set to NULL. */ 3229 tree 3230 build_external_ref (location_t loc, tree id, bool fun, tree *type) 3231 { 3232 tree ref; 3233 tree decl = lookup_name (id); 3234 3235 /* In Objective-C, an instance variable (ivar) may be preferred to 3236 whatever lookup_name() found. */ 3237 decl = objc_lookup_ivar (decl, id); 3238 3239 *type = NULL; 3240 if (decl && decl != error_mark_node) 3241 { 3242 ref = decl; 3243 *type = TREE_TYPE (ref); 3244 if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl)) 3245 error_at (loc, "underspecified %qD referenced in its initializer", 3246 decl); 3247 } 3248 else if (fun) 3249 /* Implicit function declaration. */ 3250 ref = implicitly_declare (loc, id); 3251 else if (decl == error_mark_node) 3252 /* Don't complain about something that's already been 3253 complained about. */ 3254 return error_mark_node; 3255 else 3256 { 3257 undeclared_variable (loc, id); 3258 return error_mark_node; 3259 } 3260 3261 /* For an OpenMP map clause, we can get better diagnostics for decls with 3262 unmappable types if we return the decl with an error_mark_node type, 3263 rather than returning error_mark_node for the decl itself. */ 3264 if (TREE_TYPE (ref) == error_mark_node 3265 && !c_omp_array_section_p) 3266 return error_mark_node; 3267 3268 if (TREE_UNAVAILABLE (ref)) 3269 error_unavailable_use (ref, NULL_TREE); 3270 else if (TREE_DEPRECATED (ref)) 3271 warn_deprecated_use (ref, NULL_TREE); 3272 3273 /* Recursive call does not count as usage. */ 3274 if (ref != current_function_decl) 3275 { 3276 TREE_USED (ref) = 1; 3277 } 3278 3279 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof) 3280 { 3281 if (!in_sizeof && !in_typeof) 3282 C_DECL_USED (ref) = 1; 3283 else if (DECL_INITIAL (ref) == NULL_TREE 3284 && DECL_EXTERNAL (ref) 3285 && !TREE_PUBLIC (ref)) 3286 record_maybe_used_decl (ref); 3287 } 3288 3289 if (TREE_CODE (ref) == CONST_DECL) 3290 { 3291 used_types_insert (TREE_TYPE (ref)); 3292 3293 if (warn_cxx_compat 3294 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE 3295 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref))) 3296 { 3297 warning_at (loc, OPT_Wc___compat, 3298 ("enum constant defined in struct or union " 3299 "is not visible in C++")); 3300 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here"); 3301 } 3302 3303 ref = DECL_INITIAL (ref); 3304 TREE_CONSTANT (ref) = 1; 3305 } 3306 else if (current_function_decl != NULL_TREE 3307 && !DECL_FILE_SCOPE_P (current_function_decl) 3308 && (VAR_OR_FUNCTION_DECL_P (ref) 3309 || TREE_CODE (ref) == PARM_DECL)) 3310 { 3311 tree context = decl_function_context (ref); 3312 3313 if (context != NULL_TREE && context != current_function_decl) 3314 DECL_NONLOCAL (ref) = 1; 3315 } 3316 /* C99 6.7.4p3: An inline definition of a function with external 3317 linkage ... shall not contain a reference to an identifier with 3318 internal linkage. */ 3319 else if (current_function_decl != NULL_TREE 3320 && DECL_DECLARED_INLINE_P (current_function_decl) 3321 && DECL_EXTERNAL (current_function_decl) 3322 && VAR_OR_FUNCTION_DECL_P (ref) 3323 && (!VAR_P (ref) || TREE_STATIC (ref)) 3324 && ! TREE_PUBLIC (ref) 3325 && DECL_CONTEXT (ref) != current_function_decl) 3326 record_inline_static (loc, current_function_decl, ref, 3327 csi_internal); 3328 3329 return ref; 3330 } 3331 3332 /* Record details of decls possibly used inside sizeof or typeof. */ 3333 struct maybe_used_decl 3334 { 3335 /* The decl. */ 3336 tree decl; 3337 /* The level seen at (in_sizeof + in_typeof). */ 3338 int level; 3339 /* The next one at this level or above, or NULL. */ 3340 struct maybe_used_decl *next; 3341 }; 3342 3343 static struct maybe_used_decl *maybe_used_decls; 3344 3345 /* Record that DECL, an undefined static function reference seen 3346 inside sizeof or typeof, might be used if the operand of sizeof is 3347 a VLA type or the operand of typeof is a variably modified 3348 type. */ 3349 3350 static void 3351 record_maybe_used_decl (tree decl) 3352 { 3353 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl); 3354 t->decl = decl; 3355 t->level = in_sizeof + in_typeof; 3356 t->next = maybe_used_decls; 3357 maybe_used_decls = t; 3358 } 3359 3360 /* Pop the stack of decls possibly used inside sizeof or typeof. If 3361 USED is false, just discard them. If it is true, mark them used 3362 (if no longer inside sizeof or typeof) or move them to the next 3363 level up (if still inside sizeof or typeof). */ 3364 3365 void 3366 pop_maybe_used (bool used) 3367 { 3368 struct maybe_used_decl *p = maybe_used_decls; 3369 int cur_level = in_sizeof + in_typeof; 3370 while (p && p->level > cur_level) 3371 { 3372 if (used) 3373 { 3374 if (cur_level == 0) 3375 C_DECL_USED (p->decl) = 1; 3376 else 3377 p->level = cur_level; 3378 } 3379 p = p->next; 3380 } 3381 if (!used || cur_level == 0) 3382 maybe_used_decls = p; 3383 } 3384 3385 /* Return the result of sizeof applied to EXPR. */ 3386 3387 struct c_expr 3388 c_expr_sizeof_expr (location_t loc, struct c_expr expr) 3389 { 3390 struct c_expr ret; 3391 if (expr.value == error_mark_node) 3392 { 3393 ret.value = error_mark_node; 3394 ret.original_code = ERROR_MARK; 3395 ret.original_type = NULL; 3396 ret.m_decimal = 0; 3397 pop_maybe_used (false); 3398 } 3399 else 3400 { 3401 bool expr_const_operands = true; 3402 3403 if (TREE_CODE (expr.value) == PARM_DECL 3404 && C_ARRAY_PARAMETER (expr.value)) 3405 { 3406 auto_diagnostic_group d; 3407 if (warning_at (loc, OPT_Wsizeof_array_argument, 3408 "%<sizeof%> on array function parameter %qE will " 3409 "return size of %qT", expr.value, 3410 TREE_TYPE (expr.value))) 3411 inform (DECL_SOURCE_LOCATION (expr.value), "declared here"); 3412 } 3413 tree folded_expr = c_fully_fold (expr.value, require_constant_value, 3414 &expr_const_operands); 3415 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr)); 3416 c_last_sizeof_arg = expr.value; 3417 c_last_sizeof_loc = loc; 3418 ret.original_code = SIZEOF_EXPR; 3419 ret.original_type = NULL; 3420 ret.m_decimal = 0; 3421 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr))) 3422 { 3423 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */ 3424 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), 3425 folded_expr, ret.value); 3426 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands; 3427 SET_EXPR_LOCATION (ret.value, loc); 3428 } 3429 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr))); 3430 } 3431 return ret; 3432 } 3433 3434 /* Return the result of sizeof applied to T, a structure for the type 3435 name passed to sizeof (rather than the type itself). LOC is the 3436 location of the original expression. */ 3437 3438 struct c_expr 3439 c_expr_sizeof_type (location_t loc, struct c_type_name *t) 3440 { 3441 tree type; 3442 struct c_expr ret; 3443 tree type_expr = NULL_TREE; 3444 bool type_expr_const = true; 3445 type = groktypename (t, &type_expr, &type_expr_const); 3446 ret.value = c_sizeof (loc, type); 3447 c_last_sizeof_arg = type; 3448 c_last_sizeof_loc = loc; 3449 ret.original_code = SIZEOF_EXPR; 3450 ret.original_type = NULL; 3451 ret.m_decimal = 0; 3452 if (type == error_mark_node) 3453 { 3454 ret.value = error_mark_node; 3455 ret.original_code = ERROR_MARK; 3456 } 3457 else 3458 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST) 3459 && C_TYPE_VARIABLE_SIZE (type)) 3460 { 3461 /* If the type is a [*] array, it is a VLA but is represented as 3462 having a size of zero. In such a case we must ensure that 3463 the result of sizeof does not get folded to a constant by 3464 c_fully_fold, because if the size is evaluated the result is 3465 not constant and so constraints on zero or negative size 3466 arrays must not be applied when this sizeof call is inside 3467 another array declarator. */ 3468 if (!type_expr) 3469 type_expr = integer_zero_node; 3470 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), 3471 type_expr, ret.value); 3472 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const; 3473 } 3474 pop_maybe_used (type != error_mark_node 3475 ? C_TYPE_VARIABLE_SIZE (type) : false); 3476 return ret; 3477 } 3478 3479 /* Build a function call to function FUNCTION with parameters PARAMS. 3480 The function call is at LOC. 3481 PARAMS is a list--a chain of TREE_LIST nodes--in which the 3482 TREE_VALUE of each node is a parameter-expression. 3483 FUNCTION's data type may be a function type or a pointer-to-function. */ 3484 3485 tree 3486 build_function_call (location_t loc, tree function, tree params) 3487 { 3488 vec<tree, va_gc> *v; 3489 tree ret; 3490 3491 vec_alloc (v, list_length (params)); 3492 for (; params; params = TREE_CHAIN (params)) 3493 v->quick_push (TREE_VALUE (params)); 3494 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL); 3495 vec_free (v); 3496 return ret; 3497 } 3498 3499 /* Give a note about the location of the declaration of DECL. */ 3500 3501 static void 3502 inform_declaration (tree decl) 3503 { 3504 if (decl && (TREE_CODE (decl) != FUNCTION_DECL 3505 || !DECL_IS_UNDECLARED_BUILTIN (decl))) 3506 inform (DECL_SOURCE_LOCATION (decl), "declared here"); 3507 } 3508 3509 /* Build a function call to function FUNCTION with parameters PARAMS. 3510 If FUNCTION is the result of resolving an overloaded target built-in, 3511 ORIG_FUNDECL is the original function decl, otherwise it is null. 3512 ORIGTYPES, if not NULL, is a vector of types; each element is 3513 either NULL or the original type of the corresponding element in 3514 PARAMS. The original type may differ from TREE_TYPE of the 3515 parameter for enums. FUNCTION's data type may be a function type 3516 or pointer-to-function. This function changes the elements of 3517 PARAMS. */ 3518 3519 tree 3520 build_function_call_vec (location_t loc, vec<location_t> arg_loc, 3521 tree function, vec<tree, va_gc> *params, 3522 vec<tree, va_gc> *origtypes, tree orig_fundecl) 3523 { 3524 tree fntype, fundecl = NULL_TREE; 3525 tree name = NULL_TREE, result; 3526 tree tem; 3527 int nargs; 3528 tree *argarray; 3529 3530 3531 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ 3532 STRIP_TYPE_NOPS (function); 3533 3534 /* Convert anything with function type to a pointer-to-function. */ 3535 if (TREE_CODE (function) == FUNCTION_DECL) 3536 { 3537 name = DECL_NAME (function); 3538 3539 if (flag_tm) 3540 tm_malloc_replacement (function); 3541 fundecl = function; 3542 if (!orig_fundecl) 3543 orig_fundecl = fundecl; 3544 /* Atomic functions have type checking/casting already done. They are 3545 often rewritten and don't match the original parameter list. */ 3546 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_")) 3547 origtypes = NULL; 3548 } 3549 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE) 3550 function = function_to_pointer_conversion (loc, function); 3551 3552 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF 3553 expressions, like those used for ObjC messenger dispatches. */ 3554 if (params && !params->is_empty ()) 3555 function = objc_rewrite_function_call (function, (*params)[0]); 3556 3557 function = c_fully_fold (function, false, NULL); 3558 3559 fntype = TREE_TYPE (function); 3560 3561 if (TREE_CODE (fntype) == ERROR_MARK) 3562 return error_mark_node; 3563 3564 if (!(TREE_CODE (fntype) == POINTER_TYPE 3565 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) 3566 { 3567 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function)) 3568 error_at (loc, 3569 "called object %qE is not a function or function pointer", 3570 function); 3571 else if (DECL_P (function)) 3572 { 3573 error_at (loc, 3574 "called object %qD is not a function or function pointer", 3575 function); 3576 inform_declaration (function); 3577 } 3578 else 3579 error_at (loc, 3580 "called object is not a function or function pointer"); 3581 return error_mark_node; 3582 } 3583 3584 if (fundecl && TREE_THIS_VOLATILE (fundecl)) 3585 current_function_returns_abnormally = 1; 3586 3587 /* fntype now gets the type of function pointed to. */ 3588 fntype = TREE_TYPE (fntype); 3589 tree return_type = TREE_TYPE (fntype); 3590 3591 /* Convert the parameters to the types declared in the 3592 function prototype, or apply default promotions. */ 3593 3594 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params, 3595 origtypes, function, fundecl); 3596 if (nargs < 0) 3597 return error_mark_node; 3598 3599 /* Check that the function is called through a compatible prototype. 3600 If it is not, warn. */ 3601 if (CONVERT_EXPR_P (function) 3602 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR 3603 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL 3604 && !comptypes (fntype, TREE_TYPE (tem))) 3605 { 3606 /* This situation leads to run-time undefined behavior. We can't, 3607 therefore, simply error unless we can prove that all possible 3608 executions of the program must execute the code. */ 3609 warning_at (loc, 0, "function called through a non-compatible type"); 3610 3611 if (VOID_TYPE_P (return_type) 3612 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED) 3613 pedwarn (loc, 0, 3614 "function with qualified void return type called"); 3615 } 3616 3617 argarray = vec_safe_address (params); 3618 3619 /* Check that arguments to builtin functions match the expectations. */ 3620 if (fundecl 3621 && fndecl_built_in_p (fundecl) 3622 && !check_builtin_function_arguments (loc, arg_loc, fundecl, 3623 orig_fundecl, nargs, argarray)) 3624 return error_mark_node; 3625 3626 /* Check that the arguments to the function are valid. */ 3627 bool warned_p = check_function_arguments (loc, fundecl, fntype, 3628 nargs, argarray, &arg_loc); 3629 3630 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED 3631 && !VOID_TYPE_P (return_type)) 3632 return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED); 3633 if (name != NULL_TREE 3634 && startswith (IDENTIFIER_POINTER (name), "__builtin_")) 3635 { 3636 if (require_constant_value) 3637 result 3638 = fold_build_call_array_initializer_loc (loc, return_type, 3639 function, nargs, argarray); 3640 else 3641 result = fold_build_call_array_loc (loc, return_type, 3642 function, nargs, argarray); 3643 if (TREE_CODE (result) == NOP_EXPR 3644 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST) 3645 STRIP_TYPE_NOPS (result); 3646 } 3647 else 3648 result = build_call_array_loc (loc, return_type, 3649 function, nargs, argarray); 3650 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again 3651 later. */ 3652 if (warned_p && TREE_CODE (result) == CALL_EXPR) 3653 suppress_warning (result, OPT_Wnonnull); 3654 3655 /* In this improbable scenario, a nested function returns a VM type. 3656 Create a TARGET_EXPR so that the call always has a LHS, much as 3657 what the C++ FE does for functions returning non-PODs. */ 3658 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (fntype))) 3659 { 3660 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype)); 3661 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result, 3662 NULL_TREE, NULL_TREE); 3663 } 3664 3665 if (VOID_TYPE_P (TREE_TYPE (result))) 3666 { 3667 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED) 3668 pedwarn (loc, 0, 3669 "function with qualified void return type called"); 3670 return result; 3671 } 3672 return require_complete_type (loc, result); 3673 } 3674 3675 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */ 3676 3677 tree 3678 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc, 3679 tree function, vec<tree, va_gc> *params, 3680 vec<tree, va_gc> *origtypes) 3681 { 3682 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ 3683 STRIP_TYPE_NOPS (function); 3684 3685 /* Convert anything with function type to a pointer-to-function. */ 3686 if (TREE_CODE (function) == FUNCTION_DECL) 3687 { 3688 /* Implement type-directed function overloading for builtins. 3689 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin 3690 handle all the type checking. The result is a complete expression 3691 that implements this function call. */ 3692 tree tem = resolve_overloaded_builtin (loc, function, params); 3693 if (tem) 3694 return tem; 3695 } 3696 return build_function_call_vec (loc, arg_loc, function, params, origtypes); 3697 } 3698 3699 /* Helper for convert_arguments called to convert the VALue of argument 3701 number ARGNUM from ORIGTYPE to the corresponding parameter number 3702 PARMNUM and TYPE. 3703 PLOC is the location where the conversion is being performed. 3704 FUNCTION and FUNDECL are the same as in convert_arguments. 3705 VALTYPE is the original type of VAL before the conversion and, 3706 for EXCESS_PRECISION_EXPR, the operand of the expression. 3707 NPC is true if VAL represents the null pointer constant (VAL itself 3708 will have been folded to an integer constant). 3709 RNAME is the same as FUNCTION except in Objective C when it's 3710 the function selector. 3711 EXCESS_PRECISION is true when VAL was originally represented 3712 as EXCESS_PRECISION_EXPR. 3713 WARNOPT is the same as in convert_for_assignment. */ 3714 3715 static tree 3716 convert_argument (location_t ploc, tree function, tree fundecl, 3717 tree type, tree origtype, tree val, tree valtype, 3718 bool npc, tree rname, int parmnum, int argnum, 3719 bool excess_precision, int warnopt) 3720 { 3721 /* Formal parm type is specified by a function prototype. */ 3722 3723 if (type == error_mark_node || !COMPLETE_TYPE_P (type)) 3724 { 3725 error_at (ploc, "type of formal parameter %d is incomplete", 3726 parmnum + 1); 3727 return error_mark_node; 3728 } 3729 3730 /* Optionally warn about conversions that differ from the default 3731 conversions. */ 3732 if (warn_traditional_conversion || warn_traditional) 3733 { 3734 if (INTEGRAL_TYPE_P (type) 3735 && SCALAR_FLOAT_TYPE_P (valtype)) 3736 warning_at (ploc, OPT_Wtraditional_conversion, 3737 "passing argument %d of %qE as integer rather " 3738 "than floating due to prototype", 3739 argnum, rname); 3740 if (INTEGRAL_TYPE_P (type) 3741 && TREE_CODE (valtype) == COMPLEX_TYPE) 3742 warning_at (ploc, OPT_Wtraditional_conversion, 3743 "passing argument %d of %qE as integer rather " 3744 "than complex due to prototype", 3745 argnum, rname); 3746 else if (TREE_CODE (type) == COMPLEX_TYPE 3747 && SCALAR_FLOAT_TYPE_P (valtype)) 3748 warning_at (ploc, OPT_Wtraditional_conversion, 3749 "passing argument %d of %qE as complex rather " 3750 "than floating due to prototype", 3751 argnum, rname); 3752 else if (SCALAR_FLOAT_TYPE_P (type) 3753 && INTEGRAL_TYPE_P (valtype)) 3754 warning_at (ploc, OPT_Wtraditional_conversion, 3755 "passing argument %d of %qE as floating rather " 3756 "than integer due to prototype", 3757 argnum, rname); 3758 else if (TREE_CODE (type) == COMPLEX_TYPE 3759 && INTEGRAL_TYPE_P (valtype)) 3760 warning_at (ploc, OPT_Wtraditional_conversion, 3761 "passing argument %d of %qE as complex rather " 3762 "than integer due to prototype", 3763 argnum, rname); 3764 else if (SCALAR_FLOAT_TYPE_P (type) 3765 && TREE_CODE (valtype) == COMPLEX_TYPE) 3766 warning_at (ploc, OPT_Wtraditional_conversion, 3767 "passing argument %d of %qE as floating rather " 3768 "than complex due to prototype", 3769 argnum, rname); 3770 /* ??? At some point, messages should be written about 3771 conversions between complex types, but that's too messy 3772 to do now. */ 3773 else if (SCALAR_FLOAT_TYPE_P (type) 3774 && SCALAR_FLOAT_TYPE_P (valtype)) 3775 { 3776 unsigned int formal_prec = TYPE_PRECISION (type); 3777 3778 /* Warn if any argument is passed as `float', 3779 since without a prototype it would be `double'. */ 3780 if (formal_prec == TYPE_PRECISION (float_type_node) 3781 && type != dfloat32_type_node) 3782 warning_at (ploc, 0, 3783 "passing argument %d of %qE as %<float%> " 3784 "rather than %<double%> due to prototype", 3785 argnum, rname); 3786 3787 /* Warn if mismatch between argument and prototype 3788 for decimal float types. Warn of conversions with 3789 binary float types and of precision narrowing due to 3790 prototype. */ 3791 else if (type != valtype 3792 && (type == dfloat32_type_node 3793 || type == dfloat64_type_node 3794 || type == dfloat128_type_node 3795 || valtype == dfloat32_type_node 3796 || valtype == dfloat64_type_node 3797 || valtype == dfloat128_type_node) 3798 && (formal_prec 3799 <= TYPE_PRECISION (valtype) 3800 || (type == dfloat128_type_node 3801 && (valtype 3802 != dfloat64_type_node 3803 && (valtype 3804 != dfloat32_type_node))) 3805 || (type == dfloat64_type_node 3806 && (valtype 3807 != dfloat32_type_node)))) 3808 warning_at (ploc, 0, 3809 "passing argument %d of %qE as %qT " 3810 "rather than %qT due to prototype", 3811 argnum, rname, type, valtype); 3812 3813 } 3814 /* Detect integer changing in width or signedness. 3815 These warnings are only activated with 3816 -Wtraditional-conversion, not with -Wtraditional. */ 3817 else if (warn_traditional_conversion 3818 && INTEGRAL_TYPE_P (type) 3819 && INTEGRAL_TYPE_P (valtype)) 3820 { 3821 unsigned int formal_prec = TYPE_PRECISION (type); 3822 tree would_have_been = default_conversion (val); 3823 tree type1 = TREE_TYPE (would_have_been); 3824 3825 if (val == error_mark_node) 3826 /* VAL could have been of incomplete type. */; 3827 else if (TREE_CODE (type) == ENUMERAL_TYPE 3828 && (TYPE_MAIN_VARIANT (type) 3829 == TYPE_MAIN_VARIANT (valtype))) 3830 /* No warning if function asks for enum 3831 and the actual arg is that enum type. */ 3832 ; 3833 else if (formal_prec != TYPE_PRECISION (type1)) 3834 warning_at (ploc, OPT_Wtraditional_conversion, 3835 "passing argument %d of %qE " 3836 "with different width due to prototype", 3837 argnum, rname); 3838 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1)) 3839 ; 3840 /* Don't complain if the formal parameter type 3841 is an enum, because we can't tell now whether 3842 the value was an enum--even the same enum. */ 3843 else if (TREE_CODE (type) == ENUMERAL_TYPE) 3844 ; 3845 else if (TREE_CODE (val) == INTEGER_CST 3846 && int_fits_type_p (val, type)) 3847 /* Change in signedness doesn't matter 3848 if a constant value is unaffected. */ 3849 ; 3850 /* If the value is extended from a narrower 3851 unsigned type, it doesn't matter whether we 3852 pass it as signed or unsigned; the value 3853 certainly is the same either way. */ 3854 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type) 3855 && TYPE_UNSIGNED (valtype)) 3856 ; 3857 else if (TYPE_UNSIGNED (type)) 3858 warning_at (ploc, OPT_Wtraditional_conversion, 3859 "passing argument %d of %qE " 3860 "as unsigned due to prototype", 3861 argnum, rname); 3862 else 3863 warning_at (ploc, OPT_Wtraditional_conversion, 3864 "passing argument %d of %qE " 3865 "as signed due to prototype", 3866 argnum, rname); 3867 } 3868 } 3869 3870 /* Possibly restore an EXCESS_PRECISION_EXPR for the 3871 sake of better warnings from convert_and_check. */ 3872 if (excess_precision) 3873 val = build1 (EXCESS_PRECISION_EXPR, valtype, val); 3874 3875 tree parmval = convert_for_assignment (ploc, ploc, type, 3876 val, origtype, ic_argpass, 3877 npc, fundecl, function, 3878 parmnum + 1, warnopt); 3879 3880 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0) 3881 && INTEGRAL_TYPE_P (type) 3882 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) 3883 parmval = default_conversion (parmval); 3884 3885 return parmval; 3886 } 3887 3888 /* Convert the argument expressions in the vector VALUES 3889 to the types in the list TYPELIST. 3890 3891 If TYPELIST is exhausted, or when an element has NULL as its type, 3892 perform the default conversions. 3893 3894 ORIGTYPES is the original types of the expressions in VALUES. This 3895 holds the type of enum values which have been converted to integral 3896 types. It may be NULL. 3897 3898 FUNCTION is a tree for the called function. It is used only for 3899 error messages, where it is formatted with %qE. 3900 3901 This is also where warnings about wrong number of args are generated. 3902 3903 ARG_LOC are locations of function arguments (if any). 3904 3905 Returns the actual number of arguments processed (which may be less 3906 than the length of VALUES in some error situations), or -1 on 3907 failure. */ 3908 3909 static int 3910 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist, 3911 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes, 3912 tree function, tree fundecl) 3913 { 3914 unsigned int parmnum; 3915 bool error_args = false; 3916 const bool type_generic = fundecl 3917 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl))); 3918 bool type_generic_remove_excess_precision = false; 3919 bool type_generic_overflow_p = false; 3920 bool type_generic_bit_query = false; 3921 tree selector; 3922 3923 /* Change pointer to function to the function itself for 3924 diagnostics. */ 3925 if (TREE_CODE (function) == ADDR_EXPR 3926 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) 3927 function = TREE_OPERAND (function, 0); 3928 3929 /* Handle an ObjC selector specially for diagnostics. */ 3930 selector = objc_message_selector (); 3931 3932 /* For a call to a built-in function declared without a prototype, 3933 set to the built-in function's argument list. */ 3934 tree builtin_typelist = NULL_TREE; 3935 3936 /* For type-generic built-in functions, determine whether excess 3937 precision should be removed (classification) or not 3938 (comparison). */ 3939 if (fundecl 3940 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL)) 3941 { 3942 built_in_function code = DECL_FUNCTION_CODE (fundecl); 3943 if (C_DECL_BUILTIN_PROTOTYPE (fundecl)) 3944 { 3945 /* For a call to a built-in function declared without a prototype 3946 use the types of the parameters of the internal built-in to 3947 match those of the arguments to. */ 3948 if (tree bdecl = builtin_decl_explicit (code)) 3949 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl)); 3950 } 3951 3952 /* For type-generic built-in functions, determine whether excess 3953 precision should be removed (classification) or not 3954 (comparison). */ 3955 if (type_generic) 3956 switch (code) 3957 { 3958 case BUILT_IN_ISFINITE: 3959 case BUILT_IN_ISINF: 3960 case BUILT_IN_ISINF_SIGN: 3961 case BUILT_IN_ISNAN: 3962 case BUILT_IN_ISNORMAL: 3963 case BUILT_IN_ISSIGNALING: 3964 case BUILT_IN_FPCLASSIFY: 3965 type_generic_remove_excess_precision = true; 3966 break; 3967 3968 case BUILT_IN_ADD_OVERFLOW_P: 3969 case BUILT_IN_SUB_OVERFLOW_P: 3970 case BUILT_IN_MUL_OVERFLOW_P: 3971 /* The last argument of these type-generic builtins 3972 should not be promoted. */ 3973 type_generic_overflow_p = true; 3974 break; 3975 3976 case BUILT_IN_CLZG: 3977 case BUILT_IN_CTZG: 3978 case BUILT_IN_CLRSBG: 3979 case BUILT_IN_FFSG: 3980 case BUILT_IN_PARITYG: 3981 case BUILT_IN_POPCOUNTG: 3982 /* The first argument of these type-generic builtins 3983 should not be promoted. */ 3984 type_generic_bit_query = true; 3985 break; 3986 3987 default: 3988 break; 3989 } 3990 } 3991 3992 /* Scan the given expressions (VALUES) and types (TYPELIST), producing 3993 individual converted arguments. */ 3994 3995 tree typetail, builtin_typetail, val; 3996 for (typetail = typelist, 3997 builtin_typetail = builtin_typelist, 3998 parmnum = 0; 3999 values && values->iterate (parmnum, &val); 4000 ++parmnum) 4001 { 4002 /* The type of the function parameter (if it was declared with one). */ 4003 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE; 4004 /* The type of the built-in function parameter (if the function 4005 is a built-in). Used to detect type incompatibilities in 4006 calls to built-ins declared without a prototype. */ 4007 tree builtin_type = (builtin_typetail 4008 ? TREE_VALUE (builtin_typetail) : NULL_TREE); 4009 /* The original type of the argument being passed to the function. */ 4010 tree valtype = TREE_TYPE (val); 4011 /* The called function (or function selector in Objective C). */ 4012 tree rname = function; 4013 int argnum = parmnum + 1; 4014 const char *invalid_func_diag; 4015 /* Set for EXCESS_PRECISION_EXPR arguments. */ 4016 bool excess_precision = false; 4017 /* The value of the argument after conversion to the type 4018 of the function parameter it is passed to. */ 4019 tree parmval; 4020 /* Some __atomic_* builtins have additional hidden argument at 4021 position 0. */ 4022 location_t ploc 4023 = !arg_loc.is_empty () && values->length () == arg_loc.length () 4024 ? expansion_point_location_if_in_system_header (arg_loc[parmnum]) 4025 : input_location; 4026 4027 if (type == void_type_node) 4028 { 4029 if (selector) 4030 error_at (loc, "too many arguments to method %qE", selector); 4031 else 4032 error_at (loc, "too many arguments to function %qE", function); 4033 inform_declaration (fundecl); 4034 return error_args ? -1 : (int) parmnum; 4035 } 4036 4037 if (builtin_type == void_type_node) 4038 { 4039 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch, 4040 "too many arguments to built-in function %qE " 4041 "expecting %d", function, parmnum)) 4042 inform_declaration (fundecl); 4043 builtin_typetail = NULL_TREE; 4044 } 4045 4046 if (selector && argnum > 2) 4047 { 4048 rname = selector; 4049 argnum -= 2; 4050 } 4051 4052 /* Determine if VAL is a null pointer constant before folding it. */ 4053 bool npc = null_pointer_constant_p (val); 4054 4055 /* If there is excess precision and a prototype, convert once to 4056 the required type rather than converting via the semantic 4057 type. Likewise without a prototype a float value represented 4058 as long double should be converted once to double. But for 4059 type-generic classification functions excess precision must 4060 be removed here. */ 4061 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR 4062 && (type || !type_generic || !type_generic_remove_excess_precision)) 4063 { 4064 val = TREE_OPERAND (val, 0); 4065 excess_precision = true; 4066 } 4067 val = c_fully_fold (val, false, NULL); 4068 STRIP_TYPE_NOPS (val); 4069 4070 val = require_complete_type (ploc, val); 4071 4072 /* Some floating-point arguments must be promoted to double when 4073 no type is specified by a prototype. This applies to 4074 arguments of type float, and to architecture-specific types 4075 (ARM __fp16), but not to _FloatN or _FloatNx types. */ 4076 bool promote_float_arg = false; 4077 if (type == NULL_TREE 4078 && TREE_CODE (valtype) == REAL_TYPE 4079 && (TYPE_PRECISION (valtype) 4080 <= TYPE_PRECISION (double_type_node)) 4081 && TYPE_MAIN_VARIANT (valtype) != double_type_node 4082 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node 4083 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype))) 4084 { 4085 /* Promote this argument, unless it has a _FloatN or 4086 _FloatNx type. */ 4087 promote_float_arg = true; 4088 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++) 4089 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i)) 4090 { 4091 promote_float_arg = false; 4092 break; 4093 } 4094 /* Don't promote __bf16 either. */ 4095 if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node) 4096 promote_float_arg = false; 4097 } 4098 4099 if (type != NULL_TREE) 4100 { 4101 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum]; 4102 parmval = convert_argument (ploc, function, fundecl, type, origtype, 4103 val, valtype, npc, rname, parmnum, argnum, 4104 excess_precision, 0); 4105 } 4106 else if (promote_float_arg) 4107 { 4108 if (type_generic) 4109 parmval = val; 4110 else 4111 { 4112 /* Convert `float' to `double'. */ 4113 if (warn_double_promotion && !c_inhibit_evaluation_warnings) 4114 warning_at (ploc, OPT_Wdouble_promotion, 4115 "implicit conversion from %qT to %qT when passing " 4116 "argument to function", 4117 valtype, double_type_node); 4118 parmval = convert (double_type_node, val); 4119 } 4120 } 4121 else if ((excess_precision && !type_generic) 4122 || (type_generic_overflow_p && parmnum == 2) 4123 || (type_generic_bit_query && parmnum == 0)) 4124 /* A "double" argument with excess precision being passed 4125 without a prototype or in variable arguments. 4126 The last argument of __builtin_*_overflow_p should not be 4127 promoted, similarly the first argument of 4128 __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */ 4129 parmval = convert (valtype, val); 4130 else if ((invalid_func_diag = 4131 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val))) 4132 { 4133 error (invalid_func_diag); 4134 return -1; 4135 } 4136 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val)) 4137 { 4138 return -1; 4139 } 4140 else 4141 /* Convert `short' and `char' to full-size `int'. */ 4142 parmval = default_conversion (val); 4143 4144 (*values)[parmnum] = parmval; 4145 if (parmval == error_mark_node) 4146 error_args = true; 4147 4148 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE) 4149 { 4150 /* For a call to a built-in function declared without a prototype, 4151 perform the conversions from the argument to the expected type 4152 but issue warnings rather than errors for any mismatches. 4153 Ignore the converted argument and use the PARMVAL obtained 4154 above by applying default conversions instead. */ 4155 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum]; 4156 convert_argument (ploc, function, fundecl, builtin_type, origtype, 4157 val, valtype, npc, rname, parmnum, argnum, 4158 excess_precision, 4159 OPT_Wbuiltin_declaration_mismatch); 4160 } 4161 4162 if (typetail) 4163 typetail = TREE_CHAIN (typetail); 4164 4165 if (builtin_typetail) 4166 builtin_typetail = TREE_CHAIN (builtin_typetail); 4167 } 4168 4169 gcc_assert (parmnum == vec_safe_length (values)); 4170 4171 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node) 4172 { 4173 error_at (loc, "too few arguments to function %qE", function); 4174 inform_declaration (fundecl); 4175 return -1; 4176 } 4177 4178 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node) 4179 { 4180 unsigned nargs = parmnum; 4181 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t)) 4182 ++nargs; 4183 4184 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch, 4185 "too few arguments to built-in function %qE " 4186 "expecting %u", function, nargs - 1)) 4187 inform_declaration (fundecl); 4188 } 4189 4190 return error_args ? -1 : (int) parmnum; 4191 } 4192 4193 /* This is the entry point used by the parser to build unary operators 4195 in the input. CODE, a tree_code, specifies the unary operator, and 4196 ARG is the operand. For unary plus, the C parser currently uses 4197 CONVERT_EXPR for code. 4198 4199 LOC is the location to use for the tree generated. 4200 */ 4201 4202 struct c_expr 4203 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg) 4204 { 4205 struct c_expr result; 4206 4207 result.original_code = code; 4208 result.original_type = NULL; 4209 result.m_decimal = 0; 4210 4211 if (reject_gcc_builtin (arg.value)) 4212 { 4213 result.value = error_mark_node; 4214 } 4215 else 4216 { 4217 result.value = build_unary_op (loc, code, arg.value, false); 4218 4219 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value)) 4220 overflow_warning (loc, result.value, arg.value); 4221 } 4222 4223 /* We are typically called when parsing a prefix token at LOC acting on 4224 ARG. Reflect this by updating the source range of the result to 4225 start at LOC and end at the end of ARG. */ 4226 set_c_expr_source_range (&result, 4227 loc, arg.get_finish ()); 4228 4229 return result; 4230 } 4231 4232 /* Returns true if TYPE is a character type, *not* including wchar_t. */ 4233 4234 bool 4235 char_type_p (tree type) 4236 { 4237 return (type == char_type_node 4238 || type == unsigned_char_type_node 4239 || type == signed_char_type_node 4240 || type == char16_type_node 4241 || type == char32_type_node); 4242 } 4243 4244 /* This is the entry point used by the parser to build binary operators 4245 in the input. CODE, a tree_code, specifies the binary operator, and 4246 ARG1 and ARG2 are the operands. In addition to constructing the 4247 expression, we check for operands that were written with other binary 4248 operators in a way that is likely to confuse the user. 4249 4250 LOCATION is the location of the binary operator. */ 4251 4252 struct c_expr 4253 parser_build_binary_op (location_t location, enum tree_code code, 4254 struct c_expr arg1, struct c_expr arg2) 4255 { 4256 struct c_expr result; 4257 result.m_decimal = 0; 4258 4259 enum tree_code code1 = arg1.original_code; 4260 enum tree_code code2 = arg2.original_code; 4261 tree type1 = (arg1.original_type 4262 ? arg1.original_type 4263 : TREE_TYPE (arg1.value)); 4264 tree type2 = (arg2.original_type 4265 ? arg2.original_type 4266 : TREE_TYPE (arg2.value)); 4267 4268 result.value = build_binary_op (location, code, 4269 arg1.value, arg2.value, true); 4270 result.original_code = code; 4271 result.original_type = NULL; 4272 result.m_decimal = 0; 4273 4274 if (TREE_CODE (result.value) == ERROR_MARK) 4275 { 4276 set_c_expr_source_range (&result, 4277 arg1.get_start (), 4278 arg2.get_finish ()); 4279 return result; 4280 } 4281 4282 if (location != UNKNOWN_LOCATION) 4283 protected_set_expr_location (result.value, location); 4284 4285 set_c_expr_source_range (&result, 4286 arg1.get_start (), 4287 arg2.get_finish ()); 4288 4289 /* Check for cases such as x+y<<z which users are likely 4290 to misinterpret. */ 4291 if (warn_parentheses) 4292 warn_about_parentheses (location, code, code1, arg1.value, code2, 4293 arg2.value); 4294 4295 if (warn_logical_op) 4296 warn_logical_operator (location, code, TREE_TYPE (result.value), 4297 code1, arg1.value, code2, arg2.value); 4298 4299 if (warn_tautological_compare) 4300 { 4301 tree lhs = arg1.value; 4302 tree rhs = arg2.value; 4303 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) 4304 { 4305 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE 4306 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs))) 4307 lhs = NULL_TREE; 4308 else 4309 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs); 4310 } 4311 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR) 4312 { 4313 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE 4314 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs))) 4315 rhs = NULL_TREE; 4316 else 4317 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs); 4318 } 4319 if (lhs != NULL_TREE && rhs != NULL_TREE) 4320 warn_tautological_cmp (location, code, lhs, rhs); 4321 } 4322 4323 if (warn_logical_not_paren 4324 && TREE_CODE_CLASS (code) == tcc_comparison 4325 && code1 == TRUTH_NOT_EXPR 4326 && code2 != TRUTH_NOT_EXPR 4327 /* Avoid warning for !!x == y. */ 4328 && (TREE_CODE (arg1.value) != NE_EXPR 4329 || !integer_zerop (TREE_OPERAND (arg1.value, 1)))) 4330 { 4331 /* Avoid warning for !b == y where b has _Bool type. */ 4332 tree t = integer_zero_node; 4333 if (TREE_CODE (arg1.value) == EQ_EXPR 4334 && integer_zerop (TREE_OPERAND (arg1.value, 1)) 4335 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node) 4336 { 4337 t = TREE_OPERAND (arg1.value, 0); 4338 do 4339 { 4340 if (TREE_TYPE (t) != integer_type_node) 4341 break; 4342 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR) 4343 t = C_MAYBE_CONST_EXPR_EXPR (t); 4344 else if (CONVERT_EXPR_P (t)) 4345 t = TREE_OPERAND (t, 0); 4346 else 4347 break; 4348 } 4349 while (1); 4350 } 4351 if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t))) 4352 warn_logical_not_parentheses (location, code, arg1.value, arg2.value); 4353 } 4354 4355 /* Warn about comparisons against string literals, with the exception 4356 of testing for equality or inequality of a string literal with NULL. */ 4357 if (code == EQ_EXPR || code == NE_EXPR) 4358 { 4359 if ((code1 == STRING_CST 4360 && !integer_zerop (tree_strip_nop_conversions (arg2.value))) 4361 || (code2 == STRING_CST 4362 && !integer_zerop (tree_strip_nop_conversions (arg1.value)))) 4363 warning_at (location, OPT_Waddress, 4364 "comparison with string literal results in unspecified behavior"); 4365 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */ 4366 if (POINTER_TYPE_P (type1) 4367 && null_pointer_constant_p (arg2.value) 4368 && char_type_p (type2)) 4369 { 4370 auto_diagnostic_group d; 4371 if (warning_at (location, OPT_Wpointer_compare, 4372 "comparison between pointer and zero character " 4373 "constant")) 4374 inform (arg1.get_start (), 4375 "did you mean to dereference the pointer?"); 4376 } 4377 else if (POINTER_TYPE_P (type2) 4378 && null_pointer_constant_p (arg1.value) 4379 && char_type_p (type1)) 4380 { 4381 auto_diagnostic_group d; 4382 if (warning_at (location, OPT_Wpointer_compare, 4383 "comparison between pointer and zero character " 4384 "constant")) 4385 inform (arg2.get_start (), 4386 "did you mean to dereference the pointer?"); 4387 } 4388 } 4389 else if (TREE_CODE_CLASS (code) == tcc_comparison 4390 && (code1 == STRING_CST || code2 == STRING_CST)) 4391 warning_at (location, OPT_Waddress, 4392 "comparison with string literal results in unspecified " 4393 "behavior"); 4394 4395 if (warn_array_compare 4396 && TREE_CODE_CLASS (code) == tcc_comparison 4397 && TREE_CODE (type1) == ARRAY_TYPE 4398 && TREE_CODE (type2) == ARRAY_TYPE) 4399 do_warn_array_compare (location, code, arg1.value, arg2.value); 4400 4401 if (TREE_OVERFLOW_P (result.value) 4402 && !TREE_OVERFLOW_P (arg1.value) 4403 && !TREE_OVERFLOW_P (arg2.value)) 4404 overflow_warning (location, result.value); 4405 4406 /* Warn about comparisons of different enum types. */ 4407 if (warn_enum_compare 4408 && TREE_CODE_CLASS (code) == tcc_comparison 4409 && TREE_CODE (type1) == ENUMERAL_TYPE 4410 && TREE_CODE (type2) == ENUMERAL_TYPE 4411 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2)) 4412 warning_at (location, OPT_Wenum_compare, 4413 "comparison between %qT and %qT", 4414 type1, type2); 4415 4416 if (warn_xor_used_as_pow 4417 && code == BIT_XOR_EXPR 4418 && arg1.m_decimal 4419 && arg2.m_decimal) 4420 check_for_xor_used_as_pow (arg1.get_location (), arg1.value, 4421 location, 4422 arg2.get_location (), arg2.value); 4423 4424 return result; 4425 } 4426 4427 /* Return a tree for the difference of pointers OP0 and OP1. 4429 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is 4430 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */ 4431 4432 static tree 4433 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr) 4434 { 4435 tree restype = ptrdiff_type_node; 4436 tree result, inttype; 4437 4438 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0))); 4439 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1))); 4440 tree target_type = TREE_TYPE (TREE_TYPE (op0)); 4441 tree orig_op0 = op0; 4442 tree orig_op1 = op1; 4443 4444 /* If the operands point into different address spaces, we need to 4445 explicitly convert them to pointers into the common address space 4446 before we can subtract the numerical address values. */ 4447 if (as0 != as1) 4448 { 4449 addr_space_t as_common; 4450 tree common_type; 4451 4452 /* Determine the common superset address space. This is guaranteed 4453 to exist because the caller verified that comp_target_types 4454 returned non-zero. */ 4455 if (!addr_space_superset (as0, as1, &as_common)) 4456 gcc_unreachable (); 4457 4458 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1)); 4459 op0 = convert (common_type, op0); 4460 op1 = convert (common_type, op1); 4461 } 4462 4463 /* Determine integer type result of the subtraction. This will usually 4464 be the same as the result type (ptrdiff_t), but may need to be a wider 4465 type if pointers for the address space are wider than ptrdiff_t. */ 4466 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0))) 4467 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0); 4468 else 4469 inttype = restype; 4470 4471 if (VOID_TYPE_P (target_type)) 4472 pedwarn (loc, OPT_Wpointer_arith, 4473 "pointer of type %<void *%> used in subtraction"); 4474 if (TREE_CODE (target_type) == FUNCTION_TYPE) 4475 pedwarn (loc, OPT_Wpointer_arith, 4476 "pointer to a function used in subtraction"); 4477 4478 if (current_function_decl != NULL_TREE 4479 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT)) 4480 { 4481 op0 = save_expr (c_fully_fold (op0, false, NULL)); 4482 op1 = save_expr (c_fully_fold (op1, false, NULL)); 4483 4484 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT); 4485 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1); 4486 } 4487 4488 /* First do the subtraction, then build the divide operator 4489 and only convert at the very end. 4490 Do not do default conversions in case restype is a short type. */ 4491 4492 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as 4493 pointers. If some platform cannot provide that, or has a larger 4494 ptrdiff_type to support differences larger than half the address 4495 space, cast the pointers to some larger integer type and do the 4496 computations in that type. */ 4497 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0))) 4498 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0), 4499 convert (inttype, op1), false); 4500 else 4501 { 4502 /* Cast away qualifiers. */ 4503 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0); 4504 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1); 4505 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1); 4506 } 4507 4508 /* This generates an error if op1 is pointer to incomplete type. */ 4509 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1)))) 4510 error_at (loc, "arithmetic on pointer to an incomplete type"); 4511 else if (verify_type_context (loc, TCTX_POINTER_ARITH, 4512 TREE_TYPE (TREE_TYPE (orig_op0)))) 4513 verify_type_context (loc, TCTX_POINTER_ARITH, 4514 TREE_TYPE (TREE_TYPE (orig_op1))); 4515 4516 op1 = c_size_in_bytes (target_type); 4517 4518 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1))) 4519 error_at (loc, "arithmetic on pointer to an empty aggregate"); 4520 4521 /* Divide by the size, in easiest possible way. */ 4522 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype, 4523 op0, convert (inttype, op1)); 4524 4525 /* Convert to final result type if necessary. */ 4526 return convert (restype, result); 4527 } 4528 4529 /* Expand atomic compound assignments into an appropriate sequence as 4531 specified by the C11 standard section 6.5.16.2. 4532 4533 _Atomic T1 E1 4534 T2 E2 4535 E1 op= E2 4536 4537 This sequence is used for all types for which these operations are 4538 supported. 4539 4540 In addition, built-in versions of the 'fe' prefixed routines may 4541 need to be invoked for floating point (real, complex or vector) when 4542 floating-point exceptions are supported. See 6.5.16.2 footnote 113. 4543 4544 T1 newval; 4545 T1 old; 4546 T1 *addr 4547 T2 val 4548 fenv_t fenv 4549 4550 addr = &E1; 4551 val = (E2); 4552 __atomic_load (addr, &old, SEQ_CST); 4553 feholdexcept (&fenv); 4554 loop: 4555 newval = old op val; 4556 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST, 4557 SEQ_CST)) 4558 goto done; 4559 feclearexcept (FE_ALL_EXCEPT); 4560 goto loop: 4561 done: 4562 feupdateenv (&fenv); 4563 4564 The compiler will issue the __atomic_fetch_* built-in when possible, 4565 otherwise it will generate the generic form of the atomic operations. 4566 This requires temp(s) and has their address taken. The atomic processing 4567 is smart enough to figure out when the size of an object can utilize 4568 a lock-free version, and convert the built-in call to the appropriate 4569 lock-free routine. The optimizers will then dispose of any temps that 4570 are no longer required, and lock-free implementations are utilized as 4571 long as there is target support for the required size. 4572 4573 If the operator is NOP_EXPR, then this is a simple assignment, and 4574 an __atomic_store is issued to perform the assignment rather than 4575 the above loop. */ 4576 4577 /* Build an atomic assignment at LOC, expanding into the proper 4578 sequence to store LHS MODIFYCODE= RHS. Return a value representing 4579 the result of the operation, unless RETURN_OLD_P, in which case 4580 return the old value of LHS (this is only for postincrement and 4581 postdecrement). */ 4582 4583 static tree 4584 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode, 4585 tree rhs, bool return_old_p) 4586 { 4587 tree fndecl, func_call; 4588 vec<tree, va_gc> *params; 4589 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr; 4590 tree old, old_addr; 4591 tree compound_stmt = NULL_TREE; 4592 tree stmt, goto_stmt; 4593 tree loop_label, loop_decl, done_label, done_decl; 4594 4595 tree lhs_type = TREE_TYPE (lhs); 4596 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false); 4597 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST); 4598 tree rhs_semantic_type = TREE_TYPE (rhs); 4599 tree nonatomic_rhs_semantic_type; 4600 tree rhs_type; 4601 4602 gcc_assert (TYPE_ATOMIC (lhs_type)); 4603 4604 if (return_old_p) 4605 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR); 4606 4607 /* Allocate enough vector items for a compare_exchange. */ 4608 vec_alloc (params, 6); 4609 4610 /* Create a compound statement to hold the sequence of statements 4611 with a loop. */ 4612 if (modifycode != NOP_EXPR) 4613 { 4614 compound_stmt = c_begin_compound_stmt (false); 4615 4616 /* For consistency with build_modify_expr on non-_Atomic, 4617 mark the lhs as read. Also, it would be very hard to match 4618 such expressions in mark_exp_read. */ 4619 mark_exp_read (lhs); 4620 } 4621 4622 /* Remove any excess precision (which is only present here in the 4623 case of compound assignments). */ 4624 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) 4625 { 4626 gcc_assert (modifycode != NOP_EXPR); 4627 rhs = TREE_OPERAND (rhs, 0); 4628 } 4629 rhs_type = TREE_TYPE (rhs); 4630 4631 /* Fold the RHS if it hasn't already been folded. */ 4632 if (modifycode != NOP_EXPR) 4633 rhs = c_fully_fold (rhs, false, NULL); 4634 4635 /* Remove the qualifiers for the rest of the expressions and create 4636 the VAL temp variable to hold the RHS. */ 4637 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED); 4638 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED); 4639 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type, 4640 TYPE_UNQUALIFIED); 4641 val = create_tmp_var_raw (nonatomic_rhs_type); 4642 TREE_ADDRESSABLE (val) = 1; 4643 suppress_warning (val); 4644 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE, 4645 NULL_TREE); 4646 TREE_SIDE_EFFECTS (rhs) = 1; 4647 SET_EXPR_LOCATION (rhs, loc); 4648 if (modifycode != NOP_EXPR) 4649 add_stmt (rhs); 4650 4651 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue 4652 an atomic_store. */ 4653 if (modifycode == NOP_EXPR) 4654 { 4655 compound_stmt = rhs; 4656 /* Build __atomic_store (&lhs, &val, SEQ_CST) */ 4657 rhs = build_unary_op (loc, ADDR_EXPR, val, false); 4658 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE); 4659 params->quick_push (lhs_addr); 4660 params->quick_push (rhs); 4661 params->quick_push (seq_cst); 4662 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); 4663 4664 compound_stmt = build2 (COMPOUND_EXPR, void_type_node, 4665 compound_stmt, func_call); 4666 4667 /* VAL is the value which was stored, return a COMPOUND_STMT of 4668 the statement and that value. */ 4669 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val); 4670 } 4671 4672 /* Attempt to implement the atomic operation as an __atomic_fetch_* or 4673 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type 4674 isn't applicable for such builtins. ??? Do we want to handle enums? */ 4675 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type)) 4676 && TREE_CODE (rhs_type) == INTEGER_TYPE) 4677 { 4678 built_in_function fncode; 4679 switch (modifycode) 4680 { 4681 case PLUS_EXPR: 4682 case POINTER_PLUS_EXPR: 4683 fncode = (return_old_p 4684 ? BUILT_IN_ATOMIC_FETCH_ADD_N 4685 : BUILT_IN_ATOMIC_ADD_FETCH_N); 4686 break; 4687 case MINUS_EXPR: 4688 fncode = (return_old_p 4689 ? BUILT_IN_ATOMIC_FETCH_SUB_N 4690 : BUILT_IN_ATOMIC_SUB_FETCH_N); 4691 break; 4692 case BIT_AND_EXPR: 4693 fncode = (return_old_p 4694 ? BUILT_IN_ATOMIC_FETCH_AND_N 4695 : BUILT_IN_ATOMIC_AND_FETCH_N); 4696 break; 4697 case BIT_IOR_EXPR: 4698 fncode = (return_old_p 4699 ? BUILT_IN_ATOMIC_FETCH_OR_N 4700 : BUILT_IN_ATOMIC_OR_FETCH_N); 4701 break; 4702 case BIT_XOR_EXPR: 4703 fncode = (return_old_p 4704 ? BUILT_IN_ATOMIC_FETCH_XOR_N 4705 : BUILT_IN_ATOMIC_XOR_FETCH_N); 4706 break; 4707 default: 4708 goto cas_loop; 4709 } 4710 4711 /* We can only use "_1" through "_16" variants of the atomic fetch 4712 built-ins. */ 4713 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type)); 4714 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16) 4715 goto cas_loop; 4716 4717 /* If this is a pointer type, we need to multiply by the size of 4718 the pointer target type. */ 4719 if (POINTER_TYPE_P (lhs_type)) 4720 { 4721 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type)) 4722 /* ??? This would introduce -Wdiscarded-qualifiers 4723 warning: __atomic_fetch_* expect volatile void * 4724 type as the first argument. (Assignments between 4725 atomic and non-atomic objects are OK.) */ 4726 || TYPE_RESTRICT (lhs_type)) 4727 goto cas_loop; 4728 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type)); 4729 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node, 4730 convert (ptrdiff_type_node, rhs), 4731 convert (ptrdiff_type_node, sz)); 4732 } 4733 4734 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or 4735 __atomic_*_fetch (&lhs, &val, SEQ_CST). */ 4736 fndecl = builtin_decl_explicit (fncode); 4737 params->quick_push (lhs_addr); 4738 params->quick_push (rhs); 4739 params->quick_push (seq_cst); 4740 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); 4741 4742 newval = create_tmp_var_raw (nonatomic_lhs_type); 4743 TREE_ADDRESSABLE (newval) = 1; 4744 suppress_warning (newval); 4745 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call, 4746 NULL_TREE, NULL_TREE); 4747 SET_EXPR_LOCATION (rhs, loc); 4748 add_stmt (rhs); 4749 4750 /* Finish the compound statement. */ 4751 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false); 4752 4753 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of 4754 the statement and that value. */ 4755 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval); 4756 } 4757 4758 cas_loop: 4759 /* Create the variables and labels required for the op= form. */ 4760 old = create_tmp_var_raw (nonatomic_lhs_type); 4761 old_addr = build_unary_op (loc, ADDR_EXPR, old, false); 4762 TREE_ADDRESSABLE (old) = 1; 4763 suppress_warning (old); 4764 4765 newval = create_tmp_var_raw (nonatomic_lhs_type); 4766 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false); 4767 TREE_ADDRESSABLE (newval) = 1; 4768 suppress_warning (newval); 4769 4770 loop_decl = create_artificial_label (loc); 4771 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl); 4772 4773 done_decl = create_artificial_label (loc); 4774 done_label = build1 (LABEL_EXPR, void_type_node, done_decl); 4775 4776 /* __atomic_load (addr, &old, SEQ_CST). */ 4777 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD); 4778 params->quick_push (lhs_addr); 4779 params->quick_push (old_addr); 4780 params->quick_push (seq_cst); 4781 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); 4782 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE, 4783 NULL_TREE); 4784 add_stmt (old); 4785 params->truncate (0); 4786 4787 /* Create the expressions for floating-point environment 4788 manipulation, if required. */ 4789 bool need_fenv = (flag_trapping_math 4790 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type))); 4791 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE; 4792 if (need_fenv) 4793 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call); 4794 4795 if (hold_call) 4796 add_stmt (hold_call); 4797 4798 /* loop: */ 4799 add_stmt (loop_label); 4800 4801 /* newval = old + val; */ 4802 if (rhs_type != rhs_semantic_type) 4803 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val); 4804 rhs = build_binary_op (loc, modifycode, old, val, true); 4805 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) 4806 { 4807 tree eptype = TREE_TYPE (rhs); 4808 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL); 4809 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs); 4810 } 4811 else 4812 rhs = c_fully_fold (rhs, false, NULL); 4813 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type, 4814 rhs, NULL_TREE, ic_assign, false, NULL_TREE, 4815 NULL_TREE, 0); 4816 if (rhs != error_mark_node) 4817 { 4818 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE, 4819 NULL_TREE); 4820 SET_EXPR_LOCATION (rhs, loc); 4821 add_stmt (rhs); 4822 } 4823 4824 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST)) 4825 goto done; */ 4826 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE); 4827 params->quick_push (lhs_addr); 4828 params->quick_push (old_addr); 4829 params->quick_push (newval_addr); 4830 params->quick_push (integer_zero_node); 4831 params->quick_push (seq_cst); 4832 params->quick_push (seq_cst); 4833 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); 4834 4835 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl); 4836 SET_EXPR_LOCATION (goto_stmt, loc); 4837 4838 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE); 4839 SET_EXPR_LOCATION (stmt, loc); 4840 add_stmt (stmt); 4841 4842 if (clear_call) 4843 add_stmt (clear_call); 4844 4845 /* goto loop; */ 4846 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl); 4847 SET_EXPR_LOCATION (goto_stmt, loc); 4848 add_stmt (goto_stmt); 4849 4850 /* done: */ 4851 add_stmt (done_label); 4852 4853 if (update_call) 4854 add_stmt (update_call); 4855 4856 /* Finish the compound statement. */ 4857 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false); 4858 4859 /* NEWVAL is the value that was successfully stored, return a 4860 COMPOUND_EXPR of the statement and the appropriate value. */ 4861 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, 4862 return_old_p ? old : newval); 4863 } 4864 4865 /* Construct and perhaps optimize a tree representation 4866 for a unary operation. CODE, a tree_code, specifies the operation 4867 and XARG is the operand. 4868 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default 4869 promotions (such as from short to int). 4870 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows 4871 non-lvalues; this is only used to handle conversion of non-lvalue arrays 4872 to pointers in C99. 4873 4874 LOCATION is the location of the operator. */ 4875 4876 tree 4877 build_unary_op (location_t location, enum tree_code code, tree xarg, 4878 bool noconvert) 4879 { 4880 /* No default_conversion here. It causes trouble for ADDR_EXPR. */ 4881 tree arg = xarg; 4882 tree argtype = NULL_TREE; 4883 enum tree_code typecode; 4884 tree val; 4885 tree ret = error_mark_node; 4886 tree eptype = NULL_TREE; 4887 const char *invalid_op_diag; 4888 bool int_operands; 4889 4890 int_operands = EXPR_INT_CONST_OPERANDS (xarg); 4891 if (int_operands) 4892 arg = remove_c_maybe_const_expr (arg); 4893 4894 if (code != ADDR_EXPR) 4895 arg = require_complete_type (location, arg); 4896 4897 typecode = TREE_CODE (TREE_TYPE (arg)); 4898 if (typecode == ERROR_MARK) 4899 return error_mark_node; 4900 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE) 4901 typecode = INTEGER_TYPE; 4902 4903 if ((invalid_op_diag 4904 = targetm.invalid_unary_op (code, TREE_TYPE (xarg)))) 4905 { 4906 error_at (location, invalid_op_diag); 4907 return error_mark_node; 4908 } 4909 4910 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR) 4911 { 4912 eptype = TREE_TYPE (arg); 4913 arg = TREE_OPERAND (arg, 0); 4914 } 4915 4916 switch (code) 4917 { 4918 case CONVERT_EXPR: 4919 /* This is used for unary plus, because a CONVERT_EXPR 4920 is enough to prevent anybody from looking inside for 4921 associativity, but won't generate any code. */ 4922 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE 4923 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE 4924 || typecode == BITINT_TYPE 4925 || gnu_vector_type_p (TREE_TYPE (arg)))) 4926 { 4927 error_at (location, "wrong type argument to unary plus"); 4928 return error_mark_node; 4929 } 4930 else if (!noconvert) 4931 arg = default_conversion (arg); 4932 arg = non_lvalue_loc (location, arg); 4933 break; 4934 4935 case NEGATE_EXPR: 4936 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE 4937 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE 4938 || typecode == BITINT_TYPE 4939 || gnu_vector_type_p (TREE_TYPE (arg)))) 4940 { 4941 error_at (location, "wrong type argument to unary minus"); 4942 return error_mark_node; 4943 } 4944 else if (!noconvert) 4945 arg = default_conversion (arg); 4946 break; 4947 4948 case BIT_NOT_EXPR: 4949 /* ~ works on integer types and non float vectors. */ 4950 if (typecode == INTEGER_TYPE 4951 || typecode == BITINT_TYPE 4952 || (gnu_vector_type_p (TREE_TYPE (arg)) 4953 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg)))) 4954 { 4955 tree e = arg; 4956 4957 /* Warn if the expression has boolean value. */ 4958 while (TREE_CODE (e) == COMPOUND_EXPR) 4959 e = TREE_OPERAND (e, 1); 4960 4961 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg)) 4962 || truth_value_p (TREE_CODE (e)))) 4963 { 4964 auto_diagnostic_group d; 4965 if (warning_at (location, OPT_Wbool_operation, 4966 "%<~%> on a boolean expression")) 4967 { 4968 gcc_rich_location richloc (location); 4969 richloc.add_fixit_insert_before (location, "!"); 4970 inform (&richloc, "did you mean to use logical not?"); 4971 } 4972 } 4973 if (!noconvert) 4974 arg = default_conversion (arg); 4975 } 4976 else if (typecode == COMPLEX_TYPE) 4977 { 4978 code = CONJ_EXPR; 4979 pedwarn (location, OPT_Wpedantic, 4980 "ISO C does not support %<~%> for complex conjugation"); 4981 if (!noconvert) 4982 arg = default_conversion (arg); 4983 } 4984 else 4985 { 4986 error_at (location, "wrong type argument to bit-complement"); 4987 return error_mark_node; 4988 } 4989 break; 4990 4991 case ABS_EXPR: 4992 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE)) 4993 { 4994 error_at (location, "wrong type argument to abs"); 4995 return error_mark_node; 4996 } 4997 else if (!noconvert) 4998 arg = default_conversion (arg); 4999 break; 5000 5001 case ABSU_EXPR: 5002 if (!(typecode == INTEGER_TYPE)) 5003 { 5004 error_at (location, "wrong type argument to absu"); 5005 return error_mark_node; 5006 } 5007 else if (!noconvert) 5008 arg = default_conversion (arg); 5009 break; 5010 5011 case CONJ_EXPR: 5012 /* Conjugating a real value is a no-op, but allow it anyway. */ 5013 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE 5014 || typecode == COMPLEX_TYPE)) 5015 { 5016 error_at (location, "wrong type argument to conjugation"); 5017 return error_mark_node; 5018 } 5019 else if (!noconvert) 5020 arg = default_conversion (arg); 5021 break; 5022 5023 case TRUTH_NOT_EXPR: 5024 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE 5025 && typecode != REAL_TYPE && typecode != POINTER_TYPE 5026 && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE 5027 && typecode != BITINT_TYPE) 5028 { 5029 error_at (location, 5030 "wrong type argument to unary exclamation mark"); 5031 return error_mark_node; 5032 } 5033 if (int_operands) 5034 { 5035 arg = c_objc_common_truthvalue_conversion (location, xarg); 5036 arg = remove_c_maybe_const_expr (arg); 5037 } 5038 else 5039 arg = c_objc_common_truthvalue_conversion (location, arg); 5040 ret = invert_truthvalue_loc (location, arg); 5041 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */ 5042 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret)) 5043 location = EXPR_LOCATION (ret); 5044 goto return_build_unary_op; 5045 5046 case REALPART_EXPR: 5047 case IMAGPART_EXPR: 5048 ret = build_real_imag_expr (location, code, arg); 5049 if (ret == error_mark_node) 5050 return error_mark_node; 5051 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE) 5052 eptype = TREE_TYPE (eptype); 5053 goto return_build_unary_op; 5054 5055 case PREINCREMENT_EXPR: 5056 case POSTINCREMENT_EXPR: 5057 case PREDECREMENT_EXPR: 5058 case POSTDECREMENT_EXPR: 5059 5060 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) 5061 { 5062 tree inner = build_unary_op (location, code, 5063 C_MAYBE_CONST_EXPR_EXPR (arg), 5064 noconvert); 5065 if (inner == error_mark_node) 5066 return error_mark_node; 5067 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), 5068 C_MAYBE_CONST_EXPR_PRE (arg), inner); 5069 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); 5070 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1; 5071 goto return_build_unary_op; 5072 } 5073 5074 /* Complain about anything that is not a true lvalue. In 5075 Objective-C, skip this check for property_refs. */ 5076 if (!objc_is_property_ref (arg) 5077 && !lvalue_or_else (location, 5078 arg, ((code == PREINCREMENT_EXPR 5079 || code == POSTINCREMENT_EXPR) 5080 ? lv_increment 5081 : lv_decrement))) 5082 return error_mark_node; 5083 5084 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE) 5085 { 5086 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) 5087 warning_at (location, OPT_Wc___compat, 5088 "increment of enumeration value is invalid in C++"); 5089 else 5090 warning_at (location, OPT_Wc___compat, 5091 "decrement of enumeration value is invalid in C++"); 5092 } 5093 5094 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg))) 5095 { 5096 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) 5097 warning_at (location, OPT_Wbool_operation, 5098 "increment of a boolean expression"); 5099 else 5100 warning_at (location, OPT_Wbool_operation, 5101 "decrement of a boolean expression"); 5102 } 5103 5104 /* Ensure the argument is fully folded inside any SAVE_EXPR. */ 5105 arg = c_fully_fold (arg, false, NULL, true); 5106 5107 bool atomic_op; 5108 atomic_op = really_atomic_lvalue (arg); 5109 5110 /* Increment or decrement the real part of the value, 5111 and don't change the imaginary part. */ 5112 if (typecode == COMPLEX_TYPE) 5113 { 5114 tree real, imag; 5115 5116 pedwarn (location, OPT_Wpedantic, 5117 "ISO C does not support %<++%> and %<--%> on complex types"); 5118 5119 if (!atomic_op) 5120 { 5121 arg = stabilize_reference (arg); 5122 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 5123 true); 5124 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 5125 true); 5126 real = build_unary_op (EXPR_LOCATION (arg), code, real, true); 5127 if (real == error_mark_node || imag == error_mark_node) 5128 return error_mark_node; 5129 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg), 5130 real, imag); 5131 goto return_build_unary_op; 5132 } 5133 } 5134 5135 /* Report invalid types. */ 5136 5137 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE 5138 && typecode != INTEGER_TYPE && typecode != REAL_TYPE 5139 && typecode != COMPLEX_TYPE && typecode != BITINT_TYPE 5140 && !gnu_vector_type_p (TREE_TYPE (arg))) 5141 { 5142 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) 5143 error_at (location, "wrong type argument to increment"); 5144 else 5145 error_at (location, "wrong type argument to decrement"); 5146 5147 return error_mark_node; 5148 } 5149 5150 { 5151 tree inc; 5152 5153 argtype = TREE_TYPE (arg); 5154 5155 /* Compute the increment. */ 5156 5157 if (typecode == POINTER_TYPE) 5158 { 5159 /* If pointer target is an incomplete type, 5160 we just cannot know how to do the arithmetic. */ 5161 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype))) 5162 { 5163 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) 5164 error_at (location, 5165 "increment of pointer to an incomplete type %qT", 5166 TREE_TYPE (argtype)); 5167 else 5168 error_at (location, 5169 "decrement of pointer to an incomplete type %qT", 5170 TREE_TYPE (argtype)); 5171 } 5172 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE 5173 || VOID_TYPE_P (TREE_TYPE (argtype))) 5174 { 5175 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) 5176 pedwarn (location, OPT_Wpointer_arith, 5177 "wrong type argument to increment"); 5178 else 5179 pedwarn (location, OPT_Wpointer_arith, 5180 "wrong type argument to decrement"); 5181 } 5182 else 5183 verify_type_context (location, TCTX_POINTER_ARITH, 5184 TREE_TYPE (argtype)); 5185 5186 inc = c_size_in_bytes (TREE_TYPE (argtype)); 5187 inc = convert_to_ptrofftype_loc (location, inc); 5188 } 5189 else if (FRACT_MODE_P (TYPE_MODE (argtype))) 5190 { 5191 /* For signed fract types, we invert ++ to -- or 5192 -- to ++, and change inc from 1 to -1, because 5193 it is not possible to represent 1 in signed fract constants. 5194 For unsigned fract types, the result always overflows and 5195 we get an undefined (original) or the maximum value. */ 5196 if (code == PREINCREMENT_EXPR) 5197 code = PREDECREMENT_EXPR; 5198 else if (code == PREDECREMENT_EXPR) 5199 code = PREINCREMENT_EXPR; 5200 else if (code == POSTINCREMENT_EXPR) 5201 code = POSTDECREMENT_EXPR; 5202 else /* code == POSTDECREMENT_EXPR */ 5203 code = POSTINCREMENT_EXPR; 5204 5205 inc = integer_minus_one_node; 5206 inc = convert (argtype, inc); 5207 } 5208 else 5209 { 5210 inc = VECTOR_TYPE_P (argtype) 5211 ? build_one_cst (argtype) 5212 : integer_one_node; 5213 inc = convert (argtype, inc); 5214 } 5215 5216 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we 5217 need to ask Objective-C to build the increment or decrement 5218 expression for it. */ 5219 if (objc_is_property_ref (arg)) 5220 return objc_build_incr_expr_for_property_ref (location, code, 5221 arg, inc); 5222 5223 /* Report a read-only lvalue. */ 5224 if (TYPE_READONLY (argtype)) 5225 { 5226 readonly_error (location, arg, 5227 ((code == PREINCREMENT_EXPR 5228 || code == POSTINCREMENT_EXPR) 5229 ? lv_increment : lv_decrement)); 5230 return error_mark_node; 5231 } 5232 else if (TREE_READONLY (arg)) 5233 readonly_warning (arg, 5234 ((code == PREINCREMENT_EXPR 5235 || code == POSTINCREMENT_EXPR) 5236 ? lv_increment : lv_decrement)); 5237 5238 /* If the argument is atomic, use the special code sequences for 5239 atomic compound assignment. */ 5240 if (atomic_op) 5241 { 5242 arg = stabilize_reference (arg); 5243 ret = build_atomic_assign (location, arg, 5244 ((code == PREINCREMENT_EXPR 5245 || code == POSTINCREMENT_EXPR) 5246 ? PLUS_EXPR 5247 : MINUS_EXPR), 5248 (FRACT_MODE_P (TYPE_MODE (argtype)) 5249 ? inc 5250 : integer_one_node), 5251 (code == POSTINCREMENT_EXPR 5252 || code == POSTDECREMENT_EXPR)); 5253 goto return_build_unary_op; 5254 } 5255 5256 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg))) 5257 val = boolean_increment (code, arg); 5258 else 5259 val = build2 (code, TREE_TYPE (arg), arg, inc); 5260 TREE_SIDE_EFFECTS (val) = 1; 5261 if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED) 5262 TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val), 5263 TYPE_UNQUALIFIED); 5264 ret = val; 5265 goto return_build_unary_op; 5266 } 5267 5268 case ADDR_EXPR: 5269 /* Note that this operation never does default_conversion. */ 5270 5271 /* The operand of unary '&' must be an lvalue (which excludes 5272 expressions of type void), or, in C99, the result of a [] or 5273 unary '*' operator. */ 5274 if (VOID_TYPE_P (TREE_TYPE (arg)) 5275 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED 5276 && (!INDIRECT_REF_P (arg) || !flag_isoc99)) 5277 pedwarn (location, 0, "taking address of expression of type %<void%>"); 5278 5279 /* Let &* cancel out to simplify resulting code. */ 5280 if (INDIRECT_REF_P (arg)) 5281 { 5282 /* Don't let this be an lvalue. */ 5283 if (lvalue_p (TREE_OPERAND (arg, 0))) 5284 return non_lvalue_loc (location, TREE_OPERAND (arg, 0)); 5285 ret = TREE_OPERAND (arg, 0); 5286 goto return_build_unary_op; 5287 } 5288 5289 /* Anything not already handled and not a true memory reference 5290 or a non-lvalue array is an error. */ 5291 if (typecode != FUNCTION_TYPE && !noconvert 5292 && !lvalue_or_else (location, arg, lv_addressof)) 5293 return error_mark_node; 5294 5295 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify 5296 folding later. */ 5297 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) 5298 { 5299 tree inner = build_unary_op (location, code, 5300 C_MAYBE_CONST_EXPR_EXPR (arg), 5301 noconvert); 5302 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), 5303 C_MAYBE_CONST_EXPR_PRE (arg), inner); 5304 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); 5305 C_MAYBE_CONST_EXPR_NON_CONST (ret) 5306 = C_MAYBE_CONST_EXPR_NON_CONST (arg); 5307 goto return_build_unary_op; 5308 } 5309 5310 /* Ordinary case; arg is a COMPONENT_REF or a decl. */ 5311 argtype = TREE_TYPE (arg); 5312 5313 /* If the lvalue is const or volatile, merge that into the type 5314 to which the address will point. This is only needed 5315 for function types. */ 5316 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg)) 5317 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)) 5318 && TREE_CODE (argtype) == FUNCTION_TYPE) 5319 { 5320 int orig_quals = TYPE_QUALS (strip_array_types (argtype)); 5321 int quals = orig_quals; 5322 5323 if (TREE_READONLY (arg)) 5324 quals |= TYPE_QUAL_CONST; 5325 if (TREE_THIS_VOLATILE (arg)) 5326 quals |= TYPE_QUAL_VOLATILE; 5327 5328 argtype = c_build_qualified_type (argtype, quals); 5329 } 5330 5331 switch (TREE_CODE (arg)) 5332 { 5333 case COMPONENT_REF: 5334 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))) 5335 { 5336 error_at (location, "cannot take address of bit-field %qD", 5337 TREE_OPERAND (arg, 1)); 5338 return error_mark_node; 5339 } 5340 5341 /* fall through */ 5342 5343 case ARRAY_REF: 5344 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0)))) 5345 { 5346 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg)) 5347 && !POINTER_TYPE_P (TREE_TYPE (arg)) 5348 && !VECTOR_TYPE_P (TREE_TYPE (arg))) 5349 { 5350 error_at (location, "cannot take address of scalar with " 5351 "reverse storage order"); 5352 return error_mark_node; 5353 } 5354 5355 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE 5356 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg))) 5357 warning_at (location, OPT_Wscalar_storage_order, 5358 "address of array with reverse scalar storage " 5359 "order requested"); 5360 } 5361 5362 default: 5363 break; 5364 } 5365 5366 if (!c_mark_addressable (arg)) 5367 return error_mark_node; 5368 5369 gcc_assert (TREE_CODE (arg) != COMPONENT_REF 5370 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))); 5371 5372 argtype = c_build_pointer_type (argtype); 5373 5374 /* ??? Cope with user tricks that amount to offsetof. Delete this 5375 when we have proper support for integer constant expressions. */ 5376 val = get_base_address (arg); 5377 if (val && INDIRECT_REF_P (val) 5378 && TREE_CONSTANT (TREE_OPERAND (val, 0))) 5379 { 5380 ret = fold_offsetof (arg, argtype); 5381 goto return_build_unary_op; 5382 } 5383 5384 val = build1 (ADDR_EXPR, argtype, arg); 5385 5386 ret = val; 5387 goto return_build_unary_op; 5388 5389 case PAREN_EXPR: 5390 ret = build1 (code, TREE_TYPE (arg), arg); 5391 goto return_build_unary_op; 5392 5393 default: 5394 gcc_unreachable (); 5395 } 5396 5397 if (argtype == NULL_TREE) 5398 argtype = TREE_TYPE (arg); 5399 if (TREE_CODE (arg) == INTEGER_CST) 5400 ret = (require_constant_value 5401 ? fold_build1_initializer_loc (location, code, argtype, arg) 5402 : fold_build1_loc (location, code, argtype, arg)); 5403 else 5404 ret = build1 (code, argtype, arg); 5405 return_build_unary_op: 5406 gcc_assert (ret != error_mark_node); 5407 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) 5408 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg))) 5409 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); 5410 else if (TREE_CODE (ret) != INTEGER_CST && int_operands) 5411 ret = note_integer_operands (ret); 5412 if (eptype) 5413 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); 5414 protected_set_expr_location (ret, location); 5415 return ret; 5416 } 5417 5418 /* Return nonzero if REF is an lvalue valid for this language. 5419 Lvalues can be assigned, unless their type has TYPE_READONLY. 5420 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */ 5421 5422 bool 5423 lvalue_p (const_tree ref) 5424 { 5425 const enum tree_code code = TREE_CODE (ref); 5426 5427 switch (code) 5428 { 5429 case REALPART_EXPR: 5430 case IMAGPART_EXPR: 5431 case COMPONENT_REF: 5432 return lvalue_p (TREE_OPERAND (ref, 0)); 5433 5434 case C_MAYBE_CONST_EXPR: 5435 return lvalue_p (TREE_OPERAND (ref, 1)); 5436 5437 case COMPOUND_LITERAL_EXPR: 5438 case STRING_CST: 5439 return true; 5440 5441 case MEM_REF: 5442 case TARGET_MEM_REF: 5443 /* MEM_REFs can appear from -fgimple parsing or folding, so allow them 5444 here as well. */ 5445 case INDIRECT_REF: 5446 case ARRAY_REF: 5447 case VAR_DECL: 5448 case PARM_DECL: 5449 case RESULT_DECL: 5450 case ERROR_MARK: 5451 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE 5452 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE); 5453 5454 case BIND_EXPR: 5455 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; 5456 5457 default: 5458 return false; 5459 } 5460 } 5461 5462 /* Give a warning for storing in something that is read-only in GCC 5464 terms but not const in ISO C terms. */ 5465 5466 static void 5467 readonly_warning (tree arg, enum lvalue_use use) 5468 { 5469 switch (use) 5470 { 5471 case lv_assign: 5472 warning (0, "assignment of read-only location %qE", arg); 5473 break; 5474 case lv_increment: 5475 warning (0, "increment of read-only location %qE", arg); 5476 break; 5477 case lv_decrement: 5478 warning (0, "decrement of read-only location %qE", arg); 5479 break; 5480 default: 5481 gcc_unreachable (); 5482 } 5483 return; 5484 } 5485 5486 5487 /* Return nonzero if REF is an lvalue valid for this language; 5488 otherwise, print an error message and return zero. USE says 5489 how the lvalue is being used and so selects the error message. 5490 LOCATION is the location at which any error should be reported. */ 5491 5492 static int 5493 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use) 5494 { 5495 int win = lvalue_p (ref); 5496 5497 if (!win) 5498 lvalue_error (loc, use); 5499 5500 return win; 5501 } 5502 5503 /* Mark EXP saying that we need to be able to take the 5505 address of it; it should not be allocated in a register. 5506 Returns true if successful. ARRAY_REF_P is true if this 5507 is for ARRAY_REF construction - in that case we don't want 5508 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE, 5509 it is fine to use ARRAY_REFs for vector subscripts on vector 5510 register variables. */ 5511 5512 bool 5513 c_mark_addressable (tree exp, bool array_ref_p) 5514 { 5515 tree x = exp; 5516 5517 while (1) 5518 switch (TREE_CODE (x)) 5519 { 5520 case VIEW_CONVERT_EXPR: 5521 if (array_ref_p 5522 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 5523 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0)))) 5524 return true; 5525 x = TREE_OPERAND (x, 0); 5526 break; 5527 5528 case COMPONENT_REF: 5529 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) 5530 { 5531 error ("cannot take address of bit-field %qD", 5532 TREE_OPERAND (x, 1)); 5533 return false; 5534 } 5535 /* FALLTHRU */ 5536 case ADDR_EXPR: 5537 case ARRAY_REF: 5538 case REALPART_EXPR: 5539 case IMAGPART_EXPR: 5540 x = TREE_OPERAND (x, 0); 5541 break; 5542 5543 case COMPOUND_LITERAL_EXPR: 5544 if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x))) 5545 { 5546 error ("address of register compound literal requested"); 5547 return false; 5548 } 5549 TREE_ADDRESSABLE (x) = 1; 5550 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1; 5551 return true; 5552 5553 case CONSTRUCTOR: 5554 TREE_ADDRESSABLE (x) = 1; 5555 return true; 5556 5557 case VAR_DECL: 5558 case CONST_DECL: 5559 case PARM_DECL: 5560 case RESULT_DECL: 5561 if (C_DECL_REGISTER (x) 5562 && DECL_NONLOCAL (x)) 5563 { 5564 if (TREE_PUBLIC (x) || is_global_var (x)) 5565 { 5566 error 5567 ("global register variable %qD used in nested function", x); 5568 return false; 5569 } 5570 pedwarn (input_location, 0, "register variable %qD used in nested function", x); 5571 } 5572 else if (C_DECL_REGISTER (x)) 5573 { 5574 if (TREE_PUBLIC (x) || is_global_var (x)) 5575 error ("address of global register variable %qD requested", x); 5576 else 5577 error ("address of register variable %qD requested", x); 5578 return false; 5579 } 5580 5581 /* FALLTHRU */ 5582 case FUNCTION_DECL: 5583 TREE_ADDRESSABLE (x) = 1; 5584 /* FALLTHRU */ 5585 default: 5586 return true; 5587 } 5588 } 5589 5590 /* Convert EXPR to TYPE, warning about conversion problems with 5592 constants. SEMANTIC_TYPE is the type this conversion would use 5593 without excess precision. If SEMANTIC_TYPE is NULL, this function 5594 is equivalent to convert_and_check. This function is a wrapper that 5595 handles conversions that may be different than 5596 the usual ones because of excess precision. */ 5597 5598 static tree 5599 ep_convert_and_check (location_t loc, tree type, tree expr, 5600 tree semantic_type) 5601 { 5602 if (TREE_TYPE (expr) == type) 5603 return expr; 5604 5605 /* For C11, integer conversions may have results with excess 5606 precision. */ 5607 if (flag_isoc11 || !semantic_type) 5608 return convert_and_check (loc, type, expr); 5609 5610 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE 5611 && TREE_TYPE (expr) != semantic_type) 5612 { 5613 /* For integers, we need to check the real conversion, not 5614 the conversion to the excess precision type. */ 5615 expr = convert_and_check (loc, semantic_type, expr); 5616 } 5617 /* Result type is the excess precision type, which should be 5618 large enough, so do not check. */ 5619 return convert (type, expr); 5620 } 5621 5622 /* If EXPR refers to a built-in declared without a prototype returns 5623 the actual type of the built-in and, if non-null, set *BLTIN to 5624 a pointer to the built-in. Otherwise return the type of EXPR 5625 and clear *BLTIN if non-null. */ 5626 5627 static tree 5628 type_or_builtin_type (tree expr, tree *bltin = NULL) 5629 { 5630 tree dummy; 5631 if (!bltin) 5632 bltin = &dummy; 5633 5634 *bltin = NULL_TREE; 5635 5636 tree type = TREE_TYPE (expr); 5637 if (TREE_CODE (expr) != ADDR_EXPR) 5638 return type; 5639 5640 tree oper = TREE_OPERAND (expr, 0); 5641 if (!DECL_P (oper) 5642 || TREE_CODE (oper) != FUNCTION_DECL 5643 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL)) 5644 return type; 5645 5646 built_in_function code = DECL_FUNCTION_CODE (oper); 5647 if (!C_DECL_BUILTIN_PROTOTYPE (oper)) 5648 return type; 5649 5650 if ((*bltin = builtin_decl_implicit (code))) 5651 type = c_build_pointer_type (TREE_TYPE (*bltin)); 5652 5653 return type; 5654 } 5655 5656 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If 5657 IFEXP_BCP then the condition is a call to __builtin_constant_p, and 5658 if folded to an integer constant then the unselected half may 5659 contain arbitrary operations not normally permitted in constant 5660 expressions. Set the location of the expression to LOC. */ 5661 5662 tree 5663 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, 5664 tree op1, tree op1_original_type, location_t op1_loc, 5665 tree op2, tree op2_original_type, location_t op2_loc) 5666 { 5667 tree type1; 5668 tree type2; 5669 enum tree_code code1; 5670 enum tree_code code2; 5671 tree result_type = NULL; 5672 tree semantic_result_type = NULL; 5673 tree orig_op1 = op1, orig_op2 = op2; 5674 bool int_const, op1_int_operands, op2_int_operands, int_operands; 5675 bool ifexp_int_operands; 5676 tree ret; 5677 5678 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); 5679 if (op1_int_operands) 5680 op1 = remove_c_maybe_const_expr (op1); 5681 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2); 5682 if (op2_int_operands) 5683 op2 = remove_c_maybe_const_expr (op2); 5684 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp); 5685 if (ifexp_int_operands) 5686 ifexp = remove_c_maybe_const_expr (ifexp); 5687 5688 /* Promote both alternatives. */ 5689 5690 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) 5691 op1 = default_conversion (op1); 5692 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) 5693 op2 = default_conversion (op2); 5694 5695 if (TREE_CODE (ifexp) == ERROR_MARK 5696 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK 5697 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) 5698 return error_mark_node; 5699 5700 tree bltin1 = NULL_TREE; 5701 tree bltin2 = NULL_TREE; 5702 type1 = type_or_builtin_type (op1, &bltin1); 5703 code1 = TREE_CODE (type1); 5704 type2 = type_or_builtin_type (op2, &bltin2); 5705 code2 = TREE_CODE (type2); 5706 5707 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1)) 5708 return error_mark_node; 5709 5710 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2)) 5711 return error_mark_node; 5712 5713 /* C90 does not permit non-lvalue arrays in conditional expressions. 5714 In C99 they will be pointers by now. */ 5715 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE) 5716 { 5717 error_at (colon_loc, "non-lvalue array in conditional expression"); 5718 return error_mark_node; 5719 } 5720 5721 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR 5722 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR) 5723 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE 5724 || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE) 5725 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE 5726 || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE)) 5727 { 5728 semantic_result_type = c_common_type (type1, type2); 5729 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) 5730 { 5731 op1 = TREE_OPERAND (op1, 0); 5732 type1 = TREE_TYPE (op1); 5733 gcc_assert (TREE_CODE (type1) == code1); 5734 } 5735 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR) 5736 { 5737 op2 = TREE_OPERAND (op2, 0); 5738 type2 = TREE_TYPE (op2); 5739 gcc_assert (TREE_CODE (type2) == code2); 5740 } 5741 } 5742 5743 if (warn_cxx_compat) 5744 { 5745 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1); 5746 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2); 5747 5748 if (TREE_CODE (t1) == ENUMERAL_TYPE 5749 && TREE_CODE (t2) == ENUMERAL_TYPE 5750 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2)) 5751 warning_at (colon_loc, OPT_Wc___compat, 5752 ("different enum types in conditional is " 5753 "invalid in C++: %qT vs %qT"), 5754 t1, t2); 5755 } 5756 5757 /* Quickly detect the usual case where op1 and op2 have the same type 5758 after promotion. */ 5759 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) 5760 { 5761 if (type1 == type2) 5762 result_type = type1; 5763 else 5764 result_type = TYPE_MAIN_VARIANT (type1); 5765 } 5766 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE 5767 || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE) 5768 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE 5769 || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE)) 5770 { 5771 /* In C11, a conditional expression between a floating-point 5772 type and an integer type should convert the integer type to 5773 the evaluation format of the floating-point type, with 5774 possible excess precision. */ 5775 tree eptype1 = type1; 5776 tree eptype2 = type2; 5777 if (flag_isoc11) 5778 { 5779 tree eptype; 5780 if (ANY_INTEGRAL_TYPE_P (type1) 5781 && (eptype = excess_precision_type (type2)) != NULL_TREE) 5782 { 5783 eptype2 = eptype; 5784 if (!semantic_result_type) 5785 semantic_result_type = c_common_type (type1, type2); 5786 } 5787 else if (ANY_INTEGRAL_TYPE_P (type2) 5788 && (eptype = excess_precision_type (type1)) != NULL_TREE) 5789 { 5790 eptype1 = eptype; 5791 if (!semantic_result_type) 5792 semantic_result_type = c_common_type (type1, type2); 5793 } 5794 } 5795 result_type = c_common_type (eptype1, eptype2); 5796 if (result_type == error_mark_node) 5797 return error_mark_node; 5798 do_warn_double_promotion (result_type, type1, type2, 5799 "implicit conversion from %qT to %qT to " 5800 "match other result of conditional", 5801 colon_loc); 5802 5803 /* If -Wsign-compare, warn here if type1 and type2 have 5804 different signedness. We'll promote the signed to unsigned 5805 and later code won't know it used to be different. 5806 Do this check on the original types, so that explicit casts 5807 will be considered, but default promotions won't. */ 5808 if (c_inhibit_evaluation_warnings == 0) 5809 { 5810 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1)); 5811 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2)); 5812 5813 if (unsigned_op1 ^ unsigned_op2) 5814 { 5815 bool ovf; 5816 5817 /* Do not warn if the result type is signed, since the 5818 signed type will only be chosen if it can represent 5819 all the values of the unsigned type. */ 5820 if (!TYPE_UNSIGNED (result_type)) 5821 /* OK */; 5822 else 5823 { 5824 bool op1_maybe_const = true; 5825 bool op2_maybe_const = true; 5826 5827 /* Do not warn if the signed quantity is an 5828 unsuffixed integer literal (or some static 5829 constant expression involving such literals) and 5830 it is non-negative. This warning requires the 5831 operands to be folded for best results, so do 5832 that folding in this case even without 5833 warn_sign_compare to avoid warning options 5834 possibly affecting code generation. */ 5835 c_inhibit_evaluation_warnings 5836 += (ifexp == truthvalue_false_node); 5837 op1 = c_fully_fold (op1, require_constant_value, 5838 &op1_maybe_const); 5839 c_inhibit_evaluation_warnings 5840 -= (ifexp == truthvalue_false_node); 5841 5842 c_inhibit_evaluation_warnings 5843 += (ifexp == truthvalue_true_node); 5844 op2 = c_fully_fold (op2, require_constant_value, 5845 &op2_maybe_const); 5846 c_inhibit_evaluation_warnings 5847 -= (ifexp == truthvalue_true_node); 5848 5849 if (warn_sign_compare) 5850 { 5851 if ((unsigned_op2 5852 && tree_expr_nonnegative_warnv_p (op1, &ovf)) 5853 || (unsigned_op1 5854 && tree_expr_nonnegative_warnv_p (op2, &ovf))) 5855 /* OK */; 5856 else if (unsigned_op2) 5857 warning_at (op1_loc, OPT_Wsign_compare, 5858 "operand of %<?:%> changes signedness from " 5859 "%qT to %qT due to unsignedness of other " 5860 "operand", TREE_TYPE (orig_op1), 5861 TREE_TYPE (orig_op2)); 5862 else 5863 warning_at (op2_loc, OPT_Wsign_compare, 5864 "operand of %<?:%> changes signedness from " 5865 "%qT to %qT due to unsignedness of other " 5866 "operand", TREE_TYPE (orig_op2), 5867 TREE_TYPE (orig_op1)); 5868 } 5869 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) 5870 op1 = c_wrap_maybe_const (op1, !op1_maybe_const); 5871 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST) 5872 op2 = c_wrap_maybe_const (op2, !op2_maybe_const); 5873 } 5874 } 5875 } 5876 } 5877 else if (code1 == VOID_TYPE || code2 == VOID_TYPE) 5878 { 5879 if (code1 != VOID_TYPE || code2 != VOID_TYPE) 5880 pedwarn (colon_loc, OPT_Wpedantic, 5881 "ISO C forbids conditional expr with only one void side"); 5882 result_type = void_type_node; 5883 } 5884 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) 5885 { 5886 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); 5887 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2)); 5888 addr_space_t as_common; 5889 5890 if (comp_target_types (colon_loc, type1, type2)) 5891 result_type = common_pointer_type (type1, type2); 5892 else if (null_pointer_constant_p (orig_op1)) 5893 result_type = type2; 5894 else if (null_pointer_constant_p (orig_op2)) 5895 result_type = type1; 5896 else if (!addr_space_superset (as1, as2, &as_common)) 5897 { 5898 error_at (colon_loc, "pointers to disjoint address spaces " 5899 "used in conditional expression"); 5900 return error_mark_node; 5901 } 5902 else if ((VOID_TYPE_P (TREE_TYPE (type1)) 5903 && !TYPE_ATOMIC (TREE_TYPE (type1))) 5904 || (VOID_TYPE_P (TREE_TYPE (type2)) 5905 && !TYPE_ATOMIC (TREE_TYPE (type2)))) 5906 { 5907 tree t1 = TREE_TYPE (type1); 5908 tree t2 = TREE_TYPE (type2); 5909 if (!(VOID_TYPE_P (t1) 5910 && !TYPE_ATOMIC (t1))) 5911 { 5912 /* roles are swapped */ 5913 t1 = t2; 5914 t2 = TREE_TYPE (type1); 5915 } 5916 tree t2_stripped = strip_array_types (t2); 5917 if ((TREE_CODE (t2) == ARRAY_TYPE) 5918 && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1))) 5919 { 5920 if (!flag_isoc23) 5921 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers, 5922 "pointer to array loses qualifier " 5923 "in conditional expression"); 5924 else if (warn_c11_c23_compat > 0) 5925 warning_at (colon_loc, OPT_Wc11_c23_compat, 5926 "pointer to array loses qualifier " 5927 "in conditional expression in ISO C before C23"); 5928 } 5929 if (TREE_CODE (t2) == FUNCTION_TYPE) 5930 pedwarn (colon_loc, OPT_Wpedantic, 5931 "ISO C forbids conditional expr between " 5932 "%<void *%> and function pointer"); 5933 /* for array, use qualifiers of element type */ 5934 if (flag_isoc23) 5935 t2 = t2_stripped; 5936 result_type = c_build_pointer_type (qualify_type (t1, t2)); 5937 } 5938 /* Objective-C pointer comparisons are a bit more lenient. */ 5939 else if (objc_have_common_type (type1, type2, -3, NULL_TREE)) 5940 result_type = objc_common_type (type1, type2); 5941 else 5942 { 5943 int qual = ENCODE_QUAL_ADDR_SPACE (as_common); 5944 diagnostic_t kind = DK_PERMERROR; 5945 if (!flag_isoc99) 5946 /* This downgrade to a warning ensures that -std=gnu89 5947 -pedantic-errors does not flag these mismatches between 5948 builtins as errors (as DK_PERMERROR would). ISO C99 5949 and later do not have implicit function declarations, 5950 so the mismatch cannot occur naturally there. */ 5951 kind = bltin1 && bltin2 ? DK_WARNING : DK_PEDWARN; 5952 if (emit_diagnostic (kind, colon_loc, OPT_Wincompatible_pointer_types, 5953 "pointer type mismatch " 5954 "in conditional expression")) 5955 { 5956 inform (op1_loc, "first expression has type %qT", type1); 5957 inform (op2_loc, "second expression has type %qT", type2); 5958 } 5959 result_type = c_build_pointer_type 5960 (c_build_qualified_type (void_type_node, qual)); 5961 } 5962 } 5963 else if (code1 == POINTER_TYPE 5964 && (code2 == INTEGER_TYPE || code2 == BITINT_TYPE)) 5965 { 5966 if (!null_pointer_constant_p (orig_op2)) 5967 permerror_opt (colon_loc, OPT_Wint_conversion, 5968 "pointer/integer type mismatch " 5969 "in conditional expression"); 5970 else 5971 { 5972 op2 = null_pointer_node; 5973 } 5974 result_type = type1; 5975 } 5976 else if (code2 == POINTER_TYPE 5977 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE)) 5978 { 5979 if (!null_pointer_constant_p (orig_op1)) 5980 permerror_opt (colon_loc, OPT_Wint_conversion, 5981 "pointer/integer type mismatch " 5982 "in conditional expression"); 5983 else 5984 { 5985 op1 = null_pointer_node; 5986 } 5987 result_type = type2; 5988 } 5989 /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has 5990 type nullptr_t and the other is a pointer, the result type is the pointer 5991 type." */ 5992 else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE) 5993 result_type = type2; 5994 else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE) 5995 result_type = type1; 5996 else if (RECORD_OR_UNION_TYPE_P (type1) && RECORD_OR_UNION_TYPE_P (type2) 5997 && comptypes (TYPE_MAIN_VARIANT (type1), 5998 TYPE_MAIN_VARIANT (type2))) 5999 result_type = composite_type (TYPE_MAIN_VARIANT (type1), 6000 TYPE_MAIN_VARIANT (type2)); 6001 6002 if (!result_type) 6003 { 6004 if (flag_cond_mismatch) 6005 result_type = void_type_node; 6006 else 6007 { 6008 error_at (colon_loc, "type mismatch in conditional expression"); 6009 return error_mark_node; 6010 } 6011 } 6012 6013 /* Merge const and volatile flags of the incoming types. */ 6014 result_type 6015 = build_type_variant (result_type, 6016 TYPE_READONLY (type1) || TYPE_READONLY (type2), 6017 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2)); 6018 6019 op1 = ep_convert_and_check (colon_loc, result_type, op1, 6020 semantic_result_type); 6021 op2 = ep_convert_and_check (colon_loc, result_type, op2, 6022 semantic_result_type); 6023 6024 if (ifexp_bcp && ifexp == truthvalue_true_node) 6025 { 6026 op2_int_operands = true; 6027 op1 = c_fully_fold (op1, require_constant_value, NULL); 6028 } 6029 if (ifexp_bcp && ifexp == truthvalue_false_node) 6030 { 6031 op1_int_operands = true; 6032 op2 = c_fully_fold (op2, require_constant_value, NULL); 6033 } 6034 int_const = int_operands = (ifexp_int_operands 6035 && op1_int_operands 6036 && op2_int_operands); 6037 if (int_operands) 6038 { 6039 int_const = ((ifexp == truthvalue_true_node 6040 && TREE_CODE (orig_op1) == INTEGER_CST 6041 && !TREE_OVERFLOW (orig_op1)) 6042 || (ifexp == truthvalue_false_node 6043 && TREE_CODE (orig_op2) == INTEGER_CST 6044 && !TREE_OVERFLOW (orig_op2))); 6045 } 6046 6047 /* Need to convert condition operand into a vector mask. */ 6048 if (VECTOR_TYPE_P (TREE_TYPE (ifexp))) 6049 { 6050 tree vectype = TREE_TYPE (ifexp); 6051 tree elem_type = TREE_TYPE (vectype); 6052 tree zero = build_int_cst (elem_type, 0); 6053 tree zero_vec = build_vector_from_val (vectype, zero); 6054 tree cmp_type = truth_type_for (vectype); 6055 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec); 6056 } 6057 6058 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST)) 6059 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2); 6060 else 6061 { 6062 if (int_operands) 6063 { 6064 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be 6065 nested inside of the expression. */ 6066 op1 = c_fully_fold (op1, false, NULL); 6067 op2 = c_fully_fold (op2, false, NULL); 6068 } 6069 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2); 6070 if (int_operands) 6071 ret = note_integer_operands (ret); 6072 } 6073 if (semantic_result_type) 6074 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); 6075 6076 protected_set_expr_location (ret, colon_loc); 6077 6078 /* If the OP1 and OP2 are the same and don't have side-effects, 6079 warn here, because the COND_EXPR will be turned into OP1. */ 6080 if (warn_duplicated_branches 6081 && TREE_CODE (ret) == COND_EXPR 6082 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD))) 6083 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches, 6084 "this condition has identical branches"); 6085 6086 return ret; 6087 } 6088 6089 /* EXPR is an expression, location LOC, whose result is discarded. 6091 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR 6092 whose right-hand operand is such a call, possibly recursively). */ 6093 6094 static void 6095 maybe_warn_nodiscard (location_t loc, tree expr) 6096 { 6097 if (VOID_TYPE_P (TREE_TYPE (expr))) 6098 return; 6099 while (TREE_CODE (expr) == COMPOUND_EXPR) 6100 { 6101 expr = TREE_OPERAND (expr, 1); 6102 if (EXPR_HAS_LOCATION (expr)) 6103 loc = EXPR_LOCATION (expr); 6104 } 6105 if (TREE_CODE (expr) != CALL_EXPR) 6106 return; 6107 tree fn = CALL_EXPR_FN (expr); 6108 if (!fn) 6109 return; 6110 tree attr; 6111 if (TREE_CODE (fn) == ADDR_EXPR 6112 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL 6113 && (attr = lookup_attribute ("nodiscard", 6114 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0))))) 6115 { 6116 fn = TREE_OPERAND (fn, 0); 6117 tree args = TREE_VALUE (attr); 6118 if (args) 6119 args = TREE_VALUE (args); 6120 auto_diagnostic_group d; 6121 int warned; 6122 if (args) 6123 warned = warning_at (loc, OPT_Wunused_result, 6124 "ignoring return value of %qD, declared with " 6125 "attribute %<nodiscard%>: %E", fn, args); 6126 else 6127 warned = warning_at (loc, OPT_Wunused_result, 6128 "ignoring return value of %qD, declared with " 6129 "attribute %<nodiscard%>", fn); 6130 if (warned) 6131 inform (DECL_SOURCE_LOCATION (fn), "declared here"); 6132 } 6133 else 6134 { 6135 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))); 6136 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype)); 6137 if (!attr) 6138 return; 6139 tree args = TREE_VALUE (attr); 6140 if (args) 6141 args = TREE_VALUE (args); 6142 auto_diagnostic_group d; 6143 int warned; 6144 if (args) 6145 warned = warning_at (loc, OPT_Wunused_result, 6146 "ignoring return value of type %qT, declared " 6147 "with attribute %<nodiscard%>: %E", 6148 rettype, args); 6149 else 6150 warned = warning_at (loc, OPT_Wunused_result, 6151 "ignoring return value of type %qT, declared " 6152 "with attribute %<nodiscard%>", rettype); 6153 if (warned) 6154 { 6155 if (TREE_CODE (fn) == ADDR_EXPR) 6156 { 6157 fn = TREE_OPERAND (fn, 0); 6158 if (TREE_CODE (fn) == FUNCTION_DECL) 6159 inform (DECL_SOURCE_LOCATION (fn), 6160 "in call to %qD, declared here", fn); 6161 } 6162 } 6163 } 6164 } 6165 6166 /* Return a compound expression that performs two expressions and 6167 returns the value of the second of them. 6168 6169 LOC is the location of the COMPOUND_EXPR. */ 6170 6171 tree 6172 build_compound_expr (location_t loc, tree expr1, tree expr2) 6173 { 6174 bool expr1_int_operands, expr2_int_operands; 6175 tree eptype = NULL_TREE; 6176 tree ret; 6177 6178 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1); 6179 if (expr1_int_operands) 6180 expr1 = remove_c_maybe_const_expr (expr1); 6181 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2); 6182 if (expr2_int_operands) 6183 expr2 = remove_c_maybe_const_expr (expr2); 6184 6185 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR) 6186 expr1 = TREE_OPERAND (expr1, 0); 6187 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR) 6188 { 6189 eptype = TREE_TYPE (expr2); 6190 expr2 = TREE_OPERAND (expr2, 0); 6191 } 6192 6193 if (!TREE_SIDE_EFFECTS (expr1)) 6194 { 6195 /* The left-hand operand of a comma expression is like an expression 6196 statement: with -Wunused, we should warn if it doesn't have 6197 any side-effects, unless it was explicitly cast to (void). */ 6198 if (warn_unused_value) 6199 { 6200 if (VOID_TYPE_P (TREE_TYPE (expr1)) 6201 && CONVERT_EXPR_P (expr1)) 6202 ; /* (void) a, b */ 6203 else if (VOID_TYPE_P (TREE_TYPE (expr1)) 6204 && TREE_CODE (expr1) == COMPOUND_EXPR 6205 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1))) 6206 ; /* (void) a, (void) b, c */ 6207 else 6208 warning_at (loc, OPT_Wunused_value, 6209 "left-hand operand of comma expression has no effect"); 6210 } 6211 } 6212 else if (TREE_CODE (expr1) == COMPOUND_EXPR 6213 && warn_unused_value) 6214 { 6215 tree r = expr1; 6216 location_t cloc = loc; 6217 while (TREE_CODE (r) == COMPOUND_EXPR) 6218 { 6219 if (EXPR_HAS_LOCATION (r)) 6220 cloc = EXPR_LOCATION (r); 6221 r = TREE_OPERAND (r, 1); 6222 } 6223 if (!TREE_SIDE_EFFECTS (r) 6224 && !VOID_TYPE_P (TREE_TYPE (r)) 6225 && !CONVERT_EXPR_P (r)) 6226 warning_at (cloc, OPT_Wunused_value, 6227 "right-hand operand of comma expression has no effect"); 6228 } 6229 6230 /* With -Wunused, we should also warn if the left-hand operand does have 6231 side-effects, but computes a value which is not used. For example, in 6232 `foo() + bar(), baz()' the result of the `+' operator is not used, 6233 so we should issue a warning. */ 6234 else if (warn_unused_value) 6235 warn_if_unused_value (expr1, loc); 6236 6237 maybe_warn_nodiscard (loc, expr1); 6238 6239 if (expr2 == error_mark_node) 6240 return error_mark_node; 6241 6242 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2); 6243 6244 if (flag_isoc99 6245 && expr1_int_operands 6246 && expr2_int_operands) 6247 ret = note_integer_operands (ret); 6248 6249 if (eptype) 6250 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); 6251 6252 protected_set_expr_location (ret, loc); 6253 return ret; 6254 } 6255 6256 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to 6257 which we are casting. OTYPE is the type of the expression being 6258 cast. Both TYPE and OTYPE are pointer types. LOC is the location 6259 of the cast. -Wcast-qual appeared on the command line. Named 6260 address space qualifiers are not handled here, because they result 6261 in different warnings. */ 6262 6263 static void 6264 handle_warn_cast_qual (location_t loc, tree type, tree otype) 6265 { 6266 tree in_type = type; 6267 tree in_otype = otype; 6268 int added = 0; 6269 int discarded = 0; 6270 bool is_const; 6271 6272 /* Check that the qualifiers on IN_TYPE are a superset of the 6273 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE 6274 nodes is uninteresting and we stop as soon as we hit a 6275 non-POINTER_TYPE node on either type. */ 6276 do 6277 { 6278 in_otype = TREE_TYPE (in_otype); 6279 in_type = TREE_TYPE (in_type); 6280 6281 /* GNU C allows cv-qualified function types. 'const' means the 6282 function is very pure, 'volatile' means it can't return. We 6283 need to warn when such qualifiers are added, not when they're 6284 taken away. */ 6285 if (TREE_CODE (in_otype) == FUNCTION_TYPE 6286 && TREE_CODE (in_type) == FUNCTION_TYPE) 6287 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type) 6288 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype)); 6289 else 6290 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype) 6291 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type)); 6292 } 6293 while (TREE_CODE (in_type) == POINTER_TYPE 6294 && TREE_CODE (in_otype) == POINTER_TYPE); 6295 6296 if (added) 6297 warning_at (loc, OPT_Wcast_qual, 6298 "cast adds %q#v qualifier to function type", added); 6299 6300 if (discarded) 6301 /* There are qualifiers present in IN_OTYPE that are not present 6302 in IN_TYPE. */ 6303 warning_at (loc, OPT_Wcast_qual, 6304 "cast discards %qv qualifier from pointer target type", 6305 discarded); 6306 6307 if (added || discarded) 6308 return; 6309 6310 /* A cast from **T to const **T is unsafe, because it can cause a 6311 const value to be changed with no additional warning. We only 6312 issue this warning if T is the same on both sides, and we only 6313 issue the warning if there are the same number of pointers on 6314 both sides, as otherwise the cast is clearly unsafe anyhow. A 6315 cast is unsafe when a qualifier is added at one level and const 6316 is not present at all outer levels. 6317 6318 To issue this warning, we check at each level whether the cast 6319 adds new qualifiers not already seen. We don't need to special 6320 case function types, as they won't have the same 6321 TYPE_MAIN_VARIANT. */ 6322 6323 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype)) 6324 return; 6325 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE) 6326 return; 6327 6328 in_type = type; 6329 in_otype = otype; 6330 is_const = TYPE_READONLY (TREE_TYPE (in_type)); 6331 do 6332 { 6333 in_type = TREE_TYPE (in_type); 6334 in_otype = TREE_TYPE (in_otype); 6335 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0 6336 && !is_const) 6337 { 6338 warning_at (loc, OPT_Wcast_qual, 6339 "to be safe all intermediate pointers in cast from " 6340 "%qT to %qT must be %<const%> qualified", 6341 otype, type); 6342 break; 6343 } 6344 if (is_const) 6345 is_const = TYPE_READONLY (in_type); 6346 } 6347 while (TREE_CODE (in_type) == POINTER_TYPE); 6348 } 6349 6350 /* Heuristic check if two parameter types can be considered ABI-equivalent. */ 6351 6352 static bool 6353 c_safe_arg_type_equiv_p (tree t1, tree t2) 6354 { 6355 if (error_operand_p (t1) || error_operand_p (t2)) 6356 return true; 6357 6358 t1 = TYPE_MAIN_VARIANT (t1); 6359 t2 = TYPE_MAIN_VARIANT (t2); 6360 6361 if (TREE_CODE (t1) == POINTER_TYPE 6362 && TREE_CODE (t2) == POINTER_TYPE) 6363 return true; 6364 6365 /* The signedness of the parameter matters only when an integral 6366 type smaller than int is promoted to int, otherwise only the 6367 precision of the parameter matters. 6368 This check should make sure that the callee does not see 6369 undefined values in argument registers. */ 6370 if (INTEGRAL_TYPE_P (t1) 6371 && INTEGRAL_TYPE_P (t2) 6372 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2) 6373 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2) 6374 || !targetm.calls.promote_prototypes (NULL_TREE) 6375 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node))) 6376 return true; 6377 6378 return comptypes (t1, t2); 6379 } 6380 6381 /* Check if a type cast between two function types can be considered safe. */ 6382 6383 static bool 6384 c_safe_function_type_cast_p (tree t1, tree t2) 6385 { 6386 if (TREE_TYPE (t1) == void_type_node && 6387 TYPE_ARG_TYPES (t1) == void_list_node) 6388 return true; 6389 6390 if (TREE_TYPE (t2) == void_type_node && 6391 TYPE_ARG_TYPES (t2) == void_list_node) 6392 return true; 6393 6394 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2))) 6395 return false; 6396 6397 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2); 6398 t1 && t2; 6399 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) 6400 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2))) 6401 return false; 6402 6403 return true; 6404 } 6405 6406 /* Build an expression representing a cast to type TYPE of expression EXPR. 6407 LOC is the location of the cast-- typically the open paren of the cast. */ 6408 6409 tree 6410 build_c_cast (location_t loc, tree type, tree expr) 6411 { 6412 tree value; 6413 6414 bool int_operands = EXPR_INT_CONST_OPERANDS (expr); 6415 6416 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) 6417 expr = TREE_OPERAND (expr, 0); 6418 6419 value = expr; 6420 if (int_operands) 6421 value = remove_c_maybe_const_expr (value); 6422 6423 if (type == error_mark_node || expr == error_mark_node) 6424 return error_mark_node; 6425 6426 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing 6427 only in <protocol> qualifications. But when constructing cast expressions, 6428 the protocols do matter and must be kept around. */ 6429 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr))) 6430 return build1 (NOP_EXPR, type, expr); 6431 6432 type = TYPE_MAIN_VARIANT (type); 6433 6434 if (TREE_CODE (type) == ARRAY_TYPE) 6435 { 6436 error_at (loc, "cast specifies array type"); 6437 return error_mark_node; 6438 } 6439 6440 if (TREE_CODE (type) == FUNCTION_TYPE) 6441 { 6442 error_at (loc, "cast specifies function type"); 6443 return error_mark_node; 6444 } 6445 6446 if (!VOID_TYPE_P (type)) 6447 { 6448 value = require_complete_type (loc, value); 6449 if (value == error_mark_node) 6450 return error_mark_node; 6451 } 6452 6453 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value))) 6454 { 6455 if (RECORD_OR_UNION_TYPE_P (type) 6456 && pedwarn (loc, OPT_Wpedantic, 6457 "ISO C forbids casting nonscalar to the same type")) 6458 ; 6459 else if (warn_useless_cast) 6460 warning_at (loc, OPT_Wuseless_cast, 6461 "useless cast to type %qT", type); 6462 6463 /* Convert to remove any qualifiers from VALUE's type. */ 6464 value = convert (type, value); 6465 } 6466 else if (TREE_CODE (type) == UNION_TYPE) 6467 { 6468 tree field; 6469 6470 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 6471 if (TREE_TYPE (field) != error_mark_node 6472 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), 6473 TYPE_MAIN_VARIANT (TREE_TYPE (value)))) 6474 break; 6475 6476 if (field) 6477 { 6478 tree t; 6479 bool maybe_const = true; 6480 6481 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type"); 6482 t = c_fully_fold (value, false, &maybe_const); 6483 t = build_constructor_single (type, field, t); 6484 if (!maybe_const) 6485 t = c_wrap_maybe_const (t, true); 6486 t = digest_init (loc, type, t, 6487 NULL_TREE, false, false, false, true, false, false); 6488 TREE_CONSTANT (t) = TREE_CONSTANT (value); 6489 return t; 6490 } 6491 error_at (loc, "cast to union type from type not present in union"); 6492 return error_mark_node; 6493 } 6494 else 6495 { 6496 tree otype, ovalue; 6497 6498 if (type == void_type_node) 6499 { 6500 tree t = build1 (CONVERT_EXPR, type, value); 6501 SET_EXPR_LOCATION (t, loc); 6502 return t; 6503 } 6504 6505 otype = TREE_TYPE (value); 6506 6507 /* Optionally warn about potentially worrisome casts. */ 6508 if (warn_cast_qual 6509 && TREE_CODE (type) == POINTER_TYPE 6510 && TREE_CODE (otype) == POINTER_TYPE) 6511 handle_warn_cast_qual (loc, type, otype); 6512 6513 /* Warn about conversions between pointers to disjoint 6514 address spaces. */ 6515 if (TREE_CODE (type) == POINTER_TYPE 6516 && TREE_CODE (otype) == POINTER_TYPE 6517 && !null_pointer_constant_p (value)) 6518 { 6519 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type)); 6520 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype)); 6521 addr_space_t as_common; 6522 6523 if (!addr_space_superset (as_to, as_from, &as_common)) 6524 { 6525 if (ADDR_SPACE_GENERIC_P (as_from)) 6526 warning_at (loc, 0, "cast to %qs address space pointer " 6527 "from disjoint generic address space pointer", 6528 c_addr_space_name (as_to)); 6529 6530 else if (ADDR_SPACE_GENERIC_P (as_to)) 6531 warning_at (loc, 0, "cast to generic address space pointer " 6532 "from disjoint %qs address space pointer", 6533 c_addr_space_name (as_from)); 6534 6535 else 6536 warning_at (loc, 0, "cast to %qs address space pointer " 6537 "from disjoint %qs address space pointer", 6538 c_addr_space_name (as_to), 6539 c_addr_space_name (as_from)); 6540 } 6541 6542 /* Warn of new allocations that are not big enough for the target 6543 type. */ 6544 if (warn_alloc_size && TREE_CODE (value) == CALL_EXPR) 6545 if (tree fndecl = get_callee_fndecl (value)) 6546 if (DECL_IS_MALLOC (fndecl)) 6547 { 6548 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl)); 6549 tree alloc_size = lookup_attribute ("alloc_size", attrs); 6550 if (alloc_size) 6551 warn_for_alloc_size (loc, TREE_TYPE (type), value, 6552 alloc_size); 6553 } 6554 } 6555 6556 /* Warn about possible alignment problems. */ 6557 if ((STRICT_ALIGNMENT || warn_cast_align == 2) 6558 && TREE_CODE (type) == POINTER_TYPE 6559 && TREE_CODE (otype) == POINTER_TYPE 6560 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE 6561 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE 6562 /* Don't warn about opaque types, where the actual alignment 6563 restriction is unknown. */ 6564 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype)) 6565 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) 6566 && min_align_of_type (TREE_TYPE (type)) 6567 > min_align_of_type (TREE_TYPE (otype))) 6568 warning_at (loc, OPT_Wcast_align, 6569 "cast increases required alignment of target type"); 6570 6571 if ((TREE_CODE (type) == INTEGER_TYPE 6572 || TREE_CODE (type) == BITINT_TYPE) 6573 && TREE_CODE (otype) == POINTER_TYPE 6574 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)) 6575 /* Unlike conversion of integers to pointers, where the 6576 warning is disabled for converting constants because 6577 of cases such as SIG_*, warn about converting constant 6578 pointers to integers. In some cases it may cause unwanted 6579 sign extension, and a warning is appropriate. */ 6580 warning_at (loc, OPT_Wpointer_to_int_cast, 6581 "cast from pointer to integer of different size"); 6582 6583 if (TREE_CODE (value) == CALL_EXPR 6584 && TREE_CODE (type) != TREE_CODE (otype)) 6585 warning_at (loc, OPT_Wbad_function_cast, 6586 "cast from function call of type %qT " 6587 "to non-matching type %qT", otype, type); 6588 6589 if (TREE_CODE (type) == POINTER_TYPE 6590 && (TREE_CODE (otype) == INTEGER_TYPE 6591 || TREE_CODE (otype) == BITINT_TYPE) 6592 && TYPE_PRECISION (type) != TYPE_PRECISION (otype) 6593 /* Don't warn about converting any constant. */ 6594 && !TREE_CONSTANT (value)) 6595 warning_at (loc, 6596 OPT_Wint_to_pointer_cast, "cast to pointer from integer " 6597 "of different size"); 6598 6599 if (warn_strict_aliasing <= 2) 6600 strict_aliasing_warning (EXPR_LOCATION (value), type, expr); 6601 6602 /* If pedantic, warn for conversions between function and object 6603 pointer types, except for converting a null pointer constant 6604 to function pointer type. */ 6605 if (pedantic 6606 && TREE_CODE (type) == POINTER_TYPE 6607 && TREE_CODE (otype) == POINTER_TYPE 6608 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE 6609 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE) 6610 pedwarn (loc, OPT_Wpedantic, "ISO C forbids " 6611 "conversion of function pointer to object pointer type"); 6612 6613 if (pedantic 6614 && TREE_CODE (type) == POINTER_TYPE 6615 && TREE_CODE (otype) == POINTER_TYPE 6616 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE 6617 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE 6618 && !null_pointer_constant_p (value)) 6619 pedwarn (loc, OPT_Wpedantic, "ISO C forbids " 6620 "conversion of object pointer to function pointer type"); 6621 6622 if (TREE_CODE (type) == POINTER_TYPE 6623 && TREE_CODE (otype) == POINTER_TYPE 6624 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE 6625 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE 6626 && !c_safe_function_type_cast_p (TREE_TYPE (type), 6627 TREE_TYPE (otype))) 6628 warning_at (loc, OPT_Wcast_function_type, 6629 "cast between incompatible function types" 6630 " from %qT to %qT", otype, type); 6631 6632 ovalue = value; 6633 /* If converting to boolean a value with integer operands that 6634 is not itself represented as an INTEGER_CST, the call below 6635 to note_integer_operands may insert a C_MAYBE_CONST_EXPR, but 6636 build_binary_op as called by c_common_truthvalue_conversion 6637 may also insert a C_MAYBE_CONST_EXPR to indicate that a 6638 subexpression has been fully folded. To avoid nested 6639 C_MAYBE_CONST_EXPR, ensure that 6640 c_objc_common_truthvalue_conversion receives an argument 6641 properly marked as having integer operands in that case. */ 6642 if (int_operands 6643 && TREE_CODE (value) != INTEGER_CST 6644 && (TREE_CODE (type) == BOOLEAN_TYPE 6645 || (TREE_CODE (type) == ENUMERAL_TYPE 6646 && ENUM_UNDERLYING_TYPE (type) != NULL_TREE 6647 && TREE_CODE (ENUM_UNDERLYING_TYPE (type)) == BOOLEAN_TYPE))) 6648 value = note_integer_operands (value); 6649 value = convert (type, value); 6650 6651 /* Ignore any integer overflow caused by the cast. */ 6652 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype)) 6653 { 6654 if (TREE_OVERFLOW_P (ovalue)) 6655 { 6656 if (!TREE_OVERFLOW (value)) 6657 { 6658 /* Avoid clobbering a shared constant. */ 6659 value = copy_node (value); 6660 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); 6661 } 6662 } 6663 else if (TREE_OVERFLOW (value)) 6664 /* Reset VALUE's overflow flags, ensuring constant sharing. */ 6665 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value)); 6666 } 6667 } 6668 6669 /* Don't let a cast be an lvalue. */ 6670 if (lvalue_p (value)) 6671 value = non_lvalue_loc (loc, value); 6672 6673 /* Don't allow the results of casting to floating-point or complex 6674 types be confused with actual constants, or casts involving 6675 integer and pointer types other than direct integer-to-integer 6676 and integer-to-pointer be confused with integer constant 6677 expressions and null pointer constants. */ 6678 if (TREE_CODE (value) == REAL_CST 6679 || TREE_CODE (value) == COMPLEX_CST 6680 || (TREE_CODE (value) == INTEGER_CST 6681 && !((TREE_CODE (expr) == INTEGER_CST 6682 && INTEGRAL_TYPE_P (TREE_TYPE (expr))) 6683 || TREE_CODE (expr) == REAL_CST 6684 || TREE_CODE (expr) == COMPLEX_CST))) 6685 value = build1 (NOP_EXPR, type, value); 6686 6687 /* If the expression has integer operands and so can occur in an 6688 unevaluated part of an integer constant expression, ensure the 6689 return value reflects this. */ 6690 if (int_operands 6691 && INTEGRAL_TYPE_P (type) 6692 && value != error_mark_node 6693 && !EXPR_INT_CONST_OPERANDS (value)) 6694 value = note_integer_operands (value); 6695 6696 protected_set_expr_location (value, loc); 6697 return value; 6698 } 6699 6700 /* Interpret a cast of expression EXPR to type TYPE. LOC is the 6701 location of the open paren of the cast, or the position of the cast 6702 expr. */ 6703 tree 6704 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr) 6705 { 6706 tree type; 6707 tree type_expr = NULL_TREE; 6708 bool type_expr_const = true; 6709 tree ret; 6710 int saved_wsp = warn_strict_prototypes; 6711 6712 /* This avoids warnings about unprototyped casts on 6713 integers. E.g. "#define SIG_DFL (void(*)())0". */ 6714 if (TREE_CODE (expr) == INTEGER_CST) 6715 warn_strict_prototypes = 0; 6716 type = groktypename (type_name, &type_expr, &type_expr_const); 6717 warn_strict_prototypes = saved_wsp; 6718 6719 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type) 6720 && reject_gcc_builtin (expr)) 6721 return error_mark_node; 6722 6723 ret = build_c_cast (loc, type, expr); 6724 if (type_expr) 6725 { 6726 bool inner_expr_const = true; 6727 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const); 6728 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret); 6729 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const 6730 && inner_expr_const); 6731 SET_EXPR_LOCATION (ret, loc); 6732 } 6733 6734 if (!EXPR_HAS_LOCATION (ret)) 6735 protected_set_expr_location (ret, loc); 6736 6737 /* C++ does not permits types to be defined in a cast, but it 6738 allows references to incomplete types. */ 6739 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef) 6740 warning_at (loc, OPT_Wc___compat, 6741 "defining a type in a cast is invalid in C++"); 6742 6743 return ret; 6744 } 6745 6746 /* Build an assignment expression of lvalue LHS from value RHS. 6748 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which 6749 may differ from TREE_TYPE (LHS) for an enum bitfield. 6750 MODIFYCODE is the code for a binary operator that we use 6751 to combine the old value of LHS with RHS to get the new value. 6752 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. 6753 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS, 6754 which may differ from TREE_TYPE (RHS) for an enum value. 6755 6756 LOCATION is the location of the MODIFYCODE operator. 6757 RHS_LOC is the location of the RHS. */ 6758 6759 tree 6760 build_modify_expr (location_t location, tree lhs, tree lhs_origtype, 6761 enum tree_code modifycode, 6762 location_t rhs_loc, tree rhs, tree rhs_origtype) 6763 { 6764 tree result; 6765 tree newrhs; 6766 tree rhseval = NULL_TREE; 6767 tree lhstype = TREE_TYPE (lhs); 6768 tree olhstype = lhstype; 6769 bool npc; 6770 bool is_atomic_op; 6771 6772 /* Types that aren't fully specified cannot be used in assignments. */ 6773 lhs = require_complete_type (location, lhs); 6774 6775 /* Avoid duplicate error messages from operands that had errors. */ 6776 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) 6777 return error_mark_node; 6778 6779 /* Ensure an error for assigning a non-lvalue array to an array in 6780 C90. */ 6781 if (TREE_CODE (lhstype) == ARRAY_TYPE) 6782 { 6783 error_at (location, "assignment to expression with array type"); 6784 return error_mark_node; 6785 } 6786 6787 /* For ObjC properties, defer this check. */ 6788 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign)) 6789 return error_mark_node; 6790 6791 is_atomic_op = really_atomic_lvalue (lhs); 6792 6793 newrhs = rhs; 6794 6795 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) 6796 { 6797 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs), 6798 lhs_origtype, modifycode, rhs_loc, rhs, 6799 rhs_origtype); 6800 if (inner == error_mark_node) 6801 return error_mark_node; 6802 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), 6803 C_MAYBE_CONST_EXPR_PRE (lhs), inner); 6804 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs)); 6805 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1; 6806 protected_set_expr_location (result, location); 6807 return result; 6808 } 6809 6810 /* If a binary op has been requested, combine the old LHS value with the RHS 6811 producing the value we should actually store into the LHS. */ 6812 6813 if (modifycode != NOP_EXPR) 6814 { 6815 lhs = c_fully_fold (lhs, false, NULL, true); 6816 lhs = stabilize_reference (lhs); 6817 6818 /* Construct the RHS for any non-atomic compound assignemnt. */ 6819 if (!is_atomic_op) 6820 { 6821 /* If in LHS op= RHS the RHS has side-effects, ensure they 6822 are preevaluated before the rest of the assignment expression's 6823 side-effects, because RHS could contain e.g. function calls 6824 that modify LHS. */ 6825 if (TREE_SIDE_EFFECTS (rhs)) 6826 { 6827 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) 6828 newrhs = save_expr (TREE_OPERAND (rhs, 0)); 6829 else 6830 newrhs = save_expr (rhs); 6831 rhseval = newrhs; 6832 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) 6833 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs), 6834 newrhs); 6835 } 6836 newrhs = build_binary_op (location, 6837 modifycode, lhs, newrhs, true); 6838 6839 /* The original type of the right hand side is no longer 6840 meaningful. */ 6841 rhs_origtype = NULL_TREE; 6842 } 6843 } 6844 6845 if (c_dialect_objc ()) 6846 { 6847 /* Check if we are modifying an Objective-C property reference; 6848 if so, we need to generate setter calls. */ 6849 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR) 6850 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0)); 6851 else 6852 result = objc_maybe_build_modify_expr (lhs, newrhs); 6853 if (result) 6854 goto return_result; 6855 6856 /* Else, do the check that we postponed for Objective-C. */ 6857 if (!lvalue_or_else (location, lhs, lv_assign)) 6858 return error_mark_node; 6859 } 6860 6861 /* Give an error for storing in something that is 'const'. */ 6862 6863 if (TYPE_READONLY (lhstype) 6864 || (RECORD_OR_UNION_TYPE_P (lhstype) 6865 && C_TYPE_FIELDS_READONLY (lhstype))) 6866 { 6867 readonly_error (location, lhs, lv_assign); 6868 return error_mark_node; 6869 } 6870 else if (TREE_READONLY (lhs)) 6871 readonly_warning (lhs, lv_assign); 6872 6873 /* If storing into a structure or union member, 6874 it has probably been given type `int'. 6875 Compute the type that would go with 6876 the actual amount of storage the member occupies. */ 6877 6878 if (TREE_CODE (lhs) == COMPONENT_REF 6879 && (TREE_CODE (lhstype) == INTEGER_TYPE 6880 || TREE_CODE (lhstype) == BOOLEAN_TYPE 6881 || SCALAR_FLOAT_TYPE_P (lhstype) 6882 || TREE_CODE (lhstype) == ENUMERAL_TYPE)) 6883 lhstype = TREE_TYPE (get_unwidened (lhs, 0)); 6884 6885 /* If storing in a field that is in actuality a short or narrower than one, 6886 we must store in the field in its actual type. */ 6887 6888 if (lhstype != TREE_TYPE (lhs)) 6889 { 6890 lhs = copy_node (lhs); 6891 TREE_TYPE (lhs) = lhstype; 6892 } 6893 6894 /* Issue -Wc++-compat warnings about an assignment to an enum type 6895 when LHS does not have its original type. This happens for, 6896 e.g., an enum bitfield in a struct. */ 6897 if (warn_cxx_compat 6898 && lhs_origtype != NULL_TREE 6899 && lhs_origtype != lhstype 6900 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE) 6901 { 6902 tree checktype = (rhs_origtype != NULL_TREE 6903 ? rhs_origtype 6904 : TREE_TYPE (rhs)); 6905 if (checktype != error_mark_node 6906 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype) 6907 || (is_atomic_op && modifycode != NOP_EXPR))) 6908 warning_at (location, OPT_Wc___compat, 6909 "enum conversion in assignment is invalid in C++"); 6910 } 6911 6912 /* Remove qualifiers. */ 6913 lhstype = c_build_qualified_type (lhstype, TYPE_UNQUALIFIED); 6914 olhstype = c_build_qualified_type (olhstype, TYPE_UNQUALIFIED); 6915 6916 /* Convert new value to destination type. Fold it first, then 6917 restore any excess precision information, for the sake of 6918 conversion warnings. */ 6919 6920 if (!(is_atomic_op && modifycode != NOP_EXPR)) 6921 { 6922 tree rhs_semantic_type = NULL_TREE; 6923 if (!c_in_omp_for) 6924 { 6925 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR) 6926 { 6927 rhs_semantic_type = TREE_TYPE (newrhs); 6928 newrhs = TREE_OPERAND (newrhs, 0); 6929 } 6930 npc = null_pointer_constant_p (newrhs); 6931 newrhs = c_fully_fold (newrhs, false, NULL); 6932 if (rhs_semantic_type) 6933 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs); 6934 } 6935 else 6936 npc = null_pointer_constant_p (newrhs); 6937 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs, 6938 rhs_origtype, ic_assign, npc, 6939 NULL_TREE, NULL_TREE, 0); 6940 if (TREE_CODE (newrhs) == ERROR_MARK) 6941 return error_mark_node; 6942 } 6943 6944 /* Emit ObjC write barrier, if necessary. */ 6945 if (c_dialect_objc () && flag_objc_gc) 6946 { 6947 result = objc_generate_write_barrier (lhs, modifycode, newrhs); 6948 if (result) 6949 { 6950 protected_set_expr_location (result, location); 6951 goto return_result; 6952 } 6953 } 6954 6955 /* Scan operands. */ 6956 6957 if (is_atomic_op) 6958 result = build_atomic_assign (location, lhs, modifycode, newrhs, false); 6959 else 6960 { 6961 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs); 6962 TREE_SIDE_EFFECTS (result) = 1; 6963 protected_set_expr_location (result, location); 6964 } 6965 6966 /* If we got the LHS in a different type for storing in, 6967 convert the result back to the nominal type of LHS 6968 so that the value we return always has the same type 6969 as the LHS argument. */ 6970 6971 if (olhstype == TREE_TYPE (result)) 6972 goto return_result; 6973 6974 result = convert_for_assignment (location, rhs_loc, olhstype, result, 6975 rhs_origtype, ic_assign, false, NULL_TREE, 6976 NULL_TREE, 0); 6977 protected_set_expr_location (result, location); 6978 6979 return_result: 6980 if (rhseval) 6981 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result); 6982 return result; 6983 } 6984 6985 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE. 6987 This is used to implement -fplan9-extensions. */ 6988 6989 static bool 6990 find_anonymous_field_with_type (tree struct_type, tree type) 6991 { 6992 tree field; 6993 bool found; 6994 6995 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type)); 6996 found = false; 6997 for (field = TYPE_FIELDS (struct_type); 6998 field != NULL_TREE; 6999 field = TREE_CHAIN (field)) 7000 { 7001 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field)) 7002 ? c_build_qualified_type (TREE_TYPE (field), 7003 TYPE_QUAL_ATOMIC) 7004 : TYPE_MAIN_VARIANT (TREE_TYPE (field))); 7005 if (DECL_NAME (field) == NULL 7006 && comptypes (type, fieldtype)) 7007 { 7008 if (found) 7009 return false; 7010 found = true; 7011 } 7012 else if (DECL_NAME (field) == NULL 7013 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)) 7014 && find_anonymous_field_with_type (TREE_TYPE (field), type)) 7015 { 7016 if (found) 7017 return false; 7018 found = true; 7019 } 7020 } 7021 return found; 7022 } 7023 7024 /* RHS is an expression whose type is pointer to struct. If there is 7025 an anonymous field in RHS with type TYPE, then return a pointer to 7026 that field in RHS. This is used with -fplan9-extensions. This 7027 returns NULL if no conversion could be found. */ 7028 7029 static tree 7030 convert_to_anonymous_field (location_t location, tree type, tree rhs) 7031 { 7032 tree rhs_struct_type, lhs_main_type; 7033 tree field, found_field; 7034 bool found_sub_field; 7035 tree ret; 7036 7037 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs))); 7038 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs)); 7039 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type)); 7040 7041 gcc_assert (POINTER_TYPE_P (type)); 7042 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type)) 7043 ? c_build_qualified_type (TREE_TYPE (type), 7044 TYPE_QUAL_ATOMIC) 7045 : TYPE_MAIN_VARIANT (TREE_TYPE (type))); 7046 7047 found_field = NULL_TREE; 7048 found_sub_field = false; 7049 for (field = TYPE_FIELDS (rhs_struct_type); 7050 field != NULL_TREE; 7051 field = TREE_CHAIN (field)) 7052 { 7053 if (DECL_NAME (field) != NULL_TREE 7054 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))) 7055 continue; 7056 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field)) 7057 ? c_build_qualified_type (TREE_TYPE (field), 7058 TYPE_QUAL_ATOMIC) 7059 : TYPE_MAIN_VARIANT (TREE_TYPE (field))); 7060 if (comptypes (lhs_main_type, fieldtype)) 7061 { 7062 if (found_field != NULL_TREE) 7063 return NULL_TREE; 7064 found_field = field; 7065 } 7066 else if (find_anonymous_field_with_type (TREE_TYPE (field), 7067 lhs_main_type)) 7068 { 7069 if (found_field != NULL_TREE) 7070 return NULL_TREE; 7071 found_field = field; 7072 found_sub_field = true; 7073 } 7074 } 7075 7076 if (found_field == NULL_TREE) 7077 return NULL_TREE; 7078 7079 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field), 7080 build_fold_indirect_ref (rhs), found_field, 7081 NULL_TREE); 7082 ret = build_fold_addr_expr_loc (location, ret); 7083 7084 if (found_sub_field) 7085 { 7086 ret = convert_to_anonymous_field (location, type, ret); 7087 gcc_assert (ret != NULL_TREE); 7088 } 7089 7090 return ret; 7091 } 7092 7093 /* Issue an error message for a bad initializer component. 7094 GMSGID identifies the message. 7095 The component name is taken from the spelling stack. */ 7096 7097 static void ATTRIBUTE_GCC_DIAG (2,0) 7098 error_init (location_t loc, const char *gmsgid, ...) 7099 { 7100 char *ofwhat; 7101 7102 auto_diagnostic_group d; 7103 7104 /* The gmsgid may be a format string with %< and %>. */ 7105 va_list ap; 7106 va_start (ap, gmsgid); 7107 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap); 7108 va_end (ap); 7109 7110 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); 7111 if (*ofwhat && warned) 7112 inform (loc, "(near initialization for %qs)", ofwhat); 7113 } 7114 7115 /* Used to implement pedwarn_init and permerror_init. */ 7116 7117 static void ATTRIBUTE_GCC_DIAG (3,0) 7118 pedwarn_permerror_init (location_t loc, int opt, const char *gmsgid, 7119 va_list *ap, diagnostic_t kind) 7120 { 7121 /* Use the location where a macro was expanded rather than where 7122 it was defined to make sure macros defined in system headers 7123 but used incorrectly elsewhere are diagnosed. */ 7124 location_t exploc = expansion_point_location_if_in_system_header (loc); 7125 auto_diagnostic_group d; 7126 bool warned = emit_diagnostic_valist (kind, exploc, opt, gmsgid, ap); 7127 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); 7128 if (*ofwhat && warned) 7129 inform (exploc, "(near initialization for %qs)", ofwhat); 7130 } 7131 7132 /* Issue a pedantic warning for a bad initializer component. OPT is 7133 the option OPT_* (from options.h) controlling this warning or 0 if 7134 it is unconditionally given. GMSGID identifies the message. The 7135 component name is taken from the spelling stack. */ 7136 7137 static void ATTRIBUTE_GCC_DIAG (3,0) 7138 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...) 7139 { 7140 va_list ap; 7141 va_start (ap, gmsgid); 7142 pedwarn_permerror_init (loc, opt, gmsgid, &ap, DK_PEDWARN); 7143 va_end (ap); 7144 } 7145 7146 /* Like pedwarn_init, but issue a permerror. */ 7147 7148 static void ATTRIBUTE_GCC_DIAG (3,0) 7149 permerror_init (location_t loc, int opt, const char *gmsgid, ...) 7150 { 7151 va_list ap; 7152 va_start (ap, gmsgid); 7153 pedwarn_permerror_init (loc, opt, gmsgid, &ap, DK_PERMERROR); 7154 va_end (ap); 7155 } 7156 7157 /* Issue a warning for a bad initializer component. 7158 7159 OPT is the OPT_W* value corresponding to the warning option that 7160 controls this warning. GMSGID identifies the message. The 7161 component name is taken from the spelling stack. */ 7162 7163 static void 7164 warning_init (location_t loc, int opt, const char *gmsgid) 7165 { 7166 char *ofwhat; 7167 bool warned; 7168 7169 auto_diagnostic_group d; 7170 7171 /* Use the location where a macro was expanded rather than where 7172 it was defined to make sure macros defined in system headers 7173 but used incorrectly elsewhere are diagnosed. */ 7174 location_t exploc = expansion_point_location_if_in_system_header (loc); 7175 7176 /* The gmsgid may be a format string with %< and %>. */ 7177 warned = warning_at (exploc, opt, gmsgid); 7178 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); 7179 if (*ofwhat && warned) 7180 inform (exploc, "(near initialization for %qs)", ofwhat); 7181 } 7182 7183 /* If TYPE is an array type and EXPR is a parenthesized string 7185 constant, warn if pedantic that EXPR is being used to initialize an 7186 object of type TYPE. */ 7187 7188 void 7189 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr) 7190 { 7191 if (pedantic 7192 && TREE_CODE (type) == ARRAY_TYPE 7193 && TREE_CODE (expr.value) == STRING_CST 7194 && expr.original_code != STRING_CST) 7195 pedwarn_init (loc, OPT_Wpedantic, 7196 "array initialized from parenthesized string constant"); 7197 } 7198 7199 /* Attempt to locate the parameter with the given index within FNDECL, 7200 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */ 7201 7202 static location_t 7203 get_fndecl_argument_location (tree fndecl, int argnum) 7204 { 7205 int i; 7206 tree param; 7207 7208 /* Locate param by index within DECL_ARGUMENTS (fndecl). */ 7209 for (i = 0, param = DECL_ARGUMENTS (fndecl); 7210 i < argnum && param; 7211 i++, param = TREE_CHAIN (param)) 7212 ; 7213 7214 /* If something went wrong (e.g. if we have a builtin and thus no arguments), 7215 return DECL_SOURCE_LOCATION (FNDECL). */ 7216 if (param == NULL) 7217 return DECL_SOURCE_LOCATION (fndecl); 7218 7219 return DECL_SOURCE_LOCATION (param); 7220 } 7221 7222 /* Issue a note about a mismatching argument for parameter PARMNUM 7223 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE. 7224 Attempt to issue the note at the pertinent parameter of the decl; 7225 failing that issue it at the location of FUNDECL; failing that 7226 issue it at PLOC. */ 7227 7228 static void 7229 inform_for_arg (tree fundecl, location_t ploc, int parmnum, 7230 tree expected_type, tree actual_type) 7231 { 7232 location_t loc; 7233 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl)) 7234 loc = get_fndecl_argument_location (fundecl, parmnum - 1); 7235 else 7236 loc = ploc; 7237 7238 inform (loc, 7239 "expected %qT but argument is of type %qT", 7240 expected_type, actual_type); 7241 } 7242 7243 /* Issue a warning when an argument of ARGTYPE is passed to a built-in 7244 function FUNDECL declared without prototype to parameter PARMNUM of 7245 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */ 7246 7247 static void 7248 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum, 7249 tree parmtype, tree argtype) 7250 { 7251 tree_code parmcode = TREE_CODE (parmtype); 7252 tree_code argcode = TREE_CODE (argtype); 7253 tree promoted = c_type_promotes_to (argtype); 7254 7255 /* Avoid warning for enum arguments that promote to an integer type 7256 of the same size/mode. */ 7257 if (parmcode == INTEGER_TYPE 7258 && argcode == ENUMERAL_TYPE 7259 && TYPE_MODE (parmtype) == TYPE_MODE (argtype)) 7260 return; 7261 7262 if ((parmcode == argcode 7263 || (parmcode == INTEGER_TYPE 7264 && argcode == ENUMERAL_TYPE)) 7265 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted)) 7266 return; 7267 7268 /* This diagnoses even signed/unsigned mismatches. Those might be 7269 safe in many cases but GCC may emit suboptimal code for them so 7270 warning on those cases drives efficiency improvements. */ 7271 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch, 7272 TYPE_MAIN_VARIANT (promoted) == argtype 7273 ? G_("%qD argument %d type is %qT where %qT is expected " 7274 "in a call to built-in function declared without " 7275 "prototype") 7276 : G_("%qD argument %d promotes to %qT where %qT is expected " 7277 "in a call to built-in function declared without " 7278 "prototype"), 7279 fundecl, parmnum, promoted, parmtype)) 7280 inform (DECL_SOURCE_LOCATION (fundecl), 7281 "built-in %qD declared here", 7282 fundecl); 7283 } 7284 7285 /* Convert value RHS to type TYPE as preparation for an assignment to 7286 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the 7287 original type of RHS; this differs from TREE_TYPE (RHS) for enum 7288 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer 7289 constant before any folding. 7290 The real work of conversion is done by `convert'. 7291 The purpose of this function is to generate error messages 7292 for assignments that are not allowed in C. 7293 ERRTYPE says whether it is argument passing, assignment, 7294 initialization or return. 7295 7296 In the following example, '~' denotes where EXPR_LOC and '^' where 7297 LOCATION point to: 7298 7299 f (var); [ic_argpass] 7300 ^ ~~~ 7301 x = var; [ic_assign] 7302 ^ ~~~; 7303 int x = var; [ic_init] 7304 ^^^ 7305 return x; [ic_return] 7306 ^ 7307 7308 FUNCTION is a tree for the function being called. 7309 PARMNUM is the number of the argument, for printing in error messages. 7310 WARNOPT may be set to a warning option to issue the corresponding warning 7311 rather than an error for invalid conversions. Used for calls to built-in 7312 functions declared without a prototype. */ 7313 7314 static tree 7315 convert_for_assignment (location_t location, location_t expr_loc, tree type, 7316 tree rhs, tree origtype, enum impl_conv errtype, 7317 bool null_pointer_constant, tree fundecl, 7318 tree function, int parmnum, int warnopt /* = 0 */) 7319 { 7320 enum tree_code codel = TREE_CODE (type); 7321 tree orig_rhs = rhs; 7322 tree rhstype; 7323 enum tree_code coder; 7324 tree rname = NULL_TREE; 7325 bool objc_ok = false; 7326 7327 /* Use the expansion point location to handle cases such as user's 7328 function returning a wrong-type macro defined in a system header. */ 7329 location = expansion_point_location_if_in_system_header (location); 7330 7331 if (errtype == ic_argpass) 7332 { 7333 tree selector; 7334 /* Change pointer to function to the function itself for 7335 diagnostics. */ 7336 if (TREE_CODE (function) == ADDR_EXPR 7337 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) 7338 function = TREE_OPERAND (function, 0); 7339 7340 /* Handle an ObjC selector specially for diagnostics. */ 7341 selector = objc_message_selector (); 7342 rname = function; 7343 if (selector && parmnum > 2) 7344 { 7345 rname = selector; 7346 parmnum -= 2; 7347 } 7348 } 7349 7350 /* This macro is used to emit diagnostics to ensure that all format 7351 strings are complete sentences, visible to gettext and checked at 7352 compile time. */ 7353 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \ 7354 do { \ 7355 switch (errtype) \ 7356 { \ 7357 case ic_argpass: \ 7358 { \ 7359 auto_diagnostic_group d; \ 7360 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \ 7361 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \ 7362 } \ 7363 break; \ 7364 case ic_assign: \ 7365 pedwarn (LOCATION, OPT, AS); \ 7366 break; \ 7367 case ic_init: \ 7368 case ic_init_const: \ 7369 pedwarn_init (LOCATION, OPT, IN); \ 7370 break; \ 7371 case ic_return: \ 7372 pedwarn (LOCATION, OPT, RE); \ 7373 break; \ 7374 default: \ 7375 gcc_unreachable (); \ 7376 } \ 7377 } while (0) 7378 7379 /* This macro is used to emit diagnostics to ensure that all format 7380 strings are complete sentences, visible to gettext and checked at 7381 compile time. It can be called with 'pedwarn' or 'warning_at'. */ 7382 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \ 7383 do { \ 7384 switch (errtype) \ 7385 { \ 7386 case ic_argpass: \ 7387 { \ 7388 auto_diagnostic_group d; \ 7389 if (PEDWARN) { \ 7390 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \ 7391 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \ 7392 } else { \ 7393 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \ 7394 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \ 7395 } \ 7396 } \ 7397 break; \ 7398 case ic_assign: \ 7399 if (PEDWARN) \ 7400 pedwarn (LOCATION, OPT, AS, QUALS); \ 7401 else \ 7402 warning_at (LOCATION, OPT, AS, QUALS); \ 7403 break; \ 7404 case ic_init: \ 7405 case ic_init_const: \ 7406 if (PEDWARN) \ 7407 pedwarn (LOCATION, OPT, IN, QUALS); \ 7408 else \ 7409 warning_at (LOCATION, OPT, IN, QUALS); \ 7410 break; \ 7411 case ic_return: \ 7412 if (PEDWARN) \ 7413 pedwarn (LOCATION, OPT, RE, QUALS); \ 7414 else \ 7415 warning_at (LOCATION, OPT, RE, QUALS); \ 7416 break; \ 7417 default: \ 7418 gcc_unreachable (); \ 7419 } \ 7420 } while (0) 7421 7422 /* This macro is used to emit diagnostics to ensure that all format 7423 strings are complete sentences, visible to gettext and checked at 7424 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an 7425 extra parameter to enumerate qualifiers. */ 7426 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \ 7427 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) 7428 7429 7430 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) 7431 rhs = TREE_OPERAND (rhs, 0); 7432 7433 rhstype = TREE_TYPE (rhs); 7434 coder = TREE_CODE (rhstype); 7435 7436 if (coder == ERROR_MARK) 7437 return error_mark_node; 7438 7439 if (c_dialect_objc ()) 7440 { 7441 int parmno; 7442 7443 switch (errtype) 7444 { 7445 case ic_return: 7446 parmno = 0; 7447 break; 7448 7449 case ic_assign: 7450 parmno = -1; 7451 break; 7452 7453 case ic_init: 7454 case ic_init_const: 7455 parmno = -2; 7456 break; 7457 7458 default: 7459 parmno = parmnum; 7460 break; 7461 } 7462 7463 objc_ok = objc_compare_types (type, rhstype, parmno, rname); 7464 } 7465 7466 if (warn_cxx_compat) 7467 { 7468 tree checktype = origtype != NULL_TREE ? origtype : rhstype; 7469 if (checktype != error_mark_node 7470 && TREE_CODE (type) == ENUMERAL_TYPE 7471 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type)) 7472 switch (errtype) 7473 { 7474 case ic_argpass: 7475 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when " 7476 "passing argument %d of %qE is invalid in C++", 7477 parmnum, rname)) 7478 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl)) 7479 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc, 7480 "expected %qT but argument is of type %qT", 7481 type, rhstype); 7482 break; 7483 case ic_assign: 7484 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to " 7485 "%qT in assignment is invalid in C++", rhstype, type); 7486 break; 7487 case ic_init: 7488 case ic_init_const: 7489 pedwarn_init (location, OPT_Wc___compat, "enum conversion from " 7490 "%qT to %qT in initialization is invalid in C++", 7491 rhstype, type); 7492 break; 7493 case ic_return: 7494 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to " 7495 "%qT in return is invalid in C++", rhstype, type); 7496 break; 7497 default: 7498 gcc_unreachable (); 7499 } 7500 } 7501 7502 if (warn_enum_conversion) 7503 { 7504 tree checktype = origtype != NULL_TREE ? origtype : rhstype; 7505 if (checktype != error_mark_node 7506 && TREE_CODE (checktype) == ENUMERAL_TYPE 7507 && TREE_CODE (type) == ENUMERAL_TYPE 7508 && !comptypes (TYPE_MAIN_VARIANT (checktype), TYPE_MAIN_VARIANT (type))) 7509 { 7510 gcc_rich_location loc (location); 7511 warning_at (&loc, OPT_Wenum_conversion, 7512 "implicit conversion from %qT to %qT", 7513 checktype, type); 7514 } 7515 } 7516 7517 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) 7518 { 7519 warn_for_address_of_packed_member (type, orig_rhs); 7520 return rhs; 7521 } 7522 7523 if (coder == VOID_TYPE) 7524 { 7525 /* Except for passing an argument to an unprototyped function, 7526 this is a constraint violation. When passing an argument to 7527 an unprototyped function, it is compile-time undefined; 7528 making it a constraint in that case was rejected in 7529 DR#252. */ 7530 const char msg[] = "void value not ignored as it ought to be"; 7531 if (warnopt) 7532 warning_at (location, warnopt, msg); 7533 else 7534 error_at (location, msg); 7535 return error_mark_node; 7536 } 7537 rhs = require_complete_type (location, rhs); 7538 if (rhs == error_mark_node) 7539 return error_mark_node; 7540 7541 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs)) 7542 return error_mark_node; 7543 7544 /* A non-reference type can convert to a reference. This handles 7545 va_start, va_copy and possibly port built-ins. */ 7546 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE) 7547 { 7548 if (!lvalue_p (rhs)) 7549 { 7550 const char msg[] = "cannot pass rvalue to reference parameter"; 7551 if (warnopt) 7552 warning_at (location, warnopt, msg); 7553 else 7554 error_at (location, msg); 7555 return error_mark_node; 7556 } 7557 if (!c_mark_addressable (rhs)) 7558 return error_mark_node; 7559 rhs = build1 (ADDR_EXPR, c_build_pointer_type (TREE_TYPE (rhs)), rhs); 7560 SET_EXPR_LOCATION (rhs, location); 7561 7562 rhs = convert_for_assignment (location, expr_loc, 7563 c_build_pointer_type (TREE_TYPE (type)), 7564 rhs, origtype, errtype, 7565 null_pointer_constant, fundecl, function, 7566 parmnum, warnopt); 7567 if (rhs == error_mark_node) 7568 return error_mark_node; 7569 7570 rhs = build1 (NOP_EXPR, type, rhs); 7571 SET_EXPR_LOCATION (rhs, location); 7572 return rhs; 7573 } 7574 /* Some types can interconvert without explicit casts. */ 7575 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE 7576 && vector_types_convertible_p (type, TREE_TYPE (rhs), true)) 7577 return convert (type, rhs); 7578 /* Arithmetic types all interconvert, and enum is treated like int. */ 7579 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE 7580 || codel == FIXED_POINT_TYPE 7581 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE 7582 || codel == BOOLEAN_TYPE || codel == BITINT_TYPE) 7583 && (coder == INTEGER_TYPE || coder == REAL_TYPE 7584 || coder == FIXED_POINT_TYPE 7585 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE 7586 || coder == BOOLEAN_TYPE || coder == BITINT_TYPE)) 7587 { 7588 if (warnopt && errtype == ic_argpass) 7589 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type, 7590 rhstype); 7591 7592 bool save = in_late_binary_op; 7593 if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE 7594 || (coder == REAL_TYPE 7595 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE) 7596 && sanitize_flags_p (SANITIZE_FLOAT_CAST))) 7597 in_late_binary_op = true; 7598 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION 7599 ? expr_loc : location, type, orig_rhs, 7600 errtype == ic_init_const); 7601 in_late_binary_op = save; 7602 return ret; 7603 } 7604 7605 /* Aggregates in different TUs might need conversion. */ 7606 if ((codel == RECORD_TYPE || codel == UNION_TYPE) 7607 && codel == coder 7608 && comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (rhstype))) 7609 return convert_and_check (expr_loc != UNKNOWN_LOCATION 7610 ? expr_loc : location, type, rhs); 7611 7612 /* Conversion to a transparent union or record from its member types. 7613 This applies only to function arguments. */ 7614 if (((codel == UNION_TYPE || codel == RECORD_TYPE) 7615 && TYPE_TRANSPARENT_AGGR (type)) 7616 && errtype == ic_argpass) 7617 { 7618 tree memb, marginal_memb = NULL_TREE; 7619 7620 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb)) 7621 { 7622 tree memb_type = TREE_TYPE (memb); 7623 7624 if (comptypes (TYPE_MAIN_VARIANT (memb_type), 7625 TYPE_MAIN_VARIANT (rhstype))) 7626 break; 7627 7628 if (TREE_CODE (memb_type) != POINTER_TYPE) 7629 continue; 7630 7631 if (coder == POINTER_TYPE) 7632 { 7633 tree ttl = TREE_TYPE (memb_type); 7634 tree ttr = TREE_TYPE (rhstype); 7635 7636 /* Any non-function converts to a [const][volatile] void * 7637 and vice versa; otherwise, targets must be the same. 7638 Meanwhile, the lhs target must have all the qualifiers of 7639 the rhs. */ 7640 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl)) 7641 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr)) 7642 || comp_target_types (location, memb_type, rhstype)) 7643 { 7644 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC; 7645 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC; 7646 /* If this type won't generate any warnings, use it. */ 7647 if (lquals == rquals 7648 || ((TREE_CODE (ttr) == FUNCTION_TYPE 7649 && TREE_CODE (ttl) == FUNCTION_TYPE) 7650 ? ((lquals | rquals) == rquals) 7651 : ((lquals | rquals) == lquals))) 7652 break; 7653 7654 /* Keep looking for a better type, but remember this one. */ 7655 if (!marginal_memb) 7656 marginal_memb = memb; 7657 } 7658 } 7659 7660 /* Can convert integer zero to any pointer type. */ 7661 if (null_pointer_constant) 7662 { 7663 rhs = null_pointer_node; 7664 break; 7665 } 7666 } 7667 7668 if (memb || marginal_memb) 7669 { 7670 if (!memb) 7671 { 7672 /* We have only a marginally acceptable member type; 7673 it needs a warning. */ 7674 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb)); 7675 tree ttr = TREE_TYPE (rhstype); 7676 7677 /* Const and volatile mean something different for function 7678 types, so the usual warnings are not appropriate. */ 7679 if (TREE_CODE (ttr) == FUNCTION_TYPE 7680 && TREE_CODE (ttl) == FUNCTION_TYPE) 7681 { 7682 /* Because const and volatile on functions are 7683 restrictions that say the function will not do 7684 certain things, it is okay to use a const or volatile 7685 function where an ordinary one is wanted, but not 7686 vice-versa. */ 7687 if (TYPE_QUALS_NO_ADDR_SPACE (ttl) 7688 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) 7689 PEDWARN_FOR_QUALIFIERS (location, expr_loc, 7690 OPT_Wdiscarded_qualifiers, 7691 G_("passing argument %d of %qE " 7692 "makes %q#v qualified function " 7693 "pointer from unqualified"), 7694 G_("assignment makes %q#v qualified " 7695 "function pointer from " 7696 "unqualified"), 7697 G_("initialization makes %q#v qualified " 7698 "function pointer from " 7699 "unqualified"), 7700 G_("return makes %q#v qualified function " 7701 "pointer from unqualified"), 7702 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); 7703 } 7704 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr) 7705 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) 7706 PEDWARN_FOR_QUALIFIERS (location, expr_loc, 7707 OPT_Wdiscarded_qualifiers, 7708 G_("passing argument %d of %qE discards " 7709 "%qv qualifier from pointer target type"), 7710 G_("assignment discards %qv qualifier " 7711 "from pointer target type"), 7712 G_("initialization discards %qv qualifier " 7713 "from pointer target type"), 7714 G_("return discards %qv qualifier from " 7715 "pointer target type"), 7716 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); 7717 7718 memb = marginal_memb; 7719 } 7720 7721 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)) 7722 pedwarn (location, OPT_Wpedantic, 7723 "ISO C prohibits argument conversion to union type"); 7724 7725 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs); 7726 return build_constructor_single (type, memb, rhs); 7727 } 7728 } 7729 7730 /* Conversions among pointers */ 7731 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) 7732 && (coder == codel)) 7733 { 7734 /* If RHS refers to a built-in declared without a prototype 7735 BLTIN is the declaration of the built-in with a prototype 7736 and RHSTYPE is set to the actual type of the built-in. */ 7737 tree bltin; 7738 rhstype = type_or_builtin_type (rhs, &bltin); 7739 7740 tree ttl = TREE_TYPE (type); 7741 tree ttr = TREE_TYPE (rhstype); 7742 tree mvl = ttl; 7743 tree mvr = ttr; 7744 bool is_opaque_pointer; 7745 bool target_cmp = false; /* Cache comp_target_types () result. */ 7746 addr_space_t asl; 7747 addr_space_t asr; 7748 7749 if (TREE_CODE (mvl) != ARRAY_TYPE) 7750 mvl = (TYPE_ATOMIC (mvl) 7751 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), 7752 TYPE_QUAL_ATOMIC) 7753 : TYPE_MAIN_VARIANT (mvl)); 7754 if (TREE_CODE (mvr) != ARRAY_TYPE) 7755 mvr = (TYPE_ATOMIC (mvr) 7756 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), 7757 TYPE_QUAL_ATOMIC) 7758 : TYPE_MAIN_VARIANT (mvr)); 7759 /* Opaque pointers are treated like void pointers. */ 7760 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr); 7761 7762 /* The Plan 9 compiler permits a pointer to a struct to be 7763 automatically converted into a pointer to an anonymous field 7764 within the struct. */ 7765 if (flag_plan9_extensions 7766 && RECORD_OR_UNION_TYPE_P (mvl) 7767 && RECORD_OR_UNION_TYPE_P (mvr) 7768 && mvl != mvr) 7769 { 7770 tree new_rhs = convert_to_anonymous_field (location, type, rhs); 7771 if (new_rhs != NULL_TREE) 7772 { 7773 rhs = new_rhs; 7774 rhstype = TREE_TYPE (rhs); 7775 coder = TREE_CODE (rhstype); 7776 ttr = TREE_TYPE (rhstype); 7777 mvr = TYPE_MAIN_VARIANT (ttr); 7778 } 7779 } 7780 7781 /* C++ does not allow the implicit conversion void* -> T*. However, 7782 for the purpose of reducing the number of false positives, we 7783 tolerate the special case of 7784 7785 int *p = NULL; 7786 7787 where NULL is typically defined in C to be '(void *) 0'. */ 7788 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl)) 7789 warning_at (errtype == ic_argpass ? expr_loc : location, 7790 OPT_Wc___compat, 7791 "request for implicit conversion " 7792 "from %qT to %qT not permitted in C++", rhstype, type); 7793 7794 /* Warn of new allocations that are not big enough for the target 7795 type. */ 7796 if (warn_alloc_size && TREE_CODE (rhs) == CALL_EXPR) 7797 if (tree fndecl = get_callee_fndecl (rhs)) 7798 if (DECL_IS_MALLOC (fndecl)) 7799 { 7800 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl)); 7801 tree alloc_size = lookup_attribute ("alloc_size", attrs); 7802 if (alloc_size) 7803 warn_for_alloc_size (location, ttl, rhs, alloc_size); 7804 } 7805 7806 /* See if the pointers point to incompatible address spaces. */ 7807 asl = TYPE_ADDR_SPACE (ttl); 7808 asr = TYPE_ADDR_SPACE (ttr); 7809 if (!null_pointer_constant_p (rhs) 7810 && asr != asl && !targetm.addr_space.subset_p (asr, asl)) 7811 { 7812 auto_diagnostic_group d; 7813 bool diagnosed = true; 7814 switch (errtype) 7815 { 7816 case ic_argpass: 7817 { 7818 const char msg[] = G_("passing argument %d of %qE from " 7819 "pointer to non-enclosed address space"); 7820 if (warnopt) 7821 diagnosed 7822 = warning_at (expr_loc, warnopt, msg, parmnum, rname); 7823 else 7824 error_at (expr_loc, msg, parmnum, rname); 7825 break; 7826 } 7827 case ic_assign: 7828 { 7829 const char msg[] = G_("assignment from pointer to " 7830 "non-enclosed address space"); 7831 if (warnopt) 7832 diagnosed = warning_at (location, warnopt, msg); 7833 else 7834 error_at (location, msg); 7835 break; 7836 } 7837 case ic_init: 7838 case ic_init_const: 7839 { 7840 const char msg[] = G_("initialization from pointer to " 7841 "non-enclosed address space"); 7842 if (warnopt) 7843 diagnosed = warning_at (location, warnopt, msg); 7844 else 7845 error_at (location, msg); 7846 break; 7847 } 7848 case ic_return: 7849 { 7850 const char msg[] = G_("return from pointer to " 7851 "non-enclosed address space"); 7852 if (warnopt) 7853 diagnosed = warning_at (location, warnopt, msg); 7854 else 7855 error_at (location, msg); 7856 break; 7857 } 7858 default: 7859 gcc_unreachable (); 7860 } 7861 if (diagnosed) 7862 { 7863 if (errtype == ic_argpass) 7864 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype); 7865 else 7866 inform (location, "expected %qT but pointer is of type %qT", 7867 type, rhstype); 7868 } 7869 return error_mark_node; 7870 } 7871 7872 /* Check if the right-hand side has a format attribute but the 7873 left-hand side doesn't. */ 7874 if (warn_suggest_attribute_format 7875 && check_missing_format_attribute (type, rhstype)) 7876 { 7877 switch (errtype) 7878 { 7879 case ic_argpass: 7880 warning_at (expr_loc, OPT_Wsuggest_attribute_format, 7881 "argument %d of %qE might be " 7882 "a candidate for a format attribute", 7883 parmnum, rname); 7884 break; 7885 case ic_assign: 7886 warning_at (location, OPT_Wsuggest_attribute_format, 7887 "assignment left-hand side might be " 7888 "a candidate for a format attribute"); 7889 break; 7890 case ic_init: 7891 case ic_init_const: 7892 warning_at (location, OPT_Wsuggest_attribute_format, 7893 "initialization left-hand side might be " 7894 "a candidate for a format attribute"); 7895 break; 7896 case ic_return: 7897 warning_at (location, OPT_Wsuggest_attribute_format, 7898 "return type might be " 7899 "a candidate for a format attribute"); 7900 break; 7901 default: 7902 gcc_unreachable (); 7903 } 7904 } 7905 7906 /* See if the pointers point to incompatible scalar storage orders. */ 7907 if (warn_scalar_storage_order 7908 && !null_pointer_constant_p (rhs) 7909 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl)) 7910 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr))) 7911 { 7912 tree t; 7913 7914 switch (errtype) 7915 { 7916 case ic_argpass: 7917 /* Do not warn for built-in functions, for example memcpy, since we 7918 control how they behave and they can be useful in this area. */ 7919 if (TREE_CODE (rname) != FUNCTION_DECL 7920 || !fndecl_built_in_p (rname)) 7921 warning_at (location, OPT_Wscalar_storage_order, 7922 "passing argument %d of %qE from incompatible " 7923 "scalar storage order", parmnum, rname); 7924 break; 7925 case ic_assign: 7926 /* Do not warn if the RHS is a call to a function that returns a 7927 pointer that is not an alias. */ 7928 if (TREE_CODE (rhs) != CALL_EXPR 7929 || (t = get_callee_fndecl (rhs)) == NULL_TREE 7930 || !DECL_IS_MALLOC (t)) 7931 warning_at (location, OPT_Wscalar_storage_order, 7932 "assignment to %qT from pointer type %qT with " 7933 "incompatible scalar storage order", type, rhstype); 7934 break; 7935 case ic_init: 7936 case ic_init_const: 7937 /* Likewise. */ 7938 if (TREE_CODE (rhs) != CALL_EXPR 7939 || (t = get_callee_fndecl (rhs)) == NULL_TREE 7940 || !DECL_IS_MALLOC (t)) 7941 warning_at (location, OPT_Wscalar_storage_order, 7942 "initialization of %qT from pointer type %qT with " 7943 "incompatible scalar storage order", type, rhstype); 7944 break; 7945 case ic_return: 7946 warning_at (location, OPT_Wscalar_storage_order, 7947 "returning %qT from pointer type with incompatible " 7948 "scalar storage order %qT", rhstype, type); 7949 break; 7950 default: 7951 gcc_unreachable (); 7952 } 7953 } 7954 7955 /* Any non-function converts to a [const][volatile] void * 7956 and vice versa; otherwise, targets must be the same. 7957 Meanwhile, the lhs target must have all the qualifiers of the rhs. */ 7958 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl)) 7959 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr)) 7960 || (target_cmp = comp_target_types (location, type, rhstype)) 7961 || is_opaque_pointer 7962 || ((c_common_unsigned_type (mvl) 7963 == c_common_unsigned_type (mvr)) 7964 && (c_common_signed_type (mvl) 7965 == c_common_signed_type (mvr)) 7966 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr))) 7967 { 7968 /* Warn about loss of qualifers from pointers to arrays with 7969 qualifiers on the element type. */ 7970 if (TREE_CODE (ttr) == ARRAY_TYPE) 7971 { 7972 ttr = strip_array_types (ttr); 7973 ttl = strip_array_types (ttl); 7974 7975 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr) 7976 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl)) 7977 WARNING_FOR_QUALIFIERS (flag_isoc23, 7978 location, expr_loc, 7979 OPT_Wdiscarded_array_qualifiers, 7980 G_("passing argument %d of %qE discards " 7981 "%qv qualifier from pointer target type"), 7982 G_("assignment discards %qv qualifier " 7983 "from pointer target type"), 7984 G_("initialization discards %qv qualifier " 7985 "from pointer target type"), 7986 G_("return discards %qv qualifier from " 7987 "pointer target type"), 7988 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); 7989 } 7990 else if (pedantic 7991 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE) 7992 || 7993 (VOID_TYPE_P (ttr) 7994 && !null_pointer_constant 7995 && TREE_CODE (ttl) == FUNCTION_TYPE))) 7996 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic, 7997 G_("ISO C forbids passing argument %d of " 7998 "%qE between function pointer " 7999 "and %<void *%>"), 8000 G_("ISO C forbids assignment between " 8001 "function pointer and %<void *%>"), 8002 G_("ISO C forbids initialization between " 8003 "function pointer and %<void *%>"), 8004 G_("ISO C forbids return between function " 8005 "pointer and %<void *%>")); 8006 /* Const and volatile mean something different for function types, 8007 so the usual warnings are not appropriate. */ 8008 else if (TREE_CODE (ttr) != FUNCTION_TYPE 8009 && TREE_CODE (ttl) != FUNCTION_TYPE) 8010 { 8011 /* Assignments between atomic and non-atomic objects are OK. */ 8012 bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr) 8013 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl); 8014 bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr) 8015 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl)); 8016 8017 /* Don't warn about loss of qualifier for conversions from 8018 qualified void* to pointers to arrays with corresponding 8019 qualifier on the element type (except for pedantic before C23). */ 8020 if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc23)) 8021 PEDWARN_FOR_QUALIFIERS (location, expr_loc, 8022 OPT_Wdiscarded_qualifiers, 8023 G_("passing argument %d of %qE discards " 8024 "%qv qualifier from pointer target type"), 8025 G_("assignment discards %qv qualifier " 8026 "from pointer target type"), 8027 G_("initialization discards %qv qualifier " 8028 "from pointer target type"), 8029 G_("return discards %qv qualifier from " 8030 "pointer target type"), 8031 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); 8032 else if (warn_quals_ped) 8033 pedwarn_c11 (location, OPT_Wc11_c23_compat, 8034 "array with qualifier on the element is not qualified before C23"); 8035 8036 /* If this is not a case of ignoring a mismatch in signedness, 8037 no warning. */ 8038 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) 8039 || target_cmp) 8040 ; 8041 /* If there is a mismatch, do warn. */ 8042 else if (warn_pointer_sign) 8043 switch (errtype) 8044 { 8045 case ic_argpass: 8046 { 8047 auto_diagnostic_group d; 8048 range_label_for_type_mismatch rhs_label (rhstype, type); 8049 gcc_rich_location richloc (expr_loc, &rhs_label); 8050 if (pedwarn (&richloc, OPT_Wpointer_sign, 8051 "pointer targets in passing argument %d of " 8052 "%qE differ in signedness", parmnum, rname)) 8053 inform_for_arg (fundecl, expr_loc, parmnum, type, 8054 rhstype); 8055 } 8056 break; 8057 case ic_assign: 8058 pedwarn (location, OPT_Wpointer_sign, 8059 "pointer targets in assignment from %qT to %qT " 8060 "differ in signedness", rhstype, type); 8061 break; 8062 case ic_init: 8063 case ic_init_const: 8064 pedwarn_init (location, OPT_Wpointer_sign, 8065 "pointer targets in initialization of %qT " 8066 "from %qT differ in signedness", type, 8067 rhstype); 8068 break; 8069 case ic_return: 8070 pedwarn (location, OPT_Wpointer_sign, "pointer targets in " 8071 "returning %qT from a function with return type " 8072 "%qT differ in signedness", rhstype, type); 8073 break; 8074 default: 8075 gcc_unreachable (); 8076 } 8077 } 8078 else if (TREE_CODE (ttl) == FUNCTION_TYPE 8079 && TREE_CODE (ttr) == FUNCTION_TYPE) 8080 { 8081 /* Because const and volatile on functions are restrictions 8082 that say the function will not do certain things, 8083 it is okay to use a const or volatile function 8084 where an ordinary one is wanted, but not vice-versa. */ 8085 if (TYPE_QUALS_NO_ADDR_SPACE (ttl) 8086 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) 8087 PEDWARN_FOR_QUALIFIERS (location, expr_loc, 8088 OPT_Wdiscarded_qualifiers, 8089 G_("passing argument %d of %qE makes " 8090 "%q#v qualified function pointer " 8091 "from unqualified"), 8092 G_("assignment makes %q#v qualified function " 8093 "pointer from unqualified"), 8094 G_("initialization makes %q#v qualified " 8095 "function pointer from unqualified"), 8096 G_("return makes %q#v qualified function " 8097 "pointer from unqualified"), 8098 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); 8099 } 8100 } 8101 /* Avoid warning about the volatile ObjC EH puts on decls. */ 8102 else if (!objc_ok) 8103 { 8104 switch (errtype) 8105 { 8106 case ic_argpass: 8107 { 8108 auto_diagnostic_group d; 8109 range_label_for_type_mismatch rhs_label (rhstype, type); 8110 gcc_rich_location richloc (expr_loc, &rhs_label); 8111 if (permerror_opt (&richloc, OPT_Wincompatible_pointer_types, 8112 "passing argument %d of %qE from " 8113 "incompatible pointer type", 8114 parmnum, rname)) 8115 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype); 8116 } 8117 break; 8118 case ic_assign: 8119 if (bltin) 8120 permerror_opt (location, OPT_Wincompatible_pointer_types, 8121 "assignment to %qT from pointer to " 8122 "%qD with incompatible type %qT", 8123 type, bltin, rhstype); 8124 else 8125 permerror_opt (location, OPT_Wincompatible_pointer_types, 8126 "assignment to %qT from incompatible pointer " 8127 "type %qT", type, rhstype); 8128 break; 8129 case ic_init: 8130 case ic_init_const: 8131 if (bltin) 8132 permerror_init (location, OPT_Wincompatible_pointer_types, 8133 "initialization of %qT from pointer to " 8134 "%qD with incompatible type %qT", 8135 type, bltin, rhstype); 8136 else 8137 permerror_init (location, OPT_Wincompatible_pointer_types, 8138 "initialization of %qT from incompatible " 8139 "pointer type %qT", 8140 type, rhstype); 8141 break; 8142 case ic_return: 8143 if (bltin) 8144 permerror_opt (location, OPT_Wincompatible_pointer_types, 8145 "returning pointer to %qD of type %qT from " 8146 "a function with incompatible type %qT", 8147 bltin, rhstype, type); 8148 else 8149 permerror_opt (location, OPT_Wincompatible_pointer_types, 8150 "returning %qT from a function with " 8151 "incompatible return type %qT", rhstype, type); 8152 break; 8153 default: 8154 gcc_unreachable (); 8155 } 8156 } 8157 8158 /* If RHS isn't an address, check pointer or array of packed 8159 struct or union. */ 8160 warn_for_address_of_packed_member (type, orig_rhs); 8161 8162 return convert (type, rhs); 8163 } 8164 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE) 8165 { 8166 /* ??? This should not be an error when inlining calls to 8167 unprototyped functions. */ 8168 const char msg[] = "invalid use of non-lvalue array"; 8169 if (warnopt) 8170 warning_at (location, warnopt, msg); 8171 else 8172 error_at (location, msg); 8173 return error_mark_node; 8174 } 8175 else if (codel == POINTER_TYPE 8176 && (coder == INTEGER_TYPE 8177 || coder == ENUMERAL_TYPE 8178 || coder == BOOLEAN_TYPE 8179 || coder == NULLPTR_TYPE 8180 || coder == BITINT_TYPE)) 8181 { 8182 /* An explicit constant 0 or type nullptr_t can convert to a pointer, 8183 or one that results from arithmetic, even including a cast to 8184 integer type. */ 8185 if (!null_pointer_constant && coder != NULLPTR_TYPE) 8186 switch (errtype) 8187 { 8188 case ic_argpass: 8189 { 8190 auto_diagnostic_group d; 8191 range_label_for_type_mismatch rhs_label (rhstype, type); 8192 gcc_rich_location richloc (expr_loc, &rhs_label); 8193 if (permerror_opt (&richloc, OPT_Wint_conversion, 8194 "passing argument %d of %qE makes pointer " 8195 "from integer without a cast", parmnum, rname)) 8196 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype); 8197 } 8198 break; 8199 case ic_assign: 8200 permerror_opt (location, OPT_Wint_conversion, 8201 "assignment to %qT from %qT makes pointer from " 8202 "integer without a cast", type, rhstype); 8203 break; 8204 case ic_init: 8205 case ic_init_const: 8206 permerror_init (location, OPT_Wint_conversion, 8207 "initialization of %qT from %qT makes pointer " 8208 "from integer without a cast", type, rhstype); 8209 break; 8210 case ic_return: 8211 permerror_init (location, OPT_Wint_conversion, 8212 "returning %qT from a function with return type " 8213 "%qT makes pointer from integer without a cast", 8214 rhstype, type); 8215 break; 8216 default: 8217 gcc_unreachable (); 8218 } 8219 8220 return convert (type, rhs); 8221 } 8222 else if ((codel == INTEGER_TYPE || codel == BITINT_TYPE) 8223 && coder == POINTER_TYPE) 8224 { 8225 switch (errtype) 8226 { 8227 case ic_argpass: 8228 { 8229 auto_diagnostic_group d; 8230 range_label_for_type_mismatch rhs_label (rhstype, type); 8231 gcc_rich_location richloc (expr_loc, &rhs_label); 8232 if (permerror_opt (&richloc, OPT_Wint_conversion, 8233 "passing argument %d of %qE makes integer from " 8234 "pointer without a cast", parmnum, rname)) 8235 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype); 8236 } 8237 break; 8238 case ic_assign: 8239 permerror_opt (location, OPT_Wint_conversion, 8240 "assignment to %qT from %qT makes integer from " 8241 "pointer without a cast", type, rhstype); 8242 break; 8243 case ic_init: 8244 case ic_init_const: 8245 permerror_init (location, OPT_Wint_conversion, 8246 "initialization of %qT from %qT makes integer " 8247 "from pointer without a cast", type, rhstype); 8248 break; 8249 case ic_return: 8250 permerror_opt (location, OPT_Wint_conversion, "returning %qT from a " 8251 "function with return type %qT makes integer from " 8252 "pointer without a cast", rhstype, type); 8253 break; 8254 default: 8255 gcc_unreachable (); 8256 } 8257 8258 return convert (type, rhs); 8259 } 8260 else if (C_BOOLEAN_TYPE_P (type) 8261 /* The type nullptr_t may be converted to bool. The 8262 result is false. */ 8263 && (coder == POINTER_TYPE || coder == NULLPTR_TYPE)) 8264 { 8265 tree ret; 8266 bool save = in_late_binary_op; 8267 in_late_binary_op = true; 8268 ret = convert (type, rhs); 8269 in_late_binary_op = save; 8270 return ret; 8271 } 8272 else if (codel == NULLPTR_TYPE && null_pointer_constant) 8273 return convert (type, rhs); 8274 8275 switch (errtype) 8276 { 8277 case ic_argpass: 8278 { 8279 auto_diagnostic_group d; 8280 range_label_for_type_mismatch rhs_label (rhstype, type); 8281 gcc_rich_location richloc (expr_loc, &rhs_label); 8282 const char msg[] = G_("incompatible type for argument %d of %qE"); 8283 if (warnopt) 8284 warning_at (expr_loc, warnopt, msg, parmnum, rname); 8285 else 8286 error_at (&richloc, msg, parmnum, rname); 8287 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype); 8288 } 8289 break; 8290 case ic_assign: 8291 { 8292 const char msg[] 8293 = G_("incompatible types when assigning to type %qT from type %qT"); 8294 if (warnopt) 8295 warning_at (expr_loc, 0, msg, type, rhstype); 8296 else 8297 error_at (expr_loc, msg, type, rhstype); 8298 break; 8299 } 8300 case ic_init: 8301 case ic_init_const: 8302 { 8303 const char msg[] 8304 = G_("incompatible types when initializing type %qT using type %qT"); 8305 if (warnopt) 8306 warning_at (location, 0, msg, type, rhstype); 8307 else 8308 error_at (location, msg, type, rhstype); 8309 break; 8310 } 8311 case ic_return: 8312 { 8313 const char msg[] 8314 = G_("incompatible types when returning type %qT but %qT was expected"); 8315 if (warnopt) 8316 warning_at (location, 0, msg, rhstype, type); 8317 else 8318 error_at (location, msg, rhstype, type); 8319 break; 8320 } 8321 default: 8322 gcc_unreachable (); 8323 } 8324 8325 return error_mark_node; 8326 } 8327 8328 /* If VALUE is a compound expr all of whose expressions are constant, then 8330 return its value. Otherwise, return error_mark_node. 8331 8332 This is for handling COMPOUND_EXPRs as initializer elements 8333 which is allowed with a warning when -pedantic is specified. */ 8334 8335 static tree 8336 valid_compound_expr_initializer (tree value, tree endtype) 8337 { 8338 if (TREE_CODE (value) == COMPOUND_EXPR) 8339 { 8340 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) 8341 == error_mark_node) 8342 return error_mark_node; 8343 return valid_compound_expr_initializer (TREE_OPERAND (value, 1), 8344 endtype); 8345 } 8346 else if (!initializer_constant_valid_p (value, endtype)) 8347 return error_mark_node; 8348 else 8349 return value; 8350 } 8351 8352 /* Perform appropriate conversions on the initial value of a variable, 8354 store it in the declaration DECL, 8355 and print any error messages that are appropriate. 8356 If ORIGTYPE is not NULL_TREE, it is the original type of INIT. 8357 If the init is invalid, store an ERROR_MARK. 8358 8359 INIT_LOC is the location of the initial value. */ 8360 8361 void 8362 store_init_value (location_t init_loc, tree decl, tree init, tree origtype) 8363 { 8364 tree value, type; 8365 bool npc = false; 8366 bool int_const_expr = false; 8367 bool arith_const_expr = false; 8368 8369 /* If variable's type was invalidly declared, just ignore it. */ 8370 8371 type = TREE_TYPE (decl); 8372 if (TREE_CODE (type) == ERROR_MARK) 8373 return; 8374 8375 /* Digest the specified initializer into an expression. */ 8376 8377 if (init) 8378 { 8379 npc = null_pointer_constant_p (init); 8380 int_const_expr = (TREE_CODE (init) == INTEGER_CST 8381 && !TREE_OVERFLOW (init) 8382 && INTEGRAL_TYPE_P (TREE_TYPE (init))); 8383 /* Not fully determined before folding. */ 8384 arith_const_expr = true; 8385 } 8386 bool constexpr_p = (VAR_P (decl) 8387 && C_DECL_DECLARED_CONSTEXPR (decl)); 8388 value = digest_init (init_loc, type, init, origtype, npc, int_const_expr, 8389 arith_const_expr, true, 8390 TREE_STATIC (decl) || constexpr_p, constexpr_p); 8391 8392 /* Store the expression if valid; else report error. */ 8393 8394 if (!in_system_header_at (input_location) 8395 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl)) 8396 warning (OPT_Wtraditional, "traditional C rejects automatic " 8397 "aggregate initialization"); 8398 8399 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL) 8400 DECL_INITIAL (decl) = value; 8401 8402 /* ANSI wants warnings about out-of-range constant initializers. */ 8403 STRIP_TYPE_NOPS (value); 8404 if (TREE_STATIC (decl)) 8405 constant_expression_warning (value); 8406 8407 /* Check if we need to set array size from compound literal size. */ 8408 if (TREE_CODE (type) == ARRAY_TYPE 8409 && TYPE_DOMAIN (type) == NULL_TREE 8410 && value != error_mark_node) 8411 { 8412 tree inside_init = init; 8413 8414 STRIP_TYPE_NOPS (inside_init); 8415 inside_init = fold (inside_init); 8416 8417 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) 8418 { 8419 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init); 8420 8421 if (TYPE_DOMAIN (TREE_TYPE (cldecl))) 8422 { 8423 /* For int foo[] = (int [3]){1}; we need to set array size 8424 now since later on array initializer will be just the 8425 brace enclosed list of the compound literal. */ 8426 tree etype = strip_array_types (TREE_TYPE (decl)); 8427 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); 8428 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl)); 8429 layout_type (type); 8430 layout_decl (cldecl, 0); 8431 TREE_TYPE (decl) 8432 = c_build_qualified_type (type, TYPE_QUALS (etype)); 8433 } 8434 } 8435 } 8436 } 8437 8438 /* Methods for storing and printing names for error messages. */ 8440 8441 /* Implement a spelling stack that allows components of a name to be pushed 8442 and popped. Each element on the stack is this structure. */ 8443 8444 struct spelling 8445 { 8446 int kind; 8447 union 8448 { 8449 unsigned HOST_WIDE_INT i; 8450 const char *s; 8451 } u; 8452 }; 8453 8454 #define SPELLING_STRING 1 8455 #define SPELLING_MEMBER 2 8456 #define SPELLING_BOUNDS 3 8457 8458 static struct spelling *spelling; /* Next stack element (unused). */ 8459 static struct spelling *spelling_base; /* Spelling stack base. */ 8460 static int spelling_size; /* Size of the spelling stack. */ 8461 8462 /* Macros to save and restore the spelling stack around push_... functions. 8463 Alternative to SAVE_SPELLING_STACK. */ 8464 8465 #define SPELLING_DEPTH() (spelling - spelling_base) 8466 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH)) 8467 8468 /* Push an element on the spelling stack with type KIND and assign VALUE 8469 to MEMBER. */ 8470 8471 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \ 8472 { \ 8473 int depth = SPELLING_DEPTH (); \ 8474 \ 8475 if (depth >= spelling_size) \ 8476 { \ 8477 spelling_size += 10; \ 8478 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \ 8479 spelling_size); \ 8480 RESTORE_SPELLING_DEPTH (depth); \ 8481 } \ 8482 \ 8483 spelling->kind = (KIND); \ 8484 spelling->MEMBER = (VALUE); \ 8485 spelling++; \ 8486 } 8487 8488 /* Push STRING on the stack. Printed literally. */ 8489 8490 static void 8491 push_string (const char *string) 8492 { 8493 PUSH_SPELLING (SPELLING_STRING, string, u.s); 8494 } 8495 8496 /* Push a member name on the stack. Printed as '.' STRING. */ 8497 8498 static void 8499 push_member_name (tree decl) 8500 { 8501 const char *const string 8502 = (DECL_NAME (decl) 8503 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))) 8504 : _("<anonymous>")); 8505 PUSH_SPELLING (SPELLING_MEMBER, string, u.s); 8506 } 8507 8508 /* Push an array bounds on the stack. Printed as [BOUNDS]. */ 8509 8510 static void 8511 push_array_bounds (unsigned HOST_WIDE_INT bounds) 8512 { 8513 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); 8514 } 8515 8516 /* Compute the maximum size in bytes of the printed spelling. */ 8517 8518 static int 8519 spelling_length (void) 8520 { 8521 int size = 0; 8522 struct spelling *p; 8523 8524 for (p = spelling_base; p < spelling; p++) 8525 { 8526 if (p->kind == SPELLING_BOUNDS) 8527 size += 25; 8528 else 8529 size += strlen (p->u.s) + 1; 8530 } 8531 8532 return size; 8533 } 8534 8535 /* Print the spelling to BUFFER and return it. */ 8536 8537 static char * 8538 print_spelling (char *buffer) 8539 { 8540 char *d = buffer; 8541 struct spelling *p; 8542 8543 for (p = spelling_base; p < spelling; p++) 8544 if (p->kind == SPELLING_BOUNDS) 8545 { 8546 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i); 8547 d += strlen (d); 8548 } 8549 else 8550 { 8551 const char *s; 8552 if (p->kind == SPELLING_MEMBER) 8553 *d++ = '.'; 8554 for (s = p->u.s; (*d = *s++); d++) 8555 ; 8556 } 8557 *d++ = '\0'; 8558 return buffer; 8559 } 8560 8561 /* Check whether INIT, a floating or integer constant, is 8562 representable in TYPE, a real floating type with the same radix or 8563 a decimal floating type initialized with a binary floating 8564 constant. Return true if OK, false if not. */ 8565 static bool 8566 constexpr_init_fits_real_type (tree type, tree init) 8567 { 8568 gcc_assert (SCALAR_FLOAT_TYPE_P (type)); 8569 gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST); 8570 if (TREE_CODE (init) == REAL_CST 8571 && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type)) 8572 { 8573 /* Same mode, no conversion required except for the case of 8574 signaling NaNs if the types are incompatible (e.g. double and 8575 long double with the same mode). */ 8576 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)) 8577 && !comptypes (TYPE_MAIN_VARIANT (type), 8578 TYPE_MAIN_VARIANT (TREE_TYPE (init)))) 8579 return false; 8580 return true; 8581 } 8582 if (TREE_CODE (init) == INTEGER_CST) 8583 { 8584 tree converted = build_real_from_int_cst (type, init); 8585 bool fail = false; 8586 wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail, 8587 TYPE_PRECISION (TREE_TYPE (init))); 8588 return !fail && wi::eq_p (w, wi::to_wide (init)); 8589 } 8590 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))) 8591 return false; 8592 if ((REAL_VALUE_ISINF (TREE_REAL_CST (init)) 8593 && MODE_HAS_INFINITIES (TYPE_MODE (type))) 8594 || (REAL_VALUE_ISNAN (TREE_REAL_CST (init)) 8595 && MODE_HAS_NANS (TYPE_MODE (type)))) 8596 return true; 8597 if (DECIMAL_FLOAT_TYPE_P (type) 8598 && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))) 8599 { 8600 /* This is valid if the real number represented by the 8601 initializer can be exactly represented in the decimal 8602 type. Compare the values using MPFR. */ 8603 REAL_VALUE_TYPE t; 8604 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init)); 8605 mpfr_t bin_val, dec_val; 8606 mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p); 8607 mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p); 8608 mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN); 8609 char string[256]; 8610 real_to_decimal (string, &t, sizeof string, 0, 1); 8611 bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0 8612 && mpfr_equal_p (bin_val, dec_val)); 8613 mpfr_clear (bin_val); 8614 mpfr_clear (dec_val); 8615 return res; 8616 } 8617 /* exact_real_truncate is not quite right here, since it doesn't 8618 allow even an exact conversion to subnormal values. */ 8619 REAL_VALUE_TYPE t; 8620 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init)); 8621 return real_identical (&t, &TREE_REAL_CST (init)); 8622 } 8623 8624 /* Check whether INIT (location LOC) is valid as a 'constexpr' 8625 initializer for type TYPE, and give an error if not. INIT has 8626 already been folded and verified to be constant. INT_CONST_EXPR 8627 and ARITH_CONST_EXPR say whether it is an integer constant 8628 expression or arithmetic constant expression, respectively. If 8629 TYPE is not a scalar type, this function does nothing. */ 8630 8631 static void 8632 check_constexpr_init (location_t loc, tree type, tree init, 8633 bool int_const_expr, bool arith_const_expr) 8634 { 8635 if (POINTER_TYPE_P (type)) 8636 { 8637 /* The initializer must be null. */ 8638 if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init)) 8639 error_at (loc, "%<constexpr%> pointer initializer is not null"); 8640 return; 8641 } 8642 if (INTEGRAL_TYPE_P (type)) 8643 { 8644 /* The initializer must be an integer constant expression, 8645 representable in the target type. */ 8646 if (!int_const_expr) 8647 error_at (loc, "%<constexpr%> integer initializer is not an " 8648 "integer constant expression"); 8649 if (!int_fits_type_p (init, type)) 8650 error_at (loc, "%<constexpr%> initializer not representable in " 8651 "type of object"); 8652 return; 8653 } 8654 /* We don't apply any extra checks to extension types such as vector 8655 or fixed-point types. */ 8656 if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE) 8657 return; 8658 if (!arith_const_expr) 8659 { 8660 error_at (loc, "%<constexpr%> initializer is not an arithmetic " 8661 "constant expression"); 8662 return; 8663 } 8664 /* We don't apply any extra checks to complex integers. */ 8665 if (TREE_CODE (type) == COMPLEX_TYPE 8666 && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE) 8667 return; 8668 /* Following N3082, a real type cannot be initialized from a complex 8669 type and a binary type cannot be initialized from a decimal type 8670 (but initializing a decimal type from a binary type is OK). 8671 Signaling NaN initializers are OK only if the types are 8672 compatible (not just the same mode); all quiet NaN and infinity 8673 initializations are considered to preserve the value. */ 8674 if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE 8675 && SCALAR_FLOAT_TYPE_P (type)) 8676 { 8677 error_at (loc, "%<constexpr%> initializer for a real type is of " 8678 "complex type"); 8679 return; 8680 } 8681 if (SCALAR_FLOAT_TYPE_P (type) 8682 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init)) 8683 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)) 8684 && !DECIMAL_FLOAT_TYPE_P (type)) 8685 { 8686 error_at (loc, "%<constexpr%> initializer for a binary " 8687 "floating-point type is of decimal type"); 8688 return; 8689 } 8690 bool fits; 8691 if (TREE_CODE (type) == COMPLEX_TYPE) 8692 { 8693 switch (TREE_CODE (init)) 8694 { 8695 case INTEGER_CST: 8696 case REAL_CST: 8697 fits = constexpr_init_fits_real_type (TREE_TYPE (type), init); 8698 break; 8699 case COMPLEX_CST: 8700 fits = (constexpr_init_fits_real_type (TREE_TYPE (type), 8701 TREE_REALPART (init)) 8702 && constexpr_init_fits_real_type (TREE_TYPE (type), 8703 TREE_IMAGPART (init))); 8704 break; 8705 default: 8706 gcc_unreachable (); 8707 } 8708 } 8709 else 8710 fits = constexpr_init_fits_real_type (type, init); 8711 if (!fits) 8712 error_at (loc, "%<constexpr%> initializer not representable in " 8713 "type of object"); 8714 } 8715 8716 /* Digest the parser output INIT as an initializer for type TYPE. 8717 Return a C expression of type TYPE to represent the initial value. 8718 8719 If ORIGTYPE is not NULL_TREE, it is the original type of INIT. 8720 8721 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant, 8722 INT_CONST_EXPR is true if INIT is an integer constant expression, 8723 and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic 8724 constant expression, false if it has already been determined in the 8725 caller that it is not (but folding may have made the value passed here 8726 indistinguishable from an arithmetic constant expression). 8727 8728 If INIT is a string constant, STRICT_STRING is true if it is 8729 unparenthesized or we should not warn here for it being parenthesized. 8730 For other types of INIT, STRICT_STRING is not used. 8731 8732 INIT_LOC is the location of the INIT. 8733 8734 REQUIRE_CONSTANT requests an error if non-constant initializers or 8735 elements are seen. REQUIRE_CONSTEXPR means the stricter requirements 8736 on initializers for 'constexpr' objects apply. */ 8737 8738 static tree 8739 digest_init (location_t init_loc, tree type, tree init, tree origtype, 8740 bool null_pointer_constant, bool int_const_expr, 8741 bool arith_const_expr, bool strict_string, 8742 bool require_constant, bool require_constexpr) 8743 { 8744 enum tree_code code = TREE_CODE (type); 8745 tree inside_init = init; 8746 tree semantic_type = NULL_TREE; 8747 bool maybe_const = true; 8748 8749 if (type == error_mark_node 8750 || !init 8751 || error_operand_p (init)) 8752 return error_mark_node; 8753 8754 STRIP_TYPE_NOPS (inside_init); 8755 8756 if (!c_in_omp_for) 8757 { 8758 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR) 8759 { 8760 semantic_type = TREE_TYPE (inside_init); 8761 inside_init = TREE_OPERAND (inside_init, 0); 8762 } 8763 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const); 8764 } 8765 /* TODO: this may not detect all cases of expressions folding to 8766 constants that are not arithmetic constant expressions. */ 8767 if (!maybe_const) 8768 arith_const_expr = false; 8769 else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init)) 8770 && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE 8771 && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE) 8772 arith_const_expr = false; 8773 else if (TREE_CODE (inside_init) != INTEGER_CST 8774 && TREE_CODE (inside_init) != REAL_CST 8775 && TREE_CODE (inside_init) != COMPLEX_CST) 8776 arith_const_expr = false; 8777 else if (TREE_OVERFLOW (inside_init)) 8778 arith_const_expr = false; 8779 8780 /* Initialization of an array of chars from a string constant 8781 optionally enclosed in braces. */ 8782 8783 if (code == ARRAY_TYPE && inside_init 8784 && TREE_CODE (inside_init) == STRING_CST) 8785 { 8786 tree typ1 8787 = (TYPE_ATOMIC (TREE_TYPE (type)) 8788 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)), 8789 TYPE_QUAL_ATOMIC) 8790 : TYPE_MAIN_VARIANT (TREE_TYPE (type))); 8791 /* Note that an array could be both an array of character type 8792 and an array of wchar_t if wchar_t is signed char or unsigned 8793 char. */ 8794 bool char_array = (typ1 == char_type_node 8795 || typ1 == signed_char_type_node 8796 || typ1 == unsigned_char_type_node); 8797 bool wchar_array = !!comptypes (typ1, wchar_type_node); 8798 bool char16_array = !!comptypes (typ1, char16_type_node); 8799 bool char32_array = !!comptypes (typ1, char32_type_node); 8800 8801 if (char_array || wchar_array || char16_array || char32_array) 8802 { 8803 struct c_expr expr; 8804 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))); 8805 bool incompat_string_cst = false; 8806 expr.value = inside_init; 8807 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK); 8808 expr.original_type = NULL; 8809 expr.m_decimal = 0; 8810 maybe_warn_string_init (init_loc, type, expr); 8811 8812 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type))) 8813 pedwarn_init (init_loc, OPT_Wpedantic, 8814 "initialization of a flexible array member"); 8815 8816 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), 8817 TYPE_MAIN_VARIANT (type))) 8818 return inside_init; 8819 8820 if (char_array) 8821 { 8822 if (typ2 != char_type_node && typ2 != char8_type_node) 8823 incompat_string_cst = true; 8824 } 8825 else if (!comptypes (typ1, typ2)) 8826 incompat_string_cst = true; 8827 8828 if (incompat_string_cst) 8829 { 8830 error_init (init_loc, "cannot initialize array of %qT from " 8831 "a string literal with type array of %qT", 8832 typ1, typ2); 8833 return error_mark_node; 8834 } 8835 8836 if (require_constexpr 8837 && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2)) 8838 { 8839 /* Check if all characters of the string can be 8840 represented in the type of the constexpr object being 8841 initialized. */ 8842 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init); 8843 const unsigned char *p = 8844 (const unsigned char *) TREE_STRING_POINTER (inside_init); 8845 gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8); 8846 for (unsigned i = 0; i < len; i++) 8847 if (p[i] > 127) 8848 { 8849 error_init (init_loc, "%<constexpr%> initializer not " 8850 "representable in type of object"); 8851 break; 8852 } 8853 } 8854 8855 if (TYPE_DOMAIN (type) != NULL_TREE 8856 && TYPE_SIZE (type) != NULL_TREE 8857 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) 8858 { 8859 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init); 8860 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT; 8861 8862 /* Subtract the size of a single (possibly wide) character 8863 because it's ok to ignore the terminating null char 8864 that is counted in the length of the constant. */ 8865 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0) 8866 pedwarn_init (init_loc, 0, 8867 ("initializer-string for array of %qT " 8868 "is too long"), typ1); 8869 else if (warn_cxx_compat 8870 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0) 8871 warning_at (init_loc, OPT_Wc___compat, 8872 ("initializer-string for array of %qT " 8873 "is too long for C++"), typ1); 8874 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0) 8875 { 8876 unsigned HOST_WIDE_INT size 8877 = tree_to_uhwi (TYPE_SIZE_UNIT (type)); 8878 const char *p = TREE_STRING_POINTER (inside_init); 8879 8880 inside_init = build_string (size, p); 8881 } 8882 } 8883 8884 TREE_TYPE (inside_init) = type; 8885 return inside_init; 8886 } 8887 else if (INTEGRAL_TYPE_P (typ1)) 8888 { 8889 error_init (init_loc, "array of inappropriate type initialized " 8890 "from string constant"); 8891 return error_mark_node; 8892 } 8893 } 8894 8895 /* Build a VECTOR_CST from a *constant* vector constructor. If the 8896 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt 8897 below and handle as a constructor. */ 8898 if (code == VECTOR_TYPE 8899 && VECTOR_TYPE_P (TREE_TYPE (inside_init)) 8900 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true) 8901 && TREE_CONSTANT (inside_init)) 8902 { 8903 if (TREE_CODE (inside_init) == VECTOR_CST 8904 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), 8905 TYPE_MAIN_VARIANT (type))) 8906 return inside_init; 8907 8908 if (TREE_CODE (inside_init) == CONSTRUCTOR) 8909 { 8910 unsigned HOST_WIDE_INT ix; 8911 tree value; 8912 bool constant_p = true; 8913 8914 /* Iterate through elements and check if all constructor 8915 elements are *_CSTs. */ 8916 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value) 8917 if (!CONSTANT_CLASS_P (value)) 8918 { 8919 constant_p = false; 8920 break; 8921 } 8922 8923 if (constant_p) 8924 return build_vector_from_ctor (type, 8925 CONSTRUCTOR_ELTS (inside_init)); 8926 } 8927 } 8928 8929 if (warn_sequence_point) 8930 verify_sequence_points (inside_init); 8931 8932 /* Any type can be initialized 8933 from an expression of the same type, optionally with braces. */ 8934 8935 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE 8936 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), 8937 TYPE_MAIN_VARIANT (type)) 8938 || (code == ARRAY_TYPE 8939 && comptypes (TREE_TYPE (inside_init), type)) 8940 || (gnu_vector_type_p (type) 8941 && comptypes (TREE_TYPE (inside_init), type)) 8942 || (code == POINTER_TYPE 8943 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE 8944 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), 8945 TREE_TYPE (type))))) 8946 { 8947 if (code == POINTER_TYPE) 8948 { 8949 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE) 8950 { 8951 if (TREE_CODE (inside_init) == STRING_CST 8952 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) 8953 inside_init = array_to_pointer_conversion 8954 (init_loc, inside_init); 8955 else 8956 { 8957 error_init (init_loc, "invalid use of non-lvalue array"); 8958 return error_mark_node; 8959 } 8960 } 8961 } 8962 8963 if (code == VECTOR_TYPE || c_hardbool_type_attr (type)) 8964 /* Although the types are compatible, we may require a 8965 conversion. */ 8966 inside_init = convert (type, inside_init); 8967 8968 if ((code == RECORD_TYPE || code == UNION_TYPE) 8969 && !comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)))) 8970 { 8971 error_init (init_loc, "invalid initializer"); 8972 return error_mark_node; 8973 } 8974 8975 if (require_constant 8976 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) 8977 { 8978 /* As an extension, allow initializing objects with static storage 8979 duration with compound literals (which are then treated just as 8980 the brace enclosed list they contain). Also allow this for 8981 vectors, as we can only assign them with compound literals. */ 8982 if (flag_isoc99 && code != VECTOR_TYPE) 8983 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element " 8984 "is not constant"); 8985 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init); 8986 inside_init = DECL_INITIAL (decl); 8987 } 8988 8989 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST 8990 && TREE_CODE (inside_init) != CONSTRUCTOR) 8991 { 8992 error_init (init_loc, "array initialized from non-constant array " 8993 "expression"); 8994 return error_mark_node; 8995 } 8996 8997 /* Compound expressions can only occur here if -Wpedantic or 8998 -pedantic-errors is specified. In the later case, we always want 8999 an error. In the former case, we simply want a warning. */ 9000 if (require_constant && pedantic 9001 && TREE_CODE (inside_init) == COMPOUND_EXPR) 9002 { 9003 inside_init 9004 = valid_compound_expr_initializer (inside_init, 9005 TREE_TYPE (inside_init)); 9006 if (inside_init == error_mark_node) 9007 error_init (init_loc, "initializer element is not constant"); 9008 else 9009 pedwarn_init (init_loc, OPT_Wpedantic, 9010 "initializer element is not constant"); 9011 if (flag_pedantic_errors) 9012 inside_init = error_mark_node; 9013 } 9014 else if (require_constant 9015 && !initializer_constant_valid_p (inside_init, 9016 TREE_TYPE (inside_init))) 9017 { 9018 error_init (init_loc, "initializer element is not constant"); 9019 inside_init = error_mark_node; 9020 } 9021 else if (require_constant && !maybe_const) 9022 pedwarn_init (init_loc, OPT_Wpedantic, 9023 "initializer element is not a constant expression"); 9024 else if (require_constexpr) 9025 check_constexpr_init (init_loc, type, inside_init, 9026 int_const_expr, arith_const_expr); 9027 9028 /* Added to enable additional -Wsuggest-attribute=format warnings. */ 9029 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE) 9030 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION, 9031 type, inside_init, origtype, 9032 (require_constant 9033 ? ic_init_const 9034 : ic_init), null_pointer_constant, 9035 NULL_TREE, NULL_TREE, 0); 9036 return inside_init; 9037 } 9038 9039 /* Handle scalar types, including conversions. */ 9040 9041 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE 9042 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE 9043 || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE 9044 || code == BITINT_TYPE) 9045 { 9046 tree unconverted_init = inside_init; 9047 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE 9048 && (TREE_CODE (init) == STRING_CST 9049 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR)) 9050 inside_init = init = array_to_pointer_conversion (init_loc, init); 9051 if (semantic_type) 9052 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type, 9053 inside_init); 9054 inside_init 9055 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type, 9056 inside_init, origtype, 9057 require_constant ? ic_init_const : ic_init, 9058 null_pointer_constant, NULL_TREE, NULL_TREE, 9059 0); 9060 9061 /* Check to see if we have already given an error message. */ 9062 if (inside_init == error_mark_node) 9063 ; 9064 else if (require_constant && !TREE_CONSTANT (inside_init)) 9065 { 9066 error_init (init_loc, "initializer element is not constant"); 9067 inside_init = error_mark_node; 9068 } 9069 else if (require_constant 9070 && !initializer_constant_valid_p (inside_init, 9071 TREE_TYPE (inside_init))) 9072 { 9073 error_init (init_loc, "initializer element is not computable at " 9074 "load time"); 9075 inside_init = error_mark_node; 9076 } 9077 else if (require_constant && !maybe_const) 9078 pedwarn_init (init_loc, OPT_Wpedantic, 9079 "initializer element is not a constant expression"); 9080 else if (require_constexpr) 9081 check_constexpr_init (init_loc, type, unconverted_init, 9082 int_const_expr, arith_const_expr); 9083 9084 return inside_init; 9085 } 9086 9087 /* Come here only for records and arrays. */ 9088 9089 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) 9090 { 9091 error_init (init_loc, 9092 "variable-sized object may not be initialized except " 9093 "with an empty initializer"); 9094 return error_mark_node; 9095 } 9096 9097 error_init (init_loc, "invalid initializer"); 9098 return error_mark_node; 9099 } 9100 9101 /* Handle initializers that use braces. */ 9103 9104 /* Type of object we are accumulating a constructor for. 9105 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ 9106 static tree constructor_type; 9107 9108 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields 9109 left to fill. */ 9110 static tree constructor_fields; 9111 9112 /* For an ARRAY_TYPE, this is the specified index 9113 at which to store the next element we get. */ 9114 static tree constructor_index; 9115 9116 /* For an ARRAY_TYPE, this is the maximum index. */ 9117 static tree constructor_max_index; 9118 9119 /* For a RECORD_TYPE, this is the first field not yet written out. */ 9120 static tree constructor_unfilled_fields; 9121 9122 /* For an ARRAY_TYPE, this is the index of the first element 9123 not yet written out. */ 9124 static tree constructor_unfilled_index; 9125 9126 /* In a RECORD_TYPE, the byte index of the next consecutive field. 9127 This is so we can generate gaps between fields, when appropriate. */ 9128 static tree constructor_bit_index; 9129 9130 /* If we are saving up the elements rather than allocating them, 9131 this is the list of elements so far (in reverse order, 9132 most recent first). */ 9133 static vec<constructor_elt, va_gc> *constructor_elements; 9134 9135 /* 1 if constructor should be incrementally stored into a constructor chain, 9136 0 if all the elements should be kept in AVL tree. */ 9137 static int constructor_incremental; 9138 9139 /* 1 if so far this constructor's elements are all compile-time constants. */ 9140 static int constructor_constant; 9141 9142 /* 1 if so far this constructor's elements are all valid address constants. */ 9143 static int constructor_simple; 9144 9145 /* 1 if this constructor has an element that cannot be part of a 9146 constant expression. */ 9147 static int constructor_nonconst; 9148 9149 /* 1 if this constructor is erroneous so far. */ 9150 static int constructor_erroneous; 9151 9152 /* 1 if this constructor is the universal zero initializer { 0 }. */ 9153 static int constructor_zeroinit; 9154 9155 /* Structure for managing pending initializer elements, organized as an 9156 AVL tree. */ 9157 9158 struct init_node 9159 { 9160 struct init_node *left, *right; 9161 struct init_node *parent; 9162 int balance; 9163 tree purpose; 9164 tree value; 9165 tree origtype; 9166 }; 9167 9168 /* Tree of pending elements at this constructor level. 9169 These are elements encountered out of order 9170 which belong at places we haven't reached yet in actually 9171 writing the output. 9172 Will never hold tree nodes across GC runs. */ 9173 static struct init_node *constructor_pending_elts; 9174 9175 /* The SPELLING_DEPTH of this constructor. */ 9176 static int constructor_depth; 9177 9178 /* DECL node for which an initializer is being read. 9179 0 means we are reading a constructor expression 9180 such as (struct foo) {...}. */ 9181 static tree constructor_decl; 9182 9183 /* Nonzero if there were any member designators in this initializer. */ 9184 static int constructor_designated; 9185 9186 /* Nesting depth of designator list. */ 9187 static int designator_depth; 9188 9189 /* Nonzero if there were diagnosed errors in this designator list. */ 9190 static int designator_erroneous; 9191 9192 9193 /* This stack has a level for each implicit or explicit level of 9195 structuring in the initializer, including the outermost one. It 9196 saves the values of most of the variables above. */ 9197 9198 struct constructor_range_stack; 9199 9200 struct constructor_stack 9201 { 9202 struct constructor_stack *next; 9203 tree type; 9204 tree fields; 9205 tree index; 9206 tree max_index; 9207 tree unfilled_index; 9208 tree unfilled_fields; 9209 tree bit_index; 9210 vec<constructor_elt, va_gc> *elements; 9211 struct init_node *pending_elts; 9212 int offset; 9213 int depth; 9214 /* If value nonzero, this value should replace the entire 9215 constructor at this level. */ 9216 struct c_expr replacement_value; 9217 struct constructor_range_stack *range_stack; 9218 char constant; 9219 char simple; 9220 char nonconst; 9221 char implicit; 9222 char erroneous; 9223 char outer; 9224 char incremental; 9225 char designated; 9226 int designator_depth; 9227 }; 9228 9229 static struct constructor_stack *constructor_stack; 9230 9231 /* This stack represents designators from some range designator up to 9232 the last designator in the list. */ 9233 9234 struct constructor_range_stack 9235 { 9236 struct constructor_range_stack *next, *prev; 9237 struct constructor_stack *stack; 9238 tree range_start; 9239 tree index; 9240 tree range_end; 9241 tree fields; 9242 }; 9243 9244 static struct constructor_range_stack *constructor_range_stack; 9245 9246 /* This stack records separate initializers that are nested. 9247 Nested initializers can't happen in ANSI C, but GNU C allows them 9248 in cases like { ... (struct foo) { ... } ... }. */ 9249 9250 struct initializer_stack 9251 { 9252 struct initializer_stack *next; 9253 tree decl; 9254 struct constructor_stack *constructor_stack; 9255 struct constructor_range_stack *constructor_range_stack; 9256 vec<constructor_elt, va_gc> *elements; 9257 struct spelling *spelling; 9258 struct spelling *spelling_base; 9259 int spelling_size; 9260 char require_constant_value; 9261 char require_constant_elements; 9262 char require_constexpr_value; 9263 char designated; 9264 rich_location *missing_brace_richloc; 9265 }; 9266 9267 static struct initializer_stack *initializer_stack; 9268 9269 /* Prepare to parse and output the initializer for variable DECL. */ 9271 9272 void 9273 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, 9274 bool init_require_constant, bool init_require_constexpr, 9275 rich_location *richloc) 9276 { 9277 const char *locus; 9278 struct initializer_stack *p = XNEW (struct initializer_stack); 9279 9280 p->decl = constructor_decl; 9281 p->require_constant_value = require_constant_value; 9282 p->require_constant_elements = require_constant_elements; 9283 p->require_constexpr_value = require_constexpr_value; 9284 p->constructor_stack = constructor_stack; 9285 p->constructor_range_stack = constructor_range_stack; 9286 p->elements = constructor_elements; 9287 p->spelling = spelling; 9288 p->spelling_base = spelling_base; 9289 p->spelling_size = spelling_size; 9290 p->next = initializer_stack; 9291 p->missing_brace_richloc = richloc; 9292 p->designated = constructor_designated; 9293 initializer_stack = p; 9294 9295 constructor_decl = decl; 9296 constructor_designated = 0; 9297 9298 require_constant_value = init_require_constant; 9299 require_constexpr_value = init_require_constexpr; 9300 if (decl != NULL_TREE && decl != error_mark_node) 9301 { 9302 require_constant_elements 9303 = ((init_require_constant || (pedantic && !flag_isoc99)) 9304 /* For a scalar, you can always use any value to initialize, 9305 even within braces. */ 9306 && AGGREGATE_TYPE_P (TREE_TYPE (decl))); 9307 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))); 9308 } 9309 else 9310 { 9311 require_constant_elements = false; 9312 locus = _("(anonymous)"); 9313 } 9314 9315 constructor_stack = 0; 9316 constructor_range_stack = 0; 9317 9318 found_missing_braces = 0; 9319 9320 spelling_base = 0; 9321 spelling_size = 0; 9322 RESTORE_SPELLING_DEPTH (0); 9323 9324 if (locus) 9325 push_string (locus); 9326 } 9327 9328 void 9329 finish_init (void) 9330 { 9331 struct initializer_stack *p = initializer_stack; 9332 9333 /* Free the whole constructor stack of this initializer. */ 9334 while (constructor_stack) 9335 { 9336 struct constructor_stack *q = constructor_stack; 9337 constructor_stack = q->next; 9338 XDELETE (q); 9339 } 9340 9341 gcc_assert (!constructor_range_stack); 9342 9343 /* Pop back to the data of the outer initializer (if any). */ 9344 XDELETE (spelling_base); 9345 9346 constructor_decl = p->decl; 9347 require_constant_value = p->require_constant_value; 9348 require_constant_elements = p->require_constant_elements; 9349 require_constexpr_value = p->require_constexpr_value; 9350 constructor_stack = p->constructor_stack; 9351 constructor_designated = p->designated; 9352 constructor_range_stack = p->constructor_range_stack; 9353 constructor_elements = p->elements; 9354 spelling = p->spelling; 9355 spelling_base = p->spelling_base; 9356 spelling_size = p->spelling_size; 9357 initializer_stack = p->next; 9358 XDELETE (p); 9359 } 9360 9361 /* Call here when we see the initializer is surrounded by braces. 9363 This is instead of a call to push_init_level; 9364 it is matched by a call to pop_init_level. 9365 9366 TYPE is the type to initialize, for a constructor expression. 9367 For an initializer for a decl, TYPE is zero. */ 9368 9369 void 9370 really_start_incremental_init (tree type) 9371 { 9372 struct constructor_stack *p = XNEW (struct constructor_stack); 9373 9374 if (type == NULL_TREE) 9375 type = TREE_TYPE (constructor_decl); 9376 9377 if (VECTOR_TYPE_P (type) 9378 && TYPE_VECTOR_OPAQUE (type)) 9379 error ("opaque vector types cannot be initialized"); 9380 9381 p->type = constructor_type; 9382 p->fields = constructor_fields; 9383 p->index = constructor_index; 9384 p->max_index = constructor_max_index; 9385 p->unfilled_index = constructor_unfilled_index; 9386 p->unfilled_fields = constructor_unfilled_fields; 9387 p->bit_index = constructor_bit_index; 9388 p->elements = constructor_elements; 9389 p->constant = constructor_constant; 9390 p->simple = constructor_simple; 9391 p->nonconst = constructor_nonconst; 9392 p->erroneous = constructor_erroneous; 9393 p->pending_elts = constructor_pending_elts; 9394 p->depth = constructor_depth; 9395 p->replacement_value.value = 0; 9396 p->replacement_value.original_code = ERROR_MARK; 9397 p->replacement_value.original_type = NULL; 9398 p->implicit = 0; 9399 p->range_stack = 0; 9400 p->outer = 0; 9401 p->incremental = constructor_incremental; 9402 p->designated = constructor_designated; 9403 p->designator_depth = designator_depth; 9404 p->next = 0; 9405 constructor_stack = p; 9406 9407 constructor_constant = 1; 9408 constructor_simple = 1; 9409 constructor_nonconst = 0; 9410 constructor_depth = SPELLING_DEPTH (); 9411 constructor_elements = NULL; 9412 constructor_pending_elts = 0; 9413 constructor_type = type; 9414 constructor_incremental = 1; 9415 constructor_designated = 0; 9416 constructor_zeroinit = 1; 9417 designator_depth = 0; 9418 designator_erroneous = 0; 9419 9420 if (RECORD_OR_UNION_TYPE_P (constructor_type)) 9421 { 9422 constructor_fields = TYPE_FIELDS (constructor_type); 9423 /* Skip any nameless bit fields at the beginning. */ 9424 while (constructor_fields != NULL_TREE 9425 && DECL_UNNAMED_BIT_FIELD (constructor_fields)) 9426 constructor_fields = DECL_CHAIN (constructor_fields); 9427 9428 constructor_unfilled_fields = constructor_fields; 9429 constructor_bit_index = bitsize_zero_node; 9430 } 9431 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 9432 { 9433 if (TYPE_DOMAIN (constructor_type)) 9434 { 9435 constructor_max_index 9436 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); 9437 9438 /* Detect non-empty initializations of zero-length arrays. */ 9439 if (constructor_max_index == NULL_TREE 9440 && TYPE_SIZE (constructor_type)) 9441 constructor_max_index = integer_minus_one_node; 9442 9443 /* constructor_max_index needs to be an INTEGER_CST. Attempts 9444 to initialize VLAs with a nonempty initializer will cause a 9445 proper error; avoid tree checking errors as well by setting a 9446 safe value. */ 9447 if (constructor_max_index 9448 && TREE_CODE (constructor_max_index) != INTEGER_CST) 9449 constructor_max_index = integer_minus_one_node; 9450 9451 constructor_index 9452 = convert (bitsizetype, 9453 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); 9454 } 9455 else 9456 { 9457 constructor_index = bitsize_zero_node; 9458 constructor_max_index = NULL_TREE; 9459 } 9460 9461 constructor_unfilled_index = constructor_index; 9462 } 9463 else if (gnu_vector_type_p (constructor_type)) 9464 { 9465 /* Vectors are like simple fixed-size arrays. */ 9466 constructor_max_index = 9467 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); 9468 constructor_index = bitsize_zero_node; 9469 constructor_unfilled_index = constructor_index; 9470 } 9471 else 9472 { 9473 /* Handle the case of int x = {5}; */ 9474 constructor_fields = constructor_type; 9475 constructor_unfilled_fields = constructor_type; 9476 } 9477 } 9478 9479 extern location_t last_init_list_comma; 9481 9482 /* Called when we see an open brace for a nested initializer. Finish 9483 off any pending levels with implicit braces. */ 9484 void 9485 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack) 9486 { 9487 while (constructor_stack->implicit) 9488 { 9489 if (RECORD_OR_UNION_TYPE_P (constructor_type) 9490 && constructor_fields == NULL_TREE) 9491 process_init_element (input_location, 9492 pop_init_level (loc, 1, braced_init_obstack, 9493 last_init_list_comma), 9494 true, braced_init_obstack); 9495 else if (TREE_CODE (constructor_type) == ARRAY_TYPE 9496 && constructor_max_index 9497 && tree_int_cst_lt (constructor_max_index, 9498 constructor_index)) 9499 process_init_element (input_location, 9500 pop_init_level (loc, 1, braced_init_obstack, 9501 last_init_list_comma), 9502 true, braced_init_obstack); 9503 else 9504 break; 9505 } 9506 } 9507 9508 /* Push down into a subobject, for initialization. 9509 If this is for an explicit set of braces, IMPLICIT is 0. 9510 If it is because the next element belongs at a lower level, 9511 IMPLICIT is 1 (or 2 if the push is because of designator list). */ 9512 9513 void 9514 push_init_level (location_t loc, int implicit, 9515 struct obstack *braced_init_obstack) 9516 { 9517 struct constructor_stack *p; 9518 tree value = NULL_TREE; 9519 9520 /* Unless this is an explicit brace, we need to preserve previous 9521 content if any. */ 9522 if (implicit) 9523 { 9524 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields) 9525 value = find_init_member (constructor_fields, braced_init_obstack); 9526 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 9527 value = find_init_member (constructor_index, braced_init_obstack); 9528 } 9529 9530 p = XNEW (struct constructor_stack); 9531 p->type = constructor_type; 9532 p->fields = constructor_fields; 9533 p->index = constructor_index; 9534 p->max_index = constructor_max_index; 9535 p->unfilled_index = constructor_unfilled_index; 9536 p->unfilled_fields = constructor_unfilled_fields; 9537 p->bit_index = constructor_bit_index; 9538 p->elements = constructor_elements; 9539 p->constant = constructor_constant; 9540 p->simple = constructor_simple; 9541 p->nonconst = constructor_nonconst; 9542 p->erroneous = constructor_erroneous; 9543 p->pending_elts = constructor_pending_elts; 9544 p->depth = constructor_depth; 9545 p->replacement_value.value = NULL_TREE; 9546 p->replacement_value.original_code = ERROR_MARK; 9547 p->replacement_value.original_type = NULL; 9548 p->implicit = implicit; 9549 p->outer = 0; 9550 p->incremental = constructor_incremental; 9551 p->designated = constructor_designated; 9552 p->designator_depth = designator_depth; 9553 p->next = constructor_stack; 9554 p->range_stack = 0; 9555 constructor_stack = p; 9556 9557 constructor_constant = 1; 9558 constructor_simple = 1; 9559 constructor_nonconst = 0; 9560 constructor_depth = SPELLING_DEPTH (); 9561 constructor_elements = NULL; 9562 constructor_incremental = 1; 9563 /* If the upper initializer is designated, then mark this as 9564 designated too to prevent bogus warnings. */ 9565 constructor_designated = p->designated; 9566 constructor_pending_elts = 0; 9567 if (!implicit) 9568 { 9569 p->range_stack = constructor_range_stack; 9570 constructor_range_stack = 0; 9571 designator_depth = 0; 9572 designator_erroneous = 0; 9573 } 9574 9575 /* Don't die if an entire brace-pair level is superfluous 9576 in the containing level. */ 9577 if (constructor_type == NULL_TREE) 9578 ; 9579 else if (RECORD_OR_UNION_TYPE_P (constructor_type)) 9580 { 9581 /* Don't die if there are extra init elts at the end. */ 9582 if (constructor_fields == NULL_TREE) 9583 constructor_type = NULL_TREE; 9584 else 9585 { 9586 constructor_type = TREE_TYPE (constructor_fields); 9587 push_member_name (constructor_fields); 9588 constructor_depth++; 9589 } 9590 } 9591 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 9592 { 9593 constructor_type = TREE_TYPE (constructor_type); 9594 push_array_bounds (tree_to_uhwi (constructor_index)); 9595 constructor_depth++; 9596 } 9597 9598 if (constructor_type == NULL_TREE) 9599 { 9600 error_init (loc, "extra brace group at end of initializer"); 9601 constructor_fields = NULL_TREE; 9602 constructor_unfilled_fields = NULL_TREE; 9603 return; 9604 } 9605 9606 if (value && TREE_CODE (value) == CONSTRUCTOR) 9607 { 9608 constructor_constant = TREE_CONSTANT (value); 9609 constructor_simple = TREE_STATIC (value); 9610 constructor_nonconst = CONSTRUCTOR_NON_CONST (value); 9611 constructor_elements = CONSTRUCTOR_ELTS (value); 9612 if (!vec_safe_is_empty (constructor_elements) 9613 && (TREE_CODE (constructor_type) == RECORD_TYPE 9614 || TREE_CODE (constructor_type) == ARRAY_TYPE)) 9615 set_nonincremental_init (braced_init_obstack); 9616 } 9617 9618 if (implicit == 1) 9619 { 9620 found_missing_braces = 1; 9621 if (initializer_stack->missing_brace_richloc) 9622 initializer_stack->missing_brace_richloc->add_fixit_insert_before 9623 (loc, "{"); 9624 } 9625 9626 if (RECORD_OR_UNION_TYPE_P (constructor_type)) 9627 { 9628 constructor_fields = TYPE_FIELDS (constructor_type); 9629 /* Skip any nameless bit fields at the beginning. */ 9630 while (constructor_fields != NULL_TREE 9631 && DECL_UNNAMED_BIT_FIELD (constructor_fields)) 9632 constructor_fields = DECL_CHAIN (constructor_fields); 9633 9634 constructor_unfilled_fields = constructor_fields; 9635 constructor_bit_index = bitsize_zero_node; 9636 } 9637 else if (gnu_vector_type_p (constructor_type)) 9638 { 9639 /* Vectors are like simple fixed-size arrays. */ 9640 constructor_max_index = 9641 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); 9642 constructor_index = bitsize_int (0); 9643 constructor_unfilled_index = constructor_index; 9644 } 9645 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 9646 { 9647 if (TYPE_DOMAIN (constructor_type)) 9648 { 9649 constructor_max_index 9650 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); 9651 9652 /* Detect non-empty initializations of zero-length arrays. */ 9653 if (constructor_max_index == NULL_TREE 9654 && TYPE_SIZE (constructor_type)) 9655 constructor_max_index = integer_minus_one_node; 9656 9657 /* constructor_max_index needs to be an INTEGER_CST. Attempts 9658 to initialize VLAs will cause a proper error; avoid tree 9659 checking errors as well by setting a safe value. */ 9660 if (constructor_max_index 9661 && TREE_CODE (constructor_max_index) != INTEGER_CST) 9662 constructor_max_index = integer_minus_one_node; 9663 9664 constructor_index 9665 = convert (bitsizetype, 9666 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); 9667 } 9668 else 9669 constructor_index = bitsize_zero_node; 9670 9671 constructor_unfilled_index = constructor_index; 9672 if (value && TREE_CODE (value) == STRING_CST) 9673 { 9674 /* We need to split the char/wchar array into individual 9675 characters, so that we don't have to special case it 9676 everywhere. */ 9677 set_nonincremental_init_from_string (value, braced_init_obstack); 9678 } 9679 } 9680 else 9681 { 9682 if (constructor_type != error_mark_node) 9683 warning_init (input_location, 0, "braces around scalar initializer"); 9684 constructor_fields = constructor_type; 9685 constructor_unfilled_fields = constructor_type; 9686 } 9687 } 9688 9689 /* At the end of an implicit or explicit brace level, 9690 finish up that level of constructor. If a single expression 9691 with redundant braces initialized that level, return the 9692 c_expr structure for that expression. Otherwise, the original_code 9693 element is set to ERROR_MARK. 9694 If we were outputting the elements as they are read, return 0 as the value 9695 from inner levels (process_init_element ignores that), 9696 but return error_mark_node as the value from the outermost level 9697 (that's what we want to put in DECL_INITIAL). 9698 Otherwise, return a CONSTRUCTOR expression as the value. */ 9699 9700 struct c_expr 9701 pop_init_level (location_t loc, int implicit, 9702 struct obstack *braced_init_obstack, 9703 location_t insert_before) 9704 { 9705 struct constructor_stack *p; 9706 struct c_expr ret; 9707 ret.value = NULL_TREE; 9708 ret.original_code = ERROR_MARK; 9709 ret.original_type = NULL; 9710 ret.m_decimal = 0; 9711 9712 if (implicit == 0) 9713 { 9714 /* When we come to an explicit close brace, 9715 pop any inner levels that didn't have explicit braces. */ 9716 while (constructor_stack->implicit) 9717 process_init_element (input_location, 9718 pop_init_level (loc, 1, braced_init_obstack, 9719 insert_before), 9720 true, braced_init_obstack); 9721 gcc_assert (!constructor_range_stack); 9722 } 9723 else 9724 if (initializer_stack->missing_brace_richloc) 9725 initializer_stack->missing_brace_richloc->add_fixit_insert_before 9726 (insert_before, "}"); 9727 9728 /* Now output all pending elements. */ 9729 constructor_incremental = 1; 9730 output_pending_init_elements (1, braced_init_obstack); 9731 9732 p = constructor_stack; 9733 9734 /* Error for initializing a flexible array member, or a zero-length 9735 array member in an inappropriate context. */ 9736 if (constructor_type && constructor_fields 9737 && TREE_CODE (constructor_type) == ARRAY_TYPE 9738 && TYPE_DOMAIN (constructor_type) 9739 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) 9740 { 9741 /* Silently discard empty initializations. The parser will 9742 already have pedwarned for empty brackets. */ 9743 if (integer_zerop (constructor_unfilled_index)) 9744 constructor_type = NULL_TREE; 9745 else 9746 { 9747 gcc_assert (!TYPE_SIZE (constructor_type)); 9748 9749 if (constructor_depth > 2) 9750 error_init (loc, "initialization of flexible array member in a nested context"); 9751 else 9752 pedwarn_init (loc, OPT_Wpedantic, 9753 "initialization of a flexible array member"); 9754 9755 /* We have already issued an error message for the existence 9756 of a flexible array member not at the end of the structure. 9757 Discard the initializer so that we do not die later. */ 9758 if (DECL_CHAIN (constructor_fields) != NULL_TREE) 9759 constructor_type = NULL_TREE; 9760 } 9761 } 9762 9763 switch (vec_safe_length (constructor_elements)) 9764 { 9765 case 0: 9766 /* Initialization with { } counts as zeroinit. */ 9767 constructor_zeroinit = 1; 9768 break; 9769 case 1: 9770 /* This might be zeroinit as well. */ 9771 if (integer_zerop ((*constructor_elements)[0].value)) 9772 constructor_zeroinit = 1; 9773 break; 9774 default: 9775 /* If the constructor has more than one element, it can't be { 0 }. */ 9776 constructor_zeroinit = 0; 9777 break; 9778 } 9779 9780 /* Warn when some structs are initialized with direct aggregation. */ 9781 if (!implicit && found_missing_braces && warn_missing_braces 9782 && !constructor_zeroinit) 9783 { 9784 gcc_assert (initializer_stack->missing_brace_richloc); 9785 warning_at (initializer_stack->missing_brace_richloc, 9786 OPT_Wmissing_braces, 9787 "missing braces around initializer"); 9788 } 9789 9790 /* Warn when some struct elements are implicitly initialized to zero. */ 9791 if (warn_missing_field_initializers 9792 && constructor_type 9793 && TREE_CODE (constructor_type) == RECORD_TYPE 9794 && constructor_unfilled_fields) 9795 { 9796 /* Do not warn for flexible array members or zero-length arrays. */ 9797 while (constructor_unfilled_fields 9798 && (!DECL_SIZE (constructor_unfilled_fields) 9799 || integer_zerop (DECL_SIZE (constructor_unfilled_fields)))) 9800 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields); 9801 9802 if (constructor_unfilled_fields 9803 /* Do not warn if this level of the initializer uses member 9804 designators; it is likely to be deliberate. */ 9805 && !constructor_designated 9806 /* Do not warn about initializing with { 0 } or with { }. */ 9807 && !constructor_zeroinit) 9808 { 9809 if (warning_at (input_location, OPT_Wmissing_field_initializers, 9810 "missing initializer for field %qD of %qT", 9811 constructor_unfilled_fields, 9812 constructor_type)) 9813 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields), 9814 "%qD declared here", constructor_unfilled_fields); 9815 } 9816 } 9817 9818 /* Pad out the end of the structure. */ 9819 if (p->replacement_value.value) 9820 /* If this closes a superfluous brace pair, 9821 just pass out the element between them. */ 9822 ret = p->replacement_value; 9823 else if (constructor_type == NULL_TREE) 9824 ; 9825 else if (!RECORD_OR_UNION_TYPE_P (constructor_type) 9826 && TREE_CODE (constructor_type) != ARRAY_TYPE 9827 && !gnu_vector_type_p (constructor_type)) 9828 { 9829 /* A nonincremental scalar initializer--just return 9830 the element, after verifying there is just one. 9831 Empty scalar initializers are supported in C23. */ 9832 if (vec_safe_is_empty (constructor_elements)) 9833 { 9834 if (constructor_erroneous || constructor_type == error_mark_node) 9835 ret.value = error_mark_node; 9836 else if (TREE_CODE (constructor_type) == FUNCTION_TYPE) 9837 { 9838 error_init (loc, "invalid initializer"); 9839 ret.value = error_mark_node; 9840 } 9841 else if (TREE_CODE (constructor_type) == POINTER_TYPE) 9842 /* Ensure this is a null pointer constant in the case of a 9843 'constexpr' object initialized with {}. */ 9844 ret.value = build_zero_cst (ptr_type_node); 9845 else 9846 ret.value = build_zero_cst (constructor_type); 9847 } 9848 else if (vec_safe_length (constructor_elements) != 1) 9849 { 9850 error_init (loc, "extra elements in scalar initializer"); 9851 ret.value = (*constructor_elements)[0].value; 9852 } 9853 else 9854 ret.value = (*constructor_elements)[0].value; 9855 } 9856 else 9857 { 9858 if (constructor_erroneous) 9859 ret.value = error_mark_node; 9860 else 9861 { 9862 ret.value = build_constructor (constructor_type, 9863 constructor_elements); 9864 if (constructor_constant) 9865 TREE_CONSTANT (ret.value) = 1; 9866 if (constructor_constant && constructor_simple) 9867 TREE_STATIC (ret.value) = 1; 9868 if (constructor_nonconst) 9869 CONSTRUCTOR_NON_CONST (ret.value) = 1; 9870 } 9871 } 9872 9873 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR) 9874 { 9875 if (constructor_nonconst) 9876 ret.original_code = C_MAYBE_CONST_EXPR; 9877 else if (ret.original_code == C_MAYBE_CONST_EXPR) 9878 ret.original_code = ERROR_MARK; 9879 } 9880 9881 constructor_type = p->type; 9882 constructor_fields = p->fields; 9883 constructor_index = p->index; 9884 constructor_max_index = p->max_index; 9885 constructor_unfilled_index = p->unfilled_index; 9886 constructor_unfilled_fields = p->unfilled_fields; 9887 constructor_bit_index = p->bit_index; 9888 constructor_elements = p->elements; 9889 constructor_constant = p->constant; 9890 constructor_simple = p->simple; 9891 constructor_nonconst = p->nonconst; 9892 constructor_erroneous = p->erroneous; 9893 constructor_incremental = p->incremental; 9894 constructor_designated = p->designated; 9895 designator_depth = p->designator_depth; 9896 constructor_pending_elts = p->pending_elts; 9897 constructor_depth = p->depth; 9898 if (!p->implicit) 9899 constructor_range_stack = p->range_stack; 9900 RESTORE_SPELLING_DEPTH (constructor_depth); 9901 9902 constructor_stack = p->next; 9903 XDELETE (p); 9904 9905 if (ret.value == NULL_TREE && constructor_stack == 0) 9906 ret.value = error_mark_node; 9907 return ret; 9908 } 9909 9910 /* Common handling for both array range and field name designators. 9911 ARRAY argument is nonzero for array ranges. Returns false for success. */ 9912 9913 static bool 9914 set_designator (location_t loc, bool array, 9915 struct obstack *braced_init_obstack) 9916 { 9917 tree subtype; 9918 enum tree_code subcode; 9919 9920 /* Don't die if an entire brace-pair level is superfluous 9921 in the containing level, or for an erroneous type. */ 9922 if (constructor_type == NULL_TREE || constructor_type == error_mark_node) 9923 return true; 9924 9925 /* If there were errors in this designator list already, bail out 9926 silently. */ 9927 if (designator_erroneous) 9928 return true; 9929 9930 /* Likewise for an initializer for a variable-size type. Those are 9931 diagnosed in the parser, except for empty initializer braces. */ 9932 if (COMPLETE_TYPE_P (constructor_type) 9933 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST) 9934 return true; 9935 9936 if (!designator_depth) 9937 { 9938 gcc_assert (!constructor_range_stack); 9939 9940 /* Designator list starts at the level of closest explicit 9941 braces. */ 9942 while (constructor_stack->implicit) 9943 process_init_element (input_location, 9944 pop_init_level (loc, 1, braced_init_obstack, 9945 last_init_list_comma), 9946 true, braced_init_obstack); 9947 constructor_designated = 1; 9948 return false; 9949 } 9950 9951 switch (TREE_CODE (constructor_type)) 9952 { 9953 case RECORD_TYPE: 9954 case UNION_TYPE: 9955 subtype = TREE_TYPE (constructor_fields); 9956 if (subtype != error_mark_node) 9957 subtype = TYPE_MAIN_VARIANT (subtype); 9958 break; 9959 case ARRAY_TYPE: 9960 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); 9961 break; 9962 default: 9963 gcc_unreachable (); 9964 } 9965 9966 subcode = TREE_CODE (subtype); 9967 if (array && subcode != ARRAY_TYPE) 9968 { 9969 error_init (loc, "array index in non-array initializer"); 9970 return true; 9971 } 9972 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE) 9973 { 9974 error_init (loc, "field name not in record or union initializer"); 9975 return true; 9976 } 9977 9978 constructor_designated = 1; 9979 finish_implicit_inits (loc, braced_init_obstack); 9980 push_init_level (loc, 2, braced_init_obstack); 9981 return false; 9982 } 9983 9984 /* If there are range designators in designator list, push a new designator 9985 to constructor_range_stack. RANGE_END is end of such stack range or 9986 NULL_TREE if there is no range designator at this level. */ 9987 9988 static void 9989 push_range_stack (tree range_end, struct obstack * braced_init_obstack) 9990 { 9991 struct constructor_range_stack *p; 9992 9993 p = (struct constructor_range_stack *) 9994 obstack_alloc (braced_init_obstack, 9995 sizeof (struct constructor_range_stack)); 9996 p->prev = constructor_range_stack; 9997 p->next = 0; 9998 p->fields = constructor_fields; 9999 p->range_start = constructor_index; 10000 p->index = constructor_index; 10001 p->stack = constructor_stack; 10002 p->range_end = range_end; 10003 if (constructor_range_stack) 10004 constructor_range_stack->next = p; 10005 constructor_range_stack = p; 10006 } 10007 10008 /* Within an array initializer, specify the next index to be initialized. 10009 FIRST is that index. If LAST is nonzero, then initialize a range 10010 of indices, running from FIRST through LAST. */ 10011 10012 void 10013 set_init_index (location_t loc, tree first, tree last, 10014 struct obstack *braced_init_obstack) 10015 { 10016 if (set_designator (loc, true, braced_init_obstack)) 10017 return; 10018 10019 designator_erroneous = 1; 10020 10021 if (!INTEGRAL_TYPE_P (TREE_TYPE (first)) 10022 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last)))) 10023 { 10024 error_init (loc, "array index in initializer not of integer type"); 10025 return; 10026 } 10027 10028 if (TREE_CODE (first) != INTEGER_CST) 10029 { 10030 first = c_fully_fold (first, false, NULL); 10031 if (TREE_CODE (first) == INTEGER_CST) 10032 pedwarn_init (loc, OPT_Wpedantic, 10033 "array index in initializer is not " 10034 "an integer constant expression"); 10035 } 10036 10037 if (last && TREE_CODE (last) != INTEGER_CST) 10038 { 10039 last = c_fully_fold (last, false, NULL); 10040 if (TREE_CODE (last) == INTEGER_CST) 10041 pedwarn_init (loc, OPT_Wpedantic, 10042 "array index in initializer is not " 10043 "an integer constant expression"); 10044 } 10045 10046 if (TREE_CODE (first) != INTEGER_CST) 10047 error_init (loc, "nonconstant array index in initializer"); 10048 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST) 10049 error_init (loc, "nonconstant array index in initializer"); 10050 else if (TREE_CODE (constructor_type) != ARRAY_TYPE) 10051 error_init (loc, "array index in non-array initializer"); 10052 else if (tree_int_cst_sgn (first) == -1) 10053 error_init (loc, "array index in initializer exceeds array bounds"); 10054 else if (constructor_max_index 10055 && tree_int_cst_lt (constructor_max_index, first)) 10056 error_init (loc, "array index in initializer exceeds array bounds"); 10057 else 10058 { 10059 constant_expression_warning (first); 10060 if (last) 10061 constant_expression_warning (last); 10062 constructor_index = convert (bitsizetype, first); 10063 if (tree_int_cst_lt (constructor_index, first)) 10064 { 10065 constructor_index = copy_node (constructor_index); 10066 TREE_OVERFLOW (constructor_index) = 1; 10067 } 10068 10069 if (last) 10070 { 10071 if (tree_int_cst_equal (first, last)) 10072 last = NULL_TREE; 10073 else if (tree_int_cst_lt (last, first)) 10074 { 10075 error_init (loc, "empty index range in initializer"); 10076 last = NULL_TREE; 10077 } 10078 else 10079 { 10080 last = convert (bitsizetype, last); 10081 if (constructor_max_index != NULL_TREE 10082 && tree_int_cst_lt (constructor_max_index, last)) 10083 { 10084 error_init (loc, "array index range in initializer exceeds " 10085 "array bounds"); 10086 last = NULL_TREE; 10087 } 10088 } 10089 } 10090 10091 designator_depth++; 10092 designator_erroneous = 0; 10093 if (constructor_range_stack || last) 10094 push_range_stack (last, braced_init_obstack); 10095 } 10096 } 10097 10098 /* Within a struct initializer, specify the next field to be initialized. */ 10099 10100 void 10101 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc, 10102 struct obstack *braced_init_obstack) 10103 { 10104 tree field; 10105 10106 if (set_designator (loc, false, braced_init_obstack)) 10107 return; 10108 10109 designator_erroneous = 1; 10110 10111 if (!RECORD_OR_UNION_TYPE_P (constructor_type)) 10112 { 10113 error_init (loc, "field name not in record or union initializer"); 10114 return; 10115 } 10116 10117 field = lookup_field (constructor_type, fieldname); 10118 10119 if (field == NULL_TREE) 10120 { 10121 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname); 10122 if (guessed_id) 10123 { 10124 gcc_rich_location rich_loc (fieldname_loc); 10125 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id); 10126 error_at (&rich_loc, 10127 "%qT has no member named %qE; did you mean %qE?", 10128 constructor_type, fieldname, guessed_id); 10129 } 10130 else 10131 error_at (fieldname_loc, "%qT has no member named %qE", 10132 constructor_type, fieldname); 10133 } 10134 else 10135 do 10136 { 10137 constructor_fields = TREE_VALUE (field); 10138 designator_depth++; 10139 designator_erroneous = 0; 10140 if (constructor_range_stack) 10141 push_range_stack (NULL_TREE, braced_init_obstack); 10142 field = TREE_CHAIN (field); 10143 if (field) 10144 { 10145 if (set_designator (loc, false, braced_init_obstack)) 10146 return; 10147 } 10148 } 10149 while (field != NULL_TREE); 10150 } 10151 10152 /* Add a new initializer to the tree of pending initializers. PURPOSE 10154 identifies the initializer, either array index or field in a structure. 10155 VALUE is the value of that index or field. If ORIGTYPE is not 10156 NULL_TREE, it is the original type of VALUE. 10157 10158 IMPLICIT is true if value comes from pop_init_level (1), 10159 the new initializer has been merged with the existing one 10160 and thus no warnings should be emitted about overriding an 10161 existing initializer. */ 10162 10163 static void 10164 add_pending_init (location_t loc, tree purpose, tree value, tree origtype, 10165 bool implicit, struct obstack *braced_init_obstack) 10166 { 10167 struct init_node *p, **q, *r; 10168 10169 q = &constructor_pending_elts; 10170 p = 0; 10171 10172 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 10173 { 10174 while (*q != 0) 10175 { 10176 p = *q; 10177 if (tree_int_cst_lt (purpose, p->purpose)) 10178 q = &p->left; 10179 else if (tree_int_cst_lt (p->purpose, purpose)) 10180 q = &p->right; 10181 else 10182 { 10183 if (!implicit) 10184 { 10185 if (TREE_SIDE_EFFECTS (p->value)) 10186 warning_init (loc, OPT_Woverride_init_side_effects, 10187 "initialized field with side-effects " 10188 "overwritten"); 10189 else if (warn_override_init) 10190 warning_init (loc, OPT_Woverride_init, 10191 "initialized field overwritten"); 10192 } 10193 p->value = value; 10194 p->origtype = origtype; 10195 return; 10196 } 10197 } 10198 } 10199 else 10200 { 10201 tree bitpos; 10202 10203 bitpos = bit_position (purpose); 10204 while (*q != NULL) 10205 { 10206 p = *q; 10207 if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) 10208 q = &p->left; 10209 else if (p->purpose != purpose) 10210 q = &p->right; 10211 else 10212 { 10213 if (!implicit) 10214 { 10215 if (TREE_SIDE_EFFECTS (p->value)) 10216 warning_init (loc, OPT_Woverride_init_side_effects, 10217 "initialized field with side-effects " 10218 "overwritten"); 10219 else if (warn_override_init) 10220 warning_init (loc, OPT_Woverride_init, 10221 "initialized field overwritten"); 10222 } 10223 p->value = value; 10224 p->origtype = origtype; 10225 return; 10226 } 10227 } 10228 } 10229 10230 r = (struct init_node *) obstack_alloc (braced_init_obstack, 10231 sizeof (struct init_node)); 10232 r->purpose = purpose; 10233 r->value = value; 10234 r->origtype = origtype; 10235 10236 *q = r; 10237 r->parent = p; 10238 r->left = 0; 10239 r->right = 0; 10240 r->balance = 0; 10241 10242 while (p) 10243 { 10244 struct init_node *s; 10245 10246 if (r == p->left) 10247 { 10248 if (p->balance == 0) 10249 p->balance = -1; 10250 else if (p->balance < 0) 10251 { 10252 if (r->balance < 0) 10253 { 10254 /* L rotation. */ 10255 p->left = r->right; 10256 if (p->left) 10257 p->left->parent = p; 10258 r->right = p; 10259 10260 p->balance = 0; 10261 r->balance = 0; 10262 10263 s = p->parent; 10264 p->parent = r; 10265 r->parent = s; 10266 if (s) 10267 { 10268 if (s->left == p) 10269 s->left = r; 10270 else 10271 s->right = r; 10272 } 10273 else 10274 constructor_pending_elts = r; 10275 } 10276 else 10277 { 10278 /* LR rotation. */ 10279 struct init_node *t = r->right; 10280 10281 r->right = t->left; 10282 if (r->right) 10283 r->right->parent = r; 10284 t->left = r; 10285 10286 p->left = t->right; 10287 if (p->left) 10288 p->left->parent = p; 10289 t->right = p; 10290 10291 p->balance = t->balance < 0; 10292 r->balance = -(t->balance > 0); 10293 t->balance = 0; 10294 10295 s = p->parent; 10296 p->parent = t; 10297 r->parent = t; 10298 t->parent = s; 10299 if (s) 10300 { 10301 if (s->left == p) 10302 s->left = t; 10303 else 10304 s->right = t; 10305 } 10306 else 10307 constructor_pending_elts = t; 10308 } 10309 break; 10310 } 10311 else 10312 { 10313 /* p->balance == +1; growth of left side balances the node. */ 10314 p->balance = 0; 10315 break; 10316 } 10317 } 10318 else /* r == p->right */ 10319 { 10320 if (p->balance == 0) 10321 /* Growth propagation from right side. */ 10322 p->balance++; 10323 else if (p->balance > 0) 10324 { 10325 if (r->balance > 0) 10326 { 10327 /* R rotation. */ 10328 p->right = r->left; 10329 if (p->right) 10330 p->right->parent = p; 10331 r->left = p; 10332 10333 p->balance = 0; 10334 r->balance = 0; 10335 10336 s = p->parent; 10337 p->parent = r; 10338 r->parent = s; 10339 if (s) 10340 { 10341 if (s->left == p) 10342 s->left = r; 10343 else 10344 s->right = r; 10345 } 10346 else 10347 constructor_pending_elts = r; 10348 } 10349 else /* r->balance == -1 */ 10350 { 10351 /* RL rotation */ 10352 struct init_node *t = r->left; 10353 10354 r->left = t->right; 10355 if (r->left) 10356 r->left->parent = r; 10357 t->right = r; 10358 10359 p->right = t->left; 10360 if (p->right) 10361 p->right->parent = p; 10362 t->left = p; 10363 10364 r->balance = (t->balance < 0); 10365 p->balance = -(t->balance > 0); 10366 t->balance = 0; 10367 10368 s = p->parent; 10369 p->parent = t; 10370 r->parent = t; 10371 t->parent = s; 10372 if (s) 10373 { 10374 if (s->left == p) 10375 s->left = t; 10376 else 10377 s->right = t; 10378 } 10379 else 10380 constructor_pending_elts = t; 10381 } 10382 break; 10383 } 10384 else 10385 { 10386 /* p->balance == -1; growth of right side balances the node. */ 10387 p->balance = 0; 10388 break; 10389 } 10390 } 10391 10392 r = p; 10393 p = p->parent; 10394 } 10395 } 10396 10397 /* Build AVL tree from a sorted chain. */ 10398 10399 static void 10400 set_nonincremental_init (struct obstack * braced_init_obstack) 10401 { 10402 unsigned HOST_WIDE_INT ix; 10403 tree index, value; 10404 10405 if (TREE_CODE (constructor_type) != RECORD_TYPE 10406 && TREE_CODE (constructor_type) != ARRAY_TYPE) 10407 return; 10408 10409 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value) 10410 add_pending_init (input_location, index, value, NULL_TREE, true, 10411 braced_init_obstack); 10412 constructor_elements = NULL; 10413 if (TREE_CODE (constructor_type) == RECORD_TYPE) 10414 { 10415 constructor_unfilled_fields = TYPE_FIELDS (constructor_type); 10416 /* Skip any nameless bit fields at the beginning. */ 10417 while (constructor_unfilled_fields != NULL_TREE 10418 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields)) 10419 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); 10420 10421 } 10422 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 10423 { 10424 if (TYPE_DOMAIN (constructor_type)) 10425 constructor_unfilled_index 10426 = convert (bitsizetype, 10427 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); 10428 else 10429 constructor_unfilled_index = bitsize_zero_node; 10430 } 10431 constructor_incremental = 0; 10432 } 10433 10434 /* Build AVL tree from a string constant. */ 10435 10436 static void 10437 set_nonincremental_init_from_string (tree str, 10438 struct obstack * braced_init_obstack) 10439 { 10440 tree value, purpose, type; 10441 HOST_WIDE_INT val[2]; 10442 const char *p, *end; 10443 int byte, wchar_bytes, charwidth, bitpos; 10444 10445 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE); 10446 10447 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT; 10448 charwidth = TYPE_PRECISION (char_type_node); 10449 gcc_assert ((size_t) wchar_bytes * charwidth 10450 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT); 10451 type = TREE_TYPE (constructor_type); 10452 p = TREE_STRING_POINTER (str); 10453 end = p + TREE_STRING_LENGTH (str); 10454 10455 for (purpose = bitsize_zero_node; 10456 p < end 10457 && !(constructor_max_index 10458 && tree_int_cst_lt (constructor_max_index, purpose)); 10459 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node)) 10460 { 10461 if (wchar_bytes == 1) 10462 { 10463 val[0] = (unsigned char) *p++; 10464 val[1] = 0; 10465 } 10466 else 10467 { 10468 val[1] = 0; 10469 val[0] = 0; 10470 for (byte = 0; byte < wchar_bytes; byte++) 10471 { 10472 if (BYTES_BIG_ENDIAN) 10473 bitpos = (wchar_bytes - byte - 1) * charwidth; 10474 else 10475 bitpos = byte * charwidth; 10476 val[bitpos / HOST_BITS_PER_WIDE_INT] 10477 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++)) 10478 << (bitpos % HOST_BITS_PER_WIDE_INT); 10479 } 10480 } 10481 10482 if (!TYPE_UNSIGNED (type)) 10483 { 10484 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR; 10485 if (bitpos < HOST_BITS_PER_WIDE_INT) 10486 { 10487 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1))) 10488 { 10489 val[0] |= HOST_WIDE_INT_M1U << bitpos; 10490 val[1] = -1; 10491 } 10492 } 10493 else if (bitpos == HOST_BITS_PER_WIDE_INT) 10494 { 10495 if (val[0] < 0) 10496 val[1] = -1; 10497 } 10498 else if (val[1] & (HOST_WIDE_INT_1 10499 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT))) 10500 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT); 10501 } 10502 10503 value = wide_int_to_tree (type, 10504 wide_int::from_array (val, 2, 10505 HOST_BITS_PER_WIDE_INT * 2)); 10506 add_pending_init (input_location, purpose, value, NULL_TREE, true, 10507 braced_init_obstack); 10508 } 10509 10510 constructor_incremental = 0; 10511 } 10512 10513 /* Return value of FIELD in pending initializer or NULL_TREE if the field was 10514 not initialized yet. */ 10515 10516 static tree 10517 find_init_member (tree field, struct obstack * braced_init_obstack) 10518 { 10519 struct init_node *p; 10520 10521 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 10522 { 10523 if (constructor_incremental 10524 && tree_int_cst_lt (field, constructor_unfilled_index)) 10525 set_nonincremental_init (braced_init_obstack); 10526 10527 p = constructor_pending_elts; 10528 while (p) 10529 { 10530 if (tree_int_cst_lt (field, p->purpose)) 10531 p = p->left; 10532 else if (tree_int_cst_lt (p->purpose, field)) 10533 p = p->right; 10534 else 10535 return p->value; 10536 } 10537 } 10538 else if (TREE_CODE (constructor_type) == RECORD_TYPE) 10539 { 10540 tree bitpos = bit_position (field); 10541 10542 if (constructor_incremental 10543 && (!constructor_unfilled_fields 10544 || tree_int_cst_lt (bitpos, 10545 bit_position (constructor_unfilled_fields)))) 10546 set_nonincremental_init (braced_init_obstack); 10547 10548 p = constructor_pending_elts; 10549 while (p) 10550 { 10551 if (field == p->purpose) 10552 return p->value; 10553 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) 10554 p = p->left; 10555 else 10556 p = p->right; 10557 } 10558 } 10559 else if (TREE_CODE (constructor_type) == UNION_TYPE) 10560 { 10561 if (!vec_safe_is_empty (constructor_elements) 10562 && (constructor_elements->last ().index == field)) 10563 return constructor_elements->last ().value; 10564 } 10565 return NULL_TREE; 10566 } 10567 10568 /* "Output" the next constructor element. 10569 At top level, really output it to assembler code now. 10570 Otherwise, collect it in a list from which we will make a CONSTRUCTOR. 10571 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE. 10572 TYPE is the data type that the containing data type wants here. 10573 FIELD is the field (a FIELD_DECL) or the index that this element fills. 10574 If VALUE is a string constant, STRICT_STRING is true if it is 10575 unparenthesized or we should not warn here for it being parenthesized. 10576 For other types of VALUE, STRICT_STRING is not used. 10577 10578 PENDING if true means output pending elements that belong 10579 right after this element. (PENDING is normally true; 10580 it is false while outputting pending elements, to avoid recursion.) 10581 10582 IMPLICIT is true if value comes from pop_init_level (1), 10583 the new initializer has been merged with the existing one 10584 and thus no warnings should be emitted about overriding an 10585 existing initializer. */ 10586 10587 static void 10588 output_init_element (location_t loc, tree value, tree origtype, 10589 bool strict_string, tree type, tree field, bool pending, 10590 bool implicit, struct obstack * braced_init_obstack) 10591 { 10592 tree semantic_type = NULL_TREE; 10593 bool maybe_const = true; 10594 bool npc, int_const_expr, arith_const_expr; 10595 10596 if (type == error_mark_node || value == error_mark_node) 10597 { 10598 constructor_erroneous = 1; 10599 return; 10600 } 10601 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE 10602 && (TREE_CODE (value) == STRING_CST 10603 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR) 10604 && !(TREE_CODE (value) == STRING_CST 10605 && TREE_CODE (type) == ARRAY_TYPE 10606 && INTEGRAL_TYPE_P (TREE_TYPE (type))) 10607 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), 10608 TYPE_MAIN_VARIANT (type))) 10609 value = array_to_pointer_conversion (input_location, value); 10610 10611 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR 10612 && require_constant_value && pending) 10613 { 10614 /* As an extension, allow initializing objects with static storage 10615 duration with compound literals (which are then treated just as 10616 the brace enclosed list they contain). */ 10617 if (flag_isoc99) 10618 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not " 10619 "constant"); 10620 tree decl = COMPOUND_LITERAL_EXPR_DECL (value); 10621 value = DECL_INITIAL (decl); 10622 } 10623 10624 npc = null_pointer_constant_p (value); 10625 int_const_expr = (TREE_CODE (value) == INTEGER_CST 10626 && !TREE_OVERFLOW (value) 10627 && INTEGRAL_TYPE_P (TREE_TYPE (value))); 10628 /* Not fully determined before folding. */ 10629 arith_const_expr = true; 10630 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR) 10631 { 10632 semantic_type = TREE_TYPE (value); 10633 value = TREE_OPERAND (value, 0); 10634 } 10635 value = c_fully_fold (value, require_constant_value, &maybe_const); 10636 /* TODO: this may not detect all cases of expressions folding to 10637 constants that are not arithmetic constant expressions. */ 10638 if (!maybe_const) 10639 arith_const_expr = false; 10640 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)) 10641 && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE 10642 && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE) 10643 arith_const_expr = false; 10644 else if (TREE_CODE (value) != INTEGER_CST 10645 && TREE_CODE (value) != REAL_CST 10646 && TREE_CODE (value) != COMPLEX_CST) 10647 arith_const_expr = false; 10648 else if (TREE_OVERFLOW (value)) 10649 arith_const_expr = false; 10650 10651 if (value == error_mark_node) 10652 constructor_erroneous = 1; 10653 else if (!TREE_CONSTANT (value)) 10654 constructor_constant = 0; 10655 else if (!initializer_constant_valid_p (value, 10656 TREE_TYPE (value), 10657 AGGREGATE_TYPE_P (constructor_type) 10658 && TYPE_REVERSE_STORAGE_ORDER 10659 (constructor_type)) 10660 || (RECORD_OR_UNION_TYPE_P (constructor_type) 10661 && DECL_C_BIT_FIELD (field) 10662 && TREE_CODE (value) != INTEGER_CST)) 10663 constructor_simple = 0; 10664 if (!maybe_const) 10665 constructor_nonconst = 1; 10666 10667 /* Digest the initializer and issue any errors about incompatible 10668 types before issuing errors about non-constant initializers. */ 10669 tree new_value = value; 10670 if (semantic_type) 10671 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value); 10672 /* In the case of braces around a scalar initializer, the result of 10673 this initializer processing goes through digest_init again at the 10674 outer level. In the case of a constexpr initializer for a 10675 pointer, avoid converting a null pointer constant to something 10676 that is not a null pointer constant to avoid a spurious error 10677 from that second processing. */ 10678 if (!require_constexpr_value 10679 || !npc 10680 || TREE_CODE (constructor_type) != POINTER_TYPE) 10681 new_value = digest_init (loc, type, new_value, origtype, npc, 10682 int_const_expr, arith_const_expr, strict_string, 10683 require_constant_value, require_constexpr_value); 10684 if (new_value == error_mark_node) 10685 { 10686 constructor_erroneous = 1; 10687 return; 10688 } 10689 if (require_constant_value || require_constant_elements) 10690 constant_expression_warning (new_value); 10691 10692 /* Proceed to check the constness of the original initializer. */ 10693 if (!initializer_constant_valid_p (value, TREE_TYPE (value))) 10694 { 10695 if (require_constant_value) 10696 { 10697 error_init (loc, "initializer element is not constant"); 10698 value = error_mark_node; 10699 } 10700 else if (require_constant_elements) 10701 pedwarn (loc, OPT_Wpedantic, 10702 "initializer element is not computable at load time"); 10703 } 10704 else if (!maybe_const 10705 && (require_constant_value || require_constant_elements)) 10706 pedwarn_init (loc, OPT_Wpedantic, 10707 "initializer element is not a constant expression"); 10708 /* digest_init has already carried out the additional checks 10709 required for 'constexpr' initializers (using the information 10710 passed to it about whether the original initializer was certain 10711 kinds of constant expression), so that check does not need to be 10712 repeated here. */ 10713 10714 /* Issue -Wc++-compat warnings about initializing a bitfield with 10715 enum type. */ 10716 if (warn_cxx_compat 10717 && field != NULL_TREE 10718 && TREE_CODE (field) == FIELD_DECL 10719 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE 10720 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)) 10721 != TYPE_MAIN_VARIANT (type)) 10722 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE) 10723 { 10724 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value); 10725 if (checktype != error_mark_node 10726 && (TYPE_MAIN_VARIANT (checktype) 10727 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)))) 10728 warning_init (loc, OPT_Wc___compat, 10729 "enum conversion in initialization is invalid in C++"); 10730 } 10731 10732 /* If this field is empty and does not have side effects (and is not at 10733 the end of structure), don't do anything other than checking the 10734 initializer. */ 10735 if (field 10736 && (TREE_TYPE (field) == error_mark_node 10737 || (COMPLETE_TYPE_P (TREE_TYPE (field)) 10738 && integer_zerop (TYPE_SIZE (TREE_TYPE (field))) 10739 && !TREE_SIDE_EFFECTS (new_value) 10740 && (TREE_CODE (constructor_type) == ARRAY_TYPE 10741 || DECL_CHAIN (field))))) 10742 return; 10743 10744 /* Finally, set VALUE to the initializer value digested above. */ 10745 value = new_value; 10746 10747 /* If this element doesn't come next in sequence, 10748 put it on constructor_pending_elts. */ 10749 if (TREE_CODE (constructor_type) == ARRAY_TYPE 10750 && (!constructor_incremental 10751 || !tree_int_cst_equal (field, constructor_unfilled_index))) 10752 { 10753 if (constructor_incremental 10754 && tree_int_cst_lt (field, constructor_unfilled_index)) 10755 set_nonincremental_init (braced_init_obstack); 10756 10757 add_pending_init (loc, field, value, origtype, implicit, 10758 braced_init_obstack); 10759 return; 10760 } 10761 else if (TREE_CODE (constructor_type) == RECORD_TYPE 10762 && (!constructor_incremental 10763 || field != constructor_unfilled_fields)) 10764 { 10765 /* We do this for records but not for unions. In a union, 10766 no matter which field is specified, it can be initialized 10767 right away since it starts at the beginning of the union. */ 10768 if (constructor_incremental) 10769 { 10770 if (!constructor_unfilled_fields) 10771 set_nonincremental_init (braced_init_obstack); 10772 else 10773 { 10774 tree bitpos, unfillpos; 10775 10776 bitpos = bit_position (field); 10777 unfillpos = bit_position (constructor_unfilled_fields); 10778 10779 if (tree_int_cst_lt (bitpos, unfillpos)) 10780 set_nonincremental_init (braced_init_obstack); 10781 } 10782 } 10783 10784 add_pending_init (loc, field, value, origtype, implicit, 10785 braced_init_obstack); 10786 return; 10787 } 10788 else if (TREE_CODE (constructor_type) == UNION_TYPE 10789 && !vec_safe_is_empty (constructor_elements)) 10790 { 10791 if (!implicit) 10792 { 10793 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value)) 10794 warning_init (loc, OPT_Woverride_init_side_effects, 10795 "initialized field with side-effects overwritten"); 10796 else if (warn_override_init) 10797 warning_init (loc, OPT_Woverride_init, 10798 "initialized field overwritten"); 10799 } 10800 10801 /* We can have just one union field set. */ 10802 constructor_elements = NULL; 10803 } 10804 10805 /* Otherwise, output this element either to 10806 constructor_elements or to the assembler file. */ 10807 10808 constructor_elt celt = {field, value}; 10809 vec_safe_push (constructor_elements, celt); 10810 10811 /* Advance the variable that indicates sequential elements output. */ 10812 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 10813 constructor_unfilled_index 10814 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index, 10815 bitsize_one_node); 10816 else if (TREE_CODE (constructor_type) == RECORD_TYPE) 10817 { 10818 constructor_unfilled_fields 10819 = DECL_CHAIN (constructor_unfilled_fields); 10820 10821 /* Skip any nameless bit fields. */ 10822 while (constructor_unfilled_fields != NULL_TREE 10823 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields)) 10824 constructor_unfilled_fields = 10825 DECL_CHAIN (constructor_unfilled_fields); 10826 } 10827 else if (TREE_CODE (constructor_type) == UNION_TYPE) 10828 constructor_unfilled_fields = NULL_TREE; 10829 10830 /* Now output any pending elements which have become next. */ 10831 if (pending) 10832 output_pending_init_elements (0, braced_init_obstack); 10833 } 10834 10835 /* For two FIELD_DECLs in the same chain, return -1 if field1 10836 comes before field2, 1 if field1 comes after field2 and 10837 0 if field1 == field2. */ 10838 10839 static int 10840 init_field_decl_cmp (tree field1, tree field2) 10841 { 10842 if (field1 == field2) 10843 return 0; 10844 10845 tree bitpos1 = bit_position (field1); 10846 tree bitpos2 = bit_position (field2); 10847 if (tree_int_cst_equal (bitpos1, bitpos2)) 10848 { 10849 /* If one of the fields has non-zero bitsize, then that 10850 field must be the last one in a sequence of zero 10851 sized fields, fields after it will have bigger 10852 bit_position. */ 10853 if (TREE_TYPE (field1) != error_mark_node 10854 && COMPLETE_TYPE_P (TREE_TYPE (field1)) 10855 && integer_nonzerop (TREE_TYPE (field1))) 10856 return 1; 10857 if (TREE_TYPE (field2) != error_mark_node 10858 && COMPLETE_TYPE_P (TREE_TYPE (field2)) 10859 && integer_nonzerop (TREE_TYPE (field2))) 10860 return -1; 10861 /* Otherwise, fallback to DECL_CHAIN walk to find out 10862 which field comes earlier. Walk chains of both 10863 fields, so that if field1 and field2 are close to each 10864 other in either order, it is found soon even for large 10865 sequences of zero sized fields. */ 10866 tree f1 = field1, f2 = field2; 10867 while (1) 10868 { 10869 f1 = DECL_CHAIN (f1); 10870 f2 = DECL_CHAIN (f2); 10871 if (f1 == NULL_TREE) 10872 { 10873 gcc_assert (f2); 10874 return 1; 10875 } 10876 if (f2 == NULL_TREE) 10877 return -1; 10878 if (f1 == field2) 10879 return -1; 10880 if (f2 == field1) 10881 return 1; 10882 if (!tree_int_cst_equal (bit_position (f1), bitpos1)) 10883 return 1; 10884 if (!tree_int_cst_equal (bit_position (f2), bitpos1)) 10885 return -1; 10886 } 10887 } 10888 else if (tree_int_cst_lt (bitpos1, bitpos2)) 10889 return -1; 10890 else 10891 return 1; 10892 } 10893 10894 /* Output any pending elements which have become next. 10895 As we output elements, constructor_unfilled_{fields,index} 10896 advances, which may cause other elements to become next; 10897 if so, they too are output. 10898 10899 If ALL is 0, we return when there are 10900 no more pending elements to output now. 10901 10902 If ALL is 1, we output space as necessary so that 10903 we can output all the pending elements. */ 10904 static void 10905 output_pending_init_elements (int all, struct obstack * braced_init_obstack) 10906 { 10907 struct init_node *elt = constructor_pending_elts; 10908 tree next; 10909 10910 retry: 10911 10912 /* Look through the whole pending tree. 10913 If we find an element that should be output now, 10914 output it. Otherwise, set NEXT to the element 10915 that comes first among those still pending. */ 10916 10917 next = NULL_TREE; 10918 while (elt) 10919 { 10920 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 10921 { 10922 if (tree_int_cst_equal (elt->purpose, 10923 constructor_unfilled_index)) 10924 output_init_element (input_location, elt->value, elt->origtype, 10925 true, TREE_TYPE (constructor_type), 10926 constructor_unfilled_index, false, false, 10927 braced_init_obstack); 10928 else if (tree_int_cst_lt (constructor_unfilled_index, 10929 elt->purpose)) 10930 { 10931 /* Advance to the next smaller node. */ 10932 if (elt->left) 10933 elt = elt->left; 10934 else 10935 { 10936 /* We have reached the smallest node bigger than the 10937 current unfilled index. Fill the space first. */ 10938 next = elt->purpose; 10939 break; 10940 } 10941 } 10942 else 10943 { 10944 /* Advance to the next bigger node. */ 10945 if (elt->right) 10946 elt = elt->right; 10947 else 10948 { 10949 /* We have reached the biggest node in a subtree. Find 10950 the parent of it, which is the next bigger node. */ 10951 while (elt->parent && elt->parent->right == elt) 10952 elt = elt->parent; 10953 elt = elt->parent; 10954 if (elt && tree_int_cst_lt (constructor_unfilled_index, 10955 elt->purpose)) 10956 { 10957 next = elt->purpose; 10958 break; 10959 } 10960 } 10961 } 10962 } 10963 else if (RECORD_OR_UNION_TYPE_P (constructor_type)) 10964 { 10965 /* If the current record is complete we are done. */ 10966 if (constructor_unfilled_fields == NULL_TREE) 10967 break; 10968 10969 int cmp = init_field_decl_cmp (constructor_unfilled_fields, 10970 elt->purpose); 10971 if (cmp == 0) 10972 output_init_element (input_location, elt->value, elt->origtype, 10973 true, TREE_TYPE (elt->purpose), 10974 elt->purpose, false, false, 10975 braced_init_obstack); 10976 else if (cmp < 0) 10977 { 10978 /* Advance to the next smaller node. */ 10979 if (elt->left) 10980 elt = elt->left; 10981 else 10982 { 10983 /* We have reached the smallest node bigger than the 10984 current unfilled field. Fill the space first. */ 10985 next = elt->purpose; 10986 break; 10987 } 10988 } 10989 else 10990 { 10991 /* Advance to the next bigger node. */ 10992 if (elt->right) 10993 elt = elt->right; 10994 else 10995 { 10996 /* We have reached the biggest node in a subtree. Find 10997 the parent of it, which is the next bigger node. */ 10998 while (elt->parent && elt->parent->right == elt) 10999 elt = elt->parent; 11000 elt = elt->parent; 11001 if (elt 11002 && init_field_decl_cmp (constructor_unfilled_fields, 11003 elt->purpose) < 0) 11004 { 11005 next = elt->purpose; 11006 break; 11007 } 11008 } 11009 } 11010 } 11011 } 11012 11013 /* Ordinarily return, but not if we want to output all 11014 and there are elements left. */ 11015 if (!(all && next != NULL_TREE)) 11016 return; 11017 11018 /* If it's not incremental, just skip over the gap, so that after 11019 jumping to retry we will output the next successive element. */ 11020 if (RECORD_OR_UNION_TYPE_P (constructor_type)) 11021 constructor_unfilled_fields = next; 11022 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 11023 constructor_unfilled_index = next; 11024 11025 /* ELT now points to the node in the pending tree with the next 11026 initializer to output. */ 11027 goto retry; 11028 } 11029 11030 /* Expression VALUE coincides with the start of type TYPE in a braced 11032 initializer. Return true if we should treat VALUE as initializing 11033 the first element of TYPE, false if we should treat it as initializing 11034 TYPE as a whole. 11035 11036 If the initializer is clearly invalid, the question becomes: 11037 which choice gives the best error message? */ 11038 11039 static bool 11040 initialize_elementwise_p (tree type, tree value) 11041 { 11042 if (type == error_mark_node || value == error_mark_node) 11043 return false; 11044 11045 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type); 11046 11047 tree value_type = TREE_TYPE (value); 11048 if (value_type == error_mark_node) 11049 return false; 11050 11051 /* GNU vectors can be initialized elementwise. However, treat any 11052 kind of vector value as initializing the vector type as a whole, 11053 regardless of whether the value is a GNU vector. Such initializers 11054 are valid if and only if they would have been valid in a non-braced 11055 initializer like: 11056 11057 TYPE foo = VALUE; 11058 11059 so recursing into the vector type would be at best confusing or at 11060 worst wrong. For example, when -flax-vector-conversions is in effect, 11061 it's possible to initialize a V8HI from a V4SI, even though the vectors 11062 have different element types and different numbers of elements. */ 11063 if (gnu_vector_type_p (type)) 11064 return !VECTOR_TYPE_P (value_type); 11065 11066 if (AGGREGATE_TYPE_P (type)) 11067 return !comptypes (type, TYPE_MAIN_VARIANT (value_type)); 11068 11069 return false; 11070 } 11071 11072 /* Add one non-braced element to the current constructor level. 11073 This adjusts the current position within the constructor's type. 11074 This may also start or terminate implicit levels 11075 to handle a partly-braced initializer. 11076 11077 Once this has found the correct level for the new element, 11078 it calls output_init_element. 11079 11080 IMPLICIT is true if value comes from pop_init_level (1), 11081 the new initializer has been merged with the existing one 11082 and thus no warnings should be emitted about overriding an 11083 existing initializer. */ 11084 11085 void 11086 process_init_element (location_t loc, struct c_expr value, bool implicit, 11087 struct obstack * braced_init_obstack) 11088 { 11089 tree orig_value = value.value; 11090 int string_flag 11091 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST); 11092 bool strict_string = value.original_code == STRING_CST; 11093 bool was_designated = designator_depth != 0; 11094 11095 designator_depth = 0; 11096 designator_erroneous = 0; 11097 11098 if (!implicit && value.value && !integer_zerop (value.value)) 11099 constructor_zeroinit = 0; 11100 11101 /* Handle superfluous braces around string cst as in 11102 char x[] = {"foo"}; */ 11103 if (constructor_type 11104 && !was_designated 11105 && TREE_CODE (constructor_type) == ARRAY_TYPE 11106 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type)) 11107 && integer_zerop (constructor_unfilled_index)) 11108 { 11109 if (constructor_stack->replacement_value.value) 11110 { 11111 error_init (loc, "excess elements in %qT initializer", constructor_type); 11112 return; 11113 } 11114 else if (string_flag) 11115 { 11116 constructor_stack->replacement_value = value; 11117 return; 11118 } 11119 } 11120 11121 if (constructor_stack->replacement_value.value != NULL_TREE) 11122 { 11123 error_init (loc, "excess elements in struct initializer"); 11124 return; 11125 } 11126 11127 /* Ignore elements of a brace group if it is entirely superfluous 11128 and has already been diagnosed, or if the type is erroneous. */ 11129 if (constructor_type == NULL_TREE || constructor_type == error_mark_node) 11130 return; 11131 11132 /* Ignore elements of an initializer for a variable-size type. 11133 Those are diagnosed in the parser (empty initializer braces are OK). */ 11134 if (COMPLETE_TYPE_P (constructor_type) 11135 && !poly_int_tree_p (TYPE_SIZE (constructor_type))) 11136 return; 11137 11138 if (!implicit && warn_designated_init && !was_designated 11139 && TREE_CODE (constructor_type) == RECORD_TYPE 11140 && lookup_attribute ("designated_init", 11141 TYPE_ATTRIBUTES (constructor_type))) 11142 warning_init (loc, 11143 OPT_Wdesignated_init, 11144 "positional initialization of field " 11145 "in %<struct%> declared with %<designated_init%> attribute"); 11146 11147 /* If we've exhausted any levels that didn't have braces, 11148 pop them now. */ 11149 while (constructor_stack->implicit) 11150 { 11151 if (RECORD_OR_UNION_TYPE_P (constructor_type) 11152 && constructor_fields == NULL_TREE) 11153 process_init_element (loc, 11154 pop_init_level (loc, 1, braced_init_obstack, 11155 last_init_list_comma), 11156 true, braced_init_obstack); 11157 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE 11158 || gnu_vector_type_p (constructor_type)) 11159 && constructor_max_index 11160 && tree_int_cst_lt (constructor_max_index, 11161 constructor_index)) 11162 process_init_element (loc, 11163 pop_init_level (loc, 1, braced_init_obstack, 11164 last_init_list_comma), 11165 true, braced_init_obstack); 11166 else 11167 break; 11168 } 11169 11170 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */ 11171 if (constructor_range_stack) 11172 { 11173 /* If value is a compound literal and we'll be just using its 11174 content, don't put it into a SAVE_EXPR. */ 11175 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR 11176 || !require_constant_value) 11177 { 11178 tree semantic_type = NULL_TREE; 11179 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR) 11180 { 11181 semantic_type = TREE_TYPE (value.value); 11182 value.value = TREE_OPERAND (value.value, 0); 11183 } 11184 value.value = save_expr (value.value); 11185 if (semantic_type) 11186 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type, 11187 value.value); 11188 } 11189 } 11190 11191 while (1) 11192 { 11193 if (TREE_CODE (constructor_type) == RECORD_TYPE) 11194 { 11195 tree fieldtype; 11196 enum tree_code fieldcode; 11197 11198 if (constructor_fields == NULL_TREE) 11199 { 11200 pedwarn_init (loc, 0, "excess elements in struct initializer"); 11201 break; 11202 } 11203 11204 fieldtype = TREE_TYPE (constructor_fields); 11205 if (fieldtype != error_mark_node) 11206 fieldtype = TYPE_MAIN_VARIANT (fieldtype); 11207 fieldcode = TREE_CODE (fieldtype); 11208 11209 /* Error for non-static initialization of a flexible array member. */ 11210 if (fieldcode == ARRAY_TYPE 11211 && !require_constant_value 11212 && TYPE_SIZE (fieldtype) == NULL_TREE 11213 && DECL_CHAIN (constructor_fields) == NULL_TREE) 11214 { 11215 error_init (loc, "non-static initialization of a flexible " 11216 "array member"); 11217 break; 11218 } 11219 11220 /* Error for initialization of a flexible array member with 11221 a string constant if the structure is in an array. E.g.: 11222 struct S { int x; char y[]; }; 11223 struct S s[] = { { 1, "foo" } }; 11224 is invalid. */ 11225 if (string_flag 11226 && fieldcode == ARRAY_TYPE 11227 && constructor_depth > 1 11228 && TYPE_SIZE (fieldtype) == NULL_TREE 11229 && DECL_CHAIN (constructor_fields) == NULL_TREE) 11230 { 11231 bool in_array_p = false; 11232 for (struct constructor_stack *p = constructor_stack; 11233 p && p->type; p = p->next) 11234 if (TREE_CODE (p->type) == ARRAY_TYPE) 11235 { 11236 in_array_p = true; 11237 break; 11238 } 11239 if (in_array_p) 11240 { 11241 error_init (loc, "initialization of flexible array " 11242 "member in a nested context"); 11243 break; 11244 } 11245 } 11246 11247 /* Accept a string constant to initialize a subarray. */ 11248 if (value.value != NULL_TREE 11249 && fieldcode == ARRAY_TYPE 11250 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) 11251 && string_flag) 11252 value.value = orig_value; 11253 /* Otherwise, if we have come to a subaggregate, 11254 and we don't have an element of its type, push into it. */ 11255 else if (value.value != NULL_TREE 11256 && initialize_elementwise_p (fieldtype, value.value)) 11257 { 11258 push_init_level (loc, 1, braced_init_obstack); 11259 continue; 11260 } 11261 11262 if (value.value) 11263 { 11264 push_member_name (constructor_fields); 11265 output_init_element (loc, value.value, value.original_type, 11266 strict_string, fieldtype, 11267 constructor_fields, true, implicit, 11268 braced_init_obstack); 11269 RESTORE_SPELLING_DEPTH (constructor_depth); 11270 } 11271 else 11272 /* Do the bookkeeping for an element that was 11273 directly output as a constructor. */ 11274 { 11275 /* For a record, keep track of end position of last field. */ 11276 if (DECL_SIZE (constructor_fields)) 11277 constructor_bit_index 11278 = size_binop_loc (input_location, PLUS_EXPR, 11279 bit_position (constructor_fields), 11280 DECL_SIZE (constructor_fields)); 11281 11282 /* If the current field was the first one not yet written out, 11283 it isn't now, so update. */ 11284 if (constructor_unfilled_fields == constructor_fields) 11285 { 11286 constructor_unfilled_fields = DECL_CHAIN (constructor_fields); 11287 /* Skip any nameless bit fields. */ 11288 while (constructor_unfilled_fields != 0 11289 && (DECL_UNNAMED_BIT_FIELD 11290 (constructor_unfilled_fields))) 11291 constructor_unfilled_fields = 11292 DECL_CHAIN (constructor_unfilled_fields); 11293 } 11294 } 11295 11296 constructor_fields = DECL_CHAIN (constructor_fields); 11297 /* Skip any nameless bit fields at the beginning. */ 11298 while (constructor_fields != NULL_TREE 11299 && DECL_UNNAMED_BIT_FIELD (constructor_fields)) 11300 constructor_fields = DECL_CHAIN (constructor_fields); 11301 } 11302 else if (TREE_CODE (constructor_type) == UNION_TYPE) 11303 { 11304 tree fieldtype; 11305 enum tree_code fieldcode; 11306 11307 if (constructor_fields == NULL_TREE) 11308 { 11309 pedwarn_init (loc, 0, 11310 "excess elements in union initializer"); 11311 break; 11312 } 11313 11314 fieldtype = TREE_TYPE (constructor_fields); 11315 if (fieldtype != error_mark_node) 11316 fieldtype = TYPE_MAIN_VARIANT (fieldtype); 11317 fieldcode = TREE_CODE (fieldtype); 11318 11319 /* Warn that traditional C rejects initialization of unions. 11320 We skip the warning if the value is zero. This is done 11321 under the assumption that the zero initializer in user 11322 code appears conditioned on e.g. __STDC__ to avoid 11323 "missing initializer" warnings and relies on default 11324 initialization to zero in the traditional C case. 11325 We also skip the warning if the initializer is designated, 11326 again on the assumption that this must be conditional on 11327 __STDC__ anyway (and we've already complained about the 11328 member-designator already). */ 11329 if (!in_system_header_at (input_location) && !constructor_designated 11330 && !(value.value && (integer_zerop (value.value) 11331 || real_zerop (value.value)))) 11332 warning (OPT_Wtraditional, "traditional C rejects initialization " 11333 "of unions"); 11334 11335 /* Accept a string constant to initialize a subarray. */ 11336 if (value.value != NULL_TREE 11337 && fieldcode == ARRAY_TYPE 11338 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) 11339 && string_flag) 11340 value.value = orig_value; 11341 /* Otherwise, if we have come to a subaggregate, 11342 and we don't have an element of its type, push into it. */ 11343 else if (value.value != NULL_TREE 11344 && initialize_elementwise_p (fieldtype, value.value)) 11345 { 11346 push_init_level (loc, 1, braced_init_obstack); 11347 continue; 11348 } 11349 11350 if (value.value) 11351 { 11352 push_member_name (constructor_fields); 11353 output_init_element (loc, value.value, value.original_type, 11354 strict_string, fieldtype, 11355 constructor_fields, true, implicit, 11356 braced_init_obstack); 11357 RESTORE_SPELLING_DEPTH (constructor_depth); 11358 } 11359 else 11360 /* Do the bookkeeping for an element that was 11361 directly output as a constructor. */ 11362 { 11363 constructor_bit_index = DECL_SIZE (constructor_fields); 11364 constructor_unfilled_fields = DECL_CHAIN (constructor_fields); 11365 } 11366 11367 constructor_fields = NULL_TREE; 11368 } 11369 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 11370 { 11371 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); 11372 enum tree_code eltcode = TREE_CODE (elttype); 11373 11374 /* Accept a string constant to initialize a subarray. */ 11375 if (value.value != NULL_TREE 11376 && eltcode == ARRAY_TYPE 11377 && INTEGRAL_TYPE_P (TREE_TYPE (elttype)) 11378 && string_flag) 11379 value.value = orig_value; 11380 /* Otherwise, if we have come to a subaggregate, 11381 and we don't have an element of its type, push into it. */ 11382 else if (value.value != NULL_TREE 11383 && initialize_elementwise_p (elttype, value.value)) 11384 { 11385 push_init_level (loc, 1, braced_init_obstack); 11386 continue; 11387 } 11388 11389 if (constructor_max_index != NULL_TREE 11390 && (tree_int_cst_lt (constructor_max_index, constructor_index) 11391 || integer_all_onesp (constructor_max_index))) 11392 { 11393 pedwarn_init (loc, 0, 11394 "excess elements in array initializer"); 11395 break; 11396 } 11397 11398 /* Now output the actual element. */ 11399 if (value.value) 11400 { 11401 push_array_bounds (tree_to_uhwi (constructor_index)); 11402 output_init_element (loc, value.value, value.original_type, 11403 strict_string, elttype, 11404 constructor_index, true, implicit, 11405 braced_init_obstack); 11406 RESTORE_SPELLING_DEPTH (constructor_depth); 11407 } 11408 11409 constructor_index 11410 = size_binop_loc (input_location, PLUS_EXPR, 11411 constructor_index, bitsize_one_node); 11412 11413 if (!value.value) 11414 /* If we are doing the bookkeeping for an element that was 11415 directly output as a constructor, we must update 11416 constructor_unfilled_index. */ 11417 constructor_unfilled_index = constructor_index; 11418 } 11419 else if (gnu_vector_type_p (constructor_type)) 11420 { 11421 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); 11422 11423 /* Do a basic check of initializer size. Note that vectors 11424 always have a fixed size derived from their type. */ 11425 if (tree_int_cst_lt (constructor_max_index, constructor_index)) 11426 { 11427 pedwarn_init (loc, 0, 11428 "excess elements in vector initializer"); 11429 break; 11430 } 11431 11432 /* Now output the actual element. */ 11433 if (value.value) 11434 { 11435 if (TREE_CODE (value.value) == VECTOR_CST) 11436 elttype = TYPE_MAIN_VARIANT (constructor_type); 11437 output_init_element (loc, value.value, value.original_type, 11438 strict_string, elttype, 11439 constructor_index, true, implicit, 11440 braced_init_obstack); 11441 } 11442 11443 constructor_index 11444 = size_binop_loc (input_location, 11445 PLUS_EXPR, constructor_index, bitsize_one_node); 11446 11447 if (!value.value) 11448 /* If we are doing the bookkeeping for an element that was 11449 directly output as a constructor, we must update 11450 constructor_unfilled_index. */ 11451 constructor_unfilled_index = constructor_index; 11452 } 11453 11454 /* Handle the sole element allowed in a braced initializer 11455 for a scalar variable. */ 11456 else if (constructor_type != error_mark_node 11457 && constructor_fields == NULL_TREE) 11458 { 11459 pedwarn_init (loc, 0, 11460 "excess elements in scalar initializer"); 11461 break; 11462 } 11463 else 11464 { 11465 if (value.value) 11466 output_init_element (loc, value.value, value.original_type, 11467 strict_string, constructor_type, 11468 NULL_TREE, true, implicit, 11469 braced_init_obstack); 11470 constructor_fields = NULL_TREE; 11471 } 11472 11473 /* Handle range initializers either at this level or anywhere higher 11474 in the designator stack. */ 11475 if (constructor_range_stack) 11476 { 11477 struct constructor_range_stack *p, *range_stack; 11478 int finish = 0; 11479 11480 range_stack = constructor_range_stack; 11481 constructor_range_stack = 0; 11482 while (constructor_stack != range_stack->stack) 11483 { 11484 gcc_assert (constructor_stack->implicit); 11485 process_init_element (loc, 11486 pop_init_level (loc, 1, 11487 braced_init_obstack, 11488 last_init_list_comma), 11489 true, braced_init_obstack); 11490 } 11491 for (p = range_stack; 11492 !p->range_end || tree_int_cst_equal (p->index, p->range_end); 11493 p = p->prev) 11494 { 11495 gcc_assert (constructor_stack->implicit); 11496 process_init_element (loc, 11497 pop_init_level (loc, 1, 11498 braced_init_obstack, 11499 last_init_list_comma), 11500 true, braced_init_obstack); 11501 } 11502 11503 p->index = size_binop_loc (input_location, 11504 PLUS_EXPR, p->index, bitsize_one_node); 11505 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev) 11506 finish = 1; 11507 11508 while (1) 11509 { 11510 constructor_index = p->index; 11511 constructor_fields = p->fields; 11512 if (finish && p->range_end && p->index == p->range_start) 11513 { 11514 finish = 0; 11515 p->prev = 0; 11516 } 11517 p = p->next; 11518 if (!p) 11519 break; 11520 finish_implicit_inits (loc, braced_init_obstack); 11521 push_init_level (loc, 2, braced_init_obstack); 11522 p->stack = constructor_stack; 11523 if (p->range_end && tree_int_cst_equal (p->index, p->range_end)) 11524 p->index = p->range_start; 11525 } 11526 11527 if (!finish) 11528 constructor_range_stack = range_stack; 11529 continue; 11530 } 11531 11532 break; 11533 } 11534 11535 constructor_range_stack = 0; 11536 } 11537 11538 /* Build a complete asm-statement, whose components are a CV_QUALIFIER 11540 (guaranteed to be 'volatile' or null) and ARGS (represented using 11541 an ASM_EXPR node). */ 11542 tree 11543 build_asm_stmt (bool is_volatile, tree args) 11544 { 11545 if (is_volatile) 11546 ASM_VOLATILE_P (args) = 1; 11547 return add_stmt (args); 11548 } 11549 11550 /* Build an asm-expr, whose components are a STRING, some OUTPUTS, 11551 some INPUTS, and some CLOBBERS. The latter three may be NULL. 11552 SIMPLE indicates whether there was anything at all after the 11553 string in the asm expression -- asm("blah") and asm("blah" : ) 11554 are subtly different. We use a ASM_EXPR node to represent this. 11555 LOC is the location of the asm, and IS_INLINE says whether this 11556 is asm inline. */ 11557 tree 11558 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs, 11559 tree clobbers, tree labels, bool simple, bool is_inline) 11560 { 11561 tree tail; 11562 tree args; 11563 int i; 11564 const char *constraint; 11565 const char **oconstraints; 11566 bool allows_mem, allows_reg, is_inout; 11567 int ninputs, noutputs; 11568 11569 ninputs = list_length (inputs); 11570 noutputs = list_length (outputs); 11571 oconstraints = (const char **) alloca (noutputs * sizeof (const char *)); 11572 11573 string = resolve_asm_operand_names (string, outputs, inputs, labels); 11574 11575 /* Remove output conversions that change the type but not the mode. */ 11576 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail)) 11577 { 11578 tree output = TREE_VALUE (tail); 11579 11580 output = c_fully_fold (output, false, NULL, true); 11581 11582 /* ??? Really, this should not be here. Users should be using a 11583 proper lvalue, dammit. But there's a long history of using casts 11584 in the output operands. In cases like longlong.h, this becomes a 11585 primitive form of typechecking -- if the cast can be removed, then 11586 the output operand had a type of the proper width; otherwise we'll 11587 get an error. Gross, but ... */ 11588 STRIP_NOPS (output); 11589 11590 if (!lvalue_or_else (loc, output, lv_asm)) 11591 output = error_mark_node; 11592 11593 if (output != error_mark_node 11594 && (TREE_READONLY (output) 11595 || TYPE_READONLY (TREE_TYPE (output)) 11596 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output)) 11597 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output))))) 11598 readonly_error (loc, output, lv_asm); 11599 11600 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); 11601 oconstraints[i] = constraint; 11602 11603 if (parse_output_constraint (&constraint, i, ninputs, noutputs, 11604 &allows_mem, &allows_reg, &is_inout)) 11605 { 11606 /* If the operand is going to end up in memory, 11607 mark it addressable. */ 11608 if (!allows_reg && !c_mark_addressable (output)) 11609 output = error_mark_node; 11610 if (!(!allows_reg && allows_mem) 11611 && output != error_mark_node 11612 && VOID_TYPE_P (TREE_TYPE (output))) 11613 { 11614 error_at (loc, "invalid use of void expression"); 11615 output = error_mark_node; 11616 } 11617 } 11618 else 11619 output = error_mark_node; 11620 11621 TREE_VALUE (tail) = output; 11622 } 11623 11624 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail)) 11625 { 11626 tree input; 11627 11628 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); 11629 input = TREE_VALUE (tail); 11630 11631 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0, 11632 oconstraints, &allows_mem, &allows_reg)) 11633 { 11634 /* If the operand is going to end up in memory, 11635 mark it addressable. */ 11636 if (!allows_reg && allows_mem) 11637 { 11638 input = c_fully_fold (input, false, NULL, true); 11639 11640 /* Strip the nops as we allow this case. FIXME, this really 11641 should be rejected or made deprecated. */ 11642 STRIP_NOPS (input); 11643 if (!c_mark_addressable (input)) 11644 input = error_mark_node; 11645 } 11646 else 11647 { 11648 struct c_expr expr; 11649 memset (&expr, 0, sizeof (expr)); 11650 expr.value = input; 11651 expr = convert_lvalue_to_rvalue (loc, expr, true, false); 11652 input = c_fully_fold (expr.value, false, NULL); 11653 11654 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input))) 11655 { 11656 error_at (loc, "invalid use of void expression"); 11657 input = error_mark_node; 11658 } 11659 } 11660 } 11661 else 11662 input = error_mark_node; 11663 11664 TREE_VALUE (tail) = input; 11665 } 11666 11667 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels); 11668 11669 /* asm statements without outputs, including simple ones, are treated 11670 as volatile. */ 11671 ASM_INPUT_P (args) = simple; 11672 ASM_VOLATILE_P (args) = (noutputs == 0); 11673 ASM_INLINE_P (args) = is_inline; 11674 11675 return args; 11676 } 11677 11678 /* Generate a goto statement to LABEL. LOC is the location of the 11680 GOTO. */ 11681 11682 tree 11683 c_finish_goto_label (location_t loc, tree label) 11684 { 11685 tree decl = lookup_label_for_goto (loc, label); 11686 if (!decl) 11687 return NULL_TREE; 11688 TREE_USED (decl) = 1; 11689 { 11690 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN)); 11691 tree t = build1 (GOTO_EXPR, void_type_node, decl); 11692 SET_EXPR_LOCATION (t, loc); 11693 return add_stmt (t); 11694 } 11695 } 11696 11697 /* Generate a computed goto statement to EXPR. LOC is the location of 11698 the GOTO. */ 11699 11700 tree 11701 c_finish_goto_ptr (location_t loc, c_expr val) 11702 { 11703 tree expr = val.value; 11704 tree t; 11705 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>"); 11706 if (expr != error_mark_node 11707 && !POINTER_TYPE_P (TREE_TYPE (expr)) 11708 && !null_pointer_constant_p (expr)) 11709 { 11710 error_at (val.get_location (), 11711 "computed goto must be pointer type"); 11712 expr = build_zero_cst (ptr_type_node); 11713 } 11714 expr = c_fully_fold (expr, false, NULL); 11715 expr = convert (ptr_type_node, expr); 11716 t = build1 (GOTO_EXPR, void_type_node, expr); 11717 SET_EXPR_LOCATION (t, loc); 11718 return add_stmt (t); 11719 } 11720 11721 /* Generate a C `return' statement. RETVAL is the expression for what 11722 to return, or a null pointer for `return;' with no value. LOC is 11723 the location of the return statement, or the location of the expression, 11724 if the statement has any. If ORIGTYPE is not NULL_TREE, it 11725 is the original type of RETVAL. */ 11726 11727 tree 11728 c_finish_return (location_t loc, tree retval, tree origtype) 11729 { 11730 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt; 11731 bool no_warning = false; 11732 bool npc = false; 11733 11734 /* Use the expansion point to handle cases such as returning NULL 11735 in a function returning void. */ 11736 location_t xloc = expansion_point_location_if_in_system_header (loc); 11737 11738 if (TREE_THIS_VOLATILE (current_function_decl)) 11739 warning_at (xloc, 0, 11740 "function declared %<noreturn%> has a %<return%> statement"); 11741 11742 if (retval) 11743 { 11744 tree semantic_type = NULL_TREE; 11745 npc = null_pointer_constant_p (retval); 11746 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR) 11747 { 11748 semantic_type = TREE_TYPE (retval); 11749 retval = TREE_OPERAND (retval, 0); 11750 } 11751 retval = c_fully_fold (retval, false, NULL); 11752 if (semantic_type 11753 && valtype != NULL_TREE 11754 && TREE_CODE (valtype) != VOID_TYPE) 11755 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval); 11756 } 11757 11758 if (!retval) 11759 { 11760 current_function_returns_null = 1; 11761 if ((warn_return_type >= 0 || flag_isoc99) 11762 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE) 11763 { 11764 no_warning = true; 11765 if (emit_diagnostic (flag_isoc99 ? DK_PERMERROR : DK_WARNING, 11766 loc, OPT_Wreturn_mismatch, 11767 "%<return%> with no value," 11768 " in function returning non-void")) 11769 inform (DECL_SOURCE_LOCATION (current_function_decl), 11770 "declared here"); 11771 } 11772 } 11773 else if (valtype == NULL_TREE || VOID_TYPE_P (valtype)) 11774 { 11775 current_function_returns_null = 1; 11776 bool warned_here; 11777 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) 11778 warned_here = permerror_opt 11779 (xloc, OPT_Wreturn_mismatch, 11780 "%<return%> with a value, in function returning void"); 11781 else 11782 warned_here = pedwarn 11783 (xloc, OPT_Wpedantic, "ISO C forbids " 11784 "%<return%> with expression, in function returning void"); 11785 if (warned_here) 11786 inform (DECL_SOURCE_LOCATION (current_function_decl), 11787 "declared here"); 11788 } 11789 else 11790 { 11791 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype, 11792 retval, origtype, ic_return, 11793 npc, NULL_TREE, NULL_TREE, 0); 11794 tree res = DECL_RESULT (current_function_decl); 11795 tree inner; 11796 bool save; 11797 11798 current_function_returns_value = 1; 11799 if (t == error_mark_node) 11800 return NULL_TREE; 11801 11802 save = in_late_binary_op; 11803 if (C_BOOLEAN_TYPE_P (TREE_TYPE (res)) 11804 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE 11805 || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t)) 11806 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE 11807 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE) 11808 && sanitize_flags_p (SANITIZE_FLOAT_CAST))) 11809 in_late_binary_op = true; 11810 inner = t = convert (TREE_TYPE (res), t); 11811 in_late_binary_op = save; 11812 11813 /* Strip any conversions, additions, and subtractions, and see if 11814 we are returning the address of a local variable. Warn if so. */ 11815 while (1) 11816 { 11817 switch (TREE_CODE (inner)) 11818 { 11819 CASE_CONVERT: 11820 case NON_LVALUE_EXPR: 11821 case PLUS_EXPR: 11822 case POINTER_PLUS_EXPR: 11823 inner = TREE_OPERAND (inner, 0); 11824 continue; 11825 11826 case MINUS_EXPR: 11827 /* If the second operand of the MINUS_EXPR has a pointer 11828 type (or is converted from it), this may be valid, so 11829 don't give a warning. */ 11830 { 11831 tree op1 = TREE_OPERAND (inner, 1); 11832 11833 while (!POINTER_TYPE_P (TREE_TYPE (op1)) 11834 && (CONVERT_EXPR_P (op1) 11835 || TREE_CODE (op1) == NON_LVALUE_EXPR)) 11836 op1 = TREE_OPERAND (op1, 0); 11837 11838 if (POINTER_TYPE_P (TREE_TYPE (op1))) 11839 break; 11840 11841 inner = TREE_OPERAND (inner, 0); 11842 continue; 11843 } 11844 11845 case ADDR_EXPR: 11846 inner = TREE_OPERAND (inner, 0); 11847 11848 while (REFERENCE_CLASS_P (inner) 11849 && !INDIRECT_REF_P (inner)) 11850 inner = TREE_OPERAND (inner, 0); 11851 11852 if (DECL_P (inner) 11853 && !DECL_EXTERNAL (inner) 11854 && !TREE_STATIC (inner) 11855 && DECL_CONTEXT (inner) == current_function_decl 11856 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))) 11857 { 11858 if (TREE_CODE (inner) == LABEL_DECL) 11859 warning_at (loc, OPT_Wreturn_local_addr, 11860 "function returns address of label"); 11861 else 11862 { 11863 warning_at (loc, OPT_Wreturn_local_addr, 11864 "function returns address of local variable"); 11865 tree zero = build_zero_cst (TREE_TYPE (res)); 11866 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero); 11867 } 11868 } 11869 break; 11870 11871 default: 11872 break; 11873 } 11874 11875 break; 11876 } 11877 11878 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t); 11879 SET_EXPR_LOCATION (retval, loc); 11880 11881 if (warn_sequence_point) 11882 verify_sequence_points (retval); 11883 } 11884 11885 ret_stmt = build_stmt (loc, RETURN_EXPR, retval); 11886 if (no_warning) 11887 suppress_warning (ret_stmt, OPT_Wreturn_type); 11888 return add_stmt (ret_stmt); 11889 } 11890 11891 struct c_switch { 11893 /* The SWITCH_STMT being built. */ 11894 tree switch_stmt; 11895 11896 /* The original type of the testing expression, i.e. before the 11897 default conversion is applied. */ 11898 tree orig_type; 11899 11900 /* A splay-tree mapping the low element of a case range to the high 11901 element, or NULL_TREE if there is no high element. Used to 11902 determine whether or not a new case label duplicates an old case 11903 label. We need a tree, rather than simply a hash table, because 11904 of the GNU case range extension. */ 11905 splay_tree cases; 11906 11907 /* The bindings at the point of the switch. This is used for 11908 warnings crossing decls when branching to a case label. */ 11909 struct c_spot_bindings *bindings; 11910 11911 /* Whether the switch includes any break statements. */ 11912 bool break_stmt_seen_p; 11913 11914 /* The next node on the stack. */ 11915 struct c_switch *next; 11916 11917 /* Remember whether the controlling expression had boolean type 11918 before integer promotions for the sake of -Wswitch-bool. */ 11919 bool bool_cond_p; 11920 }; 11921 11922 /* A stack of the currently active switch statements. The innermost 11923 switch statement is on the top of the stack. There is no need to 11924 mark the stack for garbage collection because it is only active 11925 during the processing of the body of a function, and we never 11926 collect at that point. */ 11927 11928 struct c_switch *c_switch_stack; 11929 11930 /* Start a C switch statement, testing expression EXP. Return the new 11931 SWITCH_STMT. SWITCH_LOC is the location of the `switch'. 11932 SWITCH_COND_LOC is the location of the switch's condition. 11933 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */ 11934 11935 tree 11936 c_start_switch (location_t switch_loc, 11937 location_t switch_cond_loc, 11938 tree exp, bool explicit_cast_p) 11939 { 11940 tree orig_type = error_mark_node; 11941 bool bool_cond_p = false; 11942 struct c_switch *cs; 11943 11944 if (exp != error_mark_node) 11945 { 11946 orig_type = TREE_TYPE (exp); 11947 11948 if (!INTEGRAL_TYPE_P (orig_type)) 11949 { 11950 if (orig_type != error_mark_node) 11951 { 11952 error_at (switch_cond_loc, "switch quantity not an integer"); 11953 orig_type = error_mark_node; 11954 } 11955 exp = integer_zero_node; 11956 } 11957 else 11958 { 11959 tree type = TYPE_MAIN_VARIANT (orig_type); 11960 tree e = exp; 11961 11962 /* Warn if the condition has boolean value. */ 11963 while (TREE_CODE (e) == COMPOUND_EXPR) 11964 e = TREE_OPERAND (e, 1); 11965 11966 if ((C_BOOLEAN_TYPE_P (type) 11967 || truth_value_p (TREE_CODE (e))) 11968 /* Explicit cast to int suppresses this warning. */ 11969 && !(TREE_CODE (type) == INTEGER_TYPE 11970 && explicit_cast_p)) 11971 bool_cond_p = true; 11972 11973 if (!in_system_header_at (input_location) 11974 && (type == long_integer_type_node 11975 || type == long_unsigned_type_node)) 11976 warning_at (switch_cond_loc, 11977 OPT_Wtraditional, "%<long%> switch expression not " 11978 "converted to %<int%> in ISO C"); 11979 11980 exp = c_fully_fold (exp, false, NULL); 11981 exp = default_conversion (exp); 11982 11983 if (warn_sequence_point) 11984 verify_sequence_points (exp); 11985 } 11986 } 11987 11988 /* Add this new SWITCH_STMT to the stack. */ 11989 cs = XNEW (struct c_switch); 11990 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp, 11991 NULL_TREE, orig_type, NULL_TREE); 11992 cs->orig_type = orig_type; 11993 cs->cases = splay_tree_new (case_compare, NULL, NULL); 11994 cs->bindings = c_get_switch_bindings (); 11995 cs->break_stmt_seen_p = false; 11996 cs->bool_cond_p = bool_cond_p; 11997 cs->next = c_switch_stack; 11998 c_switch_stack = cs; 11999 12000 return add_stmt (cs->switch_stmt); 12001 } 12002 12003 /* Process a case label at location LOC, with attributes ATTRS. */ 12004 12005 tree 12006 do_case (location_t loc, tree low_value, tree high_value, tree attrs) 12007 { 12008 tree label = NULL_TREE; 12009 12010 if (low_value && TREE_CODE (low_value) != INTEGER_CST) 12011 { 12012 low_value = c_fully_fold (low_value, false, NULL); 12013 if (TREE_CODE (low_value) == INTEGER_CST) 12014 pedwarn (loc, OPT_Wpedantic, 12015 "case label is not an integer constant expression"); 12016 } 12017 12018 if (high_value && TREE_CODE (high_value) != INTEGER_CST) 12019 { 12020 high_value = c_fully_fold (high_value, false, NULL); 12021 if (TREE_CODE (high_value) == INTEGER_CST) 12022 pedwarn (input_location, OPT_Wpedantic, 12023 "case label is not an integer constant expression"); 12024 } 12025 12026 if (c_switch_stack == NULL) 12027 { 12028 if (low_value) 12029 error_at (loc, "case label not within a switch statement"); 12030 else 12031 error_at (loc, "%<default%> label not within a switch statement"); 12032 return NULL_TREE; 12033 } 12034 12035 if (c_check_switch_jump_warnings (c_switch_stack->bindings, 12036 EXPR_LOCATION (c_switch_stack->switch_stmt), 12037 loc)) 12038 return NULL_TREE; 12039 12040 label = c_add_case_label (loc, c_switch_stack->cases, 12041 SWITCH_STMT_COND (c_switch_stack->switch_stmt), 12042 low_value, high_value, attrs); 12043 if (label == error_mark_node) 12044 label = NULL_TREE; 12045 return label; 12046 } 12047 12048 /* Finish the switch statement. TYPE is the original type of the 12049 controlling expression of the switch, or NULL_TREE. */ 12050 12051 void 12052 c_finish_switch (tree body, tree type) 12053 { 12054 struct c_switch *cs = c_switch_stack; 12055 location_t switch_location; 12056 12057 SWITCH_STMT_BODY (cs->switch_stmt) = body; 12058 12059 /* Emit warnings as needed. */ 12060 switch_location = EXPR_LOCATION (cs->switch_stmt); 12061 c_do_switch_warnings (cs->cases, switch_location, 12062 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt), 12063 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p); 12064 if (c_switch_covers_all_cases_p (cs->cases, 12065 SWITCH_STMT_TYPE (cs->switch_stmt))) 12066 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1; 12067 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p; 12068 12069 /* Pop the stack. */ 12070 c_switch_stack = cs->next; 12071 splay_tree_delete (cs->cases); 12072 c_release_switch_bindings (cs->bindings); 12073 XDELETE (cs); 12074 } 12075 12076 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND, 12078 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK 12079 may be null. */ 12080 12081 void 12082 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block, 12083 tree else_block) 12084 { 12085 tree stmt; 12086 12087 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block); 12088 SET_EXPR_LOCATION (stmt, if_locus); 12089 add_stmt (stmt); 12090 } 12091 12092 tree 12093 c_finish_bc_stmt (location_t loc, tree label, bool is_break) 12094 { 12095 /* In switch statements break is sometimes stylistically used after 12096 a return statement. This can lead to spurious warnings about 12097 control reaching the end of a non-void function when it is 12098 inlined. Note that we are calling block_may_fallthru with 12099 language specific tree nodes; this works because 12100 block_may_fallthru returns true when given something it does not 12101 understand. */ 12102 bool skip = !block_may_fallthru (cur_stmt_list); 12103 12104 if (is_break) 12105 switch (in_statement) 12106 { 12107 case 0: 12108 error_at (loc, "break statement not within loop or switch"); 12109 return NULL_TREE; 12110 case IN_OMP_BLOCK: 12111 error_at (loc, "invalid exit from OpenMP structured block"); 12112 return NULL_TREE; 12113 case IN_OMP_FOR: 12114 error_at (loc, "break statement used with OpenMP for loop"); 12115 return NULL_TREE; 12116 case IN_ITERATION_STMT: 12117 case IN_OBJC_FOREACH: 12118 break; 12119 default: 12120 gcc_assert (in_statement & IN_SWITCH_STMT); 12121 c_switch_stack->break_stmt_seen_p = true; 12122 break; 12123 } 12124 else 12125 switch (in_statement & ~IN_SWITCH_STMT) 12126 { 12127 case 0: 12128 error_at (loc, "continue statement not within a loop"); 12129 return NULL_TREE; 12130 case IN_OMP_BLOCK: 12131 error_at (loc, "invalid exit from OpenMP structured block"); 12132 return NULL_TREE; 12133 case IN_ITERATION_STMT: 12134 case IN_OMP_FOR: 12135 case IN_OBJC_FOREACH: 12136 break; 12137 default: 12138 gcc_unreachable (); 12139 } 12140 12141 if (skip) 12142 return NULL_TREE; 12143 else if ((in_statement & IN_OBJC_FOREACH) 12144 && !(is_break && (in_statement & IN_SWITCH_STMT))) 12145 { 12146 /* The foreach expander produces low-level code using gotos instead 12147 of a structured loop construct. */ 12148 gcc_assert (label); 12149 return add_stmt (build_stmt (loc, GOTO_EXPR, label)); 12150 } 12151 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT))); 12152 } 12153 12154 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */ 12155 12156 static void 12157 emit_side_effect_warnings (location_t loc, tree expr) 12158 { 12159 maybe_warn_nodiscard (loc, expr); 12160 if (!warn_unused_value) 12161 return; 12162 if (expr == error_mark_node) 12163 ; 12164 else if (!TREE_SIDE_EFFECTS (expr)) 12165 { 12166 if (!VOID_TYPE_P (TREE_TYPE (expr)) 12167 && !warning_suppressed_p (expr, OPT_Wunused_value)) 12168 warning_at (loc, OPT_Wunused_value, "statement with no effect"); 12169 } 12170 else if (TREE_CODE (expr) == COMPOUND_EXPR) 12171 { 12172 tree r = expr; 12173 location_t cloc = loc; 12174 while (TREE_CODE (r) == COMPOUND_EXPR) 12175 { 12176 if (EXPR_HAS_LOCATION (r)) 12177 cloc = EXPR_LOCATION (r); 12178 r = TREE_OPERAND (r, 1); 12179 } 12180 if (!TREE_SIDE_EFFECTS (r) 12181 && !VOID_TYPE_P (TREE_TYPE (r)) 12182 && !CONVERT_EXPR_P (r) 12183 && !warning_suppressed_p (r, OPT_Wunused_value) 12184 && !warning_suppressed_p (expr, OPT_Wunused_value)) 12185 warning_at (cloc, OPT_Wunused_value, 12186 "right-hand operand of comma expression has no effect"); 12187 } 12188 else 12189 warn_if_unused_value (expr, loc); 12190 } 12191 12192 /* Process an expression as if it were a complete statement. Emit 12193 diagnostics, but do not call ADD_STMT. LOC is the location of the 12194 statement. */ 12195 12196 tree 12197 c_process_expr_stmt (location_t loc, tree expr) 12198 { 12199 tree exprv; 12200 12201 if (!expr) 12202 return NULL_TREE; 12203 12204 expr = c_fully_fold (expr, false, NULL); 12205 12206 if (warn_sequence_point) 12207 verify_sequence_points (expr); 12208 12209 if (TREE_TYPE (expr) != error_mark_node 12210 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) 12211 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) 12212 error_at (loc, "expression statement has incomplete type"); 12213 12214 /* If we're not processing a statement expression, warn about unused values. 12215 Warnings for statement expressions will be emitted later, once we figure 12216 out which is the result. */ 12217 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list) 12218 && (warn_unused_value || warn_unused_result)) 12219 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr); 12220 12221 exprv = expr; 12222 while (TREE_CODE (exprv) == COMPOUND_EXPR) 12223 exprv = TREE_OPERAND (exprv, 1); 12224 while (CONVERT_EXPR_P (exprv)) 12225 exprv = TREE_OPERAND (exprv, 0); 12226 if (DECL_P (exprv) 12227 || handled_component_p (exprv) 12228 || TREE_CODE (exprv) == ADDR_EXPR) 12229 mark_exp_read (exprv); 12230 12231 /* If the expression is not of a type to which we cannot assign a line 12232 number, wrap the thing in a no-op NOP_EXPR. */ 12233 if (DECL_P (expr) || CONSTANT_CLASS_P (expr)) 12234 { 12235 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr); 12236 SET_EXPR_LOCATION (expr, loc); 12237 } 12238 12239 return expr; 12240 } 12241 12242 /* Emit an expression as a statement. LOC is the location of the 12243 expression. */ 12244 12245 tree 12246 c_finish_expr_stmt (location_t loc, tree expr) 12247 { 12248 if (expr) 12249 return add_stmt (c_process_expr_stmt (loc, expr)); 12250 else 12251 return NULL; 12252 } 12253 12254 /* Do the opposite and emit a statement as an expression. To begin, 12255 create a new binding level and return it. */ 12256 12257 tree 12258 c_begin_stmt_expr (void) 12259 { 12260 tree ret; 12261 12262 /* We must force a BLOCK for this level so that, if it is not expanded 12263 later, there is a way to turn off the entire subtree of blocks that 12264 are contained in it. */ 12265 keep_next_level (); 12266 ret = c_begin_compound_stmt (true); 12267 12268 c_bindings_start_stmt_expr (c_switch_stack == NULL 12269 ? NULL 12270 : c_switch_stack->bindings); 12271 12272 /* Mark the current statement list as belonging to a statement list. */ 12273 STATEMENT_LIST_STMT_EXPR (ret) = 1; 12274 12275 return ret; 12276 } 12277 12278 /* LOC is the location of the compound statement to which this body 12279 belongs. */ 12280 12281 tree 12282 c_finish_stmt_expr (location_t loc, tree body) 12283 { 12284 tree last, type, tmp, val; 12285 tree *last_p; 12286 12287 body = c_end_compound_stmt (loc, body, true); 12288 12289 c_bindings_end_stmt_expr (c_switch_stack == NULL 12290 ? NULL 12291 : c_switch_stack->bindings); 12292 12293 /* Locate the last statement in BODY. See c_end_compound_stmt 12294 about always returning a BIND_EXPR. */ 12295 last_p = &BIND_EXPR_BODY (body); 12296 last = BIND_EXPR_BODY (body); 12297 12298 continue_searching: 12299 if (TREE_CODE (last) == STATEMENT_LIST) 12300 { 12301 tree_stmt_iterator l = tsi_last (last); 12302 12303 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT) 12304 tsi_prev (&l); 12305 12306 /* This can happen with degenerate cases like ({ }). No value. */ 12307 if (tsi_end_p (l)) 12308 return body; 12309 12310 /* If we're supposed to generate side effects warnings, process 12311 all of the statements except the last. */ 12312 if (warn_unused_value || warn_unused_result) 12313 { 12314 for (tree_stmt_iterator i = tsi_start (last); 12315 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i)) 12316 { 12317 location_t tloc; 12318 tree t = tsi_stmt (i); 12319 12320 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc; 12321 emit_side_effect_warnings (tloc, t); 12322 } 12323 } 12324 last_p = tsi_stmt_ptr (l); 12325 last = *last_p; 12326 } 12327 12328 /* If the end of the list is exception related, then the list was split 12329 by a call to push_cleanup. Continue searching. */ 12330 if (TREE_CODE (last) == TRY_FINALLY_EXPR 12331 || TREE_CODE (last) == TRY_CATCH_EXPR) 12332 { 12333 last_p = &TREE_OPERAND (last, 0); 12334 last = *last_p; 12335 goto continue_searching; 12336 } 12337 12338 if (last == error_mark_node) 12339 return last; 12340 12341 /* In the case that the BIND_EXPR is not necessary, return the 12342 expression out from inside it. */ 12343 if ((last == BIND_EXPR_BODY (body) 12344 /* Skip nested debug stmts. */ 12345 || last == expr_first (BIND_EXPR_BODY (body))) 12346 && BIND_EXPR_VARS (body) == NULL) 12347 { 12348 /* Even if this looks constant, do not allow it in a constant 12349 expression. */ 12350 last = c_wrap_maybe_const (last, true); 12351 /* Do not warn if the return value of a statement expression is 12352 unused. */ 12353 suppress_warning (last, OPT_Wunused); 12354 return last; 12355 } 12356 12357 /* Extract the type of said expression. */ 12358 type = TREE_TYPE (last); 12359 12360 /* If we're not returning a value at all, then the BIND_EXPR that 12361 we already have is a fine expression to return. */ 12362 if (!type || VOID_TYPE_P (type)) 12363 return body; 12364 12365 /* Now that we've located the expression containing the value, it seems 12366 silly to make voidify_wrapper_expr repeat the process. Create a 12367 temporary of the appropriate type and stick it in a TARGET_EXPR. */ 12368 tmp = create_tmp_var_raw (type); 12369 12370 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids 12371 tree_expr_nonnegative_p giving up immediately. */ 12372 val = last; 12373 if (TREE_CODE (val) == NOP_EXPR 12374 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))) 12375 val = TREE_OPERAND (val, 0); 12376 12377 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val); 12378 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last)); 12379 12380 { 12381 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE); 12382 SET_EXPR_LOCATION (t, loc); 12383 return t; 12384 } 12385 } 12386 12387 /* Begin and end compound statements. This is as simple as pushing 12389 and popping new statement lists from the tree. */ 12390 12391 tree 12392 c_begin_compound_stmt (bool do_scope) 12393 { 12394 tree stmt = push_stmt_list (); 12395 if (do_scope) 12396 push_scope (); 12397 return stmt; 12398 } 12399 12400 /* End a compound statement. STMT is the statement. LOC is the 12401 location of the compound statement-- this is usually the location 12402 of the opening brace. */ 12403 12404 tree 12405 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope) 12406 { 12407 tree block = NULL; 12408 12409 if (do_scope) 12410 { 12411 if (c_dialect_objc ()) 12412 objc_clear_super_receiver (); 12413 block = pop_scope (); 12414 } 12415 12416 stmt = pop_stmt_list (stmt); 12417 stmt = c_build_bind_expr (loc, block, stmt); 12418 12419 /* If this compound statement is nested immediately inside a statement 12420 expression, then force a BIND_EXPR to be created. Otherwise we'll 12421 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular, 12422 STATEMENT_LISTs merge, and thus we can lose track of what statement 12423 was really last. */ 12424 if (building_stmt_list_p () 12425 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list) 12426 && TREE_CODE (stmt) != BIND_EXPR) 12427 { 12428 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL); 12429 TREE_SIDE_EFFECTS (stmt) = 1; 12430 SET_EXPR_LOCATION (stmt, loc); 12431 } 12432 12433 return stmt; 12434 } 12435 12436 /* Queue a cleanup. CLEANUP is an expression/statement to be executed 12437 when the current scope is exited. EH_ONLY is true when this is not 12438 meant to apply to normal control flow transfer. */ 12439 12440 void 12441 push_cleanup (tree decl, tree cleanup, bool eh_only) 12442 { 12443 enum tree_code code; 12444 tree stmt, list; 12445 bool stmt_expr; 12446 12447 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR; 12448 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup); 12449 add_stmt (stmt); 12450 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list); 12451 list = push_stmt_list (); 12452 TREE_OPERAND (stmt, 0) = list; 12453 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr; 12454 } 12455 12456 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode 12458 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */ 12459 12460 static tree 12461 build_vec_cmp (tree_code code, tree type, 12462 tree arg0, tree arg1) 12463 { 12464 tree zero_vec = build_zero_cst (type); 12465 tree minus_one_vec = build_minus_one_cst (type); 12466 tree cmp_type = truth_type_for (TREE_TYPE (arg0)); 12467 tree cmp = build2 (code, cmp_type, arg0, arg1); 12468 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec); 12469 } 12470 12471 /* Possibly warn about an address of OP never being NULL in a comparison 12472 operation CODE involving null. */ 12473 12474 static void 12475 maybe_warn_for_null_address (location_t loc, tree op, tree_code code) 12476 { 12477 /* Prevent warnings issued for macro expansion. */ 12478 if (!warn_address 12479 || warning_suppressed_p (op, OPT_Waddress) 12480 || from_macro_expansion_at (loc)) 12481 return; 12482 12483 if (TREE_CODE (op) == NOP_EXPR) 12484 { 12485 /* Allow casts to intptr_t to suppress the warning. */ 12486 tree type = TREE_TYPE (op); 12487 if (TREE_CODE (type) == INTEGER_TYPE) 12488 return; 12489 op = TREE_OPERAND (op, 0); 12490 } 12491 12492 if (TREE_CODE (op) == POINTER_PLUS_EXPR) 12493 { 12494 /* Allow a cast to void* to suppress the warning. */ 12495 tree type = TREE_TYPE (TREE_TYPE (op)); 12496 if (VOID_TYPE_P (type)) 12497 return; 12498 12499 /* Adding any value to a null pointer, including zero, is undefined 12500 in C. This includes the expression &p[0] where p is the null 12501 pointer, although &p[0] will have been folded to p by this point 12502 and so not diagnosed. */ 12503 if (code == EQ_EXPR) 12504 warning_at (loc, OPT_Waddress, 12505 "the comparison will always evaluate as %<false%> " 12506 "for the pointer operand in %qE must not be NULL", 12507 op); 12508 else 12509 warning_at (loc, OPT_Waddress, 12510 "the comparison will always evaluate as %<true%> " 12511 "for the pointer operand in %qE must not be NULL", 12512 op); 12513 12514 return; 12515 } 12516 12517 if (TREE_CODE (op) != ADDR_EXPR) 12518 return; 12519 12520 op = TREE_OPERAND (op, 0); 12521 12522 if (TREE_CODE (op) == IMAGPART_EXPR 12523 || TREE_CODE (op) == REALPART_EXPR) 12524 { 12525 /* The address of either complex part may not be null. */ 12526 if (code == EQ_EXPR) 12527 warning_at (loc, OPT_Waddress, 12528 "the comparison will always evaluate as %<false%> " 12529 "for the address of %qE will never be NULL", 12530 op); 12531 else 12532 warning_at (loc, OPT_Waddress, 12533 "the comparison will always evaluate as %<true%> " 12534 "for the address of %qE will never be NULL", 12535 op); 12536 return; 12537 } 12538 12539 /* Set to true in the loop below if OP dereferences is operand. 12540 In such a case the ultimate target need not be a decl for 12541 the null [in]equality test to be constant. */ 12542 bool deref = false; 12543 12544 /* Get the outermost array or object, or member. */ 12545 while (handled_component_p (op)) 12546 { 12547 if (TREE_CODE (op) == COMPONENT_REF) 12548 { 12549 /* Get the member (its address is never null). */ 12550 op = TREE_OPERAND (op, 1); 12551 break; 12552 } 12553 12554 /* Get the outer array/object to refer to in the warning. */ 12555 op = TREE_OPERAND (op, 0); 12556 deref = true; 12557 } 12558 12559 if ((!deref && !decl_with_nonnull_addr_p (op)) 12560 || from_macro_expansion_at (loc)) 12561 return; 12562 12563 bool w; 12564 if (code == EQ_EXPR) 12565 w = warning_at (loc, OPT_Waddress, 12566 "the comparison will always evaluate as %<false%> " 12567 "for the address of %qE will never be NULL", 12568 op); 12569 else 12570 w = warning_at (loc, OPT_Waddress, 12571 "the comparison will always evaluate as %<true%> " 12572 "for the address of %qE will never be NULL", 12573 op); 12574 12575 if (w && DECL_P (op)) 12576 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op); 12577 } 12578 12579 /* Build a binary-operation expression without default conversions. 12580 CODE is the kind of expression to build. 12581 LOCATION is the operator's location. 12582 This function differs from `build' in several ways: 12583 the data type of the result is computed and recorded in it, 12584 warnings are generated if arg data types are invalid, 12585 special handling for addition and subtraction of pointers is known, 12586 and some optimization is done (operations on narrow ints 12587 are done in the narrower type when that gives the same result). 12588 Constant folding is also done before the result is returned. 12589 12590 Note that the operands will never have enumeral types, or function 12591 or array types, because either they will have the default conversions 12592 performed or they have both just been converted to some other type in which 12593 the arithmetic is to be done. */ 12594 12595 tree 12596 build_binary_op (location_t location, enum tree_code code, 12597 tree orig_op0, tree orig_op1, bool convert_p) 12598 { 12599 tree type0, type1, orig_type0, orig_type1; 12600 tree eptype; 12601 enum tree_code code0, code1; 12602 tree op0, op1; 12603 tree ret = error_mark_node; 12604 const char *invalid_op_diag; 12605 bool op0_int_operands, op1_int_operands; 12606 bool int_const, int_const_or_overflow, int_operands; 12607 12608 /* Expression code to give to the expression when it is built. 12609 Normally this is CODE, which is what the caller asked for, 12610 but in some special cases we change it. */ 12611 enum tree_code resultcode = code; 12612 12613 /* Data type in which the computation is to be performed. 12614 In the simplest cases this is the common type of the arguments. */ 12615 tree result_type = NULL; 12616 12617 /* When the computation is in excess precision, the type of the 12618 final EXCESS_PRECISION_EXPR. */ 12619 tree semantic_result_type = NULL; 12620 12621 /* Nonzero means operands have already been type-converted 12622 in whatever way is necessary. 12623 Zero means they need to be converted to RESULT_TYPE. */ 12624 int converted = 0; 12625 12626 /* Nonzero means create the expression with this type, rather than 12627 RESULT_TYPE. */ 12628 tree build_type = NULL_TREE; 12629 12630 /* Nonzero means after finally constructing the expression 12631 convert it to this type. */ 12632 tree final_type = NULL_TREE; 12633 12634 /* Nonzero if this is an operation like MIN or MAX which can 12635 safely be computed in short if both args are promoted shorts. 12636 Also implies COMMON. 12637 -1 indicates a bitwise operation; this makes a difference 12638 in the exact conditions for when it is safe to do the operation 12639 in a narrower mode. */ 12640 int shorten = 0; 12641 12642 /* Nonzero if this is a comparison operation; 12643 if both args are promoted shorts, compare the original shorts. 12644 Also implies COMMON. */ 12645 int short_compare = 0; 12646 12647 /* Nonzero if this is a right-shift operation, which can be computed on the 12648 original short and then promoted if the operand is a promoted short. */ 12649 int short_shift = 0; 12650 12651 /* Nonzero means set RESULT_TYPE to the common type of the args. */ 12652 int common = 0; 12653 12654 /* True means types are compatible as far as ObjC is concerned. */ 12655 bool objc_ok; 12656 12657 /* True means this is an arithmetic operation that may need excess 12658 precision. */ 12659 bool may_need_excess_precision; 12660 12661 /* True means this is a boolean operation that converts both its 12662 operands to truth-values. */ 12663 bool boolean_op = false; 12664 12665 /* Remember whether we're doing / or %. */ 12666 bool doing_div_or_mod = false; 12667 12668 /* Remember whether we're doing << or >>. */ 12669 bool doing_shift = false; 12670 12671 /* Tree holding instrumentation expression. */ 12672 tree instrument_expr = NULL; 12673 12674 if (location == UNKNOWN_LOCATION) 12675 location = input_location; 12676 12677 op0 = orig_op0; 12678 op1 = orig_op1; 12679 12680 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0); 12681 if (op0_int_operands) 12682 op0 = remove_c_maybe_const_expr (op0); 12683 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); 12684 if (op1_int_operands) 12685 op1 = remove_c_maybe_const_expr (op1); 12686 int_operands = (op0_int_operands && op1_int_operands); 12687 if (int_operands) 12688 { 12689 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST 12690 && TREE_CODE (orig_op1) == INTEGER_CST); 12691 int_const = (int_const_or_overflow 12692 && !TREE_OVERFLOW (orig_op0) 12693 && !TREE_OVERFLOW (orig_op1)); 12694 } 12695 else 12696 int_const = int_const_or_overflow = false; 12697 12698 /* Do not apply default conversion in mixed vector/scalar expression. */ 12699 if (convert_p 12700 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1))) 12701 { 12702 op0 = default_conversion (op0); 12703 op1 = default_conversion (op1); 12704 } 12705 12706 orig_type0 = type0 = TREE_TYPE (op0); 12707 12708 orig_type1 = type1 = TREE_TYPE (op1); 12709 12710 /* The expression codes of the data types of the arguments tell us 12711 whether the arguments are integers, floating, pointers, etc. */ 12712 code0 = TREE_CODE (type0); 12713 code1 = TREE_CODE (type1); 12714 12715 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ 12716 STRIP_TYPE_NOPS (op0); 12717 STRIP_TYPE_NOPS (op1); 12718 12719 /* If an error was already reported for one of the arguments, 12720 avoid reporting another error. */ 12721 12722 if (code0 == ERROR_MARK || code1 == ERROR_MARK) 12723 return error_mark_node; 12724 12725 if (code0 == POINTER_TYPE 12726 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0))) 12727 return error_mark_node; 12728 12729 if (code1 == POINTER_TYPE 12730 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1))) 12731 return error_mark_node; 12732 12733 if ((invalid_op_diag 12734 = targetm.invalid_binary_op (code, type0, type1))) 12735 { 12736 error_at (location, invalid_op_diag); 12737 return error_mark_node; 12738 } 12739 12740 switch (code) 12741 { 12742 case PLUS_EXPR: 12743 case MINUS_EXPR: 12744 case MULT_EXPR: 12745 case TRUNC_DIV_EXPR: 12746 case CEIL_DIV_EXPR: 12747 case FLOOR_DIV_EXPR: 12748 case ROUND_DIV_EXPR: 12749 case EXACT_DIV_EXPR: 12750 may_need_excess_precision = true; 12751 break; 12752 12753 case EQ_EXPR: 12754 case NE_EXPR: 12755 case LE_EXPR: 12756 case GE_EXPR: 12757 case LT_EXPR: 12758 case GT_EXPR: 12759 /* Excess precision for implicit conversions of integers to 12760 floating point in C11 and later. */ 12761 may_need_excess_precision = (flag_isoc11 12762 && (ANY_INTEGRAL_TYPE_P (type0) 12763 || ANY_INTEGRAL_TYPE_P (type1))); 12764 break; 12765 12766 default: 12767 may_need_excess_precision = false; 12768 break; 12769 } 12770 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR) 12771 { 12772 op0 = TREE_OPERAND (op0, 0); 12773 type0 = TREE_TYPE (op0); 12774 } 12775 else if (may_need_excess_precision 12776 && (eptype = excess_precision_type (type0)) != NULL_TREE) 12777 { 12778 type0 = eptype; 12779 op0 = convert (eptype, op0); 12780 } 12781 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) 12782 { 12783 op1 = TREE_OPERAND (op1, 0); 12784 type1 = TREE_TYPE (op1); 12785 } 12786 else if (may_need_excess_precision 12787 && (eptype = excess_precision_type (type1)) != NULL_TREE) 12788 { 12789 type1 = eptype; 12790 op1 = convert (eptype, op1); 12791 } 12792 12793 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE); 12794 12795 /* In case when one of the operands of the binary operation is 12796 a vector and another is a scalar -- convert scalar to vector. */ 12797 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE) 12798 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE)) 12799 { 12800 enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0, 12801 orig_op1, true); 12802 12803 switch (convert_flag) 12804 { 12805 case stv_error: 12806 return error_mark_node; 12807 case stv_firstarg: 12808 { 12809 bool maybe_const = true; 12810 tree sc; 12811 sc = c_fully_fold (op0, false, &maybe_const); 12812 sc = save_expr (sc); 12813 sc = convert (TREE_TYPE (type1), sc); 12814 op0 = build_vector_from_val (type1, sc); 12815 if (!maybe_const) 12816 op0 = c_wrap_maybe_const (op0, true); 12817 orig_type0 = type0 = TREE_TYPE (op0); 12818 code0 = TREE_CODE (type0); 12819 converted = 1; 12820 break; 12821 } 12822 case stv_secondarg: 12823 { 12824 bool maybe_const = true; 12825 tree sc; 12826 sc = c_fully_fold (op1, false, &maybe_const); 12827 sc = save_expr (sc); 12828 sc = convert (TREE_TYPE (type0), sc); 12829 op1 = build_vector_from_val (type0, sc); 12830 if (!maybe_const) 12831 op1 = c_wrap_maybe_const (op1, true); 12832 orig_type1 = type1 = TREE_TYPE (op1); 12833 code1 = TREE_CODE (type1); 12834 converted = 1; 12835 break; 12836 } 12837 default: 12838 break; 12839 } 12840 } 12841 12842 switch (code) 12843 { 12844 case PLUS_EXPR: 12845 /* Handle the pointer + int case. */ 12846 if (code0 == POINTER_TYPE 12847 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE)) 12848 { 12849 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1); 12850 goto return_build_binary_op; 12851 } 12852 else if (code1 == POINTER_TYPE 12853 && (code0 == INTEGER_TYPE || code0 == BITINT_TYPE)) 12854 { 12855 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0); 12856 goto return_build_binary_op; 12857 } 12858 else 12859 common = 1; 12860 break; 12861 12862 case MINUS_EXPR: 12863 /* Subtraction of two similar pointers. 12864 We must subtract them as integers, then divide by object size. */ 12865 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE 12866 && comp_target_types (location, type0, type1)) 12867 { 12868 ret = pointer_diff (location, op0, op1, &instrument_expr); 12869 goto return_build_binary_op; 12870 } 12871 /* Handle pointer minus int. Just like pointer plus int. */ 12872 else if (code0 == POINTER_TYPE 12873 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE)) 12874 { 12875 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1); 12876 goto return_build_binary_op; 12877 } 12878 else 12879 common = 1; 12880 break; 12881 12882 case MULT_EXPR: 12883 common = 1; 12884 break; 12885 12886 case TRUNC_DIV_EXPR: 12887 case CEIL_DIV_EXPR: 12888 case FLOOR_DIV_EXPR: 12889 case ROUND_DIV_EXPR: 12890 case EXACT_DIV_EXPR: 12891 doing_div_or_mod = true; 12892 warn_for_div_by_zero (location, op1); 12893 12894 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE 12895 || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE 12896 || code0 == COMPLEX_TYPE 12897 || gnu_vector_type_p (type0)) 12898 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE 12899 || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE 12900 || code1 == COMPLEX_TYPE 12901 || gnu_vector_type_p (type1))) 12902 { 12903 enum tree_code tcode0 = code0, tcode1 = code1; 12904 12905 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) 12906 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); 12907 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE) 12908 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); 12909 12910 if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE) 12911 && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE)) 12912 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE))) 12913 resultcode = RDIV_EXPR; 12914 else 12915 /* Although it would be tempting to shorten always here, that 12916 loses on some targets, since the modulo instruction is 12917 undefined if the quotient can't be represented in the 12918 computation mode. We shorten only if unsigned or if 12919 dividing by something we know != -1. */ 12920 shorten = may_shorten_divmod (op0, op1); 12921 common = 1; 12922 } 12923 break; 12924 12925 case BIT_AND_EXPR: 12926 case BIT_IOR_EXPR: 12927 case BIT_XOR_EXPR: 12928 if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE) 12929 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE)) 12930 shorten = -1; 12931 /* Allow vector types which are not floating point types. */ 12932 else if (gnu_vector_type_p (type0) 12933 && gnu_vector_type_p (type1) 12934 && !VECTOR_FLOAT_TYPE_P (type0) 12935 && !VECTOR_FLOAT_TYPE_P (type1)) 12936 common = 1; 12937 break; 12938 12939 case TRUNC_MOD_EXPR: 12940 case FLOOR_MOD_EXPR: 12941 doing_div_or_mod = true; 12942 warn_for_div_by_zero (location, op1); 12943 12944 if (gnu_vector_type_p (type0) 12945 && gnu_vector_type_p (type1) 12946 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE 12947 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) 12948 common = 1; 12949 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE) 12950 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE)) 12951 { 12952 /* Although it would be tempting to shorten always here, that loses 12953 on some targets, since the modulo instruction is undefined if the 12954 quotient can't be represented in the computation mode. We shorten 12955 only if unsigned or if dividing by something we know != -1. */ 12956 shorten = may_shorten_divmod (op0, op1); 12957 common = 1; 12958 } 12959 break; 12960 12961 case TRUTH_ANDIF_EXPR: 12962 case TRUTH_ORIF_EXPR: 12963 case TRUTH_AND_EXPR: 12964 case TRUTH_OR_EXPR: 12965 case TRUTH_XOR_EXPR: 12966 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE 12967 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE 12968 || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE 12969 || code0 == BITINT_TYPE) 12970 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE 12971 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE 12972 || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE 12973 || code1 == BITINT_TYPE)) 12974 { 12975 /* Result of these operations is always an int, 12976 but that does not mean the operands should be 12977 converted to ints! */ 12978 result_type = integer_type_node; 12979 if (op0_int_operands) 12980 { 12981 op0 = c_objc_common_truthvalue_conversion (location, orig_op0); 12982 op0 = remove_c_maybe_const_expr (op0); 12983 } 12984 else 12985 op0 = c_objc_common_truthvalue_conversion (location, op0); 12986 if (op1_int_operands) 12987 { 12988 op1 = c_objc_common_truthvalue_conversion (location, orig_op1); 12989 op1 = remove_c_maybe_const_expr (op1); 12990 } 12991 else 12992 op1 = c_objc_common_truthvalue_conversion (location, op1); 12993 converted = 1; 12994 boolean_op = true; 12995 } 12996 if (code == TRUTH_ANDIF_EXPR) 12997 { 12998 int_const_or_overflow = (int_operands 12999 && TREE_CODE (orig_op0) == INTEGER_CST 13000 && (op0 == truthvalue_false_node 13001 || TREE_CODE (orig_op1) == INTEGER_CST)); 13002 int_const = (int_const_or_overflow 13003 && !TREE_OVERFLOW (orig_op0) 13004 && (op0 == truthvalue_false_node 13005 || !TREE_OVERFLOW (orig_op1))); 13006 } 13007 else if (code == TRUTH_ORIF_EXPR) 13008 { 13009 int_const_or_overflow = (int_operands 13010 && TREE_CODE (orig_op0) == INTEGER_CST 13011 && (op0 == truthvalue_true_node 13012 || TREE_CODE (orig_op1) == INTEGER_CST)); 13013 int_const = (int_const_or_overflow 13014 && !TREE_OVERFLOW (orig_op0) 13015 && (op0 == truthvalue_true_node 13016 || !TREE_OVERFLOW (orig_op1))); 13017 } 13018 break; 13019 13020 /* Shift operations: result has same type as first operand; 13021 always convert second operand to int. 13022 Also set SHORT_SHIFT if shifting rightward. */ 13023 13024 case RSHIFT_EXPR: 13025 if (gnu_vector_type_p (type0) 13026 && gnu_vector_type_p (type1) 13027 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE 13028 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE 13029 && known_eq (TYPE_VECTOR_SUBPARTS (type0), 13030 TYPE_VECTOR_SUBPARTS (type1))) 13031 { 13032 result_type = type0; 13033 converted = 1; 13034 } 13035 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE 13036 || code0 == BITINT_TYPE 13037 || (gnu_vector_type_p (type0) 13038 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)) 13039 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE)) 13040 { 13041 doing_shift = true; 13042 if (TREE_CODE (op1) == INTEGER_CST) 13043 { 13044 if (tree_int_cst_sgn (op1) < 0) 13045 { 13046 int_const = false; 13047 if (c_inhibit_evaluation_warnings == 0) 13048 warning_at (location, OPT_Wshift_count_negative, 13049 "right shift count is negative"); 13050 } 13051 else if (code0 == VECTOR_TYPE) 13052 { 13053 if (compare_tree_int (op1, 13054 TYPE_PRECISION (TREE_TYPE (type0))) 13055 >= 0) 13056 { 13057 int_const = false; 13058 if (c_inhibit_evaluation_warnings == 0) 13059 warning_at (location, OPT_Wshift_count_overflow, 13060 "right shift count >= width of vector element"); 13061 } 13062 } 13063 else 13064 { 13065 if (!integer_zerop (op1)) 13066 short_shift = 1; 13067 13068 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) 13069 { 13070 int_const = false; 13071 if (c_inhibit_evaluation_warnings == 0) 13072 warning_at (location, OPT_Wshift_count_overflow, 13073 "right shift count >= width of type"); 13074 } 13075 } 13076 } 13077 13078 /* Use the type of the value to be shifted. */ 13079 result_type = type0; 13080 /* Avoid converting op1 to result_type later. */ 13081 converted = 1; 13082 } 13083 break; 13084 13085 case LSHIFT_EXPR: 13086 if (gnu_vector_type_p (type0) 13087 && gnu_vector_type_p (type1) 13088 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE 13089 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE 13090 && known_eq (TYPE_VECTOR_SUBPARTS (type0), 13091 TYPE_VECTOR_SUBPARTS (type1))) 13092 { 13093 result_type = type0; 13094 converted = 1; 13095 } 13096 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE 13097 || code0 == BITINT_TYPE 13098 || (gnu_vector_type_p (type0) 13099 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)) 13100 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE)) 13101 { 13102 doing_shift = true; 13103 if (TREE_CODE (op0) == INTEGER_CST 13104 && tree_int_cst_sgn (op0) < 0 13105 && !TYPE_OVERFLOW_WRAPS (type0)) 13106 { 13107 /* Don't reject a left shift of a negative value in a context 13108 where a constant expression is needed in C90. */ 13109 if (flag_isoc99) 13110 int_const = false; 13111 if (c_inhibit_evaluation_warnings == 0) 13112 warning_at (location, OPT_Wshift_negative_value, 13113 "left shift of negative value"); 13114 } 13115 if (TREE_CODE (op1) == INTEGER_CST) 13116 { 13117 if (tree_int_cst_sgn (op1) < 0) 13118 { 13119 int_const = false; 13120 if (c_inhibit_evaluation_warnings == 0) 13121 warning_at (location, OPT_Wshift_count_negative, 13122 "left shift count is negative"); 13123 } 13124 else if (code0 == VECTOR_TYPE) 13125 { 13126 if (compare_tree_int (op1, 13127 TYPE_PRECISION (TREE_TYPE (type0))) 13128 >= 0) 13129 { 13130 int_const = false; 13131 if (c_inhibit_evaluation_warnings == 0) 13132 warning_at (location, OPT_Wshift_count_overflow, 13133 "left shift count >= width of vector element"); 13134 } 13135 } 13136 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) 13137 { 13138 int_const = false; 13139 if (c_inhibit_evaluation_warnings == 0) 13140 warning_at (location, OPT_Wshift_count_overflow, 13141 "left shift count >= width of type"); 13142 } 13143 else if (TREE_CODE (op0) == INTEGER_CST 13144 && maybe_warn_shift_overflow (location, op0, op1) 13145 && flag_isoc99) 13146 int_const = false; 13147 } 13148 13149 /* Use the type of the value to be shifted. */ 13150 result_type = type0; 13151 /* Avoid converting op1 to result_type later. */ 13152 converted = 1; 13153 } 13154 break; 13155 13156 case EQ_EXPR: 13157 case NE_EXPR: 13158 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1)) 13159 { 13160 tree intt; 13161 if (!vector_types_compatible_elements_p (type0, type1)) 13162 { 13163 error_at (location, "comparing vectors with different " 13164 "element types"); 13165 return error_mark_node; 13166 } 13167 13168 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0), 13169 TYPE_VECTOR_SUBPARTS (type1))) 13170 { 13171 error_at (location, "comparing vectors with different " 13172 "number of elements"); 13173 return error_mark_node; 13174 } 13175 13176 /* It's not precisely specified how the usual arithmetic 13177 conversions apply to the vector types. Here, we use 13178 the unsigned type if one of the operands is signed and 13179 the other one is unsigned. */ 13180 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)) 13181 { 13182 if (!TYPE_UNSIGNED (type0)) 13183 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0); 13184 else 13185 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1); 13186 warning_at (location, OPT_Wsign_compare, "comparison between " 13187 "types %qT and %qT", type0, type1); 13188 } 13189 13190 /* Always construct signed integer vector type. */ 13191 intt = c_common_type_for_size (GET_MODE_BITSIZE 13192 (SCALAR_TYPE_MODE 13193 (TREE_TYPE (type0))), 0); 13194 if (!intt) 13195 { 13196 error_at (location, "could not find an integer type " 13197 "of the same size as %qT", 13198 TREE_TYPE (type0)); 13199 return error_mark_node; 13200 } 13201 result_type = build_opaque_vector_type (intt, 13202 TYPE_VECTOR_SUBPARTS (type0)); 13203 converted = 1; 13204 ret = build_vec_cmp (resultcode, result_type, op0, op1); 13205 goto return_build_binary_op; 13206 } 13207 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)) 13208 warning_at (location, 13209 OPT_Wfloat_equal, 13210 "comparing floating-point with %<==%> or %<!=%> is unsafe"); 13211 /* Result of comparison is always int, 13212 but don't convert the args to int! */ 13213 build_type = integer_type_node; 13214 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == BITINT_TYPE 13215 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE) 13216 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE 13217 || code1 == BITINT_TYPE 13218 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE)) 13219 short_compare = 1; 13220 else if (code0 == POINTER_TYPE 13221 && (code1 == NULLPTR_TYPE 13222 || null_pointer_constant_p (orig_op1))) 13223 { 13224 maybe_warn_for_null_address (location, op0, code); 13225 result_type = type0; 13226 } 13227 else if (code1 == POINTER_TYPE 13228 && (code0 == NULLPTR_TYPE 13229 || null_pointer_constant_p (orig_op0))) 13230 { 13231 maybe_warn_for_null_address (location, op1, code); 13232 result_type = type1; 13233 } 13234 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) 13235 { 13236 tree tt0 = TREE_TYPE (type0); 13237 tree tt1 = TREE_TYPE (type1); 13238 addr_space_t as0 = TYPE_ADDR_SPACE (tt0); 13239 addr_space_t as1 = TYPE_ADDR_SPACE (tt1); 13240 addr_space_t as_common = ADDR_SPACE_GENERIC; 13241 13242 /* Anything compares with void *. void * compares with anything. 13243 Otherwise, the targets must be compatible 13244 and both must be object or both incomplete. */ 13245 if (comp_target_types (location, type0, type1)) 13246 result_type = common_pointer_type (type0, type1); 13247 else if (!addr_space_superset (as0, as1, &as_common)) 13248 { 13249 error_at (location, "comparison of pointers to " 13250 "disjoint address spaces"); 13251 return error_mark_node; 13252 } 13253 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0)) 13254 { 13255 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE) 13256 pedwarn (location, OPT_Wpedantic, "ISO C forbids " 13257 "comparison of %<void *%> with function pointer"); 13258 } 13259 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1)) 13260 { 13261 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE) 13262 pedwarn (location, OPT_Wpedantic, "ISO C forbids " 13263 "comparison of %<void *%> with function pointer"); 13264 } 13265 else 13266 /* Avoid warning about the volatile ObjC EH puts on decls. */ 13267 if (!objc_ok) 13268 pedwarn (location, OPT_Wcompare_distinct_pointer_types, 13269 "comparison of distinct pointer types lacks a cast"); 13270 13271 if (result_type == NULL_TREE) 13272 { 13273 int qual = ENCODE_QUAL_ADDR_SPACE (as_common); 13274 result_type = c_build_pointer_type 13275 (c_build_qualified_type (void_type_node, qual)); 13276 } 13277 } 13278 else if (code0 == POINTER_TYPE 13279 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE)) 13280 { 13281 result_type = type0; 13282 pedwarn (location, 0, "comparison between pointer and integer"); 13283 } 13284 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE) 13285 && code1 == POINTER_TYPE) 13286 { 13287 result_type = type1; 13288 pedwarn (location, 0, "comparison between pointer and integer"); 13289 } 13290 /* 6.5.9: One of the following shall hold: 13291 -- both operands have type nullptr_t; */ 13292 else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE) 13293 { 13294 result_type = nullptr_type_node; 13295 /* No need to convert the operands to result_type later. */ 13296 converted = 1; 13297 } 13298 /* -- one operand has type nullptr_t and the other is a null pointer 13299 constant. We will have to convert the former to the type of the 13300 latter, because during gimplification we can't have mismatching 13301 comparison operand type. We convert from nullptr_t to the other 13302 type, since only nullptr_t can be converted to nullptr_t. Also, 13303 even a constant 0 is a null pointer constant, so we may have to 13304 create a pointer type from its type. */ 13305 else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1)) 13306 result_type = (INTEGRAL_TYPE_P (type1) 13307 ? c_build_pointer_type (type1) : type1); 13308 else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0)) 13309 result_type = (INTEGRAL_TYPE_P (type0) 13310 ? c_build_pointer_type (type0) : type0); 13311 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0)) 13312 || truth_value_p (TREE_CODE (orig_op0))) 13313 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1)) 13314 || truth_value_p (TREE_CODE (orig_op1)))) 13315 maybe_warn_bool_compare (location, code, orig_op0, orig_op1); 13316 break; 13317 13318 case LE_EXPR: 13319 case GE_EXPR: 13320 case LT_EXPR: 13321 case GT_EXPR: 13322 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1)) 13323 { 13324 tree intt; 13325 if (!vector_types_compatible_elements_p (type0, type1)) 13326 { 13327 error_at (location, "comparing vectors with different " 13328 "element types"); 13329 return error_mark_node; 13330 } 13331 13332 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0), 13333 TYPE_VECTOR_SUBPARTS (type1))) 13334 { 13335 error_at (location, "comparing vectors with different " 13336 "number of elements"); 13337 return error_mark_node; 13338 } 13339 13340 /* It's not precisely specified how the usual arithmetic 13341 conversions apply to the vector types. Here, we use 13342 the unsigned type if one of the operands is signed and 13343 the other one is unsigned. */ 13344 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)) 13345 { 13346 if (!TYPE_UNSIGNED (type0)) 13347 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0); 13348 else 13349 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1); 13350 warning_at (location, OPT_Wsign_compare, "comparison between " 13351 "types %qT and %qT", type0, type1); 13352 } 13353 13354 /* Always construct signed integer vector type. */ 13355 intt = c_common_type_for_size (GET_MODE_BITSIZE 13356 (SCALAR_TYPE_MODE 13357 (TREE_TYPE (type0))), 0); 13358 if (!intt) 13359 { 13360 error_at (location, "could not find an integer type " 13361 "of the same size as %qT", 13362 TREE_TYPE (type0)); 13363 return error_mark_node; 13364 } 13365 result_type = build_opaque_vector_type (intt, 13366 TYPE_VECTOR_SUBPARTS (type0)); 13367 converted = 1; 13368 ret = build_vec_cmp (resultcode, result_type, op0, op1); 13369 goto return_build_binary_op; 13370 } 13371 build_type = integer_type_node; 13372 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE 13373 || code0 == BITINT_TYPE || code0 == FIXED_POINT_TYPE) 13374 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE 13375 || code1 == BITINT_TYPE || code1 == FIXED_POINT_TYPE)) 13376 short_compare = 1; 13377 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) 13378 { 13379 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0)); 13380 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); 13381 addr_space_t as_common; 13382 13383 if (comp_target_types (location, type0, type1)) 13384 { 13385 result_type = common_pointer_type (type0, type1); 13386 if (!COMPLETE_TYPE_P (TREE_TYPE (type0)) 13387 != !COMPLETE_TYPE_P (TREE_TYPE (type1))) 13388 pedwarn_c99 (location, OPT_Wpedantic, 13389 "comparison of complete and incomplete pointers"); 13390 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) 13391 pedwarn (location, OPT_Wpedantic, "ISO C forbids " 13392 "ordered comparisons of pointers to functions"); 13393 else if (null_pointer_constant_p (orig_op0) 13394 || null_pointer_constant_p (orig_op1)) 13395 warning_at (location, OPT_Wextra, 13396 "ordered comparison of pointer with null pointer"); 13397 13398 } 13399 else if (!addr_space_superset (as0, as1, &as_common)) 13400 { 13401 error_at (location, "comparison of pointers to " 13402 "disjoint address spaces"); 13403 return error_mark_node; 13404 } 13405 else 13406 { 13407 int qual = ENCODE_QUAL_ADDR_SPACE (as_common); 13408 result_type = c_build_pointer_type 13409 (c_build_qualified_type (void_type_node, qual)); 13410 pedwarn (location, OPT_Wcompare_distinct_pointer_types, 13411 "comparison of distinct pointer types lacks a cast"); 13412 } 13413 } 13414 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) 13415 { 13416 result_type = type0; 13417 if (pedantic) 13418 pedwarn (location, OPT_Wpedantic, 13419 "ordered comparison of pointer with integer zero"); 13420 else if (extra_warnings) 13421 warning_at (location, OPT_Wextra, 13422 "ordered comparison of pointer with integer zero"); 13423 } 13424 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) 13425 { 13426 result_type = type1; 13427 if (pedantic) 13428 pedwarn (location, OPT_Wpedantic, 13429 "ordered comparison of pointer with integer zero"); 13430 else if (extra_warnings) 13431 warning_at (location, OPT_Wextra, 13432 "ordered comparison of pointer with integer zero"); 13433 } 13434 else if (code0 == POINTER_TYPE 13435 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE)) 13436 { 13437 result_type = type0; 13438 pedwarn (location, 0, "comparison between pointer and integer"); 13439 } 13440 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE) 13441 && code1 == POINTER_TYPE) 13442 { 13443 result_type = type1; 13444 pedwarn (location, 0, "comparison between pointer and integer"); 13445 } 13446 13447 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE) 13448 && current_function_decl != NULL_TREE 13449 && sanitize_flags_p (SANITIZE_POINTER_COMPARE)) 13450 { 13451 op0 = save_expr (c_fully_fold (op0, false, NULL)); 13452 op1 = save_expr (c_fully_fold (op1, false, NULL)); 13453 13454 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE); 13455 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1); 13456 } 13457 13458 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0)) 13459 || truth_value_p (TREE_CODE (orig_op0))) 13460 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1)) 13461 || truth_value_p (TREE_CODE (orig_op1)))) 13462 maybe_warn_bool_compare (location, code, orig_op0, orig_op1); 13463 break; 13464 13465 case MIN_EXPR: 13466 case MAX_EXPR: 13467 /* Used for OpenMP atomics. */ 13468 gcc_assert (flag_openmp); 13469 common = 1; 13470 break; 13471 13472 default: 13473 gcc_unreachable (); 13474 } 13475 13476 if (code0 == ERROR_MARK || code1 == ERROR_MARK) 13477 return error_mark_node; 13478 13479 if (gnu_vector_type_p (type0) 13480 && gnu_vector_type_p (type1) 13481 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) 13482 || !vector_types_compatible_elements_p (type0, type1))) 13483 { 13484 gcc_rich_location richloc (location); 13485 maybe_range_label_for_tree_type_mismatch 13486 label_for_op0 (orig_op0, orig_op1), 13487 label_for_op1 (orig_op1, orig_op0); 13488 richloc.maybe_add_expr (orig_op0, &label_for_op0); 13489 richloc.maybe_add_expr (orig_op1, &label_for_op1); 13490 binary_op_error (&richloc, code, type0, type1); 13491 return error_mark_node; 13492 } 13493 13494 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE 13495 || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE 13496 || gnu_vector_type_p (type0)) 13497 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE 13498 || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE 13499 || gnu_vector_type_p (type1))) 13500 { 13501 bool first_complex = (code0 == COMPLEX_TYPE); 13502 bool second_complex = (code1 == COMPLEX_TYPE); 13503 int none_complex = (!first_complex && !second_complex); 13504 13505 if (shorten || common || short_compare) 13506 { 13507 result_type = c_common_type (type0, type1); 13508 do_warn_double_promotion (result_type, type0, type1, 13509 "implicit conversion from %qT to %qT " 13510 "to match other operand of binary " 13511 "expression", 13512 location); 13513 if (result_type == error_mark_node) 13514 return error_mark_node; 13515 } 13516 13517 if (first_complex != second_complex 13518 && (code == PLUS_EXPR 13519 || code == MINUS_EXPR 13520 || code == MULT_EXPR 13521 || (code == TRUNC_DIV_EXPR && first_complex)) 13522 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE 13523 && flag_signed_zeros) 13524 { 13525 /* An operation on mixed real/complex operands must be 13526 handled specially, but the language-independent code can 13527 more easily optimize the plain complex arithmetic if 13528 -fno-signed-zeros. */ 13529 tree real_type = TREE_TYPE (result_type); 13530 tree real, imag; 13531 if (type0 != orig_type0 || type1 != orig_type1) 13532 { 13533 gcc_assert (may_need_excess_precision && common); 13534 semantic_result_type = c_common_type (orig_type0, orig_type1); 13535 } 13536 if (first_complex) 13537 { 13538 if (TREE_TYPE (op0) != result_type) 13539 op0 = convert_and_check (location, result_type, op0); 13540 if (TREE_TYPE (op1) != real_type) 13541 op1 = convert_and_check (location, real_type, op1); 13542 } 13543 else 13544 { 13545 if (TREE_TYPE (op0) != real_type) 13546 op0 = convert_and_check (location, real_type, op0); 13547 if (TREE_TYPE (op1) != result_type) 13548 op1 = convert_and_check (location, result_type, op1); 13549 } 13550 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) 13551 return error_mark_node; 13552 if (first_complex) 13553 { 13554 op0 = save_expr (op0); 13555 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR, 13556 op0, true); 13557 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR, 13558 op0, true); 13559 switch (code) 13560 { 13561 case MULT_EXPR: 13562 case TRUNC_DIV_EXPR: 13563 op1 = save_expr (op1); 13564 imag = build2 (resultcode, real_type, imag, op1); 13565 /* Fall through. */ 13566 case PLUS_EXPR: 13567 case MINUS_EXPR: 13568 real = build2 (resultcode, real_type, real, op1); 13569 break; 13570 default: 13571 gcc_unreachable(); 13572 } 13573 } 13574 else 13575 { 13576 op1 = save_expr (op1); 13577 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR, 13578 op1, true); 13579 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR, 13580 op1, true); 13581 switch (code) 13582 { 13583 case MULT_EXPR: 13584 op0 = save_expr (op0); 13585 imag = build2 (resultcode, real_type, op0, imag); 13586 /* Fall through. */ 13587 case PLUS_EXPR: 13588 real = build2 (resultcode, real_type, op0, real); 13589 break; 13590 case MINUS_EXPR: 13591 real = build2 (resultcode, real_type, op0, real); 13592 imag = build1 (NEGATE_EXPR, real_type, imag); 13593 break; 13594 default: 13595 gcc_unreachable(); 13596 } 13597 } 13598 ret = build2 (COMPLEX_EXPR, result_type, real, imag); 13599 goto return_build_binary_op; 13600 } 13601 13602 /* For certain operations (which identify themselves by shorten != 0) 13603 if both args were extended from the same smaller type, 13604 do the arithmetic in that type and then extend. 13605 13606 shorten !=0 and !=1 indicates a bitwise operation. 13607 For them, this optimization is safe only if 13608 both args are zero-extended or both are sign-extended. 13609 Otherwise, we might change the result. 13610 Eg, (short)-1 | (unsigned short)-1 is (int)-1 13611 but calculated in (unsigned short) it would be (unsigned short)-1. */ 13612 13613 if (shorten && none_complex) 13614 { 13615 final_type = result_type; 13616 result_type = shorten_binary_op (result_type, op0, op1, 13617 shorten == -1); 13618 } 13619 13620 /* Shifts can be shortened if shifting right. */ 13621 13622 if (short_shift) 13623 { 13624 int unsigned_arg; 13625 tree arg0 = get_narrower (op0, &unsigned_arg); 13626 13627 final_type = result_type; 13628 13629 if (arg0 == op0 && final_type == TREE_TYPE (op0)) 13630 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0)); 13631 13632 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) 13633 && tree_int_cst_sgn (op1) > 0 13634 /* We can shorten only if the shift count is less than the 13635 number of bits in the smaller type size. */ 13636 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0 13637 /* We cannot drop an unsigned shift after sign-extension. */ 13638 && (!TYPE_UNSIGNED (final_type) || unsigned_arg)) 13639 { 13640 /* Do an unsigned shift if the operand was zero-extended. */ 13641 result_type 13642 = c_common_signed_or_unsigned_type (unsigned_arg, 13643 TREE_TYPE (arg0)); 13644 /* Convert value-to-be-shifted to that type. */ 13645 if (TREE_TYPE (op0) != result_type) 13646 op0 = convert (result_type, op0); 13647 converted = 1; 13648 } 13649 } 13650 13651 /* Comparison operations are shortened too but differently. 13652 They identify themselves by setting short_compare = 1. */ 13653 13654 if (short_compare) 13655 { 13656 /* Don't write &op0, etc., because that would prevent op0 13657 from being kept in a register. 13658 Instead, make copies of the our local variables and 13659 pass the copies by reference, then copy them back afterward. */ 13660 tree xop0 = op0, xop1 = op1, xresult_type = result_type; 13661 enum tree_code xresultcode = resultcode; 13662 tree val 13663 = shorten_compare (location, &xop0, &xop1, &xresult_type, 13664 &xresultcode); 13665 13666 if (val != NULL_TREE) 13667 { 13668 ret = val; 13669 goto return_build_binary_op; 13670 } 13671 13672 op0 = xop0, op1 = xop1; 13673 converted = 1; 13674 resultcode = xresultcode; 13675 13676 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for) 13677 { 13678 bool op0_maybe_const = true; 13679 bool op1_maybe_const = true; 13680 tree orig_op0_folded, orig_op1_folded; 13681 13682 if (in_late_binary_op) 13683 { 13684 orig_op0_folded = orig_op0; 13685 orig_op1_folded = orig_op1; 13686 } 13687 else 13688 { 13689 /* Fold for the sake of possible warnings, as in 13690 build_conditional_expr. This requires the 13691 "original" values to be folded, not just op0 and 13692 op1. */ 13693 c_inhibit_evaluation_warnings++; 13694 op0 = c_fully_fold (op0, require_constant_value, 13695 &op0_maybe_const); 13696 op1 = c_fully_fold (op1, require_constant_value, 13697 &op1_maybe_const); 13698 c_inhibit_evaluation_warnings--; 13699 orig_op0_folded = c_fully_fold (orig_op0, 13700 require_constant_value, 13701 NULL); 13702 orig_op1_folded = c_fully_fold (orig_op1, 13703 require_constant_value, 13704 NULL); 13705 } 13706 13707 if (warn_sign_compare) 13708 warn_for_sign_compare (location, orig_op0_folded, 13709 orig_op1_folded, op0, op1, 13710 result_type, resultcode); 13711 if (!in_late_binary_op && !int_operands) 13712 { 13713 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST) 13714 op0 = c_wrap_maybe_const (op0, !op0_maybe_const); 13715 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) 13716 op1 = c_wrap_maybe_const (op1, !op1_maybe_const); 13717 } 13718 } 13719 } 13720 } 13721 13722 /* At this point, RESULT_TYPE must be nonzero to avoid an error message. 13723 If CONVERTED is zero, both args will be converted to type RESULT_TYPE. 13724 Then the expression will be built. 13725 It will be given type FINAL_TYPE if that is nonzero; 13726 otherwise, it will be given type RESULT_TYPE. */ 13727 13728 if (!result_type) 13729 { 13730 /* Favor showing any expression locations that are available. */ 13731 op_location_t oploc (location, UNKNOWN_LOCATION); 13732 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true); 13733 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1)); 13734 return error_mark_node; 13735 } 13736 13737 if (build_type == NULL_TREE) 13738 { 13739 build_type = result_type; 13740 if ((type0 != orig_type0 || type1 != orig_type1) 13741 && !boolean_op) 13742 { 13743 gcc_assert (may_need_excess_precision && common); 13744 semantic_result_type = c_common_type (orig_type0, orig_type1); 13745 } 13746 } 13747 13748 if (!converted) 13749 { 13750 op0 = ep_convert_and_check (location, result_type, op0, 13751 semantic_result_type); 13752 op1 = ep_convert_and_check (location, result_type, op1, 13753 semantic_result_type); 13754 13755 /* This can happen if one operand has a vector type, and the other 13756 has a different type. */ 13757 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) 13758 return error_mark_node; 13759 } 13760 13761 if (sanitize_flags_p ((SANITIZE_SHIFT 13762 | SANITIZE_DIVIDE 13763 | SANITIZE_FLOAT_DIVIDE 13764 | SANITIZE_SI_OVERFLOW)) 13765 && current_function_decl != NULL_TREE 13766 && (doing_div_or_mod || doing_shift) 13767 && !require_constant_value) 13768 { 13769 /* OP0 and/or OP1 might have side-effects. */ 13770 op0 = save_expr (op0); 13771 op1 = save_expr (op1); 13772 op0 = c_fully_fold (op0, false, NULL); 13773 op1 = c_fully_fold (op1, false, NULL); 13774 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE 13775 | SANITIZE_FLOAT_DIVIDE 13776 | SANITIZE_SI_OVERFLOW)))) 13777 instrument_expr = ubsan_instrument_division (location, op0, op1); 13778 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT)) 13779 instrument_expr = ubsan_instrument_shift (location, code, op0, op1); 13780 } 13781 13782 /* Treat expressions in initializers specially as they can't trap. */ 13783 if (int_const_or_overflow) 13784 ret = (require_constant_value 13785 ? fold_build2_initializer_loc (location, resultcode, build_type, 13786 op0, op1) 13787 : fold_build2_loc (location, resultcode, build_type, op0, op1)); 13788 else 13789 ret = build2 (resultcode, build_type, op0, op1); 13790 if (final_type != NULL_TREE) 13791 ret = convert (final_type, ret); 13792 13793 return_build_binary_op: 13794 gcc_assert (ret != error_mark_node); 13795 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const) 13796 ret = (int_operands 13797 ? note_integer_operands (ret) 13798 : build1 (NOP_EXPR, TREE_TYPE (ret), ret)); 13799 else if (TREE_CODE (ret) != INTEGER_CST && int_operands 13800 && !in_late_binary_op) 13801 ret = note_integer_operands (ret); 13802 protected_set_expr_location (ret, location); 13803 13804 if (instrument_expr != NULL) 13805 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret), 13806 instrument_expr, ret); 13807 13808 if (semantic_result_type) 13809 ret = build1_loc (location, EXCESS_PRECISION_EXPR, 13810 semantic_result_type, ret); 13811 13812 return ret; 13813 } 13814 13815 13816 /* Convert EXPR to be a truth-value (type TYPE), validating its type for this 13817 purpose. LOCATION is the source location for the expression. */ 13818 13819 tree 13820 c_objc_common_truthvalue_conversion (location_t location, tree expr, tree type) 13821 { 13822 bool int_const, int_operands; 13823 13824 switch (TREE_CODE (TREE_TYPE (expr))) 13825 { 13826 case ARRAY_TYPE: 13827 error_at (location, "used array that cannot be converted to pointer where scalar is required"); 13828 return error_mark_node; 13829 13830 case RECORD_TYPE: 13831 error_at (location, "used struct type value where scalar is required"); 13832 return error_mark_node; 13833 13834 case UNION_TYPE: 13835 error_at (location, "used union type value where scalar is required"); 13836 return error_mark_node; 13837 13838 case VOID_TYPE: 13839 error_at (location, "void value not ignored as it ought to be"); 13840 return error_mark_node; 13841 13842 case POINTER_TYPE: 13843 if (reject_gcc_builtin (expr)) 13844 return error_mark_node; 13845 break; 13846 13847 case FUNCTION_TYPE: 13848 gcc_unreachable (); 13849 13850 case VECTOR_TYPE: 13851 error_at (location, "used vector type where scalar is required"); 13852 return error_mark_node; 13853 13854 default: 13855 break; 13856 } 13857 13858 /* Conversion of a floating constant to boolean goes through here 13859 and yields an integer constant expression. Otherwise, the result 13860 is only an integer constant expression if the argument is. */ 13861 int_const = ((TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr)) 13862 || ((TREE_CODE (expr) == REAL_CST 13863 || TREE_CODE (expr) == COMPLEX_CST) 13864 && (TREE_CODE (type) == BOOLEAN_TYPE 13865 || (TREE_CODE (type) == ENUMERAL_TYPE 13866 && ENUM_UNDERLYING_TYPE (type) != NULL_TREE 13867 && (TREE_CODE (ENUM_UNDERLYING_TYPE (type)) 13868 == BOOLEAN_TYPE))))); 13869 int_operands = EXPR_INT_CONST_OPERANDS (expr); 13870 if (int_operands && TREE_CODE (expr) != INTEGER_CST) 13871 { 13872 expr = remove_c_maybe_const_expr (expr); 13873 expr = build2 (NE_EXPR, type, expr, 13874 convert (TREE_TYPE (expr), integer_zero_node)); 13875 expr = note_integer_operands (expr); 13876 } 13877 else 13878 { 13879 /* ??? Should we also give an error for vectors rather than leaving 13880 those to give errors later? */ 13881 expr = c_common_truthvalue_conversion (location, expr); 13882 expr = fold_convert_loc (location, type, expr); 13883 } 13884 13885 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const) 13886 { 13887 if (TREE_OVERFLOW (expr)) 13888 return expr; 13889 else 13890 return note_integer_operands (expr); 13891 } 13892 if (TREE_CODE (expr) == INTEGER_CST && !int_const) 13893 return build1 (NOP_EXPR, TREE_TYPE (expr), expr); 13894 return expr; 13895 } 13896 13897 13899 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as 13900 required. */ 13901 13902 tree 13903 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se) 13904 { 13905 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR) 13906 { 13907 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr); 13908 /* Executing a compound literal inside a function reinitializes 13909 it. */ 13910 if (!TREE_STATIC (decl)) 13911 *se = true; 13912 return decl; 13913 } 13914 else 13915 return expr; 13916 } 13917 13918 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound 13920 statement. LOC is the location of the construct. */ 13921 13922 tree 13923 c_finish_omp_construct (location_t loc, enum tree_code code, tree body, 13924 tree clauses) 13925 { 13926 body = c_end_compound_stmt (loc, body, true); 13927 13928 tree stmt = make_node (code); 13929 TREE_TYPE (stmt) = void_type_node; 13930 OMP_BODY (stmt) = body; 13931 OMP_CLAUSES (stmt) = clauses; 13932 SET_EXPR_LOCATION (stmt, loc); 13933 13934 return add_stmt (stmt); 13935 } 13936 13937 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound 13938 statement. LOC is the location of the OACC_DATA. */ 13939 13940 tree 13941 c_finish_oacc_data (location_t loc, tree clauses, tree block) 13942 { 13943 tree stmt; 13944 13945 block = c_end_compound_stmt (loc, block, true); 13946 13947 stmt = make_node (OACC_DATA); 13948 TREE_TYPE (stmt) = void_type_node; 13949 OACC_DATA_CLAUSES (stmt) = clauses; 13950 OACC_DATA_BODY (stmt) = block; 13951 SET_EXPR_LOCATION (stmt, loc); 13952 13953 return add_stmt (stmt); 13954 } 13955 13956 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound 13957 statement. LOC is the location of the OACC_HOST_DATA. */ 13958 13959 tree 13960 c_finish_oacc_host_data (location_t loc, tree clauses, tree block) 13961 { 13962 tree stmt; 13963 13964 block = c_end_compound_stmt (loc, block, true); 13965 13966 stmt = make_node (OACC_HOST_DATA); 13967 TREE_TYPE (stmt) = void_type_node; 13968 OACC_HOST_DATA_CLAUSES (stmt) = clauses; 13969 OACC_HOST_DATA_BODY (stmt) = block; 13970 SET_EXPR_LOCATION (stmt, loc); 13971 13972 return add_stmt (stmt); 13973 } 13974 13975 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ 13976 13977 tree 13978 c_begin_omp_parallel (void) 13979 { 13980 tree block; 13981 13982 keep_next_level (); 13983 block = c_begin_compound_stmt (true); 13984 13985 return block; 13986 } 13987 13988 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound 13989 statement. LOC is the location of the OMP_PARALLEL. */ 13990 13991 tree 13992 c_finish_omp_parallel (location_t loc, tree clauses, tree block) 13993 { 13994 tree stmt; 13995 13996 block = c_end_compound_stmt (loc, block, true); 13997 13998 stmt = make_node (OMP_PARALLEL); 13999 TREE_TYPE (stmt) = void_type_node; 14000 OMP_PARALLEL_CLAUSES (stmt) = clauses; 14001 OMP_PARALLEL_BODY (stmt) = block; 14002 SET_EXPR_LOCATION (stmt, loc); 14003 14004 return add_stmt (stmt); 14005 } 14006 14007 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ 14008 14009 tree 14010 c_begin_omp_task (void) 14011 { 14012 tree block; 14013 14014 keep_next_level (); 14015 block = c_begin_compound_stmt (true); 14016 14017 return block; 14018 } 14019 14020 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound 14021 statement. LOC is the location of the #pragma. */ 14022 14023 tree 14024 c_finish_omp_task (location_t loc, tree clauses, tree block) 14025 { 14026 tree stmt; 14027 14028 block = c_end_compound_stmt (loc, block, true); 14029 14030 stmt = make_node (OMP_TASK); 14031 TREE_TYPE (stmt) = void_type_node; 14032 OMP_TASK_CLAUSES (stmt) = clauses; 14033 OMP_TASK_BODY (stmt) = block; 14034 SET_EXPR_LOCATION (stmt, loc); 14035 14036 return add_stmt (stmt); 14037 } 14038 14039 /* Generate GOMP_cancel call for #pragma omp cancel. */ 14040 14041 void 14042 c_finish_omp_cancel (location_t loc, tree clauses) 14043 { 14044 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL); 14045 int mask = 0; 14046 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL)) 14047 mask = 1; 14048 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR)) 14049 mask = 2; 14050 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS)) 14051 mask = 4; 14052 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP)) 14053 mask = 8; 14054 else 14055 { 14056 error_at (loc, "%<#pragma omp cancel%> must specify one of " 14057 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> " 14058 "clauses"); 14059 return; 14060 } 14061 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF); 14062 if (ifc != NULL_TREE) 14063 { 14064 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK 14065 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST) 14066 error_at (OMP_CLAUSE_LOCATION (ifc), 14067 "expected %<cancel%> %<if%> clause modifier"); 14068 else 14069 { 14070 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF); 14071 if (ifc2 != NULL_TREE) 14072 { 14073 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST 14074 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK 14075 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST); 14076 error_at (OMP_CLAUSE_LOCATION (ifc2), 14077 "expected %<cancel%> %<if%> clause modifier"); 14078 } 14079 } 14080 14081 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc)); 14082 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR, 14083 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc), 14084 build_zero_cst (type)); 14085 } 14086 else 14087 ifc = boolean_true_node; 14088 tree stmt = build_call_expr_loc (loc, fn, 2, 14089 build_int_cst (integer_type_node, mask), 14090 ifc); 14091 add_stmt (stmt); 14092 } 14093 14094 /* Generate GOMP_cancellation_point call for 14095 #pragma omp cancellation point. */ 14096 14097 void 14098 c_finish_omp_cancellation_point (location_t loc, tree clauses) 14099 { 14100 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT); 14101 int mask = 0; 14102 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL)) 14103 mask = 1; 14104 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR)) 14105 mask = 2; 14106 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS)) 14107 mask = 4; 14108 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP)) 14109 mask = 8; 14110 else 14111 { 14112 error_at (loc, "%<#pragma omp cancellation point%> must specify one of " 14113 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> " 14114 "clauses"); 14115 return; 14116 } 14117 tree stmt = build_call_expr_loc (loc, fn, 1, 14118 build_int_cst (integer_type_node, mask)); 14119 add_stmt (stmt); 14120 } 14121 14122 /* Helper function for handle_omp_array_sections. Called recursively 14123 to handle multiple array-section-subscripts. C is the clause, 14124 T current expression (initially OMP_CLAUSE_DECL), which is either 14125 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound 14126 expression if specified, TREE_VALUE length expression if specified, 14127 TREE_CHAIN is what it has been specified after, or some decl. 14128 TYPES vector is populated with array section types, MAYBE_ZERO_LEN 14129 set to true if any of the array-section-subscript could have length 14130 of zero (explicit or implicit), FIRST_NON_ONE is the index of the 14131 first array-section-subscript which is known not to have length 14132 of one. Given say: 14133 map(a[:b][2:1][:c][:2][:d][e:f][2:5]) 14134 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c] 14135 all are or may have length of 1, array-section-subscript [:2] is the 14136 first one known not to have length 1. For array-section-subscript 14137 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't 14138 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we 14139 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above 14140 case though, as some lengths could be zero. */ 14141 14142 static tree 14143 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types, 14144 bool &maybe_zero_len, unsigned int &first_non_one, 14145 enum c_omp_region_type ort) 14146 { 14147 tree ret, low_bound, length, type; 14148 bool openacc = (ort & C_ORT_ACC) != 0; 14149 if (TREE_CODE (t) != OMP_ARRAY_SECTION) 14150 { 14151 if (error_operand_p (t)) 14152 return error_mark_node; 14153 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t); 14154 ret = t; 14155 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY 14156 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND 14157 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t)))) 14158 { 14159 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause", 14160 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14161 return error_mark_node; 14162 } 14163 if (!ai.check_clause (c)) 14164 return error_mark_node; 14165 else if (ai.component_access_p () 14166 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP 14167 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO 14168 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)) 14169 t = ai.get_root_term (true); 14170 else 14171 t = ai.unconverted_ref_origin (); 14172 if (t == error_mark_node) 14173 return error_mark_node; 14174 if (!VAR_P (t) 14175 && (ort == C_ORT_ACC || !EXPR_P (t)) 14176 && TREE_CODE (t) != PARM_DECL) 14177 { 14178 if (DECL_P (t)) 14179 error_at (OMP_CLAUSE_LOCATION (c), 14180 "%qD is not a variable in %qs clause", t, 14181 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14182 else 14183 error_at (OMP_CLAUSE_LOCATION (c), 14184 "%qE is not a variable in %qs clause", t, 14185 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14186 return error_mark_node; 14187 } 14188 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY 14189 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND 14190 && TYPE_ATOMIC (TREE_TYPE (t))) 14191 { 14192 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause", 14193 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14194 return error_mark_node; 14195 } 14196 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY 14197 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND 14198 && VAR_P (t) 14199 && DECL_THREAD_LOCAL_P (t)) 14200 { 14201 error_at (OMP_CLAUSE_LOCATION (c), 14202 "%qD is threadprivate variable in %qs clause", t, 14203 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14204 return error_mark_node; 14205 } 14206 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY 14207 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND) 14208 && TYPE_ATOMIC (TREE_TYPE (t)) 14209 && POINTER_TYPE_P (TREE_TYPE (t))) 14210 { 14211 /* If the array section is pointer based and the pointer 14212 itself is _Atomic qualified, we need to atomically load 14213 the pointer. */ 14214 c_expr expr; 14215 memset (&expr, 0, sizeof (expr)); 14216 expr.value = ret; 14217 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c), 14218 expr, false, false); 14219 ret = expr.value; 14220 } 14221 return ret; 14222 } 14223 14224 ret = handle_omp_array_sections_1 (c, TREE_OPERAND (t, 0), types, 14225 maybe_zero_len, first_non_one, ort); 14226 if (ret == error_mark_node || ret == NULL_TREE) 14227 return ret; 14228 14229 type = TREE_TYPE (ret); 14230 low_bound = TREE_OPERAND (t, 1); 14231 length = TREE_OPERAND (t, 2); 14232 14233 if (low_bound == error_mark_node || length == error_mark_node) 14234 return error_mark_node; 14235 14236 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound))) 14237 { 14238 error_at (OMP_CLAUSE_LOCATION (c), 14239 "low bound %qE of array section does not have integral type", 14240 low_bound); 14241 return error_mark_node; 14242 } 14243 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length))) 14244 { 14245 error_at (OMP_CLAUSE_LOCATION (c), 14246 "length %qE of array section does not have integral type", 14247 length); 14248 return error_mark_node; 14249 } 14250 if (low_bound 14251 && TREE_CODE (low_bound) == INTEGER_CST 14252 && TYPE_PRECISION (TREE_TYPE (low_bound)) 14253 > TYPE_PRECISION (sizetype)) 14254 low_bound = fold_convert (sizetype, low_bound); 14255 if (length 14256 && TREE_CODE (length) == INTEGER_CST 14257 && TYPE_PRECISION (TREE_TYPE (length)) 14258 > TYPE_PRECISION (sizetype)) 14259 length = fold_convert (sizetype, length); 14260 if (low_bound == NULL_TREE) 14261 low_bound = integer_zero_node; 14262 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP 14263 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH 14264 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)) 14265 { 14266 if (length != integer_one_node) 14267 { 14268 error_at (OMP_CLAUSE_LOCATION (c), 14269 "expected single pointer in %qs clause", 14270 user_omp_clause_code_name (c, openacc)); 14271 return error_mark_node; 14272 } 14273 } 14274 if (length != NULL_TREE) 14275 { 14276 if (!integer_nonzerop (length)) 14277 { 14278 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY 14279 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND 14280 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION 14281 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION 14282 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION) 14283 { 14284 if (integer_zerop (length)) 14285 { 14286 error_at (OMP_CLAUSE_LOCATION (c), 14287 "zero length array section in %qs clause", 14288 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14289 return error_mark_node; 14290 } 14291 } 14292 else 14293 maybe_zero_len = true; 14294 } 14295 if (first_non_one == types.length () 14296 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length))) 14297 first_non_one++; 14298 } 14299 if (TREE_CODE (type) == ARRAY_TYPE) 14300 { 14301 if (length == NULL_TREE 14302 && (TYPE_DOMAIN (type) == NULL_TREE 14303 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)) 14304 { 14305 error_at (OMP_CLAUSE_LOCATION (c), 14306 "for unknown bound array type length expression must " 14307 "be specified"); 14308 return error_mark_node; 14309 } 14310 if (TREE_CODE (low_bound) == INTEGER_CST 14311 && tree_int_cst_sgn (low_bound) == -1) 14312 { 14313 error_at (OMP_CLAUSE_LOCATION (c), 14314 "negative low bound in array section in %qs clause", 14315 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14316 return error_mark_node; 14317 } 14318 if (length != NULL_TREE 14319 && TREE_CODE (length) == INTEGER_CST 14320 && tree_int_cst_sgn (length) == -1) 14321 { 14322 error_at (OMP_CLAUSE_LOCATION (c), 14323 "negative length in array section in %qs clause", 14324 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14325 return error_mark_node; 14326 } 14327 if (TYPE_DOMAIN (type) 14328 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) 14329 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) 14330 == INTEGER_CST) 14331 { 14332 tree size 14333 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type))); 14334 size = size_binop (PLUS_EXPR, size, size_one_node); 14335 if (TREE_CODE (low_bound) == INTEGER_CST) 14336 { 14337 if (tree_int_cst_lt (size, low_bound)) 14338 { 14339 error_at (OMP_CLAUSE_LOCATION (c), 14340 "low bound %qE above array section size " 14341 "in %qs clause", low_bound, 14342 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14343 return error_mark_node; 14344 } 14345 if (tree_int_cst_equal (size, low_bound)) 14346 { 14347 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY 14348 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND 14349 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION 14350 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION 14351 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION) 14352 { 14353 error_at (OMP_CLAUSE_LOCATION (c), 14354 "zero length array section in %qs clause", 14355 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14356 return error_mark_node; 14357 } 14358 maybe_zero_len = true; 14359 } 14360 else if (length == NULL_TREE 14361 && first_non_one == types.length () 14362 && tree_int_cst_equal 14363 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), 14364 low_bound)) 14365 first_non_one++; 14366 } 14367 else if (length == NULL_TREE) 14368 { 14369 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY 14370 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND 14371 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION 14372 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION 14373 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION) 14374 maybe_zero_len = true; 14375 if (first_non_one == types.length ()) 14376 first_non_one++; 14377 } 14378 if (length && TREE_CODE (length) == INTEGER_CST) 14379 { 14380 if (tree_int_cst_lt (size, length)) 14381 { 14382 error_at (OMP_CLAUSE_LOCATION (c), 14383 "length %qE above array section size " 14384 "in %qs clause", length, 14385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14386 return error_mark_node; 14387 } 14388 if (TREE_CODE (low_bound) == INTEGER_CST) 14389 { 14390 tree lbpluslen 14391 = size_binop (PLUS_EXPR, 14392 fold_convert (sizetype, low_bound), 14393 fold_convert (sizetype, length)); 14394 if (TREE_CODE (lbpluslen) == INTEGER_CST 14395 && tree_int_cst_lt (size, lbpluslen)) 14396 { 14397 error_at (OMP_CLAUSE_LOCATION (c), 14398 "high bound %qE above array section size " 14399 "in %qs clause", lbpluslen, 14400 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14401 return error_mark_node; 14402 } 14403 } 14404 } 14405 } 14406 else if (length == NULL_TREE) 14407 { 14408 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY 14409 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND 14410 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION 14411 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION 14412 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION) 14413 maybe_zero_len = true; 14414 if (first_non_one == types.length ()) 14415 first_non_one++; 14416 } 14417 14418 /* For [lb:] we will need to evaluate lb more than once. */ 14419 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND) 14420 { 14421 tree lb = save_expr (low_bound); 14422 if (lb != low_bound) 14423 { 14424 TREE_OPERAND (t, 1) = lb; 14425 low_bound = lb; 14426 } 14427 } 14428 } 14429 else if (TREE_CODE (type) == POINTER_TYPE) 14430 { 14431 if (length == NULL_TREE) 14432 { 14433 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret)) 14434 error_at (OMP_CLAUSE_LOCATION (c), 14435 "for array function parameter length expression " 14436 "must be specified"); 14437 else 14438 error_at (OMP_CLAUSE_LOCATION (c), 14439 "for pointer type length expression must be specified"); 14440 return error_mark_node; 14441 } 14442 if (length != NULL_TREE 14443 && TREE_CODE (length) == INTEGER_CST 14444 && tree_int_cst_sgn (length) == -1) 14445 { 14446 error_at (OMP_CLAUSE_LOCATION (c), 14447 "negative length in array section in %qs clause", 14448 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14449 return error_mark_node; 14450 } 14451 /* If there is a pointer type anywhere but in the very first 14452 array-section-subscript, the array section could be non-contiguous. */ 14453 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND 14454 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY 14455 && TREE_CODE (TREE_OPERAND (t, 0)) == OMP_ARRAY_SECTION) 14456 { 14457 /* If any prior dimension has a non-one length, then deem this 14458 array section as non-contiguous. */ 14459 for (tree d = TREE_OPERAND (t, 0); 14460 TREE_CODE (d) == OMP_ARRAY_SECTION; 14461 d = TREE_OPERAND (d, 0)) 14462 { 14463 tree d_length = TREE_OPERAND (d, 2); 14464 if (d_length == NULL_TREE || !integer_onep (d_length)) 14465 { 14466 error_at (OMP_CLAUSE_LOCATION (c), 14467 "array section is not contiguous in %qs clause", 14468 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14469 return error_mark_node; 14470 } 14471 } 14472 } 14473 } 14474 else 14475 { 14476 error_at (OMP_CLAUSE_LOCATION (c), 14477 "%qE does not have pointer or array type", ret); 14478 return error_mark_node; 14479 } 14480 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND) 14481 types.safe_push (TREE_TYPE (ret)); 14482 /* We will need to evaluate lb more than once. */ 14483 tree lb = save_expr (low_bound); 14484 if (lb != low_bound) 14485 { 14486 TREE_OPERAND (t, 1) = lb; 14487 low_bound = lb; 14488 } 14489 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound); 14490 return ret; 14491 } 14492 14493 /* Handle array sections for clause C. */ 14494 14495 static bool 14496 handle_omp_array_sections (tree &c, enum c_omp_region_type ort) 14497 { 14498 bool maybe_zero_len = false; 14499 unsigned int first_non_one = 0; 14500 auto_vec<tree, 10> types; 14501 tree *tp = &OMP_CLAUSE_DECL (c); 14502 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND 14503 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY) 14504 && TREE_CODE (*tp) == TREE_LIST 14505 && TREE_PURPOSE (*tp) 14506 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC) 14507 tp = &TREE_VALUE (*tp); 14508 tree first = handle_omp_array_sections_1 (c, *tp, types, 14509 maybe_zero_len, first_non_one, 14510 ort); 14511 if (first == error_mark_node) 14512 return true; 14513 if (first == NULL_TREE) 14514 return false; 14515 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND 14516 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY) 14517 { 14518 tree t = *tp; 14519 tree tem = NULL_TREE; 14520 /* Need to evaluate side effects in the length expressions 14521 if any. */ 14522 while (TREE_CODE (t) == TREE_LIST) 14523 { 14524 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t))) 14525 { 14526 if (tem == NULL_TREE) 14527 tem = TREE_VALUE (t); 14528 else 14529 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), 14530 TREE_VALUE (t), tem); 14531 } 14532 t = TREE_CHAIN (t); 14533 } 14534 if (tem) 14535 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first); 14536 first = c_fully_fold (first, false, NULL, true); 14537 *tp = first; 14538 } 14539 else 14540 { 14541 unsigned int num = types.length (), i; 14542 tree t, side_effects = NULL_TREE, size = NULL_TREE; 14543 tree condition = NULL_TREE; 14544 14545 if (int_size_in_bytes (TREE_TYPE (first)) <= 0) 14546 maybe_zero_len = true; 14547 14548 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0; 14549 t = TREE_OPERAND (t, 0)) 14550 { 14551 tree low_bound = TREE_OPERAND (t, 1); 14552 tree length = TREE_OPERAND (t, 2); 14553 14554 i--; 14555 if (low_bound 14556 && TREE_CODE (low_bound) == INTEGER_CST 14557 && TYPE_PRECISION (TREE_TYPE (low_bound)) 14558 > TYPE_PRECISION (sizetype)) 14559 low_bound = fold_convert (sizetype, low_bound); 14560 if (length 14561 && TREE_CODE (length) == INTEGER_CST 14562 && TYPE_PRECISION (TREE_TYPE (length)) 14563 > TYPE_PRECISION (sizetype)) 14564 length = fold_convert (sizetype, length); 14565 if (low_bound == NULL_TREE) 14566 low_bound = integer_zero_node; 14567 if (!maybe_zero_len && i > first_non_one) 14568 { 14569 if (integer_nonzerop (low_bound)) 14570 goto do_warn_noncontiguous; 14571 if (length != NULL_TREE 14572 && TREE_CODE (length) == INTEGER_CST 14573 && TYPE_DOMAIN (types[i]) 14574 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])) 14575 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))) 14576 == INTEGER_CST) 14577 { 14578 tree size; 14579 size = size_binop (PLUS_EXPR, 14580 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])), 14581 size_one_node); 14582 if (!tree_int_cst_equal (length, size)) 14583 { 14584 do_warn_noncontiguous: 14585 error_at (OMP_CLAUSE_LOCATION (c), 14586 "array section is not contiguous in %qs " 14587 "clause", 14588 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14589 return true; 14590 } 14591 } 14592 if (length != NULL_TREE 14593 && TREE_SIDE_EFFECTS (length)) 14594 { 14595 if (side_effects == NULL_TREE) 14596 side_effects = length; 14597 else 14598 side_effects = build2 (COMPOUND_EXPR, 14599 TREE_TYPE (side_effects), 14600 length, side_effects); 14601 } 14602 } 14603 else 14604 { 14605 tree l; 14606 14607 if (i > first_non_one 14608 && ((length && integer_nonzerop (length)) 14609 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION 14610 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION 14611 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)) 14612 continue; 14613 if (length) 14614 l = fold_convert (sizetype, length); 14615 else 14616 { 14617 l = size_binop (PLUS_EXPR, 14618 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])), 14619 size_one_node); 14620 l = size_binop (MINUS_EXPR, l, 14621 fold_convert (sizetype, low_bound)); 14622 } 14623 if (i > first_non_one) 14624 { 14625 l = fold_build2 (NE_EXPR, boolean_type_node, l, 14626 size_zero_node); 14627 if (condition == NULL_TREE) 14628 condition = l; 14629 else 14630 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node, 14631 l, condition); 14632 } 14633 else if (size == NULL_TREE) 14634 { 14635 size = size_in_bytes (TREE_TYPE (types[i])); 14636 tree eltype = TREE_TYPE (types[num - 1]); 14637 while (TREE_CODE (eltype) == ARRAY_TYPE) 14638 eltype = TREE_TYPE (eltype); 14639 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION 14640 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION 14641 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION) 14642 { 14643 if (integer_zerop (size) 14644 || integer_zerop (size_in_bytes (eltype))) 14645 { 14646 error_at (OMP_CLAUSE_LOCATION (c), 14647 "zero length array section in %qs clause", 14648 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 14649 return error_mark_node; 14650 } 14651 size = size_binop (EXACT_DIV_EXPR, size, 14652 size_in_bytes (eltype)); 14653 } 14654 size = size_binop (MULT_EXPR, size, l); 14655 if (condition) 14656 size = fold_build3 (COND_EXPR, sizetype, condition, 14657 size, size_zero_node); 14658 } 14659 else 14660 size = size_binop (MULT_EXPR, size, l); 14661 } 14662 } 14663 if (side_effects) 14664 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size); 14665 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION 14666 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION 14667 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION) 14668 { 14669 size = size_binop (MINUS_EXPR, size, size_one_node); 14670 size = c_fully_fold (size, false, NULL); 14671 size = save_expr (size); 14672 tree index_type = build_index_type (size); 14673 tree eltype = TREE_TYPE (first); 14674 while (TREE_CODE (eltype) == ARRAY_TYPE) 14675 eltype = TREE_TYPE (eltype); 14676 tree type = c_build_array_type (eltype, index_type); 14677 tree ptype = c_build_pointer_type (eltype); 14678 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 14679 t = build_fold_addr_expr (t); 14680 tree t2 = build_fold_addr_expr (first); 14681 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), 14682 ptrdiff_type_node, t2); 14683 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR, 14684 ptrdiff_type_node, t2, 14685 fold_convert_loc (OMP_CLAUSE_LOCATION (c), 14686 ptrdiff_type_node, t)); 14687 t2 = c_fully_fold (t2, false, NULL); 14688 if (tree_fits_shwi_p (t2)) 14689 t = build2 (MEM_REF, type, t, 14690 build_int_cst (ptype, tree_to_shwi (t2))); 14691 else 14692 { 14693 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2); 14694 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR, 14695 TREE_TYPE (t), t, t2); 14696 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0)); 14697 } 14698 OMP_CLAUSE_DECL (c) = t; 14699 return false; 14700 } 14701 first = c_fully_fold (first, false, NULL); 14702 OMP_CLAUSE_DECL (c) = first; 14703 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR) 14704 return false; 14705 /* Don't set OMP_CLAUSE_SIZE for bare attach/detach clauses. */ 14706 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP 14707 || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH 14708 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH 14709 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH)) 14710 { 14711 if (size) 14712 size = c_fully_fold (size, false, NULL); 14713 OMP_CLAUSE_SIZE (c) = size; 14714 } 14715 14716 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) 14717 return false; 14718 14719 auto_vec<omp_addr_token *, 10> addr_tokens; 14720 14721 if (!omp_parse_expr (addr_tokens, first)) 14722 return true; 14723 14724 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t); 14725 14726 tree nc = ai.expand_map_clause (c, first, addr_tokens, ort); 14727 if (nc != error_mark_node) 14728 { 14729 using namespace omp_addr_tokenizer; 14730 14731 if (ai.maybe_zero_length_array_section (c)) 14732 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1; 14733 14734 /* !!! If we're accessing a base decl via chained access 14735 methods (e.g. multiple indirections), duplicate clause 14736 detection won't work properly. Skip it in that case. */ 14737 if ((addr_tokens[0]->type == STRUCTURE_BASE 14738 || addr_tokens[0]->type == ARRAY_BASE) 14739 && addr_tokens[0]->u.structure_base_kind == BASE_DECL 14740 && addr_tokens[1]->type == ACCESS_METHOD 14741 && omp_access_chain_p (addr_tokens, 1)) 14742 c = nc; 14743 14744 return false; 14745 } 14746 } 14747 return false; 14748 } 14749 14750 /* Helper function of finish_omp_clauses. Clone STMT as if we were making 14751 an inline call. But, remap 14752 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER 14753 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */ 14754 14755 static tree 14756 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2, 14757 tree decl, tree placeholder) 14758 { 14759 copy_body_data id; 14760 hash_map<tree, tree> decl_map; 14761 14762 decl_map.put (omp_decl1, placeholder); 14763 decl_map.put (omp_decl2, decl); 14764 memset (&id, 0, sizeof (id)); 14765 id.src_fn = DECL_CONTEXT (omp_decl1); 14766 id.dst_fn = current_function_decl; 14767 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn); 14768 id.decl_map = &decl_map; 14769 14770 id.copy_decl = copy_decl_no_change; 14771 id.transform_call_graph_edges = CB_CGE_DUPLICATE; 14772 id.transform_new_cfg = true; 14773 id.transform_return_to_modify = false; 14774 id.eh_lp_nr = 0; 14775 walk_tree (&stmt, copy_tree_body_r, &id, NULL); 14776 return stmt; 14777 } 14778 14779 /* Helper function of c_finish_omp_clauses, called via walk_tree. 14780 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */ 14781 14782 static tree 14783 c_find_omp_placeholder_r (tree *tp, int *, void *data) 14784 { 14785 if (*tp == (tree) data) 14786 return *tp; 14787 return NULL_TREE; 14788 } 14789 14790 /* Similarly, but also walk aggregate fields. */ 14791 14792 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; }; 14793 14794 static tree 14795 c_find_omp_var_r (tree *tp, int *, void *data) 14796 { 14797 if (*tp == ((struct c_find_omp_var_s *) data)->var) 14798 return *tp; 14799 if (RECORD_OR_UNION_TYPE_P (*tp)) 14800 { 14801 tree field; 14802 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset; 14803 14804 for (field = TYPE_FIELDS (*tp); field; 14805 field = DECL_CHAIN (field)) 14806 if (TREE_CODE (field) == FIELD_DECL) 14807 { 14808 tree ret = walk_tree (&DECL_FIELD_OFFSET (field), 14809 c_find_omp_var_r, data, pset); 14810 if (ret) 14811 return ret; 14812 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset); 14813 if (ret) 14814 return ret; 14815 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data, 14816 pset); 14817 if (ret) 14818 return ret; 14819 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset); 14820 if (ret) 14821 return ret; 14822 } 14823 } 14824 else if (INTEGRAL_TYPE_P (*tp)) 14825 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data, 14826 ((struct c_find_omp_var_s *) data)->pset); 14827 return NULL_TREE; 14828 } 14829 14830 /* Finish OpenMP iterators ITER. Return true if they are errorneous 14831 and clauses containing them should be removed. */ 14832 14833 static bool 14834 c_omp_finish_iterators (tree iter) 14835 { 14836 bool ret = false; 14837 for (tree it = iter; it; it = TREE_CHAIN (it)) 14838 { 14839 tree var = TREE_VEC_ELT (it, 0); 14840 tree begin = TREE_VEC_ELT (it, 1); 14841 tree end = TREE_VEC_ELT (it, 2); 14842 tree step = TREE_VEC_ELT (it, 3); 14843 tree orig_step; 14844 tree type = TREE_TYPE (var); 14845 location_t loc = DECL_SOURCE_LOCATION (var); 14846 if (type == error_mark_node) 14847 { 14848 ret = true; 14849 continue; 14850 } 14851 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type)) 14852 { 14853 error_at (loc, "iterator %qD has neither integral nor pointer type", 14854 var); 14855 ret = true; 14856 continue; 14857 } 14858 else if (TYPE_ATOMIC (type)) 14859 { 14860 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var); 14861 ret = true; 14862 continue; 14863 } 14864 else if (TYPE_READONLY (type)) 14865 { 14866 error_at (loc, "iterator %qD has const qualified type", var); 14867 ret = true; 14868 continue; 14869 } 14870 else if (step == error_mark_node 14871 || TREE_TYPE (step) == error_mark_node) 14872 { 14873 ret = true; 14874 continue; 14875 } 14876 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step))) 14877 { 14878 error_at (EXPR_LOC_OR_LOC (step, loc), 14879 "iterator step with non-integral type"); 14880 ret = true; 14881 continue; 14882 } 14883 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL); 14884 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL); 14885 orig_step = save_expr (c_fully_fold (step, false, NULL)); 14886 tree stype = POINTER_TYPE_P (type) ? sizetype : type; 14887 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL); 14888 if (POINTER_TYPE_P (type)) 14889 { 14890 begin = save_expr (begin); 14891 step = pointer_int_sum (loc, PLUS_EXPR, begin, step); 14892 step = fold_build2_loc (loc, MINUS_EXPR, sizetype, 14893 fold_convert (sizetype, step), 14894 fold_convert (sizetype, begin)); 14895 step = fold_convert (ssizetype, step); 14896 } 14897 if (integer_zerop (step)) 14898 { 14899 error_at (loc, "iterator %qD has zero step", var); 14900 ret = true; 14901 continue; 14902 } 14903 14904 if (begin == error_mark_node 14905 || end == error_mark_node 14906 || step == error_mark_node 14907 || orig_step == error_mark_node) 14908 { 14909 ret = true; 14910 continue; 14911 } 14912 hash_set<tree> pset; 14913 tree it2; 14914 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2)) 14915 { 14916 tree var2 = TREE_VEC_ELT (it2, 0); 14917 tree begin2 = TREE_VEC_ELT (it2, 1); 14918 tree end2 = TREE_VEC_ELT (it2, 2); 14919 tree step2 = TREE_VEC_ELT (it2, 3); 14920 tree type2 = TREE_TYPE (var2); 14921 location_t loc2 = DECL_SOURCE_LOCATION (var2); 14922 struct c_find_omp_var_s data = { var, &pset }; 14923 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset)) 14924 { 14925 error_at (loc2, 14926 "type of iterator %qD refers to outer iterator %qD", 14927 var2, var); 14928 break; 14929 } 14930 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset)) 14931 { 14932 error_at (EXPR_LOC_OR_LOC (begin2, loc2), 14933 "begin expression refers to outer iterator %qD", var); 14934 break; 14935 } 14936 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset)) 14937 { 14938 error_at (EXPR_LOC_OR_LOC (end2, loc2), 14939 "end expression refers to outer iterator %qD", var); 14940 break; 14941 } 14942 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset)) 14943 { 14944 error_at (EXPR_LOC_OR_LOC (step2, loc2), 14945 "step expression refers to outer iterator %qD", var); 14946 break; 14947 } 14948 } 14949 if (it2) 14950 { 14951 ret = true; 14952 continue; 14953 } 14954 TREE_VEC_ELT (it, 1) = begin; 14955 TREE_VEC_ELT (it, 2) = end; 14956 TREE_VEC_ELT (it, 3) = step; 14957 TREE_VEC_ELT (it, 4) = orig_step; 14958 } 14959 return ret; 14960 } 14961 14962 /* Ensure that pointers are used in OpenACC attach and detach clauses. 14963 Return true if an error has been detected. */ 14964 14965 static bool 14966 c_oacc_check_attachments (tree c) 14967 { 14968 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) 14969 return false; 14970 14971 /* OpenACC attach / detach clauses must be pointers. */ 14972 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH 14973 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH) 14974 { 14975 tree t = OMP_CLAUSE_DECL (c); 14976 14977 while (TREE_CODE (t) == OMP_ARRAY_SECTION) 14978 t = TREE_OPERAND (t, 0); 14979 14980 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE) 14981 { 14982 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause", 14983 user_omp_clause_code_name (c, true)); 14984 return true; 14985 } 14986 } 14987 14988 return false; 14989 } 14990 14991 /* For all elements of CLAUSES, validate them against their constraints. 14992 Remove any elements from the list that are invalid. */ 14993 14994 tree 14995 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort) 14996 { 14997 bitmap_head generic_head, firstprivate_head, lastprivate_head; 14998 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head; 14999 bitmap_head oacc_reduction_head, is_on_device_head; 15000 tree c, t, type, *pc; 15001 tree simdlen = NULL_TREE, safelen = NULL_TREE; 15002 bool branch_seen = false; 15003 bool copyprivate_seen = false; 15004 bool mergeable_seen = false; 15005 tree *detach_seen = NULL; 15006 bool linear_variable_step_check = false; 15007 tree *nowait_clause = NULL; 15008 tree ordered_clause = NULL_TREE; 15009 tree schedule_clause = NULL_TREE; 15010 bool oacc_async = false; 15011 tree last_iterators = NULL_TREE; 15012 bool last_iterators_remove = false; 15013 tree *nogroup_seen = NULL; 15014 tree *order_clause = NULL; 15015 /* 1 if normal/task reduction has been seen, -1 if inscan reduction 15016 has been seen, -2 if mixed inscan/normal reduction diagnosed. */ 15017 int reduction_seen = 0; 15018 bool allocate_seen = false; 15019 bool implicit_moved = false; 15020 bool target_in_reduction_seen = false; 15021 bool openacc = (ort & C_ORT_ACC) != 0; 15022 15023 bitmap_obstack_initialize (NULL); 15024 bitmap_initialize (&generic_head, &bitmap_default_obstack); 15025 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack); 15026 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack); 15027 bitmap_initialize (&aligned_head, &bitmap_default_obstack); 15028 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */ 15029 bitmap_initialize (&map_head, &bitmap_default_obstack); 15030 bitmap_initialize (&map_field_head, &bitmap_default_obstack); 15031 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack); 15032 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head 15033 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */ 15034 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack); 15035 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack); 15036 15037 if (openacc) 15038 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c)) 15039 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC) 15040 { 15041 oacc_async = true; 15042 break; 15043 } 15044 15045 tree *grp_start_p = NULL, grp_sentinel = NULL_TREE; 15046 15047 for (pc = &clauses, c = clauses; c ; c = *pc) 15048 { 15049 bool remove = false; 15050 bool need_complete = false; 15051 bool need_implicitly_determined = false; 15052 15053 /* We've reached the end of a list of expanded nodes. Reset the group 15054 start pointer. */ 15055 if (c == grp_sentinel) 15056 grp_start_p = NULL; 15057 15058 switch (OMP_CLAUSE_CODE (c)) 15059 { 15060 case OMP_CLAUSE_SHARED: 15061 need_implicitly_determined = true; 15062 goto check_dup_generic; 15063 15064 case OMP_CLAUSE_PRIVATE: 15065 need_complete = true; 15066 need_implicitly_determined = true; 15067 goto check_dup_generic; 15068 15069 case OMP_CLAUSE_REDUCTION: 15070 if (reduction_seen == 0) 15071 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1; 15072 else if (reduction_seen != -2 15073 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c) 15074 ? -1 : 1)) 15075 { 15076 error_at (OMP_CLAUSE_LOCATION (c), 15077 "%<inscan%> and non-%<inscan%> %<reduction%> clauses " 15078 "on the same construct"); 15079 reduction_seen = -2; 15080 } 15081 /* FALLTHRU */ 15082 case OMP_CLAUSE_IN_REDUCTION: 15083 case OMP_CLAUSE_TASK_REDUCTION: 15084 need_implicitly_determined = true; 15085 t = OMP_CLAUSE_DECL (c); 15086 if (TREE_CODE (t) == OMP_ARRAY_SECTION) 15087 { 15088 if (handle_omp_array_sections (c, ort)) 15089 { 15090 remove = true; 15091 break; 15092 } 15093 15094 t = OMP_CLAUSE_DECL (c); 15095 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION 15096 && OMP_CLAUSE_REDUCTION_INSCAN (c)) 15097 { 15098 error_at (OMP_CLAUSE_LOCATION (c), 15099 "%<inscan%> %<reduction%> clause with array " 15100 "section"); 15101 remove = true; 15102 break; 15103 } 15104 } 15105 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t); 15106 if (t == error_mark_node) 15107 { 15108 remove = true; 15109 break; 15110 } 15111 if (oacc_async) 15112 c_mark_addressable (t); 15113 type = TREE_TYPE (t); 15114 if (TREE_CODE (t) == MEM_REF) 15115 type = TREE_TYPE (type); 15116 if (TREE_CODE (type) == ARRAY_TYPE) 15117 { 15118 tree oatype = type; 15119 gcc_assert (TREE_CODE (t) != MEM_REF); 15120 while (TREE_CODE (type) == ARRAY_TYPE) 15121 type = TREE_TYPE (type); 15122 if (integer_zerop (TYPE_SIZE_UNIT (type))) 15123 { 15124 error_at (OMP_CLAUSE_LOCATION (c), 15125 "%qD in %<reduction%> clause is a zero size array", 15126 t); 15127 remove = true; 15128 break; 15129 } 15130 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype), 15131 TYPE_SIZE_UNIT (type)); 15132 if (integer_zerop (size)) 15133 { 15134 error_at (OMP_CLAUSE_LOCATION (c), 15135 "%qD in %<reduction%> clause is a zero size array", 15136 t); 15137 remove = true; 15138 break; 15139 } 15140 size = size_binop (MINUS_EXPR, size, size_one_node); 15141 size = save_expr (size); 15142 tree index_type = build_index_type (size); 15143 tree atype = c_build_array_type (TYPE_MAIN_VARIANT (type), 15144 index_type); 15145 atype = c_build_qualified_type (atype, TYPE_QUALS (type)); 15146 tree ptype = c_build_pointer_type (type); 15147 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 15148 t = build_fold_addr_expr (t); 15149 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0)); 15150 OMP_CLAUSE_DECL (c) = t; 15151 } 15152 if (TYPE_ATOMIC (type)) 15153 { 15154 error_at (OMP_CLAUSE_LOCATION (c), 15155 "%<_Atomic%> %qE in %<reduction%> clause", t); 15156 remove = true; 15157 break; 15158 } 15159 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION 15160 || OMP_CLAUSE_REDUCTION_TASK (c)) 15161 { 15162 /* Disallow zero sized or potentially zero sized task 15163 reductions. */ 15164 if (integer_zerop (TYPE_SIZE_UNIT (type))) 15165 { 15166 error_at (OMP_CLAUSE_LOCATION (c), 15167 "zero sized type %qT in %qs clause", type, 15168 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 15169 remove = true; 15170 break; 15171 } 15172 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST) 15173 { 15174 error_at (OMP_CLAUSE_LOCATION (c), 15175 "variable sized type %qT in %qs clause", type, 15176 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 15177 remove = true; 15178 break; 15179 } 15180 } 15181 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE 15182 && (FLOAT_TYPE_P (type) 15183 || TREE_CODE (type) == COMPLEX_TYPE)) 15184 { 15185 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c); 15186 const char *r_name = NULL; 15187 15188 switch (r_code) 15189 { 15190 case PLUS_EXPR: 15191 case MULT_EXPR: 15192 case MINUS_EXPR: 15193 case TRUTH_ANDIF_EXPR: 15194 case TRUTH_ORIF_EXPR: 15195 break; 15196 case MIN_EXPR: 15197 if (TREE_CODE (type) == COMPLEX_TYPE) 15198 r_name = "min"; 15199 break; 15200 case MAX_EXPR: 15201 if (TREE_CODE (type) == COMPLEX_TYPE) 15202 r_name = "max"; 15203 break; 15204 case BIT_AND_EXPR: 15205 r_name = "&"; 15206 break; 15207 case BIT_XOR_EXPR: 15208 r_name = "^"; 15209 break; 15210 case BIT_IOR_EXPR: 15211 r_name = "|"; 15212 break; 15213 default: 15214 gcc_unreachable (); 15215 } 15216 if (r_name) 15217 { 15218 error_at (OMP_CLAUSE_LOCATION (c), 15219 "%qE has invalid type for %<reduction(%s)%>", 15220 t, r_name); 15221 remove = true; 15222 break; 15223 } 15224 } 15225 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node) 15226 { 15227 error_at (OMP_CLAUSE_LOCATION (c), 15228 "user defined reduction not found for %qE", t); 15229 remove = true; 15230 break; 15231 } 15232 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)) 15233 { 15234 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c); 15235 type = TYPE_MAIN_VARIANT (type); 15236 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c), 15237 VAR_DECL, NULL_TREE, type); 15238 tree decl_placeholder = NULL_TREE; 15239 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder; 15240 DECL_ARTIFICIAL (placeholder) = 1; 15241 DECL_IGNORED_P (placeholder) = 1; 15242 if (TREE_CODE (t) == MEM_REF) 15243 { 15244 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c), 15245 VAR_DECL, NULL_TREE, type); 15246 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder; 15247 DECL_ARTIFICIAL (decl_placeholder) = 1; 15248 DECL_IGNORED_P (decl_placeholder) = 1; 15249 } 15250 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0))) 15251 c_mark_addressable (placeholder); 15252 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1))) 15253 c_mark_addressable (decl_placeholder ? decl_placeholder 15254 : OMP_CLAUSE_DECL (c)); 15255 OMP_CLAUSE_REDUCTION_MERGE (c) 15256 = c_clone_omp_udr (TREE_VEC_ELT (list, 2), 15257 TREE_VEC_ELT (list, 0), 15258 TREE_VEC_ELT (list, 1), 15259 decl_placeholder ? decl_placeholder 15260 : OMP_CLAUSE_DECL (c), placeholder); 15261 OMP_CLAUSE_REDUCTION_MERGE (c) 15262 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR, 15263 void_type_node, NULL_TREE, 15264 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE); 15265 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1; 15266 if (TREE_VEC_LENGTH (list) == 6) 15267 { 15268 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3))) 15269 c_mark_addressable (decl_placeholder ? decl_placeholder 15270 : OMP_CLAUSE_DECL (c)); 15271 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4))) 15272 c_mark_addressable (placeholder); 15273 tree init = TREE_VEC_ELT (list, 5); 15274 if (init == error_mark_node) 15275 init = DECL_INITIAL (TREE_VEC_ELT (list, 3)); 15276 OMP_CLAUSE_REDUCTION_INIT (c) 15277 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4), 15278 TREE_VEC_ELT (list, 3), 15279 decl_placeholder ? decl_placeholder 15280 : OMP_CLAUSE_DECL (c), placeholder); 15281 if (TREE_VEC_ELT (list, 5) == error_mark_node) 15282 { 15283 tree v = decl_placeholder ? decl_placeholder : t; 15284 OMP_CLAUSE_REDUCTION_INIT (c) 15285 = build2 (INIT_EXPR, TREE_TYPE (v), v, 15286 OMP_CLAUSE_REDUCTION_INIT (c)); 15287 } 15288 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c), 15289 c_find_omp_placeholder_r, 15290 placeholder, NULL)) 15291 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1; 15292 } 15293 else 15294 { 15295 tree init; 15296 tree v = decl_placeholder ? decl_placeholder : t; 15297 if (AGGREGATE_TYPE_P (TREE_TYPE (v))) 15298 init = build_constructor (TREE_TYPE (v), NULL); 15299 else 15300 init = fold_convert (TREE_TYPE (v), integer_zero_node); 15301 OMP_CLAUSE_REDUCTION_INIT (c) 15302 = build2 (INIT_EXPR, TREE_TYPE (v), v, init); 15303 } 15304 OMP_CLAUSE_REDUCTION_INIT (c) 15305 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR, 15306 void_type_node, NULL_TREE, 15307 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE); 15308 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1; 15309 } 15310 if (TREE_CODE (t) == MEM_REF) 15311 { 15312 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE 15313 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)))) 15314 != INTEGER_CST) 15315 { 15316 sorry ("variable length element type in array " 15317 "%<reduction%> clause"); 15318 remove = true; 15319 break; 15320 } 15321 t = TREE_OPERAND (t, 0); 15322 if (TREE_CODE (t) == POINTER_PLUS_EXPR) 15323 t = TREE_OPERAND (t, 0); 15324 if (TREE_CODE (t) == ADDR_EXPR) 15325 t = TREE_OPERAND (t, 0); 15326 } 15327 goto check_dup_generic_t; 15328 15329 case OMP_CLAUSE_COPYPRIVATE: 15330 copyprivate_seen = true; 15331 if (nowait_clause) 15332 { 15333 error_at (OMP_CLAUSE_LOCATION (*nowait_clause), 15334 "%<nowait%> clause must not be used together " 15335 "with %<copyprivate%>"); 15336 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause); 15337 nowait_clause = NULL; 15338 } 15339 goto check_dup_generic; 15340 15341 case OMP_CLAUSE_COPYIN: 15342 t = OMP_CLAUSE_DECL (c); 15343 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t)) 15344 { 15345 error_at (OMP_CLAUSE_LOCATION (c), 15346 "%qE must be %<threadprivate%> for %<copyin%>", t); 15347 remove = true; 15348 break; 15349 } 15350 goto check_dup_generic; 15351 15352 case OMP_CLAUSE_LINEAR: 15353 if (ort != C_ORT_OMP_DECLARE_SIMD) 15354 need_implicitly_determined = true; 15355 t = OMP_CLAUSE_DECL (c); 15356 if (ort != C_ORT_OMP_DECLARE_SIMD 15357 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT 15358 && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c)) 15359 { 15360 error_at (OMP_CLAUSE_LOCATION (c), 15361 "modifier should not be specified in %<linear%> " 15362 "clause on %<simd%> or %<for%> constructs when not " 15363 "using OpenMP 5.2 modifiers"); 15364 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT; 15365 } 15366 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)) 15367 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE) 15368 { 15369 error_at (OMP_CLAUSE_LOCATION (c), 15370 "linear clause applied to non-integral non-pointer " 15371 "variable with type %qT", TREE_TYPE (t)); 15372 remove = true; 15373 break; 15374 } 15375 if (TYPE_ATOMIC (TREE_TYPE (t))) 15376 { 15377 error_at (OMP_CLAUSE_LOCATION (c), 15378 "%<_Atomic%> %qD in %<linear%> clause", t); 15379 remove = true; 15380 break; 15381 } 15382 if (ort == C_ORT_OMP_DECLARE_SIMD) 15383 { 15384 tree s = OMP_CLAUSE_LINEAR_STEP (c); 15385 if (TREE_CODE (s) == PARM_DECL) 15386 { 15387 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1; 15388 /* map_head bitmap is used as uniform_head if 15389 declare_simd. */ 15390 if (!bitmap_bit_p (&map_head, DECL_UID (s))) 15391 linear_variable_step_check = true; 15392 goto check_dup_generic; 15393 } 15394 if (TREE_CODE (s) != INTEGER_CST) 15395 { 15396 error_at (OMP_CLAUSE_LOCATION (c), 15397 "%<linear%> clause step %qE is neither constant " 15398 "nor a parameter", s); 15399 remove = true; 15400 break; 15401 } 15402 } 15403 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE) 15404 { 15405 tree s = OMP_CLAUSE_LINEAR_STEP (c); 15406 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR, 15407 OMP_CLAUSE_DECL (c), s); 15408 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR, 15409 sizetype, fold_convert (sizetype, s), 15410 fold_convert 15411 (sizetype, OMP_CLAUSE_DECL (c))); 15412 if (s == error_mark_node) 15413 s = size_one_node; 15414 OMP_CLAUSE_LINEAR_STEP (c) = s; 15415 } 15416 else 15417 OMP_CLAUSE_LINEAR_STEP (c) 15418 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c)); 15419 goto check_dup_generic; 15420 15421 check_dup_generic: 15422 t = OMP_CLAUSE_DECL (c); 15423 check_dup_generic_t: 15424 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) 15425 { 15426 error_at (OMP_CLAUSE_LOCATION (c), 15427 "%qE is not a variable in clause %qs", t, 15428 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 15429 remove = true; 15430 } 15431 else if ((openacc && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION) 15432 || (ort == C_ORT_OMP 15433 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR 15434 || (OMP_CLAUSE_CODE (c) 15435 == OMP_CLAUSE_USE_DEVICE_ADDR))) 15436 || (ort == C_ORT_OMP_TARGET 15437 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)) 15438 { 15439 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION 15440 && (bitmap_bit_p (&generic_head, DECL_UID (t)) 15441 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))) 15442 { 15443 error_at (OMP_CLAUSE_LOCATION (c), 15444 "%qD appears more than once in data-sharing " 15445 "clauses", t); 15446 remove = true; 15447 break; 15448 } 15449 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION) 15450 target_in_reduction_seen = true; 15451 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t))) 15452 { 15453 error_at (OMP_CLAUSE_LOCATION (c), 15454 openacc 15455 ? "%qD appears more than once in reduction clauses" 15456 : "%qD appears more than once in data clauses", 15457 t); 15458 remove = true; 15459 } 15460 else 15461 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t)); 15462 } 15463 else if (bitmap_bit_p (&generic_head, DECL_UID (t)) 15464 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) 15465 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)) 15466 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))) 15467 { 15468 error_at (OMP_CLAUSE_LOCATION (c), 15469 "%qE appears more than once in data clauses", t); 15470 remove = true; 15471 } 15472 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE 15473 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR 15474 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR) 15475 && bitmap_bit_p (&map_head, DECL_UID (t))) 15476 { 15477 if (openacc) 15478 error_at (OMP_CLAUSE_LOCATION (c), 15479 "%qD appears more than once in data clauses", t); 15480 else 15481 error_at (OMP_CLAUSE_LOCATION (c), 15482 "%qD appears both in data and map clauses", t); 15483 remove = true; 15484 } 15485 else 15486 bitmap_set_bit (&generic_head, DECL_UID (t)); 15487 break; 15488 15489 case OMP_CLAUSE_FIRSTPRIVATE: 15490 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved) 15491 { 15492 move_implicit: 15493 implicit_moved = true; 15494 /* Move firstprivate and map clauses with 15495 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of 15496 clauses chain. */ 15497 tree cl1 = NULL_TREE, cl2 = NULL_TREE; 15498 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2; 15499 while (*pc1) 15500 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE 15501 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1)) 15502 { 15503 *pc3 = *pc1; 15504 pc3 = &OMP_CLAUSE_CHAIN (*pc3); 15505 *pc1 = OMP_CLAUSE_CHAIN (*pc1); 15506 } 15507 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP 15508 && OMP_CLAUSE_MAP_IMPLICIT (*pc1)) 15509 { 15510 *pc2 = *pc1; 15511 pc2 = &OMP_CLAUSE_CHAIN (*pc2); 15512 *pc1 = OMP_CLAUSE_CHAIN (*pc1); 15513 } 15514 else 15515 pc1 = &OMP_CLAUSE_CHAIN (*pc1); 15516 *pc3 = NULL; 15517 *pc2 = cl2; 15518 *pc1 = cl1; 15519 continue; 15520 } 15521 t = OMP_CLAUSE_DECL (c); 15522 need_complete = true; 15523 need_implicitly_determined = true; 15524 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) 15525 { 15526 error_at (OMP_CLAUSE_LOCATION (c), 15527 "%qE is not a variable in clause %<firstprivate%>", t); 15528 remove = true; 15529 } 15530 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) 15531 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c) 15532 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))) 15533 remove = true; 15534 else if (bitmap_bit_p (&generic_head, DECL_UID (t)) 15535 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) 15536 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))) 15537 { 15538 error_at (OMP_CLAUSE_LOCATION (c), 15539 "%qE appears more than once in data clauses", t); 15540 remove = true; 15541 } 15542 else if (bitmap_bit_p (&map_head, DECL_UID (t)) 15543 || bitmap_bit_p (&map_field_head, DECL_UID (t))) 15544 { 15545 if (openacc) 15546 error_at (OMP_CLAUSE_LOCATION (c), 15547 "%qD appears more than once in data clauses", t); 15548 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) 15549 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)) 15550 /* Silently drop the clause. */; 15551 else 15552 error_at (OMP_CLAUSE_LOCATION (c), 15553 "%qD appears both in data and map clauses", t); 15554 remove = true; 15555 } 15556 else 15557 bitmap_set_bit (&firstprivate_head, DECL_UID (t)); 15558 break; 15559 15560 case OMP_CLAUSE_LASTPRIVATE: 15561 t = OMP_CLAUSE_DECL (c); 15562 need_complete = true; 15563 need_implicitly_determined = true; 15564 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) 15565 { 15566 error_at (OMP_CLAUSE_LOCATION (c), 15567 "%qE is not a variable in clause %<lastprivate%>", t); 15568 remove = true; 15569 } 15570 else if (bitmap_bit_p (&generic_head, DECL_UID (t)) 15571 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) 15572 { 15573 error_at (OMP_CLAUSE_LOCATION (c), 15574 "%qE appears more than once in data clauses", t); 15575 remove = true; 15576 } 15577 else 15578 bitmap_set_bit (&lastprivate_head, DECL_UID (t)); 15579 break; 15580 15581 case OMP_CLAUSE_ALIGNED: 15582 t = OMP_CLAUSE_DECL (c); 15583 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) 15584 { 15585 error_at (OMP_CLAUSE_LOCATION (c), 15586 "%qE is not a variable in %<aligned%> clause", t); 15587 remove = true; 15588 } 15589 else if (!POINTER_TYPE_P (TREE_TYPE (t)) 15590 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE) 15591 { 15592 error_at (OMP_CLAUSE_LOCATION (c), 15593 "%qE in %<aligned%> clause is neither a pointer nor " 15594 "an array", t); 15595 remove = true; 15596 } 15597 else if (TYPE_ATOMIC (TREE_TYPE (t))) 15598 { 15599 error_at (OMP_CLAUSE_LOCATION (c), 15600 "%<_Atomic%> %qD in %<aligned%> clause", t); 15601 remove = true; 15602 break; 15603 } 15604 else if (bitmap_bit_p (&aligned_head, DECL_UID (t))) 15605 { 15606 error_at (OMP_CLAUSE_LOCATION (c), 15607 "%qE appears more than once in %<aligned%> clauses", 15608 t); 15609 remove = true; 15610 } 15611 else 15612 bitmap_set_bit (&aligned_head, DECL_UID (t)); 15613 break; 15614 15615 case OMP_CLAUSE_NONTEMPORAL: 15616 t = OMP_CLAUSE_DECL (c); 15617 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) 15618 { 15619 error_at (OMP_CLAUSE_LOCATION (c), 15620 "%qE is not a variable in %<nontemporal%> clause", t); 15621 remove = true; 15622 } 15623 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t))) 15624 { 15625 error_at (OMP_CLAUSE_LOCATION (c), 15626 "%qE appears more than once in %<nontemporal%> " 15627 "clauses", t); 15628 remove = true; 15629 } 15630 else 15631 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t)); 15632 break; 15633 15634 case OMP_CLAUSE_ALLOCATE: 15635 t = OMP_CLAUSE_DECL (c); 15636 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) 15637 { 15638 error_at (OMP_CLAUSE_LOCATION (c), 15639 "%qE is not a variable in %<allocate%> clause", t); 15640 remove = true; 15641 } 15642 else if (bitmap_bit_p (&aligned_head, DECL_UID (t))) 15643 { 15644 warning_at (OMP_CLAUSE_LOCATION (c), 0, 15645 "%qE appears more than once in %<allocate%> clauses", 15646 t); 15647 remove = true; 15648 } 15649 else 15650 { 15651 bitmap_set_bit (&aligned_head, DECL_UID (t)); 15652 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c)) 15653 allocate_seen = true; 15654 } 15655 break; 15656 15657 case OMP_CLAUSE_DOACROSS: 15658 t = OMP_CLAUSE_DECL (c); 15659 if (t == NULL_TREE) 15660 break; 15661 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK) 15662 { 15663 gcc_assert (TREE_CODE (t) == TREE_LIST); 15664 for (; t; t = TREE_CHAIN (t)) 15665 { 15666 tree decl = TREE_VALUE (t); 15667 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE) 15668 { 15669 tree offset = TREE_PURPOSE (t); 15670 bool neg = wi::neg_p (wi::to_wide (offset)); 15671 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset); 15672 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c), 15673 neg ? MINUS_EXPR : PLUS_EXPR, 15674 decl, offset); 15675 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR, 15676 sizetype, 15677 fold_convert (sizetype, t2), 15678 fold_convert (sizetype, decl)); 15679 if (t2 == error_mark_node) 15680 { 15681 remove = true; 15682 break; 15683 } 15684 TREE_PURPOSE (t) = t2; 15685 } 15686 } 15687 break; 15688 } 15689 gcc_unreachable (); 15690 case OMP_CLAUSE_DEPEND: 15691 case OMP_CLAUSE_AFFINITY: 15692 t = OMP_CLAUSE_DECL (c); 15693 if (TREE_CODE (t) == TREE_LIST 15694 && TREE_PURPOSE (t) 15695 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC) 15696 { 15697 if (TREE_PURPOSE (t) != last_iterators) 15698 last_iterators_remove 15699 = c_omp_finish_iterators (TREE_PURPOSE (t)); 15700 last_iterators = TREE_PURPOSE (t); 15701 t = TREE_VALUE (t); 15702 if (last_iterators_remove) 15703 t = error_mark_node; 15704 } 15705 else 15706 last_iterators = NULL_TREE; 15707 if (TREE_CODE (t) == OMP_ARRAY_SECTION) 15708 { 15709 if (handle_omp_array_sections (c, ort)) 15710 remove = true; 15711 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND 15712 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ) 15713 { 15714 error_at (OMP_CLAUSE_LOCATION (c), 15715 "%<depend%> clause with %<depobj%> dependence " 15716 "type on array section"); 15717 remove = true; 15718 } 15719 break; 15720 } 15721 if (t == error_mark_node) 15722 remove = true; 15723 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND 15724 && t == ridpointers[RID_OMP_ALL_MEMORY]) 15725 { 15726 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT 15727 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT) 15728 { 15729 error_at (OMP_CLAUSE_LOCATION (c), 15730 "%<omp_all_memory%> used with %<depend%> kind " 15731 "other than %<out%> or %<inout%>"); 15732 remove = true; 15733 } 15734 } 15735 else if (!lvalue_p (t)) 15736 { 15737 error_at (OMP_CLAUSE_LOCATION (c), 15738 "%qE is not lvalue expression nor array section in " 15739 "%qs clause", t, 15740 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 15741 remove = true; 15742 } 15743 else if (TREE_CODE (t) == COMPONENT_REF 15744 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1))) 15745 { 15746 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND 15747 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY); 15748 error_at (OMP_CLAUSE_LOCATION (c), 15749 "bit-field %qE in %qs clause", t, 15750 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 15751 remove = true; 15752 } 15753 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND 15754 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ) 15755 { 15756 if (!c_omp_depend_t_p (TREE_TYPE (t))) 15757 { 15758 error_at (OMP_CLAUSE_LOCATION (c), 15759 "%qE does not have %<omp_depend_t%> type in " 15760 "%<depend%> clause with %<depobj%> dependence " 15761 "type", t); 15762 remove = true; 15763 } 15764 } 15765 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND 15766 && c_omp_depend_t_p (TREE_TYPE (t))) 15767 { 15768 error_at (OMP_CLAUSE_LOCATION (c), 15769 "%qE should not have %<omp_depend_t%> type in " 15770 "%<depend%> clause with dependence type other than " 15771 "%<depobj%>", t); 15772 remove = true; 15773 } 15774 if (!remove) 15775 { 15776 if (t == ridpointers[RID_OMP_ALL_MEMORY]) 15777 t = null_pointer_node; 15778 else 15779 { 15780 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), 15781 ADDR_EXPR, t, false); 15782 if (addr == error_mark_node) 15783 { 15784 remove = true; 15785 break; 15786 } 15787 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr, 15788 RO_UNARY_STAR); 15789 if (t == error_mark_node) 15790 { 15791 remove = true; 15792 break; 15793 } 15794 } 15795 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST 15796 && TREE_PURPOSE (OMP_CLAUSE_DECL (c)) 15797 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c))) 15798 == TREE_VEC)) 15799 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t; 15800 else 15801 OMP_CLAUSE_DECL (c) = t; 15802 } 15803 break; 15804 15805 case OMP_CLAUSE_MAP: 15806 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved) 15807 goto move_implicit; 15808 /* FALLTHRU */ 15809 case OMP_CLAUSE_TO: 15810 case OMP_CLAUSE_FROM: 15811 case OMP_CLAUSE__CACHE_: 15812 { 15813 using namespace omp_addr_tokenizer; 15814 auto_vec<omp_addr_token *, 10> addr_tokens; 15815 15816 t = OMP_CLAUSE_DECL (c); 15817 if (TREE_CODE (t) == OMP_ARRAY_SECTION) 15818 { 15819 grp_start_p = pc; 15820 grp_sentinel = OMP_CLAUSE_CHAIN (c); 15821 15822 if (handle_omp_array_sections (c, ort)) 15823 remove = true; 15824 else 15825 { 15826 t = OMP_CLAUSE_DECL (c); 15827 if (!omp_mappable_type (TREE_TYPE (t))) 15828 { 15829 error_at (OMP_CLAUSE_LOCATION (c), 15830 "array section does not have mappable type " 15831 "in %qs clause", 15832 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 15833 remove = true; 15834 } 15835 else if (TYPE_ATOMIC (TREE_TYPE (t))) 15836 { 15837 error_at (OMP_CLAUSE_LOCATION (c), 15838 "%<_Atomic%> %qE in %qs clause", t, 15839 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 15840 remove = true; 15841 } 15842 while (TREE_CODE (t) == ARRAY_REF) 15843 t = TREE_OPERAND (t, 0); 15844 15845 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t); 15846 15847 if (!omp_parse_expr (addr_tokens, t)) 15848 { 15849 sorry_at (OMP_CLAUSE_LOCATION (c), 15850 "unsupported map expression %qE", 15851 OMP_CLAUSE_DECL (c)); 15852 remove = true; 15853 break; 15854 } 15855 15856 /* This check is to determine if this will be the only map 15857 node created for this clause. Otherwise, we'll check 15858 the following FIRSTPRIVATE_POINTER or ATTACH_DETACH 15859 node on the next iteration(s) of the loop. */ 15860 if (addr_tokens.length () >= 4 15861 && addr_tokens[0]->type == STRUCTURE_BASE 15862 && addr_tokens[0]->u.structure_base_kind == BASE_DECL 15863 && addr_tokens[1]->type == ACCESS_METHOD 15864 && addr_tokens[2]->type == COMPONENT_SELECTOR 15865 && addr_tokens[3]->type == ACCESS_METHOD 15866 && (addr_tokens[3]->u.access_kind == ACCESS_DIRECT 15867 || (addr_tokens[3]->u.access_kind 15868 == ACCESS_INDEXED_ARRAY))) 15869 { 15870 tree rt = addr_tokens[1]->expr; 15871 15872 gcc_assert (DECL_P (rt)); 15873 15874 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP 15875 && OMP_CLAUSE_MAP_IMPLICIT (c) 15876 && (bitmap_bit_p (&map_head, DECL_UID (rt)) 15877 || bitmap_bit_p (&map_field_head, DECL_UID (rt)) 15878 || bitmap_bit_p (&map_firstprivate_head, 15879 DECL_UID (rt)))) 15880 { 15881 remove = true; 15882 break; 15883 } 15884 if (bitmap_bit_p (&map_field_head, DECL_UID (rt))) 15885 break; 15886 if (bitmap_bit_p (&map_head, DECL_UID (rt))) 15887 { 15888 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) 15889 error_at (OMP_CLAUSE_LOCATION (c), 15890 "%qD appears more than once in motion " 15891 "clauses", rt); 15892 else if (openacc) 15893 error_at (OMP_CLAUSE_LOCATION (c), 15894 "%qD appears more than once in data " 15895 "clauses", rt); 15896 else 15897 error_at (OMP_CLAUSE_LOCATION (c), 15898 "%qD appears more than once in map " 15899 "clauses", rt); 15900 remove = true; 15901 } 15902 else 15903 { 15904 bitmap_set_bit (&map_head, DECL_UID (rt)); 15905 bitmap_set_bit (&map_field_head, DECL_UID (rt)); 15906 } 15907 } 15908 } 15909 if (c_oacc_check_attachments (c)) 15910 remove = true; 15911 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP 15912 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH 15913 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH) 15914 && !OMP_CLAUSE_SIZE (c)) 15915 /* In this case, we have a single array element which is a 15916 pointer, and we already set OMP_CLAUSE_SIZE in 15917 handle_omp_array_sections above. For attach/detach 15918 clauses, reset the OMP_CLAUSE_SIZE (representing a bias) 15919 to zero here. */ 15920 OMP_CLAUSE_SIZE (c) = size_zero_node; 15921 break; 15922 } 15923 else if (!omp_parse_expr (addr_tokens, t)) 15924 { 15925 sorry_at (OMP_CLAUSE_LOCATION (c), 15926 "unsupported map expression %qE", 15927 OMP_CLAUSE_DECL (c)); 15928 remove = true; 15929 break; 15930 } 15931 if (t == error_mark_node) 15932 { 15933 remove = true; 15934 break; 15935 } 15936 /* OpenACC attach / detach clauses must be pointers. */ 15937 if (c_oacc_check_attachments (c)) 15938 { 15939 remove = true; 15940 break; 15941 } 15942 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP 15943 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH 15944 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH) 15945 && !OMP_CLAUSE_SIZE (c)) 15946 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a 15947 bias) to zero here, so it is not set erroneously to the pointer 15948 size later on in gimplify.cc. */ 15949 OMP_CLAUSE_SIZE (c) = size_zero_node; 15950 15951 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t); 15952 15953 if (!ai.check_clause (c)) 15954 { 15955 remove = true; 15956 break; 15957 } 15958 15959 if (!ai.map_supported_p ()) 15960 { 15961 sorry_at (OMP_CLAUSE_LOCATION (c), 15962 "unsupported map expression %qE", 15963 OMP_CLAUSE_DECL (c)); 15964 remove = true; 15965 break; 15966 } 15967 15968 gcc_assert ((addr_tokens[0]->type == ARRAY_BASE 15969 || addr_tokens[0]->type == STRUCTURE_BASE) 15970 && addr_tokens[1]->type == ACCESS_METHOD); 15971 15972 t = addr_tokens[1]->expr; 15973 15974 if (addr_tokens[0]->u.structure_base_kind != BASE_DECL) 15975 goto skip_decl_checks; 15976 15977 /* For OpenMP, we can access a struct "t" and "t.d" on the same 15978 mapping. OpenACC allows multiple fields of the same structure 15979 to be written. */ 15980 if (addr_tokens[0]->type == STRUCTURE_BASE 15981 && (bitmap_bit_p (&map_field_head, DECL_UID (t)) 15982 || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t))))) 15983 goto skip_decl_checks; 15984 15985 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) 15986 { 15987 if (ort != C_ORT_ACC && EXPR_P (t)) 15988 break; 15989 15990 error_at (OMP_CLAUSE_LOCATION (c), 15991 "%qE is not a variable in %qs clause", t, 15992 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 15993 remove = true; 15994 } 15995 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t)) 15996 { 15997 error_at (OMP_CLAUSE_LOCATION (c), 15998 "%qD is threadprivate variable in %qs clause", t, 15999 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 16000 remove = true; 16001 } 16002 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP 16003 || (OMP_CLAUSE_MAP_KIND (c) 16004 != GOMP_MAP_FIRSTPRIVATE_POINTER)) 16005 && !c_mark_addressable (t)) 16006 remove = true; 16007 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP 16008 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER 16009 || (OMP_CLAUSE_MAP_KIND (c) 16010 == GOMP_MAP_FIRSTPRIVATE_POINTER) 16011 || (OMP_CLAUSE_MAP_KIND (c) 16012 == GOMP_MAP_FORCE_DEVICEPTR))) 16013 && t == OMP_CLAUSE_DECL (c) 16014 && !omp_mappable_type (TREE_TYPE (t))) 16015 { 16016 error_at (OMP_CLAUSE_LOCATION (c), 16017 "%qD does not have a mappable type in %qs clause", t, 16018 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 16019 remove = true; 16020 } 16021 else if (TREE_TYPE (t) == error_mark_node) 16022 remove = true; 16023 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t)))) 16024 { 16025 error_at (OMP_CLAUSE_LOCATION (c), 16026 "%<_Atomic%> %qE in %qs clause", t, 16027 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 16028 remove = true; 16029 } 16030 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP 16031 && OMP_CLAUSE_MAP_IMPLICIT (c) 16032 && (bitmap_bit_p (&map_head, DECL_UID (t)) 16033 || bitmap_bit_p (&map_field_head, DECL_UID (t)) 16034 || bitmap_bit_p (&map_firstprivate_head, 16035 DECL_UID (t)))) 16036 remove = true; 16037 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP 16038 && (OMP_CLAUSE_MAP_KIND (c) 16039 == GOMP_MAP_FIRSTPRIVATE_POINTER)) 16040 { 16041 if (bitmap_bit_p (&generic_head, DECL_UID (t)) 16042 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) 16043 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))) 16044 { 16045 error_at (OMP_CLAUSE_LOCATION (c), 16046 "%qD appears more than once in data clauses", t); 16047 remove = true; 16048 } 16049 else if (bitmap_bit_p (&map_head, DECL_UID (t)) 16050 && !bitmap_bit_p (&map_field_head, DECL_UID (t)) 16051 && openacc) 16052 { 16053 error_at (OMP_CLAUSE_LOCATION (c), 16054 "%qD appears more than once in data clauses", t); 16055 remove = true; 16056 } 16057 else 16058 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t)); 16059 } 16060 else if (bitmap_bit_p (&map_head, DECL_UID (t)) 16061 && !bitmap_bit_p (&map_field_head, DECL_UID (t)) 16062 && ort != C_ORT_OMP 16063 && ort != C_ORT_OMP_EXIT_DATA) 16064 { 16065 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) 16066 error_at (OMP_CLAUSE_LOCATION (c), 16067 "%qD appears more than once in motion clauses", t); 16068 else if (openacc) 16069 error_at (OMP_CLAUSE_LOCATION (c), 16070 "%qD appears more than once in data clauses", t); 16071 else 16072 error_at (OMP_CLAUSE_LOCATION (c), 16073 "%qD appears more than once in map clauses", t); 16074 remove = true; 16075 } 16076 else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t))) 16077 { 16078 error_at (OMP_CLAUSE_LOCATION (c), 16079 "%qD appears more than once in data clauses", t); 16080 remove = true; 16081 } 16082 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t)) 16083 || bitmap_bit_p (&is_on_device_head, DECL_UID (t))) 16084 { 16085 if (openacc) 16086 error_at (OMP_CLAUSE_LOCATION (c), 16087 "%qD appears more than once in data clauses", t); 16088 else 16089 error_at (OMP_CLAUSE_LOCATION (c), 16090 "%qD appears both in data and map clauses", t); 16091 remove = true; 16092 } 16093 else if (!omp_access_chain_p (addr_tokens, 1)) 16094 { 16095 bitmap_set_bit (&map_head, DECL_UID (t)); 16096 if (t != OMP_CLAUSE_DECL (c) 16097 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF) 16098 bitmap_set_bit (&map_field_head, DECL_UID (t)); 16099 } 16100 16101 skip_decl_checks: 16102 /* If we call omp_expand_map_clause in handle_omp_array_sections, 16103 the containing loop (here) iterates through the new nodes 16104 created by that expansion. Avoid expanding those again (just 16105 by checking the node type). */ 16106 if (!remove 16107 && ort != C_ORT_DECLARE_SIMD 16108 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP 16109 || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER 16110 && (OMP_CLAUSE_MAP_KIND (c) 16111 != GOMP_MAP_FIRSTPRIVATE_REFERENCE) 16112 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER 16113 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH 16114 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH 16115 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH))) 16116 { 16117 grp_start_p = pc; 16118 grp_sentinel = OMP_CLAUSE_CHAIN (c); 16119 tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c), 16120 addr_tokens, ort); 16121 if (nc != error_mark_node) 16122 c = nc; 16123 } 16124 } 16125 break; 16126 16127 case OMP_CLAUSE_ENTER: 16128 case OMP_CLAUSE_LINK: 16129 t = OMP_CLAUSE_DECL (c); 16130 const char *cname; 16131 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)]; 16132 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER 16133 && OMP_CLAUSE_ENTER_TO (c)) 16134 cname = "to"; 16135 if (TREE_CODE (t) == FUNCTION_DECL 16136 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER) 16137 ; 16138 else if (!VAR_P (t)) 16139 { 16140 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER) 16141 error_at (OMP_CLAUSE_LOCATION (c), 16142 "%qE is neither a variable nor a function name in " 16143 "clause %qs", t, cname); 16144 else 16145 error_at (OMP_CLAUSE_LOCATION (c), 16146 "%qE is not a variable in clause %qs", t, cname); 16147 remove = true; 16148 } 16149 else if (DECL_THREAD_LOCAL_P (t)) 16150 { 16151 error_at (OMP_CLAUSE_LOCATION (c), 16152 "%qD is threadprivate variable in %qs clause", t, 16153 cname); 16154 remove = true; 16155 } 16156 else if (!omp_mappable_type (TREE_TYPE (t))) 16157 { 16158 error_at (OMP_CLAUSE_LOCATION (c), 16159 "%qD does not have a mappable type in %qs clause", t, 16160 cname); 16161 remove = true; 16162 } 16163 if (remove) 16164 break; 16165 if (bitmap_bit_p (&generic_head, DECL_UID (t))) 16166 { 16167 error_at (OMP_CLAUSE_LOCATION (c), 16168 "%qE appears more than once on the same " 16169 "%<declare target%> directive", t); 16170 remove = true; 16171 } 16172 else 16173 bitmap_set_bit (&generic_head, DECL_UID (t)); 16174 break; 16175 16176 case OMP_CLAUSE_UNIFORM: 16177 t = OMP_CLAUSE_DECL (c); 16178 if (TREE_CODE (t) != PARM_DECL) 16179 { 16180 if (DECL_P (t)) 16181 error_at (OMP_CLAUSE_LOCATION (c), 16182 "%qD is not an argument in %<uniform%> clause", t); 16183 else 16184 error_at (OMP_CLAUSE_LOCATION (c), 16185 "%qE is not an argument in %<uniform%> clause", t); 16186 remove = true; 16187 break; 16188 } 16189 /* map_head bitmap is used as uniform_head if declare_simd. */ 16190 bitmap_set_bit (&map_head, DECL_UID (t)); 16191 goto check_dup_generic; 16192 16193 case OMP_CLAUSE_IS_DEVICE_PTR: 16194 case OMP_CLAUSE_USE_DEVICE_PTR: 16195 t = OMP_CLAUSE_DECL (c); 16196 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR) 16197 bitmap_set_bit (&is_on_device_head, DECL_UID (t)); 16198 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE) 16199 { 16200 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR 16201 && !openacc) 16202 { 16203 error_at (OMP_CLAUSE_LOCATION (c), 16204 "%qs variable is not a pointer", 16205 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 16206 remove = true; 16207 } 16208 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE) 16209 { 16210 error_at (OMP_CLAUSE_LOCATION (c), 16211 "%qs variable is neither a pointer nor an array", 16212 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 16213 remove = true; 16214 } 16215 } 16216 goto check_dup_generic; 16217 16218 case OMP_CLAUSE_HAS_DEVICE_ADDR: 16219 t = OMP_CLAUSE_DECL (c); 16220 if (TREE_CODE (t) == OMP_ARRAY_SECTION) 16221 { 16222 if (handle_omp_array_sections (c, ort)) 16223 remove = true; 16224 else 16225 { 16226 t = OMP_CLAUSE_DECL (c); 16227 while (TREE_CODE (t) == ARRAY_REF) 16228 t = TREE_OPERAND (t, 0); 16229 } 16230 } 16231 bitmap_set_bit (&is_on_device_head, DECL_UID (t)); 16232 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL) 16233 c_mark_addressable (t); 16234 goto check_dup_generic_t; 16235 16236 case OMP_CLAUSE_USE_DEVICE_ADDR: 16237 t = OMP_CLAUSE_DECL (c); 16238 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL) 16239 c_mark_addressable (t); 16240 goto check_dup_generic; 16241 16242 case OMP_CLAUSE_NOWAIT: 16243 if (copyprivate_seen) 16244 { 16245 error_at (OMP_CLAUSE_LOCATION (c), 16246 "%<nowait%> clause must not be used together " 16247 "with %<copyprivate%>"); 16248 remove = true; 16249 break; 16250 } 16251 nowait_clause = pc; 16252 pc = &OMP_CLAUSE_CHAIN (c); 16253 continue; 16254 16255 case OMP_CLAUSE_ORDER: 16256 if (ordered_clause) 16257 { 16258 error_at (OMP_CLAUSE_LOCATION (c), 16259 "%<order%> clause must not be used together " 16260 "with %<ordered%>"); 16261 remove = true; 16262 break; 16263 } 16264 else if (order_clause) 16265 { 16266 /* Silently remove duplicates. */ 16267 remove = true; 16268 break; 16269 } 16270 order_clause = pc; 16271 pc = &OMP_CLAUSE_CHAIN (c); 16272 continue; 16273 16274 case OMP_CLAUSE_DETACH: 16275 t = OMP_CLAUSE_DECL (c); 16276 if (detach_seen) 16277 { 16278 error_at (OMP_CLAUSE_LOCATION (c), 16279 "too many %qs clauses on a task construct", 16280 "detach"); 16281 remove = true; 16282 break; 16283 } 16284 detach_seen = pc; 16285 pc = &OMP_CLAUSE_CHAIN (c); 16286 c_mark_addressable (t); 16287 continue; 16288 16289 case OMP_CLAUSE_IF: 16290 case OMP_CLAUSE_SELF: 16291 case OMP_CLAUSE_NUM_THREADS: 16292 case OMP_CLAUSE_NUM_TEAMS: 16293 case OMP_CLAUSE_THREAD_LIMIT: 16294 case OMP_CLAUSE_DEFAULT: 16295 case OMP_CLAUSE_UNTIED: 16296 case OMP_CLAUSE_COLLAPSE: 16297 case OMP_CLAUSE_FINAL: 16298 case OMP_CLAUSE_DEVICE: 16299 case OMP_CLAUSE_DIST_SCHEDULE: 16300 case OMP_CLAUSE_PARALLEL: 16301 case OMP_CLAUSE_FOR: 16302 case OMP_CLAUSE_SECTIONS: 16303 case OMP_CLAUSE_TASKGROUP: 16304 case OMP_CLAUSE_PROC_BIND: 16305 case OMP_CLAUSE_DEVICE_TYPE: 16306 case OMP_CLAUSE_PRIORITY: 16307 case OMP_CLAUSE_GRAINSIZE: 16308 case OMP_CLAUSE_NUM_TASKS: 16309 case OMP_CLAUSE_THREADS: 16310 case OMP_CLAUSE_SIMD: 16311 case OMP_CLAUSE_HINT: 16312 case OMP_CLAUSE_FILTER: 16313 case OMP_CLAUSE_DEFAULTMAP: 16314 case OMP_CLAUSE_BIND: 16315 case OMP_CLAUSE_NUM_GANGS: 16316 case OMP_CLAUSE_NUM_WORKERS: 16317 case OMP_CLAUSE_VECTOR_LENGTH: 16318 case OMP_CLAUSE_ASYNC: 16319 case OMP_CLAUSE_WAIT: 16320 case OMP_CLAUSE_AUTO: 16321 case OMP_CLAUSE_INDEPENDENT: 16322 case OMP_CLAUSE_SEQ: 16323 case OMP_CLAUSE_GANG: 16324 case OMP_CLAUSE_WORKER: 16325 case OMP_CLAUSE_VECTOR: 16326 case OMP_CLAUSE_TILE: 16327 case OMP_CLAUSE_IF_PRESENT: 16328 case OMP_CLAUSE_FINALIZE: 16329 case OMP_CLAUSE_NOHOST: 16330 case OMP_CLAUSE_INDIRECT: 16331 pc = &OMP_CLAUSE_CHAIN (c); 16332 continue; 16333 16334 case OMP_CLAUSE_MERGEABLE: 16335 mergeable_seen = true; 16336 pc = &OMP_CLAUSE_CHAIN (c); 16337 continue; 16338 16339 case OMP_CLAUSE_NOGROUP: 16340 nogroup_seen = pc; 16341 pc = &OMP_CLAUSE_CHAIN (c); 16342 continue; 16343 16344 case OMP_CLAUSE_SCHEDULE: 16345 schedule_clause = c; 16346 pc = &OMP_CLAUSE_CHAIN (c); 16347 continue; 16348 16349 case OMP_CLAUSE_ORDERED: 16350 ordered_clause = c; 16351 if (order_clause) 16352 { 16353 error_at (OMP_CLAUSE_LOCATION (*order_clause), 16354 "%<order%> clause must not be used together " 16355 "with %<ordered%>"); 16356 *order_clause = OMP_CLAUSE_CHAIN (*order_clause); 16357 order_clause = NULL; 16358 } 16359 pc = &OMP_CLAUSE_CHAIN (c); 16360 continue; 16361 16362 case OMP_CLAUSE_SAFELEN: 16363 safelen = c; 16364 pc = &OMP_CLAUSE_CHAIN (c); 16365 continue; 16366 case OMP_CLAUSE_SIMDLEN: 16367 simdlen = c; 16368 pc = &OMP_CLAUSE_CHAIN (c); 16369 continue; 16370 16371 case OMP_CLAUSE_INBRANCH: 16372 case OMP_CLAUSE_NOTINBRANCH: 16373 if (branch_seen) 16374 { 16375 error_at (OMP_CLAUSE_LOCATION (c), 16376 "%<inbranch%> clause is incompatible with " 16377 "%<notinbranch%>"); 16378 remove = true; 16379 break; 16380 } 16381 branch_seen = true; 16382 pc = &OMP_CLAUSE_CHAIN (c); 16383 continue; 16384 16385 case OMP_CLAUSE_INCLUSIVE: 16386 case OMP_CLAUSE_EXCLUSIVE: 16387 need_complete = true; 16388 need_implicitly_determined = true; 16389 t = OMP_CLAUSE_DECL (c); 16390 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) 16391 { 16392 error_at (OMP_CLAUSE_LOCATION (c), 16393 "%qE is not a variable in clause %qs", t, 16394 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 16395 remove = true; 16396 } 16397 break; 16398 16399 default: 16400 gcc_unreachable (); 16401 } 16402 16403 if (!remove) 16404 { 16405 t = OMP_CLAUSE_DECL (c); 16406 16407 if (need_complete) 16408 { 16409 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t); 16410 if (t == error_mark_node) 16411 remove = true; 16412 } 16413 16414 if (need_implicitly_determined) 16415 { 16416 const char *share_name = NULL; 16417 16418 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t)) 16419 share_name = "threadprivate"; 16420 else switch (c_omp_predetermined_sharing (t)) 16421 { 16422 case OMP_CLAUSE_DEFAULT_UNSPECIFIED: 16423 break; 16424 case OMP_CLAUSE_DEFAULT_SHARED: 16425 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED 16426 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE) 16427 && c_omp_predefined_variable (t)) 16428 /* The __func__ variable and similar function-local 16429 predefined variables may be listed in a shared or 16430 firstprivate clause. */ 16431 break; 16432 share_name = "shared"; 16433 break; 16434 case OMP_CLAUSE_DEFAULT_PRIVATE: 16435 share_name = "private"; 16436 break; 16437 default: 16438 gcc_unreachable (); 16439 } 16440 if (share_name) 16441 { 16442 error_at (OMP_CLAUSE_LOCATION (c), 16443 "%qE is predetermined %qs for %qs", 16444 t, share_name, 16445 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 16446 remove = true; 16447 } 16448 else if (TREE_READONLY (t) 16449 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED 16450 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE) 16451 { 16452 error_at (OMP_CLAUSE_LOCATION (c), 16453 "%<const%> qualified %qE may appear only in " 16454 "%<shared%> or %<firstprivate%> clauses", t); 16455 remove = true; 16456 } 16457 } 16458 } 16459 16460 if (remove) 16461 { 16462 if (grp_start_p) 16463 { 16464 /* If we found a clause to remove, we want to remove the whole 16465 expanded group, otherwise gimplify 16466 (omp_resolve_clause_dependencies) can get confused. */ 16467 *grp_start_p = grp_sentinel; 16468 pc = grp_start_p; 16469 grp_start_p = NULL; 16470 } 16471 else 16472 *pc = OMP_CLAUSE_CHAIN (c); 16473 } 16474 else 16475 pc = &OMP_CLAUSE_CHAIN (c); 16476 } 16477 16478 if (simdlen 16479 && safelen 16480 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen), 16481 OMP_CLAUSE_SIMDLEN_EXPR (simdlen))) 16482 { 16483 error_at (OMP_CLAUSE_LOCATION (simdlen), 16484 "%<simdlen%> clause value is bigger than " 16485 "%<safelen%> clause value"); 16486 OMP_CLAUSE_SIMDLEN_EXPR (simdlen) 16487 = OMP_CLAUSE_SAFELEN_EXPR (safelen); 16488 } 16489 16490 if (ordered_clause 16491 && schedule_clause 16492 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause) 16493 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)) 16494 { 16495 error_at (OMP_CLAUSE_LOCATION (schedule_clause), 16496 "%<nonmonotonic%> schedule modifier specified together " 16497 "with %<ordered%> clause"); 16498 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause) 16499 = (enum omp_clause_schedule_kind) 16500 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause) 16501 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC); 16502 } 16503 16504 if (reduction_seen < 0 && ordered_clause) 16505 { 16506 error_at (OMP_CLAUSE_LOCATION (ordered_clause), 16507 "%qs clause specified together with %<inscan%> " 16508 "%<reduction%> clause", "ordered"); 16509 reduction_seen = -2; 16510 } 16511 16512 if (reduction_seen < 0 && schedule_clause) 16513 { 16514 error_at (OMP_CLAUSE_LOCATION (schedule_clause), 16515 "%qs clause specified together with %<inscan%> " 16516 "%<reduction%> clause", "schedule"); 16517 reduction_seen = -2; 16518 } 16519 16520 if (linear_variable_step_check 16521 || reduction_seen == -2 16522 || allocate_seen 16523 || target_in_reduction_seen) 16524 for (pc = &clauses, c = clauses; c ; c = *pc) 16525 { 16526 bool remove = false; 16527 if (allocate_seen) 16528 switch (OMP_CLAUSE_CODE (c)) 16529 { 16530 case OMP_CLAUSE_REDUCTION: 16531 case OMP_CLAUSE_IN_REDUCTION: 16532 case OMP_CLAUSE_TASK_REDUCTION: 16533 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF) 16534 { 16535 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0); 16536 if (TREE_CODE (t) == POINTER_PLUS_EXPR) 16537 t = TREE_OPERAND (t, 0); 16538 if (TREE_CODE (t) == ADDR_EXPR 16539 || INDIRECT_REF_P (t)) 16540 t = TREE_OPERAND (t, 0); 16541 if (DECL_P (t)) 16542 bitmap_clear_bit (&aligned_head, DECL_UID (t)); 16543 break; 16544 } 16545 /* FALLTHRU */ 16546 case OMP_CLAUSE_PRIVATE: 16547 case OMP_CLAUSE_FIRSTPRIVATE: 16548 case OMP_CLAUSE_LASTPRIVATE: 16549 case OMP_CLAUSE_LINEAR: 16550 if (DECL_P (OMP_CLAUSE_DECL (c))) 16551 bitmap_clear_bit (&aligned_head, 16552 DECL_UID (OMP_CLAUSE_DECL (c))); 16553 break; 16554 default: 16555 break; 16556 } 16557 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR 16558 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) 16559 && !bitmap_bit_p (&map_head, 16560 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c)))) 16561 { 16562 error_at (OMP_CLAUSE_LOCATION (c), 16563 "%<linear%> clause step is a parameter %qD not " 16564 "specified in %<uniform%> clause", 16565 OMP_CLAUSE_LINEAR_STEP (c)); 16566 remove = true; 16567 } 16568 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION 16569 && reduction_seen == -2) 16570 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0; 16571 if (target_in_reduction_seen 16572 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP) 16573 { 16574 tree t = OMP_CLAUSE_DECL (c); 16575 while (handled_component_p (t) 16576 || INDIRECT_REF_P (t) 16577 || TREE_CODE (t) == ADDR_EXPR 16578 || TREE_CODE (t) == MEM_REF 16579 || TREE_CODE (t) == NON_LVALUE_EXPR) 16580 t = TREE_OPERAND (t, 0); 16581 if (DECL_P (t) 16582 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t))) 16583 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1; 16584 } 16585 16586 if (remove) 16587 *pc = OMP_CLAUSE_CHAIN (c); 16588 else 16589 pc = &OMP_CLAUSE_CHAIN (c); 16590 } 16591 16592 if (allocate_seen) 16593 for (pc = &clauses, c = clauses; c ; c = *pc) 16594 { 16595 bool remove = false; 16596 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE 16597 && !OMP_CLAUSE_ALLOCATE_COMBINED (c) 16598 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c)))) 16599 { 16600 error_at (OMP_CLAUSE_LOCATION (c), 16601 "%qD specified in %<allocate%> clause but not in " 16602 "an explicit privatization clause", OMP_CLAUSE_DECL (c)); 16603 remove = true; 16604 } 16605 if (remove) 16606 *pc = OMP_CLAUSE_CHAIN (c); 16607 else 16608 pc = &OMP_CLAUSE_CHAIN (c); 16609 } 16610 16611 if (nogroup_seen && reduction_seen) 16612 { 16613 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen), 16614 "%<nogroup%> clause must not be used together with " 16615 "%<reduction%> clause"); 16616 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen); 16617 } 16618 16619 if (detach_seen) 16620 { 16621 if (mergeable_seen) 16622 { 16623 error_at (OMP_CLAUSE_LOCATION (*detach_seen), 16624 "%<detach%> clause must not be used together with " 16625 "%<mergeable%> clause"); 16626 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen); 16627 } 16628 else 16629 { 16630 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen); 16631 16632 for (pc = &clauses, c = clauses; c ; c = *pc) 16633 { 16634 bool remove = false; 16635 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED 16636 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE 16637 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE 16638 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE) 16639 && OMP_CLAUSE_DECL (c) == detach_decl) 16640 { 16641 error_at (OMP_CLAUSE_LOCATION (c), 16642 "the event handle of a %<detach%> clause " 16643 "should not be in a data-sharing clause"); 16644 remove = true; 16645 } 16646 if (remove) 16647 *pc = OMP_CLAUSE_CHAIN (c); 16648 else 16649 pc = &OMP_CLAUSE_CHAIN (c); 16650 } 16651 } 16652 } 16653 16654 bitmap_obstack_release (NULL); 16655 return clauses; 16656 } 16657 16658 /* Return code to initialize DST with a copy constructor from SRC. 16659 C doesn't have copy constructors nor assignment operators, only for 16660 _Atomic vars we need to perform __atomic_load from src into a temporary 16661 followed by __atomic_store of the temporary to dst. */ 16662 16663 tree 16664 c_omp_clause_copy_ctor (tree clause, tree dst, tree src) 16665 { 16666 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src)) 16667 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src); 16668 16669 location_t loc = OMP_CLAUSE_LOCATION (clause); 16670 tree type = TREE_TYPE (dst); 16671 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED); 16672 tree tmp = create_tmp_var (nonatomic_type); 16673 tree tmp_addr = build_fold_addr_expr (tmp); 16674 TREE_ADDRESSABLE (tmp) = 1; 16675 suppress_warning (tmp); 16676 tree src_addr = build_fold_addr_expr (src); 16677 tree dst_addr = build_fold_addr_expr (dst); 16678 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST); 16679 vec<tree, va_gc> *params; 16680 /* Expansion of a generic atomic load may require an addition 16681 element, so allocate enough to prevent a resize. */ 16682 vec_alloc (params, 4); 16683 16684 /* Build __atomic_load (&src, &tmp, SEQ_CST); */ 16685 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD); 16686 params->quick_push (src_addr); 16687 params->quick_push (tmp_addr); 16688 params->quick_push (seq_cst); 16689 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); 16690 16691 vec_alloc (params, 4); 16692 16693 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */ 16694 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE); 16695 params->quick_push (dst_addr); 16696 params->quick_push (tmp_addr); 16697 params->quick_push (seq_cst); 16698 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); 16699 return build2 (COMPOUND_EXPR, void_type_node, load, store); 16700 } 16701 16702 /* Create a transaction node. */ 16703 16704 tree 16705 c_finish_transaction (location_t loc, tree block, int flags) 16706 { 16707 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block); 16708 if (flags & TM_STMT_ATTR_OUTER) 16709 TRANSACTION_EXPR_OUTER (stmt) = 1; 16710 if (flags & TM_STMT_ATTR_RELAXED) 16711 TRANSACTION_EXPR_RELAXED (stmt) = 1; 16712 return add_stmt (stmt); 16713 } 16714 16715 /* Make a variant type in the proper way for C/C++, propagating qualifiers 16716 down to the element type of an array. If ORIG_QUAL_TYPE is not 16717 NULL, then it should be used as the qualified type 16718 ORIG_QUAL_INDIRECT levels down in array type derivation (to 16719 preserve information about the typedef name from which an array 16720 type was derived). */ 16721 16722 tree 16723 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type, 16724 size_t orig_qual_indirect) 16725 { 16726 if (type == error_mark_node) 16727 return type; 16728 16729 if (TREE_CODE (type) == ARRAY_TYPE) 16730 { 16731 tree t; 16732 tree element_type = c_build_qualified_type (TREE_TYPE (type), 16733 type_quals, orig_qual_type, 16734 orig_qual_indirect - 1); 16735 16736 /* See if we already have an identically qualified type. */ 16737 if (orig_qual_type && orig_qual_indirect == 0) 16738 t = orig_qual_type; 16739 else 16740 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) 16741 { 16742 if (TYPE_QUALS (strip_array_types (t)) == type_quals 16743 && TYPE_NAME (t) == TYPE_NAME (type) 16744 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type) 16745 && attribute_list_equal (TYPE_ATTRIBUTES (t), 16746 TYPE_ATTRIBUTES (type))) 16747 break; 16748 } 16749 if (!t) 16750 { 16751 tree domain = TYPE_DOMAIN (type); 16752 16753 t = build_variant_type_copy (type); 16754 TREE_TYPE (t) = element_type; 16755 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (element_type); 16756 16757 if (TYPE_STRUCTURAL_EQUALITY_P (element_type) 16758 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain))) 16759 SET_TYPE_STRUCTURAL_EQUALITY (t); 16760 else if (TYPE_CANONICAL (element_type) != element_type 16761 || (domain && TYPE_CANONICAL (domain) != domain)) 16762 { 16763 tree unqualified_canon 16764 = c_build_array_type (TYPE_CANONICAL (element_type), 16765 domain ? TYPE_CANONICAL (domain) 16766 : NULL_TREE); 16767 if (TYPE_REVERSE_STORAGE_ORDER (type)) 16768 { 16769 unqualified_canon 16770 = build_distinct_type_copy (unqualified_canon); 16771 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1; 16772 } 16773 TYPE_CANONICAL (t) 16774 = c_build_qualified_type (unqualified_canon, type_quals); 16775 } 16776 else 16777 TYPE_CANONICAL (t) = t; 16778 } 16779 return t; 16780 } 16781 16782 /* A restrict-qualified pointer type must be a pointer to object or 16783 incomplete type. Note that the use of POINTER_TYPE_P also allows 16784 REFERENCE_TYPEs, which is appropriate for C++. */ 16785 if ((type_quals & TYPE_QUAL_RESTRICT) 16786 && (!POINTER_TYPE_P (type) 16787 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) 16788 { 16789 error ("invalid use of %<restrict%>"); 16790 type_quals &= ~TYPE_QUAL_RESTRICT; 16791 } 16792 16793 tree var_type = (orig_qual_type && orig_qual_indirect == 0 16794 ? orig_qual_type 16795 : build_qualified_type (type, type_quals)); 16796 16797 gcc_checking_assert (C_TYPE_VARIABLE_SIZE (var_type) 16798 == C_TYPE_VARIABLE_SIZE (type)); 16799 gcc_checking_assert (C_TYPE_VARIABLY_MODIFIED (var_type) 16800 == C_TYPE_VARIABLY_MODIFIED (type)); 16801 16802 /* A variant type does not inherit the list of incomplete vars from the 16803 type main variant. */ 16804 if ((RECORD_OR_UNION_TYPE_P (var_type) 16805 || TREE_CODE (var_type) == ENUMERAL_TYPE) 16806 && TYPE_MAIN_VARIANT (var_type) != var_type) 16807 C_TYPE_INCOMPLETE_VARS (var_type) = 0; 16808 return var_type; 16809 } 16810 16811 /* Build a VA_ARG_EXPR for the C parser. */ 16812 16813 tree 16814 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type) 16815 { 16816 if (error_operand_p (type)) 16817 return error_mark_node; 16818 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage 16819 order because it takes the address of the expression. */ 16820 else if (handled_component_p (expr) 16821 && reverse_storage_order_for_component_p (expr)) 16822 { 16823 error_at (loc1, "cannot use %<va_arg%> with reverse storage order"); 16824 return error_mark_node; 16825 } 16826 else if (!COMPLETE_TYPE_P (type)) 16827 { 16828 error_at (loc2, "second argument to %<va_arg%> is of incomplete " 16829 "type %qT", type); 16830 return error_mark_node; 16831 } 16832 else if (TREE_CODE (type) == FUNCTION_TYPE) 16833 { 16834 error_at (loc2, "second argument to %<va_arg%> is a function type %qT", 16835 type); 16836 return error_mark_node; 16837 } 16838 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE) 16839 warning_at (loc2, OPT_Wc___compat, 16840 "C++ requires promoted type, not enum type, in %<va_arg%>"); 16841 return build_va_arg (loc2, expr, type); 16842 } 16843 16844 /* Return truthvalue of whether T1 is the same tree structure as T2. 16845 Return 1 if they are the same. Return false if they are different. */ 16846 16847 bool 16848 c_tree_equal (tree t1, tree t2) 16849 { 16850 enum tree_code code1, code2; 16851 16852 if (t1 == t2) 16853 return true; 16854 if (!t1 || !t2) 16855 return false; 16856 16857 for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR; 16858 code1 = TREE_CODE (t1)) 16859 t1 = TREE_OPERAND (t1, 0); 16860 for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR; 16861 code2 = TREE_CODE (t2)) 16862 t2 = TREE_OPERAND (t2, 0); 16863 16864 /* They might have become equal now. */ 16865 if (t1 == t2) 16866 return true; 16867 16868 if (code1 != code2) 16869 return false; 16870 16871 if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2))) 16872 return false; 16873 16874 switch (code1) 16875 { 16876 case INTEGER_CST: 16877 return wi::to_wide (t1) == wi::to_wide (t2); 16878 16879 case REAL_CST: 16880 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2)); 16881 16882 case STRING_CST: 16883 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2) 16884 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2), 16885 TREE_STRING_LENGTH (t1)); 16886 16887 case FIXED_CST: 16888 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), 16889 TREE_FIXED_CST (t2)); 16890 16891 case COMPLEX_CST: 16892 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2)) 16893 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2)); 16894 16895 case VECTOR_CST: 16896 return operand_equal_p (t1, t2, OEP_ONLY_CONST); 16897 16898 case CONSTRUCTOR: 16899 /* We need to do this when determining whether or not two 16900 non-type pointer to member function template arguments 16901 are the same. */ 16902 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)) 16903 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2)) 16904 return false; 16905 { 16906 tree field, value; 16907 unsigned int i; 16908 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value) 16909 { 16910 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i); 16911 if (!c_tree_equal (field, elt2->index) 16912 || !c_tree_equal (value, elt2->value)) 16913 return false; 16914 } 16915 } 16916 return true; 16917 16918 case TREE_LIST: 16919 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))) 16920 return false; 16921 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2))) 16922 return false; 16923 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2)); 16924 16925 case SAVE_EXPR: 16926 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); 16927 16928 case CALL_EXPR: 16929 { 16930 tree arg1, arg2; 16931 call_expr_arg_iterator iter1, iter2; 16932 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2))) 16933 return false; 16934 for (arg1 = first_call_expr_arg (t1, &iter1), 16935 arg2 = first_call_expr_arg (t2, &iter2); 16936 arg1 && arg2; 16937 arg1 = next_call_expr_arg (&iter1), 16938 arg2 = next_call_expr_arg (&iter2)) 16939 if (!c_tree_equal (arg1, arg2)) 16940 return false; 16941 if (arg1 || arg2) 16942 return false; 16943 return true; 16944 } 16945 16946 case TARGET_EXPR: 16947 { 16948 tree o1 = TREE_OPERAND (t1, 0); 16949 tree o2 = TREE_OPERAND (t2, 0); 16950 16951 /* Special case: if either target is an unallocated VAR_DECL, 16952 it means that it's going to be unified with whatever the 16953 TARGET_EXPR is really supposed to initialize, so treat it 16954 as being equivalent to anything. */ 16955 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE 16956 && !DECL_RTL_SET_P (o1)) 16957 /*Nop*/; 16958 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE 16959 && !DECL_RTL_SET_P (o2)) 16960 /*Nop*/; 16961 else if (!c_tree_equal (o1, o2)) 16962 return false; 16963 16964 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)); 16965 } 16966 16967 case COMPONENT_REF: 16968 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1)) 16969 return false; 16970 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); 16971 16972 case PARM_DECL: 16973 case VAR_DECL: 16974 case CONST_DECL: 16975 case FIELD_DECL: 16976 case FUNCTION_DECL: 16977 case IDENTIFIER_NODE: 16978 case SSA_NAME: 16979 return false; 16980 16981 case TREE_VEC: 16982 { 16983 unsigned ix; 16984 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) 16985 return false; 16986 for (ix = TREE_VEC_LENGTH (t1); ix--;) 16987 if (!c_tree_equal (TREE_VEC_ELT (t1, ix), 16988 TREE_VEC_ELT (t2, ix))) 16989 return false; 16990 return true; 16991 } 16992 16993 CASE_CONVERT: 16994 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))) 16995 return false; 16996 break; 16997 16998 default: 16999 break; 17000 } 17001 17002 switch (TREE_CODE_CLASS (code1)) 17003 { 17004 case tcc_unary: 17005 case tcc_binary: 17006 case tcc_comparison: 17007 case tcc_expression: 17008 case tcc_vl_exp: 17009 case tcc_reference: 17010 case tcc_statement: 17011 { 17012 int i, n = TREE_OPERAND_LENGTH (t1); 17013 17014 switch (code1) 17015 { 17016 case PREINCREMENT_EXPR: 17017 case PREDECREMENT_EXPR: 17018 case POSTINCREMENT_EXPR: 17019 case POSTDECREMENT_EXPR: 17020 n = 1; 17021 break; 17022 case ARRAY_REF: 17023 n = 2; 17024 break; 17025 default: 17026 break; 17027 } 17028 17029 if (TREE_CODE_CLASS (code1) == tcc_vl_exp 17030 && n != TREE_OPERAND_LENGTH (t2)) 17031 return false; 17032 17033 for (i = 0; i < n; ++i) 17034 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i))) 17035 return false; 17036 17037 return true; 17038 } 17039 17040 case tcc_type: 17041 return comptypes (t1, t2); 17042 default: 17043 gcc_unreachable (); 17044 } 17045 } 17046 17047 /* Returns true when the function declaration FNDECL is implicit, 17048 introduced as a result of a call to an otherwise undeclared 17049 function, and false otherwise. */ 17050 17051 bool 17052 c_decl_implicit (const_tree fndecl) 17053 { 17054 return C_DECL_IMPLICIT (fndecl); 17055 } 17056