c-decl.c revision 190404
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: head/contrib/gcc/c-decl.c 190404 2009-03-25 05:10:32Z das $ */ 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); 1693132730Skan if (TREE_CODE (olddecl) != FUNCTION_DECL) 1694132730Skan if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl)) 1695132730Skan { 1696132730Skan DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl); 1697132730Skan DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl); 1698132730Skan } 1699132730Skan } 170018334Speter 170190075Sobrien 1702132730Skan /* Merge the type qualifiers. */ 1703132730Skan if (TREE_READONLY (newdecl)) 1704132730Skan TREE_READONLY (olddecl) = 1; 170518334Speter 1706132730Skan if (TREE_THIS_VOLATILE (newdecl)) 1707169699Skan TREE_THIS_VOLATILE (olddecl) = 1; 170818334Speter 1709169699Skan /* Merge deprecatedness. */ 1710169699Skan if (TREE_DEPRECATED (newdecl)) 1711169699Skan TREE_DEPRECATED (olddecl) = 1; 1712169699Skan 1713169699Skan /* Keep source location of definition rather than declaration and of 1714169699Skan prototype rather than non-prototype unless that prototype is 1715169699Skan built-in. */ 1716169699Skan if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0) 1717169699Skan || (old_is_prototype && !new_is_prototype 1718169699Skan && !C_DECL_BUILTIN_PROTOTYPE (olddecl))) 1719132730Skan DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); 172018334Speter 1721132730Skan /* Merge the initialization information. */ 1722132730Skan if (DECL_INITIAL (newdecl) == 0) 1723132730Skan DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 172418334Speter 1725169699Skan /* Merge the threadprivate attribute. */ 1726169699Skan if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl)) 1727169699Skan { 1728169699Skan DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl); 1729169699Skan C_DECL_THREADPRIVATE_P (newdecl) = 1; 1730169699Skan } 173190075Sobrien 1732169699Skan if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)) 1733169699Skan { 1734169699Skan /* Merge the unused-warning information. */ 1735169699Skan if (DECL_IN_SYSTEM_HEADER (olddecl)) 1736169699Skan DECL_IN_SYSTEM_HEADER (newdecl) = 1; 1737169699Skan else if (DECL_IN_SYSTEM_HEADER (newdecl)) 1738169699Skan DECL_IN_SYSTEM_HEADER (olddecl) = 1; 1739132730Skan 1740169699Skan /* Merge the section attribute. 1741169699Skan We want to issue an error if the sections conflict but that 1742169699Skan must be done later in decl_attributes since we are called 1743169699Skan before attributes are assigned. */ 1744169699Skan if (DECL_SECTION_NAME (newdecl) == NULL_TREE) 1745169699Skan DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); 1746132730Skan 1747169699Skan /* Copy the assembler name. 1748169699Skan Currently, it can only be defined in the prototype. */ 1749169699Skan COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); 175018334Speter 1751169699Skan /* Use visibility of whichever declaration had it specified */ 1752169699Skan if (DECL_VISIBILITY_SPECIFIED (olddecl)) 1753169699Skan { 1754169699Skan DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); 1755169699Skan DECL_VISIBILITY_SPECIFIED (newdecl) = 1; 1756169699Skan } 175796263Sobrien 1758169699Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 1759169699Skan { 1760169699Skan DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl); 1761169699Skan DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); 1762169699Skan DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); 1763169699Skan DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) 1764169699Skan |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); 1765169699Skan TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1766169699Skan TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); 1767169699Skan DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); 1768169699Skan DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); 1769169699Skan DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl); 1770169699Skan } 1771169699Skan 1772169699Skan /* Merge the storage class information. */ 1773169699Skan merge_weak (newdecl, olddecl); 1774169699Skan 1775169699Skan /* For functions, static overrides non-static. */ 1776169699Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 1777169699Skan { 1778169699Skan TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl); 1779169699Skan /* This is since we don't automatically 1780169699Skan copy the attributes of NEWDECL into OLDDECL. */ 1781169699Skan TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 1782169699Skan /* If this clears `static', clear it in the identifier too. */ 1783169699Skan if (!TREE_PUBLIC (olddecl)) 1784169699Skan TREE_PUBLIC (DECL_NAME (olddecl)) = 0; 1785169699Skan } 178618334Speter } 1787169699Skan 1788189824Sdas /* In c99, 'extern' declaration before (or after) 'inline' means this 1789189824Sdas function is not DECL_EXTERNAL, unless 'gnu_inline' attribute 1790189824Sdas is present. */ 1791189824Sdas if (TREE_CODE (newdecl) == FUNCTION_DECL 1792189824Sdas && !flag_gnu89_inline 1793189824Sdas && (DECL_DECLARED_INLINE_P (newdecl) 1794189824Sdas || DECL_DECLARED_INLINE_P (olddecl)) 1795189824Sdas && (!DECL_DECLARED_INLINE_P (newdecl) 1796189824Sdas || !DECL_DECLARED_INLINE_P (olddecl) 1797189824Sdas || !DECL_EXTERNAL (olddecl)) 1798189824Sdas && DECL_EXTERNAL (newdecl) 1799189824Sdas && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))) 1800189824Sdas DECL_EXTERNAL (newdecl) = 0; 1801189824Sdas 180218334Speter if (DECL_EXTERNAL (newdecl)) 180318334Speter { 1804132730Skan TREE_STATIC (newdecl) = TREE_STATIC (olddecl); 1805132730Skan DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl); 1806132730Skan 180718334Speter /* An extern decl does not override previous storage class. */ 180818334Speter TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); 1809169699Skan if (!DECL_EXTERNAL (newdecl)) 1810132730Skan { 1811132730Skan DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); 1812132730Skan DECL_COMMON (newdecl) = DECL_COMMON (olddecl); 1813132730Skan } 181418334Speter } 181518334Speter else 181618334Speter { 181718334Speter TREE_STATIC (olddecl) = TREE_STATIC (newdecl); 181818334Speter TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 181918334Speter } 182018334Speter 182150397Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL) 182218334Speter { 182390075Sobrien /* If we're redefining a function previously defined as extern 182490075Sobrien inline, make sure we emit debug info for the inline before we 1825169699Skan throw it away, in case it was inlined into a function that 1826169699Skan hasn't been written out yet. */ 1827110623Skan if (new_is_definition && DECL_INITIAL (olddecl)) 182890075Sobrien { 1829132730Skan if (TREE_USED (olddecl) 1830132730Skan /* In unit-at-a-time mode we never inline re-defined extern 1831169699Skan inline functions. */ 1832132730Skan && !flag_unit_at_a_time 1833132730Skan && cgraph_function_possibly_inlined_p (olddecl)) 1834110623Skan (*debug_hooks->outlining_inline_function) (olddecl); 183590075Sobrien 183690075Sobrien /* The new defn must not be inline. */ 183790075Sobrien DECL_INLINE (newdecl) = 0; 183890075Sobrien DECL_UNINLINABLE (newdecl) = 1; 183990075Sobrien } 184090075Sobrien else 184190075Sobrien { 1842169699Skan /* If either decl says `inline', this fn is inline, unless 1843169699Skan its definition was passed already. */ 184490075Sobrien if (DECL_DECLARED_INLINE_P (newdecl) 184590075Sobrien || DECL_DECLARED_INLINE_P (olddecl)) 184690075Sobrien DECL_DECLARED_INLINE_P (newdecl) = 1; 184790075Sobrien 184890075Sobrien DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) 184990075Sobrien = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); 185090075Sobrien } 185190075Sobrien 185218334Speter if (DECL_BUILT_IN (olddecl)) 185318334Speter { 1854169699Skan /* If redeclaring a builtin function, it stays built in. 1855169699Skan But it gets tagged as having been declared. */ 1856132730Skan DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); 1857132730Skan DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); 1858169699Skan C_DECL_DECLARED_BUILTIN (newdecl) = 1; 1859169699Skan if (new_is_prototype) 1860169699Skan C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0; 1861169699Skan else 1862169699Skan C_DECL_BUILTIN_PROTOTYPE (newdecl) 1863169699Skan = C_DECL_BUILTIN_PROTOTYPE (olddecl); 186418334Speter } 186590075Sobrien 186650397Sobrien /* Also preserve various other info from the definition. */ 1867169699Skan if (!new_is_definition) 186850397Sobrien { 186950397Sobrien DECL_RESULT (newdecl) = DECL_RESULT (olddecl); 1870132730Skan DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 1871169699Skan DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl); 187290075Sobrien DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); 187350397Sobrien DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); 187490075Sobrien 187590075Sobrien /* Set DECL_INLINE on the declaration if we've got a body 187690075Sobrien from which to instantiate. */ 1877169699Skan if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl)) 187890075Sobrien { 187990075Sobrien DECL_INLINE (newdecl) = 1; 188090075Sobrien DECL_ABSTRACT_ORIGIN (newdecl) 1881132730Skan = DECL_ABSTRACT_ORIGIN (olddecl); 188290075Sobrien } 188350397Sobrien } 188490075Sobrien else 188590075Sobrien { 188690075Sobrien /* If a previous declaration said inline, mark the 188790075Sobrien definition as inlinable. */ 188890075Sobrien if (DECL_DECLARED_INLINE_P (newdecl) 1889169699Skan && !DECL_UNINLINABLE (newdecl)) 189090075Sobrien DECL_INLINE (newdecl) = 1; 189190075Sobrien } 189250397Sobrien } 189318334Speter 1894189824Sdas extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl); 1895189824Sdas 189618334Speter /* Copy most of the decl-specific fields of NEWDECL into OLDDECL. 1897169699Skan But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */ 189818334Speter { 189990075Sobrien unsigned olddecl_uid = DECL_UID (olddecl); 1900169699Skan tree olddecl_context = DECL_CONTEXT (olddecl); 190118334Speter 190290075Sobrien memcpy ((char *) olddecl + sizeof (struct tree_common), 190390075Sobrien (char *) newdecl + sizeof (struct tree_common), 1904169699Skan sizeof (struct tree_decl_common) - sizeof (struct tree_common)); 1905169699Skan switch (TREE_CODE (olddecl)) 1906169699Skan { 1907169699Skan case FIELD_DECL: 1908169699Skan case VAR_DECL: 1909169699Skan case PARM_DECL: 1910169699Skan case LABEL_DECL: 1911169699Skan case RESULT_DECL: 1912169699Skan case CONST_DECL: 1913169699Skan case TYPE_DECL: 1914169699Skan case FUNCTION_DECL: 1915169699Skan memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 1916169699Skan (char *) newdecl + sizeof (struct tree_decl_common), 1917169699Skan tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common)); 1918169699Skan break; 1919169699Skan 1920169699Skan default: 1921169699Skan 1922169699Skan memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 1923169699Skan (char *) newdecl + sizeof (struct tree_decl_common), 1924169699Skan sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)); 1925169699Skan } 192618334Speter DECL_UID (olddecl) = olddecl_uid; 1927169699Skan DECL_CONTEXT (olddecl) = olddecl_context; 192818334Speter } 192918334Speter 1930132730Skan /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl 1931119268Skan so that encode_section_info has a chance to look at the new decl 1932119268Skan flags and attributes. */ 1933119268Skan if (DECL_RTL_SET_P (olddecl) 1934119268Skan && (TREE_CODE (olddecl) == FUNCTION_DECL 1935119268Skan || (TREE_CODE (olddecl) == VAR_DECL 1936119268Skan && TREE_STATIC (olddecl)))) 1937169699Skan make_decl_rtl (olddecl); 1938189824Sdas 1939189824Sdas /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL, 1940189824Sdas and the definition is coming from the old version, cgraph needs 1941189824Sdas to be called again. */ 1942189824Sdas if (extern_changed && !new_is_definition 1943189824Sdas && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl)) 1944189824Sdas cgraph_finalize_function (olddecl, false); 1945132730Skan} 1946119268Skan 1947132730Skan/* Handle when a new declaration NEWDECL has the same name as an old 1948132730Skan one OLDDECL in the same binding contour. Prints an error message 1949132730Skan if appropriate. 1950132730Skan 1951132730Skan If safely possible, alter OLDDECL to look like NEWDECL, and return 1952132730Skan true. Otherwise, return false. */ 1953132730Skan 1954132730Skanstatic bool 1955132730Skanduplicate_decls (tree newdecl, tree olddecl) 1956132730Skan{ 1957169699Skan tree newtype = NULL, oldtype = NULL; 1958132730Skan 1959132730Skan if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype)) 1960169699Skan { 1961169699Skan /* Avoid `unused variable' and other warnings warnings for OLDDECL. */ 1962169699Skan TREE_NO_WARNING (olddecl) = 1; 1963169699Skan return false; 1964169699Skan } 1965132730Skan 1966132730Skan merge_decls (newdecl, olddecl, newtype, oldtype); 1967132730Skan return true; 196818334Speter} 1969169699Skan 1970132730Skan 1971169699Skan/* Check whether decl-node NEW_DECL shadows an existing declaration. */ 1972132730Skanstatic void 1973169699Skanwarn_if_shadowing (tree new_decl) 1974132730Skan{ 1975169699Skan struct c_binding *b; 1976132730Skan 1977169699Skan /* Shadow warnings wanted? */ 1978169699Skan if (!warn_shadow 1979132730Skan /* No shadow warnings for internally generated vars. */ 1980169699Skan || DECL_IS_BUILTIN (new_decl) 1981132730Skan /* No shadow warnings for vars made for inlining. */ 1982169699Skan || DECL_FROM_INLINE (new_decl)) 198390075Sobrien return; 198490075Sobrien 1985169699Skan /* Is anything being shadowed? Invisible decls do not count. */ 1986169699Skan for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed) 1987169699Skan if (b->decl && b->decl != new_decl && !b->invisible) 1988169699Skan { 1989169699Skan tree old_decl = b->decl; 199090075Sobrien 1991169699Skan if (old_decl == error_mark_node) 1992169699Skan { 1993169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows previous " 1994169699Skan "non-variable", new_decl); 1995169699Skan break; 1996169699Skan } 1997169699Skan else if (TREE_CODE (old_decl) == PARM_DECL) 1998169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows a parameter", 1999169699Skan new_decl); 2000169699Skan else if (DECL_FILE_SCOPE_P (old_decl)) 2001169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows a global " 2002169699Skan "declaration", new_decl); 2003169699Skan else if (TREE_CODE (old_decl) == FUNCTION_DECL 2004169699Skan && DECL_BUILT_IN (old_decl)) 2005169699Skan { 2006169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows " 2007169699Skan "a built-in function", new_decl); 2008169699Skan break; 2009169699Skan } 2010169699Skan else 2011169699Skan warning (OPT_Wshadow, "declaration of %q+D shadows a previous local", 2012169699Skan new_decl); 2013169699Skan 2014169699Skan warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl); 2015169699Skan 2016169699Skan break; 2017169699Skan } 2018132730Skan} 2019132730Skan 2020132730Skan 2021132730Skan/* Subroutine of pushdecl. 2022132730Skan 2023132730Skan X is a TYPE_DECL for a typedef statement. Create a brand new 2024132730Skan ..._TYPE node (which will be just a variant of the existing 2025132730Skan ..._TYPE node with identical properties) and then install X 2026132730Skan as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. 2027132730Skan 2028132730Skan The whole point here is to end up with a situation where each 2029132730Skan and every ..._TYPE node the compiler creates will be uniquely 2030132730Skan associated with AT MOST one node representing a typedef name. 2031132730Skan This way, even though the compiler substitutes corresponding 2032132730Skan ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very 2033132730Skan early on, later parts of the compiler can always do the reverse 2034132730Skan translation and get back the corresponding typedef name. For 2035132730Skan example, given: 2036132730Skan 2037169699Skan typedef struct S MY_TYPE; 2038132730Skan MY_TYPE object; 2039132730Skan 2040132730Skan Later parts of the compiler might only know that `object' was of 2041132730Skan type `struct S' if it were not for code just below. With this 2042132730Skan code however, later parts of the compiler see something like: 2043132730Skan 2044132730Skan struct S' == struct S 2045132730Skan typedef struct S' MY_TYPE; 2046132730Skan struct S' object; 2047132730Skan 2048132730Skan And they can then deduce (from the node for type struct S') that 2049132730Skan the original object declaration was: 2050132730Skan 2051132730Skan MY_TYPE object; 2052132730Skan 2053132730Skan Being able to do this is important for proper support of protoize, 2054132730Skan and also for generating precise symbolic debugging information 2055132730Skan which takes full account of the programmer's (typedef) vocabulary. 2056132730Skan 2057132730Skan Obviously, we don't want to generate a duplicate ..._TYPE node if 2058132730Skan the TYPE_DECL node that we are now processing really represents a 2059132730Skan standard built-in type. 2060132730Skan 2061132730Skan Since all standard types are effectively declared at line zero 2062132730Skan in the source file, we can easily check to see if we are working 2063132730Skan on a standard type by checking the current value of lineno. */ 2064132730Skan 2065132730Skanstatic void 2066132730Skanclone_underlying_type (tree x) 2067132730Skan{ 2068169699Skan if (DECL_IS_BUILTIN (x)) 206990075Sobrien { 2070132730Skan if (TYPE_NAME (TREE_TYPE (x)) == 0) 2071132730Skan TYPE_NAME (TREE_TYPE (x)) = x; 207290075Sobrien } 2073132730Skan else if (TREE_TYPE (x) != error_mark_node 2074132730Skan && DECL_ORIGINAL_TYPE (x) == NULL_TREE) 207590075Sobrien { 2076132730Skan tree tt = TREE_TYPE (x); 2077132730Skan DECL_ORIGINAL_TYPE (x) = tt; 2078169699Skan tt = build_variant_type_copy (tt); 2079132730Skan TYPE_NAME (tt) = x; 2080132730Skan TREE_USED (tt) = TREE_USED (x); 2081132730Skan TREE_TYPE (x) = tt; 208290075Sobrien } 208390075Sobrien} 208490075Sobrien 208518334Speter/* Record a decl-node X as belonging to the current lexical scope. 208618334Speter Check for errors (such as an incompatible declaration for the same 208718334Speter name already seen in the same scope). 208818334Speter 208918334Speter Returns either X or an old decl for the same name. 209018334Speter If an old decl is returned, it may have been smashed 209118334Speter to agree with what X says. */ 209218334Speter 209318334Spetertree 2094132730Skanpushdecl (tree x) 209518334Speter{ 209690075Sobrien tree name = DECL_NAME (x); 2097132730Skan struct c_scope *scope = current_scope; 2098169699Skan struct c_binding *b; 2099169699Skan bool nested = false; 210018334Speter 210190075Sobrien /* Functions need the lang_decl data. */ 2102169699Skan if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x)) 2103169699Skan DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl); 210490075Sobrien 2105169699Skan /* Must set DECL_CONTEXT for everything not at file scope or 2106169699Skan DECL_FILE_SCOPE_P won't work. Local externs don't count 2107169699Skan unless they have initializers (which generate code). */ 2108169699Skan if (current_function_decl 2109169699Skan && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL) 2110169699Skan || DECL_INITIAL (x) || !DECL_EXTERNAL (x))) 2111132730Skan DECL_CONTEXT (x) = current_function_decl; 211218334Speter 2113169699Skan /* If this is of variably modified type, prevent jumping into its 2114169699Skan scope. */ 2115169699Skan if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL) 2116169699Skan && variably_modified_type_p (TREE_TYPE (x), NULL_TREE)) 2117169699Skan c_begin_vm_scope (scope->depth); 2118169699Skan 2119169699Skan /* Anonymous decls are just inserted in the scope. */ 2120169699Skan if (!name) 212118334Speter { 2122169699Skan bind (name, x, scope, /*invisible=*/false, /*nested=*/false); 2123169699Skan return x; 2124169699Skan } 212518334Speter 2126169699Skan /* First, see if there is another declaration with the same name in 2127169699Skan the current scope. If there is, duplicate_decls may do all the 2128169699Skan work for us. If duplicate_decls returns false, that indicates 2129169699Skan two incompatible decls in the same scope; we are to silently 2130169699Skan replace the old one (duplicate_decls has issued all appropriate 2131169699Skan diagnostics). In particular, we should not consider possible 2132169699Skan duplicates in the external scope, or shadowing. */ 2133169699Skan b = I_SYMBOL_BINDING (name); 2134169699Skan if (b && B_IN_SCOPE (b, scope)) 2135169699Skan { 2136169699Skan struct c_binding *b_ext, *b_use; 2137169699Skan tree type = TREE_TYPE (x); 2138169699Skan tree visdecl = b->decl; 2139169699Skan tree vistype = TREE_TYPE (visdecl); 2140169699Skan if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 2141169699Skan && COMPLETE_TYPE_P (TREE_TYPE (x))) 2142169699Skan b->inner_comp = false; 2143169699Skan b_use = b; 2144169699Skan b_ext = b; 2145169699Skan /* If this is an external linkage declaration, we should check 2146169699Skan for compatibility with the type in the external scope before 2147169699Skan setting the type at this scope based on the visible 2148169699Skan information only. */ 2149169699Skan if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl)) 2150169699Skan { 2151169699Skan while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext)) 2152169699Skan b_ext = b_ext->shadowed; 2153169699Skan if (b_ext) 2154169699Skan { 2155169699Skan b_use = b_ext; 2156169699Skan if (b_use->type) 2157169699Skan TREE_TYPE (b_use->decl) = b_use->type; 2158169699Skan } 2159169699Skan } 2160169699Skan if (duplicate_decls (x, b_use->decl)) 2161169699Skan { 2162169699Skan if (b_use != b) 2163169699Skan { 2164169699Skan /* Save the updated type in the external scope and 2165169699Skan restore the proper type for this scope. */ 2166169699Skan tree thistype; 2167169699Skan if (comptypes (vistype, type)) 2168169699Skan thistype = composite_type (vistype, type); 2169169699Skan else 2170169699Skan thistype = TREE_TYPE (b_use->decl); 2171169699Skan b_use->type = TREE_TYPE (b_use->decl); 2172169699Skan if (TREE_CODE (b_use->decl) == FUNCTION_DECL 2173169699Skan && DECL_BUILT_IN (b_use->decl)) 2174169699Skan thistype 2175169699Skan = build_type_attribute_variant (thistype, 2176169699Skan TYPE_ATTRIBUTES 2177169699Skan (b_use->type)); 2178169699Skan TREE_TYPE (b_use->decl) = thistype; 2179169699Skan } 2180169699Skan return b_use->decl; 2181169699Skan } 2182169699Skan else 2183169699Skan goto skip_external_and_shadow_checks; 2184169699Skan } 2185169699Skan 2186169699Skan /* All declarations with external linkage, and all external 2187169699Skan references, go in the external scope, no matter what scope is 2188169699Skan current. However, the binding in that scope is ignored for 2189169699Skan purposes of normal name lookup. A separate binding structure is 2190169699Skan created in the requested scope; this governs the normal 2191169699Skan visibility of the symbol. 2192169699Skan 2193169699Skan The binding in the externals scope is used exclusively for 2194169699Skan detecting duplicate declarations of the same object, no matter 2195169699Skan what scope they are in; this is what we do here. (C99 6.2.7p2: 2196169699Skan All declarations that refer to the same object or function shall 2197169699Skan have compatible type; otherwise, the behavior is undefined.) */ 2198169699Skan if (DECL_EXTERNAL (x) || scope == file_scope) 2199169699Skan { 2200169699Skan tree type = TREE_TYPE (x); 2201169699Skan tree vistype = 0; 2202169699Skan tree visdecl = 0; 2203169699Skan bool type_saved = false; 2204169699Skan if (b && !B_IN_EXTERNAL_SCOPE (b) 2205169699Skan && (TREE_CODE (b->decl) == FUNCTION_DECL 2206169699Skan || TREE_CODE (b->decl) == VAR_DECL) 2207169699Skan && DECL_FILE_SCOPE_P (b->decl)) 2208169699Skan { 2209169699Skan visdecl = b->decl; 2210169699Skan vistype = TREE_TYPE (visdecl); 2211169699Skan } 2212169699Skan if (scope != file_scope 221390075Sobrien && !DECL_IN_SYSTEM_HEADER (x)) 2214169699Skan warning (OPT_Wnested_externs, "nested extern declaration of %qD", x); 221590075Sobrien 2216169699Skan while (b && !B_IN_EXTERNAL_SCOPE (b)) 221750397Sobrien { 2218169699Skan /* If this decl might be modified, save its type. This is 2219169699Skan done here rather than when the decl is first bound 2220169699Skan because the type may change after first binding, through 2221169699Skan being completed or through attributes being added. If we 2222169699Skan encounter multiple such decls, only the first should have 2223169699Skan its type saved; the others will already have had their 2224169699Skan proper types saved and the types will not have changed as 2225169699Skan their scopes will not have been re-entered. */ 2226169699Skan if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved) 222718334Speter { 2228169699Skan b->type = TREE_TYPE (b->decl); 2229169699Skan type_saved = true; 223018334Speter } 2231169699Skan if (B_IN_FILE_SCOPE (b) 2232169699Skan && TREE_CODE (b->decl) == VAR_DECL 2233169699Skan && TREE_STATIC (b->decl) 2234169699Skan && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE 2235169699Skan && !TYPE_DOMAIN (TREE_TYPE (b->decl)) 2236169699Skan && TREE_CODE (type) == ARRAY_TYPE 2237169699Skan && TYPE_DOMAIN (type) 2238169699Skan && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) 2239169699Skan && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) 2240169699Skan { 2241169699Skan /* Array type completed in inner scope, which should be 2242169699Skan diagnosed if the completion does not have size 1 and 2243169699Skan it does not get completed in the file scope. */ 2244169699Skan b->inner_comp = true; 2245169699Skan } 2246169699Skan b = b->shadowed; 224718334Speter } 2248169699Skan 2249169699Skan /* If a matching external declaration has been found, set its 2250169699Skan type to the composite of all the types of that declaration. 2251169699Skan After the consistency checks, it will be reset to the 2252169699Skan composite of the visible types only. */ 2253169699Skan if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl)) 2254169699Skan && b->type) 2255169699Skan TREE_TYPE (b->decl) = b->type; 2256169699Skan 2257169699Skan /* The point of the same_translation_unit_p check here is, 2258169699Skan we want to detect a duplicate decl for a construct like 2259169699Skan foo() { extern bar(); } ... static bar(); but not if 2260169699Skan they are in different translation units. In any case, 2261169699Skan the static does not go in the externals scope. */ 2262169699Skan if (b 2263169699Skan && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl)) 2264169699Skan && duplicate_decls (x, b->decl)) 226590075Sobrien { 2266169699Skan tree thistype; 2267169699Skan if (vistype) 226890075Sobrien { 2269169699Skan if (comptypes (vistype, type)) 2270169699Skan thistype = composite_type (vistype, type); 2271169699Skan else 2272169699Skan thistype = TREE_TYPE (b->decl); 227390075Sobrien } 227418334Speter else 2275169699Skan thistype = type; 2276169699Skan b->type = TREE_TYPE (b->decl); 2277169699Skan if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl)) 2278169699Skan thistype 2279169699Skan = build_type_attribute_variant (thistype, 2280169699Skan TYPE_ATTRIBUTES (b->type)); 2281169699Skan TREE_TYPE (b->decl) = thistype; 2282169699Skan bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true); 2283169699Skan return b->decl; 228418334Speter } 2285169699Skan else if (TREE_PUBLIC (x)) 228618334Speter { 2287169699Skan if (visdecl && !b && duplicate_decls (x, visdecl)) 228818334Speter { 2289169699Skan /* An external declaration at block scope referring to a 2290169699Skan visible entity with internal linkage. The composite 2291169699Skan type will already be correct for this scope, so we 2292169699Skan just need to fall through to make the declaration in 2293169699Skan this scope. */ 2294169699Skan nested = true; 2295169699Skan x = visdecl; 229618334Speter } 2297169699Skan else 2298169699Skan { 2299169699Skan bind (name, x, external_scope, /*invisible=*/true, 2300169699Skan /*nested=*/false); 2301169699Skan nested = true; 2302169699Skan } 230318334Speter } 2304169699Skan } 230590075Sobrien 2306169699Skan if (TREE_CODE (x) != PARM_DECL) 2307169699Skan warn_if_shadowing (x); 230818334Speter 2309169699Skan skip_external_and_shadow_checks: 2310169699Skan if (TREE_CODE (x) == TYPE_DECL) 2311169699Skan clone_underlying_type (x); 231218334Speter 2313169699Skan bind (name, x, scope, /*invisible=*/false, nested); 231490075Sobrien 2315169699Skan /* If x's type is incomplete because it's based on a 2316169699Skan structure or union which has not yet been fully declared, 2317169699Skan attach it to that structure or union type, so we can go 2318169699Skan back and complete the variable declaration later, if the 2319169699Skan structure or union gets fully declared. 2320132730Skan 2321169699Skan If the input is erroneous, we can have error_mark in the type 2322169699Skan slot (e.g. "f(void a, ...)") - that doesn't count as an 2323169699Skan incomplete type. */ 2324169699Skan if (TREE_TYPE (x) != error_mark_node 2325169699Skan && !COMPLETE_TYPE_P (TREE_TYPE (x))) 2326169699Skan { 2327169699Skan tree element = TREE_TYPE (x); 232818334Speter 2329169699Skan while (TREE_CODE (element) == ARRAY_TYPE) 2330169699Skan element = TREE_TYPE (element); 2331169699Skan element = TYPE_MAIN_VARIANT (element); 233218334Speter 2333169699Skan if ((TREE_CODE (element) == RECORD_TYPE 2334169699Skan || TREE_CODE (element) == UNION_TYPE) 2335169699Skan && (TREE_CODE (x) != TYPE_DECL 2336169699Skan || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE) 2337169699Skan && !COMPLETE_TYPE_P (element)) 2338169699Skan C_TYPE_INCOMPLETE_VARS (element) 2339169699Skan = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element)); 2340169699Skan } 234118334Speter return x; 234218334Speter} 234318334Speter 2344169699Skan/* Record X as belonging to file scope. 2345132730Skan This is used only internally by the Objective-C front end, 2346132730Skan and is limited to its needs. duplicate_decls is not called; 2347132730Skan if there is any preexisting decl for this identifier, it is an ICE. */ 234818334Speter 234918334Spetertree 2350132730Skanpushdecl_top_level (tree x) 235118334Speter{ 2352132730Skan tree name; 2353169699Skan bool nested = false; 2354169699Skan gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL); 235518334Speter 2356132730Skan name = DECL_NAME (x); 2357132730Skan 2358169699Skan gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name)); 2359132730Skan 2360169699Skan if (TREE_PUBLIC (x)) 2361169699Skan { 2362169699Skan bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false); 2363169699Skan nested = true; 2364169699Skan } 2365169699Skan if (file_scope) 2366169699Skan bind (name, x, file_scope, /*invisible=*/false, nested); 2367132730Skan 2368132730Skan return x; 236918334Speter} 237018334Speter 2371169699Skanstatic void 2372169699Skanimplicit_decl_warning (tree id, tree olddecl) 2373169699Skan{ 2374169699Skan void (*diag) (const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2); 2375169699Skan switch (mesg_implicit_function_declaration) 2376169699Skan { 2377169699Skan case 0: return; 2378169699Skan case 1: diag = warning0; break; 2379169699Skan case 2: diag = error; break; 2380169699Skan default: gcc_unreachable (); 2381169699Skan } 2382169699Skan 2383169699Skan diag (G_("implicit declaration of function %qE"), id); 2384169699Skan if (olddecl) 2385169699Skan locate_old_decl (olddecl, diag); 2386169699Skan} 2387169699Skan 2388132730Skan/* Generate an implicit declaration for identifier FUNCTIONID as a 2389132730Skan function of type int (). */ 239018334Speter 239118334Spetertree 2392132730Skanimplicitly_declare (tree functionid) 239318334Speter{ 2394169699Skan struct c_binding *b; 2395169699Skan tree decl = 0; 2396169699Skan tree asmspec_tree; 239718334Speter 2398169699Skan for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed) 2399169699Skan { 2400169699Skan if (B_IN_SCOPE (b, external_scope)) 2401169699Skan { 2402169699Skan decl = b->decl; 2403169699Skan break; 2404169699Skan } 2405169699Skan } 2406169699Skan 2407132730Skan if (decl) 2408132730Skan { 2409169699Skan if (decl == error_mark_node) 2410169699Skan return decl; 2411169699Skan 2412169699Skan /* FIXME: Objective-C has weird not-really-builtin functions 2413169699Skan which are supposed to be visible automatically. They wind up 2414169699Skan in the external scope because they're pushed before the file 2415169699Skan scope gets created. Catch this here and rebind them into the 2416169699Skan file scope. */ 2417169699Skan if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl)) 2418132730Skan { 2419169699Skan bind (functionid, decl, file_scope, 2420169699Skan /*invisible=*/false, /*nested=*/true); 2421169699Skan return decl; 2422132730Skan } 2423169699Skan else 2424169699Skan { 2425169699Skan tree newtype = default_function_type; 2426169699Skan if (b->type) 2427169699Skan TREE_TYPE (decl) = b->type; 2428169699Skan /* Implicit declaration of a function already declared 2429169699Skan (somehow) in a different scope, or as a built-in. 2430169699Skan If this is the first time this has happened, warn; 2431169699Skan then recycle the old declaration but with the new type. */ 2432169699Skan if (!C_DECL_IMPLICIT (decl)) 2433169699Skan { 2434169699Skan implicit_decl_warning (functionid, decl); 2435169699Skan C_DECL_IMPLICIT (decl) = 1; 2436169699Skan } 2437169699Skan if (DECL_BUILT_IN (decl)) 2438169699Skan { 2439169699Skan newtype = build_type_attribute_variant (newtype, 2440169699Skan TYPE_ATTRIBUTES 2441169699Skan (TREE_TYPE (decl))); 2442169699Skan if (!comptypes (newtype, TREE_TYPE (decl))) 2443169699Skan { 2444169699Skan warning (0, "incompatible implicit declaration of built-in" 2445169699Skan " function %qD", decl); 2446169699Skan newtype = TREE_TYPE (decl); 2447169699Skan } 2448169699Skan } 2449169699Skan else 2450169699Skan { 2451169699Skan if (!comptypes (newtype, TREE_TYPE (decl))) 2452169699Skan { 2453169699Skan error ("incompatible implicit declaration of function %qD", 2454169699Skan decl); 2455169699Skan locate_old_decl (decl, error); 2456169699Skan } 2457169699Skan } 2458169699Skan b->type = TREE_TYPE (decl); 2459169699Skan TREE_TYPE (decl) = newtype; 2460169699Skan bind (functionid, decl, current_scope, 2461169699Skan /*invisible=*/false, /*nested=*/true); 2462169699Skan return decl; 2463169699Skan } 2464132730Skan } 246518334Speter 2466132730Skan /* Not seen before. */ 2467132730Skan decl = build_decl (FUNCTION_DECL, functionid, default_function_type); 246818334Speter DECL_EXTERNAL (decl) = 1; 246918334Speter TREE_PUBLIC (decl) = 1; 2470132730Skan C_DECL_IMPLICIT (decl) = 1; 2471169699Skan implicit_decl_warning (functionid, 0); 2472169699Skan asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL); 2473169699Skan if (asmspec_tree) 2474169699Skan set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree)); 247518334Speter 2476132730Skan /* C89 says implicit declarations are in the innermost block. 2477117421Skan So we record the decl in the standard fashion. */ 2478132730Skan decl = pushdecl (decl); 247918334Speter 2480132730Skan /* No need to call objc_check_decl here - it's a function type. */ 2481169699Skan rest_of_decl_compilation (decl, 0, 0); 248218334Speter 2483132730Skan /* Write a record describing this implicit function declaration 2484132730Skan to the prototypes file (if requested). */ 248518334Speter gen_aux_info_record (decl, 0, 1, 0); 248618334Speter 248790075Sobrien /* Possibly apply some default attributes to this implicit declaration. */ 248890075Sobrien decl_attributes (&decl, NULL_TREE, 0); 248918334Speter 249018334Speter return decl; 249118334Speter} 249218334Speter 2493132730Skan/* Issue an error message for a reference to an undeclared variable 2494132730Skan ID, including a reference to a builtin outside of function-call 2495132730Skan context. Establish a binding of the identifier to error_mark_node 2496132730Skan in an appropriate scope, which will suppress further errors for the 2497169699Skan same identifier. The error message should be given location LOC. */ 2498132730Skanvoid 2499169699Skanundeclared_variable (tree id, location_t loc) 2500132730Skan{ 2501132730Skan static bool already = false; 2502132730Skan struct c_scope *scope; 250318334Speter 2504132730Skan if (current_function_decl == 0) 250518334Speter { 2506169699Skan error ("%H%qE undeclared here (not in a function)", &loc, id); 2507132730Skan scope = current_scope; 250818334Speter } 250918334Speter else 251018334Speter { 2511169699Skan error ("%H%qE undeclared (first use in this function)", &loc, id); 2512132730Skan 2513169699Skan if (!already) 2514132730Skan { 2515169699Skan error ("%H(Each undeclared identifier is reported only once", &loc); 2516169699Skan error ("%Hfor each function it appears in.)", &loc); 2517132730Skan already = true; 2518132730Skan } 2519132730Skan 2520169699Skan /* If we are parsing old-style parameter decls, current_function_decl 2521169699Skan will be nonnull but current_function_scope will be null. */ 2522169699Skan scope = current_function_scope ? current_function_scope : current_scope; 252318334Speter } 2524169699Skan bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false); 252518334Speter} 252618334Speter 2527132730Skan/* Subroutine of lookup_label, declare_label, define_label: construct a 2528132730Skan LABEL_DECL with all the proper frills. */ 2529132730Skan 2530132730Skanstatic tree 2531132730Skanmake_label (tree name, location_t location) 2532132730Skan{ 2533132730Skan tree label = build_decl (LABEL_DECL, name, void_type_node); 2534132730Skan 2535132730Skan DECL_CONTEXT (label) = current_function_decl; 2536132730Skan DECL_MODE (label) = VOIDmode; 2537132730Skan DECL_SOURCE_LOCATION (label) = location; 2538132730Skan 2539132730Skan return label; 2540132730Skan} 2541132730Skan 2542132730Skan/* Get the LABEL_DECL corresponding to identifier NAME as a label. 254318334Speter Create one if none exists so far for the current function. 2544132730Skan This is called when a label is used in a goto expression or 2545132730Skan has its address taken. */ 254618334Speter 254718334Spetertree 2548132730Skanlookup_label (tree name) 254918334Speter{ 2550132730Skan tree label; 255118334Speter 255218334Speter if (current_function_decl == 0) 255318334Speter { 2554169699Skan error ("label %qE referenced outside of any function", name); 255518334Speter return 0; 255618334Speter } 255718334Speter 2558132730Skan /* Use a label already defined or ref'd with this name, but not if 2559132730Skan it is inherited from a containing function and wasn't declared 2560132730Skan using __label__. */ 2561169699Skan label = I_LABEL_DECL (name); 2562132730Skan if (label && (DECL_CONTEXT (label) == current_function_decl 2563132730Skan || C_DECLARED_LABEL_FLAG (label))) 256418334Speter { 2565132730Skan /* If the label has only been declared, update its apparent 2566132730Skan location to point here, for better diagnostics if it 2567132730Skan turns out not to have been defined. */ 2568132730Skan if (!TREE_USED (label)) 2569132730Skan DECL_SOURCE_LOCATION (label) = input_location; 2570132730Skan return label; 257118334Speter } 257218334Speter 2573132730Skan /* No label binding for that identifier; make one. */ 2574132730Skan label = make_label (name, input_location); 257518334Speter 2576132730Skan /* Ordinary labels go in the current function scope. */ 2577169699Skan bind (name, label, current_function_scope, 2578169699Skan /*invisible=*/false, /*nested=*/false); 2579132730Skan return label; 258018334Speter} 258118334Speter 2582132730Skan/* Make a label named NAME in the current function, shadowing silently 2583132730Skan any that may be inherited from containing functions or containing 2584132730Skan scopes. This is called for __label__ declarations. */ 258518334Speter 258618334Spetertree 2587132730Skandeclare_label (tree name) 258818334Speter{ 2589169699Skan struct c_binding *b = I_LABEL_BINDING (name); 2590169699Skan tree label; 259118334Speter 2592132730Skan /* Check to make sure that the label hasn't already been declared 2593132730Skan at this scope */ 2594169699Skan if (b && B_IN_CURRENT_SCOPE (b)) 2595169699Skan { 2596169699Skan error ("duplicate label declaration %qE", name); 2597169699Skan locate_old_decl (b->decl, error); 259818334Speter 2599169699Skan /* Just use the previous declaration. */ 2600169699Skan return b->decl; 2601169699Skan } 260218334Speter 2603132730Skan label = make_label (name, input_location); 2604132730Skan C_DECLARED_LABEL_FLAG (label) = 1; 260518334Speter 2606132730Skan /* Declared labels go in the current scope. */ 2607169699Skan bind (name, label, current_scope, 2608169699Skan /*invisible=*/false, /*nested=*/false); 2609132730Skan return label; 261018334Speter} 261118334Speter 261218334Speter/* Define a label, specifying the location in the source file. 261318334Speter Return the LABEL_DECL node for the label, if the definition is valid. 261418334Speter Otherwise return 0. */ 261518334Speter 261618334Spetertree 2617132730Skandefine_label (location_t location, tree name) 261818334Speter{ 2619132730Skan /* Find any preexisting label with this name. It is an error 2620132730Skan if that label has already been defined in this function, or 2621132730Skan if there is a containing function with a declared label with 2622132730Skan the same name. */ 2623169699Skan tree label = I_LABEL_DECL (name); 2624169699Skan struct c_label_list *nlist_se, *nlist_vm; 262518334Speter 2626132730Skan if (label 2627132730Skan && ((DECL_CONTEXT (label) == current_function_decl 2628132730Skan && DECL_INITIAL (label) != 0) 2629132730Skan || (DECL_CONTEXT (label) != current_function_decl 2630132730Skan && C_DECLARED_LABEL_FLAG (label)))) 263118334Speter { 2632169699Skan error ("%Hduplicate label %qD", &location, label); 2633169699Skan locate_old_decl (label, error); 263418334Speter return 0; 263518334Speter } 2636132730Skan else if (label && DECL_CONTEXT (label) == current_function_decl) 2637132730Skan { 2638132730Skan /* The label has been used or declared already in this function, 2639132730Skan but not defined. Update its location to point to this 2640132730Skan definition. */ 2641169699Skan if (C_DECL_UNDEFINABLE_STMT_EXPR (label)) 2642169699Skan error ("%Jjump into statement expression", label); 2643169699Skan if (C_DECL_UNDEFINABLE_VM (label)) 2644169699Skan error ("%Jjump into scope of identifier with variably modified type", 2645169699Skan label); 2646132730Skan DECL_SOURCE_LOCATION (label) = location; 2647132730Skan } 264818334Speter else 264918334Speter { 2650132730Skan /* No label binding for that identifier; make one. */ 2651132730Skan label = make_label (name, location); 2652132730Skan 2653132730Skan /* Ordinary labels go in the current function scope. */ 2654169699Skan bind (name, label, current_function_scope, 2655169699Skan /*invisible=*/false, /*nested=*/false); 265618334Speter } 2657132730Skan 2658169699Skan if (!in_system_header && lookup_name (name)) 2659169699Skan warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace " 2660169699Skan "for labels, identifier %qE conflicts", &location, name); 2661132730Skan 2662169699Skan nlist_se = XOBNEW (&parser_obstack, struct c_label_list); 2663169699Skan nlist_se->next = label_context_stack_se->labels_def; 2664169699Skan nlist_se->label = label; 2665169699Skan label_context_stack_se->labels_def = nlist_se; 2666169699Skan 2667169699Skan nlist_vm = XOBNEW (&parser_obstack, struct c_label_list); 2668169699Skan nlist_vm->next = label_context_stack_vm->labels_def; 2669169699Skan nlist_vm->label = label; 2670169699Skan label_context_stack_vm->labels_def = nlist_vm; 2671169699Skan 2672132730Skan /* Mark label as having been defined. */ 2673132730Skan DECL_INITIAL (label) = error_mark_node; 2674132730Skan return label; 267518334Speter} 267618334Speter 267718334Speter/* Given NAME, an IDENTIFIER_NODE, 267818334Speter return the structure (or union or enum) definition for that name. 2679132730Skan If THISLEVEL_ONLY is nonzero, searches only the current_scope. 268018334Speter CODE says which kind of type the caller wants; 268118334Speter it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. 268218334Speter If the wrong kind of type is found, an error is reported. */ 268318334Speter 268418334Speterstatic tree 2685132730Skanlookup_tag (enum tree_code code, tree name, int thislevel_only) 268618334Speter{ 2687169699Skan struct c_binding *b = I_TAG_BINDING (name); 2688132730Skan int thislevel = 0; 268918334Speter 2690169699Skan if (!b || !b->decl) 2691132730Skan return 0; 2692132730Skan 2693132730Skan /* We only care about whether it's in this level if 2694132730Skan thislevel_only was set or it might be a type clash. */ 2695169699Skan if (thislevel_only || TREE_CODE (b->decl) != code) 269618334Speter { 2697169699Skan /* For our purposes, a tag in the external scope is the same as 2698169699Skan a tag in the file scope. (Primarily relevant to Objective-C 2699169699Skan and its builtin structure tags, which get pushed before the 2700169699Skan file scope is created.) */ 2701169699Skan if (B_IN_CURRENT_SCOPE (b) 2702169699Skan || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b))) 2703132730Skan thislevel = 1; 270418334Speter } 2705132730Skan 2706132730Skan if (thislevel_only && !thislevel) 2707132730Skan return 0; 2708132730Skan 2709169699Skan if (TREE_CODE (b->decl) != code) 2710132730Skan { 2711132730Skan /* Definition isn't the kind we were looking for. */ 2712132730Skan pending_invalid_xref = name; 2713132730Skan pending_invalid_xref_location = input_location; 2714132730Skan 2715132730Skan /* If in the same binding level as a declaration as a tag 2716132730Skan of a different type, this must not be allowed to 2717132730Skan shadow that tag, so give the error immediately. 2718132730Skan (For example, "struct foo; union foo;" is invalid.) */ 2719132730Skan if (thislevel) 2720132730Skan pending_xref_error (); 2721132730Skan } 2722169699Skan return b->decl; 272318334Speter} 272418334Speter 272518334Speter/* Print an error message now 272618334Speter for a recent invalid struct, union or enum cross reference. 272718334Speter We don't print them immediately because they are not invalid 272818334Speter when used in the `struct foo;' construct for shadowing. */ 272918334Speter 273018334Spetervoid 2731132730Skanpending_xref_error (void) 273218334Speter{ 273318334Speter if (pending_invalid_xref != 0) 2734169699Skan error ("%H%qE defined as wrong kind of tag", 2735169699Skan &pending_invalid_xref_location, pending_invalid_xref); 273618334Speter pending_invalid_xref = 0; 273718334Speter} 273818334Speter 273918334Speter 2740132730Skan/* Look up NAME in the current scope and its superiors 274118334Speter in the namespace of variables, functions and typedefs. 274218334Speter Return a ..._DECL node of some kind representing its definition, 274318334Speter or return 0 if it is undefined. */ 274418334Speter 274518334Spetertree 2746132730Skanlookup_name (tree name) 274718334Speter{ 2748169699Skan struct c_binding *b = I_SYMBOL_BINDING (name); 2749169699Skan if (b && !b->invisible) 2750169699Skan return b->decl; 2751169699Skan return 0; 275218334Speter} 275318334Speter 2754169699Skan/* Similar to `lookup_name' but look only at the indicated scope. */ 275518334Speter 2756132730Skanstatic tree 2757169699Skanlookup_name_in_scope (tree name, struct c_scope *scope) 275818334Speter{ 2759169699Skan struct c_binding *b; 276018334Speter 2761169699Skan for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed) 2762169699Skan if (B_IN_SCOPE (b, scope)) 2763169699Skan return b->decl; 2764132730Skan return 0; 276518334Speter} 276618334Speter 276718334Speter/* Create the predefined scalar types of C, 276850397Sobrien and some nodes representing standard constants (0, 1, (void *) 0). 2769132730Skan Initialize the global scope. 277018334Speter Make definitions for built-in primitive functions. */ 277118334Speter 277218334Spetervoid 2773132730Skanc_init_decl_processing (void) 277418334Speter{ 2775132730Skan location_t save_loc = input_location; 277618334Speter 2777169699Skan /* Initialize reserved words for parser. */ 277890075Sobrien c_parse_init (); 277990075Sobrien 2780132730Skan current_function_decl = 0; 278190075Sobrien 2782169699Skan gcc_obstack_init (&parser_obstack); 278318334Speter 2784169699Skan /* Make the externals scope. */ 2785169699Skan push_scope (); 2786169699Skan external_scope = current_scope; 2787169699Skan 2788132730Skan /* Declarations from c_common_nodes_and_builtins must not be associated 2789132730Skan with this input file, lest we get differences between using and not 2790132730Skan using preprocessed headers. */ 2791169699Skan#ifdef USE_MAPPED_LOCATION 2792169699Skan input_location = BUILTINS_LOCATION; 2793169699Skan#else 2794169699Skan input_location.file = "<built-in>"; 2795132730Skan input_location.line = 0; 2796169699Skan#endif 2797132730Skan 2798169699Skan build_common_tree_nodes (flag_signed_char, false); 2799132730Skan 280090075Sobrien c_common_nodes_and_builtins (); 280118334Speter 2802132730Skan /* In C, comparisons and TRUTH_* expressions have type int. */ 2803132730Skan truthvalue_type_node = integer_type_node; 2804132730Skan truthvalue_true_node = integer_one_node; 2805132730Skan truthvalue_false_node = integer_zero_node; 280618334Speter 2807132730Skan /* Even in C99, which has a real boolean type. */ 280890075Sobrien pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"), 2809132730Skan boolean_type_node)); 281018334Speter 2811132730Skan input_location = save_loc; 2812132730Skan 2813169699Skan pedantic_lvalues = true; 281418334Speter 281590075Sobrien make_fname_decl = c_make_fname_decl; 281690075Sobrien start_fname_decls (); 281790075Sobrien} 281818334Speter 281990075Sobrien/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the 282090075Sobrien decl, NAME is the initialization string and TYPE_DEP indicates whether 282190075Sobrien NAME depended on the type of the function. As we don't yet implement 282290075Sobrien delayed emission of static data, we mark the decl as emitted 282390075Sobrien so it is not placed in the output. Anything using it must therefore pull 2824132730Skan out the STRING_CST initializer directly. FIXME. */ 282518334Speter 282690075Sobrienstatic tree 2827132730Skanc_make_fname_decl (tree id, int type_dep) 282890075Sobrien{ 282990075Sobrien const char *name = fname_as_string (type_dep); 283090075Sobrien tree decl, type, init; 283190075Sobrien size_t length = strlen (name); 283218334Speter 2833169699Skan type = build_array_type (char_type_node, 2834169699Skan build_index_type (size_int (length))); 2835169699Skan type = c_build_qualified_type (type, TYPE_QUAL_CONST); 283618334Speter 283790075Sobrien decl = build_decl (VAR_DECL, id, type); 2838132730Skan 283990075Sobrien TREE_STATIC (decl) = 1; 284090075Sobrien TREE_READONLY (decl) = 1; 284190075Sobrien DECL_ARTIFICIAL (decl) = 1; 2842132730Skan 284390075Sobrien init = build_string (length + 1, name); 2844169699Skan free ((char *) name); 284590075Sobrien TREE_TYPE (init) = type; 284690075Sobrien DECL_INITIAL (decl) = init; 284718334Speter 284890075Sobrien TREE_USED (decl) = 1; 2849132730Skan 2850169699Skan if (current_function_decl 2851169699Skan /* For invalid programs like this: 2852169699Skan 2853169699Skan void foo() 2854169699Skan const char* p = __FUNCTION__; 2855169699Skan 2856169699Skan the __FUNCTION__ is believed to appear in K&R style function 2857169699Skan parameter declarator. In that case we still don't have 2858169699Skan function_scope. */ 2859169699Skan && (!errorcount || current_function_scope)) 2860132730Skan { 2861132730Skan DECL_CONTEXT (decl) = current_function_decl; 2862169699Skan bind (id, decl, current_function_scope, 2863169699Skan /*invisible=*/false, /*nested=*/false); 2864132730Skan } 2865132730Skan 286690075Sobrien finish_decl (decl, init, NULL_TREE); 286718334Speter 286890075Sobrien return decl; 286918334Speter} 287018334Speter 287118334Speter/* Return a definition for a builtin function named NAME and whose data type 287218334Speter is TYPE. TYPE should be a function type with argument types. 287318334Speter FUNCTION_CODE tells later passes how to compile calls to this function. 287418334Speter See tree.h for its possible values. 287518334Speter 287618334Speter If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME, 2877117421Skan the name to be called if we can't opencode the function. If 2878117421Skan ATTRS is nonzero, use that for the function's attribute list. */ 287918334Speter 288018334Spetertree 2881132730Skanbuiltin_function (const char *name, tree type, int function_code, 2882169699Skan enum built_in_class cl, const char *library_name, 2883132730Skan tree attrs) 288418334Speter{ 2885169699Skan tree id = get_identifier (name); 2886169699Skan tree decl = build_decl (FUNCTION_DECL, id, type); 2887169699Skan TREE_PUBLIC (decl) = 1; 288818334Speter DECL_EXTERNAL (decl) = 1; 2889169699Skan DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl); 2890169699Skan DECL_BUILT_IN_CLASS (decl) = cl; 2891169699Skan DECL_FUNCTION_CODE (decl) = function_code; 2892169699Skan C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0); 289318334Speter if (library_name) 289490075Sobrien SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name)); 289590075Sobrien 2896169699Skan /* Should never be called on a symbol with a preexisting meaning. */ 2897169699Skan gcc_assert (!I_SYMBOL_BINDING (id)); 289818334Speter 2899169699Skan bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false); 2900169699Skan 2901169699Skan /* Builtins in the implementation namespace are made visible without 2902169699Skan needing to be explicitly declared. See push_file_scope. */ 2903169699Skan if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1]))) 2904169699Skan { 2905169699Skan TREE_CHAIN (decl) = visible_builtins; 2906169699Skan visible_builtins = decl; 2907169699Skan } 2908169699Skan 290990075Sobrien /* Possibly apply some default attributes to this built-in function. */ 2910117421Skan if (attrs) 2911117421Skan decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN); 2912117421Skan else 2913117421Skan decl_attributes (&decl, NULL_TREE, 0); 291490075Sobrien 291518334Speter return decl; 291618334Speter} 291718334Speter 291818334Speter/* Called when a declaration is seen that contains no names to declare. 291918334Speter If its type is a reference to a structure, union or enum inherited 292018334Speter from a containing scope, shadow that tag name for the current scope 292118334Speter with a forward reference. 292218334Speter If its type defines a new named structure or union 292318334Speter or defines an enum, it is valid but we need not do anything here. 292418334Speter Otherwise, it is an error. */ 292518334Speter 292618334Spetervoid 2927169699Skanshadow_tag (const struct c_declspecs *declspecs) 292818334Speter{ 292918334Speter shadow_tag_warned (declspecs, 0); 293018334Speter} 293118334Speter 2932169699Skan/* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning, 2933169699Skan but no pedwarn. */ 293418334Spetervoid 2935169699Skanshadow_tag_warned (const struct c_declspecs *declspecs, int warned) 293618334Speter{ 2937169699Skan bool found_tag = false; 293818334Speter 2939169699Skan if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p) 294018334Speter { 2941169699Skan tree value = declspecs->type; 294290075Sobrien enum tree_code code = TREE_CODE (value); 294318334Speter 294418334Speter if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE) 294518334Speter /* Used to test also that TYPE_SIZE (value) != 0. 294618334Speter That caused warning for `struct foo;' at top level in the file. */ 294718334Speter { 2948132730Skan tree name = TYPE_NAME (value); 294990075Sobrien tree t; 295018334Speter 2951169699Skan found_tag = true; 295218334Speter 295318334Speter if (name == 0) 295418334Speter { 295518334Speter if (warned != 1 && code != ENUMERAL_TYPE) 295618334Speter /* Empty unnamed enum OK */ 295718334Speter { 295818334Speter pedwarn ("unnamed struct/union that defines no instances"); 295918334Speter warned = 1; 296018334Speter } 296118334Speter } 2962169699Skan else if (!declspecs->tag_defined_p 2963169699Skan && declspecs->storage_class != csc_none) 2964169699Skan { 2965169699Skan if (warned != 1) 2966169699Skan pedwarn ("empty declaration with storage class specifier " 2967169699Skan "does not redeclare tag"); 2968169699Skan warned = 1; 2969169699Skan pending_xref_error (); 2970169699Skan } 2971169699Skan else if (!declspecs->tag_defined_p 2972169699Skan && (declspecs->const_p 2973169699Skan || declspecs->volatile_p 2974169699Skan || declspecs->restrict_p)) 2975169699Skan { 2976169699Skan if (warned != 1) 2977169699Skan pedwarn ("empty declaration with type qualifier " 2978169699Skan "does not redeclare tag"); 2979169699Skan warned = 1; 2980169699Skan pending_xref_error (); 2981169699Skan } 298218334Speter else 298318334Speter { 2984169699Skan pending_invalid_xref = 0; 2985132730Skan t = lookup_tag (code, name, 1); 298618334Speter 298718334Speter if (t == 0) 298818334Speter { 298918334Speter t = make_node (code); 299018334Speter pushtag (name, t); 299118334Speter } 299218334Speter } 299318334Speter } 299418334Speter else 299518334Speter { 2996169699Skan if (warned != 1 && !in_system_header) 299718334Speter { 2998169699Skan pedwarn ("useless type name in empty declaration"); 2999169699Skan warned = 1; 300018334Speter } 300118334Speter } 300218334Speter } 3003169699Skan else if (warned != 1 && !in_system_header && declspecs->typedef_p) 3004169699Skan { 3005169699Skan pedwarn ("useless type name in empty declaration"); 3006169699Skan warned = 1; 3007169699Skan } 300818334Speter 3009169699Skan pending_invalid_xref = 0; 301018334Speter 3011169699Skan if (declspecs->inline_p) 3012169699Skan { 3013169699Skan error ("%<inline%> in empty declaration"); 3014169699Skan warned = 1; 3015169699Skan } 3016169699Skan 3017169699Skan if (current_scope == file_scope && declspecs->storage_class == csc_auto) 3018169699Skan { 3019169699Skan error ("%<auto%> in file-scope empty declaration"); 3020169699Skan warned = 1; 3021169699Skan } 3022169699Skan 3023169699Skan if (current_scope == file_scope && declspecs->storage_class == csc_register) 3024169699Skan { 3025169699Skan error ("%<register%> in file-scope empty declaration"); 3026169699Skan warned = 1; 3027169699Skan } 3028169699Skan 3029169699Skan if (!warned && !in_system_header && declspecs->storage_class != csc_none) 3030169699Skan { 3031169699Skan warning (0, "useless storage class specifier in empty declaration"); 3032169699Skan warned = 2; 3033169699Skan } 3034169699Skan 3035169699Skan if (!warned && !in_system_header && declspecs->thread_p) 3036169699Skan { 3037169699Skan warning (0, "useless %<__thread%> in empty declaration"); 3038169699Skan warned = 2; 3039169699Skan } 3040169699Skan 3041169699Skan if (!warned && !in_system_header && (declspecs->const_p 3042169699Skan || declspecs->volatile_p 3043169699Skan || declspecs->restrict_p)) 3044169699Skan { 3045169699Skan warning (0, "useless type qualifier in empty declaration"); 3046169699Skan warned = 2; 3047169699Skan } 3048169699Skan 304918334Speter if (warned != 1) 305018334Speter { 3051169699Skan if (!found_tag) 305218334Speter pedwarn ("empty declaration"); 305318334Speter } 305418334Speter} 305518334Speter 305690075Sobrien 3057169699Skan/* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_* 3058169699Skan bits. SPECS represents declaration specifiers that the grammar 3059169699Skan only permits to contain type qualifiers and attributes. */ 3060169699Skan 3061169699Skanint 3062169699Skanquals_from_declspecs (const struct c_declspecs *specs) 306390075Sobrien{ 3064169699Skan int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0) 3065169699Skan | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0) 3066169699Skan | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)); 3067169699Skan gcc_assert (!specs->type 3068169699Skan && !specs->decl_attr 3069169699Skan && specs->typespec_word == cts_none 3070169699Skan && specs->storage_class == csc_none 3071169699Skan && !specs->typedef_p 3072169699Skan && !specs->explicit_signed_p 3073169699Skan && !specs->deprecated_p 3074169699Skan && !specs->long_p 3075169699Skan && !specs->long_long_p 3076169699Skan && !specs->short_p 3077169699Skan && !specs->signed_p 3078169699Skan && !specs->unsigned_p 3079169699Skan && !specs->complex_p 3080169699Skan && !specs->inline_p 3081169699Skan && !specs->thread_p); 3082169699Skan return quals; 3083169699Skan} 3084169699Skan 3085169699Skan/* Construct an array declarator. EXPR is the expression inside [], 3086169699Skan or NULL_TREE. QUALS are the type qualifiers inside the [] (to be 3087169699Skan applied to the pointer to which a parameter array is converted). 3088169699Skan STATIC_P is true if "static" is inside the [], false otherwise. 3089169699Skan VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified 3090169699Skan length which is nevertheless a complete type, false otherwise. The 3091169699Skan field for the contained declarator is left to be filled in by 3092169699Skan set_array_declarator_inner. */ 3093169699Skan 3094169699Skanstruct c_declarator * 3095169699Skanbuild_array_declarator (tree expr, struct c_declspecs *quals, bool static_p, 3096169699Skan bool vla_unspec_p) 3097169699Skan{ 3098169699Skan struct c_declarator *declarator = XOBNEW (&parser_obstack, 3099169699Skan struct c_declarator); 3100169699Skan declarator->kind = cdk_array; 3101169699Skan declarator->declarator = 0; 3102169699Skan declarator->u.array.dimen = expr; 3103169699Skan if (quals) 3104169699Skan { 3105169699Skan declarator->u.array.attrs = quals->attrs; 3106169699Skan declarator->u.array.quals = quals_from_declspecs (quals); 3107169699Skan } 3108169699Skan else 3109169699Skan { 3110169699Skan declarator->u.array.attrs = NULL_TREE; 3111169699Skan declarator->u.array.quals = 0; 3112169699Skan } 3113169699Skan declarator->u.array.static_p = static_p; 3114169699Skan declarator->u.array.vla_unspec_p = vla_unspec_p; 311590075Sobrien if (pedantic && !flag_isoc99) 311690075Sobrien { 3117169699Skan if (static_p || quals != NULL) 3118169699Skan pedwarn ("ISO C90 does not support %<static%> or type " 3119169699Skan "qualifiers in parameter array declarators"); 312090075Sobrien if (vla_unspec_p) 3121169699Skan pedwarn ("ISO C90 does not support %<[*]%> array declarators"); 312290075Sobrien } 312390075Sobrien if (vla_unspec_p) 3124169699Skan { 3125169699Skan if (!current_scope->parm_flag) 3126169699Skan { 3127169699Skan /* C99 6.7.5.2p4 */ 3128169699Skan error ("%<[*]%> not allowed in other than function prototype scope"); 3129169699Skan declarator->u.array.vla_unspec_p = false; 3130169699Skan return NULL; 3131169699Skan } 3132169699Skan current_scope->had_vla_unspec = true; 3133169699Skan } 3134169699Skan return declarator; 313590075Sobrien} 313690075Sobrien 3137169699Skan/* Set the contained declarator of an array declarator. DECL is the 3138169699Skan declarator, as constructed by build_array_declarator; INNER is what 3139169699Skan appears on the left of the []. ABSTRACT_P is true if it is an 3140169699Skan abstract declarator, false otherwise; this is used to reject static 3141169699Skan and type qualifiers in abstract declarators, where they are not in 3142169699Skan the C99 grammar (subject to possible change in DR#289). */ 314390075Sobrien 3144169699Skanstruct c_declarator * 3145169699Skanset_array_declarator_inner (struct c_declarator *decl, 3146169699Skan struct c_declarator *inner, bool abstract_p) 314790075Sobrien{ 3148169699Skan decl->declarator = inner; 3149169699Skan if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED 3150169699Skan || decl->u.array.attrs != NULL_TREE 3151169699Skan || decl->u.array.static_p)) 315290075Sobrien error ("static or type qualifiers in abstract declarator"); 315390075Sobrien return decl; 315490075Sobrien} 3155169699Skan 3156169699Skan/* INIT is a constructor that forms DECL's initializer. If the final 3157169699Skan element initializes a flexible array field, add the size of that 3158169699Skan initializer to DECL's size. */ 3159169699Skan 3160169699Skanstatic void 3161169699Skanadd_flexible_array_elts_to_size (tree decl, tree init) 3162169699Skan{ 3163169699Skan tree elt, type; 3164169699Skan 3165169699Skan if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init))) 3166169699Skan return; 3167169699Skan 3168169699Skan elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value; 3169169699Skan type = TREE_TYPE (elt); 3170169699Skan if (TREE_CODE (type) == ARRAY_TYPE 3171169699Skan && TYPE_SIZE (type) == NULL_TREE 3172169699Skan && TYPE_DOMAIN (type) != NULL_TREE 3173169699Skan && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE) 3174169699Skan { 3175169699Skan complete_array_type (&type, elt, false); 3176169699Skan DECL_SIZE (decl) 3177169699Skan = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type)); 3178169699Skan DECL_SIZE_UNIT (decl) 3179169699Skan = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type)); 3180169699Skan } 3181169699Skan} 318290075Sobrien 318318334Speter/* Decode a "typename", such as "int **", returning a ..._TYPE node. */ 318418334Speter 318518334Spetertree 3186169699Skangroktypename (struct c_type_name *type_name) 318718334Speter{ 3188169699Skan tree type; 3189169699Skan tree attrs = type_name->specs->attrs; 319090075Sobrien 3191169699Skan type_name->specs->attrs = NULL_TREE; 319290075Sobrien 3193169699Skan type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME, 3194169699Skan false, NULL); 319590075Sobrien 319690075Sobrien /* Apply attributes. */ 3197169699Skan decl_attributes (&type, attrs, 0); 319890075Sobrien 3199169699Skan return type; 320018334Speter} 320118334Speter 320218334Speter/* Decode a declarator in an ordinary declaration or data definition. 320318334Speter This is called as soon as the type information and variable name 320418334Speter have been parsed, before parsing the initializer if any. 320518334Speter Here we create the ..._DECL node, fill in its type, 320618334Speter and put it on the list of decls for the current context. 320718334Speter The ..._DECL node is returned as the value. 320818334Speter 320918334Speter Exception: for arrays where the length is not specified, 321018334Speter the type is left null, to be filled in by `finish_decl'. 321118334Speter 321218334Speter Function definitions do not come here; they go to start_function 321318334Speter instead. However, external and forward declarations of functions 321418334Speter do go through here. Structure field declarations are done by 321518334Speter grokfield and not through here. */ 321618334Speter 321718334Spetertree 3218169699Skanstart_decl (struct c_declarator *declarator, struct c_declspecs *declspecs, 3219169699Skan bool initialized, tree attributes) 322018334Speter{ 322190075Sobrien tree decl; 322290075Sobrien tree tem; 3223132730Skan 322490075Sobrien /* An object declared as __attribute__((deprecated)) suppresses 322590075Sobrien warnings of uses of other deprecated items. */ 322690075Sobrien if (lookup_attribute ("deprecated", attributes)) 322790075Sobrien deprecated_state = DEPRECATED_SUPPRESS; 322818334Speter 322990075Sobrien decl = grokdeclarator (declarator, declspecs, 3230132730Skan NORMAL, initialized, NULL); 3231169699Skan if (!decl) 3232169699Skan return 0; 3233132730Skan 323490075Sobrien deprecated_state = DEPRECATED_NORMAL; 323518334Speter 323690075Sobrien if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL 323790075Sobrien && MAIN_NAME_P (DECL_NAME (decl))) 3238169699Skan warning (OPT_Wmain, "%q+D is usually a function", decl); 323950397Sobrien 324018334Speter if (initialized) 324118334Speter /* Is it valid for this decl to have an initializer at all? 324218334Speter If not, set INITIALIZED to zero, which will indirectly 3243169699Skan tell 'finish_decl' to ignore the initializer once it is parsed. */ 324418334Speter switch (TREE_CODE (decl)) 324518334Speter { 324618334Speter case TYPE_DECL: 3247169699Skan error ("typedef %qD is initialized (use __typeof__ instead)", decl); 3248104761Skan initialized = 0; 324918334Speter break; 325018334Speter 325118334Speter case FUNCTION_DECL: 3252169699Skan error ("function %qD is initialized like a variable", decl); 325318334Speter initialized = 0; 325418334Speter break; 325518334Speter 325618334Speter case PARM_DECL: 325718334Speter /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */ 3258169699Skan error ("parameter %qD is initialized", decl); 325918334Speter initialized = 0; 326018334Speter break; 326118334Speter 326218334Speter default: 3263169699Skan /* Don't allow initializations for incomplete types except for 3264169699Skan arrays which might be completed by the initialization. */ 326590075Sobrien 3266169699Skan /* This can happen if the array size is an undefined macro. 3267169699Skan We already gave a warning, so we don't need another one. */ 326890075Sobrien if (TREE_TYPE (decl) == error_mark_node) 326990075Sobrien initialized = 0; 327090075Sobrien else if (COMPLETE_TYPE_P (TREE_TYPE (decl))) 327118334Speter { 327218334Speter /* A complete type is ok if size is fixed. */ 327318334Speter 327418334Speter if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST 327518334Speter || C_DECL_VARIABLE_SIZE (decl)) 327618334Speter { 327718334Speter error ("variable-sized object may not be initialized"); 327818334Speter initialized = 0; 327918334Speter } 328018334Speter } 328118334Speter else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE) 328218334Speter { 3283169699Skan error ("variable %qD has initializer but incomplete type", decl); 328418334Speter initialized = 0; 328518334Speter } 3286169699Skan else if (C_DECL_VARIABLE_SIZE (decl)) 328718334Speter { 3288169699Skan /* Although C99 is unclear about whether incomplete arrays 3289169699Skan of VLAs themselves count as VLAs, it does not make 3290169699Skan sense to permit them to be initialized given that 3291169699Skan ordinary VLAs may not be initialized. */ 3292169699Skan error ("variable-sized object may not be initialized"); 329318334Speter initialized = 0; 329418334Speter } 329518334Speter } 329618334Speter 329718334Speter if (initialized) 329818334Speter { 3299169699Skan if (current_scope == file_scope) 330018334Speter TREE_STATIC (decl) = 1; 330118334Speter 3302169699Skan /* Tell 'pushdecl' this is an initialized decl 330318334Speter even though we don't yet have the initializer expression. 3304169699Skan Also tell 'finish_decl' it may store the real initializer. */ 330518334Speter DECL_INITIAL (decl) = error_mark_node; 330618334Speter } 330718334Speter 330818334Speter /* If this is a function declaration, write a record describing it to the 330918334Speter prototypes file (if requested). */ 331018334Speter 331118334Speter if (TREE_CODE (decl) == FUNCTION_DECL) 331218334Speter gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0); 331318334Speter 331450397Sobrien /* ANSI specifies that a tentative definition which is not merged with 331550397Sobrien a non-tentative definition behaves exactly like a definition with an 331650397Sobrien initializer equal to zero. (Section 3.7.2) 3317117421Skan 3318117421Skan -fno-common gives strict ANSI behavior, though this tends to break 3319117421Skan a large body of code that grew up without this rule. 3320117421Skan 3321117421Skan Thread-local variables are never common, since there's no entrenched 3322117421Skan body of code to break, and it allows more efficient variable references 3323132730Skan in the presence of dynamic linking. */ 3324117421Skan 3325117421Skan if (TREE_CODE (decl) == VAR_DECL 3326117421Skan && !initialized 3327117421Skan && TREE_PUBLIC (decl) 3328169699Skan && !DECL_THREAD_LOCAL_P (decl) 3329117421Skan && !flag_no_common) 333050397Sobrien DECL_COMMON (decl) = 1; 333118334Speter 333218334Speter /* Set attributes here so if duplicate decl, will have proper attributes. */ 333390075Sobrien decl_attributes (&decl, attributes, 0); 333418334Speter 3335189824Sdas /* Handle gnu_inline attribute. */ 3336189824Sdas if (declspecs->inline_p 3337189824Sdas && !flag_gnu89_inline 3338189824Sdas && TREE_CODE (decl) == FUNCTION_DECL 3339189824Sdas && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))) 3340189824Sdas { 3341189824Sdas if (declspecs->storage_class == csc_auto && current_scope != file_scope) 3342189824Sdas ; 3343189824Sdas else if (declspecs->storage_class != csc_static) 3344189824Sdas DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl); 3345189824Sdas } 3346189824Sdas 3347132730Skan if (TREE_CODE (decl) == FUNCTION_DECL 3348132730Skan && targetm.calls.promote_prototypes (TREE_TYPE (decl))) 3349132730Skan { 3350169699Skan struct c_declarator *ce = declarator; 335196263Sobrien 3352169699Skan if (ce->kind == cdk_pointer) 3353169699Skan ce = declarator->declarator; 3354169699Skan if (ce->kind == cdk_function) 3355132730Skan { 3356169699Skan tree args = ce->u.arg_info->parms; 3357132730Skan for (; args; args = TREE_CHAIN (args)) 3358132730Skan { 3359132730Skan tree type = TREE_TYPE (args); 3360169699Skan if (type && INTEGRAL_TYPE_P (type) 3361132730Skan && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 3362132730Skan DECL_ARG_TYPE (args) = integer_type_node; 3363132730Skan } 3364132730Skan } 3365132730Skan } 3366132730Skan 336790075Sobrien if (TREE_CODE (decl) == FUNCTION_DECL 336890075Sobrien && DECL_DECLARED_INLINE_P (decl) 336990075Sobrien && DECL_UNINLINABLE (decl) 337090075Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) 3371169699Skan warning (OPT_Wattributes, "inline function %q+D given attribute noinline", 3372169699Skan decl); 337390075Sobrien 3374189824Sdas /* C99 6.7.4p3: An inline definition of a function with external 3375189824Sdas linkage shall not contain a definition of a modifiable object 3376189824Sdas with static storage duration... */ 3377189824Sdas if (TREE_CODE (decl) == VAR_DECL 3378189824Sdas && current_scope != file_scope 3379189824Sdas && TREE_STATIC (decl) 3380189824Sdas && !TREE_READONLY (decl) 3381189824Sdas && DECL_DECLARED_INLINE_P (current_function_decl) 3382189824Sdas && DECL_EXTERNAL (current_function_decl)) 3383189824Sdas pedwarn ("%q+D is static but declared in inline function %qD " 3384189824Sdas "which is not static", decl, current_function_decl); 3385189824Sdas 3386132730Skan /* Add this decl to the current scope. 338718334Speter TEM may equal DECL or it may be a previous decl of the same name. */ 338818334Speter tem = pushdecl (decl); 338918334Speter 3390169699Skan if (initialized && DECL_EXTERNAL (tem)) 339118334Speter { 3392169699Skan DECL_EXTERNAL (tem) = 0; 3393169699Skan TREE_STATIC (tem) = 1; 339418334Speter } 339518334Speter 339618334Speter return tem; 339718334Speter} 339818334Speter 3399169699Skan/* Initialize EH if not initialized yet and exceptions are enabled. */ 3400169699Skan 3401169699Skanvoid 3402169699Skanc_maybe_initialize_eh (void) 3403169699Skan{ 3404169699Skan if (!flag_exceptions || c_eh_initialized_p) 3405169699Skan return; 3406169699Skan 3407169699Skan c_eh_initialized_p = true; 3408169699Skan eh_personality_libfunc 3409169699Skan = init_one_libfunc (USING_SJLJ_EXCEPTIONS 3410169699Skan ? "__gcc_personality_sj0" 3411169699Skan : "__gcc_personality_v0"); 3412169699Skan default_init_unwind_resume_libfunc (); 3413169699Skan using_eh_for_cleanups (); 3414169699Skan} 3415169699Skan 341618334Speter/* Finish processing of a declaration; 341718334Speter install its initial value. 341818334Speter If the length of an array type is not known before, 341918334Speter it must be determined now, from the initial value, or it is an error. */ 342018334Speter 342118334Spetervoid 3422132730Skanfinish_decl (tree decl, tree init, tree asmspec_tree) 342318334Speter{ 3424169699Skan tree type; 342518334Speter int was_incomplete = (DECL_SIZE (decl) == 0); 342690075Sobrien const char *asmspec = 0; 342718334Speter 342890075Sobrien /* If a name was specified, get the string. */ 3429169699Skan if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL) 3430169699Skan && DECL_FILE_SCOPE_P (decl)) 343196263Sobrien asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); 343218334Speter if (asmspec_tree) 343318334Speter asmspec = TREE_STRING_POINTER (asmspec_tree); 343418334Speter 343518334Speter /* If `start_decl' didn't like having an initialization, ignore it now. */ 343618334Speter if (init != 0 && DECL_INITIAL (decl) == 0) 343718334Speter init = 0; 3438132730Skan 343918334Speter /* Don't crash if parm is initialized. */ 344018334Speter if (TREE_CODE (decl) == PARM_DECL) 344118334Speter init = 0; 344218334Speter 344318334Speter if (init) 3444104761Skan store_init_value (decl, init); 344518334Speter 3446132730Skan if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL 3447169699Skan || TREE_CODE (decl) == FUNCTION_DECL 3448169699Skan || TREE_CODE (decl) == FIELD_DECL)) 3449132730Skan objc_check_decl (decl); 3450132730Skan 3451169699Skan type = TREE_TYPE (decl); 3452169699Skan 3453132730Skan /* Deduce size of array from initialization, if not already known. */ 345418334Speter if (TREE_CODE (type) == ARRAY_TYPE 345518334Speter && TYPE_DOMAIN (type) == 0 345618334Speter && TREE_CODE (decl) != TYPE_DECL) 345718334Speter { 3458169699Skan bool do_default 345918334Speter = (TREE_STATIC (decl) 346018334Speter /* Even if pedantic, an external linkage array 346118334Speter may have incomplete type at first. */ 346218334Speter ? pedantic && !TREE_PUBLIC (decl) 346318334Speter : !DECL_EXTERNAL (decl)); 346418334Speter int failure 3465169699Skan = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl), 3466169699Skan do_default); 346718334Speter 346818334Speter /* Get the completed type made by complete_array_type. */ 346918334Speter type = TREE_TYPE (decl); 347018334Speter 3471169699Skan switch (failure) 3472169699Skan { 3473169699Skan case 1: 3474169699Skan error ("initializer fails to determine size of %q+D", decl); 3475169699Skan break; 347618334Speter 3477169699Skan case 2: 347818334Speter if (do_default) 3479169699Skan error ("array size missing in %q+D", decl); 348018334Speter /* If a `static' var's size isn't known, 348118334Speter make it extern as well as static, so it does not get 348218334Speter allocated. 348318334Speter If it is not `static', then do not mark extern; 348418334Speter finish_incomplete_decl will give it a default size 348518334Speter and it will get allocated. */ 3486169699Skan else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) 348718334Speter DECL_EXTERNAL (decl) = 1; 3488169699Skan break; 3489169699Skan 3490169699Skan case 3: 3491169699Skan error ("zero or negative size array %q+D", decl); 3492169699Skan break; 3493169699Skan 3494169699Skan case 0: 3495169699Skan /* For global variables, update the copy of the type that 3496169699Skan exists in the binding. */ 3497169699Skan if (TREE_PUBLIC (decl)) 3498169699Skan { 3499169699Skan struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl)); 3500169699Skan while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext)) 3501169699Skan b_ext = b_ext->shadowed; 3502169699Skan if (b_ext) 3503169699Skan { 3504169699Skan if (b_ext->type) 3505169699Skan b_ext->type = composite_type (b_ext->type, type); 3506169699Skan else 3507169699Skan b_ext->type = type; 3508169699Skan } 3509169699Skan } 3510169699Skan break; 3511169699Skan 3512169699Skan default: 3513169699Skan gcc_unreachable (); 351418334Speter } 351518334Speter 3516169699Skan if (DECL_INITIAL (decl)) 3517169699Skan TREE_TYPE (DECL_INITIAL (decl)) = type; 351818334Speter 351918334Speter layout_decl (decl, 0); 352018334Speter } 352118334Speter 352218334Speter if (TREE_CODE (decl) == VAR_DECL) 352318334Speter { 3524169699Skan if (init && TREE_CODE (init) == CONSTRUCTOR) 3525169699Skan add_flexible_array_elts_to_size (decl, init); 3526169699Skan 352790075Sobrien if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node 352890075Sobrien && COMPLETE_TYPE_P (TREE_TYPE (decl))) 352918334Speter layout_decl (decl, 0); 353018334Speter 353118334Speter if (DECL_SIZE (decl) == 0 353290075Sobrien /* Don't give an error if we already gave one earlier. */ 353390075Sobrien && TREE_TYPE (decl) != error_mark_node 353418334Speter && (TREE_STATIC (decl) 3535169699Skan /* A static variable with an incomplete type 3536169699Skan is an error if it is initialized. 3537169699Skan Also if it is not file scope. 3538169699Skan Otherwise, let it through, but if it is not `extern' 3539169699Skan then it may cause an error message later. */ 3540169699Skan ? (DECL_INITIAL (decl) != 0 3541132730Skan || !DECL_FILE_SCOPE_P (decl)) 3542169699Skan /* An automatic variable with an incomplete type 3543169699Skan is an error. */ 3544169699Skan : !DECL_EXTERNAL (decl))) 3545169699Skan { 3546169699Skan error ("storage size of %q+D isn%'t known", decl); 3547169699Skan TREE_TYPE (decl) = error_mark_node; 3548169699Skan } 354918334Speter 355018334Speter if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 355118334Speter && DECL_SIZE (decl) != 0) 355218334Speter { 355318334Speter if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) 355418334Speter constant_expression_warning (DECL_SIZE (decl)); 355518334Speter else 3556169699Skan error ("storage size of %q+D isn%'t constant", decl); 355718334Speter } 355850397Sobrien 355990075Sobrien if (TREE_USED (type)) 356050397Sobrien TREE_USED (decl) = 1; 356118334Speter } 356218334Speter 3563132730Skan /* If this is a function and an assembler name is specified, reset DECL_RTL 3564132730Skan so we can give it its new name. Also, update built_in_decls if it 3565132730Skan was a normal built-in. */ 356618334Speter if (TREE_CODE (decl) == FUNCTION_DECL && asmspec) 356790075Sobrien { 3568132730Skan if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) 3569169699Skan set_builtin_user_assembler_name (decl, asmspec); 3570169699Skan set_user_assembler_name (decl, asmspec); 357190075Sobrien } 357218334Speter 3573132730Skan /* If #pragma weak was used, mark the decl weak now. */ 3574169699Skan maybe_apply_pragma_weak (decl); 3575132730Skan 357618334Speter /* Output the assembler code and/or RTL code for variables and functions, 357718334Speter unless the type is an undefined structure or union. 357818334Speter If not, it will get done when the type is completed. */ 357918334Speter 358018334Speter if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL) 358118334Speter { 3582169699Skan /* Determine the ELF visibility. */ 3583169699Skan if (TREE_PUBLIC (decl)) 3584169699Skan c_determine_visibility (decl); 3585169699Skan 3586117421Skan /* This is a no-op in c-lang.c or something real in objc-act.c. */ 3587132730Skan if (c_dialect_objc ()) 3588117421Skan objc_check_decl (decl); 358990075Sobrien 3590169699Skan if (asmspec) 3591169699Skan { 3592169699Skan /* If this is not a static variable, issue a warning. 3593169699Skan It doesn't make any sense to give an ASMSPEC for an 3594169699Skan ordinary, non-register local variable. Historically, 3595169699Skan GCC has accepted -- but ignored -- the ASMSPEC in 3596169699Skan this case. */ 3597169699Skan if (!DECL_FILE_SCOPE_P (decl) 3598169699Skan && TREE_CODE (decl) == VAR_DECL 3599169699Skan && !C_DECL_REGISTER (decl) 3600169699Skan && !TREE_STATIC (decl)) 3601169699Skan warning (0, "ignoring asm-specifier for non-static local " 3602169699Skan "variable %q+D", decl); 3603169699Skan else 3604169699Skan set_user_assembler_name (decl, asmspec); 3605169699Skan } 3606169699Skan 3607132730Skan if (DECL_FILE_SCOPE_P (decl)) 360818334Speter { 360990075Sobrien if (DECL_INITIAL (decl) == NULL_TREE 361090075Sobrien || DECL_INITIAL (decl) == error_mark_node) 361190075Sobrien /* Don't output anything 361290075Sobrien when a tentative file-scope definition is seen. 361390075Sobrien But at end of compilation, do output code for them. */ 361490075Sobrien DECL_DEFER_OUTPUT (decl) = 1; 3615169699Skan rest_of_decl_compilation (decl, true, 0); 361618334Speter } 361718334Speter else 361818334Speter { 3619169699Skan /* In conjunction with an ASMSPEC, the `register' 3620169699Skan keyword indicates that we should place the variable 3621169699Skan in a particular register. */ 3622169699Skan if (asmspec && C_DECL_REGISTER (decl)) 362390075Sobrien { 3624169699Skan DECL_HARD_REGISTER (decl) = 1; 3625169699Skan /* This cannot be done for a structure with volatile 3626169699Skan fields, on which DECL_REGISTER will have been 3627169699Skan reset. */ 3628169699Skan if (!DECL_REGISTER (decl)) 3629169699Skan error ("cannot put object with volatile field into register"); 363090075Sobrien } 363190075Sobrien 363290075Sobrien if (TREE_CODE (decl) != FUNCTION_DECL) 3633169699Skan { 3634169699Skan /* If we're building a variable sized type, and we might be 3635169699Skan reachable other than via the top of the current binding 3636169699Skan level, then create a new BIND_EXPR so that we deallocate 3637169699Skan the object at the right time. */ 3638169699Skan /* Note that DECL_SIZE can be null due to errors. */ 3639169699Skan if (DECL_SIZE (decl) 3640169699Skan && !TREE_CONSTANT (DECL_SIZE (decl)) 3641169699Skan && STATEMENT_LIST_HAS_LABEL (cur_stmt_list)) 3642169699Skan { 3643169699Skan tree bind; 3644169699Skan bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL); 3645169699Skan TREE_SIDE_EFFECTS (bind) = 1; 3646169699Skan add_stmt (bind); 3647169699Skan BIND_EXPR_BODY (bind) = push_stmt_list (); 3648169699Skan } 3649169699Skan add_stmt (build_stmt (DECL_EXPR, decl)); 3650169699Skan } 365118334Speter } 365290075Sobrien 3653169699Skan 3654132730Skan if (!DECL_FILE_SCOPE_P (decl)) 365518334Speter { 365618334Speter /* Recompute the RTL of a local array now 365718334Speter if it used to be an incomplete type. */ 365818334Speter if (was_incomplete 3659169699Skan && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) 366018334Speter { 366118334Speter /* If we used it already as memory, it must stay in memory. */ 366218334Speter TREE_ADDRESSABLE (decl) = TREE_USED (decl); 366318334Speter /* If it's still incomplete now, no init will save it. */ 366418334Speter if (DECL_SIZE (decl) == 0) 366518334Speter DECL_INITIAL (decl) = 0; 366618334Speter } 366718334Speter } 366818334Speter } 366918334Speter 3670132730Skan /* If this was marked 'used', be sure it will be output. */ 3671169699Skan if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl))) 3672169699Skan mark_decl_referenced (decl); 3673132730Skan 367418334Speter if (TREE_CODE (decl) == TYPE_DECL) 3675169699Skan { 3676169699Skan if (!DECL_FILE_SCOPE_P (decl) 3677169699Skan && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE)) 3678169699Skan add_stmt (build_stmt (DECL_EXPR, decl)); 367918334Speter 3680169699Skan rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0); 3681169699Skan } 3682169699Skan 368318334Speter /* At the end of a declaration, throw away any variable type sizes 368418334Speter of types defined inside that declaration. There is no use 368518334Speter computing them in the following function definition. */ 3686169699Skan if (current_scope == file_scope) 368718334Speter get_pending_sizes (); 368818334Speter 3689117421Skan /* Install a cleanup (aka destructor) if one was given. */ 3690117421Skan if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl)) 3691117421Skan { 3692117421Skan tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl)); 3693117421Skan if (attr) 3694117421Skan { 3695117421Skan tree cleanup_id = TREE_VALUE (TREE_VALUE (attr)); 3696117421Skan tree cleanup_decl = lookup_name (cleanup_id); 3697117421Skan tree cleanup; 3698117421Skan 3699117421Skan /* Build "cleanup(&decl)" for the destructor. */ 3700117421Skan cleanup = build_unary_op (ADDR_EXPR, decl, 0); 3701117421Skan cleanup = build_tree_list (NULL_TREE, cleanup); 3702117421Skan cleanup = build_function_call (cleanup_decl, cleanup); 3703117421Skan 3704117421Skan /* Don't warn about decl unused; the cleanup uses it. */ 3705117421Skan TREE_USED (decl) = 1; 3706169699Skan TREE_USED (cleanup_decl) = 1; 3707117421Skan 3708117421Skan /* Initialize EH, if we've been told to do so. */ 3709169699Skan c_maybe_initialize_eh (); 3710117421Skan 3711169699Skan push_cleanup (decl, cleanup, false); 3712117421Skan } 3713117421Skan } 371418334Speter} 371518334Speter 3716169699Skan/* Given a parsed parameter declaration, decode it into a PARM_DECL. */ 3717169699Skan 3718169699Skantree 3719169699Skangrokparm (const struct c_parm *parm) 3720169699Skan{ 3721169699Skan tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, 3722169699Skan NULL); 3723169699Skan 3724169699Skan decl_attributes (&decl, parm->attrs, 0); 3725169699Skan 3726169699Skan return decl; 3727169699Skan} 3728169699Skan 3729132730Skan/* Given a parsed parameter declaration, decode it into a PARM_DECL 3730132730Skan and push that on the current scope. */ 373118334Speter 373218334Spetervoid 3733169699Skanpush_parm_decl (const struct c_parm *parm) 373418334Speter{ 373518334Speter tree decl; 3736132730Skan 3737169699Skan decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL); 3738169699Skan decl_attributes (&decl, parm->attrs, 0); 373918334Speter 374018334Speter decl = pushdecl (decl); 374118334Speter 3742132730Skan finish_decl (decl, NULL_TREE, NULL_TREE); 374318334Speter} 374418334Speter 3745169699Skan/* Mark all the parameter declarations to date as forward decls. 3746132730Skan Also diagnose use of this extension. */ 374718334Speter 374818334Spetervoid 3749132730Skanmark_forward_parm_decls (void) 375018334Speter{ 3751169699Skan struct c_binding *b; 3752132730Skan 3753132730Skan if (pedantic && !current_scope->warned_forward_parm_decls) 3754132730Skan { 3755132730Skan pedwarn ("ISO C forbids forward parameter declarations"); 3756132730Skan current_scope->warned_forward_parm_decls = true; 3757132730Skan } 3758132730Skan 3759169699Skan for (b = current_scope->bindings; b; b = b->prev) 3760169699Skan if (TREE_CODE (b->decl) == PARM_DECL) 3761169699Skan TREE_ASM_WRITTEN (b->decl) = 1; 376218334Speter} 376318334Speter 376490075Sobrien/* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound 376590075Sobrien literal, which may be an incomplete array type completed by the 376690075Sobrien initializer; INIT is a CONSTRUCTOR that initializes the compound 376790075Sobrien literal. */ 376890075Sobrien 376990075Sobrientree 3770132730Skanbuild_compound_literal (tree type, tree init) 377190075Sobrien{ 377290075Sobrien /* We do not use start_decl here because we have a type, not a declarator; 377390075Sobrien and do not use finish_decl because the decl should be stored inside 3774169699Skan the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */ 3775169699Skan tree decl; 377690075Sobrien tree complit; 377790075Sobrien tree stmt; 3778169699Skan 3779169699Skan if (type == error_mark_node) 3780169699Skan return error_mark_node; 3781169699Skan 3782169699Skan decl = build_decl (VAR_DECL, NULL_TREE, type); 378390075Sobrien DECL_EXTERNAL (decl) = 0; 378490075Sobrien TREE_PUBLIC (decl) = 0; 3785169699Skan TREE_STATIC (decl) = (current_scope == file_scope); 378690075Sobrien DECL_CONTEXT (decl) = current_function_decl; 378790075Sobrien TREE_USED (decl) = 1; 378890075Sobrien TREE_TYPE (decl) = type; 3789169699Skan TREE_READONLY (decl) = TYPE_READONLY (type); 379090075Sobrien store_init_value (decl, init); 379190075Sobrien 379290075Sobrien if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type)) 379390075Sobrien { 3794169699Skan int failure = complete_array_type (&TREE_TYPE (decl), 3795169699Skan DECL_INITIAL (decl), true); 3796169699Skan gcc_assert (!failure); 3797169699Skan 3798169699Skan type = TREE_TYPE (decl); 3799169699Skan TREE_TYPE (DECL_INITIAL (decl)) = type; 380090075Sobrien } 380190075Sobrien 380290075Sobrien if (type == error_mark_node || !COMPLETE_TYPE_P (type)) 380390075Sobrien return error_mark_node; 380490075Sobrien 3805169699Skan stmt = build_stmt (DECL_EXPR, decl); 3806169699Skan complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt); 380790075Sobrien TREE_SIDE_EFFECTS (complit) = 1; 380890075Sobrien 380990075Sobrien layout_decl (decl, 0); 381090075Sobrien 381190075Sobrien if (TREE_STATIC (decl)) 381290075Sobrien { 3813169699Skan /* This decl needs a name for the assembler output. */ 3814169699Skan set_compound_literal_name (decl); 3815102790Skan DECL_DEFER_OUTPUT (decl) = 1; 3816102790Skan DECL_COMDAT (decl) = 1; 3817102790Skan DECL_ARTIFICIAL (decl) = 1; 3818169699Skan DECL_IGNORED_P (decl) = 1; 3819102790Skan pushdecl (decl); 3820169699Skan rest_of_decl_compilation (decl, 1, 0); 382190075Sobrien } 382290075Sobrien 382390075Sobrien return complit; 382490075Sobrien} 382590075Sobrien 3826117421Skan/* Determine whether TYPE is a structure with a flexible array member, 3827117421Skan or a union containing such a structure (possibly recursively). */ 3828117421Skan 3829117421Skanstatic bool 3830132730Skanflexible_array_type_p (tree type) 3831117421Skan{ 3832117421Skan tree x; 3833117421Skan switch (TREE_CODE (type)) 3834117421Skan { 3835117421Skan case RECORD_TYPE: 3836117421Skan x = TYPE_FIELDS (type); 3837117421Skan if (x == NULL_TREE) 3838117421Skan return false; 3839117421Skan while (TREE_CHAIN (x) != NULL_TREE) 3840117421Skan x = TREE_CHAIN (x); 3841117421Skan if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 3842117421Skan && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 3843117421Skan && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 3844117421Skan && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 3845117421Skan return true; 3846117421Skan return false; 3847117421Skan case UNION_TYPE: 3848117421Skan for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x)) 3849117421Skan { 3850117421Skan if (flexible_array_type_p (TREE_TYPE (x))) 3851117421Skan return true; 3852117421Skan } 3853117421Skan return false; 3854117421Skan default: 3855117421Skan return false; 3856117421Skan } 3857117421Skan} 3858117421Skan 3859132730Skan/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME, 3860132730Skan replacing with appropriate values if they are invalid. */ 3861132730Skanstatic void 3862132730Skancheck_bitfield_type_and_width (tree *type, tree *width, const char *orig_name) 3863132730Skan{ 3864132730Skan tree type_mv; 3865132730Skan unsigned int max_width; 3866132730Skan unsigned HOST_WIDE_INT w; 3867132730Skan const char *name = orig_name ? orig_name: _("<anonymous>"); 3868132730Skan 3869132730Skan /* Detect and ignore out of range field width and process valid 3870132730Skan field widths. */ 3871169699Skan if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)) 3872169699Skan || TREE_CODE (*width) != INTEGER_CST) 3873132730Skan { 3874169699Skan error ("bit-field %qs width not an integer constant", name); 3875132730Skan *width = integer_one_node; 3876132730Skan } 3877132730Skan else 3878132730Skan { 3879132730Skan constant_expression_warning (*width); 3880132730Skan if (tree_int_cst_sgn (*width) < 0) 3881132730Skan { 3882169699Skan error ("negative width in bit-field %qs", name); 3883132730Skan *width = integer_one_node; 3884132730Skan } 3885132730Skan else if (integer_zerop (*width) && orig_name) 3886132730Skan { 3887169699Skan error ("zero width for bit-field %qs", name); 3888132730Skan *width = integer_one_node; 3889132730Skan } 3890132730Skan } 3891132730Skan 3892132730Skan /* Detect invalid bit-field type. */ 3893132730Skan if (TREE_CODE (*type) != INTEGER_TYPE 3894132730Skan && TREE_CODE (*type) != BOOLEAN_TYPE 3895132730Skan && TREE_CODE (*type) != ENUMERAL_TYPE) 3896132730Skan { 3897169699Skan error ("bit-field %qs has invalid type", name); 3898132730Skan *type = unsigned_type_node; 3899132730Skan } 3900132730Skan 3901132730Skan type_mv = TYPE_MAIN_VARIANT (*type); 3902132730Skan if (pedantic 3903169699Skan && !in_system_header 3904132730Skan && type_mv != integer_type_node 3905132730Skan && type_mv != unsigned_type_node 3906132730Skan && type_mv != boolean_type_node) 3907169699Skan pedwarn ("type of bit-field %qs is a GCC extension", name); 3908132730Skan 3909132730Skan if (type_mv == boolean_type_node) 3910132730Skan max_width = CHAR_TYPE_SIZE; 3911132730Skan else 3912132730Skan max_width = TYPE_PRECISION (*type); 3913132730Skan 3914132730Skan if (0 < compare_tree_int (*width, max_width)) 3915132730Skan { 3916169699Skan error ("width of %qs exceeds its type", name); 3917132730Skan w = max_width; 3918169699Skan *width = build_int_cst (NULL_TREE, w); 3919132730Skan } 3920132730Skan else 3921132730Skan w = tree_low_cst (*width, 1); 3922132730Skan 3923169699Skan if (TREE_CODE (*type) == ENUMERAL_TYPE) 3924169699Skan { 3925169699Skan struct lang_type *lt = TYPE_LANG_SPECIFIC (*type); 3926169699Skan if (!lt 3927169699Skan || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type)) 3928169699Skan || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type))) 3929169699Skan warning (0, "%qs is narrower than values of its type", name); 3930169699Skan } 3931132730Skan} 3932169699Skan 3933132730Skan 393418334Speter/* Given declspecs and a declarator, 393518334Speter determine the name and type of the object declared 393618334Speter and construct a ..._DECL node for it. 393718334Speter (In one case we can return a ..._TYPE node instead. 393818334Speter For invalid input we sometimes return 0.) 393918334Speter 3940169699Skan DECLSPECS is a c_declspecs structure for the declaration specifiers. 394118334Speter 394218334Speter DECL_CONTEXT says which syntactic context this declaration is in: 394318334Speter NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. 394418334Speter FUNCDEF for a function definition. Like NORMAL but a few different 394518334Speter error messages in each case. Return value may be zero meaning 394618334Speter this definition is too screwy to try to parse. 394718334Speter PARM for a parameter declaration (either within a function prototype 394818334Speter or before a function body). Make a PARM_DECL, or return void_type_node. 394918334Speter TYPENAME if for a typename (in a cast or sizeof). 395018334Speter Don't make a DECL node; just return the ..._TYPE node. 395118334Speter FIELD for a struct or union field; make a FIELD_DECL. 3952169699Skan INITIALIZED is true if the decl has an initializer. 3953132730Skan WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node 3954132730Skan representing the width of the bit-field. 395518334Speter 395618334Speter In the TYPENAME case, DECLARATOR is really an absolute declarator. 395718334Speter It may also be so in the PARM case, for a prototype where the 395818334Speter argument type is specified but not the name. 395918334Speter 396018334Speter This function is where the complicated C meanings of `static' 396118334Speter and `extern' are interpreted. */ 396218334Speter 396318334Speterstatic tree 3964169699Skangrokdeclarator (const struct c_declarator *declarator, 3965169699Skan struct c_declspecs *declspecs, 3966169699Skan enum decl_context decl_context, bool initialized, tree *width) 396718334Speter{ 3968169699Skan tree type = declspecs->type; 3969169699Skan bool threadp = declspecs->thread_p; 3970169699Skan enum c_storage_class storage_class = declspecs->storage_class; 397118334Speter int constp; 397252284Sobrien int restrictp; 397318334Speter int volatilep; 397452284Sobrien int type_quals = TYPE_UNQUALIFIED; 3975132730Skan const char *name, *orig_name; 397618334Speter tree typedef_type = 0; 3977169699Skan bool funcdef_flag = false; 3978169699Skan bool funcdef_syntax = false; 397918334Speter int size_varies = 0; 3980169699Skan tree decl_attr = declspecs->decl_attr; 3981169699Skan int array_ptr_quals = TYPE_UNQUALIFIED; 3982169699Skan tree array_ptr_attrs = NULL_TREE; 398390075Sobrien int array_parm_static = 0; 3984169699Skan bool array_parm_vla_unspec_p = false; 398590075Sobrien tree returned_attrs = NULL_TREE; 3986132730Skan bool bitfield = width != NULL; 3987132730Skan tree element_type; 3988169699Skan struct c_arg_info *arg_info = 0; 398918334Speter 399018334Speter if (decl_context == FUNCDEF) 3991169699Skan funcdef_flag = true, decl_context = NORMAL; 399218334Speter 399318334Speter /* Look inside a declarator for the name being declared 399418334Speter and get it as a string, for an error message. */ 399518334Speter { 3996169699Skan const struct c_declarator *decl = declarator; 399718334Speter name = 0; 399818334Speter 399918334Speter while (decl) 4000169699Skan switch (decl->kind) 400118334Speter { 4002169699Skan case cdk_function: 4003169699Skan case cdk_array: 4004169699Skan case cdk_pointer: 4005169699Skan funcdef_syntax = (decl->kind == cdk_function); 4006169699Skan decl = decl->declarator; 400718334Speter break; 400818334Speter 4009169699Skan case cdk_attrs: 4010169699Skan decl = decl->declarator; 401190075Sobrien break; 401290075Sobrien 4013169699Skan case cdk_id: 4014169699Skan if (decl->u.id) 4015169699Skan name = IDENTIFIER_POINTER (decl->u.id); 401618334Speter decl = 0; 401718334Speter break; 401818334Speter 401918334Speter default: 4020169699Skan gcc_unreachable (); 402118334Speter } 4022132730Skan orig_name = name; 402318334Speter if (name == 0) 402418334Speter name = "type name"; 402518334Speter } 402618334Speter 402718334Speter /* A function definition's declarator must have the form of 402818334Speter a function declarator. */ 402918334Speter 4030169699Skan if (funcdef_flag && !funcdef_syntax) 403118334Speter return 0; 403218334Speter 403318334Speter /* If this looks like a function definition, make it one, 403418334Speter even if it occurs where parms are expected. 403518334Speter Then store_parm_decls will reject it and not use it as a parm. */ 4036169699Skan if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag) 403718334Speter decl_context = PARM; 403818334Speter 4039169699Skan if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS) 4040169699Skan warn_deprecated_use (declspecs->type); 404118334Speter 4042169699Skan if ((decl_context == NORMAL || decl_context == FIELD) 4043169699Skan && current_scope == file_scope 4044169699Skan && variably_modified_type_p (type, NULL_TREE)) 404518334Speter { 4046169699Skan error ("variably modified %qs at file scope", name); 4047169699Skan type = integer_type_node; 404818334Speter } 404918334Speter 405018334Speter typedef_type = type; 4051169699Skan size_varies = C_TYPE_VARIABLE_SIZE (type); 405218334Speter 4053169699Skan /* Diagnose defaulting to "int". */ 405418334Speter 4055169699Skan if (declspecs->default_int_p && !in_system_header) 405618334Speter { 4057169699Skan /* Issue a warning if this is an ISO C 99 program or if 4058169699Skan -Wreturn-type and this is a function, or if -Wimplicit; 4059169699Skan prefer the former warning since it is more explicit. */ 4060169699Skan if ((warn_implicit_int || warn_return_type || flag_isoc99) 4061169699Skan && funcdef_flag) 4062169699Skan warn_about_return_type = 1; 4063169699Skan else if (warn_implicit_int || flag_isoc99) 4064169699Skan pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name); 406518334Speter } 406618334Speter 4067169699Skan /* Adjust the type if a bit-field is being declared, 4068169699Skan -funsigned-bitfields applied and the type is not explicitly 4069169699Skan "signed". */ 4070169699Skan if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p 4071169699Skan && TREE_CODE (type) == INTEGER_TYPE) 4072169699Skan type = c_common_unsigned_type (type); 407318334Speter 407452284Sobrien /* Figure out the type qualifiers for the declaration. There are 407552284Sobrien two ways a declaration can become qualified. One is something 407652284Sobrien like `const int i' where the `const' is explicit. Another is 407752284Sobrien something like `typedef const int CI; CI i' where the type of the 4078132730Skan declaration contains the `const'. A third possibility is that 4079132730Skan there is a type qualifier on the element type of a typedefed 4080132730Skan array type, in which case we should extract that qualifier so 4081132730Skan that c_apply_type_quals_to_decls receives the full list of 4082132730Skan qualifiers to work with (C90 is not entirely clear about whether 4083132730Skan duplicate qualifiers should be diagnosed in this case, but it 4084132730Skan seems most appropriate to do so). */ 4085132730Skan element_type = strip_array_types (type); 4086169699Skan constp = declspecs->const_p + TYPE_READONLY (element_type); 4087169699Skan restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type); 4088169699Skan volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type); 4089132730Skan if (pedantic && !flag_isoc99) 4090132730Skan { 4091132730Skan if (constp > 1) 4092169699Skan pedwarn ("duplicate %<const%>"); 4093132730Skan if (restrictp > 1) 4094169699Skan pedwarn ("duplicate %<restrict%>"); 4095132730Skan if (volatilep > 1) 4096169699Skan pedwarn ("duplicate %<volatile%>"); 4097132730Skan } 4098169699Skan if (!flag_gen_aux_info && (TYPE_QUALS (element_type))) 409918334Speter type = TYPE_MAIN_VARIANT (type); 410052284Sobrien type_quals = ((constp ? TYPE_QUAL_CONST : 0) 410152284Sobrien | (restrictp ? TYPE_QUAL_RESTRICT : 0) 410252284Sobrien | (volatilep ? TYPE_QUAL_VOLATILE : 0)); 410318334Speter 4104169699Skan /* Warn about storage classes that are invalid for certain 4105169699Skan kinds of declarations (parameters, typenames, etc.). */ 410618334Speter 4107169699Skan if (funcdef_flag 4108169699Skan && (threadp 4109169699Skan || storage_class == csc_auto 4110169699Skan || storage_class == csc_register 4111169699Skan || storage_class == csc_typedef)) 4112169699Skan { 4113169699Skan if (storage_class == csc_auto 4114169699Skan && (pedantic || current_scope == file_scope)) 4115169699Skan pedwarn ("function definition declared %<auto%>"); 4116169699Skan if (storage_class == csc_register) 4117169699Skan error ("function definition declared %<register%>"); 4118169699Skan if (storage_class == csc_typedef) 4119169699Skan error ("function definition declared %<typedef%>"); 4120169699Skan if (threadp) 4121169699Skan error ("function definition declared %<__thread%>"); 4122169699Skan threadp = false; 4123169699Skan if (storage_class == csc_auto 4124169699Skan || storage_class == csc_register 4125169699Skan || storage_class == csc_typedef) 4126169699Skan storage_class = csc_none; 4127169699Skan } 4128169699Skan else if (decl_context != NORMAL && (storage_class != csc_none || threadp)) 4129169699Skan { 4130169699Skan if (decl_context == PARM && storage_class == csc_register) 4131169699Skan ; 4132169699Skan else 4133169699Skan { 4134169699Skan switch (decl_context) 4135169699Skan { 4136169699Skan case FIELD: 4137169699Skan error ("storage class specified for structure field %qs", 4138169699Skan name); 4139169699Skan break; 4140169699Skan case PARM: 4141169699Skan error ("storage class specified for parameter %qs", name); 4142169699Skan break; 4143169699Skan default: 4144169699Skan error ("storage class specified for typename"); 4145169699Skan break; 4146169699Skan } 4147169699Skan storage_class = csc_none; 4148169699Skan threadp = false; 4149169699Skan } 4150169699Skan } 4151169699Skan else if (storage_class == csc_extern 4152169699Skan && initialized 4153169699Skan && !funcdef_flag) 4154169699Skan { 4155169699Skan /* 'extern' with initialization is invalid if not at file scope. */ 4156169699Skan if (current_scope == file_scope) 4157169699Skan { 4158169699Skan /* It is fine to have 'extern const' when compiling at C 4159169699Skan and C++ intersection. */ 4160169699Skan if (!(warn_cxx_compat && constp)) 4161169699Skan warning (0, "%qs initialized and declared %<extern%>", name); 4162169699Skan } 4163169699Skan else 4164169699Skan error ("%qs has both %<extern%> and initializer", name); 4165169699Skan } 4166169699Skan else if (current_scope == file_scope) 4167169699Skan { 4168169699Skan if (storage_class == csc_auto) 4169169699Skan error ("file-scope declaration of %qs specifies %<auto%>", name); 4170169699Skan if (pedantic && storage_class == csc_register) 4171169699Skan pedwarn ("file-scope declaration of %qs specifies %<register%>", name); 4172169699Skan } 4173169699Skan else 4174169699Skan { 4175169699Skan if (storage_class == csc_extern && funcdef_flag) 4176169699Skan error ("nested function %qs declared %<extern%>", name); 4177169699Skan else if (threadp && storage_class == csc_none) 4178169699Skan { 4179169699Skan error ("function-scope %qs implicitly auto and declared " 4180169699Skan "%<__thread%>", 4181169699Skan name); 4182169699Skan threadp = false; 4183169699Skan } 4184169699Skan } 418518334Speter 418618334Speter /* Now figure out the structure of the declarator proper. 418718334Speter Descend through it, creating more complex types, until we reach 4188169699Skan the declared identifier (or NULL_TREE, in an absolute declarator). 4189169699Skan At each stage we maintain an unqualified version of the type 4190169699Skan together with any qualifiers that should be applied to it with 4191169699Skan c_build_qualified_type; this way, array types including 4192169699Skan multidimensional array types are first built up in unqualified 4193169699Skan form and then the qualified form is created with 4194169699Skan TYPE_MAIN_VARIANT pointing to the unqualified form. */ 419518334Speter 4196169699Skan while (declarator && declarator->kind != cdk_id) 419718334Speter { 419818334Speter if (type == error_mark_node) 419918334Speter { 4200169699Skan declarator = declarator->declarator; 420118334Speter continue; 420218334Speter } 420318334Speter 4204169699Skan /* Each level of DECLARATOR is either a cdk_array (for ...[..]), 4205169699Skan a cdk_pointer (for *...), 4206169699Skan a cdk_function (for ...(...)), 4207169699Skan a cdk_attrs (for nested attributes), 4208169699Skan or a cdk_id (for the name being declared 4209169699Skan or the place in an absolute declarator 421018334Speter where the name was omitted). 4211169699Skan For the last case, we have just exited the loop. 421218334Speter 421318334Speter At this point, TYPE is the type of elements of an array, 421418334Speter or for a function to return, or for a pointer to point to. 421518334Speter After this sequence of ifs, TYPE is the type of the 421618334Speter array or function or pointer, and DECLARATOR has had its 421718334Speter outermost layer removed. */ 421818334Speter 4219169699Skan if (array_ptr_quals != TYPE_UNQUALIFIED 4220169699Skan || array_ptr_attrs != NULL_TREE 4221169699Skan || array_parm_static) 422218334Speter { 422390075Sobrien /* Only the innermost declarator (making a parameter be of 422490075Sobrien array type which is converted to pointer type) 422590075Sobrien may have static or type qualifiers. */ 422690075Sobrien error ("static or type qualifiers in non-parameter array declarator"); 4227169699Skan array_ptr_quals = TYPE_UNQUALIFIED; 4228169699Skan array_ptr_attrs = NULL_TREE; 422990075Sobrien array_parm_static = 0; 423090075Sobrien } 423190075Sobrien 4232169699Skan switch (declarator->kind) 423390075Sobrien { 4234169699Skan case cdk_attrs: 4235169699Skan { 4236169699Skan /* A declarator with embedded attributes. */ 4237169699Skan tree attrs = declarator->u.attrs; 4238169699Skan const struct c_declarator *inner_decl; 4239169699Skan int attr_flags = 0; 4240169699Skan declarator = declarator->declarator; 4241169699Skan inner_decl = declarator; 4242169699Skan while (inner_decl->kind == cdk_attrs) 4243169699Skan inner_decl = inner_decl->declarator; 4244169699Skan if (inner_decl->kind == cdk_id) 4245169699Skan attr_flags |= (int) ATTR_FLAG_DECL_NEXT; 4246169699Skan else if (inner_decl->kind == cdk_function) 4247169699Skan attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; 4248169699Skan else if (inner_decl->kind == cdk_array) 4249169699Skan attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; 4250169699Skan returned_attrs = decl_attributes (&type, 4251169699Skan chainon (returned_attrs, attrs), 4252169699Skan attr_flags); 4253169699Skan break; 4254169699Skan } 4255169699Skan case cdk_array: 4256169699Skan { 4257169699Skan tree itype = NULL_TREE; 4258169699Skan tree size = declarator->u.array.dimen; 4259169699Skan /* The index is a signed object `sizetype' bits wide. */ 4260169699Skan tree index_type = c_common_signed_type (sizetype); 426118334Speter 4262169699Skan array_ptr_quals = declarator->u.array.quals; 4263169699Skan array_ptr_attrs = declarator->u.array.attrs; 4264169699Skan array_parm_static = declarator->u.array.static_p; 4265169699Skan array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p; 426690075Sobrien 4267169699Skan declarator = declarator->declarator; 426818334Speter 4269169699Skan /* Check for some types that there cannot be arrays of. */ 427018334Speter 4271169699Skan if (VOID_TYPE_P (type)) 4272169699Skan { 4273169699Skan error ("declaration of %qs as array of voids", name); 4274169699Skan type = error_mark_node; 4275169699Skan } 427618334Speter 4277169699Skan if (TREE_CODE (type) == FUNCTION_TYPE) 4278169699Skan { 4279169699Skan error ("declaration of %qs as array of functions", name); 4280169699Skan type = error_mark_node; 4281169699Skan } 4282169699Skan 4283169699Skan if (pedantic && !in_system_header && flexible_array_type_p (type)) 4284169699Skan pedwarn ("invalid use of structure with flexible array member"); 4285169699Skan 4286169699Skan if (size == error_mark_node) 428718334Speter type = error_mark_node; 428818334Speter 4289169699Skan if (type == error_mark_node) 4290169699Skan continue; 4291117421Skan 4292169699Skan /* If size was specified, set ITYPE to a range-type for 4293169699Skan that size. Otherwise, ITYPE remains null. finish_decl 4294169699Skan may figure it out from an initial value. */ 429518334Speter 4296169699Skan if (size) 4297169699Skan { 4298169699Skan /* Strip NON_LVALUE_EXPRs since we aren't using as an 4299169699Skan lvalue. */ 4300169699Skan STRIP_TYPE_NOPS (size); 430118334Speter 4302169699Skan if (!INTEGRAL_TYPE_P (TREE_TYPE (size))) 4303169699Skan { 4304169699Skan error ("size of array %qs has non-integer type", name); 4305169699Skan size = integer_one_node; 4306169699Skan } 430718334Speter 4308169699Skan if (pedantic && integer_zerop (size)) 4309169699Skan pedwarn ("ISO C forbids zero-size array %qs", name); 431018334Speter 4311169699Skan if (TREE_CODE (size) == INTEGER_CST) 4312169699Skan { 4313169699Skan constant_expression_warning (size); 4314169699Skan if (tree_int_cst_sgn (size) < 0) 4315169699Skan { 4316169699Skan error ("size of array %qs is negative", name); 4317169699Skan size = integer_one_node; 4318169699Skan } 4319169699Skan } 4320169699Skan else if ((decl_context == NORMAL || decl_context == FIELD) 4321169699Skan && current_scope == file_scope) 4322169699Skan { 4323169699Skan error ("variably modified %qs at file scope", name); 4324169699Skan size = integer_one_node; 4325169699Skan } 4326169699Skan else 4327169699Skan { 4328169699Skan /* Make sure the array size remains visibly 4329169699Skan nonconstant even if it is (eg) a const variable 4330169699Skan with known value. */ 4331169699Skan size_varies = 1; 433218334Speter 4333169699Skan if (!flag_isoc99 && pedantic) 4334169699Skan { 4335169699Skan if (TREE_CONSTANT (size)) 4336169699Skan pedwarn ("ISO C90 forbids array %qs whose size " 4337169699Skan "can%'t be evaluated", 4338169699Skan name); 4339169699Skan else 4340169699Skan pedwarn ("ISO C90 forbids variable-size array %qs", 4341169699Skan name); 4342169699Skan } 4343169699Skan } 434418334Speter 4345169699Skan if (integer_zerop (size)) 4346169699Skan { 4347169699Skan /* A zero-length array cannot be represented with 4348169699Skan an unsigned index type, which is what we'll 4349169699Skan get with build_index_type. Create an 4350169699Skan open-ended range instead. */ 4351169699Skan itype = build_range_type (sizetype, size, NULL_TREE); 4352169699Skan } 4353169699Skan else 4354169699Skan { 4355169699Skan /* Arrange for the SAVE_EXPR on the inside of the 4356169699Skan MINUS_EXPR, which allows the -1 to get folded 4357169699Skan with the +1 that happens when building TYPE_SIZE. */ 4358169699Skan if (size_varies) 4359169699Skan size = variable_size (size); 436018334Speter 4361169699Skan /* Compute the maximum valid index, that is, size 4362169699Skan - 1. Do the calculation in index_type, so that 4363169699Skan if it is a variable the computations will be 4364169699Skan done in the proper mode. */ 4365169699Skan itype = fold_build2 (MINUS_EXPR, index_type, 4366169699Skan convert (index_type, size), 4367169699Skan convert (index_type, 4368169699Skan size_one_node)); 436918334Speter 4370169699Skan /* If that overflowed, the array is too big. ??? 4371169699Skan While a size of INT_MAX+1 technically shouldn't 4372169699Skan cause an overflow (because we subtract 1), the 4373169699Skan overflow is recorded during the conversion to 4374169699Skan index_type, before the subtraction. Handling 4375169699Skan this case seems like an unnecessary 4376169699Skan complication. */ 4377169699Skan if (TREE_CODE (itype) == INTEGER_CST 4378169699Skan && TREE_OVERFLOW (itype)) 4379169699Skan { 4380169699Skan error ("size of array %qs is too large", name); 4381169699Skan type = error_mark_node; 4382169699Skan continue; 4383169699Skan } 438450397Sobrien 4385169699Skan itype = build_index_type (itype); 4386169699Skan } 4387169699Skan } 4388169699Skan else if (decl_context == FIELD) 4389169699Skan { 4390169699Skan if (pedantic && !flag_isoc99 && !in_system_header) 4391169699Skan pedwarn ("ISO C90 does not support flexible array members"); 439290075Sobrien 4393169699Skan /* ISO C99 Flexible array members are effectively 4394169699Skan identical to GCC's zero-length array extension. */ 4395169699Skan itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 4396169699Skan } 4397169699Skan else if (decl_context == PARM) 4398169699Skan { 4399169699Skan if (array_parm_vla_unspec_p) 4400169699Skan { 4401169699Skan if (! orig_name) 4402169699Skan { 4403169699Skan /* C99 6.7.5.2p4 */ 4404169699Skan error ("%<[*]%> not allowed in other than a declaration"); 4405169699Skan } 440618334Speter 4407169699Skan itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 4408169699Skan size_varies = 1; 4409169699Skan } 4410169699Skan } 4411169699Skan else if (decl_context == TYPENAME) 4412169699Skan { 4413169699Skan if (array_parm_vla_unspec_p) 4414169699Skan { 4415169699Skan /* The error is printed elsewhere. We use this to 4416169699Skan avoid messing up with incomplete array types of 4417169699Skan the same type, that would otherwise be modified 4418169699Skan below. */ 4419169699Skan itype = build_range_type (sizetype, size_zero_node, 4420169699Skan NULL_TREE); 4421169699Skan } 4422169699Skan } 442318334Speter 4424169699Skan /* Complain about arrays of incomplete types. */ 4425169699Skan if (!COMPLETE_TYPE_P (type)) 4426169699Skan { 4427169699Skan error ("array type has incomplete element type"); 4428169699Skan type = error_mark_node; 4429169699Skan } 4430169699Skan else 4431169699Skan /* When itype is NULL, a shared incomplete array type is 4432169699Skan returned for all array of a given type. Elsewhere we 4433169699Skan make sure we don't complete that type before copying 4434169699Skan it, but here we want to make sure we don't ever 4435169699Skan modify the shared type, so we gcc_assert (itype) 4436169699Skan below. */ 4437169699Skan type = build_array_type (type, itype); 443890075Sobrien 4439169699Skan if (type != error_mark_node) 4440169699Skan { 4441169699Skan if (size_varies) 4442169699Skan { 4443169699Skan /* It is ok to modify type here even if itype is 4444169699Skan NULL: if size_varies, we're in a 4445169699Skan multi-dimensional array and the inner type has 4446169699Skan variable size, so the enclosing shared array type 4447169699Skan must too. */ 4448169699Skan if (size && TREE_CODE (size) == INTEGER_CST) 4449169699Skan type 4450169699Skan = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); 4451169699Skan C_TYPE_VARIABLE_SIZE (type) = 1; 4452169699Skan } 445390075Sobrien 4454169699Skan /* The GCC extension for zero-length arrays differs from 4455169699Skan ISO flexible array members in that sizeof yields 4456169699Skan zero. */ 4457169699Skan if (size && integer_zerop (size)) 4458169699Skan { 4459169699Skan gcc_assert (itype); 4460169699Skan TYPE_SIZE (type) = bitsize_zero_node; 4461169699Skan TYPE_SIZE_UNIT (type) = size_zero_node; 4462169699Skan } 4463169699Skan if (array_parm_vla_unspec_p) 4464169699Skan { 4465169699Skan gcc_assert (itype); 4466169699Skan /* The type is complete. C99 6.7.5.2p4 */ 4467169699Skan TYPE_SIZE (type) = bitsize_zero_node; 4468169699Skan TYPE_SIZE_UNIT (type) = size_zero_node; 4469169699Skan } 4470169699Skan } 447118334Speter 4472169699Skan if (decl_context != PARM 4473169699Skan && (array_ptr_quals != TYPE_UNQUALIFIED 4474169699Skan || array_ptr_attrs != NULL_TREE 4475169699Skan || array_parm_static)) 4476169699Skan { 4477169699Skan error ("static or type qualifiers in non-parameter array declarator"); 4478169699Skan array_ptr_quals = TYPE_UNQUALIFIED; 4479169699Skan array_ptr_attrs = NULL_TREE; 4480169699Skan array_parm_static = 0; 4481169699Skan } 4482169699Skan break; 4483169699Skan } 4484169699Skan case cdk_function: 4485169699Skan { 4486169699Skan /* Say it's a definition only for the declarator closest 4487169699Skan to the identifier, apart possibly from some 4488169699Skan attributes. */ 4489169699Skan bool really_funcdef = false; 4490169699Skan tree arg_types; 4491169699Skan if (funcdef_flag) 4492169699Skan { 4493169699Skan const struct c_declarator *t = declarator->declarator; 4494169699Skan while (t->kind == cdk_attrs) 4495169699Skan t = t->declarator; 4496169699Skan really_funcdef = (t->kind == cdk_id); 4497169699Skan } 449818334Speter 4499169699Skan /* Declaring a function type. Make sure we have a valid 4500169699Skan type for the function to return. */ 4501169699Skan if (type == error_mark_node) 4502169699Skan continue; 450390075Sobrien 4504169699Skan size_varies = 0; 450518334Speter 4506169699Skan /* Warn about some types functions can't return. */ 4507169699Skan if (TREE_CODE (type) == FUNCTION_TYPE) 4508169699Skan { 4509169699Skan error ("%qs declared as function returning a function", name); 4510169699Skan type = integer_type_node; 4511169699Skan } 4512169699Skan if (TREE_CODE (type) == ARRAY_TYPE) 4513169699Skan { 4514169699Skan error ("%qs declared as function returning an array", name); 4515169699Skan type = integer_type_node; 4516169699Skan } 451718334Speter 4518169699Skan /* Construct the function type and go to the next 4519169699Skan inner layer of declarator. */ 4520169699Skan arg_info = declarator->u.arg_info; 4521169699Skan arg_types = grokparms (arg_info, really_funcdef); 4522169699Skan if (really_funcdef) 4523169699Skan put_pending_sizes (arg_info->pending_sizes); 452418334Speter 4525169699Skan /* Type qualifiers before the return type of the function 4526169699Skan qualify the return type, not the function type. */ 4527169699Skan if (type_quals) 4528169699Skan { 4529169699Skan /* Type qualifiers on a function return type are 4530169699Skan normally permitted by the standard but have no 4531169699Skan effect, so give a warning at -Wreturn-type. 4532169699Skan Qualifiers on a void return type are banned on 4533169699Skan function definitions in ISO C; GCC used to used 4534169699Skan them for noreturn functions. */ 4535169699Skan if (VOID_TYPE_P (type) && really_funcdef) 4536169699Skan pedwarn ("function definition has qualified void return type"); 4537169699Skan else 4538169699Skan warning (OPT_Wreturn_type, 4539169699Skan "type qualifiers ignored on function return type"); 454018334Speter 4541169699Skan type = c_build_qualified_type (type, type_quals); 4542169699Skan } 4543169699Skan type_quals = TYPE_UNQUALIFIED; 454418334Speter 4545169699Skan type = build_function_type (type, arg_types); 4546169699Skan declarator = declarator->declarator; 454718334Speter 4548169699Skan /* Set the TYPE_CONTEXTs for each tagged type which is local to 4549169699Skan the formal parameter list of this FUNCTION_TYPE to point to 4550169699Skan the FUNCTION_TYPE node itself. */ 455190075Sobrien { 4552169699Skan tree link; 455390075Sobrien 4554169699Skan for (link = arg_info->tags; 4555169699Skan link; 4556169699Skan link = TREE_CHAIN (link)) 4557169699Skan TYPE_CONTEXT (TREE_VALUE (link)) = type; 455890075Sobrien } 4559169699Skan break; 4560169699Skan } 4561169699Skan case cdk_pointer: 456218334Speter { 4563169699Skan /* Merge any constancy or volatility into the target type 4564169699Skan for the pointer. */ 456518334Speter 4566169699Skan if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 4567169699Skan && type_quals) 4568169699Skan pedwarn ("ISO C forbids qualified function types"); 4569169699Skan if (type_quals) 4570169699Skan type = c_build_qualified_type (type, type_quals); 4571169699Skan size_varies = 0; 457218334Speter 4573169699Skan /* When the pointed-to type involves components of variable size, 4574169699Skan care must be taken to ensure that the size evaluation code is 4575169699Skan emitted early enough to dominate all the possible later uses 4576169699Skan and late enough for the variables on which it depends to have 4577169699Skan been assigned. 457818334Speter 4579169699Skan This is expected to happen automatically when the pointed-to 4580169699Skan type has a name/declaration of it's own, but special attention 4581169699Skan is required if the type is anonymous. 458218334Speter 4583169699Skan We handle the NORMAL and FIELD contexts here by attaching an 4584169699Skan artificial TYPE_DECL to such pointed-to type. This forces the 4585169699Skan sizes evaluation at a safe point and ensures it is not deferred 4586169699Skan until e.g. within a deeper conditional context. 458718334Speter 4588169699Skan We expect nothing to be needed here for PARM or TYPENAME. 4589169699Skan Pushing a TYPE_DECL at this point for TYPENAME would actually 4590169699Skan be incorrect, as we might be in the middle of an expression 4591169699Skan with side effects on the pointed-to type size "arguments" prior 4592169699Skan to the pointer declaration point and the fake TYPE_DECL in the 4593169699Skan enclosing context would force the size evaluation prior to the 4594169699Skan side effects. */ 459552284Sobrien 4596169699Skan if (!TYPE_NAME (type) 4597169699Skan && (decl_context == NORMAL || decl_context == FIELD) 4598169699Skan && variably_modified_type_p (type, NULL_TREE)) 4599169699Skan { 4600169699Skan tree decl = build_decl (TYPE_DECL, NULL_TREE, type); 4601169699Skan DECL_ARTIFICIAL (decl) = 1; 4602169699Skan pushdecl (decl); 4603169699Skan finish_decl (decl, NULL_TREE, NULL_TREE); 4604169699Skan TYPE_NAME (type) = decl; 4605169699Skan } 460652284Sobrien 4607169699Skan type = build_pointer_type (type); 460890075Sobrien 4609169699Skan /* Process type qualifiers (such as const or volatile) 4610169699Skan that were given inside the `*'. */ 4611169699Skan type_quals = declarator->u.pointer_quals; 461252284Sobrien 4613169699Skan declarator = declarator->declarator; 4614169699Skan break; 4615169699Skan } 4616169699Skan default: 4617169699Skan gcc_unreachable (); 461818334Speter } 461918334Speter } 462018334Speter 4621169699Skan /* Now TYPE has the actual type, apart from any qualifiers in 4622169699Skan TYPE_QUALS. */ 462318334Speter 4624146906Skan /* Check the type and width of a bit-field. */ 4625146906Skan if (bitfield) 4626146906Skan check_bitfield_type_and_width (&type, width, orig_name); 4627146906Skan 462850397Sobrien /* Did array size calculations overflow? */ 462950397Sobrien 463050397Sobrien if (TREE_CODE (type) == ARRAY_TYPE 463190075Sobrien && COMPLETE_TYPE_P (type) 4632169699Skan && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST 4633169699Skan && TREE_OVERFLOW (TYPE_SIZE_UNIT (type))) 463490075Sobrien { 4635169699Skan error ("size of array %qs is too large", name); 463690075Sobrien /* If we proceed with the array type as it is, we'll eventually 463790075Sobrien crash in tree_low_cst(). */ 463890075Sobrien type = error_mark_node; 463990075Sobrien } 464050397Sobrien 464118334Speter /* If this is declaring a typedef name, return a TYPE_DECL. */ 464218334Speter 4643169699Skan if (storage_class == csc_typedef) 464418334Speter { 464518334Speter tree decl; 464618334Speter if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 464752284Sobrien && type_quals) 464890075Sobrien pedwarn ("ISO C forbids qualified function types"); 464952284Sobrien if (type_quals) 465052284Sobrien type = c_build_qualified_type (type, type_quals); 4651169699Skan decl = build_decl (TYPE_DECL, declarator->u.id, type); 4652169699Skan if (declspecs->explicit_signed_p) 465318334Speter C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 465490075Sobrien decl_attributes (&decl, returned_attrs, 0); 4655169699Skan if (declspecs->inline_p) 4656169699Skan pedwarn ("typedef %q+D declared %<inline%>", decl); 465718334Speter return decl; 465818334Speter } 465918334Speter 466018334Speter /* If this is a type name (such as, in a cast or sizeof), 466118334Speter compute the type and return it now. */ 466218334Speter 466318334Speter if (decl_context == TYPENAME) 466418334Speter { 4665169699Skan /* Note that the grammar rejects storage classes in typenames 4666169699Skan and fields. */ 4667169699Skan gcc_assert (storage_class == csc_none && !threadp 4668169699Skan && !declspecs->inline_p); 466918334Speter if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 467052284Sobrien && type_quals) 467190075Sobrien pedwarn ("ISO C forbids const or volatile function types"); 467252284Sobrien if (type_quals) 467352284Sobrien type = c_build_qualified_type (type, type_quals); 467490075Sobrien decl_attributes (&type, returned_attrs, 0); 467518334Speter return type; 467618334Speter } 467718334Speter 4678169699Skan if (pedantic && decl_context == FIELD 4679169699Skan && variably_modified_type_p (type, NULL_TREE)) 4680169699Skan { 4681169699Skan /* C99 6.7.2.1p8 */ 4682169699Skan pedwarn ("a member of a structure or union cannot have a variably modified type"); 4683169699Skan } 4684169699Skan 468518334Speter /* Aside from typedefs and type names (handle above), 468618334Speter `void' at top level (not within pointer) 468718334Speter is allowed only in public variables. 468818334Speter We don't complain about parms either, but that is because 468918334Speter a better error message can be made later. */ 469018334Speter 469190075Sobrien if (VOID_TYPE_P (type) && decl_context != PARM 4692169699Skan && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE) 4693169699Skan && (storage_class == csc_extern 4694169699Skan || (current_scope == file_scope 4695169699Skan && !(storage_class == csc_static 4696169699Skan || storage_class == csc_register))))) 469718334Speter { 4698169699Skan error ("variable or field %qs declared void", name); 469918334Speter type = integer_type_node; 470018334Speter } 470118334Speter 470218334Speter /* Now create the decl, which may be a VAR_DECL, a PARM_DECL 470318334Speter or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ 470418334Speter 470518334Speter { 470690075Sobrien tree decl; 470718334Speter 470818334Speter if (decl_context == PARM) 470918334Speter { 471090075Sobrien tree type_as_written; 471190075Sobrien tree promoted_type; 471218334Speter 471318334Speter /* A parameter declared as an array of T is really a pointer to T. 471418334Speter One declared as a function is really a pointer to a function. */ 471518334Speter 471618334Speter if (TREE_CODE (type) == ARRAY_TYPE) 471718334Speter { 471818334Speter /* Transfer const-ness of array into that of type pointed to. */ 471918334Speter type = TREE_TYPE (type); 472052284Sobrien if (type_quals) 472152284Sobrien type = c_build_qualified_type (type, type_quals); 472218334Speter type = build_pointer_type (type); 4723169699Skan type_quals = array_ptr_quals; 472490075Sobrien 4725169699Skan /* We don't yet implement attributes in this context. */ 4726169699Skan if (array_ptr_attrs != NULL_TREE) 4727169699Skan warning (OPT_Wattributes, 4728169699Skan "attributes in parameter array declarator ignored"); 472990075Sobrien 473018334Speter size_varies = 0; 473118334Speter } 473218334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 473318334Speter { 473452284Sobrien if (pedantic && type_quals) 473590075Sobrien pedwarn ("ISO C forbids qualified function types"); 473652284Sobrien if (type_quals) 473752284Sobrien type = c_build_qualified_type (type, type_quals); 473818334Speter type = build_pointer_type (type); 473952284Sobrien type_quals = TYPE_UNQUALIFIED; 474018334Speter } 474190075Sobrien else if (type_quals) 474290075Sobrien type = c_build_qualified_type (type, type_quals); 4743132730Skan 474490075Sobrien type_as_written = type; 474518334Speter 4746169699Skan decl = build_decl (PARM_DECL, declarator->u.id, type); 474718334Speter if (size_varies) 474818334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 474918334Speter 475018334Speter /* Compute the type actually passed in the parmlist, 475118334Speter for the case where there is no prototype. 475218334Speter (For example, shorts and chars are passed as ints.) 475318334Speter When there is a prototype, this is overridden later. */ 475418334Speter 475590075Sobrien if (type == error_mark_node) 475690075Sobrien promoted_type = type; 475790075Sobrien else 4758117421Skan promoted_type = c_type_promotes_to (type); 475918334Speter 476090075Sobrien DECL_ARG_TYPE (decl) = promoted_type; 4761169699Skan if (declspecs->inline_p) 4762169699Skan pedwarn ("parameter %q+D declared %<inline%>", decl); 476318334Speter } 476418334Speter else if (decl_context == FIELD) 476518334Speter { 4766169699Skan /* Note that the grammar rejects storage classes in typenames 4767169699Skan and fields. */ 4768169699Skan gcc_assert (storage_class == csc_none && !threadp 4769169699Skan && !declspecs->inline_p); 4770169699Skan 477118334Speter /* Structure field. It may not be a function. */ 477296263Sobrien 477318334Speter if (TREE_CODE (type) == FUNCTION_TYPE) 477418334Speter { 4775169699Skan error ("field %qs declared as a function", name); 477618334Speter type = build_pointer_type (type); 477718334Speter } 477890075Sobrien else if (TREE_CODE (type) != ERROR_MARK 4779169699Skan && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type)) 478018334Speter { 4781169699Skan error ("field %qs has incomplete type", name); 478218334Speter type = error_mark_node; 478318334Speter } 4784169699Skan type = c_build_qualified_type (type, type_quals); 4785169699Skan decl = build_decl (FIELD_DECL, declarator->u.id, type); 478690075Sobrien DECL_NONADDRESSABLE_P (decl) = bitfield; 478790075Sobrien 478818334Speter if (size_varies) 478918334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 479018334Speter } 479118334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 479218334Speter { 4793169699Skan if (storage_class == csc_register || threadp) 4794169699Skan { 4795169699Skan error ("invalid storage class for function %qs", name); 4796169699Skan } 4797169699Skan else if (current_scope != file_scope) 4798169699Skan { 4799169699Skan /* Function declaration not at file scope. Storage 4800169699Skan classes other than `extern' are not allowed, C99 4801169699Skan 6.7.1p5, and `extern' makes no difference. However, 4802169699Skan GCC allows 'auto', perhaps with 'inline', to support 4803169699Skan nested functions. */ 4804169699Skan if (storage_class == csc_auto) 4805169699Skan { 4806169699Skan if (pedantic) 4807169699Skan pedwarn ("invalid storage class for function %qs", name); 4808169699Skan } 4809169699Skan else if (storage_class == csc_static) 4810169699Skan { 4811169699Skan error ("invalid storage class for function %qs", name); 4812169699Skan if (funcdef_flag) 4813169699Skan storage_class = declspecs->storage_class = csc_none; 4814169699Skan else 4815169699Skan return 0; 4816169699Skan } 4817169699Skan } 481818334Speter 4819169699Skan decl = build_decl (FUNCTION_DECL, declarator->u.id, type); 482090075Sobrien decl = build_decl_attribute_variant (decl, decl_attr); 482118334Speter 4822169699Skan DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl); 482390075Sobrien 4824169699Skan if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl)) 482590075Sobrien pedwarn ("ISO C forbids qualified function types"); 482618334Speter 4827169699Skan /* GNU C interprets a volatile-qualified function type to indicate 482852284Sobrien that the function does not return. */ 482952284Sobrien if ((type_quals & TYPE_QUAL_VOLATILE) 483090075Sobrien && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) 4831169699Skan warning (0, "%<noreturn%> function returns non-void value"); 483218334Speter 4833169699Skan /* Every function declaration is an external reference 4834169699Skan (DECL_EXTERNAL) except for those which are not at file 4835169699Skan scope and are explicitly declared "auto". This is 4836169699Skan forbidden by standard C (C99 6.7.1p5) and is interpreted by 4837169699Skan GCC to signify a forward declaration of a nested function. */ 4838169699Skan if (storage_class == csc_auto && current_scope != file_scope) 4839169699Skan DECL_EXTERNAL (decl) = 0; 4840189824Sdas /* In C99, a function which is declared 'inline' with 'extern' 4841189824Sdas is not an external reference (which is confusing). It 4842189824Sdas means that the later definition of the function must be output 4843189824Sdas in this file, C99 6.7.4p6. In GNU C89, a function declared 4844189824Sdas 'extern inline' is an external reference. */ 4845189824Sdas else if (declspecs->inline_p && storage_class != csc_static) 4846189824Sdas DECL_EXTERNAL (decl) = ((storage_class == csc_extern) 4847189824Sdas == flag_gnu89_inline); 4848169699Skan else 4849189824Sdas DECL_EXTERNAL (decl) = !initialized; 4850169699Skan 485118334Speter /* Record absence of global scope for `static' or `auto'. */ 485218334Speter TREE_PUBLIC (decl) 4853169699Skan = !(storage_class == csc_static || storage_class == csc_auto); 485418334Speter 4855169699Skan /* For a function definition, record the argument information 4856169699Skan block where store_parm_decls will look for it. */ 4857169699Skan if (funcdef_flag) 4858169699Skan current_function_arg_info = arg_info; 4859169699Skan 4860169699Skan if (declspecs->default_int_p) 486196263Sobrien C_FUNCTION_IMPLICIT_INT (decl) = 1; 486296263Sobrien 486318334Speter /* Record presence of `inline', if it is reasonable. */ 4864169699Skan if (flag_hosted && MAIN_NAME_P (declarator->u.id)) 486518334Speter { 4866169699Skan if (declspecs->inline_p) 4867169699Skan pedwarn ("cannot inline function %<main%>"); 486890075Sobrien } 4869169699Skan else if (declspecs->inline_p) 487090075Sobrien { 4871132730Skan /* Record that the function is declared `inline'. */ 487290075Sobrien DECL_DECLARED_INLINE_P (decl) = 1; 487318334Speter 487490075Sobrien /* Do not mark bare declarations as DECL_INLINE. Doing so 487590075Sobrien in the presence of multiple declarations can result in 487690075Sobrien the abstract origin pointing between the declarations, 487790075Sobrien which will confuse dwarf2out. */ 487890075Sobrien if (initialized) 4879189824Sdas DECL_INLINE (decl) = 1; 488018334Speter } 488190075Sobrien /* If -finline-functions, assume it can be inlined. This does 488290075Sobrien two things: let the function be deferred until it is actually 488390075Sobrien needed, and let dwarf2 know that the function is inlinable. */ 488490075Sobrien else if (flag_inline_trees == 2 && initialized) 4885132730Skan DECL_INLINE (decl) = 1; 488618334Speter } 488718334Speter else 488818334Speter { 488918334Speter /* It's a variable. */ 489018334Speter /* An uninitialized decl with `extern' is a reference. */ 4891169699Skan int extern_ref = !initialized && storage_class == csc_extern; 489218334Speter 4893169699Skan type = c_build_qualified_type (type, type_quals); 4894132730Skan 4895169699Skan /* C99 6.2.2p7: It is invalid (compile-time undefined 4896169699Skan behavior) to create an 'extern' declaration for a 4897132730Skan variable if there is a global declaration that is 4898169699Skan 'static' and the global declaration is not visible. 4899169699Skan (If the static declaration _is_ currently visible, 4900169699Skan the 'extern' declaration is taken to refer to that decl.) */ 4901169699Skan if (extern_ref && current_scope != file_scope) 4902132730Skan { 4903169699Skan tree global_decl = identifier_global_value (declarator->u.id); 4904169699Skan tree visible_decl = lookup_name (declarator->u.id); 4905132730Skan 4906132730Skan if (global_decl 4907169699Skan && global_decl != visible_decl 4908132730Skan && TREE_CODE (global_decl) == VAR_DECL 4909132730Skan && !TREE_PUBLIC (global_decl)) 4910169699Skan error ("variable previously declared %<static%> redeclared " 4911169699Skan "%<extern%>"); 4912132730Skan } 4913132730Skan 4914169699Skan decl = build_decl (VAR_DECL, declarator->u.id, type); 4915169699Skan DECL_SOURCE_LOCATION (decl) = declarator->id_loc; 491618334Speter if (size_varies) 491718334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 491818334Speter 4919169699Skan if (declspecs->inline_p) 4920169699Skan pedwarn ("variable %q+D declared %<inline%>", decl); 492118334Speter 4922169699Skan /* At file scope, an initialized extern declaration may follow 4923169699Skan a static declaration. In that case, DECL_EXTERNAL will be 4924169699Skan reset later in start_decl. */ 4925169699Skan DECL_EXTERNAL (decl) = (storage_class == csc_extern); 4926117421Skan 4927132730Skan /* At file scope, the presence of a `static' or `register' storage 492818334Speter class specifier, or the absence of all storage class specifiers 492918334Speter makes this declaration a definition (perhaps tentative). Also, 4930169699Skan the absence of `static' makes it public. */ 4931169699Skan if (current_scope == file_scope) 493218334Speter { 4933169699Skan TREE_PUBLIC (decl) = storage_class != csc_static; 4934117421Skan TREE_STATIC (decl) = !extern_ref; 493518334Speter } 4936132730Skan /* Not at file scope, only `static' makes a static definition. */ 493718334Speter else 493818334Speter { 4939169699Skan TREE_STATIC (decl) = (storage_class == csc_static); 4940117421Skan TREE_PUBLIC (decl) = extern_ref; 494118334Speter } 4942117421Skan 4943169699Skan if (threadp) 4944117421Skan { 4945117421Skan if (targetm.have_tls) 4946169699Skan DECL_TLS_MODEL (decl) = decl_default_tls_model (decl); 4947117421Skan else 4948117421Skan /* A mere warning is sure to result in improper semantics 4949117421Skan at runtime. Don't bother to allow this to compile. */ 4950117421Skan error ("thread-local storage not supported for this target"); 4951117421Skan } 495218334Speter } 495318334Speter 4954169699Skan if (storage_class == csc_extern 4955169699Skan && variably_modified_type_p (type, NULL_TREE)) 4956169699Skan { 4957169699Skan /* C99 6.7.5.2p2 */ 4958169699Skan error ("object with variably modified type must have no linkage"); 4959169699Skan } 4960169699Skan 496118334Speter /* Record `register' declaration for warnings on & 496218334Speter and in case doing stupid register allocation. */ 496318334Speter 4964169699Skan if (storage_class == csc_register) 4965169699Skan { 4966169699Skan C_DECL_REGISTER (decl) = 1; 4967169699Skan DECL_REGISTER (decl) = 1; 4968169699Skan } 496918334Speter 497018334Speter /* Record constancy and volatility. */ 497152284Sobrien c_apply_type_quals_to_decl (type_quals, decl); 497218334Speter 497318334Speter /* If a type has volatile components, it should be stored in memory. 497418334Speter Otherwise, the fact that those components are volatile 4975169699Skan will be ignored, and would even crash the compiler. 4976169699Skan Of course, this only makes sense on VAR,PARM, and RESULT decl's. */ 4977169699Skan if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)) 4978169699Skan && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL 4979169699Skan || TREE_CODE (decl) == RESULT_DECL)) 4980169699Skan { 4981169699Skan /* It is not an error for a structure with volatile fields to 4982169699Skan be declared register, but reset DECL_REGISTER since it 4983169699Skan cannot actually go in a register. */ 4984169699Skan int was_reg = C_DECL_REGISTER (decl); 4985169699Skan C_DECL_REGISTER (decl) = 0; 4986169699Skan DECL_REGISTER (decl) = 0; 4987169699Skan c_mark_addressable (decl); 4988169699Skan C_DECL_REGISTER (decl) = was_reg; 4989169699Skan } 499018334Speter 4991132730Skan /* This is the earliest point at which we might know the assembler 4992132730Skan name of a variable. Thus, if it's known before this, die horribly. */ 4993169699Skan gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)); 4994132730Skan 499590075Sobrien decl_attributes (&decl, returned_attrs, 0); 499618334Speter 499718334Speter return decl; 499818334Speter } 499918334Speter} 500018334Speter 500118334Speter/* Decode the parameter-list info for a function type or function definition. 5002169699Skan The argument is the value returned by `get_parm_info' (or made in c-parse.c 500318334Speter if there is an identifier list instead of a parameter decl list). 500418334Speter These two functions are separate because when a function returns 500518334Speter or receives functions then each is called multiple times but the order 500618334Speter of calls is different. The last call to `grokparms' is always the one 500718334Speter that contains the formal parameter names of a function definition. 500818334Speter 500918334Speter Return a list of arg types to use in the FUNCTION_TYPE for this function. 501018334Speter 5011169699Skan FUNCDEF_FLAG is true for a function definition, false for 501218334Speter a mere declaration. A nonempty identifier-list gets an error message 5013169699Skan when FUNCDEF_FLAG is false. */ 501418334Speter 501518334Speterstatic tree 5016169699Skangrokparms (struct c_arg_info *arg_info, bool funcdef_flag) 501718334Speter{ 5018169699Skan tree arg_types = arg_info->types; 501918334Speter 5020169699Skan if (funcdef_flag && arg_info->had_vla_unspec) 5021169699Skan { 5022169699Skan /* A function definition isn't function prototype scope C99 6.2.1p4. */ 5023169699Skan /* C99 6.7.5.2p4 */ 5024169699Skan error ("%<[*]%> not allowed in other than function prototype scope"); 5025169699Skan } 502618334Speter 5027169699Skan if (arg_types == 0 && !funcdef_flag && !in_system_header) 5028169699Skan warning (OPT_Wstrict_prototypes, 5029169699Skan "function declaration isn%'t a prototype"); 503018334Speter 5031169699Skan if (arg_types == error_mark_node) 5032169699Skan return 0; /* don't set TYPE_ARG_TYPES in this case */ 5033169699Skan 5034169699Skan else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE) 503518334Speter { 5036169699Skan if (!funcdef_flag) 503718334Speter pedwarn ("parameter names (without types) in function declaration"); 503818334Speter 5039169699Skan arg_info->parms = arg_info->types; 5040169699Skan arg_info->types = 0; 504118334Speter return 0; 504218334Speter } 504318334Speter else 504418334Speter { 5045169699Skan tree parm, type, typelt; 5046169699Skan unsigned int parmno; 504718334Speter 5048169699Skan /* If there is a parameter of incomplete type in a definition, 5049169699Skan this is an error. In a declaration this is valid, and a 5050169699Skan struct or union type may be completed later, before any calls 5051169699Skan or definition of the function. In the case where the tag was 5052169699Skan first declared within the parameter list, a warning has 5053169699Skan already been given. If a parameter has void type, then 5054169699Skan however the function cannot be defined or called, so 5055169699Skan warn. */ 5056169699Skan 5057169699Skan for (parm = arg_info->parms, typelt = arg_types, parmno = 1; 5058132730Skan parm; 5059169699Skan parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++) 5060169699Skan { 5061169699Skan type = TREE_VALUE (typelt); 5062169699Skan if (type == error_mark_node) 5063169699Skan continue; 5064132730Skan 5065169699Skan if (!COMPLETE_TYPE_P (type)) 5066169699Skan { 5067169699Skan if (funcdef_flag) 5068169699Skan { 5069169699Skan if (DECL_NAME (parm)) 5070169699Skan error ("parameter %u (%q+D) has incomplete type", 5071169699Skan parmno, parm); 5072169699Skan else 5073169699Skan error ("%Jparameter %u has incomplete type", 5074169699Skan parm, parmno); 5075169699Skan 5076169699Skan TREE_VALUE (typelt) = error_mark_node; 5077169699Skan TREE_TYPE (parm) = error_mark_node; 5078169699Skan } 5079169699Skan else if (VOID_TYPE_P (type)) 5080169699Skan { 5081169699Skan if (DECL_NAME (parm)) 5082169699Skan warning (0, "parameter %u (%q+D) has void type", 5083169699Skan parmno, parm); 5084169699Skan else 5085169699Skan warning (0, "%Jparameter %u has void type", 5086169699Skan parm, parmno); 5087169699Skan } 5088169699Skan } 5089169699Skan 5090169699Skan if (DECL_NAME (parm) && TREE_USED (parm)) 5091169699Skan warn_if_shadowing (parm); 5092169699Skan } 5093169699Skan return arg_types; 509418334Speter } 509518334Speter} 509618334Speter 5097169699Skan/* Take apart the current scope and return a c_arg_info structure with 5098169699Skan info on a parameter list just parsed. 509918334Speter 5100169699Skan This structure is later fed to 'grokparms' and 'store_parm_decls'. 510118334Speter 5102169699Skan ELLIPSIS being true means the argument list ended in '...' so don't 5103169699Skan append a sentinel (void_list_node) to the end of the type-list. */ 5104169699Skan 5105169699Skanstruct c_arg_info * 5106169699Skanget_parm_info (bool ellipsis) 510718334Speter{ 5108169699Skan struct c_binding *b = current_scope->bindings; 5109169699Skan struct c_arg_info *arg_info = XOBNEW (&parser_obstack, 5110169699Skan struct c_arg_info); 5111169699Skan tree parms = 0; 5112169699Skan tree tags = 0; 5113169699Skan tree types = 0; 5114169699Skan tree others = 0; 5115169699Skan 5116132730Skan static bool explained_incomplete_types = false; 5117132730Skan bool gave_void_only_once_err = false; 511818334Speter 5119169699Skan arg_info->parms = 0; 5120169699Skan arg_info->tags = 0; 5121169699Skan arg_info->types = 0; 5122169699Skan arg_info->others = 0; 5123169699Skan arg_info->pending_sizes = 0; 5124169699Skan arg_info->had_vla_unspec = current_scope->had_vla_unspec; 5125132730Skan 5126169699Skan /* The bindings in this scope must not get put into a block. 5127169699Skan We will take care of deleting the binding nodes. */ 5128169699Skan current_scope->bindings = 0; 512918334Speter 5130169699Skan /* This function is only called if there was *something* on the 5131169699Skan parameter list. */ 5132169699Skan gcc_assert (b); 5133169699Skan 5134169699Skan /* A parameter list consisting solely of 'void' indicates that the 5135169699Skan function takes no arguments. But if the 'void' is qualified 5136169699Skan (by 'const' or 'volatile'), or has a storage class specifier 5137169699Skan ('register'), then the behavior is undefined; issue an error. 5138169699Skan Typedefs for 'void' are OK (see DR#157). */ 5139169699Skan if (b->prev == 0 /* one binding */ 5140169699Skan && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */ 5141169699Skan && !DECL_NAME (b->decl) /* anonymous */ 5142169699Skan && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */ 514318334Speter { 5144169699Skan if (TREE_THIS_VOLATILE (b->decl) 5145169699Skan || TREE_READONLY (b->decl) 5146169699Skan || C_DECL_REGISTER (b->decl)) 5147169699Skan error ("%<void%> as only parameter may not be qualified"); 514818334Speter 5149169699Skan /* There cannot be an ellipsis. */ 5150169699Skan if (ellipsis) 5151169699Skan error ("%<void%> must be the only parameter"); 5152132730Skan 5153169699Skan arg_info->types = void_list_node; 5154169699Skan return arg_info; 515518334Speter } 515618334Speter 5157169699Skan if (!ellipsis) 5158169699Skan types = void_list_node; 515918334Speter 5160169699Skan /* Break up the bindings list into parms, tags, types, and others; 5161169699Skan apply sanity checks; purge the name-to-decl bindings. */ 5162169699Skan while (b) 516318334Speter { 5164169699Skan tree decl = b->decl; 5165169699Skan tree type = TREE_TYPE (decl); 5166132730Skan const char *keyword; 5167132730Skan 5168169699Skan switch (TREE_CODE (decl)) 516990075Sobrien { 5170169699Skan case PARM_DECL: 5171169699Skan if (b->id) 5172169699Skan { 5173169699Skan gcc_assert (I_SYMBOL_BINDING (b->id) == b); 5174169699Skan I_SYMBOL_BINDING (b->id) = b->shadowed; 5175169699Skan } 5176132730Skan 5177169699Skan /* Check for forward decls that never got their actual decl. */ 5178169699Skan if (TREE_ASM_WRITTEN (decl)) 5179169699Skan error ("parameter %q+D has just a forward declaration", decl); 5180169699Skan /* Check for (..., void, ...) and issue an error. */ 5181169699Skan else if (VOID_TYPE_P (type) && !DECL_NAME (decl)) 5182169699Skan { 5183169699Skan if (!gave_void_only_once_err) 5184169699Skan { 5185169699Skan error ("%<void%> must be the only parameter"); 5186169699Skan gave_void_only_once_err = true; 5187169699Skan } 5188169699Skan } 5189169699Skan else 5190169699Skan { 5191169699Skan /* Valid parameter, add it to the list. */ 5192169699Skan TREE_CHAIN (decl) = parms; 5193169699Skan parms = decl; 5194132730Skan 5195169699Skan /* Since there is a prototype, args are passed in their 5196169699Skan declared types. The back end may override this later. */ 5197169699Skan DECL_ARG_TYPE (decl) = type; 5198169699Skan types = tree_cons (0, type, types); 5199169699Skan } 5200169699Skan break; 5201169699Skan 5202169699Skan case ENUMERAL_TYPE: keyword = "enum"; goto tag; 5203169699Skan case UNION_TYPE: keyword = "union"; goto tag; 5204169699Skan case RECORD_TYPE: keyword = "struct"; goto tag; 5205169699Skan tag: 5206169699Skan /* Types may not have tag-names, in which case the type 5207169699Skan appears in the bindings list with b->id NULL. */ 5208169699Skan if (b->id) 5209169699Skan { 5210169699Skan gcc_assert (I_TAG_BINDING (b->id) == b); 5211169699Skan I_TAG_BINDING (b->id) = b->shadowed; 5212169699Skan } 5213169699Skan 5214169699Skan /* Warn about any struct, union or enum tags defined in a 5215169699Skan parameter list. The scope of such types is limited to 5216169699Skan the parameter list, which is rarely if ever desirable 5217169699Skan (it's impossible to call such a function with type- 5218169699Skan correct arguments). An anonymous union parm type is 5219169699Skan meaningful as a GNU extension, so don't warn for that. */ 5220169699Skan if (TREE_CODE (decl) != UNION_TYPE || b->id != 0) 5221169699Skan { 5222169699Skan if (b->id) 5223169699Skan /* The %s will be one of 'struct', 'union', or 'enum'. */ 5224169699Skan warning (0, "%<%s %E%> declared inside parameter list", 5225169699Skan keyword, b->id); 5226169699Skan else 5227169699Skan /* The %s will be one of 'struct', 'union', or 'enum'. */ 5228169699Skan warning (0, "anonymous %s declared inside parameter list", 5229169699Skan keyword); 5230169699Skan 5231169699Skan if (!explained_incomplete_types) 5232169699Skan { 5233169699Skan warning (0, "its scope is only this definition or declaration," 5234169699Skan " which is probably not what you want"); 5235169699Skan explained_incomplete_types = true; 5236169699Skan } 5237169699Skan } 5238169699Skan 5239169699Skan tags = tree_cons (b->id, decl, tags); 5240169699Skan break; 5241169699Skan 5242169699Skan case CONST_DECL: 5243169699Skan case TYPE_DECL: 5244169699Skan case FUNCTION_DECL: 5245169699Skan /* CONST_DECLs appear here when we have an embedded enum, 5246169699Skan and TYPE_DECLs appear here when we have an embedded struct 5247169699Skan or union. No warnings for this - we already warned about the 5248169699Skan type itself. FUNCTION_DECLs appear when there is an implicit 5249169699Skan function declaration in the parameter list. */ 5250169699Skan 5251169699Skan TREE_CHAIN (decl) = others; 5252169699Skan others = decl; 5253169699Skan /* fall through */ 5254169699Skan 5255169699Skan case ERROR_MARK: 5256169699Skan /* error_mark_node appears here when we have an undeclared 5257169699Skan variable. Just throw it away. */ 5258169699Skan if (b->id) 5259169699Skan { 5260169699Skan gcc_assert (I_SYMBOL_BINDING (b->id) == b); 5261169699Skan I_SYMBOL_BINDING (b->id) = b->shadowed; 5262169699Skan } 5263169699Skan break; 5264169699Skan 5265169699Skan /* Other things that might be encountered. */ 5266169699Skan case LABEL_DECL: 5267169699Skan case VAR_DECL: 5268169699Skan default: 5269169699Skan gcc_unreachable (); 527018334Speter } 5271132730Skan 5272169699Skan b = free_binding_and_advance (b); 5273132730Skan } 5274132730Skan 5275169699Skan arg_info->parms = parms; 5276169699Skan arg_info->tags = tags; 5277169699Skan arg_info->types = types; 5278169699Skan arg_info->others = others; 5279169699Skan arg_info->pending_sizes = get_pending_sizes (); 5280169699Skan return arg_info; 528118334Speter} 528218334Speter 528318334Speter/* Get the struct, enum or union (CODE says which) with tag NAME. 5284169699Skan Define the tag as a forward-reference if it is not defined. 5285169699Skan Return a c_typespec structure for the type specifier. */ 528618334Speter 5287169699Skanstruct c_typespec 5288169699Skanparser_xref_tag (enum tree_code code, tree name) 528918334Speter{ 5290169699Skan struct c_typespec ret; 529118334Speter /* If a cross reference is requested, look up the type 529218334Speter already defined for this tag and return it. */ 529318334Speter 5294132730Skan tree ref = lookup_tag (code, name, 0); 529590075Sobrien /* If this is the right type of tag, return what we found. 529690075Sobrien (This reference will be shadowed by shadow_tag later if appropriate.) 529790075Sobrien If this is the wrong type of tag, do not return it. If it was the 5298132730Skan wrong type in the same scope, we will have had an error 5299132730Skan message already; if in a different scope and declaring 530090075Sobrien a name, pending_xref_error will give an error message; but if in a 5301132730Skan different scope and not declaring a name, this tag should 530290075Sobrien shadow the previous declaration of a different type of tag, and 530390075Sobrien this would not work properly if we return the reference found. 530490075Sobrien (For example, with "struct foo" in an outer scope, "union foo;" 530590075Sobrien must shadow that tag with a new one of union type.) */ 5306169699Skan ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref); 530790075Sobrien if (ref && TREE_CODE (ref) == code) 5308169699Skan { 5309169699Skan ret.spec = ref; 5310169699Skan return ret; 5311169699Skan } 531218334Speter 531318334Speter /* If no such tag is yet defined, create a forward-reference node 531418334Speter and record it as the "definition". 531518334Speter When a real declaration of this type is found, 531618334Speter the forward-reference will be altered into a real type. */ 531718334Speter 531818334Speter ref = make_node (code); 531918334Speter if (code == ENUMERAL_TYPE) 532018334Speter { 532118334Speter /* Give the type a default layout like unsigned int 532218334Speter to avoid crashing if it does not get defined. */ 532318334Speter TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node); 532418334Speter TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); 532590075Sobrien TYPE_USER_ALIGN (ref) = 0; 5326169699Skan TYPE_UNSIGNED (ref) = 1; 532718334Speter TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); 532818334Speter TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); 532918334Speter TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); 533018334Speter } 533118334Speter 533218334Speter pushtag (name, ref); 533318334Speter 5334169699Skan ret.spec = ref; 5335169699Skan return ret; 533618334Speter} 5337169699Skan 5338169699Skan/* Get the struct, enum or union (CODE says which) with tag NAME. 5339169699Skan Define the tag as a forward-reference if it is not defined. 5340169699Skan Return a tree for the type. */ 5341169699Skan 5342169699Skantree 5343169699Skanxref_tag (enum tree_code code, tree name) 5344169699Skan{ 5345169699Skan return parser_xref_tag (code, name).spec; 5346169699Skan} 534718334Speter 5348132730Skan/* Make sure that the tag NAME is defined *in the current scope* 534918334Speter at least as a forward reference. 535090075Sobrien CODE says which kind of tag NAME ought to be. */ 535118334Speter 535218334Spetertree 5353132730Skanstart_struct (enum tree_code code, tree name) 535418334Speter{ 5355132730Skan /* If there is already a tag defined at this scope 535618334Speter (as a forward reference), just return it. */ 535718334Speter 535890075Sobrien tree ref = 0; 535918334Speter 536018334Speter if (name != 0) 5361132730Skan ref = lookup_tag (code, name, 1); 536218334Speter if (ref && TREE_CODE (ref) == code) 536318334Speter { 5364161660Skan if (TYPE_SIZE (ref)) 5365169699Skan { 536690075Sobrien if (code == UNION_TYPE) 5367169699Skan error ("redefinition of %<union %E%>", name); 5368169699Skan else 5369169699Skan error ("redefinition of %<struct %E%>", name); 5370132730Skan } 5371161660Skan else if (C_TYPE_BEING_DEFINED (ref)) 5372161660Skan { 5373161660Skan if (code == UNION_TYPE) 5374169699Skan error ("nested redefinition of %<union %E%>", name); 5375169699Skan else 5376169699Skan error ("nested redefinition of %<struct %E%>", name); 5377189824Sdas /* Don't create structures that contain themselves. */ 5378189824Sdas ref = NULL_TREE; 5379161660Skan } 5380117421Skan } 5381189824Sdas 5382189824Sdas /* Otherwise create a forward-reference just so the tag is in scope. */ 5383189824Sdas 5384189824Sdas if (ref == NULL_TREE || TREE_CODE (ref) != code) 5385117421Skan { 5386117421Skan ref = make_node (code); 5387117421Skan pushtag (name, ref); 538818334Speter } 5389132730Skan 539018334Speter C_TYPE_BEING_DEFINED (ref) = 1; 539150397Sobrien TYPE_PACKED (ref) = flag_pack_struct; 539218334Speter return ref; 539318334Speter} 539418334Speter 5395169699Skan/* Process the specs, declarator and width (NULL if omitted) 539618334Speter of a structure component, returning a FIELD_DECL node. 5397132730Skan WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node. 539818334Speter 539918334Speter This is done during the parsing of the struct declaration. 540018334Speter The FIELD_DECL nodes are chained together and the lot of them 540118334Speter are ultimately passed to `build_struct' to make the RECORD_TYPE node. */ 540218334Speter 540318334Spetertree 5404169699Skangrokfield (struct c_declarator *declarator, struct c_declspecs *declspecs, 5405169699Skan tree width) 540618334Speter{ 540718334Speter tree value; 540818334Speter 5409169699Skan if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE 5410169699Skan && width == NULL_TREE) 541190075Sobrien { 5412117421Skan /* This is an unnamed decl. 5413117421Skan 5414117421Skan If we have something of the form "union { list } ;" then this 5415117421Skan is the anonymous union extension. Similarly for struct. 5416117421Skan 5417117421Skan If this is something of the form "struct foo;", then 5418117421Skan If MS extensions are enabled, this is handled as an 5419117421Skan anonymous struct. 5420117421Skan Otherwise this is a forward declaration of a structure tag. 5421117421Skan 5422117421Skan If this is something of the form "foo;" and foo is a TYPE_DECL, then 5423117421Skan If MS extensions are enabled and foo names a structure, then 5424117421Skan again this is an anonymous struct. 5425117421Skan Otherwise this is an error. 5426117421Skan 5427132730Skan Oh what a horrid tangled web we weave. I wonder if MS consciously 5428117421Skan took this from Plan 9 or if it was an accident of implementation 5429117421Skan that took root before someone noticed the bug... */ 5430117421Skan 5431169699Skan tree type = declspecs->type; 5432169699Skan bool type_ok = (TREE_CODE (type) == RECORD_TYPE 5433169699Skan || TREE_CODE (type) == UNION_TYPE); 5434169699Skan bool ok = false; 5435102790Skan 5436169699Skan if (type_ok 5437169699Skan && (flag_ms_extensions || !declspecs->typedef_p)) 543890075Sobrien { 5439117421Skan if (flag_ms_extensions) 5440169699Skan ok = true; 5441117421Skan else if (flag_iso) 5442169699Skan ok = false; 5443117421Skan else if (TYPE_NAME (type) == NULL) 5444169699Skan ok = true; 5445117421Skan else 5446169699Skan ok = false; 5447117421Skan } 5448169699Skan if (!ok) 5449117421Skan { 5450169699Skan pedwarn ("declaration does not declare anything"); 545190075Sobrien return NULL_TREE; 545290075Sobrien } 5453169699Skan if (pedantic) 5454169699Skan pedwarn ("ISO C doesn%'t support unnamed structs/unions"); 545590075Sobrien } 545618334Speter 5457169699Skan value = grokdeclarator (declarator, declspecs, FIELD, false, 5458132730Skan width ? &width : NULL); 545918334Speter 546018334Speter finish_decl (value, NULL_TREE, NULL_TREE); 546196263Sobrien DECL_INITIAL (value) = width; 546218334Speter 546318334Speter return value; 546418334Speter} 546518334Speter 5466132730Skan/* Generate an error for any duplicate field names in FIELDLIST. Munge 5467132730Skan the list such that this does not present a problem later. */ 5468122196Skan 5469132730Skanstatic void 5470132730Skandetect_field_duplicates (tree fieldlist) 5471132730Skan{ 5472132730Skan tree x, y; 5473132730Skan int timeout = 10; 5474122196Skan 5475132730Skan /* First, see if there are more than "a few" fields. 5476132730Skan This is trivially true if there are zero or one fields. */ 5477132730Skan if (!fieldlist) 5478132730Skan return; 5479132730Skan x = TREE_CHAIN (fieldlist); 5480132730Skan if (!x) 5481132730Skan return; 5482132730Skan do { 5483132730Skan timeout--; 5484132730Skan x = TREE_CHAIN (x); 5485132730Skan } while (timeout > 0 && x); 5486132730Skan 5487132730Skan /* If there were "few" fields, avoid the overhead of allocating 5488132730Skan a hash table. Instead just do the nested traversal thing. */ 5489132730Skan if (timeout > 0) 5490132730Skan { 5491132730Skan for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x)) 5492132730Skan if (DECL_NAME (x)) 5493132730Skan { 5494132730Skan for (y = fieldlist; y != x; y = TREE_CHAIN (y)) 5495132730Skan if (DECL_NAME (y) == DECL_NAME (x)) 5496132730Skan { 5497169699Skan error ("duplicate member %q+D", x); 5498132730Skan DECL_NAME (x) = NULL_TREE; 5499132730Skan } 5500132730Skan } 5501132730Skan } 5502132730Skan else 5503132730Skan { 5504132730Skan htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); 5505132730Skan void **slot; 5506132730Skan 5507132730Skan for (x = fieldlist; x ; x = TREE_CHAIN (x)) 5508132730Skan if ((y = DECL_NAME (x)) != 0) 5509132730Skan { 5510132730Skan slot = htab_find_slot (htab, y, INSERT); 5511132730Skan if (*slot) 5512132730Skan { 5513169699Skan error ("duplicate member %q+D", x); 5514132730Skan DECL_NAME (x) = NULL_TREE; 5515132730Skan } 5516132730Skan *slot = y; 5517132730Skan } 5518132730Skan 5519132730Skan htab_delete (htab); 5520132730Skan } 5521122196Skan} 5522132730Skan 552318334Speter/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. 552418334Speter FIELDLIST is a chain of FIELD_DECL nodes for the fields. 552590075Sobrien ATTRIBUTES are attributes to be applied to the structure. */ 552618334Speter 552718334Spetertree 5528132730Skanfinish_struct (tree t, tree fieldlist, tree attributes) 552918334Speter{ 553090075Sobrien tree x; 5531169699Skan bool toplevel = file_scope == current_scope; 553290075Sobrien int saw_named_field; 553318334Speter 553418334Speter /* If this type was previously laid out as a forward reference, 553518334Speter make sure we lay it out again. */ 553618334Speter 553718334Speter TYPE_SIZE (t) = 0; 553818334Speter 553990075Sobrien decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 554018334Speter 554150397Sobrien if (pedantic) 554250397Sobrien { 554350397Sobrien for (x = fieldlist; x; x = TREE_CHAIN (x)) 554450397Sobrien if (DECL_NAME (x) != 0) 554550397Sobrien break; 554618334Speter 554750397Sobrien if (x == 0) 5548169699Skan { 5549169699Skan if (TREE_CODE (t) == UNION_TYPE) 5550169699Skan { 5551169699Skan if (fieldlist) 5552169699Skan pedwarn ("union has no named members"); 5553169699Skan else 5554169699Skan pedwarn ("union has no members"); 5555169699Skan } 5556169699Skan else 5557169699Skan { 5558169699Skan if (fieldlist) 5559169699Skan pedwarn ("struct has no named members"); 5560169699Skan else 5561169699Skan pedwarn ("struct has no members"); 5562169699Skan } 5563169699Skan } 556450397Sobrien } 556550397Sobrien 556696263Sobrien /* Install struct as DECL_CONTEXT of each field decl. 5567169699Skan Also process specified field sizes, found in the DECL_INITIAL, 5568169699Skan storing 0 there after the type has been changed to precision equal 5569169699Skan to its width, rather than the precision of the specified standard 5570169699Skan type. (Correct layout requires the original type to have been preserved 5571169699Skan until now.) */ 557218334Speter 557390075Sobrien saw_named_field = 0; 557418334Speter for (x = fieldlist; x; x = TREE_CHAIN (x)) 557518334Speter { 5576169699Skan if (TREE_TYPE (x) == error_mark_node) 5577169699Skan continue; 5578169699Skan 557918334Speter DECL_CONTEXT (x) = t; 558018334Speter 5581169699Skan if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT) 5582169699Skan DECL_PACKED (x) = 1; 5583169699Skan 558418334Speter /* If any field is const, the structure type is pseudo-const. */ 558518334Speter if (TREE_READONLY (x)) 558618334Speter C_TYPE_FIELDS_READONLY (t) = 1; 558718334Speter else 558818334Speter { 558918334Speter /* A field that is pseudo-const makes the structure likewise. */ 559018334Speter tree t1 = TREE_TYPE (x); 559118334Speter while (TREE_CODE (t1) == ARRAY_TYPE) 559218334Speter t1 = TREE_TYPE (t1); 559318334Speter if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE) 559418334Speter && C_TYPE_FIELDS_READONLY (t1)) 559518334Speter C_TYPE_FIELDS_READONLY (t) = 1; 559618334Speter } 559718334Speter 559818334Speter /* Any field that is volatile means variables of this type must be 559918334Speter treated in some ways as volatile. */ 560018334Speter if (TREE_THIS_VOLATILE (x)) 560118334Speter C_TYPE_FIELDS_VOLATILE (t) = 1; 560218334Speter 560318334Speter /* Any field of nominal variable size implies structure is too. */ 560418334Speter if (C_DECL_VARIABLE_SIZE (x)) 560518334Speter C_TYPE_VARIABLE_SIZE (t) = 1; 560618334Speter 560796263Sobrien if (DECL_INITIAL (x)) 560818334Speter { 5609132730Skan unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1); 5610132730Skan DECL_SIZE (x) = bitsize_int (width); 5611132730Skan DECL_BIT_FIELD (x) = 1; 5612132730Skan SET_DECL_C_BIT_FIELD (x); 561396263Sobrien } 561496263Sobrien 561590075Sobrien /* Detect flexible array member in an invalid context. */ 561690075Sobrien if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 561790075Sobrien && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 561890075Sobrien && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 561990075Sobrien && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 562018334Speter { 562190075Sobrien if (TREE_CODE (t) == UNION_TYPE) 5622132730Skan { 5623132730Skan error ("%Jflexible array member in union", x); 5624132730Skan TREE_TYPE (x) = error_mark_node; 5625132730Skan } 562690075Sobrien else if (TREE_CHAIN (x) != NULL_TREE) 5627132730Skan { 5628132730Skan error ("%Jflexible array member not at end of struct", x); 5629132730Skan TREE_TYPE (x) = error_mark_node; 5630132730Skan } 5631169699Skan else if (!saw_named_field) 5632132730Skan { 5633132730Skan error ("%Jflexible array member in otherwise empty struct", x); 5634132730Skan TREE_TYPE (x) = error_mark_node; 5635132730Skan } 563618334Speter } 5637117421Skan 5638132730Skan if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE 5639117421Skan && flexible_array_type_p (TREE_TYPE (x))) 5640132730Skan pedwarn ("%Jinvalid use of structure with flexible array member", x); 5641117421Skan 564290075Sobrien if (DECL_NAME (x)) 564390075Sobrien saw_named_field = 1; 564418334Speter } 564518334Speter 5646132730Skan detect_field_duplicates (fieldlist); 564790075Sobrien 564818334Speter /* Now we have the nearly final fieldlist. Record it, 564918334Speter then lay out the structure or union (including the fields). */ 565018334Speter 565118334Speter TYPE_FIELDS (t) = fieldlist; 565218334Speter 565318334Speter layout_type (t); 565418334Speter 5655169699Skan /* Give bit-fields their proper types. */ 565690075Sobrien { 565790075Sobrien tree *fieldlistp = &fieldlist; 565890075Sobrien while (*fieldlistp) 5659169699Skan if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp) 5660169699Skan && TREE_TYPE (*fieldlistp) != error_mark_node) 5661169699Skan { 5662169699Skan unsigned HOST_WIDE_INT width 5663169699Skan = tree_low_cst (DECL_INITIAL (*fieldlistp), 1); 5664169699Skan tree type = TREE_TYPE (*fieldlistp); 5665169699Skan if (width != TYPE_PRECISION (type)) 5666169699Skan { 5667169699Skan TREE_TYPE (*fieldlistp) 5668169699Skan = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type)); 5669169699Skan DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp)); 5670169699Skan } 5671169699Skan DECL_INITIAL (*fieldlistp) = 0; 5672169699Skan } 567390075Sobrien else 567490075Sobrien fieldlistp = &TREE_CHAIN (*fieldlistp); 567590075Sobrien } 567618334Speter 567790075Sobrien /* Now we have the truly final field list. 567890075Sobrien Store it in this type and in the variants. */ 567918334Speter 568018334Speter TYPE_FIELDS (t) = fieldlist; 568118334Speter 5682122196Skan /* If there are lots of fields, sort so we can look through them fast. 5683132730Skan We arbitrarily consider 16 or more elts to be "a lot". */ 5684122196Skan 5685122196Skan { 5686122196Skan int len = 0; 5687122196Skan 5688122196Skan for (x = fieldlist; x; x = TREE_CHAIN (x)) 5689122196Skan { 5690169699Skan if (len > 15 || DECL_NAME (x) == NULL) 5691169699Skan break; 5692169699Skan len += 1; 5693122196Skan } 5694122196Skan 5695122196Skan if (len > 15) 5696122196Skan { 5697169699Skan tree *field_array; 5698169699Skan struct lang_type *space; 5699169699Skan struct sorted_fields_type *space2; 5700132730Skan 5701169699Skan len += list_length (x); 5702132730Skan 5703169699Skan /* Use the same allocation policy here that make_node uses, to 5704169699Skan ensure that this lives as long as the rest of the struct decl. 5705169699Skan All decls in an inline function need to be saved. */ 5706132730Skan 5707169699Skan space = GGC_CNEW (struct lang_type); 5708169699Skan space2 = GGC_NEWVAR (struct sorted_fields_type, 5709169699Skan sizeof (struct sorted_fields_type) + len * sizeof (tree)); 5710132730Skan 5711169699Skan len = 0; 5712132730Skan space->s = space2; 5713132730Skan field_array = &space2->elts[0]; 5714169699Skan for (x = fieldlist; x; x = TREE_CHAIN (x)) 5715169699Skan { 5716169699Skan field_array[len++] = x; 5717132730Skan 5718169699Skan /* If there is anonymous struct or union, break out of the loop. */ 5719169699Skan if (DECL_NAME (x) == NULL) 5720169699Skan break; 5721169699Skan } 5722169699Skan /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */ 5723169699Skan if (x == NULL) 5724169699Skan { 5725169699Skan TYPE_LANG_SPECIFIC (t) = space; 5726169699Skan TYPE_LANG_SPECIFIC (t)->s->len = len; 5727169699Skan field_array = TYPE_LANG_SPECIFIC (t)->s->elts; 5728169699Skan qsort (field_array, len, sizeof (tree), field_decl_cmp); 5729169699Skan } 5730122196Skan } 5731122196Skan } 5732132730Skan 573318334Speter for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) 573418334Speter { 573518334Speter TYPE_FIELDS (x) = TYPE_FIELDS (t); 573618334Speter TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t); 5737161660Skan C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t); 5738161660Skan C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t); 5739161660Skan C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t); 574018334Speter } 574118334Speter 574218334Speter /* If this was supposed to be a transparent union, but we can't 574318334Speter make it one, warn and turn off the flag. */ 574418334Speter if (TREE_CODE (t) == UNION_TYPE 574518334Speter && TYPE_TRANSPARENT_UNION (t) 5746146906Skan && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))) 574718334Speter { 574818334Speter TYPE_TRANSPARENT_UNION (t) = 0; 5749169699Skan warning (0, "union cannot be made transparent"); 575018334Speter } 575118334Speter 575218334Speter /* If this structure or union completes the type of any previous 575318334Speter variable declaration, lay it out and output its rtl. */ 5754132730Skan for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)); 5755132730Skan x; 5756132730Skan x = TREE_CHAIN (x)) 575718334Speter { 5758132730Skan tree decl = TREE_VALUE (x); 5759132730Skan if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) 5760132730Skan layout_array_type (TREE_TYPE (decl)); 5761132730Skan if (TREE_CODE (decl) != TYPE_DECL) 5762132730Skan { 5763132730Skan layout_decl (decl, 0); 5764132730Skan if (c_dialect_objc ()) 5765132730Skan objc_check_decl (decl); 5766169699Skan rest_of_decl_compilation (decl, toplevel, 0); 5767169699Skan if (!toplevel) 5768132730Skan expand_decl (decl); 576918334Speter } 577018334Speter } 5771132730Skan C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0; 577218334Speter 577318334Speter /* Finish debugging output for this type. */ 577418334Speter rest_of_type_compilation (t, toplevel); 577518334Speter 5776169699Skan /* If we're inside a function proper, i.e. not file-scope and not still 5777169699Skan parsing parameters, then arrange for the size of a variable sized type 5778169699Skan to be bound now. */ 5779169699Skan if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE)) 5780169699Skan add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t))); 5781169699Skan 578218334Speter return t; 578318334Speter} 578418334Speter 578518334Speter/* Lay out the type T, and its element type, and so on. */ 578618334Speter 578718334Speterstatic void 5788132730Skanlayout_array_type (tree t) 578918334Speter{ 579018334Speter if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 579118334Speter layout_array_type (TREE_TYPE (t)); 579218334Speter layout_type (t); 579318334Speter} 579418334Speter 579518334Speter/* Begin compiling the definition of an enumeration type. 579618334Speter NAME is its name (or null if anonymous). 579718334Speter Returns the type object, as yet incomplete. 579818334Speter Also records info about it so that build_enumerator 579918334Speter may be used to declare the individual values as they are read. */ 580018334Speter 580118334Spetertree 5802132730Skanstart_enum (tree name) 580318334Speter{ 580490075Sobrien tree enumtype = 0; 580518334Speter 580618334Speter /* If this is the real definition for a previous forward reference, 580718334Speter fill in the contents in the same object that used to be the 580818334Speter forward reference. */ 580918334Speter 581018334Speter if (name != 0) 5811132730Skan enumtype = lookup_tag (ENUMERAL_TYPE, name, 1); 581218334Speter 581318334Speter if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE) 581418334Speter { 581518334Speter enumtype = make_node (ENUMERAL_TYPE); 581618334Speter pushtag (name, enumtype); 581718334Speter } 581818334Speter 5819161660Skan if (C_TYPE_BEING_DEFINED (enumtype)) 5820169699Skan error ("nested redefinition of %<enum %E%>", name); 5821161660Skan 582218334Speter C_TYPE_BEING_DEFINED (enumtype) = 1; 582318334Speter 582418334Speter if (TYPE_VALUES (enumtype) != 0) 582518334Speter { 582618334Speter /* This enum is a named one that has been declared already. */ 5827169699Skan error ("redeclaration of %<enum %E%>", name); 582818334Speter 582918334Speter /* Completely replace its old definition. 583018334Speter The old enumerators remain defined, however. */ 583118334Speter TYPE_VALUES (enumtype) = 0; 583218334Speter } 583318334Speter 583418334Speter enum_next_value = integer_zero_node; 583518334Speter enum_overflow = 0; 583618334Speter 583750397Sobrien if (flag_short_enums) 583850397Sobrien TYPE_PACKED (enumtype) = 1; 583950397Sobrien 584018334Speter return enumtype; 584118334Speter} 584218334Speter 584318334Speter/* After processing and defining all the values of an enumeration type, 584418334Speter install their decls in the enumeration type and finish it off. 584518334Speter ENUMTYPE is the type object, VALUES a list of decl-value pairs, 584618334Speter and ATTRIBUTES are the specified attributes. 584718334Speter Returns ENUMTYPE. */ 584818334Speter 584918334Spetertree 5850132730Skanfinish_enum (tree enumtype, tree values, tree attributes) 585118334Speter{ 585290075Sobrien tree pair, tem; 5853169699Skan tree minnode = 0, maxnode = 0; 585490075Sobrien int precision, unsign; 5855169699Skan bool toplevel = (file_scope == current_scope); 5856169699Skan struct lang_type *lt; 585718334Speter 585890075Sobrien decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 585918334Speter 586018334Speter /* Calculate the maximum value of any enumerator in this type. */ 586118334Speter 586218334Speter if (values == error_mark_node) 586318334Speter minnode = maxnode = integer_zero_node; 586418334Speter else 586590075Sobrien { 586690075Sobrien minnode = maxnode = TREE_VALUE (values); 586790075Sobrien for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair)) 586890075Sobrien { 586990075Sobrien tree value = TREE_VALUE (pair); 587090075Sobrien if (tree_int_cst_lt (maxnode, value)) 587190075Sobrien maxnode = value; 587290075Sobrien if (tree_int_cst_lt (value, minnode)) 587390075Sobrien minnode = value; 587490075Sobrien } 587590075Sobrien } 587618334Speter 587790075Sobrien /* Construct the final type of this enumeration. It is the same 587890075Sobrien as one of the integral types - the narrowest one that fits, except 587990075Sobrien that normally we only go as narrow as int - and signed iff any of 588090075Sobrien the values are negative. */ 588190075Sobrien unsign = (tree_int_cst_sgn (minnode) >= 0); 588290075Sobrien precision = MAX (min_precision (minnode, unsign), 588390075Sobrien min_precision (maxnode, unsign)); 5884169699Skan 588550397Sobrien if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node)) 588650397Sobrien { 5887169699Skan tem = c_common_type_for_size (precision, unsign); 5888169699Skan if (tem == NULL) 588950397Sobrien { 5890169699Skan warning (0, "enumeration values exceed range of largest integer"); 5891169699Skan tem = long_long_integer_type_node; 589250397Sobrien } 589350397Sobrien } 589418334Speter else 5895169699Skan tem = unsign ? unsigned_type_node : integer_type_node; 589618334Speter 5897169699Skan TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem); 5898169699Skan TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem); 5899169699Skan TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem); 590018334Speter TYPE_SIZE (enumtype) = 0; 5901146906Skan 5902146906Skan /* If the precision of the type was specific with an attribute and it 5903146906Skan was too small, give an error. Otherwise, use it. */ 5904146906Skan if (TYPE_PRECISION (enumtype)) 5905146906Skan { 5906146906Skan if (precision > TYPE_PRECISION (enumtype)) 5907146906Skan error ("specified mode too small for enumeral values"); 5908146906Skan } 5909146906Skan else 5910169699Skan TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem); 5911146906Skan 591218334Speter layout_type (enumtype); 591318334Speter 591418334Speter if (values != error_mark_node) 591518334Speter { 591690075Sobrien /* Change the type of the enumerators to be the enum type. We 591790075Sobrien need to do this irrespective of the size of the enum, for 591890075Sobrien proper type checking. Replace the DECL_INITIALs of the 591990075Sobrien enumerators, and the value slots of the list, with copies 592090075Sobrien that have the enum type; they cannot be modified in place 592190075Sobrien because they may be shared (e.g. integer_zero_node) Finally, 592290075Sobrien change the purpose slots to point to the names of the decls. */ 592318334Speter for (pair = values; pair; pair = TREE_CHAIN (pair)) 592418334Speter { 592590075Sobrien tree enu = TREE_PURPOSE (pair); 5926169699Skan tree ini = DECL_INITIAL (enu); 592790075Sobrien 592890075Sobrien TREE_TYPE (enu) = enumtype; 592990075Sobrien 593090075Sobrien /* The ISO C Standard mandates enumerators to have type int, 593190075Sobrien even though the underlying type of an enum type is 593290075Sobrien unspecified. Here we convert any enumerators that fit in 593390075Sobrien an int to type int, to avoid promotions to unsigned types 593490075Sobrien when comparing integers with enumerators that fit in the 593590075Sobrien int range. When -pedantic is given, build_enumerator() 593690075Sobrien would have already taken care of those that don't fit. */ 5937169699Skan if (int_fits_type_p (ini, integer_type_node)) 5938169699Skan tem = integer_type_node; 593990075Sobrien else 5940169699Skan tem = enumtype; 5941169699Skan ini = convert (tem, ini); 594290075Sobrien 5943169699Skan DECL_INITIAL (enu) = ini; 594490075Sobrien TREE_PURPOSE (pair) = DECL_NAME (enu); 5945169699Skan TREE_VALUE (pair) = ini; 594618334Speter } 594718334Speter 594818334Speter TYPE_VALUES (enumtype) = values; 594918334Speter } 595018334Speter 5951169699Skan /* Record the min/max values so that we can warn about bit-field 5952169699Skan enumerations that are too small for the values. */ 5953169699Skan lt = GGC_CNEW (struct lang_type); 5954169699Skan lt->enum_min = minnode; 5955169699Skan lt->enum_max = maxnode; 5956169699Skan TYPE_LANG_SPECIFIC (enumtype) = lt; 5957169699Skan 595818334Speter /* Fix up all variant types of this enum type. */ 595918334Speter for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem)) 596018334Speter { 596190075Sobrien if (tem == enumtype) 596290075Sobrien continue; 596318334Speter TYPE_VALUES (tem) = TYPE_VALUES (enumtype); 596418334Speter TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype); 596518334Speter TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype); 596618334Speter TYPE_SIZE (tem) = TYPE_SIZE (enumtype); 596750397Sobrien TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype); 596818334Speter TYPE_MODE (tem) = TYPE_MODE (enumtype); 596918334Speter TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype); 597018334Speter TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype); 597190075Sobrien TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype); 5972169699Skan TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype); 5973169699Skan TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype); 597418334Speter } 597518334Speter 597618334Speter /* Finish debugging output for this type. */ 597718334Speter rest_of_type_compilation (enumtype, toplevel); 597818334Speter 597918334Speter return enumtype; 598018334Speter} 598118334Speter 598218334Speter/* Build and install a CONST_DECL for one value of the 598318334Speter current enumeration type (one that was begun with start_enum). 598418334Speter Return a tree-list containing the CONST_DECL and its value. 598518334Speter Assignment of sequential values by default is handled here. */ 598618334Speter 598718334Spetertree 5988132730Skanbuild_enumerator (tree name, tree value) 598918334Speter{ 599090075Sobrien tree decl, type; 599118334Speter 599218334Speter /* Validate and default VALUE. */ 599318334Speter 599418334Speter if (value != 0) 599518334Speter { 5996169699Skan /* Don't issue more errors for error_mark_node (i.e. an 5997169699Skan undeclared identifier) - just ignore the value expression. */ 5998169699Skan if (value == error_mark_node) 5999169699Skan value = 0; 6000169699Skan else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)) 6001169699Skan || TREE_CODE (value) != INTEGER_CST) 600218334Speter { 6003169699Skan error ("enumerator value for %qE is not an integer constant", name); 6004169699Skan value = 0; 600518334Speter } 600618334Speter else 600718334Speter { 6008169699Skan value = default_conversion (value); 6009169699Skan constant_expression_warning (value); 601018334Speter } 601118334Speter } 601218334Speter 601318334Speter /* Default based on previous value. */ 601418334Speter /* It should no longer be possible to have NON_LVALUE_EXPR 601518334Speter in the default. */ 601618334Speter if (value == 0) 601718334Speter { 601818334Speter value = enum_next_value; 601918334Speter if (enum_overflow) 602018334Speter error ("overflow in enumeration values"); 602118334Speter } 602218334Speter 6023169699Skan if (pedantic && !int_fits_type_p (value, integer_type_node)) 602418334Speter { 6025169699Skan pedwarn ("ISO C restricts enumerator values to range of %<int%>"); 6026169699Skan /* XXX This causes -pedantic to change the meaning of the program. 6027169699Skan Remove? -zw 2004-03-15 */ 602890075Sobrien value = convert (integer_type_node, value); 602918334Speter } 603018334Speter 603118334Speter /* Set basis for default for next value. */ 603218334Speter enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0); 603318334Speter enum_overflow = tree_int_cst_lt (enum_next_value, value); 603418334Speter 603518334Speter /* Now create a declaration for the enum value name. */ 603618334Speter 603718334Speter type = TREE_TYPE (value); 6038117421Skan type = c_common_type_for_size (MAX (TYPE_PRECISION (type), 6039117421Skan TYPE_PRECISION (integer_type_node)), 6040117421Skan (TYPE_PRECISION (type) 6041117421Skan >= TYPE_PRECISION (integer_type_node) 6042169699Skan && TYPE_UNSIGNED (type))); 604318334Speter 604418334Speter decl = build_decl (CONST_DECL, name, type); 604590075Sobrien DECL_INITIAL (decl) = convert (type, value); 604618334Speter pushdecl (decl); 604718334Speter 604890075Sobrien return tree_cons (decl, value, NULL_TREE); 604918334Speter} 605090075Sobrien 605118334Speter 605218334Speter/* Create the FUNCTION_DECL for a function definition. 605390075Sobrien DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of 605418334Speter the declaration; they describe the function's name and the type it returns, 605518334Speter but twisted together in a fashion that parallels the syntax of C. 605618334Speter 605718334Speter This function creates a binding context for the function body 605818334Speter as well as setting up the FUNCTION_DECL in current_function_decl. 605918334Speter 606018334Speter Returns 1 on success. If the DECLARATOR is not suitable for a function 606118334Speter (it defines a datum instead), we return 0, which tells 606290075Sobrien yyparse to report a parse error. */ 606318334Speter 606418334Speterint 6065169699Skanstart_function (struct c_declspecs *declspecs, struct c_declarator *declarator, 6066169699Skan tree attributes) 606718334Speter{ 606818334Speter tree decl1, old_decl; 6069169699Skan tree restype, resdecl; 6070169699Skan struct c_label_context_se *nstack_se; 6071169699Skan struct c_label_context_vm *nstack_vm; 607218334Speter 607350397Sobrien current_function_returns_value = 0; /* Assume, until we see it does. */ 607418334Speter current_function_returns_null = 0; 607596263Sobrien current_function_returns_abnormally = 0; 607618334Speter warn_about_return_type = 0; 6077169699Skan c_switch_stack = NULL; 607818334Speter 6079169699Skan nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se); 6080169699Skan nstack_se->labels_def = NULL; 6081169699Skan nstack_se->labels_used = NULL; 6082169699Skan nstack_se->next = label_context_stack_se; 6083169699Skan label_context_stack_se = nstack_se; 608418334Speter 6085169699Skan nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm); 6086169699Skan nstack_vm->labels_def = NULL; 6087169699Skan nstack_vm->labels_used = NULL; 6088169699Skan nstack_vm->scope = 0; 6089169699Skan nstack_vm->next = label_context_stack_vm; 6090169699Skan label_context_stack_vm = nstack_vm; 609118334Speter 6092169699Skan /* Indicate no valid break/continue context by setting these variables 6093169699Skan to some non-null, non-label value. We'll notice and emit the proper 6094169699Skan error message in c_finish_bc_stmt. */ 6095169699Skan c_break_label = c_cont_label = size_zero_node; 6096169699Skan 6097169699Skan decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL); 6098169699Skan 609918334Speter /* If the declarator is not suitable for a function definition, 610018334Speter cause a syntax error. */ 610118334Speter if (decl1 == 0) 610250397Sobrien { 6103169699Skan label_context_stack_se = label_context_stack_se->next; 6104169699Skan label_context_stack_vm = label_context_stack_vm->next; 610550397Sobrien return 0; 610650397Sobrien } 610718334Speter 610890075Sobrien decl_attributes (&decl1, attributes, 0); 610918334Speter 611090075Sobrien if (DECL_DECLARED_INLINE_P (decl1) 611190075Sobrien && DECL_UNINLINABLE (decl1) 611290075Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1))) 6113169699Skan warning (OPT_Wattributes, "inline function %q+D given attribute noinline", 6114169699Skan decl1); 611590075Sobrien 6116189824Sdas /* Handle gnu_inline attribute. */ 6117189824Sdas if (declspecs->inline_p 6118189824Sdas && !flag_gnu89_inline 6119189824Sdas && TREE_CODE (decl1) == FUNCTION_DECL 6120189824Sdas && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))) 6121189824Sdas { 6122189824Sdas if (declspecs->storage_class != csc_static) 6123189824Sdas DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1); 6124189824Sdas } 6125189824Sdas 612618334Speter announce_function (decl1); 612718334Speter 612890075Sobrien if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1)))) 612918334Speter { 613090075Sobrien error ("return type is an incomplete type"); 613118334Speter /* Make it return void instead. */ 613218334Speter TREE_TYPE (decl1) 613318334Speter = build_function_type (void_type_node, 613418334Speter TYPE_ARG_TYPES (TREE_TYPE (decl1))); 613518334Speter } 613618334Speter 613718334Speter if (warn_about_return_type) 6138169699Skan pedwarn_c99 ("return type defaults to %<int%>"); 613918334Speter 614018334Speter /* Make the init_value nonzero so pushdecl knows this is not tentative. 6141169699Skan error_mark_node is replaced below (in pop_scope) with the BLOCK. */ 614218334Speter DECL_INITIAL (decl1) = error_mark_node; 614318334Speter 614418334Speter /* If this definition isn't a prototype and we had a prototype declaration 6145169699Skan before, copy the arg type info from that prototype. */ 6146169699Skan old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope); 6147169699Skan if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL) 6148169699Skan old_decl = 0; 6149169699Skan current_function_prototype_locus = UNKNOWN_LOCATION; 6150169699Skan current_function_prototype_built_in = false; 6151169699Skan current_function_prototype_arg_types = NULL_TREE; 6152169699Skan if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0) 615318334Speter { 6154169699Skan if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE 6155169699Skan && comptypes (TREE_TYPE (TREE_TYPE (decl1)), 6156169699Skan TREE_TYPE (TREE_TYPE (old_decl)))) 6157169699Skan { 6158169699Skan TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl), 6159169699Skan TREE_TYPE (decl1)); 6160169699Skan current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl); 6161169699Skan current_function_prototype_built_in 6162169699Skan = C_DECL_BUILTIN_PROTOTYPE (old_decl); 6163169699Skan current_function_prototype_arg_types 6164169699Skan = TYPE_ARG_TYPES (TREE_TYPE (decl1)); 6165169699Skan } 6166169699Skan if (TREE_PUBLIC (decl1)) 6167169699Skan { 6168169699Skan /* If there is an external prototype declaration of this 6169169699Skan function, record its location but do not copy information 6170169699Skan to this decl. This may be an invisible declaration 6171169699Skan (built-in or in a scope which has finished) or simply 6172169699Skan have more refined argument types than any declaration 6173169699Skan found above. */ 6174169699Skan struct c_binding *b; 6175169699Skan for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed) 6176169699Skan if (B_IN_SCOPE (b, external_scope)) 6177169699Skan break; 6178169699Skan if (b) 6179169699Skan { 6180169699Skan tree ext_decl, ext_type; 6181169699Skan ext_decl = b->decl; 6182169699Skan ext_type = b->type ? b->type : TREE_TYPE (ext_decl); 6183169699Skan if (TREE_CODE (ext_type) == FUNCTION_TYPE 6184169699Skan && comptypes (TREE_TYPE (TREE_TYPE (decl1)), 6185169699Skan TREE_TYPE (ext_type))) 6186169699Skan { 6187169699Skan current_function_prototype_locus 6188169699Skan = DECL_SOURCE_LOCATION (ext_decl); 6189169699Skan current_function_prototype_built_in 6190169699Skan = C_DECL_BUILTIN_PROTOTYPE (ext_decl); 6191169699Skan current_function_prototype_arg_types 6192169699Skan = TYPE_ARG_TYPES (ext_type); 6193169699Skan } 6194169699Skan } 6195169699Skan } 619618334Speter } 619718334Speter 619818334Speter /* Optionally warn of old-fashioned def with no previous prototype. */ 619918334Speter if (warn_strict_prototypes 6200169699Skan && old_decl != error_mark_node 620118334Speter && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0 6202132730Skan && C_DECL_ISNT_PROTOTYPE (old_decl)) 6203169699Skan warning (OPT_Wstrict_prototypes, 6204169699Skan "function declaration isn%'t a prototype"); 620518334Speter /* Optionally warn of any global def with no previous prototype. */ 620618334Speter else if (warn_missing_prototypes 6207169699Skan && old_decl != error_mark_node 620818334Speter && TREE_PUBLIC (decl1) 6209169699Skan && !MAIN_NAME_P (DECL_NAME (decl1)) 6210132730Skan && C_DECL_ISNT_PROTOTYPE (old_decl)) 6211169699Skan warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1); 621218334Speter /* Optionally warn of any def with no previous prototype 621318334Speter if the function has already been used. */ 621418334Speter else if (warn_missing_prototypes 6215169699Skan && old_decl != 0 6216169699Skan && old_decl != error_mark_node 6217169699Skan && TREE_USED (old_decl) 621818334Speter && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0) 6219169699Skan warning (OPT_Wmissing_prototypes, 6220169699Skan "%q+D was used with no prototype before its definition", decl1); 622118334Speter /* Optionally warn of any global def with no previous declaration. */ 622218334Speter else if (warn_missing_declarations 622318334Speter && TREE_PUBLIC (decl1) 622418334Speter && old_decl == 0 6225169699Skan && !MAIN_NAME_P (DECL_NAME (decl1))) 6226169699Skan warning (OPT_Wmissing_declarations, "no previous declaration for %q+D", 6227169699Skan decl1); 622818334Speter /* Optionally warn of any def with no previous declaration 622918334Speter if the function has already been used. */ 623018334Speter else if (warn_missing_declarations 6231169699Skan && old_decl != 0 6232169699Skan && old_decl != error_mark_node 6233169699Skan && TREE_USED (old_decl) 6234132730Skan && C_DECL_IMPLICIT (old_decl)) 6235169699Skan warning (OPT_Wmissing_declarations, 6236169699Skan "%q+D was used with no declaration before its definition", decl1); 623718334Speter 623818334Speter /* This function exists in static storage. 623918334Speter (This does not mean `static' in the C sense!) */ 624018334Speter TREE_STATIC (decl1) = 1; 624118334Speter 624218334Speter /* A nested function is not global. */ 624318334Speter if (current_function_decl != 0) 624418334Speter TREE_PUBLIC (decl1) = 0; 624518334Speter 6246132730Skan /* This is the earliest point at which we might know the assembler 6247132730Skan name of the function. Thus, if it's set before this, die horribly. */ 6248169699Skan gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1)); 6249132730Skan 6250132730Skan /* If #pragma weak was used, mark the decl weak now. */ 6251169699Skan if (current_scope == file_scope) 6252132730Skan maybe_apply_pragma_weak (decl1); 6253132730Skan 625490075Sobrien /* Warn for unlikely, improbable, or stupid declarations of `main'. */ 625590075Sobrien if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1))) 625650397Sobrien { 625750397Sobrien tree args; 625850397Sobrien int argct = 0; 625950397Sobrien 626050397Sobrien if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1))) 626190075Sobrien != integer_type_node) 6262169699Skan pedwarn ("return type of %q+D is not %<int%>", decl1); 626350397Sobrien 626450397Sobrien for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args; 626550397Sobrien args = TREE_CHAIN (args)) 626650397Sobrien { 626750397Sobrien tree type = args ? TREE_VALUE (args) : 0; 626850397Sobrien 626950397Sobrien if (type == void_type_node) 627050397Sobrien break; 627150397Sobrien 627250397Sobrien ++argct; 627350397Sobrien switch (argct) 627450397Sobrien { 627550397Sobrien case 1: 627650397Sobrien if (TYPE_MAIN_VARIANT (type) != integer_type_node) 6277169699Skan pedwarn ("first argument of %q+D should be %<int%>", decl1); 627850397Sobrien break; 627950397Sobrien 628050397Sobrien case 2: 628150397Sobrien if (TREE_CODE (type) != POINTER_TYPE 628250397Sobrien || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 628350397Sobrien || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 628450397Sobrien != char_type_node)) 6285169699Skan pedwarn ("second argument of %q+D should be %<char **%>", 6286169699Skan decl1); 628750397Sobrien break; 628850397Sobrien 628950397Sobrien case 3: 629050397Sobrien if (TREE_CODE (type) != POINTER_TYPE 629150397Sobrien || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 629250397Sobrien || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 629350397Sobrien != char_type_node)) 6294169699Skan pedwarn ("third argument of %q+D should probably be " 6295169699Skan "%<char **%>", decl1); 629650397Sobrien break; 629750397Sobrien } 629850397Sobrien } 629950397Sobrien 630050397Sobrien /* It is intentional that this message does not mention the third 630190075Sobrien argument because it's only mentioned in an appendix of the 630290075Sobrien standard. */ 630350397Sobrien if (argct > 0 && (argct < 2 || argct > 3)) 6304169699Skan pedwarn ("%q+D takes only zero or two arguments", decl1); 630550397Sobrien 6306169699Skan if (!TREE_PUBLIC (decl1)) 6307169699Skan pedwarn ("%q+D is normally a non-static function", decl1); 630850397Sobrien } 630950397Sobrien 631018334Speter /* Record the decl so that the function name is defined. 631118334Speter If we already have a decl for this name, and it is a FUNCTION_DECL, 631218334Speter use the old decl. */ 631318334Speter 631418334Speter current_function_decl = pushdecl (decl1); 631518334Speter 6316169699Skan push_scope (); 6317132730Skan declare_parm_level (); 631818334Speter 631918334Speter restype = TREE_TYPE (TREE_TYPE (current_function_decl)); 632018334Speter /* Promote the value to int before returning it. */ 632190075Sobrien if (c_promoting_integer_type_p (restype)) 632218334Speter { 6323117421Skan /* It retains unsignedness if not really getting wider. */ 6324169699Skan if (TYPE_UNSIGNED (restype) 6325117421Skan && (TYPE_PRECISION (restype) 6326117421Skan == TYPE_PRECISION (integer_type_node))) 632718334Speter restype = unsigned_type_node; 632818334Speter else 632918334Speter restype = integer_type_node; 633018334Speter } 633118334Speter 6332169699Skan resdecl = build_decl (RESULT_DECL, NULL_TREE, restype); 6333169699Skan DECL_ARTIFICIAL (resdecl) = 1; 6334169699Skan DECL_IGNORED_P (resdecl) = 1; 6335169699Skan DECL_RESULT (current_function_decl) = resdecl; 633618334Speter 633790075Sobrien start_fname_decls (); 6338132730Skan 633918334Speter return 1; 634018334Speter} 634118334Speter 6342132730Skan/* Subroutine of store_parm_decls which handles new-style function 6343132730Skan definitions (prototype format). The parms already have decls, so we 6344132730Skan need only record them as in effect and complain if any redundant 6345132730Skan old-style parm decls were written. */ 6346132730Skanstatic void 6347169699Skanstore_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info) 634818334Speter{ 6349169699Skan tree decl; 635018334Speter 6351169699Skan if (current_scope->bindings) 6352132730Skan { 6353132730Skan error ("%Jold-style parameter declarations in prototyped " 6354132730Skan "function definition", fndecl); 635518334Speter 6356132730Skan /* Get rid of the old-style declarations. */ 6357169699Skan pop_scope (); 6358169699Skan push_scope (); 6359132730Skan } 6360169699Skan /* Don't issue this warning for nested functions, and don't issue this 6361169699Skan warning if we got here because ARG_INFO_TYPES was error_mark_node 6362169699Skan (this happens when a function definition has just an ellipsis in 6363169699Skan its parameter list). */ 6364169699Skan else if (!in_system_header && !current_function_scope 6365169699Skan && arg_info->types != error_mark_node) 6366169699Skan warning (OPT_Wtraditional, 6367169699Skan "%Jtraditional C rejects ISO C style function definitions", 6368169699Skan fndecl); 636918334Speter 6370132730Skan /* Now make all the parameter declarations visible in the function body. 6371132730Skan We can bypass most of the grunt work of pushdecl. */ 6372169699Skan for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl)) 6373132730Skan { 6374132730Skan DECL_CONTEXT (decl) = current_function_decl; 6375169699Skan if (DECL_NAME (decl)) 6376132730Skan { 6377169699Skan bind (DECL_NAME (decl), decl, current_scope, 6378169699Skan /*invisible=*/false, /*nested=*/false); 6379169699Skan if (!TREE_USED (decl)) 6380169699Skan warn_if_shadowing (decl); 6381132730Skan } 6382169699Skan else 6383169699Skan error ("%Jparameter name omitted", decl); 6384132730Skan } 638518334Speter 6386132730Skan /* Record the parameter list in the function declaration. */ 6387169699Skan DECL_ARGUMENTS (fndecl) = arg_info->parms; 638818334Speter 6389132730Skan /* Now make all the ancillary declarations visible, likewise. */ 6390169699Skan for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl)) 6391132730Skan { 6392132730Skan DECL_CONTEXT (decl) = current_function_decl; 6393169699Skan if (DECL_NAME (decl)) 6394169699Skan bind (DECL_NAME (decl), decl, current_scope, 6395169699Skan /*invisible=*/false, /*nested=*/false); 6396132730Skan } 639790075Sobrien 6398132730Skan /* And all the tag declarations. */ 6399169699Skan for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl)) 6400132730Skan if (TREE_PURPOSE (decl)) 6401169699Skan bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope, 6402169699Skan /*invisible=*/false, /*nested=*/false); 6403132730Skan} 640418334Speter 6405132730Skan/* Subroutine of store_parm_decls which handles old-style function 6406132730Skan definitions (separate parameter list and declarations). */ 640790075Sobrien 6408132730Skanstatic void 6409169699Skanstore_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info) 6410132730Skan{ 6411169699Skan struct c_binding *b; 6412132730Skan tree parm, decl, last; 6413169699Skan tree parmids = arg_info->parms; 6414169699Skan struct pointer_set_t *seen_args = pointer_set_create (); 641590075Sobrien 6416169699Skan if (!in_system_header) 6417169699Skan warning (OPT_Wold_style_definition, "%Jold-style function definition", 6418169699Skan fndecl); 641918334Speter 6420132730Skan /* Match each formal parameter name with its declaration. Save each 6421132730Skan decl in the appropriate TREE_PURPOSE slot of the parmids chain. */ 6422132730Skan for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 6423132730Skan { 6424132730Skan if (TREE_VALUE (parm) == 0) 642518334Speter { 6426132730Skan error ("%Jparameter name missing from parameter list", fndecl); 6427132730Skan TREE_PURPOSE (parm) = 0; 6428132730Skan continue; 642918334Speter } 643018334Speter 6431169699Skan b = I_SYMBOL_BINDING (TREE_VALUE (parm)); 6432169699Skan if (b && B_IN_CURRENT_SCOPE (b)) 643318334Speter { 6434169699Skan decl = b->decl; 6435132730Skan /* If we got something other than a PARM_DECL it is an error. */ 6436132730Skan if (TREE_CODE (decl) != PARM_DECL) 6437169699Skan error ("%q+D declared as a non-parameter", decl); 6438132730Skan /* If the declaration is already marked, we have a duplicate 6439132730Skan name. Complain and ignore the duplicate. */ 6440169699Skan else if (pointer_set_contains (seen_args, decl)) 644118334Speter { 6442169699Skan error ("multiple parameters named %q+D", decl); 6443132730Skan TREE_PURPOSE (parm) = 0; 6444132730Skan continue; 644518334Speter } 6446132730Skan /* If the declaration says "void", complain and turn it into 6447132730Skan an int. */ 6448132730Skan else if (VOID_TYPE_P (TREE_TYPE (decl))) 644918334Speter { 6450169699Skan error ("parameter %q+D declared with void type", decl); 6451132730Skan TREE_TYPE (decl) = integer_type_node; 6452132730Skan DECL_ARG_TYPE (decl) = integer_type_node; 6453132730Skan layout_decl (decl, 0); 645418334Speter } 6455169699Skan warn_if_shadowing (decl); 645618334Speter } 6457132730Skan /* If no declaration found, default to int. */ 6458132730Skan else 645918334Speter { 6460132730Skan decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node); 6461132730Skan DECL_ARG_TYPE (decl) = TREE_TYPE (decl); 6462132730Skan DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl); 6463132730Skan pushdecl (decl); 6464169699Skan warn_if_shadowing (decl); 646518334Speter 6466132730Skan if (flag_isoc99) 6467169699Skan pedwarn ("type of %q+D defaults to %<int%>", decl); 6468132730Skan else if (extra_warnings) 6469169699Skan warning (OPT_Wextra, "type of %q+D defaults to %<int%>", decl); 647018334Speter } 647118334Speter 6472132730Skan TREE_PURPOSE (parm) = decl; 6473169699Skan pointer_set_insert (seen_args, decl); 647418334Speter } 647518334Speter 6476132730Skan /* Now examine the parms chain for incomplete declarations 6477132730Skan and declarations with no corresponding names. */ 647818334Speter 6479169699Skan for (b = current_scope->bindings; b; b = b->prev) 6480132730Skan { 6481169699Skan parm = b->decl; 6482169699Skan if (TREE_CODE (parm) != PARM_DECL) 6483169699Skan continue; 6484169699Skan 6485169699Skan if (TREE_TYPE (parm) != error_mark_node 6486169699Skan && !COMPLETE_TYPE_P (TREE_TYPE (parm))) 6487132730Skan { 6488169699Skan error ("parameter %q+D has incomplete type", parm); 6489132730Skan TREE_TYPE (parm) = error_mark_node; 6490132730Skan } 649118334Speter 6492169699Skan if (!pointer_set_contains (seen_args, parm)) 649318334Speter { 6494169699Skan error ("declaration for parameter %q+D but no such parameter", parm); 649518334Speter 6496132730Skan /* Pretend the parameter was not missing. 6497132730Skan This gets us to a standard state and minimizes 6498132730Skan further error messages. */ 6499132730Skan parmids = chainon (parmids, tree_cons (parm, 0, 0)); 6500132730Skan } 6501132730Skan } 650218334Speter 6503132730Skan /* Chain the declarations together in the order of the list of 6504132730Skan names. Store that chain in the function decl, replacing the 6505132730Skan list of names. Update the current scope to match. */ 6506132730Skan DECL_ARGUMENTS (fndecl) = 0; 650718334Speter 6508132730Skan for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 6509132730Skan if (TREE_PURPOSE (parm)) 6510132730Skan break; 6511132730Skan if (parm && TREE_PURPOSE (parm)) 6512132730Skan { 6513132730Skan last = TREE_PURPOSE (parm); 6514132730Skan DECL_ARGUMENTS (fndecl) = last; 651518334Speter 6516132730Skan for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm)) 6517132730Skan if (TREE_PURPOSE (parm)) 6518132730Skan { 6519132730Skan TREE_CHAIN (last) = TREE_PURPOSE (parm); 6520132730Skan last = TREE_PURPOSE (parm); 6521132730Skan } 6522132730Skan TREE_CHAIN (last) = 0; 6523132730Skan } 652418334Speter 6525169699Skan pointer_set_destroy (seen_args); 6526169699Skan 6527132730Skan /* If there was a previous prototype, 6528132730Skan set the DECL_ARG_TYPE of each argument according to 6529132730Skan the type previously specified, and report any mismatches. */ 653018334Speter 6531169699Skan if (current_function_prototype_arg_types) 6532132730Skan { 6533132730Skan tree type; 6534132730Skan for (parm = DECL_ARGUMENTS (fndecl), 6535169699Skan type = current_function_prototype_arg_types; 6536132730Skan parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) 6537132730Skan != void_type_node)); 6538132730Skan parm = TREE_CHAIN (parm), type = TREE_CHAIN (type)) 653918334Speter { 6540132730Skan if (parm == 0 || type == 0 6541132730Skan || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node) 654218334Speter { 6543169699Skan if (current_function_prototype_built_in) 6544169699Skan warning (0, "number of arguments doesn%'t match " 6545169699Skan "built-in prototype"); 6546169699Skan else 6547169699Skan { 6548169699Skan error ("number of arguments doesn%'t match prototype"); 6549169699Skan error ("%Hprototype declaration", 6550169699Skan ¤t_function_prototype_locus); 6551169699Skan } 6552132730Skan break; 655318334Speter } 6554132730Skan /* Type for passing arg must be consistent with that 6555132730Skan declared for the arg. ISO C says we take the unqualified 6556132730Skan type for parameters declared with qualified type. */ 6557169699Skan if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)), 6558169699Skan TYPE_MAIN_VARIANT (TREE_VALUE (type)))) 655918334Speter { 6560132730Skan if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) 6561132730Skan == TYPE_MAIN_VARIANT (TREE_VALUE (type))) 656218334Speter { 6563132730Skan /* Adjust argument to match prototype. E.g. a previous 6564132730Skan `int foo(float);' prototype causes 6565132730Skan `int foo(x) float x; {...}' to be treated like 6566132730Skan `int foo(float x) {...}'. This is particularly 6567132730Skan useful for argument types like uid_t. */ 6568132730Skan DECL_ARG_TYPE (parm) = TREE_TYPE (parm); 656990075Sobrien 6570132730Skan if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl)) 6571132730Skan && INTEGRAL_TYPE_P (TREE_TYPE (parm)) 6572132730Skan && TYPE_PRECISION (TREE_TYPE (parm)) 6573132730Skan < TYPE_PRECISION (integer_type_node)) 6574132730Skan DECL_ARG_TYPE (parm) = integer_type_node; 657590075Sobrien 6576132730Skan if (pedantic) 657718334Speter { 6578169699Skan /* ??? Is it possible to get here with a 6579169699Skan built-in prototype or will it always have 6580169699Skan been diagnosed as conflicting with an 6581169699Skan old-style definition and discarded? */ 6582169699Skan if (current_function_prototype_built_in) 6583169699Skan warning (0, "promoted argument %qD " 6584169699Skan "doesn%'t match built-in prototype", parm); 6585169699Skan else 6586169699Skan { 6587169699Skan pedwarn ("promoted argument %qD " 6588169699Skan "doesn%'t match prototype", parm); 6589169699Skan pedwarn ("%Hprototype declaration", 6590169699Skan ¤t_function_prototype_locus); 6591169699Skan } 659218334Speter } 659318334Speter } 6594132730Skan else 6595132730Skan { 6596169699Skan if (current_function_prototype_built_in) 6597169699Skan warning (0, "argument %qD doesn%'t match " 6598169699Skan "built-in prototype", parm); 6599169699Skan else 6600169699Skan { 6601169699Skan error ("argument %qD doesn%'t match prototype", parm); 6602169699Skan error ("%Hprototype declaration", 6603169699Skan ¤t_function_prototype_locus); 6604169699Skan } 6605132730Skan } 660618334Speter } 660718334Speter } 6608132730Skan TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0; 6609132730Skan } 661018334Speter 6611132730Skan /* Otherwise, create a prototype that would match. */ 661218334Speter 6613132730Skan else 6614132730Skan { 6615132730Skan tree actual = 0, last = 0, type; 6616132730Skan 6617132730Skan for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm)) 661818334Speter { 6619132730Skan type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE); 662018334Speter if (last) 662118334Speter TREE_CHAIN (last) = type; 662218334Speter else 662318334Speter actual = type; 6624132730Skan last = type; 6625132730Skan } 6626132730Skan type = tree_cons (NULL_TREE, void_type_node, NULL_TREE); 6627132730Skan if (last) 6628132730Skan TREE_CHAIN (last) = type; 6629132730Skan else 6630132730Skan actual = type; 663118334Speter 6632132730Skan /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES 6633132730Skan of the type of this function, but we need to avoid having this 6634132730Skan affect the types of other similarly-typed functions, so we must 6635132730Skan first force the generation of an identical (but separate) type 6636132730Skan node for the relevant function type. The new node we create 6637132730Skan will be a variant of the main variant of the original function 6638132730Skan type. */ 663918334Speter 6640169699Skan TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl)); 664118334Speter 6642132730Skan TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual; 6643132730Skan } 6644132730Skan} 664518334Speter 6646169699Skan/* Store parameter declarations passed in ARG_INFO into the current 6647169699Skan function declaration. */ 6648169699Skan 6649169699Skanvoid 6650169699Skanstore_parm_decls_from (struct c_arg_info *arg_info) 6651169699Skan{ 6652169699Skan current_function_arg_info = arg_info; 6653169699Skan store_parm_decls (); 6654169699Skan} 6655169699Skan 6656132730Skan/* Store the parameter declarations into the current function declaration. 6657132730Skan This is called after parsing the parameter declarations, before 6658132730Skan digesting the body of the function. 665918334Speter 6660132730Skan For an old-style definition, construct a prototype out of the old-style 6661132730Skan parameter declarations and inject it into the function's type. */ 666218334Speter 6663132730Skanvoid 6664132730Skanstore_parm_decls (void) 6665132730Skan{ 6666132730Skan tree fndecl = current_function_decl; 6667169699Skan bool proto; 666818334Speter 6669169699Skan /* The argument information block for FNDECL. */ 6670169699Skan struct c_arg_info *arg_info = current_function_arg_info; 6671169699Skan current_function_arg_info = 0; 667218334Speter 6673169699Skan /* True if this definition is written with a prototype. Note: 6674169699Skan despite C99 6.7.5.3p14, we can *not* treat an empty argument 6675169699Skan list in a function definition as equivalent to (void) -- an 6676169699Skan empty argument list specifies the function has no parameters, 6677169699Skan but only (void) sets up a prototype for future calls. */ 6678169699Skan proto = arg_info->types != 0; 6679169699Skan 6680169699Skan if (proto) 6681169699Skan store_parm_decls_newstyle (fndecl, arg_info); 6682132730Skan else 6683169699Skan store_parm_decls_oldstyle (fndecl, arg_info); 6684132730Skan 6685169699Skan /* The next call to push_scope will be a function body. */ 6686132730Skan 6687132730Skan next_is_function_body = true; 6688132730Skan 668918334Speter /* Write a record describing this function definition to the prototypes 669018334Speter file (if requested). */ 669118334Speter 6692169699Skan gen_aux_info_record (fndecl, 1, 0, proto); 669318334Speter 669418334Speter /* Initialize the RTL code for the function. */ 6695132730Skan allocate_struct_function (fndecl); 669618334Speter 669790075Sobrien /* Begin the statement tree for this function. */ 6698169699Skan DECL_SAVED_TREE (fndecl) = push_stmt_list (); 669918334Speter 6700169699Skan /* ??? Insert the contents of the pending sizes list into the function 6701169699Skan to be evaluated. The only reason left to have this is 6702169699Skan void foo(int n, int array[n++]) 6703169699Skan because we throw away the array type in favor of a pointer type, and 6704169699Skan thus won't naturally see the SAVE_EXPR containing the increment. All 6705169699Skan other pending sizes would be handled by gimplify_parameters. */ 6706169699Skan { 6707169699Skan tree t; 6708169699Skan for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t)) 6709169699Skan add_stmt (TREE_VALUE (t)); 6710169699Skan } 671118334Speter 671290075Sobrien /* Even though we're inside a function body, we still don't want to 671390075Sobrien call expand_expr to calculate the size of a variable-sized array. 671490075Sobrien We haven't necessarily assigned RTL to all variables yet, so it's 671590075Sobrien not safe to try to expand expressions involving them. */ 671690075Sobrien cfun->x_dont_save_pending_sizes_p = 1; 671718334Speter} 671818334Speter 6719169699Skan/* Emit diagnostics that require gimple input for detection. Operate on 6720169699Skan FNDECL and all its nested functions. */ 6721169699Skan 6722169699Skanstatic void 6723169699Skanc_gimple_diagnostics_recursively (tree fndecl) 6724169699Skan{ 6725169699Skan struct cgraph_node *cgn; 6726169699Skan 6727169699Skan /* Handle attribute((warn_unused_result)). Relies on gimple input. */ 6728169699Skan c_warn_unused_result (&DECL_SAVED_TREE (fndecl)); 6729169699Skan 6730169699Skan /* Notice when OpenMP structured block constraints are violated. */ 6731169699Skan if (flag_openmp) 6732169699Skan diagnose_omp_structured_block_errors (fndecl); 6733169699Skan 6734169699Skan /* Finalize all nested functions now. */ 6735169699Skan cgn = cgraph_node (fndecl); 6736169699Skan for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested) 6737169699Skan c_gimple_diagnostics_recursively (cgn->decl); 6738169699Skan} 6739169699Skan 674018334Speter/* Finish up a function declaration and compile that function 674118334Speter all the way to assembler language output. The free the storage 674218334Speter for the function definition. 674318334Speter 6744132730Skan This is called after parsing the body of the function definition. */ 674518334Speter 674618334Spetervoid 6747132730Skanfinish_function (void) 674818334Speter{ 674990075Sobrien tree fndecl = current_function_decl; 675018334Speter 6751169699Skan label_context_stack_se = label_context_stack_se->next; 6752169699Skan label_context_stack_vm = label_context_stack_vm->next; 675318334Speter 6754132730Skan if (TREE_CODE (fndecl) == FUNCTION_DECL 6755132730Skan && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))) 6756132730Skan { 6757132730Skan tree args = DECL_ARGUMENTS (fndecl); 6758132730Skan for (; args; args = TREE_CHAIN (args)) 6759169699Skan { 6760169699Skan tree type = TREE_TYPE (args); 6761169699Skan if (INTEGRAL_TYPE_P (type) 6762169699Skan && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 6763169699Skan DECL_ARG_TYPE (args) = integer_type_node; 6764169699Skan } 6765132730Skan } 6766132730Skan 6767132730Skan if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) 6768132730Skan BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; 6769132730Skan 677018334Speter /* Must mark the RESULT_DECL as being in this function. */ 677118334Speter 6772132730Skan if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node) 6773132730Skan DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; 677418334Speter 677590075Sobrien if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted) 677618334Speter { 677718334Speter if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))) 677818334Speter != integer_type_node) 677950397Sobrien { 678052284Sobrien /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned. 678190075Sobrien If warn_main is -1 (-Wno-main) we don't want to be warned. */ 6782132730Skan if (!warn_main) 6783169699Skan pedwarn ("return type of %q+D is not %<int%>", fndecl); 678450397Sobrien } 678518334Speter else 678618334Speter { 6787169699Skan if (flag_isoc99) 6788169699Skan { 6789169699Skan tree stmt = c_finish_return (integer_zero_node); 6790169699Skan#ifdef USE_MAPPED_LOCATION 6791169699Skan /* Hack. We don't want the middle-end to warn that this return 6792169699Skan is unreachable, so we mark its location as special. Using 6793169699Skan UNKNOWN_LOCATION has the problem that it gets clobbered in 6794169699Skan annotate_one_with_locus. A cleaner solution might be to 6795169699Skan ensure ! should_carry_locus_p (stmt), but that needs a flag. 6796169699Skan */ 6797169699Skan SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION); 679890075Sobrien#else 6799169699Skan /* Hack. We don't want the middle-end to warn that this 6800169699Skan return is unreachable, so put the statement on the 6801169699Skan special line 0. */ 6802169699Skan annotate_with_file_line (stmt, input_filename, 0); 680350397Sobrien#endif 6804169699Skan } 680518334Speter } 680618334Speter } 6807132730Skan 6808169699Skan /* Tie off the statement tree for this function. */ 6809169699Skan DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl)); 6810169699Skan 681190075Sobrien finish_fname_decls (); 681218334Speter 681396263Sobrien /* Complain if there's just no return statement. */ 681496263Sobrien if (warn_return_type 681596263Sobrien && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE 681696263Sobrien && !current_function_returns_value && !current_function_returns_null 6817169699Skan /* Don't complain if we are no-return. */ 681896263Sobrien && !current_function_returns_abnormally 681996263Sobrien /* Don't warn for main(). */ 682096263Sobrien && !MAIN_NAME_P (DECL_NAME (fndecl)) 682196263Sobrien /* Or if they didn't actually specify a return type. */ 682296263Sobrien && !C_FUNCTION_IMPLICIT_INT (fndecl) 682396263Sobrien /* Normally, with -Wreturn-type, flow will complain. Unless we're an 682496263Sobrien inline function, as we might never be compiled separately. */ 682596263Sobrien && DECL_INLINE (fndecl)) 6826169699Skan { 6827169699Skan warning (OPT_Wreturn_type, 6828169699Skan "no return statement in function returning non-void"); 6829169699Skan TREE_NO_WARNING (fndecl) = 1; 6830169699Skan } 683196263Sobrien 6832132730Skan /* With just -Wextra, complain only if function returns both with 6833122196Skan and without a value. */ 6834122196Skan if (extra_warnings 6835122196Skan && current_function_returns_value 6836122196Skan && current_function_returns_null) 6837169699Skan warning (OPT_Wextra, "this function may return with or without a value"); 6838122196Skan 6839169699Skan /* Store the end of the function, so that we get good line number 6840169699Skan info for the epilogue. */ 6841169699Skan cfun->function_end_locus = input_location; 684290075Sobrien 6843169699Skan /* If we don't have ctors/dtors sections, and this is a static 6844169699Skan constructor or destructor, it must be recorded now. */ 6845169699Skan if (DECL_STATIC_CONSTRUCTOR (fndecl) 6846169699Skan && !targetm.have_ctors_dtors) 6847169699Skan static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors); 6848169699Skan if (DECL_STATIC_DESTRUCTOR (fndecl) 6849169699Skan && !targetm.have_ctors_dtors) 6850169699Skan static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors); 685190075Sobrien 6852169699Skan /* Finalize the ELF visibility for the function. */ 6853169699Skan c_determine_visibility (fndecl); 685490075Sobrien 6855169699Skan /* Genericize before inlining. Delay genericizing nested functions 6856169699Skan until their parent function is genericized. Since finalizing 6857169699Skan requires GENERIC, delay that as well. */ 6858169699Skan 6859169699Skan if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node 6860169699Skan && !undef_nested_function) 686190075Sobrien { 6862169699Skan if (!decl_function_context (fndecl)) 6863132730Skan { 6864169699Skan c_genericize (fndecl); 6865169699Skan c_gimple_diagnostics_recursively (fndecl); 686690075Sobrien 6867169699Skan /* ??? Objc emits functions after finalizing the compilation unit. 6868169699Skan This should be cleaned up later and this conditional removed. */ 6869169699Skan if (cgraph_global_info_ready) 6870169699Skan { 6871169699Skan c_expand_body (fndecl); 6872169699Skan return; 6873169699Skan } 687490075Sobrien 6875169699Skan cgraph_finalize_function (fndecl, false); 6876169699Skan } 687718334Speter else 6878169699Skan { 6879169699Skan /* Register this function with cgraph just far enough to get it 6880169699Skan added to our parent's nested function list. Handy, since the 6881169699Skan C front end doesn't have such a list. */ 6882169699Skan (void) cgraph_node (fndecl); 6883169699Skan } 688418334Speter } 688590075Sobrien 6886169699Skan if (!decl_function_context (fndecl)) 6887169699Skan undef_nested_function = false; 6888169699Skan 6889169699Skan /* We're leaving the context of this function, so zap cfun. 6890169699Skan It's still in DECL_STRUCT_FUNCTION, and we'll restore it in 6891169699Skan tree_rest_of_compilation. */ 6892169699Skan cfun = NULL; 6893169699Skan current_function_decl = NULL; 6894132730Skan} 689518334Speter 6896169699Skan/* Generate the RTL for the body of FNDECL. */ 6897117421Skan 6898132730Skanvoid 6899132730Skanc_expand_body (tree fndecl) 6900132730Skan{ 6901132730Skan 6902169699Skan if (!DECL_INITIAL (fndecl) 6903169699Skan || DECL_INITIAL (fndecl) == error_mark_node) 6904169699Skan return; 6905169699Skan 6906169699Skan tree_rest_of_compilation (fndecl); 6907169699Skan 6908169699Skan if (DECL_STATIC_CONSTRUCTOR (fndecl) 6909169699Skan && targetm.have_ctors_dtors) 6910169699Skan targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0), 6911169699Skan DEFAULT_INIT_PRIORITY); 6912169699Skan if (DECL_STATIC_DESTRUCTOR (fndecl) 6913169699Skan && targetm.have_ctors_dtors) 6914169699Skan targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0), 6915169699Skan DEFAULT_INIT_PRIORITY); 691690075Sobrien} 691790075Sobrien 691890075Sobrien/* Check the declarations given in a for-loop for satisfying the C99 6919169699Skan constraints. If exactly one such decl is found, return it. */ 6920169699Skan 6921169699Skantree 6922132730Skancheck_for_loop_decls (void) 692390075Sobrien{ 6924169699Skan struct c_binding *b; 6925169699Skan tree one_decl = NULL_TREE; 6926169699Skan int n_decls = 0; 692790075Sobrien 6928169699Skan 692990075Sobrien if (!flag_isoc99) 693018334Speter { 693190075Sobrien /* If we get here, declarations have been used in a for loop without 693290075Sobrien the C99 for loop scope. This doesn't make much sense, so don't 693390075Sobrien allow it. */ 6934169699Skan error ("%<for%> loop initial declaration used outside C99 mode"); 6935169699Skan return NULL_TREE; 693618334Speter } 693790075Sobrien /* C99 subclause 6.8.5 paragraph 3: 693890075Sobrien 693990075Sobrien [#3] The declaration part of a for statement shall only 694090075Sobrien declare identifiers for objects having storage class auto or 694190075Sobrien register. 694290075Sobrien 694390075Sobrien It isn't clear whether, in this sentence, "identifiers" binds to 694490075Sobrien "shall only declare" or to "objects" - that is, whether all identifiers 694590075Sobrien declared must be identifiers for objects, or whether the restriction 694690075Sobrien only applies to those that are. (A question on this in comp.std.c 694790075Sobrien in November 2000 received no answer.) We implement the strictest 694890075Sobrien interpretation, to avoid creating an extension which later causes 694990075Sobrien problems. */ 695090075Sobrien 6951169699Skan for (b = current_scope->bindings; b; b = b->prev) 695290075Sobrien { 6953169699Skan tree id = b->id; 6954169699Skan tree decl = b->decl; 6955132730Skan 6956169699Skan if (!id) 6957169699Skan continue; 6958169699Skan 6959169699Skan switch (TREE_CODE (decl)) 6960169699Skan { 6961169699Skan case VAR_DECL: 6962169699Skan if (TREE_STATIC (decl)) 6963169699Skan error ("declaration of static variable %q+D in %<for%> loop " 6964169699Skan "initial declaration", decl); 6965169699Skan else if (DECL_EXTERNAL (decl)) 6966169699Skan error ("declaration of %<extern%> variable %q+D in %<for%> loop " 6967169699Skan "initial declaration", decl); 6968169699Skan break; 6969169699Skan 6970169699Skan case RECORD_TYPE: 6971169699Skan error ("%<struct %E%> declared in %<for%> loop initial declaration", 6972169699Skan id); 6973169699Skan break; 6974169699Skan case UNION_TYPE: 6975169699Skan error ("%<union %E%> declared in %<for%> loop initial declaration", 6976169699Skan id); 6977169699Skan break; 6978169699Skan case ENUMERAL_TYPE: 6979169699Skan error ("%<enum %E%> declared in %<for%> loop initial declaration", 6980169699Skan id); 6981169699Skan break; 6982169699Skan default: 6983169699Skan error ("declaration of non-variable %q+D in %<for%> loop " 6984169699Skan "initial declaration", decl); 6985169699Skan } 6986169699Skan 6987169699Skan n_decls++; 6988169699Skan one_decl = decl; 698990075Sobrien } 699090075Sobrien 6991169699Skan return n_decls == 1 ? one_decl : NULL_TREE; 699218334Speter} 699318334Speter 699418334Speter/* Save and reinitialize the variables 699518334Speter used during compilation of a C function. */ 699618334Speter 699718334Spetervoid 6998132730Skanc_push_function_context (struct function *f) 699918334Speter{ 7000117421Skan struct language_function *p; 7001169699Skan p = GGC_NEW (struct language_function); 7002117421Skan f->language = p; 700318334Speter 700490075Sobrien p->base.x_stmt_tree = c_stmt_tree; 7005169699Skan p->x_break_label = c_break_label; 7006169699Skan p->x_cont_label = c_cont_label; 7007169699Skan p->x_switch_stack = c_switch_stack; 7008169699Skan p->arg_info = current_function_arg_info; 700918334Speter p->returns_value = current_function_returns_value; 701018334Speter p->returns_null = current_function_returns_null; 701196263Sobrien p->returns_abnormally = current_function_returns_abnormally; 701218334Speter p->warn_about_return_type = warn_about_return_type; 701318334Speter} 701418334Speter 701518334Speter/* Restore the variables used during compilation of a C function. */ 701618334Speter 701718334Spetervoid 7018132730Skanc_pop_function_context (struct function *f) 701918334Speter{ 7020117421Skan struct language_function *p = f->language; 702118334Speter 7022169699Skan if (DECL_STRUCT_FUNCTION (current_function_decl) == 0 702390075Sobrien && DECL_SAVED_TREE (current_function_decl) == NULL_TREE) 702418334Speter { 702518334Speter /* Stop pointing to the local nodes about to be freed. */ 702618334Speter /* But DECL_INITIAL must remain nonzero so we know this 702718334Speter was an actual function definition. */ 702818334Speter DECL_INITIAL (current_function_decl) = error_mark_node; 702918334Speter DECL_ARGUMENTS (current_function_decl) = 0; 703018334Speter } 703118334Speter 703290075Sobrien c_stmt_tree = p->base.x_stmt_tree; 7033169699Skan c_break_label = p->x_break_label; 7034169699Skan c_cont_label = p->x_cont_label; 7035169699Skan c_switch_stack = p->x_switch_stack; 7036169699Skan current_function_arg_info = p->arg_info; 703718334Speter current_function_returns_value = p->returns_value; 703818334Speter current_function_returns_null = p->returns_null; 703996263Sobrien current_function_returns_abnormally = p->returns_abnormally; 704018334Speter warn_about_return_type = p->warn_about_return_type; 704118334Speter 7042117421Skan f->language = NULL; 704318334Speter} 704418334Speter 7045117421Skan/* Copy the DECL_LANG_SPECIFIC data associated with DECL. */ 704618334Speter 704718334Spetervoid 7048132730Skanc_dup_lang_specific_decl (tree decl) 704990075Sobrien{ 705090075Sobrien struct lang_decl *ld; 705190075Sobrien 705290075Sobrien if (!DECL_LANG_SPECIFIC (decl)) 705390075Sobrien return; 705490075Sobrien 7055169699Skan ld = GGC_NEW (struct lang_decl); 7056132730Skan memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl)); 705790075Sobrien DECL_LANG_SPECIFIC (decl) = ld; 705890075Sobrien} 705990075Sobrien 706090075Sobrien/* The functions below are required for functionality of doing 706190075Sobrien function at once processing in the C front end. Currently these 706290075Sobrien functions are not called from anywhere in the C front end, but as 706390075Sobrien these changes continue, that will change. */ 706490075Sobrien 706590075Sobrien/* Returns the stmt_tree (if any) to which statements are currently 706690075Sobrien being added. If there is no active statement-tree, NULL is 706790075Sobrien returned. */ 706890075Sobrien 706990075Sobrienstmt_tree 7070132730Skancurrent_stmt_tree (void) 707190075Sobrien{ 707290075Sobrien return &c_stmt_tree; 707390075Sobrien} 707490075Sobrien 707590075Sobrien/* Nonzero if TYPE is an anonymous union or struct type. Always 0 in 707690075Sobrien C. */ 707790075Sobrien 707890075Sobrienint 7079169699Skananon_aggr_type_p (tree ARG_UNUSED (node)) 708018334Speter{ 708190075Sobrien return 0; 708218334Speter} 708390075Sobrien 7084132730Skan/* Return the global value of T as a symbol. */ 708590075Sobrien 708690075Sobrientree 7087132730Skanidentifier_global_value (tree t) 708890075Sobrien{ 7089169699Skan struct c_binding *b; 7090132730Skan 7091169699Skan for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed) 7092169699Skan if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b)) 7093169699Skan return b->decl; 7094132730Skan 7095132730Skan return 0; 709690075Sobrien} 709790075Sobrien 709890075Sobrien/* Record a builtin type for C. If NAME is non-NULL, it is the name used; 709990075Sobrien otherwise the name is found in ridpointers from RID_INDEX. */ 710090075Sobrien 710190075Sobrienvoid 7102132730Skanrecord_builtin_type (enum rid rid_index, const char *name, tree type) 710390075Sobrien{ 7104169699Skan tree id, decl; 710590075Sobrien if (name == 0) 710690075Sobrien id = ridpointers[(int) rid_index]; 710790075Sobrien else 710890075Sobrien id = get_identifier (name); 7109169699Skan decl = build_decl (TYPE_DECL, id, type); 7110169699Skan pushdecl (decl); 7111169699Skan if (debug_hooks->type_decl) 7112169699Skan debug_hooks->type_decl (decl, false); 711390075Sobrien} 711490075Sobrien 711590075Sobrien/* Build the void_list_node (void_type_node having been created). */ 711690075Sobrientree 7117132730Skanbuild_void_list_node (void) 711890075Sobrien{ 711990075Sobrien tree t = build_tree_list (NULL_TREE, void_type_node); 712090075Sobrien return t; 712190075Sobrien} 7122117421Skan 7123169699Skan/* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */ 7124117421Skan 7125169699Skanstruct c_parm * 7126169699Skanbuild_c_parm (struct c_declspecs *specs, tree attrs, 7127169699Skan struct c_declarator *declarator) 7128169699Skan{ 7129169699Skan struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm); 7130169699Skan ret->specs = specs; 7131169699Skan ret->attrs = attrs; 7132169699Skan ret->declarator = declarator; 7133169699Skan return ret; 7134169699Skan} 7135117421Skan 7136169699Skan/* Return a declarator with nested attributes. TARGET is the inner 7137169699Skan declarator to which these attributes apply. ATTRS are the 7138169699Skan attributes. */ 7139169699Skan 7140169699Skanstruct c_declarator * 7141169699Skanbuild_attrs_declarator (tree attrs, struct c_declarator *target) 7142117421Skan{ 7143169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7144169699Skan ret->kind = cdk_attrs; 7145169699Skan ret->declarator = target; 7146169699Skan ret->u.attrs = attrs; 7147169699Skan return ret; 7148117421Skan} 7149117421Skan 7150169699Skan/* Return a declarator for a function with arguments specified by ARGS 7151169699Skan and return type specified by TARGET. */ 7152132730Skan 7153169699Skanstruct c_declarator * 7154169699Skanbuild_function_declarator (struct c_arg_info *args, 7155169699Skan struct c_declarator *target) 7156132730Skan{ 7157169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7158169699Skan ret->kind = cdk_function; 7159169699Skan ret->declarator = target; 7160169699Skan ret->u.arg_info = args; 7161169699Skan return ret; 7162132730Skan} 7163132730Skan 7164169699Skan/* Return a declarator for the identifier IDENT (which may be 7165169699Skan NULL_TREE for an abstract declarator). */ 7166132730Skan 7167169699Skanstruct c_declarator * 7168169699Skanbuild_id_declarator (tree ident) 7169132730Skan{ 7170169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7171169699Skan ret->kind = cdk_id; 7172169699Skan ret->declarator = 0; 7173169699Skan ret->u.id = ident; 7174169699Skan /* Default value - may get reset to a more precise location. */ 7175169699Skan ret->id_loc = input_location; 7176169699Skan return ret; 7177132730Skan} 7178132730Skan 7179169699Skan/* Return something to represent absolute declarators containing a *. 7180169699Skan TARGET is the absolute declarator that the * contains. 7181169699Skan TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes 7182169699Skan to apply to the pointer type. */ 7183169699Skan 7184169699Skanstruct c_declarator * 7185169699Skanmake_pointer_declarator (struct c_declspecs *type_quals_attrs, 7186169699Skan struct c_declarator *target) 7187132730Skan{ 7188169699Skan tree attrs; 7189169699Skan int quals = 0; 7190169699Skan struct c_declarator *itarget = target; 7191169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7192169699Skan if (type_quals_attrs) 7193169699Skan { 7194169699Skan attrs = type_quals_attrs->attrs; 7195169699Skan quals = quals_from_declspecs (type_quals_attrs); 7196169699Skan if (attrs != NULL_TREE) 7197169699Skan itarget = build_attrs_declarator (attrs, target); 7198169699Skan } 7199169699Skan ret->kind = cdk_pointer; 7200169699Skan ret->declarator = itarget; 7201169699Skan ret->u.pointer_quals = quals; 7202169699Skan return ret; 7203132730Skan} 7204132730Skan 7205169699Skan/* Return a pointer to a structure for an empty list of declaration 7206169699Skan specifiers. */ 7207132730Skan 7208169699Skanstruct c_declspecs * 7209169699Skanbuild_null_declspecs (void) 7210132730Skan{ 7211169699Skan struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs); 7212169699Skan ret->type = 0; 7213169699Skan ret->decl_attr = 0; 7214169699Skan ret->attrs = 0; 7215169699Skan ret->typespec_word = cts_none; 7216169699Skan ret->storage_class = csc_none; 7217169699Skan ret->declspecs_seen_p = false; 7218169699Skan ret->type_seen_p = false; 7219169699Skan ret->non_sc_seen_p = false; 7220169699Skan ret->typedef_p = false; 7221169699Skan ret->tag_defined_p = false; 7222169699Skan ret->explicit_signed_p = false; 7223169699Skan ret->deprecated_p = false; 7224169699Skan ret->default_int_p = false; 7225169699Skan ret->long_p = false; 7226169699Skan ret->long_long_p = false; 7227169699Skan ret->short_p = false; 7228169699Skan ret->signed_p = false; 7229169699Skan ret->unsigned_p = false; 7230169699Skan ret->complex_p = false; 7231169699Skan ret->inline_p = false; 7232169699Skan ret->thread_p = false; 7233169699Skan ret->const_p = false; 7234169699Skan ret->volatile_p = false; 7235169699Skan ret->restrict_p = false; 7236169699Skan return ret; 7237169699Skan} 7238132730Skan 7239169699Skan/* Add the type qualifier QUAL to the declaration specifiers SPECS, 7240169699Skan returning SPECS. */ 7241132730Skan 7242169699Skanstruct c_declspecs * 7243169699Skandeclspecs_add_qual (struct c_declspecs *specs, tree qual) 7244169699Skan{ 7245169699Skan enum rid i; 7246169699Skan bool dupe = false; 7247169699Skan specs->non_sc_seen_p = true; 7248169699Skan specs->declspecs_seen_p = true; 7249169699Skan gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE 7250169699Skan && C_IS_RESERVED_WORD (qual)); 7251169699Skan i = C_RID_CODE (qual); 7252169699Skan switch (i) 7253169699Skan { 7254169699Skan case RID_CONST: 7255169699Skan dupe = specs->const_p; 7256169699Skan specs->const_p = true; 7257169699Skan break; 7258169699Skan case RID_VOLATILE: 7259169699Skan dupe = specs->volatile_p; 7260169699Skan specs->volatile_p = true; 7261169699Skan break; 7262169699Skan case RID_RESTRICT: 7263169699Skan dupe = specs->restrict_p; 7264169699Skan specs->restrict_p = true; 7265169699Skan break; 7266169699Skan default: 7267169699Skan gcc_unreachable (); 7268169699Skan } 7269169699Skan if (dupe && pedantic && !flag_isoc99) 7270169699Skan pedwarn ("duplicate %qE", qual); 7271169699Skan return specs; 7272169699Skan} 7273132730Skan 7274169699Skan/* Add the type specifier TYPE to the declaration specifiers SPECS, 7275169699Skan returning SPECS. */ 7276132730Skan 7277169699Skanstruct c_declspecs * 7278169699Skandeclspecs_add_type (struct c_declspecs *specs, struct c_typespec spec) 7279169699Skan{ 7280169699Skan tree type = spec.spec; 7281169699Skan specs->non_sc_seen_p = true; 7282169699Skan specs->declspecs_seen_p = true; 7283169699Skan specs->type_seen_p = true; 7284169699Skan if (TREE_DEPRECATED (type)) 7285169699Skan specs->deprecated_p = true; 7286169699Skan 7287169699Skan /* Handle type specifier keywords. */ 7288169699Skan if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type)) 7289169699Skan { 7290169699Skan enum rid i = C_RID_CODE (type); 7291169699Skan if (specs->type) 7292132730Skan { 7293169699Skan error ("two or more data types in declaration specifiers"); 7294169699Skan return specs; 7295169699Skan } 7296169699Skan if ((int) i <= (int) RID_LAST_MODIFIER) 7297169699Skan { 7298169699Skan /* "long", "short", "signed", "unsigned" or "_Complex". */ 7299169699Skan bool dupe = false; 7300169699Skan switch (i) 7301132730Skan { 7302169699Skan case RID_LONG: 7303169699Skan if (specs->long_long_p) 7304132730Skan { 7305169699Skan error ("%<long long long%> is too long for GCC"); 7306169699Skan break; 7307132730Skan } 7308169699Skan if (specs->long_p) 7309132730Skan { 7310169699Skan if (specs->typespec_word == cts_double) 7311169699Skan { 7312169699Skan error ("both %<long long%> and %<double%> in " 7313169699Skan "declaration specifiers"); 7314169699Skan break; 7315169699Skan } 7316169699Skan if (pedantic && !flag_isoc99 && !in_system_header 7317169699Skan && warn_long_long) 7318169699Skan pedwarn ("ISO C90 does not support %<long long%>"); 7319169699Skan specs->long_long_p = 1; 7320169699Skan break; 7321132730Skan } 7322169699Skan if (specs->short_p) 7323169699Skan error ("both %<long%> and %<short%> in " 7324169699Skan "declaration specifiers"); 7325169699Skan else if (specs->typespec_word == cts_void) 7326169699Skan error ("both %<long%> and %<void%> in " 7327169699Skan "declaration specifiers"); 7328169699Skan else if (specs->typespec_word == cts_bool) 7329169699Skan error ("both %<long%> and %<_Bool%> in " 7330169699Skan "declaration specifiers"); 7331169699Skan else if (specs->typespec_word == cts_char) 7332169699Skan error ("both %<long%> and %<char%> in " 7333169699Skan "declaration specifiers"); 7334169699Skan else if (specs->typespec_word == cts_float) 7335169699Skan error ("both %<long%> and %<float%> in " 7336169699Skan "declaration specifiers"); 7337169699Skan else if (specs->typespec_word == cts_dfloat32) 7338169699Skan error ("both %<long%> and %<_Decimal32%> in " 7339169699Skan "declaration specifiers"); 7340169699Skan else if (specs->typespec_word == cts_dfloat64) 7341169699Skan error ("both %<long%> and %<_Decimal64%> in " 7342169699Skan "declaration specifiers"); 7343169699Skan else if (specs->typespec_word == cts_dfloat128) 7344169699Skan error ("both %<long%> and %<_Decimal128%> in " 7345169699Skan "declaration specifiers"); 7346132730Skan else 7347169699Skan specs->long_p = true; 7348169699Skan break; 7349169699Skan case RID_SHORT: 7350169699Skan dupe = specs->short_p; 7351169699Skan if (specs->long_p) 7352169699Skan error ("both %<long%> and %<short%> in " 7353169699Skan "declaration specifiers"); 7354169699Skan else if (specs->typespec_word == cts_void) 7355169699Skan error ("both %<short%> and %<void%> in " 7356169699Skan "declaration specifiers"); 7357169699Skan else if (specs->typespec_word == cts_bool) 7358169699Skan error ("both %<short%> and %<_Bool%> in " 7359169699Skan "declaration specifiers"); 7360169699Skan else if (specs->typespec_word == cts_char) 7361169699Skan error ("both %<short%> and %<char%> in " 7362169699Skan "declaration specifiers"); 7363169699Skan else if (specs->typespec_word == cts_float) 7364169699Skan error ("both %<short%> and %<float%> in " 7365169699Skan "declaration specifiers"); 7366169699Skan else if (specs->typespec_word == cts_double) 7367169699Skan error ("both %<short%> and %<double%> in " 7368169699Skan "declaration specifiers"); 7369169699Skan else if (specs->typespec_word == cts_dfloat32) 7370169699Skan error ("both %<short%> and %<_Decimal32%> in " 7371169699Skan "declaration specifiers"); 7372169699Skan else if (specs->typespec_word == cts_dfloat64) 7373169699Skan error ("both %<short%> and %<_Decimal64%> in " 7374169699Skan "declaration specifiers"); 7375169699Skan else if (specs->typespec_word == cts_dfloat128) 7376169699Skan error ("both %<short%> and %<_Decimal128%> in " 7377169699Skan "declaration specifiers"); 7378169699Skan else 7379169699Skan specs->short_p = true; 7380169699Skan break; 7381169699Skan case RID_SIGNED: 7382169699Skan dupe = specs->signed_p; 7383169699Skan if (specs->unsigned_p) 7384169699Skan error ("both %<signed%> and %<unsigned%> in " 7385169699Skan "declaration specifiers"); 7386169699Skan else if (specs->typespec_word == cts_void) 7387169699Skan error ("both %<signed%> and %<void%> in " 7388169699Skan "declaration specifiers"); 7389169699Skan else if (specs->typespec_word == cts_bool) 7390169699Skan error ("both %<signed%> and %<_Bool%> in " 7391169699Skan "declaration specifiers"); 7392169699Skan else if (specs->typespec_word == cts_float) 7393169699Skan error ("both %<signed%> and %<float%> in " 7394169699Skan "declaration specifiers"); 7395169699Skan else if (specs->typespec_word == cts_double) 7396169699Skan error ("both %<signed%> and %<double%> in " 7397169699Skan "declaration specifiers"); 7398169699Skan else if (specs->typespec_word == cts_dfloat32) 7399169699Skan error ("both %<signed%> and %<_Decimal32%> in " 7400169699Skan "declaration specifiers"); 7401169699Skan else if (specs->typespec_word == cts_dfloat64) 7402169699Skan error ("both %<signed%> and %<_Decimal64%> in " 7403169699Skan "declaration specifiers"); 7404169699Skan else if (specs->typespec_word == cts_dfloat128) 7405169699Skan error ("both %<signed%> and %<_Decimal128%> in " 7406169699Skan "declaration specifiers"); 7407169699Skan else 7408169699Skan specs->signed_p = true; 7409169699Skan break; 7410169699Skan case RID_UNSIGNED: 7411169699Skan dupe = specs->unsigned_p; 7412169699Skan if (specs->signed_p) 7413169699Skan error ("both %<signed%> and %<unsigned%> in " 7414169699Skan "declaration specifiers"); 7415169699Skan else if (specs->typespec_word == cts_void) 7416169699Skan error ("both %<unsigned%> and %<void%> in " 7417169699Skan "declaration specifiers"); 7418169699Skan else if (specs->typespec_word == cts_bool) 7419169699Skan error ("both %<unsigned%> and %<_Bool%> in " 7420169699Skan "declaration specifiers"); 7421169699Skan else if (specs->typespec_word == cts_float) 7422169699Skan error ("both %<unsigned%> and %<float%> in " 7423169699Skan "declaration specifiers"); 7424169699Skan else if (specs->typespec_word == cts_double) 7425169699Skan error ("both %<unsigned%> and %<double%> in " 7426169699Skan "declaration specifiers"); 7427169699Skan else if (specs->typespec_word == cts_dfloat32) 7428169699Skan error ("both %<unsigned%> and %<_Decimal32%> in " 7429169699Skan "declaration specifiers"); 7430169699Skan else if (specs->typespec_word == cts_dfloat64) 7431169699Skan error ("both %<unsigned%> and %<_Decimal64%> in " 7432169699Skan "declaration specifiers"); 7433169699Skan else if (specs->typespec_word == cts_dfloat128) 7434169699Skan error ("both %<unsigned%> and %<_Decimal128%> in " 7435169699Skan "declaration specifiers"); 7436169699Skan else 7437169699Skan specs->unsigned_p = true; 7438169699Skan break; 7439169699Skan case RID_COMPLEX: 7440169699Skan dupe = specs->complex_p; 7441169699Skan if (pedantic && !flag_isoc99 && !in_system_header) 7442169699Skan pedwarn ("ISO C90 does not support complex types"); 7443169699Skan if (specs->typespec_word == cts_void) 7444169699Skan error ("both %<complex%> and %<void%> in " 7445169699Skan "declaration specifiers"); 7446169699Skan else if (specs->typespec_word == cts_bool) 7447169699Skan error ("both %<complex%> and %<_Bool%> in " 7448169699Skan "declaration specifiers"); 7449169699Skan else if (specs->typespec_word == cts_dfloat32) 7450169699Skan error ("both %<complex%> and %<_Decimal32%> in " 7451169699Skan "declaration specifiers"); 7452169699Skan else if (specs->typespec_word == cts_dfloat64) 7453169699Skan error ("both %<complex%> and %<_Decimal64%> in " 7454169699Skan "declaration specifiers"); 7455169699Skan else if (specs->typespec_word == cts_dfloat128) 7456169699Skan error ("both %<complex%> and %<_Decimal128%> in " 7457169699Skan "declaration specifiers"); 7458169699Skan else 7459169699Skan specs->complex_p = true; 7460169699Skan break; 7461169699Skan default: 7462169699Skan gcc_unreachable (); 7463132730Skan } 7464169699Skan 7465169699Skan if (dupe) 7466169699Skan error ("duplicate %qE", type); 7467169699Skan 7468169699Skan return specs; 7469132730Skan } 7470169699Skan else 7471169699Skan { 7472169699Skan /* "void", "_Bool", "char", "int", "float" or "double". */ 7473169699Skan if (specs->typespec_word != cts_none) 7474169699Skan { 7475169699Skan error ("two or more data types in declaration specifiers"); 7476169699Skan return specs; 7477169699Skan } 7478169699Skan switch (i) 7479169699Skan { 7480169699Skan case RID_VOID: 7481169699Skan if (specs->long_p) 7482169699Skan error ("both %<long%> and %<void%> in " 7483169699Skan "declaration specifiers"); 7484169699Skan else if (specs->short_p) 7485169699Skan error ("both %<short%> and %<void%> in " 7486169699Skan "declaration specifiers"); 7487169699Skan else if (specs->signed_p) 7488169699Skan error ("both %<signed%> and %<void%> in " 7489169699Skan "declaration specifiers"); 7490169699Skan else if (specs->unsigned_p) 7491169699Skan error ("both %<unsigned%> and %<void%> in " 7492169699Skan "declaration specifiers"); 7493169699Skan else if (specs->complex_p) 7494169699Skan error ("both %<complex%> and %<void%> in " 7495169699Skan "declaration specifiers"); 7496169699Skan else 7497169699Skan specs->typespec_word = cts_void; 7498169699Skan return specs; 7499169699Skan case RID_BOOL: 7500169699Skan if (specs->long_p) 7501169699Skan error ("both %<long%> and %<_Bool%> in " 7502169699Skan "declaration specifiers"); 7503169699Skan else if (specs->short_p) 7504169699Skan error ("both %<short%> and %<_Bool%> in " 7505169699Skan "declaration specifiers"); 7506169699Skan else if (specs->signed_p) 7507169699Skan error ("both %<signed%> and %<_Bool%> in " 7508169699Skan "declaration specifiers"); 7509169699Skan else if (specs->unsigned_p) 7510169699Skan error ("both %<unsigned%> and %<_Bool%> in " 7511169699Skan "declaration specifiers"); 7512169699Skan else if (specs->complex_p) 7513169699Skan error ("both %<complex%> and %<_Bool%> in " 7514169699Skan "declaration specifiers"); 7515169699Skan else 7516169699Skan specs->typespec_word = cts_bool; 7517169699Skan return specs; 7518169699Skan case RID_CHAR: 7519169699Skan if (specs->long_p) 7520169699Skan error ("both %<long%> and %<char%> in " 7521169699Skan "declaration specifiers"); 7522169699Skan else if (specs->short_p) 7523169699Skan error ("both %<short%> and %<char%> in " 7524169699Skan "declaration specifiers"); 7525169699Skan else 7526169699Skan specs->typespec_word = cts_char; 7527169699Skan return specs; 7528169699Skan case RID_INT: 7529169699Skan specs->typespec_word = cts_int; 7530169699Skan return specs; 7531169699Skan case RID_FLOAT: 7532169699Skan if (specs->long_p) 7533169699Skan error ("both %<long%> and %<float%> in " 7534169699Skan "declaration specifiers"); 7535169699Skan else if (specs->short_p) 7536169699Skan error ("both %<short%> and %<float%> in " 7537169699Skan "declaration specifiers"); 7538169699Skan else if (specs->signed_p) 7539169699Skan error ("both %<signed%> and %<float%> in " 7540169699Skan "declaration specifiers"); 7541169699Skan else if (specs->unsigned_p) 7542169699Skan error ("both %<unsigned%> and %<float%> in " 7543169699Skan "declaration specifiers"); 7544169699Skan else 7545169699Skan specs->typespec_word = cts_float; 7546169699Skan return specs; 7547169699Skan case RID_DOUBLE: 7548169699Skan if (specs->long_long_p) 7549169699Skan error ("both %<long long%> and %<double%> in " 7550169699Skan "declaration specifiers"); 7551169699Skan else if (specs->short_p) 7552169699Skan error ("both %<short%> and %<double%> in " 7553169699Skan "declaration specifiers"); 7554169699Skan else if (specs->signed_p) 7555169699Skan error ("both %<signed%> and %<double%> in " 7556169699Skan "declaration specifiers"); 7557169699Skan else if (specs->unsigned_p) 7558169699Skan error ("both %<unsigned%> and %<double%> in " 7559169699Skan "declaration specifiers"); 7560169699Skan else 7561169699Skan specs->typespec_word = cts_double; 7562169699Skan return specs; 7563169699Skan case RID_DFLOAT32: 7564169699Skan case RID_DFLOAT64: 7565169699Skan case RID_DFLOAT128: 7566169699Skan { 7567169699Skan const char *str; 7568169699Skan if (i == RID_DFLOAT32) 7569169699Skan str = "_Decimal32"; 7570169699Skan else if (i == RID_DFLOAT64) 7571169699Skan str = "_Decimal64"; 7572169699Skan else 7573169699Skan str = "_Decimal128"; 7574169699Skan if (specs->long_long_p) 7575169699Skan error ("both %<long long%> and %<%s%> in " 7576169699Skan "declaration specifiers", str); 7577169699Skan if (specs->long_p) 7578169699Skan error ("both %<long%> and %<%s%> in " 7579169699Skan "declaration specifiers", str); 7580169699Skan else if (specs->short_p) 7581169699Skan error ("both %<short%> and %<%s%> in " 7582169699Skan "declaration specifiers", str); 7583169699Skan else if (specs->signed_p) 7584169699Skan error ("both %<signed%> and %<%s%> in " 7585169699Skan "declaration specifiers", str); 7586169699Skan else if (specs->unsigned_p) 7587169699Skan error ("both %<unsigned%> and %<%s%> in " 7588169699Skan "declaration specifiers", str); 7589169699Skan else if (specs->complex_p) 7590169699Skan error ("both %<complex%> and %<%s%> in " 7591169699Skan "declaration specifiers", str); 7592169699Skan else if (i == RID_DFLOAT32) 7593169699Skan specs->typespec_word = cts_dfloat32; 7594169699Skan else if (i == RID_DFLOAT64) 7595169699Skan specs->typespec_word = cts_dfloat64; 7596169699Skan else 7597169699Skan specs->typespec_word = cts_dfloat128; 7598169699Skan } 7599169699Skan if (!targetm.decimal_float_supported_p ()) 7600169699Skan error ("decimal floating point not supported for this target"); 7601169699Skan if (pedantic) 7602169699Skan pedwarn ("ISO C does not support decimal floating point"); 7603169699Skan return specs; 7604169699Skan default: 7605169699Skan /* ObjC reserved word "id", handled below. */ 7606169699Skan break; 7607169699Skan } 7608169699Skan } 7609169699Skan } 7610132730Skan 7611169699Skan /* Now we have a typedef (a TYPE_DECL node), an identifier (some 7612169699Skan form of ObjC type, cases such as "int" and "long" being handled 7613169699Skan above), a TYPE (struct, union, enum and typeof specifiers) or an 7614169699Skan ERROR_MARK. In none of these cases may there have previously 7615169699Skan been any type specifiers. */ 7616169699Skan if (specs->type || specs->typespec_word != cts_none 7617169699Skan || specs->long_p || specs->short_p || specs->signed_p 7618169699Skan || specs->unsigned_p || specs->complex_p) 7619169699Skan error ("two or more data types in declaration specifiers"); 7620169699Skan else if (TREE_CODE (type) == TYPE_DECL) 7621169699Skan { 7622169699Skan if (TREE_TYPE (type) == error_mark_node) 7623169699Skan ; /* Allow the type to default to int to avoid cascading errors. */ 7624169699Skan else 7625132730Skan { 7626169699Skan specs->type = TREE_TYPE (type); 7627169699Skan specs->decl_attr = DECL_ATTRIBUTES (type); 7628169699Skan specs->typedef_p = true; 7629169699Skan specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type); 7630169699Skan } 7631169699Skan } 7632169699Skan else if (TREE_CODE (type) == IDENTIFIER_NODE) 7633169699Skan { 7634169699Skan tree t = lookup_name (type); 7635169699Skan if (!t || TREE_CODE (t) != TYPE_DECL) 7636169699Skan error ("%qE fails to be a typedef or built in type", type); 7637169699Skan else if (TREE_TYPE (t) == error_mark_node) 7638169699Skan ; 7639169699Skan else 7640169699Skan specs->type = TREE_TYPE (t); 7641169699Skan } 7642169699Skan else if (TREE_CODE (type) != ERROR_MARK) 7643169699Skan { 7644169699Skan if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref) 7645169699Skan specs->tag_defined_p = true; 7646169699Skan if (spec.kind == ctsk_typeof) 7647169699Skan specs->typedef_p = true; 7648169699Skan specs->type = type; 7649169699Skan } 7650132730Skan 7651169699Skan return specs; 7652169699Skan} 7653132730Skan 7654169699Skan/* Add the storage class specifier or function specifier SCSPEC to the 7655169699Skan declaration specifiers SPECS, returning SPECS. */ 7656169699Skan 7657169699Skanstruct c_declspecs * 7658169699Skandeclspecs_add_scspec (struct c_declspecs *specs, tree scspec) 7659169699Skan{ 7660169699Skan enum rid i; 7661169699Skan enum c_storage_class n = csc_none; 7662169699Skan bool dupe = false; 7663169699Skan specs->declspecs_seen_p = true; 7664169699Skan gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE 7665169699Skan && C_IS_RESERVED_WORD (scspec)); 7666169699Skan i = C_RID_CODE (scspec); 7667169699Skan if (extra_warnings && specs->non_sc_seen_p) 7668169699Skan warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec); 7669169699Skan switch (i) 7670169699Skan { 7671169699Skan case RID_INLINE: 7672169699Skan /* C99 permits duplicate inline. Although of doubtful utility, 7673169699Skan it seems simplest to permit it in gnu89 mode as well, as 7674169699Skan there is also little utility in maintaining this as a 7675169699Skan difference between gnu89 and C99 inline. */ 7676169699Skan dupe = false; 7677169699Skan specs->inline_p = true; 7678169699Skan break; 7679169699Skan case RID_THREAD: 7680169699Skan dupe = specs->thread_p; 7681169699Skan if (specs->storage_class == csc_auto) 7682169699Skan error ("%<__thread%> used with %<auto%>"); 7683169699Skan else if (specs->storage_class == csc_register) 7684169699Skan error ("%<__thread%> used with %<register%>"); 7685169699Skan else if (specs->storage_class == csc_typedef) 7686169699Skan error ("%<__thread%> used with %<typedef%>"); 7687169699Skan else 7688169699Skan specs->thread_p = true; 7689169699Skan break; 7690169699Skan case RID_AUTO: 7691169699Skan n = csc_auto; 7692169699Skan break; 7693169699Skan case RID_EXTERN: 7694169699Skan n = csc_extern; 7695169699Skan /* Diagnose "__thread extern". */ 7696169699Skan if (specs->thread_p) 7697169699Skan error ("%<__thread%> before %<extern%>"); 7698169699Skan break; 7699169699Skan case RID_REGISTER: 7700169699Skan n = csc_register; 7701169699Skan break; 7702169699Skan case RID_STATIC: 7703169699Skan n = csc_static; 7704169699Skan /* Diagnose "__thread static". */ 7705169699Skan if (specs->thread_p) 7706169699Skan error ("%<__thread%> before %<static%>"); 7707169699Skan break; 7708169699Skan case RID_TYPEDEF: 7709169699Skan n = csc_typedef; 7710169699Skan break; 7711169699Skan default: 7712169699Skan gcc_unreachable (); 7713169699Skan } 7714169699Skan if (n != csc_none && n == specs->storage_class) 7715169699Skan dupe = true; 7716169699Skan if (dupe) 7717169699Skan error ("duplicate %qE", scspec); 7718169699Skan if (n != csc_none) 7719169699Skan { 7720169699Skan if (specs->storage_class != csc_none && n != specs->storage_class) 7721169699Skan { 7722169699Skan error ("multiple storage classes in declaration specifiers"); 7723132730Skan } 7724169699Skan else 7725169699Skan { 7726169699Skan specs->storage_class = n; 7727169699Skan if (n != csc_extern && n != csc_static && specs->thread_p) 7728169699Skan { 7729169699Skan error ("%<__thread%> used with %qE", scspec); 7730169699Skan specs->thread_p = false; 7731169699Skan } 7732169699Skan } 7733169699Skan } 7734169699Skan return specs; 7735169699Skan} 7736132730Skan 7737169699Skan/* Add the attributes ATTRS to the declaration specifiers SPECS, 7738169699Skan returning SPECS. */ 7739169699Skan 7740169699Skanstruct c_declspecs * 7741169699Skandeclspecs_add_attrs (struct c_declspecs *specs, tree attrs) 7742169699Skan{ 7743169699Skan specs->attrs = chainon (attrs, specs->attrs); 7744169699Skan specs->declspecs_seen_p = true; 7745169699Skan return specs; 7746132730Skan} 7747132730Skan 7748169699Skan/* Combine "long", "short", "signed", "unsigned" and "_Complex" type 7749169699Skan specifiers with any other type specifier to determine the resulting 7750169699Skan type. This is where ISO C checks on complex types are made, since 7751169699Skan "_Complex long" is a prefix of the valid ISO C type "_Complex long 7752169699Skan double". */ 7753132730Skan 7754169699Skanstruct c_declspecs * 7755169699Skanfinish_declspecs (struct c_declspecs *specs) 7756132730Skan{ 7757169699Skan /* If a type was specified as a whole, we have no modifiers and are 7758169699Skan done. */ 7759169699Skan if (specs->type != NULL_TREE) 7760169699Skan { 7761169699Skan gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p 7762169699Skan && !specs->signed_p && !specs->unsigned_p 7763169699Skan && !specs->complex_p); 7764169699Skan return specs; 7765169699Skan } 7766132730Skan 7767169699Skan /* If none of "void", "_Bool", "char", "int", "float" or "double" 7768169699Skan has been specified, treat it as "int" unless "_Complex" is 7769169699Skan present and there are no other specifiers. If we just have 7770169699Skan "_Complex", it is equivalent to "_Complex double", but e.g. 7771169699Skan "_Complex short" is equivalent to "_Complex short int". */ 7772169699Skan if (specs->typespec_word == cts_none) 7773132730Skan { 7774169699Skan if (specs->long_p || specs->short_p 7775169699Skan || specs->signed_p || specs->unsigned_p) 7776169699Skan { 7777169699Skan specs->typespec_word = cts_int; 7778169699Skan } 7779169699Skan else if (specs->complex_p) 7780169699Skan { 7781169699Skan specs->typespec_word = cts_double; 7782169699Skan if (pedantic) 7783169699Skan pedwarn ("ISO C does not support plain %<complex%> meaning " 7784169699Skan "%<double complex%>"); 7785169699Skan } 7786169699Skan else 7787169699Skan { 7788169699Skan specs->typespec_word = cts_int; 7789169699Skan specs->default_int_p = true; 7790169699Skan /* We don't diagnose this here because grokdeclarator will 7791169699Skan give more specific diagnostics according to whether it is 7792169699Skan a function definition. */ 7793169699Skan } 7794169699Skan } 7795132730Skan 7796169699Skan /* If "signed" was specified, record this to distinguish "int" and 7797169699Skan "signed int" in the case of a bit-field with 7798169699Skan -funsigned-bitfields. */ 7799169699Skan specs->explicit_signed_p = specs->signed_p; 7800132730Skan 7801169699Skan /* Now compute the actual type. */ 7802169699Skan switch (specs->typespec_word) 7803169699Skan { 7804169699Skan case cts_void: 7805169699Skan gcc_assert (!specs->long_p && !specs->short_p 7806169699Skan && !specs->signed_p && !specs->unsigned_p 7807169699Skan && !specs->complex_p); 7808169699Skan specs->type = void_type_node; 7809169699Skan break; 7810169699Skan case cts_bool: 7811169699Skan gcc_assert (!specs->long_p && !specs->short_p 7812169699Skan && !specs->signed_p && !specs->unsigned_p 7813169699Skan && !specs->complex_p); 7814169699Skan specs->type = boolean_type_node; 7815169699Skan break; 7816169699Skan case cts_char: 7817169699Skan gcc_assert (!specs->long_p && !specs->short_p); 7818169699Skan gcc_assert (!(specs->signed_p && specs->unsigned_p)); 7819169699Skan if (specs->signed_p) 7820169699Skan specs->type = signed_char_type_node; 7821169699Skan else if (specs->unsigned_p) 7822169699Skan specs->type = unsigned_char_type_node; 7823169699Skan else 7824169699Skan specs->type = char_type_node; 7825169699Skan if (specs->complex_p) 7826169699Skan { 7827169699Skan if (pedantic) 7828169699Skan pedwarn ("ISO C does not support complex integer types"); 7829169699Skan specs->type = build_complex_type (specs->type); 7830169699Skan } 7831169699Skan break; 7832169699Skan case cts_int: 7833169699Skan gcc_assert (!(specs->long_p && specs->short_p)); 7834169699Skan gcc_assert (!(specs->signed_p && specs->unsigned_p)); 7835169699Skan if (specs->long_long_p) 7836169699Skan specs->type = (specs->unsigned_p 7837169699Skan ? long_long_unsigned_type_node 7838169699Skan : long_long_integer_type_node); 7839169699Skan else if (specs->long_p) 7840169699Skan specs->type = (specs->unsigned_p 7841169699Skan ? long_unsigned_type_node 7842169699Skan : long_integer_type_node); 7843169699Skan else if (specs->short_p) 7844169699Skan specs->type = (specs->unsigned_p 7845169699Skan ? short_unsigned_type_node 7846169699Skan : short_integer_type_node); 7847169699Skan else 7848169699Skan specs->type = (specs->unsigned_p 7849169699Skan ? unsigned_type_node 7850169699Skan : integer_type_node); 7851169699Skan if (specs->complex_p) 7852169699Skan { 7853169699Skan if (pedantic) 7854169699Skan pedwarn ("ISO C does not support complex integer types"); 7855169699Skan specs->type = build_complex_type (specs->type); 7856169699Skan } 7857169699Skan break; 7858169699Skan case cts_float: 7859169699Skan gcc_assert (!specs->long_p && !specs->short_p 7860169699Skan && !specs->signed_p && !specs->unsigned_p); 7861169699Skan specs->type = (specs->complex_p 7862169699Skan ? complex_float_type_node 7863169699Skan : float_type_node); 7864169699Skan break; 7865169699Skan case cts_double: 7866169699Skan gcc_assert (!specs->long_long_p && !specs->short_p 7867169699Skan && !specs->signed_p && !specs->unsigned_p); 7868169699Skan if (specs->long_p) 7869169699Skan { 7870169699Skan specs->type = (specs->complex_p 7871169699Skan ? complex_long_double_type_node 7872169699Skan : long_double_type_node); 7873169699Skan } 7874169699Skan else 7875169699Skan { 7876169699Skan specs->type = (specs->complex_p 7877169699Skan ? complex_double_type_node 7878169699Skan : double_type_node); 7879169699Skan } 7880169699Skan break; 7881169699Skan case cts_dfloat32: 7882169699Skan case cts_dfloat64: 7883169699Skan case cts_dfloat128: 7884169699Skan gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p 7885169699Skan && !specs->signed_p && !specs->unsigned_p && !specs->complex_p); 7886169699Skan if (specs->typespec_word == cts_dfloat32) 7887169699Skan specs->type = dfloat32_type_node; 7888169699Skan else if (specs->typespec_word == cts_dfloat64) 7889169699Skan specs->type = dfloat64_type_node; 7890169699Skan else 7891169699Skan specs->type = dfloat128_type_node; 7892169699Skan break; 7893169699Skan default: 7894169699Skan gcc_unreachable (); 7895169699Skan } 7896132730Skan 7897169699Skan return specs; 7898169699Skan} 7899132730Skan 7900169699Skan/* Synthesize a function which calls all the global ctors or global 7901169699Skan dtors in this file. This is only used for targets which do not 7902169699Skan support .ctors/.dtors sections. FIXME: Migrate into cgraph. */ 7903169699Skanstatic void 7904169699Skanbuild_cdtor (int method_type, tree cdtors) 7905169699Skan{ 7906169699Skan tree body = 0; 7907132730Skan 7908169699Skan if (!cdtors) 7909169699Skan return; 7910169699Skan 7911169699Skan for (; cdtors; cdtors = TREE_CHAIN (cdtors)) 7912169699Skan append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0), 7913169699Skan &body); 7914169699Skan 7915169699Skan cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY); 7916169699Skan} 7917169699Skan 7918169699Skan/* A subroutine of c_write_global_declarations. Perform final processing 7919169699Skan on one file scope's declarations (or the external scope's declarations), 7920169699Skan GLOBALS. */ 7921169699Skan 7922169699Skanstatic void 7923169699Skanc_write_global_declarations_1 (tree globals) 7924169699Skan{ 7925169699Skan tree decl; 7926169699Skan bool reconsider; 7927169699Skan 7928169699Skan /* Process the decls in the order they were written. */ 7929169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 7930169699Skan { 7931169699Skan /* Check for used but undefined static functions using the C 7932169699Skan standard's definition of "used", and set TREE_NO_WARNING so 7933169699Skan that check_global_declarations doesn't repeat the check. */ 7934169699Skan if (TREE_CODE (decl) == FUNCTION_DECL 7935169699Skan && DECL_INITIAL (decl) == 0 7936169699Skan && DECL_EXTERNAL (decl) 7937169699Skan && !TREE_PUBLIC (decl) 7938169699Skan && C_DECL_USED (decl)) 7939169699Skan { 7940169699Skan pedwarn ("%q+F used but never defined", decl); 7941169699Skan TREE_NO_WARNING (decl) = 1; 7942169699Skan } 7943169699Skan 7944169699Skan wrapup_global_declaration_1 (decl); 7945132730Skan } 7946169699Skan 7947169699Skan do 7948169699Skan { 7949169699Skan reconsider = false; 7950169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 7951169699Skan reconsider |= wrapup_global_declaration_2 (decl); 7952169699Skan } 7953169699Skan while (reconsider); 7954169699Skan 7955169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 7956169699Skan check_global_declaration_1 (decl); 7957132730Skan} 7958132730Skan 7959169699Skan/* A subroutine of c_write_global_declarations Emit debug information for each 7960169699Skan of the declarations in GLOBALS. */ 7961132730Skan 7962169699Skanstatic void 7963169699Skanc_write_global_declarations_2 (tree globals) 7964169699Skan{ 7965169699Skan tree decl; 7966169699Skan 7967169699Skan for (decl = globals; decl ; decl = TREE_CHAIN (decl)) 7968169699Skan debug_hooks->global_decl (decl); 7969169699Skan} 7970169699Skan 7971169699Skan/* Preserve the external declarations scope across a garbage collect. */ 7972169699Skanstatic GTY(()) tree ext_block; 7973169699Skan 7974132730Skanvoid 7975169699Skanc_write_global_declarations (void) 7976132730Skan{ 7977169699Skan tree t; 7978132730Skan 7979169699Skan /* We don't want to do this if generating a PCH. */ 7980169699Skan if (pch_file) 7981169699Skan return; 7982132730Skan 7983169699Skan /* Don't waste time on further processing if -fsyntax-only or we've 7984169699Skan encountered errors. */ 7985169699Skan if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in)) 7986169699Skan return; 7987132730Skan 7988169699Skan /* Close the external scope. */ 7989169699Skan ext_block = pop_scope (); 7990169699Skan external_scope = 0; 7991169699Skan gcc_assert (!current_scope); 7992169699Skan 7993169699Skan if (ext_block) 7994169699Skan { 7995169699Skan tree tmp = BLOCK_VARS (ext_block); 7996169699Skan int flags; 7997169699Skan FILE * stream = dump_begin (TDI_tu, &flags); 7998169699Skan if (stream && tmp) 7999169699Skan { 8000169699Skan dump_node (tmp, flags & ~TDF_SLIM, stream); 8001169699Skan dump_end (TDI_tu, stream); 8002169699Skan } 8003169699Skan } 8004169699Skan 8005169699Skan /* Process all file scopes in this compilation, and the external_scope, 8006169699Skan through wrapup_global_declarations and check_global_declarations. */ 8007169699Skan for (t = all_translation_units; t; t = TREE_CHAIN (t)) 8008169699Skan c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t))); 8009169699Skan c_write_global_declarations_1 (BLOCK_VARS (ext_block)); 8010169699Skan 8011169699Skan /* Generate functions to call static constructors and destructors 8012169699Skan for targets that do not support .ctors/.dtors sections. These 8013169699Skan functions have magic names which are detected by collect2. */ 8014169699Skan build_cdtor ('I', static_ctors); static_ctors = 0; 8015169699Skan build_cdtor ('D', static_dtors); static_dtors = 0; 8016169699Skan 8017169699Skan /* We're done parsing; proceed to optimize and emit assembly. 8018169699Skan FIXME: shouldn't be the front end's responsibility to call this. */ 8019169699Skan cgraph_optimize (); 8020169699Skan 8021169699Skan /* After cgraph has had a chance to emit everything that's going to 8022169699Skan be emitted, output debug information for globals. */ 8023169699Skan if (errorcount == 0 && sorrycount == 0) 8024169699Skan { 8025169699Skan timevar_push (TV_SYMOUT); 8026169699Skan for (t = all_translation_units; t; t = TREE_CHAIN (t)) 8027169699Skan c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t))); 8028169699Skan c_write_global_declarations_2 (BLOCK_VARS (ext_block)); 8029169699Skan timevar_pop (TV_SYMOUT); 8030169699Skan } 8031169699Skan 8032169699Skan ext_block = NULL; 8033132730Skan} 8034132730Skan 8035117421Skan#include "gt-c-decl.h" 8036