c-decl.c revision 146906
118334Speter/* Process declarations and variables for C compiler. 290075Sobrien Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 3132730Skan 2001, 2002, 2003, 2004 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 1990075SobrienSoftware Foundation, 59 Temple Place - Suite 330, Boston, MA 2090075Sobrien02111-1307, USA. */ 2118334Speter 2296549Sobrien/* $FreeBSD: head/contrib/gcc/c-decl.c 146906 2005-06-03 03:50:42Z kan $ */ 2396549Sobrien 2418334Speter/* Process declarations and symbol lookup for C front end. 2518334Speter Also constructs types; the standard scalar types at initialization, 2618334Speter and structure, union, array and enum types when they are declared. */ 2718334Speter 2818334Speter/* ??? not all decl nodes are given the most useful possible 2918334Speter line numbers. For example, the CONST_DECLs for enum values. */ 3018334Speter 3118334Speter#include "config.h" 3250397Sobrien#include "system.h" 33132730Skan#include "coretypes.h" 34132730Skan#include "tm.h" 3590075Sobrien#include "intl.h" 3618334Speter#include "tree.h" 3790075Sobrien#include "tree-inline.h" 3890075Sobrien#include "rtl.h" 3918334Speter#include "flags.h" 4090075Sobrien#include "function.h" 4118334Speter#include "output.h" 4290075Sobrien#include "expr.h" 4318334Speter#include "c-tree.h" 4450397Sobrien#include "toplev.h" 4590075Sobrien#include "ggc.h" 4690075Sobrien#include "tm_p.h" 4750397Sobrien#include "cpplib.h" 4890075Sobrien#include "target.h" 4990075Sobrien#include "debug.h" 50132730Skan#include "opts.h" 5190075Sobrien#include "timevar.h" 5290075Sobrien#include "c-common.h" 5396263Sobrien#include "c-pragma.h" 54132730Skan#include "cgraph.h" 55132730Skan#include "hashtab.h" 56117421Skan#include "libfuncs.h" 57117421Skan#include "except.h" 58132730Skan#include "langhooks-def.h" 5950397Sobrien 6018334Speter/* In grokdeclarator, distinguish syntactic contexts of declarators. */ 6118334Speterenum decl_context 6218334Speter{ NORMAL, /* Ordinary declaration */ 6318334Speter FUNCDEF, /* Function definition */ 6418334Speter PARM, /* Declaration of parm before function body */ 6518334Speter FIELD, /* Declaration inside struct or union */ 6618334Speter TYPENAME}; /* Typename (inside cast or sizeof) */ 6718334Speter 6818334Speter 6918334Speter/* Nonzero if we have seen an invalid cross reference 7018334Speter to a struct, union, or enum, but not yet printed the message. */ 7118334Speter 7218334Spetertree pending_invalid_xref; 7318334Speter/* File and line to appear in the eventual error message. */ 74132730Skanlocation_t pending_invalid_xref_location; 7518334Speter 7618334Speter/* While defining an enum type, this is 1 plus the last enumerator 7718334Speter constant value. Note that will do not have to save this or `enum_overflow' 7818334Speter around nested function definition since such a definition could only 7918334Speter occur in an enum value expression and we don't use these variables in 8018334Speter that case. */ 8118334Speter 8218334Speterstatic tree enum_next_value; 8318334Speter 8418334Speter/* Nonzero means that there was overflow computing enum_next_value. */ 8518334Speter 8618334Speterstatic int enum_overflow; 8718334Speter 8818334Speter/* Parsing a function declarator leaves a list of parameter names 89132730Skan or a chain of parameter decls here. */ 9018334Speter 9118334Speterstatic tree last_function_parms; 9218334Speter 93132730Skan/* ... and a chain of structure and enum types declared in the 94132730Skan parmlist here. */ 9518334Speter 9618334Speterstatic tree last_function_parm_tags; 9718334Speter 98132730Skan/* ... and a chain of all non-parameter declarations (such as 99132730Skan CONST_DECLs from enumerations) here. */ 100132730Skan 101132730Skanstatic tree last_function_parm_others; 102132730Skan 10318334Speter/* After parsing the declarator that starts a function definition, 104132730Skan `start_function' puts the list of parameter names or chain of decls here 105132730Skan for `store_parm_decls' to find. */ 10618334Speter 10718334Speterstatic tree current_function_parms; 10818334Speter 10918334Speter/* Similar, for last_function_parm_tags. */ 110132730Skan 11118334Speterstatic tree current_function_parm_tags; 11218334Speter 113132730Skan/* And for last_function_parm_others. */ 114132730Skan 115132730Skanstatic tree current_function_parm_others; 116132730Skan 11718334Speter/* Similar, for the file and line that the prototype came from if this is 11818334Speter an old-style definition. */ 11918334Speter 120132730Skanstatic location_t current_function_prototype_locus; 121132730Skan 12290075Sobrien/* The current statement tree. */ 12390075Sobrien 124117421Skanstatic GTY(()) struct stmt_tree_s c_stmt_tree; 12590075Sobrien 12690075Sobrien/* The current scope statement stack. */ 12790075Sobrien 128117421Skanstatic GTY(()) tree c_scope_stmt_stack; 12990075Sobrien 130132730Skan/* State saving variables. */ 131132730Skanint c_in_iteration_stmt; 132132730Skanint c_in_case_stmt; 13318334Speter 134132730Skan/* A list of external DECLs that appeared at block scope when there was 135132730Skan some other global meaning for that identifier. */ 136132730Skanstatic GTY(()) tree truly_local_externals; 13718334Speter 138132730Skan/* All the builtins; this is a subset of the entries of global_scope. */ 13918334Speter 140132730Skanstatic GTY(()) tree first_builtin_decl; 141132730Skanstatic GTY(()) tree last_builtin_decl; 14218334Speter 143132730Skan/* A DECL for the current file-scope context. */ 144132730Skan 145132730Skanstatic GTY(()) tree current_file_decl; 146132730Skan 14718334Speter/* Set to 0 at beginning of a function definition, set to 1 if 14818334Speter a return statement that specifies a return value is seen. */ 14918334Speter 15018334Speterint current_function_returns_value; 15118334Speter 15218334Speter/* Set to 0 at beginning of a function definition, set to 1 if 15318334Speter a return statement with no argument is seen. */ 15418334Speter 15518334Speterint current_function_returns_null; 15618334Speter 15796263Sobrien/* Set to 0 at beginning of a function definition, set to 1 if 15896263Sobrien a call to a noreturn function is seen. */ 15996263Sobrien 16096263Sobrienint current_function_returns_abnormally; 16196263Sobrien 16218334Speter/* Set to nonzero by `grokdeclarator' for a function 16318334Speter whose return type is defaulted, if warnings for this are desired. */ 16418334Speter 16518334Speterstatic int warn_about_return_type; 16618334Speter 16718334Speter/* Nonzero when starting a function declared `extern inline'. */ 16818334Speter 16918334Speterstatic int current_extern_inline; 17018334Speter 171132730Skan/* Each c_scope structure describes the complete contents of one scope. 172132730Skan Three scopes are distinguished specially: the innermost or current 173132730Skan scope, the innermost function scope, and the outermost or file scope. 17418334Speter 175132730Skan Most declarations are recorded in the current scope. 17618334Speter 177132730Skan All normal label declarations are recorded in the innermost 178132730Skan function scope, as are bindings of undeclared identifiers to 179132730Skan error_mark_node. (GCC permits nested functions as an extension, 180132730Skan hence the 'innermost' qualifier.) Explicitly declared labels 181132730Skan (using the __label__ extension) appear in the current scope. 18218334Speter 183132730Skan Being in the global scope (current_scope == global_scope) causes 184132730Skan special behavior in several places below. Also, under some 185132730Skan conditions the Objective-C front end records declarations in the 186132730Skan global scope even though that isn't the current scope. 18718334Speter 188132730Skan The order of the names, parms, and blocks lists matters, and they 189132730Skan are frequently appended to. To avoid having to walk all the way to 190132730Skan the end of the list on each insertion, or reverse the lists later, 191132730Skan we maintain a pointer to the last list entry for each of the lists. 19218334Speter 193132730Skan The order of the tags, shadowed, and shadowed_tags 194132730Skan lists does not matter, so we just prepend to these lists. */ 19518334Speter 196132730Skanstruct c_scope GTY(()) 197132730Skan{ 198132730Skan /* The scope containing this one. */ 199132730Skan struct c_scope *outer; 20018334Speter 201132730Skan /* The next outermost function scope. */ 202132730Skan struct c_scope *outer_function; 20318334Speter 204132730Skan /* All variables, constants, functions, labels, and typedef names. */ 205132730Skan tree names; 206132730Skan tree names_last; 20718334Speter 208132730Skan /* All parameter declarations. Used only in the outermost scope of 209132730Skan a function. */ 210132730Skan tree parms; 211132730Skan tree parms_last; 21218334Speter 213132730Skan /* All structure, union, and enum type tags. */ 214132730Skan tree tags; 21518334Speter 216132730Skan /* For each scope, a list of shadowed outer-scope definitions 217132730Skan to be restored when this scope is popped. 218132730Skan Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and 219132730Skan whose TREE_VALUE is its old definition (a kind of ..._DECL node). */ 220132730Skan tree shadowed; 22118334Speter 222132730Skan /* For each scope, a list of shadowed outer-scope tag definitions 223132730Skan to be restored when this scope is popped. 224132730Skan Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and 225132730Skan whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */ 226132730Skan tree shadowed_tags; 22718334Speter 228132730Skan /* For each scope (except the global one), a chain of BLOCK nodes 229132730Skan for all the scopes that were entered and exited one level down. */ 230132730Skan tree blocks; 231132730Skan tree blocks_last; 23218334Speter 233132730Skan /* True if we are currently filling this scope with parameter 234132730Skan declarations. */ 235132730Skan BOOL_BITFIELD parm_flag : 1; 23618334Speter 237132730Skan /* True if we already complained about forward parameter decls 238132730Skan in this scope. This prevents double warnings on 239132730Skan foo (int a; int b; ...) */ 240132730Skan BOOL_BITFIELD warned_forward_parm_decls : 1; 24190075Sobrien 242132730Skan /* True if this is the outermost block scope of a function body. 243132730Skan This scope contains the parameters, the local variables declared 244132730Skan in the outermost block, and all the labels (except those in 245132730Skan nested functions, or declared at block scope with __label__). */ 246132730Skan BOOL_BITFIELD function_body : 1; 24718334Speter 248132730Skan /* True means make a BLOCK for this scope no matter what. */ 249132730Skan BOOL_BITFIELD keep : 1; 250132730Skan}; 25118334Speter 252132730Skan/* The scope currently in effect. */ 25318334Speter 254132730Skanstatic GTY(()) struct c_scope *current_scope; 25518334Speter 256132730Skan/* A chain of c_scope structures awaiting reuse. */ 25718334Speter 258132730Skanstatic GTY((deletable (""))) struct c_scope *scope_freelist; 25918334Speter 260132730Skan/* The innermost function scope. Ordinary (not explicitly declared) 261132730Skan labels, bindings to error_mark_node, and the lazily-created 262132730Skan bindings of __func__ and its friends get this scope. */ 26318334Speter 264132730Skanstatic GTY(()) struct c_scope *current_function_scope; 26518334Speter 266132730Skan/* The outermost scope, corresponding to the C "file scope". This is 267132730Skan created when the compiler is started and exists through the entire run. */ 26818334Speter 269132730Skanstatic GTY(()) struct c_scope *global_scope; 27018334Speter 271132730Skan/* Append VAR to LIST in scope SCOPE. */ 272132730Skan#define SCOPE_LIST_APPEND(scope, list, decl) do { \ 273132730Skan struct c_scope *s_ = (scope); \ 274132730Skan tree d_ = (decl); \ 275132730Skan if (s_->list##_last) \ 276132730Skan TREE_CHAIN (s_->list##_last) = d_; \ 277132730Skan else \ 278132730Skan s_->list = d_; \ 279132730Skan s_->list##_last = d_; \ 280132730Skan} while (0) 28118334Speter 282132730Skan/* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */ 283132730Skan#define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \ 284132730Skan struct c_scope *t_ = (tscope); \ 285132730Skan struct c_scope *f_ = (fscope); \ 286132730Skan if (t_->to##_last) \ 287132730Skan TREE_CHAIN (t_->to##_last) = f_->from; \ 288132730Skan else \ 289132730Skan t_->to = f_->from; \ 290132730Skan t_->to##_last = f_->from##_last; \ 291132730Skan} while (0) 29290075Sobrien 293132730Skan/* True means unconditionally make a BLOCK for the next scope pushed. */ 29418334Speter 295132730Skanstatic bool keep_next_level_flag; 29618334Speter 297132730Skan/* True means the next call to pushlevel will be the outermost scope 298132730Skan of a function body, so do not push a new scope, merely cease 299132730Skan expecting parameter decls. */ 300132730Skan 301132730Skanstatic bool next_is_function_body; 302132730Skan 30318334Speter/* Functions called automatically at the beginning and end of execution. */ 30418334Speter 30518334Spetertree static_ctors, static_dtors; 30618334Speter 30718334Speter/* Forward declarations. */ 30818334Speter 309132730Skanstatic struct c_scope *make_scope (void); 310132730Skanstatic void pop_scope (void); 311132730Skanstatic tree make_label (tree, location_t); 312132730Skanstatic void bind_label (tree, tree, struct c_scope *); 313132730Skanstatic void implicit_decl_warning (tree); 314132730Skanstatic tree lookup_tag (enum tree_code, tree, int); 315132730Skanstatic tree lookup_name_current_level (tree); 316132730Skanstatic tree grokdeclarator (tree, tree, enum decl_context, int, tree *); 317132730Skanstatic tree grokparms (tree, int); 318132730Skanstatic void layout_array_type (tree); 319132730Skanstatic void store_parm_decls_newstyle (void); 320132730Skanstatic void store_parm_decls_oldstyle (void); 321132730Skanstatic tree c_make_fname_decl (tree, int); 322132730Skanstatic void c_expand_body_1 (tree, int); 323132730Skanstatic tree any_external_decl (tree); 324132730Skanstatic void record_external_decl (tree); 325132730Skanstatic void warn_if_shadowing (tree, tree); 326132730Skanstatic void check_bitfield_type_and_width (tree *, tree *, const char *); 327132730Skanstatic void clone_underlying_type (tree); 328132730Skanstatic bool flexible_array_type_p (tree); 329132730Skanstatic hashval_t link_hash_hash (const void *); 330132730Skanstatic int link_hash_eq (const void *, const void *); 33118334Speter 33290075Sobrien/* States indicating how grokdeclarator() should handle declspecs marked 33390075Sobrien with __attribute__((deprecated)). An object declared as 33490075Sobrien __attribute__((deprecated)) suppresses warnings of uses of other 33590075Sobrien deprecated items. */ 336132730Skan 33790075Sobrienenum deprecated_states { 33890075Sobrien DEPRECATED_NORMAL, 33990075Sobrien DEPRECATED_SUPPRESS 34090075Sobrien}; 34190075Sobrien 34290075Sobrienstatic enum deprecated_states deprecated_state = DEPRECATED_NORMAL; 34390075Sobrien 34418334Spetervoid 345132730Skanc_print_identifier (FILE *file, tree node, int indent) 34618334Speter{ 347132730Skan print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4); 348132730Skan print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4); 34918334Speter print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4); 35090075Sobrien if (C_IS_RESERVED_WORD (node)) 35190075Sobrien { 35290075Sobrien tree rid = ridpointers[C_RID_CODE (node)]; 35390075Sobrien indent_to (file, indent + 4); 354132730Skan fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"", 355132730Skan (void *) rid, IDENTIFIER_POINTER (rid)); 35690075Sobrien } 35718334Speter} 35818334Speter 35918334Speter/* Hook called at end of compilation to assume 1 elt 360132730Skan for a file-scope tentative array defn that wasn't complete before. */ 36190075Sobrien 36218334Spetervoid 363132730Skanc_finish_incomplete_decl (tree decl) 36418334Speter{ 36550397Sobrien if (TREE_CODE (decl) == VAR_DECL) 36618334Speter { 36718334Speter tree type = TREE_TYPE (decl); 36850397Sobrien if (type != error_mark_node 36950397Sobrien && TREE_CODE (type) == ARRAY_TYPE 37090075Sobrien && ! DECL_EXTERNAL (decl) 37150397Sobrien && TYPE_DOMAIN (type) == 0) 37218334Speter { 373132730Skan warning ("%Jarray '%D' assumed to have one element", decl, decl); 37450397Sobrien 37518334Speter complete_array_type (type, NULL_TREE, 1); 37618334Speter 37718334Speter layout_decl (decl, 0); 37818334Speter } 37918334Speter } 38018334Speter} 38118334Speter 382132730Skan/* Reuse or create a struct for this scope. */ 38318334Speter 384132730Skanstatic struct c_scope * 385132730Skanmake_scope (void) 38618334Speter{ 387132730Skan struct c_scope *result; 388132730Skan if (scope_freelist) 389117421Skan { 390132730Skan result = scope_freelist; 391132730Skan scope_freelist = result->outer; 392117421Skan } 393117421Skan else 394132730Skan result = ggc_alloc_cleared (sizeof (struct c_scope)); 395132730Skan 396132730Skan return result; 39718334Speter} 39818334Speter 399132730Skan/* Remove the topmost scope from the stack and add it to the 400132730Skan free list, updating current_function_scope if necessary. */ 401117421Skan 402117421Skanstatic void 403132730Skanpop_scope (void) 404117421Skan{ 405132730Skan struct c_scope *scope = current_scope; 406132730Skan 407132730Skan current_scope = scope->outer; 408132730Skan if (scope->function_body) 409132730Skan current_function_scope = scope->outer_function; 410132730Skan 411132730Skan memset (scope, 0, sizeof (struct c_scope)); 412132730Skan scope->outer = scope_freelist; 413132730Skan scope_freelist = scope; 414117421Skan} 415117421Skan 416132730Skan/* The Objective-C front-end often needs to determine the current scope. */ 41718334Speter 418132730Skanvoid * 419132730Skanget_current_scope (void) 42018334Speter{ 421132730Skan return current_scope; 42218334Speter} 42318334Speter 424132730Skan/* The following function is used only by Objective-C. It needs to live here 425132730Skan because it accesses the innards of c_scope. */ 426132730Skan 42718334Spetervoid 428132730Skanobjc_mark_locals_volatile (void *enclosing_blk) 42918334Speter{ 430132730Skan struct c_scope *scope; 431132730Skan 432132730Skan for (scope = current_scope; 433132730Skan scope && scope != enclosing_blk; 434132730Skan scope = scope->outer) 435132730Skan { 436132730Skan tree decl; 437132730Skan 438132730Skan for (decl = scope->names; decl; decl = TREE_CHAIN (decl)) 439132730Skan { 440132730Skan DECL_REGISTER (decl) = 0; 441132730Skan TREE_THIS_VOLATILE (decl) = 1; 442132730Skan } 443132730Skan /* Do not climb up past the current function. */ 444132730Skan if (scope->function_body) 445132730Skan break; 446132730Skan } 44718334Speter} 44818334Speter 449132730Skan/* Nonzero if we are currently in the global scope. */ 45018334Speter 45118334Speterint 452132730Skanglobal_bindings_p (void) 45318334Speter{ 454132730Skan return current_scope == global_scope; 45518334Speter} 45618334Speter 457132730Skanvoid 458132730Skankeep_next_level (void) 459132730Skan{ 460132730Skan keep_next_level_flag = true; 461132730Skan} 46218334Speter 463132730Skan/* Identify this scope as currently being filled with parameters. */ 464132730Skan 46518334Spetervoid 466132730Skandeclare_parm_level (void) 46718334Speter{ 468132730Skan current_scope->parm_flag = true; 46918334Speter} 47018334Speter 47118334Speter/* Nonzero if currently making parm declarations. */ 47218334Speter 47318334Speterint 474132730Skanin_parm_level_p (void) 47518334Speter{ 476132730Skan return current_scope->parm_flag; 47718334Speter} 47818334Speter 479132730Skan/* Enter a new scope. The dummy parameter is for signature 480132730Skan compatibility with lang_hooks.decls.pushlevel. */ 48118334Speter 48218334Spetervoid 483132730Skanpushlevel (int dummy ATTRIBUTE_UNUSED) 48418334Speter{ 485132730Skan if (next_is_function_body) 486132730Skan { 487132730Skan /* This is the transition from the parameters to the top level 488132730Skan of the function body. These are the same scope 489132730Skan (C99 6.2.1p4,6) so we do not push another scope structure. 490132730Skan next_is_function_body is set only by store_parm_decls, which 491132730Skan in turn is called when and only when we are about to 492132730Skan encounter the opening curly brace for the function body. 49318334Speter 494132730Skan The outermost block of a function always gets a BLOCK node, 495132730Skan because the debugging output routines expect that each 496132730Skan function has at least one BLOCK. */ 497132730Skan current_scope->parm_flag = false; 498132730Skan current_scope->function_body = true; 499132730Skan current_scope->keep = true; 500132730Skan current_scope->outer_function = current_function_scope; 501132730Skan current_function_scope = current_scope; 50218334Speter 503132730Skan keep_next_level_flag = false; 504132730Skan next_is_function_body = false; 505132730Skan } 506132730Skan else 50718334Speter { 508132730Skan struct c_scope *scope = make_scope (); 509132730Skan 510132730Skan scope->keep = keep_next_level_flag; 511132730Skan scope->outer = current_scope; 512132730Skan current_scope = scope; 513132730Skan keep_next_level_flag = false; 51418334Speter } 51518334Speter} 51618334Speter 517132730Skan/* Exit a scope. Restore the state of the identifier-decl mappings 518132730Skan that were in effect when this scope was entered. 51950397Sobrien 520132730Skan If KEEP is KEEP_YES (1), this scope had explicit declarations, so 521132730Skan create a BLOCK node to record its declarations and subblocks for 522132730Skan debugging output. If KEEP is KEEP_MAYBE, do so only if the names 523132730Skan or tags lists are nonempty. 52450397Sobrien 525132730Skan The second parameter is ignored; it is present only for 526132730Skan signature compatibility with lang_hooks.decls.poplevel. 52750397Sobrien 52818334Speter If FUNCTIONBODY is nonzero, this level is the body of a function, 529132730Skan even if current_scope->function_body is not set. This is used 530132730Skan by language-independent code that generates synthetic functions, 531132730Skan and cannot set current_scope->function_body. 53218334Speter 533132730Skan FIXME: Eliminate the need for all arguments. */ 53418334Speter 53518334Spetertree 536132730Skanpoplevel (int keep, int dummy ATTRIBUTE_UNUSED, int functionbody) 53718334Speter{ 538132730Skan struct c_scope *scope = current_scope; 539132730Skan tree block; 54018334Speter tree decl; 541132730Skan tree p; 54218334Speter 543132730Skan /* The following line does not use |= due to a bug in HP's C compiler. */ 544132730Skan scope->function_body = scope->function_body | functionbody; 54518334Speter 546132730Skan if (keep == KEEP_MAYBE) 547132730Skan keep = (scope->names || scope->tags); 54818334Speter 549132730Skan keep |= scope->keep; 550132730Skan keep |= scope->function_body; 55118334Speter 552132730Skan /* If appropriate, create a BLOCK to record the decls for the life 553132730Skan of this function. */ 55418334Speter block = 0; 555132730Skan if (keep) 55618334Speter { 557132730Skan block = make_node (BLOCK); 558132730Skan BLOCK_VARS (block) = scope->names; 559132730Skan BLOCK_SUBBLOCKS (block) = scope->blocks; 560132730Skan TREE_USED (block) = 1; 56118334Speter } 56218334Speter 56318334Speter /* In each subblock, record that this is its superior. */ 564132730Skan for (p = scope->blocks; p; p = TREE_CHAIN (p)) 565132730Skan BLOCK_SUPERCONTEXT (p) = block; 56618334Speter 567132730Skan /* Clear out the variable bindings in this scope. 56818334Speter 569132730Skan Propagate TREE_ADDRESSABLE from nested functions to their 570132730Skan containing functions. 57118334Speter 572132730Skan Issue warnings for unused variables and labels, and errors for 573132730Skan undefined labels, if there are any. */ 574132730Skan 575132730Skan for (p = scope->names; p; p = TREE_CHAIN (p)) 57618334Speter { 577132730Skan switch (TREE_CODE (p)) 57818334Speter { 579132730Skan case LABEL_DECL: 580132730Skan if (TREE_USED (p) && !DECL_INITIAL (p)) 58118334Speter { 582132730Skan error ("%Jlabel `%D' used but not defined", p, p); 583132730Skan DECL_INITIAL (p) = error_mark_node; 58418334Speter } 585132730Skan else if (!TREE_USED (p) && warn_unused_label) 586132730Skan { 587132730Skan if (DECL_INITIAL (p)) 588132730Skan warning ("%Jlabel `%D' defined but not used", p, p); 589132730Skan else 590132730Skan warning ("%Jlabel `%D' declared but not defined", p, p); 591132730Skan } 59218334Speter 593132730Skan IDENTIFIER_LABEL_VALUE (DECL_NAME (p)) = 0; 594132730Skan break; 59518334Speter 596132730Skan case FUNCTION_DECL: 597132730Skan if (! TREE_ASM_WRITTEN (p) 598132730Skan && DECL_INITIAL (p) != 0 599132730Skan && TREE_ADDRESSABLE (p) 600132730Skan && DECL_ABSTRACT_ORIGIN (p) != 0 601132730Skan && DECL_ABSTRACT_ORIGIN (p) != p) 602132730Skan TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1; 603132730Skan goto normal; 60418334Speter 605132730Skan case VAR_DECL: 606132730Skan /* Keep this in sync with stmt.c:warn_about_unused_variables. 607132730Skan No warnings when the global scope is popped because the 608132730Skan global scope isn't popped for the last translation unit, 609132730Skan so the warnings are done in c_write_global_declaration. */ 610132730Skan if (warn_unused_variable && scope != global_scope 611132730Skan && !TREE_USED (p) 612132730Skan && !DECL_IN_SYSTEM_HEADER (p) 613132730Skan && DECL_NAME (p) 614132730Skan && !DECL_ARTIFICIAL (p)) 615132730Skan warning ("%Junused variable `%D'", p, p); 616132730Skan /* fall through */ 61718334Speter 618132730Skan default: 619132730Skan normal: 620132730Skan if (DECL_NAME (p)) 621132730Skan { 622132730Skan if (DECL_EXTERNAL (p) && scope != global_scope) 623132730Skan /* External decls stay in the symbol-value slot but are 624132730Skan inaccessible. */ 625132730Skan C_DECL_INVISIBLE (p) = 1; 626132730Skan else 627132730Skan IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0; 628132730Skan } 629132730Skan break; 630132730Skan } 631132730Skan } 63250397Sobrien 633132730Skan /* Clear out the parameter bindings in this scope, if any. 634132730Skan Unused-parameter warnings are handled by function.c. */ 635132730Skan for (p = scope->parms; p; p = TREE_CHAIN (p)) 636132730Skan if (DECL_NAME (p)) 637132730Skan IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0; 63818334Speter 639132730Skan /* Clear out the tag-meanings declared in this scope. 64018334Speter 641132730Skan Set the TYPE_CONTEXTs for all of the tagged types belonging to 642132730Skan this scope so that they point to the appropriate construct, i.e. 643132730Skan either to the current FUNCTION_DECL node, or else to the BLOCK 644132730Skan node we just constructed. 64518334Speter 646132730Skan Note that for tagged types whose scope is just the formal 647132730Skan parameter list for some function type specification, we can't 648132730Skan properly set their TYPE_CONTEXTs here, because we don't have a 649132730Skan pointer to the appropriate FUNCTION_TYPE node readily available 650132730Skan to us. For those cases, the TYPE_CONTEXTs of the relevant tagged 651132730Skan type nodes get set in `grokdeclarator' as soon as we have created 652132730Skan the FUNCTION_TYPE node which will represent the "scope" for these 653132730Skan "parameter list local" tagged types. */ 65418334Speter 655132730Skan decl = scope->function_body ? current_function_decl : block; 656132730Skan for (p = scope->tags; p; p = TREE_CHAIN (p)) 657132730Skan { 658132730Skan if (TREE_PURPOSE (p)) 659132730Skan IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = 0; 660132730Skan if (decl) 661132730Skan TYPE_CONTEXT (TREE_VALUE (p)) = decl; 66218334Speter } 66318334Speter 664132730Skan /* Restore all name- and label-meanings from outer scopes that were 665132730Skan shadowed by this scope. */ 666132730Skan for (p = scope->shadowed; p; p = TREE_CHAIN (p)) 667132730Skan if (TREE_VALUE (p) && TREE_CODE (TREE_VALUE (p)) == LABEL_DECL) 668132730Skan IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p); 669132730Skan else 670132730Skan IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p); 67118334Speter 672132730Skan /* Restore all tag-meanings from outer scopes that were shadowed by 673132730Skan this scope. */ 674132730Skan for (p = scope->shadowed_tags; p; p = TREE_CHAIN (p)) 675132730Skan IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p); 67618334Speter 67718334Speter /* Dispose of the block that we just made inside some higher level. */ 678132730Skan if (scope->function_body && current_function_decl) 67918334Speter DECL_INITIAL (current_function_decl) = block; 680132730Skan else if (scope->outer) 68118334Speter { 682132730Skan if (block) 683132730Skan SCOPE_LIST_APPEND (scope->outer, blocks, block); 684132730Skan /* If we did not make a block for the scope just exited, any 685132730Skan blocks made for inner scopes must be carried forward so they 686132730Skan will later become subblocks of something else. */ 687132730Skan else if (scope->blocks) 688132730Skan SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks); 68918334Speter } 69018334Speter 691132730Skan /* Pop the current scope, and free the structure for reuse. */ 692132730Skan pop_scope (); 69318334Speter 69418334Speter return block; 69518334Speter} 69618334Speter 697132730Skan/* Insert BLOCK at the end of the list of subblocks of the current 698132730Skan scope. This is used when a BIND_EXPR is expanded, to handle the 699132730Skan BLOCK node inside the BIND_EXPR. */ 70018334Speter 70118334Spetervoid 702132730Skaninsert_block (tree block) 70318334Speter{ 70418334Speter TREE_USED (block) = 1; 705132730Skan SCOPE_LIST_APPEND (current_scope, blocks, block); 70618334Speter} 70718334Speter 708132730Skan/* Set the BLOCK node for the innermost scope (the one we are 709132730Skan currently in). The RTL expansion machinery requires us to provide 710132730Skan this hook, but it is not useful in function-at-a-time mode. */ 71118334Speter 71218334Spetervoid 713132730Skanset_block (tree block ATTRIBUTE_UNUSED) 71418334Speter{ 71518334Speter} 71618334Speter 71718334Speter/* Push a definition or a declaration of struct, union or enum tag "name". 71818334Speter "type" should be the type node. 71918334Speter We assume that the tag "name" is not already defined. 72018334Speter 72118334Speter Note that the definition may really be just a forward reference. 72218334Speter In that case, the TYPE_SIZE will be zero. */ 72318334Speter 72418334Spetervoid 725132730Skanpushtag (tree name, tree type) 72618334Speter{ 727132730Skan struct c_scope *b = current_scope; 72818334Speter 729132730Skan /* Record the identifier as the type's name if it has none. */ 73018334Speter if (name) 73118334Speter { 73218334Speter if (TYPE_NAME (type) == 0) 73318334Speter TYPE_NAME (type) = name; 734132730Skan 735132730Skan if (IDENTIFIER_TAG_VALUE (name)) 736132730Skan b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name), 737132730Skan b->shadowed_tags); 738132730Skan IDENTIFIER_TAG_VALUE (name) = type; 73918334Speter } 74018334Speter 74190075Sobrien b->tags = tree_cons (name, type, b->tags); 74218334Speter 74318334Speter /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the 744132730Skan tagged type we just added to the current scope. This fake 74518334Speter NULL-named TYPE_DECL node helps dwarfout.c to know when it needs 74618334Speter to output a representation of a tagged type, and it also gives 74718334Speter us a convenient place to record the "scope start" address for the 74818334Speter tagged type. */ 74918334Speter 75018334Speter TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type)); 75150397Sobrien 75250397Sobrien /* An approximation for now, so we can tell this is a function-scope tag. 75350397Sobrien This will be updated in poplevel. */ 75450397Sobrien TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type)); 75518334Speter} 75618334Speter 757132730Skan/* Subroutine of compare_decls. Allow harmless mismatches in return 758132730Skan and argument types provided that the type modes match. This function 759132730Skan return a unified type given a suitable match, and 0 otherwise. */ 76018334Speter 761132730Skanstatic tree 762132730Skanmatch_builtin_function_types (tree newtype, tree oldtype) 763132730Skan{ 764132730Skan tree newrettype, oldrettype; 765132730Skan tree newargs, oldargs; 766132730Skan tree trytype, tryargs; 76718334Speter 768132730Skan /* Accept the return type of the new declaration if same modes. */ 769132730Skan oldrettype = TREE_TYPE (oldtype); 770132730Skan newrettype = TREE_TYPE (newtype); 77150397Sobrien 772132730Skan if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype)) 773132730Skan return 0; 774132730Skan 775132730Skan oldargs = TYPE_ARG_TYPES (oldtype); 776132730Skan newargs = TYPE_ARG_TYPES (newtype); 777132730Skan tryargs = newargs; 778132730Skan 779132730Skan while (oldargs || newargs) 780132730Skan { 781132730Skan if (! oldargs 782132730Skan || ! newargs 783132730Skan || ! TREE_VALUE (oldargs) 784132730Skan || ! TREE_VALUE (newargs) 785132730Skan || TYPE_MODE (TREE_VALUE (oldargs)) 786132730Skan != TYPE_MODE (TREE_VALUE (newargs))) 787132730Skan return 0; 788132730Skan 789132730Skan oldargs = TREE_CHAIN (oldargs); 790132730Skan newargs = TREE_CHAIN (newargs); 791132730Skan } 792132730Skan 793132730Skan trytype = build_function_type (newrettype, tryargs); 794132730Skan return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype)); 795132730Skan} 796132730Skan 797132730Skan/* Subroutine of diagnose_mismathed_decls. Check for function type 798132730Skan mismatch involving an empty arglist vs a nonempty one and give clearer 799132730Skan diagnostics. */ 800132730Skanstatic void 801132730Skandiagnose_arglist_conflict (tree newdecl, tree olddecl, 802132730Skan tree newtype, tree oldtype) 80318334Speter{ 804132730Skan tree t; 80518334Speter 806132730Skan if (TREE_CODE (olddecl) != FUNCTION_DECL 807132730Skan || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype), COMPARE_STRICT) 808132730Skan || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0) 809132730Skan || 810132730Skan (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0))) 811132730Skan return; 812132730Skan 813132730Skan t = TYPE_ARG_TYPES (oldtype); 814132730Skan if (t == 0) 815132730Skan t = TYPE_ARG_TYPES (newtype); 816132730Skan for (; t; t = TREE_CHAIN (t)) 81790075Sobrien { 818132730Skan tree type = TREE_VALUE (t); 819132730Skan 820132730Skan if (TREE_CHAIN (t) == 0 821132730Skan && TYPE_MAIN_VARIANT (type) != void_type_node) 82290075Sobrien { 823132730Skan inform ("a parameter list with an ellipsis can't match " 824132730Skan "an empty parameter name list declaration"); 825132730Skan break; 82690075Sobrien } 82718334Speter 828132730Skan if (c_type_promotes_to (type) != type) 829132730Skan { 830132730Skan inform ("an argument type that has a default promotion can't match " 831132730Skan "an empty parameter name list declaration"); 832132730Skan break; 833132730Skan } 83490075Sobrien } 835132730Skan} 83690075Sobrien 837132730Skan/* Another subroutine of diagnose_mismatched_decls. OLDDECL is an 838132730Skan old-style function definition, NEWDECL is a prototype declaration. 839132730Skan Diagnose inconsistencies in the argument list. Returns TRUE if 840132730Skan the prototype is compatible, FALSE if not. */ 841132730Skanstatic bool 842132730Skanvalidate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype) 843132730Skan{ 844132730Skan tree newargs, oldargs; 845132730Skan int i; 84618334Speter 847132730Skan /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */ 848132730Skan#define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node) 849132730Skan 850132730Skan oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype); 851132730Skan newargs = TYPE_ARG_TYPES (newtype); 852132730Skan i = 1; 853132730Skan 854132730Skan for (;;) 855132730Skan { 856132730Skan tree oldargtype = TREE_VALUE (oldargs); 857132730Skan tree newargtype = TREE_VALUE (newargs); 858132730Skan 859132730Skan if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype)) 860132730Skan break; 861132730Skan 862132730Skan /* Reaching the end of just one list means the two decls don't 863132730Skan agree on the number of arguments. */ 864132730Skan if (END_OF_ARGLIST (oldargtype)) 865132730Skan { 866132730Skan error ("%Jprototype for '%D' declares more arguments " 867132730Skan "than previous old-style definition", newdecl, newdecl); 868132730Skan return false; 869132730Skan } 870132730Skan else if (END_OF_ARGLIST (newargtype)) 871132730Skan { 872132730Skan error ("%Jprototype for '%D' declares fewer arguments " 873132730Skan "than previous old-style definition", newdecl, newdecl); 874132730Skan return false; 875132730Skan } 876132730Skan 877132730Skan /* Type for passing arg must be consistent with that declared 878132730Skan for the arg. */ 879132730Skan else if (! comptypes (oldargtype, newargtype, COMPARE_STRICT)) 880132730Skan { 881132730Skan error ("%Jprototype for '%D' declares arg %d with incompatible type", 882132730Skan newdecl, newdecl, i); 883132730Skan return false; 884132730Skan } 885132730Skan 886132730Skan oldargs = TREE_CHAIN (oldargs); 887132730Skan newargs = TREE_CHAIN (newargs); 888132730Skan i++; 889132730Skan } 890132730Skan 891132730Skan /* If we get here, no errors were found, but do issue a warning 892132730Skan for this poor-style construct. */ 893132730Skan warning ("%Jprototype for '%D' follows non-prototype definition", 894132730Skan newdecl, newdecl); 895132730Skan return true; 896132730Skan#undef END_OF_ARGLIST 897132730Skan} 898132730Skan 899132730Skan/* Subroutine of diagnose_mismatched_decls. Report the location of DECL, 900132730Skan first in a pair of mismatched declarations, using the diagnostic 901132730Skan function DIAG. */ 902132730Skanstatic void 903132730Skanlocate_old_decl (tree decl, void (*diag)(const char *, ...)) 904132730Skan{ 905132730Skan if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)) 906132730Skan ; 907132730Skan else if (DECL_INITIAL (decl)) 908132730Skan diag (N_("%Jprevious definition of '%D' was here"), decl, decl); 909132730Skan else if (C_DECL_IMPLICIT (decl)) 910132730Skan diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl); 911132730Skan else 912132730Skan diag (N_("%Jprevious declaration of '%D' was here"), decl, decl); 913132730Skan} 914132730Skan 915132730Skan/* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL. 916132730Skan Returns true if the caller should proceed to merge the two, false 917132730Skan if OLDDECL should simply be discarded. As a side effect, issues 918132730Skan all necessary diagnostics for invalid or poor-style combinations. 919132730Skan If it returns true, writes the types of NEWDECL and OLDDECL to 920132730Skan *NEWTYPEP and *OLDTYPEP - these may have been adjusted from 921132730Skan TREE_TYPE (NEWDECL, OLDDECL) respectively. */ 922132730Skan 923132730Skanstatic bool 924132730Skandiagnose_mismatched_decls (tree newdecl, tree olddecl, 925132730Skan tree *newtypep, tree *oldtypep) 926132730Skan{ 927132730Skan tree newtype, oldtype; 928132730Skan bool pedwarned = false; 929132730Skan bool warned = false; 930132730Skan 931132730Skan /* If we have error_mark_node for either decl or type, just discard 932132730Skan the previous decl - we're in an error cascade already. */ 933132730Skan if (olddecl == error_mark_node || newdecl == error_mark_node) 934132730Skan return false; 935132730Skan *oldtypep = oldtype = TREE_TYPE (olddecl); 936132730Skan *newtypep = newtype = TREE_TYPE (newdecl); 937132730Skan if (oldtype == error_mark_node || newtype == error_mark_node) 938132730Skan return false; 939132730Skan 940132730Skan /* Two different categories of symbol altogether. This is an error 941132730Skan unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */ 94218334Speter if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) 94318334Speter { 944132730Skan if (TREE_CODE (olddecl) != FUNCTION_DECL 945132730Skan || !DECL_BUILT_IN (olddecl) || !C_DECL_INVISIBLE (olddecl)) 946132730Skan { 947132730Skan error ("%J'%D' redeclared as different kind of symbol", 948132730Skan newdecl, newdecl); 949132730Skan locate_old_decl (olddecl, error); 950132730Skan } 951132730Skan else if (TREE_PUBLIC (newdecl)) 952132730Skan warning ("%Jbuilt-in function '%D' declared as non-function", 953132730Skan newdecl, newdecl); 954132730Skan else if (warn_shadow) 955132730Skan warning ("%Jshadowing built-in function '%D'", 956132730Skan newdecl, newdecl); 957132730Skan return false; 958132730Skan } 959132730Skan 960132730Skan if (!comptypes (oldtype, newtype, COMPARE_STRICT)) 961132730Skan { 96218334Speter if (TREE_CODE (olddecl) == FUNCTION_DECL 963132730Skan && DECL_BUILT_IN (olddecl) && C_DECL_INVISIBLE (olddecl)) 96418334Speter { 965132730Skan /* Accept harmless mismatch in function types. 966132730Skan This is for the ffs and fprintf builtins. */ 967132730Skan tree trytype = match_builtin_function_types (newtype, oldtype); 968132730Skan 969132730Skan if (trytype && comptypes (newtype, trytype, COMPARE_STRICT)) 970132730Skan *oldtypep = oldtype = trytype; 971132730Skan else 97218334Speter { 973132730Skan /* If types don't match for a built-in, throw away the 974132730Skan built-in. No point in calling locate_old_decl here, it 975132730Skan won't print anything. */ 976132730Skan warning ("%Jconflicting types for built-in function '%D'", 977132730Skan newdecl, newdecl); 978132730Skan return false; 97918334Speter } 98018334Speter } 981132730Skan else if (TREE_CODE (olddecl) == FUNCTION_DECL 982132730Skan && DECL_SOURCE_LINE (olddecl) == 0) 983132730Skan { 984132730Skan /* A conflicting function declaration for a predeclared 985132730Skan function that isn't actually built in. Objective C uses 986132730Skan these. The new declaration silently overrides everything 987132730Skan but the volatility (i.e. noreturn) indication. See also 988132730Skan below. FIXME: Make Objective C use normal builtins. */ 989132730Skan TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 990132730Skan return false; 991132730Skan } 992132730Skan /* Permit void foo (...) to match int foo (...) if the latter is 993132730Skan the definition and implicit int was used. See 994132730Skan c-torture/compile/920625-2.c. */ 995132730Skan else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) 996132730Skan && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node 997132730Skan && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node 998132730Skan && C_FUNCTION_IMPLICIT_INT (newdecl)) 999132730Skan { 1000132730Skan pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl); 1001132730Skan /* Make sure we keep void as the return type. */ 1002132730Skan TREE_TYPE (newdecl) = *newtypep = newtype = oldtype; 1003132730Skan C_FUNCTION_IMPLICIT_INT (newdecl) = 0; 1004132730Skan pedwarned = true; 1005132730Skan } 100618334Speter else 100718334Speter { 1008132730Skan error ("%Jconflicting types for '%D'", newdecl, newdecl); 1009132730Skan diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype); 1010132730Skan locate_old_decl (olddecl, error); 1011132730Skan return false; 101218334Speter } 1013132730Skan } 101418334Speter 1015132730Skan /* Redeclaration of a type is a constraint violation (6.7.2.3p1), 1016132730Skan but silently ignore the redeclaration if either is in a system 1017132730Skan header. (Conflicting redeclarations were handled above.) */ 1018132730Skan if (TREE_CODE (newdecl) == TYPE_DECL) 1019132730Skan { 1020132730Skan if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl)) 1021132730Skan return true; /* allow OLDDECL to continue in use */ 1022132730Skan 1023132730Skan error ("%Jredefinition of typedef '%D'", newdecl, newdecl); 1024132730Skan locate_old_decl (olddecl, error); 1025132730Skan return false; 102618334Speter } 102718334Speter 1028132730Skan /* Function declarations can either be 'static' or 'extern' (no 1029132730Skan qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore 1030132730Skan can never conflict with each other on account of linkage (6.2.2p4). 1031132730Skan Multiple definitions are not allowed (6.9p3,5) but GCC permits 1032132730Skan two definitions if one is 'extern inline' and one is not. The non- 1033132730Skan extern-inline definition supersedes the extern-inline definition. */ 1034132730Skan else if (TREE_CODE (newdecl) == FUNCTION_DECL) 103518334Speter { 1036132730Skan /* If you declare a built-in function name as static, or 1037132730Skan define the built-in with an old-style definition (so we 1038132730Skan can't validate the argument list) the built-in definition is 1039132730Skan overridden, but optionally warn this was a bad choice of name. */ 1040132730Skan if (DECL_BUILT_IN (olddecl) 1041132730Skan && C_DECL_INVISIBLE (olddecl) 1042132730Skan && (!TREE_PUBLIC (newdecl) 1043132730Skan || (DECL_INITIAL (newdecl) 1044132730Skan && !TYPE_ARG_TYPES (TREE_TYPE (newdecl))))) 104518334Speter { 104618334Speter if (warn_shadow) 1047132730Skan warning ("%Jshadowing built-in function '%D'", newdecl, newdecl); 104818334Speter /* Discard the old built-in function. */ 1049132730Skan return false; 105018334Speter } 1051132730Skan 1052132730Skan if (DECL_INITIAL (newdecl)) 105318334Speter { 1054132730Skan if (DECL_INITIAL (olddecl) 1055132730Skan && !(DECL_DECLARED_INLINE_P (olddecl) 1056132730Skan && DECL_EXTERNAL (olddecl) 1057132730Skan && !(DECL_DECLARED_INLINE_P (newdecl) 1058132730Skan && DECL_EXTERNAL (newdecl)))) 105918334Speter { 1060132730Skan error ("%Jredefinition of '%D'", newdecl, newdecl); 1061132730Skan locate_old_decl (olddecl, error); 1062132730Skan return false; 106318334Speter } 1064132730Skan } 1065132730Skan /* If we have a prototype after an old-style function definition, 1066132730Skan the argument types must be checked specially. */ 1067132730Skan else if (DECL_INITIAL (olddecl) 1068132730Skan && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype) 1069132730Skan && TYPE_ACTUAL_ARG_TYPES (oldtype) 1070132730Skan && !validate_proto_after_old_defn (newdecl, newtype, oldtype)) 1071132730Skan { 1072132730Skan locate_old_decl (olddecl, error); 1073132730Skan return false; 1074132730Skan } 1075132730Skan /* Mismatched non-static and static is considered poor style. 1076132730Skan We only diagnose static then non-static if -Wtraditional, 1077132730Skan because it is the most convenient way to get some effects 1078132730Skan (see e.g. what unwind-dw2-fde-glibc.c does to the definition 1079132730Skan of _Unwind_Find_FDE in unwind-dw2-fde.c). Revisit? */ 1080132730Skan if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl)) 1081132730Skan { 1082132730Skan /* A static function declaration for a predeclared function 1083132730Skan that isn't actually built in, silently overrides the 1084132730Skan default. Objective C uses these. See also above. 1085132730Skan FIXME: Make Objective C use normal builtins. */ 1086132730Skan if (TREE_CODE (olddecl) == FUNCTION_DECL 1087132730Skan && DECL_SOURCE_LINE (olddecl) == 0) 1088132730Skan return false; 1089132730Skan else 109018334Speter { 1091132730Skan warning ("%Jstatic declaration of '%D' follows " 1092132730Skan "non-static declaration", newdecl, newdecl); 1093132730Skan warned = true; 109418334Speter } 109518334Speter } 1096132730Skan else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl) 1097132730Skan && warn_traditional) 109896263Sobrien { 1099132730Skan warning ("%Jnon-static declaration of '%D' follows " 1100132730Skan "static declaration", newdecl, newdecl); 1101132730Skan warned = true; 110296263Sobrien } 110318334Speter } 1104132730Skan else if (TREE_CODE (newdecl) == VAR_DECL) 110518334Speter { 1106132730Skan /* Only variables can be thread-local, and all declarations must 1107132730Skan agree on this property. */ 1108132730Skan if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl)) 110918334Speter { 1110132730Skan if (DECL_THREAD_LOCAL (newdecl)) 1111132730Skan error ("%Jthread-local declaration of '%D' follows " 1112132730Skan "non-thread-local declaration", newdecl, newdecl); 1113132730Skan else 1114132730Skan error ("%Jnon-thread-local declaration of '%D' follows " 1115132730Skan "thread-local declaration", newdecl, newdecl); 1116132730Skan 1117132730Skan locate_old_decl (olddecl, error); 1118132730Skan return false; 111918334Speter } 1120132730Skan 1121132730Skan /* Multiple initialized definitions are not allowed (6.9p3,5). */ 1122132730Skan if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl)) 112318334Speter { 1124132730Skan error ("%Jredefinition of '%D'", newdecl, newdecl); 1125132730Skan locate_old_decl (olddecl, error); 1126132730Skan return false; 112718334Speter } 112818334Speter 1129132730Skan /* Objects declared at file scope: if at least one is 'extern', 1130132730Skan it's fine (6.2.2p4); otherwise the linkage must agree (6.2.2p7). */ 1131132730Skan if (DECL_FILE_SCOPE_P (newdecl)) 113218334Speter { 1133132730Skan if (!DECL_EXTERNAL (newdecl) 1134132730Skan && !DECL_EXTERNAL (olddecl) 1135132730Skan && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl)) 113618334Speter { 1137132730Skan if (TREE_PUBLIC (newdecl)) 1138132730Skan error ("%Jnon-static declaration of '%D' follows " 1139132730Skan "static declaration", newdecl, newdecl); 1140132730Skan else 1141132730Skan error ("%Jstatic declaration of '%D' follows " 1142132730Skan "non-static declaration", newdecl, newdecl); 114318334Speter 1144132730Skan locate_old_decl (olddecl, error); 1145132730Skan return false; 114618334Speter } 114718334Speter } 1148132730Skan /* Two objects with the same name declared at the same block 1149132730Skan scope must both be external references (6.7p3). */ 1150132730Skan else if (DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl) 1151132730Skan && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl))) 1152132730Skan { 1153132730Skan if (DECL_EXTERNAL (newdecl)) 1154132730Skan error ("%Jextern declaration of '%D' follows " 1155132730Skan "declaration with no linkage", newdecl, newdecl); 1156132730Skan else if (DECL_EXTERNAL (olddecl)) 1157132730Skan error ("%Jdeclaration of '%D' with no linkage follows " 1158132730Skan "extern declaration", newdecl, newdecl); 1159132730Skan else 1160132730Skan error ("%Jredeclaration of '%D' with no linkage", 1161132730Skan newdecl, newdecl); 1162117421Skan 1163132730Skan locate_old_decl (olddecl, error); 1164132730Skan return false; 1165132730Skan } 116618334Speter } 1167132730Skan 1168132730Skan /* warnings */ 1169132730Skan /* All decls must agree on a non-default visibility. */ 1170132730Skan if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT 1171132730Skan && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT 1172132730Skan && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) 1173117421Skan { 1174132730Skan warning ("%Jredeclaration of '%D' with different visibility " 1175132730Skan "(old visibility preserved)", newdecl, newdecl); 1176132730Skan warned = true; 1177117421Skan } 1178132730Skan 1179132730Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 1180117421Skan { 1181132730Skan /* Diagnose inline __attribute__ ((noinline)) which is silly. */ 1182132730Skan if (DECL_DECLARED_INLINE_P (newdecl) 1183132730Skan && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 118418334Speter { 1185132730Skan warning ("%Jinline declaration of '%D' follows " 1186132730Skan "declaration with attribute noinline", newdecl, newdecl); 1187132730Skan warned = true; 118818334Speter } 1189132730Skan else if (DECL_DECLARED_INLINE_P (olddecl) 1190132730Skan && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) 119118334Speter { 1192132730Skan warning ("%Jdeclaration of '%D' with attribute noinline follows " 1193132730Skan "inline declaration ", newdecl, newdecl); 1194132730Skan warned = true; 119518334Speter } 1196132730Skan 1197132730Skan /* Inline declaration after use or definition. 1198132730Skan ??? Should we still warn about this now we have unit-at-a-time 1199132730Skan mode and can get it right? */ 1200132730Skan if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)) 120118334Speter { 1202132730Skan if (TREE_USED (olddecl)) 120318334Speter { 1204132730Skan warning ("%J'%D' declared inline after being called", 1205132730Skan olddecl, olddecl); 1206132730Skan warned = true; 120718334Speter } 1208132730Skan else if (DECL_INITIAL (olddecl)) 1209132730Skan { 1210132730Skan warning ("%J'%D' declared inline after its definition", 1211132730Skan olddecl, olddecl); 1212132730Skan warned = true; 1213132730Skan } 121418334Speter } 1215132730Skan } 1216132730Skan else /* PARM_DECL, VAR_DECL */ 1217132730Skan { 1218132730Skan /* Redeclaration of a PARM_DECL is invalid unless this is the 1219132730Skan real position of a forward-declared parameter (GCC extension). */ 1220132730Skan if (TREE_CODE (newdecl) == PARM_DECL 1221132730Skan && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl))) 122218334Speter { 1223132730Skan error ("%Jredefinition of parameter '%D'", newdecl, newdecl); 1224132730Skan locate_old_decl (olddecl, error); 1225132730Skan return false; 1226132730Skan } 122718334Speter 1228132730Skan /* These bits are only type qualifiers when applied to objects. */ 1229132730Skan if (TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)) 1230132730Skan { 1231132730Skan if (TREE_THIS_VOLATILE (newdecl)) 1232132730Skan pedwarn ("%Jvolatile declaration of '%D' follows " 1233132730Skan "non-volatile declaration", newdecl, newdecl); 1234132730Skan else 1235132730Skan pedwarn ("%Jnon-volatile declaration of '%D' follows " 1236132730Skan "volatile declaration", newdecl, newdecl); 1237132730Skan pedwarned = true; 123818334Speter } 1239132730Skan if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)) 1240132730Skan { 1241132730Skan if (TREE_READONLY (newdecl)) 1242132730Skan pedwarn ("%Jconst declaration of '%D' follows " 1243132730Skan "non-const declaration", newdecl, newdecl); 1244132730Skan else 1245132730Skan pedwarn ("%Jnon-const declaration of '%D' follows " 1246132730Skan "const declaration", newdecl, newdecl); 1247132730Skan pedwarned = true; 1248132730Skan } 124918334Speter } 125018334Speter 1251132730Skan /* Optional warning for completely redundant decls. */ 1252132730Skan if (!warned && !pedwarned 1253132730Skan && warn_redundant_decls 1254132730Skan /* Don't warn about a function declaration followed by a 1255132730Skan definition. */ 1256132730Skan && !(TREE_CODE (newdecl) == FUNCTION_DECL 1257132730Skan && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)) 1258132730Skan /* Don't warn about redundant redeclarations of builtins. */ 1259132730Skan && !(TREE_CODE (newdecl) == FUNCTION_DECL 1260132730Skan && !DECL_BUILT_IN (newdecl) 1261132730Skan && DECL_BUILT_IN (olddecl) 1262132730Skan && C_DECL_INVISIBLE (olddecl)) 1263132730Skan /* Don't warn about an extern followed by a definition. */ 1264132730Skan && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl)) 1265132730Skan /* Don't warn about forward parameter decls. */ 1266132730Skan && !(TREE_CODE (newdecl) == PARM_DECL 1267132730Skan && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))) 126818334Speter { 1269132730Skan warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl); 1270132730Skan warned = true; 127118334Speter } 127218334Speter 1273132730Skan /* Report location of previous decl/defn in a consistent manner. */ 1274132730Skan if (warned || pedwarned) 1275132730Skan locate_old_decl (olddecl, pedwarned ? pedwarn : warning); 127618334Speter 1277132730Skan return true; 1278132730Skan} 127918334Speter 1280132730Skan/* Subroutine of duplicate_decls. NEWDECL has been found to be 1281132730Skan consistent with OLDDECL, but carries new information. Merge the 1282132730Skan new information into OLDDECL. This function issues no 1283132730Skan diagnostics. */ 1284132730Skan 1285132730Skanstatic void 1286132730Skanmerge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype) 1287132730Skan{ 1288132730Skan int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL 1289132730Skan && DECL_INITIAL (newdecl) != 0); 1290132730Skan 1291132730Skan /* For real parm decl following a forward decl, return 1 so old decl 1292132730Skan will be reused. Only allow this to happen once. */ 1293132730Skan if (TREE_CODE (newdecl) == PARM_DECL 1294132730Skan && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl)) 129518334Speter { 1296132730Skan TREE_ASM_WRITTEN (olddecl) = 0; 1297132730Skan return; 1298132730Skan } 129950397Sobrien 1300132730Skan DECL_ATTRIBUTES (newdecl) 1301132730Skan = (*targetm.merge_decl_attributes) (olddecl, newdecl); 130218334Speter 1303132730Skan /* Merge the data types specified in the two decls. */ 1304132730Skan TREE_TYPE (newdecl) 1305132730Skan = TREE_TYPE (olddecl) 1306132730Skan = common_type (newtype, oldtype); 130718334Speter 1308132730Skan /* Lay the type out, unless already done. */ 1309132730Skan if (oldtype != TREE_TYPE (newdecl)) 1310132730Skan { 1311132730Skan if (TREE_TYPE (newdecl) != error_mark_node) 1312132730Skan layout_type (TREE_TYPE (newdecl)); 1313132730Skan if (TREE_CODE (newdecl) != FUNCTION_DECL 1314132730Skan && TREE_CODE (newdecl) != TYPE_DECL 1315132730Skan && TREE_CODE (newdecl) != CONST_DECL) 1316132730Skan layout_decl (newdecl, 0); 1317132730Skan } 1318132730Skan else 1319132730Skan { 1320132730Skan /* Since the type is OLDDECL's, make OLDDECL's size go with. */ 1321132730Skan DECL_SIZE (newdecl) = DECL_SIZE (olddecl); 1322132730Skan DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl); 1323132730Skan DECL_MODE (newdecl) = DECL_MODE (olddecl); 1324132730Skan if (TREE_CODE (olddecl) != FUNCTION_DECL) 1325132730Skan if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl)) 1326132730Skan { 1327132730Skan DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl); 1328132730Skan DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl); 1329132730Skan } 1330132730Skan } 133118334Speter 1332132730Skan /* Keep the old rtl since we can safely use it. */ 1333132730Skan COPY_DECL_RTL (olddecl, newdecl); 133490075Sobrien 1335132730Skan /* Merge the type qualifiers. */ 1336132730Skan if (TREE_READONLY (newdecl)) 1337132730Skan TREE_READONLY (olddecl) = 1; 133818334Speter 1339132730Skan if (TREE_THIS_VOLATILE (newdecl)) 1340132730Skan { 1341132730Skan TREE_THIS_VOLATILE (olddecl) = 1; 1342132730Skan if (TREE_CODE (newdecl) == VAR_DECL) 1343132730Skan make_var_volatile (newdecl); 1344132730Skan } 134518334Speter 1346132730Skan /* Keep source location of definition rather than declaration. */ 1347132730Skan if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0) 1348132730Skan DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); 134918334Speter 1350132730Skan /* Merge the unused-warning information. */ 1351132730Skan if (DECL_IN_SYSTEM_HEADER (olddecl)) 1352132730Skan DECL_IN_SYSTEM_HEADER (newdecl) = 1; 1353132730Skan else if (DECL_IN_SYSTEM_HEADER (newdecl)) 1354132730Skan DECL_IN_SYSTEM_HEADER (olddecl) = 1; 135518334Speter 1356132730Skan /* Merge the initialization information. */ 1357132730Skan if (DECL_INITIAL (newdecl) == 0) 1358132730Skan DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 135918334Speter 1360132730Skan /* Merge the section attribute. 1361132730Skan We want to issue an error if the sections conflict but that must be 1362132730Skan done later in decl_attributes since we are called before attributes 1363132730Skan are assigned. */ 1364132730Skan if (DECL_SECTION_NAME (newdecl) == NULL_TREE) 1365132730Skan DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); 136690075Sobrien 1367132730Skan /* Copy the assembler name. 1368132730Skan Currently, it can only be defined in the prototype. */ 1369132730Skan COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); 1370132730Skan 1371132730Skan /* If either declaration has a nondefault visibility, use it. */ 1372132730Skan if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT) 1373132730Skan DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); 1374132730Skan 1375132730Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 137618334Speter { 1377132730Skan DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl); 1378132730Skan DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); 1379132730Skan DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); 1380132730Skan DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) 1381132730Skan |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); 1382132730Skan TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1383132730Skan TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); 1384132730Skan DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); 1385132730Skan DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); 138618334Speter } 138718334Speter 138818334Speter /* Merge the storage class information. */ 138996263Sobrien merge_weak (newdecl, olddecl); 139096263Sobrien 139118334Speter /* For functions, static overrides non-static. */ 139218334Speter if (TREE_CODE (newdecl) == FUNCTION_DECL) 139318334Speter { 139418334Speter TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl); 139518334Speter /* This is since we don't automatically 139618334Speter copy the attributes of NEWDECL into OLDDECL. */ 139718334Speter TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 139818334Speter /* If this clears `static', clear it in the identifier too. */ 139918334Speter if (! TREE_PUBLIC (olddecl)) 140018334Speter TREE_PUBLIC (DECL_NAME (olddecl)) = 0; 140118334Speter } 140218334Speter if (DECL_EXTERNAL (newdecl)) 140318334Speter { 1404132730Skan TREE_STATIC (newdecl) = TREE_STATIC (olddecl); 1405132730Skan DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl); 1406132730Skan 140718334Speter /* An extern decl does not override previous storage class. */ 140818334Speter TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); 140950397Sobrien if (! DECL_EXTERNAL (newdecl)) 1410132730Skan { 1411132730Skan DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); 1412132730Skan DECL_COMMON (newdecl) = DECL_COMMON (olddecl); 1413132730Skan } 141418334Speter } 141518334Speter else 141618334Speter { 141718334Speter TREE_STATIC (olddecl) = TREE_STATIC (newdecl); 141818334Speter TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 141918334Speter } 142018334Speter 142150397Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL) 142218334Speter { 142390075Sobrien /* If we're redefining a function previously defined as extern 142490075Sobrien inline, make sure we emit debug info for the inline before we 142590075Sobrien throw it away, in case it was inlined into a function that hasn't 142690075Sobrien been written out yet. */ 1427110623Skan if (new_is_definition && DECL_INITIAL (olddecl)) 142890075Sobrien { 1429132730Skan if (TREE_USED (olddecl) 1430132730Skan /* In unit-at-a-time mode we never inline re-defined extern 1431132730Skan inline functions. */ 1432132730Skan && !flag_unit_at_a_time 1433132730Skan && cgraph_function_possibly_inlined_p (olddecl)) 1434110623Skan (*debug_hooks->outlining_inline_function) (olddecl); 143590075Sobrien 143690075Sobrien /* The new defn must not be inline. */ 143790075Sobrien DECL_INLINE (newdecl) = 0; 143890075Sobrien DECL_UNINLINABLE (newdecl) = 1; 143990075Sobrien } 144090075Sobrien else 144190075Sobrien { 144290075Sobrien /* If either decl says `inline', this fn is inline, 144390075Sobrien unless its definition was passed already. */ 144490075Sobrien if (DECL_DECLARED_INLINE_P (newdecl) 144590075Sobrien || DECL_DECLARED_INLINE_P (olddecl)) 144690075Sobrien DECL_DECLARED_INLINE_P (newdecl) = 1; 144790075Sobrien 144890075Sobrien DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) 144990075Sobrien = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); 145090075Sobrien } 145190075Sobrien 145218334Speter if (DECL_BUILT_IN (olddecl)) 145318334Speter { 1454132730Skan /* If redeclaring a builtin function, it stays built in. */ 1455132730Skan DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); 1456132730Skan DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); 145718334Speter } 145890075Sobrien 145950397Sobrien /* Also preserve various other info from the definition. */ 146050397Sobrien if (! new_is_definition) 146150397Sobrien { 146250397Sobrien DECL_RESULT (newdecl) = DECL_RESULT (olddecl); 1463132730Skan DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 146450397Sobrien DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl); 146590075Sobrien DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); 146650397Sobrien DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); 146790075Sobrien 146890075Sobrien /* Set DECL_INLINE on the declaration if we've got a body 146990075Sobrien from which to instantiate. */ 147090075Sobrien if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl)) 147190075Sobrien { 147290075Sobrien DECL_INLINE (newdecl) = 1; 147390075Sobrien DECL_ABSTRACT_ORIGIN (newdecl) 1474132730Skan = DECL_ABSTRACT_ORIGIN (olddecl); 147590075Sobrien } 147650397Sobrien } 147790075Sobrien else 147890075Sobrien { 147990075Sobrien /* If a previous declaration said inline, mark the 148090075Sobrien definition as inlinable. */ 148190075Sobrien if (DECL_DECLARED_INLINE_P (newdecl) 148290075Sobrien && ! DECL_UNINLINABLE (newdecl)) 148390075Sobrien DECL_INLINE (newdecl) = 1; 148490075Sobrien } 148550397Sobrien } 148618334Speter 148718334Speter /* Copy most of the decl-specific fields of NEWDECL into OLDDECL. 1488132730Skan But preserve OLDDECL's DECL_UID and C_DECL_INVISIBLE. */ 148918334Speter { 149090075Sobrien unsigned olddecl_uid = DECL_UID (olddecl); 1491132730Skan unsigned olddecl_invisible = C_DECL_INVISIBLE (olddecl); 149218334Speter 149390075Sobrien memcpy ((char *) olddecl + sizeof (struct tree_common), 149490075Sobrien (char *) newdecl + sizeof (struct tree_common), 149590075Sobrien sizeof (struct tree_decl) - sizeof (struct tree_common)); 149618334Speter DECL_UID (olddecl) = olddecl_uid; 1497132730Skan C_DECL_INVISIBLE (olddecl) = olddecl_invisible; 149818334Speter } 149918334Speter 1500132730Skan /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl 1501119268Skan so that encode_section_info has a chance to look at the new decl 1502119268Skan flags and attributes. */ 1503119268Skan if (DECL_RTL_SET_P (olddecl) 1504119268Skan && (TREE_CODE (olddecl) == FUNCTION_DECL 1505119268Skan || (TREE_CODE (olddecl) == VAR_DECL 1506119268Skan && TREE_STATIC (olddecl)))) 1507119268Skan make_decl_rtl (olddecl, NULL); 1508132730Skan} 1509119268Skan 1510132730Skan/* Handle when a new declaration NEWDECL has the same name as an old 1511132730Skan one OLDDECL in the same binding contour. Prints an error message 1512132730Skan if appropriate. 1513132730Skan 1514132730Skan If safely possible, alter OLDDECL to look like NEWDECL, and return 1515132730Skan true. Otherwise, return false. */ 1516132730Skan 1517132730Skanstatic bool 1518132730Skanduplicate_decls (tree newdecl, tree olddecl) 1519132730Skan{ 1520132730Skan tree newtype, oldtype; 1521132730Skan 1522132730Skan if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype)) 1523132730Skan return false; 1524132730Skan 1525132730Skan merge_decls (newdecl, olddecl, newtype, oldtype); 1526132730Skan return true; 152718334Speter} 1528132730Skan 1529132730Skan 1530132730Skan/* Return any external DECL associated with ID, whether or not it is 1531132730Skan currently in scope. */ 153218334Speter 1533132730Skanstatic tree 1534132730Skanany_external_decl (tree id) 1535132730Skan{ 1536132730Skan tree decl = IDENTIFIER_SYMBOL_VALUE (id); 1537132730Skan tree t; 1538132730Skan 1539132730Skan if (decl == 0 || TREE_CODE (decl) == ERROR_MARK) 1540132730Skan return 0; 1541132730Skan else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl)) 1542132730Skan return decl; 1543132730Skan 1544132730Skan t = purpose_member (id, truly_local_externals); 1545132730Skan if (t) 1546132730Skan return TREE_VALUE (t); 1547132730Skan 1548132730Skan return 0; 1549132730Skan} 1550132730Skan 1551132730Skan/* Record an external decl DECL. This only does something if a 1552132730Skan shadowing decl already exists. */ 1553132730Skanstatic void 1554132730Skanrecord_external_decl (tree decl) 1555132730Skan{ 1556132730Skan tree name = DECL_NAME (decl); 1557132730Skan if (!IDENTIFIER_SYMBOL_VALUE (name)) 1558132730Skan return; 1559132730Skan 1560132730Skan truly_local_externals = tree_cons (name, decl, truly_local_externals); 1561132730Skan} 1562132730Skan 156390075Sobrien/* Check whether decl-node X shadows an existing declaration. 1564132730Skan OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X, 156590075Sobrien which might be a NULL_TREE. */ 156690075Sobrienstatic void 1567132730Skanwarn_if_shadowing (tree x, tree old) 156890075Sobrien{ 1569132730Skan /* Nothing to shadow? */ 1570132730Skan if (old == 0 1571132730Skan /* Shadow warnings not wanted? */ 1572132730Skan || !warn_shadow 1573132730Skan /* No shadow warnings for internally generated vars. */ 1574132730Skan || DECL_SOURCE_LINE (x) == 0 1575132730Skan /* No shadow warnings for vars made for inlining. */ 1576132730Skan || DECL_FROM_INLINE (x) 1577132730Skan /* Don't warn about the parm names in function declarator 1578132730Skan within a function declarator. 1579132730Skan It would be nice to avoid warning in any function 1580132730Skan declarator in a declaration, as opposed to a definition, 1581132730Skan but there is no way to tell it's not a definition. */ 1582132730Skan || (TREE_CODE (x) == PARM_DECL && current_scope->outer->parm_flag) 1583132730Skan /* Shadow warnings only apply to local variables and parameters. */ 1584132730Skan || (TREE_CODE (x) != PARM_DECL && DECL_FILE_SCOPE_P (x))) 158590075Sobrien return; 158690075Sobrien 1587132730Skan if (TREE_CODE (old) == PARM_DECL) 1588132730Skan warning ("%Jdeclaration of '%D' shadows a parameter", x, x); 1589132730Skan else if (DECL_FILE_SCOPE_P (old)) 1590132730Skan warning ("%Jdeclaration of '%D' shadows a global declaration", x, x); 1591132730Skan else 1592132730Skan warning ("%Jdeclaration of '%D' shadows a previous local", x, x); 159390075Sobrien 1594132730Skan warning ("%Jshadowed declaration is here", old); 1595132730Skan} 1596132730Skan 1597132730Skan 1598132730Skan/* Subroutine of pushdecl. 1599132730Skan 1600132730Skan X is a TYPE_DECL for a typedef statement. Create a brand new 1601132730Skan ..._TYPE node (which will be just a variant of the existing 1602132730Skan ..._TYPE node with identical properties) and then install X 1603132730Skan as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. 1604132730Skan 1605132730Skan The whole point here is to end up with a situation where each 1606132730Skan and every ..._TYPE node the compiler creates will be uniquely 1607132730Skan associated with AT MOST one node representing a typedef name. 1608132730Skan This way, even though the compiler substitutes corresponding 1609132730Skan ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very 1610132730Skan early on, later parts of the compiler can always do the reverse 1611132730Skan translation and get back the corresponding typedef name. For 1612132730Skan example, given: 1613132730Skan 1614132730Skan typedef struct S MY_TYPE; 1615132730Skan MY_TYPE object; 1616132730Skan 1617132730Skan Later parts of the compiler might only know that `object' was of 1618132730Skan type `struct S' if it were not for code just below. With this 1619132730Skan code however, later parts of the compiler see something like: 1620132730Skan 1621132730Skan struct S' == struct S 1622132730Skan typedef struct S' MY_TYPE; 1623132730Skan struct S' object; 1624132730Skan 1625132730Skan And they can then deduce (from the node for type struct S') that 1626132730Skan the original object declaration was: 1627132730Skan 1628132730Skan MY_TYPE object; 1629132730Skan 1630132730Skan Being able to do this is important for proper support of protoize, 1631132730Skan and also for generating precise symbolic debugging information 1632132730Skan which takes full account of the programmer's (typedef) vocabulary. 1633132730Skan 1634132730Skan Obviously, we don't want to generate a duplicate ..._TYPE node if 1635132730Skan the TYPE_DECL node that we are now processing really represents a 1636132730Skan standard built-in type. 1637132730Skan 1638132730Skan Since all standard types are effectively declared at line zero 1639132730Skan in the source file, we can easily check to see if we are working 1640132730Skan on a standard type by checking the current value of lineno. */ 1641132730Skan 1642132730Skanstatic void 1643132730Skanclone_underlying_type (tree x) 1644132730Skan{ 1645132730Skan if (DECL_SOURCE_LINE (x) == 0) 164690075Sobrien { 1647132730Skan if (TYPE_NAME (TREE_TYPE (x)) == 0) 1648132730Skan TYPE_NAME (TREE_TYPE (x)) = x; 164990075Sobrien } 1650132730Skan else if (TREE_TYPE (x) != error_mark_node 1651132730Skan && DECL_ORIGINAL_TYPE (x) == NULL_TREE) 165290075Sobrien { 1653132730Skan tree tt = TREE_TYPE (x); 1654132730Skan DECL_ORIGINAL_TYPE (x) = tt; 1655132730Skan tt = build_type_copy (tt); 1656132730Skan TYPE_NAME (tt) = x; 1657132730Skan TREE_USED (tt) = TREE_USED (x); 1658132730Skan TREE_TYPE (x) = tt; 165990075Sobrien } 166090075Sobrien} 166190075Sobrien 166218334Speter/* Record a decl-node X as belonging to the current lexical scope. 166318334Speter Check for errors (such as an incompatible declaration for the same 166418334Speter name already seen in the same scope). 166518334Speter 166618334Speter Returns either X or an old decl for the same name. 166718334Speter If an old decl is returned, it may have been smashed 166818334Speter to agree with what X says. */ 166918334Speter 167018334Spetertree 1671132730Skanpushdecl (tree x) 167218334Speter{ 167390075Sobrien tree name = DECL_NAME (x); 1674132730Skan struct c_scope *scope = current_scope; 167518334Speter 1676132730Skan#ifdef ENABLE_CHECKING 1677132730Skan if (error_mark_node == 0) 1678132730Skan /* Called too early. */ 1679132730Skan abort (); 1680132730Skan#endif 1681132730Skan 168290075Sobrien /* Functions need the lang_decl data. */ 168390075Sobrien if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x)) 1684132730Skan DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl)); 168590075Sobrien 168618334Speter /* A local extern declaration for a function doesn't constitute nesting. 168718334Speter A local auto declaration does, since it's a forward decl 168818334Speter for a nested function coming later. */ 1689132730Skan if (current_function_decl == NULL 1690132730Skan || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL) 1691132730Skan && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x))) 1692132730Skan DECL_CONTEXT (x) = current_file_decl; 1693132730Skan else 1694132730Skan DECL_CONTEXT (x) = current_function_decl; 169518334Speter 169618334Speter if (name) 169718334Speter { 1698132730Skan tree old; 169918334Speter 170090075Sobrien if (warn_nested_externs 1701132730Skan && scope != global_scope 170290075Sobrien && DECL_EXTERNAL (x) 170390075Sobrien && !DECL_IN_SYSTEM_HEADER (x)) 170490075Sobrien warning ("nested extern declaration of `%s'", 170590075Sobrien IDENTIFIER_POINTER (name)); 170690075Sobrien 1707132730Skan old = lookup_name_current_level (name); 1708132730Skan if (old && duplicate_decls (x, old)) 170950397Sobrien { 1710132730Skan /* For PARM_DECLs, old may be a forward declaration. 1711132730Skan If so, we want to remove it from its old location 1712132730Skan (in the variables chain) and rechain it in the 1713132730Skan location given by the new declaration. */ 1714132730Skan if (TREE_CODE (x) == PARM_DECL) 171518334Speter { 1716132730Skan tree *p; 1717132730Skan for (p = &scope->names; *p; p = &TREE_CHAIN (*p)) 1718132730Skan if (*p == old) 1719132730Skan { 1720132730Skan *p = TREE_CHAIN (old); 1721132730Skan SCOPE_LIST_APPEND (scope, parms, old); 1722132730Skan break; 1723132730Skan } 172418334Speter } 1725132730Skan return old; 172618334Speter } 1727132730Skan if (DECL_EXTERNAL (x) || scope == global_scope) 172890075Sobrien { 1729132730Skan /* Find and check against a previous, not-in-scope, external 1730132730Skan decl for this identifier. (C99 6.2.7p2: All declarations 1731132730Skan that refer to the same object or function shall have 1732132730Skan compatible type; otherwise, the behavior is undefined.) */ 1733132730Skan tree ext = any_external_decl (name); 1734132730Skan if (ext) 173590075Sobrien { 1736132730Skan if (duplicate_decls (x, ext)) 1737132730Skan x = copy_node (ext); 173890075Sobrien } 173918334Speter else 1740132730Skan record_external_decl (x); 174118334Speter } 174218334Speter 1743132730Skan if (TREE_CODE (x) == TYPE_DECL) 1744132730Skan clone_underlying_type (x); 174518334Speter 1746132730Skan /* If storing a local value, there may already be one 1747132730Skan (inherited). If so, record it for restoration when this 1748132730Skan scope ends. Take care not to do this if we are replacing an 1749132730Skan older decl in the same scope (i.e. duplicate_decls returned 1750132730Skan false, above). */ 1751132730Skan if (scope != global_scope) 175218334Speter { 1753132730Skan tree inherited_decl = lookup_name (name); 1754132730Skan if (inherited_decl && inherited_decl != old) 175518334Speter { 1756132730Skan warn_if_shadowing (x, inherited_decl); 1757132730Skan scope->shadowed = tree_cons (name, inherited_decl, 1758132730Skan scope->shadowed); 175918334Speter } 176018334Speter } 176190075Sobrien 1762132730Skan /* Install the new declaration in the requested scope. */ 1763132730Skan IDENTIFIER_SYMBOL_VALUE (name) = x; 1764132730Skan C_DECL_INVISIBLE (x) = 0; 176518334Speter 1766132730Skan /* If x's type is incomplete because it's based on a 1767132730Skan structure or union which has not yet been fully declared, 1768132730Skan attach it to that structure or union type, so we can go 1769132730Skan back and complete the variable declaration later, if the 1770132730Skan structure or union gets fully declared. 177118334Speter 177290075Sobrien If the input is erroneous, we can have error_mark in the type 177390075Sobrien slot (e.g. "f(void a, ...)") - that doesn't count as an 177490075Sobrien incomplete type. */ 177590075Sobrien if (TREE_TYPE (x) != error_mark_node 177690075Sobrien && !COMPLETE_TYPE_P (TREE_TYPE (x))) 177790075Sobrien { 177890075Sobrien tree element = TREE_TYPE (x); 177990075Sobrien 178090075Sobrien while (TREE_CODE (element) == ARRAY_TYPE) 178190075Sobrien element = TREE_TYPE (element); 1782132730Skan element = TYPE_MAIN_VARIANT (element); 1783132730Skan 1784122196Skan if ((TREE_CODE (element) == RECORD_TYPE 1785122196Skan || TREE_CODE (element) == UNION_TYPE) 1786122196Skan && (TREE_CODE (x) != TYPE_DECL 1787132730Skan || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE) 1788132730Skan && !COMPLETE_TYPE_P (element)) 1789132730Skan C_TYPE_INCOMPLETE_VARS (element) 1790132730Skan = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element)); 179190075Sobrien } 179218334Speter } 179318334Speter 1794132730Skan if (TREE_CODE (x) == PARM_DECL) 1795132730Skan SCOPE_LIST_APPEND (scope, parms, x); 1796132730Skan else 1797132730Skan SCOPE_LIST_APPEND (scope, names, x); 179818334Speter 179918334Speter return x; 180018334Speter} 180118334Speter 1802132730Skan/* Record X as belonging to the global scope (C99 "file scope"). 1803132730Skan This is used only internally by the Objective-C front end, 1804132730Skan and is limited to its needs. duplicate_decls is not called; 1805132730Skan if there is any preexisting decl for this identifier, it is an ICE. */ 180618334Speter 180718334Spetertree 1808132730Skanpushdecl_top_level (tree x) 180918334Speter{ 1810132730Skan tree name; 181118334Speter 1812132730Skan if (TREE_CODE (x) != VAR_DECL) 1813132730Skan abort (); 1814132730Skan 1815132730Skan name = DECL_NAME (x); 1816132730Skan 1817132730Skan if (IDENTIFIER_SYMBOL_VALUE (name)) 1818132730Skan abort (); 1819132730Skan 1820132730Skan DECL_CONTEXT (x) = current_file_decl; 1821132730Skan IDENTIFIER_SYMBOL_VALUE (name) = x; 1822132730Skan 1823132730Skan SCOPE_LIST_APPEND (global_scope, names, x); 1824132730Skan return x; 182518334Speter} 182618334Speter 1827132730Skan/* Generate an implicit declaration for identifier FUNCTIONID as a 1828132730Skan function of type int (). */ 182918334Speter 183018334Spetertree 1831132730Skanimplicitly_declare (tree functionid) 183218334Speter{ 1833132730Skan tree decl = any_external_decl (functionid); 183418334Speter 1835132730Skan if (decl) 1836132730Skan { 1837132730Skan /* Implicit declaration of a function already declared 1838132730Skan (somehow) in a different scope, or as a built-in. 1839132730Skan If this is the first time this has happened, warn; 1840132730Skan then recycle the old declaration. */ 1841132730Skan if (!C_DECL_IMPLICIT (decl)) 1842132730Skan { 1843132730Skan implicit_decl_warning (DECL_NAME (decl)); 1844132730Skan if (! DECL_FILE_SCOPE_P (decl)) 1845132730Skan warning ("%Jprevious declaration of '%D'", decl, decl); 1846132730Skan C_DECL_IMPLICIT (decl) = 1; 1847132730Skan } 1848132730Skan /* If this function is global, then it must already be in the 1849132730Skan global scope, so there's no need to push it again. */ 1850132730Skan if (current_scope == global_scope) 1851132730Skan return decl; 1852132730Skan /* If this is a local declaration, make a copy; we can't have 1853132730Skan the same DECL listed in two different scopes. */ 1854132730Skan return pushdecl (copy_node (decl)); 1855132730Skan } 185618334Speter 1857132730Skan /* Not seen before. */ 1858132730Skan decl = build_decl (FUNCTION_DECL, functionid, default_function_type); 185918334Speter DECL_EXTERNAL (decl) = 1; 186018334Speter TREE_PUBLIC (decl) = 1; 1861132730Skan C_DECL_IMPLICIT (decl) = 1; 1862132730Skan implicit_decl_warning (functionid); 186318334Speter 1864132730Skan /* C89 says implicit declarations are in the innermost block. 1865117421Skan So we record the decl in the standard fashion. */ 1866132730Skan decl = pushdecl (decl); 186718334Speter 1868132730Skan /* No need to call objc_check_decl here - it's a function type. */ 186990075Sobrien rest_of_decl_compilation (decl, NULL, 0, 0); 187018334Speter 1871132730Skan /* Write a record describing this implicit function declaration 1872132730Skan to the prototypes file (if requested). */ 187318334Speter gen_aux_info_record (decl, 0, 1, 0); 187418334Speter 187590075Sobrien /* Possibly apply some default attributes to this implicit declaration. */ 187690075Sobrien decl_attributes (&decl, NULL_TREE, 0); 187718334Speter 187818334Speter return decl; 187918334Speter} 188018334Speter 1881132730Skanstatic void 1882132730Skanimplicit_decl_warning (tree id) 188390075Sobrien{ 188490075Sobrien const char *name = IDENTIFIER_POINTER (id); 188590075Sobrien if (mesg_implicit_function_declaration == 2) 188690075Sobrien error ("implicit declaration of function `%s'", name); 188790075Sobrien else if (mesg_implicit_function_declaration == 1) 188890075Sobrien warning ("implicit declaration of function `%s'", name); 188990075Sobrien} 189090075Sobrien 1891132730Skan/* Issue an error message for a reference to an undeclared variable 1892132730Skan ID, including a reference to a builtin outside of function-call 1893132730Skan context. Establish a binding of the identifier to error_mark_node 1894132730Skan in an appropriate scope, which will suppress further errors for the 1895132730Skan same identifier. */ 1896132730Skanvoid 1897132730Skanundeclared_variable (tree id) 1898132730Skan{ 1899132730Skan static bool already = false; 1900132730Skan struct c_scope *scope; 190118334Speter 1902132730Skan if (current_function_decl == 0) 190318334Speter { 1904132730Skan error ("`%s' undeclared here (not in a function)", 1905132730Skan IDENTIFIER_POINTER (id)); 1906132730Skan scope = current_scope; 190718334Speter } 190818334Speter else 190918334Speter { 1910132730Skan error ("`%s' undeclared (first use in this function)", 1911132730Skan IDENTIFIER_POINTER (id)); 1912132730Skan 1913132730Skan if (! already) 1914132730Skan { 1915132730Skan error ("(Each undeclared identifier is reported only once"); 1916132730Skan error ("for each function it appears in.)"); 1917132730Skan already = true; 1918132730Skan } 1919132730Skan 1920132730Skan scope = current_function_scope; 192118334Speter } 1922132730Skan 1923132730Skan scope->shadowed = tree_cons (id, IDENTIFIER_SYMBOL_VALUE (id), 1924132730Skan scope->shadowed); 1925132730Skan IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node; 192618334Speter} 192718334Speter 1928132730Skan/* Subroutine of lookup_label, declare_label, define_label: construct a 1929132730Skan LABEL_DECL with all the proper frills. */ 1930132730Skan 1931132730Skanstatic tree 1932132730Skanmake_label (tree name, location_t location) 1933132730Skan{ 1934132730Skan tree label = build_decl (LABEL_DECL, name, void_type_node); 1935132730Skan 1936132730Skan DECL_CONTEXT (label) = current_function_decl; 1937132730Skan DECL_MODE (label) = VOIDmode; 1938132730Skan DECL_SOURCE_LOCATION (label) = location; 1939132730Skan 1940132730Skan return label; 1941132730Skan} 1942132730Skan 1943132730Skan/* Another subroutine of lookup_label, declare_label, define_label: 1944132730Skan set up the binding of name to LABEL_DECL in the given SCOPE. */ 1945132730Skan 1946132730Skanstatic void 1947132730Skanbind_label (tree name, tree label, struct c_scope *scope) 1948132730Skan{ 1949132730Skan if (IDENTIFIER_LABEL_VALUE (name)) 1950132730Skan scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name), 1951132730Skan scope->shadowed); 1952132730Skan IDENTIFIER_LABEL_VALUE (name) = label; 1953132730Skan 1954132730Skan SCOPE_LIST_APPEND (scope, names, label); 1955132730Skan} 1956132730Skan 1957132730Skan/* Get the LABEL_DECL corresponding to identifier NAME as a label. 195818334Speter Create one if none exists so far for the current function. 1959132730Skan This is called when a label is used in a goto expression or 1960132730Skan has its address taken. */ 196118334Speter 196218334Spetertree 1963132730Skanlookup_label (tree name) 196418334Speter{ 1965132730Skan tree label; 196618334Speter 196718334Speter if (current_function_decl == 0) 196818334Speter { 196918334Speter error ("label %s referenced outside of any function", 1970132730Skan IDENTIFIER_POINTER (name)); 197118334Speter return 0; 197218334Speter } 197318334Speter 1974132730Skan /* Use a label already defined or ref'd with this name, but not if 1975132730Skan it is inherited from a containing function and wasn't declared 1976132730Skan using __label__. */ 1977132730Skan label = IDENTIFIER_LABEL_VALUE (name); 1978132730Skan if (label && (DECL_CONTEXT (label) == current_function_decl 1979132730Skan || C_DECLARED_LABEL_FLAG (label))) 198018334Speter { 1981132730Skan /* If the label has only been declared, update its apparent 1982132730Skan location to point here, for better diagnostics if it 1983132730Skan turns out not to have been defined. */ 1984132730Skan if (!TREE_USED (label)) 1985132730Skan DECL_SOURCE_LOCATION (label) = input_location; 1986132730Skan return label; 198718334Speter } 198818334Speter 1989132730Skan /* No label binding for that identifier; make one. */ 1990132730Skan label = make_label (name, input_location); 199118334Speter 1992132730Skan /* Ordinary labels go in the current function scope. */ 1993132730Skan bind_label (name, label, current_function_scope); 1994132730Skan return label; 199518334Speter} 199618334Speter 1997132730Skan/* Make a label named NAME in the current function, shadowing silently 1998132730Skan any that may be inherited from containing functions or containing 1999132730Skan scopes. This is called for __label__ declarations. */ 200018334Speter 2001132730Skan/* Note that valid use, if the label being shadowed comes from another 2002132730Skan scope in the same function, requires calling declare_nonlocal_label 2003132730Skan right away. (Is this still true? -zw 2003-07-17) */ 200418334Speter 200518334Spetertree 2006132730Skandeclare_label (tree name) 200718334Speter{ 2008132730Skan tree label = IDENTIFIER_LABEL_VALUE (name); 2009132730Skan tree dup; 201018334Speter 2011132730Skan /* Check to make sure that the label hasn't already been declared 2012132730Skan at this scope */ 2013132730Skan for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup)) 2014132730Skan if (dup == label) 2015132730Skan { 2016132730Skan error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name)); 2017132730Skan error ("%Jthis is a previous declaration", dup); 201818334Speter 2019132730Skan /* Just use the previous declaration. */ 2020132730Skan return dup; 2021132730Skan } 202218334Speter 2023132730Skan label = make_label (name, input_location); 2024132730Skan C_DECLARED_LABEL_FLAG (label) = 1; 202518334Speter 2026132730Skan /* Declared labels go in the current scope. */ 2027132730Skan bind_label (name, label, current_scope); 2028132730Skan return label; 202918334Speter} 203018334Speter 203118334Speter/* Define a label, specifying the location in the source file. 203218334Speter Return the LABEL_DECL node for the label, if the definition is valid. 203318334Speter Otherwise return 0. */ 203418334Speter 203518334Spetertree 2036132730Skandefine_label (location_t location, tree name) 203718334Speter{ 2038132730Skan tree label; 203918334Speter 2040132730Skan /* Find any preexisting label with this name. It is an error 2041132730Skan if that label has already been defined in this function, or 2042132730Skan if there is a containing function with a declared label with 2043132730Skan the same name. */ 2044132730Skan label = IDENTIFIER_LABEL_VALUE (name); 204518334Speter 2046132730Skan if (label 2047132730Skan && ((DECL_CONTEXT (label) == current_function_decl 2048132730Skan && DECL_INITIAL (label) != 0) 2049132730Skan || (DECL_CONTEXT (label) != current_function_decl 2050132730Skan && C_DECLARED_LABEL_FLAG (label)))) 205118334Speter { 2052132730Skan error ("%Hduplicate label `%D'", &location, label); 2053132730Skan if (DECL_INITIAL (label)) 2054132730Skan error ("%J`%D' previously defined here", label, label); 2055132730Skan else 2056132730Skan error ("%J`%D' previously declared here", label, label); 205718334Speter return 0; 205818334Speter } 2059132730Skan else if (label && DECL_CONTEXT (label) == current_function_decl) 2060132730Skan { 2061132730Skan /* The label has been used or declared already in this function, 2062132730Skan but not defined. Update its location to point to this 2063132730Skan definition. */ 2064132730Skan DECL_SOURCE_LOCATION (label) = location; 2065132730Skan } 206618334Speter else 206718334Speter { 2068132730Skan /* No label binding for that identifier; make one. */ 2069132730Skan label = make_label (name, location); 2070132730Skan 2071132730Skan /* Ordinary labels go in the current function scope. */ 2072132730Skan bind_label (name, label, current_function_scope); 207318334Speter } 2074132730Skan 2075132730Skan if (warn_traditional && !in_system_header && lookup_name (name)) 2076132730Skan warning ("%Htraditional C lacks a separate namespace for labels, " 2077132730Skan "identifier `%s' conflicts", &location, 2078132730Skan IDENTIFIER_POINTER (name)); 2079132730Skan 2080132730Skan /* Mark label as having been defined. */ 2081132730Skan DECL_INITIAL (label) = error_mark_node; 2082132730Skan return label; 208318334Speter} 208418334Speter 2085132730Skan/* Return the list of declarations of the current scope. */ 208618334Speter 208718334Spetertree 2088132730Skangetdecls (void) 208918334Speter{ 2090132730Skan return current_scope->names; 209118334Speter} 209218334Speter 209318334Speter 209418334Speter/* Given NAME, an IDENTIFIER_NODE, 209518334Speter return the structure (or union or enum) definition for that name. 2096132730Skan If THISLEVEL_ONLY is nonzero, searches only the current_scope. 209718334Speter CODE says which kind of type the caller wants; 209818334Speter it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. 209918334Speter If the wrong kind of type is found, an error is reported. */ 210018334Speter 210118334Speterstatic tree 2102132730Skanlookup_tag (enum tree_code code, tree name, int thislevel_only) 210318334Speter{ 2104132730Skan tree tag = IDENTIFIER_TAG_VALUE (name); 2105132730Skan int thislevel = 0; 210618334Speter 2107132730Skan if (!tag) 2108132730Skan return 0; 2109132730Skan 2110132730Skan /* We only care about whether it's in this level if 2111132730Skan thislevel_only was set or it might be a type clash. */ 2112132730Skan if (thislevel_only || TREE_CODE (tag) != code) 211318334Speter { 2114132730Skan if (current_scope == global_scope 2115132730Skan || purpose_member (name, current_scope->tags)) 2116132730Skan thislevel = 1; 211718334Speter } 2118132730Skan 2119132730Skan if (thislevel_only && !thislevel) 2120132730Skan return 0; 2121132730Skan 2122132730Skan if (TREE_CODE (tag) != code) 2123132730Skan { 2124132730Skan /* Definition isn't the kind we were looking for. */ 2125132730Skan pending_invalid_xref = name; 2126132730Skan pending_invalid_xref_location = input_location; 2127132730Skan 2128132730Skan /* If in the same binding level as a declaration as a tag 2129132730Skan of a different type, this must not be allowed to 2130132730Skan shadow that tag, so give the error immediately. 2131132730Skan (For example, "struct foo; union foo;" is invalid.) */ 2132132730Skan if (thislevel) 2133132730Skan pending_xref_error (); 2134132730Skan } 2135132730Skan return tag; 213618334Speter} 213718334Speter 213818334Speter/* Print an error message now 213918334Speter for a recent invalid struct, union or enum cross reference. 214018334Speter We don't print them immediately because they are not invalid 214118334Speter when used in the `struct foo;' construct for shadowing. */ 214218334Speter 214318334Spetervoid 2144132730Skanpending_xref_error (void) 214518334Speter{ 214618334Speter if (pending_invalid_xref != 0) 2147132730Skan error ("%H`%s' defined as wrong kind of tag", 2148132730Skan &pending_invalid_xref_location, 2149132730Skan IDENTIFIER_POINTER (pending_invalid_xref)); 215018334Speter pending_invalid_xref = 0; 215118334Speter} 215218334Speter 215318334Speter 2154132730Skan/* Look up NAME in the current scope and its superiors 215518334Speter in the namespace of variables, functions and typedefs. 215618334Speter Return a ..._DECL node of some kind representing its definition, 215718334Speter or return 0 if it is undefined. */ 215818334Speter 215918334Spetertree 2160132730Skanlookup_name (tree name) 216118334Speter{ 2162132730Skan tree decl = IDENTIFIER_SYMBOL_VALUE (name); 2163132730Skan if (decl == 0 || decl == error_mark_node) 2164132730Skan return decl; 2165132730Skan if (C_DECL_INVISIBLE (decl)) 2166132730Skan return 0; 2167132730Skan return decl; 216818334Speter} 216918334Speter 2170132730Skan/* Similar to `lookup_name' but look only at the current scope. */ 217118334Speter 2172132730Skanstatic tree 2173132730Skanlookup_name_current_level (tree name) 217418334Speter{ 2175132730Skan tree decl = IDENTIFIER_SYMBOL_VALUE (name); 217618334Speter 2177132730Skan if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl)) 217818334Speter return 0; 217918334Speter 2180132730Skan if (current_scope == global_scope) 2181132730Skan return decl; 218218334Speter 2183132730Skan /* Scan the current scope for a decl with name NAME. 2184132730Skan For PARM_DECLs, we have to look at both ->parms and ->names, since 2185132730Skan forward parameter declarations wind up on the ->names list. */ 2186132730Skan if (TREE_CODE (decl) == PARM_DECL 2187132730Skan && chain_member (decl, current_scope->parms)) 2188132730Skan return decl; 2189132730Skan if (chain_member (decl, current_scope->names)) 2190132730Skan return decl; 2191132730Skan 2192132730Skan return 0; 219318334Speter} 219418334Speter 219518334Speter/* Create the predefined scalar types of C, 219650397Sobrien and some nodes representing standard constants (0, 1, (void *) 0). 2197132730Skan Initialize the global scope. 219818334Speter Make definitions for built-in primitive functions. */ 219918334Speter 220018334Spetervoid 2201132730Skanc_init_decl_processing (void) 220218334Speter{ 220390075Sobrien tree endlink; 220490075Sobrien tree ptr_ftype_void, ptr_ftype_ptr; 2205132730Skan location_t save_loc = input_location; 220618334Speter 220790075Sobrien /* Adds some ggc roots, and reserved words for c-parse.in. */ 220890075Sobrien c_parse_init (); 220990075Sobrien 2210132730Skan current_function_decl = 0; 221190075Sobrien 2212132730Skan /* Make the c_scope structure for global names. */ 221390075Sobrien pushlevel (0); 2214132730Skan global_scope = current_scope; 221518334Speter 2216132730Skan /* Declarations from c_common_nodes_and_builtins must not be associated 2217132730Skan with this input file, lest we get differences between using and not 2218132730Skan using preprocessed headers. */ 2219132730Skan input_location.file = "<internal>"; 2220132730Skan input_location.line = 0; 2221132730Skan 2222132730Skan /* Make the DECL for the toplevel file scope. */ 2223132730Skan current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL); 2224132730Skan 222590075Sobrien build_common_tree_nodes (flag_signed_char); 222618334Speter 222790075Sobrien c_common_nodes_and_builtins (); 222818334Speter 2229132730Skan /* In C, comparisons and TRUTH_* expressions have type int. */ 2230132730Skan truthvalue_type_node = integer_type_node; 2231132730Skan truthvalue_true_node = integer_one_node; 2232132730Skan truthvalue_false_node = integer_zero_node; 223318334Speter 2234132730Skan /* Even in C99, which has a real boolean type. */ 223590075Sobrien pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"), 2236132730Skan boolean_type_node)); 223718334Speter 223890075Sobrien endlink = void_list_node; 223950397Sobrien ptr_ftype_void = build_function_type (ptr_type_node, endlink); 224050397Sobrien ptr_ftype_ptr 224150397Sobrien = build_function_type (ptr_type_node, 224250397Sobrien tree_cons (NULL_TREE, ptr_type_node, endlink)); 224350397Sobrien 2244132730Skan input_location = save_loc; 2245132730Skan 224690075Sobrien pedantic_lvalues = pedantic; 224718334Speter 224890075Sobrien make_fname_decl = c_make_fname_decl; 224990075Sobrien start_fname_decls (); 2250132730Skan 2251132730Skan first_builtin_decl = global_scope->names; 2252132730Skan last_builtin_decl = global_scope->names_last; 225390075Sobrien} 225418334Speter 225590075Sobrien/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the 225690075Sobrien decl, NAME is the initialization string and TYPE_DEP indicates whether 225790075Sobrien NAME depended on the type of the function. As we don't yet implement 225890075Sobrien delayed emission of static data, we mark the decl as emitted 225990075Sobrien so it is not placed in the output. Anything using it must therefore pull 2260132730Skan out the STRING_CST initializer directly. FIXME. */ 226118334Speter 226290075Sobrienstatic tree 2263132730Skanc_make_fname_decl (tree id, int type_dep) 226490075Sobrien{ 226590075Sobrien const char *name = fname_as_string (type_dep); 226690075Sobrien tree decl, type, init; 226790075Sobrien size_t length = strlen (name); 226818334Speter 226990075Sobrien type = build_array_type 227090075Sobrien (build_qualified_type (char_type_node, TYPE_QUAL_CONST), 227190075Sobrien build_index_type (size_int (length))); 227218334Speter 227390075Sobrien decl = build_decl (VAR_DECL, id, type); 2274132730Skan 227590075Sobrien TREE_STATIC (decl) = 1; 227690075Sobrien TREE_READONLY (decl) = 1; 227790075Sobrien DECL_ARTIFICIAL (decl) = 1; 2278132730Skan 227990075Sobrien init = build_string (length + 1, name); 228090075Sobrien TREE_TYPE (init) = type; 228190075Sobrien DECL_INITIAL (decl) = init; 228218334Speter 228390075Sobrien TREE_USED (decl) = 1; 2284132730Skan 2285132730Skan if (current_function_decl) 2286132730Skan { 2287132730Skan DECL_CONTEXT (decl) = current_function_decl; 2288132730Skan IDENTIFIER_SYMBOL_VALUE (id) = decl; 2289132730Skan SCOPE_LIST_APPEND (current_function_scope, names, decl); 2290132730Skan } 2291132730Skan 229290075Sobrien finish_decl (decl, init, NULL_TREE); 229318334Speter 229490075Sobrien return decl; 229518334Speter} 229618334Speter 229718334Speter/* Return a definition for a builtin function named NAME and whose data type 229818334Speter is TYPE. TYPE should be a function type with argument types. 229918334Speter FUNCTION_CODE tells later passes how to compile calls to this function. 230018334Speter See tree.h for its possible values. 230118334Speter 230218334Speter If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME, 2303117421Skan the name to be called if we can't opencode the function. If 2304117421Skan ATTRS is nonzero, use that for the function's attribute list. */ 230518334Speter 230618334Spetertree 2307132730Skanbuiltin_function (const char *name, tree type, int function_code, 2308132730Skan enum built_in_class class, const char *library_name, 2309132730Skan tree attrs) 231018334Speter{ 231118334Speter tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type); 231218334Speter DECL_EXTERNAL (decl) = 1; 231318334Speter TREE_PUBLIC (decl) = 1; 231418334Speter if (library_name) 231590075Sobrien SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name)); 231690075Sobrien make_decl_rtl (decl, NULL); 231718334Speter pushdecl (decl); 231890075Sobrien DECL_BUILT_IN_CLASS (decl) = class; 231990075Sobrien DECL_FUNCTION_CODE (decl) = function_code; 232090075Sobrien 232118334Speter /* Warn if a function in the namespace for users 232218334Speter is used without an occasion to consider it declared. */ 232318334Speter if (name[0] != '_' || name[1] != '_') 2324132730Skan C_DECL_INVISIBLE (decl) = 1; 232518334Speter 232690075Sobrien /* Possibly apply some default attributes to this built-in function. */ 2327117421Skan if (attrs) 2328117421Skan decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN); 2329117421Skan else 2330117421Skan decl_attributes (&decl, NULL_TREE, 0); 233190075Sobrien 233218334Speter return decl; 233318334Speter} 233418334Speter 233518334Speter/* Called when a declaration is seen that contains no names to declare. 233618334Speter If its type is a reference to a structure, union or enum inherited 233718334Speter from a containing scope, shadow that tag name for the current scope 233818334Speter with a forward reference. 233918334Speter If its type defines a new named structure or union 234018334Speter or defines an enum, it is valid but we need not do anything here. 234118334Speter Otherwise, it is an error. */ 234218334Speter 234318334Spetervoid 2344132730Skanshadow_tag (tree declspecs) 234518334Speter{ 234618334Speter shadow_tag_warned (declspecs, 0); 234718334Speter} 234818334Speter 234918334Spetervoid 2350132730Skanshadow_tag_warned (tree declspecs, int warned) 2351132730Skan 2352132730Skan 235318334Speter /* 1 => we have done a pedwarn. 2 => we have done a warning, but 235418334Speter no pedwarn. */ 235518334Speter{ 235618334Speter int found_tag = 0; 235790075Sobrien tree link; 235850397Sobrien tree specs, attrs; 235918334Speter 236018334Speter pending_invalid_xref = 0; 236118334Speter 236250397Sobrien /* Remove the attributes from declspecs, since they will confuse the 236350397Sobrien following code. */ 236450397Sobrien split_specs_attrs (declspecs, &specs, &attrs); 236550397Sobrien 236650397Sobrien for (link = specs; link; link = TREE_CHAIN (link)) 236718334Speter { 236890075Sobrien tree value = TREE_VALUE (link); 236990075Sobrien enum tree_code code = TREE_CODE (value); 237018334Speter 237118334Speter if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE) 237218334Speter /* Used to test also that TYPE_SIZE (value) != 0. 237318334Speter That caused warning for `struct foo;' at top level in the file. */ 237418334Speter { 2375132730Skan tree name = TYPE_NAME (value); 237690075Sobrien tree t; 237718334Speter 237818334Speter found_tag++; 237918334Speter 238018334Speter if (name == 0) 238118334Speter { 238218334Speter if (warned != 1 && code != ENUMERAL_TYPE) 238318334Speter /* Empty unnamed enum OK */ 238418334Speter { 238518334Speter pedwarn ("unnamed struct/union that defines no instances"); 238618334Speter warned = 1; 238718334Speter } 238818334Speter } 238918334Speter else 239018334Speter { 2391132730Skan t = lookup_tag (code, name, 1); 239218334Speter 239318334Speter if (t == 0) 239418334Speter { 239518334Speter t = make_node (code); 239618334Speter pushtag (name, t); 239718334Speter } 239818334Speter } 239918334Speter } 240018334Speter else 240118334Speter { 240218334Speter if (!warned && ! in_system_header) 240318334Speter { 240418334Speter warning ("useless keyword or type name in empty declaration"); 240518334Speter warned = 2; 240618334Speter } 240718334Speter } 240818334Speter } 240918334Speter 241018334Speter if (found_tag > 1) 241118334Speter error ("two types specified in one empty declaration"); 241218334Speter 241318334Speter if (warned != 1) 241418334Speter { 241518334Speter if (found_tag == 0) 241618334Speter pedwarn ("empty declaration"); 241718334Speter } 241818334Speter} 241918334Speter 242090075Sobrien/* Construct an array declarator. EXPR is the expression inside [], or 242190075Sobrien NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied 242290075Sobrien to the pointer to which a parameter array is converted). STATIC_P is 2423117421Skan nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P 2424117421Skan is nonzero is the array is [*], a VLA of unspecified length which is 242590075Sobrien nevertheless a complete type (not currently implemented by GCC), 242690075Sobrien zero otherwise. The declarator is constructed as an ARRAY_REF 242790075Sobrien (to be decoded by grokdeclarator), whose operand 0 is what's on the 242890075Sobrien left of the [] (filled by in set_array_declarator_type) and operand 1 242990075Sobrien is the expression inside; whose TREE_TYPE is the type qualifiers and 243090075Sobrien which has TREE_STATIC set if "static" is used. */ 243190075Sobrien 243290075Sobrientree 2433132730Skanbuild_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p) 243490075Sobrien{ 243590075Sobrien tree decl; 243690075Sobrien decl = build_nt (ARRAY_REF, NULL_TREE, expr); 243790075Sobrien TREE_TYPE (decl) = quals; 243890075Sobrien TREE_STATIC (decl) = (static_p ? 1 : 0); 243990075Sobrien if (pedantic && !flag_isoc99) 244090075Sobrien { 244190075Sobrien if (static_p || quals != NULL_TREE) 2442117421Skan pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators"); 244390075Sobrien if (vla_unspec_p) 2444117421Skan pedwarn ("ISO C90 does not support `[*]' array declarators"); 244590075Sobrien } 244690075Sobrien if (vla_unspec_p) 244790075Sobrien warning ("GCC does not yet properly implement `[*]' array declarators"); 244890075Sobrien return decl; 244990075Sobrien} 245090075Sobrien 245190075Sobrien/* Set the type of an array declarator. DECL is the declarator, as 245290075Sobrien constructed by build_array_declarator; TYPE is what appears on the left 2453117421Skan of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an 245490075Sobrien abstract declarator, zero otherwise; this is used to reject static and 245590075Sobrien type qualifiers in abstract declarators, where they are not in the 245690075Sobrien C99 grammar. */ 245790075Sobrien 245890075Sobrientree 2459132730Skanset_array_declarator_type (tree decl, tree type, int abstract_p) 246090075Sobrien{ 246190075Sobrien TREE_OPERAND (decl, 0) = type; 246290075Sobrien if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl))) 246390075Sobrien error ("static or type qualifiers in abstract declarator"); 246490075Sobrien return decl; 246590075Sobrien} 246690075Sobrien 246718334Speter/* Decode a "typename", such as "int **", returning a ..._TYPE node. */ 246818334Speter 246918334Spetertree 2470132730Skangroktypename (tree typename) 247118334Speter{ 247290075Sobrien tree specs, attrs; 247390075Sobrien 247418334Speter if (TREE_CODE (typename) != TREE_LIST) 247518334Speter return typename; 247690075Sobrien 247790075Sobrien split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs); 247890075Sobrien 2479132730Skan typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0, 2480132730Skan NULL); 248190075Sobrien 248290075Sobrien /* Apply attributes. */ 248390075Sobrien decl_attributes (&typename, attrs, 0); 248490075Sobrien 248590075Sobrien return typename; 248618334Speter} 248718334Speter 248818334Speter/* Return a PARM_DECL node for a given pair of specs and declarator. */ 248918334Speter 249018334Spetertree 2491132730Skangroktypename_in_parm_context (tree typename) 249218334Speter{ 249318334Speter if (TREE_CODE (typename) != TREE_LIST) 249418334Speter return typename; 249518334Speter return grokdeclarator (TREE_VALUE (typename), 249618334Speter TREE_PURPOSE (typename), 2497132730Skan PARM, 0, NULL); 249818334Speter} 249918334Speter 250018334Speter/* Decode a declarator in an ordinary declaration or data definition. 250118334Speter This is called as soon as the type information and variable name 250218334Speter have been parsed, before parsing the initializer if any. 250318334Speter Here we create the ..._DECL node, fill in its type, 250418334Speter and put it on the list of decls for the current context. 250518334Speter The ..._DECL node is returned as the value. 250618334Speter 250718334Speter Exception: for arrays where the length is not specified, 250818334Speter the type is left null, to be filled in by `finish_decl'. 250918334Speter 251018334Speter Function definitions do not come here; they go to start_function 251118334Speter instead. However, external and forward declarations of functions 251218334Speter do go through here. Structure field declarations are done by 251318334Speter grokfield and not through here. */ 251418334Speter 251518334Spetertree 2516132730Skanstart_decl (tree declarator, tree declspecs, int initialized, tree attributes) 251718334Speter{ 251890075Sobrien tree decl; 251990075Sobrien tree tem; 2520132730Skan 252190075Sobrien /* An object declared as __attribute__((deprecated)) suppresses 252290075Sobrien warnings of uses of other deprecated items. */ 252390075Sobrien if (lookup_attribute ("deprecated", attributes)) 252490075Sobrien deprecated_state = DEPRECATED_SUPPRESS; 252518334Speter 252690075Sobrien decl = grokdeclarator (declarator, declspecs, 2527132730Skan NORMAL, initialized, NULL); 2528132730Skan 252990075Sobrien deprecated_state = DEPRECATED_NORMAL; 253018334Speter 253190075Sobrien if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL 253290075Sobrien && MAIN_NAME_P (DECL_NAME (decl))) 2533132730Skan warning ("%J'%D' is usually a function", decl, decl); 253450397Sobrien 253518334Speter if (initialized) 253618334Speter /* Is it valid for this decl to have an initializer at all? 253718334Speter If not, set INITIALIZED to zero, which will indirectly 253818334Speter tell `finish_decl' to ignore the initializer once it is parsed. */ 253918334Speter switch (TREE_CODE (decl)) 254018334Speter { 254118334Speter case TYPE_DECL: 2542107593Sobrien error ("typedef `%s' is initialized (use __typeof__ instead)", 2543104761Skan IDENTIFIER_POINTER (DECL_NAME (decl))); 2544104761Skan initialized = 0; 254518334Speter break; 254618334Speter 254718334Speter case FUNCTION_DECL: 254818334Speter error ("function `%s' is initialized like a variable", 254918334Speter IDENTIFIER_POINTER (DECL_NAME (decl))); 255018334Speter initialized = 0; 255118334Speter break; 255218334Speter 255318334Speter case PARM_DECL: 255418334Speter /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */ 255518334Speter error ("parameter `%s' is initialized", 255618334Speter IDENTIFIER_POINTER (DECL_NAME (decl))); 255718334Speter initialized = 0; 255818334Speter break; 255918334Speter 256018334Speter default: 256118334Speter /* Don't allow initializations for incomplete types 256218334Speter except for arrays which might be completed by the initialization. */ 256390075Sobrien 256490075Sobrien /* This can happen if the array size is an undefined macro. We already 256590075Sobrien gave a warning, so we don't need another one. */ 256690075Sobrien if (TREE_TYPE (decl) == error_mark_node) 256790075Sobrien initialized = 0; 256890075Sobrien else if (COMPLETE_TYPE_P (TREE_TYPE (decl))) 256918334Speter { 257018334Speter /* A complete type is ok if size is fixed. */ 257118334Speter 257218334Speter if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST 257318334Speter || C_DECL_VARIABLE_SIZE (decl)) 257418334Speter { 257518334Speter error ("variable-sized object may not be initialized"); 257618334Speter initialized = 0; 257718334Speter } 257818334Speter } 257918334Speter else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE) 258018334Speter { 258118334Speter error ("variable `%s' has initializer but incomplete type", 258218334Speter IDENTIFIER_POINTER (DECL_NAME (decl))); 258318334Speter initialized = 0; 258418334Speter } 258590075Sobrien else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) 258618334Speter { 258718334Speter error ("elements of array `%s' have incomplete type", 258818334Speter IDENTIFIER_POINTER (DECL_NAME (decl))); 258918334Speter initialized = 0; 259018334Speter } 259118334Speter } 259218334Speter 259318334Speter if (initialized) 259418334Speter { 259518334Speter DECL_EXTERNAL (decl) = 0; 2596132730Skan if (current_scope == global_scope) 259718334Speter TREE_STATIC (decl) = 1; 259818334Speter 259918334Speter /* Tell `pushdecl' this is an initialized decl 260018334Speter even though we don't yet have the initializer expression. 260118334Speter Also tell `finish_decl' it may store the real initializer. */ 260218334Speter DECL_INITIAL (decl) = error_mark_node; 260318334Speter } 260418334Speter 260518334Speter /* If this is a function declaration, write a record describing it to the 260618334Speter prototypes file (if requested). */ 260718334Speter 260818334Speter if (TREE_CODE (decl) == FUNCTION_DECL) 260918334Speter gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0); 261018334Speter 261150397Sobrien /* ANSI specifies that a tentative definition which is not merged with 261250397Sobrien a non-tentative definition behaves exactly like a definition with an 261350397Sobrien initializer equal to zero. (Section 3.7.2) 2614117421Skan 2615117421Skan -fno-common gives strict ANSI behavior, though this tends to break 2616117421Skan a large body of code that grew up without this rule. 2617117421Skan 2618117421Skan Thread-local variables are never common, since there's no entrenched 2619117421Skan body of code to break, and it allows more efficient variable references 2620132730Skan in the presence of dynamic linking. */ 2621117421Skan 2622117421Skan if (TREE_CODE (decl) == VAR_DECL 2623117421Skan && !initialized 2624117421Skan && TREE_PUBLIC (decl) 2625117421Skan && !DECL_THREAD_LOCAL (decl) 2626117421Skan && !flag_no_common) 262750397Sobrien DECL_COMMON (decl) = 1; 262818334Speter 262918334Speter /* Set attributes here so if duplicate decl, will have proper attributes. */ 263090075Sobrien decl_attributes (&decl, attributes, 0); 263118334Speter 2632132730Skan if (TREE_CODE (decl) == FUNCTION_DECL 2633132730Skan && targetm.calls.promote_prototypes (TREE_TYPE (decl))) 2634132730Skan { 2635132730Skan tree ce = declarator; 263696263Sobrien 2637132730Skan if (TREE_CODE (ce) == INDIRECT_REF) 2638132730Skan ce = TREE_OPERAND (declarator, 0); 2639132730Skan if (TREE_CODE (ce) == CALL_EXPR) 2640132730Skan { 2641132730Skan tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1)); 2642132730Skan for (; args; args = TREE_CHAIN (args)) 2643132730Skan { 2644132730Skan tree type = TREE_TYPE (args); 2645132730Skan if (INTEGRAL_TYPE_P (type) 2646132730Skan && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 2647132730Skan DECL_ARG_TYPE (args) = integer_type_node; 2648132730Skan } 2649132730Skan } 2650132730Skan } 2651132730Skan 265290075Sobrien if (TREE_CODE (decl) == FUNCTION_DECL 265390075Sobrien && DECL_DECLARED_INLINE_P (decl) 265490075Sobrien && DECL_UNINLINABLE (decl) 265590075Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) 2656132730Skan warning ("%Jinline function '%D' given attribute noinline", decl, decl); 265790075Sobrien 2658132730Skan /* Add this decl to the current scope. 265918334Speter TEM may equal DECL or it may be a previous decl of the same name. */ 266018334Speter tem = pushdecl (decl); 266118334Speter 266218334Speter /* For a local variable, define the RTL now. */ 2663132730Skan if (current_scope != global_scope 266418334Speter /* But not if this is a duplicate decl 266518334Speter and we preserved the rtl from the previous one 266618334Speter (which may or may not happen). */ 266790075Sobrien && !DECL_RTL_SET_P (tem) 2668132730Skan && DECL_FILE_SCOPE_P (tem)) 266918334Speter { 267090075Sobrien if (TREE_TYPE (tem) != error_mark_node 2671132730Skan && (COMPLETE_TYPE_P (TREE_TYPE (tem)) 2672132730Skan || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE 2673132730Skan && DECL_INITIAL (tem) != 0))) 267418334Speter expand_decl (tem); 267518334Speter } 267618334Speter 267718334Speter return tem; 267818334Speter} 267918334Speter 268018334Speter/* Finish processing of a declaration; 268118334Speter install its initial value. 268218334Speter If the length of an array type is not known before, 268318334Speter it must be determined now, from the initial value, or it is an error. */ 268418334Speter 268518334Spetervoid 2686132730Skanfinish_decl (tree decl, tree init, tree asmspec_tree) 268718334Speter{ 268890075Sobrien tree type = TREE_TYPE (decl); 268918334Speter int was_incomplete = (DECL_SIZE (decl) == 0); 269090075Sobrien const char *asmspec = 0; 269118334Speter 269290075Sobrien /* If a name was specified, get the string. */ 2693132730Skan if (current_scope == global_scope) 269496263Sobrien asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); 269518334Speter if (asmspec_tree) 269618334Speter asmspec = TREE_STRING_POINTER (asmspec_tree); 269718334Speter 269818334Speter /* If `start_decl' didn't like having an initialization, ignore it now. */ 269918334Speter if (init != 0 && DECL_INITIAL (decl) == 0) 270018334Speter init = 0; 2701132730Skan 270218334Speter /* Don't crash if parm is initialized. */ 270318334Speter if (TREE_CODE (decl) == PARM_DECL) 270418334Speter init = 0; 270518334Speter 270618334Speter if (init) 2707104761Skan store_init_value (decl, init); 270818334Speter 2709132730Skan if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL 2710132730Skan || TREE_CODE (decl) == FUNCTION_DECL 2711132730Skan || TREE_CODE (decl) == FIELD_DECL)) 2712132730Skan objc_check_decl (decl); 2713132730Skan 2714132730Skan /* Deduce size of array from initialization, if not already known. */ 271518334Speter if (TREE_CODE (type) == ARRAY_TYPE 271618334Speter && TYPE_DOMAIN (type) == 0 271718334Speter && TREE_CODE (decl) != TYPE_DECL) 271818334Speter { 271918334Speter int do_default 272018334Speter = (TREE_STATIC (decl) 272118334Speter /* Even if pedantic, an external linkage array 272218334Speter may have incomplete type at first. */ 272318334Speter ? pedantic && !TREE_PUBLIC (decl) 272418334Speter : !DECL_EXTERNAL (decl)); 272518334Speter int failure 272618334Speter = complete_array_type (type, DECL_INITIAL (decl), do_default); 272718334Speter 272818334Speter /* Get the completed type made by complete_array_type. */ 272918334Speter type = TREE_TYPE (decl); 273018334Speter 273118334Speter if (failure == 1) 2732132730Skan error ("%Jinitializer fails to determine size of '%D'", decl, decl); 273318334Speter 273490075Sobrien else if (failure == 2) 273518334Speter { 273618334Speter if (do_default) 2737132730Skan error ("%Jarray size missing in '%D'", decl, decl); 273818334Speter /* If a `static' var's size isn't known, 273918334Speter make it extern as well as static, so it does not get 274018334Speter allocated. 274118334Speter If it is not `static', then do not mark extern; 274218334Speter finish_incomplete_decl will give it a default size 274318334Speter and it will get allocated. */ 274418334Speter else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl)) 274518334Speter DECL_EXTERNAL (decl) = 1; 274618334Speter } 274718334Speter 274818334Speter /* TYPE_MAX_VALUE is always one less than the number of elements 274918334Speter in the array, because we start counting at zero. Therefore, 275018334Speter warn only if the value is less than zero. */ 275190075Sobrien else if (pedantic && TYPE_DOMAIN (type) != 0 275290075Sobrien && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0) 2753132730Skan error ("%Jzero or negative size array '%D'", decl, decl); 275418334Speter 275518334Speter layout_decl (decl, 0); 275618334Speter } 275718334Speter 275818334Speter if (TREE_CODE (decl) == VAR_DECL) 275918334Speter { 276090075Sobrien if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node 276190075Sobrien && COMPLETE_TYPE_P (TREE_TYPE (decl))) 276218334Speter layout_decl (decl, 0); 276318334Speter 276418334Speter if (DECL_SIZE (decl) == 0 276590075Sobrien /* Don't give an error if we already gave one earlier. */ 276690075Sobrien && TREE_TYPE (decl) != error_mark_node 276718334Speter && (TREE_STATIC (decl) 276818334Speter ? 276918334Speter /* A static variable with an incomplete type 277018334Speter is an error if it is initialized. 277118334Speter Also if it is not file scope. 277218334Speter Otherwise, let it through, but if it is not `extern' 277318334Speter then it may cause an error message later. */ 277450397Sobrien (DECL_INITIAL (decl) != 0 2775132730Skan || !DECL_FILE_SCOPE_P (decl)) 277618334Speter : 277718334Speter /* An automatic variable with an incomplete type 277818334Speter is an error. */ 277918334Speter !DECL_EXTERNAL (decl))) 278018334Speter { 2781132730Skan error ("%Jstorage size of '%D' isn't known", decl, decl); 278218334Speter TREE_TYPE (decl) = error_mark_node; 278318334Speter } 278418334Speter 278518334Speter if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 278618334Speter && DECL_SIZE (decl) != 0) 278718334Speter { 278818334Speter if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) 278918334Speter constant_expression_warning (DECL_SIZE (decl)); 279018334Speter else 2791132730Skan error ("%Jstorage size of '%D' isn't constant", decl, decl); 279218334Speter } 279350397Sobrien 279490075Sobrien if (TREE_USED (type)) 279550397Sobrien TREE_USED (decl) = 1; 279618334Speter } 279718334Speter 2798132730Skan /* If this is a function and an assembler name is specified, reset DECL_RTL 2799132730Skan so we can give it its new name. Also, update built_in_decls if it 2800132730Skan was a normal built-in. */ 280118334Speter if (TREE_CODE (decl) == FUNCTION_DECL && asmspec) 280290075Sobrien { 2803132730Skan /* ASMSPEC is given, and not the name of a register. Mark the 2804132730Skan name with a star so assemble_name won't munge it. */ 2805132730Skan char *starred = alloca (strlen (asmspec) + 2); 2806132730Skan starred[0] = '*'; 2807132730Skan strcpy (starred + 1, asmspec); 2808132730Skan 2809132730Skan if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) 2810132730Skan { 2811132730Skan tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)]; 2812132730Skan SET_DECL_RTL (builtin, NULL_RTX); 2813132730Skan SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred)); 2814132730Skan#ifdef TARGET_MEM_FUNCTIONS 2815132730Skan if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY) 2816132730Skan init_block_move_fn (starred); 2817132730Skan else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET) 2818132730Skan init_block_clear_fn (starred); 2819132730Skan#else 2820132730Skan if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY) 2821132730Skan init_block_move_fn (starred); 2822132730Skan else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO) 2823132730Skan init_block_clear_fn (starred); 2824132730Skan#endif 2825132730Skan } 282690075Sobrien SET_DECL_RTL (decl, NULL_RTX); 2827132730Skan change_decl_assembler_name (decl, get_identifier (starred)); 282890075Sobrien } 282918334Speter 2830132730Skan /* If #pragma weak was used, mark the decl weak now. */ 2831132730Skan if (current_scope == global_scope) 2832132730Skan maybe_apply_pragma_weak (decl); 2833132730Skan 283418334Speter /* Output the assembler code and/or RTL code for variables and functions, 283518334Speter unless the type is an undefined structure or union. 283618334Speter If not, it will get done when the type is completed. */ 283718334Speter 283818334Speter if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL) 283918334Speter { 2840117421Skan /* This is a no-op in c-lang.c or something real in objc-act.c. */ 2841132730Skan if (c_dialect_objc ()) 2842117421Skan objc_check_decl (decl); 284390075Sobrien 2844132730Skan if (DECL_FILE_SCOPE_P (decl)) 284518334Speter { 284690075Sobrien if (DECL_INITIAL (decl) == NULL_TREE 284790075Sobrien || DECL_INITIAL (decl) == error_mark_node) 284890075Sobrien /* Don't output anything 284990075Sobrien when a tentative file-scope definition is seen. 285090075Sobrien But at end of compilation, do output code for them. */ 285190075Sobrien DECL_DEFER_OUTPUT (decl) = 1; 2852132730Skan rest_of_decl_compilation (decl, asmspec, true, 0); 285318334Speter } 285418334Speter else 285518334Speter { 285690075Sobrien /* This is a local variable. If there is an ASMSPEC, the 285790075Sobrien user has requested that we handle it specially. */ 285890075Sobrien if (asmspec) 285990075Sobrien { 286090075Sobrien /* In conjunction with an ASMSPEC, the `register' 286190075Sobrien keyword indicates that we should place the variable 286290075Sobrien in a particular register. */ 286390075Sobrien if (DECL_REGISTER (decl)) 286490075Sobrien DECL_C_HARD_REGISTER (decl) = 1; 286590075Sobrien 286690075Sobrien /* If this is not a static variable, issue a warning. 286790075Sobrien It doesn't make any sense to give an ASMSPEC for an 286890075Sobrien ordinary, non-register local variable. Historically, 286990075Sobrien GCC has accepted -- but ignored -- the ASMSPEC in 287090075Sobrien this case. */ 2871132730Skan if (TREE_CODE (decl) == VAR_DECL 287290075Sobrien && !DECL_REGISTER (decl) 287390075Sobrien && !TREE_STATIC (decl)) 2874132730Skan warning ("%Jignoring asm-specifier for non-static local " 2875132730Skan "variable '%D'", decl, decl); 287690075Sobrien else 2877132730Skan change_decl_assembler_name (decl, get_identifier (asmspec)); 287890075Sobrien } 287990075Sobrien 288090075Sobrien if (TREE_CODE (decl) != FUNCTION_DECL) 288190075Sobrien add_decl_stmt (decl); 288218334Speter } 288390075Sobrien 2884132730Skan if (!DECL_FILE_SCOPE_P (decl)) 288518334Speter { 288618334Speter /* Recompute the RTL of a local array now 288718334Speter if it used to be an incomplete type. */ 288818334Speter if (was_incomplete 288918334Speter && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl)) 289018334Speter { 289118334Speter /* If we used it already as memory, it must stay in memory. */ 289218334Speter TREE_ADDRESSABLE (decl) = TREE_USED (decl); 289318334Speter /* If it's still incomplete now, no init will save it. */ 289418334Speter if (DECL_SIZE (decl) == 0) 289518334Speter DECL_INITIAL (decl) = 0; 289618334Speter } 289718334Speter } 289818334Speter } 289918334Speter 2900132730Skan /* If this was marked 'used', be sure it will be output. */ 2901132730Skan if (lookup_attribute ("used", DECL_ATTRIBUTES (decl))) 2902132730Skan mark_referenced (DECL_ASSEMBLER_NAME (decl)); 2903132730Skan 290418334Speter if (TREE_CODE (decl) == TYPE_DECL) 2905132730Skan rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0); 290618334Speter 290718334Speter /* At the end of a declaration, throw away any variable type sizes 290818334Speter of types defined inside that declaration. There is no use 290918334Speter computing them in the following function definition. */ 2910132730Skan if (current_scope == global_scope) 291118334Speter get_pending_sizes (); 291218334Speter 2913117421Skan /* Install a cleanup (aka destructor) if one was given. */ 2914117421Skan if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl)) 2915117421Skan { 2916117421Skan tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl)); 2917117421Skan if (attr) 2918117421Skan { 2919117421Skan static bool eh_initialized_p; 292018334Speter 2921117421Skan tree cleanup_id = TREE_VALUE (TREE_VALUE (attr)); 2922117421Skan tree cleanup_decl = lookup_name (cleanup_id); 2923117421Skan tree cleanup; 2924117421Skan 2925117421Skan /* Build "cleanup(&decl)" for the destructor. */ 2926117421Skan cleanup = build_unary_op (ADDR_EXPR, decl, 0); 2927117421Skan cleanup = build_tree_list (NULL_TREE, cleanup); 2928117421Skan cleanup = build_function_call (cleanup_decl, cleanup); 2929117421Skan 2930117421Skan /* Don't warn about decl unused; the cleanup uses it. */ 2931117421Skan TREE_USED (decl) = 1; 2932117421Skan 2933117421Skan /* Initialize EH, if we've been told to do so. */ 2934117421Skan if (flag_exceptions && !eh_initialized_p) 2935117421Skan { 2936117421Skan eh_initialized_p = true; 2937117421Skan eh_personality_libfunc 2938117421Skan = init_one_libfunc (USING_SJLJ_EXCEPTIONS 2939117421Skan ? "__gcc_personality_sj0" 2940117421Skan : "__gcc_personality_v0"); 2941117421Skan using_eh_for_cleanups (); 2942117421Skan } 2943117421Skan 2944117421Skan add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup)); 2945117421Skan } 2946117421Skan } 294718334Speter} 294818334Speter 2949132730Skan/* Given a parsed parameter declaration, decode it into a PARM_DECL 2950132730Skan and push that on the current scope. */ 295118334Speter 295218334Spetervoid 2953132730Skanpush_parm_decl (tree parm) 295418334Speter{ 295518334Speter tree decl; 2956132730Skan 2957132730Skan /* Don't attempt to expand sizes while parsing this decl. 2958132730Skan (We can get here with i_s_e 1 somehow from Objective-C.) */ 2959132730Skan int save_immediate_size_expand = immediate_size_expand; 296018334Speter immediate_size_expand = 0; 296118334Speter 296218334Speter decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)), 2963132730Skan TREE_PURPOSE (TREE_PURPOSE (parm)), 2964132730Skan PARM, 0, NULL); 296590075Sobrien decl_attributes (&decl, TREE_VALUE (parm), 0); 296618334Speter 296718334Speter decl = pushdecl (decl); 296818334Speter 2969132730Skan finish_decl (decl, NULL_TREE, NULL_TREE); 297018334Speter 2971132730Skan immediate_size_expand = save_immediate_size_expand; 297218334Speter} 297318334Speter 2974132730Skan/* Mark all the parameter declarations to date as forward decls, 2975132730Skan shift them to the variables list, and reset the parameters list. 2976132730Skan Also diagnose use of this extension. */ 297718334Speter 297818334Spetervoid 2979132730Skanmark_forward_parm_decls (void) 298018334Speter{ 2981132730Skan tree parm; 2982132730Skan 2983132730Skan if (pedantic && !current_scope->warned_forward_parm_decls) 2984132730Skan { 2985132730Skan pedwarn ("ISO C forbids forward parameter declarations"); 2986132730Skan current_scope->warned_forward_parm_decls = true; 2987132730Skan } 2988132730Skan 2989132730Skan for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm)) 2990132730Skan TREE_ASM_WRITTEN (parm) = 1; 2991132730Skan 2992132730Skan SCOPE_LIST_CONCAT (current_scope, names, current_scope, parms); 2993132730Skan current_scope->parms = 0; 2994132730Skan current_scope->parms_last = 0; 299518334Speter} 299618334Speter 2997132730Skanstatic GTY(()) int compound_literal_number; 2998132730Skan 299990075Sobrien/* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound 300090075Sobrien literal, which may be an incomplete array type completed by the 300190075Sobrien initializer; INIT is a CONSTRUCTOR that initializes the compound 300290075Sobrien literal. */ 300390075Sobrien 300490075Sobrientree 3005132730Skanbuild_compound_literal (tree type, tree init) 300690075Sobrien{ 300790075Sobrien /* We do not use start_decl here because we have a type, not a declarator; 300890075Sobrien and do not use finish_decl because the decl should be stored inside 300990075Sobrien the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */ 301090075Sobrien tree decl = build_decl (VAR_DECL, NULL_TREE, type); 301190075Sobrien tree complit; 301290075Sobrien tree stmt; 301390075Sobrien DECL_EXTERNAL (decl) = 0; 301490075Sobrien TREE_PUBLIC (decl) = 0; 3015132730Skan TREE_STATIC (decl) = (current_scope == global_scope); 301690075Sobrien DECL_CONTEXT (decl) = current_function_decl; 301790075Sobrien TREE_USED (decl) = 1; 301890075Sobrien TREE_TYPE (decl) = type; 3019102790Skan TREE_READONLY (decl) = TREE_READONLY (type); 302090075Sobrien store_init_value (decl, init); 302190075Sobrien 302290075Sobrien if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type)) 302390075Sobrien { 302490075Sobrien int failure = complete_array_type (type, DECL_INITIAL (decl), 1); 302590075Sobrien if (failure) 302690075Sobrien abort (); 302790075Sobrien } 302890075Sobrien 302990075Sobrien type = TREE_TYPE (decl); 303090075Sobrien if (type == error_mark_node || !COMPLETE_TYPE_P (type)) 303190075Sobrien return error_mark_node; 303290075Sobrien 303390075Sobrien stmt = build_stmt (DECL_STMT, decl); 303490075Sobrien complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt); 303590075Sobrien TREE_SIDE_EFFECTS (complit) = 1; 303690075Sobrien 303790075Sobrien layout_decl (decl, 0); 303890075Sobrien 303990075Sobrien if (TREE_STATIC (decl)) 304090075Sobrien { 304190075Sobrien /* This decl needs a name for the assembler output. We also need 3042102790Skan a unique suffix to be added to the name. */ 3043102790Skan char *name; 3044102790Skan 3045132730Skan ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal", 3046132730Skan compound_literal_number); 3047132730Skan compound_literal_number++; 3048102790Skan DECL_NAME (decl) = get_identifier (name); 3049102790Skan DECL_DEFER_OUTPUT (decl) = 1; 3050102790Skan DECL_COMDAT (decl) = 1; 3051102790Skan DECL_ARTIFICIAL (decl) = 1; 3052102790Skan pushdecl (decl); 305390075Sobrien rest_of_decl_compilation (decl, NULL, 1, 0); 305490075Sobrien } 305590075Sobrien 305690075Sobrien return complit; 305790075Sobrien} 305890075Sobrien 305918334Speter/* Make TYPE a complete type based on INITIAL_VALUE. 306018334Speter Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, 306118334Speter 2 if there was no information (in which case assume 1 if DO_DEFAULT). */ 306218334Speter 306318334Speterint 3064132730Skancomplete_array_type (tree type, tree initial_value, int do_default) 306518334Speter{ 306690075Sobrien tree maxindex = NULL_TREE; 306718334Speter int value = 0; 306818334Speter 306918334Speter if (initial_value) 307018334Speter { 307118334Speter /* Note MAXINDEX is really the maximum index, 307218334Speter one less than the size. */ 307318334Speter if (TREE_CODE (initial_value) == STRING_CST) 307418334Speter { 307518334Speter int eltsize 307618334Speter = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); 307718334Speter maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value) 307818334Speter / eltsize) - 1, 0); 307918334Speter } 308018334Speter else if (TREE_CODE (initial_value) == CONSTRUCTOR) 308118334Speter { 308218334Speter tree elts = CONSTRUCTOR_ELTS (initial_value); 308390075Sobrien maxindex = build_int_2 (-1, -1); 308418334Speter for (; elts; elts = TREE_CHAIN (elts)) 308518334Speter { 308618334Speter if (TREE_PURPOSE (elts)) 308718334Speter maxindex = TREE_PURPOSE (elts); 308818334Speter else 308990075Sobrien maxindex = fold (build (PLUS_EXPR, integer_type_node, 309090075Sobrien maxindex, integer_one_node)); 309118334Speter } 309218334Speter maxindex = copy_node (maxindex); 309318334Speter } 309418334Speter else 309518334Speter { 309618334Speter /* Make an error message unless that happened already. */ 309718334Speter if (initial_value != error_mark_node) 309818334Speter value = 1; 309918334Speter 310018334Speter /* Prevent further error messages. */ 310118334Speter maxindex = build_int_2 (0, 0); 310218334Speter } 310318334Speter } 310418334Speter 310518334Speter if (!maxindex) 310618334Speter { 310718334Speter if (do_default) 310818334Speter maxindex = build_int_2 (0, 0); 310918334Speter value = 2; 311018334Speter } 311118334Speter 311218334Speter if (maxindex) 311318334Speter { 311418334Speter TYPE_DOMAIN (type) = build_index_type (maxindex); 311518334Speter if (!TREE_TYPE (maxindex)) 311618334Speter TREE_TYPE (maxindex) = TYPE_DOMAIN (type); 311718334Speter } 311818334Speter 311918334Speter /* Lay out the type now that we can get the real answer. */ 312018334Speter 312118334Speter layout_type (type); 312218334Speter 312318334Speter return value; 312418334Speter} 312518334Speter 3126117421Skan/* Determine whether TYPE is a structure with a flexible array member, 3127117421Skan or a union containing such a structure (possibly recursively). */ 3128117421Skan 3129117421Skanstatic bool 3130132730Skanflexible_array_type_p (tree type) 3131117421Skan{ 3132117421Skan tree x; 3133117421Skan switch (TREE_CODE (type)) 3134117421Skan { 3135117421Skan case RECORD_TYPE: 3136117421Skan x = TYPE_FIELDS (type); 3137117421Skan if (x == NULL_TREE) 3138117421Skan return false; 3139117421Skan while (TREE_CHAIN (x) != NULL_TREE) 3140117421Skan x = TREE_CHAIN (x); 3141117421Skan if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 3142117421Skan && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 3143117421Skan && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 3144117421Skan && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 3145117421Skan return true; 3146117421Skan return false; 3147117421Skan case UNION_TYPE: 3148117421Skan for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x)) 3149117421Skan { 3150117421Skan if (flexible_array_type_p (TREE_TYPE (x))) 3151117421Skan return true; 3152117421Skan } 3153117421Skan return false; 3154117421Skan default: 3155117421Skan return false; 3156117421Skan } 3157117421Skan} 3158117421Skan 3159132730Skan/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME, 3160132730Skan replacing with appropriate values if they are invalid. */ 3161132730Skanstatic void 3162132730Skancheck_bitfield_type_and_width (tree *type, tree *width, const char *orig_name) 3163132730Skan{ 3164132730Skan tree type_mv; 3165132730Skan unsigned int max_width; 3166132730Skan unsigned HOST_WIDE_INT w; 3167132730Skan const char *name = orig_name ? orig_name: _("<anonymous>"); 3168132730Skan 3169132730Skan /* Necessary? */ 3170132730Skan STRIP_NOPS (*width); 3171132730Skan 3172132730Skan /* Detect and ignore out of range field width and process valid 3173132730Skan field widths. */ 3174132730Skan if (TREE_CODE (*width) != INTEGER_CST) 3175132730Skan { 3176132730Skan error ("bit-field `%s' width not an integer constant", name); 3177132730Skan *width = integer_one_node; 3178132730Skan } 3179132730Skan else 3180132730Skan { 3181132730Skan constant_expression_warning (*width); 3182132730Skan if (tree_int_cst_sgn (*width) < 0) 3183132730Skan { 3184132730Skan error ("negative width in bit-field `%s'", name); 3185132730Skan *width = integer_one_node; 3186132730Skan } 3187132730Skan else if (integer_zerop (*width) && orig_name) 3188132730Skan { 3189132730Skan error ("zero width for bit-field `%s'", name); 3190132730Skan *width = integer_one_node; 3191132730Skan } 3192132730Skan } 3193132730Skan 3194132730Skan /* Detect invalid bit-field type. */ 3195132730Skan if (TREE_CODE (*type) != INTEGER_TYPE 3196132730Skan && TREE_CODE (*type) != BOOLEAN_TYPE 3197132730Skan && TREE_CODE (*type) != ENUMERAL_TYPE) 3198132730Skan { 3199132730Skan error ("bit-field `%s' has invalid type", name); 3200132730Skan *type = unsigned_type_node; 3201132730Skan } 3202132730Skan 3203132730Skan type_mv = TYPE_MAIN_VARIANT (*type); 3204132730Skan if (pedantic 3205132730Skan && type_mv != integer_type_node 3206132730Skan && type_mv != unsigned_type_node 3207132730Skan && type_mv != boolean_type_node) 3208132730Skan pedwarn ("type of bit-field `%s' is a GCC extension", name); 3209132730Skan 3210132730Skan if (type_mv == boolean_type_node) 3211132730Skan max_width = CHAR_TYPE_SIZE; 3212132730Skan else 3213132730Skan max_width = TYPE_PRECISION (*type); 3214132730Skan 3215132730Skan if (0 < compare_tree_int (*width, max_width)) 3216132730Skan { 3217132730Skan error ("width of `%s' exceeds its type", name); 3218132730Skan w = max_width; 3219132730Skan *width = build_int_2 (w, 0); 3220132730Skan } 3221132730Skan else 3222132730Skan w = tree_low_cst (*width, 1); 3223132730Skan 3224132730Skan if (TREE_CODE (*type) == ENUMERAL_TYPE 3225132730Skan && (w < min_precision (TYPE_MIN_VALUE (*type), TREE_UNSIGNED (*type)) 3226132730Skan || w < min_precision (TYPE_MAX_VALUE (*type), TREE_UNSIGNED (*type)))) 3227132730Skan warning ("`%s' is narrower than values of its type", name); 3228132730Skan} 3229132730Skan 323018334Speter/* Given declspecs and a declarator, 323118334Speter determine the name and type of the object declared 323218334Speter and construct a ..._DECL node for it. 323318334Speter (In one case we can return a ..._TYPE node instead. 323418334Speter For invalid input we sometimes return 0.) 323518334Speter 323618334Speter DECLSPECS is a chain of tree_list nodes whose value fields 323718334Speter are the storage classes and type specifiers. 323818334Speter 323918334Speter DECL_CONTEXT says which syntactic context this declaration is in: 324018334Speter NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. 324118334Speter FUNCDEF for a function definition. Like NORMAL but a few different 324218334Speter error messages in each case. Return value may be zero meaning 324318334Speter this definition is too screwy to try to parse. 324418334Speter PARM for a parameter declaration (either within a function prototype 324518334Speter or before a function body). Make a PARM_DECL, or return void_type_node. 324618334Speter TYPENAME if for a typename (in a cast or sizeof). 324718334Speter Don't make a DECL node; just return the ..._TYPE node. 324818334Speter FIELD for a struct or union field; make a FIELD_DECL. 324918334Speter INITIALIZED is 1 if the decl has an initializer. 3250132730Skan WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node 3251132730Skan representing the width of the bit-field. 325218334Speter 325318334Speter In the TYPENAME case, DECLARATOR is really an absolute declarator. 325418334Speter It may also be so in the PARM case, for a prototype where the 325518334Speter argument type is specified but not the name. 325618334Speter 325718334Speter This function is where the complicated C meanings of `static' 325818334Speter and `extern' are interpreted. */ 325918334Speter 326018334Speterstatic tree 3261132730Skangrokdeclarator (tree declarator, tree declspecs, 3262132730Skan enum decl_context decl_context, int initialized, tree *width) 326318334Speter{ 326418334Speter int specbits = 0; 326518334Speter tree spec; 326618334Speter tree type = NULL_TREE; 326718334Speter int longlong = 0; 326818334Speter int constp; 326952284Sobrien int restrictp; 327018334Speter int volatilep; 327152284Sobrien int type_quals = TYPE_UNQUALIFIED; 327218334Speter int inlinep; 327318334Speter int explicit_int = 0; 327418334Speter int explicit_char = 0; 327518334Speter int defaulted_int = 0; 327618334Speter tree typedef_decl = 0; 3277132730Skan const char *name, *orig_name; 327818334Speter tree typedef_type = 0; 327918334Speter int funcdef_flag = 0; 328018334Speter enum tree_code innermost_code = ERROR_MARK; 328118334Speter int size_varies = 0; 328290075Sobrien tree decl_attr = NULL_TREE; 328390075Sobrien tree array_ptr_quals = NULL_TREE; 328490075Sobrien int array_parm_static = 0; 328590075Sobrien tree returned_attrs = NULL_TREE; 3286132730Skan bool bitfield = width != NULL; 3287132730Skan tree element_type; 328818334Speter 328918334Speter if (decl_context == FUNCDEF) 329018334Speter funcdef_flag = 1, decl_context = NORMAL; 329118334Speter 329218334Speter /* Look inside a declarator for the name being declared 329318334Speter and get it as a string, for an error message. */ 329418334Speter { 329590075Sobrien tree decl = declarator; 329618334Speter name = 0; 329718334Speter 329818334Speter while (decl) 329918334Speter switch (TREE_CODE (decl)) 330018334Speter { 330118334Speter case ARRAY_REF: 330218334Speter case INDIRECT_REF: 330318334Speter case CALL_EXPR: 330418334Speter innermost_code = TREE_CODE (decl); 330518334Speter decl = TREE_OPERAND (decl, 0); 330618334Speter break; 330718334Speter 330890075Sobrien case TREE_LIST: 330990075Sobrien decl = TREE_VALUE (decl); 331090075Sobrien break; 331190075Sobrien 331218334Speter case IDENTIFIER_NODE: 331318334Speter name = IDENTIFIER_POINTER (decl); 331418334Speter decl = 0; 331518334Speter break; 331618334Speter 331718334Speter default: 331818334Speter abort (); 331918334Speter } 3320132730Skan orig_name = name; 332118334Speter if (name == 0) 332218334Speter name = "type name"; 332318334Speter } 332418334Speter 332518334Speter /* A function definition's declarator must have the form of 332618334Speter a function declarator. */ 332718334Speter 332818334Speter if (funcdef_flag && innermost_code != CALL_EXPR) 332918334Speter return 0; 333018334Speter 333118334Speter /* If this looks like a function definition, make it one, 333218334Speter even if it occurs where parms are expected. 333318334Speter Then store_parm_decls will reject it and not use it as a parm. */ 333418334Speter if (decl_context == NORMAL && !funcdef_flag 3335132730Skan && current_scope->parm_flag) 333618334Speter decl_context = PARM; 333718334Speter 333818334Speter /* Look through the decl specs and record which ones appear. 333918334Speter Some typespecs are defined as built-in typenames. 334018334Speter Others, the ones that are modifiers of other types, 334118334Speter are represented by bits in SPECBITS: set the bits for 334218334Speter the modifiers that appear. Storage class keywords are also in SPECBITS. 334318334Speter 334418334Speter If there is a typedef name or a type, store the type in TYPE. 334518334Speter This includes builtin typedefs such as `int'. 334618334Speter 334718334Speter Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char' 334818334Speter and did not come from a user typedef. 334918334Speter 335018334Speter Set LONGLONG if `long' is mentioned twice. */ 335118334Speter 335218334Speter for (spec = declspecs; spec; spec = TREE_CHAIN (spec)) 335318334Speter { 335490075Sobrien tree id = TREE_VALUE (spec); 335518334Speter 335690075Sobrien /* If the entire declaration is itself tagged as deprecated then 335790075Sobrien suppress reports of deprecated items. */ 335890075Sobrien if (id && TREE_DEPRECATED (id)) 335990075Sobrien { 336090075Sobrien if (deprecated_state != DEPRECATED_SUPPRESS) 336190075Sobrien warn_deprecated_use (id); 336290075Sobrien } 336390075Sobrien 336418334Speter if (id == ridpointers[(int) RID_INT]) 336518334Speter explicit_int = 1; 336618334Speter if (id == ridpointers[(int) RID_CHAR]) 336718334Speter explicit_char = 1; 336818334Speter 336990075Sobrien if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id)) 337090075Sobrien { 337190075Sobrien enum rid i = C_RID_CODE (id); 337290075Sobrien if ((int) i <= (int) RID_LAST_MODIFIER) 337390075Sobrien { 3374117421Skan if (i == RID_LONG && (specbits & (1 << (int) RID_LONG))) 337590075Sobrien { 337690075Sobrien if (longlong) 337790075Sobrien error ("`long long long' is too long for GCC"); 337890075Sobrien else 337990075Sobrien { 338090075Sobrien if (pedantic && !flag_isoc99 && ! in_system_header 338190075Sobrien && warn_long_long) 3382117421Skan pedwarn ("ISO C90 does not support `long long'"); 338390075Sobrien longlong = 1; 338490075Sobrien } 338590075Sobrien } 338690075Sobrien else if (specbits & (1 << (int) i)) 3387117421Skan { 3388117421Skan if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT) 3389117421Skan { 3390132730Skan if (pedantic && !flag_isoc99) 3391117421Skan pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id)); 3392117421Skan } 3393117421Skan else 3394117421Skan error ("duplicate `%s'", IDENTIFIER_POINTER (id)); 3395117421Skan } 3396117421Skan 3397117421Skan /* Diagnose "__thread extern". Recall that this list 3398117421Skan is in the reverse order seen in the text. */ 3399117421Skan if (i == RID_THREAD 3400117421Skan && (specbits & (1 << (int) RID_EXTERN 3401117421Skan | 1 << (int) RID_STATIC))) 3402117421Skan { 3403117421Skan if (specbits & 1 << (int) RID_EXTERN) 3404117421Skan error ("`__thread' before `extern'"); 3405117421Skan else 3406117421Skan error ("`__thread' before `static'"); 3407117421Skan } 3408117421Skan 340990075Sobrien specbits |= 1 << (int) i; 341090075Sobrien goto found; 341190075Sobrien } 341290075Sobrien } 341318334Speter if (type) 341418334Speter error ("two or more data types in declaration of `%s'", name); 341518334Speter /* Actual typedefs come to us as TYPE_DECL nodes. */ 341618334Speter else if (TREE_CODE (id) == TYPE_DECL) 341718334Speter { 341890075Sobrien if (TREE_TYPE (id) == error_mark_node) 341990075Sobrien ; /* Allow the type to default to int to avoid cascading errors. */ 342090075Sobrien else 342190075Sobrien { 342290075Sobrien type = TREE_TYPE (id); 342390075Sobrien decl_attr = DECL_ATTRIBUTES (id); 342490075Sobrien typedef_decl = id; 342590075Sobrien } 342618334Speter } 342718334Speter /* Built-in types come as identifiers. */ 342818334Speter else if (TREE_CODE (id) == IDENTIFIER_NODE) 342918334Speter { 343090075Sobrien tree t = lookup_name (id); 343118334Speter if (TREE_TYPE (t) == error_mark_node) 343218334Speter ; 343318334Speter else if (!t || TREE_CODE (t) != TYPE_DECL) 343418334Speter error ("`%s' fails to be a typedef or built in type", 343518334Speter IDENTIFIER_POINTER (id)); 343618334Speter else 343718334Speter { 343818334Speter type = TREE_TYPE (t); 343918334Speter typedef_decl = t; 344018334Speter } 344118334Speter } 344218334Speter else if (TREE_CODE (id) != ERROR_MARK) 344318334Speter type = id; 344418334Speter 344590075Sobrien found: 344690075Sobrien ; 344718334Speter } 344818334Speter 344918334Speter typedef_type = type; 345018334Speter if (type) 345118334Speter size_varies = C_TYPE_VARIABLE_SIZE (type); 345218334Speter 345318334Speter /* No type at all: default to `int', and set DEFAULTED_INT 345418334Speter because it was not a user-defined typedef. */ 345518334Speter 345618334Speter if (type == 0) 345718334Speter { 345850397Sobrien if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT) 345950397Sobrien | (1 << (int) RID_SIGNED) 346090075Sobrien | (1 << (int) RID_UNSIGNED) 346190075Sobrien | (1 << (int) RID_COMPLEX)))) 346250397Sobrien /* Don't warn about typedef foo = bar. */ 346350397Sobrien && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized) 346490075Sobrien && ! in_system_header) 346550397Sobrien { 346690075Sobrien /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type 346752284Sobrien and this is a function, or if -Wimplicit; prefer the former 346852284Sobrien warning since it is more explicit. */ 346990075Sobrien if ((warn_implicit_int || warn_return_type || flag_isoc99) 347090075Sobrien && funcdef_flag) 347150397Sobrien warn_about_return_type = 1; 347290075Sobrien else if (warn_implicit_int || flag_isoc99) 347390075Sobrien pedwarn_c99 ("type defaults to `int' in declaration of `%s'", 347490075Sobrien name); 347550397Sobrien } 347650397Sobrien 347718334Speter defaulted_int = 1; 347818334Speter type = integer_type_node; 347918334Speter } 348018334Speter 348118334Speter /* Now process the modifiers that were specified 348218334Speter and check for invalid combinations. */ 348318334Speter 348418334Speter /* Long double is a special combination. */ 348518334Speter 348650397Sobrien if ((specbits & 1 << (int) RID_LONG) && ! longlong 348718334Speter && TYPE_MAIN_VARIANT (type) == double_type_node) 348818334Speter { 348990075Sobrien specbits &= ~(1 << (int) RID_LONG); 349018334Speter type = long_double_type_node; 349118334Speter } 349218334Speter 349318334Speter /* Check all other uses of type modifiers. */ 349418334Speter 349518334Speter if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT) 349618334Speter | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED))) 349718334Speter { 349818334Speter int ok = 0; 349918334Speter 350050397Sobrien if ((specbits & 1 << (int) RID_LONG) 350150397Sobrien && (specbits & 1 << (int) RID_SHORT)) 350250397Sobrien error ("both long and short specified for `%s'", name); 350318334Speter else if (((specbits & 1 << (int) RID_LONG) 350418334Speter || (specbits & 1 << (int) RID_SHORT)) 350518334Speter && explicit_char) 350618334Speter error ("long or short specified with char for `%s'", name); 350718334Speter else if (((specbits & 1 << (int) RID_LONG) 350818334Speter || (specbits & 1 << (int) RID_SHORT)) 350918334Speter && TREE_CODE (type) == REAL_TYPE) 351050397Sobrien { 351150397Sobrien static int already = 0; 351250397Sobrien 351350397Sobrien error ("long or short specified with floating type for `%s'", name); 351450397Sobrien if (! already && ! pedantic) 351550397Sobrien { 351650397Sobrien error ("the only valid combination is `long double'"); 351750397Sobrien already = 1; 351850397Sobrien } 351950397Sobrien } 352018334Speter else if ((specbits & 1 << (int) RID_SIGNED) 352118334Speter && (specbits & 1 << (int) RID_UNSIGNED)) 352250397Sobrien error ("both signed and unsigned specified for `%s'", name); 352350397Sobrien else if (TREE_CODE (type) != INTEGER_TYPE) 352450397Sobrien error ("long, short, signed or unsigned invalid for `%s'", name); 352518334Speter else 352618334Speter { 352718334Speter ok = 1; 3528117421Skan if (!explicit_int && !defaulted_int && !explicit_char) 352918334Speter { 3530117421Skan error ("long, short, signed or unsigned used invalidly for `%s'", 3531117421Skan name); 3532117421Skan ok = 0; 353318334Speter } 353418334Speter } 353518334Speter 353618334Speter /* Discard the type modifiers if they are invalid. */ 353718334Speter if (! ok) 353818334Speter { 353918334Speter specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT) 354018334Speter | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)); 354118334Speter longlong = 0; 354218334Speter } 354318334Speter } 354418334Speter 354518334Speter if ((specbits & (1 << (int) RID_COMPLEX)) 354618334Speter && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE) 354718334Speter { 354818334Speter error ("complex invalid for `%s'", name); 354990075Sobrien specbits &= ~(1 << (int) RID_COMPLEX); 355018334Speter } 355118334Speter 355218334Speter /* Decide whether an integer type is signed or not. 3553132730Skan Optionally treat bit-fields as signed by default. */ 355418334Speter if (specbits & 1 << (int) RID_UNSIGNED 355518334Speter || (bitfield && ! flag_signed_bitfields 355618334Speter && (explicit_int || defaulted_int || explicit_char 355718334Speter /* A typedef for plain `int' without `signed' 355818334Speter can be controlled just like plain `int'. */ 355918334Speter || ! (typedef_decl != 0 356018334Speter && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) 356118334Speter && TREE_CODE (type) != ENUMERAL_TYPE 356218334Speter && !(specbits & 1 << (int) RID_SIGNED))) 356318334Speter { 356418334Speter if (longlong) 356518334Speter type = long_long_unsigned_type_node; 356618334Speter else if (specbits & 1 << (int) RID_LONG) 356718334Speter type = long_unsigned_type_node; 356818334Speter else if (specbits & 1 << (int) RID_SHORT) 356918334Speter type = short_unsigned_type_node; 357018334Speter else if (type == char_type_node) 357118334Speter type = unsigned_char_type_node; 357218334Speter else if (typedef_decl) 3573117421Skan type = c_common_unsigned_type (type); 357418334Speter else 357518334Speter type = unsigned_type_node; 357618334Speter } 357718334Speter else if ((specbits & 1 << (int) RID_SIGNED) 357818334Speter && type == char_type_node) 357918334Speter type = signed_char_type_node; 358018334Speter else if (longlong) 358118334Speter type = long_long_integer_type_node; 358218334Speter else if (specbits & 1 << (int) RID_LONG) 358318334Speter type = long_integer_type_node; 358418334Speter else if (specbits & 1 << (int) RID_SHORT) 358518334Speter type = short_integer_type_node; 358618334Speter 358718334Speter if (specbits & 1 << (int) RID_COMPLEX) 358818334Speter { 358990075Sobrien if (pedantic && !flag_isoc99) 3590117421Skan pedwarn ("ISO C90 does not support complex types"); 359118334Speter /* If we just have "complex", it is equivalent to 359218334Speter "complex double", but if any modifiers at all are specified it is 359318334Speter the complex form of TYPE. E.g, "complex short" is 359418334Speter "complex short int". */ 359518334Speter 359618334Speter if (defaulted_int && ! longlong 359718334Speter && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT) 359818334Speter | (1 << (int) RID_SIGNED) 359918334Speter | (1 << (int) RID_UNSIGNED)))) 360090075Sobrien { 360190075Sobrien if (pedantic) 360290075Sobrien pedwarn ("ISO C does not support plain `complex' meaning `double complex'"); 360390075Sobrien type = complex_double_type_node; 360490075Sobrien } 360518334Speter else if (type == integer_type_node) 360690075Sobrien { 360790075Sobrien if (pedantic) 360890075Sobrien pedwarn ("ISO C does not support complex integer types"); 360990075Sobrien type = complex_integer_type_node; 361090075Sobrien } 361118334Speter else if (type == float_type_node) 361218334Speter type = complex_float_type_node; 361318334Speter else if (type == double_type_node) 361418334Speter type = complex_double_type_node; 361518334Speter else if (type == long_double_type_node) 361618334Speter type = complex_long_double_type_node; 361718334Speter else 361890075Sobrien { 361990075Sobrien if (pedantic) 362090075Sobrien pedwarn ("ISO C does not support complex integer types"); 362190075Sobrien type = build_complex_type (type); 362290075Sobrien } 362318334Speter } 362418334Speter 362552284Sobrien /* Figure out the type qualifiers for the declaration. There are 362652284Sobrien two ways a declaration can become qualified. One is something 362752284Sobrien like `const int i' where the `const' is explicit. Another is 362852284Sobrien something like `typedef const int CI; CI i' where the type of the 3629132730Skan declaration contains the `const'. A third possibility is that 3630132730Skan there is a type qualifier on the element type of a typedefed 3631132730Skan array type, in which case we should extract that qualifier so 3632132730Skan that c_apply_type_quals_to_decls receives the full list of 3633132730Skan qualifiers to work with (C90 is not entirely clear about whether 3634132730Skan duplicate qualifiers should be diagnosed in this case, but it 3635132730Skan seems most appropriate to do so). */ 3636132730Skan element_type = strip_array_types (type); 3637132730Skan constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type); 3638132730Skan restrictp 3639132730Skan = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type); 3640132730Skan volatilep 3641132730Skan = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type); 364218334Speter inlinep = !! (specbits & (1 << (int) RID_INLINE)); 3643132730Skan if (pedantic && !flag_isoc99) 3644132730Skan { 3645132730Skan if (constp > 1) 3646132730Skan pedwarn ("duplicate `const'"); 3647132730Skan if (restrictp > 1) 3648132730Skan pedwarn ("duplicate `restrict'"); 3649132730Skan if (volatilep > 1) 3650132730Skan pedwarn ("duplicate `volatile'"); 3651132730Skan } 365252284Sobrien if (! flag_gen_aux_info && (TYPE_QUALS (type))) 365318334Speter type = TYPE_MAIN_VARIANT (type); 365452284Sobrien type_quals = ((constp ? TYPE_QUAL_CONST : 0) 365552284Sobrien | (restrictp ? TYPE_QUAL_RESTRICT : 0) 365652284Sobrien | (volatilep ? TYPE_QUAL_VOLATILE : 0)); 365718334Speter 365818334Speter /* Warn if two storage classes are given. Default to `auto'. */ 365918334Speter 366018334Speter { 366118334Speter int nclasses = 0; 366218334Speter 366318334Speter if (specbits & 1 << (int) RID_AUTO) nclasses++; 366418334Speter if (specbits & 1 << (int) RID_STATIC) nclasses++; 366518334Speter if (specbits & 1 << (int) RID_EXTERN) nclasses++; 366618334Speter if (specbits & 1 << (int) RID_REGISTER) nclasses++; 366718334Speter if (specbits & 1 << (int) RID_TYPEDEF) nclasses++; 366818334Speter 3669117421Skan /* "static __thread" and "extern __thread" are allowed. */ 3670117421Skan if ((specbits & (1 << (int) RID_THREAD 3671117421Skan | 1 << (int) RID_STATIC 3672117421Skan | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD)) 3673117421Skan nclasses++; 3674117421Skan 367518334Speter /* Warn about storage classes that are invalid for certain 367618334Speter kinds of declarations (parameters, typenames, etc.). */ 367718334Speter 367818334Speter if (nclasses > 1) 367918334Speter error ("multiple storage classes in declaration of `%s'", name); 368018334Speter else if (funcdef_flag 368118334Speter && (specbits 368218334Speter & ((1 << (int) RID_REGISTER) 368318334Speter | (1 << (int) RID_AUTO) 3684117421Skan | (1 << (int) RID_TYPEDEF) 3685117421Skan | (1 << (int) RID_THREAD)))) 368618334Speter { 368718334Speter if (specbits & 1 << (int) RID_AUTO 3688132730Skan && (pedantic || current_scope == global_scope)) 368918334Speter pedwarn ("function definition declared `auto'"); 369018334Speter if (specbits & 1 << (int) RID_REGISTER) 369118334Speter error ("function definition declared `register'"); 369218334Speter if (specbits & 1 << (int) RID_TYPEDEF) 369318334Speter error ("function definition declared `typedef'"); 3694117421Skan if (specbits & 1 << (int) RID_THREAD) 3695117421Skan error ("function definition declared `__thread'"); 369690075Sobrien specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER) 3697117421Skan | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD)); 369818334Speter } 369918334Speter else if (decl_context != NORMAL && nclasses > 0) 370018334Speter { 370118334Speter if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER) 370218334Speter ; 370318334Speter else 370418334Speter { 370590075Sobrien switch (decl_context) 370690075Sobrien { 370790075Sobrien case FIELD: 370890075Sobrien error ("storage class specified for structure field `%s'", 370990075Sobrien name); 371090075Sobrien break; 371190075Sobrien case PARM: 371290075Sobrien error ("storage class specified for parameter `%s'", name); 371390075Sobrien break; 371490075Sobrien default: 371590075Sobrien error ("storage class specified for typename"); 371690075Sobrien break; 371790075Sobrien } 371890075Sobrien specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER) 371990075Sobrien | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC) 3720117421Skan | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD)); 372118334Speter } 372218334Speter } 372318334Speter else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag) 372418334Speter { 3725132730Skan /* `extern' with initialization is invalid if not at file scope. */ 3726132730Skan if (current_scope == global_scope) 372718334Speter warning ("`%s' initialized and declared `extern'", name); 372818334Speter else 372918334Speter error ("`%s' has both `extern' and initializer", name); 373018334Speter } 3731132730Skan else if (current_scope == global_scope) 3732117421Skan { 3733117421Skan if (specbits & 1 << (int) RID_AUTO) 3734132730Skan error ("file-scope declaration of `%s' specifies `auto'", name); 3735117421Skan } 3736117421Skan else 3737117421Skan { 3738117421Skan if (specbits & 1 << (int) RID_EXTERN && funcdef_flag) 3739117421Skan error ("nested function `%s' declared `extern'", name); 3740117421Skan else if ((specbits & (1 << (int) RID_THREAD 3741117421Skan | 1 << (int) RID_EXTERN 3742117421Skan | 1 << (int) RID_STATIC)) 3743117421Skan == (1 << (int) RID_THREAD)) 3744117421Skan { 3745117421Skan error ("function-scope `%s' implicitly auto and declared `__thread'", 3746117421Skan name); 3747117421Skan specbits &= ~(1 << (int) RID_THREAD); 3748117421Skan } 3749117421Skan } 375018334Speter } 375118334Speter 375218334Speter /* Now figure out the structure of the declarator proper. 375318334Speter Descend through it, creating more complex types, until we reach 375418334Speter the declared identifier (or NULL_TREE, in an absolute declarator). */ 375518334Speter 375618334Speter while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE) 375718334Speter { 375818334Speter if (type == error_mark_node) 375918334Speter { 376018334Speter declarator = TREE_OPERAND (declarator, 0); 376118334Speter continue; 376218334Speter } 376318334Speter 376418334Speter /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]), 376518334Speter an INDIRECT_REF (for *...), 376618334Speter a CALL_EXPR (for ...(...)), 376790075Sobrien a TREE_LIST (for nested attributes), 376818334Speter an identifier (for the name being declared) 376918334Speter or a null pointer (for the place in an absolute declarator 377018334Speter where the name was omitted). 377118334Speter For the last two cases, we have just exited the loop. 377218334Speter 377318334Speter At this point, TYPE is the type of elements of an array, 377418334Speter or for a function to return, or for a pointer to point to. 377518334Speter After this sequence of ifs, TYPE is the type of the 377618334Speter array or function or pointer, and DECLARATOR has had its 377718334Speter outermost layer removed. */ 377818334Speter 377990075Sobrien if (array_ptr_quals != NULL_TREE || array_parm_static) 378018334Speter { 378190075Sobrien /* Only the innermost declarator (making a parameter be of 378290075Sobrien array type which is converted to pointer type) 378390075Sobrien may have static or type qualifiers. */ 378490075Sobrien error ("static or type qualifiers in non-parameter array declarator"); 378590075Sobrien array_ptr_quals = NULL_TREE; 378690075Sobrien array_parm_static = 0; 378790075Sobrien } 378890075Sobrien 378990075Sobrien if (TREE_CODE (declarator) == TREE_LIST) 379090075Sobrien { 379190075Sobrien /* We encode a declarator with embedded attributes using 379290075Sobrien a TREE_LIST. */ 379390075Sobrien tree attrs = TREE_PURPOSE (declarator); 379490075Sobrien tree inner_decl; 379590075Sobrien int attr_flags = 0; 379690075Sobrien declarator = TREE_VALUE (declarator); 379790075Sobrien inner_decl = declarator; 379890075Sobrien while (inner_decl != NULL_TREE 379990075Sobrien && TREE_CODE (inner_decl) == TREE_LIST) 380090075Sobrien inner_decl = TREE_VALUE (inner_decl); 380190075Sobrien if (inner_decl == NULL_TREE 380290075Sobrien || TREE_CODE (inner_decl) == IDENTIFIER_NODE) 380390075Sobrien attr_flags |= (int) ATTR_FLAG_DECL_NEXT; 380496263Sobrien else if (TREE_CODE (inner_decl) == CALL_EXPR) 380590075Sobrien attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; 380696263Sobrien else if (TREE_CODE (inner_decl) == ARRAY_REF) 380790075Sobrien attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; 380890075Sobrien returned_attrs = decl_attributes (&type, 380990075Sobrien chainon (returned_attrs, attrs), 381090075Sobrien attr_flags); 381190075Sobrien } 381290075Sobrien else if (TREE_CODE (declarator) == ARRAY_REF) 381390075Sobrien { 381490075Sobrien tree itype = NULL_TREE; 381590075Sobrien tree size = TREE_OPERAND (declarator, 1); 381618334Speter /* The index is a signed object `sizetype' bits wide. */ 3817117421Skan tree index_type = c_common_signed_type (sizetype); 381818334Speter 381990075Sobrien array_ptr_quals = TREE_TYPE (declarator); 382090075Sobrien array_parm_static = TREE_STATIC (declarator); 382190075Sobrien 382218334Speter declarator = TREE_OPERAND (declarator, 0); 382318334Speter 382418334Speter /* Check for some types that there cannot be arrays of. */ 382518334Speter 382690075Sobrien if (VOID_TYPE_P (type)) 382718334Speter { 382818334Speter error ("declaration of `%s' as array of voids", name); 382918334Speter type = error_mark_node; 383018334Speter } 383118334Speter 383218334Speter if (TREE_CODE (type) == FUNCTION_TYPE) 383318334Speter { 383418334Speter error ("declaration of `%s' as array of functions", name); 383518334Speter type = error_mark_node; 383618334Speter } 383718334Speter 3838132730Skan if (pedantic && !in_system_header && flexible_array_type_p (type)) 3839117421Skan pedwarn ("invalid use of structure with flexible array member"); 3840117421Skan 384118334Speter if (size == error_mark_node) 384218334Speter type = error_mark_node; 384318334Speter 384418334Speter if (type == error_mark_node) 384518334Speter continue; 384618334Speter 384718334Speter /* If size was specified, set ITYPE to a range-type for that size. 384818334Speter Otherwise, ITYPE remains null. finish_decl may figure it out 384918334Speter from an initial value. */ 385018334Speter 385118334Speter if (size) 385218334Speter { 385318334Speter /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 385418334Speter STRIP_TYPE_NOPS (size); 385518334Speter 385690075Sobrien if (! INTEGRAL_TYPE_P (TREE_TYPE (size))) 385718334Speter { 385818334Speter error ("size of array `%s' has non-integer type", name); 385918334Speter size = integer_one_node; 386018334Speter } 386118334Speter 386218334Speter if (pedantic && integer_zerop (size)) 386390075Sobrien pedwarn ("ISO C forbids zero-size array `%s'", name); 386418334Speter 386518334Speter if (TREE_CODE (size) == INTEGER_CST) 386618334Speter { 386718334Speter constant_expression_warning (size); 386818334Speter if (tree_int_cst_sgn (size) < 0) 386918334Speter { 387018334Speter error ("size of array `%s' is negative", name); 387118334Speter size = integer_one_node; 387218334Speter } 387318334Speter } 387418334Speter else 387518334Speter { 387618334Speter /* Make sure the array size remains visibly nonconstant 387718334Speter even if it is (eg) a const variable with known value. */ 387818334Speter size_varies = 1; 387918334Speter 3880102790Skan if (!flag_isoc99 && pedantic) 388118334Speter { 388218334Speter if (TREE_CONSTANT (size)) 3883117421Skan pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated", 388490075Sobrien name); 388518334Speter else 3886117421Skan pedwarn ("ISO C90 forbids variable-size array `%s'", 388790075Sobrien name); 388818334Speter } 388918334Speter } 389018334Speter 389190075Sobrien if (integer_zerop (size)) 389250397Sobrien { 389390075Sobrien /* A zero-length array cannot be represented with an 389490075Sobrien unsigned index type, which is what we'll get with 389590075Sobrien build_index_type. Create an open-ended range instead. */ 389690075Sobrien itype = build_range_type (sizetype, size, NULL_TREE); 389750397Sobrien } 389890075Sobrien else 389990075Sobrien { 390090075Sobrien /* Compute the maximum valid index, that is, size - 1. 390190075Sobrien Do the calculation in index_type, so that if it is 390290075Sobrien a variable the computations will be done in the 390390075Sobrien proper mode. */ 390490075Sobrien itype = fold (build (MINUS_EXPR, index_type, 390590075Sobrien convert (index_type, size), 390690075Sobrien convert (index_type, size_one_node))); 390750397Sobrien 390890075Sobrien /* If that overflowed, the array is too big. 390990075Sobrien ??? While a size of INT_MAX+1 technically shouldn't 391090075Sobrien cause an overflow (because we subtract 1), the overflow 391190075Sobrien is recorded during the conversion to index_type, before 391290075Sobrien the subtraction. Handling this case seems like an 391390075Sobrien unnecessary complication. */ 391490075Sobrien if (TREE_OVERFLOW (itype)) 391590075Sobrien { 391690075Sobrien error ("size of array `%s' is too large", name); 391790075Sobrien type = error_mark_node; 391890075Sobrien continue; 391990075Sobrien } 392090075Sobrien 392190075Sobrien if (size_varies) 3922117421Skan { 3923117421Skan /* We must be able to distinguish the 3924117421Skan SAVE_EXPR_CONTEXT for the variably-sized type 3925117421Skan so that we can set it correctly in 3926117421Skan set_save_expr_context. The convention is 3927117421Skan that all SAVE_EXPRs that need to be reset 3928117421Skan have NULL_TREE for their SAVE_EXPR_CONTEXT. */ 3929117421Skan tree cfd = current_function_decl; 3930117421Skan if (decl_context == PARM) 3931117421Skan current_function_decl = NULL_TREE; 3932117421Skan itype = variable_size (itype); 3933117421Skan if (decl_context == PARM) 3934117421Skan current_function_decl = cfd; 3935117421Skan } 393690075Sobrien itype = build_index_type (itype); 393790075Sobrien } 393818334Speter } 393990075Sobrien else if (decl_context == FIELD) 394090075Sobrien { 394190075Sobrien if (pedantic && !flag_isoc99 && !in_system_header) 3942117421Skan pedwarn ("ISO C90 does not support flexible array members"); 394318334Speter 394490075Sobrien /* ISO C99 Flexible array members are effectively identical 394590075Sobrien to GCC's zero-length array extension. */ 394690075Sobrien itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 394790075Sobrien } 394818334Speter 394990075Sobrien /* If pedantic, complain about arrays of incomplete types. */ 395090075Sobrien 395190075Sobrien if (pedantic && !COMPLETE_TYPE_P (type)) 395290075Sobrien pedwarn ("array type has incomplete element type"); 395390075Sobrien 395418334Speter /* Build the array type itself, then merge any constancy or 395518334Speter volatility into the target type. We must do it in this order 395618334Speter to ensure that the TYPE_MAIN_VARIANT field of the array type 395718334Speter is set correctly. */ 395818334Speter 395918334Speter type = build_array_type (type, itype); 396052284Sobrien if (type_quals) 396152284Sobrien type = c_build_qualified_type (type, type_quals); 396218334Speter 396318334Speter if (size_varies) 396418334Speter C_TYPE_VARIABLE_SIZE (type) = 1; 396590075Sobrien 396690075Sobrien /* The GCC extension for zero-length arrays differs from 396790075Sobrien ISO flexible array members in that sizeof yields zero. */ 396890075Sobrien if (size && integer_zerop (size)) 396990075Sobrien { 397090075Sobrien layout_type (type); 397190075Sobrien TYPE_SIZE (type) = bitsize_zero_node; 397290075Sobrien TYPE_SIZE_UNIT (type) = size_zero_node; 397390075Sobrien } 397490075Sobrien if (decl_context != PARM 397590075Sobrien && (array_ptr_quals != NULL_TREE || array_parm_static)) 397690075Sobrien { 397790075Sobrien error ("static or type qualifiers in non-parameter array declarator"); 397890075Sobrien array_ptr_quals = NULL_TREE; 397990075Sobrien array_parm_static = 0; 398090075Sobrien } 398118334Speter } 398218334Speter else if (TREE_CODE (declarator) == CALL_EXPR) 398318334Speter { 3984146906Skan /* Say it's a definition only for the declarator closest to 3985146906Skan the identifier, apart possibly from some attributes. */ 3986146906Skan bool really_funcdef = false; 398718334Speter tree arg_types; 3988146906Skan if (funcdef_flag) 3989146906Skan { 3990146906Skan tree t = TREE_OPERAND (declarator, 0); 3991146906Skan while (TREE_CODE (t) == TREE_LIST) 3992146906Skan t = TREE_VALUE (t); 3993146906Skan really_funcdef = (TREE_CODE (t) == IDENTIFIER_NODE); 3994146906Skan } 399518334Speter 399618334Speter /* Declaring a function type. 399718334Speter Make sure we have a valid type for the function to return. */ 399818334Speter if (type == error_mark_node) 399918334Speter continue; 400018334Speter 400118334Speter size_varies = 0; 400218334Speter 400318334Speter /* Warn about some types functions can't return. */ 400418334Speter 400518334Speter if (TREE_CODE (type) == FUNCTION_TYPE) 400618334Speter { 400718334Speter error ("`%s' declared as function returning a function", name); 400818334Speter type = integer_type_node; 400918334Speter } 401018334Speter if (TREE_CODE (type) == ARRAY_TYPE) 401118334Speter { 401218334Speter error ("`%s' declared as function returning an array", name); 401318334Speter type = integer_type_node; 401418334Speter } 401518334Speter 401618334Speter /* Construct the function type and go to the next 401718334Speter inner layer of declarator. */ 401818334Speter 401918334Speter arg_types = grokparms (TREE_OPERAND (declarator, 1), 4020146906Skan really_funcdef); 402152284Sobrien /* Type qualifiers before the return type of the function 402252284Sobrien qualify the return type, not the function type. */ 402352284Sobrien if (type_quals) 402490075Sobrien { 402590075Sobrien /* Type qualifiers on a function return type are normally 402690075Sobrien permitted by the standard but have no effect, so give a 4027132730Skan warning at -Wextra. Qualifiers on a void return type have 402890075Sobrien meaning as a GNU extension, and are banned on function 402990075Sobrien definitions in ISO C. FIXME: strictly we shouldn't 403090075Sobrien pedwarn for qualified void return types except on function 403190075Sobrien definitions, but not doing so could lead to the undesirable 403290075Sobrien state of a "volatile void" function return type not being 403390075Sobrien warned about, and a use of the function being compiled 403490075Sobrien with GNU semantics, with no diagnostics under -pedantic. */ 403590075Sobrien if (VOID_TYPE_P (type) && pedantic && !in_system_header) 403690075Sobrien pedwarn ("ISO C forbids qualified void function return type"); 403790075Sobrien else if (extra_warnings 403890075Sobrien && !(VOID_TYPE_P (type) 403990075Sobrien && type_quals == TYPE_QUAL_VOLATILE)) 404090075Sobrien warning ("type qualifiers ignored on function return type"); 404190075Sobrien 404290075Sobrien type = c_build_qualified_type (type, type_quals); 404390075Sobrien } 404452284Sobrien type_quals = TYPE_UNQUALIFIED; 404518334Speter 404618334Speter type = build_function_type (type, arg_types); 404718334Speter declarator = TREE_OPERAND (declarator, 0); 404818334Speter 404918334Speter /* Set the TYPE_CONTEXTs for each tagged type which is local to 405018334Speter the formal parameter list of this FUNCTION_TYPE to point to 405118334Speter the FUNCTION_TYPE node itself. */ 405218334Speter 405318334Speter { 405490075Sobrien tree link; 405518334Speter 405650397Sobrien for (link = last_function_parm_tags; 405718334Speter link; 405818334Speter link = TREE_CHAIN (link)) 405918334Speter TYPE_CONTEXT (TREE_VALUE (link)) = type; 406018334Speter } 406118334Speter } 406218334Speter else if (TREE_CODE (declarator) == INDIRECT_REF) 406318334Speter { 406418334Speter /* Merge any constancy or volatility into the target type 406518334Speter for the pointer. */ 406618334Speter 406718334Speter if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 406852284Sobrien && type_quals) 406990075Sobrien pedwarn ("ISO C forbids qualified function types"); 407052284Sobrien if (type_quals) 407152284Sobrien type = c_build_qualified_type (type, type_quals); 407252284Sobrien type_quals = TYPE_UNQUALIFIED; 407318334Speter size_varies = 0; 407418334Speter 407518334Speter type = build_pointer_type (type); 407618334Speter 407718334Speter /* Process a list of type modifier keywords 407818334Speter (such as const or volatile) that were given inside the `*'. */ 407918334Speter 408018334Speter if (TREE_TYPE (declarator)) 408118334Speter { 408290075Sobrien tree typemodlist; 408318334Speter int erred = 0; 408452284Sobrien 408552284Sobrien constp = 0; 408652284Sobrien volatilep = 0; 408752284Sobrien restrictp = 0; 408818334Speter for (typemodlist = TREE_TYPE (declarator); typemodlist; 408918334Speter typemodlist = TREE_CHAIN (typemodlist)) 409018334Speter { 409152284Sobrien tree qualifier = TREE_VALUE (typemodlist); 409252284Sobrien 409390075Sobrien if (C_IS_RESERVED_WORD (qualifier)) 409418334Speter { 409590075Sobrien if (C_RID_CODE (qualifier) == RID_CONST) 409690075Sobrien constp++; 409790075Sobrien else if (C_RID_CODE (qualifier) == RID_VOLATILE) 409890075Sobrien volatilep++; 409990075Sobrien else if (C_RID_CODE (qualifier) == RID_RESTRICT) 410090075Sobrien restrictp++; 410190075Sobrien else 410290075Sobrien erred++; 410318334Speter } 410490075Sobrien else 410590075Sobrien erred++; 410618334Speter } 410790075Sobrien 410890075Sobrien if (erred) 410990075Sobrien error ("invalid type modifier within pointer declarator"); 4110132730Skan if (pedantic && !flag_isoc99) 4111132730Skan { 4112132730Skan if (constp > 1) 4113132730Skan pedwarn ("duplicate `const'"); 4114132730Skan if (volatilep > 1) 4115132730Skan pedwarn ("duplicate `volatile'"); 4116132730Skan if (restrictp > 1) 4117132730Skan pedwarn ("duplicate `restrict'"); 4118132730Skan } 411952284Sobrien 412052284Sobrien type_quals = ((constp ? TYPE_QUAL_CONST : 0) 412152284Sobrien | (restrictp ? TYPE_QUAL_RESTRICT : 0) 412252284Sobrien | (volatilep ? TYPE_QUAL_VOLATILE : 0)); 412318334Speter } 412418334Speter 412518334Speter declarator = TREE_OPERAND (declarator, 0); 412618334Speter } 412718334Speter else 412818334Speter abort (); 412918334Speter 413018334Speter } 413118334Speter 413218334Speter /* Now TYPE has the actual type. */ 413318334Speter 4134146906Skan /* Check the type and width of a bit-field. */ 4135146906Skan if (bitfield) 4136146906Skan check_bitfield_type_and_width (&type, width, orig_name); 4137146906Skan 413850397Sobrien /* Did array size calculations overflow? */ 413950397Sobrien 414050397Sobrien if (TREE_CODE (type) == ARRAY_TYPE 414190075Sobrien && COMPLETE_TYPE_P (type) 414250397Sobrien && TREE_OVERFLOW (TYPE_SIZE (type))) 414390075Sobrien { 414490075Sobrien error ("size of array `%s' is too large", name); 414590075Sobrien /* If we proceed with the array type as it is, we'll eventually 414690075Sobrien crash in tree_low_cst(). */ 414790075Sobrien type = error_mark_node; 414890075Sobrien } 414950397Sobrien 415018334Speter /* If this is declaring a typedef name, return a TYPE_DECL. */ 415118334Speter 415218334Speter if (specbits & (1 << (int) RID_TYPEDEF)) 415318334Speter { 415418334Speter tree decl; 415518334Speter /* Note that the grammar rejects storage classes 415618334Speter in typenames, fields or parameters */ 415718334Speter if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 415852284Sobrien && type_quals) 415990075Sobrien pedwarn ("ISO C forbids qualified function types"); 416052284Sobrien if (type_quals) 416152284Sobrien type = c_build_qualified_type (type, type_quals); 416218334Speter decl = build_decl (TYPE_DECL, declarator, type); 416318334Speter if ((specbits & (1 << (int) RID_SIGNED)) 416418334Speter || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) 416518334Speter C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 416690075Sobrien decl_attributes (&decl, returned_attrs, 0); 416718334Speter return decl; 416818334Speter } 416918334Speter 417018334Speter /* Detect the case of an array type of unspecified size 417118334Speter which came, as such, direct from a typedef name. 417218334Speter We must copy the type, so that each identifier gets 417318334Speter a distinct type, so that each identifier's size can be 417418334Speter controlled separately by its own initializer. */ 417518334Speter 417618334Speter if (type != 0 && typedef_type != 0 417790075Sobrien && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0 417890075Sobrien && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)) 417918334Speter { 418018334Speter type = build_array_type (TREE_TYPE (type), 0); 418118334Speter if (size_varies) 418218334Speter C_TYPE_VARIABLE_SIZE (type) = 1; 418318334Speter } 418418334Speter 418518334Speter /* If this is a type name (such as, in a cast or sizeof), 418618334Speter compute the type and return it now. */ 418718334Speter 418818334Speter if (decl_context == TYPENAME) 418918334Speter { 419018334Speter /* Note that the grammar rejects storage classes 419118334Speter in typenames, fields or parameters */ 419218334Speter if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 419352284Sobrien && type_quals) 419490075Sobrien pedwarn ("ISO C forbids const or volatile function types"); 419552284Sobrien if (type_quals) 419652284Sobrien type = c_build_qualified_type (type, type_quals); 419790075Sobrien decl_attributes (&type, returned_attrs, 0); 419818334Speter return type; 419918334Speter } 420018334Speter 420118334Speter /* Aside from typedefs and type names (handle above), 420218334Speter `void' at top level (not within pointer) 420318334Speter is allowed only in public variables. 420418334Speter We don't complain about parms either, but that is because 420518334Speter a better error message can be made later. */ 420618334Speter 420790075Sobrien if (VOID_TYPE_P (type) && decl_context != PARM 420818334Speter && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE) 420918334Speter && ((specbits & (1 << (int) RID_EXTERN)) 4210132730Skan || (current_scope == global_scope 421118334Speter && !(specbits 421218334Speter & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER))))))) 421318334Speter { 421418334Speter error ("variable or field `%s' declared void", name); 421518334Speter type = integer_type_node; 421618334Speter } 421718334Speter 421818334Speter /* Now create the decl, which may be a VAR_DECL, a PARM_DECL 421918334Speter or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ 422018334Speter 422118334Speter { 422290075Sobrien tree decl; 422318334Speter 422418334Speter if (decl_context == PARM) 422518334Speter { 422690075Sobrien tree type_as_written; 422790075Sobrien tree promoted_type; 422818334Speter 422918334Speter /* A parameter declared as an array of T is really a pointer to T. 423018334Speter One declared as a function is really a pointer to a function. */ 423118334Speter 423218334Speter if (TREE_CODE (type) == ARRAY_TYPE) 423318334Speter { 423418334Speter /* Transfer const-ness of array into that of type pointed to. */ 423518334Speter type = TREE_TYPE (type); 423652284Sobrien if (type_quals) 423752284Sobrien type = c_build_qualified_type (type, type_quals); 423818334Speter type = build_pointer_type (type); 423952284Sobrien type_quals = TYPE_UNQUALIFIED; 424090075Sobrien if (array_ptr_quals) 424190075Sobrien { 424290075Sobrien tree new_ptr_quals, new_ptr_attrs; 424390075Sobrien int erred = 0; 424490075Sobrien split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs); 424590075Sobrien /* We don't yet implement attributes in this context. */ 424690075Sobrien if (new_ptr_attrs != NULL_TREE) 424790075Sobrien warning ("attributes in parameter array declarator ignored"); 424890075Sobrien 424990075Sobrien constp = 0; 425090075Sobrien volatilep = 0; 425190075Sobrien restrictp = 0; 425290075Sobrien for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals)) 425390075Sobrien { 425490075Sobrien tree qualifier = TREE_VALUE (new_ptr_quals); 425590075Sobrien 425690075Sobrien if (C_IS_RESERVED_WORD (qualifier)) 425790075Sobrien { 425890075Sobrien if (C_RID_CODE (qualifier) == RID_CONST) 425990075Sobrien constp++; 426090075Sobrien else if (C_RID_CODE (qualifier) == RID_VOLATILE) 426190075Sobrien volatilep++; 426290075Sobrien else if (C_RID_CODE (qualifier) == RID_RESTRICT) 426390075Sobrien restrictp++; 426490075Sobrien else 426590075Sobrien erred++; 426690075Sobrien } 426790075Sobrien else 426890075Sobrien erred++; 426990075Sobrien } 427090075Sobrien 427190075Sobrien if (erred) 427290075Sobrien error ("invalid type modifier within array declarator"); 427390075Sobrien 427490075Sobrien type_quals = ((constp ? TYPE_QUAL_CONST : 0) 427590075Sobrien | (restrictp ? TYPE_QUAL_RESTRICT : 0) 427690075Sobrien | (volatilep ? TYPE_QUAL_VOLATILE : 0)); 427790075Sobrien } 427818334Speter size_varies = 0; 427918334Speter } 428018334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 428118334Speter { 428252284Sobrien if (pedantic && type_quals) 428390075Sobrien pedwarn ("ISO C forbids qualified function types"); 428452284Sobrien if (type_quals) 428552284Sobrien type = c_build_qualified_type (type, type_quals); 428618334Speter type = build_pointer_type (type); 428752284Sobrien type_quals = TYPE_UNQUALIFIED; 428818334Speter } 428990075Sobrien else if (type_quals) 429090075Sobrien type = c_build_qualified_type (type, type_quals); 4291132730Skan 429290075Sobrien type_as_written = type; 429318334Speter 429418334Speter decl = build_decl (PARM_DECL, declarator, type); 429518334Speter if (size_varies) 429618334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 429718334Speter 429818334Speter /* Compute the type actually passed in the parmlist, 429918334Speter for the case where there is no prototype. 430018334Speter (For example, shorts and chars are passed as ints.) 430118334Speter When there is a prototype, this is overridden later. */ 430218334Speter 430390075Sobrien if (type == error_mark_node) 430490075Sobrien promoted_type = type; 430590075Sobrien else 4306117421Skan promoted_type = c_type_promotes_to (type); 430718334Speter 430890075Sobrien DECL_ARG_TYPE (decl) = promoted_type; 430918334Speter DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written; 431018334Speter } 431118334Speter else if (decl_context == FIELD) 431218334Speter { 431318334Speter /* Structure field. It may not be a function. */ 431496263Sobrien 431518334Speter if (TREE_CODE (type) == FUNCTION_TYPE) 431618334Speter { 431718334Speter error ("field `%s' declared as a function", name); 431818334Speter type = build_pointer_type (type); 431918334Speter } 432090075Sobrien else if (TREE_CODE (type) != ERROR_MARK 432190075Sobrien && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type)) 432218334Speter { 432318334Speter error ("field `%s' has incomplete type", name); 432418334Speter type = error_mark_node; 432518334Speter } 432618334Speter /* Move type qualifiers down to element of an array. */ 432752284Sobrien if (TREE_CODE (type) == ARRAY_TYPE && type_quals) 4328132730Skan type = build_array_type (c_build_qualified_type (TREE_TYPE (type), 4329132730Skan type_quals), 4330132730Skan TYPE_DOMAIN (type)); 433118334Speter decl = build_decl (FIELD_DECL, declarator, type); 433290075Sobrien DECL_NONADDRESSABLE_P (decl) = bitfield; 433390075Sobrien 433418334Speter if (size_varies) 433518334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 433618334Speter } 433718334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 433818334Speter { 433918334Speter /* Every function declaration is "external" 434018334Speter except for those which are inside a function body 434118334Speter in which `auto' is used. 434218334Speter That is a case not specified by ANSI C, 434318334Speter and we use it for forward declarations for nested functions. */ 434418334Speter int extern_ref = (!(specbits & (1 << (int) RID_AUTO)) 4345132730Skan || current_scope == global_scope); 434618334Speter 434718334Speter if (specbits & (1 << (int) RID_AUTO) 4348132730Skan && (pedantic || current_scope == global_scope)) 434918334Speter pedwarn ("invalid storage class for function `%s'", name); 435018334Speter if (specbits & (1 << (int) RID_REGISTER)) 435118334Speter error ("invalid storage class for function `%s'", name); 4352117421Skan if (specbits & (1 << (int) RID_THREAD)) 4353117421Skan error ("invalid storage class for function `%s'", name); 4354132730Skan /* Function declaration not at file scope. 435518334Speter Storage classes other than `extern' are not allowed 435618334Speter and `extern' makes no difference. */ 4357132730Skan if (current_scope != global_scope 435818334Speter && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE))) 435918334Speter && pedantic) 436018334Speter pedwarn ("invalid storage class for function `%s'", name); 436118334Speter 436218334Speter decl = build_decl (FUNCTION_DECL, declarator, type); 436390075Sobrien decl = build_decl_attribute_variant (decl, decl_attr); 436418334Speter 4365132730Skan DECL_LANG_SPECIFIC (decl) 4366132730Skan = ggc_alloc_cleared (sizeof (struct lang_decl)); 436790075Sobrien 436852284Sobrien if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl)) 436990075Sobrien pedwarn ("ISO C forbids qualified function types"); 437018334Speter 437152284Sobrien /* GNU C interprets a `volatile void' return type to indicate 437252284Sobrien that the function does not return. */ 437352284Sobrien if ((type_quals & TYPE_QUAL_VOLATILE) 437490075Sobrien && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) 437518334Speter warning ("`noreturn' function returns non-void value"); 437618334Speter 437718334Speter if (extern_ref) 437818334Speter DECL_EXTERNAL (decl) = 1; 437918334Speter /* Record absence of global scope for `static' or `auto'. */ 438018334Speter TREE_PUBLIC (decl) 438118334Speter = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO))); 438218334Speter 438396263Sobrien if (defaulted_int) 438496263Sobrien C_FUNCTION_IMPLICIT_INT (decl) = 1; 438596263Sobrien 438618334Speter /* Record presence of `inline', if it is reasonable. */ 438790075Sobrien if (MAIN_NAME_P (declarator)) 438818334Speter { 438990075Sobrien if (inlinep) 439018334Speter warning ("cannot inline function `main'"); 439190075Sobrien } 439290075Sobrien else if (inlinep) 439390075Sobrien { 4394132730Skan /* Record that the function is declared `inline'. */ 439590075Sobrien DECL_DECLARED_INLINE_P (decl) = 1; 439618334Speter 439790075Sobrien /* Do not mark bare declarations as DECL_INLINE. Doing so 439890075Sobrien in the presence of multiple declarations can result in 439990075Sobrien the abstract origin pointing between the declarations, 440090075Sobrien which will confuse dwarf2out. */ 440190075Sobrien if (initialized) 440290075Sobrien { 440390075Sobrien DECL_INLINE (decl) = 1; 440490075Sobrien if (specbits & (1 << (int) RID_EXTERN)) 440590075Sobrien current_extern_inline = 1; 440690075Sobrien } 440718334Speter } 440890075Sobrien /* If -finline-functions, assume it can be inlined. This does 440990075Sobrien two things: let the function be deferred until it is actually 441090075Sobrien needed, and let dwarf2 know that the function is inlinable. */ 441190075Sobrien else if (flag_inline_trees == 2 && initialized) 4412132730Skan DECL_INLINE (decl) = 1; 441318334Speter } 441418334Speter else 441518334Speter { 441618334Speter /* It's a variable. */ 441718334Speter /* An uninitialized decl with `extern' is a reference. */ 441818334Speter int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN)); 441918334Speter 442018334Speter /* Move type qualifiers down to element of an array. */ 442152284Sobrien if (TREE_CODE (type) == ARRAY_TYPE && type_quals) 442218334Speter { 442390075Sobrien int saved_align = TYPE_ALIGN(type); 442452284Sobrien type = build_array_type (c_build_qualified_type (TREE_TYPE (type), 442552284Sobrien type_quals), 442618334Speter TYPE_DOMAIN (type)); 442790075Sobrien TYPE_ALIGN (type) = saved_align; 442818334Speter } 442990075Sobrien else if (type_quals) 443090075Sobrien type = c_build_qualified_type (type, type_quals); 4431132730Skan 4432132730Skan /* It is invalid to create an `extern' declaration for a 4433132730Skan variable if there is a global declaration that is 4434132730Skan `static' and the global declaration is not visible. */ 4435132730Skan if (extern_ref && current_scope != global_scope) 4436132730Skan { 4437132730Skan tree global_decl; 4438132730Skan 4439132730Skan global_decl = identifier_global_value (declarator); 4440132730Skan if (global_decl 4441132730Skan && TREE_CODE (global_decl) == VAR_DECL 4442132730Skan && lookup_name (declarator) != global_decl 4443132730Skan && !TREE_PUBLIC (global_decl)) 4444132730Skan error ("variable previously declared `static' redeclared " 4445132730Skan "`extern'"); 4446132730Skan } 4447132730Skan 444818334Speter decl = build_decl (VAR_DECL, declarator, type); 444918334Speter if (size_varies) 445018334Speter C_DECL_VARIABLE_SIZE (decl) = 1; 445118334Speter 445218334Speter if (inlinep) 4453132730Skan pedwarn ("%Jvariable '%D' declared `inline'", decl, decl); 445418334Speter 445518334Speter DECL_EXTERNAL (decl) = extern_ref; 4456117421Skan 4457132730Skan /* At file scope, the presence of a `static' or `register' storage 445818334Speter class specifier, or the absence of all storage class specifiers 445918334Speter makes this declaration a definition (perhaps tentative). Also, 446018334Speter the absence of both `static' and `register' makes it public. */ 4461132730Skan if (current_scope == global_scope) 446218334Speter { 4463117421Skan TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC) 4464117421Skan | (1 << (int) RID_REGISTER))); 4465117421Skan TREE_STATIC (decl) = !extern_ref; 446618334Speter } 4467132730Skan /* Not at file scope, only `static' makes a static definition. */ 446818334Speter else 446918334Speter { 447018334Speter TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0; 4471117421Skan TREE_PUBLIC (decl) = extern_ref; 447218334Speter } 4473117421Skan 4474117421Skan if (specbits & 1 << (int) RID_THREAD) 4475117421Skan { 4476117421Skan if (targetm.have_tls) 4477117421Skan DECL_THREAD_LOCAL (decl) = 1; 4478117421Skan else 4479117421Skan /* A mere warning is sure to result in improper semantics 4480117421Skan at runtime. Don't bother to allow this to compile. */ 4481117421Skan error ("thread-local storage not supported for this target"); 4482117421Skan } 448318334Speter } 448418334Speter 448518334Speter /* Record `register' declaration for warnings on & 448618334Speter and in case doing stupid register allocation. */ 448718334Speter 448818334Speter if (specbits & (1 << (int) RID_REGISTER)) 448918334Speter DECL_REGISTER (decl) = 1; 449018334Speter 449118334Speter /* Record constancy and volatility. */ 449252284Sobrien c_apply_type_quals_to_decl (type_quals, decl); 449318334Speter 449418334Speter /* If a type has volatile components, it should be stored in memory. 449518334Speter Otherwise, the fact that those components are volatile 449618334Speter will be ignored, and would even crash the compiler. */ 449718334Speter if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))) 4498117421Skan c_mark_addressable (decl); 449918334Speter 4500132730Skan#ifdef ENABLE_CHECKING 4501132730Skan /* This is the earliest point at which we might know the assembler 4502132730Skan name of a variable. Thus, if it's known before this, die horribly. */ 4503132730Skan if (DECL_ASSEMBLER_NAME_SET_P (decl)) 4504132730Skan abort (); 4505132730Skan#endif 4506132730Skan 450790075Sobrien decl_attributes (&decl, returned_attrs, 0); 450818334Speter 450918334Speter return decl; 451018334Speter } 451118334Speter} 451218334Speter 451318334Speter/* Decode the parameter-list info for a function type or function definition. 451418334Speter The argument is the value returned by `get_parm_info' (or made in parse.y 451518334Speter if there is an identifier list instead of a parameter decl list). 451618334Speter These two functions are separate because when a function returns 451718334Speter or receives functions then each is called multiple times but the order 451818334Speter of calls is different. The last call to `grokparms' is always the one 451918334Speter that contains the formal parameter names of a function definition. 452018334Speter 452118334Speter Store in `last_function_parms' a chain of the decls of parms. 452218334Speter Also store in `last_function_parm_tags' a chain of the struct, union, 452318334Speter and enum tags declared among the parms. 452418334Speter 452518334Speter Return a list of arg types to use in the FUNCTION_TYPE for this function. 452618334Speter 452718334Speter FUNCDEF_FLAG is nonzero for a function definition, 0 for 452818334Speter a mere declaration. A nonempty identifier-list gets an error message 452918334Speter when FUNCDEF_FLAG is zero. */ 453018334Speter 453118334Speterstatic tree 4532132730Skangrokparms (tree parms_info, int funcdef_flag) 453318334Speter{ 453418334Speter tree first_parm = TREE_CHAIN (parms_info); 453518334Speter 453618334Speter last_function_parms = TREE_PURPOSE (parms_info); 453718334Speter last_function_parm_tags = TREE_VALUE (parms_info); 4538132730Skan last_function_parm_others = TREE_TYPE (parms_info); 453918334Speter 454018334Speter if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag 454118334Speter && !in_system_header) 454218334Speter warning ("function declaration isn't a prototype"); 454318334Speter 454418334Speter if (first_parm != 0 454518334Speter && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE) 454618334Speter { 454718334Speter if (! funcdef_flag) 454818334Speter pedwarn ("parameter names (without types) in function declaration"); 454918334Speter 455018334Speter last_function_parms = first_parm; 455118334Speter return 0; 455218334Speter } 455318334Speter else 455418334Speter { 455518334Speter tree parm; 455618334Speter tree typelt; 4557132730Skan /* If the arg types are incomplete in a declaration, 455818334Speter they must include undefined tags. 455918334Speter These tags can never be defined in the scope of the declaration, 456018334Speter so the types can never be completed, 456118334Speter and no call can be compiled successfully. */ 456218334Speter 4563132730Skan for (parm = last_function_parms, typelt = first_parm; 4564132730Skan parm; 4565132730Skan parm = TREE_CHAIN (parm)) 4566132730Skan /* Skip over any enumeration constants declared here. */ 4567132730Skan if (TREE_CODE (parm) == PARM_DECL) 4568132730Skan { 4569132730Skan /* Barf if the parameter itself has an incomplete type. */ 4570132730Skan tree type = TREE_VALUE (typelt); 4571132730Skan if (type == error_mark_node) 4572132730Skan continue; 4573132730Skan if (!COMPLETE_TYPE_P (type)) 4574132730Skan { 4575132730Skan if (funcdef_flag && DECL_NAME (parm) != 0) 4576132730Skan error ("parameter `%s' has incomplete type", 4577132730Skan IDENTIFIER_POINTER (DECL_NAME (parm))); 4578132730Skan else 4579132730Skan warning ("parameter has incomplete type"); 4580132730Skan if (funcdef_flag) 4581132730Skan { 4582132730Skan TREE_VALUE (typelt) = error_mark_node; 4583132730Skan TREE_TYPE (parm) = error_mark_node; 4584132730Skan } 4585132730Skan } 4586132730Skan typelt = TREE_CHAIN (typelt); 4587132730Skan } 4588132730Skan 458990075Sobrien return first_parm; 459018334Speter } 459118334Speter} 459218334Speter 459318334Speter/* Return a tree_list node with info on a parameter list just parsed. 4594132730Skan The TREE_PURPOSE is a list of decls of those parms. 459518334Speter The TREE_VALUE is a list of structure, union and enum tags defined. 459618334Speter The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE. 4597132730Skan The TREE_TYPE is a list of non-parameter decls which appeared with the 4598132730Skan parameters. 459918334Speter This tree_list node is later fed to `grokparms'. 460018334Speter 460118334Speter VOID_AT_END nonzero means append `void' to the end of the type-list. 460218334Speter Zero means the parmlist ended with an ellipsis so don't append `void'. */ 460318334Speter 460418334Spetertree 4605132730Skanget_parm_info (int void_at_end) 460618334Speter{ 4607132730Skan tree decl, type, list; 460890075Sobrien tree types = 0; 4609132730Skan tree *last_type = &types; 4610132730Skan tree tags = current_scope->tags; 4611132730Skan tree parms = current_scope->parms; 4612132730Skan tree others = current_scope->names; 4613132730Skan static bool explained_incomplete_types = false; 4614132730Skan bool gave_void_only_once_err = false; 461518334Speter 4616132730Skan /* Just "void" (and no ellipsis) is special. There are really no parms. 4617132730Skan But if the "void" is qualified (by "const" or "volatile"), or has a 4618132730Skan storage class specifier ("register"), then the behavior is undefined; 4619132730Skan issue an error. Typedefs for "void" are OK (see DR#157). */ 462018334Speter if (void_at_end && parms != 0 462118334Speter && TREE_CHAIN (parms) == 0 462290075Sobrien && VOID_TYPE_P (TREE_TYPE (parms)) 4623132730Skan && !DECL_NAME (parms)) 462418334Speter { 4625132730Skan if (TREE_THIS_VOLATILE (parms) 4626132730Skan || TREE_READONLY (parms) 4627132730Skan || DECL_REGISTER (parms)) 4628132730Skan error ("\"void\" as only parameter may not be qualified"); 4629132730Skan 4630132730Skan return tree_cons (0, 0, tree_cons (0, void_type_node, 0)); 463118334Speter } 463218334Speter 4633132730Skan /* Sanity check all of the parameter declarations. */ 4634132730Skan for (decl = parms; decl; decl = TREE_CHAIN (decl)) 463518334Speter { 4636132730Skan if (TREE_CODE (decl) != PARM_DECL) 4637132730Skan abort (); 4638132730Skan if (TREE_ASM_WRITTEN (decl)) 4639132730Skan abort (); 464018334Speter 4641132730Skan /* Since there is a prototype, args are passed in their 4642132730Skan declared types. The back end may override this. */ 4643132730Skan type = TREE_TYPE (decl); 4644132730Skan DECL_ARG_TYPE (decl) = type; 4645132730Skan 4646132730Skan /* Check for (..., void, ...) and issue an error. */ 4647132730Skan if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err) 464818334Speter { 4649132730Skan error ("\"void\" must be the only parameter"); 4650132730Skan gave_void_only_once_err = true; 465118334Speter } 465218334Speter 4653132730Skan type = build_tree_list (0, type); 4654132730Skan *last_type = type; 4655132730Skan last_type = &TREE_CHAIN (type); 465618334Speter } 465718334Speter 4658132730Skan /* Check the list of non-parameter decls for any forward parm decls 4659132730Skan that never got real decls. */ 4660132730Skan for (decl = others; decl; decl = TREE_CHAIN (decl)) 466118334Speter if (TREE_CODE (decl) == PARM_DECL) 466218334Speter { 4663132730Skan if (!TREE_ASM_WRITTEN (decl)) 4664132730Skan abort (); 466518334Speter 4666132730Skan error ("%Jparameter \"%D\" has just a forward declaration", 4667132730Skan decl, decl); 466818334Speter } 466918334Speter 4670132730Skan /* Warn about any struct, union or enum tags defined within this 4671132730Skan list. The scope of such types is limited to this declaration, 4672132730Skan which is rarely if ever desirable (it's impossible to call such 4673132730Skan a function with type-correct arguments). */ 4674132730Skan for (decl = tags; decl; decl = TREE_CHAIN (decl)) 467518334Speter { 4676132730Skan enum tree_code code = TREE_CODE (TREE_VALUE (decl)); 4677132730Skan const char *keyword; 467818334Speter /* An anonymous union parm type is meaningful as a GNU extension. 467918334Speter So don't warn for that. */ 4680132730Skan if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic) 468118334Speter continue; 4682132730Skan 4683132730Skan /* The keyword should not be translated. */ 4684132730Skan switch (code) 468590075Sobrien { 4686132730Skan case RECORD_TYPE: keyword = "struct"; break; 4687132730Skan case UNION_TYPE: keyword = "union"; break; 4688132730Skan case ENUMERAL_TYPE: keyword = "enum"; break; 4689132730Skan default: abort (); 469090075Sobrien } 4691132730Skan 4692132730Skan if (TREE_PURPOSE (decl)) 4693132730Skan /* The first %s will be one of 'struct', 'union', or 'enum'. */ 4694132730Skan warning ("\"%s %s\" declared inside parameter list", 4695132730Skan keyword, IDENTIFIER_POINTER (TREE_PURPOSE (decl))); 4696132730Skan else 4697132730Skan /* The %s will be one of 'struct', 'union', or 'enum'. */ 4698132730Skan warning ("anonymous %s declared inside parameter list", keyword); 4699132730Skan 4700132730Skan if (! explained_incomplete_types) 470118334Speter { 4702132730Skan warning ("its scope is only this definition or declaration," 4703132730Skan " which is probably not what you want"); 4704132730Skan explained_incomplete_types = true; 470518334Speter } 470618334Speter } 4707132730Skan 4708132730Skan 4709132730Skan if (void_at_end) 4710132730Skan { 4711132730Skan type = build_tree_list (0, void_type_node); 4712132730Skan *last_type = type; 4713132730Skan } 4714132730Skan 4715132730Skan list = tree_cons (parms, tags, types); 4716132730Skan TREE_TYPE (list) = others; 4717132730Skan return list; 471818334Speter} 471918334Speter 472018334Speter/* Get the struct, enum or union (CODE says which) with tag NAME. 472118334Speter Define the tag as a forward-reference if it is not defined. */ 472218334Speter 472318334Spetertree 4724132730Skanxref_tag (enum tree_code code, tree name) 472518334Speter{ 472618334Speter /* If a cross reference is requested, look up the type 472718334Speter already defined for this tag and return it. */ 472818334Speter 4729132730Skan tree ref = lookup_tag (code, name, 0); 473090075Sobrien /* If this is the right type of tag, return what we found. 473190075Sobrien (This reference will be shadowed by shadow_tag later if appropriate.) 473290075Sobrien If this is the wrong type of tag, do not return it. If it was the 4733132730Skan wrong type in the same scope, we will have had an error 4734132730Skan message already; if in a different scope and declaring 473590075Sobrien a name, pending_xref_error will give an error message; but if in a 4736132730Skan different scope and not declaring a name, this tag should 473790075Sobrien shadow the previous declaration of a different type of tag, and 473890075Sobrien this would not work properly if we return the reference found. 473990075Sobrien (For example, with "struct foo" in an outer scope, "union foo;" 474090075Sobrien must shadow that tag with a new one of union type.) */ 474190075Sobrien if (ref && TREE_CODE (ref) == code) 474218334Speter return ref; 474318334Speter 474418334Speter /* If no such tag is yet defined, create a forward-reference node 474518334Speter and record it as the "definition". 474618334Speter When a real declaration of this type is found, 474718334Speter the forward-reference will be altered into a real type. */ 474818334Speter 474918334Speter ref = make_node (code); 475018334Speter if (code == ENUMERAL_TYPE) 475118334Speter { 475218334Speter /* Give the type a default layout like unsigned int 475318334Speter to avoid crashing if it does not get defined. */ 475418334Speter TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node); 475518334Speter TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); 475690075Sobrien TYPE_USER_ALIGN (ref) = 0; 475718334Speter TREE_UNSIGNED (ref) = 1; 475818334Speter TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); 475918334Speter TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); 476018334Speter TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); 476118334Speter } 476218334Speter 476318334Speter pushtag (name, ref); 476418334Speter 476518334Speter return ref; 476618334Speter} 476718334Speter 4768132730Skan/* Make sure that the tag NAME is defined *in the current scope* 476918334Speter at least as a forward reference. 477090075Sobrien CODE says which kind of tag NAME ought to be. */ 477118334Speter 477218334Spetertree 4773132730Skanstart_struct (enum tree_code code, tree name) 477418334Speter{ 4775132730Skan /* If there is already a tag defined at this scope 477618334Speter (as a forward reference), just return it. */ 477718334Speter 477890075Sobrien tree ref = 0; 477918334Speter 478018334Speter if (name != 0) 4781132730Skan ref = lookup_tag (code, name, 1); 478218334Speter if (ref && TREE_CODE (ref) == code) 478318334Speter { 478418334Speter if (TYPE_FIELDS (ref)) 478590075Sobrien { 478690075Sobrien if (code == UNION_TYPE) 4787117421Skan error ("redefinition of `union %s'", IDENTIFIER_POINTER (name)); 478890075Sobrien else 4789117421Skan error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name)); 4790132730Skan } 4791117421Skan } 4792117421Skan else 4793117421Skan { 4794117421Skan /* Otherwise create a forward-reference just so the tag is in scope. */ 479518334Speter 4796117421Skan ref = make_node (code); 4797117421Skan pushtag (name, ref); 479818334Speter } 4799132730Skan 480018334Speter C_TYPE_BEING_DEFINED (ref) = 1; 480150397Sobrien TYPE_PACKED (ref) = flag_pack_struct; 480218334Speter return ref; 480318334Speter} 480418334Speter 480518334Speter/* Process the specs, declarator (NULL if omitted) and width (NULL if omitted) 480618334Speter of a structure component, returning a FIELD_DECL node. 4807132730Skan WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node. 480818334Speter 480918334Speter This is done during the parsing of the struct declaration. 481018334Speter The FIELD_DECL nodes are chained together and the lot of them 481118334Speter are ultimately passed to `build_struct' to make the RECORD_TYPE node. */ 481218334Speter 481318334Spetertree 4814132730Skangrokfield (tree declarator, tree declspecs, tree width) 481518334Speter{ 481618334Speter tree value; 481718334Speter 481890075Sobrien if (declarator == NULL_TREE && width == NULL_TREE) 481990075Sobrien { 4820117421Skan /* This is an unnamed decl. 4821117421Skan 4822117421Skan If we have something of the form "union { list } ;" then this 4823117421Skan is the anonymous union extension. Similarly for struct. 4824117421Skan 4825117421Skan If this is something of the form "struct foo;", then 4826117421Skan If MS extensions are enabled, this is handled as an 4827117421Skan anonymous struct. 4828117421Skan Otherwise this is a forward declaration of a structure tag. 4829117421Skan 4830117421Skan If this is something of the form "foo;" and foo is a TYPE_DECL, then 4831117421Skan If MS extensions are enabled and foo names a structure, then 4832117421Skan again this is an anonymous struct. 4833117421Skan Otherwise this is an error. 4834117421Skan 4835132730Skan Oh what a horrid tangled web we weave. I wonder if MS consciously 4836117421Skan took this from Plan 9 or if it was an accident of implementation 4837117421Skan that took root before someone noticed the bug... */ 4838117421Skan 4839102790Skan tree type = TREE_VALUE (declspecs); 4840102790Skan 4841117421Skan if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL) 4842102790Skan type = TREE_TYPE (type); 4843117421Skan if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE) 484490075Sobrien { 4845117421Skan if (flag_ms_extensions) 4846117421Skan ; /* ok */ 4847117421Skan else if (flag_iso) 4848117421Skan goto warn_unnamed_field; 4849117421Skan else if (TYPE_NAME (type) == NULL) 4850117421Skan ; /* ok */ 4851117421Skan else 4852117421Skan goto warn_unnamed_field; 4853117421Skan } 4854117421Skan else 4855117421Skan { 4856117421Skan warn_unnamed_field: 4857117421Skan warning ("declaration does not declare anything"); 485890075Sobrien return NULL_TREE; 485990075Sobrien } 486090075Sobrien } 486118334Speter 4862132730Skan value = grokdeclarator (declarator, declspecs, FIELD, 0, 4863132730Skan width ? &width : NULL); 486418334Speter 486518334Speter finish_decl (value, NULL_TREE, NULL_TREE); 486696263Sobrien DECL_INITIAL (value) = width; 486718334Speter 486818334Speter return value; 486918334Speter} 487018334Speter 4871132730Skan/* Generate an error for any duplicate field names in FIELDLIST. Munge 4872132730Skan the list such that this does not present a problem later. */ 4873122196Skan 4874132730Skanstatic void 4875132730Skandetect_field_duplicates (tree fieldlist) 4876132730Skan{ 4877132730Skan tree x, y; 4878132730Skan int timeout = 10; 4879122196Skan 4880132730Skan /* First, see if there are more than "a few" fields. 4881132730Skan This is trivially true if there are zero or one fields. */ 4882132730Skan if (!fieldlist) 4883132730Skan return; 4884132730Skan x = TREE_CHAIN (fieldlist); 4885132730Skan if (!x) 4886132730Skan return; 4887132730Skan do { 4888132730Skan timeout--; 4889132730Skan x = TREE_CHAIN (x); 4890132730Skan } while (timeout > 0 && x); 4891132730Skan 4892132730Skan /* If there were "few" fields, avoid the overhead of allocating 4893132730Skan a hash table. Instead just do the nested traversal thing. */ 4894132730Skan if (timeout > 0) 4895132730Skan { 4896132730Skan for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x)) 4897132730Skan if (DECL_NAME (x)) 4898132730Skan { 4899132730Skan for (y = fieldlist; y != x; y = TREE_CHAIN (y)) 4900132730Skan if (DECL_NAME (y) == DECL_NAME (x)) 4901132730Skan { 4902132730Skan error ("%Jduplicate member '%D'", x, x); 4903132730Skan DECL_NAME (x) = NULL_TREE; 4904132730Skan } 4905132730Skan } 4906132730Skan } 4907132730Skan else 4908132730Skan { 4909132730Skan htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); 4910132730Skan void **slot; 4911132730Skan 4912132730Skan for (x = fieldlist; x ; x = TREE_CHAIN (x)) 4913132730Skan if ((y = DECL_NAME (x)) != 0) 4914132730Skan { 4915132730Skan slot = htab_find_slot (htab, y, INSERT); 4916132730Skan if (*slot) 4917132730Skan { 4918132730Skan error ("%Jduplicate member '%D'", x, x); 4919132730Skan DECL_NAME (x) = NULL_TREE; 4920132730Skan } 4921132730Skan *slot = y; 4922132730Skan } 4923132730Skan 4924132730Skan htab_delete (htab); 4925132730Skan } 4926122196Skan} 4927132730Skan 492818334Speter/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. 492918334Speter FIELDLIST is a chain of FIELD_DECL nodes for the fields. 493090075Sobrien ATTRIBUTES are attributes to be applied to the structure. */ 493118334Speter 493218334Spetertree 4933132730Skanfinish_struct (tree t, tree fieldlist, tree attributes) 493418334Speter{ 493590075Sobrien tree x; 4936132730Skan int toplevel = global_scope == current_scope; 493790075Sobrien int saw_named_field; 493818334Speter 493918334Speter /* If this type was previously laid out as a forward reference, 494018334Speter make sure we lay it out again. */ 494118334Speter 494218334Speter TYPE_SIZE (t) = 0; 494318334Speter 494490075Sobrien decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 494518334Speter 494618334Speter /* Nameless union parm types are useful as GCC extension. */ 494718334Speter if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic) 494818334Speter /* Otherwise, warn about any struct or union def. in parmlist. */ 494918334Speter if (in_parm_level_p ()) 495018334Speter { 495118334Speter if (pedantic) 495290075Sobrien pedwarn ("%s defined inside parms", 495390075Sobrien TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure")); 4954117421Skan else 495590075Sobrien warning ("%s defined inside parms", 495690075Sobrien TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure")); 495718334Speter } 495818334Speter 495950397Sobrien if (pedantic) 496050397Sobrien { 496150397Sobrien for (x = fieldlist; x; x = TREE_CHAIN (x)) 496250397Sobrien if (DECL_NAME (x) != 0) 496350397Sobrien break; 496418334Speter 496550397Sobrien if (x == 0) 496690075Sobrien pedwarn ("%s has no %s", 496790075Sobrien TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"), 496890075Sobrien fieldlist ? _("named members") : _("members")); 496950397Sobrien } 497050397Sobrien 497196263Sobrien /* Install struct as DECL_CONTEXT of each field decl. 497296263Sobrien Also process specified field sizes,m which is found in the DECL_INITIAL. 497396263Sobrien Store 0 there, except for ": 0" fields (so we can find them 497496263Sobrien and delete them, below). */ 497518334Speter 497690075Sobrien saw_named_field = 0; 497718334Speter for (x = fieldlist; x; x = TREE_CHAIN (x)) 497818334Speter { 497918334Speter DECL_CONTEXT (x) = t; 498018334Speter DECL_PACKED (x) |= TYPE_PACKED (t); 498118334Speter 498218334Speter /* If any field is const, the structure type is pseudo-const. */ 498318334Speter if (TREE_READONLY (x)) 498418334Speter C_TYPE_FIELDS_READONLY (t) = 1; 498518334Speter else 498618334Speter { 498718334Speter /* A field that is pseudo-const makes the structure likewise. */ 498818334Speter tree t1 = TREE_TYPE (x); 498918334Speter while (TREE_CODE (t1) == ARRAY_TYPE) 499018334Speter t1 = TREE_TYPE (t1); 499118334Speter if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE) 499218334Speter && C_TYPE_FIELDS_READONLY (t1)) 499318334Speter C_TYPE_FIELDS_READONLY (t) = 1; 499418334Speter } 499518334Speter 499618334Speter /* Any field that is volatile means variables of this type must be 499718334Speter treated in some ways as volatile. */ 499818334Speter if (TREE_THIS_VOLATILE (x)) 499918334Speter C_TYPE_FIELDS_VOLATILE (t) = 1; 500018334Speter 500118334Speter /* Any field of nominal variable size implies structure is too. */ 500218334Speter if (C_DECL_VARIABLE_SIZE (x)) 500318334Speter C_TYPE_VARIABLE_SIZE (t) = 1; 500418334Speter 500518334Speter /* Detect invalid nested redefinition. */ 500618334Speter if (TREE_TYPE (x) == t) 500718334Speter error ("nested redefinition of `%s'", 500818334Speter IDENTIFIER_POINTER (TYPE_NAME (t))); 500918334Speter 501096263Sobrien if (DECL_INITIAL (x)) 501118334Speter { 5012132730Skan unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1); 5013132730Skan DECL_SIZE (x) = bitsize_int (width); 5014132730Skan DECL_BIT_FIELD (x) = 1; 5015132730Skan SET_DECL_C_BIT_FIELD (x); 501696263Sobrien } 501796263Sobrien 501896263Sobrien DECL_INITIAL (x) = 0; 501996263Sobrien 502090075Sobrien /* Detect flexible array member in an invalid context. */ 502190075Sobrien if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 502290075Sobrien && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 502390075Sobrien && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 502490075Sobrien && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 502518334Speter { 502690075Sobrien if (TREE_CODE (t) == UNION_TYPE) 5027132730Skan { 5028132730Skan error ("%Jflexible array member in union", x); 5029132730Skan TREE_TYPE (x) = error_mark_node; 5030132730Skan } 503190075Sobrien else if (TREE_CHAIN (x) != NULL_TREE) 5032132730Skan { 5033132730Skan error ("%Jflexible array member not at end of struct", x); 5034132730Skan TREE_TYPE (x) = error_mark_node; 5035132730Skan } 503690075Sobrien else if (! saw_named_field) 5037132730Skan { 5038132730Skan error ("%Jflexible array member in otherwise empty struct", x); 5039132730Skan TREE_TYPE (x) = error_mark_node; 5040132730Skan } 504118334Speter } 5042117421Skan 5043132730Skan if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE 5044117421Skan && flexible_array_type_p (TREE_TYPE (x))) 5045132730Skan pedwarn ("%Jinvalid use of structure with flexible array member", x); 5046117421Skan 504790075Sobrien if (DECL_NAME (x)) 504890075Sobrien saw_named_field = 1; 504918334Speter } 505018334Speter 5051132730Skan detect_field_duplicates (fieldlist); 505290075Sobrien 505318334Speter /* Now we have the nearly final fieldlist. Record it, 505418334Speter then lay out the structure or union (including the fields). */ 505518334Speter 505618334Speter TYPE_FIELDS (t) = fieldlist; 505718334Speter 505818334Speter layout_type (t); 505918334Speter 5060132730Skan /* Delete all zero-width bit-fields from the fieldlist. */ 506190075Sobrien { 506290075Sobrien tree *fieldlistp = &fieldlist; 506390075Sobrien while (*fieldlistp) 506490075Sobrien if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)) 506590075Sobrien *fieldlistp = TREE_CHAIN (*fieldlistp); 506690075Sobrien else 506790075Sobrien fieldlistp = &TREE_CHAIN (*fieldlistp); 506890075Sobrien } 506918334Speter 507090075Sobrien /* Now we have the truly final field list. 507190075Sobrien Store it in this type and in the variants. */ 507218334Speter 507318334Speter TYPE_FIELDS (t) = fieldlist; 507418334Speter 5075122196Skan /* If there are lots of fields, sort so we can look through them fast. 5076132730Skan We arbitrarily consider 16 or more elts to be "a lot". */ 5077122196Skan 5078122196Skan { 5079122196Skan int len = 0; 5080122196Skan 5081122196Skan for (x = fieldlist; x; x = TREE_CHAIN (x)) 5082122196Skan { 5083122196Skan if (len > 15 || DECL_NAME (x) == NULL) 5084122196Skan break; 5085122196Skan len += 1; 5086122196Skan } 5087122196Skan 5088122196Skan if (len > 15) 5089122196Skan { 5090122196Skan tree *field_array; 5091132730Skan struct lang_type *space; 5092132730Skan struct sorted_fields_type *space2; 5093132730Skan 5094122196Skan len += list_length (x); 5095132730Skan 5096122196Skan /* Use the same allocation policy here that make_node uses, to 5097122196Skan ensure that this lives as long as the rest of the struct decl. 5098122196Skan All decls in an inline function need to be saved. */ 5099132730Skan 5100132730Skan space = ggc_alloc (sizeof (struct lang_type)); 5101132730Skan space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree)); 5102132730Skan 5103122196Skan len = 0; 5104132730Skan space->s = space2; 5105132730Skan field_array = &space2->elts[0]; 5106122196Skan for (x = fieldlist; x; x = TREE_CHAIN (x)) 5107122196Skan { 5108122196Skan field_array[len++] = x; 5109132730Skan 5110132730Skan /* If there is anonymous struct or union, break out of the loop. */ 5111122196Skan if (DECL_NAME (x) == NULL) 5112122196Skan break; 5113122196Skan } 5114132730Skan /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */ 5115122196Skan if (x == NULL) 5116122196Skan { 5117132730Skan TYPE_LANG_SPECIFIC (t) = space; 5118132730Skan TYPE_LANG_SPECIFIC (t)->s->len = len; 5119132730Skan field_array = TYPE_LANG_SPECIFIC (t)->s->elts; 5120122196Skan qsort (field_array, len, sizeof (tree), field_decl_cmp); 5121122196Skan } 5122122196Skan } 5123122196Skan } 5124132730Skan 512518334Speter for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) 512618334Speter { 512718334Speter TYPE_FIELDS (x) = TYPE_FIELDS (t); 512818334Speter TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t); 512918334Speter TYPE_ALIGN (x) = TYPE_ALIGN (t); 513090075Sobrien TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t); 513118334Speter } 513218334Speter 513318334Speter /* If this was supposed to be a transparent union, but we can't 513418334Speter make it one, warn and turn off the flag. */ 513518334Speter if (TREE_CODE (t) == UNION_TYPE 513618334Speter && TYPE_TRANSPARENT_UNION (t) 5137146906Skan && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))) 513818334Speter { 513918334Speter TYPE_TRANSPARENT_UNION (t) = 0; 514050397Sobrien warning ("union cannot be made transparent"); 514118334Speter } 514218334Speter 514318334Speter /* If this structure or union completes the type of any previous 514418334Speter variable declaration, lay it out and output its rtl. */ 5145132730Skan for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)); 5146132730Skan x; 5147132730Skan x = TREE_CHAIN (x)) 514818334Speter { 5149132730Skan tree decl = TREE_VALUE (x); 5150132730Skan if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) 5151132730Skan layout_array_type (TREE_TYPE (decl)); 5152132730Skan if (TREE_CODE (decl) != TYPE_DECL) 5153132730Skan { 5154132730Skan layout_decl (decl, 0); 5155132730Skan if (c_dialect_objc ()) 5156132730Skan objc_check_decl (decl); 5157132730Skan rest_of_decl_compilation (decl, NULL, toplevel, 0); 5158132730Skan if (! toplevel) 5159132730Skan expand_decl (decl); 516018334Speter } 516118334Speter } 5162132730Skan C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0; 516318334Speter 516418334Speter /* Finish debugging output for this type. */ 516518334Speter rest_of_type_compilation (t, toplevel); 516618334Speter 516718334Speter return t; 516818334Speter} 516918334Speter 517018334Speter/* Lay out the type T, and its element type, and so on. */ 517118334Speter 517218334Speterstatic void 5173132730Skanlayout_array_type (tree t) 517418334Speter{ 517518334Speter if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 517618334Speter layout_array_type (TREE_TYPE (t)); 517718334Speter layout_type (t); 517818334Speter} 517918334Speter 518018334Speter/* Begin compiling the definition of an enumeration type. 518118334Speter NAME is its name (or null if anonymous). 518218334Speter Returns the type object, as yet incomplete. 518318334Speter Also records info about it so that build_enumerator 518418334Speter may be used to declare the individual values as they are read. */ 518518334Speter 518618334Spetertree 5187132730Skanstart_enum (tree name) 518818334Speter{ 518990075Sobrien tree enumtype = 0; 519018334Speter 519118334Speter /* If this is the real definition for a previous forward reference, 519218334Speter fill in the contents in the same object that used to be the 519318334Speter forward reference. */ 519418334Speter 519518334Speter if (name != 0) 5196132730Skan enumtype = lookup_tag (ENUMERAL_TYPE, name, 1); 519718334Speter 519818334Speter if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE) 519918334Speter { 520018334Speter enumtype = make_node (ENUMERAL_TYPE); 520118334Speter pushtag (name, enumtype); 520218334Speter } 520318334Speter 520418334Speter C_TYPE_BEING_DEFINED (enumtype) = 1; 520518334Speter 520618334Speter if (TYPE_VALUES (enumtype) != 0) 520718334Speter { 520818334Speter /* This enum is a named one that has been declared already. */ 520918334Speter error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name)); 521018334Speter 521118334Speter /* Completely replace its old definition. 521218334Speter The old enumerators remain defined, however. */ 521318334Speter TYPE_VALUES (enumtype) = 0; 521418334Speter } 521518334Speter 521618334Speter enum_next_value = integer_zero_node; 521718334Speter enum_overflow = 0; 521818334Speter 521950397Sobrien if (flag_short_enums) 522050397Sobrien TYPE_PACKED (enumtype) = 1; 522150397Sobrien 522218334Speter return enumtype; 522318334Speter} 522418334Speter 522518334Speter/* After processing and defining all the values of an enumeration type, 522618334Speter install their decls in the enumeration type and finish it off. 522718334Speter ENUMTYPE is the type object, VALUES a list of decl-value pairs, 522818334Speter and ATTRIBUTES are the specified attributes. 522918334Speter Returns ENUMTYPE. */ 523018334Speter 523118334Spetertree 5232132730Skanfinish_enum (tree enumtype, tree values, tree attributes) 523318334Speter{ 523490075Sobrien tree pair, tem; 523590075Sobrien tree minnode = 0, maxnode = 0, enum_value_type; 523690075Sobrien int precision, unsign; 5237132730Skan int toplevel = (global_scope == current_scope); 523818334Speter 523918334Speter if (in_parm_level_p ()) 524018334Speter warning ("enum defined inside parms"); 524118334Speter 524290075Sobrien decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 524318334Speter 524418334Speter /* Calculate the maximum value of any enumerator in this type. */ 524518334Speter 524618334Speter if (values == error_mark_node) 524718334Speter minnode = maxnode = integer_zero_node; 524818334Speter else 524990075Sobrien { 525090075Sobrien minnode = maxnode = TREE_VALUE (values); 525190075Sobrien for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair)) 525290075Sobrien { 525390075Sobrien tree value = TREE_VALUE (pair); 525490075Sobrien if (tree_int_cst_lt (maxnode, value)) 525590075Sobrien maxnode = value; 525690075Sobrien if (tree_int_cst_lt (value, minnode)) 525790075Sobrien minnode = value; 525890075Sobrien } 525990075Sobrien } 526018334Speter 526190075Sobrien /* Construct the final type of this enumeration. It is the same 526290075Sobrien as one of the integral types - the narrowest one that fits, except 526390075Sobrien that normally we only go as narrow as int - and signed iff any of 526490075Sobrien the values are negative. */ 526590075Sobrien unsign = (tree_int_cst_sgn (minnode) >= 0); 526690075Sobrien precision = MAX (min_precision (minnode, unsign), 526790075Sobrien min_precision (maxnode, unsign)); 526850397Sobrien if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node)) 526950397Sobrien { 5270117421Skan tree narrowest = c_common_type_for_size (precision, unsign); 527150397Sobrien if (narrowest == 0) 527250397Sobrien { 527350397Sobrien warning ("enumeration values exceed range of largest integer"); 527450397Sobrien narrowest = long_long_integer_type_node; 527550397Sobrien } 527650397Sobrien 527790075Sobrien precision = TYPE_PRECISION (narrowest); 527850397Sobrien } 527918334Speter else 528090075Sobrien precision = TYPE_PRECISION (integer_type_node); 528118334Speter 528290075Sobrien if (precision == TYPE_PRECISION (integer_type_node)) 5283117421Skan enum_value_type = c_common_type_for_size (precision, 0); 528490075Sobrien else 528590075Sobrien enum_value_type = enumtype; 528690075Sobrien 528790075Sobrien TYPE_MIN_VALUE (enumtype) = minnode; 528890075Sobrien TYPE_MAX_VALUE (enumtype) = maxnode; 528990075Sobrien TREE_UNSIGNED (enumtype) = unsign; 529018334Speter TYPE_SIZE (enumtype) = 0; 5291146906Skan 5292146906Skan /* If the precision of the type was specific with an attribute and it 5293146906Skan was too small, give an error. Otherwise, use it. */ 5294146906Skan if (TYPE_PRECISION (enumtype)) 5295146906Skan { 5296146906Skan if (precision > TYPE_PRECISION (enumtype)) 5297146906Skan error ("specified mode too small for enumeral values"); 5298146906Skan } 5299146906Skan else 5300146906Skan TYPE_PRECISION (enumtype) = precision; 5301146906Skan 530218334Speter layout_type (enumtype); 530318334Speter 530418334Speter if (values != error_mark_node) 530518334Speter { 530690075Sobrien /* Change the type of the enumerators to be the enum type. We 530790075Sobrien need to do this irrespective of the size of the enum, for 530890075Sobrien proper type checking. Replace the DECL_INITIALs of the 530990075Sobrien enumerators, and the value slots of the list, with copies 531090075Sobrien that have the enum type; they cannot be modified in place 531190075Sobrien because they may be shared (e.g. integer_zero_node) Finally, 531290075Sobrien change the purpose slots to point to the names of the decls. */ 531318334Speter for (pair = values; pair; pair = TREE_CHAIN (pair)) 531418334Speter { 531590075Sobrien tree enu = TREE_PURPOSE (pair); 531690075Sobrien 531790075Sobrien TREE_TYPE (enu) = enumtype; 531890075Sobrien 531990075Sobrien /* The ISO C Standard mandates enumerators to have type int, 532090075Sobrien even though the underlying type of an enum type is 532190075Sobrien unspecified. Here we convert any enumerators that fit in 532290075Sobrien an int to type int, to avoid promotions to unsigned types 532390075Sobrien when comparing integers with enumerators that fit in the 532490075Sobrien int range. When -pedantic is given, build_enumerator() 532590075Sobrien would have already taken care of those that don't fit. */ 532690075Sobrien if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type)) 532790075Sobrien DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu)); 532890075Sobrien else 532990075Sobrien DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu)); 533090075Sobrien 533190075Sobrien TREE_PURPOSE (pair) = DECL_NAME (enu); 533290075Sobrien TREE_VALUE (pair) = DECL_INITIAL (enu); 533318334Speter } 533418334Speter 533518334Speter TYPE_VALUES (enumtype) = values; 533618334Speter } 533718334Speter 533818334Speter /* Fix up all variant types of this enum type. */ 533918334Speter for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem)) 534018334Speter { 534190075Sobrien if (tem == enumtype) 534290075Sobrien continue; 534318334Speter TYPE_VALUES (tem) = TYPE_VALUES (enumtype); 534418334Speter TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype); 534518334Speter TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype); 534618334Speter TYPE_SIZE (tem) = TYPE_SIZE (enumtype); 534750397Sobrien TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype); 534818334Speter TYPE_MODE (tem) = TYPE_MODE (enumtype); 534918334Speter TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype); 535018334Speter TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype); 535190075Sobrien TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype); 535218334Speter TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype); 535318334Speter } 535418334Speter 535518334Speter /* Finish debugging output for this type. */ 535618334Speter rest_of_type_compilation (enumtype, toplevel); 535718334Speter 535818334Speter return enumtype; 535918334Speter} 536018334Speter 536118334Speter/* Build and install a CONST_DECL for one value of the 536218334Speter current enumeration type (one that was begun with start_enum). 536318334Speter Return a tree-list containing the CONST_DECL and its value. 536418334Speter Assignment of sequential values by default is handled here. */ 536518334Speter 536618334Spetertree 5367132730Skanbuild_enumerator (tree name, tree value) 536818334Speter{ 536990075Sobrien tree decl, type; 537018334Speter 537118334Speter /* Validate and default VALUE. */ 537218334Speter 537318334Speter /* Remove no-op casts from the value. */ 537418334Speter if (value) 537518334Speter STRIP_TYPE_NOPS (value); 537618334Speter 537718334Speter if (value != 0) 537818334Speter { 537918334Speter if (TREE_CODE (value) == INTEGER_CST) 538018334Speter { 538118334Speter value = default_conversion (value); 538218334Speter constant_expression_warning (value); 538318334Speter } 538418334Speter else 538518334Speter { 538618334Speter error ("enumerator value for `%s' not integer constant", 538718334Speter IDENTIFIER_POINTER (name)); 538818334Speter value = 0; 538918334Speter } 539018334Speter } 539118334Speter 539218334Speter /* Default based on previous value. */ 539318334Speter /* It should no longer be possible to have NON_LVALUE_EXPR 539418334Speter in the default. */ 539518334Speter if (value == 0) 539618334Speter { 539718334Speter value = enum_next_value; 539818334Speter if (enum_overflow) 539918334Speter error ("overflow in enumeration values"); 540018334Speter } 540118334Speter 540218334Speter if (pedantic && ! int_fits_type_p (value, integer_type_node)) 540318334Speter { 540490075Sobrien pedwarn ("ISO C restricts enumerator values to range of `int'"); 540590075Sobrien value = convert (integer_type_node, value); 540618334Speter } 540718334Speter 540818334Speter /* Set basis for default for next value. */ 540918334Speter enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0); 541018334Speter enum_overflow = tree_int_cst_lt (enum_next_value, value); 541118334Speter 541218334Speter /* Now create a declaration for the enum value name. */ 541318334Speter 541418334Speter type = TREE_TYPE (value); 5415117421Skan type = c_common_type_for_size (MAX (TYPE_PRECISION (type), 5416117421Skan TYPE_PRECISION (integer_type_node)), 5417117421Skan (TYPE_PRECISION (type) 5418117421Skan >= TYPE_PRECISION (integer_type_node) 5419117421Skan && TREE_UNSIGNED (type))); 542018334Speter 542118334Speter decl = build_decl (CONST_DECL, name, type); 542290075Sobrien DECL_INITIAL (decl) = convert (type, value); 542318334Speter pushdecl (decl); 542418334Speter 542590075Sobrien return tree_cons (decl, value, NULL_TREE); 542618334Speter} 542790075Sobrien 542818334Speter 542918334Speter/* Create the FUNCTION_DECL for a function definition. 543090075Sobrien DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of 543118334Speter the declaration; they describe the function's name and the type it returns, 543218334Speter but twisted together in a fashion that parallels the syntax of C. 543318334Speter 543418334Speter This function creates a binding context for the function body 543518334Speter as well as setting up the FUNCTION_DECL in current_function_decl. 543618334Speter 543718334Speter Returns 1 on success. If the DECLARATOR is not suitable for a function 543818334Speter (it defines a datum instead), we return 0, which tells 543990075Sobrien yyparse to report a parse error. */ 544018334Speter 544118334Speterint 5442132730Skanstart_function (tree declspecs, tree declarator, tree attributes) 544318334Speter{ 544418334Speter tree decl1, old_decl; 544518334Speter tree restype; 544618334Speter int old_immediate_size_expand = immediate_size_expand; 544718334Speter 544850397Sobrien current_function_returns_value = 0; /* Assume, until we see it does. */ 544918334Speter current_function_returns_null = 0; 545096263Sobrien current_function_returns_abnormally = 0; 545118334Speter warn_about_return_type = 0; 545218334Speter current_extern_inline = 0; 5453132730Skan c_in_iteration_stmt = 0; 5454132730Skan c_in_case_stmt = 0; 545518334Speter 545618334Speter /* Don't expand any sizes in the return type of the function. */ 545718334Speter immediate_size_expand = 0; 545818334Speter 5459132730Skan decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL); 546018334Speter 546118334Speter /* If the declarator is not suitable for a function definition, 546218334Speter cause a syntax error. */ 546318334Speter if (decl1 == 0) 546450397Sobrien { 546550397Sobrien immediate_size_expand = old_immediate_size_expand; 546650397Sobrien return 0; 546750397Sobrien } 546818334Speter 546990075Sobrien decl_attributes (&decl1, attributes, 0); 547018334Speter 547190075Sobrien if (DECL_DECLARED_INLINE_P (decl1) 547290075Sobrien && DECL_UNINLINABLE (decl1) 547390075Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1))) 5474132730Skan warning ("%Jinline function '%D' given attribute noinline", decl1, decl1); 547590075Sobrien 547618334Speter announce_function (decl1); 547718334Speter 547890075Sobrien if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1)))) 547918334Speter { 548090075Sobrien error ("return type is an incomplete type"); 548118334Speter /* Make it return void instead. */ 548218334Speter TREE_TYPE (decl1) 548318334Speter = build_function_type (void_type_node, 548418334Speter TYPE_ARG_TYPES (TREE_TYPE (decl1))); 548518334Speter } 548618334Speter 548718334Speter if (warn_about_return_type) 548890075Sobrien pedwarn_c99 ("return type defaults to `int'"); 548918334Speter 549018334Speter /* Save the parm names or decls from this function's declarator 549118334Speter where store_parm_decls will find them. */ 549218334Speter current_function_parms = last_function_parms; 549318334Speter current_function_parm_tags = last_function_parm_tags; 5494132730Skan current_function_parm_others = last_function_parm_others; 549518334Speter 549618334Speter /* Make the init_value nonzero so pushdecl knows this is not tentative. 549718334Speter error_mark_node is replaced below (in poplevel) with the BLOCK. */ 549818334Speter DECL_INITIAL (decl1) = error_mark_node; 549918334Speter 550018334Speter /* If this definition isn't a prototype and we had a prototype declaration 550118334Speter before, copy the arg type info from that prototype. 550218334Speter But not if what we had before was a builtin function. */ 550318334Speter old_decl = lookup_name_current_level (DECL_NAME (decl1)); 550418334Speter if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE 550518334Speter && !DECL_BUILT_IN (old_decl) 550618334Speter && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1))) 550718334Speter == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl)))) 550818334Speter && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0) 550918334Speter { 551018334Speter TREE_TYPE (decl1) = TREE_TYPE (old_decl); 5511132730Skan current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl); 551218334Speter } 551318334Speter 551418334Speter /* Optionally warn of old-fashioned def with no previous prototype. */ 551518334Speter if (warn_strict_prototypes 551618334Speter && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0 5517132730Skan && C_DECL_ISNT_PROTOTYPE (old_decl)) 551818334Speter warning ("function declaration isn't a prototype"); 551918334Speter /* Optionally warn of any global def with no previous prototype. */ 552018334Speter else if (warn_missing_prototypes 552118334Speter && TREE_PUBLIC (decl1) 5522132730Skan && ! MAIN_NAME_P (DECL_NAME (decl1)) 5523132730Skan && C_DECL_ISNT_PROTOTYPE (old_decl)) 5524132730Skan warning ("%Jno previous prototype for '%D'", decl1, decl1); 552518334Speter /* Optionally warn of any def with no previous prototype 552618334Speter if the function has already been used. */ 552718334Speter else if (warn_missing_prototypes 552818334Speter && old_decl != 0 && TREE_USED (old_decl) 552918334Speter && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0) 5530132730Skan warning ("%J'%D' was used with no prototype before its definition", 5531132730Skan decl1, decl1); 553218334Speter /* Optionally warn of any global def with no previous declaration. */ 553318334Speter else if (warn_missing_declarations 553418334Speter && TREE_PUBLIC (decl1) 553518334Speter && old_decl == 0 553690075Sobrien && ! MAIN_NAME_P (DECL_NAME (decl1))) 5537132730Skan warning ("%Jno previous declaration for '%D'", decl1, decl1); 553818334Speter /* Optionally warn of any def with no previous declaration 553918334Speter if the function has already been used. */ 554018334Speter else if (warn_missing_declarations 554118334Speter && old_decl != 0 && TREE_USED (old_decl) 5542132730Skan && C_DECL_IMPLICIT (old_decl)) 5543132730Skan warning ("%J`%D' was used with no declaration before its definition", 5544132730Skan decl1, decl1); 554518334Speter 554618334Speter /* This is a definition, not a reference. 554718334Speter So normally clear DECL_EXTERNAL. 554818334Speter However, `extern inline' acts like a declaration 554918334Speter except for defining how to inline. So set DECL_EXTERNAL in that case. */ 555018334Speter DECL_EXTERNAL (decl1) = current_extern_inline; 555118334Speter 555218334Speter /* This function exists in static storage. 555318334Speter (This does not mean `static' in the C sense!) */ 555418334Speter TREE_STATIC (decl1) = 1; 555518334Speter 555618334Speter /* A nested function is not global. */ 555718334Speter if (current_function_decl != 0) 555818334Speter TREE_PUBLIC (decl1) = 0; 555918334Speter 5560132730Skan#ifdef ENABLE_CHECKING 5561132730Skan /* This is the earliest point at which we might know the assembler 5562132730Skan name of the function. Thus, if it's set before this, die horribly. */ 5563132730Skan if (DECL_ASSEMBLER_NAME_SET_P (decl1)) 5564132730Skan abort (); 5565132730Skan#endif 5566132730Skan 5567132730Skan /* If #pragma weak was used, mark the decl weak now. */ 5568132730Skan if (current_scope == global_scope) 5569132730Skan maybe_apply_pragma_weak (decl1); 5570132730Skan 557190075Sobrien /* Warn for unlikely, improbable, or stupid declarations of `main'. */ 557290075Sobrien if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1))) 557350397Sobrien { 557450397Sobrien tree args; 557550397Sobrien int argct = 0; 557650397Sobrien 557750397Sobrien if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1))) 557890075Sobrien != integer_type_node) 5579132730Skan pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1); 558050397Sobrien 558150397Sobrien for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args; 558250397Sobrien args = TREE_CHAIN (args)) 558350397Sobrien { 558450397Sobrien tree type = args ? TREE_VALUE (args) : 0; 558550397Sobrien 558650397Sobrien if (type == void_type_node) 558750397Sobrien break; 558850397Sobrien 558950397Sobrien ++argct; 559050397Sobrien switch (argct) 559150397Sobrien { 559250397Sobrien case 1: 559350397Sobrien if (TYPE_MAIN_VARIANT (type) != integer_type_node) 5594132730Skan pedwarn ("%Jfirst argument of '%D' should be `int'", 5595132730Skan decl1, decl1); 559650397Sobrien break; 559750397Sobrien 559850397Sobrien case 2: 559950397Sobrien if (TREE_CODE (type) != POINTER_TYPE 560050397Sobrien || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 560150397Sobrien || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 560250397Sobrien != char_type_node)) 5603132730Skan pedwarn ("%Jsecond argument of '%D' should be 'char **'", 5604132730Skan decl1, decl1); 560550397Sobrien break; 560650397Sobrien 560750397Sobrien case 3: 560850397Sobrien if (TREE_CODE (type) != POINTER_TYPE 560950397Sobrien || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 561050397Sobrien || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 561150397Sobrien != char_type_node)) 5612132730Skan pedwarn ("%Jthird argument of '%D' should probably be " 5613132730Skan "'char **'", decl1, decl1); 561450397Sobrien break; 561550397Sobrien } 561650397Sobrien } 561750397Sobrien 561850397Sobrien /* It is intentional that this message does not mention the third 561990075Sobrien argument because it's only mentioned in an appendix of the 562090075Sobrien standard. */ 562150397Sobrien if (argct > 0 && (argct < 2 || argct > 3)) 5622132730Skan pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1); 562350397Sobrien 562450397Sobrien if (! TREE_PUBLIC (decl1)) 5625132730Skan pedwarn ("%J'%D' is normally a non-static function", decl1, decl1); 562650397Sobrien } 562750397Sobrien 562818334Speter /* Record the decl so that the function name is defined. 562918334Speter If we already have a decl for this name, and it is a FUNCTION_DECL, 563018334Speter use the old decl. */ 563118334Speter 563218334Speter current_function_decl = pushdecl (decl1); 563318334Speter 563418334Speter pushlevel (0); 5635132730Skan declare_parm_level (); 563618334Speter 563790075Sobrien make_decl_rtl (current_function_decl, NULL); 563818334Speter 563918334Speter restype = TREE_TYPE (TREE_TYPE (current_function_decl)); 564018334Speter /* Promote the value to int before returning it. */ 564190075Sobrien if (c_promoting_integer_type_p (restype)) 564218334Speter { 5643117421Skan /* It retains unsignedness if not really getting wider. */ 564418334Speter if (TREE_UNSIGNED (restype) 5645117421Skan && (TYPE_PRECISION (restype) 5646117421Skan == TYPE_PRECISION (integer_type_node))) 564718334Speter restype = unsigned_type_node; 564818334Speter else 564918334Speter restype = integer_type_node; 565018334Speter } 565118334Speter DECL_RESULT (current_function_decl) 565218334Speter = build_decl (RESULT_DECL, NULL_TREE, restype); 565318334Speter 565418334Speter /* If this fcn was already referenced via a block-scope `extern' decl 565518334Speter (or an implicit decl), propagate certain information about the usage. */ 565618334Speter if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl))) 565718334Speter TREE_ADDRESSABLE (current_function_decl) = 1; 565818334Speter 565918334Speter immediate_size_expand = old_immediate_size_expand; 566018334Speter 566190075Sobrien start_fname_decls (); 5662132730Skan 566318334Speter return 1; 566418334Speter} 566518334Speter 5666132730Skan/* Subroutine of store_parm_decls which handles new-style function 5667132730Skan definitions (prototype format). The parms already have decls, so we 5668132730Skan need only record them as in effect and complain if any redundant 5669132730Skan old-style parm decls were written. */ 5670132730Skanstatic void 5671132730Skanstore_parm_decls_newstyle (void) 567218334Speter{ 5673132730Skan tree decl, last; 567490075Sobrien tree fndecl = current_function_decl; 5675132730Skan tree parms = current_function_parms; 5676132730Skan tree tags = current_function_parm_tags; 5677132730Skan tree others = current_function_parm_others; 567818334Speter 5679132730Skan if (current_scope->parms || current_scope->names || current_scope->tags) 5680132730Skan { 5681132730Skan error ("%Jold-style parameter declarations in prototyped " 5682132730Skan "function definition", fndecl); 568318334Speter 5684132730Skan /* Get rid of the old-style declarations. */ 5685132730Skan poplevel (0, 0, 0); 5686132730Skan pushlevel (0); 5687132730Skan } 568818334Speter 5689132730Skan /* Now make all the parameter declarations visible in the function body. 5690132730Skan We can bypass most of the grunt work of pushdecl. */ 5691132730Skan for (last = 0, decl = parms; decl; last = decl, decl = TREE_CHAIN (decl)) 5692132730Skan { 5693132730Skan DECL_CONTEXT (decl) = current_function_decl; 5694132730Skan if (DECL_NAME (decl) == 0) 5695132730Skan error ("%Jparameter name omitted", decl); 5696132730Skan else 5697132730Skan { 5698132730Skan if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl))) 5699132730Skan current_scope->shadowed 5700132730Skan = tree_cons (DECL_NAME (decl), 5701132730Skan IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)), 5702132730Skan current_scope->shadowed); 5703132730Skan IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl; 5704132730Skan } 5705132730Skan } 5706132730Skan current_scope->parms = parms; 5707132730Skan current_scope->parms_last = last; 570818334Speter 5709132730Skan /* Record the parameter list in the function declaration. */ 5710132730Skan DECL_ARGUMENTS (fndecl) = parms; 571118334Speter 5712132730Skan /* Now make all the ancillary declarations visible, likewise. */ 5713132730Skan for (last = 0, decl = others; decl; last = decl, decl = TREE_CHAIN (decl)) 5714132730Skan { 5715132730Skan DECL_CONTEXT (decl) = current_function_decl; 5716132730Skan if (DECL_NAME (decl) 5717132730Skan && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node) 5718132730Skan { 5719132730Skan if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl))) 5720132730Skan current_scope->shadowed 5721132730Skan = tree_cons (DECL_NAME (decl), 5722132730Skan IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)), 5723132730Skan current_scope->shadowed); 5724132730Skan IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl; 5725132730Skan } 5726132730Skan } 5727132730Skan current_scope->names = others; 5728132730Skan current_scope->names_last = last; 572990075Sobrien 5730132730Skan /* And all the tag declarations. */ 5731132730Skan for (decl = tags; decl; decl = TREE_CHAIN (decl)) 5732132730Skan if (TREE_PURPOSE (decl)) 5733132730Skan { 5734132730Skan if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl))) 5735132730Skan current_scope->shadowed_tags 5736132730Skan = tree_cons (TREE_PURPOSE (decl), 5737132730Skan IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)), 5738132730Skan current_scope->shadowed_tags); 5739132730Skan IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl); 5740132730Skan } 5741132730Skan current_scope->tags = tags; 5742132730Skan} 574318334Speter 5744132730Skan/* Subroutine of store_parm_decls which handles old-style function 5745132730Skan definitions (separate parameter list and declarations). */ 574690075Sobrien 5747132730Skanstatic void 5748132730Skanstore_parm_decls_oldstyle (void) 5749132730Skan{ 5750132730Skan tree parm, decl, last; 5751132730Skan tree fndecl = current_function_decl; 575290075Sobrien 5753132730Skan /* This is the identifier list from the function declarator. */ 5754132730Skan tree parmids = current_function_parms; 575518334Speter 5756132730Skan /* We use DECL_WEAK as a flag to show which parameters have been 5757132730Skan seen already, since it is not used on PARM_DECL. */ 5758132730Skan#ifdef ENABLE_CHECKING 5759132730Skan for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm)) 5760132730Skan if (DECL_WEAK (parm)) 5761132730Skan abort (); 5762132730Skan#endif 576318334Speter 5764132730Skan /* Match each formal parameter name with its declaration. Save each 5765132730Skan decl in the appropriate TREE_PURPOSE slot of the parmids chain. */ 5766132730Skan for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 5767132730Skan { 5768132730Skan if (TREE_VALUE (parm) == 0) 576918334Speter { 5770132730Skan error ("%Jparameter name missing from parameter list", fndecl); 5771132730Skan TREE_PURPOSE (parm) = 0; 5772132730Skan continue; 577318334Speter } 577418334Speter 5775132730Skan decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm)); 5776132730Skan if (decl && DECL_CONTEXT (decl) == fndecl) 577718334Speter { 5778132730Skan /* If we got something other than a PARM_DECL it is an error. */ 5779132730Skan if (TREE_CODE (decl) != PARM_DECL) 5780132730Skan error ("%J\"%D\" declared as a non-parameter", decl, decl); 5781132730Skan /* If the declaration is already marked, we have a duplicate 5782132730Skan name. Complain and ignore the duplicate. */ 5783132730Skan else if (DECL_WEAK (decl)) 578418334Speter { 5785132730Skan error ("%Jmultiple parameters named \"%D\"", decl, decl); 5786132730Skan TREE_PURPOSE (parm) = 0; 5787132730Skan continue; 578818334Speter } 5789132730Skan /* If the declaration says "void", complain and turn it into 5790132730Skan an int. */ 5791132730Skan else if (VOID_TYPE_P (TREE_TYPE (decl))) 579218334Speter { 5793132730Skan error ("%Jparameter \"%D\" declared void", decl, decl); 5794132730Skan TREE_TYPE (decl) = integer_type_node; 5795132730Skan DECL_ARG_TYPE (decl) = integer_type_node; 5796132730Skan layout_decl (decl, 0); 579718334Speter } 579818334Speter } 5799132730Skan /* If no declaration found, default to int. */ 5800132730Skan else 580118334Speter { 5802132730Skan decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node); 5803132730Skan DECL_ARG_TYPE (decl) = TREE_TYPE (decl); 5804132730Skan DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl); 5805132730Skan pushdecl (decl); 580618334Speter 5807132730Skan if (flag_isoc99) 5808132730Skan pedwarn ("%Jtype of \"%D\" defaults to \"int\"", decl, decl); 5809132730Skan else if (extra_warnings) 5810132730Skan warning ("%Jtype of \"%D\" defaults to \"int\"", decl, decl); 581118334Speter } 581218334Speter 5813132730Skan TREE_PURPOSE (parm) = decl; 5814132730Skan DECL_WEAK (decl) = 1; 581518334Speter } 581618334Speter 5817132730Skan /* Now examine the parms chain for incomplete declarations 5818132730Skan and declarations with no corresponding names. */ 581918334Speter 5820132730Skan for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm)) 5821132730Skan { 5822132730Skan if (!COMPLETE_TYPE_P (TREE_TYPE (parm))) 5823132730Skan { 5824132730Skan error ("%Jparameter \"%D\" has incomplete type", parm, parm); 5825132730Skan TREE_TYPE (parm) = error_mark_node; 5826132730Skan } 582718334Speter 5828132730Skan if (! DECL_WEAK (parm)) 582918334Speter { 5830132730Skan error ("%Jdeclaration for parameter \"%D\" but no such parameter", 5831132730Skan parm, parm); 583218334Speter 5833132730Skan /* Pretend the parameter was not missing. 5834132730Skan This gets us to a standard state and minimizes 5835132730Skan further error messages. */ 5836132730Skan parmids = chainon (parmids, tree_cons (parm, 0, 0)); 5837132730Skan } 5838132730Skan } 583918334Speter 5840132730Skan /* Chain the declarations together in the order of the list of 5841132730Skan names. Store that chain in the function decl, replacing the 5842132730Skan list of names. Update the current scope to match. */ 5843132730Skan DECL_ARGUMENTS (fndecl) = 0; 584418334Speter 5845132730Skan for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 5846132730Skan if (TREE_PURPOSE (parm)) 5847132730Skan break; 5848132730Skan if (parm && TREE_PURPOSE (parm)) 5849132730Skan { 5850132730Skan last = TREE_PURPOSE (parm); 5851132730Skan DECL_ARGUMENTS (fndecl) = last; 5852132730Skan current_scope->parms = last; 5853132730Skan DECL_WEAK (last) = 0; 585418334Speter 5855132730Skan for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm)) 5856132730Skan if (TREE_PURPOSE (parm)) 5857132730Skan { 5858132730Skan TREE_CHAIN (last) = TREE_PURPOSE (parm); 5859132730Skan last = TREE_PURPOSE (parm); 5860132730Skan DECL_WEAK (last) = 0; 5861132730Skan } 5862132730Skan current_scope->parms_last = last; 5863132730Skan TREE_CHAIN (last) = 0; 5864132730Skan } 586518334Speter 5866132730Skan /* If there was a previous prototype, 5867132730Skan set the DECL_ARG_TYPE of each argument according to 5868132730Skan the type previously specified, and report any mismatches. */ 586918334Speter 5870132730Skan if (TYPE_ARG_TYPES (TREE_TYPE (fndecl))) 5871132730Skan { 5872132730Skan tree type; 5873132730Skan for (parm = DECL_ARGUMENTS (fndecl), 5874132730Skan type = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); 5875132730Skan parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) 5876132730Skan != void_type_node)); 5877132730Skan parm = TREE_CHAIN (parm), type = TREE_CHAIN (type)) 587818334Speter { 5879132730Skan if (parm == 0 || type == 0 5880132730Skan || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node) 588118334Speter { 5882132730Skan error ("number of arguments doesn't match prototype"); 5883132730Skan error ("%Hprototype declaration", 5884132730Skan ¤t_function_prototype_locus); 5885132730Skan break; 588618334Speter } 5887132730Skan /* Type for passing arg must be consistent with that 5888132730Skan declared for the arg. ISO C says we take the unqualified 5889132730Skan type for parameters declared with qualified type. */ 5890132730Skan if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)), 5891132730Skan TYPE_MAIN_VARIANT (TREE_VALUE (type)), 5892132730Skan COMPARE_STRICT)) 589318334Speter { 5894132730Skan if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) 5895132730Skan == TYPE_MAIN_VARIANT (TREE_VALUE (type))) 589618334Speter { 5897132730Skan /* Adjust argument to match prototype. E.g. a previous 5898132730Skan `int foo(float);' prototype causes 5899132730Skan `int foo(x) float x; {...}' to be treated like 5900132730Skan `int foo(float x) {...}'. This is particularly 5901132730Skan useful for argument types like uid_t. */ 5902132730Skan DECL_ARG_TYPE (parm) = TREE_TYPE (parm); 590390075Sobrien 5904132730Skan if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl)) 5905132730Skan && INTEGRAL_TYPE_P (TREE_TYPE (parm)) 5906132730Skan && TYPE_PRECISION (TREE_TYPE (parm)) 5907132730Skan < TYPE_PRECISION (integer_type_node)) 5908132730Skan DECL_ARG_TYPE (parm) = integer_type_node; 590990075Sobrien 5910132730Skan if (pedantic) 591118334Speter { 5912132730Skan pedwarn ("promoted argument \"%D\" " 5913132730Skan "doesn't match prototype", parm); 5914132730Skan pedwarn ("%Hprototype declaration", 5915132730Skan ¤t_function_prototype_locus); 591618334Speter } 591718334Speter } 5918132730Skan else 5919132730Skan { 5920132730Skan error ("argument \"%D\" doesn't match prototype", parm); 5921132730Skan error ("%Hprototype declaration", 5922132730Skan ¤t_function_prototype_locus); 5923132730Skan } 592418334Speter } 592518334Speter } 5926132730Skan TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0; 5927132730Skan } 592818334Speter 5929132730Skan /* Otherwise, create a prototype that would match. */ 593018334Speter 5931132730Skan else 5932132730Skan { 5933132730Skan tree actual = 0, last = 0, type; 5934132730Skan 5935132730Skan for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm)) 593618334Speter { 5937132730Skan type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE); 593818334Speter if (last) 593918334Speter TREE_CHAIN (last) = type; 594018334Speter else 594118334Speter actual = type; 5942132730Skan last = type; 5943132730Skan } 5944132730Skan type = tree_cons (NULL_TREE, void_type_node, NULL_TREE); 5945132730Skan if (last) 5946132730Skan TREE_CHAIN (last) = type; 5947132730Skan else 5948132730Skan actual = type; 594918334Speter 5950132730Skan /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES 5951132730Skan of the type of this function, but we need to avoid having this 5952132730Skan affect the types of other similarly-typed functions, so we must 5953132730Skan first force the generation of an identical (but separate) type 5954132730Skan node for the relevant function type. The new node we create 5955132730Skan will be a variant of the main variant of the original function 5956132730Skan type. */ 595718334Speter 5958132730Skan TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl)); 595918334Speter 5960132730Skan TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual; 5961132730Skan } 5962132730Skan} 596318334Speter 5964132730Skan/* Store the parameter declarations into the current function declaration. 5965132730Skan This is called after parsing the parameter declarations, before 5966132730Skan digesting the body of the function. 596718334Speter 5968132730Skan For an old-style definition, construct a prototype out of the old-style 5969132730Skan parameter declarations and inject it into the function's type. */ 597018334Speter 5971132730Skanvoid 5972132730Skanstore_parm_decls (void) 5973132730Skan{ 5974132730Skan tree fndecl = current_function_decl; 597518334Speter 5976132730Skan /* The function containing FNDECL, if any. */ 5977132730Skan tree context = decl_function_context (fndecl); 597818334Speter 5979132730Skan /* True if this definition is written with a prototype. */ 5980132730Skan bool prototype = (current_function_parms 5981132730Skan && TREE_CODE (current_function_parms) != TREE_LIST); 598218334Speter 5983132730Skan if (prototype) 5984132730Skan store_parm_decls_newstyle (); 5985132730Skan else 5986132730Skan store_parm_decls_oldstyle (); 5987132730Skan 5988132730Skan /* The next call to pushlevel will be a function body. */ 5989132730Skan 5990132730Skan next_is_function_body = true; 5991132730Skan 599218334Speter /* Write a record describing this function definition to the prototypes 599318334Speter file (if requested). */ 599418334Speter 599518334Speter gen_aux_info_record (fndecl, 1, 0, prototype); 599618334Speter 599718334Speter /* Initialize the RTL code for the function. */ 5998132730Skan allocate_struct_function (fndecl); 599918334Speter 600090075Sobrien /* Begin the statement tree for this function. */ 6001132730Skan begin_stmt_tree (&DECL_SAVED_TREE (fndecl)); 600218334Speter 600390075Sobrien /* If this is a nested function, save away the sizes of any 600490075Sobrien variable-size types so that we can expand them when generating 600590075Sobrien RTL. */ 600690075Sobrien if (context) 600718334Speter { 600890075Sobrien tree t; 600918334Speter 6010132730Skan DECL_LANG_SPECIFIC (fndecl)->pending_sizes 601190075Sobrien = nreverse (get_pending_sizes ()); 601290075Sobrien for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes; 601390075Sobrien t; 601490075Sobrien t = TREE_CHAIN (t)) 601590075Sobrien SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context; 601618334Speter } 601718334Speter 601890075Sobrien /* This function is being processed in whole-function mode. */ 601990075Sobrien cfun->x_whole_function_mode_p = 1; 602018334Speter 602190075Sobrien /* Even though we're inside a function body, we still don't want to 602290075Sobrien call expand_expr to calculate the size of a variable-sized array. 602390075Sobrien We haven't necessarily assigned RTL to all variables yet, so it's 602490075Sobrien not safe to try to expand expressions involving them. */ 602590075Sobrien immediate_size_expand = 0; 602690075Sobrien cfun->x_dont_save_pending_sizes_p = 1; 602718334Speter} 602818334Speter 602918334Speter/* Finish up a function declaration and compile that function 603018334Speter all the way to assembler language output. The free the storage 603118334Speter for the function definition. 603218334Speter 6033132730Skan This is called after parsing the body of the function definition. */ 603418334Speter 603518334Spetervoid 6036132730Skanfinish_function (void) 603718334Speter{ 603890075Sobrien tree fndecl = current_function_decl; 603918334Speter 6040132730Skan /* When a function declaration is totally empty, e.g. 6041132730Skan void foo(void) { } 6042132730Skan (the argument list is irrelevant) the compstmt rule will not 6043132730Skan bother calling pushlevel/poplevel, which means we get here with 6044132730Skan the scope stack out of sync. Detect this situation by noticing 6045132730Skan that current_scope is still as store_parm_decls left it, and do 6046132730Skan a dummy push/pop to get back to consistency. 6047132730Skan Note that the call to pushlevel does not actually push another 6048132730Skan scope - see there for details. */ 604918334Speter 6050132730Skan if (current_scope->parm_flag && next_is_function_body) 6051132730Skan { 6052132730Skan pushlevel (0); 6053132730Skan poplevel (0, 0, 0); 6054132730Skan } 605518334Speter 6056132730Skan if (TREE_CODE (fndecl) == FUNCTION_DECL 6057132730Skan && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))) 6058132730Skan { 6059132730Skan tree args = DECL_ARGUMENTS (fndecl); 6060132730Skan for (; args; args = TREE_CHAIN (args)) 6061132730Skan { 6062132730Skan tree type = TREE_TYPE (args); 6063132730Skan if (INTEGRAL_TYPE_P (type) 6064132730Skan && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 6065132730Skan DECL_ARG_TYPE (args) = integer_type_node; 6066132730Skan } 6067132730Skan } 6068132730Skan 6069132730Skan if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) 6070132730Skan BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; 6071132730Skan 607218334Speter /* Must mark the RESULT_DECL as being in this function. */ 607318334Speter 6074132730Skan if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node) 6075132730Skan DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; 607618334Speter 607790075Sobrien if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted) 607818334Speter { 607918334Speter if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))) 608018334Speter != integer_type_node) 608150397Sobrien { 608252284Sobrien /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned. 608390075Sobrien If warn_main is -1 (-Wno-main) we don't want to be warned. */ 6084132730Skan if (!warn_main) 6085132730Skan pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl); 608650397Sobrien } 608718334Speter else 608818334Speter { 608950397Sobrien#ifdef DEFAULT_MAIN_RETURN 609018334Speter /* Make it so that `main' always returns success by default. */ 609118334Speter DEFAULT_MAIN_RETURN; 609290075Sobrien#else 609390075Sobrien if (flag_isoc99) 609490075Sobrien c_expand_return (integer_zero_node); 609550397Sobrien#endif 609618334Speter } 609718334Speter } 6098132730Skan 609990075Sobrien finish_fname_decls (); 610018334Speter 610190075Sobrien /* Tie off the statement tree for this function. */ 610290075Sobrien finish_stmt_tree (&DECL_SAVED_TREE (fndecl)); 610396263Sobrien 610496263Sobrien /* Complain if there's just no return statement. */ 610596263Sobrien if (warn_return_type 610696263Sobrien && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE 610796263Sobrien && !current_function_returns_value && !current_function_returns_null 610896263Sobrien /* Don't complain if we abort. */ 610996263Sobrien && !current_function_returns_abnormally 611096263Sobrien /* Don't warn for main(). */ 611196263Sobrien && !MAIN_NAME_P (DECL_NAME (fndecl)) 611296263Sobrien /* Or if they didn't actually specify a return type. */ 611396263Sobrien && !C_FUNCTION_IMPLICIT_INT (fndecl) 611496263Sobrien /* Normally, with -Wreturn-type, flow will complain. Unless we're an 611596263Sobrien inline function, as we might never be compiled separately. */ 611696263Sobrien && DECL_INLINE (fndecl)) 611796263Sobrien warning ("no return statement in function returning non-void"); 611896263Sobrien 6119132730Skan /* With just -Wextra, complain only if function returns both with 6120122196Skan and without a value. */ 6121122196Skan if (extra_warnings 6122122196Skan && current_function_returns_value 6123122196Skan && current_function_returns_null) 6124122196Skan warning ("this function may return with or without a value"); 6125122196Skan 6126132730Skan /* We're leaving the context of this function, so zap cfun. It's still in 6127132730Skan DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */ 612890075Sobrien cfun = NULL; 612990075Sobrien 6130132730Skan /* ??? Objc emits functions after finalizing the compilation unit. 6131132730Skan This should be cleaned up later and this conditional removed. */ 6132132730Skan if (!cgraph_global_info_ready) 6133132730Skan cgraph_finalize_function (fndecl, false); 6134132730Skan else 6135132730Skan c_expand_body (fndecl); 6136132730Skan current_function_decl = NULL; 613790075Sobrien} 613890075Sobrien 613990075Sobrien/* Generate the RTL for a deferred function FNDECL. */ 614090075Sobrien 614190075Sobrienvoid 6142132730Skanc_expand_deferred_function (tree fndecl) 614390075Sobrien{ 614490075Sobrien /* DECL_INLINE or DECL_RESULT might got cleared after the inline 614590075Sobrien function was deferred, e.g. in duplicate_decls. */ 614690075Sobrien if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl)) 614790075Sobrien { 6148132730Skan if (flag_inline_trees) 6149132730Skan { 6150132730Skan timevar_push (TV_INTEGRATION); 6151132730Skan optimize_inline_calls (fndecl); 6152132730Skan timevar_pop (TV_INTEGRATION); 6153132730Skan } 6154132730Skan c_expand_body (fndecl); 615590075Sobrien current_function_decl = NULL; 615690075Sobrien } 615790075Sobrien} 615890075Sobrien 6159117421Skan/* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero, 616090075Sobrien then we are already in the process of generating RTL for another 6161132730Skan function. */ 616290075Sobrien 616390075Sobrienstatic void 6164132730Skanc_expand_body_1 (tree fndecl, int nested_p) 616590075Sobrien{ 616690075Sobrien if (nested_p) 616790075Sobrien { 616890075Sobrien /* Make sure that we will evaluate variable-sized types involved 616990075Sobrien in our function's type. */ 617090075Sobrien expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes); 6171132730Skan 617290075Sobrien /* Squirrel away our current state. */ 617390075Sobrien push_function_context (); 617490075Sobrien } 6175132730Skan 6176132730Skan tree_rest_of_compilation (fndecl, nested_p); 617790075Sobrien 617890075Sobrien if (nested_p) 6179132730Skan /* Return to the enclosing function. */ 6180132730Skan pop_function_context (); 618118334Speter 618218334Speter if (DECL_STATIC_CONSTRUCTOR (fndecl)) 618318334Speter { 618490075Sobrien if (targetm.have_ctors_dtors) 618590075Sobrien (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0), 618690075Sobrien DEFAULT_INIT_PRIORITY); 618718334Speter else 618890075Sobrien static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors); 618918334Speter } 619090075Sobrien 619118334Speter if (DECL_STATIC_DESTRUCTOR (fndecl)) 619218334Speter { 619390075Sobrien if (targetm.have_ctors_dtors) 619490075Sobrien (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0), 619590075Sobrien DEFAULT_INIT_PRIORITY); 619618334Speter else 619790075Sobrien static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors); 619818334Speter } 6199132730Skan} 620018334Speter 6201132730Skan/* Like c_expand_body_1 but only for unnested functions. */ 6202117421Skan 6203132730Skanvoid 6204132730Skanc_expand_body (tree fndecl) 6205132730Skan{ 6206132730Skan 6207132730Skan if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) 6208132730Skan c_expand_body_1 (fndecl, 0); 620990075Sobrien} 621090075Sobrien 621190075Sobrien/* Check the declarations given in a for-loop for satisfying the C99 621290075Sobrien constraints. */ 621390075Sobrienvoid 6214132730Skancheck_for_loop_decls (void) 621590075Sobrien{ 621690075Sobrien tree t; 621790075Sobrien 621890075Sobrien if (!flag_isoc99) 621918334Speter { 622090075Sobrien /* If we get here, declarations have been used in a for loop without 622190075Sobrien the C99 for loop scope. This doesn't make much sense, so don't 622290075Sobrien allow it. */ 6223132730Skan error ("'for' loop initial declaration used outside C99 mode"); 622490075Sobrien return; 622518334Speter } 622690075Sobrien /* C99 subclause 6.8.5 paragraph 3: 622790075Sobrien 622890075Sobrien [#3] The declaration part of a for statement shall only 622990075Sobrien declare identifiers for objects having storage class auto or 623090075Sobrien register. 623190075Sobrien 623290075Sobrien It isn't clear whether, in this sentence, "identifiers" binds to 623390075Sobrien "shall only declare" or to "objects" - that is, whether all identifiers 623490075Sobrien declared must be identifiers for objects, or whether the restriction 623590075Sobrien only applies to those that are. (A question on this in comp.std.c 623690075Sobrien in November 2000 received no answer.) We implement the strictest 623790075Sobrien interpretation, to avoid creating an extension which later causes 623890075Sobrien problems. */ 623990075Sobrien 6240132730Skan for (t = current_scope->tags; t; t = TREE_CHAIN (t)) 624190075Sobrien { 624290075Sobrien if (TREE_PURPOSE (t) != 0) 624390075Sobrien { 624490075Sobrien enum tree_code code = TREE_CODE (TREE_VALUE (t)); 6245132730Skan 624690075Sobrien if (code == RECORD_TYPE) 6247132730Skan error ("'struct %s' declared in 'for' loop initial declaration", 624890075Sobrien IDENTIFIER_POINTER (TREE_PURPOSE (t))); 624990075Sobrien else if (code == UNION_TYPE) 6250132730Skan error ("'union %s' declared in 'for' loop initial declaration", 625190075Sobrien IDENTIFIER_POINTER (TREE_PURPOSE (t))); 625290075Sobrien else 6253132730Skan error ("'enum %s' declared in 'for' loop initial declaration", 625490075Sobrien IDENTIFIER_POINTER (TREE_PURPOSE (t))); 625590075Sobrien } 625690075Sobrien } 625790075Sobrien 625890075Sobrien for (t = getdecls (); t; t = TREE_CHAIN (t)) 625990075Sobrien { 626090075Sobrien if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t)) 6261132730Skan error ("%Jdeclaration of non-variable '%D' in 'for' loop " 6262132730Skan "initial declaration", t, t); 626390075Sobrien else if (TREE_STATIC (t)) 6264132730Skan error ("%Jdeclaration of static variable '%D' in 'for' loop " 6265132730Skan "initial declaration", t, t); 626690075Sobrien else if (DECL_EXTERNAL (t)) 6267132730Skan error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop " 6268132730Skan "initial declaration", t, t); 626990075Sobrien } 627018334Speter} 627118334Speter 627218334Speter/* Save and reinitialize the variables 627318334Speter used during compilation of a C function. */ 627418334Speter 627518334Spetervoid 6276132730Skanc_push_function_context (struct function *f) 627718334Speter{ 6278117421Skan struct language_function *p; 6279132730Skan p = ggc_alloc (sizeof (struct language_function)); 6280117421Skan f->language = p; 628118334Speter 628290075Sobrien p->base.x_stmt_tree = c_stmt_tree; 628390075Sobrien p->base.x_scope_stmt_stack = c_scope_stmt_stack; 6284132730Skan p->x_in_iteration_stmt = c_in_iteration_stmt; 6285132730Skan p->x_in_case_stmt = c_in_case_stmt; 628618334Speter p->returns_value = current_function_returns_value; 628718334Speter p->returns_null = current_function_returns_null; 628896263Sobrien p->returns_abnormally = current_function_returns_abnormally; 628918334Speter p->warn_about_return_type = warn_about_return_type; 629018334Speter p->extern_inline = current_extern_inline; 629118334Speter} 629218334Speter 629318334Speter/* Restore the variables used during compilation of a C function. */ 629418334Speter 629518334Spetervoid 6296132730Skanc_pop_function_context (struct function *f) 629718334Speter{ 6298117421Skan struct language_function *p = f->language; 629918334Speter 630090075Sobrien if (DECL_SAVED_INSNS (current_function_decl) == 0 630190075Sobrien && DECL_SAVED_TREE (current_function_decl) == NULL_TREE) 630218334Speter { 630318334Speter /* Stop pointing to the local nodes about to be freed. */ 630418334Speter /* But DECL_INITIAL must remain nonzero so we know this 630518334Speter was an actual function definition. */ 630618334Speter DECL_INITIAL (current_function_decl) = error_mark_node; 630718334Speter DECL_ARGUMENTS (current_function_decl) = 0; 630818334Speter } 630918334Speter 631090075Sobrien c_stmt_tree = p->base.x_stmt_tree; 631190075Sobrien c_scope_stmt_stack = p->base.x_scope_stmt_stack; 6312132730Skan c_in_iteration_stmt = p->x_in_iteration_stmt; 6313132730Skan c_in_case_stmt = p->x_in_case_stmt; 631418334Speter current_function_returns_value = p->returns_value; 631518334Speter current_function_returns_null = p->returns_null; 631696263Sobrien current_function_returns_abnormally = p->returns_abnormally; 631718334Speter warn_about_return_type = p->warn_about_return_type; 631818334Speter current_extern_inline = p->extern_inline; 631918334Speter 6320117421Skan f->language = NULL; 632118334Speter} 632218334Speter 6323117421Skan/* Copy the DECL_LANG_SPECIFIC data associated with DECL. */ 632418334Speter 632518334Spetervoid 6326132730Skanc_dup_lang_specific_decl (tree decl) 632790075Sobrien{ 632890075Sobrien struct lang_decl *ld; 632990075Sobrien 633090075Sobrien if (!DECL_LANG_SPECIFIC (decl)) 633190075Sobrien return; 633290075Sobrien 6333132730Skan ld = ggc_alloc (sizeof (struct lang_decl)); 6334132730Skan memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl)); 633590075Sobrien DECL_LANG_SPECIFIC (decl) = ld; 633690075Sobrien} 633790075Sobrien 633890075Sobrien/* The functions below are required for functionality of doing 633990075Sobrien function at once processing in the C front end. Currently these 634090075Sobrien functions are not called from anywhere in the C front end, but as 634190075Sobrien these changes continue, that will change. */ 634290075Sobrien 6343117421Skan/* Returns nonzero if the current statement is a full expression, 634490075Sobrien i.e. temporaries created during that statement should be destroyed 634590075Sobrien at the end of the statement. */ 634690075Sobrien 634790075Sobrienint 6348132730Skanstmts_are_full_exprs_p (void) 634990075Sobrien{ 635090075Sobrien return 0; 635190075Sobrien} 635290075Sobrien 635390075Sobrien/* Returns the stmt_tree (if any) to which statements are currently 635490075Sobrien being added. If there is no active statement-tree, NULL is 635590075Sobrien returned. */ 635690075Sobrien 635790075Sobrienstmt_tree 6358132730Skancurrent_stmt_tree (void) 635990075Sobrien{ 636090075Sobrien return &c_stmt_tree; 636190075Sobrien} 636290075Sobrien 636390075Sobrien/* Returns the stack of SCOPE_STMTs for the current function. */ 636490075Sobrien 636590075Sobrientree * 6366132730Skancurrent_scope_stmt_stack (void) 636790075Sobrien{ 636890075Sobrien return &c_scope_stmt_stack; 636990075Sobrien} 637090075Sobrien 637190075Sobrien/* Nonzero if TYPE is an anonymous union or struct type. Always 0 in 637290075Sobrien C. */ 637390075Sobrien 637490075Sobrienint 6375132730Skananon_aggr_type_p (tree node ATTRIBUTE_UNUSED) 637618334Speter{ 637790075Sobrien return 0; 637818334Speter} 637990075Sobrien 638090075Sobrien/* Dummy function in place of callback used by C++. */ 638190075Sobrien 638290075Sobrienvoid 6383132730Skanextract_interface_info (void) 638490075Sobrien{ 638590075Sobrien} 638690075Sobrien 638790075Sobrien/* Return a new COMPOUND_STMT, after adding it to the current 638890075Sobrien statement tree. */ 638990075Sobrien 639090075Sobrientree 6391132730Skanc_begin_compound_stmt (void) 639290075Sobrien{ 639390075Sobrien tree stmt; 639490075Sobrien 639590075Sobrien /* Create the COMPOUND_STMT. */ 639690075Sobrien stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE)); 639790075Sobrien 639890075Sobrien return stmt; 639990075Sobrien} 640090075Sobrien 640190075Sobrien/* Expand T (a DECL_STMT) if it declares an entity not handled by the 640290075Sobrien common code. */ 640390075Sobrien 640490075Sobrienvoid 6405132730Skanc_expand_decl_stmt (tree t) 640690075Sobrien{ 640790075Sobrien tree decl = DECL_STMT_DECL (t); 640890075Sobrien 640990075Sobrien /* Expand nested functions. */ 641090075Sobrien if (TREE_CODE (decl) == FUNCTION_DECL 641190075Sobrien && DECL_CONTEXT (decl) == current_function_decl 641290075Sobrien && DECL_SAVED_TREE (decl)) 6413132730Skan c_expand_body_1 (decl, 1); 641490075Sobrien} 641590075Sobrien 6416132730Skan/* Return the global value of T as a symbol. */ 641790075Sobrien 641890075Sobrientree 6419132730Skanidentifier_global_value (tree t) 642090075Sobrien{ 6421132730Skan tree decl = IDENTIFIER_SYMBOL_VALUE (t); 6422132730Skan if (decl == 0 || DECL_FILE_SCOPE_P (decl)) 6423132730Skan return decl; 6424132730Skan 6425132730Skan /* Shadowed by something else; find the true global value. */ 6426132730Skan for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl)) 6427132730Skan if (DECL_NAME (decl) == t) 6428132730Skan return decl; 6429132730Skan 6430132730Skan /* Only local values for this decl. */ 6431132730Skan return 0; 643290075Sobrien} 643390075Sobrien 643490075Sobrien/* Record a builtin type for C. If NAME is non-NULL, it is the name used; 643590075Sobrien otherwise the name is found in ridpointers from RID_INDEX. */ 643690075Sobrien 643790075Sobrienvoid 6438132730Skanrecord_builtin_type (enum rid rid_index, const char *name, tree type) 643990075Sobrien{ 644090075Sobrien tree id; 644190075Sobrien if (name == 0) 644290075Sobrien id = ridpointers[(int) rid_index]; 644390075Sobrien else 644490075Sobrien id = get_identifier (name); 644590075Sobrien pushdecl (build_decl (TYPE_DECL, id, type)); 644690075Sobrien} 644790075Sobrien 644890075Sobrien/* Build the void_list_node (void_type_node having been created). */ 644990075Sobrientree 6450132730Skanbuild_void_list_node (void) 645190075Sobrien{ 645290075Sobrien tree t = build_tree_list (NULL_TREE, void_type_node); 645390075Sobrien return t; 645490075Sobrien} 6455117421Skan 6456117421Skan/* Return something to represent absolute declarators containing a *. 6457117421Skan TARGET is the absolute declarator that the * contains. 6458117421Skan TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile 6459117421Skan to apply to the pointer type, represented as identifiers, possible mixed 6460117421Skan with attributes. 6461117421Skan 6462117421Skan We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST, 6463117421Skan if attributes are present) and whose type is the modifier list. */ 6464117421Skan 6465117421Skantree 6466132730Skanmake_pointer_declarator (tree type_quals_attrs, tree target) 6467117421Skan{ 6468117421Skan tree quals, attrs; 6469117421Skan tree itarget = target; 6470117421Skan split_specs_attrs (type_quals_attrs, &quals, &attrs); 6471117421Skan if (attrs != NULL_TREE) 6472117421Skan itarget = tree_cons (attrs, target, NULL_TREE); 6473117421Skan return build1 (INDIRECT_REF, quals, itarget); 6474117421Skan} 6475117421Skan 6476132730Skan/* A wrapper around lhd_set_decl_assembler_name that gives static 6477132730Skan variables their C names if they are at file scope and only one 6478132730Skan translation unit is being compiled, for backwards compatibility 6479132730Skan with certain bizarre assembler hacks (like crtstuff.c). */ 6480132730Skan 6481132730Skanvoid 6482132730Skanc_static_assembler_name (tree decl) 6483132730Skan{ 6484132730Skan if (num_in_fnames == 1 6485132730Skan && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl) 6486132730Skan && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL) 6487132730Skan SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl)); 6488132730Skan else 6489132730Skan lhd_set_decl_assembler_name (decl); 6490132730Skan} 6491132730Skan 6492132730Skan/* Hash and equality functions for link_hash_table: key off 6493132730Skan DECL_ASSEMBLER_NAME. */ 6494132730Skan 6495132730Skanstatic hashval_t 6496132730Skanlink_hash_hash (const void *x_p) 6497132730Skan{ 6498132730Skan tree x = (tree)x_p; 6499132730Skan return (hashval_t) (long)DECL_ASSEMBLER_NAME (x); 6500132730Skan} 6501132730Skan 6502132730Skanstatic int 6503132730Skanlink_hash_eq (const void *x1_p, const void *x2_p) 6504132730Skan{ 6505132730Skan tree x1 = (tree)x1_p; 6506132730Skan tree x2 = (tree)x2_p; 6507132730Skan return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2); 6508132730Skan} 6509132730Skan 6510132730Skan/* Propagate information between definitions and uses between multiple 6511132730Skan translation units in TU_LIST based on linkage rules. */ 6512132730Skan 6513132730Skanvoid 6514132730Skanmerge_translation_unit_decls (void) 6515132730Skan{ 6516132730Skan const tree tu_list = current_file_decl; 6517132730Skan tree tu; 6518132730Skan tree decl; 6519132730Skan htab_t link_hash_table; 6520132730Skan tree block; 6521132730Skan 6522132730Skan /* Create the BLOCK that poplevel would have created, but don't 6523132730Skan actually call poplevel since that's expensive. */ 6524132730Skan block = make_node (BLOCK); 6525132730Skan BLOCK_VARS (block) = current_scope->names; 6526132730Skan TREE_USED (block) = 1; 6527132730Skan DECL_INITIAL (current_file_decl) = block; 6528132730Skan 6529132730Skan /* If only one translation unit seen, no copying necessary. */ 6530132730Skan if (TREE_CHAIN (tu_list) == NULL_TREE) 6531132730Skan return; 6532132730Skan 6533132730Skan link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL); 6534132730Skan 6535132730Skan /* Enter any actual definitions into the hash table. */ 6536132730Skan for (tu = tu_list; tu; tu = TREE_CHAIN (tu)) 6537132730Skan for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl)) 6538132730Skan if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl)) 6539132730Skan { 6540132730Skan PTR *slot; 6541132730Skan slot = htab_find_slot (link_hash_table, decl, INSERT); 6542132730Skan 6543132730Skan /* If we've already got a definition, work out which one is 6544132730Skan the real one, put it into the hash table, and make the 6545132730Skan other one DECL_EXTERNAL. This is important to avoid 6546132730Skan putting out two definitions of the same symbol in the 6547132730Skan assembly output. */ 6548132730Skan if (*slot != NULL) 6549132730Skan { 6550132730Skan tree old_decl = (tree) *slot; 6551132730Skan 6552132730Skan /* If this is weak or common or whatever, suppress it 6553132730Skan in favor of the other definition. */ 6554132730Skan if (DECL_WEAK (decl)) 6555132730Skan DECL_EXTERNAL (decl) = 1; 6556132730Skan else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl)) 6557132730Skan DECL_EXTERNAL (old_decl) = 1; 6558132730Skan else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl)) 6559132730Skan DECL_EXTERNAL (decl) = 1; 6560132730Skan else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl)) 6561132730Skan DECL_EXTERNAL (old_decl) = 1; 6562132730Skan 6563132730Skan if (DECL_EXTERNAL (decl)) 6564132730Skan { 6565132730Skan DECL_INITIAL (decl) = NULL_TREE; 6566132730Skan DECL_COMMON (decl) = 0; 6567132730Skan DECL_ONE_ONLY (decl) = 0; 6568132730Skan DECL_WEAK (decl) = 0; 6569132730Skan } 6570132730Skan else if (DECL_EXTERNAL (old_decl)) 6571132730Skan { 6572132730Skan DECL_INITIAL (old_decl) = NULL_TREE; 6573132730Skan DECL_COMMON (old_decl) = 0; 6574132730Skan DECL_ONE_ONLY (old_decl) = 0; 6575132730Skan DECL_WEAK (old_decl) = 0; 6576132730Skan *slot = decl; 6577132730Skan } 6578132730Skan else 6579132730Skan { 6580132730Skan error ("%Jredefinition of global '%D'", decl, decl); 6581132730Skan error ("%J'%D' previously defined here", old_decl, old_decl); 6582132730Skan } 6583132730Skan } 6584132730Skan else 6585132730Skan *slot = decl; 6586132730Skan } 6587132730Skan 6588132730Skan /* Now insert the desired information from all the definitions 6589132730Skan into any plain declarations. */ 6590132730Skan for (tu = tu_list; tu; tu = TREE_CHAIN (tu)) 6591132730Skan for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl)) 6592132730Skan if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl)) 6593132730Skan { 6594132730Skan tree global_decl; 6595132730Skan global_decl = htab_find (link_hash_table, decl); 6596132730Skan 6597132730Skan if (! global_decl) 6598132730Skan continue; 6599132730Skan 6600132730Skan /* Print any appropriate error messages, and partially merge 6601132730Skan the decls. */ 6602132730Skan (void) duplicate_decls (decl, global_decl); 6603132730Skan } 6604132730Skan 6605132730Skan htab_delete (link_hash_table); 6606132730Skan} 6607132730Skan 6608132730Skan/* Perform final processing on file-scope data. */ 6609132730Skan 6610132730Skanvoid 6611132730Skanc_write_global_declarations(void) 6612132730Skan{ 6613132730Skan tree link; 6614132730Skan 6615132730Skan for (link = current_file_decl; link; link = TREE_CHAIN (link)) 6616132730Skan { 6617132730Skan tree globals = BLOCK_VARS (DECL_INITIAL (link)); 6618132730Skan int len = list_length (globals); 6619132730Skan tree *vec = xmalloc (sizeof (tree) * len); 6620132730Skan int i; 6621132730Skan tree decl; 6622132730Skan 6623132730Skan /* Process the decls in the order they were written. */ 6624132730Skan 6625132730Skan for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl)) 6626132730Skan vec[i] = decl; 6627132730Skan 6628132730Skan wrapup_global_declarations (vec, len); 6629132730Skan 6630132730Skan check_global_declarations (vec, len); 6631132730Skan 6632132730Skan /* Clean up. */ 6633132730Skan free (vec); 6634132730Skan } 6635132730Skan} 6636132730Skan 6637132730Skan/* Reset the parser's state in preparation for a new file. */ 6638132730Skan 6639132730Skanvoid 6640132730Skanc_reset_state (void) 6641132730Skan{ 6642132730Skan tree link; 6643132730Skan tree file_scope_decl; 6644132730Skan 6645132730Skan /* Pop the global scope. */ 6646132730Skan if (current_scope != global_scope) 6647132730Skan current_scope = global_scope; 6648132730Skan file_scope_decl = current_file_decl; 6649132730Skan DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0); 6650132730Skan BLOCK_SUPERCONTEXT (DECL_INITIAL (file_scope_decl)) = file_scope_decl; 6651132730Skan truly_local_externals = NULL_TREE; 6652132730Skan 6653132730Skan /* Start a new global binding level. */ 6654132730Skan pushlevel (0); 6655132730Skan global_scope = current_scope; 6656132730Skan current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL); 6657132730Skan TREE_CHAIN (current_file_decl) = file_scope_decl; 6658132730Skan 6659132730Skan /* Reintroduce the builtin declarations. */ 6660132730Skan for (link = first_builtin_decl; 6661132730Skan link != TREE_CHAIN (last_builtin_decl); 6662132730Skan link = TREE_CHAIN (link)) 6663132730Skan pushdecl (copy_node (link)); 6664132730Skan} 6665132730Skan 6666117421Skan#include "gt-c-decl.h" 6667