118334Speter/* Process declarations and variables for C compiler. 290075Sobrien Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 3189824Sdas 2001, 2002, 2003, 2004, 2005, 2006, 2007 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$ */ 23189824Sdas/* Merged C99 inline changes from gcc trunk 122565 2007-03-05 */ 24189824Sdas/* Fixed problems with compiling inline-25.c and inline-26.c */ 25189824Sdas/* XXX still fails inline-29.c, inline-31.c, and inline-32.c */ 2696549Sobrien 2718334Speter/* Process declarations and symbol lookup for C front end. 2818334Speter Also constructs types; the standard scalar types at initialization, 2918334Speter and structure, union, array and enum types when they are declared. */ 3018334Speter 3118334Speter/* ??? not all decl nodes are given the most useful possible 3218334Speter line numbers. For example, the CONST_DECLs for enum values. */ 3318334Speter 3418334Speter#include "config.h" 3550397Sobrien#include "system.h" 36132730Skan#include "coretypes.h" 37169699Skan#include "input.h" 38132730Skan#include "tm.h" 3990075Sobrien#include "intl.h" 4018334Speter#include "tree.h" 4190075Sobrien#include "tree-inline.h" 4290075Sobrien#include "rtl.h" 4318334Speter#include "flags.h" 4490075Sobrien#include "function.h" 4518334Speter#include "output.h" 4690075Sobrien#include "expr.h" 4718334Speter#include "c-tree.h" 4850397Sobrien#include "toplev.h" 4990075Sobrien#include "ggc.h" 5090075Sobrien#include "tm_p.h" 5150397Sobrien#include "cpplib.h" 5290075Sobrien#include "target.h" 5390075Sobrien#include "debug.h" 54132730Skan#include "opts.h" 5590075Sobrien#include "timevar.h" 5690075Sobrien#include "c-common.h" 5796263Sobrien#include "c-pragma.h" 58169699Skan#include "langhooks.h" 59169699Skan#include "tree-mudflap.h" 60169699Skan#include "tree-gimple.h" 61169699Skan#include "diagnostic.h" 62169699Skan#include "tree-dump.h" 63132730Skan#include "cgraph.h" 64132730Skan#include "hashtab.h" 65117421Skan#include "libfuncs.h" 66117421Skan#include "except.h" 67132730Skan#include "langhooks-def.h" 68169699Skan#include "pointer-set.h" 6950397Sobrien 7018334Speter/* In grokdeclarator, distinguish syntactic contexts of declarators. */ 7118334Speterenum decl_context 7218334Speter{ NORMAL, /* Ordinary declaration */ 7318334Speter FUNCDEF, /* Function definition */ 74261188Spfg /* APPLE LOCAL blocks 6339747 */ 75261188Spfg BLOCKDEF, /* Block literal declaration */ 7618334Speter PARM, /* Declaration of parm before function body */ 7718334Speter FIELD, /* Declaration inside struct or union */ 7818334Speter TYPENAME}; /* Typename (inside cast or sizeof) */ 7918334Speter 8018334Speter 8118334Speter/* Nonzero if we have seen an invalid cross reference 8218334Speter to a struct, union, or enum, but not yet printed the message. */ 83169699Skantree pending_invalid_xref; 8418334Speter 8518334Speter/* File and line to appear in the eventual error message. */ 86132730Skanlocation_t pending_invalid_xref_location; 8718334Speter 88169699Skan/* True means we've initialized exception handling. */ 89169699Skanbool c_eh_initialized_p; 90169699Skan 9118334Speter/* While defining an enum type, this is 1 plus the last enumerator 9218334Speter constant value. Note that will do not have to save this or `enum_overflow' 9318334Speter around nested function definition since such a definition could only 9418334Speter occur in an enum value expression and we don't use these variables in 9518334Speter that case. */ 9618334Speter 9718334Speterstatic tree enum_next_value; 9818334Speter 9918334Speter/* Nonzero means that there was overflow computing enum_next_value. */ 10018334Speter 10118334Speterstatic int enum_overflow; 10218334Speter 103169699Skan/* The file and line that the prototype came from if this is an 104169699Skan old-style definition; used for diagnostics in 105169699Skan store_parm_decls_oldstyle. */ 10618334Speter 107169699Skanstatic location_t current_function_prototype_locus; 10818334Speter 109169699Skan/* Whether this prototype was built-in. */ 11018334Speter 111169699Skanstatic bool current_function_prototype_built_in; 11218334Speter 113169699Skan/* The argument type information of this prototype. */ 114132730Skan 115169699Skanstatic tree current_function_prototype_arg_types; 116132730Skan 117169699Skan/* The argument information structure for the function currently being 118169699Skan defined. */ 11918334Speter 120169699Skanstatic struct c_arg_info *current_function_arg_info; 12118334Speter 122169699Skan/* The obstack on which parser and related data structures, which are 123169699Skan not live beyond their top-level declaration or definition, are 124169699Skan allocated. */ 125169699Skanstruct obstack parser_obstack; 126132730Skan 12790075Sobrien/* The current statement tree. */ 12890075Sobrien 129117421Skanstatic GTY(()) struct stmt_tree_s c_stmt_tree; 13090075Sobrien 131132730Skan/* State saving variables. */ 132169699Skantree c_break_label; 133169699Skantree c_cont_label; 13418334Speter 135169699Skan/* Linked list of TRANSLATION_UNIT_DECLS for the translation units 136169699Skan included in this invocation. Note that the current translation 137169699Skan unit is not included in this list. */ 13818334Speter 139169699Skanstatic GTY(()) tree all_translation_units; 14018334Speter 141169699Skan/* A list of decls to be made automatically visible in each file scope. */ 142169699Skanstatic GTY(()) tree visible_builtins; 14318334Speter 14418334Speter/* Set to 0 at beginning of a function definition, set to 1 if 14518334Speter a return statement that specifies a return value is seen. */ 14618334Speter 14718334Speterint current_function_returns_value; 14818334Speter 14918334Speter/* Set to 0 at beginning of a function definition, set to 1 if 15018334Speter a return statement with no argument is seen. */ 15118334Speter 15218334Speterint current_function_returns_null; 15318334Speter 15496263Sobrien/* Set to 0 at beginning of a function definition, set to 1 if 15596263Sobrien a call to a noreturn function is seen. */ 15696263Sobrien 15796263Sobrienint current_function_returns_abnormally; 15896263Sobrien 15918334Speter/* Set to nonzero by `grokdeclarator' for a function 16018334Speter whose return type is defaulted, if warnings for this are desired. */ 16118334Speter 16218334Speterstatic int warn_about_return_type; 16318334Speter 164169699Skan/* Nonzero when the current toplevel function contains a declaration 165169699Skan of a nested function which is never defined. */ 166169699Skan 167169699Skanstatic bool undef_nested_function; 168169699Skan 169169699Skan/* True means global_bindings_p should return false even if the scope stack 170169699Skan says we are in file scope. */ 171169699Skanbool c_override_global_bindings_to_false; 172169699Skan 17318334Speter 174169699Skan/* Each c_binding structure describes one binding of an identifier to 175169699Skan a decl. All the decls in a scope - irrespective of namespace - are 176169699Skan chained together by the ->prev field, which (as the name implies) 177169699Skan runs in reverse order. All the decls in a given namespace bound to 178169699Skan a given identifier are chained by the ->shadowed field, which runs 179169699Skan from inner to outer scopes. 18018334Speter 181169699Skan The ->decl field usually points to a DECL node, but there are two 182169699Skan exceptions. In the namespace of type tags, the bound entity is a 183169699Skan RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared 184169699Skan identifier is encountered, it is bound to error_mark_node to 185169699Skan suppress further errors about that identifier in the current 186169699Skan function. 187169699Skan 188169699Skan The ->type field stores the type of the declaration in this scope; 189169699Skan if NULL, the type is the type of the ->decl field. This is only of 190169699Skan relevance for objects with external or internal linkage which may 191169699Skan be redeclared in inner scopes, forming composite types that only 192169699Skan persist for the duration of those scopes. In the external scope, 193169699Skan this stores the composite of all the types declared for this 194169699Skan object, visible or not. The ->inner_comp field (used only at file 195169699Skan scope) stores whether an incomplete array type at file scope was 196169699Skan completed at an inner scope to an array size other than 1. 197169699Skan 198169699Skan The depth field is copied from the scope structure that holds this 199169699Skan decl. It is used to preserve the proper ordering of the ->shadowed 200169699Skan field (see bind()) and also for a handful of special-case checks. 201169699Skan Finally, the invisible bit is true for a decl which should be 202169699Skan ignored for purposes of normal name lookup, and the nested bit is 203169699Skan true for a decl that's been bound a second time in an inner scope; 204169699Skan in all such cases, the binding in the outer scope will have its 205169699Skan invisible bit true. */ 206169699Skan 207169699Skanstruct c_binding GTY((chain_next ("%h.prev"))) 208169699Skan{ 209169699Skan tree decl; /* the decl bound */ 210169699Skan tree type; /* the type in this scope */ 211169699Skan tree id; /* the identifier it's bound to */ 212169699Skan struct c_binding *prev; /* the previous decl in this scope */ 213169699Skan struct c_binding *shadowed; /* the innermost decl shadowed by this one */ 214169699Skan unsigned int depth : 28; /* depth of this scope */ 215169699Skan BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */ 216169699Skan BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */ 217169699Skan BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */ 218169699Skan /* one free bit */ 219169699Skan}; 220169699Skan#define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth) 221169699Skan#define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth) 222169699Skan#define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/) 223169699Skan#define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/) 224169699Skan 225169699Skan#define I_SYMBOL_BINDING(node) \ 226169699Skan (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding) 227169699Skan#define I_SYMBOL_DECL(node) \ 228169699Skan (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0) 229169699Skan 230169699Skan#define I_TAG_BINDING(node) \ 231169699Skan (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding) 232169699Skan#define I_TAG_DECL(node) \ 233169699Skan (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0) 234169699Skan 235169699Skan#define I_LABEL_BINDING(node) \ 236169699Skan (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding) 237169699Skan#define I_LABEL_DECL(node) \ 238169699Skan (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0) 239169699Skan 240169699Skan/* Each C symbol points to three linked lists of c_binding structures. 241169699Skan These describe the values of the identifier in the three different 242169699Skan namespaces defined by the language. */ 243169699Skan 244169699Skanstruct lang_identifier GTY(()) 245169699Skan{ 246169699Skan struct c_common_identifier common_id; 247169699Skan struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */ 248169699Skan struct c_binding *tag_binding; /* struct/union/enum tags */ 249169699Skan struct c_binding *label_binding; /* labels */ 250169699Skan}; 251169699Skan 252169699Skan/* Validate c-lang.c's assumptions. */ 253169699Skanextern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate 254169699Skan[(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1]; 255169699Skan 256169699Skan/* The resulting tree type. */ 257169699Skan 258169699Skanunion lang_tree_node 259169699Skan GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"), 260169699Skan 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)"))) 261169699Skan{ 262169699Skan union tree_node GTY ((tag ("0"), 263169699Skan desc ("tree_node_structure (&%h)"))) 264169699Skan generic; 265169699Skan struct lang_identifier GTY ((tag ("1"))) identifier; 266169699Skan}; 267169699Skan 268169699Skan/* Each c_scope structure describes the complete contents of one 269169699Skan scope. Four scopes are distinguished specially: the innermost or 270169699Skan current scope, the innermost function scope, the file scope (always 271169699Skan the second to outermost) and the outermost or external scope. 272169699Skan 273132730Skan Most declarations are recorded in the current scope. 27418334Speter 275132730Skan All normal label declarations are recorded in the innermost 276132730Skan function scope, as are bindings of undeclared identifiers to 277132730Skan error_mark_node. (GCC permits nested functions as an extension, 278132730Skan hence the 'innermost' qualifier.) Explicitly declared labels 279132730Skan (using the __label__ extension) appear in the current scope. 28018334Speter 281169699Skan Being in the file scope (current_scope == file_scope) causes 282132730Skan special behavior in several places below. Also, under some 283132730Skan conditions the Objective-C front end records declarations in the 284169699Skan file scope even though that isn't the current scope. 28518334Speter 286169699Skan All declarations with external linkage are recorded in the external 287169699Skan scope, even if they aren't visible there; this models the fact that 288169699Skan such declarations are visible to the entire program, and (with a 289169699Skan bit of cleverness, see pushdecl) allows diagnosis of some violations 290169699Skan of C99 6.2.2p7 and 6.2.7p2: 29118334Speter 292169699Skan If, within the same translation unit, the same identifier appears 293169699Skan with both internal and external linkage, the behavior is 294169699Skan undefined. 29518334Speter 296169699Skan All declarations that refer to the same object or function shall 297169699Skan have compatible type; otherwise, the behavior is undefined. 298169699Skan 299169699Skan Initially only the built-in declarations, which describe compiler 300169699Skan intrinsic functions plus a subset of the standard library, are in 301169699Skan this scope. 302169699Skan 303169699Skan The order of the blocks list matters, and it is frequently appended 304169699Skan to. To avoid having to walk all the way to the end of the list on 305169699Skan each insertion, or reverse the list later, we maintain a pointer to 306169699Skan the last list entry. (FIXME: It should be feasible to use a reversed 307169699Skan list here.) 308169699Skan 309169699Skan The bindings list is strictly in reverse order of declarations; 310169699Skan pop_scope relies on this. */ 311169699Skan 312169699Skan 313169699Skanstruct c_scope GTY((chain_next ("%h.outer"))) 314132730Skan{ 315132730Skan /* The scope containing this one. */ 316132730Skan struct c_scope *outer; 31718334Speter 318132730Skan /* The next outermost function scope. */ 319132730Skan struct c_scope *outer_function; 32018334Speter 321169699Skan /* All bindings in this scope. */ 322169699Skan struct c_binding *bindings; 32318334Speter 324132730Skan /* For each scope (except the global one), a chain of BLOCK nodes 325132730Skan for all the scopes that were entered and exited one level down. */ 326132730Skan tree blocks; 327132730Skan tree blocks_last; 32818334Speter 329169699Skan /* The depth of this scope. Used to keep the ->shadowed chain of 330169699Skan bindings sorted innermost to outermost. */ 331169699Skan unsigned int depth : 28; 332169699Skan 333132730Skan /* True if we are currently filling this scope with parameter 334132730Skan declarations. */ 335132730Skan BOOL_BITFIELD parm_flag : 1; 33618334Speter 337169699Skan /* True if we saw [*] in this scope. Used to give an error messages 338169699Skan if these appears in a function definition. */ 339169699Skan BOOL_BITFIELD had_vla_unspec : 1; 340169699Skan 341132730Skan /* True if we already complained about forward parameter decls 342132730Skan in this scope. This prevents double warnings on 343132730Skan foo (int a; int b; ...) */ 344132730Skan BOOL_BITFIELD warned_forward_parm_decls : 1; 34590075Sobrien 346132730Skan /* True if this is the outermost block scope of a function body. 347132730Skan This scope contains the parameters, the local variables declared 348132730Skan in the outermost block, and all the labels (except those in 349132730Skan nested functions, or declared at block scope with __label__). */ 350132730Skan BOOL_BITFIELD function_body : 1; 35118334Speter 352132730Skan /* True means make a BLOCK for this scope no matter what. */ 353132730Skan BOOL_BITFIELD keep : 1; 354132730Skan}; 35518334Speter 356132730Skan/* The scope currently in effect. */ 35718334Speter 358132730Skanstatic GTY(()) struct c_scope *current_scope; 35918334Speter 360132730Skan/* The innermost function scope. Ordinary (not explicitly declared) 361132730Skan labels, bindings to error_mark_node, and the lazily-created 362132730Skan bindings of __func__ and its friends get this scope. */ 36318334Speter 364132730Skanstatic GTY(()) struct c_scope *current_function_scope; 36518334Speter 366169699Skan/* The C file scope. This is reset for each input translation unit. */ 36718334Speter 368169699Skanstatic GTY(()) struct c_scope *file_scope; 36918334Speter 370169699Skan/* The outermost scope. This is used for all declarations with 371169699Skan external linkage, and only these, hence the name. */ 372169699Skan 373169699Skanstatic GTY(()) struct c_scope *external_scope; 374169699Skan 375169699Skan/* A chain of c_scope structures awaiting reuse. */ 376169699Skan 377169699Skanstatic GTY((deletable)) struct c_scope *scope_freelist; 378169699Skan 379169699Skan/* A chain of c_binding structures awaiting reuse. */ 380169699Skan 381169699Skanstatic GTY((deletable)) struct c_binding *binding_freelist; 382169699Skan 383132730Skan/* Append VAR to LIST in scope SCOPE. */ 384132730Skan#define SCOPE_LIST_APPEND(scope, list, decl) do { \ 385132730Skan struct c_scope *s_ = (scope); \ 386132730Skan tree d_ = (decl); \ 387132730Skan if (s_->list##_last) \ 388132730Skan TREE_CHAIN (s_->list##_last) = d_; \ 389132730Skan else \ 390132730Skan s_->list = d_; \ 391132730Skan s_->list##_last = d_; \ 392132730Skan} while (0) 39318334Speter 394132730Skan/* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */ 395132730Skan#define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \ 396132730Skan struct c_scope *t_ = (tscope); \ 397132730Skan struct c_scope *f_ = (fscope); \ 398132730Skan if (t_->to##_last) \ 399132730Skan TREE_CHAIN (t_->to##_last) = f_->from; \ 400132730Skan else \ 401132730Skan t_->to = f_->from; \ 402132730Skan t_->to##_last = f_->from##_last; \ 403132730Skan} while (0) 40490075Sobrien 405132730Skan/* True means unconditionally make a BLOCK for the next scope pushed. */ 40618334Speter 407132730Skanstatic bool keep_next_level_flag; 40818334Speter 409169699Skan/* True means the next call to push_scope will be the outermost scope 410132730Skan of a function body, so do not push a new scope, merely cease 411132730Skan expecting parameter decls. */ 412132730Skan 413132730Skanstatic bool next_is_function_body; 414132730Skan 41518334Speter/* Functions called automatically at the beginning and end of execution. */ 41618334Speter 417169699Skanstatic GTY(()) tree static_ctors; 418169699Skanstatic GTY(()) tree static_dtors; 41918334Speter 42018334Speter/* Forward declarations. */ 421169699Skanstatic tree lookup_name_in_scope (tree, struct c_scope *); 422169699Skanstatic tree c_make_fname_decl (tree, int); 423169699Skanstatic tree grokdeclarator (const struct c_declarator *, 424169699Skan struct c_declspecs *, 425169699Skan enum decl_context, bool, tree *); 426169699Skanstatic tree grokparms (struct c_arg_info *, bool); 427132730Skanstatic void layout_array_type (tree); 42818334Speter 429169699Skan/* T is a statement. Add it to the statement-tree. This is the 430169699Skan C/ObjC version--C++ has a slightly different version of this 431169699Skan function. */ 432169699Skan 433169699Skantree 434169699Skanadd_stmt (tree t) 435169699Skan{ 436169699Skan enum tree_code code = TREE_CODE (t); 437169699Skan 438169699Skan if (EXPR_P (t) && code != LABEL_EXPR) 439169699Skan { 440169699Skan if (!EXPR_HAS_LOCATION (t)) 441169699Skan SET_EXPR_LOCATION (t, input_location); 442169699Skan } 443169699Skan 444169699Skan if (code == LABEL_EXPR || code == CASE_LABEL_EXPR) 445169699Skan STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1; 446169699Skan 447169699Skan /* Add T to the statement-tree. Non-side-effect statements need to be 448169699Skan recorded during statement expressions. */ 449169699Skan append_to_statement_list_force (t, &cur_stmt_list); 450169699Skan 451169699Skan return t; 452169699Skan} 453169699Skan 45490075Sobrien/* States indicating how grokdeclarator() should handle declspecs marked 45590075Sobrien with __attribute__((deprecated)). An object declared as 45690075Sobrien __attribute__((deprecated)) suppresses warnings of uses of other 45790075Sobrien deprecated items. */ 458260918Spfg/* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */ 459260918Spfg/* Also add an __attribute__((unavailable)). An object declared as 460260918Spfg __attribute__((unavailable)) suppresses any reports of being 461260918Spfg declared with unavailable or deprecated items. */ 462260918Spfg/* APPLE LOCAL end "unavailable" attribute (radar 2809697) */ 463132730Skan 46490075Sobrienenum deprecated_states { 46590075Sobrien DEPRECATED_NORMAL, 46690075Sobrien DEPRECATED_SUPPRESS 467260918Spfg /* APPLE LOCAL "unavailable" attribute (radar 2809697) */ 468260918Spfg , DEPRECATED_UNAVAILABLE_SUPPRESS 46990075Sobrien}; 47090075Sobrien 47190075Sobrienstatic enum deprecated_states deprecated_state = DEPRECATED_NORMAL; 47290075Sobrien 47318334Spetervoid 474132730Skanc_print_identifier (FILE *file, tree node, int indent) 47518334Speter{ 476169699Skan print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4); 477169699Skan print_node (file, "tag", I_TAG_DECL (node), indent + 4); 478169699Skan print_node (file, "label", I_LABEL_DECL (node), indent + 4); 47990075Sobrien if (C_IS_RESERVED_WORD (node)) 48090075Sobrien { 48190075Sobrien tree rid = ridpointers[C_RID_CODE (node)]; 48290075Sobrien indent_to (file, indent + 4); 483169699Skan fprintf (file, "rid %p \"%s\"", 484132730Skan (void *) rid, IDENTIFIER_POINTER (rid)); 48590075Sobrien } 48618334Speter} 487169699Skan 488169699Skan/* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL, 489169699Skan which may be any of several kinds of DECL or TYPE or error_mark_node, 490169699Skan in the scope SCOPE. */ 491169699Skanstatic void 492169699Skanbind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested) 493169699Skan{ 494169699Skan struct c_binding *b, **here; 495169699Skan 496169699Skan if (binding_freelist) 497169699Skan { 498169699Skan b = binding_freelist; 499169699Skan binding_freelist = b->prev; 500169699Skan } 501169699Skan else 502169699Skan b = GGC_NEW (struct c_binding); 503169699Skan 504169699Skan b->shadowed = 0; 505169699Skan b->decl = decl; 506169699Skan b->id = name; 507169699Skan b->depth = scope->depth; 508169699Skan b->invisible = invisible; 509169699Skan b->nested = nested; 510169699Skan b->inner_comp = 0; 511169699Skan 512169699Skan b->type = 0; 513169699Skan 514169699Skan b->prev = scope->bindings; 515169699Skan scope->bindings = b; 516169699Skan 517169699Skan if (!name) 518169699Skan return; 519169699Skan 520169699Skan switch (TREE_CODE (decl)) 521169699Skan { 522169699Skan case LABEL_DECL: here = &I_LABEL_BINDING (name); break; 523169699Skan case ENUMERAL_TYPE: 524169699Skan case UNION_TYPE: 525169699Skan case RECORD_TYPE: here = &I_TAG_BINDING (name); break; 526169699Skan case VAR_DECL: 527169699Skan case FUNCTION_DECL: 528169699Skan case TYPE_DECL: 529169699Skan case CONST_DECL: 530169699Skan case PARM_DECL: 531169699Skan case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break; 532169699Skan 533169699Skan default: 534169699Skan gcc_unreachable (); 535169699Skan } 536169699Skan 537169699Skan /* Locate the appropriate place in the chain of shadowed decls 538169699Skan to insert this binding. Normally, scope == current_scope and 539169699Skan this does nothing. */ 540169699Skan while (*here && (*here)->depth > scope->depth) 541169699Skan here = &(*here)->shadowed; 542169699Skan 543169699Skan b->shadowed = *here; 544169699Skan *here = b; 545169699Skan} 546169699Skan 547169699Skan/* Clear the binding structure B, stick it on the binding_freelist, 548169699Skan and return the former value of b->prev. This is used by pop_scope 549169699Skan and get_parm_info to iterate destructively over all the bindings 550169699Skan from a given scope. */ 551169699Skanstatic struct c_binding * 552169699Skanfree_binding_and_advance (struct c_binding *b) 553169699Skan{ 554169699Skan struct c_binding *prev = b->prev; 555169699Skan 556169699Skan memset (b, 0, sizeof (struct c_binding)); 557169699Skan b->prev = binding_freelist; 558169699Skan binding_freelist = b; 559169699Skan 560169699Skan return prev; 561169699Skan} 562169699Skan 56318334Speter 56418334Speter/* Hook called at end of compilation to assume 1 elt 565132730Skan for a file-scope tentative array defn that wasn't complete before. */ 56690075Sobrien 56718334Spetervoid 568132730Skanc_finish_incomplete_decl (tree decl) 56918334Speter{ 57050397Sobrien if (TREE_CODE (decl) == VAR_DECL) 57118334Speter { 57218334Speter tree type = TREE_TYPE (decl); 57350397Sobrien if (type != error_mark_node 57450397Sobrien && TREE_CODE (type) == ARRAY_TYPE 575169699Skan && !DECL_EXTERNAL (decl) 57650397Sobrien && TYPE_DOMAIN (type) == 0) 57718334Speter { 578169699Skan warning (0, "array %q+D assumed to have one element", decl); 57950397Sobrien 580169699Skan complete_array_type (&TREE_TYPE (decl), NULL_TREE, true); 58118334Speter 58218334Speter layout_decl (decl, 0); 58318334Speter } 58418334Speter } 58518334Speter} 58618334Speter 587132730Skan/* The Objective-C front-end often needs to determine the current scope. */ 58818334Speter 589132730Skanvoid * 590169699Skanobjc_get_current_scope (void) 59118334Speter{ 592132730Skan return current_scope; 59318334Speter} 59418334Speter 595132730Skan/* The following function is used only by Objective-C. It needs to live here 596132730Skan because it accesses the innards of c_scope. */ 597132730Skan 59818334Spetervoid 599132730Skanobjc_mark_locals_volatile (void *enclosing_blk) 60018334Speter{ 601132730Skan struct c_scope *scope; 602169699Skan struct c_binding *b; 603132730Skan 604132730Skan for (scope = current_scope; 605132730Skan scope && scope != enclosing_blk; 606132730Skan scope = scope->outer) 607132730Skan { 608169699Skan for (b = scope->bindings; b; b = b->prev) 609169699Skan objc_volatilize_decl (b->decl); 610132730Skan 611132730Skan /* Do not climb up past the current function. */ 612132730Skan if (scope->function_body) 613132730Skan break; 614132730Skan } 61518334Speter} 61618334Speter 617169699Skan/* Nonzero if we are currently in file scope. */ 61818334Speter 61918334Speterint 620132730Skanglobal_bindings_p (void) 62118334Speter{ 622169699Skan return current_scope == file_scope && !c_override_global_bindings_to_false; 62318334Speter} 62418334Speter 625132730Skanvoid 626132730Skankeep_next_level (void) 627132730Skan{ 628132730Skan keep_next_level_flag = true; 629132730Skan} 63018334Speter 631132730Skan/* Identify this scope as currently being filled with parameters. */ 632132730Skan 63318334Spetervoid 634132730Skandeclare_parm_level (void) 63518334Speter{ 636132730Skan current_scope->parm_flag = true; 63718334Speter} 63818334Speter 63918334Spetervoid 640169699Skanpush_scope (void) 64118334Speter{ 642132730Skan if (next_is_function_body) 643132730Skan { 644132730Skan /* This is the transition from the parameters to the top level 645132730Skan of the function body. These are the same scope 646132730Skan (C99 6.2.1p4,6) so we do not push another scope structure. 647132730Skan next_is_function_body is set only by store_parm_decls, which 648132730Skan in turn is called when and only when we are about to 649132730Skan encounter the opening curly brace for the function body. 65018334Speter 651132730Skan The outermost block of a function always gets a BLOCK node, 652132730Skan because the debugging output routines expect that each 653132730Skan function has at least one BLOCK. */ 654132730Skan current_scope->parm_flag = false; 655132730Skan current_scope->function_body = true; 656132730Skan current_scope->keep = true; 657132730Skan current_scope->outer_function = current_function_scope; 658132730Skan current_function_scope = current_scope; 65918334Speter 660132730Skan keep_next_level_flag = false; 661132730Skan next_is_function_body = false; 662132730Skan } 663132730Skan else 66418334Speter { 665169699Skan struct c_scope *scope; 666169699Skan if (scope_freelist) 667169699Skan { 668169699Skan scope = scope_freelist; 669169699Skan scope_freelist = scope->outer; 670169699Skan } 671169699Skan else 672169699Skan scope = GGC_CNEW (struct c_scope); 673132730Skan 674132730Skan scope->keep = keep_next_level_flag; 675132730Skan scope->outer = current_scope; 676169699Skan scope->depth = current_scope ? (current_scope->depth + 1) : 0; 677169699Skan 678169699Skan /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but 679169699Skan possible. */ 680169699Skan if (current_scope && scope->depth == 0) 681169699Skan { 682169699Skan scope->depth--; 683169699Skan sorry ("GCC supports only %u nested scopes", scope->depth); 684169699Skan } 685169699Skan 686132730Skan current_scope = scope; 687132730Skan keep_next_level_flag = false; 68818334Speter } 68918334Speter} 69018334Speter 691169699Skan/* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */ 69250397Sobrien 693169699Skanstatic void 694169699Skanset_type_context (tree type, tree context) 695169699Skan{ 696169699Skan for (type = TYPE_MAIN_VARIANT (type); type; 697169699Skan type = TYPE_NEXT_VARIANT (type)) 698169699Skan TYPE_CONTEXT (type) = context; 699169699Skan} 70050397Sobrien 701169699Skan/* Exit a scope. Restore the state of the identifier-decl mappings 702169699Skan that were in effect when this scope was entered. Return a BLOCK 703169699Skan node containing all the DECLs in this scope that are of interest 704169699Skan to debug info generation. */ 70550397Sobrien 70618334Spetertree 707169699Skanpop_scope (void) 70818334Speter{ 709132730Skan struct c_scope *scope = current_scope; 710169699Skan tree block, context, p; 711169699Skan struct c_binding *b; 71218334Speter 713169699Skan bool functionbody = scope->function_body; 714169699Skan bool keep = functionbody || scope->keep || scope->bindings; 71518334Speter 716169699Skan c_end_vm_scope (scope->depth); 71718334Speter 718132730Skan /* If appropriate, create a BLOCK to record the decls for the life 719132730Skan of this function. */ 72018334Speter block = 0; 721132730Skan if (keep) 72218334Speter { 723132730Skan block = make_node (BLOCK); 724132730Skan BLOCK_SUBBLOCKS (block) = scope->blocks; 725132730Skan TREE_USED (block) = 1; 72618334Speter 727169699Skan /* In each subblock, record that this is its superior. */ 728169699Skan for (p = scope->blocks; p; p = TREE_CHAIN (p)) 729169699Skan BLOCK_SUPERCONTEXT (p) = block; 73018334Speter 731169699Skan BLOCK_VARS (block) = 0; 732169699Skan } 73318334Speter 734169699Skan /* The TYPE_CONTEXTs for all of the tagged types belonging to this 735169699Skan scope must be set so that they point to the appropriate 736169699Skan construct, i.e. either to the current FUNCTION_DECL node, or 737169699Skan else to the BLOCK node we just constructed. 73818334Speter 739169699Skan Note that for tagged types whose scope is just the formal 740169699Skan parameter list for some function type specification, we can't 741169699Skan properly set their TYPE_CONTEXTs here, because we don't have a 742169699Skan pointer to the appropriate FUNCTION_TYPE node readily available 743169699Skan to us. For those cases, the TYPE_CONTEXTs of the relevant tagged 744169699Skan type nodes get set in `grokdeclarator' as soon as we have created 745169699Skan the FUNCTION_TYPE node which will represent the "scope" for these 746169699Skan "parameter list local" tagged types. */ 747169699Skan if (scope->function_body) 748169699Skan context = current_function_decl; 749169699Skan else if (scope == file_scope) 750169699Skan { 751169699Skan tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0); 752169699Skan TREE_CHAIN (file_decl) = all_translation_units; 753169699Skan all_translation_units = file_decl; 754169699Skan context = file_decl; 755169699Skan } 756169699Skan else 757169699Skan context = block; 758132730Skan 759169699Skan /* Clear all bindings in this scope. */ 760169699Skan for (b = scope->bindings; b; b = free_binding_and_advance (b)) 76118334Speter { 762169699Skan p = b->decl; 763132730Skan switch (TREE_CODE (p)) 76418334Speter { 765132730Skan case LABEL_DECL: 766169699Skan /* Warnings for unused labels, errors for undefined labels. */ 767132730Skan if (TREE_USED (p) && !DECL_INITIAL (p)) 76818334Speter { 769169699Skan error ("label %q+D used but not defined", p); 770132730Skan DECL_INITIAL (p) = error_mark_node; 77118334Speter } 772132730Skan else if (!TREE_USED (p) && warn_unused_label) 773132730Skan { 774132730Skan if (DECL_INITIAL (p)) 775169699Skan warning (0, "label %q+D defined but not used", p); 776132730Skan else 777169699Skan warning (0, "label %q+D declared but not defined", p); 778132730Skan } 779169699Skan /* Labels go in BLOCK_VARS. */ 780169699Skan TREE_CHAIN (p) = BLOCK_VARS (block); 781169699Skan BLOCK_VARS (block) = p; 782169699Skan gcc_assert (I_LABEL_BINDING (b->id) == b); 783169699Skan I_LABEL_BINDING (b->id) = b->shadowed; 784169699Skan break; 78518334Speter 786169699Skan case ENUMERAL_TYPE: 787169699Skan case UNION_TYPE: 788169699Skan case RECORD_TYPE: 789169699Skan set_type_context (p, context); 790169699Skan 791169699Skan /* Types may not have tag-names, in which case the type 792169699Skan appears in the bindings list with b->id NULL. */ 793169699Skan if (b->id) 794169699Skan { 795169699Skan gcc_assert (I_TAG_BINDING (b->id) == b); 796169699Skan I_TAG_BINDING (b->id) = b->shadowed; 797169699Skan } 798132730Skan break; 79918334Speter 800132730Skan case FUNCTION_DECL: 801169699Skan /* Propagate TREE_ADDRESSABLE from nested functions to their 802169699Skan containing functions. */ 803169699Skan if (!TREE_ASM_WRITTEN (p) 804132730Skan && DECL_INITIAL (p) != 0 805132730Skan && TREE_ADDRESSABLE (p) 806132730Skan && DECL_ABSTRACT_ORIGIN (p) != 0 807132730Skan && DECL_ABSTRACT_ORIGIN (p) != p) 808132730Skan TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1; 809169699Skan if (!DECL_EXTERNAL (p) 810190404Sdas && DECL_INITIAL (p) == 0 811190404Sdas && scope != file_scope 812190404Sdas && scope != external_scope) 813169699Skan { 814169699Skan error ("nested function %q+D declared but never defined", p); 815169699Skan undef_nested_function = true; 816169699Skan } 817189824Sdas /* C99 6.7.4p6: "a function with external linkage... declared 818189824Sdas with an inline function specifier ... shall also be defined in the 819189824Sdas same translation unit." */ 820189824Sdas else if (DECL_DECLARED_INLINE_P (p) 821189824Sdas && TREE_PUBLIC (p) 822189824Sdas && !DECL_INITIAL (p) 823189824Sdas && !flag_gnu89_inline) 824189824Sdas pedwarn ("inline function %q+D declared but never defined", p); 825189824Sdas 826169699Skan goto common_symbol; 82718334Speter 828132730Skan case VAR_DECL: 829169699Skan /* Warnings for unused variables. */ 830169699Skan if (!TREE_USED (p) 831169699Skan && !TREE_NO_WARNING (p) 832132730Skan && !DECL_IN_SYSTEM_HEADER (p) 833132730Skan && DECL_NAME (p) 834169699Skan && !DECL_ARTIFICIAL (p) 835169699Skan && scope != file_scope 836169699Skan && scope != external_scope) 837169699Skan warning (OPT_Wunused_variable, "unused variable %q+D", p); 83818334Speter 839169699Skan if (b->inner_comp) 840132730Skan { 841169699Skan error ("type of array %q+D completed incompatibly with" 842169699Skan " implicit initialization", p); 843132730Skan } 844169699Skan 845169699Skan /* Fall through. */ 846169699Skan case TYPE_DECL: 847169699Skan case CONST_DECL: 848169699Skan common_symbol: 849169699Skan /* All of these go in BLOCK_VARS, but only if this is the 850169699Skan binding in the home scope. */ 851169699Skan if (!b->nested) 852169699Skan { 853169699Skan TREE_CHAIN (p) = BLOCK_VARS (block); 854169699Skan BLOCK_VARS (block) = p; 855169699Skan } 856169699Skan /* If this is the file scope, and we are processing more 857169699Skan than one translation unit in this compilation, set 858169699Skan DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL. 859169699Skan This makes same_translation_unit_p work, and causes 860169699Skan static declarations to be given disambiguating suffixes. */ 861169699Skan if (scope == file_scope && num_in_fnames > 1) 862169699Skan { 863169699Skan DECL_CONTEXT (p) = context; 864169699Skan if (TREE_CODE (p) == TYPE_DECL) 865169699Skan set_type_context (TREE_TYPE (p), context); 866169699Skan } 867169699Skan 868169699Skan /* Fall through. */ 869169699Skan /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have 870169699Skan already been put there by store_parm_decls. Unused- 871169699Skan parameter warnings are handled by function.c. 872169699Skan error_mark_node obviously does not go in BLOCK_VARS and 873169699Skan does not get unused-variable warnings. */ 874169699Skan case PARM_DECL: 875169699Skan case ERROR_MARK: 876169699Skan /* It is possible for a decl not to have a name. We get 877169699Skan here with b->id NULL in this case. */ 878169699Skan if (b->id) 879169699Skan { 880169699Skan gcc_assert (I_SYMBOL_BINDING (b->id) == b); 881169699Skan I_SYMBOL_BINDING (b->id) = b->shadowed; 882169699Skan if (b->shadowed && b->shadowed->type) 883169699Skan TREE_TYPE (b->shadowed->decl) = b->shadowed->type; 884169699Skan } 885132730Skan break; 886169699Skan 887169699Skan default: 888169699Skan gcc_unreachable (); 889132730Skan } 890132730Skan } 89150397Sobrien 89218334Speter 893169699Skan /* Dispose of the block that we just made inside some higher level. */ 894169699Skan if ((scope->function_body || scope == file_scope) && context) 895132730Skan { 896169699Skan DECL_INITIAL (context) = block; 897169699Skan BLOCK_SUPERCONTEXT (block) = context; 89818334Speter } 899132730Skan else if (scope->outer) 90018334Speter { 901132730Skan if (block) 902132730Skan SCOPE_LIST_APPEND (scope->outer, blocks, block); 903132730Skan /* If we did not make a block for the scope just exited, any 904132730Skan blocks made for inner scopes must be carried forward so they 905132730Skan will later become subblocks of something else. */ 906132730Skan else if (scope->blocks) 907132730Skan SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks); 90818334Speter } 90918334Speter 910132730Skan /* Pop the current scope, and free the structure for reuse. */ 911169699Skan current_scope = scope->outer; 912169699Skan if (scope->function_body) 913169699Skan current_function_scope = scope->outer_function; 91418334Speter 915169699Skan memset (scope, 0, sizeof (struct c_scope)); 916169699Skan scope->outer = scope_freelist; 917169699Skan scope_freelist = scope; 918169699Skan 91918334Speter return block; 92018334Speter} 92118334Speter 922169699Skanvoid 923169699Skanpush_file_scope (void) 924169699Skan{ 925169699Skan tree decl; 926169699Skan 927169699Skan if (file_scope) 928169699Skan return; 929169699Skan 930169699Skan push_scope (); 931169699Skan file_scope = current_scope; 932169699Skan 933169699Skan start_fname_decls (); 934169699Skan 935169699Skan for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl)) 936169699Skan bind (DECL_NAME (decl), decl, file_scope, 937169699Skan /*invisible=*/false, /*nested=*/true); 938169699Skan} 939169699Skan 940169699Skanvoid 941169699Skanpop_file_scope (void) 942169699Skan{ 943169699Skan /* In case there were missing closebraces, get us back to the global 944169699Skan binding level. */ 945169699Skan while (current_scope != file_scope) 946169699Skan pop_scope (); 947169699Skan 948169699Skan /* __FUNCTION__ is defined at file scope (""). This 949169699Skan call may not be necessary as my tests indicate it 950169699Skan still works without it. */ 951169699Skan finish_fname_decls (); 952169699Skan 953169699Skan /* This is the point to write out a PCH if we're doing that. 954169699Skan In that case we do not want to do anything else. */ 955169699Skan if (pch_file) 956169699Skan { 957169699Skan c_common_write_pch (); 958169699Skan return; 959169699Skan } 960169699Skan 961169699Skan /* Pop off the file scope and close this translation unit. */ 962169699Skan pop_scope (); 963169699Skan file_scope = 0; 964169699Skan 965169699Skan maybe_apply_pending_pragma_weaks (); 966169699Skan cgraph_finalize_compilation_unit (); 967169699Skan} 968169699Skan 969132730Skan/* Insert BLOCK at the end of the list of subblocks of the current 970132730Skan scope. This is used when a BIND_EXPR is expanded, to handle the 971132730Skan BLOCK node inside the BIND_EXPR. */ 97218334Speter 97318334Spetervoid 974132730Skaninsert_block (tree block) 97518334Speter{ 97618334Speter TREE_USED (block) = 1; 977132730Skan SCOPE_LIST_APPEND (current_scope, blocks, block); 97818334Speter} 97918334Speter 98018334Speter/* Push a definition or a declaration of struct, union or enum tag "name". 98118334Speter "type" should be the type node. 98218334Speter We assume that the tag "name" is not already defined. 98318334Speter 98418334Speter Note that the definition may really be just a forward reference. 98518334Speter In that case, the TYPE_SIZE will be zero. */ 98618334Speter 987169699Skanstatic void 988132730Skanpushtag (tree name, tree type) 98918334Speter{ 990132730Skan /* Record the identifier as the type's name if it has none. */ 991169699Skan if (name && !TYPE_NAME (type)) 992169699Skan TYPE_NAME (type) = name; 993169699Skan bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false); 994132730Skan 99518334Speter /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the 996132730Skan tagged type we just added to the current scope. This fake 99718334Speter NULL-named TYPE_DECL node helps dwarfout.c to know when it needs 99818334Speter to output a representation of a tagged type, and it also gives 99918334Speter us a convenient place to record the "scope start" address for the 100018334Speter tagged type. */ 100118334Speter 100218334Speter TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type)); 100350397Sobrien 100450397Sobrien /* An approximation for now, so we can tell this is a function-scope tag. 1005169699Skan This will be updated in pop_scope. */ 100650397Sobrien TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type)); 100718334Speter} 100818334Speter 1009132730Skan/* Subroutine of compare_decls. Allow harmless mismatches in return 1010132730Skan and argument types provided that the type modes match. This function 1011132730Skan return a unified type given a suitable match, and 0 otherwise. */ 101218334Speter 1013132730Skanstatic tree 1014132730Skanmatch_builtin_function_types (tree newtype, tree oldtype) 1015132730Skan{ 1016132730Skan tree newrettype, oldrettype; 1017132730Skan tree newargs, oldargs; 1018132730Skan tree trytype, tryargs; 101918334Speter 1020132730Skan /* Accept the return type of the new declaration if same modes. */ 1021132730Skan oldrettype = TREE_TYPE (oldtype); 1022132730Skan newrettype = TREE_TYPE (newtype); 102350397Sobrien 1024132730Skan if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype)) 1025132730Skan return 0; 1026132730Skan 1027132730Skan oldargs = TYPE_ARG_TYPES (oldtype); 1028132730Skan newargs = TYPE_ARG_TYPES (newtype); 1029132730Skan tryargs = newargs; 1030132730Skan 1031132730Skan while (oldargs || newargs) 1032132730Skan { 1033169699Skan if (!oldargs 1034169699Skan || !newargs 1035169699Skan || !TREE_VALUE (oldargs) 1036169699Skan || !TREE_VALUE (newargs) 1037132730Skan || TYPE_MODE (TREE_VALUE (oldargs)) 1038132730Skan != TYPE_MODE (TREE_VALUE (newargs))) 1039132730Skan return 0; 1040132730Skan 1041132730Skan oldargs = TREE_CHAIN (oldargs); 1042132730Skan newargs = TREE_CHAIN (newargs); 1043132730Skan } 1044132730Skan 1045132730Skan trytype = build_function_type (newrettype, tryargs); 1046132730Skan return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype)); 1047132730Skan} 1048132730Skan 1049169699Skan/* Subroutine of diagnose_mismatched_decls. Check for function type 1050132730Skan mismatch involving an empty arglist vs a nonempty one and give clearer 1051169699Skan diagnostics. */ 1052132730Skanstatic void 1053132730Skandiagnose_arglist_conflict (tree newdecl, tree olddecl, 1054132730Skan tree newtype, tree oldtype) 105518334Speter{ 1056132730Skan tree t; 105718334Speter 1058132730Skan if (TREE_CODE (olddecl) != FUNCTION_DECL 1059169699Skan || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype)) 1060132730Skan || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0) 1061132730Skan || 1062132730Skan (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0))) 1063132730Skan return; 1064132730Skan 1065132730Skan t = TYPE_ARG_TYPES (oldtype); 1066132730Skan if (t == 0) 1067132730Skan t = TYPE_ARG_TYPES (newtype); 1068132730Skan for (; t; t = TREE_CHAIN (t)) 106990075Sobrien { 1070132730Skan tree type = TREE_VALUE (t); 1071132730Skan 1072132730Skan if (TREE_CHAIN (t) == 0 1073132730Skan && TYPE_MAIN_VARIANT (type) != void_type_node) 107490075Sobrien { 1075169699Skan inform ("a parameter list with an ellipsis can%'t match " 1076132730Skan "an empty parameter name list declaration"); 1077132730Skan break; 107890075Sobrien } 107918334Speter 1080132730Skan if (c_type_promotes_to (type) != type) 1081132730Skan { 1082169699Skan inform ("an argument type that has a default promotion can%'t match " 1083132730Skan "an empty parameter name list declaration"); 1084132730Skan break; 1085132730Skan } 108690075Sobrien } 1087132730Skan} 108890075Sobrien 1089132730Skan/* Another subroutine of diagnose_mismatched_decls. OLDDECL is an 1090132730Skan old-style function definition, NEWDECL is a prototype declaration. 1091132730Skan Diagnose inconsistencies in the argument list. Returns TRUE if 1092132730Skan the prototype is compatible, FALSE if not. */ 1093132730Skanstatic bool 1094132730Skanvalidate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype) 1095132730Skan{ 1096132730Skan tree newargs, oldargs; 1097132730Skan int i; 109818334Speter 1099169699Skan#define END_OF_ARGLIST(t) ((t) == void_type_node) 1100132730Skan 1101132730Skan oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype); 1102132730Skan newargs = TYPE_ARG_TYPES (newtype); 1103132730Skan i = 1; 1104132730Skan 1105132730Skan for (;;) 1106132730Skan { 1107132730Skan tree oldargtype = TREE_VALUE (oldargs); 1108132730Skan tree newargtype = TREE_VALUE (newargs); 1109132730Skan 1110169699Skan if (oldargtype == error_mark_node || newargtype == error_mark_node) 1111169699Skan return false; 1112169699Skan 1113169699Skan oldargtype = TYPE_MAIN_VARIANT (oldargtype); 1114169699Skan newargtype = TYPE_MAIN_VARIANT (newargtype); 1115169699Skan 1116132730Skan if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype)) 1117132730Skan break; 1118132730Skan 1119132730Skan /* Reaching the end of just one list means the two decls don't 1120132730Skan agree on the number of arguments. */ 1121132730Skan if (END_OF_ARGLIST (oldargtype)) 1122132730Skan { 1123169699Skan error ("prototype for %q+D declares more arguments " 1124169699Skan "than previous old-style definition", newdecl); 1125132730Skan return false; 1126132730Skan } 1127132730Skan else if (END_OF_ARGLIST (newargtype)) 1128132730Skan { 1129169699Skan error ("prototype for %q+D declares fewer arguments " 1130169699Skan "than previous old-style definition", newdecl); 1131132730Skan return false; 1132132730Skan } 1133132730Skan 1134132730Skan /* Type for passing arg must be consistent with that declared 1135132730Skan for the arg. */ 1136169699Skan else if (!comptypes (oldargtype, newargtype)) 1137132730Skan { 1138169699Skan error ("prototype for %q+D declares argument %d" 1139169699Skan " with incompatible type", 1140169699Skan newdecl, i); 1141132730Skan return false; 1142132730Skan } 1143132730Skan 1144132730Skan oldargs = TREE_CHAIN (oldargs); 1145132730Skan newargs = TREE_CHAIN (newargs); 1146132730Skan i++; 1147132730Skan } 1148132730Skan 1149132730Skan /* If we get here, no errors were found, but do issue a warning 1150132730Skan for this poor-style construct. */ 1151169699Skan warning (0, "prototype for %q+D follows non-prototype definition", 1152169699Skan newdecl); 1153132730Skan return true; 1154132730Skan#undef END_OF_ARGLIST 1155132730Skan} 1156132730Skan 1157132730Skan/* Subroutine of diagnose_mismatched_decls. Report the location of DECL, 1158132730Skan first in a pair of mismatched declarations, using the diagnostic 1159132730Skan function DIAG. */ 1160132730Skanstatic void 1161169699Skanlocate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2)) 1162132730Skan{ 1163132730Skan if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)) 1164132730Skan ; 1165132730Skan else if (DECL_INITIAL (decl)) 1166169699Skan diag (G_("previous definition of %q+D was here"), decl); 1167132730Skan else if (C_DECL_IMPLICIT (decl)) 1168169699Skan diag (G_("previous implicit declaration of %q+D was here"), decl); 1169132730Skan else 1170169699Skan diag (G_("previous declaration of %q+D was here"), decl); 1171132730Skan} 1172132730Skan 1173132730Skan/* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL. 1174132730Skan Returns true if the caller should proceed to merge the two, false 1175132730Skan if OLDDECL should simply be discarded. As a side effect, issues 1176132730Skan all necessary diagnostics for invalid or poor-style combinations. 1177132730Skan If it returns true, writes the types of NEWDECL and OLDDECL to 1178132730Skan *NEWTYPEP and *OLDTYPEP - these may have been adjusted from 1179132730Skan TREE_TYPE (NEWDECL, OLDDECL) respectively. */ 1180132730Skan 1181132730Skanstatic bool 1182132730Skandiagnose_mismatched_decls (tree newdecl, tree olddecl, 1183132730Skan tree *newtypep, tree *oldtypep) 1184132730Skan{ 1185132730Skan tree newtype, oldtype; 1186132730Skan bool pedwarned = false; 1187132730Skan bool warned = false; 1188169699Skan bool retval = true; 1189132730Skan 1190169699Skan#define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \ 1191169699Skan && DECL_EXTERNAL (DECL)) 1192169699Skan 1193132730Skan /* If we have error_mark_node for either decl or type, just discard 1194132730Skan the previous decl - we're in an error cascade already. */ 1195132730Skan if (olddecl == error_mark_node || newdecl == error_mark_node) 1196132730Skan return false; 1197132730Skan *oldtypep = oldtype = TREE_TYPE (olddecl); 1198132730Skan *newtypep = newtype = TREE_TYPE (newdecl); 1199132730Skan if (oldtype == error_mark_node || newtype == error_mark_node) 1200132730Skan return false; 1201132730Skan 1202132730Skan /* Two different categories of symbol altogether. This is an error 1203132730Skan unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */ 120418334Speter if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) 120518334Speter { 1206169699Skan if (!(TREE_CODE (olddecl) == FUNCTION_DECL 1207169699Skan && DECL_BUILT_IN (olddecl) 1208169699Skan && !C_DECL_DECLARED_BUILTIN (olddecl))) 1209132730Skan { 1210169699Skan error ("%q+D redeclared as different kind of symbol", newdecl); 1211132730Skan locate_old_decl (olddecl, error); 1212132730Skan } 1213132730Skan else if (TREE_PUBLIC (newdecl)) 1214169699Skan warning (0, "built-in function %q+D declared as non-function", 1215169699Skan newdecl); 1216169699Skan else 1217169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows " 1218169699Skan "a built-in function", newdecl); 1219132730Skan return false; 1220132730Skan } 1221132730Skan 1222161660Skan /* Enumerators have no linkage, so may only be declared once in a 1223161660Skan given scope. */ 1224161660Skan if (TREE_CODE (olddecl) == CONST_DECL) 1225161660Skan { 1226169699Skan error ("redeclaration of enumerator %q+D", newdecl); 1227161660Skan locate_old_decl (olddecl, error); 1228161660Skan return false; 1229161660Skan } 1230161660Skan 1231169699Skan if (!comptypes (oldtype, newtype)) 1232132730Skan { 123318334Speter if (TREE_CODE (olddecl) == FUNCTION_DECL 1234169699Skan && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl)) 123518334Speter { 1236132730Skan /* Accept harmless mismatch in function types. 1237132730Skan This is for the ffs and fprintf builtins. */ 1238132730Skan tree trytype = match_builtin_function_types (newtype, oldtype); 1239132730Skan 1240169699Skan if (trytype && comptypes (newtype, trytype)) 1241132730Skan *oldtypep = oldtype = trytype; 1242132730Skan else 124318334Speter { 1244132730Skan /* If types don't match for a built-in, throw away the 1245132730Skan built-in. No point in calling locate_old_decl here, it 1246169699Skan won't print anything. */ 1247169699Skan warning (0, "conflicting types for built-in function %q+D", 1248169699Skan newdecl); 1249132730Skan return false; 125018334Speter } 125118334Speter } 1252132730Skan else if (TREE_CODE (olddecl) == FUNCTION_DECL 1253169699Skan && DECL_IS_BUILTIN (olddecl)) 1254132730Skan { 1255132730Skan /* A conflicting function declaration for a predeclared 1256132730Skan function that isn't actually built in. Objective C uses 1257132730Skan these. The new declaration silently overrides everything 1258132730Skan but the volatility (i.e. noreturn) indication. See also 1259132730Skan below. FIXME: Make Objective C use normal builtins. */ 1260132730Skan TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1261132730Skan return false; 1262132730Skan } 1263132730Skan /* Permit void foo (...) to match int foo (...) if the latter is 1264132730Skan the definition and implicit int was used. See 1265132730Skan c-torture/compile/920625-2.c. */ 1266132730Skan else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) 1267132730Skan && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node 1268132730Skan && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node 1269169699Skan && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl)) 1270132730Skan { 1271169699Skan pedwarn ("conflicting types for %q+D", newdecl); 1272132730Skan /* Make sure we keep void as the return type. */ 1273132730Skan TREE_TYPE (newdecl) = *newtypep = newtype = oldtype; 1274132730Skan C_FUNCTION_IMPLICIT_INT (newdecl) = 0; 1275132730Skan pedwarned = true; 1276132730Skan } 1277169699Skan /* Permit void foo (...) to match an earlier call to foo (...) with 1278169699Skan no declared type (thus, implicitly int). */ 1279169699Skan else if (TREE_CODE (newdecl) == FUNCTION_DECL 1280169699Skan && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node 1281169699Skan && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node 1282169699Skan && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl)) 1283169699Skan { 1284169699Skan pedwarn ("conflicting types for %q+D", newdecl); 1285169699Skan /* Make sure we keep void as the return type. */ 1286169699Skan TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype; 1287169699Skan pedwarned = true; 1288169699Skan } 128918334Speter else 129018334Speter { 1291169699Skan if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype)) 1292169699Skan error ("conflicting type qualifiers for %q+D", newdecl); 1293169699Skan else 1294169699Skan error ("conflicting types for %q+D", newdecl); 1295132730Skan diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype); 1296132730Skan locate_old_decl (olddecl, error); 1297132730Skan return false; 129818334Speter } 1299132730Skan } 130018334Speter 1301132730Skan /* Redeclaration of a type is a constraint violation (6.7.2.3p1), 1302132730Skan but silently ignore the redeclaration if either is in a system 1303132730Skan header. (Conflicting redeclarations were handled above.) */ 1304132730Skan if (TREE_CODE (newdecl) == TYPE_DECL) 1305132730Skan { 1306132730Skan if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl)) 1307169699Skan return true; /* Allow OLDDECL to continue in use. */ 1308169699Skan 1309169699Skan error ("redefinition of typedef %q+D", newdecl); 1310132730Skan locate_old_decl (olddecl, error); 1311132730Skan return false; 131218334Speter } 131318334Speter 1314132730Skan /* Function declarations can either be 'static' or 'extern' (no 1315132730Skan qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore 1316189824Sdas can never conflict with each other on account of linkage 1317189824Sdas (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but 1318189824Sdas gnu89 mode permits two definitions if one is 'extern inline' and 1319189824Sdas one is not. The non- extern-inline definition supersedes the 1320189824Sdas extern-inline definition. */ 1321169699Skan 1322132730Skan else if (TREE_CODE (newdecl) == FUNCTION_DECL) 132318334Speter { 1324132730Skan /* If you declare a built-in function name as static, or 1325132730Skan define the built-in with an old-style definition (so we 1326132730Skan can't validate the argument list) the built-in definition is 1327132730Skan overridden, but optionally warn this was a bad choice of name. */ 1328132730Skan if (DECL_BUILT_IN (olddecl) 1329169699Skan && !C_DECL_DECLARED_BUILTIN (olddecl) 1330132730Skan && (!TREE_PUBLIC (newdecl) 1331132730Skan || (DECL_INITIAL (newdecl) 1332132730Skan && !TYPE_ARG_TYPES (TREE_TYPE (newdecl))))) 133318334Speter { 1334169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows " 1335169699Skan "a built-in function", newdecl); 133618334Speter /* Discard the old built-in function. */ 1337132730Skan return false; 133818334Speter } 1339169699Skan 1340132730Skan if (DECL_INITIAL (newdecl)) 134118334Speter { 1342169699Skan if (DECL_INITIAL (olddecl)) 134318334Speter { 1344169699Skan /* If both decls are in the same TU and the new declaration 1345169699Skan isn't overriding an extern inline reject the new decl. 1346189824Sdas In c99, no overriding is allowed in the same translation 1347189824Sdas unit. */ 1348189824Sdas if ((!DECL_EXTERN_INLINE (olddecl) 1349189824Sdas || DECL_EXTERN_INLINE (newdecl) 1350189824Sdas || (!flag_gnu89_inline 1351189824Sdas && (!DECL_DECLARED_INLINE_P (olddecl) 1352189824Sdas || !lookup_attribute ("gnu_inline", 1353189824Sdas DECL_ATTRIBUTES (olddecl))) 1354189824Sdas && (!DECL_DECLARED_INLINE_P (newdecl) 1355189824Sdas || !lookup_attribute ("gnu_inline", 1356189824Sdas DECL_ATTRIBUTES (newdecl)))) 1357189824Sdas ) 1358169699Skan && same_translation_unit_p (newdecl, olddecl)) 1359169699Skan { 1360169699Skan error ("redefinition of %q+D", newdecl); 1361169699Skan locate_old_decl (olddecl, error); 1362169699Skan return false; 1363169699Skan } 136418334Speter } 1365132730Skan } 1366132730Skan /* If we have a prototype after an old-style function definition, 1367132730Skan the argument types must be checked specially. */ 1368132730Skan else if (DECL_INITIAL (olddecl) 1369132730Skan && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype) 1370132730Skan && TYPE_ACTUAL_ARG_TYPES (oldtype) 1371132730Skan && !validate_proto_after_old_defn (newdecl, newtype, oldtype)) 1372132730Skan { 1373132730Skan locate_old_decl (olddecl, error); 1374132730Skan return false; 1375132730Skan } 1376169699Skan /* A non-static declaration (even an "extern") followed by a 1377169699Skan static declaration is undefined behavior per C99 6.2.2p3-5,7. 1378169699Skan The same is true for a static forward declaration at block 1379169699Skan scope followed by a non-static declaration/definition at file 1380169699Skan scope. Static followed by non-static at the same scope is 1381169699Skan not undefined behavior, and is the most convenient way to get 1382169699Skan some effects (see e.g. what unwind-dw2-fde-glibc.c does to 1383169699Skan the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but 1384169699Skan we do diagnose it if -Wtraditional. */ 1385132730Skan if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl)) 1386132730Skan { 1387169699Skan /* Two exceptions to the rule. If olddecl is an extern 1388169699Skan inline, or a predeclared function that isn't actually 1389169699Skan built in, newdecl silently overrides olddecl. The latter 1390169699Skan occur only in Objective C; see also above. (FIXME: Make 1391169699Skan Objective C use normal builtins.) */ 1392169699Skan if (!DECL_IS_BUILTIN (olddecl) 1393169699Skan && !DECL_EXTERN_INLINE (olddecl)) 139418334Speter { 1395169699Skan error ("static declaration of %q+D follows " 1396169699Skan "non-static declaration", newdecl); 1397169699Skan locate_old_decl (olddecl, error); 139818334Speter } 1399169699Skan return false; 140018334Speter } 1401169699Skan else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)) 140296263Sobrien { 1403169699Skan if (DECL_CONTEXT (olddecl)) 1404169699Skan { 1405169699Skan error ("non-static declaration of %q+D follows " 1406169699Skan "static declaration", newdecl); 1407169699Skan locate_old_decl (olddecl, error); 1408169699Skan return false; 1409169699Skan } 1410169699Skan else if (warn_traditional) 1411169699Skan { 1412169699Skan warning (OPT_Wtraditional, "non-static declaration of %q+D " 1413169699Skan "follows static declaration", newdecl); 1414169699Skan warned = true; 1415169699Skan } 141696263Sobrien } 1417189824Sdas 1418189824Sdas /* Make sure gnu_inline attribute is either not present, or 1419189824Sdas present on all inline decls. */ 1420189824Sdas if (DECL_DECLARED_INLINE_P (olddecl) 1421189824Sdas && DECL_DECLARED_INLINE_P (newdecl)) 1422189824Sdas { 1423189824Sdas bool newa = lookup_attribute ("gnu_inline", 1424189824Sdas DECL_ATTRIBUTES (newdecl)) != NULL; 1425189824Sdas bool olda = lookup_attribute ("gnu_inline", 1426189824Sdas DECL_ATTRIBUTES (olddecl)) != NULL; 1427189824Sdas if (newa != olda) 1428189824Sdas { 1429189824Sdas error ("%<gnu_inline%> attribute present on %q+D", 1430189824Sdas newa ? newdecl : olddecl); 1431189824Sdas error ("%Jbut not here", newa ? olddecl : newdecl); 1432189824Sdas } 1433189824Sdas } 143418334Speter } 1435132730Skan else if (TREE_CODE (newdecl) == VAR_DECL) 143618334Speter { 1437132730Skan /* Only variables can be thread-local, and all declarations must 1438132730Skan agree on this property. */ 1439169699Skan if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl)) 144018334Speter { 1441169699Skan /* Nothing to check. Since OLDDECL is marked threadprivate 1442169699Skan and NEWDECL does not have a thread-local attribute, we 1443169699Skan will merge the threadprivate attribute into NEWDECL. */ 1444169699Skan ; 1445169699Skan } 1446169699Skan else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)) 1447169699Skan { 1448169699Skan if (DECL_THREAD_LOCAL_P (newdecl)) 1449169699Skan error ("thread-local declaration of %q+D follows " 1450169699Skan "non-thread-local declaration", newdecl); 1451132730Skan else 1452169699Skan error ("non-thread-local declaration of %q+D follows " 1453169699Skan "thread-local declaration", newdecl); 1454132730Skan 1455132730Skan locate_old_decl (olddecl, error); 1456132730Skan return false; 145718334Speter } 1458132730Skan 1459132730Skan /* Multiple initialized definitions are not allowed (6.9p3,5). */ 1460132730Skan if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl)) 146118334Speter { 1462169699Skan error ("redefinition of %q+D", newdecl); 1463132730Skan locate_old_decl (olddecl, error); 1464132730Skan return false; 146518334Speter } 146618334Speter 1467169699Skan /* Objects declared at file scope: if the first declaration had 1468169699Skan external linkage (even if it was an external reference) the 1469169699Skan second must have external linkage as well, or the behavior is 1470169699Skan undefined. If the first declaration had internal linkage, then 1471169699Skan the second must too, or else be an external reference (in which 1472169699Skan case the composite declaration still has internal linkage). 1473169699Skan As for function declarations, we warn about the static-then- 1474169699Skan extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */ 1475169699Skan if (DECL_FILE_SCOPE_P (newdecl) 1476169699Skan && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl)) 147718334Speter { 1478169699Skan if (DECL_EXTERNAL (newdecl)) 147918334Speter { 1480169699Skan if (!DECL_FILE_SCOPE_P (olddecl)) 1481169699Skan { 1482169699Skan error ("extern declaration of %q+D follows " 1483169699Skan "declaration with no linkage", newdecl); 1484169699Skan locate_old_decl (olddecl, error); 1485169699Skan return false; 1486169699Skan } 1487169699Skan else if (warn_traditional) 1488169699Skan { 1489169699Skan warning (OPT_Wtraditional, "non-static declaration of %q+D " 1490169699Skan "follows static declaration", newdecl); 1491169699Skan warned = true; 1492169699Skan } 1493169699Skan } 1494169699Skan else 1495169699Skan { 1496132730Skan if (TREE_PUBLIC (newdecl)) 1497169699Skan error ("non-static declaration of %q+D follows " 1498169699Skan "static declaration", newdecl); 1499132730Skan else 1500169699Skan error ("static declaration of %q+D follows " 1501169699Skan "non-static declaration", newdecl); 150218334Speter 1503132730Skan locate_old_decl (olddecl, error); 1504132730Skan return false; 150518334Speter } 150618334Speter } 1507132730Skan /* Two objects with the same name declared at the same block 1508132730Skan scope must both be external references (6.7p3). */ 1509169699Skan else if (!DECL_FILE_SCOPE_P (newdecl)) 1510132730Skan { 1511132730Skan if (DECL_EXTERNAL (newdecl)) 1512169699Skan { 1513169699Skan /* Extern with initializer at block scope, which will 1514169699Skan already have received an error. */ 1515169699Skan } 1516132730Skan else if (DECL_EXTERNAL (olddecl)) 1517169699Skan { 1518169699Skan error ("declaration of %q+D with no linkage follows " 1519169699Skan "extern declaration", newdecl); 1520169699Skan locate_old_decl (olddecl, error); 1521169699Skan } 1522132730Skan else 1523169699Skan { 1524169699Skan error ("redeclaration of %q+D with no linkage", newdecl); 1525169699Skan locate_old_decl (olddecl, error); 1526169699Skan } 1527117421Skan 1528132730Skan return false; 1529132730Skan } 153018334Speter } 1531132730Skan 1532132730Skan /* warnings */ 1533169699Skan /* All decls must agree on a visibility. */ 1534169699Skan if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS) 1535169699Skan && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl) 1536132730Skan && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) 1537117421Skan { 1538169699Skan warning (0, "redeclaration of %q+D with different visibility " 1539169699Skan "(old visibility preserved)", newdecl); 1540132730Skan warned = true; 1541117421Skan } 1542132730Skan 1543132730Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 1544117421Skan { 1545132730Skan /* Diagnose inline __attribute__ ((noinline)) which is silly. */ 1546132730Skan if (DECL_DECLARED_INLINE_P (newdecl) 1547132730Skan && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 154818334Speter { 1549169699Skan warning (OPT_Wattributes, "inline declaration of %qD follows " 1550169699Skan "declaration with attribute noinline", newdecl); 1551132730Skan warned = true; 155218334Speter } 1553132730Skan else if (DECL_DECLARED_INLINE_P (olddecl) 1554132730Skan && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) 155518334Speter { 1556169699Skan warning (OPT_Wattributes, "declaration of %q+D with attribute " 1557169699Skan "noinline follows inline declaration ", newdecl); 1558132730Skan warned = true; 155918334Speter } 1560132730Skan 1561132730Skan /* Inline declaration after use or definition. 1562132730Skan ??? Should we still warn about this now we have unit-at-a-time 1563169699Skan mode and can get it right? 1564169699Skan Definitely don't complain if the decls are in different translation 1565189824Sdas units. 1566189824Sdas C99 permits this, so don't warn in that case. (The function 1567189824Sdas may not be inlined everywhere in function-at-a-time mode, but 1568189824Sdas we still shouldn't warn.) */ 1569169699Skan if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl) 1570189824Sdas && same_translation_unit_p (olddecl, newdecl) 1571189824Sdas && flag_gnu89_inline) 157218334Speter { 1573132730Skan if (TREE_USED (olddecl)) 157418334Speter { 1575169699Skan warning (0, "%q+D declared inline after being called", olddecl); 1576132730Skan warned = true; 157718334Speter } 1578132730Skan else if (DECL_INITIAL (olddecl)) 1579132730Skan { 1580169699Skan warning (0, "%q+D declared inline after its definition", olddecl); 1581132730Skan warned = true; 1582132730Skan } 158318334Speter } 1584132730Skan } 1585132730Skan else /* PARM_DECL, VAR_DECL */ 1586132730Skan { 1587169699Skan /* Redeclaration of a parameter is a constraint violation (this is 1588169699Skan not explicitly stated, but follows from C99 6.7p3 [no more than 1589169699Skan one declaration of the same identifier with no linkage in the 1590169699Skan same scope, except type tags] and 6.2.2p6 [parameters have no 1591169699Skan linkage]). We must check for a forward parameter declaration, 1592169699Skan indicated by TREE_ASM_WRITTEN on the old declaration - this is 1593169699Skan an extension, the mandatory diagnostic for which is handled by 1594169699Skan mark_forward_parm_decls. */ 1595169699Skan 1596132730Skan if (TREE_CODE (newdecl) == PARM_DECL 1597132730Skan && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl))) 159818334Speter { 1599169699Skan error ("redefinition of parameter %q+D", newdecl); 1600132730Skan locate_old_decl (olddecl, error); 1601132730Skan return false; 1602132730Skan } 160318334Speter } 160418334Speter 1605132730Skan /* Optional warning for completely redundant decls. */ 1606132730Skan if (!warned && !pedwarned 1607132730Skan && warn_redundant_decls 1608132730Skan /* Don't warn about a function declaration followed by a 1609132730Skan definition. */ 1610132730Skan && !(TREE_CODE (newdecl) == FUNCTION_DECL 1611132730Skan && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)) 1612169699Skan /* Don't warn about redundant redeclarations of builtins. */ 1613132730Skan && !(TREE_CODE (newdecl) == FUNCTION_DECL 1614132730Skan && !DECL_BUILT_IN (newdecl) 1615132730Skan && DECL_BUILT_IN (olddecl) 1616169699Skan && !C_DECL_DECLARED_BUILTIN (olddecl)) 1617132730Skan /* Don't warn about an extern followed by a definition. */ 1618132730Skan && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl)) 1619132730Skan /* Don't warn about forward parameter decls. */ 1620132730Skan && !(TREE_CODE (newdecl) == PARM_DECL 1621149846Sobrien && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)) 1622149846Sobrien /* Don't warn about a variable definition following a declaration. */ 1623149846Sobrien && !(TREE_CODE (newdecl) == VAR_DECL 1624149846Sobrien && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))) 162518334Speter { 1626169699Skan warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D", 1627169699Skan newdecl); 1628132730Skan warned = true; 162918334Speter } 163018334Speter 1631132730Skan /* Report location of previous decl/defn in a consistent manner. */ 1632132730Skan if (warned || pedwarned) 1633169699Skan locate_old_decl (olddecl, pedwarned ? pedwarn : warning0); 163418334Speter 1635169699Skan#undef DECL_EXTERN_INLINE 1636169699Skan 1637169699Skan return retval; 1638132730Skan} 163918334Speter 1640132730Skan/* Subroutine of duplicate_decls. NEWDECL has been found to be 1641132730Skan consistent with OLDDECL, but carries new information. Merge the 1642132730Skan new information into OLDDECL. This function issues no 1643132730Skan diagnostics. */ 1644132730Skan 1645132730Skanstatic void 1646132730Skanmerge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype) 1647132730Skan{ 1648189824Sdas bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL 1649189824Sdas && DECL_INITIAL (newdecl) != 0); 1650189824Sdas bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL 1651189824Sdas && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0); 1652189824Sdas bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL 1653189824Sdas && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0); 1654189824Sdas bool extern_changed = false; 1655132730Skan 1656169699Skan /* For real parm decl following a forward decl, rechain the old decl 1657169699Skan in its new location and clear TREE_ASM_WRITTEN (it's not a 1658169699Skan forward decl anymore). */ 1659132730Skan if (TREE_CODE (newdecl) == PARM_DECL 1660169699Skan && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)) 166118334Speter { 1662169699Skan struct c_binding *b, **here; 1663169699Skan 1664169699Skan for (here = ¤t_scope->bindings; *here; here = &(*here)->prev) 1665169699Skan if ((*here)->decl == olddecl) 1666169699Skan goto found; 1667169699Skan gcc_unreachable (); 1668169699Skan 1669169699Skan found: 1670169699Skan b = *here; 1671169699Skan *here = b->prev; 1672169699Skan b->prev = current_scope->bindings; 1673169699Skan current_scope->bindings = b; 1674169699Skan 1675132730Skan TREE_ASM_WRITTEN (olddecl) = 0; 1676132730Skan } 167750397Sobrien 1678132730Skan DECL_ATTRIBUTES (newdecl) 1679169699Skan = targetm.merge_decl_attributes (olddecl, newdecl); 168018334Speter 1681132730Skan /* Merge the data types specified in the two decls. */ 1682132730Skan TREE_TYPE (newdecl) 1683132730Skan = TREE_TYPE (olddecl) 1684169699Skan = composite_type (newtype, oldtype); 168518334Speter 1686132730Skan /* Lay the type out, unless already done. */ 1687169699Skan if (!comptypes (oldtype, TREE_TYPE (newdecl))) 1688132730Skan { 1689132730Skan if (TREE_TYPE (newdecl) != error_mark_node) 1690132730Skan layout_type (TREE_TYPE (newdecl)); 1691132730Skan if (TREE_CODE (newdecl) != FUNCTION_DECL 1692132730Skan && TREE_CODE (newdecl) != TYPE_DECL 1693132730Skan && TREE_CODE (newdecl) != CONST_DECL) 1694132730Skan layout_decl (newdecl, 0); 1695132730Skan } 1696132730Skan else 1697132730Skan { 1698132730Skan /* Since the type is OLDDECL's, make OLDDECL's size go with. */ 1699132730Skan DECL_SIZE (newdecl) = DECL_SIZE (olddecl); 1700132730Skan DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl); 1701132730Skan DECL_MODE (newdecl) = DECL_MODE (olddecl); 1702259694Spfg if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl)) 1703259694Spfg { 1704259694Spfg DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl); 1705259694Spfg DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl); 1706259694Spfg } 1707132730Skan } 170818334Speter 170990075Sobrien 1710132730Skan /* Merge the type qualifiers. */ 1711132730Skan if (TREE_READONLY (newdecl)) 1712132730Skan TREE_READONLY (olddecl) = 1; 171318334Speter 1714132730Skan if (TREE_THIS_VOLATILE (newdecl)) 1715169699Skan TREE_THIS_VOLATILE (olddecl) = 1; 171618334Speter 1717169699Skan /* Merge deprecatedness. */ 1718169699Skan if (TREE_DEPRECATED (newdecl)) 1719169699Skan TREE_DEPRECATED (olddecl) = 1; 1720169699Skan 1721260918Spfg /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */ 1722260918Spfg /* Merge unavailableness. */ 1723260918Spfg if (TREE_UNAVAILABLE (newdecl)) 1724260918Spfg TREE_UNAVAILABLE (olddecl) = 1; 1725260918Spfg /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */ 1726260918Spfg 1727169699Skan /* Keep source location of definition rather than declaration and of 1728169699Skan prototype rather than non-prototype unless that prototype is 1729169699Skan built-in. */ 1730169699Skan if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0) 1731169699Skan || (old_is_prototype && !new_is_prototype 1732169699Skan && !C_DECL_BUILTIN_PROTOTYPE (olddecl))) 1733132730Skan DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); 173418334Speter 1735132730Skan /* Merge the initialization information. */ 1736132730Skan if (DECL_INITIAL (newdecl) == 0) 1737132730Skan DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 173818334Speter 1739169699Skan /* Merge the threadprivate attribute. */ 1740169699Skan if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl)) 1741169699Skan { 1742169699Skan DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl); 1743169699Skan C_DECL_THREADPRIVATE_P (newdecl) = 1; 1744169699Skan } 174590075Sobrien 1746169699Skan if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)) 1747169699Skan { 1748169699Skan /* Merge the unused-warning information. */ 1749169699Skan if (DECL_IN_SYSTEM_HEADER (olddecl)) 1750169699Skan DECL_IN_SYSTEM_HEADER (newdecl) = 1; 1751169699Skan else if (DECL_IN_SYSTEM_HEADER (newdecl)) 1752169699Skan DECL_IN_SYSTEM_HEADER (olddecl) = 1; 1753132730Skan 1754169699Skan /* Merge the section attribute. 1755169699Skan We want to issue an error if the sections conflict but that 1756169699Skan must be done later in decl_attributes since we are called 1757169699Skan before attributes are assigned. */ 1758169699Skan if (DECL_SECTION_NAME (newdecl) == NULL_TREE) 1759169699Skan DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); 1760132730Skan 1761169699Skan /* Copy the assembler name. 1762169699Skan Currently, it can only be defined in the prototype. */ 1763169699Skan COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); 176418334Speter 1765169699Skan /* Use visibility of whichever declaration had it specified */ 1766169699Skan if (DECL_VISIBILITY_SPECIFIED (olddecl)) 1767169699Skan { 1768169699Skan DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); 1769169699Skan DECL_VISIBILITY_SPECIFIED (newdecl) = 1; 1770169699Skan } 177196263Sobrien 1772169699Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 1773169699Skan { 1774169699Skan DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl); 1775169699Skan DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); 1776169699Skan DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); 1777169699Skan DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) 1778169699Skan |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); 1779169699Skan TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1780169699Skan TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); 1781169699Skan DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); 1782169699Skan DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); 1783169699Skan DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl); 1784169699Skan } 1785169699Skan 1786169699Skan /* Merge the storage class information. */ 1787169699Skan merge_weak (newdecl, olddecl); 1788169699Skan 1789169699Skan /* For functions, static overrides non-static. */ 1790169699Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 1791169699Skan { 1792169699Skan TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl); 1793169699Skan /* This is since we don't automatically 1794169699Skan copy the attributes of NEWDECL into OLDDECL. */ 1795169699Skan TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 1796169699Skan /* If this clears `static', clear it in the identifier too. */ 1797169699Skan if (!TREE_PUBLIC (olddecl)) 1798169699Skan TREE_PUBLIC (DECL_NAME (olddecl)) = 0; 1799169699Skan } 180018334Speter } 1801169699Skan 1802189824Sdas /* In c99, 'extern' declaration before (or after) 'inline' means this 1803189824Sdas function is not DECL_EXTERNAL, unless 'gnu_inline' attribute 1804189824Sdas is present. */ 1805189824Sdas if (TREE_CODE (newdecl) == FUNCTION_DECL 1806189824Sdas && !flag_gnu89_inline 1807189824Sdas && (DECL_DECLARED_INLINE_P (newdecl) 1808189824Sdas || DECL_DECLARED_INLINE_P (olddecl)) 1809189824Sdas && (!DECL_DECLARED_INLINE_P (newdecl) 1810189824Sdas || !DECL_DECLARED_INLINE_P (olddecl) 1811189824Sdas || !DECL_EXTERNAL (olddecl)) 1812189824Sdas && DECL_EXTERNAL (newdecl) 1813189824Sdas && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))) 1814189824Sdas DECL_EXTERNAL (newdecl) = 0; 1815189824Sdas 181618334Speter if (DECL_EXTERNAL (newdecl)) 181718334Speter { 1818132730Skan TREE_STATIC (newdecl) = TREE_STATIC (olddecl); 1819132730Skan DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl); 1820132730Skan 182118334Speter /* An extern decl does not override previous storage class. */ 182218334Speter TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); 1823169699Skan if (!DECL_EXTERNAL (newdecl)) 1824132730Skan { 1825132730Skan DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); 1826132730Skan DECL_COMMON (newdecl) = DECL_COMMON (olddecl); 1827132730Skan } 182818334Speter } 182918334Speter else 183018334Speter { 183118334Speter TREE_STATIC (olddecl) = TREE_STATIC (newdecl); 183218334Speter TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 183318334Speter } 183418334Speter 183550397Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL) 183618334Speter { 183790075Sobrien /* If we're redefining a function previously defined as extern 183890075Sobrien inline, make sure we emit debug info for the inline before we 1839169699Skan throw it away, in case it was inlined into a function that 1840169699Skan hasn't been written out yet. */ 1841110623Skan if (new_is_definition && DECL_INITIAL (olddecl)) 184290075Sobrien { 1843132730Skan if (TREE_USED (olddecl) 1844132730Skan /* In unit-at-a-time mode we never inline re-defined extern 1845169699Skan inline functions. */ 1846132730Skan && !flag_unit_at_a_time 1847132730Skan && cgraph_function_possibly_inlined_p (olddecl)) 1848110623Skan (*debug_hooks->outlining_inline_function) (olddecl); 184990075Sobrien 185090075Sobrien /* The new defn must not be inline. */ 185190075Sobrien DECL_INLINE (newdecl) = 0; 185290075Sobrien DECL_UNINLINABLE (newdecl) = 1; 185390075Sobrien } 185490075Sobrien else 185590075Sobrien { 1856169699Skan /* If either decl says `inline', this fn is inline, unless 1857169699Skan its definition was passed already. */ 185890075Sobrien if (DECL_DECLARED_INLINE_P (newdecl) 185990075Sobrien || DECL_DECLARED_INLINE_P (olddecl)) 186090075Sobrien DECL_DECLARED_INLINE_P (newdecl) = 1; 186190075Sobrien 186290075Sobrien DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) 186390075Sobrien = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); 186490075Sobrien } 186590075Sobrien 186618334Speter if (DECL_BUILT_IN (olddecl)) 186718334Speter { 1868169699Skan /* If redeclaring a builtin function, it stays built in. 1869169699Skan But it gets tagged as having been declared. */ 1870132730Skan DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); 1871132730Skan DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); 1872169699Skan C_DECL_DECLARED_BUILTIN (newdecl) = 1; 1873169699Skan if (new_is_prototype) 1874169699Skan C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0; 1875169699Skan else 1876169699Skan C_DECL_BUILTIN_PROTOTYPE (newdecl) 1877169699Skan = C_DECL_BUILTIN_PROTOTYPE (olddecl); 187818334Speter } 187990075Sobrien 188050397Sobrien /* Also preserve various other info from the definition. */ 1881169699Skan if (!new_is_definition) 188250397Sobrien { 188350397Sobrien DECL_RESULT (newdecl) = DECL_RESULT (olddecl); 1884132730Skan DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 1885169699Skan DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl); 188690075Sobrien DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); 188750397Sobrien DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); 188890075Sobrien 188990075Sobrien /* Set DECL_INLINE on the declaration if we've got a body 189090075Sobrien from which to instantiate. */ 1891169699Skan if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl)) 189290075Sobrien { 189390075Sobrien DECL_INLINE (newdecl) = 1; 189490075Sobrien DECL_ABSTRACT_ORIGIN (newdecl) 1895132730Skan = DECL_ABSTRACT_ORIGIN (olddecl); 189690075Sobrien } 189750397Sobrien } 189890075Sobrien else 189990075Sobrien { 190090075Sobrien /* If a previous declaration said inline, mark the 190190075Sobrien definition as inlinable. */ 190290075Sobrien if (DECL_DECLARED_INLINE_P (newdecl) 1903169699Skan && !DECL_UNINLINABLE (newdecl)) 190490075Sobrien DECL_INLINE (newdecl) = 1; 190590075Sobrien } 190650397Sobrien } 190718334Speter 1908189824Sdas extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl); 1909189824Sdas 191018334Speter /* Copy most of the decl-specific fields of NEWDECL into OLDDECL. 1911169699Skan But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */ 191218334Speter { 191390075Sobrien unsigned olddecl_uid = DECL_UID (olddecl); 1914169699Skan tree olddecl_context = DECL_CONTEXT (olddecl); 191518334Speter 191690075Sobrien memcpy ((char *) olddecl + sizeof (struct tree_common), 191790075Sobrien (char *) newdecl + sizeof (struct tree_common), 1918169699Skan sizeof (struct tree_decl_common) - sizeof (struct tree_common)); 1919169699Skan switch (TREE_CODE (olddecl)) 1920169699Skan { 1921169699Skan case FIELD_DECL: 1922169699Skan case VAR_DECL: 1923169699Skan case PARM_DECL: 1924169699Skan case LABEL_DECL: 1925169699Skan case RESULT_DECL: 1926169699Skan case CONST_DECL: 1927169699Skan case TYPE_DECL: 1928169699Skan case FUNCTION_DECL: 1929169699Skan memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 1930169699Skan (char *) newdecl + sizeof (struct tree_decl_common), 1931169699Skan tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common)); 1932169699Skan break; 1933169699Skan 1934169699Skan default: 1935169699Skan 1936169699Skan memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 1937169699Skan (char *) newdecl + sizeof (struct tree_decl_common), 1938169699Skan sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)); 1939169699Skan } 194018334Speter DECL_UID (olddecl) = olddecl_uid; 1941169699Skan DECL_CONTEXT (olddecl) = olddecl_context; 194218334Speter } 194318334Speter 1944132730Skan /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl 1945119268Skan so that encode_section_info has a chance to look at the new decl 1946119268Skan flags and attributes. */ 1947119268Skan if (DECL_RTL_SET_P (olddecl) 1948119268Skan && (TREE_CODE (olddecl) == FUNCTION_DECL 1949119268Skan || (TREE_CODE (olddecl) == VAR_DECL 1950119268Skan && TREE_STATIC (olddecl)))) 1951169699Skan make_decl_rtl (olddecl); 1952189824Sdas 1953189824Sdas /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL, 1954189824Sdas and the definition is coming from the old version, cgraph needs 1955189824Sdas to be called again. */ 1956189824Sdas if (extern_changed && !new_is_definition 1957189824Sdas && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl)) 1958189824Sdas cgraph_finalize_function (olddecl, false); 1959132730Skan} 1960119268Skan 1961132730Skan/* Handle when a new declaration NEWDECL has the same name as an old 1962132730Skan one OLDDECL in the same binding contour. Prints an error message 1963132730Skan if appropriate. 1964132730Skan 1965132730Skan If safely possible, alter OLDDECL to look like NEWDECL, and return 1966132730Skan true. Otherwise, return false. */ 1967132730Skan 1968132730Skanstatic bool 1969132730Skanduplicate_decls (tree newdecl, tree olddecl) 1970132730Skan{ 1971169699Skan tree newtype = NULL, oldtype = NULL; 1972132730Skan 1973132730Skan if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype)) 1974169699Skan { 1975169699Skan /* Avoid `unused variable' and other warnings warnings for OLDDECL. */ 1976169699Skan TREE_NO_WARNING (olddecl) = 1; 1977169699Skan return false; 1978169699Skan } 1979132730Skan 1980132730Skan merge_decls (newdecl, olddecl, newtype, oldtype); 1981132730Skan return true; 198218334Speter} 1983169699Skan 1984132730Skan 1985169699Skan/* Check whether decl-node NEW_DECL shadows an existing declaration. */ 1986132730Skanstatic void 1987169699Skanwarn_if_shadowing (tree new_decl) 1988132730Skan{ 1989169699Skan struct c_binding *b; 1990132730Skan 1991169699Skan /* Shadow warnings wanted? */ 1992169699Skan if (!warn_shadow 1993132730Skan /* No shadow warnings for internally generated vars. */ 1994169699Skan || DECL_IS_BUILTIN (new_decl) 1995132730Skan /* No shadow warnings for vars made for inlining. */ 1996169699Skan || DECL_FROM_INLINE (new_decl)) 199790075Sobrien return; 199890075Sobrien 1999169699Skan /* Is anything being shadowed? Invisible decls do not count. */ 2000169699Skan for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed) 2001169699Skan if (b->decl && b->decl != new_decl && !b->invisible) 2002169699Skan { 2003169699Skan tree old_decl = b->decl; 200490075Sobrien 2005169699Skan if (old_decl == error_mark_node) 2006169699Skan { 2007169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows previous " 2008169699Skan "non-variable", new_decl); 2009169699Skan break; 2010169699Skan } 2011169699Skan else if (TREE_CODE (old_decl) == PARM_DECL) 2012169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows a parameter", 2013169699Skan new_decl); 2014169699Skan else if (DECL_FILE_SCOPE_P (old_decl)) 2015169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows a global " 2016169699Skan "declaration", new_decl); 2017169699Skan else if (TREE_CODE (old_decl) == FUNCTION_DECL 2018169699Skan && DECL_BUILT_IN (old_decl)) 2019169699Skan { 2020169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows " 2021169699Skan "a built-in function", new_decl); 2022169699Skan break; 2023169699Skan } 2024169699Skan else 2025169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows a previous local", 2026169699Skan new_decl); 2027169699Skan 2028169699Skan warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl); 2029169699Skan 2030169699Skan break; 2031169699Skan } 2032132730Skan} 2033132730Skan 2034132730Skan 2035132730Skan/* Subroutine of pushdecl. 2036132730Skan 2037132730Skan X is a TYPE_DECL for a typedef statement. Create a brand new 2038132730Skan ..._TYPE node (which will be just a variant of the existing 2039132730Skan ..._TYPE node with identical properties) and then install X 2040132730Skan as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. 2041132730Skan 2042132730Skan The whole point here is to end up with a situation where each 2043132730Skan and every ..._TYPE node the compiler creates will be uniquely 2044132730Skan associated with AT MOST one node representing a typedef name. 2045132730Skan This way, even though the compiler substitutes corresponding 2046132730Skan ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very 2047132730Skan early on, later parts of the compiler can always do the reverse 2048132730Skan translation and get back the corresponding typedef name. For 2049132730Skan example, given: 2050132730Skan 2051169699Skan typedef struct S MY_TYPE; 2052132730Skan MY_TYPE object; 2053132730Skan 2054132730Skan Later parts of the compiler might only know that `object' was of 2055132730Skan type `struct S' if it were not for code just below. With this 2056132730Skan code however, later parts of the compiler see something like: 2057132730Skan 2058132730Skan struct S' == struct S 2059132730Skan typedef struct S' MY_TYPE; 2060132730Skan struct S' object; 2061132730Skan 2062132730Skan And they can then deduce (from the node for type struct S') that 2063132730Skan the original object declaration was: 2064132730Skan 2065132730Skan MY_TYPE object; 2066132730Skan 2067132730Skan Being able to do this is important for proper support of protoize, 2068132730Skan and also for generating precise symbolic debugging information 2069132730Skan which takes full account of the programmer's (typedef) vocabulary. 2070132730Skan 2071132730Skan Obviously, we don't want to generate a duplicate ..._TYPE node if 2072132730Skan the TYPE_DECL node that we are now processing really represents a 2073132730Skan standard built-in type. 2074132730Skan 2075132730Skan Since all standard types are effectively declared at line zero 2076132730Skan in the source file, we can easily check to see if we are working 2077132730Skan on a standard type by checking the current value of lineno. */ 2078132730Skan 2079132730Skanstatic void 2080132730Skanclone_underlying_type (tree x) 2081132730Skan{ 2082169699Skan if (DECL_IS_BUILTIN (x)) 208390075Sobrien { 2084132730Skan if (TYPE_NAME (TREE_TYPE (x)) == 0) 2085132730Skan TYPE_NAME (TREE_TYPE (x)) = x; 208690075Sobrien } 2087132730Skan else if (TREE_TYPE (x) != error_mark_node 2088132730Skan && DECL_ORIGINAL_TYPE (x) == NULL_TREE) 208990075Sobrien { 2090132730Skan tree tt = TREE_TYPE (x); 2091132730Skan DECL_ORIGINAL_TYPE (x) = tt; 2092169699Skan tt = build_variant_type_copy (tt); 2093132730Skan TYPE_NAME (tt) = x; 2094132730Skan TREE_USED (tt) = TREE_USED (x); 2095132730Skan TREE_TYPE (x) = tt; 209690075Sobrien } 209790075Sobrien} 209890075Sobrien 209918334Speter/* Record a decl-node X as belonging to the current lexical scope. 210018334Speter Check for errors (such as an incompatible declaration for the same 210118334Speter name already seen in the same scope). 210218334Speter 210318334Speter Returns either X or an old decl for the same name. 210418334Speter If an old decl is returned, it may have been smashed 210518334Speter to agree with what X says. */ 210618334Speter 210718334Spetertree 2108132730Skanpushdecl (tree x) 210918334Speter{ 211090075Sobrien tree name = DECL_NAME (x); 2111132730Skan struct c_scope *scope = current_scope; 2112169699Skan struct c_binding *b; 2113169699Skan bool nested = false; 211418334Speter 211590075Sobrien /* Functions need the lang_decl data. */ 2116169699Skan if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x)) 2117169699Skan DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl); 211890075Sobrien 2119169699Skan /* Must set DECL_CONTEXT for everything not at file scope or 2120169699Skan DECL_FILE_SCOPE_P won't work. Local externs don't count 2121169699Skan unless they have initializers (which generate code). */ 2122169699Skan if (current_function_decl 2123169699Skan && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL) 2124169699Skan || DECL_INITIAL (x) || !DECL_EXTERNAL (x))) 2125132730Skan DECL_CONTEXT (x) = current_function_decl; 212618334Speter 2127169699Skan /* If this is of variably modified type, prevent jumping into its 2128169699Skan scope. */ 2129169699Skan if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL) 2130169699Skan && variably_modified_type_p (TREE_TYPE (x), NULL_TREE)) 2131169699Skan c_begin_vm_scope (scope->depth); 2132169699Skan 2133169699Skan /* Anonymous decls are just inserted in the scope. */ 2134169699Skan if (!name) 213518334Speter { 2136169699Skan bind (name, x, scope, /*invisible=*/false, /*nested=*/false); 2137169699Skan return x; 2138169699Skan } 213918334Speter 2140169699Skan /* First, see if there is another declaration with the same name in 2141169699Skan the current scope. If there is, duplicate_decls may do all the 2142169699Skan work for us. If duplicate_decls returns false, that indicates 2143169699Skan two incompatible decls in the same scope; we are to silently 2144169699Skan replace the old one (duplicate_decls has issued all appropriate 2145169699Skan diagnostics). In particular, we should not consider possible 2146169699Skan duplicates in the external scope, or shadowing. */ 2147169699Skan b = I_SYMBOL_BINDING (name); 2148169699Skan if (b && B_IN_SCOPE (b, scope)) 2149169699Skan { 2150169699Skan struct c_binding *b_ext, *b_use; 2151169699Skan tree type = TREE_TYPE (x); 2152169699Skan tree visdecl = b->decl; 2153169699Skan tree vistype = TREE_TYPE (visdecl); 2154169699Skan if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 2155169699Skan && COMPLETE_TYPE_P (TREE_TYPE (x))) 2156169699Skan b->inner_comp = false; 2157169699Skan b_use = b; 2158169699Skan b_ext = b; 2159169699Skan /* If this is an external linkage declaration, we should check 2160169699Skan for compatibility with the type in the external scope before 2161169699Skan setting the type at this scope based on the visible 2162169699Skan information only. */ 2163169699Skan if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl)) 2164169699Skan { 2165169699Skan while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext)) 2166169699Skan b_ext = b_ext->shadowed; 2167169699Skan if (b_ext) 2168169699Skan { 2169169699Skan b_use = b_ext; 2170169699Skan if (b_use->type) 2171169699Skan TREE_TYPE (b_use->decl) = b_use->type; 2172169699Skan } 2173169699Skan } 2174169699Skan if (duplicate_decls (x, b_use->decl)) 2175169699Skan { 2176169699Skan if (b_use != b) 2177169699Skan { 2178169699Skan /* Save the updated type in the external scope and 2179169699Skan restore the proper type for this scope. */ 2180169699Skan tree thistype; 2181169699Skan if (comptypes (vistype, type)) 2182169699Skan thistype = composite_type (vistype, type); 2183169699Skan else 2184169699Skan thistype = TREE_TYPE (b_use->decl); 2185169699Skan b_use->type = TREE_TYPE (b_use->decl); 2186169699Skan if (TREE_CODE (b_use->decl) == FUNCTION_DECL 2187169699Skan && DECL_BUILT_IN (b_use->decl)) 2188169699Skan thistype 2189169699Skan = build_type_attribute_variant (thistype, 2190169699Skan TYPE_ATTRIBUTES 2191169699Skan (b_use->type)); 2192169699Skan TREE_TYPE (b_use->decl) = thistype; 2193169699Skan } 2194169699Skan return b_use->decl; 2195169699Skan } 2196169699Skan else 2197169699Skan goto skip_external_and_shadow_checks; 2198169699Skan } 2199169699Skan 2200169699Skan /* All declarations with external linkage, and all external 2201169699Skan references, go in the external scope, no matter what scope is 2202169699Skan current. However, the binding in that scope is ignored for 2203169699Skan purposes of normal name lookup. A separate binding structure is 2204169699Skan created in the requested scope; this governs the normal 2205169699Skan visibility of the symbol. 2206169699Skan 2207169699Skan The binding in the externals scope is used exclusively for 2208169699Skan detecting duplicate declarations of the same object, no matter 2209169699Skan what scope they are in; this is what we do here. (C99 6.2.7p2: 2210169699Skan All declarations that refer to the same object or function shall 2211169699Skan have compatible type; otherwise, the behavior is undefined.) */ 2212169699Skan if (DECL_EXTERNAL (x) || scope == file_scope) 2213169699Skan { 2214169699Skan tree type = TREE_TYPE (x); 2215169699Skan tree vistype = 0; 2216169699Skan tree visdecl = 0; 2217169699Skan bool type_saved = false; 2218169699Skan if (b && !B_IN_EXTERNAL_SCOPE (b) 2219169699Skan && (TREE_CODE (b->decl) == FUNCTION_DECL 2220169699Skan || TREE_CODE (b->decl) == VAR_DECL) 2221169699Skan && DECL_FILE_SCOPE_P (b->decl)) 2222169699Skan { 2223169699Skan visdecl = b->decl; 2224169699Skan vistype = TREE_TYPE (visdecl); 2225169699Skan } 2226169699Skan if (scope != file_scope 222790075Sobrien && !DECL_IN_SYSTEM_HEADER (x)) 2228169699Skan warning (OPT_Wnested_externs, "nested extern declaration of %qD", x); 222990075Sobrien 2230169699Skan while (b && !B_IN_EXTERNAL_SCOPE (b)) 223150397Sobrien { 2232169699Skan /* If this decl might be modified, save its type. This is 2233169699Skan done here rather than when the decl is first bound 2234169699Skan because the type may change after first binding, through 2235169699Skan being completed or through attributes being added. If we 2236169699Skan encounter multiple such decls, only the first should have 2237169699Skan its type saved; the others will already have had their 2238169699Skan proper types saved and the types will not have changed as 2239169699Skan their scopes will not have been re-entered. */ 2240169699Skan if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved) 224118334Speter { 2242169699Skan b->type = TREE_TYPE (b->decl); 2243169699Skan type_saved = true; 224418334Speter } 2245169699Skan if (B_IN_FILE_SCOPE (b) 2246169699Skan && TREE_CODE (b->decl) == VAR_DECL 2247169699Skan && TREE_STATIC (b->decl) 2248169699Skan && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE 2249169699Skan && !TYPE_DOMAIN (TREE_TYPE (b->decl)) 2250169699Skan && TREE_CODE (type) == ARRAY_TYPE 2251169699Skan && TYPE_DOMAIN (type) 2252169699Skan && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) 2253169699Skan && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) 2254169699Skan { 2255169699Skan /* Array type completed in inner scope, which should be 2256169699Skan diagnosed if the completion does not have size 1 and 2257169699Skan it does not get completed in the file scope. */ 2258169699Skan b->inner_comp = true; 2259169699Skan } 2260169699Skan b = b->shadowed; 226118334Speter } 2262169699Skan 2263169699Skan /* If a matching external declaration has been found, set its 2264169699Skan type to the composite of all the types of that declaration. 2265169699Skan After the consistency checks, it will be reset to the 2266169699Skan composite of the visible types only. */ 2267169699Skan if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl)) 2268169699Skan && b->type) 2269169699Skan TREE_TYPE (b->decl) = b->type; 2270169699Skan 2271169699Skan /* The point of the same_translation_unit_p check here is, 2272169699Skan we want to detect a duplicate decl for a construct like 2273169699Skan foo() { extern bar(); } ... static bar(); but not if 2274169699Skan they are in different translation units. In any case, 2275169699Skan the static does not go in the externals scope. */ 2276169699Skan if (b 2277169699Skan && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl)) 2278169699Skan && duplicate_decls (x, b->decl)) 227990075Sobrien { 2280169699Skan tree thistype; 2281169699Skan if (vistype) 228290075Sobrien { 2283169699Skan if (comptypes (vistype, type)) 2284169699Skan thistype = composite_type (vistype, type); 2285169699Skan else 2286169699Skan thistype = TREE_TYPE (b->decl); 228790075Sobrien } 228818334Speter else 2289169699Skan thistype = type; 2290169699Skan b->type = TREE_TYPE (b->decl); 2291169699Skan if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl)) 2292169699Skan thistype 2293169699Skan = build_type_attribute_variant (thistype, 2294169699Skan TYPE_ATTRIBUTES (b->type)); 2295169699Skan TREE_TYPE (b->decl) = thistype; 2296169699Skan bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true); 2297169699Skan return b->decl; 229818334Speter } 2299169699Skan else if (TREE_PUBLIC (x)) 230018334Speter { 2301169699Skan if (visdecl && !b && duplicate_decls (x, visdecl)) 230218334Speter { 2303169699Skan /* An external declaration at block scope referring to a 2304169699Skan visible entity with internal linkage. The composite 2305169699Skan type will already be correct for this scope, so we 2306169699Skan just need to fall through to make the declaration in 2307169699Skan this scope. */ 2308169699Skan nested = true; 2309169699Skan x = visdecl; 231018334Speter } 2311169699Skan else 2312169699Skan { 2313169699Skan bind (name, x, external_scope, /*invisible=*/true, 2314169699Skan /*nested=*/false); 2315169699Skan nested = true; 2316169699Skan } 231718334Speter } 2318169699Skan } 231990075Sobrien 2320169699Skan if (TREE_CODE (x) != PARM_DECL) 2321169699Skan warn_if_shadowing (x); 232218334Speter 2323169699Skan skip_external_and_shadow_checks: 2324169699Skan if (TREE_CODE (x) == TYPE_DECL) 2325169699Skan clone_underlying_type (x); 232618334Speter 2327169699Skan bind (name, x, scope, /*invisible=*/false, nested); 232890075Sobrien 2329169699Skan /* If x's type is incomplete because it's based on a 2330169699Skan structure or union which has not yet been fully declared, 2331169699Skan attach it to that structure or union type, so we can go 2332169699Skan back and complete the variable declaration later, if the 2333169699Skan structure or union gets fully declared. 2334132730Skan 2335169699Skan If the input is erroneous, we can have error_mark in the type 2336169699Skan slot (e.g. "f(void a, ...)") - that doesn't count as an 2337169699Skan incomplete type. */ 2338169699Skan if (TREE_TYPE (x) != error_mark_node 2339169699Skan && !COMPLETE_TYPE_P (TREE_TYPE (x))) 2340169699Skan { 2341169699Skan tree element = TREE_TYPE (x); 234218334Speter 2343169699Skan while (TREE_CODE (element) == ARRAY_TYPE) 2344169699Skan element = TREE_TYPE (element); 2345169699Skan element = TYPE_MAIN_VARIANT (element); 234618334Speter 2347169699Skan if ((TREE_CODE (element) == RECORD_TYPE 2348169699Skan || TREE_CODE (element) == UNION_TYPE) 2349169699Skan && (TREE_CODE (x) != TYPE_DECL 2350169699Skan || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE) 2351169699Skan && !COMPLETE_TYPE_P (element)) 2352169699Skan C_TYPE_INCOMPLETE_VARS (element) 2353169699Skan = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element)); 2354169699Skan } 235518334Speter return x; 235618334Speter} 235718334Speter 2358169699Skan/* Record X as belonging to file scope. 2359132730Skan This is used only internally by the Objective-C front end, 2360132730Skan and is limited to its needs. duplicate_decls is not called; 2361132730Skan if there is any preexisting decl for this identifier, it is an ICE. */ 236218334Speter 236318334Spetertree 2364132730Skanpushdecl_top_level (tree x) 236518334Speter{ 2366132730Skan tree name; 2367169699Skan bool nested = false; 2368169699Skan gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL); 236918334Speter 2370132730Skan name = DECL_NAME (x); 2371132730Skan 2372169699Skan gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name)); 2373132730Skan 2374169699Skan if (TREE_PUBLIC (x)) 2375169699Skan { 2376169699Skan bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false); 2377169699Skan nested = true; 2378169699Skan } 2379169699Skan if (file_scope) 2380169699Skan bind (name, x, file_scope, /*invisible=*/false, nested); 2381132730Skan 2382132730Skan return x; 238318334Speter} 238418334Speter 2385169699Skanstatic void 2386169699Skanimplicit_decl_warning (tree id, tree olddecl) 2387169699Skan{ 2388169699Skan void (*diag) (const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2); 2389169699Skan switch (mesg_implicit_function_declaration) 2390169699Skan { 2391169699Skan case 0: return; 2392169699Skan case 1: diag = warning0; break; 2393169699Skan case 2: diag = error; break; 2394169699Skan default: gcc_unreachable (); 2395169699Skan } 2396169699Skan 2397169699Skan diag (G_("implicit declaration of function %qE"), id); 2398169699Skan if (olddecl) 2399169699Skan locate_old_decl (olddecl, diag); 2400169699Skan} 2401169699Skan 2402132730Skan/* Generate an implicit declaration for identifier FUNCTIONID as a 2403132730Skan function of type int (). */ 240418334Speter 240518334Spetertree 2406132730Skanimplicitly_declare (tree functionid) 240718334Speter{ 2408169699Skan struct c_binding *b; 2409169699Skan tree decl = 0; 2410169699Skan tree asmspec_tree; 241118334Speter 2412169699Skan for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed) 2413169699Skan { 2414169699Skan if (B_IN_SCOPE (b, external_scope)) 2415169699Skan { 2416169699Skan decl = b->decl; 2417169699Skan break; 2418169699Skan } 2419169699Skan } 2420169699Skan 2421132730Skan if (decl) 2422132730Skan { 2423169699Skan if (decl == error_mark_node) 2424169699Skan return decl; 2425169699Skan 2426169699Skan /* FIXME: Objective-C has weird not-really-builtin functions 2427169699Skan which are supposed to be visible automatically. They wind up 2428169699Skan in the external scope because they're pushed before the file 2429169699Skan scope gets created. Catch this here and rebind them into the 2430169699Skan file scope. */ 2431169699Skan if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl)) 2432132730Skan { 2433169699Skan bind (functionid, decl, file_scope, 2434169699Skan /*invisible=*/false, /*nested=*/true); 2435169699Skan return decl; 2436132730Skan } 2437169699Skan else 2438169699Skan { 2439169699Skan tree newtype = default_function_type; 2440169699Skan if (b->type) 2441169699Skan TREE_TYPE (decl) = b->type; 2442169699Skan /* Implicit declaration of a function already declared 2443169699Skan (somehow) in a different scope, or as a built-in. 2444169699Skan If this is the first time this has happened, warn; 2445169699Skan then recycle the old declaration but with the new type. */ 2446169699Skan if (!C_DECL_IMPLICIT (decl)) 2447169699Skan { 2448169699Skan implicit_decl_warning (functionid, decl); 2449169699Skan C_DECL_IMPLICIT (decl) = 1; 2450169699Skan } 2451169699Skan if (DECL_BUILT_IN (decl)) 2452169699Skan { 2453169699Skan newtype = build_type_attribute_variant (newtype, 2454169699Skan TYPE_ATTRIBUTES 2455169699Skan (TREE_TYPE (decl))); 2456169699Skan if (!comptypes (newtype, TREE_TYPE (decl))) 2457169699Skan { 2458169699Skan warning (0, "incompatible implicit declaration of built-in" 2459169699Skan " function %qD", decl); 2460169699Skan newtype = TREE_TYPE (decl); 2461169699Skan } 2462169699Skan } 2463169699Skan else 2464169699Skan { 2465169699Skan if (!comptypes (newtype, TREE_TYPE (decl))) 2466169699Skan { 2467169699Skan error ("incompatible implicit declaration of function %qD", 2468169699Skan decl); 2469169699Skan locate_old_decl (decl, error); 2470169699Skan } 2471169699Skan } 2472169699Skan b->type = TREE_TYPE (decl); 2473169699Skan TREE_TYPE (decl) = newtype; 2474169699Skan bind (functionid, decl, current_scope, 2475169699Skan /*invisible=*/false, /*nested=*/true); 2476169699Skan return decl; 2477169699Skan } 2478132730Skan } 247918334Speter 2480132730Skan /* Not seen before. */ 2481132730Skan decl = build_decl (FUNCTION_DECL, functionid, default_function_type); 248218334Speter DECL_EXTERNAL (decl) = 1; 248318334Speter TREE_PUBLIC (decl) = 1; 2484132730Skan C_DECL_IMPLICIT (decl) = 1; 2485169699Skan implicit_decl_warning (functionid, 0); 2486169699Skan asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL); 2487169699Skan if (asmspec_tree) 2488169699Skan set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree)); 248918334Speter 2490132730Skan /* C89 says implicit declarations are in the innermost block. 2491117421Skan So we record the decl in the standard fashion. */ 2492132730Skan decl = pushdecl (decl); 249318334Speter 2494132730Skan /* No need to call objc_check_decl here - it's a function type. */ 2495169699Skan rest_of_decl_compilation (decl, 0, 0); 249618334Speter 2497132730Skan /* Write a record describing this implicit function declaration 2498132730Skan to the prototypes file (if requested). */ 249918334Speter gen_aux_info_record (decl, 0, 1, 0); 250018334Speter 250190075Sobrien /* Possibly apply some default attributes to this implicit declaration. */ 250290075Sobrien decl_attributes (&decl, NULL_TREE, 0); 250318334Speter 250418334Speter return decl; 250518334Speter} 250618334Speter 2507132730Skan/* Issue an error message for a reference to an undeclared variable 2508132730Skan ID, including a reference to a builtin outside of function-call 2509132730Skan context. Establish a binding of the identifier to error_mark_node 2510132730Skan in an appropriate scope, which will suppress further errors for the 2511169699Skan same identifier. The error message should be given location LOC. */ 2512132730Skanvoid 2513169699Skanundeclared_variable (tree id, location_t loc) 2514132730Skan{ 2515132730Skan static bool already = false; 2516132730Skan struct c_scope *scope; 251718334Speter 2518132730Skan if (current_function_decl == 0) 251918334Speter { 2520169699Skan error ("%H%qE undeclared here (not in a function)", &loc, id); 2521132730Skan scope = current_scope; 252218334Speter } 252318334Speter else 252418334Speter { 2525169699Skan error ("%H%qE undeclared (first use in this function)", &loc, id); 2526132730Skan 2527169699Skan if (!already) 2528132730Skan { 2529169699Skan error ("%H(Each undeclared identifier is reported only once", &loc); 2530169699Skan error ("%Hfor each function it appears in.)", &loc); 2531132730Skan already = true; 2532132730Skan } 2533132730Skan 2534169699Skan /* If we are parsing old-style parameter decls, current_function_decl 2535169699Skan will be nonnull but current_function_scope will be null. */ 2536169699Skan scope = current_function_scope ? current_function_scope : current_scope; 253718334Speter } 2538169699Skan bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false); 253918334Speter} 254018334Speter 2541132730Skan/* Subroutine of lookup_label, declare_label, define_label: construct a 2542132730Skan LABEL_DECL with all the proper frills. */ 2543132730Skan 2544132730Skanstatic tree 2545132730Skanmake_label (tree name, location_t location) 2546132730Skan{ 2547132730Skan tree label = build_decl (LABEL_DECL, name, void_type_node); 2548132730Skan 2549132730Skan DECL_CONTEXT (label) = current_function_decl; 2550132730Skan DECL_MODE (label) = VOIDmode; 2551132730Skan DECL_SOURCE_LOCATION (label) = location; 2552132730Skan 2553132730Skan return label; 2554132730Skan} 2555132730Skan 2556132730Skan/* Get the LABEL_DECL corresponding to identifier NAME as a label. 255718334Speter Create one if none exists so far for the current function. 2558132730Skan This is called when a label is used in a goto expression or 2559132730Skan has its address taken. */ 256018334Speter 256118334Spetertree 2562132730Skanlookup_label (tree name) 256318334Speter{ 2564132730Skan tree label; 256518334Speter 256618334Speter if (current_function_decl == 0) 256718334Speter { 2568169699Skan error ("label %qE referenced outside of any function", name); 256918334Speter return 0; 257018334Speter } 257118334Speter 2572132730Skan /* Use a label already defined or ref'd with this name, but not if 2573132730Skan it is inherited from a containing function and wasn't declared 2574132730Skan using __label__. */ 2575169699Skan label = I_LABEL_DECL (name); 2576132730Skan if (label && (DECL_CONTEXT (label) == current_function_decl 2577132730Skan || C_DECLARED_LABEL_FLAG (label))) 257818334Speter { 2579132730Skan /* If the label has only been declared, update its apparent 2580132730Skan location to point here, for better diagnostics if it 2581132730Skan turns out not to have been defined. */ 2582132730Skan if (!TREE_USED (label)) 2583132730Skan DECL_SOURCE_LOCATION (label) = input_location; 2584132730Skan return label; 258518334Speter } 258618334Speter 2587132730Skan /* No label binding for that identifier; make one. */ 2588132730Skan label = make_label (name, input_location); 258918334Speter 2590132730Skan /* Ordinary labels go in the current function scope. */ 2591169699Skan bind (name, label, current_function_scope, 2592169699Skan /*invisible=*/false, /*nested=*/false); 2593132730Skan return label; 259418334Speter} 259518334Speter 2596132730Skan/* Make a label named NAME in the current function, shadowing silently 2597132730Skan any that may be inherited from containing functions or containing 2598132730Skan scopes. This is called for __label__ declarations. */ 259918334Speter 260018334Spetertree 2601132730Skandeclare_label (tree name) 260218334Speter{ 2603169699Skan struct c_binding *b = I_LABEL_BINDING (name); 2604169699Skan tree label; 260518334Speter 2606132730Skan /* Check to make sure that the label hasn't already been declared 2607132730Skan at this scope */ 2608169699Skan if (b && B_IN_CURRENT_SCOPE (b)) 2609169699Skan { 2610169699Skan error ("duplicate label declaration %qE", name); 2611169699Skan locate_old_decl (b->decl, error); 261218334Speter 2613169699Skan /* Just use the previous declaration. */ 2614169699Skan return b->decl; 2615169699Skan } 261618334Speter 2617132730Skan label = make_label (name, input_location); 2618132730Skan C_DECLARED_LABEL_FLAG (label) = 1; 261918334Speter 2620132730Skan /* Declared labels go in the current scope. */ 2621169699Skan bind (name, label, current_scope, 2622169699Skan /*invisible=*/false, /*nested=*/false); 2623132730Skan return label; 262418334Speter} 262518334Speter 262618334Speter/* Define a label, specifying the location in the source file. 262718334Speter Return the LABEL_DECL node for the label, if the definition is valid. 262818334Speter Otherwise return 0. */ 262918334Speter 263018334Spetertree 2631132730Skandefine_label (location_t location, tree name) 263218334Speter{ 2633132730Skan /* Find any preexisting label with this name. It is an error 2634132730Skan if that label has already been defined in this function, or 2635132730Skan if there is a containing function with a declared label with 2636132730Skan the same name. */ 2637169699Skan tree label = I_LABEL_DECL (name); 2638169699Skan struct c_label_list *nlist_se, *nlist_vm; 263918334Speter 2640132730Skan if (label 2641132730Skan && ((DECL_CONTEXT (label) == current_function_decl 2642132730Skan && DECL_INITIAL (label) != 0) 2643132730Skan || (DECL_CONTEXT (label) != current_function_decl 2644132730Skan && C_DECLARED_LABEL_FLAG (label)))) 264518334Speter { 2646169699Skan error ("%Hduplicate label %qD", &location, label); 2647169699Skan locate_old_decl (label, error); 264818334Speter return 0; 264918334Speter } 2650132730Skan else if (label && DECL_CONTEXT (label) == current_function_decl) 2651132730Skan { 2652132730Skan /* The label has been used or declared already in this function, 2653132730Skan but not defined. Update its location to point to this 2654132730Skan definition. */ 2655169699Skan if (C_DECL_UNDEFINABLE_STMT_EXPR (label)) 2656169699Skan error ("%Jjump into statement expression", label); 2657169699Skan if (C_DECL_UNDEFINABLE_VM (label)) 2658169699Skan error ("%Jjump into scope of identifier with variably modified type", 2659169699Skan label); 2660132730Skan DECL_SOURCE_LOCATION (label) = location; 2661132730Skan } 266218334Speter else 266318334Speter { 2664132730Skan /* No label binding for that identifier; make one. */ 2665132730Skan label = make_label (name, location); 2666132730Skan 2667132730Skan /* Ordinary labels go in the current function scope. */ 2668169699Skan bind (name, label, current_function_scope, 2669169699Skan /*invisible=*/false, /*nested=*/false); 267018334Speter } 2671132730Skan 2672169699Skan if (!in_system_header && lookup_name (name)) 2673169699Skan warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace " 2674169699Skan "for labels, identifier %qE conflicts", &location, name); 2675132730Skan 2676169699Skan nlist_se = XOBNEW (&parser_obstack, struct c_label_list); 2677169699Skan nlist_se->next = label_context_stack_se->labels_def; 2678169699Skan nlist_se->label = label; 2679169699Skan label_context_stack_se->labels_def = nlist_se; 2680169699Skan 2681169699Skan nlist_vm = XOBNEW (&parser_obstack, struct c_label_list); 2682169699Skan nlist_vm->next = label_context_stack_vm->labels_def; 2683169699Skan nlist_vm->label = label; 2684169699Skan label_context_stack_vm->labels_def = nlist_vm; 2685169699Skan 2686132730Skan /* Mark label as having been defined. */ 2687132730Skan DECL_INITIAL (label) = error_mark_node; 2688132730Skan return label; 268918334Speter} 269018334Speter 269118334Speter/* Given NAME, an IDENTIFIER_NODE, 269218334Speter return the structure (or union or enum) definition for that name. 2693132730Skan If THISLEVEL_ONLY is nonzero, searches only the current_scope. 269418334Speter CODE says which kind of type the caller wants; 269518334Speter it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. 269618334Speter If the wrong kind of type is found, an error is reported. */ 269718334Speter 269818334Speterstatic tree 2699132730Skanlookup_tag (enum tree_code code, tree name, int thislevel_only) 270018334Speter{ 2701169699Skan struct c_binding *b = I_TAG_BINDING (name); 2702132730Skan int thislevel = 0; 270318334Speter 2704169699Skan if (!b || !b->decl) 2705132730Skan return 0; 2706132730Skan 2707132730Skan /* We only care about whether it's in this level if 2708132730Skan thislevel_only was set or it might be a type clash. */ 2709169699Skan if (thislevel_only || TREE_CODE (b->decl) != code) 271018334Speter { 2711169699Skan /* For our purposes, a tag in the external scope is the same as 2712169699Skan a tag in the file scope. (Primarily relevant to Objective-C 2713169699Skan and its builtin structure tags, which get pushed before the 2714169699Skan file scope is created.) */ 2715169699Skan if (B_IN_CURRENT_SCOPE (b) 2716169699Skan || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b))) 2717132730Skan thislevel = 1; 271818334Speter } 2719132730Skan 2720132730Skan if (thislevel_only && !thislevel) 2721132730Skan return 0; 2722132730Skan 2723169699Skan if (TREE_CODE (b->decl) != code) 2724132730Skan { 2725132730Skan /* Definition isn't the kind we were looking for. */ 2726132730Skan pending_invalid_xref = name; 2727132730Skan pending_invalid_xref_location = input_location; 2728132730Skan 2729132730Skan /* If in the same binding level as a declaration as a tag 2730132730Skan of a different type, this must not be allowed to 2731132730Skan shadow that tag, so give the error immediately. 2732132730Skan (For example, "struct foo; union foo;" is invalid.) */ 2733132730Skan if (thislevel) 2734132730Skan pending_xref_error (); 2735132730Skan } 2736169699Skan return b->decl; 273718334Speter} 273818334Speter 273918334Speter/* Print an error message now 274018334Speter for a recent invalid struct, union or enum cross reference. 274118334Speter We don't print them immediately because they are not invalid 274218334Speter when used in the `struct foo;' construct for shadowing. */ 274318334Speter 274418334Spetervoid 2745132730Skanpending_xref_error (void) 274618334Speter{ 274718334Speter if (pending_invalid_xref != 0) 2748169699Skan error ("%H%qE defined as wrong kind of tag", 2749169699Skan &pending_invalid_xref_location, pending_invalid_xref); 275018334Speter pending_invalid_xref = 0; 275118334Speter} 275218334Speter 275318334Speter 2754132730Skan/* Look up NAME in the current scope and its superiors 275518334Speter in the namespace of variables, functions and typedefs. 275618334Speter Return a ..._DECL node of some kind representing its definition, 275718334Speter or return 0 if it is undefined. */ 275818334Speter 275918334Spetertree 2760132730Skanlookup_name (tree name) 276118334Speter{ 2762169699Skan struct c_binding *b = I_SYMBOL_BINDING (name); 2763169699Skan if (b && !b->invisible) 2764169699Skan return b->decl; 2765169699Skan return 0; 276618334Speter} 276718334Speter 2768169699Skan/* Similar to `lookup_name' but look only at the indicated scope. */ 276918334Speter 2770132730Skanstatic tree 2771169699Skanlookup_name_in_scope (tree name, struct c_scope *scope) 277218334Speter{ 2773169699Skan struct c_binding *b; 277418334Speter 2775169699Skan for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed) 2776169699Skan if (B_IN_SCOPE (b, scope)) 2777169699Skan return b->decl; 2778132730Skan return 0; 277918334Speter} 278018334Speter 278118334Speter/* Create the predefined scalar types of C, 278250397Sobrien and some nodes representing standard constants (0, 1, (void *) 0). 2783132730Skan Initialize the global scope. 278418334Speter Make definitions for built-in primitive functions. */ 278518334Speter 278618334Spetervoid 2787132730Skanc_init_decl_processing (void) 278818334Speter{ 2789132730Skan location_t save_loc = input_location; 279018334Speter 2791169699Skan /* Initialize reserved words for parser. */ 279290075Sobrien c_parse_init (); 279390075Sobrien 2794132730Skan current_function_decl = 0; 279590075Sobrien 2796169699Skan gcc_obstack_init (&parser_obstack); 279718334Speter 2798169699Skan /* Make the externals scope. */ 2799169699Skan push_scope (); 2800169699Skan external_scope = current_scope; 2801169699Skan 2802132730Skan /* Declarations from c_common_nodes_and_builtins must not be associated 2803132730Skan with this input file, lest we get differences between using and not 2804132730Skan using preprocessed headers. */ 2805169699Skan#ifdef USE_MAPPED_LOCATION 2806169699Skan input_location = BUILTINS_LOCATION; 2807169699Skan#else 2808169699Skan input_location.file = "<built-in>"; 2809132730Skan input_location.line = 0; 2810169699Skan#endif 2811132730Skan 2812169699Skan build_common_tree_nodes (flag_signed_char, false); 2813132730Skan 281490075Sobrien c_common_nodes_and_builtins (); 281518334Speter 2816132730Skan /* In C, comparisons and TRUTH_* expressions have type int. */ 2817132730Skan truthvalue_type_node = integer_type_node; 2818132730Skan truthvalue_true_node = integer_one_node; 2819132730Skan truthvalue_false_node = integer_zero_node; 282018334Speter 2821132730Skan /* Even in C99, which has a real boolean type. */ 282290075Sobrien pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"), 2823132730Skan boolean_type_node)); 282418334Speter 2825132730Skan input_location = save_loc; 2826132730Skan 2827169699Skan pedantic_lvalues = true; 282818334Speter 282990075Sobrien make_fname_decl = c_make_fname_decl; 283090075Sobrien start_fname_decls (); 283190075Sobrien} 283218334Speter 283390075Sobrien/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the 283490075Sobrien decl, NAME is the initialization string and TYPE_DEP indicates whether 283590075Sobrien NAME depended on the type of the function. As we don't yet implement 283690075Sobrien delayed emission of static data, we mark the decl as emitted 283790075Sobrien so it is not placed in the output. Anything using it must therefore pull 2838132730Skan out the STRING_CST initializer directly. FIXME. */ 283918334Speter 284090075Sobrienstatic tree 2841132730Skanc_make_fname_decl (tree id, int type_dep) 284290075Sobrien{ 284390075Sobrien const char *name = fname_as_string (type_dep); 284490075Sobrien tree decl, type, init; 284590075Sobrien size_t length = strlen (name); 284618334Speter 2847169699Skan type = build_array_type (char_type_node, 2848169699Skan build_index_type (size_int (length))); 2849169699Skan type = c_build_qualified_type (type, TYPE_QUAL_CONST); 285018334Speter 285190075Sobrien decl = build_decl (VAR_DECL, id, type); 2852132730Skan 285390075Sobrien TREE_STATIC (decl) = 1; 285490075Sobrien TREE_READONLY (decl) = 1; 285590075Sobrien DECL_ARTIFICIAL (decl) = 1; 2856132730Skan 285790075Sobrien init = build_string (length + 1, name); 2858169699Skan free ((char *) name); 285990075Sobrien TREE_TYPE (init) = type; 286090075Sobrien DECL_INITIAL (decl) = init; 286118334Speter 286290075Sobrien TREE_USED (decl) = 1; 2863132730Skan 2864169699Skan if (current_function_decl 2865169699Skan /* For invalid programs like this: 2866169699Skan 2867169699Skan void foo() 2868169699Skan const char* p = __FUNCTION__; 2869169699Skan 2870169699Skan the __FUNCTION__ is believed to appear in K&R style function 2871169699Skan parameter declarator. In that case we still don't have 2872169699Skan function_scope. */ 2873169699Skan && (!errorcount || current_function_scope)) 2874132730Skan { 2875132730Skan DECL_CONTEXT (decl) = current_function_decl; 2876169699Skan bind (id, decl, current_function_scope, 2877169699Skan /*invisible=*/false, /*nested=*/false); 2878132730Skan } 2879132730Skan 288090075Sobrien finish_decl (decl, init, NULL_TREE); 288118334Speter 288290075Sobrien return decl; 288318334Speter} 288418334Speter 288518334Speter/* Return a definition for a builtin function named NAME and whose data type 288618334Speter is TYPE. TYPE should be a function type with argument types. 288718334Speter FUNCTION_CODE tells later passes how to compile calls to this function. 288818334Speter See tree.h for its possible values. 288918334Speter 289018334Speter If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME, 2891117421Skan the name to be called if we can't opencode the function. If 2892117421Skan ATTRS is nonzero, use that for the function's attribute list. */ 289318334Speter 289418334Spetertree 2895132730Skanbuiltin_function (const char *name, tree type, int function_code, 2896169699Skan enum built_in_class cl, const char *library_name, 2897132730Skan tree attrs) 289818334Speter{ 2899169699Skan tree id = get_identifier (name); 2900169699Skan tree decl = build_decl (FUNCTION_DECL, id, type); 2901169699Skan TREE_PUBLIC (decl) = 1; 290218334Speter DECL_EXTERNAL (decl) = 1; 2903169699Skan DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl); 2904169699Skan DECL_BUILT_IN_CLASS (decl) = cl; 2905169699Skan DECL_FUNCTION_CODE (decl) = function_code; 2906169699Skan C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0); 290718334Speter if (library_name) 290890075Sobrien SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name)); 290990075Sobrien 2910169699Skan /* Should never be called on a symbol with a preexisting meaning. */ 2911169699Skan gcc_assert (!I_SYMBOL_BINDING (id)); 291218334Speter 2913169699Skan bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false); 2914169699Skan 2915169699Skan /* Builtins in the implementation namespace are made visible without 2916169699Skan needing to be explicitly declared. See push_file_scope. */ 2917169699Skan if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1]))) 2918169699Skan { 2919169699Skan TREE_CHAIN (decl) = visible_builtins; 2920169699Skan visible_builtins = decl; 2921169699Skan } 2922169699Skan 292390075Sobrien /* Possibly apply some default attributes to this built-in function. */ 2924117421Skan if (attrs) 2925117421Skan decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN); 2926117421Skan else 2927117421Skan decl_attributes (&decl, NULL_TREE, 0); 292890075Sobrien 292918334Speter return decl; 293018334Speter} 293118334Speter 293218334Speter/* Called when a declaration is seen that contains no names to declare. 293318334Speter If its type is a reference to a structure, union or enum inherited 293418334Speter from a containing scope, shadow that tag name for the current scope 293518334Speter with a forward reference. 293618334Speter If its type defines a new named structure or union 293718334Speter or defines an enum, it is valid but we need not do anything here. 293818334Speter Otherwise, it is an error. */ 293918334Speter 294018334Spetervoid 2941169699Skanshadow_tag (const struct c_declspecs *declspecs) 294218334Speter{ 294318334Speter shadow_tag_warned (declspecs, 0); 294418334Speter} 294518334Speter 2946169699Skan/* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning, 2947169699Skan but no pedwarn. */ 294818334Spetervoid 2949169699Skanshadow_tag_warned (const struct c_declspecs *declspecs, int warned) 295018334Speter{ 2951169699Skan bool found_tag = false; 295218334Speter 2953169699Skan if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p) 295418334Speter { 2955169699Skan tree value = declspecs->type; 295690075Sobrien enum tree_code code = TREE_CODE (value); 295718334Speter 295818334Speter if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE) 295918334Speter /* Used to test also that TYPE_SIZE (value) != 0. 296018334Speter That caused warning for `struct foo;' at top level in the file. */ 296118334Speter { 2962132730Skan tree name = TYPE_NAME (value); 296390075Sobrien tree t; 296418334Speter 2965169699Skan found_tag = true; 296618334Speter 296718334Speter if (name == 0) 296818334Speter { 296918334Speter if (warned != 1 && code != ENUMERAL_TYPE) 297018334Speter /* Empty unnamed enum OK */ 297118334Speter { 297218334Speter pedwarn ("unnamed struct/union that defines no instances"); 297318334Speter warned = 1; 297418334Speter } 297518334Speter } 2976169699Skan else if (!declspecs->tag_defined_p 2977169699Skan && declspecs->storage_class != csc_none) 2978169699Skan { 2979169699Skan if (warned != 1) 2980169699Skan pedwarn ("empty declaration with storage class specifier " 2981169699Skan "does not redeclare tag"); 2982169699Skan warned = 1; 2983169699Skan pending_xref_error (); 2984169699Skan } 2985169699Skan else if (!declspecs->tag_defined_p 2986169699Skan && (declspecs->const_p 2987169699Skan || declspecs->volatile_p 2988169699Skan || declspecs->restrict_p)) 2989169699Skan { 2990169699Skan if (warned != 1) 2991169699Skan pedwarn ("empty declaration with type qualifier " 2992169699Skan "does not redeclare tag"); 2993169699Skan warned = 1; 2994169699Skan pending_xref_error (); 2995169699Skan } 299618334Speter else 299718334Speter { 2998169699Skan pending_invalid_xref = 0; 2999132730Skan t = lookup_tag (code, name, 1); 300018334Speter 300118334Speter if (t == 0) 300218334Speter { 300318334Speter t = make_node (code); 300418334Speter pushtag (name, t); 300518334Speter } 300618334Speter } 300718334Speter } 300818334Speter else 300918334Speter { 3010169699Skan if (warned != 1 && !in_system_header) 301118334Speter { 3012169699Skan pedwarn ("useless type name in empty declaration"); 3013169699Skan warned = 1; 301418334Speter } 301518334Speter } 301618334Speter } 3017169699Skan else if (warned != 1 && !in_system_header && declspecs->typedef_p) 3018169699Skan { 3019169699Skan pedwarn ("useless type name in empty declaration"); 3020169699Skan warned = 1; 3021169699Skan } 302218334Speter 3023169699Skan pending_invalid_xref = 0; 302418334Speter 3025169699Skan if (declspecs->inline_p) 3026169699Skan { 3027169699Skan error ("%<inline%> in empty declaration"); 3028169699Skan warned = 1; 3029169699Skan } 3030169699Skan 3031169699Skan if (current_scope == file_scope && declspecs->storage_class == csc_auto) 3032169699Skan { 3033169699Skan error ("%<auto%> in file-scope empty declaration"); 3034169699Skan warned = 1; 3035169699Skan } 3036169699Skan 3037169699Skan if (current_scope == file_scope && declspecs->storage_class == csc_register) 3038169699Skan { 3039169699Skan error ("%<register%> in file-scope empty declaration"); 3040169699Skan warned = 1; 3041169699Skan } 3042169699Skan 3043169699Skan if (!warned && !in_system_header && declspecs->storage_class != csc_none) 3044169699Skan { 3045169699Skan warning (0, "useless storage class specifier in empty declaration"); 3046169699Skan warned = 2; 3047169699Skan } 3048169699Skan 3049169699Skan if (!warned && !in_system_header && declspecs->thread_p) 3050169699Skan { 3051169699Skan warning (0, "useless %<__thread%> in empty declaration"); 3052169699Skan warned = 2; 3053169699Skan } 3054169699Skan 3055169699Skan if (!warned && !in_system_header && (declspecs->const_p 3056169699Skan || declspecs->volatile_p 3057169699Skan || declspecs->restrict_p)) 3058169699Skan { 3059169699Skan warning (0, "useless type qualifier in empty declaration"); 3060169699Skan warned = 2; 3061169699Skan } 3062169699Skan 306318334Speter if (warned != 1) 306418334Speter { 3065169699Skan if (!found_tag) 306618334Speter pedwarn ("empty declaration"); 306718334Speter } 306818334Speter} 306918334Speter 307090075Sobrien 3071169699Skan/* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_* 3072169699Skan bits. SPECS represents declaration specifiers that the grammar 3073169699Skan only permits to contain type qualifiers and attributes. */ 3074169699Skan 3075169699Skanint 3076169699Skanquals_from_declspecs (const struct c_declspecs *specs) 307790075Sobrien{ 3078169699Skan int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0) 3079169699Skan | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0) 3080169699Skan | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)); 3081169699Skan gcc_assert (!specs->type 3082169699Skan && !specs->decl_attr 3083169699Skan && specs->typespec_word == cts_none 3084169699Skan && specs->storage_class == csc_none 3085169699Skan && !specs->typedef_p 3086169699Skan && !specs->explicit_signed_p 3087169699Skan && !specs->deprecated_p 3088169699Skan && !specs->long_p 3089169699Skan && !specs->long_long_p 3090169699Skan && !specs->short_p 3091169699Skan && !specs->signed_p 3092169699Skan && !specs->unsigned_p 3093169699Skan && !specs->complex_p 3094169699Skan && !specs->inline_p 3095169699Skan && !specs->thread_p); 3096169699Skan return quals; 3097169699Skan} 3098169699Skan 3099169699Skan/* Construct an array declarator. EXPR is the expression inside [], 3100169699Skan or NULL_TREE. QUALS are the type qualifiers inside the [] (to be 3101169699Skan applied to the pointer to which a parameter array is converted). 3102169699Skan STATIC_P is true if "static" is inside the [], false otherwise. 3103169699Skan VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified 3104169699Skan length which is nevertheless a complete type, false otherwise. The 3105169699Skan field for the contained declarator is left to be filled in by 3106169699Skan set_array_declarator_inner. */ 3107169699Skan 3108169699Skanstruct c_declarator * 3109169699Skanbuild_array_declarator (tree expr, struct c_declspecs *quals, bool static_p, 3110169699Skan bool vla_unspec_p) 3111169699Skan{ 3112169699Skan struct c_declarator *declarator = XOBNEW (&parser_obstack, 3113169699Skan struct c_declarator); 3114169699Skan declarator->kind = cdk_array; 3115169699Skan declarator->declarator = 0; 3116169699Skan declarator->u.array.dimen = expr; 3117169699Skan if (quals) 3118169699Skan { 3119169699Skan declarator->u.array.attrs = quals->attrs; 3120169699Skan declarator->u.array.quals = quals_from_declspecs (quals); 3121169699Skan } 3122169699Skan else 3123169699Skan { 3124169699Skan declarator->u.array.attrs = NULL_TREE; 3125169699Skan declarator->u.array.quals = 0; 3126169699Skan } 3127169699Skan declarator->u.array.static_p = static_p; 3128169699Skan declarator->u.array.vla_unspec_p = vla_unspec_p; 312990075Sobrien if (pedantic && !flag_isoc99) 313090075Sobrien { 3131169699Skan if (static_p || quals != NULL) 3132169699Skan pedwarn ("ISO C90 does not support %<static%> or type " 3133169699Skan "qualifiers in parameter array declarators"); 313490075Sobrien if (vla_unspec_p) 3135169699Skan pedwarn ("ISO C90 does not support %<[*]%> array declarators"); 313690075Sobrien } 313790075Sobrien if (vla_unspec_p) 3138169699Skan { 3139169699Skan if (!current_scope->parm_flag) 3140169699Skan { 3141169699Skan /* C99 6.7.5.2p4 */ 3142169699Skan error ("%<[*]%> not allowed in other than function prototype scope"); 3143169699Skan declarator->u.array.vla_unspec_p = false; 3144169699Skan return NULL; 3145169699Skan } 3146169699Skan current_scope->had_vla_unspec = true; 3147169699Skan } 3148169699Skan return declarator; 314990075Sobrien} 315090075Sobrien 3151169699Skan/* Set the contained declarator of an array declarator. DECL is the 3152169699Skan declarator, as constructed by build_array_declarator; INNER is what 3153169699Skan appears on the left of the []. ABSTRACT_P is true if it is an 3154169699Skan abstract declarator, false otherwise; this is used to reject static 3155169699Skan and type qualifiers in abstract declarators, where they are not in 3156169699Skan the C99 grammar (subject to possible change in DR#289). */ 315790075Sobrien 3158169699Skanstruct c_declarator * 3159169699Skanset_array_declarator_inner (struct c_declarator *decl, 3160169699Skan struct c_declarator *inner, bool abstract_p) 316190075Sobrien{ 3162169699Skan decl->declarator = inner; 3163169699Skan if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED 3164169699Skan || decl->u.array.attrs != NULL_TREE 3165169699Skan || decl->u.array.static_p)) 316690075Sobrien error ("static or type qualifiers in abstract declarator"); 316790075Sobrien return decl; 316890075Sobrien} 3169169699Skan 3170169699Skan/* INIT is a constructor that forms DECL's initializer. If the final 3171169699Skan element initializes a flexible array field, add the size of that 3172169699Skan initializer to DECL's size. */ 3173169699Skan 3174169699Skanstatic void 3175169699Skanadd_flexible_array_elts_to_size (tree decl, tree init) 3176169699Skan{ 3177169699Skan tree elt, type; 3178169699Skan 3179169699Skan if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init))) 3180169699Skan return; 3181169699Skan 3182169699Skan elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value; 3183169699Skan type = TREE_TYPE (elt); 3184169699Skan if (TREE_CODE (type) == ARRAY_TYPE 3185169699Skan && TYPE_SIZE (type) == NULL_TREE 3186169699Skan && TYPE_DOMAIN (type) != NULL_TREE 3187169699Skan && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE) 3188169699Skan { 3189169699Skan complete_array_type (&type, elt, false); 3190169699Skan DECL_SIZE (decl) 3191169699Skan = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type)); 3192169699Skan DECL_SIZE_UNIT (decl) 3193169699Skan = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type)); 3194169699Skan } 3195169699Skan} 3196261188Spfg 3197261188Spfg/* APPLE LOCAL begin blocks 6339747 */ 3198261188Spfg/* Decode a block literal type, such as "int **", returning a ...FUNCTION_DECL node. */ 3199261188Spfg 3200261188Spfgtree 3201261188Spfggrokblockdecl (struct c_declspecs *specs, struct c_declarator *declarator) 3202261188Spfg{ 3203261188Spfg tree decl; 3204261188Spfg tree attrs = specs->attrs; 3205261188Spfg 3206261188Spfg specs->attrs = NULL_TREE; 3207261188Spfg 3208261188Spfg decl = grokdeclarator (declarator, specs, BLOCKDEF, 3209261188Spfg false, NULL); 3210261188Spfg 3211261188Spfg /* Apply attributes. */ 3212261188Spfg decl_attributes (&decl, attrs, 0); 3213261188Spfg 3214261188Spfg return decl; 3215261188Spfg} 3216261188Spfg/* APPLE LOCAL end blocks 6339747 */ 3217261188Spfg 321818334Speter/* Decode a "typename", such as "int **", returning a ..._TYPE node. */ 321918334Speter 322018334Spetertree 3221169699Skangroktypename (struct c_type_name *type_name) 322218334Speter{ 3223169699Skan tree type; 3224169699Skan tree attrs = type_name->specs->attrs; 322590075Sobrien 3226169699Skan type_name->specs->attrs = NULL_TREE; 322790075Sobrien 3228169699Skan type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME, 3229169699Skan false, NULL); 323090075Sobrien 323190075Sobrien /* Apply attributes. */ 3232169699Skan decl_attributes (&type, attrs, 0); 323390075Sobrien 3234169699Skan return type; 323518334Speter} 323618334Speter 323718334Speter/* Decode a declarator in an ordinary declaration or data definition. 323818334Speter This is called as soon as the type information and variable name 323918334Speter have been parsed, before parsing the initializer if any. 324018334Speter Here we create the ..._DECL node, fill in its type, 324118334Speter and put it on the list of decls for the current context. 324218334Speter The ..._DECL node is returned as the value. 324318334Speter 324418334Speter Exception: for arrays where the length is not specified, 324518334Speter the type is left null, to be filled in by `finish_decl'. 324618334Speter 324718334Speter Function definitions do not come here; they go to start_function 324818334Speter instead. However, external and forward declarations of functions 324918334Speter do go through here. Structure field declarations are done by 325018334Speter grokfield and not through here. */ 325118334Speter 325218334Spetertree 3253169699Skanstart_decl (struct c_declarator *declarator, struct c_declspecs *declspecs, 3254169699Skan bool initialized, tree attributes) 325518334Speter{ 325690075Sobrien tree decl; 325790075Sobrien tree tem; 3258132730Skan 325990075Sobrien /* An object declared as __attribute__((deprecated)) suppresses 326090075Sobrien warnings of uses of other deprecated items. */ 3261260918Spfg /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */ 3262260918Spfg /* An object declared as __attribute__((unavailable)) suppresses 3263260918Spfg any reports of being declared with unavailable or deprecated 3264260918Spfg items. An object declared as __attribute__((deprecated)) 3265260918Spfg suppresses warnings of uses of other deprecated items. */ 3266260918Spfg#ifdef A_LESS_INEFFICENT_WAY /* which I really don't want to do! */ 326790075Sobrien if (lookup_attribute ("deprecated", attributes)) 326890075Sobrien deprecated_state = DEPRECATED_SUPPRESS; 3269260918Spfg else if (lookup_attribute ("unavailable", attributes)) 3270260918Spfg deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS; 3271260918Spfg#else /* a more efficient way doing what lookup_attribute would do */ 3272260918Spfg tree a; 327318334Speter 3274260918Spfg for (a = attributes; a; a = TREE_CHAIN (a)) 3275260918Spfg { 3276260918Spfg tree name = TREE_PURPOSE (a); 3277260918Spfg if (TREE_CODE (name) == IDENTIFIER_NODE) 3278260918Spfg if (is_attribute_p ("deprecated", name)) 3279260918Spfg { 3280260918Spfg deprecated_state = DEPRECATED_SUPPRESS; 3281260918Spfg break; 3282260918Spfg } 3283260918Spfg if (is_attribute_p ("unavailable", name)) 3284260918Spfg { 3285260918Spfg deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS; 3286260918Spfg break; 3287260918Spfg } 3288260918Spfg } 3289260918Spfg#endif 3290260918Spfg /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */ 3291260918Spfg 329290075Sobrien decl = grokdeclarator (declarator, declspecs, 3293132730Skan NORMAL, initialized, NULL); 3294169699Skan if (!decl) 3295169699Skan return 0; 3296132730Skan 329790075Sobrien deprecated_state = DEPRECATED_NORMAL; 329818334Speter 329990075Sobrien if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL 330090075Sobrien && MAIN_NAME_P (DECL_NAME (decl))) 3301169699Skan warning (OPT_Wmain, "%q+D is usually a function", decl); 330250397Sobrien 330318334Speter if (initialized) 330418334Speter /* Is it valid for this decl to have an initializer at all? 330518334Speter If not, set INITIALIZED to zero, which will indirectly 3306169699Skan tell 'finish_decl' to ignore the initializer once it is parsed. */ 330718334Speter switch (TREE_CODE (decl)) 330818334Speter { 330918334Speter case TYPE_DECL: 3310169699Skan error ("typedef %qD is initialized (use __typeof__ instead)", decl); 3311104761Skan initialized = 0; 331218334Speter break; 331318334Speter 331418334Speter case FUNCTION_DECL: 3315169699Skan error ("function %qD is initialized like a variable", decl); 331618334Speter initialized = 0; 331718334Speter break; 331818334Speter 331918334Speter case PARM_DECL: 332018334Speter /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */ 3321169699Skan error ("parameter %qD is initialized", decl); 332218334Speter initialized = 0; 332318334Speter break; 332418334Speter 332518334Speter default: 3326169699Skan /* Don't allow initializations for incomplete types except for 3327169699Skan arrays which might be completed by the initialization. */ 332890075Sobrien 3329169699Skan /* This can happen if the array size is an undefined macro. 3330169699Skan We already gave a warning, so we don't need another one. */ 333190075Sobrien if (TREE_TYPE (decl) == error_mark_node) 333290075Sobrien initialized = 0; 333390075Sobrien else if (COMPLETE_TYPE_P (TREE_TYPE (decl))) 333418334Speter { 333518334Speter /* A complete type is ok if size is fixed. */ 333618334Speter 333718334Speter if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST 333818334Speter || C_DECL_VARIABLE_SIZE (decl)) 333918334Speter { 334018334Speter error ("variable-sized object may not be initialized"); 334118334Speter initialized = 0; 334218334Speter } 334318334Speter } 334418334Speter else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE) 334518334Speter { 3346169699Skan error ("variable %qD has initializer but incomplete type", decl); 334718334Speter initialized = 0; 334818334Speter } 3349169699Skan else if (C_DECL_VARIABLE_SIZE (decl)) 335018334Speter { 3351169699Skan /* Although C99 is unclear about whether incomplete arrays 3352169699Skan of VLAs themselves count as VLAs, it does not make 3353169699Skan sense to permit them to be initialized given that 3354169699Skan ordinary VLAs may not be initialized. */ 3355169699Skan error ("variable-sized object may not be initialized"); 335618334Speter initialized = 0; 335718334Speter } 335818334Speter } 335918334Speter 336018334Speter if (initialized) 336118334Speter { 3362169699Skan if (current_scope == file_scope) 336318334Speter TREE_STATIC (decl) = 1; 336418334Speter 3365169699Skan /* Tell 'pushdecl' this is an initialized decl 336618334Speter even though we don't yet have the initializer expression. 3367169699Skan Also tell 'finish_decl' it may store the real initializer. */ 336818334Speter DECL_INITIAL (decl) = error_mark_node; 336918334Speter } 337018334Speter 337118334Speter /* If this is a function declaration, write a record describing it to the 337218334Speter prototypes file (if requested). */ 337318334Speter 337418334Speter if (TREE_CODE (decl) == FUNCTION_DECL) 337518334Speter gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0); 337618334Speter 337750397Sobrien /* ANSI specifies that a tentative definition which is not merged with 337850397Sobrien a non-tentative definition behaves exactly like a definition with an 337950397Sobrien initializer equal to zero. (Section 3.7.2) 3380117421Skan 3381117421Skan -fno-common gives strict ANSI behavior, though this tends to break 3382117421Skan a large body of code that grew up without this rule. 3383117421Skan 3384117421Skan Thread-local variables are never common, since there's no entrenched 3385117421Skan body of code to break, and it allows more efficient variable references 3386132730Skan in the presence of dynamic linking. */ 3387117421Skan 3388117421Skan if (TREE_CODE (decl) == VAR_DECL 3389117421Skan && !initialized 3390117421Skan && TREE_PUBLIC (decl) 3391169699Skan && !DECL_THREAD_LOCAL_P (decl) 3392117421Skan && !flag_no_common) 339350397Sobrien DECL_COMMON (decl) = 1; 339418334Speter 339518334Speter /* Set attributes here so if duplicate decl, will have proper attributes. */ 339690075Sobrien decl_attributes (&decl, attributes, 0); 339718334Speter 3398189824Sdas /* Handle gnu_inline attribute. */ 3399189824Sdas if (declspecs->inline_p 3400189824Sdas && !flag_gnu89_inline 3401189824Sdas && TREE_CODE (decl) == FUNCTION_DECL 3402189824Sdas && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))) 3403189824Sdas { 3404189824Sdas if (declspecs->storage_class == csc_auto && current_scope != file_scope) 3405189824Sdas ; 3406189824Sdas else if (declspecs->storage_class != csc_static) 3407189824Sdas DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl); 3408189824Sdas } 3409189824Sdas 3410132730Skan if (TREE_CODE (decl) == FUNCTION_DECL 3411132730Skan && targetm.calls.promote_prototypes (TREE_TYPE (decl))) 3412132730Skan { 3413169699Skan struct c_declarator *ce = declarator; 341496263Sobrien 3415169699Skan if (ce->kind == cdk_pointer) 3416169699Skan ce = declarator->declarator; 3417169699Skan if (ce->kind == cdk_function) 3418132730Skan { 3419169699Skan tree args = ce->u.arg_info->parms; 3420132730Skan for (; args; args = TREE_CHAIN (args)) 3421132730Skan { 3422132730Skan tree type = TREE_TYPE (args); 3423169699Skan if (type && INTEGRAL_TYPE_P (type) 3424132730Skan && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 3425132730Skan DECL_ARG_TYPE (args) = integer_type_node; 3426132730Skan } 3427132730Skan } 3428132730Skan } 3429132730Skan 343090075Sobrien if (TREE_CODE (decl) == FUNCTION_DECL 343190075Sobrien && DECL_DECLARED_INLINE_P (decl) 343290075Sobrien && DECL_UNINLINABLE (decl) 343390075Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) 3434169699Skan warning (OPT_Wattributes, "inline function %q+D given attribute noinline", 3435169699Skan decl); 343690075Sobrien 3437189824Sdas /* C99 6.7.4p3: An inline definition of a function with external 3438189824Sdas linkage shall not contain a definition of a modifiable object 3439189824Sdas with static storage duration... */ 3440189824Sdas if (TREE_CODE (decl) == VAR_DECL 3441189824Sdas && current_scope != file_scope 3442189824Sdas && TREE_STATIC (decl) 3443189824Sdas && !TREE_READONLY (decl) 3444189824Sdas && DECL_DECLARED_INLINE_P (current_function_decl) 3445189824Sdas && DECL_EXTERNAL (current_function_decl)) 3446189824Sdas pedwarn ("%q+D is static but declared in inline function %qD " 3447189824Sdas "which is not static", decl, current_function_decl); 3448189824Sdas 3449132730Skan /* Add this decl to the current scope. 345018334Speter TEM may equal DECL or it may be a previous decl of the same name. */ 345118334Speter tem = pushdecl (decl); 345218334Speter 3453169699Skan if (initialized && DECL_EXTERNAL (tem)) 345418334Speter { 3455169699Skan DECL_EXTERNAL (tem) = 0; 3456169699Skan TREE_STATIC (tem) = 1; 345718334Speter } 345818334Speter 345918334Speter return tem; 346018334Speter} 346118334Speter 3462169699Skan/* Initialize EH if not initialized yet and exceptions are enabled. */ 3463169699Skan 3464169699Skanvoid 3465169699Skanc_maybe_initialize_eh (void) 3466169699Skan{ 3467169699Skan if (!flag_exceptions || c_eh_initialized_p) 3468169699Skan return; 3469169699Skan 3470169699Skan c_eh_initialized_p = true; 3471169699Skan eh_personality_libfunc 3472169699Skan = init_one_libfunc (USING_SJLJ_EXCEPTIONS 3473169699Skan ? "__gcc_personality_sj0" 3474169699Skan : "__gcc_personality_v0"); 3475169699Skan default_init_unwind_resume_libfunc (); 3476169699Skan using_eh_for_cleanups (); 3477169699Skan} 3478169699Skan 3479261188Spfg/* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ cr) */ 3480261188Spfg#define BLOCK_ALIGN_MAX 18 3481261188Spfgstatic tree block_byref_id_object_copy[BLOCK_BYREF_CURRENT_MAX*(BLOCK_ALIGN_MAX+1)]; 3482261188Spfgstatic tree block_byref_id_object_dispose[BLOCK_BYREF_CURRENT_MAX*(BLOCK_ALIGN_MAX+1)]; 3483261188Spfg 3484261188Spfg/** 3485261188Spfg This routine builds: 3486261188Spfg 3487261188Spfg void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, 3488261188Spfg struct Block_byref_id_object *src) { 3489261188Spfg _Block_object_assign (&_dest->object, _src->object, BLOCK_FIELD_IS_OBJECT[|BLOCK_FIELD_IS_WEAK]) // objects 3490261188Spfg _Block_object_assign(&_dest->object, _src->object, BLOCK_FIELD_IS_BLOCK[|BLOCK_FIELD_IS_WEAK]) // blocks 3491261188Spfg } */ 3492261188Spfgstatic void 3493261188Spfgsynth_block_byref_id_object_copy_func (int flag, int kind) 3494261188Spfg{ 3495261188Spfg tree stmt, fnbody; 3496261188Spfg tree dst_arg, src_arg; 3497261188Spfg tree dst_obj, src_obj; 3498261188Spfg struct c_arg_info * arg_info; 3499261188Spfg tree call_exp; 3500261188Spfg gcc_assert (block_byref_id_object_copy[kind]); 3501261188Spfg /* Set up: (void* _dest, void*_src) parameters. */ 3502261188Spfg dst_arg = build_decl (PARM_DECL, get_identifier ("_dst"), 3503261188Spfg ptr_type_node); 3504261188Spfg TREE_USED (dst_arg) = 1; 3505261188Spfg DECL_ARG_TYPE (dst_arg) = ptr_type_node; 3506261188Spfg src_arg = build_decl (PARM_DECL, get_identifier ("_src"), 3507261188Spfg ptr_type_node); 3508261188Spfg TREE_USED (src_arg) = 1; 3509261188Spfg DECL_ARG_TYPE (src_arg) = ptr_type_node; 3510261188Spfg arg_info = xcalloc (1, sizeof (struct c_arg_info)); 3511261188Spfg TREE_CHAIN (dst_arg) = src_arg; 3512261188Spfg arg_info->parms = dst_arg; 3513261188Spfg arg_info->types = tree_cons (NULL_TREE, ptr_type_node, 3514261188Spfg tree_cons (NULL_TREE, 3515261188Spfg ptr_type_node, 3516261188Spfg NULL_TREE)); 3517261188Spfg /* function header synthesis. */ 3518261188Spfg push_function_context (); 3519261188Spfg start_block_helper_function (block_byref_id_object_copy[kind]); 3520261188Spfg store_parm_decls_from (arg_info); 3521261188Spfg 3522261188Spfg /* Body of the function. */ 3523261188Spfg stmt = c_begin_compound_stmt (true); 3524261188Spfg /* Build dst->object */ 3525261188Spfg dst_obj = build_indirect_object_id_exp (dst_arg); 3526261188Spfg 3527261188Spfg 3528261188Spfg /* src_obj is: _src->object. */ 3529261188Spfg src_obj = build_indirect_object_id_exp (src_arg); 3530261188Spfg 3531261188Spfg /* APPLE LOCAL begin radar 6180456 */ 3532261188Spfg /* _Block_object_assign (&_dest->object, _src->object, BLOCK_FIELD_IS_OBJECT) or : 3533261188Spfg _Block_object_assign (&_dest->object, _src->object, BLOCK_FIELD_IS_BLOCK) */ 3534261188Spfg /* APPLE LOCAL begin radar 6573923 */ 3535261188Spfg /* Also add the new flag when calling _Block_object_dispose 3536261188Spfg from byref dispose helper. */ 3537261188Spfg flag |= BLOCK_BYREF_CALLER; 3538261188Spfg /* APPLE LOCAL end radar 6573923 */ 3539261188Spfg call_exp = build_block_object_assign_call_exp (build_fold_addr_expr (dst_obj), src_obj, flag); 3540261188Spfg add_stmt (call_exp); 3541261188Spfg /* APPLE LOCAL end radar 6180456 */ 3542261188Spfg 3543261188Spfg fnbody = c_end_compound_stmt (stmt, true); 3544261188Spfg add_stmt (fnbody); 3545261188Spfg finish_function (); 3546261188Spfg pop_function_context (); 3547261188Spfg free (arg_info); 3548261188Spfg} 3549261188Spfg 3550261188Spfg/** 3551261188Spfg This routine builds: 3552261188Spfg 3553261188Spfg void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { 3554261188Spfg _Block_object_dispose(_src->object, BLOCK_FIELD_IS_OBJECT[|BLOCK_FIELD_IS_WEAK]) // object 3555261188Spfg _Block_object_dispose(_src->object, BLOCK_FIELD_IS_BLOCK[|BLOCK_FIELD_IS_WEAK]) // block 3556261188Spfg } */ 3557261188Spfgstatic void 3558261188Spfgsynth_block_byref_id_object_dispose_func (int flag, int kind) 3559261188Spfg{ 3560261188Spfg tree stmt, fnbody; 3561261188Spfg tree src_arg, src_obj, rel_exp; 3562261188Spfg struct c_arg_info * arg_info; 3563261188Spfg gcc_assert (block_byref_id_object_dispose[kind]); 3564261188Spfg /* Set up: (void *_src) parameter. */ 3565261188Spfg src_arg = build_decl (PARM_DECL, get_identifier ("_src"), 3566261188Spfg ptr_type_node); 3567261188Spfg TREE_USED (src_arg) = 1; 3568261188Spfg DECL_ARG_TYPE (src_arg) = ptr_type_node; 3569261188Spfg arg_info = xcalloc (1, sizeof (struct c_arg_info)); 3570261188Spfg arg_info->parms = src_arg; 3571261188Spfg arg_info->types = tree_cons (NULL_TREE, ptr_type_node, 3572261188Spfg NULL_TREE); 3573261188Spfg /* function header synthesis. */ 3574261188Spfg push_function_context (); 3575261188Spfg start_block_helper_function (block_byref_id_object_dispose[kind]); 3576261188Spfg store_parm_decls_from (arg_info); 3577261188Spfg 3578261188Spfg /* Body of the function. */ 3579261188Spfg stmt = c_begin_compound_stmt (true); 3580261188Spfg src_obj = build_indirect_object_id_exp (src_arg); 3581261188Spfg 3582261188Spfg /* APPLE LOCAL begin radar 6180456 */ 3583261188Spfg /* _Block_object_dispose(_src->object, BLOCK_FIELD_IS_OBJECT) : or 3584261188Spfg _Block_object_dispose(_src->object, BLOCK_FIELD_IS_BLOCK) */ 3585261188Spfg /* APPLE LOCAL begin radar 6573923 */ 3586261188Spfg /* Also add the new flag when calling _Block_object_dispose 3587261188Spfg from byref dispose helper. */ 3588261188Spfg flag |= BLOCK_BYREF_CALLER; 3589261188Spfg /* APPLE LOCAL end radar 6573923 */ 3590261188Spfg rel_exp = build_block_object_dispose_call_exp (src_obj, flag); 3591261188Spfg /* APPLE LOCAL end radar 6180456 */ 3592261188Spfg add_stmt (rel_exp); 3593261188Spfg 3594261188Spfg fnbody = c_end_compound_stmt (stmt, true); 3595261188Spfg add_stmt (fnbody); 3596261188Spfg finish_function (); 3597261188Spfg pop_function_context (); 3598261188Spfg free (arg_info); 3599261188Spfg} 3600261188Spfg 3601261188Spfg/* new_block_byref_decl - This routine changes a 'typex x' declared variable into: 3602261188Spfg 3603261188Spfg struct __Block_byref_x { 3604261188Spfg // APPLE LOCAL radar 6244520 3605261188Spfg void *__isa; // NULL for everything except __weak pointers 3606261188Spfg struct Block_byref_x *__forwarding; 3607261188Spfg int32_t __flags; 3608261188Spfg int32_t __size; 3609261188Spfg void *__ByrefKeepFuncPtr; // Only if variable is __block ObjC object 3610261188Spfg void *__ByrefDestroyFuncPtr; // Only if variable is __block ObjC object 3611261188Spfg typex x; 3612261188Spfg } x; 3613261188Spfg*/ 3614261188Spfg 3615261188Spfgstatic tree 3616261188Spfgnew_block_byref_decl (tree decl) 3617261188Spfg{ 3618261188Spfg static int unique_count; 3619261188Spfg /* APPLE LOCAL radar 5847976 */ 3620261188Spfg int save_flag_objc_gc; 3621261188Spfg tree Block_byref_type; 3622261188Spfg tree field_decl_chain, field_decl; 3623261188Spfg const char *prefix = "__Block_byref_"; 3624261188Spfg char *string = alloca (strlen (IDENTIFIER_POINTER (DECL_NAME (decl))) + 3625261188Spfg strlen (prefix) + 8 /* to hold the count */); 3626261188Spfg 3627261188Spfg sprintf (string, "%s%d_%s", prefix, ++unique_count, 3628261188Spfg IDENTIFIER_POINTER (DECL_NAME (decl))); 3629261188Spfg 3630261188Spfg push_to_top_level (); 3631261188Spfg Block_byref_type = start_struct (RECORD_TYPE, get_identifier (string)); 3632261188Spfg 3633261188Spfg /* APPLE LOCAL begin radar 6244520 */ 3634261188Spfg /* void *__isa; */ 3635261188Spfg field_decl = build_decl (FIELD_DECL, get_identifier ("__isa"), ptr_type_node); 3636261188Spfg field_decl_chain = field_decl; 3637261188Spfg /* APPLE LOCAL end radar 6244520 */ 3638261188Spfg 3639261188Spfg /* struct Block_byref_x *__forwarding; */ 3640261188Spfg field_decl = build_decl (FIELD_DECL, get_identifier ("__forwarding"), 3641261188Spfg build_pointer_type (Block_byref_type)); 3642261188Spfg /* APPLE LOCAL radar 6244520 */ 3643261188Spfg chainon (field_decl_chain, field_decl); 3644261188Spfg 3645261188Spfg /* int32_t __flags; */ 3646261188Spfg field_decl = build_decl (FIELD_DECL, get_identifier ("__flags"), 3647261188Spfg unsigned_type_node); 3648261188Spfg chainon (field_decl_chain, field_decl); 3649261188Spfg 3650261188Spfg /* int32_t __size; */ 3651261188Spfg field_decl = build_decl (FIELD_DECL, get_identifier ("__size"), 3652261188Spfg unsigned_type_node); 3653261188Spfg chainon (field_decl_chain, field_decl); 3654261188Spfg 3655261188Spfg if (COPYABLE_BYREF_LOCAL_NONPOD (decl)) 3656261188Spfg { 3657261188Spfg /* void *__ByrefKeepFuncPtr; */ 3658261188Spfg field_decl = build_decl (FIELD_DECL, get_identifier ("__ByrefKeepFuncPtr"), 3659261188Spfg ptr_type_node); 3660261188Spfg chainon (field_decl_chain, field_decl); 3661261188Spfg 3662261188Spfg /* void *__ByrefDestroyFuncPtr; */ 3663261188Spfg field_decl = build_decl (FIELD_DECL, get_identifier ("__ByrefDestroyFuncPtr"), 3664261188Spfg ptr_type_node); 3665261188Spfg chainon (field_decl_chain, field_decl); 3666261188Spfg } 3667261188Spfg 3668261188Spfg /* typex x; */ 3669261188Spfg field_decl = build_decl (FIELD_DECL, DECL_NAME (decl), TREE_TYPE (decl)); 3670261188Spfg chainon (field_decl_chain, field_decl); 3671261188Spfg 3672261188Spfg pop_from_top_level (); 3673261188Spfg /* APPLE LOCAL begin radar 5847976 */ 3674261188Spfg /* Hack so we don't issue warning on a field_decl having __weak attribute */ 3675261188Spfg save_flag_objc_gc = flag_objc_gc; 3676261188Spfg flag_objc_gc = 0; 3677261188Spfg finish_struct (Block_byref_type, field_decl_chain, NULL_TREE); 3678261188Spfg flag_objc_gc = save_flag_objc_gc; 3679261188Spfg /* APPLE LOCAL end radar 5847976 */ 3680261188Spfg 3681261188Spfg TREE_TYPE (decl) = Block_byref_type; 3682261188Spfg /* Force layout_decl to recompute these fields. */ 3683261188Spfg DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0; 3684261188Spfg layout_decl (decl, 0); 3685261188Spfg return decl; 3686261188Spfg} 3687261188Spfg 3688261188Spfg/* init_byref_decl - This routine builds the initializer for the __Block_byref_x 3689261188Spfg type in the form of: 3690261188Spfg { NULL, &x, 0, sizeof(struct __Block_byref_x), initializer-expr}; 3691261188Spfg 3692261188Spfg or: 3693261188Spfg { NULL, &x, 0, sizeof(struct __Block_byref_x)}; 3694261188Spfg when INIT is NULL_TREE 3695261188Spfg 3696261188Spfg For __block ObjC objects, it also adds "byref_keep" and "byref_destroy" 3697261188Spfg Funtion pointers. So the most general initializers would be: 3698261188Spfg 3699261188Spfg { NULL, &x, 0, sizeof(struct __Block_byref_x), &byref_keep, &byref_destroy, 3700261188Spfg &initializer-expr}; 3701261188Spfg*/ 3702261188Spfgstatic tree 3703261188Spfg/* APPLE LOCAL radar 5847976 */ 3704261188Spfginit_byref_decl (tree decl, tree init, int flag) 3705261188Spfg{ 3706261188Spfg tree initlist; 3707261188Spfg tree block_byref_type = TREE_TYPE (decl); 3708261188Spfg int size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (block_byref_type)); 3709261188Spfg unsigned flags = 0; 3710261188Spfg tree fields; 3711261188Spfg 3712261188Spfg if (COPYABLE_BYREF_LOCAL_NONPOD (decl)) 3713261188Spfg flags = BLOCK_HAS_COPY_DISPOSE; 3714261188Spfg 3715261188Spfg fields = TYPE_FIELDS (block_byref_type); 3716261188Spfg /* APPLE LOCAL begin radar 6244520 */ 3717261188Spfg /* APPLE LOCAL begin radar 5847976 */ 3718261188Spfg initlist = tree_cons (fields, fold_convert (ptr_type_node, ((flag & BLOCK_FIELD_IS_WEAK) != 0) ? integer_one_node 3719261188Spfg : integer_zero_node), 0); 3720261188Spfg /* APPLE LOCAL end radar 5847976 */ 3721261188Spfg fields = TREE_CHAIN (fields); 3722261188Spfg 3723261188Spfg initlist = tree_cons (fields, 3724261188Spfg build_unary_op (ADDR_EXPR, decl, 0), initlist); 3725261188Spfg /* APPLE LOCAL end radar 6244520 */ 3726261188Spfg fields = TREE_CHAIN (fields); 3727261188Spfg 3728261188Spfg initlist = tree_cons (fields, build_int_cst (TREE_TYPE (fields), flags), 3729261188Spfg initlist); 3730261188Spfg fields = TREE_CHAIN (fields); 3731261188Spfg initlist = tree_cons (fields, build_int_cst (TREE_TYPE (fields), size), 3732261188Spfg initlist); 3733261188Spfg fields = TREE_CHAIN (fields); 3734261188Spfg 3735261188Spfg if (COPYABLE_BYREF_LOCAL_NONPOD (decl)) 3736261188Spfg { 3737261188Spfg char name[64]; 3738261188Spfg int align = exact_log2 ((DECL_ALIGN (decl)+TYPE_ALIGN (ptr_type_node)-1) / TYPE_ALIGN (ptr_type_node)); 3739261188Spfg int kind; 3740261188Spfg if (align == -1 || align > BLOCK_ALIGN_MAX) { 3741261188Spfg error ("invalid alignment for __block variable"); 3742261188Spfg kind = 0; 3743261188Spfg } else 3744261188Spfg kind = align*BLOCK_BYREF_CURRENT_MAX + flag; 3745261188Spfg /* Add &__Block_byref_id_object_copy, &__Block_byref_id_object_dispose 3746261188Spfg initializers. */ 3747261188Spfg if (!block_byref_id_object_copy[kind]) 3748261188Spfg { 3749261188Spfg /* Build a void __Block_byref_id_object_copy(void*, void*) type. */ 3750261188Spfg tree func_type = 3751261188Spfg build_function_type (void_type_node, 3752261188Spfg tree_cons (NULL_TREE, ptr_type_node, 3753261188Spfg tree_cons (NULL_TREE, ptr_type_node, 3754261188Spfg void_list_node))); 3755261188Spfg sprintf (name, "__Block_byref_id_object_copy%d", kind); 3756261188Spfg block_byref_id_object_copy[kind] = build_helper_func_decl (get_identifier (name), 3757261188Spfg func_type); 3758261188Spfg /* Synthesize function definition. */ 3759261188Spfg synth_block_byref_id_object_copy_func (flag, kind); 3760261188Spfg } 3761261188Spfg initlist = tree_cons (fields, 3762261188Spfg build_fold_addr_expr (block_byref_id_object_copy[kind]), 3763261188Spfg initlist); 3764261188Spfg fields = TREE_CHAIN (fields); 3765261188Spfg 3766261188Spfg if (!block_byref_id_object_dispose[kind]) 3767261188Spfg { 3768261188Spfg /* Synthesize void __Block_byref_id_object_dispose (void*) and 3769261188Spfg build &__Block_byref_id_object_dispose. */ 3770261188Spfg tree func_type = 3771261188Spfg build_function_type (void_type_node, 3772261188Spfg tree_cons (NULL_TREE, ptr_type_node, void_list_node)); 3773261188Spfg sprintf (name, "__Block_byref_id_object_dispose%d", kind); 3774261188Spfg block_byref_id_object_dispose[kind] = build_helper_func_decl (get_identifier (name), 3775261188Spfg func_type); 3776261188Spfg /* Synthesize function definition. */ 3777261188Spfg synth_block_byref_id_object_dispose_func (flag, kind); 3778261188Spfg } 3779261188Spfg initlist = tree_cons (fields, 3780261188Spfg build_fold_addr_expr (block_byref_id_object_dispose[kind]), 3781261188Spfg initlist); 3782261188Spfg fields = TREE_CHAIN (fields); 3783261188Spfg } 3784261188Spfg 3785261188Spfg if (init) 3786261188Spfg { 3787261188Spfg init = do_digest_init (TREE_TYPE (fields), init); 3788261188Spfg initlist = tree_cons (fields, init, initlist); 3789261188Spfg } 3790261188Spfg init = build_constructor_from_list (block_byref_type, nreverse (initlist)); 3791261188Spfg return init; 3792261188Spfg} 3793261188Spfg/* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ cr) */ 3794261188Spfg 379518334Speter/* Finish processing of a declaration; 379618334Speter install its initial value. 379718334Speter If the length of an array type is not known before, 379818334Speter it must be determined now, from the initial value, or it is an error. */ 379918334Speter 380018334Spetervoid 3801132730Skanfinish_decl (tree decl, tree init, tree asmspec_tree) 380218334Speter{ 3803169699Skan tree type; 380418334Speter int was_incomplete = (DECL_SIZE (decl) == 0); 380590075Sobrien const char *asmspec = 0; 380618334Speter 380790075Sobrien /* If a name was specified, get the string. */ 3808169699Skan if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL) 3809169699Skan && DECL_FILE_SCOPE_P (decl)) 381096263Sobrien asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); 381118334Speter if (asmspec_tree) 381218334Speter asmspec = TREE_STRING_POINTER (asmspec_tree); 381318334Speter 381418334Speter /* If `start_decl' didn't like having an initialization, ignore it now. */ 381518334Speter if (init != 0 && DECL_INITIAL (decl) == 0) 381618334Speter init = 0; 3817132730Skan 381818334Speter /* Don't crash if parm is initialized. */ 381918334Speter if (TREE_CODE (decl) == PARM_DECL) 382018334Speter init = 0; 3821261188Spfg /* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ cq) */ 3822261188Spfg /* We build a new type for each local variable declared as __block 3823261188Spfg and initialize it to a list of initializers. */ 3824261188Spfg else if (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl)) 3825261188Spfg { 3826261188Spfg if (DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 3827261188Spfg { 3828261188Spfg error ("__block attribute on %q+D not allowed, only allowed on local variables", decl); 3829261188Spfg COPYABLE_BYREF_LOCAL_VAR (decl) = 0; 3830261188Spfg COPYABLE_BYREF_LOCAL_NONPOD (decl) = 0; 3831261188Spfg } 3832261188Spfg else 3833261188Spfg { 3834261188Spfg int flag = 0; 3835261188Spfg if (objc_is_gcable_type (TREE_TYPE (decl)) == -1) 3836261188Spfg flag = BLOCK_FIELD_IS_WEAK; 3837261188Spfg if (block_requires_copying (decl)) 3838261188Spfg { 3839261188Spfg if (TREE_CODE (TREE_TYPE (decl)) == BLOCK_POINTER_TYPE) 3840261188Spfg flag |= BLOCK_FIELD_IS_BLOCK; 3841261188Spfg else 3842261188Spfg flag |= BLOCK_FIELD_IS_OBJECT; 3843261188Spfg } 3844261188Spfg decl = new_block_byref_decl (decl); 3845261188Spfg /* APPLE LOCAL begin radar 6289031 */ 3846261188Spfg#if 0 3847261188Spfg if (! flag_objc_gc_only) 3848261188Spfg#endif 3849261188Spfg { 3850261188Spfg push_cleanup (decl, build_block_byref_release_exp (decl), false); 3851261188Spfg } 3852261188Spfg /* APPLE LOCAL end radar 6289031 */ 3853261188Spfg /* APPLE LOCAL begin radar 5847976 */ 3854261188Spfg COPYABLE_WEAK_BLOCK (decl) = ((flag & BLOCK_FIELD_IS_WEAK) != 0); 3855261188Spfg init = init_byref_decl (decl, init, flag); 3856261188Spfg /* APPLE LOCAL end radar 5847976 */ 3857261188Spfg } 3858261188Spfg } 3859261188Spfg /* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ cq) */ 386018334Speter 386118334Speter if (init) 3862104761Skan store_init_value (decl, init); 386318334Speter 3864132730Skan if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL 3865169699Skan || TREE_CODE (decl) == FUNCTION_DECL 3866169699Skan || TREE_CODE (decl) == FIELD_DECL)) 3867132730Skan objc_check_decl (decl); 3868132730Skan 3869169699Skan type = TREE_TYPE (decl); 3870169699Skan 3871132730Skan /* Deduce size of array from initialization, if not already known. */ 387218334Speter if (TREE_CODE (type) == ARRAY_TYPE 387318334Speter && TYPE_DOMAIN (type) == 0 387418334Speter && TREE_CODE (decl) != TYPE_DECL) 387518334Speter { 3876169699Skan bool do_default 387718334Speter = (TREE_STATIC (decl) 387818334Speter /* Even if pedantic, an external linkage array 387918334Speter may have incomplete type at first. */ 388018334Speter ? pedantic && !TREE_PUBLIC (decl) 388118334Speter : !DECL_EXTERNAL (decl)); 388218334Speter int failure 3883169699Skan = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl), 3884169699Skan do_default); 388518334Speter 388618334Speter /* Get the completed type made by complete_array_type. */ 388718334Speter type = TREE_TYPE (decl); 388818334Speter 3889169699Skan switch (failure) 3890169699Skan { 3891169699Skan case 1: 3892169699Skan error ("initializer fails to determine size of %q+D", decl); 3893169699Skan break; 389418334Speter 3895169699Skan case 2: 389618334Speter if (do_default) 3897169699Skan error ("array size missing in %q+D", decl); 389818334Speter /* If a `static' var's size isn't known, 389918334Speter make it extern as well as static, so it does not get 390018334Speter allocated. 390118334Speter If it is not `static', then do not mark extern; 390218334Speter finish_incomplete_decl will give it a default size 390318334Speter and it will get allocated. */ 3904169699Skan else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) 390518334Speter DECL_EXTERNAL (decl) = 1; 3906169699Skan break; 3907169699Skan 3908169699Skan case 3: 3909169699Skan error ("zero or negative size array %q+D", decl); 3910169699Skan break; 3911169699Skan 3912169699Skan case 0: 3913169699Skan /* For global variables, update the copy of the type that 3914169699Skan exists in the binding. */ 3915169699Skan if (TREE_PUBLIC (decl)) 3916169699Skan { 3917169699Skan struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl)); 3918169699Skan while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext)) 3919169699Skan b_ext = b_ext->shadowed; 3920169699Skan if (b_ext) 3921169699Skan { 3922169699Skan if (b_ext->type) 3923169699Skan b_ext->type = composite_type (b_ext->type, type); 3924169699Skan else 3925169699Skan b_ext->type = type; 3926169699Skan } 3927169699Skan } 3928169699Skan break; 3929169699Skan 3930169699Skan default: 3931169699Skan gcc_unreachable (); 393218334Speter } 393318334Speter 3934169699Skan if (DECL_INITIAL (decl)) 3935169699Skan TREE_TYPE (DECL_INITIAL (decl)) = type; 393618334Speter 393718334Speter layout_decl (decl, 0); 393818334Speter } 393918334Speter 394018334Speter if (TREE_CODE (decl) == VAR_DECL) 394118334Speter { 3942169699Skan if (init && TREE_CODE (init) == CONSTRUCTOR) 3943169699Skan add_flexible_array_elts_to_size (decl, init); 3944169699Skan 394590075Sobrien if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node 394690075Sobrien && COMPLETE_TYPE_P (TREE_TYPE (decl))) 394718334Speter layout_decl (decl, 0); 394818334Speter 394918334Speter if (DECL_SIZE (decl) == 0 395090075Sobrien /* Don't give an error if we already gave one earlier. */ 395190075Sobrien && TREE_TYPE (decl) != error_mark_node 395218334Speter && (TREE_STATIC (decl) 3953169699Skan /* A static variable with an incomplete type 3954169699Skan is an error if it is initialized. 3955169699Skan Also if it is not file scope. 3956169699Skan Otherwise, let it through, but if it is not `extern' 3957169699Skan then it may cause an error message later. */ 3958169699Skan ? (DECL_INITIAL (decl) != 0 3959132730Skan || !DECL_FILE_SCOPE_P (decl)) 3960169699Skan /* An automatic variable with an incomplete type 3961169699Skan is an error. */ 3962169699Skan : !DECL_EXTERNAL (decl))) 3963169699Skan { 3964169699Skan error ("storage size of %q+D isn%'t known", decl); 3965169699Skan TREE_TYPE (decl) = error_mark_node; 3966169699Skan } 396718334Speter 396818334Speter if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 396918334Speter && DECL_SIZE (decl) != 0) 397018334Speter { 397118334Speter if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) 397218334Speter constant_expression_warning (DECL_SIZE (decl)); 397318334Speter else 3974169699Skan error ("storage size of %q+D isn%'t constant", decl); 397518334Speter } 397650397Sobrien 397790075Sobrien if (TREE_USED (type)) 397850397Sobrien TREE_USED (decl) = 1; 397918334Speter } 398018334Speter 3981132730Skan /* If this is a function and an assembler name is specified, reset DECL_RTL 3982132730Skan so we can give it its new name. Also, update built_in_decls if it 3983132730Skan was a normal built-in. */ 398418334Speter if (TREE_CODE (decl) == FUNCTION_DECL && asmspec) 398590075Sobrien { 3986132730Skan if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) 3987169699Skan set_builtin_user_assembler_name (decl, asmspec); 3988169699Skan set_user_assembler_name (decl, asmspec); 398990075Sobrien } 399018334Speter 3991132730Skan /* If #pragma weak was used, mark the decl weak now. */ 3992169699Skan maybe_apply_pragma_weak (decl); 3993132730Skan 399418334Speter /* Output the assembler code and/or RTL code for variables and functions, 399518334Speter unless the type is an undefined structure or union. 399618334Speter If not, it will get done when the type is completed. */ 399718334Speter 399818334Speter if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL) 399918334Speter { 4000169699Skan /* Determine the ELF visibility. */ 4001169699Skan if (TREE_PUBLIC (decl)) 4002169699Skan c_determine_visibility (decl); 4003169699Skan 4004117421Skan /* This is a no-op in c-lang.c or something real in objc-act.c. */ 4005132730Skan if (c_dialect_objc ()) 4006117421Skan objc_check_decl (decl); 400790075Sobrien 4008169699Skan if (asmspec) 4009169699Skan { 4010169699Skan /* If this is not a static variable, issue a warning. 4011169699Skan It doesn't make any sense to give an ASMSPEC for an 4012169699Skan ordinary, non-register local variable. Historically, 4013169699Skan GCC has accepted -- but ignored -- the ASMSPEC in 4014169699Skan this case. */ 4015169699Skan if (!DECL_FILE_SCOPE_P (decl) 4016169699Skan && TREE_CODE (decl) == VAR_DECL 4017169699Skan && !C_DECL_REGISTER (decl) 4018169699Skan && !TREE_STATIC (decl)) 4019169699Skan warning (0, "ignoring asm-specifier for non-static local " 4020169699Skan "variable %q+D", decl); 4021169699Skan else 4022169699Skan set_user_assembler_name (decl, asmspec); 4023169699Skan } 4024169699Skan 4025132730Skan if (DECL_FILE_SCOPE_P (decl)) 402618334Speter { 402790075Sobrien if (DECL_INITIAL (decl) == NULL_TREE 402890075Sobrien || DECL_INITIAL (decl) == error_mark_node) 402990075Sobrien /* Don't output anything 403090075Sobrien when a tentative file-scope definition is seen. 403190075Sobrien But at end of compilation, do output code for them. */ 403290075Sobrien DECL_DEFER_OUTPUT (decl) = 1; 4033169699Skan rest_of_decl_compilation (decl, true, 0); 403418334Speter } 403518334Speter else 403618334Speter { 4037169699Skan /* In conjunction with an ASMSPEC, the `register' 4038169699Skan keyword indicates that we should place the variable 4039169699Skan in a particular register. */ 4040169699Skan if (asmspec && C_DECL_REGISTER (decl)) 404190075Sobrien { 4042169699Skan DECL_HARD_REGISTER (decl) = 1; 4043169699Skan /* This cannot be done for a structure with volatile 4044169699Skan fields, on which DECL_REGISTER will have been 4045169699Skan reset. */ 4046169699Skan if (!DECL_REGISTER (decl)) 4047169699Skan error ("cannot put object with volatile field into register"); 404890075Sobrien } 404990075Sobrien 405090075Sobrien if (TREE_CODE (decl) != FUNCTION_DECL) 4051169699Skan { 4052169699Skan /* If we're building a variable sized type, and we might be 4053169699Skan reachable other than via the top of the current binding 4054169699Skan level, then create a new BIND_EXPR so that we deallocate 4055169699Skan the object at the right time. */ 4056169699Skan /* Note that DECL_SIZE can be null due to errors. */ 4057169699Skan if (DECL_SIZE (decl) 4058169699Skan && !TREE_CONSTANT (DECL_SIZE (decl)) 4059169699Skan && STATEMENT_LIST_HAS_LABEL (cur_stmt_list)) 4060169699Skan { 4061169699Skan tree bind; 4062169699Skan bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL); 4063169699Skan TREE_SIDE_EFFECTS (bind) = 1; 4064169699Skan add_stmt (bind); 4065169699Skan BIND_EXPR_BODY (bind) = push_stmt_list (); 4066169699Skan } 4067169699Skan add_stmt (build_stmt (DECL_EXPR, decl)); 4068169699Skan } 406918334Speter } 407090075Sobrien 4071169699Skan 4072132730Skan if (!DECL_FILE_SCOPE_P (decl)) 407318334Speter { 407418334Speter /* Recompute the RTL of a local array now 407518334Speter if it used to be an incomplete type. */ 407618334Speter if (was_incomplete 4077169699Skan && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) 407818334Speter { 407918334Speter /* If we used it already as memory, it must stay in memory. */ 408018334Speter TREE_ADDRESSABLE (decl) = TREE_USED (decl); 408118334Speter /* If it's still incomplete now, no init will save it. */ 408218334Speter if (DECL_SIZE (decl) == 0) 408318334Speter DECL_INITIAL (decl) = 0; 408418334Speter } 408518334Speter } 408618334Speter } 408718334Speter 4088132730Skan /* If this was marked 'used', be sure it will be output. */ 4089169699Skan if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl))) 4090169699Skan mark_decl_referenced (decl); 4091132730Skan 409218334Speter if (TREE_CODE (decl) == TYPE_DECL) 4093169699Skan { 4094169699Skan if (!DECL_FILE_SCOPE_P (decl) 4095169699Skan && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE)) 4096169699Skan add_stmt (build_stmt (DECL_EXPR, decl)); 409718334Speter 4098169699Skan rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0); 4099169699Skan } 4100169699Skan 410118334Speter /* At the end of a declaration, throw away any variable type sizes 410218334Speter of types defined inside that declaration. There is no use 410318334Speter computing them in the following function definition. */ 4104169699Skan if (current_scope == file_scope) 410518334Speter get_pending_sizes (); 410618334Speter 4107117421Skan /* Install a cleanup (aka destructor) if one was given. */ 4108117421Skan if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl)) 4109117421Skan { 4110117421Skan tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl)); 4111117421Skan if (attr) 4112117421Skan { 4113117421Skan tree cleanup_id = TREE_VALUE (TREE_VALUE (attr)); 4114117421Skan tree cleanup_decl = lookup_name (cleanup_id); 4115117421Skan tree cleanup; 4116117421Skan 4117117421Skan /* Build "cleanup(&decl)" for the destructor. */ 4118117421Skan cleanup = build_unary_op (ADDR_EXPR, decl, 0); 4119117421Skan cleanup = build_tree_list (NULL_TREE, cleanup); 4120117421Skan cleanup = build_function_call (cleanup_decl, cleanup); 4121117421Skan 4122117421Skan /* Don't warn about decl unused; the cleanup uses it. */ 4123117421Skan TREE_USED (decl) = 1; 4124169699Skan TREE_USED (cleanup_decl) = 1; 4125117421Skan 4126117421Skan /* Initialize EH, if we've been told to do so. */ 4127169699Skan c_maybe_initialize_eh (); 4128117421Skan 4129169699Skan push_cleanup (decl, cleanup, false); 4130117421Skan } 4131117421Skan } 413218334Speter} 413318334Speter 4134169699Skan/* Given a parsed parameter declaration, decode it into a PARM_DECL. */ 4135169699Skan 4136169699Skantree 4137169699Skangrokparm (const struct c_parm *parm) 4138169699Skan{ 4139169699Skan tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, 4140169699Skan NULL); 4141169699Skan 4142169699Skan decl_attributes (&decl, parm->attrs, 0); 4143169699Skan 4144169699Skan return decl; 4145169699Skan} 4146169699Skan 4147132730Skan/* Given a parsed parameter declaration, decode it into a PARM_DECL 4148132730Skan and push that on the current scope. */ 414918334Speter 415018334Spetervoid 4151169699Skanpush_parm_decl (const struct c_parm *parm) 415218334Speter{ 415318334Speter tree decl; 4154132730Skan 4155169699Skan decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL); 4156169699Skan decl_attributes (&decl, parm->attrs, 0); 415718334Speter 415818334Speter decl = pushdecl (decl); 415918334Speter 4160132730Skan finish_decl (decl, NULL_TREE, NULL_TREE); 416118334Speter} 416218334Speter 4163169699Skan/* Mark all the parameter declarations to date as forward decls. 4164132730Skan Also diagnose use of this extension. */ 416518334Speter 416618334Spetervoid 4167132730Skanmark_forward_parm_decls (void) 416818334Speter{ 4169169699Skan struct c_binding *b; 4170132730Skan 4171132730Skan if (pedantic && !current_scope->warned_forward_parm_decls) 4172132730Skan { 4173132730Skan pedwarn ("ISO C forbids forward parameter declarations"); 4174132730Skan current_scope->warned_forward_parm_decls = true; 4175132730Skan } 4176132730Skan 4177169699Skan for (b = current_scope->bindings; b; b = b->prev) 4178169699Skan if (TREE_CODE (b->decl) == PARM_DECL) 4179169699Skan TREE_ASM_WRITTEN (b->decl) = 1; 418018334Speter} 418118334Speter 418290075Sobrien/* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound 418390075Sobrien literal, which may be an incomplete array type completed by the 418490075Sobrien initializer; INIT is a CONSTRUCTOR that initializes the compound 418590075Sobrien literal. */ 418690075Sobrien 418790075Sobrientree 4188132730Skanbuild_compound_literal (tree type, tree init) 418990075Sobrien{ 419090075Sobrien /* We do not use start_decl here because we have a type, not a declarator; 419190075Sobrien and do not use finish_decl because the decl should be stored inside 4192169699Skan the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */ 4193169699Skan tree decl; 419490075Sobrien tree complit; 419590075Sobrien tree stmt; 4196169699Skan 4197169699Skan if (type == error_mark_node) 4198169699Skan return error_mark_node; 4199169699Skan 4200169699Skan decl = build_decl (VAR_DECL, NULL_TREE, type); 420190075Sobrien DECL_EXTERNAL (decl) = 0; 420290075Sobrien TREE_PUBLIC (decl) = 0; 4203169699Skan TREE_STATIC (decl) = (current_scope == file_scope); 420490075Sobrien DECL_CONTEXT (decl) = current_function_decl; 420590075Sobrien TREE_USED (decl) = 1; 420690075Sobrien TREE_TYPE (decl) = type; 4207169699Skan TREE_READONLY (decl) = TYPE_READONLY (type); 420890075Sobrien store_init_value (decl, init); 420990075Sobrien 421090075Sobrien if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type)) 421190075Sobrien { 4212169699Skan int failure = complete_array_type (&TREE_TYPE (decl), 4213169699Skan DECL_INITIAL (decl), true); 4214169699Skan gcc_assert (!failure); 4215169699Skan 4216169699Skan type = TREE_TYPE (decl); 4217169699Skan TREE_TYPE (DECL_INITIAL (decl)) = type; 421890075Sobrien } 421990075Sobrien 422090075Sobrien if (type == error_mark_node || !COMPLETE_TYPE_P (type)) 422190075Sobrien return error_mark_node; 422290075Sobrien 4223169699Skan stmt = build_stmt (DECL_EXPR, decl); 4224169699Skan complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt); 422590075Sobrien TREE_SIDE_EFFECTS (complit) = 1; 422690075Sobrien 422790075Sobrien layout_decl (decl, 0); 422890075Sobrien 422990075Sobrien if (TREE_STATIC (decl)) 423090075Sobrien { 4231169699Skan /* This decl needs a name for the assembler output. */ 4232169699Skan set_compound_literal_name (decl); 4233102790Skan DECL_DEFER_OUTPUT (decl) = 1; 4234102790Skan DECL_COMDAT (decl) = 1; 4235102790Skan DECL_ARTIFICIAL (decl) = 1; 4236169699Skan DECL_IGNORED_P (decl) = 1; 4237102790Skan pushdecl (decl); 4238169699Skan rest_of_decl_compilation (decl, 1, 0); 423990075Sobrien } 424090075Sobrien 424190075Sobrien return complit; 424290075Sobrien} 424390075Sobrien 4244117421Skan/* Determine whether TYPE is a structure with a flexible array member, 4245117421Skan or a union containing such a structure (possibly recursively). */ 4246117421Skan 4247117421Skanstatic bool 4248132730Skanflexible_array_type_p (tree type) 4249117421Skan{ 4250117421Skan tree x; 4251117421Skan switch (TREE_CODE (type)) 4252117421Skan { 4253117421Skan case RECORD_TYPE: 4254117421Skan x = TYPE_FIELDS (type); 4255117421Skan if (x == NULL_TREE) 4256117421Skan return false; 4257117421Skan while (TREE_CHAIN (x) != NULL_TREE) 4258117421Skan x = TREE_CHAIN (x); 4259117421Skan if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 4260117421Skan && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 4261117421Skan && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 4262117421Skan && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 4263117421Skan return true; 4264117421Skan return false; 4265117421Skan case UNION_TYPE: 4266117421Skan for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x)) 4267117421Skan { 4268117421Skan if (flexible_array_type_p (TREE_TYPE (x))) 4269117421Skan return true; 4270117421Skan } 4271117421Skan return false; 4272117421Skan default: 4273117421Skan return false; 4274117421Skan } 4275117421Skan} 4276117421Skan 4277132730Skan/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME, 4278132730Skan replacing with appropriate values if they are invalid. */ 4279132730Skanstatic void 4280132730Skancheck_bitfield_type_and_width (tree *type, tree *width, const char *orig_name) 4281132730Skan{ 4282132730Skan tree type_mv; 4283132730Skan unsigned int max_width; 4284132730Skan unsigned HOST_WIDE_INT w; 4285132730Skan const char *name = orig_name ? orig_name: _("<anonymous>"); 4286132730Skan 4287132730Skan /* Detect and ignore out of range field width and process valid 4288132730Skan field widths. */ 4289169699Skan if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)) 4290169699Skan || TREE_CODE (*width) != INTEGER_CST) 4291132730Skan { 4292169699Skan error ("bit-field %qs width not an integer constant", name); 4293132730Skan *width = integer_one_node; 4294132730Skan } 4295132730Skan else 4296132730Skan { 4297132730Skan constant_expression_warning (*width); 4298132730Skan if (tree_int_cst_sgn (*width) < 0) 4299132730Skan { 4300169699Skan error ("negative width in bit-field %qs", name); 4301132730Skan *width = integer_one_node; 4302132730Skan } 4303132730Skan else if (integer_zerop (*width) && orig_name) 4304132730Skan { 4305169699Skan error ("zero width for bit-field %qs", name); 4306132730Skan *width = integer_one_node; 4307132730Skan } 4308132730Skan } 4309132730Skan 4310132730Skan /* Detect invalid bit-field type. */ 4311132730Skan if (TREE_CODE (*type) != INTEGER_TYPE 4312132730Skan && TREE_CODE (*type) != BOOLEAN_TYPE 4313132730Skan && TREE_CODE (*type) != ENUMERAL_TYPE) 4314132730Skan { 4315169699Skan error ("bit-field %qs has invalid type", name); 4316132730Skan *type = unsigned_type_node; 4317132730Skan } 4318132730Skan 4319132730Skan type_mv = TYPE_MAIN_VARIANT (*type); 4320132730Skan if (pedantic 4321169699Skan && !in_system_header 4322132730Skan && type_mv != integer_type_node 4323132730Skan && type_mv != unsigned_type_node 4324132730Skan && type_mv != boolean_type_node) 4325169699Skan pedwarn ("type of bit-field %qs is a GCC extension", name); 4326132730Skan 4327132730Skan if (type_mv == boolean_type_node) 4328132730Skan max_width = CHAR_TYPE_SIZE; 4329132730Skan else 4330132730Skan max_width = TYPE_PRECISION (*type); 4331132730Skan 4332132730Skan if (0 < compare_tree_int (*width, max_width)) 4333132730Skan { 4334169699Skan error ("width of %qs exceeds its type", name); 4335132730Skan w = max_width; 4336169699Skan *width = build_int_cst (NULL_TREE, w); 4337132730Skan } 4338132730Skan else 4339132730Skan w = tree_low_cst (*width, 1); 4340132730Skan 4341169699Skan if (TREE_CODE (*type) == ENUMERAL_TYPE) 4342169699Skan { 4343169699Skan struct lang_type *lt = TYPE_LANG_SPECIFIC (*type); 4344169699Skan if (!lt 4345169699Skan || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type)) 4346169699Skan || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type))) 4347169699Skan warning (0, "%qs is narrower than values of its type", name); 4348169699Skan } 4349132730Skan} 4350169699Skan 4351132730Skan 4352259268Spfg 4353259268Spfg/* Print warning about variable length array if necessary. */ 4354259268Spfg 4355259268Spfgstatic void 4356259268Spfgwarn_variable_length_array (const char *name, tree size) 4357259268Spfg{ 4358259268Spfg int ped = !flag_isoc99 && pedantic && warn_vla != 0; 4359259268Spfg int const_size = TREE_CONSTANT (size); 4360259268Spfg 4361259268Spfg if (ped) 4362259268Spfg { 4363259268Spfg if (const_size) 4364259268Spfg { 4365259268Spfg if (name) 4366259268Spfg pedwarn ("ISO C90 forbids array %qs whose size " 4367259268Spfg "can%'t be evaluated", 4368259268Spfg name); 4369259268Spfg else 4370259268Spfg pedwarn ("ISO C90 forbids array whose size " 4371259268Spfg "can%'t be evaluated"); 4372259268Spfg } 4373259268Spfg else 4374259268Spfg { 4375259268Spfg if (name) 4376259268Spfg pedwarn ("ISO C90 forbids variable length array %qs", 4377259268Spfg name); 4378259268Spfg else 4379259268Spfg pedwarn ("ISO C90 forbids variable length array"); 4380259268Spfg } 4381259268Spfg } 4382259268Spfg else if (warn_vla > 0) 4383259268Spfg { 4384259268Spfg if (const_size) 4385259268Spfg { 4386259268Spfg if (name) 4387259268Spfg warning (OPT_Wvla, 4388259268Spfg "the size of array %qs can" 4389259268Spfg "%'t be evaluated", name); 4390259268Spfg else 4391259268Spfg warning (OPT_Wvla, 4392259268Spfg "the size of array can %'t be evaluated"); 4393259268Spfg } 4394259268Spfg else 4395259268Spfg { 4396259268Spfg if (name) 4397259268Spfg warning (OPT_Wvla, 4398259268Spfg "variable length array %qs is used", 4399259268Spfg name); 4400259268Spfg else 4401259268Spfg warning (OPT_Wvla, 4402259268Spfg "variable length array is used"); 4403259268Spfg } 4404259268Spfg } 4405259268Spfg} 4406259268Spfg 440718334Speter/* Given declspecs and a declarator, 440818334Speter determine the name and type of the object declared 440918334Speter and construct a ..._DECL node for it. 441018334Speter (In one case we can return a ..._TYPE node instead. 441118334Speter For invalid input we sometimes return 0.) 441218334Speter 4413169699Skan DECLSPECS is a c_declspecs structure for the declaration specifiers. 441418334Speter 441518334Speter DECL_CONTEXT says which syntactic context this declaration is in: 441618334Speter NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. 441718334Speter FUNCDEF for a function definition. Like NORMAL but a few different 441818334Speter error messages in each case. Return value may be zero meaning 441918334Speter this definition is too screwy to try to parse. 442018334Speter PARM for a parameter declaration (either within a function prototype 442118334Speter or before a function body). Make a PARM_DECL, or return void_type_node. 442218334Speter TYPENAME if for a typename (in a cast or sizeof). 442318334Speter Don't make a DECL node; just return the ..._TYPE node. 442418334Speter FIELD for a struct or union field; make a FIELD_DECL. 4425169699Skan INITIALIZED is true if the decl has an initializer. 4426132730Skan WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node 4427132730Skan representing the width of the bit-field. 442818334Speter 442918334Speter In the TYPENAME case, DECLARATOR is really an absolute declarator. 443018334Speter It may also be so in the PARM case, for a prototype where the 443118334Speter argument type is specified but not the name. 443218334Speter 443318334Speter This function is where the complicated C meanings of `static' 443418334Speter and `extern' are interpreted. */ 443518334Speter 443618334Speterstatic tree 4437169699Skangrokdeclarator (const struct c_declarator *declarator, 4438169699Skan struct c_declspecs *declspecs, 4439169699Skan enum decl_context decl_context, bool initialized, tree *width) 444018334Speter{ 4441169699Skan tree type = declspecs->type; 4442169699Skan bool threadp = declspecs->thread_p; 4443169699Skan enum c_storage_class storage_class = declspecs->storage_class; 444418334Speter int constp; 444552284Sobrien int restrictp; 444618334Speter int volatilep; 444752284Sobrien int type_quals = TYPE_UNQUALIFIED; 4448132730Skan const char *name, *orig_name; 444918334Speter tree typedef_type = 0; 4450169699Skan bool funcdef_flag = false; 4451169699Skan bool funcdef_syntax = false; 445218334Speter int size_varies = 0; 4453169699Skan tree decl_attr = declspecs->decl_attr; 4454169699Skan int array_ptr_quals = TYPE_UNQUALIFIED; 4455169699Skan tree array_ptr_attrs = NULL_TREE; 445690075Sobrien int array_parm_static = 0; 4457169699Skan bool array_parm_vla_unspec_p = false; 445890075Sobrien tree returned_attrs = NULL_TREE; 4459132730Skan bool bitfield = width != NULL; 4460132730Skan tree element_type; 4461169699Skan struct c_arg_info *arg_info = 0; 446218334Speter 446318334Speter if (decl_context == FUNCDEF) 4464169699Skan funcdef_flag = true, decl_context = NORMAL; 446518334Speter 446618334Speter /* Look inside a declarator for the name being declared 446718334Speter and get it as a string, for an error message. */ 446818334Speter { 4469169699Skan const struct c_declarator *decl = declarator; 447018334Speter name = 0; 447118334Speter 447218334Speter while (decl) 4473169699Skan switch (decl->kind) 447418334Speter { 4475169699Skan case cdk_function: 4476169699Skan case cdk_array: 4477169699Skan case cdk_pointer: 4478261188Spfg /* APPLE LOCAL radar 5732232 - blocks */ 4479261188Spfg case cdk_block_pointer: 4480169699Skan funcdef_syntax = (decl->kind == cdk_function); 4481169699Skan decl = decl->declarator; 448218334Speter break; 448318334Speter 4484169699Skan case cdk_attrs: 4485169699Skan decl = decl->declarator; 448690075Sobrien break; 448790075Sobrien 4488169699Skan case cdk_id: 4489169699Skan if (decl->u.id) 4490169699Skan name = IDENTIFIER_POINTER (decl->u.id); 449118334Speter decl = 0; 449218334Speter break; 449318334Speter 449418334Speter default: 4495169699Skan gcc_unreachable (); 449618334Speter } 4497132730Skan orig_name = name; 449818334Speter if (name == 0) 449918334Speter name = "type name"; 450018334Speter } 450118334Speter 450218334Speter /* A function definition's declarator must have the form of 450318334Speter a function declarator. */ 450418334Speter 4505169699Skan if (funcdef_flag && !funcdef_syntax) 450618334Speter return 0; 450718334Speter 450818334Speter /* If this looks like a function definition, make it one, 450918334Speter even if it occurs where parms are expected. 451018334Speter Then store_parm_decls will reject it and not use it as a parm. */ 4511260918Spfg /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */ 4512260918Spfg if (declspecs->unavailable_p) 4513260918Spfg error_unavailable_use (declspecs->type); 4514260918Spfg else 4515260918Spfg /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */ 4516169699Skan if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag) 451718334Speter decl_context = PARM; 451818334Speter 4519169699Skan if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS) 4520169699Skan warn_deprecated_use (declspecs->type); 452118334Speter 4522169699Skan if ((decl_context == NORMAL || decl_context == FIELD) 4523169699Skan && current_scope == file_scope 4524169699Skan && variably_modified_type_p (type, NULL_TREE)) 452518334Speter { 4526169699Skan error ("variably modified %qs at file scope", name); 4527169699Skan type = integer_type_node; 452818334Speter } 452918334Speter 453018334Speter typedef_type = type; 4531169699Skan size_varies = C_TYPE_VARIABLE_SIZE (type); 453218334Speter 4533169699Skan /* Diagnose defaulting to "int". */ 453418334Speter 4535169699Skan if (declspecs->default_int_p && !in_system_header) 453618334Speter { 4537169699Skan /* Issue a warning if this is an ISO C 99 program or if 4538169699Skan -Wreturn-type and this is a function, or if -Wimplicit; 4539169699Skan prefer the former warning since it is more explicit. */ 4540169699Skan if ((warn_implicit_int || warn_return_type || flag_isoc99) 4541169699Skan && funcdef_flag) 4542169699Skan warn_about_return_type = 1; 4543169699Skan else if (warn_implicit_int || flag_isoc99) 4544169699Skan pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name); 454518334Speter } 454618334Speter 4547169699Skan /* Adjust the type if a bit-field is being declared, 4548169699Skan -funsigned-bitfields applied and the type is not explicitly 4549169699Skan "signed". */ 4550169699Skan if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p 4551169699Skan && TREE_CODE (type) == INTEGER_TYPE) 4552169699Skan type = c_common_unsigned_type (type); 455318334Speter 455452284Sobrien /* Figure out the type qualifiers for the declaration. There are 455552284Sobrien two ways a declaration can become qualified. One is something 455652284Sobrien like `const int i' where the `const' is explicit. Another is 455752284Sobrien something like `typedef const int CI; CI i' where the type of the 4558132730Skan declaration contains the `const'. A third possibility is that 4559132730Skan there is a type qualifier on the element type of a typedefed 4560132730Skan array type, in which case we should extract that qualifier so 4561132730Skan that c_apply_type_quals_to_decls receives the full list of 4562132730Skan qualifiers to work with (C90 is not entirely clear about whether 4563132730Skan duplicate qualifiers should be diagnosed in this case, but it 4564132730Skan seems most appropriate to do so). */ 4565132730Skan element_type = strip_array_types (type); 4566169699Skan constp = declspecs->const_p + TYPE_READONLY (element_type); 4567169699Skan restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type); 4568169699Skan volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type); 4569132730Skan if (pedantic && !flag_isoc99) 4570132730Skan { 4571132730Skan if (constp > 1) 4572169699Skan pedwarn ("duplicate %<const%>"); 4573132730Skan if (restrictp > 1) 4574169699Skan pedwarn ("duplicate %<restrict%>"); 4575132730Skan if (volatilep > 1) 4576169699Skan pedwarn ("duplicate %<volatile%>"); 4577132730Skan } 4578169699Skan if (!flag_gen_aux_info && (TYPE_QUALS (element_type))) 457918334Speter type = TYPE_MAIN_VARIANT (type); 458052284Sobrien type_quals = ((constp ? TYPE_QUAL_CONST : 0) 458152284Sobrien | (restrictp ? TYPE_QUAL_RESTRICT : 0) 458252284Sobrien | (volatilep ? TYPE_QUAL_VOLATILE : 0)); 458318334Speter 4584169699Skan /* Warn about storage classes that are invalid for certain 4585169699Skan kinds of declarations (parameters, typenames, etc.). */ 458618334Speter 4587169699Skan if (funcdef_flag 4588169699Skan && (threadp 4589169699Skan || storage_class == csc_auto 4590169699Skan || storage_class == csc_register 4591169699Skan || storage_class == csc_typedef)) 4592169699Skan { 4593169699Skan if (storage_class == csc_auto 4594169699Skan && (pedantic || current_scope == file_scope)) 4595169699Skan pedwarn ("function definition declared %<auto%>"); 4596169699Skan if (storage_class == csc_register) 4597169699Skan error ("function definition declared %<register%>"); 4598169699Skan if (storage_class == csc_typedef) 4599169699Skan error ("function definition declared %<typedef%>"); 4600169699Skan if (threadp) 4601169699Skan error ("function definition declared %<__thread%>"); 4602169699Skan threadp = false; 4603169699Skan if (storage_class == csc_auto 4604169699Skan || storage_class == csc_register 4605169699Skan || storage_class == csc_typedef) 4606169699Skan storage_class = csc_none; 4607169699Skan } 4608169699Skan else if (decl_context != NORMAL && (storage_class != csc_none || threadp)) 4609169699Skan { 4610169699Skan if (decl_context == PARM && storage_class == csc_register) 4611169699Skan ; 4612169699Skan else 4613169699Skan { 4614169699Skan switch (decl_context) 4615169699Skan { 4616169699Skan case FIELD: 4617169699Skan error ("storage class specified for structure field %qs", 4618169699Skan name); 4619169699Skan break; 4620169699Skan case PARM: 4621169699Skan error ("storage class specified for parameter %qs", name); 4622169699Skan break; 4623169699Skan default: 4624169699Skan error ("storage class specified for typename"); 4625169699Skan break; 4626169699Skan } 4627169699Skan storage_class = csc_none; 4628169699Skan threadp = false; 4629169699Skan } 4630169699Skan } 4631169699Skan else if (storage_class == csc_extern 4632169699Skan && initialized 4633169699Skan && !funcdef_flag) 4634169699Skan { 4635169699Skan /* 'extern' with initialization is invalid if not at file scope. */ 4636169699Skan if (current_scope == file_scope) 4637169699Skan { 4638169699Skan /* It is fine to have 'extern const' when compiling at C 4639169699Skan and C++ intersection. */ 4640169699Skan if (!(warn_cxx_compat && constp)) 4641169699Skan warning (0, "%qs initialized and declared %<extern%>", name); 4642169699Skan } 4643169699Skan else 4644169699Skan error ("%qs has both %<extern%> and initializer", name); 4645169699Skan } 4646169699Skan else if (current_scope == file_scope) 4647169699Skan { 4648169699Skan if (storage_class == csc_auto) 4649169699Skan error ("file-scope declaration of %qs specifies %<auto%>", name); 4650169699Skan if (pedantic && storage_class == csc_register) 4651169699Skan pedwarn ("file-scope declaration of %qs specifies %<register%>", name); 4652169699Skan } 4653169699Skan else 4654169699Skan { 4655169699Skan if (storage_class == csc_extern && funcdef_flag) 4656169699Skan error ("nested function %qs declared %<extern%>", name); 4657169699Skan else if (threadp && storage_class == csc_none) 4658169699Skan { 4659169699Skan error ("function-scope %qs implicitly auto and declared " 4660169699Skan "%<__thread%>", 4661169699Skan name); 4662169699Skan threadp = false; 4663169699Skan } 4664169699Skan } 466518334Speter 466618334Speter /* Now figure out the structure of the declarator proper. 466718334Speter Descend through it, creating more complex types, until we reach 4668169699Skan the declared identifier (or NULL_TREE, in an absolute declarator). 4669169699Skan At each stage we maintain an unqualified version of the type 4670169699Skan together with any qualifiers that should be applied to it with 4671169699Skan c_build_qualified_type; this way, array types including 4672169699Skan multidimensional array types are first built up in unqualified 4673169699Skan form and then the qualified form is created with 4674169699Skan TYPE_MAIN_VARIANT pointing to the unqualified form. */ 467518334Speter 4676169699Skan while (declarator && declarator->kind != cdk_id) 467718334Speter { 467818334Speter if (type == error_mark_node) 467918334Speter { 4680169699Skan declarator = declarator->declarator; 468118334Speter continue; 468218334Speter } 468318334Speter 4684169699Skan /* Each level of DECLARATOR is either a cdk_array (for ...[..]), 4685169699Skan a cdk_pointer (for *...), 4686169699Skan a cdk_function (for ...(...)), 4687169699Skan a cdk_attrs (for nested attributes), 4688169699Skan or a cdk_id (for the name being declared 4689169699Skan or the place in an absolute declarator 469018334Speter where the name was omitted). 4691169699Skan For the last case, we have just exited the loop. 469218334Speter 469318334Speter At this point, TYPE is the type of elements of an array, 469418334Speter or for a function to return, or for a pointer to point to. 469518334Speter After this sequence of ifs, TYPE is the type of the 469618334Speter array or function or pointer, and DECLARATOR has had its 469718334Speter outermost layer removed. */ 469818334Speter 4699169699Skan if (array_ptr_quals != TYPE_UNQUALIFIED 4700169699Skan || array_ptr_attrs != NULL_TREE 4701169699Skan || array_parm_static) 470218334Speter { 470390075Sobrien /* Only the innermost declarator (making a parameter be of 470490075Sobrien array type which is converted to pointer type) 470590075Sobrien may have static or type qualifiers. */ 470690075Sobrien error ("static or type qualifiers in non-parameter array declarator"); 4707169699Skan array_ptr_quals = TYPE_UNQUALIFIED; 4708169699Skan array_ptr_attrs = NULL_TREE; 470990075Sobrien array_parm_static = 0; 471090075Sobrien } 471190075Sobrien 4712169699Skan switch (declarator->kind) 471390075Sobrien { 4714169699Skan case cdk_attrs: 4715169699Skan { 4716169699Skan /* A declarator with embedded attributes. */ 4717169699Skan tree attrs = declarator->u.attrs; 4718169699Skan const struct c_declarator *inner_decl; 4719169699Skan int attr_flags = 0; 4720169699Skan declarator = declarator->declarator; 4721169699Skan inner_decl = declarator; 4722169699Skan while (inner_decl->kind == cdk_attrs) 4723169699Skan inner_decl = inner_decl->declarator; 4724169699Skan if (inner_decl->kind == cdk_id) 4725169699Skan attr_flags |= (int) ATTR_FLAG_DECL_NEXT; 4726169699Skan else if (inner_decl->kind == cdk_function) 4727169699Skan attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; 4728169699Skan else if (inner_decl->kind == cdk_array) 4729169699Skan attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; 4730169699Skan returned_attrs = decl_attributes (&type, 4731169699Skan chainon (returned_attrs, attrs), 4732169699Skan attr_flags); 4733169699Skan break; 4734169699Skan } 4735169699Skan case cdk_array: 4736169699Skan { 4737169699Skan tree itype = NULL_TREE; 4738169699Skan tree size = declarator->u.array.dimen; 4739169699Skan /* The index is a signed object `sizetype' bits wide. */ 4740169699Skan tree index_type = c_common_signed_type (sizetype); 474118334Speter 4742169699Skan array_ptr_quals = declarator->u.array.quals; 4743169699Skan array_ptr_attrs = declarator->u.array.attrs; 4744169699Skan array_parm_static = declarator->u.array.static_p; 4745169699Skan array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p; 474690075Sobrien 4747169699Skan declarator = declarator->declarator; 474818334Speter 4749169699Skan /* Check for some types that there cannot be arrays of. */ 475018334Speter 4751169699Skan if (VOID_TYPE_P (type)) 4752169699Skan { 4753169699Skan error ("declaration of %qs as array of voids", name); 4754169699Skan type = error_mark_node; 4755169699Skan } 475618334Speter 4757169699Skan if (TREE_CODE (type) == FUNCTION_TYPE) 4758169699Skan { 4759169699Skan error ("declaration of %qs as array of functions", name); 4760169699Skan type = error_mark_node; 4761169699Skan } 4762169699Skan 4763169699Skan if (pedantic && !in_system_header && flexible_array_type_p (type)) 4764169699Skan pedwarn ("invalid use of structure with flexible array member"); 4765169699Skan 4766169699Skan if (size == error_mark_node) 476718334Speter type = error_mark_node; 476818334Speter 4769169699Skan if (type == error_mark_node) 4770169699Skan continue; 4771117421Skan 4772169699Skan /* If size was specified, set ITYPE to a range-type for 4773169699Skan that size. Otherwise, ITYPE remains null. finish_decl 4774169699Skan may figure it out from an initial value. */ 477518334Speter 4776169699Skan if (size) 4777169699Skan { 4778169699Skan /* Strip NON_LVALUE_EXPRs since we aren't using as an 4779169699Skan lvalue. */ 4780169699Skan STRIP_TYPE_NOPS (size); 478118334Speter 4782169699Skan if (!INTEGRAL_TYPE_P (TREE_TYPE (size))) 4783169699Skan { 4784169699Skan error ("size of array %qs has non-integer type", name); 4785169699Skan size = integer_one_node; 4786169699Skan } 478718334Speter 4788169699Skan if (pedantic && integer_zerop (size)) 4789169699Skan pedwarn ("ISO C forbids zero-size array %qs", name); 479018334Speter 4791169699Skan if (TREE_CODE (size) == INTEGER_CST) 4792169699Skan { 4793169699Skan constant_expression_warning (size); 4794169699Skan if (tree_int_cst_sgn (size) < 0) 4795169699Skan { 4796169699Skan error ("size of array %qs is negative", name); 4797169699Skan size = integer_one_node; 4798169699Skan } 4799169699Skan } 4800169699Skan else if ((decl_context == NORMAL || decl_context == FIELD) 4801169699Skan && current_scope == file_scope) 4802169699Skan { 4803169699Skan error ("variably modified %qs at file scope", name); 4804169699Skan size = integer_one_node; 4805169699Skan } 4806169699Skan else 4807169699Skan { 4808169699Skan /* Make sure the array size remains visibly 4809169699Skan nonconstant even if it is (eg) a const variable 4810169699Skan with known value. */ 4811169699Skan size_varies = 1; 4812259268Spfg warn_variable_length_array (orig_name, size); 4813223715Suqs if (warn_variable_decl) 4814223715Suqs warning (0, "variable-sized array %qs", name); 4815169699Skan } 481618334Speter 4817169699Skan if (integer_zerop (size)) 4818169699Skan { 4819169699Skan /* A zero-length array cannot be represented with 4820169699Skan an unsigned index type, which is what we'll 4821169699Skan get with build_index_type. Create an 4822169699Skan open-ended range instead. */ 4823169699Skan itype = build_range_type (sizetype, size, NULL_TREE); 4824169699Skan } 4825169699Skan else 4826169699Skan { 4827169699Skan /* Arrange for the SAVE_EXPR on the inside of the 4828169699Skan MINUS_EXPR, which allows the -1 to get folded 4829169699Skan with the +1 that happens when building TYPE_SIZE. */ 4830169699Skan if (size_varies) 4831169699Skan size = variable_size (size); 483218334Speter 4833169699Skan /* Compute the maximum valid index, that is, size 4834169699Skan - 1. Do the calculation in index_type, so that 4835169699Skan if it is a variable the computations will be 4836169699Skan done in the proper mode. */ 4837169699Skan itype = fold_build2 (MINUS_EXPR, index_type, 4838169699Skan convert (index_type, size), 4839169699Skan convert (index_type, 4840169699Skan size_one_node)); 484118334Speter 4842169699Skan /* If that overflowed, the array is too big. ??? 4843169699Skan While a size of INT_MAX+1 technically shouldn't 4844169699Skan cause an overflow (because we subtract 1), the 4845169699Skan overflow is recorded during the conversion to 4846169699Skan index_type, before the subtraction. Handling 4847169699Skan this case seems like an unnecessary 4848169699Skan complication. */ 4849169699Skan if (TREE_CODE (itype) == INTEGER_CST 4850169699Skan && TREE_OVERFLOW (itype)) 4851169699Skan { 4852169699Skan error ("size of array %qs is too large", name); 4853169699Skan type = error_mark_node; 4854169699Skan continue; 4855169699Skan } 485650397Sobrien 4857169699Skan itype = build_index_type (itype); 4858169699Skan } 4859169699Skan } 4860169699Skan else if (decl_context == FIELD) 4861169699Skan { 4862169699Skan if (pedantic && !flag_isoc99 && !in_system_header) 4863169699Skan pedwarn ("ISO C90 does not support flexible array members"); 486490075Sobrien 4865169699Skan /* ISO C99 Flexible array members are effectively 4866169699Skan identical to GCC's zero-length array extension. */ 4867169699Skan itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 4868169699Skan } 4869169699Skan else if (decl_context == PARM) 4870169699Skan { 4871169699Skan if (array_parm_vla_unspec_p) 4872169699Skan { 4873169699Skan if (! orig_name) 4874169699Skan { 4875169699Skan /* C99 6.7.5.2p4 */ 4876169699Skan error ("%<[*]%> not allowed in other than a declaration"); 4877169699Skan } 487818334Speter 4879169699Skan itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 4880169699Skan size_varies = 1; 4881169699Skan } 4882169699Skan } 4883169699Skan else if (decl_context == TYPENAME) 4884169699Skan { 4885169699Skan if (array_parm_vla_unspec_p) 4886169699Skan { 4887169699Skan /* The error is printed elsewhere. We use this to 4888169699Skan avoid messing up with incomplete array types of 4889169699Skan the same type, that would otherwise be modified 4890169699Skan below. */ 4891169699Skan itype = build_range_type (sizetype, size_zero_node, 4892169699Skan NULL_TREE); 4893169699Skan } 4894169699Skan } 489518334Speter 4896169699Skan /* Complain about arrays of incomplete types. */ 4897169699Skan if (!COMPLETE_TYPE_P (type)) 4898169699Skan { 4899169699Skan error ("array type has incomplete element type"); 4900169699Skan type = error_mark_node; 4901169699Skan } 4902169699Skan else 4903169699Skan /* When itype is NULL, a shared incomplete array type is 4904169699Skan returned for all array of a given type. Elsewhere we 4905169699Skan make sure we don't complete that type before copying 4906169699Skan it, but here we want to make sure we don't ever 4907169699Skan modify the shared type, so we gcc_assert (itype) 4908169699Skan below. */ 4909169699Skan type = build_array_type (type, itype); 491090075Sobrien 4911169699Skan if (type != error_mark_node) 4912169699Skan { 4913169699Skan if (size_varies) 4914169699Skan { 4915169699Skan /* It is ok to modify type here even if itype is 4916169699Skan NULL: if size_varies, we're in a 4917169699Skan multi-dimensional array and the inner type has 4918169699Skan variable size, so the enclosing shared array type 4919169699Skan must too. */ 4920169699Skan if (size && TREE_CODE (size) == INTEGER_CST) 4921169699Skan type 4922169699Skan = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); 4923169699Skan C_TYPE_VARIABLE_SIZE (type) = 1; 4924169699Skan } 492590075Sobrien 4926169699Skan /* The GCC extension for zero-length arrays differs from 4927169699Skan ISO flexible array members in that sizeof yields 4928169699Skan zero. */ 4929169699Skan if (size && integer_zerop (size)) 4930169699Skan { 4931169699Skan gcc_assert (itype); 4932169699Skan TYPE_SIZE (type) = bitsize_zero_node; 4933169699Skan TYPE_SIZE_UNIT (type) = size_zero_node; 4934169699Skan } 4935169699Skan if (array_parm_vla_unspec_p) 4936169699Skan { 4937169699Skan gcc_assert (itype); 4938169699Skan /* The type is complete. C99 6.7.5.2p4 */ 4939169699Skan TYPE_SIZE (type) = bitsize_zero_node; 4940169699Skan TYPE_SIZE_UNIT (type) = size_zero_node; 4941169699Skan } 4942169699Skan } 494318334Speter 4944169699Skan if (decl_context != PARM 4945169699Skan && (array_ptr_quals != TYPE_UNQUALIFIED 4946169699Skan || array_ptr_attrs != NULL_TREE 4947169699Skan || array_parm_static)) 4948169699Skan { 4949169699Skan error ("static or type qualifiers in non-parameter array declarator"); 4950169699Skan array_ptr_quals = TYPE_UNQUALIFIED; 4951169699Skan array_ptr_attrs = NULL_TREE; 4952169699Skan array_parm_static = 0; 4953169699Skan } 4954169699Skan break; 4955169699Skan } 4956169699Skan case cdk_function: 4957169699Skan { 4958169699Skan /* Say it's a definition only for the declarator closest 4959169699Skan to the identifier, apart possibly from some 4960169699Skan attributes. */ 4961169699Skan bool really_funcdef = false; 4962169699Skan tree arg_types; 4963169699Skan if (funcdef_flag) 4964169699Skan { 4965169699Skan const struct c_declarator *t = declarator->declarator; 4966169699Skan while (t->kind == cdk_attrs) 4967169699Skan t = t->declarator; 4968169699Skan really_funcdef = (t->kind == cdk_id); 4969169699Skan } 497018334Speter 4971169699Skan /* Declaring a function type. Make sure we have a valid 4972169699Skan type for the function to return. */ 4973169699Skan if (type == error_mark_node) 4974169699Skan continue; 497590075Sobrien 4976169699Skan size_varies = 0; 497718334Speter 4978169699Skan /* Warn about some types functions can't return. */ 4979169699Skan if (TREE_CODE (type) == FUNCTION_TYPE) 4980169699Skan { 4981169699Skan error ("%qs declared as function returning a function", name); 4982169699Skan type = integer_type_node; 4983169699Skan } 4984169699Skan if (TREE_CODE (type) == ARRAY_TYPE) 4985169699Skan { 4986169699Skan error ("%qs declared as function returning an array", name); 4987169699Skan type = integer_type_node; 4988169699Skan } 498918334Speter 4990169699Skan /* Construct the function type and go to the next 4991169699Skan inner layer of declarator. */ 4992169699Skan arg_info = declarator->u.arg_info; 4993169699Skan arg_types = grokparms (arg_info, really_funcdef); 4994169699Skan if (really_funcdef) 4995169699Skan put_pending_sizes (arg_info->pending_sizes); 499618334Speter 4997169699Skan /* Type qualifiers before the return type of the function 4998169699Skan qualify the return type, not the function type. */ 4999169699Skan if (type_quals) 5000169699Skan { 5001169699Skan /* Type qualifiers on a function return type are 5002169699Skan normally permitted by the standard but have no 5003169699Skan effect, so give a warning at -Wreturn-type. 5004169699Skan Qualifiers on a void return type are banned on 5005169699Skan function definitions in ISO C; GCC used to used 5006169699Skan them for noreturn functions. */ 5007169699Skan if (VOID_TYPE_P (type) && really_funcdef) 5008169699Skan pedwarn ("function definition has qualified void return type"); 5009169699Skan else 5010169699Skan warning (OPT_Wreturn_type, 5011169699Skan "type qualifiers ignored on function return type"); 501218334Speter 5013169699Skan type = c_build_qualified_type (type, type_quals); 5014169699Skan } 5015169699Skan type_quals = TYPE_UNQUALIFIED; 501618334Speter 5017169699Skan type = build_function_type (type, arg_types); 5018169699Skan declarator = declarator->declarator; 501918334Speter 5020169699Skan /* Set the TYPE_CONTEXTs for each tagged type which is local to 5021169699Skan the formal parameter list of this FUNCTION_TYPE to point to 5022169699Skan the FUNCTION_TYPE node itself. */ 502390075Sobrien { 5024169699Skan tree link; 502590075Sobrien 5026169699Skan for (link = arg_info->tags; 5027169699Skan link; 5028169699Skan link = TREE_CHAIN (link)) 5029169699Skan TYPE_CONTEXT (TREE_VALUE (link)) = type; 503090075Sobrien } 5031169699Skan break; 5032169699Skan } 5033169699Skan case cdk_pointer: 503418334Speter { 5035169699Skan /* Merge any constancy or volatility into the target type 5036169699Skan for the pointer. */ 503718334Speter 5038169699Skan if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 5039169699Skan && type_quals) 5040169699Skan pedwarn ("ISO C forbids qualified function types"); 5041169699Skan if (type_quals) 5042169699Skan type = c_build_qualified_type (type, type_quals); 5043169699Skan size_varies = 0; 504418334Speter 5045169699Skan /* When the pointed-to type involves components of variable size, 5046169699Skan care must be taken to ensure that the size evaluation code is 5047169699Skan emitted early enough to dominate all the possible later uses 5048169699Skan and late enough for the variables on which it depends to have 5049169699Skan been assigned. 505018334Speter 5051169699Skan This is expected to happen automatically when the pointed-to 5052169699Skan type has a name/declaration of it's own, but special attention 5053169699Skan is required if the type is anonymous. 505418334Speter 5055169699Skan We handle the NORMAL and FIELD contexts here by attaching an 5056169699Skan artificial TYPE_DECL to such pointed-to type. This forces the 5057169699Skan sizes evaluation at a safe point and ensures it is not deferred 5058169699Skan until e.g. within a deeper conditional context. 505918334Speter 5060169699Skan We expect nothing to be needed here for PARM or TYPENAME. 5061169699Skan Pushing a TYPE_DECL at this point for TYPENAME would actually 5062169699Skan be incorrect, as we might be in the middle of an expression 5063169699Skan with side effects on the pointed-to type size "arguments" prior 5064169699Skan to the pointer declaration point and the fake TYPE_DECL in the 5065169699Skan enclosing context would force the size evaluation prior to the 5066169699Skan side effects. */ 506752284Sobrien 5068169699Skan if (!TYPE_NAME (type) 5069169699Skan && (decl_context == NORMAL || decl_context == FIELD) 5070169699Skan && variably_modified_type_p (type, NULL_TREE)) 5071169699Skan { 5072169699Skan tree decl = build_decl (TYPE_DECL, NULL_TREE, type); 5073169699Skan DECL_ARTIFICIAL (decl) = 1; 5074169699Skan pushdecl (decl); 5075169699Skan finish_decl (decl, NULL_TREE, NULL_TREE); 5076169699Skan TYPE_NAME (type) = decl; 5077169699Skan } 507852284Sobrien 5079169699Skan type = build_pointer_type (type); 508090075Sobrien 5081169699Skan /* Process type qualifiers (such as const or volatile) 5082169699Skan that were given inside the `*'. */ 5083169699Skan type_quals = declarator->u.pointer_quals; 508452284Sobrien 5085169699Skan declarator = declarator->declarator; 5086169699Skan break; 5087169699Skan } 5088261188Spfg 5089261188Spfg 5090261188Spfg /* APPLE LOCAL begin radar 5732232 - blocks (C++ cj) */ 5091261188Spfg case cdk_block_pointer: 5092261188Spfg { 5093261188Spfg if (TREE_CODE (type) != FUNCTION_TYPE) 5094261188Spfg { 5095261188Spfg error ("block pointer to non-function type is invalid"); 5096261188Spfg type = error_mark_node; 5097261188Spfg } 5098261188Spfg else 5099261188Spfg { 5100261188Spfg type = build_block_pointer_type (type); 5101261188Spfg /* APPLE LOCAL begin radar 5814025 (C++ cj) */ 5102261188Spfg /* Process type qualifiers (such as const or volatile) 5103261188Spfg that were given inside the `^'. */ 5104261188Spfg type_quals = declarator->u.pointer_quals; 5105261188Spfg /* APPLE LOCAL end radar 5814025 (C++ cj) */ 5106261188Spfg declarator = declarator->declarator; 5107261188Spfg } 5108261188Spfg break; 5109261188Spfg } 5110261188Spfg /* APPLE LOCAL end radar 5732232 - blocks (C++ cj) */ 5111261188Spfg 5112169699Skan default: 5113169699Skan gcc_unreachable (); 511418334Speter } 511518334Speter } 511618334Speter 5117169699Skan /* Now TYPE has the actual type, apart from any qualifiers in 5118169699Skan TYPE_QUALS. */ 511918334Speter 5120146906Skan /* Check the type and width of a bit-field. */ 5121146906Skan if (bitfield) 5122146906Skan check_bitfield_type_and_width (&type, width, orig_name); 5123146906Skan 512450397Sobrien /* Did array size calculations overflow? */ 512550397Sobrien 512650397Sobrien if (TREE_CODE (type) == ARRAY_TYPE 512790075Sobrien && COMPLETE_TYPE_P (type) 5128169699Skan && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST 5129169699Skan && TREE_OVERFLOW (TYPE_SIZE_UNIT (type))) 513090075Sobrien { 5131169699Skan error ("size of array %qs is too large", name); 513290075Sobrien /* If we proceed with the array type as it is, we'll eventually 513390075Sobrien crash in tree_low_cst(). */ 513490075Sobrien type = error_mark_node; 513590075Sobrien } 513650397Sobrien 5137261188Spfg /* APPLE LOCAL begin blocks 6339747 */ 5138261188Spfg if (decl_context == BLOCKDEF) 5139261188Spfg { 5140261188Spfg tree decl; 5141261188Spfg 5142261188Spfg if (type == error_mark_node) 5143261188Spfg return error_mark_node; 5144261188Spfg 5145261188Spfg if (TREE_CODE (type) != FUNCTION_TYPE) 5146261188Spfg { 5147261188Spfg tree arg_types; 5148261188Spfg 5149261188Spfg if (TREE_CODE (type) == ARRAY_TYPE) 5150261188Spfg { 5151261188Spfg error ("block declared as returning an array"); 5152261188Spfg return error_mark_node; 5153261188Spfg } 5154261188Spfg 5155261188Spfg arg_info = XOBNEW (&parser_obstack, struct c_arg_info); 5156261188Spfg arg_info->parms = 0; 5157261188Spfg arg_info->tags = 0; 5158261188Spfg arg_info->types = 0; 5159261188Spfg arg_info->others = 0; 5160261188Spfg arg_info->pending_sizes = 0; 5161261188Spfg arg_info->had_vla_unspec = 0; 5162261188Spfg arg_types = grokparms (arg_info, false); 5163261188Spfg type_quals = TYPE_UNQUALIFIED; 5164261188Spfg type = build_function_type (type, arg_types); 5165261188Spfg } 5166261188Spfg decl = build_decl (FUNCTION_DECL, NULL_TREE, type); 5167261188Spfg DECL_ARGUMENTS (decl) = arg_info ? arg_info->parms : NULL_TREE; 5168261188Spfg return decl; 5169261188Spfg } 5170261188Spfg /* APPLE LOCAL end blocks 6339747 */ 5171261188Spfg 517218334Speter /* If this is declaring a typedef name, return a TYPE_DECL. */ 517318334Speter 5174169699Skan if (storage_class == csc_typedef) 517518334Speter { 517618334Speter tree decl; 517718334Speter if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 517852284Sobrien && type_quals) 517990075Sobrien pedwarn ("ISO C forbids qualified function types"); 518052284Sobrien if (type_quals) 518152284Sobrien type = c_build_qualified_type (type, type_quals); 5182169699Skan decl = build_decl (TYPE_DECL, declarator->u.id, type); 5183169699Skan if (declspecs->explicit_signed_p) 518418334Speter C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 518590075Sobrien decl_attributes (&decl, returned_attrs, 0); 5186169699Skan if (declspecs->inline_p) 5187169699Skan pedwarn ("typedef %q+D declared %<inline%>", decl); 518818334Speter return decl; 518918334Speter } 519018334Speter 519118334Speter /* If this is a type name (such as, in a cast or sizeof), 519218334Speter compute the type and return it now. */ 519318334Speter 519418334Speter if (decl_context == TYPENAME) 519518334Speter { 5196169699Skan /* Note that the grammar rejects storage classes in typenames 5197169699Skan and fields. */ 5198169699Skan gcc_assert (storage_class == csc_none && !threadp 5199169699Skan && !declspecs->inline_p); 520018334Speter if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 520152284Sobrien && type_quals) 520290075Sobrien pedwarn ("ISO C forbids const or volatile function types"); 520352284Sobrien if (type_quals) 520452284Sobrien type = c_build_qualified_type (type, type_quals); 520590075Sobrien decl_attributes (&type, returned_attrs, 0); 520618334Speter return type; 520718334Speter } 520818334Speter 5209169699Skan if (pedantic && decl_context == FIELD 5210169699Skan && variably_modified_type_p (type, NULL_TREE)) 5211169699Skan { 5212169699Skan /* C99 6.7.2.1p8 */ 5213169699Skan pedwarn ("a member of a structure or union cannot have a variably modified type"); 5214169699Skan } 5215169699Skan 521618334Speter /* Aside from typedefs and type names (handle above), 521718334Speter `void' at top level (not within pointer) 521818334Speter is allowed only in public variables. 521918334Speter We don't complain about parms either, but that is because 522018334Speter a better error message can be made later. */ 522118334Speter 522290075Sobrien if (VOID_TYPE_P (type) && decl_context != PARM 5223169699Skan && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE) 5224169699Skan && (storage_class == csc_extern 5225169699Skan || (current_scope == file_scope 5226169699Skan && !(storage_class == csc_static 5227169699Skan || storage_class == csc_register))))) 522818334Speter { 5229169699Skan error ("variable or field %qs declared void", name); 523018334Speter type = integer_type_node; 523118334Speter } 523218334Speter 523318334Speter /* Now create the decl, which may be a VAR_DECL, a PARM_DECL 523418334Speter or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ 523518334Speter 523618334Speter { 523790075Sobrien tree decl; 523818334Speter 523918334Speter if (decl_context == PARM) 524018334Speter { 524190075Sobrien tree type_as_written; 524290075Sobrien tree promoted_type; 524318334Speter 524418334Speter /* A parameter declared as an array of T is really a pointer to T. 524518334Speter One declared as a function is really a pointer to a function. */ 524618334Speter 524718334Speter if (TREE_CODE (type) == ARRAY_TYPE) 524818334Speter { 524918334Speter /* Transfer const-ness of array into that of type pointed to. */ 525018334Speter type = TREE_TYPE (type); 525152284Sobrien if (type_quals) 525252284Sobrien type = c_build_qualified_type (type, type_quals); 525318334Speter type = build_pointer_type (type); 5254169699Skan type_quals = array_ptr_quals; 525590075Sobrien 5256169699Skan /* We don't yet implement attributes in this context. */ 5257169699Skan if (array_ptr_attrs != NULL_TREE) 5258169699Skan warning (OPT_Wattributes, 5259169699Skan "attributes in parameter array declarator ignored"); 526090075Sobrien 526118334Speter size_varies = 0; 526218334Speter } 526318334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 526418334Speter { 526552284Sobrien if (pedantic && type_quals) 526690075Sobrien pedwarn ("ISO C forbids qualified function types"); 526752284Sobrien if (type_quals) 526852284Sobrien type = c_build_qualified_type (type, type_quals); 526918334Speter type = build_pointer_type (type); 527052284Sobrien type_quals = TYPE_UNQUALIFIED; 527118334Speter } 527290075Sobrien else if (type_quals) 527390075Sobrien type = c_build_qualified_type (type, type_quals); 5274132730Skan 527590075Sobrien type_as_written = type; 527618334Speter 5277169699Skan decl = build_decl (PARM_DECL, declarator->u.id, type); 527818334Speter if (size_varies) 527918334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 528018334Speter 528118334Speter /* Compute the type actually passed in the parmlist, 528218334Speter for the case where there is no prototype. 528318334Speter (For example, shorts and chars are passed as ints.) 528418334Speter When there is a prototype, this is overridden later. */ 528518334Speter 528690075Sobrien if (type == error_mark_node) 528790075Sobrien promoted_type = type; 528890075Sobrien else 5289117421Skan promoted_type = c_type_promotes_to (type); 529018334Speter 529190075Sobrien DECL_ARG_TYPE (decl) = promoted_type; 5292169699Skan if (declspecs->inline_p) 5293169699Skan pedwarn ("parameter %q+D declared %<inline%>", decl); 529418334Speter } 529518334Speter else if (decl_context == FIELD) 529618334Speter { 5297169699Skan /* Note that the grammar rejects storage classes in typenames 5298169699Skan and fields. */ 5299169699Skan gcc_assert (storage_class == csc_none && !threadp 5300169699Skan && !declspecs->inline_p); 5301169699Skan 530218334Speter /* Structure field. It may not be a function. */ 530396263Sobrien 530418334Speter if (TREE_CODE (type) == FUNCTION_TYPE) 530518334Speter { 5306169699Skan error ("field %qs declared as a function", name); 530718334Speter type = build_pointer_type (type); 530818334Speter } 530990075Sobrien else if (TREE_CODE (type) != ERROR_MARK 5310169699Skan && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type)) 531118334Speter { 5312169699Skan error ("field %qs has incomplete type", name); 531318334Speter type = error_mark_node; 531418334Speter } 5315169699Skan type = c_build_qualified_type (type, type_quals); 5316169699Skan decl = build_decl (FIELD_DECL, declarator->u.id, type); 531790075Sobrien DECL_NONADDRESSABLE_P (decl) = bitfield; 531890075Sobrien 531918334Speter if (size_varies) 532018334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 532118334Speter } 532218334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 532318334Speter { 5324169699Skan if (storage_class == csc_register || threadp) 5325169699Skan { 5326169699Skan error ("invalid storage class for function %qs", name); 5327169699Skan } 5328169699Skan else if (current_scope != file_scope) 5329169699Skan { 5330169699Skan /* Function declaration not at file scope. Storage 5331169699Skan classes other than `extern' are not allowed, C99 5332169699Skan 6.7.1p5, and `extern' makes no difference. However, 5333169699Skan GCC allows 'auto', perhaps with 'inline', to support 5334169699Skan nested functions. */ 5335169699Skan if (storage_class == csc_auto) 5336169699Skan { 5337169699Skan if (pedantic) 5338169699Skan pedwarn ("invalid storage class for function %qs", name); 5339169699Skan } 5340169699Skan else if (storage_class == csc_static) 5341169699Skan { 5342169699Skan error ("invalid storage class for function %qs", name); 5343169699Skan if (funcdef_flag) 5344169699Skan storage_class = declspecs->storage_class = csc_none; 5345169699Skan else 5346169699Skan return 0; 5347169699Skan } 5348169699Skan } 534918334Speter 5350169699Skan decl = build_decl (FUNCTION_DECL, declarator->u.id, type); 535190075Sobrien decl = build_decl_attribute_variant (decl, decl_attr); 535218334Speter 5353169699Skan DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl); 535490075Sobrien 5355169699Skan if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl)) 535690075Sobrien pedwarn ("ISO C forbids qualified function types"); 535718334Speter 5358169699Skan /* GNU C interprets a volatile-qualified function type to indicate 535952284Sobrien that the function does not return. */ 536052284Sobrien if ((type_quals & TYPE_QUAL_VOLATILE) 536190075Sobrien && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) 5362169699Skan warning (0, "%<noreturn%> function returns non-void value"); 536318334Speter 5364169699Skan /* Every function declaration is an external reference 5365169699Skan (DECL_EXTERNAL) except for those which are not at file 5366169699Skan scope and are explicitly declared "auto". This is 5367169699Skan forbidden by standard C (C99 6.7.1p5) and is interpreted by 5368169699Skan GCC to signify a forward declaration of a nested function. */ 5369169699Skan if (storage_class == csc_auto && current_scope != file_scope) 5370169699Skan DECL_EXTERNAL (decl) = 0; 5371189824Sdas /* In C99, a function which is declared 'inline' with 'extern' 5372189824Sdas is not an external reference (which is confusing). It 5373189824Sdas means that the later definition of the function must be output 5374189824Sdas in this file, C99 6.7.4p6. In GNU C89, a function declared 5375189824Sdas 'extern inline' is an external reference. */ 5376189824Sdas else if (declspecs->inline_p && storage_class != csc_static) 5377189824Sdas DECL_EXTERNAL (decl) = ((storage_class == csc_extern) 5378189824Sdas == flag_gnu89_inline); 5379169699Skan else 5380189824Sdas DECL_EXTERNAL (decl) = !initialized; 5381169699Skan 538218334Speter /* Record absence of global scope for `static' or `auto'. */ 538318334Speter TREE_PUBLIC (decl) 5384169699Skan = !(storage_class == csc_static || storage_class == csc_auto); 538518334Speter 5386169699Skan /* For a function definition, record the argument information 5387169699Skan block where store_parm_decls will look for it. */ 5388169699Skan if (funcdef_flag) 5389169699Skan current_function_arg_info = arg_info; 5390169699Skan 5391169699Skan if (declspecs->default_int_p) 539296263Sobrien C_FUNCTION_IMPLICIT_INT (decl) = 1; 539396263Sobrien 539418334Speter /* Record presence of `inline', if it is reasonable. */ 5395169699Skan if (flag_hosted && MAIN_NAME_P (declarator->u.id)) 539618334Speter { 5397169699Skan if (declspecs->inline_p) 5398169699Skan pedwarn ("cannot inline function %<main%>"); 539990075Sobrien } 5400169699Skan else if (declspecs->inline_p) 540190075Sobrien { 5402132730Skan /* Record that the function is declared `inline'. */ 540390075Sobrien DECL_DECLARED_INLINE_P (decl) = 1; 540418334Speter 540590075Sobrien /* Do not mark bare declarations as DECL_INLINE. Doing so 540690075Sobrien in the presence of multiple declarations can result in 540790075Sobrien the abstract origin pointing between the declarations, 540890075Sobrien which will confuse dwarf2out. */ 540990075Sobrien if (initialized) 5410189824Sdas DECL_INLINE (decl) = 1; 541118334Speter } 541290075Sobrien /* If -finline-functions, assume it can be inlined. This does 541390075Sobrien two things: let the function be deferred until it is actually 541490075Sobrien needed, and let dwarf2 know that the function is inlinable. */ 541590075Sobrien else if (flag_inline_trees == 2 && initialized) 5416132730Skan DECL_INLINE (decl) = 1; 541718334Speter } 541818334Speter else 541918334Speter { 542018334Speter /* It's a variable. */ 542118334Speter /* An uninitialized decl with `extern' is a reference. */ 5422169699Skan int extern_ref = !initialized && storage_class == csc_extern; 542318334Speter 5424169699Skan type = c_build_qualified_type (type, type_quals); 5425132730Skan 5426169699Skan /* C99 6.2.2p7: It is invalid (compile-time undefined 5427169699Skan behavior) to create an 'extern' declaration for a 5428132730Skan variable if there is a global declaration that is 5429169699Skan 'static' and the global declaration is not visible. 5430169699Skan (If the static declaration _is_ currently visible, 5431169699Skan the 'extern' declaration is taken to refer to that decl.) */ 5432169699Skan if (extern_ref && current_scope != file_scope) 5433132730Skan { 5434169699Skan tree global_decl = identifier_global_value (declarator->u.id); 5435169699Skan tree visible_decl = lookup_name (declarator->u.id); 5436132730Skan 5437132730Skan if (global_decl 5438169699Skan && global_decl != visible_decl 5439132730Skan && TREE_CODE (global_decl) == VAR_DECL 5440132730Skan && !TREE_PUBLIC (global_decl)) 5441169699Skan error ("variable previously declared %<static%> redeclared " 5442169699Skan "%<extern%>"); 5443132730Skan } 5444132730Skan 5445169699Skan decl = build_decl (VAR_DECL, declarator->u.id, type); 5446169699Skan DECL_SOURCE_LOCATION (decl) = declarator->id_loc; 544718334Speter if (size_varies) 544818334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 544918334Speter 5450169699Skan if (declspecs->inline_p) 5451169699Skan pedwarn ("variable %q+D declared %<inline%>", decl); 545218334Speter 5453169699Skan /* At file scope, an initialized extern declaration may follow 5454169699Skan a static declaration. In that case, DECL_EXTERNAL will be 5455169699Skan reset later in start_decl. */ 5456169699Skan DECL_EXTERNAL (decl) = (storage_class == csc_extern); 5457117421Skan 5458132730Skan /* At file scope, the presence of a `static' or `register' storage 545918334Speter class specifier, or the absence of all storage class specifiers 546018334Speter makes this declaration a definition (perhaps tentative). Also, 5461169699Skan the absence of `static' makes it public. */ 5462169699Skan if (current_scope == file_scope) 546318334Speter { 5464169699Skan TREE_PUBLIC (decl) = storage_class != csc_static; 5465117421Skan TREE_STATIC (decl) = !extern_ref; 546618334Speter } 5467132730Skan /* Not at file scope, only `static' makes a static definition. */ 546818334Speter else 546918334Speter { 5470169699Skan TREE_STATIC (decl) = (storage_class == csc_static); 5471117421Skan TREE_PUBLIC (decl) = extern_ref; 547218334Speter } 5473117421Skan 5474169699Skan if (threadp) 5475117421Skan { 5476117421Skan if (targetm.have_tls) 5477169699Skan DECL_TLS_MODEL (decl) = decl_default_tls_model (decl); 5478117421Skan else 5479117421Skan /* A mere warning is sure to result in improper semantics 5480117421Skan at runtime. Don't bother to allow this to compile. */ 5481117421Skan error ("thread-local storage not supported for this target"); 5482117421Skan } 548318334Speter } 548418334Speter 5485169699Skan if (storage_class == csc_extern 5486169699Skan && variably_modified_type_p (type, NULL_TREE)) 5487169699Skan { 5488169699Skan /* C99 6.7.5.2p2 */ 5489169699Skan error ("object with variably modified type must have no linkage"); 5490169699Skan } 5491169699Skan 549218334Speter /* Record `register' declaration for warnings on & 549318334Speter and in case doing stupid register allocation. */ 549418334Speter 5495169699Skan if (storage_class == csc_register) 5496169699Skan { 5497169699Skan C_DECL_REGISTER (decl) = 1; 5498169699Skan DECL_REGISTER (decl) = 1; 5499169699Skan } 550018334Speter 550118334Speter /* Record constancy and volatility. */ 550252284Sobrien c_apply_type_quals_to_decl (type_quals, decl); 550318334Speter 550418334Speter /* If a type has volatile components, it should be stored in memory. 550518334Speter Otherwise, the fact that those components are volatile 5506169699Skan will be ignored, and would even crash the compiler. 5507169699Skan Of course, this only makes sense on VAR,PARM, and RESULT decl's. */ 5508169699Skan if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)) 5509169699Skan && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL 5510169699Skan || TREE_CODE (decl) == RESULT_DECL)) 5511169699Skan { 5512169699Skan /* It is not an error for a structure with volatile fields to 5513169699Skan be declared register, but reset DECL_REGISTER since it 5514169699Skan cannot actually go in a register. */ 5515169699Skan int was_reg = C_DECL_REGISTER (decl); 5516169699Skan C_DECL_REGISTER (decl) = 0; 5517169699Skan DECL_REGISTER (decl) = 0; 5518169699Skan c_mark_addressable (decl); 5519169699Skan C_DECL_REGISTER (decl) = was_reg; 5520169699Skan } 552118334Speter 5522132730Skan /* This is the earliest point at which we might know the assembler 5523132730Skan name of a variable. Thus, if it's known before this, die horribly. */ 5524169699Skan gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)); 5525132730Skan 552690075Sobrien decl_attributes (&decl, returned_attrs, 0); 552718334Speter 552818334Speter return decl; 552918334Speter } 553018334Speter} 553118334Speter 553218334Speter/* Decode the parameter-list info for a function type or function definition. 5533169699Skan The argument is the value returned by `get_parm_info' (or made in c-parse.c 553418334Speter if there is an identifier list instead of a parameter decl list). 553518334Speter These two functions are separate because when a function returns 553618334Speter or receives functions then each is called multiple times but the order 553718334Speter of calls is different. The last call to `grokparms' is always the one 553818334Speter that contains the formal parameter names of a function definition. 553918334Speter 554018334Speter Return a list of arg types to use in the FUNCTION_TYPE for this function. 554118334Speter 5542169699Skan FUNCDEF_FLAG is true for a function definition, false for 554318334Speter a mere declaration. A nonempty identifier-list gets an error message 5544169699Skan when FUNCDEF_FLAG is false. */ 554518334Speter 554618334Speterstatic tree 5547169699Skangrokparms (struct c_arg_info *arg_info, bool funcdef_flag) 554818334Speter{ 5549169699Skan tree arg_types = arg_info->types; 555018334Speter 5551169699Skan if (funcdef_flag && arg_info->had_vla_unspec) 5552169699Skan { 5553169699Skan /* A function definition isn't function prototype scope C99 6.2.1p4. */ 5554169699Skan /* C99 6.7.5.2p4 */ 5555169699Skan error ("%<[*]%> not allowed in other than function prototype scope"); 5556169699Skan } 555718334Speter 5558169699Skan if (arg_types == 0 && !funcdef_flag && !in_system_header) 5559169699Skan warning (OPT_Wstrict_prototypes, 5560169699Skan "function declaration isn%'t a prototype"); 556118334Speter 5562169699Skan if (arg_types == error_mark_node) 5563169699Skan return 0; /* don't set TYPE_ARG_TYPES in this case */ 5564169699Skan 5565169699Skan else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE) 556618334Speter { 5567169699Skan if (!funcdef_flag) 556818334Speter pedwarn ("parameter names (without types) in function declaration"); 556918334Speter 5570169699Skan arg_info->parms = arg_info->types; 5571169699Skan arg_info->types = 0; 557218334Speter return 0; 557318334Speter } 557418334Speter else 557518334Speter { 5576169699Skan tree parm, type, typelt; 5577169699Skan unsigned int parmno; 557818334Speter 5579169699Skan /* If there is a parameter of incomplete type in a definition, 5580169699Skan this is an error. In a declaration this is valid, and a 5581169699Skan struct or union type may be completed later, before any calls 5582169699Skan or definition of the function. In the case where the tag was 5583169699Skan first declared within the parameter list, a warning has 5584169699Skan already been given. If a parameter has void type, then 5585169699Skan however the function cannot be defined or called, so 5586169699Skan warn. */ 5587169699Skan 5588169699Skan for (parm = arg_info->parms, typelt = arg_types, parmno = 1; 5589132730Skan parm; 5590169699Skan parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++) 5591169699Skan { 5592169699Skan type = TREE_VALUE (typelt); 5593169699Skan if (type == error_mark_node) 5594169699Skan continue; 5595132730Skan 5596169699Skan if (!COMPLETE_TYPE_P (type)) 5597169699Skan { 5598169699Skan if (funcdef_flag) 5599169699Skan { 5600169699Skan if (DECL_NAME (parm)) 5601169699Skan error ("parameter %u (%q+D) has incomplete type", 5602169699Skan parmno, parm); 5603169699Skan else 5604169699Skan error ("%Jparameter %u has incomplete type", 5605169699Skan parm, parmno); 5606169699Skan 5607169699Skan TREE_VALUE (typelt) = error_mark_node; 5608169699Skan TREE_TYPE (parm) = error_mark_node; 5609169699Skan } 5610169699Skan else if (VOID_TYPE_P (type)) 5611169699Skan { 5612169699Skan if (DECL_NAME (parm)) 5613169699Skan warning (0, "parameter %u (%q+D) has void type", 5614169699Skan parmno, parm); 5615169699Skan else 5616169699Skan warning (0, "%Jparameter %u has void type", 5617169699Skan parm, parmno); 5618169699Skan } 5619169699Skan } 5620169699Skan 5621169699Skan if (DECL_NAME (parm) && TREE_USED (parm)) 5622169699Skan warn_if_shadowing (parm); 5623169699Skan } 5624169699Skan return arg_types; 562518334Speter } 562618334Speter} 562718334Speter 5628169699Skan/* Take apart the current scope and return a c_arg_info structure with 5629169699Skan info on a parameter list just parsed. 563018334Speter 5631169699Skan This structure is later fed to 'grokparms' and 'store_parm_decls'. 563218334Speter 5633169699Skan ELLIPSIS being true means the argument list ended in '...' so don't 5634169699Skan append a sentinel (void_list_node) to the end of the type-list. */ 5635169699Skan 5636169699Skanstruct c_arg_info * 5637169699Skanget_parm_info (bool ellipsis) 563818334Speter{ 5639169699Skan struct c_binding *b = current_scope->bindings; 5640169699Skan struct c_arg_info *arg_info = XOBNEW (&parser_obstack, 5641169699Skan struct c_arg_info); 5642169699Skan tree parms = 0; 5643169699Skan tree tags = 0; 5644169699Skan tree types = 0; 5645169699Skan tree others = 0; 5646169699Skan 5647132730Skan static bool explained_incomplete_types = false; 5648132730Skan bool gave_void_only_once_err = false; 564918334Speter 5650169699Skan arg_info->parms = 0; 5651169699Skan arg_info->tags = 0; 5652169699Skan arg_info->types = 0; 5653169699Skan arg_info->others = 0; 5654169699Skan arg_info->pending_sizes = 0; 5655169699Skan arg_info->had_vla_unspec = current_scope->had_vla_unspec; 5656132730Skan 5657169699Skan /* The bindings in this scope must not get put into a block. 5658169699Skan We will take care of deleting the binding nodes. */ 5659169699Skan current_scope->bindings = 0; 566018334Speter 5661169699Skan /* This function is only called if there was *something* on the 5662169699Skan parameter list. */ 5663169699Skan gcc_assert (b); 5664169699Skan 5665169699Skan /* A parameter list consisting solely of 'void' indicates that the 5666169699Skan function takes no arguments. But if the 'void' is qualified 5667169699Skan (by 'const' or 'volatile'), or has a storage class specifier 5668169699Skan ('register'), then the behavior is undefined; issue an error. 5669169699Skan Typedefs for 'void' are OK (see DR#157). */ 5670169699Skan if (b->prev == 0 /* one binding */ 5671169699Skan && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */ 5672169699Skan && !DECL_NAME (b->decl) /* anonymous */ 5673169699Skan && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */ 567418334Speter { 5675169699Skan if (TREE_THIS_VOLATILE (b->decl) 5676169699Skan || TREE_READONLY (b->decl) 5677169699Skan || C_DECL_REGISTER (b->decl)) 5678169699Skan error ("%<void%> as only parameter may not be qualified"); 567918334Speter 5680169699Skan /* There cannot be an ellipsis. */ 5681169699Skan if (ellipsis) 5682169699Skan error ("%<void%> must be the only parameter"); 5683132730Skan 5684169699Skan arg_info->types = void_list_node; 5685169699Skan return arg_info; 568618334Speter } 568718334Speter 5688169699Skan if (!ellipsis) 5689169699Skan types = void_list_node; 569018334Speter 5691169699Skan /* Break up the bindings list into parms, tags, types, and others; 5692169699Skan apply sanity checks; purge the name-to-decl bindings. */ 5693169699Skan while (b) 569418334Speter { 5695169699Skan tree decl = b->decl; 5696169699Skan tree type = TREE_TYPE (decl); 5697132730Skan const char *keyword; 5698132730Skan 5699169699Skan switch (TREE_CODE (decl)) 570090075Sobrien { 5701169699Skan case PARM_DECL: 5702169699Skan if (b->id) 5703169699Skan { 5704169699Skan gcc_assert (I_SYMBOL_BINDING (b->id) == b); 5705169699Skan I_SYMBOL_BINDING (b->id) = b->shadowed; 5706169699Skan } 5707132730Skan 5708169699Skan /* Check for forward decls that never got their actual decl. */ 5709169699Skan if (TREE_ASM_WRITTEN (decl)) 5710169699Skan error ("parameter %q+D has just a forward declaration", decl); 5711169699Skan /* Check for (..., void, ...) and issue an error. */ 5712169699Skan else if (VOID_TYPE_P (type) && !DECL_NAME (decl)) 5713169699Skan { 5714169699Skan if (!gave_void_only_once_err) 5715169699Skan { 5716169699Skan error ("%<void%> must be the only parameter"); 5717169699Skan gave_void_only_once_err = true; 5718169699Skan } 5719169699Skan } 5720169699Skan else 5721169699Skan { 5722169699Skan /* Valid parameter, add it to the list. */ 5723169699Skan TREE_CHAIN (decl) = parms; 5724169699Skan parms = decl; 5725132730Skan 5726169699Skan /* Since there is a prototype, args are passed in their 5727169699Skan declared types. The back end may override this later. */ 5728169699Skan DECL_ARG_TYPE (decl) = type; 5729169699Skan types = tree_cons (0, type, types); 5730169699Skan } 5731169699Skan break; 5732169699Skan 5733169699Skan case ENUMERAL_TYPE: keyword = "enum"; goto tag; 5734169699Skan case UNION_TYPE: keyword = "union"; goto tag; 5735169699Skan case RECORD_TYPE: keyword = "struct"; goto tag; 5736169699Skan tag: 5737169699Skan /* Types may not have tag-names, in which case the type 5738169699Skan appears in the bindings list with b->id NULL. */ 5739169699Skan if (b->id) 5740169699Skan { 5741169699Skan gcc_assert (I_TAG_BINDING (b->id) == b); 5742169699Skan I_TAG_BINDING (b->id) = b->shadowed; 5743169699Skan } 5744169699Skan 5745169699Skan /* Warn about any struct, union or enum tags defined in a 5746169699Skan parameter list. The scope of such types is limited to 5747169699Skan the parameter list, which is rarely if ever desirable 5748169699Skan (it's impossible to call such a function with type- 5749169699Skan correct arguments). An anonymous union parm type is 5750169699Skan meaningful as a GNU extension, so don't warn for that. */ 5751169699Skan if (TREE_CODE (decl) != UNION_TYPE || b->id != 0) 5752169699Skan { 5753169699Skan if (b->id) 5754169699Skan /* The %s will be one of 'struct', 'union', or 'enum'. */ 5755169699Skan warning (0, "%<%s %E%> declared inside parameter list", 5756169699Skan keyword, b->id); 5757169699Skan else 5758169699Skan /* The %s will be one of 'struct', 'union', or 'enum'. */ 5759169699Skan warning (0, "anonymous %s declared inside parameter list", 5760169699Skan keyword); 5761169699Skan 5762169699Skan if (!explained_incomplete_types) 5763169699Skan { 5764169699Skan warning (0, "its scope is only this definition or declaration," 5765169699Skan " which is probably not what you want"); 5766169699Skan explained_incomplete_types = true; 5767169699Skan } 5768169699Skan } 5769169699Skan 5770169699Skan tags = tree_cons (b->id, decl, tags); 5771169699Skan break; 5772169699Skan 5773169699Skan case CONST_DECL: 5774169699Skan case TYPE_DECL: 5775169699Skan case FUNCTION_DECL: 5776169699Skan /* CONST_DECLs appear here when we have an embedded enum, 5777169699Skan and TYPE_DECLs appear here when we have an embedded struct 5778169699Skan or union. No warnings for this - we already warned about the 5779169699Skan type itself. FUNCTION_DECLs appear when there is an implicit 5780169699Skan function declaration in the parameter list. */ 5781169699Skan 5782169699Skan TREE_CHAIN (decl) = others; 5783169699Skan others = decl; 5784169699Skan /* fall through */ 5785169699Skan 5786169699Skan case ERROR_MARK: 5787169699Skan /* error_mark_node appears here when we have an undeclared 5788169699Skan variable. Just throw it away. */ 5789169699Skan if (b->id) 5790169699Skan { 5791169699Skan gcc_assert (I_SYMBOL_BINDING (b->id) == b); 5792169699Skan I_SYMBOL_BINDING (b->id) = b->shadowed; 5793169699Skan } 5794169699Skan break; 5795169699Skan 5796169699Skan /* Other things that might be encountered. */ 5797169699Skan case LABEL_DECL: 5798169699Skan case VAR_DECL: 5799169699Skan default: 5800169699Skan gcc_unreachable (); 580118334Speter } 5802132730Skan 5803169699Skan b = free_binding_and_advance (b); 5804132730Skan } 5805132730Skan 5806169699Skan arg_info->parms = parms; 5807169699Skan arg_info->tags = tags; 5808169699Skan arg_info->types = types; 5809169699Skan arg_info->others = others; 5810169699Skan arg_info->pending_sizes = get_pending_sizes (); 5811169699Skan return arg_info; 581218334Speter} 581318334Speter 581418334Speter/* Get the struct, enum or union (CODE says which) with tag NAME. 5815169699Skan Define the tag as a forward-reference if it is not defined. 5816169699Skan Return a c_typespec structure for the type specifier. */ 581718334Speter 5818169699Skanstruct c_typespec 5819169699Skanparser_xref_tag (enum tree_code code, tree name) 582018334Speter{ 5821169699Skan struct c_typespec ret; 582218334Speter /* If a cross reference is requested, look up the type 582318334Speter already defined for this tag and return it. */ 582418334Speter 5825132730Skan tree ref = lookup_tag (code, name, 0); 582690075Sobrien /* If this is the right type of tag, return what we found. 582790075Sobrien (This reference will be shadowed by shadow_tag later if appropriate.) 582890075Sobrien If this is the wrong type of tag, do not return it. If it was the 5829132730Skan wrong type in the same scope, we will have had an error 5830132730Skan message already; if in a different scope and declaring 583190075Sobrien a name, pending_xref_error will give an error message; but if in a 5832132730Skan different scope and not declaring a name, this tag should 583390075Sobrien shadow the previous declaration of a different type of tag, and 583490075Sobrien this would not work properly if we return the reference found. 583590075Sobrien (For example, with "struct foo" in an outer scope, "union foo;" 583690075Sobrien must shadow that tag with a new one of union type.) */ 5837169699Skan ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref); 583890075Sobrien if (ref && TREE_CODE (ref) == code) 5839169699Skan { 5840169699Skan ret.spec = ref; 5841169699Skan return ret; 5842169699Skan } 584318334Speter 584418334Speter /* If no such tag is yet defined, create a forward-reference node 584518334Speter and record it as the "definition". 584618334Speter When a real declaration of this type is found, 584718334Speter the forward-reference will be altered into a real type. */ 584818334Speter 584918334Speter ref = make_node (code); 585018334Speter if (code == ENUMERAL_TYPE) 585118334Speter { 585218334Speter /* Give the type a default layout like unsigned int 585318334Speter to avoid crashing if it does not get defined. */ 585418334Speter TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node); 585518334Speter TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); 585690075Sobrien TYPE_USER_ALIGN (ref) = 0; 5857169699Skan TYPE_UNSIGNED (ref) = 1; 585818334Speter TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); 585918334Speter TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); 586018334Speter TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); 586118334Speter } 586218334Speter 586318334Speter pushtag (name, ref); 586418334Speter 5865169699Skan ret.spec = ref; 5866169699Skan return ret; 586718334Speter} 5868169699Skan 5869169699Skan/* Get the struct, enum or union (CODE says which) with tag NAME. 5870169699Skan Define the tag as a forward-reference if it is not defined. 5871169699Skan Return a tree for the type. */ 5872169699Skan 5873169699Skantree 5874169699Skanxref_tag (enum tree_code code, tree name) 5875169699Skan{ 5876169699Skan return parser_xref_tag (code, name).spec; 5877169699Skan} 587818334Speter 5879132730Skan/* Make sure that the tag NAME is defined *in the current scope* 588018334Speter at least as a forward reference. 588190075Sobrien CODE says which kind of tag NAME ought to be. */ 588218334Speter 588318334Spetertree 5884132730Skanstart_struct (enum tree_code code, tree name) 588518334Speter{ 5886132730Skan /* If there is already a tag defined at this scope 588718334Speter (as a forward reference), just return it. */ 588818334Speter 588990075Sobrien tree ref = 0; 589018334Speter 589118334Speter if (name != 0) 5892132730Skan ref = lookup_tag (code, name, 1); 589318334Speter if (ref && TREE_CODE (ref) == code) 589418334Speter { 5895161660Skan if (TYPE_SIZE (ref)) 5896169699Skan { 589790075Sobrien if (code == UNION_TYPE) 5898169699Skan error ("redefinition of %<union %E%>", name); 5899169699Skan else 5900169699Skan error ("redefinition of %<struct %E%>", name); 5901132730Skan } 5902161660Skan else if (C_TYPE_BEING_DEFINED (ref)) 5903161660Skan { 5904161660Skan if (code == UNION_TYPE) 5905169699Skan error ("nested redefinition of %<union %E%>", name); 5906169699Skan else 5907169699Skan error ("nested redefinition of %<struct %E%>", name); 5908189824Sdas /* Don't create structures that contain themselves. */ 5909189824Sdas ref = NULL_TREE; 5910161660Skan } 5911117421Skan } 5912189824Sdas 5913189824Sdas /* Otherwise create a forward-reference just so the tag is in scope. */ 5914189824Sdas 5915189824Sdas if (ref == NULL_TREE || TREE_CODE (ref) != code) 5916117421Skan { 5917117421Skan ref = make_node (code); 5918117421Skan pushtag (name, ref); 591918334Speter } 5920132730Skan 592118334Speter C_TYPE_BEING_DEFINED (ref) = 1; 592250397Sobrien TYPE_PACKED (ref) = flag_pack_struct; 592318334Speter return ref; 592418334Speter} 592518334Speter 5926169699Skan/* Process the specs, declarator and width (NULL if omitted) 592718334Speter of a structure component, returning a FIELD_DECL node. 5928132730Skan WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node. 592918334Speter 593018334Speter This is done during the parsing of the struct declaration. 593118334Speter The FIELD_DECL nodes are chained together and the lot of them 593218334Speter are ultimately passed to `build_struct' to make the RECORD_TYPE node. */ 593318334Speter 593418334Spetertree 5935169699Skangrokfield (struct c_declarator *declarator, struct c_declspecs *declspecs, 5936169699Skan tree width) 593718334Speter{ 593818334Speter tree value; 593918334Speter 5940169699Skan if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE 5941169699Skan && width == NULL_TREE) 594290075Sobrien { 5943117421Skan /* This is an unnamed decl. 5944117421Skan 5945117421Skan If we have something of the form "union { list } ;" then this 5946117421Skan is the anonymous union extension. Similarly for struct. 5947117421Skan 5948117421Skan If this is something of the form "struct foo;", then 5949117421Skan If MS extensions are enabled, this is handled as an 5950117421Skan anonymous struct. 5951117421Skan Otherwise this is a forward declaration of a structure tag. 5952117421Skan 5953117421Skan If this is something of the form "foo;" and foo is a TYPE_DECL, then 5954117421Skan If MS extensions are enabled and foo names a structure, then 5955117421Skan again this is an anonymous struct. 5956117421Skan Otherwise this is an error. 5957117421Skan 5958132730Skan Oh what a horrid tangled web we weave. I wonder if MS consciously 5959117421Skan took this from Plan 9 or if it was an accident of implementation 5960117421Skan that took root before someone noticed the bug... */ 5961117421Skan 5962169699Skan tree type = declspecs->type; 5963169699Skan bool type_ok = (TREE_CODE (type) == RECORD_TYPE 5964169699Skan || TREE_CODE (type) == UNION_TYPE); 5965169699Skan bool ok = false; 5966102790Skan 5967169699Skan if (type_ok 5968169699Skan && (flag_ms_extensions || !declspecs->typedef_p)) 596990075Sobrien { 5970117421Skan if (flag_ms_extensions) 5971169699Skan ok = true; 5972117421Skan else if (flag_iso) 5973169699Skan ok = false; 5974117421Skan else if (TYPE_NAME (type) == NULL) 5975169699Skan ok = true; 5976117421Skan else 5977169699Skan ok = false; 5978117421Skan } 5979169699Skan if (!ok) 5980117421Skan { 5981169699Skan pedwarn ("declaration does not declare anything"); 598290075Sobrien return NULL_TREE; 598390075Sobrien } 5984169699Skan if (pedantic) 5985169699Skan pedwarn ("ISO C doesn%'t support unnamed structs/unions"); 598690075Sobrien } 598718334Speter 5988169699Skan value = grokdeclarator (declarator, declspecs, FIELD, false, 5989132730Skan width ? &width : NULL); 599018334Speter 599118334Speter finish_decl (value, NULL_TREE, NULL_TREE); 599296263Sobrien DECL_INITIAL (value) = width; 599318334Speter 599418334Speter return value; 599518334Speter} 599618334Speter 5997132730Skan/* Generate an error for any duplicate field names in FIELDLIST. Munge 5998132730Skan the list such that this does not present a problem later. */ 5999122196Skan 6000132730Skanstatic void 6001132730Skandetect_field_duplicates (tree fieldlist) 6002132730Skan{ 6003132730Skan tree x, y; 6004132730Skan int timeout = 10; 6005122196Skan 6006132730Skan /* First, see if there are more than "a few" fields. 6007132730Skan This is trivially true if there are zero or one fields. */ 6008132730Skan if (!fieldlist) 6009132730Skan return; 6010132730Skan x = TREE_CHAIN (fieldlist); 6011132730Skan if (!x) 6012132730Skan return; 6013132730Skan do { 6014132730Skan timeout--; 6015132730Skan x = TREE_CHAIN (x); 6016132730Skan } while (timeout > 0 && x); 6017132730Skan 6018132730Skan /* If there were "few" fields, avoid the overhead of allocating 6019132730Skan a hash table. Instead just do the nested traversal thing. */ 6020132730Skan if (timeout > 0) 6021132730Skan { 6022132730Skan for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x)) 6023132730Skan if (DECL_NAME (x)) 6024132730Skan { 6025132730Skan for (y = fieldlist; y != x; y = TREE_CHAIN (y)) 6026132730Skan if (DECL_NAME (y) == DECL_NAME (x)) 6027132730Skan { 6028169699Skan error ("duplicate member %q+D", x); 6029132730Skan DECL_NAME (x) = NULL_TREE; 6030132730Skan } 6031132730Skan } 6032132730Skan } 6033132730Skan else 6034132730Skan { 6035132730Skan htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); 6036132730Skan void **slot; 6037132730Skan 6038132730Skan for (x = fieldlist; x ; x = TREE_CHAIN (x)) 6039132730Skan if ((y = DECL_NAME (x)) != 0) 6040132730Skan { 6041132730Skan slot = htab_find_slot (htab, y, INSERT); 6042132730Skan if (*slot) 6043132730Skan { 6044169699Skan error ("duplicate member %q+D", x); 6045132730Skan DECL_NAME (x) = NULL_TREE; 6046132730Skan } 6047132730Skan *slot = y; 6048132730Skan } 6049132730Skan 6050132730Skan htab_delete (htab); 6051132730Skan } 6052122196Skan} 6053132730Skan 605418334Speter/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. 605518334Speter FIELDLIST is a chain of FIELD_DECL nodes for the fields. 605690075Sobrien ATTRIBUTES are attributes to be applied to the structure. */ 605718334Speter 605818334Spetertree 6059132730Skanfinish_struct (tree t, tree fieldlist, tree attributes) 606018334Speter{ 606190075Sobrien tree x; 6062169699Skan bool toplevel = file_scope == current_scope; 606390075Sobrien int saw_named_field; 606418334Speter 606518334Speter /* If this type was previously laid out as a forward reference, 606618334Speter make sure we lay it out again. */ 606718334Speter 606818334Speter TYPE_SIZE (t) = 0; 606918334Speter 607090075Sobrien decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 607118334Speter 607250397Sobrien if (pedantic) 607350397Sobrien { 607450397Sobrien for (x = fieldlist; x; x = TREE_CHAIN (x)) 607550397Sobrien if (DECL_NAME (x) != 0) 607650397Sobrien break; 607718334Speter 607850397Sobrien if (x == 0) 6079169699Skan { 6080169699Skan if (TREE_CODE (t) == UNION_TYPE) 6081169699Skan { 6082169699Skan if (fieldlist) 6083169699Skan pedwarn ("union has no named members"); 6084169699Skan else 6085169699Skan pedwarn ("union has no members"); 6086169699Skan } 6087169699Skan else 6088169699Skan { 6089169699Skan if (fieldlist) 6090169699Skan pedwarn ("struct has no named members"); 6091169699Skan else 6092169699Skan pedwarn ("struct has no members"); 6093169699Skan } 6094169699Skan } 609550397Sobrien } 609650397Sobrien 609796263Sobrien /* Install struct as DECL_CONTEXT of each field decl. 6098169699Skan Also process specified field sizes, found in the DECL_INITIAL, 6099169699Skan storing 0 there after the type has been changed to precision equal 6100169699Skan to its width, rather than the precision of the specified standard 6101169699Skan type. (Correct layout requires the original type to have been preserved 6102169699Skan until now.) */ 610318334Speter 610490075Sobrien saw_named_field = 0; 610518334Speter for (x = fieldlist; x; x = TREE_CHAIN (x)) 610618334Speter { 6107169699Skan if (TREE_TYPE (x) == error_mark_node) 6108169699Skan continue; 6109169699Skan 611018334Speter DECL_CONTEXT (x) = t; 611118334Speter 6112169699Skan if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT) 6113169699Skan DECL_PACKED (x) = 1; 6114169699Skan 611518334Speter /* If any field is const, the structure type is pseudo-const. */ 611618334Speter if (TREE_READONLY (x)) 611718334Speter C_TYPE_FIELDS_READONLY (t) = 1; 611818334Speter else 611918334Speter { 612018334Speter /* A field that is pseudo-const makes the structure likewise. */ 612118334Speter tree t1 = TREE_TYPE (x); 612218334Speter while (TREE_CODE (t1) == ARRAY_TYPE) 612318334Speter t1 = TREE_TYPE (t1); 612418334Speter if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE) 612518334Speter && C_TYPE_FIELDS_READONLY (t1)) 612618334Speter C_TYPE_FIELDS_READONLY (t) = 1; 612718334Speter } 612818334Speter 612918334Speter /* Any field that is volatile means variables of this type must be 613018334Speter treated in some ways as volatile. */ 613118334Speter if (TREE_THIS_VOLATILE (x)) 613218334Speter C_TYPE_FIELDS_VOLATILE (t) = 1; 613318334Speter 613418334Speter /* Any field of nominal variable size implies structure is too. */ 613518334Speter if (C_DECL_VARIABLE_SIZE (x)) 613618334Speter C_TYPE_VARIABLE_SIZE (t) = 1; 613718334Speter 613896263Sobrien if (DECL_INITIAL (x)) 613918334Speter { 6140132730Skan unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1); 6141132730Skan DECL_SIZE (x) = bitsize_int (width); 6142132730Skan DECL_BIT_FIELD (x) = 1; 6143132730Skan SET_DECL_C_BIT_FIELD (x); 614496263Sobrien } 614596263Sobrien 614690075Sobrien /* Detect flexible array member in an invalid context. */ 614790075Sobrien if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 614890075Sobrien && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 614990075Sobrien && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 615090075Sobrien && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 615118334Speter { 615290075Sobrien if (TREE_CODE (t) == UNION_TYPE) 6153132730Skan { 6154132730Skan error ("%Jflexible array member in union", x); 6155132730Skan TREE_TYPE (x) = error_mark_node; 6156132730Skan } 615790075Sobrien else if (TREE_CHAIN (x) != NULL_TREE) 6158132730Skan { 6159132730Skan error ("%Jflexible array member not at end of struct", x); 6160132730Skan TREE_TYPE (x) = error_mark_node; 6161132730Skan } 6162169699Skan else if (!saw_named_field) 6163132730Skan { 6164132730Skan error ("%Jflexible array member in otherwise empty struct", x); 6165132730Skan TREE_TYPE (x) = error_mark_node; 6166132730Skan } 616718334Speter } 6168117421Skan 6169132730Skan if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE 6170117421Skan && flexible_array_type_p (TREE_TYPE (x))) 6171132730Skan pedwarn ("%Jinvalid use of structure with flexible array member", x); 6172117421Skan 617390075Sobrien if (DECL_NAME (x)) 617490075Sobrien saw_named_field = 1; 617518334Speter } 617618334Speter 6177132730Skan detect_field_duplicates (fieldlist); 617890075Sobrien 617918334Speter /* Now we have the nearly final fieldlist. Record it, 618018334Speter then lay out the structure or union (including the fields). */ 618118334Speter 618218334Speter TYPE_FIELDS (t) = fieldlist; 618318334Speter 618418334Speter layout_type (t); 618518334Speter 6186169699Skan /* Give bit-fields their proper types. */ 618790075Sobrien { 618890075Sobrien tree *fieldlistp = &fieldlist; 618990075Sobrien while (*fieldlistp) 6190169699Skan if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp) 6191169699Skan && TREE_TYPE (*fieldlistp) != error_mark_node) 6192169699Skan { 6193169699Skan unsigned HOST_WIDE_INT width 6194169699Skan = tree_low_cst (DECL_INITIAL (*fieldlistp), 1); 6195169699Skan tree type = TREE_TYPE (*fieldlistp); 6196169699Skan if (width != TYPE_PRECISION (type)) 6197169699Skan { 6198169699Skan TREE_TYPE (*fieldlistp) 6199169699Skan = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type)); 6200169699Skan DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp)); 6201169699Skan } 6202169699Skan DECL_INITIAL (*fieldlistp) = 0; 6203169699Skan } 620490075Sobrien else 620590075Sobrien fieldlistp = &TREE_CHAIN (*fieldlistp); 620690075Sobrien } 620718334Speter 620890075Sobrien /* Now we have the truly final field list. 620990075Sobrien Store it in this type and in the variants. */ 621018334Speter 621118334Speter TYPE_FIELDS (t) = fieldlist; 621218334Speter 6213122196Skan /* If there are lots of fields, sort so we can look through them fast. 6214132730Skan We arbitrarily consider 16 or more elts to be "a lot". */ 6215122196Skan 6216122196Skan { 6217122196Skan int len = 0; 6218122196Skan 6219122196Skan for (x = fieldlist; x; x = TREE_CHAIN (x)) 6220122196Skan { 6221169699Skan if (len > 15 || DECL_NAME (x) == NULL) 6222169699Skan break; 6223169699Skan len += 1; 6224122196Skan } 6225122196Skan 6226122196Skan if (len > 15) 6227122196Skan { 6228169699Skan tree *field_array; 6229169699Skan struct lang_type *space; 6230169699Skan struct sorted_fields_type *space2; 6231132730Skan 6232169699Skan len += list_length (x); 6233132730Skan 6234169699Skan /* Use the same allocation policy here that make_node uses, to 6235169699Skan ensure that this lives as long as the rest of the struct decl. 6236169699Skan All decls in an inline function need to be saved. */ 6237132730Skan 6238169699Skan space = GGC_CNEW (struct lang_type); 6239169699Skan space2 = GGC_NEWVAR (struct sorted_fields_type, 6240169699Skan sizeof (struct sorted_fields_type) + len * sizeof (tree)); 6241132730Skan 6242169699Skan len = 0; 6243132730Skan space->s = space2; 6244132730Skan field_array = &space2->elts[0]; 6245169699Skan for (x = fieldlist; x; x = TREE_CHAIN (x)) 6246169699Skan { 6247169699Skan field_array[len++] = x; 6248132730Skan 6249169699Skan /* If there is anonymous struct or union, break out of the loop. */ 6250169699Skan if (DECL_NAME (x) == NULL) 6251169699Skan break; 6252169699Skan } 6253169699Skan /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */ 6254169699Skan if (x == NULL) 6255169699Skan { 6256169699Skan TYPE_LANG_SPECIFIC (t) = space; 6257169699Skan TYPE_LANG_SPECIFIC (t)->s->len = len; 6258169699Skan field_array = TYPE_LANG_SPECIFIC (t)->s->elts; 6259169699Skan qsort (field_array, len, sizeof (tree), field_decl_cmp); 6260169699Skan } 6261122196Skan } 6262122196Skan } 6263132730Skan 626418334Speter for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) 626518334Speter { 626618334Speter TYPE_FIELDS (x) = TYPE_FIELDS (t); 626718334Speter TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t); 6268161660Skan C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t); 6269161660Skan C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t); 6270161660Skan C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t); 627118334Speter } 627218334Speter 627318334Speter /* If this was supposed to be a transparent union, but we can't 627418334Speter make it one, warn and turn off the flag. */ 627518334Speter if (TREE_CODE (t) == UNION_TYPE 627618334Speter && TYPE_TRANSPARENT_UNION (t) 6277146906Skan && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))) 627818334Speter { 627918334Speter TYPE_TRANSPARENT_UNION (t) = 0; 6280169699Skan warning (0, "union cannot be made transparent"); 628118334Speter } 628218334Speter 628318334Speter /* If this structure or union completes the type of any previous 628418334Speter variable declaration, lay it out and output its rtl. */ 6285132730Skan for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)); 6286132730Skan x; 6287132730Skan x = TREE_CHAIN (x)) 628818334Speter { 6289132730Skan tree decl = TREE_VALUE (x); 6290132730Skan if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) 6291132730Skan layout_array_type (TREE_TYPE (decl)); 6292132730Skan if (TREE_CODE (decl) != TYPE_DECL) 6293132730Skan { 6294132730Skan layout_decl (decl, 0); 6295132730Skan if (c_dialect_objc ()) 6296132730Skan objc_check_decl (decl); 6297169699Skan rest_of_decl_compilation (decl, toplevel, 0); 6298169699Skan if (!toplevel) 6299132730Skan expand_decl (decl); 630018334Speter } 630118334Speter } 6302132730Skan C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0; 630318334Speter 630418334Speter /* Finish debugging output for this type. */ 630518334Speter rest_of_type_compilation (t, toplevel); 630618334Speter 6307169699Skan /* If we're inside a function proper, i.e. not file-scope and not still 6308169699Skan parsing parameters, then arrange for the size of a variable sized type 6309169699Skan to be bound now. */ 6310169699Skan if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE)) 6311169699Skan add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t))); 6312169699Skan 631318334Speter return t; 631418334Speter} 631518334Speter 631618334Speter/* Lay out the type T, and its element type, and so on. */ 631718334Speter 631818334Speterstatic void 6319132730Skanlayout_array_type (tree t) 632018334Speter{ 632118334Speter if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 632218334Speter layout_array_type (TREE_TYPE (t)); 632318334Speter layout_type (t); 632418334Speter} 632518334Speter 632618334Speter/* Begin compiling the definition of an enumeration type. 632718334Speter NAME is its name (or null if anonymous). 632818334Speter Returns the type object, as yet incomplete. 632918334Speter Also records info about it so that build_enumerator 633018334Speter may be used to declare the individual values as they are read. */ 633118334Speter 633218334Spetertree 6333132730Skanstart_enum (tree name) 633418334Speter{ 633590075Sobrien tree enumtype = 0; 633618334Speter 633718334Speter /* If this is the real definition for a previous forward reference, 633818334Speter fill in the contents in the same object that used to be the 633918334Speter forward reference. */ 634018334Speter 634118334Speter if (name != 0) 6342132730Skan enumtype = lookup_tag (ENUMERAL_TYPE, name, 1); 634318334Speter 634418334Speter if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE) 634518334Speter { 634618334Speter enumtype = make_node (ENUMERAL_TYPE); 634718334Speter pushtag (name, enumtype); 634818334Speter } 634918334Speter 6350161660Skan if (C_TYPE_BEING_DEFINED (enumtype)) 6351169699Skan error ("nested redefinition of %<enum %E%>", name); 6352161660Skan 635318334Speter C_TYPE_BEING_DEFINED (enumtype) = 1; 635418334Speter 635518334Speter if (TYPE_VALUES (enumtype) != 0) 635618334Speter { 635718334Speter /* This enum is a named one that has been declared already. */ 6358169699Skan error ("redeclaration of %<enum %E%>", name); 635918334Speter 636018334Speter /* Completely replace its old definition. 636118334Speter The old enumerators remain defined, however. */ 636218334Speter TYPE_VALUES (enumtype) = 0; 636318334Speter } 636418334Speter 636518334Speter enum_next_value = integer_zero_node; 636618334Speter enum_overflow = 0; 636718334Speter 636850397Sobrien if (flag_short_enums) 636950397Sobrien TYPE_PACKED (enumtype) = 1; 637050397Sobrien 637118334Speter return enumtype; 637218334Speter} 637318334Speter 637418334Speter/* After processing and defining all the values of an enumeration type, 637518334Speter install their decls in the enumeration type and finish it off. 637618334Speter ENUMTYPE is the type object, VALUES a list of decl-value pairs, 637718334Speter and ATTRIBUTES are the specified attributes. 637818334Speter Returns ENUMTYPE. */ 637918334Speter 638018334Spetertree 6381132730Skanfinish_enum (tree enumtype, tree values, tree attributes) 638218334Speter{ 638390075Sobrien tree pair, tem; 6384169699Skan tree minnode = 0, maxnode = 0; 638590075Sobrien int precision, unsign; 6386169699Skan bool toplevel = (file_scope == current_scope); 6387169699Skan struct lang_type *lt; 638818334Speter 638990075Sobrien decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 639018334Speter 639118334Speter /* Calculate the maximum value of any enumerator in this type. */ 639218334Speter 639318334Speter if (values == error_mark_node) 639418334Speter minnode = maxnode = integer_zero_node; 639518334Speter else 639690075Sobrien { 639790075Sobrien minnode = maxnode = TREE_VALUE (values); 639890075Sobrien for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair)) 639990075Sobrien { 640090075Sobrien tree value = TREE_VALUE (pair); 640190075Sobrien if (tree_int_cst_lt (maxnode, value)) 640290075Sobrien maxnode = value; 640390075Sobrien if (tree_int_cst_lt (value, minnode)) 640490075Sobrien minnode = value; 640590075Sobrien } 640690075Sobrien } 640718334Speter 640890075Sobrien /* Construct the final type of this enumeration. It is the same 640990075Sobrien as one of the integral types - the narrowest one that fits, except 641090075Sobrien that normally we only go as narrow as int - and signed iff any of 641190075Sobrien the values are negative. */ 641290075Sobrien unsign = (tree_int_cst_sgn (minnode) >= 0); 641390075Sobrien precision = MAX (min_precision (minnode, unsign), 641490075Sobrien min_precision (maxnode, unsign)); 6415169699Skan 641650397Sobrien if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node)) 641750397Sobrien { 6418169699Skan tem = c_common_type_for_size (precision, unsign); 6419169699Skan if (tem == NULL) 642050397Sobrien { 6421169699Skan warning (0, "enumeration values exceed range of largest integer"); 6422169699Skan tem = long_long_integer_type_node; 642350397Sobrien } 642450397Sobrien } 642518334Speter else 6426169699Skan tem = unsign ? unsigned_type_node : integer_type_node; 642718334Speter 6428169699Skan TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem); 6429169699Skan TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem); 6430169699Skan TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem); 643118334Speter TYPE_SIZE (enumtype) = 0; 6432146906Skan 6433146906Skan /* If the precision of the type was specific with an attribute and it 6434146906Skan was too small, give an error. Otherwise, use it. */ 6435146906Skan if (TYPE_PRECISION (enumtype)) 6436146906Skan { 6437146906Skan if (precision > TYPE_PRECISION (enumtype)) 6438146906Skan error ("specified mode too small for enumeral values"); 6439146906Skan } 6440146906Skan else 6441169699Skan TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem); 6442146906Skan 644318334Speter layout_type (enumtype); 644418334Speter 644518334Speter if (values != error_mark_node) 644618334Speter { 644790075Sobrien /* Change the type of the enumerators to be the enum type. We 644890075Sobrien need to do this irrespective of the size of the enum, for 644990075Sobrien proper type checking. Replace the DECL_INITIALs of the 645090075Sobrien enumerators, and the value slots of the list, with copies 645190075Sobrien that have the enum type; they cannot be modified in place 645290075Sobrien because they may be shared (e.g. integer_zero_node) Finally, 645390075Sobrien change the purpose slots to point to the names of the decls. */ 645418334Speter for (pair = values; pair; pair = TREE_CHAIN (pair)) 645518334Speter { 645690075Sobrien tree enu = TREE_PURPOSE (pair); 6457169699Skan tree ini = DECL_INITIAL (enu); 645890075Sobrien 645990075Sobrien TREE_TYPE (enu) = enumtype; 646090075Sobrien 646190075Sobrien /* The ISO C Standard mandates enumerators to have type int, 646290075Sobrien even though the underlying type of an enum type is 646390075Sobrien unspecified. Here we convert any enumerators that fit in 646490075Sobrien an int to type int, to avoid promotions to unsigned types 646590075Sobrien when comparing integers with enumerators that fit in the 646690075Sobrien int range. When -pedantic is given, build_enumerator() 646790075Sobrien would have already taken care of those that don't fit. */ 6468169699Skan if (int_fits_type_p (ini, integer_type_node)) 6469169699Skan tem = integer_type_node; 647090075Sobrien else 6471169699Skan tem = enumtype; 6472169699Skan ini = convert (tem, ini); 647390075Sobrien 6474169699Skan DECL_INITIAL (enu) = ini; 647590075Sobrien TREE_PURPOSE (pair) = DECL_NAME (enu); 6476169699Skan TREE_VALUE (pair) = ini; 647718334Speter } 647818334Speter 647918334Speter TYPE_VALUES (enumtype) = values; 648018334Speter } 648118334Speter 6482169699Skan /* Record the min/max values so that we can warn about bit-field 6483169699Skan enumerations that are too small for the values. */ 6484169699Skan lt = GGC_CNEW (struct lang_type); 6485169699Skan lt->enum_min = minnode; 6486169699Skan lt->enum_max = maxnode; 6487169699Skan TYPE_LANG_SPECIFIC (enumtype) = lt; 6488169699Skan 648918334Speter /* Fix up all variant types of this enum type. */ 649018334Speter for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem)) 649118334Speter { 649290075Sobrien if (tem == enumtype) 649390075Sobrien continue; 649418334Speter TYPE_VALUES (tem) = TYPE_VALUES (enumtype); 649518334Speter TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype); 649618334Speter TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype); 649718334Speter TYPE_SIZE (tem) = TYPE_SIZE (enumtype); 649850397Sobrien TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype); 649918334Speter TYPE_MODE (tem) = TYPE_MODE (enumtype); 650018334Speter TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype); 650118334Speter TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype); 650290075Sobrien TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype); 6503169699Skan TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype); 6504169699Skan TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype); 650518334Speter } 650618334Speter 650718334Speter /* Finish debugging output for this type. */ 650818334Speter rest_of_type_compilation (enumtype, toplevel); 650918334Speter 651018334Speter return enumtype; 651118334Speter} 651218334Speter 651318334Speter/* Build and install a CONST_DECL for one value of the 651418334Speter current enumeration type (one that was begun with start_enum). 651518334Speter Return a tree-list containing the CONST_DECL and its value. 651618334Speter Assignment of sequential values by default is handled here. */ 651718334Speter 651818334Spetertree 6519132730Skanbuild_enumerator (tree name, tree value) 652018334Speter{ 652190075Sobrien tree decl, type; 652218334Speter 652318334Speter /* Validate and default VALUE. */ 652418334Speter 652518334Speter if (value != 0) 652618334Speter { 6527169699Skan /* Don't issue more errors for error_mark_node (i.e. an 6528169699Skan undeclared identifier) - just ignore the value expression. */ 6529169699Skan if (value == error_mark_node) 6530169699Skan value = 0; 6531169699Skan else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)) 6532169699Skan || TREE_CODE (value) != INTEGER_CST) 653318334Speter { 6534169699Skan error ("enumerator value for %qE is not an integer constant", name); 6535169699Skan value = 0; 653618334Speter } 653718334Speter else 653818334Speter { 6539169699Skan value = default_conversion (value); 6540169699Skan constant_expression_warning (value); 654118334Speter } 654218334Speter } 654318334Speter 654418334Speter /* Default based on previous value. */ 654518334Speter /* It should no longer be possible to have NON_LVALUE_EXPR 654618334Speter in the default. */ 654718334Speter if (value == 0) 654818334Speter { 654918334Speter value = enum_next_value; 655018334Speter if (enum_overflow) 655118334Speter error ("overflow in enumeration values"); 655218334Speter } 655318334Speter 6554169699Skan if (pedantic && !int_fits_type_p (value, integer_type_node)) 655518334Speter { 6556169699Skan pedwarn ("ISO C restricts enumerator values to range of %<int%>"); 6557169699Skan /* XXX This causes -pedantic to change the meaning of the program. 6558169699Skan Remove? -zw 2004-03-15 */ 655990075Sobrien value = convert (integer_type_node, value); 656018334Speter } 656118334Speter 656218334Speter /* Set basis for default for next value. */ 656318334Speter enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0); 656418334Speter enum_overflow = tree_int_cst_lt (enum_next_value, value); 656518334Speter 656618334Speter /* Now create a declaration for the enum value name. */ 656718334Speter 656818334Speter type = TREE_TYPE (value); 6569117421Skan type = c_common_type_for_size (MAX (TYPE_PRECISION (type), 6570117421Skan TYPE_PRECISION (integer_type_node)), 6571117421Skan (TYPE_PRECISION (type) 6572117421Skan >= TYPE_PRECISION (integer_type_node) 6573169699Skan && TYPE_UNSIGNED (type))); 657418334Speter 657518334Speter decl = build_decl (CONST_DECL, name, type); 657690075Sobrien DECL_INITIAL (decl) = convert (type, value); 657718334Speter pushdecl (decl); 657818334Speter 657990075Sobrien return tree_cons (decl, value, NULL_TREE); 658018334Speter} 658190075Sobrien 658218334Speter 658318334Speter/* Create the FUNCTION_DECL for a function definition. 658490075Sobrien DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of 658518334Speter the declaration; they describe the function's name and the type it returns, 658618334Speter but twisted together in a fashion that parallels the syntax of C. 658718334Speter 658818334Speter This function creates a binding context for the function body 658918334Speter as well as setting up the FUNCTION_DECL in current_function_decl. 659018334Speter 659118334Speter Returns 1 on success. If the DECLARATOR is not suitable for a function 659218334Speter (it defines a datum instead), we return 0, which tells 659390075Sobrien yyparse to report a parse error. */ 659418334Speter 659518334Speterint 6596169699Skanstart_function (struct c_declspecs *declspecs, struct c_declarator *declarator, 6597169699Skan tree attributes) 659818334Speter{ 659918334Speter tree decl1, old_decl; 6600169699Skan tree restype, resdecl; 6601169699Skan struct c_label_context_se *nstack_se; 6602169699Skan struct c_label_context_vm *nstack_vm; 660318334Speter 660450397Sobrien current_function_returns_value = 0; /* Assume, until we see it does. */ 660518334Speter current_function_returns_null = 0; 660696263Sobrien current_function_returns_abnormally = 0; 660718334Speter warn_about_return_type = 0; 6608169699Skan c_switch_stack = NULL; 660918334Speter 6610169699Skan nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se); 6611169699Skan nstack_se->labels_def = NULL; 6612169699Skan nstack_se->labels_used = NULL; 6613169699Skan nstack_se->next = label_context_stack_se; 6614169699Skan label_context_stack_se = nstack_se; 661518334Speter 6616169699Skan nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm); 6617169699Skan nstack_vm->labels_def = NULL; 6618169699Skan nstack_vm->labels_used = NULL; 6619169699Skan nstack_vm->scope = 0; 6620169699Skan nstack_vm->next = label_context_stack_vm; 6621169699Skan label_context_stack_vm = nstack_vm; 662218334Speter 6623169699Skan /* Indicate no valid break/continue context by setting these variables 6624169699Skan to some non-null, non-label value. We'll notice and emit the proper 6625169699Skan error message in c_finish_bc_stmt. */ 6626169699Skan c_break_label = c_cont_label = size_zero_node; 6627169699Skan 6628169699Skan decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL); 6629169699Skan 663018334Speter /* If the declarator is not suitable for a function definition, 663118334Speter cause a syntax error. */ 663218334Speter if (decl1 == 0) 663350397Sobrien { 6634169699Skan label_context_stack_se = label_context_stack_se->next; 6635169699Skan label_context_stack_vm = label_context_stack_vm->next; 663650397Sobrien return 0; 663750397Sobrien } 663818334Speter 663990075Sobrien decl_attributes (&decl1, attributes, 0); 664018334Speter 664190075Sobrien if (DECL_DECLARED_INLINE_P (decl1) 664290075Sobrien && DECL_UNINLINABLE (decl1) 664390075Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1))) 6644169699Skan warning (OPT_Wattributes, "inline function %q+D given attribute noinline", 6645169699Skan decl1); 664690075Sobrien 6647189824Sdas /* Handle gnu_inline attribute. */ 6648189824Sdas if (declspecs->inline_p 6649189824Sdas && !flag_gnu89_inline 6650189824Sdas && TREE_CODE (decl1) == FUNCTION_DECL 6651189824Sdas && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))) 6652189824Sdas { 6653189824Sdas if (declspecs->storage_class != csc_static) 6654189824Sdas DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1); 6655189824Sdas } 6656189824Sdas 665718334Speter announce_function (decl1); 665818334Speter 665990075Sobrien if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1)))) 666018334Speter { 666190075Sobrien error ("return type is an incomplete type"); 666218334Speter /* Make it return void instead. */ 666318334Speter TREE_TYPE (decl1) 666418334Speter = build_function_type (void_type_node, 666518334Speter TYPE_ARG_TYPES (TREE_TYPE (decl1))); 666618334Speter } 666718334Speter 666818334Speter if (warn_about_return_type) 6669169699Skan pedwarn_c99 ("return type defaults to %<int%>"); 667018334Speter 667118334Speter /* Make the init_value nonzero so pushdecl knows this is not tentative. 6672169699Skan error_mark_node is replaced below (in pop_scope) with the BLOCK. */ 667318334Speter DECL_INITIAL (decl1) = error_mark_node; 667418334Speter 667518334Speter /* If this definition isn't a prototype and we had a prototype declaration 6676169699Skan before, copy the arg type info from that prototype. */ 6677169699Skan old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope); 6678169699Skan if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL) 6679169699Skan old_decl = 0; 6680169699Skan current_function_prototype_locus = UNKNOWN_LOCATION; 6681169699Skan current_function_prototype_built_in = false; 6682169699Skan current_function_prototype_arg_types = NULL_TREE; 6683169699Skan if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0) 668418334Speter { 6685169699Skan if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE 6686169699Skan && comptypes (TREE_TYPE (TREE_TYPE (decl1)), 6687169699Skan TREE_TYPE (TREE_TYPE (old_decl)))) 6688169699Skan { 6689169699Skan TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl), 6690169699Skan TREE_TYPE (decl1)); 6691169699Skan current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl); 6692169699Skan current_function_prototype_built_in 6693169699Skan = C_DECL_BUILTIN_PROTOTYPE (old_decl); 6694169699Skan current_function_prototype_arg_types 6695169699Skan = TYPE_ARG_TYPES (TREE_TYPE (decl1)); 6696169699Skan } 6697169699Skan if (TREE_PUBLIC (decl1)) 6698169699Skan { 6699169699Skan /* If there is an external prototype declaration of this 6700169699Skan function, record its location but do not copy information 6701169699Skan to this decl. This may be an invisible declaration 6702169699Skan (built-in or in a scope which has finished) or simply 6703169699Skan have more refined argument types than any declaration 6704169699Skan found above. */ 6705169699Skan struct c_binding *b; 6706169699Skan for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed) 6707169699Skan if (B_IN_SCOPE (b, external_scope)) 6708169699Skan break; 6709169699Skan if (b) 6710169699Skan { 6711169699Skan tree ext_decl, ext_type; 6712169699Skan ext_decl = b->decl; 6713169699Skan ext_type = b->type ? b->type : TREE_TYPE (ext_decl); 6714169699Skan if (TREE_CODE (ext_type) == FUNCTION_TYPE 6715169699Skan && comptypes (TREE_TYPE (TREE_TYPE (decl1)), 6716169699Skan TREE_TYPE (ext_type))) 6717169699Skan { 6718169699Skan current_function_prototype_locus 6719169699Skan = DECL_SOURCE_LOCATION (ext_decl); 6720169699Skan current_function_prototype_built_in 6721169699Skan = C_DECL_BUILTIN_PROTOTYPE (ext_decl); 6722169699Skan current_function_prototype_arg_types 6723169699Skan = TYPE_ARG_TYPES (ext_type); 6724169699Skan } 6725169699Skan } 6726169699Skan } 672718334Speter } 672818334Speter 672918334Speter /* Optionally warn of old-fashioned def with no previous prototype. */ 673018334Speter if (warn_strict_prototypes 6731169699Skan && old_decl != error_mark_node 673218334Speter && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0 6733132730Skan && C_DECL_ISNT_PROTOTYPE (old_decl)) 6734169699Skan warning (OPT_Wstrict_prototypes, 6735169699Skan "function declaration isn%'t a prototype"); 673618334Speter /* Optionally warn of any global def with no previous prototype. */ 673718334Speter else if (warn_missing_prototypes 6738169699Skan && old_decl != error_mark_node 673918334Speter && TREE_PUBLIC (decl1) 6740169699Skan && !MAIN_NAME_P (DECL_NAME (decl1)) 6741132730Skan && C_DECL_ISNT_PROTOTYPE (old_decl)) 6742169699Skan warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1); 674318334Speter /* Optionally warn of any def with no previous prototype 674418334Speter if the function has already been used. */ 674518334Speter else if (warn_missing_prototypes 6746169699Skan && old_decl != 0 6747169699Skan && old_decl != error_mark_node 6748169699Skan && TREE_USED (old_decl) 674918334Speter && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0) 6750169699Skan warning (OPT_Wmissing_prototypes, 6751169699Skan "%q+D was used with no prototype before its definition", decl1); 675218334Speter /* Optionally warn of any global def with no previous declaration. */ 675318334Speter else if (warn_missing_declarations 675418334Speter && TREE_PUBLIC (decl1) 675518334Speter && old_decl == 0 6756169699Skan && !MAIN_NAME_P (DECL_NAME (decl1))) 6757169699Skan warning (OPT_Wmissing_declarations, "no previous declaration for %q+D", 6758169699Skan decl1); 675918334Speter /* Optionally warn of any def with no previous declaration 676018334Speter if the function has already been used. */ 676118334Speter else if (warn_missing_declarations 6762169699Skan && old_decl != 0 6763169699Skan && old_decl != error_mark_node 6764169699Skan && TREE_USED (old_decl) 6765132730Skan && C_DECL_IMPLICIT (old_decl)) 6766169699Skan warning (OPT_Wmissing_declarations, 6767169699Skan "%q+D was used with no declaration before its definition", decl1); 676818334Speter 676918334Speter /* This function exists in static storage. 677018334Speter (This does not mean `static' in the C sense!) */ 677118334Speter TREE_STATIC (decl1) = 1; 677218334Speter 677318334Speter /* A nested function is not global. */ 677418334Speter if (current_function_decl != 0) 677518334Speter TREE_PUBLIC (decl1) = 0; 677618334Speter 6777132730Skan /* This is the earliest point at which we might know the assembler 6778132730Skan name of the function. Thus, if it's set before this, die horribly. */ 6779169699Skan gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1)); 6780132730Skan 6781132730Skan /* If #pragma weak was used, mark the decl weak now. */ 6782169699Skan if (current_scope == file_scope) 6783132730Skan maybe_apply_pragma_weak (decl1); 6784132730Skan 678590075Sobrien /* Warn for unlikely, improbable, or stupid declarations of `main'. */ 678690075Sobrien if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1))) 678750397Sobrien { 678850397Sobrien tree args; 678950397Sobrien int argct = 0; 679050397Sobrien 679150397Sobrien if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1))) 679290075Sobrien != integer_type_node) 6793169699Skan pedwarn ("return type of %q+D is not %<int%>", decl1); 679450397Sobrien 679550397Sobrien for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args; 679650397Sobrien args = TREE_CHAIN (args)) 679750397Sobrien { 679850397Sobrien tree type = args ? TREE_VALUE (args) : 0; 679950397Sobrien 680050397Sobrien if (type == void_type_node) 680150397Sobrien break; 680250397Sobrien 680350397Sobrien ++argct; 680450397Sobrien switch (argct) 680550397Sobrien { 680650397Sobrien case 1: 680750397Sobrien if (TYPE_MAIN_VARIANT (type) != integer_type_node) 6808169699Skan pedwarn ("first argument of %q+D should be %<int%>", decl1); 680950397Sobrien break; 681050397Sobrien 681150397Sobrien case 2: 681250397Sobrien if (TREE_CODE (type) != POINTER_TYPE 681350397Sobrien || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 681450397Sobrien || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 681550397Sobrien != char_type_node)) 6816169699Skan pedwarn ("second argument of %q+D should be %<char **%>", 6817169699Skan decl1); 681850397Sobrien break; 681950397Sobrien 682050397Sobrien case 3: 682150397Sobrien if (TREE_CODE (type) != POINTER_TYPE 682250397Sobrien || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 682350397Sobrien || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 682450397Sobrien != char_type_node)) 6825169699Skan pedwarn ("third argument of %q+D should probably be " 6826169699Skan "%<char **%>", decl1); 682750397Sobrien break; 682850397Sobrien } 682950397Sobrien } 683050397Sobrien 683150397Sobrien /* It is intentional that this message does not mention the third 683290075Sobrien argument because it's only mentioned in an appendix of the 683390075Sobrien standard. */ 683450397Sobrien if (argct > 0 && (argct < 2 || argct > 3)) 6835169699Skan pedwarn ("%q+D takes only zero or two arguments", decl1); 683650397Sobrien 6837169699Skan if (!TREE_PUBLIC (decl1)) 6838169699Skan pedwarn ("%q+D is normally a non-static function", decl1); 683950397Sobrien } 684050397Sobrien 684118334Speter /* Record the decl so that the function name is defined. 684218334Speter If we already have a decl for this name, and it is a FUNCTION_DECL, 684318334Speter use the old decl. */ 684418334Speter 684518334Speter current_function_decl = pushdecl (decl1); 684618334Speter 6847169699Skan push_scope (); 6848132730Skan declare_parm_level (); 684918334Speter 685018334Speter restype = TREE_TYPE (TREE_TYPE (current_function_decl)); 685118334Speter /* Promote the value to int before returning it. */ 685290075Sobrien if (c_promoting_integer_type_p (restype)) 685318334Speter { 6854117421Skan /* It retains unsignedness if not really getting wider. */ 6855169699Skan if (TYPE_UNSIGNED (restype) 6856117421Skan && (TYPE_PRECISION (restype) 6857117421Skan == TYPE_PRECISION (integer_type_node))) 685818334Speter restype = unsigned_type_node; 685918334Speter else 686018334Speter restype = integer_type_node; 686118334Speter } 686218334Speter 6863169699Skan resdecl = build_decl (RESULT_DECL, NULL_TREE, restype); 6864169699Skan DECL_ARTIFICIAL (resdecl) = 1; 6865169699Skan DECL_IGNORED_P (resdecl) = 1; 6866169699Skan DECL_RESULT (current_function_decl) = resdecl; 686718334Speter 686890075Sobrien start_fname_decls (); 6869132730Skan 687018334Speter return 1; 687118334Speter} 687218334Speter 6873132730Skan/* Subroutine of store_parm_decls which handles new-style function 6874132730Skan definitions (prototype format). The parms already have decls, so we 6875132730Skan need only record them as in effect and complain if any redundant 6876132730Skan old-style parm decls were written. */ 6877132730Skanstatic void 6878169699Skanstore_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info) 687918334Speter{ 6880169699Skan tree decl; 688118334Speter 6882169699Skan if (current_scope->bindings) 6883132730Skan { 6884132730Skan error ("%Jold-style parameter declarations in prototyped " 6885132730Skan "function definition", fndecl); 688618334Speter 6887132730Skan /* Get rid of the old-style declarations. */ 6888169699Skan pop_scope (); 6889169699Skan push_scope (); 6890132730Skan } 6891169699Skan /* Don't issue this warning for nested functions, and don't issue this 6892169699Skan warning if we got here because ARG_INFO_TYPES was error_mark_node 6893169699Skan (this happens when a function definition has just an ellipsis in 6894169699Skan its parameter list). */ 6895169699Skan else if (!in_system_header && !current_function_scope 6896169699Skan && arg_info->types != error_mark_node) 6897169699Skan warning (OPT_Wtraditional, 6898169699Skan "%Jtraditional C rejects ISO C style function definitions", 6899169699Skan fndecl); 690018334Speter 6901132730Skan /* Now make all the parameter declarations visible in the function body. 6902132730Skan We can bypass most of the grunt work of pushdecl. */ 6903169699Skan for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl)) 6904132730Skan { 6905132730Skan DECL_CONTEXT (decl) = current_function_decl; 6906169699Skan if (DECL_NAME (decl)) 6907132730Skan { 6908169699Skan bind (DECL_NAME (decl), decl, current_scope, 6909169699Skan /*invisible=*/false, /*nested=*/false); 6910169699Skan if (!TREE_USED (decl)) 6911169699Skan warn_if_shadowing (decl); 6912132730Skan } 6913169699Skan else 6914169699Skan error ("%Jparameter name omitted", decl); 6915132730Skan } 691618334Speter 6917132730Skan /* Record the parameter list in the function declaration. */ 6918169699Skan DECL_ARGUMENTS (fndecl) = arg_info->parms; 691918334Speter 6920132730Skan /* Now make all the ancillary declarations visible, likewise. */ 6921169699Skan for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl)) 6922132730Skan { 6923132730Skan DECL_CONTEXT (decl) = current_function_decl; 6924169699Skan if (DECL_NAME (decl)) 6925169699Skan bind (DECL_NAME (decl), decl, current_scope, 6926169699Skan /*invisible=*/false, /*nested=*/false); 6927132730Skan } 692890075Sobrien 6929132730Skan /* And all the tag declarations. */ 6930169699Skan for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl)) 6931132730Skan if (TREE_PURPOSE (decl)) 6932169699Skan bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope, 6933169699Skan /*invisible=*/false, /*nested=*/false); 6934132730Skan} 693518334Speter 6936132730Skan/* Subroutine of store_parm_decls which handles old-style function 6937132730Skan definitions (separate parameter list and declarations). */ 693890075Sobrien 6939132730Skanstatic void 6940169699Skanstore_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info) 6941132730Skan{ 6942169699Skan struct c_binding *b; 6943132730Skan tree parm, decl, last; 6944169699Skan tree parmids = arg_info->parms; 6945169699Skan struct pointer_set_t *seen_args = pointer_set_create (); 694690075Sobrien 6947169699Skan if (!in_system_header) 6948169699Skan warning (OPT_Wold_style_definition, "%Jold-style function definition", 6949169699Skan fndecl); 695018334Speter 6951132730Skan /* Match each formal parameter name with its declaration. Save each 6952132730Skan decl in the appropriate TREE_PURPOSE slot of the parmids chain. */ 6953132730Skan for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 6954132730Skan { 6955132730Skan if (TREE_VALUE (parm) == 0) 695618334Speter { 6957132730Skan error ("%Jparameter name missing from parameter list", fndecl); 6958132730Skan TREE_PURPOSE (parm) = 0; 6959132730Skan continue; 696018334Speter } 696118334Speter 6962169699Skan b = I_SYMBOL_BINDING (TREE_VALUE (parm)); 6963169699Skan if (b && B_IN_CURRENT_SCOPE (b)) 696418334Speter { 6965169699Skan decl = b->decl; 6966132730Skan /* If we got something other than a PARM_DECL it is an error. */ 6967132730Skan if (TREE_CODE (decl) != PARM_DECL) 6968169699Skan error ("%q+D declared as a non-parameter", decl); 6969132730Skan /* If the declaration is already marked, we have a duplicate 6970132730Skan name. Complain and ignore the duplicate. */ 6971169699Skan else if (pointer_set_contains (seen_args, decl)) 697218334Speter { 6973169699Skan error ("multiple parameters named %q+D", decl); 6974132730Skan TREE_PURPOSE (parm) = 0; 6975132730Skan continue; 697618334Speter } 6977132730Skan /* If the declaration says "void", complain and turn it into 6978132730Skan an int. */ 6979132730Skan else if (VOID_TYPE_P (TREE_TYPE (decl))) 698018334Speter { 6981169699Skan error ("parameter %q+D declared with void type", decl); 6982132730Skan TREE_TYPE (decl) = integer_type_node; 6983132730Skan DECL_ARG_TYPE (decl) = integer_type_node; 6984132730Skan layout_decl (decl, 0); 698518334Speter } 6986169699Skan warn_if_shadowing (decl); 698718334Speter } 6988132730Skan /* If no declaration found, default to int. */ 6989132730Skan else 699018334Speter { 6991132730Skan decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node); 6992132730Skan DECL_ARG_TYPE (decl) = TREE_TYPE (decl); 6993132730Skan DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl); 6994132730Skan pushdecl (decl); 6995169699Skan warn_if_shadowing (decl); 699618334Speter 6997132730Skan if (flag_isoc99) 6998169699Skan pedwarn ("type of %q+D defaults to %<int%>", decl); 6999132730Skan else if (extra_warnings) 7000169699Skan warning (OPT_Wextra, "type of %q+D defaults to %<int%>", decl); 700118334Speter } 700218334Speter 7003132730Skan TREE_PURPOSE (parm) = decl; 7004169699Skan pointer_set_insert (seen_args, decl); 700518334Speter } 700618334Speter 7007132730Skan /* Now examine the parms chain for incomplete declarations 7008132730Skan and declarations with no corresponding names. */ 700918334Speter 7010169699Skan for (b = current_scope->bindings; b; b = b->prev) 7011132730Skan { 7012169699Skan parm = b->decl; 7013169699Skan if (TREE_CODE (parm) != PARM_DECL) 7014169699Skan continue; 7015169699Skan 7016169699Skan if (TREE_TYPE (parm) != error_mark_node 7017169699Skan && !COMPLETE_TYPE_P (TREE_TYPE (parm))) 7018132730Skan { 7019169699Skan error ("parameter %q+D has incomplete type", parm); 7020132730Skan TREE_TYPE (parm) = error_mark_node; 7021132730Skan } 702218334Speter 7023169699Skan if (!pointer_set_contains (seen_args, parm)) 702418334Speter { 7025169699Skan error ("declaration for parameter %q+D but no such parameter", parm); 702618334Speter 7027132730Skan /* Pretend the parameter was not missing. 7028132730Skan This gets us to a standard state and minimizes 7029132730Skan further error messages. */ 7030132730Skan parmids = chainon (parmids, tree_cons (parm, 0, 0)); 7031132730Skan } 7032132730Skan } 703318334Speter 7034132730Skan /* Chain the declarations together in the order of the list of 7035132730Skan names. Store that chain in the function decl, replacing the 7036132730Skan list of names. Update the current scope to match. */ 7037132730Skan DECL_ARGUMENTS (fndecl) = 0; 703818334Speter 7039132730Skan for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 7040132730Skan if (TREE_PURPOSE (parm)) 7041132730Skan break; 7042132730Skan if (parm && TREE_PURPOSE (parm)) 7043132730Skan { 7044132730Skan last = TREE_PURPOSE (parm); 7045132730Skan DECL_ARGUMENTS (fndecl) = last; 704618334Speter 7047132730Skan for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm)) 7048132730Skan if (TREE_PURPOSE (parm)) 7049132730Skan { 7050132730Skan TREE_CHAIN (last) = TREE_PURPOSE (parm); 7051132730Skan last = TREE_PURPOSE (parm); 7052132730Skan } 7053132730Skan TREE_CHAIN (last) = 0; 7054132730Skan } 705518334Speter 7056169699Skan pointer_set_destroy (seen_args); 7057169699Skan 7058132730Skan /* If there was a previous prototype, 7059132730Skan set the DECL_ARG_TYPE of each argument according to 7060132730Skan the type previously specified, and report any mismatches. */ 706118334Speter 7062169699Skan if (current_function_prototype_arg_types) 7063132730Skan { 7064132730Skan tree type; 7065132730Skan for (parm = DECL_ARGUMENTS (fndecl), 7066169699Skan type = current_function_prototype_arg_types; 7067132730Skan parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) 7068132730Skan != void_type_node)); 7069132730Skan parm = TREE_CHAIN (parm), type = TREE_CHAIN (type)) 707018334Speter { 7071132730Skan if (parm == 0 || type == 0 7072132730Skan || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node) 707318334Speter { 7074169699Skan if (current_function_prototype_built_in) 7075169699Skan warning (0, "number of arguments doesn%'t match " 7076169699Skan "built-in prototype"); 7077169699Skan else 7078169699Skan { 7079169699Skan error ("number of arguments doesn%'t match prototype"); 7080169699Skan error ("%Hprototype declaration", 7081169699Skan ¤t_function_prototype_locus); 7082169699Skan } 7083132730Skan break; 708418334Speter } 7085132730Skan /* Type for passing arg must be consistent with that 7086132730Skan declared for the arg. ISO C says we take the unqualified 7087132730Skan type for parameters declared with qualified type. */ 7088169699Skan if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)), 7089169699Skan TYPE_MAIN_VARIANT (TREE_VALUE (type)))) 709018334Speter { 7091132730Skan if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) 7092132730Skan == TYPE_MAIN_VARIANT (TREE_VALUE (type))) 709318334Speter { 7094132730Skan /* Adjust argument to match prototype. E.g. a previous 7095132730Skan `int foo(float);' prototype causes 7096132730Skan `int foo(x) float x; {...}' to be treated like 7097132730Skan `int foo(float x) {...}'. This is particularly 7098132730Skan useful for argument types like uid_t. */ 7099132730Skan DECL_ARG_TYPE (parm) = TREE_TYPE (parm); 710090075Sobrien 7101132730Skan if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl)) 7102132730Skan && INTEGRAL_TYPE_P (TREE_TYPE (parm)) 7103132730Skan && TYPE_PRECISION (TREE_TYPE (parm)) 7104132730Skan < TYPE_PRECISION (integer_type_node)) 7105132730Skan DECL_ARG_TYPE (parm) = integer_type_node; 710690075Sobrien 7107132730Skan if (pedantic) 710818334Speter { 7109169699Skan /* ??? Is it possible to get here with a 7110169699Skan built-in prototype or will it always have 7111169699Skan been diagnosed as conflicting with an 7112169699Skan old-style definition and discarded? */ 7113169699Skan if (current_function_prototype_built_in) 7114169699Skan warning (0, "promoted argument %qD " 7115169699Skan "doesn%'t match built-in prototype", parm); 7116169699Skan else 7117169699Skan { 7118169699Skan pedwarn ("promoted argument %qD " 7119169699Skan "doesn%'t match prototype", parm); 7120169699Skan pedwarn ("%Hprototype declaration", 7121169699Skan ¤t_function_prototype_locus); 7122169699Skan } 712318334Speter } 712418334Speter } 7125132730Skan else 7126132730Skan { 7127169699Skan if (current_function_prototype_built_in) 7128169699Skan warning (0, "argument %qD doesn%'t match " 7129169699Skan "built-in prototype", parm); 7130169699Skan else 7131169699Skan { 7132169699Skan error ("argument %qD doesn%'t match prototype", parm); 7133169699Skan error ("%Hprototype declaration", 7134169699Skan ¤t_function_prototype_locus); 7135169699Skan } 7136132730Skan } 713718334Speter } 713818334Speter } 7139132730Skan TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0; 7140132730Skan } 714118334Speter 7142132730Skan /* Otherwise, create a prototype that would match. */ 714318334Speter 7144132730Skan else 7145132730Skan { 7146132730Skan tree actual = 0, last = 0, type; 7147132730Skan 7148132730Skan for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm)) 714918334Speter { 7150132730Skan type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE); 715118334Speter if (last) 715218334Speter TREE_CHAIN (last) = type; 715318334Speter else 715418334Speter actual = type; 7155132730Skan last = type; 7156132730Skan } 7157132730Skan type = tree_cons (NULL_TREE, void_type_node, NULL_TREE); 7158132730Skan if (last) 7159132730Skan TREE_CHAIN (last) = type; 7160132730Skan else 7161132730Skan actual = type; 716218334Speter 7163132730Skan /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES 7164132730Skan of the type of this function, but we need to avoid having this 7165132730Skan affect the types of other similarly-typed functions, so we must 7166132730Skan first force the generation of an identical (but separate) type 7167132730Skan node for the relevant function type. The new node we create 7168132730Skan will be a variant of the main variant of the original function 7169132730Skan type. */ 717018334Speter 7171169699Skan TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl)); 717218334Speter 7173132730Skan TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual; 7174132730Skan } 7175132730Skan} 717618334Speter 7177169699Skan/* Store parameter declarations passed in ARG_INFO into the current 7178169699Skan function declaration. */ 7179169699Skan 7180169699Skanvoid 7181169699Skanstore_parm_decls_from (struct c_arg_info *arg_info) 7182169699Skan{ 7183169699Skan current_function_arg_info = arg_info; 7184169699Skan store_parm_decls (); 7185169699Skan} 7186169699Skan 7187132730Skan/* Store the parameter declarations into the current function declaration. 7188132730Skan This is called after parsing the parameter declarations, before 7189132730Skan digesting the body of the function. 719018334Speter 7191132730Skan For an old-style definition, construct a prototype out of the old-style 7192132730Skan parameter declarations and inject it into the function's type. */ 719318334Speter 7194132730Skanvoid 7195132730Skanstore_parm_decls (void) 7196132730Skan{ 7197132730Skan tree fndecl = current_function_decl; 7198169699Skan bool proto; 719918334Speter 7200169699Skan /* The argument information block for FNDECL. */ 7201169699Skan struct c_arg_info *arg_info = current_function_arg_info; 7202169699Skan current_function_arg_info = 0; 720318334Speter 7204169699Skan /* True if this definition is written with a prototype. Note: 7205169699Skan despite C99 6.7.5.3p14, we can *not* treat an empty argument 7206169699Skan list in a function definition as equivalent to (void) -- an 7207169699Skan empty argument list specifies the function has no parameters, 7208169699Skan but only (void) sets up a prototype for future calls. */ 7209169699Skan proto = arg_info->types != 0; 7210169699Skan 7211169699Skan if (proto) 7212169699Skan store_parm_decls_newstyle (fndecl, arg_info); 7213132730Skan else 7214169699Skan store_parm_decls_oldstyle (fndecl, arg_info); 7215132730Skan 7216169699Skan /* The next call to push_scope will be a function body. */ 7217132730Skan 7218132730Skan next_is_function_body = true; 7219132730Skan 722018334Speter /* Write a record describing this function definition to the prototypes 722118334Speter file (if requested). */ 722218334Speter 7223169699Skan gen_aux_info_record (fndecl, 1, 0, proto); 722418334Speter 722518334Speter /* Initialize the RTL code for the function. */ 7226132730Skan allocate_struct_function (fndecl); 722718334Speter 722890075Sobrien /* Begin the statement tree for this function. */ 7229169699Skan DECL_SAVED_TREE (fndecl) = push_stmt_list (); 723018334Speter 7231169699Skan /* ??? Insert the contents of the pending sizes list into the function 7232169699Skan to be evaluated. The only reason left to have this is 7233169699Skan void foo(int n, int array[n++]) 7234169699Skan because we throw away the array type in favor of a pointer type, and 7235169699Skan thus won't naturally see the SAVE_EXPR containing the increment. All 7236169699Skan other pending sizes would be handled by gimplify_parameters. */ 7237169699Skan { 7238169699Skan tree t; 7239169699Skan for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t)) 7240169699Skan add_stmt (TREE_VALUE (t)); 7241169699Skan } 724218334Speter 724390075Sobrien /* Even though we're inside a function body, we still don't want to 724490075Sobrien call expand_expr to calculate the size of a variable-sized array. 724590075Sobrien We haven't necessarily assigned RTL to all variables yet, so it's 724690075Sobrien not safe to try to expand expressions involving them. */ 724790075Sobrien cfun->x_dont_save_pending_sizes_p = 1; 724818334Speter} 724918334Speter 7250169699Skan/* Emit diagnostics that require gimple input for detection. Operate on 7251169699Skan FNDECL and all its nested functions. */ 7252169699Skan 7253169699Skanstatic void 7254169699Skanc_gimple_diagnostics_recursively (tree fndecl) 7255169699Skan{ 7256169699Skan struct cgraph_node *cgn; 7257169699Skan 7258169699Skan /* Handle attribute((warn_unused_result)). Relies on gimple input. */ 7259169699Skan c_warn_unused_result (&DECL_SAVED_TREE (fndecl)); 7260169699Skan 7261169699Skan /* Notice when OpenMP structured block constraints are violated. */ 7262169699Skan if (flag_openmp) 7263169699Skan diagnose_omp_structured_block_errors (fndecl); 7264169699Skan 7265169699Skan /* Finalize all nested functions now. */ 7266169699Skan cgn = cgraph_node (fndecl); 7267169699Skan for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested) 7268169699Skan c_gimple_diagnostics_recursively (cgn->decl); 7269169699Skan} 7270169699Skan 727118334Speter/* Finish up a function declaration and compile that function 727218334Speter all the way to assembler language output. The free the storage 727318334Speter for the function definition. 727418334Speter 7275132730Skan This is called after parsing the body of the function definition. */ 727618334Speter 727718334Spetervoid 7278132730Skanfinish_function (void) 727918334Speter{ 728090075Sobrien tree fndecl = current_function_decl; 728118334Speter 7282169699Skan label_context_stack_se = label_context_stack_se->next; 7283169699Skan label_context_stack_vm = label_context_stack_vm->next; 728418334Speter 7285132730Skan if (TREE_CODE (fndecl) == FUNCTION_DECL 7286132730Skan && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))) 7287132730Skan { 7288132730Skan tree args = DECL_ARGUMENTS (fndecl); 7289132730Skan for (; args; args = TREE_CHAIN (args)) 7290169699Skan { 7291169699Skan tree type = TREE_TYPE (args); 7292169699Skan if (INTEGRAL_TYPE_P (type) 7293169699Skan && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 7294169699Skan DECL_ARG_TYPE (args) = integer_type_node; 7295169699Skan } 7296132730Skan } 7297132730Skan 7298132730Skan if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) 7299132730Skan BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; 7300132730Skan 730118334Speter /* Must mark the RESULT_DECL as being in this function. */ 730218334Speter 7303132730Skan if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node) 7304132730Skan DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; 730518334Speter 730690075Sobrien if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted) 730718334Speter { 730818334Speter if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))) 730918334Speter != integer_type_node) 731050397Sobrien { 731152284Sobrien /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned. 731290075Sobrien If warn_main is -1 (-Wno-main) we don't want to be warned. */ 7313132730Skan if (!warn_main) 7314169699Skan pedwarn ("return type of %q+D is not %<int%>", fndecl); 731550397Sobrien } 731618334Speter else 731718334Speter { 7318169699Skan if (flag_isoc99) 7319169699Skan { 7320169699Skan tree stmt = c_finish_return (integer_zero_node); 7321169699Skan#ifdef USE_MAPPED_LOCATION 7322169699Skan /* Hack. We don't want the middle-end to warn that this return 7323169699Skan is unreachable, so we mark its location as special. Using 7324169699Skan UNKNOWN_LOCATION has the problem that it gets clobbered in 7325169699Skan annotate_one_with_locus. A cleaner solution might be to 7326169699Skan ensure ! should_carry_locus_p (stmt), but that needs a flag. 7327169699Skan */ 7328169699Skan SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION); 732990075Sobrien#else 7330169699Skan /* Hack. We don't want the middle-end to warn that this 7331169699Skan return is unreachable, so put the statement on the 7332169699Skan special line 0. */ 7333169699Skan annotate_with_file_line (stmt, input_filename, 0); 733450397Sobrien#endif 7335169699Skan } 733618334Speter } 733718334Speter } 7338132730Skan 7339169699Skan /* Tie off the statement tree for this function. */ 7340169699Skan DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl)); 7341169699Skan 734290075Sobrien finish_fname_decls (); 734318334Speter 734496263Sobrien /* Complain if there's just no return statement. */ 734596263Sobrien if (warn_return_type 734696263Sobrien && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE 734796263Sobrien && !current_function_returns_value && !current_function_returns_null 7348169699Skan /* Don't complain if we are no-return. */ 734996263Sobrien && !current_function_returns_abnormally 735096263Sobrien /* Don't warn for main(). */ 735196263Sobrien && !MAIN_NAME_P (DECL_NAME (fndecl)) 735296263Sobrien /* Or if they didn't actually specify a return type. */ 735396263Sobrien && !C_FUNCTION_IMPLICIT_INT (fndecl) 735496263Sobrien /* Normally, with -Wreturn-type, flow will complain. Unless we're an 735596263Sobrien inline function, as we might never be compiled separately. */ 735696263Sobrien && DECL_INLINE (fndecl)) 7357169699Skan { 7358169699Skan warning (OPT_Wreturn_type, 7359169699Skan "no return statement in function returning non-void"); 7360169699Skan TREE_NO_WARNING (fndecl) = 1; 7361169699Skan } 736296263Sobrien 7363132730Skan /* With just -Wextra, complain only if function returns both with 7364122196Skan and without a value. */ 7365122196Skan if (extra_warnings 7366122196Skan && current_function_returns_value 7367122196Skan && current_function_returns_null) 7368169699Skan warning (OPT_Wextra, "this function may return with or without a value"); 7369122196Skan 7370169699Skan /* Store the end of the function, so that we get good line number 7371169699Skan info for the epilogue. */ 7372169699Skan cfun->function_end_locus = input_location; 737390075Sobrien 7374169699Skan /* If we don't have ctors/dtors sections, and this is a static 7375169699Skan constructor or destructor, it must be recorded now. */ 7376169699Skan if (DECL_STATIC_CONSTRUCTOR (fndecl) 7377169699Skan && !targetm.have_ctors_dtors) 7378169699Skan static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors); 7379169699Skan if (DECL_STATIC_DESTRUCTOR (fndecl) 7380169699Skan && !targetm.have_ctors_dtors) 7381169699Skan static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors); 738290075Sobrien 7383169699Skan /* Finalize the ELF visibility for the function. */ 7384169699Skan c_determine_visibility (fndecl); 738590075Sobrien 7386169699Skan /* Genericize before inlining. Delay genericizing nested functions 7387169699Skan until their parent function is genericized. Since finalizing 7388169699Skan requires GENERIC, delay that as well. */ 7389169699Skan 7390169699Skan if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node 7391169699Skan && !undef_nested_function) 739290075Sobrien { 7393169699Skan if (!decl_function_context (fndecl)) 7394132730Skan { 7395169699Skan c_genericize (fndecl); 7396169699Skan c_gimple_diagnostics_recursively (fndecl); 739790075Sobrien 7398169699Skan /* ??? Objc emits functions after finalizing the compilation unit. 7399169699Skan This should be cleaned up later and this conditional removed. */ 7400169699Skan if (cgraph_global_info_ready) 7401169699Skan { 7402169699Skan c_expand_body (fndecl); 7403169699Skan return; 7404169699Skan } 740590075Sobrien 7406169699Skan cgraph_finalize_function (fndecl, false); 7407169699Skan } 740818334Speter else 7409169699Skan { 7410169699Skan /* Register this function with cgraph just far enough to get it 7411169699Skan added to our parent's nested function list. Handy, since the 7412169699Skan C front end doesn't have such a list. */ 7413169699Skan (void) cgraph_node (fndecl); 7414169699Skan } 741518334Speter } 741690075Sobrien 7417169699Skan if (!decl_function_context (fndecl)) 7418169699Skan undef_nested_function = false; 7419169699Skan 7420169699Skan /* We're leaving the context of this function, so zap cfun. 7421169699Skan It's still in DECL_STRUCT_FUNCTION, and we'll restore it in 7422169699Skan tree_rest_of_compilation. */ 7423169699Skan cfun = NULL; 7424169699Skan current_function_decl = NULL; 7425132730Skan} 742618334Speter 7427169699Skan/* Generate the RTL for the body of FNDECL. */ 7428117421Skan 7429132730Skanvoid 7430132730Skanc_expand_body (tree fndecl) 7431132730Skan{ 7432132730Skan 7433169699Skan if (!DECL_INITIAL (fndecl) 7434169699Skan || DECL_INITIAL (fndecl) == error_mark_node) 7435169699Skan return; 7436169699Skan 7437169699Skan tree_rest_of_compilation (fndecl); 7438169699Skan 7439169699Skan if (DECL_STATIC_CONSTRUCTOR (fndecl) 7440169699Skan && targetm.have_ctors_dtors) 7441169699Skan targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0), 7442169699Skan DEFAULT_INIT_PRIORITY); 7443169699Skan if (DECL_STATIC_DESTRUCTOR (fndecl) 7444169699Skan && targetm.have_ctors_dtors) 7445169699Skan targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0), 7446169699Skan DEFAULT_INIT_PRIORITY); 744790075Sobrien} 744890075Sobrien 744990075Sobrien/* Check the declarations given in a for-loop for satisfying the C99 7450169699Skan constraints. If exactly one such decl is found, return it. */ 7451169699Skan 7452169699Skantree 7453132730Skancheck_for_loop_decls (void) 745490075Sobrien{ 7455169699Skan struct c_binding *b; 7456169699Skan tree one_decl = NULL_TREE; 7457169699Skan int n_decls = 0; 745890075Sobrien 7459169699Skan 746090075Sobrien if (!flag_isoc99) 746118334Speter { 746290075Sobrien /* If we get here, declarations have been used in a for loop without 746390075Sobrien the C99 for loop scope. This doesn't make much sense, so don't 746490075Sobrien allow it. */ 7465169699Skan error ("%<for%> loop initial declaration used outside C99 mode"); 7466169699Skan return NULL_TREE; 746718334Speter } 746890075Sobrien /* C99 subclause 6.8.5 paragraph 3: 746990075Sobrien 747090075Sobrien [#3] The declaration part of a for statement shall only 747190075Sobrien declare identifiers for objects having storage class auto or 747290075Sobrien register. 747390075Sobrien 747490075Sobrien It isn't clear whether, in this sentence, "identifiers" binds to 747590075Sobrien "shall only declare" or to "objects" - that is, whether all identifiers 747690075Sobrien declared must be identifiers for objects, or whether the restriction 747790075Sobrien only applies to those that are. (A question on this in comp.std.c 747890075Sobrien in November 2000 received no answer.) We implement the strictest 747990075Sobrien interpretation, to avoid creating an extension which later causes 748090075Sobrien problems. */ 748190075Sobrien 7482169699Skan for (b = current_scope->bindings; b; b = b->prev) 748390075Sobrien { 7484169699Skan tree id = b->id; 7485169699Skan tree decl = b->decl; 7486132730Skan 7487169699Skan if (!id) 7488169699Skan continue; 7489169699Skan 7490169699Skan switch (TREE_CODE (decl)) 7491169699Skan { 7492169699Skan case VAR_DECL: 7493169699Skan if (TREE_STATIC (decl)) 7494169699Skan error ("declaration of static variable %q+D in %<for%> loop " 7495169699Skan "initial declaration", decl); 7496169699Skan else if (DECL_EXTERNAL (decl)) 7497169699Skan error ("declaration of %<extern%> variable %q+D in %<for%> loop " 7498169699Skan "initial declaration", decl); 7499169699Skan break; 7500169699Skan 7501169699Skan case RECORD_TYPE: 7502169699Skan error ("%<struct %E%> declared in %<for%> loop initial declaration", 7503169699Skan id); 7504169699Skan break; 7505169699Skan case UNION_TYPE: 7506169699Skan error ("%<union %E%> declared in %<for%> loop initial declaration", 7507169699Skan id); 7508169699Skan break; 7509169699Skan case ENUMERAL_TYPE: 7510169699Skan error ("%<enum %E%> declared in %<for%> loop initial declaration", 7511169699Skan id); 7512169699Skan break; 7513261188Spfg /* APPLE LOCAL begin radar 6268817 */ 7514261188Spfg case FUNCTION_DECL: 7515261188Spfg /* Block helper function can be declared in the statement block 7516261188Spfg for the for-loop declarations. */ 7517261188Spfg if (BLOCK_SYNTHESIZED_FUNC (decl)) 7518261188Spfg break; 7519261188Spfg /* APPLE LOCAL end radar 6268817 */ 7520169699Skan default: 7521169699Skan error ("declaration of non-variable %q+D in %<for%> loop " 7522169699Skan "initial declaration", decl); 7523169699Skan } 7524169699Skan 7525169699Skan n_decls++; 7526169699Skan one_decl = decl; 752790075Sobrien } 752890075Sobrien 7529169699Skan return n_decls == 1 ? one_decl : NULL_TREE; 753018334Speter} 753118334Speter 753218334Speter/* Save and reinitialize the variables 753318334Speter used during compilation of a C function. */ 753418334Speter 753518334Spetervoid 7536132730Skanc_push_function_context (struct function *f) 753718334Speter{ 7538117421Skan struct language_function *p; 7539169699Skan p = GGC_NEW (struct language_function); 7540117421Skan f->language = p; 754118334Speter 754290075Sobrien p->base.x_stmt_tree = c_stmt_tree; 7543169699Skan p->x_break_label = c_break_label; 7544169699Skan p->x_cont_label = c_cont_label; 7545169699Skan p->x_switch_stack = c_switch_stack; 7546169699Skan p->arg_info = current_function_arg_info; 754718334Speter p->returns_value = current_function_returns_value; 754818334Speter p->returns_null = current_function_returns_null; 754996263Sobrien p->returns_abnormally = current_function_returns_abnormally; 755018334Speter p->warn_about_return_type = warn_about_return_type; 755118334Speter} 755218334Speter 755318334Speter/* Restore the variables used during compilation of a C function. */ 755418334Speter 755518334Spetervoid 7556132730Skanc_pop_function_context (struct function *f) 755718334Speter{ 7558117421Skan struct language_function *p = f->language; 755918334Speter 7560261188Spfg /* APPLE LOCAL begin blocks 6040305 */ 7561261188Spfg if (current_function_decl 7562261188Spfg && DECL_STRUCT_FUNCTION (current_function_decl) == 0 7563261188Spfg /* APPLE LOCAL end blocks 6040305 */ 756490075Sobrien && DECL_SAVED_TREE (current_function_decl) == NULL_TREE) 756518334Speter { 756618334Speter /* Stop pointing to the local nodes about to be freed. */ 756718334Speter /* But DECL_INITIAL must remain nonzero so we know this 756818334Speter was an actual function definition. */ 756918334Speter DECL_INITIAL (current_function_decl) = error_mark_node; 757018334Speter DECL_ARGUMENTS (current_function_decl) = 0; 757118334Speter } 757218334Speter 757390075Sobrien c_stmt_tree = p->base.x_stmt_tree; 7574169699Skan c_break_label = p->x_break_label; 7575169699Skan c_cont_label = p->x_cont_label; 7576169699Skan c_switch_stack = p->x_switch_stack; 7577169699Skan current_function_arg_info = p->arg_info; 757818334Speter current_function_returns_value = p->returns_value; 757918334Speter current_function_returns_null = p->returns_null; 758096263Sobrien current_function_returns_abnormally = p->returns_abnormally; 758118334Speter warn_about_return_type = p->warn_about_return_type; 758218334Speter 7583117421Skan f->language = NULL; 758418334Speter} 758518334Speter 7586117421Skan/* Copy the DECL_LANG_SPECIFIC data associated with DECL. */ 758718334Speter 758818334Spetervoid 7589132730Skanc_dup_lang_specific_decl (tree decl) 759090075Sobrien{ 759190075Sobrien struct lang_decl *ld; 759290075Sobrien 759390075Sobrien if (!DECL_LANG_SPECIFIC (decl)) 759490075Sobrien return; 759590075Sobrien 7596169699Skan ld = GGC_NEW (struct lang_decl); 7597132730Skan memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl)); 759890075Sobrien DECL_LANG_SPECIFIC (decl) = ld; 759990075Sobrien} 760090075Sobrien 760190075Sobrien/* The functions below are required for functionality of doing 760290075Sobrien function at once processing in the C front end. Currently these 760390075Sobrien functions are not called from anywhere in the C front end, but as 760490075Sobrien these changes continue, that will change. */ 760590075Sobrien 760690075Sobrien/* Returns the stmt_tree (if any) to which statements are currently 760790075Sobrien being added. If there is no active statement-tree, NULL is 760890075Sobrien returned. */ 760990075Sobrien 761090075Sobrienstmt_tree 7611132730Skancurrent_stmt_tree (void) 761290075Sobrien{ 761390075Sobrien return &c_stmt_tree; 761490075Sobrien} 761590075Sobrien 761690075Sobrien/* Nonzero if TYPE is an anonymous union or struct type. Always 0 in 761790075Sobrien C. */ 761890075Sobrien 761990075Sobrienint 7620169699Skananon_aggr_type_p (tree ARG_UNUSED (node)) 762118334Speter{ 762290075Sobrien return 0; 762318334Speter} 762490075Sobrien 7625132730Skan/* Return the global value of T as a symbol. */ 762690075Sobrien 762790075Sobrientree 7628132730Skanidentifier_global_value (tree t) 762990075Sobrien{ 7630169699Skan struct c_binding *b; 7631132730Skan 7632169699Skan for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed) 7633169699Skan if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b)) 7634169699Skan return b->decl; 7635132730Skan 7636132730Skan return 0; 763790075Sobrien} 763890075Sobrien 763990075Sobrien/* Record a builtin type for C. If NAME is non-NULL, it is the name used; 764090075Sobrien otherwise the name is found in ridpointers from RID_INDEX. */ 764190075Sobrien 764290075Sobrienvoid 7643132730Skanrecord_builtin_type (enum rid rid_index, const char *name, tree type) 764490075Sobrien{ 7645169699Skan tree id, decl; 764690075Sobrien if (name == 0) 764790075Sobrien id = ridpointers[(int) rid_index]; 764890075Sobrien else 764990075Sobrien id = get_identifier (name); 7650169699Skan decl = build_decl (TYPE_DECL, id, type); 7651169699Skan pushdecl (decl); 7652169699Skan if (debug_hooks->type_decl) 7653169699Skan debug_hooks->type_decl (decl, false); 765490075Sobrien} 765590075Sobrien 765690075Sobrien/* Build the void_list_node (void_type_node having been created). */ 765790075Sobrientree 7658132730Skanbuild_void_list_node (void) 765990075Sobrien{ 766090075Sobrien tree t = build_tree_list (NULL_TREE, void_type_node); 766190075Sobrien return t; 766290075Sobrien} 7663117421Skan 7664169699Skan/* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */ 7665117421Skan 7666169699Skanstruct c_parm * 7667169699Skanbuild_c_parm (struct c_declspecs *specs, tree attrs, 7668169699Skan struct c_declarator *declarator) 7669169699Skan{ 7670169699Skan struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm); 7671169699Skan ret->specs = specs; 7672169699Skan ret->attrs = attrs; 7673169699Skan ret->declarator = declarator; 7674169699Skan return ret; 7675169699Skan} 7676117421Skan 7677169699Skan/* Return a declarator with nested attributes. TARGET is the inner 7678169699Skan declarator to which these attributes apply. ATTRS are the 7679169699Skan attributes. */ 7680169699Skan 7681169699Skanstruct c_declarator * 7682169699Skanbuild_attrs_declarator (tree attrs, struct c_declarator *target) 7683117421Skan{ 7684169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7685169699Skan ret->kind = cdk_attrs; 7686169699Skan ret->declarator = target; 7687169699Skan ret->u.attrs = attrs; 7688169699Skan return ret; 7689117421Skan} 7690117421Skan 7691169699Skan/* Return a declarator for a function with arguments specified by ARGS 7692169699Skan and return type specified by TARGET. */ 7693132730Skan 7694169699Skanstruct c_declarator * 7695169699Skanbuild_function_declarator (struct c_arg_info *args, 7696169699Skan struct c_declarator *target) 7697132730Skan{ 7698169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7699169699Skan ret->kind = cdk_function; 7700169699Skan ret->declarator = target; 7701169699Skan ret->u.arg_info = args; 7702169699Skan return ret; 7703132730Skan} 7704132730Skan 7705169699Skan/* Return a declarator for the identifier IDENT (which may be 7706169699Skan NULL_TREE for an abstract declarator). */ 7707132730Skan 7708169699Skanstruct c_declarator * 7709169699Skanbuild_id_declarator (tree ident) 7710132730Skan{ 7711169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7712169699Skan ret->kind = cdk_id; 7713169699Skan ret->declarator = 0; 7714169699Skan ret->u.id = ident; 7715169699Skan /* Default value - may get reset to a more precise location. */ 7716169699Skan ret->id_loc = input_location; 7717169699Skan return ret; 7718132730Skan} 7719132730Skan 7720169699Skan/* Return something to represent absolute declarators containing a *. 7721169699Skan TARGET is the absolute declarator that the * contains. 7722169699Skan TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes 7723169699Skan to apply to the pointer type. */ 7724169699Skan 7725169699Skanstruct c_declarator * 7726169699Skanmake_pointer_declarator (struct c_declspecs *type_quals_attrs, 7727169699Skan struct c_declarator *target) 7728132730Skan{ 7729169699Skan tree attrs; 7730169699Skan int quals = 0; 7731169699Skan struct c_declarator *itarget = target; 7732169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7733169699Skan if (type_quals_attrs) 7734169699Skan { 7735169699Skan attrs = type_quals_attrs->attrs; 7736169699Skan quals = quals_from_declspecs (type_quals_attrs); 7737169699Skan if (attrs != NULL_TREE) 7738169699Skan itarget = build_attrs_declarator (attrs, target); 7739169699Skan } 7740169699Skan ret->kind = cdk_pointer; 7741169699Skan ret->declarator = itarget; 7742169699Skan ret->u.pointer_quals = quals; 7743169699Skan return ret; 7744132730Skan} 7745132730Skan 7746261188Spfg/* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ ch) */ 7747261188Spfg/* build_byref_local_var_access - converts EXPR to: 7748261188Spfg EXPR.__forwarding-><decl-name>. 7749261188Spfg*/ 7750261188Spfgtree 7751261188Spfgbuild_byref_local_var_access (tree expr, tree decl_name) 7752261188Spfg{ 7753261188Spfg tree exp = build_component_ref (expr, get_identifier ("__forwarding")); 7754261188Spfg exp = build_indirect_ref (exp, "unary *"); 7755261188Spfg exp = build_component_ref (exp, decl_name); 7756261188Spfg return exp; 7757261188Spfg} 7758261188Spfg/* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ ch) */ 7759261188Spfg/* APPLE LOCAL begin radar 5732232 - blocks (C++ ch) */ 7760261188Spfg/** 7761261188Spfg build_block_byref_decl - This routine inserts a variable declared as a 7762261188Spfg 'byref' variable using the |...| syntax in helper function's outer-most scope. 7763261188Spfg*/ 7764261188Spfgtree 7765261188Spfgbuild_block_byref_decl (tree name, tree decl, tree exp) 7766261188Spfg{ 7767261188Spfg struct c_scope *scope = current_scope; 7768261188Spfg tree ptr_type, byref_decl; 7769261188Spfg /* APPLE LOCAL begin radar 6225809 */ 7770261188Spfg if (cur_block->prev_block_info) { 7771261188Spfg /* Traverse enclosing blocks. Insert a __block variable in 7772261188Spfg each enclosing block which has no declaration of this 7773261188Spfg variable. This is to ensure that the current (inner) block 7774261188Spfg gets the __block version of the variable; */ 7775261188Spfg struct block_sema_info *cb = cur_block->prev_block_info; 7776261188Spfg while (cb) { 7777261188Spfg struct c_binding *b = I_SYMBOL_BINDING (name); 7778261188Spfg /* Find the first declaration not in current block. */ 7779261188Spfg while (b && b->decl 7780261188Spfg && (TREE_CODE (b->decl) == VAR_DECL 7781261188Spfg || TREE_CODE (b->decl) == PARM_DECL) 7782261188Spfg && b->depth >= cur_block->the_scope->depth) 7783261188Spfg b = b->shadowed; 7784261188Spfg 7785261188Spfg /* Is the next declaration not in enclosing block? */ 7786261188Spfg if (b && b->decl 7787261188Spfg && (TREE_CODE (b->decl) == VAR_DECL 7788261188Spfg || TREE_CODE (b->decl) == PARM_DECL) 7789261188Spfg && b->depth < cb->the_scope->depth) { 7790261188Spfg /* No declaration of variable seen in the block. Must insert one. */ 7791261188Spfg struct c_scope *save_scope = current_scope; 7792261188Spfg struct block_sema_info *save_current_block = cur_block; 7793261188Spfg current_scope = cb->the_scope; 7794261188Spfg cur_block = cb; 7795261188Spfg decl = build_block_byref_decl (name, decl, exp); 7796261188Spfg cur_block = save_current_block; 7797261188Spfg current_scope = save_scope; 7798261188Spfg } 7799261188Spfg cb = cb->prev_block_info; 7800261188Spfg } 7801261188Spfg } 7802261188Spfg /* APPLE LOCAL end radar 6225809 */ 7803261188Spfg 7804261188Spfg /* If it is already a byref declaration, do not add the pointer type 7805261188Spfg because such declarations already have the pointer type 7806261188Spfg added. This happens when we have two nested byref declarations in 7807261188Spfg nested blocks. */ 7808261188Spfg ptr_type = (TREE_CODE (decl) == VAR_DECL && BLOCK_DECL_BYREF (decl)) 7809261188Spfg ? TREE_TYPE (decl) : build_pointer_type (TREE_TYPE (decl)); 7810261188Spfg byref_decl = build_decl (VAR_DECL, name, ptr_type); 7811261188Spfg /* APPLE LOCAL begin radars 6144664 & 6145471 */ 7812261188Spfg DECL_SOURCE_LOCATION (byref_decl) = DECL_SOURCE_LOCATION 7813261188Spfg (cur_block->helper_func_decl); 7814261188Spfg /* APPLE LOCAL end radars 6144664 & 6145471 */ 7815261188Spfg BLOCK_DECL_BYREF (byref_decl) = 1; 7816261188Spfg 7817261188Spfg /* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ ch) */ 7818261188Spfg if (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl)) 7819261188Spfg { 7820261188Spfg COPYABLE_BYREF_LOCAL_VAR (byref_decl) = 1; 7821261188Spfg COPYABLE_BYREF_LOCAL_NONPOD (byref_decl) = COPYABLE_BYREF_LOCAL_NONPOD (decl); 7822261188Spfg /* APPLE LOCAL radar 5847976 */ 7823261188Spfg COPYABLE_WEAK_BLOCK (byref_decl) = COPYABLE_WEAK_BLOCK (decl); 7824261188Spfg } 7825261188Spfg /* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ ch) */ 7826261188Spfg 7827261188Spfg /* In the presence of nested "{" 7828261188Spfg move up the scope chain until reaching the main function body's scope. 7829261188Spfg */ 7830261188Spfg while (scope && !scope->function_body) 7831261188Spfg scope = scope->outer; 7832261188Spfg /* Current scope must be that of the main function body. */ 7833261188Spfg gcc_assert (scope && scope->function_body); 7834261188Spfg bind (name, byref_decl, 7835261188Spfg scope, /*invisible=*/false, /*nested=*/false); 7836261188Spfg cur_block->block_byref_decl_list = 7837261188Spfg tree_cons (NULL_TREE, byref_decl, cur_block->block_byref_decl_list); 7838261188Spfg /* APPLE LOCAL radar 5847213 - building block_original_byref_decl_list list removed. */ 7839261188Spfg /* APPLE LOCAL begin radar 6289031 */ 7840261188Spfg#if 0 7841261188Spfg if (! flag_objc_gc_only) 7842261188Spfg#endif 7843261188Spfg push_cleanup (byref_decl, build_block_byref_release_exp (byref_decl), false); 7844261188Spfg /* APPLE LOCAL end radar 6289031 */ 7845261188Spfg 7846261188Spfg return byref_decl; 7847261188Spfg} 7848261188Spfg 7849261188Spfg/** 7850261188Spfg build_block_ref_decl - This routine inserts a copied-in variable (a variable 7851261188Spfg referenced in the block but whose scope is outside the block) in helper 7852261188Spfg function's outer-most scope. It also sets its type to 'const' as such 7853261188Spfg variables are read-only. 7854261188Spfg*/ 7855261188Spfgtree 7856261188Spfgbuild_block_ref_decl (tree name, tree decl) 7857261188Spfg{ 7858261188Spfg struct c_scope *scope = current_scope; 7859261188Spfg tree ref_decl; 7860261188Spfg /* APPLE LOCAL radar 6212722 */ 7861261188Spfg tree type, exp; 7862261188Spfg /* APPLE LOCAL begin radar 5932809 - copyable byref blocks (C++ ch) */ 7863261188Spfg /* 'decl' was previously declared as __block. Simply, copy the value 7864261188Spfg embedded in the above variable. */ 7865261188Spfg if (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl)) 7866261188Spfg decl = build_byref_local_var_access (decl, DECL_NAME (decl)); 7867261188Spfg else { 7868261188Spfg /* APPLE LOCAL begin radar 5988451 (C++ ch) */ 7869261188Spfg if (cur_block->prev_block_info) { 7870261188Spfg /* Traverse enclosing blocks. Insert a copied-in variable in 7871261188Spfg each enclosing block which has no declaration of this 7872261188Spfg variable. This is to ensure that the current (inner) block 7873261188Spfg has the 'frozen' value of the copied-in variable; which means 7874261188Spfg the value of the copied in variable is at the point of the 7875261188Spfg block declaration and *not* when the inner block is 7876261188Spfg invoked. */ 7877261188Spfg struct block_sema_info *cb = cur_block->prev_block_info; 7878261188Spfg while (cb) { 7879261188Spfg struct c_binding *b = I_SYMBOL_BINDING (name); 7880261188Spfg /* Find the first declaration not in current block. */ 7881261188Spfg while (b && b->decl 7882261188Spfg && (TREE_CODE (b->decl) == VAR_DECL 7883261188Spfg || TREE_CODE (b->decl) == PARM_DECL) 7884261188Spfg && b->depth >= cur_block->the_scope->depth) 7885261188Spfg b = b->shadowed; 7886261188Spfg 7887261188Spfg /* Is the next declaration not in enclosing block? */ 7888261188Spfg if (b && b->decl 7889261188Spfg && (TREE_CODE (b->decl) == VAR_DECL 7890261188Spfg || TREE_CODE (b->decl) == PARM_DECL) 7891261188Spfg && b->depth < cb->the_scope->depth) { 7892261188Spfg /* No declaration of variable seen in the block. Must insert one, 7893261188Spfg so it 'freezes' the variable in this block. */ 7894261188Spfg struct c_scope *save_scope = current_scope; 7895261188Spfg struct block_sema_info *save_current_block = cur_block; 7896261188Spfg current_scope = cb->the_scope; 7897261188Spfg cur_block = cb; 7898261188Spfg decl = build_block_ref_decl (name, decl); 7899261188Spfg cur_block = save_current_block; 7900261188Spfg current_scope = save_scope; 7901261188Spfg } 7902261188Spfg cb = cb->prev_block_info; 7903261188Spfg } 7904261188Spfg } 7905261188Spfg /* APPLE LOCAL end radar 5988451 (C++ ch) */ 7906261188Spfg } 7907261188Spfg /* APPLE LOCAL end radar 5932809 - copyable byref blocks (C++ ch) */ 7908261188Spfg /* APPLE LOCAL begin radar 6212722 */ 7909261188Spfg exp = decl; 7910261188Spfg type = TREE_TYPE (exp); 7911261188Spfg if (TREE_CODE (type) == ARRAY_TYPE) { 7912261188Spfg exp = array_to_pointer_conversion (decl); 7913261188Spfg type = TREE_TYPE (exp); 7914261188Spfg } 7915261188Spfg else if (TREE_CODE (type) == FUNCTION_TYPE) { 7916261188Spfg exp = function_to_pointer_conversion (exp); 7917261188Spfg type = TREE_TYPE (exp); 7918261188Spfg } 7919261188Spfg ref_decl = build_decl (VAR_DECL, name, 7920261188Spfg build_qualified_type (type, TYPE_QUAL_CONST)); 7921261188Spfg /* APPLE LOCAL end radar 6212722 */ 7922261188Spfg /* APPLE LOCAL begin radars 6144664 & 6145471 */ 7923261188Spfg DECL_SOURCE_LOCATION (ref_decl) = DECL_SOURCE_LOCATION 7924261188Spfg (cur_block->helper_func_decl); 7925261188Spfg /* APPLE LOCAL end radars 6144664 & 6145471 */ 7926261188Spfg DECL_INITIAL (ref_decl) = error_mark_node; 7927261188Spfg /* APPLE LOCAL radar 5805175 - blocks (C++ ch) */ 7928261188Spfg c_apply_type_quals_to_decl (TYPE_QUAL_CONST, ref_decl); 7929261188Spfg BLOCK_DECL_COPIED (ref_decl) = 1; 7930261188Spfg 7931261188Spfg /* Find the scope for function body (outer-most scope) and insert 7932261188Spfg this variable in that scope. This is to avoid duplicate 7933261188Spfg declaration of the save variable. */ 7934261188Spfg while (scope && !scope->function_body) 7935261188Spfg scope = scope->outer; 7936261188Spfg /* We are enterring the copied-in variable in helper function's 7937261188Spfg outer scope; that of its main body. */ 7938261188Spfg gcc_assert (scope); 7939261188Spfg bind (name, ref_decl, 7940261188Spfg scope, /*invisible=*/false, /*nested=*/false); 7941261188Spfg cur_block->block_ref_decl_list = 7942261188Spfg tree_cons (NULL_TREE, ref_decl, cur_block->block_ref_decl_list); 7943261188Spfg cur_block->block_original_ref_decl_list = 7944261188Spfg /* APPLE LOCAL radar 6212722 */ 7945261188Spfg tree_cons (NULL_TREE, exp, cur_block->block_original_ref_decl_list); 7946261188Spfg return ref_decl; 7947261188Spfg} 7948261188Spfg 7949261188Spfg/* APPLE LOCAL begin radar 5847213 - radar 6329245 */ 7950261188Spfgstatic GTY (()) tree descriptor_ptr_type; 7951261188Spfgstatic GTY (()) tree descriptor_ptr_type_with_copydispose; 7952261188Spfg/** build_block_descriptor_type - This routine builds following internal type: 7953261188Spfg struct __block_descriptor { 7954261188Spfg unsigned long int reserved; // NULL 7955261188Spfg unsigned long int Size; // sizeof(struct Block_literal_1) 7956261188Spfg 7957261188Spfg // optional helper functions 7958261188Spfg void *CopyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE is set (withCopyDispose true) 7959261188Spfg void *DestroyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE is set (withCopyDispose true) 7960261188Spfg} *descriptor_ptr_type; 7961261188Spfg 7962261188SpfgObjects of this type will always be static. This is one main component of abi change. 7963261188Spfg*/ 7964261188Spfgtree 7965261188Spfgbuild_block_descriptor_type (bool withCopyDispose) 7966261188Spfg{ 7967261188Spfg tree field_decl_chain, field_decl; 7968261188Spfg tree main_type; 7969261188Spfg 7970261188Spfg if (withCopyDispose && descriptor_ptr_type_with_copydispose) 7971261188Spfg return descriptor_ptr_type_with_copydispose; 7972261188Spfg if (!withCopyDispose && descriptor_ptr_type) 7973261188Spfg return descriptor_ptr_type; 7974261188Spfg 7975261188Spfg main_type = 7976261188Spfg withCopyDispose ? 7977261188Spfg start_struct (RECORD_TYPE, get_identifier ("__block_descriptor_withcopydispose")) 7978261188Spfg : start_struct (RECORD_TYPE, get_identifier ("__block_descriptor")); 7979261188Spfg 7980261188Spfg /* unsigned long int reserved; */ 7981261188Spfg field_decl = build_decl (FIELD_DECL, get_identifier ("reserved"), long_unsigned_type_node); 7982261188Spfg field_decl_chain = field_decl; 7983261188Spfg 7984261188Spfg /* unsigned long int Size; */ 7985261188Spfg field_decl = build_decl (FIELD_DECL, get_identifier ("Size"), long_unsigned_type_node); 7986261188Spfg chainon (field_decl_chain, field_decl); 7987261188Spfg 7988261188Spfg if (withCopyDispose) 7989261188Spfg { 7990261188Spfg /* void *CopyFuncPtr; */ 7991261188Spfg field_decl = build_decl (FIELD_DECL, get_identifier ("CopyFuncPtr"), ptr_type_node); 7992261188Spfg chainon (field_decl_chain, field_decl); 7993261188Spfg /* void *DestroyFuncPtr; */ 7994261188Spfg field_decl = build_decl (FIELD_DECL, get_identifier ("DestroyFuncPtr"), ptr_type_node); 7995261188Spfg chainon (field_decl_chain, field_decl); 7996261188Spfg } 7997261188Spfg 7998261188Spfg /* Mark this struct as being a block struct rather than a 'normal' 7999261188Spfg struct. */ 8000261188Spfg TYPE_BLOCK_IMPL_STRUCT (main_type) = 1; 8001261188Spfg finish_struct (main_type, field_decl_chain, NULL_TREE); 8002261188Spfg 8003261188Spfg main_type = build_pointer_type (main_type); 8004261188Spfg if (withCopyDispose) 8005261188Spfg descriptor_ptr_type_with_copydispose = main_type; 8006261188Spfg else 8007261188Spfg descriptor_ptr_type = main_type; 8008261188Spfg return main_type; 8009261188Spfg} 8010261188Spfg/* APPLE LOCAL end radar 5847213 - radar 6329245 */ 8011261188Spfg 8012261188Spfg/* APPLE LOCAL begin radar 5814025 (C++ ch) */ 8013261188Spfgstruct c_declarator * 8014261188Spfgmake_block_pointer_declarator (struct c_declspecs *type_quals_attrs, 8015261188Spfg struct c_declarator *target) 8016261188Spfg{ 8017261188Spfg int quals = 0; 8018261188Spfg struct c_declarator *itarget = target; 8019261188Spfg struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 8020261188Spfg 8021261188Spfg if (type_quals_attrs) 8022261188Spfg { 8023261188Spfg tree attrs = type_quals_attrs->attrs; 8024261188Spfg quals = quals_from_declspecs (type_quals_attrs); 8025261188Spfg if (attrs != NULL_TREE) 8026261188Spfg itarget = build_attrs_declarator (attrs, target); 8027261188Spfg } 8028261188Spfg ret->kind = cdk_block_pointer; 8029261188Spfg /* APPLE LOCAL radar 5882266 (C++ ch) */ 8030261188Spfg ret->declarator = itarget; 8031261188Spfg ret->u.pointer_quals = quals; 8032261188Spfg return ret; 8033261188Spfg} 8034261188Spfg/* APPLE LOCAL end radar 5814025 (C++ ch) */ 8035261188Spfg 8036261188Spfgtree 8037261188Spfgbegin_block (void) 8038261188Spfg{ 8039261188Spfg struct block_sema_info *csi; 8040261188Spfg#if 0 8041261188Spfg push_scope (); 8042261188Spfg#endif 8043261188Spfg csi = (struct block_sema_info*)xcalloc (1, sizeof (struct block_sema_info)); 8044261188Spfg csi->prev_block_info = cur_block; 8045261188Spfg cur_block = csi; 8046261188Spfg return NULL_TREE; 8047261188Spfg} 8048261188Spfg 8049261188Spfgstruct block_sema_info * 8050261188Spfgfinish_block (tree block __attribute__ ((__unused__))) 8051261188Spfg{ 8052261188Spfg struct block_sema_info *csi = cur_block; 8053261188Spfg cur_block = cur_block->prev_block_info; 8054261188Spfg#if 0 8055261188Spfg pop_scope (); 8056261188Spfg#endif 8057261188Spfg return csi; 8058261188Spfg} 8059261188Spfg 8060261188Spfgbool 8061261188Spfgin_imm_block (void) 8062261188Spfg{ 8063261188Spfg /* APPLE LOCAL radar 5988451 (C++ ch) */ 8064261188Spfg return (cur_block && cur_block->the_scope == current_scope); 8065261188Spfg} 8066261188Spfg 8067261188Spfg/* This routine returns 'true' if 'name' has a declaration inside the 8068261188Spfg current block, 'false' otherwise. If 'name' has no declaration in 8069261188Spfg the current block, it returns in DECL the user declaration for 8070261188Spfg 'name' found in the enclosing scope. Note that if it is declared 8071261188Spfg in current declaration, it can be either a user declaration or a 8072261188Spfg byref/copied-in declaration added in current block's scope by the 8073261188Spfg compiler. */ 8074261188Spfgbool 8075261188Spfglookup_name_in_block (tree name, tree *decl) 8076261188Spfg{ 8077261188Spfg if (cur_block) 8078261188Spfg { 8079261188Spfg struct c_binding *b = I_SYMBOL_BINDING (name); 8080261188Spfg if (b->depth >= cur_block->the_scope->depth) 8081261188Spfg return true; 8082261188Spfg 8083261188Spfg /* Check for common case of block nested inside a non-block. */ 8084261188Spfg if (!cur_block->prev_block_info) 8085261188Spfg return false; 8086261188Spfg /* Check for less common case of nested blocks. */ 8087261188Spfg /* Declaration not in current block. Find the first user 8088261188Spfg declaration of 'name' in outer scope. */ 8089261188Spfg /* APPLE LOCAL begin radar 5988451 (C++ ch) */ 8090261188Spfg /* Check for variables only, as we may have parameters, such as 8091261188Spfg 'self' */ 8092261188Spfg /* Note that if a copied-in variable (BLOCK_DECL_COPIED) in the 8093261188Spfg enclosing block is found, it must be returned as this is 8094261188Spfg where the variable in current (nested block) will have to get 8095261188Spfg its value. */ 8096261188Spfg while (b && b->decl 8097261188Spfg && (TREE_CODE (b->decl) == VAR_DECL) 8098261188Spfg && BLOCK_DECL_BYREF (b->decl)) 8099261188Spfg b = b->shadowed; 8100261188Spfg /* APPLE LOCAL end radar 5988451 (C++ ch) */ 8101261188Spfg if (b && b->decl) 8102261188Spfg *decl = b->decl; 8103261188Spfg } 8104261188Spfg return false; 8105261188Spfg} 8106261188Spfg 8107261188Spfgstatic struct c_scope *save_current_scope; 8108261188Spfgstatic tree save_current_function_decl; 8109261188Spfgvoid 8110261188Spfgpush_to_top_level (void) 8111261188Spfg{ 8112261188Spfg save_current_scope = current_scope; 8113261188Spfg save_current_function_decl = current_function_decl; 8114261188Spfg current_scope = file_scope; 8115261188Spfg current_function_decl = NULL_TREE; 8116261188Spfg} 8117261188Spfg 8118261188Spfgvoid 8119261188Spfgpop_from_top_level (void) 8120261188Spfg{ 8121261188Spfg current_scope = save_current_scope; 8122261188Spfg current_function_decl = save_current_function_decl; 8123261188Spfg} 8124261188Spfg 8125261188Spfg/** 8126261188Spfg build_helper_func_decl - This routine builds a FUNCTION_DECL for 8127261188Spfg a block helper function. 8128261188Spfg*/ 8129261188Spfgtree 8130261188Spfgbuild_helper_func_decl (tree ident, tree type) 8131261188Spfg{ 8132261188Spfg tree func_decl = build_decl (FUNCTION_DECL, ident, type); 8133261188Spfg DECL_EXTERNAL (func_decl) = 0; 8134261188Spfg TREE_PUBLIC (func_decl) = 0; 8135261188Spfg TREE_USED (func_decl) = 1; 8136261188Spfg TREE_NOTHROW (func_decl) = 0; 8137261188Spfg /* APPLE LOCAL radar 6172148 */ 8138261188Spfg BLOCK_SYNTHESIZED_FUNC (func_decl) = 1; 8139261188Spfg return func_decl; 8140261188Spfg} 8141261188Spfg 8142261188Spfg/** 8143261188Spfg start_block_helper_function - This is a light-weight version of start_function(). 8144261188Spfg It has removed all the fuss in the start_function(). 8145261188Spfg */ 8146261188Spfgvoid 8147261188Spfgstart_block_helper_function (tree decl1) 8148261188Spfg{ 8149261188Spfg struct c_label_context_se *nstack_se; 8150261188Spfg struct c_label_context_vm *nstack_vm; 8151261188Spfg tree restype, resdecl; 8152261188Spfg 8153261188Spfg current_function_returns_value = 0; /* Assume, until we see it does. */ 8154261188Spfg current_function_returns_null = 0; 8155261188Spfg current_function_returns_abnormally = 0; 8156261188Spfg warn_about_return_type = 0; 8157261188Spfg c_switch_stack = NULL; 8158261188Spfg 8159261188Spfg nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se); 8160261188Spfg nstack_se->labels_def = NULL; 8161261188Spfg nstack_se->labels_used = NULL; 8162261188Spfg nstack_se->next = label_context_stack_se; 8163261188Spfg label_context_stack_se = nstack_se; 8164261188Spfg 8165261188Spfg nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm); 8166261188Spfg nstack_vm->labels_def = NULL; 8167261188Spfg nstack_vm->labels_used = NULL; 8168261188Spfg nstack_vm->scope = 0; 8169261188Spfg nstack_vm->next = label_context_stack_vm; 8170261188Spfg label_context_stack_vm = nstack_vm; 8171261188Spfg 8172261188Spfg /* Indicate no valid break/continue context by setting these variables 8173261188Spfg to some non-null, non-label value. We'll notice and emit the proper 8174261188Spfg error message in c_finish_bc_stmt. */ 8175261188Spfg c_break_label = c_cont_label = size_zero_node; 8176261188Spfg 8177261188Spfg announce_function (decl1); 8178261188Spfg 8179261188Spfg /* Make the init_value nonzero so pushdecl knows this is not tentative. 8180261188Spfg error_mark_node is replaced below (in pop_scope) with the BLOCK. */ 8181261188Spfg DECL_INITIAL (decl1) = error_mark_node; 8182261188Spfg 8183261188Spfg current_function_prototype_locus = UNKNOWN_LOCATION; 8184261188Spfg current_function_prototype_built_in = false; 8185261188Spfg current_function_prototype_arg_types = NULL_TREE; 8186261188Spfg 8187261188Spfg /* This function exists in static storage. 8188261188Spfg (This does not mean `static' in the C sense!) */ 8189261188Spfg TREE_STATIC (decl1) = 1; 8190261188Spfg /* A helper function is not global */ 8191261188Spfg TREE_PUBLIC (decl1) = 0; 8192261188Spfg 8193261188Spfg /* This is the earliest point at which we might know the assembler 8194261188Spfg name of the function. Thus, if it's set before this, die horribly. */ 8195261188Spfg gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1)); 8196261188Spfg current_function_decl = pushdecl (decl1); 8197261188Spfg 8198261188Spfg push_scope (); 8199261188Spfg declare_parm_level (); 8200261188Spfg 8201261188Spfg restype = TREE_TYPE (TREE_TYPE (current_function_decl)); 8202261188Spfg resdecl = build_decl (RESULT_DECL, NULL_TREE, restype); 8203261188Spfg DECL_ARTIFICIAL (resdecl) = 1; 8204261188Spfg DECL_IGNORED_P (resdecl) = 1; 8205261188Spfg DECL_RESULT (current_function_decl) = resdecl; 8206261188Spfg 8207261188Spfg start_fname_decls (); 8208261188Spfg} 8209261188Spfg 8210261188Spfg/** 8211261188Spfg declare_block_prologue_local_vars - utility routine to do the actual 8212261188Spfg declaration and initialization for each referecned block variable. 8213261188Spfg*/ 8214261188Spfgstatic void 8215261188Spfgdeclare_block_prologue_local_vars (tree self_parm, tree component, 8216261188Spfg tree stmt) 8217261188Spfg{ 8218261188Spfg tree decl, block_component; 8219261188Spfg tree_stmt_iterator i; 8220261188Spfg tree decl_stmt; 8221261188Spfg 8222261188Spfg decl = component; 8223261188Spfg block_component = build_component_ref (build_indirect_ref (self_parm, "->"), 8224261188Spfg DECL_NAME (component)); 8225261188Spfg gcc_assert (block_component); 8226261188Spfg DECL_EXTERNAL (decl) = 0; 8227261188Spfg TREE_STATIC (decl) = 0; 8228261188Spfg TREE_USED (decl) = 1; 8229261188Spfg DECL_CONTEXT (decl) = current_function_decl; 8230261188Spfg DECL_ARTIFICIAL (decl) = 1; 8231261188Spfg DECL_INITIAL (decl) = block_component; 8232261188Spfg /* Prepend a DECL_EXPR statement to the statement list. */ 8233261188Spfg i = tsi_start (stmt); 8234261188Spfg decl_stmt = build_stmt (DECL_EXPR, decl); 8235261188Spfg /* APPLE LOCAL Radar 5811961, Fix location of block prologue vars (C++ ch) */ 8236261188Spfg SET_EXPR_LOCATION (decl_stmt, DECL_SOURCE_LOCATION (decl)); 8237261188Spfg /* APPLE LOCAL begin radar 6163705, Blocks prologues */ 8238261188Spfg /* Give the prologue statements a line number of one before the beginning of 8239261188Spfg the function, to make them easily identifiable later. */ 8240261188Spfg EXPR_LINENO (decl_stmt) = DECL_SOURCE_LINE (decl) - 1; 8241261188Spfg /* APPLE LOCAL end radar 6163705, Blocks prologues */ 8242261188Spfg tsi_link_before (&i, decl_stmt, TSI_SAME_STMT); 8243261188Spfg} 8244261188Spfg 8245261188Spfg/** 8246261188Spfg block_build_prologue 8247261188Spfg - This routine builds the declarations for the 8248261188Spfg variables referenced in the block; as in: 8249261188Spfg int *y = .block_descriptor->y; 8250261188Spfg int x = .block_descriptor->x; 8251261188Spfg 8252261188Spfg The decl_expr declaration for each initialization is enterred at the 8253261188Spfg beginning of the helper function's statement-list which is passed 8254261188Spfg in block_impl->block_body. 8255261188Spfg*/ 8256261188Spfgvoid 8257261188Spfgblock_build_prologue (struct block_sema_info *block_impl) 8258261188Spfg{ 8259261188Spfg tree chain; 8260261188Spfg /* APPLE LOCAL radar 6404979 */ 8261261188Spfg tree self_parm = lookup_name (get_identifier (".block_descriptor")); 8262261188Spfg gcc_assert (self_parm); 8263261188Spfg 8264261188Spfg for (chain = block_impl->block_ref_decl_list; chain; 8265261188Spfg chain = TREE_CHAIN (chain)) 8266261188Spfg declare_block_prologue_local_vars (self_parm, TREE_VALUE (chain), 8267261188Spfg block_impl->block_body); 8268261188Spfg 8269261188Spfg for (chain = block_impl->block_byref_decl_list; chain; 8270261188Spfg chain = TREE_CHAIN (chain)) 8271261188Spfg declare_block_prologue_local_vars (self_parm, TREE_VALUE (chain), 8272261188Spfg block_impl->block_body); 8273261188Spfg} 8274261188Spfg/* APPLE LOCAL end radar 5732232 - blocks (C++ ch) */ 8275261188Spfg 8276169699Skan/* Return a pointer to a structure for an empty list of declaration 8277169699Skan specifiers. */ 8278132730Skan 8279169699Skanstruct c_declspecs * 8280169699Skanbuild_null_declspecs (void) 8281132730Skan{ 8282169699Skan struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs); 8283169699Skan ret->type = 0; 8284169699Skan ret->decl_attr = 0; 8285169699Skan ret->attrs = 0; 8286169699Skan ret->typespec_word = cts_none; 8287169699Skan ret->storage_class = csc_none; 8288169699Skan ret->declspecs_seen_p = false; 8289169699Skan ret->type_seen_p = false; 8290169699Skan ret->non_sc_seen_p = false; 8291169699Skan ret->typedef_p = false; 8292169699Skan ret->tag_defined_p = false; 8293169699Skan ret->explicit_signed_p = false; 8294169699Skan ret->deprecated_p = false; 8295260918Spfg /* APPLE LOCAL "unavailable" attribute (radar 2809697) */ 8296260918Spfg ret->unavailable_p = false; 8297169699Skan ret->default_int_p = false; 8298169699Skan ret->long_p = false; 8299169699Skan ret->long_long_p = false; 8300169699Skan ret->short_p = false; 8301169699Skan ret->signed_p = false; 8302169699Skan ret->unsigned_p = false; 8303169699Skan ret->complex_p = false; 8304169699Skan ret->inline_p = false; 8305169699Skan ret->thread_p = false; 8306169699Skan ret->const_p = false; 8307169699Skan ret->volatile_p = false; 8308169699Skan ret->restrict_p = false; 8309169699Skan return ret; 8310169699Skan} 8311132730Skan 8312169699Skan/* Add the type qualifier QUAL to the declaration specifiers SPECS, 8313169699Skan returning SPECS. */ 8314132730Skan 8315169699Skanstruct c_declspecs * 8316169699Skandeclspecs_add_qual (struct c_declspecs *specs, tree qual) 8317169699Skan{ 8318169699Skan enum rid i; 8319169699Skan bool dupe = false; 8320169699Skan specs->non_sc_seen_p = true; 8321169699Skan specs->declspecs_seen_p = true; 8322169699Skan gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE 8323169699Skan && C_IS_RESERVED_WORD (qual)); 8324169699Skan i = C_RID_CODE (qual); 8325169699Skan switch (i) 8326169699Skan { 8327169699Skan case RID_CONST: 8328169699Skan dupe = specs->const_p; 8329169699Skan specs->const_p = true; 8330169699Skan break; 8331169699Skan case RID_VOLATILE: 8332169699Skan dupe = specs->volatile_p; 8333169699Skan specs->volatile_p = true; 8334169699Skan break; 8335169699Skan case RID_RESTRICT: 8336169699Skan dupe = specs->restrict_p; 8337169699Skan specs->restrict_p = true; 8338169699Skan break; 8339169699Skan default: 8340169699Skan gcc_unreachable (); 8341169699Skan } 8342169699Skan if (dupe && pedantic && !flag_isoc99) 8343169699Skan pedwarn ("duplicate %qE", qual); 8344169699Skan return specs; 8345169699Skan} 8346132730Skan 8347169699Skan/* Add the type specifier TYPE to the declaration specifiers SPECS, 8348169699Skan returning SPECS. */ 8349132730Skan 8350169699Skanstruct c_declspecs * 8351169699Skandeclspecs_add_type (struct c_declspecs *specs, struct c_typespec spec) 8352169699Skan{ 8353169699Skan tree type = spec.spec; 8354169699Skan specs->non_sc_seen_p = true; 8355169699Skan specs->declspecs_seen_p = true; 8356169699Skan specs->type_seen_p = true; 8357169699Skan if (TREE_DEPRECATED (type)) 8358169699Skan specs->deprecated_p = true; 8359169699Skan 8360260918Spfg /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */ 8361260918Spfg if (TREE_UNAVAILABLE (type)) 8362260918Spfg specs->unavailable_p = true; 8363260918Spfg /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */ 8364260918Spfg 8365169699Skan /* Handle type specifier keywords. */ 8366169699Skan if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type)) 8367169699Skan { 8368169699Skan enum rid i = C_RID_CODE (type); 8369169699Skan if (specs->type) 8370132730Skan { 8371169699Skan error ("two or more data types in declaration specifiers"); 8372169699Skan return specs; 8373169699Skan } 8374169699Skan if ((int) i <= (int) RID_LAST_MODIFIER) 8375169699Skan { 8376169699Skan /* "long", "short", "signed", "unsigned" or "_Complex". */ 8377169699Skan bool dupe = false; 8378169699Skan switch (i) 8379132730Skan { 8380169699Skan case RID_LONG: 8381169699Skan if (specs->long_long_p) 8382132730Skan { 8383169699Skan error ("%<long long long%> is too long for GCC"); 8384169699Skan break; 8385132730Skan } 8386169699Skan if (specs->long_p) 8387132730Skan { 8388169699Skan if (specs->typespec_word == cts_double) 8389169699Skan { 8390169699Skan error ("both %<long long%> and %<double%> in " 8391169699Skan "declaration specifiers"); 8392169699Skan break; 8393169699Skan } 8394169699Skan if (pedantic && !flag_isoc99 && !in_system_header 8395169699Skan && warn_long_long) 8396169699Skan pedwarn ("ISO C90 does not support %<long long%>"); 8397169699Skan specs->long_long_p = 1; 8398169699Skan break; 8399132730Skan } 8400169699Skan if (specs->short_p) 8401169699Skan error ("both %<long%> and %<short%> in " 8402169699Skan "declaration specifiers"); 8403169699Skan else if (specs->typespec_word == cts_void) 8404169699Skan error ("both %<long%> and %<void%> in " 8405169699Skan "declaration specifiers"); 8406169699Skan else if (specs->typespec_word == cts_bool) 8407169699Skan error ("both %<long%> and %<_Bool%> in " 8408169699Skan "declaration specifiers"); 8409169699Skan else if (specs->typespec_word == cts_char) 8410169699Skan error ("both %<long%> and %<char%> in " 8411169699Skan "declaration specifiers"); 8412169699Skan else if (specs->typespec_word == cts_float) 8413169699Skan error ("both %<long%> and %<float%> in " 8414169699Skan "declaration specifiers"); 8415169699Skan else if (specs->typespec_word == cts_dfloat32) 8416169699Skan error ("both %<long%> and %<_Decimal32%> in " 8417169699Skan "declaration specifiers"); 8418169699Skan else if (specs->typespec_word == cts_dfloat64) 8419169699Skan error ("both %<long%> and %<_Decimal64%> in " 8420169699Skan "declaration specifiers"); 8421169699Skan else if (specs->typespec_word == cts_dfloat128) 8422169699Skan error ("both %<long%> and %<_Decimal128%> in " 8423169699Skan "declaration specifiers"); 8424132730Skan else 8425169699Skan specs->long_p = true; 8426169699Skan break; 8427169699Skan case RID_SHORT: 8428169699Skan dupe = specs->short_p; 8429169699Skan if (specs->long_p) 8430169699Skan error ("both %<long%> and %<short%> in " 8431169699Skan "declaration specifiers"); 8432169699Skan else if (specs->typespec_word == cts_void) 8433169699Skan error ("both %<short%> and %<void%> in " 8434169699Skan "declaration specifiers"); 8435169699Skan else if (specs->typespec_word == cts_bool) 8436169699Skan error ("both %<short%> and %<_Bool%> in " 8437169699Skan "declaration specifiers"); 8438169699Skan else if (specs->typespec_word == cts_char) 8439169699Skan error ("both %<short%> and %<char%> in " 8440169699Skan "declaration specifiers"); 8441169699Skan else if (specs->typespec_word == cts_float) 8442169699Skan error ("both %<short%> and %<float%> in " 8443169699Skan "declaration specifiers"); 8444169699Skan else if (specs->typespec_word == cts_double) 8445169699Skan error ("both %<short%> and %<double%> in " 8446169699Skan "declaration specifiers"); 8447169699Skan else if (specs->typespec_word == cts_dfloat32) 8448169699Skan error ("both %<short%> and %<_Decimal32%> in " 8449169699Skan "declaration specifiers"); 8450169699Skan else if (specs->typespec_word == cts_dfloat64) 8451169699Skan error ("both %<short%> and %<_Decimal64%> in " 8452169699Skan "declaration specifiers"); 8453169699Skan else if (specs->typespec_word == cts_dfloat128) 8454169699Skan error ("both %<short%> and %<_Decimal128%> in " 8455169699Skan "declaration specifiers"); 8456169699Skan else 8457169699Skan specs->short_p = true; 8458169699Skan break; 8459169699Skan case RID_SIGNED: 8460169699Skan dupe = specs->signed_p; 8461169699Skan if (specs->unsigned_p) 8462169699Skan error ("both %<signed%> and %<unsigned%> in " 8463169699Skan "declaration specifiers"); 8464169699Skan else if (specs->typespec_word == cts_void) 8465169699Skan error ("both %<signed%> and %<void%> in " 8466169699Skan "declaration specifiers"); 8467169699Skan else if (specs->typespec_word == cts_bool) 8468169699Skan error ("both %<signed%> and %<_Bool%> in " 8469169699Skan "declaration specifiers"); 8470169699Skan else if (specs->typespec_word == cts_float) 8471169699Skan error ("both %<signed%> and %<float%> in " 8472169699Skan "declaration specifiers"); 8473169699Skan else if (specs->typespec_word == cts_double) 8474169699Skan error ("both %<signed%> and %<double%> in " 8475169699Skan "declaration specifiers"); 8476169699Skan else if (specs->typespec_word == cts_dfloat32) 8477169699Skan error ("both %<signed%> and %<_Decimal32%> in " 8478169699Skan "declaration specifiers"); 8479169699Skan else if (specs->typespec_word == cts_dfloat64) 8480169699Skan error ("both %<signed%> and %<_Decimal64%> in " 8481169699Skan "declaration specifiers"); 8482169699Skan else if (specs->typespec_word == cts_dfloat128) 8483169699Skan error ("both %<signed%> and %<_Decimal128%> in " 8484169699Skan "declaration specifiers"); 8485169699Skan else 8486169699Skan specs->signed_p = true; 8487169699Skan break; 8488169699Skan case RID_UNSIGNED: 8489169699Skan dupe = specs->unsigned_p; 8490169699Skan if (specs->signed_p) 8491169699Skan error ("both %<signed%> and %<unsigned%> in " 8492169699Skan "declaration specifiers"); 8493169699Skan else if (specs->typespec_word == cts_void) 8494169699Skan error ("both %<unsigned%> and %<void%> in " 8495169699Skan "declaration specifiers"); 8496169699Skan else if (specs->typespec_word == cts_bool) 8497169699Skan error ("both %<unsigned%> and %<_Bool%> in " 8498169699Skan "declaration specifiers"); 8499169699Skan else if (specs->typespec_word == cts_float) 8500169699Skan error ("both %<unsigned%> and %<float%> in " 8501169699Skan "declaration specifiers"); 8502169699Skan else if (specs->typespec_word == cts_double) 8503169699Skan error ("both %<unsigned%> and %<double%> in " 8504169699Skan "declaration specifiers"); 8505169699Skan else if (specs->typespec_word == cts_dfloat32) 8506169699Skan error ("both %<unsigned%> and %<_Decimal32%> in " 8507169699Skan "declaration specifiers"); 8508169699Skan else if (specs->typespec_word == cts_dfloat64) 8509169699Skan error ("both %<unsigned%> and %<_Decimal64%> in " 8510169699Skan "declaration specifiers"); 8511169699Skan else if (specs->typespec_word == cts_dfloat128) 8512169699Skan error ("both %<unsigned%> and %<_Decimal128%> in " 8513169699Skan "declaration specifiers"); 8514169699Skan else 8515169699Skan specs->unsigned_p = true; 8516169699Skan break; 8517169699Skan case RID_COMPLEX: 8518169699Skan dupe = specs->complex_p; 8519169699Skan if (pedantic && !flag_isoc99 && !in_system_header) 8520169699Skan pedwarn ("ISO C90 does not support complex types"); 8521169699Skan if (specs->typespec_word == cts_void) 8522169699Skan error ("both %<complex%> and %<void%> in " 8523169699Skan "declaration specifiers"); 8524169699Skan else if (specs->typespec_word == cts_bool) 8525169699Skan error ("both %<complex%> and %<_Bool%> in " 8526169699Skan "declaration specifiers"); 8527169699Skan else if (specs->typespec_word == cts_dfloat32) 8528169699Skan error ("both %<complex%> and %<_Decimal32%> in " 8529169699Skan "declaration specifiers"); 8530169699Skan else if (specs->typespec_word == cts_dfloat64) 8531169699Skan error ("both %<complex%> and %<_Decimal64%> in " 8532169699Skan "declaration specifiers"); 8533169699Skan else if (specs->typespec_word == cts_dfloat128) 8534169699Skan error ("both %<complex%> and %<_Decimal128%> in " 8535169699Skan "declaration specifiers"); 8536169699Skan else 8537169699Skan specs->complex_p = true; 8538169699Skan break; 8539169699Skan default: 8540169699Skan gcc_unreachable (); 8541132730Skan } 8542169699Skan 8543169699Skan if (dupe) 8544169699Skan error ("duplicate %qE", type); 8545169699Skan 8546169699Skan return specs; 8547132730Skan } 8548169699Skan else 8549169699Skan { 8550169699Skan /* "void", "_Bool", "char", "int", "float" or "double". */ 8551169699Skan if (specs->typespec_word != cts_none) 8552169699Skan { 8553169699Skan error ("two or more data types in declaration specifiers"); 8554169699Skan return specs; 8555169699Skan } 8556169699Skan switch (i) 8557169699Skan { 8558169699Skan case RID_VOID: 8559169699Skan if (specs->long_p) 8560169699Skan error ("both %<long%> and %<void%> in " 8561169699Skan "declaration specifiers"); 8562169699Skan else if (specs->short_p) 8563169699Skan error ("both %<short%> and %<void%> in " 8564169699Skan "declaration specifiers"); 8565169699Skan else if (specs->signed_p) 8566169699Skan error ("both %<signed%> and %<void%> in " 8567169699Skan "declaration specifiers"); 8568169699Skan else if (specs->unsigned_p) 8569169699Skan error ("both %<unsigned%> and %<void%> in " 8570169699Skan "declaration specifiers"); 8571169699Skan else if (specs->complex_p) 8572169699Skan error ("both %<complex%> and %<void%> in " 8573169699Skan "declaration specifiers"); 8574169699Skan else 8575169699Skan specs->typespec_word = cts_void; 8576169699Skan return specs; 8577169699Skan case RID_BOOL: 8578169699Skan if (specs->long_p) 8579169699Skan error ("both %<long%> and %<_Bool%> in " 8580169699Skan "declaration specifiers"); 8581169699Skan else if (specs->short_p) 8582169699Skan error ("both %<short%> and %<_Bool%> in " 8583169699Skan "declaration specifiers"); 8584169699Skan else if (specs->signed_p) 8585169699Skan error ("both %<signed%> and %<_Bool%> in " 8586169699Skan "declaration specifiers"); 8587169699Skan else if (specs->unsigned_p) 8588169699Skan error ("both %<unsigned%> and %<_Bool%> in " 8589169699Skan "declaration specifiers"); 8590169699Skan else if (specs->complex_p) 8591169699Skan error ("both %<complex%> and %<_Bool%> in " 8592169699Skan "declaration specifiers"); 8593169699Skan else 8594169699Skan specs->typespec_word = cts_bool; 8595169699Skan return specs; 8596169699Skan case RID_CHAR: 8597169699Skan if (specs->long_p) 8598169699Skan error ("both %<long%> and %<char%> in " 8599169699Skan "declaration specifiers"); 8600169699Skan else if (specs->short_p) 8601169699Skan error ("both %<short%> and %<char%> in " 8602169699Skan "declaration specifiers"); 8603169699Skan else 8604169699Skan specs->typespec_word = cts_char; 8605169699Skan return specs; 8606169699Skan case RID_INT: 8607169699Skan specs->typespec_word = cts_int; 8608169699Skan return specs; 8609169699Skan case RID_FLOAT: 8610169699Skan if (specs->long_p) 8611169699Skan error ("both %<long%> and %<float%> in " 8612169699Skan "declaration specifiers"); 8613169699Skan else if (specs->short_p) 8614169699Skan error ("both %<short%> and %<float%> in " 8615169699Skan "declaration specifiers"); 8616169699Skan else if (specs->signed_p) 8617169699Skan error ("both %<signed%> and %<float%> in " 8618169699Skan "declaration specifiers"); 8619169699Skan else if (specs->unsigned_p) 8620169699Skan error ("both %<unsigned%> and %<float%> in " 8621169699Skan "declaration specifiers"); 8622169699Skan else 8623169699Skan specs->typespec_word = cts_float; 8624169699Skan return specs; 8625169699Skan case RID_DOUBLE: 8626169699Skan if (specs->long_long_p) 8627169699Skan error ("both %<long long%> and %<double%> in " 8628169699Skan "declaration specifiers"); 8629169699Skan else if (specs->short_p) 8630169699Skan error ("both %<short%> and %<double%> in " 8631169699Skan "declaration specifiers"); 8632169699Skan else if (specs->signed_p) 8633169699Skan error ("both %<signed%> and %<double%> in " 8634169699Skan "declaration specifiers"); 8635169699Skan else if (specs->unsigned_p) 8636169699Skan error ("both %<unsigned%> and %<double%> in " 8637169699Skan "declaration specifiers"); 8638169699Skan else 8639169699Skan specs->typespec_word = cts_double; 8640169699Skan return specs; 8641169699Skan case RID_DFLOAT32: 8642169699Skan case RID_DFLOAT64: 8643169699Skan case RID_DFLOAT128: 8644169699Skan { 8645169699Skan const char *str; 8646169699Skan if (i == RID_DFLOAT32) 8647169699Skan str = "_Decimal32"; 8648169699Skan else if (i == RID_DFLOAT64) 8649169699Skan str = "_Decimal64"; 8650169699Skan else 8651169699Skan str = "_Decimal128"; 8652169699Skan if (specs->long_long_p) 8653169699Skan error ("both %<long long%> and %<%s%> in " 8654169699Skan "declaration specifiers", str); 8655169699Skan if (specs->long_p) 8656169699Skan error ("both %<long%> and %<%s%> in " 8657169699Skan "declaration specifiers", str); 8658169699Skan else if (specs->short_p) 8659169699Skan error ("both %<short%> and %<%s%> in " 8660169699Skan "declaration specifiers", str); 8661169699Skan else if (specs->signed_p) 8662169699Skan error ("both %<signed%> and %<%s%> in " 8663169699Skan "declaration specifiers", str); 8664169699Skan else if (specs->unsigned_p) 8665169699Skan error ("both %<unsigned%> and %<%s%> in " 8666169699Skan "declaration specifiers", str); 8667169699Skan else if (specs->complex_p) 8668169699Skan error ("both %<complex%> and %<%s%> in " 8669169699Skan "declaration specifiers", str); 8670169699Skan else if (i == RID_DFLOAT32) 8671169699Skan specs->typespec_word = cts_dfloat32; 8672169699Skan else if (i == RID_DFLOAT64) 8673169699Skan specs->typespec_word = cts_dfloat64; 8674169699Skan else 8675169699Skan specs->typespec_word = cts_dfloat128; 8676169699Skan } 8677169699Skan if (!targetm.decimal_float_supported_p ()) 8678169699Skan error ("decimal floating point not supported for this target"); 8679169699Skan if (pedantic) 8680169699Skan pedwarn ("ISO C does not support decimal floating point"); 8681169699Skan return specs; 8682169699Skan default: 8683169699Skan /* ObjC reserved word "id", handled below. */ 8684169699Skan break; 8685169699Skan } 8686169699Skan } 8687169699Skan } 8688132730Skan 8689169699Skan /* Now we have a typedef (a TYPE_DECL node), an identifier (some 8690169699Skan form of ObjC type, cases such as "int" and "long" being handled 8691169699Skan above), a TYPE (struct, union, enum and typeof specifiers) or an 8692169699Skan ERROR_MARK. In none of these cases may there have previously 8693169699Skan been any type specifiers. */ 8694169699Skan if (specs->type || specs->typespec_word != cts_none 8695169699Skan || specs->long_p || specs->short_p || specs->signed_p 8696169699Skan || specs->unsigned_p || specs->complex_p) 8697169699Skan error ("two or more data types in declaration specifiers"); 8698169699Skan else if (TREE_CODE (type) == TYPE_DECL) 8699169699Skan { 8700169699Skan if (TREE_TYPE (type) == error_mark_node) 8701169699Skan ; /* Allow the type to default to int to avoid cascading errors. */ 8702169699Skan else 8703132730Skan { 8704169699Skan specs->type = TREE_TYPE (type); 8705169699Skan specs->decl_attr = DECL_ATTRIBUTES (type); 8706169699Skan specs->typedef_p = true; 8707169699Skan specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type); 8708169699Skan } 8709169699Skan } 8710169699Skan else if (TREE_CODE (type) == IDENTIFIER_NODE) 8711169699Skan { 8712169699Skan tree t = lookup_name (type); 8713169699Skan if (!t || TREE_CODE (t) != TYPE_DECL) 8714169699Skan error ("%qE fails to be a typedef or built in type", type); 8715169699Skan else if (TREE_TYPE (t) == error_mark_node) 8716169699Skan ; 8717169699Skan else 8718169699Skan specs->type = TREE_TYPE (t); 8719169699Skan } 8720169699Skan else if (TREE_CODE (type) != ERROR_MARK) 8721169699Skan { 8722169699Skan if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref) 8723169699Skan specs->tag_defined_p = true; 8724169699Skan if (spec.kind == ctsk_typeof) 8725169699Skan specs->typedef_p = true; 8726169699Skan specs->type = type; 8727169699Skan } 8728132730Skan 8729169699Skan return specs; 8730169699Skan} 8731132730Skan 8732169699Skan/* Add the storage class specifier or function specifier SCSPEC to the 8733169699Skan declaration specifiers SPECS, returning SPECS. */ 8734169699Skan 8735169699Skanstruct c_declspecs * 8736169699Skandeclspecs_add_scspec (struct c_declspecs *specs, tree scspec) 8737169699Skan{ 8738169699Skan enum rid i; 8739169699Skan enum c_storage_class n = csc_none; 8740169699Skan bool dupe = false; 8741169699Skan specs->declspecs_seen_p = true; 8742169699Skan gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE 8743169699Skan && C_IS_RESERVED_WORD (scspec)); 8744169699Skan i = C_RID_CODE (scspec); 8745169699Skan if (extra_warnings && specs->non_sc_seen_p) 8746169699Skan warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec); 8747169699Skan switch (i) 8748169699Skan { 8749169699Skan case RID_INLINE: 8750169699Skan /* C99 permits duplicate inline. Although of doubtful utility, 8751169699Skan it seems simplest to permit it in gnu89 mode as well, as 8752169699Skan there is also little utility in maintaining this as a 8753169699Skan difference between gnu89 and C99 inline. */ 8754169699Skan dupe = false; 8755169699Skan specs->inline_p = true; 8756169699Skan break; 8757169699Skan case RID_THREAD: 8758169699Skan dupe = specs->thread_p; 8759169699Skan if (specs->storage_class == csc_auto) 8760169699Skan error ("%<__thread%> used with %<auto%>"); 8761169699Skan else if (specs->storage_class == csc_register) 8762169699Skan error ("%<__thread%> used with %<register%>"); 8763169699Skan else if (specs->storage_class == csc_typedef) 8764169699Skan error ("%<__thread%> used with %<typedef%>"); 8765169699Skan else 8766169699Skan specs->thread_p = true; 8767169699Skan break; 8768169699Skan case RID_AUTO: 8769169699Skan n = csc_auto; 8770169699Skan break; 8771169699Skan case RID_EXTERN: 8772169699Skan n = csc_extern; 8773169699Skan /* Diagnose "__thread extern". */ 8774169699Skan if (specs->thread_p) 8775169699Skan error ("%<__thread%> before %<extern%>"); 8776169699Skan break; 8777169699Skan case RID_REGISTER: 8778169699Skan n = csc_register; 8779169699Skan break; 8780169699Skan case RID_STATIC: 8781169699Skan n = csc_static; 8782169699Skan /* Diagnose "__thread static". */ 8783169699Skan if (specs->thread_p) 8784169699Skan error ("%<__thread%> before %<static%>"); 8785169699Skan break; 8786169699Skan case RID_TYPEDEF: 8787169699Skan n = csc_typedef; 8788169699Skan break; 8789169699Skan default: 8790169699Skan gcc_unreachable (); 8791169699Skan } 8792169699Skan if (n != csc_none && n == specs->storage_class) 8793169699Skan dupe = true; 8794169699Skan if (dupe) 8795169699Skan error ("duplicate %qE", scspec); 8796169699Skan if (n != csc_none) 8797169699Skan { 8798169699Skan if (specs->storage_class != csc_none && n != specs->storage_class) 8799169699Skan { 8800169699Skan error ("multiple storage classes in declaration specifiers"); 8801132730Skan } 8802169699Skan else 8803169699Skan { 8804169699Skan specs->storage_class = n; 8805169699Skan if (n != csc_extern && n != csc_static && specs->thread_p) 8806169699Skan { 8807169699Skan error ("%<__thread%> used with %qE", scspec); 8808169699Skan specs->thread_p = false; 8809169699Skan } 8810169699Skan } 8811169699Skan } 8812169699Skan return specs; 8813169699Skan} 8814132730Skan 8815169699Skan/* Add the attributes ATTRS to the declaration specifiers SPECS, 8816169699Skan returning SPECS. */ 8817169699Skan 8818169699Skanstruct c_declspecs * 8819169699Skandeclspecs_add_attrs (struct c_declspecs *specs, tree attrs) 8820169699Skan{ 8821169699Skan specs->attrs = chainon (attrs, specs->attrs); 8822169699Skan specs->declspecs_seen_p = true; 8823169699Skan return specs; 8824132730Skan} 8825132730Skan 8826169699Skan/* Combine "long", "short", "signed", "unsigned" and "_Complex" type 8827169699Skan specifiers with any other type specifier to determine the resulting 8828169699Skan type. This is where ISO C checks on complex types are made, since 8829169699Skan "_Complex long" is a prefix of the valid ISO C type "_Complex long 8830169699Skan double". */ 8831132730Skan 8832169699Skanstruct c_declspecs * 8833169699Skanfinish_declspecs (struct c_declspecs *specs) 8834132730Skan{ 8835169699Skan /* If a type was specified as a whole, we have no modifiers and are 8836169699Skan done. */ 8837169699Skan if (specs->type != NULL_TREE) 8838169699Skan { 8839169699Skan gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p 8840169699Skan && !specs->signed_p && !specs->unsigned_p 8841169699Skan && !specs->complex_p); 8842169699Skan return specs; 8843169699Skan } 8844132730Skan 8845169699Skan /* If none of "void", "_Bool", "char", "int", "float" or "double" 8846169699Skan has been specified, treat it as "int" unless "_Complex" is 8847169699Skan present and there are no other specifiers. If we just have 8848169699Skan "_Complex", it is equivalent to "_Complex double", but e.g. 8849169699Skan "_Complex short" is equivalent to "_Complex short int". */ 8850169699Skan if (specs->typespec_word == cts_none) 8851132730Skan { 8852169699Skan if (specs->long_p || specs->short_p 8853169699Skan || specs->signed_p || specs->unsigned_p) 8854169699Skan { 8855169699Skan specs->typespec_word = cts_int; 8856169699Skan } 8857169699Skan else if (specs->complex_p) 8858169699Skan { 8859169699Skan specs->typespec_word = cts_double; 8860169699Skan if (pedantic) 8861169699Skan pedwarn ("ISO C does not support plain %<complex%> meaning " 8862169699Skan "%<double complex%>"); 8863169699Skan } 8864169699Skan else 8865169699Skan { 8866169699Skan specs->typespec_word = cts_int; 8867169699Skan specs->default_int_p = true; 8868169699Skan /* We don't diagnose this here because grokdeclarator will 8869169699Skan give more specific diagnostics according to whether it is 8870169699Skan a function definition. */ 8871169699Skan } 8872169699Skan } 8873132730Skan 8874169699Skan /* If "signed" was specified, record this to distinguish "int" and 8875169699Skan "signed int" in the case of a bit-field with 8876169699Skan -funsigned-bitfields. */ 8877169699Skan specs->explicit_signed_p = specs->signed_p; 8878132730Skan 8879169699Skan /* Now compute the actual type. */ 8880169699Skan switch (specs->typespec_word) 8881169699Skan { 8882169699Skan case cts_void: 8883169699Skan gcc_assert (!specs->long_p && !specs->short_p 8884169699Skan && !specs->signed_p && !specs->unsigned_p 8885169699Skan && !specs->complex_p); 8886169699Skan specs->type = void_type_node; 8887169699Skan break; 8888169699Skan case cts_bool: 8889169699Skan gcc_assert (!specs->long_p && !specs->short_p 8890169699Skan && !specs->signed_p && !specs->unsigned_p 8891169699Skan && !specs->complex_p); 8892169699Skan specs->type = boolean_type_node; 8893169699Skan break; 8894169699Skan case cts_char: 8895169699Skan gcc_assert (!specs->long_p && !specs->short_p); 8896169699Skan gcc_assert (!(specs->signed_p && specs->unsigned_p)); 8897169699Skan if (specs->signed_p) 8898169699Skan specs->type = signed_char_type_node; 8899169699Skan else if (specs->unsigned_p) 8900169699Skan specs->type = unsigned_char_type_node; 8901169699Skan else 8902169699Skan specs->type = char_type_node; 8903169699Skan if (specs->complex_p) 8904169699Skan { 8905169699Skan if (pedantic) 8906169699Skan pedwarn ("ISO C does not support complex integer types"); 8907169699Skan specs->type = build_complex_type (specs->type); 8908169699Skan } 8909169699Skan break; 8910169699Skan case cts_int: 8911169699Skan gcc_assert (!(specs->long_p && specs->short_p)); 8912169699Skan gcc_assert (!(specs->signed_p && specs->unsigned_p)); 8913169699Skan if (specs->long_long_p) 8914169699Skan specs->type = (specs->unsigned_p 8915169699Skan ? long_long_unsigned_type_node 8916169699Skan : long_long_integer_type_node); 8917169699Skan else if (specs->long_p) 8918169699Skan specs->type = (specs->unsigned_p 8919169699Skan ? long_unsigned_type_node 8920169699Skan : long_integer_type_node); 8921169699Skan else if (specs->short_p) 8922169699Skan specs->type = (specs->unsigned_p 8923169699Skan ? short_unsigned_type_node 8924169699Skan : short_integer_type_node); 8925169699Skan else 8926169699Skan specs->type = (specs->unsigned_p 8927169699Skan ? unsigned_type_node 8928169699Skan : integer_type_node); 8929169699Skan if (specs->complex_p) 8930169699Skan { 8931169699Skan if (pedantic) 8932169699Skan pedwarn ("ISO C does not support complex integer types"); 8933169699Skan specs->type = build_complex_type (specs->type); 8934169699Skan } 8935169699Skan break; 8936169699Skan case cts_float: 8937169699Skan gcc_assert (!specs->long_p && !specs->short_p 8938169699Skan && !specs->signed_p && !specs->unsigned_p); 8939169699Skan specs->type = (specs->complex_p 8940169699Skan ? complex_float_type_node 8941169699Skan : float_type_node); 8942169699Skan break; 8943169699Skan case cts_double: 8944169699Skan gcc_assert (!specs->long_long_p && !specs->short_p 8945169699Skan && !specs->signed_p && !specs->unsigned_p); 8946169699Skan if (specs->long_p) 8947169699Skan { 8948169699Skan specs->type = (specs->complex_p 8949169699Skan ? complex_long_double_type_node 8950169699Skan : long_double_type_node); 8951169699Skan } 8952169699Skan else 8953169699Skan { 8954169699Skan specs->type = (specs->complex_p 8955169699Skan ? complex_double_type_node 8956169699Skan : double_type_node); 8957169699Skan } 8958169699Skan break; 8959169699Skan case cts_dfloat32: 8960169699Skan case cts_dfloat64: 8961169699Skan case cts_dfloat128: 8962169699Skan gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p 8963169699Skan && !specs->signed_p && !specs->unsigned_p && !specs->complex_p); 8964169699Skan if (specs->typespec_word == cts_dfloat32) 8965169699Skan specs->type = dfloat32_type_node; 8966169699Skan else if (specs->typespec_word == cts_dfloat64) 8967169699Skan specs->type = dfloat64_type_node; 8968169699Skan else 8969169699Skan specs->type = dfloat128_type_node; 8970169699Skan break; 8971169699Skan default: 8972169699Skan gcc_unreachable (); 8973169699Skan } 8974132730Skan 8975169699Skan return specs; 8976169699Skan} 8977132730Skan 8978169699Skan/* Synthesize a function which calls all the global ctors or global 8979169699Skan dtors in this file. This is only used for targets which do not 8980169699Skan support .ctors/.dtors sections. FIXME: Migrate into cgraph. */ 8981169699Skanstatic void 8982169699Skanbuild_cdtor (int method_type, tree cdtors) 8983169699Skan{ 8984169699Skan tree body = 0; 8985132730Skan 8986169699Skan if (!cdtors) 8987169699Skan return; 8988169699Skan 8989169699Skan for (; cdtors; cdtors = TREE_CHAIN (cdtors)) 8990169699Skan append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0), 8991169699Skan &body); 8992169699Skan 8993169699Skan cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY); 8994169699Skan} 8995169699Skan 8996169699Skan/* A subroutine of c_write_global_declarations. Perform final processing 8997169699Skan on one file scope's declarations (or the external scope's declarations), 8998169699Skan GLOBALS. */ 8999169699Skan 9000169699Skanstatic void 9001169699Skanc_write_global_declarations_1 (tree globals) 9002169699Skan{ 9003169699Skan tree decl; 9004169699Skan bool reconsider; 9005169699Skan 9006169699Skan /* Process the decls in the order they were written. */ 9007169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 9008169699Skan { 9009169699Skan /* Check for used but undefined static functions using the C 9010169699Skan standard's definition of "used", and set TREE_NO_WARNING so 9011169699Skan that check_global_declarations doesn't repeat the check. */ 9012169699Skan if (TREE_CODE (decl) == FUNCTION_DECL 9013169699Skan && DECL_INITIAL (decl) == 0 9014169699Skan && DECL_EXTERNAL (decl) 9015169699Skan && !TREE_PUBLIC (decl) 9016169699Skan && C_DECL_USED (decl)) 9017169699Skan { 9018169699Skan pedwarn ("%q+F used but never defined", decl); 9019169699Skan TREE_NO_WARNING (decl) = 1; 9020169699Skan } 9021169699Skan 9022169699Skan wrapup_global_declaration_1 (decl); 9023132730Skan } 9024169699Skan 9025169699Skan do 9026169699Skan { 9027169699Skan reconsider = false; 9028169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 9029169699Skan reconsider |= wrapup_global_declaration_2 (decl); 9030169699Skan } 9031169699Skan while (reconsider); 9032169699Skan 9033169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 9034169699Skan check_global_declaration_1 (decl); 9035132730Skan} 9036132730Skan 9037169699Skan/* A subroutine of c_write_global_declarations Emit debug information for each 9038169699Skan of the declarations in GLOBALS. */ 9039132730Skan 9040169699Skanstatic void 9041169699Skanc_write_global_declarations_2 (tree globals) 9042169699Skan{ 9043169699Skan tree decl; 9044169699Skan 9045169699Skan for (decl = globals; decl ; decl = TREE_CHAIN (decl)) 9046169699Skan debug_hooks->global_decl (decl); 9047169699Skan} 9048169699Skan 9049169699Skan/* Preserve the external declarations scope across a garbage collect. */ 9050169699Skanstatic GTY(()) tree ext_block; 9051169699Skan 9052132730Skanvoid 9053169699Skanc_write_global_declarations (void) 9054132730Skan{ 9055169699Skan tree t; 9056132730Skan 9057169699Skan /* We don't want to do this if generating a PCH. */ 9058169699Skan if (pch_file) 9059169699Skan return; 9060132730Skan 9061169699Skan /* Don't waste time on further processing if -fsyntax-only or we've 9062169699Skan encountered errors. */ 9063169699Skan if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in)) 9064169699Skan return; 9065132730Skan 9066169699Skan /* Close the external scope. */ 9067169699Skan ext_block = pop_scope (); 9068169699Skan external_scope = 0; 9069169699Skan gcc_assert (!current_scope); 9070169699Skan 9071169699Skan if (ext_block) 9072169699Skan { 9073169699Skan tree tmp = BLOCK_VARS (ext_block); 9074169699Skan int flags; 9075169699Skan FILE * stream = dump_begin (TDI_tu, &flags); 9076169699Skan if (stream && tmp) 9077169699Skan { 9078169699Skan dump_node (tmp, flags & ~TDF_SLIM, stream); 9079169699Skan dump_end (TDI_tu, stream); 9080169699Skan } 9081169699Skan } 9082169699Skan 9083169699Skan /* Process all file scopes in this compilation, and the external_scope, 9084169699Skan through wrapup_global_declarations and check_global_declarations. */ 9085169699Skan for (t = all_translation_units; t; t = TREE_CHAIN (t)) 9086169699Skan c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t))); 9087169699Skan c_write_global_declarations_1 (BLOCK_VARS (ext_block)); 9088169699Skan 9089169699Skan /* Generate functions to call static constructors and destructors 9090169699Skan for targets that do not support .ctors/.dtors sections. These 9091169699Skan functions have magic names which are detected by collect2. */ 9092169699Skan build_cdtor ('I', static_ctors); static_ctors = 0; 9093169699Skan build_cdtor ('D', static_dtors); static_dtors = 0; 9094169699Skan 9095169699Skan /* We're done parsing; proceed to optimize and emit assembly. 9096169699Skan FIXME: shouldn't be the front end's responsibility to call this. */ 9097169699Skan cgraph_optimize (); 9098169699Skan 9099169699Skan /* After cgraph has had a chance to emit everything that's going to 9100169699Skan be emitted, output debug information for globals. */ 9101169699Skan if (errorcount == 0 && sorrycount == 0) 9102169699Skan { 9103169699Skan timevar_push (TV_SYMOUT); 9104169699Skan for (t = all_translation_units; t; t = TREE_CHAIN (t)) 9105169699Skan c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t))); 9106169699Skan c_write_global_declarations_2 (BLOCK_VARS (ext_block)); 9107169699Skan timevar_pop (TV_SYMOUT); 9108169699Skan } 9109169699Skan 9110169699Skan ext_block = NULL; 9111132730Skan} 9112132730Skan 9113261188Spfg/* APPLE LOCAL begin radar 5741070 */ 9114261188Spfg 9115261188Spfg/* Given an IDENTIFIER tree for a class interface, find (if possible) and 9116261188Spfg return the record type for the class interface. */ 9117261188Spfg 9118261188Spfgtree 9119261188Spfgc_return_interface_record_type (tree typename) 9120261188Spfg{ 9121261188Spfg enum tree_code_class class; 9122261188Spfg enum tree_code code; 9123261188Spfg tree retval = NULL; 9124261188Spfg 9125261188Spfg if (typename == NULL) 9126261188Spfg return retval; 9127261188Spfg 9128261188Spfg code = TREE_CODE (typename); 9129261188Spfg class = TREE_CODE_CLASS (code); 9130261188Spfg 9131261188Spfg if (code != IDENTIFIER_NODE 9132261188Spfg || class != tcc_exceptional) 9133261188Spfg return retval; 9134261188Spfg 9135261188Spfg retval = I_TAG_DECL (typename); 9136261188Spfg 9137261188Spfg if (TREE_CODE (retval) != RECORD_TYPE) 9138261188Spfg retval = NULL; 9139261188Spfg 9140261188Spfg return retval; 9141261188Spfg} 9142261188Spfg/* APPLE LOCAL end radar 5741070 */ 9143261188Spfg 9144117421Skan#include "gt-c-decl.h" 9145