1 /* Process declarations and variables for C compiler. 2 Copyright (C) 1988-2018 Free Software Foundation, Inc. 3 4 This file is part of GCC. 5 6 GCC is free software; you can redistribute it and/or modify it under 7 the terms of the GNU General Public License as published by the Free 8 Software Foundation; either version 3, or (at your option) any later 9 version. 10 11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12 WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GCC; see the file COPYING3. If not see 18 <http://www.gnu.org/licenses/>. */ 19 20 /* Process declarations and symbol lookup for C front end. 21 Also constructs types; the standard scalar types at initialization, 22 and structure, union, array and enum types when they are declared. */ 23 24 /* ??? not all decl nodes are given the most useful possible 25 line numbers. For example, the CONST_DECLs for enum values. */ 26 27 #include "config.h" 28 #define INCLUDE_UNIQUE_PTR 29 #include "system.h" 30 #include "coretypes.h" 31 #include "target.h" 32 #include "function.h" 33 #include "c-tree.h" 34 #include "timevar.h" 35 #include "stringpool.h" 36 #include "cgraph.h" 37 #include "intl.h" 38 #include "print-tree.h" 39 #include "stor-layout.h" 40 #include "varasm.h" 41 #include "attribs.h" 42 #include "toplev.h" 43 #include "debug.h" 44 #include "c-family/c-objc.h" 45 #include "c-family/c-pragma.h" 46 #include "c-family/c-ubsan.h" 47 #include "c-lang.h" 48 #include "langhooks.h" 49 #include "tree-iterator.h" 50 #include "dumpfile.h" 51 #include "plugin.h" 52 #include "c-family/c-ada-spec.h" 53 #include "builtins.h" 54 #include "spellcheck-tree.h" 55 #include "gcc-rich-location.h" 56 #include "asan.h" 57 #include "c-family/name-hint.h" 58 #include "c-family/known-headers.h" 59 #include "c-family/c-spellcheck.h" 60 61 /* In grokdeclarator, distinguish syntactic contexts of declarators. */ 62 enum decl_context 63 { NORMAL, /* Ordinary declaration */ 64 FUNCDEF, /* Function definition */ 65 PARM, /* Declaration of parm before function body */ 66 FIELD, /* Declaration inside struct or union */ 67 TYPENAME}; /* Typename (inside cast or sizeof) */ 68 69 /* States indicating how grokdeclarator() should handle declspecs marked 70 with __attribute__((deprecated)). An object declared as 71 __attribute__((deprecated)) suppresses warnings of uses of other 72 deprecated items. */ 73 74 enum deprecated_states { 75 DEPRECATED_NORMAL, 76 DEPRECATED_SUPPRESS 77 }; 78 79 80 /* Nonzero if we have seen an invalid cross reference 81 to a struct, union, or enum, but not yet printed the message. */ 82 tree pending_invalid_xref; 83 84 /* File and line to appear in the eventual error message. */ 85 location_t pending_invalid_xref_location; 86 87 /* The file and line that the prototype came from if this is an 88 old-style definition; used for diagnostics in 89 store_parm_decls_oldstyle. */ 90 91 static location_t current_function_prototype_locus; 92 93 /* Whether this prototype was built-in. */ 94 95 static bool current_function_prototype_built_in; 96 97 /* The argument type information of this prototype. */ 98 99 static tree current_function_prototype_arg_types; 100 101 /* The argument information structure for the function currently being 102 defined. */ 103 104 static struct c_arg_info *current_function_arg_info; 105 106 /* The obstack on which parser and related data structures, which are 107 not live beyond their top-level declaration or definition, are 108 allocated. */ 109 struct obstack parser_obstack; 110 111 /* The current statement tree. */ 112 113 static GTY(()) struct stmt_tree_s c_stmt_tree; 114 115 /* State saving variables. */ 116 tree c_break_label; 117 tree c_cont_label; 118 119 /* A list of decls to be made automatically visible in each file scope. */ 120 static GTY(()) tree visible_builtins; 121 122 /* Set to 0 at beginning of a function definition, set to 1 if 123 a return statement that specifies a return value is seen. */ 124 125 int current_function_returns_value; 126 127 /* Set to 0 at beginning of a function definition, set to 1 if 128 a return statement with no argument is seen. */ 129 130 int current_function_returns_null; 131 132 /* Set to 0 at beginning of a function definition, set to 1 if 133 a call to a noreturn function is seen. */ 134 135 int current_function_returns_abnormally; 136 137 /* Set to nonzero by `grokdeclarator' for a function 138 whose return type is defaulted, if warnings for this are desired. */ 139 140 static int warn_about_return_type; 141 142 /* Nonzero when the current toplevel function contains a declaration 143 of a nested function which is never defined. */ 144 145 static bool undef_nested_function; 146 147 /* If non-zero, implicit "omp declare target" attribute is added into the 148 attribute lists. */ 149 int current_omp_declare_target_attribute; 150 151 /* Each c_binding structure describes one binding of an identifier to 152 a decl. All the decls in a scope - irrespective of namespace - are 153 chained together by the ->prev field, which (as the name implies) 154 runs in reverse order. All the decls in a given namespace bound to 155 a given identifier are chained by the ->shadowed field, which runs 156 from inner to outer scopes. 157 158 The ->decl field usually points to a DECL node, but there are two 159 exceptions. In the namespace of type tags, the bound entity is a 160 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared 161 identifier is encountered, it is bound to error_mark_node to 162 suppress further errors about that identifier in the current 163 function. 164 165 The ->u.type field stores the type of the declaration in this scope; 166 if NULL, the type is the type of the ->decl field. This is only of 167 relevance for objects with external or internal linkage which may 168 be redeclared in inner scopes, forming composite types that only 169 persist for the duration of those scopes. In the external scope, 170 this stores the composite of all the types declared for this 171 object, visible or not. The ->inner_comp field (used only at file 172 scope) stores whether an incomplete array type at file scope was 173 completed at an inner scope to an array size other than 1. 174 175 The ->u.label field is used for labels. It points to a structure 176 which stores additional information used for warnings. 177 178 The depth field is copied from the scope structure that holds this 179 decl. It is used to preserve the proper ordering of the ->shadowed 180 field (see bind()) and also for a handful of special-case checks. 181 Finally, the invisible bit is true for a decl which should be 182 ignored for purposes of normal name lookup, and the nested bit is 183 true for a decl that's been bound a second time in an inner scope; 184 in all such cases, the binding in the outer scope will have its 185 invisible bit true. */ 186 187 struct GTY((chain_next ("%h.prev"))) c_binding { 188 union GTY(()) { /* first so GTY desc can use decl */ 189 tree GTY((tag ("0"))) type; /* the type in this scope */ 190 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */ 191 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u; 192 tree decl; /* the decl bound */ 193 tree id; /* the identifier it's bound to */ 194 struct c_binding *prev; /* the previous decl in this scope */ 195 struct c_binding *shadowed; /* the innermost decl shadowed by this one */ 196 unsigned int depth : 28; /* depth of this scope */ 197 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */ 198 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */ 199 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */ 200 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */ 201 location_t locus; /* location for nested bindings */ 202 }; 203 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth) 204 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth) 205 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/) 206 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/) 207 208 /* Each C symbol points to three linked lists of c_binding structures. 209 These describe the values of the identifier in the three different 210 namespaces defined by the language. */ 211 212 struct GTY(()) lang_identifier { 213 struct c_common_identifier common_id; 214 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */ 215 struct c_binding *tag_binding; /* struct/union/enum tags */ 216 struct c_binding *label_binding; /* labels */ 217 }; 218 219 /* Validate c-lang.c's assumptions. */ 220 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate 221 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1]; 222 223 /* The binding oracle; see c-tree.h. */ 224 void (*c_binding_oracle) (enum c_oracle_request, tree identifier); 225 226 /* This flag is set on an identifier if we have previously asked the 227 binding oracle for this identifier's symbol binding. */ 228 #define I_SYMBOL_CHECKED(node) \ 229 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node))) 230 231 static inline struct c_binding* * 232 i_symbol_binding (tree node) 233 { 234 struct lang_identifier *lid 235 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node); 236 237 if (lid->symbol_binding == NULL 238 && c_binding_oracle != NULL 239 && !I_SYMBOL_CHECKED (node)) 240 { 241 /* Set the "checked" flag first, to avoid infinite recursion 242 when the binding oracle calls back into gcc. */ 243 I_SYMBOL_CHECKED (node) = 1; 244 c_binding_oracle (C_ORACLE_SYMBOL, node); 245 } 246 247 return &lid->symbol_binding; 248 } 249 250 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node)) 251 252 #define I_SYMBOL_DECL(node) \ 253 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0) 254 255 /* This flag is set on an identifier if we have previously asked the 256 binding oracle for this identifier's tag binding. */ 257 #define I_TAG_CHECKED(node) \ 258 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node))) 259 260 static inline struct c_binding ** 261 i_tag_binding (tree node) 262 { 263 struct lang_identifier *lid 264 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node); 265 266 if (lid->tag_binding == NULL 267 && c_binding_oracle != NULL 268 && !I_TAG_CHECKED (node)) 269 { 270 /* Set the "checked" flag first, to avoid infinite recursion 271 when the binding oracle calls back into gcc. */ 272 I_TAG_CHECKED (node) = 1; 273 c_binding_oracle (C_ORACLE_TAG, node); 274 } 275 276 return &lid->tag_binding; 277 } 278 279 #define I_TAG_BINDING(node) (*i_tag_binding (node)) 280 281 #define I_TAG_DECL(node) \ 282 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0) 283 284 /* This flag is set on an identifier if we have previously asked the 285 binding oracle for this identifier's label binding. */ 286 #define I_LABEL_CHECKED(node) \ 287 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node))) 288 289 static inline struct c_binding ** 290 i_label_binding (tree node) 291 { 292 struct lang_identifier *lid 293 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node); 294 295 if (lid->label_binding == NULL 296 && c_binding_oracle != NULL 297 && !I_LABEL_CHECKED (node)) 298 { 299 /* Set the "checked" flag first, to avoid infinite recursion 300 when the binding oracle calls back into gcc. */ 301 I_LABEL_CHECKED (node) = 1; 302 c_binding_oracle (C_ORACLE_LABEL, node); 303 } 304 305 return &lid->label_binding; 306 } 307 308 #define I_LABEL_BINDING(node) (*i_label_binding (node)) 309 310 #define I_LABEL_DECL(node) \ 311 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0) 312 313 /* The resulting tree type. */ 314 315 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"), 316 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node 317 { 318 union tree_node GTY ((tag ("0"), 319 desc ("tree_node_structure (&%h)"))) 320 generic; 321 struct lang_identifier GTY ((tag ("1"))) identifier; 322 }; 323 324 /* Track bindings and other things that matter for goto warnings. For 325 efficiency, we do not gather all the decls at the point of 326 definition. Instead, we point into the bindings structure. As 327 scopes are popped, we update these structures and gather the decls 328 that matter at that time. */ 329 330 struct GTY(()) c_spot_bindings { 331 /* The currently open scope which holds bindings defined when the 332 label was defined or the goto statement was found. */ 333 struct c_scope *scope; 334 /* The bindings in the scope field which were defined at the point 335 of the label or goto. This lets us look at older or newer 336 bindings in the scope, as appropriate. */ 337 struct c_binding *bindings_in_scope; 338 /* The number of statement expressions that have started since this 339 label or goto statement was defined. This is zero if we are at 340 the same statement expression level. It is positive if we are in 341 a statement expression started since this spot. It is negative 342 if this spot was in a statement expression and we have left 343 it. */ 344 int stmt_exprs; 345 /* Whether we started in a statement expression but are no longer in 346 it. This is set to true if stmt_exprs ever goes negative. */ 347 bool left_stmt_expr; 348 }; 349 350 /* This structure is used to keep track of bindings seen when a goto 351 statement is defined. This is only used if we see the goto 352 statement before we see the label. */ 353 354 struct GTY(()) c_goto_bindings { 355 /* The location of the goto statement. */ 356 location_t loc; 357 /* The bindings of the goto statement. */ 358 struct c_spot_bindings goto_bindings; 359 }; 360 361 typedef struct c_goto_bindings *c_goto_bindings_p; 362 363 /* The additional information we keep track of for a label binding. 364 These fields are updated as scopes are popped. */ 365 366 struct GTY(()) c_label_vars { 367 /* The shadowed c_label_vars, when one label shadows another (which 368 can only happen using a __label__ declaration). */ 369 struct c_label_vars *shadowed; 370 /* The bindings when the label was defined. */ 371 struct c_spot_bindings label_bindings; 372 /* A list of decls that we care about: decls about which we should 373 warn if a goto branches to this label from later in the function. 374 Decls are added to this list as scopes are popped. We only add 375 the decls that matter. */ 376 vec<tree, va_gc> *decls_in_scope; 377 /* A list of goto statements to this label. This is only used for 378 goto statements seen before the label was defined, so that we can 379 issue appropriate warnings for them. */ 380 vec<c_goto_bindings_p, va_gc> *gotos; 381 }; 382 383 /* Each c_scope structure describes the complete contents of one 384 scope. Four scopes are distinguished specially: the innermost or 385 current scope, the innermost function scope, the file scope (always 386 the second to outermost) and the outermost or external scope. 387 388 Most declarations are recorded in the current scope. 389 390 All normal label declarations are recorded in the innermost 391 function scope, as are bindings of undeclared identifiers to 392 error_mark_node. (GCC permits nested functions as an extension, 393 hence the 'innermost' qualifier.) Explicitly declared labels 394 (using the __label__ extension) appear in the current scope. 395 396 Being in the file scope (current_scope == file_scope) causes 397 special behavior in several places below. Also, under some 398 conditions the Objective-C front end records declarations in the 399 file scope even though that isn't the current scope. 400 401 All declarations with external linkage are recorded in the external 402 scope, even if they aren't visible there; this models the fact that 403 such declarations are visible to the entire program, and (with a 404 bit of cleverness, see pushdecl) allows diagnosis of some violations 405 of C99 6.2.2p7 and 6.2.7p2: 406 407 If, within the same translation unit, the same identifier appears 408 with both internal and external linkage, the behavior is 409 undefined. 410 411 All declarations that refer to the same object or function shall 412 have compatible type; otherwise, the behavior is undefined. 413 414 Initially only the built-in declarations, which describe compiler 415 intrinsic functions plus a subset of the standard library, are in 416 this scope. 417 418 The order of the blocks list matters, and it is frequently appended 419 to. To avoid having to walk all the way to the end of the list on 420 each insertion, or reverse the list later, we maintain a pointer to 421 the last list entry. (FIXME: It should be feasible to use a reversed 422 list here.) 423 424 The bindings list is strictly in reverse order of declarations; 425 pop_scope relies on this. */ 426 427 428 struct GTY((chain_next ("%h.outer"))) c_scope { 429 /* The scope containing this one. */ 430 struct c_scope *outer; 431 432 /* The next outermost function scope. */ 433 struct c_scope *outer_function; 434 435 /* All bindings in this scope. */ 436 struct c_binding *bindings; 437 438 /* For each scope (except the global one), a chain of BLOCK nodes 439 for all the scopes that were entered and exited one level down. */ 440 tree blocks; 441 tree blocks_last; 442 443 /* The depth of this scope. Used to keep the ->shadowed chain of 444 bindings sorted innermost to outermost. */ 445 unsigned int depth : 28; 446 447 /* True if we are currently filling this scope with parameter 448 declarations. */ 449 BOOL_BITFIELD parm_flag : 1; 450 451 /* True if we saw [*] in this scope. Used to give an error messages 452 if these appears in a function definition. */ 453 BOOL_BITFIELD had_vla_unspec : 1; 454 455 /* True if we already complained about forward parameter decls 456 in this scope. This prevents double warnings on 457 foo (int a; int b; ...) */ 458 BOOL_BITFIELD warned_forward_parm_decls : 1; 459 460 /* True if this is the outermost block scope of a function body. 461 This scope contains the parameters, the local variables declared 462 in the outermost block, and all the labels (except those in 463 nested functions, or declared at block scope with __label__). */ 464 BOOL_BITFIELD function_body : 1; 465 466 /* True means make a BLOCK for this scope no matter what. */ 467 BOOL_BITFIELD keep : 1; 468 469 /* True means that an unsuffixed float constant is _Decimal64. */ 470 BOOL_BITFIELD float_const_decimal64 : 1; 471 472 /* True if this scope has any label bindings. This is used to speed 473 up searching for labels when popping scopes, particularly since 474 labels are normally only found at function scope. */ 475 BOOL_BITFIELD has_label_bindings : 1; 476 477 /* True if we should issue a warning if a goto statement crosses any 478 of the bindings. We still need to check the list of bindings to 479 find the specific ones we need to warn about. This is true if 480 decl_jump_unsafe would return true for any of the bindings. This 481 is used to avoid looping over all the bindings unnecessarily. */ 482 BOOL_BITFIELD has_jump_unsafe_decl : 1; 483 }; 484 485 /* The scope currently in effect. */ 486 487 static GTY(()) struct c_scope *current_scope; 488 489 /* The innermost function scope. Ordinary (not explicitly declared) 490 labels, bindings to error_mark_node, and the lazily-created 491 bindings of __func__ and its friends get this scope. */ 492 493 static GTY(()) struct c_scope *current_function_scope; 494 495 /* The C file scope. This is reset for each input translation unit. */ 496 497 static GTY(()) struct c_scope *file_scope; 498 499 /* The outermost scope. This is used for all declarations with 500 external linkage, and only these, hence the name. */ 501 502 static GTY(()) struct c_scope *external_scope; 503 504 /* A chain of c_scope structures awaiting reuse. */ 505 506 static GTY((deletable)) struct c_scope *scope_freelist; 507 508 /* A chain of c_binding structures awaiting reuse. */ 509 510 static GTY((deletable)) struct c_binding *binding_freelist; 511 512 /* Append VAR to LIST in scope SCOPE. */ 513 #define SCOPE_LIST_APPEND(scope, list, decl) do { \ 514 struct c_scope *s_ = (scope); \ 515 tree d_ = (decl); \ 516 if (s_->list##_last) \ 517 BLOCK_CHAIN (s_->list##_last) = d_; \ 518 else \ 519 s_->list = d_; \ 520 s_->list##_last = d_; \ 521 } while (0) 522 523 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */ 524 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \ 525 struct c_scope *t_ = (tscope); \ 526 struct c_scope *f_ = (fscope); \ 527 if (t_->to##_last) \ 528 BLOCK_CHAIN (t_->to##_last) = f_->from; \ 529 else \ 530 t_->to = f_->from; \ 531 t_->to##_last = f_->from##_last; \ 532 } while (0) 533 534 /* A c_inline_static structure stores details of a static identifier 535 referenced in a definition of a function that may be an inline 536 definition if no subsequent declaration of that function uses 537 "extern" or does not use "inline". */ 538 539 struct GTY((chain_next ("%h.next"))) c_inline_static { 540 /* The location for a diagnostic. */ 541 location_t location; 542 543 /* The function that may be an inline definition. */ 544 tree function; 545 546 /* The object or function referenced. */ 547 tree static_decl; 548 549 /* What sort of reference this is. */ 550 enum c_inline_static_type type; 551 552 /* The next such structure or NULL. */ 553 struct c_inline_static *next; 554 }; 555 556 /* List of static identifiers used or referenced in functions that may 557 be inline definitions. */ 558 static GTY(()) struct c_inline_static *c_inline_statics; 559 560 /* True means unconditionally make a BLOCK for the next scope pushed. */ 561 562 static bool keep_next_level_flag; 563 564 /* True means the next call to push_scope will be the outermost scope 565 of a function body, so do not push a new scope, merely cease 566 expecting parameter decls. */ 567 568 static bool next_is_function_body; 569 570 /* A vector of pointers to c_binding structures. */ 571 572 typedef struct c_binding *c_binding_ptr; 573 574 /* Information that we keep for a struct or union while it is being 575 parsed. */ 576 577 struct c_struct_parse_info 578 { 579 /* If warn_cxx_compat, a list of types defined within this 580 struct. */ 581 auto_vec<tree> struct_types; 582 /* If warn_cxx_compat, a list of field names which have bindings, 583 and which are defined in this struct, but which are not defined 584 in any enclosing struct. This is used to clear the in_struct 585 field of the c_bindings structure. */ 586 auto_vec<c_binding_ptr> fields; 587 /* If warn_cxx_compat, a list of typedef names used when defining 588 fields in this struct. */ 589 auto_vec<tree> typedefs_seen; 590 }; 591 592 /* Information for the struct or union currently being parsed, or 593 NULL if not parsing a struct or union. */ 594 static struct c_struct_parse_info *struct_parse_info; 595 596 /* Forward declarations. */ 597 static tree lookup_name_in_scope (tree, struct c_scope *); 598 static tree c_make_fname_decl (location_t, tree, int); 599 static tree grokdeclarator (const struct c_declarator *, 600 struct c_declspecs *, 601 enum decl_context, bool, tree *, tree *, tree *, 602 bool *, enum deprecated_states); 603 static tree grokparms (struct c_arg_info *, bool); 604 static void layout_array_type (tree); 605 static void warn_defaults_to (location_t, int, const char *, ...) 606 ATTRIBUTE_GCC_DIAG(3,4); 607 608 /* T is a statement. Add it to the statement-tree. This is the 609 C/ObjC version--C++ has a slightly different version of this 610 function. */ 611 612 tree 613 add_stmt (tree t) 614 { 615 enum tree_code code = TREE_CODE (t); 616 617 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR) 618 { 619 if (!EXPR_HAS_LOCATION (t)) 620 SET_EXPR_LOCATION (t, input_location); 621 } 622 623 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR) 624 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1; 625 626 /* Add T to the statement-tree. Non-side-effect statements need to be 627 recorded during statement expressions. */ 628 if (!building_stmt_list_p ()) 629 push_stmt_list (); 630 append_to_statement_list_force (t, &cur_stmt_list); 631 632 return t; 633 } 634 635 /* Build a pointer type using the default pointer mode. */ 636 637 static tree 638 c_build_pointer_type (tree to_type) 639 { 640 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC 641 : TYPE_ADDR_SPACE (to_type); 642 machine_mode pointer_mode; 643 644 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode) 645 pointer_mode = targetm.addr_space.pointer_mode (as); 646 else 647 pointer_mode = c_default_pointer_mode; 648 return build_pointer_type_for_mode (to_type, pointer_mode, false); 649 } 650 651 652 /* Return true if we will want to say something if a goto statement 653 crosses DECL. */ 654 655 static bool 656 decl_jump_unsafe (tree decl) 657 { 658 if (error_operand_p (decl)) 659 return false; 660 661 /* Always warn about crossing variably modified types. */ 662 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL) 663 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE)) 664 return true; 665 666 /* Otherwise, only warn if -Wgoto-misses-init and this is an 667 initialized automatic decl. */ 668 if (warn_jump_misses_init 669 && VAR_P (decl) 670 && !TREE_STATIC (decl) 671 && DECL_INITIAL (decl) != NULL_TREE) 672 return true; 673 674 return false; 675 } 676 677 678 void 679 c_print_identifier (FILE *file, tree node, int indent) 680 { 681 void (*save) (enum c_oracle_request, tree identifier); 682 683 /* Temporarily hide any binding oracle. Without this, calls to 684 debug_tree from the debugger will end up calling into the oracle, 685 making for a confusing debug session. As the oracle isn't needed 686 here for normal operation, it's simplest to suppress it. */ 687 save = c_binding_oracle; 688 c_binding_oracle = NULL; 689 690 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4); 691 print_node (file, "tag", I_TAG_DECL (node), indent + 4); 692 print_node (file, "label", I_LABEL_DECL (node), indent + 4); 693 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN) 694 { 695 tree rid = ridpointers[C_RID_CODE (node)]; 696 indent_to (file, indent + 4); 697 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"", 698 (void *) rid, IDENTIFIER_POINTER (rid)); 699 } 700 701 c_binding_oracle = save; 702 } 703 704 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL, 705 which may be any of several kinds of DECL or TYPE or error_mark_node, 706 in the scope SCOPE. */ 707 static void 708 bind (tree name, tree decl, struct c_scope *scope, bool invisible, 709 bool nested, location_t locus) 710 { 711 struct c_binding *b, **here; 712 713 if (binding_freelist) 714 { 715 b = binding_freelist; 716 binding_freelist = b->prev; 717 } 718 else 719 b = ggc_alloc<c_binding> (); 720 721 b->shadowed = 0; 722 b->decl = decl; 723 b->id = name; 724 b->depth = scope->depth; 725 b->invisible = invisible; 726 b->nested = nested; 727 b->inner_comp = 0; 728 b->in_struct = 0; 729 b->locus = locus; 730 731 b->u.type = NULL; 732 733 b->prev = scope->bindings; 734 scope->bindings = b; 735 736 if (decl_jump_unsafe (decl)) 737 scope->has_jump_unsafe_decl = 1; 738 739 if (!name) 740 return; 741 742 switch (TREE_CODE (decl)) 743 { 744 case LABEL_DECL: here = &I_LABEL_BINDING (name); break; 745 case ENUMERAL_TYPE: 746 case UNION_TYPE: 747 case RECORD_TYPE: here = &I_TAG_BINDING (name); break; 748 case VAR_DECL: 749 case FUNCTION_DECL: 750 case TYPE_DECL: 751 case CONST_DECL: 752 case PARM_DECL: 753 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break; 754 755 default: 756 gcc_unreachable (); 757 } 758 759 /* Locate the appropriate place in the chain of shadowed decls 760 to insert this binding. Normally, scope == current_scope and 761 this does nothing. */ 762 while (*here && (*here)->depth > scope->depth) 763 here = &(*here)->shadowed; 764 765 b->shadowed = *here; 766 *here = b; 767 } 768 769 /* Clear the binding structure B, stick it on the binding_freelist, 770 and return the former value of b->prev. This is used by pop_scope 771 and get_parm_info to iterate destructively over all the bindings 772 from a given scope. */ 773 static struct c_binding * 774 free_binding_and_advance (struct c_binding *b) 775 { 776 struct c_binding *prev = b->prev; 777 778 memset (b, 0, sizeof (struct c_binding)); 779 b->prev = binding_freelist; 780 binding_freelist = b; 781 782 return prev; 783 } 784 785 /* Bind a label. Like bind, but skip fields which aren't used for 786 labels, and add the LABEL_VARS value. */ 787 static void 788 bind_label (tree name, tree label, struct c_scope *scope, 789 struct c_label_vars *label_vars) 790 { 791 struct c_binding *b; 792 793 bind (name, label, scope, /*invisible=*/false, /*nested=*/false, 794 UNKNOWN_LOCATION); 795 796 scope->has_label_bindings = true; 797 798 b = scope->bindings; 799 gcc_assert (b->decl == label); 800 label_vars->shadowed = b->u.label; 801 b->u.label = label_vars; 802 } 803 804 /* Hook called at end of compilation to assume 1 elt 805 for a file-scope tentative array defn that wasn't complete before. */ 806 807 void 808 c_finish_incomplete_decl (tree decl) 809 { 810 if (VAR_P (decl)) 811 { 812 tree type = TREE_TYPE (decl); 813 if (type != error_mark_node 814 && TREE_CODE (type) == ARRAY_TYPE 815 && !DECL_EXTERNAL (decl) 816 && TYPE_DOMAIN (type) == NULL_TREE) 817 { 818 warning_at (DECL_SOURCE_LOCATION (decl), 819 0, "array %q+D assumed to have one element", decl); 820 821 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true); 822 823 relayout_decl (decl); 824 } 825 } 826 } 827 828 /* Record that inline function FUNC contains a reference (location 829 LOC) to static DECL (file-scope or function-local according to 830 TYPE). */ 831 832 void 833 record_inline_static (location_t loc, tree func, tree decl, 834 enum c_inline_static_type type) 835 { 836 c_inline_static *csi = ggc_alloc<c_inline_static> (); 837 csi->location = loc; 838 csi->function = func; 839 csi->static_decl = decl; 840 csi->type = type; 841 csi->next = c_inline_statics; 842 c_inline_statics = csi; 843 } 844 845 /* Check for references to static declarations in inline functions at 846 the end of the translation unit and diagnose them if the functions 847 are still inline definitions. */ 848 849 static void 850 check_inline_statics (void) 851 { 852 struct c_inline_static *csi; 853 for (csi = c_inline_statics; csi; csi = csi->next) 854 { 855 if (DECL_EXTERNAL (csi->function)) 856 switch (csi->type) 857 { 858 case csi_internal: 859 pedwarn (csi->location, 0, 860 "%qD is static but used in inline function %qD " 861 "which is not static", csi->static_decl, csi->function); 862 break; 863 case csi_modifiable: 864 pedwarn (csi->location, 0, 865 "%q+D is static but declared in inline function %qD " 866 "which is not static", csi->static_decl, csi->function); 867 break; 868 default: 869 gcc_unreachable (); 870 } 871 } 872 c_inline_statics = NULL; 873 } 874 875 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it 876 for the current state, otherwise set it to uninitialized. */ 877 878 static void 879 set_spot_bindings (struct c_spot_bindings *p, bool defining) 880 { 881 if (defining) 882 { 883 p->scope = current_scope; 884 p->bindings_in_scope = current_scope->bindings; 885 } 886 else 887 { 888 p->scope = NULL; 889 p->bindings_in_scope = NULL; 890 } 891 p->stmt_exprs = 0; 892 p->left_stmt_expr = false; 893 } 894 895 /* Update spot bindings P as we pop out of SCOPE. Return true if we 896 should push decls for a label. */ 897 898 static bool 899 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p) 900 { 901 if (p->scope != scope) 902 { 903 /* This label or goto is defined in some other scope, or it is a 904 label which is not yet defined. There is nothing to 905 update. */ 906 return false; 907 } 908 909 /* Adjust the spot bindings to refer to the bindings already defined 910 in the enclosing scope. */ 911 p->scope = scope->outer; 912 p->bindings_in_scope = p->scope->bindings; 913 914 return true; 915 } 916 917 /* The Objective-C front-end often needs to determine the current scope. */ 918 919 void * 920 objc_get_current_scope (void) 921 { 922 return current_scope; 923 } 924 925 /* The following function is used only by Objective-C. It needs to live here 926 because it accesses the innards of c_scope. */ 927 928 void 929 objc_mark_locals_volatile (void *enclosing_blk) 930 { 931 struct c_scope *scope; 932 struct c_binding *b; 933 934 for (scope = current_scope; 935 scope && scope != enclosing_blk; 936 scope = scope->outer) 937 { 938 for (b = scope->bindings; b; b = b->prev) 939 objc_volatilize_decl (b->decl); 940 941 /* Do not climb up past the current function. */ 942 if (scope->function_body) 943 break; 944 } 945 } 946 947 /* Return true if we are in the global binding level. */ 948 949 bool 950 global_bindings_p (void) 951 { 952 return current_scope == file_scope; 953 } 954 955 void 956 keep_next_level (void) 957 { 958 keep_next_level_flag = true; 959 } 960 961 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */ 962 963 void 964 set_float_const_decimal64 (void) 965 { 966 current_scope->float_const_decimal64 = true; 967 } 968 969 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */ 970 971 void 972 clear_float_const_decimal64 (void) 973 { 974 current_scope->float_const_decimal64 = false; 975 } 976 977 /* Return nonzero if an unsuffixed float constant is _Decimal64. */ 978 979 bool 980 float_const_decimal64_p (void) 981 { 982 return current_scope->float_const_decimal64; 983 } 984 985 /* Identify this scope as currently being filled with parameters. */ 986 987 void 988 declare_parm_level (void) 989 { 990 current_scope->parm_flag = true; 991 } 992 993 void 994 push_scope (void) 995 { 996 if (next_is_function_body) 997 { 998 /* This is the transition from the parameters to the top level 999 of the function body. These are the same scope 1000 (C99 6.2.1p4,6) so we do not push another scope structure. 1001 next_is_function_body is set only by store_parm_decls, which 1002 in turn is called when and only when we are about to 1003 encounter the opening curly brace for the function body. 1004 1005 The outermost block of a function always gets a BLOCK node, 1006 because the debugging output routines expect that each 1007 function has at least one BLOCK. */ 1008 current_scope->parm_flag = false; 1009 current_scope->function_body = true; 1010 current_scope->keep = true; 1011 current_scope->outer_function = current_function_scope; 1012 current_function_scope = current_scope; 1013 1014 keep_next_level_flag = false; 1015 next_is_function_body = false; 1016 1017 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */ 1018 if (current_scope->outer) 1019 current_scope->float_const_decimal64 1020 = current_scope->outer->float_const_decimal64; 1021 else 1022 current_scope->float_const_decimal64 = false; 1023 } 1024 else 1025 { 1026 struct c_scope *scope; 1027 if (scope_freelist) 1028 { 1029 scope = scope_freelist; 1030 scope_freelist = scope->outer; 1031 } 1032 else 1033 scope = ggc_cleared_alloc<c_scope> (); 1034 1035 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */ 1036 if (current_scope) 1037 scope->float_const_decimal64 = current_scope->float_const_decimal64; 1038 else 1039 scope->float_const_decimal64 = false; 1040 1041 scope->keep = keep_next_level_flag; 1042 scope->outer = current_scope; 1043 scope->depth = current_scope ? (current_scope->depth + 1) : 0; 1044 1045 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but 1046 possible. */ 1047 if (current_scope && scope->depth == 0) 1048 { 1049 scope->depth--; 1050 sorry ("GCC supports only %u nested scopes", scope->depth); 1051 } 1052 1053 current_scope = scope; 1054 keep_next_level_flag = false; 1055 } 1056 } 1057 1058 /* This is called when we are leaving SCOPE. For each label defined 1059 in SCOPE, add any appropriate decls to its decls_in_scope fields. 1060 These are the decls whose initialization will be skipped by a goto 1061 later in the function. */ 1062 1063 static void 1064 update_label_decls (struct c_scope *scope) 1065 { 1066 struct c_scope *s; 1067 1068 s = scope; 1069 while (s != NULL) 1070 { 1071 if (s->has_label_bindings) 1072 { 1073 struct c_binding *b; 1074 1075 for (b = s->bindings; b != NULL; b = b->prev) 1076 { 1077 struct c_label_vars *label_vars; 1078 struct c_binding *b1; 1079 bool hjud; 1080 unsigned int ix; 1081 struct c_goto_bindings *g; 1082 1083 if (TREE_CODE (b->decl) != LABEL_DECL) 1084 continue; 1085 label_vars = b->u.label; 1086 1087 b1 = label_vars->label_bindings.bindings_in_scope; 1088 if (label_vars->label_bindings.scope == NULL) 1089 hjud = false; 1090 else 1091 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl; 1092 if (update_spot_bindings (scope, &label_vars->label_bindings)) 1093 { 1094 /* This label is defined in this scope. */ 1095 if (hjud) 1096 { 1097 for (; b1 != NULL; b1 = b1->prev) 1098 { 1099 /* A goto from later in the function to this 1100 label will never see the initialization 1101 of B1, if any. Save it to issue a 1102 warning if needed. */ 1103 if (decl_jump_unsafe (b1->decl)) 1104 vec_safe_push(label_vars->decls_in_scope, b1->decl); 1105 } 1106 } 1107 } 1108 1109 /* Update the bindings of any goto statements associated 1110 with this label. */ 1111 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g) 1112 update_spot_bindings (scope, &g->goto_bindings); 1113 } 1114 } 1115 1116 /* Don't search beyond the current function. */ 1117 if (s == current_function_scope) 1118 break; 1119 1120 s = s->outer; 1121 } 1122 } 1123 1124 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */ 1125 1126 static void 1127 set_type_context (tree type, tree context) 1128 { 1129 for (type = TYPE_MAIN_VARIANT (type); type; 1130 type = TYPE_NEXT_VARIANT (type)) 1131 TYPE_CONTEXT (type) = context; 1132 } 1133 1134 /* Exit a scope. Restore the state of the identifier-decl mappings 1135 that were in effect when this scope was entered. Return a BLOCK 1136 node containing all the DECLs in this scope that are of interest 1137 to debug info generation. */ 1138 1139 tree 1140 pop_scope (void) 1141 { 1142 struct c_scope *scope = current_scope; 1143 tree block, context, p; 1144 struct c_binding *b; 1145 1146 bool functionbody = scope->function_body; 1147 bool keep = functionbody || scope->keep || scope->bindings; 1148 1149 update_label_decls (scope); 1150 1151 /* If appropriate, create a BLOCK to record the decls for the life 1152 of this function. */ 1153 block = NULL_TREE; 1154 if (keep) 1155 { 1156 block = make_node (BLOCK); 1157 BLOCK_SUBBLOCKS (block) = scope->blocks; 1158 TREE_USED (block) = 1; 1159 1160 /* In each subblock, record that this is its superior. */ 1161 for (p = scope->blocks; p; p = BLOCK_CHAIN (p)) 1162 BLOCK_SUPERCONTEXT (p) = block; 1163 1164 BLOCK_VARS (block) = NULL_TREE; 1165 } 1166 1167 /* The TYPE_CONTEXTs for all of the tagged types belonging to this 1168 scope must be set so that they point to the appropriate 1169 construct, i.e. either to the current FUNCTION_DECL node, or 1170 else to the BLOCK node we just constructed. 1171 1172 Note that for tagged types whose scope is just the formal 1173 parameter list for some function type specification, we can't 1174 properly set their TYPE_CONTEXTs here, because we don't have a 1175 pointer to the appropriate FUNCTION_TYPE node readily available 1176 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged 1177 type nodes get set in `grokdeclarator' as soon as we have created 1178 the FUNCTION_TYPE node which will represent the "scope" for these 1179 "parameter list local" tagged types. */ 1180 if (scope->function_body) 1181 context = current_function_decl; 1182 else if (scope == file_scope) 1183 { 1184 tree file_decl 1185 = build_translation_unit_decl (get_identifier (main_input_filename)); 1186 context = file_decl; 1187 debug_hooks->register_main_translation_unit (file_decl); 1188 } 1189 else 1190 context = block; 1191 1192 /* Clear all bindings in this scope. */ 1193 for (b = scope->bindings; b; b = free_binding_and_advance (b)) 1194 { 1195 p = b->decl; 1196 switch (TREE_CODE (p)) 1197 { 1198 case LABEL_DECL: 1199 /* Warnings for unused labels, errors for undefined labels. */ 1200 if (TREE_USED (p) && !DECL_INITIAL (p)) 1201 { 1202 error ("label %q+D used but not defined", p); 1203 DECL_INITIAL (p) = error_mark_node; 1204 } 1205 else 1206 warn_for_unused_label (p); 1207 1208 /* Labels go in BLOCK_VARS. */ 1209 DECL_CHAIN (p) = BLOCK_VARS (block); 1210 BLOCK_VARS (block) = p; 1211 gcc_assert (I_LABEL_BINDING (b->id) == b); 1212 I_LABEL_BINDING (b->id) = b->shadowed; 1213 1214 /* Also pop back to the shadowed label_vars. */ 1215 release_tree_vector (b->u.label->decls_in_scope); 1216 b->u.label = b->u.label->shadowed; 1217 break; 1218 1219 case ENUMERAL_TYPE: 1220 case UNION_TYPE: 1221 case RECORD_TYPE: 1222 set_type_context (p, context); 1223 1224 /* Types may not have tag-names, in which case the type 1225 appears in the bindings list with b->id NULL. */ 1226 if (b->id) 1227 { 1228 gcc_assert (I_TAG_BINDING (b->id) == b); 1229 I_TAG_BINDING (b->id) = b->shadowed; 1230 } 1231 break; 1232 1233 case FUNCTION_DECL: 1234 /* Propagate TREE_ADDRESSABLE from nested functions to their 1235 containing functions. */ 1236 if (!TREE_ASM_WRITTEN (p) 1237 && DECL_INITIAL (p) != NULL_TREE 1238 && TREE_ADDRESSABLE (p) 1239 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE 1240 && DECL_ABSTRACT_ORIGIN (p) != p) 1241 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1; 1242 if (!TREE_PUBLIC (p) 1243 && !DECL_INITIAL (p) 1244 && !b->nested 1245 && scope != file_scope 1246 && scope != external_scope) 1247 { 1248 error ("nested function %q+D declared but never defined", p); 1249 undef_nested_function = true; 1250 } 1251 else if (DECL_DECLARED_INLINE_P (p) 1252 && TREE_PUBLIC (p) 1253 && !DECL_INITIAL (p)) 1254 { 1255 /* C99 6.7.4p6: "a function with external linkage... declared 1256 with an inline function specifier ... shall also be defined 1257 in the same translation unit." */ 1258 if (!flag_gnu89_inline 1259 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p)) 1260 && scope == external_scope) 1261 pedwarn (input_location, 0, 1262 "inline function %q+D declared but never defined", p); 1263 DECL_EXTERNAL (p) = 1; 1264 } 1265 1266 goto common_symbol; 1267 1268 case VAR_DECL: 1269 /* Warnings for unused variables. */ 1270 if ((!TREE_USED (p) || !DECL_READ_P (p)) 1271 && !TREE_NO_WARNING (p) 1272 && !DECL_IN_SYSTEM_HEADER (p) 1273 && DECL_NAME (p) 1274 && !DECL_ARTIFICIAL (p) 1275 && scope != file_scope 1276 && scope != external_scope) 1277 { 1278 if (!TREE_USED (p)) 1279 warning (OPT_Wunused_variable, "unused variable %q+D", p); 1280 else if (DECL_CONTEXT (p) == current_function_decl) 1281 warning_at (DECL_SOURCE_LOCATION (p), 1282 OPT_Wunused_but_set_variable, 1283 "variable %qD set but not used", p); 1284 } 1285 1286 if (b->inner_comp) 1287 { 1288 error ("type of array %q+D completed incompatibly with" 1289 " implicit initialization", p); 1290 } 1291 1292 /* Fall through. */ 1293 case TYPE_DECL: 1294 case CONST_DECL: 1295 common_symbol: 1296 /* All of these go in BLOCK_VARS, but only if this is the 1297 binding in the home scope. */ 1298 if (!b->nested) 1299 { 1300 DECL_CHAIN (p) = BLOCK_VARS (block); 1301 BLOCK_VARS (block) = p; 1302 } 1303 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope) 1304 { 1305 /* For block local externs add a special 1306 DECL_EXTERNAL decl for debug info generation. */ 1307 tree extp = copy_node (p); 1308 1309 DECL_EXTERNAL (extp) = 1; 1310 TREE_STATIC (extp) = 0; 1311 TREE_PUBLIC (extp) = 1; 1312 DECL_INITIAL (extp) = NULL_TREE; 1313 DECL_LANG_SPECIFIC (extp) = NULL; 1314 DECL_CONTEXT (extp) = current_function_decl; 1315 if (TREE_CODE (p) == FUNCTION_DECL) 1316 { 1317 DECL_RESULT (extp) = NULL_TREE; 1318 DECL_SAVED_TREE (extp) = NULL_TREE; 1319 DECL_STRUCT_FUNCTION (extp) = NULL; 1320 } 1321 if (b->locus != UNKNOWN_LOCATION) 1322 DECL_SOURCE_LOCATION (extp) = b->locus; 1323 DECL_CHAIN (extp) = BLOCK_VARS (block); 1324 BLOCK_VARS (block) = extp; 1325 } 1326 /* If this is the file scope set DECL_CONTEXT of each decl to 1327 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p 1328 work. */ 1329 if (scope == file_scope) 1330 { 1331 DECL_CONTEXT (p) = context; 1332 if (TREE_CODE (p) == TYPE_DECL 1333 && TREE_TYPE (p) != error_mark_node) 1334 set_type_context (TREE_TYPE (p), context); 1335 } 1336 1337 gcc_fallthrough (); 1338 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have 1339 already been put there by store_parm_decls. Unused- 1340 parameter warnings are handled by function.c. 1341 error_mark_node obviously does not go in BLOCK_VARS and 1342 does not get unused-variable warnings. */ 1343 case PARM_DECL: 1344 case ERROR_MARK: 1345 /* It is possible for a decl not to have a name. We get 1346 here with b->id NULL in this case. */ 1347 if (b->id) 1348 { 1349 gcc_assert (I_SYMBOL_BINDING (b->id) == b); 1350 I_SYMBOL_BINDING (b->id) = b->shadowed; 1351 if (b->shadowed && b->shadowed->u.type) 1352 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type; 1353 } 1354 break; 1355 1356 default: 1357 gcc_unreachable (); 1358 } 1359 } 1360 1361 1362 /* Dispose of the block that we just made inside some higher level. */ 1363 if ((scope->function_body || scope == file_scope) && context) 1364 { 1365 DECL_INITIAL (context) = block; 1366 BLOCK_SUPERCONTEXT (block) = context; 1367 } 1368 else if (scope->outer) 1369 { 1370 if (block) 1371 SCOPE_LIST_APPEND (scope->outer, blocks, block); 1372 /* If we did not make a block for the scope just exited, any 1373 blocks made for inner scopes must be carried forward so they 1374 will later become subblocks of something else. */ 1375 else if (scope->blocks) 1376 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks); 1377 } 1378 1379 /* Pop the current scope, and free the structure for reuse. */ 1380 current_scope = scope->outer; 1381 if (scope->function_body) 1382 current_function_scope = scope->outer_function; 1383 1384 memset (scope, 0, sizeof (struct c_scope)); 1385 scope->outer = scope_freelist; 1386 scope_freelist = scope; 1387 1388 return block; 1389 } 1390 1391 void 1392 push_file_scope (void) 1393 { 1394 tree decl; 1395 1396 if (file_scope) 1397 return; 1398 1399 push_scope (); 1400 file_scope = current_scope; 1401 1402 start_fname_decls (); 1403 1404 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl)) 1405 bind (DECL_NAME (decl), decl, file_scope, 1406 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl)); 1407 } 1408 1409 void 1410 pop_file_scope (void) 1411 { 1412 /* In case there were missing closebraces, get us back to the global 1413 binding level. */ 1414 while (current_scope != file_scope) 1415 pop_scope (); 1416 1417 /* __FUNCTION__ is defined at file scope (""). This 1418 call may not be necessary as my tests indicate it 1419 still works without it. */ 1420 finish_fname_decls (); 1421 1422 check_inline_statics (); 1423 1424 /* This is the point to write out a PCH if we're doing that. 1425 In that case we do not want to do anything else. */ 1426 if (pch_file) 1427 { 1428 c_common_write_pch (); 1429 /* Ensure even the callers don't try to finalize the CU. */ 1430 flag_syntax_only = 1; 1431 return; 1432 } 1433 1434 /* Pop off the file scope and close this translation unit. */ 1435 pop_scope (); 1436 file_scope = 0; 1437 1438 maybe_apply_pending_pragma_weaks (); 1439 } 1440 1441 /* Adjust the bindings for the start of a statement expression. */ 1442 1443 void 1444 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings) 1445 { 1446 struct c_scope *scope; 1447 1448 for (scope = current_scope; scope != NULL; scope = scope->outer) 1449 { 1450 struct c_binding *b; 1451 1452 if (!scope->has_label_bindings) 1453 continue; 1454 1455 for (b = scope->bindings; b != NULL; b = b->prev) 1456 { 1457 struct c_label_vars *label_vars; 1458 unsigned int ix; 1459 struct c_goto_bindings *g; 1460 1461 if (TREE_CODE (b->decl) != LABEL_DECL) 1462 continue; 1463 label_vars = b->u.label; 1464 ++label_vars->label_bindings.stmt_exprs; 1465 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g) 1466 ++g->goto_bindings.stmt_exprs; 1467 } 1468 } 1469 1470 if (switch_bindings != NULL) 1471 ++switch_bindings->stmt_exprs; 1472 } 1473 1474 /* Adjust the bindings for the end of a statement expression. */ 1475 1476 void 1477 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings) 1478 { 1479 struct c_scope *scope; 1480 1481 for (scope = current_scope; scope != NULL; scope = scope->outer) 1482 { 1483 struct c_binding *b; 1484 1485 if (!scope->has_label_bindings) 1486 continue; 1487 1488 for (b = scope->bindings; b != NULL; b = b->prev) 1489 { 1490 struct c_label_vars *label_vars; 1491 unsigned int ix; 1492 struct c_goto_bindings *g; 1493 1494 if (TREE_CODE (b->decl) != LABEL_DECL) 1495 continue; 1496 label_vars = b->u.label; 1497 --label_vars->label_bindings.stmt_exprs; 1498 if (label_vars->label_bindings.stmt_exprs < 0) 1499 { 1500 label_vars->label_bindings.left_stmt_expr = true; 1501 label_vars->label_bindings.stmt_exprs = 0; 1502 } 1503 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g) 1504 { 1505 --g->goto_bindings.stmt_exprs; 1506 if (g->goto_bindings.stmt_exprs < 0) 1507 { 1508 g->goto_bindings.left_stmt_expr = true; 1509 g->goto_bindings.stmt_exprs = 0; 1510 } 1511 } 1512 } 1513 } 1514 1515 if (switch_bindings != NULL) 1516 { 1517 --switch_bindings->stmt_exprs; 1518 gcc_assert (switch_bindings->stmt_exprs >= 0); 1519 } 1520 } 1521 1522 /* Push a definition or a declaration of struct, union or enum tag "name". 1523 "type" should be the type node. 1524 We assume that the tag "name" is not already defined, and has a location 1525 of LOC. 1526 1527 Note that the definition may really be just a forward reference. 1528 In that case, the TYPE_SIZE will be zero. */ 1529 1530 static void 1531 pushtag (location_t loc, tree name, tree type) 1532 { 1533 /* Record the identifier as the type's name if it has none. */ 1534 if (name && !TYPE_NAME (type)) 1535 TYPE_NAME (type) = name; 1536 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc); 1537 1538 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the 1539 tagged type we just added to the current scope. This fake 1540 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs 1541 to output a representation of a tagged type, and it also gives 1542 us a convenient place to record the "scope start" address for the 1543 tagged type. */ 1544 1545 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc, 1546 TYPE_DECL, NULL_TREE, type)); 1547 1548 /* An approximation for now, so we can tell this is a function-scope tag. 1549 This will be updated in pop_scope. */ 1550 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type)); 1551 1552 if (warn_cxx_compat && name != NULL_TREE) 1553 { 1554 struct c_binding *b = I_SYMBOL_BINDING (name); 1555 1556 if (b != NULL 1557 && b->decl != NULL_TREE 1558 && TREE_CODE (b->decl) == TYPE_DECL 1559 && (B_IN_CURRENT_SCOPE (b) 1560 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b))) 1561 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl)) 1562 != TYPE_MAIN_VARIANT (type))) 1563 { 1564 if (warning_at (loc, OPT_Wc___compat, 1565 ("using %qD as both a typedef and a tag is " 1566 "invalid in C++"), b->decl) 1567 && b->locus != UNKNOWN_LOCATION) 1568 inform (b->locus, "originally defined here"); 1569 } 1570 } 1571 } 1572 1573 /* An exported interface to pushtag. This is used by the gdb plugin's 1574 binding oracle to introduce a new tag binding. */ 1575 1576 void 1577 c_pushtag (location_t loc, tree name, tree type) 1578 { 1579 pushtag (loc, name, type); 1580 } 1581 1582 /* An exported interface to bind a declaration. LOC is the location 1583 to use. DECL is the declaration to bind. The decl's name is used 1584 to determine how it is bound. If DECL is a VAR_DECL, then 1585 IS_GLOBAL determines whether the decl is put into the global (file 1586 and external) scope or the current function's scope; if DECL is not 1587 a VAR_DECL then it is always put into the file scope. */ 1588 1589 void 1590 c_bind (location_t loc, tree decl, bool is_global) 1591 { 1592 struct c_scope *scope; 1593 bool nested = false; 1594 1595 if (!VAR_P (decl) || current_function_scope == NULL) 1596 { 1597 /* Types and functions are always considered to be global. */ 1598 scope = file_scope; 1599 DECL_EXTERNAL (decl) = 1; 1600 TREE_PUBLIC (decl) = 1; 1601 } 1602 else if (is_global) 1603 { 1604 /* Also bind it into the external scope. */ 1605 bind (DECL_NAME (decl), decl, external_scope, true, false, loc); 1606 nested = true; 1607 scope = file_scope; 1608 DECL_EXTERNAL (decl) = 1; 1609 TREE_PUBLIC (decl) = 1; 1610 } 1611 else 1612 { 1613 DECL_CONTEXT (decl) = current_function_decl; 1614 TREE_PUBLIC (decl) = 0; 1615 scope = current_function_scope; 1616 } 1617 1618 bind (DECL_NAME (decl), decl, scope, false, nested, loc); 1619 } 1620 1621 /* Subroutine of compare_decls. Allow harmless mismatches in return 1622 and argument types provided that the type modes match. This function 1623 return a unified type given a suitable match, and 0 otherwise. */ 1624 1625 static tree 1626 match_builtin_function_types (tree newtype, tree oldtype) 1627 { 1628 tree newrettype, oldrettype; 1629 tree newargs, oldargs; 1630 tree trytype, tryargs; 1631 1632 /* Accept the return type of the new declaration if same modes. */ 1633 oldrettype = TREE_TYPE (oldtype); 1634 newrettype = TREE_TYPE (newtype); 1635 1636 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype)) 1637 return NULL_TREE; 1638 1639 oldargs = TYPE_ARG_TYPES (oldtype); 1640 newargs = TYPE_ARG_TYPES (newtype); 1641 tryargs = newargs; 1642 1643 while (oldargs || newargs) 1644 { 1645 if (!oldargs 1646 || !newargs 1647 || !TREE_VALUE (oldargs) 1648 || !TREE_VALUE (newargs) 1649 || TYPE_MODE (TREE_VALUE (oldargs)) 1650 != TYPE_MODE (TREE_VALUE (newargs))) 1651 return NULL_TREE; 1652 1653 oldargs = TREE_CHAIN (oldargs); 1654 newargs = TREE_CHAIN (newargs); 1655 } 1656 1657 trytype = build_function_type (newrettype, tryargs); 1658 1659 /* Allow declaration to change transaction_safe attribute. */ 1660 tree oldattrs = TYPE_ATTRIBUTES (oldtype); 1661 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs); 1662 tree newattrs = TYPE_ATTRIBUTES (newtype); 1663 tree newtsafe = lookup_attribute ("transaction_safe", newattrs); 1664 if (oldtsafe && !newtsafe) 1665 oldattrs = remove_attribute ("transaction_safe", oldattrs); 1666 else if (newtsafe && !oldtsafe) 1667 oldattrs = tree_cons (get_identifier ("transaction_safe"), 1668 NULL_TREE, oldattrs); 1669 1670 return build_type_attribute_variant (trytype, oldattrs); 1671 } 1672 1673 /* Subroutine of diagnose_mismatched_decls. Check for function type 1674 mismatch involving an empty arglist vs a nonempty one and give clearer 1675 diagnostics. */ 1676 static void 1677 diagnose_arglist_conflict (tree newdecl, tree olddecl, 1678 tree newtype, tree oldtype) 1679 { 1680 tree t; 1681 1682 if (TREE_CODE (olddecl) != FUNCTION_DECL 1683 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype)) 1684 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE) 1685 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE))) 1686 return; 1687 1688 t = TYPE_ARG_TYPES (oldtype); 1689 if (t == NULL_TREE) 1690 t = TYPE_ARG_TYPES (newtype); 1691 for (; t; t = TREE_CHAIN (t)) 1692 { 1693 tree type = TREE_VALUE (t); 1694 1695 if (TREE_CHAIN (t) == NULL_TREE 1696 && TYPE_MAIN_VARIANT (type) != void_type_node) 1697 { 1698 inform (input_location, "a parameter list with an ellipsis can%'t match " 1699 "an empty parameter name list declaration"); 1700 break; 1701 } 1702 1703 if (c_type_promotes_to (type) != type) 1704 { 1705 inform (input_location, "an argument type that has a default promotion can%'t match " 1706 "an empty parameter name list declaration"); 1707 break; 1708 } 1709 } 1710 } 1711 1712 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an 1713 old-style function definition, NEWDECL is a prototype declaration. 1714 Diagnose inconsistencies in the argument list. Returns TRUE if 1715 the prototype is compatible, FALSE if not. */ 1716 static bool 1717 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype) 1718 { 1719 tree newargs, oldargs; 1720 int i; 1721 1722 #define END_OF_ARGLIST(t) ((t) == void_type_node) 1723 1724 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype); 1725 newargs = TYPE_ARG_TYPES (newtype); 1726 i = 1; 1727 1728 for (;;) 1729 { 1730 tree oldargtype = TREE_VALUE (oldargs); 1731 tree newargtype = TREE_VALUE (newargs); 1732 1733 if (oldargtype == error_mark_node || newargtype == error_mark_node) 1734 return false; 1735 1736 oldargtype = (TYPE_ATOMIC (oldargtype) 1737 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype), 1738 TYPE_QUAL_ATOMIC) 1739 : TYPE_MAIN_VARIANT (oldargtype)); 1740 newargtype = (TYPE_ATOMIC (newargtype) 1741 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype), 1742 TYPE_QUAL_ATOMIC) 1743 : TYPE_MAIN_VARIANT (newargtype)); 1744 1745 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype)) 1746 break; 1747 1748 /* Reaching the end of just one list means the two decls don't 1749 agree on the number of arguments. */ 1750 if (END_OF_ARGLIST (oldargtype)) 1751 { 1752 error ("prototype for %q+D declares more arguments " 1753 "than previous old-style definition", newdecl); 1754 return false; 1755 } 1756 else if (END_OF_ARGLIST (newargtype)) 1757 { 1758 error ("prototype for %q+D declares fewer arguments " 1759 "than previous old-style definition", newdecl); 1760 return false; 1761 } 1762 1763 /* Type for passing arg must be consistent with that declared 1764 for the arg. */ 1765 else if (!comptypes (oldargtype, newargtype)) 1766 { 1767 error ("prototype for %q+D declares argument %d" 1768 " with incompatible type", 1769 newdecl, i); 1770 return false; 1771 } 1772 1773 oldargs = TREE_CHAIN (oldargs); 1774 newargs = TREE_CHAIN (newargs); 1775 i++; 1776 } 1777 1778 /* If we get here, no errors were found, but do issue a warning 1779 for this poor-style construct. */ 1780 warning (0, "prototype for %q+D follows non-prototype definition", 1781 newdecl); 1782 return true; 1783 #undef END_OF_ARGLIST 1784 } 1785 1786 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL, 1787 first in a pair of mismatched declarations, using the diagnostic 1788 function DIAG. */ 1789 static void 1790 locate_old_decl (tree decl) 1791 { 1792 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl) 1793 && !C_DECL_DECLARED_BUILTIN (decl)) 1794 ; 1795 else if (DECL_INITIAL (decl)) 1796 inform (input_location, "previous definition of %q+D was here", decl); 1797 else if (C_DECL_IMPLICIT (decl)) 1798 inform (input_location, "previous implicit declaration of %q+D was here", decl); 1799 else 1800 inform (input_location, "previous declaration of %q+D was here", decl); 1801 } 1802 1803 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL. 1804 Returns true if the caller should proceed to merge the two, false 1805 if OLDDECL should simply be discarded. As a side effect, issues 1806 all necessary diagnostics for invalid or poor-style combinations. 1807 If it returns true, writes the types of NEWDECL and OLDDECL to 1808 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from 1809 TREE_TYPE (NEWDECL, OLDDECL) respectively. */ 1810 1811 static bool 1812 diagnose_mismatched_decls (tree newdecl, tree olddecl, 1813 tree *newtypep, tree *oldtypep) 1814 { 1815 tree newtype, oldtype; 1816 bool pedwarned = false; 1817 bool warned = false; 1818 bool retval = true; 1819 1820 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \ 1821 && DECL_EXTERNAL (DECL)) 1822 1823 /* If we have error_mark_node for either decl or type, just discard 1824 the previous decl - we're in an error cascade already. */ 1825 if (olddecl == error_mark_node || newdecl == error_mark_node) 1826 return false; 1827 *oldtypep = oldtype = TREE_TYPE (olddecl); 1828 *newtypep = newtype = TREE_TYPE (newdecl); 1829 if (oldtype == error_mark_node || newtype == error_mark_node) 1830 return false; 1831 1832 /* Two different categories of symbol altogether. This is an error 1833 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */ 1834 if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) 1835 { 1836 if (!(TREE_CODE (olddecl) == FUNCTION_DECL 1837 && DECL_BUILT_IN (olddecl) 1838 && !C_DECL_DECLARED_BUILTIN (olddecl))) 1839 { 1840 error ("%q+D redeclared as different kind of symbol", newdecl); 1841 locate_old_decl (olddecl); 1842 } 1843 else if (TREE_PUBLIC (newdecl)) 1844 warning (OPT_Wbuiltin_declaration_mismatch, 1845 "built-in function %q+D declared as non-function", 1846 newdecl); 1847 else 1848 warning (OPT_Wshadow, "declaration of %q+D shadows " 1849 "a built-in function", newdecl); 1850 return false; 1851 } 1852 1853 /* Enumerators have no linkage, so may only be declared once in a 1854 given scope. */ 1855 if (TREE_CODE (olddecl) == CONST_DECL) 1856 { 1857 error ("redeclaration of enumerator %q+D", newdecl); 1858 locate_old_decl (olddecl); 1859 return false; 1860 } 1861 1862 if (!comptypes (oldtype, newtype)) 1863 { 1864 if (TREE_CODE (olddecl) == FUNCTION_DECL 1865 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl)) 1866 { 1867 /* Accept harmless mismatch in function types. 1868 This is for the ffs and fprintf builtins. */ 1869 tree trytype = match_builtin_function_types (newtype, oldtype); 1870 1871 if (trytype && comptypes (newtype, trytype)) 1872 *oldtypep = oldtype = trytype; 1873 else 1874 { 1875 /* If types don't match for a built-in, throw away the 1876 built-in. No point in calling locate_old_decl here, it 1877 won't print anything. */ 1878 warning (OPT_Wbuiltin_declaration_mismatch, 1879 "conflicting types for built-in function %q+D", 1880 newdecl); 1881 return false; 1882 } 1883 } 1884 else if (TREE_CODE (olddecl) == FUNCTION_DECL 1885 && DECL_IS_BUILTIN (olddecl)) 1886 { 1887 /* A conflicting function declaration for a predeclared 1888 function that isn't actually built in. Objective C uses 1889 these. The new declaration silently overrides everything 1890 but the volatility (i.e. noreturn) indication. See also 1891 below. FIXME: Make Objective C use normal builtins. */ 1892 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1893 return false; 1894 } 1895 /* Permit void foo (...) to match int foo (...) if the latter is 1896 the definition and implicit int was used. See 1897 c-torture/compile/920625-2.c. */ 1898 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) 1899 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node 1900 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node 1901 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl)) 1902 { 1903 pedwarned = pedwarn (input_location, 0, 1904 "conflicting types for %q+D", newdecl); 1905 /* Make sure we keep void as the return type. */ 1906 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype; 1907 C_FUNCTION_IMPLICIT_INT (newdecl) = 0; 1908 } 1909 /* Permit void foo (...) to match an earlier call to foo (...) with 1910 no declared type (thus, implicitly int). */ 1911 else if (TREE_CODE (newdecl) == FUNCTION_DECL 1912 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node 1913 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node 1914 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl)) 1915 { 1916 pedwarned = pedwarn (input_location, 0, 1917 "conflicting types for %q+D", newdecl); 1918 /* Make sure we keep void as the return type. */ 1919 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype; 1920 } 1921 else 1922 { 1923 int new_quals = TYPE_QUALS (newtype); 1924 int old_quals = TYPE_QUALS (oldtype); 1925 1926 if (new_quals != old_quals) 1927 { 1928 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals); 1929 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals); 1930 if (new_addr != old_addr) 1931 { 1932 if (ADDR_SPACE_GENERIC_P (new_addr)) 1933 error ("conflicting named address spaces (generic vs %s) " 1934 "for %q+D", 1935 c_addr_space_name (old_addr), newdecl); 1936 else if (ADDR_SPACE_GENERIC_P (old_addr)) 1937 error ("conflicting named address spaces (%s vs generic) " 1938 "for %q+D", 1939 c_addr_space_name (new_addr), newdecl); 1940 else 1941 error ("conflicting named address spaces (%s vs %s) " 1942 "for %q+D", 1943 c_addr_space_name (new_addr), 1944 c_addr_space_name (old_addr), 1945 newdecl); 1946 } 1947 1948 if (CLEAR_QUAL_ADDR_SPACE (new_quals) 1949 != CLEAR_QUAL_ADDR_SPACE (old_quals)) 1950 error ("conflicting type qualifiers for %q+D", newdecl); 1951 } 1952 else 1953 error ("conflicting types for %q+D", newdecl); 1954 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype); 1955 locate_old_decl (olddecl); 1956 return false; 1957 } 1958 } 1959 1960 /* Redeclaration of a type is a constraint violation (6.7.2.3p1), 1961 but silently ignore the redeclaration if either is in a system 1962 header. (Conflicting redeclarations were handled above.) This 1963 is allowed for C11 if the types are the same, not just 1964 compatible. */ 1965 if (TREE_CODE (newdecl) == TYPE_DECL) 1966 { 1967 bool types_different = false; 1968 int comptypes_result; 1969 1970 comptypes_result 1971 = comptypes_check_different_types (oldtype, newtype, &types_different); 1972 1973 if (comptypes_result != 1 || types_different) 1974 { 1975 error ("redefinition of typedef %q+D with different type", newdecl); 1976 locate_old_decl (olddecl); 1977 return false; 1978 } 1979 1980 if (DECL_IN_SYSTEM_HEADER (newdecl) 1981 || DECL_IN_SYSTEM_HEADER (olddecl) 1982 || TREE_NO_WARNING (newdecl) 1983 || TREE_NO_WARNING (olddecl)) 1984 return true; /* Allow OLDDECL to continue in use. */ 1985 1986 if (variably_modified_type_p (newtype, NULL)) 1987 { 1988 error ("redefinition of typedef %q+D with variably modified type", 1989 newdecl); 1990 locate_old_decl (olddecl); 1991 } 1992 else if (pedwarn_c99 (input_location, OPT_Wpedantic, 1993 "redefinition of typedef %q+D", newdecl)) 1994 locate_old_decl (olddecl); 1995 1996 return true; 1997 } 1998 1999 /* Function declarations can either be 'static' or 'extern' (no 2000 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore 2001 can never conflict with each other on account of linkage 2002 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but 2003 gnu89 mode permits two definitions if one is 'extern inline' and 2004 one is not. The non- extern-inline definition supersedes the 2005 extern-inline definition. */ 2006 2007 else if (TREE_CODE (newdecl) == FUNCTION_DECL) 2008 { 2009 /* If you declare a built-in function name as static, or 2010 define the built-in with an old-style definition (so we 2011 can't validate the argument list) the built-in definition is 2012 overridden, but optionally warn this was a bad choice of name. */ 2013 if (DECL_BUILT_IN (olddecl) 2014 && !C_DECL_DECLARED_BUILTIN (olddecl) 2015 && (!TREE_PUBLIC (newdecl) 2016 || (DECL_INITIAL (newdecl) 2017 && !prototype_p (TREE_TYPE (newdecl))))) 2018 { 2019 warning (OPT_Wshadow, "declaration of %q+D shadows " 2020 "a built-in function", newdecl); 2021 /* Discard the old built-in function. */ 2022 return false; 2023 } 2024 2025 if (DECL_INITIAL (newdecl)) 2026 { 2027 if (DECL_INITIAL (olddecl)) 2028 { 2029 /* If both decls are in the same TU and the new declaration 2030 isn't overriding an extern inline reject the new decl. 2031 In c99, no overriding is allowed in the same translation 2032 unit. */ 2033 if ((!DECL_EXTERN_INLINE (olddecl) 2034 || DECL_EXTERN_INLINE (newdecl) 2035 || (!flag_gnu89_inline 2036 && (!DECL_DECLARED_INLINE_P (olddecl) 2037 || !lookup_attribute ("gnu_inline", 2038 DECL_ATTRIBUTES (olddecl))) 2039 && (!DECL_DECLARED_INLINE_P (newdecl) 2040 || !lookup_attribute ("gnu_inline", 2041 DECL_ATTRIBUTES (newdecl)))) 2042 ) 2043 && same_translation_unit_p (newdecl, olddecl)) 2044 { 2045 error ("redefinition of %q+D", newdecl); 2046 locate_old_decl (olddecl); 2047 return false; 2048 } 2049 } 2050 } 2051 /* If we have a prototype after an old-style function definition, 2052 the argument types must be checked specially. */ 2053 else if (DECL_INITIAL (olddecl) 2054 && !prototype_p (oldtype) && prototype_p (newtype) 2055 && TYPE_ACTUAL_ARG_TYPES (oldtype) 2056 && !validate_proto_after_old_defn (newdecl, newtype, oldtype)) 2057 { 2058 locate_old_decl (olddecl); 2059 return false; 2060 } 2061 /* A non-static declaration (even an "extern") followed by a 2062 static declaration is undefined behavior per C99 6.2.2p3-5,7. 2063 The same is true for a static forward declaration at block 2064 scope followed by a non-static declaration/definition at file 2065 scope. Static followed by non-static at the same scope is 2066 not undefined behavior, and is the most convenient way to get 2067 some effects (see e.g. what unwind-dw2-fde-glibc.c does to 2068 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but 2069 we do diagnose it if -Wtraditional. */ 2070 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl)) 2071 { 2072 /* Two exceptions to the rule. If olddecl is an extern 2073 inline, or a predeclared function that isn't actually 2074 built in, newdecl silently overrides olddecl. The latter 2075 occur only in Objective C; see also above. (FIXME: Make 2076 Objective C use normal builtins.) */ 2077 if (!DECL_IS_BUILTIN (olddecl) 2078 && !DECL_EXTERN_INLINE (olddecl)) 2079 { 2080 error ("static declaration of %q+D follows " 2081 "non-static declaration", newdecl); 2082 locate_old_decl (olddecl); 2083 } 2084 return false; 2085 } 2086 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)) 2087 { 2088 if (DECL_CONTEXT (olddecl)) 2089 { 2090 error ("non-static declaration of %q+D follows " 2091 "static declaration", newdecl); 2092 locate_old_decl (olddecl); 2093 return false; 2094 } 2095 else if (warn_traditional) 2096 { 2097 warned |= warning (OPT_Wtraditional, 2098 "non-static declaration of %q+D " 2099 "follows static declaration", newdecl); 2100 } 2101 } 2102 2103 /* Make sure gnu_inline attribute is either not present, or 2104 present on all inline decls. */ 2105 if (DECL_DECLARED_INLINE_P (olddecl) 2106 && DECL_DECLARED_INLINE_P (newdecl)) 2107 { 2108 bool newa = lookup_attribute ("gnu_inline", 2109 DECL_ATTRIBUTES (newdecl)) != NULL; 2110 bool olda = lookup_attribute ("gnu_inline", 2111 DECL_ATTRIBUTES (olddecl)) != NULL; 2112 if (newa != olda) 2113 { 2114 error_at (input_location, "%<gnu_inline%> attribute present on %q+D", 2115 newa ? newdecl : olddecl); 2116 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl), 2117 "but not here"); 2118 } 2119 } 2120 } 2121 else if (VAR_P (newdecl)) 2122 { 2123 /* Only variables can be thread-local, and all declarations must 2124 agree on this property. */ 2125 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl)) 2126 { 2127 /* Nothing to check. Since OLDDECL is marked threadprivate 2128 and NEWDECL does not have a thread-local attribute, we 2129 will merge the threadprivate attribute into NEWDECL. */ 2130 ; 2131 } 2132 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)) 2133 { 2134 if (DECL_THREAD_LOCAL_P (newdecl)) 2135 error ("thread-local declaration of %q+D follows " 2136 "non-thread-local declaration", newdecl); 2137 else 2138 error ("non-thread-local declaration of %q+D follows " 2139 "thread-local declaration", newdecl); 2140 2141 locate_old_decl (olddecl); 2142 return false; 2143 } 2144 2145 /* Multiple initialized definitions are not allowed (6.9p3,5). */ 2146 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl)) 2147 { 2148 error ("redefinition of %q+D", newdecl); 2149 locate_old_decl (olddecl); 2150 return false; 2151 } 2152 2153 /* Objects declared at file scope: if the first declaration had 2154 external linkage (even if it was an external reference) the 2155 second must have external linkage as well, or the behavior is 2156 undefined. If the first declaration had internal linkage, then 2157 the second must too, or else be an external reference (in which 2158 case the composite declaration still has internal linkage). 2159 As for function declarations, we warn about the static-then- 2160 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */ 2161 if (DECL_FILE_SCOPE_P (newdecl) 2162 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl)) 2163 { 2164 if (DECL_EXTERNAL (newdecl)) 2165 { 2166 if (!DECL_FILE_SCOPE_P (olddecl)) 2167 { 2168 error ("extern declaration of %q+D follows " 2169 "declaration with no linkage", newdecl); 2170 locate_old_decl (olddecl); 2171 return false; 2172 } 2173 else if (warn_traditional) 2174 { 2175 warned |= warning (OPT_Wtraditional, 2176 "non-static declaration of %q+D " 2177 "follows static declaration", newdecl); 2178 } 2179 } 2180 else 2181 { 2182 if (TREE_PUBLIC (newdecl)) 2183 error ("non-static declaration of %q+D follows " 2184 "static declaration", newdecl); 2185 else 2186 error ("static declaration of %q+D follows " 2187 "non-static declaration", newdecl); 2188 2189 locate_old_decl (olddecl); 2190 return false; 2191 } 2192 } 2193 /* Two objects with the same name declared at the same block 2194 scope must both be external references (6.7p3). */ 2195 else if (!DECL_FILE_SCOPE_P (newdecl)) 2196 { 2197 if (DECL_EXTERNAL (newdecl)) 2198 { 2199 /* Extern with initializer at block scope, which will 2200 already have received an error. */ 2201 } 2202 else if (DECL_EXTERNAL (olddecl)) 2203 { 2204 error ("declaration of %q+D with no linkage follows " 2205 "extern declaration", newdecl); 2206 locate_old_decl (olddecl); 2207 } 2208 else 2209 { 2210 error ("redeclaration of %q+D with no linkage", newdecl); 2211 locate_old_decl (olddecl); 2212 } 2213 2214 return false; 2215 } 2216 2217 /* C++ does not permit a decl to appear multiple times at file 2218 scope. */ 2219 if (warn_cxx_compat 2220 && DECL_FILE_SCOPE_P (newdecl) 2221 && !DECL_EXTERNAL (newdecl) 2222 && !DECL_EXTERNAL (olddecl)) 2223 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl), 2224 OPT_Wc___compat, 2225 ("duplicate declaration of %qD is " 2226 "invalid in C++"), 2227 newdecl); 2228 } 2229 2230 /* warnings */ 2231 /* All decls must agree on a visibility. */ 2232 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS) 2233 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl) 2234 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) 2235 { 2236 warned |= warning (0, "redeclaration of %q+D with different visibility " 2237 "(old visibility preserved)", newdecl); 2238 } 2239 2240 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2241 warned |= diagnose_mismatched_attributes (olddecl, newdecl); 2242 else /* PARM_DECL, VAR_DECL */ 2243 { 2244 /* Redeclaration of a parameter is a constraint violation (this is 2245 not explicitly stated, but follows from C99 6.7p3 [no more than 2246 one declaration of the same identifier with no linkage in the 2247 same scope, except type tags] and 6.2.2p6 [parameters have no 2248 linkage]). We must check for a forward parameter declaration, 2249 indicated by TREE_ASM_WRITTEN on the old declaration - this is 2250 an extension, the mandatory diagnostic for which is handled by 2251 mark_forward_parm_decls. */ 2252 2253 if (TREE_CODE (newdecl) == PARM_DECL 2254 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl))) 2255 { 2256 error ("redefinition of parameter %q+D", newdecl); 2257 locate_old_decl (olddecl); 2258 return false; 2259 } 2260 } 2261 2262 /* Optional warning for completely redundant decls. */ 2263 if (!warned && !pedwarned 2264 && warn_redundant_decls 2265 /* Don't warn about a function declaration followed by a 2266 definition. */ 2267 && !(TREE_CODE (newdecl) == FUNCTION_DECL 2268 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)) 2269 /* Don't warn about redundant redeclarations of builtins. */ 2270 && !(TREE_CODE (newdecl) == FUNCTION_DECL 2271 && !DECL_BUILT_IN (newdecl) 2272 && DECL_BUILT_IN (olddecl) 2273 && !C_DECL_DECLARED_BUILTIN (olddecl)) 2274 /* Don't warn about an extern followed by a definition. */ 2275 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl)) 2276 /* Don't warn about forward parameter decls. */ 2277 && !(TREE_CODE (newdecl) == PARM_DECL 2278 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)) 2279 /* Don't warn about a variable definition following a declaration. */ 2280 && !(VAR_P (newdecl) 2281 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))) 2282 { 2283 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D", 2284 newdecl); 2285 } 2286 2287 /* Report location of previous decl/defn. */ 2288 if (warned || pedwarned) 2289 locate_old_decl (olddecl); 2290 2291 #undef DECL_EXTERN_INLINE 2292 2293 return retval; 2294 } 2295 2296 /* Subroutine of duplicate_decls. NEWDECL has been found to be 2297 consistent with OLDDECL, but carries new information. Merge the 2298 new information into OLDDECL. This function issues no 2299 diagnostics. */ 2300 2301 static void 2302 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype) 2303 { 2304 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL 2305 && DECL_INITIAL (newdecl) != NULL_TREE); 2306 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL 2307 && prototype_p (TREE_TYPE (newdecl))); 2308 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL 2309 && prototype_p (TREE_TYPE (olddecl))); 2310 2311 /* For real parm decl following a forward decl, rechain the old decl 2312 in its new location and clear TREE_ASM_WRITTEN (it's not a 2313 forward decl anymore). */ 2314 if (TREE_CODE (newdecl) == PARM_DECL 2315 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)) 2316 { 2317 struct c_binding *b, **here; 2318 2319 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev) 2320 if ((*here)->decl == olddecl) 2321 goto found; 2322 gcc_unreachable (); 2323 2324 found: 2325 b = *here; 2326 *here = b->prev; 2327 b->prev = current_scope->bindings; 2328 current_scope->bindings = b; 2329 2330 TREE_ASM_WRITTEN (olddecl) = 0; 2331 } 2332 2333 DECL_ATTRIBUTES (newdecl) 2334 = targetm.merge_decl_attributes (olddecl, newdecl); 2335 2336 /* For typedefs use the old type, as the new type's DECL_NAME points 2337 at newdecl, which will be ggc_freed. */ 2338 if (TREE_CODE (newdecl) == TYPE_DECL) 2339 { 2340 /* But NEWTYPE might have an attribute, honor that. */ 2341 tree tem = newtype; 2342 newtype = oldtype; 2343 2344 if (TYPE_USER_ALIGN (tem)) 2345 { 2346 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype)) 2347 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem)); 2348 TYPE_USER_ALIGN (newtype) = true; 2349 } 2350 2351 /* And remove the new type from the variants list. */ 2352 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl) 2353 { 2354 tree remove = TREE_TYPE (newdecl); 2355 if (TYPE_MAIN_VARIANT (remove) == remove) 2356 { 2357 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE); 2358 /* If remove is the main variant, no need to remove that 2359 from the list. One of the DECL_ORIGINAL_TYPE 2360 variants, e.g. created for aligned attribute, might still 2361 refer to the newdecl TYPE_DECL though, so remove that one 2362 in that case. */ 2363 if (DECL_ORIGINAL_TYPE (newdecl) 2364 && DECL_ORIGINAL_TYPE (newdecl) != remove) 2365 for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl)); 2366 t; t = TYPE_MAIN_VARIANT (t)) 2367 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl) 2368 { 2369 TYPE_NEXT_VARIANT (t) 2370 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t)); 2371 break; 2372 } 2373 } 2374 else 2375 for (tree t = TYPE_MAIN_VARIANT (remove); ; 2376 t = TYPE_NEXT_VARIANT (t)) 2377 if (TYPE_NEXT_VARIANT (t) == remove) 2378 { 2379 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove); 2380 break; 2381 } 2382 } 2383 } 2384 2385 /* Merge the data types specified in the two decls. */ 2386 TREE_TYPE (newdecl) 2387 = TREE_TYPE (olddecl) 2388 = composite_type (newtype, oldtype); 2389 2390 /* Lay the type out, unless already done. */ 2391 if (!comptypes (oldtype, TREE_TYPE (newdecl))) 2392 { 2393 if (TREE_TYPE (newdecl) != error_mark_node) 2394 layout_type (TREE_TYPE (newdecl)); 2395 if (TREE_CODE (newdecl) != FUNCTION_DECL 2396 && TREE_CODE (newdecl) != TYPE_DECL 2397 && TREE_CODE (newdecl) != CONST_DECL) 2398 layout_decl (newdecl, 0); 2399 } 2400 else 2401 { 2402 /* Since the type is OLDDECL's, make OLDDECL's size go with. */ 2403 DECL_SIZE (newdecl) = DECL_SIZE (olddecl); 2404 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl); 2405 SET_DECL_MODE (newdecl, DECL_MODE (olddecl)); 2406 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl)) 2407 { 2408 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl)); 2409 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl); 2410 } 2411 if (DECL_WARN_IF_NOT_ALIGN (olddecl) 2412 > DECL_WARN_IF_NOT_ALIGN (newdecl)) 2413 SET_DECL_WARN_IF_NOT_ALIGN (newdecl, 2414 DECL_WARN_IF_NOT_ALIGN (olddecl)); 2415 } 2416 2417 /* Keep the old rtl since we can safely use it. */ 2418 if (HAS_RTL_P (olddecl)) 2419 COPY_DECL_RTL (olddecl, newdecl); 2420 2421 /* Merge the type qualifiers. */ 2422 if (TREE_READONLY (newdecl)) 2423 TREE_READONLY (olddecl) = 1; 2424 2425 if (TREE_THIS_VOLATILE (newdecl)) 2426 TREE_THIS_VOLATILE (olddecl) = 1; 2427 2428 /* Merge deprecatedness. */ 2429 if (TREE_DEPRECATED (newdecl)) 2430 TREE_DEPRECATED (olddecl) = 1; 2431 2432 /* If a decl is in a system header and the other isn't, keep the one on the 2433 system header. Otherwise, keep source location of definition rather than 2434 declaration and of prototype rather than non-prototype unless that 2435 prototype is built-in. */ 2436 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS) 2437 && DECL_IN_SYSTEM_HEADER (olddecl) 2438 && !DECL_IN_SYSTEM_HEADER (newdecl) ) 2439 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); 2440 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS) 2441 && DECL_IN_SYSTEM_HEADER (newdecl) 2442 && !DECL_IN_SYSTEM_HEADER (olddecl)) 2443 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl); 2444 else if ((DECL_INITIAL (newdecl) == NULL_TREE 2445 && DECL_INITIAL (olddecl) != NULL_TREE) 2446 || (old_is_prototype && !new_is_prototype 2447 && !C_DECL_BUILTIN_PROTOTYPE (olddecl))) 2448 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); 2449 2450 /* Merge the initialization information. */ 2451 if (DECL_INITIAL (newdecl) == NULL_TREE) 2452 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 2453 2454 /* Merge the threadprivate attribute. */ 2455 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl)) 2456 C_DECL_THREADPRIVATE_P (newdecl) = 1; 2457 2458 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)) 2459 { 2460 /* Copy the assembler name. 2461 Currently, it can only be defined in the prototype. */ 2462 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); 2463 2464 /* Use visibility of whichever declaration had it specified */ 2465 if (DECL_VISIBILITY_SPECIFIED (olddecl)) 2466 { 2467 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); 2468 DECL_VISIBILITY_SPECIFIED (newdecl) = 1; 2469 } 2470 2471 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2472 { 2473 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl); 2474 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); 2475 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); 2476 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) 2477 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); 2478 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 2479 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); 2480 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl); 2481 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); 2482 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl); 2483 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl); 2484 } 2485 2486 /* Merge the storage class information. */ 2487 merge_weak (newdecl, olddecl); 2488 2489 /* For functions, static overrides non-static. */ 2490 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2491 { 2492 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl); 2493 /* This is since we don't automatically 2494 copy the attributes of NEWDECL into OLDDECL. */ 2495 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 2496 /* If this clears `static', clear it in the identifier too. */ 2497 if (!TREE_PUBLIC (olddecl)) 2498 TREE_PUBLIC (DECL_NAME (olddecl)) = 0; 2499 } 2500 } 2501 2502 /* In c99, 'extern' declaration before (or after) 'inline' means this 2503 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute 2504 is present. */ 2505 if (TREE_CODE (newdecl) == FUNCTION_DECL 2506 && !flag_gnu89_inline 2507 && (DECL_DECLARED_INLINE_P (newdecl) 2508 || DECL_DECLARED_INLINE_P (olddecl)) 2509 && (!DECL_DECLARED_INLINE_P (newdecl) 2510 || !DECL_DECLARED_INLINE_P (olddecl) 2511 || !DECL_EXTERNAL (olddecl)) 2512 && DECL_EXTERNAL (newdecl) 2513 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl)) 2514 && !current_function_decl) 2515 DECL_EXTERNAL (newdecl) = 0; 2516 2517 /* An inline definition following a static declaration is not 2518 DECL_EXTERNAL. */ 2519 if (new_is_definition 2520 && (DECL_DECLARED_INLINE_P (newdecl) 2521 || DECL_DECLARED_INLINE_P (olddecl)) 2522 && !TREE_PUBLIC (olddecl)) 2523 DECL_EXTERNAL (newdecl) = 0; 2524 2525 if (DECL_EXTERNAL (newdecl)) 2526 { 2527 TREE_STATIC (newdecl) = TREE_STATIC (olddecl); 2528 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl); 2529 2530 /* An extern decl does not override previous storage class. */ 2531 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); 2532 if (!DECL_EXTERNAL (newdecl)) 2533 { 2534 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); 2535 DECL_COMMON (newdecl) = DECL_COMMON (olddecl); 2536 } 2537 } 2538 else 2539 { 2540 TREE_STATIC (olddecl) = TREE_STATIC (newdecl); 2541 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 2542 } 2543 2544 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2545 { 2546 /* If we're redefining a function previously defined as extern 2547 inline, make sure we emit debug info for the inline before we 2548 throw it away, in case it was inlined into a function that 2549 hasn't been written out yet. */ 2550 if (new_is_definition && DECL_INITIAL (olddecl)) 2551 /* The new defn must not be inline. */ 2552 DECL_UNINLINABLE (newdecl) = 1; 2553 else 2554 { 2555 /* If either decl says `inline', this fn is inline, unless 2556 its definition was passed already. */ 2557 if (DECL_DECLARED_INLINE_P (newdecl) 2558 || DECL_DECLARED_INLINE_P (olddecl)) 2559 DECL_DECLARED_INLINE_P (newdecl) = 1; 2560 2561 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) 2562 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); 2563 2564 DECL_DISREGARD_INLINE_LIMITS (newdecl) 2565 = DECL_DISREGARD_INLINE_LIMITS (olddecl) 2566 = (DECL_DISREGARD_INLINE_LIMITS (newdecl) 2567 || DECL_DISREGARD_INLINE_LIMITS (olddecl)); 2568 } 2569 2570 if (DECL_BUILT_IN (olddecl)) 2571 { 2572 /* If redeclaring a builtin function, it stays built in. 2573 But it gets tagged as having been declared. */ 2574 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); 2575 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); 2576 C_DECL_DECLARED_BUILTIN (newdecl) = 1; 2577 if (new_is_prototype) 2578 { 2579 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0; 2580 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL) 2581 { 2582 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl); 2583 switch (fncode) 2584 { 2585 /* If a compatible prototype of these builtin functions 2586 is seen, assume the runtime implements it with the 2587 expected semantics. */ 2588 case BUILT_IN_STPCPY: 2589 if (builtin_decl_explicit_p (fncode)) 2590 set_builtin_decl_implicit_p (fncode, true); 2591 break; 2592 default: 2593 if (builtin_decl_explicit_p (fncode)) 2594 set_builtin_decl_declared_p (fncode, true); 2595 break; 2596 } 2597 2598 copy_attributes_to_builtin (newdecl); 2599 } 2600 } 2601 else 2602 C_DECL_BUILTIN_PROTOTYPE (newdecl) 2603 = C_DECL_BUILTIN_PROTOTYPE (olddecl); 2604 } 2605 2606 /* Preserve function specific target and optimization options */ 2607 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl) 2608 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl)) 2609 DECL_FUNCTION_SPECIFIC_TARGET (newdecl) 2610 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl); 2611 2612 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl) 2613 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)) 2614 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl) 2615 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl); 2616 2617 /* Also preserve various other info from the definition. */ 2618 if (!new_is_definition) 2619 { 2620 tree t; 2621 DECL_RESULT (newdecl) = DECL_RESULT (olddecl); 2622 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 2623 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl); 2624 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); 2625 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl)); 2626 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t)) 2627 DECL_CONTEXT (t) = newdecl; 2628 2629 /* See if we've got a function to instantiate from. */ 2630 if (DECL_SAVED_TREE (olddecl)) 2631 DECL_ABSTRACT_ORIGIN (newdecl) 2632 = DECL_ABSTRACT_ORIGIN (olddecl); 2633 } 2634 } 2635 2636 /* Merge the USED information. */ 2637 if (TREE_USED (olddecl)) 2638 TREE_USED (newdecl) = 1; 2639 else if (TREE_USED (newdecl)) 2640 TREE_USED (olddecl) = 1; 2641 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL) 2642 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl); 2643 if (DECL_PRESERVE_P (olddecl)) 2644 DECL_PRESERVE_P (newdecl) = 1; 2645 else if (DECL_PRESERVE_P (newdecl)) 2646 DECL_PRESERVE_P (olddecl) = 1; 2647 2648 /* Merge DECL_COMMON */ 2649 if (VAR_P (olddecl) && VAR_P (newdecl) 2650 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl)) 2651 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl))) 2652 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl); 2653 2654 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL. 2655 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and 2656 DECL_ARGUMENTS (if appropriate). */ 2657 { 2658 unsigned olddecl_uid = DECL_UID (olddecl); 2659 tree olddecl_context = DECL_CONTEXT (olddecl); 2660 tree olddecl_arguments = NULL; 2661 if (TREE_CODE (olddecl) == FUNCTION_DECL) 2662 olddecl_arguments = DECL_ARGUMENTS (olddecl); 2663 2664 memcpy ((char *) olddecl + sizeof (struct tree_common), 2665 (char *) newdecl + sizeof (struct tree_common), 2666 sizeof (struct tree_decl_common) - sizeof (struct tree_common)); 2667 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl); 2668 switch (TREE_CODE (olddecl)) 2669 { 2670 case FUNCTION_DECL: 2671 case VAR_DECL: 2672 { 2673 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node; 2674 2675 memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 2676 (char *) newdecl + sizeof (struct tree_decl_common), 2677 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common)); 2678 olddecl->decl_with_vis.symtab_node = snode; 2679 2680 if ((DECL_EXTERNAL (olddecl) 2681 || TREE_PUBLIC (olddecl) 2682 || TREE_STATIC (olddecl)) 2683 && DECL_SECTION_NAME (newdecl) != NULL) 2684 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl)); 2685 2686 /* This isn't quite correct for something like 2687 int __thread x attribute ((tls_model ("local-exec"))); 2688 extern int __thread x; 2689 as we'll lose the "local-exec" model. */ 2690 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl)) 2691 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl)); 2692 break; 2693 } 2694 2695 case FIELD_DECL: 2696 case PARM_DECL: 2697 case LABEL_DECL: 2698 case RESULT_DECL: 2699 case CONST_DECL: 2700 case TYPE_DECL: 2701 memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 2702 (char *) newdecl + sizeof (struct tree_decl_common), 2703 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common)); 2704 break; 2705 2706 default: 2707 2708 memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 2709 (char *) newdecl + sizeof (struct tree_decl_common), 2710 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)); 2711 } 2712 DECL_UID (olddecl) = olddecl_uid; 2713 DECL_CONTEXT (olddecl) = olddecl_context; 2714 if (TREE_CODE (olddecl) == FUNCTION_DECL) 2715 DECL_ARGUMENTS (olddecl) = olddecl_arguments; 2716 } 2717 2718 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl 2719 so that encode_section_info has a chance to look at the new decl 2720 flags and attributes. */ 2721 if (DECL_RTL_SET_P (olddecl) 2722 && (TREE_CODE (olddecl) == FUNCTION_DECL 2723 || (VAR_P (olddecl) && TREE_STATIC (olddecl)))) 2724 make_decl_rtl (olddecl); 2725 } 2726 2727 /* Handle when a new declaration NEWDECL has the same name as an old 2728 one OLDDECL in the same binding contour. Prints an error message 2729 if appropriate. 2730 2731 If safely possible, alter OLDDECL to look like NEWDECL, and return 2732 true. Otherwise, return false. */ 2733 2734 static bool 2735 duplicate_decls (tree newdecl, tree olddecl) 2736 { 2737 tree newtype = NULL, oldtype = NULL; 2738 2739 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype)) 2740 { 2741 /* Avoid `unused variable' and other warnings for OLDDECL. */ 2742 TREE_NO_WARNING (olddecl) = 1; 2743 return false; 2744 } 2745 2746 merge_decls (newdecl, olddecl, newtype, oldtype); 2747 2748 /* The NEWDECL will no longer be needed. 2749 2750 Before releasing the node, be sure to remove function from symbol 2751 table that might have been inserted there to record comdat group. 2752 Be sure to however do not free DECL_STRUCT_FUNCTION because this 2753 structure is shared in between NEWDECL and OLDECL. */ 2754 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2755 DECL_STRUCT_FUNCTION (newdecl) = NULL; 2756 if (VAR_OR_FUNCTION_DECL_P (newdecl)) 2757 { 2758 struct symtab_node *snode = symtab_node::get (newdecl); 2759 if (snode) 2760 snode->remove (); 2761 } 2762 ggc_free (newdecl); 2763 return true; 2764 } 2765 2766 2767 /* Check whether decl-node NEW_DECL shadows an existing declaration. */ 2768 static void 2769 warn_if_shadowing (tree new_decl) 2770 { 2771 struct c_binding *b; 2772 2773 /* Shadow warnings wanted? */ 2774 if (!(warn_shadow 2775 || warn_shadow_local 2776 || warn_shadow_compatible_local) 2777 /* No shadow warnings for internally generated vars. */ 2778 || DECL_IS_BUILTIN (new_decl) 2779 /* No shadow warnings for vars made for inlining. */ 2780 || DECL_FROM_INLINE (new_decl)) 2781 return; 2782 2783 /* Is anything being shadowed? Invisible decls do not count. */ 2784 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed) 2785 if (b->decl && b->decl != new_decl && !b->invisible 2786 && (b->decl == error_mark_node 2787 || diagnostic_report_warnings_p (global_dc, 2788 DECL_SOURCE_LOCATION (b->decl)))) 2789 { 2790 tree old_decl = b->decl; 2791 bool warned = false; 2792 2793 if (old_decl == error_mark_node) 2794 { 2795 warning (OPT_Wshadow, "declaration of %q+D shadows previous " 2796 "non-variable", new_decl); 2797 break; 2798 } 2799 else if (TREE_CODE (old_decl) == PARM_DECL) 2800 { 2801 enum opt_code warning_code; 2802 2803 /* If '-Wshadow=compatible-local' is specified without other 2804 -Wshadow= flags, we will warn only when the types of the 2805 shadowing variable (i.e. new_decl) and the shadowed variable 2806 (old_decl) are compatible. */ 2807 if (warn_shadow) 2808 warning_code = OPT_Wshadow; 2809 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl))) 2810 warning_code = OPT_Wshadow_compatible_local; 2811 else 2812 warning_code = OPT_Wshadow_local; 2813 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code, 2814 "declaration of %qD shadows a parameter", 2815 new_decl); 2816 } 2817 else if (DECL_FILE_SCOPE_P (old_decl)) 2818 { 2819 /* Do not warn if a variable shadows a function, unless 2820 the variable is a function or a pointer-to-function. */ 2821 if (TREE_CODE (old_decl) == FUNCTION_DECL 2822 && TREE_CODE (new_decl) != FUNCTION_DECL 2823 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl))) 2824 continue; 2825 2826 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow, 2827 "declaration of %qD shadows a global " 2828 "declaration", 2829 new_decl); 2830 } 2831 else if (TREE_CODE (old_decl) == FUNCTION_DECL 2832 && DECL_BUILT_IN (old_decl)) 2833 { 2834 warning (OPT_Wshadow, "declaration of %q+D shadows " 2835 "a built-in function", new_decl); 2836 break; 2837 } 2838 else 2839 { 2840 enum opt_code warning_code; 2841 2842 /* If '-Wshadow=compatible-local' is specified without other 2843 -Wshadow= flags, we will warn only when the types of the 2844 shadowing variable (i.e. new_decl) and the shadowed variable 2845 (old_decl) are compatible. */ 2846 if (warn_shadow) 2847 warning_code = OPT_Wshadow; 2848 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl))) 2849 warning_code = OPT_Wshadow_compatible_local; 2850 else 2851 warning_code = OPT_Wshadow_local; 2852 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code, 2853 "declaration of %qD shadows a previous local", 2854 new_decl); 2855 } 2856 2857 if (warned) 2858 inform (DECL_SOURCE_LOCATION (old_decl), 2859 "shadowed declaration is here"); 2860 2861 break; 2862 } 2863 } 2864 2865 /* Record a decl-node X as belonging to the current lexical scope. 2866 Check for errors (such as an incompatible declaration for the same 2867 name already seen in the same scope). 2868 2869 Returns either X or an old decl for the same name. 2870 If an old decl is returned, it may have been smashed 2871 to agree with what X says. */ 2872 2873 tree 2874 pushdecl (tree x) 2875 { 2876 tree name = DECL_NAME (x); 2877 struct c_scope *scope = current_scope; 2878 struct c_binding *b; 2879 bool nested = false; 2880 location_t locus = DECL_SOURCE_LOCATION (x); 2881 2882 /* Must set DECL_CONTEXT for everything not at file scope or 2883 DECL_FILE_SCOPE_P won't work. Local externs don't count 2884 unless they have initializers (which generate code). */ 2885 if (current_function_decl 2886 && (!VAR_OR_FUNCTION_DECL_P (x) 2887 || DECL_INITIAL (x) || !TREE_PUBLIC (x))) 2888 DECL_CONTEXT (x) = current_function_decl; 2889 2890 /* Anonymous decls are just inserted in the scope. */ 2891 if (!name) 2892 { 2893 bind (name, x, scope, /*invisible=*/false, /*nested=*/false, 2894 locus); 2895 return x; 2896 } 2897 2898 /* First, see if there is another declaration with the same name in 2899 the current scope. If there is, duplicate_decls may do all the 2900 work for us. If duplicate_decls returns false, that indicates 2901 two incompatible decls in the same scope; we are to silently 2902 replace the old one (duplicate_decls has issued all appropriate 2903 diagnostics). In particular, we should not consider possible 2904 duplicates in the external scope, or shadowing. */ 2905 b = I_SYMBOL_BINDING (name); 2906 if (b && B_IN_SCOPE (b, scope)) 2907 { 2908 struct c_binding *b_ext, *b_use; 2909 tree type = TREE_TYPE (x); 2910 tree visdecl = b->decl; 2911 tree vistype = TREE_TYPE (visdecl); 2912 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 2913 && COMPLETE_TYPE_P (TREE_TYPE (x))) 2914 b->inner_comp = false; 2915 b_use = b; 2916 b_ext = b; 2917 /* If this is an external linkage declaration, we should check 2918 for compatibility with the type in the external scope before 2919 setting the type at this scope based on the visible 2920 information only. */ 2921 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl)) 2922 { 2923 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext)) 2924 b_ext = b_ext->shadowed; 2925 if (b_ext) 2926 { 2927 b_use = b_ext; 2928 if (b_use->u.type) 2929 TREE_TYPE (b_use->decl) = b_use->u.type; 2930 } 2931 } 2932 if (duplicate_decls (x, b_use->decl)) 2933 { 2934 if (b_use != b) 2935 { 2936 /* Save the updated type in the external scope and 2937 restore the proper type for this scope. */ 2938 tree thistype; 2939 if (comptypes (vistype, type)) 2940 thistype = composite_type (vistype, type); 2941 else 2942 thistype = TREE_TYPE (b_use->decl); 2943 b_use->u.type = TREE_TYPE (b_use->decl); 2944 if (TREE_CODE (b_use->decl) == FUNCTION_DECL 2945 && DECL_BUILT_IN (b_use->decl)) 2946 thistype 2947 = build_type_attribute_variant (thistype, 2948 TYPE_ATTRIBUTES 2949 (b_use->u.type)); 2950 TREE_TYPE (b_use->decl) = thistype; 2951 } 2952 return b_use->decl; 2953 } 2954 else 2955 goto skip_external_and_shadow_checks; 2956 } 2957 2958 /* All declarations with external linkage, and all external 2959 references, go in the external scope, no matter what scope is 2960 current. However, the binding in that scope is ignored for 2961 purposes of normal name lookup. A separate binding structure is 2962 created in the requested scope; this governs the normal 2963 visibility of the symbol. 2964 2965 The binding in the externals scope is used exclusively for 2966 detecting duplicate declarations of the same object, no matter 2967 what scope they are in; this is what we do here. (C99 6.2.7p2: 2968 All declarations that refer to the same object or function shall 2969 have compatible type; otherwise, the behavior is undefined.) */ 2970 if (DECL_EXTERNAL (x) || scope == file_scope) 2971 { 2972 tree type = TREE_TYPE (x); 2973 tree vistype = NULL_TREE; 2974 tree visdecl = NULL_TREE; 2975 bool type_saved = false; 2976 if (b && !B_IN_EXTERNAL_SCOPE (b) 2977 && VAR_OR_FUNCTION_DECL_P (b->decl) 2978 && DECL_FILE_SCOPE_P (b->decl)) 2979 { 2980 visdecl = b->decl; 2981 vistype = TREE_TYPE (visdecl); 2982 } 2983 if (scope != file_scope 2984 && !DECL_IN_SYSTEM_HEADER (x)) 2985 warning_at (locus, OPT_Wnested_externs, 2986 "nested extern declaration of %qD", x); 2987 2988 while (b && !B_IN_EXTERNAL_SCOPE (b)) 2989 { 2990 /* If this decl might be modified, save its type. This is 2991 done here rather than when the decl is first bound 2992 because the type may change after first binding, through 2993 being completed or through attributes being added. If we 2994 encounter multiple such decls, only the first should have 2995 its type saved; the others will already have had their 2996 proper types saved and the types will not have changed as 2997 their scopes will not have been re-entered. */ 2998 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved) 2999 { 3000 b->u.type = TREE_TYPE (b->decl); 3001 type_saved = true; 3002 } 3003 if (B_IN_FILE_SCOPE (b) 3004 && VAR_P (b->decl) 3005 && TREE_STATIC (b->decl) 3006 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE 3007 && !TYPE_DOMAIN (TREE_TYPE (b->decl)) 3008 && TREE_CODE (type) == ARRAY_TYPE 3009 && TYPE_DOMAIN (type) 3010 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) 3011 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) 3012 { 3013 /* Array type completed in inner scope, which should be 3014 diagnosed if the completion does not have size 1 and 3015 it does not get completed in the file scope. */ 3016 b->inner_comp = true; 3017 } 3018 b = b->shadowed; 3019 } 3020 3021 /* If a matching external declaration has been found, set its 3022 type to the composite of all the types of that declaration. 3023 After the consistency checks, it will be reset to the 3024 composite of the visible types only. */ 3025 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl)) 3026 && b->u.type) 3027 TREE_TYPE (b->decl) = b->u.type; 3028 3029 /* The point of the same_translation_unit_p check here is, 3030 we want to detect a duplicate decl for a construct like 3031 foo() { extern bar(); } ... static bar(); but not if 3032 they are in different translation units. In any case, 3033 the static does not go in the externals scope. */ 3034 if (b 3035 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl)) 3036 && duplicate_decls (x, b->decl)) 3037 { 3038 tree thistype; 3039 if (vistype) 3040 { 3041 if (comptypes (vistype, type)) 3042 thistype = composite_type (vistype, type); 3043 else 3044 thistype = TREE_TYPE (b->decl); 3045 } 3046 else 3047 thistype = type; 3048 b->u.type = TREE_TYPE (b->decl); 3049 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl)) 3050 thistype 3051 = build_type_attribute_variant (thistype, 3052 TYPE_ATTRIBUTES (b->u.type)); 3053 TREE_TYPE (b->decl) = thistype; 3054 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true, 3055 locus); 3056 return b->decl; 3057 } 3058 else if (TREE_PUBLIC (x)) 3059 { 3060 if (visdecl && !b && duplicate_decls (x, visdecl)) 3061 { 3062 /* An external declaration at block scope referring to a 3063 visible entity with internal linkage. The composite 3064 type will already be correct for this scope, so we 3065 just need to fall through to make the declaration in 3066 this scope. */ 3067 nested = true; 3068 x = visdecl; 3069 } 3070 else 3071 { 3072 bind (name, x, external_scope, /*invisible=*/true, 3073 /*nested=*/false, locus); 3074 nested = true; 3075 } 3076 } 3077 } 3078 3079 if (TREE_CODE (x) != PARM_DECL) 3080 warn_if_shadowing (x); 3081 3082 skip_external_and_shadow_checks: 3083 if (TREE_CODE (x) == TYPE_DECL) 3084 { 3085 /* So this is a typedef, set its underlying type. */ 3086 set_underlying_type (x); 3087 3088 /* If X is a typedef defined in the current function, record it 3089 for the purpose of implementing the -Wunused-local-typedefs 3090 warning. */ 3091 record_locally_defined_typedef (x); 3092 } 3093 3094 bind (name, x, scope, /*invisible=*/false, nested, locus); 3095 3096 /* If x's type is incomplete because it's based on a 3097 structure or union which has not yet been fully declared, 3098 attach it to that structure or union type, so we can go 3099 back and complete the variable declaration later, if the 3100 structure or union gets fully declared. 3101 3102 If the input is erroneous, we can have error_mark in the type 3103 slot (e.g. "f(void a, ...)") - that doesn't count as an 3104 incomplete type. */ 3105 if (TREE_TYPE (x) != error_mark_node 3106 && !COMPLETE_TYPE_P (TREE_TYPE (x))) 3107 { 3108 tree element = TREE_TYPE (x); 3109 3110 while (TREE_CODE (element) == ARRAY_TYPE) 3111 element = TREE_TYPE (element); 3112 element = TYPE_MAIN_VARIANT (element); 3113 3114 if (RECORD_OR_UNION_TYPE_P (element) 3115 && (TREE_CODE (x) != TYPE_DECL 3116 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE) 3117 && !COMPLETE_TYPE_P (element)) 3118 C_TYPE_INCOMPLETE_VARS (element) 3119 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element)); 3120 } 3121 return x; 3122 } 3123 3124 3125 /* Issue a warning about implicit function declaration. ID is the function 3126 identifier, OLDDECL is a declaration of the function in a different scope, 3127 or NULL_TREE. */ 3128 3129 static void 3130 implicit_decl_warning (location_t loc, tree id, tree olddecl) 3131 { 3132 if (!warn_implicit_function_declaration) 3133 return; 3134 3135 bool warned; 3136 name_hint hint; 3137 if (!olddecl) 3138 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc); 3139 3140 if (flag_isoc99) 3141 { 3142 if (hint) 3143 { 3144 gcc_rich_location richloc (loc); 3145 richloc.add_fixit_replace (hint.suggestion ()); 3146 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration, 3147 "implicit declaration of function %qE;" 3148 " did you mean %qs?", 3149 id, hint.suggestion ()); 3150 } 3151 else 3152 warned = pedwarn (loc, OPT_Wimplicit_function_declaration, 3153 "implicit declaration of function %qE", id); 3154 } 3155 else if (hint) 3156 { 3157 gcc_rich_location richloc (loc); 3158 richloc.add_fixit_replace (hint.suggestion ()); 3159 warned = warning_at 3160 (&richloc, OPT_Wimplicit_function_declaration, 3161 G_("implicit declaration of function %qE; did you mean %qs?"), 3162 id, hint.suggestion ()); 3163 } 3164 else 3165 warned = warning_at (loc, OPT_Wimplicit_function_declaration, 3166 G_("implicit declaration of function %qE"), id); 3167 3168 if (olddecl && warned) 3169 locate_old_decl (olddecl); 3170 3171 if (!warned) 3172 hint.suppress (); 3173 } 3174 3175 /* This function represents mapping of a function code FCODE 3176 to its respective header. */ 3177 3178 static const char * 3179 header_for_builtin_fn (enum built_in_function fcode) 3180 { 3181 switch (fcode) 3182 { 3183 CASE_FLT_FN (BUILT_IN_ACOS): 3184 CASE_FLT_FN (BUILT_IN_ACOSH): 3185 CASE_FLT_FN (BUILT_IN_ASIN): 3186 CASE_FLT_FN (BUILT_IN_ASINH): 3187 CASE_FLT_FN (BUILT_IN_ATAN): 3188 CASE_FLT_FN (BUILT_IN_ATANH): 3189 CASE_FLT_FN (BUILT_IN_ATAN2): 3190 CASE_FLT_FN (BUILT_IN_CBRT): 3191 CASE_FLT_FN (BUILT_IN_CEIL): 3192 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL): 3193 CASE_FLT_FN (BUILT_IN_COPYSIGN): 3194 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN): 3195 CASE_FLT_FN (BUILT_IN_COS): 3196 CASE_FLT_FN (BUILT_IN_COSH): 3197 CASE_FLT_FN (BUILT_IN_ERF): 3198 CASE_FLT_FN (BUILT_IN_ERFC): 3199 CASE_FLT_FN (BUILT_IN_EXP): 3200 CASE_FLT_FN (BUILT_IN_EXP2): 3201 CASE_FLT_FN (BUILT_IN_EXPM1): 3202 CASE_FLT_FN (BUILT_IN_FABS): 3203 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS): 3204 CASE_FLT_FN (BUILT_IN_FDIM): 3205 CASE_FLT_FN (BUILT_IN_FLOOR): 3206 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR): 3207 CASE_FLT_FN (BUILT_IN_FMA): 3208 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA): 3209 CASE_FLT_FN (BUILT_IN_FMAX): 3210 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX): 3211 CASE_FLT_FN (BUILT_IN_FMIN): 3212 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN): 3213 CASE_FLT_FN (BUILT_IN_FMOD): 3214 CASE_FLT_FN (BUILT_IN_FREXP): 3215 CASE_FLT_FN (BUILT_IN_HYPOT): 3216 CASE_FLT_FN (BUILT_IN_ILOGB): 3217 CASE_FLT_FN (BUILT_IN_LDEXP): 3218 CASE_FLT_FN (BUILT_IN_LGAMMA): 3219 CASE_FLT_FN (BUILT_IN_LLRINT): 3220 CASE_FLT_FN (BUILT_IN_LLROUND): 3221 CASE_FLT_FN (BUILT_IN_LOG): 3222 CASE_FLT_FN (BUILT_IN_LOG10): 3223 CASE_FLT_FN (BUILT_IN_LOG1P): 3224 CASE_FLT_FN (BUILT_IN_LOG2): 3225 CASE_FLT_FN (BUILT_IN_LOGB): 3226 CASE_FLT_FN (BUILT_IN_LRINT): 3227 CASE_FLT_FN (BUILT_IN_LROUND): 3228 CASE_FLT_FN (BUILT_IN_MODF): 3229 CASE_FLT_FN (BUILT_IN_NAN): 3230 CASE_FLT_FN (BUILT_IN_NEARBYINT): 3231 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT): 3232 CASE_FLT_FN (BUILT_IN_NEXTAFTER): 3233 CASE_FLT_FN (BUILT_IN_NEXTTOWARD): 3234 CASE_FLT_FN (BUILT_IN_POW): 3235 CASE_FLT_FN (BUILT_IN_REMAINDER): 3236 CASE_FLT_FN (BUILT_IN_REMQUO): 3237 CASE_FLT_FN (BUILT_IN_RINT): 3238 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT): 3239 CASE_FLT_FN (BUILT_IN_ROUND): 3240 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND): 3241 CASE_FLT_FN (BUILT_IN_SCALBLN): 3242 CASE_FLT_FN (BUILT_IN_SCALBN): 3243 CASE_FLT_FN (BUILT_IN_SIN): 3244 CASE_FLT_FN (BUILT_IN_SINH): 3245 CASE_FLT_FN (BUILT_IN_SINCOS): 3246 CASE_FLT_FN (BUILT_IN_SQRT): 3247 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT): 3248 CASE_FLT_FN (BUILT_IN_TAN): 3249 CASE_FLT_FN (BUILT_IN_TANH): 3250 CASE_FLT_FN (BUILT_IN_TGAMMA): 3251 CASE_FLT_FN (BUILT_IN_TRUNC): 3252 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC): 3253 case BUILT_IN_ISINF: 3254 case BUILT_IN_ISNAN: 3255 return "<math.h>"; 3256 CASE_FLT_FN (BUILT_IN_CABS): 3257 CASE_FLT_FN (BUILT_IN_CACOS): 3258 CASE_FLT_FN (BUILT_IN_CACOSH): 3259 CASE_FLT_FN (BUILT_IN_CARG): 3260 CASE_FLT_FN (BUILT_IN_CASIN): 3261 CASE_FLT_FN (BUILT_IN_CASINH): 3262 CASE_FLT_FN (BUILT_IN_CATAN): 3263 CASE_FLT_FN (BUILT_IN_CATANH): 3264 CASE_FLT_FN (BUILT_IN_CCOS): 3265 CASE_FLT_FN (BUILT_IN_CCOSH): 3266 CASE_FLT_FN (BUILT_IN_CEXP): 3267 CASE_FLT_FN (BUILT_IN_CIMAG): 3268 CASE_FLT_FN (BUILT_IN_CLOG): 3269 CASE_FLT_FN (BUILT_IN_CONJ): 3270 CASE_FLT_FN (BUILT_IN_CPOW): 3271 CASE_FLT_FN (BUILT_IN_CPROJ): 3272 CASE_FLT_FN (BUILT_IN_CREAL): 3273 CASE_FLT_FN (BUILT_IN_CSIN): 3274 CASE_FLT_FN (BUILT_IN_CSINH): 3275 CASE_FLT_FN (BUILT_IN_CSQRT): 3276 CASE_FLT_FN (BUILT_IN_CTAN): 3277 CASE_FLT_FN (BUILT_IN_CTANH): 3278 return "<complex.h>"; 3279 case BUILT_IN_MEMCHR: 3280 case BUILT_IN_MEMCMP: 3281 case BUILT_IN_MEMCPY: 3282 case BUILT_IN_MEMMOVE: 3283 case BUILT_IN_MEMSET: 3284 case BUILT_IN_STRCAT: 3285 case BUILT_IN_STRCHR: 3286 case BUILT_IN_STRCMP: 3287 case BUILT_IN_STRCPY: 3288 case BUILT_IN_STRCSPN: 3289 case BUILT_IN_STRLEN: 3290 case BUILT_IN_STRNCAT: 3291 case BUILT_IN_STRNCMP: 3292 case BUILT_IN_STRNCPY: 3293 case BUILT_IN_STRPBRK: 3294 case BUILT_IN_STRRCHR: 3295 case BUILT_IN_STRSPN: 3296 case BUILT_IN_STRSTR: 3297 return "<string.h>"; 3298 case BUILT_IN_FPRINTF: 3299 case BUILT_IN_PUTC: 3300 case BUILT_IN_FPUTC: 3301 case BUILT_IN_FPUTS: 3302 case BUILT_IN_FSCANF: 3303 case BUILT_IN_FWRITE: 3304 case BUILT_IN_PRINTF: 3305 case BUILT_IN_PUTCHAR: 3306 case BUILT_IN_PUTS: 3307 case BUILT_IN_SCANF: 3308 case BUILT_IN_SNPRINTF: 3309 case BUILT_IN_SPRINTF: 3310 case BUILT_IN_SSCANF: 3311 case BUILT_IN_VFPRINTF: 3312 case BUILT_IN_VFSCANF: 3313 case BUILT_IN_VPRINTF: 3314 case BUILT_IN_VSCANF: 3315 case BUILT_IN_VSNPRINTF: 3316 case BUILT_IN_VSPRINTF: 3317 case BUILT_IN_VSSCANF: 3318 return "<stdio.h>"; 3319 case BUILT_IN_ISALNUM: 3320 case BUILT_IN_ISALPHA: 3321 case BUILT_IN_ISBLANK: 3322 case BUILT_IN_ISCNTRL: 3323 case BUILT_IN_ISDIGIT: 3324 case BUILT_IN_ISGRAPH: 3325 case BUILT_IN_ISLOWER: 3326 case BUILT_IN_ISPRINT: 3327 case BUILT_IN_ISPUNCT: 3328 case BUILT_IN_ISSPACE: 3329 case BUILT_IN_ISUPPER: 3330 case BUILT_IN_ISXDIGIT: 3331 case BUILT_IN_TOLOWER: 3332 case BUILT_IN_TOUPPER: 3333 return "<ctype.h>"; 3334 case BUILT_IN_ISWALNUM: 3335 case BUILT_IN_ISWALPHA: 3336 case BUILT_IN_ISWBLANK: 3337 case BUILT_IN_ISWCNTRL: 3338 case BUILT_IN_ISWDIGIT: 3339 case BUILT_IN_ISWGRAPH: 3340 case BUILT_IN_ISWLOWER: 3341 case BUILT_IN_ISWPRINT: 3342 case BUILT_IN_ISWPUNCT: 3343 case BUILT_IN_ISWSPACE: 3344 case BUILT_IN_ISWUPPER: 3345 case BUILT_IN_ISWXDIGIT: 3346 case BUILT_IN_TOWLOWER: 3347 case BUILT_IN_TOWUPPER: 3348 return "<wctype.h>"; 3349 case BUILT_IN_ABORT: 3350 case BUILT_IN_ABS: 3351 case BUILT_IN_CALLOC: 3352 case BUILT_IN_EXIT: 3353 case BUILT_IN_FREE: 3354 case BUILT_IN_LABS: 3355 case BUILT_IN_LLABS: 3356 case BUILT_IN_MALLOC: 3357 case BUILT_IN_REALLOC: 3358 case BUILT_IN__EXIT2: 3359 case BUILT_IN_ALIGNED_ALLOC: 3360 return "<stdlib.h>"; 3361 case BUILT_IN_IMAXABS: 3362 return "<inttypes.h>"; 3363 case BUILT_IN_STRFTIME: 3364 return "<time.h>"; 3365 default: 3366 return NULL; 3367 } 3368 } 3369 3370 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a 3371 function of type int (). */ 3372 3373 tree 3374 implicitly_declare (location_t loc, tree functionid) 3375 { 3376 struct c_binding *b; 3377 tree decl = NULL_TREE; 3378 tree asmspec_tree; 3379 3380 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed) 3381 { 3382 if (B_IN_SCOPE (b, external_scope)) 3383 { 3384 decl = b->decl; 3385 break; 3386 } 3387 } 3388 3389 if (decl) 3390 { 3391 if (TREE_CODE (decl) != FUNCTION_DECL) 3392 return decl; 3393 3394 /* FIXME: Objective-C has weird not-really-builtin functions 3395 which are supposed to be visible automatically. They wind up 3396 in the external scope because they're pushed before the file 3397 scope gets created. Catch this here and rebind them into the 3398 file scope. */ 3399 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl)) 3400 { 3401 bind (functionid, decl, file_scope, 3402 /*invisible=*/false, /*nested=*/true, 3403 DECL_SOURCE_LOCATION (decl)); 3404 return decl; 3405 } 3406 else 3407 { 3408 tree newtype = default_function_type; 3409 if (b->u.type) 3410 TREE_TYPE (decl) = b->u.type; 3411 /* Implicit declaration of a function already declared 3412 (somehow) in a different scope, or as a built-in. 3413 If this is the first time this has happened, warn; 3414 then recycle the old declaration but with the new type. */ 3415 if (!C_DECL_IMPLICIT (decl)) 3416 { 3417 implicit_decl_warning (loc, functionid, decl); 3418 C_DECL_IMPLICIT (decl) = 1; 3419 } 3420 if (DECL_BUILT_IN (decl)) 3421 { 3422 newtype = build_type_attribute_variant (newtype, 3423 TYPE_ATTRIBUTES 3424 (TREE_TYPE (decl))); 3425 if (!comptypes (newtype, TREE_TYPE (decl))) 3426 { 3427 bool warned = warning_at (loc, 0, "incompatible implicit " 3428 "declaration of built-in " 3429 "function %qD", decl); 3430 /* See if we can hint which header to include. */ 3431 const char *header 3432 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl)); 3433 if (header != NULL && warned) 3434 { 3435 rich_location richloc (line_table, loc); 3436 maybe_add_include_fixit (&richloc, header); 3437 inform (&richloc, 3438 "include %qs or provide a declaration of %qD", 3439 header, decl); 3440 } 3441 newtype = TREE_TYPE (decl); 3442 } 3443 } 3444 else 3445 { 3446 if (!comptypes (newtype, TREE_TYPE (decl))) 3447 { 3448 error_at (loc, "incompatible implicit declaration of " 3449 "function %qD", decl); 3450 locate_old_decl (decl); 3451 } 3452 } 3453 b->u.type = TREE_TYPE (decl); 3454 TREE_TYPE (decl) = newtype; 3455 bind (functionid, decl, current_scope, 3456 /*invisible=*/false, /*nested=*/true, 3457 DECL_SOURCE_LOCATION (decl)); 3458 return decl; 3459 } 3460 } 3461 3462 /* Not seen before. */ 3463 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type); 3464 DECL_EXTERNAL (decl) = 1; 3465 TREE_PUBLIC (decl) = 1; 3466 C_DECL_IMPLICIT (decl) = 1; 3467 implicit_decl_warning (loc, functionid, 0); 3468 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL); 3469 if (asmspec_tree) 3470 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree)); 3471 3472 /* C89 says implicit declarations are in the innermost block. 3473 So we record the decl in the standard fashion. */ 3474 decl = pushdecl (decl); 3475 3476 /* No need to call objc_check_decl here - it's a function type. */ 3477 rest_of_decl_compilation (decl, 0, 0); 3478 3479 /* Write a record describing this implicit function declaration 3480 to the prototypes file (if requested). */ 3481 gen_aux_info_record (decl, 0, 1, 0); 3482 3483 /* Possibly apply some default attributes to this implicit declaration. */ 3484 decl_attributes (&decl, NULL_TREE, 0); 3485 3486 return decl; 3487 } 3488 3489 /* Issue an error message for a reference to an undeclared variable 3490 ID, including a reference to a builtin outside of function-call 3491 context. Establish a binding of the identifier to error_mark_node 3492 in an appropriate scope, which will suppress further errors for the 3493 same identifier. The error message should be given location LOC. */ 3494 void 3495 undeclared_variable (location_t loc, tree id) 3496 { 3497 static bool already = false; 3498 struct c_scope *scope; 3499 3500 if (current_function_decl == NULL_TREE) 3501 { 3502 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc); 3503 if (guessed_id) 3504 { 3505 gcc_rich_location richloc (loc); 3506 richloc.add_fixit_replace (guessed_id.suggestion ()); 3507 error_at (&richloc, 3508 "%qE undeclared here (not in a function);" 3509 " did you mean %qs?", 3510 id, guessed_id.suggestion ()); 3511 } 3512 else 3513 error_at (loc, "%qE undeclared here (not in a function)", id); 3514 scope = current_scope; 3515 } 3516 else 3517 { 3518 if (!objc_diagnose_private_ivar (id)) 3519 { 3520 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc); 3521 if (guessed_id) 3522 { 3523 gcc_rich_location richloc (loc); 3524 richloc.add_fixit_replace (guessed_id.suggestion ()); 3525 error_at (&richloc, 3526 "%qE undeclared (first use in this function);" 3527 " did you mean %qs?", 3528 id, guessed_id.suggestion ()); 3529 } 3530 else 3531 error_at (loc, "%qE undeclared (first use in this function)", id); 3532 } 3533 if (!already) 3534 { 3535 inform (loc, "each undeclared identifier is reported only" 3536 " once for each function it appears in"); 3537 already = true; 3538 } 3539 3540 /* If we are parsing old-style parameter decls, current_function_decl 3541 will be nonnull but current_function_scope will be null. */ 3542 scope = current_function_scope ? current_function_scope : current_scope; 3543 } 3544 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false, 3545 UNKNOWN_LOCATION); 3546 } 3547 3548 /* Subroutine of lookup_label, declare_label, define_label: construct a 3549 LABEL_DECL with all the proper frills. Also create a struct 3550 c_label_vars initialized for the current scope. */ 3551 3552 static tree 3553 make_label (location_t location, tree name, bool defining, 3554 struct c_label_vars **p_label_vars) 3555 { 3556 tree label = build_decl (location, LABEL_DECL, name, void_type_node); 3557 DECL_CONTEXT (label) = current_function_decl; 3558 SET_DECL_MODE (label, VOIDmode); 3559 3560 c_label_vars *label_vars = ggc_alloc<c_label_vars> (); 3561 label_vars->shadowed = NULL; 3562 set_spot_bindings (&label_vars->label_bindings, defining); 3563 label_vars->decls_in_scope = make_tree_vector (); 3564 label_vars->gotos = NULL; 3565 *p_label_vars = label_vars; 3566 3567 return label; 3568 } 3569 3570 /* Get the LABEL_DECL corresponding to identifier NAME as a label. 3571 Create one if none exists so far for the current function. 3572 This is called when a label is used in a goto expression or 3573 has its address taken. */ 3574 3575 tree 3576 lookup_label (tree name) 3577 { 3578 tree label; 3579 struct c_label_vars *label_vars; 3580 3581 if (current_function_scope == 0) 3582 { 3583 error ("label %qE referenced outside of any function", name); 3584 return NULL_TREE; 3585 } 3586 3587 /* Use a label already defined or ref'd with this name, but not if 3588 it is inherited from a containing function and wasn't declared 3589 using __label__. */ 3590 label = I_LABEL_DECL (name); 3591 if (label && (DECL_CONTEXT (label) == current_function_decl 3592 || C_DECLARED_LABEL_FLAG (label))) 3593 { 3594 /* If the label has only been declared, update its apparent 3595 location to point here, for better diagnostics if it 3596 turns out not to have been defined. */ 3597 if (DECL_INITIAL (label) == NULL_TREE) 3598 DECL_SOURCE_LOCATION (label) = input_location; 3599 return label; 3600 } 3601 3602 /* No label binding for that identifier; make one. */ 3603 label = make_label (input_location, name, false, &label_vars); 3604 3605 /* Ordinary labels go in the current function scope. */ 3606 bind_label (name, label, current_function_scope, label_vars); 3607 3608 return label; 3609 } 3610 3611 /* Issue a warning about DECL for a goto statement at GOTO_LOC going 3612 to LABEL. */ 3613 3614 static void 3615 warn_about_goto (location_t goto_loc, tree label, tree decl) 3616 { 3617 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE)) 3618 error_at (goto_loc, 3619 "jump into scope of identifier with variably modified type"); 3620 else 3621 warning_at (goto_loc, OPT_Wjump_misses_init, 3622 "jump skips variable initialization"); 3623 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label); 3624 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl); 3625 } 3626 3627 /* Look up a label because of a goto statement. This is like 3628 lookup_label, but also issues any appropriate warnings. */ 3629 3630 tree 3631 lookup_label_for_goto (location_t loc, tree name) 3632 { 3633 tree label; 3634 struct c_label_vars *label_vars; 3635 unsigned int ix; 3636 tree decl; 3637 3638 label = lookup_label (name); 3639 if (label == NULL_TREE) 3640 return NULL_TREE; 3641 3642 /* If we are jumping to a different function, we can't issue any 3643 useful warnings. */ 3644 if (DECL_CONTEXT (label) != current_function_decl) 3645 { 3646 gcc_assert (C_DECLARED_LABEL_FLAG (label)); 3647 return label; 3648 } 3649 3650 label_vars = I_LABEL_BINDING (name)->u.label; 3651 3652 /* If the label has not yet been defined, then push this goto on a 3653 list for possible later warnings. */ 3654 if (label_vars->label_bindings.scope == NULL) 3655 { 3656 c_goto_bindings *g = ggc_alloc<c_goto_bindings> (); 3657 3658 g->loc = loc; 3659 set_spot_bindings (&g->goto_bindings, true); 3660 vec_safe_push (label_vars->gotos, g); 3661 return label; 3662 } 3663 3664 /* If there are any decls in label_vars->decls_in_scope, then this 3665 goto has missed the declaration of the decl. This happens for a 3666 case like 3667 int i = 1; 3668 lab: 3669 ... 3670 goto lab; 3671 Issue a warning or error. */ 3672 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl) 3673 warn_about_goto (loc, label, decl); 3674 3675 if (label_vars->label_bindings.left_stmt_expr) 3676 { 3677 error_at (loc, "jump into statement expression"); 3678 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label); 3679 } 3680 3681 return label; 3682 } 3683 3684 /* Make a label named NAME in the current function, shadowing silently 3685 any that may be inherited from containing functions or containing 3686 scopes. This is called for __label__ declarations. */ 3687 3688 tree 3689 declare_label (tree name) 3690 { 3691 struct c_binding *b = I_LABEL_BINDING (name); 3692 tree label; 3693 struct c_label_vars *label_vars; 3694 3695 /* Check to make sure that the label hasn't already been declared 3696 at this scope */ 3697 if (b && B_IN_CURRENT_SCOPE (b)) 3698 { 3699 error ("duplicate label declaration %qE", name); 3700 locate_old_decl (b->decl); 3701 3702 /* Just use the previous declaration. */ 3703 return b->decl; 3704 } 3705 3706 label = make_label (input_location, name, false, &label_vars); 3707 C_DECLARED_LABEL_FLAG (label) = 1; 3708 3709 /* Declared labels go in the current scope. */ 3710 bind_label (name, label, current_scope, label_vars); 3711 3712 return label; 3713 } 3714 3715 /* When we define a label, issue any appropriate warnings if there are 3716 any gotos earlier in the function which jump to this label. */ 3717 3718 static void 3719 check_earlier_gotos (tree label, struct c_label_vars* label_vars) 3720 { 3721 unsigned int ix; 3722 struct c_goto_bindings *g; 3723 3724 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g) 3725 { 3726 struct c_binding *b; 3727 struct c_scope *scope; 3728 3729 /* We have a goto to this label. The goto is going forward. In 3730 g->scope, the goto is going to skip any binding which was 3731 defined after g->bindings_in_scope. */ 3732 if (g->goto_bindings.scope->has_jump_unsafe_decl) 3733 { 3734 for (b = g->goto_bindings.scope->bindings; 3735 b != g->goto_bindings.bindings_in_scope; 3736 b = b->prev) 3737 { 3738 if (decl_jump_unsafe (b->decl)) 3739 warn_about_goto (g->loc, label, b->decl); 3740 } 3741 } 3742 3743 /* We also need to warn about decls defined in any scopes 3744 between the scope of the label and the scope of the goto. */ 3745 for (scope = label_vars->label_bindings.scope; 3746 scope != g->goto_bindings.scope; 3747 scope = scope->outer) 3748 { 3749 gcc_assert (scope != NULL); 3750 if (scope->has_jump_unsafe_decl) 3751 { 3752 if (scope == label_vars->label_bindings.scope) 3753 b = label_vars->label_bindings.bindings_in_scope; 3754 else 3755 b = scope->bindings; 3756 for (; b != NULL; b = b->prev) 3757 { 3758 if (decl_jump_unsafe (b->decl)) 3759 warn_about_goto (g->loc, label, b->decl); 3760 } 3761 } 3762 } 3763 3764 if (g->goto_bindings.stmt_exprs > 0) 3765 { 3766 error_at (g->loc, "jump into statement expression"); 3767 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", 3768 label); 3769 } 3770 } 3771 3772 /* Now that the label is defined, we will issue warnings about 3773 subsequent gotos to this label when we see them. */ 3774 vec_safe_truncate (label_vars->gotos, 0); 3775 label_vars->gotos = NULL; 3776 } 3777 3778 /* Define a label, specifying the location in the source file. 3779 Return the LABEL_DECL node for the label, if the definition is valid. 3780 Otherwise return NULL_TREE. */ 3781 3782 tree 3783 define_label (location_t location, tree name) 3784 { 3785 /* Find any preexisting label with this name. It is an error 3786 if that label has already been defined in this function, or 3787 if there is a containing function with a declared label with 3788 the same name. */ 3789 tree label = I_LABEL_DECL (name); 3790 3791 if (label 3792 && ((DECL_CONTEXT (label) == current_function_decl 3793 && DECL_INITIAL (label) != NULL_TREE) 3794 || (DECL_CONTEXT (label) != current_function_decl 3795 && C_DECLARED_LABEL_FLAG (label)))) 3796 { 3797 error_at (location, "duplicate label %qD", label); 3798 locate_old_decl (label); 3799 return NULL_TREE; 3800 } 3801 else if (label && DECL_CONTEXT (label) == current_function_decl) 3802 { 3803 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label; 3804 3805 /* The label has been used or declared already in this function, 3806 but not defined. Update its location to point to this 3807 definition. */ 3808 DECL_SOURCE_LOCATION (label) = location; 3809 set_spot_bindings (&label_vars->label_bindings, true); 3810 3811 /* Issue warnings as required about any goto statements from 3812 earlier in the function. */ 3813 check_earlier_gotos (label, label_vars); 3814 } 3815 else 3816 { 3817 struct c_label_vars *label_vars; 3818 3819 /* No label binding for that identifier; make one. */ 3820 label = make_label (location, name, true, &label_vars); 3821 3822 /* Ordinary labels go in the current function scope. */ 3823 bind_label (name, label, current_function_scope, label_vars); 3824 } 3825 3826 if (!in_system_header_at (input_location) && lookup_name (name)) 3827 warning_at (location, OPT_Wtraditional, 3828 "traditional C lacks a separate namespace " 3829 "for labels, identifier %qE conflicts", name); 3830 3831 /* Mark label as having been defined. */ 3832 DECL_INITIAL (label) = error_mark_node; 3833 return label; 3834 } 3835 3836 /* Get the bindings for a new switch statement. This is used to issue 3837 warnings as appropriate for jumps from the switch to case or 3838 default labels. */ 3839 3840 struct c_spot_bindings * 3841 c_get_switch_bindings (void) 3842 { 3843 struct c_spot_bindings *switch_bindings; 3844 3845 switch_bindings = XNEW (struct c_spot_bindings); 3846 set_spot_bindings (switch_bindings, true); 3847 return switch_bindings; 3848 } 3849 3850 void 3851 c_release_switch_bindings (struct c_spot_bindings *bindings) 3852 { 3853 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr); 3854 XDELETE (bindings); 3855 } 3856 3857 /* This is called at the point of a case or default label to issue 3858 warnings about decls as needed. It returns true if it found an 3859 error, not just a warning. */ 3860 3861 bool 3862 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings, 3863 location_t switch_loc, location_t case_loc) 3864 { 3865 bool saw_error; 3866 struct c_scope *scope; 3867 3868 saw_error = false; 3869 for (scope = current_scope; 3870 scope != switch_bindings->scope; 3871 scope = scope->outer) 3872 { 3873 struct c_binding *b; 3874 3875 gcc_assert (scope != NULL); 3876 3877 if (!scope->has_jump_unsafe_decl) 3878 continue; 3879 3880 for (b = scope->bindings; b != NULL; b = b->prev) 3881 { 3882 if (decl_jump_unsafe (b->decl)) 3883 { 3884 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE)) 3885 { 3886 saw_error = true; 3887 error_at (case_loc, 3888 ("switch jumps into scope of identifier with " 3889 "variably modified type")); 3890 } 3891 else 3892 warning_at (case_loc, OPT_Wjump_misses_init, 3893 "switch jumps over variable initialization"); 3894 inform (switch_loc, "switch starts here"); 3895 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here", 3896 b->decl); 3897 } 3898 } 3899 } 3900 3901 if (switch_bindings->stmt_exprs > 0) 3902 { 3903 saw_error = true; 3904 error_at (case_loc, "switch jumps into statement expression"); 3905 inform (switch_loc, "switch starts here"); 3906 } 3907 3908 return saw_error; 3909 } 3910 3911 /* Given NAME, an IDENTIFIER_NODE, 3912 return the structure (or union or enum) definition for that name. 3913 If THISLEVEL_ONLY is nonzero, searches only the current_scope. 3914 CODE says which kind of type the caller wants; 3915 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. 3916 If PLOC is not NULL and this returns non-null, it sets *PLOC to the 3917 location where the tag was defined. 3918 If the wrong kind of type is found, an error is reported. */ 3919 3920 static tree 3921 lookup_tag (enum tree_code code, tree name, bool thislevel_only, 3922 location_t *ploc) 3923 { 3924 struct c_binding *b = I_TAG_BINDING (name); 3925 bool thislevel = false; 3926 3927 if (!b || !b->decl) 3928 return NULL_TREE; 3929 3930 /* We only care about whether it's in this level if 3931 thislevel_only was set or it might be a type clash. */ 3932 if (thislevel_only || TREE_CODE (b->decl) != code) 3933 { 3934 /* For our purposes, a tag in the external scope is the same as 3935 a tag in the file scope. (Primarily relevant to Objective-C 3936 and its builtin structure tags, which get pushed before the 3937 file scope is created.) */ 3938 if (B_IN_CURRENT_SCOPE (b) 3939 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b))) 3940 thislevel = true; 3941 } 3942 3943 if (thislevel_only && !thislevel) 3944 return NULL_TREE; 3945 3946 if (TREE_CODE (b->decl) != code) 3947 { 3948 /* Definition isn't the kind we were looking for. */ 3949 pending_invalid_xref = name; 3950 pending_invalid_xref_location = input_location; 3951 3952 /* If in the same binding level as a declaration as a tag 3953 of a different type, this must not be allowed to 3954 shadow that tag, so give the error immediately. 3955 (For example, "struct foo; union foo;" is invalid.) */ 3956 if (thislevel) 3957 pending_xref_error (); 3958 } 3959 3960 if (ploc != NULL) 3961 *ploc = b->locus; 3962 3963 return b->decl; 3964 } 3965 3966 /* Return true if a definition exists for NAME with code CODE. */ 3967 3968 bool 3969 tag_exists_p (enum tree_code code, tree name) 3970 { 3971 struct c_binding *b = I_TAG_BINDING (name); 3972 3973 if (b == NULL || b->decl == NULL_TREE) 3974 return false; 3975 return TREE_CODE (b->decl) == code; 3976 } 3977 3978 /* Print an error message now 3979 for a recent invalid struct, union or enum cross reference. 3980 We don't print them immediately because they are not invalid 3981 when used in the `struct foo;' construct for shadowing. */ 3982 3983 void 3984 pending_xref_error (void) 3985 { 3986 if (pending_invalid_xref != NULL_TREE) 3987 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag", 3988 pending_invalid_xref); 3989 pending_invalid_xref = NULL_TREE; 3990 } 3991 3992 3993 /* Look up NAME in the current scope and its superiors 3994 in the namespace of variables, functions and typedefs. 3995 Return a ..._DECL node of some kind representing its definition, 3996 or return NULL_TREE if it is undefined. */ 3997 3998 tree 3999 lookup_name (tree name) 4000 { 4001 struct c_binding *b = I_SYMBOL_BINDING (name); 4002 if (b && !b->invisible) 4003 { 4004 maybe_record_typedef_use (b->decl); 4005 return b->decl; 4006 } 4007 return NULL_TREE; 4008 } 4009 4010 /* Similar to `lookup_name' but look only at the indicated scope. */ 4011 4012 static tree 4013 lookup_name_in_scope (tree name, struct c_scope *scope) 4014 { 4015 struct c_binding *b; 4016 4017 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed) 4018 if (B_IN_SCOPE (b, scope)) 4019 return b->decl; 4020 return NULL_TREE; 4021 } 4022 4023 /* Look for the closest match for NAME within the currently valid 4024 scopes. 4025 4026 This finds the identifier with the lowest Levenshtein distance to 4027 NAME. If there are multiple candidates with equal minimal distance, 4028 the first one found is returned. Scopes are searched from innermost 4029 outwards, and within a scope in reverse order of declaration, thus 4030 benefiting candidates "near" to the current scope. 4031 4032 The function also looks for similar macro names to NAME, since a 4033 misspelled macro name will not be expanded, and hence looks like an 4034 identifier to the C frontend. 4035 4036 It also looks for start_typename keywords, to detect "singed" vs "signed" 4037 typos. 4038 4039 Use LOC for any deferred diagnostics. */ 4040 4041 name_hint 4042 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc) 4043 { 4044 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 4045 4046 /* First, try some well-known names in the C standard library, in case 4047 the user forgot a #include. */ 4048 const char *header_hint 4049 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name)); 4050 4051 if (header_hint) 4052 return name_hint (NULL, 4053 new suggest_missing_header (loc, 4054 IDENTIFIER_POINTER (name), 4055 header_hint)); 4056 4057 /* Only suggest names reserved for the implementation if NAME begins 4058 with an underscore. */ 4059 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_'); 4060 4061 best_match<tree, tree> bm (name); 4062 4063 /* Look within currently valid scopes. */ 4064 for (c_scope *scope = current_scope; scope; scope = scope->outer) 4065 for (c_binding *binding = scope->bindings; binding; binding = binding->prev) 4066 { 4067 if (!binding->id || binding->invisible) 4068 continue; 4069 if (binding->decl == error_mark_node) 4070 continue; 4071 /* Don't use bindings from implicitly declared functions, 4072 as they were likely misspellings themselves. */ 4073 if (TREE_CODE (binding->decl) == FUNCTION_DECL) 4074 if (C_DECL_IMPLICIT (binding->decl)) 4075 continue; 4076 /* Don't suggest names that are reserved for use by the 4077 implementation, unless NAME began with an underscore. */ 4078 if (!consider_implementation_names) 4079 { 4080 const char *suggestion_str = IDENTIFIER_POINTER (binding->id); 4081 if (name_reserved_for_implementation_p (suggestion_str)) 4082 continue; 4083 } 4084 switch (kind) 4085 { 4086 case FUZZY_LOOKUP_TYPENAME: 4087 if (TREE_CODE (binding->decl) != TYPE_DECL) 4088 continue; 4089 break; 4090 4091 case FUZZY_LOOKUP_FUNCTION_NAME: 4092 if (TREE_CODE (binding->decl) != FUNCTION_DECL) 4093 { 4094 /* Allow function pointers. */ 4095 if ((VAR_P (binding->decl) 4096 || TREE_CODE (binding->decl) == PARM_DECL) 4097 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE 4098 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl))) 4099 == FUNCTION_TYPE)) 4100 break; 4101 continue; 4102 } 4103 break; 4104 4105 default: 4106 break; 4107 } 4108 bm.consider (binding->id); 4109 } 4110 4111 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO" 4112 as: 4113 x = SOME_OTHER_MACRO (y); 4114 then "SOME_OTHER_MACRO" will survive to the frontend and show up 4115 as a misspelled identifier. 4116 4117 Use the best distance so far so that a candidate is only set if 4118 a macro is better than anything so far. This allows early rejection 4119 (without calculating the edit distance) of macro names that must have 4120 distance >= bm.get_best_distance (), and means that we only get a 4121 non-NULL result for best_macro_match if it's better than any of 4122 the identifiers already checked, which avoids needless creation 4123 of identifiers for macro hashnodes. */ 4124 best_macro_match bmm (name, bm.get_best_distance (), parse_in); 4125 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate (); 4126 /* If a macro is the closest so far to NAME, use it, creating an 4127 identifier tree node for it. */ 4128 if (best_macro) 4129 { 4130 const char *id = (const char *)best_macro->ident.str; 4131 tree macro_as_identifier 4132 = get_identifier_with_length (id, best_macro->ident.len); 4133 bm.set_best_so_far (macro_as_identifier, 4134 bmm.get_best_distance (), 4135 bmm.get_best_candidate_length ()); 4136 } 4137 4138 /* Try the "start_typename" keywords to detect 4139 "singed" vs "signed" typos. */ 4140 if (kind == FUZZY_LOOKUP_TYPENAME) 4141 { 4142 for (unsigned i = 0; i < num_c_common_reswords; i++) 4143 { 4144 const c_common_resword *resword = &c_common_reswords[i]; 4145 if (!c_keyword_starts_typename (resword->rid)) 4146 continue; 4147 tree resword_identifier = ridpointers [resword->rid]; 4148 if (!resword_identifier) 4149 continue; 4150 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE); 4151 bm.consider (resword_identifier); 4152 } 4153 } 4154 4155 tree best = bm.get_best_meaningful_candidate (); 4156 if (best) 4157 return name_hint (IDENTIFIER_POINTER (best), NULL); 4158 else 4159 return name_hint (NULL, NULL); 4160 } 4161 4162 4163 /* Create the predefined scalar types of C, 4164 and some nodes representing standard constants (0, 1, (void *) 0). 4165 Initialize the global scope. 4166 Make definitions for built-in primitive functions. */ 4167 4168 void 4169 c_init_decl_processing (void) 4170 { 4171 location_t save_loc = input_location; 4172 4173 /* Initialize reserved words for parser. */ 4174 c_parse_init (); 4175 4176 current_function_decl = NULL_TREE; 4177 4178 gcc_obstack_init (&parser_obstack); 4179 4180 /* Make the externals scope. */ 4181 push_scope (); 4182 external_scope = current_scope; 4183 4184 /* Declarations from c_common_nodes_and_builtins must not be associated 4185 with this input file, lest we get differences between using and not 4186 using preprocessed headers. */ 4187 input_location = BUILTINS_LOCATION; 4188 4189 c_common_nodes_and_builtins (); 4190 4191 /* In C, comparisons and TRUTH_* expressions have type int. */ 4192 truthvalue_type_node = integer_type_node; 4193 truthvalue_true_node = integer_one_node; 4194 truthvalue_false_node = integer_zero_node; 4195 4196 /* Even in C99, which has a real boolean type. */ 4197 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"), 4198 boolean_type_node)); 4199 4200 input_location = save_loc; 4201 4202 make_fname_decl = c_make_fname_decl; 4203 start_fname_decls (); 4204 } 4205 4206 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to 4207 give the decl, NAME is the initialization string and TYPE_DEP 4208 indicates whether NAME depended on the type of the function. As we 4209 don't yet implement delayed emission of static data, we mark the 4210 decl as emitted so it is not placed in the output. Anything using 4211 it must therefore pull out the STRING_CST initializer directly. 4212 FIXME. */ 4213 4214 static tree 4215 c_make_fname_decl (location_t loc, tree id, int type_dep) 4216 { 4217 const char *name = fname_as_string (type_dep); 4218 tree decl, type, init; 4219 size_t length = strlen (name); 4220 4221 type = build_array_type (char_type_node, 4222 build_index_type (size_int (length))); 4223 type = c_build_qualified_type (type, TYPE_QUAL_CONST); 4224 4225 decl = build_decl (loc, VAR_DECL, id, type); 4226 4227 TREE_STATIC (decl) = 1; 4228 TREE_READONLY (decl) = 1; 4229 DECL_ARTIFICIAL (decl) = 1; 4230 4231 init = build_string (length + 1, name); 4232 free (CONST_CAST (char *, name)); 4233 TREE_TYPE (init) = type; 4234 DECL_INITIAL (decl) = init; 4235 4236 TREE_USED (decl) = 1; 4237 4238 if (current_function_decl 4239 /* For invalid programs like this: 4240 4241 void foo() 4242 const char* p = __FUNCTION__; 4243 4244 the __FUNCTION__ is believed to appear in K&R style function 4245 parameter declarator. In that case we still don't have 4246 function_scope. */ 4247 && current_function_scope) 4248 { 4249 DECL_CONTEXT (decl) = current_function_decl; 4250 bind (id, decl, current_function_scope, 4251 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION); 4252 } 4253 4254 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE); 4255 4256 return decl; 4257 } 4258 4259 tree 4260 c_builtin_function (tree decl) 4261 { 4262 tree type = TREE_TYPE (decl); 4263 tree id = DECL_NAME (decl); 4264 4265 const char *name = IDENTIFIER_POINTER (id); 4266 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type); 4267 4268 /* Should never be called on a symbol with a preexisting meaning. */ 4269 gcc_assert (!I_SYMBOL_BINDING (id)); 4270 4271 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false, 4272 UNKNOWN_LOCATION); 4273 4274 /* Builtins in the implementation namespace are made visible without 4275 needing to be explicitly declared. See push_file_scope. */ 4276 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1]))) 4277 { 4278 DECL_CHAIN (decl) = visible_builtins; 4279 visible_builtins = decl; 4280 } 4281 4282 return decl; 4283 } 4284 4285 tree 4286 c_builtin_function_ext_scope (tree decl) 4287 { 4288 tree type = TREE_TYPE (decl); 4289 tree id = DECL_NAME (decl); 4290 4291 const char *name = IDENTIFIER_POINTER (id); 4292 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type); 4293 4294 if (external_scope) 4295 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false, 4296 UNKNOWN_LOCATION); 4297 4298 /* Builtins in the implementation namespace are made visible without 4299 needing to be explicitly declared. See push_file_scope. */ 4300 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1]))) 4301 { 4302 DECL_CHAIN (decl) = visible_builtins; 4303 visible_builtins = decl; 4304 } 4305 4306 return decl; 4307 } 4308 4309 /* Called when a declaration is seen that contains no names to declare. 4310 If its type is a reference to a structure, union or enum inherited 4311 from a containing scope, shadow that tag name for the current scope 4312 with a forward reference. 4313 If its type defines a new named structure or union 4314 or defines an enum, it is valid but we need not do anything here. 4315 Otherwise, it is an error. */ 4316 4317 void 4318 shadow_tag (const struct c_declspecs *declspecs) 4319 { 4320 shadow_tag_warned (declspecs, 0); 4321 } 4322 4323 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning, 4324 but no pedwarn. */ 4325 void 4326 shadow_tag_warned (const struct c_declspecs *declspecs, int warned) 4327 { 4328 bool found_tag = false; 4329 4330 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p) 4331 { 4332 tree value = declspecs->type; 4333 enum tree_code code = TREE_CODE (value); 4334 4335 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE) 4336 /* Used to test also that TYPE_SIZE (value) != 0. 4337 That caused warning for `struct foo;' at top level in the file. */ 4338 { 4339 tree name = TYPE_NAME (value); 4340 tree t; 4341 4342 found_tag = true; 4343 4344 if (declspecs->restrict_p) 4345 { 4346 error ("invalid use of %<restrict%>"); 4347 warned = 1; 4348 } 4349 4350 if (name == NULL_TREE) 4351 { 4352 if (warned != 1 && code != ENUMERAL_TYPE) 4353 /* Empty unnamed enum OK */ 4354 { 4355 pedwarn (input_location, 0, 4356 "unnamed struct/union that defines no instances"); 4357 warned = 1; 4358 } 4359 } 4360 else if (declspecs->typespec_kind != ctsk_tagdef 4361 && declspecs->typespec_kind != ctsk_tagfirstref 4362 && declspecs->storage_class != csc_none) 4363 { 4364 if (warned != 1) 4365 pedwarn (input_location, 0, 4366 "empty declaration with storage class specifier " 4367 "does not redeclare tag"); 4368 warned = 1; 4369 pending_xref_error (); 4370 } 4371 else if (declspecs->typespec_kind != ctsk_tagdef 4372 && declspecs->typespec_kind != ctsk_tagfirstref 4373 && (declspecs->const_p 4374 || declspecs->volatile_p 4375 || declspecs->atomic_p 4376 || declspecs->restrict_p 4377 || declspecs->address_space)) 4378 { 4379 if (warned != 1) 4380 pedwarn (input_location, 0, 4381 "empty declaration with type qualifier " 4382 "does not redeclare tag"); 4383 warned = 1; 4384 pending_xref_error (); 4385 } 4386 else if (declspecs->typespec_kind != ctsk_tagdef 4387 && declspecs->typespec_kind != ctsk_tagfirstref 4388 && declspecs->alignas_p) 4389 { 4390 if (warned != 1) 4391 pedwarn (input_location, 0, 4392 "empty declaration with %<_Alignas%> " 4393 "does not redeclare tag"); 4394 warned = 1; 4395 pending_xref_error (); 4396 } 4397 else 4398 { 4399 pending_invalid_xref = NULL_TREE; 4400 t = lookup_tag (code, name, true, NULL); 4401 4402 if (t == NULL_TREE) 4403 { 4404 t = make_node (code); 4405 pushtag (input_location, name, t); 4406 } 4407 } 4408 } 4409 else 4410 { 4411 if (warned != 1 && !in_system_header_at (input_location)) 4412 { 4413 pedwarn (input_location, 0, 4414 "useless type name in empty declaration"); 4415 warned = 1; 4416 } 4417 } 4418 } 4419 else if (warned != 1 && !in_system_header_at (input_location) 4420 && declspecs->typedef_p) 4421 { 4422 pedwarn (input_location, 0, "useless type name in empty declaration"); 4423 warned = 1; 4424 } 4425 4426 pending_invalid_xref = NULL_TREE; 4427 4428 if (declspecs->inline_p) 4429 { 4430 error ("%<inline%> in empty declaration"); 4431 warned = 1; 4432 } 4433 4434 if (declspecs->noreturn_p) 4435 { 4436 error ("%<_Noreturn%> in empty declaration"); 4437 warned = 1; 4438 } 4439 4440 if (current_scope == file_scope && declspecs->storage_class == csc_auto) 4441 { 4442 error ("%<auto%> in file-scope empty declaration"); 4443 warned = 1; 4444 } 4445 4446 if (current_scope == file_scope && declspecs->storage_class == csc_register) 4447 { 4448 error ("%<register%> in file-scope empty declaration"); 4449 warned = 1; 4450 } 4451 4452 if (!warned && !in_system_header_at (input_location) 4453 && declspecs->storage_class != csc_none) 4454 { 4455 warning (0, "useless storage class specifier in empty declaration"); 4456 warned = 2; 4457 } 4458 4459 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p) 4460 { 4461 warning (0, "useless %qs in empty declaration", 4462 declspecs->thread_gnu_p ? "__thread" : "_Thread_local"); 4463 warned = 2; 4464 } 4465 4466 if (!warned 4467 && !in_system_header_at (input_location) 4468 && (declspecs->const_p 4469 || declspecs->volatile_p 4470 || declspecs->atomic_p 4471 || declspecs->restrict_p 4472 || declspecs->address_space)) 4473 { 4474 warning (0, "useless type qualifier in empty declaration"); 4475 warned = 2; 4476 } 4477 4478 if (!warned && !in_system_header_at (input_location) 4479 && declspecs->alignas_p) 4480 { 4481 warning (0, "useless %<_Alignas%> in empty declaration"); 4482 warned = 2; 4483 } 4484 4485 if (warned != 1) 4486 { 4487 if (!found_tag) 4488 pedwarn (input_location, 0, "empty declaration"); 4489 } 4490 } 4491 4492 4493 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_* 4494 bits. SPECS represents declaration specifiers that the grammar 4495 only permits to contain type qualifiers and attributes. */ 4496 4497 int 4498 quals_from_declspecs (const struct c_declspecs *specs) 4499 { 4500 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0) 4501 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0) 4502 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0) 4503 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0) 4504 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space))); 4505 gcc_assert (!specs->type 4506 && !specs->decl_attr 4507 && specs->typespec_word == cts_none 4508 && specs->storage_class == csc_none 4509 && !specs->typedef_p 4510 && !specs->explicit_signed_p 4511 && !specs->deprecated_p 4512 && !specs->long_p 4513 && !specs->long_long_p 4514 && !specs->short_p 4515 && !specs->signed_p 4516 && !specs->unsigned_p 4517 && !specs->complex_p 4518 && !specs->inline_p 4519 && !specs->noreturn_p 4520 && !specs->thread_p); 4521 return quals; 4522 } 4523 4524 /* Construct an array declarator. LOC is the location of the 4525 beginning of the array (usually the opening brace). EXPR is the 4526 expression inside [], or NULL_TREE. QUALS are the type qualifiers 4527 inside the [] (to be applied to the pointer to which a parameter 4528 array is converted). STATIC_P is true if "static" is inside the 4529 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a 4530 VLA of unspecified length which is nevertheless a complete type, 4531 false otherwise. The field for the contained declarator is left to 4532 be filled in by set_array_declarator_inner. */ 4533 4534 struct c_declarator * 4535 build_array_declarator (location_t loc, 4536 tree expr, struct c_declspecs *quals, bool static_p, 4537 bool vla_unspec_p) 4538 { 4539 struct c_declarator *declarator = XOBNEW (&parser_obstack, 4540 struct c_declarator); 4541 declarator->id_loc = loc; 4542 declarator->kind = cdk_array; 4543 declarator->declarator = 0; 4544 declarator->u.array.dimen = expr; 4545 if (quals) 4546 { 4547 declarator->u.array.attrs = quals->attrs; 4548 declarator->u.array.quals = quals_from_declspecs (quals); 4549 } 4550 else 4551 { 4552 declarator->u.array.attrs = NULL_TREE; 4553 declarator->u.array.quals = 0; 4554 } 4555 declarator->u.array.static_p = static_p; 4556 declarator->u.array.vla_unspec_p = vla_unspec_p; 4557 if (static_p || quals != NULL) 4558 pedwarn_c90 (loc, OPT_Wpedantic, 4559 "ISO C90 does not support %<static%> or type " 4560 "qualifiers in parameter array declarators"); 4561 if (vla_unspec_p) 4562 pedwarn_c90 (loc, OPT_Wpedantic, 4563 "ISO C90 does not support %<[*]%> array declarators"); 4564 if (vla_unspec_p) 4565 { 4566 if (!current_scope->parm_flag) 4567 { 4568 /* C99 6.7.5.2p4 */ 4569 error_at (loc, "%<[*]%> not allowed in other than " 4570 "function prototype scope"); 4571 declarator->u.array.vla_unspec_p = false; 4572 return NULL; 4573 } 4574 current_scope->had_vla_unspec = true; 4575 } 4576 return declarator; 4577 } 4578 4579 /* Set the contained declarator of an array declarator. DECL is the 4580 declarator, as constructed by build_array_declarator; INNER is what 4581 appears on the left of the []. */ 4582 4583 struct c_declarator * 4584 set_array_declarator_inner (struct c_declarator *decl, 4585 struct c_declarator *inner) 4586 { 4587 decl->declarator = inner; 4588 return decl; 4589 } 4590 4591 /* INIT is a constructor that forms DECL's initializer. If the final 4592 element initializes a flexible array field, add the size of that 4593 initializer to DECL's size. */ 4594 4595 static void 4596 add_flexible_array_elts_to_size (tree decl, tree init) 4597 { 4598 tree elt, type; 4599 4600 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) 4601 return; 4602 4603 elt = CONSTRUCTOR_ELTS (init)->last ().value; 4604 type = TREE_TYPE (elt); 4605 if (TREE_CODE (type) == ARRAY_TYPE 4606 && TYPE_SIZE (type) == NULL_TREE 4607 && TYPE_DOMAIN (type) != NULL_TREE 4608 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE) 4609 { 4610 complete_array_type (&type, elt, false); 4611 DECL_SIZE (decl) 4612 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type)); 4613 DECL_SIZE_UNIT (decl) 4614 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type)); 4615 } 4616 } 4617 4618 /* Decode a "typename", such as "int **", returning a ..._TYPE node. 4619 Set *EXPR, if EXPR not NULL, to any expression to be evaluated 4620 before the type name, and set *EXPR_CONST_OPERANDS, if 4621 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may 4622 appear in a constant expression. */ 4623 4624 tree 4625 groktypename (struct c_type_name *type_name, tree *expr, 4626 bool *expr_const_operands) 4627 { 4628 tree type; 4629 tree attrs = type_name->specs->attrs; 4630 4631 type_name->specs->attrs = NULL_TREE; 4632 4633 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME, 4634 false, NULL, &attrs, expr, expr_const_operands, 4635 DEPRECATED_NORMAL); 4636 4637 /* Apply attributes. */ 4638 decl_attributes (&type, attrs, 0); 4639 4640 return type; 4641 } 4642 4643 /* Wrapper for decl_attributes that adds some implicit attributes 4644 to VAR_DECLs or FUNCTION_DECLs. */ 4645 4646 static tree 4647 c_decl_attributes (tree *node, tree attributes, int flags) 4648 { 4649 /* Add implicit "omp declare target" attribute if requested. */ 4650 if (current_omp_declare_target_attribute 4651 && ((VAR_P (*node) && is_global_var (*node)) 4652 || TREE_CODE (*node) == FUNCTION_DECL)) 4653 { 4654 if (VAR_P (*node) 4655 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node))) 4656 attributes = tree_cons (get_identifier ("omp declare target implicit"), 4657 NULL_TREE, attributes); 4658 else 4659 attributes = tree_cons (get_identifier ("omp declare target"), 4660 NULL_TREE, attributes); 4661 } 4662 4663 /* Look up the current declaration with all the attributes merged 4664 so far so that attributes on the current declaration that's 4665 about to be pushed that conflict with the former can be detected, 4666 diagnosed, and rejected as appropriate. */ 4667 tree last_decl = lookup_name (DECL_NAME (*node)); 4668 if (!last_decl) 4669 last_decl = lookup_name_in_scope (DECL_NAME (*node), external_scope); 4670 4671 return decl_attributes (node, attributes, flags, last_decl); 4672 } 4673 4674 4675 /* Decode a declarator in an ordinary declaration or data definition. 4676 This is called as soon as the type information and variable name 4677 have been parsed, before parsing the initializer if any. 4678 Here we create the ..._DECL node, fill in its type, 4679 and put it on the list of decls for the current context. 4680 The ..._DECL node is returned as the value. 4681 4682 Exception: for arrays where the length is not specified, 4683 the type is left null, to be filled in by `finish_decl'. 4684 4685 Function definitions do not come here; they go to start_function 4686 instead. However, external and forward declarations of functions 4687 do go through here. Structure field declarations are done by 4688 grokfield and not through here. */ 4689 4690 tree 4691 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs, 4692 bool initialized, tree attributes) 4693 { 4694 tree decl; 4695 tree tem; 4696 tree expr = NULL_TREE; 4697 enum deprecated_states deprecated_state = DEPRECATED_NORMAL; 4698 4699 /* An object declared as __attribute__((deprecated)) suppresses 4700 warnings of uses of other deprecated items. */ 4701 if (lookup_attribute ("deprecated", attributes)) 4702 deprecated_state = DEPRECATED_SUPPRESS; 4703 4704 decl = grokdeclarator (declarator, declspecs, 4705 NORMAL, initialized, NULL, &attributes, &expr, NULL, 4706 deprecated_state); 4707 if (!decl || decl == error_mark_node) 4708 return NULL_TREE; 4709 4710 if (expr) 4711 add_stmt (fold_convert (void_type_node, expr)); 4712 4713 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))) 4714 warning (OPT_Wmain, "%q+D is usually a function", decl); 4715 4716 if (initialized) 4717 /* Is it valid for this decl to have an initializer at all? 4718 If not, set INITIALIZED to zero, which will indirectly 4719 tell 'finish_decl' to ignore the initializer once it is parsed. */ 4720 switch (TREE_CODE (decl)) 4721 { 4722 case TYPE_DECL: 4723 error ("typedef %qD is initialized (use __typeof__ instead)", decl); 4724 initialized = false; 4725 break; 4726 4727 case FUNCTION_DECL: 4728 error ("function %qD is initialized like a variable", decl); 4729 initialized = false; 4730 break; 4731 4732 case PARM_DECL: 4733 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */ 4734 error ("parameter %qD is initialized", decl); 4735 initialized = false; 4736 break; 4737 4738 default: 4739 /* Don't allow initializations for incomplete types except for 4740 arrays which might be completed by the initialization. */ 4741 4742 /* This can happen if the array size is an undefined macro. 4743 We already gave a warning, so we don't need another one. */ 4744 if (TREE_TYPE (decl) == error_mark_node) 4745 initialized = false; 4746 else if (COMPLETE_TYPE_P (TREE_TYPE (decl))) 4747 { 4748 /* A complete type is ok if size is fixed. */ 4749 4750 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST 4751 || C_DECL_VARIABLE_SIZE (decl)) 4752 { 4753 error ("variable-sized object may not be initialized"); 4754 initialized = false; 4755 } 4756 } 4757 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE) 4758 { 4759 error ("variable %qD has initializer but incomplete type", decl); 4760 initialized = false; 4761 } 4762 else if (C_DECL_VARIABLE_SIZE (decl)) 4763 { 4764 /* Although C99 is unclear about whether incomplete arrays 4765 of VLAs themselves count as VLAs, it does not make 4766 sense to permit them to be initialized given that 4767 ordinary VLAs may not be initialized. */ 4768 error ("variable-sized object may not be initialized"); 4769 initialized = false; 4770 } 4771 } 4772 4773 if (initialized) 4774 { 4775 if (current_scope == file_scope) 4776 TREE_STATIC (decl) = 1; 4777 4778 /* Tell 'pushdecl' this is an initialized decl 4779 even though we don't yet have the initializer expression. 4780 Also tell 'finish_decl' it may store the real initializer. */ 4781 DECL_INITIAL (decl) = error_mark_node; 4782 } 4783 4784 /* If this is a function declaration, write a record describing it to the 4785 prototypes file (if requested). */ 4786 4787 if (TREE_CODE (decl) == FUNCTION_DECL) 4788 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl))); 4789 4790 /* ANSI specifies that a tentative definition which is not merged with 4791 a non-tentative definition behaves exactly like a definition with an 4792 initializer equal to zero. (Section 3.7.2) 4793 4794 -fno-common gives strict ANSI behavior, though this tends to break 4795 a large body of code that grew up without this rule. 4796 4797 Thread-local variables are never common, since there's no entrenched 4798 body of code to break, and it allows more efficient variable references 4799 in the presence of dynamic linking. */ 4800 4801 if (VAR_P (decl) 4802 && !initialized 4803 && TREE_PUBLIC (decl) 4804 && !DECL_THREAD_LOCAL_P (decl) 4805 && !flag_no_common) 4806 DECL_COMMON (decl) = 1; 4807 4808 /* Set attributes here so if duplicate decl, will have proper attributes. */ 4809 c_decl_attributes (&decl, attributes, 0); 4810 4811 /* Handle gnu_inline attribute. */ 4812 if (declspecs->inline_p 4813 && !flag_gnu89_inline 4814 && TREE_CODE (decl) == FUNCTION_DECL 4815 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)) 4816 || current_function_decl)) 4817 { 4818 if (declspecs->storage_class == csc_auto && current_scope != file_scope) 4819 ; 4820 else if (declspecs->storage_class != csc_static) 4821 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl); 4822 } 4823 4824 if (TREE_CODE (decl) == FUNCTION_DECL 4825 && targetm.calls.promote_prototypes (TREE_TYPE (decl))) 4826 { 4827 struct c_declarator *ce = declarator; 4828 4829 if (ce->kind == cdk_pointer) 4830 ce = declarator->declarator; 4831 if (ce->kind == cdk_function) 4832 { 4833 tree args = ce->u.arg_info->parms; 4834 for (; args; args = DECL_CHAIN (args)) 4835 { 4836 tree type = TREE_TYPE (args); 4837 if (type && INTEGRAL_TYPE_P (type) 4838 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 4839 DECL_ARG_TYPE (args) = c_type_promotes_to (type); 4840 } 4841 } 4842 } 4843 4844 if (TREE_CODE (decl) == FUNCTION_DECL 4845 && DECL_DECLARED_INLINE_P (decl) 4846 && DECL_UNINLINABLE (decl) 4847 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) 4848 warning (OPT_Wattributes, "inline function %q+D given attribute noinline", 4849 decl); 4850 4851 /* C99 6.7.4p3: An inline definition of a function with external 4852 linkage shall not contain a definition of a modifiable object 4853 with static storage duration... */ 4854 if (VAR_P (decl) 4855 && current_scope != file_scope 4856 && TREE_STATIC (decl) 4857 && !TREE_READONLY (decl) 4858 && DECL_DECLARED_INLINE_P (current_function_decl) 4859 && DECL_EXTERNAL (current_function_decl)) 4860 record_inline_static (input_location, current_function_decl, 4861 decl, csi_modifiable); 4862 4863 if (c_dialect_objc () 4864 && VAR_OR_FUNCTION_DECL_P (decl)) 4865 objc_check_global_decl (decl); 4866 4867 /* Add this decl to the current scope. 4868 TEM may equal DECL or it may be a previous decl of the same name. */ 4869 tem = pushdecl (decl); 4870 4871 if (initialized && DECL_EXTERNAL (tem)) 4872 { 4873 DECL_EXTERNAL (tem) = 0; 4874 TREE_STATIC (tem) = 1; 4875 } 4876 4877 return tem; 4878 } 4879 4880 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object 4881 DECL or the non-array element type if DECL is an uninitialized array. 4882 If that type has a const member, diagnose this. */ 4883 4884 static void 4885 diagnose_uninitialized_cst_member (tree decl, tree type) 4886 { 4887 tree field; 4888 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 4889 { 4890 tree field_type; 4891 if (TREE_CODE (field) != FIELD_DECL) 4892 continue; 4893 field_type = strip_array_types (TREE_TYPE (field)); 4894 4895 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST) 4896 { 4897 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat, 4898 "uninitialized const member in %qT is invalid in C++", 4899 strip_array_types (TREE_TYPE (decl))); 4900 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field); 4901 } 4902 4903 if (RECORD_OR_UNION_TYPE_P (field_type)) 4904 diagnose_uninitialized_cst_member (decl, field_type); 4905 } 4906 } 4907 4908 /* Finish processing of a declaration; 4909 install its initial value. 4910 If ORIGTYPE is not NULL_TREE, it is the original type of INIT. 4911 If the length of an array type is not known before, 4912 it must be determined now, from the initial value, or it is an error. 4913 4914 INIT_LOC is the location of the initial value. */ 4915 4916 void 4917 finish_decl (tree decl, location_t init_loc, tree init, 4918 tree origtype, tree asmspec_tree) 4919 { 4920 tree type; 4921 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE); 4922 const char *asmspec = 0; 4923 4924 /* If a name was specified, get the string. */ 4925 if (VAR_OR_FUNCTION_DECL_P (decl) 4926 && DECL_FILE_SCOPE_P (decl)) 4927 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); 4928 if (asmspec_tree) 4929 asmspec = TREE_STRING_POINTER (asmspec_tree); 4930 4931 if (VAR_P (decl) 4932 && TREE_STATIC (decl) 4933 && global_bindings_p ()) 4934 /* So decl is a global variable. Record the types it uses 4935 so that we can decide later to emit debug info for them. */ 4936 record_types_used_by_current_var_decl (decl); 4937 4938 /* If `start_decl' didn't like having an initialization, ignore it now. */ 4939 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE) 4940 init = NULL_TREE; 4941 4942 /* Don't crash if parm is initialized. */ 4943 if (TREE_CODE (decl) == PARM_DECL) 4944 init = NULL_TREE; 4945 4946 if (init) 4947 store_init_value (init_loc, decl, init, origtype); 4948 4949 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl) 4950 || TREE_CODE (decl) == FIELD_DECL)) 4951 objc_check_decl (decl); 4952 4953 type = TREE_TYPE (decl); 4954 4955 /* Deduce size of array from initialization, if not already known. */ 4956 if (TREE_CODE (type) == ARRAY_TYPE 4957 && TYPE_DOMAIN (type) == NULL_TREE 4958 && TREE_CODE (decl) != TYPE_DECL) 4959 { 4960 bool do_default 4961 = (TREE_STATIC (decl) 4962 /* Even if pedantic, an external linkage array 4963 may have incomplete type at first. */ 4964 ? pedantic && !TREE_PUBLIC (decl) 4965 : !DECL_EXTERNAL (decl)); 4966 int failure 4967 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl), 4968 do_default); 4969 4970 /* Get the completed type made by complete_array_type. */ 4971 type = TREE_TYPE (decl); 4972 4973 switch (failure) 4974 { 4975 case 1: 4976 error ("initializer fails to determine size of %q+D", decl); 4977 break; 4978 4979 case 2: 4980 if (do_default) 4981 error ("array size missing in %q+D", decl); 4982 /* If a `static' var's size isn't known, 4983 make it extern as well as static, so it does not get 4984 allocated. 4985 If it is not `static', then do not mark extern; 4986 finish_incomplete_decl will give it a default size 4987 and it will get allocated. */ 4988 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) 4989 DECL_EXTERNAL (decl) = 1; 4990 break; 4991 4992 case 3: 4993 error ("zero or negative size array %q+D", decl); 4994 break; 4995 4996 case 0: 4997 /* For global variables, update the copy of the type that 4998 exists in the binding. */ 4999 if (TREE_PUBLIC (decl)) 5000 { 5001 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl)); 5002 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext)) 5003 b_ext = b_ext->shadowed; 5004 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl)) 5005 { 5006 if (b_ext->u.type && comptypes (b_ext->u.type, type)) 5007 b_ext->u.type = composite_type (b_ext->u.type, type); 5008 else 5009 b_ext->u.type = type; 5010 } 5011 } 5012 break; 5013 5014 default: 5015 gcc_unreachable (); 5016 } 5017 5018 if (DECL_INITIAL (decl)) 5019 TREE_TYPE (DECL_INITIAL (decl)) = type; 5020 5021 relayout_decl (decl); 5022 } 5023 5024 if (VAR_P (decl)) 5025 { 5026 if (init && TREE_CODE (init) == CONSTRUCTOR) 5027 add_flexible_array_elts_to_size (decl, init); 5028 5029 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node 5030 && COMPLETE_TYPE_P (TREE_TYPE (decl))) 5031 layout_decl (decl, 0); 5032 5033 if (DECL_SIZE (decl) == NULL_TREE 5034 /* Don't give an error if we already gave one earlier. */ 5035 && TREE_TYPE (decl) != error_mark_node 5036 && (TREE_STATIC (decl) 5037 /* A static variable with an incomplete type 5038 is an error if it is initialized. 5039 Also if it is not file scope. 5040 Otherwise, let it through, but if it is not `extern' 5041 then it may cause an error message later. */ 5042 ? (DECL_INITIAL (decl) != NULL_TREE 5043 || !DECL_FILE_SCOPE_P (decl)) 5044 /* An automatic variable with an incomplete type 5045 is an error. */ 5046 : !DECL_EXTERNAL (decl))) 5047 { 5048 error ("storage size of %q+D isn%'t known", decl); 5049 TREE_TYPE (decl) = error_mark_node; 5050 } 5051 5052 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)) 5053 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) 5054 && DECL_SIZE (decl) == NULL_TREE 5055 && TREE_STATIC (decl)) 5056 incomplete_record_decls.safe_push (decl); 5057 5058 if (is_global_var (decl) && DECL_SIZE (decl) != NULL_TREE) 5059 { 5060 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) 5061 constant_expression_warning (DECL_SIZE (decl)); 5062 else 5063 { 5064 error ("storage size of %q+D isn%'t constant", decl); 5065 TREE_TYPE (decl) = error_mark_node; 5066 } 5067 } 5068 5069 if (TREE_USED (type)) 5070 { 5071 TREE_USED (decl) = 1; 5072 DECL_READ_P (decl) = 1; 5073 } 5074 } 5075 5076 /* If this is a function and an assembler name is specified, reset DECL_RTL 5077 so we can give it its new name. Also, update builtin_decl if it 5078 was a normal built-in. */ 5079 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec) 5080 { 5081 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) 5082 set_builtin_user_assembler_name (decl, asmspec); 5083 set_user_assembler_name (decl, asmspec); 5084 } 5085 5086 /* If #pragma weak was used, mark the decl weak now. */ 5087 maybe_apply_pragma_weak (decl); 5088 5089 /* Output the assembler code and/or RTL code for variables and functions, 5090 unless the type is an undefined structure or union. 5091 If not, it will get done when the type is completed. */ 5092 5093 if (VAR_OR_FUNCTION_DECL_P (decl)) 5094 { 5095 /* Determine the ELF visibility. */ 5096 if (TREE_PUBLIC (decl)) 5097 c_determine_visibility (decl); 5098 5099 /* This is a no-op in c-lang.c or something real in objc-act.c. */ 5100 if (c_dialect_objc ()) 5101 objc_check_decl (decl); 5102 5103 if (asmspec) 5104 { 5105 /* If this is not a static variable, issue a warning. 5106 It doesn't make any sense to give an ASMSPEC for an 5107 ordinary, non-register local variable. Historically, 5108 GCC has accepted -- but ignored -- the ASMSPEC in 5109 this case. */ 5110 if (!DECL_FILE_SCOPE_P (decl) 5111 && VAR_P (decl) 5112 && !C_DECL_REGISTER (decl) 5113 && !TREE_STATIC (decl)) 5114 warning (0, "ignoring asm-specifier for non-static local " 5115 "variable %q+D", decl); 5116 else 5117 set_user_assembler_name (decl, asmspec); 5118 } 5119 5120 if (DECL_FILE_SCOPE_P (decl)) 5121 { 5122 if (DECL_INITIAL (decl) == NULL_TREE 5123 || DECL_INITIAL (decl) == error_mark_node) 5124 /* Don't output anything 5125 when a tentative file-scope definition is seen. 5126 But at end of compilation, do output code for them. */ 5127 DECL_DEFER_OUTPUT (decl) = 1; 5128 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl)) 5129 DECL_HARD_REGISTER (decl) = 1; 5130 rest_of_decl_compilation (decl, true, 0); 5131 } 5132 else 5133 { 5134 /* In conjunction with an ASMSPEC, the `register' 5135 keyword indicates that we should place the variable 5136 in a particular register. */ 5137 if (asmspec && C_DECL_REGISTER (decl)) 5138 { 5139 DECL_HARD_REGISTER (decl) = 1; 5140 /* This cannot be done for a structure with volatile 5141 fields, on which DECL_REGISTER will have been 5142 reset. */ 5143 if (!DECL_REGISTER (decl)) 5144 error ("cannot put object with volatile field into register"); 5145 } 5146 5147 if (TREE_CODE (decl) != FUNCTION_DECL) 5148 { 5149 /* If we're building a variable sized type, and we might be 5150 reachable other than via the top of the current binding 5151 level, then create a new BIND_EXPR so that we deallocate 5152 the object at the right time. */ 5153 /* Note that DECL_SIZE can be null due to errors. */ 5154 if (DECL_SIZE (decl) 5155 && !TREE_CONSTANT (DECL_SIZE (decl)) 5156 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list)) 5157 { 5158 tree bind; 5159 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL); 5160 TREE_SIDE_EFFECTS (bind) = 1; 5161 add_stmt (bind); 5162 BIND_EXPR_BODY (bind) = push_stmt_list (); 5163 } 5164 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), 5165 DECL_EXPR, decl)); 5166 } 5167 } 5168 5169 5170 if (!DECL_FILE_SCOPE_P (decl)) 5171 { 5172 /* Recompute the RTL of a local array now 5173 if it used to be an incomplete type. */ 5174 if (was_incomplete && !is_global_var (decl)) 5175 { 5176 /* If we used it already as memory, it must stay in memory. */ 5177 TREE_ADDRESSABLE (decl) = TREE_USED (decl); 5178 /* If it's still incomplete now, no init will save it. */ 5179 if (DECL_SIZE (decl) == NULL_TREE) 5180 DECL_INITIAL (decl) = NULL_TREE; 5181 } 5182 } 5183 } 5184 5185 if (TREE_CODE (decl) == TYPE_DECL) 5186 { 5187 if (!DECL_FILE_SCOPE_P (decl) 5188 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE)) 5189 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl)); 5190 5191 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0); 5192 } 5193 5194 /* Install a cleanup (aka destructor) if one was given. */ 5195 if (VAR_P (decl) && !TREE_STATIC (decl)) 5196 { 5197 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl)); 5198 if (attr) 5199 { 5200 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr)); 5201 tree cleanup_decl = lookup_name (cleanup_id); 5202 tree cleanup; 5203 vec<tree, va_gc> *v; 5204 5205 /* Build "cleanup(&decl)" for the destructor. */ 5206 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false); 5207 vec_alloc (v, 1); 5208 v->quick_push (cleanup); 5209 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl), 5210 vNULL, cleanup_decl, v, NULL); 5211 vec_free (v); 5212 5213 /* Don't warn about decl unused; the cleanup uses it. */ 5214 TREE_USED (decl) = 1; 5215 TREE_USED (cleanup_decl) = 1; 5216 DECL_READ_P (decl) = 1; 5217 5218 push_cleanup (decl, cleanup, false); 5219 } 5220 } 5221 5222 if (warn_cxx_compat 5223 && VAR_P (decl) 5224 && !DECL_EXTERNAL (decl) 5225 && DECL_INITIAL (decl) == NULL_TREE) 5226 { 5227 type = strip_array_types (type); 5228 if (TREE_READONLY (decl)) 5229 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat, 5230 "uninitialized const %qD is invalid in C++", decl); 5231 else if (RECORD_OR_UNION_TYPE_P (type) 5232 && C_TYPE_FIELDS_READONLY (type)) 5233 diagnose_uninitialized_cst_member (decl, type); 5234 } 5235 5236 if (flag_openmp 5237 && VAR_P (decl) 5238 && lookup_attribute ("omp declare target implicit", 5239 DECL_ATTRIBUTES (decl))) 5240 { 5241 DECL_ATTRIBUTES (decl) 5242 = remove_attribute ("omp declare target implicit", 5243 DECL_ATTRIBUTES (decl)); 5244 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl))) 5245 error ("%q+D in declare target directive does not have mappable type", 5246 decl); 5247 else if (!lookup_attribute ("omp declare target", 5248 DECL_ATTRIBUTES (decl)) 5249 && !lookup_attribute ("omp declare target link", 5250 DECL_ATTRIBUTES (decl))) 5251 DECL_ATTRIBUTES (decl) 5252 = tree_cons (get_identifier ("omp declare target"), 5253 NULL_TREE, DECL_ATTRIBUTES (decl)); 5254 } 5255 5256 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl); 5257 } 5258 5259 /* Given a parsed parameter declaration, decode it into a PARM_DECL. 5260 EXPR is NULL or a pointer to an expression that needs to be 5261 evaluated for the side effects of array size expressions in the 5262 parameters. */ 5263 5264 tree 5265 grokparm (const struct c_parm *parm, tree *expr) 5266 { 5267 tree attrs = parm->attrs; 5268 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, 5269 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL); 5270 5271 decl_attributes (&decl, attrs, 0); 5272 5273 return decl; 5274 } 5275 5276 /* Given a parsed parameter declaration, decode it into a PARM_DECL 5277 and push that on the current scope. EXPR is a pointer to an 5278 expression that needs to be evaluated for the side effects of array 5279 size expressions in the parameters. */ 5280 5281 void 5282 push_parm_decl (const struct c_parm *parm, tree *expr) 5283 { 5284 tree attrs = parm->attrs; 5285 tree decl; 5286 5287 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL, 5288 &attrs, expr, NULL, DEPRECATED_NORMAL); 5289 if (decl && DECL_P (decl)) 5290 DECL_SOURCE_LOCATION (decl) = parm->loc; 5291 decl_attributes (&decl, attrs, 0); 5292 5293 decl = pushdecl (decl); 5294 5295 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE); 5296 } 5297 5298 /* Mark all the parameter declarations to date as forward decls. 5299 Also diagnose use of this extension. */ 5300 5301 void 5302 mark_forward_parm_decls (void) 5303 { 5304 struct c_binding *b; 5305 5306 if (pedantic && !current_scope->warned_forward_parm_decls) 5307 { 5308 pedwarn (input_location, OPT_Wpedantic, 5309 "ISO C forbids forward parameter declarations"); 5310 current_scope->warned_forward_parm_decls = true; 5311 } 5312 5313 for (b = current_scope->bindings; b; b = b->prev) 5314 if (TREE_CODE (b->decl) == PARM_DECL) 5315 TREE_ASM_WRITTEN (b->decl) = 1; 5316 } 5317 5318 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound 5319 literal, which may be an incomplete array type completed by the 5320 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound 5321 literal. NON_CONST is true if the initializers contain something 5322 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero, 5323 it is the (valid) alignment for this compound literal, as specified 5324 with _Alignas. */ 5325 5326 tree 5327 build_compound_literal (location_t loc, tree type, tree init, bool non_const, 5328 unsigned int alignas_align) 5329 { 5330 /* We do not use start_decl here because we have a type, not a declarator; 5331 and do not use finish_decl because the decl should be stored inside 5332 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */ 5333 tree decl; 5334 tree complit; 5335 tree stmt; 5336 5337 if (type == error_mark_node 5338 || init == error_mark_node) 5339 return error_mark_node; 5340 5341 decl = build_decl (loc, VAR_DECL, NULL_TREE, type); 5342 DECL_EXTERNAL (decl) = 0; 5343 TREE_PUBLIC (decl) = 0; 5344 TREE_STATIC (decl) = (current_scope == file_scope); 5345 DECL_CONTEXT (decl) = current_function_decl; 5346 TREE_USED (decl) = 1; 5347 DECL_READ_P (decl) = 1; 5348 DECL_ARTIFICIAL (decl) = 1; 5349 DECL_IGNORED_P (decl) = 1; 5350 TREE_TYPE (decl) = type; 5351 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl); 5352 if (alignas_align) 5353 { 5354 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT); 5355 DECL_USER_ALIGN (decl) = 1; 5356 } 5357 store_init_value (loc, decl, init, NULL_TREE); 5358 5359 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type)) 5360 { 5361 int failure = complete_array_type (&TREE_TYPE (decl), 5362 DECL_INITIAL (decl), true); 5363 /* If complete_array_type returns 3, it means that the 5364 initial value of the compound literal is empty. Allow it. */ 5365 gcc_assert (failure == 0 || failure == 3); 5366 5367 type = TREE_TYPE (decl); 5368 TREE_TYPE (DECL_INITIAL (decl)) = type; 5369 } 5370 5371 if (type == error_mark_node || !COMPLETE_TYPE_P (type)) 5372 { 5373 c_incomplete_type_error (loc, NULL_TREE, type); 5374 return error_mark_node; 5375 } 5376 5377 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl); 5378 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt); 5379 TREE_SIDE_EFFECTS (complit) = 1; 5380 5381 layout_decl (decl, 0); 5382 5383 if (TREE_STATIC (decl)) 5384 { 5385 /* This decl needs a name for the assembler output. */ 5386 set_compound_literal_name (decl); 5387 DECL_DEFER_OUTPUT (decl) = 1; 5388 DECL_COMDAT (decl) = 1; 5389 pushdecl (decl); 5390 rest_of_decl_compilation (decl, 1, 0); 5391 } 5392 5393 if (non_const) 5394 { 5395 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit); 5396 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1; 5397 } 5398 5399 return complit; 5400 } 5401 5402 /* Check the type of a compound literal. Here we just check that it 5403 is valid for C++. */ 5404 5405 void 5406 check_compound_literal_type (location_t loc, struct c_type_name *type_name) 5407 { 5408 if (warn_cxx_compat 5409 && (type_name->specs->typespec_kind == ctsk_tagdef 5410 || type_name->specs->typespec_kind == ctsk_tagfirstref)) 5411 warning_at (loc, OPT_Wc___compat, 5412 "defining a type in a compound literal is invalid in C++"); 5413 } 5414 5415 /* Determine whether TYPE is a structure with a flexible array member, 5416 or a union containing such a structure (possibly recursively). */ 5417 5418 static bool 5419 flexible_array_type_p (tree type) 5420 { 5421 tree x; 5422 switch (TREE_CODE (type)) 5423 { 5424 case RECORD_TYPE: 5425 x = TYPE_FIELDS (type); 5426 if (x == NULL_TREE) 5427 return false; 5428 while (DECL_CHAIN (x) != NULL_TREE) 5429 x = DECL_CHAIN (x); 5430 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 5431 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 5432 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 5433 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 5434 return true; 5435 return false; 5436 case UNION_TYPE: 5437 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x)) 5438 { 5439 if (flexible_array_type_p (TREE_TYPE (x))) 5440 return true; 5441 } 5442 return false; 5443 default: 5444 return false; 5445 } 5446 } 5447 5448 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME, 5449 replacing with appropriate values if they are invalid. */ 5450 5451 static void 5452 check_bitfield_type_and_width (location_t loc, tree *type, tree *width, 5453 tree orig_name) 5454 { 5455 tree type_mv; 5456 unsigned int max_width; 5457 unsigned HOST_WIDE_INT w; 5458 const char *name = (orig_name 5459 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name)) 5460 : _("<anonymous>")); 5461 5462 /* Detect and ignore out of range field width and process valid 5463 field widths. */ 5464 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))) 5465 { 5466 error_at (loc, "bit-field %qs width not an integer constant", name); 5467 *width = integer_one_node; 5468 } 5469 else 5470 { 5471 if (TREE_CODE (*width) != INTEGER_CST) 5472 { 5473 *width = c_fully_fold (*width, false, NULL); 5474 if (TREE_CODE (*width) == INTEGER_CST) 5475 pedwarn (loc, OPT_Wpedantic, 5476 "bit-field %qs width not an integer constant expression", 5477 name); 5478 } 5479 if (TREE_CODE (*width) != INTEGER_CST) 5480 { 5481 error_at (loc, "bit-field %qs width not an integer constant", name); 5482 *width = integer_one_node; 5483 } 5484 constant_expression_warning (*width); 5485 if (tree_int_cst_sgn (*width) < 0) 5486 { 5487 error_at (loc, "negative width in bit-field %qs", name); 5488 *width = integer_one_node; 5489 } 5490 else if (integer_zerop (*width) && orig_name) 5491 { 5492 error_at (loc, "zero width for bit-field %qs", name); 5493 *width = integer_one_node; 5494 } 5495 } 5496 5497 /* Detect invalid bit-field type. */ 5498 if (TREE_CODE (*type) != INTEGER_TYPE 5499 && TREE_CODE (*type) != BOOLEAN_TYPE 5500 && TREE_CODE (*type) != ENUMERAL_TYPE) 5501 { 5502 error_at (loc, "bit-field %qs has invalid type", name); 5503 *type = unsigned_type_node; 5504 } 5505 5506 if (TYPE_WARN_IF_NOT_ALIGN (*type)) 5507 { 5508 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type", 5509 name); 5510 *type = unsigned_type_node; 5511 } 5512 5513 type_mv = TYPE_MAIN_VARIANT (*type); 5514 if (!in_system_header_at (input_location) 5515 && type_mv != integer_type_node 5516 && type_mv != unsigned_type_node 5517 && type_mv != boolean_type_node) 5518 pedwarn_c90 (loc, OPT_Wpedantic, 5519 "type of bit-field %qs is a GCC extension", name); 5520 5521 max_width = TYPE_PRECISION (*type); 5522 5523 if (compare_tree_int (*width, max_width) > 0) 5524 { 5525 error_at (loc, "width of %qs exceeds its type", name); 5526 w = max_width; 5527 *width = build_int_cst (integer_type_node, w); 5528 } 5529 else 5530 w = tree_to_uhwi (*width); 5531 5532 if (TREE_CODE (*type) == ENUMERAL_TYPE) 5533 { 5534 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type); 5535 if (!lt 5536 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type)) 5537 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type))) 5538 warning_at (loc, 0, "%qs is narrower than values of its type", name); 5539 } 5540 } 5541 5542 5543 5544 /* Print warning about variable length array if necessary. */ 5545 5546 static void 5547 warn_variable_length_array (tree name, tree size) 5548 { 5549 if (TREE_CONSTANT (size)) 5550 { 5551 if (name) 5552 pedwarn_c90 (input_location, OPT_Wvla, 5553 "ISO C90 forbids array %qE whose size " 5554 "can%'t be evaluated", name); 5555 else 5556 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array " 5557 "whose size can%'t be evaluated"); 5558 } 5559 else 5560 { 5561 if (name) 5562 pedwarn_c90 (input_location, OPT_Wvla, 5563 "ISO C90 forbids variable length array %qE", name); 5564 else 5565 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable " 5566 "length array"); 5567 } 5568 } 5569 5570 /* Print warning about defaulting to int if necessary. */ 5571 5572 static void 5573 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...) 5574 { 5575 diagnostic_info diagnostic; 5576 va_list ap; 5577 rich_location richloc (line_table, location); 5578 5579 va_start (ap, gmsgid); 5580 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, 5581 flag_isoc99 ? DK_PEDWARN : DK_WARNING); 5582 diagnostic.option_index = opt; 5583 diagnostic_report_diagnostic (global_dc, &diagnostic); 5584 va_end (ap); 5585 } 5586 5587 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS, 5588 considering only those c_declspec_words found in LIST, which 5589 must be terminated by cdw_number_of_elements. */ 5590 5591 static location_t 5592 smallest_type_quals_location (const location_t *locations, 5593 const c_declspec_word *list) 5594 { 5595 location_t loc = UNKNOWN_LOCATION; 5596 while (*list != cdw_number_of_elements) 5597 { 5598 location_t newloc = locations[*list]; 5599 if (loc == UNKNOWN_LOCATION 5600 || (newloc != UNKNOWN_LOCATION && newloc < loc)) 5601 loc = newloc; 5602 list++; 5603 } 5604 5605 return loc; 5606 } 5607 5608 /* Given declspecs and a declarator, 5609 determine the name and type of the object declared 5610 and construct a ..._DECL node for it. 5611 (In one case we can return a ..._TYPE node instead. 5612 For invalid input we sometimes return NULL_TREE.) 5613 5614 DECLSPECS is a c_declspecs structure for the declaration specifiers. 5615 5616 DECL_CONTEXT says which syntactic context this declaration is in: 5617 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. 5618 FUNCDEF for a function definition. Like NORMAL but a few different 5619 error messages in each case. Return value may be zero meaning 5620 this definition is too screwy to try to parse. 5621 PARM for a parameter declaration (either within a function prototype 5622 or before a function body). Make a PARM_DECL, or return void_type_node. 5623 TYPENAME if for a typename (in a cast or sizeof). 5624 Don't make a DECL node; just return the ..._TYPE node. 5625 FIELD for a struct or union field; make a FIELD_DECL. 5626 INITIALIZED is true if the decl has an initializer. 5627 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node 5628 representing the width of the bit-field. 5629 DECL_ATTRS points to the list of attributes that should be added to this 5630 decl. Any nested attributes that belong on the decl itself will be 5631 added to this list. 5632 If EXPR is not NULL, any expressions that need to be evaluated as 5633 part of evaluating variably modified types will be stored in *EXPR. 5634 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be 5635 set to indicate whether operands in *EXPR can be used in constant 5636 expressions. 5637 DEPRECATED_STATE is a deprecated_states value indicating whether 5638 deprecation warnings should be suppressed. 5639 5640 In the TYPENAME case, DECLARATOR is really an absolute declarator. 5641 It may also be so in the PARM case, for a prototype where the 5642 argument type is specified but not the name. 5643 5644 This function is where the complicated C meanings of `static' 5645 and `extern' are interpreted. */ 5646 5647 static tree 5648 grokdeclarator (const struct c_declarator *declarator, 5649 struct c_declspecs *declspecs, 5650 enum decl_context decl_context, bool initialized, tree *width, 5651 tree *decl_attrs, tree *expr, bool *expr_const_operands, 5652 enum deprecated_states deprecated_state) 5653 { 5654 tree type = declspecs->type; 5655 bool threadp = declspecs->thread_p; 5656 enum c_storage_class storage_class = declspecs->storage_class; 5657 int constp; 5658 int restrictp; 5659 int volatilep; 5660 int atomicp; 5661 int type_quals = TYPE_UNQUALIFIED; 5662 tree name = NULL_TREE; 5663 bool funcdef_flag = false; 5664 bool funcdef_syntax = false; 5665 bool size_varies = false; 5666 tree decl_attr = declspecs->decl_attr; 5667 int array_ptr_quals = TYPE_UNQUALIFIED; 5668 tree array_ptr_attrs = NULL_TREE; 5669 bool array_parm_static = false; 5670 bool array_parm_vla_unspec_p = false; 5671 tree returned_attrs = NULL_TREE; 5672 bool bitfield = width != NULL; 5673 tree element_type; 5674 tree orig_qual_type = NULL; 5675 size_t orig_qual_indirect = 0; 5676 struct c_arg_info *arg_info = 0; 5677 addr_space_t as1, as2, address_space; 5678 location_t loc = UNKNOWN_LOCATION; 5679 tree expr_dummy; 5680 bool expr_const_operands_dummy; 5681 enum c_declarator_kind first_non_attr_kind; 5682 unsigned int alignas_align = 0; 5683 5684 if (TREE_CODE (type) == ERROR_MARK) 5685 return error_mark_node; 5686 if (expr == NULL) 5687 { 5688 expr = &expr_dummy; 5689 expr_dummy = NULL_TREE; 5690 } 5691 if (expr_const_operands == NULL) 5692 expr_const_operands = &expr_const_operands_dummy; 5693 5694 if (declspecs->expr) 5695 { 5696 if (*expr) 5697 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr, 5698 declspecs->expr); 5699 else 5700 *expr = declspecs->expr; 5701 } 5702 *expr_const_operands = declspecs->expr_const_operands; 5703 5704 if (decl_context == FUNCDEF) 5705 funcdef_flag = true, decl_context = NORMAL; 5706 5707 /* Look inside a declarator for the name being declared 5708 and get it as an IDENTIFIER_NODE, for an error message. */ 5709 { 5710 const struct c_declarator *decl = declarator; 5711 5712 first_non_attr_kind = cdk_attrs; 5713 while (decl) 5714 switch (decl->kind) 5715 { 5716 case cdk_array: 5717 loc = decl->id_loc; 5718 /* FALL THRU. */ 5719 5720 case cdk_function: 5721 case cdk_pointer: 5722 funcdef_syntax = (decl->kind == cdk_function); 5723 if (first_non_attr_kind == cdk_attrs) 5724 first_non_attr_kind = decl->kind; 5725 decl = decl->declarator; 5726 break; 5727 5728 case cdk_attrs: 5729 decl = decl->declarator; 5730 break; 5731 5732 case cdk_id: 5733 loc = decl->id_loc; 5734 if (decl->u.id) 5735 name = decl->u.id; 5736 if (first_non_attr_kind == cdk_attrs) 5737 first_non_attr_kind = decl->kind; 5738 decl = 0; 5739 break; 5740 5741 default: 5742 gcc_unreachable (); 5743 } 5744 if (name == NULL_TREE) 5745 { 5746 gcc_assert (decl_context == PARM 5747 || decl_context == TYPENAME 5748 || (decl_context == FIELD 5749 && declarator->kind == cdk_id)); 5750 gcc_assert (!initialized); 5751 } 5752 } 5753 5754 /* A function definition's declarator must have the form of 5755 a function declarator. */ 5756 5757 if (funcdef_flag && !funcdef_syntax) 5758 return NULL_TREE; 5759 5760 /* If this looks like a function definition, make it one, 5761 even if it occurs where parms are expected. 5762 Then store_parm_decls will reject it and not use it as a parm. */ 5763 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag) 5764 decl_context = PARM; 5765 5766 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS) 5767 warn_deprecated_use (declspecs->type, declspecs->decl_attr); 5768 5769 if ((decl_context == NORMAL || decl_context == FIELD) 5770 && current_scope == file_scope 5771 && variably_modified_type_p (type, NULL_TREE)) 5772 { 5773 if (name) 5774 error_at (loc, "variably modified %qE at file scope", name); 5775 else 5776 error_at (loc, "variably modified field at file scope"); 5777 type = integer_type_node; 5778 } 5779 5780 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0; 5781 5782 /* Diagnose defaulting to "int". */ 5783 5784 if (declspecs->default_int_p && !in_system_header_at (input_location)) 5785 { 5786 /* Issue a warning if this is an ISO C 99 program or if 5787 -Wreturn-type and this is a function, or if -Wimplicit; 5788 prefer the former warning since it is more explicit. */ 5789 if ((warn_implicit_int || warn_return_type || flag_isoc99) 5790 && funcdef_flag) 5791 warn_about_return_type = 1; 5792 else 5793 { 5794 if (name) 5795 warn_defaults_to (loc, OPT_Wimplicit_int, 5796 "type defaults to %<int%> in declaration " 5797 "of %qE", name); 5798 else 5799 warn_defaults_to (loc, OPT_Wimplicit_int, 5800 "type defaults to %<int%> in type name"); 5801 } 5802 } 5803 5804 /* Adjust the type if a bit-field is being declared, 5805 -funsigned-bitfields applied and the type is not explicitly 5806 "signed". */ 5807 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p 5808 && TREE_CODE (type) == INTEGER_TYPE) 5809 type = unsigned_type_for (type); 5810 5811 /* Figure out the type qualifiers for the declaration. There are 5812 two ways a declaration can become qualified. One is something 5813 like `const int i' where the `const' is explicit. Another is 5814 something like `typedef const int CI; CI i' where the type of the 5815 declaration contains the `const'. A third possibility is that 5816 there is a type qualifier on the element type of a typedefed 5817 array type, in which case we should extract that qualifier so 5818 that c_apply_type_quals_to_decl receives the full list of 5819 qualifiers to work with (C90 is not entirely clear about whether 5820 duplicate qualifiers should be diagnosed in this case, but it 5821 seems most appropriate to do so). */ 5822 element_type = strip_array_types (type); 5823 constp = declspecs->const_p + TYPE_READONLY (element_type); 5824 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type); 5825 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type); 5826 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type); 5827 as1 = declspecs->address_space; 5828 as2 = TYPE_ADDR_SPACE (element_type); 5829 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1; 5830 5831 if (constp > 1) 5832 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>"); 5833 if (restrictp > 1) 5834 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>"); 5835 if (volatilep > 1) 5836 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>"); 5837 if (atomicp > 1) 5838 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>"); 5839 5840 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2) 5841 error_at (loc, "conflicting named address spaces (%s vs %s)", 5842 c_addr_space_name (as1), c_addr_space_name (as2)); 5843 5844 if ((TREE_CODE (type) == ARRAY_TYPE 5845 || first_non_attr_kind == cdk_array) 5846 && TYPE_QUALS (element_type)) 5847 { 5848 orig_qual_type = type; 5849 type = TYPE_MAIN_VARIANT (type); 5850 } 5851 type_quals = ((constp ? TYPE_QUAL_CONST : 0) 5852 | (restrictp ? TYPE_QUAL_RESTRICT : 0) 5853 | (volatilep ? TYPE_QUAL_VOLATILE : 0) 5854 | (atomicp ? TYPE_QUAL_ATOMIC : 0) 5855 | ENCODE_QUAL_ADDR_SPACE (address_space)); 5856 if (type_quals != TYPE_QUALS (element_type)) 5857 orig_qual_type = NULL_TREE; 5858 5859 /* Applying the _Atomic qualifier to an array type (through the use 5860 of typedefs or typeof) must be detected here. If the qualifier 5861 is introduced later, any appearance of applying it to an array is 5862 actually applying it to an element of that array. */ 5863 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE) 5864 error_at (loc, "%<_Atomic%>-qualified array type"); 5865 5866 /* Warn about storage classes that are invalid for certain 5867 kinds of declarations (parameters, typenames, etc.). */ 5868 5869 if (funcdef_flag 5870 && (threadp 5871 || storage_class == csc_auto 5872 || storage_class == csc_register 5873 || storage_class == csc_typedef)) 5874 { 5875 if (storage_class == csc_auto) 5876 pedwarn (loc, 5877 (current_scope == file_scope) ? 0 : OPT_Wpedantic, 5878 "function definition declared %<auto%>"); 5879 if (storage_class == csc_register) 5880 error_at (loc, "function definition declared %<register%>"); 5881 if (storage_class == csc_typedef) 5882 error_at (loc, "function definition declared %<typedef%>"); 5883 if (threadp) 5884 error_at (loc, "function definition declared %qs", 5885 declspecs->thread_gnu_p ? "__thread" : "_Thread_local"); 5886 threadp = false; 5887 if (storage_class == csc_auto 5888 || storage_class == csc_register 5889 || storage_class == csc_typedef) 5890 storage_class = csc_none; 5891 } 5892 else if (decl_context != NORMAL && (storage_class != csc_none || threadp)) 5893 { 5894 if (decl_context == PARM && storage_class == csc_register) 5895 ; 5896 else 5897 { 5898 switch (decl_context) 5899 { 5900 case FIELD: 5901 if (name) 5902 error_at (loc, "storage class specified for structure " 5903 "field %qE", name); 5904 else 5905 error_at (loc, "storage class specified for structure field"); 5906 break; 5907 case PARM: 5908 if (name) 5909 error_at (loc, "storage class specified for parameter %qE", 5910 name); 5911 else 5912 error_at (loc, "storage class specified for unnamed parameter"); 5913 break; 5914 default: 5915 error_at (loc, "storage class specified for typename"); 5916 break; 5917 } 5918 storage_class = csc_none; 5919 threadp = false; 5920 } 5921 } 5922 else if (storage_class == csc_extern 5923 && initialized 5924 && !funcdef_flag) 5925 { 5926 /* 'extern' with initialization is invalid if not at file scope. */ 5927 if (current_scope == file_scope) 5928 { 5929 /* It is fine to have 'extern const' when compiling at C 5930 and C++ intersection. */ 5931 if (!(warn_cxx_compat && constp)) 5932 warning_at (loc, 0, "%qE initialized and declared %<extern%>", 5933 name); 5934 } 5935 else 5936 error_at (loc, "%qE has both %<extern%> and initializer", name); 5937 } 5938 else if (current_scope == file_scope) 5939 { 5940 if (storage_class == csc_auto) 5941 error_at (loc, "file-scope declaration of %qE specifies %<auto%>", 5942 name); 5943 if (pedantic && storage_class == csc_register) 5944 pedwarn (input_location, OPT_Wpedantic, 5945 "file-scope declaration of %qE specifies %<register%>", name); 5946 } 5947 else 5948 { 5949 if (storage_class == csc_extern && funcdef_flag) 5950 error_at (loc, "nested function %qE declared %<extern%>", name); 5951 else if (threadp && storage_class == csc_none) 5952 { 5953 error_at (loc, "function-scope %qE implicitly auto and declared " 5954 "%qs", name, 5955 declspecs->thread_gnu_p ? "__thread" : "_Thread_local"); 5956 threadp = false; 5957 } 5958 } 5959 5960 /* Now figure out the structure of the declarator proper. 5961 Descend through it, creating more complex types, until we reach 5962 the declared identifier (or NULL_TREE, in an absolute declarator). 5963 At each stage we maintain an unqualified version of the type 5964 together with any qualifiers that should be applied to it with 5965 c_build_qualified_type; this way, array types including 5966 multidimensional array types are first built up in unqualified 5967 form and then the qualified form is created with 5968 TYPE_MAIN_VARIANT pointing to the unqualified form. */ 5969 5970 while (declarator && declarator->kind != cdk_id) 5971 { 5972 if (type == error_mark_node) 5973 { 5974 declarator = declarator->declarator; 5975 continue; 5976 } 5977 5978 /* Each level of DECLARATOR is either a cdk_array (for ...[..]), 5979 a cdk_pointer (for *...), 5980 a cdk_function (for ...(...)), 5981 a cdk_attrs (for nested attributes), 5982 or a cdk_id (for the name being declared 5983 or the place in an absolute declarator 5984 where the name was omitted). 5985 For the last case, we have just exited the loop. 5986 5987 At this point, TYPE is the type of elements of an array, 5988 or for a function to return, or for a pointer to point to. 5989 After this sequence of ifs, TYPE is the type of the 5990 array or function or pointer, and DECLARATOR has had its 5991 outermost layer removed. */ 5992 5993 if (array_ptr_quals != TYPE_UNQUALIFIED 5994 || array_ptr_attrs != NULL_TREE 5995 || array_parm_static) 5996 { 5997 /* Only the innermost declarator (making a parameter be of 5998 array type which is converted to pointer type) 5999 may have static or type qualifiers. */ 6000 error_at (loc, "static or type qualifiers in non-parameter array declarator"); 6001 array_ptr_quals = TYPE_UNQUALIFIED; 6002 array_ptr_attrs = NULL_TREE; 6003 array_parm_static = false; 6004 } 6005 6006 switch (declarator->kind) 6007 { 6008 case cdk_attrs: 6009 { 6010 /* A declarator with embedded attributes. */ 6011 tree attrs = declarator->u.attrs; 6012 const struct c_declarator *inner_decl; 6013 int attr_flags = 0; 6014 declarator = declarator->declarator; 6015 inner_decl = declarator; 6016 while (inner_decl->kind == cdk_attrs) 6017 inner_decl = inner_decl->declarator; 6018 if (inner_decl->kind == cdk_id) 6019 attr_flags |= (int) ATTR_FLAG_DECL_NEXT; 6020 else if (inner_decl->kind == cdk_function) 6021 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; 6022 else if (inner_decl->kind == cdk_array) 6023 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; 6024 returned_attrs = decl_attributes (&type, 6025 chainon (returned_attrs, attrs), 6026 attr_flags); 6027 break; 6028 } 6029 case cdk_array: 6030 { 6031 tree itype = NULL_TREE; 6032 tree size = declarator->u.array.dimen; 6033 /* The index is a signed object `sizetype' bits wide. */ 6034 tree index_type = c_common_signed_type (sizetype); 6035 6036 array_ptr_quals = declarator->u.array.quals; 6037 array_ptr_attrs = declarator->u.array.attrs; 6038 array_parm_static = declarator->u.array.static_p; 6039 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p; 6040 6041 declarator = declarator->declarator; 6042 6043 /* Check for some types that there cannot be arrays of. */ 6044 6045 if (VOID_TYPE_P (type)) 6046 { 6047 if (name) 6048 error_at (loc, "declaration of %qE as array of voids", name); 6049 else 6050 error_at (loc, "declaration of type name as array of voids"); 6051 type = error_mark_node; 6052 } 6053 6054 if (TREE_CODE (type) == FUNCTION_TYPE) 6055 { 6056 if (name) 6057 error_at (loc, "declaration of %qE as array of functions", 6058 name); 6059 else 6060 error_at (loc, "declaration of type name as array of " 6061 "functions"); 6062 type = error_mark_node; 6063 } 6064 6065 if (pedantic && !in_system_header_at (input_location) 6066 && flexible_array_type_p (type)) 6067 pedwarn (loc, OPT_Wpedantic, 6068 "invalid use of structure with flexible array member"); 6069 6070 if (size == error_mark_node) 6071 type = error_mark_node; 6072 6073 if (type == error_mark_node) 6074 continue; 6075 6076 /* If size was specified, set ITYPE to a range-type for 6077 that size. Otherwise, ITYPE remains null. finish_decl 6078 may figure it out from an initial value. */ 6079 6080 if (size) 6081 { 6082 bool size_maybe_const = true; 6083 bool size_int_const = (TREE_CODE (size) == INTEGER_CST 6084 && !TREE_OVERFLOW (size)); 6085 bool this_size_varies = false; 6086 6087 /* Strip NON_LVALUE_EXPRs since we aren't using as an 6088 lvalue. */ 6089 STRIP_TYPE_NOPS (size); 6090 6091 if (!INTEGRAL_TYPE_P (TREE_TYPE (size))) 6092 { 6093 if (name) 6094 error_at (loc, "size of array %qE has non-integer type", 6095 name); 6096 else 6097 error_at (loc, 6098 "size of unnamed array has non-integer type"); 6099 size = integer_one_node; 6100 } 6101 /* This can happen with enum forward declaration. */ 6102 else if (!COMPLETE_TYPE_P (TREE_TYPE (size))) 6103 { 6104 if (name) 6105 error_at (loc, "size of array %qE has incomplete type", 6106 name); 6107 else 6108 error_at (loc, "size of unnamed array has incomplete " 6109 "type"); 6110 size = integer_one_node; 6111 } 6112 6113 size = c_fully_fold (size, false, &size_maybe_const); 6114 6115 if (pedantic && size_maybe_const && integer_zerop (size)) 6116 { 6117 if (name) 6118 pedwarn (loc, OPT_Wpedantic, 6119 "ISO C forbids zero-size array %qE", name); 6120 else 6121 pedwarn (loc, OPT_Wpedantic, 6122 "ISO C forbids zero-size array"); 6123 } 6124 6125 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const) 6126 { 6127 constant_expression_warning (size); 6128 if (tree_int_cst_sgn (size) < 0) 6129 { 6130 if (name) 6131 error_at (loc, "size of array %qE is negative", name); 6132 else 6133 error_at (loc, "size of unnamed array is negative"); 6134 size = integer_one_node; 6135 } 6136 /* Handle a size folded to an integer constant but 6137 not an integer constant expression. */ 6138 if (!size_int_const) 6139 { 6140 /* If this is a file scope declaration of an 6141 ordinary identifier, this is invalid code; 6142 diagnosing it here and not subsequently 6143 treating the type as variable-length avoids 6144 more confusing diagnostics later. */ 6145 if ((decl_context == NORMAL || decl_context == FIELD) 6146 && current_scope == file_scope) 6147 pedwarn (input_location, 0, 6148 "variably modified %qE at file scope", 6149 name); 6150 else 6151 this_size_varies = size_varies = true; 6152 warn_variable_length_array (name, size); 6153 } 6154 } 6155 else if ((decl_context == NORMAL || decl_context == FIELD) 6156 && current_scope == file_scope) 6157 { 6158 error_at (loc, "variably modified %qE at file scope", name); 6159 size = integer_one_node; 6160 } 6161 else 6162 { 6163 /* Make sure the array size remains visibly 6164 nonconstant even if it is (eg) a const variable 6165 with known value. */ 6166 this_size_varies = size_varies = true; 6167 warn_variable_length_array (name, size); 6168 if (sanitize_flags_p (SANITIZE_VLA) 6169 && current_function_decl != NULL_TREE 6170 && decl_context == NORMAL) 6171 { 6172 /* Evaluate the array size only once. */ 6173 size = save_expr (size); 6174 size = c_fully_fold (size, false, NULL); 6175 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size), 6176 ubsan_instrument_vla (loc, size), 6177 size); 6178 } 6179 } 6180 6181 if (integer_zerop (size) && !this_size_varies) 6182 { 6183 /* A zero-length array cannot be represented with 6184 an unsigned index type, which is what we'll 6185 get with build_index_type. Create an 6186 open-ended range instead. */ 6187 itype = build_range_type (sizetype, size, NULL_TREE); 6188 } 6189 else 6190 { 6191 /* Arrange for the SAVE_EXPR on the inside of the 6192 MINUS_EXPR, which allows the -1 to get folded 6193 with the +1 that happens when building TYPE_SIZE. */ 6194 if (size_varies) 6195 size = save_expr (size); 6196 if (this_size_varies && TREE_CODE (size) == INTEGER_CST) 6197 size = build2 (COMPOUND_EXPR, TREE_TYPE (size), 6198 integer_zero_node, size); 6199 6200 /* Compute the maximum valid index, that is, size 6201 - 1. Do the calculation in index_type, so that 6202 if it is a variable the computations will be 6203 done in the proper mode. */ 6204 itype = fold_build2_loc (loc, MINUS_EXPR, index_type, 6205 convert (index_type, size), 6206 convert (index_type, 6207 size_one_node)); 6208 6209 /* The above overflows when size does not fit 6210 in index_type. 6211 ??? While a size of INT_MAX+1 technically shouldn't 6212 cause an overflow (because we subtract 1), handling 6213 this case seems like an unnecessary complication. */ 6214 if (TREE_CODE (size) == INTEGER_CST 6215 && !int_fits_type_p (size, index_type)) 6216 { 6217 if (name) 6218 error_at (loc, "size of array %qE is too large", 6219 name); 6220 else 6221 error_at (loc, "size of unnamed array is too large"); 6222 type = error_mark_node; 6223 continue; 6224 } 6225 6226 itype = build_index_type (itype); 6227 } 6228 if (this_size_varies) 6229 { 6230 if (TREE_SIDE_EFFECTS (size)) 6231 { 6232 if (*expr) 6233 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size), 6234 *expr, size); 6235 else 6236 *expr = size; 6237 } 6238 *expr_const_operands &= size_maybe_const; 6239 } 6240 } 6241 else if (decl_context == FIELD) 6242 { 6243 bool flexible_array_member = false; 6244 if (array_parm_vla_unspec_p) 6245 /* Field names can in fact have function prototype 6246 scope so [*] is disallowed here through making 6247 the field variably modified, not through being 6248 something other than a declaration with function 6249 prototype scope. */ 6250 size_varies = true; 6251 else 6252 { 6253 const struct c_declarator *t = declarator; 6254 while (t->kind == cdk_attrs) 6255 t = t->declarator; 6256 flexible_array_member = (t->kind == cdk_id); 6257 } 6258 if (flexible_array_member 6259 && !in_system_header_at (input_location)) 6260 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not " 6261 "support flexible array members"); 6262 6263 /* ISO C99 Flexible array members are effectively 6264 identical to GCC's zero-length array extension. */ 6265 if (flexible_array_member || array_parm_vla_unspec_p) 6266 itype = build_range_type (sizetype, size_zero_node, 6267 NULL_TREE); 6268 } 6269 else if (decl_context == PARM) 6270 { 6271 if (array_parm_vla_unspec_p) 6272 { 6273 itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 6274 size_varies = true; 6275 } 6276 } 6277 else if (decl_context == TYPENAME) 6278 { 6279 if (array_parm_vla_unspec_p) 6280 { 6281 /* C99 6.7.5.2p4 */ 6282 warning (0, "%<[*]%> not in a declaration"); 6283 /* We use this to avoid messing up with incomplete 6284 array types of the same type, that would 6285 otherwise be modified below. */ 6286 itype = build_range_type (sizetype, size_zero_node, 6287 NULL_TREE); 6288 size_varies = true; 6289 } 6290 } 6291 6292 /* Complain about arrays of incomplete types. */ 6293 if (!COMPLETE_TYPE_P (type)) 6294 { 6295 error_at (loc, "array type has incomplete element type %qT", 6296 type); 6297 /* See if we can be more helpful. */ 6298 if (TREE_CODE (type) == ARRAY_TYPE) 6299 { 6300 if (name) 6301 inform (loc, "declaration of %qE as multidimensional " 6302 "array must have bounds for all dimensions " 6303 "except the first", name); 6304 else 6305 inform (loc, "declaration of multidimensional array " 6306 "must have bounds for all dimensions except " 6307 "the first"); 6308 } 6309 type = error_mark_node; 6310 } 6311 else 6312 /* When itype is NULL, a shared incomplete array type is 6313 returned for all array of a given type. Elsewhere we 6314 make sure we don't complete that type before copying 6315 it, but here we want to make sure we don't ever 6316 modify the shared type, so we gcc_assert (itype) 6317 below. */ 6318 { 6319 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals); 6320 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type)) 6321 type = build_qualified_type (type, 6322 ENCODE_QUAL_ADDR_SPACE (as)); 6323 6324 type = build_array_type (type, itype); 6325 } 6326 6327 if (type != error_mark_node) 6328 { 6329 if (size_varies) 6330 { 6331 /* It is ok to modify type here even if itype is 6332 NULL: if size_varies, we're in a 6333 multi-dimensional array and the inner type has 6334 variable size, so the enclosing shared array type 6335 must too. */ 6336 if (size && TREE_CODE (size) == INTEGER_CST) 6337 type 6338 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); 6339 C_TYPE_VARIABLE_SIZE (type) = 1; 6340 } 6341 6342 /* The GCC extension for zero-length arrays differs from 6343 ISO flexible array members in that sizeof yields 6344 zero. */ 6345 if (size && integer_zerop (size)) 6346 { 6347 gcc_assert (itype); 6348 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); 6349 TYPE_SIZE (type) = bitsize_zero_node; 6350 TYPE_SIZE_UNIT (type) = size_zero_node; 6351 SET_TYPE_STRUCTURAL_EQUALITY (type); 6352 } 6353 if (array_parm_vla_unspec_p) 6354 { 6355 gcc_assert (itype); 6356 /* The type is complete. C99 6.7.5.2p4 */ 6357 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); 6358 TYPE_SIZE (type) = bitsize_zero_node; 6359 TYPE_SIZE_UNIT (type) = size_zero_node; 6360 SET_TYPE_STRUCTURAL_EQUALITY (type); 6361 } 6362 6363 if (!valid_array_size_p (loc, type, name)) 6364 type = error_mark_node; 6365 } 6366 6367 if (decl_context != PARM 6368 && (array_ptr_quals != TYPE_UNQUALIFIED 6369 || array_ptr_attrs != NULL_TREE 6370 || array_parm_static)) 6371 { 6372 error_at (loc, "static or type qualifiers in non-parameter " 6373 "array declarator"); 6374 array_ptr_quals = TYPE_UNQUALIFIED; 6375 array_ptr_attrs = NULL_TREE; 6376 array_parm_static = false; 6377 } 6378 orig_qual_indirect++; 6379 break; 6380 } 6381 case cdk_function: 6382 { 6383 /* Say it's a definition only for the declarator closest 6384 to the identifier, apart possibly from some 6385 attributes. */ 6386 bool really_funcdef = false; 6387 tree arg_types; 6388 orig_qual_type = NULL_TREE; 6389 if (funcdef_flag) 6390 { 6391 const struct c_declarator *t = declarator->declarator; 6392 while (t->kind == cdk_attrs) 6393 t = t->declarator; 6394 really_funcdef = (t->kind == cdk_id); 6395 } 6396 6397 /* Declaring a function type. Make sure we have a valid 6398 type for the function to return. */ 6399 if (type == error_mark_node) 6400 continue; 6401 6402 size_varies = false; 6403 6404 /* Warn about some types functions can't return. */ 6405 if (TREE_CODE (type) == FUNCTION_TYPE) 6406 { 6407 if (name) 6408 error_at (loc, "%qE declared as function returning a " 6409 "function", name); 6410 else 6411 error_at (loc, "type name declared as function " 6412 "returning a function"); 6413 type = integer_type_node; 6414 } 6415 if (TREE_CODE (type) == ARRAY_TYPE) 6416 { 6417 if (name) 6418 error_at (loc, "%qE declared as function returning an array", 6419 name); 6420 else 6421 error_at (loc, "type name declared as function returning " 6422 "an array"); 6423 type = integer_type_node; 6424 } 6425 6426 /* Construct the function type and go to the next 6427 inner layer of declarator. */ 6428 arg_info = declarator->u.arg_info; 6429 arg_types = grokparms (arg_info, really_funcdef); 6430 6431 /* Type qualifiers before the return type of the function 6432 qualify the return type, not the function type. */ 6433 if (type_quals) 6434 { 6435 const enum c_declspec_word ignored_quals_list[] = 6436 { 6437 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space, 6438 cdw_atomic, cdw_number_of_elements 6439 }; 6440 location_t specs_loc 6441 = smallest_type_quals_location (declspecs->locations, 6442 ignored_quals_list); 6443 if (specs_loc == UNKNOWN_LOCATION) 6444 specs_loc = declspecs->locations[cdw_typedef]; 6445 if (specs_loc == UNKNOWN_LOCATION) 6446 specs_loc = loc; 6447 6448 /* Type qualifiers on a function return type are 6449 normally permitted by the standard but have no 6450 effect, so give a warning at -Wreturn-type. 6451 Qualifiers on a void return type are banned on 6452 function definitions in ISO C; GCC used to used 6453 them for noreturn functions. The resolution of C11 6454 DR#423 means qualifiers (other than _Atomic) are 6455 actually removed from the return type when 6456 determining the function type. */ 6457 int quals_used = type_quals; 6458 if (flag_isoc11) 6459 quals_used &= TYPE_QUAL_ATOMIC; 6460 if (quals_used && VOID_TYPE_P (type) && really_funcdef) 6461 pedwarn (specs_loc, 0, 6462 "function definition has qualified void " 6463 "return type"); 6464 else 6465 warning_at (specs_loc, OPT_Wignored_qualifiers, 6466 "type qualifiers ignored on function " 6467 "return type"); 6468 6469 /* Ensure an error for restrict on invalid types; the 6470 DR#423 resolution is not entirely clear about 6471 this. */ 6472 if (flag_isoc11 6473 && (type_quals & TYPE_QUAL_RESTRICT) 6474 && (!POINTER_TYPE_P (type) 6475 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) 6476 error_at (loc, "invalid use of %<restrict%>"); 6477 type = c_build_qualified_type (type, quals_used); 6478 } 6479 type_quals = TYPE_UNQUALIFIED; 6480 6481 type = build_function_type (type, arg_types); 6482 declarator = declarator->declarator; 6483 6484 /* Set the TYPE_CONTEXTs for each tagged type which is local to 6485 the formal parameter list of this FUNCTION_TYPE to point to 6486 the FUNCTION_TYPE node itself. */ 6487 { 6488 c_arg_tag *tag; 6489 unsigned ix; 6490 6491 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag) 6492 TYPE_CONTEXT (tag->type) = type; 6493 } 6494 break; 6495 } 6496 case cdk_pointer: 6497 { 6498 /* Merge any constancy or volatility into the target type 6499 for the pointer. */ 6500 if ((type_quals & TYPE_QUAL_ATOMIC) 6501 && TREE_CODE (type) == FUNCTION_TYPE) 6502 { 6503 error_at (loc, 6504 "%<_Atomic%>-qualified function type"); 6505 type_quals &= ~TYPE_QUAL_ATOMIC; 6506 } 6507 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 6508 && type_quals) 6509 pedwarn (loc, OPT_Wpedantic, 6510 "ISO C forbids qualified function types"); 6511 if (type_quals) 6512 type = c_build_qualified_type (type, type_quals, orig_qual_type, 6513 orig_qual_indirect); 6514 orig_qual_type = NULL_TREE; 6515 size_varies = false; 6516 6517 /* When the pointed-to type involves components of variable size, 6518 care must be taken to ensure that the size evaluation code is 6519 emitted early enough to dominate all the possible later uses 6520 and late enough for the variables on which it depends to have 6521 been assigned. 6522 6523 This is expected to happen automatically when the pointed-to 6524 type has a name/declaration of it's own, but special attention 6525 is required if the type is anonymous. 6526 6527 We attach an artificial TYPE_DECL to such pointed-to type 6528 and arrange for it to be included in a DECL_EXPR. This 6529 forces the sizes evaluation at a safe point and ensures it 6530 is not deferred until e.g. within a deeper conditional context. 6531 6532 PARM contexts have no enclosing statement list that 6533 can hold the DECL_EXPR, so we need to use a BIND_EXPR 6534 instead, and add it to the list of expressions that 6535 need to be evaluated. 6536 6537 TYPENAME contexts do have an enclosing statement list, 6538 but it would be incorrect to use it, as the size should 6539 only be evaluated if the containing expression is 6540 evaluated. We might also be in the middle of an 6541 expression with side effects on the pointed-to type size 6542 "arguments" prior to the pointer declaration point and 6543 the fake TYPE_DECL in the enclosing context would force 6544 the size evaluation prior to the side effects. We therefore 6545 use BIND_EXPRs in TYPENAME contexts too. */ 6546 if (!TYPE_NAME (type) 6547 && variably_modified_type_p (type, NULL_TREE)) 6548 { 6549 tree bind = NULL_TREE; 6550 if (decl_context == TYPENAME || decl_context == PARM) 6551 { 6552 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, 6553 NULL_TREE, NULL_TREE); 6554 TREE_SIDE_EFFECTS (bind) = 1; 6555 BIND_EXPR_BODY (bind) = push_stmt_list (); 6556 push_scope (); 6557 } 6558 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type); 6559 DECL_ARTIFICIAL (decl) = 1; 6560 pushdecl (decl); 6561 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE); 6562 TYPE_NAME (type) = decl; 6563 if (bind) 6564 { 6565 pop_scope (); 6566 BIND_EXPR_BODY (bind) 6567 = pop_stmt_list (BIND_EXPR_BODY (bind)); 6568 if (*expr) 6569 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, 6570 bind); 6571 else 6572 *expr = bind; 6573 } 6574 } 6575 6576 type = c_build_pointer_type (type); 6577 6578 /* Process type qualifiers (such as const or volatile) 6579 that were given inside the `*'. */ 6580 type_quals = declarator->u.pointer_quals; 6581 6582 declarator = declarator->declarator; 6583 break; 6584 } 6585 default: 6586 gcc_unreachable (); 6587 } 6588 } 6589 *decl_attrs = chainon (returned_attrs, *decl_attrs); 6590 6591 /* Now TYPE has the actual type, apart from any qualifiers in 6592 TYPE_QUALS. */ 6593 6594 /* Warn about address space used for things other than static memory or 6595 pointers. */ 6596 address_space = DECODE_QUAL_ADDR_SPACE (type_quals); 6597 if (!ADDR_SPACE_GENERIC_P (address_space)) 6598 { 6599 if (decl_context == NORMAL) 6600 { 6601 switch (storage_class) 6602 { 6603 case csc_auto: 6604 error ("%qs combined with %<auto%> qualifier for %qE", 6605 c_addr_space_name (address_space), name); 6606 break; 6607 case csc_register: 6608 error ("%qs combined with %<register%> qualifier for %qE", 6609 c_addr_space_name (address_space), name); 6610 break; 6611 case csc_none: 6612 if (current_function_scope) 6613 { 6614 error ("%qs specified for auto variable %qE", 6615 c_addr_space_name (address_space), name); 6616 break; 6617 } 6618 break; 6619 case csc_static: 6620 case csc_extern: 6621 case csc_typedef: 6622 break; 6623 default: 6624 gcc_unreachable (); 6625 } 6626 } 6627 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE) 6628 { 6629 if (name) 6630 error ("%qs specified for parameter %qE", 6631 c_addr_space_name (address_space), name); 6632 else 6633 error ("%qs specified for unnamed parameter", 6634 c_addr_space_name (address_space)); 6635 } 6636 else if (decl_context == FIELD) 6637 { 6638 if (name) 6639 error ("%qs specified for structure field %qE", 6640 c_addr_space_name (address_space), name); 6641 else 6642 error ("%qs specified for structure field", 6643 c_addr_space_name (address_space)); 6644 } 6645 } 6646 6647 /* Check the type and width of a bit-field. */ 6648 if (bitfield) 6649 { 6650 check_bitfield_type_and_width (loc, &type, width, name); 6651 /* C11 makes it implementation-defined (6.7.2.1#5) whether 6652 atomic types are permitted for bit-fields; we have no code to 6653 make bit-field accesses atomic, so disallow them. */ 6654 if (type_quals & TYPE_QUAL_ATOMIC) 6655 { 6656 if (name) 6657 error_at (loc, "bit-field %qE has atomic type", name); 6658 else 6659 error_at (loc, "bit-field has atomic type"); 6660 type_quals &= ~TYPE_QUAL_ATOMIC; 6661 } 6662 } 6663 6664 /* Reject invalid uses of _Alignas. */ 6665 if (declspecs->alignas_p) 6666 { 6667 if (storage_class == csc_typedef) 6668 error_at (loc, "alignment specified for typedef %qE", name); 6669 else if (storage_class == csc_register) 6670 error_at (loc, "alignment specified for %<register%> object %qE", 6671 name); 6672 else if (decl_context == PARM) 6673 { 6674 if (name) 6675 error_at (loc, "alignment specified for parameter %qE", name); 6676 else 6677 error_at (loc, "alignment specified for unnamed parameter"); 6678 } 6679 else if (bitfield) 6680 { 6681 if (name) 6682 error_at (loc, "alignment specified for bit-field %qE", name); 6683 else 6684 error_at (loc, "alignment specified for unnamed bit-field"); 6685 } 6686 else if (TREE_CODE (type) == FUNCTION_TYPE) 6687 error_at (loc, "alignment specified for function %qE", name); 6688 else if (declspecs->align_log != -1 && TYPE_P (type)) 6689 { 6690 alignas_align = 1U << declspecs->align_log; 6691 if (alignas_align < min_align_of_type (type)) 6692 { 6693 if (name) 6694 error_at (loc, "%<_Alignas%> specifiers cannot reduce " 6695 "alignment of %qE", name); 6696 else 6697 error_at (loc, "%<_Alignas%> specifiers cannot reduce " 6698 "alignment of unnamed field"); 6699 alignas_align = 0; 6700 } 6701 } 6702 } 6703 6704 /* If this is declaring a typedef name, return a TYPE_DECL. */ 6705 6706 if (storage_class == csc_typedef) 6707 { 6708 tree decl; 6709 if ((type_quals & TYPE_QUAL_ATOMIC) 6710 && TREE_CODE (type) == FUNCTION_TYPE) 6711 { 6712 error_at (loc, 6713 "%<_Atomic%>-qualified function type"); 6714 type_quals &= ~TYPE_QUAL_ATOMIC; 6715 } 6716 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 6717 && type_quals) 6718 pedwarn (loc, OPT_Wpedantic, 6719 "ISO C forbids qualified function types"); 6720 if (type_quals) 6721 type = c_build_qualified_type (type, type_quals, orig_qual_type, 6722 orig_qual_indirect); 6723 decl = build_decl (declarator->id_loc, 6724 TYPE_DECL, declarator->u.id, type); 6725 if (declspecs->explicit_signed_p) 6726 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 6727 if (declspecs->inline_p) 6728 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl); 6729 if (declspecs->noreturn_p) 6730 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl); 6731 6732 if (warn_cxx_compat && declarator->u.id != NULL_TREE) 6733 { 6734 struct c_binding *b = I_TAG_BINDING (declarator->u.id); 6735 6736 if (b != NULL 6737 && b->decl != NULL_TREE 6738 && (B_IN_CURRENT_SCOPE (b) 6739 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b))) 6740 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type)) 6741 { 6742 if (warning_at (declarator->id_loc, OPT_Wc___compat, 6743 ("using %qD as both a typedef and a tag is " 6744 "invalid in C++"), decl) 6745 && b->locus != UNKNOWN_LOCATION) 6746 inform (b->locus, "originally defined here"); 6747 } 6748 } 6749 6750 return decl; 6751 } 6752 6753 /* If this is a type name (such as, in a cast or sizeof), 6754 compute the type and return it now. */ 6755 6756 if (decl_context == TYPENAME) 6757 { 6758 /* Note that the grammar rejects storage classes in typenames 6759 and fields. */ 6760 gcc_assert (storage_class == csc_none && !threadp 6761 && !declspecs->inline_p && !declspecs->noreturn_p); 6762 if ((type_quals & TYPE_QUAL_ATOMIC) 6763 && TREE_CODE (type) == FUNCTION_TYPE) 6764 { 6765 error_at (loc, 6766 "%<_Atomic%>-qualified function type"); 6767 type_quals &= ~TYPE_QUAL_ATOMIC; 6768 } 6769 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 6770 && type_quals) 6771 pedwarn (loc, OPT_Wpedantic, 6772 "ISO C forbids const or volatile function types"); 6773 if (type_quals) 6774 type = c_build_qualified_type (type, type_quals, orig_qual_type, 6775 orig_qual_indirect); 6776 return type; 6777 } 6778 6779 if (pedantic && decl_context == FIELD 6780 && variably_modified_type_p (type, NULL_TREE)) 6781 { 6782 /* C99 6.7.2.1p8 */ 6783 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot " 6784 "have a variably modified type"); 6785 } 6786 6787 /* Aside from typedefs and type names (handle above), 6788 `void' at top level (not within pointer) 6789 is allowed only in public variables. 6790 We don't complain about parms either, but that is because 6791 a better error message can be made later. */ 6792 6793 if (VOID_TYPE_P (type) && decl_context != PARM 6794 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE) 6795 && (storage_class == csc_extern 6796 || (current_scope == file_scope 6797 && !(storage_class == csc_static 6798 || storage_class == csc_register))))) 6799 { 6800 error_at (loc, "variable or field %qE declared void", name); 6801 type = integer_type_node; 6802 } 6803 6804 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL 6805 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ 6806 6807 { 6808 tree decl; 6809 6810 if (decl_context == PARM) 6811 { 6812 tree promoted_type; 6813 bool array_parameter_p = false; 6814 6815 /* A parameter declared as an array of T is really a pointer to T. 6816 One declared as a function is really a pointer to a function. */ 6817 6818 if (TREE_CODE (type) == ARRAY_TYPE) 6819 { 6820 /* Transfer const-ness of array into that of type pointed to. */ 6821 type = TREE_TYPE (type); 6822 if (orig_qual_type != NULL_TREE) 6823 { 6824 if (orig_qual_indirect == 0) 6825 orig_qual_type = TREE_TYPE (orig_qual_type); 6826 else 6827 orig_qual_indirect--; 6828 } 6829 if (type_quals) 6830 type = c_build_qualified_type (type, type_quals, orig_qual_type, 6831 orig_qual_indirect); 6832 type = c_build_pointer_type (type); 6833 type_quals = array_ptr_quals; 6834 if (type_quals) 6835 type = c_build_qualified_type (type, type_quals); 6836 6837 /* We don't yet implement attributes in this context. */ 6838 if (array_ptr_attrs != NULL_TREE) 6839 warning_at (loc, OPT_Wattributes, 6840 "attributes in parameter array declarator ignored"); 6841 6842 size_varies = false; 6843 array_parameter_p = true; 6844 } 6845 else if (TREE_CODE (type) == FUNCTION_TYPE) 6846 { 6847 if (type_quals & TYPE_QUAL_ATOMIC) 6848 { 6849 error_at (loc, 6850 "%<_Atomic%>-qualified function type"); 6851 type_quals &= ~TYPE_QUAL_ATOMIC; 6852 } 6853 else if (type_quals) 6854 pedwarn (loc, OPT_Wpedantic, 6855 "ISO C forbids qualified function types"); 6856 if (type_quals) 6857 type = c_build_qualified_type (type, type_quals); 6858 type = c_build_pointer_type (type); 6859 type_quals = TYPE_UNQUALIFIED; 6860 } 6861 else if (type_quals) 6862 type = c_build_qualified_type (type, type_quals); 6863 6864 decl = build_decl (declarator->id_loc, 6865 PARM_DECL, declarator->u.id, type); 6866 if (size_varies) 6867 C_DECL_VARIABLE_SIZE (decl) = 1; 6868 C_ARRAY_PARAMETER (decl) = array_parameter_p; 6869 6870 /* Compute the type actually passed in the parmlist, 6871 for the case where there is no prototype. 6872 (For example, shorts and chars are passed as ints.) 6873 When there is a prototype, this is overridden later. */ 6874 6875 if (type == error_mark_node) 6876 promoted_type = type; 6877 else 6878 promoted_type = c_type_promotes_to (type); 6879 6880 DECL_ARG_TYPE (decl) = promoted_type; 6881 if (declspecs->inline_p) 6882 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl); 6883 if (declspecs->noreturn_p) 6884 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl); 6885 } 6886 else if (decl_context == FIELD) 6887 { 6888 /* Note that the grammar rejects storage classes in typenames 6889 and fields. */ 6890 gcc_assert (storage_class == csc_none && !threadp 6891 && !declspecs->inline_p && !declspecs->noreturn_p); 6892 6893 /* Structure field. It may not be a function. */ 6894 6895 if (TREE_CODE (type) == FUNCTION_TYPE) 6896 { 6897 error_at (loc, "field %qE declared as a function", name); 6898 type = build_pointer_type (type); 6899 } 6900 else if (TREE_CODE (type) != ERROR_MARK 6901 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type)) 6902 { 6903 if (name) 6904 error_at (loc, "field %qE has incomplete type", name); 6905 else 6906 error_at (loc, "unnamed field has incomplete type"); 6907 type = error_mark_node; 6908 } 6909 else if (TREE_CODE (type) == ARRAY_TYPE 6910 && TYPE_DOMAIN (type) == NULL_TREE) 6911 { 6912 /* We have a flexible array member through a typedef. 6913 Set suitable range. Whether this is a correct position 6914 for a flexible array member will be determined elsewhere. */ 6915 if (!in_system_header_at (input_location)) 6916 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not " 6917 "support flexible array members"); 6918 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); 6919 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node, 6920 NULL_TREE); 6921 if (orig_qual_indirect == 0) 6922 orig_qual_type = NULL_TREE; 6923 } 6924 type = c_build_qualified_type (type, type_quals, orig_qual_type, 6925 orig_qual_indirect); 6926 decl = build_decl (declarator->id_loc, 6927 FIELD_DECL, declarator->u.id, type); 6928 DECL_NONADDRESSABLE_P (decl) = bitfield; 6929 if (bitfield && !declarator->u.id) 6930 { 6931 TREE_NO_WARNING (decl) = 1; 6932 DECL_PADDING_P (decl) = 1; 6933 } 6934 6935 if (size_varies) 6936 C_DECL_VARIABLE_SIZE (decl) = 1; 6937 } 6938 else if (TREE_CODE (type) == FUNCTION_TYPE) 6939 { 6940 if (storage_class == csc_register || threadp) 6941 { 6942 error_at (loc, "invalid storage class for function %qE", name); 6943 } 6944 else if (current_scope != file_scope) 6945 { 6946 /* Function declaration not at file scope. Storage 6947 classes other than `extern' are not allowed, C99 6948 6.7.1p5, and `extern' makes no difference. However, 6949 GCC allows 'auto', perhaps with 'inline', to support 6950 nested functions. */ 6951 if (storage_class == csc_auto) 6952 pedwarn (loc, OPT_Wpedantic, 6953 "invalid storage class for function %qE", name); 6954 else if (storage_class == csc_static) 6955 { 6956 error_at (loc, "invalid storage class for function %qE", name); 6957 if (funcdef_flag) 6958 storage_class = declspecs->storage_class = csc_none; 6959 else 6960 return NULL_TREE; 6961 } 6962 } 6963 6964 decl = build_decl (declarator->id_loc, 6965 FUNCTION_DECL, declarator->u.id, type); 6966 decl = build_decl_attribute_variant (decl, decl_attr); 6967 6968 if (type_quals & TYPE_QUAL_ATOMIC) 6969 { 6970 error_at (loc, 6971 "%<_Atomic%>-qualified function type"); 6972 type_quals &= ~TYPE_QUAL_ATOMIC; 6973 } 6974 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl)) 6975 pedwarn (loc, OPT_Wpedantic, 6976 "ISO C forbids qualified function types"); 6977 6978 /* Every function declaration is an external reference 6979 (DECL_EXTERNAL) except for those which are not at file 6980 scope and are explicitly declared "auto". This is 6981 forbidden by standard C (C99 6.7.1p5) and is interpreted by 6982 GCC to signify a forward declaration of a nested function. */ 6983 if (storage_class == csc_auto && current_scope != file_scope) 6984 DECL_EXTERNAL (decl) = 0; 6985 /* In C99, a function which is declared 'inline' with 'extern' 6986 is not an external reference (which is confusing). It 6987 means that the later definition of the function must be output 6988 in this file, C99 6.7.4p6. In GNU C89, a function declared 6989 'extern inline' is an external reference. */ 6990 else if (declspecs->inline_p && storage_class != csc_static) 6991 DECL_EXTERNAL (decl) = ((storage_class == csc_extern) 6992 == flag_gnu89_inline); 6993 else 6994 DECL_EXTERNAL (decl) = !initialized; 6995 6996 /* Record absence of global scope for `static' or `auto'. */ 6997 TREE_PUBLIC (decl) 6998 = !(storage_class == csc_static || storage_class == csc_auto); 6999 7000 /* For a function definition, record the argument information 7001 block where store_parm_decls will look for it. */ 7002 if (funcdef_flag) 7003 current_function_arg_info = arg_info; 7004 7005 if (declspecs->default_int_p) 7006 C_FUNCTION_IMPLICIT_INT (decl) = 1; 7007 7008 /* Record presence of `inline' and `_Noreturn', if it is 7009 reasonable. */ 7010 if (flag_hosted && MAIN_NAME_P (declarator->u.id)) 7011 { 7012 if (declspecs->inline_p) 7013 pedwarn (loc, 0, "cannot inline function %<main%>"); 7014 if (declspecs->noreturn_p) 7015 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>"); 7016 } 7017 else 7018 { 7019 if (declspecs->inline_p) 7020 /* Record that the function is declared `inline'. */ 7021 DECL_DECLARED_INLINE_P (decl) = 1; 7022 if (declspecs->noreturn_p) 7023 { 7024 if (flag_isoc99) 7025 pedwarn_c99 (loc, OPT_Wpedantic, 7026 "ISO C99 does not support %<_Noreturn%>"); 7027 else 7028 pedwarn_c99 (loc, OPT_Wpedantic, 7029 "ISO C90 does not support %<_Noreturn%>"); 7030 TREE_THIS_VOLATILE (decl) = 1; 7031 } 7032 } 7033 } 7034 else 7035 { 7036 /* It's a variable. */ 7037 /* An uninitialized decl with `extern' is a reference. */ 7038 int extern_ref = !initialized && storage_class == csc_extern; 7039 7040 type = c_build_qualified_type (type, type_quals, orig_qual_type, 7041 orig_qual_indirect); 7042 7043 /* C99 6.2.2p7: It is invalid (compile-time undefined 7044 behavior) to create an 'extern' declaration for a 7045 variable if there is a global declaration that is 7046 'static' and the global declaration is not visible. 7047 (If the static declaration _is_ currently visible, 7048 the 'extern' declaration is taken to refer to that decl.) */ 7049 if (extern_ref && current_scope != file_scope) 7050 { 7051 tree global_decl = identifier_global_value (declarator->u.id); 7052 tree visible_decl = lookup_name (declarator->u.id); 7053 7054 if (global_decl 7055 && global_decl != visible_decl 7056 && VAR_P (global_decl) 7057 && !TREE_PUBLIC (global_decl)) 7058 error_at (loc, "variable previously declared %<static%> " 7059 "redeclared %<extern%>"); 7060 } 7061 7062 decl = build_decl (declarator->id_loc, 7063 VAR_DECL, declarator->u.id, type); 7064 if (size_varies) 7065 C_DECL_VARIABLE_SIZE (decl) = 1; 7066 7067 if (declspecs->inline_p) 7068 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl); 7069 if (declspecs->noreturn_p) 7070 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl); 7071 7072 /* At file scope, an initialized extern declaration may follow 7073 a static declaration. In that case, DECL_EXTERNAL will be 7074 reset later in start_decl. */ 7075 DECL_EXTERNAL (decl) = (storage_class == csc_extern); 7076 7077 /* At file scope, the presence of a `static' or `register' storage 7078 class specifier, or the absence of all storage class specifiers 7079 makes this declaration a definition (perhaps tentative). Also, 7080 the absence of `static' makes it public. */ 7081 if (current_scope == file_scope) 7082 { 7083 TREE_PUBLIC (decl) = storage_class != csc_static; 7084 TREE_STATIC (decl) = !extern_ref; 7085 } 7086 /* Not at file scope, only `static' makes a static definition. */ 7087 else 7088 { 7089 TREE_STATIC (decl) = (storage_class == csc_static); 7090 TREE_PUBLIC (decl) = extern_ref; 7091 } 7092 7093 if (threadp) 7094 set_decl_tls_model (decl, decl_default_tls_model (decl)); 7095 } 7096 7097 if ((storage_class == csc_extern 7098 || (storage_class == csc_none 7099 && TREE_CODE (type) == FUNCTION_TYPE 7100 && !funcdef_flag)) 7101 && variably_modified_type_p (type, NULL_TREE)) 7102 { 7103 /* C99 6.7.5.2p2 */ 7104 if (TREE_CODE (type) == FUNCTION_TYPE) 7105 error_at (loc, "non-nested function with variably modified type"); 7106 else 7107 error_at (loc, "object with variably modified type must have " 7108 "no linkage"); 7109 } 7110 7111 /* Record `register' declaration for warnings on & 7112 and in case doing stupid register allocation. */ 7113 7114 if (storage_class == csc_register) 7115 { 7116 C_DECL_REGISTER (decl) = 1; 7117 DECL_REGISTER (decl) = 1; 7118 } 7119 7120 /* Record constancy and volatility. */ 7121 c_apply_type_quals_to_decl (type_quals, decl); 7122 7123 /* Apply _Alignas specifiers. */ 7124 if (alignas_align) 7125 { 7126 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT); 7127 DECL_USER_ALIGN (decl) = 1; 7128 } 7129 7130 /* If a type has volatile components, it should be stored in memory. 7131 Otherwise, the fact that those components are volatile 7132 will be ignored, and would even crash the compiler. 7133 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */ 7134 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)) 7135 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL 7136 || TREE_CODE (decl) == RESULT_DECL)) 7137 { 7138 /* It is not an error for a structure with volatile fields to 7139 be declared register, but reset DECL_REGISTER since it 7140 cannot actually go in a register. */ 7141 int was_reg = C_DECL_REGISTER (decl); 7142 C_DECL_REGISTER (decl) = 0; 7143 DECL_REGISTER (decl) = 0; 7144 c_mark_addressable (decl); 7145 C_DECL_REGISTER (decl) = was_reg; 7146 } 7147 7148 /* This is the earliest point at which we might know the assembler 7149 name of a variable. Thus, if it's known before this, die horribly. */ 7150 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl) 7151 || !DECL_ASSEMBLER_NAME_SET_P (decl)); 7152 7153 if (warn_cxx_compat 7154 && VAR_P (decl) 7155 && TREE_PUBLIC (decl) 7156 && TREE_STATIC (decl) 7157 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)) 7158 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) 7159 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE) 7160 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat, 7161 ("non-local variable %qD with anonymous type is " 7162 "questionable in C++"), 7163 decl); 7164 7165 return decl; 7166 } 7167 } 7168 7169 /* Decode the parameter-list info for a function type or function definition. 7170 The argument is the value returned by `get_parm_info' (or made in c-parse.c 7171 if there is an identifier list instead of a parameter decl list). 7172 These two functions are separate because when a function returns 7173 or receives functions then each is called multiple times but the order 7174 of calls is different. The last call to `grokparms' is always the one 7175 that contains the formal parameter names of a function definition. 7176 7177 Return a list of arg types to use in the FUNCTION_TYPE for this function. 7178 7179 FUNCDEF_FLAG is true for a function definition, false for 7180 a mere declaration. A nonempty identifier-list gets an error message 7181 when FUNCDEF_FLAG is false. */ 7182 7183 static tree 7184 grokparms (struct c_arg_info *arg_info, bool funcdef_flag) 7185 { 7186 tree arg_types = arg_info->types; 7187 7188 if (funcdef_flag && arg_info->had_vla_unspec) 7189 { 7190 /* A function definition isn't function prototype scope C99 6.2.1p4. */ 7191 /* C99 6.7.5.2p4 */ 7192 error ("%<[*]%> not allowed in other than function prototype scope"); 7193 } 7194 7195 if (arg_types == NULL_TREE && !funcdef_flag 7196 && !in_system_header_at (input_location)) 7197 warning (OPT_Wstrict_prototypes, 7198 "function declaration isn%'t a prototype"); 7199 7200 if (arg_types == error_mark_node) 7201 /* Don't set TYPE_ARG_TYPES in this case. */ 7202 return NULL_TREE; 7203 7204 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE) 7205 { 7206 if (!funcdef_flag) 7207 { 7208 pedwarn (input_location, 0, "parameter names (without types) in " 7209 "function declaration"); 7210 arg_info->parms = NULL_TREE; 7211 } 7212 else 7213 arg_info->parms = arg_info->types; 7214 7215 arg_info->types = NULL_TREE; 7216 return NULL_TREE; 7217 } 7218 else 7219 { 7220 tree parm, type, typelt; 7221 unsigned int parmno; 7222 7223 /* If there is a parameter of incomplete type in a definition, 7224 this is an error. In a declaration this is valid, and a 7225 struct or union type may be completed later, before any calls 7226 or definition of the function. In the case where the tag was 7227 first declared within the parameter list, a warning has 7228 already been given. If a parameter has void type, then 7229 however the function cannot be defined or called, so 7230 warn. */ 7231 7232 for (parm = arg_info->parms, typelt = arg_types, parmno = 1; 7233 parm; 7234 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++) 7235 { 7236 type = TREE_VALUE (typelt); 7237 if (type == error_mark_node) 7238 continue; 7239 7240 if (!COMPLETE_TYPE_P (type)) 7241 { 7242 if (funcdef_flag) 7243 { 7244 if (DECL_NAME (parm)) 7245 error_at (input_location, 7246 "parameter %u (%q+D) has incomplete type", 7247 parmno, parm); 7248 else 7249 error_at (DECL_SOURCE_LOCATION (parm), 7250 "parameter %u has incomplete type", 7251 parmno); 7252 7253 TREE_VALUE (typelt) = error_mark_node; 7254 TREE_TYPE (parm) = error_mark_node; 7255 arg_types = NULL_TREE; 7256 } 7257 else if (VOID_TYPE_P (type)) 7258 { 7259 if (DECL_NAME (parm)) 7260 warning_at (input_location, 0, 7261 "parameter %u (%q+D) has void type", 7262 parmno, parm); 7263 else 7264 warning_at (DECL_SOURCE_LOCATION (parm), 0, 7265 "parameter %u has void type", 7266 parmno); 7267 } 7268 } 7269 7270 if (DECL_NAME (parm) && TREE_USED (parm)) 7271 warn_if_shadowing (parm); 7272 } 7273 return arg_types; 7274 } 7275 } 7276 7277 /* Allocate and initialize a c_arg_info structure from the parser's 7278 obstack. */ 7279 7280 struct c_arg_info * 7281 build_arg_info (void) 7282 { 7283 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info); 7284 ret->parms = NULL_TREE; 7285 ret->tags = NULL; 7286 ret->types = NULL_TREE; 7287 ret->others = NULL_TREE; 7288 ret->pending_sizes = NULL; 7289 ret->had_vla_unspec = 0; 7290 return ret; 7291 } 7292 7293 /* Take apart the current scope and return a c_arg_info structure with 7294 info on a parameter list just parsed. 7295 7296 This structure is later fed to 'grokparms' and 'store_parm_decls'. 7297 7298 ELLIPSIS being true means the argument list ended in '...' so don't 7299 append a sentinel (void_list_node) to the end of the type-list. 7300 7301 EXPR is NULL or an expression that needs to be evaluated for the 7302 side effects of array size expressions in the parameters. */ 7303 7304 struct c_arg_info * 7305 get_parm_info (bool ellipsis, tree expr) 7306 { 7307 struct c_binding *b = current_scope->bindings; 7308 struct c_arg_info *arg_info = build_arg_info (); 7309 7310 tree parms = NULL_TREE; 7311 vec<c_arg_tag, va_gc> *tags = NULL; 7312 tree types = NULL_TREE; 7313 tree others = NULL_TREE; 7314 7315 bool gave_void_only_once_err = false; 7316 7317 arg_info->had_vla_unspec = current_scope->had_vla_unspec; 7318 7319 /* The bindings in this scope must not get put into a block. 7320 We will take care of deleting the binding nodes. */ 7321 current_scope->bindings = 0; 7322 7323 /* This function is only called if there was *something* on the 7324 parameter list. */ 7325 gcc_assert (b); 7326 7327 /* A parameter list consisting solely of 'void' indicates that the 7328 function takes no arguments. But if the 'void' is qualified 7329 (by 'const' or 'volatile'), or has a storage class specifier 7330 ('register'), then the behavior is undefined; issue an error. 7331 Typedefs for 'void' are OK (see DR#157). */ 7332 if (b->prev == 0 /* one binding */ 7333 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */ 7334 && !DECL_NAME (b->decl) /* anonymous */ 7335 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */ 7336 { 7337 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED 7338 || C_DECL_REGISTER (b->decl)) 7339 error_at (b->locus, "%<void%> as only parameter may not be qualified"); 7340 7341 /* There cannot be an ellipsis. */ 7342 if (ellipsis) 7343 error_at (b->locus, "%<void%> must be the only parameter"); 7344 7345 arg_info->types = void_list_node; 7346 return arg_info; 7347 } 7348 7349 if (!ellipsis) 7350 types = void_list_node; 7351 7352 /* Break up the bindings list into parms, tags, types, and others; 7353 apply sanity checks; purge the name-to-decl bindings. */ 7354 while (b) 7355 { 7356 tree decl = b->decl; 7357 tree type = TREE_TYPE (decl); 7358 c_arg_tag tag; 7359 const char *keyword; 7360 7361 switch (TREE_CODE (decl)) 7362 { 7363 case PARM_DECL: 7364 if (b->id) 7365 { 7366 gcc_assert (I_SYMBOL_BINDING (b->id) == b); 7367 I_SYMBOL_BINDING (b->id) = b->shadowed; 7368 } 7369 7370 /* Check for forward decls that never got their actual decl. */ 7371 if (TREE_ASM_WRITTEN (decl)) 7372 error_at (b->locus, 7373 "parameter %q+D has just a forward declaration", decl); 7374 /* Check for (..., void, ...) and issue an error. */ 7375 else if (VOID_TYPE_P (type) && !DECL_NAME (decl)) 7376 { 7377 if (!gave_void_only_once_err) 7378 { 7379 error_at (b->locus, "%<void%> must be the only parameter"); 7380 gave_void_only_once_err = true; 7381 } 7382 } 7383 else 7384 { 7385 /* Valid parameter, add it to the list. */ 7386 DECL_CHAIN (decl) = parms; 7387 parms = decl; 7388 7389 /* Since there is a prototype, args are passed in their 7390 declared types. The back end may override this later. */ 7391 DECL_ARG_TYPE (decl) = type; 7392 types = tree_cons (0, type, types); 7393 } 7394 break; 7395 7396 case ENUMERAL_TYPE: keyword = "enum"; goto tag; 7397 case UNION_TYPE: keyword = "union"; goto tag; 7398 case RECORD_TYPE: keyword = "struct"; goto tag; 7399 tag: 7400 /* Types may not have tag-names, in which case the type 7401 appears in the bindings list with b->id NULL. */ 7402 if (b->id) 7403 { 7404 gcc_assert (I_TAG_BINDING (b->id) == b); 7405 I_TAG_BINDING (b->id) = b->shadowed; 7406 } 7407 7408 /* Warn about any struct, union or enum tags defined in a 7409 parameter list. The scope of such types is limited to 7410 the parameter list, which is rarely if ever desirable 7411 (it's impossible to call such a function with type- 7412 correct arguments). An anonymous union parm type is 7413 meaningful as a GNU extension, so don't warn for that. */ 7414 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE) 7415 { 7416 if (b->id) 7417 /* The %s will be one of 'struct', 'union', or 'enum'. */ 7418 warning_at (b->locus, 0, 7419 "%<%s %E%> declared inside parameter list" 7420 " will not be visible outside of this definition or" 7421 " declaration", keyword, b->id); 7422 else 7423 /* The %s will be one of 'struct', 'union', or 'enum'. */ 7424 warning_at (b->locus, 0, 7425 "anonymous %s declared inside parameter list" 7426 " will not be visible outside of this definition or" 7427 " declaration", keyword); 7428 } 7429 7430 tag.id = b->id; 7431 tag.type = decl; 7432 vec_safe_push (tags, tag); 7433 break; 7434 7435 case FUNCTION_DECL: 7436 /* FUNCTION_DECLs appear when there is an implicit function 7437 declaration in the parameter list. */ 7438 gcc_assert (b->nested || seen_error ()); 7439 goto set_shadowed; 7440 7441 case CONST_DECL: 7442 case TYPE_DECL: 7443 /* CONST_DECLs appear here when we have an embedded enum, 7444 and TYPE_DECLs appear here when we have an embedded struct 7445 or union. No warnings for this - we already warned about the 7446 type itself. */ 7447 7448 /* When we reinsert this decl in the function body, we need 7449 to reconstruct whether it was marked as nested. */ 7450 gcc_assert (!b->nested); 7451 DECL_CHAIN (decl) = others; 7452 others = decl; 7453 /* fall through */ 7454 7455 case ERROR_MARK: 7456 set_shadowed: 7457 /* error_mark_node appears here when we have an undeclared 7458 variable. Just throw it away. */ 7459 if (b->id) 7460 { 7461 gcc_assert (I_SYMBOL_BINDING (b->id) == b); 7462 I_SYMBOL_BINDING (b->id) = b->shadowed; 7463 } 7464 break; 7465 7466 /* Other things that might be encountered. */ 7467 case LABEL_DECL: 7468 case VAR_DECL: 7469 default: 7470 gcc_unreachable (); 7471 } 7472 7473 b = free_binding_and_advance (b); 7474 } 7475 7476 arg_info->parms = parms; 7477 arg_info->tags = tags; 7478 arg_info->types = types; 7479 arg_info->others = others; 7480 arg_info->pending_sizes = expr; 7481 return arg_info; 7482 } 7483 7484 /* Get the struct, enum or union (CODE says which) with tag NAME. 7485 Define the tag as a forward-reference with location LOC if it is 7486 not defined. Return a c_typespec structure for the type 7487 specifier. */ 7488 7489 struct c_typespec 7490 parser_xref_tag (location_t loc, enum tree_code code, tree name) 7491 { 7492 struct c_typespec ret; 7493 tree ref; 7494 location_t refloc; 7495 7496 ret.expr = NULL_TREE; 7497 ret.expr_const_operands = true; 7498 7499 /* If a cross reference is requested, look up the type 7500 already defined for this tag and return it. */ 7501 7502 ref = lookup_tag (code, name, false, &refloc); 7503 /* If this is the right type of tag, return what we found. 7504 (This reference will be shadowed by shadow_tag later if appropriate.) 7505 If this is the wrong type of tag, do not return it. If it was the 7506 wrong type in the same scope, we will have had an error 7507 message already; if in a different scope and declaring 7508 a name, pending_xref_error will give an error message; but if in a 7509 different scope and not declaring a name, this tag should 7510 shadow the previous declaration of a different type of tag, and 7511 this would not work properly if we return the reference found. 7512 (For example, with "struct foo" in an outer scope, "union foo;" 7513 must shadow that tag with a new one of union type.) */ 7514 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref); 7515 if (ref && TREE_CODE (ref) == code) 7516 { 7517 if (C_TYPE_DEFINED_IN_STRUCT (ref) 7518 && loc != UNKNOWN_LOCATION 7519 && warn_cxx_compat) 7520 { 7521 switch (code) 7522 { 7523 case ENUMERAL_TYPE: 7524 warning_at (loc, OPT_Wc___compat, 7525 ("enum type defined in struct or union " 7526 "is not visible in C++")); 7527 inform (refloc, "enum type defined here"); 7528 break; 7529 case RECORD_TYPE: 7530 warning_at (loc, OPT_Wc___compat, 7531 ("struct defined in struct or union " 7532 "is not visible in C++")); 7533 inform (refloc, "struct defined here"); 7534 break; 7535 case UNION_TYPE: 7536 warning_at (loc, OPT_Wc___compat, 7537 ("union defined in struct or union " 7538 "is not visible in C++")); 7539 inform (refloc, "union defined here"); 7540 break; 7541 default: 7542 gcc_unreachable(); 7543 } 7544 } 7545 7546 ret.spec = ref; 7547 return ret; 7548 } 7549 7550 /* If no such tag is yet defined, create a forward-reference node 7551 and record it as the "definition". 7552 When a real declaration of this type is found, 7553 the forward-reference will be altered into a real type. */ 7554 7555 ref = make_node (code); 7556 if (code == ENUMERAL_TYPE) 7557 { 7558 /* Give the type a default layout like unsigned int 7559 to avoid crashing if it does not get defined. */ 7560 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node)); 7561 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node)); 7562 TYPE_USER_ALIGN (ref) = 0; 7563 TYPE_UNSIGNED (ref) = 1; 7564 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); 7565 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); 7566 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); 7567 } 7568 7569 pushtag (loc, name, ref); 7570 7571 ret.spec = ref; 7572 return ret; 7573 } 7574 7575 /* Get the struct, enum or union (CODE says which) with tag NAME. 7576 Define the tag as a forward-reference if it is not defined. 7577 Return a tree for the type. */ 7578 7579 tree 7580 xref_tag (enum tree_code code, tree name) 7581 { 7582 return parser_xref_tag (input_location, code, name).spec; 7583 } 7584 7585 /* Make sure that the tag NAME is defined *in the current scope* 7586 at least as a forward reference. 7587 LOC is the location of the struct's definition. 7588 CODE says which kind of tag NAME ought to be. 7589 7590 This stores the current value of the file static STRUCT_PARSE_INFO 7591 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a 7592 new c_struct_parse_info structure. The old value of 7593 STRUCT_PARSE_INFO is restored in finish_struct. */ 7594 7595 tree 7596 start_struct (location_t loc, enum tree_code code, tree name, 7597 struct c_struct_parse_info **enclosing_struct_parse_info) 7598 { 7599 /* If there is already a tag defined at this scope 7600 (as a forward reference), just return it. */ 7601 7602 tree ref = NULL_TREE; 7603 location_t refloc = UNKNOWN_LOCATION; 7604 7605 if (name != NULL_TREE) 7606 ref = lookup_tag (code, name, true, &refloc); 7607 if (ref && TREE_CODE (ref) == code) 7608 { 7609 if (TYPE_STUB_DECL (ref)) 7610 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref)); 7611 7612 if (TYPE_SIZE (ref)) 7613 { 7614 if (code == UNION_TYPE) 7615 error_at (loc, "redefinition of %<union %E%>", name); 7616 else 7617 error_at (loc, "redefinition of %<struct %E%>", name); 7618 if (refloc != UNKNOWN_LOCATION) 7619 inform (refloc, "originally defined here"); 7620 /* Don't create structures using a name already in use. */ 7621 ref = NULL_TREE; 7622 } 7623 else if (C_TYPE_BEING_DEFINED (ref)) 7624 { 7625 if (code == UNION_TYPE) 7626 error_at (loc, "nested redefinition of %<union %E%>", name); 7627 else 7628 error_at (loc, "nested redefinition of %<struct %E%>", name); 7629 /* Don't bother to report "originally defined here" for a 7630 nested redefinition; the original definition should be 7631 obvious. */ 7632 /* Don't create structures that contain themselves. */ 7633 ref = NULL_TREE; 7634 } 7635 } 7636 7637 /* Otherwise create a forward-reference just so the tag is in scope. */ 7638 7639 if (ref == NULL_TREE || TREE_CODE (ref) != code) 7640 { 7641 ref = make_node (code); 7642 pushtag (loc, name, ref); 7643 } 7644 7645 C_TYPE_BEING_DEFINED (ref) = 1; 7646 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v)) 7647 TYPE_PACKED (v) = flag_pack_struct; 7648 7649 *enclosing_struct_parse_info = struct_parse_info; 7650 struct_parse_info = new c_struct_parse_info (); 7651 7652 /* FIXME: This will issue a warning for a use of a type defined 7653 within a statement expr used within sizeof, et. al. This is not 7654 terribly serious as C++ doesn't permit statement exprs within 7655 sizeof anyhow. */ 7656 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof)) 7657 warning_at (loc, OPT_Wc___compat, 7658 "defining type in %qs expression is invalid in C++", 7659 (in_sizeof 7660 ? "sizeof" 7661 : (in_typeof ? "typeof" : "alignof"))); 7662 7663 return ref; 7664 } 7665 7666 /* Process the specs, declarator and width (NULL if omitted) 7667 of a structure component, returning a FIELD_DECL node. 7668 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node. 7669 DECL_ATTRS is as for grokdeclarator. 7670 7671 LOC is the location of the structure component. 7672 7673 This is done during the parsing of the struct declaration. 7674 The FIELD_DECL nodes are chained together and the lot of them 7675 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */ 7676 7677 tree 7678 grokfield (location_t loc, 7679 struct c_declarator *declarator, struct c_declspecs *declspecs, 7680 tree width, tree *decl_attrs) 7681 { 7682 tree value; 7683 7684 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE 7685 && width == NULL_TREE) 7686 { 7687 /* This is an unnamed decl. 7688 7689 If we have something of the form "union { list } ;" then this 7690 is the anonymous union extension. Similarly for struct. 7691 7692 If this is something of the form "struct foo;", then 7693 If MS or Plan 9 extensions are enabled, this is handled as 7694 an anonymous struct. 7695 Otherwise this is a forward declaration of a structure tag. 7696 7697 If this is something of the form "foo;" and foo is a TYPE_DECL, then 7698 If foo names a structure or union without a tag, then this 7699 is an anonymous struct (this is permitted by C11). 7700 If MS or Plan 9 extensions are enabled and foo names a 7701 structure, then again this is an anonymous struct. 7702 Otherwise this is an error. 7703 7704 Oh what a horrid tangled web we weave. I wonder if MS consciously 7705 took this from Plan 9 or if it was an accident of implementation 7706 that took root before someone noticed the bug... */ 7707 7708 tree type = declspecs->type; 7709 bool ok = false; 7710 7711 if (RECORD_OR_UNION_TYPE_P (type) 7712 && (flag_ms_extensions 7713 || flag_plan9_extensions 7714 || !declspecs->typedef_p)) 7715 { 7716 if (flag_ms_extensions || flag_plan9_extensions) 7717 ok = true; 7718 else if (TYPE_NAME (type) == NULL) 7719 ok = true; 7720 else 7721 ok = false; 7722 } 7723 if (!ok) 7724 { 7725 pedwarn (loc, 0, "declaration does not declare anything"); 7726 return NULL_TREE; 7727 } 7728 if (flag_isoc99) 7729 pedwarn_c99 (loc, OPT_Wpedantic, 7730 "ISO C99 doesn%'t support unnamed structs/unions"); 7731 else 7732 pedwarn_c99 (loc, OPT_Wpedantic, 7733 "ISO C90 doesn%'t support unnamed structs/unions"); 7734 } 7735 7736 value = grokdeclarator (declarator, declspecs, FIELD, false, 7737 width ? &width : NULL, decl_attrs, NULL, NULL, 7738 DEPRECATED_NORMAL); 7739 7740 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE); 7741 DECL_INITIAL (value) = width; 7742 if (width) 7743 SET_DECL_C_BIT_FIELD (value); 7744 7745 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE) 7746 { 7747 /* If we currently have a binding for this field, set the 7748 in_struct field in the binding, so that we warn about lookups 7749 which find it. */ 7750 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value)); 7751 if (b != NULL) 7752 { 7753 /* If the in_struct field is not yet set, push it on a list 7754 to be cleared when this struct is finished. */ 7755 if (!b->in_struct) 7756 { 7757 struct_parse_info->fields.safe_push (b); 7758 b->in_struct = 1; 7759 } 7760 } 7761 } 7762 7763 return value; 7764 } 7765 7766 /* Subroutine of detect_field_duplicates: return whether X and Y, 7767 which are both fields in the same struct, have duplicate field 7768 names. */ 7769 7770 static bool 7771 is_duplicate_field (tree x, tree y) 7772 { 7773 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y)) 7774 return true; 7775 7776 /* When using -fplan9-extensions, an anonymous field whose name is a 7777 typedef can duplicate a field name. */ 7778 if (flag_plan9_extensions 7779 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE)) 7780 { 7781 tree xt, xn, yt, yn; 7782 7783 xt = TREE_TYPE (x); 7784 if (DECL_NAME (x) != NULL_TREE) 7785 xn = DECL_NAME (x); 7786 else if (RECORD_OR_UNION_TYPE_P (xt) 7787 && TYPE_NAME (xt) != NULL_TREE 7788 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL) 7789 xn = DECL_NAME (TYPE_NAME (xt)); 7790 else 7791 xn = NULL_TREE; 7792 7793 yt = TREE_TYPE (y); 7794 if (DECL_NAME (y) != NULL_TREE) 7795 yn = DECL_NAME (y); 7796 else if (RECORD_OR_UNION_TYPE_P (yt) 7797 && TYPE_NAME (yt) != NULL_TREE 7798 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL) 7799 yn = DECL_NAME (TYPE_NAME (yt)); 7800 else 7801 yn = NULL_TREE; 7802 7803 if (xn != NULL_TREE && xn == yn) 7804 return true; 7805 } 7806 7807 return false; 7808 } 7809 7810 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST 7811 to HTAB, giving errors for any duplicates. */ 7812 7813 static void 7814 detect_field_duplicates_hash (tree fieldlist, 7815 hash_table<nofree_ptr_hash <tree_node> > *htab) 7816 { 7817 tree x, y; 7818 tree_node **slot; 7819 7820 for (x = fieldlist; x ; x = DECL_CHAIN (x)) 7821 if ((y = DECL_NAME (x)) != NULL_TREE) 7822 { 7823 slot = htab->find_slot (y, INSERT); 7824 if (*slot) 7825 { 7826 error ("duplicate member %q+D", x); 7827 DECL_NAME (x) = NULL_TREE; 7828 } 7829 *slot = y; 7830 } 7831 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) 7832 { 7833 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab); 7834 7835 /* When using -fplan9-extensions, an anonymous field whose 7836 name is a typedef can duplicate a field name. */ 7837 if (flag_plan9_extensions 7838 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE 7839 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL) 7840 { 7841 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x))); 7842 slot = htab->find_slot (xn, INSERT); 7843 if (*slot) 7844 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x))); 7845 *slot = xn; 7846 } 7847 } 7848 } 7849 7850 /* Generate an error for any duplicate field names in FIELDLIST. Munge 7851 the list such that this does not present a problem later. */ 7852 7853 static void 7854 detect_field_duplicates (tree fieldlist) 7855 { 7856 tree x, y; 7857 int timeout = 10; 7858 7859 /* If the struct is the list of instance variables of an Objective-C 7860 class, then we need to check all the instance variables of 7861 superclasses when checking for duplicates (since you can't have 7862 an instance variable in a subclass with the same name as an 7863 instance variable in a superclass). We pass on this job to the 7864 Objective-C compiler. objc_detect_field_duplicates() will return 7865 false if we are not checking the list of instance variables and 7866 the C frontend should proceed with the standard field duplicate 7867 checks. If we are checking the list of instance variables, the 7868 ObjC frontend will do the check, emit the errors if needed, and 7869 then return true. */ 7870 if (c_dialect_objc ()) 7871 if (objc_detect_field_duplicates (false)) 7872 return; 7873 7874 /* First, see if there are more than "a few" fields. 7875 This is trivially true if there are zero or one fields. */ 7876 if (!fieldlist || !DECL_CHAIN (fieldlist)) 7877 return; 7878 x = fieldlist; 7879 do { 7880 timeout--; 7881 if (DECL_NAME (x) == NULL_TREE 7882 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) 7883 timeout = 0; 7884 x = DECL_CHAIN (x); 7885 } while (timeout > 0 && x); 7886 7887 /* If there were "few" fields and no anonymous structures or unions, 7888 avoid the overhead of allocating a hash table. Instead just do 7889 the nested traversal thing. */ 7890 if (timeout > 0) 7891 { 7892 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x)) 7893 /* When using -fplan9-extensions, we can have duplicates 7894 between typedef names and fields. */ 7895 if (DECL_NAME (x) 7896 || (flag_plan9_extensions 7897 && DECL_NAME (x) == NULL_TREE 7898 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)) 7899 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE 7900 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)) 7901 { 7902 for (y = fieldlist; y != x; y = TREE_CHAIN (y)) 7903 if (is_duplicate_field (y, x)) 7904 { 7905 error ("duplicate member %q+D", x); 7906 DECL_NAME (x) = NULL_TREE; 7907 } 7908 } 7909 } 7910 else 7911 { 7912 hash_table<nofree_ptr_hash <tree_node> > htab (37); 7913 detect_field_duplicates_hash (fieldlist, &htab); 7914 } 7915 } 7916 7917 /* Finish up struct info used by -Wc++-compat. */ 7918 7919 static void 7920 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code, 7921 location_t record_loc) 7922 { 7923 unsigned int ix; 7924 tree x; 7925 struct c_binding *b; 7926 7927 if (fieldlist == NULL_TREE) 7928 { 7929 if (code == RECORD_TYPE) 7930 warning_at (record_loc, OPT_Wc___compat, 7931 "empty struct has size 0 in C, size 1 in C++"); 7932 else 7933 warning_at (record_loc, OPT_Wc___compat, 7934 "empty union has size 0 in C, size 1 in C++"); 7935 } 7936 7937 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in 7938 the current struct. We do this now at the end of the struct 7939 because the flag is used to issue visibility warnings, and we 7940 only want to issue those warnings if the type is referenced 7941 outside of the struct declaration. */ 7942 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x) 7943 C_TYPE_DEFINED_IN_STRUCT (x) = 1; 7944 7945 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of 7946 typedefs used when declaring fields in this struct. If the name 7947 of any of the fields is also a typedef name then the struct would 7948 not parse in C++, because the C++ lookup rules say that the 7949 typedef name would be looked up in the context of the struct, and 7950 would thus be the field rather than the typedef. */ 7951 if (!struct_parse_info->typedefs_seen.is_empty () 7952 && fieldlist != NULL_TREE) 7953 { 7954 /* Use a hash_set<tree> using the name of the typedef. We can use 7955 a hash_set<tree> because identifiers are interned. */ 7956 hash_set<tree> tset; 7957 7958 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x) 7959 tset.add (DECL_NAME (x)); 7960 7961 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x)) 7962 { 7963 if (DECL_NAME (x) != NULL_TREE 7964 && tset.contains (DECL_NAME (x))) 7965 { 7966 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat, 7967 ("using %qD as both field and typedef name is " 7968 "invalid in C++"), 7969 x); 7970 /* FIXME: It would be nice to report the location where 7971 the typedef name is used. */ 7972 } 7973 } 7974 } 7975 7976 /* For each field which has a binding and which was not defined in 7977 an enclosing struct, clear the in_struct field. */ 7978 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b) 7979 b->in_struct = 0; 7980 } 7981 7982 /* Function to help qsort sort FIELD_DECLs by name order. */ 7983 7984 static int 7985 field_decl_cmp (const void *x_p, const void *y_p) 7986 { 7987 const tree *const x = (const tree *) x_p; 7988 const tree *const y = (const tree *) y_p; 7989 7990 if (DECL_NAME (*x) == DECL_NAME (*y)) 7991 /* A nontype is "greater" than a type. */ 7992 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); 7993 if (DECL_NAME (*x) == NULL_TREE) 7994 return -1; 7995 if (DECL_NAME (*y) == NULL_TREE) 7996 return 1; 7997 if (DECL_NAME (*x) < DECL_NAME (*y)) 7998 return -1; 7999 return 1; 8000 } 8001 8002 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. 8003 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition. 8004 FIELDLIST is a chain of FIELD_DECL nodes for the fields. 8005 ATTRIBUTES are attributes to be applied to the structure. 8006 8007 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when 8008 the struct was started. */ 8009 8010 tree 8011 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, 8012 struct c_struct_parse_info *enclosing_struct_parse_info) 8013 { 8014 tree x; 8015 bool toplevel = file_scope == current_scope; 8016 8017 /* If this type was previously laid out as a forward reference, 8018 make sure we lay it out again. */ 8019 8020 TYPE_SIZE (t) = NULL_TREE; 8021 8022 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 8023 8024 if (pedantic) 8025 { 8026 for (x = fieldlist; x; x = DECL_CHAIN (x)) 8027 { 8028 if (DECL_NAME (x) != NULL_TREE) 8029 break; 8030 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) 8031 break; 8032 } 8033 8034 if (x == NULL_TREE) 8035 { 8036 if (TREE_CODE (t) == UNION_TYPE) 8037 { 8038 if (fieldlist) 8039 pedwarn (loc, OPT_Wpedantic, "union has no named members"); 8040 else 8041 pedwarn (loc, OPT_Wpedantic, "union has no members"); 8042 } 8043 else 8044 { 8045 if (fieldlist) 8046 pedwarn (loc, OPT_Wpedantic, "struct has no named members"); 8047 else 8048 pedwarn (loc, OPT_Wpedantic, "struct has no members"); 8049 } 8050 } 8051 } 8052 8053 /* Install struct as DECL_CONTEXT of each field decl. 8054 Also process specified field sizes, found in the DECL_INITIAL, 8055 storing 0 there after the type has been changed to precision equal 8056 to its width, rather than the precision of the specified standard 8057 type. (Correct layout requires the original type to have been preserved 8058 until now.) */ 8059 8060 bool saw_named_field = false; 8061 for (x = fieldlist; x; x = DECL_CHAIN (x)) 8062 { 8063 if (TREE_TYPE (x) == error_mark_node) 8064 continue; 8065 8066 DECL_CONTEXT (x) = t; 8067 8068 /* If any field is const, the structure type is pseudo-const. */ 8069 if (TREE_READONLY (x)) 8070 C_TYPE_FIELDS_READONLY (t) = 1; 8071 else 8072 { 8073 /* A field that is pseudo-const makes the structure likewise. */ 8074 tree t1 = strip_array_types (TREE_TYPE (x)); 8075 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1)) 8076 C_TYPE_FIELDS_READONLY (t) = 1; 8077 } 8078 8079 /* Any field that is volatile means variables of this type must be 8080 treated in some ways as volatile. */ 8081 if (TREE_THIS_VOLATILE (x)) 8082 C_TYPE_FIELDS_VOLATILE (t) = 1; 8083 8084 /* Any field of nominal variable size implies structure is too. */ 8085 if (C_DECL_VARIABLE_SIZE (x)) 8086 C_TYPE_VARIABLE_SIZE (t) = 1; 8087 8088 if (DECL_C_BIT_FIELD (x)) 8089 { 8090 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x)); 8091 DECL_SIZE (x) = bitsize_int (width); 8092 DECL_BIT_FIELD (x) = 1; 8093 } 8094 8095 if (TYPE_PACKED (t) 8096 && (DECL_BIT_FIELD (x) 8097 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)) 8098 DECL_PACKED (x) = 1; 8099 8100 /* Detect flexible array member in an invalid context. */ 8101 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 8102 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 8103 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 8104 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 8105 { 8106 if (TREE_CODE (t) == UNION_TYPE) 8107 { 8108 error_at (DECL_SOURCE_LOCATION (x), 8109 "flexible array member in union"); 8110 TREE_TYPE (x) = error_mark_node; 8111 } 8112 else if (DECL_CHAIN (x) != NULL_TREE) 8113 { 8114 error_at (DECL_SOURCE_LOCATION (x), 8115 "flexible array member not at end of struct"); 8116 TREE_TYPE (x) = error_mark_node; 8117 } 8118 else if (!saw_named_field) 8119 { 8120 error_at (DECL_SOURCE_LOCATION (x), 8121 "flexible array member in a struct with no named " 8122 "members"); 8123 TREE_TYPE (x) = error_mark_node; 8124 } 8125 } 8126 8127 if (pedantic && TREE_CODE (t) == RECORD_TYPE 8128 && flexible_array_type_p (TREE_TYPE (x))) 8129 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic, 8130 "invalid use of structure with flexible array member"); 8131 8132 if (DECL_NAME (x) 8133 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) 8134 saw_named_field = true; 8135 } 8136 8137 detect_field_duplicates (fieldlist); 8138 8139 /* Now we have the nearly final fieldlist. Record it, 8140 then lay out the structure or union (including the fields). */ 8141 8142 TYPE_FIELDS (t) = fieldlist; 8143 8144 maybe_apply_pragma_scalar_storage_order (t); 8145 8146 layout_type (t); 8147 8148 if (TYPE_SIZE_UNIT (t) 8149 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST 8150 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t)) 8151 && !valid_constant_size_p (TYPE_SIZE_UNIT (t))) 8152 error ("type %qT is too large", t); 8153 8154 /* Give bit-fields their proper types and rewrite the type of array fields 8155 with scalar component if the enclosing type has reverse storage order. */ 8156 for (tree field = fieldlist; field; field = DECL_CHAIN (field)) 8157 { 8158 if (TREE_CODE (field) == FIELD_DECL 8159 && DECL_INITIAL (field) 8160 && TREE_TYPE (field) != error_mark_node) 8161 { 8162 unsigned HOST_WIDE_INT width 8163 = tree_to_uhwi (DECL_INITIAL (field)); 8164 tree type = TREE_TYPE (field); 8165 if (width != TYPE_PRECISION (type)) 8166 { 8167 TREE_TYPE (field) 8168 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type)); 8169 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field))); 8170 } 8171 DECL_INITIAL (field) = NULL_TREE; 8172 } 8173 else if (TYPE_REVERSE_STORAGE_ORDER (t) 8174 && TREE_CODE (field) == FIELD_DECL 8175 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE) 8176 { 8177 tree ftype = TREE_TYPE (field); 8178 tree ctype = strip_array_types (ftype); 8179 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode) 8180 { 8181 tree fmain_type = TYPE_MAIN_VARIANT (ftype); 8182 tree *typep = &fmain_type; 8183 do { 8184 *typep = build_distinct_type_copy (*typep); 8185 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1; 8186 typep = &TREE_TYPE (*typep); 8187 } while (TREE_CODE (*typep) == ARRAY_TYPE); 8188 TREE_TYPE (field) 8189 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype)); 8190 } 8191 } 8192 } 8193 8194 /* Now we have the truly final field list. 8195 Store it in this type and in the variants. */ 8196 8197 TYPE_FIELDS (t) = fieldlist; 8198 8199 /* If there are lots of fields, sort so we can look through them fast. 8200 We arbitrarily consider 16 or more elts to be "a lot". */ 8201 8202 { 8203 int len = 0; 8204 8205 for (x = fieldlist; x; x = DECL_CHAIN (x)) 8206 { 8207 if (len > 15 || DECL_NAME (x) == NULL) 8208 break; 8209 len += 1; 8210 } 8211 8212 if (len > 15) 8213 { 8214 tree *field_array; 8215 struct lang_type *space; 8216 struct sorted_fields_type *space2; 8217 8218 len += list_length (x); 8219 8220 /* Use the same allocation policy here that make_node uses, to 8221 ensure that this lives as long as the rest of the struct decl. 8222 All decls in an inline function need to be saved. */ 8223 8224 space = ggc_cleared_alloc<struct lang_type> (); 8225 space2 = (sorted_fields_type *) ggc_internal_alloc 8226 (sizeof (struct sorted_fields_type) + len * sizeof (tree)); 8227 8228 len = 0; 8229 space->s = space2; 8230 field_array = &space2->elts[0]; 8231 for (x = fieldlist; x; x = DECL_CHAIN (x)) 8232 { 8233 field_array[len++] = x; 8234 8235 /* If there is anonymous struct or union, break out of the loop. */ 8236 if (DECL_NAME (x) == NULL) 8237 break; 8238 } 8239 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */ 8240 if (x == NULL) 8241 { 8242 TYPE_LANG_SPECIFIC (t) = space; 8243 TYPE_LANG_SPECIFIC (t)->s->len = len; 8244 field_array = TYPE_LANG_SPECIFIC (t)->s->elts; 8245 qsort (field_array, len, sizeof (tree), field_decl_cmp); 8246 } 8247 } 8248 } 8249 8250 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used 8251 in dwarf2out via rest_of_decl_compilation below and means 8252 something totally different. Since we will be clearing 8253 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them, 8254 clear it ahead of time and avoid problems in dwarf2out. Ideally, 8255 C_TYPE_INCOMPLETE_VARS should use some language specific 8256 node. */ 8257 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)); 8258 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) 8259 { 8260 TYPE_FIELDS (x) = TYPE_FIELDS (t); 8261 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t); 8262 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t); 8263 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t); 8264 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t); 8265 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE; 8266 } 8267 8268 /* If this was supposed to be a transparent union, but we can't 8269 make it one, warn and turn off the flag. */ 8270 if (TREE_CODE (t) == UNION_TYPE 8271 && TYPE_TRANSPARENT_AGGR (t) 8272 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))) 8273 { 8274 TYPE_TRANSPARENT_AGGR (t) = 0; 8275 warning_at (loc, 0, "union cannot be made transparent"); 8276 } 8277 8278 /* Update type location to the one of the definition, instead of e.g. 8279 a forward declaration. */ 8280 if (TYPE_STUB_DECL (t)) 8281 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc; 8282 8283 /* Finish debugging output for this type. */ 8284 rest_of_type_compilation (t, toplevel); 8285 8286 /* If this structure or union completes the type of any previous 8287 variable declaration, lay it out and output its rtl. */ 8288 for (x = incomplete_vars; x; x = TREE_CHAIN (x)) 8289 { 8290 tree decl = TREE_VALUE (x); 8291 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) 8292 layout_array_type (TREE_TYPE (decl)); 8293 if (TREE_CODE (decl) != TYPE_DECL) 8294 { 8295 layout_decl (decl, 0); 8296 if (c_dialect_objc ()) 8297 objc_check_decl (decl); 8298 rest_of_decl_compilation (decl, toplevel, 0); 8299 } 8300 } 8301 8302 /* If we're inside a function proper, i.e. not file-scope and not still 8303 parsing parameters, then arrange for the size of a variable sized type 8304 to be bound now. */ 8305 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE)) 8306 add_stmt (build_stmt (loc, 8307 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t))); 8308 8309 if (warn_cxx_compat) 8310 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc); 8311 8312 delete struct_parse_info; 8313 8314 struct_parse_info = enclosing_struct_parse_info; 8315 8316 /* If this struct is defined inside a struct, add it to 8317 struct_types. */ 8318 if (warn_cxx_compat 8319 && struct_parse_info != NULL 8320 && !in_sizeof && !in_typeof && !in_alignof) 8321 struct_parse_info->struct_types.safe_push (t); 8322 8323 return t; 8324 } 8325 8326 static struct { 8327 gt_pointer_operator new_value; 8328 void *cookie; 8329 } resort_data; 8330 8331 /* This routine compares two fields like field_decl_cmp but using the 8332 pointer operator in resort_data. */ 8333 8334 static int 8335 resort_field_decl_cmp (const void *x_p, const void *y_p) 8336 { 8337 const tree *const x = (const tree *) x_p; 8338 const tree *const y = (const tree *) y_p; 8339 8340 if (DECL_NAME (*x) == DECL_NAME (*y)) 8341 /* A nontype is "greater" than a type. */ 8342 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); 8343 if (DECL_NAME (*x) == NULL_TREE) 8344 return -1; 8345 if (DECL_NAME (*y) == NULL_TREE) 8346 return 1; 8347 { 8348 tree d1 = DECL_NAME (*x); 8349 tree d2 = DECL_NAME (*y); 8350 resort_data.new_value (&d1, resort_data.cookie); 8351 resort_data.new_value (&d2, resort_data.cookie); 8352 if (d1 < d2) 8353 return -1; 8354 } 8355 return 1; 8356 } 8357 8358 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */ 8359 8360 void 8361 resort_sorted_fields (void *obj, 8362 void * ARG_UNUSED (orig_obj), 8363 gt_pointer_operator new_value, 8364 void *cookie) 8365 { 8366 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj; 8367 resort_data.new_value = new_value; 8368 resort_data.cookie = cookie; 8369 qsort (&sf->elts[0], sf->len, sizeof (tree), 8370 resort_field_decl_cmp); 8371 } 8372 8373 /* Lay out the type T, and its element type, and so on. */ 8374 8375 static void 8376 layout_array_type (tree t) 8377 { 8378 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 8379 layout_array_type (TREE_TYPE (t)); 8380 layout_type (t); 8381 } 8382 8383 /* Begin compiling the definition of an enumeration type. 8384 NAME is its name (or null if anonymous). 8385 LOC is the enum's location. 8386 Returns the type object, as yet incomplete. 8387 Also records info about it so that build_enumerator 8388 may be used to declare the individual values as they are read. */ 8389 8390 tree 8391 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name) 8392 { 8393 tree enumtype = NULL_TREE; 8394 location_t enumloc = UNKNOWN_LOCATION; 8395 8396 /* If this is the real definition for a previous forward reference, 8397 fill in the contents in the same object that used to be the 8398 forward reference. */ 8399 8400 if (name != NULL_TREE) 8401 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc); 8402 8403 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE) 8404 { 8405 enumtype = make_node (ENUMERAL_TYPE); 8406 pushtag (loc, name, enumtype); 8407 } 8408 /* Update type location to the one of the definition, instead of e.g. 8409 a forward declaration. */ 8410 else if (TYPE_STUB_DECL (enumtype)) 8411 { 8412 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)); 8413 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc; 8414 } 8415 8416 if (C_TYPE_BEING_DEFINED (enumtype)) 8417 error_at (loc, "nested redefinition of %<enum %E%>", name); 8418 8419 C_TYPE_BEING_DEFINED (enumtype) = 1; 8420 8421 if (TYPE_VALUES (enumtype) != NULL_TREE) 8422 { 8423 /* This enum is a named one that has been declared already. */ 8424 error_at (loc, "redeclaration of %<enum %E%>", name); 8425 if (enumloc != UNKNOWN_LOCATION) 8426 inform (enumloc, "originally defined here"); 8427 8428 /* Completely replace its old definition. 8429 The old enumerators remain defined, however. */ 8430 TYPE_VALUES (enumtype) = NULL_TREE; 8431 } 8432 8433 the_enum->enum_next_value = integer_zero_node; 8434 the_enum->enum_overflow = 0; 8435 8436 if (flag_short_enums) 8437 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v)) 8438 TYPE_PACKED (v) = 1; 8439 8440 /* FIXME: This will issue a warning for a use of a type defined 8441 within sizeof in a statement expr. This is not terribly serious 8442 as C++ doesn't permit statement exprs within sizeof anyhow. */ 8443 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof)) 8444 warning_at (loc, OPT_Wc___compat, 8445 "defining type in %qs expression is invalid in C++", 8446 (in_sizeof 8447 ? "sizeof" 8448 : (in_typeof ? "typeof" : "alignof"))); 8449 8450 return enumtype; 8451 } 8452 8453 /* After processing and defining all the values of an enumeration type, 8454 install their decls in the enumeration type and finish it off. 8455 ENUMTYPE is the type object, VALUES a list of decl-value pairs, 8456 and ATTRIBUTES are the specified attributes. 8457 Returns ENUMTYPE. */ 8458 8459 tree 8460 finish_enum (tree enumtype, tree values, tree attributes) 8461 { 8462 tree pair, tem; 8463 tree minnode = NULL_TREE, maxnode = NULL_TREE; 8464 int precision; 8465 signop sign; 8466 bool toplevel = (file_scope == current_scope); 8467 struct lang_type *lt; 8468 8469 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 8470 8471 /* Calculate the maximum value of any enumerator in this type. */ 8472 8473 if (values == error_mark_node) 8474 minnode = maxnode = integer_zero_node; 8475 else 8476 { 8477 minnode = maxnode = TREE_VALUE (values); 8478 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair)) 8479 { 8480 tree value = TREE_VALUE (pair); 8481 if (tree_int_cst_lt (maxnode, value)) 8482 maxnode = value; 8483 if (tree_int_cst_lt (value, minnode)) 8484 minnode = value; 8485 } 8486 } 8487 8488 /* Construct the final type of this enumeration. It is the same 8489 as one of the integral types - the narrowest one that fits, except 8490 that normally we only go as narrow as int - and signed iff any of 8491 the values are negative. */ 8492 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED; 8493 precision = MAX (tree_int_cst_min_precision (minnode, sign), 8494 tree_int_cst_min_precision (maxnode, sign)); 8495 8496 /* If the precision of the type was specified with an attribute and it 8497 was too small, give an error. Otherwise, use it. */ 8498 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes)) 8499 { 8500 if (precision > TYPE_PRECISION (enumtype)) 8501 { 8502 TYPE_PRECISION (enumtype) = 0; 8503 error ("specified mode too small for enumeral values"); 8504 } 8505 else 8506 precision = TYPE_PRECISION (enumtype); 8507 } 8508 else 8509 TYPE_PRECISION (enumtype) = 0; 8510 8511 if (TYPE_PACKED (enumtype) 8512 || precision > TYPE_PRECISION (integer_type_node) 8513 || TYPE_PRECISION (enumtype)) 8514 { 8515 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0); 8516 if (tem == NULL) 8517 { 8518 warning (0, "enumeration values exceed range of largest integer"); 8519 tem = long_long_integer_type_node; 8520 } 8521 } 8522 else 8523 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node; 8524 8525 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem); 8526 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem); 8527 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem); 8528 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem)); 8529 TYPE_SIZE (enumtype) = NULL_TREE; 8530 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem); 8531 8532 layout_type (enumtype); 8533 8534 if (values != error_mark_node) 8535 { 8536 /* Change the type of the enumerators to be the enum type. We 8537 need to do this irrespective of the size of the enum, for 8538 proper type checking. Replace the DECL_INITIALs of the 8539 enumerators, and the value slots of the list, with copies 8540 that have the enum type; they cannot be modified in place 8541 because they may be shared (e.g. integer_zero_node) Finally, 8542 change the purpose slots to point to the names of the decls. */ 8543 for (pair = values; pair; pair = TREE_CHAIN (pair)) 8544 { 8545 tree enu = TREE_PURPOSE (pair); 8546 tree ini = DECL_INITIAL (enu); 8547 8548 TREE_TYPE (enu) = enumtype; 8549 8550 /* The ISO C Standard mandates enumerators to have type int, 8551 even though the underlying type of an enum type is 8552 unspecified. However, GCC allows enumerators of any 8553 integer type as an extensions. build_enumerator() 8554 converts any enumerators that fit in an int to type int, 8555 to avoid promotions to unsigned types when comparing 8556 integers with enumerators that fit in the int range. 8557 When -pedantic is given, build_enumerator() would have 8558 already warned about those that don't fit. Here we 8559 convert the rest to the enumerator type. */ 8560 if (TREE_TYPE (ini) != integer_type_node) 8561 ini = convert (enumtype, ini); 8562 8563 DECL_INITIAL (enu) = ini; 8564 TREE_PURPOSE (pair) = DECL_NAME (enu); 8565 TREE_VALUE (pair) = ini; 8566 } 8567 8568 TYPE_VALUES (enumtype) = values; 8569 } 8570 8571 /* Record the min/max values so that we can warn about bit-field 8572 enumerations that are too small for the values. */ 8573 lt = ggc_cleared_alloc<struct lang_type> (); 8574 lt->enum_min = minnode; 8575 lt->enum_max = maxnode; 8576 TYPE_LANG_SPECIFIC (enumtype) = lt; 8577 8578 /* Fix up all variant types of this enum type. */ 8579 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem)) 8580 { 8581 if (tem == enumtype) 8582 continue; 8583 TYPE_VALUES (tem) = TYPE_VALUES (enumtype); 8584 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype); 8585 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype); 8586 TYPE_SIZE (tem) = TYPE_SIZE (enumtype); 8587 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype); 8588 SET_TYPE_MODE (tem, TYPE_MODE (enumtype)); 8589 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype); 8590 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype)); 8591 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype); 8592 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype); 8593 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype); 8594 } 8595 8596 /* Finish debugging output for this type. */ 8597 rest_of_type_compilation (enumtype, toplevel); 8598 8599 /* If this enum is defined inside a struct, add it to 8600 struct_types. */ 8601 if (warn_cxx_compat 8602 && struct_parse_info != NULL 8603 && !in_sizeof && !in_typeof && !in_alignof) 8604 struct_parse_info->struct_types.safe_push (enumtype); 8605 8606 return enumtype; 8607 } 8608 8609 /* Build and install a CONST_DECL for one value of the 8610 current enumeration type (one that was begun with start_enum). 8611 DECL_LOC is the location of the enumerator. 8612 LOC is the location of the '=' operator if any, DECL_LOC otherwise. 8613 Return a tree-list containing the CONST_DECL and its value. 8614 Assignment of sequential values by default is handled here. */ 8615 8616 tree 8617 build_enumerator (location_t decl_loc, location_t loc, 8618 struct c_enum_contents *the_enum, tree name, tree value) 8619 { 8620 tree decl, type; 8621 8622 /* Validate and default VALUE. */ 8623 8624 if (value != NULL_TREE) 8625 { 8626 /* Don't issue more errors for error_mark_node (i.e. an 8627 undeclared identifier) - just ignore the value expression. */ 8628 if (value == error_mark_node) 8629 value = NULL_TREE; 8630 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))) 8631 { 8632 error_at (loc, "enumerator value for %qE is not an integer constant", 8633 name); 8634 value = NULL_TREE; 8635 } 8636 else 8637 { 8638 if (TREE_CODE (value) != INTEGER_CST) 8639 { 8640 value = c_fully_fold (value, false, NULL); 8641 if (TREE_CODE (value) == INTEGER_CST) 8642 pedwarn (loc, OPT_Wpedantic, 8643 "enumerator value for %qE is not an integer " 8644 "constant expression", name); 8645 } 8646 if (TREE_CODE (value) != INTEGER_CST) 8647 { 8648 error ("enumerator value for %qE is not an integer constant", 8649 name); 8650 value = NULL_TREE; 8651 } 8652 else 8653 { 8654 value = default_conversion (value); 8655 constant_expression_warning (value); 8656 } 8657 } 8658 } 8659 8660 /* Default based on previous value. */ 8661 /* It should no longer be possible to have NON_LVALUE_EXPR 8662 in the default. */ 8663 if (value == NULL_TREE) 8664 { 8665 value = the_enum->enum_next_value; 8666 if (the_enum->enum_overflow) 8667 error_at (loc, "overflow in enumeration values"); 8668 } 8669 /* Even though the underlying type of an enum is unspecified, the 8670 type of enumeration constants is explicitly defined as int 8671 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as 8672 an extension. */ 8673 else if (!int_fits_type_p (value, integer_type_node)) 8674 pedwarn (loc, OPT_Wpedantic, 8675 "ISO C restricts enumerator values to range of %<int%>"); 8676 8677 /* The ISO C Standard mandates enumerators to have type int, even 8678 though the underlying type of an enum type is unspecified. 8679 However, GCC allows enumerators of any integer type as an 8680 extensions. Here we convert any enumerators that fit in an int 8681 to type int, to avoid promotions to unsigned types when comparing 8682 integers with enumerators that fit in the int range. When 8683 -pedantic is given, we would have already warned about those that 8684 don't fit. We have to do this here rather than in finish_enum 8685 because this value may be used to define more enumerators. */ 8686 if (int_fits_type_p (value, integer_type_node)) 8687 value = convert (integer_type_node, value); 8688 8689 /* Set basis for default for next value. */ 8690 the_enum->enum_next_value 8691 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location), 8692 PLUS_EXPR, value, integer_one_node, false); 8693 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value); 8694 8695 /* Now create a declaration for the enum value name. */ 8696 8697 type = TREE_TYPE (value); 8698 type = c_common_type_for_size (MAX (TYPE_PRECISION (type), 8699 TYPE_PRECISION (integer_type_node)), 8700 (TYPE_PRECISION (type) 8701 >= TYPE_PRECISION (integer_type_node) 8702 && TYPE_UNSIGNED (type))); 8703 8704 decl = build_decl (decl_loc, CONST_DECL, name, type); 8705 DECL_INITIAL (decl) = convert (type, value); 8706 pushdecl (decl); 8707 8708 return tree_cons (decl, value, NULL_TREE); 8709 } 8710 8711 8712 /* Create the FUNCTION_DECL for a function definition. 8713 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of 8714 the declaration; they describe the function's name and the type it returns, 8715 but twisted together in a fashion that parallels the syntax of C. 8716 8717 This function creates a binding context for the function body 8718 as well as setting up the FUNCTION_DECL in current_function_decl. 8719 8720 Returns true on success. If the DECLARATOR is not suitable for a function 8721 (it defines a datum instead), we return false to report a parse error. */ 8722 8723 bool 8724 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator, 8725 tree attributes) 8726 { 8727 tree decl1, old_decl; 8728 tree restype, resdecl; 8729 location_t loc; 8730 8731 current_function_returns_value = 0; /* Assume, until we see it does. */ 8732 current_function_returns_null = 0; 8733 current_function_returns_abnormally = 0; 8734 warn_about_return_type = 0; 8735 c_switch_stack = NULL; 8736 8737 /* Indicate no valid break/continue context by setting these variables 8738 to some non-null, non-label value. We'll notice and emit the proper 8739 error message in c_finish_bc_stmt. */ 8740 c_break_label = c_cont_label = size_zero_node; 8741 8742 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL, 8743 &attributes, NULL, NULL, DEPRECATED_NORMAL); 8744 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1); 8745 8746 /* If the declarator is not suitable for a function definition, 8747 cause a syntax error. */ 8748 if (decl1 == NULL_TREE 8749 || TREE_CODE (decl1) != FUNCTION_DECL) 8750 return false; 8751 8752 loc = DECL_SOURCE_LOCATION (decl1); 8753 8754 c_decl_attributes (&decl1, attributes, 0); 8755 8756 if (DECL_DECLARED_INLINE_P (decl1) 8757 && DECL_UNINLINABLE (decl1) 8758 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1))) 8759 warning_at (loc, OPT_Wattributes, 8760 "inline function %qD given attribute noinline", 8761 decl1); 8762 8763 /* Handle gnu_inline attribute. */ 8764 if (declspecs->inline_p 8765 && !flag_gnu89_inline 8766 && TREE_CODE (decl1) == FUNCTION_DECL 8767 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1)) 8768 || current_function_decl)) 8769 { 8770 if (declspecs->storage_class != csc_static) 8771 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1); 8772 } 8773 8774 announce_function (decl1); 8775 8776 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1)))) 8777 { 8778 error_at (loc, "return type is an incomplete type"); 8779 /* Make it return void instead. */ 8780 TREE_TYPE (decl1) 8781 = build_function_type (void_type_node, 8782 TYPE_ARG_TYPES (TREE_TYPE (decl1))); 8783 } 8784 8785 if (warn_about_return_type) 8786 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int 8787 : (warn_return_type ? OPT_Wreturn_type 8788 : OPT_Wimplicit_int), 8789 "return type defaults to %<int%>"); 8790 8791 /* Make the init_value nonzero so pushdecl knows this is not tentative. 8792 error_mark_node is replaced below (in pop_scope) with the BLOCK. */ 8793 DECL_INITIAL (decl1) = error_mark_node; 8794 8795 /* A nested function is not global. */ 8796 if (current_function_decl != NULL_TREE) 8797 TREE_PUBLIC (decl1) = 0; 8798 8799 /* If this definition isn't a prototype and we had a prototype declaration 8800 before, copy the arg type info from that prototype. */ 8801 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope); 8802 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL) 8803 old_decl = NULL_TREE; 8804 current_function_prototype_locus = UNKNOWN_LOCATION; 8805 current_function_prototype_built_in = false; 8806 current_function_prototype_arg_types = NULL_TREE; 8807 if (!prototype_p (TREE_TYPE (decl1))) 8808 { 8809 if (old_decl != NULL_TREE 8810 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE 8811 && comptypes (TREE_TYPE (TREE_TYPE (decl1)), 8812 TREE_TYPE (TREE_TYPE (old_decl)))) 8813 { 8814 if (stdarg_p (TREE_TYPE (old_decl))) 8815 { 8816 warning_at (loc, 0, "%q+D defined as variadic function " 8817 "without prototype", decl1); 8818 locate_old_decl (old_decl); 8819 } 8820 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl), 8821 TREE_TYPE (decl1)); 8822 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl); 8823 current_function_prototype_built_in 8824 = C_DECL_BUILTIN_PROTOTYPE (old_decl); 8825 current_function_prototype_arg_types 8826 = TYPE_ARG_TYPES (TREE_TYPE (decl1)); 8827 } 8828 if (TREE_PUBLIC (decl1)) 8829 { 8830 /* If there is an external prototype declaration of this 8831 function, record its location but do not copy information 8832 to this decl. This may be an invisible declaration 8833 (built-in or in a scope which has finished) or simply 8834 have more refined argument types than any declaration 8835 found above. */ 8836 struct c_binding *b; 8837 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed) 8838 if (B_IN_SCOPE (b, external_scope)) 8839 break; 8840 if (b) 8841 { 8842 tree ext_decl, ext_type; 8843 ext_decl = b->decl; 8844 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl); 8845 if (TREE_CODE (ext_type) == FUNCTION_TYPE 8846 && comptypes (TREE_TYPE (TREE_TYPE (decl1)), 8847 TREE_TYPE (ext_type))) 8848 { 8849 current_function_prototype_locus 8850 = DECL_SOURCE_LOCATION (ext_decl); 8851 current_function_prototype_built_in 8852 = C_DECL_BUILTIN_PROTOTYPE (ext_decl); 8853 current_function_prototype_arg_types 8854 = TYPE_ARG_TYPES (ext_type); 8855 } 8856 } 8857 } 8858 } 8859 8860 /* Optionally warn of old-fashioned def with no previous prototype. */ 8861 if (warn_strict_prototypes 8862 && old_decl != error_mark_node 8863 && !prototype_p (TREE_TYPE (decl1)) 8864 && C_DECL_ISNT_PROTOTYPE (old_decl)) 8865 warning_at (loc, OPT_Wstrict_prototypes, 8866 "function declaration isn%'t a prototype"); 8867 /* Optionally warn of any global def with no previous prototype. */ 8868 else if (warn_missing_prototypes 8869 && old_decl != error_mark_node 8870 && TREE_PUBLIC (decl1) 8871 && !MAIN_NAME_P (DECL_NAME (decl1)) 8872 && C_DECL_ISNT_PROTOTYPE (old_decl) 8873 && !DECL_DECLARED_INLINE_P (decl1)) 8874 warning_at (loc, OPT_Wmissing_prototypes, 8875 "no previous prototype for %qD", decl1); 8876 /* Optionally warn of any def with no previous prototype 8877 if the function has already been used. */ 8878 else if (warn_missing_prototypes 8879 && old_decl != NULL_TREE 8880 && old_decl != error_mark_node 8881 && TREE_USED (old_decl) 8882 && !prototype_p (TREE_TYPE (old_decl))) 8883 warning_at (loc, OPT_Wmissing_prototypes, 8884 "%qD was used with no prototype before its definition", decl1); 8885 /* Optionally warn of any global def with no previous declaration. */ 8886 else if (warn_missing_declarations 8887 && TREE_PUBLIC (decl1) 8888 && old_decl == NULL_TREE 8889 && !MAIN_NAME_P (DECL_NAME (decl1)) 8890 && !DECL_DECLARED_INLINE_P (decl1)) 8891 warning_at (loc, OPT_Wmissing_declarations, 8892 "no previous declaration for %qD", 8893 decl1); 8894 /* Optionally warn of any def with no previous declaration 8895 if the function has already been used. */ 8896 else if (warn_missing_declarations 8897 && old_decl != NULL_TREE 8898 && old_decl != error_mark_node 8899 && TREE_USED (old_decl) 8900 && C_DECL_IMPLICIT (old_decl)) 8901 warning_at (loc, OPT_Wmissing_declarations, 8902 "%qD was used with no declaration before its definition", decl1); 8903 8904 /* This function exists in static storage. 8905 (This does not mean `static' in the C sense!) */ 8906 TREE_STATIC (decl1) = 1; 8907 8908 /* This is the earliest point at which we might know the assembler 8909 name of the function. Thus, if it's set before this, die horribly. */ 8910 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1)); 8911 8912 /* If #pragma weak was used, mark the decl weak now. */ 8913 if (current_scope == file_scope) 8914 maybe_apply_pragma_weak (decl1); 8915 8916 /* Warn for unlikely, improbable, or stupid declarations of `main'. */ 8917 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1))) 8918 { 8919 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1))) 8920 != integer_type_node) 8921 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1); 8922 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1)))) 8923 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD", 8924 decl1); 8925 8926 check_main_parameter_types (decl1); 8927 8928 if (!TREE_PUBLIC (decl1)) 8929 pedwarn (loc, OPT_Wmain, 8930 "%qD is normally a non-static function", decl1); 8931 } 8932 8933 /* Record the decl so that the function name is defined. 8934 If we already have a decl for this name, and it is a FUNCTION_DECL, 8935 use the old decl. */ 8936 8937 current_function_decl = pushdecl (decl1); 8938 8939 push_scope (); 8940 declare_parm_level (); 8941 8942 restype = TREE_TYPE (TREE_TYPE (current_function_decl)); 8943 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype); 8944 DECL_ARTIFICIAL (resdecl) = 1; 8945 DECL_IGNORED_P (resdecl) = 1; 8946 DECL_RESULT (current_function_decl) = resdecl; 8947 8948 start_fname_decls (); 8949 8950 return true; 8951 } 8952 8953 /* Subroutine of store_parm_decls which handles new-style function 8954 definitions (prototype format). The parms already have decls, so we 8955 need only record them as in effect and complain if any redundant 8956 old-style parm decls were written. */ 8957 static void 8958 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info) 8959 { 8960 tree decl; 8961 c_arg_tag *tag; 8962 unsigned ix; 8963 8964 if (current_scope->bindings) 8965 { 8966 error_at (DECL_SOURCE_LOCATION (fndecl), 8967 "old-style parameter declarations in prototyped " 8968 "function definition"); 8969 8970 /* Get rid of the old-style declarations. */ 8971 pop_scope (); 8972 push_scope (); 8973 } 8974 /* Don't issue this warning for nested functions, and don't issue this 8975 warning if we got here because ARG_INFO_TYPES was error_mark_node 8976 (this happens when a function definition has just an ellipsis in 8977 its parameter list). */ 8978 else if (!in_system_header_at (input_location) 8979 && !current_function_scope 8980 && arg_info->types != error_mark_node) 8981 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional, 8982 "traditional C rejects ISO C style function definitions"); 8983 8984 /* Now make all the parameter declarations visible in the function body. 8985 We can bypass most of the grunt work of pushdecl. */ 8986 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl)) 8987 { 8988 DECL_CONTEXT (decl) = current_function_decl; 8989 if (DECL_NAME (decl)) 8990 { 8991 bind (DECL_NAME (decl), decl, current_scope, 8992 /*invisible=*/false, /*nested=*/false, 8993 UNKNOWN_LOCATION); 8994 if (!TREE_USED (decl)) 8995 warn_if_shadowing (decl); 8996 } 8997 else 8998 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted"); 8999 } 9000 9001 /* Record the parameter list in the function declaration. */ 9002 DECL_ARGUMENTS (fndecl) = arg_info->parms; 9003 9004 /* Now make all the ancillary declarations visible, likewise. */ 9005 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl)) 9006 { 9007 DECL_CONTEXT (decl) = current_function_decl; 9008 if (DECL_NAME (decl)) 9009 bind (DECL_NAME (decl), decl, current_scope, 9010 /*invisible=*/false, 9011 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL), 9012 UNKNOWN_LOCATION); 9013 } 9014 9015 /* And all the tag declarations. */ 9016 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag) 9017 if (tag->id) 9018 bind (tag->id, tag->type, current_scope, 9019 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION); 9020 } 9021 9022 /* Subroutine of store_parm_decls which handles old-style function 9023 definitions (separate parameter list and declarations). */ 9024 9025 static void 9026 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info) 9027 { 9028 struct c_binding *b; 9029 tree parm, decl, last; 9030 tree parmids = arg_info->parms; 9031 hash_set<tree> seen_args; 9032 9033 if (!in_system_header_at (input_location)) 9034 warning_at (DECL_SOURCE_LOCATION (fndecl), 9035 OPT_Wold_style_definition, "old-style function definition"); 9036 9037 /* Match each formal parameter name with its declaration. Save each 9038 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */ 9039 for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 9040 { 9041 if (TREE_VALUE (parm) == NULL_TREE) 9042 { 9043 error_at (DECL_SOURCE_LOCATION (fndecl), 9044 "parameter name missing from parameter list"); 9045 TREE_PURPOSE (parm) = NULL_TREE; 9046 continue; 9047 } 9048 9049 b = I_SYMBOL_BINDING (TREE_VALUE (parm)); 9050 if (b && B_IN_CURRENT_SCOPE (b)) 9051 { 9052 decl = b->decl; 9053 /* Skip erroneous parameters. */ 9054 if (decl == error_mark_node) 9055 continue; 9056 /* If we got something other than a PARM_DECL it is an error. */ 9057 if (TREE_CODE (decl) != PARM_DECL) 9058 { 9059 error_at (DECL_SOURCE_LOCATION (decl), 9060 "%qD declared as a non-parameter", decl); 9061 continue; 9062 } 9063 /* If the declaration is already marked, we have a duplicate 9064 name. Complain and ignore the duplicate. */ 9065 else if (seen_args.contains (decl)) 9066 { 9067 error_at (DECL_SOURCE_LOCATION (decl), 9068 "multiple parameters named %qD", decl); 9069 TREE_PURPOSE (parm) = NULL_TREE; 9070 continue; 9071 } 9072 /* If the declaration says "void", complain and turn it into 9073 an int. */ 9074 else if (VOID_TYPE_P (TREE_TYPE (decl))) 9075 { 9076 error_at (DECL_SOURCE_LOCATION (decl), 9077 "parameter %qD declared with void type", decl); 9078 TREE_TYPE (decl) = integer_type_node; 9079 DECL_ARG_TYPE (decl) = integer_type_node; 9080 layout_decl (decl, 0); 9081 } 9082 warn_if_shadowing (decl); 9083 } 9084 /* If no declaration found, default to int. */ 9085 else 9086 { 9087 /* FIXME diagnostics: This should be the location of the argument, 9088 not the FNDECL. E.g., for an old-style declaration 9089 9090 int f10(v) { blah; } 9091 9092 We should use the location of the V, not the F10. 9093 Unfortunately, the V is an IDENTIFIER_NODE which has no 9094 location. In the future we need locations for c_arg_info 9095 entries. 9096 9097 See gcc.dg/Wshadow-3.c for an example of this problem. */ 9098 decl = build_decl (DECL_SOURCE_LOCATION (fndecl), 9099 PARM_DECL, TREE_VALUE (parm), integer_type_node); 9100 DECL_ARG_TYPE (decl) = TREE_TYPE (decl); 9101 pushdecl (decl); 9102 warn_if_shadowing (decl); 9103 9104 if (flag_isoc99) 9105 pedwarn (DECL_SOURCE_LOCATION (decl), 9106 OPT_Wimplicit_int, "type of %qD defaults to %<int%>", 9107 decl); 9108 else 9109 warning_at (DECL_SOURCE_LOCATION (decl), 9110 OPT_Wmissing_parameter_type, 9111 "type of %qD defaults to %<int%>", decl); 9112 } 9113 9114 TREE_PURPOSE (parm) = decl; 9115 seen_args.add (decl); 9116 } 9117 9118 /* Now examine the parms chain for incomplete declarations 9119 and declarations with no corresponding names. */ 9120 9121 for (b = current_scope->bindings; b; b = b->prev) 9122 { 9123 parm = b->decl; 9124 if (TREE_CODE (parm) != PARM_DECL) 9125 continue; 9126 9127 if (TREE_TYPE (parm) != error_mark_node 9128 && !COMPLETE_TYPE_P (TREE_TYPE (parm))) 9129 { 9130 error_at (DECL_SOURCE_LOCATION (parm), 9131 "parameter %qD has incomplete type", parm); 9132 TREE_TYPE (parm) = error_mark_node; 9133 } 9134 9135 if (!seen_args.contains (parm)) 9136 { 9137 error_at (DECL_SOURCE_LOCATION (parm), 9138 "declaration for parameter %qD but no such parameter", 9139 parm); 9140 9141 /* Pretend the parameter was not missing. 9142 This gets us to a standard state and minimizes 9143 further error messages. */ 9144 parmids = chainon (parmids, tree_cons (parm, 0, 0)); 9145 } 9146 } 9147 9148 /* Chain the declarations together in the order of the list of 9149 names. Store that chain in the function decl, replacing the 9150 list of names. Update the current scope to match. */ 9151 DECL_ARGUMENTS (fndecl) = NULL_TREE; 9152 9153 for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 9154 if (TREE_PURPOSE (parm)) 9155 break; 9156 if (parm && TREE_PURPOSE (parm)) 9157 { 9158 last = TREE_PURPOSE (parm); 9159 DECL_ARGUMENTS (fndecl) = last; 9160 9161 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm)) 9162 if (TREE_PURPOSE (parm)) 9163 { 9164 DECL_CHAIN (last) = TREE_PURPOSE (parm); 9165 last = TREE_PURPOSE (parm); 9166 } 9167 DECL_CHAIN (last) = NULL_TREE; 9168 } 9169 9170 /* If there was a previous prototype, 9171 set the DECL_ARG_TYPE of each argument according to 9172 the type previously specified, and report any mismatches. */ 9173 9174 if (current_function_prototype_arg_types) 9175 { 9176 tree type; 9177 for (parm = DECL_ARGUMENTS (fndecl), 9178 type = current_function_prototype_arg_types; 9179 parm || (type != NULL_TREE 9180 && TREE_VALUE (type) != error_mark_node 9181 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node); 9182 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type)) 9183 { 9184 if (parm == NULL_TREE 9185 || type == NULL_TREE 9186 || (TREE_VALUE (type) != error_mark_node 9187 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)) 9188 { 9189 if (current_function_prototype_built_in) 9190 warning_at (DECL_SOURCE_LOCATION (fndecl), 9191 0, "number of arguments doesn%'t match " 9192 "built-in prototype"); 9193 else 9194 { 9195 /* FIXME diagnostics: This should be the location of 9196 FNDECL, but there is bug when a prototype is 9197 declared inside function context, but defined 9198 outside of it (e.g., gcc.dg/pr15698-2.c). In 9199 which case FNDECL gets the location of the 9200 prototype, not the definition. */ 9201 error_at (input_location, 9202 "number of arguments doesn%'t match prototype"); 9203 9204 error_at (current_function_prototype_locus, 9205 "prototype declaration"); 9206 } 9207 break; 9208 } 9209 /* Type for passing arg must be consistent with that 9210 declared for the arg. ISO C says we take the unqualified 9211 type for parameters declared with qualified type. */ 9212 if (TREE_TYPE (parm) != error_mark_node 9213 && TREE_VALUE (type) != error_mark_node 9214 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm)) 9215 != TYPE_ATOMIC (TREE_VALUE (type))) 9216 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)), 9217 TYPE_MAIN_VARIANT (TREE_VALUE (type))))) 9218 { 9219 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm)) 9220 == TYPE_ATOMIC (TREE_VALUE (type))) 9221 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) 9222 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))) 9223 { 9224 /* Adjust argument to match prototype. E.g. a previous 9225 `int foo(float);' prototype causes 9226 `int foo(x) float x; {...}' to be treated like 9227 `int foo(float x) {...}'. This is particularly 9228 useful for argument types like uid_t. */ 9229 DECL_ARG_TYPE (parm) = TREE_TYPE (parm); 9230 9231 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl)) 9232 && INTEGRAL_TYPE_P (TREE_TYPE (parm)) 9233 && (TYPE_PRECISION (TREE_TYPE (parm)) 9234 < TYPE_PRECISION (integer_type_node))) 9235 DECL_ARG_TYPE (parm) 9236 = c_type_promotes_to (TREE_TYPE (parm)); 9237 9238 /* ??? Is it possible to get here with a 9239 built-in prototype or will it always have 9240 been diagnosed as conflicting with an 9241 old-style definition and discarded? */ 9242 if (current_function_prototype_built_in) 9243 warning_at (DECL_SOURCE_LOCATION (parm), 9244 OPT_Wpedantic, "promoted argument %qD " 9245 "doesn%'t match built-in prototype", parm); 9246 else 9247 { 9248 pedwarn (DECL_SOURCE_LOCATION (parm), 9249 OPT_Wpedantic, "promoted argument %qD " 9250 "doesn%'t match prototype", parm); 9251 pedwarn (current_function_prototype_locus, OPT_Wpedantic, 9252 "prototype declaration"); 9253 } 9254 } 9255 else 9256 { 9257 if (current_function_prototype_built_in) 9258 warning_at (DECL_SOURCE_LOCATION (parm), 9259 0, "argument %qD doesn%'t match " 9260 "built-in prototype", parm); 9261 else 9262 { 9263 error_at (DECL_SOURCE_LOCATION (parm), 9264 "argument %qD doesn%'t match prototype", parm); 9265 error_at (current_function_prototype_locus, 9266 "prototype declaration"); 9267 } 9268 } 9269 } 9270 } 9271 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE; 9272 } 9273 9274 /* Otherwise, create a prototype that would match. */ 9275 9276 else 9277 { 9278 tree actual = NULL_TREE, last = NULL_TREE, type; 9279 9280 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm)) 9281 { 9282 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE); 9283 if (last) 9284 TREE_CHAIN (last) = type; 9285 else 9286 actual = type; 9287 last = type; 9288 } 9289 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE); 9290 if (last) 9291 TREE_CHAIN (last) = type; 9292 else 9293 actual = type; 9294 9295 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES 9296 of the type of this function, but we need to avoid having this 9297 affect the types of other similarly-typed functions, so we must 9298 first force the generation of an identical (but separate) type 9299 node for the relevant function type. The new node we create 9300 will be a variant of the main variant of the original function 9301 type. */ 9302 9303 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl)); 9304 9305 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual; 9306 } 9307 } 9308 9309 /* Store parameter declarations passed in ARG_INFO into the current 9310 function declaration. */ 9311 9312 void 9313 store_parm_decls_from (struct c_arg_info *arg_info) 9314 { 9315 current_function_arg_info = arg_info; 9316 store_parm_decls (); 9317 } 9318 9319 /* Called by walk_tree to look for and update context-less labels. */ 9320 9321 static tree 9322 set_labels_context_r (tree *tp, int *walk_subtrees, void *data) 9323 { 9324 if (TREE_CODE (*tp) == LABEL_EXPR 9325 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE) 9326 { 9327 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data); 9328 *walk_subtrees = 0; 9329 } 9330 9331 return NULL_TREE; 9332 } 9333 9334 /* Store the parameter declarations into the current function declaration. 9335 This is called after parsing the parameter declarations, before 9336 digesting the body of the function. 9337 9338 For an old-style definition, construct a prototype out of the old-style 9339 parameter declarations and inject it into the function's type. */ 9340 9341 void 9342 store_parm_decls (void) 9343 { 9344 tree fndecl = current_function_decl; 9345 bool proto; 9346 9347 /* The argument information block for FNDECL. */ 9348 struct c_arg_info *arg_info = current_function_arg_info; 9349 current_function_arg_info = 0; 9350 9351 /* True if this definition is written with a prototype. Note: 9352 despite C99 6.7.5.3p14, we can *not* treat an empty argument 9353 list in a function definition as equivalent to (void) -- an 9354 empty argument list specifies the function has no parameters, 9355 but only (void) sets up a prototype for future calls. */ 9356 proto = arg_info->types != 0; 9357 9358 if (proto) 9359 store_parm_decls_newstyle (fndecl, arg_info); 9360 else 9361 store_parm_decls_oldstyle (fndecl, arg_info); 9362 9363 /* The next call to push_scope will be a function body. */ 9364 9365 next_is_function_body = true; 9366 9367 /* Write a record describing this function definition to the prototypes 9368 file (if requested). */ 9369 9370 gen_aux_info_record (fndecl, 1, 0, proto); 9371 9372 /* Initialize the RTL code for the function. */ 9373 allocate_struct_function (fndecl, false); 9374 9375 if (warn_unused_local_typedefs) 9376 cfun->language = ggc_cleared_alloc<language_function> (); 9377 9378 /* Begin the statement tree for this function. */ 9379 DECL_SAVED_TREE (fndecl) = push_stmt_list (); 9380 9381 /* ??? Insert the contents of the pending sizes list into the function 9382 to be evaluated. The only reason left to have this is 9383 void foo(int n, int array[n++]) 9384 because we throw away the array type in favor of a pointer type, and 9385 thus won't naturally see the SAVE_EXPR containing the increment. All 9386 other pending sizes would be handled by gimplify_parameters. */ 9387 if (arg_info->pending_sizes) 9388 { 9389 /* In very special circumstances, e.g. for code like 9390 _Atomic int i = 5; 9391 void f (int a[i += 2]) {} 9392 we need to execute the atomic assignment on function entry. 9393 But in this case, it is not just a straight store, it has the 9394 op= form, which means that build_atomic_assign has generated 9395 gotos, labels, etc. Because at that time the function decl 9396 for F has not been created yet, those labels do not have any 9397 function context. But we have the fndecl now, so update the 9398 labels accordingly. gimplify_expr would crash otherwise. */ 9399 walk_tree_without_duplicates (&arg_info->pending_sizes, 9400 set_labels_context_r, fndecl); 9401 add_stmt (arg_info->pending_sizes); 9402 } 9403 } 9404 9405 /* Store PARM_DECLs in PARMS into scope temporarily. Used for 9406 c_finish_omp_declare_simd for function prototypes. No diagnostics 9407 should be done. */ 9408 9409 void 9410 temp_store_parm_decls (tree fndecl, tree parms) 9411 { 9412 push_scope (); 9413 for (tree p = parms; p; p = DECL_CHAIN (p)) 9414 { 9415 DECL_CONTEXT (p) = fndecl; 9416 if (DECL_NAME (p)) 9417 bind (DECL_NAME (p), p, current_scope, 9418 /*invisible=*/false, /*nested=*/false, 9419 UNKNOWN_LOCATION); 9420 } 9421 } 9422 9423 /* Undo what temp_store_parm_decls did. */ 9424 9425 void 9426 temp_pop_parm_decls (void) 9427 { 9428 /* Clear all bindings in this temporary scope, so that 9429 pop_scope doesn't create a BLOCK. */ 9430 struct c_binding *b = current_scope->bindings; 9431 current_scope->bindings = NULL; 9432 for (; b; b = free_binding_and_advance (b)) 9433 { 9434 gcc_assert (TREE_CODE (b->decl) == PARM_DECL 9435 || b->decl == error_mark_node); 9436 gcc_assert (I_SYMBOL_BINDING (b->id) == b); 9437 I_SYMBOL_BINDING (b->id) = b->shadowed; 9438 if (b->shadowed && b->shadowed->u.type) 9439 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type; 9440 } 9441 pop_scope (); 9442 } 9443 9444 9445 /* Finish up a function declaration and compile that function 9446 all the way to assembler language output. Then free the storage 9447 for the function definition. 9448 9449 This is called after parsing the body of the function definition. */ 9450 9451 void 9452 finish_function (void) 9453 { 9454 tree fndecl = current_function_decl; 9455 9456 if (c_dialect_objc ()) 9457 objc_finish_function (); 9458 9459 if (TREE_CODE (fndecl) == FUNCTION_DECL 9460 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))) 9461 { 9462 tree args = DECL_ARGUMENTS (fndecl); 9463 for (; args; args = DECL_CHAIN (args)) 9464 { 9465 tree type = TREE_TYPE (args); 9466 if (INTEGRAL_TYPE_P (type) 9467 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 9468 DECL_ARG_TYPE (args) = c_type_promotes_to (type); 9469 } 9470 } 9471 9472 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) 9473 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; 9474 9475 /* Must mark the RESULT_DECL as being in this function. */ 9476 9477 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node) 9478 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; 9479 9480 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted 9481 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))) 9482 == integer_type_node && flag_isoc99) 9483 { 9484 /* Hack. We don't want the middle-end to warn that this return 9485 is unreachable, so we mark its location as special. Using 9486 UNKNOWN_LOCATION has the problem that it gets clobbered in 9487 annotate_one_with_locus. A cleaner solution might be to 9488 ensure ! should_carry_locus_p (stmt), but that needs a flag. 9489 */ 9490 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE); 9491 } 9492 9493 /* Tie off the statement tree for this function. */ 9494 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl)); 9495 9496 finish_fname_decls (); 9497 9498 /* Complain if there's just no return statement. */ 9499 if (warn_return_type 9500 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE 9501 && !current_function_returns_value && !current_function_returns_null 9502 /* Don't complain if we are no-return. */ 9503 && !current_function_returns_abnormally 9504 /* Don't complain if we are declared noreturn. */ 9505 && !TREE_THIS_VOLATILE (fndecl) 9506 /* Don't warn for main(). */ 9507 && !MAIN_NAME_P (DECL_NAME (fndecl)) 9508 /* Or if they didn't actually specify a return type. */ 9509 && !C_FUNCTION_IMPLICIT_INT (fndecl) 9510 /* Normally, with -Wreturn-type, flow will complain, but we might 9511 optimize out static functions. */ 9512 && !TREE_PUBLIC (fndecl) 9513 && targetm.warn_func_return (fndecl)) 9514 { 9515 warning (OPT_Wreturn_type, 9516 "no return statement in function returning non-void"); 9517 TREE_NO_WARNING (fndecl) = 1; 9518 } 9519 9520 /* Complain about parameters that are only set, but never otherwise used. */ 9521 if (warn_unused_but_set_parameter) 9522 { 9523 tree decl; 9524 9525 for (decl = DECL_ARGUMENTS (fndecl); 9526 decl; 9527 decl = DECL_CHAIN (decl)) 9528 if (TREE_USED (decl) 9529 && TREE_CODE (decl) == PARM_DECL 9530 && !DECL_READ_P (decl) 9531 && DECL_NAME (decl) 9532 && !DECL_ARTIFICIAL (decl) 9533 && !TREE_NO_WARNING (decl)) 9534 warning_at (DECL_SOURCE_LOCATION (decl), 9535 OPT_Wunused_but_set_parameter, 9536 "parameter %qD set but not used", decl); 9537 } 9538 9539 /* Complain about locally defined typedefs that are not used in this 9540 function. */ 9541 maybe_warn_unused_local_typedefs (); 9542 9543 /* Possibly warn about unused parameters. */ 9544 if (warn_unused_parameter) 9545 do_warn_unused_parameter (fndecl); 9546 9547 /* Store the end of the function, so that we get good line number 9548 info for the epilogue. */ 9549 cfun->function_end_locus = input_location; 9550 9551 /* Finalize the ELF visibility for the function. */ 9552 c_determine_visibility (fndecl); 9553 9554 /* For GNU C extern inline functions disregard inline limits. */ 9555 if (DECL_EXTERNAL (fndecl) 9556 && DECL_DECLARED_INLINE_P (fndecl) 9557 && (flag_gnu89_inline 9558 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl)))) 9559 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1; 9560 9561 /* Genericize before inlining. Delay genericizing nested functions 9562 until their parent function is genericized. Since finalizing 9563 requires GENERIC, delay that as well. */ 9564 9565 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node 9566 && !undef_nested_function) 9567 { 9568 if (!decl_function_context (fndecl)) 9569 { 9570 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl); 9571 c_genericize (fndecl); 9572 9573 /* ??? Objc emits functions after finalizing the compilation unit. 9574 This should be cleaned up later and this conditional removed. */ 9575 if (symtab->global_info_ready) 9576 { 9577 cgraph_node::add_new_function (fndecl, false); 9578 return; 9579 } 9580 cgraph_node::finalize_function (fndecl, false); 9581 } 9582 else 9583 { 9584 /* Register this function with cgraph just far enough to get it 9585 added to our parent's nested function list. Handy, since the 9586 C front end doesn't have such a list. */ 9587 (void) cgraph_node::get_create (fndecl); 9588 } 9589 } 9590 9591 if (!decl_function_context (fndecl)) 9592 undef_nested_function = false; 9593 9594 if (cfun->language != NULL) 9595 { 9596 ggc_free (cfun->language); 9597 cfun->language = NULL; 9598 } 9599 9600 /* We're leaving the context of this function, so zap cfun. 9601 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in 9602 tree_rest_of_compilation. */ 9603 set_cfun (NULL); 9604 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl); 9605 current_function_decl = NULL; 9606 } 9607 9608 /* Check the declarations given in a for-loop for satisfying the C99 9609 constraints. If exactly one such decl is found, return it. LOC is 9610 the location of the opening parenthesis of the for loop. The last 9611 parameter allows you to control the "for loop initial declarations 9612 are only allowed in C99 mode". Normally, you should pass 9613 flag_isoc99 as that parameter. But in some cases (Objective-C 9614 foreach loop, for example) we want to run the checks in this 9615 function even if not in C99 mode, so we allow the caller to turn 9616 off the error about not being in C99 mode. 9617 */ 9618 9619 tree 9620 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error) 9621 { 9622 struct c_binding *b; 9623 tree one_decl = NULL_TREE; 9624 int n_decls = 0; 9625 9626 if (!turn_off_iso_c99_error) 9627 { 9628 static bool hint = true; 9629 /* If we get here, declarations have been used in a for loop without 9630 the C99 for loop scope. This doesn't make much sense, so don't 9631 allow it. */ 9632 error_at (loc, "%<for%> loop initial declarations " 9633 "are only allowed in C99 or C11 mode"); 9634 if (hint) 9635 { 9636 inform (loc, 9637 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 " 9638 "to compile your code"); 9639 hint = false; 9640 } 9641 return NULL_TREE; 9642 } 9643 /* C99 subclause 6.8.5 paragraph 3: 9644 9645 [#3] The declaration part of a for statement shall only 9646 declare identifiers for objects having storage class auto or 9647 register. 9648 9649 It isn't clear whether, in this sentence, "identifiers" binds to 9650 "shall only declare" or to "objects" - that is, whether all identifiers 9651 declared must be identifiers for objects, or whether the restriction 9652 only applies to those that are. (A question on this in comp.std.c 9653 in November 2000 received no answer.) We implement the strictest 9654 interpretation, to avoid creating an extension which later causes 9655 problems. */ 9656 9657 for (b = current_scope->bindings; b; b = b->prev) 9658 { 9659 tree id = b->id; 9660 tree decl = b->decl; 9661 9662 if (!id) 9663 continue; 9664 9665 switch (TREE_CODE (decl)) 9666 { 9667 case VAR_DECL: 9668 { 9669 location_t decl_loc = DECL_SOURCE_LOCATION (decl); 9670 if (TREE_STATIC (decl)) 9671 error_at (decl_loc, 9672 "declaration of static variable %qD in %<for%> loop " 9673 "initial declaration", decl); 9674 else if (DECL_EXTERNAL (decl)) 9675 error_at (decl_loc, 9676 "declaration of %<extern%> variable %qD in %<for%> loop " 9677 "initial declaration", decl); 9678 } 9679 break; 9680 9681 case RECORD_TYPE: 9682 error_at (loc, 9683 "%<struct %E%> declared in %<for%> loop initial " 9684 "declaration", id); 9685 break; 9686 case UNION_TYPE: 9687 error_at (loc, 9688 "%<union %E%> declared in %<for%> loop initial declaration", 9689 id); 9690 break; 9691 case ENUMERAL_TYPE: 9692 error_at (loc, "%<enum %E%> declared in %<for%> loop " 9693 "initial declaration", id); 9694 break; 9695 default: 9696 error_at (loc, "declaration of non-variable " 9697 "%qD in %<for%> loop initial declaration", decl); 9698 } 9699 9700 n_decls++; 9701 one_decl = decl; 9702 } 9703 9704 return n_decls == 1 ? one_decl : NULL_TREE; 9705 } 9706 9707 /* Save and reinitialize the variables 9708 used during compilation of a C function. */ 9709 9710 void 9711 c_push_function_context (void) 9712 { 9713 struct language_function *p = cfun->language; 9714 /* cfun->language might have been already allocated by the use of 9715 -Wunused-local-typedefs. In that case, just re-use it. */ 9716 if (p == NULL) 9717 cfun->language = p = ggc_cleared_alloc<language_function> (); 9718 9719 p->base.x_stmt_tree = c_stmt_tree; 9720 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list); 9721 p->x_break_label = c_break_label; 9722 p->x_cont_label = c_cont_label; 9723 p->x_switch_stack = c_switch_stack; 9724 p->arg_info = current_function_arg_info; 9725 p->returns_value = current_function_returns_value; 9726 p->returns_null = current_function_returns_null; 9727 p->returns_abnormally = current_function_returns_abnormally; 9728 p->warn_about_return_type = warn_about_return_type; 9729 9730 push_function_context (); 9731 } 9732 9733 /* Restore the variables used during compilation of a C function. */ 9734 9735 void 9736 c_pop_function_context (void) 9737 { 9738 struct language_function *p; 9739 9740 pop_function_context (); 9741 p = cfun->language; 9742 9743 /* When -Wunused-local-typedefs is in effect, cfun->languages is 9744 used to store data throughout the life time of the current cfun, 9745 So don't deallocate it. */ 9746 if (!warn_unused_local_typedefs) 9747 cfun->language = NULL; 9748 9749 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0 9750 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE) 9751 { 9752 /* Stop pointing to the local nodes about to be freed. */ 9753 /* But DECL_INITIAL must remain nonzero so we know this 9754 was an actual function definition. */ 9755 DECL_INITIAL (current_function_decl) = error_mark_node; 9756 DECL_ARGUMENTS (current_function_decl) = NULL_TREE; 9757 } 9758 9759 c_stmt_tree = p->base.x_stmt_tree; 9760 p->base.x_stmt_tree.x_cur_stmt_list = NULL; 9761 c_break_label = p->x_break_label; 9762 c_cont_label = p->x_cont_label; 9763 c_switch_stack = p->x_switch_stack; 9764 current_function_arg_info = p->arg_info; 9765 current_function_returns_value = p->returns_value; 9766 current_function_returns_null = p->returns_null; 9767 current_function_returns_abnormally = p->returns_abnormally; 9768 warn_about_return_type = p->warn_about_return_type; 9769 } 9770 9771 /* The functions below are required for functionality of doing 9772 function at once processing in the C front end. Currently these 9773 functions are not called from anywhere in the C front end, but as 9774 these changes continue, that will change. */ 9775 9776 /* Returns the stmt_tree (if any) to which statements are currently 9777 being added. If there is no active statement-tree, NULL is 9778 returned. */ 9779 9780 stmt_tree 9781 current_stmt_tree (void) 9782 { 9783 return &c_stmt_tree; 9784 } 9785 9786 /* Return the global value of T as a symbol. */ 9787 9788 tree 9789 identifier_global_value (tree t) 9790 { 9791 struct c_binding *b; 9792 9793 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed) 9794 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b)) 9795 return b->decl; 9796 9797 return NULL_TREE; 9798 } 9799 9800 /* In C, the only C-linkage public declaration is at file scope. */ 9801 9802 tree 9803 c_linkage_bindings (tree name) 9804 { 9805 return identifier_global_value (name); 9806 } 9807 9808 /* Record a builtin type for C. If NAME is non-NULL, it is the name used; 9809 otherwise the name is found in ridpointers from RID_INDEX. */ 9810 9811 void 9812 record_builtin_type (enum rid rid_index, const char *name, tree type) 9813 { 9814 tree id, decl; 9815 if (name == 0) 9816 id = ridpointers[(int) rid_index]; 9817 else 9818 id = get_identifier (name); 9819 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type); 9820 pushdecl (decl); 9821 if (debug_hooks->type_decl) 9822 debug_hooks->type_decl (decl, false); 9823 } 9824 9825 /* Build the void_list_node (void_type_node having been created). */ 9826 tree 9827 build_void_list_node (void) 9828 { 9829 tree t = build_tree_list (NULL_TREE, void_type_node); 9830 return t; 9831 } 9832 9833 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */ 9834 9835 struct c_parm * 9836 build_c_parm (struct c_declspecs *specs, tree attrs, 9837 struct c_declarator *declarator, 9838 location_t loc) 9839 { 9840 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm); 9841 ret->specs = specs; 9842 ret->attrs = attrs; 9843 ret->declarator = declarator; 9844 ret->loc = loc; 9845 return ret; 9846 } 9847 9848 /* Return a declarator with nested attributes. TARGET is the inner 9849 declarator to which these attributes apply. ATTRS are the 9850 attributes. */ 9851 9852 struct c_declarator * 9853 build_attrs_declarator (tree attrs, struct c_declarator *target) 9854 { 9855 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 9856 ret->kind = cdk_attrs; 9857 ret->declarator = target; 9858 ret->u.attrs = attrs; 9859 return ret; 9860 } 9861 9862 /* Return a declarator for a function with arguments specified by ARGS 9863 and return type specified by TARGET. */ 9864 9865 struct c_declarator * 9866 build_function_declarator (struct c_arg_info *args, 9867 struct c_declarator *target) 9868 { 9869 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 9870 ret->kind = cdk_function; 9871 ret->declarator = target; 9872 ret->u.arg_info = args; 9873 return ret; 9874 } 9875 9876 /* Return a declarator for the identifier IDENT (which may be 9877 NULL_TREE for an abstract declarator). */ 9878 9879 struct c_declarator * 9880 build_id_declarator (tree ident) 9881 { 9882 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 9883 ret->kind = cdk_id; 9884 ret->declarator = 0; 9885 ret->u.id = ident; 9886 /* Default value - may get reset to a more precise location. */ 9887 ret->id_loc = input_location; 9888 return ret; 9889 } 9890 9891 /* Return something to represent absolute declarators containing a *. 9892 TARGET is the absolute declarator that the * contains. 9893 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes 9894 to apply to the pointer type. */ 9895 9896 struct c_declarator * 9897 make_pointer_declarator (struct c_declspecs *type_quals_attrs, 9898 struct c_declarator *target) 9899 { 9900 tree attrs; 9901 int quals = 0; 9902 struct c_declarator *itarget = target; 9903 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 9904 if (type_quals_attrs) 9905 { 9906 attrs = type_quals_attrs->attrs; 9907 quals = quals_from_declspecs (type_quals_attrs); 9908 if (attrs != NULL_TREE) 9909 itarget = build_attrs_declarator (attrs, target); 9910 } 9911 ret->kind = cdk_pointer; 9912 ret->declarator = itarget; 9913 ret->u.pointer_quals = quals; 9914 return ret; 9915 } 9916 9917 /* Return a pointer to a structure for an empty list of declaration 9918 specifiers. */ 9919 9920 struct c_declspecs * 9921 build_null_declspecs (void) 9922 { 9923 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs); 9924 memset (ret, 0, sizeof *ret); 9925 ret->align_log = -1; 9926 ret->typespec_word = cts_none; 9927 ret->storage_class = csc_none; 9928 ret->expr_const_operands = true; 9929 ret->typespec_kind = ctsk_none; 9930 ret->address_space = ADDR_SPACE_GENERIC; 9931 return ret; 9932 } 9933 9934 /* Add the address space ADDRSPACE to the declaration specifiers 9935 SPECS, returning SPECS. */ 9936 9937 struct c_declspecs * 9938 declspecs_add_addrspace (source_location location, 9939 struct c_declspecs *specs, addr_space_t as) 9940 { 9941 specs->non_sc_seen_p = true; 9942 specs->declspecs_seen_p = true; 9943 9944 if (!ADDR_SPACE_GENERIC_P (specs->address_space) 9945 && specs->address_space != as) 9946 error ("incompatible address space qualifiers %qs and %qs", 9947 c_addr_space_name (as), 9948 c_addr_space_name (specs->address_space)); 9949 else 9950 { 9951 specs->address_space = as; 9952 specs->locations[cdw_address_space] = location; 9953 } 9954 return specs; 9955 } 9956 9957 /* Add the type qualifier QUAL to the declaration specifiers SPECS, 9958 returning SPECS. */ 9959 9960 struct c_declspecs * 9961 declspecs_add_qual (source_location loc, 9962 struct c_declspecs *specs, tree qual) 9963 { 9964 enum rid i; 9965 bool dupe = false; 9966 specs->non_sc_seen_p = true; 9967 specs->declspecs_seen_p = true; 9968 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE 9969 && C_IS_RESERVED_WORD (qual)); 9970 i = C_RID_CODE (qual); 9971 location_t prev_loc = UNKNOWN_LOCATION; 9972 switch (i) 9973 { 9974 case RID_CONST: 9975 dupe = specs->const_p; 9976 specs->const_p = true; 9977 prev_loc = specs->locations[cdw_const]; 9978 specs->locations[cdw_const] = loc; 9979 break; 9980 case RID_VOLATILE: 9981 dupe = specs->volatile_p; 9982 specs->volatile_p = true; 9983 prev_loc = specs->locations[cdw_volatile]; 9984 specs->locations[cdw_volatile] = loc; 9985 break; 9986 case RID_RESTRICT: 9987 dupe = specs->restrict_p; 9988 specs->restrict_p = true; 9989 prev_loc = specs->locations[cdw_restrict]; 9990 specs->locations[cdw_restrict] = loc; 9991 break; 9992 case RID_ATOMIC: 9993 dupe = specs->atomic_p; 9994 specs->atomic_p = true; 9995 prev_loc = specs->locations[cdw_atomic]; 9996 specs->locations[cdw_atomic] = loc; 9997 break; 9998 default: 9999 gcc_unreachable (); 10000 } 10001 if (dupe) 10002 { 10003 bool warned = pedwarn_c90 (loc, OPT_Wpedantic, 10004 "duplicate %qE declaration specifier", qual); 10005 if (!warned 10006 && warn_duplicate_decl_specifier 10007 && prev_loc >= RESERVED_LOCATION_COUNT 10008 && !from_macro_expansion_at (prev_loc) 10009 && !from_macro_expansion_at (loc)) 10010 warning_at (loc, OPT_Wduplicate_decl_specifier, 10011 "duplicate %qE declaration specifier", qual); 10012 } 10013 return specs; 10014 } 10015 10016 /* Add the type specifier TYPE to the declaration specifiers SPECS, 10017 returning SPECS. */ 10018 10019 struct c_declspecs * 10020 declspecs_add_type (location_t loc, struct c_declspecs *specs, 10021 struct c_typespec spec) 10022 { 10023 tree type = spec.spec; 10024 specs->non_sc_seen_p = true; 10025 specs->declspecs_seen_p = true; 10026 specs->typespec_kind = spec.kind; 10027 if (TREE_DEPRECATED (type)) 10028 specs->deprecated_p = true; 10029 10030 /* Handle type specifier keywords. */ 10031 if (TREE_CODE (type) == IDENTIFIER_NODE 10032 && C_IS_RESERVED_WORD (type) 10033 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN) 10034 { 10035 enum rid i = C_RID_CODE (type); 10036 if (specs->type) 10037 { 10038 error_at (loc, "two or more data types in declaration specifiers"); 10039 return specs; 10040 } 10041 if ((int) i <= (int) RID_LAST_MODIFIER) 10042 { 10043 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */ 10044 bool dupe = false; 10045 switch (i) 10046 { 10047 case RID_LONG: 10048 if (specs->long_long_p) 10049 { 10050 error_at (loc, "%<long long long%> is too long for GCC"); 10051 break; 10052 } 10053 if (specs->long_p) 10054 { 10055 if (specs->typespec_word == cts_double) 10056 { 10057 error_at (loc, 10058 ("both %<long long%> and %<double%> in " 10059 "declaration specifiers")); 10060 break; 10061 } 10062 pedwarn_c90 (loc, OPT_Wlong_long, 10063 "ISO C90 does not support %<long long%>"); 10064 specs->long_long_p = 1; 10065 specs->locations[cdw_long_long] = loc; 10066 break; 10067 } 10068 if (specs->short_p) 10069 error_at (loc, 10070 ("both %<long%> and %<short%> in " 10071 "declaration specifiers")); 10072 else if (specs->typespec_word == cts_auto_type) 10073 error_at (loc, 10074 ("both %<long%> and %<__auto_type%> in " 10075 "declaration specifiers")); 10076 else if (specs->typespec_word == cts_void) 10077 error_at (loc, 10078 ("both %<long%> and %<void%> in " 10079 "declaration specifiers")); 10080 else if (specs->typespec_word == cts_int_n) 10081 error_at (loc, 10082 ("both %<long%> and %<__int%d%> in " 10083 "declaration specifiers"), 10084 int_n_data[specs->int_n_idx].bitsize); 10085 else if (specs->typespec_word == cts_bool) 10086 error_at (loc, 10087 ("both %<long%> and %<_Bool%> in " 10088 "declaration specifiers")); 10089 else if (specs->typespec_word == cts_char) 10090 error_at (loc, 10091 ("both %<long%> and %<char%> in " 10092 "declaration specifiers")); 10093 else if (specs->typespec_word == cts_float) 10094 error_at (loc, 10095 ("both %<long%> and %<float%> in " 10096 "declaration specifiers")); 10097 else if (specs->typespec_word == cts_floatn_nx) 10098 error_at (loc, 10099 ("both %<long%> and %<_Float%d%s%> in " 10100 "declaration specifiers"), 10101 floatn_nx_types[specs->floatn_nx_idx].n, 10102 (floatn_nx_types[specs->floatn_nx_idx].extended 10103 ? "x" 10104 : "")); 10105 else if (specs->typespec_word == cts_dfloat32) 10106 error_at (loc, 10107 ("both %<long%> and %<_Decimal32%> in " 10108 "declaration specifiers")); 10109 else if (specs->typespec_word == cts_dfloat64) 10110 error_at (loc, 10111 ("both %<long%> and %<_Decimal64%> in " 10112 "declaration specifiers")); 10113 else if (specs->typespec_word == cts_dfloat128) 10114 error_at (loc, 10115 ("both %<long%> and %<_Decimal128%> in " 10116 "declaration specifiers")); 10117 else 10118 { 10119 specs->long_p = true; 10120 specs->locations[cdw_long] = loc; 10121 } 10122 break; 10123 case RID_SHORT: 10124 dupe = specs->short_p; 10125 if (specs->long_p) 10126 error_at (loc, 10127 ("both %<long%> and %<short%> in " 10128 "declaration specifiers")); 10129 else if (specs->typespec_word == cts_auto_type) 10130 error_at (loc, 10131 ("both %<short%> and %<__auto_type%> in " 10132 "declaration specifiers")); 10133 else if (specs->typespec_word == cts_void) 10134 error_at (loc, 10135 ("both %<short%> and %<void%> in " 10136 "declaration specifiers")); 10137 else if (specs->typespec_word == cts_int_n) 10138 error_at (loc, 10139 ("both %<short%> and %<__int%d%> in " 10140 "declaration specifiers"), 10141 int_n_data[specs->int_n_idx].bitsize); 10142 else if (specs->typespec_word == cts_bool) 10143 error_at (loc, 10144 ("both %<short%> and %<_Bool%> in " 10145 "declaration specifiers")); 10146 else if (specs->typespec_word == cts_char) 10147 error_at (loc, 10148 ("both %<short%> and %<char%> in " 10149 "declaration specifiers")); 10150 else if (specs->typespec_word == cts_float) 10151 error_at (loc, 10152 ("both %<short%> and %<float%> in " 10153 "declaration specifiers")); 10154 else if (specs->typespec_word == cts_double) 10155 error_at (loc, 10156 ("both %<short%> and %<double%> in " 10157 "declaration specifiers")); 10158 else if (specs->typespec_word == cts_floatn_nx) 10159 error_at (loc, 10160 ("both %<short%> and %<_Float%d%s%> in " 10161 "declaration specifiers"), 10162 floatn_nx_types[specs->floatn_nx_idx].n, 10163 (floatn_nx_types[specs->floatn_nx_idx].extended 10164 ? "x" 10165 : "")); 10166 else if (specs->typespec_word == cts_dfloat32) 10167 error_at (loc, 10168 ("both %<short%> and %<_Decimal32%> in " 10169 "declaration specifiers")); 10170 else if (specs->typespec_word == cts_dfloat64) 10171 error_at (loc, 10172 ("both %<short%> and %<_Decimal64%> in " 10173 "declaration specifiers")); 10174 else if (specs->typespec_word == cts_dfloat128) 10175 error_at (loc, 10176 ("both %<short%> and %<_Decimal128%> in " 10177 "declaration specifiers")); 10178 else 10179 { 10180 specs->short_p = true; 10181 specs->locations[cdw_short] = loc; 10182 } 10183 break; 10184 case RID_SIGNED: 10185 dupe = specs->signed_p; 10186 if (specs->unsigned_p) 10187 error_at (loc, 10188 ("both %<signed%> and %<unsigned%> in " 10189 "declaration specifiers")); 10190 else if (specs->typespec_word == cts_auto_type) 10191 error_at (loc, 10192 ("both %<signed%> and %<__auto_type%> in " 10193 "declaration specifiers")); 10194 else if (specs->typespec_word == cts_void) 10195 error_at (loc, 10196 ("both %<signed%> and %<void%> in " 10197 "declaration specifiers")); 10198 else if (specs->typespec_word == cts_bool) 10199 error_at (loc, 10200 ("both %<signed%> and %<_Bool%> in " 10201 "declaration specifiers")); 10202 else if (specs->typespec_word == cts_float) 10203 error_at (loc, 10204 ("both %<signed%> and %<float%> in " 10205 "declaration specifiers")); 10206 else if (specs->typespec_word == cts_double) 10207 error_at (loc, 10208 ("both %<signed%> and %<double%> in " 10209 "declaration specifiers")); 10210 else if (specs->typespec_word == cts_floatn_nx) 10211 error_at (loc, 10212 ("both %<signed%> and %<_Float%d%s%> in " 10213 "declaration specifiers"), 10214 floatn_nx_types[specs->floatn_nx_idx].n, 10215 (floatn_nx_types[specs->floatn_nx_idx].extended 10216 ? "x" 10217 : "")); 10218 else if (specs->typespec_word == cts_dfloat32) 10219 error_at (loc, 10220 ("both %<signed%> and %<_Decimal32%> in " 10221 "declaration specifiers")); 10222 else if (specs->typespec_word == cts_dfloat64) 10223 error_at (loc, 10224 ("both %<signed%> and %<_Decimal64%> in " 10225 "declaration specifiers")); 10226 else if (specs->typespec_word == cts_dfloat128) 10227 error_at (loc, 10228 ("both %<signed%> and %<_Decimal128%> in " 10229 "declaration specifiers")); 10230 else 10231 { 10232 specs->signed_p = true; 10233 specs->locations[cdw_signed] = loc; 10234 } 10235 break; 10236 case RID_UNSIGNED: 10237 dupe = specs->unsigned_p; 10238 if (specs->signed_p) 10239 error_at (loc, 10240 ("both %<signed%> and %<unsigned%> in " 10241 "declaration specifiers")); 10242 else if (specs->typespec_word == cts_auto_type) 10243 error_at (loc, 10244 ("both %<unsigned%> and %<__auto_type%> in " 10245 "declaration specifiers")); 10246 else if (specs->typespec_word == cts_void) 10247 error_at (loc, 10248 ("both %<unsigned%> and %<void%> in " 10249 "declaration specifiers")); 10250 else if (specs->typespec_word == cts_bool) 10251 error_at (loc, 10252 ("both %<unsigned%> and %<_Bool%> in " 10253 "declaration specifiers")); 10254 else if (specs->typespec_word == cts_float) 10255 error_at (loc, 10256 ("both %<unsigned%> and %<float%> in " 10257 "declaration specifiers")); 10258 else if (specs->typespec_word == cts_double) 10259 error_at (loc, 10260 ("both %<unsigned%> and %<double%> in " 10261 "declaration specifiers")); 10262 else if (specs->typespec_word == cts_floatn_nx) 10263 error_at (loc, 10264 ("both %<unsigned%> and %<_Float%d%s%> in " 10265 "declaration specifiers"), 10266 floatn_nx_types[specs->floatn_nx_idx].n, 10267 (floatn_nx_types[specs->floatn_nx_idx].extended 10268 ? "x" 10269 : "")); 10270 else if (specs->typespec_word == cts_dfloat32) 10271 error_at (loc, 10272 ("both %<unsigned%> and %<_Decimal32%> in " 10273 "declaration specifiers")); 10274 else if (specs->typespec_word == cts_dfloat64) 10275 error_at (loc, 10276 ("both %<unsigned%> and %<_Decimal64%> in " 10277 "declaration specifiers")); 10278 else if (specs->typespec_word == cts_dfloat128) 10279 error_at (loc, 10280 ("both %<unsigned%> and %<_Decimal128%> in " 10281 "declaration specifiers")); 10282 else 10283 { 10284 specs->unsigned_p = true; 10285 specs->locations[cdw_unsigned] = loc; 10286 } 10287 break; 10288 case RID_COMPLEX: 10289 dupe = specs->complex_p; 10290 if (!in_system_header_at (loc)) 10291 pedwarn_c90 (loc, OPT_Wpedantic, 10292 "ISO C90 does not support complex types"); 10293 if (specs->typespec_word == cts_auto_type) 10294 error_at (loc, 10295 ("both %<complex%> and %<__auto_type%> in " 10296 "declaration specifiers")); 10297 else if (specs->typespec_word == cts_void) 10298 error_at (loc, 10299 ("both %<complex%> and %<void%> in " 10300 "declaration specifiers")); 10301 else if (specs->typespec_word == cts_bool) 10302 error_at (loc, 10303 ("both %<complex%> and %<_Bool%> in " 10304 "declaration specifiers")); 10305 else if (specs->typespec_word == cts_dfloat32) 10306 error_at (loc, 10307 ("both %<complex%> and %<_Decimal32%> in " 10308 "declaration specifiers")); 10309 else if (specs->typespec_word == cts_dfloat64) 10310 error_at (loc, 10311 ("both %<complex%> and %<_Decimal64%> in " 10312 "declaration specifiers")); 10313 else if (specs->typespec_word == cts_dfloat128) 10314 error_at (loc, 10315 ("both %<complex%> and %<_Decimal128%> in " 10316 "declaration specifiers")); 10317 else if (specs->typespec_word == cts_fract) 10318 error_at (loc, 10319 ("both %<complex%> and %<_Fract%> in " 10320 "declaration specifiers")); 10321 else if (specs->typespec_word == cts_accum) 10322 error_at (loc, 10323 ("both %<complex%> and %<_Accum%> in " 10324 "declaration specifiers")); 10325 else if (specs->saturating_p) 10326 error_at (loc, 10327 ("both %<complex%> and %<_Sat%> in " 10328 "declaration specifiers")); 10329 else 10330 { 10331 specs->complex_p = true; 10332 specs->locations[cdw_complex] = loc; 10333 } 10334 break; 10335 case RID_SAT: 10336 dupe = specs->saturating_p; 10337 pedwarn (loc, OPT_Wpedantic, 10338 "ISO C does not support saturating types"); 10339 if (specs->typespec_word == cts_int_n) 10340 { 10341 error_at (loc, 10342 ("both %<_Sat%> and %<__int%d%> in " 10343 "declaration specifiers"), 10344 int_n_data[specs->int_n_idx].bitsize); 10345 } 10346 else if (specs->typespec_word == cts_auto_type) 10347 error_at (loc, 10348 ("both %<_Sat%> and %<__auto_type%> in " 10349 "declaration specifiers")); 10350 else if (specs->typespec_word == cts_void) 10351 error_at (loc, 10352 ("both %<_Sat%> and %<void%> in " 10353 "declaration specifiers")); 10354 else if (specs->typespec_word == cts_bool) 10355 error_at (loc, 10356 ("both %<_Sat%> and %<_Bool%> in " 10357 "declaration specifiers")); 10358 else if (specs->typespec_word == cts_char) 10359 error_at (loc, 10360 ("both %<_Sat%> and %<char%> in " 10361 "declaration specifiers")); 10362 else if (specs->typespec_word == cts_int) 10363 error_at (loc, 10364 ("both %<_Sat%> and %<int%> in " 10365 "declaration specifiers")); 10366 else if (specs->typespec_word == cts_float) 10367 error_at (loc, 10368 ("both %<_Sat%> and %<float%> in " 10369 "declaration specifiers")); 10370 else if (specs->typespec_word == cts_double) 10371 error_at (loc, 10372 ("both %<_Sat%> and %<double%> in " 10373 "declaration specifiers")); 10374 else if (specs->typespec_word == cts_floatn_nx) 10375 error_at (loc, 10376 ("both %<_Sat%> and %<_Float%d%s%> in " 10377 "declaration specifiers"), 10378 floatn_nx_types[specs->floatn_nx_idx].n, 10379 (floatn_nx_types[specs->floatn_nx_idx].extended 10380 ? "x" 10381 : "")); 10382 else if (specs->typespec_word == cts_dfloat32) 10383 error_at (loc, 10384 ("both %<_Sat%> and %<_Decimal32%> in " 10385 "declaration specifiers")); 10386 else if (specs->typespec_word == cts_dfloat64) 10387 error_at (loc, 10388 ("both %<_Sat%> and %<_Decimal64%> in " 10389 "declaration specifiers")); 10390 else if (specs->typespec_word == cts_dfloat128) 10391 error_at (loc, 10392 ("both %<_Sat%> and %<_Decimal128%> in " 10393 "declaration specifiers")); 10394 else if (specs->complex_p) 10395 error_at (loc, 10396 ("both %<_Sat%> and %<complex%> in " 10397 "declaration specifiers")); 10398 else 10399 { 10400 specs->saturating_p = true; 10401 specs->locations[cdw_saturating] = loc; 10402 } 10403 break; 10404 default: 10405 gcc_unreachable (); 10406 } 10407 10408 if (dupe) 10409 error_at (loc, "duplicate %qE", type); 10410 10411 return specs; 10412 } 10413 else 10414 { 10415 /* "void", "_Bool", "char", "int", "float", "double", 10416 "_FloatN", "_FloatNx", "_Decimal32", "__intN", 10417 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or 10418 "__auto_type". */ 10419 if (specs->typespec_word != cts_none) 10420 { 10421 error_at (loc, 10422 "two or more data types in declaration specifiers"); 10423 return specs; 10424 } 10425 switch (i) 10426 { 10427 case RID_AUTO_TYPE: 10428 if (specs->long_p) 10429 error_at (loc, 10430 ("both %<long%> and %<__auto_type%> in " 10431 "declaration specifiers")); 10432 else if (specs->short_p) 10433 error_at (loc, 10434 ("both %<short%> and %<__auto_type%> in " 10435 "declaration specifiers")); 10436 else if (specs->signed_p) 10437 error_at (loc, 10438 ("both %<signed%> and %<__auto_type%> in " 10439 "declaration specifiers")); 10440 else if (specs->unsigned_p) 10441 error_at (loc, 10442 ("both %<unsigned%> and %<__auto_type%> in " 10443 "declaration specifiers")); 10444 else if (specs->complex_p) 10445 error_at (loc, 10446 ("both %<complex%> and %<__auto_type%> in " 10447 "declaration specifiers")); 10448 else if (specs->saturating_p) 10449 error_at (loc, 10450 ("both %<_Sat%> and %<__auto_type%> in " 10451 "declaration specifiers")); 10452 else 10453 { 10454 specs->typespec_word = cts_auto_type; 10455 specs->locations[cdw_typespec] = loc; 10456 } 10457 return specs; 10458 case RID_INT_N_0: 10459 case RID_INT_N_1: 10460 case RID_INT_N_2: 10461 case RID_INT_N_3: 10462 specs->int_n_idx = i - RID_INT_N_0; 10463 if (!in_system_header_at (input_location)) 10464 pedwarn (loc, OPT_Wpedantic, 10465 "ISO C does not support %<__int%d%> types", 10466 int_n_data[specs->int_n_idx].bitsize); 10467 10468 if (specs->long_p) 10469 error_at (loc, 10470 ("both %<__int%d%> and %<long%> in " 10471 "declaration specifiers"), 10472 int_n_data[specs->int_n_idx].bitsize); 10473 else if (specs->saturating_p) 10474 error_at (loc, 10475 ("both %<_Sat%> and %<__int%d%> in " 10476 "declaration specifiers"), 10477 int_n_data[specs->int_n_idx].bitsize); 10478 else if (specs->short_p) 10479 error_at (loc, 10480 ("both %<__int%d%> and %<short%> in " 10481 "declaration specifiers"), 10482 int_n_data[specs->int_n_idx].bitsize); 10483 else if (! int_n_enabled_p[specs->int_n_idx]) 10484 { 10485 specs->typespec_word = cts_int_n; 10486 error_at (loc, 10487 "%<__int%d%> is not supported on this target", 10488 int_n_data[specs->int_n_idx].bitsize); 10489 } 10490 else 10491 { 10492 specs->typespec_word = cts_int_n; 10493 specs->locations[cdw_typespec] = loc; 10494 } 10495 return specs; 10496 case RID_VOID: 10497 if (specs->long_p) 10498 error_at (loc, 10499 ("both %<long%> and %<void%> in " 10500 "declaration specifiers")); 10501 else if (specs->short_p) 10502 error_at (loc, 10503 ("both %<short%> and %<void%> in " 10504 "declaration specifiers")); 10505 else if (specs->signed_p) 10506 error_at (loc, 10507 ("both %<signed%> and %<void%> in " 10508 "declaration specifiers")); 10509 else if (specs->unsigned_p) 10510 error_at (loc, 10511 ("both %<unsigned%> and %<void%> in " 10512 "declaration specifiers")); 10513 else if (specs->complex_p) 10514 error_at (loc, 10515 ("both %<complex%> and %<void%> in " 10516 "declaration specifiers")); 10517 else if (specs->saturating_p) 10518 error_at (loc, 10519 ("both %<_Sat%> and %<void%> in " 10520 "declaration specifiers")); 10521 else 10522 { 10523 specs->typespec_word = cts_void; 10524 specs->locations[cdw_typespec] = loc; 10525 } 10526 return specs; 10527 case RID_BOOL: 10528 if (!in_system_header_at (loc)) 10529 pedwarn_c90 (loc, OPT_Wpedantic, 10530 "ISO C90 does not support boolean types"); 10531 if (specs->long_p) 10532 error_at (loc, 10533 ("both %<long%> and %<_Bool%> in " 10534 "declaration specifiers")); 10535 else if (specs->short_p) 10536 error_at (loc, 10537 ("both %<short%> and %<_Bool%> in " 10538 "declaration specifiers")); 10539 else if (specs->signed_p) 10540 error_at (loc, 10541 ("both %<signed%> and %<_Bool%> in " 10542 "declaration specifiers")); 10543 else if (specs->unsigned_p) 10544 error_at (loc, 10545 ("both %<unsigned%> and %<_Bool%> in " 10546 "declaration specifiers")); 10547 else if (specs->complex_p) 10548 error_at (loc, 10549 ("both %<complex%> and %<_Bool%> in " 10550 "declaration specifiers")); 10551 else if (specs->saturating_p) 10552 error_at (loc, 10553 ("both %<_Sat%> and %<_Bool%> in " 10554 "declaration specifiers")); 10555 else 10556 { 10557 specs->typespec_word = cts_bool; 10558 specs->locations[cdw_typespec] = loc; 10559 } 10560 return specs; 10561 case RID_CHAR: 10562 if (specs->long_p) 10563 error_at (loc, 10564 ("both %<long%> and %<char%> in " 10565 "declaration specifiers")); 10566 else if (specs->short_p) 10567 error_at (loc, 10568 ("both %<short%> and %<char%> in " 10569 "declaration specifiers")); 10570 else if (specs->saturating_p) 10571 error_at (loc, 10572 ("both %<_Sat%> and %<char%> in " 10573 "declaration specifiers")); 10574 else 10575 { 10576 specs->typespec_word = cts_char; 10577 specs->locations[cdw_typespec] = loc; 10578 } 10579 return specs; 10580 case RID_INT: 10581 if (specs->saturating_p) 10582 error_at (loc, 10583 ("both %<_Sat%> and %<int%> in " 10584 "declaration specifiers")); 10585 else 10586 { 10587 specs->typespec_word = cts_int; 10588 specs->locations[cdw_typespec] = loc; 10589 } 10590 return specs; 10591 case RID_FLOAT: 10592 if (specs->long_p) 10593 error_at (loc, 10594 ("both %<long%> and %<float%> in " 10595 "declaration specifiers")); 10596 else if (specs->short_p) 10597 error_at (loc, 10598 ("both %<short%> and %<float%> in " 10599 "declaration specifiers")); 10600 else if (specs->signed_p) 10601 error_at (loc, 10602 ("both %<signed%> and %<float%> in " 10603 "declaration specifiers")); 10604 else if (specs->unsigned_p) 10605 error_at (loc, 10606 ("both %<unsigned%> and %<float%> in " 10607 "declaration specifiers")); 10608 else if (specs->saturating_p) 10609 error_at (loc, 10610 ("both %<_Sat%> and %<float%> in " 10611 "declaration specifiers")); 10612 else 10613 { 10614 specs->typespec_word = cts_float; 10615 specs->locations[cdw_typespec] = loc; 10616 } 10617 return specs; 10618 case RID_DOUBLE: 10619 if (specs->long_long_p) 10620 error_at (loc, 10621 ("both %<long long%> and %<double%> in " 10622 "declaration specifiers")); 10623 else if (specs->short_p) 10624 error_at (loc, 10625 ("both %<short%> and %<double%> in " 10626 "declaration specifiers")); 10627 else if (specs->signed_p) 10628 error_at (loc, 10629 ("both %<signed%> and %<double%> in " 10630 "declaration specifiers")); 10631 else if (specs->unsigned_p) 10632 error_at (loc, 10633 ("both %<unsigned%> and %<double%> in " 10634 "declaration specifiers")); 10635 else if (specs->saturating_p) 10636 error_at (loc, 10637 ("both %<_Sat%> and %<double%> in " 10638 "declaration specifiers")); 10639 else 10640 { 10641 specs->typespec_word = cts_double; 10642 specs->locations[cdw_typespec] = loc; 10643 } 10644 return specs; 10645 CASE_RID_FLOATN_NX: 10646 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST; 10647 if (!in_system_header_at (input_location)) 10648 pedwarn (loc, OPT_Wpedantic, 10649 "ISO C does not support the %<_Float%d%s%> type", 10650 floatn_nx_types[specs->floatn_nx_idx].n, 10651 (floatn_nx_types[specs->floatn_nx_idx].extended 10652 ? "x" 10653 : "")); 10654 10655 if (specs->long_p) 10656 error_at (loc, 10657 ("both %<long%> and %<_Float%d%s%> in " 10658 "declaration specifiers"), 10659 floatn_nx_types[specs->floatn_nx_idx].n, 10660 (floatn_nx_types[specs->floatn_nx_idx].extended 10661 ? "x" 10662 : "")); 10663 else if (specs->short_p) 10664 error_at (loc, 10665 ("both %<short%> and %<_Float%d%s%> in " 10666 "declaration specifiers"), 10667 floatn_nx_types[specs->floatn_nx_idx].n, 10668 (floatn_nx_types[specs->floatn_nx_idx].extended 10669 ? "x" 10670 : "")); 10671 else if (specs->signed_p) 10672 error_at (loc, 10673 ("both %<signed%> and %<_Float%d%s%> in " 10674 "declaration specifiers"), 10675 floatn_nx_types[specs->floatn_nx_idx].n, 10676 (floatn_nx_types[specs->floatn_nx_idx].extended 10677 ? "x" 10678 : "")); 10679 else if (specs->unsigned_p) 10680 error_at (loc, 10681 ("both %<unsigned%> and %<_Float%d%s%> in " 10682 "declaration specifiers"), 10683 floatn_nx_types[specs->floatn_nx_idx].n, 10684 (floatn_nx_types[specs->floatn_nx_idx].extended 10685 ? "x" 10686 : "")); 10687 else if (specs->saturating_p) 10688 error_at (loc, 10689 ("both %<_Sat%> and %<_Float%d%s%> in " 10690 "declaration specifiers"), 10691 floatn_nx_types[specs->floatn_nx_idx].n, 10692 (floatn_nx_types[specs->floatn_nx_idx].extended 10693 ? "x" 10694 : "")); 10695 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE) 10696 { 10697 specs->typespec_word = cts_floatn_nx; 10698 error_at (loc, 10699 "%<_Float%d%s%> is not supported on this target", 10700 floatn_nx_types[specs->floatn_nx_idx].n, 10701 (floatn_nx_types[specs->floatn_nx_idx].extended 10702 ? "x" 10703 : "")); 10704 } 10705 else 10706 { 10707 specs->typespec_word = cts_floatn_nx; 10708 specs->locations[cdw_typespec] = loc; 10709 } 10710 return specs; 10711 case RID_DFLOAT32: 10712 case RID_DFLOAT64: 10713 case RID_DFLOAT128: 10714 { 10715 const char *str; 10716 if (i == RID_DFLOAT32) 10717 str = "_Decimal32"; 10718 else if (i == RID_DFLOAT64) 10719 str = "_Decimal64"; 10720 else 10721 str = "_Decimal128"; 10722 if (specs->long_long_p) 10723 error_at (loc, 10724 ("both %<long long%> and %qs in " 10725 "declaration specifiers"), 10726 str); 10727 if (specs->long_p) 10728 error_at (loc, 10729 ("both %<long%> and %qs in " 10730 "declaration specifiers"), 10731 str); 10732 else if (specs->short_p) 10733 error_at (loc, 10734 ("both %<short%> and %qs in " 10735 "declaration specifiers"), 10736 str); 10737 else if (specs->signed_p) 10738 error_at (loc, 10739 ("both %<signed%> and %qs in " 10740 "declaration specifiers"), 10741 str); 10742 else if (specs->unsigned_p) 10743 error_at (loc, 10744 ("both %<unsigned%> and %qs in " 10745 "declaration specifiers"), 10746 str); 10747 else if (specs->complex_p) 10748 error_at (loc, 10749 ("both %<complex%> and %qs in " 10750 "declaration specifiers"), 10751 str); 10752 else if (specs->saturating_p) 10753 error_at (loc, 10754 ("both %<_Sat%> and %qs in " 10755 "declaration specifiers"), 10756 str); 10757 else if (i == RID_DFLOAT32) 10758 specs->typespec_word = cts_dfloat32; 10759 else if (i == RID_DFLOAT64) 10760 specs->typespec_word = cts_dfloat64; 10761 else 10762 specs->typespec_word = cts_dfloat128; 10763 specs->locations[cdw_typespec] = loc; 10764 } 10765 if (!targetm.decimal_float_supported_p ()) 10766 error_at (loc, 10767 ("decimal floating point not supported " 10768 "for this target")); 10769 pedwarn (loc, OPT_Wpedantic, 10770 "ISO C does not support decimal floating point"); 10771 return specs; 10772 case RID_FRACT: 10773 case RID_ACCUM: 10774 { 10775 const char *str; 10776 if (i == RID_FRACT) 10777 str = "_Fract"; 10778 else 10779 str = "_Accum"; 10780 if (specs->complex_p) 10781 error_at (loc, 10782 ("both %<complex%> and %qs in " 10783 "declaration specifiers"), 10784 str); 10785 else if (i == RID_FRACT) 10786 specs->typespec_word = cts_fract; 10787 else 10788 specs->typespec_word = cts_accum; 10789 specs->locations[cdw_typespec] = loc; 10790 } 10791 if (!targetm.fixed_point_supported_p ()) 10792 error_at (loc, 10793 "fixed-point types not supported for this target"); 10794 pedwarn (loc, OPT_Wpedantic, 10795 "ISO C does not support fixed-point types"); 10796 return specs; 10797 default: 10798 /* ObjC reserved word "id", handled below. */ 10799 break; 10800 } 10801 } 10802 } 10803 10804 /* Now we have a typedef (a TYPE_DECL node), an identifier (some 10805 form of ObjC type, cases such as "int" and "long" being handled 10806 above), a TYPE (struct, union, enum and typeof specifiers) or an 10807 ERROR_MARK. In none of these cases may there have previously 10808 been any type specifiers. */ 10809 if (specs->type || specs->typespec_word != cts_none 10810 || specs->long_p || specs->short_p || specs->signed_p 10811 || specs->unsigned_p || specs->complex_p) 10812 error_at (loc, "two or more data types in declaration specifiers"); 10813 else if (TREE_CODE (type) == TYPE_DECL) 10814 { 10815 if (TREE_TYPE (type) == error_mark_node) 10816 ; /* Allow the type to default to int to avoid cascading errors. */ 10817 else 10818 { 10819 specs->type = TREE_TYPE (type); 10820 specs->decl_attr = DECL_ATTRIBUTES (type); 10821 specs->typedef_p = true; 10822 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type); 10823 specs->locations[cdw_typedef] = loc; 10824 10825 /* If this typedef name is defined in a struct, then a C++ 10826 lookup would return a different value. */ 10827 if (warn_cxx_compat 10828 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct) 10829 warning_at (loc, OPT_Wc___compat, 10830 "C++ lookup of %qD would return a field, not a type", 10831 type); 10832 10833 /* If we are parsing a struct, record that a struct field 10834 used a typedef. */ 10835 if (warn_cxx_compat && struct_parse_info != NULL) 10836 struct_parse_info->typedefs_seen.safe_push (type); 10837 } 10838 } 10839 else if (TREE_CODE (type) == IDENTIFIER_NODE) 10840 { 10841 tree t = lookup_name (type); 10842 if (!t || TREE_CODE (t) != TYPE_DECL) 10843 error_at (loc, "%qE fails to be a typedef or built in type", type); 10844 else if (TREE_TYPE (t) == error_mark_node) 10845 ; 10846 else 10847 { 10848 specs->type = TREE_TYPE (t); 10849 specs->locations[cdw_typespec] = loc; 10850 } 10851 } 10852 else 10853 { 10854 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof) 10855 { 10856 specs->typedef_p = true; 10857 specs->locations[cdw_typedef] = loc; 10858 if (spec.expr) 10859 { 10860 if (specs->expr) 10861 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr), 10862 specs->expr, spec.expr); 10863 else 10864 specs->expr = spec.expr; 10865 specs->expr_const_operands &= spec.expr_const_operands; 10866 } 10867 } 10868 specs->type = type; 10869 } 10870 10871 return specs; 10872 } 10873 10874 /* Add the storage class specifier or function specifier SCSPEC to the 10875 declaration specifiers SPECS, returning SPECS. */ 10876 10877 struct c_declspecs * 10878 declspecs_add_scspec (source_location loc, 10879 struct c_declspecs *specs, 10880 tree scspec) 10881 { 10882 enum rid i; 10883 enum c_storage_class n = csc_none; 10884 bool dupe = false; 10885 specs->declspecs_seen_p = true; 10886 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE 10887 && C_IS_RESERVED_WORD (scspec)); 10888 i = C_RID_CODE (scspec); 10889 if (specs->non_sc_seen_p) 10890 warning (OPT_Wold_style_declaration, 10891 "%qE is not at beginning of declaration", scspec); 10892 switch (i) 10893 { 10894 case RID_INLINE: 10895 /* C99 permits duplicate inline. Although of doubtful utility, 10896 it seems simplest to permit it in gnu89 mode as well, as 10897 there is also little utility in maintaining this as a 10898 difference between gnu89 and C99 inline. */ 10899 dupe = false; 10900 specs->inline_p = true; 10901 specs->locations[cdw_inline] = loc; 10902 break; 10903 case RID_NORETURN: 10904 /* Duplicate _Noreturn is permitted. */ 10905 dupe = false; 10906 specs->noreturn_p = true; 10907 specs->locations[cdw_noreturn] = loc; 10908 break; 10909 case RID_THREAD: 10910 dupe = specs->thread_p; 10911 if (specs->storage_class == csc_auto) 10912 error ("%qE used with %<auto%>", scspec); 10913 else if (specs->storage_class == csc_register) 10914 error ("%qE used with %<register%>", scspec); 10915 else if (specs->storage_class == csc_typedef) 10916 error ("%qE used with %<typedef%>", scspec); 10917 else 10918 { 10919 specs->thread_p = true; 10920 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec), 10921 "__thread") == 0); 10922 /* A diagnostic is not required for the use of this 10923 identifier in the implementation namespace; only diagnose 10924 it for the C11 spelling because of existing code using 10925 the other spelling. */ 10926 if (!specs->thread_gnu_p) 10927 { 10928 if (flag_isoc99) 10929 pedwarn_c99 (loc, OPT_Wpedantic, 10930 "ISO C99 does not support %qE", scspec); 10931 else 10932 pedwarn_c99 (loc, OPT_Wpedantic, 10933 "ISO C90 does not support %qE", scspec); 10934 } 10935 specs->locations[cdw_thread] = loc; 10936 } 10937 break; 10938 case RID_AUTO: 10939 n = csc_auto; 10940 break; 10941 case RID_EXTERN: 10942 n = csc_extern; 10943 /* Diagnose "__thread extern". */ 10944 if (specs->thread_p && specs->thread_gnu_p) 10945 error ("%<__thread%> before %<extern%>"); 10946 break; 10947 case RID_REGISTER: 10948 n = csc_register; 10949 break; 10950 case RID_STATIC: 10951 n = csc_static; 10952 /* Diagnose "__thread static". */ 10953 if (specs->thread_p && specs->thread_gnu_p) 10954 error ("%<__thread%> before %<static%>"); 10955 break; 10956 case RID_TYPEDEF: 10957 n = csc_typedef; 10958 break; 10959 default: 10960 gcc_unreachable (); 10961 } 10962 if (n != csc_none && n == specs->storage_class) 10963 dupe = true; 10964 if (dupe) 10965 { 10966 if (i == RID_THREAD) 10967 error ("duplicate %<_Thread_local%> or %<__thread%>"); 10968 else 10969 error ("duplicate %qE", scspec); 10970 } 10971 if (n != csc_none) 10972 { 10973 if (specs->storage_class != csc_none && n != specs->storage_class) 10974 { 10975 error ("multiple storage classes in declaration specifiers"); 10976 } 10977 else 10978 { 10979 specs->storage_class = n; 10980 specs->locations[cdw_storage_class] = loc; 10981 if (n != csc_extern && n != csc_static && specs->thread_p) 10982 { 10983 error ("%qs used with %qE", 10984 specs->thread_gnu_p ? "__thread" : "_Thread_local", 10985 scspec); 10986 specs->thread_p = false; 10987 } 10988 } 10989 } 10990 return specs; 10991 } 10992 10993 /* Add the attributes ATTRS to the declaration specifiers SPECS, 10994 returning SPECS. */ 10995 10996 struct c_declspecs * 10997 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs) 10998 { 10999 specs->attrs = chainon (attrs, specs->attrs); 11000 specs->locations[cdw_attributes] = loc; 11001 specs->declspecs_seen_p = true; 11002 return specs; 11003 } 11004 11005 /* Add an _Alignas specifier (expression ALIGN, or type whose 11006 alignment is ALIGN) to the declaration specifiers SPECS, returning 11007 SPECS. */ 11008 struct c_declspecs * 11009 declspecs_add_alignas (source_location loc, 11010 struct c_declspecs *specs, tree align) 11011 { 11012 int align_log; 11013 specs->alignas_p = true; 11014 specs->locations[cdw_alignas] = loc; 11015 if (align == error_mark_node) 11016 return specs; 11017 align_log = check_user_alignment (align, true); 11018 if (align_log > specs->align_log) 11019 specs->align_log = align_log; 11020 return specs; 11021 } 11022 11023 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type 11024 specifiers with any other type specifier to determine the resulting 11025 type. This is where ISO C checks on complex types are made, since 11026 "_Complex long" is a prefix of the valid ISO C type "_Complex long 11027 double". */ 11028 11029 struct c_declspecs * 11030 finish_declspecs (struct c_declspecs *specs) 11031 { 11032 /* If a type was specified as a whole, we have no modifiers and are 11033 done. */ 11034 if (specs->type != NULL_TREE) 11035 { 11036 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p 11037 && !specs->signed_p && !specs->unsigned_p 11038 && !specs->complex_p); 11039 11040 /* Set a dummy type. */ 11041 if (TREE_CODE (specs->type) == ERROR_MARK) 11042 specs->type = integer_type_node; 11043 return specs; 11044 } 11045 11046 /* If none of "void", "_Bool", "char", "int", "float" or "double" 11047 has been specified, treat it as "int" unless "_Complex" is 11048 present and there are no other specifiers. If we just have 11049 "_Complex", it is equivalent to "_Complex double", but e.g. 11050 "_Complex short" is equivalent to "_Complex short int". */ 11051 if (specs->typespec_word == cts_none) 11052 { 11053 if (specs->saturating_p) 11054 { 11055 error_at (specs->locations[cdw_saturating], 11056 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>"); 11057 if (!targetm.fixed_point_supported_p ()) 11058 error_at (specs->locations[cdw_saturating], 11059 "fixed-point types not supported for this target"); 11060 specs->typespec_word = cts_fract; 11061 } 11062 else if (specs->long_p || specs->short_p 11063 || specs->signed_p || specs->unsigned_p) 11064 { 11065 specs->typespec_word = cts_int; 11066 } 11067 else if (specs->complex_p) 11068 { 11069 specs->typespec_word = cts_double; 11070 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic, 11071 "ISO C does not support plain %<complex%> meaning " 11072 "%<double complex%>"); 11073 } 11074 else 11075 { 11076 specs->typespec_word = cts_int; 11077 specs->default_int_p = true; 11078 /* We don't diagnose this here because grokdeclarator will 11079 give more specific diagnostics according to whether it is 11080 a function definition. */ 11081 } 11082 } 11083 11084 /* If "signed" was specified, record this to distinguish "int" and 11085 "signed int" in the case of a bit-field with 11086 -funsigned-bitfields. */ 11087 specs->explicit_signed_p = specs->signed_p; 11088 11089 /* Now compute the actual type. */ 11090 switch (specs->typespec_word) 11091 { 11092 case cts_auto_type: 11093 gcc_assert (!specs->long_p && !specs->short_p 11094 && !specs->signed_p && !specs->unsigned_p 11095 && !specs->complex_p); 11096 /* Type to be filled in later. */ 11097 break; 11098 case cts_void: 11099 gcc_assert (!specs->long_p && !specs->short_p 11100 && !specs->signed_p && !specs->unsigned_p 11101 && !specs->complex_p); 11102 specs->type = void_type_node; 11103 break; 11104 case cts_bool: 11105 gcc_assert (!specs->long_p && !specs->short_p 11106 && !specs->signed_p && !specs->unsigned_p 11107 && !specs->complex_p); 11108 specs->type = boolean_type_node; 11109 break; 11110 case cts_char: 11111 gcc_assert (!specs->long_p && !specs->short_p); 11112 gcc_assert (!(specs->signed_p && specs->unsigned_p)); 11113 if (specs->signed_p) 11114 specs->type = signed_char_type_node; 11115 else if (specs->unsigned_p) 11116 specs->type = unsigned_char_type_node; 11117 else 11118 specs->type = char_type_node; 11119 if (specs->complex_p) 11120 { 11121 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic, 11122 "ISO C does not support complex integer types"); 11123 specs->type = build_complex_type (specs->type); 11124 } 11125 break; 11126 case cts_int_n: 11127 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p); 11128 gcc_assert (!(specs->signed_p && specs->unsigned_p)); 11129 if (! int_n_enabled_p[specs->int_n_idx]) 11130 specs->type = integer_type_node; 11131 else 11132 specs->type = (specs->unsigned_p 11133 ? int_n_trees[specs->int_n_idx].unsigned_type 11134 : int_n_trees[specs->int_n_idx].signed_type); 11135 if (specs->complex_p) 11136 { 11137 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic, 11138 "ISO C does not support complex integer types"); 11139 specs->type = build_complex_type (specs->type); 11140 } 11141 break; 11142 case cts_int: 11143 gcc_assert (!(specs->long_p && specs->short_p)); 11144 gcc_assert (!(specs->signed_p && specs->unsigned_p)); 11145 if (specs->long_long_p) 11146 specs->type = (specs->unsigned_p 11147 ? long_long_unsigned_type_node 11148 : long_long_integer_type_node); 11149 else if (specs->long_p) 11150 specs->type = (specs->unsigned_p 11151 ? long_unsigned_type_node 11152 : long_integer_type_node); 11153 else if (specs->short_p) 11154 specs->type = (specs->unsigned_p 11155 ? short_unsigned_type_node 11156 : short_integer_type_node); 11157 else 11158 specs->type = (specs->unsigned_p 11159 ? unsigned_type_node 11160 : integer_type_node); 11161 if (specs->complex_p) 11162 { 11163 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic, 11164 "ISO C does not support complex integer types"); 11165 specs->type = build_complex_type (specs->type); 11166 } 11167 break; 11168 case cts_float: 11169 gcc_assert (!specs->long_p && !specs->short_p 11170 && !specs->signed_p && !specs->unsigned_p); 11171 specs->type = (specs->complex_p 11172 ? complex_float_type_node 11173 : float_type_node); 11174 break; 11175 case cts_double: 11176 gcc_assert (!specs->long_long_p && !specs->short_p 11177 && !specs->signed_p && !specs->unsigned_p); 11178 if (specs->long_p) 11179 { 11180 specs->type = (specs->complex_p 11181 ? complex_long_double_type_node 11182 : long_double_type_node); 11183 } 11184 else 11185 { 11186 specs->type = (specs->complex_p 11187 ? complex_double_type_node 11188 : double_type_node); 11189 } 11190 break; 11191 case cts_floatn_nx: 11192 gcc_assert (!specs->long_p && !specs->short_p 11193 && !specs->signed_p && !specs->unsigned_p); 11194 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE) 11195 specs->type = integer_type_node; 11196 else if (specs->complex_p) 11197 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx); 11198 else 11199 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx); 11200 break; 11201 case cts_dfloat32: 11202 case cts_dfloat64: 11203 case cts_dfloat128: 11204 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p 11205 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p); 11206 if (specs->typespec_word == cts_dfloat32) 11207 specs->type = dfloat32_type_node; 11208 else if (specs->typespec_word == cts_dfloat64) 11209 specs->type = dfloat64_type_node; 11210 else 11211 specs->type = dfloat128_type_node; 11212 break; 11213 case cts_fract: 11214 gcc_assert (!specs->complex_p); 11215 if (!targetm.fixed_point_supported_p ()) 11216 specs->type = integer_type_node; 11217 else if (specs->saturating_p) 11218 { 11219 if (specs->long_long_p) 11220 specs->type = specs->unsigned_p 11221 ? sat_unsigned_long_long_fract_type_node 11222 : sat_long_long_fract_type_node; 11223 else if (specs->long_p) 11224 specs->type = specs->unsigned_p 11225 ? sat_unsigned_long_fract_type_node 11226 : sat_long_fract_type_node; 11227 else if (specs->short_p) 11228 specs->type = specs->unsigned_p 11229 ? sat_unsigned_short_fract_type_node 11230 : sat_short_fract_type_node; 11231 else 11232 specs->type = specs->unsigned_p 11233 ? sat_unsigned_fract_type_node 11234 : sat_fract_type_node; 11235 } 11236 else 11237 { 11238 if (specs->long_long_p) 11239 specs->type = specs->unsigned_p 11240 ? unsigned_long_long_fract_type_node 11241 : long_long_fract_type_node; 11242 else if (specs->long_p) 11243 specs->type = specs->unsigned_p 11244 ? unsigned_long_fract_type_node 11245 : long_fract_type_node; 11246 else if (specs->short_p) 11247 specs->type = specs->unsigned_p 11248 ? unsigned_short_fract_type_node 11249 : short_fract_type_node; 11250 else 11251 specs->type = specs->unsigned_p 11252 ? unsigned_fract_type_node 11253 : fract_type_node; 11254 } 11255 break; 11256 case cts_accum: 11257 gcc_assert (!specs->complex_p); 11258 if (!targetm.fixed_point_supported_p ()) 11259 specs->type = integer_type_node; 11260 else if (specs->saturating_p) 11261 { 11262 if (specs->long_long_p) 11263 specs->type = specs->unsigned_p 11264 ? sat_unsigned_long_long_accum_type_node 11265 : sat_long_long_accum_type_node; 11266 else if (specs->long_p) 11267 specs->type = specs->unsigned_p 11268 ? sat_unsigned_long_accum_type_node 11269 : sat_long_accum_type_node; 11270 else if (specs->short_p) 11271 specs->type = specs->unsigned_p 11272 ? sat_unsigned_short_accum_type_node 11273 : sat_short_accum_type_node; 11274 else 11275 specs->type = specs->unsigned_p 11276 ? sat_unsigned_accum_type_node 11277 : sat_accum_type_node; 11278 } 11279 else 11280 { 11281 if (specs->long_long_p) 11282 specs->type = specs->unsigned_p 11283 ? unsigned_long_long_accum_type_node 11284 : long_long_accum_type_node; 11285 else if (specs->long_p) 11286 specs->type = specs->unsigned_p 11287 ? unsigned_long_accum_type_node 11288 : long_accum_type_node; 11289 else if (specs->short_p) 11290 specs->type = specs->unsigned_p 11291 ? unsigned_short_accum_type_node 11292 : short_accum_type_node; 11293 else 11294 specs->type = specs->unsigned_p 11295 ? unsigned_accum_type_node 11296 : accum_type_node; 11297 } 11298 break; 11299 default: 11300 gcc_unreachable (); 11301 } 11302 11303 return specs; 11304 } 11305 11306 /* Perform final processing on one file scope's declarations (or the 11307 external scope's declarations), GLOBALS. */ 11308 11309 static void 11310 c_write_global_declarations_1 (tree globals) 11311 { 11312 tree decl; 11313 bool reconsider; 11314 11315 /* Process the decls in the order they were written. */ 11316 for (decl = globals; decl; decl = DECL_CHAIN (decl)) 11317 { 11318 /* Check for used but undefined static functions using the C 11319 standard's definition of "used", and set TREE_NO_WARNING so 11320 that check_global_declaration doesn't repeat the check. */ 11321 if (TREE_CODE (decl) == FUNCTION_DECL 11322 && DECL_INITIAL (decl) == NULL_TREE 11323 && DECL_EXTERNAL (decl) 11324 && !TREE_PUBLIC (decl)) 11325 { 11326 if (C_DECL_USED (decl)) 11327 { 11328 pedwarn (input_location, 0, "%q+F used but never defined", decl); 11329 TREE_NO_WARNING (decl) = 1; 11330 } 11331 /* For -Wunused-function warn about unused static prototypes. */ 11332 else if (warn_unused_function 11333 && ! DECL_ARTIFICIAL (decl) 11334 && ! TREE_NO_WARNING (decl)) 11335 { 11336 warning (OPT_Wunused_function, 11337 "%q+F declared %<static%> but never defined", decl); 11338 TREE_NO_WARNING (decl) = 1; 11339 } 11340 } 11341 11342 wrapup_global_declaration_1 (decl); 11343 } 11344 11345 do 11346 { 11347 reconsider = false; 11348 for (decl = globals; decl; decl = DECL_CHAIN (decl)) 11349 reconsider |= wrapup_global_declaration_2 (decl); 11350 } 11351 while (reconsider); 11352 } 11353 11354 /* Callback to collect a source_ref from a DECL. */ 11355 11356 static void 11357 collect_source_ref_cb (tree decl) 11358 { 11359 if (!DECL_IS_BUILTIN (decl)) 11360 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false))); 11361 } 11362 11363 /* Preserve the external declarations scope across a garbage collect. */ 11364 static GTY(()) tree ext_block; 11365 11366 /* Collect all references relevant to SOURCE_FILE. */ 11367 11368 static void 11369 collect_all_refs (const char *source_file) 11370 { 11371 tree t; 11372 unsigned i; 11373 11374 FOR_EACH_VEC_ELT (*all_translation_units, i, t) 11375 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file); 11376 11377 collect_ada_nodes (BLOCK_VARS (ext_block), source_file); 11378 } 11379 11380 /* Iterate over all global declarations and call CALLBACK. */ 11381 11382 static void 11383 for_each_global_decl (void (*callback) (tree decl)) 11384 { 11385 tree t; 11386 tree decls; 11387 tree decl; 11388 unsigned i; 11389 11390 FOR_EACH_VEC_ELT (*all_translation_units, i, t) 11391 { 11392 decls = DECL_INITIAL (t); 11393 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl)) 11394 callback (decl); 11395 } 11396 11397 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl)) 11398 callback (decl); 11399 } 11400 11401 /* Perform any final parser cleanups and generate initial debugging 11402 information. */ 11403 11404 void 11405 c_parse_final_cleanups (void) 11406 { 11407 tree t; 11408 unsigned i; 11409 11410 /* We don't want to do this if generating a PCH. */ 11411 if (pch_file) 11412 return; 11413 11414 timevar_stop (TV_PHASE_PARSING); 11415 timevar_start (TV_PHASE_DEFERRED); 11416 11417 /* Do the Objective-C stuff. This is where all the Objective-C 11418 module stuff gets generated (symtab, class/protocol/selector 11419 lists etc). */ 11420 if (c_dialect_objc ()) 11421 objc_write_global_declarations (); 11422 11423 /* Close the external scope. */ 11424 ext_block = pop_scope (); 11425 external_scope = 0; 11426 gcc_assert (!current_scope); 11427 11428 /* Handle -fdump-ada-spec[-slim]. */ 11429 if (flag_dump_ada_spec || flag_dump_ada_spec_slim) 11430 { 11431 /* Build a table of files to generate specs for */ 11432 if (flag_dump_ada_spec_slim) 11433 collect_source_ref (main_input_filename); 11434 else 11435 for_each_global_decl (collect_source_ref_cb); 11436 11437 dump_ada_specs (collect_all_refs, NULL); 11438 } 11439 11440 /* Process all file scopes in this compilation, and the external_scope, 11441 through wrapup_global_declarations. */ 11442 FOR_EACH_VEC_ELT (*all_translation_units, i, t) 11443 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t))); 11444 c_write_global_declarations_1 (BLOCK_VARS (ext_block)); 11445 11446 timevar_stop (TV_PHASE_DEFERRED); 11447 timevar_start (TV_PHASE_PARSING); 11448 11449 ext_block = NULL; 11450 } 11451 11452 /* Register reserved keyword WORD as qualifier for address space AS. */ 11453 11454 void 11455 c_register_addr_space (const char *word, addr_space_t as) 11456 { 11457 int rid = RID_FIRST_ADDR_SPACE + as; 11458 tree id; 11459 11460 /* Address space qualifiers are only supported 11461 in C with GNU extensions enabled. */ 11462 if (c_dialect_objc () || flag_no_asm) 11463 return; 11464 11465 id = get_identifier (word); 11466 C_SET_RID_CODE (id, rid); 11467 C_IS_RESERVED_WORD (id) = 1; 11468 ridpointers [rid] = id; 11469 } 11470 11471 /* Return identifier to look up for omp declare reduction. */ 11472 11473 tree 11474 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id) 11475 { 11476 const char *p = NULL; 11477 switch (reduction_code) 11478 { 11479 case PLUS_EXPR: p = "+"; break; 11480 case MULT_EXPR: p = "*"; break; 11481 case MINUS_EXPR: p = "-"; break; 11482 case BIT_AND_EXPR: p = "&"; break; 11483 case BIT_XOR_EXPR: p = "^"; break; 11484 case BIT_IOR_EXPR: p = "|"; break; 11485 case TRUTH_ANDIF_EXPR: p = "&&"; break; 11486 case TRUTH_ORIF_EXPR: p = "||"; break; 11487 case MIN_EXPR: p = "min"; break; 11488 case MAX_EXPR: p = "max"; break; 11489 default: 11490 break; 11491 } 11492 11493 if (p == NULL) 11494 { 11495 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE) 11496 return error_mark_node; 11497 p = IDENTIFIER_POINTER (reduction_id); 11498 } 11499 11500 const char prefix[] = "omp declare reduction "; 11501 size_t lenp = sizeof (prefix); 11502 size_t len = strlen (p); 11503 char *name = XALLOCAVEC (char, lenp + len); 11504 memcpy (name, prefix, lenp - 1); 11505 memcpy (name + lenp - 1, p, len + 1); 11506 return get_identifier (name); 11507 } 11508 11509 /* Lookup REDUCTION_ID in the current scope, or create an artificial 11510 VAR_DECL, bind it into the current scope and return it. */ 11511 11512 tree 11513 c_omp_reduction_decl (tree reduction_id) 11514 { 11515 struct c_binding *b = I_SYMBOL_BINDING (reduction_id); 11516 if (b != NULL && B_IN_CURRENT_SCOPE (b)) 11517 return b->decl; 11518 11519 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL, 11520 reduction_id, integer_type_node); 11521 DECL_ARTIFICIAL (decl) = 1; 11522 DECL_EXTERNAL (decl) = 1; 11523 TREE_STATIC (decl) = 1; 11524 TREE_PUBLIC (decl) = 0; 11525 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION); 11526 return decl; 11527 } 11528 11529 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */ 11530 11531 tree 11532 c_omp_reduction_lookup (tree reduction_id, tree type) 11533 { 11534 struct c_binding *b = I_SYMBOL_BINDING (reduction_id); 11535 while (b) 11536 { 11537 tree t; 11538 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t)) 11539 if (comptypes (TREE_PURPOSE (t), type)) 11540 return TREE_VALUE (t); 11541 b = b->shadowed; 11542 } 11543 return error_mark_node; 11544 } 11545 11546 /* Helper function called via walk_tree, to diagnose invalid 11547 #pragma omp declare reduction combiners or initializers. */ 11548 11549 tree 11550 c_check_omp_declare_reduction_r (tree *tp, int *, void *data) 11551 { 11552 tree *vars = (tree *) data; 11553 if (SSA_VAR_P (*tp) 11554 && !DECL_ARTIFICIAL (*tp) 11555 && *tp != vars[0] 11556 && *tp != vars[1]) 11557 { 11558 location_t loc = DECL_SOURCE_LOCATION (vars[0]); 11559 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0) 11560 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to " 11561 "variable %qD which is not %<omp_out%> nor %<omp_in%>", 11562 *tp); 11563 else 11564 error_at (loc, "%<#pragma omp declare reduction%> initializer refers " 11565 "to variable %qD which is not %<omp_priv%> nor " 11566 "%<omp_orig%>", 11567 *tp); 11568 return *tp; 11569 } 11570 return NULL_TREE; 11571 } 11572 11573 #include "gt-c-c-decl.h" 11574