1 /* Subroutines shared by all languages that are variants of C. 2 Copyright (C) 1992-2013 Free Software Foundation, Inc. 3 4 This file is part of GCC. 5 6 GCC is free software; you can redistribute it and/or modify it under 7 the terms of the GNU General Public License as published by the Free 8 Software Foundation; either version 3, or (at your option) any later 9 version. 10 11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12 WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GCC; see the file COPYING3. If not see 18 <http://www.gnu.org/licenses/>. */ 19 20 #include "config.h" 21 #include "system.h" 22 #include "coretypes.h" 23 #include "tm.h" 24 #include "intl.h" 25 #include "tree.h" 26 #include "flags.h" 27 #include "c-pragma.h" 28 #include "ggc.h" 29 #include "c-common.h" 30 #include "c-objc.h" 31 #include "tm_p.h" 32 #include "obstack.h" 33 #include "cpplib.h" 34 #include "target.h" 35 #include "common/common-target.h" 36 #include "langhooks.h" 37 #include "tree-inline.h" 38 #include "toplev.h" 39 #include "diagnostic.h" 40 #include "tree-iterator.h" 41 #include "hashtab.h" 42 #include "tree-mudflap.h" 43 #include "opts.h" 44 #include "cgraph.h" 45 #include "target-def.h" 46 47 cpp_reader *parse_in; /* Declared in c-pragma.h. */ 48 49 /* The following symbols are subsumed in the c_global_trees array, and 50 listed here individually for documentation purposes. 51 52 INTEGER_TYPE and REAL_TYPE nodes for the standard data types. 53 54 tree short_integer_type_node; 55 tree long_integer_type_node; 56 tree long_long_integer_type_node; 57 tree int128_integer_type_node; 58 59 tree short_unsigned_type_node; 60 tree long_unsigned_type_node; 61 tree long_long_unsigned_type_node; 62 tree int128_unsigned_type_node; 63 64 tree truthvalue_type_node; 65 tree truthvalue_false_node; 66 tree truthvalue_true_node; 67 68 tree ptrdiff_type_node; 69 70 tree unsigned_char_type_node; 71 tree signed_char_type_node; 72 tree wchar_type_node; 73 74 tree char16_type_node; 75 tree char32_type_node; 76 77 tree float_type_node; 78 tree double_type_node; 79 tree long_double_type_node; 80 81 tree complex_integer_type_node; 82 tree complex_float_type_node; 83 tree complex_double_type_node; 84 tree complex_long_double_type_node; 85 86 tree dfloat32_type_node; 87 tree dfloat64_type_node; 88 tree_dfloat128_type_node; 89 90 tree intQI_type_node; 91 tree intHI_type_node; 92 tree intSI_type_node; 93 tree intDI_type_node; 94 tree intTI_type_node; 95 96 tree unsigned_intQI_type_node; 97 tree unsigned_intHI_type_node; 98 tree unsigned_intSI_type_node; 99 tree unsigned_intDI_type_node; 100 tree unsigned_intTI_type_node; 101 102 tree widest_integer_literal_type_node; 103 tree widest_unsigned_literal_type_node; 104 105 Nodes for types `void *' and `const void *'. 106 107 tree ptr_type_node, const_ptr_type_node; 108 109 Nodes for types `char *' and `const char *'. 110 111 tree string_type_node, const_string_type_node; 112 113 Type `char[SOMENUMBER]'. 114 Used when an array of char is needed and the size is irrelevant. 115 116 tree char_array_type_node; 117 118 Type `int[SOMENUMBER]' or something like it. 119 Used when an array of int needed and the size is irrelevant. 120 121 tree int_array_type_node; 122 123 Type `wchar_t[SOMENUMBER]' or something like it. 124 Used when a wide string literal is created. 125 126 tree wchar_array_type_node; 127 128 Type `char16_t[SOMENUMBER]' or something like it. 129 Used when a UTF-16 string literal is created. 130 131 tree char16_array_type_node; 132 133 Type `char32_t[SOMENUMBER]' or something like it. 134 Used when a UTF-32 string literal is created. 135 136 tree char32_array_type_node; 137 138 Type `int ()' -- used for implicit declaration of functions. 139 140 tree default_function_type; 141 142 A VOID_TYPE node, packaged in a TREE_LIST. 143 144 tree void_list_node; 145 146 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__, 147 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__ 148 VAR_DECLS, but C++ does.) 149 150 tree function_name_decl_node; 151 tree pretty_function_name_decl_node; 152 tree c99_function_name_decl_node; 153 154 Stack of nested function name VAR_DECLs. 155 156 tree saved_function_name_decls; 157 158 */ 159 160 tree c_global_trees[CTI_MAX]; 161 162 /* Switches common to the C front ends. */ 163 164 /* Nonzero means don't output line number information. */ 165 166 char flag_no_line_commands; 167 168 /* Nonzero causes -E output not to be done, but directives such as 169 #define that have side effects are still obeyed. */ 170 171 char flag_no_output; 172 173 /* Nonzero means dump macros in some fashion. */ 174 175 char flag_dump_macros; 176 177 /* Nonzero means pass #include lines through to the output. */ 178 179 char flag_dump_includes; 180 181 /* Nonzero means process PCH files while preprocessing. */ 182 183 bool flag_pch_preprocess; 184 185 /* The file name to which we should write a precompiled header, or 186 NULL if no header will be written in this compile. */ 187 188 const char *pch_file; 189 190 /* Nonzero if an ISO standard was selected. It rejects macros in the 191 user's namespace. */ 192 int flag_iso; 193 194 /* C/ObjC language option variables. */ 195 196 197 /* Nonzero means allow type mismatches in conditional expressions; 198 just make their values `void'. */ 199 200 int flag_cond_mismatch; 201 202 /* Nonzero means enable C89 Amendment 1 features. */ 203 204 int flag_isoc94; 205 206 /* Nonzero means use the ISO C99 (or C11) dialect of C. */ 207 208 int flag_isoc99; 209 210 /* Nonzero means use the ISO C11 dialect of C. */ 211 212 int flag_isoc11; 213 214 /* Nonzero means that we have builtin functions, and main is an int. */ 215 216 int flag_hosted = 1; 217 218 219 /* ObjC language option variables. */ 220 221 222 /* Tells the compiler that this is a special run. Do not perform any 223 compiling, instead we are to test some platform dependent features 224 and output a C header file with appropriate definitions. */ 225 226 int print_struct_values; 227 228 /* Tells the compiler what is the constant string class for ObjC. */ 229 230 const char *constant_string_class_name; 231 232 233 /* C++ language option variables. */ 234 235 236 /* Nonzero means generate separate instantiation control files and 237 juggle them at link time. */ 238 239 int flag_use_repository; 240 241 /* The C++ dialect being used. C++98 is the default. */ 242 243 enum cxx_dialect cxx_dialect = cxx98; 244 245 /* Maximum template instantiation depth. This limit exists to limit the 246 time it takes to notice excessively recursive template instantiations. 247 248 The default is lower than the 1024 recommended by the C++0x standard 249 because G++ runs out of stack before 1024 with highly recursive template 250 argument deduction substitution (g++.dg/cpp0x/enum11.C). */ 251 252 int max_tinst_depth = 900; 253 254 /* The elements of `ridpointers' are identifier nodes for the reserved 255 type names and storage classes. It is indexed by a RID_... value. */ 256 tree *ridpointers; 257 258 tree (*make_fname_decl) (location_t, tree, int); 259 260 /* Nonzero means don't warn about problems that occur when the code is 261 executed. */ 262 int c_inhibit_evaluation_warnings; 263 264 /* Whether we are building a boolean conversion inside 265 convert_for_assignment, or some other late binary operation. If 266 build_binary_op is called for C (from code shared by C and C++) in 267 this case, then the operands have already been folded and the 268 result will not be folded again, so C_MAYBE_CONST_EXPR should not 269 be generated. */ 270 bool in_late_binary_op; 271 272 /* Whether lexing has been completed, so subsequent preprocessor 273 errors should use the compiler's input_location. */ 274 bool done_lexing = false; 275 276 /* Information about how a function name is generated. */ 277 struct fname_var_t 278 { 279 tree *const decl; /* pointer to the VAR_DECL. */ 280 const unsigned rid; /* RID number for the identifier. */ 281 const int pretty; /* How pretty is it? */ 282 }; 283 284 /* The three ways of getting then name of the current function. */ 285 286 const struct fname_var_t fname_vars[] = 287 { 288 /* C99 compliant __func__, must be first. */ 289 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0}, 290 /* GCC __FUNCTION__ compliant. */ 291 {&function_name_decl_node, RID_FUNCTION_NAME, 0}, 292 /* GCC __PRETTY_FUNCTION__ compliant. */ 293 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1}, 294 {NULL, 0, 0}, 295 }; 296 297 /* Global visibility options. */ 298 struct visibility_flags visibility_options; 299 300 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *); 301 static tree check_case_value (tree); 302 static bool check_case_bounds (tree, tree, tree *, tree *); 303 304 static tree handle_packed_attribute (tree *, tree, tree, int, bool *); 305 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *); 306 static tree handle_common_attribute (tree *, tree, tree, int, bool *); 307 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *); 308 static tree handle_hot_attribute (tree *, tree, tree, int, bool *); 309 static tree handle_cold_attribute (tree *, tree, tree, int, bool *); 310 static tree handle_no_sanitize_address_attribute (tree *, tree, tree, 311 int, bool *); 312 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree, 313 int, bool *); 314 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *); 315 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *); 316 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *); 317 static tree handle_always_inline_attribute (tree *, tree, tree, int, 318 bool *); 319 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *); 320 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *); 321 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *); 322 static tree handle_error_attribute (tree *, tree, tree, int, bool *); 323 static tree handle_used_attribute (tree *, tree, tree, int, bool *); 324 static tree handle_unused_attribute (tree *, tree, tree, int, bool *); 325 static tree handle_externally_visible_attribute (tree *, tree, tree, int, 326 bool *); 327 static tree handle_const_attribute (tree *, tree, tree, int, bool *); 328 static tree handle_transparent_union_attribute (tree *, tree, tree, 329 int, bool *); 330 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *); 331 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *); 332 static tree handle_mode_attribute (tree *, tree, tree, int, bool *); 333 static tree handle_section_attribute (tree *, tree, tree, int, bool *); 334 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *); 335 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ; 336 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *); 337 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *); 338 static tree handle_alias_attribute (tree *, tree, tree, int, bool *); 339 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ; 340 static tree handle_visibility_attribute (tree *, tree, tree, int, 341 bool *); 342 static tree handle_tls_model_attribute (tree *, tree, tree, int, 343 bool *); 344 static tree handle_no_instrument_function_attribute (tree *, tree, 345 tree, int, bool *); 346 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *); 347 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *); 348 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int, 349 bool *); 350 static tree handle_pure_attribute (tree *, tree, tree, int, bool *); 351 static tree handle_tm_attribute (tree *, tree, tree, int, bool *); 352 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *); 353 static tree handle_novops_attribute (tree *, tree, tree, int, bool *); 354 static tree handle_deprecated_attribute (tree *, tree, tree, int, 355 bool *); 356 static tree handle_vector_size_attribute (tree *, tree, tree, int, 357 bool *); 358 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *); 359 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *); 360 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *); 361 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int, 362 bool *); 363 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *); 364 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *); 365 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *); 366 static tree handle_target_attribute (tree *, tree, tree, int, bool *); 367 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *); 368 static tree ignore_attribute (tree *, tree, tree, int, bool *); 369 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *); 370 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *); 371 372 static void check_function_nonnull (tree, int, tree *); 373 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT); 374 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT); 375 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *); 376 static int resort_field_decl_cmp (const void *, const void *); 377 378 /* Reserved words. The third field is a mask: keywords are disabled 379 if they match the mask. 380 381 Masks for languages: 382 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC 383 C --std=c99: D_CXXONLY | D_OBJC 384 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set 385 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC 386 C++ --std=c0x: D_CONLY | D_OBJC 387 ObjC++ is like C++ except that D_OBJC is not set 388 389 If -fno-asm is used, D_ASM is added to the mask. If 390 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in 391 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords. 392 In C with -Wc++-compat, we warn if D_CXXWARN is set. 393 394 Note the complication of the D_CXX_OBJC keywords. These are 395 reserved words such as 'class'. In C++, 'class' is a reserved 396 word. In Objective-C++ it is too. In Objective-C, it is a 397 reserved word too, but only if it follows an '@' sign. 398 */ 399 const struct c_common_resword c_common_reswords[] = 400 { 401 { "_Alignas", RID_ALIGNAS, D_CONLY }, 402 { "_Alignof", RID_ALIGNOF, D_CONLY }, 403 { "_Bool", RID_BOOL, D_CONLY }, 404 { "_Complex", RID_COMPLEX, 0 }, 405 { "_Imaginary", RID_IMAGINARY, D_CONLY }, 406 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT }, 407 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT }, 408 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT }, 409 { "_Fract", RID_FRACT, D_CONLY | D_EXT }, 410 { "_Accum", RID_ACCUM, D_CONLY | D_EXT }, 411 { "_Sat", RID_SAT, D_CONLY | D_EXT }, 412 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY }, 413 { "_Noreturn", RID_NORETURN, D_CONLY }, 414 { "__FUNCTION__", RID_FUNCTION_NAME, 0 }, 415 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 }, 416 { "__alignof", RID_ALIGNOF, 0 }, 417 { "__alignof__", RID_ALIGNOF, 0 }, 418 { "__asm", RID_ASM, 0 }, 419 { "__asm__", RID_ASM, 0 }, 420 { "__attribute", RID_ATTRIBUTE, 0 }, 421 { "__attribute__", RID_ATTRIBUTE, 0 }, 422 { "__bases", RID_BASES, D_CXXONLY }, 423 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY }, 424 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY }, 425 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 }, 426 { "__builtin_offsetof", RID_OFFSETOF, 0 }, 427 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY }, 428 { "__builtin_va_arg", RID_VA_ARG, 0 }, 429 { "__complex", RID_COMPLEX, 0 }, 430 { "__complex__", RID_COMPLEX, 0 }, 431 { "__const", RID_CONST, 0 }, 432 { "__const__", RID_CONST, 0 }, 433 { "__decltype", RID_DECLTYPE, D_CXXONLY }, 434 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY }, 435 { "__extension__", RID_EXTENSION, 0 }, 436 { "__func__", RID_C99_FUNCTION_NAME, 0 }, 437 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY }, 438 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY }, 439 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY }, 440 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY }, 441 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY }, 442 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY }, 443 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY }, 444 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY }, 445 { "__imag", RID_IMAGPART, 0 }, 446 { "__imag__", RID_IMAGPART, 0 }, 447 { "__inline", RID_INLINE, 0 }, 448 { "__inline__", RID_INLINE, 0 }, 449 { "__int128", RID_INT128, 0 }, 450 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY }, 451 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY }, 452 { "__is_class", RID_IS_CLASS, D_CXXONLY }, 453 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY }, 454 { "__is_empty", RID_IS_EMPTY, D_CXXONLY }, 455 { "__is_enum", RID_IS_ENUM, D_CXXONLY }, 456 { "__is_final", RID_IS_FINAL, D_CXXONLY }, 457 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY }, 458 { "__is_pod", RID_IS_POD, D_CXXONLY }, 459 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY }, 460 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY }, 461 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY }, 462 { "__is_union", RID_IS_UNION, D_CXXONLY }, 463 { "__label__", RID_LABEL, 0 }, 464 { "__null", RID_NULL, 0 }, 465 { "__real", RID_REALPART, 0 }, 466 { "__real__", RID_REALPART, 0 }, 467 { "__restrict", RID_RESTRICT, 0 }, 468 { "__restrict__", RID_RESTRICT, 0 }, 469 { "__signed", RID_SIGNED, 0 }, 470 { "__signed__", RID_SIGNED, 0 }, 471 { "__thread", RID_THREAD, 0 }, 472 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 }, 473 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 }, 474 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 }, 475 { "__typeof", RID_TYPEOF, 0 }, 476 { "__typeof__", RID_TYPEOF, 0 }, 477 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY }, 478 { "__volatile", RID_VOLATILE, 0 }, 479 { "__volatile__", RID_VOLATILE, 0 }, 480 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN }, 481 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN }, 482 { "asm", RID_ASM, D_ASM }, 483 { "auto", RID_AUTO, 0 }, 484 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN }, 485 { "break", RID_BREAK, 0 }, 486 { "case", RID_CASE, 0 }, 487 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN }, 488 { "char", RID_CHAR, 0 }, 489 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN }, 490 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN }, 491 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN }, 492 { "const", RID_CONST, 0 }, 493 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN }, 494 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN }, 495 { "continue", RID_CONTINUE, 0 }, 496 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN }, 497 { "default", RID_DEFAULT, 0 }, 498 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN }, 499 { "do", RID_DO, 0 }, 500 { "double", RID_DOUBLE, 0 }, 501 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN }, 502 { "else", RID_ELSE, 0 }, 503 { "enum", RID_ENUM, 0 }, 504 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN }, 505 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN }, 506 { "extern", RID_EXTERN, 0 }, 507 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN }, 508 { "float", RID_FLOAT, 0 }, 509 { "for", RID_FOR, 0 }, 510 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN }, 511 { "goto", RID_GOTO, 0 }, 512 { "if", RID_IF, 0 }, 513 { "inline", RID_INLINE, D_EXT89 }, 514 { "int", RID_INT, 0 }, 515 { "long", RID_LONG, 0 }, 516 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN }, 517 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN }, 518 { "new", RID_NEW, D_CXXONLY | D_CXXWARN }, 519 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN }, 520 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN }, 521 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN }, 522 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN }, 523 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN }, 524 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN }, 525 { "register", RID_REGISTER, 0 }, 526 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN }, 527 { "restrict", RID_RESTRICT, D_CONLY | D_C99 }, 528 { "return", RID_RETURN, 0 }, 529 { "short", RID_SHORT, 0 }, 530 { "signed", RID_SIGNED, 0 }, 531 { "sizeof", RID_SIZEOF, 0 }, 532 { "static", RID_STATIC, 0 }, 533 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN }, 534 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN }, 535 { "struct", RID_STRUCT, 0 }, 536 { "switch", RID_SWITCH, 0 }, 537 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN }, 538 { "this", RID_THIS, D_CXXONLY | D_CXXWARN }, 539 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN }, 540 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN }, 541 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN }, 542 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN }, 543 { "typedef", RID_TYPEDEF, 0 }, 544 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN }, 545 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN }, 546 { "typeof", RID_TYPEOF, D_ASM | D_EXT }, 547 { "union", RID_UNION, 0 }, 548 { "unsigned", RID_UNSIGNED, 0 }, 549 { "using", RID_USING, D_CXXONLY | D_CXXWARN }, 550 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN }, 551 { "void", RID_VOID, 0 }, 552 { "volatile", RID_VOLATILE, 0 }, 553 { "wchar_t", RID_WCHAR, D_CXXONLY }, 554 { "while", RID_WHILE, 0 }, 555 /* These Objective-C keywords are recognized only immediately after 556 an '@'. */ 557 { "compatibility_alias", RID_AT_ALIAS, D_OBJC }, 558 { "defs", RID_AT_DEFS, D_OBJC }, 559 { "encode", RID_AT_ENCODE, D_OBJC }, 560 { "end", RID_AT_END, D_OBJC }, 561 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC }, 562 { "interface", RID_AT_INTERFACE, D_OBJC }, 563 { "protocol", RID_AT_PROTOCOL, D_OBJC }, 564 { "selector", RID_AT_SELECTOR, D_OBJC }, 565 { "finally", RID_AT_FINALLY, D_OBJC }, 566 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC }, 567 { "optional", RID_AT_OPTIONAL, D_OBJC }, 568 { "required", RID_AT_REQUIRED, D_OBJC }, 569 { "property", RID_AT_PROPERTY, D_OBJC }, 570 { "package", RID_AT_PACKAGE, D_OBJC }, 571 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC }, 572 { "dynamic", RID_AT_DYNAMIC, D_OBJC }, 573 /* These are recognized only in protocol-qualifier context 574 (see above) */ 575 { "bycopy", RID_BYCOPY, D_OBJC }, 576 { "byref", RID_BYREF, D_OBJC }, 577 { "in", RID_IN, D_OBJC }, 578 { "inout", RID_INOUT, D_OBJC }, 579 { "oneway", RID_ONEWAY, D_OBJC }, 580 { "out", RID_OUT, D_OBJC }, 581 /* These are recognized inside a property attribute list */ 582 { "assign", RID_ASSIGN, D_OBJC }, 583 { "copy", RID_COPY, D_OBJC }, 584 { "getter", RID_GETTER, D_OBJC }, 585 { "nonatomic", RID_NONATOMIC, D_OBJC }, 586 { "readonly", RID_READONLY, D_OBJC }, 587 { "readwrite", RID_READWRITE, D_OBJC }, 588 { "retain", RID_RETAIN, D_OBJC }, 589 { "setter", RID_SETTER, D_OBJC }, 590 }; 591 592 const unsigned int num_c_common_reswords = 593 sizeof c_common_reswords / sizeof (struct c_common_resword); 594 595 /* Table of machine-independent attributes common to all C-like languages. */ 596 const struct attribute_spec c_common_attribute_table[] = 597 { 598 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, 599 affects_type_identity } */ 600 { "packed", 0, 0, false, false, false, 601 handle_packed_attribute , false}, 602 { "nocommon", 0, 0, true, false, false, 603 handle_nocommon_attribute, false}, 604 { "common", 0, 0, true, false, false, 605 handle_common_attribute, false }, 606 /* FIXME: logically, noreturn attributes should be listed as 607 "false, true, true" and apply to function types. But implementing this 608 would require all the places in the compiler that use TREE_THIS_VOLATILE 609 on a decl to identify non-returning functions to be located and fixed 610 to check the function type instead. */ 611 { "noreturn", 0, 0, true, false, false, 612 handle_noreturn_attribute, false }, 613 { "volatile", 0, 0, true, false, false, 614 handle_noreturn_attribute, false }, 615 { "noinline", 0, 0, true, false, false, 616 handle_noinline_attribute, false }, 617 { "noclone", 0, 0, true, false, false, 618 handle_noclone_attribute, false }, 619 { "leaf", 0, 0, true, false, false, 620 handle_leaf_attribute, false }, 621 { "always_inline", 0, 0, true, false, false, 622 handle_always_inline_attribute, false }, 623 { "gnu_inline", 0, 0, true, false, false, 624 handle_gnu_inline_attribute, false }, 625 { "artificial", 0, 0, true, false, false, 626 handle_artificial_attribute, false }, 627 { "flatten", 0, 0, true, false, false, 628 handle_flatten_attribute, false }, 629 { "used", 0, 0, true, false, false, 630 handle_used_attribute, false }, 631 { "unused", 0, 0, false, false, false, 632 handle_unused_attribute, false }, 633 { "externally_visible", 0, 0, true, false, false, 634 handle_externally_visible_attribute, false }, 635 /* The same comments as for noreturn attributes apply to const ones. */ 636 { "const", 0, 0, true, false, false, 637 handle_const_attribute, false }, 638 { "transparent_union", 0, 0, false, false, false, 639 handle_transparent_union_attribute, false }, 640 { "constructor", 0, 1, true, false, false, 641 handle_constructor_attribute, false }, 642 { "destructor", 0, 1, true, false, false, 643 handle_destructor_attribute, false }, 644 { "mode", 1, 1, false, true, false, 645 handle_mode_attribute, false }, 646 { "section", 1, 1, true, false, false, 647 handle_section_attribute, false }, 648 { "aligned", 0, 1, false, false, false, 649 handle_aligned_attribute, false }, 650 { "weak", 0, 0, true, false, false, 651 handle_weak_attribute, false }, 652 { "ifunc", 1, 1, true, false, false, 653 handle_ifunc_attribute, false }, 654 { "alias", 1, 1, true, false, false, 655 handle_alias_attribute, false }, 656 { "weakref", 0, 1, true, false, false, 657 handle_weakref_attribute, false }, 658 { "no_instrument_function", 0, 0, true, false, false, 659 handle_no_instrument_function_attribute, 660 false }, 661 { "malloc", 0, 0, true, false, false, 662 handle_malloc_attribute, false }, 663 { "returns_twice", 0, 0, true, false, false, 664 handle_returns_twice_attribute, false }, 665 { "no_stack_limit", 0, 0, true, false, false, 666 handle_no_limit_stack_attribute, false }, 667 { "pure", 0, 0, true, false, false, 668 handle_pure_attribute, false }, 669 { "transaction_callable", 0, 0, false, true, false, 670 handle_tm_attribute, false }, 671 { "transaction_unsafe", 0, 0, false, true, false, 672 handle_tm_attribute, false }, 673 { "transaction_safe", 0, 0, false, true, false, 674 handle_tm_attribute, false }, 675 { "transaction_may_cancel_outer", 0, 0, false, true, false, 676 handle_tm_attribute, false }, 677 /* ??? These two attributes didn't make the transition from the 678 Intel language document to the multi-vendor language document. */ 679 { "transaction_pure", 0, 0, false, true, false, 680 handle_tm_attribute, false }, 681 { "transaction_wrap", 1, 1, true, false, false, 682 handle_tm_wrap_attribute, false }, 683 /* For internal use (marking of builtins) only. The name contains space 684 to prevent its usage in source code. */ 685 { "no vops", 0, 0, true, false, false, 686 handle_novops_attribute, false }, 687 { "deprecated", 0, 1, false, false, false, 688 handle_deprecated_attribute, false }, 689 { "vector_size", 1, 1, false, true, false, 690 handle_vector_size_attribute, false }, 691 { "visibility", 1, 1, false, false, false, 692 handle_visibility_attribute, false }, 693 { "tls_model", 1, 1, true, false, false, 694 handle_tls_model_attribute, false }, 695 { "nonnull", 0, -1, false, true, true, 696 handle_nonnull_attribute, false }, 697 { "nothrow", 0, 0, true, false, false, 698 handle_nothrow_attribute, false }, 699 { "may_alias", 0, 0, false, true, false, NULL, false }, 700 { "cleanup", 1, 1, true, false, false, 701 handle_cleanup_attribute, false }, 702 { "warn_unused_result", 0, 0, false, true, true, 703 handle_warn_unused_result_attribute, false }, 704 { "sentinel", 0, 1, false, true, true, 705 handle_sentinel_attribute, false }, 706 /* For internal use (marking of builtins) only. The name contains space 707 to prevent its usage in source code. */ 708 { "type generic", 0, 0, false, true, true, 709 handle_type_generic_attribute, false }, 710 { "alloc_size", 1, 2, false, true, true, 711 handle_alloc_size_attribute, false }, 712 { "cold", 0, 0, true, false, false, 713 handle_cold_attribute, false }, 714 { "hot", 0, 0, true, false, false, 715 handle_hot_attribute, false }, 716 { "no_address_safety_analysis", 717 0, 0, true, false, false, 718 handle_no_address_safety_analysis_attribute, 719 false }, 720 { "no_sanitize_address", 0, 0, true, false, false, 721 handle_no_sanitize_address_attribute, 722 false }, 723 { "warning", 1, 1, true, false, false, 724 handle_error_attribute, false }, 725 { "error", 1, 1, true, false, false, 726 handle_error_attribute, false }, 727 { "target", 1, -1, true, false, false, 728 handle_target_attribute, false }, 729 { "optimize", 1, -1, true, false, false, 730 handle_optimize_attribute, false }, 731 /* For internal use only. The leading '*' both prevents its usage in 732 source code and signals that it may be overridden by machine tables. */ 733 { "*tm regparm", 0, 0, false, true, true, 734 ignore_attribute, false }, 735 { "no_split_stack", 0, 0, true, false, false, 736 handle_no_split_stack_attribute, false }, 737 /* For internal use (marking of builtins and runtime functions) only. 738 The name contains space to prevent its usage in source code. */ 739 { "fn spec", 1, 1, false, true, true, 740 handle_fnspec_attribute, false }, 741 { NULL, 0, 0, false, false, false, NULL, false } 742 }; 743 744 /* Give the specifications for the format attributes, used by C and all 745 descendants. */ 746 747 const struct attribute_spec c_common_format_attribute_table[] = 748 { 749 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, 750 affects_type_identity } */ 751 { "format", 3, 3, false, true, true, 752 handle_format_attribute, false }, 753 { "format_arg", 1, 1, false, true, true, 754 handle_format_arg_attribute, false }, 755 { NULL, 0, 0, false, false, false, NULL, false } 756 }; 757 758 /* Return identifier for address space AS. */ 759 760 const char * 761 c_addr_space_name (addr_space_t as) 762 { 763 int rid = RID_FIRST_ADDR_SPACE + as; 764 gcc_assert (ridpointers [rid]); 765 return IDENTIFIER_POINTER (ridpointers [rid]); 766 } 767 768 /* Push current bindings for the function name VAR_DECLS. */ 769 770 void 771 start_fname_decls (void) 772 { 773 unsigned ix; 774 tree saved = NULL_TREE; 775 776 for (ix = 0; fname_vars[ix].decl; ix++) 777 { 778 tree decl = *fname_vars[ix].decl; 779 780 if (decl) 781 { 782 saved = tree_cons (decl, build_int_cst (integer_type_node, ix), 783 saved); 784 *fname_vars[ix].decl = NULL_TREE; 785 } 786 } 787 if (saved || saved_function_name_decls) 788 /* Normally they'll have been NULL, so only push if we've got a 789 stack, or they are non-NULL. */ 790 saved_function_name_decls = tree_cons (saved, NULL_TREE, 791 saved_function_name_decls); 792 } 793 794 /* Finish up the current bindings, adding them into the current function's 795 statement tree. This must be done _before_ finish_stmt_tree is called. 796 If there is no current function, we must be at file scope and no statements 797 are involved. Pop the previous bindings. */ 798 799 void 800 finish_fname_decls (void) 801 { 802 unsigned ix; 803 tree stmts = NULL_TREE; 804 tree stack = saved_function_name_decls; 805 806 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack)) 807 append_to_statement_list (TREE_VALUE (stack), &stmts); 808 809 if (stmts) 810 { 811 tree *bodyp = &DECL_SAVED_TREE (current_function_decl); 812 813 if (TREE_CODE (*bodyp) == BIND_EXPR) 814 bodyp = &BIND_EXPR_BODY (*bodyp); 815 816 append_to_statement_list_force (*bodyp, &stmts); 817 *bodyp = stmts; 818 } 819 820 for (ix = 0; fname_vars[ix].decl; ix++) 821 *fname_vars[ix].decl = NULL_TREE; 822 823 if (stack) 824 { 825 /* We had saved values, restore them. */ 826 tree saved; 827 828 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved)) 829 { 830 tree decl = TREE_PURPOSE (saved); 831 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved)); 832 833 *fname_vars[ix].decl = decl; 834 } 835 stack = TREE_CHAIN (stack); 836 } 837 saved_function_name_decls = stack; 838 } 839 840 /* Return the text name of the current function, suitably prettified 841 by PRETTY_P. Return string must be freed by caller. */ 842 843 const char * 844 fname_as_string (int pretty_p) 845 { 846 const char *name = "top level"; 847 char *namep; 848 int vrb = 2, len; 849 cpp_string cstr = { 0, 0 }, strname; 850 851 if (!pretty_p) 852 { 853 name = ""; 854 vrb = 0; 855 } 856 857 if (current_function_decl) 858 name = lang_hooks.decl_printable_name (current_function_decl, vrb); 859 860 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */ 861 862 namep = XNEWVEC (char, len); 863 snprintf (namep, len, "\"%s\"", name); 864 strname.text = (unsigned char *) namep; 865 strname.len = len - 1; 866 867 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING)) 868 { 869 XDELETEVEC (namep); 870 return (const char *) cstr.text; 871 } 872 873 return namep; 874 } 875 876 /* Return the VAR_DECL for a const char array naming the current 877 function. If the VAR_DECL has not yet been created, create it 878 now. RID indicates how it should be formatted and IDENTIFIER_NODE 879 ID is its name (unfortunately C and C++ hold the RID values of 880 keywords in different places, so we can't derive RID from ID in 881 this language independent code. LOC is the location of the 882 function. */ 883 884 tree 885 fname_decl (location_t loc, unsigned int rid, tree id) 886 { 887 unsigned ix; 888 tree decl = NULL_TREE; 889 890 for (ix = 0; fname_vars[ix].decl; ix++) 891 if (fname_vars[ix].rid == rid) 892 break; 893 894 decl = *fname_vars[ix].decl; 895 if (!decl) 896 { 897 /* If a tree is built here, it would normally have the lineno of 898 the current statement. Later this tree will be moved to the 899 beginning of the function and this line number will be wrong. 900 To avoid this problem set the lineno to 0 here; that prevents 901 it from appearing in the RTL. */ 902 tree stmts; 903 location_t saved_location = input_location; 904 input_location = UNKNOWN_LOCATION; 905 906 stmts = push_stmt_list (); 907 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty); 908 stmts = pop_stmt_list (stmts); 909 if (!IS_EMPTY_STMT (stmts)) 910 saved_function_name_decls 911 = tree_cons (decl, stmts, saved_function_name_decls); 912 *fname_vars[ix].decl = decl; 913 input_location = saved_location; 914 } 915 if (!ix && !current_function_decl) 916 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl); 917 918 return decl; 919 } 920 921 /* Given a STRING_CST, give it a suitable array-of-chars data type. */ 922 923 tree 924 fix_string_type (tree value) 925 { 926 int length = TREE_STRING_LENGTH (value); 927 int nchars; 928 tree e_type, i_type, a_type; 929 930 /* Compute the number of elements, for the array type. */ 931 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value)) 932 { 933 nchars = length; 934 e_type = char_type_node; 935 } 936 else if (TREE_TYPE (value) == char16_array_type_node) 937 { 938 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT); 939 e_type = char16_type_node; 940 } 941 else if (TREE_TYPE (value) == char32_array_type_node) 942 { 943 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT); 944 e_type = char32_type_node; 945 } 946 else 947 { 948 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT); 949 e_type = wchar_type_node; 950 } 951 952 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous 953 limit in C++98 Annex B is very large (65536) and is not normative, 954 so we do not diagnose it (warn_overlength_strings is forced off 955 in c_common_post_options). */ 956 if (warn_overlength_strings) 957 { 958 const int nchars_max = flag_isoc99 ? 4095 : 509; 959 const int relevant_std = flag_isoc99 ? 99 : 90; 960 if (nchars - 1 > nchars_max) 961 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not 962 separate the %d from the 'C'. 'ISO' should not be 963 translated, but it may be moved after 'C%d' in languages 964 where modifiers follow nouns. */ 965 pedwarn (input_location, OPT_Woverlength_strings, 966 "string length %qd is greater than the length %qd " 967 "ISO C%d compilers are required to support", 968 nchars - 1, nchars_max, relevant_std); 969 } 970 971 /* Create the array type for the string constant. The ISO C++ 972 standard says that a string literal has type `const char[N]' or 973 `const wchar_t[N]'. We use the same logic when invoked as a C 974 front-end with -Wwrite-strings. 975 ??? We should change the type of an expression depending on the 976 state of a warning flag. We should just be warning -- see how 977 this is handled in the C++ front-end for the deprecated implicit 978 conversion from string literals to `char*' or `wchar_t*'. 979 980 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified 981 array type being the unqualified version of that type. 982 Therefore, if we are constructing an array of const char, we must 983 construct the matching unqualified array type first. The C front 984 end does not require this, but it does no harm, so we do it 985 unconditionally. */ 986 i_type = build_index_type (size_int (nchars - 1)); 987 a_type = build_array_type (e_type, i_type); 988 if (c_dialect_cxx() || warn_write_strings) 989 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST); 990 991 TREE_TYPE (value) = a_type; 992 TREE_CONSTANT (value) = 1; 993 TREE_READONLY (value) = 1; 994 TREE_STATIC (value) = 1; 995 return value; 996 } 997 998 /* If DISABLE is true, stop issuing warnings. This is used when 999 parsing code that we know will not be executed. This function may 1000 be called multiple times, and works as a stack. */ 1001 1002 static void 1003 c_disable_warnings (bool disable) 1004 { 1005 if (disable) 1006 { 1007 ++c_inhibit_evaluation_warnings; 1008 fold_defer_overflow_warnings (); 1009 } 1010 } 1011 1012 /* If ENABLE is true, reenable issuing warnings. */ 1013 1014 static void 1015 c_enable_warnings (bool enable) 1016 { 1017 if (enable) 1018 { 1019 --c_inhibit_evaluation_warnings; 1020 fold_undefer_and_ignore_overflow_warnings (); 1021 } 1022 } 1023 1024 /* Fully fold EXPR, an expression that was not folded (beyond integer 1025 constant expressions and null pointer constants) when being built 1026 up. If IN_INIT, this is in a static initializer and certain 1027 changes are made to the folding done. Clear *MAYBE_CONST if 1028 MAYBE_CONST is not NULL and EXPR is definitely not a constant 1029 expression because it contains an evaluated operator (in C99) or an 1030 operator outside of sizeof returning an integer constant (in C90) 1031 not permitted in constant expressions, or because it contains an 1032 evaluated arithmetic overflow. (*MAYBE_CONST should typically be 1033 set to true by callers before calling this function.) Return the 1034 folded expression. Function arguments have already been folded 1035 before calling this function, as have the contents of SAVE_EXPR, 1036 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and 1037 C_MAYBE_CONST_EXPR. */ 1038 1039 tree 1040 c_fully_fold (tree expr, bool in_init, bool *maybe_const) 1041 { 1042 tree ret; 1043 tree eptype = NULL_TREE; 1044 bool dummy = true; 1045 bool maybe_const_itself = true; 1046 location_t loc = EXPR_LOCATION (expr); 1047 1048 /* This function is not relevant to C++ because C++ folds while 1049 parsing, and may need changes to be correct for C++ when C++ 1050 stops folding while parsing. */ 1051 if (c_dialect_cxx ()) 1052 gcc_unreachable (); 1053 1054 if (!maybe_const) 1055 maybe_const = &dummy; 1056 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) 1057 { 1058 eptype = TREE_TYPE (expr); 1059 expr = TREE_OPERAND (expr, 0); 1060 } 1061 ret = c_fully_fold_internal (expr, in_init, maybe_const, 1062 &maybe_const_itself); 1063 if (eptype) 1064 ret = fold_convert_loc (loc, eptype, ret); 1065 *maybe_const &= maybe_const_itself; 1066 return ret; 1067 } 1068 1069 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for 1070 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands 1071 not permitted, while *MAYBE_CONST_ITSELF is cleared because of 1072 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from 1073 both evaluated and unevaluated subexpressions while 1074 *MAYBE_CONST_ITSELF is carried from only evaluated 1075 subexpressions). */ 1076 1077 static tree 1078 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands, 1079 bool *maybe_const_itself) 1080 { 1081 tree ret = expr; 1082 enum tree_code code = TREE_CODE (expr); 1083 enum tree_code_class kind = TREE_CODE_CLASS (code); 1084 location_t loc = EXPR_LOCATION (expr); 1085 tree op0, op1, op2, op3; 1086 tree orig_op0, orig_op1, orig_op2; 1087 bool op0_const = true, op1_const = true, op2_const = true; 1088 bool op0_const_self = true, op1_const_self = true, op2_const_self = true; 1089 bool nowarning = TREE_NO_WARNING (expr); 1090 bool unused_p; 1091 1092 /* This function is not relevant to C++ because C++ folds while 1093 parsing, and may need changes to be correct for C++ when C++ 1094 stops folding while parsing. */ 1095 if (c_dialect_cxx ()) 1096 gcc_unreachable (); 1097 1098 /* Constants, declarations, statements, errors, SAVE_EXPRs and 1099 anything else not counted as an expression cannot usefully be 1100 folded further at this point. */ 1101 if (!IS_EXPR_CODE_CLASS (kind) 1102 || kind == tcc_statement 1103 || code == SAVE_EXPR) 1104 return expr; 1105 1106 /* Operands of variable-length expressions (function calls) have 1107 already been folded, as have __builtin_* function calls, and such 1108 expressions cannot occur in constant expressions. */ 1109 if (kind == tcc_vl_exp) 1110 { 1111 *maybe_const_operands = false; 1112 ret = fold (expr); 1113 goto out; 1114 } 1115 1116 if (code == C_MAYBE_CONST_EXPR) 1117 { 1118 tree pre = C_MAYBE_CONST_EXPR_PRE (expr); 1119 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr); 1120 if (C_MAYBE_CONST_EXPR_NON_CONST (expr)) 1121 *maybe_const_operands = false; 1122 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr)) 1123 *maybe_const_itself = false; 1124 if (pre && !in_init) 1125 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner); 1126 else 1127 ret = inner; 1128 goto out; 1129 } 1130 1131 /* Assignment, increment, decrement, function call and comma 1132 operators, and statement expressions, cannot occur in constant 1133 expressions if evaluated / outside of sizeof. (Function calls 1134 were handled above, though VA_ARG_EXPR is treated like a function 1135 call here, and statement expressions are handled through 1136 C_MAYBE_CONST_EXPR to avoid folding inside them.) */ 1137 switch (code) 1138 { 1139 case MODIFY_EXPR: 1140 case PREDECREMENT_EXPR: 1141 case PREINCREMENT_EXPR: 1142 case POSTDECREMENT_EXPR: 1143 case POSTINCREMENT_EXPR: 1144 case COMPOUND_EXPR: 1145 *maybe_const_operands = false; 1146 break; 1147 1148 case VA_ARG_EXPR: 1149 case TARGET_EXPR: 1150 case BIND_EXPR: 1151 case OBJ_TYPE_REF: 1152 *maybe_const_operands = false; 1153 ret = fold (expr); 1154 goto out; 1155 1156 default: 1157 break; 1158 } 1159 1160 /* Fold individual tree codes as appropriate. */ 1161 switch (code) 1162 { 1163 case COMPOUND_LITERAL_EXPR: 1164 /* Any non-constancy will have been marked in a containing 1165 C_MAYBE_CONST_EXPR; there is no more folding to do here. */ 1166 goto out; 1167 1168 case COMPONENT_REF: 1169 orig_op0 = op0 = TREE_OPERAND (expr, 0); 1170 op1 = TREE_OPERAND (expr, 1); 1171 op2 = TREE_OPERAND (expr, 2); 1172 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, 1173 maybe_const_itself); 1174 STRIP_TYPE_NOPS (op0); 1175 if (op0 != orig_op0) 1176 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2); 1177 if (ret != expr) 1178 { 1179 TREE_READONLY (ret) = TREE_READONLY (expr); 1180 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); 1181 } 1182 goto out; 1183 1184 case ARRAY_REF: 1185 orig_op0 = op0 = TREE_OPERAND (expr, 0); 1186 orig_op1 = op1 = TREE_OPERAND (expr, 1); 1187 op2 = TREE_OPERAND (expr, 2); 1188 op3 = TREE_OPERAND (expr, 3); 1189 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, 1190 maybe_const_itself); 1191 STRIP_TYPE_NOPS (op0); 1192 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands, 1193 maybe_const_itself); 1194 STRIP_TYPE_NOPS (op1); 1195 op1 = decl_constant_value_for_optimization (op1); 1196 if (op0 != orig_op0 || op1 != orig_op1) 1197 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3); 1198 if (ret != expr) 1199 { 1200 TREE_READONLY (ret) = TREE_READONLY (expr); 1201 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr); 1202 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); 1203 } 1204 ret = fold (ret); 1205 goto out; 1206 1207 case COMPOUND_EXPR: 1208 case MODIFY_EXPR: 1209 case PREDECREMENT_EXPR: 1210 case PREINCREMENT_EXPR: 1211 case POSTDECREMENT_EXPR: 1212 case POSTINCREMENT_EXPR: 1213 case PLUS_EXPR: 1214 case MINUS_EXPR: 1215 case MULT_EXPR: 1216 case POINTER_PLUS_EXPR: 1217 case TRUNC_DIV_EXPR: 1218 case CEIL_DIV_EXPR: 1219 case FLOOR_DIV_EXPR: 1220 case TRUNC_MOD_EXPR: 1221 case RDIV_EXPR: 1222 case EXACT_DIV_EXPR: 1223 case LSHIFT_EXPR: 1224 case RSHIFT_EXPR: 1225 case BIT_IOR_EXPR: 1226 case BIT_XOR_EXPR: 1227 case BIT_AND_EXPR: 1228 case LT_EXPR: 1229 case LE_EXPR: 1230 case GT_EXPR: 1231 case GE_EXPR: 1232 case EQ_EXPR: 1233 case NE_EXPR: 1234 case COMPLEX_EXPR: 1235 case TRUTH_AND_EXPR: 1236 case TRUTH_OR_EXPR: 1237 case TRUTH_XOR_EXPR: 1238 case UNORDERED_EXPR: 1239 case ORDERED_EXPR: 1240 case UNLT_EXPR: 1241 case UNLE_EXPR: 1242 case UNGT_EXPR: 1243 case UNGE_EXPR: 1244 case UNEQ_EXPR: 1245 /* Binary operations evaluating both arguments (increment and 1246 decrement are binary internally in GCC). */ 1247 orig_op0 = op0 = TREE_OPERAND (expr, 0); 1248 orig_op1 = op1 = TREE_OPERAND (expr, 1); 1249 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, 1250 maybe_const_itself); 1251 STRIP_TYPE_NOPS (op0); 1252 if (code != MODIFY_EXPR 1253 && code != PREDECREMENT_EXPR 1254 && code != PREINCREMENT_EXPR 1255 && code != POSTDECREMENT_EXPR 1256 && code != POSTINCREMENT_EXPR) 1257 op0 = decl_constant_value_for_optimization (op0); 1258 /* The RHS of a MODIFY_EXPR was fully folded when building that 1259 expression for the sake of conversion warnings. */ 1260 if (code != MODIFY_EXPR) 1261 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands, 1262 maybe_const_itself); 1263 STRIP_TYPE_NOPS (op1); 1264 op1 = decl_constant_value_for_optimization (op1); 1265 if (op0 != orig_op0 || op1 != orig_op1 || in_init) 1266 ret = in_init 1267 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1) 1268 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1); 1269 else 1270 ret = fold (expr); 1271 if (TREE_OVERFLOW_P (ret) 1272 && !TREE_OVERFLOW_P (op0) 1273 && !TREE_OVERFLOW_P (op1)) 1274 overflow_warning (EXPR_LOCATION (expr), ret); 1275 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR) 1276 && TREE_CODE (orig_op1) != INTEGER_CST 1277 && TREE_CODE (op1) == INTEGER_CST 1278 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE 1279 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE) 1280 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE 1281 && c_inhibit_evaluation_warnings == 0) 1282 { 1283 if (tree_int_cst_sgn (op1) < 0) 1284 warning_at (loc, 0, (code == LSHIFT_EXPR 1285 ? G_("left shift count is negative") 1286 : G_("right shift count is negative"))); 1287 else if (compare_tree_int (op1, 1288 TYPE_PRECISION (TREE_TYPE (orig_op0))) 1289 >= 0) 1290 warning_at (loc, 0, (code == LSHIFT_EXPR 1291 ? G_("left shift count >= width of type") 1292 : G_("right shift count >= width of type"))); 1293 } 1294 goto out; 1295 1296 case INDIRECT_REF: 1297 case FIX_TRUNC_EXPR: 1298 case FLOAT_EXPR: 1299 CASE_CONVERT: 1300 case VIEW_CONVERT_EXPR: 1301 case NON_LVALUE_EXPR: 1302 case NEGATE_EXPR: 1303 case BIT_NOT_EXPR: 1304 case TRUTH_NOT_EXPR: 1305 case ADDR_EXPR: 1306 case CONJ_EXPR: 1307 case REALPART_EXPR: 1308 case IMAGPART_EXPR: 1309 /* Unary operations. */ 1310 orig_op0 = op0 = TREE_OPERAND (expr, 0); 1311 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, 1312 maybe_const_itself); 1313 STRIP_TYPE_NOPS (op0); 1314 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR) 1315 op0 = decl_constant_value_for_optimization (op0); 1316 /* ??? Cope with user tricks that amount to offsetof. The middle-end is 1317 not prepared to deal with them if they occur in initializers. */ 1318 if (op0 != orig_op0 1319 && code == ADDR_EXPR 1320 && (op1 = get_base_address (op0)) != NULL_TREE 1321 && TREE_CODE (op1) == INDIRECT_REF 1322 && TREE_CONSTANT (TREE_OPERAND (op1, 0))) 1323 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0)); 1324 else if (op0 != orig_op0 || in_init) 1325 ret = in_init 1326 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0) 1327 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0); 1328 else 1329 ret = fold (expr); 1330 if (code == INDIRECT_REF 1331 && ret != expr 1332 && TREE_CODE (ret) == INDIRECT_REF) 1333 { 1334 TREE_READONLY (ret) = TREE_READONLY (expr); 1335 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr); 1336 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); 1337 } 1338 switch (code) 1339 { 1340 case FIX_TRUNC_EXPR: 1341 case FLOAT_EXPR: 1342 CASE_CONVERT: 1343 /* Don't warn about explicit conversions. We will already 1344 have warned about suspect implicit conversions. */ 1345 break; 1346 1347 default: 1348 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0)) 1349 overflow_warning (EXPR_LOCATION (expr), ret); 1350 break; 1351 } 1352 goto out; 1353 1354 case TRUTH_ANDIF_EXPR: 1355 case TRUTH_ORIF_EXPR: 1356 /* Binary operations not necessarily evaluating both 1357 arguments. */ 1358 orig_op0 = op0 = TREE_OPERAND (expr, 0); 1359 orig_op1 = op1 = TREE_OPERAND (expr, 1); 1360 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self); 1361 STRIP_TYPE_NOPS (op0); 1362 1363 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR 1364 ? truthvalue_false_node 1365 : truthvalue_true_node)); 1366 c_disable_warnings (unused_p); 1367 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self); 1368 STRIP_TYPE_NOPS (op1); 1369 c_enable_warnings (unused_p); 1370 1371 if (op0 != orig_op0 || op1 != orig_op1 || in_init) 1372 ret = in_init 1373 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1) 1374 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1); 1375 else 1376 ret = fold (expr); 1377 *maybe_const_operands &= op0_const; 1378 *maybe_const_itself &= op0_const_self; 1379 if (!(flag_isoc99 1380 && op0_const 1381 && op0_const_self 1382 && (code == TRUTH_ANDIF_EXPR 1383 ? op0 == truthvalue_false_node 1384 : op0 == truthvalue_true_node))) 1385 *maybe_const_operands &= op1_const; 1386 if (!(op0_const 1387 && op0_const_self 1388 && (code == TRUTH_ANDIF_EXPR 1389 ? op0 == truthvalue_false_node 1390 : op0 == truthvalue_true_node))) 1391 *maybe_const_itself &= op1_const_self; 1392 goto out; 1393 1394 case COND_EXPR: 1395 orig_op0 = op0 = TREE_OPERAND (expr, 0); 1396 orig_op1 = op1 = TREE_OPERAND (expr, 1); 1397 orig_op2 = op2 = TREE_OPERAND (expr, 2); 1398 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self); 1399 1400 STRIP_TYPE_NOPS (op0); 1401 c_disable_warnings (op0 == truthvalue_false_node); 1402 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self); 1403 STRIP_TYPE_NOPS (op1); 1404 c_enable_warnings (op0 == truthvalue_false_node); 1405 1406 c_disable_warnings (op0 == truthvalue_true_node); 1407 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self); 1408 STRIP_TYPE_NOPS (op2); 1409 c_enable_warnings (op0 == truthvalue_true_node); 1410 1411 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2) 1412 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2); 1413 else 1414 ret = fold (expr); 1415 *maybe_const_operands &= op0_const; 1416 *maybe_const_itself &= op0_const_self; 1417 if (!(flag_isoc99 1418 && op0_const 1419 && op0_const_self 1420 && op0 == truthvalue_false_node)) 1421 *maybe_const_operands &= op1_const; 1422 if (!(op0_const 1423 && op0_const_self 1424 && op0 == truthvalue_false_node)) 1425 *maybe_const_itself &= op1_const_self; 1426 if (!(flag_isoc99 1427 && op0_const 1428 && op0_const_self 1429 && op0 == truthvalue_true_node)) 1430 *maybe_const_operands &= op2_const; 1431 if (!(op0_const 1432 && op0_const_self 1433 && op0 == truthvalue_true_node)) 1434 *maybe_const_itself &= op2_const_self; 1435 goto out; 1436 1437 case EXCESS_PRECISION_EXPR: 1438 /* Each case where an operand with excess precision may be 1439 encountered must remove the EXCESS_PRECISION_EXPR around 1440 inner operands and possibly put one around the whole 1441 expression or possibly convert to the semantic type (which 1442 c_fully_fold does); we cannot tell at this stage which is 1443 appropriate in any particular case. */ 1444 gcc_unreachable (); 1445 1446 default: 1447 /* Various codes may appear through folding built-in functions 1448 and their arguments. */ 1449 goto out; 1450 } 1451 1452 out: 1453 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks 1454 have been done by this point, so remove them again. */ 1455 nowarning |= TREE_NO_WARNING (ret); 1456 STRIP_TYPE_NOPS (ret); 1457 if (nowarning && !TREE_NO_WARNING (ret)) 1458 { 1459 if (!CAN_HAVE_LOCATION_P (ret)) 1460 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); 1461 TREE_NO_WARNING (ret) = 1; 1462 } 1463 if (ret != expr) 1464 protected_set_expr_location (ret, loc); 1465 return ret; 1466 } 1467 1468 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type, 1469 return EXP. Otherwise, return either EXP or its known constant 1470 value (if it has one), but return EXP if EXP has mode BLKmode. ??? 1471 Is the BLKmode test appropriate? */ 1472 1473 tree 1474 decl_constant_value_for_optimization (tree exp) 1475 { 1476 tree ret; 1477 1478 /* This function is only used by C, for c_fully_fold and other 1479 optimization, and may not be correct for C++. */ 1480 if (c_dialect_cxx ()) 1481 gcc_unreachable (); 1482 1483 if (!optimize 1484 || TREE_CODE (exp) != VAR_DECL 1485 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE 1486 || DECL_MODE (exp) == BLKmode) 1487 return exp; 1488 1489 ret = decl_constant_value (exp); 1490 /* Avoid unwanted tree sharing between the initializer and current 1491 function's body where the tree can be modified e.g. by the 1492 gimplifier. */ 1493 if (ret != exp && TREE_STATIC (exp)) 1494 ret = unshare_expr (ret); 1495 return ret; 1496 } 1497 1498 /* Print a warning if a constant expression had overflow in folding. 1499 Invoke this function on every expression that the language 1500 requires to be a constant expression. 1501 Note the ANSI C standard says it is erroneous for a 1502 constant expression to overflow. */ 1503 1504 void 1505 constant_expression_warning (tree value) 1506 { 1507 if (warn_overflow && pedantic 1508 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST 1509 || TREE_CODE (value) == FIXED_CST 1510 || TREE_CODE (value) == VECTOR_CST 1511 || TREE_CODE (value) == COMPLEX_CST) 1512 && TREE_OVERFLOW (value)) 1513 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression"); 1514 } 1515 1516 /* The same as above but print an unconditional error. */ 1517 void 1518 constant_expression_error (tree value) 1519 { 1520 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST 1521 || TREE_CODE (value) == FIXED_CST 1522 || TREE_CODE (value) == VECTOR_CST 1523 || TREE_CODE (value) == COMPLEX_CST) 1524 && TREE_OVERFLOW (value)) 1525 error ("overflow in constant expression"); 1526 } 1527 1528 /* Print a warning if an expression had overflow in folding and its 1529 operands hadn't. 1530 1531 Invoke this function on every expression that 1532 (1) appears in the source code, and 1533 (2) is a constant expression that overflowed, and 1534 (3) is not already checked by convert_and_check; 1535 however, do not invoke this function on operands of explicit casts 1536 or when the expression is the result of an operator and any operand 1537 already overflowed. */ 1538 1539 void 1540 overflow_warning (location_t loc, tree value) 1541 { 1542 if (c_inhibit_evaluation_warnings != 0) 1543 return; 1544 1545 switch (TREE_CODE (value)) 1546 { 1547 case INTEGER_CST: 1548 warning_at (loc, OPT_Woverflow, "integer overflow in expression"); 1549 break; 1550 1551 case REAL_CST: 1552 warning_at (loc, OPT_Woverflow, 1553 "floating point overflow in expression"); 1554 break; 1555 1556 case FIXED_CST: 1557 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression"); 1558 break; 1559 1560 case VECTOR_CST: 1561 warning_at (loc, OPT_Woverflow, "vector overflow in expression"); 1562 break; 1563 1564 case COMPLEX_CST: 1565 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST) 1566 warning_at (loc, OPT_Woverflow, 1567 "complex integer overflow in expression"); 1568 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST) 1569 warning_at (loc, OPT_Woverflow, 1570 "complex floating point overflow in expression"); 1571 break; 1572 1573 default: 1574 break; 1575 } 1576 } 1577 1578 /* Warn about uses of logical || / && operator in a context where it 1579 is likely that the bitwise equivalent was intended by the 1580 programmer. We have seen an expression in which CODE is a binary 1581 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding 1582 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */ 1583 void 1584 warn_logical_operator (location_t location, enum tree_code code, tree type, 1585 enum tree_code code_left, tree op_left, 1586 enum tree_code ARG_UNUSED (code_right), tree op_right) 1587 { 1588 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR); 1589 int in0_p, in1_p, in_p; 1590 tree low0, low1, low, high0, high1, high, lhs, rhs, tem; 1591 bool strict_overflow_p = false; 1592 1593 if (code != TRUTH_ANDIF_EXPR 1594 && code != TRUTH_AND_EXPR 1595 && code != TRUTH_ORIF_EXPR 1596 && code != TRUTH_OR_EXPR) 1597 return; 1598 1599 /* Warn if &&/|| are being used in a context where it is 1600 likely that the bitwise equivalent was intended by the 1601 programmer. That is, an expression such as op && MASK 1602 where op should not be any boolean expression, nor a 1603 constant, and mask seems to be a non-boolean integer constant. */ 1604 if (!truth_value_p (code_left) 1605 && INTEGRAL_TYPE_P (TREE_TYPE (op_left)) 1606 && !CONSTANT_CLASS_P (op_left) 1607 && !TREE_NO_WARNING (op_left) 1608 && TREE_CODE (op_right) == INTEGER_CST 1609 && !integer_zerop (op_right) 1610 && !integer_onep (op_right)) 1611 { 1612 if (or_op) 1613 warning_at (location, OPT_Wlogical_op, "logical %<or%>" 1614 " applied to non-boolean constant"); 1615 else 1616 warning_at (location, OPT_Wlogical_op, "logical %<and%>" 1617 " applied to non-boolean constant"); 1618 TREE_NO_WARNING (op_left) = true; 1619 return; 1620 } 1621 1622 /* We do not warn for constants because they are typical of macro 1623 expansions that test for features. */ 1624 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right)) 1625 return; 1626 1627 /* This warning only makes sense with logical operands. */ 1628 if (!(truth_value_p (TREE_CODE (op_left)) 1629 || INTEGRAL_TYPE_P (TREE_TYPE (op_left))) 1630 || !(truth_value_p (TREE_CODE (op_right)) 1631 || INTEGRAL_TYPE_P (TREE_TYPE (op_right)))) 1632 return; 1633 1634 1635 /* We first test whether either side separately is trivially true 1636 (with OR) or trivially false (with AND). If so, do not warn. 1637 This is a common idiom for testing ranges of data types in 1638 portable code. */ 1639 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p); 1640 if (!lhs) 1641 return; 1642 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) 1643 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs); 1644 1645 /* If this is an OR operation, invert both sides; now, the result 1646 should be always false to get a warning. */ 1647 if (or_op) 1648 in0_p = !in0_p; 1649 1650 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0); 1651 if (tem && integer_zerop (tem)) 1652 return; 1653 1654 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p); 1655 if (!rhs) 1656 return; 1657 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR) 1658 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs); 1659 1660 /* If this is an OR operation, invert both sides; now, the result 1661 should be always false to get a warning. */ 1662 if (or_op) 1663 in1_p = !in1_p; 1664 1665 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1); 1666 if (tem && integer_zerop (tem)) 1667 return; 1668 1669 /* If both expressions have the same operand, if we can merge the 1670 ranges, and if the range test is always false, then warn. */ 1671 if (operand_equal_p (lhs, rhs, 0) 1672 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, 1673 in1_p, low1, high1) 1674 && 0 != (tem = build_range_check (UNKNOWN_LOCATION, 1675 type, lhs, in_p, low, high)) 1676 && integer_zerop (tem)) 1677 { 1678 if (or_op) 1679 warning_at (location, OPT_Wlogical_op, 1680 "logical %<or%> " 1681 "of collectively exhaustive tests is always true"); 1682 else 1683 warning_at (location, OPT_Wlogical_op, 1684 "logical %<and%> " 1685 "of mutually exclusive tests is always false"); 1686 } 1687 } 1688 1689 1690 /* Warn if EXP contains any computations whose results are not used. 1691 Return true if a warning is printed; false otherwise. LOCUS is the 1692 (potential) location of the expression. */ 1693 1694 bool 1695 warn_if_unused_value (const_tree exp, location_t locus) 1696 { 1697 restart: 1698 if (TREE_USED (exp) || TREE_NO_WARNING (exp)) 1699 return false; 1700 1701 /* Don't warn about void constructs. This includes casting to void, 1702 void function calls, and statement expressions with a final cast 1703 to void. */ 1704 if (VOID_TYPE_P (TREE_TYPE (exp))) 1705 return false; 1706 1707 if (EXPR_HAS_LOCATION (exp)) 1708 locus = EXPR_LOCATION (exp); 1709 1710 switch (TREE_CODE (exp)) 1711 { 1712 case PREINCREMENT_EXPR: 1713 case POSTINCREMENT_EXPR: 1714 case PREDECREMENT_EXPR: 1715 case POSTDECREMENT_EXPR: 1716 case MODIFY_EXPR: 1717 case INIT_EXPR: 1718 case TARGET_EXPR: 1719 case CALL_EXPR: 1720 case TRY_CATCH_EXPR: 1721 case WITH_CLEANUP_EXPR: 1722 case EXIT_EXPR: 1723 case VA_ARG_EXPR: 1724 return false; 1725 1726 case BIND_EXPR: 1727 /* For a binding, warn if no side effect within it. */ 1728 exp = BIND_EXPR_BODY (exp); 1729 goto restart; 1730 1731 case SAVE_EXPR: 1732 case NON_LVALUE_EXPR: 1733 case NOP_EXPR: 1734 exp = TREE_OPERAND (exp, 0); 1735 goto restart; 1736 1737 case TRUTH_ORIF_EXPR: 1738 case TRUTH_ANDIF_EXPR: 1739 /* In && or ||, warn if 2nd operand has no side effect. */ 1740 exp = TREE_OPERAND (exp, 1); 1741 goto restart; 1742 1743 case COMPOUND_EXPR: 1744 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus)) 1745 return true; 1746 /* Let people do `(foo (), 0)' without a warning. */ 1747 if (TREE_CONSTANT (TREE_OPERAND (exp, 1))) 1748 return false; 1749 exp = TREE_OPERAND (exp, 1); 1750 goto restart; 1751 1752 case COND_EXPR: 1753 /* If this is an expression with side effects, don't warn; this 1754 case commonly appears in macro expansions. */ 1755 if (TREE_SIDE_EFFECTS (exp)) 1756 return false; 1757 goto warn; 1758 1759 case INDIRECT_REF: 1760 /* Don't warn about automatic dereferencing of references, since 1761 the user cannot control it. */ 1762 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE) 1763 { 1764 exp = TREE_OPERAND (exp, 0); 1765 goto restart; 1766 } 1767 /* Fall through. */ 1768 1769 default: 1770 /* Referencing a volatile value is a side effect, so don't warn. */ 1771 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp)) 1772 && TREE_THIS_VOLATILE (exp)) 1773 return false; 1774 1775 /* If this is an expression which has no operands, there is no value 1776 to be unused. There are no such language-independent codes, 1777 but front ends may define such. */ 1778 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0) 1779 return false; 1780 1781 warn: 1782 return warning_at (locus, OPT_Wunused_value, "value computed is not used"); 1783 } 1784 } 1785 1786 1787 /* Print a warning about casts that might indicate violation 1788 of strict aliasing rules if -Wstrict-aliasing is used and 1789 strict aliasing mode is in effect. OTYPE is the original 1790 TREE_TYPE of EXPR, and TYPE the type we're casting to. */ 1791 1792 bool 1793 strict_aliasing_warning (tree otype, tree type, tree expr) 1794 { 1795 /* Strip pointer conversion chains and get to the correct original type. */ 1796 STRIP_NOPS (expr); 1797 otype = TREE_TYPE (expr); 1798 1799 if (!(flag_strict_aliasing 1800 && POINTER_TYPE_P (type) 1801 && POINTER_TYPE_P (otype) 1802 && !VOID_TYPE_P (TREE_TYPE (type))) 1803 /* If the type we are casting to is a ref-all pointer 1804 dereferencing it is always valid. */ 1805 || TYPE_REF_CAN_ALIAS_ALL (type)) 1806 return false; 1807 1808 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR 1809 && (DECL_P (TREE_OPERAND (expr, 0)) 1810 || handled_component_p (TREE_OPERAND (expr, 0)))) 1811 { 1812 /* Casting the address of an object to non void pointer. Warn 1813 if the cast breaks type based aliasing. */ 1814 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2) 1815 { 1816 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type " 1817 "might break strict-aliasing rules"); 1818 return true; 1819 } 1820 else 1821 { 1822 /* warn_strict_aliasing >= 3. This includes the default (3). 1823 Only warn if the cast is dereferenced immediately. */ 1824 alias_set_type set1 = 1825 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); 1826 alias_set_type set2 = get_alias_set (TREE_TYPE (type)); 1827 1828 if (set1 != set2 && set2 != 0 1829 && (set1 == 0 || !alias_sets_conflict_p (set1, set2))) 1830 { 1831 warning (OPT_Wstrict_aliasing, "dereferencing type-punned " 1832 "pointer will break strict-aliasing rules"); 1833 return true; 1834 } 1835 else if (warn_strict_aliasing == 2 1836 && !alias_sets_must_conflict_p (set1, set2)) 1837 { 1838 warning (OPT_Wstrict_aliasing, "dereferencing type-punned " 1839 "pointer might break strict-aliasing rules"); 1840 return true; 1841 } 1842 } 1843 } 1844 else 1845 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype))) 1846 { 1847 /* At this level, warn for any conversions, even if an address is 1848 not taken in the same statement. This will likely produce many 1849 false positives, but could be useful to pinpoint problems that 1850 are not revealed at higher levels. */ 1851 alias_set_type set1 = get_alias_set (TREE_TYPE (otype)); 1852 alias_set_type set2 = get_alias_set (TREE_TYPE (type)); 1853 if (!COMPLETE_TYPE_P (type) 1854 || !alias_sets_must_conflict_p (set1, set2)) 1855 { 1856 warning (OPT_Wstrict_aliasing, "dereferencing type-punned " 1857 "pointer might break strict-aliasing rules"); 1858 return true; 1859 } 1860 } 1861 1862 return false; 1863 } 1864 1865 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with 1866 sizeof as last operand of certain builtins. */ 1867 1868 void 1869 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee, 1870 vec<tree, va_gc> *params, tree *sizeof_arg, 1871 bool (*comp_types) (tree, tree)) 1872 { 1873 tree type, dest = NULL_TREE, src = NULL_TREE, tem; 1874 bool strop = false, cmp = false; 1875 unsigned int idx = ~0; 1876 location_t loc; 1877 1878 if (TREE_CODE (callee) != FUNCTION_DECL 1879 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL 1880 || vec_safe_length (params) <= 1) 1881 return; 1882 1883 switch (DECL_FUNCTION_CODE (callee)) 1884 { 1885 case BUILT_IN_STRNCMP: 1886 case BUILT_IN_STRNCASECMP: 1887 cmp = true; 1888 /* FALLTHRU */ 1889 case BUILT_IN_STRNCPY: 1890 case BUILT_IN_STRNCPY_CHK: 1891 case BUILT_IN_STRNCAT: 1892 case BUILT_IN_STRNCAT_CHK: 1893 case BUILT_IN_STPNCPY: 1894 case BUILT_IN_STPNCPY_CHK: 1895 strop = true; 1896 /* FALLTHRU */ 1897 case BUILT_IN_MEMCPY: 1898 case BUILT_IN_MEMCPY_CHK: 1899 case BUILT_IN_MEMMOVE: 1900 case BUILT_IN_MEMMOVE_CHK: 1901 if (params->length () < 3) 1902 return; 1903 src = (*params)[1]; 1904 dest = (*params)[0]; 1905 idx = 2; 1906 break; 1907 case BUILT_IN_BCOPY: 1908 if (params->length () < 3) 1909 return; 1910 src = (*params)[0]; 1911 dest = (*params)[1]; 1912 idx = 2; 1913 break; 1914 case BUILT_IN_MEMCMP: 1915 case BUILT_IN_BCMP: 1916 if (params->length () < 3) 1917 return; 1918 src = (*params)[1]; 1919 dest = (*params)[0]; 1920 idx = 2; 1921 cmp = true; 1922 break; 1923 case BUILT_IN_MEMSET: 1924 case BUILT_IN_MEMSET_CHK: 1925 if (params->length () < 3) 1926 return; 1927 dest = (*params)[0]; 1928 idx = 2; 1929 break; 1930 case BUILT_IN_BZERO: 1931 dest = (*params)[0]; 1932 idx = 1; 1933 break; 1934 case BUILT_IN_STRNDUP: 1935 src = (*params)[0]; 1936 strop = true; 1937 idx = 1; 1938 break; 1939 case BUILT_IN_MEMCHR: 1940 if (params->length () < 3) 1941 return; 1942 src = (*params)[0]; 1943 idx = 2; 1944 break; 1945 case BUILT_IN_SNPRINTF: 1946 case BUILT_IN_SNPRINTF_CHK: 1947 case BUILT_IN_VSNPRINTF: 1948 case BUILT_IN_VSNPRINTF_CHK: 1949 dest = (*params)[0]; 1950 idx = 1; 1951 strop = true; 1952 break; 1953 default: 1954 break; 1955 } 1956 1957 if (idx >= 3) 1958 return; 1959 1960 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node) 1961 return; 1962 1963 type = TYPE_P (sizeof_arg[idx]) 1964 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]); 1965 if (!POINTER_TYPE_P (type)) 1966 return; 1967 1968 if (dest 1969 && (tem = tree_strip_nop_conversions (dest)) 1970 && POINTER_TYPE_P (TREE_TYPE (tem)) 1971 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) 1972 return; 1973 1974 if (src 1975 && (tem = tree_strip_nop_conversions (src)) 1976 && POINTER_TYPE_P (TREE_TYPE (tem)) 1977 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) 1978 return; 1979 1980 loc = sizeof_arg_loc[idx]; 1981 1982 if (dest && !cmp) 1983 { 1984 if (!TYPE_P (sizeof_arg[idx]) 1985 && operand_equal_p (dest, sizeof_arg[idx], 0) 1986 && comp_types (TREE_TYPE (dest), type)) 1987 { 1988 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) 1989 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 1990 "argument to %<sizeof%> in %qD call is the same " 1991 "expression as the destination; did you mean to " 1992 "remove the addressof?", callee); 1993 else if ((TYPE_PRECISION (TREE_TYPE (type)) 1994 == TYPE_PRECISION (char_type_node)) 1995 || strop) 1996 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 1997 "argument to %<sizeof%> in %qD call is the same " 1998 "expression as the destination; did you mean to " 1999 "provide an explicit length?", callee); 2000 else 2001 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 2002 "argument to %<sizeof%> in %qD call is the same " 2003 "expression as the destination; did you mean to " 2004 "dereference it?", callee); 2005 return; 2006 } 2007 2008 if (POINTER_TYPE_P (TREE_TYPE (dest)) 2009 && !strop 2010 && comp_types (TREE_TYPE (dest), type) 2011 && !VOID_TYPE_P (TREE_TYPE (type))) 2012 { 2013 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 2014 "argument to %<sizeof%> in %qD call is the same " 2015 "pointer type %qT as the destination; expected %qT " 2016 "or an explicit length", callee, TREE_TYPE (dest), 2017 TREE_TYPE (TREE_TYPE (dest))); 2018 return; 2019 } 2020 } 2021 2022 if (src && !cmp) 2023 { 2024 if (!TYPE_P (sizeof_arg[idx]) 2025 && operand_equal_p (src, sizeof_arg[idx], 0) 2026 && comp_types (TREE_TYPE (src), type)) 2027 { 2028 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) 2029 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 2030 "argument to %<sizeof%> in %qD call is the same " 2031 "expression as the source; did you mean to " 2032 "remove the addressof?", callee); 2033 else if ((TYPE_PRECISION (TREE_TYPE (type)) 2034 == TYPE_PRECISION (char_type_node)) 2035 || strop) 2036 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 2037 "argument to %<sizeof%> in %qD call is the same " 2038 "expression as the source; did you mean to " 2039 "provide an explicit length?", callee); 2040 else 2041 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 2042 "argument to %<sizeof%> in %qD call is the same " 2043 "expression as the source; did you mean to " 2044 "dereference it?", callee); 2045 return; 2046 } 2047 2048 if (POINTER_TYPE_P (TREE_TYPE (src)) 2049 && !strop 2050 && comp_types (TREE_TYPE (src), type) 2051 && !VOID_TYPE_P (TREE_TYPE (type))) 2052 { 2053 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 2054 "argument to %<sizeof%> in %qD call is the same " 2055 "pointer type %qT as the source; expected %qT " 2056 "or an explicit length", callee, TREE_TYPE (src), 2057 TREE_TYPE (TREE_TYPE (src))); 2058 return; 2059 } 2060 } 2061 2062 if (dest) 2063 { 2064 if (!TYPE_P (sizeof_arg[idx]) 2065 && operand_equal_p (dest, sizeof_arg[idx], 0) 2066 && comp_types (TREE_TYPE (dest), type)) 2067 { 2068 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) 2069 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 2070 "argument to %<sizeof%> in %qD call is the same " 2071 "expression as the first source; did you mean to " 2072 "remove the addressof?", callee); 2073 else if ((TYPE_PRECISION (TREE_TYPE (type)) 2074 == TYPE_PRECISION (char_type_node)) 2075 || strop) 2076 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 2077 "argument to %<sizeof%> in %qD call is the same " 2078 "expression as the first source; did you mean to " 2079 "provide an explicit length?", callee); 2080 else 2081 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 2082 "argument to %<sizeof%> in %qD call is the same " 2083 "expression as the first source; did you mean to " 2084 "dereference it?", callee); 2085 return; 2086 } 2087 2088 if (POINTER_TYPE_P (TREE_TYPE (dest)) 2089 && !strop 2090 && comp_types (TREE_TYPE (dest), type) 2091 && !VOID_TYPE_P (TREE_TYPE (type))) 2092 { 2093 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 2094 "argument to %<sizeof%> in %qD call is the same " 2095 "pointer type %qT as the first source; expected %qT " 2096 "or an explicit length", callee, TREE_TYPE (dest), 2097 TREE_TYPE (TREE_TYPE (dest))); 2098 return; 2099 } 2100 } 2101 2102 if (src) 2103 { 2104 if (!TYPE_P (sizeof_arg[idx]) 2105 && operand_equal_p (src, sizeof_arg[idx], 0) 2106 && comp_types (TREE_TYPE (src), type)) 2107 { 2108 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) 2109 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 2110 "argument to %<sizeof%> in %qD call is the same " 2111 "expression as the second source; did you mean to " 2112 "remove the addressof?", callee); 2113 else if ((TYPE_PRECISION (TREE_TYPE (type)) 2114 == TYPE_PRECISION (char_type_node)) 2115 || strop) 2116 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 2117 "argument to %<sizeof%> in %qD call is the same " 2118 "expression as the second source; did you mean to " 2119 "provide an explicit length?", callee); 2120 else 2121 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 2122 "argument to %<sizeof%> in %qD call is the same " 2123 "expression as the second source; did you mean to " 2124 "dereference it?", callee); 2125 return; 2126 } 2127 2128 if (POINTER_TYPE_P (TREE_TYPE (src)) 2129 && !strop 2130 && comp_types (TREE_TYPE (src), type) 2131 && !VOID_TYPE_P (TREE_TYPE (type))) 2132 { 2133 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 2134 "argument to %<sizeof%> in %qD call is the same " 2135 "pointer type %qT as the second source; expected %qT " 2136 "or an explicit length", callee, TREE_TYPE (src), 2137 TREE_TYPE (TREE_TYPE (src))); 2138 return; 2139 } 2140 } 2141 2142 } 2143 2144 /* Warn for unlikely, improbable, or stupid DECL declarations 2145 of `main'. */ 2146 2147 void 2148 check_main_parameter_types (tree decl) 2149 { 2150 function_args_iterator iter; 2151 tree type; 2152 int argct = 0; 2153 2154 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter) 2155 { 2156 /* XXX void_type_node belies the abstraction. */ 2157 if (type == void_type_node || type == error_mark_node ) 2158 break; 2159 2160 ++argct; 2161 switch (argct) 2162 { 2163 case 1: 2164 if (TYPE_MAIN_VARIANT (type) != integer_type_node) 2165 pedwarn (input_location, OPT_Wmain, 2166 "first argument of %q+D should be %<int%>", decl); 2167 break; 2168 2169 case 2: 2170 if (TREE_CODE (type) != POINTER_TYPE 2171 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 2172 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 2173 != char_type_node)) 2174 pedwarn (input_location, OPT_Wmain, 2175 "second argument of %q+D should be %<char **%>", decl); 2176 break; 2177 2178 case 3: 2179 if (TREE_CODE (type) != POINTER_TYPE 2180 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 2181 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 2182 != char_type_node)) 2183 pedwarn (input_location, OPT_Wmain, 2184 "third argument of %q+D should probably be " 2185 "%<char **%>", decl); 2186 break; 2187 } 2188 } 2189 2190 /* It is intentional that this message does not mention the third 2191 argument because it's only mentioned in an appendix of the 2192 standard. */ 2193 if (argct > 0 && (argct < 2 || argct > 3)) 2194 pedwarn (input_location, OPT_Wmain, 2195 "%q+D takes only zero or two arguments", decl); 2196 } 2197 2198 /* True if pointers to distinct types T1 and T2 can be converted to 2199 each other without an explicit cast. Only returns true for opaque 2200 vector types. */ 2201 bool 2202 vector_targets_convertible_p (const_tree t1, const_tree t2) 2203 { 2204 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE 2205 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2)) 2206 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) 2207 return true; 2208 2209 return false; 2210 } 2211 2212 /* True if vector types T1 and T2 can be converted to each other 2213 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2 2214 can only be converted with -flax-vector-conversions yet that is not 2215 in effect, emit a note telling the user about that option if such 2216 a note has not previously been emitted. */ 2217 bool 2218 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note) 2219 { 2220 static bool emitted_lax_note = false; 2221 bool convertible_lax; 2222 2223 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2)) 2224 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) 2225 return true; 2226 2227 convertible_lax = 2228 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)) 2229 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE || 2230 TYPE_PRECISION (t1) == TYPE_PRECISION (t2)) 2231 && (INTEGRAL_TYPE_P (TREE_TYPE (t1)) 2232 == INTEGRAL_TYPE_P (TREE_TYPE (t2)))); 2233 2234 if (!convertible_lax || flag_lax_vector_conversions) 2235 return convertible_lax; 2236 2237 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) 2238 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))) 2239 return true; 2240 2241 if (emit_lax_note && !emitted_lax_note) 2242 { 2243 emitted_lax_note = true; 2244 inform (input_location, "use -flax-vector-conversions to permit " 2245 "conversions between vectors with differing " 2246 "element types or numbers of subparts"); 2247 } 2248 2249 return false; 2250 } 2251 2252 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes 2253 and have vector types, V0 has the same type as V1, and the number of 2254 elements of V0, V1, MASK is the same. 2255 2256 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was 2257 called with two arguments. In this case implementation passes the 2258 first argument twice in order to share the same tree code. This fact 2259 could enable the mask-values being twice the vector length. This is 2260 an implementation accident and this semantics is not guaranteed to 2261 the user. */ 2262 tree 2263 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask) 2264 { 2265 tree ret; 2266 bool wrap = true; 2267 bool maybe_const = false; 2268 bool two_arguments = false; 2269 2270 if (v1 == NULL_TREE) 2271 { 2272 two_arguments = true; 2273 v1 = v0; 2274 } 2275 2276 if (v0 == error_mark_node || v1 == error_mark_node 2277 || mask == error_mark_node) 2278 return error_mark_node; 2279 2280 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE 2281 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE) 2282 { 2283 error_at (loc, "__builtin_shuffle last argument must " 2284 "be an integer vector"); 2285 return error_mark_node; 2286 } 2287 2288 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE 2289 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE) 2290 { 2291 error_at (loc, "__builtin_shuffle arguments must be vectors"); 2292 return error_mark_node; 2293 } 2294 2295 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1))) 2296 { 2297 error_at (loc, "__builtin_shuffle argument vectors must be of " 2298 "the same type"); 2299 return error_mark_node; 2300 } 2301 2302 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)) 2303 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)) 2304 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)) 2305 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))) 2306 { 2307 error_at (loc, "__builtin_shuffle number of elements of the " 2308 "argument vector(s) and the mask vector should " 2309 "be the same"); 2310 return error_mark_node; 2311 } 2312 2313 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0)))) 2314 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask))))) 2315 { 2316 error_at (loc, "__builtin_shuffle argument vector(s) inner type " 2317 "must have the same size as inner type of the mask"); 2318 return error_mark_node; 2319 } 2320 2321 if (!c_dialect_cxx ()) 2322 { 2323 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */ 2324 v0 = c_fully_fold (v0, false, &maybe_const); 2325 wrap &= maybe_const; 2326 2327 if (two_arguments) 2328 v1 = v0 = save_expr (v0); 2329 else 2330 { 2331 v1 = c_fully_fold (v1, false, &maybe_const); 2332 wrap &= maybe_const; 2333 } 2334 2335 mask = c_fully_fold (mask, false, &maybe_const); 2336 wrap &= maybe_const; 2337 } 2338 2339 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask); 2340 2341 if (!c_dialect_cxx () && !wrap) 2342 ret = c_wrap_maybe_const (ret, true); 2343 2344 return ret; 2345 } 2346 2347 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum 2348 to integral type. */ 2349 2350 static tree 2351 c_common_get_narrower (tree op, int *unsignedp_ptr) 2352 { 2353 op = get_narrower (op, unsignedp_ptr); 2354 2355 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE 2356 && ENUM_IS_SCOPED (TREE_TYPE (op))) 2357 { 2358 /* C++0x scoped enumerations don't implicitly convert to integral 2359 type; if we stripped an explicit conversion to a larger type we 2360 need to replace it so common_type will still work. */ 2361 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)), 2362 TYPE_UNSIGNED (TREE_TYPE (op))); 2363 op = fold_convert (type, op); 2364 } 2365 return op; 2366 } 2367 2368 /* This is a helper function of build_binary_op. 2369 2370 For certain operations if both args were extended from the same 2371 smaller type, do the arithmetic in that type and then extend. 2372 2373 BITWISE indicates a bitwise operation. 2374 For them, this optimization is safe only if 2375 both args are zero-extended or both are sign-extended. 2376 Otherwise, we might change the result. 2377 Eg, (short)-1 | (unsigned short)-1 is (int)-1 2378 but calculated in (unsigned short) it would be (unsigned short)-1. 2379 */ 2380 tree 2381 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise) 2382 { 2383 int unsigned0, unsigned1; 2384 tree arg0, arg1; 2385 int uns; 2386 tree type; 2387 2388 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents 2389 excessive narrowing when we call get_narrower below. For 2390 example, suppose that OP0 is of unsigned int extended 2391 from signed char and that RESULT_TYPE is long long int. 2392 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look 2393 like 2394 2395 (long long int) (unsigned int) signed_char 2396 2397 which get_narrower would narrow down to 2398 2399 (unsigned int) signed char 2400 2401 If we do not cast OP0 first, get_narrower would return 2402 signed_char, which is inconsistent with the case of the 2403 explicit cast. */ 2404 op0 = convert (result_type, op0); 2405 op1 = convert (result_type, op1); 2406 2407 arg0 = c_common_get_narrower (op0, &unsigned0); 2408 arg1 = c_common_get_narrower (op1, &unsigned1); 2409 2410 /* UNS is 1 if the operation to be done is an unsigned one. */ 2411 uns = TYPE_UNSIGNED (result_type); 2412 2413 /* Handle the case that OP0 (or OP1) does not *contain* a conversion 2414 but it *requires* conversion to FINAL_TYPE. */ 2415 2416 if ((TYPE_PRECISION (TREE_TYPE (op0)) 2417 == TYPE_PRECISION (TREE_TYPE (arg0))) 2418 && TREE_TYPE (op0) != result_type) 2419 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); 2420 if ((TYPE_PRECISION (TREE_TYPE (op1)) 2421 == TYPE_PRECISION (TREE_TYPE (arg1))) 2422 && TREE_TYPE (op1) != result_type) 2423 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); 2424 2425 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */ 2426 2427 /* For bitwise operations, signedness of nominal type 2428 does not matter. Consider only how operands were extended. */ 2429 if (bitwise) 2430 uns = unsigned0; 2431 2432 /* Note that in all three cases below we refrain from optimizing 2433 an unsigned operation on sign-extended args. 2434 That would not be valid. */ 2435 2436 /* Both args variable: if both extended in same way 2437 from same width, do it in that width. 2438 Do it unsigned if args were zero-extended. */ 2439 if ((TYPE_PRECISION (TREE_TYPE (arg0)) 2440 < TYPE_PRECISION (result_type)) 2441 && (TYPE_PRECISION (TREE_TYPE (arg1)) 2442 == TYPE_PRECISION (TREE_TYPE (arg0))) 2443 && unsigned0 == unsigned1 2444 && (unsigned0 || !uns)) 2445 return c_common_signed_or_unsigned_type 2446 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1))); 2447 2448 else if (TREE_CODE (arg0) == INTEGER_CST 2449 && (unsigned1 || !uns) 2450 && (TYPE_PRECISION (TREE_TYPE (arg1)) 2451 < TYPE_PRECISION (result_type)) 2452 && (type 2453 = c_common_signed_or_unsigned_type (unsigned1, 2454 TREE_TYPE (arg1))) 2455 && !POINTER_TYPE_P (type) 2456 && int_fits_type_p (arg0, type)) 2457 return type; 2458 2459 else if (TREE_CODE (arg1) == INTEGER_CST 2460 && (unsigned0 || !uns) 2461 && (TYPE_PRECISION (TREE_TYPE (arg0)) 2462 < TYPE_PRECISION (result_type)) 2463 && (type 2464 = c_common_signed_or_unsigned_type (unsigned0, 2465 TREE_TYPE (arg0))) 2466 && !POINTER_TYPE_P (type) 2467 && int_fits_type_p (arg1, type)) 2468 return type; 2469 2470 return result_type; 2471 } 2472 2473 /* Checks if expression EXPR of real/integer type cannot be converted 2474 to the real/integer type TYPE. Function returns true when: 2475 * EXPR is a constant which cannot be exactly converted to TYPE 2476 * EXPR is not a constant and size of EXPR's type > than size of TYPE, 2477 for EXPR type and TYPE being both integers or both real. 2478 * EXPR is not a constant of real type and TYPE is an integer. 2479 * EXPR is not a constant of integer type which cannot be 2480 exactly converted to real type. 2481 Function allows conversions between types of different signedness and 2482 does not return true in that case. Function can produce signedness 2483 warnings if PRODUCE_WARNS is true. */ 2484 bool 2485 unsafe_conversion_p (tree type, tree expr, bool produce_warns) 2486 { 2487 bool give_warning = false; 2488 tree expr_type = TREE_TYPE (expr); 2489 location_t loc = EXPR_LOC_OR_HERE (expr); 2490 2491 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST) 2492 { 2493 /* Warn for real constant that is not an exact integer converted 2494 to integer type. */ 2495 if (TREE_CODE (expr_type) == REAL_TYPE 2496 && TREE_CODE (type) == INTEGER_TYPE) 2497 { 2498 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type))) 2499 give_warning = true; 2500 } 2501 /* Warn for an integer constant that does not fit into integer type. */ 2502 else if (TREE_CODE (expr_type) == INTEGER_TYPE 2503 && TREE_CODE (type) == INTEGER_TYPE 2504 && !int_fits_type_p (expr, type)) 2505 { 2506 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type) 2507 && tree_int_cst_sgn (expr) < 0) 2508 { 2509 if (produce_warns) 2510 warning_at (loc, OPT_Wsign_conversion, "negative integer" 2511 " implicitly converted to unsigned type"); 2512 } 2513 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type)) 2514 { 2515 if (produce_warns) 2516 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned" 2517 " constant value to negative integer"); 2518 } 2519 else 2520 give_warning = true; 2521 } 2522 else if (TREE_CODE (type) == REAL_TYPE) 2523 { 2524 /* Warn for an integer constant that does not fit into real type. */ 2525 if (TREE_CODE (expr_type) == INTEGER_TYPE) 2526 { 2527 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr); 2528 if (!exact_real_truncate (TYPE_MODE (type), &a)) 2529 give_warning = true; 2530 } 2531 /* Warn for a real constant that does not fit into a smaller 2532 real type. */ 2533 else if (TREE_CODE (expr_type) == REAL_TYPE 2534 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) 2535 { 2536 REAL_VALUE_TYPE a = TREE_REAL_CST (expr); 2537 if (!exact_real_truncate (TYPE_MODE (type), &a)) 2538 give_warning = true; 2539 } 2540 } 2541 } 2542 else 2543 { 2544 /* Warn for real types converted to integer types. */ 2545 if (TREE_CODE (expr_type) == REAL_TYPE 2546 && TREE_CODE (type) == INTEGER_TYPE) 2547 give_warning = true; 2548 2549 else if (TREE_CODE (expr_type) == INTEGER_TYPE 2550 && TREE_CODE (type) == INTEGER_TYPE) 2551 { 2552 /* Don't warn about unsigned char y = 0xff, x = (int) y; */ 2553 expr = get_unwidened (expr, 0); 2554 expr_type = TREE_TYPE (expr); 2555 2556 /* Don't warn for short y; short x = ((int)y & 0xff); */ 2557 if (TREE_CODE (expr) == BIT_AND_EXPR 2558 || TREE_CODE (expr) == BIT_IOR_EXPR 2559 || TREE_CODE (expr) == BIT_XOR_EXPR) 2560 { 2561 /* If both args were extended from a shortest type, 2562 use that type if that is safe. */ 2563 expr_type = shorten_binary_op (expr_type, 2564 TREE_OPERAND (expr, 0), 2565 TREE_OPERAND (expr, 1), 2566 /* bitwise */1); 2567 2568 if (TREE_CODE (expr) == BIT_AND_EXPR) 2569 { 2570 tree op0 = TREE_OPERAND (expr, 0); 2571 tree op1 = TREE_OPERAND (expr, 1); 2572 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); 2573 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); 2574 2575 /* If one of the operands is a non-negative constant 2576 that fits in the target type, then the type of the 2577 other operand does not matter. */ 2578 if ((TREE_CODE (op0) == INTEGER_CST 2579 && int_fits_type_p (op0, c_common_signed_type (type)) 2580 && int_fits_type_p (op0, c_common_unsigned_type (type))) 2581 || (TREE_CODE (op1) == INTEGER_CST 2582 && int_fits_type_p (op1, c_common_signed_type (type)) 2583 && int_fits_type_p (op1, 2584 c_common_unsigned_type (type)))) 2585 return false; 2586 /* If constant is unsigned and fits in the target 2587 type, then the result will also fit. */ 2588 else if ((TREE_CODE (op0) == INTEGER_CST 2589 && unsigned0 2590 && int_fits_type_p (op0, type)) 2591 || (TREE_CODE (op1) == INTEGER_CST 2592 && unsigned1 2593 && int_fits_type_p (op1, type))) 2594 return false; 2595 } 2596 } 2597 /* Warn for integer types converted to smaller integer types. */ 2598 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) 2599 give_warning = true; 2600 2601 /* When they are the same width but different signedness, 2602 then the value may change. */ 2603 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type) 2604 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type)) 2605 /* Even when converted to a bigger type, if the type is 2606 unsigned but expr is signed, then negative values 2607 will be changed. */ 2608 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type))) 2609 && produce_warns) 2610 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT " 2611 "may change the sign of the result", 2612 type, expr_type); 2613 } 2614 2615 /* Warn for integer types converted to real types if and only if 2616 all the range of values of the integer type cannot be 2617 represented by the real type. */ 2618 else if (TREE_CODE (expr_type) == INTEGER_TYPE 2619 && TREE_CODE (type) == REAL_TYPE) 2620 { 2621 tree type_low_bound, type_high_bound; 2622 REAL_VALUE_TYPE real_low_bound, real_high_bound; 2623 2624 /* Don't warn about char y = 0xff; float x = (int) y; */ 2625 expr = get_unwidened (expr, 0); 2626 expr_type = TREE_TYPE (expr); 2627 2628 type_low_bound = TYPE_MIN_VALUE (expr_type); 2629 type_high_bound = TYPE_MAX_VALUE (expr_type); 2630 real_low_bound = real_value_from_int_cst (0, type_low_bound); 2631 real_high_bound = real_value_from_int_cst (0, type_high_bound); 2632 2633 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound) 2634 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound)) 2635 give_warning = true; 2636 } 2637 2638 /* Warn for real types converted to smaller real types. */ 2639 else if (TREE_CODE (expr_type) == REAL_TYPE 2640 && TREE_CODE (type) == REAL_TYPE 2641 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) 2642 give_warning = true; 2643 } 2644 2645 return give_warning; 2646 } 2647 2648 /* Warns if the conversion of EXPR to TYPE may alter a value. 2649 This is a helper function for warnings_for_convert_and_check. */ 2650 2651 static void 2652 conversion_warning (tree type, tree expr) 2653 { 2654 tree expr_type = TREE_TYPE (expr); 2655 location_t loc = EXPR_LOC_OR_HERE (expr); 2656 2657 if (!warn_conversion && !warn_sign_conversion) 2658 return; 2659 2660 switch (TREE_CODE (expr)) 2661 { 2662 case EQ_EXPR: 2663 case NE_EXPR: 2664 case LE_EXPR: 2665 case GE_EXPR: 2666 case LT_EXPR: 2667 case GT_EXPR: 2668 case TRUTH_ANDIF_EXPR: 2669 case TRUTH_ORIF_EXPR: 2670 case TRUTH_AND_EXPR: 2671 case TRUTH_OR_EXPR: 2672 case TRUTH_XOR_EXPR: 2673 case TRUTH_NOT_EXPR: 2674 /* Conversion from boolean to a signed:1 bit-field (which only 2675 can hold the values 0 and -1) doesn't lose information - but 2676 it does change the value. */ 2677 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type)) 2678 warning_at (loc, OPT_Wconversion, 2679 "conversion to %qT from boolean expression", type); 2680 return; 2681 2682 case REAL_CST: 2683 case INTEGER_CST: 2684 if (unsafe_conversion_p (type, expr, true)) 2685 warning_at (loc, OPT_Wconversion, 2686 "conversion to %qT alters %qT constant value", 2687 type, expr_type); 2688 return; 2689 2690 case COND_EXPR: 2691 { 2692 /* In case of COND_EXPR, we do not care about the type of 2693 COND_EXPR, only about the conversion of each operand. */ 2694 tree op1 = TREE_OPERAND (expr, 1); 2695 tree op2 = TREE_OPERAND (expr, 2); 2696 2697 conversion_warning (type, op1); 2698 conversion_warning (type, op2); 2699 return; 2700 } 2701 2702 default: /* 'expr' is not a constant. */ 2703 if (unsafe_conversion_p (type, expr, true)) 2704 warning_at (loc, OPT_Wconversion, 2705 "conversion to %qT from %qT may alter its value", 2706 type, expr_type); 2707 } 2708 } 2709 2710 /* Produce warnings after a conversion. RESULT is the result of 2711 converting EXPR to TYPE. This is a helper function for 2712 convert_and_check and cp_convert_and_check. */ 2713 2714 void 2715 warnings_for_convert_and_check (tree type, tree expr, tree result) 2716 { 2717 location_t loc = EXPR_LOC_OR_HERE (expr); 2718 2719 if (TREE_CODE (expr) == INTEGER_CST 2720 && (TREE_CODE (type) == INTEGER_TYPE 2721 || TREE_CODE (type) == ENUMERAL_TYPE) 2722 && !int_fits_type_p (expr, type)) 2723 { 2724 /* Do not diagnose overflow in a constant expression merely 2725 because a conversion overflowed. */ 2726 if (TREE_OVERFLOW (result)) 2727 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr); 2728 2729 if (TYPE_UNSIGNED (type)) 2730 { 2731 /* This detects cases like converting -129 or 256 to 2732 unsigned char. */ 2733 if (!int_fits_type_p (expr, c_common_signed_type (type))) 2734 warning_at (loc, OPT_Woverflow, 2735 "large integer implicitly truncated to unsigned type"); 2736 else 2737 conversion_warning (type, expr); 2738 } 2739 else if (!int_fits_type_p (expr, c_common_unsigned_type (type))) 2740 warning (OPT_Woverflow, 2741 "overflow in implicit constant conversion"); 2742 /* No warning for converting 0x80000000 to int. */ 2743 else if (pedantic 2744 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE 2745 || TYPE_PRECISION (TREE_TYPE (expr)) 2746 != TYPE_PRECISION (type))) 2747 warning_at (loc, OPT_Woverflow, 2748 "overflow in implicit constant conversion"); 2749 2750 else 2751 conversion_warning (type, expr); 2752 } 2753 else if ((TREE_CODE (result) == INTEGER_CST 2754 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result)) 2755 warning_at (loc, OPT_Woverflow, 2756 "overflow in implicit constant conversion"); 2757 else 2758 conversion_warning (type, expr); 2759 } 2760 2761 2762 /* Convert EXPR to TYPE, warning about conversion problems with constants. 2763 Invoke this function on every expression that is converted implicitly, 2764 i.e. because of language rules and not because of an explicit cast. */ 2765 2766 tree 2767 convert_and_check (tree type, tree expr) 2768 { 2769 tree result; 2770 tree expr_for_warning; 2771 2772 /* Convert from a value with possible excess precision rather than 2773 via the semantic type, but do not warn about values not fitting 2774 exactly in the semantic type. */ 2775 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) 2776 { 2777 tree orig_type = TREE_TYPE (expr); 2778 expr = TREE_OPERAND (expr, 0); 2779 expr_for_warning = convert (orig_type, expr); 2780 if (orig_type == type) 2781 return expr_for_warning; 2782 } 2783 else 2784 expr_for_warning = expr; 2785 2786 if (TREE_TYPE (expr) == type) 2787 return expr; 2788 2789 result = convert (type, expr); 2790 2791 if (c_inhibit_evaluation_warnings == 0 2792 && !TREE_OVERFLOW_P (expr) 2793 && result != error_mark_node) 2794 warnings_for_convert_and_check (type, expr_for_warning, result); 2795 2796 return result; 2797 } 2798 2799 /* A node in a list that describes references to variables (EXPR), which are 2800 either read accesses if WRITER is zero, or write accesses, in which case 2801 WRITER is the parent of EXPR. */ 2802 struct tlist 2803 { 2804 struct tlist *next; 2805 tree expr, writer; 2806 }; 2807 2808 /* Used to implement a cache the results of a call to verify_tree. We only 2809 use this for SAVE_EXPRs. */ 2810 struct tlist_cache 2811 { 2812 struct tlist_cache *next; 2813 struct tlist *cache_before_sp; 2814 struct tlist *cache_after_sp; 2815 tree expr; 2816 }; 2817 2818 /* Obstack to use when allocating tlist structures, and corresponding 2819 firstobj. */ 2820 static struct obstack tlist_obstack; 2821 static char *tlist_firstobj = 0; 2822 2823 /* Keep track of the identifiers we've warned about, so we can avoid duplicate 2824 warnings. */ 2825 static struct tlist *warned_ids; 2826 /* SAVE_EXPRs need special treatment. We process them only once and then 2827 cache the results. */ 2828 static struct tlist_cache *save_expr_cache; 2829 2830 static void add_tlist (struct tlist **, struct tlist *, tree, int); 2831 static void merge_tlist (struct tlist **, struct tlist *, int); 2832 static void verify_tree (tree, struct tlist **, struct tlist **, tree); 2833 static int warning_candidate_p (tree); 2834 static bool candidate_equal_p (const_tree, const_tree); 2835 static void warn_for_collisions (struct tlist *); 2836 static void warn_for_collisions_1 (tree, tree, struct tlist *, int); 2837 static struct tlist *new_tlist (struct tlist *, tree, tree); 2838 2839 /* Create a new struct tlist and fill in its fields. */ 2840 static struct tlist * 2841 new_tlist (struct tlist *next, tree t, tree writer) 2842 { 2843 struct tlist *l; 2844 l = XOBNEW (&tlist_obstack, struct tlist); 2845 l->next = next; 2846 l->expr = t; 2847 l->writer = writer; 2848 return l; 2849 } 2850 2851 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER 2852 is nonnull, we ignore any node we find which has a writer equal to it. */ 2853 2854 static void 2855 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy) 2856 { 2857 while (add) 2858 { 2859 struct tlist *next = add->next; 2860 if (!copy) 2861 add->next = *to; 2862 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer)) 2863 *to = copy ? new_tlist (*to, add->expr, add->writer) : add; 2864 add = next; 2865 } 2866 } 2867 2868 /* Merge the nodes of ADD into TO. This merging process is done so that for 2869 each variable that already exists in TO, no new node is added; however if 2870 there is a write access recorded in ADD, and an occurrence on TO is only 2871 a read access, then the occurrence in TO will be modified to record the 2872 write. */ 2873 2874 static void 2875 merge_tlist (struct tlist **to, struct tlist *add, int copy) 2876 { 2877 struct tlist **end = to; 2878 2879 while (*end) 2880 end = &(*end)->next; 2881 2882 while (add) 2883 { 2884 int found = 0; 2885 struct tlist *tmp2; 2886 struct tlist *next = add->next; 2887 2888 for (tmp2 = *to; tmp2; tmp2 = tmp2->next) 2889 if (candidate_equal_p (tmp2->expr, add->expr)) 2890 { 2891 found = 1; 2892 if (!tmp2->writer) 2893 tmp2->writer = add->writer; 2894 } 2895 if (!found) 2896 { 2897 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add; 2898 end = &(*end)->next; 2899 *end = 0; 2900 } 2901 add = next; 2902 } 2903 } 2904 2905 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable 2906 references in list LIST conflict with it, excluding reads if ONLY writers 2907 is nonzero. */ 2908 2909 static void 2910 warn_for_collisions_1 (tree written, tree writer, struct tlist *list, 2911 int only_writes) 2912 { 2913 struct tlist *tmp; 2914 2915 /* Avoid duplicate warnings. */ 2916 for (tmp = warned_ids; tmp; tmp = tmp->next) 2917 if (candidate_equal_p (tmp->expr, written)) 2918 return; 2919 2920 while (list) 2921 { 2922 if (candidate_equal_p (list->expr, written) 2923 && !candidate_equal_p (list->writer, writer) 2924 && (!only_writes || list->writer)) 2925 { 2926 warned_ids = new_tlist (warned_ids, written, NULL_TREE); 2927 warning_at (EXPR_LOC_OR_HERE (writer), 2928 OPT_Wsequence_point, "operation on %qE may be undefined", 2929 list->expr); 2930 } 2931 list = list->next; 2932 } 2933 } 2934 2935 /* Given a list LIST of references to variables, find whether any of these 2936 can cause conflicts due to missing sequence points. */ 2937 2938 static void 2939 warn_for_collisions (struct tlist *list) 2940 { 2941 struct tlist *tmp; 2942 2943 for (tmp = list; tmp; tmp = tmp->next) 2944 { 2945 if (tmp->writer) 2946 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0); 2947 } 2948 } 2949 2950 /* Return nonzero if X is a tree that can be verified by the sequence point 2951 warnings. */ 2952 static int 2953 warning_candidate_p (tree x) 2954 { 2955 if (DECL_P (x) && DECL_ARTIFICIAL (x)) 2956 return 0; 2957 2958 if (TREE_CODE (x) == BLOCK) 2959 return 0; 2960 2961 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c 2962 (lvalue_p) crash on TRY/CATCH. */ 2963 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x))) 2964 return 0; 2965 2966 if (!lvalue_p (x)) 2967 return 0; 2968 2969 /* No point to track non-const calls, they will never satisfy 2970 operand_equal_p. */ 2971 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0) 2972 return 0; 2973 2974 if (TREE_CODE (x) == STRING_CST) 2975 return 0; 2976 2977 return 1; 2978 } 2979 2980 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */ 2981 static bool 2982 candidate_equal_p (const_tree x, const_tree y) 2983 { 2984 return (x == y) || (x && y && operand_equal_p (x, y, 0)); 2985 } 2986 2987 /* Walk the tree X, and record accesses to variables. If X is written by the 2988 parent tree, WRITER is the parent. 2989 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this 2990 expression or its only operand forces a sequence point, then everything up 2991 to the sequence point is stored in PBEFORE_SP. Everything else gets stored 2992 in PNO_SP. 2993 Once we return, we will have emitted warnings if any subexpression before 2994 such a sequence point could be undefined. On a higher level, however, the 2995 sequence point may not be relevant, and we'll merge the two lists. 2996 2997 Example: (b++, a) + b; 2998 The call that processes the COMPOUND_EXPR will store the increment of B 2999 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that 3000 processes the PLUS_EXPR will need to merge the two lists so that 3001 eventually, all accesses end up on the same list (and we'll warn about the 3002 unordered subexpressions b++ and b. 3003 3004 A note on merging. If we modify the former example so that our expression 3005 becomes 3006 (b++, b) + a 3007 care must be taken not simply to add all three expressions into the final 3008 PNO_SP list. The function merge_tlist takes care of that by merging the 3009 before-SP list of the COMPOUND_EXPR into its after-SP list in a special 3010 way, so that no more than one access to B is recorded. */ 3011 3012 static void 3013 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp, 3014 tree writer) 3015 { 3016 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3; 3017 enum tree_code code; 3018 enum tree_code_class cl; 3019 3020 /* X may be NULL if it is the operand of an empty statement expression 3021 ({ }). */ 3022 if (x == NULL) 3023 return; 3024 3025 restart: 3026 code = TREE_CODE (x); 3027 cl = TREE_CODE_CLASS (code); 3028 3029 if (warning_candidate_p (x)) 3030 *pno_sp = new_tlist (*pno_sp, x, writer); 3031 3032 switch (code) 3033 { 3034 case CONSTRUCTOR: 3035 case SIZEOF_EXPR: 3036 return; 3037 3038 case COMPOUND_EXPR: 3039 case TRUTH_ANDIF_EXPR: 3040 case TRUTH_ORIF_EXPR: 3041 tmp_before = tmp_nosp = tmp_list3 = 0; 3042 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); 3043 warn_for_collisions (tmp_nosp); 3044 merge_tlist (pbefore_sp, tmp_before, 0); 3045 merge_tlist (pbefore_sp, tmp_nosp, 0); 3046 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE); 3047 merge_tlist (pbefore_sp, tmp_list3, 0); 3048 return; 3049 3050 case COND_EXPR: 3051 tmp_before = tmp_list2 = 0; 3052 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE); 3053 warn_for_collisions (tmp_list2); 3054 merge_tlist (pbefore_sp, tmp_before, 0); 3055 merge_tlist (pbefore_sp, tmp_list2, 0); 3056 3057 tmp_list3 = tmp_nosp = 0; 3058 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE); 3059 warn_for_collisions (tmp_nosp); 3060 merge_tlist (pbefore_sp, tmp_list3, 0); 3061 3062 tmp_list3 = tmp_list2 = 0; 3063 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE); 3064 warn_for_collisions (tmp_list2); 3065 merge_tlist (pbefore_sp, tmp_list3, 0); 3066 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the 3067 two first, to avoid warning for (a ? b++ : b++). */ 3068 merge_tlist (&tmp_nosp, tmp_list2, 0); 3069 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); 3070 return; 3071 3072 case PREDECREMENT_EXPR: 3073 case PREINCREMENT_EXPR: 3074 case POSTDECREMENT_EXPR: 3075 case POSTINCREMENT_EXPR: 3076 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x); 3077 return; 3078 3079 case MODIFY_EXPR: 3080 tmp_before = tmp_nosp = tmp_list3 = 0; 3081 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE); 3082 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x); 3083 /* Expressions inside the LHS are not ordered wrt. the sequence points 3084 in the RHS. Example: 3085 *a = (a++, 2) 3086 Despite the fact that the modification of "a" is in the before_sp 3087 list (tmp_before), it conflicts with the use of "a" in the LHS. 3088 We can handle this by adding the contents of tmp_list3 3089 to those of tmp_before, and redoing the collision warnings for that 3090 list. */ 3091 add_tlist (&tmp_before, tmp_list3, x, 1); 3092 warn_for_collisions (tmp_before); 3093 /* Exclude the LHS itself here; we first have to merge it into the 3094 tmp_nosp list. This is done to avoid warning for "a = a"; if we 3095 didn't exclude the LHS, we'd get it twice, once as a read and once 3096 as a write. */ 3097 add_tlist (pno_sp, tmp_list3, x, 0); 3098 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1); 3099 3100 merge_tlist (pbefore_sp, tmp_before, 0); 3101 if (warning_candidate_p (TREE_OPERAND (x, 0))) 3102 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0); 3103 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1); 3104 return; 3105 3106 case CALL_EXPR: 3107 /* We need to warn about conflicts among arguments and conflicts between 3108 args and the function address. Side effects of the function address, 3109 however, are not ordered by the sequence point of the call. */ 3110 { 3111 call_expr_arg_iterator iter; 3112 tree arg; 3113 tmp_before = tmp_nosp = 0; 3114 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE); 3115 FOR_EACH_CALL_EXPR_ARG (arg, iter, x) 3116 { 3117 tmp_list2 = tmp_list3 = 0; 3118 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE); 3119 merge_tlist (&tmp_list3, tmp_list2, 0); 3120 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0); 3121 } 3122 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0); 3123 warn_for_collisions (tmp_before); 3124 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0); 3125 return; 3126 } 3127 3128 case TREE_LIST: 3129 /* Scan all the list, e.g. indices of multi dimensional array. */ 3130 while (x) 3131 { 3132 tmp_before = tmp_nosp = 0; 3133 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE); 3134 merge_tlist (&tmp_nosp, tmp_before, 0); 3135 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); 3136 x = TREE_CHAIN (x); 3137 } 3138 return; 3139 3140 case SAVE_EXPR: 3141 { 3142 struct tlist_cache *t; 3143 for (t = save_expr_cache; t; t = t->next) 3144 if (candidate_equal_p (t->expr, x)) 3145 break; 3146 3147 if (!t) 3148 { 3149 t = XOBNEW (&tlist_obstack, struct tlist_cache); 3150 t->next = save_expr_cache; 3151 t->expr = x; 3152 save_expr_cache = t; 3153 3154 tmp_before = tmp_nosp = 0; 3155 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); 3156 warn_for_collisions (tmp_nosp); 3157 3158 tmp_list3 = 0; 3159 merge_tlist (&tmp_list3, tmp_nosp, 0); 3160 t->cache_before_sp = tmp_before; 3161 t->cache_after_sp = tmp_list3; 3162 } 3163 merge_tlist (pbefore_sp, t->cache_before_sp, 1); 3164 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1); 3165 return; 3166 } 3167 3168 case ADDR_EXPR: 3169 x = TREE_OPERAND (x, 0); 3170 if (DECL_P (x)) 3171 return; 3172 writer = 0; 3173 goto restart; 3174 3175 default: 3176 /* For other expressions, simply recurse on their operands. 3177 Manual tail recursion for unary expressions. 3178 Other non-expressions need not be processed. */ 3179 if (cl == tcc_unary) 3180 { 3181 x = TREE_OPERAND (x, 0); 3182 writer = 0; 3183 goto restart; 3184 } 3185 else if (IS_EXPR_CODE_CLASS (cl)) 3186 { 3187 int lp; 3188 int max = TREE_OPERAND_LENGTH (x); 3189 for (lp = 0; lp < max; lp++) 3190 { 3191 tmp_before = tmp_nosp = 0; 3192 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0); 3193 merge_tlist (&tmp_nosp, tmp_before, 0); 3194 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); 3195 } 3196 } 3197 return; 3198 } 3199 } 3200 3201 /* Try to warn for undefined behavior in EXPR due to missing sequence 3202 points. */ 3203 3204 DEBUG_FUNCTION void 3205 verify_sequence_points (tree expr) 3206 { 3207 struct tlist *before_sp = 0, *after_sp = 0; 3208 3209 warned_ids = 0; 3210 save_expr_cache = 0; 3211 if (tlist_firstobj == 0) 3212 { 3213 gcc_obstack_init (&tlist_obstack); 3214 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0); 3215 } 3216 3217 verify_tree (expr, &before_sp, &after_sp, 0); 3218 warn_for_collisions (after_sp); 3219 obstack_free (&tlist_obstack, tlist_firstobj); 3220 } 3221 3222 /* Validate the expression after `case' and apply default promotions. */ 3223 3224 static tree 3225 check_case_value (tree value) 3226 { 3227 if (value == NULL_TREE) 3228 return value; 3229 3230 if (TREE_CODE (value) == INTEGER_CST) 3231 /* Promote char or short to int. */ 3232 value = perform_integral_promotions (value); 3233 else if (value != error_mark_node) 3234 { 3235 error ("case label does not reduce to an integer constant"); 3236 value = error_mark_node; 3237 } 3238 3239 constant_expression_warning (value); 3240 3241 return value; 3242 } 3243 3244 /* See if the case values LOW and HIGH are in the range of the original 3245 type (i.e. before the default conversion to int) of the switch testing 3246 expression. 3247 TYPE is the promoted type of the testing expression, and ORIG_TYPE is 3248 the type before promoting it. CASE_LOW_P is a pointer to the lower 3249 bound of the case label, and CASE_HIGH_P is the upper bound or NULL 3250 if the case is not a case range. 3251 The caller has to make sure that we are not called with NULL for 3252 CASE_LOW_P (i.e. the default case). 3253 Returns true if the case label is in range of ORIG_TYPE (saturated or 3254 untouched) or false if the label is out of range. */ 3255 3256 static bool 3257 check_case_bounds (tree type, tree orig_type, 3258 tree *case_low_p, tree *case_high_p) 3259 { 3260 tree min_value, max_value; 3261 tree case_low = *case_low_p; 3262 tree case_high = case_high_p ? *case_high_p : case_low; 3263 3264 /* If there was a problem with the original type, do nothing. */ 3265 if (orig_type == error_mark_node) 3266 return true; 3267 3268 min_value = TYPE_MIN_VALUE (orig_type); 3269 max_value = TYPE_MAX_VALUE (orig_type); 3270 3271 /* Case label is less than minimum for type. */ 3272 if (tree_int_cst_compare (case_low, min_value) < 0 3273 && tree_int_cst_compare (case_high, min_value) < 0) 3274 { 3275 warning (0, "case label value is less than minimum value for type"); 3276 return false; 3277 } 3278 3279 /* Case value is greater than maximum for type. */ 3280 if (tree_int_cst_compare (case_low, max_value) > 0 3281 && tree_int_cst_compare (case_high, max_value) > 0) 3282 { 3283 warning (0, "case label value exceeds maximum value for type"); 3284 return false; 3285 } 3286 3287 /* Saturate lower case label value to minimum. */ 3288 if (tree_int_cst_compare (case_high, min_value) >= 0 3289 && tree_int_cst_compare (case_low, min_value) < 0) 3290 { 3291 warning (0, "lower value in case label range" 3292 " less than minimum value for type"); 3293 case_low = min_value; 3294 } 3295 3296 /* Saturate upper case label value to maximum. */ 3297 if (tree_int_cst_compare (case_low, max_value) <= 0 3298 && tree_int_cst_compare (case_high, max_value) > 0) 3299 { 3300 warning (0, "upper value in case label range" 3301 " exceeds maximum value for type"); 3302 case_high = max_value; 3303 } 3304 3305 if (*case_low_p != case_low) 3306 *case_low_p = convert (type, case_low); 3307 if (case_high_p && *case_high_p != case_high) 3308 *case_high_p = convert (type, case_high); 3309 3310 return true; 3311 } 3312 3313 /* Return an integer type with BITS bits of precision, 3314 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */ 3315 3316 tree 3317 c_common_type_for_size (unsigned int bits, int unsignedp) 3318 { 3319 if (bits == TYPE_PRECISION (integer_type_node)) 3320 return unsignedp ? unsigned_type_node : integer_type_node; 3321 3322 if (bits == TYPE_PRECISION (signed_char_type_node)) 3323 return unsignedp ? unsigned_char_type_node : signed_char_type_node; 3324 3325 if (bits == TYPE_PRECISION (short_integer_type_node)) 3326 return unsignedp ? short_unsigned_type_node : short_integer_type_node; 3327 3328 if (bits == TYPE_PRECISION (long_integer_type_node)) 3329 return unsignedp ? long_unsigned_type_node : long_integer_type_node; 3330 3331 if (bits == TYPE_PRECISION (long_long_integer_type_node)) 3332 return (unsignedp ? long_long_unsigned_type_node 3333 : long_long_integer_type_node); 3334 3335 if (int128_integer_type_node 3336 && bits == TYPE_PRECISION (int128_integer_type_node)) 3337 return (unsignedp ? int128_unsigned_type_node 3338 : int128_integer_type_node); 3339 3340 if (bits == TYPE_PRECISION (widest_integer_literal_type_node)) 3341 return (unsignedp ? widest_unsigned_literal_type_node 3342 : widest_integer_literal_type_node); 3343 3344 if (bits <= TYPE_PRECISION (intQI_type_node)) 3345 return unsignedp ? unsigned_intQI_type_node : intQI_type_node; 3346 3347 if (bits <= TYPE_PRECISION (intHI_type_node)) 3348 return unsignedp ? unsigned_intHI_type_node : intHI_type_node; 3349 3350 if (bits <= TYPE_PRECISION (intSI_type_node)) 3351 return unsignedp ? unsigned_intSI_type_node : intSI_type_node; 3352 3353 if (bits <= TYPE_PRECISION (intDI_type_node)) 3354 return unsignedp ? unsigned_intDI_type_node : intDI_type_node; 3355 3356 return 0; 3357 } 3358 3359 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits 3360 that is unsigned if UNSIGNEDP is nonzero, otherwise signed; 3361 and saturating if SATP is nonzero, otherwise not saturating. */ 3362 3363 tree 3364 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit, 3365 int unsignedp, int satp) 3366 { 3367 enum machine_mode mode; 3368 if (ibit == 0) 3369 mode = unsignedp ? UQQmode : QQmode; 3370 else 3371 mode = unsignedp ? UHAmode : HAmode; 3372 3373 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode)) 3374 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit) 3375 break; 3376 3377 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode)) 3378 { 3379 sorry ("GCC cannot support operators with integer types and " 3380 "fixed-point types that have too many integral and " 3381 "fractional bits together"); 3382 return 0; 3383 } 3384 3385 return c_common_type_for_mode (mode, satp); 3386 } 3387 3388 /* Used for communication between c_common_type_for_mode and 3389 c_register_builtin_type. */ 3390 static GTY(()) tree registered_builtin_types; 3391 3392 /* Return a data type that has machine mode MODE. 3393 If the mode is an integer, 3394 then UNSIGNEDP selects between signed and unsigned types. 3395 If the mode is a fixed-point mode, 3396 then UNSIGNEDP selects between saturating and nonsaturating types. */ 3397 3398 tree 3399 c_common_type_for_mode (enum machine_mode mode, int unsignedp) 3400 { 3401 tree t; 3402 3403 if (mode == TYPE_MODE (integer_type_node)) 3404 return unsignedp ? unsigned_type_node : integer_type_node; 3405 3406 if (mode == TYPE_MODE (signed_char_type_node)) 3407 return unsignedp ? unsigned_char_type_node : signed_char_type_node; 3408 3409 if (mode == TYPE_MODE (short_integer_type_node)) 3410 return unsignedp ? short_unsigned_type_node : short_integer_type_node; 3411 3412 if (mode == TYPE_MODE (long_integer_type_node)) 3413 return unsignedp ? long_unsigned_type_node : long_integer_type_node; 3414 3415 if (mode == TYPE_MODE (long_long_integer_type_node)) 3416 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; 3417 3418 if (int128_integer_type_node 3419 && mode == TYPE_MODE (int128_integer_type_node)) 3420 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node; 3421 3422 if (mode == TYPE_MODE (widest_integer_literal_type_node)) 3423 return unsignedp ? widest_unsigned_literal_type_node 3424 : widest_integer_literal_type_node; 3425 3426 if (mode == QImode) 3427 return unsignedp ? unsigned_intQI_type_node : intQI_type_node; 3428 3429 if (mode == HImode) 3430 return unsignedp ? unsigned_intHI_type_node : intHI_type_node; 3431 3432 if (mode == SImode) 3433 return unsignedp ? unsigned_intSI_type_node : intSI_type_node; 3434 3435 if (mode == DImode) 3436 return unsignedp ? unsigned_intDI_type_node : intDI_type_node; 3437 3438 #if HOST_BITS_PER_WIDE_INT >= 64 3439 if (mode == TYPE_MODE (intTI_type_node)) 3440 return unsignedp ? unsigned_intTI_type_node : intTI_type_node; 3441 #endif 3442 3443 if (mode == TYPE_MODE (float_type_node)) 3444 return float_type_node; 3445 3446 if (mode == TYPE_MODE (double_type_node)) 3447 return double_type_node; 3448 3449 if (mode == TYPE_MODE (long_double_type_node)) 3450 return long_double_type_node; 3451 3452 if (mode == TYPE_MODE (void_type_node)) 3453 return void_type_node; 3454 3455 if (mode == TYPE_MODE (build_pointer_type (char_type_node))) 3456 return (unsignedp 3457 ? make_unsigned_type (GET_MODE_PRECISION (mode)) 3458 : make_signed_type (GET_MODE_PRECISION (mode))); 3459 3460 if (mode == TYPE_MODE (build_pointer_type (integer_type_node))) 3461 return (unsignedp 3462 ? make_unsigned_type (GET_MODE_PRECISION (mode)) 3463 : make_signed_type (GET_MODE_PRECISION (mode))); 3464 3465 if (COMPLEX_MODE_P (mode)) 3466 { 3467 enum machine_mode inner_mode; 3468 tree inner_type; 3469 3470 if (mode == TYPE_MODE (complex_float_type_node)) 3471 return complex_float_type_node; 3472 if (mode == TYPE_MODE (complex_double_type_node)) 3473 return complex_double_type_node; 3474 if (mode == TYPE_MODE (complex_long_double_type_node)) 3475 return complex_long_double_type_node; 3476 3477 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp) 3478 return complex_integer_type_node; 3479 3480 inner_mode = GET_MODE_INNER (mode); 3481 inner_type = c_common_type_for_mode (inner_mode, unsignedp); 3482 if (inner_type != NULL_TREE) 3483 return build_complex_type (inner_type); 3484 } 3485 else if (VECTOR_MODE_P (mode)) 3486 { 3487 enum machine_mode inner_mode = GET_MODE_INNER (mode); 3488 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp); 3489 if (inner_type != NULL_TREE) 3490 return build_vector_type_for_mode (inner_type, mode); 3491 } 3492 3493 if (mode == TYPE_MODE (dfloat32_type_node)) 3494 return dfloat32_type_node; 3495 if (mode == TYPE_MODE (dfloat64_type_node)) 3496 return dfloat64_type_node; 3497 if (mode == TYPE_MODE (dfloat128_type_node)) 3498 return dfloat128_type_node; 3499 3500 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode)) 3501 { 3502 if (mode == TYPE_MODE (short_fract_type_node)) 3503 return unsignedp ? sat_short_fract_type_node : short_fract_type_node; 3504 if (mode == TYPE_MODE (fract_type_node)) 3505 return unsignedp ? sat_fract_type_node : fract_type_node; 3506 if (mode == TYPE_MODE (long_fract_type_node)) 3507 return unsignedp ? sat_long_fract_type_node : long_fract_type_node; 3508 if (mode == TYPE_MODE (long_long_fract_type_node)) 3509 return unsignedp ? sat_long_long_fract_type_node 3510 : long_long_fract_type_node; 3511 3512 if (mode == TYPE_MODE (unsigned_short_fract_type_node)) 3513 return unsignedp ? sat_unsigned_short_fract_type_node 3514 : unsigned_short_fract_type_node; 3515 if (mode == TYPE_MODE (unsigned_fract_type_node)) 3516 return unsignedp ? sat_unsigned_fract_type_node 3517 : unsigned_fract_type_node; 3518 if (mode == TYPE_MODE (unsigned_long_fract_type_node)) 3519 return unsignedp ? sat_unsigned_long_fract_type_node 3520 : unsigned_long_fract_type_node; 3521 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node)) 3522 return unsignedp ? sat_unsigned_long_long_fract_type_node 3523 : unsigned_long_long_fract_type_node; 3524 3525 if (mode == TYPE_MODE (short_accum_type_node)) 3526 return unsignedp ? sat_short_accum_type_node : short_accum_type_node; 3527 if (mode == TYPE_MODE (accum_type_node)) 3528 return unsignedp ? sat_accum_type_node : accum_type_node; 3529 if (mode == TYPE_MODE (long_accum_type_node)) 3530 return unsignedp ? sat_long_accum_type_node : long_accum_type_node; 3531 if (mode == TYPE_MODE (long_long_accum_type_node)) 3532 return unsignedp ? sat_long_long_accum_type_node 3533 : long_long_accum_type_node; 3534 3535 if (mode == TYPE_MODE (unsigned_short_accum_type_node)) 3536 return unsignedp ? sat_unsigned_short_accum_type_node 3537 : unsigned_short_accum_type_node; 3538 if (mode == TYPE_MODE (unsigned_accum_type_node)) 3539 return unsignedp ? sat_unsigned_accum_type_node 3540 : unsigned_accum_type_node; 3541 if (mode == TYPE_MODE (unsigned_long_accum_type_node)) 3542 return unsignedp ? sat_unsigned_long_accum_type_node 3543 : unsigned_long_accum_type_node; 3544 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node)) 3545 return unsignedp ? sat_unsigned_long_long_accum_type_node 3546 : unsigned_long_long_accum_type_node; 3547 3548 if (mode == QQmode) 3549 return unsignedp ? sat_qq_type_node : qq_type_node; 3550 if (mode == HQmode) 3551 return unsignedp ? sat_hq_type_node : hq_type_node; 3552 if (mode == SQmode) 3553 return unsignedp ? sat_sq_type_node : sq_type_node; 3554 if (mode == DQmode) 3555 return unsignedp ? sat_dq_type_node : dq_type_node; 3556 if (mode == TQmode) 3557 return unsignedp ? sat_tq_type_node : tq_type_node; 3558 3559 if (mode == UQQmode) 3560 return unsignedp ? sat_uqq_type_node : uqq_type_node; 3561 if (mode == UHQmode) 3562 return unsignedp ? sat_uhq_type_node : uhq_type_node; 3563 if (mode == USQmode) 3564 return unsignedp ? sat_usq_type_node : usq_type_node; 3565 if (mode == UDQmode) 3566 return unsignedp ? sat_udq_type_node : udq_type_node; 3567 if (mode == UTQmode) 3568 return unsignedp ? sat_utq_type_node : utq_type_node; 3569 3570 if (mode == HAmode) 3571 return unsignedp ? sat_ha_type_node : ha_type_node; 3572 if (mode == SAmode) 3573 return unsignedp ? sat_sa_type_node : sa_type_node; 3574 if (mode == DAmode) 3575 return unsignedp ? sat_da_type_node : da_type_node; 3576 if (mode == TAmode) 3577 return unsignedp ? sat_ta_type_node : ta_type_node; 3578 3579 if (mode == UHAmode) 3580 return unsignedp ? sat_uha_type_node : uha_type_node; 3581 if (mode == USAmode) 3582 return unsignedp ? sat_usa_type_node : usa_type_node; 3583 if (mode == UDAmode) 3584 return unsignedp ? sat_uda_type_node : uda_type_node; 3585 if (mode == UTAmode) 3586 return unsignedp ? sat_uta_type_node : uta_type_node; 3587 } 3588 3589 for (t = registered_builtin_types; t; t = TREE_CHAIN (t)) 3590 if (TYPE_MODE (TREE_VALUE (t)) == mode 3591 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t))) 3592 return TREE_VALUE (t); 3593 3594 return 0; 3595 } 3596 3597 tree 3598 c_common_unsigned_type (tree type) 3599 { 3600 return c_common_signed_or_unsigned_type (1, type); 3601 } 3602 3603 /* Return a signed type the same as TYPE in other respects. */ 3604 3605 tree 3606 c_common_signed_type (tree type) 3607 { 3608 return c_common_signed_or_unsigned_type (0, type); 3609 } 3610 3611 /* Return a type the same as TYPE except unsigned or 3612 signed according to UNSIGNEDP. */ 3613 3614 tree 3615 c_common_signed_or_unsigned_type (int unsignedp, tree type) 3616 { 3617 tree type1; 3618 3619 /* This block of code emulates the behavior of the old 3620 c_common_unsigned_type. In particular, it returns 3621 long_unsigned_type_node if passed a long, even when a int would 3622 have the same size. This is necessary for warnings to work 3623 correctly in archs where sizeof(int) == sizeof(long) */ 3624 3625 type1 = TYPE_MAIN_VARIANT (type); 3626 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node) 3627 return unsignedp ? unsigned_char_type_node : signed_char_type_node; 3628 if (type1 == integer_type_node || type1 == unsigned_type_node) 3629 return unsignedp ? unsigned_type_node : integer_type_node; 3630 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node) 3631 return unsignedp ? short_unsigned_type_node : short_integer_type_node; 3632 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node) 3633 return unsignedp ? long_unsigned_type_node : long_integer_type_node; 3634 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node) 3635 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; 3636 if (int128_integer_type_node 3637 && (type1 == int128_integer_type_node 3638 || type1 == int128_unsigned_type_node)) 3639 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node; 3640 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node) 3641 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node; 3642 #if HOST_BITS_PER_WIDE_INT >= 64 3643 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node) 3644 return unsignedp ? unsigned_intTI_type_node : intTI_type_node; 3645 #endif 3646 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node) 3647 return unsignedp ? unsigned_intDI_type_node : intDI_type_node; 3648 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node) 3649 return unsignedp ? unsigned_intSI_type_node : intSI_type_node; 3650 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node) 3651 return unsignedp ? unsigned_intHI_type_node : intHI_type_node; 3652 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node) 3653 return unsignedp ? unsigned_intQI_type_node : intQI_type_node; 3654 3655 #define C_COMMON_FIXED_TYPES(NAME) \ 3656 if (type1 == short_ ## NAME ## _type_node \ 3657 || type1 == unsigned_short_ ## NAME ## _type_node) \ 3658 return unsignedp ? unsigned_short_ ## NAME ## _type_node \ 3659 : short_ ## NAME ## _type_node; \ 3660 if (type1 == NAME ## _type_node \ 3661 || type1 == unsigned_ ## NAME ## _type_node) \ 3662 return unsignedp ? unsigned_ ## NAME ## _type_node \ 3663 : NAME ## _type_node; \ 3664 if (type1 == long_ ## NAME ## _type_node \ 3665 || type1 == unsigned_long_ ## NAME ## _type_node) \ 3666 return unsignedp ? unsigned_long_ ## NAME ## _type_node \ 3667 : long_ ## NAME ## _type_node; \ 3668 if (type1 == long_long_ ## NAME ## _type_node \ 3669 || type1 == unsigned_long_long_ ## NAME ## _type_node) \ 3670 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \ 3671 : long_long_ ## NAME ## _type_node; 3672 3673 #define C_COMMON_FIXED_MODE_TYPES(NAME) \ 3674 if (type1 == NAME ## _type_node \ 3675 || type1 == u ## NAME ## _type_node) \ 3676 return unsignedp ? u ## NAME ## _type_node \ 3677 : NAME ## _type_node; 3678 3679 #define C_COMMON_FIXED_TYPES_SAT(NAME) \ 3680 if (type1 == sat_ ## short_ ## NAME ## _type_node \ 3681 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \ 3682 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \ 3683 : sat_ ## short_ ## NAME ## _type_node; \ 3684 if (type1 == sat_ ## NAME ## _type_node \ 3685 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \ 3686 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \ 3687 : sat_ ## NAME ## _type_node; \ 3688 if (type1 == sat_ ## long_ ## NAME ## _type_node \ 3689 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \ 3690 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \ 3691 : sat_ ## long_ ## NAME ## _type_node; \ 3692 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \ 3693 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \ 3694 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \ 3695 : sat_ ## long_long_ ## NAME ## _type_node; 3696 3697 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \ 3698 if (type1 == sat_ ## NAME ## _type_node \ 3699 || type1 == sat_ ## u ## NAME ## _type_node) \ 3700 return unsignedp ? sat_ ## u ## NAME ## _type_node \ 3701 : sat_ ## NAME ## _type_node; 3702 3703 C_COMMON_FIXED_TYPES (fract); 3704 C_COMMON_FIXED_TYPES_SAT (fract); 3705 C_COMMON_FIXED_TYPES (accum); 3706 C_COMMON_FIXED_TYPES_SAT (accum); 3707 3708 C_COMMON_FIXED_MODE_TYPES (qq); 3709 C_COMMON_FIXED_MODE_TYPES (hq); 3710 C_COMMON_FIXED_MODE_TYPES (sq); 3711 C_COMMON_FIXED_MODE_TYPES (dq); 3712 C_COMMON_FIXED_MODE_TYPES (tq); 3713 C_COMMON_FIXED_MODE_TYPES_SAT (qq); 3714 C_COMMON_FIXED_MODE_TYPES_SAT (hq); 3715 C_COMMON_FIXED_MODE_TYPES_SAT (sq); 3716 C_COMMON_FIXED_MODE_TYPES_SAT (dq); 3717 C_COMMON_FIXED_MODE_TYPES_SAT (tq); 3718 C_COMMON_FIXED_MODE_TYPES (ha); 3719 C_COMMON_FIXED_MODE_TYPES (sa); 3720 C_COMMON_FIXED_MODE_TYPES (da); 3721 C_COMMON_FIXED_MODE_TYPES (ta); 3722 C_COMMON_FIXED_MODE_TYPES_SAT (ha); 3723 C_COMMON_FIXED_MODE_TYPES_SAT (sa); 3724 C_COMMON_FIXED_MODE_TYPES_SAT (da); 3725 C_COMMON_FIXED_MODE_TYPES_SAT (ta); 3726 3727 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not 3728 the precision; they have precision set to match their range, but 3729 may use a wider mode to match an ABI. If we change modes, we may 3730 wind up with bad conversions. For INTEGER_TYPEs in C, must check 3731 the precision as well, so as to yield correct results for 3732 bit-field types. C++ does not have these separate bit-field 3733 types, and producing a signed or unsigned variant of an 3734 ENUMERAL_TYPE may cause other problems as well. */ 3735 3736 if (!INTEGRAL_TYPE_P (type) 3737 || TYPE_UNSIGNED (type) == unsignedp) 3738 return type; 3739 3740 #define TYPE_OK(node) \ 3741 (TYPE_MODE (type) == TYPE_MODE (node) \ 3742 && TYPE_PRECISION (type) == TYPE_PRECISION (node)) 3743 if (TYPE_OK (signed_char_type_node)) 3744 return unsignedp ? unsigned_char_type_node : signed_char_type_node; 3745 if (TYPE_OK (integer_type_node)) 3746 return unsignedp ? unsigned_type_node : integer_type_node; 3747 if (TYPE_OK (short_integer_type_node)) 3748 return unsignedp ? short_unsigned_type_node : short_integer_type_node; 3749 if (TYPE_OK (long_integer_type_node)) 3750 return unsignedp ? long_unsigned_type_node : long_integer_type_node; 3751 if (TYPE_OK (long_long_integer_type_node)) 3752 return (unsignedp ? long_long_unsigned_type_node 3753 : long_long_integer_type_node); 3754 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node)) 3755 return (unsignedp ? int128_unsigned_type_node 3756 : int128_integer_type_node); 3757 if (TYPE_OK (widest_integer_literal_type_node)) 3758 return (unsignedp ? widest_unsigned_literal_type_node 3759 : widest_integer_literal_type_node); 3760 3761 #if HOST_BITS_PER_WIDE_INT >= 64 3762 if (TYPE_OK (intTI_type_node)) 3763 return unsignedp ? unsigned_intTI_type_node : intTI_type_node; 3764 #endif 3765 if (TYPE_OK (intDI_type_node)) 3766 return unsignedp ? unsigned_intDI_type_node : intDI_type_node; 3767 if (TYPE_OK (intSI_type_node)) 3768 return unsignedp ? unsigned_intSI_type_node : intSI_type_node; 3769 if (TYPE_OK (intHI_type_node)) 3770 return unsignedp ? unsigned_intHI_type_node : intHI_type_node; 3771 if (TYPE_OK (intQI_type_node)) 3772 return unsignedp ? unsigned_intQI_type_node : intQI_type_node; 3773 #undef TYPE_OK 3774 3775 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp); 3776 } 3777 3778 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */ 3779 3780 tree 3781 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp) 3782 { 3783 /* Extended integer types of the same width as a standard type have 3784 lesser rank, so those of the same width as int promote to int or 3785 unsigned int and are valid for printf formats expecting int or 3786 unsigned int. To avoid such special cases, avoid creating 3787 extended integer types for bit-fields if a standard integer type 3788 is available. */ 3789 if (width == TYPE_PRECISION (integer_type_node)) 3790 return unsignedp ? unsigned_type_node : integer_type_node; 3791 if (width == TYPE_PRECISION (signed_char_type_node)) 3792 return unsignedp ? unsigned_char_type_node : signed_char_type_node; 3793 if (width == TYPE_PRECISION (short_integer_type_node)) 3794 return unsignedp ? short_unsigned_type_node : short_integer_type_node; 3795 if (width == TYPE_PRECISION (long_integer_type_node)) 3796 return unsignedp ? long_unsigned_type_node : long_integer_type_node; 3797 if (width == TYPE_PRECISION (long_long_integer_type_node)) 3798 return (unsignedp ? long_long_unsigned_type_node 3799 : long_long_integer_type_node); 3800 if (int128_integer_type_node 3801 && width == TYPE_PRECISION (int128_integer_type_node)) 3802 return (unsignedp ? int128_unsigned_type_node 3803 : int128_integer_type_node); 3804 return build_nonstandard_integer_type (width, unsignedp); 3805 } 3806 3807 /* The C version of the register_builtin_type langhook. */ 3808 3809 void 3810 c_register_builtin_type (tree type, const char* name) 3811 { 3812 tree decl; 3813 3814 decl = build_decl (UNKNOWN_LOCATION, 3815 TYPE_DECL, get_identifier (name), type); 3816 DECL_ARTIFICIAL (decl) = 1; 3817 if (!TYPE_NAME (type)) 3818 TYPE_NAME (type) = decl; 3819 pushdecl (decl); 3820 3821 registered_builtin_types = tree_cons (0, type, registered_builtin_types); 3822 } 3823 3824 /* Print an error message for invalid operands to arith operation 3825 CODE with TYPE0 for operand 0, and TYPE1 for operand 1. 3826 LOCATION is the location of the message. */ 3827 3828 void 3829 binary_op_error (location_t location, enum tree_code code, 3830 tree type0, tree type1) 3831 { 3832 const char *opname; 3833 3834 switch (code) 3835 { 3836 case PLUS_EXPR: 3837 opname = "+"; break; 3838 case MINUS_EXPR: 3839 opname = "-"; break; 3840 case MULT_EXPR: 3841 opname = "*"; break; 3842 case MAX_EXPR: 3843 opname = "max"; break; 3844 case MIN_EXPR: 3845 opname = "min"; break; 3846 case EQ_EXPR: 3847 opname = "=="; break; 3848 case NE_EXPR: 3849 opname = "!="; break; 3850 case LE_EXPR: 3851 opname = "<="; break; 3852 case GE_EXPR: 3853 opname = ">="; break; 3854 case LT_EXPR: 3855 opname = "<"; break; 3856 case GT_EXPR: 3857 opname = ">"; break; 3858 case LSHIFT_EXPR: 3859 opname = "<<"; break; 3860 case RSHIFT_EXPR: 3861 opname = ">>"; break; 3862 case TRUNC_MOD_EXPR: 3863 case FLOOR_MOD_EXPR: 3864 opname = "%"; break; 3865 case TRUNC_DIV_EXPR: 3866 case FLOOR_DIV_EXPR: 3867 opname = "/"; break; 3868 case BIT_AND_EXPR: 3869 opname = "&"; break; 3870 case BIT_IOR_EXPR: 3871 opname = "|"; break; 3872 case TRUTH_ANDIF_EXPR: 3873 opname = "&&"; break; 3874 case TRUTH_ORIF_EXPR: 3875 opname = "||"; break; 3876 case BIT_XOR_EXPR: 3877 opname = "^"; break; 3878 default: 3879 gcc_unreachable (); 3880 } 3881 error_at (location, 3882 "invalid operands to binary %s (have %qT and %qT)", opname, 3883 type0, type1); 3884 } 3885 3886 /* Given an expression as a tree, return its original type. Do this 3887 by stripping any conversion that preserves the sign and precision. */ 3888 static tree 3889 expr_original_type (tree expr) 3890 { 3891 STRIP_SIGN_NOPS (expr); 3892 return TREE_TYPE (expr); 3893 } 3894 3895 /* Subroutine of build_binary_op, used for comparison operations. 3896 See if the operands have both been converted from subword integer types 3897 and, if so, perhaps change them both back to their original type. 3898 This function is also responsible for converting the two operands 3899 to the proper common type for comparison. 3900 3901 The arguments of this function are all pointers to local variables 3902 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1, 3903 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE. 3904 3905 If this function returns nonzero, it means that the comparison has 3906 a constant value. What this function returns is an expression for 3907 that value. */ 3908 3909 tree 3910 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr, 3911 enum tree_code *rescode_ptr) 3912 { 3913 tree type; 3914 tree op0 = *op0_ptr; 3915 tree op1 = *op1_ptr; 3916 int unsignedp0, unsignedp1; 3917 int real1, real2; 3918 tree primop0, primop1; 3919 enum tree_code code = *rescode_ptr; 3920 location_t loc = EXPR_LOC_OR_HERE (op0); 3921 3922 /* Throw away any conversions to wider types 3923 already present in the operands. */ 3924 3925 primop0 = c_common_get_narrower (op0, &unsignedp0); 3926 primop1 = c_common_get_narrower (op1, &unsignedp1); 3927 3928 /* If primopN is first sign-extended from primopN's precision to opN's 3929 precision, then zero-extended from opN's precision to 3930 *restype_ptr precision, shortenings might be invalid. */ 3931 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0)) 3932 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr) 3933 && !unsignedp0 3934 && TYPE_UNSIGNED (TREE_TYPE (op0))) 3935 primop0 = op0; 3936 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1)) 3937 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr) 3938 && !unsignedp1 3939 && TYPE_UNSIGNED (TREE_TYPE (op1))) 3940 primop1 = op1; 3941 3942 /* Handle the case that OP0 does not *contain* a conversion 3943 but it *requires* conversion to FINAL_TYPE. */ 3944 3945 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr) 3946 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0)); 3947 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr) 3948 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1)); 3949 3950 /* If one of the operands must be floated, we cannot optimize. */ 3951 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE; 3952 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE; 3953 3954 /* If first arg is constant, swap the args (changing operation 3955 so value is preserved), for canonicalization. Don't do this if 3956 the second arg is 0. */ 3957 3958 if (TREE_CONSTANT (primop0) 3959 && !integer_zerop (primop1) && !real_zerop (primop1) 3960 && !fixed_zerop (primop1)) 3961 { 3962 tree tem = primop0; 3963 int temi = unsignedp0; 3964 primop0 = primop1; 3965 primop1 = tem; 3966 tem = op0; 3967 op0 = op1; 3968 op1 = tem; 3969 *op0_ptr = op0; 3970 *op1_ptr = op1; 3971 unsignedp0 = unsignedp1; 3972 unsignedp1 = temi; 3973 temi = real1; 3974 real1 = real2; 3975 real2 = temi; 3976 3977 switch (code) 3978 { 3979 case LT_EXPR: 3980 code = GT_EXPR; 3981 break; 3982 case GT_EXPR: 3983 code = LT_EXPR; 3984 break; 3985 case LE_EXPR: 3986 code = GE_EXPR; 3987 break; 3988 case GE_EXPR: 3989 code = LE_EXPR; 3990 break; 3991 default: 3992 break; 3993 } 3994 *rescode_ptr = code; 3995 } 3996 3997 /* If comparing an integer against a constant more bits wide, 3998 maybe we can deduce a value of 1 or 0 independent of the data. 3999 Or else truncate the constant now 4000 rather than extend the variable at run time. 4001 4002 This is only interesting if the constant is the wider arg. 4003 Also, it is not safe if the constant is unsigned and the 4004 variable arg is signed, since in this case the variable 4005 would be sign-extended and then regarded as unsigned. 4006 Our technique fails in this case because the lowest/highest 4007 possible unsigned results don't follow naturally from the 4008 lowest/highest possible values of the variable operand. 4009 For just EQ_EXPR and NE_EXPR there is another technique that 4010 could be used: see if the constant can be faithfully represented 4011 in the other operand's type, by truncating it and reextending it 4012 and see if that preserves the constant's value. */ 4013 4014 if (!real1 && !real2 4015 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE 4016 && TREE_CODE (primop1) == INTEGER_CST 4017 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)) 4018 { 4019 int min_gt, max_gt, min_lt, max_lt; 4020 tree maxval, minval; 4021 /* 1 if comparison is nominally unsigned. */ 4022 int unsignedp = TYPE_UNSIGNED (*restype_ptr); 4023 tree val; 4024 4025 type = c_common_signed_or_unsigned_type (unsignedp0, 4026 TREE_TYPE (primop0)); 4027 4028 maxval = TYPE_MAX_VALUE (type); 4029 minval = TYPE_MIN_VALUE (type); 4030 4031 if (unsignedp && !unsignedp0) 4032 *restype_ptr = c_common_signed_type (*restype_ptr); 4033 4034 if (TREE_TYPE (primop1) != *restype_ptr) 4035 { 4036 /* Convert primop1 to target type, but do not introduce 4037 additional overflow. We know primop1 is an int_cst. */ 4038 primop1 = force_fit_type_double (*restype_ptr, 4039 tree_to_double_int (primop1), 4040 0, TREE_OVERFLOW (primop1)); 4041 } 4042 if (type != *restype_ptr) 4043 { 4044 minval = convert (*restype_ptr, minval); 4045 maxval = convert (*restype_ptr, maxval); 4046 } 4047 4048 if (unsignedp && unsignedp0) 4049 { 4050 min_gt = INT_CST_LT_UNSIGNED (primop1, minval); 4051 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval); 4052 min_lt = INT_CST_LT_UNSIGNED (minval, primop1); 4053 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1); 4054 } 4055 else 4056 { 4057 min_gt = INT_CST_LT (primop1, minval); 4058 max_gt = INT_CST_LT (primop1, maxval); 4059 min_lt = INT_CST_LT (minval, primop1); 4060 max_lt = INT_CST_LT (maxval, primop1); 4061 } 4062 4063 val = 0; 4064 /* This used to be a switch, but Genix compiler can't handle that. */ 4065 if (code == NE_EXPR) 4066 { 4067 if (max_lt || min_gt) 4068 val = truthvalue_true_node; 4069 } 4070 else if (code == EQ_EXPR) 4071 { 4072 if (max_lt || min_gt) 4073 val = truthvalue_false_node; 4074 } 4075 else if (code == LT_EXPR) 4076 { 4077 if (max_lt) 4078 val = truthvalue_true_node; 4079 if (!min_lt) 4080 val = truthvalue_false_node; 4081 } 4082 else if (code == GT_EXPR) 4083 { 4084 if (min_gt) 4085 val = truthvalue_true_node; 4086 if (!max_gt) 4087 val = truthvalue_false_node; 4088 } 4089 else if (code == LE_EXPR) 4090 { 4091 if (!max_gt) 4092 val = truthvalue_true_node; 4093 if (min_gt) 4094 val = truthvalue_false_node; 4095 } 4096 else if (code == GE_EXPR) 4097 { 4098 if (!min_lt) 4099 val = truthvalue_true_node; 4100 if (max_lt) 4101 val = truthvalue_false_node; 4102 } 4103 4104 /* If primop0 was sign-extended and unsigned comparison specd, 4105 we did a signed comparison above using the signed type bounds. 4106 But the comparison we output must be unsigned. 4107 4108 Also, for inequalities, VAL is no good; but if the signed 4109 comparison had *any* fixed result, it follows that the 4110 unsigned comparison just tests the sign in reverse 4111 (positive values are LE, negative ones GE). 4112 So we can generate an unsigned comparison 4113 against an extreme value of the signed type. */ 4114 4115 if (unsignedp && !unsignedp0) 4116 { 4117 if (val != 0) 4118 switch (code) 4119 { 4120 case LT_EXPR: 4121 case GE_EXPR: 4122 primop1 = TYPE_MIN_VALUE (type); 4123 val = 0; 4124 break; 4125 4126 case LE_EXPR: 4127 case GT_EXPR: 4128 primop1 = TYPE_MAX_VALUE (type); 4129 val = 0; 4130 break; 4131 4132 default: 4133 break; 4134 } 4135 type = c_common_unsigned_type (type); 4136 } 4137 4138 if (TREE_CODE (primop0) != INTEGER_CST 4139 && c_inhibit_evaluation_warnings == 0) 4140 { 4141 if (val == truthvalue_false_node) 4142 warning_at (loc, OPT_Wtype_limits, 4143 "comparison is always false due to limited range of data type"); 4144 if (val == truthvalue_true_node) 4145 warning_at (loc, OPT_Wtype_limits, 4146 "comparison is always true due to limited range of data type"); 4147 } 4148 4149 if (val != 0) 4150 { 4151 /* Don't forget to evaluate PRIMOP0 if it has side effects. */ 4152 if (TREE_SIDE_EFFECTS (primop0)) 4153 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val); 4154 return val; 4155 } 4156 4157 /* Value is not predetermined, but do the comparison 4158 in the type of the operand that is not constant. 4159 TYPE is already properly set. */ 4160 } 4161 4162 /* If either arg is decimal float and the other is float, find the 4163 proper common type to use for comparison. */ 4164 else if (real1 && real2 4165 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0))) 4166 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))) 4167 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); 4168 4169 else if (real1 && real2 4170 && (TYPE_PRECISION (TREE_TYPE (primop0)) 4171 == TYPE_PRECISION (TREE_TYPE (primop1)))) 4172 type = TREE_TYPE (primop0); 4173 4174 /* If args' natural types are both narrower than nominal type 4175 and both extend in the same manner, compare them 4176 in the type of the wider arg. 4177 Otherwise must actually extend both to the nominal 4178 common type lest different ways of extending 4179 alter the result. 4180 (eg, (short)-1 == (unsigned short)-1 should be 0.) */ 4181 4182 else if (unsignedp0 == unsignedp1 && real1 == real2 4183 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr) 4184 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)) 4185 { 4186 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); 4187 type = c_common_signed_or_unsigned_type (unsignedp0 4188 || TYPE_UNSIGNED (*restype_ptr), 4189 type); 4190 /* Make sure shorter operand is extended the right way 4191 to match the longer operand. */ 4192 primop0 4193 = convert (c_common_signed_or_unsigned_type (unsignedp0, 4194 TREE_TYPE (primop0)), 4195 primop0); 4196 primop1 4197 = convert (c_common_signed_or_unsigned_type (unsignedp1, 4198 TREE_TYPE (primop1)), 4199 primop1); 4200 } 4201 else 4202 { 4203 /* Here we must do the comparison on the nominal type 4204 using the args exactly as we received them. */ 4205 type = *restype_ptr; 4206 primop0 = op0; 4207 primop1 = op1; 4208 4209 if (!real1 && !real2 && integer_zerop (primop1) 4210 && TYPE_UNSIGNED (*restype_ptr)) 4211 { 4212 tree value = 0; 4213 /* All unsigned values are >= 0, so we warn. However, 4214 if OP0 is a constant that is >= 0, the signedness of 4215 the comparison isn't an issue, so suppress the 4216 warning. */ 4217 bool warn = 4218 warn_type_limits && !in_system_header 4219 && c_inhibit_evaluation_warnings == 0 4220 && !(TREE_CODE (primop0) == INTEGER_CST 4221 && !TREE_OVERFLOW (convert (c_common_signed_type (type), 4222 primop0))) 4223 /* Do not warn for enumeration types. */ 4224 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE); 4225 4226 switch (code) 4227 { 4228 case GE_EXPR: 4229 if (warn) 4230 warning_at (loc, OPT_Wtype_limits, 4231 "comparison of unsigned expression >= 0 is always true"); 4232 value = truthvalue_true_node; 4233 break; 4234 4235 case LT_EXPR: 4236 if (warn) 4237 warning_at (loc, OPT_Wtype_limits, 4238 "comparison of unsigned expression < 0 is always false"); 4239 value = truthvalue_false_node; 4240 break; 4241 4242 default: 4243 break; 4244 } 4245 4246 if (value != 0) 4247 { 4248 /* Don't forget to evaluate PRIMOP0 if it has side effects. */ 4249 if (TREE_SIDE_EFFECTS (primop0)) 4250 return build2 (COMPOUND_EXPR, TREE_TYPE (value), 4251 primop0, value); 4252 return value; 4253 } 4254 } 4255 } 4256 4257 *op0_ptr = convert (type, primop0); 4258 *op1_ptr = convert (type, primop1); 4259 4260 *restype_ptr = truthvalue_type_node; 4261 4262 return 0; 4263 } 4264 4265 /* Return a tree for the sum or difference (RESULTCODE says which) 4266 of pointer PTROP and integer INTOP. */ 4267 4268 tree 4269 pointer_int_sum (location_t loc, enum tree_code resultcode, 4270 tree ptrop, tree intop) 4271 { 4272 tree size_exp, ret; 4273 4274 /* The result is a pointer of the same type that is being added. */ 4275 tree result_type = TREE_TYPE (ptrop); 4276 4277 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE) 4278 { 4279 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, 4280 "pointer of type %<void *%> used in arithmetic"); 4281 size_exp = integer_one_node; 4282 } 4283 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE) 4284 { 4285 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, 4286 "pointer to a function used in arithmetic"); 4287 size_exp = integer_one_node; 4288 } 4289 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE) 4290 { 4291 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, 4292 "pointer to member function used in arithmetic"); 4293 size_exp = integer_one_node; 4294 } 4295 else 4296 size_exp = size_in_bytes (TREE_TYPE (result_type)); 4297 4298 /* We are manipulating pointer values, so we don't need to warn 4299 about relying on undefined signed overflow. We disable the 4300 warning here because we use integer types so fold won't know that 4301 they are really pointers. */ 4302 fold_defer_overflow_warnings (); 4303 4304 /* If what we are about to multiply by the size of the elements 4305 contains a constant term, apply distributive law 4306 and multiply that constant term separately. 4307 This helps produce common subexpressions. */ 4308 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR) 4309 && !TREE_CONSTANT (intop) 4310 && TREE_CONSTANT (TREE_OPERAND (intop, 1)) 4311 && TREE_CONSTANT (size_exp) 4312 /* If the constant comes from pointer subtraction, 4313 skip this optimization--it would cause an error. */ 4314 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE 4315 /* If the constant is unsigned, and smaller than the pointer size, 4316 then we must skip this optimization. This is because it could cause 4317 an overflow error if the constant is negative but INTOP is not. */ 4318 && (!TYPE_UNSIGNED (TREE_TYPE (intop)) 4319 || (TYPE_PRECISION (TREE_TYPE (intop)) 4320 == TYPE_PRECISION (TREE_TYPE (ptrop))))) 4321 { 4322 enum tree_code subcode = resultcode; 4323 tree int_type = TREE_TYPE (intop); 4324 if (TREE_CODE (intop) == MINUS_EXPR) 4325 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR); 4326 /* Convert both subexpression types to the type of intop, 4327 because weird cases involving pointer arithmetic 4328 can result in a sum or difference with different type args. */ 4329 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)), 4330 subcode, ptrop, 4331 convert (int_type, TREE_OPERAND (intop, 1)), 1); 4332 intop = convert (int_type, TREE_OPERAND (intop, 0)); 4333 } 4334 4335 /* Convert the integer argument to a type the same size as sizetype 4336 so the multiply won't overflow spuriously. */ 4337 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype) 4338 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype)) 4339 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype), 4340 TYPE_UNSIGNED (sizetype)), intop); 4341 4342 /* Replace the integer argument with a suitable product by the object size. 4343 Do this multiplication as signed, then convert to the appropriate type 4344 for the pointer operation and disregard an overflow that occurred only 4345 because of the sign-extension change in the latter conversion. */ 4346 { 4347 tree t = build_binary_op (loc, 4348 MULT_EXPR, intop, 4349 convert (TREE_TYPE (intop), size_exp), 1); 4350 intop = convert (sizetype, t); 4351 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t)) 4352 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop), 4353 TREE_INT_CST_HIGH (intop)); 4354 } 4355 4356 /* Create the sum or difference. */ 4357 if (resultcode == MINUS_EXPR) 4358 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop); 4359 4360 ret = fold_build_pointer_plus_loc (loc, ptrop, intop); 4361 4362 fold_undefer_and_ignore_overflow_warnings (); 4363 4364 return ret; 4365 } 4366 4367 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded 4368 and if NON_CONST is known not to be permitted in an evaluated part 4369 of a constant expression. */ 4370 4371 tree 4372 c_wrap_maybe_const (tree expr, bool non_const) 4373 { 4374 bool nowarning = TREE_NO_WARNING (expr); 4375 location_t loc = EXPR_LOCATION (expr); 4376 4377 /* This should never be called for C++. */ 4378 if (c_dialect_cxx ()) 4379 gcc_unreachable (); 4380 4381 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */ 4382 STRIP_TYPE_NOPS (expr); 4383 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr); 4384 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const; 4385 if (nowarning) 4386 TREE_NO_WARNING (expr) = 1; 4387 protected_set_expr_location (expr, loc); 4388 4389 return expr; 4390 } 4391 4392 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but 4393 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR 4394 around the SAVE_EXPR if needed so that c_fully_fold does not need 4395 to look inside SAVE_EXPRs. */ 4396 4397 tree 4398 c_save_expr (tree expr) 4399 { 4400 bool maybe_const = true; 4401 if (c_dialect_cxx ()) 4402 return save_expr (expr); 4403 expr = c_fully_fold (expr, false, &maybe_const); 4404 expr = save_expr (expr); 4405 if (!maybe_const) 4406 expr = c_wrap_maybe_const (expr, true); 4407 return expr; 4408 } 4409 4410 /* Return whether EXPR is a declaration whose address can never be 4411 NULL. */ 4412 4413 bool 4414 decl_with_nonnull_addr_p (const_tree expr) 4415 { 4416 return (DECL_P (expr) 4417 && (TREE_CODE (expr) == PARM_DECL 4418 || TREE_CODE (expr) == LABEL_DECL 4419 || !DECL_WEAK (expr))); 4420 } 4421 4422 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR, 4423 or for an `if' or `while' statement or ?..: exp. It should already 4424 have been validated to be of suitable type; otherwise, a bad 4425 diagnostic may result. 4426 4427 The EXPR is located at LOCATION. 4428 4429 This preparation consists of taking the ordinary 4430 representation of an expression expr and producing a valid tree 4431 boolean expression describing whether expr is nonzero. We could 4432 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1), 4433 but we optimize comparisons, &&, ||, and !. 4434 4435 The resulting type should always be `truthvalue_type_node'. */ 4436 4437 tree 4438 c_common_truthvalue_conversion (location_t location, tree expr) 4439 { 4440 switch (TREE_CODE (expr)) 4441 { 4442 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR: 4443 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR: 4444 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR: 4445 case ORDERED_EXPR: case UNORDERED_EXPR: 4446 if (TREE_TYPE (expr) == truthvalue_type_node) 4447 return expr; 4448 expr = build2 (TREE_CODE (expr), truthvalue_type_node, 4449 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1)); 4450 goto ret; 4451 4452 case TRUTH_ANDIF_EXPR: 4453 case TRUTH_ORIF_EXPR: 4454 case TRUTH_AND_EXPR: 4455 case TRUTH_OR_EXPR: 4456 case TRUTH_XOR_EXPR: 4457 if (TREE_TYPE (expr) == truthvalue_type_node) 4458 return expr; 4459 expr = build2 (TREE_CODE (expr), truthvalue_type_node, 4460 c_common_truthvalue_conversion (location, 4461 TREE_OPERAND (expr, 0)), 4462 c_common_truthvalue_conversion (location, 4463 TREE_OPERAND (expr, 1))); 4464 goto ret; 4465 4466 case TRUTH_NOT_EXPR: 4467 if (TREE_TYPE (expr) == truthvalue_type_node) 4468 return expr; 4469 expr = build1 (TREE_CODE (expr), truthvalue_type_node, 4470 c_common_truthvalue_conversion (location, 4471 TREE_OPERAND (expr, 0))); 4472 goto ret; 4473 4474 case ERROR_MARK: 4475 return expr; 4476 4477 case INTEGER_CST: 4478 return integer_zerop (expr) ? truthvalue_false_node 4479 : truthvalue_true_node; 4480 4481 case REAL_CST: 4482 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0) 4483 ? truthvalue_true_node 4484 : truthvalue_false_node; 4485 4486 case FIXED_CST: 4487 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr), 4488 &FCONST0 (TYPE_MODE (TREE_TYPE (expr)))) 4489 ? truthvalue_true_node 4490 : truthvalue_false_node; 4491 4492 case FUNCTION_DECL: 4493 expr = build_unary_op (location, ADDR_EXPR, expr, 0); 4494 /* Fall through. */ 4495 4496 case ADDR_EXPR: 4497 { 4498 tree inner = TREE_OPERAND (expr, 0); 4499 if (decl_with_nonnull_addr_p (inner)) 4500 { 4501 /* Common Ada/Pascal programmer's mistake. */ 4502 warning_at (location, 4503 OPT_Waddress, 4504 "the address of %qD will always evaluate as %<true%>", 4505 inner); 4506 return truthvalue_true_node; 4507 } 4508 break; 4509 } 4510 4511 case COMPLEX_EXPR: 4512 expr = build_binary_op (EXPR_LOCATION (expr), 4513 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)) 4514 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), 4515 c_common_truthvalue_conversion (location, 4516 TREE_OPERAND (expr, 0)), 4517 c_common_truthvalue_conversion (location, 4518 TREE_OPERAND (expr, 1)), 4519 0); 4520 goto ret; 4521 4522 case NEGATE_EXPR: 4523 case ABS_EXPR: 4524 case FLOAT_EXPR: 4525 case EXCESS_PRECISION_EXPR: 4526 /* These don't change whether an object is nonzero or zero. */ 4527 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0)); 4528 4529 case LROTATE_EXPR: 4530 case RROTATE_EXPR: 4531 /* These don't change whether an object is zero or nonzero, but 4532 we can't ignore them if their second arg has side-effects. */ 4533 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))) 4534 { 4535 expr = build2 (COMPOUND_EXPR, truthvalue_type_node, 4536 TREE_OPERAND (expr, 1), 4537 c_common_truthvalue_conversion 4538 (location, TREE_OPERAND (expr, 0))); 4539 goto ret; 4540 } 4541 else 4542 return c_common_truthvalue_conversion (location, 4543 TREE_OPERAND (expr, 0)); 4544 4545 case COND_EXPR: 4546 /* Distribute the conversion into the arms of a COND_EXPR. */ 4547 if (c_dialect_cxx ()) 4548 { 4549 tree op1 = TREE_OPERAND (expr, 1); 4550 tree op2 = TREE_OPERAND (expr, 2); 4551 /* In C++ one of the arms might have void type if it is throw. */ 4552 if (!VOID_TYPE_P (TREE_TYPE (op1))) 4553 op1 = c_common_truthvalue_conversion (location, op1); 4554 if (!VOID_TYPE_P (TREE_TYPE (op2))) 4555 op2 = c_common_truthvalue_conversion (location, op2); 4556 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node, 4557 TREE_OPERAND (expr, 0), op1, op2); 4558 goto ret; 4559 } 4560 else 4561 { 4562 /* Folding will happen later for C. */ 4563 expr = build3 (COND_EXPR, truthvalue_type_node, 4564 TREE_OPERAND (expr, 0), 4565 c_common_truthvalue_conversion (location, 4566 TREE_OPERAND (expr, 1)), 4567 c_common_truthvalue_conversion (location, 4568 TREE_OPERAND (expr, 2))); 4569 goto ret; 4570 } 4571 4572 CASE_CONVERT: 4573 { 4574 tree totype = TREE_TYPE (expr); 4575 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0)); 4576 4577 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE, 4578 since that affects how `default_conversion' will behave. */ 4579 if (TREE_CODE (totype) == REFERENCE_TYPE 4580 || TREE_CODE (fromtype) == REFERENCE_TYPE) 4581 break; 4582 /* Don't strip a conversion from C++0x scoped enum, since they 4583 don't implicitly convert to other types. */ 4584 if (TREE_CODE (fromtype) == ENUMERAL_TYPE 4585 && ENUM_IS_SCOPED (fromtype)) 4586 break; 4587 /* If this isn't narrowing the argument, we can ignore it. */ 4588 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype)) 4589 return c_common_truthvalue_conversion (location, 4590 TREE_OPERAND (expr, 0)); 4591 } 4592 break; 4593 4594 case MODIFY_EXPR: 4595 if (!TREE_NO_WARNING (expr) 4596 && warn_parentheses) 4597 { 4598 warning (OPT_Wparentheses, 4599 "suggest parentheses around assignment used as truth value"); 4600 TREE_NO_WARNING (expr) = 1; 4601 } 4602 break; 4603 4604 default: 4605 break; 4606 } 4607 4608 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE) 4609 { 4610 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr)); 4611 expr = (build_binary_op 4612 (EXPR_LOCATION (expr), 4613 (TREE_SIDE_EFFECTS (expr) 4614 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), 4615 c_common_truthvalue_conversion 4616 (location, 4617 build_unary_op (location, REALPART_EXPR, t, 0)), 4618 c_common_truthvalue_conversion 4619 (location, 4620 build_unary_op (location, IMAGPART_EXPR, t, 0)), 4621 0)); 4622 goto ret; 4623 } 4624 4625 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE) 4626 { 4627 tree fixed_zero_node = build_fixed (TREE_TYPE (expr), 4628 FCONST0 (TYPE_MODE 4629 (TREE_TYPE (expr)))); 4630 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1); 4631 } 4632 else 4633 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1); 4634 4635 ret: 4636 protected_set_expr_location (expr, location); 4637 return expr; 4638 } 4639 4640 static void def_builtin_1 (enum built_in_function fncode, 4641 const char *name, 4642 enum built_in_class fnclass, 4643 tree fntype, tree libtype, 4644 bool both_p, bool fallback_p, bool nonansi_p, 4645 tree fnattrs, bool implicit_p); 4646 4647 4648 /* Apply the TYPE_QUALS to the new DECL. */ 4649 4650 void 4651 c_apply_type_quals_to_decl (int type_quals, tree decl) 4652 { 4653 tree type = TREE_TYPE (decl); 4654 4655 if (type == error_mark_node) 4656 return; 4657 4658 if ((type_quals & TYPE_QUAL_CONST) 4659 || (type && TREE_CODE (type) == REFERENCE_TYPE)) 4660 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr 4661 constructor can produce constant init, so rely on cp_finish_decl to 4662 clear TREE_READONLY if the variable has non-constant init. */ 4663 TREE_READONLY (decl) = 1; 4664 if (type_quals & TYPE_QUAL_VOLATILE) 4665 { 4666 TREE_SIDE_EFFECTS (decl) = 1; 4667 TREE_THIS_VOLATILE (decl) = 1; 4668 } 4669 if (type_quals & TYPE_QUAL_RESTRICT) 4670 { 4671 while (type && TREE_CODE (type) == ARRAY_TYPE) 4672 /* Allow 'restrict' on arrays of pointers. 4673 FIXME currently we just ignore it. */ 4674 type = TREE_TYPE (type); 4675 if (!type 4676 || !POINTER_TYPE_P (type) 4677 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))) 4678 error ("invalid use of %<restrict%>"); 4679 } 4680 } 4681 4682 /* Hash function for the problem of multiple type definitions in 4683 different files. This must hash all types that will compare 4684 equal via comptypes to the same value. In practice it hashes 4685 on some of the simple stuff and leaves the details to comptypes. */ 4686 4687 static hashval_t 4688 c_type_hash (const void *p) 4689 { 4690 int n_elements; 4691 int shift, size; 4692 const_tree const t = (const_tree) p; 4693 tree t2; 4694 switch (TREE_CODE (t)) 4695 { 4696 /* For pointers, hash on pointee type plus some swizzling. */ 4697 case POINTER_TYPE: 4698 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003; 4699 /* Hash on number of elements and total size. */ 4700 case ENUMERAL_TYPE: 4701 shift = 3; 4702 t2 = TYPE_VALUES (t); 4703 break; 4704 case RECORD_TYPE: 4705 shift = 0; 4706 t2 = TYPE_FIELDS (t); 4707 break; 4708 case QUAL_UNION_TYPE: 4709 shift = 1; 4710 t2 = TYPE_FIELDS (t); 4711 break; 4712 case UNION_TYPE: 4713 shift = 2; 4714 t2 = TYPE_FIELDS (t); 4715 break; 4716 default: 4717 gcc_unreachable (); 4718 } 4719 /* FIXME: We want to use a DECL_CHAIN iteration method here, but 4720 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */ 4721 n_elements = list_length (t2); 4722 /* We might have a VLA here. */ 4723 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST) 4724 size = 0; 4725 else 4726 size = TREE_INT_CST_LOW (TYPE_SIZE (t)); 4727 return ((size << 24) | (n_elements << shift)); 4728 } 4729 4730 static GTY((param_is (union tree_node))) htab_t type_hash_table; 4731 4732 /* Return the typed-based alias set for T, which may be an expression 4733 or a type. Return -1 if we don't do anything special. */ 4734 4735 alias_set_type 4736 c_common_get_alias_set (tree t) 4737 { 4738 tree u; 4739 PTR *slot; 4740 4741 /* For VLAs, use the alias set of the element type rather than the 4742 default of alias set 0 for types compared structurally. */ 4743 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t)) 4744 { 4745 if (TREE_CODE (t) == ARRAY_TYPE) 4746 return get_alias_set (TREE_TYPE (t)); 4747 return -1; 4748 } 4749 4750 /* Permit type-punning when accessing a union, provided the access 4751 is directly through the union. For example, this code does not 4752 permit taking the address of a union member and then storing 4753 through it. Even the type-punning allowed here is a GCC 4754 extension, albeit a common and useful one; the C standard says 4755 that such accesses have implementation-defined behavior. */ 4756 for (u = t; 4757 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF; 4758 u = TREE_OPERAND (u, 0)) 4759 if (TREE_CODE (u) == COMPONENT_REF 4760 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE) 4761 return 0; 4762 4763 /* That's all the expressions we handle specially. */ 4764 if (!TYPE_P (t)) 4765 return -1; 4766 4767 /* The C standard guarantees that any object may be accessed via an 4768 lvalue that has character type. */ 4769 if (t == char_type_node 4770 || t == signed_char_type_node 4771 || t == unsigned_char_type_node) 4772 return 0; 4773 4774 /* The C standard specifically allows aliasing between signed and 4775 unsigned variants of the same type. We treat the signed 4776 variant as canonical. */ 4777 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t)) 4778 { 4779 tree t1 = c_common_signed_type (t); 4780 4781 /* t1 == t can happen for boolean nodes which are always unsigned. */ 4782 if (t1 != t) 4783 return get_alias_set (t1); 4784 } 4785 4786 /* Handle the case of multiple type nodes referring to "the same" type, 4787 which occurs with IMA. These share an alias set. FIXME: Currently only 4788 C90 is handled. (In C99 type compatibility is not transitive, which 4789 complicates things mightily. The alias set splay trees can theoretically 4790 represent this, but insertion is tricky when you consider all the 4791 different orders things might arrive in.) */ 4792 4793 if (c_language != clk_c || flag_isoc99) 4794 return -1; 4795 4796 /* Save time if there's only one input file. */ 4797 if (num_in_fnames == 1) 4798 return -1; 4799 4800 /* Pointers need special handling if they point to any type that 4801 needs special handling (below). */ 4802 if (TREE_CODE (t) == POINTER_TYPE) 4803 { 4804 tree t2; 4805 /* Find bottom type under any nested POINTERs. */ 4806 for (t2 = TREE_TYPE (t); 4807 TREE_CODE (t2) == POINTER_TYPE; 4808 t2 = TREE_TYPE (t2)) 4809 ; 4810 if (TREE_CODE (t2) != RECORD_TYPE 4811 && TREE_CODE (t2) != ENUMERAL_TYPE 4812 && TREE_CODE (t2) != QUAL_UNION_TYPE 4813 && TREE_CODE (t2) != UNION_TYPE) 4814 return -1; 4815 if (TYPE_SIZE (t2) == 0) 4816 return -1; 4817 } 4818 /* These are the only cases that need special handling. */ 4819 if (TREE_CODE (t) != RECORD_TYPE 4820 && TREE_CODE (t) != ENUMERAL_TYPE 4821 && TREE_CODE (t) != QUAL_UNION_TYPE 4822 && TREE_CODE (t) != UNION_TYPE 4823 && TREE_CODE (t) != POINTER_TYPE) 4824 return -1; 4825 /* Undefined? */ 4826 if (TYPE_SIZE (t) == 0) 4827 return -1; 4828 4829 /* Look up t in hash table. Only one of the compatible types within each 4830 alias set is recorded in the table. */ 4831 if (!type_hash_table) 4832 type_hash_table = htab_create_ggc (1021, c_type_hash, 4833 (htab_eq) lang_hooks.types_compatible_p, 4834 NULL); 4835 slot = htab_find_slot (type_hash_table, t, INSERT); 4836 if (*slot != NULL) 4837 { 4838 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot); 4839 return TYPE_ALIAS_SET ((tree)*slot); 4840 } 4841 else 4842 /* Our caller will assign and record (in t) a new alias set; all we need 4843 to do is remember t in the hash table. */ 4844 *slot = t; 4845 4846 return -1; 4847 } 4848 4849 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where 4850 the second parameter indicates which OPERATOR is being applied. 4851 The COMPLAIN flag controls whether we should diagnose possibly 4852 ill-formed constructs or not. LOC is the location of the SIZEOF or 4853 TYPEOF operator. */ 4854 4855 tree 4856 c_sizeof_or_alignof_type (location_t loc, 4857 tree type, bool is_sizeof, int complain) 4858 { 4859 const char *op_name; 4860 tree value = NULL; 4861 enum tree_code type_code = TREE_CODE (type); 4862 4863 op_name = is_sizeof ? "sizeof" : "__alignof__"; 4864 4865 if (type_code == FUNCTION_TYPE) 4866 { 4867 if (is_sizeof) 4868 { 4869 if (complain && (pedantic || warn_pointer_arith)) 4870 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, 4871 "invalid application of %<sizeof%> to a function type"); 4872 else if (!complain) 4873 return error_mark_node; 4874 value = size_one_node; 4875 } 4876 else 4877 { 4878 if (complain) 4879 { 4880 if (c_dialect_cxx ()) 4881 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit " 4882 "%<alignof%> applied to a function type"); 4883 else 4884 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit " 4885 "%<_Alignof%> applied to a function type"); 4886 } 4887 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); 4888 } 4889 } 4890 else if (type_code == VOID_TYPE || type_code == ERROR_MARK) 4891 { 4892 if (type_code == VOID_TYPE 4893 && complain && (pedantic || warn_pointer_arith)) 4894 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, 4895 "invalid application of %qs to a void type", op_name); 4896 else if (!complain) 4897 return error_mark_node; 4898 value = size_one_node; 4899 } 4900 else if (!COMPLETE_TYPE_P (type) 4901 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE)) 4902 { 4903 if (complain) 4904 error_at (loc, "invalid application of %qs to incomplete type %qT", 4905 op_name, type); 4906 return error_mark_node; 4907 } 4908 else if (c_dialect_cxx () && type_code == ARRAY_TYPE 4909 && !COMPLETE_TYPE_P (TREE_TYPE (type))) 4910 { 4911 if (complain) 4912 error_at (loc, "invalid application of %qs to array type %qT of " 4913 "incomplete element type", op_name, type); 4914 return error_mark_node; 4915 } 4916 else 4917 { 4918 if (is_sizeof) 4919 /* Convert in case a char is more than one unit. */ 4920 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), 4921 size_int (TYPE_PRECISION (char_type_node) 4922 / BITS_PER_UNIT)); 4923 else 4924 value = size_int (TYPE_ALIGN_UNIT (type)); 4925 } 4926 4927 /* VALUE will have the middle-end integer type sizetype. 4928 However, we should really return a value of type `size_t', 4929 which is just a typedef for an ordinary integer type. */ 4930 value = fold_convert_loc (loc, size_type_node, value); 4931 4932 return value; 4933 } 4934 4935 /* Implement the __alignof keyword: Return the minimum required 4936 alignment of EXPR, measured in bytes. For VAR_DECLs, 4937 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set 4938 from an "aligned" __attribute__ specification). LOC is the 4939 location of the ALIGNOF operator. */ 4940 4941 tree 4942 c_alignof_expr (location_t loc, tree expr) 4943 { 4944 tree t; 4945 4946 if (VAR_OR_FUNCTION_DECL_P (expr)) 4947 t = size_int (DECL_ALIGN_UNIT (expr)); 4948 4949 else if (TREE_CODE (expr) == COMPONENT_REF 4950 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) 4951 { 4952 error_at (loc, "%<__alignof%> applied to a bit-field"); 4953 t = size_one_node; 4954 } 4955 else if (TREE_CODE (expr) == COMPONENT_REF 4956 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL) 4957 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1))); 4958 4959 else if (TREE_CODE (expr) == INDIRECT_REF) 4960 { 4961 tree t = TREE_OPERAND (expr, 0); 4962 tree best = t; 4963 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); 4964 4965 while (CONVERT_EXPR_P (t) 4966 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE) 4967 { 4968 int thisalign; 4969 4970 t = TREE_OPERAND (t, 0); 4971 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); 4972 if (thisalign > bestalign) 4973 best = t, bestalign = thisalign; 4974 } 4975 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best))); 4976 } 4977 else 4978 return c_alignof (loc, TREE_TYPE (expr)); 4979 4980 return fold_convert_loc (loc, size_type_node, t); 4981 } 4982 4983 /* Handle C and C++ default attributes. */ 4984 4985 enum built_in_attribute 4986 { 4987 #define DEF_ATTR_NULL_TREE(ENUM) ENUM, 4988 #define DEF_ATTR_INT(ENUM, VALUE) ENUM, 4989 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM, 4990 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM, 4991 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM, 4992 #include "builtin-attrs.def" 4993 #undef DEF_ATTR_NULL_TREE 4994 #undef DEF_ATTR_INT 4995 #undef DEF_ATTR_STRING 4996 #undef DEF_ATTR_IDENT 4997 #undef DEF_ATTR_TREE_LIST 4998 ATTR_LAST 4999 }; 5000 5001 static GTY(()) tree built_in_attributes[(int) ATTR_LAST]; 5002 5003 static void c_init_attributes (void); 5004 5005 enum c_builtin_type 5006 { 5007 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME, 5008 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME, 5009 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME, 5010 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME, 5011 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, 5012 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, 5013 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME, 5014 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME, 5015 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME, 5016 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME, 5017 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME, 5018 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME, 5019 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, 5020 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, 5021 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \ 5022 NAME, 5023 #define DEF_POINTER_TYPE(NAME, TYPE) NAME, 5024 #include "builtin-types.def" 5025 #undef DEF_PRIMITIVE_TYPE 5026 #undef DEF_FUNCTION_TYPE_0 5027 #undef DEF_FUNCTION_TYPE_1 5028 #undef DEF_FUNCTION_TYPE_2 5029 #undef DEF_FUNCTION_TYPE_3 5030 #undef DEF_FUNCTION_TYPE_4 5031 #undef DEF_FUNCTION_TYPE_5 5032 #undef DEF_FUNCTION_TYPE_6 5033 #undef DEF_FUNCTION_TYPE_7 5034 #undef DEF_FUNCTION_TYPE_VAR_0 5035 #undef DEF_FUNCTION_TYPE_VAR_1 5036 #undef DEF_FUNCTION_TYPE_VAR_2 5037 #undef DEF_FUNCTION_TYPE_VAR_3 5038 #undef DEF_FUNCTION_TYPE_VAR_4 5039 #undef DEF_FUNCTION_TYPE_VAR_5 5040 #undef DEF_POINTER_TYPE 5041 BT_LAST 5042 }; 5043 5044 typedef enum c_builtin_type builtin_type; 5045 5046 /* A temporary array for c_common_nodes_and_builtins. Used in 5047 communication with def_fn_type. */ 5048 static tree builtin_types[(int) BT_LAST + 1]; 5049 5050 /* A helper function for c_common_nodes_and_builtins. Build function type 5051 for DEF with return type RET and N arguments. If VAR is true, then the 5052 function should be variadic after those N arguments. 5053 5054 Takes special care not to ICE if any of the types involved are 5055 error_mark_node, which indicates that said type is not in fact available 5056 (see builtin_type_for_size). In which case the function type as a whole 5057 should be error_mark_node. */ 5058 5059 static void 5060 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...) 5061 { 5062 tree t; 5063 tree *args = XALLOCAVEC (tree, n); 5064 va_list list; 5065 int i; 5066 5067 va_start (list, n); 5068 for (i = 0; i < n; ++i) 5069 { 5070 builtin_type a = (builtin_type) va_arg (list, int); 5071 t = builtin_types[a]; 5072 if (t == error_mark_node) 5073 goto egress; 5074 args[i] = t; 5075 } 5076 5077 t = builtin_types[ret]; 5078 if (t == error_mark_node) 5079 goto egress; 5080 if (var) 5081 t = build_varargs_function_type_array (t, n, args); 5082 else 5083 t = build_function_type_array (t, n, args); 5084 5085 egress: 5086 builtin_types[def] = t; 5087 va_end (list); 5088 } 5089 5090 /* Build builtin functions common to both C and C++ language 5091 frontends. */ 5092 5093 static void 5094 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node) 5095 { 5096 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \ 5097 builtin_types[ENUM] = VALUE; 5098 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \ 5099 def_fn_type (ENUM, RETURN, 0, 0); 5100 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \ 5101 def_fn_type (ENUM, RETURN, 0, 1, ARG1); 5102 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \ 5103 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2); 5104 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ 5105 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3); 5106 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ 5107 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4); 5108 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ 5109 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5); 5110 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ 5111 ARG6) \ 5112 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6); 5113 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ 5114 ARG6, ARG7) \ 5115 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7); 5116 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \ 5117 def_fn_type (ENUM, RETURN, 1, 0); 5118 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \ 5119 def_fn_type (ENUM, RETURN, 1, 1, ARG1); 5120 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \ 5121 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2); 5122 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ 5123 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3); 5124 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ 5125 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4); 5126 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ 5127 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5); 5128 #define DEF_POINTER_TYPE(ENUM, TYPE) \ 5129 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]); 5130 5131 #include "builtin-types.def" 5132 5133 #undef DEF_PRIMITIVE_TYPE 5134 #undef DEF_FUNCTION_TYPE_1 5135 #undef DEF_FUNCTION_TYPE_2 5136 #undef DEF_FUNCTION_TYPE_3 5137 #undef DEF_FUNCTION_TYPE_4 5138 #undef DEF_FUNCTION_TYPE_5 5139 #undef DEF_FUNCTION_TYPE_6 5140 #undef DEF_FUNCTION_TYPE_VAR_0 5141 #undef DEF_FUNCTION_TYPE_VAR_1 5142 #undef DEF_FUNCTION_TYPE_VAR_2 5143 #undef DEF_FUNCTION_TYPE_VAR_3 5144 #undef DEF_FUNCTION_TYPE_VAR_4 5145 #undef DEF_FUNCTION_TYPE_VAR_5 5146 #undef DEF_POINTER_TYPE 5147 builtin_types[(int) BT_LAST] = NULL_TREE; 5148 5149 c_init_attributes (); 5150 5151 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \ 5152 NONANSI_P, ATTRS, IMPLICIT, COND) \ 5153 if (NAME && COND) \ 5154 def_builtin_1 (ENUM, NAME, CLASS, \ 5155 builtin_types[(int) TYPE], \ 5156 builtin_types[(int) LIBTYPE], \ 5157 BOTH_P, FALLBACK_P, NONANSI_P, \ 5158 built_in_attributes[(int) ATTRS], IMPLICIT); 5159 #include "builtins.def" 5160 #undef DEF_BUILTIN 5161 5162 targetm.init_builtins (); 5163 5164 build_common_builtin_nodes (); 5165 5166 if (flag_mudflap) 5167 mudflap_init (); 5168 } 5169 5170 /* Like get_identifier, but avoid warnings about null arguments when 5171 the argument may be NULL for targets where GCC lacks stdint.h type 5172 information. */ 5173 5174 static inline tree 5175 c_get_ident (const char *id) 5176 { 5177 return get_identifier (id); 5178 } 5179 5180 /* Build tree nodes and builtin functions common to both C and C++ language 5181 frontends. */ 5182 5183 void 5184 c_common_nodes_and_builtins (void) 5185 { 5186 int char16_type_size; 5187 int char32_type_size; 5188 int wchar_type_size; 5189 tree array_domain_type; 5190 tree va_list_ref_type_node; 5191 tree va_list_arg_type_node; 5192 5193 build_common_tree_nodes (flag_signed_char, flag_short_double); 5194 5195 /* Define `int' and `char' first so that dbx will output them first. */ 5196 record_builtin_type (RID_INT, NULL, integer_type_node); 5197 record_builtin_type (RID_CHAR, "char", char_type_node); 5198 5199 /* `signed' is the same as `int'. FIXME: the declarations of "signed", 5200 "unsigned long", "long long unsigned" and "unsigned short" were in C++ 5201 but not C. Are the conditionals here needed? */ 5202 if (c_dialect_cxx ()) 5203 record_builtin_type (RID_SIGNED, NULL, integer_type_node); 5204 record_builtin_type (RID_LONG, "long int", long_integer_type_node); 5205 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node); 5206 record_builtin_type (RID_MAX, "long unsigned int", 5207 long_unsigned_type_node); 5208 if (int128_integer_type_node != NULL_TREE) 5209 { 5210 record_builtin_type (RID_INT128, "__int128", 5211 int128_integer_type_node); 5212 record_builtin_type (RID_MAX, "__int128 unsigned", 5213 int128_unsigned_type_node); 5214 } 5215 if (c_dialect_cxx ()) 5216 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node); 5217 record_builtin_type (RID_MAX, "long long int", 5218 long_long_integer_type_node); 5219 record_builtin_type (RID_MAX, "long long unsigned int", 5220 long_long_unsigned_type_node); 5221 if (c_dialect_cxx ()) 5222 record_builtin_type (RID_MAX, "long long unsigned", 5223 long_long_unsigned_type_node); 5224 record_builtin_type (RID_SHORT, "short int", short_integer_type_node); 5225 record_builtin_type (RID_MAX, "short unsigned int", 5226 short_unsigned_type_node); 5227 if (c_dialect_cxx ()) 5228 record_builtin_type (RID_MAX, "unsigned short", 5229 short_unsigned_type_node); 5230 5231 /* Define both `signed char' and `unsigned char'. */ 5232 record_builtin_type (RID_MAX, "signed char", signed_char_type_node); 5233 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node); 5234 5235 /* These are types that c_common_type_for_size and 5236 c_common_type_for_mode use. */ 5237 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5238 TYPE_DECL, NULL_TREE, 5239 intQI_type_node)); 5240 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5241 TYPE_DECL, NULL_TREE, 5242 intHI_type_node)); 5243 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5244 TYPE_DECL, NULL_TREE, 5245 intSI_type_node)); 5246 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5247 TYPE_DECL, NULL_TREE, 5248 intDI_type_node)); 5249 #if HOST_BITS_PER_WIDE_INT >= 64 5250 if (targetm.scalar_mode_supported_p (TImode)) 5251 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5252 TYPE_DECL, 5253 get_identifier ("__int128_t"), 5254 intTI_type_node)); 5255 #endif 5256 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5257 TYPE_DECL, NULL_TREE, 5258 unsigned_intQI_type_node)); 5259 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5260 TYPE_DECL, NULL_TREE, 5261 unsigned_intHI_type_node)); 5262 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5263 TYPE_DECL, NULL_TREE, 5264 unsigned_intSI_type_node)); 5265 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5266 TYPE_DECL, NULL_TREE, 5267 unsigned_intDI_type_node)); 5268 #if HOST_BITS_PER_WIDE_INT >= 64 5269 if (targetm.scalar_mode_supported_p (TImode)) 5270 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5271 TYPE_DECL, 5272 get_identifier ("__uint128_t"), 5273 unsigned_intTI_type_node)); 5274 #endif 5275 5276 /* Create the widest literal types. */ 5277 widest_integer_literal_type_node 5278 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2); 5279 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5280 TYPE_DECL, NULL_TREE, 5281 widest_integer_literal_type_node)); 5282 5283 widest_unsigned_literal_type_node 5284 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2); 5285 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5286 TYPE_DECL, NULL_TREE, 5287 widest_unsigned_literal_type_node)); 5288 5289 signed_size_type_node = c_common_signed_type (size_type_node); 5290 5291 pid_type_node = 5292 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE))); 5293 5294 record_builtin_type (RID_FLOAT, NULL, float_type_node); 5295 record_builtin_type (RID_DOUBLE, NULL, double_type_node); 5296 record_builtin_type (RID_MAX, "long double", long_double_type_node); 5297 5298 /* Only supported decimal floating point extension if the target 5299 actually supports underlying modes. */ 5300 if (targetm.scalar_mode_supported_p (SDmode) 5301 && targetm.scalar_mode_supported_p (DDmode) 5302 && targetm.scalar_mode_supported_p (TDmode)) 5303 { 5304 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node); 5305 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node); 5306 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node); 5307 } 5308 5309 if (targetm.fixed_point_supported_p ()) 5310 { 5311 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node); 5312 record_builtin_type (RID_FRACT, NULL, fract_type_node); 5313 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node); 5314 record_builtin_type (RID_MAX, "long long _Fract", 5315 long_long_fract_type_node); 5316 record_builtin_type (RID_MAX, "unsigned short _Fract", 5317 unsigned_short_fract_type_node); 5318 record_builtin_type (RID_MAX, "unsigned _Fract", 5319 unsigned_fract_type_node); 5320 record_builtin_type (RID_MAX, "unsigned long _Fract", 5321 unsigned_long_fract_type_node); 5322 record_builtin_type (RID_MAX, "unsigned long long _Fract", 5323 unsigned_long_long_fract_type_node); 5324 record_builtin_type (RID_MAX, "_Sat short _Fract", 5325 sat_short_fract_type_node); 5326 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node); 5327 record_builtin_type (RID_MAX, "_Sat long _Fract", 5328 sat_long_fract_type_node); 5329 record_builtin_type (RID_MAX, "_Sat long long _Fract", 5330 sat_long_long_fract_type_node); 5331 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract", 5332 sat_unsigned_short_fract_type_node); 5333 record_builtin_type (RID_MAX, "_Sat unsigned _Fract", 5334 sat_unsigned_fract_type_node); 5335 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract", 5336 sat_unsigned_long_fract_type_node); 5337 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract", 5338 sat_unsigned_long_long_fract_type_node); 5339 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node); 5340 record_builtin_type (RID_ACCUM, NULL, accum_type_node); 5341 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node); 5342 record_builtin_type (RID_MAX, "long long _Accum", 5343 long_long_accum_type_node); 5344 record_builtin_type (RID_MAX, "unsigned short _Accum", 5345 unsigned_short_accum_type_node); 5346 record_builtin_type (RID_MAX, "unsigned _Accum", 5347 unsigned_accum_type_node); 5348 record_builtin_type (RID_MAX, "unsigned long _Accum", 5349 unsigned_long_accum_type_node); 5350 record_builtin_type (RID_MAX, "unsigned long long _Accum", 5351 unsigned_long_long_accum_type_node); 5352 record_builtin_type (RID_MAX, "_Sat short _Accum", 5353 sat_short_accum_type_node); 5354 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node); 5355 record_builtin_type (RID_MAX, "_Sat long _Accum", 5356 sat_long_accum_type_node); 5357 record_builtin_type (RID_MAX, "_Sat long long _Accum", 5358 sat_long_long_accum_type_node); 5359 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum", 5360 sat_unsigned_short_accum_type_node); 5361 record_builtin_type (RID_MAX, "_Sat unsigned _Accum", 5362 sat_unsigned_accum_type_node); 5363 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum", 5364 sat_unsigned_long_accum_type_node); 5365 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum", 5366 sat_unsigned_long_long_accum_type_node); 5367 5368 } 5369 5370 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5371 TYPE_DECL, 5372 get_identifier ("complex int"), 5373 complex_integer_type_node)); 5374 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5375 TYPE_DECL, 5376 get_identifier ("complex float"), 5377 complex_float_type_node)); 5378 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, 5379 TYPE_DECL, 5380 get_identifier ("complex double"), 5381 complex_double_type_node)); 5382 lang_hooks.decls.pushdecl 5383 (build_decl (UNKNOWN_LOCATION, 5384 TYPE_DECL, get_identifier ("complex long double"), 5385 complex_long_double_type_node)); 5386 5387 if (c_dialect_cxx ()) 5388 /* For C++, make fileptr_type_node a distinct void * type until 5389 FILE type is defined. */ 5390 fileptr_type_node = build_variant_type_copy (ptr_type_node); 5391 5392 record_builtin_type (RID_VOID, NULL, void_type_node); 5393 5394 /* Set the TYPE_NAME for any variants that were built before 5395 record_builtin_type gave names to the built-in types. */ 5396 { 5397 tree void_name = TYPE_NAME (void_type_node); 5398 TYPE_NAME (void_type_node) = NULL_TREE; 5399 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST)) 5400 = void_name; 5401 TYPE_NAME (void_type_node) = void_name; 5402 } 5403 5404 /* This node must not be shared. */ 5405 void_zero_node = make_node (INTEGER_CST); 5406 TREE_TYPE (void_zero_node) = void_type_node; 5407 5408 void_list_node = build_void_list_node (); 5409 5410 /* Make a type to be the domain of a few array types 5411 whose domains don't really matter. 5412 200 is small enough that it always fits in size_t 5413 and large enough that it can hold most function names for the 5414 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */ 5415 array_domain_type = build_index_type (size_int (200)); 5416 5417 /* Make a type for arrays of characters. 5418 With luck nothing will ever really depend on the length of this 5419 array type. */ 5420 char_array_type_node 5421 = build_array_type (char_type_node, array_domain_type); 5422 5423 /* Likewise for arrays of ints. */ 5424 int_array_type_node 5425 = build_array_type (integer_type_node, array_domain_type); 5426 5427 string_type_node = build_pointer_type (char_type_node); 5428 const_string_type_node 5429 = build_pointer_type (build_qualified_type 5430 (char_type_node, TYPE_QUAL_CONST)); 5431 5432 /* This is special for C++ so functions can be overloaded. */ 5433 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE); 5434 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node)); 5435 wchar_type_size = TYPE_PRECISION (wchar_type_node); 5436 underlying_wchar_type_node = wchar_type_node; 5437 if (c_dialect_cxx ()) 5438 { 5439 if (TYPE_UNSIGNED (wchar_type_node)) 5440 wchar_type_node = make_unsigned_type (wchar_type_size); 5441 else 5442 wchar_type_node = make_signed_type (wchar_type_size); 5443 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node); 5444 } 5445 5446 /* This is for wide string constants. */ 5447 wchar_array_type_node 5448 = build_array_type (wchar_type_node, array_domain_type); 5449 5450 /* Define 'char16_t'. */ 5451 char16_type_node = get_identifier (CHAR16_TYPE); 5452 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node)); 5453 char16_type_size = TYPE_PRECISION (char16_type_node); 5454 if (c_dialect_cxx ()) 5455 { 5456 char16_type_node = make_unsigned_type (char16_type_size); 5457 5458 if (cxx_dialect >= cxx0x) 5459 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node); 5460 } 5461 5462 /* This is for UTF-16 string constants. */ 5463 char16_array_type_node 5464 = build_array_type (char16_type_node, array_domain_type); 5465 5466 /* Define 'char32_t'. */ 5467 char32_type_node = get_identifier (CHAR32_TYPE); 5468 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node)); 5469 char32_type_size = TYPE_PRECISION (char32_type_node); 5470 if (c_dialect_cxx ()) 5471 { 5472 char32_type_node = make_unsigned_type (char32_type_size); 5473 5474 if (cxx_dialect >= cxx0x) 5475 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node); 5476 } 5477 5478 /* This is for UTF-32 string constants. */ 5479 char32_array_type_node 5480 = build_array_type (char32_type_node, array_domain_type); 5481 5482 wint_type_node = 5483 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE))); 5484 5485 intmax_type_node = 5486 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE))); 5487 uintmax_type_node = 5488 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE))); 5489 5490 if (SIG_ATOMIC_TYPE) 5491 sig_atomic_type_node = 5492 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE))); 5493 if (INT8_TYPE) 5494 int8_type_node = 5495 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE))); 5496 if (INT16_TYPE) 5497 int16_type_node = 5498 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE))); 5499 if (INT32_TYPE) 5500 int32_type_node = 5501 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE))); 5502 if (INT64_TYPE) 5503 int64_type_node = 5504 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE))); 5505 if (UINT8_TYPE) 5506 uint8_type_node = 5507 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE))); 5508 if (UINT16_TYPE) 5509 c_uint16_type_node = uint16_type_node = 5510 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE))); 5511 if (UINT32_TYPE) 5512 c_uint32_type_node = uint32_type_node = 5513 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE))); 5514 if (UINT64_TYPE) 5515 c_uint64_type_node = uint64_type_node = 5516 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE))); 5517 if (INT_LEAST8_TYPE) 5518 int_least8_type_node = 5519 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE))); 5520 if (INT_LEAST16_TYPE) 5521 int_least16_type_node = 5522 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE))); 5523 if (INT_LEAST32_TYPE) 5524 int_least32_type_node = 5525 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE))); 5526 if (INT_LEAST64_TYPE) 5527 int_least64_type_node = 5528 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE))); 5529 if (UINT_LEAST8_TYPE) 5530 uint_least8_type_node = 5531 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE))); 5532 if (UINT_LEAST16_TYPE) 5533 uint_least16_type_node = 5534 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE))); 5535 if (UINT_LEAST32_TYPE) 5536 uint_least32_type_node = 5537 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE))); 5538 if (UINT_LEAST64_TYPE) 5539 uint_least64_type_node = 5540 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE))); 5541 if (INT_FAST8_TYPE) 5542 int_fast8_type_node = 5543 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE))); 5544 if (INT_FAST16_TYPE) 5545 int_fast16_type_node = 5546 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE))); 5547 if (INT_FAST32_TYPE) 5548 int_fast32_type_node = 5549 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE))); 5550 if (INT_FAST64_TYPE) 5551 int_fast64_type_node = 5552 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE))); 5553 if (UINT_FAST8_TYPE) 5554 uint_fast8_type_node = 5555 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE))); 5556 if (UINT_FAST16_TYPE) 5557 uint_fast16_type_node = 5558 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE))); 5559 if (UINT_FAST32_TYPE) 5560 uint_fast32_type_node = 5561 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE))); 5562 if (UINT_FAST64_TYPE) 5563 uint_fast64_type_node = 5564 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE))); 5565 if (INTPTR_TYPE) 5566 intptr_type_node = 5567 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE))); 5568 if (UINTPTR_TYPE) 5569 uintptr_type_node = 5570 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE))); 5571 5572 default_function_type 5573 = build_varargs_function_type_list (integer_type_node, NULL_TREE); 5574 ptrdiff_type_node 5575 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE))); 5576 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node); 5577 5578 lang_hooks.decls.pushdecl 5579 (build_decl (UNKNOWN_LOCATION, 5580 TYPE_DECL, get_identifier ("__builtin_va_list"), 5581 va_list_type_node)); 5582 if (targetm.enum_va_list_p) 5583 { 5584 int l; 5585 const char *pname; 5586 tree ptype; 5587 5588 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l) 5589 { 5590 lang_hooks.decls.pushdecl 5591 (build_decl (UNKNOWN_LOCATION, 5592 TYPE_DECL, get_identifier (pname), 5593 ptype)); 5594 5595 } 5596 } 5597 5598 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) 5599 { 5600 va_list_arg_type_node = va_list_ref_type_node = 5601 build_pointer_type (TREE_TYPE (va_list_type_node)); 5602 } 5603 else 5604 { 5605 va_list_arg_type_node = va_list_type_node; 5606 va_list_ref_type_node = build_reference_type (va_list_type_node); 5607 } 5608 5609 if (!flag_preprocess_only) 5610 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node); 5611 5612 main_identifier_node = get_identifier ("main"); 5613 5614 /* Create the built-in __null node. It is important that this is 5615 not shared. */ 5616 null_node = make_node (INTEGER_CST); 5617 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0); 5618 5619 /* Since builtin_types isn't gc'ed, don't export these nodes. */ 5620 memset (builtin_types, 0, sizeof (builtin_types)); 5621 } 5622 5623 /* The number of named compound-literals generated thus far. */ 5624 static GTY(()) int compound_literal_number; 5625 5626 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */ 5627 5628 void 5629 set_compound_literal_name (tree decl) 5630 { 5631 char *name; 5632 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal", 5633 compound_literal_number); 5634 compound_literal_number++; 5635 DECL_NAME (decl) = get_identifier (name); 5636 } 5637 5638 tree 5639 build_va_arg (location_t loc, tree expr, tree type) 5640 { 5641 expr = build1 (VA_ARG_EXPR, type, expr); 5642 SET_EXPR_LOCATION (expr, loc); 5643 return expr; 5644 } 5645 5646 5647 /* Linked list of disabled built-in functions. */ 5648 5649 typedef struct disabled_builtin 5650 { 5651 const char *name; 5652 struct disabled_builtin *next; 5653 } disabled_builtin; 5654 static disabled_builtin *disabled_builtins = NULL; 5655 5656 static bool builtin_function_disabled_p (const char *); 5657 5658 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME 5659 begins with "__builtin_", give an error. */ 5660 5661 void 5662 disable_builtin_function (const char *name) 5663 { 5664 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0) 5665 error ("cannot disable built-in function %qs", name); 5666 else 5667 { 5668 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin); 5669 new_disabled_builtin->name = name; 5670 new_disabled_builtin->next = disabled_builtins; 5671 disabled_builtins = new_disabled_builtin; 5672 } 5673 } 5674 5675 5676 /* Return true if the built-in function NAME has been disabled, false 5677 otherwise. */ 5678 5679 static bool 5680 builtin_function_disabled_p (const char *name) 5681 { 5682 disabled_builtin *p; 5683 for (p = disabled_builtins; p != NULL; p = p->next) 5684 { 5685 if (strcmp (name, p->name) == 0) 5686 return true; 5687 } 5688 return false; 5689 } 5690 5691 5692 /* Worker for DEF_BUILTIN. 5693 Possibly define a builtin function with one or two names. 5694 Does not declare a non-__builtin_ function if flag_no_builtin, or if 5695 nonansi_p and flag_no_nonansi_builtin. */ 5696 5697 static void 5698 def_builtin_1 (enum built_in_function fncode, 5699 const char *name, 5700 enum built_in_class fnclass, 5701 tree fntype, tree libtype, 5702 bool both_p, bool fallback_p, bool nonansi_p, 5703 tree fnattrs, bool implicit_p) 5704 { 5705 tree decl; 5706 const char *libname; 5707 5708 if (fntype == error_mark_node) 5709 return; 5710 5711 gcc_assert ((!both_p && !fallback_p) 5712 || !strncmp (name, "__builtin_", 5713 strlen ("__builtin_"))); 5714 5715 libname = name + strlen ("__builtin_"); 5716 decl = add_builtin_function (name, fntype, fncode, fnclass, 5717 (fallback_p ? libname : NULL), 5718 fnattrs); 5719 5720 set_builtin_decl (fncode, decl, implicit_p); 5721 5722 if (both_p 5723 && !flag_no_builtin && !builtin_function_disabled_p (libname) 5724 && !(nonansi_p && flag_no_nonansi_builtin)) 5725 add_builtin_function (libname, libtype, fncode, fnclass, 5726 NULL, fnattrs); 5727 } 5728 5729 /* Nonzero if the type T promotes to int. This is (nearly) the 5730 integral promotions defined in ISO C99 6.3.1.1/2. */ 5731 5732 bool 5733 c_promoting_integer_type_p (const_tree t) 5734 { 5735 switch (TREE_CODE (t)) 5736 { 5737 case INTEGER_TYPE: 5738 return (TYPE_MAIN_VARIANT (t) == char_type_node 5739 || TYPE_MAIN_VARIANT (t) == signed_char_type_node 5740 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node 5741 || TYPE_MAIN_VARIANT (t) == short_integer_type_node 5742 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node 5743 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node)); 5744 5745 case ENUMERAL_TYPE: 5746 /* ??? Technically all enumerations not larger than an int 5747 promote to an int. But this is used along code paths 5748 that only want to notice a size change. */ 5749 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node); 5750 5751 case BOOLEAN_TYPE: 5752 return 1; 5753 5754 default: 5755 return 0; 5756 } 5757 } 5758 5759 /* Return 1 if PARMS specifies a fixed number of parameters 5760 and none of their types is affected by default promotions. */ 5761 5762 int 5763 self_promoting_args_p (const_tree parms) 5764 { 5765 const_tree t; 5766 for (t = parms; t; t = TREE_CHAIN (t)) 5767 { 5768 tree type = TREE_VALUE (t); 5769 5770 if (type == error_mark_node) 5771 continue; 5772 5773 if (TREE_CHAIN (t) == 0 && type != void_type_node) 5774 return 0; 5775 5776 if (type == 0) 5777 return 0; 5778 5779 if (TYPE_MAIN_VARIANT (type) == float_type_node) 5780 return 0; 5781 5782 if (c_promoting_integer_type_p (type)) 5783 return 0; 5784 } 5785 return 1; 5786 } 5787 5788 /* Recursively remove any '*' or '&' operator from TYPE. */ 5789 tree 5790 strip_pointer_operator (tree t) 5791 { 5792 while (POINTER_TYPE_P (t)) 5793 t = TREE_TYPE (t); 5794 return t; 5795 } 5796 5797 /* Recursively remove pointer or array type from TYPE. */ 5798 tree 5799 strip_pointer_or_array_types (tree t) 5800 { 5801 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t)) 5802 t = TREE_TYPE (t); 5803 return t; 5804 } 5805 5806 /* Used to compare case labels. K1 and K2 are actually tree nodes 5807 representing case labels, or NULL_TREE for a `default' label. 5808 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after 5809 K2, and 0 if K1 and K2 are equal. */ 5810 5811 int 5812 case_compare (splay_tree_key k1, splay_tree_key k2) 5813 { 5814 /* Consider a NULL key (such as arises with a `default' label) to be 5815 smaller than anything else. */ 5816 if (!k1) 5817 return k2 ? -1 : 0; 5818 else if (!k2) 5819 return k1 ? 1 : 0; 5820 5821 return tree_int_cst_compare ((tree) k1, (tree) k2); 5822 } 5823 5824 /* Process a case label, located at LOC, for the range LOW_VALUE 5825 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE 5826 then this case label is actually a `default' label. If only 5827 HIGH_VALUE is NULL_TREE, then case label was declared using the 5828 usual C/C++ syntax, rather than the GNU case range extension. 5829 CASES is a tree containing all the case ranges processed so far; 5830 COND is the condition for the switch-statement itself. Returns the 5831 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR 5832 is created. */ 5833 5834 tree 5835 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type, 5836 tree low_value, tree high_value) 5837 { 5838 tree type; 5839 tree label; 5840 tree case_label; 5841 splay_tree_node node; 5842 5843 /* Create the LABEL_DECL itself. */ 5844 label = create_artificial_label (loc); 5845 5846 /* If there was an error processing the switch condition, bail now 5847 before we get more confused. */ 5848 if (!cond || cond == error_mark_node) 5849 goto error_out; 5850 5851 if ((low_value && TREE_TYPE (low_value) 5852 && POINTER_TYPE_P (TREE_TYPE (low_value))) 5853 || (high_value && TREE_TYPE (high_value) 5854 && POINTER_TYPE_P (TREE_TYPE (high_value)))) 5855 { 5856 error_at (loc, "pointers are not permitted as case values"); 5857 goto error_out; 5858 } 5859 5860 /* Case ranges are a GNU extension. */ 5861 if (high_value) 5862 pedwarn (loc, OPT_Wpedantic, 5863 "range expressions in switch statements are non-standard"); 5864 5865 type = TREE_TYPE (cond); 5866 if (low_value) 5867 { 5868 low_value = check_case_value (low_value); 5869 low_value = convert_and_check (type, low_value); 5870 if (low_value == error_mark_node) 5871 goto error_out; 5872 } 5873 if (high_value) 5874 { 5875 high_value = check_case_value (high_value); 5876 high_value = convert_and_check (type, high_value); 5877 if (high_value == error_mark_node) 5878 goto error_out; 5879 } 5880 5881 if (low_value && high_value) 5882 { 5883 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't 5884 really a case range, even though it was written that way. 5885 Remove the HIGH_VALUE to simplify later processing. */ 5886 if (tree_int_cst_equal (low_value, high_value)) 5887 high_value = NULL_TREE; 5888 else if (!tree_int_cst_lt (low_value, high_value)) 5889 warning_at (loc, 0, "empty range specified"); 5890 } 5891 5892 /* See if the case is in range of the type of the original testing 5893 expression. If both low_value and high_value are out of range, 5894 don't insert the case label and return NULL_TREE. */ 5895 if (low_value 5896 && !check_case_bounds (type, orig_type, 5897 &low_value, high_value ? &high_value : NULL)) 5898 return NULL_TREE; 5899 5900 /* Look up the LOW_VALUE in the table of case labels we already 5901 have. */ 5902 node = splay_tree_lookup (cases, (splay_tree_key) low_value); 5903 /* If there was not an exact match, check for overlapping ranges. 5904 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE; 5905 that's a `default' label and the only overlap is an exact match. */ 5906 if (!node && (low_value || high_value)) 5907 { 5908 splay_tree_node low_bound; 5909 splay_tree_node high_bound; 5910 5911 /* Even though there wasn't an exact match, there might be an 5912 overlap between this case range and another case range. 5913 Since we've (inductively) not allowed any overlapping case 5914 ranges, we simply need to find the greatest low case label 5915 that is smaller that LOW_VALUE, and the smallest low case 5916 label that is greater than LOW_VALUE. If there is an overlap 5917 it will occur in one of these two ranges. */ 5918 low_bound = splay_tree_predecessor (cases, 5919 (splay_tree_key) low_value); 5920 high_bound = splay_tree_successor (cases, 5921 (splay_tree_key) low_value); 5922 5923 /* Check to see if the LOW_BOUND overlaps. It is smaller than 5924 the LOW_VALUE, so there is no need to check unless the 5925 LOW_BOUND is in fact itself a case range. */ 5926 if (low_bound 5927 && CASE_HIGH ((tree) low_bound->value) 5928 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value), 5929 low_value) >= 0) 5930 node = low_bound; 5931 /* Check to see if the HIGH_BOUND overlaps. The low end of that 5932 range is bigger than the low end of the current range, so we 5933 are only interested if the current range is a real range, and 5934 not an ordinary case label. */ 5935 else if (high_bound 5936 && high_value 5937 && (tree_int_cst_compare ((tree) high_bound->key, 5938 high_value) 5939 <= 0)) 5940 node = high_bound; 5941 } 5942 /* If there was an overlap, issue an error. */ 5943 if (node) 5944 { 5945 tree duplicate = CASE_LABEL ((tree) node->value); 5946 5947 if (high_value) 5948 { 5949 error_at (loc, "duplicate (or overlapping) case value"); 5950 error_at (DECL_SOURCE_LOCATION (duplicate), 5951 "this is the first entry overlapping that value"); 5952 } 5953 else if (low_value) 5954 { 5955 error_at (loc, "duplicate case value") ; 5956 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here"); 5957 } 5958 else 5959 { 5960 error_at (loc, "multiple default labels in one switch"); 5961 error_at (DECL_SOURCE_LOCATION (duplicate), 5962 "this is the first default label"); 5963 } 5964 goto error_out; 5965 } 5966 5967 /* Add a CASE_LABEL to the statement-tree. */ 5968 case_label = add_stmt (build_case_label (low_value, high_value, label)); 5969 /* Register this case label in the splay tree. */ 5970 splay_tree_insert (cases, 5971 (splay_tree_key) low_value, 5972 (splay_tree_value) case_label); 5973 5974 return case_label; 5975 5976 error_out: 5977 /* Add a label so that the back-end doesn't think that the beginning of 5978 the switch is unreachable. Note that we do not add a case label, as 5979 that just leads to duplicates and thence to failure later on. */ 5980 if (!cases->root) 5981 { 5982 tree t = create_artificial_label (loc); 5983 add_stmt (build_stmt (loc, LABEL_EXPR, t)); 5984 } 5985 return error_mark_node; 5986 } 5987 5988 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach. 5989 Used to verify that case values match up with enumerator values. */ 5990 5991 static void 5992 match_case_to_enum_1 (tree key, tree type, tree label) 5993 { 5994 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1]; 5995 5996 /* ??? Not working too hard to print the double-word value. 5997 Should perhaps be done with %lwd in the diagnostic routines? */ 5998 if (TREE_INT_CST_HIGH (key) == 0) 5999 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED, 6000 TREE_INT_CST_LOW (key)); 6001 else if (!TYPE_UNSIGNED (type) 6002 && TREE_INT_CST_HIGH (key) == -1 6003 && TREE_INT_CST_LOW (key) != 0) 6004 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED, 6005 -TREE_INT_CST_LOW (key)); 6006 else 6007 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX, 6008 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key), 6009 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key)); 6010 6011 if (TYPE_NAME (type) == 0) 6012 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), 6013 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, 6014 "case value %qs not in enumerated type", 6015 buf); 6016 else 6017 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), 6018 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, 6019 "case value %qs not in enumerated type %qT", 6020 buf, type); 6021 } 6022 6023 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach. 6024 Used to verify that case values match up with enumerator values. */ 6025 6026 static int 6027 match_case_to_enum (splay_tree_node node, void *data) 6028 { 6029 tree label = (tree) node->value; 6030 tree type = (tree) data; 6031 6032 /* Skip default case. */ 6033 if (!CASE_LOW (label)) 6034 return 0; 6035 6036 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear 6037 when we did our enum->case scan. Reset our scratch bit after. */ 6038 if (!CASE_LOW_SEEN (label)) 6039 match_case_to_enum_1 (CASE_LOW (label), type, label); 6040 else 6041 CASE_LOW_SEEN (label) = 0; 6042 6043 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is 6044 not set, that means that CASE_HIGH did not appear when we did our 6045 enum->case scan. Reset our scratch bit after. */ 6046 if (CASE_HIGH (label)) 6047 { 6048 if (!CASE_HIGH_SEEN (label)) 6049 match_case_to_enum_1 (CASE_HIGH (label), type, label); 6050 else 6051 CASE_HIGH_SEEN (label) = 0; 6052 } 6053 6054 return 0; 6055 } 6056 6057 /* Handle -Wswitch*. Called from the front end after parsing the 6058 switch construct. */ 6059 /* ??? Should probably be somewhere generic, since other languages 6060 besides C and C++ would want this. At the moment, however, C/C++ 6061 are the only tree-ssa languages that support enumerations at all, 6062 so the point is moot. */ 6063 6064 void 6065 c_do_switch_warnings (splay_tree cases, location_t switch_location, 6066 tree type, tree cond) 6067 { 6068 splay_tree_node default_node; 6069 splay_tree_node node; 6070 tree chain; 6071 6072 if (!warn_switch && !warn_switch_enum && !warn_switch_default) 6073 return; 6074 6075 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL); 6076 if (!default_node) 6077 warning_at (switch_location, OPT_Wswitch_default, 6078 "switch missing default case"); 6079 6080 /* From here on, we only care about about enumerated types. */ 6081 if (!type || TREE_CODE (type) != ENUMERAL_TYPE) 6082 return; 6083 6084 /* From here on, we only care about -Wswitch and -Wswitch-enum. */ 6085 if (!warn_switch_enum && !warn_switch) 6086 return; 6087 6088 /* Check the cases. Warn about case values which are not members of 6089 the enumerated type. For -Wswitch-enum, or for -Wswitch when 6090 there is no default case, check that exactly all enumeration 6091 literals are covered by the cases. */ 6092 6093 /* Clearing COND if it is not an integer constant simplifies 6094 the tests inside the loop below. */ 6095 if (TREE_CODE (cond) != INTEGER_CST) 6096 cond = NULL_TREE; 6097 6098 /* The time complexity here is O(N*lg(N)) worst case, but for the 6099 common case of monotonically increasing enumerators, it is 6100 O(N), since the nature of the splay tree will keep the next 6101 element adjacent to the root at all times. */ 6102 6103 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain)) 6104 { 6105 tree value = TREE_VALUE (chain); 6106 if (TREE_CODE (value) == CONST_DECL) 6107 value = DECL_INITIAL (value); 6108 node = splay_tree_lookup (cases, (splay_tree_key) value); 6109 if (node) 6110 { 6111 /* Mark the CASE_LOW part of the case entry as seen. */ 6112 tree label = (tree) node->value; 6113 CASE_LOW_SEEN (label) = 1; 6114 continue; 6115 } 6116 6117 /* Even though there wasn't an exact match, there might be a 6118 case range which includes the enumerator's value. */ 6119 node = splay_tree_predecessor (cases, (splay_tree_key) value); 6120 if (node && CASE_HIGH ((tree) node->value)) 6121 { 6122 tree label = (tree) node->value; 6123 int cmp = tree_int_cst_compare (CASE_HIGH (label), value); 6124 if (cmp >= 0) 6125 { 6126 /* If we match the upper bound exactly, mark the CASE_HIGH 6127 part of the case entry as seen. */ 6128 if (cmp == 0) 6129 CASE_HIGH_SEEN (label) = 1; 6130 continue; 6131 } 6132 } 6133 6134 /* We've now determined that this enumerated literal isn't 6135 handled by the case labels of the switch statement. */ 6136 6137 /* If the switch expression is a constant, we only really care 6138 about whether that constant is handled by the switch. */ 6139 if (cond && tree_int_cst_compare (cond, value)) 6140 continue; 6141 6142 /* If there is a default_node, the only relevant option is 6143 Wswitch-enum. Otherwise, if both are enabled then we prefer 6144 to warn using -Wswitch because -Wswitch is enabled by -Wall 6145 while -Wswitch-enum is explicit. */ 6146 warning_at (switch_location, 6147 (default_node || !warn_switch 6148 ? OPT_Wswitch_enum 6149 : OPT_Wswitch), 6150 "enumeration value %qE not handled in switch", 6151 TREE_PURPOSE (chain)); 6152 } 6153 6154 /* Warn if there are case expressions that don't correspond to 6155 enumerators. This can occur since C and C++ don't enforce 6156 type-checking of assignments to enumeration variables. 6157 6158 The time complexity here is now always O(N) worst case, since 6159 we should have marked both the lower bound and upper bound of 6160 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN 6161 above. This scan also resets those fields. */ 6162 6163 splay_tree_foreach (cases, match_case_to_enum, type); 6164 } 6165 6166 /* Finish an expression taking the address of LABEL (an 6167 IDENTIFIER_NODE). Returns an expression for the address. 6168 6169 LOC is the location for the expression returned. */ 6170 6171 tree 6172 finish_label_address_expr (tree label, location_t loc) 6173 { 6174 tree result; 6175 6176 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard"); 6177 6178 if (label == error_mark_node) 6179 return error_mark_node; 6180 6181 label = lookup_label (label); 6182 if (label == NULL_TREE) 6183 result = null_pointer_node; 6184 else 6185 { 6186 TREE_USED (label) = 1; 6187 result = build1 (ADDR_EXPR, ptr_type_node, label); 6188 /* The current function is not necessarily uninlinable. 6189 Computed gotos are incompatible with inlining, but the value 6190 here could be used only in a diagnostic, for example. */ 6191 protected_set_expr_location (result, loc); 6192 } 6193 6194 return result; 6195 } 6196 6197 6198 /* Given a boolean expression ARG, return a tree representing an increment 6199 or decrement (as indicated by CODE) of ARG. The front end must check for 6200 invalid cases (e.g., decrement in C++). */ 6201 tree 6202 boolean_increment (enum tree_code code, tree arg) 6203 { 6204 tree val; 6205 tree true_res = build_int_cst (TREE_TYPE (arg), 1); 6206 6207 arg = stabilize_reference (arg); 6208 switch (code) 6209 { 6210 case PREINCREMENT_EXPR: 6211 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); 6212 break; 6213 case POSTINCREMENT_EXPR: 6214 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); 6215 arg = save_expr (arg); 6216 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); 6217 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); 6218 break; 6219 case PREDECREMENT_EXPR: 6220 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, 6221 invert_truthvalue_loc (input_location, arg)); 6222 break; 6223 case POSTDECREMENT_EXPR: 6224 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, 6225 invert_truthvalue_loc (input_location, arg)); 6226 arg = save_expr (arg); 6227 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); 6228 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); 6229 break; 6230 default: 6231 gcc_unreachable (); 6232 } 6233 TREE_SIDE_EFFECTS (val) = 1; 6234 return val; 6235 } 6236 6237 /* Built-in macros for stddef.h and stdint.h, that require macros 6238 defined in this file. */ 6239 void 6240 c_stddef_cpp_builtins(void) 6241 { 6242 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0); 6243 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0); 6244 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0); 6245 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0); 6246 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0); 6247 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0); 6248 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0); 6249 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0); 6250 if (SIG_ATOMIC_TYPE) 6251 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0); 6252 if (INT8_TYPE) 6253 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0); 6254 if (INT16_TYPE) 6255 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0); 6256 if (INT32_TYPE) 6257 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0); 6258 if (INT64_TYPE) 6259 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0); 6260 if (UINT8_TYPE) 6261 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0); 6262 if (UINT16_TYPE) 6263 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0); 6264 if (UINT32_TYPE) 6265 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0); 6266 if (UINT64_TYPE) 6267 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0); 6268 if (INT_LEAST8_TYPE) 6269 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0); 6270 if (INT_LEAST16_TYPE) 6271 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0); 6272 if (INT_LEAST32_TYPE) 6273 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0); 6274 if (INT_LEAST64_TYPE) 6275 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0); 6276 if (UINT_LEAST8_TYPE) 6277 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0); 6278 if (UINT_LEAST16_TYPE) 6279 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0); 6280 if (UINT_LEAST32_TYPE) 6281 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0); 6282 if (UINT_LEAST64_TYPE) 6283 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0); 6284 if (INT_FAST8_TYPE) 6285 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0); 6286 if (INT_FAST16_TYPE) 6287 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0); 6288 if (INT_FAST32_TYPE) 6289 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0); 6290 if (INT_FAST64_TYPE) 6291 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0); 6292 if (UINT_FAST8_TYPE) 6293 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0); 6294 if (UINT_FAST16_TYPE) 6295 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0); 6296 if (UINT_FAST32_TYPE) 6297 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0); 6298 if (UINT_FAST64_TYPE) 6299 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0); 6300 if (INTPTR_TYPE) 6301 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0); 6302 if (UINTPTR_TYPE) 6303 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0); 6304 } 6305 6306 static void 6307 c_init_attributes (void) 6308 { 6309 /* Fill in the built_in_attributes array. */ 6310 #define DEF_ATTR_NULL_TREE(ENUM) \ 6311 built_in_attributes[(int) ENUM] = NULL_TREE; 6312 #define DEF_ATTR_INT(ENUM, VALUE) \ 6313 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE); 6314 #define DEF_ATTR_STRING(ENUM, VALUE) \ 6315 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE); 6316 #define DEF_ATTR_IDENT(ENUM, STRING) \ 6317 built_in_attributes[(int) ENUM] = get_identifier (STRING); 6318 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \ 6319 built_in_attributes[(int) ENUM] \ 6320 = tree_cons (built_in_attributes[(int) PURPOSE], \ 6321 built_in_attributes[(int) VALUE], \ 6322 built_in_attributes[(int) CHAIN]); 6323 #include "builtin-attrs.def" 6324 #undef DEF_ATTR_NULL_TREE 6325 #undef DEF_ATTR_INT 6326 #undef DEF_ATTR_IDENT 6327 #undef DEF_ATTR_TREE_LIST 6328 } 6329 6330 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain 6331 identifier as an argument, so the front end shouldn't look it up. */ 6332 6333 bool 6334 attribute_takes_identifier_p (const_tree attr_id) 6335 { 6336 const struct attribute_spec *spec = lookup_attribute_spec (attr_id); 6337 if (spec == NULL) 6338 /* Unknown attribute that we'll end up ignoring, return true so we 6339 don't complain about an identifier argument. */ 6340 return true; 6341 else if (!strcmp ("mode", spec->name) 6342 || !strcmp ("format", spec->name) 6343 || !strcmp ("cleanup", spec->name)) 6344 return true; 6345 else 6346 return targetm.attribute_takes_identifier_p (attr_id); 6347 } 6348 6349 /* Attribute handlers common to C front ends. */ 6350 6351 /* Handle a "packed" attribute; arguments as in 6352 struct attribute_spec.handler. */ 6353 6354 static tree 6355 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args), 6356 int flags, bool *no_add_attrs) 6357 { 6358 if (TYPE_P (*node)) 6359 { 6360 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) 6361 *node = build_variant_type_copy (*node); 6362 TYPE_PACKED (*node) = 1; 6363 } 6364 else if (TREE_CODE (*node) == FIELD_DECL) 6365 { 6366 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT 6367 /* Still pack bitfields. */ 6368 && ! DECL_INITIAL (*node)) 6369 warning (OPT_Wattributes, 6370 "%qE attribute ignored for field of type %qT", 6371 name, TREE_TYPE (*node)); 6372 else 6373 DECL_PACKED (*node) = 1; 6374 } 6375 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is 6376 used for DECL_REGISTER. It wouldn't mean anything anyway. 6377 We can't set DECL_PACKED on the type of a TYPE_DECL, because 6378 that changes what the typedef is typing. */ 6379 else 6380 { 6381 warning (OPT_Wattributes, "%qE attribute ignored", name); 6382 *no_add_attrs = true; 6383 } 6384 6385 return NULL_TREE; 6386 } 6387 6388 /* Handle a "nocommon" attribute; arguments as in 6389 struct attribute_spec.handler. */ 6390 6391 static tree 6392 handle_nocommon_attribute (tree *node, tree name, 6393 tree ARG_UNUSED (args), 6394 int ARG_UNUSED (flags), bool *no_add_attrs) 6395 { 6396 if (TREE_CODE (*node) == VAR_DECL) 6397 DECL_COMMON (*node) = 0; 6398 else 6399 { 6400 warning (OPT_Wattributes, "%qE attribute ignored", name); 6401 *no_add_attrs = true; 6402 } 6403 6404 return NULL_TREE; 6405 } 6406 6407 /* Handle a "common" attribute; arguments as in 6408 struct attribute_spec.handler. */ 6409 6410 static tree 6411 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args), 6412 int ARG_UNUSED (flags), bool *no_add_attrs) 6413 { 6414 if (TREE_CODE (*node) == VAR_DECL) 6415 DECL_COMMON (*node) = 1; 6416 else 6417 { 6418 warning (OPT_Wattributes, "%qE attribute ignored", name); 6419 *no_add_attrs = true; 6420 } 6421 6422 return NULL_TREE; 6423 } 6424 6425 /* Handle a "noreturn" attribute; arguments as in 6426 struct attribute_spec.handler. */ 6427 6428 static tree 6429 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args), 6430 int ARG_UNUSED (flags), bool *no_add_attrs) 6431 { 6432 tree type = TREE_TYPE (*node); 6433 6434 /* See FIXME comment in c_common_attribute_table. */ 6435 if (TREE_CODE (*node) == FUNCTION_DECL 6436 || objc_method_decl (TREE_CODE (*node))) 6437 TREE_THIS_VOLATILE (*node) = 1; 6438 else if (TREE_CODE (type) == POINTER_TYPE 6439 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) 6440 TREE_TYPE (*node) 6441 = build_pointer_type 6442 (build_type_variant (TREE_TYPE (type), 6443 TYPE_READONLY (TREE_TYPE (type)), 1)); 6444 else 6445 { 6446 warning (OPT_Wattributes, "%qE attribute ignored", name); 6447 *no_add_attrs = true; 6448 } 6449 6450 return NULL_TREE; 6451 } 6452 6453 /* Handle a "hot" and attribute; arguments as in 6454 struct attribute_spec.handler. */ 6455 6456 static tree 6457 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args), 6458 int ARG_UNUSED (flags), bool *no_add_attrs) 6459 { 6460 if (TREE_CODE (*node) == FUNCTION_DECL 6461 || TREE_CODE (*node) == LABEL_DECL) 6462 { 6463 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL) 6464 { 6465 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s", 6466 name, "cold"); 6467 *no_add_attrs = true; 6468 } 6469 /* Most of the rest of the hot processing is done later with 6470 lookup_attribute. */ 6471 } 6472 else 6473 { 6474 warning (OPT_Wattributes, "%qE attribute ignored", name); 6475 *no_add_attrs = true; 6476 } 6477 6478 return NULL_TREE; 6479 } 6480 6481 /* Handle a "cold" and attribute; arguments as in 6482 struct attribute_spec.handler. */ 6483 6484 static tree 6485 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args), 6486 int ARG_UNUSED (flags), bool *no_add_attrs) 6487 { 6488 if (TREE_CODE (*node) == FUNCTION_DECL 6489 || TREE_CODE (*node) == LABEL_DECL) 6490 { 6491 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL) 6492 { 6493 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s", 6494 name, "hot"); 6495 *no_add_attrs = true; 6496 } 6497 /* Most of the rest of the cold processing is done later with 6498 lookup_attribute. */ 6499 } 6500 else 6501 { 6502 warning (OPT_Wattributes, "%qE attribute ignored", name); 6503 *no_add_attrs = true; 6504 } 6505 6506 return NULL_TREE; 6507 } 6508 6509 /* Handle a "no_sanitize_address" attribute; arguments as in 6510 struct attribute_spec.handler. */ 6511 6512 static tree 6513 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int, 6514 bool *no_add_attrs) 6515 { 6516 if (TREE_CODE (*node) != FUNCTION_DECL) 6517 { 6518 warning (OPT_Wattributes, "%qE attribute ignored", name); 6519 *no_add_attrs = true; 6520 } 6521 6522 return NULL_TREE; 6523 } 6524 6525 /* Handle a "no_address_safety_analysis" attribute; arguments as in 6526 struct attribute_spec.handler. */ 6527 6528 static tree 6529 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int, 6530 bool *no_add_attrs) 6531 { 6532 if (TREE_CODE (*node) != FUNCTION_DECL) 6533 warning (OPT_Wattributes, "%qE attribute ignored", name); 6534 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node))) 6535 DECL_ATTRIBUTES (*node) 6536 = tree_cons (get_identifier ("no_sanitize_address"), 6537 NULL_TREE, DECL_ATTRIBUTES (*node)); 6538 *no_add_attrs = true; 6539 return NULL_TREE; 6540 } 6541 6542 /* Handle a "noinline" attribute; arguments as in 6543 struct attribute_spec.handler. */ 6544 6545 static tree 6546 handle_noinline_attribute (tree *node, tree name, 6547 tree ARG_UNUSED (args), 6548 int ARG_UNUSED (flags), bool *no_add_attrs) 6549 { 6550 if (TREE_CODE (*node) == FUNCTION_DECL) 6551 DECL_UNINLINABLE (*node) = 1; 6552 else 6553 { 6554 warning (OPT_Wattributes, "%qE attribute ignored", name); 6555 *no_add_attrs = true; 6556 } 6557 6558 return NULL_TREE; 6559 } 6560 6561 /* Handle a "noclone" attribute; arguments as in 6562 struct attribute_spec.handler. */ 6563 6564 static tree 6565 handle_noclone_attribute (tree *node, tree name, 6566 tree ARG_UNUSED (args), 6567 int ARG_UNUSED (flags), bool *no_add_attrs) 6568 { 6569 if (TREE_CODE (*node) != FUNCTION_DECL) 6570 { 6571 warning (OPT_Wattributes, "%qE attribute ignored", name); 6572 *no_add_attrs = true; 6573 } 6574 6575 return NULL_TREE; 6576 } 6577 6578 /* Handle a "always_inline" attribute; arguments as in 6579 struct attribute_spec.handler. */ 6580 6581 static tree 6582 handle_always_inline_attribute (tree *node, tree name, 6583 tree ARG_UNUSED (args), 6584 int ARG_UNUSED (flags), 6585 bool *no_add_attrs) 6586 { 6587 if (TREE_CODE (*node) == FUNCTION_DECL) 6588 { 6589 /* Set the attribute and mark it for disregarding inline 6590 limits. */ 6591 DECL_DISREGARD_INLINE_LIMITS (*node) = 1; 6592 } 6593 else 6594 { 6595 warning (OPT_Wattributes, "%qE attribute ignored", name); 6596 *no_add_attrs = true; 6597 } 6598 6599 return NULL_TREE; 6600 } 6601 6602 /* Handle a "gnu_inline" attribute; arguments as in 6603 struct attribute_spec.handler. */ 6604 6605 static tree 6606 handle_gnu_inline_attribute (tree *node, tree name, 6607 tree ARG_UNUSED (args), 6608 int ARG_UNUSED (flags), 6609 bool *no_add_attrs) 6610 { 6611 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) 6612 { 6613 /* Do nothing else, just set the attribute. We'll get at 6614 it later with lookup_attribute. */ 6615 } 6616 else 6617 { 6618 warning (OPT_Wattributes, "%qE attribute ignored", name); 6619 *no_add_attrs = true; 6620 } 6621 6622 return NULL_TREE; 6623 } 6624 6625 /* Handle a "leaf" attribute; arguments as in 6626 struct attribute_spec.handler. */ 6627 6628 static tree 6629 handle_leaf_attribute (tree *node, tree name, 6630 tree ARG_UNUSED (args), 6631 int ARG_UNUSED (flags), bool *no_add_attrs) 6632 { 6633 if (TREE_CODE (*node) != FUNCTION_DECL) 6634 { 6635 warning (OPT_Wattributes, "%qE attribute ignored", name); 6636 *no_add_attrs = true; 6637 } 6638 if (!TREE_PUBLIC (*node)) 6639 { 6640 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name); 6641 *no_add_attrs = true; 6642 } 6643 6644 return NULL_TREE; 6645 } 6646 6647 /* Handle an "artificial" attribute; arguments as in 6648 struct attribute_spec.handler. */ 6649 6650 static tree 6651 handle_artificial_attribute (tree *node, tree name, 6652 tree ARG_UNUSED (args), 6653 int ARG_UNUSED (flags), 6654 bool *no_add_attrs) 6655 { 6656 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) 6657 { 6658 /* Do nothing else, just set the attribute. We'll get at 6659 it later with lookup_attribute. */ 6660 } 6661 else 6662 { 6663 warning (OPT_Wattributes, "%qE attribute ignored", name); 6664 *no_add_attrs = true; 6665 } 6666 6667 return NULL_TREE; 6668 } 6669 6670 /* Handle a "flatten" attribute; arguments as in 6671 struct attribute_spec.handler. */ 6672 6673 static tree 6674 handle_flatten_attribute (tree *node, tree name, 6675 tree args ATTRIBUTE_UNUSED, 6676 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) 6677 { 6678 if (TREE_CODE (*node) == FUNCTION_DECL) 6679 /* Do nothing else, just set the attribute. We'll get at 6680 it later with lookup_attribute. */ 6681 ; 6682 else 6683 { 6684 warning (OPT_Wattributes, "%qE attribute ignored", name); 6685 *no_add_attrs = true; 6686 } 6687 6688 return NULL_TREE; 6689 } 6690 6691 /* Handle a "warning" or "error" attribute; arguments as in 6692 struct attribute_spec.handler. */ 6693 6694 static tree 6695 handle_error_attribute (tree *node, tree name, tree args, 6696 int ARG_UNUSED (flags), bool *no_add_attrs) 6697 { 6698 if (TREE_CODE (*node) == FUNCTION_DECL 6699 && TREE_CODE (TREE_VALUE (args)) == STRING_CST) 6700 /* Do nothing else, just set the attribute. We'll get at 6701 it later with lookup_attribute. */ 6702 ; 6703 else 6704 { 6705 warning (OPT_Wattributes, "%qE attribute ignored", name); 6706 *no_add_attrs = true; 6707 } 6708 6709 return NULL_TREE; 6710 } 6711 6712 /* Handle a "used" attribute; arguments as in 6713 struct attribute_spec.handler. */ 6714 6715 static tree 6716 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args), 6717 int ARG_UNUSED (flags), bool *no_add_attrs) 6718 { 6719 tree node = *pnode; 6720 6721 if (TREE_CODE (node) == FUNCTION_DECL 6722 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node)) 6723 || (TREE_CODE (node) == TYPE_DECL)) 6724 { 6725 TREE_USED (node) = 1; 6726 DECL_PRESERVE_P (node) = 1; 6727 if (TREE_CODE (node) == VAR_DECL) 6728 DECL_READ_P (node) = 1; 6729 } 6730 else 6731 { 6732 warning (OPT_Wattributes, "%qE attribute ignored", name); 6733 *no_add_attrs = true; 6734 } 6735 6736 return NULL_TREE; 6737 } 6738 6739 /* Handle a "unused" attribute; arguments as in 6740 struct attribute_spec.handler. */ 6741 6742 static tree 6743 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args), 6744 int flags, bool *no_add_attrs) 6745 { 6746 if (DECL_P (*node)) 6747 { 6748 tree decl = *node; 6749 6750 if (TREE_CODE (decl) == PARM_DECL 6751 || TREE_CODE (decl) == VAR_DECL 6752 || TREE_CODE (decl) == FUNCTION_DECL 6753 || TREE_CODE (decl) == LABEL_DECL 6754 || TREE_CODE (decl) == TYPE_DECL) 6755 { 6756 TREE_USED (decl) = 1; 6757 if (TREE_CODE (decl) == VAR_DECL 6758 || TREE_CODE (decl) == PARM_DECL) 6759 DECL_READ_P (decl) = 1; 6760 } 6761 else 6762 { 6763 warning (OPT_Wattributes, "%qE attribute ignored", name); 6764 *no_add_attrs = true; 6765 } 6766 } 6767 else 6768 { 6769 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) 6770 *node = build_variant_type_copy (*node); 6771 TREE_USED (*node) = 1; 6772 } 6773 6774 return NULL_TREE; 6775 } 6776 6777 /* Handle a "externally_visible" attribute; arguments as in 6778 struct attribute_spec.handler. */ 6779 6780 static tree 6781 handle_externally_visible_attribute (tree *pnode, tree name, 6782 tree ARG_UNUSED (args), 6783 int ARG_UNUSED (flags), 6784 bool *no_add_attrs) 6785 { 6786 tree node = *pnode; 6787 6788 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL) 6789 { 6790 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL 6791 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node)) 6792 { 6793 warning (OPT_Wattributes, 6794 "%qE attribute have effect only on public objects", name); 6795 *no_add_attrs = true; 6796 } 6797 } 6798 else 6799 { 6800 warning (OPT_Wattributes, "%qE attribute ignored", name); 6801 *no_add_attrs = true; 6802 } 6803 6804 return NULL_TREE; 6805 } 6806 6807 /* Handle a "const" attribute; arguments as in 6808 struct attribute_spec.handler. */ 6809 6810 static tree 6811 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args), 6812 int ARG_UNUSED (flags), bool *no_add_attrs) 6813 { 6814 tree type = TREE_TYPE (*node); 6815 6816 /* See FIXME comment on noreturn in c_common_attribute_table. */ 6817 if (TREE_CODE (*node) == FUNCTION_DECL) 6818 TREE_READONLY (*node) = 1; 6819 else if (TREE_CODE (type) == POINTER_TYPE 6820 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) 6821 TREE_TYPE (*node) 6822 = build_pointer_type 6823 (build_type_variant (TREE_TYPE (type), 1, 6824 TREE_THIS_VOLATILE (TREE_TYPE (type)))); 6825 else 6826 { 6827 warning (OPT_Wattributes, "%qE attribute ignored", name); 6828 *no_add_attrs = true; 6829 } 6830 6831 return NULL_TREE; 6832 } 6833 6834 /* Handle a "transparent_union" attribute; arguments as in 6835 struct attribute_spec.handler. */ 6836 6837 static tree 6838 handle_transparent_union_attribute (tree *node, tree name, 6839 tree ARG_UNUSED (args), int flags, 6840 bool *no_add_attrs) 6841 { 6842 tree type; 6843 6844 *no_add_attrs = true; 6845 6846 6847 if (TREE_CODE (*node) == TYPE_DECL 6848 && ! (flags & ATTR_FLAG_CXX11)) 6849 node = &TREE_TYPE (*node); 6850 type = *node; 6851 6852 if (TREE_CODE (type) == UNION_TYPE) 6853 { 6854 /* Make sure that the first field will work for a transparent union. 6855 If the type isn't complete yet, leave the check to the code in 6856 finish_struct. */ 6857 if (TYPE_SIZE (type)) 6858 { 6859 tree first = first_field (type); 6860 if (first == NULL_TREE 6861 || DECL_ARTIFICIAL (first) 6862 || TYPE_MODE (type) != DECL_MODE (first)) 6863 goto ignored; 6864 } 6865 6866 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) 6867 { 6868 /* If the type isn't complete yet, setting the flag 6869 on a variant wouldn't ever be checked. */ 6870 if (!TYPE_SIZE (type)) 6871 goto ignored; 6872 6873 /* build_duplicate_type doesn't work for C++. */ 6874 if (c_dialect_cxx ()) 6875 goto ignored; 6876 6877 /* A type variant isn't good enough, since we don't a cast 6878 to such a type removed as a no-op. */ 6879 *node = type = build_duplicate_type (type); 6880 } 6881 6882 TYPE_TRANSPARENT_AGGR (type) = 1; 6883 return NULL_TREE; 6884 } 6885 6886 ignored: 6887 warning (OPT_Wattributes, "%qE attribute ignored", name); 6888 return NULL_TREE; 6889 } 6890 6891 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to 6892 get the requested priority for a constructor or destructor, 6893 possibly issuing diagnostics for invalid or reserved 6894 priorities. */ 6895 6896 static priority_type 6897 get_priority (tree args, bool is_destructor) 6898 { 6899 HOST_WIDE_INT pri; 6900 tree arg; 6901 6902 if (!args) 6903 return DEFAULT_INIT_PRIORITY; 6904 6905 if (!SUPPORTS_INIT_PRIORITY) 6906 { 6907 if (is_destructor) 6908 error ("destructor priorities are not supported"); 6909 else 6910 error ("constructor priorities are not supported"); 6911 return DEFAULT_INIT_PRIORITY; 6912 } 6913 6914 arg = TREE_VALUE (args); 6915 if (TREE_CODE (arg) == IDENTIFIER_NODE) 6916 goto invalid; 6917 if (arg == error_mark_node) 6918 return DEFAULT_INIT_PRIORITY; 6919 arg = default_conversion (arg); 6920 if (!host_integerp (arg, /*pos=*/0) 6921 || !INTEGRAL_TYPE_P (TREE_TYPE (arg))) 6922 goto invalid; 6923 6924 pri = tree_low_cst (arg, /*pos=*/0); 6925 if (pri < 0 || pri > MAX_INIT_PRIORITY) 6926 goto invalid; 6927 6928 if (pri <= MAX_RESERVED_INIT_PRIORITY) 6929 { 6930 if (is_destructor) 6931 warning (0, 6932 "destructor priorities from 0 to %d are reserved " 6933 "for the implementation", 6934 MAX_RESERVED_INIT_PRIORITY); 6935 else 6936 warning (0, 6937 "constructor priorities from 0 to %d are reserved " 6938 "for the implementation", 6939 MAX_RESERVED_INIT_PRIORITY); 6940 } 6941 return pri; 6942 6943 invalid: 6944 if (is_destructor) 6945 error ("destructor priorities must be integers from 0 to %d inclusive", 6946 MAX_INIT_PRIORITY); 6947 else 6948 error ("constructor priorities must be integers from 0 to %d inclusive", 6949 MAX_INIT_PRIORITY); 6950 return DEFAULT_INIT_PRIORITY; 6951 } 6952 6953 /* Handle a "constructor" attribute; arguments as in 6954 struct attribute_spec.handler. */ 6955 6956 static tree 6957 handle_constructor_attribute (tree *node, tree name, tree args, 6958 int ARG_UNUSED (flags), 6959 bool *no_add_attrs) 6960 { 6961 tree decl = *node; 6962 tree type = TREE_TYPE (decl); 6963 6964 if (TREE_CODE (decl) == FUNCTION_DECL 6965 && TREE_CODE (type) == FUNCTION_TYPE 6966 && decl_function_context (decl) == 0) 6967 { 6968 priority_type priority; 6969 DECL_STATIC_CONSTRUCTOR (decl) = 1; 6970 priority = get_priority (args, /*is_destructor=*/false); 6971 SET_DECL_INIT_PRIORITY (decl, priority); 6972 TREE_USED (decl) = 1; 6973 } 6974 else 6975 { 6976 warning (OPT_Wattributes, "%qE attribute ignored", name); 6977 *no_add_attrs = true; 6978 } 6979 6980 return NULL_TREE; 6981 } 6982 6983 /* Handle a "destructor" attribute; arguments as in 6984 struct attribute_spec.handler. */ 6985 6986 static tree 6987 handle_destructor_attribute (tree *node, tree name, tree args, 6988 int ARG_UNUSED (flags), 6989 bool *no_add_attrs) 6990 { 6991 tree decl = *node; 6992 tree type = TREE_TYPE (decl); 6993 6994 if (TREE_CODE (decl) == FUNCTION_DECL 6995 && TREE_CODE (type) == FUNCTION_TYPE 6996 && decl_function_context (decl) == 0) 6997 { 6998 priority_type priority; 6999 DECL_STATIC_DESTRUCTOR (decl) = 1; 7000 priority = get_priority (args, /*is_destructor=*/true); 7001 SET_DECL_FINI_PRIORITY (decl, priority); 7002 TREE_USED (decl) = 1; 7003 } 7004 else 7005 { 7006 warning (OPT_Wattributes, "%qE attribute ignored", name); 7007 *no_add_attrs = true; 7008 } 7009 7010 return NULL_TREE; 7011 } 7012 7013 /* Nonzero if the mode is a valid vector mode for this architecture. 7014 This returns nonzero even if there is no hardware support for the 7015 vector mode, but we can emulate with narrower modes. */ 7016 7017 static int 7018 vector_mode_valid_p (enum machine_mode mode) 7019 { 7020 enum mode_class mclass = GET_MODE_CLASS (mode); 7021 enum machine_mode innermode; 7022 7023 /* Doh! What's going on? */ 7024 if (mclass != MODE_VECTOR_INT 7025 && mclass != MODE_VECTOR_FLOAT 7026 && mclass != MODE_VECTOR_FRACT 7027 && mclass != MODE_VECTOR_UFRACT 7028 && mclass != MODE_VECTOR_ACCUM 7029 && mclass != MODE_VECTOR_UACCUM) 7030 return 0; 7031 7032 /* Hardware support. Woo hoo! */ 7033 if (targetm.vector_mode_supported_p (mode)) 7034 return 1; 7035 7036 innermode = GET_MODE_INNER (mode); 7037 7038 /* We should probably return 1 if requesting V4DI and we have no DI, 7039 but we have V2DI, but this is probably very unlikely. */ 7040 7041 /* If we have support for the inner mode, we can safely emulate it. 7042 We may not have V2DI, but me can emulate with a pair of DIs. */ 7043 return targetm.scalar_mode_supported_p (innermode); 7044 } 7045 7046 7047 /* Handle a "mode" attribute; arguments as in 7048 struct attribute_spec.handler. */ 7049 7050 static tree 7051 handle_mode_attribute (tree *node, tree name, tree args, 7052 int ARG_UNUSED (flags), bool *no_add_attrs) 7053 { 7054 tree type = *node; 7055 tree ident = TREE_VALUE (args); 7056 7057 *no_add_attrs = true; 7058 7059 if (TREE_CODE (ident) != IDENTIFIER_NODE) 7060 warning (OPT_Wattributes, "%qE attribute ignored", name); 7061 else 7062 { 7063 int j; 7064 const char *p = IDENTIFIER_POINTER (ident); 7065 int len = strlen (p); 7066 enum machine_mode mode = VOIDmode; 7067 tree typefm; 7068 bool valid_mode; 7069 7070 if (len > 4 && p[0] == '_' && p[1] == '_' 7071 && p[len - 1] == '_' && p[len - 2] == '_') 7072 { 7073 char *newp = (char *) alloca (len - 1); 7074 7075 strcpy (newp, &p[2]); 7076 newp[len - 4] = '\0'; 7077 p = newp; 7078 } 7079 7080 /* Change this type to have a type with the specified mode. 7081 First check for the special modes. */ 7082 if (!strcmp (p, "byte")) 7083 mode = byte_mode; 7084 else if (!strcmp (p, "word")) 7085 mode = word_mode; 7086 else if (!strcmp (p, "pointer")) 7087 mode = ptr_mode; 7088 else if (!strcmp (p, "libgcc_cmp_return")) 7089 mode = targetm.libgcc_cmp_return_mode (); 7090 else if (!strcmp (p, "libgcc_shift_count")) 7091 mode = targetm.libgcc_shift_count_mode (); 7092 else if (!strcmp (p, "unwind_word")) 7093 mode = targetm.unwind_word_mode (); 7094 else 7095 for (j = 0; j < NUM_MACHINE_MODES; j++) 7096 if (!strcmp (p, GET_MODE_NAME (j))) 7097 { 7098 mode = (enum machine_mode) j; 7099 break; 7100 } 7101 7102 if (mode == VOIDmode) 7103 { 7104 error ("unknown machine mode %qE", ident); 7105 return NULL_TREE; 7106 } 7107 7108 valid_mode = false; 7109 switch (GET_MODE_CLASS (mode)) 7110 { 7111 case MODE_INT: 7112 case MODE_PARTIAL_INT: 7113 case MODE_FLOAT: 7114 case MODE_DECIMAL_FLOAT: 7115 case MODE_FRACT: 7116 case MODE_UFRACT: 7117 case MODE_ACCUM: 7118 case MODE_UACCUM: 7119 valid_mode = targetm.scalar_mode_supported_p (mode); 7120 break; 7121 7122 case MODE_COMPLEX_INT: 7123 case MODE_COMPLEX_FLOAT: 7124 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode)); 7125 break; 7126 7127 case MODE_VECTOR_INT: 7128 case MODE_VECTOR_FLOAT: 7129 case MODE_VECTOR_FRACT: 7130 case MODE_VECTOR_UFRACT: 7131 case MODE_VECTOR_ACCUM: 7132 case MODE_VECTOR_UACCUM: 7133 warning (OPT_Wattributes, "specifying vector types with " 7134 "__attribute__ ((mode)) is deprecated"); 7135 warning (OPT_Wattributes, 7136 "use __attribute__ ((vector_size)) instead"); 7137 valid_mode = vector_mode_valid_p (mode); 7138 break; 7139 7140 default: 7141 break; 7142 } 7143 if (!valid_mode) 7144 { 7145 error ("unable to emulate %qs", p); 7146 return NULL_TREE; 7147 } 7148 7149 if (POINTER_TYPE_P (type)) 7150 { 7151 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type)); 7152 tree (*fn)(tree, enum machine_mode, bool); 7153 7154 if (!targetm.addr_space.valid_pointer_mode (mode, as)) 7155 { 7156 error ("invalid pointer mode %qs", p); 7157 return NULL_TREE; 7158 } 7159 7160 if (TREE_CODE (type) == POINTER_TYPE) 7161 fn = build_pointer_type_for_mode; 7162 else 7163 fn = build_reference_type_for_mode; 7164 typefm = fn (TREE_TYPE (type), mode, false); 7165 } 7166 else 7167 { 7168 /* For fixed-point modes, we need to test if the signness of type 7169 and the machine mode are consistent. */ 7170 if (ALL_FIXED_POINT_MODE_P (mode) 7171 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode)) 7172 { 7173 error ("signedness of type and machine mode %qs don%'t match", p); 7174 return NULL_TREE; 7175 } 7176 /* For fixed-point modes, we need to pass saturating info. */ 7177 typefm = lang_hooks.types.type_for_mode (mode, 7178 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type) 7179 : TYPE_UNSIGNED (type)); 7180 } 7181 7182 if (typefm == NULL_TREE) 7183 { 7184 error ("no data type for mode %qs", p); 7185 return NULL_TREE; 7186 } 7187 else if (TREE_CODE (type) == ENUMERAL_TYPE) 7188 { 7189 /* For enumeral types, copy the precision from the integer 7190 type returned above. If not an INTEGER_TYPE, we can't use 7191 this mode for this type. */ 7192 if (TREE_CODE (typefm) != INTEGER_TYPE) 7193 { 7194 error ("cannot use mode %qs for enumeral types", p); 7195 return NULL_TREE; 7196 } 7197 7198 if (flags & ATTR_FLAG_TYPE_IN_PLACE) 7199 { 7200 TYPE_PRECISION (type) = TYPE_PRECISION (typefm); 7201 typefm = type; 7202 } 7203 else 7204 { 7205 /* We cannot build a type variant, as there's code that assumes 7206 that TYPE_MAIN_VARIANT has the same mode. This includes the 7207 debug generators. Instead, create a subrange type. This 7208 results in all of the enumeral values being emitted only once 7209 in the original, and the subtype gets them by reference. */ 7210 if (TYPE_UNSIGNED (type)) 7211 typefm = make_unsigned_type (TYPE_PRECISION (typefm)); 7212 else 7213 typefm = make_signed_type (TYPE_PRECISION (typefm)); 7214 TREE_TYPE (typefm) = type; 7215 } 7216 } 7217 else if (VECTOR_MODE_P (mode) 7218 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm)) 7219 : TREE_CODE (type) != TREE_CODE (typefm)) 7220 { 7221 error ("mode %qs applied to inappropriate type", p); 7222 return NULL_TREE; 7223 } 7224 7225 *node = typefm; 7226 } 7227 7228 return NULL_TREE; 7229 } 7230 7231 /* Handle a "section" attribute; arguments as in 7232 struct attribute_spec.handler. */ 7233 7234 static tree 7235 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args, 7236 int ARG_UNUSED (flags), bool *no_add_attrs) 7237 { 7238 tree decl = *node; 7239 7240 if (targetm_common.have_named_sections) 7241 { 7242 user_defined_section_attribute = true; 7243 7244 if ((TREE_CODE (decl) == FUNCTION_DECL 7245 || TREE_CODE (decl) == VAR_DECL) 7246 && TREE_CODE (TREE_VALUE (args)) == STRING_CST) 7247 { 7248 if (TREE_CODE (decl) == VAR_DECL 7249 && current_function_decl != NULL_TREE 7250 && !TREE_STATIC (decl)) 7251 { 7252 error_at (DECL_SOURCE_LOCATION (decl), 7253 "section attribute cannot be specified for " 7254 "local variables"); 7255 *no_add_attrs = true; 7256 } 7257 7258 /* The decl may have already been given a section attribute 7259 from a previous declaration. Ensure they match. */ 7260 else if (DECL_SECTION_NAME (decl) != NULL_TREE 7261 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)), 7262 TREE_STRING_POINTER (TREE_VALUE (args))) != 0) 7263 { 7264 error ("section of %q+D conflicts with previous declaration", 7265 *node); 7266 *no_add_attrs = true; 7267 } 7268 else if (TREE_CODE (decl) == VAR_DECL 7269 && !targetm.have_tls && targetm.emutls.tmpl_section 7270 && DECL_THREAD_LOCAL_P (decl)) 7271 { 7272 error ("section of %q+D cannot be overridden", *node); 7273 *no_add_attrs = true; 7274 } 7275 else 7276 DECL_SECTION_NAME (decl) = TREE_VALUE (args); 7277 } 7278 else 7279 { 7280 error ("section attribute not allowed for %q+D", *node); 7281 *no_add_attrs = true; 7282 } 7283 } 7284 else 7285 { 7286 error_at (DECL_SOURCE_LOCATION (*node), 7287 "section attributes are not supported for this target"); 7288 *no_add_attrs = true; 7289 } 7290 7291 return NULL_TREE; 7292 } 7293 7294 /* Check whether ALIGN is a valid user-specified alignment. If so, 7295 return its base-2 log; if not, output an error and return -1. If 7296 ALLOW_ZERO then 0 is valid and should result in a return of -1 with 7297 no error. */ 7298 int 7299 check_user_alignment (const_tree align, bool allow_zero) 7300 { 7301 int i; 7302 7303 if (TREE_CODE (align) != INTEGER_CST 7304 || !INTEGRAL_TYPE_P (TREE_TYPE (align))) 7305 { 7306 error ("requested alignment is not an integer constant"); 7307 return -1; 7308 } 7309 else if (allow_zero && integer_zerop (align)) 7310 return -1; 7311 else if ((i = tree_log2 (align)) == -1) 7312 { 7313 error ("requested alignment is not a power of 2"); 7314 return -1; 7315 } 7316 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG) 7317 { 7318 error ("requested alignment is too large"); 7319 return -1; 7320 } 7321 return i; 7322 } 7323 7324 /* 7325 If in c++-11, check if the c++-11 alignment constraint with respect 7326 to fundamental alignment (in [dcl.align]) are satisfied. If not in 7327 c++-11 mode, does nothing. 7328 7329 [dcl.align]2/ says: 7330 7331 [* if the constant expression evaluates to a fundamental alignment, 7332 the alignment requirement of the declared entity shall be the 7333 specified fundamental alignment. 7334 7335 * if the constant expression evaluates to an extended alignment 7336 and the implementation supports that alignment in the context 7337 of the declaration, the alignment of the declared entity shall 7338 be that alignment 7339 7340 * if the constant expression evaluates to an extended alignment 7341 and the implementation does not support that alignment in the 7342 context of the declaration, the program is ill-formed]. */ 7343 7344 static bool 7345 check_cxx_fundamental_alignment_constraints (tree node, 7346 unsigned align_log, 7347 int flags) 7348 { 7349 bool alignment_too_large_p = false; 7350 unsigned requested_alignment = 1U << align_log; 7351 unsigned max_align = 0; 7352 7353 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat) 7354 || (node == NULL_TREE || node == error_mark_node)) 7355 return true; 7356 7357 if (cxx_fundamental_alignment_p (requested_alignment)) 7358 return true; 7359 7360 if (DECL_P (node)) 7361 { 7362 if (TREE_STATIC (node)) 7363 { 7364 /* For file scope variables and static members, the target 7365 supports alignments that are at most 7366 MAX_OFILE_ALIGNMENT. */ 7367 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT)) 7368 alignment_too_large_p = true; 7369 } 7370 else 7371 { 7372 #ifdef BIGGEST_FIELD_ALIGNMENT 7373 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT 7374 #else 7375 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT 7376 #endif 7377 /* For non-static members, the target supports either 7378 alignments that at most either BIGGEST_FIELD_ALIGNMENT 7379 if it is defined or BIGGEST_ALIGNMENT. */ 7380 max_align = MAX_TARGET_FIELD_ALIGNMENT; 7381 if (TREE_CODE (node) == FIELD_DECL 7382 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT)) 7383 alignment_too_large_p = true; 7384 #undef MAX_TARGET_FIELD_ALIGNMENT 7385 /* For stack variables, the target supports at most 7386 MAX_STACK_ALIGNMENT. */ 7387 else if (decl_function_context (node) != NULL 7388 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT)) 7389 alignment_too_large_p = true; 7390 } 7391 } 7392 else if (TYPE_P (node)) 7393 { 7394 /* Let's be liberal for types. */ 7395 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT)) 7396 alignment_too_large_p = true; 7397 } 7398 7399 if (alignment_too_large_p) 7400 pedwarn (input_location, OPT_Wattributes, 7401 "requested alignment %d is larger than %d", 7402 requested_alignment, max_align); 7403 7404 return !alignment_too_large_p; 7405 } 7406 7407 /* Handle a "aligned" attribute; arguments as in 7408 struct attribute_spec.handler. */ 7409 7410 static tree 7411 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args, 7412 int flags, bool *no_add_attrs) 7413 { 7414 tree decl = NULL_TREE; 7415 tree *type = NULL; 7416 int is_type = 0; 7417 tree align_expr = (args ? TREE_VALUE (args) 7418 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT)); 7419 int i; 7420 7421 if (DECL_P (*node)) 7422 { 7423 decl = *node; 7424 type = &TREE_TYPE (decl); 7425 is_type = TREE_CODE (*node) == TYPE_DECL; 7426 } 7427 else if (TYPE_P (*node)) 7428 type = node, is_type = 1; 7429 7430 if ((i = check_user_alignment (align_expr, false)) == -1 7431 || !check_cxx_fundamental_alignment_constraints (*node, i, flags)) 7432 *no_add_attrs = true; 7433 else if (is_type) 7434 { 7435 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) 7436 /* OK, modify the type in place. */; 7437 /* If we have a TYPE_DECL, then copy the type, so that we 7438 don't accidentally modify a builtin type. See pushdecl. */ 7439 else if (decl && TREE_TYPE (decl) != error_mark_node 7440 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE) 7441 { 7442 tree tt = TREE_TYPE (decl); 7443 *type = build_variant_type_copy (*type); 7444 DECL_ORIGINAL_TYPE (decl) = tt; 7445 TYPE_NAME (*type) = decl; 7446 TREE_USED (*type) = TREE_USED (decl); 7447 TREE_TYPE (decl) = *type; 7448 } 7449 else 7450 *type = build_variant_type_copy (*type); 7451 7452 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT; 7453 TYPE_USER_ALIGN (*type) = 1; 7454 } 7455 else if (! VAR_OR_FUNCTION_DECL_P (decl) 7456 && TREE_CODE (decl) != FIELD_DECL) 7457 { 7458 error ("alignment may not be specified for %q+D", decl); 7459 *no_add_attrs = true; 7460 } 7461 else if (DECL_USER_ALIGN (decl) 7462 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT) 7463 /* C++-11 [dcl.align/4]: 7464 7465 When multiple alignment-specifiers are specified for an 7466 entity, the alignment requirement shall be set to the 7467 strictest specified alignment. 7468 7469 This formally comes from the c++11 specification but we are 7470 doing it for the GNU attribute syntax as well. */ 7471 *no_add_attrs = true; 7472 else if (TREE_CODE (decl) == FUNCTION_DECL 7473 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT) 7474 { 7475 if (DECL_USER_ALIGN (decl)) 7476 error ("alignment for %q+D was previously specified as %d " 7477 "and may not be decreased", decl, 7478 DECL_ALIGN (decl) / BITS_PER_UNIT); 7479 else 7480 error ("alignment for %q+D must be at least %d", decl, 7481 DECL_ALIGN (decl) / BITS_PER_UNIT); 7482 *no_add_attrs = true; 7483 } 7484 else 7485 { 7486 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT; 7487 DECL_USER_ALIGN (decl) = 1; 7488 } 7489 7490 return NULL_TREE; 7491 } 7492 7493 /* Handle a "weak" attribute; arguments as in 7494 struct attribute_spec.handler. */ 7495 7496 static tree 7497 handle_weak_attribute (tree *node, tree name, 7498 tree ARG_UNUSED (args), 7499 int ARG_UNUSED (flags), 7500 bool * ARG_UNUSED (no_add_attrs)) 7501 { 7502 if (TREE_CODE (*node) == FUNCTION_DECL 7503 && DECL_DECLARED_INLINE_P (*node)) 7504 { 7505 warning (OPT_Wattributes, "inline function %q+D declared weak", *node); 7506 *no_add_attrs = true; 7507 } 7508 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node))) 7509 { 7510 error ("indirect function %q+D cannot be declared weak", *node); 7511 *no_add_attrs = true; 7512 return NULL_TREE; 7513 } 7514 else if (TREE_CODE (*node) == FUNCTION_DECL 7515 || TREE_CODE (*node) == VAR_DECL) 7516 declare_weak (*node); 7517 else 7518 warning (OPT_Wattributes, "%qE attribute ignored", name); 7519 7520 return NULL_TREE; 7521 } 7522 7523 /* Handle an "alias" or "ifunc" attribute; arguments as in 7524 struct attribute_spec.handler, except that IS_ALIAS tells us 7525 whether this is an alias as opposed to ifunc attribute. */ 7526 7527 static tree 7528 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args, 7529 bool *no_add_attrs) 7530 { 7531 tree decl = *node; 7532 7533 if (TREE_CODE (decl) != FUNCTION_DECL 7534 && (!is_alias || TREE_CODE (decl) != VAR_DECL)) 7535 { 7536 warning (OPT_Wattributes, "%qE attribute ignored", name); 7537 *no_add_attrs = true; 7538 } 7539 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl)) 7540 || (TREE_CODE (decl) != FUNCTION_DECL 7541 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) 7542 /* A static variable declaration is always a tentative definition, 7543 but the alias is a non-tentative definition which overrides. */ 7544 || (TREE_CODE (decl) != FUNCTION_DECL 7545 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl))) 7546 { 7547 error ("%q+D defined both normally and as %qE attribute", decl, name); 7548 *no_add_attrs = true; 7549 return NULL_TREE; 7550 } 7551 else if (!is_alias 7552 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl)) 7553 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))) 7554 { 7555 error ("weak %q+D cannot be defined %qE", decl, name); 7556 *no_add_attrs = true; 7557 return NULL_TREE; 7558 } 7559 7560 /* Note that the very first time we process a nested declaration, 7561 decl_function_context will not be set. Indeed, *would* never 7562 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that 7563 we do below. After such frobbery, pushdecl would set the context. 7564 In any case, this is never what we want. */ 7565 else if (decl_function_context (decl) == 0 && current_function_decl == NULL) 7566 { 7567 tree id; 7568 7569 id = TREE_VALUE (args); 7570 if (TREE_CODE (id) != STRING_CST) 7571 { 7572 error ("attribute %qE argument not a string", name); 7573 *no_add_attrs = true; 7574 return NULL_TREE; 7575 } 7576 id = get_identifier (TREE_STRING_POINTER (id)); 7577 /* This counts as a use of the object pointed to. */ 7578 TREE_USED (id) = 1; 7579 7580 if (TREE_CODE (decl) == FUNCTION_DECL) 7581 DECL_INITIAL (decl) = error_mark_node; 7582 else 7583 { 7584 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))) 7585 DECL_EXTERNAL (decl) = 1; 7586 else 7587 DECL_EXTERNAL (decl) = 0; 7588 TREE_STATIC (decl) = 1; 7589 } 7590 7591 if (!is_alias) 7592 /* ifuncs are also aliases, so set that attribute too. */ 7593 DECL_ATTRIBUTES (decl) 7594 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl)); 7595 } 7596 else 7597 { 7598 warning (OPT_Wattributes, "%qE attribute ignored", name); 7599 *no_add_attrs = true; 7600 } 7601 7602 return NULL_TREE; 7603 } 7604 7605 /* Handle an "alias" or "ifunc" attribute; arguments as in 7606 struct attribute_spec.handler. */ 7607 7608 static tree 7609 handle_ifunc_attribute (tree *node, tree name, tree args, 7610 int ARG_UNUSED (flags), bool *no_add_attrs) 7611 { 7612 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs); 7613 } 7614 7615 /* Handle an "alias" or "ifunc" attribute; arguments as in 7616 struct attribute_spec.handler. */ 7617 7618 static tree 7619 handle_alias_attribute (tree *node, tree name, tree args, 7620 int ARG_UNUSED (flags), bool *no_add_attrs) 7621 { 7622 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs); 7623 } 7624 7625 /* Handle a "weakref" attribute; arguments as in struct 7626 attribute_spec.handler. */ 7627 7628 static tree 7629 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args, 7630 int flags, bool *no_add_attrs) 7631 { 7632 tree attr = NULL_TREE; 7633 7634 /* We must ignore the attribute when it is associated with 7635 local-scoped decls, since attribute alias is ignored and many 7636 such symbols do not even have a DECL_WEAK field. */ 7637 if (decl_function_context (*node) 7638 || current_function_decl 7639 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL)) 7640 { 7641 warning (OPT_Wattributes, "%qE attribute ignored", name); 7642 *no_add_attrs = true; 7643 return NULL_TREE; 7644 } 7645 7646 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node))) 7647 { 7648 error ("indirect function %q+D cannot be declared weakref", *node); 7649 *no_add_attrs = true; 7650 return NULL_TREE; 7651 } 7652 7653 /* The idea here is that `weakref("name")' mutates into `weakref, 7654 alias("name")', and weakref without arguments, in turn, 7655 implicitly adds weak. */ 7656 7657 if (args) 7658 { 7659 attr = tree_cons (get_identifier ("alias"), args, attr); 7660 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr); 7661 7662 *no_add_attrs = true; 7663 7664 decl_attributes (node, attr, flags); 7665 } 7666 else 7667 { 7668 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node))) 7669 error_at (DECL_SOURCE_LOCATION (*node), 7670 "weakref attribute must appear before alias attribute"); 7671 7672 /* Can't call declare_weak because it wants this to be TREE_PUBLIC, 7673 and that isn't supported; and because it wants to add it to 7674 the list of weak decls, which isn't helpful. */ 7675 DECL_WEAK (*node) = 1; 7676 } 7677 7678 return NULL_TREE; 7679 } 7680 7681 /* Handle an "visibility" attribute; arguments as in 7682 struct attribute_spec.handler. */ 7683 7684 static tree 7685 handle_visibility_attribute (tree *node, tree name, tree args, 7686 int ARG_UNUSED (flags), 7687 bool *ARG_UNUSED (no_add_attrs)) 7688 { 7689 tree decl = *node; 7690 tree id = TREE_VALUE (args); 7691 enum symbol_visibility vis; 7692 7693 if (TYPE_P (*node)) 7694 { 7695 if (TREE_CODE (*node) == ENUMERAL_TYPE) 7696 /* OK */; 7697 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE) 7698 { 7699 warning (OPT_Wattributes, "%qE attribute ignored on non-class types", 7700 name); 7701 return NULL_TREE; 7702 } 7703 else if (TYPE_FIELDS (*node)) 7704 { 7705 error ("%qE attribute ignored because %qT is already defined", 7706 name, *node); 7707 return NULL_TREE; 7708 } 7709 } 7710 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl)) 7711 { 7712 warning (OPT_Wattributes, "%qE attribute ignored", name); 7713 return NULL_TREE; 7714 } 7715 7716 if (TREE_CODE (id) != STRING_CST) 7717 { 7718 error ("visibility argument not a string"); 7719 return NULL_TREE; 7720 } 7721 7722 /* If this is a type, set the visibility on the type decl. */ 7723 if (TYPE_P (decl)) 7724 { 7725 decl = TYPE_NAME (decl); 7726 if (!decl) 7727 return NULL_TREE; 7728 if (TREE_CODE (decl) == IDENTIFIER_NODE) 7729 { 7730 warning (OPT_Wattributes, "%qE attribute ignored on types", 7731 name); 7732 return NULL_TREE; 7733 } 7734 } 7735 7736 if (strcmp (TREE_STRING_POINTER (id), "default") == 0) 7737 vis = VISIBILITY_DEFAULT; 7738 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0) 7739 vis = VISIBILITY_INTERNAL; 7740 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0) 7741 vis = VISIBILITY_HIDDEN; 7742 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0) 7743 vis = VISIBILITY_PROTECTED; 7744 else 7745 { 7746 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\""); 7747 vis = VISIBILITY_DEFAULT; 7748 } 7749 7750 if (DECL_VISIBILITY_SPECIFIED (decl) 7751 && vis != DECL_VISIBILITY (decl)) 7752 { 7753 tree attributes = (TYPE_P (*node) 7754 ? TYPE_ATTRIBUTES (*node) 7755 : DECL_ATTRIBUTES (decl)); 7756 if (lookup_attribute ("visibility", attributes)) 7757 error ("%qD redeclared with different visibility", decl); 7758 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES 7759 && lookup_attribute ("dllimport", attributes)) 7760 error ("%qD was declared %qs which implies default visibility", 7761 decl, "dllimport"); 7762 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES 7763 && lookup_attribute ("dllexport", attributes)) 7764 error ("%qD was declared %qs which implies default visibility", 7765 decl, "dllexport"); 7766 } 7767 7768 DECL_VISIBILITY (decl) = vis; 7769 DECL_VISIBILITY_SPECIFIED (decl) = 1; 7770 7771 /* Go ahead and attach the attribute to the node as well. This is needed 7772 so we can determine whether we have VISIBILITY_DEFAULT because the 7773 visibility was not specified, or because it was explicitly overridden 7774 from the containing scope. */ 7775 7776 return NULL_TREE; 7777 } 7778 7779 /* Determine the ELF symbol visibility for DECL, which is either a 7780 variable or a function. It is an error to use this function if a 7781 definition of DECL is not available in this translation unit. 7782 Returns true if the final visibility has been determined by this 7783 function; false if the caller is free to make additional 7784 modifications. */ 7785 7786 bool 7787 c_determine_visibility (tree decl) 7788 { 7789 gcc_assert (TREE_CODE (decl) == VAR_DECL 7790 || TREE_CODE (decl) == FUNCTION_DECL); 7791 7792 /* If the user explicitly specified the visibility with an 7793 attribute, honor that. DECL_VISIBILITY will have been set during 7794 the processing of the attribute. We check for an explicit 7795 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED, 7796 to distinguish the use of an attribute from the use of a "#pragma 7797 GCC visibility push(...)"; in the latter case we still want other 7798 considerations to be able to overrule the #pragma. */ 7799 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)) 7800 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES 7801 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl)) 7802 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))))) 7803 return true; 7804 7805 /* Set default visibility to whatever the user supplied with 7806 visibility_specified depending on #pragma GCC visibility. */ 7807 if (!DECL_VISIBILITY_SPECIFIED (decl)) 7808 { 7809 if (visibility_options.inpragma 7810 || DECL_VISIBILITY (decl) != default_visibility) 7811 { 7812 DECL_VISIBILITY (decl) = default_visibility; 7813 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma; 7814 /* If visibility changed and DECL already has DECL_RTL, ensure 7815 symbol flags are updated. */ 7816 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)) 7817 || TREE_CODE (decl) == FUNCTION_DECL) 7818 && DECL_RTL_SET_P (decl)) 7819 make_decl_rtl (decl); 7820 } 7821 } 7822 return false; 7823 } 7824 7825 /* Handle an "tls_model" attribute; arguments as in 7826 struct attribute_spec.handler. */ 7827 7828 static tree 7829 handle_tls_model_attribute (tree *node, tree name, tree args, 7830 int ARG_UNUSED (flags), bool *no_add_attrs) 7831 { 7832 tree id; 7833 tree decl = *node; 7834 enum tls_model kind; 7835 7836 *no_add_attrs = true; 7837 7838 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl)) 7839 { 7840 warning (OPT_Wattributes, "%qE attribute ignored", name); 7841 return NULL_TREE; 7842 } 7843 7844 kind = DECL_TLS_MODEL (decl); 7845 id = TREE_VALUE (args); 7846 if (TREE_CODE (id) != STRING_CST) 7847 { 7848 error ("tls_model argument not a string"); 7849 return NULL_TREE; 7850 } 7851 7852 if (!strcmp (TREE_STRING_POINTER (id), "local-exec")) 7853 kind = TLS_MODEL_LOCAL_EXEC; 7854 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec")) 7855 kind = TLS_MODEL_INITIAL_EXEC; 7856 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic")) 7857 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC; 7858 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic")) 7859 kind = TLS_MODEL_GLOBAL_DYNAMIC; 7860 else 7861 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\""); 7862 7863 DECL_TLS_MODEL (decl) = kind; 7864 return NULL_TREE; 7865 } 7866 7867 /* Handle a "no_instrument_function" attribute; arguments as in 7868 struct attribute_spec.handler. */ 7869 7870 static tree 7871 handle_no_instrument_function_attribute (tree *node, tree name, 7872 tree ARG_UNUSED (args), 7873 int ARG_UNUSED (flags), 7874 bool *no_add_attrs) 7875 { 7876 tree decl = *node; 7877 7878 if (TREE_CODE (decl) != FUNCTION_DECL) 7879 { 7880 error_at (DECL_SOURCE_LOCATION (decl), 7881 "%qE attribute applies only to functions", name); 7882 *no_add_attrs = true; 7883 } 7884 else if (DECL_INITIAL (decl)) 7885 { 7886 error_at (DECL_SOURCE_LOCATION (decl), 7887 "can%'t set %qE attribute after definition", name); 7888 *no_add_attrs = true; 7889 } 7890 else 7891 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; 7892 7893 return NULL_TREE; 7894 } 7895 7896 /* Handle a "malloc" attribute; arguments as in 7897 struct attribute_spec.handler. */ 7898 7899 static tree 7900 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args), 7901 int ARG_UNUSED (flags), bool *no_add_attrs) 7902 { 7903 if (TREE_CODE (*node) == FUNCTION_DECL 7904 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))) 7905 DECL_IS_MALLOC (*node) = 1; 7906 else 7907 { 7908 warning (OPT_Wattributes, "%qE attribute ignored", name); 7909 *no_add_attrs = true; 7910 } 7911 7912 return NULL_TREE; 7913 } 7914 7915 /* Handle a "alloc_size" attribute; arguments as in 7916 struct attribute_spec.handler. */ 7917 7918 static tree 7919 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args, 7920 int ARG_UNUSED (flags), bool *no_add_attrs) 7921 { 7922 unsigned arg_count = type_num_arguments (*node); 7923 for (; args; args = TREE_CHAIN (args)) 7924 { 7925 tree position = TREE_VALUE (args); 7926 7927 if (TREE_CODE (position) != INTEGER_CST 7928 || TREE_INT_CST_HIGH (position) 7929 || TREE_INT_CST_LOW (position) < 1 7930 || TREE_INT_CST_LOW (position) > arg_count ) 7931 { 7932 warning (OPT_Wattributes, 7933 "alloc_size parameter outside range"); 7934 *no_add_attrs = true; 7935 return NULL_TREE; 7936 } 7937 } 7938 return NULL_TREE; 7939 } 7940 7941 /* Handle a "fn spec" attribute; arguments as in 7942 struct attribute_spec.handler. */ 7943 7944 static tree 7945 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name), 7946 tree args, int ARG_UNUSED (flags), 7947 bool *no_add_attrs ATTRIBUTE_UNUSED) 7948 { 7949 gcc_assert (args 7950 && TREE_CODE (TREE_VALUE (args)) == STRING_CST 7951 && !TREE_CHAIN (args)); 7952 return NULL_TREE; 7953 } 7954 7955 /* Handle a "returns_twice" attribute; arguments as in 7956 struct attribute_spec.handler. */ 7957 7958 static tree 7959 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args), 7960 int ARG_UNUSED (flags), bool *no_add_attrs) 7961 { 7962 if (TREE_CODE (*node) == FUNCTION_DECL) 7963 DECL_IS_RETURNS_TWICE (*node) = 1; 7964 else 7965 { 7966 warning (OPT_Wattributes, "%qE attribute ignored", name); 7967 *no_add_attrs = true; 7968 } 7969 7970 return NULL_TREE; 7971 } 7972 7973 /* Handle a "no_limit_stack" attribute; arguments as in 7974 struct attribute_spec.handler. */ 7975 7976 static tree 7977 handle_no_limit_stack_attribute (tree *node, tree name, 7978 tree ARG_UNUSED (args), 7979 int ARG_UNUSED (flags), 7980 bool *no_add_attrs) 7981 { 7982 tree decl = *node; 7983 7984 if (TREE_CODE (decl) != FUNCTION_DECL) 7985 { 7986 error_at (DECL_SOURCE_LOCATION (decl), 7987 "%qE attribute applies only to functions", name); 7988 *no_add_attrs = true; 7989 } 7990 else if (DECL_INITIAL (decl)) 7991 { 7992 error_at (DECL_SOURCE_LOCATION (decl), 7993 "can%'t set %qE attribute after definition", name); 7994 *no_add_attrs = true; 7995 } 7996 else 7997 DECL_NO_LIMIT_STACK (decl) = 1; 7998 7999 return NULL_TREE; 8000 } 8001 8002 /* Handle a "pure" attribute; arguments as in 8003 struct attribute_spec.handler. */ 8004 8005 static tree 8006 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args), 8007 int ARG_UNUSED (flags), bool *no_add_attrs) 8008 { 8009 if (TREE_CODE (*node) == FUNCTION_DECL) 8010 DECL_PURE_P (*node) = 1; 8011 /* ??? TODO: Support types. */ 8012 else 8013 { 8014 warning (OPT_Wattributes, "%qE attribute ignored", name); 8015 *no_add_attrs = true; 8016 } 8017 8018 return NULL_TREE; 8019 } 8020 8021 /* Digest an attribute list destined for a transactional memory statement. 8022 ALLOWED is the set of attributes that are allowed for this statement; 8023 return the attribute we parsed. Multiple attributes are never allowed. */ 8024 8025 int 8026 parse_tm_stmt_attr (tree attrs, int allowed) 8027 { 8028 tree a_seen = NULL; 8029 int m_seen = 0; 8030 8031 for ( ; attrs ; attrs = TREE_CHAIN (attrs)) 8032 { 8033 tree a = TREE_PURPOSE (attrs); 8034 int m = 0; 8035 8036 if (is_attribute_p ("outer", a)) 8037 m = TM_STMT_ATTR_OUTER; 8038 8039 if ((m & allowed) == 0) 8040 { 8041 warning (OPT_Wattributes, "%qE attribute directive ignored", a); 8042 continue; 8043 } 8044 8045 if (m_seen == 0) 8046 { 8047 a_seen = a; 8048 m_seen = m; 8049 } 8050 else if (m_seen == m) 8051 warning (OPT_Wattributes, "%qE attribute duplicated", a); 8052 else 8053 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen); 8054 } 8055 8056 return m_seen; 8057 } 8058 8059 /* Transform a TM attribute name into a maskable integer and back. 8060 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding 8061 to how the lack of an attribute is treated. */ 8062 8063 int 8064 tm_attr_to_mask (tree attr) 8065 { 8066 if (attr == NULL) 8067 return 0; 8068 if (is_attribute_p ("transaction_safe", attr)) 8069 return TM_ATTR_SAFE; 8070 if (is_attribute_p ("transaction_callable", attr)) 8071 return TM_ATTR_CALLABLE; 8072 if (is_attribute_p ("transaction_pure", attr)) 8073 return TM_ATTR_PURE; 8074 if (is_attribute_p ("transaction_unsafe", attr)) 8075 return TM_ATTR_IRREVOCABLE; 8076 if (is_attribute_p ("transaction_may_cancel_outer", attr)) 8077 return TM_ATTR_MAY_CANCEL_OUTER; 8078 return 0; 8079 } 8080 8081 tree 8082 tm_mask_to_attr (int mask) 8083 { 8084 const char *str; 8085 switch (mask) 8086 { 8087 case TM_ATTR_SAFE: 8088 str = "transaction_safe"; 8089 break; 8090 case TM_ATTR_CALLABLE: 8091 str = "transaction_callable"; 8092 break; 8093 case TM_ATTR_PURE: 8094 str = "transaction_pure"; 8095 break; 8096 case TM_ATTR_IRREVOCABLE: 8097 str = "transaction_unsafe"; 8098 break; 8099 case TM_ATTR_MAY_CANCEL_OUTER: 8100 str = "transaction_may_cancel_outer"; 8101 break; 8102 default: 8103 gcc_unreachable (); 8104 } 8105 return get_identifier (str); 8106 } 8107 8108 /* Return the first TM attribute seen in LIST. */ 8109 8110 tree 8111 find_tm_attribute (tree list) 8112 { 8113 for (; list ; list = TREE_CHAIN (list)) 8114 { 8115 tree name = TREE_PURPOSE (list); 8116 if (tm_attr_to_mask (name) != 0) 8117 return name; 8118 } 8119 return NULL_TREE; 8120 } 8121 8122 /* Handle the TM attributes; arguments as in struct attribute_spec.handler. 8123 Here we accept only function types, and verify that none of the other 8124 function TM attributes are also applied. */ 8125 /* ??? We need to accept class types for C++, but not C. This greatly 8126 complicates this function, since we can no longer rely on the extra 8127 processing given by function_type_required. */ 8128 8129 static tree 8130 handle_tm_attribute (tree *node, tree name, tree args, 8131 int flags, bool *no_add_attrs) 8132 { 8133 /* Only one path adds the attribute; others don't. */ 8134 *no_add_attrs = true; 8135 8136 switch (TREE_CODE (*node)) 8137 { 8138 case RECORD_TYPE: 8139 case UNION_TYPE: 8140 /* Only tm_callable and tm_safe apply to classes. */ 8141 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE)) 8142 goto ignored; 8143 /* FALLTHRU */ 8144 8145 case FUNCTION_TYPE: 8146 case METHOD_TYPE: 8147 { 8148 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node)); 8149 if (old_name == name) 8150 ; 8151 else if (old_name != NULL_TREE) 8152 error ("type was previously declared %qE", old_name); 8153 else 8154 *no_add_attrs = false; 8155 } 8156 break; 8157 8158 case POINTER_TYPE: 8159 { 8160 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node)); 8161 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE) 8162 { 8163 tree fn_tmp = TREE_TYPE (*node); 8164 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0); 8165 *node = build_pointer_type (fn_tmp); 8166 break; 8167 } 8168 } 8169 /* FALLTHRU */ 8170 8171 default: 8172 /* If a function is next, pass it on to be tried next. */ 8173 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT) 8174 return tree_cons (name, args, NULL); 8175 8176 ignored: 8177 warning (OPT_Wattributes, "%qE attribute ignored", name); 8178 break; 8179 } 8180 8181 return NULL_TREE; 8182 } 8183 8184 /* Handle the TM_WRAP attribute; arguments as in 8185 struct attribute_spec.handler. */ 8186 8187 static tree 8188 handle_tm_wrap_attribute (tree *node, tree name, tree args, 8189 int ARG_UNUSED (flags), bool *no_add_attrs) 8190 { 8191 tree decl = *node; 8192 8193 /* We don't need the attribute even on success, since we 8194 record the entry in an external table. */ 8195 *no_add_attrs = true; 8196 8197 if (TREE_CODE (decl) != FUNCTION_DECL) 8198 warning (OPT_Wattributes, "%qE attribute ignored", name); 8199 else 8200 { 8201 tree wrap_decl = TREE_VALUE (args); 8202 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE 8203 && TREE_CODE (wrap_decl) != VAR_DECL 8204 && TREE_CODE (wrap_decl) != FUNCTION_DECL) 8205 error ("%qE argument not an identifier", name); 8206 else 8207 { 8208 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE) 8209 wrap_decl = lookup_name (wrap_decl); 8210 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL) 8211 { 8212 if (lang_hooks.types_compatible_p (TREE_TYPE (decl), 8213 TREE_TYPE (wrap_decl))) 8214 record_tm_replacement (wrap_decl, decl); 8215 else 8216 error ("%qD is not compatible with %qD", wrap_decl, decl); 8217 } 8218 else 8219 error ("transaction_wrap argument is not a function"); 8220 } 8221 } 8222 8223 return NULL_TREE; 8224 } 8225 8226 /* Ignore the given attribute. Used when this attribute may be usefully 8227 overridden by the target, but is not used generically. */ 8228 8229 static tree 8230 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name), 8231 tree ARG_UNUSED (args), int ARG_UNUSED (flags), 8232 bool *no_add_attrs) 8233 { 8234 *no_add_attrs = true; 8235 return NULL_TREE; 8236 } 8237 8238 /* Handle a "no vops" attribute; arguments as in 8239 struct attribute_spec.handler. */ 8240 8241 static tree 8242 handle_novops_attribute (tree *node, tree ARG_UNUSED (name), 8243 tree ARG_UNUSED (args), int ARG_UNUSED (flags), 8244 bool *ARG_UNUSED (no_add_attrs)) 8245 { 8246 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL); 8247 DECL_IS_NOVOPS (*node) = 1; 8248 return NULL_TREE; 8249 } 8250 8251 /* Handle a "deprecated" attribute; arguments as in 8252 struct attribute_spec.handler. */ 8253 8254 static tree 8255 handle_deprecated_attribute (tree *node, tree name, 8256 tree args, int flags, 8257 bool *no_add_attrs) 8258 { 8259 tree type = NULL_TREE; 8260 int warn = 0; 8261 tree what = NULL_TREE; 8262 8263 if (!args) 8264 *no_add_attrs = true; 8265 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST) 8266 { 8267 error ("deprecated message is not a string"); 8268 *no_add_attrs = true; 8269 } 8270 8271 if (DECL_P (*node)) 8272 { 8273 tree decl = *node; 8274 type = TREE_TYPE (decl); 8275 8276 if (TREE_CODE (decl) == TYPE_DECL 8277 || TREE_CODE (decl) == PARM_DECL 8278 || TREE_CODE (decl) == VAR_DECL 8279 || TREE_CODE (decl) == FUNCTION_DECL 8280 || TREE_CODE (decl) == FIELD_DECL 8281 || objc_method_decl (TREE_CODE (decl))) 8282 TREE_DEPRECATED (decl) = 1; 8283 else 8284 warn = 1; 8285 } 8286 else if (TYPE_P (*node)) 8287 { 8288 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) 8289 *node = build_variant_type_copy (*node); 8290 TREE_DEPRECATED (*node) = 1; 8291 type = *node; 8292 } 8293 else 8294 warn = 1; 8295 8296 if (warn) 8297 { 8298 *no_add_attrs = true; 8299 if (type && TYPE_NAME (type)) 8300 { 8301 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) 8302 what = TYPE_NAME (*node); 8303 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL 8304 && DECL_NAME (TYPE_NAME (type))) 8305 what = DECL_NAME (TYPE_NAME (type)); 8306 } 8307 if (what) 8308 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what); 8309 else 8310 warning (OPT_Wattributes, "%qE attribute ignored", name); 8311 } 8312 8313 return NULL_TREE; 8314 } 8315 8316 /* Handle a "vector_size" attribute; arguments as in 8317 struct attribute_spec.handler. */ 8318 8319 static tree 8320 handle_vector_size_attribute (tree *node, tree name, tree args, 8321 int ARG_UNUSED (flags), 8322 bool *no_add_attrs) 8323 { 8324 unsigned HOST_WIDE_INT vecsize, nunits; 8325 enum machine_mode orig_mode; 8326 tree type = *node, new_type, size; 8327 8328 *no_add_attrs = true; 8329 8330 size = TREE_VALUE (args); 8331 8332 if (!host_integerp (size, 1)) 8333 { 8334 warning (OPT_Wattributes, "%qE attribute ignored", name); 8335 return NULL_TREE; 8336 } 8337 8338 /* Get the vector size (in bytes). */ 8339 vecsize = tree_low_cst (size, 1); 8340 8341 /* We need to provide for vector pointers, vector arrays, and 8342 functions returning vectors. For example: 8343 8344 __attribute__((vector_size(16))) short *foo; 8345 8346 In this case, the mode is SI, but the type being modified is 8347 HI, so we need to look further. */ 8348 8349 while (POINTER_TYPE_P (type) 8350 || TREE_CODE (type) == FUNCTION_TYPE 8351 || TREE_CODE (type) == METHOD_TYPE 8352 || TREE_CODE (type) == ARRAY_TYPE 8353 || TREE_CODE (type) == OFFSET_TYPE) 8354 type = TREE_TYPE (type); 8355 8356 /* Get the mode of the type being modified. */ 8357 orig_mode = TYPE_MODE (type); 8358 8359 if ((!INTEGRAL_TYPE_P (type) 8360 && !SCALAR_FLOAT_TYPE_P (type) 8361 && !FIXED_POINT_TYPE_P (type)) 8362 || (!SCALAR_FLOAT_MODE_P (orig_mode) 8363 && GET_MODE_CLASS (orig_mode) != MODE_INT 8364 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode)) 8365 || !host_integerp (TYPE_SIZE_UNIT (type), 1) 8366 || TREE_CODE (type) == BOOLEAN_TYPE) 8367 { 8368 error ("invalid vector type for attribute %qE", name); 8369 return NULL_TREE; 8370 } 8371 8372 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1)) 8373 { 8374 error ("vector size not an integral multiple of component size"); 8375 return NULL; 8376 } 8377 8378 if (vecsize == 0) 8379 { 8380 error ("zero vector size"); 8381 return NULL; 8382 } 8383 8384 /* Calculate how many units fit in the vector. */ 8385 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1); 8386 if (nunits & (nunits - 1)) 8387 { 8388 error ("number of components of the vector not a power of two"); 8389 return NULL_TREE; 8390 } 8391 8392 new_type = build_vector_type (type, nunits); 8393 8394 /* Build back pointers if needed. */ 8395 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type); 8396 8397 return NULL_TREE; 8398 } 8399 8400 /* Handle the "nonnull" attribute. */ 8401 static tree 8402 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name), 8403 tree args, int ARG_UNUSED (flags), 8404 bool *no_add_attrs) 8405 { 8406 tree type = *node; 8407 unsigned HOST_WIDE_INT attr_arg_num; 8408 8409 /* If no arguments are specified, all pointer arguments should be 8410 non-null. Verify a full prototype is given so that the arguments 8411 will have the correct types when we actually check them later. */ 8412 if (!args) 8413 { 8414 if (!prototype_p (type)) 8415 { 8416 error ("nonnull attribute without arguments on a non-prototype"); 8417 *no_add_attrs = true; 8418 } 8419 return NULL_TREE; 8420 } 8421 8422 /* Argument list specified. Verify that each argument number references 8423 a pointer argument. */ 8424 for (attr_arg_num = 1; args; args = TREE_CHAIN (args)) 8425 { 8426 unsigned HOST_WIDE_INT arg_num = 0, ck_num; 8427 8428 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num)) 8429 { 8430 error ("nonnull argument has invalid operand number (argument %lu)", 8431 (unsigned long) attr_arg_num); 8432 *no_add_attrs = true; 8433 return NULL_TREE; 8434 } 8435 8436 if (prototype_p (type)) 8437 { 8438 function_args_iterator iter; 8439 tree argument; 8440 8441 function_args_iter_init (&iter, type); 8442 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter)) 8443 { 8444 argument = function_args_iter_cond (&iter); 8445 if (argument == NULL_TREE || ck_num == arg_num) 8446 break; 8447 } 8448 8449 if (!argument 8450 || TREE_CODE (argument) == VOID_TYPE) 8451 { 8452 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)", 8453 (unsigned long) attr_arg_num, (unsigned long) arg_num); 8454 *no_add_attrs = true; 8455 return NULL_TREE; 8456 } 8457 8458 if (TREE_CODE (argument) != POINTER_TYPE) 8459 { 8460 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)", 8461 (unsigned long) attr_arg_num, (unsigned long) arg_num); 8462 *no_add_attrs = true; 8463 return NULL_TREE; 8464 } 8465 } 8466 } 8467 8468 return NULL_TREE; 8469 } 8470 8471 /* Check the argument list of a function call for null in argument slots 8472 that are marked as requiring a non-null pointer argument. The NARGS 8473 arguments are passed in the array ARGARRAY. 8474 */ 8475 8476 static void 8477 check_function_nonnull (tree attrs, int nargs, tree *argarray) 8478 { 8479 tree a; 8480 int i; 8481 8482 attrs = lookup_attribute ("nonnull", attrs); 8483 if (attrs == NULL_TREE) 8484 return; 8485 8486 a = attrs; 8487 /* See if any of the nonnull attributes has no arguments. If so, 8488 then every pointer argument is checked (in which case the check 8489 for pointer type is done in check_nonnull_arg). */ 8490 if (TREE_VALUE (a) != NULL_TREE) 8491 do 8492 a = lookup_attribute ("nonnull", TREE_CHAIN (a)); 8493 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE); 8494 8495 if (a != NULL_TREE) 8496 for (i = 0; i < nargs; i++) 8497 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i], 8498 i + 1); 8499 else 8500 { 8501 /* Walk the argument list. If we encounter an argument number we 8502 should check for non-null, do it. */ 8503 for (i = 0; i < nargs; i++) 8504 { 8505 for (a = attrs; ; a = TREE_CHAIN (a)) 8506 { 8507 a = lookup_attribute ("nonnull", a); 8508 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1)) 8509 break; 8510 } 8511 8512 if (a != NULL_TREE) 8513 check_function_arguments_recurse (check_nonnull_arg, NULL, 8514 argarray[i], i + 1); 8515 } 8516 } 8517 } 8518 8519 /* Check that the Nth argument of a function call (counting backwards 8520 from the end) is a (pointer)0. The NARGS arguments are passed in the 8521 array ARGARRAY. */ 8522 8523 static void 8524 check_function_sentinel (const_tree fntype, int nargs, tree *argarray) 8525 { 8526 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype)); 8527 8528 if (attr) 8529 { 8530 int len = 0; 8531 int pos = 0; 8532 tree sentinel; 8533 function_args_iterator iter; 8534 tree t; 8535 8536 /* Skip over the named arguments. */ 8537 FOREACH_FUNCTION_ARGS (fntype, t, iter) 8538 { 8539 if (len == nargs) 8540 break; 8541 len++; 8542 } 8543 8544 if (TREE_VALUE (attr)) 8545 { 8546 tree p = TREE_VALUE (TREE_VALUE (attr)); 8547 pos = TREE_INT_CST_LOW (p); 8548 } 8549 8550 /* The sentinel must be one of the varargs, i.e. 8551 in position >= the number of fixed arguments. */ 8552 if ((nargs - 1 - pos) < len) 8553 { 8554 warning (OPT_Wformat_, 8555 "not enough variable arguments to fit a sentinel"); 8556 return; 8557 } 8558 8559 /* Validate the sentinel. */ 8560 sentinel = argarray[nargs - 1 - pos]; 8561 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel)) 8562 || !integer_zerop (sentinel)) 8563 /* Although __null (in C++) is only an integer we allow it 8564 nevertheless, as we are guaranteed that it's exactly 8565 as wide as a pointer, and we don't want to force 8566 users to cast the NULL they have written there. 8567 We warn with -Wstrict-null-sentinel, though. */ 8568 && (warn_strict_null_sentinel || null_node != sentinel)) 8569 warning (OPT_Wformat_, "missing sentinel in function call"); 8570 } 8571 } 8572 8573 /* Helper for check_function_nonnull; given a list of operands which 8574 must be non-null in ARGS, determine if operand PARAM_NUM should be 8575 checked. */ 8576 8577 static bool 8578 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num) 8579 { 8580 unsigned HOST_WIDE_INT arg_num = 0; 8581 8582 for (; args; args = TREE_CHAIN (args)) 8583 { 8584 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num); 8585 8586 gcc_assert (found); 8587 8588 if (arg_num == param_num) 8589 return true; 8590 } 8591 return false; 8592 } 8593 8594 /* Check that the function argument PARAM (which is operand number 8595 PARAM_NUM) is non-null. This is called by check_function_nonnull 8596 via check_function_arguments_recurse. */ 8597 8598 static void 8599 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param, 8600 unsigned HOST_WIDE_INT param_num) 8601 { 8602 /* Just skip checking the argument if it's not a pointer. This can 8603 happen if the "nonnull" attribute was given without an operand 8604 list (which means to check every pointer argument). */ 8605 8606 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE) 8607 return; 8608 8609 if (integer_zerop (param)) 8610 warning (OPT_Wnonnull, "null argument where non-null required " 8611 "(argument %lu)", (unsigned long) param_num); 8612 } 8613 8614 /* Helper for nonnull attribute handling; fetch the operand number 8615 from the attribute argument list. */ 8616 8617 static bool 8618 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp) 8619 { 8620 /* Verify the arg number is a constant. */ 8621 if (TREE_CODE (arg_num_expr) != INTEGER_CST 8622 || TREE_INT_CST_HIGH (arg_num_expr) != 0) 8623 return false; 8624 8625 *valp = TREE_INT_CST_LOW (arg_num_expr); 8626 return true; 8627 } 8628 8629 /* Handle a "nothrow" attribute; arguments as in 8630 struct attribute_spec.handler. */ 8631 8632 static tree 8633 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args), 8634 int ARG_UNUSED (flags), bool *no_add_attrs) 8635 { 8636 if (TREE_CODE (*node) == FUNCTION_DECL) 8637 TREE_NOTHROW (*node) = 1; 8638 /* ??? TODO: Support types. */ 8639 else 8640 { 8641 warning (OPT_Wattributes, "%qE attribute ignored", name); 8642 *no_add_attrs = true; 8643 } 8644 8645 return NULL_TREE; 8646 } 8647 8648 /* Handle a "cleanup" attribute; arguments as in 8649 struct attribute_spec.handler. */ 8650 8651 static tree 8652 handle_cleanup_attribute (tree *node, tree name, tree args, 8653 int ARG_UNUSED (flags), bool *no_add_attrs) 8654 { 8655 tree decl = *node; 8656 tree cleanup_id, cleanup_decl; 8657 8658 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do 8659 for global destructors in C++. This requires infrastructure that 8660 we don't have generically at the moment. It's also not a feature 8661 we'd be missing too much, since we do have attribute constructor. */ 8662 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)) 8663 { 8664 warning (OPT_Wattributes, "%qE attribute ignored", name); 8665 *no_add_attrs = true; 8666 return NULL_TREE; 8667 } 8668 8669 /* Verify that the argument is a function in scope. */ 8670 /* ??? We could support pointers to functions here as well, if 8671 that was considered desirable. */ 8672 cleanup_id = TREE_VALUE (args); 8673 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE) 8674 { 8675 error ("cleanup argument not an identifier"); 8676 *no_add_attrs = true; 8677 return NULL_TREE; 8678 } 8679 cleanup_decl = lookup_name (cleanup_id); 8680 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL) 8681 { 8682 error ("cleanup argument not a function"); 8683 *no_add_attrs = true; 8684 return NULL_TREE; 8685 } 8686 8687 /* That the function has proper type is checked with the 8688 eventual call to build_function_call. */ 8689 8690 return NULL_TREE; 8691 } 8692 8693 /* Handle a "warn_unused_result" attribute. No special handling. */ 8694 8695 static tree 8696 handle_warn_unused_result_attribute (tree *node, tree name, 8697 tree ARG_UNUSED (args), 8698 int ARG_UNUSED (flags), bool *no_add_attrs) 8699 { 8700 /* Ignore the attribute for functions not returning any value. */ 8701 if (VOID_TYPE_P (TREE_TYPE (*node))) 8702 { 8703 warning (OPT_Wattributes, "%qE attribute ignored", name); 8704 *no_add_attrs = true; 8705 } 8706 8707 return NULL_TREE; 8708 } 8709 8710 /* Handle a "sentinel" attribute. */ 8711 8712 static tree 8713 handle_sentinel_attribute (tree *node, tree name, tree args, 8714 int ARG_UNUSED (flags), bool *no_add_attrs) 8715 { 8716 if (!prototype_p (*node)) 8717 { 8718 warning (OPT_Wattributes, 8719 "%qE attribute requires prototypes with named arguments", name); 8720 *no_add_attrs = true; 8721 } 8722 else 8723 { 8724 if (!stdarg_p (*node)) 8725 { 8726 warning (OPT_Wattributes, 8727 "%qE attribute only applies to variadic functions", name); 8728 *no_add_attrs = true; 8729 } 8730 } 8731 8732 if (args) 8733 { 8734 tree position = TREE_VALUE (args); 8735 8736 if (TREE_CODE (position) != INTEGER_CST) 8737 { 8738 warning (OPT_Wattributes, 8739 "requested position is not an integer constant"); 8740 *no_add_attrs = true; 8741 } 8742 else 8743 { 8744 if (tree_int_cst_lt (position, integer_zero_node)) 8745 { 8746 warning (OPT_Wattributes, 8747 "requested position is less than zero"); 8748 *no_add_attrs = true; 8749 } 8750 } 8751 } 8752 8753 return NULL_TREE; 8754 } 8755 8756 /* Handle a "type_generic" attribute. */ 8757 8758 static tree 8759 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name), 8760 tree ARG_UNUSED (args), int ARG_UNUSED (flags), 8761 bool * ARG_UNUSED (no_add_attrs)) 8762 { 8763 /* Ensure we have a function type. */ 8764 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE); 8765 8766 /* Ensure we have a variadic function. */ 8767 gcc_assert (!prototype_p (*node) || stdarg_p (*node)); 8768 8769 return NULL_TREE; 8770 } 8771 8772 /* Handle a "target" attribute. */ 8773 8774 static tree 8775 handle_target_attribute (tree *node, tree name, tree args, int flags, 8776 bool *no_add_attrs) 8777 { 8778 /* Ensure we have a function type. */ 8779 if (TREE_CODE (*node) != FUNCTION_DECL) 8780 { 8781 warning (OPT_Wattributes, "%qE attribute ignored", name); 8782 *no_add_attrs = true; 8783 } 8784 else if (! targetm.target_option.valid_attribute_p (*node, name, args, 8785 flags)) 8786 *no_add_attrs = true; 8787 8788 return NULL_TREE; 8789 } 8790 8791 /* Arguments being collected for optimization. */ 8792 typedef const char *const_char_p; /* For DEF_VEC_P. */ 8793 static GTY(()) vec<const_char_p, va_gc> *optimize_args; 8794 8795 8796 /* Inner function to convert a TREE_LIST to argv string to parse the optimize 8797 options in ARGS. ATTR_P is true if this is for attribute(optimize), and 8798 false for #pragma GCC optimize. */ 8799 8800 bool 8801 parse_optimize_options (tree args, bool attr_p) 8802 { 8803 bool ret = true; 8804 unsigned opt_argc; 8805 unsigned i; 8806 int saved_flag_strict_aliasing; 8807 const char **opt_argv; 8808 struct cl_decoded_option *decoded_options; 8809 unsigned int decoded_options_count; 8810 tree ap; 8811 8812 /* Build up argv vector. Just in case the string is stored away, use garbage 8813 collected strings. */ 8814 vec_safe_truncate (optimize_args, 0); 8815 vec_safe_push (optimize_args, (const char *) NULL); 8816 8817 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap)) 8818 { 8819 tree value = TREE_VALUE (ap); 8820 8821 if (TREE_CODE (value) == INTEGER_CST) 8822 { 8823 char buffer[20]; 8824 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value)); 8825 vec_safe_push (optimize_args, ggc_strdup (buffer)); 8826 } 8827 8828 else if (TREE_CODE (value) == STRING_CST) 8829 { 8830 /* Split string into multiple substrings. */ 8831 size_t len = TREE_STRING_LENGTH (value); 8832 char *p = ASTRDUP (TREE_STRING_POINTER (value)); 8833 char *end = p + len; 8834 char *comma; 8835 char *next_p = p; 8836 8837 while (next_p != NULL) 8838 { 8839 size_t len2; 8840 char *q, *r; 8841 8842 p = next_p; 8843 comma = strchr (p, ','); 8844 if (comma) 8845 { 8846 len2 = comma - p; 8847 *comma = '\0'; 8848 next_p = comma+1; 8849 } 8850 else 8851 { 8852 len2 = end - p; 8853 next_p = NULL; 8854 } 8855 8856 r = q = (char *) ggc_alloc_atomic (len2 + 3); 8857 8858 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx 8859 options. */ 8860 if (*p == '-' && p[1] != 'O' && p[1] != 'f') 8861 { 8862 ret = false; 8863 if (attr_p) 8864 warning (OPT_Wattributes, 8865 "bad option %s to optimize attribute", p); 8866 else 8867 warning (OPT_Wpragmas, 8868 "bad option %s to pragma attribute", p); 8869 continue; 8870 } 8871 8872 if (*p != '-') 8873 { 8874 *r++ = '-'; 8875 8876 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by 8877 itself is -Os, and any other switch begins with a -f. */ 8878 if ((*p >= '0' && *p <= '9') 8879 || (p[0] == 's' && p[1] == '\0')) 8880 *r++ = 'O'; 8881 else if (*p != 'O') 8882 *r++ = 'f'; 8883 } 8884 8885 memcpy (r, p, len2); 8886 r[len2] = '\0'; 8887 vec_safe_push (optimize_args, (const char *) q); 8888 } 8889 8890 } 8891 } 8892 8893 opt_argc = optimize_args->length (); 8894 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1)); 8895 8896 for (i = 1; i < opt_argc; i++) 8897 opt_argv[i] = (*optimize_args)[i]; 8898 8899 saved_flag_strict_aliasing = flag_strict_aliasing; 8900 8901 /* Now parse the options. */ 8902 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv, 8903 &decoded_options, 8904 &decoded_options_count); 8905 decode_options (&global_options, &global_options_set, 8906 decoded_options, decoded_options_count, 8907 input_location, global_dc); 8908 8909 targetm.override_options_after_change(); 8910 8911 /* Don't allow changing -fstrict-aliasing. */ 8912 flag_strict_aliasing = saved_flag_strict_aliasing; 8913 8914 optimize_args->truncate (0); 8915 return ret; 8916 } 8917 8918 /* For handling "optimize" attribute. arguments as in 8919 struct attribute_spec.handler. */ 8920 8921 static tree 8922 handle_optimize_attribute (tree *node, tree name, tree args, 8923 int ARG_UNUSED (flags), bool *no_add_attrs) 8924 { 8925 /* Ensure we have a function type. */ 8926 if (TREE_CODE (*node) != FUNCTION_DECL) 8927 { 8928 warning (OPT_Wattributes, "%qE attribute ignored", name); 8929 *no_add_attrs = true; 8930 } 8931 else 8932 { 8933 struct cl_optimization cur_opts; 8934 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node); 8935 8936 /* Save current options. */ 8937 cl_optimization_save (&cur_opts, &global_options); 8938 8939 /* If we previously had some optimization options, use them as the 8940 default. */ 8941 if (old_opts) 8942 cl_optimization_restore (&global_options, 8943 TREE_OPTIMIZATION (old_opts)); 8944 8945 /* Parse options, and update the vector. */ 8946 parse_optimize_options (args, true); 8947 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) 8948 = build_optimization_node (); 8949 8950 /* Restore current options. */ 8951 cl_optimization_restore (&global_options, &cur_opts); 8952 } 8953 8954 return NULL_TREE; 8955 } 8956 8957 /* Handle a "no_split_stack" attribute. */ 8958 8959 static tree 8960 handle_no_split_stack_attribute (tree *node, tree name, 8961 tree ARG_UNUSED (args), 8962 int ARG_UNUSED (flags), 8963 bool *no_add_attrs) 8964 { 8965 tree decl = *node; 8966 8967 if (TREE_CODE (decl) != FUNCTION_DECL) 8968 { 8969 error_at (DECL_SOURCE_LOCATION (decl), 8970 "%qE attribute applies only to functions", name); 8971 *no_add_attrs = true; 8972 } 8973 else if (DECL_INITIAL (decl)) 8974 { 8975 error_at (DECL_SOURCE_LOCATION (decl), 8976 "can%'t set %qE attribute after definition", name); 8977 *no_add_attrs = true; 8978 } 8979 8980 return NULL_TREE; 8981 } 8982 8983 /* Check for valid arguments being passed to a function with FNTYPE. 8984 There are NARGS arguments in the array ARGARRAY. */ 8985 void 8986 check_function_arguments (const_tree fntype, int nargs, tree *argarray) 8987 { 8988 /* Check for null being passed in a pointer argument that must be 8989 non-null. We also need to do this if format checking is enabled. */ 8990 8991 if (warn_nonnull) 8992 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray); 8993 8994 /* Check for errors in format strings. */ 8995 8996 if (warn_format || warn_suggest_attribute_format) 8997 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray); 8998 8999 if (warn_format) 9000 check_function_sentinel (fntype, nargs, argarray); 9001 } 9002 9003 /* Generic argument checking recursion routine. PARAM is the argument to 9004 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked 9005 once the argument is resolved. CTX is context for the callback. */ 9006 void 9007 check_function_arguments_recurse (void (*callback) 9008 (void *, tree, unsigned HOST_WIDE_INT), 9009 void *ctx, tree param, 9010 unsigned HOST_WIDE_INT param_num) 9011 { 9012 if (CONVERT_EXPR_P (param) 9013 && (TYPE_PRECISION (TREE_TYPE (param)) 9014 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0))))) 9015 { 9016 /* Strip coercion. */ 9017 check_function_arguments_recurse (callback, ctx, 9018 TREE_OPERAND (param, 0), param_num); 9019 return; 9020 } 9021 9022 if (TREE_CODE (param) == CALL_EXPR) 9023 { 9024 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param))); 9025 tree attrs; 9026 bool found_format_arg = false; 9027 9028 /* See if this is a call to a known internationalization function 9029 that modifies a format arg. Such a function may have multiple 9030 format_arg attributes (for example, ngettext). */ 9031 9032 for (attrs = TYPE_ATTRIBUTES (type); 9033 attrs; 9034 attrs = TREE_CHAIN (attrs)) 9035 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs))) 9036 { 9037 tree inner_arg; 9038 tree format_num_expr; 9039 int format_num; 9040 int i; 9041 call_expr_arg_iterator iter; 9042 9043 /* Extract the argument number, which was previously checked 9044 to be valid. */ 9045 format_num_expr = TREE_VALUE (TREE_VALUE (attrs)); 9046 9047 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST 9048 && !TREE_INT_CST_HIGH (format_num_expr)); 9049 9050 format_num = TREE_INT_CST_LOW (format_num_expr); 9051 9052 for (inner_arg = first_call_expr_arg (param, &iter), i = 1; 9053 inner_arg != 0; 9054 inner_arg = next_call_expr_arg (&iter), i++) 9055 if (i == format_num) 9056 { 9057 check_function_arguments_recurse (callback, ctx, 9058 inner_arg, param_num); 9059 found_format_arg = true; 9060 break; 9061 } 9062 } 9063 9064 /* If we found a format_arg attribute and did a recursive check, 9065 we are done with checking this argument. Otherwise, we continue 9066 and this will be considered a non-literal. */ 9067 if (found_format_arg) 9068 return; 9069 } 9070 9071 if (TREE_CODE (param) == COND_EXPR) 9072 { 9073 /* Check both halves of the conditional expression. */ 9074 check_function_arguments_recurse (callback, ctx, 9075 TREE_OPERAND (param, 1), param_num); 9076 check_function_arguments_recurse (callback, ctx, 9077 TREE_OPERAND (param, 2), param_num); 9078 return; 9079 } 9080 9081 (*callback) (ctx, param, param_num); 9082 } 9083 9084 /* Checks for a builtin function FNDECL that the number of arguments 9085 NARGS against the required number REQUIRED and issues an error if 9086 there is a mismatch. Returns true if the number of arguments is 9087 correct, otherwise false. */ 9088 9089 static bool 9090 builtin_function_validate_nargs (tree fndecl, int nargs, int required) 9091 { 9092 if (nargs < required) 9093 { 9094 error_at (input_location, 9095 "not enough arguments to function %qE", fndecl); 9096 return false; 9097 } 9098 else if (nargs > required) 9099 { 9100 error_at (input_location, 9101 "too many arguments to function %qE", fndecl); 9102 return false; 9103 } 9104 return true; 9105 } 9106 9107 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL. 9108 Returns false if there was an error, otherwise true. */ 9109 9110 bool 9111 check_builtin_function_arguments (tree fndecl, int nargs, tree *args) 9112 { 9113 if (!DECL_BUILT_IN (fndecl) 9114 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL) 9115 return true; 9116 9117 switch (DECL_FUNCTION_CODE (fndecl)) 9118 { 9119 case BUILT_IN_CONSTANT_P: 9120 return builtin_function_validate_nargs (fndecl, nargs, 1); 9121 9122 case BUILT_IN_ISFINITE: 9123 case BUILT_IN_ISINF: 9124 case BUILT_IN_ISINF_SIGN: 9125 case BUILT_IN_ISNAN: 9126 case BUILT_IN_ISNORMAL: 9127 if (builtin_function_validate_nargs (fndecl, nargs, 1)) 9128 { 9129 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE) 9130 { 9131 error ("non-floating-point argument in call to " 9132 "function %qE", fndecl); 9133 return false; 9134 } 9135 return true; 9136 } 9137 return false; 9138 9139 case BUILT_IN_ISGREATER: 9140 case BUILT_IN_ISGREATEREQUAL: 9141 case BUILT_IN_ISLESS: 9142 case BUILT_IN_ISLESSEQUAL: 9143 case BUILT_IN_ISLESSGREATER: 9144 case BUILT_IN_ISUNORDERED: 9145 if (builtin_function_validate_nargs (fndecl, nargs, 2)) 9146 { 9147 enum tree_code code0, code1; 9148 code0 = TREE_CODE (TREE_TYPE (args[0])); 9149 code1 = TREE_CODE (TREE_TYPE (args[1])); 9150 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE) 9151 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE) 9152 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE))) 9153 { 9154 error ("non-floating-point arguments in call to " 9155 "function %qE", fndecl); 9156 return false; 9157 } 9158 return true; 9159 } 9160 return false; 9161 9162 case BUILT_IN_FPCLASSIFY: 9163 if (builtin_function_validate_nargs (fndecl, nargs, 6)) 9164 { 9165 unsigned i; 9166 9167 for (i=0; i<5; i++) 9168 if (TREE_CODE (args[i]) != INTEGER_CST) 9169 { 9170 error ("non-const integer argument %u in call to function %qE", 9171 i+1, fndecl); 9172 return false; 9173 } 9174 9175 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE) 9176 { 9177 error ("non-floating-point argument in call to function %qE", 9178 fndecl); 9179 return false; 9180 } 9181 return true; 9182 } 9183 return false; 9184 9185 case BUILT_IN_ASSUME_ALIGNED: 9186 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2))) 9187 { 9188 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE) 9189 { 9190 error ("non-integer argument 3 in call to function %qE", fndecl); 9191 return false; 9192 } 9193 return true; 9194 } 9195 return false; 9196 9197 default: 9198 return true; 9199 } 9200 } 9201 9202 /* Function to help qsort sort FIELD_DECLs by name order. */ 9203 9204 int 9205 field_decl_cmp (const void *x_p, const void *y_p) 9206 { 9207 const tree *const x = (const tree *const) x_p; 9208 const tree *const y = (const tree *const) y_p; 9209 9210 if (DECL_NAME (*x) == DECL_NAME (*y)) 9211 /* A nontype is "greater" than a type. */ 9212 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); 9213 if (DECL_NAME (*x) == NULL_TREE) 9214 return -1; 9215 if (DECL_NAME (*y) == NULL_TREE) 9216 return 1; 9217 if (DECL_NAME (*x) < DECL_NAME (*y)) 9218 return -1; 9219 return 1; 9220 } 9221 9222 static struct { 9223 gt_pointer_operator new_value; 9224 void *cookie; 9225 } resort_data; 9226 9227 /* This routine compares two fields like field_decl_cmp but using the 9228 pointer operator in resort_data. */ 9229 9230 static int 9231 resort_field_decl_cmp (const void *x_p, const void *y_p) 9232 { 9233 const tree *const x = (const tree *const) x_p; 9234 const tree *const y = (const tree *const) y_p; 9235 9236 if (DECL_NAME (*x) == DECL_NAME (*y)) 9237 /* A nontype is "greater" than a type. */ 9238 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); 9239 if (DECL_NAME (*x) == NULL_TREE) 9240 return -1; 9241 if (DECL_NAME (*y) == NULL_TREE) 9242 return 1; 9243 { 9244 tree d1 = DECL_NAME (*x); 9245 tree d2 = DECL_NAME (*y); 9246 resort_data.new_value (&d1, resort_data.cookie); 9247 resort_data.new_value (&d2, resort_data.cookie); 9248 if (d1 < d2) 9249 return -1; 9250 } 9251 return 1; 9252 } 9253 9254 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */ 9255 9256 void 9257 resort_sorted_fields (void *obj, 9258 void * ARG_UNUSED (orig_obj), 9259 gt_pointer_operator new_value, 9260 void *cookie) 9261 { 9262 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj; 9263 resort_data.new_value = new_value; 9264 resort_data.cookie = cookie; 9265 qsort (&sf->elts[0], sf->len, sizeof (tree), 9266 resort_field_decl_cmp); 9267 } 9268 9269 /* Subroutine of c_parse_error. 9270 Return the result of concatenating LHS and RHS. RHS is really 9271 a string literal, its first character is indicated by RHS_START and 9272 RHS_SIZE is its length (including the terminating NUL character). 9273 9274 The caller is responsible for deleting the returned pointer. */ 9275 9276 static char * 9277 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size) 9278 { 9279 const int lhs_size = strlen (lhs); 9280 char *result = XNEWVEC (char, lhs_size + rhs_size); 9281 strncpy (result, lhs, lhs_size); 9282 strncpy (result + lhs_size, rhs_start, rhs_size); 9283 return result; 9284 } 9285 9286 /* Issue the error given by GMSGID, indicating that it occurred before 9287 TOKEN, which had the associated VALUE. */ 9288 9289 void 9290 c_parse_error (const char *gmsgid, enum cpp_ttype token_type, 9291 tree value, unsigned char token_flags) 9292 { 9293 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2)) 9294 9295 char *message = NULL; 9296 9297 if (token_type == CPP_EOF) 9298 message = catenate_messages (gmsgid, " at end of input"); 9299 else if (token_type == CPP_CHAR 9300 || token_type == CPP_WCHAR 9301 || token_type == CPP_CHAR16 9302 || token_type == CPP_CHAR32) 9303 { 9304 unsigned int val = TREE_INT_CST_LOW (value); 9305 const char *prefix; 9306 9307 switch (token_type) 9308 { 9309 default: 9310 prefix = ""; 9311 break; 9312 case CPP_WCHAR: 9313 prefix = "L"; 9314 break; 9315 case CPP_CHAR16: 9316 prefix = "u"; 9317 break; 9318 case CPP_CHAR32: 9319 prefix = "U"; 9320 break; 9321 } 9322 9323 if (val <= UCHAR_MAX && ISGRAPH (val)) 9324 message = catenate_messages (gmsgid, " before %s'%c'"); 9325 else 9326 message = catenate_messages (gmsgid, " before %s'\\x%x'"); 9327 9328 error (message, prefix, val); 9329 free (message); 9330 message = NULL; 9331 } 9332 else if (token_type == CPP_STRING 9333 || token_type == CPP_WSTRING 9334 || token_type == CPP_STRING16 9335 || token_type == CPP_STRING32 9336 || token_type == CPP_UTF8STRING) 9337 message = catenate_messages (gmsgid, " before string constant"); 9338 else if (token_type == CPP_NUMBER) 9339 message = catenate_messages (gmsgid, " before numeric constant"); 9340 else if (token_type == CPP_NAME) 9341 { 9342 message = catenate_messages (gmsgid, " before %qE"); 9343 error (message, value); 9344 free (message); 9345 message = NULL; 9346 } 9347 else if (token_type == CPP_PRAGMA) 9348 message = catenate_messages (gmsgid, " before %<#pragma%>"); 9349 else if (token_type == CPP_PRAGMA_EOL) 9350 message = catenate_messages (gmsgid, " before end of line"); 9351 else if (token_type == CPP_DECLTYPE) 9352 message = catenate_messages (gmsgid, " before %<decltype%>"); 9353 else if (token_type < N_TTYPES) 9354 { 9355 message = catenate_messages (gmsgid, " before %qs token"); 9356 error (message, cpp_type2name (token_type, token_flags)); 9357 free (message); 9358 message = NULL; 9359 } 9360 else 9361 error (gmsgid); 9362 9363 if (message) 9364 { 9365 error (message); 9366 free (message); 9367 } 9368 #undef catenate_messages 9369 } 9370 9371 /* Mapping for cpp message reasons to the options that enable them. */ 9372 9373 struct reason_option_codes_t 9374 { 9375 const int reason; /* cpplib message reason. */ 9376 const int option_code; /* gcc option that controls this message. */ 9377 }; 9378 9379 static const struct reason_option_codes_t option_codes[] = { 9380 {CPP_W_DEPRECATED, OPT_Wdeprecated}, 9381 {CPP_W_COMMENTS, OPT_Wcomment}, 9382 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs}, 9383 {CPP_W_MULTICHAR, OPT_Wmultichar}, 9384 {CPP_W_TRADITIONAL, OPT_Wtraditional}, 9385 {CPP_W_LONG_LONG, OPT_Wlong_long}, 9386 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels}, 9387 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros}, 9388 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined}, 9389 {CPP_W_UNDEF, OPT_Wundef}, 9390 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros}, 9391 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat}, 9392 {CPP_W_NORMALIZE, OPT_Wnormalized_}, 9393 {CPP_W_INVALID_PCH, OPT_Winvalid_pch}, 9394 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp}, 9395 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix}, 9396 {CPP_W_NONE, 0} 9397 }; 9398 9399 /* Return the gcc option code associated with the reason for a cpp 9400 message, or 0 if none. */ 9401 9402 static int 9403 c_option_controlling_cpp_error (int reason) 9404 { 9405 const struct reason_option_codes_t *entry; 9406 9407 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++) 9408 { 9409 if (entry->reason == reason) 9410 return entry->option_code; 9411 } 9412 return 0; 9413 } 9414 9415 /* Callback from cpp_error for PFILE to print diagnostics from the 9416 preprocessor. The diagnostic is of type LEVEL, with REASON set 9417 to the reason code if LEVEL is represents a warning, at location 9418 LOCATION unless this is after lexing and the compiler's location 9419 should be used instead, with column number possibly overridden by 9420 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP 9421 the arguments. Returns true if a diagnostic was emitted, false 9422 otherwise. */ 9423 9424 bool 9425 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason, 9426 location_t location, unsigned int column_override, 9427 const char *msg, va_list *ap) 9428 { 9429 diagnostic_info diagnostic; 9430 diagnostic_t dlevel; 9431 bool save_warn_system_headers = global_dc->dc_warn_system_headers; 9432 bool ret; 9433 9434 switch (level) 9435 { 9436 case CPP_DL_WARNING_SYSHDR: 9437 if (flag_no_output) 9438 return false; 9439 global_dc->dc_warn_system_headers = 1; 9440 /* Fall through. */ 9441 case CPP_DL_WARNING: 9442 if (flag_no_output) 9443 return false; 9444 dlevel = DK_WARNING; 9445 break; 9446 case CPP_DL_PEDWARN: 9447 if (flag_no_output && !flag_pedantic_errors) 9448 return false; 9449 dlevel = DK_PEDWARN; 9450 break; 9451 case CPP_DL_ERROR: 9452 dlevel = DK_ERROR; 9453 break; 9454 case CPP_DL_ICE: 9455 dlevel = DK_ICE; 9456 break; 9457 case CPP_DL_NOTE: 9458 dlevel = DK_NOTE; 9459 break; 9460 case CPP_DL_FATAL: 9461 dlevel = DK_FATAL; 9462 break; 9463 default: 9464 gcc_unreachable (); 9465 } 9466 if (done_lexing) 9467 location = input_location; 9468 diagnostic_set_info_translated (&diagnostic, msg, ap, 9469 location, dlevel); 9470 if (column_override) 9471 diagnostic_override_column (&diagnostic, column_override); 9472 diagnostic_override_option_index (&diagnostic, 9473 c_option_controlling_cpp_error (reason)); 9474 ret = report_diagnostic (&diagnostic); 9475 if (level == CPP_DL_WARNING_SYSHDR) 9476 global_dc->dc_warn_system_headers = save_warn_system_headers; 9477 return ret; 9478 } 9479 9480 /* Convert a character from the host to the target execution character 9481 set. cpplib handles this, mostly. */ 9482 9483 HOST_WIDE_INT 9484 c_common_to_target_charset (HOST_WIDE_INT c) 9485 { 9486 /* Character constants in GCC proper are sign-extended under -fsigned-char, 9487 zero-extended under -fno-signed-char. cpplib insists that characters 9488 and character constants are always unsigned. Hence we must convert 9489 back and forth. */ 9490 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1); 9491 9492 uc = cpp_host_to_exec_charset (parse_in, uc); 9493 9494 if (flag_signed_char) 9495 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE) 9496 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE); 9497 else 9498 return uc; 9499 } 9500 9501 /* Fold an offsetof-like expression. EXPR is a nested sequence of component 9502 references with an INDIRECT_REF of a constant at the bottom; much like the 9503 traditional rendering of offsetof as a macro. Return the folded result. */ 9504 9505 tree 9506 fold_offsetof_1 (tree expr) 9507 { 9508 tree base, off, t; 9509 9510 switch (TREE_CODE (expr)) 9511 { 9512 case ERROR_MARK: 9513 return expr; 9514 9515 case VAR_DECL: 9516 error ("cannot apply %<offsetof%> to static data member %qD", expr); 9517 return error_mark_node; 9518 9519 case CALL_EXPR: 9520 case TARGET_EXPR: 9521 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded"); 9522 return error_mark_node; 9523 9524 case NOP_EXPR: 9525 case INDIRECT_REF: 9526 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0))) 9527 { 9528 error ("cannot apply %<offsetof%> to a non constant address"); 9529 return error_mark_node; 9530 } 9531 return TREE_OPERAND (expr, 0); 9532 9533 case COMPONENT_REF: 9534 base = fold_offsetof_1 (TREE_OPERAND (expr, 0)); 9535 if (base == error_mark_node) 9536 return base; 9537 9538 t = TREE_OPERAND (expr, 1); 9539 if (DECL_C_BIT_FIELD (t)) 9540 { 9541 error ("attempt to take address of bit-field structure " 9542 "member %qD", t); 9543 return error_mark_node; 9544 } 9545 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t), 9546 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t), 9547 1) 9548 / BITS_PER_UNIT)); 9549 break; 9550 9551 case ARRAY_REF: 9552 base = fold_offsetof_1 (TREE_OPERAND (expr, 0)); 9553 if (base == error_mark_node) 9554 return base; 9555 9556 t = TREE_OPERAND (expr, 1); 9557 9558 /* Check if the offset goes beyond the upper bound of the array. */ 9559 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0) 9560 { 9561 tree upbound = array_ref_up_bound (expr); 9562 if (upbound != NULL_TREE 9563 && TREE_CODE (upbound) == INTEGER_CST 9564 && !tree_int_cst_equal (upbound, 9565 TYPE_MAX_VALUE (TREE_TYPE (upbound)))) 9566 { 9567 upbound = size_binop (PLUS_EXPR, upbound, 9568 build_int_cst (TREE_TYPE (upbound), 1)); 9569 if (tree_int_cst_lt (upbound, t)) 9570 { 9571 tree v; 9572 9573 for (v = TREE_OPERAND (expr, 0); 9574 TREE_CODE (v) == COMPONENT_REF; 9575 v = TREE_OPERAND (v, 0)) 9576 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) 9577 == RECORD_TYPE) 9578 { 9579 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1)); 9580 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain)) 9581 if (TREE_CODE (fld_chain) == FIELD_DECL) 9582 break; 9583 9584 if (fld_chain) 9585 break; 9586 } 9587 /* Don't warn if the array might be considered a poor 9588 man's flexible array member with a very permissive 9589 definition thereof. */ 9590 if (TREE_CODE (v) == ARRAY_REF 9591 || TREE_CODE (v) == COMPONENT_REF) 9592 warning (OPT_Warray_bounds, 9593 "index %E denotes an offset " 9594 "greater than size of %qT", 9595 t, TREE_TYPE (TREE_OPERAND (expr, 0))); 9596 } 9597 } 9598 } 9599 9600 t = convert (sizetype, t); 9601 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t); 9602 break; 9603 9604 case COMPOUND_EXPR: 9605 /* Handle static members of volatile structs. */ 9606 t = TREE_OPERAND (expr, 1); 9607 gcc_assert (TREE_CODE (t) == VAR_DECL); 9608 return fold_offsetof_1 (t); 9609 9610 default: 9611 gcc_unreachable (); 9612 } 9613 9614 return fold_build_pointer_plus (base, off); 9615 } 9616 9617 /* Likewise, but convert it to the return type of offsetof. */ 9618 9619 tree 9620 fold_offsetof (tree expr) 9621 { 9622 return convert (size_type_node, fold_offsetof_1 (expr)); 9623 } 9624 9625 /* Warn for A ?: C expressions (with B omitted) where A is a boolean 9626 expression, because B will always be true. */ 9627 9628 void 9629 warn_for_omitted_condop (location_t location, tree cond) 9630 { 9631 if (truth_value_p (TREE_CODE (cond))) 9632 warning_at (location, OPT_Wparentheses, 9633 "the omitted middle operand in ?: will always be %<true%>, " 9634 "suggest explicit middle operand"); 9635 } 9636 9637 /* Give an error for storing into ARG, which is 'const'. USE indicates 9638 how ARG was being used. */ 9639 9640 void 9641 readonly_error (tree arg, enum lvalue_use use) 9642 { 9643 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement 9644 || use == lv_asm); 9645 /* Using this macro rather than (for example) arrays of messages 9646 ensures that all the format strings are checked at compile 9647 time. */ 9648 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \ 9649 : (use == lv_increment ? (I) \ 9650 : (use == lv_decrement ? (D) : (AS)))) 9651 if (TREE_CODE (arg) == COMPONENT_REF) 9652 { 9653 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) 9654 error (READONLY_MSG (G_("assignment of member " 9655 "%qD in read-only object"), 9656 G_("increment of member " 9657 "%qD in read-only object"), 9658 G_("decrement of member " 9659 "%qD in read-only object"), 9660 G_("member %qD in read-only object " 9661 "used as %<asm%> output")), 9662 TREE_OPERAND (arg, 1)); 9663 else 9664 error (READONLY_MSG (G_("assignment of read-only member %qD"), 9665 G_("increment of read-only member %qD"), 9666 G_("decrement of read-only member %qD"), 9667 G_("read-only member %qD used as %<asm%> output")), 9668 TREE_OPERAND (arg, 1)); 9669 } 9670 else if (TREE_CODE (arg) == VAR_DECL) 9671 error (READONLY_MSG (G_("assignment of read-only variable %qD"), 9672 G_("increment of read-only variable %qD"), 9673 G_("decrement of read-only variable %qD"), 9674 G_("read-only variable %qD used as %<asm%> output")), 9675 arg); 9676 else if (TREE_CODE (arg) == PARM_DECL) 9677 error (READONLY_MSG (G_("assignment of read-only parameter %qD"), 9678 G_("increment of read-only parameter %qD"), 9679 G_("decrement of read-only parameter %qD"), 9680 G_("read-only parameter %qD use as %<asm%> output")), 9681 arg); 9682 else if (TREE_CODE (arg) == RESULT_DECL) 9683 { 9684 gcc_assert (c_dialect_cxx ()); 9685 error (READONLY_MSG (G_("assignment of " 9686 "read-only named return value %qD"), 9687 G_("increment of " 9688 "read-only named return value %qD"), 9689 G_("decrement of " 9690 "read-only named return value %qD"), 9691 G_("read-only named return value %qD " 9692 "used as %<asm%>output")), 9693 arg); 9694 } 9695 else if (TREE_CODE (arg) == FUNCTION_DECL) 9696 error (READONLY_MSG (G_("assignment of function %qD"), 9697 G_("increment of function %qD"), 9698 G_("decrement of function %qD"), 9699 G_("function %qD used as %<asm%> output")), 9700 arg); 9701 else 9702 error (READONLY_MSG (G_("assignment of read-only location %qE"), 9703 G_("increment of read-only location %qE"), 9704 G_("decrement of read-only location %qE"), 9705 G_("read-only location %qE used as %<asm%> output")), 9706 arg); 9707 } 9708 9709 /* Print an error message for an invalid lvalue. USE says 9710 how the lvalue is being used and so selects the error message. LOC 9711 is the location for the error. */ 9712 9713 void 9714 lvalue_error (location_t loc, enum lvalue_use use) 9715 { 9716 switch (use) 9717 { 9718 case lv_assign: 9719 error_at (loc, "lvalue required as left operand of assignment"); 9720 break; 9721 case lv_increment: 9722 error_at (loc, "lvalue required as increment operand"); 9723 break; 9724 case lv_decrement: 9725 error_at (loc, "lvalue required as decrement operand"); 9726 break; 9727 case lv_addressof: 9728 error_at (loc, "lvalue required as unary %<&%> operand"); 9729 break; 9730 case lv_asm: 9731 error_at (loc, "lvalue required in asm statement"); 9732 break; 9733 default: 9734 gcc_unreachable (); 9735 } 9736 } 9737 9738 /* Print an error message for an invalid indirection of type TYPE. 9739 ERRSTRING is the name of the operator for the indirection. */ 9740 9741 void 9742 invalid_indirection_error (location_t loc, tree type, ref_operator errstring) 9743 { 9744 switch (errstring) 9745 { 9746 case RO_NULL: 9747 gcc_assert (c_dialect_cxx ()); 9748 error_at (loc, "invalid type argument (have %qT)", type); 9749 break; 9750 case RO_ARRAY_INDEXING: 9751 error_at (loc, 9752 "invalid type argument of array indexing (have %qT)", 9753 type); 9754 break; 9755 case RO_UNARY_STAR: 9756 error_at (loc, 9757 "invalid type argument of unary %<*%> (have %qT)", 9758 type); 9759 break; 9760 case RO_ARROW: 9761 error_at (loc, 9762 "invalid type argument of %<->%> (have %qT)", 9763 type); 9764 break; 9765 case RO_ARROW_STAR: 9766 error_at (loc, 9767 "invalid type argument of %<->*%> (have %qT)", 9768 type); 9769 break; 9770 case RO_IMPLICIT_CONVERSION: 9771 error_at (loc, 9772 "invalid type argument of implicit conversion (have %qT)", 9773 type); 9774 break; 9775 default: 9776 gcc_unreachable (); 9777 } 9778 } 9779 9780 /* *PTYPE is an incomplete array. Complete it with a domain based on 9781 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT 9782 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, 9783 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */ 9784 9785 int 9786 complete_array_type (tree *ptype, tree initial_value, bool do_default) 9787 { 9788 tree maxindex, type, main_type, elt, unqual_elt; 9789 int failure = 0, quals; 9790 hashval_t hashcode = 0; 9791 9792 maxindex = size_zero_node; 9793 if (initial_value) 9794 { 9795 if (TREE_CODE (initial_value) == STRING_CST) 9796 { 9797 int eltsize 9798 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); 9799 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1); 9800 } 9801 else if (TREE_CODE (initial_value) == CONSTRUCTOR) 9802 { 9803 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value); 9804 9805 if (vec_safe_is_empty (v)) 9806 { 9807 if (pedantic) 9808 failure = 3; 9809 maxindex = ssize_int (-1); 9810 } 9811 else 9812 { 9813 tree curindex; 9814 unsigned HOST_WIDE_INT cnt; 9815 constructor_elt *ce; 9816 bool fold_p = false; 9817 9818 if ((*v)[0].index) 9819 maxindex = fold_convert_loc (input_location, sizetype, 9820 (*v)[0].index); 9821 curindex = maxindex; 9822 9823 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++) 9824 { 9825 bool curfold_p = false; 9826 if (ce->index) 9827 curindex = ce->index, curfold_p = true; 9828 else 9829 { 9830 if (fold_p) 9831 curindex = fold_convert (sizetype, curindex); 9832 curindex = size_binop (PLUS_EXPR, curindex, 9833 size_one_node); 9834 } 9835 if (tree_int_cst_lt (maxindex, curindex)) 9836 maxindex = curindex, fold_p = curfold_p; 9837 } 9838 if (fold_p) 9839 maxindex = fold_convert (sizetype, maxindex); 9840 } 9841 } 9842 else 9843 { 9844 /* Make an error message unless that happened already. */ 9845 if (initial_value != error_mark_node) 9846 failure = 1; 9847 } 9848 } 9849 else 9850 { 9851 failure = 2; 9852 if (!do_default) 9853 return failure; 9854 } 9855 9856 type = *ptype; 9857 elt = TREE_TYPE (type); 9858 quals = TYPE_QUALS (strip_array_types (elt)); 9859 if (quals == 0) 9860 unqual_elt = elt; 9861 else 9862 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals)); 9863 9864 /* Using build_distinct_type_copy and modifying things afterward instead 9865 of using build_array_type to create a new type preserves all of the 9866 TYPE_LANG_FLAG_? bits that the front end may have set. */ 9867 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); 9868 TREE_TYPE (main_type) = unqual_elt; 9869 TYPE_DOMAIN (main_type) 9870 = build_range_type (TREE_TYPE (maxindex), 9871 build_int_cst (TREE_TYPE (maxindex), 0), maxindex); 9872 layout_type (main_type); 9873 9874 /* Make sure we have the canonical MAIN_TYPE. */ 9875 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode); 9876 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)), 9877 hashcode); 9878 main_type = type_hash_canon (hashcode, main_type); 9879 9880 /* Fix the canonical type. */ 9881 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type)) 9882 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type))) 9883 SET_TYPE_STRUCTURAL_EQUALITY (main_type); 9884 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type) 9885 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type)) 9886 != TYPE_DOMAIN (main_type))) 9887 TYPE_CANONICAL (main_type) 9888 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)), 9889 TYPE_CANONICAL (TYPE_DOMAIN (main_type))); 9890 else 9891 TYPE_CANONICAL (main_type) = main_type; 9892 9893 if (quals == 0) 9894 type = main_type; 9895 else 9896 type = c_build_qualified_type (main_type, quals); 9897 9898 if (COMPLETE_TYPE_P (type) 9899 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST 9900 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type))) 9901 { 9902 error ("size of array is too large"); 9903 /* If we proceed with the array type as it is, we'll eventually 9904 crash in tree_low_cst(). */ 9905 type = error_mark_node; 9906 } 9907 9908 *ptype = type; 9909 return failure; 9910 } 9911 9912 /* Like c_mark_addressable but don't check register qualifier. */ 9913 void 9914 c_common_mark_addressable_vec (tree t) 9915 { 9916 while (handled_component_p (t)) 9917 t = TREE_OPERAND (t, 0); 9918 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 9919 return; 9920 TREE_ADDRESSABLE (t) = 1; 9921 } 9922 9923 9924 9925 /* Used to help initialize the builtin-types.def table. When a type of 9926 the correct size doesn't exist, use error_mark_node instead of NULL. 9927 The later results in segfaults even when a decl using the type doesn't 9928 get invoked. */ 9929 9930 tree 9931 builtin_type_for_size (int size, bool unsignedp) 9932 { 9933 tree type = c_common_type_for_size (size, unsignedp); 9934 return type ? type : error_mark_node; 9935 } 9936 9937 /* A helper function for resolve_overloaded_builtin in resolving the 9938 overloaded __sync_ builtins. Returns a positive power of 2 if the 9939 first operand of PARAMS is a pointer to a supported data type. 9940 Returns 0 if an error is encountered. */ 9941 9942 static int 9943 sync_resolve_size (tree function, vec<tree, va_gc> *params) 9944 { 9945 tree type; 9946 int size; 9947 9948 if (!params) 9949 { 9950 error ("too few arguments to function %qE", function); 9951 return 0; 9952 } 9953 9954 type = TREE_TYPE ((*params)[0]); 9955 if (TREE_CODE (type) != POINTER_TYPE) 9956 goto incompatible; 9957 9958 type = TREE_TYPE (type); 9959 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type)) 9960 goto incompatible; 9961 9962 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1); 9963 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16) 9964 return size; 9965 9966 incompatible: 9967 error ("incompatible type for argument %d of %qE", 1, function); 9968 return 0; 9969 } 9970 9971 /* A helper function for resolve_overloaded_builtin. Adds casts to 9972 PARAMS to make arguments match up with those of FUNCTION. Drops 9973 the variadic arguments at the end. Returns false if some error 9974 was encountered; true on success. */ 9975 9976 static bool 9977 sync_resolve_params (location_t loc, tree orig_function, tree function, 9978 vec<tree, va_gc> *params, bool orig_format) 9979 { 9980 function_args_iterator iter; 9981 tree ptype; 9982 unsigned int parmnum; 9983 9984 function_args_iter_init (&iter, TREE_TYPE (function)); 9985 /* We've declared the implementation functions to use "volatile void *" 9986 as the pointer parameter, so we shouldn't get any complaints from the 9987 call to check_function_arguments what ever type the user used. */ 9988 function_args_iter_next (&iter); 9989 ptype = TREE_TYPE (TREE_TYPE ((*params)[0])); 9990 9991 /* For the rest of the values, we need to cast these to FTYPE, so that we 9992 don't get warnings for passing pointer types, etc. */ 9993 parmnum = 0; 9994 while (1) 9995 { 9996 tree val, arg_type; 9997 9998 arg_type = function_args_iter_cond (&iter); 9999 /* XXX void_type_node belies the abstraction. */ 10000 if (arg_type == void_type_node) 10001 break; 10002 10003 ++parmnum; 10004 if (params->length () <= parmnum) 10005 { 10006 error_at (loc, "too few arguments to function %qE", orig_function); 10007 return false; 10008 } 10009 10010 /* Only convert parameters if arg_type is unsigned integer type with 10011 new format sync routines, i.e. don't attempt to convert pointer 10012 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n), 10013 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel 10014 kinds). */ 10015 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type)) 10016 { 10017 /* Ideally for the first conversion we'd use convert_for_assignment 10018 so that we get warnings for anything that doesn't match the pointer 10019 type. This isn't portable across the C and C++ front ends atm. */ 10020 val = (*params)[parmnum]; 10021 val = convert (ptype, val); 10022 val = convert (arg_type, val); 10023 (*params)[parmnum] = val; 10024 } 10025 10026 function_args_iter_next (&iter); 10027 } 10028 10029 /* __atomic routines are not variadic. */ 10030 if (!orig_format && params->length () != parmnum + 1) 10031 { 10032 error_at (loc, "too many arguments to function %qE", orig_function); 10033 return false; 10034 } 10035 10036 /* The definition of these primitives is variadic, with the remaining 10037 being "an optional list of variables protected by the memory barrier". 10038 No clue what that's supposed to mean, precisely, but we consider all 10039 call-clobbered variables to be protected so we're safe. */ 10040 params->truncate (parmnum + 1); 10041 10042 return true; 10043 } 10044 10045 /* A helper function for resolve_overloaded_builtin. Adds a cast to 10046 RESULT to make it match the type of the first pointer argument in 10047 PARAMS. */ 10048 10049 static tree 10050 sync_resolve_return (tree first_param, tree result, bool orig_format) 10051 { 10052 tree ptype = TREE_TYPE (TREE_TYPE (first_param)); 10053 tree rtype = TREE_TYPE (result); 10054 ptype = TYPE_MAIN_VARIANT (ptype); 10055 10056 /* New format doesn't require casting unless the types are the same size. */ 10057 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype))) 10058 return convert (ptype, result); 10059 else 10060 return result; 10061 } 10062 10063 /* This function verifies the PARAMS to generic atomic FUNCTION. 10064 It returns the size if all the parameters are the same size, otherwise 10065 0 is returned if the parameters are invalid. */ 10066 10067 static int 10068 get_atomic_generic_size (location_t loc, tree function, 10069 vec<tree, va_gc> *params) 10070 { 10071 unsigned int n_param; 10072 unsigned int n_model; 10073 unsigned int x; 10074 int size_0; 10075 tree type_0; 10076 10077 /* Determine the parameter makeup. */ 10078 switch (DECL_FUNCTION_CODE (function)) 10079 { 10080 case BUILT_IN_ATOMIC_EXCHANGE: 10081 n_param = 4; 10082 n_model = 1; 10083 break; 10084 case BUILT_IN_ATOMIC_LOAD: 10085 case BUILT_IN_ATOMIC_STORE: 10086 n_param = 3; 10087 n_model = 1; 10088 break; 10089 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: 10090 n_param = 6; 10091 n_model = 2; 10092 break; 10093 default: 10094 gcc_unreachable (); 10095 } 10096 10097 if (vec_safe_length (params) != n_param) 10098 { 10099 error_at (loc, "incorrect number of arguments to function %qE", function); 10100 return 0; 10101 } 10102 10103 /* Get type of first parameter, and determine its size. */ 10104 type_0 = TREE_TYPE ((*params)[0]); 10105 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0))) 10106 { 10107 error_at (loc, "argument 1 of %qE must be a non-void pointer type", 10108 function); 10109 return 0; 10110 } 10111 10112 /* Types must be compile time constant sizes. */ 10113 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST) 10114 { 10115 error_at (loc, 10116 "argument 1 of %qE must be a pointer to a constant size type", 10117 function); 10118 return 0; 10119 } 10120 10121 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1); 10122 10123 /* Zero size objects are not allowed. */ 10124 if (size_0 == 0) 10125 { 10126 error_at (loc, 10127 "argument 1 of %qE must be a pointer to a nonzero size object", 10128 function); 10129 return 0; 10130 } 10131 10132 /* Check each other parameter is a pointer and the same size. */ 10133 for (x = 0; x < n_param - n_model; x++) 10134 { 10135 int size; 10136 tree type = TREE_TYPE ((*params)[x]); 10137 /* __atomic_compare_exchange has a bool in the 4th postion, skip it. */ 10138 if (n_param == 6 && x == 3) 10139 continue; 10140 if (!POINTER_TYPE_P (type)) 10141 { 10142 error_at (loc, "argument %d of %qE must be a pointer type", x + 1, 10143 function); 10144 return 0; 10145 } 10146 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type)); 10147 size = type_size ? tree_low_cst (type_size, 1) : 0; 10148 if (size != size_0) 10149 { 10150 error_at (loc, "size mismatch in argument %d of %qE", x + 1, 10151 function); 10152 return 0; 10153 } 10154 } 10155 10156 /* Check memory model parameters for validity. */ 10157 for (x = n_param - n_model ; x < n_param; x++) 10158 { 10159 tree p = (*params)[x]; 10160 if (TREE_CODE (p) == INTEGER_CST) 10161 { 10162 int i = tree_low_cst (p, 1); 10163 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST) 10164 { 10165 warning_at (loc, OPT_Winvalid_memory_model, 10166 "invalid memory model argument %d of %qE", x + 1, 10167 function); 10168 } 10169 } 10170 else 10171 if (!INTEGRAL_TYPE_P (TREE_TYPE (p))) 10172 { 10173 error_at (loc, "non-integer memory model argument %d of %qE", x + 1, 10174 function); 10175 return 0; 10176 } 10177 } 10178 10179 return size_0; 10180 } 10181 10182 10183 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N 10184 at the beginning of the parameter list PARAMS representing the size of the 10185 objects. This is to match the library ABI requirement. LOC is the location 10186 of the function call. 10187 The new function is returned if it needed rebuilding, otherwise NULL_TREE is 10188 returned to allow the external call to be constructed. */ 10189 10190 static tree 10191 add_atomic_size_parameter (unsigned n, location_t loc, tree function, 10192 vec<tree, va_gc> *params) 10193 { 10194 tree size_node; 10195 10196 /* Insert a SIZE_T parameter as the first param. If there isn't 10197 enough space, allocate a new vector and recursively re-build with that. */ 10198 if (!params->space (1)) 10199 { 10200 unsigned int z, len; 10201 vec<tree, va_gc> *v; 10202 tree f; 10203 10204 len = params->length (); 10205 vec_alloc (v, len + 1); 10206 v->quick_push (build_int_cst (size_type_node, n)); 10207 for (z = 0; z < len; z++) 10208 v->quick_push ((*params)[z]); 10209 f = build_function_call_vec (loc, function, v, NULL); 10210 vec_free (v); 10211 return f; 10212 } 10213 10214 /* Add the size parameter and leave as a function call for processing. */ 10215 size_node = build_int_cst (size_type_node, n); 10216 params->quick_insert (0, size_node); 10217 return NULL_TREE; 10218 } 10219 10220 10221 /* This will process an __atomic_exchange function call, determine whether it 10222 needs to be mapped to the _N variation, or turned into a library call. 10223 LOC is the location of the builtin call. 10224 FUNCTION is the DECL that has been invoked; 10225 PARAMS is the argument list for the call. The return value is non-null 10226 TRUE is returned if it is translated into the proper format for a call to the 10227 external library, and NEW_RETURN is set the tree for that function. 10228 FALSE is returned if processing for the _N variation is required, and 10229 NEW_RETURN is set to the the return value the result is copied into. */ 10230 static bool 10231 resolve_overloaded_atomic_exchange (location_t loc, tree function, 10232 vec<tree, va_gc> *params, tree *new_return) 10233 { 10234 tree p0, p1, p2, p3; 10235 tree I_type, I_type_ptr; 10236 int n = get_atomic_generic_size (loc, function, params); 10237 10238 /* Size of 0 is an error condition. */ 10239 if (n == 0) 10240 { 10241 *new_return = error_mark_node; 10242 return true; 10243 } 10244 10245 /* If not a lock-free size, change to the library generic format. */ 10246 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16) 10247 { 10248 *new_return = add_atomic_size_parameter (n, loc, function, params); 10249 return true; 10250 } 10251 10252 /* Otherwise there is a lockfree match, transform the call from: 10253 void fn(T* mem, T* desired, T* return, model) 10254 into 10255 *return = (T) (fn (In* mem, (In) *desired, model)) */ 10256 10257 p0 = (*params)[0]; 10258 p1 = (*params)[1]; 10259 p2 = (*params)[2]; 10260 p3 = (*params)[3]; 10261 10262 /* Create pointer to appropriate size. */ 10263 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); 10264 I_type_ptr = build_pointer_type (I_type); 10265 10266 /* Convert object pointer to required type. */ 10267 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); 10268 (*params)[0] = p0; 10269 /* Convert new value to required type, and dereference it. */ 10270 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR); 10271 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1); 10272 (*params)[1] = p1; 10273 10274 /* Move memory model to the 3rd position, and end param list. */ 10275 (*params)[2] = p3; 10276 params->truncate (3); 10277 10278 /* Convert return pointer and dereference it for later assignment. */ 10279 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR); 10280 10281 return false; 10282 } 10283 10284 10285 /* This will process an __atomic_compare_exchange function call, determine 10286 whether it needs to be mapped to the _N variation, or turned into a lib call. 10287 LOC is the location of the builtin call. 10288 FUNCTION is the DECL that has been invoked; 10289 PARAMS is the argument list for the call. The return value is non-null 10290 TRUE is returned if it is translated into the proper format for a call to the 10291 external library, and NEW_RETURN is set the tree for that function. 10292 FALSE is returned if processing for the _N variation is required. */ 10293 10294 static bool 10295 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function, 10296 vec<tree, va_gc> *params, 10297 tree *new_return) 10298 { 10299 tree p0, p1, p2; 10300 tree I_type, I_type_ptr; 10301 int n = get_atomic_generic_size (loc, function, params); 10302 10303 /* Size of 0 is an error condition. */ 10304 if (n == 0) 10305 { 10306 *new_return = error_mark_node; 10307 return true; 10308 } 10309 10310 /* If not a lock-free size, change to the library generic format. */ 10311 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16) 10312 { 10313 /* The library generic format does not have the weak parameter, so 10314 remove it from the param list. Since a parameter has been removed, 10315 we can be sure that there is room for the SIZE_T parameter, meaning 10316 there will not be a recursive rebuilding of the parameter list, so 10317 there is no danger this will be done twice. */ 10318 if (n > 0) 10319 { 10320 (*params)[3] = (*params)[4]; 10321 (*params)[4] = (*params)[5]; 10322 params->truncate (5); 10323 } 10324 *new_return = add_atomic_size_parameter (n, loc, function, params); 10325 return true; 10326 } 10327 10328 /* Otherwise, there is a match, so the call needs to be transformed from: 10329 bool fn(T* mem, T* desired, T* return, weak, success, failure) 10330 into 10331 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */ 10332 10333 p0 = (*params)[0]; 10334 p1 = (*params)[1]; 10335 p2 = (*params)[2]; 10336 10337 /* Create pointer to appropriate size. */ 10338 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); 10339 I_type_ptr = build_pointer_type (I_type); 10340 10341 /* Convert object pointer to required type. */ 10342 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); 10343 (*params)[0] = p0; 10344 10345 /* Convert expected pointer to required type. */ 10346 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1); 10347 (*params)[1] = p1; 10348 10349 /* Convert desired value to required type, and dereference it. */ 10350 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR); 10351 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2); 10352 (*params)[2] = p2; 10353 10354 /* The rest of the parameters are fine. NULL means no special return value 10355 processing.*/ 10356 *new_return = NULL; 10357 return false; 10358 } 10359 10360 10361 /* This will process an __atomic_load function call, determine whether it 10362 needs to be mapped to the _N variation, or turned into a library call. 10363 LOC is the location of the builtin call. 10364 FUNCTION is the DECL that has been invoked; 10365 PARAMS is the argument list for the call. The return value is non-null 10366 TRUE is returned if it is translated into the proper format for a call to the 10367 external library, and NEW_RETURN is set the tree for that function. 10368 FALSE is returned if processing for the _N variation is required, and 10369 NEW_RETURN is set to the the return value the result is copied into. */ 10370 10371 static bool 10372 resolve_overloaded_atomic_load (location_t loc, tree function, 10373 vec<tree, va_gc> *params, tree *new_return) 10374 { 10375 tree p0, p1, p2; 10376 tree I_type, I_type_ptr; 10377 int n = get_atomic_generic_size (loc, function, params); 10378 10379 /* Size of 0 is an error condition. */ 10380 if (n == 0) 10381 { 10382 *new_return = error_mark_node; 10383 return true; 10384 } 10385 10386 /* If not a lock-free size, change to the library generic format. */ 10387 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16) 10388 { 10389 *new_return = add_atomic_size_parameter (n, loc, function, params); 10390 return true; 10391 } 10392 10393 /* Otherwise, there is a match, so the call needs to be transformed from: 10394 void fn(T* mem, T* return, model) 10395 into 10396 *return = (T) (fn ((In *) mem, model)) */ 10397 10398 p0 = (*params)[0]; 10399 p1 = (*params)[1]; 10400 p2 = (*params)[2]; 10401 10402 /* Create pointer to appropriate size. */ 10403 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); 10404 I_type_ptr = build_pointer_type (I_type); 10405 10406 /* Convert object pointer to required type. */ 10407 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); 10408 (*params)[0] = p0; 10409 10410 /* Move memory model to the 2nd position, and end param list. */ 10411 (*params)[1] = p2; 10412 params->truncate (2); 10413 10414 /* Convert return pointer and dereference it for later assignment. */ 10415 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR); 10416 10417 return false; 10418 } 10419 10420 10421 /* This will process an __atomic_store function call, determine whether it 10422 needs to be mapped to the _N variation, or turned into a library call. 10423 LOC is the location of the builtin call. 10424 FUNCTION is the DECL that has been invoked; 10425 PARAMS is the argument list for the call. The return value is non-null 10426 TRUE is returned if it is translated into the proper format for a call to the 10427 external library, and NEW_RETURN is set the tree for that function. 10428 FALSE is returned if processing for the _N variation is required, and 10429 NEW_RETURN is set to the the return value the result is copied into. */ 10430 10431 static bool 10432 resolve_overloaded_atomic_store (location_t loc, tree function, 10433 vec<tree, va_gc> *params, tree *new_return) 10434 { 10435 tree p0, p1; 10436 tree I_type, I_type_ptr; 10437 int n = get_atomic_generic_size (loc, function, params); 10438 10439 /* Size of 0 is an error condition. */ 10440 if (n == 0) 10441 { 10442 *new_return = error_mark_node; 10443 return true; 10444 } 10445 10446 /* If not a lock-free size, change to the library generic format. */ 10447 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16) 10448 { 10449 *new_return = add_atomic_size_parameter (n, loc, function, params); 10450 return true; 10451 } 10452 10453 /* Otherwise, there is a match, so the call needs to be transformed from: 10454 void fn(T* mem, T* value, model) 10455 into 10456 fn ((In *) mem, (In) *value, model) */ 10457 10458 p0 = (*params)[0]; 10459 p1 = (*params)[1]; 10460 10461 /* Create pointer to appropriate size. */ 10462 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); 10463 I_type_ptr = build_pointer_type (I_type); 10464 10465 /* Convert object pointer to required type. */ 10466 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); 10467 (*params)[0] = p0; 10468 10469 /* Convert new value to required type, and dereference it. */ 10470 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR); 10471 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1); 10472 (*params)[1] = p1; 10473 10474 /* The memory model is in the right spot already. Return is void. */ 10475 *new_return = NULL_TREE; 10476 10477 return false; 10478 } 10479 10480 10481 /* Some builtin functions are placeholders for other expressions. This 10482 function should be called immediately after parsing the call expression 10483 before surrounding code has committed to the type of the expression. 10484 10485 LOC is the location of the builtin call. 10486 10487 FUNCTION is the DECL that has been invoked; it is known to be a builtin. 10488 PARAMS is the argument list for the call. The return value is non-null 10489 when expansion is complete, and null if normal processing should 10490 continue. */ 10491 10492 tree 10493 resolve_overloaded_builtin (location_t loc, tree function, 10494 vec<tree, va_gc> *params) 10495 { 10496 enum built_in_function orig_code = DECL_FUNCTION_CODE (function); 10497 bool orig_format = true; 10498 tree new_return = NULL_TREE; 10499 10500 switch (DECL_BUILT_IN_CLASS (function)) 10501 { 10502 case BUILT_IN_NORMAL: 10503 break; 10504 case BUILT_IN_MD: 10505 if (targetm.resolve_overloaded_builtin) 10506 return targetm.resolve_overloaded_builtin (loc, function, params); 10507 else 10508 return NULL_TREE; 10509 default: 10510 return NULL_TREE; 10511 } 10512 10513 /* Handle BUILT_IN_NORMAL here. */ 10514 switch (orig_code) 10515 { 10516 case BUILT_IN_ATOMIC_EXCHANGE: 10517 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: 10518 case BUILT_IN_ATOMIC_LOAD: 10519 case BUILT_IN_ATOMIC_STORE: 10520 { 10521 /* Handle these 4 together so that they can fall through to the next 10522 case if the call is transformed to an _N variant. */ 10523 switch (orig_code) 10524 { 10525 case BUILT_IN_ATOMIC_EXCHANGE: 10526 { 10527 if (resolve_overloaded_atomic_exchange (loc, function, params, 10528 &new_return)) 10529 return new_return; 10530 /* Change to the _N variant. */ 10531 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N; 10532 break; 10533 } 10534 10535 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: 10536 { 10537 if (resolve_overloaded_atomic_compare_exchange (loc, function, 10538 params, 10539 &new_return)) 10540 return new_return; 10541 /* Change to the _N variant. */ 10542 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N; 10543 break; 10544 } 10545 case BUILT_IN_ATOMIC_LOAD: 10546 { 10547 if (resolve_overloaded_atomic_load (loc, function, params, 10548 &new_return)) 10549 return new_return; 10550 /* Change to the _N variant. */ 10551 orig_code = BUILT_IN_ATOMIC_LOAD_N; 10552 break; 10553 } 10554 case BUILT_IN_ATOMIC_STORE: 10555 { 10556 if (resolve_overloaded_atomic_store (loc, function, params, 10557 &new_return)) 10558 return new_return; 10559 /* Change to the _N variant. */ 10560 orig_code = BUILT_IN_ATOMIC_STORE_N; 10561 break; 10562 } 10563 default: 10564 gcc_unreachable (); 10565 } 10566 /* Fallthrough to the normal processing. */ 10567 } 10568 case BUILT_IN_ATOMIC_EXCHANGE_N: 10569 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N: 10570 case BUILT_IN_ATOMIC_LOAD_N: 10571 case BUILT_IN_ATOMIC_STORE_N: 10572 case BUILT_IN_ATOMIC_ADD_FETCH_N: 10573 case BUILT_IN_ATOMIC_SUB_FETCH_N: 10574 case BUILT_IN_ATOMIC_AND_FETCH_N: 10575 case BUILT_IN_ATOMIC_NAND_FETCH_N: 10576 case BUILT_IN_ATOMIC_XOR_FETCH_N: 10577 case BUILT_IN_ATOMIC_OR_FETCH_N: 10578 case BUILT_IN_ATOMIC_FETCH_ADD_N: 10579 case BUILT_IN_ATOMIC_FETCH_SUB_N: 10580 case BUILT_IN_ATOMIC_FETCH_AND_N: 10581 case BUILT_IN_ATOMIC_FETCH_NAND_N: 10582 case BUILT_IN_ATOMIC_FETCH_XOR_N: 10583 case BUILT_IN_ATOMIC_FETCH_OR_N: 10584 { 10585 orig_format = false; 10586 /* Fallthru for parameter processing. */ 10587 } 10588 case BUILT_IN_SYNC_FETCH_AND_ADD_N: 10589 case BUILT_IN_SYNC_FETCH_AND_SUB_N: 10590 case BUILT_IN_SYNC_FETCH_AND_OR_N: 10591 case BUILT_IN_SYNC_FETCH_AND_AND_N: 10592 case BUILT_IN_SYNC_FETCH_AND_XOR_N: 10593 case BUILT_IN_SYNC_FETCH_AND_NAND_N: 10594 case BUILT_IN_SYNC_ADD_AND_FETCH_N: 10595 case BUILT_IN_SYNC_SUB_AND_FETCH_N: 10596 case BUILT_IN_SYNC_OR_AND_FETCH_N: 10597 case BUILT_IN_SYNC_AND_AND_FETCH_N: 10598 case BUILT_IN_SYNC_XOR_AND_FETCH_N: 10599 case BUILT_IN_SYNC_NAND_AND_FETCH_N: 10600 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N: 10601 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N: 10602 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N: 10603 case BUILT_IN_SYNC_LOCK_RELEASE_N: 10604 { 10605 int n = sync_resolve_size (function, params); 10606 tree new_function, first_param, result; 10607 enum built_in_function fncode; 10608 10609 if (n == 0) 10610 return error_mark_node; 10611 10612 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1); 10613 new_function = builtin_decl_explicit (fncode); 10614 if (!sync_resolve_params (loc, function, new_function, params, 10615 orig_format)) 10616 return error_mark_node; 10617 10618 first_param = (*params)[0]; 10619 result = build_function_call_vec (loc, new_function, params, NULL); 10620 if (result == error_mark_node) 10621 return result; 10622 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N 10623 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N 10624 && orig_code != BUILT_IN_ATOMIC_STORE_N) 10625 result = sync_resolve_return (first_param, result, orig_format); 10626 10627 /* If new_return is set, assign function to that expr and cast the 10628 result to void since the generic interface returned void. */ 10629 if (new_return) 10630 { 10631 /* Cast function result from I{1,2,4,8,16} to the required type. */ 10632 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result); 10633 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return, 10634 result); 10635 TREE_SIDE_EFFECTS (result) = 1; 10636 protected_set_expr_location (result, loc); 10637 result = convert (void_type_node, result); 10638 } 10639 return result; 10640 } 10641 10642 default: 10643 return NULL_TREE; 10644 } 10645 } 10646 10647 /* Ignoring their sign, return true if two scalar types are the same. */ 10648 bool 10649 same_scalar_type_ignoring_signedness (tree t1, tree t2) 10650 { 10651 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2); 10652 10653 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE) 10654 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE 10655 || c2 == FIXED_POINT_TYPE)); 10656 10657 /* Equality works here because c_common_signed_type uses 10658 TYPE_MAIN_VARIANT. */ 10659 return c_common_signed_type (t1) 10660 == c_common_signed_type (t2); 10661 } 10662 10663 /* Check for missing format attributes on function pointers. LTYPE is 10664 the new type or left-hand side type. RTYPE is the old type or 10665 right-hand side type. Returns TRUE if LTYPE is missing the desired 10666 attribute. */ 10667 10668 bool 10669 check_missing_format_attribute (tree ltype, tree rtype) 10670 { 10671 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype); 10672 tree ra; 10673 10674 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra)) 10675 if (is_attribute_p ("format", TREE_PURPOSE (ra))) 10676 break; 10677 if (ra) 10678 { 10679 tree la; 10680 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la)) 10681 if (is_attribute_p ("format", TREE_PURPOSE (la))) 10682 break; 10683 return !la; 10684 } 10685 else 10686 return false; 10687 } 10688 10689 /* Subscripting with type char is likely to lose on a machine where 10690 chars are signed. So warn on any machine, but optionally. Don't 10691 warn for unsigned char since that type is safe. Don't warn for 10692 signed char because anyone who uses that must have done so 10693 deliberately. Furthermore, we reduce the false positive load by 10694 warning only for non-constant value of type char. */ 10695 10696 void 10697 warn_array_subscript_with_type_char (tree index) 10698 { 10699 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node 10700 && TREE_CODE (index) != INTEGER_CST) 10701 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>"); 10702 } 10703 10704 /* Implement -Wparentheses for the unexpected C precedence rules, to 10705 cover cases like x + y << z which readers are likely to 10706 misinterpret. We have seen an expression in which CODE is a binary 10707 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which 10708 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and 10709 CODE_RIGHT may be ERROR_MARK, which means that that side of the 10710 expression was not formed using a binary or unary operator, or it 10711 was enclosed in parentheses. */ 10712 10713 void 10714 warn_about_parentheses (location_t loc, enum tree_code code, 10715 enum tree_code code_left, tree arg_left, 10716 enum tree_code code_right, tree arg_right) 10717 { 10718 if (!warn_parentheses) 10719 return; 10720 10721 /* This macro tests that the expression ARG with original tree code 10722 CODE appears to be a boolean expression. or the result of folding a 10723 boolean expression. */ 10724 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \ 10725 (truth_value_p (TREE_CODE (ARG)) \ 10726 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \ 10727 /* Folding may create 0 or 1 integers from other expressions. */ \ 10728 || ((CODE) != INTEGER_CST \ 10729 && (integer_onep (ARG) || integer_zerop (ARG)))) 10730 10731 switch (code) 10732 { 10733 case LSHIFT_EXPR: 10734 if (code_left == PLUS_EXPR) 10735 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10736 "suggest parentheses around %<+%> inside %<<<%>"); 10737 else if (code_right == PLUS_EXPR) 10738 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10739 "suggest parentheses around %<+%> inside %<<<%>"); 10740 else if (code_left == MINUS_EXPR) 10741 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10742 "suggest parentheses around %<-%> inside %<<<%>"); 10743 else if (code_right == MINUS_EXPR) 10744 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10745 "suggest parentheses around %<-%> inside %<<<%>"); 10746 return; 10747 10748 case RSHIFT_EXPR: 10749 if (code_left == PLUS_EXPR) 10750 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10751 "suggest parentheses around %<+%> inside %<>>%>"); 10752 else if (code_right == PLUS_EXPR) 10753 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10754 "suggest parentheses around %<+%> inside %<>>%>"); 10755 else if (code_left == MINUS_EXPR) 10756 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10757 "suggest parentheses around %<-%> inside %<>>%>"); 10758 else if (code_right == MINUS_EXPR) 10759 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10760 "suggest parentheses around %<-%> inside %<>>%>"); 10761 return; 10762 10763 case TRUTH_ORIF_EXPR: 10764 if (code_left == TRUTH_ANDIF_EXPR) 10765 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10766 "suggest parentheses around %<&&%> within %<||%>"); 10767 else if (code_right == TRUTH_ANDIF_EXPR) 10768 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10769 "suggest parentheses around %<&&%> within %<||%>"); 10770 return; 10771 10772 case BIT_IOR_EXPR: 10773 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR 10774 || code_left == PLUS_EXPR || code_left == MINUS_EXPR) 10775 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10776 "suggest parentheses around arithmetic in operand of %<|%>"); 10777 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR 10778 || code_right == PLUS_EXPR || code_right == MINUS_EXPR) 10779 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10780 "suggest parentheses around arithmetic in operand of %<|%>"); 10781 /* Check cases like x|y==z */ 10782 else if (TREE_CODE_CLASS (code_left) == tcc_comparison) 10783 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10784 "suggest parentheses around comparison in operand of %<|%>"); 10785 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 10786 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10787 "suggest parentheses around comparison in operand of %<|%>"); 10788 /* Check cases like !x | y */ 10789 else if (code_left == TRUTH_NOT_EXPR 10790 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) 10791 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10792 "suggest parentheses around operand of " 10793 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>"); 10794 return; 10795 10796 case BIT_XOR_EXPR: 10797 if (code_left == BIT_AND_EXPR 10798 || code_left == PLUS_EXPR || code_left == MINUS_EXPR) 10799 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10800 "suggest parentheses around arithmetic in operand of %<^%>"); 10801 else if (code_right == BIT_AND_EXPR 10802 || code_right == PLUS_EXPR || code_right == MINUS_EXPR) 10803 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10804 "suggest parentheses around arithmetic in operand of %<^%>"); 10805 /* Check cases like x^y==z */ 10806 else if (TREE_CODE_CLASS (code_left) == tcc_comparison) 10807 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10808 "suggest parentheses around comparison in operand of %<^%>"); 10809 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 10810 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10811 "suggest parentheses around comparison in operand of %<^%>"); 10812 return; 10813 10814 case BIT_AND_EXPR: 10815 if (code_left == PLUS_EXPR) 10816 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10817 "suggest parentheses around %<+%> in operand of %<&%>"); 10818 else if (code_right == PLUS_EXPR) 10819 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10820 "suggest parentheses around %<+%> in operand of %<&%>"); 10821 else if (code_left == MINUS_EXPR) 10822 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10823 "suggest parentheses around %<-%> in operand of %<&%>"); 10824 else if (code_right == MINUS_EXPR) 10825 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10826 "suggest parentheses around %<-%> in operand of %<&%>"); 10827 /* Check cases like x&y==z */ 10828 else if (TREE_CODE_CLASS (code_left) == tcc_comparison) 10829 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10830 "suggest parentheses around comparison in operand of %<&%>"); 10831 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 10832 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10833 "suggest parentheses around comparison in operand of %<&%>"); 10834 /* Check cases like !x & y */ 10835 else if (code_left == TRUTH_NOT_EXPR 10836 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) 10837 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10838 "suggest parentheses around operand of " 10839 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>"); 10840 return; 10841 10842 case EQ_EXPR: 10843 if (TREE_CODE_CLASS (code_left) == tcc_comparison) 10844 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10845 "suggest parentheses around comparison in operand of %<==%>"); 10846 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 10847 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10848 "suggest parentheses around comparison in operand of %<==%>"); 10849 return; 10850 case NE_EXPR: 10851 if (TREE_CODE_CLASS (code_left) == tcc_comparison) 10852 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10853 "suggest parentheses around comparison in operand of %<!=%>"); 10854 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 10855 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10856 "suggest parentheses around comparison in operand of %<!=%>"); 10857 return; 10858 10859 default: 10860 if (TREE_CODE_CLASS (code) == tcc_comparison) 10861 { 10862 if (TREE_CODE_CLASS (code_left) == tcc_comparison 10863 && code_left != NE_EXPR && code_left != EQ_EXPR 10864 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left))) 10865 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 10866 "comparisons like %<X<=Y<=Z%> do not " 10867 "have their mathematical meaning"); 10868 else if (TREE_CODE_CLASS (code_right) == tcc_comparison 10869 && code_right != NE_EXPR && code_right != EQ_EXPR 10870 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right))) 10871 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 10872 "comparisons like %<X<=Y<=Z%> do not " 10873 "have their mathematical meaning"); 10874 } 10875 return; 10876 } 10877 #undef NOT_A_BOOLEAN_EXPR_P 10878 } 10879 10880 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */ 10881 10882 void 10883 warn_for_unused_label (tree label) 10884 { 10885 if (!TREE_USED (label)) 10886 { 10887 if (DECL_INITIAL (label)) 10888 warning (OPT_Wunused_label, "label %q+D defined but not used", label); 10889 else 10890 warning (OPT_Wunused_label, "label %q+D declared but not defined", label); 10891 } 10892 } 10893 10894 /* Warn for division by zero according to the value of DIVISOR. LOC 10895 is the location of the division operator. */ 10896 10897 void 10898 warn_for_div_by_zero (location_t loc, tree divisor) 10899 { 10900 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning 10901 about division by zero. Do not issue a warning if DIVISOR has a 10902 floating-point type, since we consider 0.0/0.0 a valid way of 10903 generating a NaN. */ 10904 if (c_inhibit_evaluation_warnings == 0 10905 && (integer_zerop (divisor) || fixed_zerop (divisor))) 10906 warning_at (loc, OPT_Wdiv_by_zero, "division by zero"); 10907 } 10908 10909 /* Subroutine of build_binary_op. Give warnings for comparisons 10910 between signed and unsigned quantities that may fail. Do the 10911 checking based on the original operand trees ORIG_OP0 and ORIG_OP1, 10912 so that casts will be considered, but default promotions won't 10913 be. 10914 10915 LOCATION is the location of the comparison operator. 10916 10917 The arguments of this function map directly to local variables 10918 of build_binary_op. */ 10919 10920 void 10921 warn_for_sign_compare (location_t location, 10922 tree orig_op0, tree orig_op1, 10923 tree op0, tree op1, 10924 tree result_type, enum tree_code resultcode) 10925 { 10926 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0)); 10927 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1)); 10928 int unsignedp0, unsignedp1; 10929 10930 /* In C++, check for comparison of different enum types. */ 10931 if (c_dialect_cxx() 10932 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE 10933 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE 10934 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0)) 10935 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1))) 10936 { 10937 warning_at (location, 10938 OPT_Wsign_compare, "comparison between types %qT and %qT", 10939 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1)); 10940 } 10941 10942 /* Do not warn if the comparison is being done in a signed type, 10943 since the signed type will only be chosen if it can represent 10944 all the values of the unsigned type. */ 10945 if (!TYPE_UNSIGNED (result_type)) 10946 /* OK */; 10947 /* Do not warn if both operands are unsigned. */ 10948 else if (op0_signed == op1_signed) 10949 /* OK */; 10950 else 10951 { 10952 tree sop, uop, base_type; 10953 bool ovf; 10954 10955 if (op0_signed) 10956 sop = orig_op0, uop = orig_op1; 10957 else 10958 sop = orig_op1, uop = orig_op0; 10959 10960 STRIP_TYPE_NOPS (sop); 10961 STRIP_TYPE_NOPS (uop); 10962 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE 10963 ? TREE_TYPE (result_type) : result_type); 10964 10965 /* Do not warn if the signed quantity is an unsuffixed integer 10966 literal (or some static constant expression involving such 10967 literals or a conditional expression involving such literals) 10968 and it is non-negative. */ 10969 if (tree_expr_nonnegative_warnv_p (sop, &ovf)) 10970 /* OK */; 10971 /* Do not warn if the comparison is an equality operation, the 10972 unsigned quantity is an integral constant, and it would fit 10973 in the result if the result were signed. */ 10974 else if (TREE_CODE (uop) == INTEGER_CST 10975 && (resultcode == EQ_EXPR || resultcode == NE_EXPR) 10976 && int_fits_type_p (uop, c_common_signed_type (base_type))) 10977 /* OK */; 10978 /* In C, do not warn if the unsigned quantity is an enumeration 10979 constant and its maximum value would fit in the result if the 10980 result were signed. */ 10981 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST 10982 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE 10983 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)), 10984 c_common_signed_type (base_type))) 10985 /* OK */; 10986 else 10987 warning_at (location, 10988 OPT_Wsign_compare, 10989 "comparison between signed and unsigned integer expressions"); 10990 } 10991 10992 /* Warn if two unsigned values are being compared in a size larger 10993 than their original size, and one (and only one) is the result of 10994 a `~' operator. This comparison will always fail. 10995 10996 Also warn if one operand is a constant, and the constant does not 10997 have all bits set that are set in the ~ operand when it is 10998 extended. */ 10999 11000 op0 = c_common_get_narrower (op0, &unsignedp0); 11001 op1 = c_common_get_narrower (op1, &unsignedp1); 11002 11003 if ((TREE_CODE (op0) == BIT_NOT_EXPR) 11004 ^ (TREE_CODE (op1) == BIT_NOT_EXPR)) 11005 { 11006 if (TREE_CODE (op0) == BIT_NOT_EXPR) 11007 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0); 11008 if (TREE_CODE (op1) == BIT_NOT_EXPR) 11009 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1); 11010 11011 if (host_integerp (op0, 0) || host_integerp (op1, 0)) 11012 { 11013 tree primop; 11014 HOST_WIDE_INT constant, mask; 11015 int unsignedp; 11016 unsigned int bits; 11017 11018 if (host_integerp (op0, 0)) 11019 { 11020 primop = op1; 11021 unsignedp = unsignedp1; 11022 constant = tree_low_cst (op0, 0); 11023 } 11024 else 11025 { 11026 primop = op0; 11027 unsignedp = unsignedp0; 11028 constant = tree_low_cst (op1, 0); 11029 } 11030 11031 bits = TYPE_PRECISION (TREE_TYPE (primop)); 11032 if (bits < TYPE_PRECISION (result_type) 11033 && bits < HOST_BITS_PER_LONG && unsignedp) 11034 { 11035 mask = (~ (HOST_WIDE_INT) 0) << bits; 11036 if ((mask & constant) != mask) 11037 { 11038 if (constant == 0) 11039 warning (OPT_Wsign_compare, 11040 "promoted ~unsigned is always non-zero"); 11041 else 11042 warning_at (location, OPT_Wsign_compare, 11043 "comparison of promoted ~unsigned with constant"); 11044 } 11045 } 11046 } 11047 else if (unsignedp0 && unsignedp1 11048 && (TYPE_PRECISION (TREE_TYPE (op0)) 11049 < TYPE_PRECISION (result_type)) 11050 && (TYPE_PRECISION (TREE_TYPE (op1)) 11051 < TYPE_PRECISION (result_type))) 11052 warning_at (location, OPT_Wsign_compare, 11053 "comparison of promoted ~unsigned with unsigned"); 11054 } 11055 } 11056 11057 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common 11058 type via c_common_type. If -Wdouble-promotion is in use, and the 11059 conditions for warning have been met, issue a warning. GMSGID is 11060 the warning message. It must have two %T specifiers for the type 11061 that was converted (generally "float") and the type to which it was 11062 converted (generally "double), respectively. LOC is the location 11063 to which the awrning should refer. */ 11064 11065 void 11066 do_warn_double_promotion (tree result_type, tree type1, tree type2, 11067 const char *gmsgid, location_t loc) 11068 { 11069 tree source_type; 11070 11071 if (!warn_double_promotion) 11072 return; 11073 /* If the conversion will not occur at run-time, there is no need to 11074 warn about it. */ 11075 if (c_inhibit_evaluation_warnings) 11076 return; 11077 if (TYPE_MAIN_VARIANT (result_type) != double_type_node 11078 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node) 11079 return; 11080 if (TYPE_MAIN_VARIANT (type1) == float_type_node 11081 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node) 11082 source_type = type1; 11083 else if (TYPE_MAIN_VARIANT (type2) == float_type_node 11084 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node) 11085 source_type = type2; 11086 else 11087 return; 11088 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type); 11089 } 11090 11091 /* Setup a TYPE_DECL node as a typedef representation. 11092 11093 X is a TYPE_DECL for a typedef statement. Create a brand new 11094 ..._TYPE node (which will be just a variant of the existing 11095 ..._TYPE node with identical properties) and then install X 11096 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. 11097 11098 The whole point here is to end up with a situation where each 11099 and every ..._TYPE node the compiler creates will be uniquely 11100 associated with AT MOST one node representing a typedef name. 11101 This way, even though the compiler substitutes corresponding 11102 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very 11103 early on, later parts of the compiler can always do the reverse 11104 translation and get back the corresponding typedef name. For 11105 example, given: 11106 11107 typedef struct S MY_TYPE; 11108 MY_TYPE object; 11109 11110 Later parts of the compiler might only know that `object' was of 11111 type `struct S' if it were not for code just below. With this 11112 code however, later parts of the compiler see something like: 11113 11114 struct S' == struct S 11115 typedef struct S' MY_TYPE; 11116 struct S' object; 11117 11118 And they can then deduce (from the node for type struct S') that 11119 the original object declaration was: 11120 11121 MY_TYPE object; 11122 11123 Being able to do this is important for proper support of protoize, 11124 and also for generating precise symbolic debugging information 11125 which takes full account of the programmer's (typedef) vocabulary. 11126 11127 Obviously, we don't want to generate a duplicate ..._TYPE node if 11128 the TYPE_DECL node that we are now processing really represents a 11129 standard built-in type. */ 11130 11131 void 11132 set_underlying_type (tree x) 11133 { 11134 if (x == error_mark_node) 11135 return; 11136 if (DECL_IS_BUILTIN (x)) 11137 { 11138 if (TYPE_NAME (TREE_TYPE (x)) == 0) 11139 TYPE_NAME (TREE_TYPE (x)) = x; 11140 } 11141 else if (TREE_TYPE (x) != error_mark_node 11142 && DECL_ORIGINAL_TYPE (x) == NULL_TREE) 11143 { 11144 tree tt = TREE_TYPE (x); 11145 DECL_ORIGINAL_TYPE (x) = tt; 11146 tt = build_variant_type_copy (tt); 11147 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x)); 11148 TYPE_NAME (tt) = x; 11149 TREE_USED (tt) = TREE_USED (x); 11150 TREE_TYPE (x) = tt; 11151 } 11152 } 11153 11154 /* Record the types used by the current global variable declaration 11155 being parsed, so that we can decide later to emit their debug info. 11156 Those types are in types_used_by_cur_var_decl, and we are going to 11157 store them in the types_used_by_vars_hash hash table. 11158 DECL is the declaration of the global variable that has been parsed. */ 11159 11160 void 11161 record_types_used_by_current_var_decl (tree decl) 11162 { 11163 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl)); 11164 11165 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ()) 11166 { 11167 tree type = types_used_by_cur_var_decl->pop (); 11168 types_used_by_var_decl_insert (type, decl); 11169 } 11170 } 11171 11172 /* If DECL is a typedef that is declared in the current function, 11173 record it for the purpose of -Wunused-local-typedefs. */ 11174 11175 void 11176 record_locally_defined_typedef (tree decl) 11177 { 11178 struct c_language_function *l; 11179 11180 if (!warn_unused_local_typedefs 11181 || cfun == NULL 11182 /* if this is not a locally defined typedef then we are not 11183 interested. */ 11184 || !is_typedef_decl (decl) 11185 || !decl_function_context (decl)) 11186 return; 11187 11188 l = (struct c_language_function *) cfun->language; 11189 vec_safe_push (l->local_typedefs, decl); 11190 } 11191 11192 /* If T is a TYPE_DECL declared locally, mark it as used. */ 11193 11194 void 11195 maybe_record_typedef_use (tree t) 11196 { 11197 if (!is_typedef_decl (t)) 11198 return; 11199 11200 TREE_USED (t) = true; 11201 } 11202 11203 /* Warn if there are some unused locally defined typedefs in the 11204 current function. */ 11205 11206 void 11207 maybe_warn_unused_local_typedefs (void) 11208 { 11209 int i; 11210 tree decl; 11211 /* The number of times we have emitted -Wunused-local-typedefs 11212 warnings. If this is different from errorcount, that means some 11213 unrelated errors have been issued. In which case, we'll avoid 11214 emitting "unused-local-typedefs" warnings. */ 11215 static int unused_local_typedefs_warn_count; 11216 struct c_language_function *l; 11217 11218 if (cfun == NULL) 11219 return; 11220 11221 if ((l = (struct c_language_function *) cfun->language) == NULL) 11222 return; 11223 11224 if (warn_unused_local_typedefs 11225 && errorcount == unused_local_typedefs_warn_count) 11226 { 11227 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl) 11228 if (!TREE_USED (decl)) 11229 warning_at (DECL_SOURCE_LOCATION (decl), 11230 OPT_Wunused_local_typedefs, 11231 "typedef %qD locally defined but not used", decl); 11232 unused_local_typedefs_warn_count = errorcount; 11233 } 11234 11235 vec_free (l->local_typedefs); 11236 } 11237 11238 /* The C and C++ parsers both use vectors to hold function arguments. 11239 For efficiency, we keep a cache of unused vectors. This is the 11240 cache. */ 11241 11242 typedef vec<tree, va_gc> *tree_gc_vec; 11243 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache; 11244 11245 /* Return a new vector from the cache. If the cache is empty, 11246 allocate a new vector. These vectors are GC'ed, so it is OK if the 11247 pointer is not released.. */ 11248 11249 vec<tree, va_gc> * 11250 make_tree_vector (void) 11251 { 11252 if (tree_vector_cache && !tree_vector_cache->is_empty ()) 11253 return tree_vector_cache->pop (); 11254 else 11255 { 11256 /* Passing 0 to vec::alloc returns NULL, and our callers require 11257 that we always return a non-NULL value. The vector code uses 11258 4 when growing a NULL vector, so we do too. */ 11259 vec<tree, va_gc> *v; 11260 vec_alloc (v, 4); 11261 return v; 11262 } 11263 } 11264 11265 /* Release a vector of trees back to the cache. */ 11266 11267 void 11268 release_tree_vector (vec<tree, va_gc> *vec) 11269 { 11270 if (vec != NULL) 11271 { 11272 vec->truncate (0); 11273 vec_safe_push (tree_vector_cache, vec); 11274 } 11275 } 11276 11277 /* Get a new tree vector holding a single tree. */ 11278 11279 vec<tree, va_gc> * 11280 make_tree_vector_single (tree t) 11281 { 11282 vec<tree, va_gc> *ret = make_tree_vector (); 11283 ret->quick_push (t); 11284 return ret; 11285 } 11286 11287 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */ 11288 11289 vec<tree, va_gc> * 11290 make_tree_vector_from_list (tree list) 11291 { 11292 vec<tree, va_gc> *ret = make_tree_vector (); 11293 for (; list; list = TREE_CHAIN (list)) 11294 vec_safe_push (ret, TREE_VALUE (list)); 11295 return ret; 11296 } 11297 11298 /* Get a new tree vector which is a copy of an existing one. */ 11299 11300 vec<tree, va_gc> * 11301 make_tree_vector_copy (const vec<tree, va_gc> *orig) 11302 { 11303 vec<tree, va_gc> *ret; 11304 unsigned int ix; 11305 tree t; 11306 11307 ret = make_tree_vector (); 11308 vec_safe_reserve (ret, vec_safe_length (orig)); 11309 FOR_EACH_VEC_SAFE_ELT (orig, ix, t) 11310 ret->quick_push (t); 11311 return ret; 11312 } 11313 11314 /* Return true if KEYWORD starts a type specifier. */ 11315 11316 bool 11317 keyword_begins_type_specifier (enum rid keyword) 11318 { 11319 switch (keyword) 11320 { 11321 case RID_INT: 11322 case RID_CHAR: 11323 case RID_FLOAT: 11324 case RID_DOUBLE: 11325 case RID_VOID: 11326 case RID_INT128: 11327 case RID_UNSIGNED: 11328 case RID_LONG: 11329 case RID_SHORT: 11330 case RID_SIGNED: 11331 case RID_DFLOAT32: 11332 case RID_DFLOAT64: 11333 case RID_DFLOAT128: 11334 case RID_FRACT: 11335 case RID_ACCUM: 11336 case RID_BOOL: 11337 case RID_WCHAR: 11338 case RID_CHAR16: 11339 case RID_CHAR32: 11340 case RID_SAT: 11341 case RID_COMPLEX: 11342 case RID_TYPEOF: 11343 case RID_STRUCT: 11344 case RID_CLASS: 11345 case RID_UNION: 11346 case RID_ENUM: 11347 return true; 11348 default: 11349 return false; 11350 } 11351 } 11352 11353 /* Return true if KEYWORD names a type qualifier. */ 11354 11355 bool 11356 keyword_is_type_qualifier (enum rid keyword) 11357 { 11358 switch (keyword) 11359 { 11360 case RID_CONST: 11361 case RID_VOLATILE: 11362 case RID_RESTRICT: 11363 return true; 11364 default: 11365 return false; 11366 } 11367 } 11368 11369 /* Return true if KEYWORD names a storage class specifier. 11370 11371 RID_TYPEDEF is not included in this list despite `typedef' being 11372 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as 11373 such for syntactic convenience only. */ 11374 11375 bool 11376 keyword_is_storage_class_specifier (enum rid keyword) 11377 { 11378 switch (keyword) 11379 { 11380 case RID_STATIC: 11381 case RID_EXTERN: 11382 case RID_REGISTER: 11383 case RID_AUTO: 11384 case RID_MUTABLE: 11385 case RID_THREAD: 11386 return true; 11387 default: 11388 return false; 11389 } 11390 } 11391 11392 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */ 11393 11394 static bool 11395 keyword_is_function_specifier (enum rid keyword) 11396 { 11397 switch (keyword) 11398 { 11399 case RID_INLINE: 11400 case RID_NORETURN: 11401 case RID_VIRTUAL: 11402 case RID_EXPLICIT: 11403 return true; 11404 default: 11405 return false; 11406 } 11407 } 11408 11409 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a 11410 declaration-specifier (C99 6.7). */ 11411 11412 bool 11413 keyword_is_decl_specifier (enum rid keyword) 11414 { 11415 if (keyword_is_storage_class_specifier (keyword) 11416 || keyword_is_type_qualifier (keyword) 11417 || keyword_is_function_specifier (keyword)) 11418 return true; 11419 11420 switch (keyword) 11421 { 11422 case RID_TYPEDEF: 11423 case RID_FRIEND: 11424 case RID_CONSTEXPR: 11425 return true; 11426 default: 11427 return false; 11428 } 11429 } 11430 11431 /* Initialize language-specific-bits of tree_contains_struct. */ 11432 11433 void 11434 c_common_init_ts (void) 11435 { 11436 MARK_TS_TYPED (C_MAYBE_CONST_EXPR); 11437 MARK_TS_TYPED (EXCESS_PRECISION_EXPR); 11438 } 11439 11440 /* Build a user-defined numeric literal out of an integer constant type VALUE 11441 with identifier SUFFIX. */ 11442 11443 tree 11444 build_userdef_literal (tree suffix_id, tree value, 11445 enum overflow_type overflow, tree num_string) 11446 { 11447 tree literal = make_node (USERDEF_LITERAL); 11448 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id; 11449 USERDEF_LITERAL_VALUE (literal) = value; 11450 USERDEF_LITERAL_OVERFLOW (literal) = overflow; 11451 USERDEF_LITERAL_NUM_STRING (literal) = num_string; 11452 return literal; 11453 } 11454 11455 /* For vector[index], convert the vector to a 11456 pointer of the underlying type. */ 11457 void 11458 convert_vector_to_pointer_for_subscript (location_t loc, 11459 tree* vecp, tree index) 11460 { 11461 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE) 11462 { 11463 tree type = TREE_TYPE (*vecp); 11464 tree type1; 11465 11466 if (TREE_CODE (index) == INTEGER_CST) 11467 if (!host_integerp (index, 1) 11468 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1) 11469 >= TYPE_VECTOR_SUBPARTS (type))) 11470 warning_at (loc, OPT_Warray_bounds, "index value is out of bound"); 11471 11472 c_common_mark_addressable_vec (*vecp); 11473 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type)); 11474 type = build_pointer_type (type); 11475 type1 = build_pointer_type (TREE_TYPE (*vecp)); 11476 *vecp = build1 (ADDR_EXPR, type1, *vecp); 11477 *vecp = convert (type, *vecp); 11478 } 11479 } 11480 11481 /* Determine which of the operands, if any, is a scalar that needs to be 11482 converted to a vector, for the range of operations. */ 11483 enum stv_conv 11484 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1, 11485 bool complain) 11486 { 11487 tree type0 = TREE_TYPE (op0); 11488 tree type1 = TREE_TYPE (op1); 11489 bool integer_only_op = false; 11490 enum stv_conv ret = stv_firstarg; 11491 11492 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE 11493 || TREE_CODE (type1) == VECTOR_TYPE); 11494 switch (code) 11495 { 11496 /* Most GENERIC binary expressions require homogeneous arguments. 11497 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first 11498 argument that is a vector and a second one that is a scalar, so 11499 we never return stv_secondarg for them. */ 11500 case RSHIFT_EXPR: 11501 case LSHIFT_EXPR: 11502 if (TREE_CODE (type0) == INTEGER_TYPE 11503 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) 11504 { 11505 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false)) 11506 { 11507 if (complain) 11508 error_at (loc, "conversion of scalar %qT to vector %qT " 11509 "involves truncation", type0, type1); 11510 return stv_error; 11511 } 11512 else 11513 return stv_firstarg; 11514 } 11515 break; 11516 11517 case BIT_IOR_EXPR: 11518 case BIT_XOR_EXPR: 11519 case BIT_AND_EXPR: 11520 integer_only_op = true; 11521 /* ... fall through ... */ 11522 11523 case VEC_COND_EXPR: 11524 11525 case PLUS_EXPR: 11526 case MINUS_EXPR: 11527 case MULT_EXPR: 11528 case TRUNC_DIV_EXPR: 11529 case CEIL_DIV_EXPR: 11530 case FLOOR_DIV_EXPR: 11531 case ROUND_DIV_EXPR: 11532 case EXACT_DIV_EXPR: 11533 case TRUNC_MOD_EXPR: 11534 case FLOOR_MOD_EXPR: 11535 case RDIV_EXPR: 11536 case EQ_EXPR: 11537 case NE_EXPR: 11538 case LE_EXPR: 11539 case GE_EXPR: 11540 case LT_EXPR: 11541 case GT_EXPR: 11542 /* What about UNLT_EXPR? */ 11543 if (TREE_CODE (type0) == VECTOR_TYPE) 11544 { 11545 tree tmp; 11546 ret = stv_secondarg; 11547 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */ 11548 tmp = type0; type0 = type1; type1 = tmp; 11549 tmp = op0; op0 = op1; op1 = tmp; 11550 } 11551 11552 if (TREE_CODE (type0) == INTEGER_TYPE 11553 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) 11554 { 11555 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false)) 11556 { 11557 if (complain) 11558 error_at (loc, "conversion of scalar %qT to vector %qT " 11559 "involves truncation", type0, type1); 11560 return stv_error; 11561 } 11562 return ret; 11563 } 11564 else if (!integer_only_op 11565 /* Allow integer --> real conversion if safe. */ 11566 && (TREE_CODE (type0) == REAL_TYPE 11567 || TREE_CODE (type0) == INTEGER_TYPE) 11568 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1))) 11569 { 11570 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false)) 11571 { 11572 if (complain) 11573 error_at (loc, "conversion of scalar %qT to vector %qT " 11574 "involves truncation", type0, type1); 11575 return stv_error; 11576 } 11577 return ret; 11578 } 11579 default: 11580 break; 11581 } 11582 11583 return stv_nothing; 11584 } 11585 11586 /* Return true iff ALIGN is an integral constant that is a fundamental 11587 alignment, as defined by [basic.align] in the c++-11 11588 specifications. 11589 11590 That is: 11591 11592 [A fundamental alignment is represented by an alignment less than or 11593 equal to the greatest alignment supported by the implementation 11594 in all contexts, which is equal to 11595 alignof(max_align_t)]. */ 11596 11597 bool 11598 cxx_fundamental_alignment_p (unsigned align) 11599 { 11600 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node), 11601 TYPE_ALIGN (long_double_type_node))); 11602 } 11603 11604 #include "gt-c-family-c-common.h" 11605