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