c-decl.c revision 169699
118334Speter/* Process declarations and variables for C compiler. 290075Sobrien Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 3169699Skan 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 418334Speter 590075SobrienThis file is part of GCC. 618334Speter 790075SobrienGCC is free software; you can redistribute it and/or modify it under 890075Sobrienthe terms of the GNU General Public License as published by the Free 990075SobrienSoftware Foundation; either version 2, or (at your option) any later 1090075Sobrienversion. 1118334Speter 1290075SobrienGCC is distributed in the hope that it will be useful, but WITHOUT ANY 1390075SobrienWARRANTY; without even the implied warranty of MERCHANTABILITY or 1490075SobrienFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1590075Sobrienfor more details. 1618334Speter 1718334SpeterYou should have received a copy of the GNU General Public License 1890075Sobrienalong with GCC; see the file COPYING. If not, write to the Free 19169699SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 20169699Skan02110-1301, USA. */ 2118334Speter 2296549Sobrien/* $FreeBSD: head/contrib/gcc/c-decl.c 169699 2007-05-19 02:12:21Z kan $ */ 2396549Sobrien 2418334Speter/* Process declarations and symbol lookup for C front end. 2518334Speter Also constructs types; the standard scalar types at initialization, 2618334Speter and structure, union, array and enum types when they are declared. */ 2718334Speter 2818334Speter/* ??? not all decl nodes are given the most useful possible 2918334Speter line numbers. For example, the CONST_DECLs for enum values. */ 3018334Speter 3118334Speter#include "config.h" 3250397Sobrien#include "system.h" 33132730Skan#include "coretypes.h" 34169699Skan#include "input.h" 35132730Skan#include "tm.h" 3690075Sobrien#include "intl.h" 3718334Speter#include "tree.h" 3890075Sobrien#include "tree-inline.h" 3990075Sobrien#include "rtl.h" 4018334Speter#include "flags.h" 4190075Sobrien#include "function.h" 4218334Speter#include "output.h" 4390075Sobrien#include "expr.h" 4418334Speter#include "c-tree.h" 4550397Sobrien#include "toplev.h" 4690075Sobrien#include "ggc.h" 4790075Sobrien#include "tm_p.h" 4850397Sobrien#include "cpplib.h" 4990075Sobrien#include "target.h" 5090075Sobrien#include "debug.h" 51132730Skan#include "opts.h" 5290075Sobrien#include "timevar.h" 5390075Sobrien#include "c-common.h" 5496263Sobrien#include "c-pragma.h" 55169699Skan#include "langhooks.h" 56169699Skan#include "tree-mudflap.h" 57169699Skan#include "tree-gimple.h" 58169699Skan#include "diagnostic.h" 59169699Skan#include "tree-dump.h" 60132730Skan#include "cgraph.h" 61132730Skan#include "hashtab.h" 62117421Skan#include "libfuncs.h" 63117421Skan#include "except.h" 64132730Skan#include "langhooks-def.h" 65169699Skan#include "pointer-set.h" 6650397Sobrien 6718334Speter/* In grokdeclarator, distinguish syntactic contexts of declarators. */ 6818334Speterenum decl_context 6918334Speter{ NORMAL, /* Ordinary declaration */ 7018334Speter FUNCDEF, /* Function definition */ 7118334Speter PARM, /* Declaration of parm before function body */ 7218334Speter FIELD, /* Declaration inside struct or union */ 7318334Speter TYPENAME}; /* Typename (inside cast or sizeof) */ 7418334Speter 7518334Speter 7618334Speter/* Nonzero if we have seen an invalid cross reference 7718334Speter to a struct, union, or enum, but not yet printed the message. */ 78169699Skantree pending_invalid_xref; 7918334Speter 8018334Speter/* File and line to appear in the eventual error message. */ 81132730Skanlocation_t pending_invalid_xref_location; 8218334Speter 83169699Skan/* True means we've initialized exception handling. */ 84169699Skanbool c_eh_initialized_p; 85169699Skan 8618334Speter/* While defining an enum type, this is 1 plus the last enumerator 8718334Speter constant value. Note that will do not have to save this or `enum_overflow' 8818334Speter around nested function definition since such a definition could only 8918334Speter occur in an enum value expression and we don't use these variables in 9018334Speter that case. */ 9118334Speter 9218334Speterstatic tree enum_next_value; 9318334Speter 9418334Speter/* Nonzero means that there was overflow computing enum_next_value. */ 9518334Speter 9618334Speterstatic int enum_overflow; 9718334Speter 98169699Skan/* The file and line that the prototype came from if this is an 99169699Skan old-style definition; used for diagnostics in 100169699Skan store_parm_decls_oldstyle. */ 10118334Speter 102169699Skanstatic location_t current_function_prototype_locus; 10318334Speter 104169699Skan/* Whether this prototype was built-in. */ 10518334Speter 106169699Skanstatic bool current_function_prototype_built_in; 10718334Speter 108169699Skan/* The argument type information of this prototype. */ 109132730Skan 110169699Skanstatic tree current_function_prototype_arg_types; 111132730Skan 112169699Skan/* The argument information structure for the function currently being 113169699Skan defined. */ 11418334Speter 115169699Skanstatic struct c_arg_info *current_function_arg_info; 11618334Speter 117169699Skan/* The obstack on which parser and related data structures, which are 118169699Skan not live beyond their top-level declaration or definition, are 119169699Skan allocated. */ 120169699Skanstruct obstack parser_obstack; 121132730Skan 12290075Sobrien/* The current statement tree. */ 12390075Sobrien 124117421Skanstatic GTY(()) struct stmt_tree_s c_stmt_tree; 12590075Sobrien 126132730Skan/* State saving variables. */ 127169699Skantree c_break_label; 128169699Skantree c_cont_label; 12918334Speter 130169699Skan/* Linked list of TRANSLATION_UNIT_DECLS for the translation units 131169699Skan included in this invocation. Note that the current translation 132169699Skan unit is not included in this list. */ 13318334Speter 134169699Skanstatic GTY(()) tree all_translation_units; 13518334Speter 136169699Skan/* A list of decls to be made automatically visible in each file scope. */ 137169699Skanstatic GTY(()) tree visible_builtins; 13818334Speter 13918334Speter/* Set to 0 at beginning of a function definition, set to 1 if 14018334Speter a return statement that specifies a return value is seen. */ 14118334Speter 14218334Speterint current_function_returns_value; 14318334Speter 14418334Speter/* Set to 0 at beginning of a function definition, set to 1 if 14518334Speter a return statement with no argument is seen. */ 14618334Speter 14718334Speterint current_function_returns_null; 14818334Speter 14996263Sobrien/* Set to 0 at beginning of a function definition, set to 1 if 15096263Sobrien a call to a noreturn function is seen. */ 15196263Sobrien 15296263Sobrienint current_function_returns_abnormally; 15396263Sobrien 15418334Speter/* Set to nonzero by `grokdeclarator' for a function 15518334Speter whose return type is defaulted, if warnings for this are desired. */ 15618334Speter 15718334Speterstatic int warn_about_return_type; 15818334Speter 15918334Speter/* Nonzero when starting a function declared `extern inline'. */ 16018334Speter 16118334Speterstatic int current_extern_inline; 162169699Skan 163169699Skan/* Nonzero when the current toplevel function contains a declaration 164169699Skan of a nested function which is never defined. */ 165169699Skan 166169699Skanstatic bool undef_nested_function; 167169699Skan 168169699Skan/* True means global_bindings_p should return false even if the scope stack 169169699Skan says we are in file scope. */ 170169699Skanbool c_override_global_bindings_to_false; 171169699Skan 17218334Speter 173169699Skan/* Each c_binding structure describes one binding of an identifier to 174169699Skan a decl. All the decls in a scope - irrespective of namespace - are 175169699Skan chained together by the ->prev field, which (as the name implies) 176169699Skan runs in reverse order. All the decls in a given namespace bound to 177169699Skan a given identifier are chained by the ->shadowed field, which runs 178169699Skan from inner to outer scopes. 17918334Speter 180169699Skan The ->decl field usually points to a DECL node, but there are two 181169699Skan exceptions. In the namespace of type tags, the bound entity is a 182169699Skan RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared 183169699Skan identifier is encountered, it is bound to error_mark_node to 184169699Skan suppress further errors about that identifier in the current 185169699Skan function. 186169699Skan 187169699Skan The ->type field stores the type of the declaration in this scope; 188169699Skan if NULL, the type is the type of the ->decl field. This is only of 189169699Skan relevance for objects with external or internal linkage which may 190169699Skan be redeclared in inner scopes, forming composite types that only 191169699Skan persist for the duration of those scopes. In the external scope, 192169699Skan this stores the composite of all the types declared for this 193169699Skan object, visible or not. The ->inner_comp field (used only at file 194169699Skan scope) stores whether an incomplete array type at file scope was 195169699Skan completed at an inner scope to an array size other than 1. 196169699Skan 197169699Skan The depth field is copied from the scope structure that holds this 198169699Skan decl. It is used to preserve the proper ordering of the ->shadowed 199169699Skan field (see bind()) and also for a handful of special-case checks. 200169699Skan Finally, the invisible bit is true for a decl which should be 201169699Skan ignored for purposes of normal name lookup, and the nested bit is 202169699Skan true for a decl that's been bound a second time in an inner scope; 203169699Skan in all such cases, the binding in the outer scope will have its 204169699Skan invisible bit true. */ 205169699Skan 206169699Skanstruct c_binding GTY((chain_next ("%h.prev"))) 207169699Skan{ 208169699Skan tree decl; /* the decl bound */ 209169699Skan tree type; /* the type in this scope */ 210169699Skan tree id; /* the identifier it's bound to */ 211169699Skan struct c_binding *prev; /* the previous decl in this scope */ 212169699Skan struct c_binding *shadowed; /* the innermost decl shadowed by this one */ 213169699Skan unsigned int depth : 28; /* depth of this scope */ 214169699Skan BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */ 215169699Skan BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */ 216169699Skan BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */ 217169699Skan /* one free bit */ 218169699Skan}; 219169699Skan#define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth) 220169699Skan#define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth) 221169699Skan#define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/) 222169699Skan#define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/) 223169699Skan 224169699Skan#define I_SYMBOL_BINDING(node) \ 225169699Skan (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding) 226169699Skan#define I_SYMBOL_DECL(node) \ 227169699Skan (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0) 228169699Skan 229169699Skan#define I_TAG_BINDING(node) \ 230169699Skan (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding) 231169699Skan#define I_TAG_DECL(node) \ 232169699Skan (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0) 233169699Skan 234169699Skan#define I_LABEL_BINDING(node) \ 235169699Skan (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding) 236169699Skan#define I_LABEL_DECL(node) \ 237169699Skan (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0) 238169699Skan 239169699Skan/* Each C symbol points to three linked lists of c_binding structures. 240169699Skan These describe the values of the identifier in the three different 241169699Skan namespaces defined by the language. */ 242169699Skan 243169699Skanstruct lang_identifier GTY(()) 244169699Skan{ 245169699Skan struct c_common_identifier common_id; 246169699Skan struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */ 247169699Skan struct c_binding *tag_binding; /* struct/union/enum tags */ 248169699Skan struct c_binding *label_binding; /* labels */ 249169699Skan}; 250169699Skan 251169699Skan/* Validate c-lang.c's assumptions. */ 252169699Skanextern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate 253169699Skan[(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1]; 254169699Skan 255169699Skan/* The resulting tree type. */ 256169699Skan 257169699Skanunion lang_tree_node 258169699Skan GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"), 259169699Skan 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)"))) 260169699Skan{ 261169699Skan union tree_node GTY ((tag ("0"), 262169699Skan desc ("tree_node_structure (&%h)"))) 263169699Skan generic; 264169699Skan struct lang_identifier GTY ((tag ("1"))) identifier; 265169699Skan}; 266169699Skan 267169699Skan/* Each c_scope structure describes the complete contents of one 268169699Skan scope. Four scopes are distinguished specially: the innermost or 269169699Skan current scope, the innermost function scope, the file scope (always 270169699Skan the second to outermost) and the outermost or external scope. 271169699Skan 272132730Skan Most declarations are recorded in the current scope. 27318334Speter 274132730Skan All normal label declarations are recorded in the innermost 275132730Skan function scope, as are bindings of undeclared identifiers to 276132730Skan error_mark_node. (GCC permits nested functions as an extension, 277132730Skan hence the 'innermost' qualifier.) Explicitly declared labels 278132730Skan (using the __label__ extension) appear in the current scope. 27918334Speter 280169699Skan Being in the file scope (current_scope == file_scope) causes 281132730Skan special behavior in several places below. Also, under some 282132730Skan conditions the Objective-C front end records declarations in the 283169699Skan file scope even though that isn't the current scope. 28418334Speter 285169699Skan All declarations with external linkage are recorded in the external 286169699Skan scope, even if they aren't visible there; this models the fact that 287169699Skan such declarations are visible to the entire program, and (with a 288169699Skan bit of cleverness, see pushdecl) allows diagnosis of some violations 289169699Skan of C99 6.2.2p7 and 6.2.7p2: 29018334Speter 291169699Skan If, within the same translation unit, the same identifier appears 292169699Skan with both internal and external linkage, the behavior is 293169699Skan undefined. 29418334Speter 295169699Skan All declarations that refer to the same object or function shall 296169699Skan have compatible type; otherwise, the behavior is undefined. 297169699Skan 298169699Skan Initially only the built-in declarations, which describe compiler 299169699Skan intrinsic functions plus a subset of the standard library, are in 300169699Skan this scope. 301169699Skan 302169699Skan The order of the blocks list matters, and it is frequently appended 303169699Skan to. To avoid having to walk all the way to the end of the list on 304169699Skan each insertion, or reverse the list later, we maintain a pointer to 305169699Skan the last list entry. (FIXME: It should be feasible to use a reversed 306169699Skan list here.) 307169699Skan 308169699Skan The bindings list is strictly in reverse order of declarations; 309169699Skan pop_scope relies on this. */ 310169699Skan 311169699Skan 312169699Skanstruct c_scope GTY((chain_next ("%h.outer"))) 313132730Skan{ 314132730Skan /* The scope containing this one. */ 315132730Skan struct c_scope *outer; 31618334Speter 317132730Skan /* The next outermost function scope. */ 318132730Skan struct c_scope *outer_function; 31918334Speter 320169699Skan /* All bindings in this scope. */ 321169699Skan struct c_binding *bindings; 32218334Speter 323132730Skan /* For each scope (except the global one), a chain of BLOCK nodes 324132730Skan for all the scopes that were entered and exited one level down. */ 325132730Skan tree blocks; 326132730Skan tree blocks_last; 32718334Speter 328169699Skan /* The depth of this scope. Used to keep the ->shadowed chain of 329169699Skan bindings sorted innermost to outermost. */ 330169699Skan unsigned int depth : 28; 331169699Skan 332132730Skan /* True if we are currently filling this scope with parameter 333132730Skan declarations. */ 334132730Skan BOOL_BITFIELD parm_flag : 1; 33518334Speter 336169699Skan /* True if we saw [*] in this scope. Used to give an error messages 337169699Skan if these appears in a function definition. */ 338169699Skan BOOL_BITFIELD had_vla_unspec : 1; 339169699Skan 340132730Skan /* True if we already complained about forward parameter decls 341132730Skan in this scope. This prevents double warnings on 342132730Skan foo (int a; int b; ...) */ 343132730Skan BOOL_BITFIELD warned_forward_parm_decls : 1; 34490075Sobrien 345132730Skan /* True if this is the outermost block scope of a function body. 346132730Skan This scope contains the parameters, the local variables declared 347132730Skan in the outermost block, and all the labels (except those in 348132730Skan nested functions, or declared at block scope with __label__). */ 349132730Skan BOOL_BITFIELD function_body : 1; 35018334Speter 351132730Skan /* True means make a BLOCK for this scope no matter what. */ 352132730Skan BOOL_BITFIELD keep : 1; 353132730Skan}; 35418334Speter 355132730Skan/* The scope currently in effect. */ 35618334Speter 357132730Skanstatic GTY(()) struct c_scope *current_scope; 35818334Speter 359132730Skan/* The innermost function scope. Ordinary (not explicitly declared) 360132730Skan labels, bindings to error_mark_node, and the lazily-created 361132730Skan bindings of __func__ and its friends get this scope. */ 36218334Speter 363132730Skanstatic GTY(()) struct c_scope *current_function_scope; 36418334Speter 365169699Skan/* The C file scope. This is reset for each input translation unit. */ 36618334Speter 367169699Skanstatic GTY(()) struct c_scope *file_scope; 36818334Speter 369169699Skan/* The outermost scope. This is used for all declarations with 370169699Skan external linkage, and only these, hence the name. */ 371169699Skan 372169699Skanstatic GTY(()) struct c_scope *external_scope; 373169699Skan 374169699Skan/* A chain of c_scope structures awaiting reuse. */ 375169699Skan 376169699Skanstatic GTY((deletable)) struct c_scope *scope_freelist; 377169699Skan 378169699Skan/* A chain of c_binding structures awaiting reuse. */ 379169699Skan 380169699Skanstatic GTY((deletable)) struct c_binding *binding_freelist; 381169699Skan 382132730Skan/* Append VAR to LIST in scope SCOPE. */ 383132730Skan#define SCOPE_LIST_APPEND(scope, list, decl) do { \ 384132730Skan struct c_scope *s_ = (scope); \ 385132730Skan tree d_ = (decl); \ 386132730Skan if (s_->list##_last) \ 387132730Skan TREE_CHAIN (s_->list##_last) = d_; \ 388132730Skan else \ 389132730Skan s_->list = d_; \ 390132730Skan s_->list##_last = d_; \ 391132730Skan} while (0) 39218334Speter 393132730Skan/* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */ 394132730Skan#define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \ 395132730Skan struct c_scope *t_ = (tscope); \ 396132730Skan struct c_scope *f_ = (fscope); \ 397132730Skan if (t_->to##_last) \ 398132730Skan TREE_CHAIN (t_->to##_last) = f_->from; \ 399132730Skan else \ 400132730Skan t_->to = f_->from; \ 401132730Skan t_->to##_last = f_->from##_last; \ 402132730Skan} while (0) 40390075Sobrien 404132730Skan/* True means unconditionally make a BLOCK for the next scope pushed. */ 40518334Speter 406132730Skanstatic bool keep_next_level_flag; 40718334Speter 408169699Skan/* True means the next call to push_scope will be the outermost scope 409132730Skan of a function body, so do not push a new scope, merely cease 410132730Skan expecting parameter decls. */ 411132730Skan 412132730Skanstatic bool next_is_function_body; 413132730Skan 41418334Speter/* Functions called automatically at the beginning and end of execution. */ 41518334Speter 416169699Skanstatic GTY(()) tree static_ctors; 417169699Skanstatic GTY(()) tree static_dtors; 41818334Speter 41918334Speter/* Forward declarations. */ 420169699Skanstatic tree lookup_name_in_scope (tree, struct c_scope *); 421169699Skanstatic tree c_make_fname_decl (tree, int); 422169699Skanstatic tree grokdeclarator (const struct c_declarator *, 423169699Skan struct c_declspecs *, 424169699Skan enum decl_context, bool, tree *); 425169699Skanstatic tree grokparms (struct c_arg_info *, bool); 426132730Skanstatic void layout_array_type (tree); 42718334Speter 428169699Skan/* T is a statement. Add it to the statement-tree. This is the 429169699Skan C/ObjC version--C++ has a slightly different version of this 430169699Skan function. */ 431169699Skan 432169699Skantree 433169699Skanadd_stmt (tree t) 434169699Skan{ 435169699Skan enum tree_code code = TREE_CODE (t); 436169699Skan 437169699Skan if (EXPR_P (t) && code != LABEL_EXPR) 438169699Skan { 439169699Skan if (!EXPR_HAS_LOCATION (t)) 440169699Skan SET_EXPR_LOCATION (t, input_location); 441169699Skan } 442169699Skan 443169699Skan if (code == LABEL_EXPR || code == CASE_LABEL_EXPR) 444169699Skan STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1; 445169699Skan 446169699Skan /* Add T to the statement-tree. Non-side-effect statements need to be 447169699Skan recorded during statement expressions. */ 448169699Skan append_to_statement_list_force (t, &cur_stmt_list); 449169699Skan 450169699Skan return t; 451169699Skan} 452169699Skan 45390075Sobrien/* States indicating how grokdeclarator() should handle declspecs marked 45490075Sobrien with __attribute__((deprecated)). An object declared as 45590075Sobrien __attribute__((deprecated)) suppresses warnings of uses of other 45690075Sobrien deprecated items. */ 457132730Skan 45890075Sobrienenum deprecated_states { 45990075Sobrien DEPRECATED_NORMAL, 46090075Sobrien DEPRECATED_SUPPRESS 46190075Sobrien}; 46290075Sobrien 46390075Sobrienstatic enum deprecated_states deprecated_state = DEPRECATED_NORMAL; 46490075Sobrien 46518334Spetervoid 466132730Skanc_print_identifier (FILE *file, tree node, int indent) 46718334Speter{ 468169699Skan print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4); 469169699Skan print_node (file, "tag", I_TAG_DECL (node), indent + 4); 470169699Skan print_node (file, "label", I_LABEL_DECL (node), indent + 4); 47190075Sobrien if (C_IS_RESERVED_WORD (node)) 47290075Sobrien { 47390075Sobrien tree rid = ridpointers[C_RID_CODE (node)]; 47490075Sobrien indent_to (file, indent + 4); 475169699Skan fprintf (file, "rid %p \"%s\"", 476132730Skan (void *) rid, IDENTIFIER_POINTER (rid)); 47790075Sobrien } 47818334Speter} 479169699Skan 480169699Skan/* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL, 481169699Skan which may be any of several kinds of DECL or TYPE or error_mark_node, 482169699Skan in the scope SCOPE. */ 483169699Skanstatic void 484169699Skanbind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested) 485169699Skan{ 486169699Skan struct c_binding *b, **here; 487169699Skan 488169699Skan if (binding_freelist) 489169699Skan { 490169699Skan b = binding_freelist; 491169699Skan binding_freelist = b->prev; 492169699Skan } 493169699Skan else 494169699Skan b = GGC_NEW (struct c_binding); 495169699Skan 496169699Skan b->shadowed = 0; 497169699Skan b->decl = decl; 498169699Skan b->id = name; 499169699Skan b->depth = scope->depth; 500169699Skan b->invisible = invisible; 501169699Skan b->nested = nested; 502169699Skan b->inner_comp = 0; 503169699Skan 504169699Skan b->type = 0; 505169699Skan 506169699Skan b->prev = scope->bindings; 507169699Skan scope->bindings = b; 508169699Skan 509169699Skan if (!name) 510169699Skan return; 511169699Skan 512169699Skan switch (TREE_CODE (decl)) 513169699Skan { 514169699Skan case LABEL_DECL: here = &I_LABEL_BINDING (name); break; 515169699Skan case ENUMERAL_TYPE: 516169699Skan case UNION_TYPE: 517169699Skan case RECORD_TYPE: here = &I_TAG_BINDING (name); break; 518169699Skan case VAR_DECL: 519169699Skan case FUNCTION_DECL: 520169699Skan case TYPE_DECL: 521169699Skan case CONST_DECL: 522169699Skan case PARM_DECL: 523169699Skan case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break; 524169699Skan 525169699Skan default: 526169699Skan gcc_unreachable (); 527169699Skan } 528169699Skan 529169699Skan /* Locate the appropriate place in the chain of shadowed decls 530169699Skan to insert this binding. Normally, scope == current_scope and 531169699Skan this does nothing. */ 532169699Skan while (*here && (*here)->depth > scope->depth) 533169699Skan here = &(*here)->shadowed; 534169699Skan 535169699Skan b->shadowed = *here; 536169699Skan *here = b; 537169699Skan} 538169699Skan 539169699Skan/* Clear the binding structure B, stick it on the binding_freelist, 540169699Skan and return the former value of b->prev. This is used by pop_scope 541169699Skan and get_parm_info to iterate destructively over all the bindings 542169699Skan from a given scope. */ 543169699Skanstatic struct c_binding * 544169699Skanfree_binding_and_advance (struct c_binding *b) 545169699Skan{ 546169699Skan struct c_binding *prev = b->prev; 547169699Skan 548169699Skan memset (b, 0, sizeof (struct c_binding)); 549169699Skan b->prev = binding_freelist; 550169699Skan binding_freelist = b; 551169699Skan 552169699Skan return prev; 553169699Skan} 554169699Skan 55518334Speter 55618334Speter/* Hook called at end of compilation to assume 1 elt 557132730Skan for a file-scope tentative array defn that wasn't complete before. */ 55890075Sobrien 55918334Spetervoid 560132730Skanc_finish_incomplete_decl (tree decl) 56118334Speter{ 56250397Sobrien if (TREE_CODE (decl) == VAR_DECL) 56318334Speter { 56418334Speter tree type = TREE_TYPE (decl); 56550397Sobrien if (type != error_mark_node 56650397Sobrien && TREE_CODE (type) == ARRAY_TYPE 567169699Skan && !DECL_EXTERNAL (decl) 56850397Sobrien && TYPE_DOMAIN (type) == 0) 56918334Speter { 570169699Skan warning (0, "array %q+D assumed to have one element", decl); 57150397Sobrien 572169699Skan complete_array_type (&TREE_TYPE (decl), NULL_TREE, true); 57318334Speter 57418334Speter layout_decl (decl, 0); 57518334Speter } 57618334Speter } 57718334Speter} 57818334Speter 579132730Skan/* The Objective-C front-end often needs to determine the current scope. */ 58018334Speter 581132730Skanvoid * 582169699Skanobjc_get_current_scope (void) 58318334Speter{ 584132730Skan return current_scope; 58518334Speter} 58618334Speter 587132730Skan/* The following function is used only by Objective-C. It needs to live here 588132730Skan because it accesses the innards of c_scope. */ 589132730Skan 59018334Spetervoid 591132730Skanobjc_mark_locals_volatile (void *enclosing_blk) 59218334Speter{ 593132730Skan struct c_scope *scope; 594169699Skan struct c_binding *b; 595132730Skan 596132730Skan for (scope = current_scope; 597132730Skan scope && scope != enclosing_blk; 598132730Skan scope = scope->outer) 599132730Skan { 600169699Skan for (b = scope->bindings; b; b = b->prev) 601169699Skan objc_volatilize_decl (b->decl); 602132730Skan 603132730Skan /* Do not climb up past the current function. */ 604132730Skan if (scope->function_body) 605132730Skan break; 606132730Skan } 60718334Speter} 60818334Speter 609169699Skan/* Nonzero if we are currently in file scope. */ 61018334Speter 61118334Speterint 612132730Skanglobal_bindings_p (void) 61318334Speter{ 614169699Skan return current_scope == file_scope && !c_override_global_bindings_to_false; 61518334Speter} 61618334Speter 617132730Skanvoid 618132730Skankeep_next_level (void) 619132730Skan{ 620132730Skan keep_next_level_flag = true; 621132730Skan} 62218334Speter 623132730Skan/* Identify this scope as currently being filled with parameters. */ 624132730Skan 62518334Spetervoid 626132730Skandeclare_parm_level (void) 62718334Speter{ 628132730Skan current_scope->parm_flag = true; 62918334Speter} 63018334Speter 63118334Spetervoid 632169699Skanpush_scope (void) 63318334Speter{ 634132730Skan if (next_is_function_body) 635132730Skan { 636132730Skan /* This is the transition from the parameters to the top level 637132730Skan of the function body. These are the same scope 638132730Skan (C99 6.2.1p4,6) so we do not push another scope structure. 639132730Skan next_is_function_body is set only by store_parm_decls, which 640132730Skan in turn is called when and only when we are about to 641132730Skan encounter the opening curly brace for the function body. 64218334Speter 643132730Skan The outermost block of a function always gets a BLOCK node, 644132730Skan because the debugging output routines expect that each 645132730Skan function has at least one BLOCK. */ 646132730Skan current_scope->parm_flag = false; 647132730Skan current_scope->function_body = true; 648132730Skan current_scope->keep = true; 649132730Skan current_scope->outer_function = current_function_scope; 650132730Skan current_function_scope = current_scope; 65118334Speter 652132730Skan keep_next_level_flag = false; 653132730Skan next_is_function_body = false; 654132730Skan } 655132730Skan else 65618334Speter { 657169699Skan struct c_scope *scope; 658169699Skan if (scope_freelist) 659169699Skan { 660169699Skan scope = scope_freelist; 661169699Skan scope_freelist = scope->outer; 662169699Skan } 663169699Skan else 664169699Skan scope = GGC_CNEW (struct c_scope); 665132730Skan 666132730Skan scope->keep = keep_next_level_flag; 667132730Skan scope->outer = current_scope; 668169699Skan scope->depth = current_scope ? (current_scope->depth + 1) : 0; 669169699Skan 670169699Skan /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but 671169699Skan possible. */ 672169699Skan if (current_scope && scope->depth == 0) 673169699Skan { 674169699Skan scope->depth--; 675169699Skan sorry ("GCC supports only %u nested scopes", scope->depth); 676169699Skan } 677169699Skan 678132730Skan current_scope = scope; 679132730Skan keep_next_level_flag = false; 68018334Speter } 68118334Speter} 68218334Speter 683169699Skan/* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */ 68450397Sobrien 685169699Skanstatic void 686169699Skanset_type_context (tree type, tree context) 687169699Skan{ 688169699Skan for (type = TYPE_MAIN_VARIANT (type); type; 689169699Skan type = TYPE_NEXT_VARIANT (type)) 690169699Skan TYPE_CONTEXT (type) = context; 691169699Skan} 69250397Sobrien 693169699Skan/* Exit a scope. Restore the state of the identifier-decl mappings 694169699Skan that were in effect when this scope was entered. Return a BLOCK 695169699Skan node containing all the DECLs in this scope that are of interest 696169699Skan to debug info generation. */ 69750397Sobrien 69818334Spetertree 699169699Skanpop_scope (void) 70018334Speter{ 701132730Skan struct c_scope *scope = current_scope; 702169699Skan tree block, context, p; 703169699Skan struct c_binding *b; 70418334Speter 705169699Skan bool functionbody = scope->function_body; 706169699Skan bool keep = functionbody || scope->keep || scope->bindings; 70718334Speter 708169699Skan c_end_vm_scope (scope->depth); 70918334Speter 710132730Skan /* If appropriate, create a BLOCK to record the decls for the life 711132730Skan of this function. */ 71218334Speter block = 0; 713132730Skan if (keep) 71418334Speter { 715132730Skan block = make_node (BLOCK); 716132730Skan BLOCK_SUBBLOCKS (block) = scope->blocks; 717132730Skan TREE_USED (block) = 1; 71818334Speter 719169699Skan /* In each subblock, record that this is its superior. */ 720169699Skan for (p = scope->blocks; p; p = TREE_CHAIN (p)) 721169699Skan BLOCK_SUPERCONTEXT (p) = block; 72218334Speter 723169699Skan BLOCK_VARS (block) = 0; 724169699Skan } 72518334Speter 726169699Skan /* The TYPE_CONTEXTs for all of the tagged types belonging to this 727169699Skan scope must be set so that they point to the appropriate 728169699Skan construct, i.e. either to the current FUNCTION_DECL node, or 729169699Skan else to the BLOCK node we just constructed. 73018334Speter 731169699Skan Note that for tagged types whose scope is just the formal 732169699Skan parameter list for some function type specification, we can't 733169699Skan properly set their TYPE_CONTEXTs here, because we don't have a 734169699Skan pointer to the appropriate FUNCTION_TYPE node readily available 735169699Skan to us. For those cases, the TYPE_CONTEXTs of the relevant tagged 736169699Skan type nodes get set in `grokdeclarator' as soon as we have created 737169699Skan the FUNCTION_TYPE node which will represent the "scope" for these 738169699Skan "parameter list local" tagged types. */ 739169699Skan if (scope->function_body) 740169699Skan context = current_function_decl; 741169699Skan else if (scope == file_scope) 742169699Skan { 743169699Skan tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0); 744169699Skan TREE_CHAIN (file_decl) = all_translation_units; 745169699Skan all_translation_units = file_decl; 746169699Skan context = file_decl; 747169699Skan } 748169699Skan else 749169699Skan context = block; 750132730Skan 751169699Skan /* Clear all bindings in this scope. */ 752169699Skan for (b = scope->bindings; b; b = free_binding_and_advance (b)) 75318334Speter { 754169699Skan p = b->decl; 755132730Skan switch (TREE_CODE (p)) 75618334Speter { 757132730Skan case LABEL_DECL: 758169699Skan /* Warnings for unused labels, errors for undefined labels. */ 759132730Skan if (TREE_USED (p) && !DECL_INITIAL (p)) 76018334Speter { 761169699Skan error ("label %q+D used but not defined", p); 762132730Skan DECL_INITIAL (p) = error_mark_node; 76318334Speter } 764132730Skan else if (!TREE_USED (p) && warn_unused_label) 765132730Skan { 766132730Skan if (DECL_INITIAL (p)) 767169699Skan warning (0, "label %q+D defined but not used", p); 768132730Skan else 769169699Skan warning (0, "label %q+D declared but not defined", p); 770132730Skan } 771169699Skan /* Labels go in BLOCK_VARS. */ 772169699Skan TREE_CHAIN (p) = BLOCK_VARS (block); 773169699Skan BLOCK_VARS (block) = p; 774169699Skan gcc_assert (I_LABEL_BINDING (b->id) == b); 775169699Skan I_LABEL_BINDING (b->id) = b->shadowed; 776169699Skan break; 77718334Speter 778169699Skan case ENUMERAL_TYPE: 779169699Skan case UNION_TYPE: 780169699Skan case RECORD_TYPE: 781169699Skan set_type_context (p, context); 782169699Skan 783169699Skan /* Types may not have tag-names, in which case the type 784169699Skan appears in the bindings list with b->id NULL. */ 785169699Skan if (b->id) 786169699Skan { 787169699Skan gcc_assert (I_TAG_BINDING (b->id) == b); 788169699Skan I_TAG_BINDING (b->id) = b->shadowed; 789169699Skan } 790132730Skan break; 79118334Speter 792132730Skan case FUNCTION_DECL: 793169699Skan /* Propagate TREE_ADDRESSABLE from nested functions to their 794169699Skan containing functions. */ 795169699Skan if (!TREE_ASM_WRITTEN (p) 796132730Skan && DECL_INITIAL (p) != 0 797132730Skan && TREE_ADDRESSABLE (p) 798132730Skan && DECL_ABSTRACT_ORIGIN (p) != 0 799132730Skan && DECL_ABSTRACT_ORIGIN (p) != p) 800132730Skan TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1; 801169699Skan if (!DECL_EXTERNAL (p) 802169699Skan && DECL_INITIAL (p) == 0) 803169699Skan { 804169699Skan error ("nested function %q+D declared but never defined", p); 805169699Skan undef_nested_function = true; 806169699Skan } 807169699Skan goto common_symbol; 80818334Speter 809132730Skan case VAR_DECL: 810169699Skan /* Warnings for unused variables. */ 811169699Skan if (!TREE_USED (p) 812169699Skan && !TREE_NO_WARNING (p) 813132730Skan && !DECL_IN_SYSTEM_HEADER (p) 814132730Skan && DECL_NAME (p) 815169699Skan && !DECL_ARTIFICIAL (p) 816169699Skan && scope != file_scope 817169699Skan && scope != external_scope) 818169699Skan warning (OPT_Wunused_variable, "unused variable %q+D", p); 81918334Speter 820169699Skan if (b->inner_comp) 821132730Skan { 822169699Skan error ("type of array %q+D completed incompatibly with" 823169699Skan " implicit initialization", p); 824132730Skan } 825169699Skan 826169699Skan /* Fall through. */ 827169699Skan case TYPE_DECL: 828169699Skan case CONST_DECL: 829169699Skan common_symbol: 830169699Skan /* All of these go in BLOCK_VARS, but only if this is the 831169699Skan binding in the home scope. */ 832169699Skan if (!b->nested) 833169699Skan { 834169699Skan TREE_CHAIN (p) = BLOCK_VARS (block); 835169699Skan BLOCK_VARS (block) = p; 836169699Skan } 837169699Skan /* If this is the file scope, and we are processing more 838169699Skan than one translation unit in this compilation, set 839169699Skan DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL. 840169699Skan This makes same_translation_unit_p work, and causes 841169699Skan static declarations to be given disambiguating suffixes. */ 842169699Skan if (scope == file_scope && num_in_fnames > 1) 843169699Skan { 844169699Skan DECL_CONTEXT (p) = context; 845169699Skan if (TREE_CODE (p) == TYPE_DECL) 846169699Skan set_type_context (TREE_TYPE (p), context); 847169699Skan } 848169699Skan 849169699Skan /* Fall through. */ 850169699Skan /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have 851169699Skan already been put there by store_parm_decls. Unused- 852169699Skan parameter warnings are handled by function.c. 853169699Skan error_mark_node obviously does not go in BLOCK_VARS and 854169699Skan does not get unused-variable warnings. */ 855169699Skan case PARM_DECL: 856169699Skan case ERROR_MARK: 857169699Skan /* It is possible for a decl not to have a name. We get 858169699Skan here with b->id NULL in this case. */ 859169699Skan if (b->id) 860169699Skan { 861169699Skan gcc_assert (I_SYMBOL_BINDING (b->id) == b); 862169699Skan I_SYMBOL_BINDING (b->id) = b->shadowed; 863169699Skan if (b->shadowed && b->shadowed->type) 864169699Skan TREE_TYPE (b->shadowed->decl) = b->shadowed->type; 865169699Skan } 866132730Skan break; 867169699Skan 868169699Skan default: 869169699Skan gcc_unreachable (); 870132730Skan } 871132730Skan } 87250397Sobrien 87318334Speter 874169699Skan /* Dispose of the block that we just made inside some higher level. */ 875169699Skan if ((scope->function_body || scope == file_scope) && context) 876132730Skan { 877169699Skan DECL_INITIAL (context) = block; 878169699Skan BLOCK_SUPERCONTEXT (block) = context; 87918334Speter } 880132730Skan else if (scope->outer) 88118334Speter { 882132730Skan if (block) 883132730Skan SCOPE_LIST_APPEND (scope->outer, blocks, block); 884132730Skan /* If we did not make a block for the scope just exited, any 885132730Skan blocks made for inner scopes must be carried forward so they 886132730Skan will later become subblocks of something else. */ 887132730Skan else if (scope->blocks) 888132730Skan SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks); 88918334Speter } 89018334Speter 891132730Skan /* Pop the current scope, and free the structure for reuse. */ 892169699Skan current_scope = scope->outer; 893169699Skan if (scope->function_body) 894169699Skan current_function_scope = scope->outer_function; 89518334Speter 896169699Skan memset (scope, 0, sizeof (struct c_scope)); 897169699Skan scope->outer = scope_freelist; 898169699Skan scope_freelist = scope; 899169699Skan 90018334Speter return block; 90118334Speter} 90218334Speter 903169699Skanvoid 904169699Skanpush_file_scope (void) 905169699Skan{ 906169699Skan tree decl; 907169699Skan 908169699Skan if (file_scope) 909169699Skan return; 910169699Skan 911169699Skan push_scope (); 912169699Skan file_scope = current_scope; 913169699Skan 914169699Skan start_fname_decls (); 915169699Skan 916169699Skan for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl)) 917169699Skan bind (DECL_NAME (decl), decl, file_scope, 918169699Skan /*invisible=*/false, /*nested=*/true); 919169699Skan} 920169699Skan 921169699Skanvoid 922169699Skanpop_file_scope (void) 923169699Skan{ 924169699Skan /* In case there were missing closebraces, get us back to the global 925169699Skan binding level. */ 926169699Skan while (current_scope != file_scope) 927169699Skan pop_scope (); 928169699Skan 929169699Skan /* __FUNCTION__ is defined at file scope (""). This 930169699Skan call may not be necessary as my tests indicate it 931169699Skan still works without it. */ 932169699Skan finish_fname_decls (); 933169699Skan 934169699Skan /* This is the point to write out a PCH if we're doing that. 935169699Skan In that case we do not want to do anything else. */ 936169699Skan if (pch_file) 937169699Skan { 938169699Skan c_common_write_pch (); 939169699Skan return; 940169699Skan } 941169699Skan 942169699Skan /* Pop off the file scope and close this translation unit. */ 943169699Skan pop_scope (); 944169699Skan file_scope = 0; 945169699Skan 946169699Skan maybe_apply_pending_pragma_weaks (); 947169699Skan cgraph_finalize_compilation_unit (); 948169699Skan} 949169699Skan 950132730Skan/* Insert BLOCK at the end of the list of subblocks of the current 951132730Skan scope. This is used when a BIND_EXPR is expanded, to handle the 952132730Skan BLOCK node inside the BIND_EXPR. */ 95318334Speter 95418334Spetervoid 955132730Skaninsert_block (tree block) 95618334Speter{ 95718334Speter TREE_USED (block) = 1; 958132730Skan SCOPE_LIST_APPEND (current_scope, blocks, block); 95918334Speter} 96018334Speter 96118334Speter/* Push a definition or a declaration of struct, union or enum tag "name". 96218334Speter "type" should be the type node. 96318334Speter We assume that the tag "name" is not already defined. 96418334Speter 96518334Speter Note that the definition may really be just a forward reference. 96618334Speter In that case, the TYPE_SIZE will be zero. */ 96718334Speter 968169699Skanstatic void 969132730Skanpushtag (tree name, tree type) 97018334Speter{ 971132730Skan /* Record the identifier as the type's name if it has none. */ 972169699Skan if (name && !TYPE_NAME (type)) 973169699Skan TYPE_NAME (type) = name; 974169699Skan bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false); 975132730Skan 97618334Speter /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the 977132730Skan tagged type we just added to the current scope. This fake 97818334Speter NULL-named TYPE_DECL node helps dwarfout.c to know when it needs 97918334Speter to output a representation of a tagged type, and it also gives 98018334Speter us a convenient place to record the "scope start" address for the 98118334Speter tagged type. */ 98218334Speter 98318334Speter TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type)); 98450397Sobrien 98550397Sobrien /* An approximation for now, so we can tell this is a function-scope tag. 986169699Skan This will be updated in pop_scope. */ 98750397Sobrien TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type)); 98818334Speter} 98918334Speter 990132730Skan/* Subroutine of compare_decls. Allow harmless mismatches in return 991132730Skan and argument types provided that the type modes match. This function 992132730Skan return a unified type given a suitable match, and 0 otherwise. */ 99318334Speter 994132730Skanstatic tree 995132730Skanmatch_builtin_function_types (tree newtype, tree oldtype) 996132730Skan{ 997132730Skan tree newrettype, oldrettype; 998132730Skan tree newargs, oldargs; 999132730Skan tree trytype, tryargs; 100018334Speter 1001132730Skan /* Accept the return type of the new declaration if same modes. */ 1002132730Skan oldrettype = TREE_TYPE (oldtype); 1003132730Skan newrettype = TREE_TYPE (newtype); 100450397Sobrien 1005132730Skan if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype)) 1006132730Skan return 0; 1007132730Skan 1008132730Skan oldargs = TYPE_ARG_TYPES (oldtype); 1009132730Skan newargs = TYPE_ARG_TYPES (newtype); 1010132730Skan tryargs = newargs; 1011132730Skan 1012132730Skan while (oldargs || newargs) 1013132730Skan { 1014169699Skan if (!oldargs 1015169699Skan || !newargs 1016169699Skan || !TREE_VALUE (oldargs) 1017169699Skan || !TREE_VALUE (newargs) 1018132730Skan || TYPE_MODE (TREE_VALUE (oldargs)) 1019132730Skan != TYPE_MODE (TREE_VALUE (newargs))) 1020132730Skan return 0; 1021132730Skan 1022132730Skan oldargs = TREE_CHAIN (oldargs); 1023132730Skan newargs = TREE_CHAIN (newargs); 1024132730Skan } 1025132730Skan 1026132730Skan trytype = build_function_type (newrettype, tryargs); 1027132730Skan return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype)); 1028132730Skan} 1029132730Skan 1030169699Skan/* Subroutine of diagnose_mismatched_decls. Check for function type 1031132730Skan mismatch involving an empty arglist vs a nonempty one and give clearer 1032169699Skan diagnostics. */ 1033132730Skanstatic void 1034132730Skandiagnose_arglist_conflict (tree newdecl, tree olddecl, 1035132730Skan tree newtype, tree oldtype) 103618334Speter{ 1037132730Skan tree t; 103818334Speter 1039132730Skan if (TREE_CODE (olddecl) != FUNCTION_DECL 1040169699Skan || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype)) 1041132730Skan || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0) 1042132730Skan || 1043132730Skan (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0))) 1044132730Skan return; 1045132730Skan 1046132730Skan t = TYPE_ARG_TYPES (oldtype); 1047132730Skan if (t == 0) 1048132730Skan t = TYPE_ARG_TYPES (newtype); 1049132730Skan for (; t; t = TREE_CHAIN (t)) 105090075Sobrien { 1051132730Skan tree type = TREE_VALUE (t); 1052132730Skan 1053132730Skan if (TREE_CHAIN (t) == 0 1054132730Skan && TYPE_MAIN_VARIANT (type) != void_type_node) 105590075Sobrien { 1056169699Skan inform ("a parameter list with an ellipsis can%'t match " 1057132730Skan "an empty parameter name list declaration"); 1058132730Skan break; 105990075Sobrien } 106018334Speter 1061132730Skan if (c_type_promotes_to (type) != type) 1062132730Skan { 1063169699Skan inform ("an argument type that has a default promotion can%'t match " 1064132730Skan "an empty parameter name list declaration"); 1065132730Skan break; 1066132730Skan } 106790075Sobrien } 1068132730Skan} 106990075Sobrien 1070132730Skan/* Another subroutine of diagnose_mismatched_decls. OLDDECL is an 1071132730Skan old-style function definition, NEWDECL is a prototype declaration. 1072132730Skan Diagnose inconsistencies in the argument list. Returns TRUE if 1073132730Skan the prototype is compatible, FALSE if not. */ 1074132730Skanstatic bool 1075132730Skanvalidate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype) 1076132730Skan{ 1077132730Skan tree newargs, oldargs; 1078132730Skan int i; 107918334Speter 1080169699Skan#define END_OF_ARGLIST(t) ((t) == void_type_node) 1081132730Skan 1082132730Skan oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype); 1083132730Skan newargs = TYPE_ARG_TYPES (newtype); 1084132730Skan i = 1; 1085132730Skan 1086132730Skan for (;;) 1087132730Skan { 1088132730Skan tree oldargtype = TREE_VALUE (oldargs); 1089132730Skan tree newargtype = TREE_VALUE (newargs); 1090132730Skan 1091169699Skan if (oldargtype == error_mark_node || newargtype == error_mark_node) 1092169699Skan return false; 1093169699Skan 1094169699Skan oldargtype = TYPE_MAIN_VARIANT (oldargtype); 1095169699Skan newargtype = TYPE_MAIN_VARIANT (newargtype); 1096169699Skan 1097132730Skan if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype)) 1098132730Skan break; 1099132730Skan 1100132730Skan /* Reaching the end of just one list means the two decls don't 1101132730Skan agree on the number of arguments. */ 1102132730Skan if (END_OF_ARGLIST (oldargtype)) 1103132730Skan { 1104169699Skan error ("prototype for %q+D declares more arguments " 1105169699Skan "than previous old-style definition", newdecl); 1106132730Skan return false; 1107132730Skan } 1108132730Skan else if (END_OF_ARGLIST (newargtype)) 1109132730Skan { 1110169699Skan error ("prototype for %q+D declares fewer arguments " 1111169699Skan "than previous old-style definition", newdecl); 1112132730Skan return false; 1113132730Skan } 1114132730Skan 1115132730Skan /* Type for passing arg must be consistent with that declared 1116132730Skan for the arg. */ 1117169699Skan else if (!comptypes (oldargtype, newargtype)) 1118132730Skan { 1119169699Skan error ("prototype for %q+D declares argument %d" 1120169699Skan " with incompatible type", 1121169699Skan newdecl, i); 1122132730Skan return false; 1123132730Skan } 1124132730Skan 1125132730Skan oldargs = TREE_CHAIN (oldargs); 1126132730Skan newargs = TREE_CHAIN (newargs); 1127132730Skan i++; 1128132730Skan } 1129132730Skan 1130132730Skan /* If we get here, no errors were found, but do issue a warning 1131132730Skan for this poor-style construct. */ 1132169699Skan warning (0, "prototype for %q+D follows non-prototype definition", 1133169699Skan newdecl); 1134132730Skan return true; 1135132730Skan#undef END_OF_ARGLIST 1136132730Skan} 1137132730Skan 1138132730Skan/* Subroutine of diagnose_mismatched_decls. Report the location of DECL, 1139132730Skan first in a pair of mismatched declarations, using the diagnostic 1140132730Skan function DIAG. */ 1141132730Skanstatic void 1142169699Skanlocate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2)) 1143132730Skan{ 1144132730Skan if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)) 1145132730Skan ; 1146132730Skan else if (DECL_INITIAL (decl)) 1147169699Skan diag (G_("previous definition of %q+D was here"), decl); 1148132730Skan else if (C_DECL_IMPLICIT (decl)) 1149169699Skan diag (G_("previous implicit declaration of %q+D was here"), decl); 1150132730Skan else 1151169699Skan diag (G_("previous declaration of %q+D was here"), decl); 1152132730Skan} 1153132730Skan 1154132730Skan/* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL. 1155132730Skan Returns true if the caller should proceed to merge the two, false 1156132730Skan if OLDDECL should simply be discarded. As a side effect, issues 1157132730Skan all necessary diagnostics for invalid or poor-style combinations. 1158132730Skan If it returns true, writes the types of NEWDECL and OLDDECL to 1159132730Skan *NEWTYPEP and *OLDTYPEP - these may have been adjusted from 1160132730Skan TREE_TYPE (NEWDECL, OLDDECL) respectively. */ 1161132730Skan 1162132730Skanstatic bool 1163132730Skandiagnose_mismatched_decls (tree newdecl, tree olddecl, 1164132730Skan tree *newtypep, tree *oldtypep) 1165132730Skan{ 1166132730Skan tree newtype, oldtype; 1167132730Skan bool pedwarned = false; 1168132730Skan bool warned = false; 1169169699Skan bool retval = true; 1170132730Skan 1171169699Skan#define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \ 1172169699Skan && DECL_EXTERNAL (DECL)) 1173169699Skan 1174132730Skan /* If we have error_mark_node for either decl or type, just discard 1175132730Skan the previous decl - we're in an error cascade already. */ 1176132730Skan if (olddecl == error_mark_node || newdecl == error_mark_node) 1177132730Skan return false; 1178132730Skan *oldtypep = oldtype = TREE_TYPE (olddecl); 1179132730Skan *newtypep = newtype = TREE_TYPE (newdecl); 1180132730Skan if (oldtype == error_mark_node || newtype == error_mark_node) 1181132730Skan return false; 1182132730Skan 1183132730Skan /* Two different categories of symbol altogether. This is an error 1184132730Skan unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */ 118518334Speter if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) 118618334Speter { 1187169699Skan if (!(TREE_CODE (olddecl) == FUNCTION_DECL 1188169699Skan && DECL_BUILT_IN (olddecl) 1189169699Skan && !C_DECL_DECLARED_BUILTIN (olddecl))) 1190132730Skan { 1191169699Skan error ("%q+D redeclared as different kind of symbol", newdecl); 1192132730Skan locate_old_decl (olddecl, error); 1193132730Skan } 1194132730Skan else if (TREE_PUBLIC (newdecl)) 1195169699Skan warning (0, "built-in function %q+D declared as non-function", 1196169699Skan newdecl); 1197169699Skan else 1198169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows " 1199169699Skan "a built-in function", newdecl); 1200132730Skan return false; 1201132730Skan } 1202132730Skan 1203161660Skan /* Enumerators have no linkage, so may only be declared once in a 1204161660Skan given scope. */ 1205161660Skan if (TREE_CODE (olddecl) == CONST_DECL) 1206161660Skan { 1207169699Skan error ("redeclaration of enumerator %q+D", newdecl); 1208161660Skan locate_old_decl (olddecl, error); 1209161660Skan return false; 1210161660Skan } 1211161660Skan 1212169699Skan if (!comptypes (oldtype, newtype)) 1213132730Skan { 121418334Speter if (TREE_CODE (olddecl) == FUNCTION_DECL 1215169699Skan && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl)) 121618334Speter { 1217132730Skan /* Accept harmless mismatch in function types. 1218132730Skan This is for the ffs and fprintf builtins. */ 1219132730Skan tree trytype = match_builtin_function_types (newtype, oldtype); 1220132730Skan 1221169699Skan if (trytype && comptypes (newtype, trytype)) 1222132730Skan *oldtypep = oldtype = trytype; 1223132730Skan else 122418334Speter { 1225132730Skan /* If types don't match for a built-in, throw away the 1226132730Skan built-in. No point in calling locate_old_decl here, it 1227169699Skan won't print anything. */ 1228169699Skan warning (0, "conflicting types for built-in function %q+D", 1229169699Skan newdecl); 1230132730Skan return false; 123118334Speter } 123218334Speter } 1233132730Skan else if (TREE_CODE (olddecl) == FUNCTION_DECL 1234169699Skan && DECL_IS_BUILTIN (olddecl)) 1235132730Skan { 1236132730Skan /* A conflicting function declaration for a predeclared 1237132730Skan function that isn't actually built in. Objective C uses 1238132730Skan these. The new declaration silently overrides everything 1239132730Skan but the volatility (i.e. noreturn) indication. See also 1240132730Skan below. FIXME: Make Objective C use normal builtins. */ 1241132730Skan TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1242132730Skan return false; 1243132730Skan } 1244132730Skan /* Permit void foo (...) to match int foo (...) if the latter is 1245132730Skan the definition and implicit int was used. See 1246132730Skan c-torture/compile/920625-2.c. */ 1247132730Skan else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) 1248132730Skan && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node 1249132730Skan && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node 1250169699Skan && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl)) 1251132730Skan { 1252169699Skan pedwarn ("conflicting types for %q+D", newdecl); 1253132730Skan /* Make sure we keep void as the return type. */ 1254132730Skan TREE_TYPE (newdecl) = *newtypep = newtype = oldtype; 1255132730Skan C_FUNCTION_IMPLICIT_INT (newdecl) = 0; 1256132730Skan pedwarned = true; 1257132730Skan } 1258169699Skan /* Permit void foo (...) to match an earlier call to foo (...) with 1259169699Skan no declared type (thus, implicitly int). */ 1260169699Skan else if (TREE_CODE (newdecl) == FUNCTION_DECL 1261169699Skan && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node 1262169699Skan && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node 1263169699Skan && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl)) 1264169699Skan { 1265169699Skan pedwarn ("conflicting types for %q+D", newdecl); 1266169699Skan /* Make sure we keep void as the return type. */ 1267169699Skan TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype; 1268169699Skan pedwarned = true; 1269169699Skan } 127018334Speter else 127118334Speter { 1272169699Skan if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype)) 1273169699Skan error ("conflicting type qualifiers for %q+D", newdecl); 1274169699Skan else 1275169699Skan error ("conflicting types for %q+D", newdecl); 1276132730Skan diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype); 1277132730Skan locate_old_decl (olddecl, error); 1278132730Skan return false; 127918334Speter } 1280132730Skan } 128118334Speter 1282132730Skan /* Redeclaration of a type is a constraint violation (6.7.2.3p1), 1283132730Skan but silently ignore the redeclaration if either is in a system 1284132730Skan header. (Conflicting redeclarations were handled above.) */ 1285132730Skan if (TREE_CODE (newdecl) == TYPE_DECL) 1286132730Skan { 1287132730Skan if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl)) 1288169699Skan return true; /* Allow OLDDECL to continue in use. */ 1289169699Skan 1290169699Skan error ("redefinition of typedef %q+D", newdecl); 1291132730Skan locate_old_decl (olddecl, error); 1292132730Skan return false; 129318334Speter } 129418334Speter 1295132730Skan /* Function declarations can either be 'static' or 'extern' (no 1296132730Skan qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore 1297132730Skan can never conflict with each other on account of linkage (6.2.2p4). 1298132730Skan Multiple definitions are not allowed (6.9p3,5) but GCC permits 1299132730Skan two definitions if one is 'extern inline' and one is not. The non- 1300132730Skan extern-inline definition supersedes the extern-inline definition. */ 1301169699Skan 1302132730Skan else if (TREE_CODE (newdecl) == FUNCTION_DECL) 130318334Speter { 1304132730Skan /* If you declare a built-in function name as static, or 1305132730Skan define the built-in with an old-style definition (so we 1306132730Skan can't validate the argument list) the built-in definition is 1307132730Skan overridden, but optionally warn this was a bad choice of name. */ 1308132730Skan if (DECL_BUILT_IN (olddecl) 1309169699Skan && !C_DECL_DECLARED_BUILTIN (olddecl) 1310132730Skan && (!TREE_PUBLIC (newdecl) 1311132730Skan || (DECL_INITIAL (newdecl) 1312132730Skan && !TYPE_ARG_TYPES (TREE_TYPE (newdecl))))) 131318334Speter { 1314169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows " 1315169699Skan "a built-in function", newdecl); 131618334Speter /* Discard the old built-in function. */ 1317132730Skan return false; 131818334Speter } 1319169699Skan 1320132730Skan if (DECL_INITIAL (newdecl)) 132118334Speter { 1322169699Skan if (DECL_INITIAL (olddecl)) 132318334Speter { 1324169699Skan /* If both decls are in the same TU and the new declaration 1325169699Skan isn't overriding an extern inline reject the new decl. 1326169699Skan When we handle c99 style inline rules we'll want to reject 1327169699Skan the following: 1328169699Skan 1329169699Skan DECL_EXTERN_INLINE (olddecl) 1330169699Skan && !DECL_EXTERN_INLINE (newdecl) 1331169699Skan 1332169699Skan if they're in the same translation unit. Until we implement 1333169699Skan the full semantics we accept the construct. */ 1334169699Skan if (!(DECL_EXTERN_INLINE (olddecl) 1335169699Skan && !DECL_EXTERN_INLINE (newdecl)) 1336169699Skan && same_translation_unit_p (newdecl, olddecl)) 1337169699Skan { 1338169699Skan error ("redefinition of %q+D", newdecl); 1339169699Skan locate_old_decl (olddecl, error); 1340169699Skan return false; 1341169699Skan } 134218334Speter } 1343132730Skan } 1344132730Skan /* If we have a prototype after an old-style function definition, 1345132730Skan the argument types must be checked specially. */ 1346132730Skan else if (DECL_INITIAL (olddecl) 1347132730Skan && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype) 1348132730Skan && TYPE_ACTUAL_ARG_TYPES (oldtype) 1349132730Skan && !validate_proto_after_old_defn (newdecl, newtype, oldtype)) 1350132730Skan { 1351132730Skan locate_old_decl (olddecl, error); 1352132730Skan return false; 1353132730Skan } 1354169699Skan /* A non-static declaration (even an "extern") followed by a 1355169699Skan static declaration is undefined behavior per C99 6.2.2p3-5,7. 1356169699Skan The same is true for a static forward declaration at block 1357169699Skan scope followed by a non-static declaration/definition at file 1358169699Skan scope. Static followed by non-static at the same scope is 1359169699Skan not undefined behavior, and is the most convenient way to get 1360169699Skan some effects (see e.g. what unwind-dw2-fde-glibc.c does to 1361169699Skan the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but 1362169699Skan we do diagnose it if -Wtraditional. */ 1363132730Skan if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl)) 1364132730Skan { 1365169699Skan /* Two exceptions to the rule. If olddecl is an extern 1366169699Skan inline, or a predeclared function that isn't actually 1367169699Skan built in, newdecl silently overrides olddecl. The latter 1368169699Skan occur only in Objective C; see also above. (FIXME: Make 1369169699Skan Objective C use normal builtins.) */ 1370169699Skan if (!DECL_IS_BUILTIN (olddecl) 1371169699Skan && !DECL_EXTERN_INLINE (olddecl)) 137218334Speter { 1373169699Skan error ("static declaration of %q+D follows " 1374169699Skan "non-static declaration", newdecl); 1375169699Skan locate_old_decl (olddecl, error); 137618334Speter } 1377169699Skan return false; 137818334Speter } 1379169699Skan else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)) 138096263Sobrien { 1381169699Skan if (DECL_CONTEXT (olddecl)) 1382169699Skan { 1383169699Skan error ("non-static declaration of %q+D follows " 1384169699Skan "static declaration", newdecl); 1385169699Skan locate_old_decl (olddecl, error); 1386169699Skan return false; 1387169699Skan } 1388169699Skan else if (warn_traditional) 1389169699Skan { 1390169699Skan warning (OPT_Wtraditional, "non-static declaration of %q+D " 1391169699Skan "follows static declaration", newdecl); 1392169699Skan warned = true; 1393169699Skan } 139496263Sobrien } 139518334Speter } 1396132730Skan else if (TREE_CODE (newdecl) == VAR_DECL) 139718334Speter { 1398132730Skan /* Only variables can be thread-local, and all declarations must 1399132730Skan agree on this property. */ 1400169699Skan if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl)) 140118334Speter { 1402169699Skan /* Nothing to check. Since OLDDECL is marked threadprivate 1403169699Skan and NEWDECL does not have a thread-local attribute, we 1404169699Skan will merge the threadprivate attribute into NEWDECL. */ 1405169699Skan ; 1406169699Skan } 1407169699Skan else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)) 1408169699Skan { 1409169699Skan if (DECL_THREAD_LOCAL_P (newdecl)) 1410169699Skan error ("thread-local declaration of %q+D follows " 1411169699Skan "non-thread-local declaration", newdecl); 1412132730Skan else 1413169699Skan error ("non-thread-local declaration of %q+D follows " 1414169699Skan "thread-local declaration", newdecl); 1415132730Skan 1416132730Skan locate_old_decl (olddecl, error); 1417132730Skan return false; 141818334Speter } 1419132730Skan 1420132730Skan /* Multiple initialized definitions are not allowed (6.9p3,5). */ 1421132730Skan if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl)) 142218334Speter { 1423169699Skan error ("redefinition of %q+D", newdecl); 1424132730Skan locate_old_decl (olddecl, error); 1425132730Skan return false; 142618334Speter } 142718334Speter 1428169699Skan /* Objects declared at file scope: if the first declaration had 1429169699Skan external linkage (even if it was an external reference) the 1430169699Skan second must have external linkage as well, or the behavior is 1431169699Skan undefined. If the first declaration had internal linkage, then 1432169699Skan the second must too, or else be an external reference (in which 1433169699Skan case the composite declaration still has internal linkage). 1434169699Skan As for function declarations, we warn about the static-then- 1435169699Skan extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */ 1436169699Skan if (DECL_FILE_SCOPE_P (newdecl) 1437169699Skan && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl)) 143818334Speter { 1439169699Skan if (DECL_EXTERNAL (newdecl)) 144018334Speter { 1441169699Skan if (!DECL_FILE_SCOPE_P (olddecl)) 1442169699Skan { 1443169699Skan error ("extern declaration of %q+D follows " 1444169699Skan "declaration with no linkage", newdecl); 1445169699Skan locate_old_decl (olddecl, error); 1446169699Skan return false; 1447169699Skan } 1448169699Skan else if (warn_traditional) 1449169699Skan { 1450169699Skan warning (OPT_Wtraditional, "non-static declaration of %q+D " 1451169699Skan "follows static declaration", newdecl); 1452169699Skan warned = true; 1453169699Skan } 1454169699Skan } 1455169699Skan else 1456169699Skan { 1457132730Skan if (TREE_PUBLIC (newdecl)) 1458169699Skan error ("non-static declaration of %q+D follows " 1459169699Skan "static declaration", newdecl); 1460132730Skan else 1461169699Skan error ("static declaration of %q+D follows " 1462169699Skan "non-static declaration", newdecl); 146318334Speter 1464132730Skan locate_old_decl (olddecl, error); 1465132730Skan return false; 146618334Speter } 146718334Speter } 1468132730Skan /* Two objects with the same name declared at the same block 1469132730Skan scope must both be external references (6.7p3). */ 1470169699Skan else if (!DECL_FILE_SCOPE_P (newdecl)) 1471132730Skan { 1472132730Skan if (DECL_EXTERNAL (newdecl)) 1473169699Skan { 1474169699Skan /* Extern with initializer at block scope, which will 1475169699Skan already have received an error. */ 1476169699Skan } 1477132730Skan else if (DECL_EXTERNAL (olddecl)) 1478169699Skan { 1479169699Skan error ("declaration of %q+D with no linkage follows " 1480169699Skan "extern declaration", newdecl); 1481169699Skan locate_old_decl (olddecl, error); 1482169699Skan } 1483132730Skan else 1484169699Skan { 1485169699Skan error ("redeclaration of %q+D with no linkage", newdecl); 1486169699Skan locate_old_decl (olddecl, error); 1487169699Skan } 1488117421Skan 1489132730Skan return false; 1490132730Skan } 149118334Speter } 1492132730Skan 1493132730Skan /* warnings */ 1494169699Skan /* All decls must agree on a visibility. */ 1495169699Skan if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS) 1496169699Skan && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl) 1497132730Skan && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) 1498117421Skan { 1499169699Skan warning (0, "redeclaration of %q+D with different visibility " 1500169699Skan "(old visibility preserved)", newdecl); 1501132730Skan warned = true; 1502117421Skan } 1503132730Skan 1504132730Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 1505117421Skan { 1506132730Skan /* Diagnose inline __attribute__ ((noinline)) which is silly. */ 1507132730Skan if (DECL_DECLARED_INLINE_P (newdecl) 1508132730Skan && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 150918334Speter { 1510169699Skan warning (OPT_Wattributes, "inline declaration of %qD follows " 1511169699Skan "declaration with attribute noinline", newdecl); 1512132730Skan warned = true; 151318334Speter } 1514132730Skan else if (DECL_DECLARED_INLINE_P (olddecl) 1515132730Skan && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) 151618334Speter { 1517169699Skan warning (OPT_Wattributes, "declaration of %q+D with attribute " 1518169699Skan "noinline follows inline declaration ", newdecl); 1519132730Skan warned = true; 152018334Speter } 1521132730Skan 1522132730Skan /* Inline declaration after use or definition. 1523132730Skan ??? Should we still warn about this now we have unit-at-a-time 1524169699Skan mode and can get it right? 1525169699Skan Definitely don't complain if the decls are in different translation 1526169699Skan units. */ 1527169699Skan if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl) 1528169699Skan && same_translation_unit_p (olddecl, newdecl)) 152918334Speter { 1530132730Skan if (TREE_USED (olddecl)) 153118334Speter { 1532169699Skan warning (0, "%q+D declared inline after being called", olddecl); 1533132730Skan warned = true; 153418334Speter } 1535132730Skan else if (DECL_INITIAL (olddecl)) 1536132730Skan { 1537169699Skan warning (0, "%q+D declared inline after its definition", olddecl); 1538132730Skan warned = true; 1539132730Skan } 154018334Speter } 1541132730Skan } 1542132730Skan else /* PARM_DECL, VAR_DECL */ 1543132730Skan { 1544169699Skan /* Redeclaration of a parameter is a constraint violation (this is 1545169699Skan not explicitly stated, but follows from C99 6.7p3 [no more than 1546169699Skan one declaration of the same identifier with no linkage in the 1547169699Skan same scope, except type tags] and 6.2.2p6 [parameters have no 1548169699Skan linkage]). We must check for a forward parameter declaration, 1549169699Skan indicated by TREE_ASM_WRITTEN on the old declaration - this is 1550169699Skan an extension, the mandatory diagnostic for which is handled by 1551169699Skan mark_forward_parm_decls. */ 1552169699Skan 1553132730Skan if (TREE_CODE (newdecl) == PARM_DECL 1554132730Skan && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl))) 155518334Speter { 1556169699Skan error ("redefinition of parameter %q+D", newdecl); 1557132730Skan locate_old_decl (olddecl, error); 1558132730Skan return false; 1559132730Skan } 156018334Speter } 156118334Speter 1562132730Skan /* Optional warning for completely redundant decls. */ 1563132730Skan if (!warned && !pedwarned 1564132730Skan && warn_redundant_decls 1565132730Skan /* Don't warn about a function declaration followed by a 1566132730Skan definition. */ 1567132730Skan && !(TREE_CODE (newdecl) == FUNCTION_DECL 1568132730Skan && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)) 1569169699Skan /* Don't warn about redundant redeclarations of builtins. */ 1570132730Skan && !(TREE_CODE (newdecl) == FUNCTION_DECL 1571132730Skan && !DECL_BUILT_IN (newdecl) 1572132730Skan && DECL_BUILT_IN (olddecl) 1573169699Skan && !C_DECL_DECLARED_BUILTIN (olddecl)) 1574132730Skan /* Don't warn about an extern followed by a definition. */ 1575132730Skan && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl)) 1576132730Skan /* Don't warn about forward parameter decls. */ 1577132730Skan && !(TREE_CODE (newdecl) == PARM_DECL 1578149846Sobrien && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)) 1579149846Sobrien /* Don't warn about a variable definition following a declaration. */ 1580149846Sobrien && !(TREE_CODE (newdecl) == VAR_DECL 1581149846Sobrien && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))) 158218334Speter { 1583169699Skan warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D", 1584169699Skan newdecl); 1585132730Skan warned = true; 158618334Speter } 158718334Speter 1588132730Skan /* Report location of previous decl/defn in a consistent manner. */ 1589132730Skan if (warned || pedwarned) 1590169699Skan locate_old_decl (olddecl, pedwarned ? pedwarn : warning0); 159118334Speter 1592169699Skan#undef DECL_EXTERN_INLINE 1593169699Skan 1594169699Skan return retval; 1595132730Skan} 159618334Speter 1597132730Skan/* Subroutine of duplicate_decls. NEWDECL has been found to be 1598132730Skan consistent with OLDDECL, but carries new information. Merge the 1599132730Skan new information into OLDDECL. This function issues no 1600132730Skan diagnostics. */ 1601132730Skan 1602132730Skanstatic void 1603132730Skanmerge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype) 1604132730Skan{ 1605132730Skan int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL 1606132730Skan && DECL_INITIAL (newdecl) != 0); 1607169699Skan int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL 1608169699Skan && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0); 1609169699Skan int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL 1610169699Skan && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0); 1611132730Skan 1612169699Skan /* For real parm decl following a forward decl, rechain the old decl 1613169699Skan in its new location and clear TREE_ASM_WRITTEN (it's not a 1614169699Skan forward decl anymore). */ 1615132730Skan if (TREE_CODE (newdecl) == PARM_DECL 1616169699Skan && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)) 161718334Speter { 1618169699Skan struct c_binding *b, **here; 1619169699Skan 1620169699Skan for (here = ¤t_scope->bindings; *here; here = &(*here)->prev) 1621169699Skan if ((*here)->decl == olddecl) 1622169699Skan goto found; 1623169699Skan gcc_unreachable (); 1624169699Skan 1625169699Skan found: 1626169699Skan b = *here; 1627169699Skan *here = b->prev; 1628169699Skan b->prev = current_scope->bindings; 1629169699Skan current_scope->bindings = b; 1630169699Skan 1631132730Skan TREE_ASM_WRITTEN (olddecl) = 0; 1632132730Skan } 163350397Sobrien 1634132730Skan DECL_ATTRIBUTES (newdecl) 1635169699Skan = targetm.merge_decl_attributes (olddecl, newdecl); 163618334Speter 1637132730Skan /* Merge the data types specified in the two decls. */ 1638132730Skan TREE_TYPE (newdecl) 1639132730Skan = TREE_TYPE (olddecl) 1640169699Skan = composite_type (newtype, oldtype); 164118334Speter 1642132730Skan /* Lay the type out, unless already done. */ 1643169699Skan if (!comptypes (oldtype, TREE_TYPE (newdecl))) 1644132730Skan { 1645132730Skan if (TREE_TYPE (newdecl) != error_mark_node) 1646132730Skan layout_type (TREE_TYPE (newdecl)); 1647132730Skan if (TREE_CODE (newdecl) != FUNCTION_DECL 1648132730Skan && TREE_CODE (newdecl) != TYPE_DECL 1649132730Skan && TREE_CODE (newdecl) != CONST_DECL) 1650132730Skan layout_decl (newdecl, 0); 1651132730Skan } 1652132730Skan else 1653132730Skan { 1654132730Skan /* Since the type is OLDDECL's, make OLDDECL's size go with. */ 1655132730Skan DECL_SIZE (newdecl) = DECL_SIZE (olddecl); 1656132730Skan DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl); 1657132730Skan DECL_MODE (newdecl) = DECL_MODE (olddecl); 1658132730Skan if (TREE_CODE (olddecl) != FUNCTION_DECL) 1659132730Skan if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl)) 1660132730Skan { 1661132730Skan DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl); 1662132730Skan DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl); 1663132730Skan } 1664132730Skan } 166518334Speter 166690075Sobrien 1667132730Skan /* Merge the type qualifiers. */ 1668132730Skan if (TREE_READONLY (newdecl)) 1669132730Skan TREE_READONLY (olddecl) = 1; 167018334Speter 1671132730Skan if (TREE_THIS_VOLATILE (newdecl)) 1672169699Skan TREE_THIS_VOLATILE (olddecl) = 1; 167318334Speter 1674169699Skan /* Merge deprecatedness. */ 1675169699Skan if (TREE_DEPRECATED (newdecl)) 1676169699Skan TREE_DEPRECATED (olddecl) = 1; 1677169699Skan 1678169699Skan /* Keep source location of definition rather than declaration and of 1679169699Skan prototype rather than non-prototype unless that prototype is 1680169699Skan built-in. */ 1681169699Skan if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0) 1682169699Skan || (old_is_prototype && !new_is_prototype 1683169699Skan && !C_DECL_BUILTIN_PROTOTYPE (olddecl))) 1684132730Skan DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); 168518334Speter 1686132730Skan /* Merge the initialization information. */ 1687132730Skan if (DECL_INITIAL (newdecl) == 0) 1688132730Skan DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 168918334Speter 1690169699Skan /* Merge the threadprivate attribute. */ 1691169699Skan if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl)) 1692169699Skan { 1693169699Skan DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl); 1694169699Skan C_DECL_THREADPRIVATE_P (newdecl) = 1; 1695169699Skan } 169690075Sobrien 1697169699Skan if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)) 1698169699Skan { 1699169699Skan /* Merge the unused-warning information. */ 1700169699Skan if (DECL_IN_SYSTEM_HEADER (olddecl)) 1701169699Skan DECL_IN_SYSTEM_HEADER (newdecl) = 1; 1702169699Skan else if (DECL_IN_SYSTEM_HEADER (newdecl)) 1703169699Skan DECL_IN_SYSTEM_HEADER (olddecl) = 1; 1704132730Skan 1705169699Skan /* Merge the section attribute. 1706169699Skan We want to issue an error if the sections conflict but that 1707169699Skan must be done later in decl_attributes since we are called 1708169699Skan before attributes are assigned. */ 1709169699Skan if (DECL_SECTION_NAME (newdecl) == NULL_TREE) 1710169699Skan DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); 1711132730Skan 1712169699Skan /* Copy the assembler name. 1713169699Skan Currently, it can only be defined in the prototype. */ 1714169699Skan COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); 171518334Speter 1716169699Skan /* Use visibility of whichever declaration had it specified */ 1717169699Skan if (DECL_VISIBILITY_SPECIFIED (olddecl)) 1718169699Skan { 1719169699Skan DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); 1720169699Skan DECL_VISIBILITY_SPECIFIED (newdecl) = 1; 1721169699Skan } 172296263Sobrien 1723169699Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 1724169699Skan { 1725169699Skan DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl); 1726169699Skan DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); 1727169699Skan DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); 1728169699Skan DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) 1729169699Skan |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); 1730169699Skan TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1731169699Skan TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); 1732169699Skan DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); 1733169699Skan DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); 1734169699Skan DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl); 1735169699Skan } 1736169699Skan 1737169699Skan /* Merge the storage class information. */ 1738169699Skan merge_weak (newdecl, olddecl); 1739169699Skan 1740169699Skan /* For functions, static overrides non-static. */ 1741169699Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 1742169699Skan { 1743169699Skan TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl); 1744169699Skan /* This is since we don't automatically 1745169699Skan copy the attributes of NEWDECL into OLDDECL. */ 1746169699Skan TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 1747169699Skan /* If this clears `static', clear it in the identifier too. */ 1748169699Skan if (!TREE_PUBLIC (olddecl)) 1749169699Skan TREE_PUBLIC (DECL_NAME (olddecl)) = 0; 1750169699Skan } 175118334Speter } 1752169699Skan 175318334Speter if (DECL_EXTERNAL (newdecl)) 175418334Speter { 1755132730Skan TREE_STATIC (newdecl) = TREE_STATIC (olddecl); 1756132730Skan DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl); 1757132730Skan 175818334Speter /* An extern decl does not override previous storage class. */ 175918334Speter TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); 1760169699Skan if (!DECL_EXTERNAL (newdecl)) 1761132730Skan { 1762132730Skan DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); 1763132730Skan DECL_COMMON (newdecl) = DECL_COMMON (olddecl); 1764132730Skan } 176518334Speter } 176618334Speter else 176718334Speter { 176818334Speter TREE_STATIC (olddecl) = TREE_STATIC (newdecl); 176918334Speter TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 177018334Speter } 177118334Speter 177250397Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL) 177318334Speter { 177490075Sobrien /* If we're redefining a function previously defined as extern 177590075Sobrien inline, make sure we emit debug info for the inline before we 1776169699Skan throw it away, in case it was inlined into a function that 1777169699Skan hasn't been written out yet. */ 1778110623Skan if (new_is_definition && DECL_INITIAL (olddecl)) 177990075Sobrien { 1780132730Skan if (TREE_USED (olddecl) 1781132730Skan /* In unit-at-a-time mode we never inline re-defined extern 1782169699Skan inline functions. */ 1783132730Skan && !flag_unit_at_a_time 1784132730Skan && cgraph_function_possibly_inlined_p (olddecl)) 1785110623Skan (*debug_hooks->outlining_inline_function) (olddecl); 178690075Sobrien 178790075Sobrien /* The new defn must not be inline. */ 178890075Sobrien DECL_INLINE (newdecl) = 0; 178990075Sobrien DECL_UNINLINABLE (newdecl) = 1; 179090075Sobrien } 179190075Sobrien else 179290075Sobrien { 1793169699Skan /* If either decl says `inline', this fn is inline, unless 1794169699Skan its definition was passed already. */ 179590075Sobrien if (DECL_DECLARED_INLINE_P (newdecl) 179690075Sobrien || DECL_DECLARED_INLINE_P (olddecl)) 179790075Sobrien DECL_DECLARED_INLINE_P (newdecl) = 1; 179890075Sobrien 179990075Sobrien DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) 180090075Sobrien = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); 180190075Sobrien } 180290075Sobrien 180318334Speter if (DECL_BUILT_IN (olddecl)) 180418334Speter { 1805169699Skan /* If redeclaring a builtin function, it stays built in. 1806169699Skan But it gets tagged as having been declared. */ 1807132730Skan DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); 1808132730Skan DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); 1809169699Skan C_DECL_DECLARED_BUILTIN (newdecl) = 1; 1810169699Skan if (new_is_prototype) 1811169699Skan C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0; 1812169699Skan else 1813169699Skan C_DECL_BUILTIN_PROTOTYPE (newdecl) 1814169699Skan = C_DECL_BUILTIN_PROTOTYPE (olddecl); 181518334Speter } 181690075Sobrien 181750397Sobrien /* Also preserve various other info from the definition. */ 1818169699Skan if (!new_is_definition) 181950397Sobrien { 182050397Sobrien DECL_RESULT (newdecl) = DECL_RESULT (olddecl); 1821132730Skan DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 1822169699Skan DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl); 182390075Sobrien DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); 182450397Sobrien DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); 182590075Sobrien 182690075Sobrien /* Set DECL_INLINE on the declaration if we've got a body 182790075Sobrien from which to instantiate. */ 1828169699Skan if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl)) 182990075Sobrien { 183090075Sobrien DECL_INLINE (newdecl) = 1; 183190075Sobrien DECL_ABSTRACT_ORIGIN (newdecl) 1832132730Skan = DECL_ABSTRACT_ORIGIN (olddecl); 183390075Sobrien } 183450397Sobrien } 183590075Sobrien else 183690075Sobrien { 183790075Sobrien /* If a previous declaration said inline, mark the 183890075Sobrien definition as inlinable. */ 183990075Sobrien if (DECL_DECLARED_INLINE_P (newdecl) 1840169699Skan && !DECL_UNINLINABLE (newdecl)) 184190075Sobrien DECL_INLINE (newdecl) = 1; 184290075Sobrien } 184350397Sobrien } 184418334Speter 184518334Speter /* Copy most of the decl-specific fields of NEWDECL into OLDDECL. 1846169699Skan But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */ 184718334Speter { 184890075Sobrien unsigned olddecl_uid = DECL_UID (olddecl); 1849169699Skan tree olddecl_context = DECL_CONTEXT (olddecl); 185018334Speter 185190075Sobrien memcpy ((char *) olddecl + sizeof (struct tree_common), 185290075Sobrien (char *) newdecl + sizeof (struct tree_common), 1853169699Skan sizeof (struct tree_decl_common) - sizeof (struct tree_common)); 1854169699Skan switch (TREE_CODE (olddecl)) 1855169699Skan { 1856169699Skan case FIELD_DECL: 1857169699Skan case VAR_DECL: 1858169699Skan case PARM_DECL: 1859169699Skan case LABEL_DECL: 1860169699Skan case RESULT_DECL: 1861169699Skan case CONST_DECL: 1862169699Skan case TYPE_DECL: 1863169699Skan case FUNCTION_DECL: 1864169699Skan memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 1865169699Skan (char *) newdecl + sizeof (struct tree_decl_common), 1866169699Skan tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common)); 1867169699Skan break; 1868169699Skan 1869169699Skan default: 1870169699Skan 1871169699Skan memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 1872169699Skan (char *) newdecl + sizeof (struct tree_decl_common), 1873169699Skan sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)); 1874169699Skan } 187518334Speter DECL_UID (olddecl) = olddecl_uid; 1876169699Skan DECL_CONTEXT (olddecl) = olddecl_context; 187718334Speter } 187818334Speter 1879132730Skan /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl 1880119268Skan so that encode_section_info has a chance to look at the new decl 1881119268Skan flags and attributes. */ 1882119268Skan if (DECL_RTL_SET_P (olddecl) 1883119268Skan && (TREE_CODE (olddecl) == FUNCTION_DECL 1884119268Skan || (TREE_CODE (olddecl) == VAR_DECL 1885119268Skan && TREE_STATIC (olddecl)))) 1886169699Skan make_decl_rtl (olddecl); 1887132730Skan} 1888119268Skan 1889132730Skan/* Handle when a new declaration NEWDECL has the same name as an old 1890132730Skan one OLDDECL in the same binding contour. Prints an error message 1891132730Skan if appropriate. 1892132730Skan 1893132730Skan If safely possible, alter OLDDECL to look like NEWDECL, and return 1894132730Skan true. Otherwise, return false. */ 1895132730Skan 1896132730Skanstatic bool 1897132730Skanduplicate_decls (tree newdecl, tree olddecl) 1898132730Skan{ 1899169699Skan tree newtype = NULL, oldtype = NULL; 1900132730Skan 1901132730Skan if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype)) 1902169699Skan { 1903169699Skan /* Avoid `unused variable' and other warnings warnings for OLDDECL. */ 1904169699Skan TREE_NO_WARNING (olddecl) = 1; 1905169699Skan return false; 1906169699Skan } 1907132730Skan 1908132730Skan merge_decls (newdecl, olddecl, newtype, oldtype); 1909132730Skan return true; 191018334Speter} 1911169699Skan 1912132730Skan 1913169699Skan/* Check whether decl-node NEW_DECL shadows an existing declaration. */ 1914132730Skanstatic void 1915169699Skanwarn_if_shadowing (tree new_decl) 1916132730Skan{ 1917169699Skan struct c_binding *b; 1918132730Skan 1919169699Skan /* Shadow warnings wanted? */ 1920169699Skan if (!warn_shadow 1921132730Skan /* No shadow warnings for internally generated vars. */ 1922169699Skan || DECL_IS_BUILTIN (new_decl) 1923132730Skan /* No shadow warnings for vars made for inlining. */ 1924169699Skan || DECL_FROM_INLINE (new_decl)) 192590075Sobrien return; 192690075Sobrien 1927169699Skan /* Is anything being shadowed? Invisible decls do not count. */ 1928169699Skan for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed) 1929169699Skan if (b->decl && b->decl != new_decl && !b->invisible) 1930169699Skan { 1931169699Skan tree old_decl = b->decl; 193290075Sobrien 1933169699Skan if (old_decl == error_mark_node) 1934169699Skan { 1935169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows previous " 1936169699Skan "non-variable", new_decl); 1937169699Skan break; 1938169699Skan } 1939169699Skan else if (TREE_CODE (old_decl) == PARM_DECL) 1940169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows a parameter", 1941169699Skan new_decl); 1942169699Skan else if (DECL_FILE_SCOPE_P (old_decl)) 1943169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows a global " 1944169699Skan "declaration", new_decl); 1945169699Skan else if (TREE_CODE (old_decl) == FUNCTION_DECL 1946169699Skan && DECL_BUILT_IN (old_decl)) 1947169699Skan { 1948169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows " 1949169699Skan "a built-in function", new_decl); 1950169699Skan break; 1951169699Skan } 1952169699Skan else 1953169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows a previous local", 1954169699Skan new_decl); 1955169699Skan 1956169699Skan warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl); 1957169699Skan 1958169699Skan break; 1959169699Skan } 1960132730Skan} 1961132730Skan 1962132730Skan 1963132730Skan/* Subroutine of pushdecl. 1964132730Skan 1965132730Skan X is a TYPE_DECL for a typedef statement. Create a brand new 1966132730Skan ..._TYPE node (which will be just a variant of the existing 1967132730Skan ..._TYPE node with identical properties) and then install X 1968132730Skan as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. 1969132730Skan 1970132730Skan The whole point here is to end up with a situation where each 1971132730Skan and every ..._TYPE node the compiler creates will be uniquely 1972132730Skan associated with AT MOST one node representing a typedef name. 1973132730Skan This way, even though the compiler substitutes corresponding 1974132730Skan ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very 1975132730Skan early on, later parts of the compiler can always do the reverse 1976132730Skan translation and get back the corresponding typedef name. For 1977132730Skan example, given: 1978132730Skan 1979169699Skan typedef struct S MY_TYPE; 1980132730Skan MY_TYPE object; 1981132730Skan 1982132730Skan Later parts of the compiler might only know that `object' was of 1983132730Skan type `struct S' if it were not for code just below. With this 1984132730Skan code however, later parts of the compiler see something like: 1985132730Skan 1986132730Skan struct S' == struct S 1987132730Skan typedef struct S' MY_TYPE; 1988132730Skan struct S' object; 1989132730Skan 1990132730Skan And they can then deduce (from the node for type struct S') that 1991132730Skan the original object declaration was: 1992132730Skan 1993132730Skan MY_TYPE object; 1994132730Skan 1995132730Skan Being able to do this is important for proper support of protoize, 1996132730Skan and also for generating precise symbolic debugging information 1997132730Skan which takes full account of the programmer's (typedef) vocabulary. 1998132730Skan 1999132730Skan Obviously, we don't want to generate a duplicate ..._TYPE node if 2000132730Skan the TYPE_DECL node that we are now processing really represents a 2001132730Skan standard built-in type. 2002132730Skan 2003132730Skan Since all standard types are effectively declared at line zero 2004132730Skan in the source file, we can easily check to see if we are working 2005132730Skan on a standard type by checking the current value of lineno. */ 2006132730Skan 2007132730Skanstatic void 2008132730Skanclone_underlying_type (tree x) 2009132730Skan{ 2010169699Skan if (DECL_IS_BUILTIN (x)) 201190075Sobrien { 2012132730Skan if (TYPE_NAME (TREE_TYPE (x)) == 0) 2013132730Skan TYPE_NAME (TREE_TYPE (x)) = x; 201490075Sobrien } 2015132730Skan else if (TREE_TYPE (x) != error_mark_node 2016132730Skan && DECL_ORIGINAL_TYPE (x) == NULL_TREE) 201790075Sobrien { 2018132730Skan tree tt = TREE_TYPE (x); 2019132730Skan DECL_ORIGINAL_TYPE (x) = tt; 2020169699Skan tt = build_variant_type_copy (tt); 2021132730Skan TYPE_NAME (tt) = x; 2022132730Skan TREE_USED (tt) = TREE_USED (x); 2023132730Skan TREE_TYPE (x) = tt; 202490075Sobrien } 202590075Sobrien} 202690075Sobrien 202718334Speter/* Record a decl-node X as belonging to the current lexical scope. 202818334Speter Check for errors (such as an incompatible declaration for the same 202918334Speter name already seen in the same scope). 203018334Speter 203118334Speter Returns either X or an old decl for the same name. 203218334Speter If an old decl is returned, it may have been smashed 203318334Speter to agree with what X says. */ 203418334Speter 203518334Spetertree 2036132730Skanpushdecl (tree x) 203718334Speter{ 203890075Sobrien tree name = DECL_NAME (x); 2039132730Skan struct c_scope *scope = current_scope; 2040169699Skan struct c_binding *b; 2041169699Skan bool nested = false; 204218334Speter 204390075Sobrien /* Functions need the lang_decl data. */ 2044169699Skan if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x)) 2045169699Skan DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl); 204690075Sobrien 2047169699Skan /* Must set DECL_CONTEXT for everything not at file scope or 2048169699Skan DECL_FILE_SCOPE_P won't work. Local externs don't count 2049169699Skan unless they have initializers (which generate code). */ 2050169699Skan if (current_function_decl 2051169699Skan && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL) 2052169699Skan || DECL_INITIAL (x) || !DECL_EXTERNAL (x))) 2053132730Skan DECL_CONTEXT (x) = current_function_decl; 205418334Speter 2055169699Skan /* If this is of variably modified type, prevent jumping into its 2056169699Skan scope. */ 2057169699Skan if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL) 2058169699Skan && variably_modified_type_p (TREE_TYPE (x), NULL_TREE)) 2059169699Skan c_begin_vm_scope (scope->depth); 2060169699Skan 2061169699Skan /* Anonymous decls are just inserted in the scope. */ 2062169699Skan if (!name) 206318334Speter { 2064169699Skan bind (name, x, scope, /*invisible=*/false, /*nested=*/false); 2065169699Skan return x; 2066169699Skan } 206718334Speter 2068169699Skan /* First, see if there is another declaration with the same name in 2069169699Skan the current scope. If there is, duplicate_decls may do all the 2070169699Skan work for us. If duplicate_decls returns false, that indicates 2071169699Skan two incompatible decls in the same scope; we are to silently 2072169699Skan replace the old one (duplicate_decls has issued all appropriate 2073169699Skan diagnostics). In particular, we should not consider possible 2074169699Skan duplicates in the external scope, or shadowing. */ 2075169699Skan b = I_SYMBOL_BINDING (name); 2076169699Skan if (b && B_IN_SCOPE (b, scope)) 2077169699Skan { 2078169699Skan struct c_binding *b_ext, *b_use; 2079169699Skan tree type = TREE_TYPE (x); 2080169699Skan tree visdecl = b->decl; 2081169699Skan tree vistype = TREE_TYPE (visdecl); 2082169699Skan if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 2083169699Skan && COMPLETE_TYPE_P (TREE_TYPE (x))) 2084169699Skan b->inner_comp = false; 2085169699Skan b_use = b; 2086169699Skan b_ext = b; 2087169699Skan /* If this is an external linkage declaration, we should check 2088169699Skan for compatibility with the type in the external scope before 2089169699Skan setting the type at this scope based on the visible 2090169699Skan information only. */ 2091169699Skan if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl)) 2092169699Skan { 2093169699Skan while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext)) 2094169699Skan b_ext = b_ext->shadowed; 2095169699Skan if (b_ext) 2096169699Skan { 2097169699Skan b_use = b_ext; 2098169699Skan if (b_use->type) 2099169699Skan TREE_TYPE (b_use->decl) = b_use->type; 2100169699Skan } 2101169699Skan } 2102169699Skan if (duplicate_decls (x, b_use->decl)) 2103169699Skan { 2104169699Skan if (b_use != b) 2105169699Skan { 2106169699Skan /* Save the updated type in the external scope and 2107169699Skan restore the proper type for this scope. */ 2108169699Skan tree thistype; 2109169699Skan if (comptypes (vistype, type)) 2110169699Skan thistype = composite_type (vistype, type); 2111169699Skan else 2112169699Skan thistype = TREE_TYPE (b_use->decl); 2113169699Skan b_use->type = TREE_TYPE (b_use->decl); 2114169699Skan if (TREE_CODE (b_use->decl) == FUNCTION_DECL 2115169699Skan && DECL_BUILT_IN (b_use->decl)) 2116169699Skan thistype 2117169699Skan = build_type_attribute_variant (thistype, 2118169699Skan TYPE_ATTRIBUTES 2119169699Skan (b_use->type)); 2120169699Skan TREE_TYPE (b_use->decl) = thistype; 2121169699Skan } 2122169699Skan return b_use->decl; 2123169699Skan } 2124169699Skan else 2125169699Skan goto skip_external_and_shadow_checks; 2126169699Skan } 2127169699Skan 2128169699Skan /* All declarations with external linkage, and all external 2129169699Skan references, go in the external scope, no matter what scope is 2130169699Skan current. However, the binding in that scope is ignored for 2131169699Skan purposes of normal name lookup. A separate binding structure is 2132169699Skan created in the requested scope; this governs the normal 2133169699Skan visibility of the symbol. 2134169699Skan 2135169699Skan The binding in the externals scope is used exclusively for 2136169699Skan detecting duplicate declarations of the same object, no matter 2137169699Skan what scope they are in; this is what we do here. (C99 6.2.7p2: 2138169699Skan All declarations that refer to the same object or function shall 2139169699Skan have compatible type; otherwise, the behavior is undefined.) */ 2140169699Skan if (DECL_EXTERNAL (x) || scope == file_scope) 2141169699Skan { 2142169699Skan tree type = TREE_TYPE (x); 2143169699Skan tree vistype = 0; 2144169699Skan tree visdecl = 0; 2145169699Skan bool type_saved = false; 2146169699Skan if (b && !B_IN_EXTERNAL_SCOPE (b) 2147169699Skan && (TREE_CODE (b->decl) == FUNCTION_DECL 2148169699Skan || TREE_CODE (b->decl) == VAR_DECL) 2149169699Skan && DECL_FILE_SCOPE_P (b->decl)) 2150169699Skan { 2151169699Skan visdecl = b->decl; 2152169699Skan vistype = TREE_TYPE (visdecl); 2153169699Skan } 2154169699Skan if (scope != file_scope 215590075Sobrien && !DECL_IN_SYSTEM_HEADER (x)) 2156169699Skan warning (OPT_Wnested_externs, "nested extern declaration of %qD", x); 215790075Sobrien 2158169699Skan while (b && !B_IN_EXTERNAL_SCOPE (b)) 215950397Sobrien { 2160169699Skan /* If this decl might be modified, save its type. This is 2161169699Skan done here rather than when the decl is first bound 2162169699Skan because the type may change after first binding, through 2163169699Skan being completed or through attributes being added. If we 2164169699Skan encounter multiple such decls, only the first should have 2165169699Skan its type saved; the others will already have had their 2166169699Skan proper types saved and the types will not have changed as 2167169699Skan their scopes will not have been re-entered. */ 2168169699Skan if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved) 216918334Speter { 2170169699Skan b->type = TREE_TYPE (b->decl); 2171169699Skan type_saved = true; 217218334Speter } 2173169699Skan if (B_IN_FILE_SCOPE (b) 2174169699Skan && TREE_CODE (b->decl) == VAR_DECL 2175169699Skan && TREE_STATIC (b->decl) 2176169699Skan && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE 2177169699Skan && !TYPE_DOMAIN (TREE_TYPE (b->decl)) 2178169699Skan && TREE_CODE (type) == ARRAY_TYPE 2179169699Skan && TYPE_DOMAIN (type) 2180169699Skan && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) 2181169699Skan && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) 2182169699Skan { 2183169699Skan /* Array type completed in inner scope, which should be 2184169699Skan diagnosed if the completion does not have size 1 and 2185169699Skan it does not get completed in the file scope. */ 2186169699Skan b->inner_comp = true; 2187169699Skan } 2188169699Skan b = b->shadowed; 218918334Speter } 2190169699Skan 2191169699Skan /* If a matching external declaration has been found, set its 2192169699Skan type to the composite of all the types of that declaration. 2193169699Skan After the consistency checks, it will be reset to the 2194169699Skan composite of the visible types only. */ 2195169699Skan if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl)) 2196169699Skan && b->type) 2197169699Skan TREE_TYPE (b->decl) = b->type; 2198169699Skan 2199169699Skan /* The point of the same_translation_unit_p check here is, 2200169699Skan we want to detect a duplicate decl for a construct like 2201169699Skan foo() { extern bar(); } ... static bar(); but not if 2202169699Skan they are in different translation units. In any case, 2203169699Skan the static does not go in the externals scope. */ 2204169699Skan if (b 2205169699Skan && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl)) 2206169699Skan && duplicate_decls (x, b->decl)) 220790075Sobrien { 2208169699Skan tree thistype; 2209169699Skan if (vistype) 221090075Sobrien { 2211169699Skan if (comptypes (vistype, type)) 2212169699Skan thistype = composite_type (vistype, type); 2213169699Skan else 2214169699Skan thistype = TREE_TYPE (b->decl); 221590075Sobrien } 221618334Speter else 2217169699Skan thistype = type; 2218169699Skan b->type = TREE_TYPE (b->decl); 2219169699Skan if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl)) 2220169699Skan thistype 2221169699Skan = build_type_attribute_variant (thistype, 2222169699Skan TYPE_ATTRIBUTES (b->type)); 2223169699Skan TREE_TYPE (b->decl) = thistype; 2224169699Skan bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true); 2225169699Skan return b->decl; 222618334Speter } 2227169699Skan else if (TREE_PUBLIC (x)) 222818334Speter { 2229169699Skan if (visdecl && !b && duplicate_decls (x, visdecl)) 223018334Speter { 2231169699Skan /* An external declaration at block scope referring to a 2232169699Skan visible entity with internal linkage. The composite 2233169699Skan type will already be correct for this scope, so we 2234169699Skan just need to fall through to make the declaration in 2235169699Skan this scope. */ 2236169699Skan nested = true; 2237169699Skan x = visdecl; 223818334Speter } 2239169699Skan else 2240169699Skan { 2241169699Skan bind (name, x, external_scope, /*invisible=*/true, 2242169699Skan /*nested=*/false); 2243169699Skan nested = true; 2244169699Skan } 224518334Speter } 2246169699Skan } 224790075Sobrien 2248169699Skan if (TREE_CODE (x) != PARM_DECL) 2249169699Skan warn_if_shadowing (x); 225018334Speter 2251169699Skan skip_external_and_shadow_checks: 2252169699Skan if (TREE_CODE (x) == TYPE_DECL) 2253169699Skan clone_underlying_type (x); 225418334Speter 2255169699Skan bind (name, x, scope, /*invisible=*/false, nested); 225690075Sobrien 2257169699Skan /* If x's type is incomplete because it's based on a 2258169699Skan structure or union which has not yet been fully declared, 2259169699Skan attach it to that structure or union type, so we can go 2260169699Skan back and complete the variable declaration later, if the 2261169699Skan structure or union gets fully declared. 2262132730Skan 2263169699Skan If the input is erroneous, we can have error_mark in the type 2264169699Skan slot (e.g. "f(void a, ...)") - that doesn't count as an 2265169699Skan incomplete type. */ 2266169699Skan if (TREE_TYPE (x) != error_mark_node 2267169699Skan && !COMPLETE_TYPE_P (TREE_TYPE (x))) 2268169699Skan { 2269169699Skan tree element = TREE_TYPE (x); 227018334Speter 2271169699Skan while (TREE_CODE (element) == ARRAY_TYPE) 2272169699Skan element = TREE_TYPE (element); 2273169699Skan element = TYPE_MAIN_VARIANT (element); 227418334Speter 2275169699Skan if ((TREE_CODE (element) == RECORD_TYPE 2276169699Skan || TREE_CODE (element) == UNION_TYPE) 2277169699Skan && (TREE_CODE (x) != TYPE_DECL 2278169699Skan || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE) 2279169699Skan && !COMPLETE_TYPE_P (element)) 2280169699Skan C_TYPE_INCOMPLETE_VARS (element) 2281169699Skan = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element)); 2282169699Skan } 228318334Speter return x; 228418334Speter} 228518334Speter 2286169699Skan/* Record X as belonging to file scope. 2287132730Skan This is used only internally by the Objective-C front end, 2288132730Skan and is limited to its needs. duplicate_decls is not called; 2289132730Skan if there is any preexisting decl for this identifier, it is an ICE. */ 229018334Speter 229118334Spetertree 2292132730Skanpushdecl_top_level (tree x) 229318334Speter{ 2294132730Skan tree name; 2295169699Skan bool nested = false; 2296169699Skan gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL); 229718334Speter 2298132730Skan name = DECL_NAME (x); 2299132730Skan 2300169699Skan gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name)); 2301132730Skan 2302169699Skan if (TREE_PUBLIC (x)) 2303169699Skan { 2304169699Skan bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false); 2305169699Skan nested = true; 2306169699Skan } 2307169699Skan if (file_scope) 2308169699Skan bind (name, x, file_scope, /*invisible=*/false, nested); 2309132730Skan 2310132730Skan return x; 231118334Speter} 231218334Speter 2313169699Skanstatic void 2314169699Skanimplicit_decl_warning (tree id, tree olddecl) 2315169699Skan{ 2316169699Skan void (*diag) (const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2); 2317169699Skan switch (mesg_implicit_function_declaration) 2318169699Skan { 2319169699Skan case 0: return; 2320169699Skan case 1: diag = warning0; break; 2321169699Skan case 2: diag = error; break; 2322169699Skan default: gcc_unreachable (); 2323169699Skan } 2324169699Skan 2325169699Skan diag (G_("implicit declaration of function %qE"), id); 2326169699Skan if (olddecl) 2327169699Skan locate_old_decl (olddecl, diag); 2328169699Skan} 2329169699Skan 2330132730Skan/* Generate an implicit declaration for identifier FUNCTIONID as a 2331132730Skan function of type int (). */ 233218334Speter 233318334Spetertree 2334132730Skanimplicitly_declare (tree functionid) 233518334Speter{ 2336169699Skan struct c_binding *b; 2337169699Skan tree decl = 0; 2338169699Skan tree asmspec_tree; 233918334Speter 2340169699Skan for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed) 2341169699Skan { 2342169699Skan if (B_IN_SCOPE (b, external_scope)) 2343169699Skan { 2344169699Skan decl = b->decl; 2345169699Skan break; 2346169699Skan } 2347169699Skan } 2348169699Skan 2349132730Skan if (decl) 2350132730Skan { 2351169699Skan if (decl == error_mark_node) 2352169699Skan return decl; 2353169699Skan 2354169699Skan /* FIXME: Objective-C has weird not-really-builtin functions 2355169699Skan which are supposed to be visible automatically. They wind up 2356169699Skan in the external scope because they're pushed before the file 2357169699Skan scope gets created. Catch this here and rebind them into the 2358169699Skan file scope. */ 2359169699Skan if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl)) 2360132730Skan { 2361169699Skan bind (functionid, decl, file_scope, 2362169699Skan /*invisible=*/false, /*nested=*/true); 2363169699Skan return decl; 2364132730Skan } 2365169699Skan else 2366169699Skan { 2367169699Skan tree newtype = default_function_type; 2368169699Skan if (b->type) 2369169699Skan TREE_TYPE (decl) = b->type; 2370169699Skan /* Implicit declaration of a function already declared 2371169699Skan (somehow) in a different scope, or as a built-in. 2372169699Skan If this is the first time this has happened, warn; 2373169699Skan then recycle the old declaration but with the new type. */ 2374169699Skan if (!C_DECL_IMPLICIT (decl)) 2375169699Skan { 2376169699Skan implicit_decl_warning (functionid, decl); 2377169699Skan C_DECL_IMPLICIT (decl) = 1; 2378169699Skan } 2379169699Skan if (DECL_BUILT_IN (decl)) 2380169699Skan { 2381169699Skan newtype = build_type_attribute_variant (newtype, 2382169699Skan TYPE_ATTRIBUTES 2383169699Skan (TREE_TYPE (decl))); 2384169699Skan if (!comptypes (newtype, TREE_TYPE (decl))) 2385169699Skan { 2386169699Skan warning (0, "incompatible implicit declaration of built-in" 2387169699Skan " function %qD", decl); 2388169699Skan newtype = TREE_TYPE (decl); 2389169699Skan } 2390169699Skan } 2391169699Skan else 2392169699Skan { 2393169699Skan if (!comptypes (newtype, TREE_TYPE (decl))) 2394169699Skan { 2395169699Skan error ("incompatible implicit declaration of function %qD", 2396169699Skan decl); 2397169699Skan locate_old_decl (decl, error); 2398169699Skan } 2399169699Skan } 2400169699Skan b->type = TREE_TYPE (decl); 2401169699Skan TREE_TYPE (decl) = newtype; 2402169699Skan bind (functionid, decl, current_scope, 2403169699Skan /*invisible=*/false, /*nested=*/true); 2404169699Skan return decl; 2405169699Skan } 2406132730Skan } 240718334Speter 2408132730Skan /* Not seen before. */ 2409132730Skan decl = build_decl (FUNCTION_DECL, functionid, default_function_type); 241018334Speter DECL_EXTERNAL (decl) = 1; 241118334Speter TREE_PUBLIC (decl) = 1; 2412132730Skan C_DECL_IMPLICIT (decl) = 1; 2413169699Skan implicit_decl_warning (functionid, 0); 2414169699Skan asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL); 2415169699Skan if (asmspec_tree) 2416169699Skan set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree)); 241718334Speter 2418132730Skan /* C89 says implicit declarations are in the innermost block. 2419117421Skan So we record the decl in the standard fashion. */ 2420132730Skan decl = pushdecl (decl); 242118334Speter 2422132730Skan /* No need to call objc_check_decl here - it's a function type. */ 2423169699Skan rest_of_decl_compilation (decl, 0, 0); 242418334Speter 2425132730Skan /* Write a record describing this implicit function declaration 2426132730Skan to the prototypes file (if requested). */ 242718334Speter gen_aux_info_record (decl, 0, 1, 0); 242818334Speter 242990075Sobrien /* Possibly apply some default attributes to this implicit declaration. */ 243090075Sobrien decl_attributes (&decl, NULL_TREE, 0); 243118334Speter 243218334Speter return decl; 243318334Speter} 243418334Speter 2435132730Skan/* Issue an error message for a reference to an undeclared variable 2436132730Skan ID, including a reference to a builtin outside of function-call 2437132730Skan context. Establish a binding of the identifier to error_mark_node 2438132730Skan in an appropriate scope, which will suppress further errors for the 2439169699Skan same identifier. The error message should be given location LOC. */ 2440132730Skanvoid 2441169699Skanundeclared_variable (tree id, location_t loc) 2442132730Skan{ 2443132730Skan static bool already = false; 2444132730Skan struct c_scope *scope; 244518334Speter 2446132730Skan if (current_function_decl == 0) 244718334Speter { 2448169699Skan error ("%H%qE undeclared here (not in a function)", &loc, id); 2449132730Skan scope = current_scope; 245018334Speter } 245118334Speter else 245218334Speter { 2453169699Skan error ("%H%qE undeclared (first use in this function)", &loc, id); 2454132730Skan 2455169699Skan if (!already) 2456132730Skan { 2457169699Skan error ("%H(Each undeclared identifier is reported only once", &loc); 2458169699Skan error ("%Hfor each function it appears in.)", &loc); 2459132730Skan already = true; 2460132730Skan } 2461132730Skan 2462169699Skan /* If we are parsing old-style parameter decls, current_function_decl 2463169699Skan will be nonnull but current_function_scope will be null. */ 2464169699Skan scope = current_function_scope ? current_function_scope : current_scope; 246518334Speter } 2466169699Skan bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false); 246718334Speter} 246818334Speter 2469132730Skan/* Subroutine of lookup_label, declare_label, define_label: construct a 2470132730Skan LABEL_DECL with all the proper frills. */ 2471132730Skan 2472132730Skanstatic tree 2473132730Skanmake_label (tree name, location_t location) 2474132730Skan{ 2475132730Skan tree label = build_decl (LABEL_DECL, name, void_type_node); 2476132730Skan 2477132730Skan DECL_CONTEXT (label) = current_function_decl; 2478132730Skan DECL_MODE (label) = VOIDmode; 2479132730Skan DECL_SOURCE_LOCATION (label) = location; 2480132730Skan 2481132730Skan return label; 2482132730Skan} 2483132730Skan 2484132730Skan/* Get the LABEL_DECL corresponding to identifier NAME as a label. 248518334Speter Create one if none exists so far for the current function. 2486132730Skan This is called when a label is used in a goto expression or 2487132730Skan has its address taken. */ 248818334Speter 248918334Spetertree 2490132730Skanlookup_label (tree name) 249118334Speter{ 2492132730Skan tree label; 249318334Speter 249418334Speter if (current_function_decl == 0) 249518334Speter { 2496169699Skan error ("label %qE referenced outside of any function", name); 249718334Speter return 0; 249818334Speter } 249918334Speter 2500132730Skan /* Use a label already defined or ref'd with this name, but not if 2501132730Skan it is inherited from a containing function and wasn't declared 2502132730Skan using __label__. */ 2503169699Skan label = I_LABEL_DECL (name); 2504132730Skan if (label && (DECL_CONTEXT (label) == current_function_decl 2505132730Skan || C_DECLARED_LABEL_FLAG (label))) 250618334Speter { 2507132730Skan /* If the label has only been declared, update its apparent 2508132730Skan location to point here, for better diagnostics if it 2509132730Skan turns out not to have been defined. */ 2510132730Skan if (!TREE_USED (label)) 2511132730Skan DECL_SOURCE_LOCATION (label) = input_location; 2512132730Skan return label; 251318334Speter } 251418334Speter 2515132730Skan /* No label binding for that identifier; make one. */ 2516132730Skan label = make_label (name, input_location); 251718334Speter 2518132730Skan /* Ordinary labels go in the current function scope. */ 2519169699Skan bind (name, label, current_function_scope, 2520169699Skan /*invisible=*/false, /*nested=*/false); 2521132730Skan return label; 252218334Speter} 252318334Speter 2524132730Skan/* Make a label named NAME in the current function, shadowing silently 2525132730Skan any that may be inherited from containing functions or containing 2526132730Skan scopes. This is called for __label__ declarations. */ 252718334Speter 252818334Spetertree 2529132730Skandeclare_label (tree name) 253018334Speter{ 2531169699Skan struct c_binding *b = I_LABEL_BINDING (name); 2532169699Skan tree label; 253318334Speter 2534132730Skan /* Check to make sure that the label hasn't already been declared 2535132730Skan at this scope */ 2536169699Skan if (b && B_IN_CURRENT_SCOPE (b)) 2537169699Skan { 2538169699Skan error ("duplicate label declaration %qE", name); 2539169699Skan locate_old_decl (b->decl, error); 254018334Speter 2541169699Skan /* Just use the previous declaration. */ 2542169699Skan return b->decl; 2543169699Skan } 254418334Speter 2545132730Skan label = make_label (name, input_location); 2546132730Skan C_DECLARED_LABEL_FLAG (label) = 1; 254718334Speter 2548132730Skan /* Declared labels go in the current scope. */ 2549169699Skan bind (name, label, current_scope, 2550169699Skan /*invisible=*/false, /*nested=*/false); 2551132730Skan return label; 255218334Speter} 255318334Speter 255418334Speter/* Define a label, specifying the location in the source file. 255518334Speter Return the LABEL_DECL node for the label, if the definition is valid. 255618334Speter Otherwise return 0. */ 255718334Speter 255818334Spetertree 2559132730Skandefine_label (location_t location, tree name) 256018334Speter{ 2561132730Skan /* Find any preexisting label with this name. It is an error 2562132730Skan if that label has already been defined in this function, or 2563132730Skan if there is a containing function with a declared label with 2564132730Skan the same name. */ 2565169699Skan tree label = I_LABEL_DECL (name); 2566169699Skan struct c_label_list *nlist_se, *nlist_vm; 256718334Speter 2568132730Skan if (label 2569132730Skan && ((DECL_CONTEXT (label) == current_function_decl 2570132730Skan && DECL_INITIAL (label) != 0) 2571132730Skan || (DECL_CONTEXT (label) != current_function_decl 2572132730Skan && C_DECLARED_LABEL_FLAG (label)))) 257318334Speter { 2574169699Skan error ("%Hduplicate label %qD", &location, label); 2575169699Skan locate_old_decl (label, error); 257618334Speter return 0; 257718334Speter } 2578132730Skan else if (label && DECL_CONTEXT (label) == current_function_decl) 2579132730Skan { 2580132730Skan /* The label has been used or declared already in this function, 2581132730Skan but not defined. Update its location to point to this 2582132730Skan definition. */ 2583169699Skan if (C_DECL_UNDEFINABLE_STMT_EXPR (label)) 2584169699Skan error ("%Jjump into statement expression", label); 2585169699Skan if (C_DECL_UNDEFINABLE_VM (label)) 2586169699Skan error ("%Jjump into scope of identifier with variably modified type", 2587169699Skan label); 2588132730Skan DECL_SOURCE_LOCATION (label) = location; 2589132730Skan } 259018334Speter else 259118334Speter { 2592132730Skan /* No label binding for that identifier; make one. */ 2593132730Skan label = make_label (name, location); 2594132730Skan 2595132730Skan /* Ordinary labels go in the current function scope. */ 2596169699Skan bind (name, label, current_function_scope, 2597169699Skan /*invisible=*/false, /*nested=*/false); 259818334Speter } 2599132730Skan 2600169699Skan if (!in_system_header && lookup_name (name)) 2601169699Skan warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace " 2602169699Skan "for labels, identifier %qE conflicts", &location, name); 2603132730Skan 2604169699Skan nlist_se = XOBNEW (&parser_obstack, struct c_label_list); 2605169699Skan nlist_se->next = label_context_stack_se->labels_def; 2606169699Skan nlist_se->label = label; 2607169699Skan label_context_stack_se->labels_def = nlist_se; 2608169699Skan 2609169699Skan nlist_vm = XOBNEW (&parser_obstack, struct c_label_list); 2610169699Skan nlist_vm->next = label_context_stack_vm->labels_def; 2611169699Skan nlist_vm->label = label; 2612169699Skan label_context_stack_vm->labels_def = nlist_vm; 2613169699Skan 2614132730Skan /* Mark label as having been defined. */ 2615132730Skan DECL_INITIAL (label) = error_mark_node; 2616132730Skan return label; 261718334Speter} 261818334Speter 261918334Speter/* Given NAME, an IDENTIFIER_NODE, 262018334Speter return the structure (or union or enum) definition for that name. 2621132730Skan If THISLEVEL_ONLY is nonzero, searches only the current_scope. 262218334Speter CODE says which kind of type the caller wants; 262318334Speter it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. 262418334Speter If the wrong kind of type is found, an error is reported. */ 262518334Speter 262618334Speterstatic tree 2627132730Skanlookup_tag (enum tree_code code, tree name, int thislevel_only) 262818334Speter{ 2629169699Skan struct c_binding *b = I_TAG_BINDING (name); 2630132730Skan int thislevel = 0; 263118334Speter 2632169699Skan if (!b || !b->decl) 2633132730Skan return 0; 2634132730Skan 2635132730Skan /* We only care about whether it's in this level if 2636132730Skan thislevel_only was set or it might be a type clash. */ 2637169699Skan if (thislevel_only || TREE_CODE (b->decl) != code) 263818334Speter { 2639169699Skan /* For our purposes, a tag in the external scope is the same as 2640169699Skan a tag in the file scope. (Primarily relevant to Objective-C 2641169699Skan and its builtin structure tags, which get pushed before the 2642169699Skan file scope is created.) */ 2643169699Skan if (B_IN_CURRENT_SCOPE (b) 2644169699Skan || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b))) 2645132730Skan thislevel = 1; 264618334Speter } 2647132730Skan 2648132730Skan if (thislevel_only && !thislevel) 2649132730Skan return 0; 2650132730Skan 2651169699Skan if (TREE_CODE (b->decl) != code) 2652132730Skan { 2653132730Skan /* Definition isn't the kind we were looking for. */ 2654132730Skan pending_invalid_xref = name; 2655132730Skan pending_invalid_xref_location = input_location; 2656132730Skan 2657132730Skan /* If in the same binding level as a declaration as a tag 2658132730Skan of a different type, this must not be allowed to 2659132730Skan shadow that tag, so give the error immediately. 2660132730Skan (For example, "struct foo; union foo;" is invalid.) */ 2661132730Skan if (thislevel) 2662132730Skan pending_xref_error (); 2663132730Skan } 2664169699Skan return b->decl; 266518334Speter} 266618334Speter 266718334Speter/* Print an error message now 266818334Speter for a recent invalid struct, union or enum cross reference. 266918334Speter We don't print them immediately because they are not invalid 267018334Speter when used in the `struct foo;' construct for shadowing. */ 267118334Speter 267218334Spetervoid 2673132730Skanpending_xref_error (void) 267418334Speter{ 267518334Speter if (pending_invalid_xref != 0) 2676169699Skan error ("%H%qE defined as wrong kind of tag", 2677169699Skan &pending_invalid_xref_location, pending_invalid_xref); 267818334Speter pending_invalid_xref = 0; 267918334Speter} 268018334Speter 268118334Speter 2682132730Skan/* Look up NAME in the current scope and its superiors 268318334Speter in the namespace of variables, functions and typedefs. 268418334Speter Return a ..._DECL node of some kind representing its definition, 268518334Speter or return 0 if it is undefined. */ 268618334Speter 268718334Spetertree 2688132730Skanlookup_name (tree name) 268918334Speter{ 2690169699Skan struct c_binding *b = I_SYMBOL_BINDING (name); 2691169699Skan if (b && !b->invisible) 2692169699Skan return b->decl; 2693169699Skan return 0; 269418334Speter} 269518334Speter 2696169699Skan/* Similar to `lookup_name' but look only at the indicated scope. */ 269718334Speter 2698132730Skanstatic tree 2699169699Skanlookup_name_in_scope (tree name, struct c_scope *scope) 270018334Speter{ 2701169699Skan struct c_binding *b; 270218334Speter 2703169699Skan for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed) 2704169699Skan if (B_IN_SCOPE (b, scope)) 2705169699Skan return b->decl; 2706132730Skan return 0; 270718334Speter} 270818334Speter 270918334Speter/* Create the predefined scalar types of C, 271050397Sobrien and some nodes representing standard constants (0, 1, (void *) 0). 2711132730Skan Initialize the global scope. 271218334Speter Make definitions for built-in primitive functions. */ 271318334Speter 271418334Spetervoid 2715132730Skanc_init_decl_processing (void) 271618334Speter{ 2717132730Skan location_t save_loc = input_location; 271818334Speter 2719169699Skan /* Initialize reserved words for parser. */ 272090075Sobrien c_parse_init (); 272190075Sobrien 2722132730Skan current_function_decl = 0; 272390075Sobrien 2724169699Skan gcc_obstack_init (&parser_obstack); 272518334Speter 2726169699Skan /* Make the externals scope. */ 2727169699Skan push_scope (); 2728169699Skan external_scope = current_scope; 2729169699Skan 2730132730Skan /* Declarations from c_common_nodes_and_builtins must not be associated 2731132730Skan with this input file, lest we get differences between using and not 2732132730Skan using preprocessed headers. */ 2733169699Skan#ifdef USE_MAPPED_LOCATION 2734169699Skan input_location = BUILTINS_LOCATION; 2735169699Skan#else 2736169699Skan input_location.file = "<built-in>"; 2737132730Skan input_location.line = 0; 2738169699Skan#endif 2739132730Skan 2740169699Skan build_common_tree_nodes (flag_signed_char, false); 2741132730Skan 274290075Sobrien c_common_nodes_and_builtins (); 274318334Speter 2744132730Skan /* In C, comparisons and TRUTH_* expressions have type int. */ 2745132730Skan truthvalue_type_node = integer_type_node; 2746132730Skan truthvalue_true_node = integer_one_node; 2747132730Skan truthvalue_false_node = integer_zero_node; 274818334Speter 2749132730Skan /* Even in C99, which has a real boolean type. */ 275090075Sobrien pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"), 2751132730Skan boolean_type_node)); 275218334Speter 2753132730Skan input_location = save_loc; 2754132730Skan 2755169699Skan pedantic_lvalues = true; 275618334Speter 275790075Sobrien make_fname_decl = c_make_fname_decl; 275890075Sobrien start_fname_decls (); 275990075Sobrien} 276018334Speter 276190075Sobrien/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the 276290075Sobrien decl, NAME is the initialization string and TYPE_DEP indicates whether 276390075Sobrien NAME depended on the type of the function. As we don't yet implement 276490075Sobrien delayed emission of static data, we mark the decl as emitted 276590075Sobrien so it is not placed in the output. Anything using it must therefore pull 2766132730Skan out the STRING_CST initializer directly. FIXME. */ 276718334Speter 276890075Sobrienstatic tree 2769132730Skanc_make_fname_decl (tree id, int type_dep) 277090075Sobrien{ 277190075Sobrien const char *name = fname_as_string (type_dep); 277290075Sobrien tree decl, type, init; 277390075Sobrien size_t length = strlen (name); 277418334Speter 2775169699Skan type = build_array_type (char_type_node, 2776169699Skan build_index_type (size_int (length))); 2777169699Skan type = c_build_qualified_type (type, TYPE_QUAL_CONST); 277818334Speter 277990075Sobrien decl = build_decl (VAR_DECL, id, type); 2780132730Skan 278190075Sobrien TREE_STATIC (decl) = 1; 278290075Sobrien TREE_READONLY (decl) = 1; 278390075Sobrien DECL_ARTIFICIAL (decl) = 1; 2784132730Skan 278590075Sobrien init = build_string (length + 1, name); 2786169699Skan free ((char *) name); 278790075Sobrien TREE_TYPE (init) = type; 278890075Sobrien DECL_INITIAL (decl) = init; 278918334Speter 279090075Sobrien TREE_USED (decl) = 1; 2791132730Skan 2792169699Skan if (current_function_decl 2793169699Skan /* For invalid programs like this: 2794169699Skan 2795169699Skan void foo() 2796169699Skan const char* p = __FUNCTION__; 2797169699Skan 2798169699Skan the __FUNCTION__ is believed to appear in K&R style function 2799169699Skan parameter declarator. In that case we still don't have 2800169699Skan function_scope. */ 2801169699Skan && (!errorcount || current_function_scope)) 2802132730Skan { 2803132730Skan DECL_CONTEXT (decl) = current_function_decl; 2804169699Skan bind (id, decl, current_function_scope, 2805169699Skan /*invisible=*/false, /*nested=*/false); 2806132730Skan } 2807132730Skan 280890075Sobrien finish_decl (decl, init, NULL_TREE); 280918334Speter 281090075Sobrien return decl; 281118334Speter} 281218334Speter 281318334Speter/* Return a definition for a builtin function named NAME and whose data type 281418334Speter is TYPE. TYPE should be a function type with argument types. 281518334Speter FUNCTION_CODE tells later passes how to compile calls to this function. 281618334Speter See tree.h for its possible values. 281718334Speter 281818334Speter If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME, 2819117421Skan the name to be called if we can't opencode the function. If 2820117421Skan ATTRS is nonzero, use that for the function's attribute list. */ 282118334Speter 282218334Spetertree 2823132730Skanbuiltin_function (const char *name, tree type, int function_code, 2824169699Skan enum built_in_class cl, const char *library_name, 2825132730Skan tree attrs) 282618334Speter{ 2827169699Skan tree id = get_identifier (name); 2828169699Skan tree decl = build_decl (FUNCTION_DECL, id, type); 2829169699Skan TREE_PUBLIC (decl) = 1; 283018334Speter DECL_EXTERNAL (decl) = 1; 2831169699Skan DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl); 2832169699Skan DECL_BUILT_IN_CLASS (decl) = cl; 2833169699Skan DECL_FUNCTION_CODE (decl) = function_code; 2834169699Skan C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0); 283518334Speter if (library_name) 283690075Sobrien SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name)); 283790075Sobrien 2838169699Skan /* Should never be called on a symbol with a preexisting meaning. */ 2839169699Skan gcc_assert (!I_SYMBOL_BINDING (id)); 284018334Speter 2841169699Skan bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false); 2842169699Skan 2843169699Skan /* Builtins in the implementation namespace are made visible without 2844169699Skan needing to be explicitly declared. See push_file_scope. */ 2845169699Skan if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1]))) 2846169699Skan { 2847169699Skan TREE_CHAIN (decl) = visible_builtins; 2848169699Skan visible_builtins = decl; 2849169699Skan } 2850169699Skan 285190075Sobrien /* Possibly apply some default attributes to this built-in function. */ 2852117421Skan if (attrs) 2853117421Skan decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN); 2854117421Skan else 2855117421Skan decl_attributes (&decl, NULL_TREE, 0); 285690075Sobrien 285718334Speter return decl; 285818334Speter} 285918334Speter 286018334Speter/* Called when a declaration is seen that contains no names to declare. 286118334Speter If its type is a reference to a structure, union or enum inherited 286218334Speter from a containing scope, shadow that tag name for the current scope 286318334Speter with a forward reference. 286418334Speter If its type defines a new named structure or union 286518334Speter or defines an enum, it is valid but we need not do anything here. 286618334Speter Otherwise, it is an error. */ 286718334Speter 286818334Spetervoid 2869169699Skanshadow_tag (const struct c_declspecs *declspecs) 287018334Speter{ 287118334Speter shadow_tag_warned (declspecs, 0); 287218334Speter} 287318334Speter 2874169699Skan/* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning, 2875169699Skan but no pedwarn. */ 287618334Spetervoid 2877169699Skanshadow_tag_warned (const struct c_declspecs *declspecs, int warned) 287818334Speter{ 2879169699Skan bool found_tag = false; 288018334Speter 2881169699Skan if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p) 288218334Speter { 2883169699Skan tree value = declspecs->type; 288490075Sobrien enum tree_code code = TREE_CODE (value); 288518334Speter 288618334Speter if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE) 288718334Speter /* Used to test also that TYPE_SIZE (value) != 0. 288818334Speter That caused warning for `struct foo;' at top level in the file. */ 288918334Speter { 2890132730Skan tree name = TYPE_NAME (value); 289190075Sobrien tree t; 289218334Speter 2893169699Skan found_tag = true; 289418334Speter 289518334Speter if (name == 0) 289618334Speter { 289718334Speter if (warned != 1 && code != ENUMERAL_TYPE) 289818334Speter /* Empty unnamed enum OK */ 289918334Speter { 290018334Speter pedwarn ("unnamed struct/union that defines no instances"); 290118334Speter warned = 1; 290218334Speter } 290318334Speter } 2904169699Skan else if (!declspecs->tag_defined_p 2905169699Skan && declspecs->storage_class != csc_none) 2906169699Skan { 2907169699Skan if (warned != 1) 2908169699Skan pedwarn ("empty declaration with storage class specifier " 2909169699Skan "does not redeclare tag"); 2910169699Skan warned = 1; 2911169699Skan pending_xref_error (); 2912169699Skan } 2913169699Skan else if (!declspecs->tag_defined_p 2914169699Skan && (declspecs->const_p 2915169699Skan || declspecs->volatile_p 2916169699Skan || declspecs->restrict_p)) 2917169699Skan { 2918169699Skan if (warned != 1) 2919169699Skan pedwarn ("empty declaration with type qualifier " 2920169699Skan "does not redeclare tag"); 2921169699Skan warned = 1; 2922169699Skan pending_xref_error (); 2923169699Skan } 292418334Speter else 292518334Speter { 2926169699Skan pending_invalid_xref = 0; 2927132730Skan t = lookup_tag (code, name, 1); 292818334Speter 292918334Speter if (t == 0) 293018334Speter { 293118334Speter t = make_node (code); 293218334Speter pushtag (name, t); 293318334Speter } 293418334Speter } 293518334Speter } 293618334Speter else 293718334Speter { 2938169699Skan if (warned != 1 && !in_system_header) 293918334Speter { 2940169699Skan pedwarn ("useless type name in empty declaration"); 2941169699Skan warned = 1; 294218334Speter } 294318334Speter } 294418334Speter } 2945169699Skan else if (warned != 1 && !in_system_header && declspecs->typedef_p) 2946169699Skan { 2947169699Skan pedwarn ("useless type name in empty declaration"); 2948169699Skan warned = 1; 2949169699Skan } 295018334Speter 2951169699Skan pending_invalid_xref = 0; 295218334Speter 2953169699Skan if (declspecs->inline_p) 2954169699Skan { 2955169699Skan error ("%<inline%> in empty declaration"); 2956169699Skan warned = 1; 2957169699Skan } 2958169699Skan 2959169699Skan if (current_scope == file_scope && declspecs->storage_class == csc_auto) 2960169699Skan { 2961169699Skan error ("%<auto%> in file-scope empty declaration"); 2962169699Skan warned = 1; 2963169699Skan } 2964169699Skan 2965169699Skan if (current_scope == file_scope && declspecs->storage_class == csc_register) 2966169699Skan { 2967169699Skan error ("%<register%> in file-scope empty declaration"); 2968169699Skan warned = 1; 2969169699Skan } 2970169699Skan 2971169699Skan if (!warned && !in_system_header && declspecs->storage_class != csc_none) 2972169699Skan { 2973169699Skan warning (0, "useless storage class specifier in empty declaration"); 2974169699Skan warned = 2; 2975169699Skan } 2976169699Skan 2977169699Skan if (!warned && !in_system_header && declspecs->thread_p) 2978169699Skan { 2979169699Skan warning (0, "useless %<__thread%> in empty declaration"); 2980169699Skan warned = 2; 2981169699Skan } 2982169699Skan 2983169699Skan if (!warned && !in_system_header && (declspecs->const_p 2984169699Skan || declspecs->volatile_p 2985169699Skan || declspecs->restrict_p)) 2986169699Skan { 2987169699Skan warning (0, "useless type qualifier in empty declaration"); 2988169699Skan warned = 2; 2989169699Skan } 2990169699Skan 299118334Speter if (warned != 1) 299218334Speter { 2993169699Skan if (!found_tag) 299418334Speter pedwarn ("empty declaration"); 299518334Speter } 299618334Speter} 299718334Speter 299890075Sobrien 2999169699Skan/* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_* 3000169699Skan bits. SPECS represents declaration specifiers that the grammar 3001169699Skan only permits to contain type qualifiers and attributes. */ 3002169699Skan 3003169699Skanint 3004169699Skanquals_from_declspecs (const struct c_declspecs *specs) 300590075Sobrien{ 3006169699Skan int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0) 3007169699Skan | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0) 3008169699Skan | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)); 3009169699Skan gcc_assert (!specs->type 3010169699Skan && !specs->decl_attr 3011169699Skan && specs->typespec_word == cts_none 3012169699Skan && specs->storage_class == csc_none 3013169699Skan && !specs->typedef_p 3014169699Skan && !specs->explicit_signed_p 3015169699Skan && !specs->deprecated_p 3016169699Skan && !specs->long_p 3017169699Skan && !specs->long_long_p 3018169699Skan && !specs->short_p 3019169699Skan && !specs->signed_p 3020169699Skan && !specs->unsigned_p 3021169699Skan && !specs->complex_p 3022169699Skan && !specs->inline_p 3023169699Skan && !specs->thread_p); 3024169699Skan return quals; 3025169699Skan} 3026169699Skan 3027169699Skan/* Construct an array declarator. EXPR is the expression inside [], 3028169699Skan or NULL_TREE. QUALS are the type qualifiers inside the [] (to be 3029169699Skan applied to the pointer to which a parameter array is converted). 3030169699Skan STATIC_P is true if "static" is inside the [], false otherwise. 3031169699Skan VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified 3032169699Skan length which is nevertheless a complete type, false otherwise. The 3033169699Skan field for the contained declarator is left to be filled in by 3034169699Skan set_array_declarator_inner. */ 3035169699Skan 3036169699Skanstruct c_declarator * 3037169699Skanbuild_array_declarator (tree expr, struct c_declspecs *quals, bool static_p, 3038169699Skan bool vla_unspec_p) 3039169699Skan{ 3040169699Skan struct c_declarator *declarator = XOBNEW (&parser_obstack, 3041169699Skan struct c_declarator); 3042169699Skan declarator->kind = cdk_array; 3043169699Skan declarator->declarator = 0; 3044169699Skan declarator->u.array.dimen = expr; 3045169699Skan if (quals) 3046169699Skan { 3047169699Skan declarator->u.array.attrs = quals->attrs; 3048169699Skan declarator->u.array.quals = quals_from_declspecs (quals); 3049169699Skan } 3050169699Skan else 3051169699Skan { 3052169699Skan declarator->u.array.attrs = NULL_TREE; 3053169699Skan declarator->u.array.quals = 0; 3054169699Skan } 3055169699Skan declarator->u.array.static_p = static_p; 3056169699Skan declarator->u.array.vla_unspec_p = vla_unspec_p; 305790075Sobrien if (pedantic && !flag_isoc99) 305890075Sobrien { 3059169699Skan if (static_p || quals != NULL) 3060169699Skan pedwarn ("ISO C90 does not support %<static%> or type " 3061169699Skan "qualifiers in parameter array declarators"); 306290075Sobrien if (vla_unspec_p) 3063169699Skan pedwarn ("ISO C90 does not support %<[*]%> array declarators"); 306490075Sobrien } 306590075Sobrien if (vla_unspec_p) 3066169699Skan { 3067169699Skan if (!current_scope->parm_flag) 3068169699Skan { 3069169699Skan /* C99 6.7.5.2p4 */ 3070169699Skan error ("%<[*]%> not allowed in other than function prototype scope"); 3071169699Skan declarator->u.array.vla_unspec_p = false; 3072169699Skan return NULL; 3073169699Skan } 3074169699Skan current_scope->had_vla_unspec = true; 3075169699Skan } 3076169699Skan return declarator; 307790075Sobrien} 307890075Sobrien 3079169699Skan/* Set the contained declarator of an array declarator. DECL is the 3080169699Skan declarator, as constructed by build_array_declarator; INNER is what 3081169699Skan appears on the left of the []. ABSTRACT_P is true if it is an 3082169699Skan abstract declarator, false otherwise; this is used to reject static 3083169699Skan and type qualifiers in abstract declarators, where they are not in 3084169699Skan the C99 grammar (subject to possible change in DR#289). */ 308590075Sobrien 3086169699Skanstruct c_declarator * 3087169699Skanset_array_declarator_inner (struct c_declarator *decl, 3088169699Skan struct c_declarator *inner, bool abstract_p) 308990075Sobrien{ 3090169699Skan decl->declarator = inner; 3091169699Skan if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED 3092169699Skan || decl->u.array.attrs != NULL_TREE 3093169699Skan || decl->u.array.static_p)) 309490075Sobrien error ("static or type qualifiers in abstract declarator"); 309590075Sobrien return decl; 309690075Sobrien} 3097169699Skan 3098169699Skan/* INIT is a constructor that forms DECL's initializer. If the final 3099169699Skan element initializes a flexible array field, add the size of that 3100169699Skan initializer to DECL's size. */ 3101169699Skan 3102169699Skanstatic void 3103169699Skanadd_flexible_array_elts_to_size (tree decl, tree init) 3104169699Skan{ 3105169699Skan tree elt, type; 3106169699Skan 3107169699Skan if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init))) 3108169699Skan return; 3109169699Skan 3110169699Skan elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value; 3111169699Skan type = TREE_TYPE (elt); 3112169699Skan if (TREE_CODE (type) == ARRAY_TYPE 3113169699Skan && TYPE_SIZE (type) == NULL_TREE 3114169699Skan && TYPE_DOMAIN (type) != NULL_TREE 3115169699Skan && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE) 3116169699Skan { 3117169699Skan complete_array_type (&type, elt, false); 3118169699Skan DECL_SIZE (decl) 3119169699Skan = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type)); 3120169699Skan DECL_SIZE_UNIT (decl) 3121169699Skan = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type)); 3122169699Skan } 3123169699Skan} 312490075Sobrien 312518334Speter/* Decode a "typename", such as "int **", returning a ..._TYPE node. */ 312618334Speter 312718334Spetertree 3128169699Skangroktypename (struct c_type_name *type_name) 312918334Speter{ 3130169699Skan tree type; 3131169699Skan tree attrs = type_name->specs->attrs; 313290075Sobrien 3133169699Skan type_name->specs->attrs = NULL_TREE; 313490075Sobrien 3135169699Skan type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME, 3136169699Skan false, NULL); 313790075Sobrien 313890075Sobrien /* Apply attributes. */ 3139169699Skan decl_attributes (&type, attrs, 0); 314090075Sobrien 3141169699Skan return type; 314218334Speter} 314318334Speter 314418334Speter/* Decode a declarator in an ordinary declaration or data definition. 314518334Speter This is called as soon as the type information and variable name 314618334Speter have been parsed, before parsing the initializer if any. 314718334Speter Here we create the ..._DECL node, fill in its type, 314818334Speter and put it on the list of decls for the current context. 314918334Speter The ..._DECL node is returned as the value. 315018334Speter 315118334Speter Exception: for arrays where the length is not specified, 315218334Speter the type is left null, to be filled in by `finish_decl'. 315318334Speter 315418334Speter Function definitions do not come here; they go to start_function 315518334Speter instead. However, external and forward declarations of functions 315618334Speter do go through here. Structure field declarations are done by 315718334Speter grokfield and not through here. */ 315818334Speter 315918334Spetertree 3160169699Skanstart_decl (struct c_declarator *declarator, struct c_declspecs *declspecs, 3161169699Skan bool initialized, tree attributes) 316218334Speter{ 316390075Sobrien tree decl; 316490075Sobrien tree tem; 3165132730Skan 316690075Sobrien /* An object declared as __attribute__((deprecated)) suppresses 316790075Sobrien warnings of uses of other deprecated items. */ 316890075Sobrien if (lookup_attribute ("deprecated", attributes)) 316990075Sobrien deprecated_state = DEPRECATED_SUPPRESS; 317018334Speter 317190075Sobrien decl = grokdeclarator (declarator, declspecs, 3172132730Skan NORMAL, initialized, NULL); 3173169699Skan if (!decl) 3174169699Skan return 0; 3175132730Skan 317690075Sobrien deprecated_state = DEPRECATED_NORMAL; 317718334Speter 317890075Sobrien if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL 317990075Sobrien && MAIN_NAME_P (DECL_NAME (decl))) 3180169699Skan warning (OPT_Wmain, "%q+D is usually a function", decl); 318150397Sobrien 318218334Speter if (initialized) 318318334Speter /* Is it valid for this decl to have an initializer at all? 318418334Speter If not, set INITIALIZED to zero, which will indirectly 3185169699Skan tell 'finish_decl' to ignore the initializer once it is parsed. */ 318618334Speter switch (TREE_CODE (decl)) 318718334Speter { 318818334Speter case TYPE_DECL: 3189169699Skan error ("typedef %qD is initialized (use __typeof__ instead)", decl); 3190104761Skan initialized = 0; 319118334Speter break; 319218334Speter 319318334Speter case FUNCTION_DECL: 3194169699Skan error ("function %qD is initialized like a variable", decl); 319518334Speter initialized = 0; 319618334Speter break; 319718334Speter 319818334Speter case PARM_DECL: 319918334Speter /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */ 3200169699Skan error ("parameter %qD is initialized", decl); 320118334Speter initialized = 0; 320218334Speter break; 320318334Speter 320418334Speter default: 3205169699Skan /* Don't allow initializations for incomplete types except for 3206169699Skan arrays which might be completed by the initialization. */ 320790075Sobrien 3208169699Skan /* This can happen if the array size is an undefined macro. 3209169699Skan We already gave a warning, so we don't need another one. */ 321090075Sobrien if (TREE_TYPE (decl) == error_mark_node) 321190075Sobrien initialized = 0; 321290075Sobrien else if (COMPLETE_TYPE_P (TREE_TYPE (decl))) 321318334Speter { 321418334Speter /* A complete type is ok if size is fixed. */ 321518334Speter 321618334Speter if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST 321718334Speter || C_DECL_VARIABLE_SIZE (decl)) 321818334Speter { 321918334Speter error ("variable-sized object may not be initialized"); 322018334Speter initialized = 0; 322118334Speter } 322218334Speter } 322318334Speter else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE) 322418334Speter { 3225169699Skan error ("variable %qD has initializer but incomplete type", decl); 322618334Speter initialized = 0; 322718334Speter } 3228169699Skan else if (C_DECL_VARIABLE_SIZE (decl)) 322918334Speter { 3230169699Skan /* Although C99 is unclear about whether incomplete arrays 3231169699Skan of VLAs themselves count as VLAs, it does not make 3232169699Skan sense to permit them to be initialized given that 3233169699Skan ordinary VLAs may not be initialized. */ 3234169699Skan error ("variable-sized object may not be initialized"); 323518334Speter initialized = 0; 323618334Speter } 323718334Speter } 323818334Speter 323918334Speter if (initialized) 324018334Speter { 3241169699Skan if (current_scope == file_scope) 324218334Speter TREE_STATIC (decl) = 1; 324318334Speter 3244169699Skan /* Tell 'pushdecl' this is an initialized decl 324518334Speter even though we don't yet have the initializer expression. 3246169699Skan Also tell 'finish_decl' it may store the real initializer. */ 324718334Speter DECL_INITIAL (decl) = error_mark_node; 324818334Speter } 324918334Speter 325018334Speter /* If this is a function declaration, write a record describing it to the 325118334Speter prototypes file (if requested). */ 325218334Speter 325318334Speter if (TREE_CODE (decl) == FUNCTION_DECL) 325418334Speter gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0); 325518334Speter 325650397Sobrien /* ANSI specifies that a tentative definition which is not merged with 325750397Sobrien a non-tentative definition behaves exactly like a definition with an 325850397Sobrien initializer equal to zero. (Section 3.7.2) 3259117421Skan 3260117421Skan -fno-common gives strict ANSI behavior, though this tends to break 3261117421Skan a large body of code that grew up without this rule. 3262117421Skan 3263117421Skan Thread-local variables are never common, since there's no entrenched 3264117421Skan body of code to break, and it allows more efficient variable references 3265132730Skan in the presence of dynamic linking. */ 3266117421Skan 3267117421Skan if (TREE_CODE (decl) == VAR_DECL 3268117421Skan && !initialized 3269117421Skan && TREE_PUBLIC (decl) 3270169699Skan && !DECL_THREAD_LOCAL_P (decl) 3271117421Skan && !flag_no_common) 327250397Sobrien DECL_COMMON (decl) = 1; 327318334Speter 327418334Speter /* Set attributes here so if duplicate decl, will have proper attributes. */ 327590075Sobrien decl_attributes (&decl, attributes, 0); 327618334Speter 3277132730Skan if (TREE_CODE (decl) == FUNCTION_DECL 3278132730Skan && targetm.calls.promote_prototypes (TREE_TYPE (decl))) 3279132730Skan { 3280169699Skan struct c_declarator *ce = declarator; 328196263Sobrien 3282169699Skan if (ce->kind == cdk_pointer) 3283169699Skan ce = declarator->declarator; 3284169699Skan if (ce->kind == cdk_function) 3285132730Skan { 3286169699Skan tree args = ce->u.arg_info->parms; 3287132730Skan for (; args; args = TREE_CHAIN (args)) 3288132730Skan { 3289132730Skan tree type = TREE_TYPE (args); 3290169699Skan if (type && INTEGRAL_TYPE_P (type) 3291132730Skan && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 3292132730Skan DECL_ARG_TYPE (args) = integer_type_node; 3293132730Skan } 3294132730Skan } 3295132730Skan } 3296132730Skan 329790075Sobrien if (TREE_CODE (decl) == FUNCTION_DECL 329890075Sobrien && DECL_DECLARED_INLINE_P (decl) 329990075Sobrien && DECL_UNINLINABLE (decl) 330090075Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) 3301169699Skan warning (OPT_Wattributes, "inline function %q+D given attribute noinline", 3302169699Skan decl); 330390075Sobrien 3304132730Skan /* Add this decl to the current scope. 330518334Speter TEM may equal DECL or it may be a previous decl of the same name. */ 330618334Speter tem = pushdecl (decl); 330718334Speter 3308169699Skan if (initialized && DECL_EXTERNAL (tem)) 330918334Speter { 3310169699Skan DECL_EXTERNAL (tem) = 0; 3311169699Skan TREE_STATIC (tem) = 1; 331218334Speter } 331318334Speter 331418334Speter return tem; 331518334Speter} 331618334Speter 3317169699Skan/* Initialize EH if not initialized yet and exceptions are enabled. */ 3318169699Skan 3319169699Skanvoid 3320169699Skanc_maybe_initialize_eh (void) 3321169699Skan{ 3322169699Skan if (!flag_exceptions || c_eh_initialized_p) 3323169699Skan return; 3324169699Skan 3325169699Skan c_eh_initialized_p = true; 3326169699Skan eh_personality_libfunc 3327169699Skan = init_one_libfunc (USING_SJLJ_EXCEPTIONS 3328169699Skan ? "__gcc_personality_sj0" 3329169699Skan : "__gcc_personality_v0"); 3330169699Skan default_init_unwind_resume_libfunc (); 3331169699Skan using_eh_for_cleanups (); 3332169699Skan} 3333169699Skan 333418334Speter/* Finish processing of a declaration; 333518334Speter install its initial value. 333618334Speter If the length of an array type is not known before, 333718334Speter it must be determined now, from the initial value, or it is an error. */ 333818334Speter 333918334Spetervoid 3340132730Skanfinish_decl (tree decl, tree init, tree asmspec_tree) 334118334Speter{ 3342169699Skan tree type; 334318334Speter int was_incomplete = (DECL_SIZE (decl) == 0); 334490075Sobrien const char *asmspec = 0; 334518334Speter 334690075Sobrien /* If a name was specified, get the string. */ 3347169699Skan if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL) 3348169699Skan && DECL_FILE_SCOPE_P (decl)) 334996263Sobrien asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); 335018334Speter if (asmspec_tree) 335118334Speter asmspec = TREE_STRING_POINTER (asmspec_tree); 335218334Speter 335318334Speter /* If `start_decl' didn't like having an initialization, ignore it now. */ 335418334Speter if (init != 0 && DECL_INITIAL (decl) == 0) 335518334Speter init = 0; 3356132730Skan 335718334Speter /* Don't crash if parm is initialized. */ 335818334Speter if (TREE_CODE (decl) == PARM_DECL) 335918334Speter init = 0; 336018334Speter 336118334Speter if (init) 3362104761Skan store_init_value (decl, init); 336318334Speter 3364132730Skan if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL 3365169699Skan || TREE_CODE (decl) == FUNCTION_DECL 3366169699Skan || TREE_CODE (decl) == FIELD_DECL)) 3367132730Skan objc_check_decl (decl); 3368132730Skan 3369169699Skan type = TREE_TYPE (decl); 3370169699Skan 3371132730Skan /* Deduce size of array from initialization, if not already known. */ 337218334Speter if (TREE_CODE (type) == ARRAY_TYPE 337318334Speter && TYPE_DOMAIN (type) == 0 337418334Speter && TREE_CODE (decl) != TYPE_DECL) 337518334Speter { 3376169699Skan bool do_default 337718334Speter = (TREE_STATIC (decl) 337818334Speter /* Even if pedantic, an external linkage array 337918334Speter may have incomplete type at first. */ 338018334Speter ? pedantic && !TREE_PUBLIC (decl) 338118334Speter : !DECL_EXTERNAL (decl)); 338218334Speter int failure 3383169699Skan = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl), 3384169699Skan do_default); 338518334Speter 338618334Speter /* Get the completed type made by complete_array_type. */ 338718334Speter type = TREE_TYPE (decl); 338818334Speter 3389169699Skan switch (failure) 3390169699Skan { 3391169699Skan case 1: 3392169699Skan error ("initializer fails to determine size of %q+D", decl); 3393169699Skan break; 339418334Speter 3395169699Skan case 2: 339618334Speter if (do_default) 3397169699Skan error ("array size missing in %q+D", decl); 339818334Speter /* If a `static' var's size isn't known, 339918334Speter make it extern as well as static, so it does not get 340018334Speter allocated. 340118334Speter If it is not `static', then do not mark extern; 340218334Speter finish_incomplete_decl will give it a default size 340318334Speter and it will get allocated. */ 3404169699Skan else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) 340518334Speter DECL_EXTERNAL (decl) = 1; 3406169699Skan break; 3407169699Skan 3408169699Skan case 3: 3409169699Skan error ("zero or negative size array %q+D", decl); 3410169699Skan break; 3411169699Skan 3412169699Skan case 0: 3413169699Skan /* For global variables, update the copy of the type that 3414169699Skan exists in the binding. */ 3415169699Skan if (TREE_PUBLIC (decl)) 3416169699Skan { 3417169699Skan struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl)); 3418169699Skan while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext)) 3419169699Skan b_ext = b_ext->shadowed; 3420169699Skan if (b_ext) 3421169699Skan { 3422169699Skan if (b_ext->type) 3423169699Skan b_ext->type = composite_type (b_ext->type, type); 3424169699Skan else 3425169699Skan b_ext->type = type; 3426169699Skan } 3427169699Skan } 3428169699Skan break; 3429169699Skan 3430169699Skan default: 3431169699Skan gcc_unreachable (); 343218334Speter } 343318334Speter 3434169699Skan if (DECL_INITIAL (decl)) 3435169699Skan TREE_TYPE (DECL_INITIAL (decl)) = type; 343618334Speter 343718334Speter layout_decl (decl, 0); 343818334Speter } 343918334Speter 344018334Speter if (TREE_CODE (decl) == VAR_DECL) 344118334Speter { 3442169699Skan if (init && TREE_CODE (init) == CONSTRUCTOR) 3443169699Skan add_flexible_array_elts_to_size (decl, init); 3444169699Skan 344590075Sobrien if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node 344690075Sobrien && COMPLETE_TYPE_P (TREE_TYPE (decl))) 344718334Speter layout_decl (decl, 0); 344818334Speter 344918334Speter if (DECL_SIZE (decl) == 0 345090075Sobrien /* Don't give an error if we already gave one earlier. */ 345190075Sobrien && TREE_TYPE (decl) != error_mark_node 345218334Speter && (TREE_STATIC (decl) 3453169699Skan /* A static variable with an incomplete type 3454169699Skan is an error if it is initialized. 3455169699Skan Also if it is not file scope. 3456169699Skan Otherwise, let it through, but if it is not `extern' 3457169699Skan then it may cause an error message later. */ 3458169699Skan ? (DECL_INITIAL (decl) != 0 3459132730Skan || !DECL_FILE_SCOPE_P (decl)) 3460169699Skan /* An automatic variable with an incomplete type 3461169699Skan is an error. */ 3462169699Skan : !DECL_EXTERNAL (decl))) 3463169699Skan { 3464169699Skan error ("storage size of %q+D isn%'t known", decl); 3465169699Skan TREE_TYPE (decl) = error_mark_node; 3466169699Skan } 346718334Speter 346818334Speter if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 346918334Speter && DECL_SIZE (decl) != 0) 347018334Speter { 347118334Speter if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) 347218334Speter constant_expression_warning (DECL_SIZE (decl)); 347318334Speter else 3474169699Skan error ("storage size of %q+D isn%'t constant", decl); 347518334Speter } 347650397Sobrien 347790075Sobrien if (TREE_USED (type)) 347850397Sobrien TREE_USED (decl) = 1; 347918334Speter } 348018334Speter 3481132730Skan /* If this is a function and an assembler name is specified, reset DECL_RTL 3482132730Skan so we can give it its new name. Also, update built_in_decls if it 3483132730Skan was a normal built-in. */ 348418334Speter if (TREE_CODE (decl) == FUNCTION_DECL && asmspec) 348590075Sobrien { 3486132730Skan if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) 3487169699Skan set_builtin_user_assembler_name (decl, asmspec); 3488169699Skan set_user_assembler_name (decl, asmspec); 348990075Sobrien } 349018334Speter 3491132730Skan /* If #pragma weak was used, mark the decl weak now. */ 3492169699Skan maybe_apply_pragma_weak (decl); 3493132730Skan 349418334Speter /* Output the assembler code and/or RTL code for variables and functions, 349518334Speter unless the type is an undefined structure or union. 349618334Speter If not, it will get done when the type is completed. */ 349718334Speter 349818334Speter if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL) 349918334Speter { 3500169699Skan /* Determine the ELF visibility. */ 3501169699Skan if (TREE_PUBLIC (decl)) 3502169699Skan c_determine_visibility (decl); 3503169699Skan 3504117421Skan /* This is a no-op in c-lang.c or something real in objc-act.c. */ 3505132730Skan if (c_dialect_objc ()) 3506117421Skan objc_check_decl (decl); 350790075Sobrien 3508169699Skan if (asmspec) 3509169699Skan { 3510169699Skan /* If this is not a static variable, issue a warning. 3511169699Skan It doesn't make any sense to give an ASMSPEC for an 3512169699Skan ordinary, non-register local variable. Historically, 3513169699Skan GCC has accepted -- but ignored -- the ASMSPEC in 3514169699Skan this case. */ 3515169699Skan if (!DECL_FILE_SCOPE_P (decl) 3516169699Skan && TREE_CODE (decl) == VAR_DECL 3517169699Skan && !C_DECL_REGISTER (decl) 3518169699Skan && !TREE_STATIC (decl)) 3519169699Skan warning (0, "ignoring asm-specifier for non-static local " 3520169699Skan "variable %q+D", decl); 3521169699Skan else 3522169699Skan set_user_assembler_name (decl, asmspec); 3523169699Skan } 3524169699Skan 3525132730Skan if (DECL_FILE_SCOPE_P (decl)) 352618334Speter { 352790075Sobrien if (DECL_INITIAL (decl) == NULL_TREE 352890075Sobrien || DECL_INITIAL (decl) == error_mark_node) 352990075Sobrien /* Don't output anything 353090075Sobrien when a tentative file-scope definition is seen. 353190075Sobrien But at end of compilation, do output code for them. */ 353290075Sobrien DECL_DEFER_OUTPUT (decl) = 1; 3533169699Skan rest_of_decl_compilation (decl, true, 0); 353418334Speter } 353518334Speter else 353618334Speter { 3537169699Skan /* In conjunction with an ASMSPEC, the `register' 3538169699Skan keyword indicates that we should place the variable 3539169699Skan in a particular register. */ 3540169699Skan if (asmspec && C_DECL_REGISTER (decl)) 354190075Sobrien { 3542169699Skan DECL_HARD_REGISTER (decl) = 1; 3543169699Skan /* This cannot be done for a structure with volatile 3544169699Skan fields, on which DECL_REGISTER will have been 3545169699Skan reset. */ 3546169699Skan if (!DECL_REGISTER (decl)) 3547169699Skan error ("cannot put object with volatile field into register"); 354890075Sobrien } 354990075Sobrien 355090075Sobrien if (TREE_CODE (decl) != FUNCTION_DECL) 3551169699Skan { 3552169699Skan /* If we're building a variable sized type, and we might be 3553169699Skan reachable other than via the top of the current binding 3554169699Skan level, then create a new BIND_EXPR so that we deallocate 3555169699Skan the object at the right time. */ 3556169699Skan /* Note that DECL_SIZE can be null due to errors. */ 3557169699Skan if (DECL_SIZE (decl) 3558169699Skan && !TREE_CONSTANT (DECL_SIZE (decl)) 3559169699Skan && STATEMENT_LIST_HAS_LABEL (cur_stmt_list)) 3560169699Skan { 3561169699Skan tree bind; 3562169699Skan bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL); 3563169699Skan TREE_SIDE_EFFECTS (bind) = 1; 3564169699Skan add_stmt (bind); 3565169699Skan BIND_EXPR_BODY (bind) = push_stmt_list (); 3566169699Skan } 3567169699Skan add_stmt (build_stmt (DECL_EXPR, decl)); 3568169699Skan } 356918334Speter } 357090075Sobrien 3571169699Skan 3572132730Skan if (!DECL_FILE_SCOPE_P (decl)) 357318334Speter { 357418334Speter /* Recompute the RTL of a local array now 357518334Speter if it used to be an incomplete type. */ 357618334Speter if (was_incomplete 3577169699Skan && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) 357818334Speter { 357918334Speter /* If we used it already as memory, it must stay in memory. */ 358018334Speter TREE_ADDRESSABLE (decl) = TREE_USED (decl); 358118334Speter /* If it's still incomplete now, no init will save it. */ 358218334Speter if (DECL_SIZE (decl) == 0) 358318334Speter DECL_INITIAL (decl) = 0; 358418334Speter } 358518334Speter } 358618334Speter } 358718334Speter 3588132730Skan /* If this was marked 'used', be sure it will be output. */ 3589169699Skan if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl))) 3590169699Skan mark_decl_referenced (decl); 3591132730Skan 359218334Speter if (TREE_CODE (decl) == TYPE_DECL) 3593169699Skan { 3594169699Skan if (!DECL_FILE_SCOPE_P (decl) 3595169699Skan && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE)) 3596169699Skan add_stmt (build_stmt (DECL_EXPR, decl)); 359718334Speter 3598169699Skan rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0); 3599169699Skan } 3600169699Skan 360118334Speter /* At the end of a declaration, throw away any variable type sizes 360218334Speter of types defined inside that declaration. There is no use 360318334Speter computing them in the following function definition. */ 3604169699Skan if (current_scope == file_scope) 360518334Speter get_pending_sizes (); 360618334Speter 3607117421Skan /* Install a cleanup (aka destructor) if one was given. */ 3608117421Skan if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl)) 3609117421Skan { 3610117421Skan tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl)); 3611117421Skan if (attr) 3612117421Skan { 3613117421Skan tree cleanup_id = TREE_VALUE (TREE_VALUE (attr)); 3614117421Skan tree cleanup_decl = lookup_name (cleanup_id); 3615117421Skan tree cleanup; 3616117421Skan 3617117421Skan /* Build "cleanup(&decl)" for the destructor. */ 3618117421Skan cleanup = build_unary_op (ADDR_EXPR, decl, 0); 3619117421Skan cleanup = build_tree_list (NULL_TREE, cleanup); 3620117421Skan cleanup = build_function_call (cleanup_decl, cleanup); 3621117421Skan 3622117421Skan /* Don't warn about decl unused; the cleanup uses it. */ 3623117421Skan TREE_USED (decl) = 1; 3624169699Skan TREE_USED (cleanup_decl) = 1; 3625117421Skan 3626117421Skan /* Initialize EH, if we've been told to do so. */ 3627169699Skan c_maybe_initialize_eh (); 3628117421Skan 3629169699Skan push_cleanup (decl, cleanup, false); 3630117421Skan } 3631117421Skan } 363218334Speter} 363318334Speter 3634169699Skan/* Given a parsed parameter declaration, decode it into a PARM_DECL. */ 3635169699Skan 3636169699Skantree 3637169699Skangrokparm (const struct c_parm *parm) 3638169699Skan{ 3639169699Skan tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, 3640169699Skan NULL); 3641169699Skan 3642169699Skan decl_attributes (&decl, parm->attrs, 0); 3643169699Skan 3644169699Skan return decl; 3645169699Skan} 3646169699Skan 3647132730Skan/* Given a parsed parameter declaration, decode it into a PARM_DECL 3648132730Skan and push that on the current scope. */ 364918334Speter 365018334Spetervoid 3651169699Skanpush_parm_decl (const struct c_parm *parm) 365218334Speter{ 365318334Speter tree decl; 3654132730Skan 3655169699Skan decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL); 3656169699Skan decl_attributes (&decl, parm->attrs, 0); 365718334Speter 365818334Speter decl = pushdecl (decl); 365918334Speter 3660132730Skan finish_decl (decl, NULL_TREE, NULL_TREE); 366118334Speter} 366218334Speter 3663169699Skan/* Mark all the parameter declarations to date as forward decls. 3664132730Skan Also diagnose use of this extension. */ 366518334Speter 366618334Spetervoid 3667132730Skanmark_forward_parm_decls (void) 366818334Speter{ 3669169699Skan struct c_binding *b; 3670132730Skan 3671132730Skan if (pedantic && !current_scope->warned_forward_parm_decls) 3672132730Skan { 3673132730Skan pedwarn ("ISO C forbids forward parameter declarations"); 3674132730Skan current_scope->warned_forward_parm_decls = true; 3675132730Skan } 3676132730Skan 3677169699Skan for (b = current_scope->bindings; b; b = b->prev) 3678169699Skan if (TREE_CODE (b->decl) == PARM_DECL) 3679169699Skan TREE_ASM_WRITTEN (b->decl) = 1; 368018334Speter} 368118334Speter 368290075Sobrien/* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound 368390075Sobrien literal, which may be an incomplete array type completed by the 368490075Sobrien initializer; INIT is a CONSTRUCTOR that initializes the compound 368590075Sobrien literal. */ 368690075Sobrien 368790075Sobrientree 3688132730Skanbuild_compound_literal (tree type, tree init) 368990075Sobrien{ 369090075Sobrien /* We do not use start_decl here because we have a type, not a declarator; 369190075Sobrien and do not use finish_decl because the decl should be stored inside 3692169699Skan the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */ 3693169699Skan tree decl; 369490075Sobrien tree complit; 369590075Sobrien tree stmt; 3696169699Skan 3697169699Skan if (type == error_mark_node) 3698169699Skan return error_mark_node; 3699169699Skan 3700169699Skan decl = build_decl (VAR_DECL, NULL_TREE, type); 370190075Sobrien DECL_EXTERNAL (decl) = 0; 370290075Sobrien TREE_PUBLIC (decl) = 0; 3703169699Skan TREE_STATIC (decl) = (current_scope == file_scope); 370490075Sobrien DECL_CONTEXT (decl) = current_function_decl; 370590075Sobrien TREE_USED (decl) = 1; 370690075Sobrien TREE_TYPE (decl) = type; 3707169699Skan TREE_READONLY (decl) = TYPE_READONLY (type); 370890075Sobrien store_init_value (decl, init); 370990075Sobrien 371090075Sobrien if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type)) 371190075Sobrien { 3712169699Skan int failure = complete_array_type (&TREE_TYPE (decl), 3713169699Skan DECL_INITIAL (decl), true); 3714169699Skan gcc_assert (!failure); 3715169699Skan 3716169699Skan type = TREE_TYPE (decl); 3717169699Skan TREE_TYPE (DECL_INITIAL (decl)) = type; 371890075Sobrien } 371990075Sobrien 372090075Sobrien if (type == error_mark_node || !COMPLETE_TYPE_P (type)) 372190075Sobrien return error_mark_node; 372290075Sobrien 3723169699Skan stmt = build_stmt (DECL_EXPR, decl); 3724169699Skan complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt); 372590075Sobrien TREE_SIDE_EFFECTS (complit) = 1; 372690075Sobrien 372790075Sobrien layout_decl (decl, 0); 372890075Sobrien 372990075Sobrien if (TREE_STATIC (decl)) 373090075Sobrien { 3731169699Skan /* This decl needs a name for the assembler output. */ 3732169699Skan set_compound_literal_name (decl); 3733102790Skan DECL_DEFER_OUTPUT (decl) = 1; 3734102790Skan DECL_COMDAT (decl) = 1; 3735102790Skan DECL_ARTIFICIAL (decl) = 1; 3736169699Skan DECL_IGNORED_P (decl) = 1; 3737102790Skan pushdecl (decl); 3738169699Skan rest_of_decl_compilation (decl, 1, 0); 373990075Sobrien } 374090075Sobrien 374190075Sobrien return complit; 374290075Sobrien} 374390075Sobrien 3744117421Skan/* Determine whether TYPE is a structure with a flexible array member, 3745117421Skan or a union containing such a structure (possibly recursively). */ 3746117421Skan 3747117421Skanstatic bool 3748132730Skanflexible_array_type_p (tree type) 3749117421Skan{ 3750117421Skan tree x; 3751117421Skan switch (TREE_CODE (type)) 3752117421Skan { 3753117421Skan case RECORD_TYPE: 3754117421Skan x = TYPE_FIELDS (type); 3755117421Skan if (x == NULL_TREE) 3756117421Skan return false; 3757117421Skan while (TREE_CHAIN (x) != NULL_TREE) 3758117421Skan x = TREE_CHAIN (x); 3759117421Skan if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 3760117421Skan && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 3761117421Skan && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 3762117421Skan && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 3763117421Skan return true; 3764117421Skan return false; 3765117421Skan case UNION_TYPE: 3766117421Skan for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x)) 3767117421Skan { 3768117421Skan if (flexible_array_type_p (TREE_TYPE (x))) 3769117421Skan return true; 3770117421Skan } 3771117421Skan return false; 3772117421Skan default: 3773117421Skan return false; 3774117421Skan } 3775117421Skan} 3776117421Skan 3777132730Skan/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME, 3778132730Skan replacing with appropriate values if they are invalid. */ 3779132730Skanstatic void 3780132730Skancheck_bitfield_type_and_width (tree *type, tree *width, const char *orig_name) 3781132730Skan{ 3782132730Skan tree type_mv; 3783132730Skan unsigned int max_width; 3784132730Skan unsigned HOST_WIDE_INT w; 3785132730Skan const char *name = orig_name ? orig_name: _("<anonymous>"); 3786132730Skan 3787132730Skan /* Detect and ignore out of range field width and process valid 3788132730Skan field widths. */ 3789169699Skan if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)) 3790169699Skan || TREE_CODE (*width) != INTEGER_CST) 3791132730Skan { 3792169699Skan error ("bit-field %qs width not an integer constant", name); 3793132730Skan *width = integer_one_node; 3794132730Skan } 3795132730Skan else 3796132730Skan { 3797132730Skan constant_expression_warning (*width); 3798132730Skan if (tree_int_cst_sgn (*width) < 0) 3799132730Skan { 3800169699Skan error ("negative width in bit-field %qs", name); 3801132730Skan *width = integer_one_node; 3802132730Skan } 3803132730Skan else if (integer_zerop (*width) && orig_name) 3804132730Skan { 3805169699Skan error ("zero width for bit-field %qs", name); 3806132730Skan *width = integer_one_node; 3807132730Skan } 3808132730Skan } 3809132730Skan 3810132730Skan /* Detect invalid bit-field type. */ 3811132730Skan if (TREE_CODE (*type) != INTEGER_TYPE 3812132730Skan && TREE_CODE (*type) != BOOLEAN_TYPE 3813132730Skan && TREE_CODE (*type) != ENUMERAL_TYPE) 3814132730Skan { 3815169699Skan error ("bit-field %qs has invalid type", name); 3816132730Skan *type = unsigned_type_node; 3817132730Skan } 3818132730Skan 3819132730Skan type_mv = TYPE_MAIN_VARIANT (*type); 3820132730Skan if (pedantic 3821169699Skan && !in_system_header 3822132730Skan && type_mv != integer_type_node 3823132730Skan && type_mv != unsigned_type_node 3824132730Skan && type_mv != boolean_type_node) 3825169699Skan pedwarn ("type of bit-field %qs is a GCC extension", name); 3826132730Skan 3827132730Skan if (type_mv == boolean_type_node) 3828132730Skan max_width = CHAR_TYPE_SIZE; 3829132730Skan else 3830132730Skan max_width = TYPE_PRECISION (*type); 3831132730Skan 3832132730Skan if (0 < compare_tree_int (*width, max_width)) 3833132730Skan { 3834169699Skan error ("width of %qs exceeds its type", name); 3835132730Skan w = max_width; 3836169699Skan *width = build_int_cst (NULL_TREE, w); 3837132730Skan } 3838132730Skan else 3839132730Skan w = tree_low_cst (*width, 1); 3840132730Skan 3841169699Skan if (TREE_CODE (*type) == ENUMERAL_TYPE) 3842169699Skan { 3843169699Skan struct lang_type *lt = TYPE_LANG_SPECIFIC (*type); 3844169699Skan if (!lt 3845169699Skan || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type)) 3846169699Skan || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type))) 3847169699Skan warning (0, "%qs is narrower than values of its type", name); 3848169699Skan } 3849132730Skan} 3850169699Skan 3851132730Skan 385218334Speter/* Given declspecs and a declarator, 385318334Speter determine the name and type of the object declared 385418334Speter and construct a ..._DECL node for it. 385518334Speter (In one case we can return a ..._TYPE node instead. 385618334Speter For invalid input we sometimes return 0.) 385718334Speter 3858169699Skan DECLSPECS is a c_declspecs structure for the declaration specifiers. 385918334Speter 386018334Speter DECL_CONTEXT says which syntactic context this declaration is in: 386118334Speter NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. 386218334Speter FUNCDEF for a function definition. Like NORMAL but a few different 386318334Speter error messages in each case. Return value may be zero meaning 386418334Speter this definition is too screwy to try to parse. 386518334Speter PARM for a parameter declaration (either within a function prototype 386618334Speter or before a function body). Make a PARM_DECL, or return void_type_node. 386718334Speter TYPENAME if for a typename (in a cast or sizeof). 386818334Speter Don't make a DECL node; just return the ..._TYPE node. 386918334Speter FIELD for a struct or union field; make a FIELD_DECL. 3870169699Skan INITIALIZED is true if the decl has an initializer. 3871132730Skan WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node 3872132730Skan representing the width of the bit-field. 387318334Speter 387418334Speter In the TYPENAME case, DECLARATOR is really an absolute declarator. 387518334Speter It may also be so in the PARM case, for a prototype where the 387618334Speter argument type is specified but not the name. 387718334Speter 387818334Speter This function is where the complicated C meanings of `static' 387918334Speter and `extern' are interpreted. */ 388018334Speter 388118334Speterstatic tree 3882169699Skangrokdeclarator (const struct c_declarator *declarator, 3883169699Skan struct c_declspecs *declspecs, 3884169699Skan enum decl_context decl_context, bool initialized, tree *width) 388518334Speter{ 3886169699Skan tree type = declspecs->type; 3887169699Skan bool threadp = declspecs->thread_p; 3888169699Skan enum c_storage_class storage_class = declspecs->storage_class; 388918334Speter int constp; 389052284Sobrien int restrictp; 389118334Speter int volatilep; 389252284Sobrien int type_quals = TYPE_UNQUALIFIED; 3893132730Skan const char *name, *orig_name; 389418334Speter tree typedef_type = 0; 3895169699Skan bool funcdef_flag = false; 3896169699Skan bool funcdef_syntax = false; 389718334Speter int size_varies = 0; 3898169699Skan tree decl_attr = declspecs->decl_attr; 3899169699Skan int array_ptr_quals = TYPE_UNQUALIFIED; 3900169699Skan tree array_ptr_attrs = NULL_TREE; 390190075Sobrien int array_parm_static = 0; 3902169699Skan bool array_parm_vla_unspec_p = false; 390390075Sobrien tree returned_attrs = NULL_TREE; 3904132730Skan bool bitfield = width != NULL; 3905132730Skan tree element_type; 3906169699Skan struct c_arg_info *arg_info = 0; 390718334Speter 390818334Speter if (decl_context == FUNCDEF) 3909169699Skan funcdef_flag = true, decl_context = NORMAL; 391018334Speter 391118334Speter /* Look inside a declarator for the name being declared 391218334Speter and get it as a string, for an error message. */ 391318334Speter { 3914169699Skan const struct c_declarator *decl = declarator; 391518334Speter name = 0; 391618334Speter 391718334Speter while (decl) 3918169699Skan switch (decl->kind) 391918334Speter { 3920169699Skan case cdk_function: 3921169699Skan case cdk_array: 3922169699Skan case cdk_pointer: 3923169699Skan funcdef_syntax = (decl->kind == cdk_function); 3924169699Skan decl = decl->declarator; 392518334Speter break; 392618334Speter 3927169699Skan case cdk_attrs: 3928169699Skan decl = decl->declarator; 392990075Sobrien break; 393090075Sobrien 3931169699Skan case cdk_id: 3932169699Skan if (decl->u.id) 3933169699Skan name = IDENTIFIER_POINTER (decl->u.id); 393418334Speter decl = 0; 393518334Speter break; 393618334Speter 393718334Speter default: 3938169699Skan gcc_unreachable (); 393918334Speter } 3940132730Skan orig_name = name; 394118334Speter if (name == 0) 394218334Speter name = "type name"; 394318334Speter } 394418334Speter 394518334Speter /* A function definition's declarator must have the form of 394618334Speter a function declarator. */ 394718334Speter 3948169699Skan if (funcdef_flag && !funcdef_syntax) 394918334Speter return 0; 395018334Speter 395118334Speter /* If this looks like a function definition, make it one, 395218334Speter even if it occurs where parms are expected. 395318334Speter Then store_parm_decls will reject it and not use it as a parm. */ 3954169699Skan if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag) 395518334Speter decl_context = PARM; 395618334Speter 3957169699Skan if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS) 3958169699Skan warn_deprecated_use (declspecs->type); 395918334Speter 3960169699Skan if ((decl_context == NORMAL || decl_context == FIELD) 3961169699Skan && current_scope == file_scope 3962169699Skan && variably_modified_type_p (type, NULL_TREE)) 396318334Speter { 3964169699Skan error ("variably modified %qs at file scope", name); 3965169699Skan type = integer_type_node; 396618334Speter } 396718334Speter 396818334Speter typedef_type = type; 3969169699Skan size_varies = C_TYPE_VARIABLE_SIZE (type); 397018334Speter 3971169699Skan /* Diagnose defaulting to "int". */ 397218334Speter 3973169699Skan if (declspecs->default_int_p && !in_system_header) 397418334Speter { 3975169699Skan /* Issue a warning if this is an ISO C 99 program or if 3976169699Skan -Wreturn-type and this is a function, or if -Wimplicit; 3977169699Skan prefer the former warning since it is more explicit. */ 3978169699Skan if ((warn_implicit_int || warn_return_type || flag_isoc99) 3979169699Skan && funcdef_flag) 3980169699Skan warn_about_return_type = 1; 3981169699Skan else if (warn_implicit_int || flag_isoc99) 3982169699Skan pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name); 398318334Speter } 398418334Speter 3985169699Skan /* Adjust the type if a bit-field is being declared, 3986169699Skan -funsigned-bitfields applied and the type is not explicitly 3987169699Skan "signed". */ 3988169699Skan if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p 3989169699Skan && TREE_CODE (type) == INTEGER_TYPE) 3990169699Skan type = c_common_unsigned_type (type); 399118334Speter 399252284Sobrien /* Figure out the type qualifiers for the declaration. There are 399352284Sobrien two ways a declaration can become qualified. One is something 399452284Sobrien like `const int i' where the `const' is explicit. Another is 399552284Sobrien something like `typedef const int CI; CI i' where the type of the 3996132730Skan declaration contains the `const'. A third possibility is that 3997132730Skan there is a type qualifier on the element type of a typedefed 3998132730Skan array type, in which case we should extract that qualifier so 3999132730Skan that c_apply_type_quals_to_decls receives the full list of 4000132730Skan qualifiers to work with (C90 is not entirely clear about whether 4001132730Skan duplicate qualifiers should be diagnosed in this case, but it 4002132730Skan seems most appropriate to do so). */ 4003132730Skan element_type = strip_array_types (type); 4004169699Skan constp = declspecs->const_p + TYPE_READONLY (element_type); 4005169699Skan restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type); 4006169699Skan volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type); 4007132730Skan if (pedantic && !flag_isoc99) 4008132730Skan { 4009132730Skan if (constp > 1) 4010169699Skan pedwarn ("duplicate %<const%>"); 4011132730Skan if (restrictp > 1) 4012169699Skan pedwarn ("duplicate %<restrict%>"); 4013132730Skan if (volatilep > 1) 4014169699Skan pedwarn ("duplicate %<volatile%>"); 4015132730Skan } 4016169699Skan if (!flag_gen_aux_info && (TYPE_QUALS (element_type))) 401718334Speter type = TYPE_MAIN_VARIANT (type); 401852284Sobrien type_quals = ((constp ? TYPE_QUAL_CONST : 0) 401952284Sobrien | (restrictp ? TYPE_QUAL_RESTRICT : 0) 402052284Sobrien | (volatilep ? TYPE_QUAL_VOLATILE : 0)); 402118334Speter 4022169699Skan /* Warn about storage classes that are invalid for certain 4023169699Skan kinds of declarations (parameters, typenames, etc.). */ 402418334Speter 4025169699Skan if (funcdef_flag 4026169699Skan && (threadp 4027169699Skan || storage_class == csc_auto 4028169699Skan || storage_class == csc_register 4029169699Skan || storage_class == csc_typedef)) 4030169699Skan { 4031169699Skan if (storage_class == csc_auto 4032169699Skan && (pedantic || current_scope == file_scope)) 4033169699Skan pedwarn ("function definition declared %<auto%>"); 4034169699Skan if (storage_class == csc_register) 4035169699Skan error ("function definition declared %<register%>"); 4036169699Skan if (storage_class == csc_typedef) 4037169699Skan error ("function definition declared %<typedef%>"); 4038169699Skan if (threadp) 4039169699Skan error ("function definition declared %<__thread%>"); 4040169699Skan threadp = false; 4041169699Skan if (storage_class == csc_auto 4042169699Skan || storage_class == csc_register 4043169699Skan || storage_class == csc_typedef) 4044169699Skan storage_class = csc_none; 4045169699Skan } 4046169699Skan else if (decl_context != NORMAL && (storage_class != csc_none || threadp)) 4047169699Skan { 4048169699Skan if (decl_context == PARM && storage_class == csc_register) 4049169699Skan ; 4050169699Skan else 4051169699Skan { 4052169699Skan switch (decl_context) 4053169699Skan { 4054169699Skan case FIELD: 4055169699Skan error ("storage class specified for structure field %qs", 4056169699Skan name); 4057169699Skan break; 4058169699Skan case PARM: 4059169699Skan error ("storage class specified for parameter %qs", name); 4060169699Skan break; 4061169699Skan default: 4062169699Skan error ("storage class specified for typename"); 4063169699Skan break; 4064169699Skan } 4065169699Skan storage_class = csc_none; 4066169699Skan threadp = false; 4067169699Skan } 4068169699Skan } 4069169699Skan else if (storage_class == csc_extern 4070169699Skan && initialized 4071169699Skan && !funcdef_flag) 4072169699Skan { 4073169699Skan /* 'extern' with initialization is invalid if not at file scope. */ 4074169699Skan if (current_scope == file_scope) 4075169699Skan { 4076169699Skan /* It is fine to have 'extern const' when compiling at C 4077169699Skan and C++ intersection. */ 4078169699Skan if (!(warn_cxx_compat && constp)) 4079169699Skan warning (0, "%qs initialized and declared %<extern%>", name); 4080169699Skan } 4081169699Skan else 4082169699Skan error ("%qs has both %<extern%> and initializer", name); 4083169699Skan } 4084169699Skan else if (current_scope == file_scope) 4085169699Skan { 4086169699Skan if (storage_class == csc_auto) 4087169699Skan error ("file-scope declaration of %qs specifies %<auto%>", name); 4088169699Skan if (pedantic && storage_class == csc_register) 4089169699Skan pedwarn ("file-scope declaration of %qs specifies %<register%>", name); 4090169699Skan } 4091169699Skan else 4092169699Skan { 4093169699Skan if (storage_class == csc_extern && funcdef_flag) 4094169699Skan error ("nested function %qs declared %<extern%>", name); 4095169699Skan else if (threadp && storage_class == csc_none) 4096169699Skan { 4097169699Skan error ("function-scope %qs implicitly auto and declared " 4098169699Skan "%<__thread%>", 4099169699Skan name); 4100169699Skan threadp = false; 4101169699Skan } 4102169699Skan } 410318334Speter 410418334Speter /* Now figure out the structure of the declarator proper. 410518334Speter Descend through it, creating more complex types, until we reach 4106169699Skan the declared identifier (or NULL_TREE, in an absolute declarator). 4107169699Skan At each stage we maintain an unqualified version of the type 4108169699Skan together with any qualifiers that should be applied to it with 4109169699Skan c_build_qualified_type; this way, array types including 4110169699Skan multidimensional array types are first built up in unqualified 4111169699Skan form and then the qualified form is created with 4112169699Skan TYPE_MAIN_VARIANT pointing to the unqualified form. */ 411318334Speter 4114169699Skan while (declarator && declarator->kind != cdk_id) 411518334Speter { 411618334Speter if (type == error_mark_node) 411718334Speter { 4118169699Skan declarator = declarator->declarator; 411918334Speter continue; 412018334Speter } 412118334Speter 4122169699Skan /* Each level of DECLARATOR is either a cdk_array (for ...[..]), 4123169699Skan a cdk_pointer (for *...), 4124169699Skan a cdk_function (for ...(...)), 4125169699Skan a cdk_attrs (for nested attributes), 4126169699Skan or a cdk_id (for the name being declared 4127169699Skan or the place in an absolute declarator 412818334Speter where the name was omitted). 4129169699Skan For the last case, we have just exited the loop. 413018334Speter 413118334Speter At this point, TYPE is the type of elements of an array, 413218334Speter or for a function to return, or for a pointer to point to. 413318334Speter After this sequence of ifs, TYPE is the type of the 413418334Speter array or function or pointer, and DECLARATOR has had its 413518334Speter outermost layer removed. */ 413618334Speter 4137169699Skan if (array_ptr_quals != TYPE_UNQUALIFIED 4138169699Skan || array_ptr_attrs != NULL_TREE 4139169699Skan || array_parm_static) 414018334Speter { 414190075Sobrien /* Only the innermost declarator (making a parameter be of 414290075Sobrien array type which is converted to pointer type) 414390075Sobrien may have static or type qualifiers. */ 414490075Sobrien error ("static or type qualifiers in non-parameter array declarator"); 4145169699Skan array_ptr_quals = TYPE_UNQUALIFIED; 4146169699Skan array_ptr_attrs = NULL_TREE; 414790075Sobrien array_parm_static = 0; 414890075Sobrien } 414990075Sobrien 4150169699Skan switch (declarator->kind) 415190075Sobrien { 4152169699Skan case cdk_attrs: 4153169699Skan { 4154169699Skan /* A declarator with embedded attributes. */ 4155169699Skan tree attrs = declarator->u.attrs; 4156169699Skan const struct c_declarator *inner_decl; 4157169699Skan int attr_flags = 0; 4158169699Skan declarator = declarator->declarator; 4159169699Skan inner_decl = declarator; 4160169699Skan while (inner_decl->kind == cdk_attrs) 4161169699Skan inner_decl = inner_decl->declarator; 4162169699Skan if (inner_decl->kind == cdk_id) 4163169699Skan attr_flags |= (int) ATTR_FLAG_DECL_NEXT; 4164169699Skan else if (inner_decl->kind == cdk_function) 4165169699Skan attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; 4166169699Skan else if (inner_decl->kind == cdk_array) 4167169699Skan attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; 4168169699Skan returned_attrs = decl_attributes (&type, 4169169699Skan chainon (returned_attrs, attrs), 4170169699Skan attr_flags); 4171169699Skan break; 4172169699Skan } 4173169699Skan case cdk_array: 4174169699Skan { 4175169699Skan tree itype = NULL_TREE; 4176169699Skan tree size = declarator->u.array.dimen; 4177169699Skan /* The index is a signed object `sizetype' bits wide. */ 4178169699Skan tree index_type = c_common_signed_type (sizetype); 417918334Speter 4180169699Skan array_ptr_quals = declarator->u.array.quals; 4181169699Skan array_ptr_attrs = declarator->u.array.attrs; 4182169699Skan array_parm_static = declarator->u.array.static_p; 4183169699Skan array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p; 418490075Sobrien 4185169699Skan declarator = declarator->declarator; 418618334Speter 4187169699Skan /* Check for some types that there cannot be arrays of. */ 418818334Speter 4189169699Skan if (VOID_TYPE_P (type)) 4190169699Skan { 4191169699Skan error ("declaration of %qs as array of voids", name); 4192169699Skan type = error_mark_node; 4193169699Skan } 419418334Speter 4195169699Skan if (TREE_CODE (type) == FUNCTION_TYPE) 4196169699Skan { 4197169699Skan error ("declaration of %qs as array of functions", name); 4198169699Skan type = error_mark_node; 4199169699Skan } 4200169699Skan 4201169699Skan if (pedantic && !in_system_header && flexible_array_type_p (type)) 4202169699Skan pedwarn ("invalid use of structure with flexible array member"); 4203169699Skan 4204169699Skan if (size == error_mark_node) 420518334Speter type = error_mark_node; 420618334Speter 4207169699Skan if (type == error_mark_node) 4208169699Skan continue; 4209117421Skan 4210169699Skan /* If size was specified, set ITYPE to a range-type for 4211169699Skan that size. Otherwise, ITYPE remains null. finish_decl 4212169699Skan may figure it out from an initial value. */ 421318334Speter 4214169699Skan if (size) 4215169699Skan { 4216169699Skan /* Strip NON_LVALUE_EXPRs since we aren't using as an 4217169699Skan lvalue. */ 4218169699Skan STRIP_TYPE_NOPS (size); 421918334Speter 4220169699Skan if (!INTEGRAL_TYPE_P (TREE_TYPE (size))) 4221169699Skan { 4222169699Skan error ("size of array %qs has non-integer type", name); 4223169699Skan size = integer_one_node; 4224169699Skan } 422518334Speter 4226169699Skan if (pedantic && integer_zerop (size)) 4227169699Skan pedwarn ("ISO C forbids zero-size array %qs", name); 422818334Speter 4229169699Skan if (TREE_CODE (size) == INTEGER_CST) 4230169699Skan { 4231169699Skan constant_expression_warning (size); 4232169699Skan if (tree_int_cst_sgn (size) < 0) 4233169699Skan { 4234169699Skan error ("size of array %qs is negative", name); 4235169699Skan size = integer_one_node; 4236169699Skan } 4237169699Skan } 4238169699Skan else if ((decl_context == NORMAL || decl_context == FIELD) 4239169699Skan && current_scope == file_scope) 4240169699Skan { 4241169699Skan error ("variably modified %qs at file scope", name); 4242169699Skan size = integer_one_node; 4243169699Skan } 4244169699Skan else 4245169699Skan { 4246169699Skan /* Make sure the array size remains visibly 4247169699Skan nonconstant even if it is (eg) a const variable 4248169699Skan with known value. */ 4249169699Skan size_varies = 1; 425018334Speter 4251169699Skan if (!flag_isoc99 && pedantic) 4252169699Skan { 4253169699Skan if (TREE_CONSTANT (size)) 4254169699Skan pedwarn ("ISO C90 forbids array %qs whose size " 4255169699Skan "can%'t be evaluated", 4256169699Skan name); 4257169699Skan else 4258169699Skan pedwarn ("ISO C90 forbids variable-size array %qs", 4259169699Skan name); 4260169699Skan } 4261169699Skan } 426218334Speter 4263169699Skan if (integer_zerop (size)) 4264169699Skan { 4265169699Skan /* A zero-length array cannot be represented with 4266169699Skan an unsigned index type, which is what we'll 4267169699Skan get with build_index_type. Create an 4268169699Skan open-ended range instead. */ 4269169699Skan itype = build_range_type (sizetype, size, NULL_TREE); 4270169699Skan } 4271169699Skan else 4272169699Skan { 4273169699Skan /* Arrange for the SAVE_EXPR on the inside of the 4274169699Skan MINUS_EXPR, which allows the -1 to get folded 4275169699Skan with the +1 that happens when building TYPE_SIZE. */ 4276169699Skan if (size_varies) 4277169699Skan size = variable_size (size); 427818334Speter 4279169699Skan /* Compute the maximum valid index, that is, size 4280169699Skan - 1. Do the calculation in index_type, so that 4281169699Skan if it is a variable the computations will be 4282169699Skan done in the proper mode. */ 4283169699Skan itype = fold_build2 (MINUS_EXPR, index_type, 4284169699Skan convert (index_type, size), 4285169699Skan convert (index_type, 4286169699Skan size_one_node)); 428718334Speter 4288169699Skan /* If that overflowed, the array is too big. ??? 4289169699Skan While a size of INT_MAX+1 technically shouldn't 4290169699Skan cause an overflow (because we subtract 1), the 4291169699Skan overflow is recorded during the conversion to 4292169699Skan index_type, before the subtraction. Handling 4293169699Skan this case seems like an unnecessary 4294169699Skan complication. */ 4295169699Skan if (TREE_CODE (itype) == INTEGER_CST 4296169699Skan && TREE_OVERFLOW (itype)) 4297169699Skan { 4298169699Skan error ("size of array %qs is too large", name); 4299169699Skan type = error_mark_node; 4300169699Skan continue; 4301169699Skan } 430250397Sobrien 4303169699Skan itype = build_index_type (itype); 4304169699Skan } 4305169699Skan } 4306169699Skan else if (decl_context == FIELD) 4307169699Skan { 4308169699Skan if (pedantic && !flag_isoc99 && !in_system_header) 4309169699Skan pedwarn ("ISO C90 does not support flexible array members"); 431090075Sobrien 4311169699Skan /* ISO C99 Flexible array members are effectively 4312169699Skan identical to GCC's zero-length array extension. */ 4313169699Skan itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 4314169699Skan } 4315169699Skan else if (decl_context == PARM) 4316169699Skan { 4317169699Skan if (array_parm_vla_unspec_p) 4318169699Skan { 4319169699Skan if (! orig_name) 4320169699Skan { 4321169699Skan /* C99 6.7.5.2p4 */ 4322169699Skan error ("%<[*]%> not allowed in other than a declaration"); 4323169699Skan } 432418334Speter 4325169699Skan itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 4326169699Skan size_varies = 1; 4327169699Skan } 4328169699Skan } 4329169699Skan else if (decl_context == TYPENAME) 4330169699Skan { 4331169699Skan if (array_parm_vla_unspec_p) 4332169699Skan { 4333169699Skan /* The error is printed elsewhere. We use this to 4334169699Skan avoid messing up with incomplete array types of 4335169699Skan the same type, that would otherwise be modified 4336169699Skan below. */ 4337169699Skan itype = build_range_type (sizetype, size_zero_node, 4338169699Skan NULL_TREE); 4339169699Skan } 4340169699Skan } 434118334Speter 4342169699Skan /* Complain about arrays of incomplete types. */ 4343169699Skan if (!COMPLETE_TYPE_P (type)) 4344169699Skan { 4345169699Skan error ("array type has incomplete element type"); 4346169699Skan type = error_mark_node; 4347169699Skan } 4348169699Skan else 4349169699Skan /* When itype is NULL, a shared incomplete array type is 4350169699Skan returned for all array of a given type. Elsewhere we 4351169699Skan make sure we don't complete that type before copying 4352169699Skan it, but here we want to make sure we don't ever 4353169699Skan modify the shared type, so we gcc_assert (itype) 4354169699Skan below. */ 4355169699Skan type = build_array_type (type, itype); 435690075Sobrien 4357169699Skan if (type != error_mark_node) 4358169699Skan { 4359169699Skan if (size_varies) 4360169699Skan { 4361169699Skan /* It is ok to modify type here even if itype is 4362169699Skan NULL: if size_varies, we're in a 4363169699Skan multi-dimensional array and the inner type has 4364169699Skan variable size, so the enclosing shared array type 4365169699Skan must too. */ 4366169699Skan if (size && TREE_CODE (size) == INTEGER_CST) 4367169699Skan type 4368169699Skan = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); 4369169699Skan C_TYPE_VARIABLE_SIZE (type) = 1; 4370169699Skan } 437190075Sobrien 4372169699Skan /* The GCC extension for zero-length arrays differs from 4373169699Skan ISO flexible array members in that sizeof yields 4374169699Skan zero. */ 4375169699Skan if (size && integer_zerop (size)) 4376169699Skan { 4377169699Skan gcc_assert (itype); 4378169699Skan TYPE_SIZE (type) = bitsize_zero_node; 4379169699Skan TYPE_SIZE_UNIT (type) = size_zero_node; 4380169699Skan } 4381169699Skan if (array_parm_vla_unspec_p) 4382169699Skan { 4383169699Skan gcc_assert (itype); 4384169699Skan /* The type is complete. C99 6.7.5.2p4 */ 4385169699Skan TYPE_SIZE (type) = bitsize_zero_node; 4386169699Skan TYPE_SIZE_UNIT (type) = size_zero_node; 4387169699Skan } 4388169699Skan } 438918334Speter 4390169699Skan if (decl_context != PARM 4391169699Skan && (array_ptr_quals != TYPE_UNQUALIFIED 4392169699Skan || array_ptr_attrs != NULL_TREE 4393169699Skan || array_parm_static)) 4394169699Skan { 4395169699Skan error ("static or type qualifiers in non-parameter array declarator"); 4396169699Skan array_ptr_quals = TYPE_UNQUALIFIED; 4397169699Skan array_ptr_attrs = NULL_TREE; 4398169699Skan array_parm_static = 0; 4399169699Skan } 4400169699Skan break; 4401169699Skan } 4402169699Skan case cdk_function: 4403169699Skan { 4404169699Skan /* Say it's a definition only for the declarator closest 4405169699Skan to the identifier, apart possibly from some 4406169699Skan attributes. */ 4407169699Skan bool really_funcdef = false; 4408169699Skan tree arg_types; 4409169699Skan if (funcdef_flag) 4410169699Skan { 4411169699Skan const struct c_declarator *t = declarator->declarator; 4412169699Skan while (t->kind == cdk_attrs) 4413169699Skan t = t->declarator; 4414169699Skan really_funcdef = (t->kind == cdk_id); 4415169699Skan } 441618334Speter 4417169699Skan /* Declaring a function type. Make sure we have a valid 4418169699Skan type for the function to return. */ 4419169699Skan if (type == error_mark_node) 4420169699Skan continue; 442190075Sobrien 4422169699Skan size_varies = 0; 442318334Speter 4424169699Skan /* Warn about some types functions can't return. */ 4425169699Skan if (TREE_CODE (type) == FUNCTION_TYPE) 4426169699Skan { 4427169699Skan error ("%qs declared as function returning a function", name); 4428169699Skan type = integer_type_node; 4429169699Skan } 4430169699Skan if (TREE_CODE (type) == ARRAY_TYPE) 4431169699Skan { 4432169699Skan error ("%qs declared as function returning an array", name); 4433169699Skan type = integer_type_node; 4434169699Skan } 443518334Speter 4436169699Skan /* Construct the function type and go to the next 4437169699Skan inner layer of declarator. */ 4438169699Skan arg_info = declarator->u.arg_info; 4439169699Skan arg_types = grokparms (arg_info, really_funcdef); 4440169699Skan if (really_funcdef) 4441169699Skan put_pending_sizes (arg_info->pending_sizes); 444218334Speter 4443169699Skan /* Type qualifiers before the return type of the function 4444169699Skan qualify the return type, not the function type. */ 4445169699Skan if (type_quals) 4446169699Skan { 4447169699Skan /* Type qualifiers on a function return type are 4448169699Skan normally permitted by the standard but have no 4449169699Skan effect, so give a warning at -Wreturn-type. 4450169699Skan Qualifiers on a void return type are banned on 4451169699Skan function definitions in ISO C; GCC used to used 4452169699Skan them for noreturn functions. */ 4453169699Skan if (VOID_TYPE_P (type) && really_funcdef) 4454169699Skan pedwarn ("function definition has qualified void return type"); 4455169699Skan else 4456169699Skan warning (OPT_Wreturn_type, 4457169699Skan "type qualifiers ignored on function return type"); 445818334Speter 4459169699Skan type = c_build_qualified_type (type, type_quals); 4460169699Skan } 4461169699Skan type_quals = TYPE_UNQUALIFIED; 446218334Speter 4463169699Skan type = build_function_type (type, arg_types); 4464169699Skan declarator = declarator->declarator; 446518334Speter 4466169699Skan /* Set the TYPE_CONTEXTs for each tagged type which is local to 4467169699Skan the formal parameter list of this FUNCTION_TYPE to point to 4468169699Skan the FUNCTION_TYPE node itself. */ 446990075Sobrien { 4470169699Skan tree link; 447190075Sobrien 4472169699Skan for (link = arg_info->tags; 4473169699Skan link; 4474169699Skan link = TREE_CHAIN (link)) 4475169699Skan TYPE_CONTEXT (TREE_VALUE (link)) = type; 447690075Sobrien } 4477169699Skan break; 4478169699Skan } 4479169699Skan case cdk_pointer: 448018334Speter { 4481169699Skan /* Merge any constancy or volatility into the target type 4482169699Skan for the pointer. */ 448318334Speter 4484169699Skan if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 4485169699Skan && type_quals) 4486169699Skan pedwarn ("ISO C forbids qualified function types"); 4487169699Skan if (type_quals) 4488169699Skan type = c_build_qualified_type (type, type_quals); 4489169699Skan size_varies = 0; 449018334Speter 4491169699Skan /* When the pointed-to type involves components of variable size, 4492169699Skan care must be taken to ensure that the size evaluation code is 4493169699Skan emitted early enough to dominate all the possible later uses 4494169699Skan and late enough for the variables on which it depends to have 4495169699Skan been assigned. 449618334Speter 4497169699Skan This is expected to happen automatically when the pointed-to 4498169699Skan type has a name/declaration of it's own, but special attention 4499169699Skan is required if the type is anonymous. 450018334Speter 4501169699Skan We handle the NORMAL and FIELD contexts here by attaching an 4502169699Skan artificial TYPE_DECL to such pointed-to type. This forces the 4503169699Skan sizes evaluation at a safe point and ensures it is not deferred 4504169699Skan until e.g. within a deeper conditional context. 450518334Speter 4506169699Skan We expect nothing to be needed here for PARM or TYPENAME. 4507169699Skan Pushing a TYPE_DECL at this point for TYPENAME would actually 4508169699Skan be incorrect, as we might be in the middle of an expression 4509169699Skan with side effects on the pointed-to type size "arguments" prior 4510169699Skan to the pointer declaration point and the fake TYPE_DECL in the 4511169699Skan enclosing context would force the size evaluation prior to the 4512169699Skan side effects. */ 451352284Sobrien 4514169699Skan if (!TYPE_NAME (type) 4515169699Skan && (decl_context == NORMAL || decl_context == FIELD) 4516169699Skan && variably_modified_type_p (type, NULL_TREE)) 4517169699Skan { 4518169699Skan tree decl = build_decl (TYPE_DECL, NULL_TREE, type); 4519169699Skan DECL_ARTIFICIAL (decl) = 1; 4520169699Skan pushdecl (decl); 4521169699Skan finish_decl (decl, NULL_TREE, NULL_TREE); 4522169699Skan TYPE_NAME (type) = decl; 4523169699Skan } 452452284Sobrien 4525169699Skan type = build_pointer_type (type); 452690075Sobrien 4527169699Skan /* Process type qualifiers (such as const or volatile) 4528169699Skan that were given inside the `*'. */ 4529169699Skan type_quals = declarator->u.pointer_quals; 453052284Sobrien 4531169699Skan declarator = declarator->declarator; 4532169699Skan break; 4533169699Skan } 4534169699Skan default: 4535169699Skan gcc_unreachable (); 453618334Speter } 453718334Speter } 453818334Speter 4539169699Skan /* Now TYPE has the actual type, apart from any qualifiers in 4540169699Skan TYPE_QUALS. */ 454118334Speter 4542146906Skan /* Check the type and width of a bit-field. */ 4543146906Skan if (bitfield) 4544146906Skan check_bitfield_type_and_width (&type, width, orig_name); 4545146906Skan 454650397Sobrien /* Did array size calculations overflow? */ 454750397Sobrien 454850397Sobrien if (TREE_CODE (type) == ARRAY_TYPE 454990075Sobrien && COMPLETE_TYPE_P (type) 4550169699Skan && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST 4551169699Skan && TREE_OVERFLOW (TYPE_SIZE_UNIT (type))) 455290075Sobrien { 4553169699Skan error ("size of array %qs is too large", name); 455490075Sobrien /* If we proceed with the array type as it is, we'll eventually 455590075Sobrien crash in tree_low_cst(). */ 455690075Sobrien type = error_mark_node; 455790075Sobrien } 455850397Sobrien 455918334Speter /* If this is declaring a typedef name, return a TYPE_DECL. */ 456018334Speter 4561169699Skan if (storage_class == csc_typedef) 456218334Speter { 456318334Speter tree decl; 456418334Speter if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 456552284Sobrien && type_quals) 456690075Sobrien pedwarn ("ISO C forbids qualified function types"); 456752284Sobrien if (type_quals) 456852284Sobrien type = c_build_qualified_type (type, type_quals); 4569169699Skan decl = build_decl (TYPE_DECL, declarator->u.id, type); 4570169699Skan if (declspecs->explicit_signed_p) 457118334Speter C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 457290075Sobrien decl_attributes (&decl, returned_attrs, 0); 4573169699Skan if (declspecs->inline_p) 4574169699Skan pedwarn ("typedef %q+D declared %<inline%>", decl); 457518334Speter return decl; 457618334Speter } 457718334Speter 457818334Speter /* If this is a type name (such as, in a cast or sizeof), 457918334Speter compute the type and return it now. */ 458018334Speter 458118334Speter if (decl_context == TYPENAME) 458218334Speter { 4583169699Skan /* Note that the grammar rejects storage classes in typenames 4584169699Skan and fields. */ 4585169699Skan gcc_assert (storage_class == csc_none && !threadp 4586169699Skan && !declspecs->inline_p); 458718334Speter if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 458852284Sobrien && type_quals) 458990075Sobrien pedwarn ("ISO C forbids const or volatile function types"); 459052284Sobrien if (type_quals) 459152284Sobrien type = c_build_qualified_type (type, type_quals); 459290075Sobrien decl_attributes (&type, returned_attrs, 0); 459318334Speter return type; 459418334Speter } 459518334Speter 4596169699Skan if (pedantic && decl_context == FIELD 4597169699Skan && variably_modified_type_p (type, NULL_TREE)) 4598169699Skan { 4599169699Skan /* C99 6.7.2.1p8 */ 4600169699Skan pedwarn ("a member of a structure or union cannot have a variably modified type"); 4601169699Skan } 4602169699Skan 460318334Speter /* Aside from typedefs and type names (handle above), 460418334Speter `void' at top level (not within pointer) 460518334Speter is allowed only in public variables. 460618334Speter We don't complain about parms either, but that is because 460718334Speter a better error message can be made later. */ 460818334Speter 460990075Sobrien if (VOID_TYPE_P (type) && decl_context != PARM 4610169699Skan && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE) 4611169699Skan && (storage_class == csc_extern 4612169699Skan || (current_scope == file_scope 4613169699Skan && !(storage_class == csc_static 4614169699Skan || storage_class == csc_register))))) 461518334Speter { 4616169699Skan error ("variable or field %qs declared void", name); 461718334Speter type = integer_type_node; 461818334Speter } 461918334Speter 462018334Speter /* Now create the decl, which may be a VAR_DECL, a PARM_DECL 462118334Speter or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ 462218334Speter 462318334Speter { 462490075Sobrien tree decl; 462518334Speter 462618334Speter if (decl_context == PARM) 462718334Speter { 462890075Sobrien tree type_as_written; 462990075Sobrien tree promoted_type; 463018334Speter 463118334Speter /* A parameter declared as an array of T is really a pointer to T. 463218334Speter One declared as a function is really a pointer to a function. */ 463318334Speter 463418334Speter if (TREE_CODE (type) == ARRAY_TYPE) 463518334Speter { 463618334Speter /* Transfer const-ness of array into that of type pointed to. */ 463718334Speter type = TREE_TYPE (type); 463852284Sobrien if (type_quals) 463952284Sobrien type = c_build_qualified_type (type, type_quals); 464018334Speter type = build_pointer_type (type); 4641169699Skan type_quals = array_ptr_quals; 464290075Sobrien 4643169699Skan /* We don't yet implement attributes in this context. */ 4644169699Skan if (array_ptr_attrs != NULL_TREE) 4645169699Skan warning (OPT_Wattributes, 4646169699Skan "attributes in parameter array declarator ignored"); 464790075Sobrien 464818334Speter size_varies = 0; 464918334Speter } 465018334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 465118334Speter { 465252284Sobrien if (pedantic && type_quals) 465390075Sobrien pedwarn ("ISO C forbids qualified function types"); 465452284Sobrien if (type_quals) 465552284Sobrien type = c_build_qualified_type (type, type_quals); 465618334Speter type = build_pointer_type (type); 465752284Sobrien type_quals = TYPE_UNQUALIFIED; 465818334Speter } 465990075Sobrien else if (type_quals) 466090075Sobrien type = c_build_qualified_type (type, type_quals); 4661132730Skan 466290075Sobrien type_as_written = type; 466318334Speter 4664169699Skan decl = build_decl (PARM_DECL, declarator->u.id, type); 466518334Speter if (size_varies) 466618334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 466718334Speter 466818334Speter /* Compute the type actually passed in the parmlist, 466918334Speter for the case where there is no prototype. 467018334Speter (For example, shorts and chars are passed as ints.) 467118334Speter When there is a prototype, this is overridden later. */ 467218334Speter 467390075Sobrien if (type == error_mark_node) 467490075Sobrien promoted_type = type; 467590075Sobrien else 4676117421Skan promoted_type = c_type_promotes_to (type); 467718334Speter 467890075Sobrien DECL_ARG_TYPE (decl) = promoted_type; 4679169699Skan if (declspecs->inline_p) 4680169699Skan pedwarn ("parameter %q+D declared %<inline%>", decl); 468118334Speter } 468218334Speter else if (decl_context == FIELD) 468318334Speter { 4684169699Skan /* Note that the grammar rejects storage classes in typenames 4685169699Skan and fields. */ 4686169699Skan gcc_assert (storage_class == csc_none && !threadp 4687169699Skan && !declspecs->inline_p); 4688169699Skan 468918334Speter /* Structure field. It may not be a function. */ 469096263Sobrien 469118334Speter if (TREE_CODE (type) == FUNCTION_TYPE) 469218334Speter { 4693169699Skan error ("field %qs declared as a function", name); 469418334Speter type = build_pointer_type (type); 469518334Speter } 469690075Sobrien else if (TREE_CODE (type) != ERROR_MARK 4697169699Skan && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type)) 469818334Speter { 4699169699Skan error ("field %qs has incomplete type", name); 470018334Speter type = error_mark_node; 470118334Speter } 4702169699Skan type = c_build_qualified_type (type, type_quals); 4703169699Skan decl = build_decl (FIELD_DECL, declarator->u.id, type); 470490075Sobrien DECL_NONADDRESSABLE_P (decl) = bitfield; 470590075Sobrien 470618334Speter if (size_varies) 470718334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 470818334Speter } 470918334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 471018334Speter { 4711169699Skan if (storage_class == csc_register || threadp) 4712169699Skan { 4713169699Skan error ("invalid storage class for function %qs", name); 4714169699Skan } 4715169699Skan else if (current_scope != file_scope) 4716169699Skan { 4717169699Skan /* Function declaration not at file scope. Storage 4718169699Skan classes other than `extern' are not allowed, C99 4719169699Skan 6.7.1p5, and `extern' makes no difference. However, 4720169699Skan GCC allows 'auto', perhaps with 'inline', to support 4721169699Skan nested functions. */ 4722169699Skan if (storage_class == csc_auto) 4723169699Skan { 4724169699Skan if (pedantic) 4725169699Skan pedwarn ("invalid storage class for function %qs", name); 4726169699Skan } 4727169699Skan else if (storage_class == csc_static) 4728169699Skan { 4729169699Skan error ("invalid storage class for function %qs", name); 4730169699Skan if (funcdef_flag) 4731169699Skan storage_class = declspecs->storage_class = csc_none; 4732169699Skan else 4733169699Skan return 0; 4734169699Skan } 4735169699Skan } 473618334Speter 4737169699Skan decl = build_decl (FUNCTION_DECL, declarator->u.id, type); 473890075Sobrien decl = build_decl_attribute_variant (decl, decl_attr); 473918334Speter 4740169699Skan DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl); 474190075Sobrien 4742169699Skan if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl)) 474390075Sobrien pedwarn ("ISO C forbids qualified function types"); 474418334Speter 4745169699Skan /* GNU C interprets a volatile-qualified function type to indicate 474652284Sobrien that the function does not return. */ 474752284Sobrien if ((type_quals & TYPE_QUAL_VOLATILE) 474890075Sobrien && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) 4749169699Skan warning (0, "%<noreturn%> function returns non-void value"); 475018334Speter 4751169699Skan /* Every function declaration is an external reference 4752169699Skan (DECL_EXTERNAL) except for those which are not at file 4753169699Skan scope and are explicitly declared "auto". This is 4754169699Skan forbidden by standard C (C99 6.7.1p5) and is interpreted by 4755169699Skan GCC to signify a forward declaration of a nested function. */ 4756169699Skan if (storage_class == csc_auto && current_scope != file_scope) 4757169699Skan DECL_EXTERNAL (decl) = 0; 4758169699Skan else 475918334Speter DECL_EXTERNAL (decl) = 1; 4760169699Skan 476118334Speter /* Record absence of global scope for `static' or `auto'. */ 476218334Speter TREE_PUBLIC (decl) 4763169699Skan = !(storage_class == csc_static || storage_class == csc_auto); 476418334Speter 4765169699Skan /* For a function definition, record the argument information 4766169699Skan block where store_parm_decls will look for it. */ 4767169699Skan if (funcdef_flag) 4768169699Skan current_function_arg_info = arg_info; 4769169699Skan 4770169699Skan if (declspecs->default_int_p) 477196263Sobrien C_FUNCTION_IMPLICIT_INT (decl) = 1; 477296263Sobrien 477318334Speter /* Record presence of `inline', if it is reasonable. */ 4774169699Skan if (flag_hosted && MAIN_NAME_P (declarator->u.id)) 477518334Speter { 4776169699Skan if (declspecs->inline_p) 4777169699Skan pedwarn ("cannot inline function %<main%>"); 477890075Sobrien } 4779169699Skan else if (declspecs->inline_p) 478090075Sobrien { 4781132730Skan /* Record that the function is declared `inline'. */ 478290075Sobrien DECL_DECLARED_INLINE_P (decl) = 1; 478318334Speter 478490075Sobrien /* Do not mark bare declarations as DECL_INLINE. Doing so 478590075Sobrien in the presence of multiple declarations can result in 478690075Sobrien the abstract origin pointing between the declarations, 478790075Sobrien which will confuse dwarf2out. */ 478890075Sobrien if (initialized) 478990075Sobrien { 479090075Sobrien DECL_INLINE (decl) = 1; 4791169699Skan if (storage_class == csc_extern) 479290075Sobrien current_extern_inline = 1; 479390075Sobrien } 479418334Speter } 479590075Sobrien /* If -finline-functions, assume it can be inlined. This does 479690075Sobrien two things: let the function be deferred until it is actually 479790075Sobrien needed, and let dwarf2 know that the function is inlinable. */ 479890075Sobrien else if (flag_inline_trees == 2 && initialized) 4799132730Skan DECL_INLINE (decl) = 1; 480018334Speter } 480118334Speter else 480218334Speter { 480318334Speter /* It's a variable. */ 480418334Speter /* An uninitialized decl with `extern' is a reference. */ 4805169699Skan int extern_ref = !initialized && storage_class == csc_extern; 480618334Speter 4807169699Skan type = c_build_qualified_type (type, type_quals); 4808132730Skan 4809169699Skan /* C99 6.2.2p7: It is invalid (compile-time undefined 4810169699Skan behavior) to create an 'extern' declaration for a 4811132730Skan variable if there is a global declaration that is 4812169699Skan 'static' and the global declaration is not visible. 4813169699Skan (If the static declaration _is_ currently visible, 4814169699Skan the 'extern' declaration is taken to refer to that decl.) */ 4815169699Skan if (extern_ref && current_scope != file_scope) 4816132730Skan { 4817169699Skan tree global_decl = identifier_global_value (declarator->u.id); 4818169699Skan tree visible_decl = lookup_name (declarator->u.id); 4819132730Skan 4820132730Skan if (global_decl 4821169699Skan && global_decl != visible_decl 4822132730Skan && TREE_CODE (global_decl) == VAR_DECL 4823132730Skan && !TREE_PUBLIC (global_decl)) 4824169699Skan error ("variable previously declared %<static%> redeclared " 4825169699Skan "%<extern%>"); 4826132730Skan } 4827132730Skan 4828169699Skan decl = build_decl (VAR_DECL, declarator->u.id, type); 4829169699Skan DECL_SOURCE_LOCATION (decl) = declarator->id_loc; 483018334Speter if (size_varies) 483118334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 483218334Speter 4833169699Skan if (declspecs->inline_p) 4834169699Skan pedwarn ("variable %q+D declared %<inline%>", decl); 483518334Speter 4836169699Skan /* At file scope, an initialized extern declaration may follow 4837169699Skan a static declaration. In that case, DECL_EXTERNAL will be 4838169699Skan reset later in start_decl. */ 4839169699Skan DECL_EXTERNAL (decl) = (storage_class == csc_extern); 4840117421Skan 4841132730Skan /* At file scope, the presence of a `static' or `register' storage 484218334Speter class specifier, or the absence of all storage class specifiers 484318334Speter makes this declaration a definition (perhaps tentative). Also, 4844169699Skan the absence of `static' makes it public. */ 4845169699Skan if (current_scope == file_scope) 484618334Speter { 4847169699Skan TREE_PUBLIC (decl) = storage_class != csc_static; 4848117421Skan TREE_STATIC (decl) = !extern_ref; 484918334Speter } 4850132730Skan /* Not at file scope, only `static' makes a static definition. */ 485118334Speter else 485218334Speter { 4853169699Skan TREE_STATIC (decl) = (storage_class == csc_static); 4854117421Skan TREE_PUBLIC (decl) = extern_ref; 485518334Speter } 4856117421Skan 4857169699Skan if (threadp) 4858117421Skan { 4859117421Skan if (targetm.have_tls) 4860169699Skan DECL_TLS_MODEL (decl) = decl_default_tls_model (decl); 4861117421Skan else 4862117421Skan /* A mere warning is sure to result in improper semantics 4863117421Skan at runtime. Don't bother to allow this to compile. */ 4864117421Skan error ("thread-local storage not supported for this target"); 4865117421Skan } 486618334Speter } 486718334Speter 4868169699Skan if (storage_class == csc_extern 4869169699Skan && variably_modified_type_p (type, NULL_TREE)) 4870169699Skan { 4871169699Skan /* C99 6.7.5.2p2 */ 4872169699Skan error ("object with variably modified type must have no linkage"); 4873169699Skan } 4874169699Skan 487518334Speter /* Record `register' declaration for warnings on & 487618334Speter and in case doing stupid register allocation. */ 487718334Speter 4878169699Skan if (storage_class == csc_register) 4879169699Skan { 4880169699Skan C_DECL_REGISTER (decl) = 1; 4881169699Skan DECL_REGISTER (decl) = 1; 4882169699Skan } 488318334Speter 488418334Speter /* Record constancy and volatility. */ 488552284Sobrien c_apply_type_quals_to_decl (type_quals, decl); 488618334Speter 488718334Speter /* If a type has volatile components, it should be stored in memory. 488818334Speter Otherwise, the fact that those components are volatile 4889169699Skan will be ignored, and would even crash the compiler. 4890169699Skan Of course, this only makes sense on VAR,PARM, and RESULT decl's. */ 4891169699Skan if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)) 4892169699Skan && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL 4893169699Skan || TREE_CODE (decl) == RESULT_DECL)) 4894169699Skan { 4895169699Skan /* It is not an error for a structure with volatile fields to 4896169699Skan be declared register, but reset DECL_REGISTER since it 4897169699Skan cannot actually go in a register. */ 4898169699Skan int was_reg = C_DECL_REGISTER (decl); 4899169699Skan C_DECL_REGISTER (decl) = 0; 4900169699Skan DECL_REGISTER (decl) = 0; 4901169699Skan c_mark_addressable (decl); 4902169699Skan C_DECL_REGISTER (decl) = was_reg; 4903169699Skan } 490418334Speter 4905132730Skan /* This is the earliest point at which we might know the assembler 4906132730Skan name of a variable. Thus, if it's known before this, die horribly. */ 4907169699Skan gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)); 4908132730Skan 490990075Sobrien decl_attributes (&decl, returned_attrs, 0); 491018334Speter 491118334Speter return decl; 491218334Speter } 491318334Speter} 491418334Speter 491518334Speter/* Decode the parameter-list info for a function type or function definition. 4916169699Skan The argument is the value returned by `get_parm_info' (or made in c-parse.c 491718334Speter if there is an identifier list instead of a parameter decl list). 491818334Speter These two functions are separate because when a function returns 491918334Speter or receives functions then each is called multiple times but the order 492018334Speter of calls is different. The last call to `grokparms' is always the one 492118334Speter that contains the formal parameter names of a function definition. 492218334Speter 492318334Speter Return a list of arg types to use in the FUNCTION_TYPE for this function. 492418334Speter 4925169699Skan FUNCDEF_FLAG is true for a function definition, false for 492618334Speter a mere declaration. A nonempty identifier-list gets an error message 4927169699Skan when FUNCDEF_FLAG is false. */ 492818334Speter 492918334Speterstatic tree 4930169699Skangrokparms (struct c_arg_info *arg_info, bool funcdef_flag) 493118334Speter{ 4932169699Skan tree arg_types = arg_info->types; 493318334Speter 4934169699Skan if (funcdef_flag && arg_info->had_vla_unspec) 4935169699Skan { 4936169699Skan /* A function definition isn't function prototype scope C99 6.2.1p4. */ 4937169699Skan /* C99 6.7.5.2p4 */ 4938169699Skan error ("%<[*]%> not allowed in other than function prototype scope"); 4939169699Skan } 494018334Speter 4941169699Skan if (arg_types == 0 && !funcdef_flag && !in_system_header) 4942169699Skan warning (OPT_Wstrict_prototypes, 4943169699Skan "function declaration isn%'t a prototype"); 494418334Speter 4945169699Skan if (arg_types == error_mark_node) 4946169699Skan return 0; /* don't set TYPE_ARG_TYPES in this case */ 4947169699Skan 4948169699Skan else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE) 494918334Speter { 4950169699Skan if (!funcdef_flag) 495118334Speter pedwarn ("parameter names (without types) in function declaration"); 495218334Speter 4953169699Skan arg_info->parms = arg_info->types; 4954169699Skan arg_info->types = 0; 495518334Speter return 0; 495618334Speter } 495718334Speter else 495818334Speter { 4959169699Skan tree parm, type, typelt; 4960169699Skan unsigned int parmno; 496118334Speter 4962169699Skan /* If there is a parameter of incomplete type in a definition, 4963169699Skan this is an error. In a declaration this is valid, and a 4964169699Skan struct or union type may be completed later, before any calls 4965169699Skan or definition of the function. In the case where the tag was 4966169699Skan first declared within the parameter list, a warning has 4967169699Skan already been given. If a parameter has void type, then 4968169699Skan however the function cannot be defined or called, so 4969169699Skan warn. */ 4970169699Skan 4971169699Skan for (parm = arg_info->parms, typelt = arg_types, parmno = 1; 4972132730Skan parm; 4973169699Skan parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++) 4974169699Skan { 4975169699Skan type = TREE_VALUE (typelt); 4976169699Skan if (type == error_mark_node) 4977169699Skan continue; 4978132730Skan 4979169699Skan if (!COMPLETE_TYPE_P (type)) 4980169699Skan { 4981169699Skan if (funcdef_flag) 4982169699Skan { 4983169699Skan if (DECL_NAME (parm)) 4984169699Skan error ("parameter %u (%q+D) has incomplete type", 4985169699Skan parmno, parm); 4986169699Skan else 4987169699Skan error ("%Jparameter %u has incomplete type", 4988169699Skan parm, parmno); 4989169699Skan 4990169699Skan TREE_VALUE (typelt) = error_mark_node; 4991169699Skan TREE_TYPE (parm) = error_mark_node; 4992169699Skan } 4993169699Skan else if (VOID_TYPE_P (type)) 4994169699Skan { 4995169699Skan if (DECL_NAME (parm)) 4996169699Skan warning (0, "parameter %u (%q+D) has void type", 4997169699Skan parmno, parm); 4998169699Skan else 4999169699Skan warning (0, "%Jparameter %u has void type", 5000169699Skan parm, parmno); 5001169699Skan } 5002169699Skan } 5003169699Skan 5004169699Skan if (DECL_NAME (parm) && TREE_USED (parm)) 5005169699Skan warn_if_shadowing (parm); 5006169699Skan } 5007169699Skan return arg_types; 500818334Speter } 500918334Speter} 501018334Speter 5011169699Skan/* Take apart the current scope and return a c_arg_info structure with 5012169699Skan info on a parameter list just parsed. 501318334Speter 5014169699Skan This structure is later fed to 'grokparms' and 'store_parm_decls'. 501518334Speter 5016169699Skan ELLIPSIS being true means the argument list ended in '...' so don't 5017169699Skan append a sentinel (void_list_node) to the end of the type-list. */ 5018169699Skan 5019169699Skanstruct c_arg_info * 5020169699Skanget_parm_info (bool ellipsis) 502118334Speter{ 5022169699Skan struct c_binding *b = current_scope->bindings; 5023169699Skan struct c_arg_info *arg_info = XOBNEW (&parser_obstack, 5024169699Skan struct c_arg_info); 5025169699Skan tree parms = 0; 5026169699Skan tree tags = 0; 5027169699Skan tree types = 0; 5028169699Skan tree others = 0; 5029169699Skan 5030132730Skan static bool explained_incomplete_types = false; 5031132730Skan bool gave_void_only_once_err = false; 503218334Speter 5033169699Skan arg_info->parms = 0; 5034169699Skan arg_info->tags = 0; 5035169699Skan arg_info->types = 0; 5036169699Skan arg_info->others = 0; 5037169699Skan arg_info->pending_sizes = 0; 5038169699Skan arg_info->had_vla_unspec = current_scope->had_vla_unspec; 5039132730Skan 5040169699Skan /* The bindings in this scope must not get put into a block. 5041169699Skan We will take care of deleting the binding nodes. */ 5042169699Skan current_scope->bindings = 0; 504318334Speter 5044169699Skan /* This function is only called if there was *something* on the 5045169699Skan parameter list. */ 5046169699Skan gcc_assert (b); 5047169699Skan 5048169699Skan /* A parameter list consisting solely of 'void' indicates that the 5049169699Skan function takes no arguments. But if the 'void' is qualified 5050169699Skan (by 'const' or 'volatile'), or has a storage class specifier 5051169699Skan ('register'), then the behavior is undefined; issue an error. 5052169699Skan Typedefs for 'void' are OK (see DR#157). */ 5053169699Skan if (b->prev == 0 /* one binding */ 5054169699Skan && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */ 5055169699Skan && !DECL_NAME (b->decl) /* anonymous */ 5056169699Skan && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */ 505718334Speter { 5058169699Skan if (TREE_THIS_VOLATILE (b->decl) 5059169699Skan || TREE_READONLY (b->decl) 5060169699Skan || C_DECL_REGISTER (b->decl)) 5061169699Skan error ("%<void%> as only parameter may not be qualified"); 506218334Speter 5063169699Skan /* There cannot be an ellipsis. */ 5064169699Skan if (ellipsis) 5065169699Skan error ("%<void%> must be the only parameter"); 5066132730Skan 5067169699Skan arg_info->types = void_list_node; 5068169699Skan return arg_info; 506918334Speter } 507018334Speter 5071169699Skan if (!ellipsis) 5072169699Skan types = void_list_node; 507318334Speter 5074169699Skan /* Break up the bindings list into parms, tags, types, and others; 5075169699Skan apply sanity checks; purge the name-to-decl bindings. */ 5076169699Skan while (b) 507718334Speter { 5078169699Skan tree decl = b->decl; 5079169699Skan tree type = TREE_TYPE (decl); 5080132730Skan const char *keyword; 5081132730Skan 5082169699Skan switch (TREE_CODE (decl)) 508390075Sobrien { 5084169699Skan case PARM_DECL: 5085169699Skan if (b->id) 5086169699Skan { 5087169699Skan gcc_assert (I_SYMBOL_BINDING (b->id) == b); 5088169699Skan I_SYMBOL_BINDING (b->id) = b->shadowed; 5089169699Skan } 5090132730Skan 5091169699Skan /* Check for forward decls that never got their actual decl. */ 5092169699Skan if (TREE_ASM_WRITTEN (decl)) 5093169699Skan error ("parameter %q+D has just a forward declaration", decl); 5094169699Skan /* Check for (..., void, ...) and issue an error. */ 5095169699Skan else if (VOID_TYPE_P (type) && !DECL_NAME (decl)) 5096169699Skan { 5097169699Skan if (!gave_void_only_once_err) 5098169699Skan { 5099169699Skan error ("%<void%> must be the only parameter"); 5100169699Skan gave_void_only_once_err = true; 5101169699Skan } 5102169699Skan } 5103169699Skan else 5104169699Skan { 5105169699Skan /* Valid parameter, add it to the list. */ 5106169699Skan TREE_CHAIN (decl) = parms; 5107169699Skan parms = decl; 5108132730Skan 5109169699Skan /* Since there is a prototype, args are passed in their 5110169699Skan declared types. The back end may override this later. */ 5111169699Skan DECL_ARG_TYPE (decl) = type; 5112169699Skan types = tree_cons (0, type, types); 5113169699Skan } 5114169699Skan break; 5115169699Skan 5116169699Skan case ENUMERAL_TYPE: keyword = "enum"; goto tag; 5117169699Skan case UNION_TYPE: keyword = "union"; goto tag; 5118169699Skan case RECORD_TYPE: keyword = "struct"; goto tag; 5119169699Skan tag: 5120169699Skan /* Types may not have tag-names, in which case the type 5121169699Skan appears in the bindings list with b->id NULL. */ 5122169699Skan if (b->id) 5123169699Skan { 5124169699Skan gcc_assert (I_TAG_BINDING (b->id) == b); 5125169699Skan I_TAG_BINDING (b->id) = b->shadowed; 5126169699Skan } 5127169699Skan 5128169699Skan /* Warn about any struct, union or enum tags defined in a 5129169699Skan parameter list. The scope of such types is limited to 5130169699Skan the parameter list, which is rarely if ever desirable 5131169699Skan (it's impossible to call such a function with type- 5132169699Skan correct arguments). An anonymous union parm type is 5133169699Skan meaningful as a GNU extension, so don't warn for that. */ 5134169699Skan if (TREE_CODE (decl) != UNION_TYPE || b->id != 0) 5135169699Skan { 5136169699Skan if (b->id) 5137169699Skan /* The %s will be one of 'struct', 'union', or 'enum'. */ 5138169699Skan warning (0, "%<%s %E%> declared inside parameter list", 5139169699Skan keyword, b->id); 5140169699Skan else 5141169699Skan /* The %s will be one of 'struct', 'union', or 'enum'. */ 5142169699Skan warning (0, "anonymous %s declared inside parameter list", 5143169699Skan keyword); 5144169699Skan 5145169699Skan if (!explained_incomplete_types) 5146169699Skan { 5147169699Skan warning (0, "its scope is only this definition or declaration," 5148169699Skan " which is probably not what you want"); 5149169699Skan explained_incomplete_types = true; 5150169699Skan } 5151169699Skan } 5152169699Skan 5153169699Skan tags = tree_cons (b->id, decl, tags); 5154169699Skan break; 5155169699Skan 5156169699Skan case CONST_DECL: 5157169699Skan case TYPE_DECL: 5158169699Skan case FUNCTION_DECL: 5159169699Skan /* CONST_DECLs appear here when we have an embedded enum, 5160169699Skan and TYPE_DECLs appear here when we have an embedded struct 5161169699Skan or union. No warnings for this - we already warned about the 5162169699Skan type itself. FUNCTION_DECLs appear when there is an implicit 5163169699Skan function declaration in the parameter list. */ 5164169699Skan 5165169699Skan TREE_CHAIN (decl) = others; 5166169699Skan others = decl; 5167169699Skan /* fall through */ 5168169699Skan 5169169699Skan case ERROR_MARK: 5170169699Skan /* error_mark_node appears here when we have an undeclared 5171169699Skan variable. Just throw it away. */ 5172169699Skan if (b->id) 5173169699Skan { 5174169699Skan gcc_assert (I_SYMBOL_BINDING (b->id) == b); 5175169699Skan I_SYMBOL_BINDING (b->id) = b->shadowed; 5176169699Skan } 5177169699Skan break; 5178169699Skan 5179169699Skan /* Other things that might be encountered. */ 5180169699Skan case LABEL_DECL: 5181169699Skan case VAR_DECL: 5182169699Skan default: 5183169699Skan gcc_unreachable (); 518418334Speter } 5185132730Skan 5186169699Skan b = free_binding_and_advance (b); 5187132730Skan } 5188132730Skan 5189169699Skan arg_info->parms = parms; 5190169699Skan arg_info->tags = tags; 5191169699Skan arg_info->types = types; 5192169699Skan arg_info->others = others; 5193169699Skan arg_info->pending_sizes = get_pending_sizes (); 5194169699Skan return arg_info; 519518334Speter} 519618334Speter 519718334Speter/* Get the struct, enum or union (CODE says which) with tag NAME. 5198169699Skan Define the tag as a forward-reference if it is not defined. 5199169699Skan Return a c_typespec structure for the type specifier. */ 520018334Speter 5201169699Skanstruct c_typespec 5202169699Skanparser_xref_tag (enum tree_code code, tree name) 520318334Speter{ 5204169699Skan struct c_typespec ret; 520518334Speter /* If a cross reference is requested, look up the type 520618334Speter already defined for this tag and return it. */ 520718334Speter 5208132730Skan tree ref = lookup_tag (code, name, 0); 520990075Sobrien /* If this is the right type of tag, return what we found. 521090075Sobrien (This reference will be shadowed by shadow_tag later if appropriate.) 521190075Sobrien If this is the wrong type of tag, do not return it. If it was the 5212132730Skan wrong type in the same scope, we will have had an error 5213132730Skan message already; if in a different scope and declaring 521490075Sobrien a name, pending_xref_error will give an error message; but if in a 5215132730Skan different scope and not declaring a name, this tag should 521690075Sobrien shadow the previous declaration of a different type of tag, and 521790075Sobrien this would not work properly if we return the reference found. 521890075Sobrien (For example, with "struct foo" in an outer scope, "union foo;" 521990075Sobrien must shadow that tag with a new one of union type.) */ 5220169699Skan ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref); 522190075Sobrien if (ref && TREE_CODE (ref) == code) 5222169699Skan { 5223169699Skan ret.spec = ref; 5224169699Skan return ret; 5225169699Skan } 522618334Speter 522718334Speter /* If no such tag is yet defined, create a forward-reference node 522818334Speter and record it as the "definition". 522918334Speter When a real declaration of this type is found, 523018334Speter the forward-reference will be altered into a real type. */ 523118334Speter 523218334Speter ref = make_node (code); 523318334Speter if (code == ENUMERAL_TYPE) 523418334Speter { 523518334Speter /* Give the type a default layout like unsigned int 523618334Speter to avoid crashing if it does not get defined. */ 523718334Speter TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node); 523818334Speter TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); 523990075Sobrien TYPE_USER_ALIGN (ref) = 0; 5240169699Skan TYPE_UNSIGNED (ref) = 1; 524118334Speter TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); 524218334Speter TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); 524318334Speter TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); 524418334Speter } 524518334Speter 524618334Speter pushtag (name, ref); 524718334Speter 5248169699Skan ret.spec = ref; 5249169699Skan return ret; 525018334Speter} 5251169699Skan 5252169699Skan/* Get the struct, enum or union (CODE says which) with tag NAME. 5253169699Skan Define the tag as a forward-reference if it is not defined. 5254169699Skan Return a tree for the type. */ 5255169699Skan 5256169699Skantree 5257169699Skanxref_tag (enum tree_code code, tree name) 5258169699Skan{ 5259169699Skan return parser_xref_tag (code, name).spec; 5260169699Skan} 526118334Speter 5262132730Skan/* Make sure that the tag NAME is defined *in the current scope* 526318334Speter at least as a forward reference. 526490075Sobrien CODE says which kind of tag NAME ought to be. */ 526518334Speter 526618334Spetertree 5267132730Skanstart_struct (enum tree_code code, tree name) 526818334Speter{ 5269132730Skan /* If there is already a tag defined at this scope 527018334Speter (as a forward reference), just return it. */ 527118334Speter 527290075Sobrien tree ref = 0; 527318334Speter 527418334Speter if (name != 0) 5275132730Skan ref = lookup_tag (code, name, 1); 527618334Speter if (ref && TREE_CODE (ref) == code) 527718334Speter { 5278161660Skan if (TYPE_SIZE (ref)) 5279169699Skan { 528090075Sobrien if (code == UNION_TYPE) 5281169699Skan error ("redefinition of %<union %E%>", name); 5282169699Skan else 5283169699Skan error ("redefinition of %<struct %E%>", name); 5284132730Skan } 5285161660Skan else if (C_TYPE_BEING_DEFINED (ref)) 5286161660Skan { 5287161660Skan if (code == UNION_TYPE) 5288169699Skan error ("nested redefinition of %<union %E%>", name); 5289169699Skan else 5290169699Skan error ("nested redefinition of %<struct %E%>", name); 5291161660Skan } 5292117421Skan } 5293117421Skan else 5294117421Skan { 5295117421Skan /* Otherwise create a forward-reference just so the tag is in scope. */ 529618334Speter 5297117421Skan ref = make_node (code); 5298117421Skan pushtag (name, ref); 529918334Speter } 5300132730Skan 530118334Speter C_TYPE_BEING_DEFINED (ref) = 1; 530250397Sobrien TYPE_PACKED (ref) = flag_pack_struct; 530318334Speter return ref; 530418334Speter} 530518334Speter 5306169699Skan/* Process the specs, declarator and width (NULL if omitted) 530718334Speter of a structure component, returning a FIELD_DECL node. 5308132730Skan WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node. 530918334Speter 531018334Speter This is done during the parsing of the struct declaration. 531118334Speter The FIELD_DECL nodes are chained together and the lot of them 531218334Speter are ultimately passed to `build_struct' to make the RECORD_TYPE node. */ 531318334Speter 531418334Spetertree 5315169699Skangrokfield (struct c_declarator *declarator, struct c_declspecs *declspecs, 5316169699Skan tree width) 531718334Speter{ 531818334Speter tree value; 531918334Speter 5320169699Skan if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE 5321169699Skan && width == NULL_TREE) 532290075Sobrien { 5323117421Skan /* This is an unnamed decl. 5324117421Skan 5325117421Skan If we have something of the form "union { list } ;" then this 5326117421Skan is the anonymous union extension. Similarly for struct. 5327117421Skan 5328117421Skan If this is something of the form "struct foo;", then 5329117421Skan If MS extensions are enabled, this is handled as an 5330117421Skan anonymous struct. 5331117421Skan Otherwise this is a forward declaration of a structure tag. 5332117421Skan 5333117421Skan If this is something of the form "foo;" and foo is a TYPE_DECL, then 5334117421Skan If MS extensions are enabled and foo names a structure, then 5335117421Skan again this is an anonymous struct. 5336117421Skan Otherwise this is an error. 5337117421Skan 5338132730Skan Oh what a horrid tangled web we weave. I wonder if MS consciously 5339117421Skan took this from Plan 9 or if it was an accident of implementation 5340117421Skan that took root before someone noticed the bug... */ 5341117421Skan 5342169699Skan tree type = declspecs->type; 5343169699Skan bool type_ok = (TREE_CODE (type) == RECORD_TYPE 5344169699Skan || TREE_CODE (type) == UNION_TYPE); 5345169699Skan bool ok = false; 5346102790Skan 5347169699Skan if (type_ok 5348169699Skan && (flag_ms_extensions || !declspecs->typedef_p)) 534990075Sobrien { 5350117421Skan if (flag_ms_extensions) 5351169699Skan ok = true; 5352117421Skan else if (flag_iso) 5353169699Skan ok = false; 5354117421Skan else if (TYPE_NAME (type) == NULL) 5355169699Skan ok = true; 5356117421Skan else 5357169699Skan ok = false; 5358117421Skan } 5359169699Skan if (!ok) 5360117421Skan { 5361169699Skan pedwarn ("declaration does not declare anything"); 536290075Sobrien return NULL_TREE; 536390075Sobrien } 5364169699Skan if (pedantic) 5365169699Skan pedwarn ("ISO C doesn%'t support unnamed structs/unions"); 536690075Sobrien } 536718334Speter 5368169699Skan value = grokdeclarator (declarator, declspecs, FIELD, false, 5369132730Skan width ? &width : NULL); 537018334Speter 537118334Speter finish_decl (value, NULL_TREE, NULL_TREE); 537296263Sobrien DECL_INITIAL (value) = width; 537318334Speter 537418334Speter return value; 537518334Speter} 537618334Speter 5377132730Skan/* Generate an error for any duplicate field names in FIELDLIST. Munge 5378132730Skan the list such that this does not present a problem later. */ 5379122196Skan 5380132730Skanstatic void 5381132730Skandetect_field_duplicates (tree fieldlist) 5382132730Skan{ 5383132730Skan tree x, y; 5384132730Skan int timeout = 10; 5385122196Skan 5386132730Skan /* First, see if there are more than "a few" fields. 5387132730Skan This is trivially true if there are zero or one fields. */ 5388132730Skan if (!fieldlist) 5389132730Skan return; 5390132730Skan x = TREE_CHAIN (fieldlist); 5391132730Skan if (!x) 5392132730Skan return; 5393132730Skan do { 5394132730Skan timeout--; 5395132730Skan x = TREE_CHAIN (x); 5396132730Skan } while (timeout > 0 && x); 5397132730Skan 5398132730Skan /* If there were "few" fields, avoid the overhead of allocating 5399132730Skan a hash table. Instead just do the nested traversal thing. */ 5400132730Skan if (timeout > 0) 5401132730Skan { 5402132730Skan for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x)) 5403132730Skan if (DECL_NAME (x)) 5404132730Skan { 5405132730Skan for (y = fieldlist; y != x; y = TREE_CHAIN (y)) 5406132730Skan if (DECL_NAME (y) == DECL_NAME (x)) 5407132730Skan { 5408169699Skan error ("duplicate member %q+D", x); 5409132730Skan DECL_NAME (x) = NULL_TREE; 5410132730Skan } 5411132730Skan } 5412132730Skan } 5413132730Skan else 5414132730Skan { 5415132730Skan htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); 5416132730Skan void **slot; 5417132730Skan 5418132730Skan for (x = fieldlist; x ; x = TREE_CHAIN (x)) 5419132730Skan if ((y = DECL_NAME (x)) != 0) 5420132730Skan { 5421132730Skan slot = htab_find_slot (htab, y, INSERT); 5422132730Skan if (*slot) 5423132730Skan { 5424169699Skan error ("duplicate member %q+D", x); 5425132730Skan DECL_NAME (x) = NULL_TREE; 5426132730Skan } 5427132730Skan *slot = y; 5428132730Skan } 5429132730Skan 5430132730Skan htab_delete (htab); 5431132730Skan } 5432122196Skan} 5433132730Skan 543418334Speter/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. 543518334Speter FIELDLIST is a chain of FIELD_DECL nodes for the fields. 543690075Sobrien ATTRIBUTES are attributes to be applied to the structure. */ 543718334Speter 543818334Spetertree 5439132730Skanfinish_struct (tree t, tree fieldlist, tree attributes) 544018334Speter{ 544190075Sobrien tree x; 5442169699Skan bool toplevel = file_scope == current_scope; 544390075Sobrien int saw_named_field; 544418334Speter 544518334Speter /* If this type was previously laid out as a forward reference, 544618334Speter make sure we lay it out again. */ 544718334Speter 544818334Speter TYPE_SIZE (t) = 0; 544918334Speter 545090075Sobrien decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 545118334Speter 545250397Sobrien if (pedantic) 545350397Sobrien { 545450397Sobrien for (x = fieldlist; x; x = TREE_CHAIN (x)) 545550397Sobrien if (DECL_NAME (x) != 0) 545650397Sobrien break; 545718334Speter 545850397Sobrien if (x == 0) 5459169699Skan { 5460169699Skan if (TREE_CODE (t) == UNION_TYPE) 5461169699Skan { 5462169699Skan if (fieldlist) 5463169699Skan pedwarn ("union has no named members"); 5464169699Skan else 5465169699Skan pedwarn ("union has no members"); 5466169699Skan } 5467169699Skan else 5468169699Skan { 5469169699Skan if (fieldlist) 5470169699Skan pedwarn ("struct has no named members"); 5471169699Skan else 5472169699Skan pedwarn ("struct has no members"); 5473169699Skan } 5474169699Skan } 547550397Sobrien } 547650397Sobrien 547796263Sobrien /* Install struct as DECL_CONTEXT of each field decl. 5478169699Skan Also process specified field sizes, found in the DECL_INITIAL, 5479169699Skan storing 0 there after the type has been changed to precision equal 5480169699Skan to its width, rather than the precision of the specified standard 5481169699Skan type. (Correct layout requires the original type to have been preserved 5482169699Skan until now.) */ 548318334Speter 548490075Sobrien saw_named_field = 0; 548518334Speter for (x = fieldlist; x; x = TREE_CHAIN (x)) 548618334Speter { 5487169699Skan if (TREE_TYPE (x) == error_mark_node) 5488169699Skan continue; 5489169699Skan 549018334Speter DECL_CONTEXT (x) = t; 549118334Speter 5492169699Skan if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT) 5493169699Skan DECL_PACKED (x) = 1; 5494169699Skan 549518334Speter /* If any field is const, the structure type is pseudo-const. */ 549618334Speter if (TREE_READONLY (x)) 549718334Speter C_TYPE_FIELDS_READONLY (t) = 1; 549818334Speter else 549918334Speter { 550018334Speter /* A field that is pseudo-const makes the structure likewise. */ 550118334Speter tree t1 = TREE_TYPE (x); 550218334Speter while (TREE_CODE (t1) == ARRAY_TYPE) 550318334Speter t1 = TREE_TYPE (t1); 550418334Speter if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE) 550518334Speter && C_TYPE_FIELDS_READONLY (t1)) 550618334Speter C_TYPE_FIELDS_READONLY (t) = 1; 550718334Speter } 550818334Speter 550918334Speter /* Any field that is volatile means variables of this type must be 551018334Speter treated in some ways as volatile. */ 551118334Speter if (TREE_THIS_VOLATILE (x)) 551218334Speter C_TYPE_FIELDS_VOLATILE (t) = 1; 551318334Speter 551418334Speter /* Any field of nominal variable size implies structure is too. */ 551518334Speter if (C_DECL_VARIABLE_SIZE (x)) 551618334Speter C_TYPE_VARIABLE_SIZE (t) = 1; 551718334Speter 551896263Sobrien if (DECL_INITIAL (x)) 551918334Speter { 5520132730Skan unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1); 5521132730Skan DECL_SIZE (x) = bitsize_int (width); 5522132730Skan DECL_BIT_FIELD (x) = 1; 5523132730Skan SET_DECL_C_BIT_FIELD (x); 552496263Sobrien } 552596263Sobrien 552690075Sobrien /* Detect flexible array member in an invalid context. */ 552790075Sobrien if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 552890075Sobrien && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 552990075Sobrien && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 553090075Sobrien && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 553118334Speter { 553290075Sobrien if (TREE_CODE (t) == UNION_TYPE) 5533132730Skan { 5534132730Skan error ("%Jflexible array member in union", x); 5535132730Skan TREE_TYPE (x) = error_mark_node; 5536132730Skan } 553790075Sobrien else if (TREE_CHAIN (x) != NULL_TREE) 5538132730Skan { 5539132730Skan error ("%Jflexible array member not at end of struct", x); 5540132730Skan TREE_TYPE (x) = error_mark_node; 5541132730Skan } 5542169699Skan else if (!saw_named_field) 5543132730Skan { 5544132730Skan error ("%Jflexible array member in otherwise empty struct", x); 5545132730Skan TREE_TYPE (x) = error_mark_node; 5546132730Skan } 554718334Speter } 5548117421Skan 5549132730Skan if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE 5550117421Skan && flexible_array_type_p (TREE_TYPE (x))) 5551132730Skan pedwarn ("%Jinvalid use of structure with flexible array member", x); 5552117421Skan 555390075Sobrien if (DECL_NAME (x)) 555490075Sobrien saw_named_field = 1; 555518334Speter } 555618334Speter 5557132730Skan detect_field_duplicates (fieldlist); 555890075Sobrien 555918334Speter /* Now we have the nearly final fieldlist. Record it, 556018334Speter then lay out the structure or union (including the fields). */ 556118334Speter 556218334Speter TYPE_FIELDS (t) = fieldlist; 556318334Speter 556418334Speter layout_type (t); 556518334Speter 5566169699Skan /* Give bit-fields their proper types. */ 556790075Sobrien { 556890075Sobrien tree *fieldlistp = &fieldlist; 556990075Sobrien while (*fieldlistp) 5570169699Skan if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp) 5571169699Skan && TREE_TYPE (*fieldlistp) != error_mark_node) 5572169699Skan { 5573169699Skan unsigned HOST_WIDE_INT width 5574169699Skan = tree_low_cst (DECL_INITIAL (*fieldlistp), 1); 5575169699Skan tree type = TREE_TYPE (*fieldlistp); 5576169699Skan if (width != TYPE_PRECISION (type)) 5577169699Skan { 5578169699Skan TREE_TYPE (*fieldlistp) 5579169699Skan = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type)); 5580169699Skan DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp)); 5581169699Skan } 5582169699Skan DECL_INITIAL (*fieldlistp) = 0; 5583169699Skan } 558490075Sobrien else 558590075Sobrien fieldlistp = &TREE_CHAIN (*fieldlistp); 558690075Sobrien } 558718334Speter 558890075Sobrien /* Now we have the truly final field list. 558990075Sobrien Store it in this type and in the variants. */ 559018334Speter 559118334Speter TYPE_FIELDS (t) = fieldlist; 559218334Speter 5593122196Skan /* If there are lots of fields, sort so we can look through them fast. 5594132730Skan We arbitrarily consider 16 or more elts to be "a lot". */ 5595122196Skan 5596122196Skan { 5597122196Skan int len = 0; 5598122196Skan 5599122196Skan for (x = fieldlist; x; x = TREE_CHAIN (x)) 5600122196Skan { 5601169699Skan if (len > 15 || DECL_NAME (x) == NULL) 5602169699Skan break; 5603169699Skan len += 1; 5604122196Skan } 5605122196Skan 5606122196Skan if (len > 15) 5607122196Skan { 5608169699Skan tree *field_array; 5609169699Skan struct lang_type *space; 5610169699Skan struct sorted_fields_type *space2; 5611132730Skan 5612169699Skan len += list_length (x); 5613132730Skan 5614169699Skan /* Use the same allocation policy here that make_node uses, to 5615169699Skan ensure that this lives as long as the rest of the struct decl. 5616169699Skan All decls in an inline function need to be saved. */ 5617132730Skan 5618169699Skan space = GGC_CNEW (struct lang_type); 5619169699Skan space2 = GGC_NEWVAR (struct sorted_fields_type, 5620169699Skan sizeof (struct sorted_fields_type) + len * sizeof (tree)); 5621132730Skan 5622169699Skan len = 0; 5623132730Skan space->s = space2; 5624132730Skan field_array = &space2->elts[0]; 5625169699Skan for (x = fieldlist; x; x = TREE_CHAIN (x)) 5626169699Skan { 5627169699Skan field_array[len++] = x; 5628132730Skan 5629169699Skan /* If there is anonymous struct or union, break out of the loop. */ 5630169699Skan if (DECL_NAME (x) == NULL) 5631169699Skan break; 5632169699Skan } 5633169699Skan /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */ 5634169699Skan if (x == NULL) 5635169699Skan { 5636169699Skan TYPE_LANG_SPECIFIC (t) = space; 5637169699Skan TYPE_LANG_SPECIFIC (t)->s->len = len; 5638169699Skan field_array = TYPE_LANG_SPECIFIC (t)->s->elts; 5639169699Skan qsort (field_array, len, sizeof (tree), field_decl_cmp); 5640169699Skan } 5641122196Skan } 5642122196Skan } 5643132730Skan 564418334Speter for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) 564518334Speter { 564618334Speter TYPE_FIELDS (x) = TYPE_FIELDS (t); 564718334Speter TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t); 5648161660Skan C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t); 5649161660Skan C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t); 5650161660Skan C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t); 565118334Speter } 565218334Speter 565318334Speter /* If this was supposed to be a transparent union, but we can't 565418334Speter make it one, warn and turn off the flag. */ 565518334Speter if (TREE_CODE (t) == UNION_TYPE 565618334Speter && TYPE_TRANSPARENT_UNION (t) 5657146906Skan && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))) 565818334Speter { 565918334Speter TYPE_TRANSPARENT_UNION (t) = 0; 5660169699Skan warning (0, "union cannot be made transparent"); 566118334Speter } 566218334Speter 566318334Speter /* If this structure or union completes the type of any previous 566418334Speter variable declaration, lay it out and output its rtl. */ 5665132730Skan for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)); 5666132730Skan x; 5667132730Skan x = TREE_CHAIN (x)) 566818334Speter { 5669132730Skan tree decl = TREE_VALUE (x); 5670132730Skan if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) 5671132730Skan layout_array_type (TREE_TYPE (decl)); 5672132730Skan if (TREE_CODE (decl) != TYPE_DECL) 5673132730Skan { 5674132730Skan layout_decl (decl, 0); 5675132730Skan if (c_dialect_objc ()) 5676132730Skan objc_check_decl (decl); 5677169699Skan rest_of_decl_compilation (decl, toplevel, 0); 5678169699Skan if (!toplevel) 5679132730Skan expand_decl (decl); 568018334Speter } 568118334Speter } 5682132730Skan C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0; 568318334Speter 568418334Speter /* Finish debugging output for this type. */ 568518334Speter rest_of_type_compilation (t, toplevel); 568618334Speter 5687169699Skan /* If we're inside a function proper, i.e. not file-scope and not still 5688169699Skan parsing parameters, then arrange for the size of a variable sized type 5689169699Skan to be bound now. */ 5690169699Skan if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE)) 5691169699Skan add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t))); 5692169699Skan 569318334Speter return t; 569418334Speter} 569518334Speter 569618334Speter/* Lay out the type T, and its element type, and so on. */ 569718334Speter 569818334Speterstatic void 5699132730Skanlayout_array_type (tree t) 570018334Speter{ 570118334Speter if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 570218334Speter layout_array_type (TREE_TYPE (t)); 570318334Speter layout_type (t); 570418334Speter} 570518334Speter 570618334Speter/* Begin compiling the definition of an enumeration type. 570718334Speter NAME is its name (or null if anonymous). 570818334Speter Returns the type object, as yet incomplete. 570918334Speter Also records info about it so that build_enumerator 571018334Speter may be used to declare the individual values as they are read. */ 571118334Speter 571218334Spetertree 5713132730Skanstart_enum (tree name) 571418334Speter{ 571590075Sobrien tree enumtype = 0; 571618334Speter 571718334Speter /* If this is the real definition for a previous forward reference, 571818334Speter fill in the contents in the same object that used to be the 571918334Speter forward reference. */ 572018334Speter 572118334Speter if (name != 0) 5722132730Skan enumtype = lookup_tag (ENUMERAL_TYPE, name, 1); 572318334Speter 572418334Speter if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE) 572518334Speter { 572618334Speter enumtype = make_node (ENUMERAL_TYPE); 572718334Speter pushtag (name, enumtype); 572818334Speter } 572918334Speter 5730161660Skan if (C_TYPE_BEING_DEFINED (enumtype)) 5731169699Skan error ("nested redefinition of %<enum %E%>", name); 5732161660Skan 573318334Speter C_TYPE_BEING_DEFINED (enumtype) = 1; 573418334Speter 573518334Speter if (TYPE_VALUES (enumtype) != 0) 573618334Speter { 573718334Speter /* This enum is a named one that has been declared already. */ 5738169699Skan error ("redeclaration of %<enum %E%>", name); 573918334Speter 574018334Speter /* Completely replace its old definition. 574118334Speter The old enumerators remain defined, however. */ 574218334Speter TYPE_VALUES (enumtype) = 0; 574318334Speter } 574418334Speter 574518334Speter enum_next_value = integer_zero_node; 574618334Speter enum_overflow = 0; 574718334Speter 574850397Sobrien if (flag_short_enums) 574950397Sobrien TYPE_PACKED (enumtype) = 1; 575050397Sobrien 575118334Speter return enumtype; 575218334Speter} 575318334Speter 575418334Speter/* After processing and defining all the values of an enumeration type, 575518334Speter install their decls in the enumeration type and finish it off. 575618334Speter ENUMTYPE is the type object, VALUES a list of decl-value pairs, 575718334Speter and ATTRIBUTES are the specified attributes. 575818334Speter Returns ENUMTYPE. */ 575918334Speter 576018334Spetertree 5761132730Skanfinish_enum (tree enumtype, tree values, tree attributes) 576218334Speter{ 576390075Sobrien tree pair, tem; 5764169699Skan tree minnode = 0, maxnode = 0; 576590075Sobrien int precision, unsign; 5766169699Skan bool toplevel = (file_scope == current_scope); 5767169699Skan struct lang_type *lt; 576818334Speter 576990075Sobrien decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 577018334Speter 577118334Speter /* Calculate the maximum value of any enumerator in this type. */ 577218334Speter 577318334Speter if (values == error_mark_node) 577418334Speter minnode = maxnode = integer_zero_node; 577518334Speter else 577690075Sobrien { 577790075Sobrien minnode = maxnode = TREE_VALUE (values); 577890075Sobrien for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair)) 577990075Sobrien { 578090075Sobrien tree value = TREE_VALUE (pair); 578190075Sobrien if (tree_int_cst_lt (maxnode, value)) 578290075Sobrien maxnode = value; 578390075Sobrien if (tree_int_cst_lt (value, minnode)) 578490075Sobrien minnode = value; 578590075Sobrien } 578690075Sobrien } 578718334Speter 578890075Sobrien /* Construct the final type of this enumeration. It is the same 578990075Sobrien as one of the integral types - the narrowest one that fits, except 579090075Sobrien that normally we only go as narrow as int - and signed iff any of 579190075Sobrien the values are negative. */ 579290075Sobrien unsign = (tree_int_cst_sgn (minnode) >= 0); 579390075Sobrien precision = MAX (min_precision (minnode, unsign), 579490075Sobrien min_precision (maxnode, unsign)); 5795169699Skan 579650397Sobrien if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node)) 579750397Sobrien { 5798169699Skan tem = c_common_type_for_size (precision, unsign); 5799169699Skan if (tem == NULL) 580050397Sobrien { 5801169699Skan warning (0, "enumeration values exceed range of largest integer"); 5802169699Skan tem = long_long_integer_type_node; 580350397Sobrien } 580450397Sobrien } 580518334Speter else 5806169699Skan tem = unsign ? unsigned_type_node : integer_type_node; 580718334Speter 5808169699Skan TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem); 5809169699Skan TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem); 5810169699Skan TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem); 581118334Speter TYPE_SIZE (enumtype) = 0; 5812146906Skan 5813146906Skan /* If the precision of the type was specific with an attribute and it 5814146906Skan was too small, give an error. Otherwise, use it. */ 5815146906Skan if (TYPE_PRECISION (enumtype)) 5816146906Skan { 5817146906Skan if (precision > TYPE_PRECISION (enumtype)) 5818146906Skan error ("specified mode too small for enumeral values"); 5819146906Skan } 5820146906Skan else 5821169699Skan TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem); 5822146906Skan 582318334Speter layout_type (enumtype); 582418334Speter 582518334Speter if (values != error_mark_node) 582618334Speter { 582790075Sobrien /* Change the type of the enumerators to be the enum type. We 582890075Sobrien need to do this irrespective of the size of the enum, for 582990075Sobrien proper type checking. Replace the DECL_INITIALs of the 583090075Sobrien enumerators, and the value slots of the list, with copies 583190075Sobrien that have the enum type; they cannot be modified in place 583290075Sobrien because they may be shared (e.g. integer_zero_node) Finally, 583390075Sobrien change the purpose slots to point to the names of the decls. */ 583418334Speter for (pair = values; pair; pair = TREE_CHAIN (pair)) 583518334Speter { 583690075Sobrien tree enu = TREE_PURPOSE (pair); 5837169699Skan tree ini = DECL_INITIAL (enu); 583890075Sobrien 583990075Sobrien TREE_TYPE (enu) = enumtype; 584090075Sobrien 584190075Sobrien /* The ISO C Standard mandates enumerators to have type int, 584290075Sobrien even though the underlying type of an enum type is 584390075Sobrien unspecified. Here we convert any enumerators that fit in 584490075Sobrien an int to type int, to avoid promotions to unsigned types 584590075Sobrien when comparing integers with enumerators that fit in the 584690075Sobrien int range. When -pedantic is given, build_enumerator() 584790075Sobrien would have already taken care of those that don't fit. */ 5848169699Skan if (int_fits_type_p (ini, integer_type_node)) 5849169699Skan tem = integer_type_node; 585090075Sobrien else 5851169699Skan tem = enumtype; 5852169699Skan ini = convert (tem, ini); 585390075Sobrien 5854169699Skan DECL_INITIAL (enu) = ini; 585590075Sobrien TREE_PURPOSE (pair) = DECL_NAME (enu); 5856169699Skan TREE_VALUE (pair) = ini; 585718334Speter } 585818334Speter 585918334Speter TYPE_VALUES (enumtype) = values; 586018334Speter } 586118334Speter 5862169699Skan /* Record the min/max values so that we can warn about bit-field 5863169699Skan enumerations that are too small for the values. */ 5864169699Skan lt = GGC_CNEW (struct lang_type); 5865169699Skan lt->enum_min = minnode; 5866169699Skan lt->enum_max = maxnode; 5867169699Skan TYPE_LANG_SPECIFIC (enumtype) = lt; 5868169699Skan 586918334Speter /* Fix up all variant types of this enum type. */ 587018334Speter for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem)) 587118334Speter { 587290075Sobrien if (tem == enumtype) 587390075Sobrien continue; 587418334Speter TYPE_VALUES (tem) = TYPE_VALUES (enumtype); 587518334Speter TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype); 587618334Speter TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype); 587718334Speter TYPE_SIZE (tem) = TYPE_SIZE (enumtype); 587850397Sobrien TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype); 587918334Speter TYPE_MODE (tem) = TYPE_MODE (enumtype); 588018334Speter TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype); 588118334Speter TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype); 588290075Sobrien TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype); 5883169699Skan TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype); 5884169699Skan TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype); 588518334Speter } 588618334Speter 588718334Speter /* Finish debugging output for this type. */ 588818334Speter rest_of_type_compilation (enumtype, toplevel); 588918334Speter 589018334Speter return enumtype; 589118334Speter} 589218334Speter 589318334Speter/* Build and install a CONST_DECL for one value of the 589418334Speter current enumeration type (one that was begun with start_enum). 589518334Speter Return a tree-list containing the CONST_DECL and its value. 589618334Speter Assignment of sequential values by default is handled here. */ 589718334Speter 589818334Spetertree 5899132730Skanbuild_enumerator (tree name, tree value) 590018334Speter{ 590190075Sobrien tree decl, type; 590218334Speter 590318334Speter /* Validate and default VALUE. */ 590418334Speter 590518334Speter if (value != 0) 590618334Speter { 5907169699Skan /* Don't issue more errors for error_mark_node (i.e. an 5908169699Skan undeclared identifier) - just ignore the value expression. */ 5909169699Skan if (value == error_mark_node) 5910169699Skan value = 0; 5911169699Skan else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)) 5912169699Skan || TREE_CODE (value) != INTEGER_CST) 591318334Speter { 5914169699Skan error ("enumerator value for %qE is not an integer constant", name); 5915169699Skan value = 0; 591618334Speter } 591718334Speter else 591818334Speter { 5919169699Skan value = default_conversion (value); 5920169699Skan constant_expression_warning (value); 592118334Speter } 592218334Speter } 592318334Speter 592418334Speter /* Default based on previous value. */ 592518334Speter /* It should no longer be possible to have NON_LVALUE_EXPR 592618334Speter in the default. */ 592718334Speter if (value == 0) 592818334Speter { 592918334Speter value = enum_next_value; 593018334Speter if (enum_overflow) 593118334Speter error ("overflow in enumeration values"); 593218334Speter } 593318334Speter 5934169699Skan if (pedantic && !int_fits_type_p (value, integer_type_node)) 593518334Speter { 5936169699Skan pedwarn ("ISO C restricts enumerator values to range of %<int%>"); 5937169699Skan /* XXX This causes -pedantic to change the meaning of the program. 5938169699Skan Remove? -zw 2004-03-15 */ 593990075Sobrien value = convert (integer_type_node, value); 594018334Speter } 594118334Speter 594218334Speter /* Set basis for default for next value. */ 594318334Speter enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0); 594418334Speter enum_overflow = tree_int_cst_lt (enum_next_value, value); 594518334Speter 594618334Speter /* Now create a declaration for the enum value name. */ 594718334Speter 594818334Speter type = TREE_TYPE (value); 5949117421Skan type = c_common_type_for_size (MAX (TYPE_PRECISION (type), 5950117421Skan TYPE_PRECISION (integer_type_node)), 5951117421Skan (TYPE_PRECISION (type) 5952117421Skan >= TYPE_PRECISION (integer_type_node) 5953169699Skan && TYPE_UNSIGNED (type))); 595418334Speter 595518334Speter decl = build_decl (CONST_DECL, name, type); 595690075Sobrien DECL_INITIAL (decl) = convert (type, value); 595718334Speter pushdecl (decl); 595818334Speter 595990075Sobrien return tree_cons (decl, value, NULL_TREE); 596018334Speter} 596190075Sobrien 596218334Speter 596318334Speter/* Create the FUNCTION_DECL for a function definition. 596490075Sobrien DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of 596518334Speter the declaration; they describe the function's name and the type it returns, 596618334Speter but twisted together in a fashion that parallels the syntax of C. 596718334Speter 596818334Speter This function creates a binding context for the function body 596918334Speter as well as setting up the FUNCTION_DECL in current_function_decl. 597018334Speter 597118334Speter Returns 1 on success. If the DECLARATOR is not suitable for a function 597218334Speter (it defines a datum instead), we return 0, which tells 597390075Sobrien yyparse to report a parse error. */ 597418334Speter 597518334Speterint 5976169699Skanstart_function (struct c_declspecs *declspecs, struct c_declarator *declarator, 5977169699Skan tree attributes) 597818334Speter{ 597918334Speter tree decl1, old_decl; 5980169699Skan tree restype, resdecl; 5981169699Skan struct c_label_context_se *nstack_se; 5982169699Skan struct c_label_context_vm *nstack_vm; 598318334Speter 598450397Sobrien current_function_returns_value = 0; /* Assume, until we see it does. */ 598518334Speter current_function_returns_null = 0; 598696263Sobrien current_function_returns_abnormally = 0; 598718334Speter warn_about_return_type = 0; 598818334Speter current_extern_inline = 0; 5989169699Skan c_switch_stack = NULL; 599018334Speter 5991169699Skan nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se); 5992169699Skan nstack_se->labels_def = NULL; 5993169699Skan nstack_se->labels_used = NULL; 5994169699Skan nstack_se->next = label_context_stack_se; 5995169699Skan label_context_stack_se = nstack_se; 599618334Speter 5997169699Skan nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm); 5998169699Skan nstack_vm->labels_def = NULL; 5999169699Skan nstack_vm->labels_used = NULL; 6000169699Skan nstack_vm->scope = 0; 6001169699Skan nstack_vm->next = label_context_stack_vm; 6002169699Skan label_context_stack_vm = nstack_vm; 600318334Speter 6004169699Skan /* Indicate no valid break/continue context by setting these variables 6005169699Skan to some non-null, non-label value. We'll notice and emit the proper 6006169699Skan error message in c_finish_bc_stmt. */ 6007169699Skan c_break_label = c_cont_label = size_zero_node; 6008169699Skan 6009169699Skan decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL); 6010169699Skan 601118334Speter /* If the declarator is not suitable for a function definition, 601218334Speter cause a syntax error. */ 601318334Speter if (decl1 == 0) 601450397Sobrien { 6015169699Skan label_context_stack_se = label_context_stack_se->next; 6016169699Skan label_context_stack_vm = label_context_stack_vm->next; 601750397Sobrien return 0; 601850397Sobrien } 601918334Speter 602090075Sobrien decl_attributes (&decl1, attributes, 0); 602118334Speter 602290075Sobrien if (DECL_DECLARED_INLINE_P (decl1) 602390075Sobrien && DECL_UNINLINABLE (decl1) 602490075Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1))) 6025169699Skan warning (OPT_Wattributes, "inline function %q+D given attribute noinline", 6026169699Skan decl1); 602790075Sobrien 602818334Speter announce_function (decl1); 602918334Speter 603090075Sobrien if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1)))) 603118334Speter { 603290075Sobrien error ("return type is an incomplete type"); 603318334Speter /* Make it return void instead. */ 603418334Speter TREE_TYPE (decl1) 603518334Speter = build_function_type (void_type_node, 603618334Speter TYPE_ARG_TYPES (TREE_TYPE (decl1))); 603718334Speter } 603818334Speter 603918334Speter if (warn_about_return_type) 6040169699Skan pedwarn_c99 ("return type defaults to %<int%>"); 604118334Speter 604218334Speter /* Make the init_value nonzero so pushdecl knows this is not tentative. 6043169699Skan error_mark_node is replaced below (in pop_scope) with the BLOCK. */ 604418334Speter DECL_INITIAL (decl1) = error_mark_node; 604518334Speter 604618334Speter /* If this definition isn't a prototype and we had a prototype declaration 6047169699Skan before, copy the arg type info from that prototype. */ 6048169699Skan old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope); 6049169699Skan if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL) 6050169699Skan old_decl = 0; 6051169699Skan current_function_prototype_locus = UNKNOWN_LOCATION; 6052169699Skan current_function_prototype_built_in = false; 6053169699Skan current_function_prototype_arg_types = NULL_TREE; 6054169699Skan if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0) 605518334Speter { 6056169699Skan if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE 6057169699Skan && comptypes (TREE_TYPE (TREE_TYPE (decl1)), 6058169699Skan TREE_TYPE (TREE_TYPE (old_decl)))) 6059169699Skan { 6060169699Skan TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl), 6061169699Skan TREE_TYPE (decl1)); 6062169699Skan current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl); 6063169699Skan current_function_prototype_built_in 6064169699Skan = C_DECL_BUILTIN_PROTOTYPE (old_decl); 6065169699Skan current_function_prototype_arg_types 6066169699Skan = TYPE_ARG_TYPES (TREE_TYPE (decl1)); 6067169699Skan } 6068169699Skan if (TREE_PUBLIC (decl1)) 6069169699Skan { 6070169699Skan /* If there is an external prototype declaration of this 6071169699Skan function, record its location but do not copy information 6072169699Skan to this decl. This may be an invisible declaration 6073169699Skan (built-in or in a scope which has finished) or simply 6074169699Skan have more refined argument types than any declaration 6075169699Skan found above. */ 6076169699Skan struct c_binding *b; 6077169699Skan for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed) 6078169699Skan if (B_IN_SCOPE (b, external_scope)) 6079169699Skan break; 6080169699Skan if (b) 6081169699Skan { 6082169699Skan tree ext_decl, ext_type; 6083169699Skan ext_decl = b->decl; 6084169699Skan ext_type = b->type ? b->type : TREE_TYPE (ext_decl); 6085169699Skan if (TREE_CODE (ext_type) == FUNCTION_TYPE 6086169699Skan && comptypes (TREE_TYPE (TREE_TYPE (decl1)), 6087169699Skan TREE_TYPE (ext_type))) 6088169699Skan { 6089169699Skan current_function_prototype_locus 6090169699Skan = DECL_SOURCE_LOCATION (ext_decl); 6091169699Skan current_function_prototype_built_in 6092169699Skan = C_DECL_BUILTIN_PROTOTYPE (ext_decl); 6093169699Skan current_function_prototype_arg_types 6094169699Skan = TYPE_ARG_TYPES (ext_type); 6095169699Skan } 6096169699Skan } 6097169699Skan } 609818334Speter } 609918334Speter 610018334Speter /* Optionally warn of old-fashioned def with no previous prototype. */ 610118334Speter if (warn_strict_prototypes 6102169699Skan && old_decl != error_mark_node 610318334Speter && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0 6104132730Skan && C_DECL_ISNT_PROTOTYPE (old_decl)) 6105169699Skan warning (OPT_Wstrict_prototypes, 6106169699Skan "function declaration isn%'t a prototype"); 610718334Speter /* Optionally warn of any global def with no previous prototype. */ 610818334Speter else if (warn_missing_prototypes 6109169699Skan && old_decl != error_mark_node 611018334Speter && TREE_PUBLIC (decl1) 6111169699Skan && !MAIN_NAME_P (DECL_NAME (decl1)) 6112132730Skan && C_DECL_ISNT_PROTOTYPE (old_decl)) 6113169699Skan warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1); 611418334Speter /* Optionally warn of any def with no previous prototype 611518334Speter if the function has already been used. */ 611618334Speter else if (warn_missing_prototypes 6117169699Skan && old_decl != 0 6118169699Skan && old_decl != error_mark_node 6119169699Skan && TREE_USED (old_decl) 612018334Speter && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0) 6121169699Skan warning (OPT_Wmissing_prototypes, 6122169699Skan "%q+D was used with no prototype before its definition", decl1); 612318334Speter /* Optionally warn of any global def with no previous declaration. */ 612418334Speter else if (warn_missing_declarations 612518334Speter && TREE_PUBLIC (decl1) 612618334Speter && old_decl == 0 6127169699Skan && !MAIN_NAME_P (DECL_NAME (decl1))) 6128169699Skan warning (OPT_Wmissing_declarations, "no previous declaration for %q+D", 6129169699Skan decl1); 613018334Speter /* Optionally warn of any def with no previous declaration 613118334Speter if the function has already been used. */ 613218334Speter else if (warn_missing_declarations 6133169699Skan && old_decl != 0 6134169699Skan && old_decl != error_mark_node 6135169699Skan && TREE_USED (old_decl) 6136132730Skan && C_DECL_IMPLICIT (old_decl)) 6137169699Skan warning (OPT_Wmissing_declarations, 6138169699Skan "%q+D was used with no declaration before its definition", decl1); 613918334Speter 614018334Speter /* This is a definition, not a reference. 614118334Speter So normally clear DECL_EXTERNAL. 614218334Speter However, `extern inline' acts like a declaration 614318334Speter except for defining how to inline. So set DECL_EXTERNAL in that case. */ 614418334Speter DECL_EXTERNAL (decl1) = current_extern_inline; 614518334Speter 6146169699Skan /* C99 specified different behaviour for non-static inline 6147169699Skan functions, compared with the traditional GNU behaviour. We don't 6148169699Skan support the C99 behaviour, but we do warn about non-static inline 6149169699Skan functions here. The warning can be disabled via an explicit use 6150169699Skan of -fgnu89-inline, or by using the gnu_inline attribute. */ 6151169699Skan if (DECL_DECLARED_INLINE_P (decl1) 6152169699Skan && TREE_PUBLIC (decl1) 6153169699Skan && flag_isoc99 6154169699Skan && flag_gnu89_inline != 1 6155169699Skan && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1)) 6156169699Skan && diagnostic_report_warnings_p ()) 6157169699Skan { 6158169699Skan static bool info = false; 6159169699Skan 6160169699Skan warning (0, "C99 inline functions are not supported; using GNU89"); 6161169699Skan if (!info) 6162169699Skan { 6163169699Skan warning (0, 6164169699Skan "to disable this warning use -fgnu89-inline or " 6165169699Skan "the gnu_inline function attribute"); 6166169699Skan info = true; 6167169699Skan } 6168169699Skan } 6169169699Skan 617018334Speter /* This function exists in static storage. 617118334Speter (This does not mean `static' in the C sense!) */ 617218334Speter TREE_STATIC (decl1) = 1; 617318334Speter 617418334Speter /* A nested function is not global. */ 617518334Speter if (current_function_decl != 0) 617618334Speter TREE_PUBLIC (decl1) = 0; 617718334Speter 6178132730Skan /* This is the earliest point at which we might know the assembler 6179132730Skan name of the function. Thus, if it's set before this, die horribly. */ 6180169699Skan gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1)); 6181132730Skan 6182132730Skan /* If #pragma weak was used, mark the decl weak now. */ 6183169699Skan if (current_scope == file_scope) 6184132730Skan maybe_apply_pragma_weak (decl1); 6185132730Skan 618690075Sobrien /* Warn for unlikely, improbable, or stupid declarations of `main'. */ 618790075Sobrien if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1))) 618850397Sobrien { 618950397Sobrien tree args; 619050397Sobrien int argct = 0; 619150397Sobrien 619250397Sobrien if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1))) 619390075Sobrien != integer_type_node) 6194169699Skan pedwarn ("return type of %q+D is not %<int%>", decl1); 619550397Sobrien 619650397Sobrien for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args; 619750397Sobrien args = TREE_CHAIN (args)) 619850397Sobrien { 619950397Sobrien tree type = args ? TREE_VALUE (args) : 0; 620050397Sobrien 620150397Sobrien if (type == void_type_node) 620250397Sobrien break; 620350397Sobrien 620450397Sobrien ++argct; 620550397Sobrien switch (argct) 620650397Sobrien { 620750397Sobrien case 1: 620850397Sobrien if (TYPE_MAIN_VARIANT (type) != integer_type_node) 6209169699Skan pedwarn ("first argument of %q+D should be %<int%>", decl1); 621050397Sobrien break; 621150397Sobrien 621250397Sobrien case 2: 621350397Sobrien if (TREE_CODE (type) != POINTER_TYPE 621450397Sobrien || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 621550397Sobrien || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 621650397Sobrien != char_type_node)) 6217169699Skan pedwarn ("second argument of %q+D should be %<char **%>", 6218169699Skan decl1); 621950397Sobrien break; 622050397Sobrien 622150397Sobrien case 3: 622250397Sobrien if (TREE_CODE (type) != POINTER_TYPE 622350397Sobrien || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 622450397Sobrien || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 622550397Sobrien != char_type_node)) 6226169699Skan pedwarn ("third argument of %q+D should probably be " 6227169699Skan "%<char **%>", decl1); 622850397Sobrien break; 622950397Sobrien } 623050397Sobrien } 623150397Sobrien 623250397Sobrien /* It is intentional that this message does not mention the third 623390075Sobrien argument because it's only mentioned in an appendix of the 623490075Sobrien standard. */ 623550397Sobrien if (argct > 0 && (argct < 2 || argct > 3)) 6236169699Skan pedwarn ("%q+D takes only zero or two arguments", decl1); 623750397Sobrien 6238169699Skan if (!TREE_PUBLIC (decl1)) 6239169699Skan pedwarn ("%q+D is normally a non-static function", decl1); 624050397Sobrien } 624150397Sobrien 624218334Speter /* Record the decl so that the function name is defined. 624318334Speter If we already have a decl for this name, and it is a FUNCTION_DECL, 624418334Speter use the old decl. */ 624518334Speter 624618334Speter current_function_decl = pushdecl (decl1); 624718334Speter 6248169699Skan push_scope (); 6249132730Skan declare_parm_level (); 625018334Speter 625118334Speter restype = TREE_TYPE (TREE_TYPE (current_function_decl)); 625218334Speter /* Promote the value to int before returning it. */ 625390075Sobrien if (c_promoting_integer_type_p (restype)) 625418334Speter { 6255117421Skan /* It retains unsignedness if not really getting wider. */ 6256169699Skan if (TYPE_UNSIGNED (restype) 6257117421Skan && (TYPE_PRECISION (restype) 6258117421Skan == TYPE_PRECISION (integer_type_node))) 625918334Speter restype = unsigned_type_node; 626018334Speter else 626118334Speter restype = integer_type_node; 626218334Speter } 626318334Speter 6264169699Skan resdecl = build_decl (RESULT_DECL, NULL_TREE, restype); 6265169699Skan DECL_ARTIFICIAL (resdecl) = 1; 6266169699Skan DECL_IGNORED_P (resdecl) = 1; 6267169699Skan DECL_RESULT (current_function_decl) = resdecl; 626818334Speter 626990075Sobrien start_fname_decls (); 6270132730Skan 627118334Speter return 1; 627218334Speter} 627318334Speter 6274132730Skan/* Subroutine of store_parm_decls which handles new-style function 6275132730Skan definitions (prototype format). The parms already have decls, so we 6276132730Skan need only record them as in effect and complain if any redundant 6277132730Skan old-style parm decls were written. */ 6278132730Skanstatic void 6279169699Skanstore_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info) 628018334Speter{ 6281169699Skan tree decl; 628218334Speter 6283169699Skan if (current_scope->bindings) 6284132730Skan { 6285132730Skan error ("%Jold-style parameter declarations in prototyped " 6286132730Skan "function definition", fndecl); 628718334Speter 6288132730Skan /* Get rid of the old-style declarations. */ 6289169699Skan pop_scope (); 6290169699Skan push_scope (); 6291132730Skan } 6292169699Skan /* Don't issue this warning for nested functions, and don't issue this 6293169699Skan warning if we got here because ARG_INFO_TYPES was error_mark_node 6294169699Skan (this happens when a function definition has just an ellipsis in 6295169699Skan its parameter list). */ 6296169699Skan else if (!in_system_header && !current_function_scope 6297169699Skan && arg_info->types != error_mark_node) 6298169699Skan warning (OPT_Wtraditional, 6299169699Skan "%Jtraditional C rejects ISO C style function definitions", 6300169699Skan fndecl); 630118334Speter 6302132730Skan /* Now make all the parameter declarations visible in the function body. 6303132730Skan We can bypass most of the grunt work of pushdecl. */ 6304169699Skan for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl)) 6305132730Skan { 6306132730Skan DECL_CONTEXT (decl) = current_function_decl; 6307169699Skan if (DECL_NAME (decl)) 6308132730Skan { 6309169699Skan bind (DECL_NAME (decl), decl, current_scope, 6310169699Skan /*invisible=*/false, /*nested=*/false); 6311169699Skan if (!TREE_USED (decl)) 6312169699Skan warn_if_shadowing (decl); 6313132730Skan } 6314169699Skan else 6315169699Skan error ("%Jparameter name omitted", decl); 6316132730Skan } 631718334Speter 6318132730Skan /* Record the parameter list in the function declaration. */ 6319169699Skan DECL_ARGUMENTS (fndecl) = arg_info->parms; 632018334Speter 6321132730Skan /* Now make all the ancillary declarations visible, likewise. */ 6322169699Skan for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl)) 6323132730Skan { 6324132730Skan DECL_CONTEXT (decl) = current_function_decl; 6325169699Skan if (DECL_NAME (decl)) 6326169699Skan bind (DECL_NAME (decl), decl, current_scope, 6327169699Skan /*invisible=*/false, /*nested=*/false); 6328132730Skan } 632990075Sobrien 6330132730Skan /* And all the tag declarations. */ 6331169699Skan for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl)) 6332132730Skan if (TREE_PURPOSE (decl)) 6333169699Skan bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope, 6334169699Skan /*invisible=*/false, /*nested=*/false); 6335132730Skan} 633618334Speter 6337132730Skan/* Subroutine of store_parm_decls which handles old-style function 6338132730Skan definitions (separate parameter list and declarations). */ 633990075Sobrien 6340132730Skanstatic void 6341169699Skanstore_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info) 6342132730Skan{ 6343169699Skan struct c_binding *b; 6344132730Skan tree parm, decl, last; 6345169699Skan tree parmids = arg_info->parms; 6346169699Skan struct pointer_set_t *seen_args = pointer_set_create (); 634790075Sobrien 6348169699Skan if (!in_system_header) 6349169699Skan warning (OPT_Wold_style_definition, "%Jold-style function definition", 6350169699Skan fndecl); 635118334Speter 6352132730Skan /* Match each formal parameter name with its declaration. Save each 6353132730Skan decl in the appropriate TREE_PURPOSE slot of the parmids chain. */ 6354132730Skan for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 6355132730Skan { 6356132730Skan if (TREE_VALUE (parm) == 0) 635718334Speter { 6358132730Skan error ("%Jparameter name missing from parameter list", fndecl); 6359132730Skan TREE_PURPOSE (parm) = 0; 6360132730Skan continue; 636118334Speter } 636218334Speter 6363169699Skan b = I_SYMBOL_BINDING (TREE_VALUE (parm)); 6364169699Skan if (b && B_IN_CURRENT_SCOPE (b)) 636518334Speter { 6366169699Skan decl = b->decl; 6367132730Skan /* If we got something other than a PARM_DECL it is an error. */ 6368132730Skan if (TREE_CODE (decl) != PARM_DECL) 6369169699Skan error ("%q+D declared as a non-parameter", decl); 6370132730Skan /* If the declaration is already marked, we have a duplicate 6371132730Skan name. Complain and ignore the duplicate. */ 6372169699Skan else if (pointer_set_contains (seen_args, decl)) 637318334Speter { 6374169699Skan error ("multiple parameters named %q+D", decl); 6375132730Skan TREE_PURPOSE (parm) = 0; 6376132730Skan continue; 637718334Speter } 6378132730Skan /* If the declaration says "void", complain and turn it into 6379132730Skan an int. */ 6380132730Skan else if (VOID_TYPE_P (TREE_TYPE (decl))) 638118334Speter { 6382169699Skan error ("parameter %q+D declared with void type", decl); 6383132730Skan TREE_TYPE (decl) = integer_type_node; 6384132730Skan DECL_ARG_TYPE (decl) = integer_type_node; 6385132730Skan layout_decl (decl, 0); 638618334Speter } 6387169699Skan warn_if_shadowing (decl); 638818334Speter } 6389132730Skan /* If no declaration found, default to int. */ 6390132730Skan else 639118334Speter { 6392132730Skan decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node); 6393132730Skan DECL_ARG_TYPE (decl) = TREE_TYPE (decl); 6394132730Skan DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl); 6395132730Skan pushdecl (decl); 6396169699Skan warn_if_shadowing (decl); 639718334Speter 6398132730Skan if (flag_isoc99) 6399169699Skan pedwarn ("type of %q+D defaults to %<int%>", decl); 6400132730Skan else if (extra_warnings) 6401169699Skan warning (OPT_Wextra, "type of %q+D defaults to %<int%>", decl); 640218334Speter } 640318334Speter 6404132730Skan TREE_PURPOSE (parm) = decl; 6405169699Skan pointer_set_insert (seen_args, decl); 640618334Speter } 640718334Speter 6408132730Skan /* Now examine the parms chain for incomplete declarations 6409132730Skan and declarations with no corresponding names. */ 641018334Speter 6411169699Skan for (b = current_scope->bindings; b; b = b->prev) 6412132730Skan { 6413169699Skan parm = b->decl; 6414169699Skan if (TREE_CODE (parm) != PARM_DECL) 6415169699Skan continue; 6416169699Skan 6417169699Skan if (TREE_TYPE (parm) != error_mark_node 6418169699Skan && !COMPLETE_TYPE_P (TREE_TYPE (parm))) 6419132730Skan { 6420169699Skan error ("parameter %q+D has incomplete type", parm); 6421132730Skan TREE_TYPE (parm) = error_mark_node; 6422132730Skan } 642318334Speter 6424169699Skan if (!pointer_set_contains (seen_args, parm)) 642518334Speter { 6426169699Skan error ("declaration for parameter %q+D but no such parameter", parm); 642718334Speter 6428132730Skan /* Pretend the parameter was not missing. 6429132730Skan This gets us to a standard state and minimizes 6430132730Skan further error messages. */ 6431132730Skan parmids = chainon (parmids, tree_cons (parm, 0, 0)); 6432132730Skan } 6433132730Skan } 643418334Speter 6435132730Skan /* Chain the declarations together in the order of the list of 6436132730Skan names. Store that chain in the function decl, replacing the 6437132730Skan list of names. Update the current scope to match. */ 6438132730Skan DECL_ARGUMENTS (fndecl) = 0; 643918334Speter 6440132730Skan for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 6441132730Skan if (TREE_PURPOSE (parm)) 6442132730Skan break; 6443132730Skan if (parm && TREE_PURPOSE (parm)) 6444132730Skan { 6445132730Skan last = TREE_PURPOSE (parm); 6446132730Skan DECL_ARGUMENTS (fndecl) = last; 644718334Speter 6448132730Skan for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm)) 6449132730Skan if (TREE_PURPOSE (parm)) 6450132730Skan { 6451132730Skan TREE_CHAIN (last) = TREE_PURPOSE (parm); 6452132730Skan last = TREE_PURPOSE (parm); 6453132730Skan } 6454132730Skan TREE_CHAIN (last) = 0; 6455132730Skan } 645618334Speter 6457169699Skan pointer_set_destroy (seen_args); 6458169699Skan 6459132730Skan /* If there was a previous prototype, 6460132730Skan set the DECL_ARG_TYPE of each argument according to 6461132730Skan the type previously specified, and report any mismatches. */ 646218334Speter 6463169699Skan if (current_function_prototype_arg_types) 6464132730Skan { 6465132730Skan tree type; 6466132730Skan for (parm = DECL_ARGUMENTS (fndecl), 6467169699Skan type = current_function_prototype_arg_types; 6468132730Skan parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) 6469132730Skan != void_type_node)); 6470132730Skan parm = TREE_CHAIN (parm), type = TREE_CHAIN (type)) 647118334Speter { 6472132730Skan if (parm == 0 || type == 0 6473132730Skan || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node) 647418334Speter { 6475169699Skan if (current_function_prototype_built_in) 6476169699Skan warning (0, "number of arguments doesn%'t match " 6477169699Skan "built-in prototype"); 6478169699Skan else 6479169699Skan { 6480169699Skan error ("number of arguments doesn%'t match prototype"); 6481169699Skan error ("%Hprototype declaration", 6482169699Skan ¤t_function_prototype_locus); 6483169699Skan } 6484132730Skan break; 648518334Speter } 6486132730Skan /* Type for passing arg must be consistent with that 6487132730Skan declared for the arg. ISO C says we take the unqualified 6488132730Skan type for parameters declared with qualified type. */ 6489169699Skan if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)), 6490169699Skan TYPE_MAIN_VARIANT (TREE_VALUE (type)))) 649118334Speter { 6492132730Skan if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) 6493132730Skan == TYPE_MAIN_VARIANT (TREE_VALUE (type))) 649418334Speter { 6495132730Skan /* Adjust argument to match prototype. E.g. a previous 6496132730Skan `int foo(float);' prototype causes 6497132730Skan `int foo(x) float x; {...}' to be treated like 6498132730Skan `int foo(float x) {...}'. This is particularly 6499132730Skan useful for argument types like uid_t. */ 6500132730Skan DECL_ARG_TYPE (parm) = TREE_TYPE (parm); 650190075Sobrien 6502132730Skan if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl)) 6503132730Skan && INTEGRAL_TYPE_P (TREE_TYPE (parm)) 6504132730Skan && TYPE_PRECISION (TREE_TYPE (parm)) 6505132730Skan < TYPE_PRECISION (integer_type_node)) 6506132730Skan DECL_ARG_TYPE (parm) = integer_type_node; 650790075Sobrien 6508132730Skan if (pedantic) 650918334Speter { 6510169699Skan /* ??? Is it possible to get here with a 6511169699Skan built-in prototype or will it always have 6512169699Skan been diagnosed as conflicting with an 6513169699Skan old-style definition and discarded? */ 6514169699Skan if (current_function_prototype_built_in) 6515169699Skan warning (0, "promoted argument %qD " 6516169699Skan "doesn%'t match built-in prototype", parm); 6517169699Skan else 6518169699Skan { 6519169699Skan pedwarn ("promoted argument %qD " 6520169699Skan "doesn%'t match prototype", parm); 6521169699Skan pedwarn ("%Hprototype declaration", 6522169699Skan ¤t_function_prototype_locus); 6523169699Skan } 652418334Speter } 652518334Speter } 6526132730Skan else 6527132730Skan { 6528169699Skan if (current_function_prototype_built_in) 6529169699Skan warning (0, "argument %qD doesn%'t match " 6530169699Skan "built-in prototype", parm); 6531169699Skan else 6532169699Skan { 6533169699Skan error ("argument %qD doesn%'t match prototype", parm); 6534169699Skan error ("%Hprototype declaration", 6535169699Skan ¤t_function_prototype_locus); 6536169699Skan } 6537132730Skan } 653818334Speter } 653918334Speter } 6540132730Skan TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0; 6541132730Skan } 654218334Speter 6543132730Skan /* Otherwise, create a prototype that would match. */ 654418334Speter 6545132730Skan else 6546132730Skan { 6547132730Skan tree actual = 0, last = 0, type; 6548132730Skan 6549132730Skan for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm)) 655018334Speter { 6551132730Skan type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE); 655218334Speter if (last) 655318334Speter TREE_CHAIN (last) = type; 655418334Speter else 655518334Speter actual = type; 6556132730Skan last = type; 6557132730Skan } 6558132730Skan type = tree_cons (NULL_TREE, void_type_node, NULL_TREE); 6559132730Skan if (last) 6560132730Skan TREE_CHAIN (last) = type; 6561132730Skan else 6562132730Skan actual = type; 656318334Speter 6564132730Skan /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES 6565132730Skan of the type of this function, but we need to avoid having this 6566132730Skan affect the types of other similarly-typed functions, so we must 6567132730Skan first force the generation of an identical (but separate) type 6568132730Skan node for the relevant function type. The new node we create 6569132730Skan will be a variant of the main variant of the original function 6570132730Skan type. */ 657118334Speter 6572169699Skan TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl)); 657318334Speter 6574132730Skan TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual; 6575132730Skan } 6576132730Skan} 657718334Speter 6578169699Skan/* Store parameter declarations passed in ARG_INFO into the current 6579169699Skan function declaration. */ 6580169699Skan 6581169699Skanvoid 6582169699Skanstore_parm_decls_from (struct c_arg_info *arg_info) 6583169699Skan{ 6584169699Skan current_function_arg_info = arg_info; 6585169699Skan store_parm_decls (); 6586169699Skan} 6587169699Skan 6588132730Skan/* Store the parameter declarations into the current function declaration. 6589132730Skan This is called after parsing the parameter declarations, before 6590132730Skan digesting the body of the function. 659118334Speter 6592132730Skan For an old-style definition, construct a prototype out of the old-style 6593132730Skan parameter declarations and inject it into the function's type. */ 659418334Speter 6595132730Skanvoid 6596132730Skanstore_parm_decls (void) 6597132730Skan{ 6598132730Skan tree fndecl = current_function_decl; 6599169699Skan bool proto; 660018334Speter 6601169699Skan /* The argument information block for FNDECL. */ 6602169699Skan struct c_arg_info *arg_info = current_function_arg_info; 6603169699Skan current_function_arg_info = 0; 660418334Speter 6605169699Skan /* True if this definition is written with a prototype. Note: 6606169699Skan despite C99 6.7.5.3p14, we can *not* treat an empty argument 6607169699Skan list in a function definition as equivalent to (void) -- an 6608169699Skan empty argument list specifies the function has no parameters, 6609169699Skan but only (void) sets up a prototype for future calls. */ 6610169699Skan proto = arg_info->types != 0; 6611169699Skan 6612169699Skan if (proto) 6613169699Skan store_parm_decls_newstyle (fndecl, arg_info); 6614132730Skan else 6615169699Skan store_parm_decls_oldstyle (fndecl, arg_info); 6616132730Skan 6617169699Skan /* The next call to push_scope will be a function body. */ 6618132730Skan 6619132730Skan next_is_function_body = true; 6620132730Skan 662118334Speter /* Write a record describing this function definition to the prototypes 662218334Speter file (if requested). */ 662318334Speter 6624169699Skan gen_aux_info_record (fndecl, 1, 0, proto); 662518334Speter 662618334Speter /* Initialize the RTL code for the function. */ 6627132730Skan allocate_struct_function (fndecl); 662818334Speter 662990075Sobrien /* Begin the statement tree for this function. */ 6630169699Skan DECL_SAVED_TREE (fndecl) = push_stmt_list (); 663118334Speter 6632169699Skan /* ??? Insert the contents of the pending sizes list into the function 6633169699Skan to be evaluated. The only reason left to have this is 6634169699Skan void foo(int n, int array[n++]) 6635169699Skan because we throw away the array type in favor of a pointer type, and 6636169699Skan thus won't naturally see the SAVE_EXPR containing the increment. All 6637169699Skan other pending sizes would be handled by gimplify_parameters. */ 6638169699Skan { 6639169699Skan tree t; 6640169699Skan for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t)) 6641169699Skan add_stmt (TREE_VALUE (t)); 6642169699Skan } 664318334Speter 664490075Sobrien /* Even though we're inside a function body, we still don't want to 664590075Sobrien call expand_expr to calculate the size of a variable-sized array. 664690075Sobrien We haven't necessarily assigned RTL to all variables yet, so it's 664790075Sobrien not safe to try to expand expressions involving them. */ 664890075Sobrien cfun->x_dont_save_pending_sizes_p = 1; 664918334Speter} 665018334Speter 6651169699Skan/* Emit diagnostics that require gimple input for detection. Operate on 6652169699Skan FNDECL and all its nested functions. */ 6653169699Skan 6654169699Skanstatic void 6655169699Skanc_gimple_diagnostics_recursively (tree fndecl) 6656169699Skan{ 6657169699Skan struct cgraph_node *cgn; 6658169699Skan 6659169699Skan /* Handle attribute((warn_unused_result)). Relies on gimple input. */ 6660169699Skan c_warn_unused_result (&DECL_SAVED_TREE (fndecl)); 6661169699Skan 6662169699Skan /* Notice when OpenMP structured block constraints are violated. */ 6663169699Skan if (flag_openmp) 6664169699Skan diagnose_omp_structured_block_errors (fndecl); 6665169699Skan 6666169699Skan /* Finalize all nested functions now. */ 6667169699Skan cgn = cgraph_node (fndecl); 6668169699Skan for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested) 6669169699Skan c_gimple_diagnostics_recursively (cgn->decl); 6670169699Skan} 6671169699Skan 667218334Speter/* Finish up a function declaration and compile that function 667318334Speter all the way to assembler language output. The free the storage 667418334Speter for the function definition. 667518334Speter 6676132730Skan This is called after parsing the body of the function definition. */ 667718334Speter 667818334Spetervoid 6679132730Skanfinish_function (void) 668018334Speter{ 668190075Sobrien tree fndecl = current_function_decl; 668218334Speter 6683169699Skan label_context_stack_se = label_context_stack_se->next; 6684169699Skan label_context_stack_vm = label_context_stack_vm->next; 668518334Speter 6686132730Skan if (TREE_CODE (fndecl) == FUNCTION_DECL 6687132730Skan && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))) 6688132730Skan { 6689132730Skan tree args = DECL_ARGUMENTS (fndecl); 6690132730Skan for (; args; args = TREE_CHAIN (args)) 6691169699Skan { 6692169699Skan tree type = TREE_TYPE (args); 6693169699Skan if (INTEGRAL_TYPE_P (type) 6694169699Skan && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 6695169699Skan DECL_ARG_TYPE (args) = integer_type_node; 6696169699Skan } 6697132730Skan } 6698132730Skan 6699132730Skan if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) 6700132730Skan BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; 6701132730Skan 670218334Speter /* Must mark the RESULT_DECL as being in this function. */ 670318334Speter 6704132730Skan if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node) 6705132730Skan DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; 670618334Speter 670790075Sobrien if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted) 670818334Speter { 670918334Speter if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))) 671018334Speter != integer_type_node) 671150397Sobrien { 671252284Sobrien /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned. 671390075Sobrien If warn_main is -1 (-Wno-main) we don't want to be warned. */ 6714132730Skan if (!warn_main) 6715169699Skan pedwarn ("return type of %q+D is not %<int%>", fndecl); 671650397Sobrien } 671718334Speter else 671818334Speter { 6719169699Skan if (flag_isoc99) 6720169699Skan { 6721169699Skan tree stmt = c_finish_return (integer_zero_node); 6722169699Skan#ifdef USE_MAPPED_LOCATION 6723169699Skan /* Hack. We don't want the middle-end to warn that this return 6724169699Skan is unreachable, so we mark its location as special. Using 6725169699Skan UNKNOWN_LOCATION has the problem that it gets clobbered in 6726169699Skan annotate_one_with_locus. A cleaner solution might be to 6727169699Skan ensure ! should_carry_locus_p (stmt), but that needs a flag. 6728169699Skan */ 6729169699Skan SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION); 673090075Sobrien#else 6731169699Skan /* Hack. We don't want the middle-end to warn that this 6732169699Skan return is unreachable, so put the statement on the 6733169699Skan special line 0. */ 6734169699Skan annotate_with_file_line (stmt, input_filename, 0); 673550397Sobrien#endif 6736169699Skan } 673718334Speter } 673818334Speter } 6739132730Skan 6740169699Skan /* Tie off the statement tree for this function. */ 6741169699Skan DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl)); 6742169699Skan 674390075Sobrien finish_fname_decls (); 674418334Speter 674596263Sobrien /* Complain if there's just no return statement. */ 674696263Sobrien if (warn_return_type 674796263Sobrien && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE 674896263Sobrien && !current_function_returns_value && !current_function_returns_null 6749169699Skan /* Don't complain if we are no-return. */ 675096263Sobrien && !current_function_returns_abnormally 675196263Sobrien /* Don't warn for main(). */ 675296263Sobrien && !MAIN_NAME_P (DECL_NAME (fndecl)) 675396263Sobrien /* Or if they didn't actually specify a return type. */ 675496263Sobrien && !C_FUNCTION_IMPLICIT_INT (fndecl) 675596263Sobrien /* Normally, with -Wreturn-type, flow will complain. Unless we're an 675696263Sobrien inline function, as we might never be compiled separately. */ 675796263Sobrien && DECL_INLINE (fndecl)) 6758169699Skan { 6759169699Skan warning (OPT_Wreturn_type, 6760169699Skan "no return statement in function returning non-void"); 6761169699Skan TREE_NO_WARNING (fndecl) = 1; 6762169699Skan } 676396263Sobrien 6764132730Skan /* With just -Wextra, complain only if function returns both with 6765122196Skan and without a value. */ 6766122196Skan if (extra_warnings 6767122196Skan && current_function_returns_value 6768122196Skan && current_function_returns_null) 6769169699Skan warning (OPT_Wextra, "this function may return with or without a value"); 6770122196Skan 6771169699Skan /* Store the end of the function, so that we get good line number 6772169699Skan info for the epilogue. */ 6773169699Skan cfun->function_end_locus = input_location; 677490075Sobrien 6775169699Skan /* If we don't have ctors/dtors sections, and this is a static 6776169699Skan constructor or destructor, it must be recorded now. */ 6777169699Skan if (DECL_STATIC_CONSTRUCTOR (fndecl) 6778169699Skan && !targetm.have_ctors_dtors) 6779169699Skan static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors); 6780169699Skan if (DECL_STATIC_DESTRUCTOR (fndecl) 6781169699Skan && !targetm.have_ctors_dtors) 6782169699Skan static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors); 678390075Sobrien 6784169699Skan /* Finalize the ELF visibility for the function. */ 6785169699Skan c_determine_visibility (fndecl); 678690075Sobrien 6787169699Skan /* Genericize before inlining. Delay genericizing nested functions 6788169699Skan until their parent function is genericized. Since finalizing 6789169699Skan requires GENERIC, delay that as well. */ 6790169699Skan 6791169699Skan if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node 6792169699Skan && !undef_nested_function) 679390075Sobrien { 6794169699Skan if (!decl_function_context (fndecl)) 6795132730Skan { 6796169699Skan c_genericize (fndecl); 6797169699Skan c_gimple_diagnostics_recursively (fndecl); 679890075Sobrien 6799169699Skan /* ??? Objc emits functions after finalizing the compilation unit. 6800169699Skan This should be cleaned up later and this conditional removed. */ 6801169699Skan if (cgraph_global_info_ready) 6802169699Skan { 6803169699Skan c_expand_body (fndecl); 6804169699Skan return; 6805169699Skan } 680690075Sobrien 6807169699Skan cgraph_finalize_function (fndecl, false); 6808169699Skan } 680918334Speter else 6810169699Skan { 6811169699Skan /* Register this function with cgraph just far enough to get it 6812169699Skan added to our parent's nested function list. Handy, since the 6813169699Skan C front end doesn't have such a list. */ 6814169699Skan (void) cgraph_node (fndecl); 6815169699Skan } 681618334Speter } 681790075Sobrien 6818169699Skan if (!decl_function_context (fndecl)) 6819169699Skan undef_nested_function = false; 6820169699Skan 6821169699Skan /* We're leaving the context of this function, so zap cfun. 6822169699Skan It's still in DECL_STRUCT_FUNCTION, and we'll restore it in 6823169699Skan tree_rest_of_compilation. */ 6824169699Skan cfun = NULL; 6825169699Skan current_function_decl = NULL; 6826132730Skan} 682718334Speter 6828169699Skan/* Generate the RTL for the body of FNDECL. */ 6829117421Skan 6830132730Skanvoid 6831132730Skanc_expand_body (tree fndecl) 6832132730Skan{ 6833132730Skan 6834169699Skan if (!DECL_INITIAL (fndecl) 6835169699Skan || DECL_INITIAL (fndecl) == error_mark_node) 6836169699Skan return; 6837169699Skan 6838169699Skan tree_rest_of_compilation (fndecl); 6839169699Skan 6840169699Skan if (DECL_STATIC_CONSTRUCTOR (fndecl) 6841169699Skan && targetm.have_ctors_dtors) 6842169699Skan targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0), 6843169699Skan DEFAULT_INIT_PRIORITY); 6844169699Skan if (DECL_STATIC_DESTRUCTOR (fndecl) 6845169699Skan && targetm.have_ctors_dtors) 6846169699Skan targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0), 6847169699Skan DEFAULT_INIT_PRIORITY); 684890075Sobrien} 684990075Sobrien 685090075Sobrien/* Check the declarations given in a for-loop for satisfying the C99 6851169699Skan constraints. If exactly one such decl is found, return it. */ 6852169699Skan 6853169699Skantree 6854132730Skancheck_for_loop_decls (void) 685590075Sobrien{ 6856169699Skan struct c_binding *b; 6857169699Skan tree one_decl = NULL_TREE; 6858169699Skan int n_decls = 0; 685990075Sobrien 6860169699Skan 686190075Sobrien if (!flag_isoc99) 686218334Speter { 686390075Sobrien /* If we get here, declarations have been used in a for loop without 686490075Sobrien the C99 for loop scope. This doesn't make much sense, so don't 686590075Sobrien allow it. */ 6866169699Skan error ("%<for%> loop initial declaration used outside C99 mode"); 6867169699Skan return NULL_TREE; 686818334Speter } 686990075Sobrien /* C99 subclause 6.8.5 paragraph 3: 687090075Sobrien 687190075Sobrien [#3] The declaration part of a for statement shall only 687290075Sobrien declare identifiers for objects having storage class auto or 687390075Sobrien register. 687490075Sobrien 687590075Sobrien It isn't clear whether, in this sentence, "identifiers" binds to 687690075Sobrien "shall only declare" or to "objects" - that is, whether all identifiers 687790075Sobrien declared must be identifiers for objects, or whether the restriction 687890075Sobrien only applies to those that are. (A question on this in comp.std.c 687990075Sobrien in November 2000 received no answer.) We implement the strictest 688090075Sobrien interpretation, to avoid creating an extension which later causes 688190075Sobrien problems. */ 688290075Sobrien 6883169699Skan for (b = current_scope->bindings; b; b = b->prev) 688490075Sobrien { 6885169699Skan tree id = b->id; 6886169699Skan tree decl = b->decl; 6887132730Skan 6888169699Skan if (!id) 6889169699Skan continue; 6890169699Skan 6891169699Skan switch (TREE_CODE (decl)) 6892169699Skan { 6893169699Skan case VAR_DECL: 6894169699Skan if (TREE_STATIC (decl)) 6895169699Skan error ("declaration of static variable %q+D in %<for%> loop " 6896169699Skan "initial declaration", decl); 6897169699Skan else if (DECL_EXTERNAL (decl)) 6898169699Skan error ("declaration of %<extern%> variable %q+D in %<for%> loop " 6899169699Skan "initial declaration", decl); 6900169699Skan break; 6901169699Skan 6902169699Skan case RECORD_TYPE: 6903169699Skan error ("%<struct %E%> declared in %<for%> loop initial declaration", 6904169699Skan id); 6905169699Skan break; 6906169699Skan case UNION_TYPE: 6907169699Skan error ("%<union %E%> declared in %<for%> loop initial declaration", 6908169699Skan id); 6909169699Skan break; 6910169699Skan case ENUMERAL_TYPE: 6911169699Skan error ("%<enum %E%> declared in %<for%> loop initial declaration", 6912169699Skan id); 6913169699Skan break; 6914169699Skan default: 6915169699Skan error ("declaration of non-variable %q+D in %<for%> loop " 6916169699Skan "initial declaration", decl); 6917169699Skan } 6918169699Skan 6919169699Skan n_decls++; 6920169699Skan one_decl = decl; 692190075Sobrien } 692290075Sobrien 6923169699Skan return n_decls == 1 ? one_decl : NULL_TREE; 692418334Speter} 692518334Speter 692618334Speter/* Save and reinitialize the variables 692718334Speter used during compilation of a C function. */ 692818334Speter 692918334Spetervoid 6930132730Skanc_push_function_context (struct function *f) 693118334Speter{ 6932117421Skan struct language_function *p; 6933169699Skan p = GGC_NEW (struct language_function); 6934117421Skan f->language = p; 693518334Speter 693690075Sobrien p->base.x_stmt_tree = c_stmt_tree; 6937169699Skan p->x_break_label = c_break_label; 6938169699Skan p->x_cont_label = c_cont_label; 6939169699Skan p->x_switch_stack = c_switch_stack; 6940169699Skan p->arg_info = current_function_arg_info; 694118334Speter p->returns_value = current_function_returns_value; 694218334Speter p->returns_null = current_function_returns_null; 694396263Sobrien p->returns_abnormally = current_function_returns_abnormally; 694418334Speter p->warn_about_return_type = warn_about_return_type; 694518334Speter p->extern_inline = current_extern_inline; 694618334Speter} 694718334Speter 694818334Speter/* Restore the variables used during compilation of a C function. */ 694918334Speter 695018334Spetervoid 6951132730Skanc_pop_function_context (struct function *f) 695218334Speter{ 6953117421Skan struct language_function *p = f->language; 695418334Speter 6955169699Skan if (DECL_STRUCT_FUNCTION (current_function_decl) == 0 695690075Sobrien && DECL_SAVED_TREE (current_function_decl) == NULL_TREE) 695718334Speter { 695818334Speter /* Stop pointing to the local nodes about to be freed. */ 695918334Speter /* But DECL_INITIAL must remain nonzero so we know this 696018334Speter was an actual function definition. */ 696118334Speter DECL_INITIAL (current_function_decl) = error_mark_node; 696218334Speter DECL_ARGUMENTS (current_function_decl) = 0; 696318334Speter } 696418334Speter 696590075Sobrien c_stmt_tree = p->base.x_stmt_tree; 6966169699Skan c_break_label = p->x_break_label; 6967169699Skan c_cont_label = p->x_cont_label; 6968169699Skan c_switch_stack = p->x_switch_stack; 6969169699Skan current_function_arg_info = p->arg_info; 697018334Speter current_function_returns_value = p->returns_value; 697118334Speter current_function_returns_null = p->returns_null; 697296263Sobrien current_function_returns_abnormally = p->returns_abnormally; 697318334Speter warn_about_return_type = p->warn_about_return_type; 697418334Speter current_extern_inline = p->extern_inline; 697518334Speter 6976117421Skan f->language = NULL; 697718334Speter} 697818334Speter 6979117421Skan/* Copy the DECL_LANG_SPECIFIC data associated with DECL. */ 698018334Speter 698118334Spetervoid 6982132730Skanc_dup_lang_specific_decl (tree decl) 698390075Sobrien{ 698490075Sobrien struct lang_decl *ld; 698590075Sobrien 698690075Sobrien if (!DECL_LANG_SPECIFIC (decl)) 698790075Sobrien return; 698890075Sobrien 6989169699Skan ld = GGC_NEW (struct lang_decl); 6990132730Skan memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl)); 699190075Sobrien DECL_LANG_SPECIFIC (decl) = ld; 699290075Sobrien} 699390075Sobrien 699490075Sobrien/* The functions below are required for functionality of doing 699590075Sobrien function at once processing in the C front end. Currently these 699690075Sobrien functions are not called from anywhere in the C front end, but as 699790075Sobrien these changes continue, that will change. */ 699890075Sobrien 699990075Sobrien/* Returns the stmt_tree (if any) to which statements are currently 700090075Sobrien being added. If there is no active statement-tree, NULL is 700190075Sobrien returned. */ 700290075Sobrien 700390075Sobrienstmt_tree 7004132730Skancurrent_stmt_tree (void) 700590075Sobrien{ 700690075Sobrien return &c_stmt_tree; 700790075Sobrien} 700890075Sobrien 700990075Sobrien/* Nonzero if TYPE is an anonymous union or struct type. Always 0 in 701090075Sobrien C. */ 701190075Sobrien 701290075Sobrienint 7013169699Skananon_aggr_type_p (tree ARG_UNUSED (node)) 701418334Speter{ 701590075Sobrien return 0; 701618334Speter} 701790075Sobrien 7018132730Skan/* Return the global value of T as a symbol. */ 701990075Sobrien 702090075Sobrientree 7021132730Skanidentifier_global_value (tree t) 702290075Sobrien{ 7023169699Skan struct c_binding *b; 7024132730Skan 7025169699Skan for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed) 7026169699Skan if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b)) 7027169699Skan return b->decl; 7028132730Skan 7029132730Skan return 0; 703090075Sobrien} 703190075Sobrien 703290075Sobrien/* Record a builtin type for C. If NAME is non-NULL, it is the name used; 703390075Sobrien otherwise the name is found in ridpointers from RID_INDEX. */ 703490075Sobrien 703590075Sobrienvoid 7036132730Skanrecord_builtin_type (enum rid rid_index, const char *name, tree type) 703790075Sobrien{ 7038169699Skan tree id, decl; 703990075Sobrien if (name == 0) 704090075Sobrien id = ridpointers[(int) rid_index]; 704190075Sobrien else 704290075Sobrien id = get_identifier (name); 7043169699Skan decl = build_decl (TYPE_DECL, id, type); 7044169699Skan pushdecl (decl); 7045169699Skan if (debug_hooks->type_decl) 7046169699Skan debug_hooks->type_decl (decl, false); 704790075Sobrien} 704890075Sobrien 704990075Sobrien/* Build the void_list_node (void_type_node having been created). */ 705090075Sobrientree 7051132730Skanbuild_void_list_node (void) 705290075Sobrien{ 705390075Sobrien tree t = build_tree_list (NULL_TREE, void_type_node); 705490075Sobrien return t; 705590075Sobrien} 7056117421Skan 7057169699Skan/* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */ 7058117421Skan 7059169699Skanstruct c_parm * 7060169699Skanbuild_c_parm (struct c_declspecs *specs, tree attrs, 7061169699Skan struct c_declarator *declarator) 7062169699Skan{ 7063169699Skan struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm); 7064169699Skan ret->specs = specs; 7065169699Skan ret->attrs = attrs; 7066169699Skan ret->declarator = declarator; 7067169699Skan return ret; 7068169699Skan} 7069117421Skan 7070169699Skan/* Return a declarator with nested attributes. TARGET is the inner 7071169699Skan declarator to which these attributes apply. ATTRS are the 7072169699Skan attributes. */ 7073169699Skan 7074169699Skanstruct c_declarator * 7075169699Skanbuild_attrs_declarator (tree attrs, struct c_declarator *target) 7076117421Skan{ 7077169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7078169699Skan ret->kind = cdk_attrs; 7079169699Skan ret->declarator = target; 7080169699Skan ret->u.attrs = attrs; 7081169699Skan return ret; 7082117421Skan} 7083117421Skan 7084169699Skan/* Return a declarator for a function with arguments specified by ARGS 7085169699Skan and return type specified by TARGET. */ 7086132730Skan 7087169699Skanstruct c_declarator * 7088169699Skanbuild_function_declarator (struct c_arg_info *args, 7089169699Skan struct c_declarator *target) 7090132730Skan{ 7091169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7092169699Skan ret->kind = cdk_function; 7093169699Skan ret->declarator = target; 7094169699Skan ret->u.arg_info = args; 7095169699Skan return ret; 7096132730Skan} 7097132730Skan 7098169699Skan/* Return a declarator for the identifier IDENT (which may be 7099169699Skan NULL_TREE for an abstract declarator). */ 7100132730Skan 7101169699Skanstruct c_declarator * 7102169699Skanbuild_id_declarator (tree ident) 7103132730Skan{ 7104169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7105169699Skan ret->kind = cdk_id; 7106169699Skan ret->declarator = 0; 7107169699Skan ret->u.id = ident; 7108169699Skan /* Default value - may get reset to a more precise location. */ 7109169699Skan ret->id_loc = input_location; 7110169699Skan return ret; 7111132730Skan} 7112132730Skan 7113169699Skan/* Return something to represent absolute declarators containing a *. 7114169699Skan TARGET is the absolute declarator that the * contains. 7115169699Skan TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes 7116169699Skan to apply to the pointer type. */ 7117169699Skan 7118169699Skanstruct c_declarator * 7119169699Skanmake_pointer_declarator (struct c_declspecs *type_quals_attrs, 7120169699Skan struct c_declarator *target) 7121132730Skan{ 7122169699Skan tree attrs; 7123169699Skan int quals = 0; 7124169699Skan struct c_declarator *itarget = target; 7125169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7126169699Skan if (type_quals_attrs) 7127169699Skan { 7128169699Skan attrs = type_quals_attrs->attrs; 7129169699Skan quals = quals_from_declspecs (type_quals_attrs); 7130169699Skan if (attrs != NULL_TREE) 7131169699Skan itarget = build_attrs_declarator (attrs, target); 7132169699Skan } 7133169699Skan ret->kind = cdk_pointer; 7134169699Skan ret->declarator = itarget; 7135169699Skan ret->u.pointer_quals = quals; 7136169699Skan return ret; 7137132730Skan} 7138132730Skan 7139169699Skan/* Return a pointer to a structure for an empty list of declaration 7140169699Skan specifiers. */ 7141132730Skan 7142169699Skanstruct c_declspecs * 7143169699Skanbuild_null_declspecs (void) 7144132730Skan{ 7145169699Skan struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs); 7146169699Skan ret->type = 0; 7147169699Skan ret->decl_attr = 0; 7148169699Skan ret->attrs = 0; 7149169699Skan ret->typespec_word = cts_none; 7150169699Skan ret->storage_class = csc_none; 7151169699Skan ret->declspecs_seen_p = false; 7152169699Skan ret->type_seen_p = false; 7153169699Skan ret->non_sc_seen_p = false; 7154169699Skan ret->typedef_p = false; 7155169699Skan ret->tag_defined_p = false; 7156169699Skan ret->explicit_signed_p = false; 7157169699Skan ret->deprecated_p = false; 7158169699Skan ret->default_int_p = false; 7159169699Skan ret->long_p = false; 7160169699Skan ret->long_long_p = false; 7161169699Skan ret->short_p = false; 7162169699Skan ret->signed_p = false; 7163169699Skan ret->unsigned_p = false; 7164169699Skan ret->complex_p = false; 7165169699Skan ret->inline_p = false; 7166169699Skan ret->thread_p = false; 7167169699Skan ret->const_p = false; 7168169699Skan ret->volatile_p = false; 7169169699Skan ret->restrict_p = false; 7170169699Skan return ret; 7171169699Skan} 7172132730Skan 7173169699Skan/* Add the type qualifier QUAL to the declaration specifiers SPECS, 7174169699Skan returning SPECS. */ 7175132730Skan 7176169699Skanstruct c_declspecs * 7177169699Skandeclspecs_add_qual (struct c_declspecs *specs, tree qual) 7178169699Skan{ 7179169699Skan enum rid i; 7180169699Skan bool dupe = false; 7181169699Skan specs->non_sc_seen_p = true; 7182169699Skan specs->declspecs_seen_p = true; 7183169699Skan gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE 7184169699Skan && C_IS_RESERVED_WORD (qual)); 7185169699Skan i = C_RID_CODE (qual); 7186169699Skan switch (i) 7187169699Skan { 7188169699Skan case RID_CONST: 7189169699Skan dupe = specs->const_p; 7190169699Skan specs->const_p = true; 7191169699Skan break; 7192169699Skan case RID_VOLATILE: 7193169699Skan dupe = specs->volatile_p; 7194169699Skan specs->volatile_p = true; 7195169699Skan break; 7196169699Skan case RID_RESTRICT: 7197169699Skan dupe = specs->restrict_p; 7198169699Skan specs->restrict_p = true; 7199169699Skan break; 7200169699Skan default: 7201169699Skan gcc_unreachable (); 7202169699Skan } 7203169699Skan if (dupe && pedantic && !flag_isoc99) 7204169699Skan pedwarn ("duplicate %qE", qual); 7205169699Skan return specs; 7206169699Skan} 7207132730Skan 7208169699Skan/* Add the type specifier TYPE to the declaration specifiers SPECS, 7209169699Skan returning SPECS. */ 7210132730Skan 7211169699Skanstruct c_declspecs * 7212169699Skandeclspecs_add_type (struct c_declspecs *specs, struct c_typespec spec) 7213169699Skan{ 7214169699Skan tree type = spec.spec; 7215169699Skan specs->non_sc_seen_p = true; 7216169699Skan specs->declspecs_seen_p = true; 7217169699Skan specs->type_seen_p = true; 7218169699Skan if (TREE_DEPRECATED (type)) 7219169699Skan specs->deprecated_p = true; 7220169699Skan 7221169699Skan /* Handle type specifier keywords. */ 7222169699Skan if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type)) 7223169699Skan { 7224169699Skan enum rid i = C_RID_CODE (type); 7225169699Skan if (specs->type) 7226132730Skan { 7227169699Skan error ("two or more data types in declaration specifiers"); 7228169699Skan return specs; 7229169699Skan } 7230169699Skan if ((int) i <= (int) RID_LAST_MODIFIER) 7231169699Skan { 7232169699Skan /* "long", "short", "signed", "unsigned" or "_Complex". */ 7233169699Skan bool dupe = false; 7234169699Skan switch (i) 7235132730Skan { 7236169699Skan case RID_LONG: 7237169699Skan if (specs->long_long_p) 7238132730Skan { 7239169699Skan error ("%<long long long%> is too long for GCC"); 7240169699Skan break; 7241132730Skan } 7242169699Skan if (specs->long_p) 7243132730Skan { 7244169699Skan if (specs->typespec_word == cts_double) 7245169699Skan { 7246169699Skan error ("both %<long long%> and %<double%> in " 7247169699Skan "declaration specifiers"); 7248169699Skan break; 7249169699Skan } 7250169699Skan if (pedantic && !flag_isoc99 && !in_system_header 7251169699Skan && warn_long_long) 7252169699Skan pedwarn ("ISO C90 does not support %<long long%>"); 7253169699Skan specs->long_long_p = 1; 7254169699Skan break; 7255132730Skan } 7256169699Skan if (specs->short_p) 7257169699Skan error ("both %<long%> and %<short%> in " 7258169699Skan "declaration specifiers"); 7259169699Skan else if (specs->typespec_word == cts_void) 7260169699Skan error ("both %<long%> and %<void%> in " 7261169699Skan "declaration specifiers"); 7262169699Skan else if (specs->typespec_word == cts_bool) 7263169699Skan error ("both %<long%> and %<_Bool%> in " 7264169699Skan "declaration specifiers"); 7265169699Skan else if (specs->typespec_word == cts_char) 7266169699Skan error ("both %<long%> and %<char%> in " 7267169699Skan "declaration specifiers"); 7268169699Skan else if (specs->typespec_word == cts_float) 7269169699Skan error ("both %<long%> and %<float%> in " 7270169699Skan "declaration specifiers"); 7271169699Skan else if (specs->typespec_word == cts_dfloat32) 7272169699Skan error ("both %<long%> and %<_Decimal32%> in " 7273169699Skan "declaration specifiers"); 7274169699Skan else if (specs->typespec_word == cts_dfloat64) 7275169699Skan error ("both %<long%> and %<_Decimal64%> in " 7276169699Skan "declaration specifiers"); 7277169699Skan else if (specs->typespec_word == cts_dfloat128) 7278169699Skan error ("both %<long%> and %<_Decimal128%> in " 7279169699Skan "declaration specifiers"); 7280132730Skan else 7281169699Skan specs->long_p = true; 7282169699Skan break; 7283169699Skan case RID_SHORT: 7284169699Skan dupe = specs->short_p; 7285169699Skan if (specs->long_p) 7286169699Skan error ("both %<long%> and %<short%> in " 7287169699Skan "declaration specifiers"); 7288169699Skan else if (specs->typespec_word == cts_void) 7289169699Skan error ("both %<short%> and %<void%> in " 7290169699Skan "declaration specifiers"); 7291169699Skan else if (specs->typespec_word == cts_bool) 7292169699Skan error ("both %<short%> and %<_Bool%> in " 7293169699Skan "declaration specifiers"); 7294169699Skan else if (specs->typespec_word == cts_char) 7295169699Skan error ("both %<short%> and %<char%> in " 7296169699Skan "declaration specifiers"); 7297169699Skan else if (specs->typespec_word == cts_float) 7298169699Skan error ("both %<short%> and %<float%> in " 7299169699Skan "declaration specifiers"); 7300169699Skan else if (specs->typespec_word == cts_double) 7301169699Skan error ("both %<short%> and %<double%> in " 7302169699Skan "declaration specifiers"); 7303169699Skan else if (specs->typespec_word == cts_dfloat32) 7304169699Skan error ("both %<short%> and %<_Decimal32%> in " 7305169699Skan "declaration specifiers"); 7306169699Skan else if (specs->typespec_word == cts_dfloat64) 7307169699Skan error ("both %<short%> and %<_Decimal64%> in " 7308169699Skan "declaration specifiers"); 7309169699Skan else if (specs->typespec_word == cts_dfloat128) 7310169699Skan error ("both %<short%> and %<_Decimal128%> in " 7311169699Skan "declaration specifiers"); 7312169699Skan else 7313169699Skan specs->short_p = true; 7314169699Skan break; 7315169699Skan case RID_SIGNED: 7316169699Skan dupe = specs->signed_p; 7317169699Skan if (specs->unsigned_p) 7318169699Skan error ("both %<signed%> and %<unsigned%> in " 7319169699Skan "declaration specifiers"); 7320169699Skan else if (specs->typespec_word == cts_void) 7321169699Skan error ("both %<signed%> and %<void%> in " 7322169699Skan "declaration specifiers"); 7323169699Skan else if (specs->typespec_word == cts_bool) 7324169699Skan error ("both %<signed%> and %<_Bool%> in " 7325169699Skan "declaration specifiers"); 7326169699Skan else if (specs->typespec_word == cts_float) 7327169699Skan error ("both %<signed%> and %<float%> in " 7328169699Skan "declaration specifiers"); 7329169699Skan else if (specs->typespec_word == cts_double) 7330169699Skan error ("both %<signed%> and %<double%> in " 7331169699Skan "declaration specifiers"); 7332169699Skan else if (specs->typespec_word == cts_dfloat32) 7333169699Skan error ("both %<signed%> and %<_Decimal32%> in " 7334169699Skan "declaration specifiers"); 7335169699Skan else if (specs->typespec_word == cts_dfloat64) 7336169699Skan error ("both %<signed%> and %<_Decimal64%> in " 7337169699Skan "declaration specifiers"); 7338169699Skan else if (specs->typespec_word == cts_dfloat128) 7339169699Skan error ("both %<signed%> and %<_Decimal128%> in " 7340169699Skan "declaration specifiers"); 7341169699Skan else 7342169699Skan specs->signed_p = true; 7343169699Skan break; 7344169699Skan case RID_UNSIGNED: 7345169699Skan dupe = specs->unsigned_p; 7346169699Skan if (specs->signed_p) 7347169699Skan error ("both %<signed%> and %<unsigned%> in " 7348169699Skan "declaration specifiers"); 7349169699Skan else if (specs->typespec_word == cts_void) 7350169699Skan error ("both %<unsigned%> and %<void%> in " 7351169699Skan "declaration specifiers"); 7352169699Skan else if (specs->typespec_word == cts_bool) 7353169699Skan error ("both %<unsigned%> and %<_Bool%> in " 7354169699Skan "declaration specifiers"); 7355169699Skan else if (specs->typespec_word == cts_float) 7356169699Skan error ("both %<unsigned%> and %<float%> in " 7357169699Skan "declaration specifiers"); 7358169699Skan else if (specs->typespec_word == cts_double) 7359169699Skan error ("both %<unsigned%> and %<double%> in " 7360169699Skan "declaration specifiers"); 7361169699Skan else if (specs->typespec_word == cts_dfloat32) 7362169699Skan error ("both %<unsigned%> and %<_Decimal32%> in " 7363169699Skan "declaration specifiers"); 7364169699Skan else if (specs->typespec_word == cts_dfloat64) 7365169699Skan error ("both %<unsigned%> and %<_Decimal64%> in " 7366169699Skan "declaration specifiers"); 7367169699Skan else if (specs->typespec_word == cts_dfloat128) 7368169699Skan error ("both %<unsigned%> and %<_Decimal128%> in " 7369169699Skan "declaration specifiers"); 7370169699Skan else 7371169699Skan specs->unsigned_p = true; 7372169699Skan break; 7373169699Skan case RID_COMPLEX: 7374169699Skan dupe = specs->complex_p; 7375169699Skan if (pedantic && !flag_isoc99 && !in_system_header) 7376169699Skan pedwarn ("ISO C90 does not support complex types"); 7377169699Skan if (specs->typespec_word == cts_void) 7378169699Skan error ("both %<complex%> and %<void%> in " 7379169699Skan "declaration specifiers"); 7380169699Skan else if (specs->typespec_word == cts_bool) 7381169699Skan error ("both %<complex%> and %<_Bool%> in " 7382169699Skan "declaration specifiers"); 7383169699Skan else if (specs->typespec_word == cts_dfloat32) 7384169699Skan error ("both %<complex%> and %<_Decimal32%> in " 7385169699Skan "declaration specifiers"); 7386169699Skan else if (specs->typespec_word == cts_dfloat64) 7387169699Skan error ("both %<complex%> and %<_Decimal64%> in " 7388169699Skan "declaration specifiers"); 7389169699Skan else if (specs->typespec_word == cts_dfloat128) 7390169699Skan error ("both %<complex%> and %<_Decimal128%> in " 7391169699Skan "declaration specifiers"); 7392169699Skan else 7393169699Skan specs->complex_p = true; 7394169699Skan break; 7395169699Skan default: 7396169699Skan gcc_unreachable (); 7397132730Skan } 7398169699Skan 7399169699Skan if (dupe) 7400169699Skan error ("duplicate %qE", type); 7401169699Skan 7402169699Skan return specs; 7403132730Skan } 7404169699Skan else 7405169699Skan { 7406169699Skan /* "void", "_Bool", "char", "int", "float" or "double". */ 7407169699Skan if (specs->typespec_word != cts_none) 7408169699Skan { 7409169699Skan error ("two or more data types in declaration specifiers"); 7410169699Skan return specs; 7411169699Skan } 7412169699Skan switch (i) 7413169699Skan { 7414169699Skan case RID_VOID: 7415169699Skan if (specs->long_p) 7416169699Skan error ("both %<long%> and %<void%> in " 7417169699Skan "declaration specifiers"); 7418169699Skan else if (specs->short_p) 7419169699Skan error ("both %<short%> and %<void%> in " 7420169699Skan "declaration specifiers"); 7421169699Skan else if (specs->signed_p) 7422169699Skan error ("both %<signed%> and %<void%> in " 7423169699Skan "declaration specifiers"); 7424169699Skan else if (specs->unsigned_p) 7425169699Skan error ("both %<unsigned%> and %<void%> in " 7426169699Skan "declaration specifiers"); 7427169699Skan else if (specs->complex_p) 7428169699Skan error ("both %<complex%> and %<void%> in " 7429169699Skan "declaration specifiers"); 7430169699Skan else 7431169699Skan specs->typespec_word = cts_void; 7432169699Skan return specs; 7433169699Skan case RID_BOOL: 7434169699Skan if (specs->long_p) 7435169699Skan error ("both %<long%> and %<_Bool%> in " 7436169699Skan "declaration specifiers"); 7437169699Skan else if (specs->short_p) 7438169699Skan error ("both %<short%> and %<_Bool%> in " 7439169699Skan "declaration specifiers"); 7440169699Skan else if (specs->signed_p) 7441169699Skan error ("both %<signed%> and %<_Bool%> in " 7442169699Skan "declaration specifiers"); 7443169699Skan else if (specs->unsigned_p) 7444169699Skan error ("both %<unsigned%> and %<_Bool%> in " 7445169699Skan "declaration specifiers"); 7446169699Skan else if (specs->complex_p) 7447169699Skan error ("both %<complex%> and %<_Bool%> in " 7448169699Skan "declaration specifiers"); 7449169699Skan else 7450169699Skan specs->typespec_word = cts_bool; 7451169699Skan return specs; 7452169699Skan case RID_CHAR: 7453169699Skan if (specs->long_p) 7454169699Skan error ("both %<long%> and %<char%> in " 7455169699Skan "declaration specifiers"); 7456169699Skan else if (specs->short_p) 7457169699Skan error ("both %<short%> and %<char%> in " 7458169699Skan "declaration specifiers"); 7459169699Skan else 7460169699Skan specs->typespec_word = cts_char; 7461169699Skan return specs; 7462169699Skan case RID_INT: 7463169699Skan specs->typespec_word = cts_int; 7464169699Skan return specs; 7465169699Skan case RID_FLOAT: 7466169699Skan if (specs->long_p) 7467169699Skan error ("both %<long%> and %<float%> in " 7468169699Skan "declaration specifiers"); 7469169699Skan else if (specs->short_p) 7470169699Skan error ("both %<short%> and %<float%> in " 7471169699Skan "declaration specifiers"); 7472169699Skan else if (specs->signed_p) 7473169699Skan error ("both %<signed%> and %<float%> in " 7474169699Skan "declaration specifiers"); 7475169699Skan else if (specs->unsigned_p) 7476169699Skan error ("both %<unsigned%> and %<float%> in " 7477169699Skan "declaration specifiers"); 7478169699Skan else 7479169699Skan specs->typespec_word = cts_float; 7480169699Skan return specs; 7481169699Skan case RID_DOUBLE: 7482169699Skan if (specs->long_long_p) 7483169699Skan error ("both %<long long%> and %<double%> in " 7484169699Skan "declaration specifiers"); 7485169699Skan else if (specs->short_p) 7486169699Skan error ("both %<short%> and %<double%> in " 7487169699Skan "declaration specifiers"); 7488169699Skan else if (specs->signed_p) 7489169699Skan error ("both %<signed%> and %<double%> in " 7490169699Skan "declaration specifiers"); 7491169699Skan else if (specs->unsigned_p) 7492169699Skan error ("both %<unsigned%> and %<double%> in " 7493169699Skan "declaration specifiers"); 7494169699Skan else 7495169699Skan specs->typespec_word = cts_double; 7496169699Skan return specs; 7497169699Skan case RID_DFLOAT32: 7498169699Skan case RID_DFLOAT64: 7499169699Skan case RID_DFLOAT128: 7500169699Skan { 7501169699Skan const char *str; 7502169699Skan if (i == RID_DFLOAT32) 7503169699Skan str = "_Decimal32"; 7504169699Skan else if (i == RID_DFLOAT64) 7505169699Skan str = "_Decimal64"; 7506169699Skan else 7507169699Skan str = "_Decimal128"; 7508169699Skan if (specs->long_long_p) 7509169699Skan error ("both %<long long%> and %<%s%> in " 7510169699Skan "declaration specifiers", str); 7511169699Skan if (specs->long_p) 7512169699Skan error ("both %<long%> and %<%s%> in " 7513169699Skan "declaration specifiers", str); 7514169699Skan else if (specs->short_p) 7515169699Skan error ("both %<short%> and %<%s%> in " 7516169699Skan "declaration specifiers", str); 7517169699Skan else if (specs->signed_p) 7518169699Skan error ("both %<signed%> and %<%s%> in " 7519169699Skan "declaration specifiers", str); 7520169699Skan else if (specs->unsigned_p) 7521169699Skan error ("both %<unsigned%> and %<%s%> in " 7522169699Skan "declaration specifiers", str); 7523169699Skan else if (specs->complex_p) 7524169699Skan error ("both %<complex%> and %<%s%> in " 7525169699Skan "declaration specifiers", str); 7526169699Skan else if (i == RID_DFLOAT32) 7527169699Skan specs->typespec_word = cts_dfloat32; 7528169699Skan else if (i == RID_DFLOAT64) 7529169699Skan specs->typespec_word = cts_dfloat64; 7530169699Skan else 7531169699Skan specs->typespec_word = cts_dfloat128; 7532169699Skan } 7533169699Skan if (!targetm.decimal_float_supported_p ()) 7534169699Skan error ("decimal floating point not supported for this target"); 7535169699Skan if (pedantic) 7536169699Skan pedwarn ("ISO C does not support decimal floating point"); 7537169699Skan return specs; 7538169699Skan default: 7539169699Skan /* ObjC reserved word "id", handled below. */ 7540169699Skan break; 7541169699Skan } 7542169699Skan } 7543169699Skan } 7544132730Skan 7545169699Skan /* Now we have a typedef (a TYPE_DECL node), an identifier (some 7546169699Skan form of ObjC type, cases such as "int" and "long" being handled 7547169699Skan above), a TYPE (struct, union, enum and typeof specifiers) or an 7548169699Skan ERROR_MARK. In none of these cases may there have previously 7549169699Skan been any type specifiers. */ 7550169699Skan if (specs->type || specs->typespec_word != cts_none 7551169699Skan || specs->long_p || specs->short_p || specs->signed_p 7552169699Skan || specs->unsigned_p || specs->complex_p) 7553169699Skan error ("two or more data types in declaration specifiers"); 7554169699Skan else if (TREE_CODE (type) == TYPE_DECL) 7555169699Skan { 7556169699Skan if (TREE_TYPE (type) == error_mark_node) 7557169699Skan ; /* Allow the type to default to int to avoid cascading errors. */ 7558169699Skan else 7559132730Skan { 7560169699Skan specs->type = TREE_TYPE (type); 7561169699Skan specs->decl_attr = DECL_ATTRIBUTES (type); 7562169699Skan specs->typedef_p = true; 7563169699Skan specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type); 7564169699Skan } 7565169699Skan } 7566169699Skan else if (TREE_CODE (type) == IDENTIFIER_NODE) 7567169699Skan { 7568169699Skan tree t = lookup_name (type); 7569169699Skan if (!t || TREE_CODE (t) != TYPE_DECL) 7570169699Skan error ("%qE fails to be a typedef or built in type", type); 7571169699Skan else if (TREE_TYPE (t) == error_mark_node) 7572169699Skan ; 7573169699Skan else 7574169699Skan specs->type = TREE_TYPE (t); 7575169699Skan } 7576169699Skan else if (TREE_CODE (type) != ERROR_MARK) 7577169699Skan { 7578169699Skan if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref) 7579169699Skan specs->tag_defined_p = true; 7580169699Skan if (spec.kind == ctsk_typeof) 7581169699Skan specs->typedef_p = true; 7582169699Skan specs->type = type; 7583169699Skan } 7584132730Skan 7585169699Skan return specs; 7586169699Skan} 7587132730Skan 7588169699Skan/* Add the storage class specifier or function specifier SCSPEC to the 7589169699Skan declaration specifiers SPECS, returning SPECS. */ 7590169699Skan 7591169699Skanstruct c_declspecs * 7592169699Skandeclspecs_add_scspec (struct c_declspecs *specs, tree scspec) 7593169699Skan{ 7594169699Skan enum rid i; 7595169699Skan enum c_storage_class n = csc_none; 7596169699Skan bool dupe = false; 7597169699Skan specs->declspecs_seen_p = true; 7598169699Skan gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE 7599169699Skan && C_IS_RESERVED_WORD (scspec)); 7600169699Skan i = C_RID_CODE (scspec); 7601169699Skan if (extra_warnings && specs->non_sc_seen_p) 7602169699Skan warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec); 7603169699Skan switch (i) 7604169699Skan { 7605169699Skan case RID_INLINE: 7606169699Skan /* C99 permits duplicate inline. Although of doubtful utility, 7607169699Skan it seems simplest to permit it in gnu89 mode as well, as 7608169699Skan there is also little utility in maintaining this as a 7609169699Skan difference between gnu89 and C99 inline. */ 7610169699Skan dupe = false; 7611169699Skan specs->inline_p = true; 7612169699Skan break; 7613169699Skan case RID_THREAD: 7614169699Skan dupe = specs->thread_p; 7615169699Skan if (specs->storage_class == csc_auto) 7616169699Skan error ("%<__thread%> used with %<auto%>"); 7617169699Skan else if (specs->storage_class == csc_register) 7618169699Skan error ("%<__thread%> used with %<register%>"); 7619169699Skan else if (specs->storage_class == csc_typedef) 7620169699Skan error ("%<__thread%> used with %<typedef%>"); 7621169699Skan else 7622169699Skan specs->thread_p = true; 7623169699Skan break; 7624169699Skan case RID_AUTO: 7625169699Skan n = csc_auto; 7626169699Skan break; 7627169699Skan case RID_EXTERN: 7628169699Skan n = csc_extern; 7629169699Skan /* Diagnose "__thread extern". */ 7630169699Skan if (specs->thread_p) 7631169699Skan error ("%<__thread%> before %<extern%>"); 7632169699Skan break; 7633169699Skan case RID_REGISTER: 7634169699Skan n = csc_register; 7635169699Skan break; 7636169699Skan case RID_STATIC: 7637169699Skan n = csc_static; 7638169699Skan /* Diagnose "__thread static". */ 7639169699Skan if (specs->thread_p) 7640169699Skan error ("%<__thread%> before %<static%>"); 7641169699Skan break; 7642169699Skan case RID_TYPEDEF: 7643169699Skan n = csc_typedef; 7644169699Skan break; 7645169699Skan default: 7646169699Skan gcc_unreachable (); 7647169699Skan } 7648169699Skan if (n != csc_none && n == specs->storage_class) 7649169699Skan dupe = true; 7650169699Skan if (dupe) 7651169699Skan error ("duplicate %qE", scspec); 7652169699Skan if (n != csc_none) 7653169699Skan { 7654169699Skan if (specs->storage_class != csc_none && n != specs->storage_class) 7655169699Skan { 7656169699Skan error ("multiple storage classes in declaration specifiers"); 7657132730Skan } 7658169699Skan else 7659169699Skan { 7660169699Skan specs->storage_class = n; 7661169699Skan if (n != csc_extern && n != csc_static && specs->thread_p) 7662169699Skan { 7663169699Skan error ("%<__thread%> used with %qE", scspec); 7664169699Skan specs->thread_p = false; 7665169699Skan } 7666169699Skan } 7667169699Skan } 7668169699Skan return specs; 7669169699Skan} 7670132730Skan 7671169699Skan/* Add the attributes ATTRS to the declaration specifiers SPECS, 7672169699Skan returning SPECS. */ 7673169699Skan 7674169699Skanstruct c_declspecs * 7675169699Skandeclspecs_add_attrs (struct c_declspecs *specs, tree attrs) 7676169699Skan{ 7677169699Skan specs->attrs = chainon (attrs, specs->attrs); 7678169699Skan specs->declspecs_seen_p = true; 7679169699Skan return specs; 7680132730Skan} 7681132730Skan 7682169699Skan/* Combine "long", "short", "signed", "unsigned" and "_Complex" type 7683169699Skan specifiers with any other type specifier to determine the resulting 7684169699Skan type. This is where ISO C checks on complex types are made, since 7685169699Skan "_Complex long" is a prefix of the valid ISO C type "_Complex long 7686169699Skan double". */ 7687132730Skan 7688169699Skanstruct c_declspecs * 7689169699Skanfinish_declspecs (struct c_declspecs *specs) 7690132730Skan{ 7691169699Skan /* If a type was specified as a whole, we have no modifiers and are 7692169699Skan done. */ 7693169699Skan if (specs->type != NULL_TREE) 7694169699Skan { 7695169699Skan gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p 7696169699Skan && !specs->signed_p && !specs->unsigned_p 7697169699Skan && !specs->complex_p); 7698169699Skan return specs; 7699169699Skan } 7700132730Skan 7701169699Skan /* If none of "void", "_Bool", "char", "int", "float" or "double" 7702169699Skan has been specified, treat it as "int" unless "_Complex" is 7703169699Skan present and there are no other specifiers. If we just have 7704169699Skan "_Complex", it is equivalent to "_Complex double", but e.g. 7705169699Skan "_Complex short" is equivalent to "_Complex short int". */ 7706169699Skan if (specs->typespec_word == cts_none) 7707132730Skan { 7708169699Skan if (specs->long_p || specs->short_p 7709169699Skan || specs->signed_p || specs->unsigned_p) 7710169699Skan { 7711169699Skan specs->typespec_word = cts_int; 7712169699Skan } 7713169699Skan else if (specs->complex_p) 7714169699Skan { 7715169699Skan specs->typespec_word = cts_double; 7716169699Skan if (pedantic) 7717169699Skan pedwarn ("ISO C does not support plain %<complex%> meaning " 7718169699Skan "%<double complex%>"); 7719169699Skan } 7720169699Skan else 7721169699Skan { 7722169699Skan specs->typespec_word = cts_int; 7723169699Skan specs->default_int_p = true; 7724169699Skan /* We don't diagnose this here because grokdeclarator will 7725169699Skan give more specific diagnostics according to whether it is 7726169699Skan a function definition. */ 7727169699Skan } 7728169699Skan } 7729132730Skan 7730169699Skan /* If "signed" was specified, record this to distinguish "int" and 7731169699Skan "signed int" in the case of a bit-field with 7732169699Skan -funsigned-bitfields. */ 7733169699Skan specs->explicit_signed_p = specs->signed_p; 7734132730Skan 7735169699Skan /* Now compute the actual type. */ 7736169699Skan switch (specs->typespec_word) 7737169699Skan { 7738169699Skan case cts_void: 7739169699Skan gcc_assert (!specs->long_p && !specs->short_p 7740169699Skan && !specs->signed_p && !specs->unsigned_p 7741169699Skan && !specs->complex_p); 7742169699Skan specs->type = void_type_node; 7743169699Skan break; 7744169699Skan case cts_bool: 7745169699Skan gcc_assert (!specs->long_p && !specs->short_p 7746169699Skan && !specs->signed_p && !specs->unsigned_p 7747169699Skan && !specs->complex_p); 7748169699Skan specs->type = boolean_type_node; 7749169699Skan break; 7750169699Skan case cts_char: 7751169699Skan gcc_assert (!specs->long_p && !specs->short_p); 7752169699Skan gcc_assert (!(specs->signed_p && specs->unsigned_p)); 7753169699Skan if (specs->signed_p) 7754169699Skan specs->type = signed_char_type_node; 7755169699Skan else if (specs->unsigned_p) 7756169699Skan specs->type = unsigned_char_type_node; 7757169699Skan else 7758169699Skan specs->type = char_type_node; 7759169699Skan if (specs->complex_p) 7760169699Skan { 7761169699Skan if (pedantic) 7762169699Skan pedwarn ("ISO C does not support complex integer types"); 7763169699Skan specs->type = build_complex_type (specs->type); 7764169699Skan } 7765169699Skan break; 7766169699Skan case cts_int: 7767169699Skan gcc_assert (!(specs->long_p && specs->short_p)); 7768169699Skan gcc_assert (!(specs->signed_p && specs->unsigned_p)); 7769169699Skan if (specs->long_long_p) 7770169699Skan specs->type = (specs->unsigned_p 7771169699Skan ? long_long_unsigned_type_node 7772169699Skan : long_long_integer_type_node); 7773169699Skan else if (specs->long_p) 7774169699Skan specs->type = (specs->unsigned_p 7775169699Skan ? long_unsigned_type_node 7776169699Skan : long_integer_type_node); 7777169699Skan else if (specs->short_p) 7778169699Skan specs->type = (specs->unsigned_p 7779169699Skan ? short_unsigned_type_node 7780169699Skan : short_integer_type_node); 7781169699Skan else 7782169699Skan specs->type = (specs->unsigned_p 7783169699Skan ? unsigned_type_node 7784169699Skan : integer_type_node); 7785169699Skan if (specs->complex_p) 7786169699Skan { 7787169699Skan if (pedantic) 7788169699Skan pedwarn ("ISO C does not support complex integer types"); 7789169699Skan specs->type = build_complex_type (specs->type); 7790169699Skan } 7791169699Skan break; 7792169699Skan case cts_float: 7793169699Skan gcc_assert (!specs->long_p && !specs->short_p 7794169699Skan && !specs->signed_p && !specs->unsigned_p); 7795169699Skan specs->type = (specs->complex_p 7796169699Skan ? complex_float_type_node 7797169699Skan : float_type_node); 7798169699Skan break; 7799169699Skan case cts_double: 7800169699Skan gcc_assert (!specs->long_long_p && !specs->short_p 7801169699Skan && !specs->signed_p && !specs->unsigned_p); 7802169699Skan if (specs->long_p) 7803169699Skan { 7804169699Skan specs->type = (specs->complex_p 7805169699Skan ? complex_long_double_type_node 7806169699Skan : long_double_type_node); 7807169699Skan } 7808169699Skan else 7809169699Skan { 7810169699Skan specs->type = (specs->complex_p 7811169699Skan ? complex_double_type_node 7812169699Skan : double_type_node); 7813169699Skan } 7814169699Skan break; 7815169699Skan case cts_dfloat32: 7816169699Skan case cts_dfloat64: 7817169699Skan case cts_dfloat128: 7818169699Skan gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p 7819169699Skan && !specs->signed_p && !specs->unsigned_p && !specs->complex_p); 7820169699Skan if (specs->typespec_word == cts_dfloat32) 7821169699Skan specs->type = dfloat32_type_node; 7822169699Skan else if (specs->typespec_word == cts_dfloat64) 7823169699Skan specs->type = dfloat64_type_node; 7824169699Skan else 7825169699Skan specs->type = dfloat128_type_node; 7826169699Skan break; 7827169699Skan default: 7828169699Skan gcc_unreachable (); 7829169699Skan } 7830132730Skan 7831169699Skan return specs; 7832169699Skan} 7833132730Skan 7834169699Skan/* Synthesize a function which calls all the global ctors or global 7835169699Skan dtors in this file. This is only used for targets which do not 7836169699Skan support .ctors/.dtors sections. FIXME: Migrate into cgraph. */ 7837169699Skanstatic void 7838169699Skanbuild_cdtor (int method_type, tree cdtors) 7839169699Skan{ 7840169699Skan tree body = 0; 7841132730Skan 7842169699Skan if (!cdtors) 7843169699Skan return; 7844169699Skan 7845169699Skan for (; cdtors; cdtors = TREE_CHAIN (cdtors)) 7846169699Skan append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0), 7847169699Skan &body); 7848169699Skan 7849169699Skan cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY); 7850169699Skan} 7851169699Skan 7852169699Skan/* A subroutine of c_write_global_declarations. Perform final processing 7853169699Skan on one file scope's declarations (or the external scope's declarations), 7854169699Skan GLOBALS. */ 7855169699Skan 7856169699Skanstatic void 7857169699Skanc_write_global_declarations_1 (tree globals) 7858169699Skan{ 7859169699Skan tree decl; 7860169699Skan bool reconsider; 7861169699Skan 7862169699Skan /* Process the decls in the order they were written. */ 7863169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 7864169699Skan { 7865169699Skan /* Check for used but undefined static functions using the C 7866169699Skan standard's definition of "used", and set TREE_NO_WARNING so 7867169699Skan that check_global_declarations doesn't repeat the check. */ 7868169699Skan if (TREE_CODE (decl) == FUNCTION_DECL 7869169699Skan && DECL_INITIAL (decl) == 0 7870169699Skan && DECL_EXTERNAL (decl) 7871169699Skan && !TREE_PUBLIC (decl) 7872169699Skan && C_DECL_USED (decl)) 7873169699Skan { 7874169699Skan pedwarn ("%q+F used but never defined", decl); 7875169699Skan TREE_NO_WARNING (decl) = 1; 7876169699Skan } 7877169699Skan 7878169699Skan wrapup_global_declaration_1 (decl); 7879132730Skan } 7880169699Skan 7881169699Skan do 7882169699Skan { 7883169699Skan reconsider = false; 7884169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 7885169699Skan reconsider |= wrapup_global_declaration_2 (decl); 7886169699Skan } 7887169699Skan while (reconsider); 7888169699Skan 7889169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 7890169699Skan check_global_declaration_1 (decl); 7891132730Skan} 7892132730Skan 7893169699Skan/* A subroutine of c_write_global_declarations Emit debug information for each 7894169699Skan of the declarations in GLOBALS. */ 7895132730Skan 7896169699Skanstatic void 7897169699Skanc_write_global_declarations_2 (tree globals) 7898169699Skan{ 7899169699Skan tree decl; 7900169699Skan 7901169699Skan for (decl = globals; decl ; decl = TREE_CHAIN (decl)) 7902169699Skan debug_hooks->global_decl (decl); 7903169699Skan} 7904169699Skan 7905169699Skan/* Preserve the external declarations scope across a garbage collect. */ 7906169699Skanstatic GTY(()) tree ext_block; 7907169699Skan 7908132730Skanvoid 7909169699Skanc_write_global_declarations (void) 7910132730Skan{ 7911169699Skan tree t; 7912132730Skan 7913169699Skan /* We don't want to do this if generating a PCH. */ 7914169699Skan if (pch_file) 7915169699Skan return; 7916132730Skan 7917169699Skan /* Don't waste time on further processing if -fsyntax-only or we've 7918169699Skan encountered errors. */ 7919169699Skan if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in)) 7920169699Skan return; 7921132730Skan 7922169699Skan /* Close the external scope. */ 7923169699Skan ext_block = pop_scope (); 7924169699Skan external_scope = 0; 7925169699Skan gcc_assert (!current_scope); 7926169699Skan 7927169699Skan if (ext_block) 7928169699Skan { 7929169699Skan tree tmp = BLOCK_VARS (ext_block); 7930169699Skan int flags; 7931169699Skan FILE * stream = dump_begin (TDI_tu, &flags); 7932169699Skan if (stream && tmp) 7933169699Skan { 7934169699Skan dump_node (tmp, flags & ~TDF_SLIM, stream); 7935169699Skan dump_end (TDI_tu, stream); 7936169699Skan } 7937169699Skan } 7938169699Skan 7939169699Skan /* Process all file scopes in this compilation, and the external_scope, 7940169699Skan through wrapup_global_declarations and check_global_declarations. */ 7941169699Skan for (t = all_translation_units; t; t = TREE_CHAIN (t)) 7942169699Skan c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t))); 7943169699Skan c_write_global_declarations_1 (BLOCK_VARS (ext_block)); 7944169699Skan 7945169699Skan /* Generate functions to call static constructors and destructors 7946169699Skan for targets that do not support .ctors/.dtors sections. These 7947169699Skan functions have magic names which are detected by collect2. */ 7948169699Skan build_cdtor ('I', static_ctors); static_ctors = 0; 7949169699Skan build_cdtor ('D', static_dtors); static_dtors = 0; 7950169699Skan 7951169699Skan /* We're done parsing; proceed to optimize and emit assembly. 7952169699Skan FIXME: shouldn't be the front end's responsibility to call this. */ 7953169699Skan cgraph_optimize (); 7954169699Skan 7955169699Skan /* After cgraph has had a chance to emit everything that's going to 7956169699Skan be emitted, output debug information for globals. */ 7957169699Skan if (errorcount == 0 && sorrycount == 0) 7958169699Skan { 7959169699Skan timevar_push (TV_SYMOUT); 7960169699Skan for (t = all_translation_units; t; t = TREE_CHAIN (t)) 7961169699Skan c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t))); 7962169699Skan c_write_global_declarations_2 (BLOCK_VARS (ext_block)); 7963169699Skan timevar_pop (TV_SYMOUT); 7964169699Skan } 7965169699Skan 7966169699Skan ext_block = NULL; 7967132730Skan} 7968132730Skan 7969117421Skan#include "gt-c-decl.h" 7970