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