c-decl.c revision 260918
1/* Process declarations and variables for C compiler. 2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 3 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. 4 5This file is part of GCC. 6 7GCC is free software; you can redistribute it and/or modify it under 8the terms of the GNU General Public License as published by the Free 9Software Foundation; either version 2, or (at your option) any later 10version. 11 12GCC is distributed in the hope that it will be useful, but WITHOUT ANY 13WARRANTY; without even the implied warranty of MERCHANTABILITY or 14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15for more details. 16 17You should have received a copy of the GNU General Public License 18along with GCC; see the file COPYING. If not, write to the Free 19Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 2002110-1301, USA. */ 21 22/* $FreeBSD: stable/10/contrib/gcc/c-decl.c 260918 2014-01-20 19:37:38Z pfg $ */ 23/* Merged C99 inline changes from gcc trunk 122565 2007-03-05 */ 24/* Fixed problems with compiling inline-25.c and inline-26.c */ 25/* XXX still fails inline-29.c, inline-31.c, and inline-32.c */ 26 27/* Process declarations and symbol lookup for C front end. 28 Also constructs types; the standard scalar types at initialization, 29 and structure, union, array and enum types when they are declared. */ 30 31/* ??? not all decl nodes are given the most useful possible 32 line numbers. For example, the CONST_DECLs for enum values. */ 33 34#include "config.h" 35#include "system.h" 36#include "coretypes.h" 37#include "input.h" 38#include "tm.h" 39#include "intl.h" 40#include "tree.h" 41#include "tree-inline.h" 42#include "rtl.h" 43#include "flags.h" 44#include "function.h" 45#include "output.h" 46#include "expr.h" 47#include "c-tree.h" 48#include "toplev.h" 49#include "ggc.h" 50#include "tm_p.h" 51#include "cpplib.h" 52#include "target.h" 53#include "debug.h" 54#include "opts.h" 55#include "timevar.h" 56#include "c-common.h" 57#include "c-pragma.h" 58#include "langhooks.h" 59#include "tree-mudflap.h" 60#include "tree-gimple.h" 61#include "diagnostic.h" 62#include "tree-dump.h" 63#include "cgraph.h" 64#include "hashtab.h" 65#include "libfuncs.h" 66#include "except.h" 67#include "langhooks-def.h" 68#include "pointer-set.h" 69 70/* In grokdeclarator, distinguish syntactic contexts of declarators. */ 71enum decl_context 72{ NORMAL, /* Ordinary declaration */ 73 FUNCDEF, /* Function definition */ 74 PARM, /* Declaration of parm before function body */ 75 FIELD, /* Declaration inside struct or union */ 76 TYPENAME}; /* Typename (inside cast or sizeof) */ 77 78 79/* Nonzero if we have seen an invalid cross reference 80 to a struct, union, or enum, but not yet printed the message. */ 81tree pending_invalid_xref; 82 83/* File and line to appear in the eventual error message. */ 84location_t pending_invalid_xref_location; 85 86/* True means we've initialized exception handling. */ 87bool c_eh_initialized_p; 88 89/* While defining an enum type, this is 1 plus the last enumerator 90 constant value. Note that will do not have to save this or `enum_overflow' 91 around nested function definition since such a definition could only 92 occur in an enum value expression and we don't use these variables in 93 that case. */ 94 95static tree enum_next_value; 96 97/* Nonzero means that there was overflow computing enum_next_value. */ 98 99static int enum_overflow; 100 101/* The file and line that the prototype came from if this is an 102 old-style definition; used for diagnostics in 103 store_parm_decls_oldstyle. */ 104 105static location_t current_function_prototype_locus; 106 107/* Whether this prototype was built-in. */ 108 109static bool current_function_prototype_built_in; 110 111/* The argument type information of this prototype. */ 112 113static tree current_function_prototype_arg_types; 114 115/* The argument information structure for the function currently being 116 defined. */ 117 118static struct c_arg_info *current_function_arg_info; 119 120/* The obstack on which parser and related data structures, which are 121 not live beyond their top-level declaration or definition, are 122 allocated. */ 123struct obstack parser_obstack; 124 125/* The current statement tree. */ 126 127static GTY(()) struct stmt_tree_s c_stmt_tree; 128 129/* State saving variables. */ 130tree c_break_label; 131tree c_cont_label; 132 133/* Linked list of TRANSLATION_UNIT_DECLS for the translation units 134 included in this invocation. Note that the current translation 135 unit is not included in this list. */ 136 137static GTY(()) tree all_translation_units; 138 139/* A list of decls to be made automatically visible in each file scope. */ 140static GTY(()) tree visible_builtins; 141 142/* Set to 0 at beginning of a function definition, set to 1 if 143 a return statement that specifies a return value is seen. */ 144 145int current_function_returns_value; 146 147/* Set to 0 at beginning of a function definition, set to 1 if 148 a return statement with no argument is seen. */ 149 150int current_function_returns_null; 151 152/* Set to 0 at beginning of a function definition, set to 1 if 153 a call to a noreturn function is seen. */ 154 155int current_function_returns_abnormally; 156 157/* Set to nonzero by `grokdeclarator' for a function 158 whose return type is defaulted, if warnings for this are desired. */ 159 160static int warn_about_return_type; 161 162/* Nonzero when the current toplevel function contains a declaration 163 of a nested function which is never defined. */ 164 165static bool undef_nested_function; 166 167/* True means global_bindings_p should return false even if the scope stack 168 says we are in file scope. */ 169bool c_override_global_bindings_to_false; 170 171 172/* Each c_binding structure describes one binding of an identifier to 173 a decl. All the decls in a scope - irrespective of namespace - are 174 chained together by the ->prev field, which (as the name implies) 175 runs in reverse order. All the decls in a given namespace bound to 176 a given identifier are chained by the ->shadowed field, which runs 177 from inner to outer scopes. 178 179 The ->decl field usually points to a DECL node, but there are two 180 exceptions. In the namespace of type tags, the bound entity is a 181 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared 182 identifier is encountered, it is bound to error_mark_node to 183 suppress further errors about that identifier in the current 184 function. 185 186 The ->type field stores the type of the declaration in this scope; 187 if NULL, the type is the type of the ->decl field. This is only of 188 relevance for objects with external or internal linkage which may 189 be redeclared in inner scopes, forming composite types that only 190 persist for the duration of those scopes. In the external scope, 191 this stores the composite of all the types declared for this 192 object, visible or not. The ->inner_comp field (used only at file 193 scope) stores whether an incomplete array type at file scope was 194 completed at an inner scope to an array size other than 1. 195 196 The depth field is copied from the scope structure that holds this 197 decl. It is used to preserve the proper ordering of the ->shadowed 198 field (see bind()) and also for a handful of special-case checks. 199 Finally, the invisible bit is true for a decl which should be 200 ignored for purposes of normal name lookup, and the nested bit is 201 true for a decl that's been bound a second time in an inner scope; 202 in all such cases, the binding in the outer scope will have its 203 invisible bit true. */ 204 205struct c_binding GTY((chain_next ("%h.prev"))) 206{ 207 tree decl; /* the decl bound */ 208 tree type; /* the type in this scope */ 209 tree id; /* the identifier it's bound to */ 210 struct c_binding *prev; /* the previous decl in this scope */ 211 struct c_binding *shadowed; /* the innermost decl shadowed by this one */ 212 unsigned int depth : 28; /* depth of this scope */ 213 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */ 214 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */ 215 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */ 216 /* one free bit */ 217}; 218#define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth) 219#define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth) 220#define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/) 221#define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/) 222 223#define I_SYMBOL_BINDING(node) \ 224 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding) 225#define I_SYMBOL_DECL(node) \ 226 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0) 227 228#define I_TAG_BINDING(node) \ 229 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding) 230#define I_TAG_DECL(node) \ 231 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0) 232 233#define I_LABEL_BINDING(node) \ 234 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding) 235#define I_LABEL_DECL(node) \ 236 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0) 237 238/* Each C symbol points to three linked lists of c_binding structures. 239 These describe the values of the identifier in the three different 240 namespaces defined by the language. */ 241 242struct lang_identifier GTY(()) 243{ 244 struct c_common_identifier common_id; 245 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */ 246 struct c_binding *tag_binding; /* struct/union/enum tags */ 247 struct c_binding *label_binding; /* labels */ 248}; 249 250/* Validate c-lang.c's assumptions. */ 251extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate 252[(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1]; 253 254/* The resulting tree type. */ 255 256union lang_tree_node 257 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"), 258 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : (union lang_tree_node *) TREE_CHAIN (&%h.generic)"))) 259{ 260 union tree_node GTY ((tag ("0"), 261 desc ("tree_node_structure (&%h)"))) 262 generic; 263 struct lang_identifier GTY ((tag ("1"))) identifier; 264}; 265 266/* Each c_scope structure describes the complete contents of one 267 scope. Four scopes are distinguished specially: the innermost or 268 current scope, the innermost function scope, the file scope (always 269 the second to outermost) and the outermost or external scope. 270 271 Most declarations are recorded in the current scope. 272 273 All normal label declarations are recorded in the innermost 274 function scope, as are bindings of undeclared identifiers to 275 error_mark_node. (GCC permits nested functions as an extension, 276 hence the 'innermost' qualifier.) Explicitly declared labels 277 (using the __label__ extension) appear in the current scope. 278 279 Being in the file scope (current_scope == file_scope) causes 280 special behavior in several places below. Also, under some 281 conditions the Objective-C front end records declarations in the 282 file scope even though that isn't the current scope. 283 284 All declarations with external linkage are recorded in the external 285 scope, even if they aren't visible there; this models the fact that 286 such declarations are visible to the entire program, and (with a 287 bit of cleverness, see pushdecl) allows diagnosis of some violations 288 of C99 6.2.2p7 and 6.2.7p2: 289 290 If, within the same translation unit, the same identifier appears 291 with both internal and external linkage, the behavior is 292 undefined. 293 294 All declarations that refer to the same object or function shall 295 have compatible type; otherwise, the behavior is undefined. 296 297 Initially only the built-in declarations, which describe compiler 298 intrinsic functions plus a subset of the standard library, are in 299 this scope. 300 301 The order of the blocks list matters, and it is frequently appended 302 to. To avoid having to walk all the way to the end of the list on 303 each insertion, or reverse the list later, we maintain a pointer to 304 the last list entry. (FIXME: It should be feasible to use a reversed 305 list here.) 306 307 The bindings list is strictly in reverse order of declarations; 308 pop_scope relies on this. */ 309 310 311struct c_scope GTY((chain_next ("%h.outer"))) 312{ 313 /* The scope containing this one. */ 314 struct c_scope *outer; 315 316 /* The next outermost function scope. */ 317 struct c_scope *outer_function; 318 319 /* All bindings in this scope. */ 320 struct c_binding *bindings; 321 322 /* For each scope (except the global one), a chain of BLOCK nodes 323 for all the scopes that were entered and exited one level down. */ 324 tree blocks; 325 tree blocks_last; 326 327 /* The depth of this scope. Used to keep the ->shadowed chain of 328 bindings sorted innermost to outermost. */ 329 unsigned int depth : 28; 330 331 /* True if we are currently filling this scope with parameter 332 declarations. */ 333 BOOL_BITFIELD parm_flag : 1; 334 335 /* True if we saw [*] in this scope. Used to give an error messages 336 if these appears in a function definition. */ 337 BOOL_BITFIELD had_vla_unspec : 1; 338 339 /* True if we already complained about forward parameter decls 340 in this scope. This prevents double warnings on 341 foo (int a; int b; ...) */ 342 BOOL_BITFIELD warned_forward_parm_decls : 1; 343 344 /* True if this is the outermost block scope of a function body. 345 This scope contains the parameters, the local variables declared 346 in the outermost block, and all the labels (except those in 347 nested functions, or declared at block scope with __label__). */ 348 BOOL_BITFIELD function_body : 1; 349 350 /* True means make a BLOCK for this scope no matter what. */ 351 BOOL_BITFIELD keep : 1; 352}; 353 354/* The scope currently in effect. */ 355 356static GTY(()) struct c_scope *current_scope; 357 358/* The innermost function scope. Ordinary (not explicitly declared) 359 labels, bindings to error_mark_node, and the lazily-created 360 bindings of __func__ and its friends get this scope. */ 361 362static GTY(()) struct c_scope *current_function_scope; 363 364/* The C file scope. This is reset for each input translation unit. */ 365 366static GTY(()) struct c_scope *file_scope; 367 368/* The outermost scope. This is used for all declarations with 369 external linkage, and only these, hence the name. */ 370 371static GTY(()) struct c_scope *external_scope; 372 373/* A chain of c_scope structures awaiting reuse. */ 374 375static GTY((deletable)) struct c_scope *scope_freelist; 376 377/* A chain of c_binding structures awaiting reuse. */ 378 379static GTY((deletable)) struct c_binding *binding_freelist; 380 381/* Append VAR to LIST in scope SCOPE. */ 382#define SCOPE_LIST_APPEND(scope, list, decl) do { \ 383 struct c_scope *s_ = (scope); \ 384 tree d_ = (decl); \ 385 if (s_->list##_last) \ 386 TREE_CHAIN (s_->list##_last) = d_; \ 387 else \ 388 s_->list = d_; \ 389 s_->list##_last = d_; \ 390} while (0) 391 392/* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */ 393#define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \ 394 struct c_scope *t_ = (tscope); \ 395 struct c_scope *f_ = (fscope); \ 396 if (t_->to##_last) \ 397 TREE_CHAIN (t_->to##_last) = f_->from; \ 398 else \ 399 t_->to = f_->from; \ 400 t_->to##_last = f_->from##_last; \ 401} while (0) 402 403/* True means unconditionally make a BLOCK for the next scope pushed. */ 404 405static bool keep_next_level_flag; 406 407/* True means the next call to push_scope will be the outermost scope 408 of a function body, so do not push a new scope, merely cease 409 expecting parameter decls. */ 410 411static bool next_is_function_body; 412 413/* Functions called automatically at the beginning and end of execution. */ 414 415static GTY(()) tree static_ctors; 416static GTY(()) tree static_dtors; 417 418/* Forward declarations. */ 419static tree lookup_name_in_scope (tree, struct c_scope *); 420static tree c_make_fname_decl (tree, int); 421static tree grokdeclarator (const struct c_declarator *, 422 struct c_declspecs *, 423 enum decl_context, bool, tree *); 424static tree grokparms (struct c_arg_info *, bool); 425static void layout_array_type (tree); 426 427/* T is a statement. Add it to the statement-tree. This is the 428 C/ObjC version--C++ has a slightly different version of this 429 function. */ 430 431tree 432add_stmt (tree t) 433{ 434 enum tree_code code = TREE_CODE (t); 435 436 if (EXPR_P (t) && code != LABEL_EXPR) 437 { 438 if (!EXPR_HAS_LOCATION (t)) 439 SET_EXPR_LOCATION (t, input_location); 440 } 441 442 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR) 443 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1; 444 445 /* Add T to the statement-tree. Non-side-effect statements need to be 446 recorded during statement expressions. */ 447 append_to_statement_list_force (t, &cur_stmt_list); 448 449 return t; 450} 451 452/* States indicating how grokdeclarator() should handle declspecs marked 453 with __attribute__((deprecated)). An object declared as 454 __attribute__((deprecated)) suppresses warnings of uses of other 455 deprecated items. */ 456/* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */ 457/* Also add an __attribute__((unavailable)). An object declared as 458 __attribute__((unavailable)) suppresses any reports of being 459 declared with unavailable or deprecated items. */ 460/* APPLE LOCAL end "unavailable" attribute (radar 2809697) */ 461 462enum deprecated_states { 463 DEPRECATED_NORMAL, 464 DEPRECATED_SUPPRESS 465 /* APPLE LOCAL "unavailable" attribute (radar 2809697) */ 466 , DEPRECATED_UNAVAILABLE_SUPPRESS 467}; 468 469static enum deprecated_states deprecated_state = DEPRECATED_NORMAL; 470 471void 472c_print_identifier (FILE *file, tree node, int indent) 473{ 474 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4); 475 print_node (file, "tag", I_TAG_DECL (node), indent + 4); 476 print_node (file, "label", I_LABEL_DECL (node), indent + 4); 477 if (C_IS_RESERVED_WORD (node)) 478 { 479 tree rid = ridpointers[C_RID_CODE (node)]; 480 indent_to (file, indent + 4); 481 fprintf (file, "rid %p \"%s\"", 482 (void *) rid, IDENTIFIER_POINTER (rid)); 483 } 484} 485 486/* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL, 487 which may be any of several kinds of DECL or TYPE or error_mark_node, 488 in the scope SCOPE. */ 489static void 490bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested) 491{ 492 struct c_binding *b, **here; 493 494 if (binding_freelist) 495 { 496 b = binding_freelist; 497 binding_freelist = b->prev; 498 } 499 else 500 b = GGC_NEW (struct c_binding); 501 502 b->shadowed = 0; 503 b->decl = decl; 504 b->id = name; 505 b->depth = scope->depth; 506 b->invisible = invisible; 507 b->nested = nested; 508 b->inner_comp = 0; 509 510 b->type = 0; 511 512 b->prev = scope->bindings; 513 scope->bindings = b; 514 515 if (!name) 516 return; 517 518 switch (TREE_CODE (decl)) 519 { 520 case LABEL_DECL: here = &I_LABEL_BINDING (name); break; 521 case ENUMERAL_TYPE: 522 case UNION_TYPE: 523 case RECORD_TYPE: here = &I_TAG_BINDING (name); break; 524 case VAR_DECL: 525 case FUNCTION_DECL: 526 case TYPE_DECL: 527 case CONST_DECL: 528 case PARM_DECL: 529 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break; 530 531 default: 532 gcc_unreachable (); 533 } 534 535 /* Locate the appropriate place in the chain of shadowed decls 536 to insert this binding. Normally, scope == current_scope and 537 this does nothing. */ 538 while (*here && (*here)->depth > scope->depth) 539 here = &(*here)->shadowed; 540 541 b->shadowed = *here; 542 *here = b; 543} 544 545/* Clear the binding structure B, stick it on the binding_freelist, 546 and return the former value of b->prev. This is used by pop_scope 547 and get_parm_info to iterate destructively over all the bindings 548 from a given scope. */ 549static struct c_binding * 550free_binding_and_advance (struct c_binding *b) 551{ 552 struct c_binding *prev = b->prev; 553 554 memset (b, 0, sizeof (struct c_binding)); 555 b->prev = binding_freelist; 556 binding_freelist = b; 557 558 return prev; 559} 560 561 562/* Hook called at end of compilation to assume 1 elt 563 for a file-scope tentative array defn that wasn't complete before. */ 564 565void 566c_finish_incomplete_decl (tree decl) 567{ 568 if (TREE_CODE (decl) == VAR_DECL) 569 { 570 tree type = TREE_TYPE (decl); 571 if (type != error_mark_node 572 && TREE_CODE (type) == ARRAY_TYPE 573 && !DECL_EXTERNAL (decl) 574 && TYPE_DOMAIN (type) == 0) 575 { 576 warning (0, "array %q+D assumed to have one element", decl); 577 578 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true); 579 580 layout_decl (decl, 0); 581 } 582 } 583} 584 585/* The Objective-C front-end often needs to determine the current scope. */ 586 587void * 588objc_get_current_scope (void) 589{ 590 return current_scope; 591} 592 593/* The following function is used only by Objective-C. It needs to live here 594 because it accesses the innards of c_scope. */ 595 596void 597objc_mark_locals_volatile (void *enclosing_blk) 598{ 599 struct c_scope *scope; 600 struct c_binding *b; 601 602 for (scope = current_scope; 603 scope && scope != enclosing_blk; 604 scope = scope->outer) 605 { 606 for (b = scope->bindings; b; b = b->prev) 607 objc_volatilize_decl (b->decl); 608 609 /* Do not climb up past the current function. */ 610 if (scope->function_body) 611 break; 612 } 613} 614 615/* Nonzero if we are currently in file scope. */ 616 617int 618global_bindings_p (void) 619{ 620 return current_scope == file_scope && !c_override_global_bindings_to_false; 621} 622 623void 624keep_next_level (void) 625{ 626 keep_next_level_flag = true; 627} 628 629/* Identify this scope as currently being filled with parameters. */ 630 631void 632declare_parm_level (void) 633{ 634 current_scope->parm_flag = true; 635} 636 637void 638push_scope (void) 639{ 640 if (next_is_function_body) 641 { 642 /* This is the transition from the parameters to the top level 643 of the function body. These are the same scope 644 (C99 6.2.1p4,6) so we do not push another scope structure. 645 next_is_function_body is set only by store_parm_decls, which 646 in turn is called when and only when we are about to 647 encounter the opening curly brace for the function body. 648 649 The outermost block of a function always gets a BLOCK node, 650 because the debugging output routines expect that each 651 function has at least one BLOCK. */ 652 current_scope->parm_flag = false; 653 current_scope->function_body = true; 654 current_scope->keep = true; 655 current_scope->outer_function = current_function_scope; 656 current_function_scope = current_scope; 657 658 keep_next_level_flag = false; 659 next_is_function_body = false; 660 } 661 else 662 { 663 struct c_scope *scope; 664 if (scope_freelist) 665 { 666 scope = scope_freelist; 667 scope_freelist = scope->outer; 668 } 669 else 670 scope = GGC_CNEW (struct c_scope); 671 672 scope->keep = keep_next_level_flag; 673 scope->outer = current_scope; 674 scope->depth = current_scope ? (current_scope->depth + 1) : 0; 675 676 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but 677 possible. */ 678 if (current_scope && scope->depth == 0) 679 { 680 scope->depth--; 681 sorry ("GCC supports only %u nested scopes", scope->depth); 682 } 683 684 current_scope = scope; 685 keep_next_level_flag = false; 686 } 687} 688 689/* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */ 690 691static void 692set_type_context (tree type, tree context) 693{ 694 for (type = TYPE_MAIN_VARIANT (type); type; 695 type = TYPE_NEXT_VARIANT (type)) 696 TYPE_CONTEXT (type) = context; 697} 698 699/* Exit a scope. Restore the state of the identifier-decl mappings 700 that were in effect when this scope was entered. Return a BLOCK 701 node containing all the DECLs in this scope that are of interest 702 to debug info generation. */ 703 704tree 705pop_scope (void) 706{ 707 struct c_scope *scope = current_scope; 708 tree block, context, p; 709 struct c_binding *b; 710 711 bool functionbody = scope->function_body; 712 bool keep = functionbody || scope->keep || scope->bindings; 713 714 c_end_vm_scope (scope->depth); 715 716 /* If appropriate, create a BLOCK to record the decls for the life 717 of this function. */ 718 block = 0; 719 if (keep) 720 { 721 block = make_node (BLOCK); 722 BLOCK_SUBBLOCKS (block) = scope->blocks; 723 TREE_USED (block) = 1; 724 725 /* In each subblock, record that this is its superior. */ 726 for (p = scope->blocks; p; p = TREE_CHAIN (p)) 727 BLOCK_SUPERCONTEXT (p) = block; 728 729 BLOCK_VARS (block) = 0; 730 } 731 732 /* The TYPE_CONTEXTs for all of the tagged types belonging to this 733 scope must be set so that they point to the appropriate 734 construct, i.e. either to the current FUNCTION_DECL node, or 735 else to the BLOCK node we just constructed. 736 737 Note that for tagged types whose scope is just the formal 738 parameter list for some function type specification, we can't 739 properly set their TYPE_CONTEXTs here, because we don't have a 740 pointer to the appropriate FUNCTION_TYPE node readily available 741 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged 742 type nodes get set in `grokdeclarator' as soon as we have created 743 the FUNCTION_TYPE node which will represent the "scope" for these 744 "parameter list local" tagged types. */ 745 if (scope->function_body) 746 context = current_function_decl; 747 else if (scope == file_scope) 748 { 749 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0); 750 TREE_CHAIN (file_decl) = all_translation_units; 751 all_translation_units = file_decl; 752 context = file_decl; 753 } 754 else 755 context = block; 756 757 /* Clear all bindings in this scope. */ 758 for (b = scope->bindings; b; b = free_binding_and_advance (b)) 759 { 760 p = b->decl; 761 switch (TREE_CODE (p)) 762 { 763 case LABEL_DECL: 764 /* Warnings for unused labels, errors for undefined labels. */ 765 if (TREE_USED (p) && !DECL_INITIAL (p)) 766 { 767 error ("label %q+D used but not defined", p); 768 DECL_INITIAL (p) = error_mark_node; 769 } 770 else if (!TREE_USED (p) && warn_unused_label) 771 { 772 if (DECL_INITIAL (p)) 773 warning (0, "label %q+D defined but not used", p); 774 else 775 warning (0, "label %q+D declared but not defined", p); 776 } 777 /* Labels go in BLOCK_VARS. */ 778 TREE_CHAIN (p) = BLOCK_VARS (block); 779 BLOCK_VARS (block) = p; 780 gcc_assert (I_LABEL_BINDING (b->id) == b); 781 I_LABEL_BINDING (b->id) = b->shadowed; 782 break; 783 784 case ENUMERAL_TYPE: 785 case UNION_TYPE: 786 case RECORD_TYPE: 787 set_type_context (p, context); 788 789 /* Types may not have tag-names, in which case the type 790 appears in the bindings list with b->id NULL. */ 791 if (b->id) 792 { 793 gcc_assert (I_TAG_BINDING (b->id) == b); 794 I_TAG_BINDING (b->id) = b->shadowed; 795 } 796 break; 797 798 case FUNCTION_DECL: 799 /* Propagate TREE_ADDRESSABLE from nested functions to their 800 containing functions. */ 801 if (!TREE_ASM_WRITTEN (p) 802 && DECL_INITIAL (p) != 0 803 && TREE_ADDRESSABLE (p) 804 && DECL_ABSTRACT_ORIGIN (p) != 0 805 && DECL_ABSTRACT_ORIGIN (p) != p) 806 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1; 807 if (!DECL_EXTERNAL (p) 808 && DECL_INITIAL (p) == 0 809 && scope != file_scope 810 && scope != external_scope) 811 { 812 error ("nested function %q+D declared but never defined", p); 813 undef_nested_function = true; 814 } 815 /* C99 6.7.4p6: "a function with external linkage... declared 816 with an inline function specifier ... shall also be defined in the 817 same translation unit." */ 818 else if (DECL_DECLARED_INLINE_P (p) 819 && TREE_PUBLIC (p) 820 && !DECL_INITIAL (p) 821 && !flag_gnu89_inline) 822 pedwarn ("inline function %q+D declared but never defined", p); 823 824 goto common_symbol; 825 826 case VAR_DECL: 827 /* Warnings for unused variables. */ 828 if (!TREE_USED (p) 829 && !TREE_NO_WARNING (p) 830 && !DECL_IN_SYSTEM_HEADER (p) 831 && DECL_NAME (p) 832 && !DECL_ARTIFICIAL (p) 833 && scope != file_scope 834 && scope != external_scope) 835 warning (OPT_Wunused_variable, "unused variable %q+D", p); 836 837 if (b->inner_comp) 838 { 839 error ("type of array %q+D completed incompatibly with" 840 " implicit initialization", p); 841 } 842 843 /* Fall through. */ 844 case TYPE_DECL: 845 case CONST_DECL: 846 common_symbol: 847 /* All of these go in BLOCK_VARS, but only if this is the 848 binding in the home scope. */ 849 if (!b->nested) 850 { 851 TREE_CHAIN (p) = BLOCK_VARS (block); 852 BLOCK_VARS (block) = p; 853 } 854 /* If this is the file scope, and we are processing more 855 than one translation unit in this compilation, set 856 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL. 857 This makes same_translation_unit_p work, and causes 858 static declarations to be given disambiguating suffixes. */ 859 if (scope == file_scope && num_in_fnames > 1) 860 { 861 DECL_CONTEXT (p) = context; 862 if (TREE_CODE (p) == TYPE_DECL) 863 set_type_context (TREE_TYPE (p), context); 864 } 865 866 /* Fall through. */ 867 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have 868 already been put there by store_parm_decls. Unused- 869 parameter warnings are handled by function.c. 870 error_mark_node obviously does not go in BLOCK_VARS and 871 does not get unused-variable warnings. */ 872 case PARM_DECL: 873 case ERROR_MARK: 874 /* It is possible for a decl not to have a name. We get 875 here with b->id NULL in this case. */ 876 if (b->id) 877 { 878 gcc_assert (I_SYMBOL_BINDING (b->id) == b); 879 I_SYMBOL_BINDING (b->id) = b->shadowed; 880 if (b->shadowed && b->shadowed->type) 881 TREE_TYPE (b->shadowed->decl) = b->shadowed->type; 882 } 883 break; 884 885 default: 886 gcc_unreachable (); 887 } 888 } 889 890 891 /* Dispose of the block that we just made inside some higher level. */ 892 if ((scope->function_body || scope == file_scope) && context) 893 { 894 DECL_INITIAL (context) = block; 895 BLOCK_SUPERCONTEXT (block) = context; 896 } 897 else if (scope->outer) 898 { 899 if (block) 900 SCOPE_LIST_APPEND (scope->outer, blocks, block); 901 /* If we did not make a block for the scope just exited, any 902 blocks made for inner scopes must be carried forward so they 903 will later become subblocks of something else. */ 904 else if (scope->blocks) 905 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks); 906 } 907 908 /* Pop the current scope, and free the structure for reuse. */ 909 current_scope = scope->outer; 910 if (scope->function_body) 911 current_function_scope = scope->outer_function; 912 913 memset (scope, 0, sizeof (struct c_scope)); 914 scope->outer = scope_freelist; 915 scope_freelist = scope; 916 917 return block; 918} 919 920void 921push_file_scope (void) 922{ 923 tree decl; 924 925 if (file_scope) 926 return; 927 928 push_scope (); 929 file_scope = current_scope; 930 931 start_fname_decls (); 932 933 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl)) 934 bind (DECL_NAME (decl), decl, file_scope, 935 /*invisible=*/false, /*nested=*/true); 936} 937 938void 939pop_file_scope (void) 940{ 941 /* In case there were missing closebraces, get us back to the global 942 binding level. */ 943 while (current_scope != file_scope) 944 pop_scope (); 945 946 /* __FUNCTION__ is defined at file scope (""). This 947 call may not be necessary as my tests indicate it 948 still works without it. */ 949 finish_fname_decls (); 950 951 /* This is the point to write out a PCH if we're doing that. 952 In that case we do not want to do anything else. */ 953 if (pch_file) 954 { 955 c_common_write_pch (); 956 return; 957 } 958 959 /* Pop off the file scope and close this translation unit. */ 960 pop_scope (); 961 file_scope = 0; 962 963 maybe_apply_pending_pragma_weaks (); 964 cgraph_finalize_compilation_unit (); 965} 966 967/* Insert BLOCK at the end of the list of subblocks of the current 968 scope. This is used when a BIND_EXPR is expanded, to handle the 969 BLOCK node inside the BIND_EXPR. */ 970 971void 972insert_block (tree block) 973{ 974 TREE_USED (block) = 1; 975 SCOPE_LIST_APPEND (current_scope, blocks, block); 976} 977 978/* Push a definition or a declaration of struct, union or enum tag "name". 979 "type" should be the type node. 980 We assume that the tag "name" is not already defined. 981 982 Note that the definition may really be just a forward reference. 983 In that case, the TYPE_SIZE will be zero. */ 984 985static void 986pushtag (tree name, tree type) 987{ 988 /* Record the identifier as the type's name if it has none. */ 989 if (name && !TYPE_NAME (type)) 990 TYPE_NAME (type) = name; 991 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false); 992 993 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the 994 tagged type we just added to the current scope. This fake 995 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs 996 to output a representation of a tagged type, and it also gives 997 us a convenient place to record the "scope start" address for the 998 tagged type. */ 999 1000 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type)); 1001 1002 /* An approximation for now, so we can tell this is a function-scope tag. 1003 This will be updated in pop_scope. */ 1004 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type)); 1005} 1006 1007/* Subroutine of compare_decls. Allow harmless mismatches in return 1008 and argument types provided that the type modes match. This function 1009 return a unified type given a suitable match, and 0 otherwise. */ 1010 1011static tree 1012match_builtin_function_types (tree newtype, tree oldtype) 1013{ 1014 tree newrettype, oldrettype; 1015 tree newargs, oldargs; 1016 tree trytype, tryargs; 1017 1018 /* Accept the return type of the new declaration if same modes. */ 1019 oldrettype = TREE_TYPE (oldtype); 1020 newrettype = TREE_TYPE (newtype); 1021 1022 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype)) 1023 return 0; 1024 1025 oldargs = TYPE_ARG_TYPES (oldtype); 1026 newargs = TYPE_ARG_TYPES (newtype); 1027 tryargs = newargs; 1028 1029 while (oldargs || newargs) 1030 { 1031 if (!oldargs 1032 || !newargs 1033 || !TREE_VALUE (oldargs) 1034 || !TREE_VALUE (newargs) 1035 || TYPE_MODE (TREE_VALUE (oldargs)) 1036 != TYPE_MODE (TREE_VALUE (newargs))) 1037 return 0; 1038 1039 oldargs = TREE_CHAIN (oldargs); 1040 newargs = TREE_CHAIN (newargs); 1041 } 1042 1043 trytype = build_function_type (newrettype, tryargs); 1044 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype)); 1045} 1046 1047/* Subroutine of diagnose_mismatched_decls. Check for function type 1048 mismatch involving an empty arglist vs a nonempty one and give clearer 1049 diagnostics. */ 1050static void 1051diagnose_arglist_conflict (tree newdecl, tree olddecl, 1052 tree newtype, tree oldtype) 1053{ 1054 tree t; 1055 1056 if (TREE_CODE (olddecl) != FUNCTION_DECL 1057 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype)) 1058 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0) 1059 || 1060 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0))) 1061 return; 1062 1063 t = TYPE_ARG_TYPES (oldtype); 1064 if (t == 0) 1065 t = TYPE_ARG_TYPES (newtype); 1066 for (; t; t = TREE_CHAIN (t)) 1067 { 1068 tree type = TREE_VALUE (t); 1069 1070 if (TREE_CHAIN (t) == 0 1071 && TYPE_MAIN_VARIANT (type) != void_type_node) 1072 { 1073 inform ("a parameter list with an ellipsis can%'t match " 1074 "an empty parameter name list declaration"); 1075 break; 1076 } 1077 1078 if (c_type_promotes_to (type) != type) 1079 { 1080 inform ("an argument type that has a default promotion can%'t match " 1081 "an empty parameter name list declaration"); 1082 break; 1083 } 1084 } 1085} 1086 1087/* Another subroutine of diagnose_mismatched_decls. OLDDECL is an 1088 old-style function definition, NEWDECL is a prototype declaration. 1089 Diagnose inconsistencies in the argument list. Returns TRUE if 1090 the prototype is compatible, FALSE if not. */ 1091static bool 1092validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype) 1093{ 1094 tree newargs, oldargs; 1095 int i; 1096 1097#define END_OF_ARGLIST(t) ((t) == void_type_node) 1098 1099 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype); 1100 newargs = TYPE_ARG_TYPES (newtype); 1101 i = 1; 1102 1103 for (;;) 1104 { 1105 tree oldargtype = TREE_VALUE (oldargs); 1106 tree newargtype = TREE_VALUE (newargs); 1107 1108 if (oldargtype == error_mark_node || newargtype == error_mark_node) 1109 return false; 1110 1111 oldargtype = TYPE_MAIN_VARIANT (oldargtype); 1112 newargtype = TYPE_MAIN_VARIANT (newargtype); 1113 1114 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype)) 1115 break; 1116 1117 /* Reaching the end of just one list means the two decls don't 1118 agree on the number of arguments. */ 1119 if (END_OF_ARGLIST (oldargtype)) 1120 { 1121 error ("prototype for %q+D declares more arguments " 1122 "than previous old-style definition", newdecl); 1123 return false; 1124 } 1125 else if (END_OF_ARGLIST (newargtype)) 1126 { 1127 error ("prototype for %q+D declares fewer arguments " 1128 "than previous old-style definition", newdecl); 1129 return false; 1130 } 1131 1132 /* Type for passing arg must be consistent with that declared 1133 for the arg. */ 1134 else if (!comptypes (oldargtype, newargtype)) 1135 { 1136 error ("prototype for %q+D declares argument %d" 1137 " with incompatible type", 1138 newdecl, i); 1139 return false; 1140 } 1141 1142 oldargs = TREE_CHAIN (oldargs); 1143 newargs = TREE_CHAIN (newargs); 1144 i++; 1145 } 1146 1147 /* If we get here, no errors were found, but do issue a warning 1148 for this poor-style construct. */ 1149 warning (0, "prototype for %q+D follows non-prototype definition", 1150 newdecl); 1151 return true; 1152#undef END_OF_ARGLIST 1153} 1154 1155/* Subroutine of diagnose_mismatched_decls. Report the location of DECL, 1156 first in a pair of mismatched declarations, using the diagnostic 1157 function DIAG. */ 1158static void 1159locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2)) 1160{ 1161 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)) 1162 ; 1163 else if (DECL_INITIAL (decl)) 1164 diag (G_("previous definition of %q+D was here"), decl); 1165 else if (C_DECL_IMPLICIT (decl)) 1166 diag (G_("previous implicit declaration of %q+D was here"), decl); 1167 else 1168 diag (G_("previous declaration of %q+D was here"), decl); 1169} 1170 1171/* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL. 1172 Returns true if the caller should proceed to merge the two, false 1173 if OLDDECL should simply be discarded. As a side effect, issues 1174 all necessary diagnostics for invalid or poor-style combinations. 1175 If it returns true, writes the types of NEWDECL and OLDDECL to 1176 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from 1177 TREE_TYPE (NEWDECL, OLDDECL) respectively. */ 1178 1179static bool 1180diagnose_mismatched_decls (tree newdecl, tree olddecl, 1181 tree *newtypep, tree *oldtypep) 1182{ 1183 tree newtype, oldtype; 1184 bool pedwarned = false; 1185 bool warned = false; 1186 bool retval = true; 1187 1188#define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \ 1189 && DECL_EXTERNAL (DECL)) 1190 1191 /* If we have error_mark_node for either decl or type, just discard 1192 the previous decl - we're in an error cascade already. */ 1193 if (olddecl == error_mark_node || newdecl == error_mark_node) 1194 return false; 1195 *oldtypep = oldtype = TREE_TYPE (olddecl); 1196 *newtypep = newtype = TREE_TYPE (newdecl); 1197 if (oldtype == error_mark_node || newtype == error_mark_node) 1198 return false; 1199 1200 /* Two different categories of symbol altogether. This is an error 1201 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */ 1202 if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) 1203 { 1204 if (!(TREE_CODE (olddecl) == FUNCTION_DECL 1205 && DECL_BUILT_IN (olddecl) 1206 && !C_DECL_DECLARED_BUILTIN (olddecl))) 1207 { 1208 error ("%q+D redeclared as different kind of symbol", newdecl); 1209 locate_old_decl (olddecl, error); 1210 } 1211 else if (TREE_PUBLIC (newdecl)) 1212 warning (0, "built-in function %q+D declared as non-function", 1213 newdecl); 1214 else 1215 warning (OPT_Wshadow, "declaration of %q+D shadows " 1216 "a built-in function", newdecl); 1217 return false; 1218 } 1219 1220 /* Enumerators have no linkage, so may only be declared once in a 1221 given scope. */ 1222 if (TREE_CODE (olddecl) == CONST_DECL) 1223 { 1224 error ("redeclaration of enumerator %q+D", newdecl); 1225 locate_old_decl (olddecl, error); 1226 return false; 1227 } 1228 1229 if (!comptypes (oldtype, newtype)) 1230 { 1231 if (TREE_CODE (olddecl) == FUNCTION_DECL 1232 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl)) 1233 { 1234 /* Accept harmless mismatch in function types. 1235 This is for the ffs and fprintf builtins. */ 1236 tree trytype = match_builtin_function_types (newtype, oldtype); 1237 1238 if (trytype && comptypes (newtype, trytype)) 1239 *oldtypep = oldtype = trytype; 1240 else 1241 { 1242 /* If types don't match for a built-in, throw away the 1243 built-in. No point in calling locate_old_decl here, it 1244 won't print anything. */ 1245 warning (0, "conflicting types for built-in function %q+D", 1246 newdecl); 1247 return false; 1248 } 1249 } 1250 else if (TREE_CODE (olddecl) == FUNCTION_DECL 1251 && DECL_IS_BUILTIN (olddecl)) 1252 { 1253 /* A conflicting function declaration for a predeclared 1254 function that isn't actually built in. Objective C uses 1255 these. The new declaration silently overrides everything 1256 but the volatility (i.e. noreturn) indication. See also 1257 below. FIXME: Make Objective C use normal builtins. */ 1258 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1259 return false; 1260 } 1261 /* Permit void foo (...) to match int foo (...) if the latter is 1262 the definition and implicit int was used. See 1263 c-torture/compile/920625-2.c. */ 1264 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) 1265 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node 1266 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node 1267 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl)) 1268 { 1269 pedwarn ("conflicting types for %q+D", newdecl); 1270 /* Make sure we keep void as the return type. */ 1271 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype; 1272 C_FUNCTION_IMPLICIT_INT (newdecl) = 0; 1273 pedwarned = true; 1274 } 1275 /* Permit void foo (...) to match an earlier call to foo (...) with 1276 no declared type (thus, implicitly int). */ 1277 else if (TREE_CODE (newdecl) == FUNCTION_DECL 1278 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node 1279 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node 1280 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl)) 1281 { 1282 pedwarn ("conflicting types for %q+D", newdecl); 1283 /* Make sure we keep void as the return type. */ 1284 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype; 1285 pedwarned = true; 1286 } 1287 else 1288 { 1289 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype)) 1290 error ("conflicting type qualifiers for %q+D", newdecl); 1291 else 1292 error ("conflicting types for %q+D", newdecl); 1293 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype); 1294 locate_old_decl (olddecl, error); 1295 return false; 1296 } 1297 } 1298 1299 /* Redeclaration of a type is a constraint violation (6.7.2.3p1), 1300 but silently ignore the redeclaration if either is in a system 1301 header. (Conflicting redeclarations were handled above.) */ 1302 if (TREE_CODE (newdecl) == TYPE_DECL) 1303 { 1304 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl)) 1305 return true; /* Allow OLDDECL to continue in use. */ 1306 1307 error ("redefinition of typedef %q+D", newdecl); 1308 locate_old_decl (olddecl, error); 1309 return false; 1310 } 1311 1312 /* Function declarations can either be 'static' or 'extern' (no 1313 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore 1314 can never conflict with each other on account of linkage 1315 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but 1316 gnu89 mode permits two definitions if one is 'extern inline' and 1317 one is not. The non- extern-inline definition supersedes the 1318 extern-inline definition. */ 1319 1320 else if (TREE_CODE (newdecl) == FUNCTION_DECL) 1321 { 1322 /* If you declare a built-in function name as static, or 1323 define the built-in with an old-style definition (so we 1324 can't validate the argument list) the built-in definition is 1325 overridden, but optionally warn this was a bad choice of name. */ 1326 if (DECL_BUILT_IN (olddecl) 1327 && !C_DECL_DECLARED_BUILTIN (olddecl) 1328 && (!TREE_PUBLIC (newdecl) 1329 || (DECL_INITIAL (newdecl) 1330 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl))))) 1331 { 1332 warning (OPT_Wshadow, "declaration of %q+D shadows " 1333 "a built-in function", newdecl); 1334 /* Discard the old built-in function. */ 1335 return false; 1336 } 1337 1338 if (DECL_INITIAL (newdecl)) 1339 { 1340 if (DECL_INITIAL (olddecl)) 1341 { 1342 /* If both decls are in the same TU and the new declaration 1343 isn't overriding an extern inline reject the new decl. 1344 In c99, no overriding is allowed in the same translation 1345 unit. */ 1346 if ((!DECL_EXTERN_INLINE (olddecl) 1347 || DECL_EXTERN_INLINE (newdecl) 1348 || (!flag_gnu89_inline 1349 && (!DECL_DECLARED_INLINE_P (olddecl) 1350 || !lookup_attribute ("gnu_inline", 1351 DECL_ATTRIBUTES (olddecl))) 1352 && (!DECL_DECLARED_INLINE_P (newdecl) 1353 || !lookup_attribute ("gnu_inline", 1354 DECL_ATTRIBUTES (newdecl)))) 1355 ) 1356 && same_translation_unit_p (newdecl, olddecl)) 1357 { 1358 error ("redefinition of %q+D", newdecl); 1359 locate_old_decl (olddecl, error); 1360 return false; 1361 } 1362 } 1363 } 1364 /* If we have a prototype after an old-style function definition, 1365 the argument types must be checked specially. */ 1366 else if (DECL_INITIAL (olddecl) 1367 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype) 1368 && TYPE_ACTUAL_ARG_TYPES (oldtype) 1369 && !validate_proto_after_old_defn (newdecl, newtype, oldtype)) 1370 { 1371 locate_old_decl (olddecl, error); 1372 return false; 1373 } 1374 /* A non-static declaration (even an "extern") followed by a 1375 static declaration is undefined behavior per C99 6.2.2p3-5,7. 1376 The same is true for a static forward declaration at block 1377 scope followed by a non-static declaration/definition at file 1378 scope. Static followed by non-static at the same scope is 1379 not undefined behavior, and is the most convenient way to get 1380 some effects (see e.g. what unwind-dw2-fde-glibc.c does to 1381 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but 1382 we do diagnose it if -Wtraditional. */ 1383 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl)) 1384 { 1385 /* Two exceptions to the rule. If olddecl is an extern 1386 inline, or a predeclared function that isn't actually 1387 built in, newdecl silently overrides olddecl. The latter 1388 occur only in Objective C; see also above. (FIXME: Make 1389 Objective C use normal builtins.) */ 1390 if (!DECL_IS_BUILTIN (olddecl) 1391 && !DECL_EXTERN_INLINE (olddecl)) 1392 { 1393 error ("static declaration of %q+D follows " 1394 "non-static declaration", newdecl); 1395 locate_old_decl (olddecl, error); 1396 } 1397 return false; 1398 } 1399 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)) 1400 { 1401 if (DECL_CONTEXT (olddecl)) 1402 { 1403 error ("non-static declaration of %q+D follows " 1404 "static declaration", newdecl); 1405 locate_old_decl (olddecl, error); 1406 return false; 1407 } 1408 else if (warn_traditional) 1409 { 1410 warning (OPT_Wtraditional, "non-static declaration of %q+D " 1411 "follows static declaration", newdecl); 1412 warned = true; 1413 } 1414 } 1415 1416 /* Make sure gnu_inline attribute is either not present, or 1417 present on all inline decls. */ 1418 if (DECL_DECLARED_INLINE_P (olddecl) 1419 && DECL_DECLARED_INLINE_P (newdecl)) 1420 { 1421 bool newa = lookup_attribute ("gnu_inline", 1422 DECL_ATTRIBUTES (newdecl)) != NULL; 1423 bool olda = lookup_attribute ("gnu_inline", 1424 DECL_ATTRIBUTES (olddecl)) != NULL; 1425 if (newa != olda) 1426 { 1427 error ("%<gnu_inline%> attribute present on %q+D", 1428 newa ? newdecl : olddecl); 1429 error ("%Jbut not here", newa ? olddecl : newdecl); 1430 } 1431 } 1432 } 1433 else if (TREE_CODE (newdecl) == VAR_DECL) 1434 { 1435 /* Only variables can be thread-local, and all declarations must 1436 agree on this property. */ 1437 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl)) 1438 { 1439 /* Nothing to check. Since OLDDECL is marked threadprivate 1440 and NEWDECL does not have a thread-local attribute, we 1441 will merge the threadprivate attribute into NEWDECL. */ 1442 ; 1443 } 1444 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)) 1445 { 1446 if (DECL_THREAD_LOCAL_P (newdecl)) 1447 error ("thread-local declaration of %q+D follows " 1448 "non-thread-local declaration", newdecl); 1449 else 1450 error ("non-thread-local declaration of %q+D follows " 1451 "thread-local declaration", newdecl); 1452 1453 locate_old_decl (olddecl, error); 1454 return false; 1455 } 1456 1457 /* Multiple initialized definitions are not allowed (6.9p3,5). */ 1458 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl)) 1459 { 1460 error ("redefinition of %q+D", newdecl); 1461 locate_old_decl (olddecl, error); 1462 return false; 1463 } 1464 1465 /* Objects declared at file scope: if the first declaration had 1466 external linkage (even if it was an external reference) the 1467 second must have external linkage as well, or the behavior is 1468 undefined. If the first declaration had internal linkage, then 1469 the second must too, or else be an external reference (in which 1470 case the composite declaration still has internal linkage). 1471 As for function declarations, we warn about the static-then- 1472 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */ 1473 if (DECL_FILE_SCOPE_P (newdecl) 1474 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl)) 1475 { 1476 if (DECL_EXTERNAL (newdecl)) 1477 { 1478 if (!DECL_FILE_SCOPE_P (olddecl)) 1479 { 1480 error ("extern declaration of %q+D follows " 1481 "declaration with no linkage", newdecl); 1482 locate_old_decl (olddecl, error); 1483 return false; 1484 } 1485 else if (warn_traditional) 1486 { 1487 warning (OPT_Wtraditional, "non-static declaration of %q+D " 1488 "follows static declaration", newdecl); 1489 warned = true; 1490 } 1491 } 1492 else 1493 { 1494 if (TREE_PUBLIC (newdecl)) 1495 error ("non-static declaration of %q+D follows " 1496 "static declaration", newdecl); 1497 else 1498 error ("static declaration of %q+D follows " 1499 "non-static declaration", newdecl); 1500 1501 locate_old_decl (olddecl, error); 1502 return false; 1503 } 1504 } 1505 /* Two objects with the same name declared at the same block 1506 scope must both be external references (6.7p3). */ 1507 else if (!DECL_FILE_SCOPE_P (newdecl)) 1508 { 1509 if (DECL_EXTERNAL (newdecl)) 1510 { 1511 /* Extern with initializer at block scope, which will 1512 already have received an error. */ 1513 } 1514 else if (DECL_EXTERNAL (olddecl)) 1515 { 1516 error ("declaration of %q+D with no linkage follows " 1517 "extern declaration", newdecl); 1518 locate_old_decl (olddecl, error); 1519 } 1520 else 1521 { 1522 error ("redeclaration of %q+D with no linkage", newdecl); 1523 locate_old_decl (olddecl, error); 1524 } 1525 1526 return false; 1527 } 1528 } 1529 1530 /* warnings */ 1531 /* All decls must agree on a visibility. */ 1532 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS) 1533 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl) 1534 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) 1535 { 1536 warning (0, "redeclaration of %q+D with different visibility " 1537 "(old visibility preserved)", newdecl); 1538 warned = true; 1539 } 1540 1541 if (TREE_CODE (newdecl) == FUNCTION_DECL) 1542 { 1543 /* Diagnose inline __attribute__ ((noinline)) which is silly. */ 1544 if (DECL_DECLARED_INLINE_P (newdecl) 1545 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 1546 { 1547 warning (OPT_Wattributes, "inline declaration of %qD follows " 1548 "declaration with attribute noinline", newdecl); 1549 warned = true; 1550 } 1551 else if (DECL_DECLARED_INLINE_P (olddecl) 1552 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) 1553 { 1554 warning (OPT_Wattributes, "declaration of %q+D with attribute " 1555 "noinline follows inline declaration ", newdecl); 1556 warned = true; 1557 } 1558 1559 /* Inline declaration after use or definition. 1560 ??? Should we still warn about this now we have unit-at-a-time 1561 mode and can get it right? 1562 Definitely don't complain if the decls are in different translation 1563 units. 1564 C99 permits this, so don't warn in that case. (The function 1565 may not be inlined everywhere in function-at-a-time mode, but 1566 we still shouldn't warn.) */ 1567 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl) 1568 && same_translation_unit_p (olddecl, newdecl) 1569 && flag_gnu89_inline) 1570 { 1571 if (TREE_USED (olddecl)) 1572 { 1573 warning (0, "%q+D declared inline after being called", olddecl); 1574 warned = true; 1575 } 1576 else if (DECL_INITIAL (olddecl)) 1577 { 1578 warning (0, "%q+D declared inline after its definition", olddecl); 1579 warned = true; 1580 } 1581 } 1582 } 1583 else /* PARM_DECL, VAR_DECL */ 1584 { 1585 /* Redeclaration of a parameter is a constraint violation (this is 1586 not explicitly stated, but follows from C99 6.7p3 [no more than 1587 one declaration of the same identifier with no linkage in the 1588 same scope, except type tags] and 6.2.2p6 [parameters have no 1589 linkage]). We must check for a forward parameter declaration, 1590 indicated by TREE_ASM_WRITTEN on the old declaration - this is 1591 an extension, the mandatory diagnostic for which is handled by 1592 mark_forward_parm_decls. */ 1593 1594 if (TREE_CODE (newdecl) == PARM_DECL 1595 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl))) 1596 { 1597 error ("redefinition of parameter %q+D", newdecl); 1598 locate_old_decl (olddecl, error); 1599 return false; 1600 } 1601 } 1602 1603 /* Optional warning for completely redundant decls. */ 1604 if (!warned && !pedwarned 1605 && warn_redundant_decls 1606 /* Don't warn about a function declaration followed by a 1607 definition. */ 1608 && !(TREE_CODE (newdecl) == FUNCTION_DECL 1609 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)) 1610 /* Don't warn about redundant redeclarations of builtins. */ 1611 && !(TREE_CODE (newdecl) == FUNCTION_DECL 1612 && !DECL_BUILT_IN (newdecl) 1613 && DECL_BUILT_IN (olddecl) 1614 && !C_DECL_DECLARED_BUILTIN (olddecl)) 1615 /* Don't warn about an extern followed by a definition. */ 1616 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl)) 1617 /* Don't warn about forward parameter decls. */ 1618 && !(TREE_CODE (newdecl) == PARM_DECL 1619 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)) 1620 /* Don't warn about a variable definition following a declaration. */ 1621 && !(TREE_CODE (newdecl) == VAR_DECL 1622 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))) 1623 { 1624 warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D", 1625 newdecl); 1626 warned = true; 1627 } 1628 1629 /* Report location of previous decl/defn in a consistent manner. */ 1630 if (warned || pedwarned) 1631 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0); 1632 1633#undef DECL_EXTERN_INLINE 1634 1635 return retval; 1636} 1637 1638/* Subroutine of duplicate_decls. NEWDECL has been found to be 1639 consistent with OLDDECL, but carries new information. Merge the 1640 new information into OLDDECL. This function issues no 1641 diagnostics. */ 1642 1643static void 1644merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype) 1645{ 1646 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL 1647 && DECL_INITIAL (newdecl) != 0); 1648 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL 1649 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0); 1650 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL 1651 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0); 1652 bool extern_changed = false; 1653 1654 /* For real parm decl following a forward decl, rechain the old decl 1655 in its new location and clear TREE_ASM_WRITTEN (it's not a 1656 forward decl anymore). */ 1657 if (TREE_CODE (newdecl) == PARM_DECL 1658 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)) 1659 { 1660 struct c_binding *b, **here; 1661 1662 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev) 1663 if ((*here)->decl == olddecl) 1664 goto found; 1665 gcc_unreachable (); 1666 1667 found: 1668 b = *here; 1669 *here = b->prev; 1670 b->prev = current_scope->bindings; 1671 current_scope->bindings = b; 1672 1673 TREE_ASM_WRITTEN (olddecl) = 0; 1674 } 1675 1676 DECL_ATTRIBUTES (newdecl) 1677 = targetm.merge_decl_attributes (olddecl, newdecl); 1678 1679 /* Merge the data types specified in the two decls. */ 1680 TREE_TYPE (newdecl) 1681 = TREE_TYPE (olddecl) 1682 = composite_type (newtype, oldtype); 1683 1684 /* Lay the type out, unless already done. */ 1685 if (!comptypes (oldtype, TREE_TYPE (newdecl))) 1686 { 1687 if (TREE_TYPE (newdecl) != error_mark_node) 1688 layout_type (TREE_TYPE (newdecl)); 1689 if (TREE_CODE (newdecl) != FUNCTION_DECL 1690 && TREE_CODE (newdecl) != TYPE_DECL 1691 && TREE_CODE (newdecl) != CONST_DECL) 1692 layout_decl (newdecl, 0); 1693 } 1694 else 1695 { 1696 /* Since the type is OLDDECL's, make OLDDECL's size go with. */ 1697 DECL_SIZE (newdecl) = DECL_SIZE (olddecl); 1698 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl); 1699 DECL_MODE (newdecl) = DECL_MODE (olddecl); 1700 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl)) 1701 { 1702 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl); 1703 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl); 1704 } 1705 } 1706 1707 1708 /* Merge the type qualifiers. */ 1709 if (TREE_READONLY (newdecl)) 1710 TREE_READONLY (olddecl) = 1; 1711 1712 if (TREE_THIS_VOLATILE (newdecl)) 1713 TREE_THIS_VOLATILE (olddecl) = 1; 1714 1715 /* Merge deprecatedness. */ 1716 if (TREE_DEPRECATED (newdecl)) 1717 TREE_DEPRECATED (olddecl) = 1; 1718 1719 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */ 1720 /* Merge unavailableness. */ 1721 if (TREE_UNAVAILABLE (newdecl)) 1722 TREE_UNAVAILABLE (olddecl) = 1; 1723 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */ 1724 1725 /* Keep source location of definition rather than declaration and of 1726 prototype rather than non-prototype unless that prototype is 1727 built-in. */ 1728 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0) 1729 || (old_is_prototype && !new_is_prototype 1730 && !C_DECL_BUILTIN_PROTOTYPE (olddecl))) 1731 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); 1732 1733 /* Merge the initialization information. */ 1734 if (DECL_INITIAL (newdecl) == 0) 1735 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 1736 1737 /* Merge the threadprivate attribute. */ 1738 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl)) 1739 { 1740 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl); 1741 C_DECL_THREADPRIVATE_P (newdecl) = 1; 1742 } 1743 1744 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)) 1745 { 1746 /* Merge the unused-warning information. */ 1747 if (DECL_IN_SYSTEM_HEADER (olddecl)) 1748 DECL_IN_SYSTEM_HEADER (newdecl) = 1; 1749 else if (DECL_IN_SYSTEM_HEADER (newdecl)) 1750 DECL_IN_SYSTEM_HEADER (olddecl) = 1; 1751 1752 /* Merge the section attribute. 1753 We want to issue an error if the sections conflict but that 1754 must be done later in decl_attributes since we are called 1755 before attributes are assigned. */ 1756 if (DECL_SECTION_NAME (newdecl) == NULL_TREE) 1757 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); 1758 1759 /* Copy the assembler name. 1760 Currently, it can only be defined in the prototype. */ 1761 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); 1762 1763 /* Use visibility of whichever declaration had it specified */ 1764 if (DECL_VISIBILITY_SPECIFIED (olddecl)) 1765 { 1766 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); 1767 DECL_VISIBILITY_SPECIFIED (newdecl) = 1; 1768 } 1769 1770 if (TREE_CODE (newdecl) == FUNCTION_DECL) 1771 { 1772 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl); 1773 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); 1774 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); 1775 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) 1776 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); 1777 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1778 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); 1779 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); 1780 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); 1781 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl); 1782 } 1783 1784 /* Merge the storage class information. */ 1785 merge_weak (newdecl, olddecl); 1786 1787 /* For functions, static overrides non-static. */ 1788 if (TREE_CODE (newdecl) == FUNCTION_DECL) 1789 { 1790 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl); 1791 /* This is since we don't automatically 1792 copy the attributes of NEWDECL into OLDDECL. */ 1793 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 1794 /* If this clears `static', clear it in the identifier too. */ 1795 if (!TREE_PUBLIC (olddecl)) 1796 TREE_PUBLIC (DECL_NAME (olddecl)) = 0; 1797 } 1798 } 1799 1800 /* In c99, 'extern' declaration before (or after) 'inline' means this 1801 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute 1802 is present. */ 1803 if (TREE_CODE (newdecl) == FUNCTION_DECL 1804 && !flag_gnu89_inline 1805 && (DECL_DECLARED_INLINE_P (newdecl) 1806 || DECL_DECLARED_INLINE_P (olddecl)) 1807 && (!DECL_DECLARED_INLINE_P (newdecl) 1808 || !DECL_DECLARED_INLINE_P (olddecl) 1809 || !DECL_EXTERNAL (olddecl)) 1810 && DECL_EXTERNAL (newdecl) 1811 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))) 1812 DECL_EXTERNAL (newdecl) = 0; 1813 1814 if (DECL_EXTERNAL (newdecl)) 1815 { 1816 TREE_STATIC (newdecl) = TREE_STATIC (olddecl); 1817 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl); 1818 1819 /* An extern decl does not override previous storage class. */ 1820 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); 1821 if (!DECL_EXTERNAL (newdecl)) 1822 { 1823 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); 1824 DECL_COMMON (newdecl) = DECL_COMMON (olddecl); 1825 } 1826 } 1827 else 1828 { 1829 TREE_STATIC (olddecl) = TREE_STATIC (newdecl); 1830 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 1831 } 1832 1833 if (TREE_CODE (newdecl) == FUNCTION_DECL) 1834 { 1835 /* If we're redefining a function previously defined as extern 1836 inline, make sure we emit debug info for the inline before we 1837 throw it away, in case it was inlined into a function that 1838 hasn't been written out yet. */ 1839 if (new_is_definition && DECL_INITIAL (olddecl)) 1840 { 1841 if (TREE_USED (olddecl) 1842 /* In unit-at-a-time mode we never inline re-defined extern 1843 inline functions. */ 1844 && !flag_unit_at_a_time 1845 && cgraph_function_possibly_inlined_p (olddecl)) 1846 (*debug_hooks->outlining_inline_function) (olddecl); 1847 1848 /* The new defn must not be inline. */ 1849 DECL_INLINE (newdecl) = 0; 1850 DECL_UNINLINABLE (newdecl) = 1; 1851 } 1852 else 1853 { 1854 /* If either decl says `inline', this fn is inline, unless 1855 its definition was passed already. */ 1856 if (DECL_DECLARED_INLINE_P (newdecl) 1857 || DECL_DECLARED_INLINE_P (olddecl)) 1858 DECL_DECLARED_INLINE_P (newdecl) = 1; 1859 1860 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) 1861 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); 1862 } 1863 1864 if (DECL_BUILT_IN (olddecl)) 1865 { 1866 /* If redeclaring a builtin function, it stays built in. 1867 But it gets tagged as having been declared. */ 1868 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); 1869 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); 1870 C_DECL_DECLARED_BUILTIN (newdecl) = 1; 1871 if (new_is_prototype) 1872 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0; 1873 else 1874 C_DECL_BUILTIN_PROTOTYPE (newdecl) 1875 = C_DECL_BUILTIN_PROTOTYPE (olddecl); 1876 } 1877 1878 /* Also preserve various other info from the definition. */ 1879 if (!new_is_definition) 1880 { 1881 DECL_RESULT (newdecl) = DECL_RESULT (olddecl); 1882 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 1883 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl); 1884 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); 1885 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); 1886 1887 /* Set DECL_INLINE on the declaration if we've got a body 1888 from which to instantiate. */ 1889 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl)) 1890 { 1891 DECL_INLINE (newdecl) = 1; 1892 DECL_ABSTRACT_ORIGIN (newdecl) 1893 = DECL_ABSTRACT_ORIGIN (olddecl); 1894 } 1895 } 1896 else 1897 { 1898 /* If a previous declaration said inline, mark the 1899 definition as inlinable. */ 1900 if (DECL_DECLARED_INLINE_P (newdecl) 1901 && !DECL_UNINLINABLE (newdecl)) 1902 DECL_INLINE (newdecl) = 1; 1903 } 1904 } 1905 1906 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl); 1907 1908 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL. 1909 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */ 1910 { 1911 unsigned olddecl_uid = DECL_UID (olddecl); 1912 tree olddecl_context = DECL_CONTEXT (olddecl); 1913 1914 memcpy ((char *) olddecl + sizeof (struct tree_common), 1915 (char *) newdecl + sizeof (struct tree_common), 1916 sizeof (struct tree_decl_common) - sizeof (struct tree_common)); 1917 switch (TREE_CODE (olddecl)) 1918 { 1919 case FIELD_DECL: 1920 case VAR_DECL: 1921 case PARM_DECL: 1922 case LABEL_DECL: 1923 case RESULT_DECL: 1924 case CONST_DECL: 1925 case TYPE_DECL: 1926 case FUNCTION_DECL: 1927 memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 1928 (char *) newdecl + sizeof (struct tree_decl_common), 1929 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common)); 1930 break; 1931 1932 default: 1933 1934 memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 1935 (char *) newdecl + sizeof (struct tree_decl_common), 1936 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)); 1937 } 1938 DECL_UID (olddecl) = olddecl_uid; 1939 DECL_CONTEXT (olddecl) = olddecl_context; 1940 } 1941 1942 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl 1943 so that encode_section_info has a chance to look at the new decl 1944 flags and attributes. */ 1945 if (DECL_RTL_SET_P (olddecl) 1946 && (TREE_CODE (olddecl) == FUNCTION_DECL 1947 || (TREE_CODE (olddecl) == VAR_DECL 1948 && TREE_STATIC (olddecl)))) 1949 make_decl_rtl (olddecl); 1950 1951 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL, 1952 and the definition is coming from the old version, cgraph needs 1953 to be called again. */ 1954 if (extern_changed && !new_is_definition 1955 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl)) 1956 cgraph_finalize_function (olddecl, false); 1957} 1958 1959/* Handle when a new declaration NEWDECL has the same name as an old 1960 one OLDDECL in the same binding contour. Prints an error message 1961 if appropriate. 1962 1963 If safely possible, alter OLDDECL to look like NEWDECL, and return 1964 true. Otherwise, return false. */ 1965 1966static bool 1967duplicate_decls (tree newdecl, tree olddecl) 1968{ 1969 tree newtype = NULL, oldtype = NULL; 1970 1971 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype)) 1972 { 1973 /* Avoid `unused variable' and other warnings warnings for OLDDECL. */ 1974 TREE_NO_WARNING (olddecl) = 1; 1975 return false; 1976 } 1977 1978 merge_decls (newdecl, olddecl, newtype, oldtype); 1979 return true; 1980} 1981 1982 1983/* Check whether decl-node NEW_DECL shadows an existing declaration. */ 1984static void 1985warn_if_shadowing (tree new_decl) 1986{ 1987 struct c_binding *b; 1988 1989 /* Shadow warnings wanted? */ 1990 if (!warn_shadow 1991 /* No shadow warnings for internally generated vars. */ 1992 || DECL_IS_BUILTIN (new_decl) 1993 /* No shadow warnings for vars made for inlining. */ 1994 || DECL_FROM_INLINE (new_decl)) 1995 return; 1996 1997 /* Is anything being shadowed? Invisible decls do not count. */ 1998 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed) 1999 if (b->decl && b->decl != new_decl && !b->invisible) 2000 { 2001 tree old_decl = b->decl; 2002 2003 if (old_decl == error_mark_node) 2004 { 2005 warning (OPT_Wshadow, "declaration of %q+D shadows previous " 2006 "non-variable", new_decl); 2007 break; 2008 } 2009 else if (TREE_CODE (old_decl) == PARM_DECL) 2010 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter", 2011 new_decl); 2012 else if (DECL_FILE_SCOPE_P (old_decl)) 2013 warning (OPT_Wshadow, "declaration of %q+D shadows a global " 2014 "declaration", new_decl); 2015 else if (TREE_CODE (old_decl) == FUNCTION_DECL 2016 && DECL_BUILT_IN (old_decl)) 2017 { 2018 warning (OPT_Wshadow, "declaration of %q+D shadows " 2019 "a built-in function", new_decl); 2020 break; 2021 } 2022 else 2023 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local", 2024 new_decl); 2025 2026 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl); 2027 2028 break; 2029 } 2030} 2031 2032 2033/* Subroutine of pushdecl. 2034 2035 X is a TYPE_DECL for a typedef statement. Create a brand new 2036 ..._TYPE node (which will be just a variant of the existing 2037 ..._TYPE node with identical properties) and then install X 2038 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. 2039 2040 The whole point here is to end up with a situation where each 2041 and every ..._TYPE node the compiler creates will be uniquely 2042 associated with AT MOST one node representing a typedef name. 2043 This way, even though the compiler substitutes corresponding 2044 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very 2045 early on, later parts of the compiler can always do the reverse 2046 translation and get back the corresponding typedef name. For 2047 example, given: 2048 2049 typedef struct S MY_TYPE; 2050 MY_TYPE object; 2051 2052 Later parts of the compiler might only know that `object' was of 2053 type `struct S' if it were not for code just below. With this 2054 code however, later parts of the compiler see something like: 2055 2056 struct S' == struct S 2057 typedef struct S' MY_TYPE; 2058 struct S' object; 2059 2060 And they can then deduce (from the node for type struct S') that 2061 the original object declaration was: 2062 2063 MY_TYPE object; 2064 2065 Being able to do this is important for proper support of protoize, 2066 and also for generating precise symbolic debugging information 2067 which takes full account of the programmer's (typedef) vocabulary. 2068 2069 Obviously, we don't want to generate a duplicate ..._TYPE node if 2070 the TYPE_DECL node that we are now processing really represents a 2071 standard built-in type. 2072 2073 Since all standard types are effectively declared at line zero 2074 in the source file, we can easily check to see if we are working 2075 on a standard type by checking the current value of lineno. */ 2076 2077static void 2078clone_underlying_type (tree x) 2079{ 2080 if (DECL_IS_BUILTIN (x)) 2081 { 2082 if (TYPE_NAME (TREE_TYPE (x)) == 0) 2083 TYPE_NAME (TREE_TYPE (x)) = x; 2084 } 2085 else if (TREE_TYPE (x) != error_mark_node 2086 && DECL_ORIGINAL_TYPE (x) == NULL_TREE) 2087 { 2088 tree tt = TREE_TYPE (x); 2089 DECL_ORIGINAL_TYPE (x) = tt; 2090 tt = build_variant_type_copy (tt); 2091 TYPE_NAME (tt) = x; 2092 TREE_USED (tt) = TREE_USED (x); 2093 TREE_TYPE (x) = tt; 2094 } 2095} 2096 2097/* Record a decl-node X as belonging to the current lexical scope. 2098 Check for errors (such as an incompatible declaration for the same 2099 name already seen in the same scope). 2100 2101 Returns either X or an old decl for the same name. 2102 If an old decl is returned, it may have been smashed 2103 to agree with what X says. */ 2104 2105tree 2106pushdecl (tree x) 2107{ 2108 tree name = DECL_NAME (x); 2109 struct c_scope *scope = current_scope; 2110 struct c_binding *b; 2111 bool nested = false; 2112 2113 /* Functions need the lang_decl data. */ 2114 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x)) 2115 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl); 2116 2117 /* Must set DECL_CONTEXT for everything not at file scope or 2118 DECL_FILE_SCOPE_P won't work. Local externs don't count 2119 unless they have initializers (which generate code). */ 2120 if (current_function_decl 2121 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL) 2122 || DECL_INITIAL (x) || !DECL_EXTERNAL (x))) 2123 DECL_CONTEXT (x) = current_function_decl; 2124 2125 /* If this is of variably modified type, prevent jumping into its 2126 scope. */ 2127 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL) 2128 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE)) 2129 c_begin_vm_scope (scope->depth); 2130 2131 /* Anonymous decls are just inserted in the scope. */ 2132 if (!name) 2133 { 2134 bind (name, x, scope, /*invisible=*/false, /*nested=*/false); 2135 return x; 2136 } 2137 2138 /* First, see if there is another declaration with the same name in 2139 the current scope. If there is, duplicate_decls may do all the 2140 work for us. If duplicate_decls returns false, that indicates 2141 two incompatible decls in the same scope; we are to silently 2142 replace the old one (duplicate_decls has issued all appropriate 2143 diagnostics). In particular, we should not consider possible 2144 duplicates in the external scope, or shadowing. */ 2145 b = I_SYMBOL_BINDING (name); 2146 if (b && B_IN_SCOPE (b, scope)) 2147 { 2148 struct c_binding *b_ext, *b_use; 2149 tree type = TREE_TYPE (x); 2150 tree visdecl = b->decl; 2151 tree vistype = TREE_TYPE (visdecl); 2152 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 2153 && COMPLETE_TYPE_P (TREE_TYPE (x))) 2154 b->inner_comp = false; 2155 b_use = b; 2156 b_ext = b; 2157 /* If this is an external linkage declaration, we should check 2158 for compatibility with the type in the external scope before 2159 setting the type at this scope based on the visible 2160 information only. */ 2161 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl)) 2162 { 2163 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext)) 2164 b_ext = b_ext->shadowed; 2165 if (b_ext) 2166 { 2167 b_use = b_ext; 2168 if (b_use->type) 2169 TREE_TYPE (b_use->decl) = b_use->type; 2170 } 2171 } 2172 if (duplicate_decls (x, b_use->decl)) 2173 { 2174 if (b_use != b) 2175 { 2176 /* Save the updated type in the external scope and 2177 restore the proper type for this scope. */ 2178 tree thistype; 2179 if (comptypes (vistype, type)) 2180 thistype = composite_type (vistype, type); 2181 else 2182 thistype = TREE_TYPE (b_use->decl); 2183 b_use->type = TREE_TYPE (b_use->decl); 2184 if (TREE_CODE (b_use->decl) == FUNCTION_DECL 2185 && DECL_BUILT_IN (b_use->decl)) 2186 thistype 2187 = build_type_attribute_variant (thistype, 2188 TYPE_ATTRIBUTES 2189 (b_use->type)); 2190 TREE_TYPE (b_use->decl) = thistype; 2191 } 2192 return b_use->decl; 2193 } 2194 else 2195 goto skip_external_and_shadow_checks; 2196 } 2197 2198 /* All declarations with external linkage, and all external 2199 references, go in the external scope, no matter what scope is 2200 current. However, the binding in that scope is ignored for 2201 purposes of normal name lookup. A separate binding structure is 2202 created in the requested scope; this governs the normal 2203 visibility of the symbol. 2204 2205 The binding in the externals scope is used exclusively for 2206 detecting duplicate declarations of the same object, no matter 2207 what scope they are in; this is what we do here. (C99 6.2.7p2: 2208 All declarations that refer to the same object or function shall 2209 have compatible type; otherwise, the behavior is undefined.) */ 2210 if (DECL_EXTERNAL (x) || scope == file_scope) 2211 { 2212 tree type = TREE_TYPE (x); 2213 tree vistype = 0; 2214 tree visdecl = 0; 2215 bool type_saved = false; 2216 if (b && !B_IN_EXTERNAL_SCOPE (b) 2217 && (TREE_CODE (b->decl) == FUNCTION_DECL 2218 || TREE_CODE (b->decl) == VAR_DECL) 2219 && DECL_FILE_SCOPE_P (b->decl)) 2220 { 2221 visdecl = b->decl; 2222 vistype = TREE_TYPE (visdecl); 2223 } 2224 if (scope != file_scope 2225 && !DECL_IN_SYSTEM_HEADER (x)) 2226 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x); 2227 2228 while (b && !B_IN_EXTERNAL_SCOPE (b)) 2229 { 2230 /* If this decl might be modified, save its type. This is 2231 done here rather than when the decl is first bound 2232 because the type may change after first binding, through 2233 being completed or through attributes being added. If we 2234 encounter multiple such decls, only the first should have 2235 its type saved; the others will already have had their 2236 proper types saved and the types will not have changed as 2237 their scopes will not have been re-entered. */ 2238 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved) 2239 { 2240 b->type = TREE_TYPE (b->decl); 2241 type_saved = true; 2242 } 2243 if (B_IN_FILE_SCOPE (b) 2244 && TREE_CODE (b->decl) == VAR_DECL 2245 && TREE_STATIC (b->decl) 2246 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE 2247 && !TYPE_DOMAIN (TREE_TYPE (b->decl)) 2248 && TREE_CODE (type) == ARRAY_TYPE 2249 && TYPE_DOMAIN (type) 2250 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) 2251 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) 2252 { 2253 /* Array type completed in inner scope, which should be 2254 diagnosed if the completion does not have size 1 and 2255 it does not get completed in the file scope. */ 2256 b->inner_comp = true; 2257 } 2258 b = b->shadowed; 2259 } 2260 2261 /* If a matching external declaration has been found, set its 2262 type to the composite of all the types of that declaration. 2263 After the consistency checks, it will be reset to the 2264 composite of the visible types only. */ 2265 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl)) 2266 && b->type) 2267 TREE_TYPE (b->decl) = b->type; 2268 2269 /* The point of the same_translation_unit_p check here is, 2270 we want to detect a duplicate decl for a construct like 2271 foo() { extern bar(); } ... static bar(); but not if 2272 they are in different translation units. In any case, 2273 the static does not go in the externals scope. */ 2274 if (b 2275 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl)) 2276 && duplicate_decls (x, b->decl)) 2277 { 2278 tree thistype; 2279 if (vistype) 2280 { 2281 if (comptypes (vistype, type)) 2282 thistype = composite_type (vistype, type); 2283 else 2284 thistype = TREE_TYPE (b->decl); 2285 } 2286 else 2287 thistype = type; 2288 b->type = TREE_TYPE (b->decl); 2289 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl)) 2290 thistype 2291 = build_type_attribute_variant (thistype, 2292 TYPE_ATTRIBUTES (b->type)); 2293 TREE_TYPE (b->decl) = thistype; 2294 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true); 2295 return b->decl; 2296 } 2297 else if (TREE_PUBLIC (x)) 2298 { 2299 if (visdecl && !b && duplicate_decls (x, visdecl)) 2300 { 2301 /* An external declaration at block scope referring to a 2302 visible entity with internal linkage. The composite 2303 type will already be correct for this scope, so we 2304 just need to fall through to make the declaration in 2305 this scope. */ 2306 nested = true; 2307 x = visdecl; 2308 } 2309 else 2310 { 2311 bind (name, x, external_scope, /*invisible=*/true, 2312 /*nested=*/false); 2313 nested = true; 2314 } 2315 } 2316 } 2317 2318 if (TREE_CODE (x) != PARM_DECL) 2319 warn_if_shadowing (x); 2320 2321 skip_external_and_shadow_checks: 2322 if (TREE_CODE (x) == TYPE_DECL) 2323 clone_underlying_type (x); 2324 2325 bind (name, x, scope, /*invisible=*/false, nested); 2326 2327 /* If x's type is incomplete because it's based on a 2328 structure or union which has not yet been fully declared, 2329 attach it to that structure or union type, so we can go 2330 back and complete the variable declaration later, if the 2331 structure or union gets fully declared. 2332 2333 If the input is erroneous, we can have error_mark in the type 2334 slot (e.g. "f(void a, ...)") - that doesn't count as an 2335 incomplete type. */ 2336 if (TREE_TYPE (x) != error_mark_node 2337 && !COMPLETE_TYPE_P (TREE_TYPE (x))) 2338 { 2339 tree element = TREE_TYPE (x); 2340 2341 while (TREE_CODE (element) == ARRAY_TYPE) 2342 element = TREE_TYPE (element); 2343 element = TYPE_MAIN_VARIANT (element); 2344 2345 if ((TREE_CODE (element) == RECORD_TYPE 2346 || TREE_CODE (element) == UNION_TYPE) 2347 && (TREE_CODE (x) != TYPE_DECL 2348 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE) 2349 && !COMPLETE_TYPE_P (element)) 2350 C_TYPE_INCOMPLETE_VARS (element) 2351 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element)); 2352 } 2353 return x; 2354} 2355 2356/* Record X as belonging to file scope. 2357 This is used only internally by the Objective-C front end, 2358 and is limited to its needs. duplicate_decls is not called; 2359 if there is any preexisting decl for this identifier, it is an ICE. */ 2360 2361tree 2362pushdecl_top_level (tree x) 2363{ 2364 tree name; 2365 bool nested = false; 2366 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL); 2367 2368 name = DECL_NAME (x); 2369 2370 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name)); 2371 2372 if (TREE_PUBLIC (x)) 2373 { 2374 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false); 2375 nested = true; 2376 } 2377 if (file_scope) 2378 bind (name, x, file_scope, /*invisible=*/false, nested); 2379 2380 return x; 2381} 2382 2383static void 2384implicit_decl_warning (tree id, tree olddecl) 2385{ 2386 void (*diag) (const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2); 2387 switch (mesg_implicit_function_declaration) 2388 { 2389 case 0: return; 2390 case 1: diag = warning0; break; 2391 case 2: diag = error; break; 2392 default: gcc_unreachable (); 2393 } 2394 2395 diag (G_("implicit declaration of function %qE"), id); 2396 if (olddecl) 2397 locate_old_decl (olddecl, diag); 2398} 2399 2400/* Generate an implicit declaration for identifier FUNCTIONID as a 2401 function of type int (). */ 2402 2403tree 2404implicitly_declare (tree functionid) 2405{ 2406 struct c_binding *b; 2407 tree decl = 0; 2408 tree asmspec_tree; 2409 2410 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed) 2411 { 2412 if (B_IN_SCOPE (b, external_scope)) 2413 { 2414 decl = b->decl; 2415 break; 2416 } 2417 } 2418 2419 if (decl) 2420 { 2421 if (decl == error_mark_node) 2422 return decl; 2423 2424 /* FIXME: Objective-C has weird not-really-builtin functions 2425 which are supposed to be visible automatically. They wind up 2426 in the external scope because they're pushed before the file 2427 scope gets created. Catch this here and rebind them into the 2428 file scope. */ 2429 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl)) 2430 { 2431 bind (functionid, decl, file_scope, 2432 /*invisible=*/false, /*nested=*/true); 2433 return decl; 2434 } 2435 else 2436 { 2437 tree newtype = default_function_type; 2438 if (b->type) 2439 TREE_TYPE (decl) = b->type; 2440 /* Implicit declaration of a function already declared 2441 (somehow) in a different scope, or as a built-in. 2442 If this is the first time this has happened, warn; 2443 then recycle the old declaration but with the new type. */ 2444 if (!C_DECL_IMPLICIT (decl)) 2445 { 2446 implicit_decl_warning (functionid, decl); 2447 C_DECL_IMPLICIT (decl) = 1; 2448 } 2449 if (DECL_BUILT_IN (decl)) 2450 { 2451 newtype = build_type_attribute_variant (newtype, 2452 TYPE_ATTRIBUTES 2453 (TREE_TYPE (decl))); 2454 if (!comptypes (newtype, TREE_TYPE (decl))) 2455 { 2456 warning (0, "incompatible implicit declaration of built-in" 2457 " function %qD", decl); 2458 newtype = TREE_TYPE (decl); 2459 } 2460 } 2461 else 2462 { 2463 if (!comptypes (newtype, TREE_TYPE (decl))) 2464 { 2465 error ("incompatible implicit declaration of function %qD", 2466 decl); 2467 locate_old_decl (decl, error); 2468 } 2469 } 2470 b->type = TREE_TYPE (decl); 2471 TREE_TYPE (decl) = newtype; 2472 bind (functionid, decl, current_scope, 2473 /*invisible=*/false, /*nested=*/true); 2474 return decl; 2475 } 2476 } 2477 2478 /* Not seen before. */ 2479 decl = build_decl (FUNCTION_DECL, functionid, default_function_type); 2480 DECL_EXTERNAL (decl) = 1; 2481 TREE_PUBLIC (decl) = 1; 2482 C_DECL_IMPLICIT (decl) = 1; 2483 implicit_decl_warning (functionid, 0); 2484 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL); 2485 if (asmspec_tree) 2486 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree)); 2487 2488 /* C89 says implicit declarations are in the innermost block. 2489 So we record the decl in the standard fashion. */ 2490 decl = pushdecl (decl); 2491 2492 /* No need to call objc_check_decl here - it's a function type. */ 2493 rest_of_decl_compilation (decl, 0, 0); 2494 2495 /* Write a record describing this implicit function declaration 2496 to the prototypes file (if requested). */ 2497 gen_aux_info_record (decl, 0, 1, 0); 2498 2499 /* Possibly apply some default attributes to this implicit declaration. */ 2500 decl_attributes (&decl, NULL_TREE, 0); 2501 2502 return decl; 2503} 2504 2505/* Issue an error message for a reference to an undeclared variable 2506 ID, including a reference to a builtin outside of function-call 2507 context. Establish a binding of the identifier to error_mark_node 2508 in an appropriate scope, which will suppress further errors for the 2509 same identifier. The error message should be given location LOC. */ 2510void 2511undeclared_variable (tree id, location_t loc) 2512{ 2513 static bool already = false; 2514 struct c_scope *scope; 2515 2516 if (current_function_decl == 0) 2517 { 2518 error ("%H%qE undeclared here (not in a function)", &loc, id); 2519 scope = current_scope; 2520 } 2521 else 2522 { 2523 error ("%H%qE undeclared (first use in this function)", &loc, id); 2524 2525 if (!already) 2526 { 2527 error ("%H(Each undeclared identifier is reported only once", &loc); 2528 error ("%Hfor each function it appears in.)", &loc); 2529 already = true; 2530 } 2531 2532 /* If we are parsing old-style parameter decls, current_function_decl 2533 will be nonnull but current_function_scope will be null. */ 2534 scope = current_function_scope ? current_function_scope : current_scope; 2535 } 2536 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false); 2537} 2538 2539/* Subroutine of lookup_label, declare_label, define_label: construct a 2540 LABEL_DECL with all the proper frills. */ 2541 2542static tree 2543make_label (tree name, location_t location) 2544{ 2545 tree label = build_decl (LABEL_DECL, name, void_type_node); 2546 2547 DECL_CONTEXT (label) = current_function_decl; 2548 DECL_MODE (label) = VOIDmode; 2549 DECL_SOURCE_LOCATION (label) = location; 2550 2551 return label; 2552} 2553 2554/* Get the LABEL_DECL corresponding to identifier NAME as a label. 2555 Create one if none exists so far for the current function. 2556 This is called when a label is used in a goto expression or 2557 has its address taken. */ 2558 2559tree 2560lookup_label (tree name) 2561{ 2562 tree label; 2563 2564 if (current_function_decl == 0) 2565 { 2566 error ("label %qE referenced outside of any function", name); 2567 return 0; 2568 } 2569 2570 /* Use a label already defined or ref'd with this name, but not if 2571 it is inherited from a containing function and wasn't declared 2572 using __label__. */ 2573 label = I_LABEL_DECL (name); 2574 if (label && (DECL_CONTEXT (label) == current_function_decl 2575 || C_DECLARED_LABEL_FLAG (label))) 2576 { 2577 /* If the label has only been declared, update its apparent 2578 location to point here, for better diagnostics if it 2579 turns out not to have been defined. */ 2580 if (!TREE_USED (label)) 2581 DECL_SOURCE_LOCATION (label) = input_location; 2582 return label; 2583 } 2584 2585 /* No label binding for that identifier; make one. */ 2586 label = make_label (name, input_location); 2587 2588 /* Ordinary labels go in the current function scope. */ 2589 bind (name, label, current_function_scope, 2590 /*invisible=*/false, /*nested=*/false); 2591 return label; 2592} 2593 2594/* Make a label named NAME in the current function, shadowing silently 2595 any that may be inherited from containing functions or containing 2596 scopes. This is called for __label__ declarations. */ 2597 2598tree 2599declare_label (tree name) 2600{ 2601 struct c_binding *b = I_LABEL_BINDING (name); 2602 tree label; 2603 2604 /* Check to make sure that the label hasn't already been declared 2605 at this scope */ 2606 if (b && B_IN_CURRENT_SCOPE (b)) 2607 { 2608 error ("duplicate label declaration %qE", name); 2609 locate_old_decl (b->decl, error); 2610 2611 /* Just use the previous declaration. */ 2612 return b->decl; 2613 } 2614 2615 label = make_label (name, input_location); 2616 C_DECLARED_LABEL_FLAG (label) = 1; 2617 2618 /* Declared labels go in the current scope. */ 2619 bind (name, label, current_scope, 2620 /*invisible=*/false, /*nested=*/false); 2621 return label; 2622} 2623 2624/* Define a label, specifying the location in the source file. 2625 Return the LABEL_DECL node for the label, if the definition is valid. 2626 Otherwise return 0. */ 2627 2628tree 2629define_label (location_t location, tree name) 2630{ 2631 /* Find any preexisting label with this name. It is an error 2632 if that label has already been defined in this function, or 2633 if there is a containing function with a declared label with 2634 the same name. */ 2635 tree label = I_LABEL_DECL (name); 2636 struct c_label_list *nlist_se, *nlist_vm; 2637 2638 if (label 2639 && ((DECL_CONTEXT (label) == current_function_decl 2640 && DECL_INITIAL (label) != 0) 2641 || (DECL_CONTEXT (label) != current_function_decl 2642 && C_DECLARED_LABEL_FLAG (label)))) 2643 { 2644 error ("%Hduplicate label %qD", &location, label); 2645 locate_old_decl (label, error); 2646 return 0; 2647 } 2648 else if (label && DECL_CONTEXT (label) == current_function_decl) 2649 { 2650 /* The label has been used or declared already in this function, 2651 but not defined. Update its location to point to this 2652 definition. */ 2653 if (C_DECL_UNDEFINABLE_STMT_EXPR (label)) 2654 error ("%Jjump into statement expression", label); 2655 if (C_DECL_UNDEFINABLE_VM (label)) 2656 error ("%Jjump into scope of identifier with variably modified type", 2657 label); 2658 DECL_SOURCE_LOCATION (label) = location; 2659 } 2660 else 2661 { 2662 /* No label binding for that identifier; make one. */ 2663 label = make_label (name, location); 2664 2665 /* Ordinary labels go in the current function scope. */ 2666 bind (name, label, current_function_scope, 2667 /*invisible=*/false, /*nested=*/false); 2668 } 2669 2670 if (!in_system_header && lookup_name (name)) 2671 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace " 2672 "for labels, identifier %qE conflicts", &location, name); 2673 2674 nlist_se = XOBNEW (&parser_obstack, struct c_label_list); 2675 nlist_se->next = label_context_stack_se->labels_def; 2676 nlist_se->label = label; 2677 label_context_stack_se->labels_def = nlist_se; 2678 2679 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list); 2680 nlist_vm->next = label_context_stack_vm->labels_def; 2681 nlist_vm->label = label; 2682 label_context_stack_vm->labels_def = nlist_vm; 2683 2684 /* Mark label as having been defined. */ 2685 DECL_INITIAL (label) = error_mark_node; 2686 return label; 2687} 2688 2689/* Given NAME, an IDENTIFIER_NODE, 2690 return the structure (or union or enum) definition for that name. 2691 If THISLEVEL_ONLY is nonzero, searches only the current_scope. 2692 CODE says which kind of type the caller wants; 2693 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. 2694 If the wrong kind of type is found, an error is reported. */ 2695 2696static tree 2697lookup_tag (enum tree_code code, tree name, int thislevel_only) 2698{ 2699 struct c_binding *b = I_TAG_BINDING (name); 2700 int thislevel = 0; 2701 2702 if (!b || !b->decl) 2703 return 0; 2704 2705 /* We only care about whether it's in this level if 2706 thislevel_only was set or it might be a type clash. */ 2707 if (thislevel_only || TREE_CODE (b->decl) != code) 2708 { 2709 /* For our purposes, a tag in the external scope is the same as 2710 a tag in the file scope. (Primarily relevant to Objective-C 2711 and its builtin structure tags, which get pushed before the 2712 file scope is created.) */ 2713 if (B_IN_CURRENT_SCOPE (b) 2714 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b))) 2715 thislevel = 1; 2716 } 2717 2718 if (thislevel_only && !thislevel) 2719 return 0; 2720 2721 if (TREE_CODE (b->decl) != code) 2722 { 2723 /* Definition isn't the kind we were looking for. */ 2724 pending_invalid_xref = name; 2725 pending_invalid_xref_location = input_location; 2726 2727 /* If in the same binding level as a declaration as a tag 2728 of a different type, this must not be allowed to 2729 shadow that tag, so give the error immediately. 2730 (For example, "struct foo; union foo;" is invalid.) */ 2731 if (thislevel) 2732 pending_xref_error (); 2733 } 2734 return b->decl; 2735} 2736 2737/* Print an error message now 2738 for a recent invalid struct, union or enum cross reference. 2739 We don't print them immediately because they are not invalid 2740 when used in the `struct foo;' construct for shadowing. */ 2741 2742void 2743pending_xref_error (void) 2744{ 2745 if (pending_invalid_xref != 0) 2746 error ("%H%qE defined as wrong kind of tag", 2747 &pending_invalid_xref_location, pending_invalid_xref); 2748 pending_invalid_xref = 0; 2749} 2750 2751 2752/* Look up NAME in the current scope and its superiors 2753 in the namespace of variables, functions and typedefs. 2754 Return a ..._DECL node of some kind representing its definition, 2755 or return 0 if it is undefined. */ 2756 2757tree 2758lookup_name (tree name) 2759{ 2760 struct c_binding *b = I_SYMBOL_BINDING (name); 2761 if (b && !b->invisible) 2762 return b->decl; 2763 return 0; 2764} 2765 2766/* Similar to `lookup_name' but look only at the indicated scope. */ 2767 2768static tree 2769lookup_name_in_scope (tree name, struct c_scope *scope) 2770{ 2771 struct c_binding *b; 2772 2773 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed) 2774 if (B_IN_SCOPE (b, scope)) 2775 return b->decl; 2776 return 0; 2777} 2778 2779/* Create the predefined scalar types of C, 2780 and some nodes representing standard constants (0, 1, (void *) 0). 2781 Initialize the global scope. 2782 Make definitions for built-in primitive functions. */ 2783 2784void 2785c_init_decl_processing (void) 2786{ 2787 location_t save_loc = input_location; 2788 2789 /* Initialize reserved words for parser. */ 2790 c_parse_init (); 2791 2792 current_function_decl = 0; 2793 2794 gcc_obstack_init (&parser_obstack); 2795 2796 /* Make the externals scope. */ 2797 push_scope (); 2798 external_scope = current_scope; 2799 2800 /* Declarations from c_common_nodes_and_builtins must not be associated 2801 with this input file, lest we get differences between using and not 2802 using preprocessed headers. */ 2803#ifdef USE_MAPPED_LOCATION 2804 input_location = BUILTINS_LOCATION; 2805#else 2806 input_location.file = "<built-in>"; 2807 input_location.line = 0; 2808#endif 2809 2810 build_common_tree_nodes (flag_signed_char, false); 2811 2812 c_common_nodes_and_builtins (); 2813 2814 /* In C, comparisons and TRUTH_* expressions have type int. */ 2815 truthvalue_type_node = integer_type_node; 2816 truthvalue_true_node = integer_one_node; 2817 truthvalue_false_node = integer_zero_node; 2818 2819 /* Even in C99, which has a real boolean type. */ 2820 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"), 2821 boolean_type_node)); 2822 2823 input_location = save_loc; 2824 2825 pedantic_lvalues = true; 2826 2827 make_fname_decl = c_make_fname_decl; 2828 start_fname_decls (); 2829} 2830 2831/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the 2832 decl, NAME is the initialization string and TYPE_DEP indicates whether 2833 NAME depended on the type of the function. As we don't yet implement 2834 delayed emission of static data, we mark the decl as emitted 2835 so it is not placed in the output. Anything using it must therefore pull 2836 out the STRING_CST initializer directly. FIXME. */ 2837 2838static tree 2839c_make_fname_decl (tree id, int type_dep) 2840{ 2841 const char *name = fname_as_string (type_dep); 2842 tree decl, type, init; 2843 size_t length = strlen (name); 2844 2845 type = build_array_type (char_type_node, 2846 build_index_type (size_int (length))); 2847 type = c_build_qualified_type (type, TYPE_QUAL_CONST); 2848 2849 decl = build_decl (VAR_DECL, id, type); 2850 2851 TREE_STATIC (decl) = 1; 2852 TREE_READONLY (decl) = 1; 2853 DECL_ARTIFICIAL (decl) = 1; 2854 2855 init = build_string (length + 1, name); 2856 free ((char *) name); 2857 TREE_TYPE (init) = type; 2858 DECL_INITIAL (decl) = init; 2859 2860 TREE_USED (decl) = 1; 2861 2862 if (current_function_decl 2863 /* For invalid programs like this: 2864 2865 void foo() 2866 const char* p = __FUNCTION__; 2867 2868 the __FUNCTION__ is believed to appear in K&R style function 2869 parameter declarator. In that case we still don't have 2870 function_scope. */ 2871 && (!errorcount || current_function_scope)) 2872 { 2873 DECL_CONTEXT (decl) = current_function_decl; 2874 bind (id, decl, current_function_scope, 2875 /*invisible=*/false, /*nested=*/false); 2876 } 2877 2878 finish_decl (decl, init, NULL_TREE); 2879 2880 return decl; 2881} 2882 2883/* Return a definition for a builtin function named NAME and whose data type 2884 is TYPE. TYPE should be a function type with argument types. 2885 FUNCTION_CODE tells later passes how to compile calls to this function. 2886 See tree.h for its possible values. 2887 2888 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME, 2889 the name to be called if we can't opencode the function. If 2890 ATTRS is nonzero, use that for the function's attribute list. */ 2891 2892tree 2893builtin_function (const char *name, tree type, int function_code, 2894 enum built_in_class cl, const char *library_name, 2895 tree attrs) 2896{ 2897 tree id = get_identifier (name); 2898 tree decl = build_decl (FUNCTION_DECL, id, type); 2899 TREE_PUBLIC (decl) = 1; 2900 DECL_EXTERNAL (decl) = 1; 2901 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl); 2902 DECL_BUILT_IN_CLASS (decl) = cl; 2903 DECL_FUNCTION_CODE (decl) = function_code; 2904 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0); 2905 if (library_name) 2906 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name)); 2907 2908 /* Should never be called on a symbol with a preexisting meaning. */ 2909 gcc_assert (!I_SYMBOL_BINDING (id)); 2910 2911 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false); 2912 2913 /* Builtins in the implementation namespace are made visible without 2914 needing to be explicitly declared. See push_file_scope. */ 2915 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1]))) 2916 { 2917 TREE_CHAIN (decl) = visible_builtins; 2918 visible_builtins = decl; 2919 } 2920 2921 /* Possibly apply some default attributes to this built-in function. */ 2922 if (attrs) 2923 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN); 2924 else 2925 decl_attributes (&decl, NULL_TREE, 0); 2926 2927 return decl; 2928} 2929 2930/* Called when a declaration is seen that contains no names to declare. 2931 If its type is a reference to a structure, union or enum inherited 2932 from a containing scope, shadow that tag name for the current scope 2933 with a forward reference. 2934 If its type defines a new named structure or union 2935 or defines an enum, it is valid but we need not do anything here. 2936 Otherwise, it is an error. */ 2937 2938void 2939shadow_tag (const struct c_declspecs *declspecs) 2940{ 2941 shadow_tag_warned (declspecs, 0); 2942} 2943 2944/* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning, 2945 but no pedwarn. */ 2946void 2947shadow_tag_warned (const struct c_declspecs *declspecs, int warned) 2948{ 2949 bool found_tag = false; 2950 2951 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p) 2952 { 2953 tree value = declspecs->type; 2954 enum tree_code code = TREE_CODE (value); 2955 2956 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE) 2957 /* Used to test also that TYPE_SIZE (value) != 0. 2958 That caused warning for `struct foo;' at top level in the file. */ 2959 { 2960 tree name = TYPE_NAME (value); 2961 tree t; 2962 2963 found_tag = true; 2964 2965 if (name == 0) 2966 { 2967 if (warned != 1 && code != ENUMERAL_TYPE) 2968 /* Empty unnamed enum OK */ 2969 { 2970 pedwarn ("unnamed struct/union that defines no instances"); 2971 warned = 1; 2972 } 2973 } 2974 else if (!declspecs->tag_defined_p 2975 && declspecs->storage_class != csc_none) 2976 { 2977 if (warned != 1) 2978 pedwarn ("empty declaration with storage class specifier " 2979 "does not redeclare tag"); 2980 warned = 1; 2981 pending_xref_error (); 2982 } 2983 else if (!declspecs->tag_defined_p 2984 && (declspecs->const_p 2985 || declspecs->volatile_p 2986 || declspecs->restrict_p)) 2987 { 2988 if (warned != 1) 2989 pedwarn ("empty declaration with type qualifier " 2990 "does not redeclare tag"); 2991 warned = 1; 2992 pending_xref_error (); 2993 } 2994 else 2995 { 2996 pending_invalid_xref = 0; 2997 t = lookup_tag (code, name, 1); 2998 2999 if (t == 0) 3000 { 3001 t = make_node (code); 3002 pushtag (name, t); 3003 } 3004 } 3005 } 3006 else 3007 { 3008 if (warned != 1 && !in_system_header) 3009 { 3010 pedwarn ("useless type name in empty declaration"); 3011 warned = 1; 3012 } 3013 } 3014 } 3015 else if (warned != 1 && !in_system_header && declspecs->typedef_p) 3016 { 3017 pedwarn ("useless type name in empty declaration"); 3018 warned = 1; 3019 } 3020 3021 pending_invalid_xref = 0; 3022 3023 if (declspecs->inline_p) 3024 { 3025 error ("%<inline%> in empty declaration"); 3026 warned = 1; 3027 } 3028 3029 if (current_scope == file_scope && declspecs->storage_class == csc_auto) 3030 { 3031 error ("%<auto%> in file-scope empty declaration"); 3032 warned = 1; 3033 } 3034 3035 if (current_scope == file_scope && declspecs->storage_class == csc_register) 3036 { 3037 error ("%<register%> in file-scope empty declaration"); 3038 warned = 1; 3039 } 3040 3041 if (!warned && !in_system_header && declspecs->storage_class != csc_none) 3042 { 3043 warning (0, "useless storage class specifier in empty declaration"); 3044 warned = 2; 3045 } 3046 3047 if (!warned && !in_system_header && declspecs->thread_p) 3048 { 3049 warning (0, "useless %<__thread%> in empty declaration"); 3050 warned = 2; 3051 } 3052 3053 if (!warned && !in_system_header && (declspecs->const_p 3054 || declspecs->volatile_p 3055 || declspecs->restrict_p)) 3056 { 3057 warning (0, "useless type qualifier in empty declaration"); 3058 warned = 2; 3059 } 3060 3061 if (warned != 1) 3062 { 3063 if (!found_tag) 3064 pedwarn ("empty declaration"); 3065 } 3066} 3067 3068 3069/* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_* 3070 bits. SPECS represents declaration specifiers that the grammar 3071 only permits to contain type qualifiers and attributes. */ 3072 3073int 3074quals_from_declspecs (const struct c_declspecs *specs) 3075{ 3076 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0) 3077 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0) 3078 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)); 3079 gcc_assert (!specs->type 3080 && !specs->decl_attr 3081 && specs->typespec_word == cts_none 3082 && specs->storage_class == csc_none 3083 && !specs->typedef_p 3084 && !specs->explicit_signed_p 3085 && !specs->deprecated_p 3086 && !specs->long_p 3087 && !specs->long_long_p 3088 && !specs->short_p 3089 && !specs->signed_p 3090 && !specs->unsigned_p 3091 && !specs->complex_p 3092 && !specs->inline_p 3093 && !specs->thread_p); 3094 return quals; 3095} 3096 3097/* Construct an array declarator. EXPR is the expression inside [], 3098 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be 3099 applied to the pointer to which a parameter array is converted). 3100 STATIC_P is true if "static" is inside the [], false otherwise. 3101 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified 3102 length which is nevertheless a complete type, false otherwise. The 3103 field for the contained declarator is left to be filled in by 3104 set_array_declarator_inner. */ 3105 3106struct c_declarator * 3107build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p, 3108 bool vla_unspec_p) 3109{ 3110 struct c_declarator *declarator = XOBNEW (&parser_obstack, 3111 struct c_declarator); 3112 declarator->kind = cdk_array; 3113 declarator->declarator = 0; 3114 declarator->u.array.dimen = expr; 3115 if (quals) 3116 { 3117 declarator->u.array.attrs = quals->attrs; 3118 declarator->u.array.quals = quals_from_declspecs (quals); 3119 } 3120 else 3121 { 3122 declarator->u.array.attrs = NULL_TREE; 3123 declarator->u.array.quals = 0; 3124 } 3125 declarator->u.array.static_p = static_p; 3126 declarator->u.array.vla_unspec_p = vla_unspec_p; 3127 if (pedantic && !flag_isoc99) 3128 { 3129 if (static_p || quals != NULL) 3130 pedwarn ("ISO C90 does not support %<static%> or type " 3131 "qualifiers in parameter array declarators"); 3132 if (vla_unspec_p) 3133 pedwarn ("ISO C90 does not support %<[*]%> array declarators"); 3134 } 3135 if (vla_unspec_p) 3136 { 3137 if (!current_scope->parm_flag) 3138 { 3139 /* C99 6.7.5.2p4 */ 3140 error ("%<[*]%> not allowed in other than function prototype scope"); 3141 declarator->u.array.vla_unspec_p = false; 3142 return NULL; 3143 } 3144 current_scope->had_vla_unspec = true; 3145 } 3146 return declarator; 3147} 3148 3149/* Set the contained declarator of an array declarator. DECL is the 3150 declarator, as constructed by build_array_declarator; INNER is what 3151 appears on the left of the []. ABSTRACT_P is true if it is an 3152 abstract declarator, false otherwise; this is used to reject static 3153 and type qualifiers in abstract declarators, where they are not in 3154 the C99 grammar (subject to possible change in DR#289). */ 3155 3156struct c_declarator * 3157set_array_declarator_inner (struct c_declarator *decl, 3158 struct c_declarator *inner, bool abstract_p) 3159{ 3160 decl->declarator = inner; 3161 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED 3162 || decl->u.array.attrs != NULL_TREE 3163 || decl->u.array.static_p)) 3164 error ("static or type qualifiers in abstract declarator"); 3165 return decl; 3166} 3167 3168/* INIT is a constructor that forms DECL's initializer. If the final 3169 element initializes a flexible array field, add the size of that 3170 initializer to DECL's size. */ 3171 3172static void 3173add_flexible_array_elts_to_size (tree decl, tree init) 3174{ 3175 tree elt, type; 3176 3177 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init))) 3178 return; 3179 3180 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value; 3181 type = TREE_TYPE (elt); 3182 if (TREE_CODE (type) == ARRAY_TYPE 3183 && TYPE_SIZE (type) == NULL_TREE 3184 && TYPE_DOMAIN (type) != NULL_TREE 3185 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE) 3186 { 3187 complete_array_type (&type, elt, false); 3188 DECL_SIZE (decl) 3189 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type)); 3190 DECL_SIZE_UNIT (decl) 3191 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type)); 3192 } 3193} 3194 3195/* Decode a "typename", such as "int **", returning a ..._TYPE node. */ 3196 3197tree 3198groktypename (struct c_type_name *type_name) 3199{ 3200 tree type; 3201 tree attrs = type_name->specs->attrs; 3202 3203 type_name->specs->attrs = NULL_TREE; 3204 3205 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME, 3206 false, NULL); 3207 3208 /* Apply attributes. */ 3209 decl_attributes (&type, attrs, 0); 3210 3211 return type; 3212} 3213 3214/* Decode a declarator in an ordinary declaration or data definition. 3215 This is called as soon as the type information and variable name 3216 have been parsed, before parsing the initializer if any. 3217 Here we create the ..._DECL node, fill in its type, 3218 and put it on the list of decls for the current context. 3219 The ..._DECL node is returned as the value. 3220 3221 Exception: for arrays where the length is not specified, 3222 the type is left null, to be filled in by `finish_decl'. 3223 3224 Function definitions do not come here; they go to start_function 3225 instead. However, external and forward declarations of functions 3226 do go through here. Structure field declarations are done by 3227 grokfield and not through here. */ 3228 3229tree 3230start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs, 3231 bool initialized, tree attributes) 3232{ 3233 tree decl; 3234 tree tem; 3235 3236 /* An object declared as __attribute__((deprecated)) suppresses 3237 warnings of uses of other deprecated items. */ 3238 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */ 3239 /* An object declared as __attribute__((unavailable)) suppresses 3240 any reports of being declared with unavailable or deprecated 3241 items. An object declared as __attribute__((deprecated)) 3242 suppresses warnings of uses of other deprecated items. */ 3243#ifdef A_LESS_INEFFICENT_WAY /* which I really don't want to do! */ 3244 if (lookup_attribute ("deprecated", attributes)) 3245 deprecated_state = DEPRECATED_SUPPRESS; 3246 else if (lookup_attribute ("unavailable", attributes)) 3247 deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS; 3248#else /* a more efficient way doing what lookup_attribute would do */ 3249 tree a; 3250 3251 for (a = attributes; a; a = TREE_CHAIN (a)) 3252 { 3253 tree name = TREE_PURPOSE (a); 3254 if (TREE_CODE (name) == IDENTIFIER_NODE) 3255 if (is_attribute_p ("deprecated", name)) 3256 { 3257 deprecated_state = DEPRECATED_SUPPRESS; 3258 break; 3259 } 3260 if (is_attribute_p ("unavailable", name)) 3261 { 3262 deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS; 3263 break; 3264 } 3265 } 3266#endif 3267 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */ 3268 3269 decl = grokdeclarator (declarator, declspecs, 3270 NORMAL, initialized, NULL); 3271 if (!decl) 3272 return 0; 3273 3274 deprecated_state = DEPRECATED_NORMAL; 3275 3276 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL 3277 && MAIN_NAME_P (DECL_NAME (decl))) 3278 warning (OPT_Wmain, "%q+D is usually a function", decl); 3279 3280 if (initialized) 3281 /* Is it valid for this decl to have an initializer at all? 3282 If not, set INITIALIZED to zero, which will indirectly 3283 tell 'finish_decl' to ignore the initializer once it is parsed. */ 3284 switch (TREE_CODE (decl)) 3285 { 3286 case TYPE_DECL: 3287 error ("typedef %qD is initialized (use __typeof__ instead)", decl); 3288 initialized = 0; 3289 break; 3290 3291 case FUNCTION_DECL: 3292 error ("function %qD is initialized like a variable", decl); 3293 initialized = 0; 3294 break; 3295 3296 case PARM_DECL: 3297 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */ 3298 error ("parameter %qD is initialized", decl); 3299 initialized = 0; 3300 break; 3301 3302 default: 3303 /* Don't allow initializations for incomplete types except for 3304 arrays which might be completed by the initialization. */ 3305 3306 /* This can happen if the array size is an undefined macro. 3307 We already gave a warning, so we don't need another one. */ 3308 if (TREE_TYPE (decl) == error_mark_node) 3309 initialized = 0; 3310 else if (COMPLETE_TYPE_P (TREE_TYPE (decl))) 3311 { 3312 /* A complete type is ok if size is fixed. */ 3313 3314 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST 3315 || C_DECL_VARIABLE_SIZE (decl)) 3316 { 3317 error ("variable-sized object may not be initialized"); 3318 initialized = 0; 3319 } 3320 } 3321 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE) 3322 { 3323 error ("variable %qD has initializer but incomplete type", decl); 3324 initialized = 0; 3325 } 3326 else if (C_DECL_VARIABLE_SIZE (decl)) 3327 { 3328 /* Although C99 is unclear about whether incomplete arrays 3329 of VLAs themselves count as VLAs, it does not make 3330 sense to permit them to be initialized given that 3331 ordinary VLAs may not be initialized. */ 3332 error ("variable-sized object may not be initialized"); 3333 initialized = 0; 3334 } 3335 } 3336 3337 if (initialized) 3338 { 3339 if (current_scope == file_scope) 3340 TREE_STATIC (decl) = 1; 3341 3342 /* Tell 'pushdecl' this is an initialized decl 3343 even though we don't yet have the initializer expression. 3344 Also tell 'finish_decl' it may store the real initializer. */ 3345 DECL_INITIAL (decl) = error_mark_node; 3346 } 3347 3348 /* If this is a function declaration, write a record describing it to the 3349 prototypes file (if requested). */ 3350 3351 if (TREE_CODE (decl) == FUNCTION_DECL) 3352 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0); 3353 3354 /* ANSI specifies that a tentative definition which is not merged with 3355 a non-tentative definition behaves exactly like a definition with an 3356 initializer equal to zero. (Section 3.7.2) 3357 3358 -fno-common gives strict ANSI behavior, though this tends to break 3359 a large body of code that grew up without this rule. 3360 3361 Thread-local variables are never common, since there's no entrenched 3362 body of code to break, and it allows more efficient variable references 3363 in the presence of dynamic linking. */ 3364 3365 if (TREE_CODE (decl) == VAR_DECL 3366 && !initialized 3367 && TREE_PUBLIC (decl) 3368 && !DECL_THREAD_LOCAL_P (decl) 3369 && !flag_no_common) 3370 DECL_COMMON (decl) = 1; 3371 3372 /* Set attributes here so if duplicate decl, will have proper attributes. */ 3373 decl_attributes (&decl, attributes, 0); 3374 3375 /* Handle gnu_inline attribute. */ 3376 if (declspecs->inline_p 3377 && !flag_gnu89_inline 3378 && TREE_CODE (decl) == FUNCTION_DECL 3379 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))) 3380 { 3381 if (declspecs->storage_class == csc_auto && current_scope != file_scope) 3382 ; 3383 else if (declspecs->storage_class != csc_static) 3384 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl); 3385 } 3386 3387 if (TREE_CODE (decl) == FUNCTION_DECL 3388 && targetm.calls.promote_prototypes (TREE_TYPE (decl))) 3389 { 3390 struct c_declarator *ce = declarator; 3391 3392 if (ce->kind == cdk_pointer) 3393 ce = declarator->declarator; 3394 if (ce->kind == cdk_function) 3395 { 3396 tree args = ce->u.arg_info->parms; 3397 for (; args; args = TREE_CHAIN (args)) 3398 { 3399 tree type = TREE_TYPE (args); 3400 if (type && INTEGRAL_TYPE_P (type) 3401 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 3402 DECL_ARG_TYPE (args) = integer_type_node; 3403 } 3404 } 3405 } 3406 3407 if (TREE_CODE (decl) == FUNCTION_DECL 3408 && DECL_DECLARED_INLINE_P (decl) 3409 && DECL_UNINLINABLE (decl) 3410 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) 3411 warning (OPT_Wattributes, "inline function %q+D given attribute noinline", 3412 decl); 3413 3414 /* C99 6.7.4p3: An inline definition of a function with external 3415 linkage shall not contain a definition of a modifiable object 3416 with static storage duration... */ 3417 if (TREE_CODE (decl) == VAR_DECL 3418 && current_scope != file_scope 3419 && TREE_STATIC (decl) 3420 && !TREE_READONLY (decl) 3421 && DECL_DECLARED_INLINE_P (current_function_decl) 3422 && DECL_EXTERNAL (current_function_decl)) 3423 pedwarn ("%q+D is static but declared in inline function %qD " 3424 "which is not static", decl, current_function_decl); 3425 3426 /* Add this decl to the current scope. 3427 TEM may equal DECL or it may be a previous decl of the same name. */ 3428 tem = pushdecl (decl); 3429 3430 if (initialized && DECL_EXTERNAL (tem)) 3431 { 3432 DECL_EXTERNAL (tem) = 0; 3433 TREE_STATIC (tem) = 1; 3434 } 3435 3436 return tem; 3437} 3438 3439/* Initialize EH if not initialized yet and exceptions are enabled. */ 3440 3441void 3442c_maybe_initialize_eh (void) 3443{ 3444 if (!flag_exceptions || c_eh_initialized_p) 3445 return; 3446 3447 c_eh_initialized_p = true; 3448 eh_personality_libfunc 3449 = init_one_libfunc (USING_SJLJ_EXCEPTIONS 3450 ? "__gcc_personality_sj0" 3451 : "__gcc_personality_v0"); 3452 default_init_unwind_resume_libfunc (); 3453 using_eh_for_cleanups (); 3454} 3455 3456/* Finish processing of a declaration; 3457 install its initial value. 3458 If the length of an array type is not known before, 3459 it must be determined now, from the initial value, or it is an error. */ 3460 3461void 3462finish_decl (tree decl, tree init, tree asmspec_tree) 3463{ 3464 tree type; 3465 int was_incomplete = (DECL_SIZE (decl) == 0); 3466 const char *asmspec = 0; 3467 3468 /* If a name was specified, get the string. */ 3469 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL) 3470 && DECL_FILE_SCOPE_P (decl)) 3471 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); 3472 if (asmspec_tree) 3473 asmspec = TREE_STRING_POINTER (asmspec_tree); 3474 3475 /* If `start_decl' didn't like having an initialization, ignore it now. */ 3476 if (init != 0 && DECL_INITIAL (decl) == 0) 3477 init = 0; 3478 3479 /* Don't crash if parm is initialized. */ 3480 if (TREE_CODE (decl) == PARM_DECL) 3481 init = 0; 3482 3483 if (init) 3484 store_init_value (decl, init); 3485 3486 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL 3487 || TREE_CODE (decl) == FUNCTION_DECL 3488 || TREE_CODE (decl) == FIELD_DECL)) 3489 objc_check_decl (decl); 3490 3491 type = TREE_TYPE (decl); 3492 3493 /* Deduce size of array from initialization, if not already known. */ 3494 if (TREE_CODE (type) == ARRAY_TYPE 3495 && TYPE_DOMAIN (type) == 0 3496 && TREE_CODE (decl) != TYPE_DECL) 3497 { 3498 bool do_default 3499 = (TREE_STATIC (decl) 3500 /* Even if pedantic, an external linkage array 3501 may have incomplete type at first. */ 3502 ? pedantic && !TREE_PUBLIC (decl) 3503 : !DECL_EXTERNAL (decl)); 3504 int failure 3505 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl), 3506 do_default); 3507 3508 /* Get the completed type made by complete_array_type. */ 3509 type = TREE_TYPE (decl); 3510 3511 switch (failure) 3512 { 3513 case 1: 3514 error ("initializer fails to determine size of %q+D", decl); 3515 break; 3516 3517 case 2: 3518 if (do_default) 3519 error ("array size missing in %q+D", decl); 3520 /* If a `static' var's size isn't known, 3521 make it extern as well as static, so it does not get 3522 allocated. 3523 If it is not `static', then do not mark extern; 3524 finish_incomplete_decl will give it a default size 3525 and it will get allocated. */ 3526 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) 3527 DECL_EXTERNAL (decl) = 1; 3528 break; 3529 3530 case 3: 3531 error ("zero or negative size array %q+D", decl); 3532 break; 3533 3534 case 0: 3535 /* For global variables, update the copy of the type that 3536 exists in the binding. */ 3537 if (TREE_PUBLIC (decl)) 3538 { 3539 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl)); 3540 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext)) 3541 b_ext = b_ext->shadowed; 3542 if (b_ext) 3543 { 3544 if (b_ext->type) 3545 b_ext->type = composite_type (b_ext->type, type); 3546 else 3547 b_ext->type = type; 3548 } 3549 } 3550 break; 3551 3552 default: 3553 gcc_unreachable (); 3554 } 3555 3556 if (DECL_INITIAL (decl)) 3557 TREE_TYPE (DECL_INITIAL (decl)) = type; 3558 3559 layout_decl (decl, 0); 3560 } 3561 3562 if (TREE_CODE (decl) == VAR_DECL) 3563 { 3564 if (init && TREE_CODE (init) == CONSTRUCTOR) 3565 add_flexible_array_elts_to_size (decl, init); 3566 3567 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node 3568 && COMPLETE_TYPE_P (TREE_TYPE (decl))) 3569 layout_decl (decl, 0); 3570 3571 if (DECL_SIZE (decl) == 0 3572 /* Don't give an error if we already gave one earlier. */ 3573 && TREE_TYPE (decl) != error_mark_node 3574 && (TREE_STATIC (decl) 3575 /* A static variable with an incomplete type 3576 is an error if it is initialized. 3577 Also if it is not file scope. 3578 Otherwise, let it through, but if it is not `extern' 3579 then it may cause an error message later. */ 3580 ? (DECL_INITIAL (decl) != 0 3581 || !DECL_FILE_SCOPE_P (decl)) 3582 /* An automatic variable with an incomplete type 3583 is an error. */ 3584 : !DECL_EXTERNAL (decl))) 3585 { 3586 error ("storage size of %q+D isn%'t known", decl); 3587 TREE_TYPE (decl) = error_mark_node; 3588 } 3589 3590 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 3591 && DECL_SIZE (decl) != 0) 3592 { 3593 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) 3594 constant_expression_warning (DECL_SIZE (decl)); 3595 else 3596 error ("storage size of %q+D isn%'t constant", decl); 3597 } 3598 3599 if (TREE_USED (type)) 3600 TREE_USED (decl) = 1; 3601 } 3602 3603 /* If this is a function and an assembler name is specified, reset DECL_RTL 3604 so we can give it its new name. Also, update built_in_decls if it 3605 was a normal built-in. */ 3606 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec) 3607 { 3608 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) 3609 set_builtin_user_assembler_name (decl, asmspec); 3610 set_user_assembler_name (decl, asmspec); 3611 } 3612 3613 /* If #pragma weak was used, mark the decl weak now. */ 3614 maybe_apply_pragma_weak (decl); 3615 3616 /* Output the assembler code and/or RTL code for variables and functions, 3617 unless the type is an undefined structure or union. 3618 If not, it will get done when the type is completed. */ 3619 3620 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL) 3621 { 3622 /* Determine the ELF visibility. */ 3623 if (TREE_PUBLIC (decl)) 3624 c_determine_visibility (decl); 3625 3626 /* This is a no-op in c-lang.c or something real in objc-act.c. */ 3627 if (c_dialect_objc ()) 3628 objc_check_decl (decl); 3629 3630 if (asmspec) 3631 { 3632 /* If this is not a static variable, issue a warning. 3633 It doesn't make any sense to give an ASMSPEC for an 3634 ordinary, non-register local variable. Historically, 3635 GCC has accepted -- but ignored -- the ASMSPEC in 3636 this case. */ 3637 if (!DECL_FILE_SCOPE_P (decl) 3638 && TREE_CODE (decl) == VAR_DECL 3639 && !C_DECL_REGISTER (decl) 3640 && !TREE_STATIC (decl)) 3641 warning (0, "ignoring asm-specifier for non-static local " 3642 "variable %q+D", decl); 3643 else 3644 set_user_assembler_name (decl, asmspec); 3645 } 3646 3647 if (DECL_FILE_SCOPE_P (decl)) 3648 { 3649 if (DECL_INITIAL (decl) == NULL_TREE 3650 || DECL_INITIAL (decl) == error_mark_node) 3651 /* Don't output anything 3652 when a tentative file-scope definition is seen. 3653 But at end of compilation, do output code for them. */ 3654 DECL_DEFER_OUTPUT (decl) = 1; 3655 rest_of_decl_compilation (decl, true, 0); 3656 } 3657 else 3658 { 3659 /* In conjunction with an ASMSPEC, the `register' 3660 keyword indicates that we should place the variable 3661 in a particular register. */ 3662 if (asmspec && C_DECL_REGISTER (decl)) 3663 { 3664 DECL_HARD_REGISTER (decl) = 1; 3665 /* This cannot be done for a structure with volatile 3666 fields, on which DECL_REGISTER will have been 3667 reset. */ 3668 if (!DECL_REGISTER (decl)) 3669 error ("cannot put object with volatile field into register"); 3670 } 3671 3672 if (TREE_CODE (decl) != FUNCTION_DECL) 3673 { 3674 /* If we're building a variable sized type, and we might be 3675 reachable other than via the top of the current binding 3676 level, then create a new BIND_EXPR so that we deallocate 3677 the object at the right time. */ 3678 /* Note that DECL_SIZE can be null due to errors. */ 3679 if (DECL_SIZE (decl) 3680 && !TREE_CONSTANT (DECL_SIZE (decl)) 3681 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list)) 3682 { 3683 tree bind; 3684 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL); 3685 TREE_SIDE_EFFECTS (bind) = 1; 3686 add_stmt (bind); 3687 BIND_EXPR_BODY (bind) = push_stmt_list (); 3688 } 3689 add_stmt (build_stmt (DECL_EXPR, decl)); 3690 } 3691 } 3692 3693 3694 if (!DECL_FILE_SCOPE_P (decl)) 3695 { 3696 /* Recompute the RTL of a local array now 3697 if it used to be an incomplete type. */ 3698 if (was_incomplete 3699 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) 3700 { 3701 /* If we used it already as memory, it must stay in memory. */ 3702 TREE_ADDRESSABLE (decl) = TREE_USED (decl); 3703 /* If it's still incomplete now, no init will save it. */ 3704 if (DECL_SIZE (decl) == 0) 3705 DECL_INITIAL (decl) = 0; 3706 } 3707 } 3708 } 3709 3710 /* If this was marked 'used', be sure it will be output. */ 3711 if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl))) 3712 mark_decl_referenced (decl); 3713 3714 if (TREE_CODE (decl) == TYPE_DECL) 3715 { 3716 if (!DECL_FILE_SCOPE_P (decl) 3717 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE)) 3718 add_stmt (build_stmt (DECL_EXPR, decl)); 3719 3720 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0); 3721 } 3722 3723 /* At the end of a declaration, throw away any variable type sizes 3724 of types defined inside that declaration. There is no use 3725 computing them in the following function definition. */ 3726 if (current_scope == file_scope) 3727 get_pending_sizes (); 3728 3729 /* Install a cleanup (aka destructor) if one was given. */ 3730 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl)) 3731 { 3732 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl)); 3733 if (attr) 3734 { 3735 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr)); 3736 tree cleanup_decl = lookup_name (cleanup_id); 3737 tree cleanup; 3738 3739 /* Build "cleanup(&decl)" for the destructor. */ 3740 cleanup = build_unary_op (ADDR_EXPR, decl, 0); 3741 cleanup = build_tree_list (NULL_TREE, cleanup); 3742 cleanup = build_function_call (cleanup_decl, cleanup); 3743 3744 /* Don't warn about decl unused; the cleanup uses it. */ 3745 TREE_USED (decl) = 1; 3746 TREE_USED (cleanup_decl) = 1; 3747 3748 /* Initialize EH, if we've been told to do so. */ 3749 c_maybe_initialize_eh (); 3750 3751 push_cleanup (decl, cleanup, false); 3752 } 3753 } 3754} 3755 3756/* Given a parsed parameter declaration, decode it into a PARM_DECL. */ 3757 3758tree 3759grokparm (const struct c_parm *parm) 3760{ 3761 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, 3762 NULL); 3763 3764 decl_attributes (&decl, parm->attrs, 0); 3765 3766 return decl; 3767} 3768 3769/* Given a parsed parameter declaration, decode it into a PARM_DECL 3770 and push that on the current scope. */ 3771 3772void 3773push_parm_decl (const struct c_parm *parm) 3774{ 3775 tree decl; 3776 3777 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL); 3778 decl_attributes (&decl, parm->attrs, 0); 3779 3780 decl = pushdecl (decl); 3781 3782 finish_decl (decl, NULL_TREE, NULL_TREE); 3783} 3784 3785/* Mark all the parameter declarations to date as forward decls. 3786 Also diagnose use of this extension. */ 3787 3788void 3789mark_forward_parm_decls (void) 3790{ 3791 struct c_binding *b; 3792 3793 if (pedantic && !current_scope->warned_forward_parm_decls) 3794 { 3795 pedwarn ("ISO C forbids forward parameter declarations"); 3796 current_scope->warned_forward_parm_decls = true; 3797 } 3798 3799 for (b = current_scope->bindings; b; b = b->prev) 3800 if (TREE_CODE (b->decl) == PARM_DECL) 3801 TREE_ASM_WRITTEN (b->decl) = 1; 3802} 3803 3804/* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound 3805 literal, which may be an incomplete array type completed by the 3806 initializer; INIT is a CONSTRUCTOR that initializes the compound 3807 literal. */ 3808 3809tree 3810build_compound_literal (tree type, tree init) 3811{ 3812 /* We do not use start_decl here because we have a type, not a declarator; 3813 and do not use finish_decl because the decl should be stored inside 3814 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */ 3815 tree decl; 3816 tree complit; 3817 tree stmt; 3818 3819 if (type == error_mark_node) 3820 return error_mark_node; 3821 3822 decl = build_decl (VAR_DECL, NULL_TREE, type); 3823 DECL_EXTERNAL (decl) = 0; 3824 TREE_PUBLIC (decl) = 0; 3825 TREE_STATIC (decl) = (current_scope == file_scope); 3826 DECL_CONTEXT (decl) = current_function_decl; 3827 TREE_USED (decl) = 1; 3828 TREE_TYPE (decl) = type; 3829 TREE_READONLY (decl) = TYPE_READONLY (type); 3830 store_init_value (decl, init); 3831 3832 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type)) 3833 { 3834 int failure = complete_array_type (&TREE_TYPE (decl), 3835 DECL_INITIAL (decl), true); 3836 gcc_assert (!failure); 3837 3838 type = TREE_TYPE (decl); 3839 TREE_TYPE (DECL_INITIAL (decl)) = type; 3840 } 3841 3842 if (type == error_mark_node || !COMPLETE_TYPE_P (type)) 3843 return error_mark_node; 3844 3845 stmt = build_stmt (DECL_EXPR, decl); 3846 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt); 3847 TREE_SIDE_EFFECTS (complit) = 1; 3848 3849 layout_decl (decl, 0); 3850 3851 if (TREE_STATIC (decl)) 3852 { 3853 /* This decl needs a name for the assembler output. */ 3854 set_compound_literal_name (decl); 3855 DECL_DEFER_OUTPUT (decl) = 1; 3856 DECL_COMDAT (decl) = 1; 3857 DECL_ARTIFICIAL (decl) = 1; 3858 DECL_IGNORED_P (decl) = 1; 3859 pushdecl (decl); 3860 rest_of_decl_compilation (decl, 1, 0); 3861 } 3862 3863 return complit; 3864} 3865 3866/* Determine whether TYPE is a structure with a flexible array member, 3867 or a union containing such a structure (possibly recursively). */ 3868 3869static bool 3870flexible_array_type_p (tree type) 3871{ 3872 tree x; 3873 switch (TREE_CODE (type)) 3874 { 3875 case RECORD_TYPE: 3876 x = TYPE_FIELDS (type); 3877 if (x == NULL_TREE) 3878 return false; 3879 while (TREE_CHAIN (x) != NULL_TREE) 3880 x = TREE_CHAIN (x); 3881 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 3882 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 3883 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 3884 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 3885 return true; 3886 return false; 3887 case UNION_TYPE: 3888 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x)) 3889 { 3890 if (flexible_array_type_p (TREE_TYPE (x))) 3891 return true; 3892 } 3893 return false; 3894 default: 3895 return false; 3896 } 3897} 3898 3899/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME, 3900 replacing with appropriate values if they are invalid. */ 3901static void 3902check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name) 3903{ 3904 tree type_mv; 3905 unsigned int max_width; 3906 unsigned HOST_WIDE_INT w; 3907 const char *name = orig_name ? orig_name: _("<anonymous>"); 3908 3909 /* Detect and ignore out of range field width and process valid 3910 field widths. */ 3911 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)) 3912 || TREE_CODE (*width) != INTEGER_CST) 3913 { 3914 error ("bit-field %qs width not an integer constant", name); 3915 *width = integer_one_node; 3916 } 3917 else 3918 { 3919 constant_expression_warning (*width); 3920 if (tree_int_cst_sgn (*width) < 0) 3921 { 3922 error ("negative width in bit-field %qs", name); 3923 *width = integer_one_node; 3924 } 3925 else if (integer_zerop (*width) && orig_name) 3926 { 3927 error ("zero width for bit-field %qs", name); 3928 *width = integer_one_node; 3929 } 3930 } 3931 3932 /* Detect invalid bit-field type. */ 3933 if (TREE_CODE (*type) != INTEGER_TYPE 3934 && TREE_CODE (*type) != BOOLEAN_TYPE 3935 && TREE_CODE (*type) != ENUMERAL_TYPE) 3936 { 3937 error ("bit-field %qs has invalid type", name); 3938 *type = unsigned_type_node; 3939 } 3940 3941 type_mv = TYPE_MAIN_VARIANT (*type); 3942 if (pedantic 3943 && !in_system_header 3944 && type_mv != integer_type_node 3945 && type_mv != unsigned_type_node 3946 && type_mv != boolean_type_node) 3947 pedwarn ("type of bit-field %qs is a GCC extension", name); 3948 3949 if (type_mv == boolean_type_node) 3950 max_width = CHAR_TYPE_SIZE; 3951 else 3952 max_width = TYPE_PRECISION (*type); 3953 3954 if (0 < compare_tree_int (*width, max_width)) 3955 { 3956 error ("width of %qs exceeds its type", name); 3957 w = max_width; 3958 *width = build_int_cst (NULL_TREE, w); 3959 } 3960 else 3961 w = tree_low_cst (*width, 1); 3962 3963 if (TREE_CODE (*type) == ENUMERAL_TYPE) 3964 { 3965 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type); 3966 if (!lt 3967 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type)) 3968 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type))) 3969 warning (0, "%qs is narrower than values of its type", name); 3970 } 3971} 3972 3973 3974 3975/* Print warning about variable length array if necessary. */ 3976 3977static void 3978warn_variable_length_array (const char *name, tree size) 3979{ 3980 int ped = !flag_isoc99 && pedantic && warn_vla != 0; 3981 int const_size = TREE_CONSTANT (size); 3982 3983 if (ped) 3984 { 3985 if (const_size) 3986 { 3987 if (name) 3988 pedwarn ("ISO C90 forbids array %qs whose size " 3989 "can%'t be evaluated", 3990 name); 3991 else 3992 pedwarn ("ISO C90 forbids array whose size " 3993 "can%'t be evaluated"); 3994 } 3995 else 3996 { 3997 if (name) 3998 pedwarn ("ISO C90 forbids variable length array %qs", 3999 name); 4000 else 4001 pedwarn ("ISO C90 forbids variable length array"); 4002 } 4003 } 4004 else if (warn_vla > 0) 4005 { 4006 if (const_size) 4007 { 4008 if (name) 4009 warning (OPT_Wvla, 4010 "the size of array %qs can" 4011 "%'t be evaluated", name); 4012 else 4013 warning (OPT_Wvla, 4014 "the size of array can %'t be evaluated"); 4015 } 4016 else 4017 { 4018 if (name) 4019 warning (OPT_Wvla, 4020 "variable length array %qs is used", 4021 name); 4022 else 4023 warning (OPT_Wvla, 4024 "variable length array is used"); 4025 } 4026 } 4027} 4028 4029/* Given declspecs and a declarator, 4030 determine the name and type of the object declared 4031 and construct a ..._DECL node for it. 4032 (In one case we can return a ..._TYPE node instead. 4033 For invalid input we sometimes return 0.) 4034 4035 DECLSPECS is a c_declspecs structure for the declaration specifiers. 4036 4037 DECL_CONTEXT says which syntactic context this declaration is in: 4038 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. 4039 FUNCDEF for a function definition. Like NORMAL but a few different 4040 error messages in each case. Return value may be zero meaning 4041 this definition is too screwy to try to parse. 4042 PARM for a parameter declaration (either within a function prototype 4043 or before a function body). Make a PARM_DECL, or return void_type_node. 4044 TYPENAME if for a typename (in a cast or sizeof). 4045 Don't make a DECL node; just return the ..._TYPE node. 4046 FIELD for a struct or union field; make a FIELD_DECL. 4047 INITIALIZED is true if the decl has an initializer. 4048 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node 4049 representing the width of the bit-field. 4050 4051 In the TYPENAME case, DECLARATOR is really an absolute declarator. 4052 It may also be so in the PARM case, for a prototype where the 4053 argument type is specified but not the name. 4054 4055 This function is where the complicated C meanings of `static' 4056 and `extern' are interpreted. */ 4057 4058static tree 4059grokdeclarator (const struct c_declarator *declarator, 4060 struct c_declspecs *declspecs, 4061 enum decl_context decl_context, bool initialized, tree *width) 4062{ 4063 tree type = declspecs->type; 4064 bool threadp = declspecs->thread_p; 4065 enum c_storage_class storage_class = declspecs->storage_class; 4066 int constp; 4067 int restrictp; 4068 int volatilep; 4069 int type_quals = TYPE_UNQUALIFIED; 4070 const char *name, *orig_name; 4071 tree typedef_type = 0; 4072 bool funcdef_flag = false; 4073 bool funcdef_syntax = false; 4074 int size_varies = 0; 4075 tree decl_attr = declspecs->decl_attr; 4076 int array_ptr_quals = TYPE_UNQUALIFIED; 4077 tree array_ptr_attrs = NULL_TREE; 4078 int array_parm_static = 0; 4079 bool array_parm_vla_unspec_p = false; 4080 tree returned_attrs = NULL_TREE; 4081 bool bitfield = width != NULL; 4082 tree element_type; 4083 struct c_arg_info *arg_info = 0; 4084 4085 if (decl_context == FUNCDEF) 4086 funcdef_flag = true, decl_context = NORMAL; 4087 4088 /* Look inside a declarator for the name being declared 4089 and get it as a string, for an error message. */ 4090 { 4091 const struct c_declarator *decl = declarator; 4092 name = 0; 4093 4094 while (decl) 4095 switch (decl->kind) 4096 { 4097 case cdk_function: 4098 case cdk_array: 4099 case cdk_pointer: 4100 funcdef_syntax = (decl->kind == cdk_function); 4101 decl = decl->declarator; 4102 break; 4103 4104 case cdk_attrs: 4105 decl = decl->declarator; 4106 break; 4107 4108 case cdk_id: 4109 if (decl->u.id) 4110 name = IDENTIFIER_POINTER (decl->u.id); 4111 decl = 0; 4112 break; 4113 4114 default: 4115 gcc_unreachable (); 4116 } 4117 orig_name = name; 4118 if (name == 0) 4119 name = "type name"; 4120 } 4121 4122 /* A function definition's declarator must have the form of 4123 a function declarator. */ 4124 4125 if (funcdef_flag && !funcdef_syntax) 4126 return 0; 4127 4128 /* If this looks like a function definition, make it one, 4129 even if it occurs where parms are expected. 4130 Then store_parm_decls will reject it and not use it as a parm. */ 4131 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */ 4132 if (declspecs->unavailable_p) 4133 error_unavailable_use (declspecs->type); 4134 else 4135 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */ 4136 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag) 4137 decl_context = PARM; 4138 4139 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS) 4140 warn_deprecated_use (declspecs->type); 4141 4142 if ((decl_context == NORMAL || decl_context == FIELD) 4143 && current_scope == file_scope 4144 && variably_modified_type_p (type, NULL_TREE)) 4145 { 4146 error ("variably modified %qs at file scope", name); 4147 type = integer_type_node; 4148 } 4149 4150 typedef_type = type; 4151 size_varies = C_TYPE_VARIABLE_SIZE (type); 4152 4153 /* Diagnose defaulting to "int". */ 4154 4155 if (declspecs->default_int_p && !in_system_header) 4156 { 4157 /* Issue a warning if this is an ISO C 99 program or if 4158 -Wreturn-type and this is a function, or if -Wimplicit; 4159 prefer the former warning since it is more explicit. */ 4160 if ((warn_implicit_int || warn_return_type || flag_isoc99) 4161 && funcdef_flag) 4162 warn_about_return_type = 1; 4163 else if (warn_implicit_int || flag_isoc99) 4164 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name); 4165 } 4166 4167 /* Adjust the type if a bit-field is being declared, 4168 -funsigned-bitfields applied and the type is not explicitly 4169 "signed". */ 4170 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p 4171 && TREE_CODE (type) == INTEGER_TYPE) 4172 type = c_common_unsigned_type (type); 4173 4174 /* Figure out the type qualifiers for the declaration. There are 4175 two ways a declaration can become qualified. One is something 4176 like `const int i' where the `const' is explicit. Another is 4177 something like `typedef const int CI; CI i' where the type of the 4178 declaration contains the `const'. A third possibility is that 4179 there is a type qualifier on the element type of a typedefed 4180 array type, in which case we should extract that qualifier so 4181 that c_apply_type_quals_to_decls receives the full list of 4182 qualifiers to work with (C90 is not entirely clear about whether 4183 duplicate qualifiers should be diagnosed in this case, but it 4184 seems most appropriate to do so). */ 4185 element_type = strip_array_types (type); 4186 constp = declspecs->const_p + TYPE_READONLY (element_type); 4187 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type); 4188 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type); 4189 if (pedantic && !flag_isoc99) 4190 { 4191 if (constp > 1) 4192 pedwarn ("duplicate %<const%>"); 4193 if (restrictp > 1) 4194 pedwarn ("duplicate %<restrict%>"); 4195 if (volatilep > 1) 4196 pedwarn ("duplicate %<volatile%>"); 4197 } 4198 if (!flag_gen_aux_info && (TYPE_QUALS (element_type))) 4199 type = TYPE_MAIN_VARIANT (type); 4200 type_quals = ((constp ? TYPE_QUAL_CONST : 0) 4201 | (restrictp ? TYPE_QUAL_RESTRICT : 0) 4202 | (volatilep ? TYPE_QUAL_VOLATILE : 0)); 4203 4204 /* Warn about storage classes that are invalid for certain 4205 kinds of declarations (parameters, typenames, etc.). */ 4206 4207 if (funcdef_flag 4208 && (threadp 4209 || storage_class == csc_auto 4210 || storage_class == csc_register 4211 || storage_class == csc_typedef)) 4212 { 4213 if (storage_class == csc_auto 4214 && (pedantic || current_scope == file_scope)) 4215 pedwarn ("function definition declared %<auto%>"); 4216 if (storage_class == csc_register) 4217 error ("function definition declared %<register%>"); 4218 if (storage_class == csc_typedef) 4219 error ("function definition declared %<typedef%>"); 4220 if (threadp) 4221 error ("function definition declared %<__thread%>"); 4222 threadp = false; 4223 if (storage_class == csc_auto 4224 || storage_class == csc_register 4225 || storage_class == csc_typedef) 4226 storage_class = csc_none; 4227 } 4228 else if (decl_context != NORMAL && (storage_class != csc_none || threadp)) 4229 { 4230 if (decl_context == PARM && storage_class == csc_register) 4231 ; 4232 else 4233 { 4234 switch (decl_context) 4235 { 4236 case FIELD: 4237 error ("storage class specified for structure field %qs", 4238 name); 4239 break; 4240 case PARM: 4241 error ("storage class specified for parameter %qs", name); 4242 break; 4243 default: 4244 error ("storage class specified for typename"); 4245 break; 4246 } 4247 storage_class = csc_none; 4248 threadp = false; 4249 } 4250 } 4251 else if (storage_class == csc_extern 4252 && initialized 4253 && !funcdef_flag) 4254 { 4255 /* 'extern' with initialization is invalid if not at file scope. */ 4256 if (current_scope == file_scope) 4257 { 4258 /* It is fine to have 'extern const' when compiling at C 4259 and C++ intersection. */ 4260 if (!(warn_cxx_compat && constp)) 4261 warning (0, "%qs initialized and declared %<extern%>", name); 4262 } 4263 else 4264 error ("%qs has both %<extern%> and initializer", name); 4265 } 4266 else if (current_scope == file_scope) 4267 { 4268 if (storage_class == csc_auto) 4269 error ("file-scope declaration of %qs specifies %<auto%>", name); 4270 if (pedantic && storage_class == csc_register) 4271 pedwarn ("file-scope declaration of %qs specifies %<register%>", name); 4272 } 4273 else 4274 { 4275 if (storage_class == csc_extern && funcdef_flag) 4276 error ("nested function %qs declared %<extern%>", name); 4277 else if (threadp && storage_class == csc_none) 4278 { 4279 error ("function-scope %qs implicitly auto and declared " 4280 "%<__thread%>", 4281 name); 4282 threadp = false; 4283 } 4284 } 4285 4286 /* Now figure out the structure of the declarator proper. 4287 Descend through it, creating more complex types, until we reach 4288 the declared identifier (or NULL_TREE, in an absolute declarator). 4289 At each stage we maintain an unqualified version of the type 4290 together with any qualifiers that should be applied to it with 4291 c_build_qualified_type; this way, array types including 4292 multidimensional array types are first built up in unqualified 4293 form and then the qualified form is created with 4294 TYPE_MAIN_VARIANT pointing to the unqualified form. */ 4295 4296 while (declarator && declarator->kind != cdk_id) 4297 { 4298 if (type == error_mark_node) 4299 { 4300 declarator = declarator->declarator; 4301 continue; 4302 } 4303 4304 /* Each level of DECLARATOR is either a cdk_array (for ...[..]), 4305 a cdk_pointer (for *...), 4306 a cdk_function (for ...(...)), 4307 a cdk_attrs (for nested attributes), 4308 or a cdk_id (for the name being declared 4309 or the place in an absolute declarator 4310 where the name was omitted). 4311 For the last case, we have just exited the loop. 4312 4313 At this point, TYPE is the type of elements of an array, 4314 or for a function to return, or for a pointer to point to. 4315 After this sequence of ifs, TYPE is the type of the 4316 array or function or pointer, and DECLARATOR has had its 4317 outermost layer removed. */ 4318 4319 if (array_ptr_quals != TYPE_UNQUALIFIED 4320 || array_ptr_attrs != NULL_TREE 4321 || array_parm_static) 4322 { 4323 /* Only the innermost declarator (making a parameter be of 4324 array type which is converted to pointer type) 4325 may have static or type qualifiers. */ 4326 error ("static or type qualifiers in non-parameter array declarator"); 4327 array_ptr_quals = TYPE_UNQUALIFIED; 4328 array_ptr_attrs = NULL_TREE; 4329 array_parm_static = 0; 4330 } 4331 4332 switch (declarator->kind) 4333 { 4334 case cdk_attrs: 4335 { 4336 /* A declarator with embedded attributes. */ 4337 tree attrs = declarator->u.attrs; 4338 const struct c_declarator *inner_decl; 4339 int attr_flags = 0; 4340 declarator = declarator->declarator; 4341 inner_decl = declarator; 4342 while (inner_decl->kind == cdk_attrs) 4343 inner_decl = inner_decl->declarator; 4344 if (inner_decl->kind == cdk_id) 4345 attr_flags |= (int) ATTR_FLAG_DECL_NEXT; 4346 else if (inner_decl->kind == cdk_function) 4347 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; 4348 else if (inner_decl->kind == cdk_array) 4349 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; 4350 returned_attrs = decl_attributes (&type, 4351 chainon (returned_attrs, attrs), 4352 attr_flags); 4353 break; 4354 } 4355 case cdk_array: 4356 { 4357 tree itype = NULL_TREE; 4358 tree size = declarator->u.array.dimen; 4359 /* The index is a signed object `sizetype' bits wide. */ 4360 tree index_type = c_common_signed_type (sizetype); 4361 4362 array_ptr_quals = declarator->u.array.quals; 4363 array_ptr_attrs = declarator->u.array.attrs; 4364 array_parm_static = declarator->u.array.static_p; 4365 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p; 4366 4367 declarator = declarator->declarator; 4368 4369 /* Check for some types that there cannot be arrays of. */ 4370 4371 if (VOID_TYPE_P (type)) 4372 { 4373 error ("declaration of %qs as array of voids", name); 4374 type = error_mark_node; 4375 } 4376 4377 if (TREE_CODE (type) == FUNCTION_TYPE) 4378 { 4379 error ("declaration of %qs as array of functions", name); 4380 type = error_mark_node; 4381 } 4382 4383 if (pedantic && !in_system_header && flexible_array_type_p (type)) 4384 pedwarn ("invalid use of structure with flexible array member"); 4385 4386 if (size == error_mark_node) 4387 type = error_mark_node; 4388 4389 if (type == error_mark_node) 4390 continue; 4391 4392 /* If size was specified, set ITYPE to a range-type for 4393 that size. Otherwise, ITYPE remains null. finish_decl 4394 may figure it out from an initial value. */ 4395 4396 if (size) 4397 { 4398 /* Strip NON_LVALUE_EXPRs since we aren't using as an 4399 lvalue. */ 4400 STRIP_TYPE_NOPS (size); 4401 4402 if (!INTEGRAL_TYPE_P (TREE_TYPE (size))) 4403 { 4404 error ("size of array %qs has non-integer type", name); 4405 size = integer_one_node; 4406 } 4407 4408 if (pedantic && integer_zerop (size)) 4409 pedwarn ("ISO C forbids zero-size array %qs", name); 4410 4411 if (TREE_CODE (size) == INTEGER_CST) 4412 { 4413 constant_expression_warning (size); 4414 if (tree_int_cst_sgn (size) < 0) 4415 { 4416 error ("size of array %qs is negative", name); 4417 size = integer_one_node; 4418 } 4419 } 4420 else if ((decl_context == NORMAL || decl_context == FIELD) 4421 && current_scope == file_scope) 4422 { 4423 error ("variably modified %qs at file scope", name); 4424 size = integer_one_node; 4425 } 4426 else 4427 { 4428 /* Make sure the array size remains visibly 4429 nonconstant even if it is (eg) a const variable 4430 with known value. */ 4431 size_varies = 1; 4432 warn_variable_length_array (orig_name, size); 4433 if (warn_variable_decl) 4434 warning (0, "variable-sized array %qs", name); 4435 } 4436 4437 if (integer_zerop (size)) 4438 { 4439 /* A zero-length array cannot be represented with 4440 an unsigned index type, which is what we'll 4441 get with build_index_type. Create an 4442 open-ended range instead. */ 4443 itype = build_range_type (sizetype, size, NULL_TREE); 4444 } 4445 else 4446 { 4447 /* Arrange for the SAVE_EXPR on the inside of the 4448 MINUS_EXPR, which allows the -1 to get folded 4449 with the +1 that happens when building TYPE_SIZE. */ 4450 if (size_varies) 4451 size = variable_size (size); 4452 4453 /* Compute the maximum valid index, that is, size 4454 - 1. Do the calculation in index_type, so that 4455 if it is a variable the computations will be 4456 done in the proper mode. */ 4457 itype = fold_build2 (MINUS_EXPR, index_type, 4458 convert (index_type, size), 4459 convert (index_type, 4460 size_one_node)); 4461 4462 /* If that overflowed, the array is too big. ??? 4463 While a size of INT_MAX+1 technically shouldn't 4464 cause an overflow (because we subtract 1), the 4465 overflow is recorded during the conversion to 4466 index_type, before the subtraction. Handling 4467 this case seems like an unnecessary 4468 complication. */ 4469 if (TREE_CODE (itype) == INTEGER_CST 4470 && TREE_OVERFLOW (itype)) 4471 { 4472 error ("size of array %qs is too large", name); 4473 type = error_mark_node; 4474 continue; 4475 } 4476 4477 itype = build_index_type (itype); 4478 } 4479 } 4480 else if (decl_context == FIELD) 4481 { 4482 if (pedantic && !flag_isoc99 && !in_system_header) 4483 pedwarn ("ISO C90 does not support flexible array members"); 4484 4485 /* ISO C99 Flexible array members are effectively 4486 identical to GCC's zero-length array extension. */ 4487 itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 4488 } 4489 else if (decl_context == PARM) 4490 { 4491 if (array_parm_vla_unspec_p) 4492 { 4493 if (! orig_name) 4494 { 4495 /* C99 6.7.5.2p4 */ 4496 error ("%<[*]%> not allowed in other than a declaration"); 4497 } 4498 4499 itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 4500 size_varies = 1; 4501 } 4502 } 4503 else if (decl_context == TYPENAME) 4504 { 4505 if (array_parm_vla_unspec_p) 4506 { 4507 /* The error is printed elsewhere. We use this to 4508 avoid messing up with incomplete array types of 4509 the same type, that would otherwise be modified 4510 below. */ 4511 itype = build_range_type (sizetype, size_zero_node, 4512 NULL_TREE); 4513 } 4514 } 4515 4516 /* Complain about arrays of incomplete types. */ 4517 if (!COMPLETE_TYPE_P (type)) 4518 { 4519 error ("array type has incomplete element type"); 4520 type = error_mark_node; 4521 } 4522 else 4523 /* When itype is NULL, a shared incomplete array type is 4524 returned for all array of a given type. Elsewhere we 4525 make sure we don't complete that type before copying 4526 it, but here we want to make sure we don't ever 4527 modify the shared type, so we gcc_assert (itype) 4528 below. */ 4529 type = build_array_type (type, itype); 4530 4531 if (type != error_mark_node) 4532 { 4533 if (size_varies) 4534 { 4535 /* It is ok to modify type here even if itype is 4536 NULL: if size_varies, we're in a 4537 multi-dimensional array and the inner type has 4538 variable size, so the enclosing shared array type 4539 must too. */ 4540 if (size && TREE_CODE (size) == INTEGER_CST) 4541 type 4542 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); 4543 C_TYPE_VARIABLE_SIZE (type) = 1; 4544 } 4545 4546 /* The GCC extension for zero-length arrays differs from 4547 ISO flexible array members in that sizeof yields 4548 zero. */ 4549 if (size && integer_zerop (size)) 4550 { 4551 gcc_assert (itype); 4552 TYPE_SIZE (type) = bitsize_zero_node; 4553 TYPE_SIZE_UNIT (type) = size_zero_node; 4554 } 4555 if (array_parm_vla_unspec_p) 4556 { 4557 gcc_assert (itype); 4558 /* The type is complete. C99 6.7.5.2p4 */ 4559 TYPE_SIZE (type) = bitsize_zero_node; 4560 TYPE_SIZE_UNIT (type) = size_zero_node; 4561 } 4562 } 4563 4564 if (decl_context != PARM 4565 && (array_ptr_quals != TYPE_UNQUALIFIED 4566 || array_ptr_attrs != NULL_TREE 4567 || array_parm_static)) 4568 { 4569 error ("static or type qualifiers in non-parameter array declarator"); 4570 array_ptr_quals = TYPE_UNQUALIFIED; 4571 array_ptr_attrs = NULL_TREE; 4572 array_parm_static = 0; 4573 } 4574 break; 4575 } 4576 case cdk_function: 4577 { 4578 /* Say it's a definition only for the declarator closest 4579 to the identifier, apart possibly from some 4580 attributes. */ 4581 bool really_funcdef = false; 4582 tree arg_types; 4583 if (funcdef_flag) 4584 { 4585 const struct c_declarator *t = declarator->declarator; 4586 while (t->kind == cdk_attrs) 4587 t = t->declarator; 4588 really_funcdef = (t->kind == cdk_id); 4589 } 4590 4591 /* Declaring a function type. Make sure we have a valid 4592 type for the function to return. */ 4593 if (type == error_mark_node) 4594 continue; 4595 4596 size_varies = 0; 4597 4598 /* Warn about some types functions can't return. */ 4599 if (TREE_CODE (type) == FUNCTION_TYPE) 4600 { 4601 error ("%qs declared as function returning a function", name); 4602 type = integer_type_node; 4603 } 4604 if (TREE_CODE (type) == ARRAY_TYPE) 4605 { 4606 error ("%qs declared as function returning an array", name); 4607 type = integer_type_node; 4608 } 4609 4610 /* Construct the function type and go to the next 4611 inner layer of declarator. */ 4612 arg_info = declarator->u.arg_info; 4613 arg_types = grokparms (arg_info, really_funcdef); 4614 if (really_funcdef) 4615 put_pending_sizes (arg_info->pending_sizes); 4616 4617 /* Type qualifiers before the return type of the function 4618 qualify the return type, not the function type. */ 4619 if (type_quals) 4620 { 4621 /* Type qualifiers on a function return type are 4622 normally permitted by the standard but have no 4623 effect, so give a warning at -Wreturn-type. 4624 Qualifiers on a void return type are banned on 4625 function definitions in ISO C; GCC used to used 4626 them for noreturn functions. */ 4627 if (VOID_TYPE_P (type) && really_funcdef) 4628 pedwarn ("function definition has qualified void return type"); 4629 else 4630 warning (OPT_Wreturn_type, 4631 "type qualifiers ignored on function return type"); 4632 4633 type = c_build_qualified_type (type, type_quals); 4634 } 4635 type_quals = TYPE_UNQUALIFIED; 4636 4637 type = build_function_type (type, arg_types); 4638 declarator = declarator->declarator; 4639 4640 /* Set the TYPE_CONTEXTs for each tagged type which is local to 4641 the formal parameter list of this FUNCTION_TYPE to point to 4642 the FUNCTION_TYPE node itself. */ 4643 { 4644 tree link; 4645 4646 for (link = arg_info->tags; 4647 link; 4648 link = TREE_CHAIN (link)) 4649 TYPE_CONTEXT (TREE_VALUE (link)) = type; 4650 } 4651 break; 4652 } 4653 case cdk_pointer: 4654 { 4655 /* Merge any constancy or volatility into the target type 4656 for the pointer. */ 4657 4658 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 4659 && type_quals) 4660 pedwarn ("ISO C forbids qualified function types"); 4661 if (type_quals) 4662 type = c_build_qualified_type (type, type_quals); 4663 size_varies = 0; 4664 4665 /* When the pointed-to type involves components of variable size, 4666 care must be taken to ensure that the size evaluation code is 4667 emitted early enough to dominate all the possible later uses 4668 and late enough for the variables on which it depends to have 4669 been assigned. 4670 4671 This is expected to happen automatically when the pointed-to 4672 type has a name/declaration of it's own, but special attention 4673 is required if the type is anonymous. 4674 4675 We handle the NORMAL and FIELD contexts here by attaching an 4676 artificial TYPE_DECL to such pointed-to type. This forces the 4677 sizes evaluation at a safe point and ensures it is not deferred 4678 until e.g. within a deeper conditional context. 4679 4680 We expect nothing to be needed here for PARM or TYPENAME. 4681 Pushing a TYPE_DECL at this point for TYPENAME would actually 4682 be incorrect, as we might be in the middle of an expression 4683 with side effects on the pointed-to type size "arguments" prior 4684 to the pointer declaration point and the fake TYPE_DECL in the 4685 enclosing context would force the size evaluation prior to the 4686 side effects. */ 4687 4688 if (!TYPE_NAME (type) 4689 && (decl_context == NORMAL || decl_context == FIELD) 4690 && variably_modified_type_p (type, NULL_TREE)) 4691 { 4692 tree decl = build_decl (TYPE_DECL, NULL_TREE, type); 4693 DECL_ARTIFICIAL (decl) = 1; 4694 pushdecl (decl); 4695 finish_decl (decl, NULL_TREE, NULL_TREE); 4696 TYPE_NAME (type) = decl; 4697 } 4698 4699 type = build_pointer_type (type); 4700 4701 /* Process type qualifiers (such as const or volatile) 4702 that were given inside the `*'. */ 4703 type_quals = declarator->u.pointer_quals; 4704 4705 declarator = declarator->declarator; 4706 break; 4707 } 4708 default: 4709 gcc_unreachable (); 4710 } 4711 } 4712 4713 /* Now TYPE has the actual type, apart from any qualifiers in 4714 TYPE_QUALS. */ 4715 4716 /* Check the type and width of a bit-field. */ 4717 if (bitfield) 4718 check_bitfield_type_and_width (&type, width, orig_name); 4719 4720 /* Did array size calculations overflow? */ 4721 4722 if (TREE_CODE (type) == ARRAY_TYPE 4723 && COMPLETE_TYPE_P (type) 4724 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST 4725 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type))) 4726 { 4727 error ("size of array %qs is too large", name); 4728 /* If we proceed with the array type as it is, we'll eventually 4729 crash in tree_low_cst(). */ 4730 type = error_mark_node; 4731 } 4732 4733 /* If this is declaring a typedef name, return a TYPE_DECL. */ 4734 4735 if (storage_class == csc_typedef) 4736 { 4737 tree decl; 4738 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 4739 && type_quals) 4740 pedwarn ("ISO C forbids qualified function types"); 4741 if (type_quals) 4742 type = c_build_qualified_type (type, type_quals); 4743 decl = build_decl (TYPE_DECL, declarator->u.id, type); 4744 if (declspecs->explicit_signed_p) 4745 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 4746 decl_attributes (&decl, returned_attrs, 0); 4747 if (declspecs->inline_p) 4748 pedwarn ("typedef %q+D declared %<inline%>", decl); 4749 return decl; 4750 } 4751 4752 /* If this is a type name (such as, in a cast or sizeof), 4753 compute the type and return it now. */ 4754 4755 if (decl_context == TYPENAME) 4756 { 4757 /* Note that the grammar rejects storage classes in typenames 4758 and fields. */ 4759 gcc_assert (storage_class == csc_none && !threadp 4760 && !declspecs->inline_p); 4761 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 4762 && type_quals) 4763 pedwarn ("ISO C forbids const or volatile function types"); 4764 if (type_quals) 4765 type = c_build_qualified_type (type, type_quals); 4766 decl_attributes (&type, returned_attrs, 0); 4767 return type; 4768 } 4769 4770 if (pedantic && decl_context == FIELD 4771 && variably_modified_type_p (type, NULL_TREE)) 4772 { 4773 /* C99 6.7.2.1p8 */ 4774 pedwarn ("a member of a structure or union cannot have a variably modified type"); 4775 } 4776 4777 /* Aside from typedefs and type names (handle above), 4778 `void' at top level (not within pointer) 4779 is allowed only in public variables. 4780 We don't complain about parms either, but that is because 4781 a better error message can be made later. */ 4782 4783 if (VOID_TYPE_P (type) && decl_context != PARM 4784 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE) 4785 && (storage_class == csc_extern 4786 || (current_scope == file_scope 4787 && !(storage_class == csc_static 4788 || storage_class == csc_register))))) 4789 { 4790 error ("variable or field %qs declared void", name); 4791 type = integer_type_node; 4792 } 4793 4794 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL 4795 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ 4796 4797 { 4798 tree decl; 4799 4800 if (decl_context == PARM) 4801 { 4802 tree type_as_written; 4803 tree promoted_type; 4804 4805 /* A parameter declared as an array of T is really a pointer to T. 4806 One declared as a function is really a pointer to a function. */ 4807 4808 if (TREE_CODE (type) == ARRAY_TYPE) 4809 { 4810 /* Transfer const-ness of array into that of type pointed to. */ 4811 type = TREE_TYPE (type); 4812 if (type_quals) 4813 type = c_build_qualified_type (type, type_quals); 4814 type = build_pointer_type (type); 4815 type_quals = array_ptr_quals; 4816 4817 /* We don't yet implement attributes in this context. */ 4818 if (array_ptr_attrs != NULL_TREE) 4819 warning (OPT_Wattributes, 4820 "attributes in parameter array declarator ignored"); 4821 4822 size_varies = 0; 4823 } 4824 else if (TREE_CODE (type) == FUNCTION_TYPE) 4825 { 4826 if (pedantic && type_quals) 4827 pedwarn ("ISO C forbids qualified function types"); 4828 if (type_quals) 4829 type = c_build_qualified_type (type, type_quals); 4830 type = build_pointer_type (type); 4831 type_quals = TYPE_UNQUALIFIED; 4832 } 4833 else if (type_quals) 4834 type = c_build_qualified_type (type, type_quals); 4835 4836 type_as_written = type; 4837 4838 decl = build_decl (PARM_DECL, declarator->u.id, type); 4839 if (size_varies) 4840 C_DECL_VARIABLE_SIZE (decl) = 1; 4841 4842 /* Compute the type actually passed in the parmlist, 4843 for the case where there is no prototype. 4844 (For example, shorts and chars are passed as ints.) 4845 When there is a prototype, this is overridden later. */ 4846 4847 if (type == error_mark_node) 4848 promoted_type = type; 4849 else 4850 promoted_type = c_type_promotes_to (type); 4851 4852 DECL_ARG_TYPE (decl) = promoted_type; 4853 if (declspecs->inline_p) 4854 pedwarn ("parameter %q+D declared %<inline%>", decl); 4855 } 4856 else if (decl_context == FIELD) 4857 { 4858 /* Note that the grammar rejects storage classes in typenames 4859 and fields. */ 4860 gcc_assert (storage_class == csc_none && !threadp 4861 && !declspecs->inline_p); 4862 4863 /* Structure field. It may not be a function. */ 4864 4865 if (TREE_CODE (type) == FUNCTION_TYPE) 4866 { 4867 error ("field %qs declared as a function", name); 4868 type = build_pointer_type (type); 4869 } 4870 else if (TREE_CODE (type) != ERROR_MARK 4871 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type)) 4872 { 4873 error ("field %qs has incomplete type", name); 4874 type = error_mark_node; 4875 } 4876 type = c_build_qualified_type (type, type_quals); 4877 decl = build_decl (FIELD_DECL, declarator->u.id, type); 4878 DECL_NONADDRESSABLE_P (decl) = bitfield; 4879 4880 if (size_varies) 4881 C_DECL_VARIABLE_SIZE (decl) = 1; 4882 } 4883 else if (TREE_CODE (type) == FUNCTION_TYPE) 4884 { 4885 if (storage_class == csc_register || threadp) 4886 { 4887 error ("invalid storage class for function %qs", name); 4888 } 4889 else if (current_scope != file_scope) 4890 { 4891 /* Function declaration not at file scope. Storage 4892 classes other than `extern' are not allowed, C99 4893 6.7.1p5, and `extern' makes no difference. However, 4894 GCC allows 'auto', perhaps with 'inline', to support 4895 nested functions. */ 4896 if (storage_class == csc_auto) 4897 { 4898 if (pedantic) 4899 pedwarn ("invalid storage class for function %qs", name); 4900 } 4901 else if (storage_class == csc_static) 4902 { 4903 error ("invalid storage class for function %qs", name); 4904 if (funcdef_flag) 4905 storage_class = declspecs->storage_class = csc_none; 4906 else 4907 return 0; 4908 } 4909 } 4910 4911 decl = build_decl (FUNCTION_DECL, declarator->u.id, type); 4912 decl = build_decl_attribute_variant (decl, decl_attr); 4913 4914 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl); 4915 4916 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl)) 4917 pedwarn ("ISO C forbids qualified function types"); 4918 4919 /* GNU C interprets a volatile-qualified function type to indicate 4920 that the function does not return. */ 4921 if ((type_quals & TYPE_QUAL_VOLATILE) 4922 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) 4923 warning (0, "%<noreturn%> function returns non-void value"); 4924 4925 /* Every function declaration is an external reference 4926 (DECL_EXTERNAL) except for those which are not at file 4927 scope and are explicitly declared "auto". This is 4928 forbidden by standard C (C99 6.7.1p5) and is interpreted by 4929 GCC to signify a forward declaration of a nested function. */ 4930 if (storage_class == csc_auto && current_scope != file_scope) 4931 DECL_EXTERNAL (decl) = 0; 4932 /* In C99, a function which is declared 'inline' with 'extern' 4933 is not an external reference (which is confusing). It 4934 means that the later definition of the function must be output 4935 in this file, C99 6.7.4p6. In GNU C89, a function declared 4936 'extern inline' is an external reference. */ 4937 else if (declspecs->inline_p && storage_class != csc_static) 4938 DECL_EXTERNAL (decl) = ((storage_class == csc_extern) 4939 == flag_gnu89_inline); 4940 else 4941 DECL_EXTERNAL (decl) = !initialized; 4942 4943 /* Record absence of global scope for `static' or `auto'. */ 4944 TREE_PUBLIC (decl) 4945 = !(storage_class == csc_static || storage_class == csc_auto); 4946 4947 /* For a function definition, record the argument information 4948 block where store_parm_decls will look for it. */ 4949 if (funcdef_flag) 4950 current_function_arg_info = arg_info; 4951 4952 if (declspecs->default_int_p) 4953 C_FUNCTION_IMPLICIT_INT (decl) = 1; 4954 4955 /* Record presence of `inline', if it is reasonable. */ 4956 if (flag_hosted && MAIN_NAME_P (declarator->u.id)) 4957 { 4958 if (declspecs->inline_p) 4959 pedwarn ("cannot inline function %<main%>"); 4960 } 4961 else if (declspecs->inline_p) 4962 { 4963 /* Record that the function is declared `inline'. */ 4964 DECL_DECLARED_INLINE_P (decl) = 1; 4965 4966 /* Do not mark bare declarations as DECL_INLINE. Doing so 4967 in the presence of multiple declarations can result in 4968 the abstract origin pointing between the declarations, 4969 which will confuse dwarf2out. */ 4970 if (initialized) 4971 DECL_INLINE (decl) = 1; 4972 } 4973 /* If -finline-functions, assume it can be inlined. This does 4974 two things: let the function be deferred until it is actually 4975 needed, and let dwarf2 know that the function is inlinable. */ 4976 else if (flag_inline_trees == 2 && initialized) 4977 DECL_INLINE (decl) = 1; 4978 } 4979 else 4980 { 4981 /* It's a variable. */ 4982 /* An uninitialized decl with `extern' is a reference. */ 4983 int extern_ref = !initialized && storage_class == csc_extern; 4984 4985 type = c_build_qualified_type (type, type_quals); 4986 4987 /* C99 6.2.2p7: It is invalid (compile-time undefined 4988 behavior) to create an 'extern' declaration for a 4989 variable if there is a global declaration that is 4990 'static' and the global declaration is not visible. 4991 (If the static declaration _is_ currently visible, 4992 the 'extern' declaration is taken to refer to that decl.) */ 4993 if (extern_ref && current_scope != file_scope) 4994 { 4995 tree global_decl = identifier_global_value (declarator->u.id); 4996 tree visible_decl = lookup_name (declarator->u.id); 4997 4998 if (global_decl 4999 && global_decl != visible_decl 5000 && TREE_CODE (global_decl) == VAR_DECL 5001 && !TREE_PUBLIC (global_decl)) 5002 error ("variable previously declared %<static%> redeclared " 5003 "%<extern%>"); 5004 } 5005 5006 decl = build_decl (VAR_DECL, declarator->u.id, type); 5007 DECL_SOURCE_LOCATION (decl) = declarator->id_loc; 5008 if (size_varies) 5009 C_DECL_VARIABLE_SIZE (decl) = 1; 5010 5011 if (declspecs->inline_p) 5012 pedwarn ("variable %q+D declared %<inline%>", decl); 5013 5014 /* At file scope, an initialized extern declaration may follow 5015 a static declaration. In that case, DECL_EXTERNAL will be 5016 reset later in start_decl. */ 5017 DECL_EXTERNAL (decl) = (storage_class == csc_extern); 5018 5019 /* At file scope, the presence of a `static' or `register' storage 5020 class specifier, or the absence of all storage class specifiers 5021 makes this declaration a definition (perhaps tentative). Also, 5022 the absence of `static' makes it public. */ 5023 if (current_scope == file_scope) 5024 { 5025 TREE_PUBLIC (decl) = storage_class != csc_static; 5026 TREE_STATIC (decl) = !extern_ref; 5027 } 5028 /* Not at file scope, only `static' makes a static definition. */ 5029 else 5030 { 5031 TREE_STATIC (decl) = (storage_class == csc_static); 5032 TREE_PUBLIC (decl) = extern_ref; 5033 } 5034 5035 if (threadp) 5036 { 5037 if (targetm.have_tls) 5038 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl); 5039 else 5040 /* A mere warning is sure to result in improper semantics 5041 at runtime. Don't bother to allow this to compile. */ 5042 error ("thread-local storage not supported for this target"); 5043 } 5044 } 5045 5046 if (storage_class == csc_extern 5047 && variably_modified_type_p (type, NULL_TREE)) 5048 { 5049 /* C99 6.7.5.2p2 */ 5050 error ("object with variably modified type must have no linkage"); 5051 } 5052 5053 /* Record `register' declaration for warnings on & 5054 and in case doing stupid register allocation. */ 5055 5056 if (storage_class == csc_register) 5057 { 5058 C_DECL_REGISTER (decl) = 1; 5059 DECL_REGISTER (decl) = 1; 5060 } 5061 5062 /* Record constancy and volatility. */ 5063 c_apply_type_quals_to_decl (type_quals, decl); 5064 5065 /* If a type has volatile components, it should be stored in memory. 5066 Otherwise, the fact that those components are volatile 5067 will be ignored, and would even crash the compiler. 5068 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */ 5069 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)) 5070 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL 5071 || TREE_CODE (decl) == RESULT_DECL)) 5072 { 5073 /* It is not an error for a structure with volatile fields to 5074 be declared register, but reset DECL_REGISTER since it 5075 cannot actually go in a register. */ 5076 int was_reg = C_DECL_REGISTER (decl); 5077 C_DECL_REGISTER (decl) = 0; 5078 DECL_REGISTER (decl) = 0; 5079 c_mark_addressable (decl); 5080 C_DECL_REGISTER (decl) = was_reg; 5081 } 5082 5083 /* This is the earliest point at which we might know the assembler 5084 name of a variable. Thus, if it's known before this, die horribly. */ 5085 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)); 5086 5087 decl_attributes (&decl, returned_attrs, 0); 5088 5089 return decl; 5090 } 5091} 5092 5093/* Decode the parameter-list info for a function type or function definition. 5094 The argument is the value returned by `get_parm_info' (or made in c-parse.c 5095 if there is an identifier list instead of a parameter decl list). 5096 These two functions are separate because when a function returns 5097 or receives functions then each is called multiple times but the order 5098 of calls is different. The last call to `grokparms' is always the one 5099 that contains the formal parameter names of a function definition. 5100 5101 Return a list of arg types to use in the FUNCTION_TYPE for this function. 5102 5103 FUNCDEF_FLAG is true for a function definition, false for 5104 a mere declaration. A nonempty identifier-list gets an error message 5105 when FUNCDEF_FLAG is false. */ 5106 5107static tree 5108grokparms (struct c_arg_info *arg_info, bool funcdef_flag) 5109{ 5110 tree arg_types = arg_info->types; 5111 5112 if (funcdef_flag && arg_info->had_vla_unspec) 5113 { 5114 /* A function definition isn't function prototype scope C99 6.2.1p4. */ 5115 /* C99 6.7.5.2p4 */ 5116 error ("%<[*]%> not allowed in other than function prototype scope"); 5117 } 5118 5119 if (arg_types == 0 && !funcdef_flag && !in_system_header) 5120 warning (OPT_Wstrict_prototypes, 5121 "function declaration isn%'t a prototype"); 5122 5123 if (arg_types == error_mark_node) 5124 return 0; /* don't set TYPE_ARG_TYPES in this case */ 5125 5126 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE) 5127 { 5128 if (!funcdef_flag) 5129 pedwarn ("parameter names (without types) in function declaration"); 5130 5131 arg_info->parms = arg_info->types; 5132 arg_info->types = 0; 5133 return 0; 5134 } 5135 else 5136 { 5137 tree parm, type, typelt; 5138 unsigned int parmno; 5139 5140 /* If there is a parameter of incomplete type in a definition, 5141 this is an error. In a declaration this is valid, and a 5142 struct or union type may be completed later, before any calls 5143 or definition of the function. In the case where the tag was 5144 first declared within the parameter list, a warning has 5145 already been given. If a parameter has void type, then 5146 however the function cannot be defined or called, so 5147 warn. */ 5148 5149 for (parm = arg_info->parms, typelt = arg_types, parmno = 1; 5150 parm; 5151 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++) 5152 { 5153 type = TREE_VALUE (typelt); 5154 if (type == error_mark_node) 5155 continue; 5156 5157 if (!COMPLETE_TYPE_P (type)) 5158 { 5159 if (funcdef_flag) 5160 { 5161 if (DECL_NAME (parm)) 5162 error ("parameter %u (%q+D) has incomplete type", 5163 parmno, parm); 5164 else 5165 error ("%Jparameter %u has incomplete type", 5166 parm, parmno); 5167 5168 TREE_VALUE (typelt) = error_mark_node; 5169 TREE_TYPE (parm) = error_mark_node; 5170 } 5171 else if (VOID_TYPE_P (type)) 5172 { 5173 if (DECL_NAME (parm)) 5174 warning (0, "parameter %u (%q+D) has void type", 5175 parmno, parm); 5176 else 5177 warning (0, "%Jparameter %u has void type", 5178 parm, parmno); 5179 } 5180 } 5181 5182 if (DECL_NAME (parm) && TREE_USED (parm)) 5183 warn_if_shadowing (parm); 5184 } 5185 return arg_types; 5186 } 5187} 5188 5189/* Take apart the current scope and return a c_arg_info structure with 5190 info on a parameter list just parsed. 5191 5192 This structure is later fed to 'grokparms' and 'store_parm_decls'. 5193 5194 ELLIPSIS being true means the argument list ended in '...' so don't 5195 append a sentinel (void_list_node) to the end of the type-list. */ 5196 5197struct c_arg_info * 5198get_parm_info (bool ellipsis) 5199{ 5200 struct c_binding *b = current_scope->bindings; 5201 struct c_arg_info *arg_info = XOBNEW (&parser_obstack, 5202 struct c_arg_info); 5203 tree parms = 0; 5204 tree tags = 0; 5205 tree types = 0; 5206 tree others = 0; 5207 5208 static bool explained_incomplete_types = false; 5209 bool gave_void_only_once_err = false; 5210 5211 arg_info->parms = 0; 5212 arg_info->tags = 0; 5213 arg_info->types = 0; 5214 arg_info->others = 0; 5215 arg_info->pending_sizes = 0; 5216 arg_info->had_vla_unspec = current_scope->had_vla_unspec; 5217 5218 /* The bindings in this scope must not get put into a block. 5219 We will take care of deleting the binding nodes. */ 5220 current_scope->bindings = 0; 5221 5222 /* This function is only called if there was *something* on the 5223 parameter list. */ 5224 gcc_assert (b); 5225 5226 /* A parameter list consisting solely of 'void' indicates that the 5227 function takes no arguments. But if the 'void' is qualified 5228 (by 'const' or 'volatile'), or has a storage class specifier 5229 ('register'), then the behavior is undefined; issue an error. 5230 Typedefs for 'void' are OK (see DR#157). */ 5231 if (b->prev == 0 /* one binding */ 5232 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */ 5233 && !DECL_NAME (b->decl) /* anonymous */ 5234 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */ 5235 { 5236 if (TREE_THIS_VOLATILE (b->decl) 5237 || TREE_READONLY (b->decl) 5238 || C_DECL_REGISTER (b->decl)) 5239 error ("%<void%> as only parameter may not be qualified"); 5240 5241 /* There cannot be an ellipsis. */ 5242 if (ellipsis) 5243 error ("%<void%> must be the only parameter"); 5244 5245 arg_info->types = void_list_node; 5246 return arg_info; 5247 } 5248 5249 if (!ellipsis) 5250 types = void_list_node; 5251 5252 /* Break up the bindings list into parms, tags, types, and others; 5253 apply sanity checks; purge the name-to-decl bindings. */ 5254 while (b) 5255 { 5256 tree decl = b->decl; 5257 tree type = TREE_TYPE (decl); 5258 const char *keyword; 5259 5260 switch (TREE_CODE (decl)) 5261 { 5262 case PARM_DECL: 5263 if (b->id) 5264 { 5265 gcc_assert (I_SYMBOL_BINDING (b->id) == b); 5266 I_SYMBOL_BINDING (b->id) = b->shadowed; 5267 } 5268 5269 /* Check for forward decls that never got their actual decl. */ 5270 if (TREE_ASM_WRITTEN (decl)) 5271 error ("parameter %q+D has just a forward declaration", decl); 5272 /* Check for (..., void, ...) and issue an error. */ 5273 else if (VOID_TYPE_P (type) && !DECL_NAME (decl)) 5274 { 5275 if (!gave_void_only_once_err) 5276 { 5277 error ("%<void%> must be the only parameter"); 5278 gave_void_only_once_err = true; 5279 } 5280 } 5281 else 5282 { 5283 /* Valid parameter, add it to the list. */ 5284 TREE_CHAIN (decl) = parms; 5285 parms = decl; 5286 5287 /* Since there is a prototype, args are passed in their 5288 declared types. The back end may override this later. */ 5289 DECL_ARG_TYPE (decl) = type; 5290 types = tree_cons (0, type, types); 5291 } 5292 break; 5293 5294 case ENUMERAL_TYPE: keyword = "enum"; goto tag; 5295 case UNION_TYPE: keyword = "union"; goto tag; 5296 case RECORD_TYPE: keyword = "struct"; goto tag; 5297 tag: 5298 /* Types may not have tag-names, in which case the type 5299 appears in the bindings list with b->id NULL. */ 5300 if (b->id) 5301 { 5302 gcc_assert (I_TAG_BINDING (b->id) == b); 5303 I_TAG_BINDING (b->id) = b->shadowed; 5304 } 5305 5306 /* Warn about any struct, union or enum tags defined in a 5307 parameter list. The scope of such types is limited to 5308 the parameter list, which is rarely if ever desirable 5309 (it's impossible to call such a function with type- 5310 correct arguments). An anonymous union parm type is 5311 meaningful as a GNU extension, so don't warn for that. */ 5312 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0) 5313 { 5314 if (b->id) 5315 /* The %s will be one of 'struct', 'union', or 'enum'. */ 5316 warning (0, "%<%s %E%> declared inside parameter list", 5317 keyword, b->id); 5318 else 5319 /* The %s will be one of 'struct', 'union', or 'enum'. */ 5320 warning (0, "anonymous %s declared inside parameter list", 5321 keyword); 5322 5323 if (!explained_incomplete_types) 5324 { 5325 warning (0, "its scope is only this definition or declaration," 5326 " which is probably not what you want"); 5327 explained_incomplete_types = true; 5328 } 5329 } 5330 5331 tags = tree_cons (b->id, decl, tags); 5332 break; 5333 5334 case CONST_DECL: 5335 case TYPE_DECL: 5336 case FUNCTION_DECL: 5337 /* CONST_DECLs appear here when we have an embedded enum, 5338 and TYPE_DECLs appear here when we have an embedded struct 5339 or union. No warnings for this - we already warned about the 5340 type itself. FUNCTION_DECLs appear when there is an implicit 5341 function declaration in the parameter list. */ 5342 5343 TREE_CHAIN (decl) = others; 5344 others = decl; 5345 /* fall through */ 5346 5347 case ERROR_MARK: 5348 /* error_mark_node appears here when we have an undeclared 5349 variable. Just throw it away. */ 5350 if (b->id) 5351 { 5352 gcc_assert (I_SYMBOL_BINDING (b->id) == b); 5353 I_SYMBOL_BINDING (b->id) = b->shadowed; 5354 } 5355 break; 5356 5357 /* Other things that might be encountered. */ 5358 case LABEL_DECL: 5359 case VAR_DECL: 5360 default: 5361 gcc_unreachable (); 5362 } 5363 5364 b = free_binding_and_advance (b); 5365 } 5366 5367 arg_info->parms = parms; 5368 arg_info->tags = tags; 5369 arg_info->types = types; 5370 arg_info->others = others; 5371 arg_info->pending_sizes = get_pending_sizes (); 5372 return arg_info; 5373} 5374 5375/* Get the struct, enum or union (CODE says which) with tag NAME. 5376 Define the tag as a forward-reference if it is not defined. 5377 Return a c_typespec structure for the type specifier. */ 5378 5379struct c_typespec 5380parser_xref_tag (enum tree_code code, tree name) 5381{ 5382 struct c_typespec ret; 5383 /* If a cross reference is requested, look up the type 5384 already defined for this tag and return it. */ 5385 5386 tree ref = lookup_tag (code, name, 0); 5387 /* If this is the right type of tag, return what we found. 5388 (This reference will be shadowed by shadow_tag later if appropriate.) 5389 If this is the wrong type of tag, do not return it. If it was the 5390 wrong type in the same scope, we will have had an error 5391 message already; if in a different scope and declaring 5392 a name, pending_xref_error will give an error message; but if in a 5393 different scope and not declaring a name, this tag should 5394 shadow the previous declaration of a different type of tag, and 5395 this would not work properly if we return the reference found. 5396 (For example, with "struct foo" in an outer scope, "union foo;" 5397 must shadow that tag with a new one of union type.) */ 5398 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref); 5399 if (ref && TREE_CODE (ref) == code) 5400 { 5401 ret.spec = ref; 5402 return ret; 5403 } 5404 5405 /* If no such tag is yet defined, create a forward-reference node 5406 and record it as the "definition". 5407 When a real declaration of this type is found, 5408 the forward-reference will be altered into a real type. */ 5409 5410 ref = make_node (code); 5411 if (code == ENUMERAL_TYPE) 5412 { 5413 /* Give the type a default layout like unsigned int 5414 to avoid crashing if it does not get defined. */ 5415 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node); 5416 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); 5417 TYPE_USER_ALIGN (ref) = 0; 5418 TYPE_UNSIGNED (ref) = 1; 5419 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); 5420 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); 5421 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); 5422 } 5423 5424 pushtag (name, ref); 5425 5426 ret.spec = ref; 5427 return ret; 5428} 5429 5430/* Get the struct, enum or union (CODE says which) with tag NAME. 5431 Define the tag as a forward-reference if it is not defined. 5432 Return a tree for the type. */ 5433 5434tree 5435xref_tag (enum tree_code code, tree name) 5436{ 5437 return parser_xref_tag (code, name).spec; 5438} 5439 5440/* Make sure that the tag NAME is defined *in the current scope* 5441 at least as a forward reference. 5442 CODE says which kind of tag NAME ought to be. */ 5443 5444tree 5445start_struct (enum tree_code code, tree name) 5446{ 5447 /* If there is already a tag defined at this scope 5448 (as a forward reference), just return it. */ 5449 5450 tree ref = 0; 5451 5452 if (name != 0) 5453 ref = lookup_tag (code, name, 1); 5454 if (ref && TREE_CODE (ref) == code) 5455 { 5456 if (TYPE_SIZE (ref)) 5457 { 5458 if (code == UNION_TYPE) 5459 error ("redefinition of %<union %E%>", name); 5460 else 5461 error ("redefinition of %<struct %E%>", name); 5462 } 5463 else if (C_TYPE_BEING_DEFINED (ref)) 5464 { 5465 if (code == UNION_TYPE) 5466 error ("nested redefinition of %<union %E%>", name); 5467 else 5468 error ("nested redefinition of %<struct %E%>", name); 5469 /* Don't create structures that contain themselves. */ 5470 ref = NULL_TREE; 5471 } 5472 } 5473 5474 /* Otherwise create a forward-reference just so the tag is in scope. */ 5475 5476 if (ref == NULL_TREE || TREE_CODE (ref) != code) 5477 { 5478 ref = make_node (code); 5479 pushtag (name, ref); 5480 } 5481 5482 C_TYPE_BEING_DEFINED (ref) = 1; 5483 TYPE_PACKED (ref) = flag_pack_struct; 5484 return ref; 5485} 5486 5487/* Process the specs, declarator and width (NULL if omitted) 5488 of a structure component, returning a FIELD_DECL node. 5489 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node. 5490 5491 This is done during the parsing of the struct declaration. 5492 The FIELD_DECL nodes are chained together and the lot of them 5493 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */ 5494 5495tree 5496grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs, 5497 tree width) 5498{ 5499 tree value; 5500 5501 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE 5502 && width == NULL_TREE) 5503 { 5504 /* This is an unnamed decl. 5505 5506 If we have something of the form "union { list } ;" then this 5507 is the anonymous union extension. Similarly for struct. 5508 5509 If this is something of the form "struct foo;", then 5510 If MS extensions are enabled, this is handled as an 5511 anonymous struct. 5512 Otherwise this is a forward declaration of a structure tag. 5513 5514 If this is something of the form "foo;" and foo is a TYPE_DECL, then 5515 If MS extensions are enabled and foo names a structure, then 5516 again this is an anonymous struct. 5517 Otherwise this is an error. 5518 5519 Oh what a horrid tangled web we weave. I wonder if MS consciously 5520 took this from Plan 9 or if it was an accident of implementation 5521 that took root before someone noticed the bug... */ 5522 5523 tree type = declspecs->type; 5524 bool type_ok = (TREE_CODE (type) == RECORD_TYPE 5525 || TREE_CODE (type) == UNION_TYPE); 5526 bool ok = false; 5527 5528 if (type_ok 5529 && (flag_ms_extensions || !declspecs->typedef_p)) 5530 { 5531 if (flag_ms_extensions) 5532 ok = true; 5533 else if (flag_iso) 5534 ok = false; 5535 else if (TYPE_NAME (type) == NULL) 5536 ok = true; 5537 else 5538 ok = false; 5539 } 5540 if (!ok) 5541 { 5542 pedwarn ("declaration does not declare anything"); 5543 return NULL_TREE; 5544 } 5545 if (pedantic) 5546 pedwarn ("ISO C doesn%'t support unnamed structs/unions"); 5547 } 5548 5549 value = grokdeclarator (declarator, declspecs, FIELD, false, 5550 width ? &width : NULL); 5551 5552 finish_decl (value, NULL_TREE, NULL_TREE); 5553 DECL_INITIAL (value) = width; 5554 5555 return value; 5556} 5557 5558/* Generate an error for any duplicate field names in FIELDLIST. Munge 5559 the list such that this does not present a problem later. */ 5560 5561static void 5562detect_field_duplicates (tree fieldlist) 5563{ 5564 tree x, y; 5565 int timeout = 10; 5566 5567 /* First, see if there are more than "a few" fields. 5568 This is trivially true if there are zero or one fields. */ 5569 if (!fieldlist) 5570 return; 5571 x = TREE_CHAIN (fieldlist); 5572 if (!x) 5573 return; 5574 do { 5575 timeout--; 5576 x = TREE_CHAIN (x); 5577 } while (timeout > 0 && x); 5578 5579 /* If there were "few" fields, avoid the overhead of allocating 5580 a hash table. Instead just do the nested traversal thing. */ 5581 if (timeout > 0) 5582 { 5583 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x)) 5584 if (DECL_NAME (x)) 5585 { 5586 for (y = fieldlist; y != x; y = TREE_CHAIN (y)) 5587 if (DECL_NAME (y) == DECL_NAME (x)) 5588 { 5589 error ("duplicate member %q+D", x); 5590 DECL_NAME (x) = NULL_TREE; 5591 } 5592 } 5593 } 5594 else 5595 { 5596 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); 5597 void **slot; 5598 5599 for (x = fieldlist; x ; x = TREE_CHAIN (x)) 5600 if ((y = DECL_NAME (x)) != 0) 5601 { 5602 slot = htab_find_slot (htab, y, INSERT); 5603 if (*slot) 5604 { 5605 error ("duplicate member %q+D", x); 5606 DECL_NAME (x) = NULL_TREE; 5607 } 5608 *slot = y; 5609 } 5610 5611 htab_delete (htab); 5612 } 5613} 5614 5615/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. 5616 FIELDLIST is a chain of FIELD_DECL nodes for the fields. 5617 ATTRIBUTES are attributes to be applied to the structure. */ 5618 5619tree 5620finish_struct (tree t, tree fieldlist, tree attributes) 5621{ 5622 tree x; 5623 bool toplevel = file_scope == current_scope; 5624 int saw_named_field; 5625 5626 /* If this type was previously laid out as a forward reference, 5627 make sure we lay it out again. */ 5628 5629 TYPE_SIZE (t) = 0; 5630 5631 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 5632 5633 if (pedantic) 5634 { 5635 for (x = fieldlist; x; x = TREE_CHAIN (x)) 5636 if (DECL_NAME (x) != 0) 5637 break; 5638 5639 if (x == 0) 5640 { 5641 if (TREE_CODE (t) == UNION_TYPE) 5642 { 5643 if (fieldlist) 5644 pedwarn ("union has no named members"); 5645 else 5646 pedwarn ("union has no members"); 5647 } 5648 else 5649 { 5650 if (fieldlist) 5651 pedwarn ("struct has no named members"); 5652 else 5653 pedwarn ("struct has no members"); 5654 } 5655 } 5656 } 5657 5658 /* Install struct as DECL_CONTEXT of each field decl. 5659 Also process specified field sizes, found in the DECL_INITIAL, 5660 storing 0 there after the type has been changed to precision equal 5661 to its width, rather than the precision of the specified standard 5662 type. (Correct layout requires the original type to have been preserved 5663 until now.) */ 5664 5665 saw_named_field = 0; 5666 for (x = fieldlist; x; x = TREE_CHAIN (x)) 5667 { 5668 if (TREE_TYPE (x) == error_mark_node) 5669 continue; 5670 5671 DECL_CONTEXT (x) = t; 5672 5673 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT) 5674 DECL_PACKED (x) = 1; 5675 5676 /* If any field is const, the structure type is pseudo-const. */ 5677 if (TREE_READONLY (x)) 5678 C_TYPE_FIELDS_READONLY (t) = 1; 5679 else 5680 { 5681 /* A field that is pseudo-const makes the structure likewise. */ 5682 tree t1 = TREE_TYPE (x); 5683 while (TREE_CODE (t1) == ARRAY_TYPE) 5684 t1 = TREE_TYPE (t1); 5685 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE) 5686 && C_TYPE_FIELDS_READONLY (t1)) 5687 C_TYPE_FIELDS_READONLY (t) = 1; 5688 } 5689 5690 /* Any field that is volatile means variables of this type must be 5691 treated in some ways as volatile. */ 5692 if (TREE_THIS_VOLATILE (x)) 5693 C_TYPE_FIELDS_VOLATILE (t) = 1; 5694 5695 /* Any field of nominal variable size implies structure is too. */ 5696 if (C_DECL_VARIABLE_SIZE (x)) 5697 C_TYPE_VARIABLE_SIZE (t) = 1; 5698 5699 if (DECL_INITIAL (x)) 5700 { 5701 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1); 5702 DECL_SIZE (x) = bitsize_int (width); 5703 DECL_BIT_FIELD (x) = 1; 5704 SET_DECL_C_BIT_FIELD (x); 5705 } 5706 5707 /* Detect flexible array member in an invalid context. */ 5708 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 5709 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 5710 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 5711 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 5712 { 5713 if (TREE_CODE (t) == UNION_TYPE) 5714 { 5715 error ("%Jflexible array member in union", x); 5716 TREE_TYPE (x) = error_mark_node; 5717 } 5718 else if (TREE_CHAIN (x) != NULL_TREE) 5719 { 5720 error ("%Jflexible array member not at end of struct", x); 5721 TREE_TYPE (x) = error_mark_node; 5722 } 5723 else if (!saw_named_field) 5724 { 5725 error ("%Jflexible array member in otherwise empty struct", x); 5726 TREE_TYPE (x) = error_mark_node; 5727 } 5728 } 5729 5730 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE 5731 && flexible_array_type_p (TREE_TYPE (x))) 5732 pedwarn ("%Jinvalid use of structure with flexible array member", x); 5733 5734 if (DECL_NAME (x)) 5735 saw_named_field = 1; 5736 } 5737 5738 detect_field_duplicates (fieldlist); 5739 5740 /* Now we have the nearly final fieldlist. Record it, 5741 then lay out the structure or union (including the fields). */ 5742 5743 TYPE_FIELDS (t) = fieldlist; 5744 5745 layout_type (t); 5746 5747 /* Give bit-fields their proper types. */ 5748 { 5749 tree *fieldlistp = &fieldlist; 5750 while (*fieldlistp) 5751 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp) 5752 && TREE_TYPE (*fieldlistp) != error_mark_node) 5753 { 5754 unsigned HOST_WIDE_INT width 5755 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1); 5756 tree type = TREE_TYPE (*fieldlistp); 5757 if (width != TYPE_PRECISION (type)) 5758 { 5759 TREE_TYPE (*fieldlistp) 5760 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type)); 5761 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp)); 5762 } 5763 DECL_INITIAL (*fieldlistp) = 0; 5764 } 5765 else 5766 fieldlistp = &TREE_CHAIN (*fieldlistp); 5767 } 5768 5769 /* Now we have the truly final field list. 5770 Store it in this type and in the variants. */ 5771 5772 TYPE_FIELDS (t) = fieldlist; 5773 5774 /* If there are lots of fields, sort so we can look through them fast. 5775 We arbitrarily consider 16 or more elts to be "a lot". */ 5776 5777 { 5778 int len = 0; 5779 5780 for (x = fieldlist; x; x = TREE_CHAIN (x)) 5781 { 5782 if (len > 15 || DECL_NAME (x) == NULL) 5783 break; 5784 len += 1; 5785 } 5786 5787 if (len > 15) 5788 { 5789 tree *field_array; 5790 struct lang_type *space; 5791 struct sorted_fields_type *space2; 5792 5793 len += list_length (x); 5794 5795 /* Use the same allocation policy here that make_node uses, to 5796 ensure that this lives as long as the rest of the struct decl. 5797 All decls in an inline function need to be saved. */ 5798 5799 space = GGC_CNEW (struct lang_type); 5800 space2 = GGC_NEWVAR (struct sorted_fields_type, 5801 sizeof (struct sorted_fields_type) + len * sizeof (tree)); 5802 5803 len = 0; 5804 space->s = space2; 5805 field_array = &space2->elts[0]; 5806 for (x = fieldlist; x; x = TREE_CHAIN (x)) 5807 { 5808 field_array[len++] = x; 5809 5810 /* If there is anonymous struct or union, break out of the loop. */ 5811 if (DECL_NAME (x) == NULL) 5812 break; 5813 } 5814 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */ 5815 if (x == NULL) 5816 { 5817 TYPE_LANG_SPECIFIC (t) = space; 5818 TYPE_LANG_SPECIFIC (t)->s->len = len; 5819 field_array = TYPE_LANG_SPECIFIC (t)->s->elts; 5820 qsort (field_array, len, sizeof (tree), field_decl_cmp); 5821 } 5822 } 5823 } 5824 5825 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) 5826 { 5827 TYPE_FIELDS (x) = TYPE_FIELDS (t); 5828 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t); 5829 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t); 5830 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t); 5831 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t); 5832 } 5833 5834 /* If this was supposed to be a transparent union, but we can't 5835 make it one, warn and turn off the flag. */ 5836 if (TREE_CODE (t) == UNION_TYPE 5837 && TYPE_TRANSPARENT_UNION (t) 5838 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))) 5839 { 5840 TYPE_TRANSPARENT_UNION (t) = 0; 5841 warning (0, "union cannot be made transparent"); 5842 } 5843 5844 /* If this structure or union completes the type of any previous 5845 variable declaration, lay it out and output its rtl. */ 5846 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)); 5847 x; 5848 x = TREE_CHAIN (x)) 5849 { 5850 tree decl = TREE_VALUE (x); 5851 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) 5852 layout_array_type (TREE_TYPE (decl)); 5853 if (TREE_CODE (decl) != TYPE_DECL) 5854 { 5855 layout_decl (decl, 0); 5856 if (c_dialect_objc ()) 5857 objc_check_decl (decl); 5858 rest_of_decl_compilation (decl, toplevel, 0); 5859 if (!toplevel) 5860 expand_decl (decl); 5861 } 5862 } 5863 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0; 5864 5865 /* Finish debugging output for this type. */ 5866 rest_of_type_compilation (t, toplevel); 5867 5868 /* If we're inside a function proper, i.e. not file-scope and not still 5869 parsing parameters, then arrange for the size of a variable sized type 5870 to be bound now. */ 5871 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE)) 5872 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t))); 5873 5874 return t; 5875} 5876 5877/* Lay out the type T, and its element type, and so on. */ 5878 5879static void 5880layout_array_type (tree t) 5881{ 5882 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 5883 layout_array_type (TREE_TYPE (t)); 5884 layout_type (t); 5885} 5886 5887/* Begin compiling the definition of an enumeration type. 5888 NAME is its name (or null if anonymous). 5889 Returns the type object, as yet incomplete. 5890 Also records info about it so that build_enumerator 5891 may be used to declare the individual values as they are read. */ 5892 5893tree 5894start_enum (tree name) 5895{ 5896 tree enumtype = 0; 5897 5898 /* If this is the real definition for a previous forward reference, 5899 fill in the contents in the same object that used to be the 5900 forward reference. */ 5901 5902 if (name != 0) 5903 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1); 5904 5905 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE) 5906 { 5907 enumtype = make_node (ENUMERAL_TYPE); 5908 pushtag (name, enumtype); 5909 } 5910 5911 if (C_TYPE_BEING_DEFINED (enumtype)) 5912 error ("nested redefinition of %<enum %E%>", name); 5913 5914 C_TYPE_BEING_DEFINED (enumtype) = 1; 5915 5916 if (TYPE_VALUES (enumtype) != 0) 5917 { 5918 /* This enum is a named one that has been declared already. */ 5919 error ("redeclaration of %<enum %E%>", name); 5920 5921 /* Completely replace its old definition. 5922 The old enumerators remain defined, however. */ 5923 TYPE_VALUES (enumtype) = 0; 5924 } 5925 5926 enum_next_value = integer_zero_node; 5927 enum_overflow = 0; 5928 5929 if (flag_short_enums) 5930 TYPE_PACKED (enumtype) = 1; 5931 5932 return enumtype; 5933} 5934 5935/* After processing and defining all the values of an enumeration type, 5936 install their decls in the enumeration type and finish it off. 5937 ENUMTYPE is the type object, VALUES a list of decl-value pairs, 5938 and ATTRIBUTES are the specified attributes. 5939 Returns ENUMTYPE. */ 5940 5941tree 5942finish_enum (tree enumtype, tree values, tree attributes) 5943{ 5944 tree pair, tem; 5945 tree minnode = 0, maxnode = 0; 5946 int precision, unsign; 5947 bool toplevel = (file_scope == current_scope); 5948 struct lang_type *lt; 5949 5950 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 5951 5952 /* Calculate the maximum value of any enumerator in this type. */ 5953 5954 if (values == error_mark_node) 5955 minnode = maxnode = integer_zero_node; 5956 else 5957 { 5958 minnode = maxnode = TREE_VALUE (values); 5959 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair)) 5960 { 5961 tree value = TREE_VALUE (pair); 5962 if (tree_int_cst_lt (maxnode, value)) 5963 maxnode = value; 5964 if (tree_int_cst_lt (value, minnode)) 5965 minnode = value; 5966 } 5967 } 5968 5969 /* Construct the final type of this enumeration. It is the same 5970 as one of the integral types - the narrowest one that fits, except 5971 that normally we only go as narrow as int - and signed iff any of 5972 the values are negative. */ 5973 unsign = (tree_int_cst_sgn (minnode) >= 0); 5974 precision = MAX (min_precision (minnode, unsign), 5975 min_precision (maxnode, unsign)); 5976 5977 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node)) 5978 { 5979 tem = c_common_type_for_size (precision, unsign); 5980 if (tem == NULL) 5981 { 5982 warning (0, "enumeration values exceed range of largest integer"); 5983 tem = long_long_integer_type_node; 5984 } 5985 } 5986 else 5987 tem = unsign ? unsigned_type_node : integer_type_node; 5988 5989 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem); 5990 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem); 5991 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem); 5992 TYPE_SIZE (enumtype) = 0; 5993 5994 /* If the precision of the type was specific with an attribute and it 5995 was too small, give an error. Otherwise, use it. */ 5996 if (TYPE_PRECISION (enumtype)) 5997 { 5998 if (precision > TYPE_PRECISION (enumtype)) 5999 error ("specified mode too small for enumeral values"); 6000 } 6001 else 6002 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem); 6003 6004 layout_type (enumtype); 6005 6006 if (values != error_mark_node) 6007 { 6008 /* Change the type of the enumerators to be the enum type. We 6009 need to do this irrespective of the size of the enum, for 6010 proper type checking. Replace the DECL_INITIALs of the 6011 enumerators, and the value slots of the list, with copies 6012 that have the enum type; they cannot be modified in place 6013 because they may be shared (e.g. integer_zero_node) Finally, 6014 change the purpose slots to point to the names of the decls. */ 6015 for (pair = values; pair; pair = TREE_CHAIN (pair)) 6016 { 6017 tree enu = TREE_PURPOSE (pair); 6018 tree ini = DECL_INITIAL (enu); 6019 6020 TREE_TYPE (enu) = enumtype; 6021 6022 /* The ISO C Standard mandates enumerators to have type int, 6023 even though the underlying type of an enum type is 6024 unspecified. Here we convert any enumerators that fit in 6025 an int to type int, to avoid promotions to unsigned types 6026 when comparing integers with enumerators that fit in the 6027 int range. When -pedantic is given, build_enumerator() 6028 would have already taken care of those that don't fit. */ 6029 if (int_fits_type_p (ini, integer_type_node)) 6030 tem = integer_type_node; 6031 else 6032 tem = enumtype; 6033 ini = convert (tem, ini); 6034 6035 DECL_INITIAL (enu) = ini; 6036 TREE_PURPOSE (pair) = DECL_NAME (enu); 6037 TREE_VALUE (pair) = ini; 6038 } 6039 6040 TYPE_VALUES (enumtype) = values; 6041 } 6042 6043 /* Record the min/max values so that we can warn about bit-field 6044 enumerations that are too small for the values. */ 6045 lt = GGC_CNEW (struct lang_type); 6046 lt->enum_min = minnode; 6047 lt->enum_max = maxnode; 6048 TYPE_LANG_SPECIFIC (enumtype) = lt; 6049 6050 /* Fix up all variant types of this enum type. */ 6051 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem)) 6052 { 6053 if (tem == enumtype) 6054 continue; 6055 TYPE_VALUES (tem) = TYPE_VALUES (enumtype); 6056 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype); 6057 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype); 6058 TYPE_SIZE (tem) = TYPE_SIZE (enumtype); 6059 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype); 6060 TYPE_MODE (tem) = TYPE_MODE (enumtype); 6061 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype); 6062 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype); 6063 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype); 6064 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype); 6065 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype); 6066 } 6067 6068 /* Finish debugging output for this type. */ 6069 rest_of_type_compilation (enumtype, toplevel); 6070 6071 return enumtype; 6072} 6073 6074/* Build and install a CONST_DECL for one value of the 6075 current enumeration type (one that was begun with start_enum). 6076 Return a tree-list containing the CONST_DECL and its value. 6077 Assignment of sequential values by default is handled here. */ 6078 6079tree 6080build_enumerator (tree name, tree value) 6081{ 6082 tree decl, type; 6083 6084 /* Validate and default VALUE. */ 6085 6086 if (value != 0) 6087 { 6088 /* Don't issue more errors for error_mark_node (i.e. an 6089 undeclared identifier) - just ignore the value expression. */ 6090 if (value == error_mark_node) 6091 value = 0; 6092 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)) 6093 || TREE_CODE (value) != INTEGER_CST) 6094 { 6095 error ("enumerator value for %qE is not an integer constant", name); 6096 value = 0; 6097 } 6098 else 6099 { 6100 value = default_conversion (value); 6101 constant_expression_warning (value); 6102 } 6103 } 6104 6105 /* Default based on previous value. */ 6106 /* It should no longer be possible to have NON_LVALUE_EXPR 6107 in the default. */ 6108 if (value == 0) 6109 { 6110 value = enum_next_value; 6111 if (enum_overflow) 6112 error ("overflow in enumeration values"); 6113 } 6114 6115 if (pedantic && !int_fits_type_p (value, integer_type_node)) 6116 { 6117 pedwarn ("ISO C restricts enumerator values to range of %<int%>"); 6118 /* XXX This causes -pedantic to change the meaning of the program. 6119 Remove? -zw 2004-03-15 */ 6120 value = convert (integer_type_node, value); 6121 } 6122 6123 /* Set basis for default for next value. */ 6124 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0); 6125 enum_overflow = tree_int_cst_lt (enum_next_value, value); 6126 6127 /* Now create a declaration for the enum value name. */ 6128 6129 type = TREE_TYPE (value); 6130 type = c_common_type_for_size (MAX (TYPE_PRECISION (type), 6131 TYPE_PRECISION (integer_type_node)), 6132 (TYPE_PRECISION (type) 6133 >= TYPE_PRECISION (integer_type_node) 6134 && TYPE_UNSIGNED (type))); 6135 6136 decl = build_decl (CONST_DECL, name, type); 6137 DECL_INITIAL (decl) = convert (type, value); 6138 pushdecl (decl); 6139 6140 return tree_cons (decl, value, NULL_TREE); 6141} 6142 6143 6144/* Create the FUNCTION_DECL for a function definition. 6145 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of 6146 the declaration; they describe the function's name and the type it returns, 6147 but twisted together in a fashion that parallels the syntax of C. 6148 6149 This function creates a binding context for the function body 6150 as well as setting up the FUNCTION_DECL in current_function_decl. 6151 6152 Returns 1 on success. If the DECLARATOR is not suitable for a function 6153 (it defines a datum instead), we return 0, which tells 6154 yyparse to report a parse error. */ 6155 6156int 6157start_function (struct c_declspecs *declspecs, struct c_declarator *declarator, 6158 tree attributes) 6159{ 6160 tree decl1, old_decl; 6161 tree restype, resdecl; 6162 struct c_label_context_se *nstack_se; 6163 struct c_label_context_vm *nstack_vm; 6164 6165 current_function_returns_value = 0; /* Assume, until we see it does. */ 6166 current_function_returns_null = 0; 6167 current_function_returns_abnormally = 0; 6168 warn_about_return_type = 0; 6169 c_switch_stack = NULL; 6170 6171 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se); 6172 nstack_se->labels_def = NULL; 6173 nstack_se->labels_used = NULL; 6174 nstack_se->next = label_context_stack_se; 6175 label_context_stack_se = nstack_se; 6176 6177 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm); 6178 nstack_vm->labels_def = NULL; 6179 nstack_vm->labels_used = NULL; 6180 nstack_vm->scope = 0; 6181 nstack_vm->next = label_context_stack_vm; 6182 label_context_stack_vm = nstack_vm; 6183 6184 /* Indicate no valid break/continue context by setting these variables 6185 to some non-null, non-label value. We'll notice and emit the proper 6186 error message in c_finish_bc_stmt. */ 6187 c_break_label = c_cont_label = size_zero_node; 6188 6189 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL); 6190 6191 /* If the declarator is not suitable for a function definition, 6192 cause a syntax error. */ 6193 if (decl1 == 0) 6194 { 6195 label_context_stack_se = label_context_stack_se->next; 6196 label_context_stack_vm = label_context_stack_vm->next; 6197 return 0; 6198 } 6199 6200 decl_attributes (&decl1, attributes, 0); 6201 6202 if (DECL_DECLARED_INLINE_P (decl1) 6203 && DECL_UNINLINABLE (decl1) 6204 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1))) 6205 warning (OPT_Wattributes, "inline function %q+D given attribute noinline", 6206 decl1); 6207 6208 /* Handle gnu_inline attribute. */ 6209 if (declspecs->inline_p 6210 && !flag_gnu89_inline 6211 && TREE_CODE (decl1) == FUNCTION_DECL 6212 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))) 6213 { 6214 if (declspecs->storage_class != csc_static) 6215 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1); 6216 } 6217 6218 announce_function (decl1); 6219 6220 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1)))) 6221 { 6222 error ("return type is an incomplete type"); 6223 /* Make it return void instead. */ 6224 TREE_TYPE (decl1) 6225 = build_function_type (void_type_node, 6226 TYPE_ARG_TYPES (TREE_TYPE (decl1))); 6227 } 6228 6229 if (warn_about_return_type) 6230 pedwarn_c99 ("return type defaults to %<int%>"); 6231 6232 /* Make the init_value nonzero so pushdecl knows this is not tentative. 6233 error_mark_node is replaced below (in pop_scope) with the BLOCK. */ 6234 DECL_INITIAL (decl1) = error_mark_node; 6235 6236 /* If this definition isn't a prototype and we had a prototype declaration 6237 before, copy the arg type info from that prototype. */ 6238 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope); 6239 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL) 6240 old_decl = 0; 6241 current_function_prototype_locus = UNKNOWN_LOCATION; 6242 current_function_prototype_built_in = false; 6243 current_function_prototype_arg_types = NULL_TREE; 6244 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0) 6245 { 6246 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE 6247 && comptypes (TREE_TYPE (TREE_TYPE (decl1)), 6248 TREE_TYPE (TREE_TYPE (old_decl)))) 6249 { 6250 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl), 6251 TREE_TYPE (decl1)); 6252 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl); 6253 current_function_prototype_built_in 6254 = C_DECL_BUILTIN_PROTOTYPE (old_decl); 6255 current_function_prototype_arg_types 6256 = TYPE_ARG_TYPES (TREE_TYPE (decl1)); 6257 } 6258 if (TREE_PUBLIC (decl1)) 6259 { 6260 /* If there is an external prototype declaration of this 6261 function, record its location but do not copy information 6262 to this decl. This may be an invisible declaration 6263 (built-in or in a scope which has finished) or simply 6264 have more refined argument types than any declaration 6265 found above. */ 6266 struct c_binding *b; 6267 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed) 6268 if (B_IN_SCOPE (b, external_scope)) 6269 break; 6270 if (b) 6271 { 6272 tree ext_decl, ext_type; 6273 ext_decl = b->decl; 6274 ext_type = b->type ? b->type : TREE_TYPE (ext_decl); 6275 if (TREE_CODE (ext_type) == FUNCTION_TYPE 6276 && comptypes (TREE_TYPE (TREE_TYPE (decl1)), 6277 TREE_TYPE (ext_type))) 6278 { 6279 current_function_prototype_locus 6280 = DECL_SOURCE_LOCATION (ext_decl); 6281 current_function_prototype_built_in 6282 = C_DECL_BUILTIN_PROTOTYPE (ext_decl); 6283 current_function_prototype_arg_types 6284 = TYPE_ARG_TYPES (ext_type); 6285 } 6286 } 6287 } 6288 } 6289 6290 /* Optionally warn of old-fashioned def with no previous prototype. */ 6291 if (warn_strict_prototypes 6292 && old_decl != error_mark_node 6293 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0 6294 && C_DECL_ISNT_PROTOTYPE (old_decl)) 6295 warning (OPT_Wstrict_prototypes, 6296 "function declaration isn%'t a prototype"); 6297 /* Optionally warn of any global def with no previous prototype. */ 6298 else if (warn_missing_prototypes 6299 && old_decl != error_mark_node 6300 && TREE_PUBLIC (decl1) 6301 && !MAIN_NAME_P (DECL_NAME (decl1)) 6302 && C_DECL_ISNT_PROTOTYPE (old_decl)) 6303 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1); 6304 /* Optionally warn of any def with no previous prototype 6305 if the function has already been used. */ 6306 else if (warn_missing_prototypes 6307 && old_decl != 0 6308 && old_decl != error_mark_node 6309 && TREE_USED (old_decl) 6310 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0) 6311 warning (OPT_Wmissing_prototypes, 6312 "%q+D was used with no prototype before its definition", decl1); 6313 /* Optionally warn of any global def with no previous declaration. */ 6314 else if (warn_missing_declarations 6315 && TREE_PUBLIC (decl1) 6316 && old_decl == 0 6317 && !MAIN_NAME_P (DECL_NAME (decl1))) 6318 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D", 6319 decl1); 6320 /* Optionally warn of any def with no previous declaration 6321 if the function has already been used. */ 6322 else if (warn_missing_declarations 6323 && old_decl != 0 6324 && old_decl != error_mark_node 6325 && TREE_USED (old_decl) 6326 && C_DECL_IMPLICIT (old_decl)) 6327 warning (OPT_Wmissing_declarations, 6328 "%q+D was used with no declaration before its definition", decl1); 6329 6330 /* This function exists in static storage. 6331 (This does not mean `static' in the C sense!) */ 6332 TREE_STATIC (decl1) = 1; 6333 6334 /* A nested function is not global. */ 6335 if (current_function_decl != 0) 6336 TREE_PUBLIC (decl1) = 0; 6337 6338 /* This is the earliest point at which we might know the assembler 6339 name of the function. Thus, if it's set before this, die horribly. */ 6340 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1)); 6341 6342 /* If #pragma weak was used, mark the decl weak now. */ 6343 if (current_scope == file_scope) 6344 maybe_apply_pragma_weak (decl1); 6345 6346 /* Warn for unlikely, improbable, or stupid declarations of `main'. */ 6347 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1))) 6348 { 6349 tree args; 6350 int argct = 0; 6351 6352 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1))) 6353 != integer_type_node) 6354 pedwarn ("return type of %q+D is not %<int%>", decl1); 6355 6356 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args; 6357 args = TREE_CHAIN (args)) 6358 { 6359 tree type = args ? TREE_VALUE (args) : 0; 6360 6361 if (type == void_type_node) 6362 break; 6363 6364 ++argct; 6365 switch (argct) 6366 { 6367 case 1: 6368 if (TYPE_MAIN_VARIANT (type) != integer_type_node) 6369 pedwarn ("first argument of %q+D should be %<int%>", decl1); 6370 break; 6371 6372 case 2: 6373 if (TREE_CODE (type) != POINTER_TYPE 6374 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 6375 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 6376 != char_type_node)) 6377 pedwarn ("second argument of %q+D should be %<char **%>", 6378 decl1); 6379 break; 6380 6381 case 3: 6382 if (TREE_CODE (type) != POINTER_TYPE 6383 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 6384 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 6385 != char_type_node)) 6386 pedwarn ("third argument of %q+D should probably be " 6387 "%<char **%>", decl1); 6388 break; 6389 } 6390 } 6391 6392 /* It is intentional that this message does not mention the third 6393 argument because it's only mentioned in an appendix of the 6394 standard. */ 6395 if (argct > 0 && (argct < 2 || argct > 3)) 6396 pedwarn ("%q+D takes only zero or two arguments", decl1); 6397 6398 if (!TREE_PUBLIC (decl1)) 6399 pedwarn ("%q+D is normally a non-static function", decl1); 6400 } 6401 6402 /* Record the decl so that the function name is defined. 6403 If we already have a decl for this name, and it is a FUNCTION_DECL, 6404 use the old decl. */ 6405 6406 current_function_decl = pushdecl (decl1); 6407 6408 push_scope (); 6409 declare_parm_level (); 6410 6411 restype = TREE_TYPE (TREE_TYPE (current_function_decl)); 6412 /* Promote the value to int before returning it. */ 6413 if (c_promoting_integer_type_p (restype)) 6414 { 6415 /* It retains unsignedness if not really getting wider. */ 6416 if (TYPE_UNSIGNED (restype) 6417 && (TYPE_PRECISION (restype) 6418 == TYPE_PRECISION (integer_type_node))) 6419 restype = unsigned_type_node; 6420 else 6421 restype = integer_type_node; 6422 } 6423 6424 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype); 6425 DECL_ARTIFICIAL (resdecl) = 1; 6426 DECL_IGNORED_P (resdecl) = 1; 6427 DECL_RESULT (current_function_decl) = resdecl; 6428 6429 start_fname_decls (); 6430 6431 return 1; 6432} 6433 6434/* Subroutine of store_parm_decls which handles new-style function 6435 definitions (prototype format). The parms already have decls, so we 6436 need only record them as in effect and complain if any redundant 6437 old-style parm decls were written. */ 6438static void 6439store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info) 6440{ 6441 tree decl; 6442 6443 if (current_scope->bindings) 6444 { 6445 error ("%Jold-style parameter declarations in prototyped " 6446 "function definition", fndecl); 6447 6448 /* Get rid of the old-style declarations. */ 6449 pop_scope (); 6450 push_scope (); 6451 } 6452 /* Don't issue this warning for nested functions, and don't issue this 6453 warning if we got here because ARG_INFO_TYPES was error_mark_node 6454 (this happens when a function definition has just an ellipsis in 6455 its parameter list). */ 6456 else if (!in_system_header && !current_function_scope 6457 && arg_info->types != error_mark_node) 6458 warning (OPT_Wtraditional, 6459 "%Jtraditional C rejects ISO C style function definitions", 6460 fndecl); 6461 6462 /* Now make all the parameter declarations visible in the function body. 6463 We can bypass most of the grunt work of pushdecl. */ 6464 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl)) 6465 { 6466 DECL_CONTEXT (decl) = current_function_decl; 6467 if (DECL_NAME (decl)) 6468 { 6469 bind (DECL_NAME (decl), decl, current_scope, 6470 /*invisible=*/false, /*nested=*/false); 6471 if (!TREE_USED (decl)) 6472 warn_if_shadowing (decl); 6473 } 6474 else 6475 error ("%Jparameter name omitted", decl); 6476 } 6477 6478 /* Record the parameter list in the function declaration. */ 6479 DECL_ARGUMENTS (fndecl) = arg_info->parms; 6480 6481 /* Now make all the ancillary declarations visible, likewise. */ 6482 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl)) 6483 { 6484 DECL_CONTEXT (decl) = current_function_decl; 6485 if (DECL_NAME (decl)) 6486 bind (DECL_NAME (decl), decl, current_scope, 6487 /*invisible=*/false, /*nested=*/false); 6488 } 6489 6490 /* And all the tag declarations. */ 6491 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl)) 6492 if (TREE_PURPOSE (decl)) 6493 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope, 6494 /*invisible=*/false, /*nested=*/false); 6495} 6496 6497/* Subroutine of store_parm_decls which handles old-style function 6498 definitions (separate parameter list and declarations). */ 6499 6500static void 6501store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info) 6502{ 6503 struct c_binding *b; 6504 tree parm, decl, last; 6505 tree parmids = arg_info->parms; 6506 struct pointer_set_t *seen_args = pointer_set_create (); 6507 6508 if (!in_system_header) 6509 warning (OPT_Wold_style_definition, "%Jold-style function definition", 6510 fndecl); 6511 6512 /* Match each formal parameter name with its declaration. Save each 6513 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */ 6514 for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 6515 { 6516 if (TREE_VALUE (parm) == 0) 6517 { 6518 error ("%Jparameter name missing from parameter list", fndecl); 6519 TREE_PURPOSE (parm) = 0; 6520 continue; 6521 } 6522 6523 b = I_SYMBOL_BINDING (TREE_VALUE (parm)); 6524 if (b && B_IN_CURRENT_SCOPE (b)) 6525 { 6526 decl = b->decl; 6527 /* If we got something other than a PARM_DECL it is an error. */ 6528 if (TREE_CODE (decl) != PARM_DECL) 6529 error ("%q+D declared as a non-parameter", decl); 6530 /* If the declaration is already marked, we have a duplicate 6531 name. Complain and ignore the duplicate. */ 6532 else if (pointer_set_contains (seen_args, decl)) 6533 { 6534 error ("multiple parameters named %q+D", decl); 6535 TREE_PURPOSE (parm) = 0; 6536 continue; 6537 } 6538 /* If the declaration says "void", complain and turn it into 6539 an int. */ 6540 else if (VOID_TYPE_P (TREE_TYPE (decl))) 6541 { 6542 error ("parameter %q+D declared with void type", decl); 6543 TREE_TYPE (decl) = integer_type_node; 6544 DECL_ARG_TYPE (decl) = integer_type_node; 6545 layout_decl (decl, 0); 6546 } 6547 warn_if_shadowing (decl); 6548 } 6549 /* If no declaration found, default to int. */ 6550 else 6551 { 6552 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node); 6553 DECL_ARG_TYPE (decl) = TREE_TYPE (decl); 6554 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl); 6555 pushdecl (decl); 6556 warn_if_shadowing (decl); 6557 6558 if (flag_isoc99) 6559 pedwarn ("type of %q+D defaults to %<int%>", decl); 6560 else if (extra_warnings) 6561 warning (OPT_Wextra, "type of %q+D defaults to %<int%>", decl); 6562 } 6563 6564 TREE_PURPOSE (parm) = decl; 6565 pointer_set_insert (seen_args, decl); 6566 } 6567 6568 /* Now examine the parms chain for incomplete declarations 6569 and declarations with no corresponding names. */ 6570 6571 for (b = current_scope->bindings; b; b = b->prev) 6572 { 6573 parm = b->decl; 6574 if (TREE_CODE (parm) != PARM_DECL) 6575 continue; 6576 6577 if (TREE_TYPE (parm) != error_mark_node 6578 && !COMPLETE_TYPE_P (TREE_TYPE (parm))) 6579 { 6580 error ("parameter %q+D has incomplete type", parm); 6581 TREE_TYPE (parm) = error_mark_node; 6582 } 6583 6584 if (!pointer_set_contains (seen_args, parm)) 6585 { 6586 error ("declaration for parameter %q+D but no such parameter", parm); 6587 6588 /* Pretend the parameter was not missing. 6589 This gets us to a standard state and minimizes 6590 further error messages. */ 6591 parmids = chainon (parmids, tree_cons (parm, 0, 0)); 6592 } 6593 } 6594 6595 /* Chain the declarations together in the order of the list of 6596 names. Store that chain in the function decl, replacing the 6597 list of names. Update the current scope to match. */ 6598 DECL_ARGUMENTS (fndecl) = 0; 6599 6600 for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 6601 if (TREE_PURPOSE (parm)) 6602 break; 6603 if (parm && TREE_PURPOSE (parm)) 6604 { 6605 last = TREE_PURPOSE (parm); 6606 DECL_ARGUMENTS (fndecl) = last; 6607 6608 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm)) 6609 if (TREE_PURPOSE (parm)) 6610 { 6611 TREE_CHAIN (last) = TREE_PURPOSE (parm); 6612 last = TREE_PURPOSE (parm); 6613 } 6614 TREE_CHAIN (last) = 0; 6615 } 6616 6617 pointer_set_destroy (seen_args); 6618 6619 /* If there was a previous prototype, 6620 set the DECL_ARG_TYPE of each argument according to 6621 the type previously specified, and report any mismatches. */ 6622 6623 if (current_function_prototype_arg_types) 6624 { 6625 tree type; 6626 for (parm = DECL_ARGUMENTS (fndecl), 6627 type = current_function_prototype_arg_types; 6628 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) 6629 != void_type_node)); 6630 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type)) 6631 { 6632 if (parm == 0 || type == 0 6633 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node) 6634 { 6635 if (current_function_prototype_built_in) 6636 warning (0, "number of arguments doesn%'t match " 6637 "built-in prototype"); 6638 else 6639 { 6640 error ("number of arguments doesn%'t match prototype"); 6641 error ("%Hprototype declaration", 6642 ¤t_function_prototype_locus); 6643 } 6644 break; 6645 } 6646 /* Type for passing arg must be consistent with that 6647 declared for the arg. ISO C says we take the unqualified 6648 type for parameters declared with qualified type. */ 6649 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)), 6650 TYPE_MAIN_VARIANT (TREE_VALUE (type)))) 6651 { 6652 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) 6653 == TYPE_MAIN_VARIANT (TREE_VALUE (type))) 6654 { 6655 /* Adjust argument to match prototype. E.g. a previous 6656 `int foo(float);' prototype causes 6657 `int foo(x) float x; {...}' to be treated like 6658 `int foo(float x) {...}'. This is particularly 6659 useful for argument types like uid_t. */ 6660 DECL_ARG_TYPE (parm) = TREE_TYPE (parm); 6661 6662 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl)) 6663 && INTEGRAL_TYPE_P (TREE_TYPE (parm)) 6664 && TYPE_PRECISION (TREE_TYPE (parm)) 6665 < TYPE_PRECISION (integer_type_node)) 6666 DECL_ARG_TYPE (parm) = integer_type_node; 6667 6668 if (pedantic) 6669 { 6670 /* ??? Is it possible to get here with a 6671 built-in prototype or will it always have 6672 been diagnosed as conflicting with an 6673 old-style definition and discarded? */ 6674 if (current_function_prototype_built_in) 6675 warning (0, "promoted argument %qD " 6676 "doesn%'t match built-in prototype", parm); 6677 else 6678 { 6679 pedwarn ("promoted argument %qD " 6680 "doesn%'t match prototype", parm); 6681 pedwarn ("%Hprototype declaration", 6682 ¤t_function_prototype_locus); 6683 } 6684 } 6685 } 6686 else 6687 { 6688 if (current_function_prototype_built_in) 6689 warning (0, "argument %qD doesn%'t match " 6690 "built-in prototype", parm); 6691 else 6692 { 6693 error ("argument %qD doesn%'t match prototype", parm); 6694 error ("%Hprototype declaration", 6695 ¤t_function_prototype_locus); 6696 } 6697 } 6698 } 6699 } 6700 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0; 6701 } 6702 6703 /* Otherwise, create a prototype that would match. */ 6704 6705 else 6706 { 6707 tree actual = 0, last = 0, type; 6708 6709 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm)) 6710 { 6711 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE); 6712 if (last) 6713 TREE_CHAIN (last) = type; 6714 else 6715 actual = type; 6716 last = type; 6717 } 6718 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE); 6719 if (last) 6720 TREE_CHAIN (last) = type; 6721 else 6722 actual = type; 6723 6724 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES 6725 of the type of this function, but we need to avoid having this 6726 affect the types of other similarly-typed functions, so we must 6727 first force the generation of an identical (but separate) type 6728 node for the relevant function type. The new node we create 6729 will be a variant of the main variant of the original function 6730 type. */ 6731 6732 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl)); 6733 6734 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual; 6735 } 6736} 6737 6738/* Store parameter declarations passed in ARG_INFO into the current 6739 function declaration. */ 6740 6741void 6742store_parm_decls_from (struct c_arg_info *arg_info) 6743{ 6744 current_function_arg_info = arg_info; 6745 store_parm_decls (); 6746} 6747 6748/* Store the parameter declarations into the current function declaration. 6749 This is called after parsing the parameter declarations, before 6750 digesting the body of the function. 6751 6752 For an old-style definition, construct a prototype out of the old-style 6753 parameter declarations and inject it into the function's type. */ 6754 6755void 6756store_parm_decls (void) 6757{ 6758 tree fndecl = current_function_decl; 6759 bool proto; 6760 6761 /* The argument information block for FNDECL. */ 6762 struct c_arg_info *arg_info = current_function_arg_info; 6763 current_function_arg_info = 0; 6764 6765 /* True if this definition is written with a prototype. Note: 6766 despite C99 6.7.5.3p14, we can *not* treat an empty argument 6767 list in a function definition as equivalent to (void) -- an 6768 empty argument list specifies the function has no parameters, 6769 but only (void) sets up a prototype for future calls. */ 6770 proto = arg_info->types != 0; 6771 6772 if (proto) 6773 store_parm_decls_newstyle (fndecl, arg_info); 6774 else 6775 store_parm_decls_oldstyle (fndecl, arg_info); 6776 6777 /* The next call to push_scope will be a function body. */ 6778 6779 next_is_function_body = true; 6780 6781 /* Write a record describing this function definition to the prototypes 6782 file (if requested). */ 6783 6784 gen_aux_info_record (fndecl, 1, 0, proto); 6785 6786 /* Initialize the RTL code for the function. */ 6787 allocate_struct_function (fndecl); 6788 6789 /* Begin the statement tree for this function. */ 6790 DECL_SAVED_TREE (fndecl) = push_stmt_list (); 6791 6792 /* ??? Insert the contents of the pending sizes list into the function 6793 to be evaluated. The only reason left to have this is 6794 void foo(int n, int array[n++]) 6795 because we throw away the array type in favor of a pointer type, and 6796 thus won't naturally see the SAVE_EXPR containing the increment. All 6797 other pending sizes would be handled by gimplify_parameters. */ 6798 { 6799 tree t; 6800 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t)) 6801 add_stmt (TREE_VALUE (t)); 6802 } 6803 6804 /* Even though we're inside a function body, we still don't want to 6805 call expand_expr to calculate the size of a variable-sized array. 6806 We haven't necessarily assigned RTL to all variables yet, so it's 6807 not safe to try to expand expressions involving them. */ 6808 cfun->x_dont_save_pending_sizes_p = 1; 6809} 6810 6811/* Emit diagnostics that require gimple input for detection. Operate on 6812 FNDECL and all its nested functions. */ 6813 6814static void 6815c_gimple_diagnostics_recursively (tree fndecl) 6816{ 6817 struct cgraph_node *cgn; 6818 6819 /* Handle attribute((warn_unused_result)). Relies on gimple input. */ 6820 c_warn_unused_result (&DECL_SAVED_TREE (fndecl)); 6821 6822 /* Notice when OpenMP structured block constraints are violated. */ 6823 if (flag_openmp) 6824 diagnose_omp_structured_block_errors (fndecl); 6825 6826 /* Finalize all nested functions now. */ 6827 cgn = cgraph_node (fndecl); 6828 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested) 6829 c_gimple_diagnostics_recursively (cgn->decl); 6830} 6831 6832/* Finish up a function declaration and compile that function 6833 all the way to assembler language output. The free the storage 6834 for the function definition. 6835 6836 This is called after parsing the body of the function definition. */ 6837 6838void 6839finish_function (void) 6840{ 6841 tree fndecl = current_function_decl; 6842 6843 label_context_stack_se = label_context_stack_se->next; 6844 label_context_stack_vm = label_context_stack_vm->next; 6845 6846 if (TREE_CODE (fndecl) == FUNCTION_DECL 6847 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))) 6848 { 6849 tree args = DECL_ARGUMENTS (fndecl); 6850 for (; args; args = TREE_CHAIN (args)) 6851 { 6852 tree type = TREE_TYPE (args); 6853 if (INTEGRAL_TYPE_P (type) 6854 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 6855 DECL_ARG_TYPE (args) = integer_type_node; 6856 } 6857 } 6858 6859 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) 6860 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; 6861 6862 /* Must mark the RESULT_DECL as being in this function. */ 6863 6864 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node) 6865 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; 6866 6867 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted) 6868 { 6869 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))) 6870 != integer_type_node) 6871 { 6872 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned. 6873 If warn_main is -1 (-Wno-main) we don't want to be warned. */ 6874 if (!warn_main) 6875 pedwarn ("return type of %q+D is not %<int%>", fndecl); 6876 } 6877 else 6878 { 6879 if (flag_isoc99) 6880 { 6881 tree stmt = c_finish_return (integer_zero_node); 6882#ifdef USE_MAPPED_LOCATION 6883 /* Hack. We don't want the middle-end to warn that this return 6884 is unreachable, so we mark its location as special. Using 6885 UNKNOWN_LOCATION has the problem that it gets clobbered in 6886 annotate_one_with_locus. A cleaner solution might be to 6887 ensure ! should_carry_locus_p (stmt), but that needs a flag. 6888 */ 6889 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION); 6890#else 6891 /* Hack. We don't want the middle-end to warn that this 6892 return is unreachable, so put the statement on the 6893 special line 0. */ 6894 annotate_with_file_line (stmt, input_filename, 0); 6895#endif 6896 } 6897 } 6898 } 6899 6900 /* Tie off the statement tree for this function. */ 6901 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl)); 6902 6903 finish_fname_decls (); 6904 6905 /* Complain if there's just no return statement. */ 6906 if (warn_return_type 6907 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE 6908 && !current_function_returns_value && !current_function_returns_null 6909 /* Don't complain if we are no-return. */ 6910 && !current_function_returns_abnormally 6911 /* Don't warn for main(). */ 6912 && !MAIN_NAME_P (DECL_NAME (fndecl)) 6913 /* Or if they didn't actually specify a return type. */ 6914 && !C_FUNCTION_IMPLICIT_INT (fndecl) 6915 /* Normally, with -Wreturn-type, flow will complain. Unless we're an 6916 inline function, as we might never be compiled separately. */ 6917 && DECL_INLINE (fndecl)) 6918 { 6919 warning (OPT_Wreturn_type, 6920 "no return statement in function returning non-void"); 6921 TREE_NO_WARNING (fndecl) = 1; 6922 } 6923 6924 /* With just -Wextra, complain only if function returns both with 6925 and without a value. */ 6926 if (extra_warnings 6927 && current_function_returns_value 6928 && current_function_returns_null) 6929 warning (OPT_Wextra, "this function may return with or without a value"); 6930 6931 /* Store the end of the function, so that we get good line number 6932 info for the epilogue. */ 6933 cfun->function_end_locus = input_location; 6934 6935 /* If we don't have ctors/dtors sections, and this is a static 6936 constructor or destructor, it must be recorded now. */ 6937 if (DECL_STATIC_CONSTRUCTOR (fndecl) 6938 && !targetm.have_ctors_dtors) 6939 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors); 6940 if (DECL_STATIC_DESTRUCTOR (fndecl) 6941 && !targetm.have_ctors_dtors) 6942 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors); 6943 6944 /* Finalize the ELF visibility for the function. */ 6945 c_determine_visibility (fndecl); 6946 6947 /* Genericize before inlining. Delay genericizing nested functions 6948 until their parent function is genericized. Since finalizing 6949 requires GENERIC, delay that as well. */ 6950 6951 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node 6952 && !undef_nested_function) 6953 { 6954 if (!decl_function_context (fndecl)) 6955 { 6956 c_genericize (fndecl); 6957 c_gimple_diagnostics_recursively (fndecl); 6958 6959 /* ??? Objc emits functions after finalizing the compilation unit. 6960 This should be cleaned up later and this conditional removed. */ 6961 if (cgraph_global_info_ready) 6962 { 6963 c_expand_body (fndecl); 6964 return; 6965 } 6966 6967 cgraph_finalize_function (fndecl, false); 6968 } 6969 else 6970 { 6971 /* Register this function with cgraph just far enough to get it 6972 added to our parent's nested function list. Handy, since the 6973 C front end doesn't have such a list. */ 6974 (void) cgraph_node (fndecl); 6975 } 6976 } 6977 6978 if (!decl_function_context (fndecl)) 6979 undef_nested_function = false; 6980 6981 /* We're leaving the context of this function, so zap cfun. 6982 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in 6983 tree_rest_of_compilation. */ 6984 cfun = NULL; 6985 current_function_decl = NULL; 6986} 6987 6988/* Generate the RTL for the body of FNDECL. */ 6989 6990void 6991c_expand_body (tree fndecl) 6992{ 6993 6994 if (!DECL_INITIAL (fndecl) 6995 || DECL_INITIAL (fndecl) == error_mark_node) 6996 return; 6997 6998 tree_rest_of_compilation (fndecl); 6999 7000 if (DECL_STATIC_CONSTRUCTOR (fndecl) 7001 && targetm.have_ctors_dtors) 7002 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0), 7003 DEFAULT_INIT_PRIORITY); 7004 if (DECL_STATIC_DESTRUCTOR (fndecl) 7005 && targetm.have_ctors_dtors) 7006 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0), 7007 DEFAULT_INIT_PRIORITY); 7008} 7009 7010/* Check the declarations given in a for-loop for satisfying the C99 7011 constraints. If exactly one such decl is found, return it. */ 7012 7013tree 7014check_for_loop_decls (void) 7015{ 7016 struct c_binding *b; 7017 tree one_decl = NULL_TREE; 7018 int n_decls = 0; 7019 7020 7021 if (!flag_isoc99) 7022 { 7023 /* If we get here, declarations have been used in a for loop without 7024 the C99 for loop scope. This doesn't make much sense, so don't 7025 allow it. */ 7026 error ("%<for%> loop initial declaration used outside C99 mode"); 7027 return NULL_TREE; 7028 } 7029 /* C99 subclause 6.8.5 paragraph 3: 7030 7031 [#3] The declaration part of a for statement shall only 7032 declare identifiers for objects having storage class auto or 7033 register. 7034 7035 It isn't clear whether, in this sentence, "identifiers" binds to 7036 "shall only declare" or to "objects" - that is, whether all identifiers 7037 declared must be identifiers for objects, or whether the restriction 7038 only applies to those that are. (A question on this in comp.std.c 7039 in November 2000 received no answer.) We implement the strictest 7040 interpretation, to avoid creating an extension which later causes 7041 problems. */ 7042 7043 for (b = current_scope->bindings; b; b = b->prev) 7044 { 7045 tree id = b->id; 7046 tree decl = b->decl; 7047 7048 if (!id) 7049 continue; 7050 7051 switch (TREE_CODE (decl)) 7052 { 7053 case VAR_DECL: 7054 if (TREE_STATIC (decl)) 7055 error ("declaration of static variable %q+D in %<for%> loop " 7056 "initial declaration", decl); 7057 else if (DECL_EXTERNAL (decl)) 7058 error ("declaration of %<extern%> variable %q+D in %<for%> loop " 7059 "initial declaration", decl); 7060 break; 7061 7062 case RECORD_TYPE: 7063 error ("%<struct %E%> declared in %<for%> loop initial declaration", 7064 id); 7065 break; 7066 case UNION_TYPE: 7067 error ("%<union %E%> declared in %<for%> loop initial declaration", 7068 id); 7069 break; 7070 case ENUMERAL_TYPE: 7071 error ("%<enum %E%> declared in %<for%> loop initial declaration", 7072 id); 7073 break; 7074 default: 7075 error ("declaration of non-variable %q+D in %<for%> loop " 7076 "initial declaration", decl); 7077 } 7078 7079 n_decls++; 7080 one_decl = decl; 7081 } 7082 7083 return n_decls == 1 ? one_decl : NULL_TREE; 7084} 7085 7086/* Save and reinitialize the variables 7087 used during compilation of a C function. */ 7088 7089void 7090c_push_function_context (struct function *f) 7091{ 7092 struct language_function *p; 7093 p = GGC_NEW (struct language_function); 7094 f->language = p; 7095 7096 p->base.x_stmt_tree = c_stmt_tree; 7097 p->x_break_label = c_break_label; 7098 p->x_cont_label = c_cont_label; 7099 p->x_switch_stack = c_switch_stack; 7100 p->arg_info = current_function_arg_info; 7101 p->returns_value = current_function_returns_value; 7102 p->returns_null = current_function_returns_null; 7103 p->returns_abnormally = current_function_returns_abnormally; 7104 p->warn_about_return_type = warn_about_return_type; 7105} 7106 7107/* Restore the variables used during compilation of a C function. */ 7108 7109void 7110c_pop_function_context (struct function *f) 7111{ 7112 struct language_function *p = f->language; 7113 7114 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0 7115 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE) 7116 { 7117 /* Stop pointing to the local nodes about to be freed. */ 7118 /* But DECL_INITIAL must remain nonzero so we know this 7119 was an actual function definition. */ 7120 DECL_INITIAL (current_function_decl) = error_mark_node; 7121 DECL_ARGUMENTS (current_function_decl) = 0; 7122 } 7123 7124 c_stmt_tree = p->base.x_stmt_tree; 7125 c_break_label = p->x_break_label; 7126 c_cont_label = p->x_cont_label; 7127 c_switch_stack = p->x_switch_stack; 7128 current_function_arg_info = p->arg_info; 7129 current_function_returns_value = p->returns_value; 7130 current_function_returns_null = p->returns_null; 7131 current_function_returns_abnormally = p->returns_abnormally; 7132 warn_about_return_type = p->warn_about_return_type; 7133 7134 f->language = NULL; 7135} 7136 7137/* Copy the DECL_LANG_SPECIFIC data associated with DECL. */ 7138 7139void 7140c_dup_lang_specific_decl (tree decl) 7141{ 7142 struct lang_decl *ld; 7143 7144 if (!DECL_LANG_SPECIFIC (decl)) 7145 return; 7146 7147 ld = GGC_NEW (struct lang_decl); 7148 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl)); 7149 DECL_LANG_SPECIFIC (decl) = ld; 7150} 7151 7152/* The functions below are required for functionality of doing 7153 function at once processing in the C front end. Currently these 7154 functions are not called from anywhere in the C front end, but as 7155 these changes continue, that will change. */ 7156 7157/* Returns the stmt_tree (if any) to which statements are currently 7158 being added. If there is no active statement-tree, NULL is 7159 returned. */ 7160 7161stmt_tree 7162current_stmt_tree (void) 7163{ 7164 return &c_stmt_tree; 7165} 7166 7167/* Nonzero if TYPE is an anonymous union or struct type. Always 0 in 7168 C. */ 7169 7170int 7171anon_aggr_type_p (tree ARG_UNUSED (node)) 7172{ 7173 return 0; 7174} 7175 7176/* Return the global value of T as a symbol. */ 7177 7178tree 7179identifier_global_value (tree t) 7180{ 7181 struct c_binding *b; 7182 7183 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed) 7184 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b)) 7185 return b->decl; 7186 7187 return 0; 7188} 7189 7190/* Record a builtin type for C. If NAME is non-NULL, it is the name used; 7191 otherwise the name is found in ridpointers from RID_INDEX. */ 7192 7193void 7194record_builtin_type (enum rid rid_index, const char *name, tree type) 7195{ 7196 tree id, decl; 7197 if (name == 0) 7198 id = ridpointers[(int) rid_index]; 7199 else 7200 id = get_identifier (name); 7201 decl = build_decl (TYPE_DECL, id, type); 7202 pushdecl (decl); 7203 if (debug_hooks->type_decl) 7204 debug_hooks->type_decl (decl, false); 7205} 7206 7207/* Build the void_list_node (void_type_node having been created). */ 7208tree 7209build_void_list_node (void) 7210{ 7211 tree t = build_tree_list (NULL_TREE, void_type_node); 7212 return t; 7213} 7214 7215/* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */ 7216 7217struct c_parm * 7218build_c_parm (struct c_declspecs *specs, tree attrs, 7219 struct c_declarator *declarator) 7220{ 7221 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm); 7222 ret->specs = specs; 7223 ret->attrs = attrs; 7224 ret->declarator = declarator; 7225 return ret; 7226} 7227 7228/* Return a declarator with nested attributes. TARGET is the inner 7229 declarator to which these attributes apply. ATTRS are the 7230 attributes. */ 7231 7232struct c_declarator * 7233build_attrs_declarator (tree attrs, struct c_declarator *target) 7234{ 7235 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7236 ret->kind = cdk_attrs; 7237 ret->declarator = target; 7238 ret->u.attrs = attrs; 7239 return ret; 7240} 7241 7242/* Return a declarator for a function with arguments specified by ARGS 7243 and return type specified by TARGET. */ 7244 7245struct c_declarator * 7246build_function_declarator (struct c_arg_info *args, 7247 struct c_declarator *target) 7248{ 7249 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7250 ret->kind = cdk_function; 7251 ret->declarator = target; 7252 ret->u.arg_info = args; 7253 return ret; 7254} 7255 7256/* Return a declarator for the identifier IDENT (which may be 7257 NULL_TREE for an abstract declarator). */ 7258 7259struct c_declarator * 7260build_id_declarator (tree ident) 7261{ 7262 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7263 ret->kind = cdk_id; 7264 ret->declarator = 0; 7265 ret->u.id = ident; 7266 /* Default value - may get reset to a more precise location. */ 7267 ret->id_loc = input_location; 7268 return ret; 7269} 7270 7271/* Return something to represent absolute declarators containing a *. 7272 TARGET is the absolute declarator that the * contains. 7273 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes 7274 to apply to the pointer type. */ 7275 7276struct c_declarator * 7277make_pointer_declarator (struct c_declspecs *type_quals_attrs, 7278 struct c_declarator *target) 7279{ 7280 tree attrs; 7281 int quals = 0; 7282 struct c_declarator *itarget = target; 7283 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7284 if (type_quals_attrs) 7285 { 7286 attrs = type_quals_attrs->attrs; 7287 quals = quals_from_declspecs (type_quals_attrs); 7288 if (attrs != NULL_TREE) 7289 itarget = build_attrs_declarator (attrs, target); 7290 } 7291 ret->kind = cdk_pointer; 7292 ret->declarator = itarget; 7293 ret->u.pointer_quals = quals; 7294 return ret; 7295} 7296 7297/* Return a pointer to a structure for an empty list of declaration 7298 specifiers. */ 7299 7300struct c_declspecs * 7301build_null_declspecs (void) 7302{ 7303 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs); 7304 ret->type = 0; 7305 ret->decl_attr = 0; 7306 ret->attrs = 0; 7307 ret->typespec_word = cts_none; 7308 ret->storage_class = csc_none; 7309 ret->declspecs_seen_p = false; 7310 ret->type_seen_p = false; 7311 ret->non_sc_seen_p = false; 7312 ret->typedef_p = false; 7313 ret->tag_defined_p = false; 7314 ret->explicit_signed_p = false; 7315 ret->deprecated_p = false; 7316 /* APPLE LOCAL "unavailable" attribute (radar 2809697) */ 7317 ret->unavailable_p = false; 7318 ret->default_int_p = false; 7319 ret->long_p = false; 7320 ret->long_long_p = false; 7321 ret->short_p = false; 7322 ret->signed_p = false; 7323 ret->unsigned_p = false; 7324 ret->complex_p = false; 7325 ret->inline_p = false; 7326 ret->thread_p = false; 7327 ret->const_p = false; 7328 ret->volatile_p = false; 7329 ret->restrict_p = false; 7330 return ret; 7331} 7332 7333/* Add the type qualifier QUAL to the declaration specifiers SPECS, 7334 returning SPECS. */ 7335 7336struct c_declspecs * 7337declspecs_add_qual (struct c_declspecs *specs, tree qual) 7338{ 7339 enum rid i; 7340 bool dupe = false; 7341 specs->non_sc_seen_p = true; 7342 specs->declspecs_seen_p = true; 7343 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE 7344 && C_IS_RESERVED_WORD (qual)); 7345 i = C_RID_CODE (qual); 7346 switch (i) 7347 { 7348 case RID_CONST: 7349 dupe = specs->const_p; 7350 specs->const_p = true; 7351 break; 7352 case RID_VOLATILE: 7353 dupe = specs->volatile_p; 7354 specs->volatile_p = true; 7355 break; 7356 case RID_RESTRICT: 7357 dupe = specs->restrict_p; 7358 specs->restrict_p = true; 7359 break; 7360 default: 7361 gcc_unreachable (); 7362 } 7363 if (dupe && pedantic && !flag_isoc99) 7364 pedwarn ("duplicate %qE", qual); 7365 return specs; 7366} 7367 7368/* Add the type specifier TYPE to the declaration specifiers SPECS, 7369 returning SPECS. */ 7370 7371struct c_declspecs * 7372declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec) 7373{ 7374 tree type = spec.spec; 7375 specs->non_sc_seen_p = true; 7376 specs->declspecs_seen_p = true; 7377 specs->type_seen_p = true; 7378 if (TREE_DEPRECATED (type)) 7379 specs->deprecated_p = true; 7380 7381 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */ 7382 if (TREE_UNAVAILABLE (type)) 7383 specs->unavailable_p = true; 7384 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */ 7385 7386 /* Handle type specifier keywords. */ 7387 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type)) 7388 { 7389 enum rid i = C_RID_CODE (type); 7390 if (specs->type) 7391 { 7392 error ("two or more data types in declaration specifiers"); 7393 return specs; 7394 } 7395 if ((int) i <= (int) RID_LAST_MODIFIER) 7396 { 7397 /* "long", "short", "signed", "unsigned" or "_Complex". */ 7398 bool dupe = false; 7399 switch (i) 7400 { 7401 case RID_LONG: 7402 if (specs->long_long_p) 7403 { 7404 error ("%<long long long%> is too long for GCC"); 7405 break; 7406 } 7407 if (specs->long_p) 7408 { 7409 if (specs->typespec_word == cts_double) 7410 { 7411 error ("both %<long long%> and %<double%> in " 7412 "declaration specifiers"); 7413 break; 7414 } 7415 if (pedantic && !flag_isoc99 && !in_system_header 7416 && warn_long_long) 7417 pedwarn ("ISO C90 does not support %<long long%>"); 7418 specs->long_long_p = 1; 7419 break; 7420 } 7421 if (specs->short_p) 7422 error ("both %<long%> and %<short%> in " 7423 "declaration specifiers"); 7424 else if (specs->typespec_word == cts_void) 7425 error ("both %<long%> and %<void%> in " 7426 "declaration specifiers"); 7427 else if (specs->typespec_word == cts_bool) 7428 error ("both %<long%> and %<_Bool%> in " 7429 "declaration specifiers"); 7430 else if (specs->typespec_word == cts_char) 7431 error ("both %<long%> and %<char%> in " 7432 "declaration specifiers"); 7433 else if (specs->typespec_word == cts_float) 7434 error ("both %<long%> and %<float%> in " 7435 "declaration specifiers"); 7436 else if (specs->typespec_word == cts_dfloat32) 7437 error ("both %<long%> and %<_Decimal32%> in " 7438 "declaration specifiers"); 7439 else if (specs->typespec_word == cts_dfloat64) 7440 error ("both %<long%> and %<_Decimal64%> in " 7441 "declaration specifiers"); 7442 else if (specs->typespec_word == cts_dfloat128) 7443 error ("both %<long%> and %<_Decimal128%> in " 7444 "declaration specifiers"); 7445 else 7446 specs->long_p = true; 7447 break; 7448 case RID_SHORT: 7449 dupe = specs->short_p; 7450 if (specs->long_p) 7451 error ("both %<long%> and %<short%> in " 7452 "declaration specifiers"); 7453 else if (specs->typespec_word == cts_void) 7454 error ("both %<short%> and %<void%> in " 7455 "declaration specifiers"); 7456 else if (specs->typespec_word == cts_bool) 7457 error ("both %<short%> and %<_Bool%> in " 7458 "declaration specifiers"); 7459 else if (specs->typespec_word == cts_char) 7460 error ("both %<short%> and %<char%> in " 7461 "declaration specifiers"); 7462 else if (specs->typespec_word == cts_float) 7463 error ("both %<short%> and %<float%> in " 7464 "declaration specifiers"); 7465 else if (specs->typespec_word == cts_double) 7466 error ("both %<short%> and %<double%> in " 7467 "declaration specifiers"); 7468 else if (specs->typespec_word == cts_dfloat32) 7469 error ("both %<short%> and %<_Decimal32%> in " 7470 "declaration specifiers"); 7471 else if (specs->typespec_word == cts_dfloat64) 7472 error ("both %<short%> and %<_Decimal64%> in " 7473 "declaration specifiers"); 7474 else if (specs->typespec_word == cts_dfloat128) 7475 error ("both %<short%> and %<_Decimal128%> in " 7476 "declaration specifiers"); 7477 else 7478 specs->short_p = true; 7479 break; 7480 case RID_SIGNED: 7481 dupe = specs->signed_p; 7482 if (specs->unsigned_p) 7483 error ("both %<signed%> and %<unsigned%> in " 7484 "declaration specifiers"); 7485 else if (specs->typespec_word == cts_void) 7486 error ("both %<signed%> and %<void%> in " 7487 "declaration specifiers"); 7488 else if (specs->typespec_word == cts_bool) 7489 error ("both %<signed%> and %<_Bool%> in " 7490 "declaration specifiers"); 7491 else if (specs->typespec_word == cts_float) 7492 error ("both %<signed%> and %<float%> in " 7493 "declaration specifiers"); 7494 else if (specs->typespec_word == cts_double) 7495 error ("both %<signed%> and %<double%> in " 7496 "declaration specifiers"); 7497 else if (specs->typespec_word == cts_dfloat32) 7498 error ("both %<signed%> and %<_Decimal32%> in " 7499 "declaration specifiers"); 7500 else if (specs->typespec_word == cts_dfloat64) 7501 error ("both %<signed%> and %<_Decimal64%> in " 7502 "declaration specifiers"); 7503 else if (specs->typespec_word == cts_dfloat128) 7504 error ("both %<signed%> and %<_Decimal128%> in " 7505 "declaration specifiers"); 7506 else 7507 specs->signed_p = true; 7508 break; 7509 case RID_UNSIGNED: 7510 dupe = specs->unsigned_p; 7511 if (specs->signed_p) 7512 error ("both %<signed%> and %<unsigned%> in " 7513 "declaration specifiers"); 7514 else if (specs->typespec_word == cts_void) 7515 error ("both %<unsigned%> and %<void%> in " 7516 "declaration specifiers"); 7517 else if (specs->typespec_word == cts_bool) 7518 error ("both %<unsigned%> and %<_Bool%> in " 7519 "declaration specifiers"); 7520 else if (specs->typespec_word == cts_float) 7521 error ("both %<unsigned%> and %<float%> in " 7522 "declaration specifiers"); 7523 else if (specs->typespec_word == cts_double) 7524 error ("both %<unsigned%> and %<double%> in " 7525 "declaration specifiers"); 7526 else if (specs->typespec_word == cts_dfloat32) 7527 error ("both %<unsigned%> and %<_Decimal32%> in " 7528 "declaration specifiers"); 7529 else if (specs->typespec_word == cts_dfloat64) 7530 error ("both %<unsigned%> and %<_Decimal64%> in " 7531 "declaration specifiers"); 7532 else if (specs->typespec_word == cts_dfloat128) 7533 error ("both %<unsigned%> and %<_Decimal128%> in " 7534 "declaration specifiers"); 7535 else 7536 specs->unsigned_p = true; 7537 break; 7538 case RID_COMPLEX: 7539 dupe = specs->complex_p; 7540 if (pedantic && !flag_isoc99 && !in_system_header) 7541 pedwarn ("ISO C90 does not support complex types"); 7542 if (specs->typespec_word == cts_void) 7543 error ("both %<complex%> and %<void%> in " 7544 "declaration specifiers"); 7545 else if (specs->typespec_word == cts_bool) 7546 error ("both %<complex%> and %<_Bool%> in " 7547 "declaration specifiers"); 7548 else if (specs->typespec_word == cts_dfloat32) 7549 error ("both %<complex%> and %<_Decimal32%> in " 7550 "declaration specifiers"); 7551 else if (specs->typespec_word == cts_dfloat64) 7552 error ("both %<complex%> and %<_Decimal64%> in " 7553 "declaration specifiers"); 7554 else if (specs->typespec_word == cts_dfloat128) 7555 error ("both %<complex%> and %<_Decimal128%> in " 7556 "declaration specifiers"); 7557 else 7558 specs->complex_p = true; 7559 break; 7560 default: 7561 gcc_unreachable (); 7562 } 7563 7564 if (dupe) 7565 error ("duplicate %qE", type); 7566 7567 return specs; 7568 } 7569 else 7570 { 7571 /* "void", "_Bool", "char", "int", "float" or "double". */ 7572 if (specs->typespec_word != cts_none) 7573 { 7574 error ("two or more data types in declaration specifiers"); 7575 return specs; 7576 } 7577 switch (i) 7578 { 7579 case RID_VOID: 7580 if (specs->long_p) 7581 error ("both %<long%> and %<void%> in " 7582 "declaration specifiers"); 7583 else if (specs->short_p) 7584 error ("both %<short%> and %<void%> in " 7585 "declaration specifiers"); 7586 else if (specs->signed_p) 7587 error ("both %<signed%> and %<void%> in " 7588 "declaration specifiers"); 7589 else if (specs->unsigned_p) 7590 error ("both %<unsigned%> and %<void%> in " 7591 "declaration specifiers"); 7592 else if (specs->complex_p) 7593 error ("both %<complex%> and %<void%> in " 7594 "declaration specifiers"); 7595 else 7596 specs->typespec_word = cts_void; 7597 return specs; 7598 case RID_BOOL: 7599 if (specs->long_p) 7600 error ("both %<long%> and %<_Bool%> in " 7601 "declaration specifiers"); 7602 else if (specs->short_p) 7603 error ("both %<short%> and %<_Bool%> in " 7604 "declaration specifiers"); 7605 else if (specs->signed_p) 7606 error ("both %<signed%> and %<_Bool%> in " 7607 "declaration specifiers"); 7608 else if (specs->unsigned_p) 7609 error ("both %<unsigned%> and %<_Bool%> in " 7610 "declaration specifiers"); 7611 else if (specs->complex_p) 7612 error ("both %<complex%> and %<_Bool%> in " 7613 "declaration specifiers"); 7614 else 7615 specs->typespec_word = cts_bool; 7616 return specs; 7617 case RID_CHAR: 7618 if (specs->long_p) 7619 error ("both %<long%> and %<char%> in " 7620 "declaration specifiers"); 7621 else if (specs->short_p) 7622 error ("both %<short%> and %<char%> in " 7623 "declaration specifiers"); 7624 else 7625 specs->typespec_word = cts_char; 7626 return specs; 7627 case RID_INT: 7628 specs->typespec_word = cts_int; 7629 return specs; 7630 case RID_FLOAT: 7631 if (specs->long_p) 7632 error ("both %<long%> and %<float%> in " 7633 "declaration specifiers"); 7634 else if (specs->short_p) 7635 error ("both %<short%> and %<float%> in " 7636 "declaration specifiers"); 7637 else if (specs->signed_p) 7638 error ("both %<signed%> and %<float%> in " 7639 "declaration specifiers"); 7640 else if (specs->unsigned_p) 7641 error ("both %<unsigned%> and %<float%> in " 7642 "declaration specifiers"); 7643 else 7644 specs->typespec_word = cts_float; 7645 return specs; 7646 case RID_DOUBLE: 7647 if (specs->long_long_p) 7648 error ("both %<long long%> and %<double%> in " 7649 "declaration specifiers"); 7650 else if (specs->short_p) 7651 error ("both %<short%> and %<double%> in " 7652 "declaration specifiers"); 7653 else if (specs->signed_p) 7654 error ("both %<signed%> and %<double%> in " 7655 "declaration specifiers"); 7656 else if (specs->unsigned_p) 7657 error ("both %<unsigned%> and %<double%> in " 7658 "declaration specifiers"); 7659 else 7660 specs->typespec_word = cts_double; 7661 return specs; 7662 case RID_DFLOAT32: 7663 case RID_DFLOAT64: 7664 case RID_DFLOAT128: 7665 { 7666 const char *str; 7667 if (i == RID_DFLOAT32) 7668 str = "_Decimal32"; 7669 else if (i == RID_DFLOAT64) 7670 str = "_Decimal64"; 7671 else 7672 str = "_Decimal128"; 7673 if (specs->long_long_p) 7674 error ("both %<long long%> and %<%s%> in " 7675 "declaration specifiers", str); 7676 if (specs->long_p) 7677 error ("both %<long%> and %<%s%> in " 7678 "declaration specifiers", str); 7679 else if (specs->short_p) 7680 error ("both %<short%> and %<%s%> in " 7681 "declaration specifiers", str); 7682 else if (specs->signed_p) 7683 error ("both %<signed%> and %<%s%> in " 7684 "declaration specifiers", str); 7685 else if (specs->unsigned_p) 7686 error ("both %<unsigned%> and %<%s%> in " 7687 "declaration specifiers", str); 7688 else if (specs->complex_p) 7689 error ("both %<complex%> and %<%s%> in " 7690 "declaration specifiers", str); 7691 else if (i == RID_DFLOAT32) 7692 specs->typespec_word = cts_dfloat32; 7693 else if (i == RID_DFLOAT64) 7694 specs->typespec_word = cts_dfloat64; 7695 else 7696 specs->typespec_word = cts_dfloat128; 7697 } 7698 if (!targetm.decimal_float_supported_p ()) 7699 error ("decimal floating point not supported for this target"); 7700 if (pedantic) 7701 pedwarn ("ISO C does not support decimal floating point"); 7702 return specs; 7703 default: 7704 /* ObjC reserved word "id", handled below. */ 7705 break; 7706 } 7707 } 7708 } 7709 7710 /* Now we have a typedef (a TYPE_DECL node), an identifier (some 7711 form of ObjC type, cases such as "int" and "long" being handled 7712 above), a TYPE (struct, union, enum and typeof specifiers) or an 7713 ERROR_MARK. In none of these cases may there have previously 7714 been any type specifiers. */ 7715 if (specs->type || specs->typespec_word != cts_none 7716 || specs->long_p || specs->short_p || specs->signed_p 7717 || specs->unsigned_p || specs->complex_p) 7718 error ("two or more data types in declaration specifiers"); 7719 else if (TREE_CODE (type) == TYPE_DECL) 7720 { 7721 if (TREE_TYPE (type) == error_mark_node) 7722 ; /* Allow the type to default to int to avoid cascading errors. */ 7723 else 7724 { 7725 specs->type = TREE_TYPE (type); 7726 specs->decl_attr = DECL_ATTRIBUTES (type); 7727 specs->typedef_p = true; 7728 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type); 7729 } 7730 } 7731 else if (TREE_CODE (type) == IDENTIFIER_NODE) 7732 { 7733 tree t = lookup_name (type); 7734 if (!t || TREE_CODE (t) != TYPE_DECL) 7735 error ("%qE fails to be a typedef or built in type", type); 7736 else if (TREE_TYPE (t) == error_mark_node) 7737 ; 7738 else 7739 specs->type = TREE_TYPE (t); 7740 } 7741 else if (TREE_CODE (type) != ERROR_MARK) 7742 { 7743 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref) 7744 specs->tag_defined_p = true; 7745 if (spec.kind == ctsk_typeof) 7746 specs->typedef_p = true; 7747 specs->type = type; 7748 } 7749 7750 return specs; 7751} 7752 7753/* Add the storage class specifier or function specifier SCSPEC to the 7754 declaration specifiers SPECS, returning SPECS. */ 7755 7756struct c_declspecs * 7757declspecs_add_scspec (struct c_declspecs *specs, tree scspec) 7758{ 7759 enum rid i; 7760 enum c_storage_class n = csc_none; 7761 bool dupe = false; 7762 specs->declspecs_seen_p = true; 7763 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE 7764 && C_IS_RESERVED_WORD (scspec)); 7765 i = C_RID_CODE (scspec); 7766 if (extra_warnings && specs->non_sc_seen_p) 7767 warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec); 7768 switch (i) 7769 { 7770 case RID_INLINE: 7771 /* C99 permits duplicate inline. Although of doubtful utility, 7772 it seems simplest to permit it in gnu89 mode as well, as 7773 there is also little utility in maintaining this as a 7774 difference between gnu89 and C99 inline. */ 7775 dupe = false; 7776 specs->inline_p = true; 7777 break; 7778 case RID_THREAD: 7779 dupe = specs->thread_p; 7780 if (specs->storage_class == csc_auto) 7781 error ("%<__thread%> used with %<auto%>"); 7782 else if (specs->storage_class == csc_register) 7783 error ("%<__thread%> used with %<register%>"); 7784 else if (specs->storage_class == csc_typedef) 7785 error ("%<__thread%> used with %<typedef%>"); 7786 else 7787 specs->thread_p = true; 7788 break; 7789 case RID_AUTO: 7790 n = csc_auto; 7791 break; 7792 case RID_EXTERN: 7793 n = csc_extern; 7794 /* Diagnose "__thread extern". */ 7795 if (specs->thread_p) 7796 error ("%<__thread%> before %<extern%>"); 7797 break; 7798 case RID_REGISTER: 7799 n = csc_register; 7800 break; 7801 case RID_STATIC: 7802 n = csc_static; 7803 /* Diagnose "__thread static". */ 7804 if (specs->thread_p) 7805 error ("%<__thread%> before %<static%>"); 7806 break; 7807 case RID_TYPEDEF: 7808 n = csc_typedef; 7809 break; 7810 default: 7811 gcc_unreachable (); 7812 } 7813 if (n != csc_none && n == specs->storage_class) 7814 dupe = true; 7815 if (dupe) 7816 error ("duplicate %qE", scspec); 7817 if (n != csc_none) 7818 { 7819 if (specs->storage_class != csc_none && n != specs->storage_class) 7820 { 7821 error ("multiple storage classes in declaration specifiers"); 7822 } 7823 else 7824 { 7825 specs->storage_class = n; 7826 if (n != csc_extern && n != csc_static && specs->thread_p) 7827 { 7828 error ("%<__thread%> used with %qE", scspec); 7829 specs->thread_p = false; 7830 } 7831 } 7832 } 7833 return specs; 7834} 7835 7836/* Add the attributes ATTRS to the declaration specifiers SPECS, 7837 returning SPECS. */ 7838 7839struct c_declspecs * 7840declspecs_add_attrs (struct c_declspecs *specs, tree attrs) 7841{ 7842 specs->attrs = chainon (attrs, specs->attrs); 7843 specs->declspecs_seen_p = true; 7844 return specs; 7845} 7846 7847/* Combine "long", "short", "signed", "unsigned" and "_Complex" type 7848 specifiers with any other type specifier to determine the resulting 7849 type. This is where ISO C checks on complex types are made, since 7850 "_Complex long" is a prefix of the valid ISO C type "_Complex long 7851 double". */ 7852 7853struct c_declspecs * 7854finish_declspecs (struct c_declspecs *specs) 7855{ 7856 /* If a type was specified as a whole, we have no modifiers and are 7857 done. */ 7858 if (specs->type != NULL_TREE) 7859 { 7860 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p 7861 && !specs->signed_p && !specs->unsigned_p 7862 && !specs->complex_p); 7863 return specs; 7864 } 7865 7866 /* If none of "void", "_Bool", "char", "int", "float" or "double" 7867 has been specified, treat it as "int" unless "_Complex" is 7868 present and there are no other specifiers. If we just have 7869 "_Complex", it is equivalent to "_Complex double", but e.g. 7870 "_Complex short" is equivalent to "_Complex short int". */ 7871 if (specs->typespec_word == cts_none) 7872 { 7873 if (specs->long_p || specs->short_p 7874 || specs->signed_p || specs->unsigned_p) 7875 { 7876 specs->typespec_word = cts_int; 7877 } 7878 else if (specs->complex_p) 7879 { 7880 specs->typespec_word = cts_double; 7881 if (pedantic) 7882 pedwarn ("ISO C does not support plain %<complex%> meaning " 7883 "%<double complex%>"); 7884 } 7885 else 7886 { 7887 specs->typespec_word = cts_int; 7888 specs->default_int_p = true; 7889 /* We don't diagnose this here because grokdeclarator will 7890 give more specific diagnostics according to whether it is 7891 a function definition. */ 7892 } 7893 } 7894 7895 /* If "signed" was specified, record this to distinguish "int" and 7896 "signed int" in the case of a bit-field with 7897 -funsigned-bitfields. */ 7898 specs->explicit_signed_p = specs->signed_p; 7899 7900 /* Now compute the actual type. */ 7901 switch (specs->typespec_word) 7902 { 7903 case cts_void: 7904 gcc_assert (!specs->long_p && !specs->short_p 7905 && !specs->signed_p && !specs->unsigned_p 7906 && !specs->complex_p); 7907 specs->type = void_type_node; 7908 break; 7909 case cts_bool: 7910 gcc_assert (!specs->long_p && !specs->short_p 7911 && !specs->signed_p && !specs->unsigned_p 7912 && !specs->complex_p); 7913 specs->type = boolean_type_node; 7914 break; 7915 case cts_char: 7916 gcc_assert (!specs->long_p && !specs->short_p); 7917 gcc_assert (!(specs->signed_p && specs->unsigned_p)); 7918 if (specs->signed_p) 7919 specs->type = signed_char_type_node; 7920 else if (specs->unsigned_p) 7921 specs->type = unsigned_char_type_node; 7922 else 7923 specs->type = char_type_node; 7924 if (specs->complex_p) 7925 { 7926 if (pedantic) 7927 pedwarn ("ISO C does not support complex integer types"); 7928 specs->type = build_complex_type (specs->type); 7929 } 7930 break; 7931 case cts_int: 7932 gcc_assert (!(specs->long_p && specs->short_p)); 7933 gcc_assert (!(specs->signed_p && specs->unsigned_p)); 7934 if (specs->long_long_p) 7935 specs->type = (specs->unsigned_p 7936 ? long_long_unsigned_type_node 7937 : long_long_integer_type_node); 7938 else if (specs->long_p) 7939 specs->type = (specs->unsigned_p 7940 ? long_unsigned_type_node 7941 : long_integer_type_node); 7942 else if (specs->short_p) 7943 specs->type = (specs->unsigned_p 7944 ? short_unsigned_type_node 7945 : short_integer_type_node); 7946 else 7947 specs->type = (specs->unsigned_p 7948 ? unsigned_type_node 7949 : integer_type_node); 7950 if (specs->complex_p) 7951 { 7952 if (pedantic) 7953 pedwarn ("ISO C does not support complex integer types"); 7954 specs->type = build_complex_type (specs->type); 7955 } 7956 break; 7957 case cts_float: 7958 gcc_assert (!specs->long_p && !specs->short_p 7959 && !specs->signed_p && !specs->unsigned_p); 7960 specs->type = (specs->complex_p 7961 ? complex_float_type_node 7962 : float_type_node); 7963 break; 7964 case cts_double: 7965 gcc_assert (!specs->long_long_p && !specs->short_p 7966 && !specs->signed_p && !specs->unsigned_p); 7967 if (specs->long_p) 7968 { 7969 specs->type = (specs->complex_p 7970 ? complex_long_double_type_node 7971 : long_double_type_node); 7972 } 7973 else 7974 { 7975 specs->type = (specs->complex_p 7976 ? complex_double_type_node 7977 : double_type_node); 7978 } 7979 break; 7980 case cts_dfloat32: 7981 case cts_dfloat64: 7982 case cts_dfloat128: 7983 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p 7984 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p); 7985 if (specs->typespec_word == cts_dfloat32) 7986 specs->type = dfloat32_type_node; 7987 else if (specs->typespec_word == cts_dfloat64) 7988 specs->type = dfloat64_type_node; 7989 else 7990 specs->type = dfloat128_type_node; 7991 break; 7992 default: 7993 gcc_unreachable (); 7994 } 7995 7996 return specs; 7997} 7998 7999/* Synthesize a function which calls all the global ctors or global 8000 dtors in this file. This is only used for targets which do not 8001 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */ 8002static void 8003build_cdtor (int method_type, tree cdtors) 8004{ 8005 tree body = 0; 8006 8007 if (!cdtors) 8008 return; 8009 8010 for (; cdtors; cdtors = TREE_CHAIN (cdtors)) 8011 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0), 8012 &body); 8013 8014 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY); 8015} 8016 8017/* A subroutine of c_write_global_declarations. Perform final processing 8018 on one file scope's declarations (or the external scope's declarations), 8019 GLOBALS. */ 8020 8021static void 8022c_write_global_declarations_1 (tree globals) 8023{ 8024 tree decl; 8025 bool reconsider; 8026 8027 /* Process the decls in the order they were written. */ 8028 for (decl = globals; decl; decl = TREE_CHAIN (decl)) 8029 { 8030 /* Check for used but undefined static functions using the C 8031 standard's definition of "used", and set TREE_NO_WARNING so 8032 that check_global_declarations doesn't repeat the check. */ 8033 if (TREE_CODE (decl) == FUNCTION_DECL 8034 && DECL_INITIAL (decl) == 0 8035 && DECL_EXTERNAL (decl) 8036 && !TREE_PUBLIC (decl) 8037 && C_DECL_USED (decl)) 8038 { 8039 pedwarn ("%q+F used but never defined", decl); 8040 TREE_NO_WARNING (decl) = 1; 8041 } 8042 8043 wrapup_global_declaration_1 (decl); 8044 } 8045 8046 do 8047 { 8048 reconsider = false; 8049 for (decl = globals; decl; decl = TREE_CHAIN (decl)) 8050 reconsider |= wrapup_global_declaration_2 (decl); 8051 } 8052 while (reconsider); 8053 8054 for (decl = globals; decl; decl = TREE_CHAIN (decl)) 8055 check_global_declaration_1 (decl); 8056} 8057 8058/* A subroutine of c_write_global_declarations Emit debug information for each 8059 of the declarations in GLOBALS. */ 8060 8061static void 8062c_write_global_declarations_2 (tree globals) 8063{ 8064 tree decl; 8065 8066 for (decl = globals; decl ; decl = TREE_CHAIN (decl)) 8067 debug_hooks->global_decl (decl); 8068} 8069 8070/* Preserve the external declarations scope across a garbage collect. */ 8071static GTY(()) tree ext_block; 8072 8073void 8074c_write_global_declarations (void) 8075{ 8076 tree t; 8077 8078 /* We don't want to do this if generating a PCH. */ 8079 if (pch_file) 8080 return; 8081 8082 /* Don't waste time on further processing if -fsyntax-only or we've 8083 encountered errors. */ 8084 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in)) 8085 return; 8086 8087 /* Close the external scope. */ 8088 ext_block = pop_scope (); 8089 external_scope = 0; 8090 gcc_assert (!current_scope); 8091 8092 if (ext_block) 8093 { 8094 tree tmp = BLOCK_VARS (ext_block); 8095 int flags; 8096 FILE * stream = dump_begin (TDI_tu, &flags); 8097 if (stream && tmp) 8098 { 8099 dump_node (tmp, flags & ~TDF_SLIM, stream); 8100 dump_end (TDI_tu, stream); 8101 } 8102 } 8103 8104 /* Process all file scopes in this compilation, and the external_scope, 8105 through wrapup_global_declarations and check_global_declarations. */ 8106 for (t = all_translation_units; t; t = TREE_CHAIN (t)) 8107 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t))); 8108 c_write_global_declarations_1 (BLOCK_VARS (ext_block)); 8109 8110 /* Generate functions to call static constructors and destructors 8111 for targets that do not support .ctors/.dtors sections. These 8112 functions have magic names which are detected by collect2. */ 8113 build_cdtor ('I', static_ctors); static_ctors = 0; 8114 build_cdtor ('D', static_dtors); static_dtors = 0; 8115 8116 /* We're done parsing; proceed to optimize and emit assembly. 8117 FIXME: shouldn't be the front end's responsibility to call this. */ 8118 cgraph_optimize (); 8119 8120 /* After cgraph has had a chance to emit everything that's going to 8121 be emitted, output debug information for globals. */ 8122 if (errorcount == 0 && sorrycount == 0) 8123 { 8124 timevar_push (TV_SYMOUT); 8125 for (t = all_translation_units; t; t = TREE_CHAIN (t)) 8126 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t))); 8127 c_write_global_declarations_2 (BLOCK_VARS (ext_block)); 8128 timevar_pop (TV_SYMOUT); 8129 } 8130 8131 ext_block = NULL; 8132} 8133 8134#include "gt-c-decl.h" 8135