c-decl.c revision 223715
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 223715 2011-07-01 20:45:55Z uqs $ */ 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 } 4343223715Suqs if (warn_variable_decl) 4344223715Suqs warning (0, "variable-sized array %qs", name); 4345169699Skan } 434618334Speter 4347169699Skan if (integer_zerop (size)) 4348169699Skan { 4349169699Skan /* A zero-length array cannot be represented with 4350169699Skan an unsigned index type, which is what we'll 4351169699Skan get with build_index_type. Create an 4352169699Skan open-ended range instead. */ 4353169699Skan itype = build_range_type (sizetype, size, NULL_TREE); 4354169699Skan } 4355169699Skan else 4356169699Skan { 4357169699Skan /* Arrange for the SAVE_EXPR on the inside of the 4358169699Skan MINUS_EXPR, which allows the -1 to get folded 4359169699Skan with the +1 that happens when building TYPE_SIZE. */ 4360169699Skan if (size_varies) 4361169699Skan size = variable_size (size); 436218334Speter 4363169699Skan /* Compute the maximum valid index, that is, size 4364169699Skan - 1. Do the calculation in index_type, so that 4365169699Skan if it is a variable the computations will be 4366169699Skan done in the proper mode. */ 4367169699Skan itype = fold_build2 (MINUS_EXPR, index_type, 4368169699Skan convert (index_type, size), 4369169699Skan convert (index_type, 4370169699Skan size_one_node)); 437118334Speter 4372169699Skan /* If that overflowed, the array is too big. ??? 4373169699Skan While a size of INT_MAX+1 technically shouldn't 4374169699Skan cause an overflow (because we subtract 1), the 4375169699Skan overflow is recorded during the conversion to 4376169699Skan index_type, before the subtraction. Handling 4377169699Skan this case seems like an unnecessary 4378169699Skan complication. */ 4379169699Skan if (TREE_CODE (itype) == INTEGER_CST 4380169699Skan && TREE_OVERFLOW (itype)) 4381169699Skan { 4382169699Skan error ("size of array %qs is too large", name); 4383169699Skan type = error_mark_node; 4384169699Skan continue; 4385169699Skan } 438650397Sobrien 4387169699Skan itype = build_index_type (itype); 4388169699Skan } 4389169699Skan } 4390169699Skan else if (decl_context == FIELD) 4391169699Skan { 4392169699Skan if (pedantic && !flag_isoc99 && !in_system_header) 4393169699Skan pedwarn ("ISO C90 does not support flexible array members"); 439490075Sobrien 4395169699Skan /* ISO C99 Flexible array members are effectively 4396169699Skan identical to GCC's zero-length array extension. */ 4397169699Skan itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 4398169699Skan } 4399169699Skan else if (decl_context == PARM) 4400169699Skan { 4401169699Skan if (array_parm_vla_unspec_p) 4402169699Skan { 4403169699Skan if (! orig_name) 4404169699Skan { 4405169699Skan /* C99 6.7.5.2p4 */ 4406169699Skan error ("%<[*]%> not allowed in other than a declaration"); 4407169699Skan } 440818334Speter 4409169699Skan itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 4410169699Skan size_varies = 1; 4411169699Skan } 4412169699Skan } 4413169699Skan else if (decl_context == TYPENAME) 4414169699Skan { 4415169699Skan if (array_parm_vla_unspec_p) 4416169699Skan { 4417169699Skan /* The error is printed elsewhere. We use this to 4418169699Skan avoid messing up with incomplete array types of 4419169699Skan the same type, that would otherwise be modified 4420169699Skan below. */ 4421169699Skan itype = build_range_type (sizetype, size_zero_node, 4422169699Skan NULL_TREE); 4423169699Skan } 4424169699Skan } 442518334Speter 4426169699Skan /* Complain about arrays of incomplete types. */ 4427169699Skan if (!COMPLETE_TYPE_P (type)) 4428169699Skan { 4429169699Skan error ("array type has incomplete element type"); 4430169699Skan type = error_mark_node; 4431169699Skan } 4432169699Skan else 4433169699Skan /* When itype is NULL, a shared incomplete array type is 4434169699Skan returned for all array of a given type. Elsewhere we 4435169699Skan make sure we don't complete that type before copying 4436169699Skan it, but here we want to make sure we don't ever 4437169699Skan modify the shared type, so we gcc_assert (itype) 4438169699Skan below. */ 4439169699Skan type = build_array_type (type, itype); 444090075Sobrien 4441169699Skan if (type != error_mark_node) 4442169699Skan { 4443169699Skan if (size_varies) 4444169699Skan { 4445169699Skan /* It is ok to modify type here even if itype is 4446169699Skan NULL: if size_varies, we're in a 4447169699Skan multi-dimensional array and the inner type has 4448169699Skan variable size, so the enclosing shared array type 4449169699Skan must too. */ 4450169699Skan if (size && TREE_CODE (size) == INTEGER_CST) 4451169699Skan type 4452169699Skan = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); 4453169699Skan C_TYPE_VARIABLE_SIZE (type) = 1; 4454169699Skan } 445590075Sobrien 4456169699Skan /* The GCC extension for zero-length arrays differs from 4457169699Skan ISO flexible array members in that sizeof yields 4458169699Skan zero. */ 4459169699Skan if (size && integer_zerop (size)) 4460169699Skan { 4461169699Skan gcc_assert (itype); 4462169699Skan TYPE_SIZE (type) = bitsize_zero_node; 4463169699Skan TYPE_SIZE_UNIT (type) = size_zero_node; 4464169699Skan } 4465169699Skan if (array_parm_vla_unspec_p) 4466169699Skan { 4467169699Skan gcc_assert (itype); 4468169699Skan /* The type is complete. C99 6.7.5.2p4 */ 4469169699Skan TYPE_SIZE (type) = bitsize_zero_node; 4470169699Skan TYPE_SIZE_UNIT (type) = size_zero_node; 4471169699Skan } 4472169699Skan } 447318334Speter 4474169699Skan if (decl_context != PARM 4475169699Skan && (array_ptr_quals != TYPE_UNQUALIFIED 4476169699Skan || array_ptr_attrs != NULL_TREE 4477169699Skan || array_parm_static)) 4478169699Skan { 4479169699Skan error ("static or type qualifiers in non-parameter array declarator"); 4480169699Skan array_ptr_quals = TYPE_UNQUALIFIED; 4481169699Skan array_ptr_attrs = NULL_TREE; 4482169699Skan array_parm_static = 0; 4483169699Skan } 4484169699Skan break; 4485169699Skan } 4486169699Skan case cdk_function: 4487169699Skan { 4488169699Skan /* Say it's a definition only for the declarator closest 4489169699Skan to the identifier, apart possibly from some 4490169699Skan attributes. */ 4491169699Skan bool really_funcdef = false; 4492169699Skan tree arg_types; 4493169699Skan if (funcdef_flag) 4494169699Skan { 4495169699Skan const struct c_declarator *t = declarator->declarator; 4496169699Skan while (t->kind == cdk_attrs) 4497169699Skan t = t->declarator; 4498169699Skan really_funcdef = (t->kind == cdk_id); 4499169699Skan } 450018334Speter 4501169699Skan /* Declaring a function type. Make sure we have a valid 4502169699Skan type for the function to return. */ 4503169699Skan if (type == error_mark_node) 4504169699Skan continue; 450590075Sobrien 4506169699Skan size_varies = 0; 450718334Speter 4508169699Skan /* Warn about some types functions can't return. */ 4509169699Skan if (TREE_CODE (type) == FUNCTION_TYPE) 4510169699Skan { 4511169699Skan error ("%qs declared as function returning a function", name); 4512169699Skan type = integer_type_node; 4513169699Skan } 4514169699Skan if (TREE_CODE (type) == ARRAY_TYPE) 4515169699Skan { 4516169699Skan error ("%qs declared as function returning an array", name); 4517169699Skan type = integer_type_node; 4518169699Skan } 451918334Speter 4520169699Skan /* Construct the function type and go to the next 4521169699Skan inner layer of declarator. */ 4522169699Skan arg_info = declarator->u.arg_info; 4523169699Skan arg_types = grokparms (arg_info, really_funcdef); 4524169699Skan if (really_funcdef) 4525169699Skan put_pending_sizes (arg_info->pending_sizes); 452618334Speter 4527169699Skan /* Type qualifiers before the return type of the function 4528169699Skan qualify the return type, not the function type. */ 4529169699Skan if (type_quals) 4530169699Skan { 4531169699Skan /* Type qualifiers on a function return type are 4532169699Skan normally permitted by the standard but have no 4533169699Skan effect, so give a warning at -Wreturn-type. 4534169699Skan Qualifiers on a void return type are banned on 4535169699Skan function definitions in ISO C; GCC used to used 4536169699Skan them for noreturn functions. */ 4537169699Skan if (VOID_TYPE_P (type) && really_funcdef) 4538169699Skan pedwarn ("function definition has qualified void return type"); 4539169699Skan else 4540169699Skan warning (OPT_Wreturn_type, 4541169699Skan "type qualifiers ignored on function return type"); 454218334Speter 4543169699Skan type = c_build_qualified_type (type, type_quals); 4544169699Skan } 4545169699Skan type_quals = TYPE_UNQUALIFIED; 454618334Speter 4547169699Skan type = build_function_type (type, arg_types); 4548169699Skan declarator = declarator->declarator; 454918334Speter 4550169699Skan /* Set the TYPE_CONTEXTs for each tagged type which is local to 4551169699Skan the formal parameter list of this FUNCTION_TYPE to point to 4552169699Skan the FUNCTION_TYPE node itself. */ 455390075Sobrien { 4554169699Skan tree link; 455590075Sobrien 4556169699Skan for (link = arg_info->tags; 4557169699Skan link; 4558169699Skan link = TREE_CHAIN (link)) 4559169699Skan TYPE_CONTEXT (TREE_VALUE (link)) = type; 456090075Sobrien } 4561169699Skan break; 4562169699Skan } 4563169699Skan case cdk_pointer: 456418334Speter { 4565169699Skan /* Merge any constancy or volatility into the target type 4566169699Skan for the pointer. */ 456718334Speter 4568169699Skan if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 4569169699Skan && type_quals) 4570169699Skan pedwarn ("ISO C forbids qualified function types"); 4571169699Skan if (type_quals) 4572169699Skan type = c_build_qualified_type (type, type_quals); 4573169699Skan size_varies = 0; 457418334Speter 4575169699Skan /* When the pointed-to type involves components of variable size, 4576169699Skan care must be taken to ensure that the size evaluation code is 4577169699Skan emitted early enough to dominate all the possible later uses 4578169699Skan and late enough for the variables on which it depends to have 4579169699Skan been assigned. 458018334Speter 4581169699Skan This is expected to happen automatically when the pointed-to 4582169699Skan type has a name/declaration of it's own, but special attention 4583169699Skan is required if the type is anonymous. 458418334Speter 4585169699Skan We handle the NORMAL and FIELD contexts here by attaching an 4586169699Skan artificial TYPE_DECL to such pointed-to type. This forces the 4587169699Skan sizes evaluation at a safe point and ensures it is not deferred 4588169699Skan until e.g. within a deeper conditional context. 458918334Speter 4590169699Skan We expect nothing to be needed here for PARM or TYPENAME. 4591169699Skan Pushing a TYPE_DECL at this point for TYPENAME would actually 4592169699Skan be incorrect, as we might be in the middle of an expression 4593169699Skan with side effects on the pointed-to type size "arguments" prior 4594169699Skan to the pointer declaration point and the fake TYPE_DECL in the 4595169699Skan enclosing context would force the size evaluation prior to the 4596169699Skan side effects. */ 459752284Sobrien 4598169699Skan if (!TYPE_NAME (type) 4599169699Skan && (decl_context == NORMAL || decl_context == FIELD) 4600169699Skan && variably_modified_type_p (type, NULL_TREE)) 4601169699Skan { 4602169699Skan tree decl = build_decl (TYPE_DECL, NULL_TREE, type); 4603169699Skan DECL_ARTIFICIAL (decl) = 1; 4604169699Skan pushdecl (decl); 4605169699Skan finish_decl (decl, NULL_TREE, NULL_TREE); 4606169699Skan TYPE_NAME (type) = decl; 4607169699Skan } 460852284Sobrien 4609169699Skan type = build_pointer_type (type); 461090075Sobrien 4611169699Skan /* Process type qualifiers (such as const or volatile) 4612169699Skan that were given inside the `*'. */ 4613169699Skan type_quals = declarator->u.pointer_quals; 461452284Sobrien 4615169699Skan declarator = declarator->declarator; 4616169699Skan break; 4617169699Skan } 4618169699Skan default: 4619169699Skan gcc_unreachable (); 462018334Speter } 462118334Speter } 462218334Speter 4623169699Skan /* Now TYPE has the actual type, apart from any qualifiers in 4624169699Skan TYPE_QUALS. */ 462518334Speter 4626146906Skan /* Check the type and width of a bit-field. */ 4627146906Skan if (bitfield) 4628146906Skan check_bitfield_type_and_width (&type, width, orig_name); 4629146906Skan 463050397Sobrien /* Did array size calculations overflow? */ 463150397Sobrien 463250397Sobrien if (TREE_CODE (type) == ARRAY_TYPE 463390075Sobrien && COMPLETE_TYPE_P (type) 4634169699Skan && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST 4635169699Skan && TREE_OVERFLOW (TYPE_SIZE_UNIT (type))) 463690075Sobrien { 4637169699Skan error ("size of array %qs is too large", name); 463890075Sobrien /* If we proceed with the array type as it is, we'll eventually 463990075Sobrien crash in tree_low_cst(). */ 464090075Sobrien type = error_mark_node; 464190075Sobrien } 464250397Sobrien 464318334Speter /* If this is declaring a typedef name, return a TYPE_DECL. */ 464418334Speter 4645169699Skan if (storage_class == csc_typedef) 464618334Speter { 464718334Speter tree decl; 464818334Speter if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 464952284Sobrien && type_quals) 465090075Sobrien pedwarn ("ISO C forbids qualified function types"); 465152284Sobrien if (type_quals) 465252284Sobrien type = c_build_qualified_type (type, type_quals); 4653169699Skan decl = build_decl (TYPE_DECL, declarator->u.id, type); 4654169699Skan if (declspecs->explicit_signed_p) 465518334Speter C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 465690075Sobrien decl_attributes (&decl, returned_attrs, 0); 4657169699Skan if (declspecs->inline_p) 4658169699Skan pedwarn ("typedef %q+D declared %<inline%>", decl); 465918334Speter return decl; 466018334Speter } 466118334Speter 466218334Speter /* If this is a type name (such as, in a cast or sizeof), 466318334Speter compute the type and return it now. */ 466418334Speter 466518334Speter if (decl_context == TYPENAME) 466618334Speter { 4667169699Skan /* Note that the grammar rejects storage classes in typenames 4668169699Skan and fields. */ 4669169699Skan gcc_assert (storage_class == csc_none && !threadp 4670169699Skan && !declspecs->inline_p); 467118334Speter if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 467252284Sobrien && type_quals) 467390075Sobrien pedwarn ("ISO C forbids const or volatile function types"); 467452284Sobrien if (type_quals) 467552284Sobrien type = c_build_qualified_type (type, type_quals); 467690075Sobrien decl_attributes (&type, returned_attrs, 0); 467718334Speter return type; 467818334Speter } 467918334Speter 4680169699Skan if (pedantic && decl_context == FIELD 4681169699Skan && variably_modified_type_p (type, NULL_TREE)) 4682169699Skan { 4683169699Skan /* C99 6.7.2.1p8 */ 4684169699Skan pedwarn ("a member of a structure or union cannot have a variably modified type"); 4685169699Skan } 4686169699Skan 468718334Speter /* Aside from typedefs and type names (handle above), 468818334Speter `void' at top level (not within pointer) 468918334Speter is allowed only in public variables. 469018334Speter We don't complain about parms either, but that is because 469118334Speter a better error message can be made later. */ 469218334Speter 469390075Sobrien if (VOID_TYPE_P (type) && decl_context != PARM 4694169699Skan && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE) 4695169699Skan && (storage_class == csc_extern 4696169699Skan || (current_scope == file_scope 4697169699Skan && !(storage_class == csc_static 4698169699Skan || storage_class == csc_register))))) 469918334Speter { 4700169699Skan error ("variable or field %qs declared void", name); 470118334Speter type = integer_type_node; 470218334Speter } 470318334Speter 470418334Speter /* Now create the decl, which may be a VAR_DECL, a PARM_DECL 470518334Speter or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ 470618334Speter 470718334Speter { 470890075Sobrien tree decl; 470918334Speter 471018334Speter if (decl_context == PARM) 471118334Speter { 471290075Sobrien tree type_as_written; 471390075Sobrien tree promoted_type; 471418334Speter 471518334Speter /* A parameter declared as an array of T is really a pointer to T. 471618334Speter One declared as a function is really a pointer to a function. */ 471718334Speter 471818334Speter if (TREE_CODE (type) == ARRAY_TYPE) 471918334Speter { 472018334Speter /* Transfer const-ness of array into that of type pointed to. */ 472118334Speter type = TREE_TYPE (type); 472252284Sobrien if (type_quals) 472352284Sobrien type = c_build_qualified_type (type, type_quals); 472418334Speter type = build_pointer_type (type); 4725169699Skan type_quals = array_ptr_quals; 472690075Sobrien 4727169699Skan /* We don't yet implement attributes in this context. */ 4728169699Skan if (array_ptr_attrs != NULL_TREE) 4729169699Skan warning (OPT_Wattributes, 4730169699Skan "attributes in parameter array declarator ignored"); 473190075Sobrien 473218334Speter size_varies = 0; 473318334Speter } 473418334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 473518334Speter { 473652284Sobrien if (pedantic && type_quals) 473790075Sobrien pedwarn ("ISO C forbids qualified function types"); 473852284Sobrien if (type_quals) 473952284Sobrien type = c_build_qualified_type (type, type_quals); 474018334Speter type = build_pointer_type (type); 474152284Sobrien type_quals = TYPE_UNQUALIFIED; 474218334Speter } 474390075Sobrien else if (type_quals) 474490075Sobrien type = c_build_qualified_type (type, type_quals); 4745132730Skan 474690075Sobrien type_as_written = type; 474718334Speter 4748169699Skan decl = build_decl (PARM_DECL, declarator->u.id, type); 474918334Speter if (size_varies) 475018334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 475118334Speter 475218334Speter /* Compute the type actually passed in the parmlist, 475318334Speter for the case where there is no prototype. 475418334Speter (For example, shorts and chars are passed as ints.) 475518334Speter When there is a prototype, this is overridden later. */ 475618334Speter 475790075Sobrien if (type == error_mark_node) 475890075Sobrien promoted_type = type; 475990075Sobrien else 4760117421Skan promoted_type = c_type_promotes_to (type); 476118334Speter 476290075Sobrien DECL_ARG_TYPE (decl) = promoted_type; 4763169699Skan if (declspecs->inline_p) 4764169699Skan pedwarn ("parameter %q+D declared %<inline%>", decl); 476518334Speter } 476618334Speter else if (decl_context == FIELD) 476718334Speter { 4768169699Skan /* Note that the grammar rejects storage classes in typenames 4769169699Skan and fields. */ 4770169699Skan gcc_assert (storage_class == csc_none && !threadp 4771169699Skan && !declspecs->inline_p); 4772169699Skan 477318334Speter /* Structure field. It may not be a function. */ 477496263Sobrien 477518334Speter if (TREE_CODE (type) == FUNCTION_TYPE) 477618334Speter { 4777169699Skan error ("field %qs declared as a function", name); 477818334Speter type = build_pointer_type (type); 477918334Speter } 478090075Sobrien else if (TREE_CODE (type) != ERROR_MARK 4781169699Skan && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type)) 478218334Speter { 4783169699Skan error ("field %qs has incomplete type", name); 478418334Speter type = error_mark_node; 478518334Speter } 4786169699Skan type = c_build_qualified_type (type, type_quals); 4787169699Skan decl = build_decl (FIELD_DECL, declarator->u.id, type); 478890075Sobrien DECL_NONADDRESSABLE_P (decl) = bitfield; 478990075Sobrien 479018334Speter if (size_varies) 479118334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 479218334Speter } 479318334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 479418334Speter { 4795169699Skan if (storage_class == csc_register || threadp) 4796169699Skan { 4797169699Skan error ("invalid storage class for function %qs", name); 4798169699Skan } 4799169699Skan else if (current_scope != file_scope) 4800169699Skan { 4801169699Skan /* Function declaration not at file scope. Storage 4802169699Skan classes other than `extern' are not allowed, C99 4803169699Skan 6.7.1p5, and `extern' makes no difference. However, 4804169699Skan GCC allows 'auto', perhaps with 'inline', to support 4805169699Skan nested functions. */ 4806169699Skan if (storage_class == csc_auto) 4807169699Skan { 4808169699Skan if (pedantic) 4809169699Skan pedwarn ("invalid storage class for function %qs", name); 4810169699Skan } 4811169699Skan else if (storage_class == csc_static) 4812169699Skan { 4813169699Skan error ("invalid storage class for function %qs", name); 4814169699Skan if (funcdef_flag) 4815169699Skan storage_class = declspecs->storage_class = csc_none; 4816169699Skan else 4817169699Skan return 0; 4818169699Skan } 4819169699Skan } 482018334Speter 4821169699Skan decl = build_decl (FUNCTION_DECL, declarator->u.id, type); 482290075Sobrien decl = build_decl_attribute_variant (decl, decl_attr); 482318334Speter 4824169699Skan DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl); 482590075Sobrien 4826169699Skan if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl)) 482790075Sobrien pedwarn ("ISO C forbids qualified function types"); 482818334Speter 4829169699Skan /* GNU C interprets a volatile-qualified function type to indicate 483052284Sobrien that the function does not return. */ 483152284Sobrien if ((type_quals & TYPE_QUAL_VOLATILE) 483290075Sobrien && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) 4833169699Skan warning (0, "%<noreturn%> function returns non-void value"); 483418334Speter 4835169699Skan /* Every function declaration is an external reference 4836169699Skan (DECL_EXTERNAL) except for those which are not at file 4837169699Skan scope and are explicitly declared "auto". This is 4838169699Skan forbidden by standard C (C99 6.7.1p5) and is interpreted by 4839169699Skan GCC to signify a forward declaration of a nested function. */ 4840169699Skan if (storage_class == csc_auto && current_scope != file_scope) 4841169699Skan DECL_EXTERNAL (decl) = 0; 4842189824Sdas /* In C99, a function which is declared 'inline' with 'extern' 4843189824Sdas is not an external reference (which is confusing). It 4844189824Sdas means that the later definition of the function must be output 4845189824Sdas in this file, C99 6.7.4p6. In GNU C89, a function declared 4846189824Sdas 'extern inline' is an external reference. */ 4847189824Sdas else if (declspecs->inline_p && storage_class != csc_static) 4848189824Sdas DECL_EXTERNAL (decl) = ((storage_class == csc_extern) 4849189824Sdas == flag_gnu89_inline); 4850169699Skan else 4851189824Sdas DECL_EXTERNAL (decl) = !initialized; 4852169699Skan 485318334Speter /* Record absence of global scope for `static' or `auto'. */ 485418334Speter TREE_PUBLIC (decl) 4855169699Skan = !(storage_class == csc_static || storage_class == csc_auto); 485618334Speter 4857169699Skan /* For a function definition, record the argument information 4858169699Skan block where store_parm_decls will look for it. */ 4859169699Skan if (funcdef_flag) 4860169699Skan current_function_arg_info = arg_info; 4861169699Skan 4862169699Skan if (declspecs->default_int_p) 486396263Sobrien C_FUNCTION_IMPLICIT_INT (decl) = 1; 486496263Sobrien 486518334Speter /* Record presence of `inline', if it is reasonable. */ 4866169699Skan if (flag_hosted && MAIN_NAME_P (declarator->u.id)) 486718334Speter { 4868169699Skan if (declspecs->inline_p) 4869169699Skan pedwarn ("cannot inline function %<main%>"); 487090075Sobrien } 4871169699Skan else if (declspecs->inline_p) 487290075Sobrien { 4873132730Skan /* Record that the function is declared `inline'. */ 487490075Sobrien DECL_DECLARED_INLINE_P (decl) = 1; 487518334Speter 487690075Sobrien /* Do not mark bare declarations as DECL_INLINE. Doing so 487790075Sobrien in the presence of multiple declarations can result in 487890075Sobrien the abstract origin pointing between the declarations, 487990075Sobrien which will confuse dwarf2out. */ 488090075Sobrien if (initialized) 4881189824Sdas DECL_INLINE (decl) = 1; 488218334Speter } 488390075Sobrien /* If -finline-functions, assume it can be inlined. This does 488490075Sobrien two things: let the function be deferred until it is actually 488590075Sobrien needed, and let dwarf2 know that the function is inlinable. */ 488690075Sobrien else if (flag_inline_trees == 2 && initialized) 4887132730Skan DECL_INLINE (decl) = 1; 488818334Speter } 488918334Speter else 489018334Speter { 489118334Speter /* It's a variable. */ 489218334Speter /* An uninitialized decl with `extern' is a reference. */ 4893169699Skan int extern_ref = !initialized && storage_class == csc_extern; 489418334Speter 4895169699Skan type = c_build_qualified_type (type, type_quals); 4896132730Skan 4897169699Skan /* C99 6.2.2p7: It is invalid (compile-time undefined 4898169699Skan behavior) to create an 'extern' declaration for a 4899132730Skan variable if there is a global declaration that is 4900169699Skan 'static' and the global declaration is not visible. 4901169699Skan (If the static declaration _is_ currently visible, 4902169699Skan the 'extern' declaration is taken to refer to that decl.) */ 4903169699Skan if (extern_ref && current_scope != file_scope) 4904132730Skan { 4905169699Skan tree global_decl = identifier_global_value (declarator->u.id); 4906169699Skan tree visible_decl = lookup_name (declarator->u.id); 4907132730Skan 4908132730Skan if (global_decl 4909169699Skan && global_decl != visible_decl 4910132730Skan && TREE_CODE (global_decl) == VAR_DECL 4911132730Skan && !TREE_PUBLIC (global_decl)) 4912169699Skan error ("variable previously declared %<static%> redeclared " 4913169699Skan "%<extern%>"); 4914132730Skan } 4915132730Skan 4916169699Skan decl = build_decl (VAR_DECL, declarator->u.id, type); 4917169699Skan DECL_SOURCE_LOCATION (decl) = declarator->id_loc; 491818334Speter if (size_varies) 491918334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 492018334Speter 4921169699Skan if (declspecs->inline_p) 4922169699Skan pedwarn ("variable %q+D declared %<inline%>", decl); 492318334Speter 4924169699Skan /* At file scope, an initialized extern declaration may follow 4925169699Skan a static declaration. In that case, DECL_EXTERNAL will be 4926169699Skan reset later in start_decl. */ 4927169699Skan DECL_EXTERNAL (decl) = (storage_class == csc_extern); 4928117421Skan 4929132730Skan /* At file scope, the presence of a `static' or `register' storage 493018334Speter class specifier, or the absence of all storage class specifiers 493118334Speter makes this declaration a definition (perhaps tentative). Also, 4932169699Skan the absence of `static' makes it public. */ 4933169699Skan if (current_scope == file_scope) 493418334Speter { 4935169699Skan TREE_PUBLIC (decl) = storage_class != csc_static; 4936117421Skan TREE_STATIC (decl) = !extern_ref; 493718334Speter } 4938132730Skan /* Not at file scope, only `static' makes a static definition. */ 493918334Speter else 494018334Speter { 4941169699Skan TREE_STATIC (decl) = (storage_class == csc_static); 4942117421Skan TREE_PUBLIC (decl) = extern_ref; 494318334Speter } 4944117421Skan 4945169699Skan if (threadp) 4946117421Skan { 4947117421Skan if (targetm.have_tls) 4948169699Skan DECL_TLS_MODEL (decl) = decl_default_tls_model (decl); 4949117421Skan else 4950117421Skan /* A mere warning is sure to result in improper semantics 4951117421Skan at runtime. Don't bother to allow this to compile. */ 4952117421Skan error ("thread-local storage not supported for this target"); 4953117421Skan } 495418334Speter } 495518334Speter 4956169699Skan if (storage_class == csc_extern 4957169699Skan && variably_modified_type_p (type, NULL_TREE)) 4958169699Skan { 4959169699Skan /* C99 6.7.5.2p2 */ 4960169699Skan error ("object with variably modified type must have no linkage"); 4961169699Skan } 4962169699Skan 496318334Speter /* Record `register' declaration for warnings on & 496418334Speter and in case doing stupid register allocation. */ 496518334Speter 4966169699Skan if (storage_class == csc_register) 4967169699Skan { 4968169699Skan C_DECL_REGISTER (decl) = 1; 4969169699Skan DECL_REGISTER (decl) = 1; 4970169699Skan } 497118334Speter 497218334Speter /* Record constancy and volatility. */ 497352284Sobrien c_apply_type_quals_to_decl (type_quals, decl); 497418334Speter 497518334Speter /* If a type has volatile components, it should be stored in memory. 497618334Speter Otherwise, the fact that those components are volatile 4977169699Skan will be ignored, and would even crash the compiler. 4978169699Skan Of course, this only makes sense on VAR,PARM, and RESULT decl's. */ 4979169699Skan if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)) 4980169699Skan && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL 4981169699Skan || TREE_CODE (decl) == RESULT_DECL)) 4982169699Skan { 4983169699Skan /* It is not an error for a structure with volatile fields to 4984169699Skan be declared register, but reset DECL_REGISTER since it 4985169699Skan cannot actually go in a register. */ 4986169699Skan int was_reg = C_DECL_REGISTER (decl); 4987169699Skan C_DECL_REGISTER (decl) = 0; 4988169699Skan DECL_REGISTER (decl) = 0; 4989169699Skan c_mark_addressable (decl); 4990169699Skan C_DECL_REGISTER (decl) = was_reg; 4991169699Skan } 499218334Speter 4993132730Skan /* This is the earliest point at which we might know the assembler 4994132730Skan name of a variable. Thus, if it's known before this, die horribly. */ 4995169699Skan gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)); 4996132730Skan 499790075Sobrien decl_attributes (&decl, returned_attrs, 0); 499818334Speter 499918334Speter return decl; 500018334Speter } 500118334Speter} 500218334Speter 500318334Speter/* Decode the parameter-list info for a function type or function definition. 5004169699Skan The argument is the value returned by `get_parm_info' (or made in c-parse.c 500518334Speter if there is an identifier list instead of a parameter decl list). 500618334Speter These two functions are separate because when a function returns 500718334Speter or receives functions then each is called multiple times but the order 500818334Speter of calls is different. The last call to `grokparms' is always the one 500918334Speter that contains the formal parameter names of a function definition. 501018334Speter 501118334Speter Return a list of arg types to use in the FUNCTION_TYPE for this function. 501218334Speter 5013169699Skan FUNCDEF_FLAG is true for a function definition, false for 501418334Speter a mere declaration. A nonempty identifier-list gets an error message 5015169699Skan when FUNCDEF_FLAG is false. */ 501618334Speter 501718334Speterstatic tree 5018169699Skangrokparms (struct c_arg_info *arg_info, bool funcdef_flag) 501918334Speter{ 5020169699Skan tree arg_types = arg_info->types; 502118334Speter 5022169699Skan if (funcdef_flag && arg_info->had_vla_unspec) 5023169699Skan { 5024169699Skan /* A function definition isn't function prototype scope C99 6.2.1p4. */ 5025169699Skan /* C99 6.7.5.2p4 */ 5026169699Skan error ("%<[*]%> not allowed in other than function prototype scope"); 5027169699Skan } 502818334Speter 5029169699Skan if (arg_types == 0 && !funcdef_flag && !in_system_header) 5030169699Skan warning (OPT_Wstrict_prototypes, 5031169699Skan "function declaration isn%'t a prototype"); 503218334Speter 5033169699Skan if (arg_types == error_mark_node) 5034169699Skan return 0; /* don't set TYPE_ARG_TYPES in this case */ 5035169699Skan 5036169699Skan else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE) 503718334Speter { 5038169699Skan if (!funcdef_flag) 503918334Speter pedwarn ("parameter names (without types) in function declaration"); 504018334Speter 5041169699Skan arg_info->parms = arg_info->types; 5042169699Skan arg_info->types = 0; 504318334Speter return 0; 504418334Speter } 504518334Speter else 504618334Speter { 5047169699Skan tree parm, type, typelt; 5048169699Skan unsigned int parmno; 504918334Speter 5050169699Skan /* If there is a parameter of incomplete type in a definition, 5051169699Skan this is an error. In a declaration this is valid, and a 5052169699Skan struct or union type may be completed later, before any calls 5053169699Skan or definition of the function. In the case where the tag was 5054169699Skan first declared within the parameter list, a warning has 5055169699Skan already been given. If a parameter has void type, then 5056169699Skan however the function cannot be defined or called, so 5057169699Skan warn. */ 5058169699Skan 5059169699Skan for (parm = arg_info->parms, typelt = arg_types, parmno = 1; 5060132730Skan parm; 5061169699Skan parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++) 5062169699Skan { 5063169699Skan type = TREE_VALUE (typelt); 5064169699Skan if (type == error_mark_node) 5065169699Skan continue; 5066132730Skan 5067169699Skan if (!COMPLETE_TYPE_P (type)) 5068169699Skan { 5069169699Skan if (funcdef_flag) 5070169699Skan { 5071169699Skan if (DECL_NAME (parm)) 5072169699Skan error ("parameter %u (%q+D) has incomplete type", 5073169699Skan parmno, parm); 5074169699Skan else 5075169699Skan error ("%Jparameter %u has incomplete type", 5076169699Skan parm, parmno); 5077169699Skan 5078169699Skan TREE_VALUE (typelt) = error_mark_node; 5079169699Skan TREE_TYPE (parm) = error_mark_node; 5080169699Skan } 5081169699Skan else if (VOID_TYPE_P (type)) 5082169699Skan { 5083169699Skan if (DECL_NAME (parm)) 5084169699Skan warning (0, "parameter %u (%q+D) has void type", 5085169699Skan parmno, parm); 5086169699Skan else 5087169699Skan warning (0, "%Jparameter %u has void type", 5088169699Skan parm, parmno); 5089169699Skan } 5090169699Skan } 5091169699Skan 5092169699Skan if (DECL_NAME (parm) && TREE_USED (parm)) 5093169699Skan warn_if_shadowing (parm); 5094169699Skan } 5095169699Skan return arg_types; 509618334Speter } 509718334Speter} 509818334Speter 5099169699Skan/* Take apart the current scope and return a c_arg_info structure with 5100169699Skan info on a parameter list just parsed. 510118334Speter 5102169699Skan This structure is later fed to 'grokparms' and 'store_parm_decls'. 510318334Speter 5104169699Skan ELLIPSIS being true means the argument list ended in '...' so don't 5105169699Skan append a sentinel (void_list_node) to the end of the type-list. */ 5106169699Skan 5107169699Skanstruct c_arg_info * 5108169699Skanget_parm_info (bool ellipsis) 510918334Speter{ 5110169699Skan struct c_binding *b = current_scope->bindings; 5111169699Skan struct c_arg_info *arg_info = XOBNEW (&parser_obstack, 5112169699Skan struct c_arg_info); 5113169699Skan tree parms = 0; 5114169699Skan tree tags = 0; 5115169699Skan tree types = 0; 5116169699Skan tree others = 0; 5117169699Skan 5118132730Skan static bool explained_incomplete_types = false; 5119132730Skan bool gave_void_only_once_err = false; 512018334Speter 5121169699Skan arg_info->parms = 0; 5122169699Skan arg_info->tags = 0; 5123169699Skan arg_info->types = 0; 5124169699Skan arg_info->others = 0; 5125169699Skan arg_info->pending_sizes = 0; 5126169699Skan arg_info->had_vla_unspec = current_scope->had_vla_unspec; 5127132730Skan 5128169699Skan /* The bindings in this scope must not get put into a block. 5129169699Skan We will take care of deleting the binding nodes. */ 5130169699Skan current_scope->bindings = 0; 513118334Speter 5132169699Skan /* This function is only called if there was *something* on the 5133169699Skan parameter list. */ 5134169699Skan gcc_assert (b); 5135169699Skan 5136169699Skan /* A parameter list consisting solely of 'void' indicates that the 5137169699Skan function takes no arguments. But if the 'void' is qualified 5138169699Skan (by 'const' or 'volatile'), or has a storage class specifier 5139169699Skan ('register'), then the behavior is undefined; issue an error. 5140169699Skan Typedefs for 'void' are OK (see DR#157). */ 5141169699Skan if (b->prev == 0 /* one binding */ 5142169699Skan && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */ 5143169699Skan && !DECL_NAME (b->decl) /* anonymous */ 5144169699Skan && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */ 514518334Speter { 5146169699Skan if (TREE_THIS_VOLATILE (b->decl) 5147169699Skan || TREE_READONLY (b->decl) 5148169699Skan || C_DECL_REGISTER (b->decl)) 5149169699Skan error ("%<void%> as only parameter may not be qualified"); 515018334Speter 5151169699Skan /* There cannot be an ellipsis. */ 5152169699Skan if (ellipsis) 5153169699Skan error ("%<void%> must be the only parameter"); 5154132730Skan 5155169699Skan arg_info->types = void_list_node; 5156169699Skan return arg_info; 515718334Speter } 515818334Speter 5159169699Skan if (!ellipsis) 5160169699Skan types = void_list_node; 516118334Speter 5162169699Skan /* Break up the bindings list into parms, tags, types, and others; 5163169699Skan apply sanity checks; purge the name-to-decl bindings. */ 5164169699Skan while (b) 516518334Speter { 5166169699Skan tree decl = b->decl; 5167169699Skan tree type = TREE_TYPE (decl); 5168132730Skan const char *keyword; 5169132730Skan 5170169699Skan switch (TREE_CODE (decl)) 517190075Sobrien { 5172169699Skan case PARM_DECL: 5173169699Skan if (b->id) 5174169699Skan { 5175169699Skan gcc_assert (I_SYMBOL_BINDING (b->id) == b); 5176169699Skan I_SYMBOL_BINDING (b->id) = b->shadowed; 5177169699Skan } 5178132730Skan 5179169699Skan /* Check for forward decls that never got their actual decl. */ 5180169699Skan if (TREE_ASM_WRITTEN (decl)) 5181169699Skan error ("parameter %q+D has just a forward declaration", decl); 5182169699Skan /* Check for (..., void, ...) and issue an error. */ 5183169699Skan else if (VOID_TYPE_P (type) && !DECL_NAME (decl)) 5184169699Skan { 5185169699Skan if (!gave_void_only_once_err) 5186169699Skan { 5187169699Skan error ("%<void%> must be the only parameter"); 5188169699Skan gave_void_only_once_err = true; 5189169699Skan } 5190169699Skan } 5191169699Skan else 5192169699Skan { 5193169699Skan /* Valid parameter, add it to the list. */ 5194169699Skan TREE_CHAIN (decl) = parms; 5195169699Skan parms = decl; 5196132730Skan 5197169699Skan /* Since there is a prototype, args are passed in their 5198169699Skan declared types. The back end may override this later. */ 5199169699Skan DECL_ARG_TYPE (decl) = type; 5200169699Skan types = tree_cons (0, type, types); 5201169699Skan } 5202169699Skan break; 5203169699Skan 5204169699Skan case ENUMERAL_TYPE: keyword = "enum"; goto tag; 5205169699Skan case UNION_TYPE: keyword = "union"; goto tag; 5206169699Skan case RECORD_TYPE: keyword = "struct"; goto tag; 5207169699Skan tag: 5208169699Skan /* Types may not have tag-names, in which case the type 5209169699Skan appears in the bindings list with b->id NULL. */ 5210169699Skan if (b->id) 5211169699Skan { 5212169699Skan gcc_assert (I_TAG_BINDING (b->id) == b); 5213169699Skan I_TAG_BINDING (b->id) = b->shadowed; 5214169699Skan } 5215169699Skan 5216169699Skan /* Warn about any struct, union or enum tags defined in a 5217169699Skan parameter list. The scope of such types is limited to 5218169699Skan the parameter list, which is rarely if ever desirable 5219169699Skan (it's impossible to call such a function with type- 5220169699Skan correct arguments). An anonymous union parm type is 5221169699Skan meaningful as a GNU extension, so don't warn for that. */ 5222169699Skan if (TREE_CODE (decl) != UNION_TYPE || b->id != 0) 5223169699Skan { 5224169699Skan if (b->id) 5225169699Skan /* The %s will be one of 'struct', 'union', or 'enum'. */ 5226169699Skan warning (0, "%<%s %E%> declared inside parameter list", 5227169699Skan keyword, b->id); 5228169699Skan else 5229169699Skan /* The %s will be one of 'struct', 'union', or 'enum'. */ 5230169699Skan warning (0, "anonymous %s declared inside parameter list", 5231169699Skan keyword); 5232169699Skan 5233169699Skan if (!explained_incomplete_types) 5234169699Skan { 5235169699Skan warning (0, "its scope is only this definition or declaration," 5236169699Skan " which is probably not what you want"); 5237169699Skan explained_incomplete_types = true; 5238169699Skan } 5239169699Skan } 5240169699Skan 5241169699Skan tags = tree_cons (b->id, decl, tags); 5242169699Skan break; 5243169699Skan 5244169699Skan case CONST_DECL: 5245169699Skan case TYPE_DECL: 5246169699Skan case FUNCTION_DECL: 5247169699Skan /* CONST_DECLs appear here when we have an embedded enum, 5248169699Skan and TYPE_DECLs appear here when we have an embedded struct 5249169699Skan or union. No warnings for this - we already warned about the 5250169699Skan type itself. FUNCTION_DECLs appear when there is an implicit 5251169699Skan function declaration in the parameter list. */ 5252169699Skan 5253169699Skan TREE_CHAIN (decl) = others; 5254169699Skan others = decl; 5255169699Skan /* fall through */ 5256169699Skan 5257169699Skan case ERROR_MARK: 5258169699Skan /* error_mark_node appears here when we have an undeclared 5259169699Skan variable. Just throw it away. */ 5260169699Skan if (b->id) 5261169699Skan { 5262169699Skan gcc_assert (I_SYMBOL_BINDING (b->id) == b); 5263169699Skan I_SYMBOL_BINDING (b->id) = b->shadowed; 5264169699Skan } 5265169699Skan break; 5266169699Skan 5267169699Skan /* Other things that might be encountered. */ 5268169699Skan case LABEL_DECL: 5269169699Skan case VAR_DECL: 5270169699Skan default: 5271169699Skan gcc_unreachable (); 527218334Speter } 5273132730Skan 5274169699Skan b = free_binding_and_advance (b); 5275132730Skan } 5276132730Skan 5277169699Skan arg_info->parms = parms; 5278169699Skan arg_info->tags = tags; 5279169699Skan arg_info->types = types; 5280169699Skan arg_info->others = others; 5281169699Skan arg_info->pending_sizes = get_pending_sizes (); 5282169699Skan return arg_info; 528318334Speter} 528418334Speter 528518334Speter/* Get the struct, enum or union (CODE says which) with tag NAME. 5286169699Skan Define the tag as a forward-reference if it is not defined. 5287169699Skan Return a c_typespec structure for the type specifier. */ 528818334Speter 5289169699Skanstruct c_typespec 5290169699Skanparser_xref_tag (enum tree_code code, tree name) 529118334Speter{ 5292169699Skan struct c_typespec ret; 529318334Speter /* If a cross reference is requested, look up the type 529418334Speter already defined for this tag and return it. */ 529518334Speter 5296132730Skan tree ref = lookup_tag (code, name, 0); 529790075Sobrien /* If this is the right type of tag, return what we found. 529890075Sobrien (This reference will be shadowed by shadow_tag later if appropriate.) 529990075Sobrien If this is the wrong type of tag, do not return it. If it was the 5300132730Skan wrong type in the same scope, we will have had an error 5301132730Skan message already; if in a different scope and declaring 530290075Sobrien a name, pending_xref_error will give an error message; but if in a 5303132730Skan different scope and not declaring a name, this tag should 530490075Sobrien shadow the previous declaration of a different type of tag, and 530590075Sobrien this would not work properly if we return the reference found. 530690075Sobrien (For example, with "struct foo" in an outer scope, "union foo;" 530790075Sobrien must shadow that tag with a new one of union type.) */ 5308169699Skan ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref); 530990075Sobrien if (ref && TREE_CODE (ref) == code) 5310169699Skan { 5311169699Skan ret.spec = ref; 5312169699Skan return ret; 5313169699Skan } 531418334Speter 531518334Speter /* If no such tag is yet defined, create a forward-reference node 531618334Speter and record it as the "definition". 531718334Speter When a real declaration of this type is found, 531818334Speter the forward-reference will be altered into a real type. */ 531918334Speter 532018334Speter ref = make_node (code); 532118334Speter if (code == ENUMERAL_TYPE) 532218334Speter { 532318334Speter /* Give the type a default layout like unsigned int 532418334Speter to avoid crashing if it does not get defined. */ 532518334Speter TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node); 532618334Speter TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); 532790075Sobrien TYPE_USER_ALIGN (ref) = 0; 5328169699Skan TYPE_UNSIGNED (ref) = 1; 532918334Speter TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); 533018334Speter TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); 533118334Speter TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); 533218334Speter } 533318334Speter 533418334Speter pushtag (name, ref); 533518334Speter 5336169699Skan ret.spec = ref; 5337169699Skan return ret; 533818334Speter} 5339169699Skan 5340169699Skan/* Get the struct, enum or union (CODE says which) with tag NAME. 5341169699Skan Define the tag as a forward-reference if it is not defined. 5342169699Skan Return a tree for the type. */ 5343169699Skan 5344169699Skantree 5345169699Skanxref_tag (enum tree_code code, tree name) 5346169699Skan{ 5347169699Skan return parser_xref_tag (code, name).spec; 5348169699Skan} 534918334Speter 5350132730Skan/* Make sure that the tag NAME is defined *in the current scope* 535118334Speter at least as a forward reference. 535290075Sobrien CODE says which kind of tag NAME ought to be. */ 535318334Speter 535418334Spetertree 5355132730Skanstart_struct (enum tree_code code, tree name) 535618334Speter{ 5357132730Skan /* If there is already a tag defined at this scope 535818334Speter (as a forward reference), just return it. */ 535918334Speter 536090075Sobrien tree ref = 0; 536118334Speter 536218334Speter if (name != 0) 5363132730Skan ref = lookup_tag (code, name, 1); 536418334Speter if (ref && TREE_CODE (ref) == code) 536518334Speter { 5366161660Skan if (TYPE_SIZE (ref)) 5367169699Skan { 536890075Sobrien if (code == UNION_TYPE) 5369169699Skan error ("redefinition of %<union %E%>", name); 5370169699Skan else 5371169699Skan error ("redefinition of %<struct %E%>", name); 5372132730Skan } 5373161660Skan else if (C_TYPE_BEING_DEFINED (ref)) 5374161660Skan { 5375161660Skan if (code == UNION_TYPE) 5376169699Skan error ("nested redefinition of %<union %E%>", name); 5377169699Skan else 5378169699Skan error ("nested redefinition of %<struct %E%>", name); 5379189824Sdas /* Don't create structures that contain themselves. */ 5380189824Sdas ref = NULL_TREE; 5381161660Skan } 5382117421Skan } 5383189824Sdas 5384189824Sdas /* Otherwise create a forward-reference just so the tag is in scope. */ 5385189824Sdas 5386189824Sdas if (ref == NULL_TREE || TREE_CODE (ref) != code) 5387117421Skan { 5388117421Skan ref = make_node (code); 5389117421Skan pushtag (name, ref); 539018334Speter } 5391132730Skan 539218334Speter C_TYPE_BEING_DEFINED (ref) = 1; 539350397Sobrien TYPE_PACKED (ref) = flag_pack_struct; 539418334Speter return ref; 539518334Speter} 539618334Speter 5397169699Skan/* Process the specs, declarator and width (NULL if omitted) 539818334Speter of a structure component, returning a FIELD_DECL node. 5399132730Skan WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node. 540018334Speter 540118334Speter This is done during the parsing of the struct declaration. 540218334Speter The FIELD_DECL nodes are chained together and the lot of them 540318334Speter are ultimately passed to `build_struct' to make the RECORD_TYPE node. */ 540418334Speter 540518334Spetertree 5406169699Skangrokfield (struct c_declarator *declarator, struct c_declspecs *declspecs, 5407169699Skan tree width) 540818334Speter{ 540918334Speter tree value; 541018334Speter 5411169699Skan if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE 5412169699Skan && width == NULL_TREE) 541390075Sobrien { 5414117421Skan /* This is an unnamed decl. 5415117421Skan 5416117421Skan If we have something of the form "union { list } ;" then this 5417117421Skan is the anonymous union extension. Similarly for struct. 5418117421Skan 5419117421Skan If this is something of the form "struct foo;", then 5420117421Skan If MS extensions are enabled, this is handled as an 5421117421Skan anonymous struct. 5422117421Skan Otherwise this is a forward declaration of a structure tag. 5423117421Skan 5424117421Skan If this is something of the form "foo;" and foo is a TYPE_DECL, then 5425117421Skan If MS extensions are enabled and foo names a structure, then 5426117421Skan again this is an anonymous struct. 5427117421Skan Otherwise this is an error. 5428117421Skan 5429132730Skan Oh what a horrid tangled web we weave. I wonder if MS consciously 5430117421Skan took this from Plan 9 or if it was an accident of implementation 5431117421Skan that took root before someone noticed the bug... */ 5432117421Skan 5433169699Skan tree type = declspecs->type; 5434169699Skan bool type_ok = (TREE_CODE (type) == RECORD_TYPE 5435169699Skan || TREE_CODE (type) == UNION_TYPE); 5436169699Skan bool ok = false; 5437102790Skan 5438169699Skan if (type_ok 5439169699Skan && (flag_ms_extensions || !declspecs->typedef_p)) 544090075Sobrien { 5441117421Skan if (flag_ms_extensions) 5442169699Skan ok = true; 5443117421Skan else if (flag_iso) 5444169699Skan ok = false; 5445117421Skan else if (TYPE_NAME (type) == NULL) 5446169699Skan ok = true; 5447117421Skan else 5448169699Skan ok = false; 5449117421Skan } 5450169699Skan if (!ok) 5451117421Skan { 5452169699Skan pedwarn ("declaration does not declare anything"); 545390075Sobrien return NULL_TREE; 545490075Sobrien } 5455169699Skan if (pedantic) 5456169699Skan pedwarn ("ISO C doesn%'t support unnamed structs/unions"); 545790075Sobrien } 545818334Speter 5459169699Skan value = grokdeclarator (declarator, declspecs, FIELD, false, 5460132730Skan width ? &width : NULL); 546118334Speter 546218334Speter finish_decl (value, NULL_TREE, NULL_TREE); 546396263Sobrien DECL_INITIAL (value) = width; 546418334Speter 546518334Speter return value; 546618334Speter} 546718334Speter 5468132730Skan/* Generate an error for any duplicate field names in FIELDLIST. Munge 5469132730Skan the list such that this does not present a problem later. */ 5470122196Skan 5471132730Skanstatic void 5472132730Skandetect_field_duplicates (tree fieldlist) 5473132730Skan{ 5474132730Skan tree x, y; 5475132730Skan int timeout = 10; 5476122196Skan 5477132730Skan /* First, see if there are more than "a few" fields. 5478132730Skan This is trivially true if there are zero or one fields. */ 5479132730Skan if (!fieldlist) 5480132730Skan return; 5481132730Skan x = TREE_CHAIN (fieldlist); 5482132730Skan if (!x) 5483132730Skan return; 5484132730Skan do { 5485132730Skan timeout--; 5486132730Skan x = TREE_CHAIN (x); 5487132730Skan } while (timeout > 0 && x); 5488132730Skan 5489132730Skan /* If there were "few" fields, avoid the overhead of allocating 5490132730Skan a hash table. Instead just do the nested traversal thing. */ 5491132730Skan if (timeout > 0) 5492132730Skan { 5493132730Skan for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x)) 5494132730Skan if (DECL_NAME (x)) 5495132730Skan { 5496132730Skan for (y = fieldlist; y != x; y = TREE_CHAIN (y)) 5497132730Skan if (DECL_NAME (y) == DECL_NAME (x)) 5498132730Skan { 5499169699Skan error ("duplicate member %q+D", x); 5500132730Skan DECL_NAME (x) = NULL_TREE; 5501132730Skan } 5502132730Skan } 5503132730Skan } 5504132730Skan else 5505132730Skan { 5506132730Skan htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); 5507132730Skan void **slot; 5508132730Skan 5509132730Skan for (x = fieldlist; x ; x = TREE_CHAIN (x)) 5510132730Skan if ((y = DECL_NAME (x)) != 0) 5511132730Skan { 5512132730Skan slot = htab_find_slot (htab, y, INSERT); 5513132730Skan if (*slot) 5514132730Skan { 5515169699Skan error ("duplicate member %q+D", x); 5516132730Skan DECL_NAME (x) = NULL_TREE; 5517132730Skan } 5518132730Skan *slot = y; 5519132730Skan } 5520132730Skan 5521132730Skan htab_delete (htab); 5522132730Skan } 5523122196Skan} 5524132730Skan 552518334Speter/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. 552618334Speter FIELDLIST is a chain of FIELD_DECL nodes for the fields. 552790075Sobrien ATTRIBUTES are attributes to be applied to the structure. */ 552818334Speter 552918334Spetertree 5530132730Skanfinish_struct (tree t, tree fieldlist, tree attributes) 553118334Speter{ 553290075Sobrien tree x; 5533169699Skan bool toplevel = file_scope == current_scope; 553490075Sobrien int saw_named_field; 553518334Speter 553618334Speter /* If this type was previously laid out as a forward reference, 553718334Speter make sure we lay it out again. */ 553818334Speter 553918334Speter TYPE_SIZE (t) = 0; 554018334Speter 554190075Sobrien decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 554218334Speter 554350397Sobrien if (pedantic) 554450397Sobrien { 554550397Sobrien for (x = fieldlist; x; x = TREE_CHAIN (x)) 554650397Sobrien if (DECL_NAME (x) != 0) 554750397Sobrien break; 554818334Speter 554950397Sobrien if (x == 0) 5550169699Skan { 5551169699Skan if (TREE_CODE (t) == UNION_TYPE) 5552169699Skan { 5553169699Skan if (fieldlist) 5554169699Skan pedwarn ("union has no named members"); 5555169699Skan else 5556169699Skan pedwarn ("union has no members"); 5557169699Skan } 5558169699Skan else 5559169699Skan { 5560169699Skan if (fieldlist) 5561169699Skan pedwarn ("struct has no named members"); 5562169699Skan else 5563169699Skan pedwarn ("struct has no members"); 5564169699Skan } 5565169699Skan } 556650397Sobrien } 556750397Sobrien 556896263Sobrien /* Install struct as DECL_CONTEXT of each field decl. 5569169699Skan Also process specified field sizes, found in the DECL_INITIAL, 5570169699Skan storing 0 there after the type has been changed to precision equal 5571169699Skan to its width, rather than the precision of the specified standard 5572169699Skan type. (Correct layout requires the original type to have been preserved 5573169699Skan until now.) */ 557418334Speter 557590075Sobrien saw_named_field = 0; 557618334Speter for (x = fieldlist; x; x = TREE_CHAIN (x)) 557718334Speter { 5578169699Skan if (TREE_TYPE (x) == error_mark_node) 5579169699Skan continue; 5580169699Skan 558118334Speter DECL_CONTEXT (x) = t; 558218334Speter 5583169699Skan if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT) 5584169699Skan DECL_PACKED (x) = 1; 5585169699Skan 558618334Speter /* If any field is const, the structure type is pseudo-const. */ 558718334Speter if (TREE_READONLY (x)) 558818334Speter C_TYPE_FIELDS_READONLY (t) = 1; 558918334Speter else 559018334Speter { 559118334Speter /* A field that is pseudo-const makes the structure likewise. */ 559218334Speter tree t1 = TREE_TYPE (x); 559318334Speter while (TREE_CODE (t1) == ARRAY_TYPE) 559418334Speter t1 = TREE_TYPE (t1); 559518334Speter if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE) 559618334Speter && C_TYPE_FIELDS_READONLY (t1)) 559718334Speter C_TYPE_FIELDS_READONLY (t) = 1; 559818334Speter } 559918334Speter 560018334Speter /* Any field that is volatile means variables of this type must be 560118334Speter treated in some ways as volatile. */ 560218334Speter if (TREE_THIS_VOLATILE (x)) 560318334Speter C_TYPE_FIELDS_VOLATILE (t) = 1; 560418334Speter 560518334Speter /* Any field of nominal variable size implies structure is too. */ 560618334Speter if (C_DECL_VARIABLE_SIZE (x)) 560718334Speter C_TYPE_VARIABLE_SIZE (t) = 1; 560818334Speter 560996263Sobrien if (DECL_INITIAL (x)) 561018334Speter { 5611132730Skan unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1); 5612132730Skan DECL_SIZE (x) = bitsize_int (width); 5613132730Skan DECL_BIT_FIELD (x) = 1; 5614132730Skan SET_DECL_C_BIT_FIELD (x); 561596263Sobrien } 561696263Sobrien 561790075Sobrien /* Detect flexible array member in an invalid context. */ 561890075Sobrien if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 561990075Sobrien && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 562090075Sobrien && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 562190075Sobrien && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 562218334Speter { 562390075Sobrien if (TREE_CODE (t) == UNION_TYPE) 5624132730Skan { 5625132730Skan error ("%Jflexible array member in union", x); 5626132730Skan TREE_TYPE (x) = error_mark_node; 5627132730Skan } 562890075Sobrien else if (TREE_CHAIN (x) != NULL_TREE) 5629132730Skan { 5630132730Skan error ("%Jflexible array member not at end of struct", x); 5631132730Skan TREE_TYPE (x) = error_mark_node; 5632132730Skan } 5633169699Skan else if (!saw_named_field) 5634132730Skan { 5635132730Skan error ("%Jflexible array member in otherwise empty struct", x); 5636132730Skan TREE_TYPE (x) = error_mark_node; 5637132730Skan } 563818334Speter } 5639117421Skan 5640132730Skan if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE 5641117421Skan && flexible_array_type_p (TREE_TYPE (x))) 5642132730Skan pedwarn ("%Jinvalid use of structure with flexible array member", x); 5643117421Skan 564490075Sobrien if (DECL_NAME (x)) 564590075Sobrien saw_named_field = 1; 564618334Speter } 564718334Speter 5648132730Skan detect_field_duplicates (fieldlist); 564990075Sobrien 565018334Speter /* Now we have the nearly final fieldlist. Record it, 565118334Speter then lay out the structure or union (including the fields). */ 565218334Speter 565318334Speter TYPE_FIELDS (t) = fieldlist; 565418334Speter 565518334Speter layout_type (t); 565618334Speter 5657169699Skan /* Give bit-fields their proper types. */ 565890075Sobrien { 565990075Sobrien tree *fieldlistp = &fieldlist; 566090075Sobrien while (*fieldlistp) 5661169699Skan if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp) 5662169699Skan && TREE_TYPE (*fieldlistp) != error_mark_node) 5663169699Skan { 5664169699Skan unsigned HOST_WIDE_INT width 5665169699Skan = tree_low_cst (DECL_INITIAL (*fieldlistp), 1); 5666169699Skan tree type = TREE_TYPE (*fieldlistp); 5667169699Skan if (width != TYPE_PRECISION (type)) 5668169699Skan { 5669169699Skan TREE_TYPE (*fieldlistp) 5670169699Skan = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type)); 5671169699Skan DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp)); 5672169699Skan } 5673169699Skan DECL_INITIAL (*fieldlistp) = 0; 5674169699Skan } 567590075Sobrien else 567690075Sobrien fieldlistp = &TREE_CHAIN (*fieldlistp); 567790075Sobrien } 567818334Speter 567990075Sobrien /* Now we have the truly final field list. 568090075Sobrien Store it in this type and in the variants. */ 568118334Speter 568218334Speter TYPE_FIELDS (t) = fieldlist; 568318334Speter 5684122196Skan /* If there are lots of fields, sort so we can look through them fast. 5685132730Skan We arbitrarily consider 16 or more elts to be "a lot". */ 5686122196Skan 5687122196Skan { 5688122196Skan int len = 0; 5689122196Skan 5690122196Skan for (x = fieldlist; x; x = TREE_CHAIN (x)) 5691122196Skan { 5692169699Skan if (len > 15 || DECL_NAME (x) == NULL) 5693169699Skan break; 5694169699Skan len += 1; 5695122196Skan } 5696122196Skan 5697122196Skan if (len > 15) 5698122196Skan { 5699169699Skan tree *field_array; 5700169699Skan struct lang_type *space; 5701169699Skan struct sorted_fields_type *space2; 5702132730Skan 5703169699Skan len += list_length (x); 5704132730Skan 5705169699Skan /* Use the same allocation policy here that make_node uses, to 5706169699Skan ensure that this lives as long as the rest of the struct decl. 5707169699Skan All decls in an inline function need to be saved. */ 5708132730Skan 5709169699Skan space = GGC_CNEW (struct lang_type); 5710169699Skan space2 = GGC_NEWVAR (struct sorted_fields_type, 5711169699Skan sizeof (struct sorted_fields_type) + len * sizeof (tree)); 5712132730Skan 5713169699Skan len = 0; 5714132730Skan space->s = space2; 5715132730Skan field_array = &space2->elts[0]; 5716169699Skan for (x = fieldlist; x; x = TREE_CHAIN (x)) 5717169699Skan { 5718169699Skan field_array[len++] = x; 5719132730Skan 5720169699Skan /* If there is anonymous struct or union, break out of the loop. */ 5721169699Skan if (DECL_NAME (x) == NULL) 5722169699Skan break; 5723169699Skan } 5724169699Skan /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */ 5725169699Skan if (x == NULL) 5726169699Skan { 5727169699Skan TYPE_LANG_SPECIFIC (t) = space; 5728169699Skan TYPE_LANG_SPECIFIC (t)->s->len = len; 5729169699Skan field_array = TYPE_LANG_SPECIFIC (t)->s->elts; 5730169699Skan qsort (field_array, len, sizeof (tree), field_decl_cmp); 5731169699Skan } 5732122196Skan } 5733122196Skan } 5734132730Skan 573518334Speter for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) 573618334Speter { 573718334Speter TYPE_FIELDS (x) = TYPE_FIELDS (t); 573818334Speter TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t); 5739161660Skan C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t); 5740161660Skan C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t); 5741161660Skan C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t); 574218334Speter } 574318334Speter 574418334Speter /* If this was supposed to be a transparent union, but we can't 574518334Speter make it one, warn and turn off the flag. */ 574618334Speter if (TREE_CODE (t) == UNION_TYPE 574718334Speter && TYPE_TRANSPARENT_UNION (t) 5748146906Skan && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))) 574918334Speter { 575018334Speter TYPE_TRANSPARENT_UNION (t) = 0; 5751169699Skan warning (0, "union cannot be made transparent"); 575218334Speter } 575318334Speter 575418334Speter /* If this structure or union completes the type of any previous 575518334Speter variable declaration, lay it out and output its rtl. */ 5756132730Skan for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)); 5757132730Skan x; 5758132730Skan x = TREE_CHAIN (x)) 575918334Speter { 5760132730Skan tree decl = TREE_VALUE (x); 5761132730Skan if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) 5762132730Skan layout_array_type (TREE_TYPE (decl)); 5763132730Skan if (TREE_CODE (decl) != TYPE_DECL) 5764132730Skan { 5765132730Skan layout_decl (decl, 0); 5766132730Skan if (c_dialect_objc ()) 5767132730Skan objc_check_decl (decl); 5768169699Skan rest_of_decl_compilation (decl, toplevel, 0); 5769169699Skan if (!toplevel) 5770132730Skan expand_decl (decl); 577118334Speter } 577218334Speter } 5773132730Skan C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0; 577418334Speter 577518334Speter /* Finish debugging output for this type. */ 577618334Speter rest_of_type_compilation (t, toplevel); 577718334Speter 5778169699Skan /* If we're inside a function proper, i.e. not file-scope and not still 5779169699Skan parsing parameters, then arrange for the size of a variable sized type 5780169699Skan to be bound now. */ 5781169699Skan if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE)) 5782169699Skan add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t))); 5783169699Skan 578418334Speter return t; 578518334Speter} 578618334Speter 578718334Speter/* Lay out the type T, and its element type, and so on. */ 578818334Speter 578918334Speterstatic void 5790132730Skanlayout_array_type (tree t) 579118334Speter{ 579218334Speter if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 579318334Speter layout_array_type (TREE_TYPE (t)); 579418334Speter layout_type (t); 579518334Speter} 579618334Speter 579718334Speter/* Begin compiling the definition of an enumeration type. 579818334Speter NAME is its name (or null if anonymous). 579918334Speter Returns the type object, as yet incomplete. 580018334Speter Also records info about it so that build_enumerator 580118334Speter may be used to declare the individual values as they are read. */ 580218334Speter 580318334Spetertree 5804132730Skanstart_enum (tree name) 580518334Speter{ 580690075Sobrien tree enumtype = 0; 580718334Speter 580818334Speter /* If this is the real definition for a previous forward reference, 580918334Speter fill in the contents in the same object that used to be the 581018334Speter forward reference. */ 581118334Speter 581218334Speter if (name != 0) 5813132730Skan enumtype = lookup_tag (ENUMERAL_TYPE, name, 1); 581418334Speter 581518334Speter if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE) 581618334Speter { 581718334Speter enumtype = make_node (ENUMERAL_TYPE); 581818334Speter pushtag (name, enumtype); 581918334Speter } 582018334Speter 5821161660Skan if (C_TYPE_BEING_DEFINED (enumtype)) 5822169699Skan error ("nested redefinition of %<enum %E%>", name); 5823161660Skan 582418334Speter C_TYPE_BEING_DEFINED (enumtype) = 1; 582518334Speter 582618334Speter if (TYPE_VALUES (enumtype) != 0) 582718334Speter { 582818334Speter /* This enum is a named one that has been declared already. */ 5829169699Skan error ("redeclaration of %<enum %E%>", name); 583018334Speter 583118334Speter /* Completely replace its old definition. 583218334Speter The old enumerators remain defined, however. */ 583318334Speter TYPE_VALUES (enumtype) = 0; 583418334Speter } 583518334Speter 583618334Speter enum_next_value = integer_zero_node; 583718334Speter enum_overflow = 0; 583818334Speter 583950397Sobrien if (flag_short_enums) 584050397Sobrien TYPE_PACKED (enumtype) = 1; 584150397Sobrien 584218334Speter return enumtype; 584318334Speter} 584418334Speter 584518334Speter/* After processing and defining all the values of an enumeration type, 584618334Speter install their decls in the enumeration type and finish it off. 584718334Speter ENUMTYPE is the type object, VALUES a list of decl-value pairs, 584818334Speter and ATTRIBUTES are the specified attributes. 584918334Speter Returns ENUMTYPE. */ 585018334Speter 585118334Spetertree 5852132730Skanfinish_enum (tree enumtype, tree values, tree attributes) 585318334Speter{ 585490075Sobrien tree pair, tem; 5855169699Skan tree minnode = 0, maxnode = 0; 585690075Sobrien int precision, unsign; 5857169699Skan bool toplevel = (file_scope == current_scope); 5858169699Skan struct lang_type *lt; 585918334Speter 586090075Sobrien decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 586118334Speter 586218334Speter /* Calculate the maximum value of any enumerator in this type. */ 586318334Speter 586418334Speter if (values == error_mark_node) 586518334Speter minnode = maxnode = integer_zero_node; 586618334Speter else 586790075Sobrien { 586890075Sobrien minnode = maxnode = TREE_VALUE (values); 586990075Sobrien for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair)) 587090075Sobrien { 587190075Sobrien tree value = TREE_VALUE (pair); 587290075Sobrien if (tree_int_cst_lt (maxnode, value)) 587390075Sobrien maxnode = value; 587490075Sobrien if (tree_int_cst_lt (value, minnode)) 587590075Sobrien minnode = value; 587690075Sobrien } 587790075Sobrien } 587818334Speter 587990075Sobrien /* Construct the final type of this enumeration. It is the same 588090075Sobrien as one of the integral types - the narrowest one that fits, except 588190075Sobrien that normally we only go as narrow as int - and signed iff any of 588290075Sobrien the values are negative. */ 588390075Sobrien unsign = (tree_int_cst_sgn (minnode) >= 0); 588490075Sobrien precision = MAX (min_precision (minnode, unsign), 588590075Sobrien min_precision (maxnode, unsign)); 5886169699Skan 588750397Sobrien if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node)) 588850397Sobrien { 5889169699Skan tem = c_common_type_for_size (precision, unsign); 5890169699Skan if (tem == NULL) 589150397Sobrien { 5892169699Skan warning (0, "enumeration values exceed range of largest integer"); 5893169699Skan tem = long_long_integer_type_node; 589450397Sobrien } 589550397Sobrien } 589618334Speter else 5897169699Skan tem = unsign ? unsigned_type_node : integer_type_node; 589818334Speter 5899169699Skan TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem); 5900169699Skan TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem); 5901169699Skan TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem); 590218334Speter TYPE_SIZE (enumtype) = 0; 5903146906Skan 5904146906Skan /* If the precision of the type was specific with an attribute and it 5905146906Skan was too small, give an error. Otherwise, use it. */ 5906146906Skan if (TYPE_PRECISION (enumtype)) 5907146906Skan { 5908146906Skan if (precision > TYPE_PRECISION (enumtype)) 5909146906Skan error ("specified mode too small for enumeral values"); 5910146906Skan } 5911146906Skan else 5912169699Skan TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem); 5913146906Skan 591418334Speter layout_type (enumtype); 591518334Speter 591618334Speter if (values != error_mark_node) 591718334Speter { 591890075Sobrien /* Change the type of the enumerators to be the enum type. We 591990075Sobrien need to do this irrespective of the size of the enum, for 592090075Sobrien proper type checking. Replace the DECL_INITIALs of the 592190075Sobrien enumerators, and the value slots of the list, with copies 592290075Sobrien that have the enum type; they cannot be modified in place 592390075Sobrien because they may be shared (e.g. integer_zero_node) Finally, 592490075Sobrien change the purpose slots to point to the names of the decls. */ 592518334Speter for (pair = values; pair; pair = TREE_CHAIN (pair)) 592618334Speter { 592790075Sobrien tree enu = TREE_PURPOSE (pair); 5928169699Skan tree ini = DECL_INITIAL (enu); 592990075Sobrien 593090075Sobrien TREE_TYPE (enu) = enumtype; 593190075Sobrien 593290075Sobrien /* The ISO C Standard mandates enumerators to have type int, 593390075Sobrien even though the underlying type of an enum type is 593490075Sobrien unspecified. Here we convert any enumerators that fit in 593590075Sobrien an int to type int, to avoid promotions to unsigned types 593690075Sobrien when comparing integers with enumerators that fit in the 593790075Sobrien int range. When -pedantic is given, build_enumerator() 593890075Sobrien would have already taken care of those that don't fit. */ 5939169699Skan if (int_fits_type_p (ini, integer_type_node)) 5940169699Skan tem = integer_type_node; 594190075Sobrien else 5942169699Skan tem = enumtype; 5943169699Skan ini = convert (tem, ini); 594490075Sobrien 5945169699Skan DECL_INITIAL (enu) = ini; 594690075Sobrien TREE_PURPOSE (pair) = DECL_NAME (enu); 5947169699Skan TREE_VALUE (pair) = ini; 594818334Speter } 594918334Speter 595018334Speter TYPE_VALUES (enumtype) = values; 595118334Speter } 595218334Speter 5953169699Skan /* Record the min/max values so that we can warn about bit-field 5954169699Skan enumerations that are too small for the values. */ 5955169699Skan lt = GGC_CNEW (struct lang_type); 5956169699Skan lt->enum_min = minnode; 5957169699Skan lt->enum_max = maxnode; 5958169699Skan TYPE_LANG_SPECIFIC (enumtype) = lt; 5959169699Skan 596018334Speter /* Fix up all variant types of this enum type. */ 596118334Speter for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem)) 596218334Speter { 596390075Sobrien if (tem == enumtype) 596490075Sobrien continue; 596518334Speter TYPE_VALUES (tem) = TYPE_VALUES (enumtype); 596618334Speter TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype); 596718334Speter TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype); 596818334Speter TYPE_SIZE (tem) = TYPE_SIZE (enumtype); 596950397Sobrien TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype); 597018334Speter TYPE_MODE (tem) = TYPE_MODE (enumtype); 597118334Speter TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype); 597218334Speter TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype); 597390075Sobrien TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype); 5974169699Skan TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype); 5975169699Skan TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype); 597618334Speter } 597718334Speter 597818334Speter /* Finish debugging output for this type. */ 597918334Speter rest_of_type_compilation (enumtype, toplevel); 598018334Speter 598118334Speter return enumtype; 598218334Speter} 598318334Speter 598418334Speter/* Build and install a CONST_DECL for one value of the 598518334Speter current enumeration type (one that was begun with start_enum). 598618334Speter Return a tree-list containing the CONST_DECL and its value. 598718334Speter Assignment of sequential values by default is handled here. */ 598818334Speter 598918334Spetertree 5990132730Skanbuild_enumerator (tree name, tree value) 599118334Speter{ 599290075Sobrien tree decl, type; 599318334Speter 599418334Speter /* Validate and default VALUE. */ 599518334Speter 599618334Speter if (value != 0) 599718334Speter { 5998169699Skan /* Don't issue more errors for error_mark_node (i.e. an 5999169699Skan undeclared identifier) - just ignore the value expression. */ 6000169699Skan if (value == error_mark_node) 6001169699Skan value = 0; 6002169699Skan else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)) 6003169699Skan || TREE_CODE (value) != INTEGER_CST) 600418334Speter { 6005169699Skan error ("enumerator value for %qE is not an integer constant", name); 6006169699Skan value = 0; 600718334Speter } 600818334Speter else 600918334Speter { 6010169699Skan value = default_conversion (value); 6011169699Skan constant_expression_warning (value); 601218334Speter } 601318334Speter } 601418334Speter 601518334Speter /* Default based on previous value. */ 601618334Speter /* It should no longer be possible to have NON_LVALUE_EXPR 601718334Speter in the default. */ 601818334Speter if (value == 0) 601918334Speter { 602018334Speter value = enum_next_value; 602118334Speter if (enum_overflow) 602218334Speter error ("overflow in enumeration values"); 602318334Speter } 602418334Speter 6025169699Skan if (pedantic && !int_fits_type_p (value, integer_type_node)) 602618334Speter { 6027169699Skan pedwarn ("ISO C restricts enumerator values to range of %<int%>"); 6028169699Skan /* XXX This causes -pedantic to change the meaning of the program. 6029169699Skan Remove? -zw 2004-03-15 */ 603090075Sobrien value = convert (integer_type_node, value); 603118334Speter } 603218334Speter 603318334Speter /* Set basis for default for next value. */ 603418334Speter enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0); 603518334Speter enum_overflow = tree_int_cst_lt (enum_next_value, value); 603618334Speter 603718334Speter /* Now create a declaration for the enum value name. */ 603818334Speter 603918334Speter type = TREE_TYPE (value); 6040117421Skan type = c_common_type_for_size (MAX (TYPE_PRECISION (type), 6041117421Skan TYPE_PRECISION (integer_type_node)), 6042117421Skan (TYPE_PRECISION (type) 6043117421Skan >= TYPE_PRECISION (integer_type_node) 6044169699Skan && TYPE_UNSIGNED (type))); 604518334Speter 604618334Speter decl = build_decl (CONST_DECL, name, type); 604790075Sobrien DECL_INITIAL (decl) = convert (type, value); 604818334Speter pushdecl (decl); 604918334Speter 605090075Sobrien return tree_cons (decl, value, NULL_TREE); 605118334Speter} 605290075Sobrien 605318334Speter 605418334Speter/* Create the FUNCTION_DECL for a function definition. 605590075Sobrien DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of 605618334Speter the declaration; they describe the function's name and the type it returns, 605718334Speter but twisted together in a fashion that parallels the syntax of C. 605818334Speter 605918334Speter This function creates a binding context for the function body 606018334Speter as well as setting up the FUNCTION_DECL in current_function_decl. 606118334Speter 606218334Speter Returns 1 on success. If the DECLARATOR is not suitable for a function 606318334Speter (it defines a datum instead), we return 0, which tells 606490075Sobrien yyparse to report a parse error. */ 606518334Speter 606618334Speterint 6067169699Skanstart_function (struct c_declspecs *declspecs, struct c_declarator *declarator, 6068169699Skan tree attributes) 606918334Speter{ 607018334Speter tree decl1, old_decl; 6071169699Skan tree restype, resdecl; 6072169699Skan struct c_label_context_se *nstack_se; 6073169699Skan struct c_label_context_vm *nstack_vm; 607418334Speter 607550397Sobrien current_function_returns_value = 0; /* Assume, until we see it does. */ 607618334Speter current_function_returns_null = 0; 607796263Sobrien current_function_returns_abnormally = 0; 607818334Speter warn_about_return_type = 0; 6079169699Skan c_switch_stack = NULL; 608018334Speter 6081169699Skan nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se); 6082169699Skan nstack_se->labels_def = NULL; 6083169699Skan nstack_se->labels_used = NULL; 6084169699Skan nstack_se->next = label_context_stack_se; 6085169699Skan label_context_stack_se = nstack_se; 608618334Speter 6087169699Skan nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm); 6088169699Skan nstack_vm->labels_def = NULL; 6089169699Skan nstack_vm->labels_used = NULL; 6090169699Skan nstack_vm->scope = 0; 6091169699Skan nstack_vm->next = label_context_stack_vm; 6092169699Skan label_context_stack_vm = nstack_vm; 609318334Speter 6094169699Skan /* Indicate no valid break/continue context by setting these variables 6095169699Skan to some non-null, non-label value. We'll notice and emit the proper 6096169699Skan error message in c_finish_bc_stmt. */ 6097169699Skan c_break_label = c_cont_label = size_zero_node; 6098169699Skan 6099169699Skan decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL); 6100169699Skan 610118334Speter /* If the declarator is not suitable for a function definition, 610218334Speter cause a syntax error. */ 610318334Speter if (decl1 == 0) 610450397Sobrien { 6105169699Skan label_context_stack_se = label_context_stack_se->next; 6106169699Skan label_context_stack_vm = label_context_stack_vm->next; 610750397Sobrien return 0; 610850397Sobrien } 610918334Speter 611090075Sobrien decl_attributes (&decl1, attributes, 0); 611118334Speter 611290075Sobrien if (DECL_DECLARED_INLINE_P (decl1) 611390075Sobrien && DECL_UNINLINABLE (decl1) 611490075Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1))) 6115169699Skan warning (OPT_Wattributes, "inline function %q+D given attribute noinline", 6116169699Skan decl1); 611790075Sobrien 6118189824Sdas /* Handle gnu_inline attribute. */ 6119189824Sdas if (declspecs->inline_p 6120189824Sdas && !flag_gnu89_inline 6121189824Sdas && TREE_CODE (decl1) == FUNCTION_DECL 6122189824Sdas && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))) 6123189824Sdas { 6124189824Sdas if (declspecs->storage_class != csc_static) 6125189824Sdas DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1); 6126189824Sdas } 6127189824Sdas 612818334Speter announce_function (decl1); 612918334Speter 613090075Sobrien if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1)))) 613118334Speter { 613290075Sobrien error ("return type is an incomplete type"); 613318334Speter /* Make it return void instead. */ 613418334Speter TREE_TYPE (decl1) 613518334Speter = build_function_type (void_type_node, 613618334Speter TYPE_ARG_TYPES (TREE_TYPE (decl1))); 613718334Speter } 613818334Speter 613918334Speter if (warn_about_return_type) 6140169699Skan pedwarn_c99 ("return type defaults to %<int%>"); 614118334Speter 614218334Speter /* Make the init_value nonzero so pushdecl knows this is not tentative. 6143169699Skan error_mark_node is replaced below (in pop_scope) with the BLOCK. */ 614418334Speter DECL_INITIAL (decl1) = error_mark_node; 614518334Speter 614618334Speter /* If this definition isn't a prototype and we had a prototype declaration 6147169699Skan before, copy the arg type info from that prototype. */ 6148169699Skan old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope); 6149169699Skan if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL) 6150169699Skan old_decl = 0; 6151169699Skan current_function_prototype_locus = UNKNOWN_LOCATION; 6152169699Skan current_function_prototype_built_in = false; 6153169699Skan current_function_prototype_arg_types = NULL_TREE; 6154169699Skan if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0) 615518334Speter { 6156169699Skan if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE 6157169699Skan && comptypes (TREE_TYPE (TREE_TYPE (decl1)), 6158169699Skan TREE_TYPE (TREE_TYPE (old_decl)))) 6159169699Skan { 6160169699Skan TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl), 6161169699Skan TREE_TYPE (decl1)); 6162169699Skan current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl); 6163169699Skan current_function_prototype_built_in 6164169699Skan = C_DECL_BUILTIN_PROTOTYPE (old_decl); 6165169699Skan current_function_prototype_arg_types 6166169699Skan = TYPE_ARG_TYPES (TREE_TYPE (decl1)); 6167169699Skan } 6168169699Skan if (TREE_PUBLIC (decl1)) 6169169699Skan { 6170169699Skan /* If there is an external prototype declaration of this 6171169699Skan function, record its location but do not copy information 6172169699Skan to this decl. This may be an invisible declaration 6173169699Skan (built-in or in a scope which has finished) or simply 6174169699Skan have more refined argument types than any declaration 6175169699Skan found above. */ 6176169699Skan struct c_binding *b; 6177169699Skan for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed) 6178169699Skan if (B_IN_SCOPE (b, external_scope)) 6179169699Skan break; 6180169699Skan if (b) 6181169699Skan { 6182169699Skan tree ext_decl, ext_type; 6183169699Skan ext_decl = b->decl; 6184169699Skan ext_type = b->type ? b->type : TREE_TYPE (ext_decl); 6185169699Skan if (TREE_CODE (ext_type) == FUNCTION_TYPE 6186169699Skan && comptypes (TREE_TYPE (TREE_TYPE (decl1)), 6187169699Skan TREE_TYPE (ext_type))) 6188169699Skan { 6189169699Skan current_function_prototype_locus 6190169699Skan = DECL_SOURCE_LOCATION (ext_decl); 6191169699Skan current_function_prototype_built_in 6192169699Skan = C_DECL_BUILTIN_PROTOTYPE (ext_decl); 6193169699Skan current_function_prototype_arg_types 6194169699Skan = TYPE_ARG_TYPES (ext_type); 6195169699Skan } 6196169699Skan } 6197169699Skan } 619818334Speter } 619918334Speter 620018334Speter /* Optionally warn of old-fashioned def with no previous prototype. */ 620118334Speter if (warn_strict_prototypes 6202169699Skan && old_decl != error_mark_node 620318334Speter && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0 6204132730Skan && C_DECL_ISNT_PROTOTYPE (old_decl)) 6205169699Skan warning (OPT_Wstrict_prototypes, 6206169699Skan "function declaration isn%'t a prototype"); 620718334Speter /* Optionally warn of any global def with no previous prototype. */ 620818334Speter else if (warn_missing_prototypes 6209169699Skan && old_decl != error_mark_node 621018334Speter && TREE_PUBLIC (decl1) 6211169699Skan && !MAIN_NAME_P (DECL_NAME (decl1)) 6212132730Skan && C_DECL_ISNT_PROTOTYPE (old_decl)) 6213169699Skan warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1); 621418334Speter /* Optionally warn of any def with no previous prototype 621518334Speter if the function has already been used. */ 621618334Speter else if (warn_missing_prototypes 6217169699Skan && old_decl != 0 6218169699Skan && old_decl != error_mark_node 6219169699Skan && TREE_USED (old_decl) 622018334Speter && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0) 6221169699Skan warning (OPT_Wmissing_prototypes, 6222169699Skan "%q+D was used with no prototype before its definition", decl1); 622318334Speter /* Optionally warn of any global def with no previous declaration. */ 622418334Speter else if (warn_missing_declarations 622518334Speter && TREE_PUBLIC (decl1) 622618334Speter && old_decl == 0 6227169699Skan && !MAIN_NAME_P (DECL_NAME (decl1))) 6228169699Skan warning (OPT_Wmissing_declarations, "no previous declaration for %q+D", 6229169699Skan decl1); 623018334Speter /* Optionally warn of any def with no previous declaration 623118334Speter if the function has already been used. */ 623218334Speter else if (warn_missing_declarations 6233169699Skan && old_decl != 0 6234169699Skan && old_decl != error_mark_node 6235169699Skan && TREE_USED (old_decl) 6236132730Skan && C_DECL_IMPLICIT (old_decl)) 6237169699Skan warning (OPT_Wmissing_declarations, 6238169699Skan "%q+D was used with no declaration before its definition", decl1); 623918334Speter 624018334Speter /* This function exists in static storage. 624118334Speter (This does not mean `static' in the C sense!) */ 624218334Speter TREE_STATIC (decl1) = 1; 624318334Speter 624418334Speter /* A nested function is not global. */ 624518334Speter if (current_function_decl != 0) 624618334Speter TREE_PUBLIC (decl1) = 0; 624718334Speter 6248132730Skan /* This is the earliest point at which we might know the assembler 6249132730Skan name of the function. Thus, if it's set before this, die horribly. */ 6250169699Skan gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1)); 6251132730Skan 6252132730Skan /* If #pragma weak was used, mark the decl weak now. */ 6253169699Skan if (current_scope == file_scope) 6254132730Skan maybe_apply_pragma_weak (decl1); 6255132730Skan 625690075Sobrien /* Warn for unlikely, improbable, or stupid declarations of `main'. */ 625790075Sobrien if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1))) 625850397Sobrien { 625950397Sobrien tree args; 626050397Sobrien int argct = 0; 626150397Sobrien 626250397Sobrien if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1))) 626390075Sobrien != integer_type_node) 6264169699Skan pedwarn ("return type of %q+D is not %<int%>", decl1); 626550397Sobrien 626650397Sobrien for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args; 626750397Sobrien args = TREE_CHAIN (args)) 626850397Sobrien { 626950397Sobrien tree type = args ? TREE_VALUE (args) : 0; 627050397Sobrien 627150397Sobrien if (type == void_type_node) 627250397Sobrien break; 627350397Sobrien 627450397Sobrien ++argct; 627550397Sobrien switch (argct) 627650397Sobrien { 627750397Sobrien case 1: 627850397Sobrien if (TYPE_MAIN_VARIANT (type) != integer_type_node) 6279169699Skan pedwarn ("first argument of %q+D should be %<int%>", decl1); 628050397Sobrien break; 628150397Sobrien 628250397Sobrien case 2: 628350397Sobrien if (TREE_CODE (type) != POINTER_TYPE 628450397Sobrien || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 628550397Sobrien || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 628650397Sobrien != char_type_node)) 6287169699Skan pedwarn ("second argument of %q+D should be %<char **%>", 6288169699Skan decl1); 628950397Sobrien break; 629050397Sobrien 629150397Sobrien case 3: 629250397Sobrien if (TREE_CODE (type) != POINTER_TYPE 629350397Sobrien || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 629450397Sobrien || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 629550397Sobrien != char_type_node)) 6296169699Skan pedwarn ("third argument of %q+D should probably be " 6297169699Skan "%<char **%>", decl1); 629850397Sobrien break; 629950397Sobrien } 630050397Sobrien } 630150397Sobrien 630250397Sobrien /* It is intentional that this message does not mention the third 630390075Sobrien argument because it's only mentioned in an appendix of the 630490075Sobrien standard. */ 630550397Sobrien if (argct > 0 && (argct < 2 || argct > 3)) 6306169699Skan pedwarn ("%q+D takes only zero or two arguments", decl1); 630750397Sobrien 6308169699Skan if (!TREE_PUBLIC (decl1)) 6309169699Skan pedwarn ("%q+D is normally a non-static function", decl1); 631050397Sobrien } 631150397Sobrien 631218334Speter /* Record the decl so that the function name is defined. 631318334Speter If we already have a decl for this name, and it is a FUNCTION_DECL, 631418334Speter use the old decl. */ 631518334Speter 631618334Speter current_function_decl = pushdecl (decl1); 631718334Speter 6318169699Skan push_scope (); 6319132730Skan declare_parm_level (); 632018334Speter 632118334Speter restype = TREE_TYPE (TREE_TYPE (current_function_decl)); 632218334Speter /* Promote the value to int before returning it. */ 632390075Sobrien if (c_promoting_integer_type_p (restype)) 632418334Speter { 6325117421Skan /* It retains unsignedness if not really getting wider. */ 6326169699Skan if (TYPE_UNSIGNED (restype) 6327117421Skan && (TYPE_PRECISION (restype) 6328117421Skan == TYPE_PRECISION (integer_type_node))) 632918334Speter restype = unsigned_type_node; 633018334Speter else 633118334Speter restype = integer_type_node; 633218334Speter } 633318334Speter 6334169699Skan resdecl = build_decl (RESULT_DECL, NULL_TREE, restype); 6335169699Skan DECL_ARTIFICIAL (resdecl) = 1; 6336169699Skan DECL_IGNORED_P (resdecl) = 1; 6337169699Skan DECL_RESULT (current_function_decl) = resdecl; 633818334Speter 633990075Sobrien start_fname_decls (); 6340132730Skan 634118334Speter return 1; 634218334Speter} 634318334Speter 6344132730Skan/* Subroutine of store_parm_decls which handles new-style function 6345132730Skan definitions (prototype format). The parms already have decls, so we 6346132730Skan need only record them as in effect and complain if any redundant 6347132730Skan old-style parm decls were written. */ 6348132730Skanstatic void 6349169699Skanstore_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info) 635018334Speter{ 6351169699Skan tree decl; 635218334Speter 6353169699Skan if (current_scope->bindings) 6354132730Skan { 6355132730Skan error ("%Jold-style parameter declarations in prototyped " 6356132730Skan "function definition", fndecl); 635718334Speter 6358132730Skan /* Get rid of the old-style declarations. */ 6359169699Skan pop_scope (); 6360169699Skan push_scope (); 6361132730Skan } 6362169699Skan /* Don't issue this warning for nested functions, and don't issue this 6363169699Skan warning if we got here because ARG_INFO_TYPES was error_mark_node 6364169699Skan (this happens when a function definition has just an ellipsis in 6365169699Skan its parameter list). */ 6366169699Skan else if (!in_system_header && !current_function_scope 6367169699Skan && arg_info->types != error_mark_node) 6368169699Skan warning (OPT_Wtraditional, 6369169699Skan "%Jtraditional C rejects ISO C style function definitions", 6370169699Skan fndecl); 637118334Speter 6372132730Skan /* Now make all the parameter declarations visible in the function body. 6373132730Skan We can bypass most of the grunt work of pushdecl. */ 6374169699Skan for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl)) 6375132730Skan { 6376132730Skan DECL_CONTEXT (decl) = current_function_decl; 6377169699Skan if (DECL_NAME (decl)) 6378132730Skan { 6379169699Skan bind (DECL_NAME (decl), decl, current_scope, 6380169699Skan /*invisible=*/false, /*nested=*/false); 6381169699Skan if (!TREE_USED (decl)) 6382169699Skan warn_if_shadowing (decl); 6383132730Skan } 6384169699Skan else 6385169699Skan error ("%Jparameter name omitted", decl); 6386132730Skan } 638718334Speter 6388132730Skan /* Record the parameter list in the function declaration. */ 6389169699Skan DECL_ARGUMENTS (fndecl) = arg_info->parms; 639018334Speter 6391132730Skan /* Now make all the ancillary declarations visible, likewise. */ 6392169699Skan for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl)) 6393132730Skan { 6394132730Skan DECL_CONTEXT (decl) = current_function_decl; 6395169699Skan if (DECL_NAME (decl)) 6396169699Skan bind (DECL_NAME (decl), decl, current_scope, 6397169699Skan /*invisible=*/false, /*nested=*/false); 6398132730Skan } 639990075Sobrien 6400132730Skan /* And all the tag declarations. */ 6401169699Skan for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl)) 6402132730Skan if (TREE_PURPOSE (decl)) 6403169699Skan bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope, 6404169699Skan /*invisible=*/false, /*nested=*/false); 6405132730Skan} 640618334Speter 6407132730Skan/* Subroutine of store_parm_decls which handles old-style function 6408132730Skan definitions (separate parameter list and declarations). */ 640990075Sobrien 6410132730Skanstatic void 6411169699Skanstore_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info) 6412132730Skan{ 6413169699Skan struct c_binding *b; 6414132730Skan tree parm, decl, last; 6415169699Skan tree parmids = arg_info->parms; 6416169699Skan struct pointer_set_t *seen_args = pointer_set_create (); 641790075Sobrien 6418169699Skan if (!in_system_header) 6419169699Skan warning (OPT_Wold_style_definition, "%Jold-style function definition", 6420169699Skan fndecl); 642118334Speter 6422132730Skan /* Match each formal parameter name with its declaration. Save each 6423132730Skan decl in the appropriate TREE_PURPOSE slot of the parmids chain. */ 6424132730Skan for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 6425132730Skan { 6426132730Skan if (TREE_VALUE (parm) == 0) 642718334Speter { 6428132730Skan error ("%Jparameter name missing from parameter list", fndecl); 6429132730Skan TREE_PURPOSE (parm) = 0; 6430132730Skan continue; 643118334Speter } 643218334Speter 6433169699Skan b = I_SYMBOL_BINDING (TREE_VALUE (parm)); 6434169699Skan if (b && B_IN_CURRENT_SCOPE (b)) 643518334Speter { 6436169699Skan decl = b->decl; 6437132730Skan /* If we got something other than a PARM_DECL it is an error. */ 6438132730Skan if (TREE_CODE (decl) != PARM_DECL) 6439169699Skan error ("%q+D declared as a non-parameter", decl); 6440132730Skan /* If the declaration is already marked, we have a duplicate 6441132730Skan name. Complain and ignore the duplicate. */ 6442169699Skan else if (pointer_set_contains (seen_args, decl)) 644318334Speter { 6444169699Skan error ("multiple parameters named %q+D", decl); 6445132730Skan TREE_PURPOSE (parm) = 0; 6446132730Skan continue; 644718334Speter } 6448132730Skan /* If the declaration says "void", complain and turn it into 6449132730Skan an int. */ 6450132730Skan else if (VOID_TYPE_P (TREE_TYPE (decl))) 645118334Speter { 6452169699Skan error ("parameter %q+D declared with void type", decl); 6453132730Skan TREE_TYPE (decl) = integer_type_node; 6454132730Skan DECL_ARG_TYPE (decl) = integer_type_node; 6455132730Skan layout_decl (decl, 0); 645618334Speter } 6457169699Skan warn_if_shadowing (decl); 645818334Speter } 6459132730Skan /* If no declaration found, default to int. */ 6460132730Skan else 646118334Speter { 6462132730Skan decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node); 6463132730Skan DECL_ARG_TYPE (decl) = TREE_TYPE (decl); 6464132730Skan DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl); 6465132730Skan pushdecl (decl); 6466169699Skan warn_if_shadowing (decl); 646718334Speter 6468132730Skan if (flag_isoc99) 6469169699Skan pedwarn ("type of %q+D defaults to %<int%>", decl); 6470132730Skan else if (extra_warnings) 6471169699Skan warning (OPT_Wextra, "type of %q+D defaults to %<int%>", decl); 647218334Speter } 647318334Speter 6474132730Skan TREE_PURPOSE (parm) = decl; 6475169699Skan pointer_set_insert (seen_args, decl); 647618334Speter } 647718334Speter 6478132730Skan /* Now examine the parms chain for incomplete declarations 6479132730Skan and declarations with no corresponding names. */ 648018334Speter 6481169699Skan for (b = current_scope->bindings; b; b = b->prev) 6482132730Skan { 6483169699Skan parm = b->decl; 6484169699Skan if (TREE_CODE (parm) != PARM_DECL) 6485169699Skan continue; 6486169699Skan 6487169699Skan if (TREE_TYPE (parm) != error_mark_node 6488169699Skan && !COMPLETE_TYPE_P (TREE_TYPE (parm))) 6489132730Skan { 6490169699Skan error ("parameter %q+D has incomplete type", parm); 6491132730Skan TREE_TYPE (parm) = error_mark_node; 6492132730Skan } 649318334Speter 6494169699Skan if (!pointer_set_contains (seen_args, parm)) 649518334Speter { 6496169699Skan error ("declaration for parameter %q+D but no such parameter", parm); 649718334Speter 6498132730Skan /* Pretend the parameter was not missing. 6499132730Skan This gets us to a standard state and minimizes 6500132730Skan further error messages. */ 6501132730Skan parmids = chainon (parmids, tree_cons (parm, 0, 0)); 6502132730Skan } 6503132730Skan } 650418334Speter 6505132730Skan /* Chain the declarations together in the order of the list of 6506132730Skan names. Store that chain in the function decl, replacing the 6507132730Skan list of names. Update the current scope to match. */ 6508132730Skan DECL_ARGUMENTS (fndecl) = 0; 650918334Speter 6510132730Skan for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 6511132730Skan if (TREE_PURPOSE (parm)) 6512132730Skan break; 6513132730Skan if (parm && TREE_PURPOSE (parm)) 6514132730Skan { 6515132730Skan last = TREE_PURPOSE (parm); 6516132730Skan DECL_ARGUMENTS (fndecl) = last; 651718334Speter 6518132730Skan for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm)) 6519132730Skan if (TREE_PURPOSE (parm)) 6520132730Skan { 6521132730Skan TREE_CHAIN (last) = TREE_PURPOSE (parm); 6522132730Skan last = TREE_PURPOSE (parm); 6523132730Skan } 6524132730Skan TREE_CHAIN (last) = 0; 6525132730Skan } 652618334Speter 6527169699Skan pointer_set_destroy (seen_args); 6528169699Skan 6529132730Skan /* If there was a previous prototype, 6530132730Skan set the DECL_ARG_TYPE of each argument according to 6531132730Skan the type previously specified, and report any mismatches. */ 653218334Speter 6533169699Skan if (current_function_prototype_arg_types) 6534132730Skan { 6535132730Skan tree type; 6536132730Skan for (parm = DECL_ARGUMENTS (fndecl), 6537169699Skan type = current_function_prototype_arg_types; 6538132730Skan parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) 6539132730Skan != void_type_node)); 6540132730Skan parm = TREE_CHAIN (parm), type = TREE_CHAIN (type)) 654118334Speter { 6542132730Skan if (parm == 0 || type == 0 6543132730Skan || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node) 654418334Speter { 6545169699Skan if (current_function_prototype_built_in) 6546169699Skan warning (0, "number of arguments doesn%'t match " 6547169699Skan "built-in prototype"); 6548169699Skan else 6549169699Skan { 6550169699Skan error ("number of arguments doesn%'t match prototype"); 6551169699Skan error ("%Hprototype declaration", 6552169699Skan ¤t_function_prototype_locus); 6553169699Skan } 6554132730Skan break; 655518334Speter } 6556132730Skan /* Type for passing arg must be consistent with that 6557132730Skan declared for the arg. ISO C says we take the unqualified 6558132730Skan type for parameters declared with qualified type. */ 6559169699Skan if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)), 6560169699Skan TYPE_MAIN_VARIANT (TREE_VALUE (type)))) 656118334Speter { 6562132730Skan if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) 6563132730Skan == TYPE_MAIN_VARIANT (TREE_VALUE (type))) 656418334Speter { 6565132730Skan /* Adjust argument to match prototype. E.g. a previous 6566132730Skan `int foo(float);' prototype causes 6567132730Skan `int foo(x) float x; {...}' to be treated like 6568132730Skan `int foo(float x) {...}'. This is particularly 6569132730Skan useful for argument types like uid_t. */ 6570132730Skan DECL_ARG_TYPE (parm) = TREE_TYPE (parm); 657190075Sobrien 6572132730Skan if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl)) 6573132730Skan && INTEGRAL_TYPE_P (TREE_TYPE (parm)) 6574132730Skan && TYPE_PRECISION (TREE_TYPE (parm)) 6575132730Skan < TYPE_PRECISION (integer_type_node)) 6576132730Skan DECL_ARG_TYPE (parm) = integer_type_node; 657790075Sobrien 6578132730Skan if (pedantic) 657918334Speter { 6580169699Skan /* ??? Is it possible to get here with a 6581169699Skan built-in prototype or will it always have 6582169699Skan been diagnosed as conflicting with an 6583169699Skan old-style definition and discarded? */ 6584169699Skan if (current_function_prototype_built_in) 6585169699Skan warning (0, "promoted argument %qD " 6586169699Skan "doesn%'t match built-in prototype", parm); 6587169699Skan else 6588169699Skan { 6589169699Skan pedwarn ("promoted argument %qD " 6590169699Skan "doesn%'t match prototype", parm); 6591169699Skan pedwarn ("%Hprototype declaration", 6592169699Skan ¤t_function_prototype_locus); 6593169699Skan } 659418334Speter } 659518334Speter } 6596132730Skan else 6597132730Skan { 6598169699Skan if (current_function_prototype_built_in) 6599169699Skan warning (0, "argument %qD doesn%'t match " 6600169699Skan "built-in prototype", parm); 6601169699Skan else 6602169699Skan { 6603169699Skan error ("argument %qD doesn%'t match prototype", parm); 6604169699Skan error ("%Hprototype declaration", 6605169699Skan ¤t_function_prototype_locus); 6606169699Skan } 6607132730Skan } 660818334Speter } 660918334Speter } 6610132730Skan TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0; 6611132730Skan } 661218334Speter 6613132730Skan /* Otherwise, create a prototype that would match. */ 661418334Speter 6615132730Skan else 6616132730Skan { 6617132730Skan tree actual = 0, last = 0, type; 6618132730Skan 6619132730Skan for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm)) 662018334Speter { 6621132730Skan type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE); 662218334Speter if (last) 662318334Speter TREE_CHAIN (last) = type; 662418334Speter else 662518334Speter actual = type; 6626132730Skan last = type; 6627132730Skan } 6628132730Skan type = tree_cons (NULL_TREE, void_type_node, NULL_TREE); 6629132730Skan if (last) 6630132730Skan TREE_CHAIN (last) = type; 6631132730Skan else 6632132730Skan actual = type; 663318334Speter 6634132730Skan /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES 6635132730Skan of the type of this function, but we need to avoid having this 6636132730Skan affect the types of other similarly-typed functions, so we must 6637132730Skan first force the generation of an identical (but separate) type 6638132730Skan node for the relevant function type. The new node we create 6639132730Skan will be a variant of the main variant of the original function 6640132730Skan type. */ 664118334Speter 6642169699Skan TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl)); 664318334Speter 6644132730Skan TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual; 6645132730Skan } 6646132730Skan} 664718334Speter 6648169699Skan/* Store parameter declarations passed in ARG_INFO into the current 6649169699Skan function declaration. */ 6650169699Skan 6651169699Skanvoid 6652169699Skanstore_parm_decls_from (struct c_arg_info *arg_info) 6653169699Skan{ 6654169699Skan current_function_arg_info = arg_info; 6655169699Skan store_parm_decls (); 6656169699Skan} 6657169699Skan 6658132730Skan/* Store the parameter declarations into the current function declaration. 6659132730Skan This is called after parsing the parameter declarations, before 6660132730Skan digesting the body of the function. 666118334Speter 6662132730Skan For an old-style definition, construct a prototype out of the old-style 6663132730Skan parameter declarations and inject it into the function's type. */ 666418334Speter 6665132730Skanvoid 6666132730Skanstore_parm_decls (void) 6667132730Skan{ 6668132730Skan tree fndecl = current_function_decl; 6669169699Skan bool proto; 667018334Speter 6671169699Skan /* The argument information block for FNDECL. */ 6672169699Skan struct c_arg_info *arg_info = current_function_arg_info; 6673169699Skan current_function_arg_info = 0; 667418334Speter 6675169699Skan /* True if this definition is written with a prototype. Note: 6676169699Skan despite C99 6.7.5.3p14, we can *not* treat an empty argument 6677169699Skan list in a function definition as equivalent to (void) -- an 6678169699Skan empty argument list specifies the function has no parameters, 6679169699Skan but only (void) sets up a prototype for future calls. */ 6680169699Skan proto = arg_info->types != 0; 6681169699Skan 6682169699Skan if (proto) 6683169699Skan store_parm_decls_newstyle (fndecl, arg_info); 6684132730Skan else 6685169699Skan store_parm_decls_oldstyle (fndecl, arg_info); 6686132730Skan 6687169699Skan /* The next call to push_scope will be a function body. */ 6688132730Skan 6689132730Skan next_is_function_body = true; 6690132730Skan 669118334Speter /* Write a record describing this function definition to the prototypes 669218334Speter file (if requested). */ 669318334Speter 6694169699Skan gen_aux_info_record (fndecl, 1, 0, proto); 669518334Speter 669618334Speter /* Initialize the RTL code for the function. */ 6697132730Skan allocate_struct_function (fndecl); 669818334Speter 669990075Sobrien /* Begin the statement tree for this function. */ 6700169699Skan DECL_SAVED_TREE (fndecl) = push_stmt_list (); 670118334Speter 6702169699Skan /* ??? Insert the contents of the pending sizes list into the function 6703169699Skan to be evaluated. The only reason left to have this is 6704169699Skan void foo(int n, int array[n++]) 6705169699Skan because we throw away the array type in favor of a pointer type, and 6706169699Skan thus won't naturally see the SAVE_EXPR containing the increment. All 6707169699Skan other pending sizes would be handled by gimplify_parameters. */ 6708169699Skan { 6709169699Skan tree t; 6710169699Skan for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t)) 6711169699Skan add_stmt (TREE_VALUE (t)); 6712169699Skan } 671318334Speter 671490075Sobrien /* Even though we're inside a function body, we still don't want to 671590075Sobrien call expand_expr to calculate the size of a variable-sized array. 671690075Sobrien We haven't necessarily assigned RTL to all variables yet, so it's 671790075Sobrien not safe to try to expand expressions involving them. */ 671890075Sobrien cfun->x_dont_save_pending_sizes_p = 1; 671918334Speter} 672018334Speter 6721169699Skan/* Emit diagnostics that require gimple input for detection. Operate on 6722169699Skan FNDECL and all its nested functions. */ 6723169699Skan 6724169699Skanstatic void 6725169699Skanc_gimple_diagnostics_recursively (tree fndecl) 6726169699Skan{ 6727169699Skan struct cgraph_node *cgn; 6728169699Skan 6729169699Skan /* Handle attribute((warn_unused_result)). Relies on gimple input. */ 6730169699Skan c_warn_unused_result (&DECL_SAVED_TREE (fndecl)); 6731169699Skan 6732169699Skan /* Notice when OpenMP structured block constraints are violated. */ 6733169699Skan if (flag_openmp) 6734169699Skan diagnose_omp_structured_block_errors (fndecl); 6735169699Skan 6736169699Skan /* Finalize all nested functions now. */ 6737169699Skan cgn = cgraph_node (fndecl); 6738169699Skan for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested) 6739169699Skan c_gimple_diagnostics_recursively (cgn->decl); 6740169699Skan} 6741169699Skan 674218334Speter/* Finish up a function declaration and compile that function 674318334Speter all the way to assembler language output. The free the storage 674418334Speter for the function definition. 674518334Speter 6746132730Skan This is called after parsing the body of the function definition. */ 674718334Speter 674818334Spetervoid 6749132730Skanfinish_function (void) 675018334Speter{ 675190075Sobrien tree fndecl = current_function_decl; 675218334Speter 6753169699Skan label_context_stack_se = label_context_stack_se->next; 6754169699Skan label_context_stack_vm = label_context_stack_vm->next; 675518334Speter 6756132730Skan if (TREE_CODE (fndecl) == FUNCTION_DECL 6757132730Skan && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))) 6758132730Skan { 6759132730Skan tree args = DECL_ARGUMENTS (fndecl); 6760132730Skan for (; args; args = TREE_CHAIN (args)) 6761169699Skan { 6762169699Skan tree type = TREE_TYPE (args); 6763169699Skan if (INTEGRAL_TYPE_P (type) 6764169699Skan && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 6765169699Skan DECL_ARG_TYPE (args) = integer_type_node; 6766169699Skan } 6767132730Skan } 6768132730Skan 6769132730Skan if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) 6770132730Skan BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; 6771132730Skan 677218334Speter /* Must mark the RESULT_DECL as being in this function. */ 677318334Speter 6774132730Skan if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node) 6775132730Skan DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; 677618334Speter 677790075Sobrien if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted) 677818334Speter { 677918334Speter if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))) 678018334Speter != integer_type_node) 678150397Sobrien { 678252284Sobrien /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned. 678390075Sobrien If warn_main is -1 (-Wno-main) we don't want to be warned. */ 6784132730Skan if (!warn_main) 6785169699Skan pedwarn ("return type of %q+D is not %<int%>", fndecl); 678650397Sobrien } 678718334Speter else 678818334Speter { 6789169699Skan if (flag_isoc99) 6790169699Skan { 6791169699Skan tree stmt = c_finish_return (integer_zero_node); 6792169699Skan#ifdef USE_MAPPED_LOCATION 6793169699Skan /* Hack. We don't want the middle-end to warn that this return 6794169699Skan is unreachable, so we mark its location as special. Using 6795169699Skan UNKNOWN_LOCATION has the problem that it gets clobbered in 6796169699Skan annotate_one_with_locus. A cleaner solution might be to 6797169699Skan ensure ! should_carry_locus_p (stmt), but that needs a flag. 6798169699Skan */ 6799169699Skan SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION); 680090075Sobrien#else 6801169699Skan /* Hack. We don't want the middle-end to warn that this 6802169699Skan return is unreachable, so put the statement on the 6803169699Skan special line 0. */ 6804169699Skan annotate_with_file_line (stmt, input_filename, 0); 680550397Sobrien#endif 6806169699Skan } 680718334Speter } 680818334Speter } 6809132730Skan 6810169699Skan /* Tie off the statement tree for this function. */ 6811169699Skan DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl)); 6812169699Skan 681390075Sobrien finish_fname_decls (); 681418334Speter 681596263Sobrien /* Complain if there's just no return statement. */ 681696263Sobrien if (warn_return_type 681796263Sobrien && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE 681896263Sobrien && !current_function_returns_value && !current_function_returns_null 6819169699Skan /* Don't complain if we are no-return. */ 682096263Sobrien && !current_function_returns_abnormally 682196263Sobrien /* Don't warn for main(). */ 682296263Sobrien && !MAIN_NAME_P (DECL_NAME (fndecl)) 682396263Sobrien /* Or if they didn't actually specify a return type. */ 682496263Sobrien && !C_FUNCTION_IMPLICIT_INT (fndecl) 682596263Sobrien /* Normally, with -Wreturn-type, flow will complain. Unless we're an 682696263Sobrien inline function, as we might never be compiled separately. */ 682796263Sobrien && DECL_INLINE (fndecl)) 6828169699Skan { 6829169699Skan warning (OPT_Wreturn_type, 6830169699Skan "no return statement in function returning non-void"); 6831169699Skan TREE_NO_WARNING (fndecl) = 1; 6832169699Skan } 683396263Sobrien 6834132730Skan /* With just -Wextra, complain only if function returns both with 6835122196Skan and without a value. */ 6836122196Skan if (extra_warnings 6837122196Skan && current_function_returns_value 6838122196Skan && current_function_returns_null) 6839169699Skan warning (OPT_Wextra, "this function may return with or without a value"); 6840122196Skan 6841169699Skan /* Store the end of the function, so that we get good line number 6842169699Skan info for the epilogue. */ 6843169699Skan cfun->function_end_locus = input_location; 684490075Sobrien 6845169699Skan /* If we don't have ctors/dtors sections, and this is a static 6846169699Skan constructor or destructor, it must be recorded now. */ 6847169699Skan if (DECL_STATIC_CONSTRUCTOR (fndecl) 6848169699Skan && !targetm.have_ctors_dtors) 6849169699Skan static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors); 6850169699Skan if (DECL_STATIC_DESTRUCTOR (fndecl) 6851169699Skan && !targetm.have_ctors_dtors) 6852169699Skan static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors); 685390075Sobrien 6854169699Skan /* Finalize the ELF visibility for the function. */ 6855169699Skan c_determine_visibility (fndecl); 685690075Sobrien 6857169699Skan /* Genericize before inlining. Delay genericizing nested functions 6858169699Skan until their parent function is genericized. Since finalizing 6859169699Skan requires GENERIC, delay that as well. */ 6860169699Skan 6861169699Skan if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node 6862169699Skan && !undef_nested_function) 686390075Sobrien { 6864169699Skan if (!decl_function_context (fndecl)) 6865132730Skan { 6866169699Skan c_genericize (fndecl); 6867169699Skan c_gimple_diagnostics_recursively (fndecl); 686890075Sobrien 6869169699Skan /* ??? Objc emits functions after finalizing the compilation unit. 6870169699Skan This should be cleaned up later and this conditional removed. */ 6871169699Skan if (cgraph_global_info_ready) 6872169699Skan { 6873169699Skan c_expand_body (fndecl); 6874169699Skan return; 6875169699Skan } 687690075Sobrien 6877169699Skan cgraph_finalize_function (fndecl, false); 6878169699Skan } 687918334Speter else 6880169699Skan { 6881169699Skan /* Register this function with cgraph just far enough to get it 6882169699Skan added to our parent's nested function list. Handy, since the 6883169699Skan C front end doesn't have such a list. */ 6884169699Skan (void) cgraph_node (fndecl); 6885169699Skan } 688618334Speter } 688790075Sobrien 6888169699Skan if (!decl_function_context (fndecl)) 6889169699Skan undef_nested_function = false; 6890169699Skan 6891169699Skan /* We're leaving the context of this function, so zap cfun. 6892169699Skan It's still in DECL_STRUCT_FUNCTION, and we'll restore it in 6893169699Skan tree_rest_of_compilation. */ 6894169699Skan cfun = NULL; 6895169699Skan current_function_decl = NULL; 6896132730Skan} 689718334Speter 6898169699Skan/* Generate the RTL for the body of FNDECL. */ 6899117421Skan 6900132730Skanvoid 6901132730Skanc_expand_body (tree fndecl) 6902132730Skan{ 6903132730Skan 6904169699Skan if (!DECL_INITIAL (fndecl) 6905169699Skan || DECL_INITIAL (fndecl) == error_mark_node) 6906169699Skan return; 6907169699Skan 6908169699Skan tree_rest_of_compilation (fndecl); 6909169699Skan 6910169699Skan if (DECL_STATIC_CONSTRUCTOR (fndecl) 6911169699Skan && targetm.have_ctors_dtors) 6912169699Skan targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0), 6913169699Skan DEFAULT_INIT_PRIORITY); 6914169699Skan if (DECL_STATIC_DESTRUCTOR (fndecl) 6915169699Skan && targetm.have_ctors_dtors) 6916169699Skan targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0), 6917169699Skan DEFAULT_INIT_PRIORITY); 691890075Sobrien} 691990075Sobrien 692090075Sobrien/* Check the declarations given in a for-loop for satisfying the C99 6921169699Skan constraints. If exactly one such decl is found, return it. */ 6922169699Skan 6923169699Skantree 6924132730Skancheck_for_loop_decls (void) 692590075Sobrien{ 6926169699Skan struct c_binding *b; 6927169699Skan tree one_decl = NULL_TREE; 6928169699Skan int n_decls = 0; 692990075Sobrien 6930169699Skan 693190075Sobrien if (!flag_isoc99) 693218334Speter { 693390075Sobrien /* If we get here, declarations have been used in a for loop without 693490075Sobrien the C99 for loop scope. This doesn't make much sense, so don't 693590075Sobrien allow it. */ 6936169699Skan error ("%<for%> loop initial declaration used outside C99 mode"); 6937169699Skan return NULL_TREE; 693818334Speter } 693990075Sobrien /* C99 subclause 6.8.5 paragraph 3: 694090075Sobrien 694190075Sobrien [#3] The declaration part of a for statement shall only 694290075Sobrien declare identifiers for objects having storage class auto or 694390075Sobrien register. 694490075Sobrien 694590075Sobrien It isn't clear whether, in this sentence, "identifiers" binds to 694690075Sobrien "shall only declare" or to "objects" - that is, whether all identifiers 694790075Sobrien declared must be identifiers for objects, or whether the restriction 694890075Sobrien only applies to those that are. (A question on this in comp.std.c 694990075Sobrien in November 2000 received no answer.) We implement the strictest 695090075Sobrien interpretation, to avoid creating an extension which later causes 695190075Sobrien problems. */ 695290075Sobrien 6953169699Skan for (b = current_scope->bindings; b; b = b->prev) 695490075Sobrien { 6955169699Skan tree id = b->id; 6956169699Skan tree decl = b->decl; 6957132730Skan 6958169699Skan if (!id) 6959169699Skan continue; 6960169699Skan 6961169699Skan switch (TREE_CODE (decl)) 6962169699Skan { 6963169699Skan case VAR_DECL: 6964169699Skan if (TREE_STATIC (decl)) 6965169699Skan error ("declaration of static variable %q+D in %<for%> loop " 6966169699Skan "initial declaration", decl); 6967169699Skan else if (DECL_EXTERNAL (decl)) 6968169699Skan error ("declaration of %<extern%> variable %q+D in %<for%> loop " 6969169699Skan "initial declaration", decl); 6970169699Skan break; 6971169699Skan 6972169699Skan case RECORD_TYPE: 6973169699Skan error ("%<struct %E%> declared in %<for%> loop initial declaration", 6974169699Skan id); 6975169699Skan break; 6976169699Skan case UNION_TYPE: 6977169699Skan error ("%<union %E%> declared in %<for%> loop initial declaration", 6978169699Skan id); 6979169699Skan break; 6980169699Skan case ENUMERAL_TYPE: 6981169699Skan error ("%<enum %E%> declared in %<for%> loop initial declaration", 6982169699Skan id); 6983169699Skan break; 6984169699Skan default: 6985169699Skan error ("declaration of non-variable %q+D in %<for%> loop " 6986169699Skan "initial declaration", decl); 6987169699Skan } 6988169699Skan 6989169699Skan n_decls++; 6990169699Skan one_decl = decl; 699190075Sobrien } 699290075Sobrien 6993169699Skan return n_decls == 1 ? one_decl : NULL_TREE; 699418334Speter} 699518334Speter 699618334Speter/* Save and reinitialize the variables 699718334Speter used during compilation of a C function. */ 699818334Speter 699918334Spetervoid 7000132730Skanc_push_function_context (struct function *f) 700118334Speter{ 7002117421Skan struct language_function *p; 7003169699Skan p = GGC_NEW (struct language_function); 7004117421Skan f->language = p; 700518334Speter 700690075Sobrien p->base.x_stmt_tree = c_stmt_tree; 7007169699Skan p->x_break_label = c_break_label; 7008169699Skan p->x_cont_label = c_cont_label; 7009169699Skan p->x_switch_stack = c_switch_stack; 7010169699Skan p->arg_info = current_function_arg_info; 701118334Speter p->returns_value = current_function_returns_value; 701218334Speter p->returns_null = current_function_returns_null; 701396263Sobrien p->returns_abnormally = current_function_returns_abnormally; 701418334Speter p->warn_about_return_type = warn_about_return_type; 701518334Speter} 701618334Speter 701718334Speter/* Restore the variables used during compilation of a C function. */ 701818334Speter 701918334Spetervoid 7020132730Skanc_pop_function_context (struct function *f) 702118334Speter{ 7022117421Skan struct language_function *p = f->language; 702318334Speter 7024169699Skan if (DECL_STRUCT_FUNCTION (current_function_decl) == 0 702590075Sobrien && DECL_SAVED_TREE (current_function_decl) == NULL_TREE) 702618334Speter { 702718334Speter /* Stop pointing to the local nodes about to be freed. */ 702818334Speter /* But DECL_INITIAL must remain nonzero so we know this 702918334Speter was an actual function definition. */ 703018334Speter DECL_INITIAL (current_function_decl) = error_mark_node; 703118334Speter DECL_ARGUMENTS (current_function_decl) = 0; 703218334Speter } 703318334Speter 703490075Sobrien c_stmt_tree = p->base.x_stmt_tree; 7035169699Skan c_break_label = p->x_break_label; 7036169699Skan c_cont_label = p->x_cont_label; 7037169699Skan c_switch_stack = p->x_switch_stack; 7038169699Skan current_function_arg_info = p->arg_info; 703918334Speter current_function_returns_value = p->returns_value; 704018334Speter current_function_returns_null = p->returns_null; 704196263Sobrien current_function_returns_abnormally = p->returns_abnormally; 704218334Speter warn_about_return_type = p->warn_about_return_type; 704318334Speter 7044117421Skan f->language = NULL; 704518334Speter} 704618334Speter 7047117421Skan/* Copy the DECL_LANG_SPECIFIC data associated with DECL. */ 704818334Speter 704918334Spetervoid 7050132730Skanc_dup_lang_specific_decl (tree decl) 705190075Sobrien{ 705290075Sobrien struct lang_decl *ld; 705390075Sobrien 705490075Sobrien if (!DECL_LANG_SPECIFIC (decl)) 705590075Sobrien return; 705690075Sobrien 7057169699Skan ld = GGC_NEW (struct lang_decl); 7058132730Skan memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl)); 705990075Sobrien DECL_LANG_SPECIFIC (decl) = ld; 706090075Sobrien} 706190075Sobrien 706290075Sobrien/* The functions below are required for functionality of doing 706390075Sobrien function at once processing in the C front end. Currently these 706490075Sobrien functions are not called from anywhere in the C front end, but as 706590075Sobrien these changes continue, that will change. */ 706690075Sobrien 706790075Sobrien/* Returns the stmt_tree (if any) to which statements are currently 706890075Sobrien being added. If there is no active statement-tree, NULL is 706990075Sobrien returned. */ 707090075Sobrien 707190075Sobrienstmt_tree 7072132730Skancurrent_stmt_tree (void) 707390075Sobrien{ 707490075Sobrien return &c_stmt_tree; 707590075Sobrien} 707690075Sobrien 707790075Sobrien/* Nonzero if TYPE is an anonymous union or struct type. Always 0 in 707890075Sobrien C. */ 707990075Sobrien 708090075Sobrienint 7081169699Skananon_aggr_type_p (tree ARG_UNUSED (node)) 708218334Speter{ 708390075Sobrien return 0; 708418334Speter} 708590075Sobrien 7086132730Skan/* Return the global value of T as a symbol. */ 708790075Sobrien 708890075Sobrientree 7089132730Skanidentifier_global_value (tree t) 709090075Sobrien{ 7091169699Skan struct c_binding *b; 7092132730Skan 7093169699Skan for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed) 7094169699Skan if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b)) 7095169699Skan return b->decl; 7096132730Skan 7097132730Skan return 0; 709890075Sobrien} 709990075Sobrien 710090075Sobrien/* Record a builtin type for C. If NAME is non-NULL, it is the name used; 710190075Sobrien otherwise the name is found in ridpointers from RID_INDEX. */ 710290075Sobrien 710390075Sobrienvoid 7104132730Skanrecord_builtin_type (enum rid rid_index, const char *name, tree type) 710590075Sobrien{ 7106169699Skan tree id, decl; 710790075Sobrien if (name == 0) 710890075Sobrien id = ridpointers[(int) rid_index]; 710990075Sobrien else 711090075Sobrien id = get_identifier (name); 7111169699Skan decl = build_decl (TYPE_DECL, id, type); 7112169699Skan pushdecl (decl); 7113169699Skan if (debug_hooks->type_decl) 7114169699Skan debug_hooks->type_decl (decl, false); 711590075Sobrien} 711690075Sobrien 711790075Sobrien/* Build the void_list_node (void_type_node having been created). */ 711890075Sobrientree 7119132730Skanbuild_void_list_node (void) 712090075Sobrien{ 712190075Sobrien tree t = build_tree_list (NULL_TREE, void_type_node); 712290075Sobrien return t; 712390075Sobrien} 7124117421Skan 7125169699Skan/* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */ 7126117421Skan 7127169699Skanstruct c_parm * 7128169699Skanbuild_c_parm (struct c_declspecs *specs, tree attrs, 7129169699Skan struct c_declarator *declarator) 7130169699Skan{ 7131169699Skan struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm); 7132169699Skan ret->specs = specs; 7133169699Skan ret->attrs = attrs; 7134169699Skan ret->declarator = declarator; 7135169699Skan return ret; 7136169699Skan} 7137117421Skan 7138169699Skan/* Return a declarator with nested attributes. TARGET is the inner 7139169699Skan declarator to which these attributes apply. ATTRS are the 7140169699Skan attributes. */ 7141169699Skan 7142169699Skanstruct c_declarator * 7143169699Skanbuild_attrs_declarator (tree attrs, struct c_declarator *target) 7144117421Skan{ 7145169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7146169699Skan ret->kind = cdk_attrs; 7147169699Skan ret->declarator = target; 7148169699Skan ret->u.attrs = attrs; 7149169699Skan return ret; 7150117421Skan} 7151117421Skan 7152169699Skan/* Return a declarator for a function with arguments specified by ARGS 7153169699Skan and return type specified by TARGET. */ 7154132730Skan 7155169699Skanstruct c_declarator * 7156169699Skanbuild_function_declarator (struct c_arg_info *args, 7157169699Skan struct c_declarator *target) 7158132730Skan{ 7159169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7160169699Skan ret->kind = cdk_function; 7161169699Skan ret->declarator = target; 7162169699Skan ret->u.arg_info = args; 7163169699Skan return ret; 7164132730Skan} 7165132730Skan 7166169699Skan/* Return a declarator for the identifier IDENT (which may be 7167169699Skan NULL_TREE for an abstract declarator). */ 7168132730Skan 7169169699Skanstruct c_declarator * 7170169699Skanbuild_id_declarator (tree ident) 7171132730Skan{ 7172169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7173169699Skan ret->kind = cdk_id; 7174169699Skan ret->declarator = 0; 7175169699Skan ret->u.id = ident; 7176169699Skan /* Default value - may get reset to a more precise location. */ 7177169699Skan ret->id_loc = input_location; 7178169699Skan return ret; 7179132730Skan} 7180132730Skan 7181169699Skan/* Return something to represent absolute declarators containing a *. 7182169699Skan TARGET is the absolute declarator that the * contains. 7183169699Skan TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes 7184169699Skan to apply to the pointer type. */ 7185169699Skan 7186169699Skanstruct c_declarator * 7187169699Skanmake_pointer_declarator (struct c_declspecs *type_quals_attrs, 7188169699Skan struct c_declarator *target) 7189132730Skan{ 7190169699Skan tree attrs; 7191169699Skan int quals = 0; 7192169699Skan struct c_declarator *itarget = target; 7193169699Skan struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator); 7194169699Skan if (type_quals_attrs) 7195169699Skan { 7196169699Skan attrs = type_quals_attrs->attrs; 7197169699Skan quals = quals_from_declspecs (type_quals_attrs); 7198169699Skan if (attrs != NULL_TREE) 7199169699Skan itarget = build_attrs_declarator (attrs, target); 7200169699Skan } 7201169699Skan ret->kind = cdk_pointer; 7202169699Skan ret->declarator = itarget; 7203169699Skan ret->u.pointer_quals = quals; 7204169699Skan return ret; 7205132730Skan} 7206132730Skan 7207169699Skan/* Return a pointer to a structure for an empty list of declaration 7208169699Skan specifiers. */ 7209132730Skan 7210169699Skanstruct c_declspecs * 7211169699Skanbuild_null_declspecs (void) 7212132730Skan{ 7213169699Skan struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs); 7214169699Skan ret->type = 0; 7215169699Skan ret->decl_attr = 0; 7216169699Skan ret->attrs = 0; 7217169699Skan ret->typespec_word = cts_none; 7218169699Skan ret->storage_class = csc_none; 7219169699Skan ret->declspecs_seen_p = false; 7220169699Skan ret->type_seen_p = false; 7221169699Skan ret->non_sc_seen_p = false; 7222169699Skan ret->typedef_p = false; 7223169699Skan ret->tag_defined_p = false; 7224169699Skan ret->explicit_signed_p = false; 7225169699Skan ret->deprecated_p = false; 7226169699Skan ret->default_int_p = false; 7227169699Skan ret->long_p = false; 7228169699Skan ret->long_long_p = false; 7229169699Skan ret->short_p = false; 7230169699Skan ret->signed_p = false; 7231169699Skan ret->unsigned_p = false; 7232169699Skan ret->complex_p = false; 7233169699Skan ret->inline_p = false; 7234169699Skan ret->thread_p = false; 7235169699Skan ret->const_p = false; 7236169699Skan ret->volatile_p = false; 7237169699Skan ret->restrict_p = false; 7238169699Skan return ret; 7239169699Skan} 7240132730Skan 7241169699Skan/* Add the type qualifier QUAL to the declaration specifiers SPECS, 7242169699Skan returning SPECS. */ 7243132730Skan 7244169699Skanstruct c_declspecs * 7245169699Skandeclspecs_add_qual (struct c_declspecs *specs, tree qual) 7246169699Skan{ 7247169699Skan enum rid i; 7248169699Skan bool dupe = false; 7249169699Skan specs->non_sc_seen_p = true; 7250169699Skan specs->declspecs_seen_p = true; 7251169699Skan gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE 7252169699Skan && C_IS_RESERVED_WORD (qual)); 7253169699Skan i = C_RID_CODE (qual); 7254169699Skan switch (i) 7255169699Skan { 7256169699Skan case RID_CONST: 7257169699Skan dupe = specs->const_p; 7258169699Skan specs->const_p = true; 7259169699Skan break; 7260169699Skan case RID_VOLATILE: 7261169699Skan dupe = specs->volatile_p; 7262169699Skan specs->volatile_p = true; 7263169699Skan break; 7264169699Skan case RID_RESTRICT: 7265169699Skan dupe = specs->restrict_p; 7266169699Skan specs->restrict_p = true; 7267169699Skan break; 7268169699Skan default: 7269169699Skan gcc_unreachable (); 7270169699Skan } 7271169699Skan if (dupe && pedantic && !flag_isoc99) 7272169699Skan pedwarn ("duplicate %qE", qual); 7273169699Skan return specs; 7274169699Skan} 7275132730Skan 7276169699Skan/* Add the type specifier TYPE to the declaration specifiers SPECS, 7277169699Skan returning SPECS. */ 7278132730Skan 7279169699Skanstruct c_declspecs * 7280169699Skandeclspecs_add_type (struct c_declspecs *specs, struct c_typespec spec) 7281169699Skan{ 7282169699Skan tree type = spec.spec; 7283169699Skan specs->non_sc_seen_p = true; 7284169699Skan specs->declspecs_seen_p = true; 7285169699Skan specs->type_seen_p = true; 7286169699Skan if (TREE_DEPRECATED (type)) 7287169699Skan specs->deprecated_p = true; 7288169699Skan 7289169699Skan /* Handle type specifier keywords. */ 7290169699Skan if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type)) 7291169699Skan { 7292169699Skan enum rid i = C_RID_CODE (type); 7293169699Skan if (specs->type) 7294132730Skan { 7295169699Skan error ("two or more data types in declaration specifiers"); 7296169699Skan return specs; 7297169699Skan } 7298169699Skan if ((int) i <= (int) RID_LAST_MODIFIER) 7299169699Skan { 7300169699Skan /* "long", "short", "signed", "unsigned" or "_Complex". */ 7301169699Skan bool dupe = false; 7302169699Skan switch (i) 7303132730Skan { 7304169699Skan case RID_LONG: 7305169699Skan if (specs->long_long_p) 7306132730Skan { 7307169699Skan error ("%<long long long%> is too long for GCC"); 7308169699Skan break; 7309132730Skan } 7310169699Skan if (specs->long_p) 7311132730Skan { 7312169699Skan if (specs->typespec_word == cts_double) 7313169699Skan { 7314169699Skan error ("both %<long long%> and %<double%> in " 7315169699Skan "declaration specifiers"); 7316169699Skan break; 7317169699Skan } 7318169699Skan if (pedantic && !flag_isoc99 && !in_system_header 7319169699Skan && warn_long_long) 7320169699Skan pedwarn ("ISO C90 does not support %<long long%>"); 7321169699Skan specs->long_long_p = 1; 7322169699Skan break; 7323132730Skan } 7324169699Skan if (specs->short_p) 7325169699Skan error ("both %<long%> and %<short%> in " 7326169699Skan "declaration specifiers"); 7327169699Skan else if (specs->typespec_word == cts_void) 7328169699Skan error ("both %<long%> and %<void%> in " 7329169699Skan "declaration specifiers"); 7330169699Skan else if (specs->typespec_word == cts_bool) 7331169699Skan error ("both %<long%> and %<_Bool%> in " 7332169699Skan "declaration specifiers"); 7333169699Skan else if (specs->typespec_word == cts_char) 7334169699Skan error ("both %<long%> and %<char%> in " 7335169699Skan "declaration specifiers"); 7336169699Skan else if (specs->typespec_word == cts_float) 7337169699Skan error ("both %<long%> and %<float%> in " 7338169699Skan "declaration specifiers"); 7339169699Skan else if (specs->typespec_word == cts_dfloat32) 7340169699Skan error ("both %<long%> and %<_Decimal32%> in " 7341169699Skan "declaration specifiers"); 7342169699Skan else if (specs->typespec_word == cts_dfloat64) 7343169699Skan error ("both %<long%> and %<_Decimal64%> in " 7344169699Skan "declaration specifiers"); 7345169699Skan else if (specs->typespec_word == cts_dfloat128) 7346169699Skan error ("both %<long%> and %<_Decimal128%> in " 7347169699Skan "declaration specifiers"); 7348132730Skan else 7349169699Skan specs->long_p = true; 7350169699Skan break; 7351169699Skan case RID_SHORT: 7352169699Skan dupe = specs->short_p; 7353169699Skan if (specs->long_p) 7354169699Skan error ("both %<long%> and %<short%> in " 7355169699Skan "declaration specifiers"); 7356169699Skan else if (specs->typespec_word == cts_void) 7357169699Skan error ("both %<short%> and %<void%> in " 7358169699Skan "declaration specifiers"); 7359169699Skan else if (specs->typespec_word == cts_bool) 7360169699Skan error ("both %<short%> and %<_Bool%> in " 7361169699Skan "declaration specifiers"); 7362169699Skan else if (specs->typespec_word == cts_char) 7363169699Skan error ("both %<short%> and %<char%> in " 7364169699Skan "declaration specifiers"); 7365169699Skan else if (specs->typespec_word == cts_float) 7366169699Skan error ("both %<short%> and %<float%> in " 7367169699Skan "declaration specifiers"); 7368169699Skan else if (specs->typespec_word == cts_double) 7369169699Skan error ("both %<short%> and %<double%> in " 7370169699Skan "declaration specifiers"); 7371169699Skan else if (specs->typespec_word == cts_dfloat32) 7372169699Skan error ("both %<short%> and %<_Decimal32%> in " 7373169699Skan "declaration specifiers"); 7374169699Skan else if (specs->typespec_word == cts_dfloat64) 7375169699Skan error ("both %<short%> and %<_Decimal64%> in " 7376169699Skan "declaration specifiers"); 7377169699Skan else if (specs->typespec_word == cts_dfloat128) 7378169699Skan error ("both %<short%> and %<_Decimal128%> in " 7379169699Skan "declaration specifiers"); 7380169699Skan else 7381169699Skan specs->short_p = true; 7382169699Skan break; 7383169699Skan case RID_SIGNED: 7384169699Skan dupe = specs->signed_p; 7385169699Skan if (specs->unsigned_p) 7386169699Skan error ("both %<signed%> and %<unsigned%> in " 7387169699Skan "declaration specifiers"); 7388169699Skan else if (specs->typespec_word == cts_void) 7389169699Skan error ("both %<signed%> and %<void%> in " 7390169699Skan "declaration specifiers"); 7391169699Skan else if (specs->typespec_word == cts_bool) 7392169699Skan error ("both %<signed%> and %<_Bool%> in " 7393169699Skan "declaration specifiers"); 7394169699Skan else if (specs->typespec_word == cts_float) 7395169699Skan error ("both %<signed%> and %<float%> in " 7396169699Skan "declaration specifiers"); 7397169699Skan else if (specs->typespec_word == cts_double) 7398169699Skan error ("both %<signed%> and %<double%> in " 7399169699Skan "declaration specifiers"); 7400169699Skan else if (specs->typespec_word == cts_dfloat32) 7401169699Skan error ("both %<signed%> and %<_Decimal32%> in " 7402169699Skan "declaration specifiers"); 7403169699Skan else if (specs->typespec_word == cts_dfloat64) 7404169699Skan error ("both %<signed%> and %<_Decimal64%> in " 7405169699Skan "declaration specifiers"); 7406169699Skan else if (specs->typespec_word == cts_dfloat128) 7407169699Skan error ("both %<signed%> and %<_Decimal128%> in " 7408169699Skan "declaration specifiers"); 7409169699Skan else 7410169699Skan specs->signed_p = true; 7411169699Skan break; 7412169699Skan case RID_UNSIGNED: 7413169699Skan dupe = specs->unsigned_p; 7414169699Skan if (specs->signed_p) 7415169699Skan error ("both %<signed%> and %<unsigned%> in " 7416169699Skan "declaration specifiers"); 7417169699Skan else if (specs->typespec_word == cts_void) 7418169699Skan error ("both %<unsigned%> and %<void%> in " 7419169699Skan "declaration specifiers"); 7420169699Skan else if (specs->typespec_word == cts_bool) 7421169699Skan error ("both %<unsigned%> and %<_Bool%> in " 7422169699Skan "declaration specifiers"); 7423169699Skan else if (specs->typespec_word == cts_float) 7424169699Skan error ("both %<unsigned%> and %<float%> in " 7425169699Skan "declaration specifiers"); 7426169699Skan else if (specs->typespec_word == cts_double) 7427169699Skan error ("both %<unsigned%> and %<double%> in " 7428169699Skan "declaration specifiers"); 7429169699Skan else if (specs->typespec_word == cts_dfloat32) 7430169699Skan error ("both %<unsigned%> and %<_Decimal32%> in " 7431169699Skan "declaration specifiers"); 7432169699Skan else if (specs->typespec_word == cts_dfloat64) 7433169699Skan error ("both %<unsigned%> and %<_Decimal64%> in " 7434169699Skan "declaration specifiers"); 7435169699Skan else if (specs->typespec_word == cts_dfloat128) 7436169699Skan error ("both %<unsigned%> and %<_Decimal128%> in " 7437169699Skan "declaration specifiers"); 7438169699Skan else 7439169699Skan specs->unsigned_p = true; 7440169699Skan break; 7441169699Skan case RID_COMPLEX: 7442169699Skan dupe = specs->complex_p; 7443169699Skan if (pedantic && !flag_isoc99 && !in_system_header) 7444169699Skan pedwarn ("ISO C90 does not support complex types"); 7445169699Skan if (specs->typespec_word == cts_void) 7446169699Skan error ("both %<complex%> and %<void%> in " 7447169699Skan "declaration specifiers"); 7448169699Skan else if (specs->typespec_word == cts_bool) 7449169699Skan error ("both %<complex%> and %<_Bool%> in " 7450169699Skan "declaration specifiers"); 7451169699Skan else if (specs->typespec_word == cts_dfloat32) 7452169699Skan error ("both %<complex%> and %<_Decimal32%> in " 7453169699Skan "declaration specifiers"); 7454169699Skan else if (specs->typespec_word == cts_dfloat64) 7455169699Skan error ("both %<complex%> and %<_Decimal64%> in " 7456169699Skan "declaration specifiers"); 7457169699Skan else if (specs->typespec_word == cts_dfloat128) 7458169699Skan error ("both %<complex%> and %<_Decimal128%> in " 7459169699Skan "declaration specifiers"); 7460169699Skan else 7461169699Skan specs->complex_p = true; 7462169699Skan break; 7463169699Skan default: 7464169699Skan gcc_unreachable (); 7465132730Skan } 7466169699Skan 7467169699Skan if (dupe) 7468169699Skan error ("duplicate %qE", type); 7469169699Skan 7470169699Skan return specs; 7471132730Skan } 7472169699Skan else 7473169699Skan { 7474169699Skan /* "void", "_Bool", "char", "int", "float" or "double". */ 7475169699Skan if (specs->typespec_word != cts_none) 7476169699Skan { 7477169699Skan error ("two or more data types in declaration specifiers"); 7478169699Skan return specs; 7479169699Skan } 7480169699Skan switch (i) 7481169699Skan { 7482169699Skan case RID_VOID: 7483169699Skan if (specs->long_p) 7484169699Skan error ("both %<long%> and %<void%> in " 7485169699Skan "declaration specifiers"); 7486169699Skan else if (specs->short_p) 7487169699Skan error ("both %<short%> and %<void%> in " 7488169699Skan "declaration specifiers"); 7489169699Skan else if (specs->signed_p) 7490169699Skan error ("both %<signed%> and %<void%> in " 7491169699Skan "declaration specifiers"); 7492169699Skan else if (specs->unsigned_p) 7493169699Skan error ("both %<unsigned%> and %<void%> in " 7494169699Skan "declaration specifiers"); 7495169699Skan else if (specs->complex_p) 7496169699Skan error ("both %<complex%> and %<void%> in " 7497169699Skan "declaration specifiers"); 7498169699Skan else 7499169699Skan specs->typespec_word = cts_void; 7500169699Skan return specs; 7501169699Skan case RID_BOOL: 7502169699Skan if (specs->long_p) 7503169699Skan error ("both %<long%> and %<_Bool%> in " 7504169699Skan "declaration specifiers"); 7505169699Skan else if (specs->short_p) 7506169699Skan error ("both %<short%> and %<_Bool%> in " 7507169699Skan "declaration specifiers"); 7508169699Skan else if (specs->signed_p) 7509169699Skan error ("both %<signed%> and %<_Bool%> in " 7510169699Skan "declaration specifiers"); 7511169699Skan else if (specs->unsigned_p) 7512169699Skan error ("both %<unsigned%> and %<_Bool%> in " 7513169699Skan "declaration specifiers"); 7514169699Skan else if (specs->complex_p) 7515169699Skan error ("both %<complex%> and %<_Bool%> in " 7516169699Skan "declaration specifiers"); 7517169699Skan else 7518169699Skan specs->typespec_word = cts_bool; 7519169699Skan return specs; 7520169699Skan case RID_CHAR: 7521169699Skan if (specs->long_p) 7522169699Skan error ("both %<long%> and %<char%> in " 7523169699Skan "declaration specifiers"); 7524169699Skan else if (specs->short_p) 7525169699Skan error ("both %<short%> and %<char%> in " 7526169699Skan "declaration specifiers"); 7527169699Skan else 7528169699Skan specs->typespec_word = cts_char; 7529169699Skan return specs; 7530169699Skan case RID_INT: 7531169699Skan specs->typespec_word = cts_int; 7532169699Skan return specs; 7533169699Skan case RID_FLOAT: 7534169699Skan if (specs->long_p) 7535169699Skan error ("both %<long%> and %<float%> in " 7536169699Skan "declaration specifiers"); 7537169699Skan else if (specs->short_p) 7538169699Skan error ("both %<short%> and %<float%> in " 7539169699Skan "declaration specifiers"); 7540169699Skan else if (specs->signed_p) 7541169699Skan error ("both %<signed%> and %<float%> in " 7542169699Skan "declaration specifiers"); 7543169699Skan else if (specs->unsigned_p) 7544169699Skan error ("both %<unsigned%> and %<float%> in " 7545169699Skan "declaration specifiers"); 7546169699Skan else 7547169699Skan specs->typespec_word = cts_float; 7548169699Skan return specs; 7549169699Skan case RID_DOUBLE: 7550169699Skan if (specs->long_long_p) 7551169699Skan error ("both %<long long%> and %<double%> in " 7552169699Skan "declaration specifiers"); 7553169699Skan else if (specs->short_p) 7554169699Skan error ("both %<short%> and %<double%> in " 7555169699Skan "declaration specifiers"); 7556169699Skan else if (specs->signed_p) 7557169699Skan error ("both %<signed%> and %<double%> in " 7558169699Skan "declaration specifiers"); 7559169699Skan else if (specs->unsigned_p) 7560169699Skan error ("both %<unsigned%> and %<double%> in " 7561169699Skan "declaration specifiers"); 7562169699Skan else 7563169699Skan specs->typespec_word = cts_double; 7564169699Skan return specs; 7565169699Skan case RID_DFLOAT32: 7566169699Skan case RID_DFLOAT64: 7567169699Skan case RID_DFLOAT128: 7568169699Skan { 7569169699Skan const char *str; 7570169699Skan if (i == RID_DFLOAT32) 7571169699Skan str = "_Decimal32"; 7572169699Skan else if (i == RID_DFLOAT64) 7573169699Skan str = "_Decimal64"; 7574169699Skan else 7575169699Skan str = "_Decimal128"; 7576169699Skan if (specs->long_long_p) 7577169699Skan error ("both %<long long%> and %<%s%> in " 7578169699Skan "declaration specifiers", str); 7579169699Skan if (specs->long_p) 7580169699Skan error ("both %<long%> and %<%s%> in " 7581169699Skan "declaration specifiers", str); 7582169699Skan else if (specs->short_p) 7583169699Skan error ("both %<short%> and %<%s%> in " 7584169699Skan "declaration specifiers", str); 7585169699Skan else if (specs->signed_p) 7586169699Skan error ("both %<signed%> and %<%s%> in " 7587169699Skan "declaration specifiers", str); 7588169699Skan else if (specs->unsigned_p) 7589169699Skan error ("both %<unsigned%> and %<%s%> in " 7590169699Skan "declaration specifiers", str); 7591169699Skan else if (specs->complex_p) 7592169699Skan error ("both %<complex%> and %<%s%> in " 7593169699Skan "declaration specifiers", str); 7594169699Skan else if (i == RID_DFLOAT32) 7595169699Skan specs->typespec_word = cts_dfloat32; 7596169699Skan else if (i == RID_DFLOAT64) 7597169699Skan specs->typespec_word = cts_dfloat64; 7598169699Skan else 7599169699Skan specs->typespec_word = cts_dfloat128; 7600169699Skan } 7601169699Skan if (!targetm.decimal_float_supported_p ()) 7602169699Skan error ("decimal floating point not supported for this target"); 7603169699Skan if (pedantic) 7604169699Skan pedwarn ("ISO C does not support decimal floating point"); 7605169699Skan return specs; 7606169699Skan default: 7607169699Skan /* ObjC reserved word "id", handled below. */ 7608169699Skan break; 7609169699Skan } 7610169699Skan } 7611169699Skan } 7612132730Skan 7613169699Skan /* Now we have a typedef (a TYPE_DECL node), an identifier (some 7614169699Skan form of ObjC type, cases such as "int" and "long" being handled 7615169699Skan above), a TYPE (struct, union, enum and typeof specifiers) or an 7616169699Skan ERROR_MARK. In none of these cases may there have previously 7617169699Skan been any type specifiers. */ 7618169699Skan if (specs->type || specs->typespec_word != cts_none 7619169699Skan || specs->long_p || specs->short_p || specs->signed_p 7620169699Skan || specs->unsigned_p || specs->complex_p) 7621169699Skan error ("two or more data types in declaration specifiers"); 7622169699Skan else if (TREE_CODE (type) == TYPE_DECL) 7623169699Skan { 7624169699Skan if (TREE_TYPE (type) == error_mark_node) 7625169699Skan ; /* Allow the type to default to int to avoid cascading errors. */ 7626169699Skan else 7627132730Skan { 7628169699Skan specs->type = TREE_TYPE (type); 7629169699Skan specs->decl_attr = DECL_ATTRIBUTES (type); 7630169699Skan specs->typedef_p = true; 7631169699Skan specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type); 7632169699Skan } 7633169699Skan } 7634169699Skan else if (TREE_CODE (type) == IDENTIFIER_NODE) 7635169699Skan { 7636169699Skan tree t = lookup_name (type); 7637169699Skan if (!t || TREE_CODE (t) != TYPE_DECL) 7638169699Skan error ("%qE fails to be a typedef or built in type", type); 7639169699Skan else if (TREE_TYPE (t) == error_mark_node) 7640169699Skan ; 7641169699Skan else 7642169699Skan specs->type = TREE_TYPE (t); 7643169699Skan } 7644169699Skan else if (TREE_CODE (type) != ERROR_MARK) 7645169699Skan { 7646169699Skan if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref) 7647169699Skan specs->tag_defined_p = true; 7648169699Skan if (spec.kind == ctsk_typeof) 7649169699Skan specs->typedef_p = true; 7650169699Skan specs->type = type; 7651169699Skan } 7652132730Skan 7653169699Skan return specs; 7654169699Skan} 7655132730Skan 7656169699Skan/* Add the storage class specifier or function specifier SCSPEC to the 7657169699Skan declaration specifiers SPECS, returning SPECS. */ 7658169699Skan 7659169699Skanstruct c_declspecs * 7660169699Skandeclspecs_add_scspec (struct c_declspecs *specs, tree scspec) 7661169699Skan{ 7662169699Skan enum rid i; 7663169699Skan enum c_storage_class n = csc_none; 7664169699Skan bool dupe = false; 7665169699Skan specs->declspecs_seen_p = true; 7666169699Skan gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE 7667169699Skan && C_IS_RESERVED_WORD (scspec)); 7668169699Skan i = C_RID_CODE (scspec); 7669169699Skan if (extra_warnings && specs->non_sc_seen_p) 7670169699Skan warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec); 7671169699Skan switch (i) 7672169699Skan { 7673169699Skan case RID_INLINE: 7674169699Skan /* C99 permits duplicate inline. Although of doubtful utility, 7675169699Skan it seems simplest to permit it in gnu89 mode as well, as 7676169699Skan there is also little utility in maintaining this as a 7677169699Skan difference between gnu89 and C99 inline. */ 7678169699Skan dupe = false; 7679169699Skan specs->inline_p = true; 7680169699Skan break; 7681169699Skan case RID_THREAD: 7682169699Skan dupe = specs->thread_p; 7683169699Skan if (specs->storage_class == csc_auto) 7684169699Skan error ("%<__thread%> used with %<auto%>"); 7685169699Skan else if (specs->storage_class == csc_register) 7686169699Skan error ("%<__thread%> used with %<register%>"); 7687169699Skan else if (specs->storage_class == csc_typedef) 7688169699Skan error ("%<__thread%> used with %<typedef%>"); 7689169699Skan else 7690169699Skan specs->thread_p = true; 7691169699Skan break; 7692169699Skan case RID_AUTO: 7693169699Skan n = csc_auto; 7694169699Skan break; 7695169699Skan case RID_EXTERN: 7696169699Skan n = csc_extern; 7697169699Skan /* Diagnose "__thread extern". */ 7698169699Skan if (specs->thread_p) 7699169699Skan error ("%<__thread%> before %<extern%>"); 7700169699Skan break; 7701169699Skan case RID_REGISTER: 7702169699Skan n = csc_register; 7703169699Skan break; 7704169699Skan case RID_STATIC: 7705169699Skan n = csc_static; 7706169699Skan /* Diagnose "__thread static". */ 7707169699Skan if (specs->thread_p) 7708169699Skan error ("%<__thread%> before %<static%>"); 7709169699Skan break; 7710169699Skan case RID_TYPEDEF: 7711169699Skan n = csc_typedef; 7712169699Skan break; 7713169699Skan default: 7714169699Skan gcc_unreachable (); 7715169699Skan } 7716169699Skan if (n != csc_none && n == specs->storage_class) 7717169699Skan dupe = true; 7718169699Skan if (dupe) 7719169699Skan error ("duplicate %qE", scspec); 7720169699Skan if (n != csc_none) 7721169699Skan { 7722169699Skan if (specs->storage_class != csc_none && n != specs->storage_class) 7723169699Skan { 7724169699Skan error ("multiple storage classes in declaration specifiers"); 7725132730Skan } 7726169699Skan else 7727169699Skan { 7728169699Skan specs->storage_class = n; 7729169699Skan if (n != csc_extern && n != csc_static && specs->thread_p) 7730169699Skan { 7731169699Skan error ("%<__thread%> used with %qE", scspec); 7732169699Skan specs->thread_p = false; 7733169699Skan } 7734169699Skan } 7735169699Skan } 7736169699Skan return specs; 7737169699Skan} 7738132730Skan 7739169699Skan/* Add the attributes ATTRS to the declaration specifiers SPECS, 7740169699Skan returning SPECS. */ 7741169699Skan 7742169699Skanstruct c_declspecs * 7743169699Skandeclspecs_add_attrs (struct c_declspecs *specs, tree attrs) 7744169699Skan{ 7745169699Skan specs->attrs = chainon (attrs, specs->attrs); 7746169699Skan specs->declspecs_seen_p = true; 7747169699Skan return specs; 7748132730Skan} 7749132730Skan 7750169699Skan/* Combine "long", "short", "signed", "unsigned" and "_Complex" type 7751169699Skan specifiers with any other type specifier to determine the resulting 7752169699Skan type. This is where ISO C checks on complex types are made, since 7753169699Skan "_Complex long" is a prefix of the valid ISO C type "_Complex long 7754169699Skan double". */ 7755132730Skan 7756169699Skanstruct c_declspecs * 7757169699Skanfinish_declspecs (struct c_declspecs *specs) 7758132730Skan{ 7759169699Skan /* If a type was specified as a whole, we have no modifiers and are 7760169699Skan done. */ 7761169699Skan if (specs->type != NULL_TREE) 7762169699Skan { 7763169699Skan gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p 7764169699Skan && !specs->signed_p && !specs->unsigned_p 7765169699Skan && !specs->complex_p); 7766169699Skan return specs; 7767169699Skan } 7768132730Skan 7769169699Skan /* If none of "void", "_Bool", "char", "int", "float" or "double" 7770169699Skan has been specified, treat it as "int" unless "_Complex" is 7771169699Skan present and there are no other specifiers. If we just have 7772169699Skan "_Complex", it is equivalent to "_Complex double", but e.g. 7773169699Skan "_Complex short" is equivalent to "_Complex short int". */ 7774169699Skan if (specs->typespec_word == cts_none) 7775132730Skan { 7776169699Skan if (specs->long_p || specs->short_p 7777169699Skan || specs->signed_p || specs->unsigned_p) 7778169699Skan { 7779169699Skan specs->typespec_word = cts_int; 7780169699Skan } 7781169699Skan else if (specs->complex_p) 7782169699Skan { 7783169699Skan specs->typespec_word = cts_double; 7784169699Skan if (pedantic) 7785169699Skan pedwarn ("ISO C does not support plain %<complex%> meaning " 7786169699Skan "%<double complex%>"); 7787169699Skan } 7788169699Skan else 7789169699Skan { 7790169699Skan specs->typespec_word = cts_int; 7791169699Skan specs->default_int_p = true; 7792169699Skan /* We don't diagnose this here because grokdeclarator will 7793169699Skan give more specific diagnostics according to whether it is 7794169699Skan a function definition. */ 7795169699Skan } 7796169699Skan } 7797132730Skan 7798169699Skan /* If "signed" was specified, record this to distinguish "int" and 7799169699Skan "signed int" in the case of a bit-field with 7800169699Skan -funsigned-bitfields. */ 7801169699Skan specs->explicit_signed_p = specs->signed_p; 7802132730Skan 7803169699Skan /* Now compute the actual type. */ 7804169699Skan switch (specs->typespec_word) 7805169699Skan { 7806169699Skan case cts_void: 7807169699Skan gcc_assert (!specs->long_p && !specs->short_p 7808169699Skan && !specs->signed_p && !specs->unsigned_p 7809169699Skan && !specs->complex_p); 7810169699Skan specs->type = void_type_node; 7811169699Skan break; 7812169699Skan case cts_bool: 7813169699Skan gcc_assert (!specs->long_p && !specs->short_p 7814169699Skan && !specs->signed_p && !specs->unsigned_p 7815169699Skan && !specs->complex_p); 7816169699Skan specs->type = boolean_type_node; 7817169699Skan break; 7818169699Skan case cts_char: 7819169699Skan gcc_assert (!specs->long_p && !specs->short_p); 7820169699Skan gcc_assert (!(specs->signed_p && specs->unsigned_p)); 7821169699Skan if (specs->signed_p) 7822169699Skan specs->type = signed_char_type_node; 7823169699Skan else if (specs->unsigned_p) 7824169699Skan specs->type = unsigned_char_type_node; 7825169699Skan else 7826169699Skan specs->type = char_type_node; 7827169699Skan if (specs->complex_p) 7828169699Skan { 7829169699Skan if (pedantic) 7830169699Skan pedwarn ("ISO C does not support complex integer types"); 7831169699Skan specs->type = build_complex_type (specs->type); 7832169699Skan } 7833169699Skan break; 7834169699Skan case cts_int: 7835169699Skan gcc_assert (!(specs->long_p && specs->short_p)); 7836169699Skan gcc_assert (!(specs->signed_p && specs->unsigned_p)); 7837169699Skan if (specs->long_long_p) 7838169699Skan specs->type = (specs->unsigned_p 7839169699Skan ? long_long_unsigned_type_node 7840169699Skan : long_long_integer_type_node); 7841169699Skan else if (specs->long_p) 7842169699Skan specs->type = (specs->unsigned_p 7843169699Skan ? long_unsigned_type_node 7844169699Skan : long_integer_type_node); 7845169699Skan else if (specs->short_p) 7846169699Skan specs->type = (specs->unsigned_p 7847169699Skan ? short_unsigned_type_node 7848169699Skan : short_integer_type_node); 7849169699Skan else 7850169699Skan specs->type = (specs->unsigned_p 7851169699Skan ? unsigned_type_node 7852169699Skan : integer_type_node); 7853169699Skan if (specs->complex_p) 7854169699Skan { 7855169699Skan if (pedantic) 7856169699Skan pedwarn ("ISO C does not support complex integer types"); 7857169699Skan specs->type = build_complex_type (specs->type); 7858169699Skan } 7859169699Skan break; 7860169699Skan case cts_float: 7861169699Skan gcc_assert (!specs->long_p && !specs->short_p 7862169699Skan && !specs->signed_p && !specs->unsigned_p); 7863169699Skan specs->type = (specs->complex_p 7864169699Skan ? complex_float_type_node 7865169699Skan : float_type_node); 7866169699Skan break; 7867169699Skan case cts_double: 7868169699Skan gcc_assert (!specs->long_long_p && !specs->short_p 7869169699Skan && !specs->signed_p && !specs->unsigned_p); 7870169699Skan if (specs->long_p) 7871169699Skan { 7872169699Skan specs->type = (specs->complex_p 7873169699Skan ? complex_long_double_type_node 7874169699Skan : long_double_type_node); 7875169699Skan } 7876169699Skan else 7877169699Skan { 7878169699Skan specs->type = (specs->complex_p 7879169699Skan ? complex_double_type_node 7880169699Skan : double_type_node); 7881169699Skan } 7882169699Skan break; 7883169699Skan case cts_dfloat32: 7884169699Skan case cts_dfloat64: 7885169699Skan case cts_dfloat128: 7886169699Skan gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p 7887169699Skan && !specs->signed_p && !specs->unsigned_p && !specs->complex_p); 7888169699Skan if (specs->typespec_word == cts_dfloat32) 7889169699Skan specs->type = dfloat32_type_node; 7890169699Skan else if (specs->typespec_word == cts_dfloat64) 7891169699Skan specs->type = dfloat64_type_node; 7892169699Skan else 7893169699Skan specs->type = dfloat128_type_node; 7894169699Skan break; 7895169699Skan default: 7896169699Skan gcc_unreachable (); 7897169699Skan } 7898132730Skan 7899169699Skan return specs; 7900169699Skan} 7901132730Skan 7902169699Skan/* Synthesize a function which calls all the global ctors or global 7903169699Skan dtors in this file. This is only used for targets which do not 7904169699Skan support .ctors/.dtors sections. FIXME: Migrate into cgraph. */ 7905169699Skanstatic void 7906169699Skanbuild_cdtor (int method_type, tree cdtors) 7907169699Skan{ 7908169699Skan tree body = 0; 7909132730Skan 7910169699Skan if (!cdtors) 7911169699Skan return; 7912169699Skan 7913169699Skan for (; cdtors; cdtors = TREE_CHAIN (cdtors)) 7914169699Skan append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0), 7915169699Skan &body); 7916169699Skan 7917169699Skan cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY); 7918169699Skan} 7919169699Skan 7920169699Skan/* A subroutine of c_write_global_declarations. Perform final processing 7921169699Skan on one file scope's declarations (or the external scope's declarations), 7922169699Skan GLOBALS. */ 7923169699Skan 7924169699Skanstatic void 7925169699Skanc_write_global_declarations_1 (tree globals) 7926169699Skan{ 7927169699Skan tree decl; 7928169699Skan bool reconsider; 7929169699Skan 7930169699Skan /* Process the decls in the order they were written. */ 7931169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 7932169699Skan { 7933169699Skan /* Check for used but undefined static functions using the C 7934169699Skan standard's definition of "used", and set TREE_NO_WARNING so 7935169699Skan that check_global_declarations doesn't repeat the check. */ 7936169699Skan if (TREE_CODE (decl) == FUNCTION_DECL 7937169699Skan && DECL_INITIAL (decl) == 0 7938169699Skan && DECL_EXTERNAL (decl) 7939169699Skan && !TREE_PUBLIC (decl) 7940169699Skan && C_DECL_USED (decl)) 7941169699Skan { 7942169699Skan pedwarn ("%q+F used but never defined", decl); 7943169699Skan TREE_NO_WARNING (decl) = 1; 7944169699Skan } 7945169699Skan 7946169699Skan wrapup_global_declaration_1 (decl); 7947132730Skan } 7948169699Skan 7949169699Skan do 7950169699Skan { 7951169699Skan reconsider = false; 7952169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 7953169699Skan reconsider |= wrapup_global_declaration_2 (decl); 7954169699Skan } 7955169699Skan while (reconsider); 7956169699Skan 7957169699Skan for (decl = globals; decl; decl = TREE_CHAIN (decl)) 7958169699Skan check_global_declaration_1 (decl); 7959132730Skan} 7960132730Skan 7961169699Skan/* A subroutine of c_write_global_declarations Emit debug information for each 7962169699Skan of the declarations in GLOBALS. */ 7963132730Skan 7964169699Skanstatic void 7965169699Skanc_write_global_declarations_2 (tree globals) 7966169699Skan{ 7967169699Skan tree decl; 7968169699Skan 7969169699Skan for (decl = globals; decl ; decl = TREE_CHAIN (decl)) 7970169699Skan debug_hooks->global_decl (decl); 7971169699Skan} 7972169699Skan 7973169699Skan/* Preserve the external declarations scope across a garbage collect. */ 7974169699Skanstatic GTY(()) tree ext_block; 7975169699Skan 7976132730Skanvoid 7977169699Skanc_write_global_declarations (void) 7978132730Skan{ 7979169699Skan tree t; 7980132730Skan 7981169699Skan /* We don't want to do this if generating a PCH. */ 7982169699Skan if (pch_file) 7983169699Skan return; 7984132730Skan 7985169699Skan /* Don't waste time on further processing if -fsyntax-only or we've 7986169699Skan encountered errors. */ 7987169699Skan if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in)) 7988169699Skan return; 7989132730Skan 7990169699Skan /* Close the external scope. */ 7991169699Skan ext_block = pop_scope (); 7992169699Skan external_scope = 0; 7993169699Skan gcc_assert (!current_scope); 7994169699Skan 7995169699Skan if (ext_block) 7996169699Skan { 7997169699Skan tree tmp = BLOCK_VARS (ext_block); 7998169699Skan int flags; 7999169699Skan FILE * stream = dump_begin (TDI_tu, &flags); 8000169699Skan if (stream && tmp) 8001169699Skan { 8002169699Skan dump_node (tmp, flags & ~TDF_SLIM, stream); 8003169699Skan dump_end (TDI_tu, stream); 8004169699Skan } 8005169699Skan } 8006169699Skan 8007169699Skan /* Process all file scopes in this compilation, and the external_scope, 8008169699Skan through wrapup_global_declarations and check_global_declarations. */ 8009169699Skan for (t = all_translation_units; t; t = TREE_CHAIN (t)) 8010169699Skan c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t))); 8011169699Skan c_write_global_declarations_1 (BLOCK_VARS (ext_block)); 8012169699Skan 8013169699Skan /* Generate functions to call static constructors and destructors 8014169699Skan for targets that do not support .ctors/.dtors sections. These 8015169699Skan functions have magic names which are detected by collect2. */ 8016169699Skan build_cdtor ('I', static_ctors); static_ctors = 0; 8017169699Skan build_cdtor ('D', static_dtors); static_dtors = 0; 8018169699Skan 8019169699Skan /* We're done parsing; proceed to optimize and emit assembly. 8020169699Skan FIXME: shouldn't be the front end's responsibility to call this. */ 8021169699Skan cgraph_optimize (); 8022169699Skan 8023169699Skan /* After cgraph has had a chance to emit everything that's going to 8024169699Skan be emitted, output debug information for globals. */ 8025169699Skan if (errorcount == 0 && sorrycount == 0) 8026169699Skan { 8027169699Skan timevar_push (TV_SYMOUT); 8028169699Skan for (t = all_translation_units; t; t = TREE_CHAIN (t)) 8029169699Skan c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t))); 8030169699Skan c_write_global_declarations_2 (BLOCK_VARS (ext_block)); 8031169699Skan timevar_pop (TV_SYMOUT); 8032169699Skan } 8033169699Skan 8034169699Skan ext_block = NULL; 8035132730Skan} 8036132730Skan 8037117421Skan#include "gt-c-decl.h" 8038