1/* Build expressions with type checking for C compiler. 2 Copyright (C) 1987-2015 Free Software Foundation, Inc. 3 4This file is part of GCC. 5 6GCC is free software; you can redistribute it and/or modify it under 7the terms of the GNU General Public License as published by the Free 8Software Foundation; either version 3, or (at your option) any later 9version. 10 11GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12WARRANTY; without even the implied warranty of MERCHANTABILITY or 13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14for more details. 15 16You should have received a copy of the GNU General Public License 17along 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 "tm.h" 30#include "hash-set.h" 31#include "vec.h" 32#include "symtab.h" 33#include "input.h" 34#include "alias.h" 35#include "double-int.h" 36#include "machmode.h" 37#include "inchash.h" 38#include "real.h" 39#include "fixed-value.h" 40#include "tree.h" 41#include "fold-const.h" 42#include "stor-layout.h" 43#include "trans-mem.h" 44#include "varasm.h" 45#include "stmt.h" 46#include "langhooks.h" 47#include "c-tree.h" 48#include "c-lang.h" 49#include "flags.h" 50#include "intl.h" 51#include "target.h" 52#include "tree-iterator.h" 53#include "bitmap.h" 54#include "predict.h" 55#include "vec.h" 56#include "hashtab.h" 57#include "hash-set.h" 58#include "machmode.h" 59#include "hard-reg-set.h" 60#include "input.h" 61#include "function.h" 62#include "gimple-expr.h" 63#include "gimplify.h" 64#include "tree-inline.h" 65#include "omp-low.h" 66#include "c-family/c-objc.h" 67#include "c-family/c-common.h" 68#include "c-family/c-ubsan.h" 69#include "cilk.h" 70#include "wide-int.h" 71#include "gomp-constants.h" 72 73/* Possible cases of implicit bad conversions. Used to select 74 diagnostic messages in convert_for_assignment. */ 75enum impl_conv { 76 ic_argpass, 77 ic_assign, 78 ic_init, 79 ic_return 80}; 81 82/* The level of nesting inside "__alignof__". */ 83int in_alignof; 84 85/* The level of nesting inside "sizeof". */ 86int in_sizeof; 87 88/* The level of nesting inside "typeof". */ 89int in_typeof; 90 91/* The argument of last parsed sizeof expression, only to be tested 92 if expr.original_code == SIZEOF_EXPR. */ 93tree c_last_sizeof_arg; 94 95/* Nonzero if we might need to print a "missing braces around 96 initializer" message within this initializer. */ 97static int found_missing_braces; 98 99static int require_constant_value; 100static int require_constant_elements; 101 102static bool null_pointer_constant_p (const_tree); 103static tree qualify_type (tree, tree); 104static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *, 105 bool *); 106static int comp_target_types (location_t, tree, tree); 107static int function_types_compatible_p (const_tree, const_tree, bool *, 108 bool *); 109static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *); 110static tree lookup_field (tree, tree); 111static int convert_arguments (location_t, vec<location_t>, tree, 112 vec<tree, va_gc> *, vec<tree, va_gc> *, tree, 113 tree); 114static tree pointer_diff (location_t, tree, tree); 115static tree convert_for_assignment (location_t, location_t, tree, tree, tree, 116 enum impl_conv, bool, tree, tree, int); 117static tree valid_compound_expr_initializer (tree, tree); 118static void push_string (const char *); 119static void push_member_name (tree); 120static int spelling_length (void); 121static char *print_spelling (char *); 122static void warning_init (location_t, int, const char *); 123static tree digest_init (location_t, tree, tree, tree, bool, bool, int); 124static void output_init_element (location_t, tree, tree, bool, tree, tree, int, 125 bool, struct obstack *); 126static void output_pending_init_elements (int, struct obstack *); 127static int set_designator (location_t, int, struct obstack *); 128static void push_range_stack (tree, struct obstack *); 129static void add_pending_init (location_t, tree, tree, tree, bool, 130 struct obstack *); 131static void set_nonincremental_init (struct obstack *); 132static void set_nonincremental_init_from_string (tree, struct obstack *); 133static tree find_init_member (tree, struct obstack *); 134static void readonly_warning (tree, enum lvalue_use); 135static int lvalue_or_else (location_t, const_tree, enum lvalue_use); 136static void record_maybe_used_decl (tree); 137static int comptypes_internal (const_tree, const_tree, bool *, bool *); 138 139/* Return true if EXP is a null pointer constant, false otherwise. */ 140 141static bool 142null_pointer_constant_p (const_tree expr) 143{ 144 /* This should really operate on c_expr structures, but they aren't 145 yet available everywhere required. */ 146 tree type = TREE_TYPE (expr); 147 return (TREE_CODE (expr) == INTEGER_CST 148 && !TREE_OVERFLOW (expr) 149 && integer_zerop (expr) 150 && (INTEGRAL_TYPE_P (type) 151 || (TREE_CODE (type) == POINTER_TYPE 152 && VOID_TYPE_P (TREE_TYPE (type)) 153 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED))); 154} 155 156/* EXPR may appear in an unevaluated part of an integer constant 157 expression, but not in an evaluated part. Wrap it in a 158 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an 159 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */ 160 161static tree 162note_integer_operands (tree expr) 163{ 164 tree ret; 165 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op) 166 { 167 ret = copy_node (expr); 168 TREE_OVERFLOW (ret) = 1; 169 } 170 else 171 { 172 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr); 173 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1; 174 } 175 return ret; 176} 177 178/* Having checked whether EXPR may appear in an unevaluated part of an 179 integer constant expression and found that it may, remove any 180 C_MAYBE_CONST_EXPR noting this fact and return the resulting 181 expression. */ 182 183static inline tree 184remove_c_maybe_const_expr (tree expr) 185{ 186 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) 187 return C_MAYBE_CONST_EXPR_EXPR (expr); 188 else 189 return expr; 190} 191 192/* This is a cache to hold if two types are compatible or not. */ 193 194struct tagged_tu_seen_cache { 195 const struct tagged_tu_seen_cache * next; 196 const_tree t1; 197 const_tree t2; 198 /* The return value of tagged_types_tu_compatible_p if we had seen 199 these two types already. */ 200 int val; 201}; 202 203static const struct tagged_tu_seen_cache * tagged_tu_seen_base; 204static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *); 205 206/* Do `exp = require_complete_type (exp);' to make sure exp 207 does not have an incomplete type. (That includes void types.) */ 208 209tree 210require_complete_type (tree value) 211{ 212 tree type = TREE_TYPE (value); 213 214 if (error_operand_p (value)) 215 return error_mark_node; 216 217 /* First, detect a valid value with a complete type. */ 218 if (COMPLETE_TYPE_P (type)) 219 return value; 220 221 c_incomplete_type_error (value, type); 222 return error_mark_node; 223} 224 225/* Print an error message for invalid use of an incomplete type. 226 VALUE is the expression that was used (or 0 if that isn't known) 227 and TYPE is the type that was invalid. */ 228 229void 230c_incomplete_type_error (const_tree value, const_tree type) 231{ 232 const char *type_code_string; 233 234 /* Avoid duplicate error message. */ 235 if (TREE_CODE (type) == ERROR_MARK) 236 return; 237 238 if (value != 0 && (TREE_CODE (value) == VAR_DECL 239 || TREE_CODE (value) == PARM_DECL)) 240 error ("%qD has an incomplete type", value); 241 else 242 { 243 retry: 244 /* We must print an error message. Be clever about what it says. */ 245 246 switch (TREE_CODE (type)) 247 { 248 case RECORD_TYPE: 249 type_code_string = "struct"; 250 break; 251 252 case UNION_TYPE: 253 type_code_string = "union"; 254 break; 255 256 case ENUMERAL_TYPE: 257 type_code_string = "enum"; 258 break; 259 260 case VOID_TYPE: 261 error ("invalid use of void expression"); 262 return; 263 264 case ARRAY_TYPE: 265 if (TYPE_DOMAIN (type)) 266 { 267 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL) 268 { 269 error ("invalid use of flexible array member"); 270 return; 271 } 272 type = TREE_TYPE (type); 273 goto retry; 274 } 275 error ("invalid use of array with unspecified bounds"); 276 return; 277 278 default: 279 gcc_unreachable (); 280 } 281 282 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) 283 error ("invalid use of undefined type %<%s %E%>", 284 type_code_string, TYPE_NAME (type)); 285 else 286 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ 287 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type)); 288 } 289} 290 291/* Given a type, apply default promotions wrt unnamed function 292 arguments and return the new type. */ 293 294tree 295c_type_promotes_to (tree type) 296{ 297 tree ret = NULL_TREE; 298 299 if (TYPE_MAIN_VARIANT (type) == float_type_node) 300 ret = double_type_node; 301 else if (c_promoting_integer_type_p (type)) 302 { 303 /* Preserve unsignedness if not really getting any wider. */ 304 if (TYPE_UNSIGNED (type) 305 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) 306 ret = unsigned_type_node; 307 else 308 ret = integer_type_node; 309 } 310 311 if (ret != NULL_TREE) 312 return (TYPE_ATOMIC (type) 313 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC) 314 : ret); 315 316 return type; 317} 318 319/* Return true if between two named address spaces, whether there is a superset 320 named address space that encompasses both address spaces. If there is a 321 superset, return which address space is the superset. */ 322 323static bool 324addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common) 325{ 326 if (as1 == as2) 327 { 328 *common = as1; 329 return true; 330 } 331 else if (targetm.addr_space.subset_p (as1, as2)) 332 { 333 *common = as2; 334 return true; 335 } 336 else if (targetm.addr_space.subset_p (as2, as1)) 337 { 338 *common = as1; 339 return true; 340 } 341 else 342 return false; 343} 344 345/* Return a variant of TYPE which has all the type qualifiers of LIKE 346 as well as those of TYPE. */ 347 348static tree 349qualify_type (tree type, tree like) 350{ 351 addr_space_t as_type = TYPE_ADDR_SPACE (type); 352 addr_space_t as_like = TYPE_ADDR_SPACE (like); 353 addr_space_t as_common; 354 355 /* If the two named address spaces are different, determine the common 356 superset address space. If there isn't one, raise an error. */ 357 if (!addr_space_superset (as_type, as_like, &as_common)) 358 { 359 as_common = as_type; 360 error ("%qT and %qT are in disjoint named address spaces", 361 type, like); 362 } 363 364 return c_build_qualified_type (type, 365 TYPE_QUALS_NO_ADDR_SPACE (type) 366 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like) 367 | ENCODE_QUAL_ADDR_SPACE (as_common)); 368} 369 370/* Return true iff the given tree T is a variable length array. */ 371 372bool 373c_vla_type_p (const_tree t) 374{ 375 if (TREE_CODE (t) == ARRAY_TYPE 376 && C_TYPE_VARIABLE_SIZE (t)) 377 return true; 378 return false; 379} 380 381/* Return the composite type of two compatible types. 382 383 We assume that comptypes has already been done and returned 384 nonzero; if that isn't so, this may crash. In particular, we 385 assume that qualifiers match. */ 386 387tree 388composite_type (tree t1, tree t2) 389{ 390 enum tree_code code1; 391 enum tree_code code2; 392 tree attributes; 393 394 /* Save time if the two types are the same. */ 395 396 if (t1 == t2) return t1; 397 398 /* If one type is nonsense, use the other. */ 399 if (t1 == error_mark_node) 400 return t2; 401 if (t2 == error_mark_node) 402 return t1; 403 404 code1 = TREE_CODE (t1); 405 code2 = TREE_CODE (t2); 406 407 /* Merge the attributes. */ 408 attributes = targetm.merge_type_attributes (t1, t2); 409 410 /* If one is an enumerated type and the other is the compatible 411 integer type, the composite type might be either of the two 412 (DR#013 question 3). For consistency, use the enumerated type as 413 the composite type. */ 414 415 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE) 416 return t1; 417 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE) 418 return t2; 419 420 gcc_assert (code1 == code2); 421 422 switch (code1) 423 { 424 case POINTER_TYPE: 425 /* For two pointers, do this recursively on the target type. */ 426 { 427 tree pointed_to_1 = TREE_TYPE (t1); 428 tree pointed_to_2 = TREE_TYPE (t2); 429 tree target = composite_type (pointed_to_1, pointed_to_2); 430 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false); 431 t1 = build_type_attribute_variant (t1, attributes); 432 return qualify_type (t1, t2); 433 } 434 435 case ARRAY_TYPE: 436 { 437 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); 438 int quals; 439 tree unqual_elt; 440 tree d1 = TYPE_DOMAIN (t1); 441 tree d2 = TYPE_DOMAIN (t2); 442 bool d1_variable, d2_variable; 443 bool d1_zero, d2_zero; 444 bool t1_complete, t2_complete; 445 446 /* We should not have any type quals on arrays at all. */ 447 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1) 448 && !TYPE_QUALS_NO_ADDR_SPACE (t2)); 449 450 t1_complete = COMPLETE_TYPE_P (t1); 451 t2_complete = COMPLETE_TYPE_P (t2); 452 453 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1); 454 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2); 455 456 d1_variable = (!d1_zero 457 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST 458 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); 459 d2_variable = (!d2_zero 460 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST 461 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); 462 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); 463 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); 464 465 /* Save space: see if the result is identical to one of the args. */ 466 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1) 467 && (d2_variable || d2_zero || !d1_variable)) 468 return build_type_attribute_variant (t1, attributes); 469 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2) 470 && (d1_variable || d1_zero || !d2_variable)) 471 return build_type_attribute_variant (t2, attributes); 472 473 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) 474 return build_type_attribute_variant (t1, attributes); 475 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) 476 return build_type_attribute_variant (t2, attributes); 477 478 /* Merge the element types, and have a size if either arg has 479 one. We may have qualifiers on the element types. To set 480 up TYPE_MAIN_VARIANT correctly, we need to form the 481 composite of the unqualified types and add the qualifiers 482 back at the end. */ 483 quals = TYPE_QUALS (strip_array_types (elt)); 484 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED); 485 t1 = build_array_type (unqual_elt, 486 TYPE_DOMAIN ((TYPE_DOMAIN (t1) 487 && (d2_variable 488 || d2_zero 489 || !d1_variable)) 490 ? t1 491 : t2)); 492 /* Ensure a composite type involving a zero-length array type 493 is a zero-length type not an incomplete type. */ 494 if (d1_zero && d2_zero 495 && (t1_complete || t2_complete) 496 && !COMPLETE_TYPE_P (t1)) 497 { 498 TYPE_SIZE (t1) = bitsize_zero_node; 499 TYPE_SIZE_UNIT (t1) = size_zero_node; 500 } 501 t1 = c_build_qualified_type (t1, quals); 502 return build_type_attribute_variant (t1, attributes); 503 } 504 505 case ENUMERAL_TYPE: 506 case RECORD_TYPE: 507 case UNION_TYPE: 508 if (attributes != NULL) 509 { 510 /* Try harder not to create a new aggregate type. */ 511 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) 512 return t1; 513 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) 514 return t2; 515 } 516 return build_type_attribute_variant (t1, attributes); 517 518 case FUNCTION_TYPE: 519 /* Function types: prefer the one that specified arg types. 520 If both do, merge the arg types. Also merge the return types. */ 521 { 522 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); 523 tree p1 = TYPE_ARG_TYPES (t1); 524 tree p2 = TYPE_ARG_TYPES (t2); 525 int len; 526 tree newargs, n; 527 int i; 528 529 /* Save space: see if the result is identical to one of the args. */ 530 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2)) 531 return build_type_attribute_variant (t1, attributes); 532 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1)) 533 return build_type_attribute_variant (t2, attributes); 534 535 /* Simple way if one arg fails to specify argument types. */ 536 if (TYPE_ARG_TYPES (t1) == 0) 537 { 538 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2)); 539 t1 = build_type_attribute_variant (t1, attributes); 540 return qualify_type (t1, t2); 541 } 542 if (TYPE_ARG_TYPES (t2) == 0) 543 { 544 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1)); 545 t1 = build_type_attribute_variant (t1, attributes); 546 return qualify_type (t1, t2); 547 } 548 549 /* If both args specify argument types, we must merge the two 550 lists, argument by argument. */ 551 552 len = list_length (p1); 553 newargs = 0; 554 555 for (i = 0; i < len; i++) 556 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); 557 558 n = newargs; 559 560 for (; p1; 561 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) 562 { 563 /* A null type means arg type is not specified. 564 Take whatever the other function type has. */ 565 if (TREE_VALUE (p1) == 0) 566 { 567 TREE_VALUE (n) = TREE_VALUE (p2); 568 goto parm_done; 569 } 570 if (TREE_VALUE (p2) == 0) 571 { 572 TREE_VALUE (n) = TREE_VALUE (p1); 573 goto parm_done; 574 } 575 576 /* Given wait (union {union wait *u; int *i} *) 577 and wait (union wait *), 578 prefer union wait * as type of parm. */ 579 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE 580 && TREE_VALUE (p1) != TREE_VALUE (p2)) 581 { 582 tree memb; 583 tree mv2 = TREE_VALUE (p2); 584 if (mv2 && mv2 != error_mark_node 585 && TREE_CODE (mv2) != ARRAY_TYPE) 586 mv2 = TYPE_MAIN_VARIANT (mv2); 587 for (memb = TYPE_FIELDS (TREE_VALUE (p1)); 588 memb; memb = DECL_CHAIN (memb)) 589 { 590 tree mv3 = TREE_TYPE (memb); 591 if (mv3 && mv3 != error_mark_node 592 && TREE_CODE (mv3) != ARRAY_TYPE) 593 mv3 = TYPE_MAIN_VARIANT (mv3); 594 if (comptypes (mv3, mv2)) 595 { 596 TREE_VALUE (n) = composite_type (TREE_TYPE (memb), 597 TREE_VALUE (p2)); 598 pedwarn (input_location, OPT_Wpedantic, 599 "function types not truly compatible in ISO C"); 600 goto parm_done; 601 } 602 } 603 } 604 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE 605 && TREE_VALUE (p2) != TREE_VALUE (p1)) 606 { 607 tree memb; 608 tree mv1 = TREE_VALUE (p1); 609 if (mv1 && mv1 != error_mark_node 610 && TREE_CODE (mv1) != ARRAY_TYPE) 611 mv1 = TYPE_MAIN_VARIANT (mv1); 612 for (memb = TYPE_FIELDS (TREE_VALUE (p2)); 613 memb; memb = DECL_CHAIN (memb)) 614 { 615 tree mv3 = TREE_TYPE (memb); 616 if (mv3 && mv3 != error_mark_node 617 && TREE_CODE (mv3) != ARRAY_TYPE) 618 mv3 = TYPE_MAIN_VARIANT (mv3); 619 if (comptypes (mv3, mv1)) 620 { 621 TREE_VALUE (n) = composite_type (TREE_TYPE (memb), 622 TREE_VALUE (p1)); 623 pedwarn (input_location, OPT_Wpedantic, 624 "function types not truly compatible in ISO C"); 625 goto parm_done; 626 } 627 } 628 } 629 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2)); 630 parm_done: ; 631 } 632 633 t1 = build_function_type (valtype, newargs); 634 t1 = qualify_type (t1, t2); 635 /* ... falls through ... */ 636 } 637 638 default: 639 return build_type_attribute_variant (t1, attributes); 640 } 641 642} 643 644/* Return the type of a conditional expression between pointers to 645 possibly differently qualified versions of compatible types. 646 647 We assume that comp_target_types has already been done and returned 648 nonzero; if that isn't so, this may crash. */ 649 650static tree 651common_pointer_type (tree t1, tree t2) 652{ 653 tree attributes; 654 tree pointed_to_1, mv1; 655 tree pointed_to_2, mv2; 656 tree target; 657 unsigned target_quals; 658 addr_space_t as1, as2, as_common; 659 int quals1, quals2; 660 661 /* Save time if the two types are the same. */ 662 663 if (t1 == t2) return t1; 664 665 /* If one type is nonsense, use the other. */ 666 if (t1 == error_mark_node) 667 return t2; 668 if (t2 == error_mark_node) 669 return t1; 670 671 gcc_assert (TREE_CODE (t1) == POINTER_TYPE 672 && TREE_CODE (t2) == POINTER_TYPE); 673 674 /* Merge the attributes. */ 675 attributes = targetm.merge_type_attributes (t1, t2); 676 677 /* Find the composite type of the target types, and combine the 678 qualifiers of the two types' targets. Do not lose qualifiers on 679 array element types by taking the TYPE_MAIN_VARIANT. */ 680 mv1 = pointed_to_1 = TREE_TYPE (t1); 681 mv2 = pointed_to_2 = TREE_TYPE (t2); 682 if (TREE_CODE (mv1) != ARRAY_TYPE) 683 mv1 = TYPE_MAIN_VARIANT (pointed_to_1); 684 if (TREE_CODE (mv2) != ARRAY_TYPE) 685 mv2 = TYPE_MAIN_VARIANT (pointed_to_2); 686 target = composite_type (mv1, mv2); 687 688 /* Strip array types to get correct qualifier for pointers to arrays */ 689 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1)); 690 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2)); 691 692 /* For function types do not merge const qualifiers, but drop them 693 if used inconsistently. The middle-end uses these to mark const 694 and noreturn functions. */ 695 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE) 696 target_quals = (quals1 & quals2); 697 else 698 target_quals = (quals1 | quals2); 699 700 /* If the two named address spaces are different, determine the common 701 superset address space. This is guaranteed to exist due to the 702 assumption that comp_target_type returned non-zero. */ 703 as1 = TYPE_ADDR_SPACE (pointed_to_1); 704 as2 = TYPE_ADDR_SPACE (pointed_to_2); 705 if (!addr_space_superset (as1, as2, &as_common)) 706 gcc_unreachable (); 707 708 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common); 709 710 t1 = build_pointer_type (c_build_qualified_type (target, target_quals)); 711 return build_type_attribute_variant (t1, attributes); 712} 713 714/* Return the common type for two arithmetic types under the usual 715 arithmetic conversions. The default conversions have already been 716 applied, and enumerated types converted to their compatible integer 717 types. The resulting type is unqualified and has no attributes. 718 719 This is the type for the result of most arithmetic operations 720 if the operands have the given two types. */ 721 722static tree 723c_common_type (tree t1, tree t2) 724{ 725 enum tree_code code1; 726 enum tree_code code2; 727 728 /* If one type is nonsense, use the other. */ 729 if (t1 == error_mark_node) 730 return t2; 731 if (t2 == error_mark_node) 732 return t1; 733 734 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED) 735 t1 = TYPE_MAIN_VARIANT (t1); 736 737 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED) 738 t2 = TYPE_MAIN_VARIANT (t2); 739 740 if (TYPE_ATTRIBUTES (t1) != NULL_TREE) 741 t1 = build_type_attribute_variant (t1, NULL_TREE); 742 743 if (TYPE_ATTRIBUTES (t2) != NULL_TREE) 744 t2 = build_type_attribute_variant (t2, NULL_TREE); 745 746 /* Save time if the two types are the same. */ 747 748 if (t1 == t2) return t1; 749 750 code1 = TREE_CODE (t1); 751 code2 = TREE_CODE (t2); 752 753 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE 754 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE 755 || code1 == INTEGER_TYPE); 756 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE 757 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE 758 || code2 == INTEGER_TYPE); 759 760 /* When one operand is a decimal float type, the other operand cannot be 761 a generic float type or a complex type. We also disallow vector types 762 here. */ 763 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2)) 764 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2))) 765 { 766 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE) 767 { 768 error ("can%'t mix operands of decimal float and vector types"); 769 return error_mark_node; 770 } 771 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) 772 { 773 error ("can%'t mix operands of decimal float and complex types"); 774 return error_mark_node; 775 } 776 if (code1 == REAL_TYPE && code2 == REAL_TYPE) 777 { 778 error ("can%'t mix operands of decimal float and other float types"); 779 return error_mark_node; 780 } 781 } 782 783 /* If one type is a vector type, return that type. (How the usual 784 arithmetic conversions apply to the vector types extension is not 785 precisely specified.) */ 786 if (code1 == VECTOR_TYPE) 787 return t1; 788 789 if (code2 == VECTOR_TYPE) 790 return t2; 791 792 /* If one type is complex, form the common type of the non-complex 793 components, then make that complex. Use T1 or T2 if it is the 794 required type. */ 795 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) 796 { 797 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; 798 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; 799 tree subtype = c_common_type (subtype1, subtype2); 800 801 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) 802 return t1; 803 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) 804 return t2; 805 else 806 return build_complex_type (subtype); 807 } 808 809 /* If only one is real, use it as the result. */ 810 811 if (code1 == REAL_TYPE && code2 != REAL_TYPE) 812 return t1; 813 814 if (code2 == REAL_TYPE && code1 != REAL_TYPE) 815 return t2; 816 817 /* If both are real and either are decimal floating point types, use 818 the decimal floating point type with the greater precision. */ 819 820 if (code1 == REAL_TYPE && code2 == REAL_TYPE) 821 { 822 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node 823 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node) 824 return dfloat128_type_node; 825 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node 826 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node) 827 return dfloat64_type_node; 828 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node 829 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node) 830 return dfloat32_type_node; 831 } 832 833 /* Deal with fixed-point types. */ 834 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE) 835 { 836 unsigned int unsignedp = 0, satp = 0; 837 machine_mode m1, m2; 838 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit; 839 840 m1 = TYPE_MODE (t1); 841 m2 = TYPE_MODE (t2); 842 843 /* If one input type is saturating, the result type is saturating. */ 844 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2)) 845 satp = 1; 846 847 /* If both fixed-point types are unsigned, the result type is unsigned. 848 When mixing fixed-point and integer types, follow the sign of the 849 fixed-point type. 850 Otherwise, the result type is signed. */ 851 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2) 852 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE) 853 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE 854 && TYPE_UNSIGNED (t1)) 855 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE 856 && TYPE_UNSIGNED (t2))) 857 unsignedp = 1; 858 859 /* The result type is signed. */ 860 if (unsignedp == 0) 861 { 862 /* If the input type is unsigned, we need to convert to the 863 signed type. */ 864 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1)) 865 { 866 enum mode_class mclass = (enum mode_class) 0; 867 if (GET_MODE_CLASS (m1) == MODE_UFRACT) 868 mclass = MODE_FRACT; 869 else if (GET_MODE_CLASS (m1) == MODE_UACCUM) 870 mclass = MODE_ACCUM; 871 else 872 gcc_unreachable (); 873 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0); 874 } 875 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2)) 876 { 877 enum mode_class mclass = (enum mode_class) 0; 878 if (GET_MODE_CLASS (m2) == MODE_UFRACT) 879 mclass = MODE_FRACT; 880 else if (GET_MODE_CLASS (m2) == MODE_UACCUM) 881 mclass = MODE_ACCUM; 882 else 883 gcc_unreachable (); 884 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0); 885 } 886 } 887 888 if (code1 == FIXED_POINT_TYPE) 889 { 890 fbit1 = GET_MODE_FBIT (m1); 891 ibit1 = GET_MODE_IBIT (m1); 892 } 893 else 894 { 895 fbit1 = 0; 896 /* Signed integers need to subtract one sign bit. */ 897 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1)); 898 } 899 900 if (code2 == FIXED_POINT_TYPE) 901 { 902 fbit2 = GET_MODE_FBIT (m2); 903 ibit2 = GET_MODE_IBIT (m2); 904 } 905 else 906 { 907 fbit2 = 0; 908 /* Signed integers need to subtract one sign bit. */ 909 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2)); 910 } 911 912 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2; 913 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2; 914 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp, 915 satp); 916 } 917 918 /* Both real or both integers; use the one with greater precision. */ 919 920 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) 921 return t1; 922 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) 923 return t2; 924 925 /* Same precision. Prefer long longs to longs to ints when the 926 same precision, following the C99 rules on integer type rank 927 (which are equivalent to the C90 rules for C90 types). */ 928 929 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node 930 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node) 931 return long_long_unsigned_type_node; 932 933 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node 934 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node) 935 { 936 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) 937 return long_long_unsigned_type_node; 938 else 939 return long_long_integer_type_node; 940 } 941 942 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node 943 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) 944 return long_unsigned_type_node; 945 946 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node 947 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) 948 { 949 /* But preserve unsignedness from the other type, 950 since long cannot hold all the values of an unsigned int. */ 951 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) 952 return long_unsigned_type_node; 953 else 954 return long_integer_type_node; 955 } 956 957 /* Likewise, prefer long double to double even if same size. */ 958 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node 959 || TYPE_MAIN_VARIANT (t2) == long_double_type_node) 960 return long_double_type_node; 961 962 /* Likewise, prefer double to float even if same size. 963 We got a couple of embedded targets with 32 bit doubles, and the 964 pdp11 might have 64 bit floats. */ 965 if (TYPE_MAIN_VARIANT (t1) == double_type_node 966 || TYPE_MAIN_VARIANT (t2) == double_type_node) 967 return double_type_node; 968 969 /* Otherwise prefer the unsigned one. */ 970 971 if (TYPE_UNSIGNED (t1)) 972 return t1; 973 else 974 return t2; 975} 976 977/* Wrapper around c_common_type that is used by c-common.c and other 978 front end optimizations that remove promotions. ENUMERAL_TYPEs 979 are allowed here and are converted to their compatible integer types. 980 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or 981 preferably a non-Boolean type as the common type. */ 982tree 983common_type (tree t1, tree t2) 984{ 985 if (TREE_CODE (t1) == ENUMERAL_TYPE) 986 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1); 987 if (TREE_CODE (t2) == ENUMERAL_TYPE) 988 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1); 989 990 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */ 991 if (TREE_CODE (t1) == BOOLEAN_TYPE 992 && TREE_CODE (t2) == BOOLEAN_TYPE) 993 return boolean_type_node; 994 995 /* If either type is BOOLEAN_TYPE, then return the other. */ 996 if (TREE_CODE (t1) == BOOLEAN_TYPE) 997 return t2; 998 if (TREE_CODE (t2) == BOOLEAN_TYPE) 999 return t1; 1000 1001 return c_common_type (t1, t2); 1002} 1003 1004/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment 1005 or various other operations. Return 2 if they are compatible 1006 but a warning may be needed if you use them together. */ 1007 1008int 1009comptypes (tree type1, tree type2) 1010{ 1011 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; 1012 int val; 1013 1014 val = comptypes_internal (type1, type2, NULL, NULL); 1015 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); 1016 1017 return val; 1018} 1019 1020/* Like comptypes, but if it returns non-zero because enum and int are 1021 compatible, it sets *ENUM_AND_INT_P to true. */ 1022 1023static int 1024comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p) 1025{ 1026 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; 1027 int val; 1028 1029 val = comptypes_internal (type1, type2, enum_and_int_p, NULL); 1030 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); 1031 1032 return val; 1033} 1034 1035/* Like comptypes, but if it returns nonzero for different types, it 1036 sets *DIFFERENT_TYPES_P to true. */ 1037 1038int 1039comptypes_check_different_types (tree type1, tree type2, 1040 bool *different_types_p) 1041{ 1042 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; 1043 int val; 1044 1045 val = comptypes_internal (type1, type2, NULL, different_types_p); 1046 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); 1047 1048 return val; 1049} 1050 1051/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment 1052 or various other operations. Return 2 if they are compatible 1053 but a warning may be needed if you use them together. If 1054 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a 1055 compatible integer type, then this sets *ENUM_AND_INT_P to true; 1056 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not 1057 NULL, and the types are compatible but different enough not to be 1058 permitted in C11 typedef redeclarations, then this sets 1059 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to 1060 false, but may or may not be set if the types are incompatible. 1061 This differs from comptypes, in that we don't free the seen 1062 types. */ 1063 1064static int 1065comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p, 1066 bool *different_types_p) 1067{ 1068 const_tree t1 = type1; 1069 const_tree t2 = type2; 1070 int attrval, val; 1071 1072 /* Suppress errors caused by previously reported errors. */ 1073 1074 if (t1 == t2 || !t1 || !t2 1075 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) 1076 return 1; 1077 1078 /* Enumerated types are compatible with integer types, but this is 1079 not transitive: two enumerated types in the same translation unit 1080 are compatible with each other only if they are the same type. */ 1081 1082 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE) 1083 { 1084 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1)); 1085 if (TREE_CODE (t2) != VOID_TYPE) 1086 { 1087 if (enum_and_int_p != NULL) 1088 *enum_and_int_p = true; 1089 if (different_types_p != NULL) 1090 *different_types_p = true; 1091 } 1092 } 1093 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE) 1094 { 1095 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2)); 1096 if (TREE_CODE (t1) != VOID_TYPE) 1097 { 1098 if (enum_and_int_p != NULL) 1099 *enum_and_int_p = true; 1100 if (different_types_p != NULL) 1101 *different_types_p = true; 1102 } 1103 } 1104 1105 if (t1 == t2) 1106 return 1; 1107 1108 /* Different classes of types can't be compatible. */ 1109 1110 if (TREE_CODE (t1) != TREE_CODE (t2)) 1111 return 0; 1112 1113 /* Qualifiers must match. C99 6.7.3p9 */ 1114 1115 if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) 1116 return 0; 1117 1118 /* Allow for two different type nodes which have essentially the same 1119 definition. Note that we already checked for equality of the type 1120 qualifiers (just above). */ 1121 1122 if (TREE_CODE (t1) != ARRAY_TYPE 1123 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) 1124 return 1; 1125 1126 /* 1 if no need for warning yet, 2 if warning cause has been seen. */ 1127 if (!(attrval = comp_type_attributes (t1, t2))) 1128 return 0; 1129 1130 /* 1 if no need for warning yet, 2 if warning cause has been seen. */ 1131 val = 0; 1132 1133 switch (TREE_CODE (t1)) 1134 { 1135 case POINTER_TYPE: 1136 /* Do not remove mode or aliasing information. */ 1137 if (TYPE_MODE (t1) != TYPE_MODE (t2) 1138 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)) 1139 break; 1140 val = (TREE_TYPE (t1) == TREE_TYPE (t2) 1141 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), 1142 enum_and_int_p, different_types_p)); 1143 break; 1144 1145 case FUNCTION_TYPE: 1146 val = function_types_compatible_p (t1, t2, enum_and_int_p, 1147 different_types_p); 1148 break; 1149 1150 case ARRAY_TYPE: 1151 { 1152 tree d1 = TYPE_DOMAIN (t1); 1153 tree d2 = TYPE_DOMAIN (t2); 1154 bool d1_variable, d2_variable; 1155 bool d1_zero, d2_zero; 1156 val = 1; 1157 1158 /* Target types must match incl. qualifiers. */ 1159 if (TREE_TYPE (t1) != TREE_TYPE (t2) 1160 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), 1161 enum_and_int_p, 1162 different_types_p))) 1163 return 0; 1164 1165 if (different_types_p != NULL 1166 && (d1 == 0) != (d2 == 0)) 1167 *different_types_p = true; 1168 /* Sizes must match unless one is missing or variable. */ 1169 if (d1 == 0 || d2 == 0 || d1 == d2) 1170 break; 1171 1172 d1_zero = !TYPE_MAX_VALUE (d1); 1173 d2_zero = !TYPE_MAX_VALUE (d2); 1174 1175 d1_variable = (!d1_zero 1176 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST 1177 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); 1178 d2_variable = (!d2_zero 1179 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST 1180 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); 1181 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); 1182 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); 1183 1184 if (different_types_p != NULL 1185 && d1_variable != d2_variable) 1186 *different_types_p = true; 1187 if (d1_variable || d2_variable) 1188 break; 1189 if (d1_zero && d2_zero) 1190 break; 1191 if (d1_zero || d2_zero 1192 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)) 1193 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2))) 1194 val = 0; 1195 1196 break; 1197 } 1198 1199 case ENUMERAL_TYPE: 1200 case RECORD_TYPE: 1201 case UNION_TYPE: 1202 if (val != 1 && !same_translation_unit_p (t1, t2)) 1203 { 1204 tree a1 = TYPE_ATTRIBUTES (t1); 1205 tree a2 = TYPE_ATTRIBUTES (t2); 1206 1207 if (! attribute_list_contained (a1, a2) 1208 && ! attribute_list_contained (a2, a1)) 1209 break; 1210 1211 if (attrval != 2) 1212 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, 1213 different_types_p); 1214 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, 1215 different_types_p); 1216 } 1217 break; 1218 1219 case VECTOR_TYPE: 1220 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) 1221 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), 1222 enum_and_int_p, different_types_p)); 1223 break; 1224 1225 default: 1226 break; 1227 } 1228 return attrval == 2 && val == 1 ? 2 : val; 1229} 1230 1231/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring 1232 their qualifiers, except for named address spaces. If the pointers point to 1233 different named addresses, then we must determine if one address space is a 1234 subset of the other. */ 1235 1236static int 1237comp_target_types (location_t location, tree ttl, tree ttr) 1238{ 1239 int val; 1240 int val_ped; 1241 tree mvl = TREE_TYPE (ttl); 1242 tree mvr = TREE_TYPE (ttr); 1243 addr_space_t asl = TYPE_ADDR_SPACE (mvl); 1244 addr_space_t asr = TYPE_ADDR_SPACE (mvr); 1245 addr_space_t as_common; 1246 bool enum_and_int_p; 1247 1248 /* Fail if pointers point to incompatible address spaces. */ 1249 if (!addr_space_superset (asl, asr, &as_common)) 1250 return 0; 1251 1252 /* For pedantic record result of comptypes on arrays before losing 1253 qualifiers on the element type below. */ 1254 val_ped = 1; 1255 1256 if (TREE_CODE (mvl) == ARRAY_TYPE 1257 && TREE_CODE (mvr) == ARRAY_TYPE) 1258 val_ped = comptypes (mvl, mvr); 1259 1260 /* Qualifiers on element types of array types that are 1261 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */ 1262 1263 mvl = (TYPE_ATOMIC (strip_array_types (mvl)) 1264 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC) 1265 : TYPE_MAIN_VARIANT (mvl)); 1266 1267 mvr = (TYPE_ATOMIC (strip_array_types (mvr)) 1268 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC) 1269 : TYPE_MAIN_VARIANT (mvr)); 1270 1271 enum_and_int_p = false; 1272 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p); 1273 1274 if (val == 1 && val_ped != 1) 1275 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers " 1276 "are incompatible in ISO C"); 1277 1278 if (val == 2) 1279 pedwarn (location, OPT_Wpedantic, "types are not quite compatible"); 1280 1281 if (val == 1 && enum_and_int_p && warn_cxx_compat) 1282 warning_at (location, OPT_Wc___compat, 1283 "pointer target types incompatible in C++"); 1284 1285 return val; 1286} 1287 1288/* Subroutines of `comptypes'. */ 1289 1290/* Determine whether two trees derive from the same translation unit. 1291 If the CONTEXT chain ends in a null, that tree's context is still 1292 being parsed, so if two trees have context chains ending in null, 1293 they're in the same translation unit. */ 1294int 1295same_translation_unit_p (const_tree t1, const_tree t2) 1296{ 1297 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL) 1298 switch (TREE_CODE_CLASS (TREE_CODE (t1))) 1299 { 1300 case tcc_declaration: 1301 t1 = DECL_CONTEXT (t1); break; 1302 case tcc_type: 1303 t1 = TYPE_CONTEXT (t1); break; 1304 case tcc_exceptional: 1305 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */ 1306 default: gcc_unreachable (); 1307 } 1308 1309 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL) 1310 switch (TREE_CODE_CLASS (TREE_CODE (t2))) 1311 { 1312 case tcc_declaration: 1313 t2 = DECL_CONTEXT (t2); break; 1314 case tcc_type: 1315 t2 = TYPE_CONTEXT (t2); break; 1316 case tcc_exceptional: 1317 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */ 1318 default: gcc_unreachable (); 1319 } 1320 1321 return t1 == t2; 1322} 1323 1324/* Allocate the seen two types, assuming that they are compatible. */ 1325 1326static struct tagged_tu_seen_cache * 1327alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2) 1328{ 1329 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache); 1330 tu->next = tagged_tu_seen_base; 1331 tu->t1 = t1; 1332 tu->t2 = t2; 1333 1334 tagged_tu_seen_base = tu; 1335 1336 /* The C standard says that two structures in different translation 1337 units are compatible with each other only if the types of their 1338 fields are compatible (among other things). We assume that they 1339 are compatible until proven otherwise when building the cache. 1340 An example where this can occur is: 1341 struct a 1342 { 1343 struct a *next; 1344 }; 1345 If we are comparing this against a similar struct in another TU, 1346 and did not assume they were compatible, we end up with an infinite 1347 loop. */ 1348 tu->val = 1; 1349 return tu; 1350} 1351 1352/* Free the seen types until we get to TU_TIL. */ 1353 1354static void 1355free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til) 1356{ 1357 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base; 1358 while (tu != tu_til) 1359 { 1360 const struct tagged_tu_seen_cache *const tu1 1361 = (const struct tagged_tu_seen_cache *) tu; 1362 tu = tu1->next; 1363 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1)); 1364 } 1365 tagged_tu_seen_base = tu_til; 1366} 1367 1368/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are 1369 compatible. If the two types are not the same (which has been 1370 checked earlier), this can only happen when multiple translation 1371 units are being compiled. See C99 6.2.7 paragraph 1 for the exact 1372 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in 1373 comptypes_internal. */ 1374 1375static int 1376tagged_types_tu_compatible_p (const_tree t1, const_tree t2, 1377 bool *enum_and_int_p, bool *different_types_p) 1378{ 1379 tree s1, s2; 1380 bool needs_warning = false; 1381 1382 /* We have to verify that the tags of the types are the same. This 1383 is harder than it looks because this may be a typedef, so we have 1384 to go look at the original type. It may even be a typedef of a 1385 typedef... 1386 In the case of compiler-created builtin structs the TYPE_DECL 1387 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */ 1388 while (TYPE_NAME (t1) 1389 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL 1390 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1))) 1391 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1)); 1392 1393 while (TYPE_NAME (t2) 1394 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL 1395 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2))) 1396 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2)); 1397 1398 /* C90 didn't have the requirement that the two tags be the same. */ 1399 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2)) 1400 return 0; 1401 1402 /* C90 didn't say what happened if one or both of the types were 1403 incomplete; we choose to follow C99 rules here, which is that they 1404 are compatible. */ 1405 if (TYPE_SIZE (t1) == NULL 1406 || TYPE_SIZE (t2) == NULL) 1407 return 1; 1408 1409 { 1410 const struct tagged_tu_seen_cache * tts_i; 1411 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next) 1412 if (tts_i->t1 == t1 && tts_i->t2 == t2) 1413 return tts_i->val; 1414 } 1415 1416 switch (TREE_CODE (t1)) 1417 { 1418 case ENUMERAL_TYPE: 1419 { 1420 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); 1421 /* Speed up the case where the type values are in the same order. */ 1422 tree tv1 = TYPE_VALUES (t1); 1423 tree tv2 = TYPE_VALUES (t2); 1424 1425 if (tv1 == tv2) 1426 { 1427 return 1; 1428 } 1429 1430 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2)) 1431 { 1432 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2)) 1433 break; 1434 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1) 1435 { 1436 tu->val = 0; 1437 return 0; 1438 } 1439 } 1440 1441 if (tv1 == NULL_TREE && tv2 == NULL_TREE) 1442 { 1443 return 1; 1444 } 1445 if (tv1 == NULL_TREE || tv2 == NULL_TREE) 1446 { 1447 tu->val = 0; 1448 return 0; 1449 } 1450 1451 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2))) 1452 { 1453 tu->val = 0; 1454 return 0; 1455 } 1456 1457 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1)) 1458 { 1459 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2)); 1460 if (s2 == NULL 1461 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1) 1462 { 1463 tu->val = 0; 1464 return 0; 1465 } 1466 } 1467 return 1; 1468 } 1469 1470 case UNION_TYPE: 1471 { 1472 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); 1473 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2))) 1474 { 1475 tu->val = 0; 1476 return 0; 1477 } 1478 1479 /* Speed up the common case where the fields are in the same order. */ 1480 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2; 1481 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) 1482 { 1483 int result; 1484 1485 if (DECL_NAME (s1) != DECL_NAME (s2)) 1486 break; 1487 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), 1488 enum_and_int_p, different_types_p); 1489 1490 if (result != 1 && !DECL_NAME (s1)) 1491 break; 1492 if (result == 0) 1493 { 1494 tu->val = 0; 1495 return 0; 1496 } 1497 if (result == 2) 1498 needs_warning = true; 1499 1500 if (TREE_CODE (s1) == FIELD_DECL 1501 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), 1502 DECL_FIELD_BIT_OFFSET (s2)) != 1) 1503 { 1504 tu->val = 0; 1505 return 0; 1506 } 1507 } 1508 if (!s1 && !s2) 1509 { 1510 tu->val = needs_warning ? 2 : 1; 1511 return tu->val; 1512 } 1513 1514 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1)) 1515 { 1516 bool ok = false; 1517 1518 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2)) 1519 if (DECL_NAME (s1) == DECL_NAME (s2)) 1520 { 1521 int result; 1522 1523 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), 1524 enum_and_int_p, 1525 different_types_p); 1526 1527 if (result != 1 && !DECL_NAME (s1)) 1528 continue; 1529 if (result == 0) 1530 { 1531 tu->val = 0; 1532 return 0; 1533 } 1534 if (result == 2) 1535 needs_warning = true; 1536 1537 if (TREE_CODE (s1) == FIELD_DECL 1538 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), 1539 DECL_FIELD_BIT_OFFSET (s2)) != 1) 1540 break; 1541 1542 ok = true; 1543 break; 1544 } 1545 if (!ok) 1546 { 1547 tu->val = 0; 1548 return 0; 1549 } 1550 } 1551 tu->val = needs_warning ? 2 : 10; 1552 return tu->val; 1553 } 1554 1555 case RECORD_TYPE: 1556 { 1557 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); 1558 1559 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); 1560 s1 && s2; 1561 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) 1562 { 1563 int result; 1564 if (TREE_CODE (s1) != TREE_CODE (s2) 1565 || DECL_NAME (s1) != DECL_NAME (s2)) 1566 break; 1567 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), 1568 enum_and_int_p, different_types_p); 1569 if (result == 0) 1570 break; 1571 if (result == 2) 1572 needs_warning = true; 1573 1574 if (TREE_CODE (s1) == FIELD_DECL 1575 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), 1576 DECL_FIELD_BIT_OFFSET (s2)) != 1) 1577 break; 1578 } 1579 if (s1 && s2) 1580 tu->val = 0; 1581 else 1582 tu->val = needs_warning ? 2 : 1; 1583 return tu->val; 1584 } 1585 1586 default: 1587 gcc_unreachable (); 1588 } 1589} 1590 1591/* Return 1 if two function types F1 and F2 are compatible. 1592 If either type specifies no argument types, 1593 the other must specify a fixed number of self-promoting arg types. 1594 Otherwise, if one type specifies only the number of arguments, 1595 the other must specify that number of self-promoting arg types. 1596 Otherwise, the argument types must match. 1597 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */ 1598 1599static int 1600function_types_compatible_p (const_tree f1, const_tree f2, 1601 bool *enum_and_int_p, bool *different_types_p) 1602{ 1603 tree args1, args2; 1604 /* 1 if no need for warning yet, 2 if warning cause has been seen. */ 1605 int val = 1; 1606 int val1; 1607 tree ret1, ret2; 1608 1609 ret1 = TREE_TYPE (f1); 1610 ret2 = TREE_TYPE (f2); 1611 1612 /* 'volatile' qualifiers on a function's return type used to mean 1613 the function is noreturn. */ 1614 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2)) 1615 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>"); 1616 if (TYPE_VOLATILE (ret1)) 1617 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1), 1618 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE); 1619 if (TYPE_VOLATILE (ret2)) 1620 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2), 1621 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE); 1622 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p); 1623 if (val == 0) 1624 return 0; 1625 1626 args1 = TYPE_ARG_TYPES (f1); 1627 args2 = TYPE_ARG_TYPES (f2); 1628 1629 if (different_types_p != NULL 1630 && (args1 == 0) != (args2 == 0)) 1631 *different_types_p = true; 1632 1633 /* An unspecified parmlist matches any specified parmlist 1634 whose argument types don't need default promotions. */ 1635 1636 if (args1 == 0) 1637 { 1638 if (!self_promoting_args_p (args2)) 1639 return 0; 1640 /* If one of these types comes from a non-prototype fn definition, 1641 compare that with the other type's arglist. 1642 If they don't match, ask for a warning (but no error). */ 1643 if (TYPE_ACTUAL_ARG_TYPES (f1) 1644 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), 1645 enum_and_int_p, different_types_p)) 1646 val = 2; 1647 return val; 1648 } 1649 if (args2 == 0) 1650 { 1651 if (!self_promoting_args_p (args1)) 1652 return 0; 1653 if (TYPE_ACTUAL_ARG_TYPES (f2) 1654 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), 1655 enum_and_int_p, different_types_p)) 1656 val = 2; 1657 return val; 1658 } 1659 1660 /* Both types have argument lists: compare them and propagate results. */ 1661 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p, 1662 different_types_p); 1663 return val1 != 1 ? val1 : val; 1664} 1665 1666/* Check two lists of types for compatibility, returning 0 for 1667 incompatible, 1 for compatible, or 2 for compatible with 1668 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in 1669 comptypes_internal. */ 1670 1671static int 1672type_lists_compatible_p (const_tree args1, const_tree args2, 1673 bool *enum_and_int_p, bool *different_types_p) 1674{ 1675 /* 1 if no need for warning yet, 2 if warning cause has been seen. */ 1676 int val = 1; 1677 int newval = 0; 1678 1679 while (1) 1680 { 1681 tree a1, mv1, a2, mv2; 1682 if (args1 == 0 && args2 == 0) 1683 return val; 1684 /* If one list is shorter than the other, 1685 they fail to match. */ 1686 if (args1 == 0 || args2 == 0) 1687 return 0; 1688 mv1 = a1 = TREE_VALUE (args1); 1689 mv2 = a2 = TREE_VALUE (args2); 1690 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE) 1691 mv1 = (TYPE_ATOMIC (mv1) 1692 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1), 1693 TYPE_QUAL_ATOMIC) 1694 : TYPE_MAIN_VARIANT (mv1)); 1695 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE) 1696 mv2 = (TYPE_ATOMIC (mv2) 1697 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2), 1698 TYPE_QUAL_ATOMIC) 1699 : TYPE_MAIN_VARIANT (mv2)); 1700 /* A null pointer instead of a type 1701 means there is supposed to be an argument 1702 but nothing is specified about what type it has. 1703 So match anything that self-promotes. */ 1704 if (different_types_p != NULL 1705 && (a1 == 0) != (a2 == 0)) 1706 *different_types_p = true; 1707 if (a1 == 0) 1708 { 1709 if (c_type_promotes_to (a2) != a2) 1710 return 0; 1711 } 1712 else if (a2 == 0) 1713 { 1714 if (c_type_promotes_to (a1) != a1) 1715 return 0; 1716 } 1717 /* If one of the lists has an error marker, ignore this arg. */ 1718 else if (TREE_CODE (a1) == ERROR_MARK 1719 || TREE_CODE (a2) == ERROR_MARK) 1720 ; 1721 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p, 1722 different_types_p))) 1723 { 1724 if (different_types_p != NULL) 1725 *different_types_p = true; 1726 /* Allow wait (union {union wait *u; int *i} *) 1727 and wait (union wait *) to be compatible. */ 1728 if (TREE_CODE (a1) == UNION_TYPE 1729 && (TYPE_NAME (a1) == 0 1730 || TYPE_TRANSPARENT_AGGR (a1)) 1731 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST 1732 && tree_int_cst_equal (TYPE_SIZE (a1), 1733 TYPE_SIZE (a2))) 1734 { 1735 tree memb; 1736 for (memb = TYPE_FIELDS (a1); 1737 memb; memb = DECL_CHAIN (memb)) 1738 { 1739 tree mv3 = TREE_TYPE (memb); 1740 if (mv3 && mv3 != error_mark_node 1741 && TREE_CODE (mv3) != ARRAY_TYPE) 1742 mv3 = (TYPE_ATOMIC (mv3) 1743 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3), 1744 TYPE_QUAL_ATOMIC) 1745 : TYPE_MAIN_VARIANT (mv3)); 1746 if (comptypes_internal (mv3, mv2, enum_and_int_p, 1747 different_types_p)) 1748 break; 1749 } 1750 if (memb == 0) 1751 return 0; 1752 } 1753 else if (TREE_CODE (a2) == UNION_TYPE 1754 && (TYPE_NAME (a2) == 0 1755 || TYPE_TRANSPARENT_AGGR (a2)) 1756 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST 1757 && tree_int_cst_equal (TYPE_SIZE (a2), 1758 TYPE_SIZE (a1))) 1759 { 1760 tree memb; 1761 for (memb = TYPE_FIELDS (a2); 1762 memb; memb = DECL_CHAIN (memb)) 1763 { 1764 tree mv3 = TREE_TYPE (memb); 1765 if (mv3 && mv3 != error_mark_node 1766 && TREE_CODE (mv3) != ARRAY_TYPE) 1767 mv3 = (TYPE_ATOMIC (mv3) 1768 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3), 1769 TYPE_QUAL_ATOMIC) 1770 : TYPE_MAIN_VARIANT (mv3)); 1771 if (comptypes_internal (mv3, mv1, enum_and_int_p, 1772 different_types_p)) 1773 break; 1774 } 1775 if (memb == 0) 1776 return 0; 1777 } 1778 else 1779 return 0; 1780 } 1781 1782 /* comptypes said ok, but record if it said to warn. */ 1783 if (newval > val) 1784 val = newval; 1785 1786 args1 = TREE_CHAIN (args1); 1787 args2 = TREE_CHAIN (args2); 1788 } 1789} 1790 1791/* Compute the size to increment a pointer by. When a function type or void 1792 type or incomplete type is passed, size_one_node is returned. 1793 This function does not emit any diagnostics; the caller is responsible 1794 for that. */ 1795 1796static tree 1797c_size_in_bytes (const_tree type) 1798{ 1799 enum tree_code code = TREE_CODE (type); 1800 1801 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK 1802 || !COMPLETE_TYPE_P (type)) 1803 return size_one_node; 1804 1805 /* Convert in case a char is more than one unit. */ 1806 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), 1807 size_int (TYPE_PRECISION (char_type_node) 1808 / BITS_PER_UNIT)); 1809} 1810 1811/* Return either DECL or its known constant value (if it has one). */ 1812 1813tree 1814decl_constant_value (tree decl) 1815{ 1816 if (/* Don't change a variable array bound or initial value to a constant 1817 in a place where a variable is invalid. Note that DECL_INITIAL 1818 isn't valid for a PARM_DECL. */ 1819 current_function_decl != 0 1820 && TREE_CODE (decl) != PARM_DECL 1821 && !TREE_THIS_VOLATILE (decl) 1822 && TREE_READONLY (decl) 1823 && DECL_INITIAL (decl) != 0 1824 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK 1825 /* This is invalid if initial value is not constant. 1826 If it has either a function call, a memory reference, 1827 or a variable, then re-evaluating it could give different results. */ 1828 && TREE_CONSTANT (DECL_INITIAL (decl)) 1829 /* Check for cases where this is sub-optimal, even though valid. */ 1830 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR) 1831 return DECL_INITIAL (decl); 1832 return decl; 1833} 1834 1835/* Convert the array expression EXP to a pointer. */ 1836static tree 1837array_to_pointer_conversion (location_t loc, tree exp) 1838{ 1839 tree orig_exp = exp; 1840 tree type = TREE_TYPE (exp); 1841 tree adr; 1842 tree restype = TREE_TYPE (type); 1843 tree ptrtype; 1844 1845 gcc_assert (TREE_CODE (type) == ARRAY_TYPE); 1846 1847 STRIP_TYPE_NOPS (exp); 1848 1849 if (TREE_NO_WARNING (orig_exp)) 1850 TREE_NO_WARNING (exp) = 1; 1851 1852 ptrtype = build_pointer_type (restype); 1853 1854 if (TREE_CODE (exp) == INDIRECT_REF) 1855 return convert (ptrtype, TREE_OPERAND (exp, 0)); 1856 1857 /* In C++ array compound literals are temporary objects unless they are 1858 const or appear in namespace scope, so they are destroyed too soon 1859 to use them for much of anything (c++/53220). */ 1860 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR) 1861 { 1862 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); 1863 if (!TREE_READONLY (decl) && !TREE_STATIC (decl)) 1864 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat, 1865 "converting an array compound literal to a pointer " 1866 "is ill-formed in C++"); 1867 } 1868 1869 adr = build_unary_op (loc, ADDR_EXPR, exp, 1); 1870 return convert (ptrtype, adr); 1871} 1872 1873/* Convert the function expression EXP to a pointer. */ 1874static tree 1875function_to_pointer_conversion (location_t loc, tree exp) 1876{ 1877 tree orig_exp = exp; 1878 1879 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE); 1880 1881 STRIP_TYPE_NOPS (exp); 1882 1883 if (TREE_NO_WARNING (orig_exp)) 1884 TREE_NO_WARNING (exp) = 1; 1885 1886 return build_unary_op (loc, ADDR_EXPR, exp, 0); 1887} 1888 1889/* Mark EXP as read, not just set, for set but not used -Wunused 1890 warning purposes. */ 1891 1892void 1893mark_exp_read (tree exp) 1894{ 1895 switch (TREE_CODE (exp)) 1896 { 1897 case VAR_DECL: 1898 case PARM_DECL: 1899 DECL_READ_P (exp) = 1; 1900 break; 1901 case ARRAY_REF: 1902 case COMPONENT_REF: 1903 case MODIFY_EXPR: 1904 case REALPART_EXPR: 1905 case IMAGPART_EXPR: 1906 CASE_CONVERT: 1907 case ADDR_EXPR: 1908 mark_exp_read (TREE_OPERAND (exp, 0)); 1909 break; 1910 case COMPOUND_EXPR: 1911 case C_MAYBE_CONST_EXPR: 1912 mark_exp_read (TREE_OPERAND (exp, 1)); 1913 break; 1914 default: 1915 break; 1916 } 1917} 1918 1919/* Perform the default conversion of arrays and functions to pointers. 1920 Return the result of converting EXP. For any other expression, just 1921 return EXP. 1922 1923 LOC is the location of the expression. */ 1924 1925struct c_expr 1926default_function_array_conversion (location_t loc, struct c_expr exp) 1927{ 1928 tree orig_exp = exp.value; 1929 tree type = TREE_TYPE (exp.value); 1930 enum tree_code code = TREE_CODE (type); 1931 1932 switch (code) 1933 { 1934 case ARRAY_TYPE: 1935 { 1936 bool not_lvalue = false; 1937 bool lvalue_array_p; 1938 1939 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR 1940 || CONVERT_EXPR_P (exp.value)) 1941 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type) 1942 { 1943 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR) 1944 not_lvalue = true; 1945 exp.value = TREE_OPERAND (exp.value, 0); 1946 } 1947 1948 if (TREE_NO_WARNING (orig_exp)) 1949 TREE_NO_WARNING (exp.value) = 1; 1950 1951 lvalue_array_p = !not_lvalue && lvalue_p (exp.value); 1952 if (!flag_isoc99 && !lvalue_array_p) 1953 { 1954 /* Before C99, non-lvalue arrays do not decay to pointers. 1955 Normally, using such an array would be invalid; but it can 1956 be used correctly inside sizeof or as a statement expression. 1957 Thus, do not give an error here; an error will result later. */ 1958 return exp; 1959 } 1960 1961 exp.value = array_to_pointer_conversion (loc, exp.value); 1962 } 1963 break; 1964 case FUNCTION_TYPE: 1965 exp.value = function_to_pointer_conversion (loc, exp.value); 1966 break; 1967 default: 1968 break; 1969 } 1970 1971 return exp; 1972} 1973 1974struct c_expr 1975default_function_array_read_conversion (location_t loc, struct c_expr exp) 1976{ 1977 mark_exp_read (exp.value); 1978 return default_function_array_conversion (loc, exp); 1979} 1980 1981/* Return whether EXPR should be treated as an atomic lvalue for the 1982 purposes of load and store handling. */ 1983 1984static bool 1985really_atomic_lvalue (tree expr) 1986{ 1987 if (error_operand_p (expr)) 1988 return false; 1989 if (!TYPE_ATOMIC (TREE_TYPE (expr))) 1990 return false; 1991 if (!lvalue_p (expr)) 1992 return false; 1993 1994 /* Ignore _Atomic on register variables, since their addresses can't 1995 be taken so (a) atomicity is irrelevant and (b) the normal atomic 1996 sequences wouldn't work. Ignore _Atomic on structures containing 1997 bit-fields, since accessing elements of atomic structures or 1998 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if 1999 it's undefined at translation time or execution time, and the 2000 normal atomic sequences again wouldn't work. */ 2001 while (handled_component_p (expr)) 2002 { 2003 if (TREE_CODE (expr) == COMPONENT_REF 2004 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) 2005 return false; 2006 expr = TREE_OPERAND (expr, 0); 2007 } 2008 if (DECL_P (expr) && C_DECL_REGISTER (expr)) 2009 return false; 2010 return true; 2011} 2012 2013/* Convert expression EXP (location LOC) from lvalue to rvalue, 2014 including converting functions and arrays to pointers if CONVERT_P. 2015 If READ_P, also mark the expression as having been read. */ 2016 2017struct c_expr 2018convert_lvalue_to_rvalue (location_t loc, struct c_expr exp, 2019 bool convert_p, bool read_p) 2020{ 2021 if (read_p) 2022 mark_exp_read (exp.value); 2023 if (convert_p) 2024 exp = default_function_array_conversion (loc, exp); 2025 if (really_atomic_lvalue (exp.value)) 2026 { 2027 vec<tree, va_gc> *params; 2028 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call; 2029 tree expr_type = TREE_TYPE (exp.value); 2030 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0); 2031 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST); 2032 2033 gcc_assert (TYPE_ATOMIC (expr_type)); 2034 2035 /* Expansion of a generic atomic load may require an addition 2036 element, so allocate enough to prevent a resize. */ 2037 vec_alloc (params, 4); 2038 2039 /* Remove the qualifiers for the rest of the expressions and 2040 create the VAL temp variable to hold the RHS. */ 2041 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED); 2042 tmp = create_tmp_var (nonatomic_type); 2043 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0); 2044 TREE_ADDRESSABLE (tmp) = 1; 2045 TREE_NO_WARNING (tmp) = 1; 2046 2047 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */ 2048 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD); 2049 params->quick_push (expr_addr); 2050 params->quick_push (tmp_addr); 2051 params->quick_push (seq_cst); 2052 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); 2053 2054 /* EXPR is always read. */ 2055 mark_exp_read (exp.value); 2056 2057 /* Return tmp which contains the value loaded. */ 2058 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp); 2059 } 2060 return exp; 2061} 2062 2063/* EXP is an expression of integer type. Apply the integer promotions 2064 to it and return the promoted value. */ 2065 2066tree 2067perform_integral_promotions (tree exp) 2068{ 2069 tree type = TREE_TYPE (exp); 2070 enum tree_code code = TREE_CODE (type); 2071 2072 gcc_assert (INTEGRAL_TYPE_P (type)); 2073 2074 /* Normally convert enums to int, 2075 but convert wide enums to something wider. */ 2076 if (code == ENUMERAL_TYPE) 2077 { 2078 type = c_common_type_for_size (MAX (TYPE_PRECISION (type), 2079 TYPE_PRECISION (integer_type_node)), 2080 ((TYPE_PRECISION (type) 2081 >= TYPE_PRECISION (integer_type_node)) 2082 && TYPE_UNSIGNED (type))); 2083 2084 return convert (type, exp); 2085 } 2086 2087 /* ??? This should no longer be needed now bit-fields have their 2088 proper types. */ 2089 if (TREE_CODE (exp) == COMPONENT_REF 2090 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)) 2091 /* If it's thinner than an int, promote it like a 2092 c_promoting_integer_type_p, otherwise leave it alone. */ 2093 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), 2094 TYPE_PRECISION (integer_type_node))) 2095 return convert (integer_type_node, exp); 2096 2097 if (c_promoting_integer_type_p (type)) 2098 { 2099 /* Preserve unsignedness if not really getting any wider. */ 2100 if (TYPE_UNSIGNED (type) 2101 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) 2102 return convert (unsigned_type_node, exp); 2103 2104 return convert (integer_type_node, exp); 2105 } 2106 2107 return exp; 2108} 2109 2110 2111/* Perform default promotions for C data used in expressions. 2112 Enumeral types or short or char are converted to int. 2113 In addition, manifest constants symbols are replaced by their values. */ 2114 2115tree 2116default_conversion (tree exp) 2117{ 2118 tree orig_exp; 2119 tree type = TREE_TYPE (exp); 2120 enum tree_code code = TREE_CODE (type); 2121 tree promoted_type; 2122 2123 mark_exp_read (exp); 2124 2125 /* Functions and arrays have been converted during parsing. */ 2126 gcc_assert (code != FUNCTION_TYPE); 2127 if (code == ARRAY_TYPE) 2128 return exp; 2129 2130 /* Constants can be used directly unless they're not loadable. */ 2131 if (TREE_CODE (exp) == CONST_DECL) 2132 exp = DECL_INITIAL (exp); 2133 2134 /* Strip no-op conversions. */ 2135 orig_exp = exp; 2136 STRIP_TYPE_NOPS (exp); 2137 2138 if (TREE_NO_WARNING (orig_exp)) 2139 TREE_NO_WARNING (exp) = 1; 2140 2141 if (code == VOID_TYPE) 2142 { 2143 error_at (EXPR_LOC_OR_LOC (exp, input_location), 2144 "void value not ignored as it ought to be"); 2145 return error_mark_node; 2146 } 2147 2148 exp = require_complete_type (exp); 2149 if (exp == error_mark_node) 2150 return error_mark_node; 2151 2152 promoted_type = targetm.promoted_type (type); 2153 if (promoted_type) 2154 return convert (promoted_type, exp); 2155 2156 if (INTEGRAL_TYPE_P (type)) 2157 return perform_integral_promotions (exp); 2158 2159 return exp; 2160} 2161 2162/* Look up COMPONENT in a structure or union TYPE. 2163 2164 If the component name is not found, returns NULL_TREE. Otherwise, 2165 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL 2166 stepping down the chain to the component, which is in the last 2167 TREE_VALUE of the list. Normally the list is of length one, but if 2168 the component is embedded within (nested) anonymous structures or 2169 unions, the list steps down the chain to the component. */ 2170 2171static tree 2172lookup_field (tree type, tree component) 2173{ 2174 tree field; 2175 2176 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers 2177 to the field elements. Use a binary search on this array to quickly 2178 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC 2179 will always be set for structures which have many elements. */ 2180 2181 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s) 2182 { 2183 int bot, top, half; 2184 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0]; 2185 2186 field = TYPE_FIELDS (type); 2187 bot = 0; 2188 top = TYPE_LANG_SPECIFIC (type)->s->len; 2189 while (top - bot > 1) 2190 { 2191 half = (top - bot + 1) >> 1; 2192 field = field_array[bot+half]; 2193 2194 if (DECL_NAME (field) == NULL_TREE) 2195 { 2196 /* Step through all anon unions in linear fashion. */ 2197 while (DECL_NAME (field_array[bot]) == NULL_TREE) 2198 { 2199 field = field_array[bot++]; 2200 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE 2201 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) 2202 { 2203 tree anon = lookup_field (TREE_TYPE (field), component); 2204 2205 if (anon) 2206 return tree_cons (NULL_TREE, field, anon); 2207 2208 /* The Plan 9 compiler permits referring 2209 directly to an anonymous struct/union field 2210 using a typedef name. */ 2211 if (flag_plan9_extensions 2212 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE 2213 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field))) 2214 == TYPE_DECL) 2215 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) 2216 == component)) 2217 break; 2218 } 2219 } 2220 2221 /* Entire record is only anon unions. */ 2222 if (bot > top) 2223 return NULL_TREE; 2224 2225 /* Restart the binary search, with new lower bound. */ 2226 continue; 2227 } 2228 2229 if (DECL_NAME (field) == component) 2230 break; 2231 if (DECL_NAME (field) < component) 2232 bot += half; 2233 else 2234 top = bot + half; 2235 } 2236 2237 if (DECL_NAME (field_array[bot]) == component) 2238 field = field_array[bot]; 2239 else if (DECL_NAME (field) != component) 2240 return NULL_TREE; 2241 } 2242 else 2243 { 2244 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 2245 { 2246 if (DECL_NAME (field) == NULL_TREE 2247 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE 2248 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)) 2249 { 2250 tree anon = lookup_field (TREE_TYPE (field), component); 2251 2252 if (anon) 2253 return tree_cons (NULL_TREE, field, anon); 2254 2255 /* The Plan 9 compiler permits referring directly to an 2256 anonymous struct/union field using a typedef 2257 name. */ 2258 if (flag_plan9_extensions 2259 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE 2260 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL 2261 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) 2262 == component)) 2263 break; 2264 } 2265 2266 if (DECL_NAME (field) == component) 2267 break; 2268 } 2269 2270 if (field == NULL_TREE) 2271 return NULL_TREE; 2272 } 2273 2274 return tree_cons (NULL_TREE, field, NULL_TREE); 2275} 2276 2277/* Make an expression to refer to the COMPONENT field of structure or 2278 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the 2279 location of the COMPONENT_REF. */ 2280 2281tree 2282build_component_ref (location_t loc, tree datum, tree component) 2283{ 2284 tree type = TREE_TYPE (datum); 2285 enum tree_code code = TREE_CODE (type); 2286 tree field = NULL; 2287 tree ref; 2288 bool datum_lvalue = lvalue_p (datum); 2289 2290 if (!objc_is_public (datum, component)) 2291 return error_mark_node; 2292 2293 /* Detect Objective-C property syntax object.property. */ 2294 if (c_dialect_objc () 2295 && (ref = objc_maybe_build_component_ref (datum, component))) 2296 return ref; 2297 2298 /* See if there is a field or component with name COMPONENT. */ 2299 2300 if (code == RECORD_TYPE || code == UNION_TYPE) 2301 { 2302 if (!COMPLETE_TYPE_P (type)) 2303 { 2304 c_incomplete_type_error (NULL_TREE, type); 2305 return error_mark_node; 2306 } 2307 2308 field = lookup_field (type, component); 2309 2310 if (!field) 2311 { 2312 error_at (loc, "%qT has no member named %qE", type, component); 2313 return error_mark_node; 2314 } 2315 2316 /* Chain the COMPONENT_REFs if necessary down to the FIELD. 2317 This might be better solved in future the way the C++ front 2318 end does it - by giving the anonymous entities each a 2319 separate name and type, and then have build_component_ref 2320 recursively call itself. We can't do that here. */ 2321 do 2322 { 2323 tree subdatum = TREE_VALUE (field); 2324 int quals; 2325 tree subtype; 2326 bool use_datum_quals; 2327 2328 if (TREE_TYPE (subdatum) == error_mark_node) 2329 return error_mark_node; 2330 2331 /* If this is an rvalue, it does not have qualifiers in C 2332 standard terms and we must avoid propagating such 2333 qualifiers down to a non-lvalue array that is then 2334 converted to a pointer. */ 2335 use_datum_quals = (datum_lvalue 2336 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE); 2337 2338 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum))); 2339 if (use_datum_quals) 2340 quals |= TYPE_QUALS (TREE_TYPE (datum)); 2341 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals); 2342 2343 ref = build3 (COMPONENT_REF, subtype, datum, subdatum, 2344 NULL_TREE); 2345 SET_EXPR_LOCATION (ref, loc); 2346 if (TREE_READONLY (subdatum) 2347 || (use_datum_quals && TREE_READONLY (datum))) 2348 TREE_READONLY (ref) = 1; 2349 if (TREE_THIS_VOLATILE (subdatum) 2350 || (use_datum_quals && TREE_THIS_VOLATILE (datum))) 2351 TREE_THIS_VOLATILE (ref) = 1; 2352 2353 if (TREE_DEPRECATED (subdatum)) 2354 warn_deprecated_use (subdatum, NULL_TREE); 2355 2356 datum = ref; 2357 2358 field = TREE_CHAIN (field); 2359 } 2360 while (field); 2361 2362 return ref; 2363 } 2364 else if (code != ERROR_MARK) 2365 error_at (loc, 2366 "request for member %qE in something not a structure or union", 2367 component); 2368 2369 return error_mark_node; 2370} 2371 2372/* Given an expression PTR for a pointer, return an expression 2373 for the value pointed to. 2374 ERRORSTRING is the name of the operator to appear in error messages. 2375 2376 LOC is the location to use for the generated tree. */ 2377 2378tree 2379build_indirect_ref (location_t loc, tree ptr, ref_operator errstring) 2380{ 2381 tree pointer = default_conversion (ptr); 2382 tree type = TREE_TYPE (pointer); 2383 tree ref; 2384 2385 if (TREE_CODE (type) == POINTER_TYPE) 2386 { 2387 if (CONVERT_EXPR_P (pointer) 2388 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR) 2389 { 2390 /* If a warning is issued, mark it to avoid duplicates from 2391 the backend. This only needs to be done at 2392 warn_strict_aliasing > 2. */ 2393 if (warn_strict_aliasing > 2) 2394 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)), 2395 type, TREE_OPERAND (pointer, 0))) 2396 TREE_NO_WARNING (pointer) = 1; 2397 } 2398 2399 if (TREE_CODE (pointer) == ADDR_EXPR 2400 && (TREE_TYPE (TREE_OPERAND (pointer, 0)) 2401 == TREE_TYPE (type))) 2402 { 2403 ref = TREE_OPERAND (pointer, 0); 2404 protected_set_expr_location (ref, loc); 2405 return ref; 2406 } 2407 else 2408 { 2409 tree t = TREE_TYPE (type); 2410 2411 ref = build1 (INDIRECT_REF, t, pointer); 2412 2413 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE) 2414 { 2415 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr))) 2416 { 2417 error_at (loc, "dereferencing pointer to incomplete type " 2418 "%qT", t); 2419 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1; 2420 } 2421 return error_mark_node; 2422 } 2423 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0) 2424 warning_at (loc, 0, "dereferencing %<void *%> pointer"); 2425 2426 /* We *must* set TREE_READONLY when dereferencing a pointer to const, 2427 so that we get the proper error message if the result is used 2428 to assign to. Also, &* is supposed to be a no-op. 2429 And ANSI C seems to specify that the type of the result 2430 should be the const type. */ 2431 /* A de-reference of a pointer to const is not a const. It is valid 2432 to change it via some other pointer. */ 2433 TREE_READONLY (ref) = TYPE_READONLY (t); 2434 TREE_SIDE_EFFECTS (ref) 2435 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer); 2436 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); 2437 protected_set_expr_location (ref, loc); 2438 return ref; 2439 } 2440 } 2441 else if (TREE_CODE (pointer) != ERROR_MARK) 2442 invalid_indirection_error (loc, type, errstring); 2443 2444 return error_mark_node; 2445} 2446 2447/* This handles expressions of the form "a[i]", which denotes 2448 an array reference. 2449 2450 This is logically equivalent in C to *(a+i), but we may do it differently. 2451 If A is a variable or a member, we generate a primitive ARRAY_REF. 2452 This avoids forcing the array out of registers, and can work on 2453 arrays that are not lvalues (for example, members of structures returned 2454 by functions). 2455 2456 For vector types, allow vector[i] but not i[vector], and create 2457 *(((type*)&vectortype) + i) for the expression. 2458 2459 LOC is the location to use for the returned expression. */ 2460 2461tree 2462build_array_ref (location_t loc, tree array, tree index) 2463{ 2464 tree ret; 2465 bool swapped = false; 2466 if (TREE_TYPE (array) == error_mark_node 2467 || TREE_TYPE (index) == error_mark_node) 2468 return error_mark_node; 2469 2470 if (flag_cilkplus && contains_array_notation_expr (index)) 2471 { 2472 size_t rank = 0; 2473 if (!find_rank (loc, index, index, true, &rank)) 2474 return error_mark_node; 2475 if (rank > 1) 2476 { 2477 error_at (loc, "rank of the array's index is greater than 1"); 2478 return error_mark_node; 2479 } 2480 } 2481 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE 2482 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE 2483 /* Allow vector[index] but not index[vector]. */ 2484 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE) 2485 { 2486 tree temp; 2487 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE 2488 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE) 2489 { 2490 error_at (loc, 2491 "subscripted value is neither array nor pointer nor vector"); 2492 2493 return error_mark_node; 2494 } 2495 temp = array; 2496 array = index; 2497 index = temp; 2498 swapped = true; 2499 } 2500 2501 if (!INTEGRAL_TYPE_P (TREE_TYPE (index))) 2502 { 2503 error_at (loc, "array subscript is not an integer"); 2504 return error_mark_node; 2505 } 2506 2507 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE) 2508 { 2509 error_at (loc, "subscripted value is pointer to function"); 2510 return error_mark_node; 2511 } 2512 2513 /* ??? Existing practice has been to warn only when the char 2514 index is syntactically the index, not for char[array]. */ 2515 if (!swapped) 2516 warn_array_subscript_with_type_char (loc, index); 2517 2518 /* Apply default promotions *after* noticing character types. */ 2519 index = default_conversion (index); 2520 if (index == error_mark_node) 2521 return error_mark_node; 2522 2523 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE); 2524 2525 bool non_lvalue 2526 = convert_vector_to_pointer_for_subscript (loc, &array, index); 2527 2528 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) 2529 { 2530 tree rval, type; 2531 2532 /* An array that is indexed by a non-constant 2533 cannot be stored in a register; we must be able to do 2534 address arithmetic on its address. 2535 Likewise an array of elements of variable size. */ 2536 if (TREE_CODE (index) != INTEGER_CST 2537 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) 2538 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) 2539 { 2540 if (!c_mark_addressable (array)) 2541 return error_mark_node; 2542 } 2543 /* An array that is indexed by a constant value which is not within 2544 the array bounds cannot be stored in a register either; because we 2545 would get a crash in store_bit_field/extract_bit_field when trying 2546 to access a non-existent part of the register. */ 2547 if (TREE_CODE (index) == INTEGER_CST 2548 && TYPE_DOMAIN (TREE_TYPE (array)) 2549 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array)))) 2550 { 2551 if (!c_mark_addressable (array)) 2552 return error_mark_node; 2553 } 2554 2555 if (pedantic || warn_c90_c99_compat) 2556 { 2557 tree foo = array; 2558 while (TREE_CODE (foo) == COMPONENT_REF) 2559 foo = TREE_OPERAND (foo, 0); 2560 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo)) 2561 pedwarn (loc, OPT_Wpedantic, 2562 "ISO C forbids subscripting %<register%> array"); 2563 else if (!lvalue_p (foo)) 2564 pedwarn_c90 (loc, OPT_Wpedantic, 2565 "ISO C90 forbids subscripting non-lvalue " 2566 "array"); 2567 } 2568 2569 type = TREE_TYPE (TREE_TYPE (array)); 2570 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE); 2571 /* Array ref is const/volatile if the array elements are 2572 or if the array is. */ 2573 TREE_READONLY (rval) 2574 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) 2575 | TREE_READONLY (array)); 2576 TREE_SIDE_EFFECTS (rval) 2577 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) 2578 | TREE_SIDE_EFFECTS (array)); 2579 TREE_THIS_VOLATILE (rval) 2580 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) 2581 /* This was added by rms on 16 Nov 91. 2582 It fixes vol struct foo *a; a->elts[1] 2583 in an inline function. 2584 Hope it doesn't break something else. */ 2585 | TREE_THIS_VOLATILE (array)); 2586 ret = require_complete_type (rval); 2587 protected_set_expr_location (ret, loc); 2588 if (non_lvalue) 2589 ret = non_lvalue_loc (loc, ret); 2590 return ret; 2591 } 2592 else 2593 { 2594 tree ar = default_conversion (array); 2595 2596 if (ar == error_mark_node) 2597 return ar; 2598 2599 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE); 2600 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE); 2601 2602 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar, 2603 index, 0), 2604 RO_ARRAY_INDEXING); 2605 if (non_lvalue) 2606 ret = non_lvalue_loc (loc, ret); 2607 return ret; 2608 } 2609} 2610 2611/* Build an external reference to identifier ID. FUN indicates 2612 whether this will be used for a function call. LOC is the source 2613 location of the identifier. This sets *TYPE to the type of the 2614 identifier, which is not the same as the type of the returned value 2615 for CONST_DECLs defined as enum constants. If the type of the 2616 identifier is not available, *TYPE is set to NULL. */ 2617tree 2618build_external_ref (location_t loc, tree id, int fun, tree *type) 2619{ 2620 tree ref; 2621 tree decl = lookup_name (id); 2622 2623 /* In Objective-C, an instance variable (ivar) may be preferred to 2624 whatever lookup_name() found. */ 2625 decl = objc_lookup_ivar (decl, id); 2626 2627 *type = NULL; 2628 if (decl && decl != error_mark_node) 2629 { 2630 ref = decl; 2631 *type = TREE_TYPE (ref); 2632 } 2633 else if (fun) 2634 /* Implicit function declaration. */ 2635 ref = implicitly_declare (loc, id); 2636 else if (decl == error_mark_node) 2637 /* Don't complain about something that's already been 2638 complained about. */ 2639 return error_mark_node; 2640 else 2641 { 2642 undeclared_variable (loc, id); 2643 return error_mark_node; 2644 } 2645 2646 if (TREE_TYPE (ref) == error_mark_node) 2647 return error_mark_node; 2648 2649 if (TREE_DEPRECATED (ref)) 2650 warn_deprecated_use (ref, NULL_TREE); 2651 2652 /* Recursive call does not count as usage. */ 2653 if (ref != current_function_decl) 2654 { 2655 TREE_USED (ref) = 1; 2656 } 2657 2658 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof) 2659 { 2660 if (!in_sizeof && !in_typeof) 2661 C_DECL_USED (ref) = 1; 2662 else if (DECL_INITIAL (ref) == 0 2663 && DECL_EXTERNAL (ref) 2664 && !TREE_PUBLIC (ref)) 2665 record_maybe_used_decl (ref); 2666 } 2667 2668 if (TREE_CODE (ref) == CONST_DECL) 2669 { 2670 used_types_insert (TREE_TYPE (ref)); 2671 2672 if (warn_cxx_compat 2673 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE 2674 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref))) 2675 { 2676 warning_at (loc, OPT_Wc___compat, 2677 ("enum constant defined in struct or union " 2678 "is not visible in C++")); 2679 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here"); 2680 } 2681 2682 ref = DECL_INITIAL (ref); 2683 TREE_CONSTANT (ref) = 1; 2684 } 2685 else if (current_function_decl != 0 2686 && !DECL_FILE_SCOPE_P (current_function_decl) 2687 && (TREE_CODE (ref) == VAR_DECL 2688 || TREE_CODE (ref) == PARM_DECL 2689 || TREE_CODE (ref) == FUNCTION_DECL)) 2690 { 2691 tree context = decl_function_context (ref); 2692 2693 if (context != 0 && context != current_function_decl) 2694 DECL_NONLOCAL (ref) = 1; 2695 } 2696 /* C99 6.7.4p3: An inline definition of a function with external 2697 linkage ... shall not contain a reference to an identifier with 2698 internal linkage. */ 2699 else if (current_function_decl != 0 2700 && DECL_DECLARED_INLINE_P (current_function_decl) 2701 && DECL_EXTERNAL (current_function_decl) 2702 && VAR_OR_FUNCTION_DECL_P (ref) 2703 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref)) 2704 && ! TREE_PUBLIC (ref) 2705 && DECL_CONTEXT (ref) != current_function_decl) 2706 record_inline_static (loc, current_function_decl, ref, 2707 csi_internal); 2708 2709 return ref; 2710} 2711 2712/* Record details of decls possibly used inside sizeof or typeof. */ 2713struct maybe_used_decl 2714{ 2715 /* The decl. */ 2716 tree decl; 2717 /* The level seen at (in_sizeof + in_typeof). */ 2718 int level; 2719 /* The next one at this level or above, or NULL. */ 2720 struct maybe_used_decl *next; 2721}; 2722 2723static struct maybe_used_decl *maybe_used_decls; 2724 2725/* Record that DECL, an undefined static function reference seen 2726 inside sizeof or typeof, might be used if the operand of sizeof is 2727 a VLA type or the operand of typeof is a variably modified 2728 type. */ 2729 2730static void 2731record_maybe_used_decl (tree decl) 2732{ 2733 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl); 2734 t->decl = decl; 2735 t->level = in_sizeof + in_typeof; 2736 t->next = maybe_used_decls; 2737 maybe_used_decls = t; 2738} 2739 2740/* Pop the stack of decls possibly used inside sizeof or typeof. If 2741 USED is false, just discard them. If it is true, mark them used 2742 (if no longer inside sizeof or typeof) or move them to the next 2743 level up (if still inside sizeof or typeof). */ 2744 2745void 2746pop_maybe_used (bool used) 2747{ 2748 struct maybe_used_decl *p = maybe_used_decls; 2749 int cur_level = in_sizeof + in_typeof; 2750 while (p && p->level > cur_level) 2751 { 2752 if (used) 2753 { 2754 if (cur_level == 0) 2755 C_DECL_USED (p->decl) = 1; 2756 else 2757 p->level = cur_level; 2758 } 2759 p = p->next; 2760 } 2761 if (!used || cur_level == 0) 2762 maybe_used_decls = p; 2763} 2764 2765/* Return the result of sizeof applied to EXPR. */ 2766 2767struct c_expr 2768c_expr_sizeof_expr (location_t loc, struct c_expr expr) 2769{ 2770 struct c_expr ret; 2771 if (expr.value == error_mark_node) 2772 { 2773 ret.value = error_mark_node; 2774 ret.original_code = ERROR_MARK; 2775 ret.original_type = NULL; 2776 pop_maybe_used (false); 2777 } 2778 else 2779 { 2780 bool expr_const_operands = true; 2781 2782 if (TREE_CODE (expr.value) == PARM_DECL 2783 && C_ARRAY_PARAMETER (expr.value)) 2784 { 2785 if (warning_at (loc, OPT_Wsizeof_array_argument, 2786 "%<sizeof%> on array function parameter %qE will " 2787 "return size of %qT", expr.value, 2788 expr.original_type)) 2789 inform (DECL_SOURCE_LOCATION (expr.value), "declared here"); 2790 } 2791 tree folded_expr = c_fully_fold (expr.value, require_constant_value, 2792 &expr_const_operands); 2793 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr)); 2794 c_last_sizeof_arg = expr.value; 2795 ret.original_code = SIZEOF_EXPR; 2796 ret.original_type = NULL; 2797 if (c_vla_type_p (TREE_TYPE (folded_expr))) 2798 { 2799 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */ 2800 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), 2801 folded_expr, ret.value); 2802 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands; 2803 SET_EXPR_LOCATION (ret.value, loc); 2804 } 2805 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr))); 2806 } 2807 return ret; 2808} 2809 2810/* Return the result of sizeof applied to T, a structure for the type 2811 name passed to sizeof (rather than the type itself). LOC is the 2812 location of the original expression. */ 2813 2814struct c_expr 2815c_expr_sizeof_type (location_t loc, struct c_type_name *t) 2816{ 2817 tree type; 2818 struct c_expr ret; 2819 tree type_expr = NULL_TREE; 2820 bool type_expr_const = true; 2821 type = groktypename (t, &type_expr, &type_expr_const); 2822 ret.value = c_sizeof (loc, type); 2823 c_last_sizeof_arg = type; 2824 ret.original_code = SIZEOF_EXPR; 2825 ret.original_type = NULL; 2826 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST) 2827 && c_vla_type_p (type)) 2828 { 2829 /* If the type is a [*] array, it is a VLA but is represented as 2830 having a size of zero. In such a case we must ensure that 2831 the result of sizeof does not get folded to a constant by 2832 c_fully_fold, because if the size is evaluated the result is 2833 not constant and so constraints on zero or negative size 2834 arrays must not be applied when this sizeof call is inside 2835 another array declarator. */ 2836 if (!type_expr) 2837 type_expr = integer_zero_node; 2838 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), 2839 type_expr, ret.value); 2840 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const; 2841 } 2842 pop_maybe_used (type != error_mark_node 2843 ? C_TYPE_VARIABLE_SIZE (type) : false); 2844 return ret; 2845} 2846 2847/* Build a function call to function FUNCTION with parameters PARAMS. 2848 The function call is at LOC. 2849 PARAMS is a list--a chain of TREE_LIST nodes--in which the 2850 TREE_VALUE of each node is a parameter-expression. 2851 FUNCTION's data type may be a function type or a pointer-to-function. */ 2852 2853tree 2854build_function_call (location_t loc, tree function, tree params) 2855{ 2856 vec<tree, va_gc> *v; 2857 tree ret; 2858 2859 vec_alloc (v, list_length (params)); 2860 for (; params; params = TREE_CHAIN (params)) 2861 v->quick_push (TREE_VALUE (params)); 2862 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL); 2863 vec_free (v); 2864 return ret; 2865} 2866 2867/* Give a note about the location of the declaration of DECL. */ 2868 2869static void inform_declaration (tree decl) 2870{ 2871 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl))) 2872 inform (DECL_SOURCE_LOCATION (decl), "declared here"); 2873} 2874 2875/* Build a function call to function FUNCTION with parameters PARAMS. 2876 ORIGTYPES, if not NULL, is a vector of types; each element is 2877 either NULL or the original type of the corresponding element in 2878 PARAMS. The original type may differ from TREE_TYPE of the 2879 parameter for enums. FUNCTION's data type may be a function type 2880 or pointer-to-function. This function changes the elements of 2881 PARAMS. */ 2882 2883tree 2884build_function_call_vec (location_t loc, vec<location_t> arg_loc, 2885 tree function, vec<tree, va_gc> *params, 2886 vec<tree, va_gc> *origtypes) 2887{ 2888 tree fntype, fundecl = 0; 2889 tree name = NULL_TREE, result; 2890 tree tem; 2891 int nargs; 2892 tree *argarray; 2893 2894 2895 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ 2896 STRIP_TYPE_NOPS (function); 2897 2898 /* Convert anything with function type to a pointer-to-function. */ 2899 if (TREE_CODE (function) == FUNCTION_DECL) 2900 { 2901 name = DECL_NAME (function); 2902 2903 if (flag_tm) 2904 tm_malloc_replacement (function); 2905 fundecl = function; 2906 /* Atomic functions have type checking/casting already done. They are 2907 often rewritten and don't match the original parameter list. */ 2908 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9)) 2909 origtypes = NULL; 2910 2911 if (flag_cilkplus 2912 && is_cilkplus_reduce_builtin (function)) 2913 origtypes = NULL; 2914 } 2915 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE) 2916 function = function_to_pointer_conversion (loc, function); 2917 2918 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF 2919 expressions, like those used for ObjC messenger dispatches. */ 2920 if (params && !params->is_empty ()) 2921 function = objc_rewrite_function_call (function, (*params)[0]); 2922 2923 function = c_fully_fold (function, false, NULL); 2924 2925 fntype = TREE_TYPE (function); 2926 2927 if (TREE_CODE (fntype) == ERROR_MARK) 2928 return error_mark_node; 2929 2930 if (!(TREE_CODE (fntype) == POINTER_TYPE 2931 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) 2932 { 2933 if (!flag_diagnostics_show_caret) 2934 error_at (loc, 2935 "called object %qE is not a function or function pointer", 2936 function); 2937 else if (DECL_P (function)) 2938 { 2939 error_at (loc, 2940 "called object %qD is not a function or function pointer", 2941 function); 2942 inform_declaration (function); 2943 } 2944 else 2945 error_at (loc, 2946 "called object is not a function or function pointer"); 2947 return error_mark_node; 2948 } 2949 2950 if (fundecl && TREE_THIS_VOLATILE (fundecl)) 2951 current_function_returns_abnormally = 1; 2952 2953 /* fntype now gets the type of function pointed to. */ 2954 fntype = TREE_TYPE (fntype); 2955 2956 /* Convert the parameters to the types declared in the 2957 function prototype, or apply default promotions. */ 2958 2959 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params, 2960 origtypes, function, fundecl); 2961 if (nargs < 0) 2962 return error_mark_node; 2963 2964 /* Check that the function is called through a compatible prototype. 2965 If it is not, warn. */ 2966 if (CONVERT_EXPR_P (function) 2967 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR 2968 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL 2969 && !comptypes (fntype, TREE_TYPE (tem))) 2970 { 2971 tree return_type = TREE_TYPE (fntype); 2972 2973 /* This situation leads to run-time undefined behavior. We can't, 2974 therefore, simply error unless we can prove that all possible 2975 executions of the program must execute the code. */ 2976 warning_at (loc, 0, "function called through a non-compatible type"); 2977 2978 if (VOID_TYPE_P (return_type) 2979 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED) 2980 pedwarn (loc, 0, 2981 "function with qualified void return type called"); 2982 } 2983 2984 argarray = vec_safe_address (params); 2985 2986 /* Check that arguments to builtin functions match the expectations. */ 2987 if (fundecl 2988 && DECL_BUILT_IN (fundecl) 2989 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL 2990 && !check_builtin_function_arguments (fundecl, nargs, argarray)) 2991 return error_mark_node; 2992 2993 /* Check that the arguments to the function are valid. */ 2994 check_function_arguments (fntype, nargs, argarray); 2995 2996 if (name != NULL_TREE 2997 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10)) 2998 { 2999 if (require_constant_value) 3000 result = 3001 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype), 3002 function, nargs, argarray); 3003 else 3004 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype), 3005 function, nargs, argarray); 3006 if (TREE_CODE (result) == NOP_EXPR 3007 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST) 3008 STRIP_TYPE_NOPS (result); 3009 } 3010 else 3011 result = build_call_array_loc (loc, TREE_TYPE (fntype), 3012 function, nargs, argarray); 3013 3014 if (VOID_TYPE_P (TREE_TYPE (result))) 3015 { 3016 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED) 3017 pedwarn (loc, 0, 3018 "function with qualified void return type called"); 3019 return result; 3020 } 3021 return require_complete_type (result); 3022} 3023 3024/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */ 3025 3026tree 3027c_build_function_call_vec (location_t loc, vec<location_t> arg_loc, 3028 tree function, vec<tree, va_gc> *params, 3029 vec<tree, va_gc> *origtypes) 3030{ 3031 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ 3032 STRIP_TYPE_NOPS (function); 3033 3034 /* Convert anything with function type to a pointer-to-function. */ 3035 if (TREE_CODE (function) == FUNCTION_DECL) 3036 { 3037 /* Implement type-directed function overloading for builtins. 3038 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin 3039 handle all the type checking. The result is a complete expression 3040 that implements this function call. */ 3041 tree tem = resolve_overloaded_builtin (loc, function, params); 3042 if (tem) 3043 return tem; 3044 } 3045 return build_function_call_vec (loc, arg_loc, function, params, origtypes); 3046} 3047 3048/* Convert the argument expressions in the vector VALUES 3049 to the types in the list TYPELIST. 3050 3051 If TYPELIST is exhausted, or when an element has NULL as its type, 3052 perform the default conversions. 3053 3054 ORIGTYPES is the original types of the expressions in VALUES. This 3055 holds the type of enum values which have been converted to integral 3056 types. It may be NULL. 3057 3058 FUNCTION is a tree for the called function. It is used only for 3059 error messages, where it is formatted with %qE. 3060 3061 This is also where warnings about wrong number of args are generated. 3062 3063 ARG_LOC are locations of function arguments (if any). 3064 3065 Returns the actual number of arguments processed (which may be less 3066 than the length of VALUES in some error situations), or -1 on 3067 failure. */ 3068 3069static int 3070convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist, 3071 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes, 3072 tree function, tree fundecl) 3073{ 3074 tree typetail, val; 3075 unsigned int parmnum; 3076 bool error_args = false; 3077 const bool type_generic = fundecl 3078 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl))); 3079 bool type_generic_remove_excess_precision = false; 3080 tree selector; 3081 3082 /* Change pointer to function to the function itself for 3083 diagnostics. */ 3084 if (TREE_CODE (function) == ADDR_EXPR 3085 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) 3086 function = TREE_OPERAND (function, 0); 3087 3088 /* Handle an ObjC selector specially for diagnostics. */ 3089 selector = objc_message_selector (); 3090 3091 /* For type-generic built-in functions, determine whether excess 3092 precision should be removed (classification) or not 3093 (comparison). */ 3094 if (type_generic 3095 && DECL_BUILT_IN (fundecl) 3096 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL) 3097 { 3098 switch (DECL_FUNCTION_CODE (fundecl)) 3099 { 3100 case BUILT_IN_ISFINITE: 3101 case BUILT_IN_ISINF: 3102 case BUILT_IN_ISINF_SIGN: 3103 case BUILT_IN_ISNAN: 3104 case BUILT_IN_ISNORMAL: 3105 case BUILT_IN_FPCLASSIFY: 3106 type_generic_remove_excess_precision = true; 3107 break; 3108 3109 default: 3110 type_generic_remove_excess_precision = false; 3111 break; 3112 } 3113 } 3114 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl)) 3115 return vec_safe_length (values); 3116 3117 /* Scan the given expressions and types, producing individual 3118 converted arguments. */ 3119 3120 for (typetail = typelist, parmnum = 0; 3121 values && values->iterate (parmnum, &val); 3122 ++parmnum) 3123 { 3124 tree type = typetail ? TREE_VALUE (typetail) : 0; 3125 tree valtype = TREE_TYPE (val); 3126 tree rname = function; 3127 int argnum = parmnum + 1; 3128 const char *invalid_func_diag; 3129 bool excess_precision = false; 3130 bool npc; 3131 tree parmval; 3132 /* Some __atomic_* builtins have additional hidden argument at 3133 position 0. */ 3134 location_t ploc 3135 = !arg_loc.is_empty () && values->length () == arg_loc.length () 3136 ? expansion_point_location_if_in_system_header (arg_loc[parmnum]) 3137 : input_location; 3138 3139 if (type == void_type_node) 3140 { 3141 if (selector) 3142 error_at (loc, "too many arguments to method %qE", selector); 3143 else 3144 error_at (loc, "too many arguments to function %qE", function); 3145 inform_declaration (fundecl); 3146 return error_args ? -1 : (int) parmnum; 3147 } 3148 3149 if (selector && argnum > 2) 3150 { 3151 rname = selector; 3152 argnum -= 2; 3153 } 3154 3155 npc = null_pointer_constant_p (val); 3156 3157 /* If there is excess precision and a prototype, convert once to 3158 the required type rather than converting via the semantic 3159 type. Likewise without a prototype a float value represented 3160 as long double should be converted once to double. But for 3161 type-generic classification functions excess precision must 3162 be removed here. */ 3163 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR 3164 && (type || !type_generic || !type_generic_remove_excess_precision)) 3165 { 3166 val = TREE_OPERAND (val, 0); 3167 excess_precision = true; 3168 } 3169 val = c_fully_fold (val, false, NULL); 3170 STRIP_TYPE_NOPS (val); 3171 3172 val = require_complete_type (val); 3173 3174 if (type != 0) 3175 { 3176 /* Formal parm type is specified by a function prototype. */ 3177 3178 if (type == error_mark_node || !COMPLETE_TYPE_P (type)) 3179 { 3180 error_at (ploc, "type of formal parameter %d is incomplete", 3181 parmnum + 1); 3182 parmval = val; 3183 } 3184 else 3185 { 3186 tree origtype; 3187 3188 /* Optionally warn about conversions that 3189 differ from the default conversions. */ 3190 if (warn_traditional_conversion || warn_traditional) 3191 { 3192 unsigned int formal_prec = TYPE_PRECISION (type); 3193 3194 if (INTEGRAL_TYPE_P (type) 3195 && TREE_CODE (valtype) == REAL_TYPE) 3196 warning_at (ploc, OPT_Wtraditional_conversion, 3197 "passing argument %d of %qE as integer rather " 3198 "than floating due to prototype", 3199 argnum, rname); 3200 if (INTEGRAL_TYPE_P (type) 3201 && TREE_CODE (valtype) == COMPLEX_TYPE) 3202 warning_at (ploc, OPT_Wtraditional_conversion, 3203 "passing argument %d of %qE as integer rather " 3204 "than complex due to prototype", 3205 argnum, rname); 3206 else if (TREE_CODE (type) == COMPLEX_TYPE 3207 && TREE_CODE (valtype) == REAL_TYPE) 3208 warning_at (ploc, OPT_Wtraditional_conversion, 3209 "passing argument %d of %qE as complex rather " 3210 "than floating due to prototype", 3211 argnum, rname); 3212 else if (TREE_CODE (type) == REAL_TYPE 3213 && INTEGRAL_TYPE_P (valtype)) 3214 warning_at (ploc, OPT_Wtraditional_conversion, 3215 "passing argument %d of %qE as floating rather " 3216 "than integer due to prototype", 3217 argnum, rname); 3218 else if (TREE_CODE (type) == COMPLEX_TYPE 3219 && INTEGRAL_TYPE_P (valtype)) 3220 warning_at (ploc, OPT_Wtraditional_conversion, 3221 "passing argument %d of %qE as complex rather " 3222 "than integer due to prototype", 3223 argnum, rname); 3224 else if (TREE_CODE (type) == REAL_TYPE 3225 && TREE_CODE (valtype) == COMPLEX_TYPE) 3226 warning_at (ploc, OPT_Wtraditional_conversion, 3227 "passing argument %d of %qE as floating rather " 3228 "than complex due to prototype", 3229 argnum, rname); 3230 /* ??? At some point, messages should be written about 3231 conversions between complex types, but that's too messy 3232 to do now. */ 3233 else if (TREE_CODE (type) == REAL_TYPE 3234 && TREE_CODE (valtype) == REAL_TYPE) 3235 { 3236 /* Warn if any argument is passed as `float', 3237 since without a prototype it would be `double'. */ 3238 if (formal_prec == TYPE_PRECISION (float_type_node) 3239 && type != dfloat32_type_node) 3240 warning_at (ploc, 0, 3241 "passing argument %d of %qE as %<float%> " 3242 "rather than %<double%> due to prototype", 3243 argnum, rname); 3244 3245 /* Warn if mismatch between argument and prototype 3246 for decimal float types. Warn of conversions with 3247 binary float types and of precision narrowing due to 3248 prototype. */ 3249 else if (type != valtype 3250 && (type == dfloat32_type_node 3251 || type == dfloat64_type_node 3252 || type == dfloat128_type_node 3253 || valtype == dfloat32_type_node 3254 || valtype == dfloat64_type_node 3255 || valtype == dfloat128_type_node) 3256 && (formal_prec 3257 <= TYPE_PRECISION (valtype) 3258 || (type == dfloat128_type_node 3259 && (valtype 3260 != dfloat64_type_node 3261 && (valtype 3262 != dfloat32_type_node))) 3263 || (type == dfloat64_type_node 3264 && (valtype 3265 != dfloat32_type_node)))) 3266 warning_at (ploc, 0, 3267 "passing argument %d of %qE as %qT " 3268 "rather than %qT due to prototype", 3269 argnum, rname, type, valtype); 3270 3271 } 3272 /* Detect integer changing in width or signedness. 3273 These warnings are only activated with 3274 -Wtraditional-conversion, not with -Wtraditional. */ 3275 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type) 3276 && INTEGRAL_TYPE_P (valtype)) 3277 { 3278 tree would_have_been = default_conversion (val); 3279 tree type1 = TREE_TYPE (would_have_been); 3280 3281 if (TREE_CODE (type) == ENUMERAL_TYPE 3282 && (TYPE_MAIN_VARIANT (type) 3283 == TYPE_MAIN_VARIANT (valtype))) 3284 /* No warning if function asks for enum 3285 and the actual arg is that enum type. */ 3286 ; 3287 else if (formal_prec != TYPE_PRECISION (type1)) 3288 warning_at (ploc, OPT_Wtraditional_conversion, 3289 "passing argument %d of %qE " 3290 "with different width due to prototype", 3291 argnum, rname); 3292 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1)) 3293 ; 3294 /* Don't complain if the formal parameter type 3295 is an enum, because we can't tell now whether 3296 the value was an enum--even the same enum. */ 3297 else if (TREE_CODE (type) == ENUMERAL_TYPE) 3298 ; 3299 else if (TREE_CODE (val) == INTEGER_CST 3300 && int_fits_type_p (val, type)) 3301 /* Change in signedness doesn't matter 3302 if a constant value is unaffected. */ 3303 ; 3304 /* If the value is extended from a narrower 3305 unsigned type, it doesn't matter whether we 3306 pass it as signed or unsigned; the value 3307 certainly is the same either way. */ 3308 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type) 3309 && TYPE_UNSIGNED (valtype)) 3310 ; 3311 else if (TYPE_UNSIGNED (type)) 3312 warning_at (ploc, OPT_Wtraditional_conversion, 3313 "passing argument %d of %qE " 3314 "as unsigned due to prototype", 3315 argnum, rname); 3316 else 3317 warning_at (ploc, OPT_Wtraditional_conversion, 3318 "passing argument %d of %qE " 3319 "as signed due to prototype", 3320 argnum, rname); 3321 } 3322 } 3323 3324 /* Possibly restore an EXCESS_PRECISION_EXPR for the 3325 sake of better warnings from convert_and_check. */ 3326 if (excess_precision) 3327 val = build1 (EXCESS_PRECISION_EXPR, valtype, val); 3328 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum]; 3329 parmval = convert_for_assignment (loc, ploc, type, 3330 val, origtype, ic_argpass, 3331 npc, fundecl, function, 3332 parmnum + 1); 3333 3334 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0) 3335 && INTEGRAL_TYPE_P (type) 3336 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) 3337 parmval = default_conversion (parmval); 3338 } 3339 } 3340 else if (TREE_CODE (valtype) == REAL_TYPE 3341 && (TYPE_PRECISION (valtype) 3342 <= TYPE_PRECISION (double_type_node)) 3343 && TYPE_MAIN_VARIANT (valtype) != double_type_node 3344 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node 3345 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype))) 3346 { 3347 if (type_generic) 3348 parmval = val; 3349 else 3350 { 3351 /* Convert `float' to `double'. */ 3352 if (warn_double_promotion && !c_inhibit_evaluation_warnings) 3353 warning_at (ploc, OPT_Wdouble_promotion, 3354 "implicit conversion from %qT to %qT when passing " 3355 "argument to function", 3356 valtype, double_type_node); 3357 parmval = convert (double_type_node, val); 3358 } 3359 } 3360 else if (excess_precision && !type_generic) 3361 /* A "double" argument with excess precision being passed 3362 without a prototype or in variable arguments. */ 3363 parmval = convert (valtype, val); 3364 else if ((invalid_func_diag = 3365 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val))) 3366 { 3367 error (invalid_func_diag); 3368 return -1; 3369 } 3370 else 3371 /* Convert `short' and `char' to full-size `int'. */ 3372 parmval = default_conversion (val); 3373 3374 (*values)[parmnum] = parmval; 3375 if (parmval == error_mark_node) 3376 error_args = true; 3377 3378 if (typetail) 3379 typetail = TREE_CHAIN (typetail); 3380 } 3381 3382 gcc_assert (parmnum == vec_safe_length (values)); 3383 3384 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) 3385 { 3386 error_at (loc, "too few arguments to function %qE", function); 3387 inform_declaration (fundecl); 3388 return -1; 3389 } 3390 3391 return error_args ? -1 : (int) parmnum; 3392} 3393 3394/* This is the entry point used by the parser to build unary operators 3395 in the input. CODE, a tree_code, specifies the unary operator, and 3396 ARG is the operand. For unary plus, the C parser currently uses 3397 CONVERT_EXPR for code. 3398 3399 LOC is the location to use for the tree generated. 3400*/ 3401 3402struct c_expr 3403parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg) 3404{ 3405 struct c_expr result; 3406 3407 result.value = build_unary_op (loc, code, arg.value, 0); 3408 result.original_code = code; 3409 result.original_type = NULL; 3410 3411 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value)) 3412 overflow_warning (loc, result.value); 3413 3414 return result; 3415} 3416 3417/* This is the entry point used by the parser to build binary operators 3418 in the input. CODE, a tree_code, specifies the binary operator, and 3419 ARG1 and ARG2 are the operands. In addition to constructing the 3420 expression, we check for operands that were written with other binary 3421 operators in a way that is likely to confuse the user. 3422 3423 LOCATION is the location of the binary operator. */ 3424 3425struct c_expr 3426parser_build_binary_op (location_t location, enum tree_code code, 3427 struct c_expr arg1, struct c_expr arg2) 3428{ 3429 struct c_expr result; 3430 3431 enum tree_code code1 = arg1.original_code; 3432 enum tree_code code2 = arg2.original_code; 3433 tree type1 = (arg1.original_type 3434 ? arg1.original_type 3435 : TREE_TYPE (arg1.value)); 3436 tree type2 = (arg2.original_type 3437 ? arg2.original_type 3438 : TREE_TYPE (arg2.value)); 3439 3440 result.value = build_binary_op (location, code, 3441 arg1.value, arg2.value, 1); 3442 result.original_code = code; 3443 result.original_type = NULL; 3444 3445 if (TREE_CODE (result.value) == ERROR_MARK) 3446 return result; 3447 3448 if (location != UNKNOWN_LOCATION) 3449 protected_set_expr_location (result.value, location); 3450 3451 /* Check for cases such as x+y<<z which users are likely 3452 to misinterpret. */ 3453 if (warn_parentheses) 3454 warn_about_parentheses (location, code, code1, arg1.value, code2, 3455 arg2.value); 3456 3457 if (warn_logical_op) 3458 warn_logical_operator (location, code, TREE_TYPE (result.value), 3459 code1, arg1.value, code2, arg2.value); 3460 3461 if (warn_logical_not_paren 3462 && TREE_CODE_CLASS (code) == tcc_comparison 3463 && code1 == TRUTH_NOT_EXPR 3464 && code2 != TRUTH_NOT_EXPR 3465 /* Avoid warning for !!x == y. */ 3466 && (TREE_CODE (arg1.value) != NE_EXPR 3467 || !integer_zerop (TREE_OPERAND (arg1.value, 1)))) 3468 { 3469 /* Avoid warning for !b == y where b has _Bool type. */ 3470 tree t = integer_zero_node; 3471 if (TREE_CODE (arg1.value) == EQ_EXPR 3472 && integer_zerop (TREE_OPERAND (arg1.value, 1)) 3473 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node) 3474 { 3475 t = TREE_OPERAND (arg1.value, 0); 3476 do 3477 { 3478 if (TREE_TYPE (t) != integer_type_node) 3479 break; 3480 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR) 3481 t = C_MAYBE_CONST_EXPR_EXPR (t); 3482 else if (CONVERT_EXPR_P (t)) 3483 t = TREE_OPERAND (t, 0); 3484 else 3485 break; 3486 } 3487 while (1); 3488 } 3489 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE) 3490 warn_logical_not_parentheses (location, code, arg2.value); 3491 } 3492 3493 /* Warn about comparisons against string literals, with the exception 3494 of testing for equality or inequality of a string literal with NULL. */ 3495 if (code == EQ_EXPR || code == NE_EXPR) 3496 { 3497 if ((code1 == STRING_CST && !integer_zerop (arg2.value)) 3498 || (code2 == STRING_CST && !integer_zerop (arg1.value))) 3499 warning_at (location, OPT_Waddress, 3500 "comparison with string literal results in unspecified behavior"); 3501 } 3502 else if (TREE_CODE_CLASS (code) == tcc_comparison 3503 && (code1 == STRING_CST || code2 == STRING_CST)) 3504 warning_at (location, OPT_Waddress, 3505 "comparison with string literal results in unspecified behavior"); 3506 3507 if (TREE_OVERFLOW_P (result.value) 3508 && !TREE_OVERFLOW_P (arg1.value) 3509 && !TREE_OVERFLOW_P (arg2.value)) 3510 overflow_warning (location, result.value); 3511 3512 /* Warn about comparisons of different enum types. */ 3513 if (warn_enum_compare 3514 && TREE_CODE_CLASS (code) == tcc_comparison 3515 && TREE_CODE (type1) == ENUMERAL_TYPE 3516 && TREE_CODE (type2) == ENUMERAL_TYPE 3517 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2)) 3518 warning_at (location, OPT_Wenum_compare, 3519 "comparison between %qT and %qT", 3520 type1, type2); 3521 3522 return result; 3523} 3524 3525/* Return a tree for the difference of pointers OP0 and OP1. 3526 The resulting tree has type int. */ 3527 3528static tree 3529pointer_diff (location_t loc, tree op0, tree op1) 3530{ 3531 tree restype = ptrdiff_type_node; 3532 tree result, inttype; 3533 3534 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0))); 3535 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1))); 3536 tree target_type = TREE_TYPE (TREE_TYPE (op0)); 3537 tree orig_op1 = op1; 3538 3539 /* If the operands point into different address spaces, we need to 3540 explicitly convert them to pointers into the common address space 3541 before we can subtract the numerical address values. */ 3542 if (as0 != as1) 3543 { 3544 addr_space_t as_common; 3545 tree common_type; 3546 3547 /* Determine the common superset address space. This is guaranteed 3548 to exist because the caller verified that comp_target_types 3549 returned non-zero. */ 3550 if (!addr_space_superset (as0, as1, &as_common)) 3551 gcc_unreachable (); 3552 3553 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1)); 3554 op0 = convert (common_type, op0); 3555 op1 = convert (common_type, op1); 3556 } 3557 3558 /* Determine integer type to perform computations in. This will usually 3559 be the same as the result type (ptrdiff_t), but may need to be a wider 3560 type if pointers for the address space are wider than ptrdiff_t. */ 3561 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0))) 3562 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0); 3563 else 3564 inttype = restype; 3565 3566 if (TREE_CODE (target_type) == VOID_TYPE) 3567 pedwarn (loc, OPT_Wpointer_arith, 3568 "pointer of type %<void *%> used in subtraction"); 3569 if (TREE_CODE (target_type) == FUNCTION_TYPE) 3570 pedwarn (loc, OPT_Wpointer_arith, 3571 "pointer to a function used in subtraction"); 3572 3573 /* First do the subtraction as integers; 3574 then drop through to build the divide operator. 3575 Do not do default conversions on the minus operator 3576 in case restype is a short type. */ 3577 3578 op0 = build_binary_op (loc, 3579 MINUS_EXPR, convert (inttype, op0), 3580 convert (inttype, op1), 0); 3581 /* This generates an error if op1 is pointer to incomplete type. */ 3582 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1)))) 3583 error_at (loc, "arithmetic on pointer to an incomplete type"); 3584 3585 op1 = c_size_in_bytes (target_type); 3586 3587 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1))) 3588 error_at (loc, "arithmetic on pointer to an empty aggregate"); 3589 3590 /* Divide by the size, in easiest possible way. */ 3591 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype, 3592 op0, convert (inttype, op1)); 3593 3594 /* Convert to final result type if necessary. */ 3595 return convert (restype, result); 3596} 3597 3598/* Expand atomic compound assignments into an approriate sequence as 3599 specified by the C11 standard section 6.5.16.2. 3600 given 3601 _Atomic T1 E1 3602 T2 E2 3603 E1 op= E2 3604 3605 This sequence is used for all types for which these operations are 3606 supported. 3607 3608 In addition, built-in versions of the 'fe' prefixed routines may 3609 need to be invoked for floating point (real, complex or vector) when 3610 floating-point exceptions are supported. See 6.5.16.2 footnote 113. 3611 3612 T1 newval; 3613 T1 old; 3614 T1 *addr 3615 T2 val 3616 fenv_t fenv 3617 3618 addr = &E1; 3619 val = (E2); 3620 __atomic_load (addr, &old, SEQ_CST); 3621 feholdexcept (&fenv); 3622loop: 3623 newval = old op val; 3624 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST, 3625 SEQ_CST)) 3626 goto done; 3627 feclearexcept (FE_ALL_EXCEPT); 3628 goto loop: 3629done: 3630 feupdateenv (&fenv); 3631 3632 Also note that the compiler is simply issuing the generic form of 3633 the atomic operations. This requires temp(s) and has their address 3634 taken. The atomic processing is smart enough to figure out when the 3635 size of an object can utilize a lock-free version, and convert the 3636 built-in call to the appropriate lock-free routine. The optimizers 3637 will then dispose of any temps that are no longer required, and 3638 lock-free implementations are utilized as long as there is target 3639 support for the required size. 3640 3641 If the operator is NOP_EXPR, then this is a simple assignment, and 3642 an __atomic_store is issued to perform the assignment rather than 3643 the above loop. 3644 3645*/ 3646 3647/* Build an atomic assignment at LOC, expanding into the proper 3648 sequence to store LHS MODIFYCODE= RHS. Return a value representing 3649 the result of the operation, unless RETURN_OLD_P in which case 3650 return the old value of LHS (this is only for postincrement and 3651 postdecrement). */ 3652static tree 3653build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode, 3654 tree rhs, bool return_old_p) 3655{ 3656 tree fndecl, func_call; 3657 vec<tree, va_gc> *params; 3658 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr; 3659 tree old, old_addr; 3660 tree compound_stmt; 3661 tree stmt, goto_stmt; 3662 tree loop_label, loop_decl, done_label, done_decl; 3663 3664 tree lhs_type = TREE_TYPE (lhs); 3665 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0); 3666 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST); 3667 tree rhs_type = TREE_TYPE (rhs); 3668 3669 gcc_assert (TYPE_ATOMIC (lhs_type)); 3670 3671 if (return_old_p) 3672 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR); 3673 3674 /* Allocate enough vector items for a compare_exchange. */ 3675 vec_alloc (params, 6); 3676 3677 /* Create a compound statement to hold the sequence of statements 3678 with a loop. */ 3679 compound_stmt = c_begin_compound_stmt (false); 3680 3681 /* Fold the RHS if it hasn't already been folded. */ 3682 if (modifycode != NOP_EXPR) 3683 rhs = c_fully_fold (rhs, false, NULL); 3684 3685 /* Remove the qualifiers for the rest of the expressions and create 3686 the VAL temp variable to hold the RHS. */ 3687 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED); 3688 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED); 3689 val = create_tmp_var (nonatomic_rhs_type); 3690 TREE_ADDRESSABLE (val) = 1; 3691 TREE_NO_WARNING (val) = 1; 3692 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs); 3693 SET_EXPR_LOCATION (rhs, loc); 3694 add_stmt (rhs); 3695 3696 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue 3697 an atomic_store. */ 3698 if (modifycode == NOP_EXPR) 3699 { 3700 /* Build __atomic_store (&lhs, &val, SEQ_CST) */ 3701 rhs = build_unary_op (loc, ADDR_EXPR, val, 0); 3702 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE); 3703 params->quick_push (lhs_addr); 3704 params->quick_push (rhs); 3705 params->quick_push (seq_cst); 3706 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); 3707 add_stmt (func_call); 3708 3709 /* Finish the compound statement. */ 3710 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false); 3711 3712 /* VAL is the value which was stored, return a COMPOUND_STMT of 3713 the statement and that value. */ 3714 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val); 3715 } 3716 3717 /* Create the variables and labels required for the op= form. */ 3718 old = create_tmp_var (nonatomic_lhs_type); 3719 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0); 3720 TREE_ADDRESSABLE (old) = 1; 3721 TREE_NO_WARNING (old) = 1; 3722 3723 newval = create_tmp_var (nonatomic_lhs_type); 3724 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0); 3725 TREE_ADDRESSABLE (newval) = 1; 3726 3727 loop_decl = create_artificial_label (loc); 3728 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl); 3729 3730 done_decl = create_artificial_label (loc); 3731 done_label = build1 (LABEL_EXPR, void_type_node, done_decl); 3732 3733 /* __atomic_load (addr, &old, SEQ_CST). */ 3734 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD); 3735 params->quick_push (lhs_addr); 3736 params->quick_push (old_addr); 3737 params->quick_push (seq_cst); 3738 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); 3739 add_stmt (func_call); 3740 params->truncate (0); 3741 3742 /* Create the expressions for floating-point environment 3743 manipulation, if required. */ 3744 bool need_fenv = (flag_trapping_math 3745 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type))); 3746 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE; 3747 if (need_fenv) 3748 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call); 3749 3750 if (hold_call) 3751 add_stmt (hold_call); 3752 3753 /* loop: */ 3754 add_stmt (loop_label); 3755 3756 /* newval = old + val; */ 3757 rhs = build_binary_op (loc, modifycode, old, val, 1); 3758 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type, 3759 rhs, NULL_TREE, ic_assign, false, NULL_TREE, 3760 NULL_TREE, 0); 3761 if (rhs != error_mark_node) 3762 { 3763 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs); 3764 SET_EXPR_LOCATION (rhs, loc); 3765 add_stmt (rhs); 3766 } 3767 3768 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST)) 3769 goto done; */ 3770 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE); 3771 params->quick_push (lhs_addr); 3772 params->quick_push (old_addr); 3773 params->quick_push (newval_addr); 3774 params->quick_push (integer_zero_node); 3775 params->quick_push (seq_cst); 3776 params->quick_push (seq_cst); 3777 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); 3778 3779 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl); 3780 SET_EXPR_LOCATION (goto_stmt, loc); 3781 3782 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE); 3783 SET_EXPR_LOCATION (stmt, loc); 3784 add_stmt (stmt); 3785 3786 if (clear_call) 3787 add_stmt (clear_call); 3788 3789 /* goto loop; */ 3790 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl); 3791 SET_EXPR_LOCATION (goto_stmt, loc); 3792 add_stmt (goto_stmt); 3793 3794 /* done: */ 3795 add_stmt (done_label); 3796 3797 if (update_call) 3798 add_stmt (update_call); 3799 3800 /* Finish the compound statement. */ 3801 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false); 3802 3803 /* NEWVAL is the value that was successfully stored, return a 3804 COMPOUND_EXPR of the statement and the appropriate value. */ 3805 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, 3806 return_old_p ? old : newval); 3807} 3808 3809/* Construct and perhaps optimize a tree representation 3810 for a unary operation. CODE, a tree_code, specifies the operation 3811 and XARG is the operand. 3812 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses 3813 the default promotions (such as from short to int). 3814 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero 3815 allows non-lvalues; this is only used to handle conversion of non-lvalue 3816 arrays to pointers in C99. 3817 3818 LOCATION is the location of the operator. */ 3819 3820tree 3821build_unary_op (location_t location, 3822 enum tree_code code, tree xarg, int flag) 3823{ 3824 /* No default_conversion here. It causes trouble for ADDR_EXPR. */ 3825 tree arg = xarg; 3826 tree argtype = 0; 3827 enum tree_code typecode; 3828 tree val; 3829 tree ret = error_mark_node; 3830 tree eptype = NULL_TREE; 3831 int noconvert = flag; 3832 const char *invalid_op_diag; 3833 bool int_operands; 3834 3835 int_operands = EXPR_INT_CONST_OPERANDS (xarg); 3836 if (int_operands) 3837 arg = remove_c_maybe_const_expr (arg); 3838 3839 if (code != ADDR_EXPR) 3840 arg = require_complete_type (arg); 3841 3842 typecode = TREE_CODE (TREE_TYPE (arg)); 3843 if (typecode == ERROR_MARK) 3844 return error_mark_node; 3845 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE) 3846 typecode = INTEGER_TYPE; 3847 3848 if ((invalid_op_diag 3849 = targetm.invalid_unary_op (code, TREE_TYPE (xarg)))) 3850 { 3851 error_at (location, invalid_op_diag); 3852 return error_mark_node; 3853 } 3854 3855 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR) 3856 { 3857 eptype = TREE_TYPE (arg); 3858 arg = TREE_OPERAND (arg, 0); 3859 } 3860 3861 switch (code) 3862 { 3863 case CONVERT_EXPR: 3864 /* This is used for unary plus, because a CONVERT_EXPR 3865 is enough to prevent anybody from looking inside for 3866 associativity, but won't generate any code. */ 3867 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE 3868 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE 3869 || typecode == VECTOR_TYPE)) 3870 { 3871 error_at (location, "wrong type argument to unary plus"); 3872 return error_mark_node; 3873 } 3874 else if (!noconvert) 3875 arg = default_conversion (arg); 3876 arg = non_lvalue_loc (location, arg); 3877 break; 3878 3879 case NEGATE_EXPR: 3880 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE 3881 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE 3882 || typecode == VECTOR_TYPE)) 3883 { 3884 error_at (location, "wrong type argument to unary minus"); 3885 return error_mark_node; 3886 } 3887 else if (!noconvert) 3888 arg = default_conversion (arg); 3889 break; 3890 3891 case BIT_NOT_EXPR: 3892 /* ~ works on integer types and non float vectors. */ 3893 if (typecode == INTEGER_TYPE 3894 || (typecode == VECTOR_TYPE 3895 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg)))) 3896 { 3897 if (!noconvert) 3898 arg = default_conversion (arg); 3899 } 3900 else if (typecode == COMPLEX_TYPE) 3901 { 3902 code = CONJ_EXPR; 3903 pedwarn (location, OPT_Wpedantic, 3904 "ISO C does not support %<~%> for complex conjugation"); 3905 if (!noconvert) 3906 arg = default_conversion (arg); 3907 } 3908 else 3909 { 3910 error_at (location, "wrong type argument to bit-complement"); 3911 return error_mark_node; 3912 } 3913 break; 3914 3915 case ABS_EXPR: 3916 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE)) 3917 { 3918 error_at (location, "wrong type argument to abs"); 3919 return error_mark_node; 3920 } 3921 else if (!noconvert) 3922 arg = default_conversion (arg); 3923 break; 3924 3925 case CONJ_EXPR: 3926 /* Conjugating a real value is a no-op, but allow it anyway. */ 3927 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE 3928 || typecode == COMPLEX_TYPE)) 3929 { 3930 error_at (location, "wrong type argument to conjugation"); 3931 return error_mark_node; 3932 } 3933 else if (!noconvert) 3934 arg = default_conversion (arg); 3935 break; 3936 3937 case TRUTH_NOT_EXPR: 3938 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE 3939 && typecode != REAL_TYPE && typecode != POINTER_TYPE 3940 && typecode != COMPLEX_TYPE) 3941 { 3942 error_at (location, 3943 "wrong type argument to unary exclamation mark"); 3944 return error_mark_node; 3945 } 3946 if (int_operands) 3947 { 3948 arg = c_objc_common_truthvalue_conversion (location, xarg); 3949 arg = remove_c_maybe_const_expr (arg); 3950 } 3951 else 3952 arg = c_objc_common_truthvalue_conversion (location, arg); 3953 ret = invert_truthvalue_loc (location, arg); 3954 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */ 3955 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret)) 3956 location = EXPR_LOCATION (ret); 3957 goto return_build_unary_op; 3958 3959 case REALPART_EXPR: 3960 case IMAGPART_EXPR: 3961 ret = build_real_imag_expr (location, code, arg); 3962 if (ret == error_mark_node) 3963 return error_mark_node; 3964 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE) 3965 eptype = TREE_TYPE (eptype); 3966 goto return_build_unary_op; 3967 3968 case PREINCREMENT_EXPR: 3969 case POSTINCREMENT_EXPR: 3970 case PREDECREMENT_EXPR: 3971 case POSTDECREMENT_EXPR: 3972 3973 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) 3974 { 3975 tree inner = build_unary_op (location, code, 3976 C_MAYBE_CONST_EXPR_EXPR (arg), flag); 3977 if (inner == error_mark_node) 3978 return error_mark_node; 3979 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), 3980 C_MAYBE_CONST_EXPR_PRE (arg), inner); 3981 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); 3982 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1; 3983 goto return_build_unary_op; 3984 } 3985 3986 /* Complain about anything that is not a true lvalue. In 3987 Objective-C, skip this check for property_refs. */ 3988 if (!objc_is_property_ref (arg) 3989 && !lvalue_or_else (location, 3990 arg, ((code == PREINCREMENT_EXPR 3991 || code == POSTINCREMENT_EXPR) 3992 ? lv_increment 3993 : lv_decrement))) 3994 return error_mark_node; 3995 3996 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE) 3997 { 3998 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) 3999 warning_at (location, OPT_Wc___compat, 4000 "increment of enumeration value is invalid in C++"); 4001 else 4002 warning_at (location, OPT_Wc___compat, 4003 "decrement of enumeration value is invalid in C++"); 4004 } 4005 4006 /* Ensure the argument is fully folded inside any SAVE_EXPR. */ 4007 arg = c_fully_fold (arg, false, NULL); 4008 4009 bool atomic_op; 4010 atomic_op = really_atomic_lvalue (arg); 4011 4012 /* Increment or decrement the real part of the value, 4013 and don't change the imaginary part. */ 4014 if (typecode == COMPLEX_TYPE) 4015 { 4016 tree real, imag; 4017 4018 pedwarn (location, OPT_Wpedantic, 4019 "ISO C does not support %<++%> and %<--%> on complex types"); 4020 4021 if (!atomic_op) 4022 { 4023 arg = stabilize_reference (arg); 4024 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1); 4025 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1); 4026 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1); 4027 if (real == error_mark_node || imag == error_mark_node) 4028 return error_mark_node; 4029 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg), 4030 real, imag); 4031 goto return_build_unary_op; 4032 } 4033 } 4034 4035 /* Report invalid types. */ 4036 4037 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE 4038 && typecode != INTEGER_TYPE && typecode != REAL_TYPE 4039 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE) 4040 { 4041 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) 4042 error_at (location, "wrong type argument to increment"); 4043 else 4044 error_at (location, "wrong type argument to decrement"); 4045 4046 return error_mark_node; 4047 } 4048 4049 { 4050 tree inc; 4051 4052 argtype = TREE_TYPE (arg); 4053 4054 /* Compute the increment. */ 4055 4056 if (typecode == POINTER_TYPE) 4057 { 4058 /* If pointer target is an incomplete type, 4059 we just cannot know how to do the arithmetic. */ 4060 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype))) 4061 { 4062 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) 4063 error_at (location, 4064 "increment of pointer to an incomplete type %qT", 4065 TREE_TYPE (argtype)); 4066 else 4067 error_at (location, 4068 "decrement of pointer to an incomplete type %qT", 4069 TREE_TYPE (argtype)); 4070 } 4071 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE 4072 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE) 4073 { 4074 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) 4075 pedwarn (location, OPT_Wpointer_arith, 4076 "wrong type argument to increment"); 4077 else 4078 pedwarn (location, OPT_Wpointer_arith, 4079 "wrong type argument to decrement"); 4080 } 4081 4082 inc = c_size_in_bytes (TREE_TYPE (argtype)); 4083 inc = convert_to_ptrofftype_loc (location, inc); 4084 } 4085 else if (FRACT_MODE_P (TYPE_MODE (argtype))) 4086 { 4087 /* For signed fract types, we invert ++ to -- or 4088 -- to ++, and change inc from 1 to -1, because 4089 it is not possible to represent 1 in signed fract constants. 4090 For unsigned fract types, the result always overflows and 4091 we get an undefined (original) or the maximum value. */ 4092 if (code == PREINCREMENT_EXPR) 4093 code = PREDECREMENT_EXPR; 4094 else if (code == PREDECREMENT_EXPR) 4095 code = PREINCREMENT_EXPR; 4096 else if (code == POSTINCREMENT_EXPR) 4097 code = POSTDECREMENT_EXPR; 4098 else /* code == POSTDECREMENT_EXPR */ 4099 code = POSTINCREMENT_EXPR; 4100 4101 inc = integer_minus_one_node; 4102 inc = convert (argtype, inc); 4103 } 4104 else 4105 { 4106 inc = VECTOR_TYPE_P (argtype) 4107 ? build_one_cst (argtype) 4108 : integer_one_node; 4109 inc = convert (argtype, inc); 4110 } 4111 4112 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we 4113 need to ask Objective-C to build the increment or decrement 4114 expression for it. */ 4115 if (objc_is_property_ref (arg)) 4116 return objc_build_incr_expr_for_property_ref (location, code, 4117 arg, inc); 4118 4119 /* Report a read-only lvalue. */ 4120 if (TYPE_READONLY (argtype)) 4121 { 4122 readonly_error (location, arg, 4123 ((code == PREINCREMENT_EXPR 4124 || code == POSTINCREMENT_EXPR) 4125 ? lv_increment : lv_decrement)); 4126 return error_mark_node; 4127 } 4128 else if (TREE_READONLY (arg)) 4129 readonly_warning (arg, 4130 ((code == PREINCREMENT_EXPR 4131 || code == POSTINCREMENT_EXPR) 4132 ? lv_increment : lv_decrement)); 4133 4134 /* If the argument is atomic, use the special code sequences for 4135 atomic compound assignment. */ 4136 if (atomic_op) 4137 { 4138 arg = stabilize_reference (arg); 4139 ret = build_atomic_assign (location, arg, 4140 ((code == PREINCREMENT_EXPR 4141 || code == POSTINCREMENT_EXPR) 4142 ? PLUS_EXPR 4143 : MINUS_EXPR), 4144 (FRACT_MODE_P (TYPE_MODE (argtype)) 4145 ? inc 4146 : integer_one_node), 4147 (code == POSTINCREMENT_EXPR 4148 || code == POSTDECREMENT_EXPR)); 4149 goto return_build_unary_op; 4150 } 4151 4152 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE) 4153 val = boolean_increment (code, arg); 4154 else 4155 val = build2 (code, TREE_TYPE (arg), arg, inc); 4156 TREE_SIDE_EFFECTS (val) = 1; 4157 if (TREE_CODE (val) != code) 4158 TREE_NO_WARNING (val) = 1; 4159 ret = val; 4160 goto return_build_unary_op; 4161 } 4162 4163 case ADDR_EXPR: 4164 /* Note that this operation never does default_conversion. */ 4165 4166 /* The operand of unary '&' must be an lvalue (which excludes 4167 expressions of type void), or, in C99, the result of a [] or 4168 unary '*' operator. */ 4169 if (VOID_TYPE_P (TREE_TYPE (arg)) 4170 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED 4171 && (TREE_CODE (arg) != INDIRECT_REF 4172 || !flag_isoc99)) 4173 pedwarn (location, 0, "taking address of expression of type %<void%>"); 4174 4175 /* Let &* cancel out to simplify resulting code. */ 4176 if (TREE_CODE (arg) == INDIRECT_REF) 4177 { 4178 /* Don't let this be an lvalue. */ 4179 if (lvalue_p (TREE_OPERAND (arg, 0))) 4180 return non_lvalue_loc (location, TREE_OPERAND (arg, 0)); 4181 ret = TREE_OPERAND (arg, 0); 4182 goto return_build_unary_op; 4183 } 4184 4185 /* For &x[y], return x+y */ 4186 if (TREE_CODE (arg) == ARRAY_REF) 4187 { 4188 tree op0 = TREE_OPERAND (arg, 0); 4189 if (!c_mark_addressable (op0)) 4190 return error_mark_node; 4191 } 4192 4193 /* Anything not already handled and not a true memory reference 4194 or a non-lvalue array is an error. */ 4195 else if (typecode != FUNCTION_TYPE && !flag 4196 && !lvalue_or_else (location, arg, lv_addressof)) 4197 return error_mark_node; 4198 4199 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify 4200 folding later. */ 4201 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) 4202 { 4203 tree inner = build_unary_op (location, code, 4204 C_MAYBE_CONST_EXPR_EXPR (arg), flag); 4205 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), 4206 C_MAYBE_CONST_EXPR_PRE (arg), inner); 4207 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); 4208 C_MAYBE_CONST_EXPR_NON_CONST (ret) 4209 = C_MAYBE_CONST_EXPR_NON_CONST (arg); 4210 goto return_build_unary_op; 4211 } 4212 4213 /* Ordinary case; arg is a COMPONENT_REF or a decl. */ 4214 argtype = TREE_TYPE (arg); 4215 4216 /* If the lvalue is const or volatile, merge that into the type 4217 to which the address will point. This is only needed 4218 for function types. */ 4219 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg)) 4220 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)) 4221 && TREE_CODE (argtype) == FUNCTION_TYPE) 4222 { 4223 int orig_quals = TYPE_QUALS (strip_array_types (argtype)); 4224 int quals = orig_quals; 4225 4226 if (TREE_READONLY (arg)) 4227 quals |= TYPE_QUAL_CONST; 4228 if (TREE_THIS_VOLATILE (arg)) 4229 quals |= TYPE_QUAL_VOLATILE; 4230 4231 argtype = c_build_qualified_type (argtype, quals); 4232 } 4233 4234 if (!c_mark_addressable (arg)) 4235 return error_mark_node; 4236 4237 gcc_assert (TREE_CODE (arg) != COMPONENT_REF 4238 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))); 4239 4240 argtype = build_pointer_type (argtype); 4241 4242 /* ??? Cope with user tricks that amount to offsetof. Delete this 4243 when we have proper support for integer constant expressions. */ 4244 val = get_base_address (arg); 4245 if (val && TREE_CODE (val) == INDIRECT_REF 4246 && TREE_CONSTANT (TREE_OPERAND (val, 0))) 4247 { 4248 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg)); 4249 goto return_build_unary_op; 4250 } 4251 4252 val = build1 (ADDR_EXPR, argtype, arg); 4253 4254 ret = val; 4255 goto return_build_unary_op; 4256 4257 default: 4258 gcc_unreachable (); 4259 } 4260 4261 if (argtype == 0) 4262 argtype = TREE_TYPE (arg); 4263 if (TREE_CODE (arg) == INTEGER_CST) 4264 ret = (require_constant_value 4265 ? fold_build1_initializer_loc (location, code, argtype, arg) 4266 : fold_build1_loc (location, code, argtype, arg)); 4267 else 4268 ret = build1 (code, argtype, arg); 4269 return_build_unary_op: 4270 gcc_assert (ret != error_mark_node); 4271 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) 4272 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg))) 4273 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); 4274 else if (TREE_CODE (ret) != INTEGER_CST && int_operands) 4275 ret = note_integer_operands (ret); 4276 if (eptype) 4277 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); 4278 protected_set_expr_location (ret, location); 4279 return ret; 4280} 4281 4282/* Return nonzero if REF is an lvalue valid for this language. 4283 Lvalues can be assigned, unless their type has TYPE_READONLY. 4284 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */ 4285 4286bool 4287lvalue_p (const_tree ref) 4288{ 4289 const enum tree_code code = TREE_CODE (ref); 4290 4291 switch (code) 4292 { 4293 case REALPART_EXPR: 4294 case IMAGPART_EXPR: 4295 case COMPONENT_REF: 4296 return lvalue_p (TREE_OPERAND (ref, 0)); 4297 4298 case C_MAYBE_CONST_EXPR: 4299 return lvalue_p (TREE_OPERAND (ref, 1)); 4300 4301 case COMPOUND_LITERAL_EXPR: 4302 case STRING_CST: 4303 return 1; 4304 4305 case INDIRECT_REF: 4306 case ARRAY_REF: 4307 case ARRAY_NOTATION_REF: 4308 case VAR_DECL: 4309 case PARM_DECL: 4310 case RESULT_DECL: 4311 case ERROR_MARK: 4312 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE 4313 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE); 4314 4315 case BIND_EXPR: 4316 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; 4317 4318 default: 4319 return 0; 4320 } 4321} 4322 4323/* Give a warning for storing in something that is read-only in GCC 4324 terms but not const in ISO C terms. */ 4325 4326static void 4327readonly_warning (tree arg, enum lvalue_use use) 4328{ 4329 switch (use) 4330 { 4331 case lv_assign: 4332 warning (0, "assignment of read-only location %qE", arg); 4333 break; 4334 case lv_increment: 4335 warning (0, "increment of read-only location %qE", arg); 4336 break; 4337 case lv_decrement: 4338 warning (0, "decrement of read-only location %qE", arg); 4339 break; 4340 default: 4341 gcc_unreachable (); 4342 } 4343 return; 4344} 4345 4346 4347/* Return nonzero if REF is an lvalue valid for this language; 4348 otherwise, print an error message and return zero. USE says 4349 how the lvalue is being used and so selects the error message. 4350 LOCATION is the location at which any error should be reported. */ 4351 4352static int 4353lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use) 4354{ 4355 int win = lvalue_p (ref); 4356 4357 if (!win) 4358 lvalue_error (loc, use); 4359 4360 return win; 4361} 4362 4363/* Mark EXP saying that we need to be able to take the 4364 address of it; it should not be allocated in a register. 4365 Returns true if successful. */ 4366 4367bool 4368c_mark_addressable (tree exp) 4369{ 4370 tree x = exp; 4371 4372 while (1) 4373 switch (TREE_CODE (x)) 4374 { 4375 case COMPONENT_REF: 4376 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) 4377 { 4378 error 4379 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1)); 4380 return false; 4381 } 4382 4383 /* ... fall through ... */ 4384 4385 case ADDR_EXPR: 4386 case ARRAY_REF: 4387 case REALPART_EXPR: 4388 case IMAGPART_EXPR: 4389 x = TREE_OPERAND (x, 0); 4390 break; 4391 4392 case COMPOUND_LITERAL_EXPR: 4393 case CONSTRUCTOR: 4394 TREE_ADDRESSABLE (x) = 1; 4395 return true; 4396 4397 case VAR_DECL: 4398 case CONST_DECL: 4399 case PARM_DECL: 4400 case RESULT_DECL: 4401 if (C_DECL_REGISTER (x) 4402 && DECL_NONLOCAL (x)) 4403 { 4404 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) 4405 { 4406 error 4407 ("global register variable %qD used in nested function", x); 4408 return false; 4409 } 4410 pedwarn (input_location, 0, "register variable %qD used in nested function", x); 4411 } 4412 else if (C_DECL_REGISTER (x)) 4413 { 4414 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) 4415 error ("address of global register variable %qD requested", x); 4416 else 4417 error ("address of register variable %qD requested", x); 4418 return false; 4419 } 4420 4421 /* drops in */ 4422 case FUNCTION_DECL: 4423 TREE_ADDRESSABLE (x) = 1; 4424 /* drops out */ 4425 default: 4426 return true; 4427 } 4428} 4429 4430/* Convert EXPR to TYPE, warning about conversion problems with 4431 constants. SEMANTIC_TYPE is the type this conversion would use 4432 without excess precision. If SEMANTIC_TYPE is NULL, this function 4433 is equivalent to convert_and_check. This function is a wrapper that 4434 handles conversions that may be different than 4435 the usual ones because of excess precision. */ 4436 4437static tree 4438ep_convert_and_check (location_t loc, tree type, tree expr, 4439 tree semantic_type) 4440{ 4441 if (TREE_TYPE (expr) == type) 4442 return expr; 4443 4444 if (!semantic_type) 4445 return convert_and_check (loc, type, expr); 4446 4447 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE 4448 && TREE_TYPE (expr) != semantic_type) 4449 { 4450 /* For integers, we need to check the real conversion, not 4451 the conversion to the excess precision type. */ 4452 expr = convert_and_check (loc, semantic_type, expr); 4453 } 4454 /* Result type is the excess precision type, which should be 4455 large enough, so do not check. */ 4456 return convert (type, expr); 4457} 4458 4459/* Build and return a conditional expression IFEXP ? OP1 : OP2. If 4460 IFEXP_BCP then the condition is a call to __builtin_constant_p, and 4461 if folded to an integer constant then the unselected half may 4462 contain arbitrary operations not normally permitted in constant 4463 expressions. Set the location of the expression to LOC. */ 4464 4465tree 4466build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, 4467 tree op1, tree op1_original_type, tree op2, 4468 tree op2_original_type) 4469{ 4470 tree type1; 4471 tree type2; 4472 enum tree_code code1; 4473 enum tree_code code2; 4474 tree result_type = NULL; 4475 tree semantic_result_type = NULL; 4476 tree orig_op1 = op1, orig_op2 = op2; 4477 bool int_const, op1_int_operands, op2_int_operands, int_operands; 4478 bool ifexp_int_operands; 4479 tree ret; 4480 4481 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); 4482 if (op1_int_operands) 4483 op1 = remove_c_maybe_const_expr (op1); 4484 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2); 4485 if (op2_int_operands) 4486 op2 = remove_c_maybe_const_expr (op2); 4487 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp); 4488 if (ifexp_int_operands) 4489 ifexp = remove_c_maybe_const_expr (ifexp); 4490 4491 /* Promote both alternatives. */ 4492 4493 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) 4494 op1 = default_conversion (op1); 4495 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) 4496 op2 = default_conversion (op2); 4497 4498 if (TREE_CODE (ifexp) == ERROR_MARK 4499 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK 4500 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) 4501 return error_mark_node; 4502 4503 type1 = TREE_TYPE (op1); 4504 code1 = TREE_CODE (type1); 4505 type2 = TREE_TYPE (op2); 4506 code2 = TREE_CODE (type2); 4507 4508 /* C90 does not permit non-lvalue arrays in conditional expressions. 4509 In C99 they will be pointers by now. */ 4510 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE) 4511 { 4512 error_at (colon_loc, "non-lvalue array in conditional expression"); 4513 return error_mark_node; 4514 } 4515 4516 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR 4517 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR) 4518 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE 4519 || code1 == COMPLEX_TYPE) 4520 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE 4521 || code2 == COMPLEX_TYPE)) 4522 { 4523 semantic_result_type = c_common_type (type1, type2); 4524 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) 4525 { 4526 op1 = TREE_OPERAND (op1, 0); 4527 type1 = TREE_TYPE (op1); 4528 gcc_assert (TREE_CODE (type1) == code1); 4529 } 4530 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR) 4531 { 4532 op2 = TREE_OPERAND (op2, 0); 4533 type2 = TREE_TYPE (op2); 4534 gcc_assert (TREE_CODE (type2) == code2); 4535 } 4536 } 4537 4538 if (warn_cxx_compat) 4539 { 4540 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1); 4541 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2); 4542 4543 if (TREE_CODE (t1) == ENUMERAL_TYPE 4544 && TREE_CODE (t2) == ENUMERAL_TYPE 4545 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2)) 4546 warning_at (colon_loc, OPT_Wc___compat, 4547 ("different enum types in conditional is " 4548 "invalid in C++: %qT vs %qT"), 4549 t1, t2); 4550 } 4551 4552 /* Quickly detect the usual case where op1 and op2 have the same type 4553 after promotion. */ 4554 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) 4555 { 4556 if (type1 == type2) 4557 result_type = type1; 4558 else 4559 result_type = TYPE_MAIN_VARIANT (type1); 4560 } 4561 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE 4562 || code1 == COMPLEX_TYPE) 4563 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE 4564 || code2 == COMPLEX_TYPE)) 4565 { 4566 result_type = c_common_type (type1, type2); 4567 do_warn_double_promotion (result_type, type1, type2, 4568 "implicit conversion from %qT to %qT to " 4569 "match other result of conditional", 4570 colon_loc); 4571 4572 /* If -Wsign-compare, warn here if type1 and type2 have 4573 different signedness. We'll promote the signed to unsigned 4574 and later code won't know it used to be different. 4575 Do this check on the original types, so that explicit casts 4576 will be considered, but default promotions won't. */ 4577 if (c_inhibit_evaluation_warnings == 0) 4578 { 4579 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1)); 4580 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2)); 4581 4582 if (unsigned_op1 ^ unsigned_op2) 4583 { 4584 bool ovf; 4585 4586 /* Do not warn if the result type is signed, since the 4587 signed type will only be chosen if it can represent 4588 all the values of the unsigned type. */ 4589 if (!TYPE_UNSIGNED (result_type)) 4590 /* OK */; 4591 else 4592 { 4593 bool op1_maybe_const = true; 4594 bool op2_maybe_const = true; 4595 4596 /* Do not warn if the signed quantity is an 4597 unsuffixed integer literal (or some static 4598 constant expression involving such literals) and 4599 it is non-negative. This warning requires the 4600 operands to be folded for best results, so do 4601 that folding in this case even without 4602 warn_sign_compare to avoid warning options 4603 possibly affecting code generation. */ 4604 c_inhibit_evaluation_warnings 4605 += (ifexp == truthvalue_false_node); 4606 op1 = c_fully_fold (op1, require_constant_value, 4607 &op1_maybe_const); 4608 c_inhibit_evaluation_warnings 4609 -= (ifexp == truthvalue_false_node); 4610 4611 c_inhibit_evaluation_warnings 4612 += (ifexp == truthvalue_true_node); 4613 op2 = c_fully_fold (op2, require_constant_value, 4614 &op2_maybe_const); 4615 c_inhibit_evaluation_warnings 4616 -= (ifexp == truthvalue_true_node); 4617 4618 if (warn_sign_compare) 4619 { 4620 if ((unsigned_op2 4621 && tree_expr_nonnegative_warnv_p (op1, &ovf)) 4622 || (unsigned_op1 4623 && tree_expr_nonnegative_warnv_p (op2, &ovf))) 4624 /* OK */; 4625 else 4626 warning_at (colon_loc, OPT_Wsign_compare, 4627 ("signed and unsigned type in " 4628 "conditional expression")); 4629 } 4630 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) 4631 op1 = c_wrap_maybe_const (op1, !op1_maybe_const); 4632 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST) 4633 op2 = c_wrap_maybe_const (op2, !op2_maybe_const); 4634 } 4635 } 4636 } 4637 } 4638 else if (code1 == VOID_TYPE || code2 == VOID_TYPE) 4639 { 4640 if (code1 != VOID_TYPE || code2 != VOID_TYPE) 4641 pedwarn (colon_loc, OPT_Wpedantic, 4642 "ISO C forbids conditional expr with only one void side"); 4643 result_type = void_type_node; 4644 } 4645 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) 4646 { 4647 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); 4648 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2)); 4649 addr_space_t as_common; 4650 4651 if (comp_target_types (colon_loc, type1, type2)) 4652 result_type = common_pointer_type (type1, type2); 4653 else if (null_pointer_constant_p (orig_op1)) 4654 result_type = type2; 4655 else if (null_pointer_constant_p (orig_op2)) 4656 result_type = type1; 4657 else if (!addr_space_superset (as1, as2, &as_common)) 4658 { 4659 error_at (colon_loc, "pointers to disjoint address spaces " 4660 "used in conditional expression"); 4661 return error_mark_node; 4662 } 4663 else if (VOID_TYPE_P (TREE_TYPE (type1)) 4664 && !TYPE_ATOMIC (TREE_TYPE (type1))) 4665 { 4666 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE) 4667 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2))) 4668 & ~TYPE_QUALS (TREE_TYPE (type1)))) 4669 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers, 4670 "pointer to array loses qualifier " 4671 "in conditional expression"); 4672 4673 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) 4674 pedwarn (colon_loc, OPT_Wpedantic, 4675 "ISO C forbids conditional expr between " 4676 "%<void *%> and function pointer"); 4677 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1), 4678 TREE_TYPE (type2))); 4679 } 4680 else if (VOID_TYPE_P (TREE_TYPE (type2)) 4681 && !TYPE_ATOMIC (TREE_TYPE (type2))) 4682 { 4683 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE) 4684 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1))) 4685 & ~TYPE_QUALS (TREE_TYPE (type2)))) 4686 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers, 4687 "pointer to array loses qualifier " 4688 "in conditional expression"); 4689 4690 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) 4691 pedwarn (colon_loc, OPT_Wpedantic, 4692 "ISO C forbids conditional expr between " 4693 "%<void *%> and function pointer"); 4694 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2), 4695 TREE_TYPE (type1))); 4696 } 4697 /* Objective-C pointer comparisons are a bit more lenient. */ 4698 else if (objc_have_common_type (type1, type2, -3, NULL_TREE)) 4699 result_type = objc_common_type (type1, type2); 4700 else 4701 { 4702 int qual = ENCODE_QUAL_ADDR_SPACE (as_common); 4703 4704 pedwarn (colon_loc, 0, 4705 "pointer type mismatch in conditional expression"); 4706 result_type = build_pointer_type 4707 (build_qualified_type (void_type_node, qual)); 4708 } 4709 } 4710 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) 4711 { 4712 if (!null_pointer_constant_p (orig_op2)) 4713 pedwarn (colon_loc, 0, 4714 "pointer/integer type mismatch in conditional expression"); 4715 else 4716 { 4717 op2 = null_pointer_node; 4718 } 4719 result_type = type1; 4720 } 4721 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) 4722 { 4723 if (!null_pointer_constant_p (orig_op1)) 4724 pedwarn (colon_loc, 0, 4725 "pointer/integer type mismatch in conditional expression"); 4726 else 4727 { 4728 op1 = null_pointer_node; 4729 } 4730 result_type = type2; 4731 } 4732 4733 if (!result_type) 4734 { 4735 if (flag_cond_mismatch) 4736 result_type = void_type_node; 4737 else 4738 { 4739 error_at (colon_loc, "type mismatch in conditional expression"); 4740 return error_mark_node; 4741 } 4742 } 4743 4744 /* Merge const and volatile flags of the incoming types. */ 4745 result_type 4746 = build_type_variant (result_type, 4747 TYPE_READONLY (type1) || TYPE_READONLY (type2), 4748 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2)); 4749 4750 op1 = ep_convert_and_check (colon_loc, result_type, op1, 4751 semantic_result_type); 4752 op2 = ep_convert_and_check (colon_loc, result_type, op2, 4753 semantic_result_type); 4754 4755 if (ifexp_bcp && ifexp == truthvalue_true_node) 4756 { 4757 op2_int_operands = true; 4758 op1 = c_fully_fold (op1, require_constant_value, NULL); 4759 } 4760 if (ifexp_bcp && ifexp == truthvalue_false_node) 4761 { 4762 op1_int_operands = true; 4763 op2 = c_fully_fold (op2, require_constant_value, NULL); 4764 } 4765 int_const = int_operands = (ifexp_int_operands 4766 && op1_int_operands 4767 && op2_int_operands); 4768 if (int_operands) 4769 { 4770 int_const = ((ifexp == truthvalue_true_node 4771 && TREE_CODE (orig_op1) == INTEGER_CST 4772 && !TREE_OVERFLOW (orig_op1)) 4773 || (ifexp == truthvalue_false_node 4774 && TREE_CODE (orig_op2) == INTEGER_CST 4775 && !TREE_OVERFLOW (orig_op2))); 4776 } 4777 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST)) 4778 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2); 4779 else 4780 { 4781 if (int_operands) 4782 { 4783 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be 4784 nested inside of the expression. */ 4785 op1 = c_fully_fold (op1, false, NULL); 4786 op2 = c_fully_fold (op2, false, NULL); 4787 } 4788 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2); 4789 if (int_operands) 4790 ret = note_integer_operands (ret); 4791 } 4792 if (semantic_result_type) 4793 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); 4794 4795 protected_set_expr_location (ret, colon_loc); 4796 return ret; 4797} 4798 4799/* Return a compound expression that performs two expressions and 4800 returns the value of the second of them. 4801 4802 LOC is the location of the COMPOUND_EXPR. */ 4803 4804tree 4805build_compound_expr (location_t loc, tree expr1, tree expr2) 4806{ 4807 bool expr1_int_operands, expr2_int_operands; 4808 tree eptype = NULL_TREE; 4809 tree ret; 4810 4811 if (flag_cilkplus 4812 && (TREE_CODE (expr1) == CILK_SPAWN_STMT 4813 || TREE_CODE (expr2) == CILK_SPAWN_STMT)) 4814 { 4815 error_at (loc, 4816 "spawned function call cannot be part of a comma expression"); 4817 return error_mark_node; 4818 } 4819 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1); 4820 if (expr1_int_operands) 4821 expr1 = remove_c_maybe_const_expr (expr1); 4822 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2); 4823 if (expr2_int_operands) 4824 expr2 = remove_c_maybe_const_expr (expr2); 4825 4826 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR) 4827 expr1 = TREE_OPERAND (expr1, 0); 4828 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR) 4829 { 4830 eptype = TREE_TYPE (expr2); 4831 expr2 = TREE_OPERAND (expr2, 0); 4832 } 4833 4834 if (!TREE_SIDE_EFFECTS (expr1)) 4835 { 4836 /* The left-hand operand of a comma expression is like an expression 4837 statement: with -Wunused, we should warn if it doesn't have 4838 any side-effects, unless it was explicitly cast to (void). */ 4839 if (warn_unused_value) 4840 { 4841 if (VOID_TYPE_P (TREE_TYPE (expr1)) 4842 && CONVERT_EXPR_P (expr1)) 4843 ; /* (void) a, b */ 4844 else if (VOID_TYPE_P (TREE_TYPE (expr1)) 4845 && TREE_CODE (expr1) == COMPOUND_EXPR 4846 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1))) 4847 ; /* (void) a, (void) b, c */ 4848 else 4849 warning_at (loc, OPT_Wunused_value, 4850 "left-hand operand of comma expression has no effect"); 4851 } 4852 } 4853 else if (TREE_CODE (expr1) == COMPOUND_EXPR 4854 && warn_unused_value) 4855 { 4856 tree r = expr1; 4857 location_t cloc = loc; 4858 while (TREE_CODE (r) == COMPOUND_EXPR) 4859 { 4860 if (EXPR_HAS_LOCATION (r)) 4861 cloc = EXPR_LOCATION (r); 4862 r = TREE_OPERAND (r, 1); 4863 } 4864 if (!TREE_SIDE_EFFECTS (r) 4865 && !VOID_TYPE_P (TREE_TYPE (r)) 4866 && !CONVERT_EXPR_P (r)) 4867 warning_at (cloc, OPT_Wunused_value, 4868 "right-hand operand of comma expression has no effect"); 4869 } 4870 4871 /* With -Wunused, we should also warn if the left-hand operand does have 4872 side-effects, but computes a value which is not used. For example, in 4873 `foo() + bar(), baz()' the result of the `+' operator is not used, 4874 so we should issue a warning. */ 4875 else if (warn_unused_value) 4876 warn_if_unused_value (expr1, loc); 4877 4878 if (expr2 == error_mark_node) 4879 return error_mark_node; 4880 4881 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2); 4882 4883 if (flag_isoc99 4884 && expr1_int_operands 4885 && expr2_int_operands) 4886 ret = note_integer_operands (ret); 4887 4888 if (eptype) 4889 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); 4890 4891 protected_set_expr_location (ret, loc); 4892 return ret; 4893} 4894 4895/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to 4896 which we are casting. OTYPE is the type of the expression being 4897 cast. Both TYPE and OTYPE are pointer types. LOC is the location 4898 of the cast. -Wcast-qual appeared on the command line. Named 4899 address space qualifiers are not handled here, because they result 4900 in different warnings. */ 4901 4902static void 4903handle_warn_cast_qual (location_t loc, tree type, tree otype) 4904{ 4905 tree in_type = type; 4906 tree in_otype = otype; 4907 int added = 0; 4908 int discarded = 0; 4909 bool is_const; 4910 4911 /* Check that the qualifiers on IN_TYPE are a superset of the 4912 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE 4913 nodes is uninteresting and we stop as soon as we hit a 4914 non-POINTER_TYPE node on either type. */ 4915 do 4916 { 4917 in_otype = TREE_TYPE (in_otype); 4918 in_type = TREE_TYPE (in_type); 4919 4920 /* GNU C allows cv-qualified function types. 'const' means the 4921 function is very pure, 'volatile' means it can't return. We 4922 need to warn when such qualifiers are added, not when they're 4923 taken away. */ 4924 if (TREE_CODE (in_otype) == FUNCTION_TYPE 4925 && TREE_CODE (in_type) == FUNCTION_TYPE) 4926 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type) 4927 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype)); 4928 else 4929 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype) 4930 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type)); 4931 } 4932 while (TREE_CODE (in_type) == POINTER_TYPE 4933 && TREE_CODE (in_otype) == POINTER_TYPE); 4934 4935 if (added) 4936 warning_at (loc, OPT_Wcast_qual, 4937 "cast adds %q#v qualifier to function type", added); 4938 4939 if (discarded) 4940 /* There are qualifiers present in IN_OTYPE that are not present 4941 in IN_TYPE. */ 4942 warning_at (loc, OPT_Wcast_qual, 4943 "cast discards %qv qualifier from pointer target type", 4944 discarded); 4945 4946 if (added || discarded) 4947 return; 4948 4949 /* A cast from **T to const **T is unsafe, because it can cause a 4950 const value to be changed with no additional warning. We only 4951 issue this warning if T is the same on both sides, and we only 4952 issue the warning if there are the same number of pointers on 4953 both sides, as otherwise the cast is clearly unsafe anyhow. A 4954 cast is unsafe when a qualifier is added at one level and const 4955 is not present at all outer levels. 4956 4957 To issue this warning, we check at each level whether the cast 4958 adds new qualifiers not already seen. We don't need to special 4959 case function types, as they won't have the same 4960 TYPE_MAIN_VARIANT. */ 4961 4962 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype)) 4963 return; 4964 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE) 4965 return; 4966 4967 in_type = type; 4968 in_otype = otype; 4969 is_const = TYPE_READONLY (TREE_TYPE (in_type)); 4970 do 4971 { 4972 in_type = TREE_TYPE (in_type); 4973 in_otype = TREE_TYPE (in_otype); 4974 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0 4975 && !is_const) 4976 { 4977 warning_at (loc, OPT_Wcast_qual, 4978 "to be safe all intermediate pointers in cast from " 4979 "%qT to %qT must be %<const%> qualified", 4980 otype, type); 4981 break; 4982 } 4983 if (is_const) 4984 is_const = TYPE_READONLY (in_type); 4985 } 4986 while (TREE_CODE (in_type) == POINTER_TYPE); 4987} 4988 4989/* Build an expression representing a cast to type TYPE of expression EXPR. 4990 LOC is the location of the cast-- typically the open paren of the cast. */ 4991 4992tree 4993build_c_cast (location_t loc, tree type, tree expr) 4994{ 4995 tree value; 4996 4997 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) 4998 expr = TREE_OPERAND (expr, 0); 4999 5000 value = expr; 5001 5002 if (type == error_mark_node || expr == error_mark_node) 5003 return error_mark_node; 5004 5005 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing 5006 only in <protocol> qualifications. But when constructing cast expressions, 5007 the protocols do matter and must be kept around. */ 5008 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr))) 5009 return build1 (NOP_EXPR, type, expr); 5010 5011 type = TYPE_MAIN_VARIANT (type); 5012 5013 if (TREE_CODE (type) == ARRAY_TYPE) 5014 { 5015 error_at (loc, "cast specifies array type"); 5016 return error_mark_node; 5017 } 5018 5019 if (TREE_CODE (type) == FUNCTION_TYPE) 5020 { 5021 error_at (loc, "cast specifies function type"); 5022 return error_mark_node; 5023 } 5024 5025 if (!VOID_TYPE_P (type)) 5026 { 5027 value = require_complete_type (value); 5028 if (value == error_mark_node) 5029 return error_mark_node; 5030 } 5031 5032 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value))) 5033 { 5034 if (TREE_CODE (type) == RECORD_TYPE 5035 || TREE_CODE (type) == UNION_TYPE) 5036 pedwarn (loc, OPT_Wpedantic, 5037 "ISO C forbids casting nonscalar to the same type"); 5038 5039 /* Convert to remove any qualifiers from VALUE's type. */ 5040 value = convert (type, value); 5041 } 5042 else if (TREE_CODE (type) == UNION_TYPE) 5043 { 5044 tree field; 5045 5046 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 5047 if (TREE_TYPE (field) != error_mark_node 5048 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), 5049 TYPE_MAIN_VARIANT (TREE_TYPE (value)))) 5050 break; 5051 5052 if (field) 5053 { 5054 tree t; 5055 bool maybe_const = true; 5056 5057 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type"); 5058 t = c_fully_fold (value, false, &maybe_const); 5059 t = build_constructor_single (type, field, t); 5060 if (!maybe_const) 5061 t = c_wrap_maybe_const (t, true); 5062 t = digest_init (loc, type, t, 5063 NULL_TREE, false, true, 0); 5064 TREE_CONSTANT (t) = TREE_CONSTANT (value); 5065 return t; 5066 } 5067 error_at (loc, "cast to union type from type not present in union"); 5068 return error_mark_node; 5069 } 5070 else 5071 { 5072 tree otype, ovalue; 5073 5074 if (type == void_type_node) 5075 { 5076 tree t = build1 (CONVERT_EXPR, type, value); 5077 SET_EXPR_LOCATION (t, loc); 5078 return t; 5079 } 5080 5081 otype = TREE_TYPE (value); 5082 5083 /* Optionally warn about potentially worrisome casts. */ 5084 if (warn_cast_qual 5085 && TREE_CODE (type) == POINTER_TYPE 5086 && TREE_CODE (otype) == POINTER_TYPE) 5087 handle_warn_cast_qual (loc, type, otype); 5088 5089 /* Warn about conversions between pointers to disjoint 5090 address spaces. */ 5091 if (TREE_CODE (type) == POINTER_TYPE 5092 && TREE_CODE (otype) == POINTER_TYPE 5093 && !null_pointer_constant_p (value)) 5094 { 5095 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type)); 5096 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype)); 5097 addr_space_t as_common; 5098 5099 if (!addr_space_superset (as_to, as_from, &as_common)) 5100 { 5101 if (ADDR_SPACE_GENERIC_P (as_from)) 5102 warning_at (loc, 0, "cast to %s address space pointer " 5103 "from disjoint generic address space pointer", 5104 c_addr_space_name (as_to)); 5105 5106 else if (ADDR_SPACE_GENERIC_P (as_to)) 5107 warning_at (loc, 0, "cast to generic address space pointer " 5108 "from disjoint %s address space pointer", 5109 c_addr_space_name (as_from)); 5110 5111 else 5112 warning_at (loc, 0, "cast to %s address space pointer " 5113 "from disjoint %s address space pointer", 5114 c_addr_space_name (as_to), 5115 c_addr_space_name (as_from)); 5116 } 5117 } 5118 5119 /* Warn about possible alignment problems. */ 5120 if (STRICT_ALIGNMENT 5121 && TREE_CODE (type) == POINTER_TYPE 5122 && TREE_CODE (otype) == POINTER_TYPE 5123 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE 5124 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE 5125 /* Don't warn about opaque types, where the actual alignment 5126 restriction is unknown. */ 5127 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE 5128 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE) 5129 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) 5130 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) 5131 warning_at (loc, OPT_Wcast_align, 5132 "cast increases required alignment of target type"); 5133 5134 if (TREE_CODE (type) == INTEGER_TYPE 5135 && TREE_CODE (otype) == POINTER_TYPE 5136 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)) 5137 /* Unlike conversion of integers to pointers, where the 5138 warning is disabled for converting constants because 5139 of cases such as SIG_*, warn about converting constant 5140 pointers to integers. In some cases it may cause unwanted 5141 sign extension, and a warning is appropriate. */ 5142 warning_at (loc, OPT_Wpointer_to_int_cast, 5143 "cast from pointer to integer of different size"); 5144 5145 if (TREE_CODE (value) == CALL_EXPR 5146 && TREE_CODE (type) != TREE_CODE (otype)) 5147 warning_at (loc, OPT_Wbad_function_cast, 5148 "cast from function call of type %qT " 5149 "to non-matching type %qT", otype, type); 5150 5151 if (TREE_CODE (type) == POINTER_TYPE 5152 && TREE_CODE (otype) == INTEGER_TYPE 5153 && TYPE_PRECISION (type) != TYPE_PRECISION (otype) 5154 /* Don't warn about converting any constant. */ 5155 && !TREE_CONSTANT (value)) 5156 warning_at (loc, 5157 OPT_Wint_to_pointer_cast, "cast to pointer from integer " 5158 "of different size"); 5159 5160 if (warn_strict_aliasing <= 2) 5161 strict_aliasing_warning (otype, type, expr); 5162 5163 /* If pedantic, warn for conversions between function and object 5164 pointer types, except for converting a null pointer constant 5165 to function pointer type. */ 5166 if (pedantic 5167 && TREE_CODE (type) == POINTER_TYPE 5168 && TREE_CODE (otype) == POINTER_TYPE 5169 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE 5170 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE) 5171 pedwarn (loc, OPT_Wpedantic, "ISO C forbids " 5172 "conversion of function pointer to object pointer type"); 5173 5174 if (pedantic 5175 && TREE_CODE (type) == POINTER_TYPE 5176 && TREE_CODE (otype) == POINTER_TYPE 5177 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE 5178 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE 5179 && !null_pointer_constant_p (value)) 5180 pedwarn (loc, OPT_Wpedantic, "ISO C forbids " 5181 "conversion of object pointer to function pointer type"); 5182 5183 ovalue = value; 5184 value = convert (type, value); 5185 5186 /* Ignore any integer overflow caused by the cast. */ 5187 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype)) 5188 { 5189 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue)) 5190 { 5191 if (!TREE_OVERFLOW (value)) 5192 { 5193 /* Avoid clobbering a shared constant. */ 5194 value = copy_node (value); 5195 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); 5196 } 5197 } 5198 else if (TREE_OVERFLOW (value)) 5199 /* Reset VALUE's overflow flags, ensuring constant sharing. */ 5200 value = wide_int_to_tree (TREE_TYPE (value), value); 5201 } 5202 } 5203 5204 /* Don't let a cast be an lvalue. */ 5205 if (value == expr) 5206 value = non_lvalue_loc (loc, value); 5207 5208 /* Don't allow the results of casting to floating-point or complex 5209 types be confused with actual constants, or casts involving 5210 integer and pointer types other than direct integer-to-integer 5211 and integer-to-pointer be confused with integer constant 5212 expressions and null pointer constants. */ 5213 if (TREE_CODE (value) == REAL_CST 5214 || TREE_CODE (value) == COMPLEX_CST 5215 || (TREE_CODE (value) == INTEGER_CST 5216 && !((TREE_CODE (expr) == INTEGER_CST 5217 && INTEGRAL_TYPE_P (TREE_TYPE (expr))) 5218 || TREE_CODE (expr) == REAL_CST 5219 || TREE_CODE (expr) == COMPLEX_CST))) 5220 value = build1 (NOP_EXPR, type, value); 5221 5222 if (CAN_HAVE_LOCATION_P (value)) 5223 SET_EXPR_LOCATION (value, loc); 5224 return value; 5225} 5226 5227/* Interpret a cast of expression EXPR to type TYPE. LOC is the 5228 location of the open paren of the cast, or the position of the cast 5229 expr. */ 5230tree 5231c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr) 5232{ 5233 tree type; 5234 tree type_expr = NULL_TREE; 5235 bool type_expr_const = true; 5236 tree ret; 5237 int saved_wsp = warn_strict_prototypes; 5238 5239 /* This avoids warnings about unprototyped casts on 5240 integers. E.g. "#define SIG_DFL (void(*)())0". */ 5241 if (TREE_CODE (expr) == INTEGER_CST) 5242 warn_strict_prototypes = 0; 5243 type = groktypename (type_name, &type_expr, &type_expr_const); 5244 warn_strict_prototypes = saved_wsp; 5245 5246 ret = build_c_cast (loc, type, expr); 5247 if (type_expr) 5248 { 5249 bool inner_expr_const = true; 5250 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const); 5251 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret); 5252 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const 5253 && inner_expr_const); 5254 SET_EXPR_LOCATION (ret, loc); 5255 } 5256 5257 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret)) 5258 SET_EXPR_LOCATION (ret, loc); 5259 5260 /* C++ does not permits types to be defined in a cast, but it 5261 allows references to incomplete types. */ 5262 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef) 5263 warning_at (loc, OPT_Wc___compat, 5264 "defining a type in a cast is invalid in C++"); 5265 5266 return ret; 5267} 5268 5269/* Build an assignment expression of lvalue LHS from value RHS. 5270 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which 5271 may differ from TREE_TYPE (LHS) for an enum bitfield. 5272 MODIFYCODE is the code for a binary operator that we use 5273 to combine the old value of LHS with RHS to get the new value. 5274 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. 5275 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS, 5276 which may differ from TREE_TYPE (RHS) for an enum value. 5277 5278 LOCATION is the location of the MODIFYCODE operator. 5279 RHS_LOC is the location of the RHS. */ 5280 5281tree 5282build_modify_expr (location_t location, tree lhs, tree lhs_origtype, 5283 enum tree_code modifycode, 5284 location_t rhs_loc, tree rhs, tree rhs_origtype) 5285{ 5286 tree result; 5287 tree newrhs; 5288 tree rhseval = NULL_TREE; 5289 tree rhs_semantic_type = NULL_TREE; 5290 tree lhstype = TREE_TYPE (lhs); 5291 tree olhstype = lhstype; 5292 bool npc; 5293 bool is_atomic_op; 5294 5295 /* Types that aren't fully specified cannot be used in assignments. */ 5296 lhs = require_complete_type (lhs); 5297 5298 /* Avoid duplicate error messages from operands that had errors. */ 5299 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) 5300 return error_mark_node; 5301 5302 /* Ensure an error for assigning a non-lvalue array to an array in 5303 C90. */ 5304 if (TREE_CODE (lhstype) == ARRAY_TYPE) 5305 { 5306 error_at (location, "assignment to expression with array type"); 5307 return error_mark_node; 5308 } 5309 5310 /* For ObjC properties, defer this check. */ 5311 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign)) 5312 return error_mark_node; 5313 5314 is_atomic_op = really_atomic_lvalue (lhs); 5315 5316 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) 5317 { 5318 rhs_semantic_type = TREE_TYPE (rhs); 5319 rhs = TREE_OPERAND (rhs, 0); 5320 } 5321 5322 newrhs = rhs; 5323 5324 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) 5325 { 5326 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs), 5327 lhs_origtype, modifycode, rhs_loc, rhs, 5328 rhs_origtype); 5329 if (inner == error_mark_node) 5330 return error_mark_node; 5331 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), 5332 C_MAYBE_CONST_EXPR_PRE (lhs), inner); 5333 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs)); 5334 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1; 5335 protected_set_expr_location (result, location); 5336 return result; 5337 } 5338 5339 /* If a binary op has been requested, combine the old LHS value with the RHS 5340 producing the value we should actually store into the LHS. */ 5341 5342 if (modifycode != NOP_EXPR) 5343 { 5344 lhs = c_fully_fold (lhs, false, NULL); 5345 lhs = stabilize_reference (lhs); 5346 5347 /* Construct the RHS for any non-atomic compound assignemnt. */ 5348 if (!is_atomic_op) 5349 { 5350 /* If in LHS op= RHS the RHS has side-effects, ensure they 5351 are preevaluated before the rest of the assignment expression's 5352 side-effects, because RHS could contain e.g. function calls 5353 that modify LHS. */ 5354 if (TREE_SIDE_EFFECTS (rhs)) 5355 { 5356 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs); 5357 rhseval = newrhs; 5358 } 5359 newrhs = build_binary_op (location, 5360 modifycode, lhs, newrhs, 1); 5361 5362 /* The original type of the right hand side is no longer 5363 meaningful. */ 5364 rhs_origtype = NULL_TREE; 5365 } 5366 } 5367 5368 if (c_dialect_objc ()) 5369 { 5370 /* Check if we are modifying an Objective-C property reference; 5371 if so, we need to generate setter calls. */ 5372 result = objc_maybe_build_modify_expr (lhs, newrhs); 5373 if (result) 5374 goto return_result; 5375 5376 /* Else, do the check that we postponed for Objective-C. */ 5377 if (!lvalue_or_else (location, lhs, lv_assign)) 5378 return error_mark_node; 5379 } 5380 5381 /* Give an error for storing in something that is 'const'. */ 5382 5383 if (TYPE_READONLY (lhstype) 5384 || ((TREE_CODE (lhstype) == RECORD_TYPE 5385 || TREE_CODE (lhstype) == UNION_TYPE) 5386 && C_TYPE_FIELDS_READONLY (lhstype))) 5387 { 5388 readonly_error (location, lhs, lv_assign); 5389 return error_mark_node; 5390 } 5391 else if (TREE_READONLY (lhs)) 5392 readonly_warning (lhs, lv_assign); 5393 5394 /* If storing into a structure or union member, 5395 it has probably been given type `int'. 5396 Compute the type that would go with 5397 the actual amount of storage the member occupies. */ 5398 5399 if (TREE_CODE (lhs) == COMPONENT_REF 5400 && (TREE_CODE (lhstype) == INTEGER_TYPE 5401 || TREE_CODE (lhstype) == BOOLEAN_TYPE 5402 || TREE_CODE (lhstype) == REAL_TYPE 5403 || TREE_CODE (lhstype) == ENUMERAL_TYPE)) 5404 lhstype = TREE_TYPE (get_unwidened (lhs, 0)); 5405 5406 /* If storing in a field that is in actuality a short or narrower than one, 5407 we must store in the field in its actual type. */ 5408 5409 if (lhstype != TREE_TYPE (lhs)) 5410 { 5411 lhs = copy_node (lhs); 5412 TREE_TYPE (lhs) = lhstype; 5413 } 5414 5415 /* Issue -Wc++-compat warnings about an assignment to an enum type 5416 when LHS does not have its original type. This happens for, 5417 e.g., an enum bitfield in a struct. */ 5418 if (warn_cxx_compat 5419 && lhs_origtype != NULL_TREE 5420 && lhs_origtype != lhstype 5421 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE) 5422 { 5423 tree checktype = (rhs_origtype != NULL_TREE 5424 ? rhs_origtype 5425 : TREE_TYPE (rhs)); 5426 if (checktype != error_mark_node 5427 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype) 5428 || (is_atomic_op && modifycode != NOP_EXPR))) 5429 warning_at (location, OPT_Wc___compat, 5430 "enum conversion in assignment is invalid in C++"); 5431 } 5432 5433 /* If the lhs is atomic, remove that qualifier. */ 5434 if (is_atomic_op) 5435 { 5436 lhstype = build_qualified_type (lhstype, 5437 (TYPE_QUALS (lhstype) 5438 & ~TYPE_QUAL_ATOMIC)); 5439 olhstype = build_qualified_type (olhstype, 5440 (TYPE_QUALS (lhstype) 5441 & ~TYPE_QUAL_ATOMIC)); 5442 } 5443 5444 /* Convert new value to destination type. Fold it first, then 5445 restore any excess precision information, for the sake of 5446 conversion warnings. */ 5447 5448 if (!(is_atomic_op && modifycode != NOP_EXPR)) 5449 { 5450 npc = null_pointer_constant_p (newrhs); 5451 newrhs = c_fully_fold (newrhs, false, NULL); 5452 if (rhs_semantic_type) 5453 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs); 5454 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs, 5455 rhs_origtype, ic_assign, npc, 5456 NULL_TREE, NULL_TREE, 0); 5457 if (TREE_CODE (newrhs) == ERROR_MARK) 5458 return error_mark_node; 5459 } 5460 5461 /* Emit ObjC write barrier, if necessary. */ 5462 if (c_dialect_objc () && flag_objc_gc) 5463 { 5464 result = objc_generate_write_barrier (lhs, modifycode, newrhs); 5465 if (result) 5466 { 5467 protected_set_expr_location (result, location); 5468 goto return_result; 5469 } 5470 } 5471 5472 /* Scan operands. */ 5473 5474 if (is_atomic_op) 5475 result = build_atomic_assign (location, lhs, modifycode, newrhs, false); 5476 else 5477 { 5478 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs); 5479 TREE_SIDE_EFFECTS (result) = 1; 5480 protected_set_expr_location (result, location); 5481 } 5482 5483 /* If we got the LHS in a different type for storing in, 5484 convert the result back to the nominal type of LHS 5485 so that the value we return always has the same type 5486 as the LHS argument. */ 5487 5488 if (olhstype == TREE_TYPE (result)) 5489 goto return_result; 5490 5491 result = convert_for_assignment (location, rhs_loc, olhstype, result, 5492 rhs_origtype, ic_assign, false, NULL_TREE, 5493 NULL_TREE, 0); 5494 protected_set_expr_location (result, location); 5495 5496return_result: 5497 if (rhseval) 5498 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result); 5499 return result; 5500} 5501 5502/* Return whether STRUCT_TYPE has an anonymous field with type TYPE. 5503 This is used to implement -fplan9-extensions. */ 5504 5505static bool 5506find_anonymous_field_with_type (tree struct_type, tree type) 5507{ 5508 tree field; 5509 bool found; 5510 5511 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE 5512 || TREE_CODE (struct_type) == UNION_TYPE); 5513 found = false; 5514 for (field = TYPE_FIELDS (struct_type); 5515 field != NULL_TREE; 5516 field = TREE_CHAIN (field)) 5517 { 5518 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field)) 5519 ? c_build_qualified_type (TREE_TYPE (field), 5520 TYPE_QUAL_ATOMIC) 5521 : TYPE_MAIN_VARIANT (TREE_TYPE (field))); 5522 if (DECL_NAME (field) == NULL 5523 && comptypes (type, fieldtype)) 5524 { 5525 if (found) 5526 return false; 5527 found = true; 5528 } 5529 else if (DECL_NAME (field) == NULL 5530 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE 5531 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) 5532 && find_anonymous_field_with_type (TREE_TYPE (field), type)) 5533 { 5534 if (found) 5535 return false; 5536 found = true; 5537 } 5538 } 5539 return found; 5540} 5541 5542/* RHS is an expression whose type is pointer to struct. If there is 5543 an anonymous field in RHS with type TYPE, then return a pointer to 5544 that field in RHS. This is used with -fplan9-extensions. This 5545 returns NULL if no conversion could be found. */ 5546 5547static tree 5548convert_to_anonymous_field (location_t location, tree type, tree rhs) 5549{ 5550 tree rhs_struct_type, lhs_main_type; 5551 tree field, found_field; 5552 bool found_sub_field; 5553 tree ret; 5554 5555 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs))); 5556 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs)); 5557 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE 5558 || TREE_CODE (rhs_struct_type) == UNION_TYPE); 5559 5560 gcc_assert (POINTER_TYPE_P (type)); 5561 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type)) 5562 ? c_build_qualified_type (TREE_TYPE (type), 5563 TYPE_QUAL_ATOMIC) 5564 : TYPE_MAIN_VARIANT (TREE_TYPE (type))); 5565 5566 found_field = NULL_TREE; 5567 found_sub_field = false; 5568 for (field = TYPE_FIELDS (rhs_struct_type); 5569 field != NULL_TREE; 5570 field = TREE_CHAIN (field)) 5571 { 5572 if (DECL_NAME (field) != NULL_TREE 5573 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE 5574 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)) 5575 continue; 5576 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field)) 5577 ? c_build_qualified_type (TREE_TYPE (field), 5578 TYPE_QUAL_ATOMIC) 5579 : TYPE_MAIN_VARIANT (TREE_TYPE (field))); 5580 if (comptypes (lhs_main_type, fieldtype)) 5581 { 5582 if (found_field != NULL_TREE) 5583 return NULL_TREE; 5584 found_field = field; 5585 } 5586 else if (find_anonymous_field_with_type (TREE_TYPE (field), 5587 lhs_main_type)) 5588 { 5589 if (found_field != NULL_TREE) 5590 return NULL_TREE; 5591 found_field = field; 5592 found_sub_field = true; 5593 } 5594 } 5595 5596 if (found_field == NULL_TREE) 5597 return NULL_TREE; 5598 5599 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field), 5600 build_fold_indirect_ref (rhs), found_field, 5601 NULL_TREE); 5602 ret = build_fold_addr_expr_loc (location, ret); 5603 5604 if (found_sub_field) 5605 { 5606 ret = convert_to_anonymous_field (location, type, ret); 5607 gcc_assert (ret != NULL_TREE); 5608 } 5609 5610 return ret; 5611} 5612 5613/* Issue an error message for a bad initializer component. 5614 GMSGID identifies the message. 5615 The component name is taken from the spelling stack. */ 5616 5617static void 5618error_init (location_t loc, const char *gmsgid) 5619{ 5620 char *ofwhat; 5621 5622 /* The gmsgid may be a format string with %< and %>. */ 5623 error_at (loc, gmsgid); 5624 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); 5625 if (*ofwhat) 5626 inform (loc, "(near initialization for %qs)", ofwhat); 5627} 5628 5629/* Issue a pedantic warning for a bad initializer component. OPT is 5630 the option OPT_* (from options.h) controlling this warning or 0 if 5631 it is unconditionally given. GMSGID identifies the message. The 5632 component name is taken from the spelling stack. */ 5633 5634static void 5635pedwarn_init (location_t location, int opt, const char *gmsgid) 5636{ 5637 char *ofwhat; 5638 bool warned; 5639 5640 /* The gmsgid may be a format string with %< and %>. */ 5641 warned = pedwarn (location, opt, gmsgid); 5642 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); 5643 if (*ofwhat && warned) 5644 inform (location, "(near initialization for %qs)", ofwhat); 5645} 5646 5647/* Issue a warning for a bad initializer component. 5648 5649 OPT is the OPT_W* value corresponding to the warning option that 5650 controls this warning. GMSGID identifies the message. The 5651 component name is taken from the spelling stack. */ 5652 5653static void 5654warning_init (location_t loc, int opt, const char *gmsgid) 5655{ 5656 char *ofwhat; 5657 bool warned; 5658 5659 /* The gmsgid may be a format string with %< and %>. */ 5660 warned = warning_at (loc, opt, gmsgid); 5661 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); 5662 if (*ofwhat && warned) 5663 inform (loc, "(near initialization for %qs)", ofwhat); 5664} 5665 5666/* If TYPE is an array type and EXPR is a parenthesized string 5667 constant, warn if pedantic that EXPR is being used to initialize an 5668 object of type TYPE. */ 5669 5670void 5671maybe_warn_string_init (location_t loc, tree type, struct c_expr expr) 5672{ 5673 if (pedantic 5674 && TREE_CODE (type) == ARRAY_TYPE 5675 && TREE_CODE (expr.value) == STRING_CST 5676 && expr.original_code != STRING_CST) 5677 pedwarn_init (loc, OPT_Wpedantic, 5678 "array initialized from parenthesized string constant"); 5679} 5680 5681/* Convert value RHS to type TYPE as preparation for an assignment to 5682 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the 5683 original type of RHS; this differs from TREE_TYPE (RHS) for enum 5684 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer 5685 constant before any folding. 5686 The real work of conversion is done by `convert'. 5687 The purpose of this function is to generate error messages 5688 for assignments that are not allowed in C. 5689 ERRTYPE says whether it is argument passing, assignment, 5690 initialization or return. 5691 5692 LOCATION is the location of the assignment, EXPR_LOC is the location of 5693 the RHS or, for a function, location of an argument. 5694 FUNCTION is a tree for the function being called. 5695 PARMNUM is the number of the argument, for printing in error messages. */ 5696 5697static tree 5698convert_for_assignment (location_t location, location_t expr_loc, tree type, 5699 tree rhs, tree origtype, enum impl_conv errtype, 5700 bool null_pointer_constant, tree fundecl, 5701 tree function, int parmnum) 5702{ 5703 enum tree_code codel = TREE_CODE (type); 5704 tree orig_rhs = rhs; 5705 tree rhstype; 5706 enum tree_code coder; 5707 tree rname = NULL_TREE; 5708 bool objc_ok = false; 5709 5710 /* Use the expansion point location to handle cases such as user's 5711 function returning a wrong-type macro defined in a system header. */ 5712 location = expansion_point_location_if_in_system_header (location); 5713 5714 if (errtype == ic_argpass) 5715 { 5716 tree selector; 5717 /* Change pointer to function to the function itself for 5718 diagnostics. */ 5719 if (TREE_CODE (function) == ADDR_EXPR 5720 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) 5721 function = TREE_OPERAND (function, 0); 5722 5723 /* Handle an ObjC selector specially for diagnostics. */ 5724 selector = objc_message_selector (); 5725 rname = function; 5726 if (selector && parmnum > 2) 5727 { 5728 rname = selector; 5729 parmnum -= 2; 5730 } 5731 } 5732 5733 /* This macro is used to emit diagnostics to ensure that all format 5734 strings are complete sentences, visible to gettext and checked at 5735 compile time. */ 5736#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \ 5737 do { \ 5738 switch (errtype) \ 5739 { \ 5740 case ic_argpass: \ 5741 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \ 5742 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ 5743 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \ 5744 "expected %qT but argument is of type %qT", \ 5745 type, rhstype); \ 5746 break; \ 5747 case ic_assign: \ 5748 pedwarn (LOCATION, OPT, AS); \ 5749 break; \ 5750 case ic_init: \ 5751 pedwarn_init (LOCATION, OPT, IN); \ 5752 break; \ 5753 case ic_return: \ 5754 pedwarn (LOCATION, OPT, RE); \ 5755 break; \ 5756 default: \ 5757 gcc_unreachable (); \ 5758 } \ 5759 } while (0) 5760 5761 /* This macro is used to emit diagnostics to ensure that all format 5762 strings are complete sentences, visible to gettext and checked at 5763 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an 5764 extra parameter to enumerate qualifiers. */ 5765#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \ 5766 do { \ 5767 switch (errtype) \ 5768 { \ 5769 case ic_argpass: \ 5770 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \ 5771 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ 5772 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \ 5773 "expected %qT but argument is of type %qT", \ 5774 type, rhstype); \ 5775 break; \ 5776 case ic_assign: \ 5777 pedwarn (LOCATION, OPT, AS, QUALS); \ 5778 break; \ 5779 case ic_init: \ 5780 pedwarn (LOCATION, OPT, IN, QUALS); \ 5781 break; \ 5782 case ic_return: \ 5783 pedwarn (LOCATION, OPT, RE, QUALS); \ 5784 break; \ 5785 default: \ 5786 gcc_unreachable (); \ 5787 } \ 5788 } while (0) 5789 5790 /* This macro is used to emit diagnostics to ensure that all format 5791 strings are complete sentences, visible to gettext and checked at 5792 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses 5793 warning_at instead of pedwarn. */ 5794#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \ 5795 do { \ 5796 switch (errtype) \ 5797 { \ 5798 case ic_argpass: \ 5799 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \ 5800 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ 5801 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \ 5802 "expected %qT but argument is of type %qT", \ 5803 type, rhstype); \ 5804 break; \ 5805 case ic_assign: \ 5806 warning_at (LOCATION, OPT, AS, QUALS); \ 5807 break; \ 5808 case ic_init: \ 5809 warning_at (LOCATION, OPT, IN, QUALS); \ 5810 break; \ 5811 case ic_return: \ 5812 warning_at (LOCATION, OPT, RE, QUALS); \ 5813 break; \ 5814 default: \ 5815 gcc_unreachable (); \ 5816 } \ 5817 } while (0) 5818 5819 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) 5820 rhs = TREE_OPERAND (rhs, 0); 5821 5822 rhstype = TREE_TYPE (rhs); 5823 coder = TREE_CODE (rhstype); 5824 5825 if (coder == ERROR_MARK) 5826 return error_mark_node; 5827 5828 if (c_dialect_objc ()) 5829 { 5830 int parmno; 5831 5832 switch (errtype) 5833 { 5834 case ic_return: 5835 parmno = 0; 5836 break; 5837 5838 case ic_assign: 5839 parmno = -1; 5840 break; 5841 5842 case ic_init: 5843 parmno = -2; 5844 break; 5845 5846 default: 5847 parmno = parmnum; 5848 break; 5849 } 5850 5851 objc_ok = objc_compare_types (type, rhstype, parmno, rname); 5852 } 5853 5854 if (warn_cxx_compat) 5855 { 5856 tree checktype = origtype != NULL_TREE ? origtype : rhstype; 5857 if (checktype != error_mark_node 5858 && TREE_CODE (type) == ENUMERAL_TYPE 5859 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type)) 5860 { 5861 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat, 5862 G_("enum conversion when passing argument " 5863 "%d of %qE is invalid in C++"), 5864 G_("enum conversion in assignment is " 5865 "invalid in C++"), 5866 G_("enum conversion in initialization is " 5867 "invalid in C++"), 5868 G_("enum conversion in return is " 5869 "invalid in C++")); 5870 } 5871 } 5872 5873 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) 5874 return rhs; 5875 5876 if (coder == VOID_TYPE) 5877 { 5878 /* Except for passing an argument to an unprototyped function, 5879 this is a constraint violation. When passing an argument to 5880 an unprototyped function, it is compile-time undefined; 5881 making it a constraint in that case was rejected in 5882 DR#252. */ 5883 error_at (location, "void value not ignored as it ought to be"); 5884 return error_mark_node; 5885 } 5886 rhs = require_complete_type (rhs); 5887 if (rhs == error_mark_node) 5888 return error_mark_node; 5889 /* A non-reference type can convert to a reference. This handles 5890 va_start, va_copy and possibly port built-ins. */ 5891 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE) 5892 { 5893 if (!lvalue_p (rhs)) 5894 { 5895 error_at (location, "cannot pass rvalue to reference parameter"); 5896 return error_mark_node; 5897 } 5898 if (!c_mark_addressable (rhs)) 5899 return error_mark_node; 5900 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs); 5901 SET_EXPR_LOCATION (rhs, location); 5902 5903 rhs = convert_for_assignment (location, expr_loc, 5904 build_pointer_type (TREE_TYPE (type)), 5905 rhs, origtype, errtype, 5906 null_pointer_constant, fundecl, function, 5907 parmnum); 5908 if (rhs == error_mark_node) 5909 return error_mark_node; 5910 5911 rhs = build1 (NOP_EXPR, type, rhs); 5912 SET_EXPR_LOCATION (rhs, location); 5913 return rhs; 5914 } 5915 /* Some types can interconvert without explicit casts. */ 5916 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE 5917 && vector_types_convertible_p (type, TREE_TYPE (rhs), true)) 5918 return convert (type, rhs); 5919 /* Arithmetic types all interconvert, and enum is treated like int. */ 5920 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE 5921 || codel == FIXED_POINT_TYPE 5922 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE 5923 || codel == BOOLEAN_TYPE) 5924 && (coder == INTEGER_TYPE || coder == REAL_TYPE 5925 || coder == FIXED_POINT_TYPE 5926 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE 5927 || coder == BOOLEAN_TYPE)) 5928 { 5929 tree ret; 5930 bool save = in_late_binary_op; 5931 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE 5932 || (coder == REAL_TYPE 5933 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE) 5934 && (flag_sanitize & SANITIZE_FLOAT_CAST))) 5935 in_late_binary_op = true; 5936 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION 5937 ? expr_loc : location, type, orig_rhs); 5938 in_late_binary_op = save; 5939 return ret; 5940 } 5941 5942 /* Aggregates in different TUs might need conversion. */ 5943 if ((codel == RECORD_TYPE || codel == UNION_TYPE) 5944 && codel == coder 5945 && comptypes (type, rhstype)) 5946 return convert_and_check (expr_loc != UNKNOWN_LOCATION 5947 ? expr_loc : location, type, rhs); 5948 5949 /* Conversion to a transparent union or record from its member types. 5950 This applies only to function arguments. */ 5951 if (((codel == UNION_TYPE || codel == RECORD_TYPE) 5952 && TYPE_TRANSPARENT_AGGR (type)) 5953 && errtype == ic_argpass) 5954 { 5955 tree memb, marginal_memb = NULL_TREE; 5956 5957 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb)) 5958 { 5959 tree memb_type = TREE_TYPE (memb); 5960 5961 if (comptypes (TYPE_MAIN_VARIANT (memb_type), 5962 TYPE_MAIN_VARIANT (rhstype))) 5963 break; 5964 5965 if (TREE_CODE (memb_type) != POINTER_TYPE) 5966 continue; 5967 5968 if (coder == POINTER_TYPE) 5969 { 5970 tree ttl = TREE_TYPE (memb_type); 5971 tree ttr = TREE_TYPE (rhstype); 5972 5973 /* Any non-function converts to a [const][volatile] void * 5974 and vice versa; otherwise, targets must be the same. 5975 Meanwhile, the lhs target must have all the qualifiers of 5976 the rhs. */ 5977 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl)) 5978 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr)) 5979 || comp_target_types (location, memb_type, rhstype)) 5980 { 5981 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC; 5982 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC; 5983 /* If this type won't generate any warnings, use it. */ 5984 if (lquals == rquals 5985 || ((TREE_CODE (ttr) == FUNCTION_TYPE 5986 && TREE_CODE (ttl) == FUNCTION_TYPE) 5987 ? ((lquals | rquals) == rquals) 5988 : ((lquals | rquals) == lquals))) 5989 break; 5990 5991 /* Keep looking for a better type, but remember this one. */ 5992 if (!marginal_memb) 5993 marginal_memb = memb; 5994 } 5995 } 5996 5997 /* Can convert integer zero to any pointer type. */ 5998 if (null_pointer_constant) 5999 { 6000 rhs = null_pointer_node; 6001 break; 6002 } 6003 } 6004 6005 if (memb || marginal_memb) 6006 { 6007 if (!memb) 6008 { 6009 /* We have only a marginally acceptable member type; 6010 it needs a warning. */ 6011 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb)); 6012 tree ttr = TREE_TYPE (rhstype); 6013 6014 /* Const and volatile mean something different for function 6015 types, so the usual warnings are not appropriate. */ 6016 if (TREE_CODE (ttr) == FUNCTION_TYPE 6017 && TREE_CODE (ttl) == FUNCTION_TYPE) 6018 { 6019 /* Because const and volatile on functions are 6020 restrictions that say the function will not do 6021 certain things, it is okay to use a const or volatile 6022 function where an ordinary one is wanted, but not 6023 vice-versa. */ 6024 if (TYPE_QUALS_NO_ADDR_SPACE (ttl) 6025 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) 6026 PEDWARN_FOR_QUALIFIERS (location, expr_loc, 6027 OPT_Wdiscarded_qualifiers, 6028 G_("passing argument %d of %qE " 6029 "makes %q#v qualified function " 6030 "pointer from unqualified"), 6031 G_("assignment makes %q#v qualified " 6032 "function pointer from " 6033 "unqualified"), 6034 G_("initialization makes %q#v qualified " 6035 "function pointer from " 6036 "unqualified"), 6037 G_("return makes %q#v qualified function " 6038 "pointer from unqualified"), 6039 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); 6040 } 6041 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr) 6042 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) 6043 PEDWARN_FOR_QUALIFIERS (location, expr_loc, 6044 OPT_Wdiscarded_qualifiers, 6045 G_("passing argument %d of %qE discards " 6046 "%qv qualifier from pointer target type"), 6047 G_("assignment discards %qv qualifier " 6048 "from pointer target type"), 6049 G_("initialization discards %qv qualifier " 6050 "from pointer target type"), 6051 G_("return discards %qv qualifier from " 6052 "pointer target type"), 6053 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); 6054 6055 memb = marginal_memb; 6056 } 6057 6058 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)) 6059 pedwarn (location, OPT_Wpedantic, 6060 "ISO C prohibits argument conversion to union type"); 6061 6062 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs); 6063 return build_constructor_single (type, memb, rhs); 6064 } 6065 } 6066 6067 /* Conversions among pointers */ 6068 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) 6069 && (coder == codel)) 6070 { 6071 tree ttl = TREE_TYPE (type); 6072 tree ttr = TREE_TYPE (rhstype); 6073 tree mvl = ttl; 6074 tree mvr = ttr; 6075 bool is_opaque_pointer; 6076 int target_cmp = 0; /* Cache comp_target_types () result. */ 6077 addr_space_t asl; 6078 addr_space_t asr; 6079 6080 if (TREE_CODE (mvl) != ARRAY_TYPE) 6081 mvl = (TYPE_ATOMIC (mvl) 6082 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), 6083 TYPE_QUAL_ATOMIC) 6084 : TYPE_MAIN_VARIANT (mvl)); 6085 if (TREE_CODE (mvr) != ARRAY_TYPE) 6086 mvr = (TYPE_ATOMIC (mvr) 6087 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), 6088 TYPE_QUAL_ATOMIC) 6089 : TYPE_MAIN_VARIANT (mvr)); 6090 /* Opaque pointers are treated like void pointers. */ 6091 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr); 6092 6093 /* The Plan 9 compiler permits a pointer to a struct to be 6094 automatically converted into a pointer to an anonymous field 6095 within the struct. */ 6096 if (flag_plan9_extensions 6097 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE) 6098 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE) 6099 && mvl != mvr) 6100 { 6101 tree new_rhs = convert_to_anonymous_field (location, type, rhs); 6102 if (new_rhs != NULL_TREE) 6103 { 6104 rhs = new_rhs; 6105 rhstype = TREE_TYPE (rhs); 6106 coder = TREE_CODE (rhstype); 6107 ttr = TREE_TYPE (rhstype); 6108 mvr = TYPE_MAIN_VARIANT (ttr); 6109 } 6110 } 6111 6112 /* C++ does not allow the implicit conversion void* -> T*. However, 6113 for the purpose of reducing the number of false positives, we 6114 tolerate the special case of 6115 6116 int *p = NULL; 6117 6118 where NULL is typically defined in C to be '(void *) 0'. */ 6119 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl)) 6120 warning_at (errtype == ic_argpass ? expr_loc : location, 6121 OPT_Wc___compat, 6122 "request for implicit conversion " 6123 "from %qT to %qT not permitted in C++", rhstype, type); 6124 6125 /* See if the pointers point to incompatible address spaces. */ 6126 asl = TYPE_ADDR_SPACE (ttl); 6127 asr = TYPE_ADDR_SPACE (ttr); 6128 if (!null_pointer_constant_p (rhs) 6129 && asr != asl && !targetm.addr_space.subset_p (asr, asl)) 6130 { 6131 switch (errtype) 6132 { 6133 case ic_argpass: 6134 error_at (expr_loc, "passing argument %d of %qE from pointer to " 6135 "non-enclosed address space", parmnum, rname); 6136 break; 6137 case ic_assign: 6138 error_at (location, "assignment from pointer to " 6139 "non-enclosed address space"); 6140 break; 6141 case ic_init: 6142 error_at (location, "initialization from pointer to " 6143 "non-enclosed address space"); 6144 break; 6145 case ic_return: 6146 error_at (location, "return from pointer to " 6147 "non-enclosed address space"); 6148 break; 6149 default: 6150 gcc_unreachable (); 6151 } 6152 return error_mark_node; 6153 } 6154 6155 /* Check if the right-hand side has a format attribute but the 6156 left-hand side doesn't. */ 6157 if (warn_suggest_attribute_format 6158 && check_missing_format_attribute (type, rhstype)) 6159 { 6160 switch (errtype) 6161 { 6162 case ic_argpass: 6163 warning_at (expr_loc, OPT_Wsuggest_attribute_format, 6164 "argument %d of %qE might be " 6165 "a candidate for a format attribute", 6166 parmnum, rname); 6167 break; 6168 case ic_assign: 6169 warning_at (location, OPT_Wsuggest_attribute_format, 6170 "assignment left-hand side might be " 6171 "a candidate for a format attribute"); 6172 break; 6173 case ic_init: 6174 warning_at (location, OPT_Wsuggest_attribute_format, 6175 "initialization left-hand side might be " 6176 "a candidate for a format attribute"); 6177 break; 6178 case ic_return: 6179 warning_at (location, OPT_Wsuggest_attribute_format, 6180 "return type might be " 6181 "a candidate for a format attribute"); 6182 break; 6183 default: 6184 gcc_unreachable (); 6185 } 6186 } 6187 6188 /* Any non-function converts to a [const][volatile] void * 6189 and vice versa; otherwise, targets must be the same. 6190 Meanwhile, the lhs target must have all the qualifiers of the rhs. */ 6191 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl)) 6192 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr)) 6193 || (target_cmp = comp_target_types (location, type, rhstype)) 6194 || is_opaque_pointer 6195 || ((c_common_unsigned_type (mvl) 6196 == c_common_unsigned_type (mvr)) 6197 && (c_common_signed_type (mvl) 6198 == c_common_signed_type (mvr)) 6199 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr))) 6200 { 6201 /* Warn about loss of qualifers from pointers to arrays with 6202 qualifiers on the element type. */ 6203 if (TREE_CODE (ttr) == ARRAY_TYPE) 6204 { 6205 ttr = strip_array_types (ttr); 6206 ttl = strip_array_types (ttl); 6207 6208 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr) 6209 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl)) 6210 WARNING_FOR_QUALIFIERS (location, expr_loc, 6211 OPT_Wdiscarded_array_qualifiers, 6212 G_("passing argument %d of %qE discards " 6213 "%qv qualifier from pointer target type"), 6214 G_("assignment discards %qv qualifier " 6215 "from pointer target type"), 6216 G_("initialization discards %qv qualifier " 6217 "from pointer target type"), 6218 G_("return discards %qv qualifier from " 6219 "pointer target type"), 6220 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); 6221 } 6222 else if (pedantic 6223 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE) 6224 || 6225 (VOID_TYPE_P (ttr) 6226 && !null_pointer_constant 6227 && TREE_CODE (ttl) == FUNCTION_TYPE))) 6228 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic, 6229 G_("ISO C forbids passing argument %d of " 6230 "%qE between function pointer " 6231 "and %<void *%>"), 6232 G_("ISO C forbids assignment between " 6233 "function pointer and %<void *%>"), 6234 G_("ISO C forbids initialization between " 6235 "function pointer and %<void *%>"), 6236 G_("ISO C forbids return between function " 6237 "pointer and %<void *%>")); 6238 /* Const and volatile mean something different for function types, 6239 so the usual warnings are not appropriate. */ 6240 else if (TREE_CODE (ttr) != FUNCTION_TYPE 6241 && TREE_CODE (ttl) != FUNCTION_TYPE) 6242 { 6243 /* Don't warn about loss of qualifier for conversions from 6244 qualified void* to pointers to arrays with corresponding 6245 qualifier on the element type. */ 6246 if (!pedantic) 6247 ttl = strip_array_types (ttl); 6248 6249 /* Assignments between atomic and non-atomic objects are OK. */ 6250 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr) 6251 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl)) 6252 { 6253 PEDWARN_FOR_QUALIFIERS (location, expr_loc, 6254 OPT_Wdiscarded_qualifiers, 6255 G_("passing argument %d of %qE discards " 6256 "%qv qualifier from pointer target type"), 6257 G_("assignment discards %qv qualifier " 6258 "from pointer target type"), 6259 G_("initialization discards %qv qualifier " 6260 "from pointer target type"), 6261 G_("return discards %qv qualifier from " 6262 "pointer target type"), 6263 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); 6264 } 6265 /* If this is not a case of ignoring a mismatch in signedness, 6266 no warning. */ 6267 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) 6268 || target_cmp) 6269 ; 6270 /* If there is a mismatch, do warn. */ 6271 else if (warn_pointer_sign) 6272 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign, 6273 G_("pointer targets in passing argument " 6274 "%d of %qE differ in signedness"), 6275 G_("pointer targets in assignment " 6276 "differ in signedness"), 6277 G_("pointer targets in initialization " 6278 "differ in signedness"), 6279 G_("pointer targets in return differ " 6280 "in signedness")); 6281 } 6282 else if (TREE_CODE (ttl) == FUNCTION_TYPE 6283 && TREE_CODE (ttr) == FUNCTION_TYPE) 6284 { 6285 /* Because const and volatile on functions are restrictions 6286 that say the function will not do certain things, 6287 it is okay to use a const or volatile function 6288 where an ordinary one is wanted, but not vice-versa. */ 6289 if (TYPE_QUALS_NO_ADDR_SPACE (ttl) 6290 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) 6291 PEDWARN_FOR_QUALIFIERS (location, expr_loc, 6292 OPT_Wdiscarded_qualifiers, 6293 G_("passing argument %d of %qE makes " 6294 "%q#v qualified function pointer " 6295 "from unqualified"), 6296 G_("assignment makes %q#v qualified function " 6297 "pointer from unqualified"), 6298 G_("initialization makes %q#v qualified " 6299 "function pointer from unqualified"), 6300 G_("return makes %q#v qualified function " 6301 "pointer from unqualified"), 6302 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); 6303 } 6304 } 6305 else 6306 /* Avoid warning about the volatile ObjC EH puts on decls. */ 6307 if (!objc_ok) 6308 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, 6309 OPT_Wincompatible_pointer_types, 6310 G_("passing argument %d of %qE from " 6311 "incompatible pointer type"), 6312 G_("assignment from incompatible pointer type"), 6313 G_("initialization from incompatible " 6314 "pointer type"), 6315 G_("return from incompatible pointer type")); 6316 6317 return convert (type, rhs); 6318 } 6319 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE) 6320 { 6321 /* ??? This should not be an error when inlining calls to 6322 unprototyped functions. */ 6323 error_at (location, "invalid use of non-lvalue array"); 6324 return error_mark_node; 6325 } 6326 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) 6327 { 6328 /* An explicit constant 0 can convert to a pointer, 6329 or one that results from arithmetic, even including 6330 a cast to integer type. */ 6331 if (!null_pointer_constant) 6332 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, 6333 OPT_Wint_conversion, 6334 G_("passing argument %d of %qE makes " 6335 "pointer from integer without a cast"), 6336 G_("assignment makes pointer from integer " 6337 "without a cast"), 6338 G_("initialization makes pointer from " 6339 "integer without a cast"), 6340 G_("return makes pointer from integer " 6341 "without a cast")); 6342 6343 return convert (type, rhs); 6344 } 6345 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) 6346 { 6347 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, 6348 OPT_Wint_conversion, 6349 G_("passing argument %d of %qE makes integer " 6350 "from pointer without a cast"), 6351 G_("assignment makes integer from pointer " 6352 "without a cast"), 6353 G_("initialization makes integer from pointer " 6354 "without a cast"), 6355 G_("return makes integer from pointer " 6356 "without a cast")); 6357 return convert (type, rhs); 6358 } 6359 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE) 6360 { 6361 tree ret; 6362 bool save = in_late_binary_op; 6363 in_late_binary_op = true; 6364 ret = convert (type, rhs); 6365 in_late_binary_op = save; 6366 return ret; 6367 } 6368 6369 switch (errtype) 6370 { 6371 case ic_argpass: 6372 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum, 6373 rname); 6374 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) 6375 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc, 6376 "expected %qT but argument is of type %qT", type, rhstype); 6377 break; 6378 case ic_assign: 6379 error_at (location, "incompatible types when assigning to type %qT from " 6380 "type %qT", type, rhstype); 6381 break; 6382 case ic_init: 6383 error_at (location, 6384 "incompatible types when initializing type %qT using type %qT", 6385 type, rhstype); 6386 break; 6387 case ic_return: 6388 error_at (location, 6389 "incompatible types when returning type %qT but %qT was " 6390 "expected", rhstype, type); 6391 break; 6392 default: 6393 gcc_unreachable (); 6394 } 6395 6396 return error_mark_node; 6397} 6398 6399/* If VALUE is a compound expr all of whose expressions are constant, then 6400 return its value. Otherwise, return error_mark_node. 6401 6402 This is for handling COMPOUND_EXPRs as initializer elements 6403 which is allowed with a warning when -pedantic is specified. */ 6404 6405static tree 6406valid_compound_expr_initializer (tree value, tree endtype) 6407{ 6408 if (TREE_CODE (value) == COMPOUND_EXPR) 6409 { 6410 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) 6411 == error_mark_node) 6412 return error_mark_node; 6413 return valid_compound_expr_initializer (TREE_OPERAND (value, 1), 6414 endtype); 6415 } 6416 else if (!initializer_constant_valid_p (value, endtype)) 6417 return error_mark_node; 6418 else 6419 return value; 6420} 6421 6422/* Perform appropriate conversions on the initial value of a variable, 6423 store it in the declaration DECL, 6424 and print any error messages that are appropriate. 6425 If ORIGTYPE is not NULL_TREE, it is the original type of INIT. 6426 If the init is invalid, store an ERROR_MARK. 6427 6428 INIT_LOC is the location of the initial value. */ 6429 6430void 6431store_init_value (location_t init_loc, tree decl, tree init, tree origtype) 6432{ 6433 tree value, type; 6434 bool npc = false; 6435 6436 /* If variable's type was invalidly declared, just ignore it. */ 6437 6438 type = TREE_TYPE (decl); 6439 if (TREE_CODE (type) == ERROR_MARK) 6440 return; 6441 6442 /* Digest the specified initializer into an expression. */ 6443 6444 if (init) 6445 npc = null_pointer_constant_p (init); 6446 value = digest_init (init_loc, type, init, origtype, npc, 6447 true, TREE_STATIC (decl)); 6448 6449 /* Store the expression if valid; else report error. */ 6450 6451 if (!in_system_header_at (input_location) 6452 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl)) 6453 warning (OPT_Wtraditional, "traditional C rejects automatic " 6454 "aggregate initialization"); 6455 6456 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL) 6457 DECL_INITIAL (decl) = value; 6458 6459 /* ANSI wants warnings about out-of-range constant initializers. */ 6460 STRIP_TYPE_NOPS (value); 6461 if (TREE_STATIC (decl)) 6462 constant_expression_warning (value); 6463 6464 /* Check if we need to set array size from compound literal size. */ 6465 if (TREE_CODE (type) == ARRAY_TYPE 6466 && TYPE_DOMAIN (type) == 0 6467 && value != error_mark_node) 6468 { 6469 tree inside_init = init; 6470 6471 STRIP_TYPE_NOPS (inside_init); 6472 inside_init = fold (inside_init); 6473 6474 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) 6475 { 6476 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init); 6477 6478 if (TYPE_DOMAIN (TREE_TYPE (cldecl))) 6479 { 6480 /* For int foo[] = (int [3]){1}; we need to set array size 6481 now since later on array initializer will be just the 6482 brace enclosed list of the compound literal. */ 6483 tree etype = strip_array_types (TREE_TYPE (decl)); 6484 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); 6485 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl)); 6486 layout_type (type); 6487 layout_decl (cldecl, 0); 6488 TREE_TYPE (decl) 6489 = c_build_qualified_type (type, TYPE_QUALS (etype)); 6490 } 6491 } 6492 } 6493} 6494 6495/* Methods for storing and printing names for error messages. */ 6496 6497/* Implement a spelling stack that allows components of a name to be pushed 6498 and popped. Each element on the stack is this structure. */ 6499 6500struct spelling 6501{ 6502 int kind; 6503 union 6504 { 6505 unsigned HOST_WIDE_INT i; 6506 const char *s; 6507 } u; 6508}; 6509 6510#define SPELLING_STRING 1 6511#define SPELLING_MEMBER 2 6512#define SPELLING_BOUNDS 3 6513 6514static struct spelling *spelling; /* Next stack element (unused). */ 6515static struct spelling *spelling_base; /* Spelling stack base. */ 6516static int spelling_size; /* Size of the spelling stack. */ 6517 6518/* Macros to save and restore the spelling stack around push_... functions. 6519 Alternative to SAVE_SPELLING_STACK. */ 6520 6521#define SPELLING_DEPTH() (spelling - spelling_base) 6522#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH)) 6523 6524/* Push an element on the spelling stack with type KIND and assign VALUE 6525 to MEMBER. */ 6526 6527#define PUSH_SPELLING(KIND, VALUE, MEMBER) \ 6528{ \ 6529 int depth = SPELLING_DEPTH (); \ 6530 \ 6531 if (depth >= spelling_size) \ 6532 { \ 6533 spelling_size += 10; \ 6534 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \ 6535 spelling_size); \ 6536 RESTORE_SPELLING_DEPTH (depth); \ 6537 } \ 6538 \ 6539 spelling->kind = (KIND); \ 6540 spelling->MEMBER = (VALUE); \ 6541 spelling++; \ 6542} 6543 6544/* Push STRING on the stack. Printed literally. */ 6545 6546static void 6547push_string (const char *string) 6548{ 6549 PUSH_SPELLING (SPELLING_STRING, string, u.s); 6550} 6551 6552/* Push a member name on the stack. Printed as '.' STRING. */ 6553 6554static void 6555push_member_name (tree decl) 6556{ 6557 const char *const string 6558 = (DECL_NAME (decl) 6559 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))) 6560 : _("<anonymous>")); 6561 PUSH_SPELLING (SPELLING_MEMBER, string, u.s); 6562} 6563 6564/* Push an array bounds on the stack. Printed as [BOUNDS]. */ 6565 6566static void 6567push_array_bounds (unsigned HOST_WIDE_INT bounds) 6568{ 6569 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); 6570} 6571 6572/* Compute the maximum size in bytes of the printed spelling. */ 6573 6574static int 6575spelling_length (void) 6576{ 6577 int size = 0; 6578 struct spelling *p; 6579 6580 for (p = spelling_base; p < spelling; p++) 6581 { 6582 if (p->kind == SPELLING_BOUNDS) 6583 size += 25; 6584 else 6585 size += strlen (p->u.s) + 1; 6586 } 6587 6588 return size; 6589} 6590 6591/* Print the spelling to BUFFER and return it. */ 6592 6593static char * 6594print_spelling (char *buffer) 6595{ 6596 char *d = buffer; 6597 struct spelling *p; 6598 6599 for (p = spelling_base; p < spelling; p++) 6600 if (p->kind == SPELLING_BOUNDS) 6601 { 6602 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i); 6603 d += strlen (d); 6604 } 6605 else 6606 { 6607 const char *s; 6608 if (p->kind == SPELLING_MEMBER) 6609 *d++ = '.'; 6610 for (s = p->u.s; (*d = *s++); d++) 6611 ; 6612 } 6613 *d++ = '\0'; 6614 return buffer; 6615} 6616 6617/* Digest the parser output INIT as an initializer for type TYPE. 6618 Return a C expression of type TYPE to represent the initial value. 6619 6620 If ORIGTYPE is not NULL_TREE, it is the original type of INIT. 6621 6622 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant. 6623 6624 If INIT is a string constant, STRICT_STRING is true if it is 6625 unparenthesized or we should not warn here for it being parenthesized. 6626 For other types of INIT, STRICT_STRING is not used. 6627 6628 INIT_LOC is the location of the INIT. 6629 6630 REQUIRE_CONSTANT requests an error if non-constant initializers or 6631 elements are seen. */ 6632 6633static tree 6634digest_init (location_t init_loc, tree type, tree init, tree origtype, 6635 bool null_pointer_constant, bool strict_string, 6636 int require_constant) 6637{ 6638 enum tree_code code = TREE_CODE (type); 6639 tree inside_init = init; 6640 tree semantic_type = NULL_TREE; 6641 bool maybe_const = true; 6642 6643 if (type == error_mark_node 6644 || !init 6645 || error_operand_p (init)) 6646 return error_mark_node; 6647 6648 STRIP_TYPE_NOPS (inside_init); 6649 6650 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR) 6651 { 6652 semantic_type = TREE_TYPE (inside_init); 6653 inside_init = TREE_OPERAND (inside_init, 0); 6654 } 6655 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const); 6656 inside_init = decl_constant_value_for_optimization (inside_init); 6657 6658 /* Initialization of an array of chars from a string constant 6659 optionally enclosed in braces. */ 6660 6661 if (code == ARRAY_TYPE && inside_init 6662 && TREE_CODE (inside_init) == STRING_CST) 6663 { 6664 tree typ1 6665 = (TYPE_ATOMIC (TREE_TYPE (type)) 6666 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)), 6667 TYPE_QUAL_ATOMIC) 6668 : TYPE_MAIN_VARIANT (TREE_TYPE (type))); 6669 /* Note that an array could be both an array of character type 6670 and an array of wchar_t if wchar_t is signed char or unsigned 6671 char. */ 6672 bool char_array = (typ1 == char_type_node 6673 || typ1 == signed_char_type_node 6674 || typ1 == unsigned_char_type_node); 6675 bool wchar_array = !!comptypes (typ1, wchar_type_node); 6676 bool char16_array = !!comptypes (typ1, char16_type_node); 6677 bool char32_array = !!comptypes (typ1, char32_type_node); 6678 6679 if (char_array || wchar_array || char16_array || char32_array) 6680 { 6681 struct c_expr expr; 6682 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))); 6683 expr.value = inside_init; 6684 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK); 6685 expr.original_type = NULL; 6686 maybe_warn_string_init (init_loc, type, expr); 6687 6688 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type))) 6689 pedwarn_init (init_loc, OPT_Wpedantic, 6690 "initialization of a flexible array member"); 6691 6692 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), 6693 TYPE_MAIN_VARIANT (type))) 6694 return inside_init; 6695 6696 if (char_array) 6697 { 6698 if (typ2 != char_type_node) 6699 { 6700 error_init (init_loc, "char-array initialized from wide " 6701 "string"); 6702 return error_mark_node; 6703 } 6704 } 6705 else 6706 { 6707 if (typ2 == char_type_node) 6708 { 6709 error_init (init_loc, "wide character array initialized " 6710 "from non-wide string"); 6711 return error_mark_node; 6712 } 6713 else if (!comptypes(typ1, typ2)) 6714 { 6715 error_init (init_loc, "wide character array initialized " 6716 "from incompatible wide string"); 6717 return error_mark_node; 6718 } 6719 } 6720 6721 TREE_TYPE (inside_init) = type; 6722 if (TYPE_DOMAIN (type) != 0 6723 && TYPE_SIZE (type) != 0 6724 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) 6725 { 6726 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init); 6727 6728 /* Subtract the size of a single (possibly wide) character 6729 because it's ok to ignore the terminating null char 6730 that is counted in the length of the constant. */ 6731 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type), 6732 (len 6733 - (TYPE_PRECISION (typ1) 6734 / BITS_PER_UNIT)))) 6735 pedwarn_init (init_loc, 0, 6736 ("initializer-string for array of chars " 6737 "is too long")); 6738 else if (warn_cxx_compat 6739 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len)) 6740 warning_at (init_loc, OPT_Wc___compat, 6741 ("initializer-string for array chars " 6742 "is too long for C++")); 6743 } 6744 6745 return inside_init; 6746 } 6747 else if (INTEGRAL_TYPE_P (typ1)) 6748 { 6749 error_init (init_loc, "array of inappropriate type initialized " 6750 "from string constant"); 6751 return error_mark_node; 6752 } 6753 } 6754 6755 /* Build a VECTOR_CST from a *constant* vector constructor. If the 6756 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt 6757 below and handle as a constructor. */ 6758 if (code == VECTOR_TYPE 6759 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE 6760 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true) 6761 && TREE_CONSTANT (inside_init)) 6762 { 6763 if (TREE_CODE (inside_init) == VECTOR_CST 6764 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), 6765 TYPE_MAIN_VARIANT (type))) 6766 return inside_init; 6767 6768 if (TREE_CODE (inside_init) == CONSTRUCTOR) 6769 { 6770 unsigned HOST_WIDE_INT ix; 6771 tree value; 6772 bool constant_p = true; 6773 6774 /* Iterate through elements and check if all constructor 6775 elements are *_CSTs. */ 6776 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value) 6777 if (!CONSTANT_CLASS_P (value)) 6778 { 6779 constant_p = false; 6780 break; 6781 } 6782 6783 if (constant_p) 6784 return build_vector_from_ctor (type, 6785 CONSTRUCTOR_ELTS (inside_init)); 6786 } 6787 } 6788 6789 if (warn_sequence_point) 6790 verify_sequence_points (inside_init); 6791 6792 /* Any type can be initialized 6793 from an expression of the same type, optionally with braces. */ 6794 6795 if (inside_init && TREE_TYPE (inside_init) != 0 6796 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), 6797 TYPE_MAIN_VARIANT (type)) 6798 || (code == ARRAY_TYPE 6799 && comptypes (TREE_TYPE (inside_init), type)) 6800 || (code == VECTOR_TYPE 6801 && comptypes (TREE_TYPE (inside_init), type)) 6802 || (code == POINTER_TYPE 6803 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE 6804 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), 6805 TREE_TYPE (type))))) 6806 { 6807 if (code == POINTER_TYPE) 6808 { 6809 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE) 6810 { 6811 if (TREE_CODE (inside_init) == STRING_CST 6812 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) 6813 inside_init = array_to_pointer_conversion 6814 (init_loc, inside_init); 6815 else 6816 { 6817 error_init (init_loc, "invalid use of non-lvalue array"); 6818 return error_mark_node; 6819 } 6820 } 6821 } 6822 6823 if (code == VECTOR_TYPE) 6824 /* Although the types are compatible, we may require a 6825 conversion. */ 6826 inside_init = convert (type, inside_init); 6827 6828 if (require_constant 6829 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) 6830 { 6831 /* As an extension, allow initializing objects with static storage 6832 duration with compound literals (which are then treated just as 6833 the brace enclosed list they contain). Also allow this for 6834 vectors, as we can only assign them with compound literals. */ 6835 if (flag_isoc99 && code != VECTOR_TYPE) 6836 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element " 6837 "is not constant"); 6838 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init); 6839 inside_init = DECL_INITIAL (decl); 6840 } 6841 6842 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST 6843 && TREE_CODE (inside_init) != CONSTRUCTOR) 6844 { 6845 error_init (init_loc, "array initialized from non-constant array " 6846 "expression"); 6847 return error_mark_node; 6848 } 6849 6850 /* Compound expressions can only occur here if -Wpedantic or 6851 -pedantic-errors is specified. In the later case, we always want 6852 an error. In the former case, we simply want a warning. */ 6853 if (require_constant && pedantic 6854 && TREE_CODE (inside_init) == COMPOUND_EXPR) 6855 { 6856 inside_init 6857 = valid_compound_expr_initializer (inside_init, 6858 TREE_TYPE (inside_init)); 6859 if (inside_init == error_mark_node) 6860 error_init (init_loc, "initializer element is not constant"); 6861 else 6862 pedwarn_init (init_loc, OPT_Wpedantic, 6863 "initializer element is not constant"); 6864 if (flag_pedantic_errors) 6865 inside_init = error_mark_node; 6866 } 6867 else if (require_constant 6868 && !initializer_constant_valid_p (inside_init, 6869 TREE_TYPE (inside_init))) 6870 { 6871 error_init (init_loc, "initializer element is not constant"); 6872 inside_init = error_mark_node; 6873 } 6874 else if (require_constant && !maybe_const) 6875 pedwarn_init (init_loc, 0, 6876 "initializer element is not a constant expression"); 6877 6878 /* Added to enable additional -Wsuggest-attribute=format warnings. */ 6879 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE) 6880 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION, 6881 type, inside_init, origtype, 6882 ic_init, null_pointer_constant, 6883 NULL_TREE, NULL_TREE, 0); 6884 return inside_init; 6885 } 6886 6887 /* Handle scalar types, including conversions. */ 6888 6889 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE 6890 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE 6891 || code == COMPLEX_TYPE || code == VECTOR_TYPE) 6892 { 6893 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE 6894 && (TREE_CODE (init) == STRING_CST 6895 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR)) 6896 inside_init = init = array_to_pointer_conversion (init_loc, init); 6897 if (semantic_type) 6898 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type, 6899 inside_init); 6900 inside_init 6901 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type, 6902 inside_init, origtype, ic_init, 6903 null_pointer_constant, NULL_TREE, NULL_TREE, 6904 0); 6905 6906 /* Check to see if we have already given an error message. */ 6907 if (inside_init == error_mark_node) 6908 ; 6909 else if (require_constant && !TREE_CONSTANT (inside_init)) 6910 { 6911 error_init (init_loc, "initializer element is not constant"); 6912 inside_init = error_mark_node; 6913 } 6914 else if (require_constant 6915 && !initializer_constant_valid_p (inside_init, 6916 TREE_TYPE (inside_init))) 6917 { 6918 error_init (init_loc, "initializer element is not computable at " 6919 "load time"); 6920 inside_init = error_mark_node; 6921 } 6922 else if (require_constant && !maybe_const) 6923 pedwarn_init (init_loc, 0, 6924 "initializer element is not a constant expression"); 6925 6926 return inside_init; 6927 } 6928 6929 /* Come here only for records and arrays. */ 6930 6931 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) 6932 { 6933 error_init (init_loc, "variable-sized object may not be initialized"); 6934 return error_mark_node; 6935 } 6936 6937 error_init (init_loc, "invalid initializer"); 6938 return error_mark_node; 6939} 6940 6941/* Handle initializers that use braces. */ 6942 6943/* Type of object we are accumulating a constructor for. 6944 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ 6945static tree constructor_type; 6946 6947/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields 6948 left to fill. */ 6949static tree constructor_fields; 6950 6951/* For an ARRAY_TYPE, this is the specified index 6952 at which to store the next element we get. */ 6953static tree constructor_index; 6954 6955/* For an ARRAY_TYPE, this is the maximum index. */ 6956static tree constructor_max_index; 6957 6958/* For a RECORD_TYPE, this is the first field not yet written out. */ 6959static tree constructor_unfilled_fields; 6960 6961/* For an ARRAY_TYPE, this is the index of the first element 6962 not yet written out. */ 6963static tree constructor_unfilled_index; 6964 6965/* In a RECORD_TYPE, the byte index of the next consecutive field. 6966 This is so we can generate gaps between fields, when appropriate. */ 6967static tree constructor_bit_index; 6968 6969/* If we are saving up the elements rather than allocating them, 6970 this is the list of elements so far (in reverse order, 6971 most recent first). */ 6972static vec<constructor_elt, va_gc> *constructor_elements; 6973 6974/* 1 if constructor should be incrementally stored into a constructor chain, 6975 0 if all the elements should be kept in AVL tree. */ 6976static int constructor_incremental; 6977 6978/* 1 if so far this constructor's elements are all compile-time constants. */ 6979static int constructor_constant; 6980 6981/* 1 if so far this constructor's elements are all valid address constants. */ 6982static int constructor_simple; 6983 6984/* 1 if this constructor has an element that cannot be part of a 6985 constant expression. */ 6986static int constructor_nonconst; 6987 6988/* 1 if this constructor is erroneous so far. */ 6989static int constructor_erroneous; 6990 6991/* 1 if this constructor is the universal zero initializer { 0 }. */ 6992static int constructor_zeroinit; 6993 6994/* Structure for managing pending initializer elements, organized as an 6995 AVL tree. */ 6996 6997struct init_node 6998{ 6999 struct init_node *left, *right; 7000 struct init_node *parent; 7001 int balance; 7002 tree purpose; 7003 tree value; 7004 tree origtype; 7005}; 7006 7007/* Tree of pending elements at this constructor level. 7008 These are elements encountered out of order 7009 which belong at places we haven't reached yet in actually 7010 writing the output. 7011 Will never hold tree nodes across GC runs. */ 7012static struct init_node *constructor_pending_elts; 7013 7014/* The SPELLING_DEPTH of this constructor. */ 7015static int constructor_depth; 7016 7017/* DECL node for which an initializer is being read. 7018 0 means we are reading a constructor expression 7019 such as (struct foo) {...}. */ 7020static tree constructor_decl; 7021 7022/* Nonzero if this is an initializer for a top-level decl. */ 7023static int constructor_top_level; 7024 7025/* Nonzero if there were any member designators in this initializer. */ 7026static int constructor_designated; 7027 7028/* Nesting depth of designator list. */ 7029static int designator_depth; 7030 7031/* Nonzero if there were diagnosed errors in this designator list. */ 7032static int designator_erroneous; 7033 7034 7035/* This stack has a level for each implicit or explicit level of 7036 structuring in the initializer, including the outermost one. It 7037 saves the values of most of the variables above. */ 7038 7039struct constructor_range_stack; 7040 7041struct constructor_stack 7042{ 7043 struct constructor_stack *next; 7044 tree type; 7045 tree fields; 7046 tree index; 7047 tree max_index; 7048 tree unfilled_index; 7049 tree unfilled_fields; 7050 tree bit_index; 7051 vec<constructor_elt, va_gc> *elements; 7052 struct init_node *pending_elts; 7053 int offset; 7054 int depth; 7055 /* If value nonzero, this value should replace the entire 7056 constructor at this level. */ 7057 struct c_expr replacement_value; 7058 struct constructor_range_stack *range_stack; 7059 char constant; 7060 char simple; 7061 char nonconst; 7062 char implicit; 7063 char erroneous; 7064 char outer; 7065 char incremental; 7066 char designated; 7067 int designator_depth; 7068}; 7069 7070static struct constructor_stack *constructor_stack; 7071 7072/* This stack represents designators from some range designator up to 7073 the last designator in the list. */ 7074 7075struct constructor_range_stack 7076{ 7077 struct constructor_range_stack *next, *prev; 7078 struct constructor_stack *stack; 7079 tree range_start; 7080 tree index; 7081 tree range_end; 7082 tree fields; 7083}; 7084 7085static struct constructor_range_stack *constructor_range_stack; 7086 7087/* This stack records separate initializers that are nested. 7088 Nested initializers can't happen in ANSI C, but GNU C allows them 7089 in cases like { ... (struct foo) { ... } ... }. */ 7090 7091struct initializer_stack 7092{ 7093 struct initializer_stack *next; 7094 tree decl; 7095 struct constructor_stack *constructor_stack; 7096 struct constructor_range_stack *constructor_range_stack; 7097 vec<constructor_elt, va_gc> *elements; 7098 struct spelling *spelling; 7099 struct spelling *spelling_base; 7100 int spelling_size; 7101 char top_level; 7102 char require_constant_value; 7103 char require_constant_elements; 7104}; 7105 7106static struct initializer_stack *initializer_stack; 7107 7108/* Prepare to parse and output the initializer for variable DECL. */ 7109 7110void 7111start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level) 7112{ 7113 const char *locus; 7114 struct initializer_stack *p = XNEW (struct initializer_stack); 7115 7116 p->decl = constructor_decl; 7117 p->require_constant_value = require_constant_value; 7118 p->require_constant_elements = require_constant_elements; 7119 p->constructor_stack = constructor_stack; 7120 p->constructor_range_stack = constructor_range_stack; 7121 p->elements = constructor_elements; 7122 p->spelling = spelling; 7123 p->spelling_base = spelling_base; 7124 p->spelling_size = spelling_size; 7125 p->top_level = constructor_top_level; 7126 p->next = initializer_stack; 7127 initializer_stack = p; 7128 7129 constructor_decl = decl; 7130 constructor_designated = 0; 7131 constructor_top_level = top_level; 7132 7133 if (decl != 0 && decl != error_mark_node) 7134 { 7135 require_constant_value = TREE_STATIC (decl); 7136 require_constant_elements 7137 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99)) 7138 /* For a scalar, you can always use any value to initialize, 7139 even within braces. */ 7140 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE 7141 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE 7142 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE 7143 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE)); 7144 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))); 7145 } 7146 else 7147 { 7148 require_constant_value = 0; 7149 require_constant_elements = 0; 7150 locus = _("(anonymous)"); 7151 } 7152 7153 constructor_stack = 0; 7154 constructor_range_stack = 0; 7155 7156 found_missing_braces = 0; 7157 7158 spelling_base = 0; 7159 spelling_size = 0; 7160 RESTORE_SPELLING_DEPTH (0); 7161 7162 if (locus) 7163 push_string (locus); 7164} 7165 7166void 7167finish_init (void) 7168{ 7169 struct initializer_stack *p = initializer_stack; 7170 7171 /* Free the whole constructor stack of this initializer. */ 7172 while (constructor_stack) 7173 { 7174 struct constructor_stack *q = constructor_stack; 7175 constructor_stack = q->next; 7176 free (q); 7177 } 7178 7179 gcc_assert (!constructor_range_stack); 7180 7181 /* Pop back to the data of the outer initializer (if any). */ 7182 free (spelling_base); 7183 7184 constructor_decl = p->decl; 7185 require_constant_value = p->require_constant_value; 7186 require_constant_elements = p->require_constant_elements; 7187 constructor_stack = p->constructor_stack; 7188 constructor_range_stack = p->constructor_range_stack; 7189 constructor_elements = p->elements; 7190 spelling = p->spelling; 7191 spelling_base = p->spelling_base; 7192 spelling_size = p->spelling_size; 7193 constructor_top_level = p->top_level; 7194 initializer_stack = p->next; 7195 free (p); 7196} 7197 7198/* Call here when we see the initializer is surrounded by braces. 7199 This is instead of a call to push_init_level; 7200 it is matched by a call to pop_init_level. 7201 7202 TYPE is the type to initialize, for a constructor expression. 7203 For an initializer for a decl, TYPE is zero. */ 7204 7205void 7206really_start_incremental_init (tree type) 7207{ 7208 struct constructor_stack *p = XNEW (struct constructor_stack); 7209 7210 if (type == 0) 7211 type = TREE_TYPE (constructor_decl); 7212 7213 if (TREE_CODE (type) == VECTOR_TYPE 7214 && TYPE_VECTOR_OPAQUE (type)) 7215 error ("opaque vector types cannot be initialized"); 7216 7217 p->type = constructor_type; 7218 p->fields = constructor_fields; 7219 p->index = constructor_index; 7220 p->max_index = constructor_max_index; 7221 p->unfilled_index = constructor_unfilled_index; 7222 p->unfilled_fields = constructor_unfilled_fields; 7223 p->bit_index = constructor_bit_index; 7224 p->elements = constructor_elements; 7225 p->constant = constructor_constant; 7226 p->simple = constructor_simple; 7227 p->nonconst = constructor_nonconst; 7228 p->erroneous = constructor_erroneous; 7229 p->pending_elts = constructor_pending_elts; 7230 p->depth = constructor_depth; 7231 p->replacement_value.value = 0; 7232 p->replacement_value.original_code = ERROR_MARK; 7233 p->replacement_value.original_type = NULL; 7234 p->implicit = 0; 7235 p->range_stack = 0; 7236 p->outer = 0; 7237 p->incremental = constructor_incremental; 7238 p->designated = constructor_designated; 7239 p->designator_depth = designator_depth; 7240 p->next = 0; 7241 constructor_stack = p; 7242 7243 constructor_constant = 1; 7244 constructor_simple = 1; 7245 constructor_nonconst = 0; 7246 constructor_depth = SPELLING_DEPTH (); 7247 constructor_elements = NULL; 7248 constructor_pending_elts = 0; 7249 constructor_type = type; 7250 constructor_incremental = 1; 7251 constructor_designated = 0; 7252 constructor_zeroinit = 1; 7253 designator_depth = 0; 7254 designator_erroneous = 0; 7255 7256 if (TREE_CODE (constructor_type) == RECORD_TYPE 7257 || TREE_CODE (constructor_type) == UNION_TYPE) 7258 { 7259 constructor_fields = TYPE_FIELDS (constructor_type); 7260 /* Skip any nameless bit fields at the beginning. */ 7261 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) 7262 && DECL_NAME (constructor_fields) == 0) 7263 constructor_fields = DECL_CHAIN (constructor_fields); 7264 7265 constructor_unfilled_fields = constructor_fields; 7266 constructor_bit_index = bitsize_zero_node; 7267 } 7268 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 7269 { 7270 if (TYPE_DOMAIN (constructor_type)) 7271 { 7272 constructor_max_index 7273 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); 7274 7275 /* Detect non-empty initializations of zero-length arrays. */ 7276 if (constructor_max_index == NULL_TREE 7277 && TYPE_SIZE (constructor_type)) 7278 constructor_max_index = integer_minus_one_node; 7279 7280 /* constructor_max_index needs to be an INTEGER_CST. Attempts 7281 to initialize VLAs will cause a proper error; avoid tree 7282 checking errors as well by setting a safe value. */ 7283 if (constructor_max_index 7284 && TREE_CODE (constructor_max_index) != INTEGER_CST) 7285 constructor_max_index = integer_minus_one_node; 7286 7287 constructor_index 7288 = convert (bitsizetype, 7289 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); 7290 } 7291 else 7292 { 7293 constructor_index = bitsize_zero_node; 7294 constructor_max_index = NULL_TREE; 7295 } 7296 7297 constructor_unfilled_index = constructor_index; 7298 } 7299 else if (TREE_CODE (constructor_type) == VECTOR_TYPE) 7300 { 7301 /* Vectors are like simple fixed-size arrays. */ 7302 constructor_max_index = 7303 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); 7304 constructor_index = bitsize_zero_node; 7305 constructor_unfilled_index = constructor_index; 7306 } 7307 else 7308 { 7309 /* Handle the case of int x = {5}; */ 7310 constructor_fields = constructor_type; 7311 constructor_unfilled_fields = constructor_type; 7312 } 7313} 7314 7315/* Called when we see an open brace for a nested initializer. Finish 7316 off any pending levels with implicit braces. */ 7317void 7318finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack) 7319{ 7320 while (constructor_stack->implicit) 7321 { 7322 if ((TREE_CODE (constructor_type) == RECORD_TYPE 7323 || TREE_CODE (constructor_type) == UNION_TYPE) 7324 && constructor_fields == 0) 7325 process_init_element (input_location, 7326 pop_init_level (loc, 1, braced_init_obstack), 7327 true, braced_init_obstack); 7328 else if (TREE_CODE (constructor_type) == ARRAY_TYPE 7329 && constructor_max_index 7330 && tree_int_cst_lt (constructor_max_index, 7331 constructor_index)) 7332 process_init_element (input_location, 7333 pop_init_level (loc, 1, braced_init_obstack), 7334 true, braced_init_obstack); 7335 else 7336 break; 7337 } 7338} 7339 7340/* Push down into a subobject, for initialization. 7341 If this is for an explicit set of braces, IMPLICIT is 0. 7342 If it is because the next element belongs at a lower level, 7343 IMPLICIT is 1 (or 2 if the push is because of designator list). */ 7344 7345void 7346push_init_level (location_t loc, int implicit, 7347 struct obstack *braced_init_obstack) 7348{ 7349 struct constructor_stack *p; 7350 tree value = NULL_TREE; 7351 7352 /* Unless this is an explicit brace, we need to preserve previous 7353 content if any. */ 7354 if (implicit) 7355 { 7356 if ((TREE_CODE (constructor_type) == RECORD_TYPE 7357 || TREE_CODE (constructor_type) == UNION_TYPE) 7358 && constructor_fields) 7359 value = find_init_member (constructor_fields, braced_init_obstack); 7360 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 7361 value = find_init_member (constructor_index, braced_init_obstack); 7362 } 7363 7364 p = XNEW (struct constructor_stack); 7365 p->type = constructor_type; 7366 p->fields = constructor_fields; 7367 p->index = constructor_index; 7368 p->max_index = constructor_max_index; 7369 p->unfilled_index = constructor_unfilled_index; 7370 p->unfilled_fields = constructor_unfilled_fields; 7371 p->bit_index = constructor_bit_index; 7372 p->elements = constructor_elements; 7373 p->constant = constructor_constant; 7374 p->simple = constructor_simple; 7375 p->nonconst = constructor_nonconst; 7376 p->erroneous = constructor_erroneous; 7377 p->pending_elts = constructor_pending_elts; 7378 p->depth = constructor_depth; 7379 p->replacement_value.value = 0; 7380 p->replacement_value.original_code = ERROR_MARK; 7381 p->replacement_value.original_type = NULL; 7382 p->implicit = implicit; 7383 p->outer = 0; 7384 p->incremental = constructor_incremental; 7385 p->designated = constructor_designated; 7386 p->designator_depth = designator_depth; 7387 p->next = constructor_stack; 7388 p->range_stack = 0; 7389 constructor_stack = p; 7390 7391 constructor_constant = 1; 7392 constructor_simple = 1; 7393 constructor_nonconst = 0; 7394 constructor_depth = SPELLING_DEPTH (); 7395 constructor_elements = NULL; 7396 constructor_incremental = 1; 7397 constructor_designated = 0; 7398 constructor_pending_elts = 0; 7399 if (!implicit) 7400 { 7401 p->range_stack = constructor_range_stack; 7402 constructor_range_stack = 0; 7403 designator_depth = 0; 7404 designator_erroneous = 0; 7405 } 7406 7407 /* Don't die if an entire brace-pair level is superfluous 7408 in the containing level. */ 7409 if (constructor_type == 0) 7410 ; 7411 else if (TREE_CODE (constructor_type) == RECORD_TYPE 7412 || TREE_CODE (constructor_type) == UNION_TYPE) 7413 { 7414 /* Don't die if there are extra init elts at the end. */ 7415 if (constructor_fields == 0) 7416 constructor_type = 0; 7417 else 7418 { 7419 constructor_type = TREE_TYPE (constructor_fields); 7420 push_member_name (constructor_fields); 7421 constructor_depth++; 7422 } 7423 /* If upper initializer is designated, then mark this as 7424 designated too to prevent bogus warnings. */ 7425 constructor_designated = p->designated; 7426 } 7427 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 7428 { 7429 constructor_type = TREE_TYPE (constructor_type); 7430 push_array_bounds (tree_to_uhwi (constructor_index)); 7431 constructor_depth++; 7432 } 7433 7434 if (constructor_type == 0) 7435 { 7436 error_init (loc, "extra brace group at end of initializer"); 7437 constructor_fields = 0; 7438 constructor_unfilled_fields = 0; 7439 return; 7440 } 7441 7442 if (value && TREE_CODE (value) == CONSTRUCTOR) 7443 { 7444 constructor_constant = TREE_CONSTANT (value); 7445 constructor_simple = TREE_STATIC (value); 7446 constructor_nonconst = CONSTRUCTOR_NON_CONST (value); 7447 constructor_elements = CONSTRUCTOR_ELTS (value); 7448 if (!vec_safe_is_empty (constructor_elements) 7449 && (TREE_CODE (constructor_type) == RECORD_TYPE 7450 || TREE_CODE (constructor_type) == ARRAY_TYPE)) 7451 set_nonincremental_init (braced_init_obstack); 7452 } 7453 7454 if (implicit == 1) 7455 found_missing_braces = 1; 7456 7457 if (TREE_CODE (constructor_type) == RECORD_TYPE 7458 || TREE_CODE (constructor_type) == UNION_TYPE) 7459 { 7460 constructor_fields = TYPE_FIELDS (constructor_type); 7461 /* Skip any nameless bit fields at the beginning. */ 7462 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) 7463 && DECL_NAME (constructor_fields) == 0) 7464 constructor_fields = DECL_CHAIN (constructor_fields); 7465 7466 constructor_unfilled_fields = constructor_fields; 7467 constructor_bit_index = bitsize_zero_node; 7468 } 7469 else if (TREE_CODE (constructor_type) == VECTOR_TYPE) 7470 { 7471 /* Vectors are like simple fixed-size arrays. */ 7472 constructor_max_index = 7473 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); 7474 constructor_index = bitsize_int (0); 7475 constructor_unfilled_index = constructor_index; 7476 } 7477 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 7478 { 7479 if (TYPE_DOMAIN (constructor_type)) 7480 { 7481 constructor_max_index 7482 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); 7483 7484 /* Detect non-empty initializations of zero-length arrays. */ 7485 if (constructor_max_index == NULL_TREE 7486 && TYPE_SIZE (constructor_type)) 7487 constructor_max_index = integer_minus_one_node; 7488 7489 /* constructor_max_index needs to be an INTEGER_CST. Attempts 7490 to initialize VLAs will cause a proper error; avoid tree 7491 checking errors as well by setting a safe value. */ 7492 if (constructor_max_index 7493 && TREE_CODE (constructor_max_index) != INTEGER_CST) 7494 constructor_max_index = integer_minus_one_node; 7495 7496 constructor_index 7497 = convert (bitsizetype, 7498 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); 7499 } 7500 else 7501 constructor_index = bitsize_zero_node; 7502 7503 constructor_unfilled_index = constructor_index; 7504 if (value && TREE_CODE (value) == STRING_CST) 7505 { 7506 /* We need to split the char/wchar array into individual 7507 characters, so that we don't have to special case it 7508 everywhere. */ 7509 set_nonincremental_init_from_string (value, braced_init_obstack); 7510 } 7511 } 7512 else 7513 { 7514 if (constructor_type != error_mark_node) 7515 warning_init (input_location, 0, "braces around scalar initializer"); 7516 constructor_fields = constructor_type; 7517 constructor_unfilled_fields = constructor_type; 7518 } 7519} 7520 7521/* At the end of an implicit or explicit brace level, 7522 finish up that level of constructor. If a single expression 7523 with redundant braces initialized that level, return the 7524 c_expr structure for that expression. Otherwise, the original_code 7525 element is set to ERROR_MARK. 7526 If we were outputting the elements as they are read, return 0 as the value 7527 from inner levels (process_init_element ignores that), 7528 but return error_mark_node as the value from the outermost level 7529 (that's what we want to put in DECL_INITIAL). 7530 Otherwise, return a CONSTRUCTOR expression as the value. */ 7531 7532struct c_expr 7533pop_init_level (location_t loc, int implicit, 7534 struct obstack *braced_init_obstack) 7535{ 7536 struct constructor_stack *p; 7537 struct c_expr ret; 7538 ret.value = 0; 7539 ret.original_code = ERROR_MARK; 7540 ret.original_type = NULL; 7541 7542 if (implicit == 0) 7543 { 7544 /* When we come to an explicit close brace, 7545 pop any inner levels that didn't have explicit braces. */ 7546 while (constructor_stack->implicit) 7547 process_init_element (input_location, 7548 pop_init_level (loc, 1, braced_init_obstack), 7549 true, braced_init_obstack); 7550 gcc_assert (!constructor_range_stack); 7551 } 7552 7553 /* Now output all pending elements. */ 7554 constructor_incremental = 1; 7555 output_pending_init_elements (1, braced_init_obstack); 7556 7557 p = constructor_stack; 7558 7559 /* Error for initializing a flexible array member, or a zero-length 7560 array member in an inappropriate context. */ 7561 if (constructor_type && constructor_fields 7562 && TREE_CODE (constructor_type) == ARRAY_TYPE 7563 && TYPE_DOMAIN (constructor_type) 7564 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) 7565 { 7566 /* Silently discard empty initializations. The parser will 7567 already have pedwarned for empty brackets. */ 7568 if (integer_zerop (constructor_unfilled_index)) 7569 constructor_type = NULL_TREE; 7570 else 7571 { 7572 gcc_assert (!TYPE_SIZE (constructor_type)); 7573 7574 if (constructor_depth > 2) 7575 error_init (loc, "initialization of flexible array member in a nested context"); 7576 else 7577 pedwarn_init (loc, OPT_Wpedantic, 7578 "initialization of a flexible array member"); 7579 7580 /* We have already issued an error message for the existence 7581 of a flexible array member not at the end of the structure. 7582 Discard the initializer so that we do not die later. */ 7583 if (DECL_CHAIN (constructor_fields) != NULL_TREE) 7584 constructor_type = NULL_TREE; 7585 } 7586 } 7587 7588 switch (vec_safe_length (constructor_elements)) 7589 { 7590 case 0: 7591 /* Initialization with { } counts as zeroinit. */ 7592 constructor_zeroinit = 1; 7593 break; 7594 case 1: 7595 /* This might be zeroinit as well. */ 7596 if (integer_zerop ((*constructor_elements)[0].value)) 7597 constructor_zeroinit = 1; 7598 break; 7599 default: 7600 /* If the constructor has more than one element, it can't be { 0 }. */ 7601 constructor_zeroinit = 0; 7602 break; 7603 } 7604 7605 /* Warn when some structs are initialized with direct aggregation. */ 7606 if (!implicit && found_missing_braces && warn_missing_braces 7607 && !constructor_zeroinit) 7608 warning_init (loc, OPT_Wmissing_braces, 7609 "missing braces around initializer"); 7610 7611 /* Warn when some struct elements are implicitly initialized to zero. */ 7612 if (warn_missing_field_initializers 7613 && constructor_type 7614 && TREE_CODE (constructor_type) == RECORD_TYPE 7615 && constructor_unfilled_fields) 7616 { 7617 /* Do not warn for flexible array members or zero-length arrays. */ 7618 while (constructor_unfilled_fields 7619 && (!DECL_SIZE (constructor_unfilled_fields) 7620 || integer_zerop (DECL_SIZE (constructor_unfilled_fields)))) 7621 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields); 7622 7623 if (constructor_unfilled_fields 7624 /* Do not warn if this level of the initializer uses member 7625 designators; it is likely to be deliberate. */ 7626 && !constructor_designated 7627 /* Do not warn about initializing with { 0 } or with { }. */ 7628 && !constructor_zeroinit) 7629 { 7630 if (warning_at (input_location, OPT_Wmissing_field_initializers, 7631 "missing initializer for field %qD of %qT", 7632 constructor_unfilled_fields, 7633 constructor_type)) 7634 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields), 7635 "%qD declared here", constructor_unfilled_fields); 7636 } 7637 } 7638 7639 /* Pad out the end of the structure. */ 7640 if (p->replacement_value.value) 7641 /* If this closes a superfluous brace pair, 7642 just pass out the element between them. */ 7643 ret = p->replacement_value; 7644 else if (constructor_type == 0) 7645 ; 7646 else if (TREE_CODE (constructor_type) != RECORD_TYPE 7647 && TREE_CODE (constructor_type) != UNION_TYPE 7648 && TREE_CODE (constructor_type) != ARRAY_TYPE 7649 && TREE_CODE (constructor_type) != VECTOR_TYPE) 7650 { 7651 /* A nonincremental scalar initializer--just return 7652 the element, after verifying there is just one. */ 7653 if (vec_safe_is_empty (constructor_elements)) 7654 { 7655 if (!constructor_erroneous) 7656 error_init (loc, "empty scalar initializer"); 7657 ret.value = error_mark_node; 7658 } 7659 else if (vec_safe_length (constructor_elements) != 1) 7660 { 7661 error_init (loc, "extra elements in scalar initializer"); 7662 ret.value = (*constructor_elements)[0].value; 7663 } 7664 else 7665 ret.value = (*constructor_elements)[0].value; 7666 } 7667 else 7668 { 7669 if (constructor_erroneous) 7670 ret.value = error_mark_node; 7671 else 7672 { 7673 ret.value = build_constructor (constructor_type, 7674 constructor_elements); 7675 if (constructor_constant) 7676 TREE_CONSTANT (ret.value) = 1; 7677 if (constructor_constant && constructor_simple) 7678 TREE_STATIC (ret.value) = 1; 7679 if (constructor_nonconst) 7680 CONSTRUCTOR_NON_CONST (ret.value) = 1; 7681 } 7682 } 7683 7684 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR) 7685 { 7686 if (constructor_nonconst) 7687 ret.original_code = C_MAYBE_CONST_EXPR; 7688 else if (ret.original_code == C_MAYBE_CONST_EXPR) 7689 ret.original_code = ERROR_MARK; 7690 } 7691 7692 constructor_type = p->type; 7693 constructor_fields = p->fields; 7694 constructor_index = p->index; 7695 constructor_max_index = p->max_index; 7696 constructor_unfilled_index = p->unfilled_index; 7697 constructor_unfilled_fields = p->unfilled_fields; 7698 constructor_bit_index = p->bit_index; 7699 constructor_elements = p->elements; 7700 constructor_constant = p->constant; 7701 constructor_simple = p->simple; 7702 constructor_nonconst = p->nonconst; 7703 constructor_erroneous = p->erroneous; 7704 constructor_incremental = p->incremental; 7705 constructor_designated = p->designated; 7706 designator_depth = p->designator_depth; 7707 constructor_pending_elts = p->pending_elts; 7708 constructor_depth = p->depth; 7709 if (!p->implicit) 7710 constructor_range_stack = p->range_stack; 7711 RESTORE_SPELLING_DEPTH (constructor_depth); 7712 7713 constructor_stack = p->next; 7714 free (p); 7715 7716 if (ret.value == 0 && constructor_stack == 0) 7717 ret.value = error_mark_node; 7718 return ret; 7719} 7720 7721/* Common handling for both array range and field name designators. 7722 ARRAY argument is nonzero for array ranges. Returns zero for success. */ 7723 7724static int 7725set_designator (location_t loc, int array, 7726 struct obstack *braced_init_obstack) 7727{ 7728 tree subtype; 7729 enum tree_code subcode; 7730 7731 /* Don't die if an entire brace-pair level is superfluous 7732 in the containing level. */ 7733 if (constructor_type == 0) 7734 return 1; 7735 7736 /* If there were errors in this designator list already, bail out 7737 silently. */ 7738 if (designator_erroneous) 7739 return 1; 7740 7741 if (!designator_depth) 7742 { 7743 gcc_assert (!constructor_range_stack); 7744 7745 /* Designator list starts at the level of closest explicit 7746 braces. */ 7747 while (constructor_stack->implicit) 7748 process_init_element (input_location, 7749 pop_init_level (loc, 1, braced_init_obstack), 7750 true, braced_init_obstack); 7751 constructor_designated = 1; 7752 return 0; 7753 } 7754 7755 switch (TREE_CODE (constructor_type)) 7756 { 7757 case RECORD_TYPE: 7758 case UNION_TYPE: 7759 subtype = TREE_TYPE (constructor_fields); 7760 if (subtype != error_mark_node) 7761 subtype = TYPE_MAIN_VARIANT (subtype); 7762 break; 7763 case ARRAY_TYPE: 7764 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); 7765 break; 7766 default: 7767 gcc_unreachable (); 7768 } 7769 7770 subcode = TREE_CODE (subtype); 7771 if (array && subcode != ARRAY_TYPE) 7772 { 7773 error_init (loc, "array index in non-array initializer"); 7774 return 1; 7775 } 7776 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE) 7777 { 7778 error_init (loc, "field name not in record or union initializer"); 7779 return 1; 7780 } 7781 7782 constructor_designated = 1; 7783 finish_implicit_inits (loc, braced_init_obstack); 7784 push_init_level (loc, 2, braced_init_obstack); 7785 return 0; 7786} 7787 7788/* If there are range designators in designator list, push a new designator 7789 to constructor_range_stack. RANGE_END is end of such stack range or 7790 NULL_TREE if there is no range designator at this level. */ 7791 7792static void 7793push_range_stack (tree range_end, struct obstack * braced_init_obstack) 7794{ 7795 struct constructor_range_stack *p; 7796 7797 p = (struct constructor_range_stack *) 7798 obstack_alloc (braced_init_obstack, 7799 sizeof (struct constructor_range_stack)); 7800 p->prev = constructor_range_stack; 7801 p->next = 0; 7802 p->fields = constructor_fields; 7803 p->range_start = constructor_index; 7804 p->index = constructor_index; 7805 p->stack = constructor_stack; 7806 p->range_end = range_end; 7807 if (constructor_range_stack) 7808 constructor_range_stack->next = p; 7809 constructor_range_stack = p; 7810} 7811 7812/* Within an array initializer, specify the next index to be initialized. 7813 FIRST is that index. If LAST is nonzero, then initialize a range 7814 of indices, running from FIRST through LAST. */ 7815 7816void 7817set_init_index (location_t loc, tree first, tree last, 7818 struct obstack *braced_init_obstack) 7819{ 7820 if (set_designator (loc, 1, braced_init_obstack)) 7821 return; 7822 7823 designator_erroneous = 1; 7824 7825 if (!INTEGRAL_TYPE_P (TREE_TYPE (first)) 7826 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last)))) 7827 { 7828 error_init (loc, "array index in initializer not of integer type"); 7829 return; 7830 } 7831 7832 if (TREE_CODE (first) != INTEGER_CST) 7833 { 7834 first = c_fully_fold (first, false, NULL); 7835 if (TREE_CODE (first) == INTEGER_CST) 7836 pedwarn_init (loc, OPT_Wpedantic, 7837 "array index in initializer is not " 7838 "an integer constant expression"); 7839 } 7840 7841 if (last && TREE_CODE (last) != INTEGER_CST) 7842 { 7843 last = c_fully_fold (last, false, NULL); 7844 if (TREE_CODE (last) == INTEGER_CST) 7845 pedwarn_init (loc, OPT_Wpedantic, 7846 "array index in initializer is not " 7847 "an integer constant expression"); 7848 } 7849 7850 if (TREE_CODE (first) != INTEGER_CST) 7851 error_init (loc, "nonconstant array index in initializer"); 7852 else if (last != 0 && TREE_CODE (last) != INTEGER_CST) 7853 error_init (loc, "nonconstant array index in initializer"); 7854 else if (TREE_CODE (constructor_type) != ARRAY_TYPE) 7855 error_init (loc, "array index in non-array initializer"); 7856 else if (tree_int_cst_sgn (first) == -1) 7857 error_init (loc, "array index in initializer exceeds array bounds"); 7858 else if (constructor_max_index 7859 && tree_int_cst_lt (constructor_max_index, first)) 7860 error_init (loc, "array index in initializer exceeds array bounds"); 7861 else 7862 { 7863 constant_expression_warning (first); 7864 if (last) 7865 constant_expression_warning (last); 7866 constructor_index = convert (bitsizetype, first); 7867 if (tree_int_cst_lt (constructor_index, first)) 7868 { 7869 constructor_index = copy_node (constructor_index); 7870 TREE_OVERFLOW (constructor_index) = 1; 7871 } 7872 7873 if (last) 7874 { 7875 if (tree_int_cst_equal (first, last)) 7876 last = 0; 7877 else if (tree_int_cst_lt (last, first)) 7878 { 7879 error_init (loc, "empty index range in initializer"); 7880 last = 0; 7881 } 7882 else 7883 { 7884 last = convert (bitsizetype, last); 7885 if (constructor_max_index != 0 7886 && tree_int_cst_lt (constructor_max_index, last)) 7887 { 7888 error_init (loc, "array index range in initializer exceeds " 7889 "array bounds"); 7890 last = 0; 7891 } 7892 } 7893 } 7894 7895 designator_depth++; 7896 designator_erroneous = 0; 7897 if (constructor_range_stack || last) 7898 push_range_stack (last, braced_init_obstack); 7899 } 7900} 7901 7902/* Within a struct initializer, specify the next field to be initialized. */ 7903 7904void 7905set_init_label (location_t loc, tree fieldname, 7906 struct obstack *braced_init_obstack) 7907{ 7908 tree field; 7909 7910 if (set_designator (loc, 0, braced_init_obstack)) 7911 return; 7912 7913 designator_erroneous = 1; 7914 7915 if (TREE_CODE (constructor_type) != RECORD_TYPE 7916 && TREE_CODE (constructor_type) != UNION_TYPE) 7917 { 7918 error_init (loc, "field name not in record or union initializer"); 7919 return; 7920 } 7921 7922 field = lookup_field (constructor_type, fieldname); 7923 7924 if (field == 0) 7925 error ("unknown field %qE specified in initializer", fieldname); 7926 else 7927 do 7928 { 7929 constructor_fields = TREE_VALUE (field); 7930 designator_depth++; 7931 designator_erroneous = 0; 7932 if (constructor_range_stack) 7933 push_range_stack (NULL_TREE, braced_init_obstack); 7934 field = TREE_CHAIN (field); 7935 if (field) 7936 { 7937 if (set_designator (loc, 0, braced_init_obstack)) 7938 return; 7939 } 7940 } 7941 while (field != NULL_TREE); 7942} 7943 7944/* Add a new initializer to the tree of pending initializers. PURPOSE 7945 identifies the initializer, either array index or field in a structure. 7946 VALUE is the value of that index or field. If ORIGTYPE is not 7947 NULL_TREE, it is the original type of VALUE. 7948 7949 IMPLICIT is true if value comes from pop_init_level (1), 7950 the new initializer has been merged with the existing one 7951 and thus no warnings should be emitted about overriding an 7952 existing initializer. */ 7953 7954static void 7955add_pending_init (location_t loc, tree purpose, tree value, tree origtype, 7956 bool implicit, struct obstack *braced_init_obstack) 7957{ 7958 struct init_node *p, **q, *r; 7959 7960 q = &constructor_pending_elts; 7961 p = 0; 7962 7963 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 7964 { 7965 while (*q != 0) 7966 { 7967 p = *q; 7968 if (tree_int_cst_lt (purpose, p->purpose)) 7969 q = &p->left; 7970 else if (tree_int_cst_lt (p->purpose, purpose)) 7971 q = &p->right; 7972 else 7973 { 7974 if (!implicit) 7975 { 7976 if (TREE_SIDE_EFFECTS (p->value)) 7977 warning_init (loc, 0, 7978 "initialized field with side-effects " 7979 "overwritten"); 7980 else if (warn_override_init) 7981 warning_init (loc, OPT_Woverride_init, 7982 "initialized field overwritten"); 7983 } 7984 p->value = value; 7985 p->origtype = origtype; 7986 return; 7987 } 7988 } 7989 } 7990 else 7991 { 7992 tree bitpos; 7993 7994 bitpos = bit_position (purpose); 7995 while (*q != NULL) 7996 { 7997 p = *q; 7998 if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) 7999 q = &p->left; 8000 else if (p->purpose != purpose) 8001 q = &p->right; 8002 else 8003 { 8004 if (!implicit) 8005 { 8006 if (TREE_SIDE_EFFECTS (p->value)) 8007 warning_init (loc, 0, 8008 "initialized field with side-effects " 8009 "overwritten"); 8010 else if (warn_override_init) 8011 warning_init (loc, OPT_Woverride_init, 8012 "initialized field overwritten"); 8013 } 8014 p->value = value; 8015 p->origtype = origtype; 8016 return; 8017 } 8018 } 8019 } 8020 8021 r = (struct init_node *) obstack_alloc (braced_init_obstack, 8022 sizeof (struct init_node)); 8023 r->purpose = purpose; 8024 r->value = value; 8025 r->origtype = origtype; 8026 8027 *q = r; 8028 r->parent = p; 8029 r->left = 0; 8030 r->right = 0; 8031 r->balance = 0; 8032 8033 while (p) 8034 { 8035 struct init_node *s; 8036 8037 if (r == p->left) 8038 { 8039 if (p->balance == 0) 8040 p->balance = -1; 8041 else if (p->balance < 0) 8042 { 8043 if (r->balance < 0) 8044 { 8045 /* L rotation. */ 8046 p->left = r->right; 8047 if (p->left) 8048 p->left->parent = p; 8049 r->right = p; 8050 8051 p->balance = 0; 8052 r->balance = 0; 8053 8054 s = p->parent; 8055 p->parent = r; 8056 r->parent = s; 8057 if (s) 8058 { 8059 if (s->left == p) 8060 s->left = r; 8061 else 8062 s->right = r; 8063 } 8064 else 8065 constructor_pending_elts = r; 8066 } 8067 else 8068 { 8069 /* LR rotation. */ 8070 struct init_node *t = r->right; 8071 8072 r->right = t->left; 8073 if (r->right) 8074 r->right->parent = r; 8075 t->left = r; 8076 8077 p->left = t->right; 8078 if (p->left) 8079 p->left->parent = p; 8080 t->right = p; 8081 8082 p->balance = t->balance < 0; 8083 r->balance = -(t->balance > 0); 8084 t->balance = 0; 8085 8086 s = p->parent; 8087 p->parent = t; 8088 r->parent = t; 8089 t->parent = s; 8090 if (s) 8091 { 8092 if (s->left == p) 8093 s->left = t; 8094 else 8095 s->right = t; 8096 } 8097 else 8098 constructor_pending_elts = t; 8099 } 8100 break; 8101 } 8102 else 8103 { 8104 /* p->balance == +1; growth of left side balances the node. */ 8105 p->balance = 0; 8106 break; 8107 } 8108 } 8109 else /* r == p->right */ 8110 { 8111 if (p->balance == 0) 8112 /* Growth propagation from right side. */ 8113 p->balance++; 8114 else if (p->balance > 0) 8115 { 8116 if (r->balance > 0) 8117 { 8118 /* R rotation. */ 8119 p->right = r->left; 8120 if (p->right) 8121 p->right->parent = p; 8122 r->left = p; 8123 8124 p->balance = 0; 8125 r->balance = 0; 8126 8127 s = p->parent; 8128 p->parent = r; 8129 r->parent = s; 8130 if (s) 8131 { 8132 if (s->left == p) 8133 s->left = r; 8134 else 8135 s->right = r; 8136 } 8137 else 8138 constructor_pending_elts = r; 8139 } 8140 else /* r->balance == -1 */ 8141 { 8142 /* RL rotation */ 8143 struct init_node *t = r->left; 8144 8145 r->left = t->right; 8146 if (r->left) 8147 r->left->parent = r; 8148 t->right = r; 8149 8150 p->right = t->left; 8151 if (p->right) 8152 p->right->parent = p; 8153 t->left = p; 8154 8155 r->balance = (t->balance < 0); 8156 p->balance = -(t->balance > 0); 8157 t->balance = 0; 8158 8159 s = p->parent; 8160 p->parent = t; 8161 r->parent = t; 8162 t->parent = s; 8163 if (s) 8164 { 8165 if (s->left == p) 8166 s->left = t; 8167 else 8168 s->right = t; 8169 } 8170 else 8171 constructor_pending_elts = t; 8172 } 8173 break; 8174 } 8175 else 8176 { 8177 /* p->balance == -1; growth of right side balances the node. */ 8178 p->balance = 0; 8179 break; 8180 } 8181 } 8182 8183 r = p; 8184 p = p->parent; 8185 } 8186} 8187 8188/* Build AVL tree from a sorted chain. */ 8189 8190static void 8191set_nonincremental_init (struct obstack * braced_init_obstack) 8192{ 8193 unsigned HOST_WIDE_INT ix; 8194 tree index, value; 8195 8196 if (TREE_CODE (constructor_type) != RECORD_TYPE 8197 && TREE_CODE (constructor_type) != ARRAY_TYPE) 8198 return; 8199 8200 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value) 8201 add_pending_init (input_location, index, value, NULL_TREE, true, 8202 braced_init_obstack); 8203 constructor_elements = NULL; 8204 if (TREE_CODE (constructor_type) == RECORD_TYPE) 8205 { 8206 constructor_unfilled_fields = TYPE_FIELDS (constructor_type); 8207 /* Skip any nameless bit fields at the beginning. */ 8208 while (constructor_unfilled_fields != 0 8209 && DECL_C_BIT_FIELD (constructor_unfilled_fields) 8210 && DECL_NAME (constructor_unfilled_fields) == 0) 8211 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); 8212 8213 } 8214 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 8215 { 8216 if (TYPE_DOMAIN (constructor_type)) 8217 constructor_unfilled_index 8218 = convert (bitsizetype, 8219 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); 8220 else 8221 constructor_unfilled_index = bitsize_zero_node; 8222 } 8223 constructor_incremental = 0; 8224} 8225 8226/* Build AVL tree from a string constant. */ 8227 8228static void 8229set_nonincremental_init_from_string (tree str, 8230 struct obstack * braced_init_obstack) 8231{ 8232 tree value, purpose, type; 8233 HOST_WIDE_INT val[2]; 8234 const char *p, *end; 8235 int byte, wchar_bytes, charwidth, bitpos; 8236 8237 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE); 8238 8239 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT; 8240 charwidth = TYPE_PRECISION (char_type_node); 8241 type = TREE_TYPE (constructor_type); 8242 p = TREE_STRING_POINTER (str); 8243 end = p + TREE_STRING_LENGTH (str); 8244 8245 for (purpose = bitsize_zero_node; 8246 p < end 8247 && !(constructor_max_index 8248 && tree_int_cst_lt (constructor_max_index, purpose)); 8249 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node)) 8250 { 8251 if (wchar_bytes == 1) 8252 { 8253 val[0] = (unsigned char) *p++; 8254 val[1] = 0; 8255 } 8256 else 8257 { 8258 val[1] = 0; 8259 val[0] = 0; 8260 for (byte = 0; byte < wchar_bytes; byte++) 8261 { 8262 if (BYTES_BIG_ENDIAN) 8263 bitpos = (wchar_bytes - byte - 1) * charwidth; 8264 else 8265 bitpos = byte * charwidth; 8266 val[bitpos % HOST_BITS_PER_WIDE_INT] 8267 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++)) 8268 << (bitpos % HOST_BITS_PER_WIDE_INT); 8269 } 8270 } 8271 8272 if (!TYPE_UNSIGNED (type)) 8273 { 8274 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR; 8275 if (bitpos < HOST_BITS_PER_WIDE_INT) 8276 { 8277 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1))) 8278 { 8279 val[0] |= ((HOST_WIDE_INT) -1) << bitpos; 8280 val[1] = -1; 8281 } 8282 } 8283 else if (bitpos == HOST_BITS_PER_WIDE_INT) 8284 { 8285 if (val[0] < 0) 8286 val[1] = -1; 8287 } 8288 else if (val[1] & (((HOST_WIDE_INT) 1) 8289 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT))) 8290 val[1] |= ((HOST_WIDE_INT) -1) 8291 << (bitpos - HOST_BITS_PER_WIDE_INT); 8292 } 8293 8294 value = wide_int_to_tree (type, 8295 wide_int::from_array (val, 2, 8296 HOST_BITS_PER_WIDE_INT * 2)); 8297 add_pending_init (input_location, purpose, value, NULL_TREE, true, 8298 braced_init_obstack); 8299 } 8300 8301 constructor_incremental = 0; 8302} 8303 8304/* Return value of FIELD in pending initializer or zero if the field was 8305 not initialized yet. */ 8306 8307static tree 8308find_init_member (tree field, struct obstack * braced_init_obstack) 8309{ 8310 struct init_node *p; 8311 8312 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 8313 { 8314 if (constructor_incremental 8315 && tree_int_cst_lt (field, constructor_unfilled_index)) 8316 set_nonincremental_init (braced_init_obstack); 8317 8318 p = constructor_pending_elts; 8319 while (p) 8320 { 8321 if (tree_int_cst_lt (field, p->purpose)) 8322 p = p->left; 8323 else if (tree_int_cst_lt (p->purpose, field)) 8324 p = p->right; 8325 else 8326 return p->value; 8327 } 8328 } 8329 else if (TREE_CODE (constructor_type) == RECORD_TYPE) 8330 { 8331 tree bitpos = bit_position (field); 8332 8333 if (constructor_incremental 8334 && (!constructor_unfilled_fields 8335 || tree_int_cst_lt (bitpos, 8336 bit_position (constructor_unfilled_fields)))) 8337 set_nonincremental_init (braced_init_obstack); 8338 8339 p = constructor_pending_elts; 8340 while (p) 8341 { 8342 if (field == p->purpose) 8343 return p->value; 8344 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) 8345 p = p->left; 8346 else 8347 p = p->right; 8348 } 8349 } 8350 else if (TREE_CODE (constructor_type) == UNION_TYPE) 8351 { 8352 if (!vec_safe_is_empty (constructor_elements) 8353 && (constructor_elements->last ().index == field)) 8354 return constructor_elements->last ().value; 8355 } 8356 return 0; 8357} 8358 8359/* "Output" the next constructor element. 8360 At top level, really output it to assembler code now. 8361 Otherwise, collect it in a list from which we will make a CONSTRUCTOR. 8362 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE. 8363 TYPE is the data type that the containing data type wants here. 8364 FIELD is the field (a FIELD_DECL) or the index that this element fills. 8365 If VALUE is a string constant, STRICT_STRING is true if it is 8366 unparenthesized or we should not warn here for it being parenthesized. 8367 For other types of VALUE, STRICT_STRING is not used. 8368 8369 PENDING if non-nil means output pending elements that belong 8370 right after this element. (PENDING is normally 1; 8371 it is 0 while outputting pending elements, to avoid recursion.) 8372 8373 IMPLICIT is true if value comes from pop_init_level (1), 8374 the new initializer has been merged with the existing one 8375 and thus no warnings should be emitted about overriding an 8376 existing initializer. */ 8377 8378static void 8379output_init_element (location_t loc, tree value, tree origtype, 8380 bool strict_string, tree type, tree field, int pending, 8381 bool implicit, struct obstack * braced_init_obstack) 8382{ 8383 tree semantic_type = NULL_TREE; 8384 bool maybe_const = true; 8385 bool npc; 8386 8387 if (type == error_mark_node || value == error_mark_node) 8388 { 8389 constructor_erroneous = 1; 8390 return; 8391 } 8392 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE 8393 && (TREE_CODE (value) == STRING_CST 8394 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR) 8395 && !(TREE_CODE (value) == STRING_CST 8396 && TREE_CODE (type) == ARRAY_TYPE 8397 && INTEGRAL_TYPE_P (TREE_TYPE (type))) 8398 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), 8399 TYPE_MAIN_VARIANT (type))) 8400 value = array_to_pointer_conversion (input_location, value); 8401 8402 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR 8403 && require_constant_value && pending) 8404 { 8405 /* As an extension, allow initializing objects with static storage 8406 duration with compound literals (which are then treated just as 8407 the brace enclosed list they contain). */ 8408 if (flag_isoc99) 8409 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not " 8410 "constant"); 8411 tree decl = COMPOUND_LITERAL_EXPR_DECL (value); 8412 value = DECL_INITIAL (decl); 8413 } 8414 8415 npc = null_pointer_constant_p (value); 8416 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR) 8417 { 8418 semantic_type = TREE_TYPE (value); 8419 value = TREE_OPERAND (value, 0); 8420 } 8421 value = c_fully_fold (value, require_constant_value, &maybe_const); 8422 8423 if (value == error_mark_node) 8424 constructor_erroneous = 1; 8425 else if (!TREE_CONSTANT (value)) 8426 constructor_constant = 0; 8427 else if (!initializer_constant_valid_p (value, TREE_TYPE (value)) 8428 || ((TREE_CODE (constructor_type) == RECORD_TYPE 8429 || TREE_CODE (constructor_type) == UNION_TYPE) 8430 && DECL_C_BIT_FIELD (field) 8431 && TREE_CODE (value) != INTEGER_CST)) 8432 constructor_simple = 0; 8433 if (!maybe_const) 8434 constructor_nonconst = 1; 8435 8436 if (!initializer_constant_valid_p (value, TREE_TYPE (value))) 8437 { 8438 if (require_constant_value) 8439 { 8440 error_init (loc, "initializer element is not constant"); 8441 value = error_mark_node; 8442 } 8443 else if (require_constant_elements) 8444 pedwarn (loc, OPT_Wpedantic, 8445 "initializer element is not computable at load time"); 8446 } 8447 else if (!maybe_const 8448 && (require_constant_value || require_constant_elements)) 8449 pedwarn_init (loc, OPT_Wpedantic, 8450 "initializer element is not a constant expression"); 8451 8452 /* Issue -Wc++-compat warnings about initializing a bitfield with 8453 enum type. */ 8454 if (warn_cxx_compat 8455 && field != NULL_TREE 8456 && TREE_CODE (field) == FIELD_DECL 8457 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE 8458 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)) 8459 != TYPE_MAIN_VARIANT (type)) 8460 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE) 8461 { 8462 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value); 8463 if (checktype != error_mark_node 8464 && (TYPE_MAIN_VARIANT (checktype) 8465 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)))) 8466 warning_init (loc, OPT_Wc___compat, 8467 "enum conversion in initialization is invalid in C++"); 8468 } 8469 8470 /* If this field is empty (and not at the end of structure), 8471 don't do anything other than checking the initializer. */ 8472 if (field 8473 && (TREE_TYPE (field) == error_mark_node 8474 || (COMPLETE_TYPE_P (TREE_TYPE (field)) 8475 && integer_zerop (TYPE_SIZE (TREE_TYPE (field))) 8476 && (TREE_CODE (constructor_type) == ARRAY_TYPE 8477 || DECL_CHAIN (field))))) 8478 return; 8479 8480 if (semantic_type) 8481 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value); 8482 value = digest_init (loc, type, value, origtype, npc, strict_string, 8483 require_constant_value); 8484 if (value == error_mark_node) 8485 { 8486 constructor_erroneous = 1; 8487 return; 8488 } 8489 if (require_constant_value || require_constant_elements) 8490 constant_expression_warning (value); 8491 8492 /* If this element doesn't come next in sequence, 8493 put it on constructor_pending_elts. */ 8494 if (TREE_CODE (constructor_type) == ARRAY_TYPE 8495 && (!constructor_incremental 8496 || !tree_int_cst_equal (field, constructor_unfilled_index))) 8497 { 8498 if (constructor_incremental 8499 && tree_int_cst_lt (field, constructor_unfilled_index)) 8500 set_nonincremental_init (braced_init_obstack); 8501 8502 add_pending_init (loc, field, value, origtype, implicit, 8503 braced_init_obstack); 8504 return; 8505 } 8506 else if (TREE_CODE (constructor_type) == RECORD_TYPE 8507 && (!constructor_incremental 8508 || field != constructor_unfilled_fields)) 8509 { 8510 /* We do this for records but not for unions. In a union, 8511 no matter which field is specified, it can be initialized 8512 right away since it starts at the beginning of the union. */ 8513 if (constructor_incremental) 8514 { 8515 if (!constructor_unfilled_fields) 8516 set_nonincremental_init (braced_init_obstack); 8517 else 8518 { 8519 tree bitpos, unfillpos; 8520 8521 bitpos = bit_position (field); 8522 unfillpos = bit_position (constructor_unfilled_fields); 8523 8524 if (tree_int_cst_lt (bitpos, unfillpos)) 8525 set_nonincremental_init (braced_init_obstack); 8526 } 8527 } 8528 8529 add_pending_init (loc, field, value, origtype, implicit, 8530 braced_init_obstack); 8531 return; 8532 } 8533 else if (TREE_CODE (constructor_type) == UNION_TYPE 8534 && !vec_safe_is_empty (constructor_elements)) 8535 { 8536 if (!implicit) 8537 { 8538 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value)) 8539 warning_init (loc, 0, 8540 "initialized field with side-effects overwritten"); 8541 else if (warn_override_init) 8542 warning_init (loc, OPT_Woverride_init, 8543 "initialized field overwritten"); 8544 } 8545 8546 /* We can have just one union field set. */ 8547 constructor_elements = NULL; 8548 } 8549 8550 /* Otherwise, output this element either to 8551 constructor_elements or to the assembler file. */ 8552 8553 constructor_elt celt = {field, value}; 8554 vec_safe_push (constructor_elements, celt); 8555 8556 /* Advance the variable that indicates sequential elements output. */ 8557 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 8558 constructor_unfilled_index 8559 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index, 8560 bitsize_one_node); 8561 else if (TREE_CODE (constructor_type) == RECORD_TYPE) 8562 { 8563 constructor_unfilled_fields 8564 = DECL_CHAIN (constructor_unfilled_fields); 8565 8566 /* Skip any nameless bit fields. */ 8567 while (constructor_unfilled_fields != 0 8568 && DECL_C_BIT_FIELD (constructor_unfilled_fields) 8569 && DECL_NAME (constructor_unfilled_fields) == 0) 8570 constructor_unfilled_fields = 8571 DECL_CHAIN (constructor_unfilled_fields); 8572 } 8573 else if (TREE_CODE (constructor_type) == UNION_TYPE) 8574 constructor_unfilled_fields = 0; 8575 8576 /* Now output any pending elements which have become next. */ 8577 if (pending) 8578 output_pending_init_elements (0, braced_init_obstack); 8579} 8580 8581/* Output any pending elements which have become next. 8582 As we output elements, constructor_unfilled_{fields,index} 8583 advances, which may cause other elements to become next; 8584 if so, they too are output. 8585 8586 If ALL is 0, we return when there are 8587 no more pending elements to output now. 8588 8589 If ALL is 1, we output space as necessary so that 8590 we can output all the pending elements. */ 8591static void 8592output_pending_init_elements (int all, struct obstack * braced_init_obstack) 8593{ 8594 struct init_node *elt = constructor_pending_elts; 8595 tree next; 8596 8597 retry: 8598 8599 /* Look through the whole pending tree. 8600 If we find an element that should be output now, 8601 output it. Otherwise, set NEXT to the element 8602 that comes first among those still pending. */ 8603 8604 next = 0; 8605 while (elt) 8606 { 8607 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 8608 { 8609 if (tree_int_cst_equal (elt->purpose, 8610 constructor_unfilled_index)) 8611 output_init_element (input_location, elt->value, elt->origtype, 8612 true, TREE_TYPE (constructor_type), 8613 constructor_unfilled_index, 0, false, 8614 braced_init_obstack); 8615 else if (tree_int_cst_lt (constructor_unfilled_index, 8616 elt->purpose)) 8617 { 8618 /* Advance to the next smaller node. */ 8619 if (elt->left) 8620 elt = elt->left; 8621 else 8622 { 8623 /* We have reached the smallest node bigger than the 8624 current unfilled index. Fill the space first. */ 8625 next = elt->purpose; 8626 break; 8627 } 8628 } 8629 else 8630 { 8631 /* Advance to the next bigger node. */ 8632 if (elt->right) 8633 elt = elt->right; 8634 else 8635 { 8636 /* We have reached the biggest node in a subtree. Find 8637 the parent of it, which is the next bigger node. */ 8638 while (elt->parent && elt->parent->right == elt) 8639 elt = elt->parent; 8640 elt = elt->parent; 8641 if (elt && tree_int_cst_lt (constructor_unfilled_index, 8642 elt->purpose)) 8643 { 8644 next = elt->purpose; 8645 break; 8646 } 8647 } 8648 } 8649 } 8650 else if (TREE_CODE (constructor_type) == RECORD_TYPE 8651 || TREE_CODE (constructor_type) == UNION_TYPE) 8652 { 8653 tree ctor_unfilled_bitpos, elt_bitpos; 8654 8655 /* If the current record is complete we are done. */ 8656 if (constructor_unfilled_fields == 0) 8657 break; 8658 8659 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields); 8660 elt_bitpos = bit_position (elt->purpose); 8661 /* We can't compare fields here because there might be empty 8662 fields in between. */ 8663 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos)) 8664 { 8665 constructor_unfilled_fields = elt->purpose; 8666 output_init_element (input_location, elt->value, elt->origtype, 8667 true, TREE_TYPE (elt->purpose), 8668 elt->purpose, 0, false, 8669 braced_init_obstack); 8670 } 8671 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos)) 8672 { 8673 /* Advance to the next smaller node. */ 8674 if (elt->left) 8675 elt = elt->left; 8676 else 8677 { 8678 /* We have reached the smallest node bigger than the 8679 current unfilled field. Fill the space first. */ 8680 next = elt->purpose; 8681 break; 8682 } 8683 } 8684 else 8685 { 8686 /* Advance to the next bigger node. */ 8687 if (elt->right) 8688 elt = elt->right; 8689 else 8690 { 8691 /* We have reached the biggest node in a subtree. Find 8692 the parent of it, which is the next bigger node. */ 8693 while (elt->parent && elt->parent->right == elt) 8694 elt = elt->parent; 8695 elt = elt->parent; 8696 if (elt 8697 && (tree_int_cst_lt (ctor_unfilled_bitpos, 8698 bit_position (elt->purpose)))) 8699 { 8700 next = elt->purpose; 8701 break; 8702 } 8703 } 8704 } 8705 } 8706 } 8707 8708 /* Ordinarily return, but not if we want to output all 8709 and there are elements left. */ 8710 if (!(all && next != 0)) 8711 return; 8712 8713 /* If it's not incremental, just skip over the gap, so that after 8714 jumping to retry we will output the next successive element. */ 8715 if (TREE_CODE (constructor_type) == RECORD_TYPE 8716 || TREE_CODE (constructor_type) == UNION_TYPE) 8717 constructor_unfilled_fields = next; 8718 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 8719 constructor_unfilled_index = next; 8720 8721 /* ELT now points to the node in the pending tree with the next 8722 initializer to output. */ 8723 goto retry; 8724} 8725 8726/* Add one non-braced element to the current constructor level. 8727 This adjusts the current position within the constructor's type. 8728 This may also start or terminate implicit levels 8729 to handle a partly-braced initializer. 8730 8731 Once this has found the correct level for the new element, 8732 it calls output_init_element. 8733 8734 IMPLICIT is true if value comes from pop_init_level (1), 8735 the new initializer has been merged with the existing one 8736 and thus no warnings should be emitted about overriding an 8737 existing initializer. */ 8738 8739void 8740process_init_element (location_t loc, struct c_expr value, bool implicit, 8741 struct obstack * braced_init_obstack) 8742{ 8743 tree orig_value = value.value; 8744 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST; 8745 bool strict_string = value.original_code == STRING_CST; 8746 bool was_designated = designator_depth != 0; 8747 8748 designator_depth = 0; 8749 designator_erroneous = 0; 8750 8751 if (!implicit && value.value && !integer_zerop (value.value)) 8752 constructor_zeroinit = 0; 8753 8754 /* Handle superfluous braces around string cst as in 8755 char x[] = {"foo"}; */ 8756 if (string_flag 8757 && constructor_type 8758 && !was_designated 8759 && TREE_CODE (constructor_type) == ARRAY_TYPE 8760 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type)) 8761 && integer_zerop (constructor_unfilled_index)) 8762 { 8763 if (constructor_stack->replacement_value.value) 8764 error_init (loc, "excess elements in char array initializer"); 8765 constructor_stack->replacement_value = value; 8766 return; 8767 } 8768 8769 if (constructor_stack->replacement_value.value != 0) 8770 { 8771 error_init (loc, "excess elements in struct initializer"); 8772 return; 8773 } 8774 8775 /* Ignore elements of a brace group if it is entirely superfluous 8776 and has already been diagnosed. */ 8777 if (constructor_type == 0) 8778 return; 8779 8780 if (!implicit && warn_designated_init && !was_designated 8781 && TREE_CODE (constructor_type) == RECORD_TYPE 8782 && lookup_attribute ("designated_init", 8783 TYPE_ATTRIBUTES (constructor_type))) 8784 warning_init (loc, 8785 OPT_Wdesignated_init, 8786 "positional initialization of field " 8787 "in %<struct%> declared with %<designated_init%> attribute"); 8788 8789 /* If we've exhausted any levels that didn't have braces, 8790 pop them now. */ 8791 while (constructor_stack->implicit) 8792 { 8793 if ((TREE_CODE (constructor_type) == RECORD_TYPE 8794 || TREE_CODE (constructor_type) == UNION_TYPE) 8795 && constructor_fields == 0) 8796 process_init_element (loc, 8797 pop_init_level (loc, 1, braced_init_obstack), 8798 true, braced_init_obstack); 8799 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE 8800 || TREE_CODE (constructor_type) == VECTOR_TYPE) 8801 && constructor_max_index 8802 && tree_int_cst_lt (constructor_max_index, 8803 constructor_index)) 8804 process_init_element (loc, 8805 pop_init_level (loc, 1, braced_init_obstack), 8806 true, braced_init_obstack); 8807 else 8808 break; 8809 } 8810 8811 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */ 8812 if (constructor_range_stack) 8813 { 8814 /* If value is a compound literal and we'll be just using its 8815 content, don't put it into a SAVE_EXPR. */ 8816 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR 8817 || !require_constant_value) 8818 { 8819 tree semantic_type = NULL_TREE; 8820 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR) 8821 { 8822 semantic_type = TREE_TYPE (value.value); 8823 value.value = TREE_OPERAND (value.value, 0); 8824 } 8825 value.value = c_save_expr (value.value); 8826 if (semantic_type) 8827 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type, 8828 value.value); 8829 } 8830 } 8831 8832 while (1) 8833 { 8834 if (TREE_CODE (constructor_type) == RECORD_TYPE) 8835 { 8836 tree fieldtype; 8837 enum tree_code fieldcode; 8838 8839 if (constructor_fields == 0) 8840 { 8841 pedwarn_init (loc, 0, "excess elements in struct initializer"); 8842 break; 8843 } 8844 8845 fieldtype = TREE_TYPE (constructor_fields); 8846 if (fieldtype != error_mark_node) 8847 fieldtype = TYPE_MAIN_VARIANT (fieldtype); 8848 fieldcode = TREE_CODE (fieldtype); 8849 8850 /* Error for non-static initialization of a flexible array member. */ 8851 if (fieldcode == ARRAY_TYPE 8852 && !require_constant_value 8853 && TYPE_SIZE (fieldtype) == NULL_TREE 8854 && DECL_CHAIN (constructor_fields) == NULL_TREE) 8855 { 8856 error_init (loc, "non-static initialization of a flexible " 8857 "array member"); 8858 break; 8859 } 8860 8861 /* Error for initialization of a flexible array member with 8862 a string constant if the structure is in an array. E.g.: 8863 struct S { int x; char y[]; }; 8864 struct S s[] = { { 1, "foo" } }; 8865 is invalid. */ 8866 if (string_flag 8867 && fieldcode == ARRAY_TYPE 8868 && constructor_depth > 1 8869 && TYPE_SIZE (fieldtype) == NULL_TREE 8870 && DECL_CHAIN (constructor_fields) == NULL_TREE) 8871 { 8872 bool in_array_p = false; 8873 for (struct constructor_stack *p = constructor_stack; 8874 p && p->type; p = p->next) 8875 if (TREE_CODE (p->type) == ARRAY_TYPE) 8876 { 8877 in_array_p = true; 8878 break; 8879 } 8880 if (in_array_p) 8881 { 8882 error_init (loc, "initialization of flexible array " 8883 "member in a nested context"); 8884 break; 8885 } 8886 } 8887 8888 /* Accept a string constant to initialize a subarray. */ 8889 if (value.value != 0 8890 && fieldcode == ARRAY_TYPE 8891 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) 8892 && string_flag) 8893 value.value = orig_value; 8894 /* Otherwise, if we have come to a subaggregate, 8895 and we don't have an element of its type, push into it. */ 8896 else if (value.value != 0 8897 && value.value != error_mark_node 8898 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype 8899 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE 8900 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) 8901 { 8902 push_init_level (loc, 1, braced_init_obstack); 8903 continue; 8904 } 8905 8906 if (value.value) 8907 { 8908 push_member_name (constructor_fields); 8909 output_init_element (loc, value.value, value.original_type, 8910 strict_string, fieldtype, 8911 constructor_fields, 1, implicit, 8912 braced_init_obstack); 8913 RESTORE_SPELLING_DEPTH (constructor_depth); 8914 } 8915 else 8916 /* Do the bookkeeping for an element that was 8917 directly output as a constructor. */ 8918 { 8919 /* For a record, keep track of end position of last field. */ 8920 if (DECL_SIZE (constructor_fields)) 8921 constructor_bit_index 8922 = size_binop_loc (input_location, PLUS_EXPR, 8923 bit_position (constructor_fields), 8924 DECL_SIZE (constructor_fields)); 8925 8926 /* If the current field was the first one not yet written out, 8927 it isn't now, so update. */ 8928 if (constructor_unfilled_fields == constructor_fields) 8929 { 8930 constructor_unfilled_fields = DECL_CHAIN (constructor_fields); 8931 /* Skip any nameless bit fields. */ 8932 while (constructor_unfilled_fields != 0 8933 && DECL_C_BIT_FIELD (constructor_unfilled_fields) 8934 && DECL_NAME (constructor_unfilled_fields) == 0) 8935 constructor_unfilled_fields = 8936 DECL_CHAIN (constructor_unfilled_fields); 8937 } 8938 } 8939 8940 constructor_fields = DECL_CHAIN (constructor_fields); 8941 /* Skip any nameless bit fields at the beginning. */ 8942 while (constructor_fields != 0 8943 && DECL_C_BIT_FIELD (constructor_fields) 8944 && DECL_NAME (constructor_fields) == 0) 8945 constructor_fields = DECL_CHAIN (constructor_fields); 8946 } 8947 else if (TREE_CODE (constructor_type) == UNION_TYPE) 8948 { 8949 tree fieldtype; 8950 enum tree_code fieldcode; 8951 8952 if (constructor_fields == 0) 8953 { 8954 pedwarn_init (loc, 0, 8955 "excess elements in union initializer"); 8956 break; 8957 } 8958 8959 fieldtype = TREE_TYPE (constructor_fields); 8960 if (fieldtype != error_mark_node) 8961 fieldtype = TYPE_MAIN_VARIANT (fieldtype); 8962 fieldcode = TREE_CODE (fieldtype); 8963 8964 /* Warn that traditional C rejects initialization of unions. 8965 We skip the warning if the value is zero. This is done 8966 under the assumption that the zero initializer in user 8967 code appears conditioned on e.g. __STDC__ to avoid 8968 "missing initializer" warnings and relies on default 8969 initialization to zero in the traditional C case. 8970 We also skip the warning if the initializer is designated, 8971 again on the assumption that this must be conditional on 8972 __STDC__ anyway (and we've already complained about the 8973 member-designator already). */ 8974 if (!in_system_header_at (input_location) && !constructor_designated 8975 && !(value.value && (integer_zerop (value.value) 8976 || real_zerop (value.value)))) 8977 warning (OPT_Wtraditional, "traditional C rejects initialization " 8978 "of unions"); 8979 8980 /* Accept a string constant to initialize a subarray. */ 8981 if (value.value != 0 8982 && fieldcode == ARRAY_TYPE 8983 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) 8984 && string_flag) 8985 value.value = orig_value; 8986 /* Otherwise, if we have come to a subaggregate, 8987 and we don't have an element of its type, push into it. */ 8988 else if (value.value != 0 8989 && value.value != error_mark_node 8990 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype 8991 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE 8992 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) 8993 { 8994 push_init_level (loc, 1, braced_init_obstack); 8995 continue; 8996 } 8997 8998 if (value.value) 8999 { 9000 push_member_name (constructor_fields); 9001 output_init_element (loc, value.value, value.original_type, 9002 strict_string, fieldtype, 9003 constructor_fields, 1, implicit, 9004 braced_init_obstack); 9005 RESTORE_SPELLING_DEPTH (constructor_depth); 9006 } 9007 else 9008 /* Do the bookkeeping for an element that was 9009 directly output as a constructor. */ 9010 { 9011 constructor_bit_index = DECL_SIZE (constructor_fields); 9012 constructor_unfilled_fields = DECL_CHAIN (constructor_fields); 9013 } 9014 9015 constructor_fields = 0; 9016 } 9017 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 9018 { 9019 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); 9020 enum tree_code eltcode = TREE_CODE (elttype); 9021 9022 /* Accept a string constant to initialize a subarray. */ 9023 if (value.value != 0 9024 && eltcode == ARRAY_TYPE 9025 && INTEGRAL_TYPE_P (TREE_TYPE (elttype)) 9026 && string_flag) 9027 value.value = orig_value; 9028 /* Otherwise, if we have come to a subaggregate, 9029 and we don't have an element of its type, push into it. */ 9030 else if (value.value != 0 9031 && value.value != error_mark_node 9032 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype 9033 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE 9034 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE)) 9035 { 9036 push_init_level (loc, 1, braced_init_obstack); 9037 continue; 9038 } 9039 9040 if (constructor_max_index != 0 9041 && (tree_int_cst_lt (constructor_max_index, constructor_index) 9042 || integer_all_onesp (constructor_max_index))) 9043 { 9044 pedwarn_init (loc, 0, 9045 "excess elements in array initializer"); 9046 break; 9047 } 9048 9049 /* Now output the actual element. */ 9050 if (value.value) 9051 { 9052 push_array_bounds (tree_to_uhwi (constructor_index)); 9053 output_init_element (loc, value.value, value.original_type, 9054 strict_string, elttype, 9055 constructor_index, 1, implicit, 9056 braced_init_obstack); 9057 RESTORE_SPELLING_DEPTH (constructor_depth); 9058 } 9059 9060 constructor_index 9061 = size_binop_loc (input_location, PLUS_EXPR, 9062 constructor_index, bitsize_one_node); 9063 9064 if (!value.value) 9065 /* If we are doing the bookkeeping for an element that was 9066 directly output as a constructor, we must update 9067 constructor_unfilled_index. */ 9068 constructor_unfilled_index = constructor_index; 9069 } 9070 else if (TREE_CODE (constructor_type) == VECTOR_TYPE) 9071 { 9072 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); 9073 9074 /* Do a basic check of initializer size. Note that vectors 9075 always have a fixed size derived from their type. */ 9076 if (tree_int_cst_lt (constructor_max_index, constructor_index)) 9077 { 9078 pedwarn_init (loc, 0, 9079 "excess elements in vector initializer"); 9080 break; 9081 } 9082 9083 /* Now output the actual element. */ 9084 if (value.value) 9085 { 9086 if (TREE_CODE (value.value) == VECTOR_CST) 9087 elttype = TYPE_MAIN_VARIANT (constructor_type); 9088 output_init_element (loc, value.value, value.original_type, 9089 strict_string, elttype, 9090 constructor_index, 1, implicit, 9091 braced_init_obstack); 9092 } 9093 9094 constructor_index 9095 = size_binop_loc (input_location, 9096 PLUS_EXPR, constructor_index, bitsize_one_node); 9097 9098 if (!value.value) 9099 /* If we are doing the bookkeeping for an element that was 9100 directly output as a constructor, we must update 9101 constructor_unfilled_index. */ 9102 constructor_unfilled_index = constructor_index; 9103 } 9104 9105 /* Handle the sole element allowed in a braced initializer 9106 for a scalar variable. */ 9107 else if (constructor_type != error_mark_node 9108 && constructor_fields == 0) 9109 { 9110 pedwarn_init (loc, 0, 9111 "excess elements in scalar initializer"); 9112 break; 9113 } 9114 else 9115 { 9116 if (value.value) 9117 output_init_element (loc, value.value, value.original_type, 9118 strict_string, constructor_type, 9119 NULL_TREE, 1, implicit, 9120 braced_init_obstack); 9121 constructor_fields = 0; 9122 } 9123 9124 /* Handle range initializers either at this level or anywhere higher 9125 in the designator stack. */ 9126 if (constructor_range_stack) 9127 { 9128 struct constructor_range_stack *p, *range_stack; 9129 int finish = 0; 9130 9131 range_stack = constructor_range_stack; 9132 constructor_range_stack = 0; 9133 while (constructor_stack != range_stack->stack) 9134 { 9135 gcc_assert (constructor_stack->implicit); 9136 process_init_element (loc, 9137 pop_init_level (loc, 1, 9138 braced_init_obstack), 9139 true, braced_init_obstack); 9140 } 9141 for (p = range_stack; 9142 !p->range_end || tree_int_cst_equal (p->index, p->range_end); 9143 p = p->prev) 9144 { 9145 gcc_assert (constructor_stack->implicit); 9146 process_init_element (loc, 9147 pop_init_level (loc, 1, 9148 braced_init_obstack), 9149 true, braced_init_obstack); 9150 } 9151 9152 p->index = size_binop_loc (input_location, 9153 PLUS_EXPR, p->index, bitsize_one_node); 9154 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev) 9155 finish = 1; 9156 9157 while (1) 9158 { 9159 constructor_index = p->index; 9160 constructor_fields = p->fields; 9161 if (finish && p->range_end && p->index == p->range_start) 9162 { 9163 finish = 0; 9164 p->prev = 0; 9165 } 9166 p = p->next; 9167 if (!p) 9168 break; 9169 finish_implicit_inits (loc, braced_init_obstack); 9170 push_init_level (loc, 2, braced_init_obstack); 9171 p->stack = constructor_stack; 9172 if (p->range_end && tree_int_cst_equal (p->index, p->range_end)) 9173 p->index = p->range_start; 9174 } 9175 9176 if (!finish) 9177 constructor_range_stack = range_stack; 9178 continue; 9179 } 9180 9181 break; 9182 } 9183 9184 constructor_range_stack = 0; 9185} 9186 9187/* Build a complete asm-statement, whose components are a CV_QUALIFIER 9188 (guaranteed to be 'volatile' or null) and ARGS (represented using 9189 an ASM_EXPR node). */ 9190tree 9191build_asm_stmt (tree cv_qualifier, tree args) 9192{ 9193 if (!ASM_VOLATILE_P (args) && cv_qualifier) 9194 ASM_VOLATILE_P (args) = 1; 9195 return add_stmt (args); 9196} 9197 9198/* Build an asm-expr, whose components are a STRING, some OUTPUTS, 9199 some INPUTS, and some CLOBBERS. The latter three may be NULL. 9200 SIMPLE indicates whether there was anything at all after the 9201 string in the asm expression -- asm("blah") and asm("blah" : ) 9202 are subtly different. We use a ASM_EXPR node to represent this. */ 9203tree 9204build_asm_expr (location_t loc, tree string, tree outputs, tree inputs, 9205 tree clobbers, tree labels, bool simple) 9206{ 9207 tree tail; 9208 tree args; 9209 int i; 9210 const char *constraint; 9211 const char **oconstraints; 9212 bool allows_mem, allows_reg, is_inout; 9213 int ninputs, noutputs; 9214 9215 ninputs = list_length (inputs); 9216 noutputs = list_length (outputs); 9217 oconstraints = (const char **) alloca (noutputs * sizeof (const char *)); 9218 9219 string = resolve_asm_operand_names (string, outputs, inputs, labels); 9220 9221 /* Remove output conversions that change the type but not the mode. */ 9222 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail)) 9223 { 9224 tree output = TREE_VALUE (tail); 9225 9226 output = c_fully_fold (output, false, NULL); 9227 9228 /* ??? Really, this should not be here. Users should be using a 9229 proper lvalue, dammit. But there's a long history of using casts 9230 in the output operands. In cases like longlong.h, this becomes a 9231 primitive form of typechecking -- if the cast can be removed, then 9232 the output operand had a type of the proper width; otherwise we'll 9233 get an error. Gross, but ... */ 9234 STRIP_NOPS (output); 9235 9236 if (!lvalue_or_else (loc, output, lv_asm)) 9237 output = error_mark_node; 9238 9239 if (output != error_mark_node 9240 && (TREE_READONLY (output) 9241 || TYPE_READONLY (TREE_TYPE (output)) 9242 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE 9243 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE) 9244 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output))))) 9245 readonly_error (loc, output, lv_asm); 9246 9247 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); 9248 oconstraints[i] = constraint; 9249 9250 if (parse_output_constraint (&constraint, i, ninputs, noutputs, 9251 &allows_mem, &allows_reg, &is_inout)) 9252 { 9253 /* If the operand is going to end up in memory, 9254 mark it addressable. */ 9255 if (!allows_reg && !c_mark_addressable (output)) 9256 output = error_mark_node; 9257 if (!(!allows_reg && allows_mem) 9258 && output != error_mark_node 9259 && VOID_TYPE_P (TREE_TYPE (output))) 9260 { 9261 error_at (loc, "invalid use of void expression"); 9262 output = error_mark_node; 9263 } 9264 } 9265 else 9266 output = error_mark_node; 9267 9268 TREE_VALUE (tail) = output; 9269 } 9270 9271 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail)) 9272 { 9273 tree input; 9274 9275 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); 9276 input = TREE_VALUE (tail); 9277 9278 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0, 9279 oconstraints, &allows_mem, &allows_reg)) 9280 { 9281 /* If the operand is going to end up in memory, 9282 mark it addressable. */ 9283 if (!allows_reg && allows_mem) 9284 { 9285 input = c_fully_fold (input, false, NULL); 9286 9287 /* Strip the nops as we allow this case. FIXME, this really 9288 should be rejected or made deprecated. */ 9289 STRIP_NOPS (input); 9290 if (!c_mark_addressable (input)) 9291 input = error_mark_node; 9292 } 9293 else 9294 { 9295 struct c_expr expr; 9296 memset (&expr, 0, sizeof (expr)); 9297 expr.value = input; 9298 expr = convert_lvalue_to_rvalue (loc, expr, true, false); 9299 input = c_fully_fold (expr.value, false, NULL); 9300 9301 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input))) 9302 { 9303 error_at (loc, "invalid use of void expression"); 9304 input = error_mark_node; 9305 } 9306 } 9307 } 9308 else 9309 input = error_mark_node; 9310 9311 TREE_VALUE (tail) = input; 9312 } 9313 9314 /* ASMs with labels cannot have outputs. This should have been 9315 enforced by the parser. */ 9316 gcc_assert (outputs == NULL || labels == NULL); 9317 9318 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels); 9319 9320 /* asm statements without outputs, including simple ones, are treated 9321 as volatile. */ 9322 ASM_INPUT_P (args) = simple; 9323 ASM_VOLATILE_P (args) = (noutputs == 0); 9324 9325 return args; 9326} 9327 9328/* Generate a goto statement to LABEL. LOC is the location of the 9329 GOTO. */ 9330 9331tree 9332c_finish_goto_label (location_t loc, tree label) 9333{ 9334 tree decl = lookup_label_for_goto (loc, label); 9335 if (!decl) 9336 return NULL_TREE; 9337 TREE_USED (decl) = 1; 9338 { 9339 tree t = build1 (GOTO_EXPR, void_type_node, decl); 9340 SET_EXPR_LOCATION (t, loc); 9341 return add_stmt (t); 9342 } 9343} 9344 9345/* Generate a computed goto statement to EXPR. LOC is the location of 9346 the GOTO. */ 9347 9348tree 9349c_finish_goto_ptr (location_t loc, tree expr) 9350{ 9351 tree t; 9352 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>"); 9353 expr = c_fully_fold (expr, false, NULL); 9354 expr = convert (ptr_type_node, expr); 9355 t = build1 (GOTO_EXPR, void_type_node, expr); 9356 SET_EXPR_LOCATION (t, loc); 9357 return add_stmt (t); 9358} 9359 9360/* Generate a C `return' statement. RETVAL is the expression for what 9361 to return, or a null pointer for `return;' with no value. LOC is 9362 the location of the return statement, or the location of the expression, 9363 if the statement has any. If ORIGTYPE is not NULL_TREE, it 9364 is the original type of RETVAL. */ 9365 9366tree 9367c_finish_return (location_t loc, tree retval, tree origtype) 9368{ 9369 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt; 9370 bool no_warning = false; 9371 bool npc = false; 9372 size_t rank = 0; 9373 9374 /* Use the expansion point to handle cases such as returning NULL 9375 in a function returning void. */ 9376 source_location xloc = expansion_point_location_if_in_system_header (loc); 9377 9378 if (TREE_THIS_VOLATILE (current_function_decl)) 9379 warning_at (xloc, 0, 9380 "function declared %<noreturn%> has a %<return%> statement"); 9381 9382 if (flag_cilkplus && contains_array_notation_expr (retval)) 9383 { 9384 /* Array notations are allowed in a return statement if it is inside a 9385 built-in array notation reduction function. */ 9386 if (!find_rank (loc, retval, retval, false, &rank)) 9387 return error_mark_node; 9388 if (rank >= 1) 9389 { 9390 error_at (loc, "array notation expression cannot be used as a " 9391 "return value"); 9392 return error_mark_node; 9393 } 9394 } 9395 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval)) 9396 { 9397 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not " 9398 "allowed"); 9399 return error_mark_node; 9400 } 9401 if (retval) 9402 { 9403 tree semantic_type = NULL_TREE; 9404 npc = null_pointer_constant_p (retval); 9405 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR) 9406 { 9407 semantic_type = TREE_TYPE (retval); 9408 retval = TREE_OPERAND (retval, 0); 9409 } 9410 retval = c_fully_fold (retval, false, NULL); 9411 if (semantic_type) 9412 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval); 9413 } 9414 9415 if (!retval) 9416 { 9417 current_function_returns_null = 1; 9418 if ((warn_return_type || flag_isoc99) 9419 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) 9420 { 9421 if (flag_isoc99) 9422 pedwarn (loc, 0, "%<return%> with no value, in " 9423 "function returning non-void"); 9424 else 9425 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, " 9426 "in function returning non-void"); 9427 no_warning = true; 9428 } 9429 } 9430 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) 9431 { 9432 current_function_returns_null = 1; 9433 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) 9434 pedwarn (xloc, 0, 9435 "%<return%> with a value, in function returning void"); 9436 else 9437 pedwarn (xloc, OPT_Wpedantic, "ISO C forbids " 9438 "%<return%> with expression, in function returning void"); 9439 } 9440 else 9441 { 9442 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype, 9443 retval, origtype, ic_return, 9444 npc, NULL_TREE, NULL_TREE, 0); 9445 tree res = DECL_RESULT (current_function_decl); 9446 tree inner; 9447 bool save; 9448 9449 current_function_returns_value = 1; 9450 if (t == error_mark_node) 9451 return NULL_TREE; 9452 9453 save = in_late_binary_op; 9454 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE 9455 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE 9456 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE 9457 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE 9458 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE) 9459 && (flag_sanitize & SANITIZE_FLOAT_CAST))) 9460 in_late_binary_op = true; 9461 inner = t = convert (TREE_TYPE (res), t); 9462 in_late_binary_op = save; 9463 9464 /* Strip any conversions, additions, and subtractions, and see if 9465 we are returning the address of a local variable. Warn if so. */ 9466 while (1) 9467 { 9468 switch (TREE_CODE (inner)) 9469 { 9470 CASE_CONVERT: 9471 case NON_LVALUE_EXPR: 9472 case PLUS_EXPR: 9473 case POINTER_PLUS_EXPR: 9474 inner = TREE_OPERAND (inner, 0); 9475 continue; 9476 9477 case MINUS_EXPR: 9478 /* If the second operand of the MINUS_EXPR has a pointer 9479 type (or is converted from it), this may be valid, so 9480 don't give a warning. */ 9481 { 9482 tree op1 = TREE_OPERAND (inner, 1); 9483 9484 while (!POINTER_TYPE_P (TREE_TYPE (op1)) 9485 && (CONVERT_EXPR_P (op1) 9486 || TREE_CODE (op1) == NON_LVALUE_EXPR)) 9487 op1 = TREE_OPERAND (op1, 0); 9488 9489 if (POINTER_TYPE_P (TREE_TYPE (op1))) 9490 break; 9491 9492 inner = TREE_OPERAND (inner, 0); 9493 continue; 9494 } 9495 9496 case ADDR_EXPR: 9497 inner = TREE_OPERAND (inner, 0); 9498 9499 while (REFERENCE_CLASS_P (inner) 9500 && TREE_CODE (inner) != INDIRECT_REF) 9501 inner = TREE_OPERAND (inner, 0); 9502 9503 if (DECL_P (inner) 9504 && !DECL_EXTERNAL (inner) 9505 && !TREE_STATIC (inner) 9506 && DECL_CONTEXT (inner) == current_function_decl) 9507 { 9508 if (TREE_CODE (inner) == LABEL_DECL) 9509 warning_at (loc, OPT_Wreturn_local_addr, 9510 "function returns address of label"); 9511 else 9512 { 9513 warning_at (loc, OPT_Wreturn_local_addr, 9514 "function returns address of local variable"); 9515 tree zero = build_zero_cst (TREE_TYPE (res)); 9516 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero); 9517 } 9518 } 9519 break; 9520 9521 default: 9522 break; 9523 } 9524 9525 break; 9526 } 9527 9528 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t); 9529 SET_EXPR_LOCATION (retval, loc); 9530 9531 if (warn_sequence_point) 9532 verify_sequence_points (retval); 9533 } 9534 9535 ret_stmt = build_stmt (loc, RETURN_EXPR, retval); 9536 TREE_NO_WARNING (ret_stmt) |= no_warning; 9537 return add_stmt (ret_stmt); 9538} 9539 9540struct c_switch { 9541 /* The SWITCH_EXPR being built. */ 9542 tree switch_expr; 9543 9544 /* The original type of the testing expression, i.e. before the 9545 default conversion is applied. */ 9546 tree orig_type; 9547 9548 /* A splay-tree mapping the low element of a case range to the high 9549 element, or NULL_TREE if there is no high element. Used to 9550 determine whether or not a new case label duplicates an old case 9551 label. We need a tree, rather than simply a hash table, because 9552 of the GNU case range extension. */ 9553 splay_tree cases; 9554 9555 /* The bindings at the point of the switch. This is used for 9556 warnings crossing decls when branching to a case label. */ 9557 struct c_spot_bindings *bindings; 9558 9559 /* The next node on the stack. */ 9560 struct c_switch *next; 9561}; 9562 9563/* A stack of the currently active switch statements. The innermost 9564 switch statement is on the top of the stack. There is no need to 9565 mark the stack for garbage collection because it is only active 9566 during the processing of the body of a function, and we never 9567 collect at that point. */ 9568 9569struct c_switch *c_switch_stack; 9570 9571/* Start a C switch statement, testing expression EXP. Return the new 9572 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'. 9573 SWITCH_COND_LOC is the location of the switch's condition. 9574 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */ 9575 9576tree 9577c_start_case (location_t switch_loc, 9578 location_t switch_cond_loc, 9579 tree exp, bool explicit_cast_p) 9580{ 9581 tree orig_type = error_mark_node; 9582 struct c_switch *cs; 9583 9584 if (exp != error_mark_node) 9585 { 9586 orig_type = TREE_TYPE (exp); 9587 9588 if (!INTEGRAL_TYPE_P (orig_type)) 9589 { 9590 if (orig_type != error_mark_node) 9591 { 9592 error_at (switch_cond_loc, "switch quantity not an integer"); 9593 orig_type = error_mark_node; 9594 } 9595 exp = integer_zero_node; 9596 } 9597 else 9598 { 9599 tree type = TYPE_MAIN_VARIANT (orig_type); 9600 tree e = exp; 9601 9602 /* Warn if the condition has boolean value. */ 9603 while (TREE_CODE (e) == COMPOUND_EXPR) 9604 e = TREE_OPERAND (e, 1); 9605 9606 if ((TREE_CODE (type) == BOOLEAN_TYPE 9607 || truth_value_p (TREE_CODE (e))) 9608 /* Explicit cast to int suppresses this warning. */ 9609 && !(TREE_CODE (type) == INTEGER_TYPE 9610 && explicit_cast_p)) 9611 warning_at (switch_cond_loc, OPT_Wswitch_bool, 9612 "switch condition has boolean value"); 9613 9614 if (!in_system_header_at (input_location) 9615 && (type == long_integer_type_node 9616 || type == long_unsigned_type_node)) 9617 warning_at (switch_cond_loc, 9618 OPT_Wtraditional, "%<long%> switch expression not " 9619 "converted to %<int%> in ISO C"); 9620 9621 exp = c_fully_fold (exp, false, NULL); 9622 exp = default_conversion (exp); 9623 9624 if (warn_sequence_point) 9625 verify_sequence_points (exp); 9626 } 9627 } 9628 9629 /* Add this new SWITCH_EXPR to the stack. */ 9630 cs = XNEW (struct c_switch); 9631 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE); 9632 SET_EXPR_LOCATION (cs->switch_expr, switch_loc); 9633 cs->orig_type = orig_type; 9634 cs->cases = splay_tree_new (case_compare, NULL, NULL); 9635 cs->bindings = c_get_switch_bindings (); 9636 cs->next = c_switch_stack; 9637 c_switch_stack = cs; 9638 9639 return add_stmt (cs->switch_expr); 9640} 9641 9642/* Process a case label at location LOC. */ 9643 9644tree 9645do_case (location_t loc, tree low_value, tree high_value) 9646{ 9647 tree label = NULL_TREE; 9648 9649 if (low_value && TREE_CODE (low_value) != INTEGER_CST) 9650 { 9651 low_value = c_fully_fold (low_value, false, NULL); 9652 if (TREE_CODE (low_value) == INTEGER_CST) 9653 pedwarn (loc, OPT_Wpedantic, 9654 "case label is not an integer constant expression"); 9655 } 9656 9657 if (high_value && TREE_CODE (high_value) != INTEGER_CST) 9658 { 9659 high_value = c_fully_fold (high_value, false, NULL); 9660 if (TREE_CODE (high_value) == INTEGER_CST) 9661 pedwarn (input_location, OPT_Wpedantic, 9662 "case label is not an integer constant expression"); 9663 } 9664 9665 if (c_switch_stack == NULL) 9666 { 9667 if (low_value) 9668 error_at (loc, "case label not within a switch statement"); 9669 else 9670 error_at (loc, "%<default%> label not within a switch statement"); 9671 return NULL_TREE; 9672 } 9673 9674 if (c_check_switch_jump_warnings (c_switch_stack->bindings, 9675 EXPR_LOCATION (c_switch_stack->switch_expr), 9676 loc)) 9677 return NULL_TREE; 9678 9679 label = c_add_case_label (loc, c_switch_stack->cases, 9680 SWITCH_COND (c_switch_stack->switch_expr), 9681 c_switch_stack->orig_type, 9682 low_value, high_value); 9683 if (label == error_mark_node) 9684 label = NULL_TREE; 9685 return label; 9686} 9687 9688/* Finish the switch statement. TYPE is the original type of the 9689 controlling expression of the switch, or NULL_TREE. */ 9690 9691void 9692c_finish_case (tree body, tree type) 9693{ 9694 struct c_switch *cs = c_switch_stack; 9695 location_t switch_location; 9696 9697 SWITCH_BODY (cs->switch_expr) = body; 9698 9699 /* Emit warnings as needed. */ 9700 switch_location = EXPR_LOCATION (cs->switch_expr); 9701 c_do_switch_warnings (cs->cases, switch_location, 9702 type ? type : TREE_TYPE (cs->switch_expr), 9703 SWITCH_COND (cs->switch_expr)); 9704 9705 /* Pop the stack. */ 9706 c_switch_stack = cs->next; 9707 splay_tree_delete (cs->cases); 9708 c_release_switch_bindings (cs->bindings); 9709 XDELETE (cs); 9710} 9711 9712/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND, 9713 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK 9714 may be null. NESTED_IF is true if THEN_BLOCK contains another IF 9715 statement, and was not surrounded with parenthesis. */ 9716 9717void 9718c_finish_if_stmt (location_t if_locus, tree cond, tree then_block, 9719 tree else_block, bool nested_if) 9720{ 9721 tree stmt; 9722 9723 /* If the condition has array notations, then the rank of the then_block and 9724 else_block must be either 0 or be equal to the rank of the condition. If 9725 the condition does not have array notations then break them up as it is 9726 broken up in a normal expression. */ 9727 if (flag_cilkplus && contains_array_notation_expr (cond)) 9728 { 9729 size_t then_rank = 0, cond_rank = 0, else_rank = 0; 9730 if (!find_rank (if_locus, cond, cond, true, &cond_rank)) 9731 return; 9732 if (then_block 9733 && !find_rank (if_locus, then_block, then_block, true, &then_rank)) 9734 return; 9735 if (else_block 9736 && !find_rank (if_locus, else_block, else_block, true, &else_rank)) 9737 return; 9738 if (cond_rank != then_rank && then_rank != 0) 9739 { 9740 error_at (if_locus, "rank-mismatch between if-statement%'s condition" 9741 " and the then-block"); 9742 return; 9743 } 9744 else if (cond_rank != else_rank && else_rank != 0) 9745 { 9746 error_at (if_locus, "rank-mismatch between if-statement%'s condition" 9747 " and the else-block"); 9748 return; 9749 } 9750 } 9751 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */ 9752 if (warn_parentheses && nested_if && else_block == NULL) 9753 { 9754 tree inner_if = then_block; 9755 9756 /* We know from the grammar productions that there is an IF nested 9757 within THEN_BLOCK. Due to labels and c99 conditional declarations, 9758 it might not be exactly THEN_BLOCK, but should be the last 9759 non-container statement within. */ 9760 while (1) 9761 switch (TREE_CODE (inner_if)) 9762 { 9763 case COND_EXPR: 9764 goto found; 9765 case BIND_EXPR: 9766 inner_if = BIND_EXPR_BODY (inner_if); 9767 break; 9768 case STATEMENT_LIST: 9769 inner_if = expr_last (then_block); 9770 break; 9771 case TRY_FINALLY_EXPR: 9772 case TRY_CATCH_EXPR: 9773 inner_if = TREE_OPERAND (inner_if, 0); 9774 break; 9775 default: 9776 gcc_unreachable (); 9777 } 9778 found: 9779 9780 if (COND_EXPR_ELSE (inner_if)) 9781 warning_at (if_locus, OPT_Wparentheses, 9782 "suggest explicit braces to avoid ambiguous %<else%>"); 9783 } 9784 9785 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block); 9786 SET_EXPR_LOCATION (stmt, if_locus); 9787 add_stmt (stmt); 9788} 9789 9790/* Emit a general-purpose loop construct. START_LOCUS is the location of 9791 the beginning of the loop. COND is the loop condition. COND_IS_FIRST 9792 is false for DO loops. INCR is the FOR increment expression. BODY is 9793 the statement controlled by the loop. BLAB is the break label. CLAB is 9794 the continue label. Everything is allowed to be NULL. */ 9795 9796void 9797c_finish_loop (location_t start_locus, tree cond, tree incr, tree body, 9798 tree blab, tree clab, bool cond_is_first) 9799{ 9800 tree entry = NULL, exit = NULL, t; 9801 9802 /* In theory could forbid cilk spawn for loop increment expression, 9803 but it should work just fine. */ 9804 9805 /* If the condition is zero don't generate a loop construct. */ 9806 if (cond && integer_zerop (cond)) 9807 { 9808 if (cond_is_first) 9809 { 9810 t = build_and_jump (&blab); 9811 SET_EXPR_LOCATION (t, start_locus); 9812 add_stmt (t); 9813 } 9814 } 9815 else 9816 { 9817 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE); 9818 9819 /* If we have an exit condition, then we build an IF with gotos either 9820 out of the loop, or to the top of it. If there's no exit condition, 9821 then we just build a jump back to the top. */ 9822 exit = build_and_jump (&LABEL_EXPR_LABEL (top)); 9823 9824 if (cond && !integer_nonzerop (cond)) 9825 { 9826 /* Canonicalize the loop condition to the end. This means 9827 generating a branch to the loop condition. Reuse the 9828 continue label, if possible. */ 9829 if (cond_is_first) 9830 { 9831 if (incr || !clab) 9832 { 9833 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE); 9834 t = build_and_jump (&LABEL_EXPR_LABEL (entry)); 9835 } 9836 else 9837 t = build1 (GOTO_EXPR, void_type_node, clab); 9838 SET_EXPR_LOCATION (t, start_locus); 9839 add_stmt (t); 9840 } 9841 9842 t = build_and_jump (&blab); 9843 if (cond_is_first) 9844 exit = fold_build3_loc (start_locus, 9845 COND_EXPR, void_type_node, cond, exit, t); 9846 else 9847 exit = fold_build3_loc (input_location, 9848 COND_EXPR, void_type_node, cond, exit, t); 9849 } 9850 9851 add_stmt (top); 9852 } 9853 9854 if (body) 9855 add_stmt (body); 9856 if (clab) 9857 add_stmt (build1 (LABEL_EXPR, void_type_node, clab)); 9858 if (incr) 9859 add_stmt (incr); 9860 if (entry) 9861 add_stmt (entry); 9862 if (exit) 9863 add_stmt (exit); 9864 if (blab) 9865 add_stmt (build1 (LABEL_EXPR, void_type_node, blab)); 9866} 9867 9868tree 9869c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break) 9870{ 9871 bool skip; 9872 tree label = *label_p; 9873 9874 /* In switch statements break is sometimes stylistically used after 9875 a return statement. This can lead to spurious warnings about 9876 control reaching the end of a non-void function when it is 9877 inlined. Note that we are calling block_may_fallthru with 9878 language specific tree nodes; this works because 9879 block_may_fallthru returns true when given something it does not 9880 understand. */ 9881 skip = !block_may_fallthru (cur_stmt_list); 9882 9883 if (!label) 9884 { 9885 if (!skip) 9886 *label_p = label = create_artificial_label (loc); 9887 } 9888 else if (TREE_CODE (label) == LABEL_DECL) 9889 ; 9890 else switch (TREE_INT_CST_LOW (label)) 9891 { 9892 case 0: 9893 if (is_break) 9894 error_at (loc, "break statement not within loop or switch"); 9895 else 9896 error_at (loc, "continue statement not within a loop"); 9897 return NULL_TREE; 9898 9899 case 1: 9900 gcc_assert (is_break); 9901 error_at (loc, "break statement used with OpenMP for loop"); 9902 return NULL_TREE; 9903 9904 case 2: 9905 if (is_break) 9906 error ("break statement within %<#pragma simd%> loop body"); 9907 else 9908 error ("continue statement within %<#pragma simd%> loop body"); 9909 return NULL_TREE; 9910 9911 default: 9912 gcc_unreachable (); 9913 } 9914 9915 if (skip) 9916 return NULL_TREE; 9917 9918 if (!is_break) 9919 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN)); 9920 9921 return add_stmt (build1 (GOTO_EXPR, void_type_node, label)); 9922} 9923 9924/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */ 9925 9926static void 9927emit_side_effect_warnings (location_t loc, tree expr) 9928{ 9929 if (expr == error_mark_node) 9930 ; 9931 else if (!TREE_SIDE_EFFECTS (expr)) 9932 { 9933 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr)) 9934 warning_at (loc, OPT_Wunused_value, "statement with no effect"); 9935 } 9936 else if (TREE_CODE (expr) == COMPOUND_EXPR) 9937 { 9938 tree r = expr; 9939 location_t cloc = loc; 9940 while (TREE_CODE (r) == COMPOUND_EXPR) 9941 { 9942 if (EXPR_HAS_LOCATION (r)) 9943 cloc = EXPR_LOCATION (r); 9944 r = TREE_OPERAND (r, 1); 9945 } 9946 if (!TREE_SIDE_EFFECTS (r) 9947 && !VOID_TYPE_P (TREE_TYPE (r)) 9948 && !CONVERT_EXPR_P (r) 9949 && !TREE_NO_WARNING (r) 9950 && !TREE_NO_WARNING (expr)) 9951 warning_at (cloc, OPT_Wunused_value, 9952 "right-hand operand of comma expression has no effect"); 9953 } 9954 else 9955 warn_if_unused_value (expr, loc); 9956} 9957 9958/* Process an expression as if it were a complete statement. Emit 9959 diagnostics, but do not call ADD_STMT. LOC is the location of the 9960 statement. */ 9961 9962tree 9963c_process_expr_stmt (location_t loc, tree expr) 9964{ 9965 tree exprv; 9966 9967 if (!expr) 9968 return NULL_TREE; 9969 9970 expr = c_fully_fold (expr, false, NULL); 9971 9972 if (warn_sequence_point) 9973 verify_sequence_points (expr); 9974 9975 if (TREE_TYPE (expr) != error_mark_node 9976 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) 9977 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) 9978 error_at (loc, "expression statement has incomplete type"); 9979 9980 /* If we're not processing a statement expression, warn about unused values. 9981 Warnings for statement expressions will be emitted later, once we figure 9982 out which is the result. */ 9983 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list) 9984 && warn_unused_value) 9985 emit_side_effect_warnings (loc, expr); 9986 9987 exprv = expr; 9988 while (TREE_CODE (exprv) == COMPOUND_EXPR) 9989 exprv = TREE_OPERAND (exprv, 1); 9990 while (CONVERT_EXPR_P (exprv)) 9991 exprv = TREE_OPERAND (exprv, 0); 9992 if (DECL_P (exprv) 9993 || handled_component_p (exprv) 9994 || TREE_CODE (exprv) == ADDR_EXPR) 9995 mark_exp_read (exprv); 9996 9997 /* If the expression is not of a type to which we cannot assign a line 9998 number, wrap the thing in a no-op NOP_EXPR. */ 9999 if (DECL_P (expr) || CONSTANT_CLASS_P (expr)) 10000 { 10001 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr); 10002 SET_EXPR_LOCATION (expr, loc); 10003 } 10004 10005 return expr; 10006} 10007 10008/* Emit an expression as a statement. LOC is the location of the 10009 expression. */ 10010 10011tree 10012c_finish_expr_stmt (location_t loc, tree expr) 10013{ 10014 if (expr) 10015 return add_stmt (c_process_expr_stmt (loc, expr)); 10016 else 10017 return NULL; 10018} 10019 10020/* Do the opposite and emit a statement as an expression. To begin, 10021 create a new binding level and return it. */ 10022 10023tree 10024c_begin_stmt_expr (void) 10025{ 10026 tree ret; 10027 10028 /* We must force a BLOCK for this level so that, if it is not expanded 10029 later, there is a way to turn off the entire subtree of blocks that 10030 are contained in it. */ 10031 keep_next_level (); 10032 ret = c_begin_compound_stmt (true); 10033 10034 c_bindings_start_stmt_expr (c_switch_stack == NULL 10035 ? NULL 10036 : c_switch_stack->bindings); 10037 10038 /* Mark the current statement list as belonging to a statement list. */ 10039 STATEMENT_LIST_STMT_EXPR (ret) = 1; 10040 10041 return ret; 10042} 10043 10044/* LOC is the location of the compound statement to which this body 10045 belongs. */ 10046 10047tree 10048c_finish_stmt_expr (location_t loc, tree body) 10049{ 10050 tree last, type, tmp, val; 10051 tree *last_p; 10052 10053 body = c_end_compound_stmt (loc, body, true); 10054 10055 c_bindings_end_stmt_expr (c_switch_stack == NULL 10056 ? NULL 10057 : c_switch_stack->bindings); 10058 10059 /* Locate the last statement in BODY. See c_end_compound_stmt 10060 about always returning a BIND_EXPR. */ 10061 last_p = &BIND_EXPR_BODY (body); 10062 last = BIND_EXPR_BODY (body); 10063 10064 continue_searching: 10065 if (TREE_CODE (last) == STATEMENT_LIST) 10066 { 10067 tree_stmt_iterator i; 10068 10069 /* This can happen with degenerate cases like ({ }). No value. */ 10070 if (!TREE_SIDE_EFFECTS (last)) 10071 return body; 10072 10073 /* If we're supposed to generate side effects warnings, process 10074 all of the statements except the last. */ 10075 if (warn_unused_value) 10076 { 10077 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i)) 10078 { 10079 location_t tloc; 10080 tree t = tsi_stmt (i); 10081 10082 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc; 10083 emit_side_effect_warnings (tloc, t); 10084 } 10085 } 10086 else 10087 i = tsi_last (last); 10088 last_p = tsi_stmt_ptr (i); 10089 last = *last_p; 10090 } 10091 10092 /* If the end of the list is exception related, then the list was split 10093 by a call to push_cleanup. Continue searching. */ 10094 if (TREE_CODE (last) == TRY_FINALLY_EXPR 10095 || TREE_CODE (last) == TRY_CATCH_EXPR) 10096 { 10097 last_p = &TREE_OPERAND (last, 0); 10098 last = *last_p; 10099 goto continue_searching; 10100 } 10101 10102 if (last == error_mark_node) 10103 return last; 10104 10105 /* In the case that the BIND_EXPR is not necessary, return the 10106 expression out from inside it. */ 10107 if (last == BIND_EXPR_BODY (body) 10108 && BIND_EXPR_VARS (body) == NULL) 10109 { 10110 /* Even if this looks constant, do not allow it in a constant 10111 expression. */ 10112 last = c_wrap_maybe_const (last, true); 10113 /* Do not warn if the return value of a statement expression is 10114 unused. */ 10115 TREE_NO_WARNING (last) = 1; 10116 return last; 10117 } 10118 10119 /* Extract the type of said expression. */ 10120 type = TREE_TYPE (last); 10121 10122 /* If we're not returning a value at all, then the BIND_EXPR that 10123 we already have is a fine expression to return. */ 10124 if (!type || VOID_TYPE_P (type)) 10125 return body; 10126 10127 /* Now that we've located the expression containing the value, it seems 10128 silly to make voidify_wrapper_expr repeat the process. Create a 10129 temporary of the appropriate type and stick it in a TARGET_EXPR. */ 10130 tmp = create_tmp_var_raw (type); 10131 10132 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids 10133 tree_expr_nonnegative_p giving up immediately. */ 10134 val = last; 10135 if (TREE_CODE (val) == NOP_EXPR 10136 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))) 10137 val = TREE_OPERAND (val, 0); 10138 10139 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val); 10140 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last)); 10141 10142 { 10143 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE); 10144 SET_EXPR_LOCATION (t, loc); 10145 return t; 10146 } 10147} 10148 10149/* Begin and end compound statements. This is as simple as pushing 10150 and popping new statement lists from the tree. */ 10151 10152tree 10153c_begin_compound_stmt (bool do_scope) 10154{ 10155 tree stmt = push_stmt_list (); 10156 if (do_scope) 10157 push_scope (); 10158 return stmt; 10159} 10160 10161/* End a compound statement. STMT is the statement. LOC is the 10162 location of the compound statement-- this is usually the location 10163 of the opening brace. */ 10164 10165tree 10166c_end_compound_stmt (location_t loc, tree stmt, bool do_scope) 10167{ 10168 tree block = NULL; 10169 10170 if (do_scope) 10171 { 10172 if (c_dialect_objc ()) 10173 objc_clear_super_receiver (); 10174 block = pop_scope (); 10175 } 10176 10177 stmt = pop_stmt_list (stmt); 10178 stmt = c_build_bind_expr (loc, block, stmt); 10179 10180 /* If this compound statement is nested immediately inside a statement 10181 expression, then force a BIND_EXPR to be created. Otherwise we'll 10182 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular, 10183 STATEMENT_LISTs merge, and thus we can lose track of what statement 10184 was really last. */ 10185 if (building_stmt_list_p () 10186 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list) 10187 && TREE_CODE (stmt) != BIND_EXPR) 10188 { 10189 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL); 10190 TREE_SIDE_EFFECTS (stmt) = 1; 10191 SET_EXPR_LOCATION (stmt, loc); 10192 } 10193 10194 return stmt; 10195} 10196 10197/* Queue a cleanup. CLEANUP is an expression/statement to be executed 10198 when the current scope is exited. EH_ONLY is true when this is not 10199 meant to apply to normal control flow transfer. */ 10200 10201void 10202push_cleanup (tree decl, tree cleanup, bool eh_only) 10203{ 10204 enum tree_code code; 10205 tree stmt, list; 10206 bool stmt_expr; 10207 10208 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR; 10209 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup); 10210 add_stmt (stmt); 10211 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list); 10212 list = push_stmt_list (); 10213 TREE_OPERAND (stmt, 0) = list; 10214 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr; 10215} 10216 10217/* Build a binary-operation expression without default conversions. 10218 CODE is the kind of expression to build. 10219 LOCATION is the operator's location. 10220 This function differs from `build' in several ways: 10221 the data type of the result is computed and recorded in it, 10222 warnings are generated if arg data types are invalid, 10223 special handling for addition and subtraction of pointers is known, 10224 and some optimization is done (operations on narrow ints 10225 are done in the narrower type when that gives the same result). 10226 Constant folding is also done before the result is returned. 10227 10228 Note that the operands will never have enumeral types, or function 10229 or array types, because either they will have the default conversions 10230 performed or they have both just been converted to some other type in which 10231 the arithmetic is to be done. */ 10232 10233tree 10234build_binary_op (location_t location, enum tree_code code, 10235 tree orig_op0, tree orig_op1, int convert_p) 10236{ 10237 tree type0, type1, orig_type0, orig_type1; 10238 tree eptype; 10239 enum tree_code code0, code1; 10240 tree op0, op1; 10241 tree ret = error_mark_node; 10242 const char *invalid_op_diag; 10243 bool op0_int_operands, op1_int_operands; 10244 bool int_const, int_const_or_overflow, int_operands; 10245 10246 /* Expression code to give to the expression when it is built. 10247 Normally this is CODE, which is what the caller asked for, 10248 but in some special cases we change it. */ 10249 enum tree_code resultcode = code; 10250 10251 /* Data type in which the computation is to be performed. 10252 In the simplest cases this is the common type of the arguments. */ 10253 tree result_type = NULL; 10254 10255 /* When the computation is in excess precision, the type of the 10256 final EXCESS_PRECISION_EXPR. */ 10257 tree semantic_result_type = NULL; 10258 10259 /* Nonzero means operands have already been type-converted 10260 in whatever way is necessary. 10261 Zero means they need to be converted to RESULT_TYPE. */ 10262 int converted = 0; 10263 10264 /* Nonzero means create the expression with this type, rather than 10265 RESULT_TYPE. */ 10266 tree build_type = 0; 10267 10268 /* Nonzero means after finally constructing the expression 10269 convert it to this type. */ 10270 tree final_type = 0; 10271 10272 /* Nonzero if this is an operation like MIN or MAX which can 10273 safely be computed in short if both args are promoted shorts. 10274 Also implies COMMON. 10275 -1 indicates a bitwise operation; this makes a difference 10276 in the exact conditions for when it is safe to do the operation 10277 in a narrower mode. */ 10278 int shorten = 0; 10279 10280 /* Nonzero if this is a comparison operation; 10281 if both args are promoted shorts, compare the original shorts. 10282 Also implies COMMON. */ 10283 int short_compare = 0; 10284 10285 /* Nonzero if this is a right-shift operation, which can be computed on the 10286 original short and then promoted if the operand is a promoted short. */ 10287 int short_shift = 0; 10288 10289 /* Nonzero means set RESULT_TYPE to the common type of the args. */ 10290 int common = 0; 10291 10292 /* True means types are compatible as far as ObjC is concerned. */ 10293 bool objc_ok; 10294 10295 /* True means this is an arithmetic operation that may need excess 10296 precision. */ 10297 bool may_need_excess_precision; 10298 10299 /* True means this is a boolean operation that converts both its 10300 operands to truth-values. */ 10301 bool boolean_op = false; 10302 10303 /* Remember whether we're doing / or %. */ 10304 bool doing_div_or_mod = false; 10305 10306 /* Remember whether we're doing << or >>. */ 10307 bool doing_shift = false; 10308 10309 /* Tree holding instrumentation expression. */ 10310 tree instrument_expr = NULL; 10311 10312 if (location == UNKNOWN_LOCATION) 10313 location = input_location; 10314 10315 op0 = orig_op0; 10316 op1 = orig_op1; 10317 10318 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0); 10319 if (op0_int_operands) 10320 op0 = remove_c_maybe_const_expr (op0); 10321 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); 10322 if (op1_int_operands) 10323 op1 = remove_c_maybe_const_expr (op1); 10324 int_operands = (op0_int_operands && op1_int_operands); 10325 if (int_operands) 10326 { 10327 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST 10328 && TREE_CODE (orig_op1) == INTEGER_CST); 10329 int_const = (int_const_or_overflow 10330 && !TREE_OVERFLOW (orig_op0) 10331 && !TREE_OVERFLOW (orig_op1)); 10332 } 10333 else 10334 int_const = int_const_or_overflow = false; 10335 10336 /* Do not apply default conversion in mixed vector/scalar expression. */ 10337 if (convert_p 10338 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE) 10339 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE))) 10340 { 10341 op0 = default_conversion (op0); 10342 op1 = default_conversion (op1); 10343 } 10344 10345 /* When Cilk Plus is enabled and there are array notations inside op0, then 10346 we check to see if there are builtin array notation functions. If 10347 so, then we take on the type of the array notation inside it. */ 10348 if (flag_cilkplus && contains_array_notation_expr (op0)) 10349 orig_type0 = type0 = find_correct_array_notation_type (op0); 10350 else 10351 orig_type0 = type0 = TREE_TYPE (op0); 10352 10353 if (flag_cilkplus && contains_array_notation_expr (op1)) 10354 orig_type1 = type1 = find_correct_array_notation_type (op1); 10355 else 10356 orig_type1 = type1 = TREE_TYPE (op1); 10357 10358 /* The expression codes of the data types of the arguments tell us 10359 whether the arguments are integers, floating, pointers, etc. */ 10360 code0 = TREE_CODE (type0); 10361 code1 = TREE_CODE (type1); 10362 10363 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ 10364 STRIP_TYPE_NOPS (op0); 10365 STRIP_TYPE_NOPS (op1); 10366 10367 /* If an error was already reported for one of the arguments, 10368 avoid reporting another error. */ 10369 10370 if (code0 == ERROR_MARK || code1 == ERROR_MARK) 10371 return error_mark_node; 10372 10373 if ((invalid_op_diag 10374 = targetm.invalid_binary_op (code, type0, type1))) 10375 { 10376 error_at (location, invalid_op_diag); 10377 return error_mark_node; 10378 } 10379 10380 switch (code) 10381 { 10382 case PLUS_EXPR: 10383 case MINUS_EXPR: 10384 case MULT_EXPR: 10385 case TRUNC_DIV_EXPR: 10386 case CEIL_DIV_EXPR: 10387 case FLOOR_DIV_EXPR: 10388 case ROUND_DIV_EXPR: 10389 case EXACT_DIV_EXPR: 10390 may_need_excess_precision = true; 10391 break; 10392 default: 10393 may_need_excess_precision = false; 10394 break; 10395 } 10396 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR) 10397 { 10398 op0 = TREE_OPERAND (op0, 0); 10399 type0 = TREE_TYPE (op0); 10400 } 10401 else if (may_need_excess_precision 10402 && (eptype = excess_precision_type (type0)) != NULL_TREE) 10403 { 10404 type0 = eptype; 10405 op0 = convert (eptype, op0); 10406 } 10407 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) 10408 { 10409 op1 = TREE_OPERAND (op1, 0); 10410 type1 = TREE_TYPE (op1); 10411 } 10412 else if (may_need_excess_precision 10413 && (eptype = excess_precision_type (type1)) != NULL_TREE) 10414 { 10415 type1 = eptype; 10416 op1 = convert (eptype, op1); 10417 } 10418 10419 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE); 10420 10421 /* In case when one of the operands of the binary operation is 10422 a vector and another is a scalar -- convert scalar to vector. */ 10423 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE)) 10424 { 10425 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1, 10426 true); 10427 10428 switch (convert_flag) 10429 { 10430 case stv_error: 10431 return error_mark_node; 10432 case stv_firstarg: 10433 { 10434 bool maybe_const = true; 10435 tree sc; 10436 sc = c_fully_fold (op0, false, &maybe_const); 10437 sc = save_expr (sc); 10438 sc = convert (TREE_TYPE (type1), sc); 10439 op0 = build_vector_from_val (type1, sc); 10440 if (!maybe_const) 10441 op0 = c_wrap_maybe_const (op0, true); 10442 orig_type0 = type0 = TREE_TYPE (op0); 10443 code0 = TREE_CODE (type0); 10444 converted = 1; 10445 break; 10446 } 10447 case stv_secondarg: 10448 { 10449 bool maybe_const = true; 10450 tree sc; 10451 sc = c_fully_fold (op1, false, &maybe_const); 10452 sc = save_expr (sc); 10453 sc = convert (TREE_TYPE (type0), sc); 10454 op1 = build_vector_from_val (type0, sc); 10455 if (!maybe_const) 10456 op1 = c_wrap_maybe_const (op1, true); 10457 orig_type1 = type1 = TREE_TYPE (op1); 10458 code1 = TREE_CODE (type1); 10459 converted = 1; 10460 break; 10461 } 10462 default: 10463 break; 10464 } 10465 } 10466 10467 switch (code) 10468 { 10469 case PLUS_EXPR: 10470 /* Handle the pointer + int case. */ 10471 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 10472 { 10473 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1); 10474 goto return_build_binary_op; 10475 } 10476 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) 10477 { 10478 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0); 10479 goto return_build_binary_op; 10480 } 10481 else 10482 common = 1; 10483 break; 10484 10485 case MINUS_EXPR: 10486 /* Subtraction of two similar pointers. 10487 We must subtract them as integers, then divide by object size. */ 10488 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE 10489 && comp_target_types (location, type0, type1)) 10490 { 10491 ret = pointer_diff (location, op0, op1); 10492 goto return_build_binary_op; 10493 } 10494 /* Handle pointer minus int. Just like pointer plus int. */ 10495 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 10496 { 10497 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1); 10498 goto return_build_binary_op; 10499 } 10500 else 10501 common = 1; 10502 break; 10503 10504 case MULT_EXPR: 10505 common = 1; 10506 break; 10507 10508 case TRUNC_DIV_EXPR: 10509 case CEIL_DIV_EXPR: 10510 case FLOOR_DIV_EXPR: 10511 case ROUND_DIV_EXPR: 10512 case EXACT_DIV_EXPR: 10513 doing_div_or_mod = true; 10514 warn_for_div_by_zero (location, op1); 10515 10516 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE 10517 || code0 == FIXED_POINT_TYPE 10518 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) 10519 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE 10520 || code1 == FIXED_POINT_TYPE 10521 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) 10522 { 10523 enum tree_code tcode0 = code0, tcode1 = code1; 10524 10525 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) 10526 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); 10527 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE) 10528 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); 10529 10530 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE) 10531 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE))) 10532 resultcode = RDIV_EXPR; 10533 else 10534 /* Although it would be tempting to shorten always here, that 10535 loses on some targets, since the modulo instruction is 10536 undefined if the quotient can't be represented in the 10537 computation mode. We shorten only if unsigned or if 10538 dividing by something we know != -1. */ 10539 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) 10540 || (TREE_CODE (op1) == INTEGER_CST 10541 && !integer_all_onesp (op1))); 10542 common = 1; 10543 } 10544 break; 10545 10546 case BIT_AND_EXPR: 10547 case BIT_IOR_EXPR: 10548 case BIT_XOR_EXPR: 10549 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 10550 shorten = -1; 10551 /* Allow vector types which are not floating point types. */ 10552 else if (code0 == VECTOR_TYPE 10553 && code1 == VECTOR_TYPE 10554 && !VECTOR_FLOAT_TYPE_P (type0) 10555 && !VECTOR_FLOAT_TYPE_P (type1)) 10556 common = 1; 10557 break; 10558 10559 case TRUNC_MOD_EXPR: 10560 case FLOOR_MOD_EXPR: 10561 doing_div_or_mod = true; 10562 warn_for_div_by_zero (location, op1); 10563 10564 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE 10565 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE 10566 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) 10567 common = 1; 10568 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 10569 { 10570 /* Although it would be tempting to shorten always here, that loses 10571 on some targets, since the modulo instruction is undefined if the 10572 quotient can't be represented in the computation mode. We shorten 10573 only if unsigned or if dividing by something we know != -1. */ 10574 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) 10575 || (TREE_CODE (op1) == INTEGER_CST 10576 && !integer_all_onesp (op1))); 10577 common = 1; 10578 } 10579 break; 10580 10581 case TRUTH_ANDIF_EXPR: 10582 case TRUTH_ORIF_EXPR: 10583 case TRUTH_AND_EXPR: 10584 case TRUTH_OR_EXPR: 10585 case TRUTH_XOR_EXPR: 10586 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE 10587 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE 10588 || code0 == FIXED_POINT_TYPE) 10589 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE 10590 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE 10591 || code1 == FIXED_POINT_TYPE)) 10592 { 10593 /* Result of these operations is always an int, 10594 but that does not mean the operands should be 10595 converted to ints! */ 10596 result_type = integer_type_node; 10597 if (op0_int_operands) 10598 { 10599 op0 = c_objc_common_truthvalue_conversion (location, orig_op0); 10600 op0 = remove_c_maybe_const_expr (op0); 10601 } 10602 else 10603 op0 = c_objc_common_truthvalue_conversion (location, op0); 10604 if (op1_int_operands) 10605 { 10606 op1 = c_objc_common_truthvalue_conversion (location, orig_op1); 10607 op1 = remove_c_maybe_const_expr (op1); 10608 } 10609 else 10610 op1 = c_objc_common_truthvalue_conversion (location, op1); 10611 converted = 1; 10612 boolean_op = true; 10613 } 10614 if (code == TRUTH_ANDIF_EXPR) 10615 { 10616 int_const_or_overflow = (int_operands 10617 && TREE_CODE (orig_op0) == INTEGER_CST 10618 && (op0 == truthvalue_false_node 10619 || TREE_CODE (orig_op1) == INTEGER_CST)); 10620 int_const = (int_const_or_overflow 10621 && !TREE_OVERFLOW (orig_op0) 10622 && (op0 == truthvalue_false_node 10623 || !TREE_OVERFLOW (orig_op1))); 10624 } 10625 else if (code == TRUTH_ORIF_EXPR) 10626 { 10627 int_const_or_overflow = (int_operands 10628 && TREE_CODE (orig_op0) == INTEGER_CST 10629 && (op0 == truthvalue_true_node 10630 || TREE_CODE (orig_op1) == INTEGER_CST)); 10631 int_const = (int_const_or_overflow 10632 && !TREE_OVERFLOW (orig_op0) 10633 && (op0 == truthvalue_true_node 10634 || !TREE_OVERFLOW (orig_op1))); 10635 } 10636 break; 10637 10638 /* Shift operations: result has same type as first operand; 10639 always convert second operand to int. 10640 Also set SHORT_SHIFT if shifting rightward. */ 10641 10642 case RSHIFT_EXPR: 10643 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE 10644 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) 10645 { 10646 result_type = type0; 10647 converted = 1; 10648 } 10649 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE 10650 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE 10651 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE 10652 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) 10653 { 10654 result_type = type0; 10655 converted = 1; 10656 } 10657 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) 10658 && code1 == INTEGER_TYPE) 10659 { 10660 doing_shift = true; 10661 if (TREE_CODE (op1) == INTEGER_CST) 10662 { 10663 if (tree_int_cst_sgn (op1) < 0) 10664 { 10665 int_const = false; 10666 if (c_inhibit_evaluation_warnings == 0) 10667 warning_at (location, OPT_Wshift_count_negative, 10668 "right shift count is negative"); 10669 } 10670 else 10671 { 10672 if (!integer_zerop (op1)) 10673 short_shift = 1; 10674 10675 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) 10676 { 10677 int_const = false; 10678 if (c_inhibit_evaluation_warnings == 0) 10679 warning_at (location, OPT_Wshift_count_overflow, 10680 "right shift count >= width of type"); 10681 } 10682 } 10683 } 10684 10685 /* Use the type of the value to be shifted. */ 10686 result_type = type0; 10687 /* Avoid converting op1 to result_type later. */ 10688 converted = 1; 10689 } 10690 break; 10691 10692 case LSHIFT_EXPR: 10693 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE 10694 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) 10695 { 10696 result_type = type0; 10697 converted = 1; 10698 } 10699 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE 10700 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE 10701 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE 10702 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) 10703 { 10704 result_type = type0; 10705 converted = 1; 10706 } 10707 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) 10708 && code1 == INTEGER_TYPE) 10709 { 10710 doing_shift = true; 10711 if (TREE_CODE (op1) == INTEGER_CST) 10712 { 10713 if (tree_int_cst_sgn (op1) < 0) 10714 { 10715 int_const = false; 10716 if (c_inhibit_evaluation_warnings == 0) 10717 warning_at (location, OPT_Wshift_count_negative, 10718 "left shift count is negative"); 10719 } 10720 10721 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) 10722 { 10723 int_const = false; 10724 if (c_inhibit_evaluation_warnings == 0) 10725 warning_at (location, OPT_Wshift_count_overflow, 10726 "left shift count >= width of type"); 10727 } 10728 } 10729 10730 /* Use the type of the value to be shifted. */ 10731 result_type = type0; 10732 /* Avoid converting op1 to result_type later. */ 10733 converted = 1; 10734 } 10735 break; 10736 10737 case EQ_EXPR: 10738 case NE_EXPR: 10739 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) 10740 { 10741 tree intt; 10742 if (!vector_types_compatible_elements_p (type0, type1)) 10743 { 10744 error_at (location, "comparing vectors with different " 10745 "element types"); 10746 return error_mark_node; 10747 } 10748 10749 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) 10750 { 10751 error_at (location, "comparing vectors with different " 10752 "number of elements"); 10753 return error_mark_node; 10754 } 10755 10756 /* It's not precisely specified how the usual arithmetic 10757 conversions apply to the vector types. Here, we use 10758 the unsigned type if one of the operands is signed and 10759 the other one is unsigned. */ 10760 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)) 10761 { 10762 if (!TYPE_UNSIGNED (type0)) 10763 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0); 10764 else 10765 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1); 10766 warning_at (location, OPT_Wsign_compare, "comparison between " 10767 "types %qT and %qT", type0, type1); 10768 } 10769 10770 /* Always construct signed integer vector type. */ 10771 intt = c_common_type_for_size (GET_MODE_BITSIZE 10772 (TYPE_MODE (TREE_TYPE (type0))), 0); 10773 result_type = build_opaque_vector_type (intt, 10774 TYPE_VECTOR_SUBPARTS (type0)); 10775 converted = 1; 10776 break; 10777 } 10778 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)) 10779 warning_at (location, 10780 OPT_Wfloat_equal, 10781 "comparing floating point with == or != is unsafe"); 10782 /* Result of comparison is always int, 10783 but don't convert the args to int! */ 10784 build_type = integer_type_node; 10785 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE 10786 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE) 10787 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE 10788 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE)) 10789 short_compare = 1; 10790 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) 10791 { 10792 if (TREE_CODE (op0) == ADDR_EXPR 10793 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))) 10794 { 10795 if (code == EQ_EXPR) 10796 warning_at (location, 10797 OPT_Waddress, 10798 "the comparison will always evaluate as %<false%> " 10799 "for the address of %qD will never be NULL", 10800 TREE_OPERAND (op0, 0)); 10801 else 10802 warning_at (location, 10803 OPT_Waddress, 10804 "the comparison will always evaluate as %<true%> " 10805 "for the address of %qD will never be NULL", 10806 TREE_OPERAND (op0, 0)); 10807 } 10808 result_type = type0; 10809 } 10810 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) 10811 { 10812 if (TREE_CODE (op1) == ADDR_EXPR 10813 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))) 10814 { 10815 if (code == EQ_EXPR) 10816 warning_at (location, 10817 OPT_Waddress, 10818 "the comparison will always evaluate as %<false%> " 10819 "for the address of %qD will never be NULL", 10820 TREE_OPERAND (op1, 0)); 10821 else 10822 warning_at (location, 10823 OPT_Waddress, 10824 "the comparison will always evaluate as %<true%> " 10825 "for the address of %qD will never be NULL", 10826 TREE_OPERAND (op1, 0)); 10827 } 10828 result_type = type1; 10829 } 10830 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) 10831 { 10832 tree tt0 = TREE_TYPE (type0); 10833 tree tt1 = TREE_TYPE (type1); 10834 addr_space_t as0 = TYPE_ADDR_SPACE (tt0); 10835 addr_space_t as1 = TYPE_ADDR_SPACE (tt1); 10836 addr_space_t as_common = ADDR_SPACE_GENERIC; 10837 10838 /* Anything compares with void *. void * compares with anything. 10839 Otherwise, the targets must be compatible 10840 and both must be object or both incomplete. */ 10841 if (comp_target_types (location, type0, type1)) 10842 result_type = common_pointer_type (type0, type1); 10843 else if (!addr_space_superset (as0, as1, &as_common)) 10844 { 10845 error_at (location, "comparison of pointers to " 10846 "disjoint address spaces"); 10847 return error_mark_node; 10848 } 10849 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0)) 10850 { 10851 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE) 10852 pedwarn (location, OPT_Wpedantic, "ISO C forbids " 10853 "comparison of %<void *%> with function pointer"); 10854 } 10855 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1)) 10856 { 10857 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE) 10858 pedwarn (location, OPT_Wpedantic, "ISO C forbids " 10859 "comparison of %<void *%> with function pointer"); 10860 } 10861 else 10862 /* Avoid warning about the volatile ObjC EH puts on decls. */ 10863 if (!objc_ok) 10864 pedwarn (location, 0, 10865 "comparison of distinct pointer types lacks a cast"); 10866 10867 if (result_type == NULL_TREE) 10868 { 10869 int qual = ENCODE_QUAL_ADDR_SPACE (as_common); 10870 result_type = build_pointer_type 10871 (build_qualified_type (void_type_node, qual)); 10872 } 10873 } 10874 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 10875 { 10876 result_type = type0; 10877 pedwarn (location, 0, "comparison between pointer and integer"); 10878 } 10879 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) 10880 { 10881 result_type = type1; 10882 pedwarn (location, 0, "comparison between pointer and integer"); 10883 } 10884 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE 10885 || truth_value_p (TREE_CODE (orig_op0))) 10886 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE 10887 || truth_value_p (TREE_CODE (orig_op1)))) 10888 maybe_warn_bool_compare (location, code, orig_op0, orig_op1); 10889 break; 10890 10891 case LE_EXPR: 10892 case GE_EXPR: 10893 case LT_EXPR: 10894 case GT_EXPR: 10895 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) 10896 { 10897 tree intt; 10898 if (!vector_types_compatible_elements_p (type0, type1)) 10899 { 10900 error_at (location, "comparing vectors with different " 10901 "element types"); 10902 return error_mark_node; 10903 } 10904 10905 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) 10906 { 10907 error_at (location, "comparing vectors with different " 10908 "number of elements"); 10909 return error_mark_node; 10910 } 10911 10912 /* It's not precisely specified how the usual arithmetic 10913 conversions apply to the vector types. Here, we use 10914 the unsigned type if one of the operands is signed and 10915 the other one is unsigned. */ 10916 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)) 10917 { 10918 if (!TYPE_UNSIGNED (type0)) 10919 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0); 10920 else 10921 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1); 10922 warning_at (location, OPT_Wsign_compare, "comparison between " 10923 "types %qT and %qT", type0, type1); 10924 } 10925 10926 /* Always construct signed integer vector type. */ 10927 intt = c_common_type_for_size (GET_MODE_BITSIZE 10928 (TYPE_MODE (TREE_TYPE (type0))), 0); 10929 result_type = build_opaque_vector_type (intt, 10930 TYPE_VECTOR_SUBPARTS (type0)); 10931 converted = 1; 10932 break; 10933 } 10934 build_type = integer_type_node; 10935 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE 10936 || code0 == FIXED_POINT_TYPE) 10937 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE 10938 || code1 == FIXED_POINT_TYPE)) 10939 short_compare = 1; 10940 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) 10941 { 10942 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0)); 10943 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); 10944 addr_space_t as_common; 10945 10946 if (comp_target_types (location, type0, type1)) 10947 { 10948 result_type = common_pointer_type (type0, type1); 10949 if (!COMPLETE_TYPE_P (TREE_TYPE (type0)) 10950 != !COMPLETE_TYPE_P (TREE_TYPE (type1))) 10951 pedwarn (location, 0, 10952 "comparison of complete and incomplete pointers"); 10953 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) 10954 pedwarn (location, OPT_Wpedantic, "ISO C forbids " 10955 "ordered comparisons of pointers to functions"); 10956 else if (null_pointer_constant_p (orig_op0) 10957 || null_pointer_constant_p (orig_op1)) 10958 warning_at (location, OPT_Wextra, 10959 "ordered comparison of pointer with null pointer"); 10960 10961 } 10962 else if (!addr_space_superset (as0, as1, &as_common)) 10963 { 10964 error_at (location, "comparison of pointers to " 10965 "disjoint address spaces"); 10966 return error_mark_node; 10967 } 10968 else 10969 { 10970 int qual = ENCODE_QUAL_ADDR_SPACE (as_common); 10971 result_type = build_pointer_type 10972 (build_qualified_type (void_type_node, qual)); 10973 pedwarn (location, 0, 10974 "comparison of distinct pointer types lacks a cast"); 10975 } 10976 } 10977 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) 10978 { 10979 result_type = type0; 10980 if (pedantic) 10981 pedwarn (location, OPT_Wpedantic, 10982 "ordered comparison of pointer with integer zero"); 10983 else if (extra_warnings) 10984 warning_at (location, OPT_Wextra, 10985 "ordered comparison of pointer with integer zero"); 10986 } 10987 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) 10988 { 10989 result_type = type1; 10990 if (pedantic) 10991 pedwarn (location, OPT_Wpedantic, 10992 "ordered comparison of pointer with integer zero"); 10993 else if (extra_warnings) 10994 warning_at (location, OPT_Wextra, 10995 "ordered comparison of pointer with integer zero"); 10996 } 10997 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 10998 { 10999 result_type = type0; 11000 pedwarn (location, 0, "comparison between pointer and integer"); 11001 } 11002 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) 11003 { 11004 result_type = type1; 11005 pedwarn (location, 0, "comparison between pointer and integer"); 11006 } 11007 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE 11008 || truth_value_p (TREE_CODE (orig_op0))) 11009 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE 11010 || truth_value_p (TREE_CODE (orig_op1)))) 11011 maybe_warn_bool_compare (location, code, orig_op0, orig_op1); 11012 break; 11013 11014 default: 11015 gcc_unreachable (); 11016 } 11017 11018 if (code0 == ERROR_MARK || code1 == ERROR_MARK) 11019 return error_mark_node; 11020 11021 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE 11022 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) 11023 || !vector_types_compatible_elements_p (type0, type1))) 11024 { 11025 binary_op_error (location, code, type0, type1); 11026 return error_mark_node; 11027 } 11028 11029 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE 11030 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE) 11031 && 11032 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE 11033 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE)) 11034 { 11035 bool first_complex = (code0 == COMPLEX_TYPE); 11036 bool second_complex = (code1 == COMPLEX_TYPE); 11037 int none_complex = (!first_complex && !second_complex); 11038 11039 if (shorten || common || short_compare) 11040 { 11041 result_type = c_common_type (type0, type1); 11042 do_warn_double_promotion (result_type, type0, type1, 11043 "implicit conversion from %qT to %qT " 11044 "to match other operand of binary " 11045 "expression", 11046 location); 11047 if (result_type == error_mark_node) 11048 return error_mark_node; 11049 } 11050 11051 if (first_complex != second_complex 11052 && (code == PLUS_EXPR 11053 || code == MINUS_EXPR 11054 || code == MULT_EXPR 11055 || (code == TRUNC_DIV_EXPR && first_complex)) 11056 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE 11057 && flag_signed_zeros) 11058 { 11059 /* An operation on mixed real/complex operands must be 11060 handled specially, but the language-independent code can 11061 more easily optimize the plain complex arithmetic if 11062 -fno-signed-zeros. */ 11063 tree real_type = TREE_TYPE (result_type); 11064 tree real, imag; 11065 if (type0 != orig_type0 || type1 != orig_type1) 11066 { 11067 gcc_assert (may_need_excess_precision && common); 11068 semantic_result_type = c_common_type (orig_type0, orig_type1); 11069 } 11070 if (first_complex) 11071 { 11072 if (TREE_TYPE (op0) != result_type) 11073 op0 = convert_and_check (location, result_type, op0); 11074 if (TREE_TYPE (op1) != real_type) 11075 op1 = convert_and_check (location, real_type, op1); 11076 } 11077 else 11078 { 11079 if (TREE_TYPE (op0) != real_type) 11080 op0 = convert_and_check (location, real_type, op0); 11081 if (TREE_TYPE (op1) != result_type) 11082 op1 = convert_and_check (location, result_type, op1); 11083 } 11084 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) 11085 return error_mark_node; 11086 if (first_complex) 11087 { 11088 op0 = c_save_expr (op0); 11089 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR, 11090 op0, 1); 11091 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR, 11092 op0, 1); 11093 switch (code) 11094 { 11095 case MULT_EXPR: 11096 case TRUNC_DIV_EXPR: 11097 op1 = c_save_expr (op1); 11098 imag = build2 (resultcode, real_type, imag, op1); 11099 /* Fall through. */ 11100 case PLUS_EXPR: 11101 case MINUS_EXPR: 11102 real = build2 (resultcode, real_type, real, op1); 11103 break; 11104 default: 11105 gcc_unreachable(); 11106 } 11107 } 11108 else 11109 { 11110 op1 = c_save_expr (op1); 11111 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR, 11112 op1, 1); 11113 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR, 11114 op1, 1); 11115 switch (code) 11116 { 11117 case MULT_EXPR: 11118 op0 = c_save_expr (op0); 11119 imag = build2 (resultcode, real_type, op0, imag); 11120 /* Fall through. */ 11121 case PLUS_EXPR: 11122 real = build2 (resultcode, real_type, op0, real); 11123 break; 11124 case MINUS_EXPR: 11125 real = build2 (resultcode, real_type, op0, real); 11126 imag = build1 (NEGATE_EXPR, real_type, imag); 11127 break; 11128 default: 11129 gcc_unreachable(); 11130 } 11131 } 11132 ret = build2 (COMPLEX_EXPR, result_type, real, imag); 11133 goto return_build_binary_op; 11134 } 11135 11136 /* For certain operations (which identify themselves by shorten != 0) 11137 if both args were extended from the same smaller type, 11138 do the arithmetic in that type and then extend. 11139 11140 shorten !=0 and !=1 indicates a bitwise operation. 11141 For them, this optimization is safe only if 11142 both args are zero-extended or both are sign-extended. 11143 Otherwise, we might change the result. 11144 Eg, (short)-1 | (unsigned short)-1 is (int)-1 11145 but calculated in (unsigned short) it would be (unsigned short)-1. */ 11146 11147 if (shorten && none_complex) 11148 { 11149 final_type = result_type; 11150 result_type = shorten_binary_op (result_type, op0, op1, 11151 shorten == -1); 11152 } 11153 11154 /* Shifts can be shortened if shifting right. */ 11155 11156 if (short_shift) 11157 { 11158 int unsigned_arg; 11159 tree arg0 = get_narrower (op0, &unsigned_arg); 11160 11161 final_type = result_type; 11162 11163 if (arg0 == op0 && final_type == TREE_TYPE (op0)) 11164 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0)); 11165 11166 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) 11167 && tree_int_cst_sgn (op1) > 0 11168 /* We can shorten only if the shift count is less than the 11169 number of bits in the smaller type size. */ 11170 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0 11171 /* We cannot drop an unsigned shift after sign-extension. */ 11172 && (!TYPE_UNSIGNED (final_type) || unsigned_arg)) 11173 { 11174 /* Do an unsigned shift if the operand was zero-extended. */ 11175 result_type 11176 = c_common_signed_or_unsigned_type (unsigned_arg, 11177 TREE_TYPE (arg0)); 11178 /* Convert value-to-be-shifted to that type. */ 11179 if (TREE_TYPE (op0) != result_type) 11180 op0 = convert (result_type, op0); 11181 converted = 1; 11182 } 11183 } 11184 11185 /* Comparison operations are shortened too but differently. 11186 They identify themselves by setting short_compare = 1. */ 11187 11188 if (short_compare) 11189 { 11190 /* Don't write &op0, etc., because that would prevent op0 11191 from being kept in a register. 11192 Instead, make copies of the our local variables and 11193 pass the copies by reference, then copy them back afterward. */ 11194 tree xop0 = op0, xop1 = op1, xresult_type = result_type; 11195 enum tree_code xresultcode = resultcode; 11196 tree val 11197 = shorten_compare (location, &xop0, &xop1, &xresult_type, 11198 &xresultcode); 11199 11200 if (val != 0) 11201 { 11202 ret = val; 11203 goto return_build_binary_op; 11204 } 11205 11206 op0 = xop0, op1 = xop1; 11207 converted = 1; 11208 resultcode = xresultcode; 11209 11210 if (c_inhibit_evaluation_warnings == 0) 11211 { 11212 bool op0_maybe_const = true; 11213 bool op1_maybe_const = true; 11214 tree orig_op0_folded, orig_op1_folded; 11215 11216 if (in_late_binary_op) 11217 { 11218 orig_op0_folded = orig_op0; 11219 orig_op1_folded = orig_op1; 11220 } 11221 else 11222 { 11223 /* Fold for the sake of possible warnings, as in 11224 build_conditional_expr. This requires the 11225 "original" values to be folded, not just op0 and 11226 op1. */ 11227 c_inhibit_evaluation_warnings++; 11228 op0 = c_fully_fold (op0, require_constant_value, 11229 &op0_maybe_const); 11230 op1 = c_fully_fold (op1, require_constant_value, 11231 &op1_maybe_const); 11232 c_inhibit_evaluation_warnings--; 11233 orig_op0_folded = c_fully_fold (orig_op0, 11234 require_constant_value, 11235 NULL); 11236 orig_op1_folded = c_fully_fold (orig_op1, 11237 require_constant_value, 11238 NULL); 11239 } 11240 11241 if (warn_sign_compare) 11242 warn_for_sign_compare (location, orig_op0_folded, 11243 orig_op1_folded, op0, op1, 11244 result_type, resultcode); 11245 if (!in_late_binary_op && !int_operands) 11246 { 11247 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST) 11248 op0 = c_wrap_maybe_const (op0, !op0_maybe_const); 11249 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) 11250 op1 = c_wrap_maybe_const (op1, !op1_maybe_const); 11251 } 11252 } 11253 } 11254 } 11255 11256 /* At this point, RESULT_TYPE must be nonzero to avoid an error message. 11257 If CONVERTED is zero, both args will be converted to type RESULT_TYPE. 11258 Then the expression will be built. 11259 It will be given type FINAL_TYPE if that is nonzero; 11260 otherwise, it will be given type RESULT_TYPE. */ 11261 11262 if (!result_type) 11263 { 11264 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1)); 11265 return error_mark_node; 11266 } 11267 11268 if (build_type == NULL_TREE) 11269 { 11270 build_type = result_type; 11271 if ((type0 != orig_type0 || type1 != orig_type1) 11272 && !boolean_op) 11273 { 11274 gcc_assert (may_need_excess_precision && common); 11275 semantic_result_type = c_common_type (orig_type0, orig_type1); 11276 } 11277 } 11278 11279 if (!converted) 11280 { 11281 op0 = ep_convert_and_check (location, result_type, op0, 11282 semantic_result_type); 11283 op1 = ep_convert_and_check (location, result_type, op1, 11284 semantic_result_type); 11285 11286 /* This can happen if one operand has a vector type, and the other 11287 has a different type. */ 11288 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) 11289 return error_mark_node; 11290 } 11291 11292 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE 11293 | SANITIZE_FLOAT_DIVIDE)) 11294 && do_ubsan_in_current_function () 11295 && (doing_div_or_mod || doing_shift)) 11296 { 11297 /* OP0 and/or OP1 might have side-effects. */ 11298 op0 = c_save_expr (op0); 11299 op1 = c_save_expr (op1); 11300 op0 = c_fully_fold (op0, false, NULL); 11301 op1 = c_fully_fold (op1, false, NULL); 11302 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE 11303 | SANITIZE_FLOAT_DIVIDE))) 11304 instrument_expr = ubsan_instrument_division (location, op0, op1); 11305 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT)) 11306 instrument_expr = ubsan_instrument_shift (location, code, op0, op1); 11307 } 11308 11309 /* Treat expressions in initializers specially as they can't trap. */ 11310 if (int_const_or_overflow) 11311 ret = (require_constant_value 11312 ? fold_build2_initializer_loc (location, resultcode, build_type, 11313 op0, op1) 11314 : fold_build2_loc (location, resultcode, build_type, op0, op1)); 11315 else 11316 ret = build2 (resultcode, build_type, op0, op1); 11317 if (final_type != 0) 11318 ret = convert (final_type, ret); 11319 11320 return_build_binary_op: 11321 gcc_assert (ret != error_mark_node); 11322 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const) 11323 ret = (int_operands 11324 ? note_integer_operands (ret) 11325 : build1 (NOP_EXPR, TREE_TYPE (ret), ret)); 11326 else if (TREE_CODE (ret) != INTEGER_CST && int_operands 11327 && !in_late_binary_op) 11328 ret = note_integer_operands (ret); 11329 if (semantic_result_type) 11330 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); 11331 protected_set_expr_location (ret, location); 11332 11333 if (instrument_expr != NULL) 11334 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret), 11335 instrument_expr, ret); 11336 11337 return ret; 11338} 11339 11340 11341/* Convert EXPR to be a truth-value, validating its type for this 11342 purpose. LOCATION is the source location for the expression. */ 11343 11344tree 11345c_objc_common_truthvalue_conversion (location_t location, tree expr) 11346{ 11347 bool int_const, int_operands; 11348 11349 switch (TREE_CODE (TREE_TYPE (expr))) 11350 { 11351 case ARRAY_TYPE: 11352 error_at (location, "used array that cannot be converted to pointer where scalar is required"); 11353 return error_mark_node; 11354 11355 case RECORD_TYPE: 11356 error_at (location, "used struct type value where scalar is required"); 11357 return error_mark_node; 11358 11359 case UNION_TYPE: 11360 error_at (location, "used union type value where scalar is required"); 11361 return error_mark_node; 11362 11363 case VOID_TYPE: 11364 error_at (location, "void value not ignored as it ought to be"); 11365 return error_mark_node; 11366 11367 case FUNCTION_TYPE: 11368 gcc_unreachable (); 11369 11370 case VECTOR_TYPE: 11371 error_at (location, "used vector type where scalar is required"); 11372 return error_mark_node; 11373 11374 default: 11375 break; 11376 } 11377 11378 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr)); 11379 int_operands = EXPR_INT_CONST_OPERANDS (expr); 11380 if (int_operands && TREE_CODE (expr) != INTEGER_CST) 11381 { 11382 expr = remove_c_maybe_const_expr (expr); 11383 expr = build2 (NE_EXPR, integer_type_node, expr, 11384 convert (TREE_TYPE (expr), integer_zero_node)); 11385 expr = note_integer_operands (expr); 11386 } 11387 else 11388 /* ??? Should we also give an error for vectors rather than leaving 11389 those to give errors later? */ 11390 expr = c_common_truthvalue_conversion (location, expr); 11391 11392 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const) 11393 { 11394 if (TREE_OVERFLOW (expr)) 11395 return expr; 11396 else 11397 return note_integer_operands (expr); 11398 } 11399 if (TREE_CODE (expr) == INTEGER_CST && !int_const) 11400 return build1 (NOP_EXPR, TREE_TYPE (expr), expr); 11401 return expr; 11402} 11403 11404 11405/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as 11406 required. */ 11407 11408tree 11409c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se) 11410{ 11411 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR) 11412 { 11413 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr); 11414 /* Executing a compound literal inside a function reinitializes 11415 it. */ 11416 if (!TREE_STATIC (decl)) 11417 *se = true; 11418 return decl; 11419 } 11420 else 11421 return expr; 11422} 11423 11424/* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound 11425 statement. LOC is the location of the OACC_PARALLEL. */ 11426 11427tree 11428c_finish_oacc_parallel (location_t loc, tree clauses, tree block) 11429{ 11430 tree stmt; 11431 11432 block = c_end_compound_stmt (loc, block, true); 11433 11434 stmt = make_node (OACC_PARALLEL); 11435 TREE_TYPE (stmt) = void_type_node; 11436 OACC_PARALLEL_CLAUSES (stmt) = clauses; 11437 OACC_PARALLEL_BODY (stmt) = block; 11438 SET_EXPR_LOCATION (stmt, loc); 11439 11440 return add_stmt (stmt); 11441} 11442 11443/* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound 11444 statement. LOC is the location of the OACC_KERNELS. */ 11445 11446tree 11447c_finish_oacc_kernels (location_t loc, tree clauses, tree block) 11448{ 11449 tree stmt; 11450 11451 block = c_end_compound_stmt (loc, block, true); 11452 11453 stmt = make_node (OACC_KERNELS); 11454 TREE_TYPE (stmt) = void_type_node; 11455 OACC_KERNELS_CLAUSES (stmt) = clauses; 11456 OACC_KERNELS_BODY (stmt) = block; 11457 SET_EXPR_LOCATION (stmt, loc); 11458 11459 return add_stmt (stmt); 11460} 11461 11462/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound 11463 statement. LOC is the location of the OACC_DATA. */ 11464 11465tree 11466c_finish_oacc_data (location_t loc, tree clauses, tree block) 11467{ 11468 tree stmt; 11469 11470 block = c_end_compound_stmt (loc, block, true); 11471 11472 stmt = make_node (OACC_DATA); 11473 TREE_TYPE (stmt) = void_type_node; 11474 OACC_DATA_CLAUSES (stmt) = clauses; 11475 OACC_DATA_BODY (stmt) = block; 11476 SET_EXPR_LOCATION (stmt, loc); 11477 11478 return add_stmt (stmt); 11479} 11480 11481/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ 11482 11483tree 11484c_begin_omp_parallel (void) 11485{ 11486 tree block; 11487 11488 keep_next_level (); 11489 block = c_begin_compound_stmt (true); 11490 11491 return block; 11492} 11493 11494/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound 11495 statement. LOC is the location of the OMP_PARALLEL. */ 11496 11497tree 11498c_finish_omp_parallel (location_t loc, tree clauses, tree block) 11499{ 11500 tree stmt; 11501 11502 block = c_end_compound_stmt (loc, block, true); 11503 11504 stmt = make_node (OMP_PARALLEL); 11505 TREE_TYPE (stmt) = void_type_node; 11506 OMP_PARALLEL_CLAUSES (stmt) = clauses; 11507 OMP_PARALLEL_BODY (stmt) = block; 11508 SET_EXPR_LOCATION (stmt, loc); 11509 11510 return add_stmt (stmt); 11511} 11512 11513/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ 11514 11515tree 11516c_begin_omp_task (void) 11517{ 11518 tree block; 11519 11520 keep_next_level (); 11521 block = c_begin_compound_stmt (true); 11522 11523 return block; 11524} 11525 11526/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound 11527 statement. LOC is the location of the #pragma. */ 11528 11529tree 11530c_finish_omp_task (location_t loc, tree clauses, tree block) 11531{ 11532 tree stmt; 11533 11534 block = c_end_compound_stmt (loc, block, true); 11535 11536 stmt = make_node (OMP_TASK); 11537 TREE_TYPE (stmt) = void_type_node; 11538 OMP_TASK_CLAUSES (stmt) = clauses; 11539 OMP_TASK_BODY (stmt) = block; 11540 SET_EXPR_LOCATION (stmt, loc); 11541 11542 return add_stmt (stmt); 11543} 11544 11545/* Generate GOMP_cancel call for #pragma omp cancel. */ 11546 11547void 11548c_finish_omp_cancel (location_t loc, tree clauses) 11549{ 11550 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL); 11551 int mask = 0; 11552 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL)) 11553 mask = 1; 11554 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR)) 11555 mask = 2; 11556 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS)) 11557 mask = 4; 11558 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP)) 11559 mask = 8; 11560 else 11561 { 11562 error_at (loc, "%<#pragma omp cancel must specify one of " 11563 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> " 11564 "clauses"); 11565 return; 11566 } 11567 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF); 11568 if (ifc != NULL_TREE) 11569 { 11570 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc)); 11571 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR, 11572 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc), 11573 build_zero_cst (type)); 11574 } 11575 else 11576 ifc = boolean_true_node; 11577 tree stmt = build_call_expr_loc (loc, fn, 2, 11578 build_int_cst (integer_type_node, mask), 11579 ifc); 11580 add_stmt (stmt); 11581} 11582 11583/* Generate GOMP_cancellation_point call for 11584 #pragma omp cancellation point. */ 11585 11586void 11587c_finish_omp_cancellation_point (location_t loc, tree clauses) 11588{ 11589 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT); 11590 int mask = 0; 11591 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL)) 11592 mask = 1; 11593 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR)) 11594 mask = 2; 11595 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS)) 11596 mask = 4; 11597 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP)) 11598 mask = 8; 11599 else 11600 { 11601 error_at (loc, "%<#pragma omp cancellation point must specify one of " 11602 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> " 11603 "clauses"); 11604 return; 11605 } 11606 tree stmt = build_call_expr_loc (loc, fn, 1, 11607 build_int_cst (integer_type_node, mask)); 11608 add_stmt (stmt); 11609} 11610 11611/* Helper function for handle_omp_array_sections. Called recursively 11612 to handle multiple array-section-subscripts. C is the clause, 11613 T current expression (initially OMP_CLAUSE_DECL), which is either 11614 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound 11615 expression if specified, TREE_VALUE length expression if specified, 11616 TREE_CHAIN is what it has been specified after, or some decl. 11617 TYPES vector is populated with array section types, MAYBE_ZERO_LEN 11618 set to true if any of the array-section-subscript could have length 11619 of zero (explicit or implicit), FIRST_NON_ONE is the index of the 11620 first array-section-subscript which is known not to have length 11621 of one. Given say: 11622 map(a[:b][2:1][:c][:2][:d][e:f][2:5]) 11623 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c] 11624 all are or may have length of 1, array-section-subscript [:2] is the 11625 first one knonwn not to have length 1. For array-section-subscript 11626 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't 11627 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we 11628 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above 11629 case though, as some lengths could be zero. */ 11630 11631static tree 11632handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types, 11633 bool &maybe_zero_len, unsigned int &first_non_one) 11634{ 11635 tree ret, low_bound, length, type; 11636 if (TREE_CODE (t) != TREE_LIST) 11637 { 11638 if (error_operand_p (t)) 11639 return error_mark_node; 11640 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 11641 { 11642 if (DECL_P (t)) 11643 error_at (OMP_CLAUSE_LOCATION (c), 11644 "%qD is not a variable in %qs clause", t, 11645 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 11646 else 11647 error_at (OMP_CLAUSE_LOCATION (c), 11648 "%qE is not a variable in %qs clause", t, 11649 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 11650 return error_mark_node; 11651 } 11652 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND 11653 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) 11654 { 11655 error_at (OMP_CLAUSE_LOCATION (c), 11656 "%qD is threadprivate variable in %qs clause", t, 11657 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 11658 return error_mark_node; 11659 } 11660 return t; 11661 } 11662 11663 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types, 11664 maybe_zero_len, first_non_one); 11665 if (ret == error_mark_node || ret == NULL_TREE) 11666 return ret; 11667 11668 type = TREE_TYPE (ret); 11669 low_bound = TREE_PURPOSE (t); 11670 length = TREE_VALUE (t); 11671 11672 if (low_bound == error_mark_node || length == error_mark_node) 11673 return error_mark_node; 11674 11675 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound))) 11676 { 11677 error_at (OMP_CLAUSE_LOCATION (c), 11678 "low bound %qE of array section does not have integral type", 11679 low_bound); 11680 return error_mark_node; 11681 } 11682 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length))) 11683 { 11684 error_at (OMP_CLAUSE_LOCATION (c), 11685 "length %qE of array section does not have integral type", 11686 length); 11687 return error_mark_node; 11688 } 11689 if (low_bound 11690 && TREE_CODE (low_bound) == INTEGER_CST 11691 && TYPE_PRECISION (TREE_TYPE (low_bound)) 11692 > TYPE_PRECISION (sizetype)) 11693 low_bound = fold_convert (sizetype, low_bound); 11694 if (length 11695 && TREE_CODE (length) == INTEGER_CST 11696 && TYPE_PRECISION (TREE_TYPE (length)) 11697 > TYPE_PRECISION (sizetype)) 11698 length = fold_convert (sizetype, length); 11699 if (low_bound == NULL_TREE) 11700 low_bound = integer_zero_node; 11701 11702 if (length != NULL_TREE) 11703 { 11704 if (!integer_nonzerop (length)) 11705 maybe_zero_len = true; 11706 if (first_non_one == types.length () 11707 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length))) 11708 first_non_one++; 11709 } 11710 if (TREE_CODE (type) == ARRAY_TYPE) 11711 { 11712 if (length == NULL_TREE 11713 && (TYPE_DOMAIN (type) == NULL_TREE 11714 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)) 11715 { 11716 error_at (OMP_CLAUSE_LOCATION (c), 11717 "for unknown bound array type length expression must " 11718 "be specified"); 11719 return error_mark_node; 11720 } 11721 if (TREE_CODE (low_bound) == INTEGER_CST 11722 && tree_int_cst_sgn (low_bound) == -1) 11723 { 11724 error_at (OMP_CLAUSE_LOCATION (c), 11725 "negative low bound in array section in %qs clause", 11726 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 11727 return error_mark_node; 11728 } 11729 if (length != NULL_TREE 11730 && TREE_CODE (length) == INTEGER_CST 11731 && tree_int_cst_sgn (length) == -1) 11732 { 11733 error_at (OMP_CLAUSE_LOCATION (c), 11734 "negative length in array section in %qs clause", 11735 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 11736 return error_mark_node; 11737 } 11738 if (TYPE_DOMAIN (type) 11739 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) 11740 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) 11741 == INTEGER_CST) 11742 { 11743 tree size = size_binop (PLUS_EXPR, 11744 TYPE_MAX_VALUE (TYPE_DOMAIN (type)), 11745 size_one_node); 11746 if (TREE_CODE (low_bound) == INTEGER_CST) 11747 { 11748 if (tree_int_cst_lt (size, low_bound)) 11749 { 11750 error_at (OMP_CLAUSE_LOCATION (c), 11751 "low bound %qE above array section size " 11752 "in %qs clause", low_bound, 11753 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 11754 return error_mark_node; 11755 } 11756 if (tree_int_cst_equal (size, low_bound)) 11757 maybe_zero_len = true; 11758 else if (length == NULL_TREE 11759 && first_non_one == types.length () 11760 && tree_int_cst_equal 11761 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), 11762 low_bound)) 11763 first_non_one++; 11764 } 11765 else if (length == NULL_TREE) 11766 { 11767 maybe_zero_len = true; 11768 if (first_non_one == types.length ()) 11769 first_non_one++; 11770 } 11771 if (length && TREE_CODE (length) == INTEGER_CST) 11772 { 11773 if (tree_int_cst_lt (size, length)) 11774 { 11775 error_at (OMP_CLAUSE_LOCATION (c), 11776 "length %qE above array section size " 11777 "in %qs clause", length, 11778 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 11779 return error_mark_node; 11780 } 11781 if (TREE_CODE (low_bound) == INTEGER_CST) 11782 { 11783 tree lbpluslen 11784 = size_binop (PLUS_EXPR, 11785 fold_convert (sizetype, low_bound), 11786 fold_convert (sizetype, length)); 11787 if (TREE_CODE (lbpluslen) == INTEGER_CST 11788 && tree_int_cst_lt (size, lbpluslen)) 11789 { 11790 error_at (OMP_CLAUSE_LOCATION (c), 11791 "high bound %qE above array section size " 11792 "in %qs clause", lbpluslen, 11793 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 11794 return error_mark_node; 11795 } 11796 } 11797 } 11798 } 11799 else if (length == NULL_TREE) 11800 { 11801 maybe_zero_len = true; 11802 if (first_non_one == types.length ()) 11803 first_non_one++; 11804 } 11805 11806 /* For [lb:] we will need to evaluate lb more than once. */ 11807 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND) 11808 { 11809 tree lb = c_save_expr (low_bound); 11810 if (lb != low_bound) 11811 { 11812 TREE_PURPOSE (t) = lb; 11813 low_bound = lb; 11814 } 11815 } 11816 } 11817 else if (TREE_CODE (type) == POINTER_TYPE) 11818 { 11819 if (length == NULL_TREE) 11820 { 11821 error_at (OMP_CLAUSE_LOCATION (c), 11822 "for pointer type length expression must be specified"); 11823 return error_mark_node; 11824 } 11825 /* If there is a pointer type anywhere but in the very first 11826 array-section-subscript, the array section can't be contiguous. */ 11827 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND 11828 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST) 11829 { 11830 error_at (OMP_CLAUSE_LOCATION (c), 11831 "array section is not contiguous in %qs clause", 11832 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 11833 return error_mark_node; 11834 } 11835 } 11836 else 11837 { 11838 error_at (OMP_CLAUSE_LOCATION (c), 11839 "%qE does not have pointer or array type", ret); 11840 return error_mark_node; 11841 } 11842 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND) 11843 types.safe_push (TREE_TYPE (ret)); 11844 /* We will need to evaluate lb more than once. */ 11845 tree lb = c_save_expr (low_bound); 11846 if (lb != low_bound) 11847 { 11848 TREE_PURPOSE (t) = lb; 11849 low_bound = lb; 11850 } 11851 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound); 11852 return ret; 11853} 11854 11855/* Handle array sections for clause C. */ 11856 11857static bool 11858handle_omp_array_sections (tree c) 11859{ 11860 bool maybe_zero_len = false; 11861 unsigned int first_non_one = 0; 11862 vec<tree> types = vNULL; 11863 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types, 11864 maybe_zero_len, first_non_one); 11865 if (first == error_mark_node) 11866 { 11867 types.release (); 11868 return true; 11869 } 11870 if (first == NULL_TREE) 11871 { 11872 types.release (); 11873 return false; 11874 } 11875 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND) 11876 { 11877 tree t = OMP_CLAUSE_DECL (c); 11878 tree tem = NULL_TREE; 11879 types.release (); 11880 /* Need to evaluate side effects in the length expressions 11881 if any. */ 11882 while (TREE_CODE (t) == TREE_LIST) 11883 { 11884 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t))) 11885 { 11886 if (tem == NULL_TREE) 11887 tem = TREE_VALUE (t); 11888 else 11889 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), 11890 TREE_VALUE (t), tem); 11891 } 11892 t = TREE_CHAIN (t); 11893 } 11894 if (tem) 11895 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first); 11896 first = c_fully_fold (first, false, NULL); 11897 OMP_CLAUSE_DECL (c) = first; 11898 } 11899 else 11900 { 11901 unsigned int num = types.length (), i; 11902 tree t, side_effects = NULL_TREE, size = NULL_TREE; 11903 tree condition = NULL_TREE; 11904 11905 if (int_size_in_bytes (TREE_TYPE (first)) <= 0) 11906 maybe_zero_len = true; 11907 11908 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0; 11909 t = TREE_CHAIN (t)) 11910 { 11911 tree low_bound = TREE_PURPOSE (t); 11912 tree length = TREE_VALUE (t); 11913 11914 i--; 11915 if (low_bound 11916 && TREE_CODE (low_bound) == INTEGER_CST 11917 && TYPE_PRECISION (TREE_TYPE (low_bound)) 11918 > TYPE_PRECISION (sizetype)) 11919 low_bound = fold_convert (sizetype, low_bound); 11920 if (length 11921 && TREE_CODE (length) == INTEGER_CST 11922 && TYPE_PRECISION (TREE_TYPE (length)) 11923 > TYPE_PRECISION (sizetype)) 11924 length = fold_convert (sizetype, length); 11925 if (low_bound == NULL_TREE) 11926 low_bound = integer_zero_node; 11927 if (!maybe_zero_len && i > first_non_one) 11928 { 11929 if (integer_nonzerop (low_bound)) 11930 goto do_warn_noncontiguous; 11931 if (length != NULL_TREE 11932 && TREE_CODE (length) == INTEGER_CST 11933 && TYPE_DOMAIN (types[i]) 11934 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])) 11935 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))) 11936 == INTEGER_CST) 11937 { 11938 tree size; 11939 size = size_binop (PLUS_EXPR, 11940 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])), 11941 size_one_node); 11942 if (!tree_int_cst_equal (length, size)) 11943 { 11944 do_warn_noncontiguous: 11945 error_at (OMP_CLAUSE_LOCATION (c), 11946 "array section is not contiguous in %qs " 11947 "clause", 11948 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 11949 types.release (); 11950 return true; 11951 } 11952 } 11953 if (length != NULL_TREE 11954 && TREE_SIDE_EFFECTS (length)) 11955 { 11956 if (side_effects == NULL_TREE) 11957 side_effects = length; 11958 else 11959 side_effects = build2 (COMPOUND_EXPR, 11960 TREE_TYPE (side_effects), 11961 length, side_effects); 11962 } 11963 } 11964 else 11965 { 11966 tree l; 11967 11968 if (i > first_non_one && length && integer_nonzerop (length)) 11969 continue; 11970 if (length) 11971 l = fold_convert (sizetype, length); 11972 else 11973 { 11974 l = size_binop (PLUS_EXPR, 11975 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])), 11976 size_one_node); 11977 l = size_binop (MINUS_EXPR, l, 11978 fold_convert (sizetype, low_bound)); 11979 } 11980 if (i > first_non_one) 11981 { 11982 l = fold_build2 (NE_EXPR, boolean_type_node, l, 11983 size_zero_node); 11984 if (condition == NULL_TREE) 11985 condition = l; 11986 else 11987 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node, 11988 l, condition); 11989 } 11990 else if (size == NULL_TREE) 11991 { 11992 size = size_in_bytes (TREE_TYPE (types[i])); 11993 size = size_binop (MULT_EXPR, size, l); 11994 if (condition) 11995 size = fold_build3 (COND_EXPR, sizetype, condition, 11996 size, size_zero_node); 11997 } 11998 else 11999 size = size_binop (MULT_EXPR, size, l); 12000 } 12001 } 12002 types.release (); 12003 if (side_effects) 12004 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size); 12005 first = c_fully_fold (first, false, NULL); 12006 OMP_CLAUSE_DECL (c) = first; 12007 if (size) 12008 size = c_fully_fold (size, false, NULL); 12009 OMP_CLAUSE_SIZE (c) = size; 12010 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) 12011 return false; 12012 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR); 12013 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP); 12014 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER); 12015 if (!c_mark_addressable (t)) 12016 return false; 12017 OMP_CLAUSE_DECL (c2) = t; 12018 t = build_fold_addr_expr (first); 12019 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t); 12020 tree ptr = OMP_CLAUSE_DECL (c2); 12021 if (!POINTER_TYPE_P (TREE_TYPE (ptr))) 12022 ptr = build_fold_addr_expr (ptr); 12023 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR, 12024 ptrdiff_type_node, t, 12025 fold_convert_loc (OMP_CLAUSE_LOCATION (c), 12026 ptrdiff_type_node, ptr)); 12027 t = c_fully_fold (t, false, NULL); 12028 OMP_CLAUSE_SIZE (c2) = t; 12029 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c); 12030 OMP_CLAUSE_CHAIN (c) = c2; 12031 } 12032 return false; 12033} 12034 12035/* Helper function of finish_omp_clauses. Clone STMT as if we were making 12036 an inline call. But, remap 12037 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER 12038 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */ 12039 12040static tree 12041c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2, 12042 tree decl, tree placeholder) 12043{ 12044 copy_body_data id; 12045 hash_map<tree, tree> decl_map; 12046 12047 decl_map.put (omp_decl1, placeholder); 12048 decl_map.put (omp_decl2, decl); 12049 memset (&id, 0, sizeof (id)); 12050 id.src_fn = DECL_CONTEXT (omp_decl1); 12051 id.dst_fn = current_function_decl; 12052 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn); 12053 id.decl_map = &decl_map; 12054 12055 id.copy_decl = copy_decl_no_change; 12056 id.transform_call_graph_edges = CB_CGE_DUPLICATE; 12057 id.transform_new_cfg = true; 12058 id.transform_return_to_modify = false; 12059 id.transform_lang_insert_block = NULL; 12060 id.eh_lp_nr = 0; 12061 walk_tree (&stmt, copy_tree_body_r, &id, NULL); 12062 return stmt; 12063} 12064 12065/* Helper function of c_finish_omp_clauses, called via walk_tree. 12066 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */ 12067 12068static tree 12069c_find_omp_placeholder_r (tree *tp, int *, void *data) 12070{ 12071 if (*tp == (tree) data) 12072 return *tp; 12073 return NULL_TREE; 12074} 12075 12076/* For all elements of CLAUSES, validate them against their constraints. 12077 Remove any elements from the list that are invalid. */ 12078 12079tree 12080c_finish_omp_clauses (tree clauses) 12081{ 12082 bitmap_head generic_head, firstprivate_head, lastprivate_head; 12083 bitmap_head aligned_head; 12084 tree c, t, *pc; 12085 bool branch_seen = false; 12086 bool copyprivate_seen = false; 12087 tree *nowait_clause = NULL; 12088 12089 bitmap_obstack_initialize (NULL); 12090 bitmap_initialize (&generic_head, &bitmap_default_obstack); 12091 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack); 12092 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack); 12093 bitmap_initialize (&aligned_head, &bitmap_default_obstack); 12094 12095 for (pc = &clauses, c = clauses; c ; c = *pc) 12096 { 12097 bool remove = false; 12098 bool need_complete = false; 12099 bool need_implicitly_determined = false; 12100 12101 switch (OMP_CLAUSE_CODE (c)) 12102 { 12103 case OMP_CLAUSE_SHARED: 12104 need_implicitly_determined = true; 12105 goto check_dup_generic; 12106 12107 case OMP_CLAUSE_PRIVATE: 12108 need_complete = true; 12109 need_implicitly_determined = true; 12110 goto check_dup_generic; 12111 12112 case OMP_CLAUSE_REDUCTION: 12113 need_implicitly_determined = true; 12114 t = OMP_CLAUSE_DECL (c); 12115 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE 12116 && (FLOAT_TYPE_P (TREE_TYPE (t)) 12117 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)) 12118 { 12119 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c); 12120 const char *r_name = NULL; 12121 12122 switch (r_code) 12123 { 12124 case PLUS_EXPR: 12125 case MULT_EXPR: 12126 case MINUS_EXPR: 12127 break; 12128 case MIN_EXPR: 12129 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE) 12130 r_name = "min"; 12131 break; 12132 case MAX_EXPR: 12133 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE) 12134 r_name = "max"; 12135 break; 12136 case BIT_AND_EXPR: 12137 r_name = "&"; 12138 break; 12139 case BIT_XOR_EXPR: 12140 r_name = "^"; 12141 break; 12142 case BIT_IOR_EXPR: 12143 r_name = "|"; 12144 break; 12145 case TRUTH_ANDIF_EXPR: 12146 if (FLOAT_TYPE_P (TREE_TYPE (t))) 12147 r_name = "&&"; 12148 break; 12149 case TRUTH_ORIF_EXPR: 12150 if (FLOAT_TYPE_P (TREE_TYPE (t))) 12151 r_name = "||"; 12152 break; 12153 default: 12154 gcc_unreachable (); 12155 } 12156 if (r_name) 12157 { 12158 error_at (OMP_CLAUSE_LOCATION (c), 12159 "%qE has invalid type for %<reduction(%s)%>", 12160 t, r_name); 12161 remove = true; 12162 break; 12163 } 12164 } 12165 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node) 12166 { 12167 error_at (OMP_CLAUSE_LOCATION (c), 12168 "user defined reduction not found for %qD", t); 12169 remove = true; 12170 break; 12171 } 12172 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)) 12173 { 12174 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c); 12175 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t)); 12176 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c), 12177 VAR_DECL, NULL_TREE, type); 12178 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder; 12179 DECL_ARTIFICIAL (placeholder) = 1; 12180 DECL_IGNORED_P (placeholder) = 1; 12181 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0))) 12182 c_mark_addressable (placeholder); 12183 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1))) 12184 c_mark_addressable (OMP_CLAUSE_DECL (c)); 12185 OMP_CLAUSE_REDUCTION_MERGE (c) 12186 = c_clone_omp_udr (TREE_VEC_ELT (list, 2), 12187 TREE_VEC_ELT (list, 0), 12188 TREE_VEC_ELT (list, 1), 12189 OMP_CLAUSE_DECL (c), placeholder); 12190 OMP_CLAUSE_REDUCTION_MERGE (c) 12191 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR, 12192 void_type_node, NULL_TREE, 12193 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE); 12194 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1; 12195 if (TREE_VEC_LENGTH (list) == 6) 12196 { 12197 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3))) 12198 c_mark_addressable (OMP_CLAUSE_DECL (c)); 12199 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4))) 12200 c_mark_addressable (placeholder); 12201 tree init = TREE_VEC_ELT (list, 5); 12202 if (init == error_mark_node) 12203 init = DECL_INITIAL (TREE_VEC_ELT (list, 3)); 12204 OMP_CLAUSE_REDUCTION_INIT (c) 12205 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4), 12206 TREE_VEC_ELT (list, 3), 12207 OMP_CLAUSE_DECL (c), placeholder); 12208 if (TREE_VEC_ELT (list, 5) == error_mark_node) 12209 OMP_CLAUSE_REDUCTION_INIT (c) 12210 = build2 (INIT_EXPR, TREE_TYPE (t), t, 12211 OMP_CLAUSE_REDUCTION_INIT (c)); 12212 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c), 12213 c_find_omp_placeholder_r, 12214 placeholder, NULL)) 12215 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1; 12216 } 12217 else 12218 { 12219 tree init; 12220 if (AGGREGATE_TYPE_P (TREE_TYPE (t))) 12221 init = build_constructor (TREE_TYPE (t), NULL); 12222 else 12223 init = fold_convert (TREE_TYPE (t), integer_zero_node); 12224 OMP_CLAUSE_REDUCTION_INIT (c) 12225 = build2 (INIT_EXPR, TREE_TYPE (t), t, init); 12226 } 12227 OMP_CLAUSE_REDUCTION_INIT (c) 12228 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR, 12229 void_type_node, NULL_TREE, 12230 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE); 12231 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1; 12232 } 12233 goto check_dup_generic; 12234 12235 case OMP_CLAUSE_COPYPRIVATE: 12236 copyprivate_seen = true; 12237 if (nowait_clause) 12238 { 12239 error_at (OMP_CLAUSE_LOCATION (*nowait_clause), 12240 "%<nowait%> clause must not be used together " 12241 "with %<copyprivate%>"); 12242 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause); 12243 nowait_clause = NULL; 12244 } 12245 goto check_dup_generic; 12246 12247 case OMP_CLAUSE_COPYIN: 12248 t = OMP_CLAUSE_DECL (c); 12249 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t)) 12250 { 12251 error_at (OMP_CLAUSE_LOCATION (c), 12252 "%qE must be %<threadprivate%> for %<copyin%>", t); 12253 remove = true; 12254 break; 12255 } 12256 goto check_dup_generic; 12257 12258 case OMP_CLAUSE_LINEAR: 12259 t = OMP_CLAUSE_DECL (c); 12260 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)) 12261 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE) 12262 { 12263 error_at (OMP_CLAUSE_LOCATION (c), 12264 "linear clause applied to non-integral non-pointer " 12265 "variable with type %qT", TREE_TYPE (t)); 12266 remove = true; 12267 break; 12268 } 12269 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE) 12270 { 12271 tree s = OMP_CLAUSE_LINEAR_STEP (c); 12272 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR, 12273 OMP_CLAUSE_DECL (c), s); 12274 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR, 12275 sizetype, s, OMP_CLAUSE_DECL (c)); 12276 if (s == error_mark_node) 12277 s = size_one_node; 12278 OMP_CLAUSE_LINEAR_STEP (c) = s; 12279 } 12280 else 12281 OMP_CLAUSE_LINEAR_STEP (c) 12282 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c)); 12283 goto check_dup_generic; 12284 12285 check_dup_generic: 12286 t = OMP_CLAUSE_DECL (c); 12287 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 12288 { 12289 error_at (OMP_CLAUSE_LOCATION (c), 12290 "%qE is not a variable in clause %qs", t, 12291 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 12292 remove = true; 12293 } 12294 else if (bitmap_bit_p (&generic_head, DECL_UID (t)) 12295 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) 12296 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) 12297 { 12298 error_at (OMP_CLAUSE_LOCATION (c), 12299 "%qE appears more than once in data clauses", t); 12300 remove = true; 12301 } 12302 else 12303 bitmap_set_bit (&generic_head, DECL_UID (t)); 12304 break; 12305 12306 case OMP_CLAUSE_FIRSTPRIVATE: 12307 t = OMP_CLAUSE_DECL (c); 12308 need_complete = true; 12309 need_implicitly_determined = true; 12310 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 12311 { 12312 error_at (OMP_CLAUSE_LOCATION (c), 12313 "%qE is not a variable in clause %<firstprivate%>", t); 12314 remove = true; 12315 } 12316 else if (bitmap_bit_p (&generic_head, DECL_UID (t)) 12317 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))) 12318 { 12319 error_at (OMP_CLAUSE_LOCATION (c), 12320 "%qE appears more than once in data clauses", t); 12321 remove = true; 12322 } 12323 else 12324 bitmap_set_bit (&firstprivate_head, DECL_UID (t)); 12325 break; 12326 12327 case OMP_CLAUSE_LASTPRIVATE: 12328 t = OMP_CLAUSE_DECL (c); 12329 need_complete = true; 12330 need_implicitly_determined = true; 12331 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 12332 { 12333 error_at (OMP_CLAUSE_LOCATION (c), 12334 "%qE is not a variable in clause %<lastprivate%>", t); 12335 remove = true; 12336 } 12337 else if (bitmap_bit_p (&generic_head, DECL_UID (t)) 12338 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) 12339 { 12340 error_at (OMP_CLAUSE_LOCATION (c), 12341 "%qE appears more than once in data clauses", t); 12342 remove = true; 12343 } 12344 else 12345 bitmap_set_bit (&lastprivate_head, DECL_UID (t)); 12346 break; 12347 12348 case OMP_CLAUSE_ALIGNED: 12349 t = OMP_CLAUSE_DECL (c); 12350 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 12351 { 12352 error_at (OMP_CLAUSE_LOCATION (c), 12353 "%qE is not a variable in %<aligned%> clause", t); 12354 remove = true; 12355 } 12356 else if (!POINTER_TYPE_P (TREE_TYPE (t)) 12357 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE) 12358 { 12359 error_at (OMP_CLAUSE_LOCATION (c), 12360 "%qE in %<aligned%> clause is neither a pointer nor " 12361 "an array", t); 12362 remove = true; 12363 } 12364 else if (bitmap_bit_p (&aligned_head, DECL_UID (t))) 12365 { 12366 error_at (OMP_CLAUSE_LOCATION (c), 12367 "%qE appears more than once in %<aligned%> clauses", 12368 t); 12369 remove = true; 12370 } 12371 else 12372 bitmap_set_bit (&aligned_head, DECL_UID (t)); 12373 break; 12374 12375 case OMP_CLAUSE_DEPEND: 12376 t = OMP_CLAUSE_DECL (c); 12377 if (TREE_CODE (t) == TREE_LIST) 12378 { 12379 if (handle_omp_array_sections (c)) 12380 remove = true; 12381 break; 12382 } 12383 if (t == error_mark_node) 12384 remove = true; 12385 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 12386 { 12387 error_at (OMP_CLAUSE_LOCATION (c), 12388 "%qE is not a variable in %<depend%> clause", t); 12389 remove = true; 12390 } 12391 else if (!c_mark_addressable (t)) 12392 remove = true; 12393 break; 12394 12395 case OMP_CLAUSE_MAP: 12396 case OMP_CLAUSE_TO: 12397 case OMP_CLAUSE_FROM: 12398 case OMP_CLAUSE__CACHE_: 12399 t = OMP_CLAUSE_DECL (c); 12400 if (TREE_CODE (t) == TREE_LIST) 12401 { 12402 if (handle_omp_array_sections (c)) 12403 remove = true; 12404 else 12405 { 12406 t = OMP_CLAUSE_DECL (c); 12407 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t))) 12408 { 12409 error_at (OMP_CLAUSE_LOCATION (c), 12410 "array section does not have mappable type " 12411 "in %qs clause", 12412 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 12413 remove = true; 12414 } 12415 } 12416 break; 12417 } 12418 if (t == error_mark_node) 12419 remove = true; 12420 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 12421 { 12422 error_at (OMP_CLAUSE_LOCATION (c), 12423 "%qE is not a variable in %qs clause", t, 12424 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 12425 remove = true; 12426 } 12427 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) 12428 { 12429 error_at (OMP_CLAUSE_LOCATION (c), 12430 "%qD is threadprivate variable in %qs clause", t, 12431 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 12432 remove = true; 12433 } 12434 else if (!c_mark_addressable (t)) 12435 remove = true; 12436 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP 12437 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER 12438 || (OMP_CLAUSE_MAP_KIND (c) 12439 == GOMP_MAP_FORCE_DEVICEPTR))) 12440 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t))) 12441 { 12442 error_at (OMP_CLAUSE_LOCATION (c), 12443 "%qD does not have a mappable type in %qs clause", t, 12444 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 12445 remove = true; 12446 } 12447 else if (bitmap_bit_p (&generic_head, DECL_UID (t))) 12448 { 12449 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) 12450 error ("%qD appears more than once in motion clauses", t); 12451 else 12452 error ("%qD appears more than once in map clauses", t); 12453 remove = true; 12454 } 12455 else 12456 bitmap_set_bit (&generic_head, DECL_UID (t)); 12457 break; 12458 12459 case OMP_CLAUSE_UNIFORM: 12460 t = OMP_CLAUSE_DECL (c); 12461 if (TREE_CODE (t) != PARM_DECL) 12462 { 12463 if (DECL_P (t)) 12464 error_at (OMP_CLAUSE_LOCATION (c), 12465 "%qD is not an argument in %<uniform%> clause", t); 12466 else 12467 error_at (OMP_CLAUSE_LOCATION (c), 12468 "%qE is not an argument in %<uniform%> clause", t); 12469 remove = true; 12470 break; 12471 } 12472 goto check_dup_generic; 12473 12474 case OMP_CLAUSE_NOWAIT: 12475 if (copyprivate_seen) 12476 { 12477 error_at (OMP_CLAUSE_LOCATION (c), 12478 "%<nowait%> clause must not be used together " 12479 "with %<copyprivate%>"); 12480 remove = true; 12481 break; 12482 } 12483 nowait_clause = pc; 12484 pc = &OMP_CLAUSE_CHAIN (c); 12485 continue; 12486 12487 case OMP_CLAUSE_IF: 12488 case OMP_CLAUSE_NUM_THREADS: 12489 case OMP_CLAUSE_NUM_TEAMS: 12490 case OMP_CLAUSE_THREAD_LIMIT: 12491 case OMP_CLAUSE_SCHEDULE: 12492 case OMP_CLAUSE_ORDERED: 12493 case OMP_CLAUSE_DEFAULT: 12494 case OMP_CLAUSE_UNTIED: 12495 case OMP_CLAUSE_COLLAPSE: 12496 case OMP_CLAUSE_FINAL: 12497 case OMP_CLAUSE_MERGEABLE: 12498 case OMP_CLAUSE_SAFELEN: 12499 case OMP_CLAUSE_SIMDLEN: 12500 case OMP_CLAUSE_DEVICE: 12501 case OMP_CLAUSE_DIST_SCHEDULE: 12502 case OMP_CLAUSE_PARALLEL: 12503 case OMP_CLAUSE_FOR: 12504 case OMP_CLAUSE_SECTIONS: 12505 case OMP_CLAUSE_TASKGROUP: 12506 case OMP_CLAUSE_PROC_BIND: 12507 case OMP_CLAUSE__CILK_FOR_COUNT_: 12508 case OMP_CLAUSE_NUM_GANGS: 12509 case OMP_CLAUSE_NUM_WORKERS: 12510 case OMP_CLAUSE_VECTOR_LENGTH: 12511 case OMP_CLAUSE_ASYNC: 12512 case OMP_CLAUSE_WAIT: 12513 case OMP_CLAUSE_AUTO: 12514 case OMP_CLAUSE_SEQ: 12515 case OMP_CLAUSE_GANG: 12516 case OMP_CLAUSE_WORKER: 12517 case OMP_CLAUSE_VECTOR: 12518 pc = &OMP_CLAUSE_CHAIN (c); 12519 continue; 12520 12521 case OMP_CLAUSE_INBRANCH: 12522 case OMP_CLAUSE_NOTINBRANCH: 12523 if (branch_seen) 12524 { 12525 error_at (OMP_CLAUSE_LOCATION (c), 12526 "%<inbranch%> clause is incompatible with " 12527 "%<notinbranch%>"); 12528 remove = true; 12529 break; 12530 } 12531 branch_seen = true; 12532 pc = &OMP_CLAUSE_CHAIN (c); 12533 continue; 12534 12535 default: 12536 gcc_unreachable (); 12537 } 12538 12539 if (!remove) 12540 { 12541 t = OMP_CLAUSE_DECL (c); 12542 12543 if (need_complete) 12544 { 12545 t = require_complete_type (t); 12546 if (t == error_mark_node) 12547 remove = true; 12548 } 12549 12550 if (need_implicitly_determined) 12551 { 12552 const char *share_name = NULL; 12553 12554 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) 12555 share_name = "threadprivate"; 12556 else switch (c_omp_predetermined_sharing (t)) 12557 { 12558 case OMP_CLAUSE_DEFAULT_UNSPECIFIED: 12559 break; 12560 case OMP_CLAUSE_DEFAULT_SHARED: 12561 /* const vars may be specified in firstprivate clause. */ 12562 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE 12563 && TREE_READONLY (t)) 12564 break; 12565 share_name = "shared"; 12566 break; 12567 case OMP_CLAUSE_DEFAULT_PRIVATE: 12568 share_name = "private"; 12569 break; 12570 default: 12571 gcc_unreachable (); 12572 } 12573 if (share_name) 12574 { 12575 error_at (OMP_CLAUSE_LOCATION (c), 12576 "%qE is predetermined %qs for %qs", 12577 t, share_name, 12578 omp_clause_code_name[OMP_CLAUSE_CODE (c)]); 12579 remove = true; 12580 } 12581 } 12582 } 12583 12584 if (remove) 12585 *pc = OMP_CLAUSE_CHAIN (c); 12586 else 12587 pc = &OMP_CLAUSE_CHAIN (c); 12588 } 12589 12590 bitmap_obstack_release (NULL); 12591 return clauses; 12592} 12593 12594/* Create a transaction node. */ 12595 12596tree 12597c_finish_transaction (location_t loc, tree block, int flags) 12598{ 12599 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block); 12600 if (flags & TM_STMT_ATTR_OUTER) 12601 TRANSACTION_EXPR_OUTER (stmt) = 1; 12602 if (flags & TM_STMT_ATTR_RELAXED) 12603 TRANSACTION_EXPR_RELAXED (stmt) = 1; 12604 return add_stmt (stmt); 12605} 12606 12607/* Make a variant type in the proper way for C/C++, propagating qualifiers 12608 down to the element type of an array. If ORIG_QUAL_TYPE is not 12609 NULL, then it should be used as the qualified type 12610 ORIG_QUAL_INDIRECT levels down in array type derivation (to 12611 preserve information about the typedef name from which an array 12612 type was derived). */ 12613 12614tree 12615c_build_qualified_type (tree type, int type_quals, tree orig_qual_type, 12616 size_t orig_qual_indirect) 12617{ 12618 if (type == error_mark_node) 12619 return type; 12620 12621 if (TREE_CODE (type) == ARRAY_TYPE) 12622 { 12623 tree t; 12624 tree element_type = c_build_qualified_type (TREE_TYPE (type), 12625 type_quals, orig_qual_type, 12626 orig_qual_indirect - 1); 12627 12628 /* See if we already have an identically qualified type. */ 12629 if (orig_qual_type && orig_qual_indirect == 0) 12630 t = orig_qual_type; 12631 else 12632 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) 12633 { 12634 if (TYPE_QUALS (strip_array_types (t)) == type_quals 12635 && TYPE_NAME (t) == TYPE_NAME (type) 12636 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type) 12637 && attribute_list_equal (TYPE_ATTRIBUTES (t), 12638 TYPE_ATTRIBUTES (type))) 12639 break; 12640 } 12641 if (!t) 12642 { 12643 tree domain = TYPE_DOMAIN (type); 12644 12645 t = build_variant_type_copy (type); 12646 TREE_TYPE (t) = element_type; 12647 12648 if (TYPE_STRUCTURAL_EQUALITY_P (element_type) 12649 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain))) 12650 SET_TYPE_STRUCTURAL_EQUALITY (t); 12651 else if (TYPE_CANONICAL (element_type) != element_type 12652 || (domain && TYPE_CANONICAL (domain) != domain)) 12653 { 12654 tree unqualified_canon 12655 = build_array_type (TYPE_CANONICAL (element_type), 12656 domain? TYPE_CANONICAL (domain) 12657 : NULL_TREE); 12658 TYPE_CANONICAL (t) 12659 = c_build_qualified_type (unqualified_canon, type_quals); 12660 } 12661 else 12662 TYPE_CANONICAL (t) = t; 12663 } 12664 return t; 12665 } 12666 12667 /* A restrict-qualified pointer type must be a pointer to object or 12668 incomplete type. Note that the use of POINTER_TYPE_P also allows 12669 REFERENCE_TYPEs, which is appropriate for C++. */ 12670 if ((type_quals & TYPE_QUAL_RESTRICT) 12671 && (!POINTER_TYPE_P (type) 12672 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) 12673 { 12674 error ("invalid use of %<restrict%>"); 12675 type_quals &= ~TYPE_QUAL_RESTRICT; 12676 } 12677 12678 tree var_type = (orig_qual_type && orig_qual_indirect == 0 12679 ? orig_qual_type 12680 : build_qualified_type (type, type_quals)); 12681 return var_type; 12682} 12683 12684/* Build a VA_ARG_EXPR for the C parser. */ 12685 12686tree 12687c_build_va_arg (location_t loc, tree expr, tree type) 12688{ 12689 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE) 12690 warning_at (loc, OPT_Wc___compat, 12691 "C++ requires promoted type, not enum type, in %<va_arg%>"); 12692 return build_va_arg (loc, expr, type); 12693} 12694 12695/* Return truthvalue of whether T1 is the same tree structure as T2. 12696 Return 1 if they are the same. Return 0 if they are different. */ 12697 12698bool 12699c_tree_equal (tree t1, tree t2) 12700{ 12701 enum tree_code code1, code2; 12702 12703 if (t1 == t2) 12704 return true; 12705 if (!t1 || !t2) 12706 return false; 12707 12708 for (code1 = TREE_CODE (t1); 12709 CONVERT_EXPR_CODE_P (code1) 12710 || code1 == NON_LVALUE_EXPR; 12711 code1 = TREE_CODE (t1)) 12712 t1 = TREE_OPERAND (t1, 0); 12713 for (code2 = TREE_CODE (t2); 12714 CONVERT_EXPR_CODE_P (code2) 12715 || code2 == NON_LVALUE_EXPR; 12716 code2 = TREE_CODE (t2)) 12717 t2 = TREE_OPERAND (t2, 0); 12718 12719 /* They might have become equal now. */ 12720 if (t1 == t2) 12721 return true; 12722 12723 if (code1 != code2) 12724 return false; 12725 12726 switch (code1) 12727 { 12728 case INTEGER_CST: 12729 return wi::eq_p (t1, t2); 12730 12731 case REAL_CST: 12732 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2)); 12733 12734 case STRING_CST: 12735 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2) 12736 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2), 12737 TREE_STRING_LENGTH (t1)); 12738 12739 case FIXED_CST: 12740 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), 12741 TREE_FIXED_CST (t2)); 12742 12743 case COMPLEX_CST: 12744 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2)) 12745 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2)); 12746 12747 case VECTOR_CST: 12748 return operand_equal_p (t1, t2, OEP_ONLY_CONST); 12749 12750 case CONSTRUCTOR: 12751 /* We need to do this when determining whether or not two 12752 non-type pointer to member function template arguments 12753 are the same. */ 12754 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)) 12755 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2)) 12756 return false; 12757 { 12758 tree field, value; 12759 unsigned int i; 12760 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value) 12761 { 12762 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i); 12763 if (!c_tree_equal (field, elt2->index) 12764 || !c_tree_equal (value, elt2->value)) 12765 return false; 12766 } 12767 } 12768 return true; 12769 12770 case TREE_LIST: 12771 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))) 12772 return false; 12773 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2))) 12774 return false; 12775 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2)); 12776 12777 case SAVE_EXPR: 12778 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); 12779 12780 case CALL_EXPR: 12781 { 12782 tree arg1, arg2; 12783 call_expr_arg_iterator iter1, iter2; 12784 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2))) 12785 return false; 12786 for (arg1 = first_call_expr_arg (t1, &iter1), 12787 arg2 = first_call_expr_arg (t2, &iter2); 12788 arg1 && arg2; 12789 arg1 = next_call_expr_arg (&iter1), 12790 arg2 = next_call_expr_arg (&iter2)) 12791 if (!c_tree_equal (arg1, arg2)) 12792 return false; 12793 if (arg1 || arg2) 12794 return false; 12795 return true; 12796 } 12797 12798 case TARGET_EXPR: 12799 { 12800 tree o1 = TREE_OPERAND (t1, 0); 12801 tree o2 = TREE_OPERAND (t2, 0); 12802 12803 /* Special case: if either target is an unallocated VAR_DECL, 12804 it means that it's going to be unified with whatever the 12805 TARGET_EXPR is really supposed to initialize, so treat it 12806 as being equivalent to anything. */ 12807 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE 12808 && !DECL_RTL_SET_P (o1)) 12809 /*Nop*/; 12810 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE 12811 && !DECL_RTL_SET_P (o2)) 12812 /*Nop*/; 12813 else if (!c_tree_equal (o1, o2)) 12814 return false; 12815 12816 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)); 12817 } 12818 12819 case COMPONENT_REF: 12820 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1)) 12821 return false; 12822 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); 12823 12824 case PARM_DECL: 12825 case VAR_DECL: 12826 case CONST_DECL: 12827 case FIELD_DECL: 12828 case FUNCTION_DECL: 12829 case IDENTIFIER_NODE: 12830 case SSA_NAME: 12831 return false; 12832 12833 case TREE_VEC: 12834 { 12835 unsigned ix; 12836 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) 12837 return false; 12838 for (ix = TREE_VEC_LENGTH (t1); ix--;) 12839 if (!c_tree_equal (TREE_VEC_ELT (t1, ix), 12840 TREE_VEC_ELT (t2, ix))) 12841 return false; 12842 return true; 12843 } 12844 12845 default: 12846 break; 12847 } 12848 12849 switch (TREE_CODE_CLASS (code1)) 12850 { 12851 case tcc_unary: 12852 case tcc_binary: 12853 case tcc_comparison: 12854 case tcc_expression: 12855 case tcc_vl_exp: 12856 case tcc_reference: 12857 case tcc_statement: 12858 { 12859 int i, n = TREE_OPERAND_LENGTH (t1); 12860 12861 switch (code1) 12862 { 12863 case PREINCREMENT_EXPR: 12864 case PREDECREMENT_EXPR: 12865 case POSTINCREMENT_EXPR: 12866 case POSTDECREMENT_EXPR: 12867 n = 1; 12868 break; 12869 case ARRAY_REF: 12870 n = 2; 12871 break; 12872 default: 12873 break; 12874 } 12875 12876 if (TREE_CODE_CLASS (code1) == tcc_vl_exp 12877 && n != TREE_OPERAND_LENGTH (t2)) 12878 return false; 12879 12880 for (i = 0; i < n; ++i) 12881 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i))) 12882 return false; 12883 12884 return true; 12885 } 12886 12887 case tcc_type: 12888 return comptypes (t1, t2); 12889 default: 12890 gcc_unreachable (); 12891 } 12892 /* We can get here with --disable-checking. */ 12893 return false; 12894} 12895 12896/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a 12897 spawn-helper and BODY is the newly created body for FNDECL. */ 12898 12899void 12900cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w) 12901{ 12902 tree list = alloc_stmt_list (); 12903 tree frame = make_cilk_frame (fndecl); 12904 tree dtor = create_cilk_function_exit (frame, false, true); 12905 add_local_decl (cfun, frame); 12906 12907 DECL_SAVED_TREE (fndecl) = list; 12908 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)), 12909 frame); 12910 tree body_list = cilk_install_body_pedigree_operations (frame_ptr); 12911 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST); 12912 12913 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr); 12914 append_to_statement_list (detach_expr, &body_list); 12915 12916 cilk_outline (fndecl, &body, (struct wrapper_data *) w); 12917 body = fold_build_cleanup_point_expr (void_type_node, body); 12918 12919 append_to_statement_list (body, &body_list); 12920 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR, 12921 body_list, dtor), &list); 12922} 12923