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