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