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