1/* Subroutines shared by all languages that are variants of C. 2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 3 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc. 4 5This file is part of GCC. 6 7GCC is free software; you can redistribute it and/or modify it under 8the terms of the GNU General Public License as published by the Free 9Software Foundation; either version 2, or (at your option) any later 10version. 11 12GCC is distributed in the hope that it will be useful, but WITHOUT ANY 13WARRANTY; without even the implied warranty of MERCHANTABILITY or 14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15for more details. 16 17You should have received a copy of the GNU General Public License 18along with GCC; see the file COPYING. If not, write to the Free 19Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 2002110-1301, USA. */ 21 22#include "config.h" 23#include "system.h" 24#include "coretypes.h" 25#include "tm.h" 26#include "intl.h" 27#include "tree.h" 28#include "flags.h" 29#include "output.h" 30#include "c-pragma.h" 31#include "rtl.h" 32#include "ggc.h" 33#include "varray.h" 34#include "expr.h" 35#include "c-common.h" 36#include "diagnostic.h" 37#include "tm_p.h" 38#include "obstack.h" 39#include "cpplib.h" 40#include "target.h" 41#include "langhooks.h" 42#include "tree-inline.h" 43#include "c-tree.h" 44#include "toplev.h" 45#include "tree-iterator.h" 46#include "hashtab.h" 47#include "tree-mudflap.h" 48#include "opts.h" 49#include "real.h" 50#include "cgraph.h" 51 52cpp_reader *parse_in; /* Declared in c-pragma.h. */ 53 54/* We let tm.h override the types used here, to handle trivial differences 55 such as the choice of unsigned int or long unsigned int for size_t. 56 When machines start needing nontrivial differences in the size type, 57 it would be best to do something here to figure out automatically 58 from other information what type to use. */ 59 60#ifndef SIZE_TYPE 61#define SIZE_TYPE "long unsigned int" 62#endif 63 64#ifndef PID_TYPE 65#define PID_TYPE "int" 66#endif 67 68#ifndef WCHAR_TYPE 69#define WCHAR_TYPE "int" 70#endif 71 72/* WCHAR_TYPE gets overridden by -fshort-wchar. */ 73#define MODIFIED_WCHAR_TYPE \ 74 (flag_short_wchar ? "short unsigned int" : WCHAR_TYPE) 75 76#ifndef PTRDIFF_TYPE 77#define PTRDIFF_TYPE "long int" 78#endif 79 80#ifndef WINT_TYPE 81#define WINT_TYPE "unsigned int" 82#endif 83 84#ifndef INTMAX_TYPE 85#define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ 86 ? "int" \ 87 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ 88 ? "long int" \ 89 : "long long int")) 90#endif 91 92#ifndef UINTMAX_TYPE 93#define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ 94 ? "unsigned int" \ 95 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ 96 ? "long unsigned int" \ 97 : "long long unsigned int")) 98#endif 99 100/* The following symbols are subsumed in the c_global_trees array, and 101 listed here individually for documentation purposes. 102 103 INTEGER_TYPE and REAL_TYPE nodes for the standard data types. 104 105 tree short_integer_type_node; 106 tree long_integer_type_node; 107 tree long_long_integer_type_node; 108 109 tree short_unsigned_type_node; 110 tree long_unsigned_type_node; 111 tree long_long_unsigned_type_node; 112 113 tree truthvalue_type_node; 114 tree truthvalue_false_node; 115 tree truthvalue_true_node; 116 117 tree ptrdiff_type_node; 118 119 tree unsigned_char_type_node; 120 tree signed_char_type_node; 121 tree wchar_type_node; 122 tree signed_wchar_type_node; 123 tree unsigned_wchar_type_node; 124 125 tree float_type_node; 126 tree double_type_node; 127 tree long_double_type_node; 128 129 tree complex_integer_type_node; 130 tree complex_float_type_node; 131 tree complex_double_type_node; 132 tree complex_long_double_type_node; 133 134 tree intQI_type_node; 135 tree intHI_type_node; 136 tree intSI_type_node; 137 tree intDI_type_node; 138 tree intTI_type_node; 139 140 tree unsigned_intQI_type_node; 141 tree unsigned_intHI_type_node; 142 tree unsigned_intSI_type_node; 143 tree unsigned_intDI_type_node; 144 tree unsigned_intTI_type_node; 145 146 tree widest_integer_literal_type_node; 147 tree widest_unsigned_literal_type_node; 148 149 Nodes for types `void *' and `const void *'. 150 151 tree ptr_type_node, const_ptr_type_node; 152 153 Nodes for types `char *' and `const char *'. 154 155 tree string_type_node, const_string_type_node; 156 157 Type `char[SOMENUMBER]'. 158 Used when an array of char is needed and the size is irrelevant. 159 160 tree char_array_type_node; 161 162 Type `int[SOMENUMBER]' or something like it. 163 Used when an array of int needed and the size is irrelevant. 164 165 tree int_array_type_node; 166 167 Type `wchar_t[SOMENUMBER]' or something like it. 168 Used when a wide string literal is created. 169 170 tree wchar_array_type_node; 171 172 Type `int ()' -- used for implicit declaration of functions. 173 174 tree default_function_type; 175 176 A VOID_TYPE node, packaged in a TREE_LIST. 177 178 tree void_list_node; 179 180 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__, 181 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__ 182 VAR_DECLS, but C++ does.) 183 184 tree function_name_decl_node; 185 tree pretty_function_name_decl_node; 186 tree c99_function_name_decl_node; 187 188 Stack of nested function name VAR_DECLs. 189 190 tree saved_function_name_decls; 191 192*/ 193 194tree c_global_trees[CTI_MAX]; 195 196/* Switches common to the C front ends. */ 197 198/* Nonzero if prepreprocessing only. */ 199 200int flag_preprocess_only; 201 202/* Nonzero means don't output line number information. */ 203 204char flag_no_line_commands; 205 206/* Nonzero causes -E output not to be done, but directives such as 207 #define that have side effects are still obeyed. */ 208 209char flag_no_output; 210 211/* Nonzero means dump macros in some fashion. */ 212 213char flag_dump_macros; 214 215/* Nonzero means pass #include lines through to the output. */ 216 217char flag_dump_includes; 218 219/* Nonzero means process PCH files while preprocessing. */ 220 221bool flag_pch_preprocess; 222 223/* The file name to which we should write a precompiled header, or 224 NULL if no header will be written in this compile. */ 225 226const char *pch_file; 227 228/* Nonzero if an ISO standard was selected. It rejects macros in the 229 user's namespace. */ 230int flag_iso; 231 232/* Nonzero if -undef was given. It suppresses target built-in macros 233 and assertions. */ 234int flag_undef; 235 236/* Nonzero means don't recognize the non-ANSI builtin functions. */ 237 238int flag_no_builtin; 239 240/* Nonzero means don't recognize the non-ANSI builtin functions. 241 -ansi sets this. */ 242 243int flag_no_nonansi_builtin; 244 245/* Nonzero means give `double' the same size as `float'. */ 246 247int flag_short_double; 248 249/* Nonzero means give `wchar_t' the same size as `short'. */ 250 251int flag_short_wchar; 252 253/* Nonzero means allow Microsoft extensions without warnings or errors. */ 254int flag_ms_extensions; 255 256/* Nonzero means don't recognize the keyword `asm'. */ 257 258int flag_no_asm; 259 260/* Nonzero means give string constants the type `const char *', as mandated 261 by the standard. */ 262 263int flag_const_strings; 264 265/* Nonzero means to treat bitfields as signed unless they say `unsigned'. */ 266 267int flag_signed_bitfields = 1; 268 269/* Nonzero means warn about deprecated conversion from string constant to 270 `char *'. */ 271 272int warn_write_strings; 273 274/* Warn about #pragma directives that are not recognized. */ 275 276int warn_unknown_pragmas; /* Tri state variable. */ 277 278/* Warn about format/argument anomalies in calls to formatted I/O functions 279 (*printf, *scanf, strftime, strfmon, etc.). */ 280 281int warn_format; 282 283/* Warn about using __null (as NULL in C++) as sentinel. For code compiled 284 with GCC this doesn't matter as __null is guaranteed to have the right 285 size. */ 286 287int warn_strict_null_sentinel; 288 289/* Zero means that faster, ...NonNil variants of objc_msgSend... 290 calls will be used in ObjC; passing nil receivers to such calls 291 will most likely result in crashes. */ 292int flag_nil_receivers = 1; 293 294/* Nonzero means that code generation will be altered to support 295 "zero-link" execution. This currently affects ObjC only, but may 296 affect other languages in the future. */ 297int flag_zero_link = 0; 298 299/* Nonzero means emit an '__OBJC, __image_info' for the current translation 300 unit. It will inform the ObjC runtime that class definition(s) herein 301 contained are to replace one(s) previously loaded. */ 302int flag_replace_objc_classes = 0; 303 304/* C/ObjC language option variables. */ 305 306 307/* Nonzero means allow type mismatches in conditional expressions; 308 just make their values `void'. */ 309 310int flag_cond_mismatch; 311 312/* Nonzero means enable C89 Amendment 1 features. */ 313 314int flag_isoc94; 315 316/* Nonzero means use the ISO C99 dialect of C. */ 317 318int flag_isoc99; 319 320/* Nonzero means that we have builtin functions, and main is an int. */ 321 322int flag_hosted = 1; 323 324/* Warn if main is suspicious. */ 325 326int warn_main; 327 328 329/* ObjC language option variables. */ 330 331 332/* Open and close the file for outputting class declarations, if 333 requested (ObjC). */ 334 335int flag_gen_declaration; 336 337/* Tells the compiler that this is a special run. Do not perform any 338 compiling, instead we are to test some platform dependent features 339 and output a C header file with appropriate definitions. */ 340 341int print_struct_values; 342 343/* Tells the compiler what is the constant string class for Objc. */ 344 345const char *constant_string_class_name; 346 347 348/* C++ language option variables. */ 349 350 351/* Nonzero means don't recognize any extension keywords. */ 352 353int flag_no_gnu_keywords; 354 355/* Nonzero means do emit exported implementations of functions even if 356 they can be inlined. */ 357 358int flag_implement_inlines = 1; 359 360/* Nonzero means that implicit instantiations will be emitted if needed. */ 361 362int flag_implicit_templates = 1; 363 364/* Nonzero means that implicit instantiations of inline templates will be 365 emitted if needed, even if instantiations of non-inline templates 366 aren't. */ 367 368int flag_implicit_inline_templates = 1; 369 370/* Nonzero means generate separate instantiation control files and 371 juggle them at link time. */ 372 373int flag_use_repository; 374 375/* Nonzero if we want to issue diagnostics that the standard says are not 376 required. */ 377 378int flag_optional_diags = 1; 379 380/* Nonzero means we should attempt to elide constructors when possible. */ 381 382int flag_elide_constructors = 1; 383 384/* Nonzero means that member functions defined in class scope are 385 inline by default. */ 386 387int flag_default_inline = 1; 388 389/* Controls whether compiler generates 'type descriptor' that give 390 run-time type information. */ 391 392int flag_rtti = 1; 393 394/* Nonzero if we want to conserve space in the .o files. We do this 395 by putting uninitialized data and runtime initialized data into 396 .common instead of .data at the expense of not flagging multiple 397 definitions. */ 398 399int flag_conserve_space; 400 401/* Nonzero if we want to obey access control semantics. */ 402 403int flag_access_control = 1; 404 405/* Nonzero if we want to check the return value of new and avoid calling 406 constructors if it is a null pointer. */ 407 408int flag_check_new; 409 410/* Nonzero if we want the new ISO rules for pushing a new scope for `for' 411 initialization variables. 412 0: Old rules, set by -fno-for-scope. 413 2: New ISO rules, set by -ffor-scope. 414 1: Try to implement new ISO rules, but with backup compatibility 415 (and warnings). This is the default, for now. */ 416 417int flag_new_for_scope = 1; 418 419/* Nonzero if we want to emit defined symbols with common-like linkage as 420 weak symbols where possible, in order to conform to C++ semantics. 421 Otherwise, emit them as local symbols. */ 422 423int flag_weak = 1; 424 425/* 0 means we want the preprocessor to not emit line directives for 426 the current working directory. 1 means we want it to do it. -1 427 means we should decide depending on whether debugging information 428 is being emitted or not. */ 429 430int flag_working_directory = -1; 431 432/* Nonzero to use __cxa_atexit, rather than atexit, to register 433 destructors for local statics and global objects. */ 434 435int flag_use_cxa_atexit = DEFAULT_USE_CXA_ATEXIT; 436 437/* Nonzero means make the default pedwarns warnings instead of errors. 438 The value of this flag is ignored if -pedantic is specified. */ 439 440int flag_permissive; 441 442/* Nonzero means to implement standard semantics for exception 443 specifications, calling unexpected if an exception is thrown that 444 doesn't match the specification. Zero means to treat them as 445 assertions and optimize accordingly, but not check them. */ 446 447int flag_enforce_eh_specs = 1; 448 449/* Nonzero means to generate thread-safe code for initializing local 450 statics. */ 451 452int flag_threadsafe_statics = 1; 453 454/* Nonzero means warn about implicit declarations. */ 455 456int warn_implicit = 1; 457 458/* Maximum template instantiation depth. This limit is rather 459 arbitrary, but it exists to limit the time it takes to notice 460 infinite template instantiations. */ 461 462int max_tinst_depth = 500; 463 464 465 466/* The elements of `ridpointers' are identifier nodes for the reserved 467 type names and storage classes. It is indexed by a RID_... value. */ 468tree *ridpointers; 469 470tree (*make_fname_decl) (tree, int); 471 472/* Nonzero means the expression being parsed will never be evaluated. 473 This is a count, since unevaluated expressions can nest. */ 474int skip_evaluation; 475 476/* Information about how a function name is generated. */ 477struct fname_var_t 478{ 479 tree *const decl; /* pointer to the VAR_DECL. */ 480 const unsigned rid; /* RID number for the identifier. */ 481 const int pretty; /* How pretty is it? */ 482}; 483 484/* The three ways of getting then name of the current function. */ 485 486const struct fname_var_t fname_vars[] = 487{ 488 /* C99 compliant __func__, must be first. */ 489 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0}, 490 /* GCC __FUNCTION__ compliant. */ 491 {&function_name_decl_node, RID_FUNCTION_NAME, 0}, 492 /* GCC __PRETTY_FUNCTION__ compliant. */ 493 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1}, 494 {NULL, 0, 0}, 495}; 496 497static int constant_fits_type_p (tree, tree); 498static tree check_case_value (tree); 499static bool check_case_bounds (tree, tree, tree *, tree *); 500 501static tree handle_packed_attribute (tree *, tree, tree, int, bool *); 502static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *); 503static tree handle_common_attribute (tree *, tree, tree, int, bool *); 504static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *); 505static tree handle_noinline_attribute (tree *, tree, tree, int, bool *); 506static tree handle_always_inline_attribute (tree *, tree, tree, int, 507 bool *); 508static tree handle_gnu_inline_attribute (tree *, tree, tree, int, 509 bool *); 510static tree handle_flatten_attribute (tree *, tree, tree, int, bool *); 511static tree handle_used_attribute (tree *, tree, tree, int, bool *); 512static tree handle_unused_attribute (tree *, tree, tree, int, bool *); 513static tree handle_externally_visible_attribute (tree *, tree, tree, int, 514 bool *); 515static tree handle_const_attribute (tree *, tree, tree, int, bool *); 516static tree handle_transparent_union_attribute (tree *, tree, tree, 517 int, bool *); 518static tree handle_constructor_attribute (tree *, tree, tree, int, bool *); 519static tree handle_destructor_attribute (tree *, tree, tree, int, bool *); 520static tree handle_mode_attribute (tree *, tree, tree, int, bool *); 521static tree handle_section_attribute (tree *, tree, tree, int, bool *); 522static tree handle_aligned_attribute (tree *, tree, tree, int, bool *); 523static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ; 524static tree handle_alias_attribute (tree *, tree, tree, int, bool *); 525static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ; 526static tree handle_visibility_attribute (tree *, tree, tree, int, 527 bool *); 528static tree handle_tls_model_attribute (tree *, tree, tree, int, 529 bool *); 530static tree handle_no_instrument_function_attribute (tree *, tree, 531 tree, int, bool *); 532static tree handle_no_stack_protector_function_attribute (tree *, tree, 533 tree, int, bool *); 534static tree handle_malloc_attribute (tree *, tree, tree, int, bool *); 535static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *); 536static tree handle_no_limit_stack_attribute (tree *, tree, tree, int, 537 bool *); 538static tree handle_pure_attribute (tree *, tree, tree, int, bool *); 539static tree handle_novops_attribute (tree *, tree, tree, int, bool *); 540static tree handle_deprecated_attribute (tree *, tree, tree, int, 541 bool *); 542static tree handle_vector_size_attribute (tree *, tree, tree, int, 543 bool *); 544static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *); 545static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *); 546static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *); 547static tree handle_warn_unused_result_attribute (tree *, tree, tree, int, 548 bool *); 549static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *); 550 551static void check_function_nonnull (tree, tree); 552static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT); 553static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT); 554static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *); 555static int resort_field_decl_cmp (const void *, const void *); 556 557/* Table of machine-independent attributes common to all C-like languages. */ 558const struct attribute_spec c_common_attribute_table[] = 559{ 560 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */ 561 { "packed", 0, 0, false, false, false, 562 handle_packed_attribute }, 563 { "nocommon", 0, 0, true, false, false, 564 handle_nocommon_attribute }, 565 { "common", 0, 0, true, false, false, 566 handle_common_attribute }, 567 /* FIXME: logically, noreturn attributes should be listed as 568 "false, true, true" and apply to function types. But implementing this 569 would require all the places in the compiler that use TREE_THIS_VOLATILE 570 on a decl to identify non-returning functions to be located and fixed 571 to check the function type instead. */ 572 { "noreturn", 0, 0, true, false, false, 573 handle_noreturn_attribute }, 574 { "volatile", 0, 0, true, false, false, 575 handle_noreturn_attribute }, 576 { "noinline", 0, 0, true, false, false, 577 handle_noinline_attribute }, 578 { "always_inline", 0, 0, true, false, false, 579 handle_always_inline_attribute }, 580 { "gnu_inline", 0, 0, true, false, false, 581 handle_gnu_inline_attribute }, 582 { "flatten", 0, 0, true, false, false, 583 handle_flatten_attribute }, 584 { "used", 0, 0, true, false, false, 585 handle_used_attribute }, 586 { "unused", 0, 0, false, false, false, 587 handle_unused_attribute }, 588 { "externally_visible", 0, 0, true, false, false, 589 handle_externally_visible_attribute }, 590 /* The same comments as for noreturn attributes apply to const ones. */ 591 { "const", 0, 0, true, false, false, 592 handle_const_attribute }, 593 { "transparent_union", 0, 0, false, false, false, 594 handle_transparent_union_attribute }, 595 { "constructor", 0, 0, true, false, false, 596 handle_constructor_attribute }, 597 { "destructor", 0, 0, true, false, false, 598 handle_destructor_attribute }, 599 { "mode", 1, 1, false, true, false, 600 handle_mode_attribute }, 601 { "section", 1, 1, true, false, false, 602 handle_section_attribute }, 603 { "aligned", 0, 1, false, false, false, 604 handle_aligned_attribute }, 605 { "weak", 0, 0, true, false, false, 606 handle_weak_attribute }, 607 { "alias", 1, 1, true, false, false, 608 handle_alias_attribute }, 609 { "weakref", 0, 1, true, false, false, 610 handle_weakref_attribute }, 611 { "no_instrument_function", 0, 0, true, false, false, 612 handle_no_instrument_function_attribute }, 613 { "no_stack_protector", 0, 0, true, false, false, 614 handle_no_stack_protector_function_attribute }, 615 { "malloc", 0, 0, true, false, false, 616 handle_malloc_attribute }, 617 { "returns_twice", 0, 0, true, false, false, 618 handle_returns_twice_attribute }, 619 { "no_stack_limit", 0, 0, true, false, false, 620 handle_no_limit_stack_attribute }, 621 { "pure", 0, 0, true, false, false, 622 handle_pure_attribute }, 623 /* For internal use (marking of builtins) only. The name contains space 624 to prevent its usage in source code. */ 625 { "no vops", 0, 0, true, false, false, 626 handle_novops_attribute }, 627 { "deprecated", 0, 0, false, false, false, 628 handle_deprecated_attribute }, 629 { "vector_size", 1, 1, false, true, false, 630 handle_vector_size_attribute }, 631 { "visibility", 1, 1, false, false, false, 632 handle_visibility_attribute }, 633 { "tls_model", 1, 1, true, false, false, 634 handle_tls_model_attribute }, 635 { "nonnull", 0, -1, false, true, true, 636 handle_nonnull_attribute }, 637 { "nothrow", 0, 0, true, false, false, 638 handle_nothrow_attribute }, 639 { "may_alias", 0, 0, false, true, false, NULL }, 640 { "cleanup", 1, 1, true, false, false, 641 handle_cleanup_attribute }, 642 { "warn_unused_result", 0, 0, false, true, true, 643 handle_warn_unused_result_attribute }, 644 { "sentinel", 0, 1, false, true, true, 645 handle_sentinel_attribute }, 646 { NULL, 0, 0, false, false, false, NULL } 647}; 648 649/* Give the specifications for the format attributes, used by C and all 650 descendants. */ 651 652const struct attribute_spec c_common_format_attribute_table[] = 653{ 654 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */ 655 { "format", 3, 3, false, true, true, 656 handle_format_attribute }, 657 { "format_arg", 1, 1, false, true, true, 658 handle_format_arg_attribute }, 659 { NULL, 0, 0, false, false, false, NULL } 660}; 661 662/* Push current bindings for the function name VAR_DECLS. */ 663 664void 665start_fname_decls (void) 666{ 667 unsigned ix; 668 tree saved = NULL_TREE; 669 670 for (ix = 0; fname_vars[ix].decl; ix++) 671 { 672 tree decl = *fname_vars[ix].decl; 673 674 if (decl) 675 { 676 saved = tree_cons (decl, build_int_cst (NULL_TREE, ix), saved); 677 *fname_vars[ix].decl = NULL_TREE; 678 } 679 } 680 if (saved || saved_function_name_decls) 681 /* Normally they'll have been NULL, so only push if we've got a 682 stack, or they are non-NULL. */ 683 saved_function_name_decls = tree_cons (saved, NULL_TREE, 684 saved_function_name_decls); 685} 686 687/* Finish up the current bindings, adding them into the current function's 688 statement tree. This must be done _before_ finish_stmt_tree is called. 689 If there is no current function, we must be at file scope and no statements 690 are involved. Pop the previous bindings. */ 691 692void 693finish_fname_decls (void) 694{ 695 unsigned ix; 696 tree stmts = NULL_TREE; 697 tree stack = saved_function_name_decls; 698 699 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack)) 700 append_to_statement_list (TREE_VALUE (stack), &stmts); 701 702 if (stmts) 703 { 704 tree *bodyp = &DECL_SAVED_TREE (current_function_decl); 705 706 if (TREE_CODE (*bodyp) == BIND_EXPR) 707 bodyp = &BIND_EXPR_BODY (*bodyp); 708 709 append_to_statement_list_force (*bodyp, &stmts); 710 *bodyp = stmts; 711 } 712 713 for (ix = 0; fname_vars[ix].decl; ix++) 714 *fname_vars[ix].decl = NULL_TREE; 715 716 if (stack) 717 { 718 /* We had saved values, restore them. */ 719 tree saved; 720 721 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved)) 722 { 723 tree decl = TREE_PURPOSE (saved); 724 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved)); 725 726 *fname_vars[ix].decl = decl; 727 } 728 stack = TREE_CHAIN (stack); 729 } 730 saved_function_name_decls = stack; 731} 732 733/* Return the text name of the current function, suitably prettified 734 by PRETTY_P. Return string must be freed by caller. */ 735 736const char * 737fname_as_string (int pretty_p) 738{ 739 const char *name = "top level"; 740 char *namep; 741 int vrb = 2; 742 743 if (!pretty_p) 744 { 745 name = ""; 746 vrb = 0; 747 } 748 749 if (current_function_decl) 750 name = lang_hooks.decl_printable_name (current_function_decl, vrb); 751 752 if (c_lex_string_translate) 753 { 754 int len = strlen (name) + 3; /* Two for '"'s. One for NULL. */ 755 cpp_string cstr = { 0, 0 }, strname; 756 757 namep = XNEWVEC (char, len); 758 snprintf (namep, len, "\"%s\"", name); 759 strname.text = (unsigned char *) namep; 760 strname.len = len - 1; 761 762 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, false)) 763 { 764 XDELETEVEC (namep); 765 return (char *) cstr.text; 766 } 767 } 768 else 769 namep = xstrdup (name); 770 771 return namep; 772} 773 774/* Expand DECL if it declares an entity not handled by the 775 common code. */ 776 777int 778c_expand_decl (tree decl) 779{ 780 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl)) 781 { 782 /* Let the back-end know about this variable. */ 783 if (!anon_aggr_type_p (TREE_TYPE (decl))) 784 emit_local_var (decl); 785 else 786 expand_anon_union_decl (decl, NULL_TREE, 787 DECL_ANON_UNION_ELEMS (decl)); 788 } 789 else 790 return 0; 791 792 return 1; 793} 794 795 796/* Return the VAR_DECL for a const char array naming the current 797 function. If the VAR_DECL has not yet been created, create it 798 now. RID indicates how it should be formatted and IDENTIFIER_NODE 799 ID is its name (unfortunately C and C++ hold the RID values of 800 keywords in different places, so we can't derive RID from ID in 801 this language independent code. */ 802 803tree 804fname_decl (unsigned int rid, tree id) 805{ 806 unsigned ix; 807 tree decl = NULL_TREE; 808 809 for (ix = 0; fname_vars[ix].decl; ix++) 810 if (fname_vars[ix].rid == rid) 811 break; 812 813 decl = *fname_vars[ix].decl; 814 if (!decl) 815 { 816 /* If a tree is built here, it would normally have the lineno of 817 the current statement. Later this tree will be moved to the 818 beginning of the function and this line number will be wrong. 819 To avoid this problem set the lineno to 0 here; that prevents 820 it from appearing in the RTL. */ 821 tree stmts; 822 location_t saved_location = input_location; 823#ifdef USE_MAPPED_LOCATION 824 input_location = UNKNOWN_LOCATION; 825#else 826 input_line = 0; 827#endif 828 829 stmts = push_stmt_list (); 830 decl = (*make_fname_decl) (id, fname_vars[ix].pretty); 831 stmts = pop_stmt_list (stmts); 832 if (!IS_EMPTY_STMT (stmts)) 833 saved_function_name_decls 834 = tree_cons (decl, stmts, saved_function_name_decls); 835 *fname_vars[ix].decl = decl; 836 input_location = saved_location; 837 } 838 if (!ix && !current_function_decl) 839 pedwarn ("%qD is not defined outside of function scope", decl); 840 841 return decl; 842} 843 844/* Given a STRING_CST, give it a suitable array-of-chars data type. */ 845 846tree 847fix_string_type (tree value) 848{ 849 const int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT; 850 const int wide_flag = TREE_TYPE (value) == wchar_array_type_node; 851 const int nchars_max = flag_isoc99 ? 4095 : 509; 852 int length = TREE_STRING_LENGTH (value); 853 int nchars; 854 tree e_type, i_type, a_type; 855 856 /* Compute the number of elements, for the array type. */ 857 nchars = wide_flag ? length / wchar_bytes : length; 858 859 if (pedantic && nchars - 1 > nchars_max && !c_dialect_cxx ()) 860 pedwarn ("string length %qd is greater than the length %qd ISO C%d compilers are required to support", 861 nchars - 1, nchars_max, flag_isoc99 ? 99 : 89); 862 863 e_type = wide_flag ? wchar_type_node : char_type_node; 864 /* Create the array type for the string constant. flag_const_strings 865 says make the string constant an array of const char so that 866 copying it to a non-const pointer will get a warning. For C++, 867 this is the standard behavior. 868 869 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified 870 array type being the unqualified version of that type. 871 Therefore, if we are constructing an array of const char, we must 872 construct the matching unqualified array type first. The C front 873 end does not require this, but it does no harm, so we do it 874 unconditionally. */ 875 i_type = build_index_type (build_int_cst (NULL_TREE, nchars - 1)); 876 a_type = build_array_type (e_type, i_type); 877 if (flag_const_strings) 878 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST); 879 880 TREE_TYPE (value) = a_type; 881 TREE_CONSTANT (value) = 1; 882 TREE_INVARIANT (value) = 1; 883 TREE_READONLY (value) = 1; 884 TREE_STATIC (value) = 1; 885 return value; 886} 887 888/* Print a warning if a constant expression had overflow in folding. 889 Invoke this function on every expression that the language 890 requires to be a constant expression. 891 Note the ANSI C standard says it is erroneous for a 892 constant expression to overflow. */ 893 894void 895constant_expression_warning (tree value) 896{ 897 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST 898 || TREE_CODE (value) == VECTOR_CST 899 || TREE_CODE (value) == COMPLEX_CST) 900 && TREE_CONSTANT_OVERFLOW (value) && pedantic) 901 pedwarn ("overflow in constant expression"); 902} 903 904/* Print a warning if an expression had overflow in folding. 905 Invoke this function on every expression that 906 (1) appears in the source code, and 907 (2) might be a constant expression that overflowed, and 908 (3) is not already checked by convert_and_check; 909 however, do not invoke this function on operands of explicit casts. */ 910 911void 912overflow_warning (tree value) 913{ 914 if ((TREE_CODE (value) == INTEGER_CST 915 || (TREE_CODE (value) == COMPLEX_CST 916 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)) 917 && TREE_OVERFLOW (value)) 918 { 919 TREE_OVERFLOW (value) = 0; 920 if (skip_evaluation == 0) 921 warning (0, "integer overflow in expression"); 922 } 923 else if ((TREE_CODE (value) == REAL_CST 924 || (TREE_CODE (value) == COMPLEX_CST 925 && TREE_CODE (TREE_REALPART (value)) == REAL_CST)) 926 && TREE_OVERFLOW (value)) 927 { 928 TREE_OVERFLOW (value) = 0; 929 if (skip_evaluation == 0) 930 warning (0, "floating point overflow in expression"); 931 } 932 else if (TREE_CODE (value) == VECTOR_CST && TREE_OVERFLOW (value)) 933 { 934 TREE_OVERFLOW (value) = 0; 935 if (skip_evaluation == 0) 936 warning (0, "vector overflow in expression"); 937 } 938} 939 940/* Print a warning if a large constant is truncated to unsigned, 941 or if -Wconversion is used and a constant < 0 is converted to unsigned. 942 Invoke this function on every expression that might be implicitly 943 converted to an unsigned type. */ 944 945void 946unsigned_conversion_warning (tree result, tree operand) 947{ 948 tree type = TREE_TYPE (result); 949 950 if (TREE_CODE (operand) == INTEGER_CST 951 && TREE_CODE (type) == INTEGER_TYPE 952 && TYPE_UNSIGNED (type) 953 && skip_evaluation == 0 954 && !int_fits_type_p (operand, type)) 955 { 956 if (!int_fits_type_p (operand, c_common_signed_type (type))) 957 /* This detects cases like converting -129 or 256 to unsigned char. */ 958 warning (0, "large integer implicitly truncated to unsigned type"); 959 else 960 warning (OPT_Wconversion, 961 "negative integer implicitly converted to unsigned type"); 962 } 963} 964 965/* Print a warning about casts that might indicate violation 966 of strict aliasing rules if -Wstrict-aliasing is used and 967 strict aliasing mode is in effect. otype is the original 968 TREE_TYPE of expr, and type the type we're casting to. */ 969 970void 971strict_aliasing_warning(tree otype, tree type, tree expr) 972{ 973 if (flag_strict_aliasing && warn_strict_aliasing 974 && POINTER_TYPE_P (type) && POINTER_TYPE_P (otype) 975 && TREE_CODE (expr) == ADDR_EXPR 976 && (DECL_P (TREE_OPERAND (expr, 0)) 977 || handled_component_p (TREE_OPERAND (expr, 0))) 978 && !VOID_TYPE_P (TREE_TYPE (type))) 979 { 980 /* Casting the address of an object to non void pointer. Warn 981 if the cast breaks type based aliasing. */ 982 if (!COMPLETE_TYPE_P (TREE_TYPE (type))) 983 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type " 984 "might break strict-aliasing rules"); 985 else 986 { 987 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); 988 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type)); 989 990 if (!alias_sets_conflict_p (set1, set2)) 991 warning (OPT_Wstrict_aliasing, "dereferencing type-punned " 992 "pointer will break strict-aliasing rules"); 993 else if (warn_strict_aliasing > 1 994 && !alias_sets_might_conflict_p (set1, set2)) 995 warning (OPT_Wstrict_aliasing, "dereferencing type-punned " 996 "pointer might break strict-aliasing rules"); 997 } 998 } 999} 1000 1001/* Nonzero if constant C has a value that is permissible 1002 for type TYPE (an INTEGER_TYPE). */ 1003 1004static int 1005constant_fits_type_p (tree c, tree type) 1006{ 1007 if (TREE_CODE (c) == INTEGER_CST) 1008 return int_fits_type_p (c, type); 1009 1010 c = convert (type, c); 1011 return !TREE_OVERFLOW (c); 1012} 1013 1014/* Nonzero if vector types T1 and T2 can be converted to each other 1015 without an explicit cast. */ 1016int 1017vector_types_convertible_p (tree t1, tree t2) 1018{ 1019 return targetm.vector_opaque_p (t1) 1020 || targetm.vector_opaque_p (t2) 1021 || (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)) 1022 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE || 1023 TYPE_PRECISION (t1) == TYPE_PRECISION (t2)) 1024 && INTEGRAL_TYPE_P (TREE_TYPE (t1)) 1025 == INTEGRAL_TYPE_P (TREE_TYPE (t2))); 1026} 1027 1028/* Convert EXPR to TYPE, warning about conversion problems with constants. 1029 Invoke this function on every expression that is converted implicitly, 1030 i.e. because of language rules and not because of an explicit cast. */ 1031 1032tree 1033convert_and_check (tree type, tree expr) 1034{ 1035 tree t = convert (type, expr); 1036 if (TREE_CODE (t) == INTEGER_CST) 1037 { 1038 if (TREE_OVERFLOW (t)) 1039 { 1040 TREE_OVERFLOW (t) = 0; 1041 1042 /* Do not diagnose overflow in a constant expression merely 1043 because a conversion overflowed. */ 1044 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr); 1045 1046 /* No warning for converting 0x80000000 to int. */ 1047 if (!(TYPE_UNSIGNED (type) < TYPE_UNSIGNED (TREE_TYPE (expr)) 1048 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE 1049 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr)))) 1050 /* If EXPR fits in the unsigned version of TYPE, 1051 don't warn unless pedantic. */ 1052 if ((pedantic 1053 || TYPE_UNSIGNED (type) 1054 || !constant_fits_type_p (expr, 1055 c_common_unsigned_type (type))) 1056 && skip_evaluation == 0) 1057 warning (0, "overflow in implicit constant conversion"); 1058 } 1059 else 1060 unsigned_conversion_warning (t, expr); 1061 } 1062 return t; 1063} 1064 1065/* A node in a list that describes references to variables (EXPR), which are 1066 either read accesses if WRITER is zero, or write accesses, in which case 1067 WRITER is the parent of EXPR. */ 1068struct tlist 1069{ 1070 struct tlist *next; 1071 tree expr, writer; 1072}; 1073 1074/* Used to implement a cache the results of a call to verify_tree. We only 1075 use this for SAVE_EXPRs. */ 1076struct tlist_cache 1077{ 1078 struct tlist_cache *next; 1079 struct tlist *cache_before_sp; 1080 struct tlist *cache_after_sp; 1081 tree expr; 1082}; 1083 1084/* Obstack to use when allocating tlist structures, and corresponding 1085 firstobj. */ 1086static struct obstack tlist_obstack; 1087static char *tlist_firstobj = 0; 1088 1089/* Keep track of the identifiers we've warned about, so we can avoid duplicate 1090 warnings. */ 1091static struct tlist *warned_ids; 1092/* SAVE_EXPRs need special treatment. We process them only once and then 1093 cache the results. */ 1094static struct tlist_cache *save_expr_cache; 1095 1096static void add_tlist (struct tlist **, struct tlist *, tree, int); 1097static void merge_tlist (struct tlist **, struct tlist *, int); 1098static void verify_tree (tree, struct tlist **, struct tlist **, tree); 1099static int warning_candidate_p (tree); 1100static void warn_for_collisions (struct tlist *); 1101static void warn_for_collisions_1 (tree, tree, struct tlist *, int); 1102static struct tlist *new_tlist (struct tlist *, tree, tree); 1103 1104/* Create a new struct tlist and fill in its fields. */ 1105static struct tlist * 1106new_tlist (struct tlist *next, tree t, tree writer) 1107{ 1108 struct tlist *l; 1109 l = XOBNEW (&tlist_obstack, struct tlist); 1110 l->next = next; 1111 l->expr = t; 1112 l->writer = writer; 1113 return l; 1114} 1115 1116/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER 1117 is nonnull, we ignore any node we find which has a writer equal to it. */ 1118 1119static void 1120add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy) 1121{ 1122 while (add) 1123 { 1124 struct tlist *next = add->next; 1125 if (!copy) 1126 add->next = *to; 1127 if (!exclude_writer || add->writer != exclude_writer) 1128 *to = copy ? new_tlist (*to, add->expr, add->writer) : add; 1129 add = next; 1130 } 1131} 1132 1133/* Merge the nodes of ADD into TO. This merging process is done so that for 1134 each variable that already exists in TO, no new node is added; however if 1135 there is a write access recorded in ADD, and an occurrence on TO is only 1136 a read access, then the occurrence in TO will be modified to record the 1137 write. */ 1138 1139static void 1140merge_tlist (struct tlist **to, struct tlist *add, int copy) 1141{ 1142 struct tlist **end = to; 1143 1144 while (*end) 1145 end = &(*end)->next; 1146 1147 while (add) 1148 { 1149 int found = 0; 1150 struct tlist *tmp2; 1151 struct tlist *next = add->next; 1152 1153 for (tmp2 = *to; tmp2; tmp2 = tmp2->next) 1154 if (tmp2->expr == add->expr) 1155 { 1156 found = 1; 1157 if (!tmp2->writer) 1158 tmp2->writer = add->writer; 1159 } 1160 if (!found) 1161 { 1162 *end = copy ? add : new_tlist (NULL, add->expr, add->writer); 1163 end = &(*end)->next; 1164 *end = 0; 1165 } 1166 add = next; 1167 } 1168} 1169 1170/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable 1171 references in list LIST conflict with it, excluding reads if ONLY writers 1172 is nonzero. */ 1173 1174static void 1175warn_for_collisions_1 (tree written, tree writer, struct tlist *list, 1176 int only_writes) 1177{ 1178 struct tlist *tmp; 1179 1180 /* Avoid duplicate warnings. */ 1181 for (tmp = warned_ids; tmp; tmp = tmp->next) 1182 if (tmp->expr == written) 1183 return; 1184 1185 while (list) 1186 { 1187 if (list->expr == written 1188 && list->writer != writer 1189 && (!only_writes || list->writer) 1190 && DECL_NAME (list->expr)) 1191 { 1192 warned_ids = new_tlist (warned_ids, written, NULL_TREE); 1193 warning (0, "operation on %qE may be undefined", list->expr); 1194 } 1195 list = list->next; 1196 } 1197} 1198 1199/* Given a list LIST of references to variables, find whether any of these 1200 can cause conflicts due to missing sequence points. */ 1201 1202static void 1203warn_for_collisions (struct tlist *list) 1204{ 1205 struct tlist *tmp; 1206 1207 for (tmp = list; tmp; tmp = tmp->next) 1208 { 1209 if (tmp->writer) 1210 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0); 1211 } 1212} 1213 1214/* Return nonzero if X is a tree that can be verified by the sequence point 1215 warnings. */ 1216static int 1217warning_candidate_p (tree x) 1218{ 1219 return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL; 1220} 1221 1222/* Walk the tree X, and record accesses to variables. If X is written by the 1223 parent tree, WRITER is the parent. 1224 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this 1225 expression or its only operand forces a sequence point, then everything up 1226 to the sequence point is stored in PBEFORE_SP. Everything else gets stored 1227 in PNO_SP. 1228 Once we return, we will have emitted warnings if any subexpression before 1229 such a sequence point could be undefined. On a higher level, however, the 1230 sequence point may not be relevant, and we'll merge the two lists. 1231 1232 Example: (b++, a) + b; 1233 The call that processes the COMPOUND_EXPR will store the increment of B 1234 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that 1235 processes the PLUS_EXPR will need to merge the two lists so that 1236 eventually, all accesses end up on the same list (and we'll warn about the 1237 unordered subexpressions b++ and b. 1238 1239 A note on merging. If we modify the former example so that our expression 1240 becomes 1241 (b++, b) + a 1242 care must be taken not simply to add all three expressions into the final 1243 PNO_SP list. The function merge_tlist takes care of that by merging the 1244 before-SP list of the COMPOUND_EXPR into its after-SP list in a special 1245 way, so that no more than one access to B is recorded. */ 1246 1247static void 1248verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp, 1249 tree writer) 1250{ 1251 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3; 1252 enum tree_code code; 1253 enum tree_code_class cl; 1254 1255 /* X may be NULL if it is the operand of an empty statement expression 1256 ({ }). */ 1257 if (x == NULL) 1258 return; 1259 1260 restart: 1261 code = TREE_CODE (x); 1262 cl = TREE_CODE_CLASS (code); 1263 1264 if (warning_candidate_p (x)) 1265 { 1266 *pno_sp = new_tlist (*pno_sp, x, writer); 1267 return; 1268 } 1269 1270 switch (code) 1271 { 1272 case CONSTRUCTOR: 1273 return; 1274 1275 case COMPOUND_EXPR: 1276 case TRUTH_ANDIF_EXPR: 1277 case TRUTH_ORIF_EXPR: 1278 tmp_before = tmp_nosp = tmp_list3 = 0; 1279 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); 1280 warn_for_collisions (tmp_nosp); 1281 merge_tlist (pbefore_sp, tmp_before, 0); 1282 merge_tlist (pbefore_sp, tmp_nosp, 0); 1283 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE); 1284 merge_tlist (pbefore_sp, tmp_list3, 0); 1285 return; 1286 1287 case COND_EXPR: 1288 tmp_before = tmp_list2 = 0; 1289 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE); 1290 warn_for_collisions (tmp_list2); 1291 merge_tlist (pbefore_sp, tmp_before, 0); 1292 merge_tlist (pbefore_sp, tmp_list2, 1); 1293 1294 tmp_list3 = tmp_nosp = 0; 1295 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE); 1296 warn_for_collisions (tmp_nosp); 1297 merge_tlist (pbefore_sp, tmp_list3, 0); 1298 1299 tmp_list3 = tmp_list2 = 0; 1300 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE); 1301 warn_for_collisions (tmp_list2); 1302 merge_tlist (pbefore_sp, tmp_list3, 0); 1303 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the 1304 two first, to avoid warning for (a ? b++ : b++). */ 1305 merge_tlist (&tmp_nosp, tmp_list2, 0); 1306 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); 1307 return; 1308 1309 case PREDECREMENT_EXPR: 1310 case PREINCREMENT_EXPR: 1311 case POSTDECREMENT_EXPR: 1312 case POSTINCREMENT_EXPR: 1313 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x); 1314 return; 1315 1316 case MODIFY_EXPR: 1317 tmp_before = tmp_nosp = tmp_list3 = 0; 1318 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE); 1319 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x); 1320 /* Expressions inside the LHS are not ordered wrt. the sequence points 1321 in the RHS. Example: 1322 *a = (a++, 2) 1323 Despite the fact that the modification of "a" is in the before_sp 1324 list (tmp_before), it conflicts with the use of "a" in the LHS. 1325 We can handle this by adding the contents of tmp_list3 1326 to those of tmp_before, and redoing the collision warnings for that 1327 list. */ 1328 add_tlist (&tmp_before, tmp_list3, x, 1); 1329 warn_for_collisions (tmp_before); 1330 /* Exclude the LHS itself here; we first have to merge it into the 1331 tmp_nosp list. This is done to avoid warning for "a = a"; if we 1332 didn't exclude the LHS, we'd get it twice, once as a read and once 1333 as a write. */ 1334 add_tlist (pno_sp, tmp_list3, x, 0); 1335 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1); 1336 1337 merge_tlist (pbefore_sp, tmp_before, 0); 1338 if (warning_candidate_p (TREE_OPERAND (x, 0))) 1339 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0); 1340 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1); 1341 return; 1342 1343 case CALL_EXPR: 1344 /* We need to warn about conflicts among arguments and conflicts between 1345 args and the function address. Side effects of the function address, 1346 however, are not ordered by the sequence point of the call. */ 1347 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0; 1348 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); 1349 if (TREE_OPERAND (x, 1)) 1350 verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE); 1351 merge_tlist (&tmp_list3, tmp_list2, 0); 1352 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0); 1353 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0); 1354 warn_for_collisions (tmp_before); 1355 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0); 1356 return; 1357 1358 case TREE_LIST: 1359 /* Scan all the list, e.g. indices of multi dimensional array. */ 1360 while (x) 1361 { 1362 tmp_before = tmp_nosp = 0; 1363 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE); 1364 merge_tlist (&tmp_nosp, tmp_before, 0); 1365 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); 1366 x = TREE_CHAIN (x); 1367 } 1368 return; 1369 1370 case SAVE_EXPR: 1371 { 1372 struct tlist_cache *t; 1373 for (t = save_expr_cache; t; t = t->next) 1374 if (t->expr == x) 1375 break; 1376 1377 if (!t) 1378 { 1379 t = XOBNEW (&tlist_obstack, struct tlist_cache); 1380 t->next = save_expr_cache; 1381 t->expr = x; 1382 save_expr_cache = t; 1383 1384 tmp_before = tmp_nosp = 0; 1385 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); 1386 warn_for_collisions (tmp_nosp); 1387 1388 tmp_list3 = 0; 1389 while (tmp_nosp) 1390 { 1391 struct tlist *t = tmp_nosp; 1392 tmp_nosp = t->next; 1393 merge_tlist (&tmp_list3, t, 0); 1394 } 1395 t->cache_before_sp = tmp_before; 1396 t->cache_after_sp = tmp_list3; 1397 } 1398 merge_tlist (pbefore_sp, t->cache_before_sp, 1); 1399 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1); 1400 return; 1401 } 1402 1403 default: 1404 /* For other expressions, simply recurse on their operands. 1405 Manual tail recursion for unary expressions. 1406 Other non-expressions need not be processed. */ 1407 if (cl == tcc_unary) 1408 { 1409 x = TREE_OPERAND (x, 0); 1410 writer = 0; 1411 goto restart; 1412 } 1413 else if (IS_EXPR_CODE_CLASS (cl)) 1414 { 1415 int lp; 1416 int max = TREE_CODE_LENGTH (TREE_CODE (x)); 1417 for (lp = 0; lp < max; lp++) 1418 { 1419 tmp_before = tmp_nosp = 0; 1420 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0); 1421 merge_tlist (&tmp_nosp, tmp_before, 0); 1422 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); 1423 } 1424 } 1425 return; 1426 } 1427} 1428 1429/* Try to warn for undefined behavior in EXPR due to missing sequence 1430 points. */ 1431 1432void 1433verify_sequence_points (tree expr) 1434{ 1435 struct tlist *before_sp = 0, *after_sp = 0; 1436 1437 warned_ids = 0; 1438 save_expr_cache = 0; 1439 if (tlist_firstobj == 0) 1440 { 1441 gcc_obstack_init (&tlist_obstack); 1442 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0); 1443 } 1444 1445 verify_tree (expr, &before_sp, &after_sp, 0); 1446 warn_for_collisions (after_sp); 1447 obstack_free (&tlist_obstack, tlist_firstobj); 1448} 1449 1450/* Validate the expression after `case' and apply default promotions. */ 1451 1452static tree 1453check_case_value (tree value) 1454{ 1455 if (value == NULL_TREE) 1456 return value; 1457 1458 /* ??? Can we ever get nops here for a valid case value? We 1459 shouldn't for C. */ 1460 STRIP_TYPE_NOPS (value); 1461 /* In C++, the following is allowed: 1462 1463 const int i = 3; 1464 switch (...) { case i: ... } 1465 1466 So, we try to reduce the VALUE to a constant that way. */ 1467 if (c_dialect_cxx ()) 1468 { 1469 value = decl_constant_value (value); 1470 STRIP_TYPE_NOPS (value); 1471 value = fold (value); 1472 } 1473 1474 if (TREE_CODE (value) == INTEGER_CST) 1475 /* Promote char or short to int. */ 1476 value = perform_integral_promotions (value); 1477 else if (value != error_mark_node) 1478 { 1479 error ("case label does not reduce to an integer constant"); 1480 value = error_mark_node; 1481 } 1482 1483 constant_expression_warning (value); 1484 1485 return value; 1486} 1487 1488/* See if the case values LOW and HIGH are in the range of the original 1489 type (i.e. before the default conversion to int) of the switch testing 1490 expression. 1491 TYPE is the promoted type of the testing expression, and ORIG_TYPE is 1492 the type before promoting it. CASE_LOW_P is a pointer to the lower 1493 bound of the case label, and CASE_HIGH_P is the upper bound or NULL 1494 if the case is not a case range. 1495 The caller has to make sure that we are not called with NULL for 1496 CASE_LOW_P (i.e. the default case). 1497 Returns true if the case label is in range of ORIG_TYPE (saturated or 1498 untouched) or false if the label is out of range. */ 1499 1500static bool 1501check_case_bounds (tree type, tree orig_type, 1502 tree *case_low_p, tree *case_high_p) 1503{ 1504 tree min_value, max_value; 1505 tree case_low = *case_low_p; 1506 tree case_high = case_high_p ? *case_high_p : case_low; 1507 1508 /* If there was a problem with the original type, do nothing. */ 1509 if (orig_type == error_mark_node) 1510 return true; 1511 1512 min_value = TYPE_MIN_VALUE (orig_type); 1513 max_value = TYPE_MAX_VALUE (orig_type); 1514 1515 /* Case label is less than minimum for type. */ 1516 if (tree_int_cst_compare (case_low, min_value) < 0 1517 && tree_int_cst_compare (case_high, min_value) < 0) 1518 { 1519 warning (0, "case label value is less than minimum value for type"); 1520 return false; 1521 } 1522 1523 /* Case value is greater than maximum for type. */ 1524 if (tree_int_cst_compare (case_low, max_value) > 0 1525 && tree_int_cst_compare (case_high, max_value) > 0) 1526 { 1527 warning (0, "case label value exceeds maximum value for type"); 1528 return false; 1529 } 1530 1531 /* Saturate lower case label value to minimum. */ 1532 if (tree_int_cst_compare (case_high, min_value) >= 0 1533 && tree_int_cst_compare (case_low, min_value) < 0) 1534 { 1535 warning (0, "lower value in case label range" 1536 " less than minimum value for type"); 1537 case_low = min_value; 1538 } 1539 1540 /* Saturate upper case label value to maximum. */ 1541 if (tree_int_cst_compare (case_low, max_value) <= 0 1542 && tree_int_cst_compare (case_high, max_value) > 0) 1543 { 1544 warning (0, "upper value in case label range" 1545 " exceeds maximum value for type"); 1546 case_high = max_value; 1547 } 1548 1549 if (*case_low_p != case_low) 1550 *case_low_p = convert (type, case_low); 1551 if (case_high_p && *case_high_p != case_high) 1552 *case_high_p = convert (type, case_high); 1553 1554 return true; 1555} 1556 1557/* Return an integer type with BITS bits of precision, 1558 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */ 1559 1560tree 1561c_common_type_for_size (unsigned int bits, int unsignedp) 1562{ 1563 if (bits == TYPE_PRECISION (integer_type_node)) 1564 return unsignedp ? unsigned_type_node : integer_type_node; 1565 1566 if (bits == TYPE_PRECISION (signed_char_type_node)) 1567 return unsignedp ? unsigned_char_type_node : signed_char_type_node; 1568 1569 if (bits == TYPE_PRECISION (short_integer_type_node)) 1570 return unsignedp ? short_unsigned_type_node : short_integer_type_node; 1571 1572 if (bits == TYPE_PRECISION (long_integer_type_node)) 1573 return unsignedp ? long_unsigned_type_node : long_integer_type_node; 1574 1575 if (bits == TYPE_PRECISION (long_long_integer_type_node)) 1576 return (unsignedp ? long_long_unsigned_type_node 1577 : long_long_integer_type_node); 1578 1579 if (bits == TYPE_PRECISION (widest_integer_literal_type_node)) 1580 return (unsignedp ? widest_unsigned_literal_type_node 1581 : widest_integer_literal_type_node); 1582 1583 if (bits <= TYPE_PRECISION (intQI_type_node)) 1584 return unsignedp ? unsigned_intQI_type_node : intQI_type_node; 1585 1586 if (bits <= TYPE_PRECISION (intHI_type_node)) 1587 return unsignedp ? unsigned_intHI_type_node : intHI_type_node; 1588 1589 if (bits <= TYPE_PRECISION (intSI_type_node)) 1590 return unsignedp ? unsigned_intSI_type_node : intSI_type_node; 1591 1592 if (bits <= TYPE_PRECISION (intDI_type_node)) 1593 return unsignedp ? unsigned_intDI_type_node : intDI_type_node; 1594 1595 return 0; 1596} 1597 1598/* Used for communication between c_common_type_for_mode and 1599 c_register_builtin_type. */ 1600static GTY(()) tree registered_builtin_types; 1601 1602/* Return a data type that has machine mode MODE. 1603 If the mode is an integer, 1604 then UNSIGNEDP selects between signed and unsigned types. */ 1605 1606tree 1607c_common_type_for_mode (enum machine_mode mode, int unsignedp) 1608{ 1609 tree t; 1610 1611 if (mode == TYPE_MODE (integer_type_node)) 1612 return unsignedp ? unsigned_type_node : integer_type_node; 1613 1614 if (mode == TYPE_MODE (signed_char_type_node)) 1615 return unsignedp ? unsigned_char_type_node : signed_char_type_node; 1616 1617 if (mode == TYPE_MODE (short_integer_type_node)) 1618 return unsignedp ? short_unsigned_type_node : short_integer_type_node; 1619 1620 if (mode == TYPE_MODE (long_integer_type_node)) 1621 return unsignedp ? long_unsigned_type_node : long_integer_type_node; 1622 1623 if (mode == TYPE_MODE (long_long_integer_type_node)) 1624 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; 1625 1626 if (mode == TYPE_MODE (widest_integer_literal_type_node)) 1627 return unsignedp ? widest_unsigned_literal_type_node 1628 : widest_integer_literal_type_node; 1629 1630 if (mode == QImode) 1631 return unsignedp ? unsigned_intQI_type_node : intQI_type_node; 1632 1633 if (mode == HImode) 1634 return unsignedp ? unsigned_intHI_type_node : intHI_type_node; 1635 1636 if (mode == SImode) 1637 return unsignedp ? unsigned_intSI_type_node : intSI_type_node; 1638 1639 if (mode == DImode) 1640 return unsignedp ? unsigned_intDI_type_node : intDI_type_node; 1641 1642#if HOST_BITS_PER_WIDE_INT >= 64 1643 if (mode == TYPE_MODE (intTI_type_node)) 1644 return unsignedp ? unsigned_intTI_type_node : intTI_type_node; 1645#endif 1646 1647 if (mode == TYPE_MODE (float_type_node)) 1648 return float_type_node; 1649 1650 if (mode == TYPE_MODE (double_type_node)) 1651 return double_type_node; 1652 1653 if (mode == TYPE_MODE (long_double_type_node)) 1654 return long_double_type_node; 1655 1656 if (mode == TYPE_MODE (void_type_node)) 1657 return void_type_node; 1658 1659 if (mode == TYPE_MODE (build_pointer_type (char_type_node))) 1660 return (unsignedp 1661 ? make_unsigned_type (GET_MODE_PRECISION (mode)) 1662 : make_signed_type (GET_MODE_PRECISION (mode))); 1663 1664 if (mode == TYPE_MODE (build_pointer_type (integer_type_node))) 1665 return (unsignedp 1666 ? make_unsigned_type (GET_MODE_PRECISION (mode)) 1667 : make_signed_type (GET_MODE_PRECISION (mode))); 1668 1669 if (COMPLEX_MODE_P (mode)) 1670 { 1671 enum machine_mode inner_mode; 1672 tree inner_type; 1673 1674 if (mode == TYPE_MODE (complex_float_type_node)) 1675 return complex_float_type_node; 1676 if (mode == TYPE_MODE (complex_double_type_node)) 1677 return complex_double_type_node; 1678 if (mode == TYPE_MODE (complex_long_double_type_node)) 1679 return complex_long_double_type_node; 1680 1681 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp) 1682 return complex_integer_type_node; 1683 1684 inner_mode = GET_MODE_INNER (mode); 1685 inner_type = c_common_type_for_mode (inner_mode, unsignedp); 1686 if (inner_type != NULL_TREE) 1687 return build_complex_type (inner_type); 1688 } 1689 else if (VECTOR_MODE_P (mode)) 1690 { 1691 enum machine_mode inner_mode = GET_MODE_INNER (mode); 1692 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp); 1693 if (inner_type != NULL_TREE) 1694 return build_vector_type_for_mode (inner_type, mode); 1695 } 1696 1697 for (t = registered_builtin_types; t; t = TREE_CHAIN (t)) 1698 if (TYPE_MODE (TREE_VALUE (t)) == mode) 1699 return TREE_VALUE (t); 1700 1701 return 0; 1702} 1703 1704/* Return an unsigned type the same as TYPE in other respects. */ 1705tree 1706c_common_unsigned_type (tree type) 1707{ 1708 tree type1 = TYPE_MAIN_VARIANT (type); 1709 if (type1 == signed_char_type_node || type1 == char_type_node) 1710 return unsigned_char_type_node; 1711 if (type1 == integer_type_node) 1712 return unsigned_type_node; 1713 if (type1 == short_integer_type_node) 1714 return short_unsigned_type_node; 1715 if (type1 == long_integer_type_node) 1716 return long_unsigned_type_node; 1717 if (type1 == long_long_integer_type_node) 1718 return long_long_unsigned_type_node; 1719 if (type1 == widest_integer_literal_type_node) 1720 return widest_unsigned_literal_type_node; 1721#if HOST_BITS_PER_WIDE_INT >= 64 1722 if (type1 == intTI_type_node) 1723 return unsigned_intTI_type_node; 1724#endif 1725 if (type1 == intDI_type_node) 1726 return unsigned_intDI_type_node; 1727 if (type1 == intSI_type_node) 1728 return unsigned_intSI_type_node; 1729 if (type1 == intHI_type_node) 1730 return unsigned_intHI_type_node; 1731 if (type1 == intQI_type_node) 1732 return unsigned_intQI_type_node; 1733 1734 return c_common_signed_or_unsigned_type (1, type); 1735} 1736 1737/* Return a signed type the same as TYPE in other respects. */ 1738 1739tree 1740c_common_signed_type (tree type) 1741{ 1742 tree type1 = TYPE_MAIN_VARIANT (type); 1743 if (type1 == unsigned_char_type_node || type1 == char_type_node) 1744 return signed_char_type_node; 1745 if (type1 == unsigned_type_node) 1746 return integer_type_node; 1747 if (type1 == short_unsigned_type_node) 1748 return short_integer_type_node; 1749 if (type1 == long_unsigned_type_node) 1750 return long_integer_type_node; 1751 if (type1 == long_long_unsigned_type_node) 1752 return long_long_integer_type_node; 1753 if (type1 == widest_unsigned_literal_type_node) 1754 return widest_integer_literal_type_node; 1755#if HOST_BITS_PER_WIDE_INT >= 64 1756 if (type1 == unsigned_intTI_type_node) 1757 return intTI_type_node; 1758#endif 1759 if (type1 == unsigned_intDI_type_node) 1760 return intDI_type_node; 1761 if (type1 == unsigned_intSI_type_node) 1762 return intSI_type_node; 1763 if (type1 == unsigned_intHI_type_node) 1764 return intHI_type_node; 1765 if (type1 == unsigned_intQI_type_node) 1766 return intQI_type_node; 1767 1768 return c_common_signed_or_unsigned_type (0, type); 1769} 1770 1771/* Return a type the same as TYPE except unsigned or 1772 signed according to UNSIGNEDP. */ 1773 1774tree 1775c_common_signed_or_unsigned_type (int unsignedp, tree type) 1776{ 1777 if (!INTEGRAL_TYPE_P (type) 1778 || TYPE_UNSIGNED (type) == unsignedp) 1779 return type; 1780 1781 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not 1782 the precision; they have precision set to match their range, but 1783 may use a wider mode to match an ABI. If we change modes, we may 1784 wind up with bad conversions. For INTEGER_TYPEs in C, must check 1785 the precision as well, so as to yield correct results for 1786 bit-field types. C++ does not have these separate bit-field 1787 types, and producing a signed or unsigned variant of an 1788 ENUMERAL_TYPE may cause other problems as well. */ 1789 1790#define TYPE_OK(node) \ 1791 (TYPE_MODE (type) == TYPE_MODE (node) \ 1792 && (c_dialect_cxx () || TYPE_PRECISION (type) == TYPE_PRECISION (node))) 1793 if (TYPE_OK (signed_char_type_node)) 1794 return unsignedp ? unsigned_char_type_node : signed_char_type_node; 1795 if (TYPE_OK (integer_type_node)) 1796 return unsignedp ? unsigned_type_node : integer_type_node; 1797 if (TYPE_OK (short_integer_type_node)) 1798 return unsignedp ? short_unsigned_type_node : short_integer_type_node; 1799 if (TYPE_OK (long_integer_type_node)) 1800 return unsignedp ? long_unsigned_type_node : long_integer_type_node; 1801 if (TYPE_OK (long_long_integer_type_node)) 1802 return (unsignedp ? long_long_unsigned_type_node 1803 : long_long_integer_type_node); 1804 if (TYPE_OK (widest_integer_literal_type_node)) 1805 return (unsignedp ? widest_unsigned_literal_type_node 1806 : widest_integer_literal_type_node); 1807 1808#if HOST_BITS_PER_WIDE_INT >= 64 1809 if (TYPE_OK (intTI_type_node)) 1810 return unsignedp ? unsigned_intTI_type_node : intTI_type_node; 1811#endif 1812 if (TYPE_OK (intDI_type_node)) 1813 return unsignedp ? unsigned_intDI_type_node : intDI_type_node; 1814 if (TYPE_OK (intSI_type_node)) 1815 return unsignedp ? unsigned_intSI_type_node : intSI_type_node; 1816 if (TYPE_OK (intHI_type_node)) 1817 return unsignedp ? unsigned_intHI_type_node : intHI_type_node; 1818 if (TYPE_OK (intQI_type_node)) 1819 return unsignedp ? unsigned_intQI_type_node : intQI_type_node; 1820#undef TYPE_OK 1821 1822 if (c_dialect_cxx ()) 1823 return type; 1824 else 1825 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp); 1826} 1827 1828/* The C version of the register_builtin_type langhook. */ 1829 1830void 1831c_register_builtin_type (tree type, const char* name) 1832{ 1833 tree decl; 1834 1835 decl = build_decl (TYPE_DECL, get_identifier (name), type); 1836 DECL_ARTIFICIAL (decl) = 1; 1837 if (!TYPE_NAME (type)) 1838 TYPE_NAME (type) = decl; 1839 pushdecl (decl); 1840 1841 registered_builtin_types = tree_cons (0, type, registered_builtin_types); 1842} 1843 1844 1845/* Return the minimum number of bits needed to represent VALUE in a 1846 signed or unsigned type, UNSIGNEDP says which. */ 1847 1848unsigned int 1849min_precision (tree value, int unsignedp) 1850{ 1851 int log; 1852 1853 /* If the value is negative, compute its negative minus 1. The latter 1854 adjustment is because the absolute value of the largest negative value 1855 is one larger than the largest positive value. This is equivalent to 1856 a bit-wise negation, so use that operation instead. */ 1857 1858 if (tree_int_cst_sgn (value) < 0) 1859 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value); 1860 1861 /* Return the number of bits needed, taking into account the fact 1862 that we need one more bit for a signed than unsigned type. */ 1863 1864 if (integer_zerop (value)) 1865 log = 0; 1866 else 1867 log = tree_floor_log2 (value); 1868 1869 return log + 1 + !unsignedp; 1870} 1871 1872/* Print an error message for invalid operands to arith operation 1873 CODE. NOP_EXPR is used as a special case (see 1874 c_common_truthvalue_conversion). */ 1875 1876void 1877binary_op_error (enum tree_code code) 1878{ 1879 const char *opname; 1880 1881 switch (code) 1882 { 1883 case NOP_EXPR: 1884 error ("invalid truth-value expression"); 1885 return; 1886 1887 case PLUS_EXPR: 1888 opname = "+"; break; 1889 case MINUS_EXPR: 1890 opname = "-"; break; 1891 case MULT_EXPR: 1892 opname = "*"; break; 1893 case MAX_EXPR: 1894 opname = "max"; break; 1895 case MIN_EXPR: 1896 opname = "min"; break; 1897 case EQ_EXPR: 1898 opname = "=="; break; 1899 case NE_EXPR: 1900 opname = "!="; break; 1901 case LE_EXPR: 1902 opname = "<="; break; 1903 case GE_EXPR: 1904 opname = ">="; break; 1905 case LT_EXPR: 1906 opname = "<"; break; 1907 case GT_EXPR: 1908 opname = ">"; break; 1909 case LSHIFT_EXPR: 1910 opname = "<<"; break; 1911 case RSHIFT_EXPR: 1912 opname = ">>"; break; 1913 case TRUNC_MOD_EXPR: 1914 case FLOOR_MOD_EXPR: 1915 opname = "%"; break; 1916 case TRUNC_DIV_EXPR: 1917 case FLOOR_DIV_EXPR: 1918 opname = "/"; break; 1919 case BIT_AND_EXPR: 1920 opname = "&"; break; 1921 case BIT_IOR_EXPR: 1922 opname = "|"; break; 1923 case TRUTH_ANDIF_EXPR: 1924 opname = "&&"; break; 1925 case TRUTH_ORIF_EXPR: 1926 opname = "||"; break; 1927 case BIT_XOR_EXPR: 1928 opname = "^"; break; 1929 default: 1930 gcc_unreachable (); 1931 } 1932 error ("invalid operands to binary %s", opname); 1933} 1934 1935/* Subroutine of build_binary_op, used for comparison operations. 1936 See if the operands have both been converted from subword integer types 1937 and, if so, perhaps change them both back to their original type. 1938 This function is also responsible for converting the two operands 1939 to the proper common type for comparison. 1940 1941 The arguments of this function are all pointers to local variables 1942 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1, 1943 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE. 1944 1945 If this function returns nonzero, it means that the comparison has 1946 a constant value. What this function returns is an expression for 1947 that value. */ 1948 1949tree 1950shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr, 1951 enum tree_code *rescode_ptr) 1952{ 1953 tree type; 1954 tree op0 = *op0_ptr; 1955 tree op1 = *op1_ptr; 1956 int unsignedp0, unsignedp1; 1957 int real1, real2; 1958 tree primop0, primop1; 1959 enum tree_code code = *rescode_ptr; 1960 1961 /* Throw away any conversions to wider types 1962 already present in the operands. */ 1963 1964 primop0 = get_narrower (op0, &unsignedp0); 1965 primop1 = get_narrower (op1, &unsignedp1); 1966 1967 /* Handle the case that OP0 does not *contain* a conversion 1968 but it *requires* conversion to FINAL_TYPE. */ 1969 1970 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr) 1971 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0)); 1972 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr) 1973 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1)); 1974 1975 /* If one of the operands must be floated, we cannot optimize. */ 1976 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE; 1977 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE; 1978 1979 /* If first arg is constant, swap the args (changing operation 1980 so value is preserved), for canonicalization. Don't do this if 1981 the second arg is 0. */ 1982 1983 if (TREE_CONSTANT (primop0) 1984 && !integer_zerop (primop1) && !real_zerop (primop1)) 1985 { 1986 tree tem = primop0; 1987 int temi = unsignedp0; 1988 primop0 = primop1; 1989 primop1 = tem; 1990 tem = op0; 1991 op0 = op1; 1992 op1 = tem; 1993 *op0_ptr = op0; 1994 *op1_ptr = op1; 1995 unsignedp0 = unsignedp1; 1996 unsignedp1 = temi; 1997 temi = real1; 1998 real1 = real2; 1999 real2 = temi; 2000 2001 switch (code) 2002 { 2003 case LT_EXPR: 2004 code = GT_EXPR; 2005 break; 2006 case GT_EXPR: 2007 code = LT_EXPR; 2008 break; 2009 case LE_EXPR: 2010 code = GE_EXPR; 2011 break; 2012 case GE_EXPR: 2013 code = LE_EXPR; 2014 break; 2015 default: 2016 break; 2017 } 2018 *rescode_ptr = code; 2019 } 2020 2021 /* If comparing an integer against a constant more bits wide, 2022 maybe we can deduce a value of 1 or 0 independent of the data. 2023 Or else truncate the constant now 2024 rather than extend the variable at run time. 2025 2026 This is only interesting if the constant is the wider arg. 2027 Also, it is not safe if the constant is unsigned and the 2028 variable arg is signed, since in this case the variable 2029 would be sign-extended and then regarded as unsigned. 2030 Our technique fails in this case because the lowest/highest 2031 possible unsigned results don't follow naturally from the 2032 lowest/highest possible values of the variable operand. 2033 For just EQ_EXPR and NE_EXPR there is another technique that 2034 could be used: see if the constant can be faithfully represented 2035 in the other operand's type, by truncating it and reextending it 2036 and see if that preserves the constant's value. */ 2037 2038 if (!real1 && !real2 2039 && TREE_CODE (primop1) == INTEGER_CST 2040 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)) 2041 { 2042 int min_gt, max_gt, min_lt, max_lt; 2043 tree maxval, minval; 2044 /* 1 if comparison is nominally unsigned. */ 2045 int unsignedp = TYPE_UNSIGNED (*restype_ptr); 2046 tree val; 2047 2048 type = c_common_signed_or_unsigned_type (unsignedp0, 2049 TREE_TYPE (primop0)); 2050 2051 maxval = TYPE_MAX_VALUE (type); 2052 minval = TYPE_MIN_VALUE (type); 2053 2054 if (unsignedp && !unsignedp0) 2055 *restype_ptr = c_common_signed_type (*restype_ptr); 2056 2057 if (TREE_TYPE (primop1) != *restype_ptr) 2058 { 2059 /* Convert primop1 to target type, but do not introduce 2060 additional overflow. We know primop1 is an int_cst. */ 2061 tree tmp = build_int_cst_wide (*restype_ptr, 2062 TREE_INT_CST_LOW (primop1), 2063 TREE_INT_CST_HIGH (primop1)); 2064 2065 primop1 = force_fit_type (tmp, 0, TREE_OVERFLOW (primop1), 2066 TREE_CONSTANT_OVERFLOW (primop1)); 2067 } 2068 if (type != *restype_ptr) 2069 { 2070 minval = convert (*restype_ptr, minval); 2071 maxval = convert (*restype_ptr, maxval); 2072 } 2073 2074 if (unsignedp && unsignedp0) 2075 { 2076 min_gt = INT_CST_LT_UNSIGNED (primop1, minval); 2077 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval); 2078 min_lt = INT_CST_LT_UNSIGNED (minval, primop1); 2079 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1); 2080 } 2081 else 2082 { 2083 min_gt = INT_CST_LT (primop1, minval); 2084 max_gt = INT_CST_LT (primop1, maxval); 2085 min_lt = INT_CST_LT (minval, primop1); 2086 max_lt = INT_CST_LT (maxval, primop1); 2087 } 2088 2089 val = 0; 2090 /* This used to be a switch, but Genix compiler can't handle that. */ 2091 if (code == NE_EXPR) 2092 { 2093 if (max_lt || min_gt) 2094 val = truthvalue_true_node; 2095 } 2096 else if (code == EQ_EXPR) 2097 { 2098 if (max_lt || min_gt) 2099 val = truthvalue_false_node; 2100 } 2101 else if (code == LT_EXPR) 2102 { 2103 if (max_lt) 2104 val = truthvalue_true_node; 2105 if (!min_lt) 2106 val = truthvalue_false_node; 2107 } 2108 else if (code == GT_EXPR) 2109 { 2110 if (min_gt) 2111 val = truthvalue_true_node; 2112 if (!max_gt) 2113 val = truthvalue_false_node; 2114 } 2115 else if (code == LE_EXPR) 2116 { 2117 if (!max_gt) 2118 val = truthvalue_true_node; 2119 if (min_gt) 2120 val = truthvalue_false_node; 2121 } 2122 else if (code == GE_EXPR) 2123 { 2124 if (!min_lt) 2125 val = truthvalue_true_node; 2126 if (max_lt) 2127 val = truthvalue_false_node; 2128 } 2129 2130 /* If primop0 was sign-extended and unsigned comparison specd, 2131 we did a signed comparison above using the signed type bounds. 2132 But the comparison we output must be unsigned. 2133 2134 Also, for inequalities, VAL is no good; but if the signed 2135 comparison had *any* fixed result, it follows that the 2136 unsigned comparison just tests the sign in reverse 2137 (positive values are LE, negative ones GE). 2138 So we can generate an unsigned comparison 2139 against an extreme value of the signed type. */ 2140 2141 if (unsignedp && !unsignedp0) 2142 { 2143 if (val != 0) 2144 switch (code) 2145 { 2146 case LT_EXPR: 2147 case GE_EXPR: 2148 primop1 = TYPE_MIN_VALUE (type); 2149 val = 0; 2150 break; 2151 2152 case LE_EXPR: 2153 case GT_EXPR: 2154 primop1 = TYPE_MAX_VALUE (type); 2155 val = 0; 2156 break; 2157 2158 default: 2159 break; 2160 } 2161 type = c_common_unsigned_type (type); 2162 } 2163 2164 if (TREE_CODE (primop0) != INTEGER_CST) 2165 { 2166 if (val == truthvalue_false_node) 2167 warning (0, "comparison is always false due to limited range of data type"); 2168 if (val == truthvalue_true_node) 2169 warning (0, "comparison is always true due to limited range of data type"); 2170 } 2171 2172 if (val != 0) 2173 { 2174 /* Don't forget to evaluate PRIMOP0 if it has side effects. */ 2175 if (TREE_SIDE_EFFECTS (primop0)) 2176 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val); 2177 return val; 2178 } 2179 2180 /* Value is not predetermined, but do the comparison 2181 in the type of the operand that is not constant. 2182 TYPE is already properly set. */ 2183 } 2184 else if (real1 && real2 2185 && (TYPE_PRECISION (TREE_TYPE (primop0)) 2186 == TYPE_PRECISION (TREE_TYPE (primop1)))) 2187 type = TREE_TYPE (primop0); 2188 2189 /* If args' natural types are both narrower than nominal type 2190 and both extend in the same manner, compare them 2191 in the type of the wider arg. 2192 Otherwise must actually extend both to the nominal 2193 common type lest different ways of extending 2194 alter the result. 2195 (eg, (short)-1 == (unsigned short)-1 should be 0.) */ 2196 2197 else if (unsignedp0 == unsignedp1 && real1 == real2 2198 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr) 2199 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)) 2200 { 2201 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); 2202 type = c_common_signed_or_unsigned_type (unsignedp0 2203 || TYPE_UNSIGNED (*restype_ptr), 2204 type); 2205 /* Make sure shorter operand is extended the right way 2206 to match the longer operand. */ 2207 primop0 2208 = convert (c_common_signed_or_unsigned_type (unsignedp0, 2209 TREE_TYPE (primop0)), 2210 primop0); 2211 primop1 2212 = convert (c_common_signed_or_unsigned_type (unsignedp1, 2213 TREE_TYPE (primop1)), 2214 primop1); 2215 } 2216 else 2217 { 2218 /* Here we must do the comparison on the nominal type 2219 using the args exactly as we received them. */ 2220 type = *restype_ptr; 2221 primop0 = op0; 2222 primop1 = op1; 2223 2224 if (!real1 && !real2 && integer_zerop (primop1) 2225 && TYPE_UNSIGNED (*restype_ptr)) 2226 { 2227 tree value = 0; 2228 switch (code) 2229 { 2230 case GE_EXPR: 2231 /* All unsigned values are >= 0, so we warn if extra warnings 2232 are requested. However, if OP0 is a constant that is 2233 >= 0, the signedness of the comparison isn't an issue, 2234 so suppress the warning. */ 2235 if (extra_warnings && !in_system_header 2236 && !(TREE_CODE (primop0) == INTEGER_CST 2237 && !TREE_OVERFLOW (convert (c_common_signed_type (type), 2238 primop0)))) 2239 warning (0, "comparison of unsigned expression >= 0 is always true"); 2240 value = truthvalue_true_node; 2241 break; 2242 2243 case LT_EXPR: 2244 if (extra_warnings && !in_system_header 2245 && !(TREE_CODE (primop0) == INTEGER_CST 2246 && !TREE_OVERFLOW (convert (c_common_signed_type (type), 2247 primop0)))) 2248 warning (0, "comparison of unsigned expression < 0 is always false"); 2249 value = truthvalue_false_node; 2250 break; 2251 2252 default: 2253 break; 2254 } 2255 2256 if (value != 0) 2257 { 2258 /* Don't forget to evaluate PRIMOP0 if it has side effects. */ 2259 if (TREE_SIDE_EFFECTS (primop0)) 2260 return build2 (COMPOUND_EXPR, TREE_TYPE (value), 2261 primop0, value); 2262 return value; 2263 } 2264 } 2265 } 2266 2267 *op0_ptr = convert (type, primop0); 2268 *op1_ptr = convert (type, primop1); 2269 2270 *restype_ptr = truthvalue_type_node; 2271 2272 return 0; 2273} 2274 2275/* Return a tree for the sum or difference (RESULTCODE says which) 2276 of pointer PTROP and integer INTOP. */ 2277 2278tree 2279pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop) 2280{ 2281 tree size_exp; 2282 2283 /* The result is a pointer of the same type that is being added. */ 2284 2285 tree result_type = TREE_TYPE (ptrop); 2286 2287 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE) 2288 { 2289 if (pedantic || warn_pointer_arith) 2290 pedwarn ("pointer of type %<void *%> used in arithmetic"); 2291 size_exp = integer_one_node; 2292 } 2293 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE) 2294 { 2295 if (pedantic || warn_pointer_arith) 2296 pedwarn ("pointer to a function used in arithmetic"); 2297 size_exp = integer_one_node; 2298 } 2299 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE) 2300 { 2301 if (pedantic || warn_pointer_arith) 2302 pedwarn ("pointer to member function used in arithmetic"); 2303 size_exp = integer_one_node; 2304 } 2305 else 2306 size_exp = size_in_bytes (TREE_TYPE (result_type)); 2307 2308 /* If what we are about to multiply by the size of the elements 2309 contains a constant term, apply distributive law 2310 and multiply that constant term separately. 2311 This helps produce common subexpressions. */ 2312 2313 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR) 2314 && !TREE_CONSTANT (intop) 2315 && TREE_CONSTANT (TREE_OPERAND (intop, 1)) 2316 && TREE_CONSTANT (size_exp) 2317 /* If the constant comes from pointer subtraction, 2318 skip this optimization--it would cause an error. */ 2319 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE 2320 /* If the constant is unsigned, and smaller than the pointer size, 2321 then we must skip this optimization. This is because it could cause 2322 an overflow error if the constant is negative but INTOP is not. */ 2323 && (!TYPE_UNSIGNED (TREE_TYPE (intop)) 2324 || (TYPE_PRECISION (TREE_TYPE (intop)) 2325 == TYPE_PRECISION (TREE_TYPE (ptrop))))) 2326 { 2327 enum tree_code subcode = resultcode; 2328 tree int_type = TREE_TYPE (intop); 2329 if (TREE_CODE (intop) == MINUS_EXPR) 2330 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR); 2331 /* Convert both subexpression types to the type of intop, 2332 because weird cases involving pointer arithmetic 2333 can result in a sum or difference with different type args. */ 2334 ptrop = build_binary_op (subcode, ptrop, 2335 convert (int_type, TREE_OPERAND (intop, 1)), 1); 2336 intop = convert (int_type, TREE_OPERAND (intop, 0)); 2337 } 2338 2339 /* Convert the integer argument to a type the same size as sizetype 2340 so the multiply won't overflow spuriously. */ 2341 2342 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype) 2343 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype)) 2344 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype), 2345 TYPE_UNSIGNED (sizetype)), intop); 2346 2347 /* Replace the integer argument with a suitable product by the object size. 2348 Do this multiplication as signed, then convert to the appropriate 2349 pointer type (actually unsigned integral). */ 2350 2351 intop = convert (result_type, 2352 build_binary_op (MULT_EXPR, intop, 2353 convert (TREE_TYPE (intop), size_exp), 1)); 2354 2355 /* Create the sum or difference. */ 2356 return fold_build2 (resultcode, result_type, ptrop, intop); 2357} 2358 2359/* Prepare expr to be an argument of a TRUTH_NOT_EXPR, 2360 or for an `if' or `while' statement or ?..: exp. It should already 2361 have been validated to be of suitable type; otherwise, a bad 2362 diagnostic may result. 2363 2364 This preparation consists of taking the ordinary 2365 representation of an expression expr and producing a valid tree 2366 boolean expression describing whether expr is nonzero. We could 2367 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1), 2368 but we optimize comparisons, &&, ||, and !. 2369 2370 The resulting type should always be `truthvalue_type_node'. */ 2371 2372tree 2373c_common_truthvalue_conversion (tree expr) 2374{ 2375 switch (TREE_CODE (expr)) 2376 { 2377 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR: 2378 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR: 2379 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR: 2380 case ORDERED_EXPR: case UNORDERED_EXPR: 2381 if (TREE_TYPE (expr) == truthvalue_type_node) 2382 return expr; 2383 return build2 (TREE_CODE (expr), truthvalue_type_node, 2384 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1)); 2385 2386 case TRUTH_ANDIF_EXPR: 2387 case TRUTH_ORIF_EXPR: 2388 case TRUTH_AND_EXPR: 2389 case TRUTH_OR_EXPR: 2390 case TRUTH_XOR_EXPR: 2391 if (TREE_TYPE (expr) == truthvalue_type_node) 2392 return expr; 2393 return build2 (TREE_CODE (expr), truthvalue_type_node, 2394 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)), 2395 c_common_truthvalue_conversion (TREE_OPERAND (expr, 1))); 2396 2397 case TRUTH_NOT_EXPR: 2398 if (TREE_TYPE (expr) == truthvalue_type_node) 2399 return expr; 2400 return build1 (TREE_CODE (expr), truthvalue_type_node, 2401 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0))); 2402 2403 case ERROR_MARK: 2404 return expr; 2405 2406 case INTEGER_CST: 2407 /* Avoid integer_zerop to ignore TREE_CONSTANT_OVERFLOW. */ 2408 return (TREE_INT_CST_LOW (expr) != 0 || TREE_INT_CST_HIGH (expr) != 0) 2409 ? truthvalue_true_node 2410 : truthvalue_false_node; 2411 2412 case REAL_CST: 2413 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0) 2414 ? truthvalue_true_node 2415 : truthvalue_false_node; 2416 2417 case FUNCTION_DECL: 2418 expr = build_unary_op (ADDR_EXPR, expr, 0); 2419 /* Fall through. */ 2420 2421 case ADDR_EXPR: 2422 { 2423 if (TREE_CODE (TREE_OPERAND (expr, 0)) == FUNCTION_DECL 2424 && !DECL_WEAK (TREE_OPERAND (expr, 0))) 2425 { 2426 /* Common Ada/Pascal programmer's mistake. We always warn 2427 about this since it is so bad. */ 2428 warning (0, "the address of %qD, will always evaluate as %<true%>", 2429 TREE_OPERAND (expr, 0)); 2430 return truthvalue_true_node; 2431 } 2432 2433 /* If we are taking the address of an external decl, it might be 2434 zero if it is weak, so we cannot optimize. */ 2435 if (DECL_P (TREE_OPERAND (expr, 0)) 2436 && DECL_EXTERNAL (TREE_OPERAND (expr, 0))) 2437 break; 2438 2439 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0))) 2440 return build2 (COMPOUND_EXPR, truthvalue_type_node, 2441 TREE_OPERAND (expr, 0), truthvalue_true_node); 2442 else 2443 return truthvalue_true_node; 2444 } 2445 2446 case COMPLEX_EXPR: 2447 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)) 2448 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), 2449 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)), 2450 c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)), 2451 0); 2452 2453 case NEGATE_EXPR: 2454 case ABS_EXPR: 2455 case FLOAT_EXPR: 2456 /* These don't change whether an object is nonzero or zero. */ 2457 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)); 2458 2459 case LROTATE_EXPR: 2460 case RROTATE_EXPR: 2461 /* These don't change whether an object is zero or nonzero, but 2462 we can't ignore them if their second arg has side-effects. */ 2463 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))) 2464 return build2 (COMPOUND_EXPR, truthvalue_type_node, 2465 TREE_OPERAND (expr, 1), 2466 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0))); 2467 else 2468 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)); 2469 2470 case COND_EXPR: 2471 /* Distribute the conversion into the arms of a COND_EXPR. */ 2472 return fold_build3 (COND_EXPR, truthvalue_type_node, 2473 TREE_OPERAND (expr, 0), 2474 c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)), 2475 c_common_truthvalue_conversion (TREE_OPERAND (expr, 2))); 2476 2477 case CONVERT_EXPR: 2478 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE, 2479 since that affects how `default_conversion' will behave. */ 2480 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE 2481 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE) 2482 break; 2483 /* Fall through.... */ 2484 case NOP_EXPR: 2485 /* If this is widening the argument, we can ignore it. */ 2486 if (TYPE_PRECISION (TREE_TYPE (expr)) 2487 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0)))) 2488 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)); 2489 break; 2490 2491 case MINUS_EXPR: 2492 /* Perhaps reduce (x - y) != 0 to (x != y). The expressions 2493 aren't guaranteed to the be same for modes that can represent 2494 infinity, since if x and y are both +infinity, or both 2495 -infinity, then x - y is not a number. 2496 2497 Note that this transformation is safe when x or y is NaN. 2498 (x - y) is then NaN, and both (x - y) != 0 and x != y will 2499 be false. */ 2500 if (HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))) 2501 break; 2502 /* Fall through.... */ 2503 case BIT_XOR_EXPR: 2504 /* This and MINUS_EXPR can be changed into a comparison of the 2505 two objects. */ 2506 if (TREE_TYPE (TREE_OPERAND (expr, 0)) 2507 == TREE_TYPE (TREE_OPERAND (expr, 1))) 2508 return fold_build2 (NE_EXPR, truthvalue_type_node, 2509 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1)); 2510 return fold_build2 (NE_EXPR, truthvalue_type_node, 2511 TREE_OPERAND (expr, 0), 2512 fold_convert (TREE_TYPE (TREE_OPERAND (expr, 0)), 2513 TREE_OPERAND (expr, 1))); 2514 2515 case BIT_AND_EXPR: 2516 if (integer_onep (TREE_OPERAND (expr, 1)) 2517 && TREE_TYPE (expr) != truthvalue_type_node) 2518 /* Using convert here would cause infinite recursion. */ 2519 return build1 (NOP_EXPR, truthvalue_type_node, expr); 2520 break; 2521 2522 case MODIFY_EXPR: 2523 if (!TREE_NO_WARNING (expr)) 2524 warning (OPT_Wparentheses, 2525 "suggest parentheses around assignment used as truth value"); 2526 break; 2527 2528 default: 2529 break; 2530 } 2531 2532 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE) 2533 { 2534 tree t = save_expr (expr); 2535 return (build_binary_op 2536 ((TREE_SIDE_EFFECTS (expr) 2537 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), 2538 c_common_truthvalue_conversion (build_unary_op (REALPART_EXPR, t, 0)), 2539 c_common_truthvalue_conversion (build_unary_op (IMAGPART_EXPR, t, 0)), 2540 0)); 2541 } 2542 2543 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1); 2544} 2545 2546static void def_builtin_1 (enum built_in_function fncode, 2547 const char *name, 2548 enum built_in_class fnclass, 2549 tree fntype, tree libtype, 2550 bool both_p, bool fallback_p, bool nonansi_p, 2551 tree fnattrs, bool implicit_p); 2552 2553/* Make a variant type in the proper way for C/C++, propagating qualifiers 2554 down to the element type of an array. */ 2555 2556tree 2557c_build_qualified_type (tree type, int type_quals) 2558{ 2559 if (type == error_mark_node) 2560 return type; 2561 2562 if (TREE_CODE (type) == ARRAY_TYPE) 2563 { 2564 tree t; 2565 tree element_type = c_build_qualified_type (TREE_TYPE (type), 2566 type_quals); 2567 2568 /* See if we already have an identically qualified type. */ 2569 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) 2570 { 2571 if (TYPE_QUALS (strip_array_types (t)) == type_quals 2572 && TYPE_NAME (t) == TYPE_NAME (type) 2573 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type) 2574 && attribute_list_equal (TYPE_ATTRIBUTES (t), 2575 TYPE_ATTRIBUTES (type))) 2576 break; 2577 } 2578 if (!t) 2579 { 2580 t = build_variant_type_copy (type); 2581 TREE_TYPE (t) = element_type; 2582 } 2583 return t; 2584 } 2585 2586 /* A restrict-qualified pointer type must be a pointer to object or 2587 incomplete type. Note that the use of POINTER_TYPE_P also allows 2588 REFERENCE_TYPEs, which is appropriate for C++. */ 2589 if ((type_quals & TYPE_QUAL_RESTRICT) 2590 && (!POINTER_TYPE_P (type) 2591 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) 2592 { 2593 error ("invalid use of %<restrict%>"); 2594 type_quals &= ~TYPE_QUAL_RESTRICT; 2595 } 2596 2597 return build_qualified_type (type, type_quals); 2598} 2599 2600/* Apply the TYPE_QUALS to the new DECL. */ 2601 2602void 2603c_apply_type_quals_to_decl (int type_quals, tree decl) 2604{ 2605 tree type = TREE_TYPE (decl); 2606 2607 if (type == error_mark_node) 2608 return; 2609 2610 if (((type_quals & TYPE_QUAL_CONST) 2611 || (type && TREE_CODE (type) == REFERENCE_TYPE)) 2612 /* An object declared 'const' is only readonly after it is 2613 initialized. We don't have any way of expressing this currently, 2614 so we need to be conservative and unset TREE_READONLY for types 2615 with constructors. Otherwise aliasing code will ignore stores in 2616 an inline constructor. */ 2617 && !(type && TYPE_NEEDS_CONSTRUCTING (type))) 2618 TREE_READONLY (decl) = 1; 2619 if (type_quals & TYPE_QUAL_VOLATILE) 2620 { 2621 TREE_SIDE_EFFECTS (decl) = 1; 2622 TREE_THIS_VOLATILE (decl) = 1; 2623 } 2624 if (type_quals & TYPE_QUAL_RESTRICT) 2625 { 2626 while (type && TREE_CODE (type) == ARRAY_TYPE) 2627 /* Allow 'restrict' on arrays of pointers. 2628 FIXME currently we just ignore it. */ 2629 type = TREE_TYPE (type); 2630 if (!type 2631 || !POINTER_TYPE_P (type) 2632 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))) 2633 error ("invalid use of %<restrict%>"); 2634 else if (flag_strict_aliasing && type == TREE_TYPE (decl)) 2635 /* Indicate we need to make a unique alias set for this pointer. 2636 We can't do it here because it might be pointing to an 2637 incomplete type. */ 2638 DECL_POINTER_ALIAS_SET (decl) = -2; 2639 } 2640} 2641 2642/* Hash function for the problem of multiple type definitions in 2643 different files. This must hash all types that will compare 2644 equal via comptypes to the same value. In practice it hashes 2645 on some of the simple stuff and leaves the details to comptypes. */ 2646 2647static hashval_t 2648c_type_hash (const void *p) 2649{ 2650 int i = 0; 2651 int shift, size; 2652 tree t = (tree) p; 2653 tree t2; 2654 switch (TREE_CODE (t)) 2655 { 2656 /* For pointers, hash on pointee type plus some swizzling. */ 2657 case POINTER_TYPE: 2658 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003; 2659 /* Hash on number of elements and total size. */ 2660 case ENUMERAL_TYPE: 2661 shift = 3; 2662 t2 = TYPE_VALUES (t); 2663 break; 2664 case RECORD_TYPE: 2665 shift = 0; 2666 t2 = TYPE_FIELDS (t); 2667 break; 2668 case QUAL_UNION_TYPE: 2669 shift = 1; 2670 t2 = TYPE_FIELDS (t); 2671 break; 2672 case UNION_TYPE: 2673 shift = 2; 2674 t2 = TYPE_FIELDS (t); 2675 break; 2676 default: 2677 gcc_unreachable (); 2678 } 2679 for (; t2; t2 = TREE_CHAIN (t2)) 2680 i++; 2681 size = TREE_INT_CST_LOW (TYPE_SIZE (t)); 2682 return ((size << 24) | (i << shift)); 2683} 2684 2685static GTY((param_is (union tree_node))) htab_t type_hash_table; 2686 2687/* Return the typed-based alias set for T, which may be an expression 2688 or a type. Return -1 if we don't do anything special. */ 2689 2690HOST_WIDE_INT 2691c_common_get_alias_set (tree t) 2692{ 2693 tree u; 2694 PTR *slot; 2695 2696 /* Permit type-punning when accessing a union, provided the access 2697 is directly through the union. For example, this code does not 2698 permit taking the address of a union member and then storing 2699 through it. Even the type-punning allowed here is a GCC 2700 extension, albeit a common and useful one; the C standard says 2701 that such accesses have implementation-defined behavior. */ 2702 for (u = t; 2703 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF; 2704 u = TREE_OPERAND (u, 0)) 2705 if (TREE_CODE (u) == COMPONENT_REF 2706 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE) 2707 return 0; 2708 2709 /* That's all the expressions we handle specially. */ 2710 if (!TYPE_P (t)) 2711 return -1; 2712 2713 /* The C standard guarantees that any object may be accessed via an 2714 lvalue that has character type. */ 2715 if (t == char_type_node 2716 || t == signed_char_type_node 2717 || t == unsigned_char_type_node) 2718 return 0; 2719 2720 /* If it has the may_alias attribute, it can alias anything. */ 2721 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t))) 2722 return 0; 2723 2724 /* The C standard specifically allows aliasing between signed and 2725 unsigned variants of the same type. We treat the signed 2726 variant as canonical. */ 2727 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t)) 2728 { 2729 tree t1 = c_common_signed_type (t); 2730 2731 /* t1 == t can happen for boolean nodes which are always unsigned. */ 2732 if (t1 != t) 2733 return get_alias_set (t1); 2734 } 2735 else if (POINTER_TYPE_P (t)) 2736 { 2737 tree t1; 2738 2739 /* Unfortunately, there is no canonical form of a pointer type. 2740 In particular, if we have `typedef int I', then `int *', and 2741 `I *' are different types. So, we have to pick a canonical 2742 representative. We do this below. 2743 2744 Technically, this approach is actually more conservative that 2745 it needs to be. In particular, `const int *' and `int *' 2746 should be in different alias sets, according to the C and C++ 2747 standard, since their types are not the same, and so, 2748 technically, an `int **' and `const int **' cannot point at 2749 the same thing. 2750 2751 But, the standard is wrong. In particular, this code is 2752 legal C++: 2753 2754 int *ip; 2755 int **ipp = &ip; 2756 const int* const* cipp = ipp; 2757 2758 And, it doesn't make sense for that to be legal unless you 2759 can dereference IPP and CIPP. So, we ignore cv-qualifiers on 2760 the pointed-to types. This issue has been reported to the 2761 C++ committee. */ 2762 t1 = build_type_no_quals (t); 2763 if (t1 != t) 2764 return get_alias_set (t1); 2765 } 2766 2767 /* Handle the case of multiple type nodes referring to "the same" type, 2768 which occurs with IMA. These share an alias set. FIXME: Currently only 2769 C90 is handled. (In C99 type compatibility is not transitive, which 2770 complicates things mightily. The alias set splay trees can theoretically 2771 represent this, but insertion is tricky when you consider all the 2772 different orders things might arrive in.) */ 2773 2774 if (c_language != clk_c || flag_isoc99) 2775 return -1; 2776 2777 /* Save time if there's only one input file. */ 2778 if (num_in_fnames == 1) 2779 return -1; 2780 2781 /* Pointers need special handling if they point to any type that 2782 needs special handling (below). */ 2783 if (TREE_CODE (t) == POINTER_TYPE) 2784 { 2785 tree t2; 2786 /* Find bottom type under any nested POINTERs. */ 2787 for (t2 = TREE_TYPE (t); 2788 TREE_CODE (t2) == POINTER_TYPE; 2789 t2 = TREE_TYPE (t2)) 2790 ; 2791 if (TREE_CODE (t2) != RECORD_TYPE 2792 && TREE_CODE (t2) != ENUMERAL_TYPE 2793 && TREE_CODE (t2) != QUAL_UNION_TYPE 2794 && TREE_CODE (t2) != UNION_TYPE) 2795 return -1; 2796 if (TYPE_SIZE (t2) == 0) 2797 return -1; 2798 } 2799 /* These are the only cases that need special handling. */ 2800 if (TREE_CODE (t) != RECORD_TYPE 2801 && TREE_CODE (t) != ENUMERAL_TYPE 2802 && TREE_CODE (t) != QUAL_UNION_TYPE 2803 && TREE_CODE (t) != UNION_TYPE 2804 && TREE_CODE (t) != POINTER_TYPE) 2805 return -1; 2806 /* Undefined? */ 2807 if (TYPE_SIZE (t) == 0) 2808 return -1; 2809 2810 /* Look up t in hash table. Only one of the compatible types within each 2811 alias set is recorded in the table. */ 2812 if (!type_hash_table) 2813 type_hash_table = htab_create_ggc (1021, c_type_hash, 2814 (htab_eq) lang_hooks.types_compatible_p, 2815 NULL); 2816 slot = htab_find_slot (type_hash_table, t, INSERT); 2817 if (*slot != NULL) 2818 { 2819 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot); 2820 return TYPE_ALIAS_SET ((tree)*slot); 2821 } 2822 else 2823 /* Our caller will assign and record (in t) a new alias set; all we need 2824 to do is remember t in the hash table. */ 2825 *slot = t; 2826 2827 return -1; 2828} 2829 2830/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where the 2831 second parameter indicates which OPERATOR is being applied. The COMPLAIN 2832 flag controls whether we should diagnose possibly ill-formed 2833 constructs or not. */ 2834 2835tree 2836c_sizeof_or_alignof_type (tree type, bool is_sizeof, int complain) 2837{ 2838 const char *op_name; 2839 tree value = NULL; 2840 enum tree_code type_code = TREE_CODE (type); 2841 2842 op_name = is_sizeof ? "sizeof" : "__alignof__"; 2843 2844 if (type_code == FUNCTION_TYPE) 2845 { 2846 if (is_sizeof) 2847 { 2848 if (complain && (pedantic || warn_pointer_arith)) 2849 pedwarn ("invalid application of %<sizeof%> to a function type"); 2850 value = size_one_node; 2851 } 2852 else 2853 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); 2854 } 2855 else if (type_code == VOID_TYPE || type_code == ERROR_MARK) 2856 { 2857 if (type_code == VOID_TYPE 2858 && complain && (pedantic || warn_pointer_arith)) 2859 pedwarn ("invalid application of %qs to a void type", op_name); 2860 value = size_one_node; 2861 } 2862 else if (!COMPLETE_TYPE_P (type)) 2863 { 2864 if (complain) 2865 error ("invalid application of %qs to incomplete type %qT ", 2866 op_name, type); 2867 value = size_zero_node; 2868 } 2869 else 2870 { 2871 if (is_sizeof) 2872 /* Convert in case a char is more than one unit. */ 2873 value = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), 2874 size_int (TYPE_PRECISION (char_type_node) 2875 / BITS_PER_UNIT)); 2876 else 2877 value = size_int (TYPE_ALIGN_UNIT (type)); 2878 } 2879 2880 /* VALUE will have an integer type with TYPE_IS_SIZETYPE set. 2881 TYPE_IS_SIZETYPE means that certain things (like overflow) will 2882 never happen. However, this node should really have type 2883 `size_t', which is just a typedef for an ordinary integer type. */ 2884 value = fold_build1 (NOP_EXPR, size_type_node, value); 2885 gcc_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value))); 2886 2887 return value; 2888} 2889 2890/* Implement the __alignof keyword: Return the minimum required 2891 alignment of EXPR, measured in bytes. For VAR_DECL's and 2892 FIELD_DECL's return DECL_ALIGN (which can be set from an 2893 "aligned" __attribute__ specification). */ 2894 2895tree 2896c_alignof_expr (tree expr) 2897{ 2898 tree t; 2899 2900 if (TREE_CODE (expr) == VAR_DECL) 2901 t = size_int (DECL_ALIGN_UNIT (expr)); 2902 2903 else if (TREE_CODE (expr) == COMPONENT_REF 2904 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) 2905 { 2906 error ("%<__alignof%> applied to a bit-field"); 2907 t = size_one_node; 2908 } 2909 else if (TREE_CODE (expr) == COMPONENT_REF 2910 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL) 2911 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1))); 2912 2913 else if (TREE_CODE (expr) == INDIRECT_REF) 2914 { 2915 tree t = TREE_OPERAND (expr, 0); 2916 tree best = t; 2917 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); 2918 2919 while (TREE_CODE (t) == NOP_EXPR 2920 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE) 2921 { 2922 int thisalign; 2923 2924 t = TREE_OPERAND (t, 0); 2925 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); 2926 if (thisalign > bestalign) 2927 best = t, bestalign = thisalign; 2928 } 2929 return c_alignof (TREE_TYPE (TREE_TYPE (best))); 2930 } 2931 else 2932 return c_alignof (TREE_TYPE (expr)); 2933 2934 return fold_build1 (NOP_EXPR, size_type_node, t); 2935} 2936 2937/* Handle C and C++ default attributes. */ 2938 2939enum built_in_attribute 2940{ 2941#define DEF_ATTR_NULL_TREE(ENUM) ENUM, 2942#define DEF_ATTR_INT(ENUM, VALUE) ENUM, 2943#define DEF_ATTR_IDENT(ENUM, STRING) ENUM, 2944#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM, 2945#include "builtin-attrs.def" 2946#undef DEF_ATTR_NULL_TREE 2947#undef DEF_ATTR_INT 2948#undef DEF_ATTR_IDENT 2949#undef DEF_ATTR_TREE_LIST 2950 ATTR_LAST 2951}; 2952 2953static GTY(()) tree built_in_attributes[(int) ATTR_LAST]; 2954 2955static void c_init_attributes (void); 2956 2957/* Build tree nodes and builtin functions common to both C and C++ language 2958 frontends. */ 2959 2960void 2961c_common_nodes_and_builtins (void) 2962{ 2963 enum builtin_type 2964 { 2965#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME, 2966#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME, 2967#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME, 2968#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME, 2969#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, 2970#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, 2971#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME, 2972#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) \ 2973 NAME, 2974#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME, 2975#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME, 2976#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME, 2977#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, 2978#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, 2979#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \ 2980 NAME, 2981#define DEF_POINTER_TYPE(NAME, TYPE) NAME, 2982#include "builtin-types.def" 2983#undef DEF_PRIMITIVE_TYPE 2984#undef DEF_FUNCTION_TYPE_0 2985#undef DEF_FUNCTION_TYPE_1 2986#undef DEF_FUNCTION_TYPE_2 2987#undef DEF_FUNCTION_TYPE_3 2988#undef DEF_FUNCTION_TYPE_4 2989#undef DEF_FUNCTION_TYPE_5 2990#undef DEF_FUNCTION_TYPE_6 2991#undef DEF_FUNCTION_TYPE_VAR_0 2992#undef DEF_FUNCTION_TYPE_VAR_1 2993#undef DEF_FUNCTION_TYPE_VAR_2 2994#undef DEF_FUNCTION_TYPE_VAR_3 2995#undef DEF_FUNCTION_TYPE_VAR_4 2996#undef DEF_FUNCTION_TYPE_VAR_5 2997#undef DEF_POINTER_TYPE 2998 BT_LAST 2999 }; 3000 3001 typedef enum builtin_type builtin_type; 3002 3003 tree builtin_types[(int) BT_LAST + 1]; 3004 int wchar_type_size; 3005 tree array_domain_type; 3006 tree va_list_ref_type_node; 3007 tree va_list_arg_type_node; 3008 3009 /* Define `int' and `char' first so that dbx will output them first. */ 3010 record_builtin_type (RID_INT, NULL, integer_type_node); 3011 record_builtin_type (RID_CHAR, "char", char_type_node); 3012 3013 /* `signed' is the same as `int'. FIXME: the declarations of "signed", 3014 "unsigned long", "long long unsigned" and "unsigned short" were in C++ 3015 but not C. Are the conditionals here needed? */ 3016 if (c_dialect_cxx ()) 3017 record_builtin_type (RID_SIGNED, NULL, integer_type_node); 3018 record_builtin_type (RID_LONG, "long int", long_integer_type_node); 3019 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node); 3020 record_builtin_type (RID_MAX, "long unsigned int", 3021 long_unsigned_type_node); 3022 if (c_dialect_cxx ()) 3023 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node); 3024 record_builtin_type (RID_MAX, "long long int", 3025 long_long_integer_type_node); 3026 record_builtin_type (RID_MAX, "long long unsigned int", 3027 long_long_unsigned_type_node); 3028 if (c_dialect_cxx ()) 3029 record_builtin_type (RID_MAX, "long long unsigned", 3030 long_long_unsigned_type_node); 3031 record_builtin_type (RID_SHORT, "short int", short_integer_type_node); 3032 record_builtin_type (RID_MAX, "short unsigned int", 3033 short_unsigned_type_node); 3034 if (c_dialect_cxx ()) 3035 record_builtin_type (RID_MAX, "unsigned short", 3036 short_unsigned_type_node); 3037 3038 /* Define both `signed char' and `unsigned char'. */ 3039 record_builtin_type (RID_MAX, "signed char", signed_char_type_node); 3040 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node); 3041 3042 /* These are types that c_common_type_for_size and 3043 c_common_type_for_mode use. */ 3044 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, 3045 intQI_type_node)); 3046 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, 3047 intHI_type_node)); 3048 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, 3049 intSI_type_node)); 3050 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, 3051 intDI_type_node)); 3052#if HOST_BITS_PER_WIDE_INT >= 64 3053 if (targetm.scalar_mode_supported_p (TImode)) 3054 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, 3055 get_identifier ("__int128_t"), 3056 intTI_type_node)); 3057#endif 3058 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, 3059 unsigned_intQI_type_node)); 3060 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, 3061 unsigned_intHI_type_node)); 3062 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, 3063 unsigned_intSI_type_node)); 3064 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, 3065 unsigned_intDI_type_node)); 3066#if HOST_BITS_PER_WIDE_INT >= 64 3067 if (targetm.scalar_mode_supported_p (TImode)) 3068 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, 3069 get_identifier ("__uint128_t"), 3070 unsigned_intTI_type_node)); 3071#endif 3072 3073 /* Create the widest literal types. */ 3074 widest_integer_literal_type_node 3075 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2); 3076 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, 3077 widest_integer_literal_type_node)); 3078 3079 widest_unsigned_literal_type_node 3080 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2); 3081 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE, 3082 widest_unsigned_literal_type_node)); 3083 3084 /* `unsigned long' is the standard type for sizeof. 3085 Note that stddef.h uses `unsigned long', 3086 and this must agree, even if long and int are the same size. */ 3087 size_type_node = 3088 TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE))); 3089 signed_size_type_node = c_common_signed_type (size_type_node); 3090 set_sizetype (size_type_node); 3091 3092 pid_type_node = 3093 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE))); 3094 3095 build_common_tree_nodes_2 (flag_short_double); 3096 3097 record_builtin_type (RID_FLOAT, NULL, float_type_node); 3098 record_builtin_type (RID_DOUBLE, NULL, double_type_node); 3099 record_builtin_type (RID_MAX, "long double", long_double_type_node); 3100 3101 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, 3102 get_identifier ("complex int"), 3103 complex_integer_type_node)); 3104 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, 3105 get_identifier ("complex float"), 3106 complex_float_type_node)); 3107 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, 3108 get_identifier ("complex double"), 3109 complex_double_type_node)); 3110 lang_hooks.decls.pushdecl 3111 (build_decl (TYPE_DECL, get_identifier ("complex long double"), 3112 complex_long_double_type_node)); 3113 3114 if (c_dialect_cxx ()) 3115 /* For C++, make fileptr_type_node a distinct void * type until 3116 FILE type is defined. */ 3117 fileptr_type_node = build_variant_type_copy (ptr_type_node); 3118 3119 record_builtin_type (RID_VOID, NULL, void_type_node); 3120 3121 /* This node must not be shared. */ 3122 void_zero_node = make_node (INTEGER_CST); 3123 TREE_TYPE (void_zero_node) = void_type_node; 3124 3125 void_list_node = build_void_list_node (); 3126 3127 /* Make a type to be the domain of a few array types 3128 whose domains don't really matter. 3129 200 is small enough that it always fits in size_t 3130 and large enough that it can hold most function names for the 3131 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */ 3132 array_domain_type = build_index_type (size_int (200)); 3133 3134 /* Make a type for arrays of characters. 3135 With luck nothing will ever really depend on the length of this 3136 array type. */ 3137 char_array_type_node 3138 = build_array_type (char_type_node, array_domain_type); 3139 3140 /* Likewise for arrays of ints. */ 3141 int_array_type_node 3142 = build_array_type (integer_type_node, array_domain_type); 3143 3144 string_type_node = build_pointer_type (char_type_node); 3145 const_string_type_node 3146 = build_pointer_type (build_qualified_type 3147 (char_type_node, TYPE_QUAL_CONST)); 3148 3149 /* This is special for C++ so functions can be overloaded. */ 3150 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE); 3151 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node)); 3152 wchar_type_size = TYPE_PRECISION (wchar_type_node); 3153 if (c_dialect_cxx ()) 3154 { 3155 if (TYPE_UNSIGNED (wchar_type_node)) 3156 wchar_type_node = make_unsigned_type (wchar_type_size); 3157 else 3158 wchar_type_node = make_signed_type (wchar_type_size); 3159 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node); 3160 } 3161 else 3162 { 3163 signed_wchar_type_node = c_common_signed_type (wchar_type_node); 3164 unsigned_wchar_type_node = c_common_unsigned_type (wchar_type_node); 3165 } 3166 3167 /* This is for wide string constants. */ 3168 wchar_array_type_node 3169 = build_array_type (wchar_type_node, array_domain_type); 3170 3171 wint_type_node = 3172 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE))); 3173 3174 intmax_type_node = 3175 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE))); 3176 uintmax_type_node = 3177 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE))); 3178 3179 default_function_type = build_function_type (integer_type_node, NULL_TREE); 3180 ptrdiff_type_node 3181 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE))); 3182 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node); 3183 3184 lang_hooks.decls.pushdecl 3185 (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"), 3186 va_list_type_node)); 3187 3188 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) 3189 { 3190 va_list_arg_type_node = va_list_ref_type_node = 3191 build_pointer_type (TREE_TYPE (va_list_type_node)); 3192 } 3193 else 3194 { 3195 va_list_arg_type_node = va_list_type_node; 3196 va_list_ref_type_node = build_reference_type (va_list_type_node); 3197 } 3198 3199#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \ 3200 builtin_types[(int) ENUM] = VALUE; 3201#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \ 3202 builtin_types[(int) ENUM] \ 3203 = build_function_type (builtin_types[(int) RETURN], \ 3204 void_list_node); 3205#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \ 3206 builtin_types[(int) ENUM] \ 3207 = build_function_type (builtin_types[(int) RETURN], \ 3208 tree_cons (NULL_TREE, \ 3209 builtin_types[(int) ARG1], \ 3210 void_list_node)); 3211#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \ 3212 builtin_types[(int) ENUM] \ 3213 = build_function_type \ 3214 (builtin_types[(int) RETURN], \ 3215 tree_cons (NULL_TREE, \ 3216 builtin_types[(int) ARG1], \ 3217 tree_cons (NULL_TREE, \ 3218 builtin_types[(int) ARG2], \ 3219 void_list_node))); 3220#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ 3221 builtin_types[(int) ENUM] \ 3222 = build_function_type \ 3223 (builtin_types[(int) RETURN], \ 3224 tree_cons (NULL_TREE, \ 3225 builtin_types[(int) ARG1], \ 3226 tree_cons (NULL_TREE, \ 3227 builtin_types[(int) ARG2], \ 3228 tree_cons (NULL_TREE, \ 3229 builtin_types[(int) ARG3], \ 3230 void_list_node)))); 3231#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ 3232 builtin_types[(int) ENUM] \ 3233 = build_function_type \ 3234 (builtin_types[(int) RETURN], \ 3235 tree_cons (NULL_TREE, \ 3236 builtin_types[(int) ARG1], \ 3237 tree_cons (NULL_TREE, \ 3238 builtin_types[(int) ARG2], \ 3239 tree_cons \ 3240 (NULL_TREE, \ 3241 builtin_types[(int) ARG3], \ 3242 tree_cons (NULL_TREE, \ 3243 builtin_types[(int) ARG4], \ 3244 void_list_node))))); 3245#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ 3246 builtin_types[(int) ENUM] \ 3247 = build_function_type \ 3248 (builtin_types[(int) RETURN], \ 3249 tree_cons (NULL_TREE, \ 3250 builtin_types[(int) ARG1], \ 3251 tree_cons (NULL_TREE, \ 3252 builtin_types[(int) ARG2], \ 3253 tree_cons \ 3254 (NULL_TREE, \ 3255 builtin_types[(int) ARG3], \ 3256 tree_cons (NULL_TREE, \ 3257 builtin_types[(int) ARG4], \ 3258 tree_cons (NULL_TREE, \ 3259 builtin_types[(int) ARG5],\ 3260 void_list_node)))))); 3261#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ 3262 ARG6) \ 3263 builtin_types[(int) ENUM] \ 3264 = build_function_type \ 3265 (builtin_types[(int) RETURN], \ 3266 tree_cons (NULL_TREE, \ 3267 builtin_types[(int) ARG1], \ 3268 tree_cons (NULL_TREE, \ 3269 builtin_types[(int) ARG2], \ 3270 tree_cons \ 3271 (NULL_TREE, \ 3272 builtin_types[(int) ARG3], \ 3273 tree_cons \ 3274 (NULL_TREE, \ 3275 builtin_types[(int) ARG4], \ 3276 tree_cons (NULL_TREE, \ 3277 builtin_types[(int) ARG5], \ 3278 tree_cons (NULL_TREE, \ 3279 builtin_types[(int) ARG6],\ 3280 void_list_node))))))); 3281#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \ 3282 builtin_types[(int) ENUM] \ 3283 = build_function_type (builtin_types[(int) RETURN], NULL_TREE); 3284#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \ 3285 builtin_types[(int) ENUM] \ 3286 = build_function_type (builtin_types[(int) RETURN], \ 3287 tree_cons (NULL_TREE, \ 3288 builtin_types[(int) ARG1], \ 3289 NULL_TREE)); 3290 3291#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \ 3292 builtin_types[(int) ENUM] \ 3293 = build_function_type \ 3294 (builtin_types[(int) RETURN], \ 3295 tree_cons (NULL_TREE, \ 3296 builtin_types[(int) ARG1], \ 3297 tree_cons (NULL_TREE, \ 3298 builtin_types[(int) ARG2], \ 3299 NULL_TREE))); 3300 3301#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ 3302 builtin_types[(int) ENUM] \ 3303 = build_function_type \ 3304 (builtin_types[(int) RETURN], \ 3305 tree_cons (NULL_TREE, \ 3306 builtin_types[(int) ARG1], \ 3307 tree_cons (NULL_TREE, \ 3308 builtin_types[(int) ARG2], \ 3309 tree_cons (NULL_TREE, \ 3310 builtin_types[(int) ARG3], \ 3311 NULL_TREE)))); 3312 3313#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ 3314 builtin_types[(int) ENUM] \ 3315 = build_function_type \ 3316 (builtin_types[(int) RETURN], \ 3317 tree_cons (NULL_TREE, \ 3318 builtin_types[(int) ARG1], \ 3319 tree_cons (NULL_TREE, \ 3320 builtin_types[(int) ARG2], \ 3321 tree_cons (NULL_TREE, \ 3322 builtin_types[(int) ARG3], \ 3323 tree_cons (NULL_TREE, \ 3324 builtin_types[(int) ARG4],\ 3325 NULL_TREE))))); 3326 3327#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, \ 3328 ARG5) \ 3329 builtin_types[(int) ENUM] \ 3330 = build_function_type \ 3331 (builtin_types[(int) RETURN], \ 3332 tree_cons (NULL_TREE, \ 3333 builtin_types[(int) ARG1], \ 3334 tree_cons (NULL_TREE, \ 3335 builtin_types[(int) ARG2], \ 3336 tree_cons \ 3337 (NULL_TREE, \ 3338 builtin_types[(int) ARG3], \ 3339 tree_cons (NULL_TREE, \ 3340 builtin_types[(int) ARG4], \ 3341 tree_cons (NULL_TREE, \ 3342 builtin_types[(int) ARG5],\ 3343 NULL_TREE)))))); 3344 3345#define DEF_POINTER_TYPE(ENUM, TYPE) \ 3346 builtin_types[(int) ENUM] \ 3347 = build_pointer_type (builtin_types[(int) TYPE]); 3348#include "builtin-types.def" 3349#undef DEF_PRIMITIVE_TYPE 3350#undef DEF_FUNCTION_TYPE_1 3351#undef DEF_FUNCTION_TYPE_2 3352#undef DEF_FUNCTION_TYPE_3 3353#undef DEF_FUNCTION_TYPE_4 3354#undef DEF_FUNCTION_TYPE_5 3355#undef DEF_FUNCTION_TYPE_6 3356#undef DEF_FUNCTION_TYPE_VAR_0 3357#undef DEF_FUNCTION_TYPE_VAR_1 3358#undef DEF_FUNCTION_TYPE_VAR_2 3359#undef DEF_FUNCTION_TYPE_VAR_3 3360#undef DEF_FUNCTION_TYPE_VAR_4 3361#undef DEF_FUNCTION_TYPE_VAR_5 3362#undef DEF_POINTER_TYPE 3363 builtin_types[(int) BT_LAST] = NULL_TREE; 3364 3365 c_init_attributes (); 3366 3367#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \ 3368 NONANSI_P, ATTRS, IMPLICIT, COND) \ 3369 if (NAME && COND) \ 3370 def_builtin_1 (ENUM, NAME, CLASS, \ 3371 builtin_types[(int) TYPE], \ 3372 builtin_types[(int) LIBTYPE], \ 3373 BOTH_P, FALLBACK_P, NONANSI_P, \ 3374 built_in_attributes[(int) ATTRS], IMPLICIT); 3375#include "builtins.def" 3376#undef DEF_BUILTIN 3377 3378 build_common_builtin_nodes (); 3379 3380 targetm.init_builtins (); 3381 if (flag_mudflap) 3382 mudflap_init (); 3383 3384 main_identifier_node = get_identifier ("main"); 3385 3386 /* Create the built-in __null node. It is important that this is 3387 not shared. */ 3388 null_node = make_node (INTEGER_CST); 3389 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0); 3390} 3391 3392/* Look up the function in built_in_decls that corresponds to DECL 3393 and set ASMSPEC as its user assembler name. DECL must be a 3394 function decl that declares a builtin. */ 3395 3396void 3397set_builtin_user_assembler_name (tree decl, const char *asmspec) 3398{ 3399 tree builtin; 3400 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL 3401 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL 3402 && asmspec != 0); 3403 3404 builtin = built_in_decls [DECL_FUNCTION_CODE (decl)]; 3405 set_user_assembler_name (builtin, asmspec); 3406 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY) 3407 init_block_move_fn (asmspec); 3408 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET) 3409 init_block_clear_fn (asmspec); 3410} 3411 3412/* The number of named compound-literals generated thus far. */ 3413static GTY(()) int compound_literal_number; 3414 3415/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */ 3416 3417void 3418set_compound_literal_name (tree decl) 3419{ 3420 char *name; 3421 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal", 3422 compound_literal_number); 3423 compound_literal_number++; 3424 DECL_NAME (decl) = get_identifier (name); 3425} 3426 3427tree 3428build_va_arg (tree expr, tree type) 3429{ 3430 return build1 (VA_ARG_EXPR, type, expr); 3431} 3432 3433 3434/* Linked list of disabled built-in functions. */ 3435 3436typedef struct disabled_builtin 3437{ 3438 const char *name; 3439 struct disabled_builtin *next; 3440} disabled_builtin; 3441static disabled_builtin *disabled_builtins = NULL; 3442 3443static bool builtin_function_disabled_p (const char *); 3444 3445/* Disable a built-in function specified by -fno-builtin-NAME. If NAME 3446 begins with "__builtin_", give an error. */ 3447 3448void 3449disable_builtin_function (const char *name) 3450{ 3451 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0) 3452 error ("cannot disable built-in function %qs", name); 3453 else 3454 { 3455 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin); 3456 new_disabled_builtin->name = name; 3457 new_disabled_builtin->next = disabled_builtins; 3458 disabled_builtins = new_disabled_builtin; 3459 } 3460} 3461 3462 3463/* Return true if the built-in function NAME has been disabled, false 3464 otherwise. */ 3465 3466static bool 3467builtin_function_disabled_p (const char *name) 3468{ 3469 disabled_builtin *p; 3470 for (p = disabled_builtins; p != NULL; p = p->next) 3471 { 3472 if (strcmp (name, p->name) == 0) 3473 return true; 3474 } 3475 return false; 3476} 3477 3478 3479/* Worker for DEF_BUILTIN. 3480 Possibly define a builtin function with one or two names. 3481 Does not declare a non-__builtin_ function if flag_no_builtin, or if 3482 nonansi_p and flag_no_nonansi_builtin. */ 3483 3484static void 3485def_builtin_1 (enum built_in_function fncode, 3486 const char *name, 3487 enum built_in_class fnclass, 3488 tree fntype, tree libtype, 3489 bool both_p, bool fallback_p, bool nonansi_p, 3490 tree fnattrs, bool implicit_p) 3491{ 3492 tree decl; 3493 const char *libname; 3494 3495 gcc_assert ((!both_p && !fallback_p) 3496 || !strncmp (name, "__builtin_", 3497 strlen ("__builtin_"))); 3498 3499 libname = name + strlen ("__builtin_"); 3500 decl = lang_hooks.builtin_function (name, fntype, fncode, fnclass, 3501 (fallback_p ? libname : NULL), 3502 fnattrs); 3503 if (both_p 3504 && !flag_no_builtin && !builtin_function_disabled_p (libname) 3505 && !(nonansi_p && flag_no_nonansi_builtin)) 3506 lang_hooks.builtin_function (libname, libtype, fncode, fnclass, 3507 NULL, fnattrs); 3508 3509 built_in_decls[(int) fncode] = decl; 3510 if (implicit_p) 3511 implicit_built_in_decls[(int) fncode] = decl; 3512} 3513 3514/* Nonzero if the type T promotes to int. This is (nearly) the 3515 integral promotions defined in ISO C99 6.3.1.1/2. */ 3516 3517bool 3518c_promoting_integer_type_p (tree t) 3519{ 3520 switch (TREE_CODE (t)) 3521 { 3522 case INTEGER_TYPE: 3523 return (TYPE_MAIN_VARIANT (t) == char_type_node 3524 || TYPE_MAIN_VARIANT (t) == signed_char_type_node 3525 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node 3526 || TYPE_MAIN_VARIANT (t) == short_integer_type_node 3527 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node 3528 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node)); 3529 3530 case ENUMERAL_TYPE: 3531 /* ??? Technically all enumerations not larger than an int 3532 promote to an int. But this is used along code paths 3533 that only want to notice a size change. */ 3534 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node); 3535 3536 case BOOLEAN_TYPE: 3537 return 1; 3538 3539 default: 3540 return 0; 3541 } 3542} 3543 3544/* Return 1 if PARMS specifies a fixed number of parameters 3545 and none of their types is affected by default promotions. */ 3546 3547int 3548self_promoting_args_p (tree parms) 3549{ 3550 tree t; 3551 for (t = parms; t; t = TREE_CHAIN (t)) 3552 { 3553 tree type = TREE_VALUE (t); 3554 3555 if (TREE_CHAIN (t) == 0 && type != void_type_node) 3556 return 0; 3557 3558 if (type == 0) 3559 return 0; 3560 3561 if (TYPE_MAIN_VARIANT (type) == float_type_node) 3562 return 0; 3563 3564 if (c_promoting_integer_type_p (type)) 3565 return 0; 3566 } 3567 return 1; 3568} 3569 3570/* Recursively examines the array elements of TYPE, until a non-array 3571 element type is found. */ 3572 3573tree 3574strip_array_types (tree type) 3575{ 3576 while (TREE_CODE (type) == ARRAY_TYPE) 3577 type = TREE_TYPE (type); 3578 3579 return type; 3580} 3581 3582/* Recursively remove any '*' or '&' operator from TYPE. */ 3583tree 3584strip_pointer_operator (tree t) 3585{ 3586 while (POINTER_TYPE_P (t)) 3587 t = TREE_TYPE (t); 3588 return t; 3589} 3590 3591/* Used to compare case labels. K1 and K2 are actually tree nodes 3592 representing case labels, or NULL_TREE for a `default' label. 3593 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after 3594 K2, and 0 if K1 and K2 are equal. */ 3595 3596int 3597case_compare (splay_tree_key k1, splay_tree_key k2) 3598{ 3599 /* Consider a NULL key (such as arises with a `default' label) to be 3600 smaller than anything else. */ 3601 if (!k1) 3602 return k2 ? -1 : 0; 3603 else if (!k2) 3604 return k1 ? 1 : 0; 3605 3606 return tree_int_cst_compare ((tree) k1, (tree) k2); 3607} 3608 3609/* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If 3610 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is 3611 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then 3612 case label was declared using the usual C/C++ syntax, rather than 3613 the GNU case range extension. CASES is a tree containing all the 3614 case ranges processed so far; COND is the condition for the 3615 switch-statement itself. Returns the CASE_LABEL_EXPR created, or 3616 ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */ 3617 3618tree 3619c_add_case_label (splay_tree cases, tree cond, tree orig_type, 3620 tree low_value, tree high_value) 3621{ 3622 tree type; 3623 tree label; 3624 tree case_label; 3625 splay_tree_node node; 3626 3627 /* Create the LABEL_DECL itself. */ 3628 label = create_artificial_label (); 3629 3630 /* If there was an error processing the switch condition, bail now 3631 before we get more confused. */ 3632 if (!cond || cond == error_mark_node) 3633 goto error_out; 3634 3635 if ((low_value && TREE_TYPE (low_value) 3636 && POINTER_TYPE_P (TREE_TYPE (low_value))) 3637 || (high_value && TREE_TYPE (high_value) 3638 && POINTER_TYPE_P (TREE_TYPE (high_value)))) 3639 { 3640 error ("pointers are not permitted as case values"); 3641 goto error_out; 3642 } 3643 3644 /* Case ranges are a GNU extension. */ 3645 if (high_value && pedantic) 3646 pedwarn ("range expressions in switch statements are non-standard"); 3647 3648 type = TREE_TYPE (cond); 3649 if (low_value) 3650 { 3651 low_value = check_case_value (low_value); 3652 low_value = convert_and_check (type, low_value); 3653 if (low_value == error_mark_node) 3654 goto error_out; 3655 } 3656 if (high_value) 3657 { 3658 high_value = check_case_value (high_value); 3659 high_value = convert_and_check (type, high_value); 3660 if (high_value == error_mark_node) 3661 goto error_out; 3662 } 3663 3664 if (low_value && high_value) 3665 { 3666 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't 3667 really a case range, even though it was written that way. 3668 Remove the HIGH_VALUE to simplify later processing. */ 3669 if (tree_int_cst_equal (low_value, high_value)) 3670 high_value = NULL_TREE; 3671 else if (!tree_int_cst_lt (low_value, high_value)) 3672 warning (0, "empty range specified"); 3673 } 3674 3675 /* See if the case is in range of the type of the original testing 3676 expression. If both low_value and high_value are out of range, 3677 don't insert the case label and return NULL_TREE. */ 3678 if (low_value 3679 && !check_case_bounds (type, orig_type, 3680 &low_value, high_value ? &high_value : NULL)) 3681 return NULL_TREE; 3682 3683 /* Look up the LOW_VALUE in the table of case labels we already 3684 have. */ 3685 node = splay_tree_lookup (cases, (splay_tree_key) low_value); 3686 /* If there was not an exact match, check for overlapping ranges. 3687 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE; 3688 that's a `default' label and the only overlap is an exact match. */ 3689 if (!node && (low_value || high_value)) 3690 { 3691 splay_tree_node low_bound; 3692 splay_tree_node high_bound; 3693 3694 /* Even though there wasn't an exact match, there might be an 3695 overlap between this case range and another case range. 3696 Since we've (inductively) not allowed any overlapping case 3697 ranges, we simply need to find the greatest low case label 3698 that is smaller that LOW_VALUE, and the smallest low case 3699 label that is greater than LOW_VALUE. If there is an overlap 3700 it will occur in one of these two ranges. */ 3701 low_bound = splay_tree_predecessor (cases, 3702 (splay_tree_key) low_value); 3703 high_bound = splay_tree_successor (cases, 3704 (splay_tree_key) low_value); 3705 3706 /* Check to see if the LOW_BOUND overlaps. It is smaller than 3707 the LOW_VALUE, so there is no need to check unless the 3708 LOW_BOUND is in fact itself a case range. */ 3709 if (low_bound 3710 && CASE_HIGH ((tree) low_bound->value) 3711 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value), 3712 low_value) >= 0) 3713 node = low_bound; 3714 /* Check to see if the HIGH_BOUND overlaps. The low end of that 3715 range is bigger than the low end of the current range, so we 3716 are only interested if the current range is a real range, and 3717 not an ordinary case label. */ 3718 else if (high_bound 3719 && high_value 3720 && (tree_int_cst_compare ((tree) high_bound->key, 3721 high_value) 3722 <= 0)) 3723 node = high_bound; 3724 } 3725 /* If there was an overlap, issue an error. */ 3726 if (node) 3727 { 3728 tree duplicate = CASE_LABEL ((tree) node->value); 3729 3730 if (high_value) 3731 { 3732 error ("duplicate (or overlapping) case value"); 3733 error ("%Jthis is the first entry overlapping that value", duplicate); 3734 } 3735 else if (low_value) 3736 { 3737 error ("duplicate case value") ; 3738 error ("%Jpreviously used here", duplicate); 3739 } 3740 else 3741 { 3742 error ("multiple default labels in one switch"); 3743 error ("%Jthis is the first default label", duplicate); 3744 } 3745 goto error_out; 3746 } 3747 3748 /* Add a CASE_LABEL to the statement-tree. */ 3749 case_label = add_stmt (build_case_label (low_value, high_value, label)); 3750 /* Register this case label in the splay tree. */ 3751 splay_tree_insert (cases, 3752 (splay_tree_key) low_value, 3753 (splay_tree_value) case_label); 3754 3755 return case_label; 3756 3757 error_out: 3758 /* Add a label so that the back-end doesn't think that the beginning of 3759 the switch is unreachable. Note that we do not add a case label, as 3760 that just leads to duplicates and thence to failure later on. */ 3761 if (!cases->root) 3762 { 3763 tree t = create_artificial_label (); 3764 add_stmt (build_stmt (LABEL_EXPR, t)); 3765 } 3766 return error_mark_node; 3767} 3768 3769/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach. 3770 Used to verify that case values match up with enumerator values. */ 3771 3772static void 3773match_case_to_enum_1 (tree key, tree type, tree label) 3774{ 3775 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1]; 3776 3777 /* ??? Not working too hard to print the double-word value. 3778 Should perhaps be done with %lwd in the diagnostic routines? */ 3779 if (TREE_INT_CST_HIGH (key) == 0) 3780 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED, 3781 TREE_INT_CST_LOW (key)); 3782 else if (!TYPE_UNSIGNED (type) 3783 && TREE_INT_CST_HIGH (key) == -1 3784 && TREE_INT_CST_LOW (key) != 0) 3785 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED, 3786 -TREE_INT_CST_LOW (key)); 3787 else 3788 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX, 3789 TREE_INT_CST_HIGH (key), TREE_INT_CST_LOW (key)); 3790 3791 if (TYPE_NAME (type) == 0) 3792 warning (0, "%Jcase value %qs not in enumerated type", 3793 CASE_LABEL (label), buf); 3794 else 3795 warning (0, "%Jcase value %qs not in enumerated type %qT", 3796 CASE_LABEL (label), buf, type); 3797} 3798 3799static int 3800match_case_to_enum (splay_tree_node node, void *data) 3801{ 3802 tree label = (tree) node->value; 3803 tree type = (tree) data; 3804 3805 /* Skip default case. */ 3806 if (!CASE_LOW (label)) 3807 return 0; 3808 3809 /* If TREE_ADDRESSABLE is not set, that means CASE_LOW did not appear 3810 when we did our enum->case scan. Reset our scratch bit after. */ 3811 if (!TREE_ADDRESSABLE (label)) 3812 match_case_to_enum_1 (CASE_LOW (label), type, label); 3813 else 3814 TREE_ADDRESSABLE (label) = 0; 3815 3816 /* If CASE_HIGH is non-null, we have a range. Here we must search. 3817 Note that the old code in stmt.c did not check for the values in 3818 the range either, just the endpoints. */ 3819 if (CASE_HIGH (label)) 3820 { 3821 tree chain, key = CASE_HIGH (label); 3822 3823 for (chain = TYPE_VALUES (type); 3824 chain && !tree_int_cst_equal (key, TREE_VALUE (chain)); 3825 chain = TREE_CHAIN (chain)) 3826 continue; 3827 if (!chain) 3828 match_case_to_enum_1 (key, type, label); 3829 } 3830 3831 return 0; 3832} 3833 3834/* Handle -Wswitch*. Called from the front end after parsing the 3835 switch construct. */ 3836/* ??? Should probably be somewhere generic, since other languages 3837 besides C and C++ would want this. At the moment, however, C/C++ 3838 are the only tree-ssa languages that support enumerations at all, 3839 so the point is moot. */ 3840 3841void 3842c_do_switch_warnings (splay_tree cases, location_t switch_location, 3843 tree type, tree cond) 3844{ 3845 splay_tree_node default_node; 3846 3847 if (!warn_switch && !warn_switch_enum && !warn_switch_default) 3848 return; 3849 3850 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL); 3851 if (!default_node) 3852 warning (OPT_Wswitch_default, "%Hswitch missing default case", 3853 &switch_location); 3854 3855 /* If the switch expression was an enumerated type, check that 3856 exactly all enumeration literals are covered by the cases. 3857 The check is made when -Wswitch was specified and there is no 3858 default case, or when -Wswitch-enum was specified. */ 3859 if (((warn_switch && !default_node) || warn_switch_enum) 3860 && type && TREE_CODE (type) == ENUMERAL_TYPE 3861 && TREE_CODE (cond) != INTEGER_CST) 3862 { 3863 tree chain; 3864 3865 /* The time complexity here is O(N*lg(N)) worst case, but for the 3866 common case of monotonically increasing enumerators, it is 3867 O(N), since the nature of the splay tree will keep the next 3868 element adjacent to the root at all times. */ 3869 3870 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain)) 3871 { 3872 splay_tree_node node 3873 = splay_tree_lookup (cases, (splay_tree_key) TREE_VALUE (chain)); 3874 if (!node) 3875 { 3876 tree low_value = TREE_VALUE (chain); 3877 splay_tree_node low_bound; 3878 splay_tree_node high_bound; 3879 /* Even though there wasn't an exact match, there might be a 3880 case range which includes the enumator's value. */ 3881 low_bound = splay_tree_predecessor (cases, 3882 (splay_tree_key) low_value); 3883 high_bound = splay_tree_successor (cases, 3884 (splay_tree_key) low_value); 3885 3886 /* It is smaller than the LOW_VALUE, so there is no need to check 3887 unless the LOW_BOUND is in fact itself a case range. */ 3888 if (low_bound 3889 && CASE_HIGH ((tree) low_bound->value) 3890 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value), 3891 low_value) >= 0) 3892 node = low_bound; 3893 /* The low end of that range is bigger than the current value. */ 3894 else if (high_bound 3895 && (tree_int_cst_compare ((tree) high_bound->key, 3896 low_value) 3897 <= 0)) 3898 node = high_bound; 3899 } 3900 if (node) 3901 { 3902 /* Mark the CASE_LOW part of the case entry as seen, so 3903 that we save time later. Choose TREE_ADDRESSABLE 3904 randomly as a bit that won't have been set to-date. */ 3905 tree label = (tree) node->value; 3906 TREE_ADDRESSABLE (label) = 1; 3907 } 3908 else 3909 { 3910 /* Warn if there are enumerators that don't correspond to 3911 case expressions. */ 3912 warning (0, "%Henumeration value %qE not handled in switch", 3913 &switch_location, TREE_PURPOSE (chain)); 3914 } 3915 } 3916 3917 /* Warn if there are case expressions that don't correspond to 3918 enumerators. This can occur since C and C++ don't enforce 3919 type-checking of assignments to enumeration variables. 3920 3921 The time complexity here is O(N**2) worst case, since we've 3922 not sorted the enumeration values. However, in the absence 3923 of case ranges this is O(N), since all single cases that 3924 corresponded to enumerations have been marked above. */ 3925 3926 splay_tree_foreach (cases, match_case_to_enum, type); 3927 } 3928} 3929 3930/* Finish an expression taking the address of LABEL (an 3931 IDENTIFIER_NODE). Returns an expression for the address. */ 3932 3933tree 3934finish_label_address_expr (tree label) 3935{ 3936 tree result; 3937 3938 if (pedantic) 3939 pedwarn ("taking the address of a label is non-standard"); 3940 3941 if (label == error_mark_node) 3942 return error_mark_node; 3943 3944 label = lookup_label (label); 3945 if (label == NULL_TREE) 3946 result = null_pointer_node; 3947 else 3948 { 3949 TREE_USED (label) = 1; 3950 result = build1 (ADDR_EXPR, ptr_type_node, label); 3951 /* The current function in not necessarily uninlinable. 3952 Computed gotos are incompatible with inlining, but the value 3953 here could be used only in a diagnostic, for example. */ 3954 } 3955 3956 return result; 3957} 3958 3959/* Hook used by expand_expr to expand language-specific tree codes. */ 3960/* The only things that should go here are bits needed to expand 3961 constant initializers. Everything else should be handled by the 3962 gimplification routines. */ 3963 3964rtx 3965c_expand_expr (tree exp, rtx target, enum machine_mode tmode, 3966 int modifier /* Actually enum_modifier. */, 3967 rtx *alt_rtl) 3968{ 3969 switch (TREE_CODE (exp)) 3970 { 3971 case COMPOUND_LITERAL_EXPR: 3972 { 3973 /* Initialize the anonymous variable declared in the compound 3974 literal, then return the variable. */ 3975 tree decl = COMPOUND_LITERAL_EXPR_DECL (exp); 3976 emit_local_var (decl); 3977 return expand_expr_real (decl, target, tmode, modifier, alt_rtl); 3978 } 3979 3980 default: 3981 gcc_unreachable (); 3982 } 3983} 3984 3985/* Hook used by staticp to handle language-specific tree codes. */ 3986 3987tree 3988c_staticp (tree exp) 3989{ 3990 return (TREE_CODE (exp) == COMPOUND_LITERAL_EXPR 3991 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)) 3992 ? exp : NULL); 3993} 3994 3995 3996/* Given a boolean expression ARG, return a tree representing an increment 3997 or decrement (as indicated by CODE) of ARG. The front end must check for 3998 invalid cases (e.g., decrement in C++). */ 3999tree 4000boolean_increment (enum tree_code code, tree arg) 4001{ 4002 tree val; 4003 tree true_res = boolean_true_node; 4004 4005 arg = stabilize_reference (arg); 4006 switch (code) 4007 { 4008 case PREINCREMENT_EXPR: 4009 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); 4010 break; 4011 case POSTINCREMENT_EXPR: 4012 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); 4013 arg = save_expr (arg); 4014 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); 4015 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); 4016 break; 4017 case PREDECREMENT_EXPR: 4018 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, 4019 invert_truthvalue (arg)); 4020 break; 4021 case POSTDECREMENT_EXPR: 4022 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, 4023 invert_truthvalue (arg)); 4024 arg = save_expr (arg); 4025 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); 4026 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); 4027 break; 4028 default: 4029 gcc_unreachable (); 4030 } 4031 TREE_SIDE_EFFECTS (val) = 1; 4032 return val; 4033} 4034 4035/* Built-in macros for stddef.h, that require macros defined in this 4036 file. */ 4037void 4038c_stddef_cpp_builtins(void) 4039{ 4040 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0); 4041 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0); 4042 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0); 4043 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0); 4044 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0); 4045 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0); 4046} 4047 4048static void 4049c_init_attributes (void) 4050{ 4051 /* Fill in the built_in_attributes array. */ 4052#define DEF_ATTR_NULL_TREE(ENUM) \ 4053 built_in_attributes[(int) ENUM] = NULL_TREE; 4054#define DEF_ATTR_INT(ENUM, VALUE) \ 4055 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE); 4056#define DEF_ATTR_IDENT(ENUM, STRING) \ 4057 built_in_attributes[(int) ENUM] = get_identifier (STRING); 4058#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \ 4059 built_in_attributes[(int) ENUM] \ 4060 = tree_cons (built_in_attributes[(int) PURPOSE], \ 4061 built_in_attributes[(int) VALUE], \ 4062 built_in_attributes[(int) CHAIN]); 4063#include "builtin-attrs.def" 4064#undef DEF_ATTR_NULL_TREE 4065#undef DEF_ATTR_INT 4066#undef DEF_ATTR_IDENT 4067#undef DEF_ATTR_TREE_LIST 4068} 4069 4070/* Attribute handlers common to C front ends. */ 4071 4072/* Handle a "packed" attribute; arguments as in 4073 struct attribute_spec.handler. */ 4074 4075static tree 4076handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args), 4077 int flags, bool *no_add_attrs) 4078{ 4079 if (TYPE_P (*node)) 4080 { 4081 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) 4082 *node = build_variant_type_copy (*node); 4083 TYPE_PACKED (*node) = 1; 4084 if (TYPE_MAIN_VARIANT (*node) == *node) 4085 { 4086 /* If it is the main variant, then pack the other variants 4087 too. This happens in, 4088 4089 struct Foo { 4090 struct Foo const *ptr; // creates a variant w/o packed flag 4091 } __ attribute__((packed)); // packs it now. 4092 */ 4093 tree probe; 4094 4095 for (probe = *node; probe; probe = TYPE_NEXT_VARIANT (probe)) 4096 TYPE_PACKED (probe) = 1; 4097 } 4098 } 4099 else if (TREE_CODE (*node) == FIELD_DECL) 4100 { 4101 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT) 4102 warning (OPT_Wattributes, 4103 "%qE attribute ignored for field of type %qT", 4104 name, TREE_TYPE (*node)); 4105 else 4106 DECL_PACKED (*node) = 1; 4107 } 4108 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is 4109 used for DECL_REGISTER. It wouldn't mean anything anyway. 4110 We can't set DECL_PACKED on the type of a TYPE_DECL, because 4111 that changes what the typedef is typing. */ 4112 else 4113 { 4114 warning (OPT_Wattributes, "%qE attribute ignored", name); 4115 *no_add_attrs = true; 4116 } 4117 4118 return NULL_TREE; 4119} 4120 4121/* Handle a "nocommon" attribute; arguments as in 4122 struct attribute_spec.handler. */ 4123 4124static tree 4125handle_nocommon_attribute (tree *node, tree name, 4126 tree ARG_UNUSED (args), 4127 int ARG_UNUSED (flags), bool *no_add_attrs) 4128{ 4129 if (TREE_CODE (*node) == VAR_DECL) 4130 DECL_COMMON (*node) = 0; 4131 else 4132 { 4133 warning (OPT_Wattributes, "%qE attribute ignored", name); 4134 *no_add_attrs = true; 4135 } 4136 4137 return NULL_TREE; 4138} 4139 4140/* Handle a "common" attribute; arguments as in 4141 struct attribute_spec.handler. */ 4142 4143static tree 4144handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args), 4145 int ARG_UNUSED (flags), bool *no_add_attrs) 4146{ 4147 if (TREE_CODE (*node) == VAR_DECL) 4148 DECL_COMMON (*node) = 1; 4149 else 4150 { 4151 warning (OPT_Wattributes, "%qE attribute ignored", name); 4152 *no_add_attrs = true; 4153 } 4154 4155 return NULL_TREE; 4156} 4157 4158/* Handle a "noreturn" attribute; arguments as in 4159 struct attribute_spec.handler. */ 4160 4161static tree 4162handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args), 4163 int ARG_UNUSED (flags), bool *no_add_attrs) 4164{ 4165 tree type = TREE_TYPE (*node); 4166 4167 /* See FIXME comment in c_common_attribute_table. */ 4168 if (TREE_CODE (*node) == FUNCTION_DECL) 4169 TREE_THIS_VOLATILE (*node) = 1; 4170 else if (TREE_CODE (type) == POINTER_TYPE 4171 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) 4172 TREE_TYPE (*node) 4173 = build_pointer_type 4174 (build_type_variant (TREE_TYPE (type), 4175 TYPE_READONLY (TREE_TYPE (type)), 1)); 4176 else 4177 { 4178 warning (OPT_Wattributes, "%qE attribute ignored", name); 4179 *no_add_attrs = true; 4180 } 4181 4182 return NULL_TREE; 4183} 4184 4185/* Handle a "noinline" attribute; arguments as in 4186 struct attribute_spec.handler. */ 4187 4188static tree 4189handle_noinline_attribute (tree *node, tree name, 4190 tree ARG_UNUSED (args), 4191 int ARG_UNUSED (flags), bool *no_add_attrs) 4192{ 4193 if (TREE_CODE (*node) == FUNCTION_DECL) 4194 DECL_UNINLINABLE (*node) = 1; 4195 else 4196 { 4197 warning (OPT_Wattributes, "%qE attribute ignored", name); 4198 *no_add_attrs = true; 4199 } 4200 4201 return NULL_TREE; 4202} 4203 4204/* Handle a "always_inline" attribute; arguments as in 4205 struct attribute_spec.handler. */ 4206 4207static tree 4208handle_always_inline_attribute (tree *node, tree name, 4209 tree ARG_UNUSED (args), 4210 int ARG_UNUSED (flags), 4211 bool *no_add_attrs) 4212{ 4213 if (TREE_CODE (*node) == FUNCTION_DECL) 4214 { 4215 /* Do nothing else, just set the attribute. We'll get at 4216 it later with lookup_attribute. */ 4217 } 4218 else 4219 { 4220 warning (OPT_Wattributes, "%qE attribute ignored", name); 4221 *no_add_attrs = true; 4222 } 4223 4224 return NULL_TREE; 4225} 4226 4227/* Handle a "gnu_inline" attribute; arguments as in 4228 struct attribute_spec.handler. */ 4229 4230static tree 4231handle_gnu_inline_attribute (tree *node, tree name, 4232 tree ARG_UNUSED (args), 4233 int ARG_UNUSED (flags), 4234 bool *no_add_attrs) 4235{ 4236 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) 4237 { 4238 /* Do nothing else, just set the attribute. We'll get at 4239 it later with lookup_attribute. */ 4240 } 4241 else 4242 { 4243 warning (OPT_Wattributes, "%qE attribute ignored", name); 4244 *no_add_attrs = true; 4245 } 4246 4247 return NULL_TREE; 4248} 4249 4250/* Handle a "flatten" attribute; arguments as in 4251 struct attribute_spec.handler. */ 4252 4253static tree 4254handle_flatten_attribute (tree *node, tree name, 4255 tree args ATTRIBUTE_UNUSED, 4256 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) 4257{ 4258 if (TREE_CODE (*node) == FUNCTION_DECL) 4259 /* Do nothing else, just set the attribute. We'll get at 4260 it later with lookup_attribute. */ 4261 ; 4262 else 4263 { 4264 warning (OPT_Wattributes, "%qE attribute ignored", name); 4265 *no_add_attrs = true; 4266 } 4267 4268 return NULL_TREE; 4269} 4270 4271 4272/* Handle a "used" attribute; arguments as in 4273 struct attribute_spec.handler. */ 4274 4275static tree 4276handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args), 4277 int ARG_UNUSED (flags), bool *no_add_attrs) 4278{ 4279 tree node = *pnode; 4280 4281 if (TREE_CODE (node) == FUNCTION_DECL 4282 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))) 4283 { 4284 TREE_USED (node) = 1; 4285 DECL_PRESERVE_P (node) = 1; 4286 } 4287 else 4288 { 4289 warning (OPT_Wattributes, "%qE attribute ignored", name); 4290 *no_add_attrs = true; 4291 } 4292 4293 return NULL_TREE; 4294} 4295 4296/* Handle a "unused" attribute; arguments as in 4297 struct attribute_spec.handler. */ 4298 4299static tree 4300handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args), 4301 int flags, bool *no_add_attrs) 4302{ 4303 if (DECL_P (*node)) 4304 { 4305 tree decl = *node; 4306 4307 if (TREE_CODE (decl) == PARM_DECL 4308 || TREE_CODE (decl) == VAR_DECL 4309 || TREE_CODE (decl) == FUNCTION_DECL 4310 || TREE_CODE (decl) == LABEL_DECL 4311 || TREE_CODE (decl) == TYPE_DECL) 4312 TREE_USED (decl) = 1; 4313 else 4314 { 4315 warning (OPT_Wattributes, "%qE attribute ignored", name); 4316 *no_add_attrs = true; 4317 } 4318 } 4319 else 4320 { 4321 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) 4322 *node = build_variant_type_copy (*node); 4323 TREE_USED (*node) = 1; 4324 } 4325 4326 return NULL_TREE; 4327} 4328 4329/* Handle a "externally_visible" attribute; arguments as in 4330 struct attribute_spec.handler. */ 4331 4332static tree 4333handle_externally_visible_attribute (tree *pnode, tree name, 4334 tree ARG_UNUSED (args), 4335 int ARG_UNUSED (flags), 4336 bool *no_add_attrs) 4337{ 4338 tree node = *pnode; 4339 4340 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL) 4341 || !TREE_PUBLIC (node)) 4342 { 4343 warning (OPT_Wattributes, 4344 "%qE attribute have effect only on public objects", name); 4345 *no_add_attrs = true; 4346 } 4347 else if (TREE_CODE (node) == FUNCTION_DECL 4348 || TREE_CODE (node) == VAR_DECL) 4349 ; 4350 else 4351 { 4352 warning (OPT_Wattributes, "%qE attribute ignored", name); 4353 *no_add_attrs = true; 4354 } 4355 4356 return NULL_TREE; 4357} 4358 4359/* Handle a "const" attribute; arguments as in 4360 struct attribute_spec.handler. */ 4361 4362static tree 4363handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args), 4364 int ARG_UNUSED (flags), bool *no_add_attrs) 4365{ 4366 tree type = TREE_TYPE (*node); 4367 4368 /* See FIXME comment on noreturn in c_common_attribute_table. */ 4369 if (TREE_CODE (*node) == FUNCTION_DECL) 4370 TREE_READONLY (*node) = 1; 4371 else if (TREE_CODE (type) == POINTER_TYPE 4372 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) 4373 TREE_TYPE (*node) 4374 = build_pointer_type 4375 (build_type_variant (TREE_TYPE (type), 1, 4376 TREE_THIS_VOLATILE (TREE_TYPE (type)))); 4377 else 4378 { 4379 warning (OPT_Wattributes, "%qE attribute ignored", name); 4380 *no_add_attrs = true; 4381 } 4382 4383 return NULL_TREE; 4384} 4385 4386/* Handle a "transparent_union" attribute; arguments as in 4387 struct attribute_spec.handler. */ 4388 4389static tree 4390handle_transparent_union_attribute (tree *node, tree name, 4391 tree ARG_UNUSED (args), int flags, 4392 bool *no_add_attrs) 4393{ 4394 tree type = NULL; 4395 4396 *no_add_attrs = true; 4397 4398 if (DECL_P (*node)) 4399 { 4400 if (TREE_CODE (*node) != TYPE_DECL) 4401 goto ignored; 4402 node = &TREE_TYPE (*node); 4403 type = *node; 4404 } 4405 else if (TYPE_P (*node)) 4406 type = *node; 4407 else 4408 goto ignored; 4409 4410 if (TREE_CODE (type) == UNION_TYPE) 4411 { 4412 /* When IN_PLACE is set, leave the check for FIELDS and MODE to 4413 the code in finish_struct. */ 4414 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) 4415 { 4416 if (TYPE_FIELDS (type) == NULL_TREE 4417 || TYPE_MODE (type) != DECL_MODE (TYPE_FIELDS (type))) 4418 goto ignored; 4419 4420 /* A type variant isn't good enough, since we don't a cast 4421 to such a type removed as a no-op. */ 4422 *node = type = build_duplicate_type (type); 4423 } 4424 4425 TYPE_TRANSPARENT_UNION (type) = 1; 4426 return NULL_TREE; 4427 } 4428 4429 ignored: 4430 warning (OPT_Wattributes, "%qE attribute ignored", name); 4431 return NULL_TREE; 4432} 4433 4434/* Handle a "constructor" attribute; arguments as in 4435 struct attribute_spec.handler. */ 4436 4437static tree 4438handle_constructor_attribute (tree *node, tree name, 4439 tree ARG_UNUSED (args), 4440 int ARG_UNUSED (flags), 4441 bool *no_add_attrs) 4442{ 4443 tree decl = *node; 4444 tree type = TREE_TYPE (decl); 4445 4446 if (TREE_CODE (decl) == FUNCTION_DECL 4447 && TREE_CODE (type) == FUNCTION_TYPE 4448 && decl_function_context (decl) == 0) 4449 { 4450 DECL_STATIC_CONSTRUCTOR (decl) = 1; 4451 TREE_USED (decl) = 1; 4452 } 4453 else 4454 { 4455 warning (OPT_Wattributes, "%qE attribute ignored", name); 4456 *no_add_attrs = true; 4457 } 4458 4459 return NULL_TREE; 4460} 4461 4462/* Handle a "destructor" attribute; arguments as in 4463 struct attribute_spec.handler. */ 4464 4465static tree 4466handle_destructor_attribute (tree *node, tree name, 4467 tree ARG_UNUSED (args), 4468 int ARG_UNUSED (flags), 4469 bool *no_add_attrs) 4470{ 4471 tree decl = *node; 4472 tree type = TREE_TYPE (decl); 4473 4474 if (TREE_CODE (decl) == FUNCTION_DECL 4475 && TREE_CODE (type) == FUNCTION_TYPE 4476 && decl_function_context (decl) == 0) 4477 { 4478 DECL_STATIC_DESTRUCTOR (decl) = 1; 4479 TREE_USED (decl) = 1; 4480 } 4481 else 4482 { 4483 warning (OPT_Wattributes, "%qE attribute ignored", name); 4484 *no_add_attrs = true; 4485 } 4486 4487 return NULL_TREE; 4488} 4489 4490/* Handle a "mode" attribute; arguments as in 4491 struct attribute_spec.handler. */ 4492 4493static tree 4494handle_mode_attribute (tree *node, tree name, tree args, 4495 int ARG_UNUSED (flags), bool *no_add_attrs) 4496{ 4497 tree type = *node; 4498 4499 *no_add_attrs = true; 4500 4501 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE) 4502 warning (OPT_Wattributes, "%qE attribute ignored", name); 4503 else 4504 { 4505 int j; 4506 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args)); 4507 int len = strlen (p); 4508 enum machine_mode mode = VOIDmode; 4509 tree typefm; 4510 bool valid_mode; 4511 4512 if (len > 4 && p[0] == '_' && p[1] == '_' 4513 && p[len - 1] == '_' && p[len - 2] == '_') 4514 { 4515 char *newp = (char *) alloca (len - 1); 4516 4517 strcpy (newp, &p[2]); 4518 newp[len - 4] = '\0'; 4519 p = newp; 4520 } 4521 4522 /* Change this type to have a type with the specified mode. 4523 First check for the special modes. */ 4524 if (!strcmp (p, "byte")) 4525 mode = byte_mode; 4526 else if (!strcmp (p, "word")) 4527 mode = word_mode; 4528 else if (!strcmp (p, "pointer")) 4529 mode = ptr_mode; 4530 else 4531 for (j = 0; j < NUM_MACHINE_MODES; j++) 4532 if (!strcmp (p, GET_MODE_NAME (j))) 4533 { 4534 mode = (enum machine_mode) j; 4535 break; 4536 } 4537 4538 if (mode == VOIDmode) 4539 { 4540 error ("unknown machine mode %qs", p); 4541 return NULL_TREE; 4542 } 4543 4544 valid_mode = false; 4545 switch (GET_MODE_CLASS (mode)) 4546 { 4547 case MODE_INT: 4548 case MODE_PARTIAL_INT: 4549 case MODE_FLOAT: 4550 valid_mode = targetm.scalar_mode_supported_p (mode); 4551 break; 4552 4553 case MODE_COMPLEX_INT: 4554 case MODE_COMPLEX_FLOAT: 4555 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode)); 4556 break; 4557 4558 case MODE_VECTOR_INT: 4559 case MODE_VECTOR_FLOAT: 4560 warning (OPT_Wattributes, "specifying vector types with " 4561 "__attribute__ ((mode)) is deprecated"); 4562 warning (OPT_Wattributes, 4563 "use __attribute__ ((vector_size)) instead"); 4564 valid_mode = vector_mode_valid_p (mode); 4565 break; 4566 4567 default: 4568 break; 4569 } 4570 if (!valid_mode) 4571 { 4572 error ("unable to emulate %qs", p); 4573 return NULL_TREE; 4574 } 4575 4576 if (POINTER_TYPE_P (type)) 4577 { 4578 tree (*fn)(tree, enum machine_mode, bool); 4579 4580 if (!targetm.valid_pointer_mode (mode)) 4581 { 4582 error ("invalid pointer mode %qs", p); 4583 return NULL_TREE; 4584 } 4585 4586 if (TREE_CODE (type) == POINTER_TYPE) 4587 fn = build_pointer_type_for_mode; 4588 else 4589 fn = build_reference_type_for_mode; 4590 typefm = fn (TREE_TYPE (type), mode, false); 4591 } 4592 else 4593 typefm = lang_hooks.types.type_for_mode (mode, TYPE_UNSIGNED (type)); 4594 4595 if (typefm == NULL_TREE) 4596 { 4597 error ("no data type for mode %qs", p); 4598 return NULL_TREE; 4599 } 4600 else if (TREE_CODE (type) == ENUMERAL_TYPE) 4601 { 4602 /* For enumeral types, copy the precision from the integer 4603 type returned above. If not an INTEGER_TYPE, we can't use 4604 this mode for this type. */ 4605 if (TREE_CODE (typefm) != INTEGER_TYPE) 4606 { 4607 error ("cannot use mode %qs for enumeral types", p); 4608 return NULL_TREE; 4609 } 4610 4611 if (flags & ATTR_FLAG_TYPE_IN_PLACE) 4612 { 4613 TYPE_PRECISION (type) = TYPE_PRECISION (typefm); 4614 typefm = type; 4615 } 4616 else 4617 { 4618 /* We cannot build a type variant, as there's code that assumes 4619 that TYPE_MAIN_VARIANT has the same mode. This includes the 4620 debug generators. Instead, create a subrange type. This 4621 results in all of the enumeral values being emitted only once 4622 in the original, and the subtype gets them by reference. */ 4623 if (TYPE_UNSIGNED (type)) 4624 typefm = make_unsigned_type (TYPE_PRECISION (typefm)); 4625 else 4626 typefm = make_signed_type (TYPE_PRECISION (typefm)); 4627 TREE_TYPE (typefm) = type; 4628 } 4629 } 4630 else if (VECTOR_MODE_P (mode) 4631 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm)) 4632 : TREE_CODE (type) != TREE_CODE (typefm)) 4633 { 4634 error ("mode %qs applied to inappropriate type", p); 4635 return NULL_TREE; 4636 } 4637 4638 *node = typefm; 4639 } 4640 4641 return NULL_TREE; 4642} 4643 4644/* Handle a "section" attribute; arguments as in 4645 struct attribute_spec.handler. */ 4646 4647static tree 4648handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args, 4649 int ARG_UNUSED (flags), bool *no_add_attrs) 4650{ 4651 tree decl = *node; 4652 4653 if (targetm.have_named_sections) 4654 { 4655 user_defined_section_attribute = true; 4656 4657 if ((TREE_CODE (decl) == FUNCTION_DECL 4658 || TREE_CODE (decl) == VAR_DECL) 4659 && TREE_CODE (TREE_VALUE (args)) == STRING_CST) 4660 { 4661 if (TREE_CODE (decl) == VAR_DECL 4662 && current_function_decl != NULL_TREE 4663 && !TREE_STATIC (decl)) 4664 { 4665 error ("%Jsection attribute cannot be specified for " 4666 "local variables", decl); 4667 *no_add_attrs = true; 4668 } 4669 4670 /* The decl may have already been given a section attribute 4671 from a previous declaration. Ensure they match. */ 4672 else if (DECL_SECTION_NAME (decl) != NULL_TREE 4673 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)), 4674 TREE_STRING_POINTER (TREE_VALUE (args))) != 0) 4675 { 4676 error ("section of %q+D conflicts with previous declaration", 4677 *node); 4678 *no_add_attrs = true; 4679 } 4680 else 4681 DECL_SECTION_NAME (decl) = TREE_VALUE (args); 4682 } 4683 else 4684 { 4685 error ("section attribute not allowed for %q+D", *node); 4686 *no_add_attrs = true; 4687 } 4688 } 4689 else 4690 { 4691 error ("%Jsection attributes are not supported for this target", *node); 4692 *no_add_attrs = true; 4693 } 4694 4695 return NULL_TREE; 4696} 4697 4698/* Handle a "aligned" attribute; arguments as in 4699 struct attribute_spec.handler. */ 4700 4701static tree 4702handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args, 4703 int flags, bool *no_add_attrs) 4704{ 4705 tree decl = NULL_TREE; 4706 tree *type = NULL; 4707 int is_type = 0; 4708 tree align_expr = (args ? TREE_VALUE (args) 4709 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT)); 4710 int i; 4711 4712 if (DECL_P (*node)) 4713 { 4714 decl = *node; 4715 type = &TREE_TYPE (decl); 4716 is_type = TREE_CODE (*node) == TYPE_DECL; 4717 } 4718 else if (TYPE_P (*node)) 4719 type = node, is_type = 1; 4720 4721 if (TREE_CODE (align_expr) != INTEGER_CST) 4722 { 4723 error ("requested alignment is not a constant"); 4724 *no_add_attrs = true; 4725 } 4726 else if ((i = tree_log2 (align_expr)) == -1) 4727 { 4728 error ("requested alignment is not a power of 2"); 4729 *no_add_attrs = true; 4730 } 4731 else if (i > HOST_BITS_PER_INT - 2) 4732 { 4733 error ("requested alignment is too large"); 4734 *no_add_attrs = true; 4735 } 4736 else if (is_type) 4737 { 4738 /* If we have a TYPE_DECL, then copy the type, so that we 4739 don't accidentally modify a builtin type. See pushdecl. */ 4740 if (decl && TREE_TYPE (decl) != error_mark_node 4741 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE) 4742 { 4743 tree tt = TREE_TYPE (decl); 4744 *type = build_variant_type_copy (*type); 4745 DECL_ORIGINAL_TYPE (decl) = tt; 4746 TYPE_NAME (*type) = decl; 4747 TREE_USED (*type) = TREE_USED (decl); 4748 TREE_TYPE (decl) = *type; 4749 } 4750 else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) 4751 *type = build_variant_type_copy (*type); 4752 4753 TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT; 4754 TYPE_USER_ALIGN (*type) = 1; 4755 } 4756 else if (TREE_CODE (decl) != VAR_DECL 4757 && TREE_CODE (decl) != FIELD_DECL) 4758 { 4759 error ("alignment may not be specified for %q+D", decl); 4760 *no_add_attrs = true; 4761 } 4762 else 4763 { 4764 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT; 4765 DECL_USER_ALIGN (decl) = 1; 4766 } 4767 4768 return NULL_TREE; 4769} 4770 4771/* Handle a "weak" attribute; arguments as in 4772 struct attribute_spec.handler. */ 4773 4774static tree 4775handle_weak_attribute (tree *node, tree ARG_UNUSED (name), 4776 tree ARG_UNUSED (args), 4777 int ARG_UNUSED (flags), 4778 bool * ARG_UNUSED (no_add_attrs)) 4779{ 4780 declare_weak (*node); 4781 4782 return NULL_TREE; 4783} 4784 4785/* Handle an "alias" attribute; arguments as in 4786 struct attribute_spec.handler. */ 4787 4788static tree 4789handle_alias_attribute (tree *node, tree name, tree args, 4790 int ARG_UNUSED (flags), bool *no_add_attrs) 4791{ 4792 tree decl = *node; 4793 4794 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl)) 4795 || (TREE_CODE (decl) != FUNCTION_DECL && !DECL_EXTERNAL (decl))) 4796 { 4797 error ("%q+D defined both normally and as an alias", decl); 4798 *no_add_attrs = true; 4799 } 4800 4801 /* Note that the very first time we process a nested declaration, 4802 decl_function_context will not be set. Indeed, *would* never 4803 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that 4804 we do below. After such frobbery, pushdecl would set the context. 4805 In any case, this is never what we want. */ 4806 else if (decl_function_context (decl) == 0 && current_function_decl == NULL) 4807 { 4808 tree id; 4809 4810 id = TREE_VALUE (args); 4811 if (TREE_CODE (id) != STRING_CST) 4812 { 4813 error ("alias argument not a string"); 4814 *no_add_attrs = true; 4815 return NULL_TREE; 4816 } 4817 id = get_identifier (TREE_STRING_POINTER (id)); 4818 /* This counts as a use of the object pointed to. */ 4819 TREE_USED (id) = 1; 4820 4821 if (TREE_CODE (decl) == FUNCTION_DECL) 4822 DECL_INITIAL (decl) = error_mark_node; 4823 else 4824 { 4825 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))) 4826 DECL_EXTERNAL (decl) = 1; 4827 else 4828 DECL_EXTERNAL (decl) = 0; 4829 TREE_STATIC (decl) = 1; 4830 } 4831 } 4832 else 4833 { 4834 warning (OPT_Wattributes, "%qE attribute ignored", name); 4835 *no_add_attrs = true; 4836 } 4837 4838 return NULL_TREE; 4839} 4840 4841/* Handle a "weakref" attribute; arguments as in struct 4842 attribute_spec.handler. */ 4843 4844static tree 4845handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args, 4846 int flags, bool *no_add_attrs) 4847{ 4848 tree attr = NULL_TREE; 4849 4850 /* We must ignore the attribute when it is associated with 4851 local-scoped decls, since attribute alias is ignored and many 4852 such symbols do not even have a DECL_WEAK field. */ 4853 if (decl_function_context (*node) || current_function_decl) 4854 { 4855 warning (OPT_Wattributes, "%qE attribute ignored", name); 4856 *no_add_attrs = true; 4857 return NULL_TREE; 4858 } 4859 4860 /* The idea here is that `weakref("name")' mutates into `weakref, 4861 alias("name")', and weakref without arguments, in turn, 4862 implicitly adds weak. */ 4863 4864 if (args) 4865 { 4866 attr = tree_cons (get_identifier ("alias"), args, attr); 4867 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr); 4868 4869 *no_add_attrs = true; 4870 } 4871 else 4872 { 4873 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node))) 4874 error ("%Jweakref attribute must appear before alias attribute", 4875 *node); 4876 4877 attr = tree_cons (get_identifier ("weak"), NULL_TREE, attr); 4878 } 4879 4880 decl_attributes (node, attr, flags); 4881 4882 return NULL_TREE; 4883} 4884 4885/* Handle an "visibility" attribute; arguments as in 4886 struct attribute_spec.handler. */ 4887 4888static tree 4889handle_visibility_attribute (tree *node, tree name, tree args, 4890 int ARG_UNUSED (flags), 4891 bool *no_add_attrs) 4892{ 4893 tree decl = *node; 4894 tree id = TREE_VALUE (args); 4895 4896 *no_add_attrs = true; 4897 4898 if (TYPE_P (*node)) 4899 { 4900 if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE) 4901 { 4902 warning (OPT_Wattributes, "%qE attribute ignored on non-class types", 4903 name); 4904 return NULL_TREE; 4905 } 4906 } 4907 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl)) 4908 { 4909 warning (OPT_Wattributes, "%qE attribute ignored", name); 4910 return NULL_TREE; 4911 } 4912 4913 if (TREE_CODE (id) != STRING_CST) 4914 { 4915 error ("visibility argument not a string"); 4916 return NULL_TREE; 4917 } 4918 4919 /* If this is a type, set the visibility on the type decl. */ 4920 if (TYPE_P (decl)) 4921 { 4922 decl = TYPE_NAME (decl); 4923 if (!decl) 4924 return NULL_TREE; 4925 if (TREE_CODE (decl) == IDENTIFIER_NODE) 4926 { 4927 warning (OPT_Wattributes, "%qE attribute ignored on types", 4928 name); 4929 return NULL_TREE; 4930 } 4931 } 4932 4933 if (strcmp (TREE_STRING_POINTER (id), "default") == 0) 4934 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; 4935 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0) 4936 DECL_VISIBILITY (decl) = VISIBILITY_INTERNAL; 4937 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0) 4938 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN; 4939 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0) 4940 DECL_VISIBILITY (decl) = VISIBILITY_PROTECTED; 4941 else 4942 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\""); 4943 DECL_VISIBILITY_SPECIFIED (decl) = 1; 4944 4945 /* For decls only, go ahead and attach the attribute to the node as well. 4946 This is needed so we can determine whether we have VISIBILITY_DEFAULT 4947 because the visibility was not specified, or because it was explicitly 4948 overridden from the class visibility. */ 4949 if (DECL_P (*node)) 4950 *no_add_attrs = false; 4951 4952 return NULL_TREE; 4953} 4954 4955/* Determine the ELF symbol visibility for DECL, which is either a 4956 variable or a function. It is an error to use this function if a 4957 definition of DECL is not available in this translation unit. 4958 Returns true if the final visibility has been determined by this 4959 function; false if the caller is free to make additional 4960 modifications. */ 4961 4962bool 4963c_determine_visibility (tree decl) 4964{ 4965 gcc_assert (TREE_CODE (decl) == VAR_DECL 4966 || TREE_CODE (decl) == FUNCTION_DECL); 4967 4968 /* If the user explicitly specified the visibility with an 4969 attribute, honor that. DECL_VISIBILITY will have been set during 4970 the processing of the attribute. We check for an explicit 4971 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED, 4972 to distinguish the use of an attribute from the use of a "#pragma 4973 GCC visibility push(...)"; in the latter case we still want other 4974 considerations to be able to overrule the #pragma. */ 4975 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))) 4976 return true; 4977 4978 /* Anything that is exported must have default visibility. */ 4979 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES 4980 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))) 4981 { 4982 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; 4983 DECL_VISIBILITY_SPECIFIED (decl) = 1; 4984 return true; 4985 } 4986 4987 return false; 4988} 4989 4990/* Handle an "tls_model" attribute; arguments as in 4991 struct attribute_spec.handler. */ 4992 4993static tree 4994handle_tls_model_attribute (tree *node, tree name, tree args, 4995 int ARG_UNUSED (flags), bool *no_add_attrs) 4996{ 4997 tree id; 4998 tree decl = *node; 4999 enum tls_model kind; 5000 5001 *no_add_attrs = true; 5002 5003 if (!DECL_THREAD_LOCAL_P (decl)) 5004 { 5005 warning (OPT_Wattributes, "%qE attribute ignored", name); 5006 return NULL_TREE; 5007 } 5008 5009 kind = DECL_TLS_MODEL (decl); 5010 id = TREE_VALUE (args); 5011 if (TREE_CODE (id) != STRING_CST) 5012 { 5013 error ("tls_model argument not a string"); 5014 return NULL_TREE; 5015 } 5016 5017 if (!strcmp (TREE_STRING_POINTER (id), "local-exec")) 5018 kind = TLS_MODEL_LOCAL_EXEC; 5019 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec")) 5020 kind = TLS_MODEL_INITIAL_EXEC; 5021 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic")) 5022 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC; 5023 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic")) 5024 kind = TLS_MODEL_GLOBAL_DYNAMIC; 5025 else 5026 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\""); 5027 5028 DECL_TLS_MODEL (decl) = kind; 5029 return NULL_TREE; 5030} 5031 5032/* Handle a "no_instrument_function" attribute; arguments as in 5033 struct attribute_spec.handler. */ 5034 5035static tree 5036handle_no_instrument_function_attribute (tree *node, tree name, 5037 tree ARG_UNUSED (args), 5038 int ARG_UNUSED (flags), 5039 bool *no_add_attrs) 5040{ 5041 tree decl = *node; 5042 5043 if (TREE_CODE (decl) != FUNCTION_DECL) 5044 { 5045 error ("%J%qE attribute applies only to functions", decl, name); 5046 *no_add_attrs = true; 5047 } 5048 else if (DECL_INITIAL (decl)) 5049 { 5050 error ("%Jcan%'t set %qE attribute after definition", decl, name); 5051 *no_add_attrs = true; 5052 } 5053 else 5054 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; 5055 5056 return NULL_TREE; 5057} 5058/* Handle a "no_instrument_function" attribute; arguments as in 5059 struct attribute_spec.handler. */ 5060 5061static tree 5062handle_no_stack_protector_function_attribute (tree *node, tree name, 5063 tree ARG_UNUSED (args), 5064 int ARG_UNUSED (flags), 5065 bool *no_add_attrs) 5066{ 5067 tree decl = *node; 5068 5069 if (TREE_CODE (decl) != FUNCTION_DECL) 5070 { 5071 error ("%J%qE attribute applies only to functions", decl, name); 5072 *no_add_attrs = true; 5073 } 5074 else if (DECL_INITIAL (decl)) 5075 { 5076 error ("%Jcan%'t set %qE attribute after definition", decl, name); 5077 *no_add_attrs = true; 5078 } 5079 else 5080 DECL_NO_STACK_PROTECTOR_FUNCTION (decl) = 1; 5081 5082 return NULL_TREE; 5083} 5084 5085/* Handle a "malloc" attribute; arguments as in 5086 struct attribute_spec.handler. */ 5087 5088static tree 5089handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args), 5090 int ARG_UNUSED (flags), bool *no_add_attrs) 5091{ 5092 if (TREE_CODE (*node) == FUNCTION_DECL 5093 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))) 5094 DECL_IS_MALLOC (*node) = 1; 5095 else 5096 { 5097 warning (OPT_Wattributes, "%qE attribute ignored", name); 5098 *no_add_attrs = true; 5099 } 5100 5101 return NULL_TREE; 5102} 5103 5104/* Handle a "returns_twice" attribute; arguments as in 5105 struct attribute_spec.handler. */ 5106 5107static tree 5108handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args), 5109 int ARG_UNUSED (flags), bool *no_add_attrs) 5110{ 5111 if (TREE_CODE (*node) == FUNCTION_DECL) 5112 DECL_IS_RETURNS_TWICE (*node) = 1; 5113 else 5114 { 5115 warning (OPT_Wattributes, "%qE attribute ignored", name); 5116 *no_add_attrs = true; 5117 } 5118 5119 return NULL_TREE; 5120} 5121 5122/* Handle a "no_limit_stack" attribute; arguments as in 5123 struct attribute_spec.handler. */ 5124 5125static tree 5126handle_no_limit_stack_attribute (tree *node, tree name, 5127 tree ARG_UNUSED (args), 5128 int ARG_UNUSED (flags), 5129 bool *no_add_attrs) 5130{ 5131 tree decl = *node; 5132 5133 if (TREE_CODE (decl) != FUNCTION_DECL) 5134 { 5135 error ("%J%qE attribute applies only to functions", decl, name); 5136 *no_add_attrs = true; 5137 } 5138 else if (DECL_INITIAL (decl)) 5139 { 5140 error ("%Jcan%'t set %qE attribute after definition", decl, name); 5141 *no_add_attrs = true; 5142 } 5143 else 5144 DECL_NO_LIMIT_STACK (decl) = 1; 5145 5146 return NULL_TREE; 5147} 5148 5149/* Handle a "pure" attribute; arguments as in 5150 struct attribute_spec.handler. */ 5151 5152static tree 5153handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args), 5154 int ARG_UNUSED (flags), bool *no_add_attrs) 5155{ 5156 if (TREE_CODE (*node) == FUNCTION_DECL) 5157 DECL_IS_PURE (*node) = 1; 5158 /* ??? TODO: Support types. */ 5159 else 5160 { 5161 warning (OPT_Wattributes, "%qE attribute ignored", name); 5162 *no_add_attrs = true; 5163 } 5164 5165 return NULL_TREE; 5166} 5167 5168/* Handle a "no vops" attribute; arguments as in 5169 struct attribute_spec.handler. */ 5170 5171static tree 5172handle_novops_attribute (tree *node, tree ARG_UNUSED (name), 5173 tree ARG_UNUSED (args), int ARG_UNUSED (flags), 5174 bool *ARG_UNUSED (no_add_attrs)) 5175{ 5176 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL); 5177 DECL_IS_NOVOPS (*node) = 1; 5178 return NULL_TREE; 5179} 5180 5181/* Handle a "deprecated" attribute; arguments as in 5182 struct attribute_spec.handler. */ 5183 5184static tree 5185handle_deprecated_attribute (tree *node, tree name, 5186 tree ARG_UNUSED (args), int flags, 5187 bool *no_add_attrs) 5188{ 5189 tree type = NULL_TREE; 5190 int warn = 0; 5191 tree what = NULL_TREE; 5192 5193 if (DECL_P (*node)) 5194 { 5195 tree decl = *node; 5196 type = TREE_TYPE (decl); 5197 5198 if (TREE_CODE (decl) == TYPE_DECL 5199 || TREE_CODE (decl) == PARM_DECL 5200 || TREE_CODE (decl) == VAR_DECL 5201 || TREE_CODE (decl) == FUNCTION_DECL 5202 || TREE_CODE (decl) == FIELD_DECL) 5203 TREE_DEPRECATED (decl) = 1; 5204 else 5205 warn = 1; 5206 } 5207 else if (TYPE_P (*node)) 5208 { 5209 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) 5210 *node = build_variant_type_copy (*node); 5211 TREE_DEPRECATED (*node) = 1; 5212 type = *node; 5213 } 5214 else 5215 warn = 1; 5216 5217 if (warn) 5218 { 5219 *no_add_attrs = true; 5220 if (type && TYPE_NAME (type)) 5221 { 5222 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) 5223 what = TYPE_NAME (*node); 5224 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL 5225 && DECL_NAME (TYPE_NAME (type))) 5226 what = DECL_NAME (TYPE_NAME (type)); 5227 } 5228 if (what) 5229 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what); 5230 else 5231 warning (OPT_Wattributes, "%qE attribute ignored", name); 5232 } 5233 5234 return NULL_TREE; 5235} 5236 5237/* Handle a "vector_size" attribute; arguments as in 5238 struct attribute_spec.handler. */ 5239 5240static tree 5241handle_vector_size_attribute (tree *node, tree name, tree args, 5242 int ARG_UNUSED (flags), 5243 bool *no_add_attrs) 5244{ 5245 unsigned HOST_WIDE_INT vecsize, nunits; 5246 enum machine_mode orig_mode; 5247 tree type = *node, new_type, size; 5248 5249 *no_add_attrs = true; 5250 5251 size = TREE_VALUE (args); 5252 5253 if (!host_integerp (size, 1)) 5254 { 5255 warning (OPT_Wattributes, "%qE attribute ignored", name); 5256 return NULL_TREE; 5257 } 5258 5259 /* Get the vector size (in bytes). */ 5260 vecsize = tree_low_cst (size, 1); 5261 5262 /* We need to provide for vector pointers, vector arrays, and 5263 functions returning vectors. For example: 5264 5265 __attribute__((vector_size(16))) short *foo; 5266 5267 In this case, the mode is SI, but the type being modified is 5268 HI, so we need to look further. */ 5269 5270 while (POINTER_TYPE_P (type) 5271 || TREE_CODE (type) == FUNCTION_TYPE 5272 || TREE_CODE (type) == METHOD_TYPE 5273 || TREE_CODE (type) == ARRAY_TYPE) 5274 type = TREE_TYPE (type); 5275 5276 /* Get the mode of the type being modified. */ 5277 orig_mode = TYPE_MODE (type); 5278 5279 if (TREE_CODE (type) == RECORD_TYPE 5280 || TREE_CODE (type) == UNION_TYPE 5281 || TREE_CODE (type) == VECTOR_TYPE 5282 || (GET_MODE_CLASS (orig_mode) != MODE_FLOAT 5283 && GET_MODE_CLASS (orig_mode) != MODE_INT) 5284 || !host_integerp (TYPE_SIZE_UNIT (type), 1)) 5285 { 5286 error ("invalid vector type for attribute %qE", name); 5287 return NULL_TREE; 5288 } 5289 5290 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1)) 5291 { 5292 error ("vector size not an integral multiple of component size"); 5293 return NULL; 5294 } 5295 5296 if (vecsize == 0) 5297 { 5298 error ("zero vector size"); 5299 return NULL; 5300 } 5301 5302 /* Calculate how many units fit in the vector. */ 5303 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1); 5304 if (nunits & (nunits - 1)) 5305 { 5306 error ("number of components of the vector not a power of two"); 5307 return NULL_TREE; 5308 } 5309 5310 new_type = build_vector_type (type, nunits); 5311 5312 /* Build back pointers if needed. */ 5313 *node = reconstruct_complex_type (*node, new_type); 5314 5315 return NULL_TREE; 5316} 5317 5318/* Handle the "nonnull" attribute. */ 5319static tree 5320handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name), 5321 tree args, int ARG_UNUSED (flags), 5322 bool *no_add_attrs) 5323{ 5324 tree type = *node; 5325 unsigned HOST_WIDE_INT attr_arg_num; 5326 5327 /* If no arguments are specified, all pointer arguments should be 5328 non-null. Verify a full prototype is given so that the arguments 5329 will have the correct types when we actually check them later. */ 5330 if (!args) 5331 { 5332 if (!TYPE_ARG_TYPES (type)) 5333 { 5334 error ("nonnull attribute without arguments on a non-prototype"); 5335 *no_add_attrs = true; 5336 } 5337 return NULL_TREE; 5338 } 5339 5340 /* Argument list specified. Verify that each argument number references 5341 a pointer argument. */ 5342 for (attr_arg_num = 1; args; args = TREE_CHAIN (args)) 5343 { 5344 tree argument; 5345 unsigned HOST_WIDE_INT arg_num = 0, ck_num; 5346 5347 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num)) 5348 { 5349 error ("nonnull argument has invalid operand number (argument %lu)", 5350 (unsigned long) attr_arg_num); 5351 *no_add_attrs = true; 5352 return NULL_TREE; 5353 } 5354 5355 argument = TYPE_ARG_TYPES (type); 5356 if (argument) 5357 { 5358 for (ck_num = 1; ; ck_num++) 5359 { 5360 if (!argument || ck_num == arg_num) 5361 break; 5362 argument = TREE_CHAIN (argument); 5363 } 5364 5365 if (!argument 5366 || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE) 5367 { 5368 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)", 5369 (unsigned long) attr_arg_num, (unsigned long) arg_num); 5370 *no_add_attrs = true; 5371 return NULL_TREE; 5372 } 5373 5374 if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE) 5375 { 5376 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)", 5377 (unsigned long) attr_arg_num, (unsigned long) arg_num); 5378 *no_add_attrs = true; 5379 return NULL_TREE; 5380 } 5381 } 5382 } 5383 5384 return NULL_TREE; 5385} 5386 5387/* Check the argument list of a function call for null in argument slots 5388 that are marked as requiring a non-null pointer argument. */ 5389 5390static void 5391check_function_nonnull (tree attrs, tree params) 5392{ 5393 tree a, args, param; 5394 int param_num; 5395 5396 for (a = attrs; a; a = TREE_CHAIN (a)) 5397 { 5398 if (is_attribute_p ("nonnull", TREE_PURPOSE (a))) 5399 { 5400 args = TREE_VALUE (a); 5401 5402 /* Walk the argument list. If we encounter an argument number we 5403 should check for non-null, do it. If the attribute has no args, 5404 then every pointer argument is checked (in which case the check 5405 for pointer type is done in check_nonnull_arg). */ 5406 for (param = params, param_num = 1; ; 5407 param_num++, param = TREE_CHAIN (param)) 5408 { 5409 if (!param) 5410 break; 5411 if (!args || nonnull_check_p (args, param_num)) 5412 check_function_arguments_recurse (check_nonnull_arg, NULL, 5413 TREE_VALUE (param), 5414 param_num); 5415 } 5416 } 5417 } 5418} 5419 5420/* Check that the Nth argument of a function call (counting backwards 5421 from the end) is a (pointer)0. */ 5422 5423static void 5424check_function_sentinel (tree attrs, tree params, tree typelist) 5425{ 5426 tree attr = lookup_attribute ("sentinel", attrs); 5427 5428 if (attr) 5429 { 5430 /* Skip over the named arguments. */ 5431 while (typelist && params) 5432 { 5433 typelist = TREE_CHAIN (typelist); 5434 params = TREE_CHAIN (params); 5435 } 5436 5437 if (typelist || !params) 5438 warning (OPT_Wformat, 5439 "not enough variable arguments to fit a sentinel"); 5440 else 5441 { 5442 tree sentinel, end; 5443 unsigned pos = 0; 5444 5445 if (TREE_VALUE (attr)) 5446 { 5447 tree p = TREE_VALUE (TREE_VALUE (attr)); 5448 pos = TREE_INT_CST_LOW (p); 5449 } 5450 5451 sentinel = end = params; 5452 5453 /* Advance `end' ahead of `sentinel' by `pos' positions. */ 5454 while (pos > 0 && TREE_CHAIN (end)) 5455 { 5456 pos--; 5457 end = TREE_CHAIN (end); 5458 } 5459 if (pos > 0) 5460 { 5461 warning (OPT_Wformat, 5462 "not enough variable arguments to fit a sentinel"); 5463 return; 5464 } 5465 5466 /* Now advance both until we find the last parameter. */ 5467 while (TREE_CHAIN (end)) 5468 { 5469 end = TREE_CHAIN (end); 5470 sentinel = TREE_CHAIN (sentinel); 5471 } 5472 5473 /* Validate the sentinel. */ 5474 if ((!POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (sentinel))) 5475 || !integer_zerop (TREE_VALUE (sentinel))) 5476 /* Although __null (in C++) is only an integer we allow it 5477 nevertheless, as we are guaranteed that it's exactly 5478 as wide as a pointer, and we don't want to force 5479 users to cast the NULL they have written there. 5480 We warn with -Wstrict-null-sentinel, though. */ 5481 && (warn_strict_null_sentinel 5482 || null_node != TREE_VALUE (sentinel))) 5483 warning (OPT_Wformat, "missing sentinel in function call"); 5484 } 5485 } 5486} 5487 5488/* Helper for check_function_nonnull; given a list of operands which 5489 must be non-null in ARGS, determine if operand PARAM_NUM should be 5490 checked. */ 5491 5492static bool 5493nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num) 5494{ 5495 unsigned HOST_WIDE_INT arg_num = 0; 5496 5497 for (; args; args = TREE_CHAIN (args)) 5498 { 5499 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num); 5500 5501 gcc_assert (found); 5502 5503 if (arg_num == param_num) 5504 return true; 5505 } 5506 return false; 5507} 5508 5509/* Check that the function argument PARAM (which is operand number 5510 PARAM_NUM) is non-null. This is called by check_function_nonnull 5511 via check_function_arguments_recurse. */ 5512 5513static void 5514check_nonnull_arg (void * ARG_UNUSED (ctx), tree param, 5515 unsigned HOST_WIDE_INT param_num) 5516{ 5517 /* Just skip checking the argument if it's not a pointer. This can 5518 happen if the "nonnull" attribute was given without an operand 5519 list (which means to check every pointer argument). */ 5520 5521 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE) 5522 return; 5523 5524 if (integer_zerop (param)) 5525 warning (OPT_Wnonnull, "null argument where non-null required " 5526 "(argument %lu)", (unsigned long) param_num); 5527} 5528 5529/* Helper for nonnull attribute handling; fetch the operand number 5530 from the attribute argument list. */ 5531 5532static bool 5533get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp) 5534{ 5535 /* Verify the arg number is a constant. */ 5536 if (TREE_CODE (arg_num_expr) != INTEGER_CST 5537 || TREE_INT_CST_HIGH (arg_num_expr) != 0) 5538 return false; 5539 5540 *valp = TREE_INT_CST_LOW (arg_num_expr); 5541 return true; 5542} 5543 5544/* Handle a "nothrow" attribute; arguments as in 5545 struct attribute_spec.handler. */ 5546 5547static tree 5548handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args), 5549 int ARG_UNUSED (flags), bool *no_add_attrs) 5550{ 5551 if (TREE_CODE (*node) == FUNCTION_DECL) 5552 TREE_NOTHROW (*node) = 1; 5553 /* ??? TODO: Support types. */ 5554 else 5555 { 5556 warning (OPT_Wattributes, "%qE attribute ignored", name); 5557 *no_add_attrs = true; 5558 } 5559 5560 return NULL_TREE; 5561} 5562 5563/* Handle a "cleanup" attribute; arguments as in 5564 struct attribute_spec.handler. */ 5565 5566static tree 5567handle_cleanup_attribute (tree *node, tree name, tree args, 5568 int ARG_UNUSED (flags), bool *no_add_attrs) 5569{ 5570 tree decl = *node; 5571 tree cleanup_id, cleanup_decl; 5572 5573 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do 5574 for global destructors in C++. This requires infrastructure that 5575 we don't have generically at the moment. It's also not a feature 5576 we'd be missing too much, since we do have attribute constructor. */ 5577 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)) 5578 { 5579 warning (OPT_Wattributes, "%qE attribute ignored", name); 5580 *no_add_attrs = true; 5581 return NULL_TREE; 5582 } 5583 5584 /* Verify that the argument is a function in scope. */ 5585 /* ??? We could support pointers to functions here as well, if 5586 that was considered desirable. */ 5587 cleanup_id = TREE_VALUE (args); 5588 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE) 5589 { 5590 error ("cleanup argument not an identifier"); 5591 *no_add_attrs = true; 5592 return NULL_TREE; 5593 } 5594 cleanup_decl = lang_hooks.decls.lookup_name (cleanup_id); 5595 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL) 5596 { 5597 error ("cleanup argument not a function"); 5598 *no_add_attrs = true; 5599 return NULL_TREE; 5600 } 5601 5602 /* That the function has proper type is checked with the 5603 eventual call to build_function_call. */ 5604 5605 return NULL_TREE; 5606} 5607 5608/* Handle a "warn_unused_result" attribute. No special handling. */ 5609 5610static tree 5611handle_warn_unused_result_attribute (tree *node, tree name, 5612 tree ARG_UNUSED (args), 5613 int ARG_UNUSED (flags), bool *no_add_attrs) 5614{ 5615 /* Ignore the attribute for functions not returning any value. */ 5616 if (VOID_TYPE_P (TREE_TYPE (*node))) 5617 { 5618 warning (OPT_Wattributes, "%qE attribute ignored", name); 5619 *no_add_attrs = true; 5620 } 5621 5622 return NULL_TREE; 5623} 5624 5625/* Handle a "sentinel" attribute. */ 5626 5627static tree 5628handle_sentinel_attribute (tree *node, tree name, tree args, 5629 int ARG_UNUSED (flags), bool *no_add_attrs) 5630{ 5631 tree params = TYPE_ARG_TYPES (*node); 5632 5633 if (!params) 5634 { 5635 warning (OPT_Wattributes, 5636 "%qE attribute requires prototypes with named arguments", name); 5637 *no_add_attrs = true; 5638 } 5639 else 5640 { 5641 while (TREE_CHAIN (params)) 5642 params = TREE_CHAIN (params); 5643 5644 if (VOID_TYPE_P (TREE_VALUE (params))) 5645 { 5646 warning (OPT_Wattributes, 5647 "%qE attribute only applies to variadic functions", name); 5648 *no_add_attrs = true; 5649 } 5650 } 5651 5652 if (args) 5653 { 5654 tree position = TREE_VALUE (args); 5655 5656 if (TREE_CODE (position) != INTEGER_CST) 5657 { 5658 warning (0, "requested position is not an integer constant"); 5659 *no_add_attrs = true; 5660 } 5661 else 5662 { 5663 if (tree_int_cst_lt (position, integer_zero_node)) 5664 { 5665 warning (0, "requested position is less than zero"); 5666 *no_add_attrs = true; 5667 } 5668 } 5669 } 5670 5671 return NULL_TREE; 5672} 5673 5674/* Check for valid arguments being passed to a function. */ 5675void 5676check_function_arguments (tree attrs, tree params, tree typelist) 5677{ 5678 /* Check for null being passed in a pointer argument that must be 5679 non-null. We also need to do this if format checking is enabled. */ 5680 5681 if (warn_nonnull) 5682 check_function_nonnull (attrs, params); 5683 5684 /* Check for errors in format strings. */ 5685 5686 if (warn_format || warn_missing_format_attribute) 5687 check_function_format (attrs, params); 5688 5689 if (warn_format) 5690 check_function_sentinel (attrs, params, typelist); 5691} 5692 5693/* Generic argument checking recursion routine. PARAM is the argument to 5694 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked 5695 once the argument is resolved. CTX is context for the callback. */ 5696void 5697check_function_arguments_recurse (void (*callback) 5698 (void *, tree, unsigned HOST_WIDE_INT), 5699 void *ctx, tree param, 5700 unsigned HOST_WIDE_INT param_num) 5701{ 5702 if (TREE_CODE (param) == NOP_EXPR) 5703 { 5704 /* Strip coercion. */ 5705 check_function_arguments_recurse (callback, ctx, 5706 TREE_OPERAND (param, 0), param_num); 5707 return; 5708 } 5709 5710 if (TREE_CODE (param) == CALL_EXPR) 5711 { 5712 tree type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (param, 0))); 5713 tree attrs; 5714 bool found_format_arg = false; 5715 5716 /* See if this is a call to a known internationalization function 5717 that modifies a format arg. Such a function may have multiple 5718 format_arg attributes (for example, ngettext). */ 5719 5720 for (attrs = TYPE_ATTRIBUTES (type); 5721 attrs; 5722 attrs = TREE_CHAIN (attrs)) 5723 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs))) 5724 { 5725 tree inner_args; 5726 tree format_num_expr; 5727 int format_num; 5728 int i; 5729 5730 /* Extract the argument number, which was previously checked 5731 to be valid. */ 5732 format_num_expr = TREE_VALUE (TREE_VALUE (attrs)); 5733 5734 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST 5735 && !TREE_INT_CST_HIGH (format_num_expr)); 5736 5737 format_num = TREE_INT_CST_LOW (format_num_expr); 5738 5739 for (inner_args = TREE_OPERAND (param, 1), i = 1; 5740 inner_args != 0; 5741 inner_args = TREE_CHAIN (inner_args), i++) 5742 if (i == format_num) 5743 { 5744 check_function_arguments_recurse (callback, ctx, 5745 TREE_VALUE (inner_args), 5746 param_num); 5747 found_format_arg = true; 5748 break; 5749 } 5750 } 5751 5752 /* If we found a format_arg attribute and did a recursive check, 5753 we are done with checking this argument. Otherwise, we continue 5754 and this will be considered a non-literal. */ 5755 if (found_format_arg) 5756 return; 5757 } 5758 5759 if (TREE_CODE (param) == COND_EXPR) 5760 { 5761 /* Check both halves of the conditional expression. */ 5762 check_function_arguments_recurse (callback, ctx, 5763 TREE_OPERAND (param, 1), param_num); 5764 check_function_arguments_recurse (callback, ctx, 5765 TREE_OPERAND (param, 2), param_num); 5766 return; 5767 } 5768 5769 (*callback) (ctx, param, param_num); 5770} 5771 5772/* Function to help qsort sort FIELD_DECLs by name order. */ 5773 5774int 5775field_decl_cmp (const void *x_p, const void *y_p) 5776{ 5777 const tree *const x = (const tree *const) x_p; 5778 const tree *const y = (const tree *const) y_p; 5779 5780 if (DECL_NAME (*x) == DECL_NAME (*y)) 5781 /* A nontype is "greater" than a type. */ 5782 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); 5783 if (DECL_NAME (*x) == NULL_TREE) 5784 return -1; 5785 if (DECL_NAME (*y) == NULL_TREE) 5786 return 1; 5787 if (DECL_NAME (*x) < DECL_NAME (*y)) 5788 return -1; 5789 return 1; 5790} 5791 5792static struct { 5793 gt_pointer_operator new_value; 5794 void *cookie; 5795} resort_data; 5796 5797/* This routine compares two fields like field_decl_cmp but using the 5798pointer operator in resort_data. */ 5799 5800static int 5801resort_field_decl_cmp (const void *x_p, const void *y_p) 5802{ 5803 const tree *const x = (const tree *const) x_p; 5804 const tree *const y = (const tree *const) y_p; 5805 5806 if (DECL_NAME (*x) == DECL_NAME (*y)) 5807 /* A nontype is "greater" than a type. */ 5808 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); 5809 if (DECL_NAME (*x) == NULL_TREE) 5810 return -1; 5811 if (DECL_NAME (*y) == NULL_TREE) 5812 return 1; 5813 { 5814 tree d1 = DECL_NAME (*x); 5815 tree d2 = DECL_NAME (*y); 5816 resort_data.new_value (&d1, resort_data.cookie); 5817 resort_data.new_value (&d2, resort_data.cookie); 5818 if (d1 < d2) 5819 return -1; 5820 } 5821 return 1; 5822} 5823 5824/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */ 5825 5826void 5827resort_sorted_fields (void *obj, 5828 void * ARG_UNUSED (orig_obj), 5829 gt_pointer_operator new_value, 5830 void *cookie) 5831{ 5832 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj; 5833 resort_data.new_value = new_value; 5834 resort_data.cookie = cookie; 5835 qsort (&sf->elts[0], sf->len, sizeof (tree), 5836 resort_field_decl_cmp); 5837} 5838 5839/* Subroutine of c_parse_error. 5840 Return the result of concatenating LHS and RHS. RHS is really 5841 a string literal, its first character is indicated by RHS_START and 5842 RHS_SIZE is its length (including the terminating NUL character). 5843 5844 The caller is responsible for deleting the returned pointer. */ 5845 5846static char * 5847catenate_strings (const char *lhs, const char *rhs_start, int rhs_size) 5848{ 5849 const int lhs_size = strlen (lhs); 5850 char *result = XNEWVEC (char, lhs_size + rhs_size); 5851 strncpy (result, lhs, lhs_size); 5852 strncpy (result + lhs_size, rhs_start, rhs_size); 5853 return result; 5854} 5855 5856/* Issue the error given by GMSGID, indicating that it occurred before 5857 TOKEN, which had the associated VALUE. */ 5858 5859void 5860c_parse_error (const char *gmsgid, enum cpp_ttype token, tree value) 5861{ 5862#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2)) 5863 5864 char *message = NULL; 5865 5866 if (token == CPP_EOF) 5867 message = catenate_messages (gmsgid, " at end of input"); 5868 else if (token == CPP_CHAR || token == CPP_WCHAR) 5869 { 5870 unsigned int val = TREE_INT_CST_LOW (value); 5871 const char *const ell = (token == CPP_CHAR) ? "" : "L"; 5872 if (val <= UCHAR_MAX && ISGRAPH (val)) 5873 message = catenate_messages (gmsgid, " before %s'%c'"); 5874 else 5875 message = catenate_messages (gmsgid, " before %s'\\x%x'"); 5876 5877 error (message, ell, val); 5878 free (message); 5879 message = NULL; 5880 } 5881 else if (token == CPP_STRING || token == CPP_WSTRING) 5882 message = catenate_messages (gmsgid, " before string constant"); 5883 else if (token == CPP_NUMBER) 5884 message = catenate_messages (gmsgid, " before numeric constant"); 5885 else if (token == CPP_NAME) 5886 { 5887 message = catenate_messages (gmsgid, " before %qE"); 5888 error (message, value); 5889 free (message); 5890 message = NULL; 5891 } 5892 else if (token < N_TTYPES) 5893 { 5894 message = catenate_messages (gmsgid, " before %qs token"); 5895 error (message, cpp_type2name (token)); 5896 free (message); 5897 message = NULL; 5898 } 5899 else 5900 error (gmsgid); 5901 5902 if (message) 5903 { 5904 error (message); 5905 free (message); 5906 } 5907#undef catenate_messages 5908} 5909 5910/* Walk a gimplified function and warn for functions whose return value is 5911 ignored and attribute((warn_unused_result)) is set. This is done before 5912 inlining, so we don't have to worry about that. */ 5913 5914void 5915c_warn_unused_result (tree *top_p) 5916{ 5917 tree t = *top_p; 5918 tree_stmt_iterator i; 5919 tree fdecl, ftype; 5920 5921 switch (TREE_CODE (t)) 5922 { 5923 case STATEMENT_LIST: 5924 for (i = tsi_start (*top_p); !tsi_end_p (i); tsi_next (&i)) 5925 c_warn_unused_result (tsi_stmt_ptr (i)); 5926 break; 5927 5928 case COND_EXPR: 5929 c_warn_unused_result (&COND_EXPR_THEN (t)); 5930 c_warn_unused_result (&COND_EXPR_ELSE (t)); 5931 break; 5932 case BIND_EXPR: 5933 c_warn_unused_result (&BIND_EXPR_BODY (t)); 5934 break; 5935 case TRY_FINALLY_EXPR: 5936 case TRY_CATCH_EXPR: 5937 c_warn_unused_result (&TREE_OPERAND (t, 0)); 5938 c_warn_unused_result (&TREE_OPERAND (t, 1)); 5939 break; 5940 case CATCH_EXPR: 5941 c_warn_unused_result (&CATCH_BODY (t)); 5942 break; 5943 case EH_FILTER_EXPR: 5944 c_warn_unused_result (&EH_FILTER_FAILURE (t)); 5945 break; 5946 5947 case CALL_EXPR: 5948 if (TREE_USED (t)) 5949 break; 5950 5951 /* This is a naked call, as opposed to a CALL_EXPR nested inside 5952 a MODIFY_EXPR. All calls whose value is ignored should be 5953 represented like this. Look for the attribute. */ 5954 fdecl = get_callee_fndecl (t); 5955 if (fdecl) 5956 ftype = TREE_TYPE (fdecl); 5957 else 5958 { 5959 ftype = TREE_TYPE (TREE_OPERAND (t, 0)); 5960 /* Look past pointer-to-function to the function type itself. */ 5961 ftype = TREE_TYPE (ftype); 5962 } 5963 5964 if (lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (ftype))) 5965 { 5966 if (fdecl) 5967 warning (0, "%Hignoring return value of %qD, " 5968 "declared with attribute warn_unused_result", 5969 EXPR_LOCUS (t), fdecl); 5970 else 5971 warning (0, "%Hignoring return value of function " 5972 "declared with attribute warn_unused_result", 5973 EXPR_LOCUS (t)); 5974 } 5975 break; 5976 5977 default: 5978 /* Not a container, not a call, or a call whose value is used. */ 5979 break; 5980 } 5981} 5982 5983/* Convert a character from the host to the target execution character 5984 set. cpplib handles this, mostly. */ 5985 5986HOST_WIDE_INT 5987c_common_to_target_charset (HOST_WIDE_INT c) 5988{ 5989 /* Character constants in GCC proper are sign-extended under -fsigned-char, 5990 zero-extended under -fno-signed-char. cpplib insists that characters 5991 and character constants are always unsigned. Hence we must convert 5992 back and forth. */ 5993 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1); 5994 5995 uc = cpp_host_to_exec_charset (parse_in, uc); 5996 5997 if (flag_signed_char) 5998 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE) 5999 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE); 6000 else 6001 return uc; 6002} 6003 6004/* Build the result of __builtin_offsetof. EXPR is a nested sequence of 6005 component references, with an INDIRECT_REF at the bottom; much like 6006 the traditional rendering of offsetof as a macro. Returns the folded 6007 and properly cast result. */ 6008 6009static tree 6010fold_offsetof_1 (tree expr) 6011{ 6012 enum tree_code code = PLUS_EXPR; 6013 tree base, off, t; 6014 6015 switch (TREE_CODE (expr)) 6016 { 6017 case ERROR_MARK: 6018 return expr; 6019 6020 case VAR_DECL: 6021 error ("cannot apply %<offsetof%> to static data member %qD", expr); 6022 return error_mark_node; 6023 6024 case INDIRECT_REF: 6025 return size_zero_node; 6026 6027 case COMPONENT_REF: 6028 base = fold_offsetof_1 (TREE_OPERAND (expr, 0)); 6029 if (base == error_mark_node) 6030 return base; 6031 6032 t = TREE_OPERAND (expr, 1); 6033 if (DECL_C_BIT_FIELD (t)) 6034 { 6035 error ("attempt to take address of bit-field structure " 6036 "member %qD", t); 6037 return error_mark_node; 6038 } 6039 off = size_binop (PLUS_EXPR, DECL_FIELD_OFFSET (t), 6040 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t), 1) 6041 / BITS_PER_UNIT)); 6042 break; 6043 6044 case ARRAY_REF: 6045 base = fold_offsetof_1 (TREE_OPERAND (expr, 0)); 6046 if (base == error_mark_node) 6047 return base; 6048 6049 t = TREE_OPERAND (expr, 1); 6050 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) < 0) 6051 { 6052 code = MINUS_EXPR; 6053 t = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t); 6054 } 6055 t = convert (sizetype, t); 6056 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t); 6057 break; 6058 6059 case COMPOUND_EXPR: 6060 /* Handle static members of volatile structs. */ 6061 t = TREE_OPERAND (expr, 1); 6062 gcc_assert (TREE_CODE (t) == VAR_DECL); 6063 return fold_offsetof_1 (t); 6064 6065 default: 6066 gcc_unreachable (); 6067 } 6068 6069 return size_binop (code, base, off); 6070} 6071 6072tree 6073fold_offsetof (tree expr) 6074{ 6075 /* Convert back from the internal sizetype to size_t. */ 6076 return convert (size_type_node, fold_offsetof_1 (expr)); 6077} 6078 6079/* Print an error message for an invalid lvalue. USE says 6080 how the lvalue is being used and so selects the error message. */ 6081 6082void 6083lvalue_error (enum lvalue_use use) 6084{ 6085 switch (use) 6086 { 6087 case lv_assign: 6088 error ("invalid lvalue in assignment"); 6089 break; 6090 case lv_increment: 6091 error ("invalid lvalue in increment"); 6092 break; 6093 case lv_decrement: 6094 error ("invalid lvalue in decrement"); 6095 break; 6096 case lv_addressof: 6097 error ("invalid lvalue in unary %<&%>"); 6098 break; 6099 case lv_asm: 6100 error ("invalid lvalue in asm statement"); 6101 break; 6102 default: 6103 gcc_unreachable (); 6104 } 6105} 6106 6107/* *PTYPE is an incomplete array. Complete it with a domain based on 6108 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT 6109 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, 6110 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */ 6111 6112int 6113complete_array_type (tree *ptype, tree initial_value, bool do_default) 6114{ 6115 tree maxindex, type, main_type, elt, unqual_elt; 6116 int failure = 0, quals; 6117 6118 maxindex = size_zero_node; 6119 if (initial_value) 6120 { 6121 if (TREE_CODE (initial_value) == STRING_CST) 6122 { 6123 int eltsize 6124 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); 6125 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1); 6126 } 6127 else if (TREE_CODE (initial_value) == CONSTRUCTOR) 6128 { 6129 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value); 6130 6131 if (VEC_empty (constructor_elt, v)) 6132 { 6133 if (pedantic) 6134 failure = 3; 6135 maxindex = integer_minus_one_node; 6136 } 6137 else 6138 { 6139 tree curindex; 6140 unsigned HOST_WIDE_INT cnt; 6141 constructor_elt *ce; 6142 6143 if (VEC_index (constructor_elt, v, 0)->index) 6144 maxindex = fold_convert (sizetype, 6145 VEC_index (constructor_elt, 6146 v, 0)->index); 6147 curindex = maxindex; 6148 6149 for (cnt = 1; 6150 VEC_iterate (constructor_elt, v, cnt, ce); 6151 cnt++) 6152 { 6153 if (ce->index) 6154 curindex = fold_convert (sizetype, ce->index); 6155 else 6156 curindex = size_binop (PLUS_EXPR, curindex, size_one_node); 6157 6158 if (tree_int_cst_lt (maxindex, curindex)) 6159 maxindex = curindex; 6160 } 6161 } 6162 } 6163 else 6164 { 6165 /* Make an error message unless that happened already. */ 6166 if (initial_value != error_mark_node) 6167 failure = 1; 6168 } 6169 } 6170 else 6171 { 6172 failure = 2; 6173 if (!do_default) 6174 return failure; 6175 } 6176 6177 type = *ptype; 6178 elt = TREE_TYPE (type); 6179 quals = TYPE_QUALS (strip_array_types (elt)); 6180 if (quals == 0) 6181 unqual_elt = elt; 6182 else 6183 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED); 6184 6185 /* Using build_distinct_type_copy and modifying things afterward instead 6186 of using build_array_type to create a new type preserves all of the 6187 TYPE_LANG_FLAG_? bits that the front end may have set. */ 6188 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); 6189 TREE_TYPE (main_type) = unqual_elt; 6190 TYPE_DOMAIN (main_type) = build_index_type (maxindex); 6191 layout_type (main_type); 6192 6193 if (quals == 0) 6194 type = main_type; 6195 else 6196 type = c_build_qualified_type (main_type, quals); 6197 6198 *ptype = type; 6199 return failure; 6200} 6201 6202 6203/* Used to help initialize the builtin-types.def table. When a type of 6204 the correct size doesn't exist, use error_mark_node instead of NULL. 6205 The later results in segfaults even when a decl using the type doesn't 6206 get invoked. */ 6207 6208tree 6209builtin_type_for_size (int size, bool unsignedp) 6210{ 6211 tree type = lang_hooks.types.type_for_size (size, unsignedp); 6212 return type ? type : error_mark_node; 6213} 6214 6215/* A helper function for resolve_overloaded_builtin in resolving the 6216 overloaded __sync_ builtins. Returns a positive power of 2 if the 6217 first operand of PARAMS is a pointer to a supported data type. 6218 Returns 0 if an error is encountered. */ 6219 6220static int 6221sync_resolve_size (tree function, tree params) 6222{ 6223 tree type; 6224 int size; 6225 6226 if (params == NULL) 6227 { 6228 error ("too few arguments to function %qE", function); 6229 return 0; 6230 } 6231 6232 type = TREE_TYPE (TREE_VALUE (params)); 6233 if (TREE_CODE (type) != POINTER_TYPE) 6234 goto incompatible; 6235 6236 type = TREE_TYPE (type); 6237 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type)) 6238 goto incompatible; 6239 6240 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1); 6241 if (size == 1 || size == 2 || size == 4 || size == 8) 6242 return size; 6243 6244 incompatible: 6245 error ("incompatible type for argument %d of %qE", 1, function); 6246 return 0; 6247} 6248 6249/* A helper function for resolve_overloaded_builtin. Adds casts to 6250 PARAMS to make arguments match up with those of FUNCTION. Drops 6251 the variadic arguments at the end. Returns false if some error 6252 was encountered; true on success. */ 6253 6254static bool 6255sync_resolve_params (tree orig_function, tree function, tree params) 6256{ 6257 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (function)); 6258 tree ptype; 6259 int number; 6260 6261 /* We've declared the implementation functions to use "volatile void *" 6262 as the pointer parameter, so we shouldn't get any complaints from the 6263 call to check_function_arguments what ever type the user used. */ 6264 arg_types = TREE_CHAIN (arg_types); 6265 ptype = TREE_TYPE (TREE_TYPE (TREE_VALUE (params))); 6266 number = 2; 6267 6268 /* For the rest of the values, we need to cast these to FTYPE, so that we 6269 don't get warnings for passing pointer types, etc. */ 6270 while (arg_types != void_list_node) 6271 { 6272 tree val; 6273 6274 params = TREE_CHAIN (params); 6275 if (params == NULL) 6276 { 6277 error ("too few arguments to function %qE", orig_function); 6278 return false; 6279 } 6280 6281 /* ??? Ideally for the first conversion we'd use convert_for_assignment 6282 so that we get warnings for anything that doesn't match the pointer 6283 type. This isn't portable across the C and C++ front ends atm. */ 6284 val = TREE_VALUE (params); 6285 val = convert (ptype, val); 6286 val = convert (TREE_VALUE (arg_types), val); 6287 TREE_VALUE (params) = val; 6288 6289 arg_types = TREE_CHAIN (arg_types); 6290 number++; 6291 } 6292 6293 /* The definition of these primitives is variadic, with the remaining 6294 being "an optional list of variables protected by the memory barrier". 6295 No clue what that's supposed to mean, precisely, but we consider all 6296 call-clobbered variables to be protected so we're safe. */ 6297 TREE_CHAIN (params) = NULL; 6298 6299 return true; 6300} 6301 6302/* A helper function for resolve_overloaded_builtin. Adds a cast to 6303 RESULT to make it match the type of the first pointer argument in 6304 PARAMS. */ 6305 6306static tree 6307sync_resolve_return (tree params, tree result) 6308{ 6309 tree ptype = TREE_TYPE (TREE_TYPE (TREE_VALUE (params))); 6310 ptype = TYPE_MAIN_VARIANT (ptype); 6311 return convert (ptype, result); 6312} 6313 6314/* Some builtin functions are placeholders for other expressions. This 6315 function should be called immediately after parsing the call expression 6316 before surrounding code has committed to the type of the expression. 6317 6318 FUNCTION is the DECL that has been invoked; it is known to be a builtin. 6319 PARAMS is the argument list for the call. The return value is non-null 6320 when expansion is complete, and null if normal processing should 6321 continue. */ 6322 6323tree 6324resolve_overloaded_builtin (tree function, tree params) 6325{ 6326 enum built_in_function orig_code = DECL_FUNCTION_CODE (function); 6327 switch (DECL_BUILT_IN_CLASS (function)) 6328 { 6329 case BUILT_IN_NORMAL: 6330 break; 6331 case BUILT_IN_MD: 6332 if (targetm.resolve_overloaded_builtin) 6333 return targetm.resolve_overloaded_builtin (function, params); 6334 else 6335 return NULL_TREE; 6336 default: 6337 return NULL_TREE; 6338 } 6339 6340 /* Handle BUILT_IN_NORMAL here. */ 6341 switch (orig_code) 6342 { 6343 case BUILT_IN_FETCH_AND_ADD_N: 6344 case BUILT_IN_FETCH_AND_SUB_N: 6345 case BUILT_IN_FETCH_AND_OR_N: 6346 case BUILT_IN_FETCH_AND_AND_N: 6347 case BUILT_IN_FETCH_AND_XOR_N: 6348 case BUILT_IN_FETCH_AND_NAND_N: 6349 case BUILT_IN_ADD_AND_FETCH_N: 6350 case BUILT_IN_SUB_AND_FETCH_N: 6351 case BUILT_IN_OR_AND_FETCH_N: 6352 case BUILT_IN_AND_AND_FETCH_N: 6353 case BUILT_IN_XOR_AND_FETCH_N: 6354 case BUILT_IN_NAND_AND_FETCH_N: 6355 case BUILT_IN_BOOL_COMPARE_AND_SWAP_N: 6356 case BUILT_IN_VAL_COMPARE_AND_SWAP_N: 6357 case BUILT_IN_LOCK_TEST_AND_SET_N: 6358 case BUILT_IN_LOCK_RELEASE_N: 6359 { 6360 int n = sync_resolve_size (function, params); 6361 tree new_function, result; 6362 6363 if (n == 0) 6364 return error_mark_node; 6365 6366 new_function = built_in_decls[orig_code + exact_log2 (n) + 1]; 6367 if (!sync_resolve_params (function, new_function, params)) 6368 return error_mark_node; 6369 6370 result = build_function_call (new_function, params); 6371 if (orig_code != BUILT_IN_BOOL_COMPARE_AND_SWAP_N 6372 && orig_code != BUILT_IN_LOCK_RELEASE_N) 6373 result = sync_resolve_return (params, result); 6374 6375 return result; 6376 } 6377 6378 default: 6379 return NULL_TREE; 6380 } 6381} 6382 6383/* Ignoring their sign, return true if two scalar types are the same. */ 6384bool 6385same_scalar_type_ignoring_signedness (tree t1, tree t2) 6386{ 6387 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2); 6388 6389 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE) 6390 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE)); 6391 6392 /* Equality works here because c_common_signed_type uses 6393 TYPE_MAIN_VARIANT. */ 6394 return lang_hooks.types.signed_type (t1) 6395 == lang_hooks.types.signed_type (t2); 6396} 6397 6398/* Check for missing format attributes on function pointers. LTYPE is 6399 the new type or left-hand side type. RTYPE is the old type or 6400 right-hand side type. Returns TRUE if LTYPE is missing the desired 6401 attribute. */ 6402 6403bool 6404check_missing_format_attribute (tree ltype, tree rtype) 6405{ 6406 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype); 6407 tree ra; 6408 6409 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra)) 6410 if (is_attribute_p ("format", TREE_PURPOSE (ra))) 6411 break; 6412 if (ra) 6413 { 6414 tree la; 6415 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la)) 6416 if (is_attribute_p ("format", TREE_PURPOSE (la))) 6417 break; 6418 return !la; 6419 } 6420 else 6421 return false; 6422} 6423 6424#include "gt-c-common.h" 6425