c-decl.c revision 259705
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: stable/9/contrib/gcc/c-decl.c 259705 2013-12-22 00:45:08Z pfg $ */ 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 */ 7418334Speter PARM, /* Declaration of parm before function body */ 7518334Speter FIELD, /* Declaration inside struct or union */ 7618334Speter TYPENAME}; /* Typename (inside cast or sizeof) */ 7718334Speter 7818334Speter 7918334Speter/* Nonzero if we have seen an invalid cross reference 8018334Speter to a struct, union, or enum, but not yet printed the message. */ 81169699Skantree pending_invalid_xref; 8218334Speter 8318334Speter/* File and line to appear in the eventual error message. */ 84132730Skanlocation_t pending_invalid_xref_location; 8518334Speter 86169699Skan/* True means we've initialized exception handling. */ 87169699Skanbool c_eh_initialized_p; 88169699Skan 8918334Speter/* While defining an enum type, this is 1 plus the last enumerator 9018334Speter constant value. Note that will do not have to save this or `enum_overflow' 9118334Speter around nested function definition since such a definition could only 9218334Speter occur in an enum value expression and we don't use these variables in 9318334Speter that case. */ 9418334Speter 9518334Speterstatic tree enum_next_value; 9618334Speter 9718334Speter/* Nonzero means that there was overflow computing enum_next_value. */ 9818334Speter 9918334Speterstatic int enum_overflow; 10018334Speter 101169699Skan/* The file and line that the prototype came from if this is an 102169699Skan old-style definition; used for diagnostics in 103169699Skan store_parm_decls_oldstyle. */ 10418334Speter 105169699Skanstatic location_t current_function_prototype_locus; 10618334Speter 107169699Skan/* Whether this prototype was built-in. */ 10818334Speter 109169699Skanstatic bool current_function_prototype_built_in; 11018334Speter 111169699Skan/* The argument type information of this prototype. */ 112132730Skan 113169699Skanstatic tree current_function_prototype_arg_types; 114132730Skan 115169699Skan/* The argument information structure for the function currently being 116169699Skan defined. */ 11718334Speter 118169699Skanstatic struct c_arg_info *current_function_arg_info; 11918334Speter 120169699Skan/* The obstack on which parser and related data structures, which are 121169699Skan not live beyond their top-level declaration or definition, are 122169699Skan allocated. */ 123169699Skanstruct obstack parser_obstack; 124132730Skan 12590075Sobrien/* The current statement tree. */ 12690075Sobrien 127117421Skanstatic GTY(()) struct stmt_tree_s c_stmt_tree; 12890075Sobrien 129132730Skan/* State saving variables. */ 130169699Skantree c_break_label; 131169699Skantree c_cont_label; 13218334Speter 133169699Skan/* Linked list of TRANSLATION_UNIT_DECLS for the translation units 134169699Skan included in this invocation. Note that the current translation 135169699Skan unit is not included in this list. */ 13618334Speter 137169699Skanstatic GTY(()) tree all_translation_units; 13818334Speter 139169699Skan/* A list of decls to be made automatically visible in each file scope. */ 140169699Skanstatic GTY(()) tree visible_builtins; 14118334Speter 14218334Speter/* Set to 0 at beginning of a function definition, set to 1 if 14318334Speter a return statement that specifies a return value is seen. */ 14418334Speter 14518334Speterint current_function_returns_value; 14618334Speter 14718334Speter/* Set to 0 at beginning of a function definition, set to 1 if 14818334Speter a return statement with no argument is seen. */ 14918334Speter 15018334Speterint current_function_returns_null; 15118334Speter 15296263Sobrien/* Set to 0 at beginning of a function definition, set to 1 if 15396263Sobrien a call to a noreturn function is seen. */ 15496263Sobrien 15596263Sobrienint current_function_returns_abnormally; 15696263Sobrien 15718334Speter/* Set to nonzero by `grokdeclarator' for a function 15818334Speter whose return type is defaulted, if warnings for this are desired. */ 15918334Speter 16018334Speterstatic int warn_about_return_type; 16118334Speter 162169699Skan/* Nonzero when the current toplevel function contains a declaration 163169699Skan of a nested function which is never defined. */ 164169699Skan 165169699Skanstatic bool undef_nested_function; 166169699Skan 167169699Skan/* True means global_bindings_p should return false even if the scope stack 168169699Skan says we are in file scope. */ 169169699Skanbool c_override_global_bindings_to_false; 170169699Skan 17118334Speter 172169699Skan/* Each c_binding structure describes one binding of an identifier to 173169699Skan a decl. All the decls in a scope - irrespective of namespace - are 174169699Skan chained together by the ->prev field, which (as the name implies) 175169699Skan runs in reverse order. All the decls in a given namespace bound to 176169699Skan a given identifier are chained by the ->shadowed field, which runs 177169699Skan from inner to outer scopes. 17818334Speter 179169699Skan The ->decl field usually points to a DECL node, but there are two 180169699Skan exceptions. In the namespace of type tags, the bound entity is a 181169699Skan RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared 182169699Skan identifier is encountered, it is bound to error_mark_node to 183169699Skan suppress further errors about that identifier in the current 184169699Skan function. 185169699Skan 186169699Skan The ->type field stores the type of the declaration in this scope; 187169699Skan if NULL, the type is the type of the ->decl field. This is only of 188169699Skan relevance for objects with external or internal linkage which may 189169699Skan be redeclared in inner scopes, forming composite types that only 190169699Skan persist for the duration of those scopes. In the external scope, 191169699Skan this stores the composite of all the types declared for this 192169699Skan object, visible or not. The ->inner_comp field (used only at file 193169699Skan scope) stores whether an incomplete array type at file scope was 194169699Skan completed at an inner scope to an array size other than 1. 195169699Skan 196169699Skan The depth field is copied from the scope structure that holds this 197169699Skan decl. It is used to preserve the proper ordering of the ->shadowed 198169699Skan field (see bind()) and also for a handful of special-case checks. 199169699Skan Finally, the invisible bit is true for a decl which should be 200169699Skan ignored for purposes of normal name lookup, and the nested bit is 201169699Skan true for a decl that's been bound a second time in an inner scope; 202169699Skan in all such cases, the binding in the outer scope will have its 203169699Skan invisible bit true. */ 204169699Skan 205169699Skanstruct c_binding GTY((chain_next ("%h.prev"))) 206169699Skan{ 207169699Skan tree decl; /* the decl bound */ 208169699Skan tree type; /* the type in this scope */ 209169699Skan tree id; /* the identifier it's bound to */ 210169699Skan struct c_binding *prev; /* the previous decl in this scope */ 211169699Skan struct c_binding *shadowed; /* the innermost decl shadowed by this one */ 212169699Skan unsigned int depth : 28; /* depth of this scope */ 213169699Skan BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */ 214169699Skan BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */ 215169699Skan BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */ 216169699Skan /* one free bit */ 217169699Skan}; 218169699Skan#define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth) 219169699Skan#define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth) 220169699Skan#define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/) 221169699Skan#define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/) 222169699Skan 223169699Skan#define I_SYMBOL_BINDING(node) \ 224169699Skan (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding) 225169699Skan#define I_SYMBOL_DECL(node) \ 226169699Skan (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0) 227169699Skan 228169699Skan#define I_TAG_BINDING(node) \ 229169699Skan (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding) 230169699Skan#define I_TAG_DECL(node) \ 231169699Skan (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0) 232169699Skan 233169699Skan#define I_LABEL_BINDING(node) \ 234169699Skan (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding) 235169699Skan#define I_LABEL_DECL(node) \ 236169699Skan (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0) 237169699Skan 238169699Skan/* Each C symbol points to three linked lists of c_binding structures. 239169699Skan These describe the values of the identifier in the three different 240169699Skan namespaces defined by the language. */ 241169699Skan 242169699Skanstruct lang_identifier GTY(()) 243169699Skan{ 244169699Skan struct c_common_identifier common_id; 245169699Skan struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */ 246169699Skan struct c_binding *tag_binding; /* struct/union/enum tags */ 247169699Skan struct c_binding *label_binding; /* labels */ 248169699Skan}; 249169699Skan 250169699Skan/* Validate c-lang.c's assumptions. */ 251169699Skanextern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate 252169699Skan[(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1]; 253169699Skan 254169699Skan/* The resulting tree type. */ 255169699Skan 256169699Skanunion lang_tree_node 257169699Skan GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"), 258169699Skan 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)"))) 259169699Skan{ 260169699Skan union tree_node GTY ((tag ("0"), 261169699Skan desc ("tree_node_structure (&%h)"))) 262169699Skan generic; 263169699Skan struct lang_identifier GTY ((tag ("1"))) identifier; 264169699Skan}; 265169699Skan 266169699Skan/* Each c_scope structure describes the complete contents of one 267169699Skan scope. Four scopes are distinguished specially: the innermost or 268169699Skan current scope, the innermost function scope, the file scope (always 269169699Skan the second to outermost) and the outermost or external scope. 270169699Skan 271132730Skan Most declarations are recorded in the current scope. 27218334Speter 273132730Skan All normal label declarations are recorded in the innermost 274132730Skan function scope, as are bindings of undeclared identifiers to 275132730Skan error_mark_node. (GCC permits nested functions as an extension, 276132730Skan hence the 'innermost' qualifier.) Explicitly declared labels 277132730Skan (using the __label__ extension) appear in the current scope. 27818334Speter 279169699Skan Being in the file scope (current_scope == file_scope) causes 280132730Skan special behavior in several places below. Also, under some 281132730Skan conditions the Objective-C front end records declarations in the 282169699Skan file scope even though that isn't the current scope. 28318334Speter 284169699Skan All declarations with external linkage are recorded in the external 285169699Skan scope, even if they aren't visible there; this models the fact that 286169699Skan such declarations are visible to the entire program, and (with a 287169699Skan bit of cleverness, see pushdecl) allows diagnosis of some violations 288169699Skan of C99 6.2.2p7 and 6.2.7p2: 28918334Speter 290169699Skan If, within the same translation unit, the same identifier appears 291169699Skan with both internal and external linkage, the behavior is 292169699Skan undefined. 29318334Speter 294169699Skan All declarations that refer to the same object or function shall 295169699Skan have compatible type; otherwise, the behavior is undefined. 296169699Skan 297169699Skan Initially only the built-in declarations, which describe compiler 298169699Skan intrinsic functions plus a subset of the standard library, are in 299169699Skan this scope. 300169699Skan 301169699Skan The order of the blocks list matters, and it is frequently appended 302169699Skan to. To avoid having to walk all the way to the end of the list on 303169699Skan each insertion, or reverse the list later, we maintain a pointer to 304169699Skan the last list entry. (FIXME: It should be feasible to use a reversed 305169699Skan list here.) 306169699Skan 307169699Skan The bindings list is strictly in reverse order of declarations; 308169699Skan pop_scope relies on this. */ 309169699Skan 310169699Skan 311169699Skanstruct c_scope GTY((chain_next ("%h.outer"))) 312132730Skan{ 313132730Skan /* The scope containing this one. */ 314132730Skan struct c_scope *outer; 31518334Speter 316132730Skan /* The next outermost function scope. */ 317132730Skan struct c_scope *outer_function; 31818334Speter 319169699Skan /* All bindings in this scope. */ 320169699Skan struct c_binding *bindings; 32118334Speter 322132730Skan /* For each scope (except the global one), a chain of BLOCK nodes 323132730Skan for all the scopes that were entered and exited one level down. */ 324132730Skan tree blocks; 325132730Skan tree blocks_last; 32618334Speter 327169699Skan /* The depth of this scope. Used to keep the ->shadowed chain of 328169699Skan bindings sorted innermost to outermost. */ 329169699Skan unsigned int depth : 28; 330169699Skan 331132730Skan /* True if we are currently filling this scope with parameter 332132730Skan declarations. */ 333132730Skan BOOL_BITFIELD parm_flag : 1; 33418334Speter 335169699Skan /* True if we saw [*] in this scope. Used to give an error messages 336169699Skan if these appears in a function definition. */ 337169699Skan BOOL_BITFIELD had_vla_unspec : 1; 338169699Skan 339132730Skan /* True if we already complained about forward parameter decls 340132730Skan in this scope. This prevents double warnings on 341132730Skan foo (int a; int b; ...) */ 342132730Skan BOOL_BITFIELD warned_forward_parm_decls : 1; 34390075Sobrien 344132730Skan /* True if this is the outermost block scope of a function body. 345132730Skan This scope contains the parameters, the local variables declared 346132730Skan in the outermost block, and all the labels (except those in 347132730Skan nested functions, or declared at block scope with __label__). */ 348132730Skan BOOL_BITFIELD function_body : 1; 34918334Speter 350132730Skan /* True means make a BLOCK for this scope no matter what. */ 351132730Skan BOOL_BITFIELD keep : 1; 352132730Skan}; 35318334Speter 354132730Skan/* The scope currently in effect. */ 35518334Speter 356132730Skanstatic GTY(()) struct c_scope *current_scope; 35718334Speter 358132730Skan/* The innermost function scope. Ordinary (not explicitly declared) 359132730Skan labels, bindings to error_mark_node, and the lazily-created 360132730Skan bindings of __func__ and its friends get this scope. */ 36118334Speter 362132730Skanstatic GTY(()) struct c_scope *current_function_scope; 36318334Speter 364169699Skan/* The C file scope. This is reset for each input translation unit. */ 36518334Speter 366169699Skanstatic GTY(()) struct c_scope *file_scope; 36718334Speter 368169699Skan/* The outermost scope. This is used for all declarations with 369169699Skan external linkage, and only these, hence the name. */ 370169699Skan 371169699Skanstatic GTY(()) struct c_scope *external_scope; 372169699Skan 373169699Skan/* A chain of c_scope structures awaiting reuse. */ 374169699Skan 375169699Skanstatic GTY((deletable)) struct c_scope *scope_freelist; 376169699Skan 377169699Skan/* A chain of c_binding structures awaiting reuse. */ 378169699Skan 379169699Skanstatic GTY((deletable)) struct c_binding *binding_freelist; 380169699Skan 381132730Skan/* Append VAR to LIST in scope SCOPE. */ 382132730Skan#define SCOPE_LIST_APPEND(scope, list, decl) do { \ 383132730Skan struct c_scope *s_ = (scope); \ 384132730Skan tree d_ = (decl); \ 385132730Skan if (s_->list##_last) \ 386132730Skan TREE_CHAIN (s_->list##_last) = d_; \ 387132730Skan else \ 388132730Skan s_->list = d_; \ 389132730Skan s_->list##_last = d_; \ 390132730Skan} while (0) 39118334Speter 392132730Skan/* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */ 393132730Skan#define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \ 394132730Skan struct c_scope *t_ = (tscope); \ 395132730Skan struct c_scope *f_ = (fscope); \ 396132730Skan if (t_->to##_last) \ 397132730Skan TREE_CHAIN (t_->to##_last) = f_->from; \ 398132730Skan else \ 399132730Skan t_->to = f_->from; \ 400132730Skan t_->to##_last = f_->from##_last; \ 401132730Skan} while (0) 40290075Sobrien 403132730Skan/* True means unconditionally make a BLOCK for the next scope pushed. */ 40418334Speter 405132730Skanstatic bool keep_next_level_flag; 40618334Speter 407169699Skan/* True means the next call to push_scope will be the outermost scope 408132730Skan of a function body, so do not push a new scope, merely cease 409132730Skan expecting parameter decls. */ 410132730Skan 411132730Skanstatic bool next_is_function_body; 412132730Skan 41318334Speter/* Functions called automatically at the beginning and end of execution. */ 41418334Speter 415169699Skanstatic GTY(()) tree static_ctors; 416169699Skanstatic GTY(()) tree static_dtors; 41718334Speter 41818334Speter/* Forward declarations. */ 419169699Skanstatic tree lookup_name_in_scope (tree, struct c_scope *); 420169699Skanstatic tree c_make_fname_decl (tree, int); 421169699Skanstatic tree grokdeclarator (const struct c_declarator *, 422169699Skan struct c_declspecs *, 423169699Skan enum decl_context, bool, tree *); 424169699Skanstatic tree grokparms (struct c_arg_info *, bool); 425132730Skanstatic void layout_array_type (tree); 42618334Speter 427169699Skan/* T is a statement. Add it to the statement-tree. This is the 428169699Skan C/ObjC version--C++ has a slightly different version of this 429169699Skan function. */ 430169699Skan 431169699Skantree 432169699Skanadd_stmt (tree t) 433169699Skan{ 434169699Skan enum tree_code code = TREE_CODE (t); 435169699Skan 436169699Skan if (EXPR_P (t) && code != LABEL_EXPR) 437169699Skan { 438169699Skan if (!EXPR_HAS_LOCATION (t)) 439169699Skan SET_EXPR_LOCATION (t, input_location); 440169699Skan } 441169699Skan 442169699Skan if (code == LABEL_EXPR || code == CASE_LABEL_EXPR) 443169699Skan STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1; 444169699Skan 445169699Skan /* Add T to the statement-tree. Non-side-effect statements need to be 446169699Skan recorded during statement expressions. */ 447169699Skan append_to_statement_list_force (t, &cur_stmt_list); 448169699Skan 449169699Skan return t; 450169699Skan} 451169699Skan 45290075Sobrien/* States indicating how grokdeclarator() should handle declspecs marked 45390075Sobrien with __attribute__((deprecated)). An object declared as 45490075Sobrien __attribute__((deprecated)) suppresses warnings of uses of other 45590075Sobrien deprecated items. */ 456132730Skan 45790075Sobrienenum deprecated_states { 45890075Sobrien DEPRECATED_NORMAL, 45990075Sobrien DEPRECATED_SUPPRESS 46090075Sobrien}; 46190075Sobrien 46290075Sobrienstatic enum deprecated_states deprecated_state = DEPRECATED_NORMAL; 46390075Sobrien 46418334Spetervoid 465132730Skanc_print_identifier (FILE *file, tree node, int indent) 46618334Speter{ 467169699Skan print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4); 468169699Skan print_node (file, "tag", I_TAG_DECL (node), indent + 4); 469169699Skan print_node (file, "label", I_LABEL_DECL (node), indent + 4); 47090075Sobrien if (C_IS_RESERVED_WORD (node)) 47190075Sobrien { 47290075Sobrien tree rid = ridpointers[C_RID_CODE (node)]; 47390075Sobrien indent_to (file, indent + 4); 474169699Skan fprintf (file, "rid %p \"%s\"", 475132730Skan (void *) rid, IDENTIFIER_POINTER (rid)); 47690075Sobrien } 47718334Speter} 478169699Skan 479169699Skan/* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL, 480169699Skan which may be any of several kinds of DECL or TYPE or error_mark_node, 481169699Skan in the scope SCOPE. */ 482169699Skanstatic void 483169699Skanbind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested) 484169699Skan{ 485169699Skan struct c_binding *b, **here; 486169699Skan 487169699Skan if (binding_freelist) 488169699Skan { 489169699Skan b = binding_freelist; 490169699Skan binding_freelist = b->prev; 491169699Skan } 492169699Skan else 493169699Skan b = GGC_NEW (struct c_binding); 494169699Skan 495169699Skan b->shadowed = 0; 496169699Skan b->decl = decl; 497169699Skan b->id = name; 498169699Skan b->depth = scope->depth; 499169699Skan b->invisible = invisible; 500169699Skan b->nested = nested; 501169699Skan b->inner_comp = 0; 502169699Skan 503169699Skan b->type = 0; 504169699Skan 505169699Skan b->prev = scope->bindings; 506169699Skan scope->bindings = b; 507169699Skan 508169699Skan if (!name) 509169699Skan return; 510169699Skan 511169699Skan switch (TREE_CODE (decl)) 512169699Skan { 513169699Skan case LABEL_DECL: here = &I_LABEL_BINDING (name); break; 514169699Skan case ENUMERAL_TYPE: 515169699Skan case UNION_TYPE: 516169699Skan case RECORD_TYPE: here = &I_TAG_BINDING (name); break; 517169699Skan case VAR_DECL: 518169699Skan case FUNCTION_DECL: 519169699Skan case TYPE_DECL: 520169699Skan case CONST_DECL: 521169699Skan case PARM_DECL: 522169699Skan case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break; 523169699Skan 524169699Skan default: 525169699Skan gcc_unreachable (); 526169699Skan } 527169699Skan 528169699Skan /* Locate the appropriate place in the chain of shadowed decls 529169699Skan to insert this binding. Normally, scope == current_scope and 530169699Skan this does nothing. */ 531169699Skan while (*here && (*here)->depth > scope->depth) 532169699Skan here = &(*here)->shadowed; 533169699Skan 534169699Skan b->shadowed = *here; 535169699Skan *here = b; 536169699Skan} 537169699Skan 538169699Skan/* Clear the binding structure B, stick it on the binding_freelist, 539169699Skan and return the former value of b->prev. This is used by pop_scope 540169699Skan and get_parm_info to iterate destructively over all the bindings 541169699Skan from a given scope. */ 542169699Skanstatic struct c_binding * 543169699Skanfree_binding_and_advance (struct c_binding *b) 544169699Skan{ 545169699Skan struct c_binding *prev = b->prev; 546169699Skan 547169699Skan memset (b, 0, sizeof (struct c_binding)); 548169699Skan b->prev = binding_freelist; 549169699Skan binding_freelist = b; 550169699Skan 551169699Skan return prev; 552169699Skan} 553169699Skan 55418334Speter 55518334Speter/* Hook called at end of compilation to assume 1 elt 556132730Skan for a file-scope tentative array defn that wasn't complete before. */ 55790075Sobrien 55818334Spetervoid 559132730Skanc_finish_incomplete_decl (tree decl) 56018334Speter{ 56150397Sobrien if (TREE_CODE (decl) == VAR_DECL) 56218334Speter { 56318334Speter tree type = TREE_TYPE (decl); 56450397Sobrien if (type != error_mark_node 56550397Sobrien && TREE_CODE (type) == ARRAY_TYPE 566169699Skan && !DECL_EXTERNAL (decl) 56750397Sobrien && TYPE_DOMAIN (type) == 0) 56818334Speter { 569169699Skan warning (0, "array %q+D assumed to have one element", decl); 57050397Sobrien 571169699Skan complete_array_type (&TREE_TYPE (decl), NULL_TREE, true); 57218334Speter 57318334Speter layout_decl (decl, 0); 57418334Speter } 57518334Speter } 57618334Speter} 57718334Speter 578132730Skan/* The Objective-C front-end often needs to determine the current scope. */ 57918334Speter 580132730Skanvoid * 581169699Skanobjc_get_current_scope (void) 58218334Speter{ 583132730Skan return current_scope; 58418334Speter} 58518334Speter 586132730Skan/* The following function is used only by Objective-C. It needs to live here 587132730Skan because it accesses the innards of c_scope. */ 588132730Skan 58918334Spetervoid 590132730Skanobjc_mark_locals_volatile (void *enclosing_blk) 59118334Speter{ 592132730Skan struct c_scope *scope; 593169699Skan struct c_binding *b; 594132730Skan 595132730Skan for (scope = current_scope; 596132730Skan scope && scope != enclosing_blk; 597132730Skan scope = scope->outer) 598132730Skan { 599169699Skan for (b = scope->bindings; b; b = b->prev) 600169699Skan objc_volatilize_decl (b->decl); 601132730Skan 602132730Skan /* Do not climb up past the current function. */ 603132730Skan if (scope->function_body) 604132730Skan break; 605132730Skan } 60618334Speter} 60718334Speter 608169699Skan/* Nonzero if we are currently in file scope. */ 60918334Speter 61018334Speterint 611132730Skanglobal_bindings_p (void) 61218334Speter{ 613169699Skan return current_scope == file_scope && !c_override_global_bindings_to_false; 61418334Speter} 61518334Speter 616132730Skanvoid 617132730Skankeep_next_level (void) 618132730Skan{ 619132730Skan keep_next_level_flag = true; 620132730Skan} 62118334Speter 622132730Skan/* Identify this scope as currently being filled with parameters. */ 623132730Skan 62418334Spetervoid 625132730Skandeclare_parm_level (void) 62618334Speter{ 627132730Skan current_scope->parm_flag = true; 62818334Speter} 62918334Speter 63018334Spetervoid 631169699Skanpush_scope (void) 63218334Speter{ 633132730Skan if (next_is_function_body) 634132730Skan { 635132730Skan /* This is the transition from the parameters to the top level 636132730Skan of the function body. These are the same scope 637132730Skan (C99 6.2.1p4,6) so we do not push another scope structure. 638132730Skan next_is_function_body is set only by store_parm_decls, which 639132730Skan in turn is called when and only when we are about to 640132730Skan encounter the opening curly brace for the function body. 64118334Speter 642132730Skan The outermost block of a function always gets a BLOCK node, 643132730Skan because the debugging output routines expect that each 644132730Skan function has at least one BLOCK. */ 645132730Skan current_scope->parm_flag = false; 646132730Skan current_scope->function_body = true; 647132730Skan current_scope->keep = true; 648132730Skan current_scope->outer_function = current_function_scope; 649132730Skan current_function_scope = current_scope; 65018334Speter 651132730Skan keep_next_level_flag = false; 652132730Skan next_is_function_body = false; 653132730Skan } 654132730Skan else 65518334Speter { 656169699Skan struct c_scope *scope; 657169699Skan if (scope_freelist) 658169699Skan { 659169699Skan scope = scope_freelist; 660169699Skan scope_freelist = scope->outer; 661169699Skan } 662169699Skan else 663169699Skan scope = GGC_CNEW (struct c_scope); 664132730Skan 665132730Skan scope->keep = keep_next_level_flag; 666132730Skan scope->outer = current_scope; 667169699Skan scope->depth = current_scope ? (current_scope->depth + 1) : 0; 668169699Skan 669169699Skan /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but 670169699Skan possible. */ 671169699Skan if (current_scope && scope->depth == 0) 672169699Skan { 673169699Skan scope->depth--; 674169699Skan sorry ("GCC supports only %u nested scopes", scope->depth); 675169699Skan } 676169699Skan 677132730Skan current_scope = scope; 678132730Skan keep_next_level_flag = false; 67918334Speter } 68018334Speter} 68118334Speter 682169699Skan/* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */ 68350397Sobrien 684169699Skanstatic void 685169699Skanset_type_context (tree type, tree context) 686169699Skan{ 687169699Skan for (type = TYPE_MAIN_VARIANT (type); type; 688169699Skan type = TYPE_NEXT_VARIANT (type)) 689169699Skan TYPE_CONTEXT (type) = context; 690169699Skan} 69150397Sobrien 692169699Skan/* Exit a scope. Restore the state of the identifier-decl mappings 693169699Skan that were in effect when this scope was entered. Return a BLOCK 694169699Skan node containing all the DECLs in this scope that are of interest 695169699Skan to debug info generation. */ 69650397Sobrien 69718334Spetertree 698169699Skanpop_scope (void) 69918334Speter{ 700132730Skan struct c_scope *scope = current_scope; 701169699Skan tree block, context, p; 702169699Skan struct c_binding *b; 70318334Speter 704169699Skan bool functionbody = scope->function_body; 705169699Skan bool keep = functionbody || scope->keep || scope->bindings; 70618334Speter 707169699Skan c_end_vm_scope (scope->depth); 70818334Speter 709132730Skan /* If appropriate, create a BLOCK to record the decls for the life 710132730Skan of this function. */ 71118334Speter block = 0; 712132730Skan if (keep) 71318334Speter { 714132730Skan block = make_node (BLOCK); 715132730Skan BLOCK_SUBBLOCKS (block) = scope->blocks; 716132730Skan TREE_USED (block) = 1; 71718334Speter 718169699Skan /* In each subblock, record that this is its superior. */ 719169699Skan for (p = scope->blocks; p; p = TREE_CHAIN (p)) 720169699Skan BLOCK_SUPERCONTEXT (p) = block; 72118334Speter 722169699Skan BLOCK_VARS (block) = 0; 723169699Skan } 72418334Speter 725169699Skan /* The TYPE_CONTEXTs for all of the tagged types belonging to this 726169699Skan scope must be set so that they point to the appropriate 727169699Skan construct, i.e. either to the current FUNCTION_DECL node, or 728169699Skan else to the BLOCK node we just constructed. 72918334Speter 730169699Skan Note that for tagged types whose scope is just the formal 731169699Skan parameter list for some function type specification, we can't 732169699Skan properly set their TYPE_CONTEXTs here, because we don't have a 733169699Skan pointer to the appropriate FUNCTION_TYPE node readily available 734169699Skan to us. For those cases, the TYPE_CONTEXTs of the relevant tagged 735169699Skan type nodes get set in `grokdeclarator' as soon as we have created 736169699Skan the FUNCTION_TYPE node which will represent the "scope" for these 737169699Skan "parameter list local" tagged types. */ 738169699Skan if (scope->function_body) 739169699Skan context = current_function_decl; 740169699Skan else if (scope == file_scope) 741169699Skan { 742169699Skan tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0); 743169699Skan TREE_CHAIN (file_decl) = all_translation_units; 744169699Skan all_translation_units = file_decl; 745169699Skan context = file_decl; 746169699Skan } 747169699Skan else 748169699Skan context = block; 749132730Skan 750169699Skan /* Clear all bindings in this scope. */ 751169699Skan for (b = scope->bindings; b; b = free_binding_and_advance (b)) 75218334Speter { 753169699Skan p = b->decl; 754132730Skan switch (TREE_CODE (p)) 75518334Speter { 756132730Skan case LABEL_DECL: 757169699Skan /* Warnings for unused labels, errors for undefined labels. */ 758132730Skan if (TREE_USED (p) && !DECL_INITIAL (p)) 75918334Speter { 760169699Skan error ("label %q+D used but not defined", p); 761132730Skan DECL_INITIAL (p) = error_mark_node; 76218334Speter } 763132730Skan else if (!TREE_USED (p) && warn_unused_label) 764132730Skan { 765132730Skan if (DECL_INITIAL (p)) 766169699Skan warning (0, "label %q+D defined but not used", p); 767132730Skan else 768169699Skan warning (0, "label %q+D declared but not defined", p); 769132730Skan } 770169699Skan /* Labels go in BLOCK_VARS. */ 771169699Skan TREE_CHAIN (p) = BLOCK_VARS (block); 772169699Skan BLOCK_VARS (block) = p; 773169699Skan gcc_assert (I_LABEL_BINDING (b->id) == b); 774169699Skan I_LABEL_BINDING (b->id) = b->shadowed; 775169699Skan break; 77618334Speter 777169699Skan case ENUMERAL_TYPE: 778169699Skan case UNION_TYPE: 779169699Skan case RECORD_TYPE: 780169699Skan set_type_context (p, context); 781169699Skan 782169699Skan /* Types may not have tag-names, in which case the type 783169699Skan appears in the bindings list with b->id NULL. */ 784169699Skan if (b->id) 785169699Skan { 786169699Skan gcc_assert (I_TAG_BINDING (b->id) == b); 787169699Skan I_TAG_BINDING (b->id) = b->shadowed; 788169699Skan } 789132730Skan break; 79018334Speter 791132730Skan case FUNCTION_DECL: 792169699Skan /* Propagate TREE_ADDRESSABLE from nested functions to their 793169699Skan containing functions. */ 794169699Skan if (!TREE_ASM_WRITTEN (p) 795132730Skan && DECL_INITIAL (p) != 0 796132730Skan && TREE_ADDRESSABLE (p) 797132730Skan && DECL_ABSTRACT_ORIGIN (p) != 0 798132730Skan && DECL_ABSTRACT_ORIGIN (p) != p) 799132730Skan TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1; 800169699Skan if (!DECL_EXTERNAL (p) 801190404Sdas && DECL_INITIAL (p) == 0 802190404Sdas && scope != file_scope 803190404Sdas && scope != external_scope) 804169699Skan { 805169699Skan error ("nested function %q+D declared but never defined", p); 806169699Skan undef_nested_function = true; 807169699Skan } 808189824Sdas /* C99 6.7.4p6: "a function with external linkage... declared 809189824Sdas with an inline function specifier ... shall also be defined in the 810189824Sdas same translation unit." */ 811189824Sdas else if (DECL_DECLARED_INLINE_P (p) 812189824Sdas && TREE_PUBLIC (p) 813189824Sdas && !DECL_INITIAL (p) 814189824Sdas && !flag_gnu89_inline) 815189824Sdas pedwarn ("inline function %q+D declared but never defined", p); 816189824Sdas 817169699Skan goto common_symbol; 81818334Speter 819132730Skan case VAR_DECL: 820169699Skan /* Warnings for unused variables. */ 821169699Skan if (!TREE_USED (p) 822169699Skan && !TREE_NO_WARNING (p) 823132730Skan && !DECL_IN_SYSTEM_HEADER (p) 824132730Skan && DECL_NAME (p) 825169699Skan && !DECL_ARTIFICIAL (p) 826169699Skan && scope != file_scope 827169699Skan && scope != external_scope) 828169699Skan warning (OPT_Wunused_variable, "unused variable %q+D", p); 82918334Speter 830169699Skan if (b->inner_comp) 831132730Skan { 832169699Skan error ("type of array %q+D completed incompatibly with" 833169699Skan " implicit initialization", p); 834132730Skan } 835169699Skan 836169699Skan /* Fall through. */ 837169699Skan case TYPE_DECL: 838169699Skan case CONST_DECL: 839169699Skan common_symbol: 840169699Skan /* All of these go in BLOCK_VARS, but only if this is the 841169699Skan binding in the home scope. */ 842169699Skan if (!b->nested) 843169699Skan { 844169699Skan TREE_CHAIN (p) = BLOCK_VARS (block); 845169699Skan BLOCK_VARS (block) = p; 846169699Skan } 847169699Skan /* If this is the file scope, and we are processing more 848169699Skan than one translation unit in this compilation, set 849169699Skan DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL. 850169699Skan This makes same_translation_unit_p work, and causes 851169699Skan static declarations to be given disambiguating suffixes. */ 852169699Skan if (scope == file_scope && num_in_fnames > 1) 853169699Skan { 854169699Skan DECL_CONTEXT (p) = context; 855169699Skan if (TREE_CODE (p) == TYPE_DECL) 856169699Skan set_type_context (TREE_TYPE (p), context); 857169699Skan } 858169699Skan 859169699Skan /* Fall through. */ 860169699Skan /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have 861169699Skan already been put there by store_parm_decls. Unused- 862169699Skan parameter warnings are handled by function.c. 863169699Skan error_mark_node obviously does not go in BLOCK_VARS and 864169699Skan does not get unused-variable warnings. */ 865169699Skan case PARM_DECL: 866169699Skan case ERROR_MARK: 867169699Skan /* It is possible for a decl not to have a name. We get 868169699Skan here with b->id NULL in this case. */ 869169699Skan if (b->id) 870169699Skan { 871169699Skan gcc_assert (I_SYMBOL_BINDING (b->id) == b); 872169699Skan I_SYMBOL_BINDING (b->id) = b->shadowed; 873169699Skan if (b->shadowed && b->shadowed->type) 874169699Skan TREE_TYPE (b->shadowed->decl) = b->shadowed->type; 875169699Skan } 876132730Skan break; 877169699Skan 878169699Skan default: 879169699Skan gcc_unreachable (); 880132730Skan } 881132730Skan } 88250397Sobrien 88318334Speter 884169699Skan /* Dispose of the block that we just made inside some higher level. */ 885169699Skan if ((scope->function_body || scope == file_scope) && context) 886132730Skan { 887169699Skan DECL_INITIAL (context) = block; 888169699Skan BLOCK_SUPERCONTEXT (block) = context; 88918334Speter } 890132730Skan else if (scope->outer) 89118334Speter { 892132730Skan if (block) 893132730Skan SCOPE_LIST_APPEND (scope->outer, blocks, block); 894132730Skan /* If we did not make a block for the scope just exited, any 895132730Skan blocks made for inner scopes must be carried forward so they 896132730Skan will later become subblocks of something else. */ 897132730Skan else if (scope->blocks) 898132730Skan SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks); 89918334Speter } 90018334Speter 901132730Skan /* Pop the current scope, and free the structure for reuse. */ 902169699Skan current_scope = scope->outer; 903169699Skan if (scope->function_body) 904169699Skan current_function_scope = scope->outer_function; 90518334Speter 906169699Skan memset (scope, 0, sizeof (struct c_scope)); 907169699Skan scope->outer = scope_freelist; 908169699Skan scope_freelist = scope; 909169699Skan 91018334Speter return block; 91118334Speter} 91218334Speter 913169699Skanvoid 914169699Skanpush_file_scope (void) 915169699Skan{ 916169699Skan tree decl; 917169699Skan 918169699Skan if (file_scope) 919169699Skan return; 920169699Skan 921169699Skan push_scope (); 922169699Skan file_scope = current_scope; 923169699Skan 924169699Skan start_fname_decls (); 925169699Skan 926169699Skan for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl)) 927169699Skan bind (DECL_NAME (decl), decl, file_scope, 928169699Skan /*invisible=*/false, /*nested=*/true); 929169699Skan} 930169699Skan 931169699Skanvoid 932169699Skanpop_file_scope (void) 933169699Skan{ 934169699Skan /* In case there were missing closebraces, get us back to the global 935169699Skan binding level. */ 936169699Skan while (current_scope != file_scope) 937169699Skan pop_scope (); 938169699Skan 939169699Skan /* __FUNCTION__ is defined at file scope (""). This 940169699Skan call may not be necessary as my tests indicate it 941169699Skan still works without it. */ 942169699Skan finish_fname_decls (); 943169699Skan 944169699Skan /* This is the point to write out a PCH if we're doing that. 945169699Skan In that case we do not want to do anything else. */ 946169699Skan if (pch_file) 947169699Skan { 948169699Skan c_common_write_pch (); 949169699Skan return; 950169699Skan } 951169699Skan 952169699Skan /* Pop off the file scope and close this translation unit. */ 953169699Skan pop_scope (); 954169699Skan file_scope = 0; 955169699Skan 956169699Skan maybe_apply_pending_pragma_weaks (); 957169699Skan cgraph_finalize_compilation_unit (); 958169699Skan} 959169699Skan 960132730Skan/* Insert BLOCK at the end of the list of subblocks of the current 961132730Skan scope. This is used when a BIND_EXPR is expanded, to handle the 962132730Skan BLOCK node inside the BIND_EXPR. */ 96318334Speter 96418334Spetervoid 965132730Skaninsert_block (tree block) 96618334Speter{ 96718334Speter TREE_USED (block) = 1; 968132730Skan SCOPE_LIST_APPEND (current_scope, blocks, block); 96918334Speter} 97018334Speter 97118334Speter/* Push a definition or a declaration of struct, union or enum tag "name". 97218334Speter "type" should be the type node. 97318334Speter We assume that the tag "name" is not already defined. 97418334Speter 97518334Speter Note that the definition may really be just a forward reference. 97618334Speter In that case, the TYPE_SIZE will be zero. */ 97718334Speter 978169699Skanstatic void 979132730Skanpushtag (tree name, tree type) 98018334Speter{ 981132730Skan /* Record the identifier as the type's name if it has none. */ 982169699Skan if (name && !TYPE_NAME (type)) 983169699Skan TYPE_NAME (type) = name; 984169699Skan bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false); 985132730Skan 98618334Speter /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the 987132730Skan tagged type we just added to the current scope. This fake 98818334Speter NULL-named TYPE_DECL node helps dwarfout.c to know when it needs 98918334Speter to output a representation of a tagged type, and it also gives 99018334Speter us a convenient place to record the "scope start" address for the 99118334Speter tagged type. */ 99218334Speter 99318334Speter TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type)); 99450397Sobrien 99550397Sobrien /* An approximation for now, so we can tell this is a function-scope tag. 996169699Skan This will be updated in pop_scope. */ 99750397Sobrien TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type)); 99818334Speter} 99918334Speter 1000132730Skan/* Subroutine of compare_decls. Allow harmless mismatches in return 1001132730Skan and argument types provided that the type modes match. This function 1002132730Skan return a unified type given a suitable match, and 0 otherwise. */ 100318334Speter 1004132730Skanstatic tree 1005132730Skanmatch_builtin_function_types (tree newtype, tree oldtype) 1006132730Skan{ 1007132730Skan tree newrettype, oldrettype; 1008132730Skan tree newargs, oldargs; 1009132730Skan tree trytype, tryargs; 101018334Speter 1011132730Skan /* Accept the return type of the new declaration if same modes. */ 1012132730Skan oldrettype = TREE_TYPE (oldtype); 1013132730Skan newrettype = TREE_TYPE (newtype); 101450397Sobrien 1015132730Skan if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype)) 1016132730Skan return 0; 1017132730Skan 1018132730Skan oldargs = TYPE_ARG_TYPES (oldtype); 1019132730Skan newargs = TYPE_ARG_TYPES (newtype); 1020132730Skan tryargs = newargs; 1021132730Skan 1022132730Skan while (oldargs || newargs) 1023132730Skan { 1024169699Skan if (!oldargs 1025169699Skan || !newargs 1026169699Skan || !TREE_VALUE (oldargs) 1027169699Skan || !TREE_VALUE (newargs) 1028132730Skan || TYPE_MODE (TREE_VALUE (oldargs)) 1029132730Skan != TYPE_MODE (TREE_VALUE (newargs))) 1030132730Skan return 0; 1031132730Skan 1032132730Skan oldargs = TREE_CHAIN (oldargs); 1033132730Skan newargs = TREE_CHAIN (newargs); 1034132730Skan } 1035132730Skan 1036132730Skan trytype = build_function_type (newrettype, tryargs); 1037132730Skan return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype)); 1038132730Skan} 1039132730Skan 1040169699Skan/* Subroutine of diagnose_mismatched_decls. Check for function type 1041132730Skan mismatch involving an empty arglist vs a nonempty one and give clearer 1042169699Skan diagnostics. */ 1043132730Skanstatic void 1044132730Skandiagnose_arglist_conflict (tree newdecl, tree olddecl, 1045132730Skan tree newtype, tree oldtype) 104618334Speter{ 1047132730Skan tree t; 104818334Speter 1049132730Skan if (TREE_CODE (olddecl) != FUNCTION_DECL 1050169699Skan || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype)) 1051132730Skan || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0) 1052132730Skan || 1053132730Skan (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0))) 1054132730Skan return; 1055132730Skan 1056132730Skan t = TYPE_ARG_TYPES (oldtype); 1057132730Skan if (t == 0) 1058132730Skan t = TYPE_ARG_TYPES (newtype); 1059132730Skan for (; t; t = TREE_CHAIN (t)) 106090075Sobrien { 1061132730Skan tree type = TREE_VALUE (t); 1062132730Skan 1063132730Skan if (TREE_CHAIN (t) == 0 1064132730Skan && TYPE_MAIN_VARIANT (type) != void_type_node) 106590075Sobrien { 1066169699Skan inform ("a parameter list with an ellipsis can%'t match " 1067132730Skan "an empty parameter name list declaration"); 1068132730Skan break; 106990075Sobrien } 107018334Speter 1071132730Skan if (c_type_promotes_to (type) != type) 1072132730Skan { 1073169699Skan inform ("an argument type that has a default promotion can%'t match " 1074132730Skan "an empty parameter name list declaration"); 1075132730Skan break; 1076132730Skan } 107790075Sobrien } 1078132730Skan} 107990075Sobrien 1080132730Skan/* Another subroutine of diagnose_mismatched_decls. OLDDECL is an 1081132730Skan old-style function definition, NEWDECL is a prototype declaration. 1082132730Skan Diagnose inconsistencies in the argument list. Returns TRUE if 1083132730Skan the prototype is compatible, FALSE if not. */ 1084132730Skanstatic bool 1085132730Skanvalidate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype) 1086132730Skan{ 1087132730Skan tree newargs, oldargs; 1088132730Skan int i; 108918334Speter 1090169699Skan#define END_OF_ARGLIST(t) ((t) == void_type_node) 1091132730Skan 1092132730Skan oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype); 1093132730Skan newargs = TYPE_ARG_TYPES (newtype); 1094132730Skan i = 1; 1095132730Skan 1096132730Skan for (;;) 1097132730Skan { 1098132730Skan tree oldargtype = TREE_VALUE (oldargs); 1099132730Skan tree newargtype = TREE_VALUE (newargs); 1100132730Skan 1101169699Skan if (oldargtype == error_mark_node || newargtype == error_mark_node) 1102169699Skan return false; 1103169699Skan 1104169699Skan oldargtype = TYPE_MAIN_VARIANT (oldargtype); 1105169699Skan newargtype = TYPE_MAIN_VARIANT (newargtype); 1106169699Skan 1107132730Skan if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype)) 1108132730Skan break; 1109132730Skan 1110132730Skan /* Reaching the end of just one list means the two decls don't 1111132730Skan agree on the number of arguments. */ 1112132730Skan if (END_OF_ARGLIST (oldargtype)) 1113132730Skan { 1114169699Skan error ("prototype for %q+D declares more arguments " 1115169699Skan "than previous old-style definition", newdecl); 1116132730Skan return false; 1117132730Skan } 1118132730Skan else if (END_OF_ARGLIST (newargtype)) 1119132730Skan { 1120169699Skan error ("prototype for %q+D declares fewer arguments " 1121169699Skan "than previous old-style definition", newdecl); 1122132730Skan return false; 1123132730Skan } 1124132730Skan 1125132730Skan /* Type for passing arg must be consistent with that declared 1126132730Skan for the arg. */ 1127169699Skan else if (!comptypes (oldargtype, newargtype)) 1128132730Skan { 1129169699Skan error ("prototype for %q+D declares argument %d" 1130169699Skan " with incompatible type", 1131169699Skan newdecl, i); 1132132730Skan return false; 1133132730Skan } 1134132730Skan 1135132730Skan oldargs = TREE_CHAIN (oldargs); 1136132730Skan newargs = TREE_CHAIN (newargs); 1137132730Skan i++; 1138132730Skan } 1139132730Skan 1140132730Skan /* If we get here, no errors were found, but do issue a warning 1141132730Skan for this poor-style construct. */ 1142169699Skan warning (0, "prototype for %q+D follows non-prototype definition", 1143169699Skan newdecl); 1144132730Skan return true; 1145132730Skan#undef END_OF_ARGLIST 1146132730Skan} 1147132730Skan 1148132730Skan/* Subroutine of diagnose_mismatched_decls. Report the location of DECL, 1149132730Skan first in a pair of mismatched declarations, using the diagnostic 1150132730Skan function DIAG. */ 1151132730Skanstatic void 1152169699Skanlocate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2)) 1153132730Skan{ 1154132730Skan if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)) 1155132730Skan ; 1156132730Skan else if (DECL_INITIAL (decl)) 1157169699Skan diag (G_("previous definition of %q+D was here"), decl); 1158132730Skan else if (C_DECL_IMPLICIT (decl)) 1159169699Skan diag (G_("previous implicit declaration of %q+D was here"), decl); 1160132730Skan else 1161169699Skan diag (G_("previous declaration of %q+D was here"), decl); 1162132730Skan} 1163132730Skan 1164132730Skan/* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL. 1165132730Skan Returns true if the caller should proceed to merge the two, false 1166132730Skan if OLDDECL should simply be discarded. As a side effect, issues 1167132730Skan all necessary diagnostics for invalid or poor-style combinations. 1168132730Skan If it returns true, writes the types of NEWDECL and OLDDECL to 1169132730Skan *NEWTYPEP and *OLDTYPEP - these may have been adjusted from 1170132730Skan TREE_TYPE (NEWDECL, OLDDECL) respectively. */ 1171132730Skan 1172132730Skanstatic bool 1173132730Skandiagnose_mismatched_decls (tree newdecl, tree olddecl, 1174132730Skan tree *newtypep, tree *oldtypep) 1175132730Skan{ 1176132730Skan tree newtype, oldtype; 1177132730Skan bool pedwarned = false; 1178132730Skan bool warned = false; 1179169699Skan bool retval = true; 1180132730Skan 1181169699Skan#define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \ 1182169699Skan && DECL_EXTERNAL (DECL)) 1183169699Skan 1184132730Skan /* If we have error_mark_node for either decl or type, just discard 1185132730Skan the previous decl - we're in an error cascade already. */ 1186132730Skan if (olddecl == error_mark_node || newdecl == error_mark_node) 1187132730Skan return false; 1188132730Skan *oldtypep = oldtype = TREE_TYPE (olddecl); 1189132730Skan *newtypep = newtype = TREE_TYPE (newdecl); 1190132730Skan if (oldtype == error_mark_node || newtype == error_mark_node) 1191132730Skan return false; 1192132730Skan 1193132730Skan /* Two different categories of symbol altogether. This is an error 1194132730Skan unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */ 119518334Speter if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) 119618334Speter { 1197169699Skan if (!(TREE_CODE (olddecl) == FUNCTION_DECL 1198169699Skan && DECL_BUILT_IN (olddecl) 1199169699Skan && !C_DECL_DECLARED_BUILTIN (olddecl))) 1200132730Skan { 1201169699Skan error ("%q+D redeclared as different kind of symbol", newdecl); 1202132730Skan locate_old_decl (olddecl, error); 1203132730Skan } 1204132730Skan else if (TREE_PUBLIC (newdecl)) 1205169699Skan warning (0, "built-in function %q+D declared as non-function", 1206169699Skan newdecl); 1207169699Skan else 1208169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows " 1209169699Skan "a built-in function", newdecl); 1210132730Skan return false; 1211132730Skan } 1212132730Skan 1213161660Skan /* Enumerators have no linkage, so may only be declared once in a 1214161660Skan given scope. */ 1215161660Skan if (TREE_CODE (olddecl) == CONST_DECL) 1216161660Skan { 1217169699Skan error ("redeclaration of enumerator %q+D", newdecl); 1218161660Skan locate_old_decl (olddecl, error); 1219161660Skan return false; 1220161660Skan } 1221161660Skan 1222169699Skan if (!comptypes (oldtype, newtype)) 1223132730Skan { 122418334Speter if (TREE_CODE (olddecl) == FUNCTION_DECL 1225169699Skan && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl)) 122618334Speter { 1227132730Skan /* Accept harmless mismatch in function types. 1228132730Skan This is for the ffs and fprintf builtins. */ 1229132730Skan tree trytype = match_builtin_function_types (newtype, oldtype); 1230132730Skan 1231169699Skan if (trytype && comptypes (newtype, trytype)) 1232132730Skan *oldtypep = oldtype = trytype; 1233132730Skan else 123418334Speter { 1235132730Skan /* If types don't match for a built-in, throw away the 1236132730Skan built-in. No point in calling locate_old_decl here, it 1237169699Skan won't print anything. */ 1238169699Skan warning (0, "conflicting types for built-in function %q+D", 1239169699Skan newdecl); 1240132730Skan return false; 124118334Speter } 124218334Speter } 1243132730Skan else if (TREE_CODE (olddecl) == FUNCTION_DECL 1244169699Skan && DECL_IS_BUILTIN (olddecl)) 1245132730Skan { 1246132730Skan /* A conflicting function declaration for a predeclared 1247132730Skan function that isn't actually built in. Objective C uses 1248132730Skan these. The new declaration silently overrides everything 1249132730Skan but the volatility (i.e. noreturn) indication. See also 1250132730Skan below. FIXME: Make Objective C use normal builtins. */ 1251132730Skan TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1252132730Skan return false; 1253132730Skan } 1254132730Skan /* Permit void foo (...) to match int foo (...) if the latter is 1255132730Skan the definition and implicit int was used. See 1256132730Skan c-torture/compile/920625-2.c. */ 1257132730Skan else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) 1258132730Skan && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node 1259132730Skan && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node 1260169699Skan && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl)) 1261132730Skan { 1262169699Skan pedwarn ("conflicting types for %q+D", newdecl); 1263132730Skan /* Make sure we keep void as the return type. */ 1264132730Skan TREE_TYPE (newdecl) = *newtypep = newtype = oldtype; 1265132730Skan C_FUNCTION_IMPLICIT_INT (newdecl) = 0; 1266132730Skan pedwarned = true; 1267132730Skan } 1268169699Skan /* Permit void foo (...) to match an earlier call to foo (...) with 1269169699Skan no declared type (thus, implicitly int). */ 1270169699Skan else if (TREE_CODE (newdecl) == FUNCTION_DECL 1271169699Skan && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node 1272169699Skan && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node 1273169699Skan && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl)) 1274169699Skan { 1275169699Skan pedwarn ("conflicting types for %q+D", newdecl); 1276169699Skan /* Make sure we keep void as the return type. */ 1277169699Skan TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype; 1278169699Skan pedwarned = true; 1279169699Skan } 128018334Speter else 128118334Speter { 1282169699Skan if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype)) 1283169699Skan error ("conflicting type qualifiers for %q+D", newdecl); 1284169699Skan else 1285169699Skan error ("conflicting types for %q+D", newdecl); 1286132730Skan diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype); 1287132730Skan locate_old_decl (olddecl, error); 1288132730Skan return false; 128918334Speter } 1290132730Skan } 129118334Speter 1292132730Skan /* Redeclaration of a type is a constraint violation (6.7.2.3p1), 1293132730Skan but silently ignore the redeclaration if either is in a system 1294132730Skan header. (Conflicting redeclarations were handled above.) */ 1295132730Skan if (TREE_CODE (newdecl) == TYPE_DECL) 1296132730Skan { 1297132730Skan if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl)) 1298169699Skan return true; /* Allow OLDDECL to continue in use. */ 1299169699Skan 1300169699Skan error ("redefinition of typedef %q+D", newdecl); 1301132730Skan locate_old_decl (olddecl, error); 1302132730Skan return false; 130318334Speter } 130418334Speter 1305132730Skan /* Function declarations can either be 'static' or 'extern' (no 1306132730Skan qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore 1307189824Sdas can never conflict with each other on account of linkage 1308189824Sdas (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but 1309189824Sdas gnu89 mode permits two definitions if one is 'extern inline' and 1310189824Sdas one is not. The non- extern-inline definition supersedes the 1311189824Sdas extern-inline definition. */ 1312169699Skan 1313132730Skan else if (TREE_CODE (newdecl) == FUNCTION_DECL) 131418334Speter { 1315132730Skan /* If you declare a built-in function name as static, or 1316132730Skan define the built-in with an old-style definition (so we 1317132730Skan can't validate the argument list) the built-in definition is 1318132730Skan overridden, but optionally warn this was a bad choice of name. */ 1319132730Skan if (DECL_BUILT_IN (olddecl) 1320169699Skan && !C_DECL_DECLARED_BUILTIN (olddecl) 1321132730Skan && (!TREE_PUBLIC (newdecl) 1322132730Skan || (DECL_INITIAL (newdecl) 1323132730Skan && !TYPE_ARG_TYPES (TREE_TYPE (newdecl))))) 132418334Speter { 1325169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows " 1326169699Skan "a built-in function", newdecl); 132718334Speter /* Discard the old built-in function. */ 1328132730Skan return false; 132918334Speter } 1330169699Skan 1331132730Skan if (DECL_INITIAL (newdecl)) 133218334Speter { 1333169699Skan if (DECL_INITIAL (olddecl)) 133418334Speter { 1335169699Skan /* If both decls are in the same TU and the new declaration 1336169699Skan isn't overriding an extern inline reject the new decl. 1337189824Sdas In c99, no overriding is allowed in the same translation 1338189824Sdas unit. */ 1339189824Sdas if ((!DECL_EXTERN_INLINE (olddecl) 1340189824Sdas || DECL_EXTERN_INLINE (newdecl) 1341189824Sdas || (!flag_gnu89_inline 1342189824Sdas && (!DECL_DECLARED_INLINE_P (olddecl) 1343189824Sdas || !lookup_attribute ("gnu_inline", 1344189824Sdas DECL_ATTRIBUTES (olddecl))) 1345189824Sdas && (!DECL_DECLARED_INLINE_P (newdecl) 1346189824Sdas || !lookup_attribute ("gnu_inline", 1347189824Sdas DECL_ATTRIBUTES (newdecl)))) 1348189824Sdas ) 1349169699Skan && same_translation_unit_p (newdecl, olddecl)) 1350169699Skan { 1351169699Skan error ("redefinition of %q+D", newdecl); 1352169699Skan locate_old_decl (olddecl, error); 1353169699Skan return false; 1354169699Skan } 135518334Speter } 1356132730Skan } 1357132730Skan /* If we have a prototype after an old-style function definition, 1358132730Skan the argument types must be checked specially. */ 1359132730Skan else if (DECL_INITIAL (olddecl) 1360132730Skan && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype) 1361132730Skan && TYPE_ACTUAL_ARG_TYPES (oldtype) 1362132730Skan && !validate_proto_after_old_defn (newdecl, newtype, oldtype)) 1363132730Skan { 1364132730Skan locate_old_decl (olddecl, error); 1365132730Skan return false; 1366132730Skan } 1367169699Skan /* A non-static declaration (even an "extern") followed by a 1368169699Skan static declaration is undefined behavior per C99 6.2.2p3-5,7. 1369169699Skan The same is true for a static forward declaration at block 1370169699Skan scope followed by a non-static declaration/definition at file 1371169699Skan scope. Static followed by non-static at the same scope is 1372169699Skan not undefined behavior, and is the most convenient way to get 1373169699Skan some effects (see e.g. what unwind-dw2-fde-glibc.c does to 1374169699Skan the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but 1375169699Skan we do diagnose it if -Wtraditional. */ 1376132730Skan if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl)) 1377132730Skan { 1378169699Skan /* Two exceptions to the rule. If olddecl is an extern 1379169699Skan inline, or a predeclared function that isn't actually 1380169699Skan built in, newdecl silently overrides olddecl. The latter 1381169699Skan occur only in Objective C; see also above. (FIXME: Make 1382169699Skan Objective C use normal builtins.) */ 1383169699Skan if (!DECL_IS_BUILTIN (olddecl) 1384169699Skan && !DECL_EXTERN_INLINE (olddecl)) 138518334Speter { 1386169699Skan error ("static declaration of %q+D follows " 1387169699Skan "non-static declaration", newdecl); 1388169699Skan locate_old_decl (olddecl, error); 138918334Speter } 1390169699Skan return false; 139118334Speter } 1392169699Skan else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)) 139396263Sobrien { 1394169699Skan if (DECL_CONTEXT (olddecl)) 1395169699Skan { 1396169699Skan error ("non-static declaration of %q+D follows " 1397169699Skan "static declaration", newdecl); 1398169699Skan locate_old_decl (olddecl, error); 1399169699Skan return false; 1400169699Skan } 1401169699Skan else if (warn_traditional) 1402169699Skan { 1403169699Skan warning (OPT_Wtraditional, "non-static declaration of %q+D " 1404169699Skan "follows static declaration", newdecl); 1405169699Skan warned = true; 1406169699Skan } 140796263Sobrien } 1408189824Sdas 1409189824Sdas /* Make sure gnu_inline attribute is either not present, or 1410189824Sdas present on all inline decls. */ 1411189824Sdas if (DECL_DECLARED_INLINE_P (olddecl) 1412189824Sdas && DECL_DECLARED_INLINE_P (newdecl)) 1413189824Sdas { 1414189824Sdas bool newa = lookup_attribute ("gnu_inline", 1415189824Sdas DECL_ATTRIBUTES (newdecl)) != NULL; 1416189824Sdas bool olda = lookup_attribute ("gnu_inline", 1417189824Sdas DECL_ATTRIBUTES (olddecl)) != NULL; 1418189824Sdas if (newa != olda) 1419189824Sdas { 1420189824Sdas error ("%<gnu_inline%> attribute present on %q+D", 1421189824Sdas newa ? newdecl : olddecl); 1422189824Sdas error ("%Jbut not here", newa ? olddecl : newdecl); 1423189824Sdas } 1424189824Sdas } 142518334Speter } 1426132730Skan else if (TREE_CODE (newdecl) == VAR_DECL) 142718334Speter { 1428132730Skan /* Only variables can be thread-local, and all declarations must 1429132730Skan agree on this property. */ 1430169699Skan if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl)) 143118334Speter { 1432169699Skan /* Nothing to check. Since OLDDECL is marked threadprivate 1433169699Skan and NEWDECL does not have a thread-local attribute, we 1434169699Skan will merge the threadprivate attribute into NEWDECL. */ 1435169699Skan ; 1436169699Skan } 1437169699Skan else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)) 1438169699Skan { 1439169699Skan if (DECL_THREAD_LOCAL_P (newdecl)) 1440169699Skan error ("thread-local declaration of %q+D follows " 1441169699Skan "non-thread-local declaration", newdecl); 1442132730Skan else 1443169699Skan error ("non-thread-local declaration of %q+D follows " 1444169699Skan "thread-local declaration", newdecl); 1445132730Skan 1446132730Skan locate_old_decl (olddecl, error); 1447132730Skan return false; 144818334Speter } 1449132730Skan 1450132730Skan /* Multiple initialized definitions are not allowed (6.9p3,5). */ 1451132730Skan if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl)) 145218334Speter { 1453169699Skan error ("redefinition of %q+D", newdecl); 1454132730Skan locate_old_decl (olddecl, error); 1455132730Skan return false; 145618334Speter } 145718334Speter 1458169699Skan /* Objects declared at file scope: if the first declaration had 1459169699Skan external linkage (even if it was an external reference) the 1460169699Skan second must have external linkage as well, or the behavior is 1461169699Skan undefined. If the first declaration had internal linkage, then 1462169699Skan the second must too, or else be an external reference (in which 1463169699Skan case the composite declaration still has internal linkage). 1464169699Skan As for function declarations, we warn about the static-then- 1465169699Skan extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */ 1466169699Skan if (DECL_FILE_SCOPE_P (newdecl) 1467169699Skan && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl)) 146818334Speter { 1469169699Skan if (DECL_EXTERNAL (newdecl)) 147018334Speter { 1471169699Skan if (!DECL_FILE_SCOPE_P (olddecl)) 1472169699Skan { 1473169699Skan error ("extern declaration of %q+D follows " 1474169699Skan "declaration with no linkage", newdecl); 1475169699Skan locate_old_decl (olddecl, error); 1476169699Skan return false; 1477169699Skan } 1478169699Skan else if (warn_traditional) 1479169699Skan { 1480169699Skan warning (OPT_Wtraditional, "non-static declaration of %q+D " 1481169699Skan "follows static declaration", newdecl); 1482169699Skan warned = true; 1483169699Skan } 1484169699Skan } 1485169699Skan else 1486169699Skan { 1487132730Skan if (TREE_PUBLIC (newdecl)) 1488169699Skan error ("non-static declaration of %q+D follows " 1489169699Skan "static declaration", newdecl); 1490132730Skan else 1491169699Skan error ("static declaration of %q+D follows " 1492169699Skan "non-static declaration", newdecl); 149318334Speter 1494132730Skan locate_old_decl (olddecl, error); 1495132730Skan return false; 149618334Speter } 149718334Speter } 1498132730Skan /* Two objects with the same name declared at the same block 1499132730Skan scope must both be external references (6.7p3). */ 1500169699Skan else if (!DECL_FILE_SCOPE_P (newdecl)) 1501132730Skan { 1502132730Skan if (DECL_EXTERNAL (newdecl)) 1503169699Skan { 1504169699Skan /* Extern with initializer at block scope, which will 1505169699Skan already have received an error. */ 1506169699Skan } 1507132730Skan else if (DECL_EXTERNAL (olddecl)) 1508169699Skan { 1509169699Skan error ("declaration of %q+D with no linkage follows " 1510169699Skan "extern declaration", newdecl); 1511169699Skan locate_old_decl (olddecl, error); 1512169699Skan } 1513132730Skan else 1514169699Skan { 1515169699Skan error ("redeclaration of %q+D with no linkage", newdecl); 1516169699Skan locate_old_decl (olddecl, error); 1517169699Skan } 1518117421Skan 1519132730Skan return false; 1520132730Skan } 152118334Speter } 1522132730Skan 1523132730Skan /* warnings */ 1524169699Skan /* All decls must agree on a visibility. */ 1525169699Skan if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS) 1526169699Skan && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl) 1527132730Skan && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) 1528117421Skan { 1529169699Skan warning (0, "redeclaration of %q+D with different visibility " 1530169699Skan "(old visibility preserved)", newdecl); 1531132730Skan warned = true; 1532117421Skan } 1533132730Skan 1534132730Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 1535117421Skan { 1536132730Skan /* Diagnose inline __attribute__ ((noinline)) which is silly. */ 1537132730Skan if (DECL_DECLARED_INLINE_P (newdecl) 1538132730Skan && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 153918334Speter { 1540169699Skan warning (OPT_Wattributes, "inline declaration of %qD follows " 1541169699Skan "declaration with attribute noinline", newdecl); 1542132730Skan warned = true; 154318334Speter } 1544132730Skan else if (DECL_DECLARED_INLINE_P (olddecl) 1545132730Skan && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) 154618334Speter { 1547169699Skan warning (OPT_Wattributes, "declaration of %q+D with attribute " 1548169699Skan "noinline follows inline declaration ", newdecl); 1549132730Skan warned = true; 155018334Speter } 1551132730Skan 1552132730Skan /* Inline declaration after use or definition. 1553132730Skan ??? Should we still warn about this now we have unit-at-a-time 1554169699Skan mode and can get it right? 1555169699Skan Definitely don't complain if the decls are in different translation 1556189824Sdas units. 1557189824Sdas C99 permits this, so don't warn in that case. (The function 1558189824Sdas may not be inlined everywhere in function-at-a-time mode, but 1559189824Sdas we still shouldn't warn.) */ 1560169699Skan if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl) 1561189824Sdas && same_translation_unit_p (olddecl, newdecl) 1562189824Sdas && flag_gnu89_inline) 156318334Speter { 1564132730Skan if (TREE_USED (olddecl)) 156518334Speter { 1566169699Skan warning (0, "%q+D declared inline after being called", olddecl); 1567132730Skan warned = true; 156818334Speter } 1569132730Skan else if (DECL_INITIAL (olddecl)) 1570132730Skan { 1571169699Skan warning (0, "%q+D declared inline after its definition", olddecl); 1572132730Skan warned = true; 1573132730Skan } 157418334Speter } 1575132730Skan } 1576132730Skan else /* PARM_DECL, VAR_DECL */ 1577132730Skan { 1578169699Skan /* Redeclaration of a parameter is a constraint violation (this is 1579169699Skan not explicitly stated, but follows from C99 6.7p3 [no more than 1580169699Skan one declaration of the same identifier with no linkage in the 1581169699Skan same scope, except type tags] and 6.2.2p6 [parameters have no 1582169699Skan linkage]). We must check for a forward parameter declaration, 1583169699Skan indicated by TREE_ASM_WRITTEN on the old declaration - this is 1584169699Skan an extension, the mandatory diagnostic for which is handled by 1585169699Skan mark_forward_parm_decls. */ 1586169699Skan 1587132730Skan if (TREE_CODE (newdecl) == PARM_DECL 1588132730Skan && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl))) 158918334Speter { 1590169699Skan error ("redefinition of parameter %q+D", newdecl); 1591132730Skan locate_old_decl (olddecl, error); 1592132730Skan return false; 1593132730Skan } 159418334Speter } 159518334Speter 1596132730Skan /* Optional warning for completely redundant decls. */ 1597132730Skan if (!warned && !pedwarned 1598132730Skan && warn_redundant_decls 1599132730Skan /* Don't warn about a function declaration followed by a 1600132730Skan definition. */ 1601132730Skan && !(TREE_CODE (newdecl) == FUNCTION_DECL 1602132730Skan && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)) 1603169699Skan /* Don't warn about redundant redeclarations of builtins. */ 1604132730Skan && !(TREE_CODE (newdecl) == FUNCTION_DECL 1605132730Skan && !DECL_BUILT_IN (newdecl) 1606132730Skan && DECL_BUILT_IN (olddecl) 1607169699Skan && !C_DECL_DECLARED_BUILTIN (olddecl)) 1608132730Skan /* Don't warn about an extern followed by a definition. */ 1609132730Skan && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl)) 1610132730Skan /* Don't warn about forward parameter decls. */ 1611132730Skan && !(TREE_CODE (newdecl) == PARM_DECL 1612149846Sobrien && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)) 1613149846Sobrien /* Don't warn about a variable definition following a declaration. */ 1614149846Sobrien && !(TREE_CODE (newdecl) == VAR_DECL 1615149846Sobrien && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))) 161618334Speter { 1617169699Skan warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D", 1618169699Skan newdecl); 1619132730Skan warned = true; 162018334Speter } 162118334Speter 1622132730Skan /* Report location of previous decl/defn in a consistent manner. */ 1623132730Skan if (warned || pedwarned) 1624169699Skan locate_old_decl (olddecl, pedwarned ? pedwarn : warning0); 162518334Speter 1626169699Skan#undef DECL_EXTERN_INLINE 1627169699Skan 1628169699Skan return retval; 1629132730Skan} 163018334Speter 1631132730Skan/* Subroutine of duplicate_decls. NEWDECL has been found to be 1632132730Skan consistent with OLDDECL, but carries new information. Merge the 1633132730Skan new information into OLDDECL. This function issues no 1634132730Skan diagnostics. */ 1635132730Skan 1636132730Skanstatic void 1637132730Skanmerge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype) 1638132730Skan{ 1639189824Sdas bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL 1640189824Sdas && DECL_INITIAL (newdecl) != 0); 1641189824Sdas bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL 1642189824Sdas && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0); 1643189824Sdas bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL 1644189824Sdas && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0); 1645189824Sdas bool extern_changed = false; 1646132730Skan 1647169699Skan /* For real parm decl following a forward decl, rechain the old decl 1648169699Skan in its new location and clear TREE_ASM_WRITTEN (it's not a 1649169699Skan forward decl anymore). */ 1650132730Skan if (TREE_CODE (newdecl) == PARM_DECL 1651169699Skan && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)) 165218334Speter { 1653169699Skan struct c_binding *b, **here; 1654169699Skan 1655169699Skan for (here = ¤t_scope->bindings; *here; here = &(*here)->prev) 1656169699Skan if ((*here)->decl == olddecl) 1657169699Skan goto found; 1658169699Skan gcc_unreachable (); 1659169699Skan 1660169699Skan found: 1661169699Skan b = *here; 1662169699Skan *here = b->prev; 1663169699Skan b->prev = current_scope->bindings; 1664169699Skan current_scope->bindings = b; 1665169699Skan 1666132730Skan TREE_ASM_WRITTEN (olddecl) = 0; 1667132730Skan } 166850397Sobrien 1669132730Skan DECL_ATTRIBUTES (newdecl) 1670169699Skan = targetm.merge_decl_attributes (olddecl, newdecl); 167118334Speter 1672132730Skan /* Merge the data types specified in the two decls. */ 1673132730Skan TREE_TYPE (newdecl) 1674132730Skan = TREE_TYPE (olddecl) 1675169699Skan = composite_type (newtype, oldtype); 167618334Speter 1677132730Skan /* Lay the type out, unless already done. */ 1678169699Skan if (!comptypes (oldtype, TREE_TYPE (newdecl))) 1679132730Skan { 1680132730Skan if (TREE_TYPE (newdecl) != error_mark_node) 1681132730Skan layout_type (TREE_TYPE (newdecl)); 1682132730Skan if (TREE_CODE (newdecl) != FUNCTION_DECL 1683132730Skan && TREE_CODE (newdecl) != TYPE_DECL 1684132730Skan && TREE_CODE (newdecl) != CONST_DECL) 1685132730Skan layout_decl (newdecl, 0); 1686132730Skan } 1687132730Skan else 1688132730Skan { 1689132730Skan /* Since the type is OLDDECL's, make OLDDECL's size go with. */ 1690132730Skan DECL_SIZE (newdecl) = DECL_SIZE (olddecl); 1691132730Skan DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl); 1692132730Skan DECL_MODE (newdecl) = DECL_MODE (olddecl); 1693259705Spfg if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl)) 1694259705Spfg { 1695259705Spfg DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl); 1696259705Spfg DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl); 1697259705Spfg } 1698132730Skan } 169918334Speter 170090075Sobrien 1701132730Skan /* Merge the type qualifiers. */ 1702132730Skan if (TREE_READONLY (newdecl)) 1703132730Skan TREE_READONLY (olddecl) = 1; 170418334Speter 1705132730Skan if (TREE_THIS_VOLATILE (newdecl)) 1706169699Skan TREE_THIS_VOLATILE (olddecl) = 1; 170718334Speter 1708169699Skan /* Merge deprecatedness. */ 1709169699Skan if (TREE_DEPRECATED (newdecl)) 1710169699Skan TREE_DEPRECATED (olddecl) = 1; 1711169699Skan 1712169699Skan /* Keep source location of definition rather than declaration and of 1713169699Skan prototype rather than non-prototype unless that prototype is 1714169699Skan built-in. */ 1715169699Skan if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0) 1716169699Skan || (old_is_prototype && !new_is_prototype 1717169699Skan && !C_DECL_BUILTIN_PROTOTYPE (olddecl))) 1718132730Skan DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); 171918334Speter 1720132730Skan /* Merge the initialization information. */ 1721132730Skan if (DECL_INITIAL (newdecl) == 0) 1722132730Skan DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 172318334Speter 1724169699Skan /* Merge the threadprivate attribute. */ 1725169699Skan if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl)) 1726169699Skan { 1727169699Skan DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl); 1728169699Skan C_DECL_THREADPRIVATE_P (newdecl) = 1; 1729169699Skan } 173090075Sobrien 1731169699Skan if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)) 1732169699Skan { 1733169699Skan /* Merge the unused-warning information. */ 1734169699Skan if (DECL_IN_SYSTEM_HEADER (olddecl)) 1735169699Skan DECL_IN_SYSTEM_HEADER (newdecl) = 1; 1736169699Skan else if (DECL_IN_SYSTEM_HEADER (newdecl)) 1737169699Skan DECL_IN_SYSTEM_HEADER (olddecl) = 1; 1738132730Skan 1739169699Skan /* Merge the section attribute. 1740169699Skan We want to issue an error if the sections conflict but that 1741169699Skan must be done later in decl_attributes since we are called 1742169699Skan before attributes are assigned. */ 1743169699Skan if (DECL_SECTION_NAME (newdecl) == NULL_TREE) 1744169699Skan DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); 1745132730Skan 1746169699Skan /* Copy the assembler name. 1747169699Skan Currently, it can only be defined in the prototype. */ 1748169699Skan COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); 174918334Speter 1750169699Skan /* Use visibility of whichever declaration had it specified */ 1751169699Skan if (DECL_VISIBILITY_SPECIFIED (olddecl)) 1752169699Skan { 1753169699Skan DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); 1754169699Skan DECL_VISIBILITY_SPECIFIED (newdecl) = 1; 1755169699Skan } 175696263Sobrien 1757169699Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 1758169699Skan { 1759169699Skan DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl); 1760169699Skan DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); 1761169699Skan DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); 1762169699Skan DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) 1763169699Skan |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); 1764169699Skan TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1765169699Skan TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); 1766169699Skan DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); 1767169699Skan DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); 1768169699Skan DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl); 1769169699Skan } 1770169699Skan 1771169699Skan /* Merge the storage class information. */ 1772169699Skan merge_weak (newdecl, olddecl); 1773169699Skan 1774169699Skan /* For functions, static overrides non-static. */ 1775169699Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 1776169699Skan { 1777169699Skan TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl); 1778169699Skan /* This is since we don't automatically 1779169699Skan copy the attributes of NEWDECL into OLDDECL. */ 1780169699Skan TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 1781169699Skan /* If this clears `static', clear it in the identifier too. */ 1782169699Skan if (!TREE_PUBLIC (olddecl)) 1783169699Skan TREE_PUBLIC (DECL_NAME (olddecl)) = 0; 1784169699Skan } 178518334Speter } 1786169699Skan 1787189824Sdas /* In c99, 'extern' declaration before (or after) 'inline' means this 1788189824Sdas function is not DECL_EXTERNAL, unless 'gnu_inline' attribute 1789189824Sdas is present. */ 1790189824Sdas if (TREE_CODE (newdecl) == FUNCTION_DECL 1791189824Sdas && !flag_gnu89_inline 1792189824Sdas && (DECL_DECLARED_INLINE_P (newdecl) 1793189824Sdas || DECL_DECLARED_INLINE_P (olddecl)) 1794189824Sdas && (!DECL_DECLARED_INLINE_P (newdecl) 1795189824Sdas || !DECL_DECLARED_INLINE_P (olddecl) 1796189824Sdas || !DECL_EXTERNAL (olddecl)) 1797189824Sdas && DECL_EXTERNAL (newdecl) 1798189824Sdas && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))) 1799189824Sdas DECL_EXTERNAL (newdecl) = 0; 1800189824Sdas 180118334Speter if (DECL_EXTERNAL (newdecl)) 180218334Speter { 1803132730Skan TREE_STATIC (newdecl) = TREE_STATIC (olddecl); 1804132730Skan DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl); 1805132730Skan 180618334Speter /* An extern decl does not override previous storage class. */ 180718334Speter TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); 1808169699Skan if (!DECL_EXTERNAL (newdecl)) 1809132730Skan { 1810132730Skan DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); 1811132730Skan DECL_COMMON (newdecl) = DECL_COMMON (olddecl); 1812132730Skan } 181318334Speter } 181418334Speter else 181518334Speter { 181618334Speter TREE_STATIC (olddecl) = TREE_STATIC (newdecl); 181718334Speter TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 181818334Speter } 181918334Speter 182050397Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL) 182118334Speter { 182290075Sobrien /* If we're redefining a function previously defined as extern 182390075Sobrien inline, make sure we emit debug info for the inline before we 1824169699Skan throw it away, in case it was inlined into a function that 1825169699Skan hasn't been written out yet. */ 1826110623Skan if (new_is_definition && DECL_INITIAL (olddecl)) 182790075Sobrien { 1828132730Skan if (TREE_USED (olddecl) 1829132730Skan /* In unit-at-a-time mode we never inline re-defined extern 1830169699Skan inline functions. */ 1831132730Skan && !flag_unit_at_a_time 1832132730Skan && cgraph_function_possibly_inlined_p (olddecl)) 1833110623Skan (*debug_hooks->outlining_inline_function) (olddecl); 183490075Sobrien 183590075Sobrien /* The new defn must not be inline. */ 183690075Sobrien DECL_INLINE (newdecl) = 0; 183790075Sobrien DECL_UNINLINABLE (newdecl) = 1; 183890075Sobrien } 183990075Sobrien else 184090075Sobrien { 1841169699Skan /* If either decl says `inline', this fn is inline, unless 1842169699Skan its definition was passed already. */ 184390075Sobrien if (DECL_DECLARED_INLINE_P (newdecl) 184490075Sobrien || DECL_DECLARED_INLINE_P (olddecl)) 184590075Sobrien DECL_DECLARED_INLINE_P (newdecl) = 1; 184690075Sobrien 184790075Sobrien DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) 184890075Sobrien = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); 184990075Sobrien } 185090075Sobrien 185118334Speter if (DECL_BUILT_IN (olddecl)) 185218334Speter { 1853169699Skan /* If redeclaring a builtin function, it stays built in. 1854169699Skan But it gets tagged as having been declared. */ 1855132730Skan DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); 1856132730Skan DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); 1857169699Skan C_DECL_DECLARED_BUILTIN (newdecl) = 1; 1858169699Skan if (new_is_prototype) 1859169699Skan C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0; 1860169699Skan else 1861169699Skan C_DECL_BUILTIN_PROTOTYPE (newdecl) 1862169699Skan = C_DECL_BUILTIN_PROTOTYPE (olddecl); 186318334Speter } 186490075Sobrien 186550397Sobrien /* Also preserve various other info from the definition. */ 1866169699Skan if (!new_is_definition) 186750397Sobrien { 186850397Sobrien DECL_RESULT (newdecl) = DECL_RESULT (olddecl); 1869132730Skan DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 1870169699Skan DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl); 187190075Sobrien DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); 187250397Sobrien DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); 187390075Sobrien 187490075Sobrien /* Set DECL_INLINE on the declaration if we've got a body 187590075Sobrien from which to instantiate. */ 1876169699Skan if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl)) 187790075Sobrien { 187890075Sobrien DECL_INLINE (newdecl) = 1; 187990075Sobrien DECL_ABSTRACT_ORIGIN (newdecl) 1880132730Skan = DECL_ABSTRACT_ORIGIN (olddecl); 188190075Sobrien } 188250397Sobrien } 188390075Sobrien else 188490075Sobrien { 188590075Sobrien /* If a previous declaration said inline, mark the 188690075Sobrien definition as inlinable. */ 188790075Sobrien if (DECL_DECLARED_INLINE_P (newdecl) 1888169699Skan && !DECL_UNINLINABLE (newdecl)) 188990075Sobrien DECL_INLINE (newdecl) = 1; 189090075Sobrien } 189150397Sobrien } 189218334Speter 1893189824Sdas extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl); 1894189824Sdas 189518334Speter /* Copy most of the decl-specific fields of NEWDECL into OLDDECL. 1896169699Skan But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */ 189718334Speter { 189890075Sobrien unsigned olddecl_uid = DECL_UID (olddecl); 1899169699Skan tree olddecl_context = DECL_CONTEXT (olddecl); 190018334Speter 190190075Sobrien memcpy ((char *) olddecl + sizeof (struct tree_common), 190290075Sobrien (char *) newdecl + sizeof (struct tree_common), 1903169699Skan sizeof (struct tree_decl_common) - sizeof (struct tree_common)); 1904169699Skan switch (TREE_CODE (olddecl)) 1905169699Skan { 1906169699Skan case FIELD_DECL: 1907169699Skan case VAR_DECL: 1908169699Skan case PARM_DECL: 1909169699Skan case LABEL_DECL: 1910169699Skan case RESULT_DECL: 1911169699Skan case CONST_DECL: 1912169699Skan case TYPE_DECL: 1913169699Skan case FUNCTION_DECL: 1914169699Skan memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 1915169699Skan (char *) newdecl + sizeof (struct tree_decl_common), 1916169699Skan tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common)); 1917169699Skan break; 1918169699Skan 1919169699Skan default: 1920169699Skan 1921169699Skan memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 1922169699Skan (char *) newdecl + sizeof (struct tree_decl_common), 1923169699Skan sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)); 1924169699Skan } 192518334Speter DECL_UID (olddecl) = olddecl_uid; 1926169699Skan DECL_CONTEXT (olddecl) = olddecl_context; 192718334Speter } 192818334Speter 1929132730Skan /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl 1930119268Skan so that encode_section_info has a chance to look at the new decl 1931119268Skan flags and attributes. */ 1932119268Skan if (DECL_RTL_SET_P (olddecl) 1933119268Skan && (TREE_CODE (olddecl) == FUNCTION_DECL 1934119268Skan || (TREE_CODE (olddecl) == VAR_DECL 1935119268Skan && TREE_STATIC (olddecl)))) 1936169699Skan make_decl_rtl (olddecl); 1937189824Sdas 1938189824Sdas /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL, 1939189824Sdas and the definition is coming from the old version, cgraph needs 1940189824Sdas to be called again. */ 1941189824Sdas if (extern_changed && !new_is_definition 1942189824Sdas && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl)) 1943189824Sdas cgraph_finalize_function (olddecl, false); 1944132730Skan} 1945119268Skan 1946132730Skan/* Handle when a new declaration NEWDECL has the same name as an old 1947132730Skan one OLDDECL in the same binding contour. Prints an error message 1948132730Skan if appropriate. 1949132730Skan 1950132730Skan If safely possible, alter OLDDECL to look like NEWDECL, and return 1951132730Skan true. Otherwise, return false. */ 1952132730Skan 1953132730Skanstatic bool 1954132730Skanduplicate_decls (tree newdecl, tree olddecl) 1955132730Skan{ 1956169699Skan tree newtype = NULL, oldtype = NULL; 1957132730Skan 1958132730Skan if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype)) 1959169699Skan { 1960169699Skan /* Avoid `unused variable' and other warnings warnings for OLDDECL. */ 1961169699Skan TREE_NO_WARNING (olddecl) = 1; 1962169699Skan return false; 1963169699Skan } 1964132730Skan 1965132730Skan merge_decls (newdecl, olddecl, newtype, oldtype); 1966132730Skan return true; 196718334Speter} 1968169699Skan 1969132730Skan 1970169699Skan/* Check whether decl-node NEW_DECL shadows an existing declaration. */ 1971132730Skanstatic void 1972169699Skanwarn_if_shadowing (tree new_decl) 1973132730Skan{ 1974169699Skan struct c_binding *b; 1975132730Skan 1976169699Skan /* Shadow warnings wanted? */ 1977169699Skan if (!warn_shadow 1978132730Skan /* No shadow warnings for internally generated vars. */ 1979169699Skan || DECL_IS_BUILTIN (new_decl) 1980132730Skan /* No shadow warnings for vars made for inlining. */ 1981169699Skan || DECL_FROM_INLINE (new_decl)) 198290075Sobrien return; 198390075Sobrien 1984169699Skan /* Is anything being shadowed? Invisible decls do not count. */ 1985169699Skan for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed) 1986169699Skan if (b->decl && b->decl != new_decl && !b->invisible) 1987169699Skan { 1988169699Skan tree old_decl = b->decl; 198990075Sobrien 1990169699Skan if (old_decl == error_mark_node) 1991169699Skan { 1992169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows previous " 1993169699Skan "non-variable", new_decl); 1994169699Skan break; 1995169699Skan } 1996169699Skan else if (TREE_CODE (old_decl) == PARM_DECL) 1997169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows a parameter", 1998169699Skan new_decl); 1999169699Skan else if (DECL_FILE_SCOPE_P (old_decl)) 2000169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows a global " 2001169699Skan "declaration", new_decl); 2002169699Skan else if (TREE_CODE (old_decl) == FUNCTION_DECL 2003169699Skan && DECL_BUILT_IN (old_decl)) 2004169699Skan { 2005169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows " 2006169699Skan "a built-in function", new_decl); 2007169699Skan break; 2008169699Skan } 2009169699Skan else 2010169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows a previous local", 2011169699Skan new_decl); 2012169699Skan 2013169699Skan warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl); 2014169699Skan 2015169699Skan break; 2016169699Skan } 2017132730Skan} 2018132730Skan 2019132730Skan 2020132730Skan/* Subroutine of pushdecl. 2021132730Skan 2022132730Skan X is a TYPE_DECL for a typedef statement. Create a brand new 2023132730Skan ..._TYPE node (which will be just a variant of the existing 2024132730Skan ..._TYPE node with identical properties) and then install X 2025132730Skan as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. 2026132730Skan 2027132730Skan The whole point here is to end up with a situation where each 2028132730Skan and every ..._TYPE node the compiler creates will be uniquely 2029132730Skan associated with AT MOST one node representing a typedef name. 2030132730Skan This way, even though the compiler substitutes corresponding 2031132730Skan ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very 2032132730Skan early on, later parts of the compiler can always do the reverse 2033132730Skan translation and get back the corresponding typedef name. For 2034132730Skan example, given: 2035132730Skan 2036169699Skan typedef struct S MY_TYPE; 2037132730Skan MY_TYPE object; 2038132730Skan 2039132730Skan Later parts of the compiler might only know that `object' was of 2040132730Skan type `struct S' if it were not for code just below. With this 2041132730Skan code however, later parts of the compiler see something like: 2042132730Skan 2043132730Skan struct S' == struct S 2044132730Skan typedef struct S' MY_TYPE; 2045132730Skan struct S' object; 2046132730Skan 2047132730Skan And they can then deduce (from the node for type struct S') that 2048132730Skan the original object declaration was: 2049132730Skan 2050132730Skan MY_TYPE object; 2051132730Skan 2052132730Skan Being able to do this is important for proper support of protoize, 2053132730Skan and also for generating precise symbolic debugging information 2054132730Skan which takes full account of the programmer's (typedef) vocabulary. 2055132730Skan 2056132730Skan Obviously, we don't want to generate a duplicate ..._TYPE node if 2057132730Skan the TYPE_DECL node that we are now processing really represents a 2058132730Skan standard built-in type. 2059132730Skan 2060132730Skan Since all standard types are effectively declared at line zero 2061132730Skan in the source file, we can easily check to see if we are working 2062132730Skan on a standard type by checking the current value of lineno. */ 2063132730Skan 2064132730Skanstatic void 2065132730Skanclone_underlying_type (tree x) 2066132730Skan{ 2067169699Skan if (DECL_IS_BUILTIN (x)) 206890075Sobrien { 2069132730Skan if (TYPE_NAME (TREE_TYPE (x)) == 0) 2070132730Skan TYPE_NAME (TREE_TYPE (x)) = x; 207190075Sobrien } 2072132730Skan else if (TREE_TYPE (x) != error_mark_node 2073132730Skan && DECL_ORIGINAL_TYPE (x) == NULL_TREE) 207490075Sobrien { 2075132730Skan tree tt = TREE_TYPE (x); 2076132730Skan DECL_ORIGINAL_TYPE (x) = tt; 2077169699Skan tt = build_variant_type_copy (tt); 2078132730Skan TYPE_NAME (tt) = x; 2079132730Skan TREE_USED (tt) = TREE_USED (x); 2080132730Skan TREE_TYPE (x) = tt; 208190075Sobrien } 208290075Sobrien} 208390075Sobrien 208418334Speter/* Record a decl-node X as belonging to the current lexical scope. 208518334Speter Check for errors (such as an incompatible declaration for the same 208618334Speter name already seen in the same scope). 208718334Speter 208818334Speter Returns either X or an old decl for the same name. 208918334Speter If an old decl is returned, it may have been smashed 209018334Speter to agree with what X says. */ 209118334Speter 209218334Spetertree 2093132730Skanpushdecl (tree x) 209418334Speter{ 209590075Sobrien tree name = DECL_NAME (x); 2096132730Skan struct c_scope *scope = current_scope; 2097169699Skan struct c_binding *b; 2098169699Skan bool nested = false; 209918334Speter 210090075Sobrien /* Functions need the lang_decl data. */ 2101169699Skan if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x)) 2102169699Skan DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl); 210390075Sobrien 2104169699Skan /* Must set DECL_CONTEXT for everything not at file scope or 2105169699Skan DECL_FILE_SCOPE_P won't work. Local externs don't count 2106169699Skan unless they have initializers (which generate code). */ 2107169699Skan if (current_function_decl 2108169699Skan && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL) 2109169699Skan || DECL_INITIAL (x) || !DECL_EXTERNAL (x))) 2110132730Skan DECL_CONTEXT (x) = current_function_decl; 211118334Speter 2112169699Skan /* If this is of variably modified type, prevent jumping into its 2113169699Skan scope. */ 2114169699Skan if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL) 2115169699Skan && variably_modified_type_p (TREE_TYPE (x), NULL_TREE)) 2116169699Skan c_begin_vm_scope (scope->depth); 2117169699Skan 2118169699Skan /* Anonymous decls are just inserted in the scope. */ 2119169699Skan if (!name) 212018334Speter { 2121169699Skan bind (name, x, scope, /*invisible=*/false, /*nested=*/false); 2122169699Skan return x; 2123169699Skan } 212418334Speter 2125169699Skan /* First, see if there is another declaration with the same name in 2126169699Skan the current scope. If there is, duplicate_decls may do all the 2127169699Skan work for us. If duplicate_decls returns false, that indicates 2128169699Skan two incompatible decls in the same scope; we are to silently 2129169699Skan replace the old one (duplicate_decls has issued all appropriate 2130169699Skan diagnostics). In particular, we should not consider possible 2131169699Skan duplicates in the external scope, or shadowing. */ 2132169699Skan b = I_SYMBOL_BINDING (name); 2133169699Skan if (b && B_IN_SCOPE (b, scope)) 2134169699Skan { 2135169699Skan struct c_binding *b_ext, *b_use; 2136169699Skan tree type = TREE_TYPE (x); 2137169699Skan tree visdecl = b->decl; 2138169699Skan tree vistype = TREE_TYPE (visdecl); 2139169699Skan if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 2140169699Skan && COMPLETE_TYPE_P (TREE_TYPE (x))) 2141169699Skan b->inner_comp = false; 2142169699Skan b_use = b; 2143169699Skan b_ext = b; 2144169699Skan /* If this is an external linkage declaration, we should check 2145169699Skan for compatibility with the type in the external scope before 2146169699Skan setting the type at this scope based on the visible 2147169699Skan information only. */ 2148169699Skan if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl)) 2149169699Skan { 2150169699Skan while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext)) 2151169699Skan b_ext = b_ext->shadowed; 2152169699Skan if (b_ext) 2153169699Skan { 2154169699Skan b_use = b_ext; 2155169699Skan if (b_use->type) 2156169699Skan TREE_TYPE (b_use->decl) = b_use->type; 2157169699Skan } 2158169699Skan } 2159169699Skan if (duplicate_decls (x, b_use->decl)) 2160169699Skan { 2161169699Skan if (b_use != b) 2162169699Skan { 2163169699Skan /* Save the updated type in the external scope and 2164169699Skan restore the proper type for this scope. */ 2165169699Skan tree thistype; 2166169699Skan if (comptypes (vistype, type)) 2167169699Skan thistype = composite_type (vistype, type); 2168169699Skan else 2169169699Skan thistype = TREE_TYPE (b_use->decl); 2170169699Skan b_use->type = TREE_TYPE (b_use->decl); 2171169699Skan if (TREE_CODE (b_use->decl) == FUNCTION_DECL 2172169699Skan && DECL_BUILT_IN (b_use->decl)) 2173169699Skan thistype 2174169699Skan = build_type_attribute_variant (thistype, 2175169699Skan TYPE_ATTRIBUTES 2176169699Skan (b_use->type)); 2177169699Skan TREE_TYPE (b_use->decl) = thistype; 2178169699Skan } 2179169699Skan return b_use->decl; 2180169699Skan } 2181169699Skan else 2182169699Skan goto skip_external_and_shadow_checks; 2183169699Skan } 2184169699Skan 2185169699Skan /* All declarations with external linkage, and all external 2186169699Skan references, go in the external scope, no matter what scope is 2187169699Skan current. However, the binding in that scope is ignored for 2188169699Skan purposes of normal name lookup. A separate binding structure is 2189169699Skan created in the requested scope; this governs the normal 2190169699Skan visibility of the symbol. 2191169699Skan 2192169699Skan The binding in the externals scope is used exclusively for 2193169699Skan detecting duplicate declarations of the same object, no matter 2194169699Skan what scope they are in; this is what we do here. (C99 6.2.7p2: 2195169699Skan All declarations that refer to the same object or function shall 2196169699Skan have compatible type; otherwise, the behavior is undefined.) */ 2197169699Skan if (DECL_EXTERNAL (x) || scope == file_scope) 2198169699Skan { 2199169699Skan tree type = TREE_TYPE (x); 2200169699Skan tree vistype = 0; 2201169699Skan tree visdecl = 0; 2202169699Skan bool type_saved = false; 2203169699Skan if (b && !B_IN_EXTERNAL_SCOPE (b) 2204169699Skan && (TREE_CODE (b->decl) == FUNCTION_DECL 2205169699Skan || TREE_CODE (b->decl) == VAR_DECL) 2206169699Skan && DECL_FILE_SCOPE_P (b->decl)) 2207169699Skan { 2208169699Skan visdecl = b->decl; 2209169699Skan vistype = TREE_TYPE (visdecl); 2210169699Skan } 2211169699Skan if (scope != file_scope 221290075Sobrien && !DECL_IN_SYSTEM_HEADER (x)) 2213169699Skan warning (OPT_Wnested_externs, "nested extern declaration of %qD", x); 221490075Sobrien 2215169699Skan while (b && !B_IN_EXTERNAL_SCOPE (b)) 221650397Sobrien { 2217169699Skan /* If this decl might be modified, save its type. This is 2218169699Skan done here rather than when the decl is first bound 2219169699Skan because the type may change after first binding, through 2220169699Skan being completed or through attributes being added. If we 2221169699Skan encounter multiple such decls, only the first should have 2222169699Skan its type saved; the others will already have had their 2223169699Skan proper types saved and the types will not have changed as 2224169699Skan their scopes will not have been re-entered. */ 2225169699Skan if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved) 222618334Speter { 2227169699Skan b->type = TREE_TYPE (b->decl); 2228169699Skan type_saved = true; 222918334Speter } 2230169699Skan if (B_IN_FILE_SCOPE (b) 2231169699Skan && TREE_CODE (b->decl) == VAR_DECL 2232169699Skan && TREE_STATIC (b->decl) 2233169699Skan && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE 2234169699Skan && !TYPE_DOMAIN (TREE_TYPE (b->decl)) 2235169699Skan && TREE_CODE (type) == ARRAY_TYPE 2236169699Skan && TYPE_DOMAIN (type) 2237169699Skan && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) 2238169699Skan && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) 2239169699Skan { 2240169699Skan /* Array type completed in inner scope, which should be 2241169699Skan diagnosed if the completion does not have size 1 and 2242169699Skan it does not get completed in the file scope. */ 2243169699Skan b->inner_comp = true; 2244169699Skan } 2245169699Skan b = b->shadowed; 224618334Speter } 2247169699Skan 2248169699Skan /* If a matching external declaration has been found, set its 2249169699Skan type to the composite of all the types of that declaration. 2250169699Skan After the consistency checks, it will be reset to the 2251169699Skan composite of the visible types only. */ 2252169699Skan if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl)) 2253169699Skan && b->type) 2254169699Skan TREE_TYPE (b->decl) = b->type; 2255169699Skan 2256169699Skan /* The point of the same_translation_unit_p check here is, 2257169699Skan we want to detect a duplicate decl for a construct like 2258169699Skan foo() { extern bar(); } ... static bar(); but not if 2259169699Skan they are in different translation units. In any case, 2260169699Skan the static does not go in the externals scope. */ 2261169699Skan if (b 2262169699Skan && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl)) 2263169699Skan && duplicate_decls (x, b->decl)) 226490075Sobrien { 2265169699Skan tree thistype; 2266169699Skan if (vistype) 226790075Sobrien { 2268169699Skan if (comptypes (vistype, type)) 2269169699Skan thistype = composite_type (vistype, type); 2270169699Skan else 2271169699Skan thistype = TREE_TYPE (b->decl); 227290075Sobrien } 227318334Speter else 2274169699Skan thistype = type; 2275169699Skan b->type = TREE_TYPE (b->decl); 2276169699Skan if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl)) 2277169699Skan thistype 2278169699Skan = build_type_attribute_variant (thistype, 2279169699Skan TYPE_ATTRIBUTES (b->type)); 2280169699Skan TREE_TYPE (b->decl) = thistype; 2281169699Skan bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true); 2282169699Skan return b->decl; 228318334Speter } 2284169699Skan else if (TREE_PUBLIC (x)) 228518334Speter { 2286169699Skan if (visdecl && !b && duplicate_decls (x, visdecl)) 228718334Speter { 2288169699Skan /* An external declaration at block scope referring to a 2289169699Skan visible entity with internal linkage. The composite 2290169699Skan type will already be correct for this scope, so we 2291169699Skan just need to fall through to make the declaration in 2292169699Skan this scope. */ 2293169699Skan nested = true; 2294169699Skan x = visdecl; 229518334Speter } 2296169699Skan else 2297169699Skan { 2298169699Skan bind (name, x, external_scope, /*invisible=*/true, 2299169699Skan /*nested=*/false); 2300169699Skan nested = true; 2301169699Skan } 230218334Speter } 2303169699Skan } 230490075Sobrien 2305169699Skan if (TREE_CODE (x) != PARM_DECL) 2306169699Skan warn_if_shadowing (x); 230718334Speter 2308169699Skan skip_external_and_shadow_checks: 2309169699Skan if (TREE_CODE (x) == TYPE_DECL) 2310169699Skan clone_underlying_type (x); 231118334Speter 2312169699Skan bind (name, x, scope, /*invisible=*/false, nested); 231390075Sobrien 2314169699Skan /* If x's type is incomplete because it's based on a 2315169699Skan structure or union which has not yet been fully declared, 2316169699Skan attach it to that structure or union type, so we can go 2317169699Skan back and complete the variable declaration later, if the 2318169699Skan structure or union gets fully declared. 2319132730Skan 2320169699Skan If the input is erroneous, we can have error_mark in the type 2321169699Skan slot (e.g. "f(void a, ...)") - that doesn't count as an 2322169699Skan incomplete type. */ 2323169699Skan if (TREE_TYPE (x) != error_mark_node 2324169699Skan && !COMPLETE_TYPE_P (TREE_TYPE (x))) 2325169699Skan { 2326169699Skan tree element = TREE_TYPE (x); 232718334Speter 2328169699Skan while (TREE_CODE (element) == ARRAY_TYPE) 2329169699Skan element = TREE_TYPE (element); 2330169699Skan element = TYPE_MAIN_VARIANT (element); 233118334Speter 2332169699Skan if ((TREE_CODE (element) == RECORD_TYPE 2333169699Skan || TREE_CODE (element) == UNION_TYPE) 2334169699Skan && (TREE_CODE (x) != TYPE_DECL 2335169699Skan || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE) 2336169699Skan && !COMPLETE_TYPE_P (element)) 2337169699Skan C_TYPE_INCOMPLETE_VARS (element) 2338169699Skan = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element)); 2339169699Skan } 234018334Speter return x; 234118334Speter} 234218334Speter 2343169699Skan/* Record X as belonging to file scope. 2344132730Skan This is used only internally by the Objective-C front end, 2345132730Skan and is limited to its needs. duplicate_decls is not called; 2346132730Skan if there is any preexisting decl for this identifier, it is an ICE. */ 234718334Speter 234818334Spetertree 2349132730Skanpushdecl_top_level (tree x) 235018334Speter{ 2351132730Skan tree name; 2352169699Skan bool nested = false; 2353169699Skan gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL); 235418334Speter 2355132730Skan name = DECL_NAME (x); 2356132730Skan 2357169699Skan gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name)); 2358132730Skan 2359169699Skan if (TREE_PUBLIC (x)) 2360169699Skan { 2361169699Skan bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false); 2362169699Skan nested = true; 2363169699Skan } 2364169699Skan if (file_scope) 2365169699Skan bind (name, x, file_scope, /*invisible=*/false, nested); 2366132730Skan 2367132730Skan return x; 236818334Speter} 236918334Speter 2370169699Skanstatic void 2371169699Skanimplicit_decl_warning (tree id, tree olddecl) 2372169699Skan{ 2373169699Skan void (*diag) (const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2); 2374169699Skan switch (mesg_implicit_function_declaration) 2375169699Skan { 2376169699Skan case 0: return; 2377169699Skan case 1: diag = warning0; break; 2378169699Skan case 2: diag = error; break; 2379169699Skan default: gcc_unreachable (); 2380169699Skan } 2381169699Skan 2382169699Skan diag (G_("implicit declaration of function %qE"), id); 2383169699Skan if (olddecl) 2384169699Skan locate_old_decl (olddecl, diag); 2385169699Skan} 2386169699Skan 2387132730Skan/* Generate an implicit declaration for identifier FUNCTIONID as a 2388132730Skan function of type int (). */ 238918334Speter 239018334Spetertree 2391132730Skanimplicitly_declare (tree functionid) 239218334Speter{ 2393169699Skan struct c_binding *b; 2394169699Skan tree decl = 0; 2395169699Skan tree asmspec_tree; 239618334Speter 2397169699Skan for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed) 2398169699Skan { 2399169699Skan if (B_IN_SCOPE (b, external_scope)) 2400169699Skan { 2401169699Skan decl = b->decl; 2402169699Skan break; 2403169699Skan } 2404169699Skan } 2405169699Skan 2406132730Skan if (decl) 2407132730Skan { 2408169699Skan if (decl == error_mark_node) 2409169699Skan return decl; 2410169699Skan 2411169699Skan /* FIXME: Objective-C has weird not-really-builtin functions 2412169699Skan which are supposed to be visible automatically. They wind up 2413169699Skan in the external scope because they're pushed before the file 2414169699Skan scope gets created. Catch this here and rebind them into the 2415169699Skan file scope. */ 2416169699Skan if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl)) 2417132730Skan { 2418169699Skan bind (functionid, decl, file_scope, 2419169699Skan /*invisible=*/false, /*nested=*/true); 2420169699Skan return decl; 2421132730Skan } 2422169699Skan else 2423169699Skan { 2424169699Skan tree newtype = default_function_type; 2425169699Skan if (b->type) 2426169699Skan TREE_TYPE (decl) = b->type; 2427169699Skan /* Implicit declaration of a function already declared 2428169699Skan (somehow) in a different scope, or as a built-in. 2429169699Skan If this is the first time this has happened, warn; 2430169699Skan then recycle the old declaration but with the new type. */ 2431169699Skan if (!C_DECL_IMPLICIT (decl)) 2432169699Skan { 2433169699Skan implicit_decl_warning (functionid, decl); 2434169699Skan C_DECL_IMPLICIT (decl) = 1; 2435169699Skan } 2436169699Skan if (DECL_BUILT_IN (decl)) 2437169699Skan { 2438169699Skan newtype = build_type_attribute_variant (newtype, 2439169699Skan TYPE_ATTRIBUTES 2440169699Skan (TREE_TYPE (decl))); 2441169699Skan if (!comptypes (newtype, TREE_TYPE (decl))) 2442169699Skan { 2443169699Skan warning (0, "incompatible implicit declaration of built-in" 2444169699Skan " function %qD", decl); 2445169699Skan newtype = TREE_TYPE (decl); 2446169699Skan } 2447169699Skan } 2448169699Skan else 2449169699Skan { 2450169699Skan if (!comptypes (newtype, TREE_TYPE (decl))) 2451169699Skan { 2452169699Skan error ("incompatible implicit declaration of function %qD", 2453169699Skan decl); 2454169699Skan locate_old_decl (decl, error); 2455169699Skan } 2456169699Skan } 2457169699Skan b->type = TREE_TYPE (decl); 2458169699Skan TREE_TYPE (decl) = newtype; 2459169699Skan bind (functionid, decl, current_scope, 2460169699Skan /*invisible=*/false, /*nested=*/true); 2461169699Skan return decl; 2462169699Skan } 2463132730Skan } 246418334Speter 2465132730Skan /* Not seen before. */ 2466132730Skan decl = build_decl (FUNCTION_DECL, functionid, default_function_type); 246718334Speter DECL_EXTERNAL (decl) = 1; 246818334Speter TREE_PUBLIC (decl) = 1; 2469132730Skan C_DECL_IMPLICIT (decl) = 1; 2470169699Skan implicit_decl_warning (functionid, 0); 2471169699Skan asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL); 2472169699Skan if (asmspec_tree) 2473169699Skan set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree)); 247418334Speter 2475132730Skan /* C89 says implicit declarations are in the innermost block. 2476117421Skan So we record the decl in the standard fashion. */ 2477132730Skan decl = pushdecl (decl); 247818334Speter 2479132730Skan /* No need to call objc_check_decl here - it's a function type. */ 2480169699Skan rest_of_decl_compilation (decl, 0, 0); 248118334Speter 2482132730Skan /* Write a record describing this implicit function declaration 2483132730Skan to the prototypes file (if requested). */ 248418334Speter gen_aux_info_record (decl, 0, 1, 0); 248518334Speter 248690075Sobrien /* Possibly apply some default attributes to this implicit declaration. */ 248790075Sobrien decl_attributes (&decl, NULL_TREE, 0); 248818334Speter 248918334Speter return decl; 249018334Speter} 249118334Speter 2492132730Skan/* Issue an error message for a reference to an undeclared variable 2493132730Skan ID, including a reference to a builtin outside of function-call 2494132730Skan context. Establish a binding of the identifier to error_mark_node 2495132730Skan in an appropriate scope, which will suppress further errors for the 2496169699Skan same identifier. The error message should be given location LOC. */ 2497132730Skanvoid 2498169699Skanundeclared_variable (tree id, location_t loc) 2499132730Skan{ 2500132730Skan static bool already = false; 2501132730Skan struct c_scope *scope; 250218334Speter 2503132730Skan if (current_function_decl == 0) 250418334Speter { 2505169699Skan error ("%H%qE undeclared here (not in a function)", &loc, id); 2506132730Skan scope = current_scope; 250718334Speter } 250818334Speter else 250918334Speter { 2510169699Skan error ("%H%qE undeclared (first use in this function)", &loc, id); 2511132730Skan 2512169699Skan if (!already) 2513132730Skan { 2514169699Skan error ("%H(Each undeclared identifier is reported only once", &loc); 2515169699Skan error ("%Hfor each function it appears in.)", &loc); 2516132730Skan already = true; 2517132730Skan } 2518132730Skan 2519169699Skan /* If we are parsing old-style parameter decls, current_function_decl 2520169699Skan will be nonnull but current_function_scope will be null. */ 2521169699Skan scope = current_function_scope ? current_function_scope : current_scope; 252218334Speter } 2523169699Skan bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false); 252418334Speter} 252518334Speter 2526132730Skan/* Subroutine of lookup_label, declare_label, define_label: construct a 2527132730Skan LABEL_DECL with all the proper frills. */ 2528132730Skan 2529132730Skanstatic tree 2530132730Skanmake_label (tree name, location_t location) 2531132730Skan{ 2532132730Skan tree label = build_decl (LABEL_DECL, name, void_type_node); 2533132730Skan 2534132730Skan DECL_CONTEXT (label) = current_function_decl; 2535132730Skan DECL_MODE (label) = VOIDmode; 2536132730Skan DECL_SOURCE_LOCATION (label) = location; 2537132730Skan 2538132730Skan return label; 2539132730Skan} 2540132730Skan 2541132730Skan/* Get the LABEL_DECL corresponding to identifier NAME as a label. 254218334Speter Create one if none exists so far for the current function. 2543132730Skan This is called when a label is used in a goto expression or 2544132730Skan has its address taken. */ 254518334Speter 254618334Spetertree 2547132730Skanlookup_label (tree name) 254818334Speter{ 2549132730Skan tree label; 255018334Speter 255118334Speter if (current_function_decl == 0) 255218334Speter { 2553169699Skan error ("label %qE referenced outside of any function", name); 255418334Speter return 0; 255518334Speter } 255618334Speter 2557132730Skan /* Use a label already defined or ref'd with this name, but not if 2558132730Skan it is inherited from a containing function and wasn't declared 2559132730Skan using __label__. */ 2560169699Skan label = I_LABEL_DECL (name); 2561132730Skan if (label && (DECL_CONTEXT (label) == current_function_decl 2562132730Skan || C_DECLARED_LABEL_FLAG (label))) 256318334Speter { 2564132730Skan /* If the label has only been declared, update its apparent 2565132730Skan location to point here, for better diagnostics if it 2566132730Skan turns out not to have been defined. */ 2567132730Skan if (!TREE_USED (label)) 2568132730Skan DECL_SOURCE_LOCATION (label) = input_location; 2569132730Skan return label; 257018334Speter } 257118334Speter 2572132730Skan /* No label binding for that identifier; make one. */ 2573132730Skan label = make_label (name, input_location); 257418334Speter 2575132730Skan /* Ordinary labels go in the current function scope. */ 2576169699Skan bind (name, label, current_function_scope, 2577169699Skan /*invisible=*/false, /*nested=*/false); 2578132730Skan return label; 257918334Speter} 258018334Speter 2581132730Skan/* Make a label named NAME in the current function, shadowing silently 2582132730Skan any that may be inherited from containing functions or containing 2583132730Skan scopes. This is called for __label__ declarations. */ 258418334Speter 258518334Spetertree 2586132730Skandeclare_label (tree name) 258718334Speter{ 2588169699Skan struct c_binding *b = I_LABEL_BINDING (name); 2589169699Skan tree label; 259018334Speter 2591132730Skan /* Check to make sure that the label hasn't already been declared 2592132730Skan at this scope */ 2593169699Skan if (b && B_IN_CURRENT_SCOPE (b)) 2594169699Skan { 2595169699Skan error ("duplicate label declaration %qE", name); 2596169699Skan locate_old_decl (b->decl, error); 259718334Speter 2598169699Skan /* Just use the previous declaration. */ 2599169699Skan return b->decl; 2600169699Skan } 260118334Speter 2602132730Skan label = make_label (name, input_location); 2603132730Skan C_DECLARED_LABEL_FLAG (label) = 1; 260418334Speter 2605132730Skan /* Declared labels go in the current scope. */ 2606169699Skan bind (name, label, current_scope, 2607169699Skan /*invisible=*/false, /*nested=*/false); 2608132730Skan return label; 260918334Speter} 261018334Speter 261118334Speter/* Define a label, specifying the location in the source file. 261218334Speter Return the LABEL_DECL node for the label, if the definition is valid. 261318334Speter Otherwise return 0. */ 261418334Speter 261518334Spetertree 2616132730Skandefine_label (location_t location, tree name) 261718334Speter{ 2618132730Skan /* Find any preexisting label with this name. It is an error 2619132730Skan if that label has already been defined in this function, or 2620132730Skan if there is a containing function with a declared label with 2621132730Skan the same name. */ 2622169699Skan tree label = I_LABEL_DECL (name); 2623169699Skan struct c_label_list *nlist_se, *nlist_vm; 262418334Speter 2625132730Skan if (label 2626132730Skan && ((DECL_CONTEXT (label) == current_function_decl 2627132730Skan && DECL_INITIAL (label) != 0) 2628132730Skan || (DECL_CONTEXT (label) != current_function_decl 2629132730Skan && C_DECLARED_LABEL_FLAG (label)))) 263018334Speter { 2631169699Skan error ("%Hduplicate label %qD", &location, label); 2632169699Skan locate_old_decl (label, error); 263318334Speter return 0; 263418334Speter } 2635132730Skan else if (label && DECL_CONTEXT (label) == current_function_decl) 2636132730Skan { 2637132730Skan /* The label has been used or declared already in this function, 2638132730Skan but not defined. Update its location to point to this 2639132730Skan definition. */ 2640169699Skan if (C_DECL_UNDEFINABLE_STMT_EXPR (label)) 2641169699Skan error ("%Jjump into statement expression", label); 2642169699Skan if (C_DECL_UNDEFINABLE_VM (label)) 2643169699Skan error ("%Jjump into scope of identifier with variably modified type", 2644169699Skan label); 2645132730Skan DECL_SOURCE_LOCATION (label) = location; 2646132730Skan } 264718334Speter else 264818334Speter { 2649132730Skan /* No label binding for that identifier; make one. */ 2650132730Skan label = make_label (name, location); 2651132730Skan 2652132730Skan /* Ordinary labels go in the current function scope. */ 2653169699Skan bind (name, label, current_function_scope, 2654169699Skan /*invisible=*/false, /*nested=*/false); 265518334Speter } 2656132730Skan 2657169699Skan if (!in_system_header && lookup_name (name)) 2658169699Skan warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace " 2659169699Skan "for labels, identifier %qE conflicts", &location, name); 2660132730Skan 2661169699Skan nlist_se = XOBNEW (&parser_obstack, struct c_label_list); 2662169699Skan nlist_se->next = label_context_stack_se->labels_def; 2663169699Skan nlist_se->label = label; 2664169699Skan label_context_stack_se->labels_def = nlist_se; 2665169699Skan 2666169699Skan nlist_vm = XOBNEW (&parser_obstack, struct c_label_list); 2667169699Skan nlist_vm->next = label_context_stack_vm->labels_def; 2668169699Skan nlist_vm->label = label; 2669169699Skan label_context_stack_vm->labels_def = nlist_vm; 2670169699Skan 2671132730Skan /* Mark label as having been defined. */ 2672132730Skan DECL_INITIAL (label) = error_mark_node; 2673132730Skan return label; 267418334Speter} 267518334Speter 267618334Speter/* Given NAME, an IDENTIFIER_NODE, 267718334Speter return the structure (or union or enum) definition for that name. 2678132730Skan If THISLEVEL_ONLY is nonzero, searches only the current_scope. 267918334Speter CODE says which kind of type the caller wants; 268018334Speter it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. 268118334Speter If the wrong kind of type is found, an error is reported. */ 268218334Speter 268318334Speterstatic tree 2684132730Skanlookup_tag (enum tree_code code, tree name, int thislevel_only) 268518334Speter{ 2686169699Skan struct c_binding *b = I_TAG_BINDING (name); 2687132730Skan int thislevel = 0; 268818334Speter 2689169699Skan if (!b || !b->decl) 2690132730Skan return 0; 2691132730Skan 2692132730Skan /* We only care about whether it's in this level if 2693132730Skan thislevel_only was set or it might be a type clash. */ 2694169699Skan if (thislevel_only || TREE_CODE (b->decl) != code) 269518334Speter { 2696169699Skan /* For our purposes, a tag in the external scope is the same as 2697169699Skan a tag in the file scope. (Primarily relevant to Objective-C 2698169699Skan and its builtin structure tags, which get pushed before the 2699169699Skan file scope is created.) */ 2700169699Skan if (B_IN_CURRENT_SCOPE (b) 2701169699Skan || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b))) 2702132730Skan thislevel = 1; 270318334Speter } 2704132730Skan 2705132730Skan if (thislevel_only && !thislevel) 2706132730Skan return 0; 2707132730Skan 2708169699Skan if (TREE_CODE (b->decl) != code) 2709132730Skan { 2710132730Skan /* Definition isn't the kind we were looking for. */ 2711132730Skan pending_invalid_xref = name; 2712132730Skan pending_invalid_xref_location = input_location; 2713132730Skan 2714132730Skan /* If in the same binding level as a declaration as a tag 2715132730Skan of a different type, this must not be allowed to 2716132730Skan shadow that tag, so give the error immediately. 2717132730Skan (For example, "struct foo; union foo;" is invalid.) */ 2718132730Skan if (thislevel) 2719132730Skan pending_xref_error (); 2720132730Skan } 2721169699Skan return b->decl; 272218334Speter} 272318334Speter 272418334Speter/* Print an error message now 272518334Speter for a recent invalid struct, union or enum cross reference. 272618334Speter We don't print them immediately because they are not invalid 272718334Speter when used in the `struct foo;' construct for shadowing. */ 272818334Speter 272918334Spetervoid 2730132730Skanpending_xref_error (void) 273118334Speter{ 273218334Speter if (pending_invalid_xref != 0) 2733169699Skan error ("%H%qE defined as wrong kind of tag", 2734169699Skan &pending_invalid_xref_location, pending_invalid_xref); 273518334Speter pending_invalid_xref = 0; 273618334Speter} 273718334Speter 273818334Speter 2739132730Skan/* Look up NAME in the current scope and its superiors 274018334Speter in the namespace of variables, functions and typedefs. 274118334Speter Return a ..._DECL node of some kind representing its definition, 274218334Speter or return 0 if it is undefined. */ 274318334Speter 274418334Spetertree 2745132730Skanlookup_name (tree name) 274618334Speter{ 2747169699Skan struct c_binding *b = I_SYMBOL_BINDING (name); 2748169699Skan if (b && !b->invisible) 2749169699Skan return b->decl; 2750169699Skan return 0; 275118334Speter} 275218334Speter 2753169699Skan/* Similar to `lookup_name' but look only at the indicated scope. */ 275418334Speter 2755132730Skanstatic tree 2756169699Skanlookup_name_in_scope (tree name, struct c_scope *scope) 275718334Speter{ 2758169699Skan struct c_binding *b; 275918334Speter 2760169699Skan for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed) 2761169699Skan if (B_IN_SCOPE (b, scope)) 2762169699Skan return b->decl; 2763132730Skan return 0; 276418334Speter} 276518334Speter 276618334Speter/* Create the predefined scalar types of C, 276750397Sobrien and some nodes representing standard constants (0, 1, (void *) 0). 2768132730Skan Initialize the global scope. 276918334Speter Make definitions for built-in primitive functions. */ 277018334Speter 277118334Spetervoid 2772132730Skanc_init_decl_processing (void) 277318334Speter{ 2774132730Skan location_t save_loc = input_location; 277518334Speter 2776169699Skan /* Initialize reserved words for parser. */ 277790075Sobrien c_parse_init (); 277890075Sobrien 2779132730Skan current_function_decl = 0; 278090075Sobrien 2781169699Skan gcc_obstack_init (&parser_obstack); 278218334Speter 2783169699Skan /* Make the externals scope. */ 2784169699Skan push_scope (); 2785169699Skan external_scope = current_scope; 2786169699Skan 2787132730Skan /* Declarations from c_common_nodes_and_builtins must not be associated 2788132730Skan with this input file, lest we get differences between using and not 2789132730Skan using preprocessed headers. */ 2790169699Skan#ifdef USE_MAPPED_LOCATION 2791169699Skan input_location = BUILTINS_LOCATION; 2792169699Skan#else 2793169699Skan input_location.file = "<built-in>"; 2794132730Skan input_location.line = 0; 2795169699Skan#endif 2796132730Skan 2797169699Skan build_common_tree_nodes (flag_signed_char, false); 2798132730Skan 279990075Sobrien c_common_nodes_and_builtins (); 280018334Speter 2801132730Skan /* In C, comparisons and TRUTH_* expressions have type int. */ 2802132730Skan truthvalue_type_node = integer_type_node; 2803132730Skan truthvalue_true_node = integer_one_node; 2804132730Skan truthvalue_false_node = integer_zero_node; 280518334Speter 2806132730Skan /* Even in C99, which has a real boolean type. */ 280790075Sobrien pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"), 2808132730Skan boolean_type_node)); 280918334Speter 2810132730Skan input_location = save_loc; 2811132730Skan 2812169699Skan pedantic_lvalues = true; 281318334Speter 281490075Sobrien make_fname_decl = c_make_fname_decl; 281590075Sobrien start_fname_decls (); 281690075Sobrien} 281718334Speter 281890075Sobrien/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the 281990075Sobrien decl, NAME is the initialization string and TYPE_DEP indicates whether 282090075Sobrien NAME depended on the type of the function. As we don't yet implement 282190075Sobrien delayed emission of static data, we mark the decl as emitted 282290075Sobrien so it is not placed in the output. Anything using it must therefore pull 2823132730Skan out the STRING_CST initializer directly. FIXME. */ 282418334Speter 282590075Sobrienstatic tree 2826132730Skanc_make_fname_decl (tree id, int type_dep) 282790075Sobrien{ 282890075Sobrien const char *name = fname_as_string (type_dep); 282990075Sobrien tree decl, type, init; 283090075Sobrien size_t length = strlen (name); 283118334Speter 2832169699Skan type = build_array_type (char_type_node, 2833169699Skan build_index_type (size_int (length))); 2834169699Skan type = c_build_qualified_type (type, TYPE_QUAL_CONST); 283518334Speter 283690075Sobrien decl = build_decl (VAR_DECL, id, type); 2837132730Skan 283890075Sobrien TREE_STATIC (decl) = 1; 283990075Sobrien TREE_READONLY (decl) = 1; 284090075Sobrien DECL_ARTIFICIAL (decl) = 1; 2841132730Skan 284290075Sobrien init = build_string (length + 1, name); 2843169699Skan free ((char *) name); 284490075Sobrien TREE_TYPE (init) = type; 284590075Sobrien DECL_INITIAL (decl) = init; 284618334Speter 284790075Sobrien TREE_USED (decl) = 1; 2848132730Skan 2849169699Skan if (current_function_decl 2850169699Skan /* For invalid programs like this: 2851169699Skan 2852169699Skan void foo() 2853169699Skan const char* p = __FUNCTION__; 2854169699Skan 2855169699Skan the __FUNCTION__ is believed to appear in K&R style function 2856169699Skan parameter declarator. In that case we still don't have 2857169699Skan function_scope. */ 2858169699Skan && (!errorcount || current_function_scope)) 2859132730Skan { 2860132730Skan DECL_CONTEXT (decl) = current_function_decl; 2861169699Skan bind (id, decl, current_function_scope, 2862169699Skan /*invisible=*/false, /*nested=*/false); 2863132730Skan } 2864132730Skan 286590075Sobrien finish_decl (decl, init, NULL_TREE); 286618334Speter 286790075Sobrien return decl; 286818334Speter} 286918334Speter 287018334Speter/* Return a definition for a builtin function named NAME and whose data type 287118334Speter is TYPE. TYPE should be a function type with argument types. 287218334Speter FUNCTION_CODE tells later passes how to compile calls to this function. 287318334Speter See tree.h for its possible values. 287418334Speter 287518334Speter If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME, 2876117421Skan the name to be called if we can't opencode the function. If 2877117421Skan ATTRS is nonzero, use that for the function's attribute list. */ 287818334Speter 287918334Spetertree 2880132730Skanbuiltin_function (const char *name, tree type, int function_code, 2881169699Skan enum built_in_class cl, const char *library_name, 2882132730Skan tree attrs) 288318334Speter{ 2884169699Skan tree id = get_identifier (name); 2885169699Skan tree decl = build_decl (FUNCTION_DECL, id, type); 2886169699Skan TREE_PUBLIC (decl) = 1; 288718334Speter DECL_EXTERNAL (decl) = 1; 2888169699Skan DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl); 2889169699Skan DECL_BUILT_IN_CLASS (decl) = cl; 2890169699Skan DECL_FUNCTION_CODE (decl) = function_code; 2891169699Skan C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0); 289218334Speter if (library_name) 289390075Sobrien SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name)); 289490075Sobrien 2895169699Skan /* Should never be called on a symbol with a preexisting meaning. */ 2896169699Skan gcc_assert (!I_SYMBOL_BINDING (id)); 289718334Speter 2898169699Skan bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false); 2899169699Skan 2900169699Skan /* Builtins in the implementation namespace are made visible without 2901169699Skan needing to be explicitly declared. See push_file_scope. */ 2902169699Skan if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1]))) 2903169699Skan { 2904169699Skan TREE_CHAIN (decl) = visible_builtins; 2905169699Skan visible_builtins = decl; 2906169699Skan } 2907169699Skan 290890075Sobrien /* Possibly apply some default attributes to this built-in function. */ 2909117421Skan if (attrs) 2910117421Skan decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN); 2911117421Skan else 2912117421Skan decl_attributes (&decl, NULL_TREE, 0); 291390075Sobrien 291418334Speter return decl; 291518334Speter} 291618334Speter 291718334Speter/* Called when a declaration is seen that contains no names to declare. 291818334Speter If its type is a reference to a structure, union or enum inherited 291918334Speter from a containing scope, shadow that tag name for the current scope 292018334Speter with a forward reference. 292118334Speter If its type defines a new named structure or union 292218334Speter or defines an enum, it is valid but we need not do anything here. 292318334Speter Otherwise, it is an error. */ 292418334Speter 292518334Spetervoid 2926169699Skanshadow_tag (const struct c_declspecs *declspecs) 292718334Speter{ 292818334Speter shadow_tag_warned (declspecs, 0); 292918334Speter} 293018334Speter 2931169699Skan/* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning, 2932169699Skan but no pedwarn. */ 293318334Spetervoid 2934169699Skanshadow_tag_warned (const struct c_declspecs *declspecs, int warned) 293518334Speter{ 2936169699Skan bool found_tag = false; 293718334Speter 2938169699Skan if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p) 293918334Speter { 2940169699Skan tree value = declspecs->type; 294190075Sobrien enum tree_code code = TREE_CODE (value); 294218334Speter 294318334Speter if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE) 294418334Speter /* Used to test also that TYPE_SIZE (value) != 0. 294518334Speter That caused warning for `struct foo;' at top level in the file. */ 294618334Speter { 2947132730Skan tree name = TYPE_NAME (value); 294890075Sobrien tree t; 294918334Speter 2950169699Skan found_tag = true; 295118334Speter 295218334Speter if (name == 0) 295318334Speter { 295418334Speter if (warned != 1 && code != ENUMERAL_TYPE) 295518334Speter /* Empty unnamed enum OK */ 295618334Speter { 295718334Speter pedwarn ("unnamed struct/union that defines no instances"); 295818334Speter warned = 1; 295918334Speter } 296018334Speter } 2961169699Skan else if (!declspecs->tag_defined_p 2962169699Skan && declspecs->storage_class != csc_none) 2963169699Skan { 2964169699Skan if (warned != 1) 2965169699Skan pedwarn ("empty declaration with storage class specifier " 2966169699Skan "does not redeclare tag"); 2967169699Skan warned = 1; 2968169699Skan pending_xref_error (); 2969169699Skan } 2970169699Skan else if (!declspecs->tag_defined_p 2971169699Skan && (declspecs->const_p 2972169699Skan || declspecs->volatile_p 2973169699Skan || declspecs->restrict_p)) 2974169699Skan { 2975169699Skan if (warned != 1) 2976169699Skan pedwarn ("empty declaration with type qualifier " 2977169699Skan "does not redeclare tag"); 2978169699Skan warned = 1; 2979169699Skan pending_xref_error (); 2980169699Skan } 298118334Speter else 298218334Speter { 2983169699Skan pending_invalid_xref = 0; 2984132730Skan t = lookup_tag (code, name, 1); 298518334Speter 298618334Speter if (t == 0) 298718334Speter { 298818334Speter t = make_node (code); 298918334Speter pushtag (name, t); 299018334Speter } 299118334Speter } 299218334Speter } 299318334Speter else 299418334Speter { 2995169699Skan if (warned != 1 && !in_system_header) 299618334Speter { 2997169699Skan pedwarn ("useless type name in empty declaration"); 2998169699Skan warned = 1; 299918334Speter } 300018334Speter } 300118334Speter } 3002169699Skan else if (warned != 1 && !in_system_header && declspecs->typedef_p) 3003169699Skan { 3004169699Skan pedwarn ("useless type name in empty declaration"); 3005169699Skan warned = 1; 3006169699Skan } 300718334Speter 3008169699Skan pending_invalid_xref = 0; 300918334Speter 3010169699Skan if (declspecs->inline_p) 3011169699Skan { 3012169699Skan error ("%<inline%> in empty declaration"); 3013169699Skan warned = 1; 3014169699Skan } 3015169699Skan 3016169699Skan if (current_scope == file_scope && declspecs->storage_class == csc_auto) 3017169699Skan { 3018169699Skan error ("%<auto%> in file-scope empty declaration"); 3019169699Skan warned = 1; 3020169699Skan } 3021169699Skan 3022169699Skan if (current_scope == file_scope && declspecs->storage_class == csc_register) 3023169699Skan { 3024169699Skan error ("%<register%> in file-scope empty declaration"); 3025169699Skan warned = 1; 3026169699Skan } 3027169699Skan 3028169699Skan if (!warned && !in_system_header && declspecs->storage_class != csc_none) 3029169699Skan { 3030169699Skan warning (0, "useless storage class specifier in empty declaration"); 3031169699Skan warned = 2; 3032169699Skan } 3033169699Skan 3034169699Skan if (!warned && !in_system_header && declspecs->thread_p) 3035169699Skan { 3036169699Skan warning (0, "useless %<__thread%> in empty declaration"); 3037169699Skan warned = 2; 3038169699Skan } 3039169699Skan 3040169699Skan if (!warned && !in_system_header && (declspecs->const_p 3041169699Skan || declspecs->volatile_p 3042169699Skan || declspecs->restrict_p)) 3043169699Skan { 3044169699Skan warning (0, "useless type qualifier in empty declaration"); 3045169699Skan warned = 2; 3046169699Skan } 3047169699Skan 304818334Speter if (warned != 1) 304918334Speter { 3050169699Skan if (!found_tag) 305118334Speter pedwarn ("empty declaration"); 305218334Speter } 305318334Speter} 305418334Speter 305590075Sobrien 3056169699Skan/* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_* 3057169699Skan bits. SPECS represents declaration specifiers that the grammar 3058169699Skan only permits to contain type qualifiers and attributes. */ 3059169699Skan 3060169699Skanint 3061169699Skanquals_from_declspecs (const struct c_declspecs *specs) 306290075Sobrien{ 3063169699Skan int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0) 3064169699Skan | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0) 3065169699Skan | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)); 3066169699Skan gcc_assert (!specs->type 3067169699Skan && !specs->decl_attr 3068169699Skan && specs->typespec_word == cts_none 3069169699Skan && specs->storage_class == csc_none 3070169699Skan && !specs->typedef_p 3071169699Skan && !specs->explicit_signed_p 3072169699Skan && !specs->deprecated_p 3073169699Skan && !specs->long_p 3074169699Skan && !specs->long_long_p 3075169699Skan && !specs->short_p 3076169699Skan && !specs->signed_p 3077169699Skan && !specs->unsigned_p 3078169699Skan && !specs->complex_p 3079169699Skan && !specs->inline_p 3080169699Skan && !specs->thread_p); 3081169699Skan return quals; 3082169699Skan} 3083169699Skan 3084169699Skan/* Construct an array declarator. EXPR is the expression inside [], 3085169699Skan or NULL_TREE. QUALS are the type qualifiers inside the [] (to be 3086169699Skan applied to the pointer to which a parameter array is converted). 3087169699Skan STATIC_P is true if "static" is inside the [], false otherwise. 3088169699Skan VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified 3089169699Skan length which is nevertheless a complete type, false otherwise. The 3090169699Skan field for the contained declarator is left to be filled in by 3091169699Skan set_array_declarator_inner. */ 3092169699Skan 3093169699Skanstruct c_declarator * 3094169699Skanbuild_array_declarator (tree expr, struct c_declspecs *quals, bool static_p, 3095169699Skan bool vla_unspec_p) 3096169699Skan{ 3097169699Skan struct c_declarator *declarator = XOBNEW (&parser_obstack, 3098169699Skan struct c_declarator); 3099169699Skan declarator->kind = cdk_array; 3100169699Skan declarator->declarator = 0; 3101169699Skan declarator->u.array.dimen = expr; 3102169699Skan if (quals) 3103169699Skan { 3104169699Skan declarator->u.array.attrs = quals->attrs; 3105169699Skan declarator->u.array.quals = quals_from_declspecs (quals); 3106169699Skan } 3107169699Skan else 3108169699Skan { 3109169699Skan declarator->u.array.attrs = NULL_TREE; 3110169699Skan declarator->u.array.quals = 0; 3111169699Skan } 3112169699Skan declarator->u.array.static_p = static_p; 3113169699Skan declarator->u.array.vla_unspec_p = vla_unspec_p; 311490075Sobrien if (pedantic && !flag_isoc99) 311590075Sobrien { 3116169699Skan if (static_p || quals != NULL) 3117169699Skan pedwarn ("ISO C90 does not support %<static%> or type " 3118169699Skan "qualifiers in parameter array declarators"); 311990075Sobrien if (vla_unspec_p) 3120169699Skan pedwarn ("ISO C90 does not support %<[*]%> array declarators"); 312190075Sobrien } 312290075Sobrien if (vla_unspec_p) 3123169699Skan { 3124169699Skan if (!current_scope->parm_flag) 3125169699Skan { 3126169699Skan /* C99 6.7.5.2p4 */ 3127169699Skan error ("%<[*]%> not allowed in other than function prototype scope"); 3128169699Skan declarator->u.array.vla_unspec_p = false; 3129169699Skan return NULL; 3130169699Skan } 3131169699Skan current_scope->had_vla_unspec = true; 3132169699Skan } 3133169699Skan return declarator; 313490075Sobrien} 313590075Sobrien 3136169699Skan/* Set the contained declarator of an array declarator. DECL is the 3137169699Skan declarator, as constructed by build_array_declarator; INNER is what 3138169699Skan appears on the left of the []. ABSTRACT_P is true if it is an 3139169699Skan abstract declarator, false otherwise; this is used to reject static 3140169699Skan and type qualifiers in abstract declarators, where they are not in 3141169699Skan the C99 grammar (subject to possible change in DR#289). */ 314290075Sobrien 3143169699Skanstruct c_declarator * 3144169699Skanset_array_declarator_inner (struct c_declarator *decl, 3145169699Skan struct c_declarator *inner, bool abstract_p) 314690075Sobrien{ 3147169699Skan decl->declarator = inner; 3148169699Skan if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED 3149169699Skan || decl->u.array.attrs != NULL_TREE 3150169699Skan || decl->u.array.static_p)) 315190075Sobrien error ("static or type qualifiers in abstract declarator"); 315290075Sobrien return decl; 315390075Sobrien} 3154169699Skan 3155169699Skan/* INIT is a constructor that forms DECL's initializer. If the final 3156169699Skan element initializes a flexible array field, add the size of that 3157169699Skan initializer to DECL's size. */ 3158169699Skan 3159169699Skanstatic void 3160169699Skanadd_flexible_array_elts_to_size (tree decl, tree init) 3161169699Skan{ 3162169699Skan tree elt, type; 3163169699Skan 3164169699Skan if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init))) 3165169699Skan return; 3166169699Skan 3167169699Skan elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value; 3168169699Skan type = TREE_TYPE (elt); 3169169699Skan if (TREE_CODE (type) == ARRAY_TYPE 3170169699Skan && TYPE_SIZE (type) == NULL_TREE 3171169699Skan && TYPE_DOMAIN (type) != NULL_TREE 3172169699Skan && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE) 3173169699Skan { 3174169699Skan complete_array_type (&type, elt, false); 3175169699Skan DECL_SIZE (decl) 3176169699Skan = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type)); 3177169699Skan DECL_SIZE_UNIT (decl) 3178169699Skan = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type)); 3179169699Skan } 3180169699Skan} 318190075Sobrien 318218334Speter/* Decode a "typename", such as "int **", returning a ..._TYPE node. */ 318318334Speter 318418334Spetertree 3185169699Skangroktypename (struct c_type_name *type_name) 318618334Speter{ 3187169699Skan tree type; 3188169699Skan tree attrs = type_name->specs->attrs; 318990075Sobrien 3190169699Skan type_name->specs->attrs = NULL_TREE; 319190075Sobrien 3192169699Skan type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME, 3193169699Skan false, NULL); 319490075Sobrien 319590075Sobrien /* Apply attributes. */ 3196169699Skan decl_attributes (&type, attrs, 0); 319790075Sobrien 3198169699Skan return type; 319918334Speter} 320018334Speter 320118334Speter/* Decode a declarator in an ordinary declaration or data definition. 320218334Speter This is called as soon as the type information and variable name 320318334Speter have been parsed, before parsing the initializer if any. 320418334Speter Here we create the ..._DECL node, fill in its type, 320518334Speter and put it on the list of decls for the current context. 320618334Speter The ..._DECL node is returned as the value. 320718334Speter 320818334Speter Exception: for arrays where the length is not specified, 320918334Speter the type is left null, to be filled in by `finish_decl'. 321018334Speter 321118334Speter Function definitions do not come here; they go to start_function 321218334Speter instead. However, external and forward declarations of functions 321318334Speter do go through here. Structure field declarations are done by 321418334Speter grokfield and not through here. */ 321518334Speter 321618334Spetertree 3217169699Skanstart_decl (struct c_declarator *declarator, struct c_declspecs *declspecs, 3218169699Skan bool initialized, tree attributes) 321918334Speter{ 322090075Sobrien tree decl; 322190075Sobrien tree tem; 3222132730Skan 322390075Sobrien /* An object declared as __attribute__((deprecated)) suppresses 322490075Sobrien warnings of uses of other deprecated items. */ 322590075Sobrien if (lookup_attribute ("deprecated", attributes)) 322690075Sobrien deprecated_state = DEPRECATED_SUPPRESS; 322718334Speter 322890075Sobrien decl = grokdeclarator (declarator, declspecs, 3229132730Skan NORMAL, initialized, NULL); 3230169699Skan if (!decl) 3231169699Skan return 0; 3232132730Skan 323390075Sobrien deprecated_state = DEPRECATED_NORMAL; 323418334Speter 323590075Sobrien if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL 323690075Sobrien && MAIN_NAME_P (DECL_NAME (decl))) 3237169699Skan warning (OPT_Wmain, "%q+D is usually a function", decl); 323850397Sobrien 323918334Speter if (initialized) 324018334Speter /* Is it valid for this decl to have an initializer at all? 324118334Speter If not, set INITIALIZED to zero, which will indirectly 3242169699Skan tell 'finish_decl' to ignore the initializer once it is parsed. */ 324318334Speter switch (TREE_CODE (decl)) 324418334Speter { 324518334Speter case TYPE_DECL: 3246169699Skan error ("typedef %qD is initialized (use __typeof__ instead)", decl); 3247104761Skan initialized = 0; 324818334Speter break; 324918334Speter 325018334Speter case FUNCTION_DECL: 3251169699Skan error ("function %qD is initialized like a variable", decl); 325218334Speter initialized = 0; 325318334Speter break; 325418334Speter 325518334Speter case PARM_DECL: 325618334Speter /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */ 3257169699Skan error ("parameter %qD is initialized", decl); 325818334Speter initialized = 0; 325918334Speter break; 326018334Speter 326118334Speter default: 3262169699Skan /* Don't allow initializations for incomplete types except for 3263169699Skan arrays which might be completed by the initialization. */ 326490075Sobrien 3265169699Skan /* This can happen if the array size is an undefined macro. 3266169699Skan We already gave a warning, so we don't need another one. */ 326790075Sobrien if (TREE_TYPE (decl) == error_mark_node) 326890075Sobrien initialized = 0; 326990075Sobrien else if (COMPLETE_TYPE_P (TREE_TYPE (decl))) 327018334Speter { 327118334Speter /* A complete type is ok if size is fixed. */ 327218334Speter 327318334Speter if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST 327418334Speter || C_DECL_VARIABLE_SIZE (decl)) 327518334Speter { 327618334Speter error ("variable-sized object may not be initialized"); 327718334Speter initialized = 0; 327818334Speter } 327918334Speter } 328018334Speter else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE) 328118334Speter { 3282169699Skan error ("variable %qD has initializer but incomplete type", decl); 328318334Speter initialized = 0; 328418334Speter } 3285169699Skan else if (C_DECL_VARIABLE_SIZE (decl)) 328618334Speter { 3287169699Skan /* Although C99 is unclear about whether incomplete arrays 3288169699Skan of VLAs themselves count as VLAs, it does not make 3289169699Skan sense to permit them to be initialized given that 3290169699Skan ordinary VLAs may not be initialized. */ 3291169699Skan error ("variable-sized object may not be initialized"); 329218334Speter initialized = 0; 329318334Speter } 329418334Speter } 329518334Speter 329618334Speter if (initialized) 329718334Speter { 3298169699Skan if (current_scope == file_scope) 329918334Speter TREE_STATIC (decl) = 1; 330018334Speter 3301169699Skan /* Tell 'pushdecl' this is an initialized decl 330218334Speter even though we don't yet have the initializer expression. 3303169699Skan Also tell 'finish_decl' it may store the real initializer. */ 330418334Speter DECL_INITIAL (decl) = error_mark_node; 330518334Speter } 330618334Speter 330718334Speter /* If this is a function declaration, write a record describing it to the 330818334Speter prototypes file (if requested). */ 330918334Speter 331018334Speter if (TREE_CODE (decl) == FUNCTION_DECL) 331118334Speter gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0); 331218334Speter 331350397Sobrien /* ANSI specifies that a tentative definition which is not merged with 331450397Sobrien a non-tentative definition behaves exactly like a definition with an 331550397Sobrien initializer equal to zero. (Section 3.7.2) 3316117421Skan 3317117421Skan -fno-common gives strict ANSI behavior, though this tends to break 3318117421Skan a large body of code that grew up without this rule. 3319117421Skan 3320117421Skan Thread-local variables are never common, since there's no entrenched 3321117421Skan body of code to break, and it allows more efficient variable references 3322132730Skan in the presence of dynamic linking. */ 3323117421Skan 3324117421Skan if (TREE_CODE (decl) == VAR_DECL 3325117421Skan && !initialized 3326117421Skan && TREE_PUBLIC (decl) 3327169699Skan && !DECL_THREAD_LOCAL_P (decl) 3328117421Skan && !flag_no_common) 332950397Sobrien DECL_COMMON (decl) = 1; 333018334Speter 333118334Speter /* Set attributes here so if duplicate decl, will have proper attributes. */ 333290075Sobrien decl_attributes (&decl, attributes, 0); 333318334Speter 3334189824Sdas /* Handle gnu_inline attribute. */ 3335189824Sdas if (declspecs->inline_p 3336189824Sdas && !flag_gnu89_inline 3337189824Sdas && TREE_CODE (decl) == FUNCTION_DECL 3338189824Sdas && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))) 3339189824Sdas { 3340189824Sdas if (declspecs->storage_class == csc_auto && current_scope != file_scope) 3341189824Sdas ; 3342189824Sdas else if (declspecs->storage_class != csc_static) 3343189824Sdas DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl); 3344189824Sdas } 3345189824Sdas 3346132730Skan if (TREE_CODE (decl) == FUNCTION_DECL 3347132730Skan && targetm.calls.promote_prototypes (TREE_TYPE (decl))) 3348132730Skan { 3349169699Skan struct c_declarator *ce = declarator; 335096263Sobrien 3351169699Skan if (ce->kind == cdk_pointer) 3352169699Skan ce = declarator->declarator; 3353169699Skan if (ce->kind == cdk_function) 3354132730Skan { 3355169699Skan tree args = ce->u.arg_info->parms; 3356132730Skan for (; args; args = TREE_CHAIN (args)) 3357132730Skan { 3358132730Skan tree type = TREE_TYPE (args); 3359169699Skan if (type && INTEGRAL_TYPE_P (type) 3360132730Skan && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 3361132730Skan DECL_ARG_TYPE (args) = integer_type_node; 3362132730Skan } 3363132730Skan } 3364132730Skan } 3365132730Skan 336690075Sobrien if (TREE_CODE (decl) == FUNCTION_DECL 336790075Sobrien && DECL_DECLARED_INLINE_P (decl) 336890075Sobrien && DECL_UNINLINABLE (decl) 336990075Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) 3370169699Skan warning (OPT_Wattributes, "inline function %q+D given attribute noinline", 3371169699Skan decl); 337290075Sobrien 3373189824Sdas /* C99 6.7.4p3: An inline definition of a function with external 3374189824Sdas linkage shall not contain a definition of a modifiable object 3375189824Sdas with static storage duration... */ 3376189824Sdas if (TREE_CODE (decl) == VAR_DECL 3377189824Sdas && current_scope != file_scope 3378189824Sdas && TREE_STATIC (decl) 3379189824Sdas && !TREE_READONLY (decl) 3380189824Sdas && DECL_DECLARED_INLINE_P (current_function_decl) 3381189824Sdas && DECL_EXTERNAL (current_function_decl)) 3382189824Sdas pedwarn ("%q+D is static but declared in inline function %qD " 3383189824Sdas "which is not static", decl, current_function_decl); 3384189824Sdas 3385132730Skan /* Add this decl to the current scope. 338618334Speter TEM may equal DECL or it may be a previous decl of the same name. */ 338718334Speter tem = pushdecl (decl); 338818334Speter 3389169699Skan if (initialized && DECL_EXTERNAL (tem)) 339018334Speter { 3391169699Skan DECL_EXTERNAL (tem) = 0; 3392169699Skan TREE_STATIC (tem) = 1; 339318334Speter } 339418334Speter 339518334Speter return tem; 339618334Speter} 339718334Speter 3398169699Skan/* Initialize EH if not initialized yet and exceptions are enabled. */ 3399169699Skan 3400169699Skanvoid 3401169699Skanc_maybe_initialize_eh (void) 3402169699Skan{ 3403169699Skan if (!flag_exceptions || c_eh_initialized_p) 3404169699Skan return; 3405169699Skan 3406169699Skan c_eh_initialized_p = true; 3407169699Skan eh_personality_libfunc 3408169699Skan = init_one_libfunc (USING_SJLJ_EXCEPTIONS 3409169699Skan ? "__gcc_personality_sj0" 3410169699Skan : "__gcc_personality_v0"); 3411169699Skan default_init_unwind_resume_libfunc (); 3412169699Skan using_eh_for_cleanups (); 3413169699Skan} 3414169699Skan 341518334Speter/* Finish processing of a declaration; 341618334Speter install its initial value. 341718334Speter If the length of an array type is not known before, 341818334Speter it must be determined now, from the initial value, or it is an error. */ 341918334Speter 342018334Spetervoid 3421132730Skanfinish_decl (tree decl, tree init, tree asmspec_tree) 342218334Speter{ 3423169699Skan tree type; 342418334Speter int was_incomplete = (DECL_SIZE (decl) == 0); 342590075Sobrien const char *asmspec = 0; 342618334Speter 342790075Sobrien /* If a name was specified, get the string. */ 3428169699Skan if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL) 3429169699Skan && DECL_FILE_SCOPE_P (decl)) 343096263Sobrien asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); 343118334Speter if (asmspec_tree) 343218334Speter asmspec = TREE_STRING_POINTER (asmspec_tree); 343318334Speter 343418334Speter /* If `start_decl' didn't like having an initialization, ignore it now. */ 343518334Speter if (init != 0 && DECL_INITIAL (decl) == 0) 343618334Speter init = 0; 3437132730Skan 343818334Speter /* Don't crash if parm is initialized. */ 343918334Speter if (TREE_CODE (decl) == PARM_DECL) 344018334Speter init = 0; 344118334Speter 344218334Speter if (init) 3443104761Skan store_init_value (decl, init); 344418334Speter 3445132730Skan if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL 3446169699Skan || TREE_CODE (decl) == FUNCTION_DECL 3447169699Skan || TREE_CODE (decl) == FIELD_DECL)) 3448132730Skan objc_check_decl (decl); 3449132730Skan 3450169699Skan type = TREE_TYPE (decl); 3451169699Skan 3452132730Skan /* Deduce size of array from initialization, if not already known. */ 345318334Speter if (TREE_CODE (type) == ARRAY_TYPE 345418334Speter && TYPE_DOMAIN (type) == 0 345518334Speter && TREE_CODE (decl) != TYPE_DECL) 345618334Speter { 3457169699Skan bool do_default 345818334Speter = (TREE_STATIC (decl) 345918334Speter /* Even if pedantic, an external linkage array 346018334Speter may have incomplete type at first. */ 346118334Speter ? pedantic && !TREE_PUBLIC (decl) 346218334Speter : !DECL_EXTERNAL (decl)); 346318334Speter int failure 3464169699Skan = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl), 3465169699Skan do_default); 346618334Speter 346718334Speter /* Get the completed type made by complete_array_type. */ 346818334Speter type = TREE_TYPE (decl); 346918334Speter 3470169699Skan switch (failure) 3471169699Skan { 3472169699Skan case 1: 3473169699Skan error ("initializer fails to determine size of %q+D", decl); 3474169699Skan break; 347518334Speter 3476169699Skan case 2: 347718334Speter if (do_default) 3478169699Skan error ("array size missing in %q+D", decl); 347918334Speter /* If a `static' var's size isn't known, 348018334Speter make it extern as well as static, so it does not get 348118334Speter allocated. 348218334Speter If it is not `static', then do not mark extern; 348318334Speter finish_incomplete_decl will give it a default size 348418334Speter and it will get allocated. */ 3485169699Skan else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) 348618334Speter DECL_EXTERNAL (decl) = 1; 3487169699Skan break; 3488169699Skan 3489169699Skan case 3: 3490169699Skan error ("zero or negative size array %q+D", decl); 3491169699Skan break; 3492169699Skan 3493169699Skan case 0: 3494169699Skan /* For global variables, update the copy of the type that 3495169699Skan exists in the binding. */ 3496169699Skan if (TREE_PUBLIC (decl)) 3497169699Skan { 3498169699Skan struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl)); 3499169699Skan while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext)) 3500169699Skan b_ext = b_ext->shadowed; 3501169699Skan if (b_ext) 3502169699Skan { 3503169699Skan if (b_ext->type) 3504169699Skan b_ext->type = composite_type (b_ext->type, type); 3505169699Skan else 3506169699Skan b_ext->type = type; 3507169699Skan } 3508169699Skan } 3509169699Skan break; 3510169699Skan 3511169699Skan default: 3512169699Skan gcc_unreachable (); 351318334Speter } 351418334Speter 3515169699Skan if (DECL_INITIAL (decl)) 3516169699Skan TREE_TYPE (DECL_INITIAL (decl)) = type; 351718334Speter 351818334Speter layout_decl (decl, 0); 351918334Speter } 352018334Speter 352118334Speter if (TREE_CODE (decl) == VAR_DECL) 352218334Speter { 3523169699Skan if (init && TREE_CODE (init) == CONSTRUCTOR) 3524169699Skan add_flexible_array_elts_to_size (decl, init); 3525169699Skan 352690075Sobrien if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node 352790075Sobrien && COMPLETE_TYPE_P (TREE_TYPE (decl))) 352818334Speter layout_decl (decl, 0); 352918334Speter 353018334Speter if (DECL_SIZE (decl) == 0 353190075Sobrien /* Don't give an error if we already gave one earlier. */ 353290075Sobrien && TREE_TYPE (decl) != error_mark_node 353318334Speter && (TREE_STATIC (decl) 3534169699Skan /* A static variable with an incomplete type 3535169699Skan is an error if it is initialized. 3536169699Skan Also if it is not file scope. 3537169699Skan Otherwise, let it through, but if it is not `extern' 3538169699Skan then it may cause an error message later. */ 3539169699Skan ? (DECL_INITIAL (decl) != 0 3540132730Skan || !DECL_FILE_SCOPE_P (decl)) 3541169699Skan /* An automatic variable with an incomplete type 3542169699Skan is an error. */ 3543169699Skan : !DECL_EXTERNAL (decl))) 3544169699Skan { 3545169699Skan error ("storage size of %q+D isn%'t known", decl); 3546169699Skan TREE_TYPE (decl) = error_mark_node; 3547169699Skan } 354818334Speter 354918334Speter if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 355018334Speter && DECL_SIZE (decl) != 0) 355118334Speter { 355218334Speter if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) 355318334Speter constant_expression_warning (DECL_SIZE (decl)); 355418334Speter else 3555169699Skan error ("storage size of %q+D isn%'t constant", decl); 355618334Speter } 355750397Sobrien 355890075Sobrien if (TREE_USED (type)) 355950397Sobrien TREE_USED (decl) = 1; 356018334Speter } 356118334Speter 3562132730Skan /* If this is a function and an assembler name is specified, reset DECL_RTL 3563132730Skan so we can give it its new name. Also, update built_in_decls if it 3564132730Skan was a normal built-in. */ 356518334Speter if (TREE_CODE (decl) == FUNCTION_DECL && asmspec) 356690075Sobrien { 3567132730Skan if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) 3568169699Skan set_builtin_user_assembler_name (decl, asmspec); 3569169699Skan set_user_assembler_name (decl, asmspec); 357090075Sobrien } 357118334Speter 3572132730Skan /* If #pragma weak was used, mark the decl weak now. */ 3573169699Skan maybe_apply_pragma_weak (decl); 3574132730Skan 357518334Speter /* Output the assembler code and/or RTL code for variables and functions, 357618334Speter unless the type is an undefined structure or union. 357718334Speter If not, it will get done when the type is completed. */ 357818334Speter 357918334Speter if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL) 358018334Speter { 3581169699Skan /* Determine the ELF visibility. */ 3582169699Skan if (TREE_PUBLIC (decl)) 3583169699Skan c_determine_visibility (decl); 3584169699Skan 3585117421Skan /* This is a no-op in c-lang.c or something real in objc-act.c. */ 3586132730Skan if (c_dialect_objc ()) 3587117421Skan objc_check_decl (decl); 358890075Sobrien 3589169699Skan if (asmspec) 3590169699Skan { 3591169699Skan /* If this is not a static variable, issue a warning. 3592169699Skan It doesn't make any sense to give an ASMSPEC for an 3593169699Skan ordinary, non-register local variable. Historically, 3594169699Skan GCC has accepted -- but ignored -- the ASMSPEC in 3595169699Skan this case. */ 3596169699Skan if (!DECL_FILE_SCOPE_P (decl) 3597169699Skan && TREE_CODE (decl) == VAR_DECL 3598169699Skan && !C_DECL_REGISTER (decl) 3599169699Skan && !TREE_STATIC (decl)) 3600169699Skan warning (0, "ignoring asm-specifier for non-static local " 3601169699Skan "variable %q+D", decl); 3602169699Skan else 3603169699Skan set_user_assembler_name (decl, asmspec); 3604169699Skan } 3605169699Skan 3606132730Skan if (DECL_FILE_SCOPE_P (decl)) 360718334Speter { 360890075Sobrien if (DECL_INITIAL (decl) == NULL_TREE 360990075Sobrien || DECL_INITIAL (decl) == error_mark_node) 361090075Sobrien /* Don't output anything 361190075Sobrien when a tentative file-scope definition is seen. 361290075Sobrien But at end of compilation, do output code for them. */ 361390075Sobrien DECL_DEFER_OUTPUT (decl) = 1; 3614169699Skan rest_of_decl_compilation (decl, true, 0); 361518334Speter } 361618334Speter else 361718334Speter { 3618169699Skan /* In conjunction with an ASMSPEC, the `register' 3619169699Skan keyword indicates that we should place the variable 3620169699Skan in a particular register. */ 3621169699Skan if (asmspec && C_DECL_REGISTER (decl)) 362290075Sobrien { 3623169699Skan DECL_HARD_REGISTER (decl) = 1; 3624169699Skan /* This cannot be done for a structure with volatile 3625169699Skan fields, on which DECL_REGISTER will have been 3626169699Skan reset. */ 3627169699Skan if (!DECL_REGISTER (decl)) 3628169699Skan error ("cannot put object with volatile field into register"); 362990075Sobrien } 363090075Sobrien 363190075Sobrien if (TREE_CODE (decl) != FUNCTION_DECL) 3632169699Skan { 3633169699Skan /* If we're building a variable sized type, and we might be 3634169699Skan reachable other than via the top of the current binding 3635169699Skan level, then create a new BIND_EXPR so that we deallocate 3636169699Skan the object at the right time. */ 3637169699Skan /* Note that DECL_SIZE can be null due to errors. */ 3638169699Skan if (DECL_SIZE (decl) 3639169699Skan && !TREE_CONSTANT (DECL_SIZE (decl)) 3640169699Skan && STATEMENT_LIST_HAS_LABEL (cur_stmt_list)) 3641169699Skan { 3642169699Skan tree bind; 3643169699Skan bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL); 3644169699Skan TREE_SIDE_EFFECTS (bind) = 1; 3645169699Skan add_stmt (bind); 3646169699Skan BIND_EXPR_BODY (bind) = push_stmt_list (); 3647169699Skan } 3648169699Skan add_stmt (build_stmt (DECL_EXPR, decl)); 3649169699Skan } 365018334Speter } 365190075Sobrien 3652169699Skan 3653132730Skan if (!DECL_FILE_SCOPE_P (decl)) 365418334Speter { 365518334Speter /* Recompute the RTL of a local array now 365618334Speter if it used to be an incomplete type. */ 365718334Speter if (was_incomplete 3658169699Skan && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) 365918334Speter { 366018334Speter /* If we used it already as memory, it must stay in memory. */ 366118334Speter TREE_ADDRESSABLE (decl) = TREE_USED (decl); 366218334Speter /* If it's still incomplete now, no init will save it. */ 366318334Speter if (DECL_SIZE (decl) == 0) 366418334Speter DECL_INITIAL (decl) = 0; 366518334Speter } 366618334Speter } 366718334Speter } 366818334Speter 3669132730Skan /* If this was marked 'used', be sure it will be output. */ 3670169699Skan if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl))) 3671169699Skan mark_decl_referenced (decl); 3672132730Skan 367318334Speter if (TREE_CODE (decl) == TYPE_DECL) 3674169699Skan { 3675169699Skan if (!DECL_FILE_SCOPE_P (decl) 3676169699Skan && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE)) 3677169699Skan add_stmt (build_stmt (DECL_EXPR, decl)); 367818334Speter 3679169699Skan rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0); 3680169699Skan } 3681169699Skan 368218334Speter /* At the end of a declaration, throw away any variable type sizes 368318334Speter of types defined inside that declaration. There is no use 368418334Speter computing them in the following function definition. */ 3685169699Skan if (current_scope == file_scope) 368618334Speter get_pending_sizes (); 368718334Speter 3688117421Skan /* Install a cleanup (aka destructor) if one was given. */ 3689117421Skan if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl)) 3690117421Skan { 3691117421Skan tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl)); 3692117421Skan if (attr) 3693117421Skan { 3694117421Skan tree cleanup_id = TREE_VALUE (TREE_VALUE (attr)); 3695117421Skan tree cleanup_decl = lookup_name (cleanup_id); 3696117421Skan tree cleanup; 3697117421Skan 3698117421Skan /* Build "cleanup(&decl)" for the destructor. */ 3699117421Skan cleanup = build_unary_op (ADDR_EXPR, decl, 0); 3700117421Skan cleanup = build_tree_list (NULL_TREE, cleanup); 3701117421Skan cleanup = build_function_call (cleanup_decl, cleanup); 3702117421Skan 3703117421Skan /* Don't warn about decl unused; the cleanup uses it. */ 3704117421Skan TREE_USED (decl) = 1; 3705169699Skan TREE_USED (cleanup_decl) = 1; 3706117421Skan 3707117421Skan /* Initialize EH, if we've been told to do so. */ 3708169699Skan c_maybe_initialize_eh (); 3709117421Skan 3710169699Skan push_cleanup (decl, cleanup, false); 3711117421Skan } 3712117421Skan } 371318334Speter} 371418334Speter 3715169699Skan/* Given a parsed parameter declaration, decode it into a PARM_DECL. */ 3716169699Skan 3717169699Skantree 3718169699Skangrokparm (const struct c_parm *parm) 3719169699Skan{ 3720169699Skan tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, 3721169699Skan NULL); 3722169699Skan 3723169699Skan decl_attributes (&decl, parm->attrs, 0); 3724169699Skan 3725169699Skan return decl; 3726169699Skan} 3727169699Skan 3728132730Skan/* Given a parsed parameter declaration, decode it into a PARM_DECL 3729132730Skan and push that on the current scope. */ 373018334Speter 373118334Spetervoid 3732169699Skanpush_parm_decl (const struct c_parm *parm) 373318334Speter{ 373418334Speter tree decl; 3735132730Skan 3736169699Skan decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL); 3737169699Skan decl_attributes (&decl, parm->attrs, 0); 373818334Speter 373918334Speter decl = pushdecl (decl); 374018334Speter 3741132730Skan finish_decl (decl, NULL_TREE, NULL_TREE); 374218334Speter} 374318334Speter 3744169699Skan/* Mark all the parameter declarations to date as forward decls. 3745132730Skan Also diagnose use of this extension. */ 374618334Speter 374718334Spetervoid 3748132730Skanmark_forward_parm_decls (void) 374918334Speter{ 3750169699Skan struct c_binding *b; 3751132730Skan 3752132730Skan if (pedantic && !current_scope->warned_forward_parm_decls) 3753132730Skan { 3754132730Skan pedwarn ("ISO C forbids forward parameter declarations"); 3755132730Skan current_scope->warned_forward_parm_decls = true; 3756132730Skan } 3757132730Skan 3758169699Skan for (b = current_scope->bindings; b; b = b->prev) 3759169699Skan if (TREE_CODE (b->decl) == PARM_DECL) 3760169699Skan TREE_ASM_WRITTEN (b->decl) = 1; 376118334Speter} 376218334Speter 376390075Sobrien/* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound 376490075Sobrien literal, which may be an incomplete array type completed by the 376590075Sobrien initializer; INIT is a CONSTRUCTOR that initializes the compound 376690075Sobrien literal. */ 376790075Sobrien 376890075Sobrientree 3769132730Skanbuild_compound_literal (tree type, tree init) 377090075Sobrien{ 377190075Sobrien /* We do not use start_decl here because we have a type, not a declarator; 377290075Sobrien and do not use finish_decl because the decl should be stored inside 3773169699Skan the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */ 3774169699Skan tree decl; 377590075Sobrien tree complit; 377690075Sobrien tree stmt; 3777169699Skan 3778169699Skan if (type == error_mark_node) 3779169699Skan return error_mark_node; 3780169699Skan 3781169699Skan decl = build_decl (VAR_DECL, NULL_TREE, type); 378290075Sobrien DECL_EXTERNAL (decl) = 0; 378390075Sobrien TREE_PUBLIC (decl) = 0; 3784169699Skan TREE_STATIC (decl) = (current_scope == file_scope); 378590075Sobrien DECL_CONTEXT (decl) = current_function_decl; 378690075Sobrien TREE_USED (decl) = 1; 378790075Sobrien TREE_TYPE (decl) = type; 3788169699Skan TREE_READONLY (decl) = TYPE_READONLY (type); 378990075Sobrien store_init_value (decl, init); 379090075Sobrien 379190075Sobrien if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type)) 379290075Sobrien { 3793169699Skan int failure = complete_array_type (&TREE_TYPE (decl), 3794169699Skan DECL_INITIAL (decl), true); 3795169699Skan gcc_assert (!failure); 3796169699Skan 3797169699Skan type = TREE_TYPE (decl); 3798169699Skan TREE_TYPE (DECL_INITIAL (decl)) = type; 379990075Sobrien } 380090075Sobrien 380190075Sobrien if (type == error_mark_node || !COMPLETE_TYPE_P (type)) 380290075Sobrien return error_mark_node; 380390075Sobrien 3804169699Skan stmt = build_stmt (DECL_EXPR, decl); 3805169699Skan complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt); 380690075Sobrien TREE_SIDE_EFFECTS (complit) = 1; 380790075Sobrien 380890075Sobrien layout_decl (decl, 0); 380990075Sobrien 381090075Sobrien if (TREE_STATIC (decl)) 381190075Sobrien { 3812169699Skan /* This decl needs a name for the assembler output. */ 3813169699Skan set_compound_literal_name (decl); 3814102790Skan DECL_DEFER_OUTPUT (decl) = 1; 3815102790Skan DECL_COMDAT (decl) = 1; 3816102790Skan DECL_ARTIFICIAL (decl) = 1; 3817169699Skan DECL_IGNORED_P (decl) = 1; 3818102790Skan pushdecl (decl); 3819169699Skan rest_of_decl_compilation (decl, 1, 0); 382090075Sobrien } 382190075Sobrien 382290075Sobrien return complit; 382390075Sobrien} 382490075Sobrien 3825117421Skan/* Determine whether TYPE is a structure with a flexible array member, 3826117421Skan or a union containing such a structure (possibly recursively). */ 3827117421Skan 3828117421Skanstatic bool 3829132730Skanflexible_array_type_p (tree type) 3830117421Skan{ 3831117421Skan tree x; 3832117421Skan switch (TREE_CODE (type)) 3833117421Skan { 3834117421Skan case RECORD_TYPE: 3835117421Skan x = TYPE_FIELDS (type); 3836117421Skan if (x == NULL_TREE) 3837117421Skan return false; 3838117421Skan while (TREE_CHAIN (x) != NULL_TREE) 3839117421Skan x = TREE_CHAIN (x); 3840117421Skan if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 3841117421Skan && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 3842117421Skan && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 3843117421Skan && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 3844117421Skan return true; 3845117421Skan return false; 3846117421Skan case UNION_TYPE: 3847117421Skan for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x)) 3848117421Skan { 3849117421Skan if (flexible_array_type_p (TREE_TYPE (x))) 3850117421Skan return true; 3851117421Skan } 3852117421Skan return false; 3853117421Skan default: 3854117421Skan return false; 3855117421Skan } 3856117421Skan} 3857117421Skan 3858132730Skan/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME, 3859132730Skan replacing with appropriate values if they are invalid. */ 3860132730Skanstatic void 3861132730Skancheck_bitfield_type_and_width (tree *type, tree *width, const char *orig_name) 3862132730Skan{ 3863132730Skan tree type_mv; 3864132730Skan unsigned int max_width; 3865132730Skan unsigned HOST_WIDE_INT w; 3866132730Skan const char *name = orig_name ? orig_name: _("<anonymous>"); 3867132730Skan 3868132730Skan /* Detect and ignore out of range field width and process valid 3869132730Skan field widths. */ 3870169699Skan if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)) 3871169699Skan || TREE_CODE (*width) != INTEGER_CST) 3872132730Skan { 3873169699Skan error ("bit-field %qs width not an integer constant", name); 3874132730Skan *width = integer_one_node; 3875132730Skan } 3876132730Skan else 3877132730Skan { 3878132730Skan constant_expression_warning (*width); 3879132730Skan if (tree_int_cst_sgn (*width) < 0) 3880132730Skan { 3881169699Skan error ("negative width in bit-field %qs", name); 3882132730Skan *width = integer_one_node; 3883132730Skan } 3884132730Skan else if (integer_zerop (*width) && orig_name) 3885132730Skan { 3886169699Skan error ("zero width for bit-field %qs", name); 3887132730Skan *width = integer_one_node; 3888132730Skan } 3889132730Skan } 3890132730Skan 3891132730Skan /* Detect invalid bit-field type. */ 3892132730Skan if (TREE_CODE (*type) != INTEGER_TYPE 3893132730Skan && TREE_CODE (*type) != BOOLEAN_TYPE 3894132730Skan && TREE_CODE (*type) != ENUMERAL_TYPE) 3895132730Skan { 3896169699Skan error ("bit-field %qs has invalid type", name); 3897132730Skan *type = unsigned_type_node; 3898132730Skan } 3899132730Skan 3900132730Skan type_mv = TYPE_MAIN_VARIANT (*type); 3901132730Skan if (pedantic 3902169699Skan && !in_system_header 3903132730Skan && type_mv != integer_type_node 3904132730Skan && type_mv != unsigned_type_node 3905132730Skan && type_mv != boolean_type_node) 3906169699Skan pedwarn ("type of bit-field %qs is a GCC extension", name); 3907132730Skan 3908132730Skan if (type_mv == boolean_type_node) 3909132730Skan max_width = CHAR_TYPE_SIZE; 3910132730Skan else 3911132730Skan max_width = TYPE_PRECISION (*type); 3912132730Skan 3913132730Skan if (0 < compare_tree_int (*width, max_width)) 3914132730Skan { 3915169699Skan error ("width of %qs exceeds its type", name); 3916132730Skan w = max_width; 3917169699Skan *width = build_int_cst (NULL_TREE, w); 3918132730Skan } 3919132730Skan else 3920132730Skan w = tree_low_cst (*width, 1); 3921132730Skan 3922169699Skan if (TREE_CODE (*type) == ENUMERAL_TYPE) 3923169699Skan { 3924169699Skan struct lang_type *lt = TYPE_LANG_SPECIFIC (*type); 3925169699Skan if (!lt 3926169699Skan || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type)) 3927169699Skan || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type))) 3928169699Skan warning (0, "%qs is narrower than values of its type", name); 3929169699Skan } 3930132730Skan} 3931169699Skan 3932132730Skan 3933259269Spfg 3934259269Spfg/* Print warning about variable length array if necessary. */ 3935259269Spfg 3936259269Spfgstatic void 3937259269Spfgwarn_variable_length_array (const char *name, tree size) 3938259269Spfg{ 3939259269Spfg int ped = !flag_isoc99 && pedantic && warn_vla != 0; 3940259269Spfg int const_size = TREE_CONSTANT (size); 3941259269Spfg 3942259269Spfg if (ped) 3943259269Spfg { 3944259269Spfg if (const_size) 3945259269Spfg { 3946259269Spfg if (name) 3947259269Spfg pedwarn ("ISO C90 forbids array %qs whose size " 3948259269Spfg "can%'t be evaluated", 3949259269Spfg name); 3950259269Spfg else 3951259269Spfg pedwarn ("ISO C90 forbids array whose size " 3952259269Spfg "can%'t be evaluated"); 3953259269Spfg } 3954259269Spfg else 3955259269Spfg { 3956259269Spfg if (name) 3957259269Spfg pedwarn ("ISO C90 forbids variable length array %qs", 3958259269Spfg name); 3959259269Spfg else 3960259269Spfg pedwarn ("ISO C90 forbids variable length array"); 3961259269Spfg } 3962259269Spfg } 3963259269Spfg else if (warn_vla > 0) 3964259269Spfg { 3965259269Spfg if (const_size) 3966259269Spfg { 3967259269Spfg if (name) 3968259269Spfg warning (OPT_Wvla, 3969259269Spfg "the size of array %qs can" 3970259269Spfg "%'t be evaluated", name); 3971259269Spfg else 3972259269Spfg warning (OPT_Wvla, 3973259269Spfg "the size of array can %'t be evaluated"); 3974259269Spfg } 3975259269Spfg else 3976259269Spfg { 3977259269Spfg if (name) 3978259269Spfg warning (OPT_Wvla, 3979259269Spfg "variable length array %qs is used", 3980259269Spfg name); 3981259269Spfg else 3982259269Spfg warning (OPT_Wvla, 3983259269Spfg "variable length array is used"); 3984259269Spfg } 3985259269Spfg } 3986259269Spfg} 3987259269Spfg 398818334Speter/* Given declspecs and a declarator, 398918334Speter determine the name and type of the object declared 399018334Speter and construct a ..._DECL node for it. 399118334Speter (In one case we can return a ..._TYPE node instead. 399218334Speter For invalid input we sometimes return 0.) 399318334Speter 3994169699Skan DECLSPECS is a c_declspecs structure for the declaration specifiers. 399518334Speter 399618334Speter DECL_CONTEXT says which syntactic context this declaration is in: 399718334Speter NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. 399818334Speter FUNCDEF for a function definition. Like NORMAL but a few different 399918334Speter error messages in each case. Return value may be zero meaning 400018334Speter this definition is too screwy to try to parse. 400118334Speter PARM for a parameter declaration (either within a function prototype 400218334Speter or before a function body). Make a PARM_DECL, or return void_type_node. 400318334Speter TYPENAME if for a typename (in a cast or sizeof). 400418334Speter Don't make a DECL node; just return the ..._TYPE node. 400518334Speter FIELD for a struct or union field; make a FIELD_DECL. 4006169699Skan INITIALIZED is true if the decl has an initializer. 4007132730Skan WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node 4008132730Skan representing the width of the bit-field. 400918334Speter 401018334Speter In the TYPENAME case, DECLARATOR is really an absolute declarator. 401118334Speter It may also be so in the PARM case, for a prototype where the 401218334Speter argument type is specified but not the name. 401318334Speter 401418334Speter This function is where the complicated C meanings of `static' 401518334Speter and `extern' are interpreted. */ 401618334Speter 401718334Speterstatic tree 4018169699Skangrokdeclarator (const struct c_declarator *declarator, 4019169699Skan struct c_declspecs *declspecs, 4020169699Skan enum decl_context decl_context, bool initialized, tree *width) 402118334Speter{ 4022169699Skan tree type = declspecs->type; 4023169699Skan bool threadp = declspecs->thread_p; 4024169699Skan enum c_storage_class storage_class = declspecs->storage_class; 402518334Speter int constp; 402652284Sobrien int restrictp; 402718334Speter int volatilep; 402852284Sobrien int type_quals = TYPE_UNQUALIFIED; 4029132730Skan const char *name, *orig_name; 403018334Speter tree typedef_type = 0; 4031169699Skan bool funcdef_flag = false; 4032169699Skan bool funcdef_syntax = false; 403318334Speter int size_varies = 0; 4034169699Skan tree decl_attr = declspecs->decl_attr; 4035169699Skan int array_ptr_quals = TYPE_UNQUALIFIED; 4036169699Skan tree array_ptr_attrs = NULL_TREE; 403790075Sobrien int array_parm_static = 0; 4038169699Skan bool array_parm_vla_unspec_p = false; 403990075Sobrien tree returned_attrs = NULL_TREE; 4040132730Skan bool bitfield = width != NULL; 4041132730Skan tree element_type; 4042169699Skan struct c_arg_info *arg_info = 0; 404318334Speter 404418334Speter if (decl_context == FUNCDEF) 4045169699Skan funcdef_flag = true, decl_context = NORMAL; 404618334Speter 404718334Speter /* Look inside a declarator for the name being declared 404818334Speter and get it as a string, for an error message. */ 404918334Speter { 4050169699Skan const struct c_declarator *decl = declarator; 405118334Speter name = 0; 405218334Speter 405318334Speter while (decl) 4054169699Skan switch (decl->kind) 405518334Speter { 4056169699Skan case cdk_function: 4057169699Skan case cdk_array: 4058169699Skan case cdk_pointer: 4059169699Skan funcdef_syntax = (decl->kind == cdk_function); 4060169699Skan decl = decl->declarator; 406118334Speter break; 406218334Speter 4063169699Skan case cdk_attrs: 4064169699Skan decl = decl->declarator; 406590075Sobrien break; 406690075Sobrien 4067169699Skan case cdk_id: 4068169699Skan if (decl->u.id) 4069169699Skan name = IDENTIFIER_POINTER (decl->u.id); 407018334Speter decl = 0; 407118334Speter break; 407218334Speter 407318334Speter default: 4074169699Skan gcc_unreachable (); 407518334Speter } 4076132730Skan orig_name = name; 407718334Speter if (name == 0) 407818334Speter name = "type name"; 407918334Speter } 408018334Speter 408118334Speter /* A function definition's declarator must have the form of 408218334Speter a function declarator. */ 408318334Speter 4084169699Skan if (funcdef_flag && !funcdef_syntax) 408518334Speter return 0; 408618334Speter 408718334Speter /* If this looks like a function definition, make it one, 408818334Speter even if it occurs where parms are expected. 408918334Speter Then store_parm_decls will reject it and not use it as a parm. */ 4090169699Skan if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag) 409118334Speter decl_context = PARM; 409218334Speter 4093169699Skan if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS) 4094169699Skan warn_deprecated_use (declspecs->type); 409518334Speter 4096169699Skan if ((decl_context == NORMAL || decl_context == FIELD) 4097169699Skan && current_scope == file_scope 4098169699Skan && variably_modified_type_p (type, NULL_TREE)) 409918334Speter { 4100169699Skan error ("variably modified %qs at file scope", name); 4101169699Skan type = integer_type_node; 410218334Speter } 410318334Speter 410418334Speter typedef_type = type; 4105169699Skan size_varies = C_TYPE_VARIABLE_SIZE (type); 410618334Speter 4107169699Skan /* Diagnose defaulting to "int". */ 410818334Speter 4109169699Skan if (declspecs->default_int_p && !in_system_header) 411018334Speter { 4111169699Skan /* Issue a warning if this is an ISO C 99 program or if 4112169699Skan -Wreturn-type and this is a function, or if -Wimplicit; 4113169699Skan prefer the former warning since it is more explicit. */ 4114169699Skan if ((warn_implicit_int || warn_return_type || flag_isoc99) 4115169699Skan && funcdef_flag) 4116169699Skan warn_about_return_type = 1; 4117169699Skan else if (warn_implicit_int || flag_isoc99) 4118169699Skan pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name); 411918334Speter } 412018334Speter 4121169699Skan /* Adjust the type if a bit-field is being declared, 4122169699Skan -funsigned-bitfields applied and the type is not explicitly 4123169699Skan "signed". */ 4124169699Skan if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p 4125169699Skan && TREE_CODE (type) == INTEGER_TYPE) 4126169699Skan type = c_common_unsigned_type (type); 412718334Speter 412852284Sobrien /* Figure out the type qualifiers for the declaration. There are 412952284Sobrien two ways a declaration can become qualified. One is something 413052284Sobrien like `const int i' where the `const' is explicit. Another is 413152284Sobrien something like `typedef const int CI; CI i' where the type of the 4132132730Skan declaration contains the `const'. A third possibility is that 4133132730Skan there is a type qualifier on the element type of a typedefed 4134132730Skan array type, in which case we should extract that qualifier so 4135132730Skan that c_apply_type_quals_to_decls receives the full list of 4136132730Skan qualifiers to work with (C90 is not entirely clear about whether 4137132730Skan duplicate qualifiers should be diagnosed in this case, but it 4138132730Skan seems most appropriate to do so). */ 4139132730Skan element_type = strip_array_types (type); 4140169699Skan constp = declspecs->const_p + TYPE_READONLY (element_type); 4141169699Skan restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type); 4142169699Skan volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type); 4143132730Skan if (pedantic && !flag_isoc99) 4144132730Skan { 4145132730Skan if (constp > 1) 4146169699Skan pedwarn ("duplicate %<const%>"); 4147132730Skan if (restrictp > 1) 4148169699Skan pedwarn ("duplicate %<restrict%>"); 4149132730Skan if (volatilep > 1) 4150169699Skan pedwarn ("duplicate %<volatile%>"); 4151132730Skan } 4152169699Skan if (!flag_gen_aux_info && (TYPE_QUALS (element_type))) 415318334Speter type = TYPE_MAIN_VARIANT (type); 415452284Sobrien type_quals = ((constp ? TYPE_QUAL_CONST : 0) 415552284Sobrien | (restrictp ? TYPE_QUAL_RESTRICT : 0) 415652284Sobrien | (volatilep ? TYPE_QUAL_VOLATILE : 0)); 415718334Speter 4158169699Skan /* Warn about storage classes that are invalid for certain 4159169699Skan kinds of declarations (parameters, typenames, etc.). */ 416018334Speter 4161169699Skan if (funcdef_flag 4162169699Skan && (threadp 4163169699Skan || storage_class == csc_auto 4164169699Skan || storage_class == csc_register 4165169699Skan || storage_class == csc_typedef)) 4166169699Skan { 4167169699Skan if (storage_class == csc_auto 4168169699Skan && (pedantic || current_scope == file_scope)) 4169169699Skan pedwarn ("function definition declared %<auto%>"); 4170169699Skan if (storage_class == csc_register) 4171169699Skan error ("function definition declared %<register%>"); 4172169699Skan if (storage_class == csc_typedef) 4173169699Skan error ("function definition declared %<typedef%>"); 4174169699Skan if (threadp) 4175169699Skan error ("function definition declared %<__thread%>"); 4176169699Skan threadp = false; 4177169699Skan if (storage_class == csc_auto 4178169699Skan || storage_class == csc_register 4179169699Skan || storage_class == csc_typedef) 4180169699Skan storage_class = csc_none; 4181169699Skan } 4182169699Skan else if (decl_context != NORMAL && (storage_class != csc_none || threadp)) 4183169699Skan { 4184169699Skan if (decl_context == PARM && storage_class == csc_register) 4185169699Skan ; 4186169699Skan else 4187169699Skan { 4188169699Skan switch (decl_context) 4189169699Skan { 4190169699Skan case FIELD: 4191169699Skan error ("storage class specified for structure field %qs", 4192169699Skan name); 4193169699Skan break; 4194169699Skan case PARM: 4195169699Skan error ("storage class specified for parameter %qs", name); 4196169699Skan break; 4197169699Skan default: 4198169699Skan error ("storage class specified for typename"); 4199169699Skan break; 4200169699Skan } 4201169699Skan storage_class = csc_none; 4202169699Skan threadp = false; 4203169699Skan } 4204169699Skan } 4205169699Skan else if (storage_class == csc_extern 4206169699Skan && initialized 4207169699Skan && !funcdef_flag) 4208169699Skan { 4209169699Skan /* 'extern' with initialization is invalid if not at file scope. */ 4210169699Skan if (current_scope == file_scope) 4211169699Skan { 4212169699Skan /* It is fine to have 'extern const' when compiling at C 4213169699Skan and C++ intersection. */ 4214169699Skan if (!(warn_cxx_compat && constp)) 4215169699Skan warning (0, "%qs initialized and declared %<extern%>", name); 4216169699Skan } 4217169699Skan else 4218169699Skan error ("%qs has both %<extern%> and initializer", name); 4219169699Skan } 4220169699Skan else if (current_scope == file_scope) 4221169699Skan { 4222169699Skan if (storage_class == csc_auto) 4223169699Skan error ("file-scope declaration of %qs specifies %<auto%>", name); 4224169699Skan if (pedantic && storage_class == csc_register) 4225169699Skan pedwarn ("file-scope declaration of %qs specifies %<register%>", name); 4226169699Skan } 4227169699Skan else 4228169699Skan { 4229169699Skan if (storage_class == csc_extern && funcdef_flag) 4230169699Skan error ("nested function %qs declared %<extern%>", name); 4231169699Skan else if (threadp && storage_class == csc_none) 4232169699Skan { 4233169699Skan error ("function-scope %qs implicitly auto and declared " 4234169699Skan "%<__thread%>", 4235169699Skan name); 4236169699Skan threadp = false; 4237169699Skan } 4238169699Skan } 423918334Speter 424018334Speter /* Now figure out the structure of the declarator proper. 424118334Speter Descend through it, creating more complex types, until we reach 4242169699Skan the declared identifier (or NULL_TREE, in an absolute declarator). 4243169699Skan At each stage we maintain an unqualified version of the type 4244169699Skan together with any qualifiers that should be applied to it with 4245169699Skan c_build_qualified_type; this way, array types including 4246169699Skan multidimensional array types are first built up in unqualified 4247169699Skan form and then the qualified form is created with 4248169699Skan TYPE_MAIN_VARIANT pointing to the unqualified form. */ 424918334Speter 4250169699Skan while (declarator && declarator->kind != cdk_id) 425118334Speter { 425218334Speter if (type == error_mark_node) 425318334Speter { 4254169699Skan declarator = declarator->declarator; 425518334Speter continue; 425618334Speter } 425718334Speter 4258169699Skan /* Each level of DECLARATOR is either a cdk_array (for ...[..]), 4259169699Skan a cdk_pointer (for *...), 4260169699Skan a cdk_function (for ...(...)), 4261169699Skan a cdk_attrs (for nested attributes), 4262169699Skan or a cdk_id (for the name being declared 4263169699Skan or the place in an absolute declarator 426418334Speter where the name was omitted). 4265169699Skan For the last case, we have just exited the loop. 426618334Speter 426718334Speter At this point, TYPE is the type of elements of an array, 426818334Speter or for a function to return, or for a pointer to point to. 426918334Speter After this sequence of ifs, TYPE is the type of the 427018334Speter array or function or pointer, and DECLARATOR has had its 427118334Speter outermost layer removed. */ 427218334Speter 4273169699Skan if (array_ptr_quals != TYPE_UNQUALIFIED 4274169699Skan || array_ptr_attrs != NULL_TREE 4275169699Skan || array_parm_static) 427618334Speter { 427790075Sobrien /* Only the innermost declarator (making a parameter be of 427890075Sobrien array type which is converted to pointer type) 427990075Sobrien may have static or type qualifiers. */ 428090075Sobrien error ("static or type qualifiers in non-parameter array declarator"); 4281169699Skan array_ptr_quals = TYPE_UNQUALIFIED; 4282169699Skan array_ptr_attrs = NULL_TREE; 428390075Sobrien array_parm_static = 0; 428490075Sobrien } 428590075Sobrien 4286169699Skan switch (declarator->kind) 428790075Sobrien { 4288169699Skan case cdk_attrs: 4289169699Skan { 4290169699Skan /* A declarator with embedded attributes. */ 4291169699Skan tree attrs = declarator->u.attrs; 4292169699Skan const struct c_declarator *inner_decl; 4293169699Skan int attr_flags = 0; 4294169699Skan declarator = declarator->declarator; 4295169699Skan inner_decl = declarator; 4296169699Skan while (inner_decl->kind == cdk_attrs) 4297169699Skan inner_decl = inner_decl->declarator; 4298169699Skan if (inner_decl->kind == cdk_id) 4299169699Skan attr_flags |= (int) ATTR_FLAG_DECL_NEXT; 4300169699Skan else if (inner_decl->kind == cdk_function) 4301169699Skan attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; 4302169699Skan else if (inner_decl->kind == cdk_array) 4303169699Skan attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; 4304169699Skan returned_attrs = decl_attributes (&type, 4305169699Skan chainon (returned_attrs, attrs), 4306169699Skan attr_flags); 4307169699Skan break; 4308169699Skan } 4309169699Skan case cdk_array: 4310169699Skan { 4311169699Skan tree itype = NULL_TREE; 4312169699Skan tree size = declarator->u.array.dimen; 4313169699Skan /* The index is a signed object `sizetype' bits wide. */ 4314169699Skan tree index_type = c_common_signed_type (sizetype); 431518334Speter 4316169699Skan array_ptr_quals = declarator->u.array.quals; 4317169699Skan array_ptr_attrs = declarator->u.array.attrs; 4318169699Skan array_parm_static = declarator->u.array.static_p; 4319169699Skan array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p; 432090075Sobrien 4321169699Skan declarator = declarator->declarator; 432218334Speter 4323169699Skan /* Check for some types that there cannot be arrays of. */ 432418334Speter 4325169699Skan if (VOID_TYPE_P (type)) 4326169699Skan { 4327169699Skan error ("declaration of %qs as array of voids", name); 4328169699Skan type = error_mark_node; 4329169699Skan } 433018334Speter 4331169699Skan if (TREE_CODE (type) == FUNCTION_TYPE) 4332169699Skan { 4333169699Skan error ("declaration of %qs as array of functions", name); 4334169699Skan type = error_mark_node; 4335169699Skan } 4336169699Skan 4337169699Skan if (pedantic && !in_system_header && flexible_array_type_p (type)) 4338169699Skan pedwarn ("invalid use of structure with flexible array member"); 4339169699Skan 4340169699Skan if (size == error_mark_node) 434118334Speter type = error_mark_node; 434218334Speter 4343169699Skan if (type == error_mark_node) 4344169699Skan continue; 4345117421Skan 4346169699Skan /* If size was specified, set ITYPE to a range-type for 4347169699Skan that size. Otherwise, ITYPE remains null. finish_decl 4348169699Skan may figure it out from an initial value. */ 434918334Speter 4350169699Skan if (size) 4351169699Skan { 4352169699Skan /* Strip NON_LVALUE_EXPRs since we aren't using as an 4353169699Skan lvalue. */ 4354169699Skan STRIP_TYPE_NOPS (size); 435518334Speter 4356169699Skan if (!INTEGRAL_TYPE_P (TREE_TYPE (size))) 4357169699Skan { 4358169699Skan error ("size of array %qs has non-integer type", name); 4359169699Skan size = integer_one_node; 4360169699Skan } 436118334Speter 4362169699Skan if (pedantic && integer_zerop (size)) 4363169699Skan pedwarn ("ISO C forbids zero-size array %qs", name); 436418334Speter 4365169699Skan if (TREE_CODE (size) == INTEGER_CST) 4366169699Skan { 4367169699Skan constant_expression_warning (size); 4368169699Skan if (tree_int_cst_sgn (size) < 0) 4369169699Skan { 4370169699Skan error ("size of array %qs is negative", name); 4371169699Skan size = integer_one_node; 4372169699Skan } 4373169699Skan } 4374169699Skan else if ((decl_context == NORMAL || decl_context == FIELD) 4375169699Skan && current_scope == file_scope) 4376169699Skan { 4377169699Skan error ("variably modified %qs at file scope", name); 4378169699Skan size = integer_one_node; 4379169699Skan } 4380169699Skan else 4381169699Skan { 4382169699Skan /* Make sure the array size remains visibly 4383169699Skan nonconstant even if it is (eg) a const variable 4384169699Skan with known value. */ 4385169699Skan size_varies = 1; 4386259269Spfg warn_variable_length_array (orig_name, size); 4387223715Suqs if (warn_variable_decl) 4388223715Suqs warning (0, "variable-sized array %qs", name); 4389169699Skan } 439018334Speter 4391169699Skan if (integer_zerop (size)) 4392169699Skan { 4393169699Skan /* A zero-length array cannot be represented with 4394169699Skan an unsigned index type, which is what we'll 4395169699Skan get with build_index_type. Create an 4396169699Skan open-ended range instead. */ 4397169699Skan itype = build_range_type (sizetype, size, NULL_TREE); 4398169699Skan } 4399169699Skan else 4400169699Skan { 4401169699Skan /* Arrange for the SAVE_EXPR on the inside of the 4402169699Skan MINUS_EXPR, which allows the -1 to get folded 4403169699Skan with the +1 that happens when building TYPE_SIZE. */ 4404169699Skan if (size_varies) 4405169699Skan size = variable_size (size); 440618334Speter 4407169699Skan /* Compute the maximum valid index, that is, size 4408169699Skan - 1. Do the calculation in index_type, so that 4409169699Skan if it is a variable the computations will be 4410169699Skan done in the proper mode. */ 4411169699Skan itype = fold_build2 (MINUS_EXPR, index_type, 4412169699Skan convert (index_type, size), 4413169699Skan convert (index_type, 4414169699Skan size_one_node)); 441518334Speter 4416169699Skan /* If that overflowed, the array is too big. ??? 4417169699Skan While a size of INT_MAX+1 technically shouldn't 4418169699Skan cause an overflow (because we subtract 1), the 4419169699Skan overflow is recorded during the conversion to 4420169699Skan index_type, before the subtraction. Handling 4421169699Skan this case seems like an unnecessary 4422169699Skan complication. */ 4423169699Skan if (TREE_CODE (itype) == INTEGER_CST 4424169699Skan && TREE_OVERFLOW (itype)) 4425169699Skan { 4426169699Skan error ("size of array %qs is too large", name); 4427169699Skan type = error_mark_node; 4428169699Skan continue; 4429169699Skan } 443050397Sobrien 4431169699Skan itype = build_index_type (itype); 4432169699Skan } 4433169699Skan } 4434169699Skan else if (decl_context == FIELD) 4435169699Skan { 4436169699Skan if (pedantic && !flag_isoc99 && !in_system_header) 4437169699Skan pedwarn ("ISO C90 does not support flexible array members"); 443890075Sobrien 4439169699Skan /* ISO C99 Flexible array members are effectively 4440169699Skan identical to GCC's zero-length array extension. */ 4441169699Skan itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 4442169699Skan } 4443169699Skan else if (decl_context == PARM) 4444169699Skan { 4445169699Skan if (array_parm_vla_unspec_p) 4446169699Skan { 4447169699Skan if (! orig_name) 4448169699Skan { 4449169699Skan /* C99 6.7.5.2p4 */ 4450169699Skan error ("%<[*]%> not allowed in other than a declaration"); 4451169699Skan } 445218334Speter 4453169699Skan itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 4454169699Skan size_varies = 1; 4455169699Skan } 4456169699Skan } 4457169699Skan else if (decl_context == TYPENAME) 4458169699Skan { 4459169699Skan if (array_parm_vla_unspec_p) 4460169699Skan { 4461169699Skan /* The error is printed elsewhere. We use this to 4462169699Skan avoid messing up with incomplete array types of 4463169699Skan the same type, that would otherwise be modified 4464169699Skan below. */ 4465169699Skan itype = build_range_type (sizetype, size_zero_node, 4466169699Skan NULL_TREE); 4467169699Skan } 4468169699Skan } 446918334Speter 4470169699Skan /* Complain about arrays of incomplete types. */ 4471169699Skan if (!COMPLETE_TYPE_P (type)) 4472169699Skan { 4473169699Skan error ("array type has incomplete element type"); 4474169699Skan type = error_mark_node; 4475169699Skan } 4476169699Skan else 4477169699Skan /* When itype is NULL, a shared incomplete array type is 4478169699Skan returned for all array of a given type. Elsewhere we 4479169699Skan make sure we don't complete that type before copying 4480169699Skan it, but here we want to make sure we don't ever 4481169699Skan modify the shared type, so we gcc_assert (itype) 4482169699Skan below. */ 4483169699Skan type = build_array_type (type, itype); 448490075Sobrien 4485169699Skan if (type != error_mark_node) 4486169699Skan { 4487169699Skan if (size_varies) 4488169699Skan { 4489169699Skan /* It is ok to modify type here even if itype is 4490169699Skan NULL: if size_varies, we're in a 4491169699Skan multi-dimensional array and the inner type has 4492169699Skan variable size, so the enclosing shared array type 4493169699Skan must too. */ 4494169699Skan if (size && TREE_CODE (size) == INTEGER_CST) 4495169699Skan type 4496169699Skan = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); 4497169699Skan C_TYPE_VARIABLE_SIZE (type) = 1; 4498169699Skan } 449990075Sobrien 4500169699Skan /* The GCC extension for zero-length arrays differs from 4501169699Skan ISO flexible array members in that sizeof yields 4502169699Skan zero. */ 4503169699Skan if (size && integer_zerop (size)) 4504169699Skan { 4505169699Skan gcc_assert (itype); 4506169699Skan TYPE_SIZE (type) = bitsize_zero_node; 4507169699Skan TYPE_SIZE_UNIT (type) = size_zero_node; 4508169699Skan } 4509169699Skan if (array_parm_vla_unspec_p) 4510169699Skan { 4511169699Skan gcc_assert (itype); 4512169699Skan /* The type is complete. C99 6.7.5.2p4 */ 4513169699Skan TYPE_SIZE (type) = bitsize_zero_node; 4514169699Skan TYPE_SIZE_UNIT (type) = size_zero_node; 4515169699Skan } 4516169699Skan } 451718334Speter 4518169699Skan if (decl_context != PARM 4519169699Skan && (array_ptr_quals != TYPE_UNQUALIFIED 4520169699Skan || array_ptr_attrs != NULL_TREE 4521169699Skan || array_parm_static)) 4522169699Skan { 4523169699Skan error ("static or type qualifiers in non-parameter array declarator"); 4524169699Skan array_ptr_quals = TYPE_UNQUALIFIED; 4525169699Skan array_ptr_attrs = NULL_TREE; 4526169699Skan array_parm_static = 0; 4527169699Skan } 4528169699Skan break; 4529169699Skan } 4530169699Skan case cdk_function: 4531169699Skan { 4532169699Skan /* Say it's a definition only for the declarator closest 4533169699Skan to the identifier, apart possibly from some 4534169699Skan attributes. */ 4535169699Skan bool really_funcdef = false; 4536169699Skan tree arg_types; 4537169699Skan if (funcdef_flag) 4538169699Skan { 4539169699Skan const struct c_declarator *t = declarator->declarator; 4540169699Skan while (t->kind == cdk_attrs) 4541169699Skan t = t->declarator; 4542169699Skan really_funcdef = (t->kind == cdk_id); 4543169699Skan } 454418334Speter 4545169699Skan /* Declaring a function type. Make sure we have a valid 4546169699Skan type for the function to return. */ 4547169699Skan if (type == error_mark_node) 4548169699Skan continue; 454990075Sobrien 4550169699Skan size_varies = 0; 455118334Speter 4552169699Skan /* Warn about some types functions can't return. */ 4553169699Skan if (TREE_CODE (type) == FUNCTION_TYPE) 4554169699Skan { 4555169699Skan error ("%qs declared as function returning a function", name); 4556169699Skan type = integer_type_node; 4557169699Skan } 4558169699Skan if (TREE_CODE (type) == ARRAY_TYPE) 4559169699Skan { 4560169699Skan error ("%qs declared as function returning an array", name); 4561169699Skan type = integer_type_node; 4562169699Skan } 456318334Speter 4564169699Skan /* Construct the function type and go to the next 4565169699Skan inner layer of declarator. */ 4566169699Skan arg_info = declarator->u.arg_info; 4567169699Skan arg_types = grokparms (arg_info, really_funcdef); 4568169699Skan if (really_funcdef) 4569169699Skan put_pending_sizes (arg_info->pending_sizes); 457018334Speter 4571169699Skan /* Type qualifiers before the return type of the function 4572169699Skan qualify the return type, not the function type. */ 4573169699Skan if (type_quals) 4574169699Skan { 4575169699Skan /* Type qualifiers on a function return type are 4576169699Skan normally permitted by the standard but have no 4577169699Skan effect, so give a warning at -Wreturn-type. 4578169699Skan Qualifiers on a void return type are banned on 4579169699Skan function definitions in ISO C; GCC used to used 4580169699Skan them for noreturn functions. */ 4581169699Skan if (VOID_TYPE_P (type) && really_funcdef) 4582169699Skan pedwarn ("function definition has qualified void return type"); 4583169699Skan else 4584169699Skan warning (OPT_Wreturn_type, 4585169699Skan "type qualifiers ignored on function return type"); 458618334Speter 4587169699Skan type = c_build_qualified_type (type, type_quals); 4588169699Skan } 4589169699Skan type_quals = TYPE_UNQUALIFIED; 459018334Speter 4591169699Skan type = build_function_type (type, arg_types); 4592169699Skan declarator = declarator->declarator; 459318334Speter 4594169699Skan /* Set the TYPE_CONTEXTs for each tagged type which is local to 4595169699Skan the formal parameter list of this FUNCTION_TYPE to point to 4596169699Skan the FUNCTION_TYPE node itself. */ 459790075Sobrien { 4598169699Skan tree link; 459990075Sobrien 4600169699Skan for (link = arg_info->tags; 4601169699Skan link; 4602169699Skan link = TREE_CHAIN (link)) 4603169699Skan TYPE_CONTEXT (TREE_VALUE (link)) = type; 460490075Sobrien } 4605169699Skan break; 4606169699Skan } 4607169699Skan case cdk_pointer: 460818334Speter { 4609169699Skan /* Merge any constancy or volatility into the target type 4610169699Skan for the pointer. */ 461118334Speter 4612169699Skan if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 4613169699Skan && type_quals) 4614169699Skan pedwarn ("ISO C forbids qualified function types"); 4615169699Skan if (type_quals) 4616169699Skan type = c_build_qualified_type (type, type_quals); 4617169699Skan size_varies = 0; 461818334Speter 4619169699Skan /* When the pointed-to type involves components of variable size, 4620169699Skan care must be taken to ensure that the size evaluation code is 4621169699Skan emitted early enough to dominate all the possible later uses 4622169699Skan and late enough for the variables on which it depends to have 4623169699Skan been assigned. 462418334Speter 4625169699Skan This is expected to happen automatically when the pointed-to 4626169699Skan type has a name/declaration of it's own, but special attention 4627169699Skan is required if the type is anonymous. 462818334Speter 4629169699Skan We handle the NORMAL and FIELD contexts here by attaching an 4630169699Skan artificial TYPE_DECL to such pointed-to type. This forces the 4631169699Skan sizes evaluation at a safe point and ensures it is not deferred 4632169699Skan until e.g. within a deeper conditional context. 463318334Speter 4634169699Skan We expect nothing to be needed here for PARM or TYPENAME. 4635169699Skan Pushing a TYPE_DECL at this point for TYPENAME would actually 4636169699Skan be incorrect, as we might be in the middle of an expression 4637169699Skan with side effects on the pointed-to type size "arguments" prior 4638169699Skan to the pointer declaration point and the fake TYPE_DECL in the 4639169699Skan enclosing context would force the size evaluation prior to the 4640169699Skan side effects. */ 464152284Sobrien 4642169699Skan if (!TYPE_NAME (type) 4643169699Skan && (decl_context == NORMAL || decl_context == FIELD) 4644169699Skan && variably_modified_type_p (type, NULL_TREE)) 4645169699Skan { 4646169699Skan tree decl = build_decl (TYPE_DECL, NULL_TREE, type); 4647169699Skan DECL_ARTIFICIAL (decl) = 1; 4648169699Skan pushdecl (decl); 4649169699Skan finish_decl (decl, NULL_TREE, NULL_TREE); 4650169699Skan TYPE_NAME (type) = decl; 4651169699Skan } 465252284Sobrien 4653169699Skan type = build_pointer_type (type); 465490075Sobrien 4655169699Skan /* Process type qualifiers (such as const or volatile) 4656169699Skan that were given inside the `*'. */ 4657169699Skan type_quals = declarator->u.pointer_quals; 465852284Sobrien 4659169699Skan declarator = declarator->declarator; 4660169699Skan break; 4661169699Skan } 4662169699Skan default: 4663169699Skan gcc_unreachable (); 466418334Speter } 466518334Speter } 466618334Speter 4667169699Skan /* Now TYPE has the actual type, apart from any qualifiers in 4668169699Skan TYPE_QUALS. */ 466918334Speter 4670146906Skan /* Check the type and width of a bit-field. */ 4671146906Skan if (bitfield) 4672146906Skan check_bitfield_type_and_width (&type, width, orig_name); 4673146906Skan 467450397Sobrien /* Did array size calculations overflow? */ 467550397Sobrien 467650397Sobrien if (TREE_CODE (type) == ARRAY_TYPE 467790075Sobrien && COMPLETE_TYPE_P (type) 4678169699Skan && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST 4679169699Skan && TREE_OVERFLOW (TYPE_SIZE_UNIT (type))) 468090075Sobrien { 4681169699Skan error ("size of array %qs is too large", name); 468290075Sobrien /* If we proceed with the array type as it is, we'll eventually 468390075Sobrien crash in tree_low_cst(). */ 468490075Sobrien type = error_mark_node; 468590075Sobrien } 468650397Sobrien 468718334Speter /* If this is declaring a typedef name, return a TYPE_DECL. */ 468818334Speter 4689169699Skan if (storage_class == csc_typedef) 469018334Speter { 469118334Speter tree decl; 469218334Speter if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 469352284Sobrien && type_quals) 469490075Sobrien pedwarn ("ISO C forbids qualified function types"); 469552284Sobrien if (type_quals) 469652284Sobrien type = c_build_qualified_type (type, type_quals); 4697169699Skan decl = build_decl (TYPE_DECL, declarator->u.id, type); 4698169699Skan if (declspecs->explicit_signed_p) 469918334Speter C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 470090075Sobrien decl_attributes (&decl, returned_attrs, 0); 4701169699Skan if (declspecs->inline_p) 4702169699Skan pedwarn ("typedef %q+D declared %<inline%>", decl); 470318334Speter return decl; 470418334Speter } 470518334Speter 470618334Speter /* If this is a type name (such as, in a cast or sizeof), 470718334Speter compute the type and return it now. */ 470818334Speter 470918334Speter if (decl_context == TYPENAME) 471018334Speter { 4711169699Skan /* Note that the grammar rejects storage classes in typenames 4712169699Skan and fields. */ 4713169699Skan gcc_assert (storage_class == csc_none && !threadp 4714169699Skan && !declspecs->inline_p); 471518334Speter if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 471652284Sobrien && type_quals) 471790075Sobrien pedwarn ("ISO C forbids const or volatile function types"); 471852284Sobrien if (type_quals) 471952284Sobrien type = c_build_qualified_type (type, type_quals); 472090075Sobrien decl_attributes (&type, returned_attrs, 0); 472118334Speter return type; 472218334Speter } 472318334Speter 4724169699Skan if (pedantic && decl_context == FIELD 4725169699Skan && variably_modified_type_p (type, NULL_TREE)) 4726169699Skan { 4727169699Skan /* C99 6.7.2.1p8 */ 4728169699Skan pedwarn ("a member of a structure or union cannot have a variably modified type"); 4729169699Skan } 4730169699Skan 473118334Speter /* Aside from typedefs and type names (handle above), 473218334Speter `void' at top level (not within pointer) 473318334Speter is allowed only in public variables. 473418334Speter We don't complain about parms either, but that is because 473518334Speter a better error message can be made later. */ 473618334Speter 473790075Sobrien if (VOID_TYPE_P (type) && decl_context != PARM 4738169699Skan && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE) 4739169699Skan && (storage_class == csc_extern 4740169699Skan || (current_scope == file_scope 4741169699Skan && !(storage_class == csc_static 4742169699Skan || storage_class == csc_register))))) 474318334Speter { 4744169699Skan error ("variable or field %qs declared void", name); 474518334Speter type = integer_type_node; 474618334Speter } 474718334Speter 474818334Speter /* Now create the decl, which may be a VAR_DECL, a PARM_DECL 474918334Speter or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ 475018334Speter 475118334Speter { 475290075Sobrien tree decl; 475318334Speter 475418334Speter if (decl_context == PARM) 475518334Speter { 475690075Sobrien tree type_as_written; 475790075Sobrien tree promoted_type; 475818334Speter 475918334Speter /* A parameter declared as an array of T is really a pointer to T. 476018334Speter One declared as a function is really a pointer to a function. */ 476118334Speter 476218334Speter if (TREE_CODE (type) == ARRAY_TYPE) 476318334Speter { 476418334Speter /* Transfer const-ness of array into that of type pointed to. */ 476518334Speter type = TREE_TYPE (type); 476652284Sobrien if (type_quals) 476752284Sobrien type = c_build_qualified_type (type, type_quals); 476818334Speter type = build_pointer_type (type); 4769169699Skan type_quals = array_ptr_quals; 477090075Sobrien 4771169699Skan /* We don't yet implement attributes in this context. */ 4772169699Skan if (array_ptr_attrs != NULL_TREE) 4773169699Skan warning (OPT_Wattributes, 4774169699Skan "attributes in parameter array declarator ignored"); 477590075Sobrien 477618334Speter size_varies = 0; 477718334Speter } 477818334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 477918334Speter { 478052284Sobrien if (pedantic && type_quals) 478190075Sobrien pedwarn ("ISO C forbids qualified function types"); 478252284Sobrien if (type_quals) 478352284Sobrien type = c_build_qualified_type (type, type_quals); 478418334Speter type = build_pointer_type (type); 478552284Sobrien type_quals = TYPE_UNQUALIFIED; 478618334Speter } 478790075Sobrien else if (type_quals) 478890075Sobrien type = c_build_qualified_type (type, type_quals); 4789132730Skan 479090075Sobrien type_as_written = type; 479118334Speter 4792169699Skan decl = build_decl (PARM_DECL, declarator->u.id, type); 479318334Speter if (size_varies) 479418334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 479518334Speter 479618334Speter /* Compute the type actually passed in the parmlist, 479718334Speter for the case where there is no prototype. 479818334Speter (For example, shorts and chars are passed as ints.) 479918334Speter When there is a prototype, this is overridden later. */ 480018334Speter 480190075Sobrien if (type == error_mark_node) 480290075Sobrien promoted_type = type; 480390075Sobrien else 4804117421Skan promoted_type = c_type_promotes_to (type); 480518334Speter 480690075Sobrien DECL_ARG_TYPE (decl) = promoted_type; 4807169699Skan if (declspecs->inline_p) 4808169699Skan pedwarn ("parameter %q+D declared %<inline%>", decl); 480918334Speter } 481018334Speter else if (decl_context == FIELD) 481118334Speter { 4812169699Skan /* Note that the grammar rejects storage classes in typenames 4813169699Skan and fields. */ 4814169699Skan gcc_assert (storage_class == csc_none && !threadp 4815169699Skan && !declspecs->inline_p); 4816169699Skan 481718334Speter /* Structure field. It may not be a function. */ 481896263Sobrien 481918334Speter if (TREE_CODE (type) == FUNCTION_TYPE) 482018334Speter { 4821169699Skan error ("field %qs declared as a function", name); 482218334Speter type = build_pointer_type (type); 482318334Speter } 482490075Sobrien else if (TREE_CODE (type) != ERROR_MARK 4825169699Skan && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type)) 482618334Speter { 4827169699Skan error ("field %qs has incomplete type", name); 482818334Speter type = error_mark_node; 482918334Speter } 4830169699Skan type = c_build_qualified_type (type, type_quals); 4831169699Skan decl = build_decl (FIELD_DECL, declarator->u.id, type); 483290075Sobrien DECL_NONADDRESSABLE_P (decl) = bitfield; 483390075Sobrien 483418334Speter if (size_varies) 483518334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 483618334Speter } 483718334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 483818334Speter { 4839169699Skan if (storage_class == csc_register || threadp) 4840169699Skan { 4841169699Skan error ("invalid storage class for function %qs", name); 4842169699Skan } 4843169699Skan else if (current_scope != file_scope) 4844169699Skan { 4845169699Skan /* Function declaration not at file scope. Storage 4846169699Skan classes other than `extern' are not allowed, C99 4847169699Skan 6.7.1p5, and `extern' makes no difference. However, 4848169699Skan GCC allows 'auto', perhaps with 'inline', to support 4849169699Skan nested functions. */ 4850169699Skan if (storage_class == csc_auto) 4851169699Skan { 4852169699Skan if (pedantic) 4853169699Skan pedwarn ("invalid storage class for function %qs", name); 4854169699Skan } 4855169699Skan else if (storage_class == csc_static) 4856169699Skan { 4857169699Skan error ("invalid storage class for function %qs", name); 4858169699Skan if (funcdef_flag) 4859169699Skan storage_class = declspecs->storage_class = csc_none; 4860169699Skan else 4861169699Skan return 0; 4862169699Skan } 4863169699Skan } 486418334Speter 4865169699Skan decl = build_decl (FUNCTION_DECL, declarator->u.id, type); 486690075Sobrien decl = build_decl_attribute_variant (decl, decl_attr); 486718334Speter 4868169699Skan DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl); 486990075Sobrien 4870169699Skan if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl)) 487190075Sobrien pedwarn ("ISO C forbids qualified function types"); 487218334Speter 4873169699Skan /* GNU C interprets a volatile-qualified function type to indicate 487452284Sobrien that the function does not return. */ 487552284Sobrien if ((type_quals & TYPE_QUAL_VOLATILE) 487690075Sobrien && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) 4877169699Skan warning (0, "%<noreturn%> function returns non-void value"); 487818334Speter 4879169699Skan /* Every function declaration is an external reference 4880169699Skan (DECL_EXTERNAL) except for those which are not at file 4881169699Skan scope and are explicitly declared "auto". This is 4882169699Skan forbidden by standard C (C99 6.7.1p5) and is interpreted by 4883169699Skan GCC to signify a forward declaration of a nested function. */ 4884169699Skan if (storage_class == csc_auto && current_scope != file_scope) 4885169699Skan DECL_EXTERNAL (decl) = 0; 4886189824Sdas /* In C99, a function which is declared 'inline' with 'extern' 4887189824Sdas is not an external reference (which is confusing). It 4888189824Sdas means that the later definition of the function must be output 4889189824Sdas in this file, C99 6.7.4p6. In GNU C89, a function declared 4890189824Sdas 'extern inline' is an external reference. */ 4891189824Sdas else if (declspecs->inline_p && storage_class != csc_static) 4892189824Sdas DECL_EXTERNAL (decl) = ((storage_class == csc_extern) 4893189824Sdas == flag_gnu89_inline); 4894169699Skan else 4895189824Sdas DECL_EXTERNAL (decl) = !initialized; 4896169699Skan 489718334Speter /* Record absence of global scope for `static' or `auto'. */ 489818334Speter TREE_PUBLIC (decl) 4899169699Skan = !(storage_class == csc_static || storage_class == csc_auto); 490018334Speter 4901169699Skan /* For a function definition, record the argument information 4902169699Skan block where store_parm_decls will look for it. */ 4903169699Skan if (funcdef_flag) 4904169699Skan current_function_arg_info = arg_info; 4905169699Skan 4906169699Skan if (declspecs->default_int_p) 490796263Sobrien C_FUNCTION_IMPLICIT_INT (decl) = 1; 490896263Sobrien 490918334Speter /* Record presence of `inline', if it is reasonable. */ 4910169699Skan if (flag_hosted && MAIN_NAME_P (declarator->u.id)) 491118334Speter { 4912169699Skan if (declspecs->inline_p) 4913169699Skan pedwarn ("cannot inline function %<main%>"); 491490075Sobrien } 4915169699Skan else if (declspecs->inline_p) 491690075Sobrien { 4917132730Skan /* Record that the function is declared `inline'. */ 491890075Sobrien DECL_DECLARED_INLINE_P (decl) = 1; 491918334Speter 492090075Sobrien /* Do not mark bare declarations as DECL_INLINE. Doing so 492190075Sobrien in the presence of multiple declarations can result in 492290075Sobrien the abstract origin pointing between the declarations, 492390075Sobrien which will confuse dwarf2out. */ 492490075Sobrien if (initialized) 4925189824Sdas DECL_INLINE (decl) = 1; 492618334Speter } 492790075Sobrien /* If -finline-functions, assume it can be inlined. This does 492890075Sobrien two things: let the function be deferred until it is actually 492990075Sobrien needed, and let dwarf2 know that the function is inlinable. */ 493090075Sobrien else if (flag_inline_trees == 2 && initialized) 4931132730Skan DECL_INLINE (decl) = 1; 493218334Speter } 493318334Speter else 493418334Speter { 493518334Speter /* It's a variable. */ 493618334Speter /* An uninitialized decl with `extern' is a reference. */ 4937169699Skan int extern_ref = !initialized && storage_class == csc_extern; 493818334Speter 4939169699Skan type = c_build_qualified_type (type, type_quals); 4940132730Skan 4941169699Skan /* C99 6.2.2p7: It is invalid (compile-time undefined 4942169699Skan behavior) to create an 'extern' declaration for a 4943132730Skan variable if there is a global declaration that is 4944169699Skan 'static' and the global declaration is not visible. 4945169699Skan (If the static declaration _is_ currently visible, 4946169699Skan the 'extern' declaration is taken to refer to that decl.) */ 4947169699Skan if (extern_ref && current_scope != file_scope) 4948132730Skan { 4949169699Skan tree global_decl = identifier_global_value (declarator->u.id); 4950169699Skan tree visible_decl = lookup_name (declarator->u.id); 4951132730Skan 4952132730Skan if (global_decl 4953169699Skan && global_decl != visible_decl 4954132730Skan && TREE_CODE (global_decl) == VAR_DECL 4955132730Skan && !TREE_PUBLIC (global_decl)) 4956169699Skan error ("variable previously declared %<static%> redeclared " 4957169699Skan "%<extern%>"); 4958132730Skan } 4959132730Skan 4960169699Skan decl = build_decl (VAR_DECL, declarator->u.id, type); 4961169699Skan DECL_SOURCE_LOCATION (decl) = declarator->id_loc; 496218334Speter if (size_varies) 496318334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 496418334Speter 4965169699Skan if (declspecs->inline_p) 4966169699Skan pedwarn ("variable %q+D declared %<inline%>", decl); 496718334Speter 4968169699Skan /* At file scope, an initialized extern declaration may follow 4969169699Skan a static declaration. In that case, DECL_EXTERNAL will be 4970169699Skan reset later in start_decl. */ 4971169699Skan DECL_EXTERNAL (decl) = (storage_class == csc_extern); 4972117421Skan 4973132730Skan /* At file scope, the presence of a `static' or `register' storage 497418334Speter class specifier, or the absence of all storage class specifiers 497518334Speter makes this declaration a definition (perhaps tentative). Also, 4976169699Skan the absence of `static' makes it public. */ 4977169699Skan if (current_scope == file_scope) 497818334Speter { 4979169699Skan TREE_PUBLIC (decl) = storage_class != csc_static; 4980117421Skan TREE_STATIC (decl) = !extern_ref; 498118334Speter } 4982132730Skan /* Not at file scope, only `static' makes a static definition. */ 498318334Speter else 498418334Speter { 4985169699Skan TREE_STATIC (decl) = (storage_class == csc_static); 4986117421Skan TREE_PUBLIC (decl) = extern_ref; 498718334Speter } 4988117421Skan 4989169699Skan if (threadp) 4990117421Skan { 4991117421Skan if (targetm.have_tls) 4992169699Skan DECL_TLS_MODEL (decl) = decl_default_tls_model (decl); 4993117421Skan else 4994117421Skan /* A mere warning is sure to result in improper semantics 4995117421Skan at runtime. Don't bother to allow this to compile. */ 4996117421Skan error ("thread-local storage not supported for this target"); 4997117421Skan } 499818334Speter } 499918334Speter 5000169699Skan if (storage_class == csc_extern 5001169699Skan && variably_modified_type_p (type, NULL_TREE)) 5002169699Skan { 5003169699Skan /* C99 6.7.5.2p2 */ 5004169699Skan error ("object with variably modified type must have no linkage"); 5005169699Skan } 5006169699Skan 500718334Speter /* Record `register' declaration for warnings on & 500818334Speter and in case doing stupid register allocation. */ 500918334Speter 5010169699Skan if (storage_class == csc_register) 5011169699Skan { 5012169699Skan C_DECL_REGISTER (decl) = 1; 5013169699Skan DECL_REGISTER (decl) = 1; 5014169699Skan } 501518334Speter 501618334Speter /* Record constancy and volatility. */ 501752284Sobrien c_apply_type_quals_to_decl (type_quals, decl); 501818334Speter 501918334Speter /* If a type has volatile components, it should be stored in memory. 502018334Speter Otherwise, the fact that those components are volatile 5021169699Skan will be ignored, and would even crash the compiler. 5022169699Skan Of course, this only makes sense on VAR,PARM, and RESULT decl's. */ 5023169699Skan if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)) 5024169699Skan && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL 5025169699Skan || TREE_CODE (decl) == RESULT_DECL)) 5026169699Skan { 5027169699Skan /* It is not an error for a structure with volatile fields to 5028169699Skan be declared register, but reset DECL_REGISTER since it 5029169699Skan cannot actually go in a register. */ 5030169699Skan int was_reg = C_DECL_REGISTER (decl); 5031169699Skan C_DECL_REGISTER (decl) = 0; 5032169699Skan DECL_REGISTER (decl) = 0; 5033169699Skan c_mark_addressable (decl); 5034169699Skan C_DECL_REGISTER (decl) = was_reg; 5035169699Skan } 503618334Speter 5037132730Skan /* This is the earliest point at which we might know the assembler 5038132730Skan name of a variable. Thus, if it's known before this, die horribly. */ 5039169699Skan gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)); 5040132730Skan 504190075Sobrien decl_attributes (&decl, returned_attrs, 0); 504218334Speter 504318334Speter return decl; 504418334Speter } 504518334Speter} 504618334Speter 504718334Speter/* Decode the parameter-list info for a function type or function definition. 5048169699Skan The argument is the value returned by `get_parm_info' (or made in c-parse.c 504918334Speter if there is an identifier list instead of a parameter decl list). 505018334Speter These two functions are separate because when a function returns 505118334Speter or receives functions then each is called multiple times but the order 505218334Speter of calls is different. The last call to `grokparms' is always the one 505318334Speter that contains the formal parameter names of a function definition. 505418334Speter 505518334Speter Return a list of arg types to use in the FUNCTION_TYPE for this function. 505618334Speter 5057169699Skan FUNCDEF_FLAG is true for a function definition, false for 505818334Speter a mere declaration. A nonempty identifier-list gets an error message 5059169699Skan when FUNCDEF_FLAG is false. */ 506018334Speter 506118334Speterstatic tree 5062169699Skangrokparms (struct c_arg_info *arg_info, bool funcdef_flag) 506318334Speter{ 5064169699Skan tree arg_types = arg_info->types; 506518334Speter 5066169699Skan if (funcdef_flag && arg_info->had_vla_unspec) 5067169699Skan { 5068169699Skan /* A function definition isn't function prototype scope C99 6.2.1p4. */ 5069169699Skan /* C99 6.7.5.2p4 */ 5070169699Skan error ("%<[*]%> not allowed in other than function prototype scope"); 5071169699Skan } 507218334Speter 5073169699Skan if (arg_types == 0 && !funcdef_flag && !in_system_header) 5074169699Skan warning (OPT_Wstrict_prototypes, 5075169699Skan "function declaration isn%'t a prototype"); 507618334Speter 5077169699Skan if (arg_types == error_mark_node) 5078169699Skan return 0; /* don't set TYPE_ARG_TYPES in this case */ 5079169699Skan 5080169699Skan else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE) 508118334Speter { 5082169699Skan if (!funcdef_flag) 508318334Speter pedwarn ("parameter names (without types) in function declaration"); 508418334Speter 5085169699Skan arg_info->parms = arg_info->types; 5086169699Skan arg_info->types = 0; 508718334Speter return 0; 508818334Speter } 508918334Speter else 509018334Speter { 5091169699Skan tree parm, type, typelt; 5092169699Skan unsigned int parmno; 509318334Speter 5094169699Skan /* If there is a parameter of incomplete type in a definition, 5095169699Skan this is an error. In a declaration this is valid, and a 5096169699Skan struct or union type may be completed later, before any calls 5097169699Skan or definition of the function. In the case where the tag was 5098169699Skan first declared within the parameter list, a warning has 5099169699Skan already been given. If a parameter has void type, then 5100169699Skan however the function cannot be defined or called, so 5101169699Skan warn. */ 5102169699Skan 5103169699Skan for (parm = arg_info->parms, typelt = arg_types, parmno = 1; 5104132730Skan parm; 5105169699Skan parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++) 5106169699Skan { 5107169699Skan type = TREE_VALUE (typelt); 5108169699Skan if (type == error_mark_node) 5109169699Skan continue; 5110132730Skan 5111169699Skan if (!COMPLETE_TYPE_P (type)) 5112169699Skan { 5113169699Skan if (funcdef_flag) 5114169699Skan { 5115169699Skan if (DECL_NAME (parm)) 5116169699Skan error ("parameter %u (%q+D) has incomplete type", 5117169699Skan parmno, parm); 5118169699Skan else 5119169699Skan error ("%Jparameter %u has incomplete type", 5120169699Skan parm, parmno); 5121169699Skan 5122169699Skan TREE_VALUE (typelt) = error_mark_node; 5123169699Skan TREE_TYPE (parm) = error_mark_node; 5124169699Skan } 5125169699Skan else if (VOID_TYPE_P (type)) 5126169699Skan { 5127169699Skan if (DECL_NAME (parm)) 5128169699Skan warning (0, "parameter %u (%q+D) has void type", 5129169699Skan parmno, parm); 5130169699Skan else 5131169699Skan warning (0, "%Jparameter %u has void type", 5132169699Skan parm, parmno); 5133169699Skan } 5134169699Skan } 5135169699Skan 5136169699Skan if (DECL_NAME (parm) && TREE_USED (parm)) 5137169699Skan warn_if_shadowing (parm); 5138169699Skan } 5139169699Skan return arg_types; 514018334Speter } 514118334Speter} 514218334Speter 5143169699Skan/* Take apart the current scope and return a c_arg_info structure with 5144169699Skan info on a parameter list just parsed. 514518334Speter 5146169699Skan This structure is later fed to 'grokparms' and 'store_parm_decls'. 514718334Speter 5148169699Skan ELLIPSIS being true means the argument list ended in '...' so don't 5149169699Skan append a sentinel (void_list_node) to the end of the type-list. */ 5150169699Skan 5151169699Skanstruct c_arg_info * 5152169699Skanget_parm_info (bool ellipsis) 515318334Speter{ 5154169699Skan struct c_binding *b = current_scope->bindings; 5155169699Skan struct c_arg_info *arg_info = XOBNEW (&parser_obstack, 5156169699Skan struct c_arg_info); 5157169699Skan tree parms = 0; 5158169699Skan tree tags = 0; 5159169699Skan tree types = 0; 5160169699Skan tree others = 0; 5161169699Skan 5162132730Skan static bool explained_incomplete_types = false; 5163132730Skan bool gave_void_only_once_err = false; 516418334Speter 5165169699Skan arg_info->parms = 0; 5166169699Skan arg_info->tags = 0; 5167169699Skan arg_info->types = 0; 5168169699Skan arg_info->others = 0; 5169169699Skan arg_info->pending_sizes = 0; 5170169699Skan arg_info->had_vla_unspec = current_scope->had_vla_unspec; 5171132730Skan 5172169699Skan /* The bindings in this scope must not get put into a block. 5173169699Skan We will take care of deleting the binding nodes. */ 5174169699Skan current_scope->bindings = 0; 517518334Speter 5176169699Skan /* This function is only called if there was *something* on the 5177169699Skan parameter list. */ 5178169699Skan gcc_assert (b); 5179169699Skan 5180169699Skan /* A parameter list consisting solely of 'void' indicates that the 5181169699Skan function takes no arguments. But if the 'void' is qualified 5182169699Skan (by 'const' or 'volatile'), or has a storage class specifier 5183169699Skan ('register'), then the behavior is undefined; issue an error. 5184169699Skan Typedefs for 'void' are OK (see DR#157). */ 5185169699Skan if (b->prev == 0 /* one binding */ 5186169699Skan && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */ 5187169699Skan && !DECL_NAME (b->decl) /* anonymous */ 5188169699Skan && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */ 518918334Speter { 5190169699Skan if (TREE_THIS_VOLATILE (b->decl) 5191169699Skan || TREE_READONLY (b->decl) 5192169699Skan || C_DECL_REGISTER (b->decl)) 5193169699Skan error ("%<void%> as only parameter may not be qualified"); 519418334Speter 5195169699Skan /* There cannot be an ellipsis. */ 5196169699Skan if (ellipsis) 5197169699Skan error ("%<void%> must be the only parameter"); 5198132730Skan 5199169699Skan arg_info->types = void_list_node; 5200169699Skan return arg_info; 520118334Speter } 520218334Speter 5203169699Skan if (!ellipsis) 5204169699Skan types = void_list_node; 520518334Speter 5206169699Skan /* Break up the bindings list into parms, tags, types, and others; 5207169699Skan apply sanity checks; purge the name-to-decl bindings. */ 5208169699Skan while (b) 520918334Speter { 5210169699Skan tree decl = b->decl; 5211169699Skan tree type = TREE_TYPE (decl); 5212132730Skan const char *keyword; 5213132730Skan 5214169699Skan switch (TREE_CODE (decl)) 521590075Sobrien { 5216169699Skan case PARM_DECL: 5217169699Skan if (b->id) 5218169699Skan { 5219169699Skan gcc_assert (I_SYMBOL_BINDING (b->id) == b); 5220169699Skan I_SYMBOL_BINDING (b->id) = b->shadowed; 5221169699Skan } 5222132730Skan 5223169699Skan /* Check for forward decls that never got their actual decl. */ 5224169699Skan if (TREE_ASM_WRITTEN (decl)) 5225169699Skan error ("parameter %q+D has just a forward declaration", decl); 5226169699Skan /* Check for (..., void, ...) and issue an error. */ 5227169699Skan else if (VOID_TYPE_P (type) && !DECL_NAME (decl)) 5228169699Skan { 5229169699Skan if (!gave_void_only_once_err) 5230169699Skan { 5231169699Skan error ("%<void%> must be the only parameter"); 5232169699Skan gave_void_only_once_err = true; 5233169699Skan } 5234169699Skan } 5235169699Skan else 5236169699Skan { 5237169699Skan /* Valid parameter, add it to the list. */ 5238169699Skan TREE_CHAIN (decl) = parms; 5239169699Skan parms = decl; 5240132730Skan 5241169699Skan /* Since there is a prototype, args are passed in their 5242169699Skan declared types. The back end may override this later. */ 5243169699Skan DECL_ARG_TYPE (decl) = type; 5244169699Skan types = tree_cons (0, type, types); 5245169699Skan } 5246169699Skan break; 5247169699Skan 5248169699Skan case ENUMERAL_TYPE: keyword = "enum"; goto tag; 5249169699Skan case UNION_TYPE: keyword = "union"; goto tag; 5250169699Skan case RECORD_TYPE: keyword = "struct"; goto tag; 5251169699Skan tag: 5252169699Skan /* Types may not have tag-names, in which case the type 5253169699Skan appears in the bindings list with b->id NULL. */ 5254169699Skan if (b->id) 5255169699Skan { 5256169699Skan gcc_assert (I_TAG_BINDING (b->id) == b); 5257169699Skan I_TAG_BINDING (b->id) = b->shadowed; 5258169699Skan } 5259169699Skan 5260169699Skan /* Warn about any struct, union or enum tags defined in a 5261169699Skan parameter list. The scope of such types is limited to 5262169699Skan the parameter list, which is rarely if ever desirable 5263169699Skan (it's impossible to call such a function with type- 5264169699Skan correct arguments). An anonymous union parm type is 5265169699Skan meaningful as a GNU extension, so don't warn for that. */ 5266169699Skan if (TREE_CODE (decl) != UNION_TYPE || b->id != 0) 5267169699Skan { 5268169699Skan if (b->id) 5269169699Skan /* The %s will be one of 'struct', 'union', or 'enum'. */ 5270169699Skan warning (0, "%<%s %E%> declared inside parameter list", 5271169699Skan keyword, b->id); 5272169699Skan else 5273169699Skan /* The %s will be one of 'struct', 'union', or 'enum'. */ 5274169699Skan warning (0, "anonymous %s declared inside parameter list", 5275169699Skan keyword); 5276169699Skan 5277169699Skan if (!explained_incomplete_types) 5278169699Skan { 5279169699Skan warning (0, "its scope is only this definition or declaration," 5280169699Skan " which is probably not what you want"); 5281169699Skan explained_incomplete_types = true; 5282169699Skan } 5283169699Skan } 5284169699Skan 5285169699Skan tags = tree_cons (b->id, decl, tags); 5286169699Skan break; 5287169699Skan 5288169699Skan case CONST_DECL: 5289169699Skan case TYPE_DECL: 5290169699Skan case FUNCTION_DECL: 5291169699Skan /* CONST_DECLs appear here when we have an embedded enum, 5292169699Skan and TYPE_DECLs appear here when we have an embedded struct 5293169699Skan or union. No warnings for this - we already warned about the 5294169699Skan type itself. FUNCTION_DECLs appear when there is an implicit 5295169699Skan function declaration in the parameter list. */ 5296169699Skan 5297169699Skan TREE_CHAIN (decl) = others; 5298169699Skan others = decl; 5299169699Skan /* fall through */ 5300169699Skan 5301169699Skan case ERROR_MARK: 5302169699Skan /* error_mark_node appears here when we have an undeclared 5303169699Skan variable. Just throw it away. */ 5304169699Skan if (b->id) 5305169699Skan { 5306169699Skan gcc_assert (I_SYMBOL_BINDING (b->id) == b); 5307169699Skan I_SYMBOL_BINDING (b->id) = b->shadowed; 5308169699Skan } 5309169699Skan break; 5310169699Skan 5311169699Skan /* Other things that might be encountered. */ 5312169699Skan case LABEL_DECL: 5313169699Skan case VAR_DECL: 5314169699Skan default: 5315169699Skan gcc_unreachable (); 531618334Speter } 5317132730Skan 5318169699Skan b = free_binding_and_advance (b); 5319132730Skan } 5320132730Skan 5321169699Skan arg_info->parms = parms; 5322169699Skan arg_info->tags = tags; 5323169699Skan arg_info->types = types; 5324169699Skan arg_info->others = others; 5325169699Skan arg_info->pending_sizes = get_pending_sizes (); 5326169699Skan return arg_info; 532718334Speter} 532818334Speter 532918334Speter/* Get the struct, enum or union (CODE says which) with tag NAME. 5330169699Skan Define the tag as a forward-reference if it is not defined. 5331169699Skan Return a c_typespec structure for the type specifier. */ 533218334Speter 5333169699Skanstruct c_typespec 5334169699Skanparser_xref_tag (enum tree_code code, tree name) 533518334Speter{ 5336169699Skan struct c_typespec ret; 533718334Speter /* If a cross reference is requested, look up the type 533818334Speter already defined for this tag and return it. */ 533918334Speter 5340132730Skan tree ref = lookup_tag (code, name, 0); 534190075Sobrien /* If this is the right type of tag, return what we found. 534290075Sobrien (This reference will be shadowed by shadow_tag later if appropriate.) 534390075Sobrien If this is the wrong type of tag, do not return it. If it was the 5344132730Skan wrong type in the same scope, we will have had an error 5345132730Skan message already; if in a different scope and declaring 534690075Sobrien a name, pending_xref_error will give an error message; but if in a 5347132730Skan different scope and not declaring a name, this tag should 534890075Sobrien shadow the previous declaration of a different type of tag, and 534990075Sobrien this would not work properly if we return the reference found. 535090075Sobrien (For example, with "struct foo" in an outer scope, "union foo;" 535190075Sobrien must shadow that tag with a new one of union type.) */ 5352169699Skan ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref); 535390075Sobrien if (ref && TREE_CODE (ref) == code) 5354169699Skan { 5355169699Skan ret.spec = ref; 5356169699Skan return ret; 5357169699Skan } 535818334Speter 535918334Speter /* If no such tag is yet defined, create a forward-reference node 536018334Speter and record it as the "definition". 536118334Speter When a real declaration of this type is found, 536218334Speter the forward-reference will be altered into a real type. */ 536318334Speter 536418334Speter ref = make_node (code); 536518334Speter if (code == ENUMERAL_TYPE) 536618334Speter { 536718334Speter /* Give the type a default layout like unsigned int 536818334Speter to avoid crashing if it does not get defined. */ 536918334Speter TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node); 537018334Speter TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); 537190075Sobrien TYPE_USER_ALIGN (ref) = 0; 5372169699Skan TYPE_UNSIGNED (ref) = 1; 537318334Speter TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); 537418334Speter TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); 537518334Speter TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); 537618334Speter } 537718334Speter 537818334Speter pushtag (name, ref); 537918334Speter 5380169699Skan ret.spec = ref; 5381169699Skan return ret; 538218334Speter} 5383169699Skan 5384169699Skan/* Get the struct, enum or union (CODE says which) with tag NAME. 5385169699Skan Define the tag as a forward-reference if it is not defined. 5386169699Skan Return a tree for the type. */ 5387169699Skan 5388169699Skantree 5389169699Skanxref_tag (enum tree_code code, tree name) 5390169699Skan{ 5391169699Skan return parser_xref_tag (code, name).spec; 5392169699Skan} 539318334Speter 5394132730Skan/* Make sure that the tag NAME is defined *in the current scope* 539518334Speter at least as a forward reference. 539690075Sobrien CODE says which kind of tag NAME ought to be. */ 539718334Speter 539818334Spetertree 5399132730Skanstart_struct (enum tree_code code, tree name) 540018334Speter{ 5401132730Skan /* If there is already a tag defined at this scope 540218334Speter (as a forward reference), just return it. */ 540318334Speter 540490075Sobrien tree ref = 0; 540518334Speter 540618334Speter if (name != 0) 5407132730Skan ref = lookup_tag (code, name, 1); 540818334Speter if (ref && TREE_CODE (ref) == code) 540918334Speter { 5410161660Skan if (TYPE_SIZE (ref)) 5411169699Skan { 541290075Sobrien if (code == UNION_TYPE) 5413169699Skan error ("redefinition of %<union %E%>", name); 5414169699Skan else 5415169699Skan error ("redefinition of %<struct %E%>", name); 5416132730Skan } 5417161660Skan else if (C_TYPE_BEING_DEFINED (ref)) 5418161660Skan { 5419161660Skan if (code == UNION_TYPE) 5420169699Skan error ("nested redefinition of %<union %E%>", name); 5421169699Skan else 5422169699Skan error ("nested redefinition of %<struct %E%>", name); 5423189824Sdas /* Don't create structures that contain themselves. */ 5424189824Sdas ref = NULL_TREE; 5425161660Skan } 5426117421Skan } 5427189824Sdas 5428189824Sdas /* Otherwise create a forward-reference just so the tag is in scope. */ 5429189824Sdas 5430189824Sdas if (ref == NULL_TREE || TREE_CODE (ref) != code) 5431117421Skan { 5432117421Skan ref = make_node (code); 5433117421Skan pushtag (name, ref); 543418334Speter } 5435132730Skan 543618334Speter C_TYPE_BEING_DEFINED (ref) = 1; 543750397Sobrien TYPE_PACKED (ref) = flag_pack_struct; 543818334Speter return ref; 543918334Speter} 544018334Speter 5441169699Skan/* Process the specs, declarator and width (NULL if omitted) 544218334Speter of a structure component, returning a FIELD_DECL node. 5443132730Skan WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node. 544418334Speter 544518334Speter This is done during the parsing of the struct declaration. 544618334Speter The FIELD_DECL nodes are chained together and the lot of them 544718334Speter are ultimately passed to `build_struct' to make the RECORD_TYPE node. */ 544818334Speter 544918334Spetertree 5450169699Skangrokfield (struct c_declarator *declarator, struct c_declspecs *declspecs, 5451169699Skan tree width) 545218334Speter{ 545318334Speter tree value; 545418334Speter 5455169699Skan if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE 5456169699Skan && width == NULL_TREE) 545790075Sobrien { 5458117421Skan /* This is an unnamed decl. 5459117421Skan 5460117421Skan If we have something of the form "union { list } ;" then this 5461117421Skan is the anonymous union extension. Similarly for struct. 5462117421Skan 5463117421Skan If this is something of the form "struct foo;", then 5464117421Skan If MS extensions are enabled, this is handled as an 5465117421Skan anonymous struct. 5466117421Skan Otherwise this is a forward declaration of a structure tag. 5467117421Skan 5468117421Skan If this is something of the form "foo;" and foo is a TYPE_DECL, then 5469117421Skan If MS extensions are enabled and foo names a structure, then 5470117421Skan again this is an anonymous struct. 5471117421Skan Otherwise this is an error. 5472117421Skan 5473132730Skan Oh what a horrid tangled web we weave. I wonder if MS consciously 5474117421Skan took this from Plan 9 or if it was an accident of implementation 5475117421Skan that took root before someone noticed the bug... */ 5476117421Skan 5477169699Skan tree type = declspecs->type; 5478169699Skan bool type_ok = (TREE_CODE (type) == RECORD_TYPE 5479169699Skan || TREE_CODE (type) == UNION_TYPE); 5480169699Skan bool ok = false; 5481102790Skan 5482169699Skan if (type_ok 5483169699Skan && (flag_ms_extensions || !declspecs->typedef_p)) 548490075Sobrien { 5485117421Skan if (flag_ms_extensions) 5486169699Skan ok = true; 5487117421Skan else if (flag_iso) 5488169699Skan ok = false; 5489117421Skan else if (TYPE_NAME (type) == NULL) 5490169699Skan ok = true; 5491117421Skan else 5492169699Skan ok = false; 5493117421Skan } 5494169699Skan if (!ok) 5495117421Skan { 5496169699Skan pedwarn ("declaration does not declare anything"); 549790075Sobrien return NULL_TREE; 549890075Sobrien } 5499169699Skan if (pedantic) 5500169699Skan pedwarn ("ISO C doesn%'t support unnamed structs/unions"); 550190075Sobrien } 550218334Speter 5503169699Skan value = grokdeclarator (declarator, declspecs, FIELD, false, 5504132730Skan width ? &width : NULL); 550518334Speter 550618334Speter finish_decl (value, NULL_TREE, NULL_TREE); 550796263Sobrien DECL_INITIAL (value) = width; 550818334Speter 550918334Speter return value; 551018334Speter} 551118334Speter 5512132730Skan/* Generate an error for any duplicate field names in FIELDLIST. Munge 5513132730Skan the list such that this does not present a problem later. */ 5514122196Skan 5515132730Skanstatic void 5516132730Skandetect_field_duplicates (tree fieldlist) 5517132730Skan{ 5518132730Skan tree x, y; 5519132730Skan int timeout = 10; 5520122196Skan 5521132730Skan /* First, see if there are more than "a few" fields. 5522132730Skan This is trivially true if there are zero or one fields. */ 5523132730Skan if (!fieldlist) 5524132730Skan return; 5525132730Skan x = TREE_CHAIN (fieldlist); 5526132730Skan if (!x) 5527132730Skan return; 5528132730Skan do { 5529132730Skan timeout--; 5530132730Skan x = TREE_CHAIN (x); 5531132730Skan } while (timeout > 0 && x); 5532132730Skan 5533132730Skan /* If there were "few" fields, avoid the overhead of allocating 5534132730Skan a hash table. Instead just do the nested traversal thing. */ 5535132730Skan if (timeout > 0) 5536132730Skan { 5537132730Skan for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x)) 5538132730Skan if (DECL_NAME (x)) 5539132730Skan { 5540132730Skan for (y = fieldlist; y != x; y = TREE_CHAIN (y)) 5541132730Skan if (DECL_NAME (y) == DECL_NAME (x)) 5542132730Skan { 5543169699Skan error ("duplicate member %q+D", x); 5544132730Skan DECL_NAME (x) = NULL_TREE; 5545132730Skan } 5546132730Skan } 5547132730Skan } 5548132730Skan else 5549132730Skan { 5550132730Skan htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); 5551132730Skan void **slot; 5552132730Skan 5553132730Skan for (x = fieldlist; x ; x = TREE_CHAIN (x)) 5554132730Skan if ((y = DECL_NAME (x)) != 0) 5555132730Skan { 5556132730Skan slot = htab_find_slot (htab, y, INSERT); 5557132730Skan if (*slot) 5558132730Skan { 5559169699Skan error ("duplicate member %q+D", x); 5560132730Skan DECL_NAME (x) = NULL_TREE; 5561132730Skan } 5562132730Skan *slot = y; 5563132730Skan } 5564132730Skan 5565132730Skan htab_delete (htab); 5566132730Skan } 5567122196Skan} 5568132730Skan 556918334Speter/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. 557018334Speter FIELDLIST is a chain of FIELD_DECL nodes for the fields. 557190075Sobrien ATTRIBUTES are attributes to be applied to the structure. */ 557218334Speter 557318334Spetertree 5574132730Skanfinish_struct (tree t, tree fieldlist, tree attributes) 557518334Speter{ 557690075Sobrien tree x; 5577169699Skan bool toplevel = file_scope == current_scope; 557890075Sobrien int saw_named_field; 557918334Speter 558018334Speter /* If this type was previously laid out as a forward reference, 558118334Speter make sure we lay it out again. */ 558218334Speter 558318334Speter TYPE_SIZE (t) = 0; 558418334Speter 558590075Sobrien decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 558618334Speter 558750397Sobrien if (pedantic) 558850397Sobrien { 558950397Sobrien for (x = fieldlist; x; x = TREE_CHAIN (x)) 559050397Sobrien if (DECL_NAME (x) != 0) 559150397Sobrien break; 559218334Speter 559350397Sobrien if (x == 0) 5594169699Skan { 5595169699Skan if (TREE_CODE (t) == UNION_TYPE) 5596169699Skan { 5597169699Skan if (fieldlist) 5598169699Skan pedwarn ("union has no named members"); 5599169699Skan else 5600169699Skan pedwarn ("union has no members"); 5601169699Skan } 5602169699Skan else 5603169699Skan { 5604169699Skan if (fieldlist) 5605169699Skan pedwarn ("struct has no named members"); 5606169699Skan else 5607169699Skan pedwarn ("struct has no members"); 5608169699Skan } 5609169699Skan } 561050397Sobrien } 561150397Sobrien 561296263Sobrien /* Install struct as DECL_CONTEXT of each field decl. 5613169699Skan Also process specified field sizes, found in the DECL_INITIAL, 5614169699Skan storing 0 there after the type has been changed to precision equal 5615169699Skan to its width, rather than the precision of the specified standard 5616169699Skan type. (Correct layout requires the original type to have been preserved 5617169699Skan until now.) */ 561818334Speter 561990075Sobrien saw_named_field = 0; 562018334Speter for (x = fieldlist; x; x = TREE_CHAIN (x)) 562118334Speter { 5622169699Skan if (TREE_TYPE (x) == error_mark_node) 5623169699Skan continue; 5624169699Skan 562518334Speter DECL_CONTEXT (x) = t; 562618334Speter 5627169699Skan if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT) 5628169699Skan DECL_PACKED (x) = 1; 5629169699Skan 563018334Speter /* If any field is const, the structure type is pseudo-const. */ 563118334Speter if (TREE_READONLY (x)) 563218334Speter C_TYPE_FIELDS_READONLY (t) = 1; 563318334Speter else 563418334Speter { 563518334Speter /* A field that is pseudo-const makes the structure likewise. */ 563618334Speter tree t1 = TREE_TYPE (x); 563718334Speter while (TREE_CODE (t1) == ARRAY_TYPE) 563818334Speter t1 = TREE_TYPE (t1); 563918334Speter if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE) 564018334Speter && C_TYPE_FIELDS_READONLY (t1)) 564118334Speter C_TYPE_FIELDS_READONLY (t) = 1; 564218334Speter } 564318334Speter 564418334Speter /* Any field that is volatile means variables of this type must be 564518334Speter treated in some ways as volatile. */ 564618334Speter if (TREE_THIS_VOLATILE (x)) 564718334Speter C_TYPE_FIELDS_VOLATILE (t) = 1; 564818334Speter 564918334Speter /* Any field of nominal variable size implies structure is too. */ 565018334Speter if (C_DECL_VARIABLE_SIZE (x)) 565118334Speter C_TYPE_VARIABLE_SIZE (t) = 1; 565218334Speter 565396263Sobrien if (DECL_INITIAL (x)) 565418334Speter { 5655132730Skan unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1); 5656132730Skan DECL_SIZE (x) = bitsize_int (width); 5657132730Skan DECL_BIT_FIELD (x) = 1; 5658132730Skan SET_DECL_C_BIT_FIELD (x); 565996263Sobrien } 566096263Sobrien 566190075Sobrien /* Detect flexible array member in an invalid context. */ 566290075Sobrien if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 566390075Sobrien && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 566490075Sobrien && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 566590075Sobrien && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 566618334Speter { 566790075Sobrien if (TREE_CODE (t) == UNION_TYPE) 5668132730Skan { 5669132730Skan error ("%Jflexible array member in union", x); 5670132730Skan TREE_TYPE (x) = error_mark_node; 5671132730Skan } 567290075Sobrien else if (TREE_CHAIN (x) != NULL_TREE) 5673132730Skan { 5674132730Skan error ("%Jflexible array member not at end of struct", x); 5675132730Skan TREE_TYPE (x) = error_mark_node; 5676132730Skan } 5677169699Skan else if (!saw_named_field) 5678132730Skan { 5679132730Skan error ("%Jflexible array member in otherwise empty struct", x); 5680132730Skan TREE_TYPE (x) = error_mark_node; 5681132730Skan } 568218334Speter } 5683117421Skan 5684132730Skan if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE 5685117421Skan && flexible_array_type_p (TREE_TYPE (x))) 5686132730Skan pedwarn ("%Jinvalid use of structure with flexible array member", x); 5687117421Skan 568890075Sobrien if (DECL_NAME (x)) 568990075Sobrien saw_named_field = 1; 569018334Speter } 569118334Speter 5692132730Skan detect_field_duplicates (fieldlist); 569390075Sobrien 569418334Speter /* Now we have the nearly final fieldlist. Record it, 569518334Speter then lay out the structure or union (including the fields). */ 569618334Speter 569718334Speter TYPE_FIELDS (t) = fieldlist; 569818334Speter 569918334Speter layout_type (t); 570018334Speter 5701169699Skan /* Give bit-fields their proper types. */ 570290075Sobrien { 570390075Sobrien tree *fieldlistp = &fieldlist; 570490075Sobrien while (*fieldlistp) 5705169699Skan if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp) 5706169699Skan && TREE_TYPE (*fieldlistp) != error_mark_node) 5707169699Skan { 5708169699Skan unsigned HOST_WIDE_INT width 5709169699Skan = tree_low_cst (DECL_INITIAL (*fieldlistp), 1); 5710169699Skan tree type = TREE_TYPE (*fieldlistp); 5711169699Skan if (width != TYPE_PRECISION (type)) 5712169699Skan { 5713169699Skan TREE_TYPE (*fieldlistp) 5714169699Skan = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type)); 5715169699Skan DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp)); 5716169699Skan } 5717169699Skan DECL_INITIAL (*fieldlistp) = 0; 5718169699Skan } 571990075Sobrien else 572090075Sobrien fieldlistp = &TREE_CHAIN (*fieldlistp); 572190075Sobrien } 572218334Speter 572390075Sobrien /* Now we have the truly final field list. 572490075Sobrien Store it in this type and in the variants. */ 572518334Speter 572618334Speter TYPE_FIELDS (t) = fieldlist; 572718334Speter 5728122196Skan /* If there are lots of fields, sort so we can look through them fast. 5729132730Skan We arbitrarily consider 16 or more elts to be "a lot". */ 5730122196Skan 5731122196Skan { 5732122196Skan int len = 0; 5733122196Skan 5734122196Skan for (x = fieldlist; x; x = TREE_CHAIN (x)) 5735122196Skan { 5736169699Skan if (len > 15 || DECL_NAME (x) == NULL) 5737169699Skan break; 5738169699Skan len += 1; 5739122196Skan } 5740122196Skan 5741122196Skan if (len > 15) 5742122196Skan { 5743169699Skan tree *field_array; 5744169699Skan struct lang_type *space; 5745169699Skan struct sorted_fields_type *space2; 5746132730Skan 5747169699Skan len += list_length (x); 5748132730Skan 5749169699Skan /* Use the same allocation policy here that make_node uses, to 5750169699Skan ensure that this lives as long as the rest of the struct decl. 5751169699Skan All decls in an inline function need to be saved. */ 5752132730Skan 5753169699Skan space = GGC_CNEW (struct lang_type); 5754169699Skan space2 = GGC_NEWVAR (struct sorted_fields_type, 5755169699Skan sizeof (struct sorted_fields_type) + len * sizeof (tree)); 5756132730Skan 5757169699Skan len = 0; 5758132730Skan space->s = space2; 5759132730Skan field_array = &space2->elts[0]; 5760169699Skan for (x = fieldlist; x; x = TREE_CHAIN (x)) 5761169699Skan { 5762169699Skan field_array[len++] = x; 5763132730Skan 5764169699Skan /* If there is anonymous struct or union, break out of the loop. */ 5765169699Skan if (DECL_NAME (x) == NULL) 5766169699Skan break; 5767169699Skan } 5768169699Skan /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */ 5769169699Skan if (x == NULL) 5770169699Skan { 5771169699Skan TYPE_LANG_SPECIFIC (t) = space; 5772169699Skan TYPE_LANG_SPECIFIC (t)->s->len = len; 5773169699Skan field_array = TYPE_LANG_SPECIFIC (t)->s->elts; 5774169699Skan qsort (field_array, len, sizeof (tree), field_decl_cmp); 5775169699Skan } 5776122196Skan } 5777122196Skan } 5778132730Skan 577918334Speter for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) 578018334Speter { 578118334Speter TYPE_FIELDS (x) = TYPE_FIELDS (t); 578218334Speter TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t); 5783161660Skan C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t); 5784161660Skan C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t); 5785161660Skan C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t); 578618334Speter } 578718334Speter 578818334Speter /* If this was supposed to be a transparent union, but we can't 578918334Speter make it one, warn and turn off the flag. */ 579018334Speter if (TREE_CODE (t) == UNION_TYPE 579118334Speter && TYPE_TRANSPARENT_UNION (t) 5792146906Skan && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))) 579318334Speter { 579418334Speter TYPE_TRANSPARENT_UNION (t) = 0; 5795169699Skan warning (0, "union cannot be made transparent"); 579618334Speter } 579718334Speter 579818334Speter /* If this structure or union completes the type of any previous 579918334Speter variable declaration, lay it out and output its rtl. */ 5800132730Skan for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)); 5801132730Skan x; 5802132730Skan x = TREE_CHAIN (x)) 580318334Speter { 5804132730Skan tree decl = TREE_VALUE (x); 5805132730Skan if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) 5806132730Skan layout_array_type (TREE_TYPE (decl)); 5807132730Skan if (TREE_CODE (decl) != TYPE_DECL) 5808132730Skan { 5809132730Skan layout_decl (decl, 0); 5810132730Skan if (c_dialect_objc ()) 5811132730Skan objc_check_decl (decl); 5812169699Skan rest_of_decl_compilation (decl, toplevel, 0); 5813169699Skan if (!toplevel) 5814132730Skan expand_decl (decl); 581518334Speter } 581618334Speter } 5817132730Skan C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0; 581818334Speter 581918334Speter /* Finish debugging output for this type. */ 582018334Speter rest_of_type_compilation (t, toplevel); 582118334Speter 5822169699Skan /* If we're inside a function proper, i.e. not file-scope and not still 5823169699Skan parsing parameters, then arrange for the size of a variable sized type 5824169699Skan to be bound now. */ 5825169699Skan if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE)) 5826169699Skan add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t))); 5827169699Skan 582818334Speter return t; 582918334Speter} 583018334Speter 583118334Speter/* Lay out the type T, and its element type, and so on. */ 583218334Speter 583318334Speterstatic void 5834132730Skanlayout_array_type (tree t) 583518334Speter{ 583618334Speter if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 583718334Speter layout_array_type (TREE_TYPE (t)); 583818334Speter layout_type (t); 583918334Speter} 584018334Speter 584118334Speter/* Begin compiling the definition of an enumeration type. 584218334Speter NAME is its name (or null if anonymous). 584318334Speter Returns the type object, as yet incomplete. 584418334Speter Also records info about it so that build_enumerator 584518334Speter may be used to declare the individual values as they are read. */ 584618334Speter 584718334Spetertree 5848132730Skanstart_enum (tree name) 584918334Speter{ 585090075Sobrien tree enumtype = 0; 585118334Speter 585218334Speter /* If this is the real definition for a previous forward reference, 585318334Speter fill in the contents in the same object that used to be the 585418334Speter forward reference. */ 585518334Speter 585618334Speter if (name != 0) 5857132730Skan enumtype = lookup_tag (ENUMERAL_TYPE, name, 1); 585818334Speter 585918334Speter if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE) 586018334Speter { 586118334Speter enumtype = make_node (ENUMERAL_TYPE); 586218334Speter pushtag (name, enumtype); 586318334Speter } 586418334Speter 5865161660Skan if (C_TYPE_BEING_DEFINED (enumtype)) 5866169699Skan error ("nested redefinition of %<enum %E%>", name); 5867161660Skan 586818334Speter C_TYPE_BEING_DEFINED (enumtype) = 1; 586918334Speter 587018334Speter if (TYPE_VALUES (enumtype) != 0) 587118334Speter { 587218334Speter /* This enum is a named one that has been declared already. */ 5873169699Skan error ("redeclaration of %<enum %E%>", name); 587418334Speter 587518334Speter /* Completely replace its old definition. 587618334Speter The old enumerators remain defined, however. */ 587718334Speter TYPE_VALUES (enumtype) = 0; 587818334Speter } 587918334Speter 588018334Speter enum_next_value = integer_zero_node; 588118334Speter enum_overflow = 0; 588218334Speter 588350397Sobrien if (flag_short_enums) 588450397Sobrien TYPE_PACKED (enumtype) = 1; 588550397Sobrien 588618334Speter return enumtype; 588718334Speter} 588818334Speter 588918334Speter/* After processing and defining all the values of an enumeration type, 589018334Speter install their decls in the enumeration type and finish it off. 589118334Speter ENUMTYPE is the type object, VALUES a list of decl-value pairs, 589218334Speter and ATTRIBUTES are the specified attributes. 589318334Speter Returns ENUMTYPE. */ 589418334Speter 589518334Spetertree 5896132730Skanfinish_enum (tree enumtype, tree values, tree attributes) 589718334Speter{ 589890075Sobrien tree pair, tem; 5899169699Skan tree minnode = 0, maxnode = 0; 590090075Sobrien int precision, unsign; 5901169699Skan bool toplevel = (file_scope == current_scope); 5902169699Skan struct lang_type *lt; 590318334Speter 590490075Sobrien decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 590518334Speter 590618334Speter /* Calculate the maximum value of any enumerator in this type. */ 590718334Speter 590818334Speter if (values == error_mark_node) 590918334Speter minnode = maxnode = integer_zero_node; 591018334Speter else 591190075Sobrien { 591290075Sobrien minnode = maxnode = TREE_VALUE (values); 591390075Sobrien for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair)) 591490075Sobrien { 591590075Sobrien tree value = TREE_VALUE (pair); 591690075Sobrien if (tree_int_cst_lt (maxnode, value)) 591790075Sobrien maxnode = value; 591890075Sobrien if (tree_int_cst_lt (value, minnode)) 591990075Sobrien minnode = value; 592090075Sobrien } 592190075Sobrien } 592218334Speter 592390075Sobrien /* Construct the final type of this enumeration. It is the same 592490075Sobrien as one of the integral types - the narrowest one that fits, except 592590075Sobrien that normally we only go as narrow as int - and signed iff any of 592690075Sobrien the values are negative. */ 592790075Sobrien unsign = (tree_int_cst_sgn (minnode) >= 0); 592890075Sobrien precision = MAX (min_precision (minnode, unsign), 592990075Sobrien min_precision (maxnode, unsign)); 5930169699Skan 593150397Sobrien if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node)) 593250397Sobrien { 5933169699Skan tem = c_common_type_for_size (precision, unsign); 5934169699Skan if (tem == NULL) 593550397Sobrien { 5936169699Skan warning (0, "enumeration values exceed range of largest integer"); 5937169699Skan tem = long_long_integer_type_node; 593850397Sobrien } 593950397Sobrien } 594018334Speter else 5941169699Skan tem = unsign ? unsigned_type_node : integer_type_node; 594218334Speter 5943169699Skan TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem); 5944169699Skan TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem); 5945169699Skan TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem); 594618334Speter TYPE_SIZE (enumtype) = 0; 5947146906Skan 5948146906Skan /* If the precision of the type was specific with an attribute and it 5949146906Skan was too small, give an error. Otherwise, use it. */ 5950146906Skan if (TYPE_PRECISION (enumtype)) 5951146906Skan { 5952146906Skan if (precision > TYPE_PRECISION (enumtype)) 5953146906Skan error ("specified mode too small for enumeral values"); 5954146906Skan } 5955146906Skan else 5956169699Skan TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem); 5957146906Skan 595818334Speter layout_type (enumtype); 595918334Speter 596018334Speter if (values != error_mark_node) 596118334Speter { 596290075Sobrien /* Change the type of the enumerators to be the enum type. We 596390075Sobrien need to do this irrespective of the size of the enum, for 596490075Sobrien proper type checking. Replace the DECL_INITIALs of the 596590075Sobrien enumerators, and the value slots of the list, with copies 596690075Sobrien that have the enum type; they cannot be modified in place 596790075Sobrien because they may be shared (e.g. integer_zero_node) Finally, 596890075Sobrien change the purpose slots to point to the names of the decls. */ 596918334Speter for (pair = values; pair; pair = TREE_CHAIN (pair)) 597018334Speter { 597190075Sobrien tree enu = TREE_PURPOSE (pair); 5972169699Skan tree ini = DECL_INITIAL (enu); 597390075Sobrien 597490075Sobrien TREE_TYPE (enu) = enumtype; 597590075Sobrien 597690075Sobrien /* The ISO C Standard mandates enumerators to have type int, 597790075Sobrien even though the underlying type of an enum type is 597890075Sobrien unspecified. Here we convert any enumerators that fit in 597990075Sobrien an int to type int, to avoid promotions to unsigned types 598090075Sobrien when comparing integers with enumerators that fit in the 598190075Sobrien int range. When -pedantic is given, build_enumerator() 598290075Sobrien would have already taken care of those that don't fit. */ 5983169699Skan if (int_fits_type_p (ini, integer_type_node)) 5984169699Skan tem = integer_type_node; 598590075Sobrien else 5986169699Skan tem = enumtype; 5987169699Skan ini = convert (tem, ini); 598890075Sobrien 5989169699Skan DECL_INITIAL (enu) = ini; 599090075Sobrien TREE_PURPOSE (pair) = DECL_NAME (enu); 5991169699Skan TREE_VALUE (pair) = ini; 599218334Speter } 599318334Speter 599418334Speter TYPE_VALUES (enumtype) = values; 599518334Speter } 599618334Speter 5997169699Skan /* Record the min/max values so that we can warn about bit-field 5998169699Skan enumerations that are too small for the values. */ 5999169699Skan lt = GGC_CNEW (struct lang_type); 6000169699Skan lt->enum_min = minnode; 6001169699Skan lt->enum_max = maxnode; 6002169699Skan TYPE_LANG_SPECIFIC (enumtype) = lt; 6003169699Skan 600418334Speter /* Fix up all variant types of this enum type. */ 600518334Speter for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem)) 600618334Speter { 600790075Sobrien if (tem == enumtype) 600890075Sobrien continue; 600918334Speter TYPE_VALUES (tem) = TYPE_VALUES (enumtype); 601018334Speter TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype); 601118334Speter TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype); 601218334Speter TYPE_SIZE (tem) = TYPE_SIZE (enumtype); 601350397Sobrien TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype); 601418334Speter TYPE_MODE (tem) = TYPE_MODE (enumtype); 601518334Speter TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype); 601618334Speter TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype); 601790075Sobrien TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype); 6018169699Skan TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype); 6019169699Skan TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype); 602018334Speter } 602118334Speter 602218334Speter /* Finish debugging output for this type. */ 602318334Speter rest_of_type_compilation (enumtype, toplevel); 602418334Speter 602518334Speter return enumtype; 602618334Speter} 602718334Speter 602818334Speter/* Build and install a CONST_DECL for one value of the 602918334Speter current enumeration type (one that was begun with start_enum). 603018334Speter Return a tree-list containing the CONST_DECL and its value. 603118334Speter Assignment of sequential values by default is handled here. */ 603218334Speter 603318334Spetertree 6034132730Skanbuild_enumerator (tree name, tree value) 603518334Speter{ 603690075Sobrien tree decl, type; 603718334Speter 603818334Speter /* Validate and default VALUE. */ 603918334Speter 604018334Speter if (value != 0) 604118334Speter { 6042169699Skan /* Don't issue more errors for error_mark_node (i.e. an 6043169699Skan undeclared identifier) - just ignore the value expression. */ 6044169699Skan if (value == error_mark_node) 6045169699Skan value = 0; 6046169699Skan else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)) 6047169699Skan || TREE_CODE (value) != INTEGER_CST) 604818334Speter { 6049169699Skan error ("enumerator value for %qE is not an integer constant", name); 6050169699Skan value = 0; 605118334Speter } 605218334Speter else 605318334Speter { 6054169699Skan value = default_conversion (value); 6055169699Skan constant_expression_warning (value); 605618334Speter } 605718334Speter } 605818334Speter 605918334Speter /* Default based on previous value. */ 606018334Speter /* It should no longer be possible to have NON_LVALUE_EXPR 606118334Speter in the default. */ 606218334Speter if (value == 0) 606318334Speter { 606418334Speter value = enum_next_value; 606518334Speter if (enum_overflow) 606618334Speter error ("overflow in enumeration values"); 606718334Speter } 606818334Speter 6069169699Skan if (pedantic && !int_fits_type_p (value, integer_type_node)) 607018334Speter { 6071169699Skan pedwarn ("ISO C restricts enumerator values to range of %<int%>"); 6072169699Skan /* XXX This causes -pedantic to change the meaning of the program. 6073169699Skan Remove? -zw 2004-03-15 */ 607490075Sobrien value = convert (integer_type_node, value); 607518334Speter } 607618334Speter 607718334Speter /* Set basis for default for next value. */ 607818334Speter enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0); 607918334Speter enum_overflow = tree_int_cst_lt (enum_next_value, value); 608018334Speter 608118334Speter /* Now create a declaration for the enum value name. */ 608218334Speter 608318334Speter type = TREE_TYPE (value); 6084117421Skan type = c_common_type_for_size (MAX (TYPE_PRECISION (type), 6085117421Skan TYPE_PRECISION (integer_type_node)), 6086117421Skan (TYPE_PRECISION (type) 6087117421Skan >= TYPE_PRECISION (integer_type_node) 6088169699Skan && TYPE_UNSIGNED (type))); 608918334Speter 609018334Speter decl = build_decl (CONST_DECL, name, type); 609190075Sobrien DECL_INITIAL (decl) = convert (type, value); 609218334Speter pushdecl (decl); 609318334Speter 609490075Sobrien return tree_cons (decl, value, NULL_TREE); 609518334Speter} 609690075Sobrien 609718334Speter 609818334Speter/* Create the FUNCTION_DECL for a function definition. 609990075Sobrien DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of 610018334Speter the declaration; they describe the function's name and the type it returns, 610118334Speter but twisted together in a fashion that parallels the syntax of C. 610218334Speter 610318334Speter This function creates a binding context for the function body 610418334Speter as well as setting up the FUNCTION_DECL in current_function_decl. 610518334Speter 610618334Speter Returns 1 on success. If the DECLARATOR is not suitable for a function 610718334Speter (it defines a datum instead), we return 0, which tells 610890075Sobrien yyparse to report a parse error. */ 610918334Speter 611018334Speterint 6111169699Skanstart_function (struct c_declspecs *declspecs, struct c_declarator *declarator, 6112169699Skan tree attributes) 611318334Speter{ 611418334Speter tree decl1, old_decl; 6115169699Skan tree restype, resdecl; 6116169699Skan struct c_label_context_se *nstack_se; 6117169699Skan struct c_label_context_vm *nstack_vm; 611818334Speter 611950397Sobrien current_function_returns_value = 0; /* Assume, until we see it does. */ 612018334Speter current_function_returns_null = 0; 612196263Sobrien current_function_returns_abnormally = 0; 612218334Speter warn_about_return_type = 0; 6123169699Skan c_switch_stack = NULL; 612418334Speter 6125169699Skan nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se); 6126169699Skan nstack_se->labels_def = NULL; 6127169699Skan nstack_se->labels_used = NULL; 6128169699Skan nstack_se->next = label_context_stack_se; 6129169699Skan label_context_stack_se = nstack_se; 613018334Speter 6131169699Skan nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm); 6132169699Skan nstack_vm->labels_def = NULL; 6133169699Skan nstack_vm->labels_used = NULL; 6134169699Skan nstack_vm->scope = 0; 6135169699Skan nstack_vm->next = label_context_stack_vm; 6136169699Skan label_context_stack_vm = nstack_vm; 613718334Speter 6138169699Skan /* Indicate no valid break/continue context by setting these variables 6139169699Skan to some non-null, non-label value. We'll notice and emit the proper 6140169699Skan error message in c_finish_bc_stmt. */ 6141169699Skan c_break_label = c_cont_label = size_zero_node; 6142169699Skan 6143169699Skan decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL); 6144169699Skan 614518334Speter /* If the declarator is not suitable for a function definition, 614618334Speter cause a syntax error. */ 614718334Speter if (decl1 == 0) 614850397Sobrien { 6149169699Skan label_context_stack_se = label_context_stack_se->next; 6150169699Skan label_context_stack_vm = label_context_stack_vm->next; 615150397Sobrien return 0; 615250397Sobrien } 615318334Speter 615490075Sobrien decl_attributes (&decl1, attributes, 0); 615518334Speter 615690075Sobrien if (DECL_DECLARED_INLINE_P (decl1) 615790075Sobrien && DECL_UNINLINABLE (decl1) 615890075Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1))) 6159169699Skan warning (OPT_Wattributes, "inline function %q+D given attribute noinline", 6160169699Skan decl1); 616190075Sobrien 6162189824Sdas /* Handle gnu_inline attribute. */ 6163189824Sdas if (declspecs->inline_p 6164189824Sdas && !flag_gnu89_inline 6165189824Sdas && TREE_CODE (decl1) == FUNCTION_DECL 6166189824Sdas && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))) 6167189824Sdas { 6168189824Sdas if (declspecs->storage_class != csc_static) 6169189824Sdas DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1); 6170189824Sdas } 6171189824Sdas 617218334Speter announce_function (decl1); 617318334Speter 617490075Sobrien if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1)))) 617518334Speter { 617690075Sobrien error ("return type is an incomplete type"); 617718334Speter /* Make it return void instead. */ 617818334Speter TREE_TYPE (decl1) 617918334Speter = build_function_type (void_type_node, 618018334Speter TYPE_ARG_TYPES (TREE_TYPE (decl1))); 618118334Speter } 618218334Speter 618318334Speter if (warn_about_return_type) 6184169699Skan pedwarn_c99 ("return type defaults to %<int%>"); 618518334Speter 618618334Speter /* Make the init_value nonzero so pushdecl knows this is not tentative. 6187169699Skan error_mark_node is replaced below (in pop_scope) with the BLOCK. */ 618818334Speter DECL_INITIAL (decl1) = error_mark_node; 618918334Speter 619018334Speter /* If this definition isn't a prototype and we had a prototype declaration 6191169699Skan before, copy the arg type info from that prototype. */ 6192169699Skan old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope); 6193169699Skan if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL) 6194169699Skan old_decl = 0; 6195169699Skan current_function_prototype_locus = UNKNOWN_LOCATION; 6196169699Skan current_function_prototype_built_in = false; 6197169699Skan current_function_prototype_arg_types = NULL_TREE; 6198169699Skan if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0) 619918334Speter { 6200169699Skan if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE 6201169699Skan && comptypes (TREE_TYPE (TREE_TYPE (decl1)), 6202169699Skan TREE_TYPE (TREE_TYPE (old_decl)))) 6203169699Skan { 6204169699Skan TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl), 6205169699Skan TREE_TYPE (decl1)); 6206169699Skan current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl); 6207169699Skan current_function_prototype_built_in 6208169699Skan = C_DECL_BUILTIN_PROTOTYPE (old_decl); 6209169699Skan current_function_prototype_arg_types 6210169699Skan = TYPE_ARG_TYPES (TREE_TYPE (decl1)); 6211169699Skan } 6212169699Skan if (TREE_PUBLIC (decl1)) 6213169699Skan { 6214169699Skan /* If there is an external prototype declaration of this 6215169699Skan function, record its location but do not copy information 6216169699Skan to this decl. This may be an invisible declaration 6217169699Skan (built-in or in a scope which has finished) or simply 6218169699Skan have more refined argument types than any declaration 6219169699Skan found above. */ 6220169699Skan struct c_binding *b; 6221169699Skan for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed) 6222169699Skan if (B_IN_SCOPE (b, external_scope)) 6223169699Skan break; 6224169699Skan if (b) 6225169699Skan { 6226169699Skan tree ext_decl, ext_type; 6227169699Skan ext_decl = b->decl; 6228169699Skan ext_type = b->type ? b->type : TREE_TYPE (ext_decl); 6229169699Skan if (TREE_CODE (ext_type) == FUNCTION_TYPE 6230169699Skan && comptypes (TREE_TYPE (TREE_TYPE (decl1)), 6231169699Skan TREE_TYPE (ext_type))) 6232169699Skan { 6233169699Skan current_function_prototype_locus 6234169699Skan = DECL_SOURCE_LOCATION (ext_decl); 6235169699Skan current_function_prototype_built_in 6236169699Skan = C_DECL_BUILTIN_PROTOTYPE (ext_decl); 6237169699Skan current_function_prototype_arg_types 6238169699Skan = TYPE_ARG_TYPES (ext_type); 6239169699Skan } 6240169699Skan } 6241169699Skan } 624218334Speter } 624318334Speter 624418334Speter /* Optionally warn of old-fashioned def with no previous prototype. */ 624518334Speter if (warn_strict_prototypes 6246169699Skan && old_decl != error_mark_node 624718334Speter && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0 6248132730Skan && C_DECL_ISNT_PROTOTYPE (old_decl)) 6249169699Skan warning (OPT_Wstrict_prototypes, 6250169699Skan "function declaration isn%'t a prototype"); 625118334Speter /* Optionally warn of any global def with no previous prototype. */ 625218334Speter else if (warn_missing_prototypes 6253169699Skan && old_decl != error_mark_node 625418334Speter && TREE_PUBLIC (decl1) 6255169699Skan && !MAIN_NAME_P (DECL_NAME (decl1)) 6256132730Skan && C_DECL_ISNT_PROTOTYPE (old_decl)) 6257169699Skan warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1); 625818334Speter /* Optionally warn of any def with no previous prototype 625918334Speter if the function has already been used. */ 626018334Speter else if (warn_missing_prototypes 6261169699Skan && old_decl != 0 6262169699Skan && old_decl != error_mark_node 6263169699Skan && TREE_USED (old_decl) 626418334Speter && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0) 6265169699Skan warning (OPT_Wmissing_prototypes, 6266169699Skan "%q+D was used with no prototype before its definition", decl1); 626718334Speter /* Optionally warn of any global def with no previous declaration. */ 626818334Speter else if (warn_missing_declarations 626918334Speter && TREE_PUBLIC (decl1) 627018334Speter && old_decl == 0 6271169699Skan && !MAIN_NAME_P (DECL_NAME (decl1))) 6272169699Skan warning (OPT_Wmissing_declarations, "no previous declaration for %q+D", 6273169699Skan decl1); 627418334Speter /* Optionally warn of any def with no previous declaration 627518334Speter if the function has already been used. */ 627618334Speter else if (warn_missing_declarations 6277169699Skan && old_decl != 0 6278169699Skan && old_decl != error_mark_node 6279169699Skan && TREE_USED (old_decl) 6280132730Skan && C_DECL_IMPLICIT (old_decl)) 6281169699Skan warning (OPT_Wmissing_declarations, 6282169699Skan "%q+D was used with no declaration before its definition", decl1); 628318334Speter 628418334Speter /* This function exists in static storage. 628518334Speter (This does not mean `static' in the C sense!) */ 628618334Speter TREE_STATIC (decl1) = 1; 628718334Speter 628818334Speter /* A nested function is not global. */ 628918334Speter if (current_function_decl != 0) 629018334Speter TREE_PUBLIC (decl1) = 0; 629118334Speter 6292132730Skan /* This is the earliest point at which we might know the assembler 6293132730Skan name of the function. Thus, if it's set before this, die horribly. */ 6294169699Skan gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1)); 6295132730Skan 6296132730Skan /* If #pragma weak was used, mark the decl weak now. */ 6297169699Skan if (current_scope == file_scope) 6298132730Skan maybe_apply_pragma_weak (decl1); 6299132730Skan 630090075Sobrien /* Warn for unlikely, improbable, or stupid declarations of `main'. */ 630190075Sobrien if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1))) 630250397Sobrien { 630350397Sobrien tree args; 630450397Sobrien int argct = 0; 630550397Sobrien 630650397Sobrien if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1))) 630790075Sobrien != integer_type_node) 6308169699Skan pedwarn ("return type of %q+D is not %<int%>", decl1); 630950397Sobrien 631050397Sobrien for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args; 631150397Sobrien args = TREE_CHAIN (args)) 631250397Sobrien { 631350397Sobrien tree type = args ? TREE_VALUE (args) : 0; 631450397Sobrien 631550397Sobrien if (type == void_type_node) 631650397Sobrien break; 631750397Sobrien 631850397Sobrien ++argct; 631950397Sobrien switch (argct) 632050397Sobrien { 632150397Sobrien case 1: 632250397Sobrien if (TYPE_MAIN_VARIANT (type) != integer_type_node) 6323169699Skan pedwarn ("first argument of %q+D should be %<int%>", decl1); 632450397Sobrien break; 632550397Sobrien 632650397Sobrien case 2: 632750397Sobrien if (TREE_CODE (type) != POINTER_TYPE 632850397Sobrien || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 632950397Sobrien || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 633050397Sobrien != char_type_node)) 6331169699Skan pedwarn ("second argument of %q+D should be %<char **%>", 6332169699Skan decl1); 633350397Sobrien break; 633450397Sobrien 633550397Sobrien case 3: 633650397Sobrien if (TREE_CODE (type) != POINTER_TYPE 633750397Sobrien || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 633850397Sobrien || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 633950397Sobrien != char_type_node)) 6340169699Skan pedwarn ("third argument of %q+D should probably be " 6341169699Skan "%<char **%>", decl1); 634250397Sobrien break; 634350397Sobrien } 634450397Sobrien } 634550397Sobrien 634650397Sobrien /* It is intentional that this message does not mention the third 634790075Sobrien argument because it's only mentioned in an appendix of the 634890075Sobrien standard. */ 634950397Sobrien if (argct > 0 && (argct < 2 || argct > 3)) 6350169699Skan pedwarn ("%q+D takes only zero or two arguments", decl1); 635150397Sobrien 6352169699Skan if (!TREE_PUBLIC (decl1)) 6353169699Skan pedwarn ("%q+D is normally a non-static function", decl1); 635450397Sobrien } 635550397Sobrien 635618334Speter /* Record the decl so that the function name is defined. 635718334Speter If we already have a decl for this name, and it is a FUNCTION_DECL, 635818334Speter use the old decl. */ 635918334Speter 636018334Speter current_function_decl = pushdecl (decl1); 636118334Speter 6362169699Skan push_scope (); 6363132730Skan declare_parm_level (); 636418334Speter 636518334Speter restype = TREE_TYPE (TREE_TYPE (current_function_decl)); 636618334Speter /* Promote the value to int before returning it. */ 636790075Sobrien if (c_promoting_integer_type_p (restype)) 636818334Speter { 6369117421Skan /* It retains unsignedness if not really getting wider. */ 6370169699Skan if (TYPE_UNSIGNED (restype) 6371117421Skan && (TYPE_PRECISION (restype) 6372117421Skan == TYPE_PRECISION (integer_type_node))) 637318334Speter restype = unsigned_type_node; 637418334Speter else 637518334Speter restype = integer_type_node; 637618334Speter } 637718334Speter 6378169699Skan resdecl = build_decl (RESULT_DECL, NULL_TREE, restype); 6379169699Skan DECL_ARTIFICIAL (resdecl) = 1; 6380169699Skan DECL_IGNORED_P (resdecl) = 1; 6381169699Skan DECL_RESULT (current_function_decl) = resdecl; 638218334Speter 638390075Sobrien start_fname_decls (); 6384132730Skan 638518334Speter return 1; 638618334Speter} 638718334Speter 6388132730Skan/* Subroutine of store_parm_decls which handles new-style function 6389132730Skan definitions (prototype format). The parms already have decls, so we 6390132730Skan need only record them as in effect and complain if any redundant 6391132730Skan old-style parm decls were written. */ 6392132730Skanstatic void 6393169699Skanstore_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info) 639418334Speter{ 6395169699Skan tree decl; 639618334Speter 6397169699Skan if (current_scope->bindings) 6398132730Skan { 6399132730Skan error ("%Jold-style parameter declarations in prototyped " 6400132730Skan "function definition", fndecl); 640118334Speter 6402132730Skan /* Get rid of the old-style declarations. */ 6403169699Skan pop_scope (); 6404169699Skan push_scope (); 6405132730Skan } 6406169699Skan /* Don't issue this warning for nested functions, and don't issue this 6407169699Skan warning if we got here because ARG_INFO_TYPES was error_mark_node 6408169699Skan (this happens when a function definition has just an ellipsis in 6409169699Skan its parameter list). */ 6410169699Skan else if (!in_system_header && !current_function_scope 6411169699Skan && arg_info->types != error_mark_node) 6412169699Skan warning (OPT_Wtraditional, 6413169699Skan "%Jtraditional C rejects ISO C style function definitions", 6414169699Skan fndecl); 641518334Speter 6416132730Skan /* Now make all the parameter declarations visible in the function body. 6417132730Skan We can bypass most of the grunt work of pushdecl. */ 6418169699Skan for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl)) 6419132730Skan { 6420132730Skan DECL_CONTEXT (decl) = current_function_decl; 6421169699Skan if (DECL_NAME (decl)) 6422132730Skan { 6423169699Skan bind (DECL_NAME (decl), decl, current_scope, 6424169699Skan /*invisible=*/false, /*nested=*/false); 6425169699Skan if (!TREE_USED (decl)) 6426169699Skan warn_if_shadowing (decl); 6427132730Skan } 6428169699Skan else 6429169699Skan error ("%Jparameter name omitted", decl); 6430132730Skan } 643118334Speter 6432132730Skan /* Record the parameter list in the function declaration. */ 6433169699Skan DECL_ARGUMENTS (fndecl) = arg_info->parms; 643418334Speter 6435132730Skan /* Now make all the ancillary declarations visible, likewise. */ 6436169699Skan for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl)) 6437132730Skan { 6438132730Skan DECL_CONTEXT (decl) = current_function_decl; 6439169699Skan if (DECL_NAME (decl)) 6440169699Skan bind (DECL_NAME (decl), decl, current_scope, 6441169699Skan /*invisible=*/false, /*nested=*/false); 6442132730Skan } 644390075Sobrien 6444132730Skan /* And all the tag declarations. */ 6445169699Skan for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl)) 6446132730Skan if (TREE_PURPOSE (decl)) 6447169699Skan bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope, 6448169699Skan /*invisible=*/false, /*nested=*/false); 6449132730Skan} 645018334Speter 6451132730Skan/* Subroutine of store_parm_decls which handles old-style function 6452132730Skan definitions (separate parameter list and declarations). */ 645390075Sobrien 6454132730Skanstatic void 6455169699Skanstore_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info) 6456132730Skan{ 6457169699Skan struct c_binding *b; 6458132730Skan tree parm, decl, last; 6459169699Skan tree parmids = arg_info->parms; 6460169699Skan struct pointer_set_t *seen_args = pointer_set_create (); 646190075Sobrien 6462169699Skan if (!in_system_header) 6463169699Skan warning (OPT_Wold_style_definition, "%Jold-style function definition", 6464169699Skan fndecl); 646518334Speter 6466132730Skan /* Match each formal parameter name with its declaration. Save each 6467132730Skan decl in the appropriate TREE_PURPOSE slot of the parmids chain. */ 6468132730Skan for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 6469132730Skan { 6470132730Skan if (TREE_VALUE (parm) == 0) 647118334Speter { 6472132730Skan error ("%Jparameter name missing from parameter list", fndecl); 6473132730Skan TREE_PURPOSE (parm) = 0; 6474132730Skan continue; 647518334Speter } 647618334Speter 6477169699Skan b = I_SYMBOL_BINDING (TREE_VALUE (parm)); 6478169699Skan if (b && B_IN_CURRENT_SCOPE (b)) 647918334Speter { 6480169699Skan decl = b->decl; 6481132730Skan /* If we got something other than a PARM_DECL it is an error. */ 6482132730Skan if (TREE_CODE (decl) != PARM_DECL) 6483169699Skan error ("%q+D declared as a non-parameter", decl); 6484132730Skan /* If the declaration is already marked, we have a duplicate 6485132730Skan name. Complain and ignore the duplicate. */ 6486169699Skan else if (pointer_set_contains (seen_args, decl)) 648718334Speter { 6488169699Skan error ("multiple parameters named %q+D", decl); 6489132730Skan TREE_PURPOSE (parm) = 0; 6490132730Skan continue; 649118334Speter } 6492132730Skan /* If the declaration says "void", complain and turn it into 6493132730Skan an int. */ 6494132730Skan else if (VOID_TYPE_P (TREE_TYPE (decl))) 649518334Speter { 6496169699Skan error ("parameter %q+D declared with void type", decl); 6497132730Skan TREE_TYPE (decl) = integer_type_node; 6498132730Skan DECL_ARG_TYPE (decl) = integer_type_node; 6499132730Skan layout_decl (decl, 0); 650018334Speter } 6501169699Skan warn_if_shadowing (decl); 650218334Speter } 6503132730Skan /* If no declaration found, default to int. */ 6504132730Skan else 650518334Speter { 6506132730Skan decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node); 6507132730Skan DECL_ARG_TYPE (decl) = TREE_TYPE (decl); 6508132730Skan DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl); 6509132730Skan pushdecl (decl); 6510169699Skan warn_if_shadowing (decl); 651118334Speter 6512132730Skan if (flag_isoc99) 6513169699Skan pedwarn ("type of %q+D defaults to %<int%>", decl); 6514132730Skan else if (extra_warnings) 6515169699Skan warning (OPT_Wextra, "type of %q+D defaults to %<int%>", decl); 651618334Speter } 651718334Speter 6518132730Skan TREE_PURPOSE (parm) = decl; 6519169699Skan pointer_set_insert (seen_args, decl); 652018334Speter } 652118334Speter 6522132730Skan /* Now examine the parms chain for incomplete declarations 6523132730Skan and declarations with no corresponding names. */ 652418334Speter 6525169699Skan for (b = current_scope->bindings; b; b = b->prev) 6526132730Skan { 6527169699Skan parm = b->decl; 6528169699Skan if (TREE_CODE (parm) != PARM_DECL) 6529169699Skan continue; 6530169699Skan 6531169699Skan if (TREE_TYPE (parm) != error_mark_node 6532169699Skan && !COMPLETE_TYPE_P (TREE_TYPE (parm))) 6533132730Skan { 6534169699Skan error ("parameter %q+D has incomplete type", parm); 6535132730Skan TREE_TYPE (parm) = error_mark_node; 6536132730Skan } 653718334Speter 6538169699Skan if (!pointer_set_contains (seen_args, parm)) 653918334Speter { 6540169699Skan error ("declaration for parameter %q+D but no such parameter", parm); 654118334Speter 6542132730Skan /* Pretend the parameter was not missing. 6543132730Skan This gets us to a standard state and minimizes 6544132730Skan further error messages. */ 6545132730Skan parmids = chainon (parmids, tree_cons (parm, 0, 0)); 6546132730Skan } 6547132730Skan } 654818334Speter 6549132730Skan /* Chain the declarations together in the order of the list of 6550132730Skan names. Store that chain in the function decl, replacing the 6551132730Skan list of names. Update the current scope to match. */ 6552132730Skan DECL_ARGUMENTS (fndecl) = 0; 655318334Speter 6554132730Skan for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 6555132730Skan if (TREE_PURPOSE (parm)) 6556132730Skan break; 6557132730Skan if (parm && TREE_PURPOSE (parm)) 6558132730Skan { 6559132730Skan last = TREE_PURPOSE (parm); 6560132730Skan DECL_ARGUMENTS (fndecl) = last; 656118334Speter 6562132730Skan for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm)) 6563132730Skan if (TREE_PURPOSE (parm)) 6564132730Skan { 6565132730Skan TREE_CHAIN (last) = TREE_PURPOSE (parm); 6566132730Skan last = TREE_PURPOSE (parm); 6567132730Skan } 6568132730Skan TREE_CHAIN (last) = 0; 6569132730Skan } 657018334Speter 6571169699Skan pointer_set_destroy (seen_args); 6572169699Skan 6573132730Skan /* If there was a previous prototype, 6574132730Skan set the DECL_ARG_TYPE of each argument according to 6575132730Skan the type previously specified, and report any mismatches. */ 657618334Speter 6577169699Skan if (current_function_prototype_arg_types) 6578132730Skan { 6579132730Skan tree type; 6580132730Skan for (parm = DECL_ARGUMENTS (fndecl), 6581169699Skan type = current_function_prototype_arg_types; 6582132730Skan parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) 6583132730Skan != void_type_node)); 6584132730Skan parm = TREE_CHAIN (parm), type = TREE_CHAIN (type)) 658518334Speter { 6586132730Skan if (parm == 0 || type == 0 6587132730Skan || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node) 658818334Speter { 6589169699Skan if (current_function_prototype_built_in) 6590169699Skan warning (0, "number of arguments doesn%'t match " 6591169699Skan "built-in prototype"); 6592169699Skan else 6593169699Skan { 6594169699Skan error ("number of arguments doesn%'t match prototype"); 6595169699Skan error ("%Hprototype declaration", 6596169699Skan ¤t_function_prototype_locus); 6597169699Skan } 6598132730Skan break; 659918334Speter } 6600132730Skan /* Type for passing arg must be consistent with that 6601132730Skan declared for the arg. ISO C says we take the unqualified 6602132730Skan type for parameters declared with qualified type. */ 6603169699Skan if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)), 6604169699Skan TYPE_MAIN_VARIANT (TREE_VALUE (type)))) 660518334Speter { 6606132730Skan if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) 6607132730Skan == TYPE_MAIN_VARIANT (TREE_VALUE (type))) 660818334Speter { 6609132730Skan /* Adjust argument to match prototype. E.g. a previous 6610132730Skan `int foo(float);' prototype causes 6611132730Skan `int foo(x) float x; {...}' to be treated like 6612132730Skan `int foo(float x) {...}'. This is particularly 6613132730Skan useful for argument types like uid_t. */ 6614132730Skan DECL_ARG_TYPE (parm) = TREE_TYPE (parm); 661590075Sobrien 6616132730Skan if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl)) 6617132730Skan && INTEGRAL_TYPE_P (TREE_TYPE (parm)) 6618132730Skan && TYPE_PRECISION (TREE_TYPE (parm)) 6619132730Skan < TYPE_PRECISION (integer_type_node)) 6620132730Skan DECL_ARG_TYPE (parm) = integer_type_node; 662190075Sobrien 6622132730Skan if (pedantic) 662318334Speter { 6624169699Skan /* ??? Is it possible to get here with a 6625169699Skan built-in prototype or will it always have 6626169699Skan been diagnosed as conflicting with an 6627169699Skan old-style definition and discarded? */ 6628169699Skan if (current_function_prototype_built_in) 6629169699Skan warning (0, "promoted argument %qD " 6630169699Skan "doesn%'t match built-in prototype", parm); 6631169699Skan else 6632169699Skan { 6633169699Skan pedwarn ("promoted argument %qD " 6634169699Skan "doesn%'t match prototype", parm); 6635169699Skan pedwarn ("%Hprototype declaration", 6636169699Skan ¤t_function_prototype_locus); 6637169699Skan } 663818334Speter } 663918334Speter } 6640132730Skan else 6641132730Skan { 6642169699Skan if (current_function_prototype_built_in) 6643169699Skan warning (0, "argument %qD doesn%'t match " 6644169699Skan "built-in prototype", parm); 6645169699Skan else 6646169699Skan { 6647169699Skan error ("argument %qD doesn%'t match prototype", parm); 6648169699Skan error ("%Hprototype declaration", 6649169699Skan ¤t_function_prototype_locus); 6650169699Skan } 6651132730Skan } 665218334Speter } 665318334Speter } 6654132730Skan TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0; 6655132730Skan } 665618334Speter 6657132730Skan /* Otherwise, create a prototype that would match. */ 665818334Speter 6659132730Skan else 6660132730Skan { 6661132730Skan tree actual = 0, last = 0, type; 6662132730Skan 6663132730Skan for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm)) 666418334Speter { 6665132730Skan type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE); 666618334Speter if (last) 666718334Speter TREE_CHAIN (last) = type; 666818334Speter else 666918334Speter actual = type; 6670132730Skan last = type; 6671132730Skan } 6672132730Skan type = tree_cons (NULL_TREE, void_type_node, NULL_TREE); 6673132730Skan if (last) 6674132730Skan TREE_CHAIN (last) = type; 6675132730Skan else 6676132730Skan actual = type; 667718334Speter 6678132730Skan /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES 6679132730Skan of the type of this function, but we need to avoid having this 6680132730Skan affect the types of other similarly-typed functions, so we must 6681132730Skan first force the generation of an identical (but separate) type 6682132730Skan node for the relevant function type. The new node we create 6683132730Skan will be a variant of the main variant of the original function 6684132730Skan type. */ 668518334Speter 6686169699Skan TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl)); 668718334Speter 6688132730Skan TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual; 6689132730Skan } 6690132730Skan} 669118334Speter 6692169699Skan/* Store parameter declarations passed in ARG_INFO into the current 6693169699Skan function declaration. */ 6694169699Skan 6695169699Skanvoid 6696169699Skanstore_parm_decls_from (struct c_arg_info *arg_info) 6697169699Skan{ 6698169699Skan current_function_arg_info = arg_info; 6699169699Skan store_parm_decls (); 6700169699Skan} 6701169699Skan 6702132730Skan/* Store the parameter declarations into the current function declaration. 6703132730Skan This is called after parsing the parameter declarations, before 6704132730Skan digesting the body of the function. 670518334Speter 6706132730Skan For an old-style definition, construct a prototype out of the old-style 6707132730Skan parameter declarations and inject it into the function's type. */ 670818334Speter 6709132730Skanvoid 6710132730Skanstore_parm_decls (void) 6711132730Skan{ 6712132730Skan tree fndecl = current_function_decl; 6713169699Skan bool proto; 671418334Speter 6715169699Skan /* The argument information block for FNDECL. */ 6716169699Skan struct c_arg_info *arg_info = current_function_arg_info; 6717169699Skan current_function_arg_info = 0; 671818334Speter 6719169699Skan /* True if this definition is written with a prototype. Note: 6720169699Skan despite C99 6.7.5.3p14, we can *not* treat an empty argument 6721169699Skan list in a function definition as equivalent to (void) -- an 6722169699Skan empty argument list specifies the function has no parameters, 6723169699Skan but only (void) sets up a prototype for future calls. */ 6724169699Skan proto = arg_info->types != 0; 6725169699Skan 6726169699Skan if (proto) 6727169699Skan store_parm_decls_newstyle (fndecl, arg_info); 6728132730Skan else 6729169699Skan store_parm_decls_oldstyle (fndecl, arg_info); 6730132730Skan 6731169699Skan /* The next call to push_scope will be a function body. */ 6732132730Skan 6733132730Skan next_is_function_body = true; 6734132730Skan 673518334Speter /* Write a record describing this function definition to the prototypes 673618334Speter file (if requested). */ 673718334Speter 6738169699Skan gen_aux_info_record (fndecl, 1, 0, proto); 673918334Speter 674018334Speter /* Initialize the RTL code for the function. */ 6741132730Skan allocate_struct_function (fndecl); 674218334Speter 674390075Sobrien /* Begin the statement tree for this function. */ 6744169699Skan DECL_SAVED_TREE (fndecl) = push_stmt_list (); 674518334Speter 6746169699Skan /* ??? Insert the contents of the pending sizes list into the function 6747169699Skan to be evaluated. The only reason left to have this is 6748169699Skan void foo(int n, int array[n++]) 6749169699Skan because we throw away the array type in favor of a pointer type, and 6750169699Skan thus won't naturally see the SAVE_EXPR containing the increment. All 6751169699Skan other pending sizes would be handled by gimplify_parameters. */ 6752169699Skan { 6753169699Skan tree t; 6754169699Skan for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t)) 6755169699Skan add_stmt (TREE_VALUE (t)); 6756169699Skan } 675718334Speter 675890075Sobrien /* Even though we're inside a function body, we still don't want to 675990075Sobrien call expand_expr to calculate the size of a variable-sized array. 676090075Sobrien We haven't necessarily assigned RTL to all variables yet, so it's 676190075Sobrien not safe to try to expand expressions involving them. */ 676290075Sobrien cfun->x_dont_save_pending_sizes_p = 1; 676318334Speter} 676418334Speter 6765169699Skan/* Emit diagnostics that require gimple input for detection. Operate on 6766169699Skan FNDECL and all its nested functions. */ 6767169699Skan 6768169699Skanstatic void 6769169699Skanc_gimple_diagnostics_recursively (tree fndecl) 6770169699Skan{ 6771169699Skan struct cgraph_node *cgn; 6772169699Skan 6773169699Skan /* Handle attribute((warn_unused_result)). Relies on gimple input. */ 6774169699Skan c_warn_unused_result (&DECL_SAVED_TREE (fndecl)); 6775169699Skan 6776169699Skan /* Notice when OpenMP structured block constraints are violated. */ 6777169699Skan if (flag_openmp) 6778169699Skan diagnose_omp_structured_block_errors (fndecl); 6779169699Skan 6780169699Skan /* Finalize all nested functions now. */ 6781169699Skan cgn = cgraph_node (fndecl); 6782169699Skan for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested) 6783169699Skan c_gimple_diagnostics_recursively (cgn->decl); 6784169699Skan} 6785169699Skan 678618334Speter/* Finish up a function declaration and compile that function 678718334Speter all the way to assembler language output. The free the storage 678818334Speter for the function definition. 678918334Speter 6790132730Skan This is called after parsing the body of the function definition. */ 679118334Speter 679218334Spetervoid 6793132730Skanfinish_function (void) 679418334Speter{ 679590075Sobrien tree fndecl = current_function_decl; 679618334Speter 6797169699Skan label_context_stack_se = label_context_stack_se->next; 6798169699Skan label_context_stack_vm = label_context_stack_vm->next; 679918334Speter 6800132730Skan if (TREE_CODE (fndecl) == FUNCTION_DECL 6801132730Skan && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))) 6802132730Skan { 6803132730Skan tree args = DECL_ARGUMENTS (fndecl); 6804132730Skan for (; args; args = TREE_CHAIN (args)) 6805169699Skan { 6806169699Skan tree type = TREE_TYPE (args); 6807169699Skan if (INTEGRAL_TYPE_P (type) 6808169699Skan && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 6809169699Skan DECL_ARG_TYPE (args) = integer_type_node; 6810169699Skan } 6811132730Skan } 6812132730Skan 6813132730Skan if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) 6814132730Skan BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; 6815132730Skan 681618334Speter /* Must mark the RESULT_DECL as being in this function. */ 681718334Speter 6818132730Skan if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node) 6819132730Skan DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; 682018334Speter 682190075Sobrien if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted) 682218334Speter { 682318334Speter if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))) 682418334Speter != integer_type_node) 682550397Sobrien { 682652284Sobrien /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned. 682790075Sobrien If warn_main is -1 (-Wno-main) we don't want to be warned. */ 6828132730Skan if (!warn_main) 6829169699Skan pedwarn ("return type of %q+D is not %<int%>", fndecl); 683050397Sobrien } 683118334Speter else 683218334Speter { 6833169699Skan if (flag_isoc99) 6834169699Skan { 6835169699Skan tree stmt = c_finish_return (integer_zero_node); 6836169699Skan#ifdef USE_MAPPED_LOCATION 6837169699Skan /* Hack. We don't want the middle-end to warn that this return 6838169699Skan is unreachable, so we mark its location as special. Using 6839169699Skan UNKNOWN_LOCATION has the problem that it gets clobbered in 6840169699Skan annotate_one_with_locus. A cleaner solution might be to 6841169699Skan ensure ! should_carry_locus_p (stmt), but that needs a flag. 6842169699Skan */ 6843169699Skan SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION); 684490075Sobrien#else 6845169699Skan /* Hack. We don't want the middle-end to warn that this 6846169699Skan return is unreachable, so put the statement on the 6847169699Skan special line 0. */ 6848169699Skan annotate_with_file_line (stmt, input_filename, 0); 684950397Sobrien#endif 6850169699Skan } 685118334Speter } 685218334Speter } 6853132730Skan 6854169699Skan /* Tie off the statement tree for this function. */ 6855169699Skan DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl)); 6856169699Skan 685790075Sobrien finish_fname_decls (); 685818334Speter 685996263Sobrien /* Complain if there's just no return statement. */ 686096263Sobrien if (warn_return_type 686196263Sobrien && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE 686296263Sobrien && !current_function_returns_value && !current_function_returns_null 6863169699Skan /* Don't complain if we are no-return. */ 686496263Sobrien && !current_function_returns_abnormally 686596263Sobrien /* Don't warn for main(). */ 686696263Sobrien && !MAIN_NAME_P (DECL_NAME (fndecl)) 686796263Sobrien /* Or if they didn't actually specify a return type. */ 686896263Sobrien && !C_FUNCTION_IMPLICIT_INT (fndecl) 686996263Sobrien /* Normally, with -Wreturn-type, flow will complain. Unless we're an 687096263Sobrien inline function, as we might never be compiled separately. */ 687196263Sobrien && DECL_INLINE (fndecl)) 6872169699Skan { 6873169699Skan warning (OPT_Wreturn_type, 6874169699Skan "no return statement in function returning non-void"); 6875169699Skan TREE_NO_WARNING (fndecl) = 1; 6876169699Skan } 687796263Sobrien 6878132730Skan /* With just -Wextra, complain only if function returns both with 6879122196Skan and without a value. */ 6880122196Skan if (extra_warnings 6881122196Skan && current_function_returns_value 6882122196Skan && current_function_returns_null) 6883169699Skan warning (OPT_Wextra, "this function may return with or without a value"); 6884122196Skan 6885169699Skan /* Store the end of the function, so that we get good line number 6886169699Skan info for the epilogue. */ 6887169699Skan cfun->function_end_locus = input_location; 688890075Sobrien 6889169699Skan /* If we don't have ctors/dtors sections, and this is a static 6890169699Skan constructor or destructor, it must be recorded now. */ 6891169699Skan if (DECL_STATIC_CONSTRUCTOR (fndecl) 6892169699Skan && !targetm.have_ctors_dtors) 6893169699Skan static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors); 6894169699Skan if (DECL_STATIC_DESTRUCTOR (fndecl) 6895169699Skan && !targetm.have_ctors_dtors) 6896169699Skan static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors); 689790075Sobrien 6898169699Skan /* Finalize the ELF visibility for the function. */ 6899169699Skan c_determine_visibility (fndecl); 690090075Sobrien 6901169699Skan /* Genericize before inlining. Delay genericizing nested functions 6902169699Skan until their parent function is genericized. Since finalizing 6903169699Skan requires GENERIC, delay that as well. */ 6904169699Skan 6905169699Skan if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node 6906169699Skan && !undef_nested_function) 690790075Sobrien { 6908169699Skan if (!decl_function_context (fndecl)) 6909132730Skan { 6910169699Skan c_genericize (fndecl); 6911169699Skan c_gimple_diagnostics_recursively (fndecl); 691290075Sobrien 6913169699Skan /* ??? Objc emits functions after finalizing the compilation unit. 6914169699Skan This should be cleaned up later and this conditional removed. */ 6915169699Skan if (cgraph_global_info_ready) 6916169699Skan { 6917169699Skan c_expand_body (fndecl); 6918169699Skan return; 6919169699Skan } 692090075Sobrien 6921169699Skan cgraph_finalize_function (fndecl, false); 6922169699Skan } 692318334Speter else 6924169699Skan { 6925169699Skan /* Register this function with cgraph just far enough to get it 6926169699Skan added to our parent's nested function list. Handy, since the 6927169699Skan C front end doesn't have such a list. */ 6928169699Skan (void) cgraph_node (fndecl); 6929169699Skan } 693018334Speter } 693190075Sobrien 6932169699Skan if (!decl_function_context (fndecl)) 6933169699Skan undef_nested_function = false; 6934169699Skan 6935169699Skan /* We're leaving the context of this function, so zap cfun. 6936169699Skan It's still in DECL_STRUCT_FUNCTION, and we'll restore it in 6937169699Skan tree_rest_of_compilation. */ 6938169699Skan cfun = NULL; 6939169699Skan current_function_decl = NULL; 6940132730Skan} 694118334Speter 6942169699Skan/* Generate the RTL for the body of FNDECL. */ 6943117421Skan 6944132730Skanvoid 6945132730Skanc_expand_body (tree fndecl) 6946132730Skan{ 6947132730Skan 6948169699Skan if (!DECL_INITIAL (fndecl) 6949169699Skan || DECL_INITIAL (fndecl) == error_mark_node) 6950169699Skan return; 6951169699Skan 6952169699Skan tree_rest_of_compilation (fndecl); 6953169699Skan 6954169699Skan if (DECL_STATIC_CONSTRUCTOR (fndecl) 6955169699Skan && targetm.have_ctors_dtors) 6956169699Skan targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0), 6957169699Skan DEFAULT_INIT_PRIORITY); 6958169699Skan if (DECL_STATIC_DESTRUCTOR (fndecl) 6959169699Skan && targetm.have_ctors_dtors) 6960169699Skan targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0), 6961169699Skan DEFAULT_INIT_PRIORITY); 696290075Sobrien} 696390075Sobrien 696490075Sobrien/* Check the declarations given in a for-loop for satisfying the C99 6965169699Skan constraints. If exactly one such decl is found, return it. */ 6966169699Skan 6967169699Skantree 6968132730Skancheck_for_loop_decls (void) 696990075Sobrien{ 6970169699Skan struct c_binding *b; 6971169699Skan tree one_decl = NULL_TREE; 6972169699Skan int n_decls = 0; 697390075Sobrien 6974169699Skan 697590075Sobrien if (!flag_isoc99) 697618334Speter { 697790075Sobrien /* If we get here, declarations have been used in a for loop without 697890075Sobrien the C99 for loop scope. This doesn't make much sense, so don't 697990075Sobrien allow it. */ 6980169699Skan error ("%<for%> loop initial declaration used outside C99 mode"); 6981169699Skan return NULL_TREE; 698218334Speter } 698390075Sobrien /* C99 subclause 6.8.5 paragraph 3: 698490075Sobrien 698590075Sobrien [#3] The declaration part of a for statement shall only 698690075Sobrien declare identifiers for objects having storage class auto or 698790075Sobrien register. 698890075Sobrien 698990075Sobrien It isn't clear whether, in this sentence, "identifiers" binds to 699090075Sobrien "shall only declare" or to "objects" - that is, whether all identifiers 699190075Sobrien declared must be identifiers for objects, or whether the restriction 699290075Sobrien only applies to those that are. (A question on this in comp.std.c 699390075Sobrien in November 2000 received no answer.) We implement the strictest 699490075Sobrien interpretation, to avoid creating an extension which later causes 699590075Sobrien problems. */ 699690075Sobrien 6997169699Skan for (b = current_scope->bindings; b; b = b->prev) 699890075Sobrien { 6999169699Skan tree id = b->id; 7000169699Skan tree decl = b->decl; 7001132730Skan 7002169699Skan if (!id) 7003169699Skan continue; 7004169699Skan 7005169699Skan switch (TREE_CODE (decl)) 7006169699Skan { 7007169699Skan case VAR_DECL: 7008169699Skan if (TREE_STATIC (decl)) 7009169699Skan error ("declaration of static variable %q+D in %<for%> loop " 7010169699Skan "initial declaration", decl); 7011169699Skan else if (DECL_EXTERNAL (decl)) 7012169699Skan error ("declaration of %<extern%> variable %q+D in %<for%> loop " 7013169699Skan "initial declaration", decl); 7014169699Skan break; 7015169699Skan 7016169699Skan case RECORD_TYPE: 7017169699Skan error ("%<struct %E%> declared in %<for%> loop initial declaration", 7018169699Skan id); 7019169699Skan break; 7020169699Skan case UNION_TYPE: 7021169699Skan error ("%<union %E%> declared in %<for%> loop initial declaration", 7022169699Skan id); 7023169699Skan break; 7024169699Skan case ENUMERAL_TYPE: 7025169699Skan error ("%<enum %E%> declared in %<for%> loop initial declaration", 7026169699Skan id); 7027169699Skan break; 7028169699Skan default: 7029169699Skan error ("declaration of non-variable %q+D in %<for%> loop " 7030169699Skan "initial declaration", decl); 7031169699Skan } 7032169699Skan 7033169699Skan n_decls++; 7034169699Skan one_decl = decl; 703590075Sobrien } 703690075Sobrien 7037169699Skan return n_decls == 1 ? one_decl : NULL_TREE; 703818334Speter} 703918334Speter 704018334Speter/* Save and reinitialize the variables 704118334Speter used during compilation of a C function. */ 704218334Speter 704318334Spetervoid 7044132730Skanc_push_function_context (struct function *f) 704518334Speter{ 7046117421Skan struct language_function *p; 7047169699Skan p = GGC_NEW (struct language_function); 7048117421Skan f->language = p; 704918334Speter 705090075Sobrien p->base.x_stmt_tree = c_stmt_tree; 7051169699Skan p->x_break_label = c_break_label; 7052169699Skan p->x_cont_label = c_cont_label; 7053169699Skan p->x_switch_stack = c_switch_stack; 7054169699Skan p->arg_info = current_function_arg_info; 705518334Speter p->returns_value = current_function_returns_value; 705618334Speter p->returns_null = current_function_returns_null; 705796263Sobrien p->returns_abnormally = current_function_returns_abnormally; 705818334Speter p->warn_about_return_type = warn_about_return_type; 705918334Speter} 706018334Speter 706118334Speter/* Restore the variables used during compilation of a C function. */ 706218334Speter 706318334Spetervoid 7064132730Skanc_pop_function_context (struct function *f) 706518334Speter{ 7066117421Skan struct language_function *p = f->language; 706718334Speter 7068169699Skan if (DECL_STRUCT_FUNCTION (current_function_decl) == 0 706990075Sobrien && DECL_SAVED_TREE (current_function_decl) == NULL_TREE) 707018334Speter { 707118334Speter /* Stop pointing to the local nodes about to be freed. */ 707218334Speter /* But DECL_INITIAL must remain nonzero so we know this 707318334Speter was an actual function definition. */ 707418334Speter DECL_INITIAL (current_function_decl) = error_mark_node; 707518334Speter DECL_ARGUMENTS (current_function_decl) = 0; 707618334Speter } 707718334Speter 707890075Sobrien c_stmt_tree = p->base.x_stmt_tree; 7079169699Skan c_break_label = p->x_break_label; 7080169699Skan c_cont_label = p->x_cont_label; 7081169699Skan c_switch_stack = p->x_switch_stack; 7082169699Skan current_function_arg_info = p->arg_info; 708318334Speter current_function_returns_value = p->returns_value; 708418334Speter current_function_returns_null = p->returns_null; 708596263Sobrien current_function_returns_abnormally = p->returns_abnormally; 708618334Speter warn_about_return_type = p->warn_about_return_type; 708718334Speter 7088117421Skan f->language = NULL; 708918334Speter} 709018334Speter 7091117421Skan/* Copy the DECL_LANG_SPECIFIC data associated with DECL. */ 709218334Speter 709318334Spetervoid 7094132730Skanc_dup_lang_specific_decl (tree decl) 709590075Sobrien{ 709690075Sobrien struct lang_decl *ld; 709790075Sobrien 709890075Sobrien if (!DECL_LANG_SPECIFIC (decl)) 709990075Sobrien return; 710090075Sobrien 7101169699Skan ld = GGC_NEW (struct lang_decl); 7102132730Skan memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl)); 710390075Sobrien DECL_LANG_SPECIFIC (decl) = ld; 710490075Sobrien} 710590075Sobrien 710690075Sobrien/* The functions below are required for functionality of doing 710790075Sobrien function at once processing in the C front end. Currently these 710890075Sobrien functions are not called from anywhere in the C front end, but as 710990075Sobrien these changes continue, that will change. */ 711090075Sobrien 711190075Sobrien/* Returns the stmt_tree (if any) to which statements are currently 711290075Sobrien being added. If there is no active statement-tree, NULL is 711390075Sobrien returned. */ 711490075Sobrien 711590075Sobrienstmt_tree 7116132730Skancurrent_stmt_tree (void) 711790075Sobrien{ 711890075Sobrien return &c_stmt_tree; 711990075Sobrien} 712090075Sobrien 712190075Sobrien/* Nonzero if TYPE is an anonymous union or struct type. Always 0 in 712290075Sobrien C. */ 712390075Sobrien 712490075Sobrienint 7125169699Skananon_aggr_type_p (tree ARG_UNUSED (node)) 712618334Speter{ 712790075Sobrien return 0; 712818334Speter} 712990075Sobrien 7130132730Skan/* Return the global value of T as a symbol. */ 713190075Sobrien 713290075Sobrientree 7133132730Skanidentifier_global_value (tree t) 713490075Sobrien{ 7135169699Skan struct c_binding *b; 7136132730Skan 7137169699Skan for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed) 7138169699Skan if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b)) 7139169699Skan return b->decl; 7140132730Skan 7141132730Skan return 0; 714290075Sobrien} 714390075Sobrien 714490075Sobrien/* Record a builtin type for C. If NAME is non-NULL, it is the name used; 714590075Sobrien otherwise the name is found in ridpointers from RID_INDEX. */ 714690075Sobrien 714790075Sobrienvoid 7148132730Skanrecord_builtin_type (enum rid rid_index, const char *name, tree type) 714990075Sobrien{ 7150169699Skan tree id, decl; 715190075Sobrien if (name == 0) 715290075Sobrien id = ridpointers[(int) rid_index]; 715390075Sobrien else 715490075Sobrien id = get_identifier (name); 7155169699Skan decl = build_decl (TYPE_DECL, id, type); 7156169699Skan pushdecl (decl); 7157169699Skan if (debug_hooks->type_decl) 7158169699Skan debug_hooks->type_decl (decl, false); 715990075Sobrien} 716090075Sobrien 716190075Sobrien/* Build the void_list_node (void_type_node having been created). */ 716290075Sobrientree 7163132730Skanbuild_void_list_node (void) 716490075Sobrien{ 716590075Sobrien tree t = build_tree_list (NULL_TREE, void_type_node); 716690075Sobrien return t; 716790075Sobrien} 7168117421Skan 7169169699Skan/* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */ 7170117421Skan 7171169699Skanstruct c_parm * 7172169699Skanbuild_c_parm (struct c_declspecs *specs, tree attrs, 7173169699Skan struct c_declarator *declarator) 7174169699Skan{ 7175169699Skan struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm); 7176169699Skan ret->specs = specs; 7177169699Skan ret->attrs = attrs; 7178169699Skan ret->declarator = declarator; 7179169699Skan return ret; 7180169699Skan} 7181117421Skan 7182169699Skan/* Return a declarator with nested attributes. TARGET is the inner 7183169699Skan declarator to which these attributes apply. ATTRS are the 7184169699Skan attributes. */ 7185169699Skan 7186169699Skanstruct c_declarator * 7187169699Skanbuild_attrs_declarator (tree attrs, struct c_declarator *target) 7188117421Skan{ 7189169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7190169699Skan ret->kind = cdk_attrs; 7191169699Skan ret->declarator = target; 7192169699Skan ret->u.attrs = attrs; 7193169699Skan return ret; 7194117421Skan} 7195117421Skan 7196169699Skan/* Return a declarator for a function with arguments specified by ARGS 7197169699Skan and return type specified by TARGET. */ 7198132730Skan 7199169699Skanstruct c_declarator * 7200169699Skanbuild_function_declarator (struct c_arg_info *args, 7201169699Skan struct c_declarator *target) 7202132730Skan{ 7203169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7204169699Skan ret->kind = cdk_function; 7205169699Skan ret->declarator = target; 7206169699Skan ret->u.arg_info = args; 7207169699Skan return ret; 7208132730Skan} 7209132730Skan 7210169699Skan/* Return a declarator for the identifier IDENT (which may be 7211169699Skan NULL_TREE for an abstract declarator). */ 7212132730Skan 7213169699Skanstruct c_declarator * 7214169699Skanbuild_id_declarator (tree ident) 7215132730Skan{ 7216169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7217169699Skan ret->kind = cdk_id; 7218169699Skan ret->declarator = 0; 7219169699Skan ret->u.id = ident; 7220169699Skan /* Default value - may get reset to a more precise location. */ 7221169699Skan ret->id_loc = input_location; 7222169699Skan return ret; 7223132730Skan} 7224132730Skan 7225169699Skan/* Return something to represent absolute declarators containing a *. 7226169699Skan TARGET is the absolute declarator that the * contains. 7227169699Skan TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes 7228169699Skan to apply to the pointer type. */ 7229169699Skan 7230169699Skanstruct c_declarator * 7231169699Skanmake_pointer_declarator (struct c_declspecs *type_quals_attrs, 7232169699Skan struct c_declarator *target) 7233132730Skan{ 7234169699Skan tree attrs; 7235169699Skan int quals = 0; 7236169699Skan struct c_declarator *itarget = target; 7237169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7238169699Skan if (type_quals_attrs) 7239169699Skan { 7240169699Skan attrs = type_quals_attrs->attrs; 7241169699Skan quals = quals_from_declspecs (type_quals_attrs); 7242169699Skan if (attrs != NULL_TREE) 7243169699Skan itarget = build_attrs_declarator (attrs, target); 7244169699Skan } 7245169699Skan ret->kind = cdk_pointer; 7246169699Skan ret->declarator = itarget; 7247169699Skan ret->u.pointer_quals = quals; 7248169699Skan return ret; 7249132730Skan} 7250132730Skan 7251169699Skan/* Return a pointer to a structure for an empty list of declaration 7252169699Skan specifiers. */ 7253132730Skan 7254169699Skanstruct c_declspecs * 7255169699Skanbuild_null_declspecs (void) 7256132730Skan{ 7257169699Skan struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs); 7258169699Skan ret->type = 0; 7259169699Skan ret->decl_attr = 0; 7260169699Skan ret->attrs = 0; 7261169699Skan ret->typespec_word = cts_none; 7262169699Skan ret->storage_class = csc_none; 7263169699Skan ret->declspecs_seen_p = false; 7264169699Skan ret->type_seen_p = false; 7265169699Skan ret->non_sc_seen_p = false; 7266169699Skan ret->typedef_p = false; 7267169699Skan ret->tag_defined_p = false; 7268169699Skan ret->explicit_signed_p = false; 7269169699Skan ret->deprecated_p = false; 7270169699Skan ret->default_int_p = false; 7271169699Skan ret->long_p = false; 7272169699Skan ret->long_long_p = false; 7273169699Skan ret->short_p = false; 7274169699Skan ret->signed_p = false; 7275169699Skan ret->unsigned_p = false; 7276169699Skan ret->complex_p = false; 7277169699Skan ret->inline_p = false; 7278169699Skan ret->thread_p = false; 7279169699Skan ret->const_p = false; 7280169699Skan ret->volatile_p = false; 7281169699Skan ret->restrict_p = false; 7282169699Skan return ret; 7283169699Skan} 7284132730Skan 7285169699Skan/* Add the type qualifier QUAL to the declaration specifiers SPECS, 7286169699Skan returning SPECS. */ 7287132730Skan 7288169699Skanstruct c_declspecs * 7289169699Skandeclspecs_add_qual (struct c_declspecs *specs, tree qual) 7290169699Skan{ 7291169699Skan enum rid i; 7292169699Skan bool dupe = false; 7293169699Skan specs->non_sc_seen_p = true; 7294169699Skan specs->declspecs_seen_p = true; 7295169699Skan gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE 7296169699Skan && C_IS_RESERVED_WORD (qual)); 7297169699Skan i = C_RID_CODE (qual); 7298169699Skan switch (i) 7299169699Skan { 7300169699Skan case RID_CONST: 7301169699Skan dupe = specs->const_p; 7302169699Skan specs->const_p = true; 7303169699Skan break; 7304169699Skan case RID_VOLATILE: 7305169699Skan dupe = specs->volatile_p; 7306169699Skan specs->volatile_p = true; 7307169699Skan break; 7308169699Skan case RID_RESTRICT: 7309169699Skan dupe = specs->restrict_p; 7310169699Skan specs->restrict_p = true; 7311169699Skan break; 7312169699Skan default: 7313169699Skan gcc_unreachable (); 7314169699Skan } 7315169699Skan if (dupe && pedantic && !flag_isoc99) 7316169699Skan pedwarn ("duplicate %qE", qual); 7317169699Skan return specs; 7318169699Skan} 7319132730Skan 7320169699Skan/* Add the type specifier TYPE to the declaration specifiers SPECS, 7321169699Skan returning SPECS. */ 7322132730Skan 7323169699Skanstruct c_declspecs * 7324169699Skandeclspecs_add_type (struct c_declspecs *specs, struct c_typespec spec) 7325169699Skan{ 7326169699Skan tree type = spec.spec; 7327169699Skan specs->non_sc_seen_p = true; 7328169699Skan specs->declspecs_seen_p = true; 7329169699Skan specs->type_seen_p = true; 7330169699Skan if (TREE_DEPRECATED (type)) 7331169699Skan specs->deprecated_p = true; 7332169699Skan 7333169699Skan /* Handle type specifier keywords. */ 7334169699Skan if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type)) 7335169699Skan { 7336169699Skan enum rid i = C_RID_CODE (type); 7337169699Skan if (specs->type) 7338132730Skan { 7339169699Skan error ("two or more data types in declaration specifiers"); 7340169699Skan return specs; 7341169699Skan } 7342169699Skan if ((int) i <= (int) RID_LAST_MODIFIER) 7343169699Skan { 7344169699Skan /* "long", "short", "signed", "unsigned" or "_Complex". */ 7345169699Skan bool dupe = false; 7346169699Skan switch (i) 7347132730Skan { 7348169699Skan case RID_LONG: 7349169699Skan if (specs->long_long_p) 7350132730Skan { 7351169699Skan error ("%<long long long%> is too long for GCC"); 7352169699Skan break; 7353132730Skan } 7354169699Skan if (specs->long_p) 7355132730Skan { 7356169699Skan if (specs->typespec_word == cts_double) 7357169699Skan { 7358169699Skan error ("both %<long long%> and %<double%> in " 7359169699Skan "declaration specifiers"); 7360169699Skan break; 7361169699Skan } 7362169699Skan if (pedantic && !flag_isoc99 && !in_system_header 7363169699Skan && warn_long_long) 7364169699Skan pedwarn ("ISO C90 does not support %<long long%>"); 7365169699Skan specs->long_long_p = 1; 7366169699Skan break; 7367132730Skan } 7368169699Skan if (specs->short_p) 7369169699Skan error ("both %<long%> and %<short%> in " 7370169699Skan "declaration specifiers"); 7371169699Skan else if (specs->typespec_word == cts_void) 7372169699Skan error ("both %<long%> and %<void%> in " 7373169699Skan "declaration specifiers"); 7374169699Skan else if (specs->typespec_word == cts_bool) 7375169699Skan error ("both %<long%> and %<_Bool%> in " 7376169699Skan "declaration specifiers"); 7377169699Skan else if (specs->typespec_word == cts_char) 7378169699Skan error ("both %<long%> and %<char%> in " 7379169699Skan "declaration specifiers"); 7380169699Skan else if (specs->typespec_word == cts_float) 7381169699Skan error ("both %<long%> and %<float%> in " 7382169699Skan "declaration specifiers"); 7383169699Skan else if (specs->typespec_word == cts_dfloat32) 7384169699Skan error ("both %<long%> and %<_Decimal32%> in " 7385169699Skan "declaration specifiers"); 7386169699Skan else if (specs->typespec_word == cts_dfloat64) 7387169699Skan error ("both %<long%> and %<_Decimal64%> in " 7388169699Skan "declaration specifiers"); 7389169699Skan else if (specs->typespec_word == cts_dfloat128) 7390169699Skan error ("both %<long%> and %<_Decimal128%> in " 7391169699Skan "declaration specifiers"); 7392132730Skan else 7393169699Skan specs->long_p = true; 7394169699Skan break; 7395169699Skan case RID_SHORT: 7396169699Skan dupe = specs->short_p; 7397169699Skan if (specs->long_p) 7398169699Skan error ("both %<long%> and %<short%> in " 7399169699Skan "declaration specifiers"); 7400169699Skan else if (specs->typespec_word == cts_void) 7401169699Skan error ("both %<short%> and %<void%> in " 7402169699Skan "declaration specifiers"); 7403169699Skan else if (specs->typespec_word == cts_bool) 7404169699Skan error ("both %<short%> and %<_Bool%> in " 7405169699Skan "declaration specifiers"); 7406169699Skan else if (specs->typespec_word == cts_char) 7407169699Skan error ("both %<short%> and %<char%> in " 7408169699Skan "declaration specifiers"); 7409169699Skan else if (specs->typespec_word == cts_float) 7410169699Skan error ("both %<short%> and %<float%> in " 7411169699Skan "declaration specifiers"); 7412169699Skan else if (specs->typespec_word == cts_double) 7413169699Skan error ("both %<short%> and %<double%> in " 7414169699Skan "declaration specifiers"); 7415169699Skan else if (specs->typespec_word == cts_dfloat32) 7416169699Skan error ("both %<short%> and %<_Decimal32%> in " 7417169699Skan "declaration specifiers"); 7418169699Skan else if (specs->typespec_word == cts_dfloat64) 7419169699Skan error ("both %<short%> and %<_Decimal64%> in " 7420169699Skan "declaration specifiers"); 7421169699Skan else if (specs->typespec_word == cts_dfloat128) 7422169699Skan error ("both %<short%> and %<_Decimal128%> in " 7423169699Skan "declaration specifiers"); 7424169699Skan else 7425169699Skan specs->short_p = true; 7426169699Skan break; 7427169699Skan case RID_SIGNED: 7428169699Skan dupe = specs->signed_p; 7429169699Skan if (specs->unsigned_p) 7430169699Skan error ("both %<signed%> and %<unsigned%> in " 7431169699Skan "declaration specifiers"); 7432169699Skan else if (specs->typespec_word == cts_void) 7433169699Skan error ("both %<signed%> and %<void%> in " 7434169699Skan "declaration specifiers"); 7435169699Skan else if (specs->typespec_word == cts_bool) 7436169699Skan error ("both %<signed%> and %<_Bool%> in " 7437169699Skan "declaration specifiers"); 7438169699Skan else if (specs->typespec_word == cts_float) 7439169699Skan error ("both %<signed%> and %<float%> in " 7440169699Skan "declaration specifiers"); 7441169699Skan else if (specs->typespec_word == cts_double) 7442169699Skan error ("both %<signed%> and %<double%> in " 7443169699Skan "declaration specifiers"); 7444169699Skan else if (specs->typespec_word == cts_dfloat32) 7445169699Skan error ("both %<signed%> and %<_Decimal32%> in " 7446169699Skan "declaration specifiers"); 7447169699Skan else if (specs->typespec_word == cts_dfloat64) 7448169699Skan error ("both %<signed%> and %<_Decimal64%> in " 7449169699Skan "declaration specifiers"); 7450169699Skan else if (specs->typespec_word == cts_dfloat128) 7451169699Skan error ("both %<signed%> and %<_Decimal128%> in " 7452169699Skan "declaration specifiers"); 7453169699Skan else 7454169699Skan specs->signed_p = true; 7455169699Skan break; 7456169699Skan case RID_UNSIGNED: 7457169699Skan dupe = specs->unsigned_p; 7458169699Skan if (specs->signed_p) 7459169699Skan error ("both %<signed%> and %<unsigned%> in " 7460169699Skan "declaration specifiers"); 7461169699Skan else if (specs->typespec_word == cts_void) 7462169699Skan error ("both %<unsigned%> and %<void%> in " 7463169699Skan "declaration specifiers"); 7464169699Skan else if (specs->typespec_word == cts_bool) 7465169699Skan error ("both %<unsigned%> and %<_Bool%> in " 7466169699Skan "declaration specifiers"); 7467169699Skan else if (specs->typespec_word == cts_float) 7468169699Skan error ("both %<unsigned%> and %<float%> in " 7469169699Skan "declaration specifiers"); 7470169699Skan else if (specs->typespec_word == cts_double) 7471169699Skan error ("both %<unsigned%> and %<double%> in " 7472169699Skan "declaration specifiers"); 7473169699Skan else if (specs->typespec_word == cts_dfloat32) 7474169699Skan error ("both %<unsigned%> and %<_Decimal32%> in " 7475169699Skan "declaration specifiers"); 7476169699Skan else if (specs->typespec_word == cts_dfloat64) 7477169699Skan error ("both %<unsigned%> and %<_Decimal64%> in " 7478169699Skan "declaration specifiers"); 7479169699Skan else if (specs->typespec_word == cts_dfloat128) 7480169699Skan error ("both %<unsigned%> and %<_Decimal128%> in " 7481169699Skan "declaration specifiers"); 7482169699Skan else 7483169699Skan specs->unsigned_p = true; 7484169699Skan break; 7485169699Skan case RID_COMPLEX: 7486169699Skan dupe = specs->complex_p; 7487169699Skan if (pedantic && !flag_isoc99 && !in_system_header) 7488169699Skan pedwarn ("ISO C90 does not support complex types"); 7489169699Skan if (specs->typespec_word == cts_void) 7490169699Skan error ("both %<complex%> and %<void%> in " 7491169699Skan "declaration specifiers"); 7492169699Skan else if (specs->typespec_word == cts_bool) 7493169699Skan error ("both %<complex%> and %<_Bool%> in " 7494169699Skan "declaration specifiers"); 7495169699Skan else if (specs->typespec_word == cts_dfloat32) 7496169699Skan error ("both %<complex%> and %<_Decimal32%> in " 7497169699Skan "declaration specifiers"); 7498169699Skan else if (specs->typespec_word == cts_dfloat64) 7499169699Skan error ("both %<complex%> and %<_Decimal64%> in " 7500169699Skan "declaration specifiers"); 7501169699Skan else if (specs->typespec_word == cts_dfloat128) 7502169699Skan error ("both %<complex%> and %<_Decimal128%> in " 7503169699Skan "declaration specifiers"); 7504169699Skan else 7505169699Skan specs->complex_p = true; 7506169699Skan break; 7507169699Skan default: 7508169699Skan gcc_unreachable (); 7509132730Skan } 7510169699Skan 7511169699Skan if (dupe) 7512169699Skan error ("duplicate %qE", type); 7513169699Skan 7514169699Skan return specs; 7515132730Skan } 7516169699Skan else 7517169699Skan { 7518169699Skan /* "void", "_Bool", "char", "int", "float" or "double". */ 7519169699Skan if (specs->typespec_word != cts_none) 7520169699Skan { 7521169699Skan error ("two or more data types in declaration specifiers"); 7522169699Skan return specs; 7523169699Skan } 7524169699Skan switch (i) 7525169699Skan { 7526169699Skan case RID_VOID: 7527169699Skan if (specs->long_p) 7528169699Skan error ("both %<long%> and %<void%> in " 7529169699Skan "declaration specifiers"); 7530169699Skan else if (specs->short_p) 7531169699Skan error ("both %<short%> and %<void%> in " 7532169699Skan "declaration specifiers"); 7533169699Skan else if (specs->signed_p) 7534169699Skan error ("both %<signed%> and %<void%> in " 7535169699Skan "declaration specifiers"); 7536169699Skan else if (specs->unsigned_p) 7537169699Skan error ("both %<unsigned%> and %<void%> in " 7538169699Skan "declaration specifiers"); 7539169699Skan else if (specs->complex_p) 7540169699Skan error ("both %<complex%> and %<void%> in " 7541169699Skan "declaration specifiers"); 7542169699Skan else 7543169699Skan specs->typespec_word = cts_void; 7544169699Skan return specs; 7545169699Skan case RID_BOOL: 7546169699Skan if (specs->long_p) 7547169699Skan error ("both %<long%> and %<_Bool%> in " 7548169699Skan "declaration specifiers"); 7549169699Skan else if (specs->short_p) 7550169699Skan error ("both %<short%> and %<_Bool%> in " 7551169699Skan "declaration specifiers"); 7552169699Skan else if (specs->signed_p) 7553169699Skan error ("both %<signed%> and %<_Bool%> in " 7554169699Skan "declaration specifiers"); 7555169699Skan else if (specs->unsigned_p) 7556169699Skan error ("both %<unsigned%> and %<_Bool%> in " 7557169699Skan "declaration specifiers"); 7558169699Skan else if (specs->complex_p) 7559169699Skan error ("both %<complex%> and %<_Bool%> in " 7560169699Skan "declaration specifiers"); 7561169699Skan else 7562169699Skan specs->typespec_word = cts_bool; 7563169699Skan return specs; 7564169699Skan case RID_CHAR: 7565169699Skan if (specs->long_p) 7566169699Skan error ("both %<long%> and %<char%> in " 7567169699Skan "declaration specifiers"); 7568169699Skan else if (specs->short_p) 7569169699Skan error ("both %<short%> and %<char%> in " 7570169699Skan "declaration specifiers"); 7571169699Skan else 7572169699Skan specs->typespec_word = cts_char; 7573169699Skan return specs; 7574169699Skan case RID_INT: 7575169699Skan specs->typespec_word = cts_int; 7576169699Skan return specs; 7577169699Skan case RID_FLOAT: 7578169699Skan if (specs->long_p) 7579169699Skan error ("both %<long%> and %<float%> in " 7580169699Skan "declaration specifiers"); 7581169699Skan else if (specs->short_p) 7582169699Skan error ("both %<short%> and %<float%> in " 7583169699Skan "declaration specifiers"); 7584169699Skan else if (specs->signed_p) 7585169699Skan error ("both %<signed%> and %<float%> in " 7586169699Skan "declaration specifiers"); 7587169699Skan else if (specs->unsigned_p) 7588169699Skan error ("both %<unsigned%> and %<float%> in " 7589169699Skan "declaration specifiers"); 7590169699Skan else 7591169699Skan specs->typespec_word = cts_float; 7592169699Skan return specs; 7593169699Skan case RID_DOUBLE: 7594169699Skan if (specs->long_long_p) 7595169699Skan error ("both %<long long%> and %<double%> in " 7596169699Skan "declaration specifiers"); 7597169699Skan else if (specs->short_p) 7598169699Skan error ("both %<short%> and %<double%> in " 7599169699Skan "declaration specifiers"); 7600169699Skan else if (specs->signed_p) 7601169699Skan error ("both %<signed%> and %<double%> in " 7602169699Skan "declaration specifiers"); 7603169699Skan else if (specs->unsigned_p) 7604169699Skan error ("both %<unsigned%> and %<double%> in " 7605169699Skan "declaration specifiers"); 7606169699Skan else 7607169699Skan specs->typespec_word = cts_double; 7608169699Skan return specs; 7609169699Skan case RID_DFLOAT32: 7610169699Skan case RID_DFLOAT64: 7611169699Skan case RID_DFLOAT128: 7612169699Skan { 7613169699Skan const char *str; 7614169699Skan if (i == RID_DFLOAT32) 7615169699Skan str = "_Decimal32"; 7616169699Skan else if (i == RID_DFLOAT64) 7617169699Skan str = "_Decimal64"; 7618169699Skan else 7619169699Skan str = "_Decimal128"; 7620169699Skan if (specs->long_long_p) 7621169699Skan error ("both %<long long%> and %<%s%> in " 7622169699Skan "declaration specifiers", str); 7623169699Skan if (specs->long_p) 7624169699Skan error ("both %<long%> and %<%s%> in " 7625169699Skan "declaration specifiers", str); 7626169699Skan else if (specs->short_p) 7627169699Skan error ("both %<short%> and %<%s%> in " 7628169699Skan "declaration specifiers", str); 7629169699Skan else if (specs->signed_p) 7630169699Skan error ("both %<signed%> and %<%s%> in " 7631169699Skan "declaration specifiers", str); 7632169699Skan else if (specs->unsigned_p) 7633169699Skan error ("both %<unsigned%> and %<%s%> in " 7634169699Skan "declaration specifiers", str); 7635169699Skan else if (specs->complex_p) 7636169699Skan error ("both %<complex%> and %<%s%> in " 7637169699Skan "declaration specifiers", str); 7638169699Skan else if (i == RID_DFLOAT32) 7639169699Skan specs->typespec_word = cts_dfloat32; 7640169699Skan else if (i == RID_DFLOAT64) 7641169699Skan specs->typespec_word = cts_dfloat64; 7642169699Skan else 7643169699Skan specs->typespec_word = cts_dfloat128; 7644169699Skan } 7645169699Skan if (!targetm.decimal_float_supported_p ()) 7646169699Skan error ("decimal floating point not supported for this target"); 7647169699Skan if (pedantic) 7648169699Skan pedwarn ("ISO C does not support decimal floating point"); 7649169699Skan return specs; 7650169699Skan default: 7651169699Skan /* ObjC reserved word "id", handled below. */ 7652169699Skan break; 7653169699Skan } 7654169699Skan } 7655169699Skan } 7656132730Skan 7657169699Skan /* Now we have a typedef (a TYPE_DECL node), an identifier (some 7658169699Skan form of ObjC type, cases such as "int" and "long" being handled 7659169699Skan above), a TYPE (struct, union, enum and typeof specifiers) or an 7660169699Skan ERROR_MARK. In none of these cases may there have previously 7661169699Skan been any type specifiers. */ 7662169699Skan if (specs->type || specs->typespec_word != cts_none 7663169699Skan || specs->long_p || specs->short_p || specs->signed_p 7664169699Skan || specs->unsigned_p || specs->complex_p) 7665169699Skan error ("two or more data types in declaration specifiers"); 7666169699Skan else if (TREE_CODE (type) == TYPE_DECL) 7667169699Skan { 7668169699Skan if (TREE_TYPE (type) == error_mark_node) 7669169699Skan ; /* Allow the type to default to int to avoid cascading errors. */ 7670169699Skan else 7671132730Skan { 7672169699Skan specs->type = TREE_TYPE (type); 7673169699Skan specs->decl_attr = DECL_ATTRIBUTES (type); 7674169699Skan specs->typedef_p = true; 7675169699Skan specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type); 7676169699Skan } 7677169699Skan } 7678169699Skan else if (TREE_CODE (type) == IDENTIFIER_NODE) 7679169699Skan { 7680169699Skan tree t = lookup_name (type); 7681169699Skan if (!t || TREE_CODE (t) != TYPE_DECL) 7682169699Skan error ("%qE fails to be a typedef or built in type", type); 7683169699Skan else if (TREE_TYPE (t) == error_mark_node) 7684169699Skan ; 7685169699Skan else 7686169699Skan specs->type = TREE_TYPE (t); 7687169699Skan } 7688169699Skan else if (TREE_CODE (type) != ERROR_MARK) 7689169699Skan { 7690169699Skan if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref) 7691169699Skan specs->tag_defined_p = true; 7692169699Skan if (spec.kind == ctsk_typeof) 7693169699Skan specs->typedef_p = true; 7694169699Skan specs->type = type; 7695169699Skan } 7696132730Skan 7697169699Skan return specs; 7698169699Skan} 7699132730Skan 7700169699Skan/* Add the storage class specifier or function specifier SCSPEC to the 7701169699Skan declaration specifiers SPECS, returning SPECS. */ 7702169699Skan 7703169699Skanstruct c_declspecs * 7704169699Skandeclspecs_add_scspec (struct c_declspecs *specs, tree scspec) 7705169699Skan{ 7706169699Skan enum rid i; 7707169699Skan enum c_storage_class n = csc_none; 7708169699Skan bool dupe = false; 7709169699Skan specs->declspecs_seen_p = true; 7710169699Skan gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE 7711169699Skan && C_IS_RESERVED_WORD (scspec)); 7712169699Skan i = C_RID_CODE (scspec); 7713169699Skan if (extra_warnings && specs->non_sc_seen_p) 7714169699Skan warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec); 7715169699Skan switch (i) 7716169699Skan { 7717169699Skan case RID_INLINE: 7718169699Skan /* C99 permits duplicate inline. Although of doubtful utility, 7719169699Skan it seems simplest to permit it in gnu89 mode as well, as 7720169699Skan there is also little utility in maintaining this as a 7721169699Skan difference between gnu89 and C99 inline. */ 7722169699Skan dupe = false; 7723169699Skan specs->inline_p = true; 7724169699Skan break; 7725169699Skan case RID_THREAD: 7726169699Skan dupe = specs->thread_p; 7727169699Skan if (specs->storage_class == csc_auto) 7728169699Skan error ("%<__thread%> used with %<auto%>"); 7729169699Skan else if (specs->storage_class == csc_register) 7730169699Skan error ("%<__thread%> used with %<register%>"); 7731169699Skan else if (specs->storage_class == csc_typedef) 7732169699Skan error ("%<__thread%> used with %<typedef%>"); 7733169699Skan else 7734169699Skan specs->thread_p = true; 7735169699Skan break; 7736169699Skan case RID_AUTO: 7737169699Skan n = csc_auto; 7738169699Skan break; 7739169699Skan case RID_EXTERN: 7740169699Skan n = csc_extern; 7741169699Skan /* Diagnose "__thread extern". */ 7742169699Skan if (specs->thread_p) 7743169699Skan error ("%<__thread%> before %<extern%>"); 7744169699Skan break; 7745169699Skan case RID_REGISTER: 7746169699Skan n = csc_register; 7747169699Skan break; 7748169699Skan case RID_STATIC: 7749169699Skan n = csc_static; 7750169699Skan /* Diagnose "__thread static". */ 7751169699Skan if (specs->thread_p) 7752169699Skan error ("%<__thread%> before %<static%>"); 7753169699Skan break; 7754169699Skan case RID_TYPEDEF: 7755169699Skan n = csc_typedef; 7756169699Skan break; 7757169699Skan default: 7758169699Skan gcc_unreachable (); 7759169699Skan } 7760169699Skan if (n != csc_none && n == specs->storage_class) 7761169699Skan dupe = true; 7762169699Skan if (dupe) 7763169699Skan error ("duplicate %qE", scspec); 7764169699Skan if (n != csc_none) 7765169699Skan { 7766169699Skan if (specs->storage_class != csc_none && n != specs->storage_class) 7767169699Skan { 7768169699Skan error ("multiple storage classes in declaration specifiers"); 7769132730Skan } 7770169699Skan else 7771169699Skan { 7772169699Skan specs->storage_class = n; 7773169699Skan if (n != csc_extern && n != csc_static && specs->thread_p) 7774169699Skan { 7775169699Skan error ("%<__thread%> used with %qE", scspec); 7776169699Skan specs->thread_p = false; 7777169699Skan } 7778169699Skan } 7779169699Skan } 7780169699Skan return specs; 7781169699Skan} 7782132730Skan 7783169699Skan/* Add the attributes ATTRS to the declaration specifiers SPECS, 7784169699Skan returning SPECS. */ 7785169699Skan 7786169699Skanstruct c_declspecs * 7787169699Skandeclspecs_add_attrs (struct c_declspecs *specs, tree attrs) 7788169699Skan{ 7789169699Skan specs->attrs = chainon (attrs, specs->attrs); 7790169699Skan specs->declspecs_seen_p = true; 7791169699Skan return specs; 7792132730Skan} 7793132730Skan 7794169699Skan/* Combine "long", "short", "signed", "unsigned" and "_Complex" type 7795169699Skan specifiers with any other type specifier to determine the resulting 7796169699Skan type. This is where ISO C checks on complex types are made, since 7797169699Skan "_Complex long" is a prefix of the valid ISO C type "_Complex long 7798169699Skan double". */ 7799132730Skan 7800169699Skanstruct c_declspecs * 7801169699Skanfinish_declspecs (struct c_declspecs *specs) 7802132730Skan{ 7803169699Skan /* If a type was specified as a whole, we have no modifiers and are 7804169699Skan done. */ 7805169699Skan if (specs->type != NULL_TREE) 7806169699Skan { 7807169699Skan gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p 7808169699Skan && !specs->signed_p && !specs->unsigned_p 7809169699Skan && !specs->complex_p); 7810169699Skan return specs; 7811169699Skan } 7812132730Skan 7813169699Skan /* If none of "void", "_Bool", "char", "int", "float" or "double" 7814169699Skan has been specified, treat it as "int" unless "_Complex" is 7815169699Skan present and there are no other specifiers. If we just have 7816169699Skan "_Complex", it is equivalent to "_Complex double", but e.g. 7817169699Skan "_Complex short" is equivalent to "_Complex short int". */ 7818169699Skan if (specs->typespec_word == cts_none) 7819132730Skan { 7820169699Skan if (specs->long_p || specs->short_p 7821169699Skan || specs->signed_p || specs->unsigned_p) 7822169699Skan { 7823169699Skan specs->typespec_word = cts_int; 7824169699Skan } 7825169699Skan else if (specs->complex_p) 7826169699Skan { 7827169699Skan specs->typespec_word = cts_double; 7828169699Skan if (pedantic) 7829169699Skan pedwarn ("ISO C does not support plain %<complex%> meaning " 7830169699Skan "%<double complex%>"); 7831169699Skan } 7832169699Skan else 7833169699Skan { 7834169699Skan specs->typespec_word = cts_int; 7835169699Skan specs->default_int_p = true; 7836169699Skan /* We don't diagnose this here because grokdeclarator will 7837169699Skan give more specific diagnostics according to whether it is 7838169699Skan a function definition. */ 7839169699Skan } 7840169699Skan } 7841132730Skan 7842169699Skan /* If "signed" was specified, record this to distinguish "int" and 7843169699Skan "signed int" in the case of a bit-field with 7844169699Skan -funsigned-bitfields. */ 7845169699Skan specs->explicit_signed_p = specs->signed_p; 7846132730Skan 7847169699Skan /* Now compute the actual type. */ 7848169699Skan switch (specs->typespec_word) 7849169699Skan { 7850169699Skan case cts_void: 7851169699Skan gcc_assert (!specs->long_p && !specs->short_p 7852169699Skan && !specs->signed_p && !specs->unsigned_p 7853169699Skan && !specs->complex_p); 7854169699Skan specs->type = void_type_node; 7855169699Skan break; 7856169699Skan case cts_bool: 7857169699Skan gcc_assert (!specs->long_p && !specs->short_p 7858169699Skan && !specs->signed_p && !specs->unsigned_p 7859169699Skan && !specs->complex_p); 7860169699Skan specs->type = boolean_type_node; 7861169699Skan break; 7862169699Skan case cts_char: 7863169699Skan gcc_assert (!specs->long_p && !specs->short_p); 7864169699Skan gcc_assert (!(specs->signed_p && specs->unsigned_p)); 7865169699Skan if (specs->signed_p) 7866169699Skan specs->type = signed_char_type_node; 7867169699Skan else if (specs->unsigned_p) 7868169699Skan specs->type = unsigned_char_type_node; 7869169699Skan else 7870169699Skan specs->type = char_type_node; 7871169699Skan if (specs->complex_p) 7872169699Skan { 7873169699Skan if (pedantic) 7874169699Skan pedwarn ("ISO C does not support complex integer types"); 7875169699Skan specs->type = build_complex_type (specs->type); 7876169699Skan } 7877169699Skan break; 7878169699Skan case cts_int: 7879169699Skan gcc_assert (!(specs->long_p && specs->short_p)); 7880169699Skan gcc_assert (!(specs->signed_p && specs->unsigned_p)); 7881169699Skan if (specs->long_long_p) 7882169699Skan specs->type = (specs->unsigned_p 7883169699Skan ? long_long_unsigned_type_node 7884169699Skan : long_long_integer_type_node); 7885169699Skan else if (specs->long_p) 7886169699Skan specs->type = (specs->unsigned_p 7887169699Skan ? long_unsigned_type_node 7888169699Skan : long_integer_type_node); 7889169699Skan else if (specs->short_p) 7890169699Skan specs->type = (specs->unsigned_p 7891169699Skan ? short_unsigned_type_node 7892169699Skan : short_integer_type_node); 7893169699Skan else 7894169699Skan specs->type = (specs->unsigned_p 7895169699Skan ? unsigned_type_node 7896169699Skan : integer_type_node); 7897169699Skan if (specs->complex_p) 7898169699Skan { 7899169699Skan if (pedantic) 7900169699Skan pedwarn ("ISO C does not support complex integer types"); 7901169699Skan specs->type = build_complex_type (specs->type); 7902169699Skan } 7903169699Skan break; 7904169699Skan case cts_float: 7905169699Skan gcc_assert (!specs->long_p && !specs->short_p 7906169699Skan && !specs->signed_p && !specs->unsigned_p); 7907169699Skan specs->type = (specs->complex_p 7908169699Skan ? complex_float_type_node 7909169699Skan : float_type_node); 7910169699Skan break; 7911169699Skan case cts_double: 7912169699Skan gcc_assert (!specs->long_long_p && !specs->short_p 7913169699Skan && !specs->signed_p && !specs->unsigned_p); 7914169699Skan if (specs->long_p) 7915169699Skan { 7916169699Skan specs->type = (specs->complex_p 7917169699Skan ? complex_long_double_type_node 7918169699Skan : long_double_type_node); 7919169699Skan } 7920169699Skan else 7921169699Skan { 7922169699Skan specs->type = (specs->complex_p 7923169699Skan ? complex_double_type_node 7924169699Skan : double_type_node); 7925169699Skan } 7926169699Skan break; 7927169699Skan case cts_dfloat32: 7928169699Skan case cts_dfloat64: 7929169699Skan case cts_dfloat128: 7930169699Skan gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p 7931169699Skan && !specs->signed_p && !specs->unsigned_p && !specs->complex_p); 7932169699Skan if (specs->typespec_word == cts_dfloat32) 7933169699Skan specs->type = dfloat32_type_node; 7934169699Skan else if (specs->typespec_word == cts_dfloat64) 7935169699Skan specs->type = dfloat64_type_node; 7936169699Skan else 7937169699Skan specs->type = dfloat128_type_node; 7938169699Skan break; 7939169699Skan default: 7940169699Skan gcc_unreachable (); 7941169699Skan } 7942132730Skan 7943169699Skan return specs; 7944169699Skan} 7945132730Skan 7946169699Skan/* Synthesize a function which calls all the global ctors or global 7947169699Skan dtors in this file. This is only used for targets which do not 7948169699Skan support .ctors/.dtors sections. FIXME: Migrate into cgraph. */ 7949169699Skanstatic void 7950169699Skanbuild_cdtor (int method_type, tree cdtors) 7951169699Skan{ 7952169699Skan tree body = 0; 7953132730Skan 7954169699Skan if (!cdtors) 7955169699Skan return; 7956169699Skan 7957169699Skan for (; cdtors; cdtors = TREE_CHAIN (cdtors)) 7958169699Skan append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0), 7959169699Skan &body); 7960169699Skan 7961169699Skan cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY); 7962169699Skan} 7963169699Skan 7964169699Skan/* A subroutine of c_write_global_declarations. Perform final processing 7965169699Skan on one file scope's declarations (or the external scope's declarations), 7966169699Skan GLOBALS. */ 7967169699Skan 7968169699Skanstatic void 7969169699Skanc_write_global_declarations_1 (tree globals) 7970169699Skan{ 7971169699Skan tree decl; 7972169699Skan bool reconsider; 7973169699Skan 7974169699Skan /* Process the decls in the order they were written. */ 7975169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 7976169699Skan { 7977169699Skan /* Check for used but undefined static functions using the C 7978169699Skan standard's definition of "used", and set TREE_NO_WARNING so 7979169699Skan that check_global_declarations doesn't repeat the check. */ 7980169699Skan if (TREE_CODE (decl) == FUNCTION_DECL 7981169699Skan && DECL_INITIAL (decl) == 0 7982169699Skan && DECL_EXTERNAL (decl) 7983169699Skan && !TREE_PUBLIC (decl) 7984169699Skan && C_DECL_USED (decl)) 7985169699Skan { 7986169699Skan pedwarn ("%q+F used but never defined", decl); 7987169699Skan TREE_NO_WARNING (decl) = 1; 7988169699Skan } 7989169699Skan 7990169699Skan wrapup_global_declaration_1 (decl); 7991132730Skan } 7992169699Skan 7993169699Skan do 7994169699Skan { 7995169699Skan reconsider = false; 7996169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 7997169699Skan reconsider |= wrapup_global_declaration_2 (decl); 7998169699Skan } 7999169699Skan while (reconsider); 8000169699Skan 8001169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 8002169699Skan check_global_declaration_1 (decl); 8003132730Skan} 8004132730Skan 8005169699Skan/* A subroutine of c_write_global_declarations Emit debug information for each 8006169699Skan of the declarations in GLOBALS. */ 8007132730Skan 8008169699Skanstatic void 8009169699Skanc_write_global_declarations_2 (tree globals) 8010169699Skan{ 8011169699Skan tree decl; 8012169699Skan 8013169699Skan for (decl = globals; decl ; decl = TREE_CHAIN (decl)) 8014169699Skan debug_hooks->global_decl (decl); 8015169699Skan} 8016169699Skan 8017169699Skan/* Preserve the external declarations scope across a garbage collect. */ 8018169699Skanstatic GTY(()) tree ext_block; 8019169699Skan 8020132730Skanvoid 8021169699Skanc_write_global_declarations (void) 8022132730Skan{ 8023169699Skan tree t; 8024132730Skan 8025169699Skan /* We don't want to do this if generating a PCH. */ 8026169699Skan if (pch_file) 8027169699Skan return; 8028132730Skan 8029169699Skan /* Don't waste time on further processing if -fsyntax-only or we've 8030169699Skan encountered errors. */ 8031169699Skan if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in)) 8032169699Skan return; 8033132730Skan 8034169699Skan /* Close the external scope. */ 8035169699Skan ext_block = pop_scope (); 8036169699Skan external_scope = 0; 8037169699Skan gcc_assert (!current_scope); 8038169699Skan 8039169699Skan if (ext_block) 8040169699Skan { 8041169699Skan tree tmp = BLOCK_VARS (ext_block); 8042169699Skan int flags; 8043169699Skan FILE * stream = dump_begin (TDI_tu, &flags); 8044169699Skan if (stream && tmp) 8045169699Skan { 8046169699Skan dump_node (tmp, flags & ~TDF_SLIM, stream); 8047169699Skan dump_end (TDI_tu, stream); 8048169699Skan } 8049169699Skan } 8050169699Skan 8051169699Skan /* Process all file scopes in this compilation, and the external_scope, 8052169699Skan through wrapup_global_declarations and check_global_declarations. */ 8053169699Skan for (t = all_translation_units; t; t = TREE_CHAIN (t)) 8054169699Skan c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t))); 8055169699Skan c_write_global_declarations_1 (BLOCK_VARS (ext_block)); 8056169699Skan 8057169699Skan /* Generate functions to call static constructors and destructors 8058169699Skan for targets that do not support .ctors/.dtors sections. These 8059169699Skan functions have magic names which are detected by collect2. */ 8060169699Skan build_cdtor ('I', static_ctors); static_ctors = 0; 8061169699Skan build_cdtor ('D', static_dtors); static_dtors = 0; 8062169699Skan 8063169699Skan /* We're done parsing; proceed to optimize and emit assembly. 8064169699Skan FIXME: shouldn't be the front end's responsibility to call this. */ 8065169699Skan cgraph_optimize (); 8066169699Skan 8067169699Skan /* After cgraph has had a chance to emit everything that's going to 8068169699Skan be emitted, output debug information for globals. */ 8069169699Skan if (errorcount == 0 && sorrycount == 0) 8070169699Skan { 8071169699Skan timevar_push (TV_SYMOUT); 8072169699Skan for (t = all_translation_units; t; t = TREE_CHAIN (t)) 8073169699Skan c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t))); 8074169699Skan c_write_global_declarations_2 (BLOCK_VARS (ext_block)); 8075169699Skan timevar_pop (TV_SYMOUT); 8076169699Skan } 8077169699Skan 8078169699Skan ext_block = NULL; 8079132730Skan} 8080132730Skan 8081117421Skan#include "gt-c-decl.h" 8082