c-decl.c revision 146906
1/* Process declarations and variables for C compiler. 2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 3 2001, 2002, 2003, 2004 Free Software Foundation, Inc. 4 5This file is part of GCC. 6 7GCC is free software; you can redistribute it and/or modify it under 8the terms of the GNU General Public License as published by the Free 9Software Foundation; either version 2, or (at your option) any later 10version. 11 12GCC is distributed in the hope that it will be useful, but WITHOUT ANY 13WARRANTY; without even the implied warranty of MERCHANTABILITY or 14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15for more details. 16 17You should have received a copy of the GNU General Public License 18along with GCC; see the file COPYING. If not, write to the Free 19Software Foundation, 59 Temple Place - Suite 330, Boston, MA 2002111-1307, USA. */ 21 22/* $FreeBSD: head/contrib/gcc/c-decl.c 146906 2005-06-03 03:50:42Z kan $ */ 23 24/* Process declarations and symbol lookup for C front end. 25 Also constructs types; the standard scalar types at initialization, 26 and structure, union, array and enum types when they are declared. */ 27 28/* ??? not all decl nodes are given the most useful possible 29 line numbers. For example, the CONST_DECLs for enum values. */ 30 31#include "config.h" 32#include "system.h" 33#include "coretypes.h" 34#include "tm.h" 35#include "intl.h" 36#include "tree.h" 37#include "tree-inline.h" 38#include "rtl.h" 39#include "flags.h" 40#include "function.h" 41#include "output.h" 42#include "expr.h" 43#include "c-tree.h" 44#include "toplev.h" 45#include "ggc.h" 46#include "tm_p.h" 47#include "cpplib.h" 48#include "target.h" 49#include "debug.h" 50#include "opts.h" 51#include "timevar.h" 52#include "c-common.h" 53#include "c-pragma.h" 54#include "cgraph.h" 55#include "hashtab.h" 56#include "libfuncs.h" 57#include "except.h" 58#include "langhooks-def.h" 59 60/* In grokdeclarator, distinguish syntactic contexts of declarators. */ 61enum decl_context 62{ NORMAL, /* Ordinary declaration */ 63 FUNCDEF, /* Function definition */ 64 PARM, /* Declaration of parm before function body */ 65 FIELD, /* Declaration inside struct or union */ 66 TYPENAME}; /* Typename (inside cast or sizeof) */ 67 68 69/* Nonzero if we have seen an invalid cross reference 70 to a struct, union, or enum, but not yet printed the message. */ 71 72tree pending_invalid_xref; 73/* File and line to appear in the eventual error message. */ 74location_t pending_invalid_xref_location; 75 76/* While defining an enum type, this is 1 plus the last enumerator 77 constant value. Note that will do not have to save this or `enum_overflow' 78 around nested function definition since such a definition could only 79 occur in an enum value expression and we don't use these variables in 80 that case. */ 81 82static tree enum_next_value; 83 84/* Nonzero means that there was overflow computing enum_next_value. */ 85 86static int enum_overflow; 87 88/* Parsing a function declarator leaves a list of parameter names 89 or a chain of parameter decls here. */ 90 91static tree last_function_parms; 92 93/* ... and a chain of structure and enum types declared in the 94 parmlist here. */ 95 96static tree last_function_parm_tags; 97 98/* ... and a chain of all non-parameter declarations (such as 99 CONST_DECLs from enumerations) here. */ 100 101static tree last_function_parm_others; 102 103/* After parsing the declarator that starts a function definition, 104 `start_function' puts the list of parameter names or chain of decls here 105 for `store_parm_decls' to find. */ 106 107static tree current_function_parms; 108 109/* Similar, for last_function_parm_tags. */ 110 111static tree current_function_parm_tags; 112 113/* And for last_function_parm_others. */ 114 115static tree current_function_parm_others; 116 117/* Similar, for the file and line that the prototype came from if this is 118 an old-style definition. */ 119 120static location_t current_function_prototype_locus; 121 122/* The current statement tree. */ 123 124static GTY(()) struct stmt_tree_s c_stmt_tree; 125 126/* The current scope statement stack. */ 127 128static GTY(()) tree c_scope_stmt_stack; 129 130/* State saving variables. */ 131int c_in_iteration_stmt; 132int c_in_case_stmt; 133 134/* A list of external DECLs that appeared at block scope when there was 135 some other global meaning for that identifier. */ 136static GTY(()) tree truly_local_externals; 137 138/* All the builtins; this is a subset of the entries of global_scope. */ 139 140static GTY(()) tree first_builtin_decl; 141static GTY(()) tree last_builtin_decl; 142 143/* A DECL for the current file-scope context. */ 144 145static GTY(()) tree current_file_decl; 146 147/* Set to 0 at beginning of a function definition, set to 1 if 148 a return statement that specifies a return value is seen. */ 149 150int current_function_returns_value; 151 152/* Set to 0 at beginning of a function definition, set to 1 if 153 a return statement with no argument is seen. */ 154 155int current_function_returns_null; 156 157/* Set to 0 at beginning of a function definition, set to 1 if 158 a call to a noreturn function is seen. */ 159 160int current_function_returns_abnormally; 161 162/* Set to nonzero by `grokdeclarator' for a function 163 whose return type is defaulted, if warnings for this are desired. */ 164 165static int warn_about_return_type; 166 167/* Nonzero when starting a function declared `extern inline'. */ 168 169static int current_extern_inline; 170 171/* Each c_scope structure describes the complete contents of one scope. 172 Three scopes are distinguished specially: the innermost or current 173 scope, the innermost function scope, and the outermost or file scope. 174 175 Most declarations are recorded in the current scope. 176 177 All normal label declarations are recorded in the innermost 178 function scope, as are bindings of undeclared identifiers to 179 error_mark_node. (GCC permits nested functions as an extension, 180 hence the 'innermost' qualifier.) Explicitly declared labels 181 (using the __label__ extension) appear in the current scope. 182 183 Being in the global scope (current_scope == global_scope) causes 184 special behavior in several places below. Also, under some 185 conditions the Objective-C front end records declarations in the 186 global scope even though that isn't the current scope. 187 188 The order of the names, parms, and blocks lists matters, and they 189 are frequently appended to. To avoid having to walk all the way to 190 the end of the list on each insertion, or reverse the lists later, 191 we maintain a pointer to the last list entry for each of the lists. 192 193 The order of the tags, shadowed, and shadowed_tags 194 lists does not matter, so we just prepend to these lists. */ 195 196struct c_scope GTY(()) 197{ 198 /* The scope containing this one. */ 199 struct c_scope *outer; 200 201 /* The next outermost function scope. */ 202 struct c_scope *outer_function; 203 204 /* All variables, constants, functions, labels, and typedef names. */ 205 tree names; 206 tree names_last; 207 208 /* All parameter declarations. Used only in the outermost scope of 209 a function. */ 210 tree parms; 211 tree parms_last; 212 213 /* All structure, union, and enum type tags. */ 214 tree tags; 215 216 /* For each scope, a list of shadowed outer-scope definitions 217 to be restored when this scope is popped. 218 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and 219 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */ 220 tree shadowed; 221 222 /* For each scope, a list of shadowed outer-scope tag definitions 223 to be restored when this scope is popped. 224 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and 225 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */ 226 tree shadowed_tags; 227 228 /* For each scope (except the global one), a chain of BLOCK nodes 229 for all the scopes that were entered and exited one level down. */ 230 tree blocks; 231 tree blocks_last; 232 233 /* True if we are currently filling this scope with parameter 234 declarations. */ 235 BOOL_BITFIELD parm_flag : 1; 236 237 /* True if we already complained about forward parameter decls 238 in this scope. This prevents double warnings on 239 foo (int a; int b; ...) */ 240 BOOL_BITFIELD warned_forward_parm_decls : 1; 241 242 /* True if this is the outermost block scope of a function body. 243 This scope contains the parameters, the local variables declared 244 in the outermost block, and all the labels (except those in 245 nested functions, or declared at block scope with __label__). */ 246 BOOL_BITFIELD function_body : 1; 247 248 /* True means make a BLOCK for this scope no matter what. */ 249 BOOL_BITFIELD keep : 1; 250}; 251 252/* The scope currently in effect. */ 253 254static GTY(()) struct c_scope *current_scope; 255 256/* A chain of c_scope structures awaiting reuse. */ 257 258static GTY((deletable (""))) struct c_scope *scope_freelist; 259 260/* The innermost function scope. Ordinary (not explicitly declared) 261 labels, bindings to error_mark_node, and the lazily-created 262 bindings of __func__ and its friends get this scope. */ 263 264static GTY(()) struct c_scope *current_function_scope; 265 266/* The outermost scope, corresponding to the C "file scope". This is 267 created when the compiler is started and exists through the entire run. */ 268 269static GTY(()) struct c_scope *global_scope; 270 271/* Append VAR to LIST in scope SCOPE. */ 272#define SCOPE_LIST_APPEND(scope, list, decl) do { \ 273 struct c_scope *s_ = (scope); \ 274 tree d_ = (decl); \ 275 if (s_->list##_last) \ 276 TREE_CHAIN (s_->list##_last) = d_; \ 277 else \ 278 s_->list = d_; \ 279 s_->list##_last = d_; \ 280} while (0) 281 282/* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */ 283#define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \ 284 struct c_scope *t_ = (tscope); \ 285 struct c_scope *f_ = (fscope); \ 286 if (t_->to##_last) \ 287 TREE_CHAIN (t_->to##_last) = f_->from; \ 288 else \ 289 t_->to = f_->from; \ 290 t_->to##_last = f_->from##_last; \ 291} while (0) 292 293/* True means unconditionally make a BLOCK for the next scope pushed. */ 294 295static bool keep_next_level_flag; 296 297/* True means the next call to pushlevel will be the outermost scope 298 of a function body, so do not push a new scope, merely cease 299 expecting parameter decls. */ 300 301static bool next_is_function_body; 302 303/* Functions called automatically at the beginning and end of execution. */ 304 305tree static_ctors, static_dtors; 306 307/* Forward declarations. */ 308 309static struct c_scope *make_scope (void); 310static void pop_scope (void); 311static tree make_label (tree, location_t); 312static void bind_label (tree, tree, struct c_scope *); 313static void implicit_decl_warning (tree); 314static tree lookup_tag (enum tree_code, tree, int); 315static tree lookup_name_current_level (tree); 316static tree grokdeclarator (tree, tree, enum decl_context, int, tree *); 317static tree grokparms (tree, int); 318static void layout_array_type (tree); 319static void store_parm_decls_newstyle (void); 320static void store_parm_decls_oldstyle (void); 321static tree c_make_fname_decl (tree, int); 322static void c_expand_body_1 (tree, int); 323static tree any_external_decl (tree); 324static void record_external_decl (tree); 325static void warn_if_shadowing (tree, tree); 326static void check_bitfield_type_and_width (tree *, tree *, const char *); 327static void clone_underlying_type (tree); 328static bool flexible_array_type_p (tree); 329static hashval_t link_hash_hash (const void *); 330static int link_hash_eq (const void *, const void *); 331 332/* States indicating how grokdeclarator() should handle declspecs marked 333 with __attribute__((deprecated)). An object declared as 334 __attribute__((deprecated)) suppresses warnings of uses of other 335 deprecated items. */ 336 337enum deprecated_states { 338 DEPRECATED_NORMAL, 339 DEPRECATED_SUPPRESS 340}; 341 342static enum deprecated_states deprecated_state = DEPRECATED_NORMAL; 343 344void 345c_print_identifier (FILE *file, tree node, int indent) 346{ 347 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4); 348 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4); 349 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4); 350 if (C_IS_RESERVED_WORD (node)) 351 { 352 tree rid = ridpointers[C_RID_CODE (node)]; 353 indent_to (file, indent + 4); 354 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"", 355 (void *) rid, IDENTIFIER_POINTER (rid)); 356 } 357} 358 359/* Hook called at end of compilation to assume 1 elt 360 for a file-scope tentative array defn that wasn't complete before. */ 361 362void 363c_finish_incomplete_decl (tree decl) 364{ 365 if (TREE_CODE (decl) == VAR_DECL) 366 { 367 tree type = TREE_TYPE (decl); 368 if (type != error_mark_node 369 && TREE_CODE (type) == ARRAY_TYPE 370 && ! DECL_EXTERNAL (decl) 371 && TYPE_DOMAIN (type) == 0) 372 { 373 warning ("%Jarray '%D' assumed to have one element", decl, decl); 374 375 complete_array_type (type, NULL_TREE, 1); 376 377 layout_decl (decl, 0); 378 } 379 } 380} 381 382/* Reuse or create a struct for this scope. */ 383 384static struct c_scope * 385make_scope (void) 386{ 387 struct c_scope *result; 388 if (scope_freelist) 389 { 390 result = scope_freelist; 391 scope_freelist = result->outer; 392 } 393 else 394 result = ggc_alloc_cleared (sizeof (struct c_scope)); 395 396 return result; 397} 398 399/* Remove the topmost scope from the stack and add it to the 400 free list, updating current_function_scope if necessary. */ 401 402static void 403pop_scope (void) 404{ 405 struct c_scope *scope = current_scope; 406 407 current_scope = scope->outer; 408 if (scope->function_body) 409 current_function_scope = scope->outer_function; 410 411 memset (scope, 0, sizeof (struct c_scope)); 412 scope->outer = scope_freelist; 413 scope_freelist = scope; 414} 415 416/* The Objective-C front-end often needs to determine the current scope. */ 417 418void * 419get_current_scope (void) 420{ 421 return current_scope; 422} 423 424/* The following function is used only by Objective-C. It needs to live here 425 because it accesses the innards of c_scope. */ 426 427void 428objc_mark_locals_volatile (void *enclosing_blk) 429{ 430 struct c_scope *scope; 431 432 for (scope = current_scope; 433 scope && scope != enclosing_blk; 434 scope = scope->outer) 435 { 436 tree decl; 437 438 for (decl = scope->names; decl; decl = TREE_CHAIN (decl)) 439 { 440 DECL_REGISTER (decl) = 0; 441 TREE_THIS_VOLATILE (decl) = 1; 442 } 443 /* Do not climb up past the current function. */ 444 if (scope->function_body) 445 break; 446 } 447} 448 449/* Nonzero if we are currently in the global scope. */ 450 451int 452global_bindings_p (void) 453{ 454 return current_scope == global_scope; 455} 456 457void 458keep_next_level (void) 459{ 460 keep_next_level_flag = true; 461} 462 463/* Identify this scope as currently being filled with parameters. */ 464 465void 466declare_parm_level (void) 467{ 468 current_scope->parm_flag = true; 469} 470 471/* Nonzero if currently making parm declarations. */ 472 473int 474in_parm_level_p (void) 475{ 476 return current_scope->parm_flag; 477} 478 479/* Enter a new scope. The dummy parameter is for signature 480 compatibility with lang_hooks.decls.pushlevel. */ 481 482void 483pushlevel (int dummy ATTRIBUTE_UNUSED) 484{ 485 if (next_is_function_body) 486 { 487 /* This is the transition from the parameters to the top level 488 of the function body. These are the same scope 489 (C99 6.2.1p4,6) so we do not push another scope structure. 490 next_is_function_body is set only by store_parm_decls, which 491 in turn is called when and only when we are about to 492 encounter the opening curly brace for the function body. 493 494 The outermost block of a function always gets a BLOCK node, 495 because the debugging output routines expect that each 496 function has at least one BLOCK. */ 497 current_scope->parm_flag = false; 498 current_scope->function_body = true; 499 current_scope->keep = true; 500 current_scope->outer_function = current_function_scope; 501 current_function_scope = current_scope; 502 503 keep_next_level_flag = false; 504 next_is_function_body = false; 505 } 506 else 507 { 508 struct c_scope *scope = make_scope (); 509 510 scope->keep = keep_next_level_flag; 511 scope->outer = current_scope; 512 current_scope = scope; 513 keep_next_level_flag = false; 514 } 515} 516 517/* Exit a scope. Restore the state of the identifier-decl mappings 518 that were in effect when this scope was entered. 519 520 If KEEP is KEEP_YES (1), this scope had explicit declarations, so 521 create a BLOCK node to record its declarations and subblocks for 522 debugging output. If KEEP is KEEP_MAYBE, do so only if the names 523 or tags lists are nonempty. 524 525 The second parameter is ignored; it is present only for 526 signature compatibility with lang_hooks.decls.poplevel. 527 528 If FUNCTIONBODY is nonzero, this level is the body of a function, 529 even if current_scope->function_body is not set. This is used 530 by language-independent code that generates synthetic functions, 531 and cannot set current_scope->function_body. 532 533 FIXME: Eliminate the need for all arguments. */ 534 535tree 536poplevel (int keep, int dummy ATTRIBUTE_UNUSED, int functionbody) 537{ 538 struct c_scope *scope = current_scope; 539 tree block; 540 tree decl; 541 tree p; 542 543 /* The following line does not use |= due to a bug in HP's C compiler. */ 544 scope->function_body = scope->function_body | functionbody; 545 546 if (keep == KEEP_MAYBE) 547 keep = (scope->names || scope->tags); 548 549 keep |= scope->keep; 550 keep |= scope->function_body; 551 552 /* If appropriate, create a BLOCK to record the decls for the life 553 of this function. */ 554 block = 0; 555 if (keep) 556 { 557 block = make_node (BLOCK); 558 BLOCK_VARS (block) = scope->names; 559 BLOCK_SUBBLOCKS (block) = scope->blocks; 560 TREE_USED (block) = 1; 561 } 562 563 /* In each subblock, record that this is its superior. */ 564 for (p = scope->blocks; p; p = TREE_CHAIN (p)) 565 BLOCK_SUPERCONTEXT (p) = block; 566 567 /* Clear out the variable bindings in this scope. 568 569 Propagate TREE_ADDRESSABLE from nested functions to their 570 containing functions. 571 572 Issue warnings for unused variables and labels, and errors for 573 undefined labels, if there are any. */ 574 575 for (p = scope->names; p; p = TREE_CHAIN (p)) 576 { 577 switch (TREE_CODE (p)) 578 { 579 case LABEL_DECL: 580 if (TREE_USED (p) && !DECL_INITIAL (p)) 581 { 582 error ("%Jlabel `%D' used but not defined", p, p); 583 DECL_INITIAL (p) = error_mark_node; 584 } 585 else if (!TREE_USED (p) && warn_unused_label) 586 { 587 if (DECL_INITIAL (p)) 588 warning ("%Jlabel `%D' defined but not used", p, p); 589 else 590 warning ("%Jlabel `%D' declared but not defined", p, p); 591 } 592 593 IDENTIFIER_LABEL_VALUE (DECL_NAME (p)) = 0; 594 break; 595 596 case FUNCTION_DECL: 597 if (! TREE_ASM_WRITTEN (p) 598 && DECL_INITIAL (p) != 0 599 && TREE_ADDRESSABLE (p) 600 && DECL_ABSTRACT_ORIGIN (p) != 0 601 && DECL_ABSTRACT_ORIGIN (p) != p) 602 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1; 603 goto normal; 604 605 case VAR_DECL: 606 /* Keep this in sync with stmt.c:warn_about_unused_variables. 607 No warnings when the global scope is popped because the 608 global scope isn't popped for the last translation unit, 609 so the warnings are done in c_write_global_declaration. */ 610 if (warn_unused_variable && scope != global_scope 611 && !TREE_USED (p) 612 && !DECL_IN_SYSTEM_HEADER (p) 613 && DECL_NAME (p) 614 && !DECL_ARTIFICIAL (p)) 615 warning ("%Junused variable `%D'", p, p); 616 /* fall through */ 617 618 default: 619 normal: 620 if (DECL_NAME (p)) 621 { 622 if (DECL_EXTERNAL (p) && scope != global_scope) 623 /* External decls stay in the symbol-value slot but are 624 inaccessible. */ 625 C_DECL_INVISIBLE (p) = 1; 626 else 627 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0; 628 } 629 break; 630 } 631 } 632 633 /* Clear out the parameter bindings in this scope, if any. 634 Unused-parameter warnings are handled by function.c. */ 635 for (p = scope->parms; p; p = TREE_CHAIN (p)) 636 if (DECL_NAME (p)) 637 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0; 638 639 /* Clear out the tag-meanings declared in this scope. 640 641 Set the TYPE_CONTEXTs for all of the tagged types belonging to 642 this scope so that they point to the appropriate construct, i.e. 643 either to the current FUNCTION_DECL node, or else to the BLOCK 644 node we just constructed. 645 646 Note that for tagged types whose scope is just the formal 647 parameter list for some function type specification, we can't 648 properly set their TYPE_CONTEXTs here, because we don't have a 649 pointer to the appropriate FUNCTION_TYPE node readily available 650 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged 651 type nodes get set in `grokdeclarator' as soon as we have created 652 the FUNCTION_TYPE node which will represent the "scope" for these 653 "parameter list local" tagged types. */ 654 655 decl = scope->function_body ? current_function_decl : block; 656 for (p = scope->tags; p; p = TREE_CHAIN (p)) 657 { 658 if (TREE_PURPOSE (p)) 659 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = 0; 660 if (decl) 661 TYPE_CONTEXT (TREE_VALUE (p)) = decl; 662 } 663 664 /* Restore all name- and label-meanings from outer scopes that were 665 shadowed by this scope. */ 666 for (p = scope->shadowed; p; p = TREE_CHAIN (p)) 667 if (TREE_VALUE (p) && TREE_CODE (TREE_VALUE (p)) == LABEL_DECL) 668 IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p); 669 else 670 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p); 671 672 /* Restore all tag-meanings from outer scopes that were shadowed by 673 this scope. */ 674 for (p = scope->shadowed_tags; p; p = TREE_CHAIN (p)) 675 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p); 676 677 /* Dispose of the block that we just made inside some higher level. */ 678 if (scope->function_body && current_function_decl) 679 DECL_INITIAL (current_function_decl) = block; 680 else if (scope->outer) 681 { 682 if (block) 683 SCOPE_LIST_APPEND (scope->outer, blocks, block); 684 /* If we did not make a block for the scope just exited, any 685 blocks made for inner scopes must be carried forward so they 686 will later become subblocks of something else. */ 687 else if (scope->blocks) 688 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks); 689 } 690 691 /* Pop the current scope, and free the structure for reuse. */ 692 pop_scope (); 693 694 return block; 695} 696 697/* Insert BLOCK at the end of the list of subblocks of the current 698 scope. This is used when a BIND_EXPR is expanded, to handle the 699 BLOCK node inside the BIND_EXPR. */ 700 701void 702insert_block (tree block) 703{ 704 TREE_USED (block) = 1; 705 SCOPE_LIST_APPEND (current_scope, blocks, block); 706} 707 708/* Set the BLOCK node for the innermost scope (the one we are 709 currently in). The RTL expansion machinery requires us to provide 710 this hook, but it is not useful in function-at-a-time mode. */ 711 712void 713set_block (tree block ATTRIBUTE_UNUSED) 714{ 715} 716 717/* Push a definition or a declaration of struct, union or enum tag "name". 718 "type" should be the type node. 719 We assume that the tag "name" is not already defined. 720 721 Note that the definition may really be just a forward reference. 722 In that case, the TYPE_SIZE will be zero. */ 723 724void 725pushtag (tree name, tree type) 726{ 727 struct c_scope *b = current_scope; 728 729 /* Record the identifier as the type's name if it has none. */ 730 if (name) 731 { 732 if (TYPE_NAME (type) == 0) 733 TYPE_NAME (type) = name; 734 735 if (IDENTIFIER_TAG_VALUE (name)) 736 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name), 737 b->shadowed_tags); 738 IDENTIFIER_TAG_VALUE (name) = type; 739 } 740 741 b->tags = tree_cons (name, type, b->tags); 742 743 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the 744 tagged type we just added to the current scope. This fake 745 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs 746 to output a representation of a tagged type, and it also gives 747 us a convenient place to record the "scope start" address for the 748 tagged type. */ 749 750 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type)); 751 752 /* An approximation for now, so we can tell this is a function-scope tag. 753 This will be updated in poplevel. */ 754 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type)); 755} 756 757/* Subroutine of compare_decls. Allow harmless mismatches in return 758 and argument types provided that the type modes match. This function 759 return a unified type given a suitable match, and 0 otherwise. */ 760 761static tree 762match_builtin_function_types (tree newtype, tree oldtype) 763{ 764 tree newrettype, oldrettype; 765 tree newargs, oldargs; 766 tree trytype, tryargs; 767 768 /* Accept the return type of the new declaration if same modes. */ 769 oldrettype = TREE_TYPE (oldtype); 770 newrettype = TREE_TYPE (newtype); 771 772 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype)) 773 return 0; 774 775 oldargs = TYPE_ARG_TYPES (oldtype); 776 newargs = TYPE_ARG_TYPES (newtype); 777 tryargs = newargs; 778 779 while (oldargs || newargs) 780 { 781 if (! oldargs 782 || ! newargs 783 || ! TREE_VALUE (oldargs) 784 || ! TREE_VALUE (newargs) 785 || TYPE_MODE (TREE_VALUE (oldargs)) 786 != TYPE_MODE (TREE_VALUE (newargs))) 787 return 0; 788 789 oldargs = TREE_CHAIN (oldargs); 790 newargs = TREE_CHAIN (newargs); 791 } 792 793 trytype = build_function_type (newrettype, tryargs); 794 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype)); 795} 796 797/* Subroutine of diagnose_mismathed_decls. Check for function type 798 mismatch involving an empty arglist vs a nonempty one and give clearer 799 diagnostics. */ 800static void 801diagnose_arglist_conflict (tree newdecl, tree olddecl, 802 tree newtype, tree oldtype) 803{ 804 tree t; 805 806 if (TREE_CODE (olddecl) != FUNCTION_DECL 807 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype), COMPARE_STRICT) 808 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0) 809 || 810 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0))) 811 return; 812 813 t = TYPE_ARG_TYPES (oldtype); 814 if (t == 0) 815 t = TYPE_ARG_TYPES (newtype); 816 for (; t; t = TREE_CHAIN (t)) 817 { 818 tree type = TREE_VALUE (t); 819 820 if (TREE_CHAIN (t) == 0 821 && TYPE_MAIN_VARIANT (type) != void_type_node) 822 { 823 inform ("a parameter list with an ellipsis can't match " 824 "an empty parameter name list declaration"); 825 break; 826 } 827 828 if (c_type_promotes_to (type) != type) 829 { 830 inform ("an argument type that has a default promotion can't match " 831 "an empty parameter name list declaration"); 832 break; 833 } 834 } 835} 836 837/* Another subroutine of diagnose_mismatched_decls. OLDDECL is an 838 old-style function definition, NEWDECL is a prototype declaration. 839 Diagnose inconsistencies in the argument list. Returns TRUE if 840 the prototype is compatible, FALSE if not. */ 841static bool 842validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype) 843{ 844 tree newargs, oldargs; 845 int i; 846 847 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */ 848#define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node) 849 850 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype); 851 newargs = TYPE_ARG_TYPES (newtype); 852 i = 1; 853 854 for (;;) 855 { 856 tree oldargtype = TREE_VALUE (oldargs); 857 tree newargtype = TREE_VALUE (newargs); 858 859 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype)) 860 break; 861 862 /* Reaching the end of just one list means the two decls don't 863 agree on the number of arguments. */ 864 if (END_OF_ARGLIST (oldargtype)) 865 { 866 error ("%Jprototype for '%D' declares more arguments " 867 "than previous old-style definition", newdecl, newdecl); 868 return false; 869 } 870 else if (END_OF_ARGLIST (newargtype)) 871 { 872 error ("%Jprototype for '%D' declares fewer arguments " 873 "than previous old-style definition", newdecl, newdecl); 874 return false; 875 } 876 877 /* Type for passing arg must be consistent with that declared 878 for the arg. */ 879 else if (! comptypes (oldargtype, newargtype, COMPARE_STRICT)) 880 { 881 error ("%Jprototype for '%D' declares arg %d with incompatible type", 882 newdecl, newdecl, i); 883 return false; 884 } 885 886 oldargs = TREE_CHAIN (oldargs); 887 newargs = TREE_CHAIN (newargs); 888 i++; 889 } 890 891 /* If we get here, no errors were found, but do issue a warning 892 for this poor-style construct. */ 893 warning ("%Jprototype for '%D' follows non-prototype definition", 894 newdecl, newdecl); 895 return true; 896#undef END_OF_ARGLIST 897} 898 899/* Subroutine of diagnose_mismatched_decls. Report the location of DECL, 900 first in a pair of mismatched declarations, using the diagnostic 901 function DIAG. */ 902static void 903locate_old_decl (tree decl, void (*diag)(const char *, ...)) 904{ 905 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)) 906 ; 907 else if (DECL_INITIAL (decl)) 908 diag (N_("%Jprevious definition of '%D' was here"), decl, decl); 909 else if (C_DECL_IMPLICIT (decl)) 910 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl); 911 else 912 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl); 913} 914 915/* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL. 916 Returns true if the caller should proceed to merge the two, false 917 if OLDDECL should simply be discarded. As a side effect, issues 918 all necessary diagnostics for invalid or poor-style combinations. 919 If it returns true, writes the types of NEWDECL and OLDDECL to 920 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from 921 TREE_TYPE (NEWDECL, OLDDECL) respectively. */ 922 923static bool 924diagnose_mismatched_decls (tree newdecl, tree olddecl, 925 tree *newtypep, tree *oldtypep) 926{ 927 tree newtype, oldtype; 928 bool pedwarned = false; 929 bool warned = false; 930 931 /* If we have error_mark_node for either decl or type, just discard 932 the previous decl - we're in an error cascade already. */ 933 if (olddecl == error_mark_node || newdecl == error_mark_node) 934 return false; 935 *oldtypep = oldtype = TREE_TYPE (olddecl); 936 *newtypep = newtype = TREE_TYPE (newdecl); 937 if (oldtype == error_mark_node || newtype == error_mark_node) 938 return false; 939 940 /* Two different categories of symbol altogether. This is an error 941 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */ 942 if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) 943 { 944 if (TREE_CODE (olddecl) != FUNCTION_DECL 945 || !DECL_BUILT_IN (olddecl) || !C_DECL_INVISIBLE (olddecl)) 946 { 947 error ("%J'%D' redeclared as different kind of symbol", 948 newdecl, newdecl); 949 locate_old_decl (olddecl, error); 950 } 951 else if (TREE_PUBLIC (newdecl)) 952 warning ("%Jbuilt-in function '%D' declared as non-function", 953 newdecl, newdecl); 954 else if (warn_shadow) 955 warning ("%Jshadowing built-in function '%D'", 956 newdecl, newdecl); 957 return false; 958 } 959 960 if (!comptypes (oldtype, newtype, COMPARE_STRICT)) 961 { 962 if (TREE_CODE (olddecl) == FUNCTION_DECL 963 && DECL_BUILT_IN (olddecl) && C_DECL_INVISIBLE (olddecl)) 964 { 965 /* Accept harmless mismatch in function types. 966 This is for the ffs and fprintf builtins. */ 967 tree trytype = match_builtin_function_types (newtype, oldtype); 968 969 if (trytype && comptypes (newtype, trytype, COMPARE_STRICT)) 970 *oldtypep = oldtype = trytype; 971 else 972 { 973 /* If types don't match for a built-in, throw away the 974 built-in. No point in calling locate_old_decl here, it 975 won't print anything. */ 976 warning ("%Jconflicting types for built-in function '%D'", 977 newdecl, newdecl); 978 return false; 979 } 980 } 981 else if (TREE_CODE (olddecl) == FUNCTION_DECL 982 && DECL_SOURCE_LINE (olddecl) == 0) 983 { 984 /* A conflicting function declaration for a predeclared 985 function that isn't actually built in. Objective C uses 986 these. The new declaration silently overrides everything 987 but the volatility (i.e. noreturn) indication. See also 988 below. FIXME: Make Objective C use normal builtins. */ 989 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 990 return false; 991 } 992 /* Permit void foo (...) to match int foo (...) if the latter is 993 the definition and implicit int was used. See 994 c-torture/compile/920625-2.c. */ 995 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) 996 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node 997 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node 998 && C_FUNCTION_IMPLICIT_INT (newdecl)) 999 { 1000 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl); 1001 /* Make sure we keep void as the return type. */ 1002 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype; 1003 C_FUNCTION_IMPLICIT_INT (newdecl) = 0; 1004 pedwarned = true; 1005 } 1006 else 1007 { 1008 error ("%Jconflicting types for '%D'", newdecl, newdecl); 1009 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype); 1010 locate_old_decl (olddecl, error); 1011 return false; 1012 } 1013 } 1014 1015 /* Redeclaration of a type is a constraint violation (6.7.2.3p1), 1016 but silently ignore the redeclaration if either is in a system 1017 header. (Conflicting redeclarations were handled above.) */ 1018 if (TREE_CODE (newdecl) == TYPE_DECL) 1019 { 1020 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl)) 1021 return true; /* allow OLDDECL to continue in use */ 1022 1023 error ("%Jredefinition of typedef '%D'", newdecl, newdecl); 1024 locate_old_decl (olddecl, error); 1025 return false; 1026 } 1027 1028 /* Function declarations can either be 'static' or 'extern' (no 1029 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore 1030 can never conflict with each other on account of linkage (6.2.2p4). 1031 Multiple definitions are not allowed (6.9p3,5) but GCC permits 1032 two definitions if one is 'extern inline' and one is not. The non- 1033 extern-inline definition supersedes the extern-inline definition. */ 1034 else if (TREE_CODE (newdecl) == FUNCTION_DECL) 1035 { 1036 /* If you declare a built-in function name as static, or 1037 define the built-in with an old-style definition (so we 1038 can't validate the argument list) the built-in definition is 1039 overridden, but optionally warn this was a bad choice of name. */ 1040 if (DECL_BUILT_IN (olddecl) 1041 && C_DECL_INVISIBLE (olddecl) 1042 && (!TREE_PUBLIC (newdecl) 1043 || (DECL_INITIAL (newdecl) 1044 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl))))) 1045 { 1046 if (warn_shadow) 1047 warning ("%Jshadowing built-in function '%D'", newdecl, newdecl); 1048 /* Discard the old built-in function. */ 1049 return false; 1050 } 1051 1052 if (DECL_INITIAL (newdecl)) 1053 { 1054 if (DECL_INITIAL (olddecl) 1055 && !(DECL_DECLARED_INLINE_P (olddecl) 1056 && DECL_EXTERNAL (olddecl) 1057 && !(DECL_DECLARED_INLINE_P (newdecl) 1058 && DECL_EXTERNAL (newdecl)))) 1059 { 1060 error ("%Jredefinition of '%D'", newdecl, newdecl); 1061 locate_old_decl (olddecl, error); 1062 return false; 1063 } 1064 } 1065 /* If we have a prototype after an old-style function definition, 1066 the argument types must be checked specially. */ 1067 else if (DECL_INITIAL (olddecl) 1068 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype) 1069 && TYPE_ACTUAL_ARG_TYPES (oldtype) 1070 && !validate_proto_after_old_defn (newdecl, newtype, oldtype)) 1071 { 1072 locate_old_decl (olddecl, error); 1073 return false; 1074 } 1075 /* Mismatched non-static and static is considered poor style. 1076 We only diagnose static then non-static if -Wtraditional, 1077 because it is the most convenient way to get some effects 1078 (see e.g. what unwind-dw2-fde-glibc.c does to the definition 1079 of _Unwind_Find_FDE in unwind-dw2-fde.c). Revisit? */ 1080 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl)) 1081 { 1082 /* A static function declaration for a predeclared function 1083 that isn't actually built in, silently overrides the 1084 default. Objective C uses these. See also above. 1085 FIXME: Make Objective C use normal builtins. */ 1086 if (TREE_CODE (olddecl) == FUNCTION_DECL 1087 && DECL_SOURCE_LINE (olddecl) == 0) 1088 return false; 1089 else 1090 { 1091 warning ("%Jstatic declaration of '%D' follows " 1092 "non-static declaration", newdecl, newdecl); 1093 warned = true; 1094 } 1095 } 1096 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl) 1097 && warn_traditional) 1098 { 1099 warning ("%Jnon-static declaration of '%D' follows " 1100 "static declaration", newdecl, newdecl); 1101 warned = true; 1102 } 1103 } 1104 else if (TREE_CODE (newdecl) == VAR_DECL) 1105 { 1106 /* Only variables can be thread-local, and all declarations must 1107 agree on this property. */ 1108 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl)) 1109 { 1110 if (DECL_THREAD_LOCAL (newdecl)) 1111 error ("%Jthread-local declaration of '%D' follows " 1112 "non-thread-local declaration", newdecl, newdecl); 1113 else 1114 error ("%Jnon-thread-local declaration of '%D' follows " 1115 "thread-local declaration", newdecl, newdecl); 1116 1117 locate_old_decl (olddecl, error); 1118 return false; 1119 } 1120 1121 /* Multiple initialized definitions are not allowed (6.9p3,5). */ 1122 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl)) 1123 { 1124 error ("%Jredefinition of '%D'", newdecl, newdecl); 1125 locate_old_decl (olddecl, error); 1126 return false; 1127 } 1128 1129 /* Objects declared at file scope: if at least one is 'extern', 1130 it's fine (6.2.2p4); otherwise the linkage must agree (6.2.2p7). */ 1131 if (DECL_FILE_SCOPE_P (newdecl)) 1132 { 1133 if (!DECL_EXTERNAL (newdecl) 1134 && !DECL_EXTERNAL (olddecl) 1135 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl)) 1136 { 1137 if (TREE_PUBLIC (newdecl)) 1138 error ("%Jnon-static declaration of '%D' follows " 1139 "static declaration", newdecl, newdecl); 1140 else 1141 error ("%Jstatic declaration of '%D' follows " 1142 "non-static declaration", newdecl, newdecl); 1143 1144 locate_old_decl (olddecl, error); 1145 return false; 1146 } 1147 } 1148 /* Two objects with the same name declared at the same block 1149 scope must both be external references (6.7p3). */ 1150 else if (DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl) 1151 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl))) 1152 { 1153 if (DECL_EXTERNAL (newdecl)) 1154 error ("%Jextern declaration of '%D' follows " 1155 "declaration with no linkage", newdecl, newdecl); 1156 else if (DECL_EXTERNAL (olddecl)) 1157 error ("%Jdeclaration of '%D' with no linkage follows " 1158 "extern declaration", newdecl, newdecl); 1159 else 1160 error ("%Jredeclaration of '%D' with no linkage", 1161 newdecl, newdecl); 1162 1163 locate_old_decl (olddecl, error); 1164 return false; 1165 } 1166 } 1167 1168 /* warnings */ 1169 /* All decls must agree on a non-default visibility. */ 1170 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT 1171 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT 1172 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) 1173 { 1174 warning ("%Jredeclaration of '%D' with different visibility " 1175 "(old visibility preserved)", newdecl, newdecl); 1176 warned = true; 1177 } 1178 1179 if (TREE_CODE (newdecl) == FUNCTION_DECL) 1180 { 1181 /* Diagnose inline __attribute__ ((noinline)) which is silly. */ 1182 if (DECL_DECLARED_INLINE_P (newdecl) 1183 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 1184 { 1185 warning ("%Jinline declaration of '%D' follows " 1186 "declaration with attribute noinline", newdecl, newdecl); 1187 warned = true; 1188 } 1189 else if (DECL_DECLARED_INLINE_P (olddecl) 1190 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) 1191 { 1192 warning ("%Jdeclaration of '%D' with attribute noinline follows " 1193 "inline declaration ", newdecl, newdecl); 1194 warned = true; 1195 } 1196 1197 /* Inline declaration after use or definition. 1198 ??? Should we still warn about this now we have unit-at-a-time 1199 mode and can get it right? */ 1200 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)) 1201 { 1202 if (TREE_USED (olddecl)) 1203 { 1204 warning ("%J'%D' declared inline after being called", 1205 olddecl, olddecl); 1206 warned = true; 1207 } 1208 else if (DECL_INITIAL (olddecl)) 1209 { 1210 warning ("%J'%D' declared inline after its definition", 1211 olddecl, olddecl); 1212 warned = true; 1213 } 1214 } 1215 } 1216 else /* PARM_DECL, VAR_DECL */ 1217 { 1218 /* Redeclaration of a PARM_DECL is invalid unless this is the 1219 real position of a forward-declared parameter (GCC extension). */ 1220 if (TREE_CODE (newdecl) == PARM_DECL 1221 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl))) 1222 { 1223 error ("%Jredefinition of parameter '%D'", newdecl, newdecl); 1224 locate_old_decl (olddecl, error); 1225 return false; 1226 } 1227 1228 /* These bits are only type qualifiers when applied to objects. */ 1229 if (TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)) 1230 { 1231 if (TREE_THIS_VOLATILE (newdecl)) 1232 pedwarn ("%Jvolatile declaration of '%D' follows " 1233 "non-volatile declaration", newdecl, newdecl); 1234 else 1235 pedwarn ("%Jnon-volatile declaration of '%D' follows " 1236 "volatile declaration", newdecl, newdecl); 1237 pedwarned = true; 1238 } 1239 if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)) 1240 { 1241 if (TREE_READONLY (newdecl)) 1242 pedwarn ("%Jconst declaration of '%D' follows " 1243 "non-const declaration", newdecl, newdecl); 1244 else 1245 pedwarn ("%Jnon-const declaration of '%D' follows " 1246 "const declaration", newdecl, newdecl); 1247 pedwarned = true; 1248 } 1249 } 1250 1251 /* Optional warning for completely redundant decls. */ 1252 if (!warned && !pedwarned 1253 && warn_redundant_decls 1254 /* Don't warn about a function declaration followed by a 1255 definition. */ 1256 && !(TREE_CODE (newdecl) == FUNCTION_DECL 1257 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)) 1258 /* Don't warn about redundant redeclarations of builtins. */ 1259 && !(TREE_CODE (newdecl) == FUNCTION_DECL 1260 && !DECL_BUILT_IN (newdecl) 1261 && DECL_BUILT_IN (olddecl) 1262 && C_DECL_INVISIBLE (olddecl)) 1263 /* Don't warn about an extern followed by a definition. */ 1264 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl)) 1265 /* Don't warn about forward parameter decls. */ 1266 && !(TREE_CODE (newdecl) == PARM_DECL 1267 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))) 1268 { 1269 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl); 1270 warned = true; 1271 } 1272 1273 /* Report location of previous decl/defn in a consistent manner. */ 1274 if (warned || pedwarned) 1275 locate_old_decl (olddecl, pedwarned ? pedwarn : warning); 1276 1277 return true; 1278} 1279 1280/* Subroutine of duplicate_decls. NEWDECL has been found to be 1281 consistent with OLDDECL, but carries new information. Merge the 1282 new information into OLDDECL. This function issues no 1283 diagnostics. */ 1284 1285static void 1286merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype) 1287{ 1288 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL 1289 && DECL_INITIAL (newdecl) != 0); 1290 1291 /* For real parm decl following a forward decl, return 1 so old decl 1292 will be reused. Only allow this to happen once. */ 1293 if (TREE_CODE (newdecl) == PARM_DECL 1294 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl)) 1295 { 1296 TREE_ASM_WRITTEN (olddecl) = 0; 1297 return; 1298 } 1299 1300 DECL_ATTRIBUTES (newdecl) 1301 = (*targetm.merge_decl_attributes) (olddecl, newdecl); 1302 1303 /* Merge the data types specified in the two decls. */ 1304 TREE_TYPE (newdecl) 1305 = TREE_TYPE (olddecl) 1306 = common_type (newtype, oldtype); 1307 1308 /* Lay the type out, unless already done. */ 1309 if (oldtype != TREE_TYPE (newdecl)) 1310 { 1311 if (TREE_TYPE (newdecl) != error_mark_node) 1312 layout_type (TREE_TYPE (newdecl)); 1313 if (TREE_CODE (newdecl) != FUNCTION_DECL 1314 && TREE_CODE (newdecl) != TYPE_DECL 1315 && TREE_CODE (newdecl) != CONST_DECL) 1316 layout_decl (newdecl, 0); 1317 } 1318 else 1319 { 1320 /* Since the type is OLDDECL's, make OLDDECL's size go with. */ 1321 DECL_SIZE (newdecl) = DECL_SIZE (olddecl); 1322 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl); 1323 DECL_MODE (newdecl) = DECL_MODE (olddecl); 1324 if (TREE_CODE (olddecl) != FUNCTION_DECL) 1325 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl)) 1326 { 1327 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl); 1328 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl); 1329 } 1330 } 1331 1332 /* Keep the old rtl since we can safely use it. */ 1333 COPY_DECL_RTL (olddecl, newdecl); 1334 1335 /* Merge the type qualifiers. */ 1336 if (TREE_READONLY (newdecl)) 1337 TREE_READONLY (olddecl) = 1; 1338 1339 if (TREE_THIS_VOLATILE (newdecl)) 1340 { 1341 TREE_THIS_VOLATILE (olddecl) = 1; 1342 if (TREE_CODE (newdecl) == VAR_DECL) 1343 make_var_volatile (newdecl); 1344 } 1345 1346 /* Keep source location of definition rather than declaration. */ 1347 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0) 1348 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); 1349 1350 /* Merge the unused-warning information. */ 1351 if (DECL_IN_SYSTEM_HEADER (olddecl)) 1352 DECL_IN_SYSTEM_HEADER (newdecl) = 1; 1353 else if (DECL_IN_SYSTEM_HEADER (newdecl)) 1354 DECL_IN_SYSTEM_HEADER (olddecl) = 1; 1355 1356 /* Merge the initialization information. */ 1357 if (DECL_INITIAL (newdecl) == 0) 1358 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 1359 1360 /* Merge the section attribute. 1361 We want to issue an error if the sections conflict but that must be 1362 done later in decl_attributes since we are called before attributes 1363 are assigned. */ 1364 if (DECL_SECTION_NAME (newdecl) == NULL_TREE) 1365 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); 1366 1367 /* Copy the assembler name. 1368 Currently, it can only be defined in the prototype. */ 1369 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); 1370 1371 /* If either declaration has a nondefault visibility, use it. */ 1372 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT) 1373 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); 1374 1375 if (TREE_CODE (newdecl) == FUNCTION_DECL) 1376 { 1377 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl); 1378 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); 1379 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); 1380 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) 1381 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); 1382 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1383 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); 1384 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); 1385 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); 1386 } 1387 1388 /* Merge the storage class information. */ 1389 merge_weak (newdecl, olddecl); 1390 1391 /* For functions, static overrides non-static. */ 1392 if (TREE_CODE (newdecl) == FUNCTION_DECL) 1393 { 1394 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl); 1395 /* This is since we don't automatically 1396 copy the attributes of NEWDECL into OLDDECL. */ 1397 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 1398 /* If this clears `static', clear it in the identifier too. */ 1399 if (! TREE_PUBLIC (olddecl)) 1400 TREE_PUBLIC (DECL_NAME (olddecl)) = 0; 1401 } 1402 if (DECL_EXTERNAL (newdecl)) 1403 { 1404 TREE_STATIC (newdecl) = TREE_STATIC (olddecl); 1405 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl); 1406 1407 /* An extern decl does not override previous storage class. */ 1408 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); 1409 if (! DECL_EXTERNAL (newdecl)) 1410 { 1411 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); 1412 DECL_COMMON (newdecl) = DECL_COMMON (olddecl); 1413 } 1414 } 1415 else 1416 { 1417 TREE_STATIC (olddecl) = TREE_STATIC (newdecl); 1418 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); 1419 } 1420 1421 if (TREE_CODE (newdecl) == FUNCTION_DECL) 1422 { 1423 /* If we're redefining a function previously defined as extern 1424 inline, make sure we emit debug info for the inline before we 1425 throw it away, in case it was inlined into a function that hasn't 1426 been written out yet. */ 1427 if (new_is_definition && DECL_INITIAL (olddecl)) 1428 { 1429 if (TREE_USED (olddecl) 1430 /* In unit-at-a-time mode we never inline re-defined extern 1431 inline functions. */ 1432 && !flag_unit_at_a_time 1433 && cgraph_function_possibly_inlined_p (olddecl)) 1434 (*debug_hooks->outlining_inline_function) (olddecl); 1435 1436 /* The new defn must not be inline. */ 1437 DECL_INLINE (newdecl) = 0; 1438 DECL_UNINLINABLE (newdecl) = 1; 1439 } 1440 else 1441 { 1442 /* If either decl says `inline', this fn is inline, 1443 unless its definition was passed already. */ 1444 if (DECL_DECLARED_INLINE_P (newdecl) 1445 || DECL_DECLARED_INLINE_P (olddecl)) 1446 DECL_DECLARED_INLINE_P (newdecl) = 1; 1447 1448 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) 1449 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); 1450 } 1451 1452 if (DECL_BUILT_IN (olddecl)) 1453 { 1454 /* If redeclaring a builtin function, it stays built in. */ 1455 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); 1456 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); 1457 } 1458 1459 /* Also preserve various other info from the definition. */ 1460 if (! new_is_definition) 1461 { 1462 DECL_RESULT (newdecl) = DECL_RESULT (olddecl); 1463 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 1464 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl); 1465 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); 1466 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); 1467 1468 /* Set DECL_INLINE on the declaration if we've got a body 1469 from which to instantiate. */ 1470 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl)) 1471 { 1472 DECL_INLINE (newdecl) = 1; 1473 DECL_ABSTRACT_ORIGIN (newdecl) 1474 = DECL_ABSTRACT_ORIGIN (olddecl); 1475 } 1476 } 1477 else 1478 { 1479 /* If a previous declaration said inline, mark the 1480 definition as inlinable. */ 1481 if (DECL_DECLARED_INLINE_P (newdecl) 1482 && ! DECL_UNINLINABLE (newdecl)) 1483 DECL_INLINE (newdecl) = 1; 1484 } 1485 } 1486 1487 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL. 1488 But preserve OLDDECL's DECL_UID and C_DECL_INVISIBLE. */ 1489 { 1490 unsigned olddecl_uid = DECL_UID (olddecl); 1491 unsigned olddecl_invisible = C_DECL_INVISIBLE (olddecl); 1492 1493 memcpy ((char *) olddecl + sizeof (struct tree_common), 1494 (char *) newdecl + sizeof (struct tree_common), 1495 sizeof (struct tree_decl) - sizeof (struct tree_common)); 1496 DECL_UID (olddecl) = olddecl_uid; 1497 C_DECL_INVISIBLE (olddecl) = olddecl_invisible; 1498 } 1499 1500 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl 1501 so that encode_section_info has a chance to look at the new decl 1502 flags and attributes. */ 1503 if (DECL_RTL_SET_P (olddecl) 1504 && (TREE_CODE (olddecl) == FUNCTION_DECL 1505 || (TREE_CODE (olddecl) == VAR_DECL 1506 && TREE_STATIC (olddecl)))) 1507 make_decl_rtl (olddecl, NULL); 1508} 1509 1510/* Handle when a new declaration NEWDECL has the same name as an old 1511 one OLDDECL in the same binding contour. Prints an error message 1512 if appropriate. 1513 1514 If safely possible, alter OLDDECL to look like NEWDECL, and return 1515 true. Otherwise, return false. */ 1516 1517static bool 1518duplicate_decls (tree newdecl, tree olddecl) 1519{ 1520 tree newtype, oldtype; 1521 1522 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype)) 1523 return false; 1524 1525 merge_decls (newdecl, olddecl, newtype, oldtype); 1526 return true; 1527} 1528 1529 1530/* Return any external DECL associated with ID, whether or not it is 1531 currently in scope. */ 1532 1533static tree 1534any_external_decl (tree id) 1535{ 1536 tree decl = IDENTIFIER_SYMBOL_VALUE (id); 1537 tree t; 1538 1539 if (decl == 0 || TREE_CODE (decl) == ERROR_MARK) 1540 return 0; 1541 else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl)) 1542 return decl; 1543 1544 t = purpose_member (id, truly_local_externals); 1545 if (t) 1546 return TREE_VALUE (t); 1547 1548 return 0; 1549} 1550 1551/* Record an external decl DECL. This only does something if a 1552 shadowing decl already exists. */ 1553static void 1554record_external_decl (tree decl) 1555{ 1556 tree name = DECL_NAME (decl); 1557 if (!IDENTIFIER_SYMBOL_VALUE (name)) 1558 return; 1559 1560 truly_local_externals = tree_cons (name, decl, truly_local_externals); 1561} 1562 1563/* Check whether decl-node X shadows an existing declaration. 1564 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X, 1565 which might be a NULL_TREE. */ 1566static void 1567warn_if_shadowing (tree x, tree old) 1568{ 1569 /* Nothing to shadow? */ 1570 if (old == 0 1571 /* Shadow warnings not wanted? */ 1572 || !warn_shadow 1573 /* No shadow warnings for internally generated vars. */ 1574 || DECL_SOURCE_LINE (x) == 0 1575 /* No shadow warnings for vars made for inlining. */ 1576 || DECL_FROM_INLINE (x) 1577 /* Don't warn about the parm names in function declarator 1578 within a function declarator. 1579 It would be nice to avoid warning in any function 1580 declarator in a declaration, as opposed to a definition, 1581 but there is no way to tell it's not a definition. */ 1582 || (TREE_CODE (x) == PARM_DECL && current_scope->outer->parm_flag) 1583 /* Shadow warnings only apply to local variables and parameters. */ 1584 || (TREE_CODE (x) != PARM_DECL && DECL_FILE_SCOPE_P (x))) 1585 return; 1586 1587 if (TREE_CODE (old) == PARM_DECL) 1588 warning ("%Jdeclaration of '%D' shadows a parameter", x, x); 1589 else if (DECL_FILE_SCOPE_P (old)) 1590 warning ("%Jdeclaration of '%D' shadows a global declaration", x, x); 1591 else 1592 warning ("%Jdeclaration of '%D' shadows a previous local", x, x); 1593 1594 warning ("%Jshadowed declaration is here", old); 1595} 1596 1597 1598/* Subroutine of pushdecl. 1599 1600 X is a TYPE_DECL for a typedef statement. Create a brand new 1601 ..._TYPE node (which will be just a variant of the existing 1602 ..._TYPE node with identical properties) and then install X 1603 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. 1604 1605 The whole point here is to end up with a situation where each 1606 and every ..._TYPE node the compiler creates will be uniquely 1607 associated with AT MOST one node representing a typedef name. 1608 This way, even though the compiler substitutes corresponding 1609 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very 1610 early on, later parts of the compiler can always do the reverse 1611 translation and get back the corresponding typedef name. For 1612 example, given: 1613 1614 typedef struct S MY_TYPE; 1615 MY_TYPE object; 1616 1617 Later parts of the compiler might only know that `object' was of 1618 type `struct S' if it were not for code just below. With this 1619 code however, later parts of the compiler see something like: 1620 1621 struct S' == struct S 1622 typedef struct S' MY_TYPE; 1623 struct S' object; 1624 1625 And they can then deduce (from the node for type struct S') that 1626 the original object declaration was: 1627 1628 MY_TYPE object; 1629 1630 Being able to do this is important for proper support of protoize, 1631 and also for generating precise symbolic debugging information 1632 which takes full account of the programmer's (typedef) vocabulary. 1633 1634 Obviously, we don't want to generate a duplicate ..._TYPE node if 1635 the TYPE_DECL node that we are now processing really represents a 1636 standard built-in type. 1637 1638 Since all standard types are effectively declared at line zero 1639 in the source file, we can easily check to see if we are working 1640 on a standard type by checking the current value of lineno. */ 1641 1642static void 1643clone_underlying_type (tree x) 1644{ 1645 if (DECL_SOURCE_LINE (x) == 0) 1646 { 1647 if (TYPE_NAME (TREE_TYPE (x)) == 0) 1648 TYPE_NAME (TREE_TYPE (x)) = x; 1649 } 1650 else if (TREE_TYPE (x) != error_mark_node 1651 && DECL_ORIGINAL_TYPE (x) == NULL_TREE) 1652 { 1653 tree tt = TREE_TYPE (x); 1654 DECL_ORIGINAL_TYPE (x) = tt; 1655 tt = build_type_copy (tt); 1656 TYPE_NAME (tt) = x; 1657 TREE_USED (tt) = TREE_USED (x); 1658 TREE_TYPE (x) = tt; 1659 } 1660} 1661 1662/* Record a decl-node X as belonging to the current lexical scope. 1663 Check for errors (such as an incompatible declaration for the same 1664 name already seen in the same scope). 1665 1666 Returns either X or an old decl for the same name. 1667 If an old decl is returned, it may have been smashed 1668 to agree with what X says. */ 1669 1670tree 1671pushdecl (tree x) 1672{ 1673 tree name = DECL_NAME (x); 1674 struct c_scope *scope = current_scope; 1675 1676#ifdef ENABLE_CHECKING 1677 if (error_mark_node == 0) 1678 /* Called too early. */ 1679 abort (); 1680#endif 1681 1682 /* Functions need the lang_decl data. */ 1683 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x)) 1684 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl)); 1685 1686 /* A local extern declaration for a function doesn't constitute nesting. 1687 A local auto declaration does, since it's a forward decl 1688 for a nested function coming later. */ 1689 if (current_function_decl == NULL 1690 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL) 1691 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x))) 1692 DECL_CONTEXT (x) = current_file_decl; 1693 else 1694 DECL_CONTEXT (x) = current_function_decl; 1695 1696 if (name) 1697 { 1698 tree old; 1699 1700 if (warn_nested_externs 1701 && scope != global_scope 1702 && DECL_EXTERNAL (x) 1703 && !DECL_IN_SYSTEM_HEADER (x)) 1704 warning ("nested extern declaration of `%s'", 1705 IDENTIFIER_POINTER (name)); 1706 1707 old = lookup_name_current_level (name); 1708 if (old && duplicate_decls (x, old)) 1709 { 1710 /* For PARM_DECLs, old may be a forward declaration. 1711 If so, we want to remove it from its old location 1712 (in the variables chain) and rechain it in the 1713 location given by the new declaration. */ 1714 if (TREE_CODE (x) == PARM_DECL) 1715 { 1716 tree *p; 1717 for (p = &scope->names; *p; p = &TREE_CHAIN (*p)) 1718 if (*p == old) 1719 { 1720 *p = TREE_CHAIN (old); 1721 SCOPE_LIST_APPEND (scope, parms, old); 1722 break; 1723 } 1724 } 1725 return old; 1726 } 1727 if (DECL_EXTERNAL (x) || scope == global_scope) 1728 { 1729 /* Find and check against a previous, not-in-scope, external 1730 decl for this identifier. (C99 6.2.7p2: All declarations 1731 that refer to the same object or function shall have 1732 compatible type; otherwise, the behavior is undefined.) */ 1733 tree ext = any_external_decl (name); 1734 if (ext) 1735 { 1736 if (duplicate_decls (x, ext)) 1737 x = copy_node (ext); 1738 } 1739 else 1740 record_external_decl (x); 1741 } 1742 1743 if (TREE_CODE (x) == TYPE_DECL) 1744 clone_underlying_type (x); 1745 1746 /* If storing a local value, there may already be one 1747 (inherited). If so, record it for restoration when this 1748 scope ends. Take care not to do this if we are replacing an 1749 older decl in the same scope (i.e. duplicate_decls returned 1750 false, above). */ 1751 if (scope != global_scope) 1752 { 1753 tree inherited_decl = lookup_name (name); 1754 if (inherited_decl && inherited_decl != old) 1755 { 1756 warn_if_shadowing (x, inherited_decl); 1757 scope->shadowed = tree_cons (name, inherited_decl, 1758 scope->shadowed); 1759 } 1760 } 1761 1762 /* Install the new declaration in the requested scope. */ 1763 IDENTIFIER_SYMBOL_VALUE (name) = x; 1764 C_DECL_INVISIBLE (x) = 0; 1765 1766 /* If x's type is incomplete because it's based on a 1767 structure or union which has not yet been fully declared, 1768 attach it to that structure or union type, so we can go 1769 back and complete the variable declaration later, if the 1770 structure or union gets fully declared. 1771 1772 If the input is erroneous, we can have error_mark in the type 1773 slot (e.g. "f(void a, ...)") - that doesn't count as an 1774 incomplete type. */ 1775 if (TREE_TYPE (x) != error_mark_node 1776 && !COMPLETE_TYPE_P (TREE_TYPE (x))) 1777 { 1778 tree element = TREE_TYPE (x); 1779 1780 while (TREE_CODE (element) == ARRAY_TYPE) 1781 element = TREE_TYPE (element); 1782 element = TYPE_MAIN_VARIANT (element); 1783 1784 if ((TREE_CODE (element) == RECORD_TYPE 1785 || TREE_CODE (element) == UNION_TYPE) 1786 && (TREE_CODE (x) != TYPE_DECL 1787 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE) 1788 && !COMPLETE_TYPE_P (element)) 1789 C_TYPE_INCOMPLETE_VARS (element) 1790 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element)); 1791 } 1792 } 1793 1794 if (TREE_CODE (x) == PARM_DECL) 1795 SCOPE_LIST_APPEND (scope, parms, x); 1796 else 1797 SCOPE_LIST_APPEND (scope, names, x); 1798 1799 return x; 1800} 1801 1802/* Record X as belonging to the global scope (C99 "file scope"). 1803 This is used only internally by the Objective-C front end, 1804 and is limited to its needs. duplicate_decls is not called; 1805 if there is any preexisting decl for this identifier, it is an ICE. */ 1806 1807tree 1808pushdecl_top_level (tree x) 1809{ 1810 tree name; 1811 1812 if (TREE_CODE (x) != VAR_DECL) 1813 abort (); 1814 1815 name = DECL_NAME (x); 1816 1817 if (IDENTIFIER_SYMBOL_VALUE (name)) 1818 abort (); 1819 1820 DECL_CONTEXT (x) = current_file_decl; 1821 IDENTIFIER_SYMBOL_VALUE (name) = x; 1822 1823 SCOPE_LIST_APPEND (global_scope, names, x); 1824 return x; 1825} 1826 1827/* Generate an implicit declaration for identifier FUNCTIONID as a 1828 function of type int (). */ 1829 1830tree 1831implicitly_declare (tree functionid) 1832{ 1833 tree decl = any_external_decl (functionid); 1834 1835 if (decl) 1836 { 1837 /* Implicit declaration of a function already declared 1838 (somehow) in a different scope, or as a built-in. 1839 If this is the first time this has happened, warn; 1840 then recycle the old declaration. */ 1841 if (!C_DECL_IMPLICIT (decl)) 1842 { 1843 implicit_decl_warning (DECL_NAME (decl)); 1844 if (! DECL_FILE_SCOPE_P (decl)) 1845 warning ("%Jprevious declaration of '%D'", decl, decl); 1846 C_DECL_IMPLICIT (decl) = 1; 1847 } 1848 /* If this function is global, then it must already be in the 1849 global scope, so there's no need to push it again. */ 1850 if (current_scope == global_scope) 1851 return decl; 1852 /* If this is a local declaration, make a copy; we can't have 1853 the same DECL listed in two different scopes. */ 1854 return pushdecl (copy_node (decl)); 1855 } 1856 1857 /* Not seen before. */ 1858 decl = build_decl (FUNCTION_DECL, functionid, default_function_type); 1859 DECL_EXTERNAL (decl) = 1; 1860 TREE_PUBLIC (decl) = 1; 1861 C_DECL_IMPLICIT (decl) = 1; 1862 implicit_decl_warning (functionid); 1863 1864 /* C89 says implicit declarations are in the innermost block. 1865 So we record the decl in the standard fashion. */ 1866 decl = pushdecl (decl); 1867 1868 /* No need to call objc_check_decl here - it's a function type. */ 1869 rest_of_decl_compilation (decl, NULL, 0, 0); 1870 1871 /* Write a record describing this implicit function declaration 1872 to the prototypes file (if requested). */ 1873 gen_aux_info_record (decl, 0, 1, 0); 1874 1875 /* Possibly apply some default attributes to this implicit declaration. */ 1876 decl_attributes (&decl, NULL_TREE, 0); 1877 1878 return decl; 1879} 1880 1881static void 1882implicit_decl_warning (tree id) 1883{ 1884 const char *name = IDENTIFIER_POINTER (id); 1885 if (mesg_implicit_function_declaration == 2) 1886 error ("implicit declaration of function `%s'", name); 1887 else if (mesg_implicit_function_declaration == 1) 1888 warning ("implicit declaration of function `%s'", name); 1889} 1890 1891/* Issue an error message for a reference to an undeclared variable 1892 ID, including a reference to a builtin outside of function-call 1893 context. Establish a binding of the identifier to error_mark_node 1894 in an appropriate scope, which will suppress further errors for the 1895 same identifier. */ 1896void 1897undeclared_variable (tree id) 1898{ 1899 static bool already = false; 1900 struct c_scope *scope; 1901 1902 if (current_function_decl == 0) 1903 { 1904 error ("`%s' undeclared here (not in a function)", 1905 IDENTIFIER_POINTER (id)); 1906 scope = current_scope; 1907 } 1908 else 1909 { 1910 error ("`%s' undeclared (first use in this function)", 1911 IDENTIFIER_POINTER (id)); 1912 1913 if (! already) 1914 { 1915 error ("(Each undeclared identifier is reported only once"); 1916 error ("for each function it appears in.)"); 1917 already = true; 1918 } 1919 1920 scope = current_function_scope; 1921 } 1922 1923 scope->shadowed = tree_cons (id, IDENTIFIER_SYMBOL_VALUE (id), 1924 scope->shadowed); 1925 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node; 1926} 1927 1928/* Subroutine of lookup_label, declare_label, define_label: construct a 1929 LABEL_DECL with all the proper frills. */ 1930 1931static tree 1932make_label (tree name, location_t location) 1933{ 1934 tree label = build_decl (LABEL_DECL, name, void_type_node); 1935 1936 DECL_CONTEXT (label) = current_function_decl; 1937 DECL_MODE (label) = VOIDmode; 1938 DECL_SOURCE_LOCATION (label) = location; 1939 1940 return label; 1941} 1942 1943/* Another subroutine of lookup_label, declare_label, define_label: 1944 set up the binding of name to LABEL_DECL in the given SCOPE. */ 1945 1946static void 1947bind_label (tree name, tree label, struct c_scope *scope) 1948{ 1949 if (IDENTIFIER_LABEL_VALUE (name)) 1950 scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name), 1951 scope->shadowed); 1952 IDENTIFIER_LABEL_VALUE (name) = label; 1953 1954 SCOPE_LIST_APPEND (scope, names, label); 1955} 1956 1957/* Get the LABEL_DECL corresponding to identifier NAME as a label. 1958 Create one if none exists so far for the current function. 1959 This is called when a label is used in a goto expression or 1960 has its address taken. */ 1961 1962tree 1963lookup_label (tree name) 1964{ 1965 tree label; 1966 1967 if (current_function_decl == 0) 1968 { 1969 error ("label %s referenced outside of any function", 1970 IDENTIFIER_POINTER (name)); 1971 return 0; 1972 } 1973 1974 /* Use a label already defined or ref'd with this name, but not if 1975 it is inherited from a containing function and wasn't declared 1976 using __label__. */ 1977 label = IDENTIFIER_LABEL_VALUE (name); 1978 if (label && (DECL_CONTEXT (label) == current_function_decl 1979 || C_DECLARED_LABEL_FLAG (label))) 1980 { 1981 /* If the label has only been declared, update its apparent 1982 location to point here, for better diagnostics if it 1983 turns out not to have been defined. */ 1984 if (!TREE_USED (label)) 1985 DECL_SOURCE_LOCATION (label) = input_location; 1986 return label; 1987 } 1988 1989 /* No label binding for that identifier; make one. */ 1990 label = make_label (name, input_location); 1991 1992 /* Ordinary labels go in the current function scope. */ 1993 bind_label (name, label, current_function_scope); 1994 return label; 1995} 1996 1997/* Make a label named NAME in the current function, shadowing silently 1998 any that may be inherited from containing functions or containing 1999 scopes. This is called for __label__ declarations. */ 2000 2001/* Note that valid use, if the label being shadowed comes from another 2002 scope in the same function, requires calling declare_nonlocal_label 2003 right away. (Is this still true? -zw 2003-07-17) */ 2004 2005tree 2006declare_label (tree name) 2007{ 2008 tree label = IDENTIFIER_LABEL_VALUE (name); 2009 tree dup; 2010 2011 /* Check to make sure that the label hasn't already been declared 2012 at this scope */ 2013 for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup)) 2014 if (dup == label) 2015 { 2016 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name)); 2017 error ("%Jthis is a previous declaration", dup); 2018 2019 /* Just use the previous declaration. */ 2020 return dup; 2021 } 2022 2023 label = make_label (name, input_location); 2024 C_DECLARED_LABEL_FLAG (label) = 1; 2025 2026 /* Declared labels go in the current scope. */ 2027 bind_label (name, label, current_scope); 2028 return label; 2029} 2030 2031/* Define a label, specifying the location in the source file. 2032 Return the LABEL_DECL node for the label, if the definition is valid. 2033 Otherwise return 0. */ 2034 2035tree 2036define_label (location_t location, tree name) 2037{ 2038 tree label; 2039 2040 /* Find any preexisting label with this name. It is an error 2041 if that label has already been defined in this function, or 2042 if there is a containing function with a declared label with 2043 the same name. */ 2044 label = IDENTIFIER_LABEL_VALUE (name); 2045 2046 if (label 2047 && ((DECL_CONTEXT (label) == current_function_decl 2048 && DECL_INITIAL (label) != 0) 2049 || (DECL_CONTEXT (label) != current_function_decl 2050 && C_DECLARED_LABEL_FLAG (label)))) 2051 { 2052 error ("%Hduplicate label `%D'", &location, label); 2053 if (DECL_INITIAL (label)) 2054 error ("%J`%D' previously defined here", label, label); 2055 else 2056 error ("%J`%D' previously declared here", label, label); 2057 return 0; 2058 } 2059 else if (label && DECL_CONTEXT (label) == current_function_decl) 2060 { 2061 /* The label has been used or declared already in this function, 2062 but not defined. Update its location to point to this 2063 definition. */ 2064 DECL_SOURCE_LOCATION (label) = location; 2065 } 2066 else 2067 { 2068 /* No label binding for that identifier; make one. */ 2069 label = make_label (name, location); 2070 2071 /* Ordinary labels go in the current function scope. */ 2072 bind_label (name, label, current_function_scope); 2073 } 2074 2075 if (warn_traditional && !in_system_header && lookup_name (name)) 2076 warning ("%Htraditional C lacks a separate namespace for labels, " 2077 "identifier `%s' conflicts", &location, 2078 IDENTIFIER_POINTER (name)); 2079 2080 /* Mark label as having been defined. */ 2081 DECL_INITIAL (label) = error_mark_node; 2082 return label; 2083} 2084 2085/* Return the list of declarations of the current scope. */ 2086 2087tree 2088getdecls (void) 2089{ 2090 return current_scope->names; 2091} 2092 2093 2094/* Given NAME, an IDENTIFIER_NODE, 2095 return the structure (or union or enum) definition for that name. 2096 If THISLEVEL_ONLY is nonzero, searches only the current_scope. 2097 CODE says which kind of type the caller wants; 2098 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. 2099 If the wrong kind of type is found, an error is reported. */ 2100 2101static tree 2102lookup_tag (enum tree_code code, tree name, int thislevel_only) 2103{ 2104 tree tag = IDENTIFIER_TAG_VALUE (name); 2105 int thislevel = 0; 2106 2107 if (!tag) 2108 return 0; 2109 2110 /* We only care about whether it's in this level if 2111 thislevel_only was set or it might be a type clash. */ 2112 if (thislevel_only || TREE_CODE (tag) != code) 2113 { 2114 if (current_scope == global_scope 2115 || purpose_member (name, current_scope->tags)) 2116 thislevel = 1; 2117 } 2118 2119 if (thislevel_only && !thislevel) 2120 return 0; 2121 2122 if (TREE_CODE (tag) != code) 2123 { 2124 /* Definition isn't the kind we were looking for. */ 2125 pending_invalid_xref = name; 2126 pending_invalid_xref_location = input_location; 2127 2128 /* If in the same binding level as a declaration as a tag 2129 of a different type, this must not be allowed to 2130 shadow that tag, so give the error immediately. 2131 (For example, "struct foo; union foo;" is invalid.) */ 2132 if (thislevel) 2133 pending_xref_error (); 2134 } 2135 return tag; 2136} 2137 2138/* Print an error message now 2139 for a recent invalid struct, union or enum cross reference. 2140 We don't print them immediately because they are not invalid 2141 when used in the `struct foo;' construct for shadowing. */ 2142 2143void 2144pending_xref_error (void) 2145{ 2146 if (pending_invalid_xref != 0) 2147 error ("%H`%s' defined as wrong kind of tag", 2148 &pending_invalid_xref_location, 2149 IDENTIFIER_POINTER (pending_invalid_xref)); 2150 pending_invalid_xref = 0; 2151} 2152 2153 2154/* Look up NAME in the current scope and its superiors 2155 in the namespace of variables, functions and typedefs. 2156 Return a ..._DECL node of some kind representing its definition, 2157 or return 0 if it is undefined. */ 2158 2159tree 2160lookup_name (tree name) 2161{ 2162 tree decl = IDENTIFIER_SYMBOL_VALUE (name); 2163 if (decl == 0 || decl == error_mark_node) 2164 return decl; 2165 if (C_DECL_INVISIBLE (decl)) 2166 return 0; 2167 return decl; 2168} 2169 2170/* Similar to `lookup_name' but look only at the current scope. */ 2171 2172static tree 2173lookup_name_current_level (tree name) 2174{ 2175 tree decl = IDENTIFIER_SYMBOL_VALUE (name); 2176 2177 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl)) 2178 return 0; 2179 2180 if (current_scope == global_scope) 2181 return decl; 2182 2183 /* Scan the current scope for a decl with name NAME. 2184 For PARM_DECLs, we have to look at both ->parms and ->names, since 2185 forward parameter declarations wind up on the ->names list. */ 2186 if (TREE_CODE (decl) == PARM_DECL 2187 && chain_member (decl, current_scope->parms)) 2188 return decl; 2189 if (chain_member (decl, current_scope->names)) 2190 return decl; 2191 2192 return 0; 2193} 2194 2195/* Create the predefined scalar types of C, 2196 and some nodes representing standard constants (0, 1, (void *) 0). 2197 Initialize the global scope. 2198 Make definitions for built-in primitive functions. */ 2199 2200void 2201c_init_decl_processing (void) 2202{ 2203 tree endlink; 2204 tree ptr_ftype_void, ptr_ftype_ptr; 2205 location_t save_loc = input_location; 2206 2207 /* Adds some ggc roots, and reserved words for c-parse.in. */ 2208 c_parse_init (); 2209 2210 current_function_decl = 0; 2211 2212 /* Make the c_scope structure for global names. */ 2213 pushlevel (0); 2214 global_scope = current_scope; 2215 2216 /* Declarations from c_common_nodes_and_builtins must not be associated 2217 with this input file, lest we get differences between using and not 2218 using preprocessed headers. */ 2219 input_location.file = "<internal>"; 2220 input_location.line = 0; 2221 2222 /* Make the DECL for the toplevel file scope. */ 2223 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL); 2224 2225 build_common_tree_nodes (flag_signed_char); 2226 2227 c_common_nodes_and_builtins (); 2228 2229 /* In C, comparisons and TRUTH_* expressions have type int. */ 2230 truthvalue_type_node = integer_type_node; 2231 truthvalue_true_node = integer_one_node; 2232 truthvalue_false_node = integer_zero_node; 2233 2234 /* Even in C99, which has a real boolean type. */ 2235 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"), 2236 boolean_type_node)); 2237 2238 endlink = void_list_node; 2239 ptr_ftype_void = build_function_type (ptr_type_node, endlink); 2240 ptr_ftype_ptr 2241 = build_function_type (ptr_type_node, 2242 tree_cons (NULL_TREE, ptr_type_node, endlink)); 2243 2244 input_location = save_loc; 2245 2246 pedantic_lvalues = pedantic; 2247 2248 make_fname_decl = c_make_fname_decl; 2249 start_fname_decls (); 2250 2251 first_builtin_decl = global_scope->names; 2252 last_builtin_decl = global_scope->names_last; 2253} 2254 2255/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the 2256 decl, NAME is the initialization string and TYPE_DEP indicates whether 2257 NAME depended on the type of the function. As we don't yet implement 2258 delayed emission of static data, we mark the decl as emitted 2259 so it is not placed in the output. Anything using it must therefore pull 2260 out the STRING_CST initializer directly. FIXME. */ 2261 2262static tree 2263c_make_fname_decl (tree id, int type_dep) 2264{ 2265 const char *name = fname_as_string (type_dep); 2266 tree decl, type, init; 2267 size_t length = strlen (name); 2268 2269 type = build_array_type 2270 (build_qualified_type (char_type_node, TYPE_QUAL_CONST), 2271 build_index_type (size_int (length))); 2272 2273 decl = build_decl (VAR_DECL, id, type); 2274 2275 TREE_STATIC (decl) = 1; 2276 TREE_READONLY (decl) = 1; 2277 DECL_ARTIFICIAL (decl) = 1; 2278 2279 init = build_string (length + 1, name); 2280 TREE_TYPE (init) = type; 2281 DECL_INITIAL (decl) = init; 2282 2283 TREE_USED (decl) = 1; 2284 2285 if (current_function_decl) 2286 { 2287 DECL_CONTEXT (decl) = current_function_decl; 2288 IDENTIFIER_SYMBOL_VALUE (id) = decl; 2289 SCOPE_LIST_APPEND (current_function_scope, names, decl); 2290 } 2291 2292 finish_decl (decl, init, NULL_TREE); 2293 2294 return decl; 2295} 2296 2297/* Return a definition for a builtin function named NAME and whose data type 2298 is TYPE. TYPE should be a function type with argument types. 2299 FUNCTION_CODE tells later passes how to compile calls to this function. 2300 See tree.h for its possible values. 2301 2302 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME, 2303 the name to be called if we can't opencode the function. If 2304 ATTRS is nonzero, use that for the function's attribute list. */ 2305 2306tree 2307builtin_function (const char *name, tree type, int function_code, 2308 enum built_in_class class, const char *library_name, 2309 tree attrs) 2310{ 2311 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type); 2312 DECL_EXTERNAL (decl) = 1; 2313 TREE_PUBLIC (decl) = 1; 2314 if (library_name) 2315 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name)); 2316 make_decl_rtl (decl, NULL); 2317 pushdecl (decl); 2318 DECL_BUILT_IN_CLASS (decl) = class; 2319 DECL_FUNCTION_CODE (decl) = function_code; 2320 2321 /* Warn if a function in the namespace for users 2322 is used without an occasion to consider it declared. */ 2323 if (name[0] != '_' || name[1] != '_') 2324 C_DECL_INVISIBLE (decl) = 1; 2325 2326 /* Possibly apply some default attributes to this built-in function. */ 2327 if (attrs) 2328 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN); 2329 else 2330 decl_attributes (&decl, NULL_TREE, 0); 2331 2332 return decl; 2333} 2334 2335/* Called when a declaration is seen that contains no names to declare. 2336 If its type is a reference to a structure, union or enum inherited 2337 from a containing scope, shadow that tag name for the current scope 2338 with a forward reference. 2339 If its type defines a new named structure or union 2340 or defines an enum, it is valid but we need not do anything here. 2341 Otherwise, it is an error. */ 2342 2343void 2344shadow_tag (tree declspecs) 2345{ 2346 shadow_tag_warned (declspecs, 0); 2347} 2348 2349void 2350shadow_tag_warned (tree declspecs, int warned) 2351 2352 2353 /* 1 => we have done a pedwarn. 2 => we have done a warning, but 2354 no pedwarn. */ 2355{ 2356 int found_tag = 0; 2357 tree link; 2358 tree specs, attrs; 2359 2360 pending_invalid_xref = 0; 2361 2362 /* Remove the attributes from declspecs, since they will confuse the 2363 following code. */ 2364 split_specs_attrs (declspecs, &specs, &attrs); 2365 2366 for (link = specs; link; link = TREE_CHAIN (link)) 2367 { 2368 tree value = TREE_VALUE (link); 2369 enum tree_code code = TREE_CODE (value); 2370 2371 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE) 2372 /* Used to test also that TYPE_SIZE (value) != 0. 2373 That caused warning for `struct foo;' at top level in the file. */ 2374 { 2375 tree name = TYPE_NAME (value); 2376 tree t; 2377 2378 found_tag++; 2379 2380 if (name == 0) 2381 { 2382 if (warned != 1 && code != ENUMERAL_TYPE) 2383 /* Empty unnamed enum OK */ 2384 { 2385 pedwarn ("unnamed struct/union that defines no instances"); 2386 warned = 1; 2387 } 2388 } 2389 else 2390 { 2391 t = lookup_tag (code, name, 1); 2392 2393 if (t == 0) 2394 { 2395 t = make_node (code); 2396 pushtag (name, t); 2397 } 2398 } 2399 } 2400 else 2401 { 2402 if (!warned && ! in_system_header) 2403 { 2404 warning ("useless keyword or type name in empty declaration"); 2405 warned = 2; 2406 } 2407 } 2408 } 2409 2410 if (found_tag > 1) 2411 error ("two types specified in one empty declaration"); 2412 2413 if (warned != 1) 2414 { 2415 if (found_tag == 0) 2416 pedwarn ("empty declaration"); 2417 } 2418} 2419 2420/* Construct an array declarator. EXPR is the expression inside [], or 2421 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied 2422 to the pointer to which a parameter array is converted). STATIC_P is 2423 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P 2424 is nonzero is the array is [*], a VLA of unspecified length which is 2425 nevertheless a complete type (not currently implemented by GCC), 2426 zero otherwise. The declarator is constructed as an ARRAY_REF 2427 (to be decoded by grokdeclarator), whose operand 0 is what's on the 2428 left of the [] (filled by in set_array_declarator_type) and operand 1 2429 is the expression inside; whose TREE_TYPE is the type qualifiers and 2430 which has TREE_STATIC set if "static" is used. */ 2431 2432tree 2433build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p) 2434{ 2435 tree decl; 2436 decl = build_nt (ARRAY_REF, NULL_TREE, expr); 2437 TREE_TYPE (decl) = quals; 2438 TREE_STATIC (decl) = (static_p ? 1 : 0); 2439 if (pedantic && !flag_isoc99) 2440 { 2441 if (static_p || quals != NULL_TREE) 2442 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators"); 2443 if (vla_unspec_p) 2444 pedwarn ("ISO C90 does not support `[*]' array declarators"); 2445 } 2446 if (vla_unspec_p) 2447 warning ("GCC does not yet properly implement `[*]' array declarators"); 2448 return decl; 2449} 2450 2451/* Set the type of an array declarator. DECL is the declarator, as 2452 constructed by build_array_declarator; TYPE is what appears on the left 2453 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an 2454 abstract declarator, zero otherwise; this is used to reject static and 2455 type qualifiers in abstract declarators, where they are not in the 2456 C99 grammar. */ 2457 2458tree 2459set_array_declarator_type (tree decl, tree type, int abstract_p) 2460{ 2461 TREE_OPERAND (decl, 0) = type; 2462 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl))) 2463 error ("static or type qualifiers in abstract declarator"); 2464 return decl; 2465} 2466 2467/* Decode a "typename", such as "int **", returning a ..._TYPE node. */ 2468 2469tree 2470groktypename (tree typename) 2471{ 2472 tree specs, attrs; 2473 2474 if (TREE_CODE (typename) != TREE_LIST) 2475 return typename; 2476 2477 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs); 2478 2479 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0, 2480 NULL); 2481 2482 /* Apply attributes. */ 2483 decl_attributes (&typename, attrs, 0); 2484 2485 return typename; 2486} 2487 2488/* Return a PARM_DECL node for a given pair of specs and declarator. */ 2489 2490tree 2491groktypename_in_parm_context (tree typename) 2492{ 2493 if (TREE_CODE (typename) != TREE_LIST) 2494 return typename; 2495 return grokdeclarator (TREE_VALUE (typename), 2496 TREE_PURPOSE (typename), 2497 PARM, 0, NULL); 2498} 2499 2500/* Decode a declarator in an ordinary declaration or data definition. 2501 This is called as soon as the type information and variable name 2502 have been parsed, before parsing the initializer if any. 2503 Here we create the ..._DECL node, fill in its type, 2504 and put it on the list of decls for the current context. 2505 The ..._DECL node is returned as the value. 2506 2507 Exception: for arrays where the length is not specified, 2508 the type is left null, to be filled in by `finish_decl'. 2509 2510 Function definitions do not come here; they go to start_function 2511 instead. However, external and forward declarations of functions 2512 do go through here. Structure field declarations are done by 2513 grokfield and not through here. */ 2514 2515tree 2516start_decl (tree declarator, tree declspecs, int initialized, tree attributes) 2517{ 2518 tree decl; 2519 tree tem; 2520 2521 /* An object declared as __attribute__((deprecated)) suppresses 2522 warnings of uses of other deprecated items. */ 2523 if (lookup_attribute ("deprecated", attributes)) 2524 deprecated_state = DEPRECATED_SUPPRESS; 2525 2526 decl = grokdeclarator (declarator, declspecs, 2527 NORMAL, initialized, NULL); 2528 2529 deprecated_state = DEPRECATED_NORMAL; 2530 2531 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL 2532 && MAIN_NAME_P (DECL_NAME (decl))) 2533 warning ("%J'%D' is usually a function", decl, decl); 2534 2535 if (initialized) 2536 /* Is it valid for this decl to have an initializer at all? 2537 If not, set INITIALIZED to zero, which will indirectly 2538 tell `finish_decl' to ignore the initializer once it is parsed. */ 2539 switch (TREE_CODE (decl)) 2540 { 2541 case TYPE_DECL: 2542 error ("typedef `%s' is initialized (use __typeof__ instead)", 2543 IDENTIFIER_POINTER (DECL_NAME (decl))); 2544 initialized = 0; 2545 break; 2546 2547 case FUNCTION_DECL: 2548 error ("function `%s' is initialized like a variable", 2549 IDENTIFIER_POINTER (DECL_NAME (decl))); 2550 initialized = 0; 2551 break; 2552 2553 case PARM_DECL: 2554 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */ 2555 error ("parameter `%s' is initialized", 2556 IDENTIFIER_POINTER (DECL_NAME (decl))); 2557 initialized = 0; 2558 break; 2559 2560 default: 2561 /* Don't allow initializations for incomplete types 2562 except for arrays which might be completed by the initialization. */ 2563 2564 /* This can happen if the array size is an undefined macro. We already 2565 gave a warning, so we don't need another one. */ 2566 if (TREE_TYPE (decl) == error_mark_node) 2567 initialized = 0; 2568 else if (COMPLETE_TYPE_P (TREE_TYPE (decl))) 2569 { 2570 /* A complete type is ok if size is fixed. */ 2571 2572 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST 2573 || C_DECL_VARIABLE_SIZE (decl)) 2574 { 2575 error ("variable-sized object may not be initialized"); 2576 initialized = 0; 2577 } 2578 } 2579 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE) 2580 { 2581 error ("variable `%s' has initializer but incomplete type", 2582 IDENTIFIER_POINTER (DECL_NAME (decl))); 2583 initialized = 0; 2584 } 2585 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) 2586 { 2587 error ("elements of array `%s' have incomplete type", 2588 IDENTIFIER_POINTER (DECL_NAME (decl))); 2589 initialized = 0; 2590 } 2591 } 2592 2593 if (initialized) 2594 { 2595 DECL_EXTERNAL (decl) = 0; 2596 if (current_scope == global_scope) 2597 TREE_STATIC (decl) = 1; 2598 2599 /* Tell `pushdecl' this is an initialized decl 2600 even though we don't yet have the initializer expression. 2601 Also tell `finish_decl' it may store the real initializer. */ 2602 DECL_INITIAL (decl) = error_mark_node; 2603 } 2604 2605 /* If this is a function declaration, write a record describing it to the 2606 prototypes file (if requested). */ 2607 2608 if (TREE_CODE (decl) == FUNCTION_DECL) 2609 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0); 2610 2611 /* ANSI specifies that a tentative definition which is not merged with 2612 a non-tentative definition behaves exactly like a definition with an 2613 initializer equal to zero. (Section 3.7.2) 2614 2615 -fno-common gives strict ANSI behavior, though this tends to break 2616 a large body of code that grew up without this rule. 2617 2618 Thread-local variables are never common, since there's no entrenched 2619 body of code to break, and it allows more efficient variable references 2620 in the presence of dynamic linking. */ 2621 2622 if (TREE_CODE (decl) == VAR_DECL 2623 && !initialized 2624 && TREE_PUBLIC (decl) 2625 && !DECL_THREAD_LOCAL (decl) 2626 && !flag_no_common) 2627 DECL_COMMON (decl) = 1; 2628 2629 /* Set attributes here so if duplicate decl, will have proper attributes. */ 2630 decl_attributes (&decl, attributes, 0); 2631 2632 if (TREE_CODE (decl) == FUNCTION_DECL 2633 && targetm.calls.promote_prototypes (TREE_TYPE (decl))) 2634 { 2635 tree ce = declarator; 2636 2637 if (TREE_CODE (ce) == INDIRECT_REF) 2638 ce = TREE_OPERAND (declarator, 0); 2639 if (TREE_CODE (ce) == CALL_EXPR) 2640 { 2641 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1)); 2642 for (; args; args = TREE_CHAIN (args)) 2643 { 2644 tree type = TREE_TYPE (args); 2645 if (INTEGRAL_TYPE_P (type) 2646 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 2647 DECL_ARG_TYPE (args) = integer_type_node; 2648 } 2649 } 2650 } 2651 2652 if (TREE_CODE (decl) == FUNCTION_DECL 2653 && DECL_DECLARED_INLINE_P (decl) 2654 && DECL_UNINLINABLE (decl) 2655 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) 2656 warning ("%Jinline function '%D' given attribute noinline", decl, decl); 2657 2658 /* Add this decl to the current scope. 2659 TEM may equal DECL or it may be a previous decl of the same name. */ 2660 tem = pushdecl (decl); 2661 2662 /* For a local variable, define the RTL now. */ 2663 if (current_scope != global_scope 2664 /* But not if this is a duplicate decl 2665 and we preserved the rtl from the previous one 2666 (which may or may not happen). */ 2667 && !DECL_RTL_SET_P (tem) 2668 && DECL_FILE_SCOPE_P (tem)) 2669 { 2670 if (TREE_TYPE (tem) != error_mark_node 2671 && (COMPLETE_TYPE_P (TREE_TYPE (tem)) 2672 || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE 2673 && DECL_INITIAL (tem) != 0))) 2674 expand_decl (tem); 2675 } 2676 2677 return tem; 2678} 2679 2680/* Finish processing of a declaration; 2681 install its initial value. 2682 If the length of an array type is not known before, 2683 it must be determined now, from the initial value, or it is an error. */ 2684 2685void 2686finish_decl (tree decl, tree init, tree asmspec_tree) 2687{ 2688 tree type = TREE_TYPE (decl); 2689 int was_incomplete = (DECL_SIZE (decl) == 0); 2690 const char *asmspec = 0; 2691 2692 /* If a name was specified, get the string. */ 2693 if (current_scope == global_scope) 2694 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); 2695 if (asmspec_tree) 2696 asmspec = TREE_STRING_POINTER (asmspec_tree); 2697 2698 /* If `start_decl' didn't like having an initialization, ignore it now. */ 2699 if (init != 0 && DECL_INITIAL (decl) == 0) 2700 init = 0; 2701 2702 /* Don't crash if parm is initialized. */ 2703 if (TREE_CODE (decl) == PARM_DECL) 2704 init = 0; 2705 2706 if (init) 2707 store_init_value (decl, init); 2708 2709 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL 2710 || TREE_CODE (decl) == FUNCTION_DECL 2711 || TREE_CODE (decl) == FIELD_DECL)) 2712 objc_check_decl (decl); 2713 2714 /* Deduce size of array from initialization, if not already known. */ 2715 if (TREE_CODE (type) == ARRAY_TYPE 2716 && TYPE_DOMAIN (type) == 0 2717 && TREE_CODE (decl) != TYPE_DECL) 2718 { 2719 int do_default 2720 = (TREE_STATIC (decl) 2721 /* Even if pedantic, an external linkage array 2722 may have incomplete type at first. */ 2723 ? pedantic && !TREE_PUBLIC (decl) 2724 : !DECL_EXTERNAL (decl)); 2725 int failure 2726 = complete_array_type (type, DECL_INITIAL (decl), do_default); 2727 2728 /* Get the completed type made by complete_array_type. */ 2729 type = TREE_TYPE (decl); 2730 2731 if (failure == 1) 2732 error ("%Jinitializer fails to determine size of '%D'", decl, decl); 2733 2734 else if (failure == 2) 2735 { 2736 if (do_default) 2737 error ("%Jarray size missing in '%D'", decl, decl); 2738 /* If a `static' var's size isn't known, 2739 make it extern as well as static, so it does not get 2740 allocated. 2741 If it is not `static', then do not mark extern; 2742 finish_incomplete_decl will give it a default size 2743 and it will get allocated. */ 2744 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl)) 2745 DECL_EXTERNAL (decl) = 1; 2746 } 2747 2748 /* TYPE_MAX_VALUE is always one less than the number of elements 2749 in the array, because we start counting at zero. Therefore, 2750 warn only if the value is less than zero. */ 2751 else if (pedantic && TYPE_DOMAIN (type) != 0 2752 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0) 2753 error ("%Jzero or negative size array '%D'", decl, decl); 2754 2755 layout_decl (decl, 0); 2756 } 2757 2758 if (TREE_CODE (decl) == VAR_DECL) 2759 { 2760 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node 2761 && COMPLETE_TYPE_P (TREE_TYPE (decl))) 2762 layout_decl (decl, 0); 2763 2764 if (DECL_SIZE (decl) == 0 2765 /* Don't give an error if we already gave one earlier. */ 2766 && TREE_TYPE (decl) != error_mark_node 2767 && (TREE_STATIC (decl) 2768 ? 2769 /* A static variable with an incomplete type 2770 is an error if it is initialized. 2771 Also if it is not file scope. 2772 Otherwise, let it through, but if it is not `extern' 2773 then it may cause an error message later. */ 2774 (DECL_INITIAL (decl) != 0 2775 || !DECL_FILE_SCOPE_P (decl)) 2776 : 2777 /* An automatic variable with an incomplete type 2778 is an error. */ 2779 !DECL_EXTERNAL (decl))) 2780 { 2781 error ("%Jstorage size of '%D' isn't known", decl, decl); 2782 TREE_TYPE (decl) = error_mark_node; 2783 } 2784 2785 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 2786 && DECL_SIZE (decl) != 0) 2787 { 2788 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) 2789 constant_expression_warning (DECL_SIZE (decl)); 2790 else 2791 error ("%Jstorage size of '%D' isn't constant", decl, decl); 2792 } 2793 2794 if (TREE_USED (type)) 2795 TREE_USED (decl) = 1; 2796 } 2797 2798 /* If this is a function and an assembler name is specified, reset DECL_RTL 2799 so we can give it its new name. Also, update built_in_decls if it 2800 was a normal built-in. */ 2801 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec) 2802 { 2803 /* ASMSPEC is given, and not the name of a register. Mark the 2804 name with a star so assemble_name won't munge it. */ 2805 char *starred = alloca (strlen (asmspec) + 2); 2806 starred[0] = '*'; 2807 strcpy (starred + 1, asmspec); 2808 2809 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) 2810 { 2811 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)]; 2812 SET_DECL_RTL (builtin, NULL_RTX); 2813 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred)); 2814#ifdef TARGET_MEM_FUNCTIONS 2815 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY) 2816 init_block_move_fn (starred); 2817 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET) 2818 init_block_clear_fn (starred); 2819#else 2820 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY) 2821 init_block_move_fn (starred); 2822 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO) 2823 init_block_clear_fn (starred); 2824#endif 2825 } 2826 SET_DECL_RTL (decl, NULL_RTX); 2827 change_decl_assembler_name (decl, get_identifier (starred)); 2828 } 2829 2830 /* If #pragma weak was used, mark the decl weak now. */ 2831 if (current_scope == global_scope) 2832 maybe_apply_pragma_weak (decl); 2833 2834 /* Output the assembler code and/or RTL code for variables and functions, 2835 unless the type is an undefined structure or union. 2836 If not, it will get done when the type is completed. */ 2837 2838 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL) 2839 { 2840 /* This is a no-op in c-lang.c or something real in objc-act.c. */ 2841 if (c_dialect_objc ()) 2842 objc_check_decl (decl); 2843 2844 if (DECL_FILE_SCOPE_P (decl)) 2845 { 2846 if (DECL_INITIAL (decl) == NULL_TREE 2847 || DECL_INITIAL (decl) == error_mark_node) 2848 /* Don't output anything 2849 when a tentative file-scope definition is seen. 2850 But at end of compilation, do output code for them. */ 2851 DECL_DEFER_OUTPUT (decl) = 1; 2852 rest_of_decl_compilation (decl, asmspec, true, 0); 2853 } 2854 else 2855 { 2856 /* This is a local variable. If there is an ASMSPEC, the 2857 user has requested that we handle it specially. */ 2858 if (asmspec) 2859 { 2860 /* In conjunction with an ASMSPEC, the `register' 2861 keyword indicates that we should place the variable 2862 in a particular register. */ 2863 if (DECL_REGISTER (decl)) 2864 DECL_C_HARD_REGISTER (decl) = 1; 2865 2866 /* If this is not a static variable, issue a warning. 2867 It doesn't make any sense to give an ASMSPEC for an 2868 ordinary, non-register local variable. Historically, 2869 GCC has accepted -- but ignored -- the ASMSPEC in 2870 this case. */ 2871 if (TREE_CODE (decl) == VAR_DECL 2872 && !DECL_REGISTER (decl) 2873 && !TREE_STATIC (decl)) 2874 warning ("%Jignoring asm-specifier for non-static local " 2875 "variable '%D'", decl, decl); 2876 else 2877 change_decl_assembler_name (decl, get_identifier (asmspec)); 2878 } 2879 2880 if (TREE_CODE (decl) != FUNCTION_DECL) 2881 add_decl_stmt (decl); 2882 } 2883 2884 if (!DECL_FILE_SCOPE_P (decl)) 2885 { 2886 /* Recompute the RTL of a local array now 2887 if it used to be an incomplete type. */ 2888 if (was_incomplete 2889 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl)) 2890 { 2891 /* If we used it already as memory, it must stay in memory. */ 2892 TREE_ADDRESSABLE (decl) = TREE_USED (decl); 2893 /* If it's still incomplete now, no init will save it. */ 2894 if (DECL_SIZE (decl) == 0) 2895 DECL_INITIAL (decl) = 0; 2896 } 2897 } 2898 } 2899 2900 /* If this was marked 'used', be sure it will be output. */ 2901 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl))) 2902 mark_referenced (DECL_ASSEMBLER_NAME (decl)); 2903 2904 if (TREE_CODE (decl) == TYPE_DECL) 2905 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0); 2906 2907 /* At the end of a declaration, throw away any variable type sizes 2908 of types defined inside that declaration. There is no use 2909 computing them in the following function definition. */ 2910 if (current_scope == global_scope) 2911 get_pending_sizes (); 2912 2913 /* Install a cleanup (aka destructor) if one was given. */ 2914 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl)) 2915 { 2916 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl)); 2917 if (attr) 2918 { 2919 static bool eh_initialized_p; 2920 2921 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr)); 2922 tree cleanup_decl = lookup_name (cleanup_id); 2923 tree cleanup; 2924 2925 /* Build "cleanup(&decl)" for the destructor. */ 2926 cleanup = build_unary_op (ADDR_EXPR, decl, 0); 2927 cleanup = build_tree_list (NULL_TREE, cleanup); 2928 cleanup = build_function_call (cleanup_decl, cleanup); 2929 2930 /* Don't warn about decl unused; the cleanup uses it. */ 2931 TREE_USED (decl) = 1; 2932 2933 /* Initialize EH, if we've been told to do so. */ 2934 if (flag_exceptions && !eh_initialized_p) 2935 { 2936 eh_initialized_p = true; 2937 eh_personality_libfunc 2938 = init_one_libfunc (USING_SJLJ_EXCEPTIONS 2939 ? "__gcc_personality_sj0" 2940 : "__gcc_personality_v0"); 2941 using_eh_for_cleanups (); 2942 } 2943 2944 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup)); 2945 } 2946 } 2947} 2948 2949/* Given a parsed parameter declaration, decode it into a PARM_DECL 2950 and push that on the current scope. */ 2951 2952void 2953push_parm_decl (tree parm) 2954{ 2955 tree decl; 2956 2957 /* Don't attempt to expand sizes while parsing this decl. 2958 (We can get here with i_s_e 1 somehow from Objective-C.) */ 2959 int save_immediate_size_expand = immediate_size_expand; 2960 immediate_size_expand = 0; 2961 2962 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)), 2963 TREE_PURPOSE (TREE_PURPOSE (parm)), 2964 PARM, 0, NULL); 2965 decl_attributes (&decl, TREE_VALUE (parm), 0); 2966 2967 decl = pushdecl (decl); 2968 2969 finish_decl (decl, NULL_TREE, NULL_TREE); 2970 2971 immediate_size_expand = save_immediate_size_expand; 2972} 2973 2974/* Mark all the parameter declarations to date as forward decls, 2975 shift them to the variables list, and reset the parameters list. 2976 Also diagnose use of this extension. */ 2977 2978void 2979mark_forward_parm_decls (void) 2980{ 2981 tree parm; 2982 2983 if (pedantic && !current_scope->warned_forward_parm_decls) 2984 { 2985 pedwarn ("ISO C forbids forward parameter declarations"); 2986 current_scope->warned_forward_parm_decls = true; 2987 } 2988 2989 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm)) 2990 TREE_ASM_WRITTEN (parm) = 1; 2991 2992 SCOPE_LIST_CONCAT (current_scope, names, current_scope, parms); 2993 current_scope->parms = 0; 2994 current_scope->parms_last = 0; 2995} 2996 2997static GTY(()) int compound_literal_number; 2998 2999/* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound 3000 literal, which may be an incomplete array type completed by the 3001 initializer; INIT is a CONSTRUCTOR that initializes the compound 3002 literal. */ 3003 3004tree 3005build_compound_literal (tree type, tree init) 3006{ 3007 /* We do not use start_decl here because we have a type, not a declarator; 3008 and do not use finish_decl because the decl should be stored inside 3009 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */ 3010 tree decl = build_decl (VAR_DECL, NULL_TREE, type); 3011 tree complit; 3012 tree stmt; 3013 DECL_EXTERNAL (decl) = 0; 3014 TREE_PUBLIC (decl) = 0; 3015 TREE_STATIC (decl) = (current_scope == global_scope); 3016 DECL_CONTEXT (decl) = current_function_decl; 3017 TREE_USED (decl) = 1; 3018 TREE_TYPE (decl) = type; 3019 TREE_READONLY (decl) = TREE_READONLY (type); 3020 store_init_value (decl, init); 3021 3022 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type)) 3023 { 3024 int failure = complete_array_type (type, DECL_INITIAL (decl), 1); 3025 if (failure) 3026 abort (); 3027 } 3028 3029 type = TREE_TYPE (decl); 3030 if (type == error_mark_node || !COMPLETE_TYPE_P (type)) 3031 return error_mark_node; 3032 3033 stmt = build_stmt (DECL_STMT, decl); 3034 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt); 3035 TREE_SIDE_EFFECTS (complit) = 1; 3036 3037 layout_decl (decl, 0); 3038 3039 if (TREE_STATIC (decl)) 3040 { 3041 /* This decl needs a name for the assembler output. We also need 3042 a unique suffix to be added to the name. */ 3043 char *name; 3044 3045 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal", 3046 compound_literal_number); 3047 compound_literal_number++; 3048 DECL_NAME (decl) = get_identifier (name); 3049 DECL_DEFER_OUTPUT (decl) = 1; 3050 DECL_COMDAT (decl) = 1; 3051 DECL_ARTIFICIAL (decl) = 1; 3052 pushdecl (decl); 3053 rest_of_decl_compilation (decl, NULL, 1, 0); 3054 } 3055 3056 return complit; 3057} 3058 3059/* Make TYPE a complete type based on INITIAL_VALUE. 3060 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, 3061 2 if there was no information (in which case assume 1 if DO_DEFAULT). */ 3062 3063int 3064complete_array_type (tree type, tree initial_value, int do_default) 3065{ 3066 tree maxindex = NULL_TREE; 3067 int value = 0; 3068 3069 if (initial_value) 3070 { 3071 /* Note MAXINDEX is really the maximum index, 3072 one less than the size. */ 3073 if (TREE_CODE (initial_value) == STRING_CST) 3074 { 3075 int eltsize 3076 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); 3077 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value) 3078 / eltsize) - 1, 0); 3079 } 3080 else if (TREE_CODE (initial_value) == CONSTRUCTOR) 3081 { 3082 tree elts = CONSTRUCTOR_ELTS (initial_value); 3083 maxindex = build_int_2 (-1, -1); 3084 for (; elts; elts = TREE_CHAIN (elts)) 3085 { 3086 if (TREE_PURPOSE (elts)) 3087 maxindex = TREE_PURPOSE (elts); 3088 else 3089 maxindex = fold (build (PLUS_EXPR, integer_type_node, 3090 maxindex, integer_one_node)); 3091 } 3092 maxindex = copy_node (maxindex); 3093 } 3094 else 3095 { 3096 /* Make an error message unless that happened already. */ 3097 if (initial_value != error_mark_node) 3098 value = 1; 3099 3100 /* Prevent further error messages. */ 3101 maxindex = build_int_2 (0, 0); 3102 } 3103 } 3104 3105 if (!maxindex) 3106 { 3107 if (do_default) 3108 maxindex = build_int_2 (0, 0); 3109 value = 2; 3110 } 3111 3112 if (maxindex) 3113 { 3114 TYPE_DOMAIN (type) = build_index_type (maxindex); 3115 if (!TREE_TYPE (maxindex)) 3116 TREE_TYPE (maxindex) = TYPE_DOMAIN (type); 3117 } 3118 3119 /* Lay out the type now that we can get the real answer. */ 3120 3121 layout_type (type); 3122 3123 return value; 3124} 3125 3126/* Determine whether TYPE is a structure with a flexible array member, 3127 or a union containing such a structure (possibly recursively). */ 3128 3129static bool 3130flexible_array_type_p (tree type) 3131{ 3132 tree x; 3133 switch (TREE_CODE (type)) 3134 { 3135 case RECORD_TYPE: 3136 x = TYPE_FIELDS (type); 3137 if (x == NULL_TREE) 3138 return false; 3139 while (TREE_CHAIN (x) != NULL_TREE) 3140 x = TREE_CHAIN (x); 3141 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 3142 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 3143 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 3144 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 3145 return true; 3146 return false; 3147 case UNION_TYPE: 3148 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x)) 3149 { 3150 if (flexible_array_type_p (TREE_TYPE (x))) 3151 return true; 3152 } 3153 return false; 3154 default: 3155 return false; 3156 } 3157} 3158 3159/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME, 3160 replacing with appropriate values if they are invalid. */ 3161static void 3162check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name) 3163{ 3164 tree type_mv; 3165 unsigned int max_width; 3166 unsigned HOST_WIDE_INT w; 3167 const char *name = orig_name ? orig_name: _("<anonymous>"); 3168 3169 /* Necessary? */ 3170 STRIP_NOPS (*width); 3171 3172 /* Detect and ignore out of range field width and process valid 3173 field widths. */ 3174 if (TREE_CODE (*width) != INTEGER_CST) 3175 { 3176 error ("bit-field `%s' width not an integer constant", name); 3177 *width = integer_one_node; 3178 } 3179 else 3180 { 3181 constant_expression_warning (*width); 3182 if (tree_int_cst_sgn (*width) < 0) 3183 { 3184 error ("negative width in bit-field `%s'", name); 3185 *width = integer_one_node; 3186 } 3187 else if (integer_zerop (*width) && orig_name) 3188 { 3189 error ("zero width for bit-field `%s'", name); 3190 *width = integer_one_node; 3191 } 3192 } 3193 3194 /* Detect invalid bit-field type. */ 3195 if (TREE_CODE (*type) != INTEGER_TYPE 3196 && TREE_CODE (*type) != BOOLEAN_TYPE 3197 && TREE_CODE (*type) != ENUMERAL_TYPE) 3198 { 3199 error ("bit-field `%s' has invalid type", name); 3200 *type = unsigned_type_node; 3201 } 3202 3203 type_mv = TYPE_MAIN_VARIANT (*type); 3204 if (pedantic 3205 && type_mv != integer_type_node 3206 && type_mv != unsigned_type_node 3207 && type_mv != boolean_type_node) 3208 pedwarn ("type of bit-field `%s' is a GCC extension", name); 3209 3210 if (type_mv == boolean_type_node) 3211 max_width = CHAR_TYPE_SIZE; 3212 else 3213 max_width = TYPE_PRECISION (*type); 3214 3215 if (0 < compare_tree_int (*width, max_width)) 3216 { 3217 error ("width of `%s' exceeds its type", name); 3218 w = max_width; 3219 *width = build_int_2 (w, 0); 3220 } 3221 else 3222 w = tree_low_cst (*width, 1); 3223 3224 if (TREE_CODE (*type) == ENUMERAL_TYPE 3225 && (w < min_precision (TYPE_MIN_VALUE (*type), TREE_UNSIGNED (*type)) 3226 || w < min_precision (TYPE_MAX_VALUE (*type), TREE_UNSIGNED (*type)))) 3227 warning ("`%s' is narrower than values of its type", name); 3228} 3229 3230/* Given declspecs and a declarator, 3231 determine the name and type of the object declared 3232 and construct a ..._DECL node for it. 3233 (In one case we can return a ..._TYPE node instead. 3234 For invalid input we sometimes return 0.) 3235 3236 DECLSPECS is a chain of tree_list nodes whose value fields 3237 are the storage classes and type specifiers. 3238 3239 DECL_CONTEXT says which syntactic context this declaration is in: 3240 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. 3241 FUNCDEF for a function definition. Like NORMAL but a few different 3242 error messages in each case. Return value may be zero meaning 3243 this definition is too screwy to try to parse. 3244 PARM for a parameter declaration (either within a function prototype 3245 or before a function body). Make a PARM_DECL, or return void_type_node. 3246 TYPENAME if for a typename (in a cast or sizeof). 3247 Don't make a DECL node; just return the ..._TYPE node. 3248 FIELD for a struct or union field; make a FIELD_DECL. 3249 INITIALIZED is 1 if the decl has an initializer. 3250 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node 3251 representing the width of the bit-field. 3252 3253 In the TYPENAME case, DECLARATOR is really an absolute declarator. 3254 It may also be so in the PARM case, for a prototype where the 3255 argument type is specified but not the name. 3256 3257 This function is where the complicated C meanings of `static' 3258 and `extern' are interpreted. */ 3259 3260static tree 3261grokdeclarator (tree declarator, tree declspecs, 3262 enum decl_context decl_context, int initialized, tree *width) 3263{ 3264 int specbits = 0; 3265 tree spec; 3266 tree type = NULL_TREE; 3267 int longlong = 0; 3268 int constp; 3269 int restrictp; 3270 int volatilep; 3271 int type_quals = TYPE_UNQUALIFIED; 3272 int inlinep; 3273 int explicit_int = 0; 3274 int explicit_char = 0; 3275 int defaulted_int = 0; 3276 tree typedef_decl = 0; 3277 const char *name, *orig_name; 3278 tree typedef_type = 0; 3279 int funcdef_flag = 0; 3280 enum tree_code innermost_code = ERROR_MARK; 3281 int size_varies = 0; 3282 tree decl_attr = NULL_TREE; 3283 tree array_ptr_quals = NULL_TREE; 3284 int array_parm_static = 0; 3285 tree returned_attrs = NULL_TREE; 3286 bool bitfield = width != NULL; 3287 tree element_type; 3288 3289 if (decl_context == FUNCDEF) 3290 funcdef_flag = 1, decl_context = NORMAL; 3291 3292 /* Look inside a declarator for the name being declared 3293 and get it as a string, for an error message. */ 3294 { 3295 tree decl = declarator; 3296 name = 0; 3297 3298 while (decl) 3299 switch (TREE_CODE (decl)) 3300 { 3301 case ARRAY_REF: 3302 case INDIRECT_REF: 3303 case CALL_EXPR: 3304 innermost_code = TREE_CODE (decl); 3305 decl = TREE_OPERAND (decl, 0); 3306 break; 3307 3308 case TREE_LIST: 3309 decl = TREE_VALUE (decl); 3310 break; 3311 3312 case IDENTIFIER_NODE: 3313 name = IDENTIFIER_POINTER (decl); 3314 decl = 0; 3315 break; 3316 3317 default: 3318 abort (); 3319 } 3320 orig_name = name; 3321 if (name == 0) 3322 name = "type name"; 3323 } 3324 3325 /* A function definition's declarator must have the form of 3326 a function declarator. */ 3327 3328 if (funcdef_flag && innermost_code != CALL_EXPR) 3329 return 0; 3330 3331 /* If this looks like a function definition, make it one, 3332 even if it occurs where parms are expected. 3333 Then store_parm_decls will reject it and not use it as a parm. */ 3334 if (decl_context == NORMAL && !funcdef_flag 3335 && current_scope->parm_flag) 3336 decl_context = PARM; 3337 3338 /* Look through the decl specs and record which ones appear. 3339 Some typespecs are defined as built-in typenames. 3340 Others, the ones that are modifiers of other types, 3341 are represented by bits in SPECBITS: set the bits for 3342 the modifiers that appear. Storage class keywords are also in SPECBITS. 3343 3344 If there is a typedef name or a type, store the type in TYPE. 3345 This includes builtin typedefs such as `int'. 3346 3347 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char' 3348 and did not come from a user typedef. 3349 3350 Set LONGLONG if `long' is mentioned twice. */ 3351 3352 for (spec = declspecs; spec; spec = TREE_CHAIN (spec)) 3353 { 3354 tree id = TREE_VALUE (spec); 3355 3356 /* If the entire declaration is itself tagged as deprecated then 3357 suppress reports of deprecated items. */ 3358 if (id && TREE_DEPRECATED (id)) 3359 { 3360 if (deprecated_state != DEPRECATED_SUPPRESS) 3361 warn_deprecated_use (id); 3362 } 3363 3364 if (id == ridpointers[(int) RID_INT]) 3365 explicit_int = 1; 3366 if (id == ridpointers[(int) RID_CHAR]) 3367 explicit_char = 1; 3368 3369 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id)) 3370 { 3371 enum rid i = C_RID_CODE (id); 3372 if ((int) i <= (int) RID_LAST_MODIFIER) 3373 { 3374 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG))) 3375 { 3376 if (longlong) 3377 error ("`long long long' is too long for GCC"); 3378 else 3379 { 3380 if (pedantic && !flag_isoc99 && ! in_system_header 3381 && warn_long_long) 3382 pedwarn ("ISO C90 does not support `long long'"); 3383 longlong = 1; 3384 } 3385 } 3386 else if (specbits & (1 << (int) i)) 3387 { 3388 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT) 3389 { 3390 if (pedantic && !flag_isoc99) 3391 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id)); 3392 } 3393 else 3394 error ("duplicate `%s'", IDENTIFIER_POINTER (id)); 3395 } 3396 3397 /* Diagnose "__thread extern". Recall that this list 3398 is in the reverse order seen in the text. */ 3399 if (i == RID_THREAD 3400 && (specbits & (1 << (int) RID_EXTERN 3401 | 1 << (int) RID_STATIC))) 3402 { 3403 if (specbits & 1 << (int) RID_EXTERN) 3404 error ("`__thread' before `extern'"); 3405 else 3406 error ("`__thread' before `static'"); 3407 } 3408 3409 specbits |= 1 << (int) i; 3410 goto found; 3411 } 3412 } 3413 if (type) 3414 error ("two or more data types in declaration of `%s'", name); 3415 /* Actual typedefs come to us as TYPE_DECL nodes. */ 3416 else if (TREE_CODE (id) == TYPE_DECL) 3417 { 3418 if (TREE_TYPE (id) == error_mark_node) 3419 ; /* Allow the type to default to int to avoid cascading errors. */ 3420 else 3421 { 3422 type = TREE_TYPE (id); 3423 decl_attr = DECL_ATTRIBUTES (id); 3424 typedef_decl = id; 3425 } 3426 } 3427 /* Built-in types come as identifiers. */ 3428 else if (TREE_CODE (id) == IDENTIFIER_NODE) 3429 { 3430 tree t = lookup_name (id); 3431 if (TREE_TYPE (t) == error_mark_node) 3432 ; 3433 else if (!t || TREE_CODE (t) != TYPE_DECL) 3434 error ("`%s' fails to be a typedef or built in type", 3435 IDENTIFIER_POINTER (id)); 3436 else 3437 { 3438 type = TREE_TYPE (t); 3439 typedef_decl = t; 3440 } 3441 } 3442 else if (TREE_CODE (id) != ERROR_MARK) 3443 type = id; 3444 3445 found: 3446 ; 3447 } 3448 3449 typedef_type = type; 3450 if (type) 3451 size_varies = C_TYPE_VARIABLE_SIZE (type); 3452 3453 /* No type at all: default to `int', and set DEFAULTED_INT 3454 because it was not a user-defined typedef. */ 3455 3456 if (type == 0) 3457 { 3458 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT) 3459 | (1 << (int) RID_SIGNED) 3460 | (1 << (int) RID_UNSIGNED) 3461 | (1 << (int) RID_COMPLEX)))) 3462 /* Don't warn about typedef foo = bar. */ 3463 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized) 3464 && ! in_system_header) 3465 { 3466 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type 3467 and this is a function, or if -Wimplicit; prefer the former 3468 warning since it is more explicit. */ 3469 if ((warn_implicit_int || warn_return_type || flag_isoc99) 3470 && funcdef_flag) 3471 warn_about_return_type = 1; 3472 else if (warn_implicit_int || flag_isoc99) 3473 pedwarn_c99 ("type defaults to `int' in declaration of `%s'", 3474 name); 3475 } 3476 3477 defaulted_int = 1; 3478 type = integer_type_node; 3479 } 3480 3481 /* Now process the modifiers that were specified 3482 and check for invalid combinations. */ 3483 3484 /* Long double is a special combination. */ 3485 3486 if ((specbits & 1 << (int) RID_LONG) && ! longlong 3487 && TYPE_MAIN_VARIANT (type) == double_type_node) 3488 { 3489 specbits &= ~(1 << (int) RID_LONG); 3490 type = long_double_type_node; 3491 } 3492 3493 /* Check all other uses of type modifiers. */ 3494 3495 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT) 3496 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED))) 3497 { 3498 int ok = 0; 3499 3500 if ((specbits & 1 << (int) RID_LONG) 3501 && (specbits & 1 << (int) RID_SHORT)) 3502 error ("both long and short specified for `%s'", name); 3503 else if (((specbits & 1 << (int) RID_LONG) 3504 || (specbits & 1 << (int) RID_SHORT)) 3505 && explicit_char) 3506 error ("long or short specified with char for `%s'", name); 3507 else if (((specbits & 1 << (int) RID_LONG) 3508 || (specbits & 1 << (int) RID_SHORT)) 3509 && TREE_CODE (type) == REAL_TYPE) 3510 { 3511 static int already = 0; 3512 3513 error ("long or short specified with floating type for `%s'", name); 3514 if (! already && ! pedantic) 3515 { 3516 error ("the only valid combination is `long double'"); 3517 already = 1; 3518 } 3519 } 3520 else if ((specbits & 1 << (int) RID_SIGNED) 3521 && (specbits & 1 << (int) RID_UNSIGNED)) 3522 error ("both signed and unsigned specified for `%s'", name); 3523 else if (TREE_CODE (type) != INTEGER_TYPE) 3524 error ("long, short, signed or unsigned invalid for `%s'", name); 3525 else 3526 { 3527 ok = 1; 3528 if (!explicit_int && !defaulted_int && !explicit_char) 3529 { 3530 error ("long, short, signed or unsigned used invalidly for `%s'", 3531 name); 3532 ok = 0; 3533 } 3534 } 3535 3536 /* Discard the type modifiers if they are invalid. */ 3537 if (! ok) 3538 { 3539 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT) 3540 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)); 3541 longlong = 0; 3542 } 3543 } 3544 3545 if ((specbits & (1 << (int) RID_COMPLEX)) 3546 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE) 3547 { 3548 error ("complex invalid for `%s'", name); 3549 specbits &= ~(1 << (int) RID_COMPLEX); 3550 } 3551 3552 /* Decide whether an integer type is signed or not. 3553 Optionally treat bit-fields as signed by default. */ 3554 if (specbits & 1 << (int) RID_UNSIGNED 3555 || (bitfield && ! flag_signed_bitfields 3556 && (explicit_int || defaulted_int || explicit_char 3557 /* A typedef for plain `int' without `signed' 3558 can be controlled just like plain `int'. */ 3559 || ! (typedef_decl != 0 3560 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) 3561 && TREE_CODE (type) != ENUMERAL_TYPE 3562 && !(specbits & 1 << (int) RID_SIGNED))) 3563 { 3564 if (longlong) 3565 type = long_long_unsigned_type_node; 3566 else if (specbits & 1 << (int) RID_LONG) 3567 type = long_unsigned_type_node; 3568 else if (specbits & 1 << (int) RID_SHORT) 3569 type = short_unsigned_type_node; 3570 else if (type == char_type_node) 3571 type = unsigned_char_type_node; 3572 else if (typedef_decl) 3573 type = c_common_unsigned_type (type); 3574 else 3575 type = unsigned_type_node; 3576 } 3577 else if ((specbits & 1 << (int) RID_SIGNED) 3578 && type == char_type_node) 3579 type = signed_char_type_node; 3580 else if (longlong) 3581 type = long_long_integer_type_node; 3582 else if (specbits & 1 << (int) RID_LONG) 3583 type = long_integer_type_node; 3584 else if (specbits & 1 << (int) RID_SHORT) 3585 type = short_integer_type_node; 3586 3587 if (specbits & 1 << (int) RID_COMPLEX) 3588 { 3589 if (pedantic && !flag_isoc99) 3590 pedwarn ("ISO C90 does not support complex types"); 3591 /* If we just have "complex", it is equivalent to 3592 "complex double", but if any modifiers at all are specified it is 3593 the complex form of TYPE. E.g, "complex short" is 3594 "complex short int". */ 3595 3596 if (defaulted_int && ! longlong 3597 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT) 3598 | (1 << (int) RID_SIGNED) 3599 | (1 << (int) RID_UNSIGNED)))) 3600 { 3601 if (pedantic) 3602 pedwarn ("ISO C does not support plain `complex' meaning `double complex'"); 3603 type = complex_double_type_node; 3604 } 3605 else if (type == integer_type_node) 3606 { 3607 if (pedantic) 3608 pedwarn ("ISO C does not support complex integer types"); 3609 type = complex_integer_type_node; 3610 } 3611 else if (type == float_type_node) 3612 type = complex_float_type_node; 3613 else if (type == double_type_node) 3614 type = complex_double_type_node; 3615 else if (type == long_double_type_node) 3616 type = complex_long_double_type_node; 3617 else 3618 { 3619 if (pedantic) 3620 pedwarn ("ISO C does not support complex integer types"); 3621 type = build_complex_type (type); 3622 } 3623 } 3624 3625 /* Figure out the type qualifiers for the declaration. There are 3626 two ways a declaration can become qualified. One is something 3627 like `const int i' where the `const' is explicit. Another is 3628 something like `typedef const int CI; CI i' where the type of the 3629 declaration contains the `const'. A third possibility is that 3630 there is a type qualifier on the element type of a typedefed 3631 array type, in which case we should extract that qualifier so 3632 that c_apply_type_quals_to_decls receives the full list of 3633 qualifiers to work with (C90 is not entirely clear about whether 3634 duplicate qualifiers should be diagnosed in this case, but it 3635 seems most appropriate to do so). */ 3636 element_type = strip_array_types (type); 3637 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type); 3638 restrictp 3639 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type); 3640 volatilep 3641 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type); 3642 inlinep = !! (specbits & (1 << (int) RID_INLINE)); 3643 if (pedantic && !flag_isoc99) 3644 { 3645 if (constp > 1) 3646 pedwarn ("duplicate `const'"); 3647 if (restrictp > 1) 3648 pedwarn ("duplicate `restrict'"); 3649 if (volatilep > 1) 3650 pedwarn ("duplicate `volatile'"); 3651 } 3652 if (! flag_gen_aux_info && (TYPE_QUALS (type))) 3653 type = TYPE_MAIN_VARIANT (type); 3654 type_quals = ((constp ? TYPE_QUAL_CONST : 0) 3655 | (restrictp ? TYPE_QUAL_RESTRICT : 0) 3656 | (volatilep ? TYPE_QUAL_VOLATILE : 0)); 3657 3658 /* Warn if two storage classes are given. Default to `auto'. */ 3659 3660 { 3661 int nclasses = 0; 3662 3663 if (specbits & 1 << (int) RID_AUTO) nclasses++; 3664 if (specbits & 1 << (int) RID_STATIC) nclasses++; 3665 if (specbits & 1 << (int) RID_EXTERN) nclasses++; 3666 if (specbits & 1 << (int) RID_REGISTER) nclasses++; 3667 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++; 3668 3669 /* "static __thread" and "extern __thread" are allowed. */ 3670 if ((specbits & (1 << (int) RID_THREAD 3671 | 1 << (int) RID_STATIC 3672 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD)) 3673 nclasses++; 3674 3675 /* Warn about storage classes that are invalid for certain 3676 kinds of declarations (parameters, typenames, etc.). */ 3677 3678 if (nclasses > 1) 3679 error ("multiple storage classes in declaration of `%s'", name); 3680 else if (funcdef_flag 3681 && (specbits 3682 & ((1 << (int) RID_REGISTER) 3683 | (1 << (int) RID_AUTO) 3684 | (1 << (int) RID_TYPEDEF) 3685 | (1 << (int) RID_THREAD)))) 3686 { 3687 if (specbits & 1 << (int) RID_AUTO 3688 && (pedantic || current_scope == global_scope)) 3689 pedwarn ("function definition declared `auto'"); 3690 if (specbits & 1 << (int) RID_REGISTER) 3691 error ("function definition declared `register'"); 3692 if (specbits & 1 << (int) RID_TYPEDEF) 3693 error ("function definition declared `typedef'"); 3694 if (specbits & 1 << (int) RID_THREAD) 3695 error ("function definition declared `__thread'"); 3696 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER) 3697 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD)); 3698 } 3699 else if (decl_context != NORMAL && nclasses > 0) 3700 { 3701 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER) 3702 ; 3703 else 3704 { 3705 switch (decl_context) 3706 { 3707 case FIELD: 3708 error ("storage class specified for structure field `%s'", 3709 name); 3710 break; 3711 case PARM: 3712 error ("storage class specified for parameter `%s'", name); 3713 break; 3714 default: 3715 error ("storage class specified for typename"); 3716 break; 3717 } 3718 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER) 3719 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC) 3720 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD)); 3721 } 3722 } 3723 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag) 3724 { 3725 /* `extern' with initialization is invalid if not at file scope. */ 3726 if (current_scope == global_scope) 3727 warning ("`%s' initialized and declared `extern'", name); 3728 else 3729 error ("`%s' has both `extern' and initializer", name); 3730 } 3731 else if (current_scope == global_scope) 3732 { 3733 if (specbits & 1 << (int) RID_AUTO) 3734 error ("file-scope declaration of `%s' specifies `auto'", name); 3735 } 3736 else 3737 { 3738 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag) 3739 error ("nested function `%s' declared `extern'", name); 3740 else if ((specbits & (1 << (int) RID_THREAD 3741 | 1 << (int) RID_EXTERN 3742 | 1 << (int) RID_STATIC)) 3743 == (1 << (int) RID_THREAD)) 3744 { 3745 error ("function-scope `%s' implicitly auto and declared `__thread'", 3746 name); 3747 specbits &= ~(1 << (int) RID_THREAD); 3748 } 3749 } 3750 } 3751 3752 /* Now figure out the structure of the declarator proper. 3753 Descend through it, creating more complex types, until we reach 3754 the declared identifier (or NULL_TREE, in an absolute declarator). */ 3755 3756 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE) 3757 { 3758 if (type == error_mark_node) 3759 { 3760 declarator = TREE_OPERAND (declarator, 0); 3761 continue; 3762 } 3763 3764 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]), 3765 an INDIRECT_REF (for *...), 3766 a CALL_EXPR (for ...(...)), 3767 a TREE_LIST (for nested attributes), 3768 an identifier (for the name being declared) 3769 or a null pointer (for the place in an absolute declarator 3770 where the name was omitted). 3771 For the last two cases, we have just exited the loop. 3772 3773 At this point, TYPE is the type of elements of an array, 3774 or for a function to return, or for a pointer to point to. 3775 After this sequence of ifs, TYPE is the type of the 3776 array or function or pointer, and DECLARATOR has had its 3777 outermost layer removed. */ 3778 3779 if (array_ptr_quals != NULL_TREE || array_parm_static) 3780 { 3781 /* Only the innermost declarator (making a parameter be of 3782 array type which is converted to pointer type) 3783 may have static or type qualifiers. */ 3784 error ("static or type qualifiers in non-parameter array declarator"); 3785 array_ptr_quals = NULL_TREE; 3786 array_parm_static = 0; 3787 } 3788 3789 if (TREE_CODE (declarator) == TREE_LIST) 3790 { 3791 /* We encode a declarator with embedded attributes using 3792 a TREE_LIST. */ 3793 tree attrs = TREE_PURPOSE (declarator); 3794 tree inner_decl; 3795 int attr_flags = 0; 3796 declarator = TREE_VALUE (declarator); 3797 inner_decl = declarator; 3798 while (inner_decl != NULL_TREE 3799 && TREE_CODE (inner_decl) == TREE_LIST) 3800 inner_decl = TREE_VALUE (inner_decl); 3801 if (inner_decl == NULL_TREE 3802 || TREE_CODE (inner_decl) == IDENTIFIER_NODE) 3803 attr_flags |= (int) ATTR_FLAG_DECL_NEXT; 3804 else if (TREE_CODE (inner_decl) == CALL_EXPR) 3805 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; 3806 else if (TREE_CODE (inner_decl) == ARRAY_REF) 3807 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; 3808 returned_attrs = decl_attributes (&type, 3809 chainon (returned_attrs, attrs), 3810 attr_flags); 3811 } 3812 else if (TREE_CODE (declarator) == ARRAY_REF) 3813 { 3814 tree itype = NULL_TREE; 3815 tree size = TREE_OPERAND (declarator, 1); 3816 /* The index is a signed object `sizetype' bits wide. */ 3817 tree index_type = c_common_signed_type (sizetype); 3818 3819 array_ptr_quals = TREE_TYPE (declarator); 3820 array_parm_static = TREE_STATIC (declarator); 3821 3822 declarator = TREE_OPERAND (declarator, 0); 3823 3824 /* Check for some types that there cannot be arrays of. */ 3825 3826 if (VOID_TYPE_P (type)) 3827 { 3828 error ("declaration of `%s' as array of voids", name); 3829 type = error_mark_node; 3830 } 3831 3832 if (TREE_CODE (type) == FUNCTION_TYPE) 3833 { 3834 error ("declaration of `%s' as array of functions", name); 3835 type = error_mark_node; 3836 } 3837 3838 if (pedantic && !in_system_header && flexible_array_type_p (type)) 3839 pedwarn ("invalid use of structure with flexible array member"); 3840 3841 if (size == error_mark_node) 3842 type = error_mark_node; 3843 3844 if (type == error_mark_node) 3845 continue; 3846 3847 /* If size was specified, set ITYPE to a range-type for that size. 3848 Otherwise, ITYPE remains null. finish_decl may figure it out 3849 from an initial value. */ 3850 3851 if (size) 3852 { 3853 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 3854 STRIP_TYPE_NOPS (size); 3855 3856 if (! INTEGRAL_TYPE_P (TREE_TYPE (size))) 3857 { 3858 error ("size of array `%s' has non-integer type", name); 3859 size = integer_one_node; 3860 } 3861 3862 if (pedantic && integer_zerop (size)) 3863 pedwarn ("ISO C forbids zero-size array `%s'", name); 3864 3865 if (TREE_CODE (size) == INTEGER_CST) 3866 { 3867 constant_expression_warning (size); 3868 if (tree_int_cst_sgn (size) < 0) 3869 { 3870 error ("size of array `%s' is negative", name); 3871 size = integer_one_node; 3872 } 3873 } 3874 else 3875 { 3876 /* Make sure the array size remains visibly nonconstant 3877 even if it is (eg) a const variable with known value. */ 3878 size_varies = 1; 3879 3880 if (!flag_isoc99 && pedantic) 3881 { 3882 if (TREE_CONSTANT (size)) 3883 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated", 3884 name); 3885 else 3886 pedwarn ("ISO C90 forbids variable-size array `%s'", 3887 name); 3888 } 3889 } 3890 3891 if (integer_zerop (size)) 3892 { 3893 /* A zero-length array cannot be represented with an 3894 unsigned index type, which is what we'll get with 3895 build_index_type. Create an open-ended range instead. */ 3896 itype = build_range_type (sizetype, size, NULL_TREE); 3897 } 3898 else 3899 { 3900 /* Compute the maximum valid index, that is, size - 1. 3901 Do the calculation in index_type, so that if it is 3902 a variable the computations will be done in the 3903 proper mode. */ 3904 itype = fold (build (MINUS_EXPR, index_type, 3905 convert (index_type, size), 3906 convert (index_type, size_one_node))); 3907 3908 /* If that overflowed, the array is too big. 3909 ??? While a size of INT_MAX+1 technically shouldn't 3910 cause an overflow (because we subtract 1), the overflow 3911 is recorded during the conversion to index_type, before 3912 the subtraction. Handling this case seems like an 3913 unnecessary complication. */ 3914 if (TREE_OVERFLOW (itype)) 3915 { 3916 error ("size of array `%s' is too large", name); 3917 type = error_mark_node; 3918 continue; 3919 } 3920 3921 if (size_varies) 3922 { 3923 /* We must be able to distinguish the 3924 SAVE_EXPR_CONTEXT for the variably-sized type 3925 so that we can set it correctly in 3926 set_save_expr_context. The convention is 3927 that all SAVE_EXPRs that need to be reset 3928 have NULL_TREE for their SAVE_EXPR_CONTEXT. */ 3929 tree cfd = current_function_decl; 3930 if (decl_context == PARM) 3931 current_function_decl = NULL_TREE; 3932 itype = variable_size (itype); 3933 if (decl_context == PARM) 3934 current_function_decl = cfd; 3935 } 3936 itype = build_index_type (itype); 3937 } 3938 } 3939 else if (decl_context == FIELD) 3940 { 3941 if (pedantic && !flag_isoc99 && !in_system_header) 3942 pedwarn ("ISO C90 does not support flexible array members"); 3943 3944 /* ISO C99 Flexible array members are effectively identical 3945 to GCC's zero-length array extension. */ 3946 itype = build_range_type (sizetype, size_zero_node, NULL_TREE); 3947 } 3948 3949 /* If pedantic, complain about arrays of incomplete types. */ 3950 3951 if (pedantic && !COMPLETE_TYPE_P (type)) 3952 pedwarn ("array type has incomplete element type"); 3953 3954 /* Build the array type itself, then merge any constancy or 3955 volatility into the target type. We must do it in this order 3956 to ensure that the TYPE_MAIN_VARIANT field of the array type 3957 is set correctly. */ 3958 3959 type = build_array_type (type, itype); 3960 if (type_quals) 3961 type = c_build_qualified_type (type, type_quals); 3962 3963 if (size_varies) 3964 C_TYPE_VARIABLE_SIZE (type) = 1; 3965 3966 /* The GCC extension for zero-length arrays differs from 3967 ISO flexible array members in that sizeof yields zero. */ 3968 if (size && integer_zerop (size)) 3969 { 3970 layout_type (type); 3971 TYPE_SIZE (type) = bitsize_zero_node; 3972 TYPE_SIZE_UNIT (type) = size_zero_node; 3973 } 3974 if (decl_context != PARM 3975 && (array_ptr_quals != NULL_TREE || array_parm_static)) 3976 { 3977 error ("static or type qualifiers in non-parameter array declarator"); 3978 array_ptr_quals = NULL_TREE; 3979 array_parm_static = 0; 3980 } 3981 } 3982 else if (TREE_CODE (declarator) == CALL_EXPR) 3983 { 3984 /* Say it's a definition only for the declarator closest to 3985 the identifier, apart possibly from some attributes. */ 3986 bool really_funcdef = false; 3987 tree arg_types; 3988 if (funcdef_flag) 3989 { 3990 tree t = TREE_OPERAND (declarator, 0); 3991 while (TREE_CODE (t) == TREE_LIST) 3992 t = TREE_VALUE (t); 3993 really_funcdef = (TREE_CODE (t) == IDENTIFIER_NODE); 3994 } 3995 3996 /* Declaring a function type. 3997 Make sure we have a valid type for the function to return. */ 3998 if (type == error_mark_node) 3999 continue; 4000 4001 size_varies = 0; 4002 4003 /* Warn about some types functions can't return. */ 4004 4005 if (TREE_CODE (type) == FUNCTION_TYPE) 4006 { 4007 error ("`%s' declared as function returning a function", name); 4008 type = integer_type_node; 4009 } 4010 if (TREE_CODE (type) == ARRAY_TYPE) 4011 { 4012 error ("`%s' declared as function returning an array", name); 4013 type = integer_type_node; 4014 } 4015 4016 /* Construct the function type and go to the next 4017 inner layer of declarator. */ 4018 4019 arg_types = grokparms (TREE_OPERAND (declarator, 1), 4020 really_funcdef); 4021 /* Type qualifiers before the return type of the function 4022 qualify the return type, not the function type. */ 4023 if (type_quals) 4024 { 4025 /* Type qualifiers on a function return type are normally 4026 permitted by the standard but have no effect, so give a 4027 warning at -Wextra. Qualifiers on a void return type have 4028 meaning as a GNU extension, and are banned on function 4029 definitions in ISO C. FIXME: strictly we shouldn't 4030 pedwarn for qualified void return types except on function 4031 definitions, but not doing so could lead to the undesirable 4032 state of a "volatile void" function return type not being 4033 warned about, and a use of the function being compiled 4034 with GNU semantics, with no diagnostics under -pedantic. */ 4035 if (VOID_TYPE_P (type) && pedantic && !in_system_header) 4036 pedwarn ("ISO C forbids qualified void function return type"); 4037 else if (extra_warnings 4038 && !(VOID_TYPE_P (type) 4039 && type_quals == TYPE_QUAL_VOLATILE)) 4040 warning ("type qualifiers ignored on function return type"); 4041 4042 type = c_build_qualified_type (type, type_quals); 4043 } 4044 type_quals = TYPE_UNQUALIFIED; 4045 4046 type = build_function_type (type, arg_types); 4047 declarator = TREE_OPERAND (declarator, 0); 4048 4049 /* Set the TYPE_CONTEXTs for each tagged type which is local to 4050 the formal parameter list of this FUNCTION_TYPE to point to 4051 the FUNCTION_TYPE node itself. */ 4052 4053 { 4054 tree link; 4055 4056 for (link = last_function_parm_tags; 4057 link; 4058 link = TREE_CHAIN (link)) 4059 TYPE_CONTEXT (TREE_VALUE (link)) = type; 4060 } 4061 } 4062 else if (TREE_CODE (declarator) == INDIRECT_REF) 4063 { 4064 /* Merge any constancy or volatility into the target type 4065 for the pointer. */ 4066 4067 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 4068 && type_quals) 4069 pedwarn ("ISO C forbids qualified function types"); 4070 if (type_quals) 4071 type = c_build_qualified_type (type, type_quals); 4072 type_quals = TYPE_UNQUALIFIED; 4073 size_varies = 0; 4074 4075 type = build_pointer_type (type); 4076 4077 /* Process a list of type modifier keywords 4078 (such as const or volatile) that were given inside the `*'. */ 4079 4080 if (TREE_TYPE (declarator)) 4081 { 4082 tree typemodlist; 4083 int erred = 0; 4084 4085 constp = 0; 4086 volatilep = 0; 4087 restrictp = 0; 4088 for (typemodlist = TREE_TYPE (declarator); typemodlist; 4089 typemodlist = TREE_CHAIN (typemodlist)) 4090 { 4091 tree qualifier = TREE_VALUE (typemodlist); 4092 4093 if (C_IS_RESERVED_WORD (qualifier)) 4094 { 4095 if (C_RID_CODE (qualifier) == RID_CONST) 4096 constp++; 4097 else if (C_RID_CODE (qualifier) == RID_VOLATILE) 4098 volatilep++; 4099 else if (C_RID_CODE (qualifier) == RID_RESTRICT) 4100 restrictp++; 4101 else 4102 erred++; 4103 } 4104 else 4105 erred++; 4106 } 4107 4108 if (erred) 4109 error ("invalid type modifier within pointer declarator"); 4110 if (pedantic && !flag_isoc99) 4111 { 4112 if (constp > 1) 4113 pedwarn ("duplicate `const'"); 4114 if (volatilep > 1) 4115 pedwarn ("duplicate `volatile'"); 4116 if (restrictp > 1) 4117 pedwarn ("duplicate `restrict'"); 4118 } 4119 4120 type_quals = ((constp ? TYPE_QUAL_CONST : 0) 4121 | (restrictp ? TYPE_QUAL_RESTRICT : 0) 4122 | (volatilep ? TYPE_QUAL_VOLATILE : 0)); 4123 } 4124 4125 declarator = TREE_OPERAND (declarator, 0); 4126 } 4127 else 4128 abort (); 4129 4130 } 4131 4132 /* Now TYPE has the actual type. */ 4133 4134 /* Check the type and width of a bit-field. */ 4135 if (bitfield) 4136 check_bitfield_type_and_width (&type, width, orig_name); 4137 4138 /* Did array size calculations overflow? */ 4139 4140 if (TREE_CODE (type) == ARRAY_TYPE 4141 && COMPLETE_TYPE_P (type) 4142 && TREE_OVERFLOW (TYPE_SIZE (type))) 4143 { 4144 error ("size of array `%s' is too large", name); 4145 /* If we proceed with the array type as it is, we'll eventually 4146 crash in tree_low_cst(). */ 4147 type = error_mark_node; 4148 } 4149 4150 /* If this is declaring a typedef name, return a TYPE_DECL. */ 4151 4152 if (specbits & (1 << (int) RID_TYPEDEF)) 4153 { 4154 tree decl; 4155 /* Note that the grammar rejects storage classes 4156 in typenames, fields or parameters */ 4157 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 4158 && type_quals) 4159 pedwarn ("ISO C forbids qualified function types"); 4160 if (type_quals) 4161 type = c_build_qualified_type (type, type_quals); 4162 decl = build_decl (TYPE_DECL, declarator, type); 4163 if ((specbits & (1 << (int) RID_SIGNED)) 4164 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) 4165 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 4166 decl_attributes (&decl, returned_attrs, 0); 4167 return decl; 4168 } 4169 4170 /* Detect the case of an array type of unspecified size 4171 which came, as such, direct from a typedef name. 4172 We must copy the type, so that each identifier gets 4173 a distinct type, so that each identifier's size can be 4174 controlled separately by its own initializer. */ 4175 4176 if (type != 0 && typedef_type != 0 4177 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0 4178 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)) 4179 { 4180 type = build_array_type (TREE_TYPE (type), 0); 4181 if (size_varies) 4182 C_TYPE_VARIABLE_SIZE (type) = 1; 4183 } 4184 4185 /* If this is a type name (such as, in a cast or sizeof), 4186 compute the type and return it now. */ 4187 4188 if (decl_context == TYPENAME) 4189 { 4190 /* Note that the grammar rejects storage classes 4191 in typenames, fields or parameters */ 4192 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE 4193 && type_quals) 4194 pedwarn ("ISO C forbids const or volatile function types"); 4195 if (type_quals) 4196 type = c_build_qualified_type (type, type_quals); 4197 decl_attributes (&type, returned_attrs, 0); 4198 return type; 4199 } 4200 4201 /* Aside from typedefs and type names (handle above), 4202 `void' at top level (not within pointer) 4203 is allowed only in public variables. 4204 We don't complain about parms either, but that is because 4205 a better error message can be made later. */ 4206 4207 if (VOID_TYPE_P (type) && decl_context != PARM 4208 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE) 4209 && ((specbits & (1 << (int) RID_EXTERN)) 4210 || (current_scope == global_scope 4211 && !(specbits 4212 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER))))))) 4213 { 4214 error ("variable or field `%s' declared void", name); 4215 type = integer_type_node; 4216 } 4217 4218 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL 4219 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ 4220 4221 { 4222 tree decl; 4223 4224 if (decl_context == PARM) 4225 { 4226 tree type_as_written; 4227 tree promoted_type; 4228 4229 /* A parameter declared as an array of T is really a pointer to T. 4230 One declared as a function is really a pointer to a function. */ 4231 4232 if (TREE_CODE (type) == ARRAY_TYPE) 4233 { 4234 /* Transfer const-ness of array into that of type pointed to. */ 4235 type = TREE_TYPE (type); 4236 if (type_quals) 4237 type = c_build_qualified_type (type, type_quals); 4238 type = build_pointer_type (type); 4239 type_quals = TYPE_UNQUALIFIED; 4240 if (array_ptr_quals) 4241 { 4242 tree new_ptr_quals, new_ptr_attrs; 4243 int erred = 0; 4244 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs); 4245 /* We don't yet implement attributes in this context. */ 4246 if (new_ptr_attrs != NULL_TREE) 4247 warning ("attributes in parameter array declarator ignored"); 4248 4249 constp = 0; 4250 volatilep = 0; 4251 restrictp = 0; 4252 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals)) 4253 { 4254 tree qualifier = TREE_VALUE (new_ptr_quals); 4255 4256 if (C_IS_RESERVED_WORD (qualifier)) 4257 { 4258 if (C_RID_CODE (qualifier) == RID_CONST) 4259 constp++; 4260 else if (C_RID_CODE (qualifier) == RID_VOLATILE) 4261 volatilep++; 4262 else if (C_RID_CODE (qualifier) == RID_RESTRICT) 4263 restrictp++; 4264 else 4265 erred++; 4266 } 4267 else 4268 erred++; 4269 } 4270 4271 if (erred) 4272 error ("invalid type modifier within array declarator"); 4273 4274 type_quals = ((constp ? TYPE_QUAL_CONST : 0) 4275 | (restrictp ? TYPE_QUAL_RESTRICT : 0) 4276 | (volatilep ? TYPE_QUAL_VOLATILE : 0)); 4277 } 4278 size_varies = 0; 4279 } 4280 else if (TREE_CODE (type) == FUNCTION_TYPE) 4281 { 4282 if (pedantic && type_quals) 4283 pedwarn ("ISO C forbids qualified function types"); 4284 if (type_quals) 4285 type = c_build_qualified_type (type, type_quals); 4286 type = build_pointer_type (type); 4287 type_quals = TYPE_UNQUALIFIED; 4288 } 4289 else if (type_quals) 4290 type = c_build_qualified_type (type, type_quals); 4291 4292 type_as_written = type; 4293 4294 decl = build_decl (PARM_DECL, declarator, type); 4295 if (size_varies) 4296 C_DECL_VARIABLE_SIZE (decl) = 1; 4297 4298 /* Compute the type actually passed in the parmlist, 4299 for the case where there is no prototype. 4300 (For example, shorts and chars are passed as ints.) 4301 When there is a prototype, this is overridden later. */ 4302 4303 if (type == error_mark_node) 4304 promoted_type = type; 4305 else 4306 promoted_type = c_type_promotes_to (type); 4307 4308 DECL_ARG_TYPE (decl) = promoted_type; 4309 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written; 4310 } 4311 else if (decl_context == FIELD) 4312 { 4313 /* Structure field. It may not be a function. */ 4314 4315 if (TREE_CODE (type) == FUNCTION_TYPE) 4316 { 4317 error ("field `%s' declared as a function", name); 4318 type = build_pointer_type (type); 4319 } 4320 else if (TREE_CODE (type) != ERROR_MARK 4321 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type)) 4322 { 4323 error ("field `%s' has incomplete type", name); 4324 type = error_mark_node; 4325 } 4326 /* Move type qualifiers down to element of an array. */ 4327 if (TREE_CODE (type) == ARRAY_TYPE && type_quals) 4328 type = build_array_type (c_build_qualified_type (TREE_TYPE (type), 4329 type_quals), 4330 TYPE_DOMAIN (type)); 4331 decl = build_decl (FIELD_DECL, declarator, type); 4332 DECL_NONADDRESSABLE_P (decl) = bitfield; 4333 4334 if (size_varies) 4335 C_DECL_VARIABLE_SIZE (decl) = 1; 4336 } 4337 else if (TREE_CODE (type) == FUNCTION_TYPE) 4338 { 4339 /* Every function declaration is "external" 4340 except for those which are inside a function body 4341 in which `auto' is used. 4342 That is a case not specified by ANSI C, 4343 and we use it for forward declarations for nested functions. */ 4344 int extern_ref = (!(specbits & (1 << (int) RID_AUTO)) 4345 || current_scope == global_scope); 4346 4347 if (specbits & (1 << (int) RID_AUTO) 4348 && (pedantic || current_scope == global_scope)) 4349 pedwarn ("invalid storage class for function `%s'", name); 4350 if (specbits & (1 << (int) RID_REGISTER)) 4351 error ("invalid storage class for function `%s'", name); 4352 if (specbits & (1 << (int) RID_THREAD)) 4353 error ("invalid storage class for function `%s'", name); 4354 /* Function declaration not at file scope. 4355 Storage classes other than `extern' are not allowed 4356 and `extern' makes no difference. */ 4357 if (current_scope != global_scope 4358 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE))) 4359 && pedantic) 4360 pedwarn ("invalid storage class for function `%s'", name); 4361 4362 decl = build_decl (FUNCTION_DECL, declarator, type); 4363 decl = build_decl_attribute_variant (decl, decl_attr); 4364 4365 DECL_LANG_SPECIFIC (decl) 4366 = ggc_alloc_cleared (sizeof (struct lang_decl)); 4367 4368 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl)) 4369 pedwarn ("ISO C forbids qualified function types"); 4370 4371 /* GNU C interprets a `volatile void' return type to indicate 4372 that the function does not return. */ 4373 if ((type_quals & TYPE_QUAL_VOLATILE) 4374 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) 4375 warning ("`noreturn' function returns non-void value"); 4376 4377 if (extern_ref) 4378 DECL_EXTERNAL (decl) = 1; 4379 /* Record absence of global scope for `static' or `auto'. */ 4380 TREE_PUBLIC (decl) 4381 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO))); 4382 4383 if (defaulted_int) 4384 C_FUNCTION_IMPLICIT_INT (decl) = 1; 4385 4386 /* Record presence of `inline', if it is reasonable. */ 4387 if (MAIN_NAME_P (declarator)) 4388 { 4389 if (inlinep) 4390 warning ("cannot inline function `main'"); 4391 } 4392 else if (inlinep) 4393 { 4394 /* Record that the function is declared `inline'. */ 4395 DECL_DECLARED_INLINE_P (decl) = 1; 4396 4397 /* Do not mark bare declarations as DECL_INLINE. Doing so 4398 in the presence of multiple declarations can result in 4399 the abstract origin pointing between the declarations, 4400 which will confuse dwarf2out. */ 4401 if (initialized) 4402 { 4403 DECL_INLINE (decl) = 1; 4404 if (specbits & (1 << (int) RID_EXTERN)) 4405 current_extern_inline = 1; 4406 } 4407 } 4408 /* If -finline-functions, assume it can be inlined. This does 4409 two things: let the function be deferred until it is actually 4410 needed, and let dwarf2 know that the function is inlinable. */ 4411 else if (flag_inline_trees == 2 && initialized) 4412 DECL_INLINE (decl) = 1; 4413 } 4414 else 4415 { 4416 /* It's a variable. */ 4417 /* An uninitialized decl with `extern' is a reference. */ 4418 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN)); 4419 4420 /* Move type qualifiers down to element of an array. */ 4421 if (TREE_CODE (type) == ARRAY_TYPE && type_quals) 4422 { 4423 int saved_align = TYPE_ALIGN(type); 4424 type = build_array_type (c_build_qualified_type (TREE_TYPE (type), 4425 type_quals), 4426 TYPE_DOMAIN (type)); 4427 TYPE_ALIGN (type) = saved_align; 4428 } 4429 else if (type_quals) 4430 type = c_build_qualified_type (type, type_quals); 4431 4432 /* It is invalid to create an `extern' declaration for a 4433 variable if there is a global declaration that is 4434 `static' and the global declaration is not visible. */ 4435 if (extern_ref && current_scope != global_scope) 4436 { 4437 tree global_decl; 4438 4439 global_decl = identifier_global_value (declarator); 4440 if (global_decl 4441 && TREE_CODE (global_decl) == VAR_DECL 4442 && lookup_name (declarator) != global_decl 4443 && !TREE_PUBLIC (global_decl)) 4444 error ("variable previously declared `static' redeclared " 4445 "`extern'"); 4446 } 4447 4448 decl = build_decl (VAR_DECL, declarator, type); 4449 if (size_varies) 4450 C_DECL_VARIABLE_SIZE (decl) = 1; 4451 4452 if (inlinep) 4453 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl); 4454 4455 DECL_EXTERNAL (decl) = extern_ref; 4456 4457 /* At file scope, the presence of a `static' or `register' storage 4458 class specifier, or the absence of all storage class specifiers 4459 makes this declaration a definition (perhaps tentative). Also, 4460 the absence of both `static' and `register' makes it public. */ 4461 if (current_scope == global_scope) 4462 { 4463 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC) 4464 | (1 << (int) RID_REGISTER))); 4465 TREE_STATIC (decl) = !extern_ref; 4466 } 4467 /* Not at file scope, only `static' makes a static definition. */ 4468 else 4469 { 4470 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0; 4471 TREE_PUBLIC (decl) = extern_ref; 4472 } 4473 4474 if (specbits & 1 << (int) RID_THREAD) 4475 { 4476 if (targetm.have_tls) 4477 DECL_THREAD_LOCAL (decl) = 1; 4478 else 4479 /* A mere warning is sure to result in improper semantics 4480 at runtime. Don't bother to allow this to compile. */ 4481 error ("thread-local storage not supported for this target"); 4482 } 4483 } 4484 4485 /* Record `register' declaration for warnings on & 4486 and in case doing stupid register allocation. */ 4487 4488 if (specbits & (1 << (int) RID_REGISTER)) 4489 DECL_REGISTER (decl) = 1; 4490 4491 /* Record constancy and volatility. */ 4492 c_apply_type_quals_to_decl (type_quals, decl); 4493 4494 /* If a type has volatile components, it should be stored in memory. 4495 Otherwise, the fact that those components are volatile 4496 will be ignored, and would even crash the compiler. */ 4497 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))) 4498 c_mark_addressable (decl); 4499 4500#ifdef ENABLE_CHECKING 4501 /* This is the earliest point at which we might know the assembler 4502 name of a variable. Thus, if it's known before this, die horribly. */ 4503 if (DECL_ASSEMBLER_NAME_SET_P (decl)) 4504 abort (); 4505#endif 4506 4507 decl_attributes (&decl, returned_attrs, 0); 4508 4509 return decl; 4510 } 4511} 4512 4513/* Decode the parameter-list info for a function type or function definition. 4514 The argument is the value returned by `get_parm_info' (or made in parse.y 4515 if there is an identifier list instead of a parameter decl list). 4516 These two functions are separate because when a function returns 4517 or receives functions then each is called multiple times but the order 4518 of calls is different. The last call to `grokparms' is always the one 4519 that contains the formal parameter names of a function definition. 4520 4521 Store in `last_function_parms' a chain of the decls of parms. 4522 Also store in `last_function_parm_tags' a chain of the struct, union, 4523 and enum tags declared among the parms. 4524 4525 Return a list of arg types to use in the FUNCTION_TYPE for this function. 4526 4527 FUNCDEF_FLAG is nonzero for a function definition, 0 for 4528 a mere declaration. A nonempty identifier-list gets an error message 4529 when FUNCDEF_FLAG is zero. */ 4530 4531static tree 4532grokparms (tree parms_info, int funcdef_flag) 4533{ 4534 tree first_parm = TREE_CHAIN (parms_info); 4535 4536 last_function_parms = TREE_PURPOSE (parms_info); 4537 last_function_parm_tags = TREE_VALUE (parms_info); 4538 last_function_parm_others = TREE_TYPE (parms_info); 4539 4540 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag 4541 && !in_system_header) 4542 warning ("function declaration isn't a prototype"); 4543 4544 if (first_parm != 0 4545 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE) 4546 { 4547 if (! funcdef_flag) 4548 pedwarn ("parameter names (without types) in function declaration"); 4549 4550 last_function_parms = first_parm; 4551 return 0; 4552 } 4553 else 4554 { 4555 tree parm; 4556 tree typelt; 4557 /* If the arg types are incomplete in a declaration, 4558 they must include undefined tags. 4559 These tags can never be defined in the scope of the declaration, 4560 so the types can never be completed, 4561 and no call can be compiled successfully. */ 4562 4563 for (parm = last_function_parms, typelt = first_parm; 4564 parm; 4565 parm = TREE_CHAIN (parm)) 4566 /* Skip over any enumeration constants declared here. */ 4567 if (TREE_CODE (parm) == PARM_DECL) 4568 { 4569 /* Barf if the parameter itself has an incomplete type. */ 4570 tree type = TREE_VALUE (typelt); 4571 if (type == error_mark_node) 4572 continue; 4573 if (!COMPLETE_TYPE_P (type)) 4574 { 4575 if (funcdef_flag && DECL_NAME (parm) != 0) 4576 error ("parameter `%s' has incomplete type", 4577 IDENTIFIER_POINTER (DECL_NAME (parm))); 4578 else 4579 warning ("parameter has incomplete type"); 4580 if (funcdef_flag) 4581 { 4582 TREE_VALUE (typelt) = error_mark_node; 4583 TREE_TYPE (parm) = error_mark_node; 4584 } 4585 } 4586 typelt = TREE_CHAIN (typelt); 4587 } 4588 4589 return first_parm; 4590 } 4591} 4592 4593/* Return a tree_list node with info on a parameter list just parsed. 4594 The TREE_PURPOSE is a list of decls of those parms. 4595 The TREE_VALUE is a list of structure, union and enum tags defined. 4596 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE. 4597 The TREE_TYPE is a list of non-parameter decls which appeared with the 4598 parameters. 4599 This tree_list node is later fed to `grokparms'. 4600 4601 VOID_AT_END nonzero means append `void' to the end of the type-list. 4602 Zero means the parmlist ended with an ellipsis so don't append `void'. */ 4603 4604tree 4605get_parm_info (int void_at_end) 4606{ 4607 tree decl, type, list; 4608 tree types = 0; 4609 tree *last_type = &types; 4610 tree tags = current_scope->tags; 4611 tree parms = current_scope->parms; 4612 tree others = current_scope->names; 4613 static bool explained_incomplete_types = false; 4614 bool gave_void_only_once_err = false; 4615 4616 /* Just "void" (and no ellipsis) is special. There are really no parms. 4617 But if the "void" is qualified (by "const" or "volatile"), or has a 4618 storage class specifier ("register"), then the behavior is undefined; 4619 issue an error. Typedefs for "void" are OK (see DR#157). */ 4620 if (void_at_end && parms != 0 4621 && TREE_CHAIN (parms) == 0 4622 && VOID_TYPE_P (TREE_TYPE (parms)) 4623 && !DECL_NAME (parms)) 4624 { 4625 if (TREE_THIS_VOLATILE (parms) 4626 || TREE_READONLY (parms) 4627 || DECL_REGISTER (parms)) 4628 error ("\"void\" as only parameter may not be qualified"); 4629 4630 return tree_cons (0, 0, tree_cons (0, void_type_node, 0)); 4631 } 4632 4633 /* Sanity check all of the parameter declarations. */ 4634 for (decl = parms; decl; decl = TREE_CHAIN (decl)) 4635 { 4636 if (TREE_CODE (decl) != PARM_DECL) 4637 abort (); 4638 if (TREE_ASM_WRITTEN (decl)) 4639 abort (); 4640 4641 /* Since there is a prototype, args are passed in their 4642 declared types. The back end may override this. */ 4643 type = TREE_TYPE (decl); 4644 DECL_ARG_TYPE (decl) = type; 4645 4646 /* Check for (..., void, ...) and issue an error. */ 4647 if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err) 4648 { 4649 error ("\"void\" must be the only parameter"); 4650 gave_void_only_once_err = true; 4651 } 4652 4653 type = build_tree_list (0, type); 4654 *last_type = type; 4655 last_type = &TREE_CHAIN (type); 4656 } 4657 4658 /* Check the list of non-parameter decls for any forward parm decls 4659 that never got real decls. */ 4660 for (decl = others; decl; decl = TREE_CHAIN (decl)) 4661 if (TREE_CODE (decl) == PARM_DECL) 4662 { 4663 if (!TREE_ASM_WRITTEN (decl)) 4664 abort (); 4665 4666 error ("%Jparameter \"%D\" has just a forward declaration", 4667 decl, decl); 4668 } 4669 4670 /* Warn about any struct, union or enum tags defined within this 4671 list. The scope of such types is limited to this declaration, 4672 which is rarely if ever desirable (it's impossible to call such 4673 a function with type-correct arguments). */ 4674 for (decl = tags; decl; decl = TREE_CHAIN (decl)) 4675 { 4676 enum tree_code code = TREE_CODE (TREE_VALUE (decl)); 4677 const char *keyword; 4678 /* An anonymous union parm type is meaningful as a GNU extension. 4679 So don't warn for that. */ 4680 if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic) 4681 continue; 4682 4683 /* The keyword should not be translated. */ 4684 switch (code) 4685 { 4686 case RECORD_TYPE: keyword = "struct"; break; 4687 case UNION_TYPE: keyword = "union"; break; 4688 case ENUMERAL_TYPE: keyword = "enum"; break; 4689 default: abort (); 4690 } 4691 4692 if (TREE_PURPOSE (decl)) 4693 /* The first %s will be one of 'struct', 'union', or 'enum'. */ 4694 warning ("\"%s %s\" declared inside parameter list", 4695 keyword, IDENTIFIER_POINTER (TREE_PURPOSE (decl))); 4696 else 4697 /* The %s will be one of 'struct', 'union', or 'enum'. */ 4698 warning ("anonymous %s declared inside parameter list", keyword); 4699 4700 if (! explained_incomplete_types) 4701 { 4702 warning ("its scope is only this definition or declaration," 4703 " which is probably not what you want"); 4704 explained_incomplete_types = true; 4705 } 4706 } 4707 4708 4709 if (void_at_end) 4710 { 4711 type = build_tree_list (0, void_type_node); 4712 *last_type = type; 4713 } 4714 4715 list = tree_cons (parms, tags, types); 4716 TREE_TYPE (list) = others; 4717 return list; 4718} 4719 4720/* Get the struct, enum or union (CODE says which) with tag NAME. 4721 Define the tag as a forward-reference if it is not defined. */ 4722 4723tree 4724xref_tag (enum tree_code code, tree name) 4725{ 4726 /* If a cross reference is requested, look up the type 4727 already defined for this tag and return it. */ 4728 4729 tree ref = lookup_tag (code, name, 0); 4730 /* If this is the right type of tag, return what we found. 4731 (This reference will be shadowed by shadow_tag later if appropriate.) 4732 If this is the wrong type of tag, do not return it. If it was the 4733 wrong type in the same scope, we will have had an error 4734 message already; if in a different scope and declaring 4735 a name, pending_xref_error will give an error message; but if in a 4736 different scope and not declaring a name, this tag should 4737 shadow the previous declaration of a different type of tag, and 4738 this would not work properly if we return the reference found. 4739 (For example, with "struct foo" in an outer scope, "union foo;" 4740 must shadow that tag with a new one of union type.) */ 4741 if (ref && TREE_CODE (ref) == code) 4742 return ref; 4743 4744 /* If no such tag is yet defined, create a forward-reference node 4745 and record it as the "definition". 4746 When a real declaration of this type is found, 4747 the forward-reference will be altered into a real type. */ 4748 4749 ref = make_node (code); 4750 if (code == ENUMERAL_TYPE) 4751 { 4752 /* Give the type a default layout like unsigned int 4753 to avoid crashing if it does not get defined. */ 4754 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node); 4755 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); 4756 TYPE_USER_ALIGN (ref) = 0; 4757 TREE_UNSIGNED (ref) = 1; 4758 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); 4759 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); 4760 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); 4761 } 4762 4763 pushtag (name, ref); 4764 4765 return ref; 4766} 4767 4768/* Make sure that the tag NAME is defined *in the current scope* 4769 at least as a forward reference. 4770 CODE says which kind of tag NAME ought to be. */ 4771 4772tree 4773start_struct (enum tree_code code, tree name) 4774{ 4775 /* If there is already a tag defined at this scope 4776 (as a forward reference), just return it. */ 4777 4778 tree ref = 0; 4779 4780 if (name != 0) 4781 ref = lookup_tag (code, name, 1); 4782 if (ref && TREE_CODE (ref) == code) 4783 { 4784 if (TYPE_FIELDS (ref)) 4785 { 4786 if (code == UNION_TYPE) 4787 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name)); 4788 else 4789 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name)); 4790 } 4791 } 4792 else 4793 { 4794 /* Otherwise create a forward-reference just so the tag is in scope. */ 4795 4796 ref = make_node (code); 4797 pushtag (name, ref); 4798 } 4799 4800 C_TYPE_BEING_DEFINED (ref) = 1; 4801 TYPE_PACKED (ref) = flag_pack_struct; 4802 return ref; 4803} 4804 4805/* Process the specs, declarator (NULL if omitted) and width (NULL if omitted) 4806 of a structure component, returning a FIELD_DECL node. 4807 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node. 4808 4809 This is done during the parsing of the struct declaration. 4810 The FIELD_DECL nodes are chained together and the lot of them 4811 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */ 4812 4813tree 4814grokfield (tree declarator, tree declspecs, tree width) 4815{ 4816 tree value; 4817 4818 if (declarator == NULL_TREE && width == NULL_TREE) 4819 { 4820 /* This is an unnamed decl. 4821 4822 If we have something of the form "union { list } ;" then this 4823 is the anonymous union extension. Similarly for struct. 4824 4825 If this is something of the form "struct foo;", then 4826 If MS extensions are enabled, this is handled as an 4827 anonymous struct. 4828 Otherwise this is a forward declaration of a structure tag. 4829 4830 If this is something of the form "foo;" and foo is a TYPE_DECL, then 4831 If MS extensions are enabled and foo names a structure, then 4832 again this is an anonymous struct. 4833 Otherwise this is an error. 4834 4835 Oh what a horrid tangled web we weave. I wonder if MS consciously 4836 took this from Plan 9 or if it was an accident of implementation 4837 that took root before someone noticed the bug... */ 4838 4839 tree type = TREE_VALUE (declspecs); 4840 4841 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL) 4842 type = TREE_TYPE (type); 4843 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE) 4844 { 4845 if (flag_ms_extensions) 4846 ; /* ok */ 4847 else if (flag_iso) 4848 goto warn_unnamed_field; 4849 else if (TYPE_NAME (type) == NULL) 4850 ; /* ok */ 4851 else 4852 goto warn_unnamed_field; 4853 } 4854 else 4855 { 4856 warn_unnamed_field: 4857 warning ("declaration does not declare anything"); 4858 return NULL_TREE; 4859 } 4860 } 4861 4862 value = grokdeclarator (declarator, declspecs, FIELD, 0, 4863 width ? &width : NULL); 4864 4865 finish_decl (value, NULL_TREE, NULL_TREE); 4866 DECL_INITIAL (value) = width; 4867 4868 return value; 4869} 4870 4871/* Generate an error for any duplicate field names in FIELDLIST. Munge 4872 the list such that this does not present a problem later. */ 4873 4874static void 4875detect_field_duplicates (tree fieldlist) 4876{ 4877 tree x, y; 4878 int timeout = 10; 4879 4880 /* First, see if there are more than "a few" fields. 4881 This is trivially true if there are zero or one fields. */ 4882 if (!fieldlist) 4883 return; 4884 x = TREE_CHAIN (fieldlist); 4885 if (!x) 4886 return; 4887 do { 4888 timeout--; 4889 x = TREE_CHAIN (x); 4890 } while (timeout > 0 && x); 4891 4892 /* If there were "few" fields, avoid the overhead of allocating 4893 a hash table. Instead just do the nested traversal thing. */ 4894 if (timeout > 0) 4895 { 4896 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x)) 4897 if (DECL_NAME (x)) 4898 { 4899 for (y = fieldlist; y != x; y = TREE_CHAIN (y)) 4900 if (DECL_NAME (y) == DECL_NAME (x)) 4901 { 4902 error ("%Jduplicate member '%D'", x, x); 4903 DECL_NAME (x) = NULL_TREE; 4904 } 4905 } 4906 } 4907 else 4908 { 4909 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); 4910 void **slot; 4911 4912 for (x = fieldlist; x ; x = TREE_CHAIN (x)) 4913 if ((y = DECL_NAME (x)) != 0) 4914 { 4915 slot = htab_find_slot (htab, y, INSERT); 4916 if (*slot) 4917 { 4918 error ("%Jduplicate member '%D'", x, x); 4919 DECL_NAME (x) = NULL_TREE; 4920 } 4921 *slot = y; 4922 } 4923 4924 htab_delete (htab); 4925 } 4926} 4927 4928/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. 4929 FIELDLIST is a chain of FIELD_DECL nodes for the fields. 4930 ATTRIBUTES are attributes to be applied to the structure. */ 4931 4932tree 4933finish_struct (tree t, tree fieldlist, tree attributes) 4934{ 4935 tree x; 4936 int toplevel = global_scope == current_scope; 4937 int saw_named_field; 4938 4939 /* If this type was previously laid out as a forward reference, 4940 make sure we lay it out again. */ 4941 4942 TYPE_SIZE (t) = 0; 4943 4944 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 4945 4946 /* Nameless union parm types are useful as GCC extension. */ 4947 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic) 4948 /* Otherwise, warn about any struct or union def. in parmlist. */ 4949 if (in_parm_level_p ()) 4950 { 4951 if (pedantic) 4952 pedwarn ("%s defined inside parms", 4953 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure")); 4954 else 4955 warning ("%s defined inside parms", 4956 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure")); 4957 } 4958 4959 if (pedantic) 4960 { 4961 for (x = fieldlist; x; x = TREE_CHAIN (x)) 4962 if (DECL_NAME (x) != 0) 4963 break; 4964 4965 if (x == 0) 4966 pedwarn ("%s has no %s", 4967 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"), 4968 fieldlist ? _("named members") : _("members")); 4969 } 4970 4971 /* Install struct as DECL_CONTEXT of each field decl. 4972 Also process specified field sizes,m which is found in the DECL_INITIAL. 4973 Store 0 there, except for ": 0" fields (so we can find them 4974 and delete them, below). */ 4975 4976 saw_named_field = 0; 4977 for (x = fieldlist; x; x = TREE_CHAIN (x)) 4978 { 4979 DECL_CONTEXT (x) = t; 4980 DECL_PACKED (x) |= TYPE_PACKED (t); 4981 4982 /* If any field is const, the structure type is pseudo-const. */ 4983 if (TREE_READONLY (x)) 4984 C_TYPE_FIELDS_READONLY (t) = 1; 4985 else 4986 { 4987 /* A field that is pseudo-const makes the structure likewise. */ 4988 tree t1 = TREE_TYPE (x); 4989 while (TREE_CODE (t1) == ARRAY_TYPE) 4990 t1 = TREE_TYPE (t1); 4991 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE) 4992 && C_TYPE_FIELDS_READONLY (t1)) 4993 C_TYPE_FIELDS_READONLY (t) = 1; 4994 } 4995 4996 /* Any field that is volatile means variables of this type must be 4997 treated in some ways as volatile. */ 4998 if (TREE_THIS_VOLATILE (x)) 4999 C_TYPE_FIELDS_VOLATILE (t) = 1; 5000 5001 /* Any field of nominal variable size implies structure is too. */ 5002 if (C_DECL_VARIABLE_SIZE (x)) 5003 C_TYPE_VARIABLE_SIZE (t) = 1; 5004 5005 /* Detect invalid nested redefinition. */ 5006 if (TREE_TYPE (x) == t) 5007 error ("nested redefinition of `%s'", 5008 IDENTIFIER_POINTER (TYPE_NAME (t))); 5009 5010 if (DECL_INITIAL (x)) 5011 { 5012 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1); 5013 DECL_SIZE (x) = bitsize_int (width); 5014 DECL_BIT_FIELD (x) = 1; 5015 SET_DECL_C_BIT_FIELD (x); 5016 } 5017 5018 DECL_INITIAL (x) = 0; 5019 5020 /* Detect flexible array member in an invalid context. */ 5021 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE 5022 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE 5023 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE 5024 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) 5025 { 5026 if (TREE_CODE (t) == UNION_TYPE) 5027 { 5028 error ("%Jflexible array member in union", x); 5029 TREE_TYPE (x) = error_mark_node; 5030 } 5031 else if (TREE_CHAIN (x) != NULL_TREE) 5032 { 5033 error ("%Jflexible array member not at end of struct", x); 5034 TREE_TYPE (x) = error_mark_node; 5035 } 5036 else if (! saw_named_field) 5037 { 5038 error ("%Jflexible array member in otherwise empty struct", x); 5039 TREE_TYPE (x) = error_mark_node; 5040 } 5041 } 5042 5043 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE 5044 && flexible_array_type_p (TREE_TYPE (x))) 5045 pedwarn ("%Jinvalid use of structure with flexible array member", x); 5046 5047 if (DECL_NAME (x)) 5048 saw_named_field = 1; 5049 } 5050 5051 detect_field_duplicates (fieldlist); 5052 5053 /* Now we have the nearly final fieldlist. Record it, 5054 then lay out the structure or union (including the fields). */ 5055 5056 TYPE_FIELDS (t) = fieldlist; 5057 5058 layout_type (t); 5059 5060 /* Delete all zero-width bit-fields from the fieldlist. */ 5061 { 5062 tree *fieldlistp = &fieldlist; 5063 while (*fieldlistp) 5064 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)) 5065 *fieldlistp = TREE_CHAIN (*fieldlistp); 5066 else 5067 fieldlistp = &TREE_CHAIN (*fieldlistp); 5068 } 5069 5070 /* Now we have the truly final field list. 5071 Store it in this type and in the variants. */ 5072 5073 TYPE_FIELDS (t) = fieldlist; 5074 5075 /* If there are lots of fields, sort so we can look through them fast. 5076 We arbitrarily consider 16 or more elts to be "a lot". */ 5077 5078 { 5079 int len = 0; 5080 5081 for (x = fieldlist; x; x = TREE_CHAIN (x)) 5082 { 5083 if (len > 15 || DECL_NAME (x) == NULL) 5084 break; 5085 len += 1; 5086 } 5087 5088 if (len > 15) 5089 { 5090 tree *field_array; 5091 struct lang_type *space; 5092 struct sorted_fields_type *space2; 5093 5094 len += list_length (x); 5095 5096 /* Use the same allocation policy here that make_node uses, to 5097 ensure that this lives as long as the rest of the struct decl. 5098 All decls in an inline function need to be saved. */ 5099 5100 space = ggc_alloc (sizeof (struct lang_type)); 5101 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree)); 5102 5103 len = 0; 5104 space->s = space2; 5105 field_array = &space2->elts[0]; 5106 for (x = fieldlist; x; x = TREE_CHAIN (x)) 5107 { 5108 field_array[len++] = x; 5109 5110 /* If there is anonymous struct or union, break out of the loop. */ 5111 if (DECL_NAME (x) == NULL) 5112 break; 5113 } 5114 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */ 5115 if (x == NULL) 5116 { 5117 TYPE_LANG_SPECIFIC (t) = space; 5118 TYPE_LANG_SPECIFIC (t)->s->len = len; 5119 field_array = TYPE_LANG_SPECIFIC (t)->s->elts; 5120 qsort (field_array, len, sizeof (tree), field_decl_cmp); 5121 } 5122 } 5123 } 5124 5125 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) 5126 { 5127 TYPE_FIELDS (x) = TYPE_FIELDS (t); 5128 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t); 5129 TYPE_ALIGN (x) = TYPE_ALIGN (t); 5130 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t); 5131 } 5132 5133 /* If this was supposed to be a transparent union, but we can't 5134 make it one, warn and turn off the flag. */ 5135 if (TREE_CODE (t) == UNION_TYPE 5136 && TYPE_TRANSPARENT_UNION (t) 5137 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))) 5138 { 5139 TYPE_TRANSPARENT_UNION (t) = 0; 5140 warning ("union cannot be made transparent"); 5141 } 5142 5143 /* If this structure or union completes the type of any previous 5144 variable declaration, lay it out and output its rtl. */ 5145 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)); 5146 x; 5147 x = TREE_CHAIN (x)) 5148 { 5149 tree decl = TREE_VALUE (x); 5150 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) 5151 layout_array_type (TREE_TYPE (decl)); 5152 if (TREE_CODE (decl) != TYPE_DECL) 5153 { 5154 layout_decl (decl, 0); 5155 if (c_dialect_objc ()) 5156 objc_check_decl (decl); 5157 rest_of_decl_compilation (decl, NULL, toplevel, 0); 5158 if (! toplevel) 5159 expand_decl (decl); 5160 } 5161 } 5162 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0; 5163 5164 /* Finish debugging output for this type. */ 5165 rest_of_type_compilation (t, toplevel); 5166 5167 return t; 5168} 5169 5170/* Lay out the type T, and its element type, and so on. */ 5171 5172static void 5173layout_array_type (tree t) 5174{ 5175 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 5176 layout_array_type (TREE_TYPE (t)); 5177 layout_type (t); 5178} 5179 5180/* Begin compiling the definition of an enumeration type. 5181 NAME is its name (or null if anonymous). 5182 Returns the type object, as yet incomplete. 5183 Also records info about it so that build_enumerator 5184 may be used to declare the individual values as they are read. */ 5185 5186tree 5187start_enum (tree name) 5188{ 5189 tree enumtype = 0; 5190 5191 /* If this is the real definition for a previous forward reference, 5192 fill in the contents in the same object that used to be the 5193 forward reference. */ 5194 5195 if (name != 0) 5196 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1); 5197 5198 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE) 5199 { 5200 enumtype = make_node (ENUMERAL_TYPE); 5201 pushtag (name, enumtype); 5202 } 5203 5204 C_TYPE_BEING_DEFINED (enumtype) = 1; 5205 5206 if (TYPE_VALUES (enumtype) != 0) 5207 { 5208 /* This enum is a named one that has been declared already. */ 5209 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name)); 5210 5211 /* Completely replace its old definition. 5212 The old enumerators remain defined, however. */ 5213 TYPE_VALUES (enumtype) = 0; 5214 } 5215 5216 enum_next_value = integer_zero_node; 5217 enum_overflow = 0; 5218 5219 if (flag_short_enums) 5220 TYPE_PACKED (enumtype) = 1; 5221 5222 return enumtype; 5223} 5224 5225/* After processing and defining all the values of an enumeration type, 5226 install their decls in the enumeration type and finish it off. 5227 ENUMTYPE is the type object, VALUES a list of decl-value pairs, 5228 and ATTRIBUTES are the specified attributes. 5229 Returns ENUMTYPE. */ 5230 5231tree 5232finish_enum (tree enumtype, tree values, tree attributes) 5233{ 5234 tree pair, tem; 5235 tree minnode = 0, maxnode = 0, enum_value_type; 5236 int precision, unsign; 5237 int toplevel = (global_scope == current_scope); 5238 5239 if (in_parm_level_p ()) 5240 warning ("enum defined inside parms"); 5241 5242 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 5243 5244 /* Calculate the maximum value of any enumerator in this type. */ 5245 5246 if (values == error_mark_node) 5247 minnode = maxnode = integer_zero_node; 5248 else 5249 { 5250 minnode = maxnode = TREE_VALUE (values); 5251 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair)) 5252 { 5253 tree value = TREE_VALUE (pair); 5254 if (tree_int_cst_lt (maxnode, value)) 5255 maxnode = value; 5256 if (tree_int_cst_lt (value, minnode)) 5257 minnode = value; 5258 } 5259 } 5260 5261 /* Construct the final type of this enumeration. It is the same 5262 as one of the integral types - the narrowest one that fits, except 5263 that normally we only go as narrow as int - and signed iff any of 5264 the values are negative. */ 5265 unsign = (tree_int_cst_sgn (minnode) >= 0); 5266 precision = MAX (min_precision (minnode, unsign), 5267 min_precision (maxnode, unsign)); 5268 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node)) 5269 { 5270 tree narrowest = c_common_type_for_size (precision, unsign); 5271 if (narrowest == 0) 5272 { 5273 warning ("enumeration values exceed range of largest integer"); 5274 narrowest = long_long_integer_type_node; 5275 } 5276 5277 precision = TYPE_PRECISION (narrowest); 5278 } 5279 else 5280 precision = TYPE_PRECISION (integer_type_node); 5281 5282 if (precision == TYPE_PRECISION (integer_type_node)) 5283 enum_value_type = c_common_type_for_size (precision, 0); 5284 else 5285 enum_value_type = enumtype; 5286 5287 TYPE_MIN_VALUE (enumtype) = minnode; 5288 TYPE_MAX_VALUE (enumtype) = maxnode; 5289 TREE_UNSIGNED (enumtype) = unsign; 5290 TYPE_SIZE (enumtype) = 0; 5291 5292 /* If the precision of the type was specific with an attribute and it 5293 was too small, give an error. Otherwise, use it. */ 5294 if (TYPE_PRECISION (enumtype)) 5295 { 5296 if (precision > TYPE_PRECISION (enumtype)) 5297 error ("specified mode too small for enumeral values"); 5298 } 5299 else 5300 TYPE_PRECISION (enumtype) = precision; 5301 5302 layout_type (enumtype); 5303 5304 if (values != error_mark_node) 5305 { 5306 /* Change the type of the enumerators to be the enum type. We 5307 need to do this irrespective of the size of the enum, for 5308 proper type checking. Replace the DECL_INITIALs of the 5309 enumerators, and the value slots of the list, with copies 5310 that have the enum type; they cannot be modified in place 5311 because they may be shared (e.g. integer_zero_node) Finally, 5312 change the purpose slots to point to the names of the decls. */ 5313 for (pair = values; pair; pair = TREE_CHAIN (pair)) 5314 { 5315 tree enu = TREE_PURPOSE (pair); 5316 5317 TREE_TYPE (enu) = enumtype; 5318 5319 /* The ISO C Standard mandates enumerators to have type int, 5320 even though the underlying type of an enum type is 5321 unspecified. Here we convert any enumerators that fit in 5322 an int to type int, to avoid promotions to unsigned types 5323 when comparing integers with enumerators that fit in the 5324 int range. When -pedantic is given, build_enumerator() 5325 would have already taken care of those that don't fit. */ 5326 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type)) 5327 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu)); 5328 else 5329 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu)); 5330 5331 TREE_PURPOSE (pair) = DECL_NAME (enu); 5332 TREE_VALUE (pair) = DECL_INITIAL (enu); 5333 } 5334 5335 TYPE_VALUES (enumtype) = values; 5336 } 5337 5338 /* Fix up all variant types of this enum type. */ 5339 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem)) 5340 { 5341 if (tem == enumtype) 5342 continue; 5343 TYPE_VALUES (tem) = TYPE_VALUES (enumtype); 5344 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype); 5345 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype); 5346 TYPE_SIZE (tem) = TYPE_SIZE (enumtype); 5347 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype); 5348 TYPE_MODE (tem) = TYPE_MODE (enumtype); 5349 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype); 5350 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype); 5351 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype); 5352 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype); 5353 } 5354 5355 /* Finish debugging output for this type. */ 5356 rest_of_type_compilation (enumtype, toplevel); 5357 5358 return enumtype; 5359} 5360 5361/* Build and install a CONST_DECL for one value of the 5362 current enumeration type (one that was begun with start_enum). 5363 Return a tree-list containing the CONST_DECL and its value. 5364 Assignment of sequential values by default is handled here. */ 5365 5366tree 5367build_enumerator (tree name, tree value) 5368{ 5369 tree decl, type; 5370 5371 /* Validate and default VALUE. */ 5372 5373 /* Remove no-op casts from the value. */ 5374 if (value) 5375 STRIP_TYPE_NOPS (value); 5376 5377 if (value != 0) 5378 { 5379 if (TREE_CODE (value) == INTEGER_CST) 5380 { 5381 value = default_conversion (value); 5382 constant_expression_warning (value); 5383 } 5384 else 5385 { 5386 error ("enumerator value for `%s' not integer constant", 5387 IDENTIFIER_POINTER (name)); 5388 value = 0; 5389 } 5390 } 5391 5392 /* Default based on previous value. */ 5393 /* It should no longer be possible to have NON_LVALUE_EXPR 5394 in the default. */ 5395 if (value == 0) 5396 { 5397 value = enum_next_value; 5398 if (enum_overflow) 5399 error ("overflow in enumeration values"); 5400 } 5401 5402 if (pedantic && ! int_fits_type_p (value, integer_type_node)) 5403 { 5404 pedwarn ("ISO C restricts enumerator values to range of `int'"); 5405 value = convert (integer_type_node, value); 5406 } 5407 5408 /* Set basis for default for next value. */ 5409 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0); 5410 enum_overflow = tree_int_cst_lt (enum_next_value, value); 5411 5412 /* Now create a declaration for the enum value name. */ 5413 5414 type = TREE_TYPE (value); 5415 type = c_common_type_for_size (MAX (TYPE_PRECISION (type), 5416 TYPE_PRECISION (integer_type_node)), 5417 (TYPE_PRECISION (type) 5418 >= TYPE_PRECISION (integer_type_node) 5419 && TREE_UNSIGNED (type))); 5420 5421 decl = build_decl (CONST_DECL, name, type); 5422 DECL_INITIAL (decl) = convert (type, value); 5423 pushdecl (decl); 5424 5425 return tree_cons (decl, value, NULL_TREE); 5426} 5427 5428 5429/* Create the FUNCTION_DECL for a function definition. 5430 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of 5431 the declaration; they describe the function's name and the type it returns, 5432 but twisted together in a fashion that parallels the syntax of C. 5433 5434 This function creates a binding context for the function body 5435 as well as setting up the FUNCTION_DECL in current_function_decl. 5436 5437 Returns 1 on success. If the DECLARATOR is not suitable for a function 5438 (it defines a datum instead), we return 0, which tells 5439 yyparse to report a parse error. */ 5440 5441int 5442start_function (tree declspecs, tree declarator, tree attributes) 5443{ 5444 tree decl1, old_decl; 5445 tree restype; 5446 int old_immediate_size_expand = immediate_size_expand; 5447 5448 current_function_returns_value = 0; /* Assume, until we see it does. */ 5449 current_function_returns_null = 0; 5450 current_function_returns_abnormally = 0; 5451 warn_about_return_type = 0; 5452 current_extern_inline = 0; 5453 c_in_iteration_stmt = 0; 5454 c_in_case_stmt = 0; 5455 5456 /* Don't expand any sizes in the return type of the function. */ 5457 immediate_size_expand = 0; 5458 5459 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL); 5460 5461 /* If the declarator is not suitable for a function definition, 5462 cause a syntax error. */ 5463 if (decl1 == 0) 5464 { 5465 immediate_size_expand = old_immediate_size_expand; 5466 return 0; 5467 } 5468 5469 decl_attributes (&decl1, attributes, 0); 5470 5471 if (DECL_DECLARED_INLINE_P (decl1) 5472 && DECL_UNINLINABLE (decl1) 5473 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1))) 5474 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1); 5475 5476 announce_function (decl1); 5477 5478 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1)))) 5479 { 5480 error ("return type is an incomplete type"); 5481 /* Make it return void instead. */ 5482 TREE_TYPE (decl1) 5483 = build_function_type (void_type_node, 5484 TYPE_ARG_TYPES (TREE_TYPE (decl1))); 5485 } 5486 5487 if (warn_about_return_type) 5488 pedwarn_c99 ("return type defaults to `int'"); 5489 5490 /* Save the parm names or decls from this function's declarator 5491 where store_parm_decls will find them. */ 5492 current_function_parms = last_function_parms; 5493 current_function_parm_tags = last_function_parm_tags; 5494 current_function_parm_others = last_function_parm_others; 5495 5496 /* Make the init_value nonzero so pushdecl knows this is not tentative. 5497 error_mark_node is replaced below (in poplevel) with the BLOCK. */ 5498 DECL_INITIAL (decl1) = error_mark_node; 5499 5500 /* If this definition isn't a prototype and we had a prototype declaration 5501 before, copy the arg type info from that prototype. 5502 But not if what we had before was a builtin function. */ 5503 old_decl = lookup_name_current_level (DECL_NAME (decl1)); 5504 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE 5505 && !DECL_BUILT_IN (old_decl) 5506 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1))) 5507 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl)))) 5508 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0) 5509 { 5510 TREE_TYPE (decl1) = TREE_TYPE (old_decl); 5511 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl); 5512 } 5513 5514 /* Optionally warn of old-fashioned def with no previous prototype. */ 5515 if (warn_strict_prototypes 5516 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0 5517 && C_DECL_ISNT_PROTOTYPE (old_decl)) 5518 warning ("function declaration isn't a prototype"); 5519 /* Optionally warn of any global def with no previous prototype. */ 5520 else if (warn_missing_prototypes 5521 && TREE_PUBLIC (decl1) 5522 && ! MAIN_NAME_P (DECL_NAME (decl1)) 5523 && C_DECL_ISNT_PROTOTYPE (old_decl)) 5524 warning ("%Jno previous prototype for '%D'", decl1, decl1); 5525 /* Optionally warn of any def with no previous prototype 5526 if the function has already been used. */ 5527 else if (warn_missing_prototypes 5528 && old_decl != 0 && TREE_USED (old_decl) 5529 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0) 5530 warning ("%J'%D' was used with no prototype before its definition", 5531 decl1, decl1); 5532 /* Optionally warn of any global def with no previous declaration. */ 5533 else if (warn_missing_declarations 5534 && TREE_PUBLIC (decl1) 5535 && old_decl == 0 5536 && ! MAIN_NAME_P (DECL_NAME (decl1))) 5537 warning ("%Jno previous declaration for '%D'", decl1, decl1); 5538 /* Optionally warn of any def with no previous declaration 5539 if the function has already been used. */ 5540 else if (warn_missing_declarations 5541 && old_decl != 0 && TREE_USED (old_decl) 5542 && C_DECL_IMPLICIT (old_decl)) 5543 warning ("%J`%D' was used with no declaration before its definition", 5544 decl1, decl1); 5545 5546 /* This is a definition, not a reference. 5547 So normally clear DECL_EXTERNAL. 5548 However, `extern inline' acts like a declaration 5549 except for defining how to inline. So set DECL_EXTERNAL in that case. */ 5550 DECL_EXTERNAL (decl1) = current_extern_inline; 5551 5552 /* This function exists in static storage. 5553 (This does not mean `static' in the C sense!) */ 5554 TREE_STATIC (decl1) = 1; 5555 5556 /* A nested function is not global. */ 5557 if (current_function_decl != 0) 5558 TREE_PUBLIC (decl1) = 0; 5559 5560#ifdef ENABLE_CHECKING 5561 /* This is the earliest point at which we might know the assembler 5562 name of the function. Thus, if it's set before this, die horribly. */ 5563 if (DECL_ASSEMBLER_NAME_SET_P (decl1)) 5564 abort (); 5565#endif 5566 5567 /* If #pragma weak was used, mark the decl weak now. */ 5568 if (current_scope == global_scope) 5569 maybe_apply_pragma_weak (decl1); 5570 5571 /* Warn for unlikely, improbable, or stupid declarations of `main'. */ 5572 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1))) 5573 { 5574 tree args; 5575 int argct = 0; 5576 5577 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1))) 5578 != integer_type_node) 5579 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1); 5580 5581 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args; 5582 args = TREE_CHAIN (args)) 5583 { 5584 tree type = args ? TREE_VALUE (args) : 0; 5585 5586 if (type == void_type_node) 5587 break; 5588 5589 ++argct; 5590 switch (argct) 5591 { 5592 case 1: 5593 if (TYPE_MAIN_VARIANT (type) != integer_type_node) 5594 pedwarn ("%Jfirst argument of '%D' should be `int'", 5595 decl1, decl1); 5596 break; 5597 5598 case 2: 5599 if (TREE_CODE (type) != POINTER_TYPE 5600 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 5601 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 5602 != char_type_node)) 5603 pedwarn ("%Jsecond argument of '%D' should be 'char **'", 5604 decl1, decl1); 5605 break; 5606 5607 case 3: 5608 if (TREE_CODE (type) != POINTER_TYPE 5609 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 5610 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 5611 != char_type_node)) 5612 pedwarn ("%Jthird argument of '%D' should probably be " 5613 "'char **'", decl1, decl1); 5614 break; 5615 } 5616 } 5617 5618 /* It is intentional that this message does not mention the third 5619 argument because it's only mentioned in an appendix of the 5620 standard. */ 5621 if (argct > 0 && (argct < 2 || argct > 3)) 5622 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1); 5623 5624 if (! TREE_PUBLIC (decl1)) 5625 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1); 5626 } 5627 5628 /* Record the decl so that the function name is defined. 5629 If we already have a decl for this name, and it is a FUNCTION_DECL, 5630 use the old decl. */ 5631 5632 current_function_decl = pushdecl (decl1); 5633 5634 pushlevel (0); 5635 declare_parm_level (); 5636 5637 make_decl_rtl (current_function_decl, NULL); 5638 5639 restype = TREE_TYPE (TREE_TYPE (current_function_decl)); 5640 /* Promote the value to int before returning it. */ 5641 if (c_promoting_integer_type_p (restype)) 5642 { 5643 /* It retains unsignedness if not really getting wider. */ 5644 if (TREE_UNSIGNED (restype) 5645 && (TYPE_PRECISION (restype) 5646 == TYPE_PRECISION (integer_type_node))) 5647 restype = unsigned_type_node; 5648 else 5649 restype = integer_type_node; 5650 } 5651 DECL_RESULT (current_function_decl) 5652 = build_decl (RESULT_DECL, NULL_TREE, restype); 5653 5654 /* If this fcn was already referenced via a block-scope `extern' decl 5655 (or an implicit decl), propagate certain information about the usage. */ 5656 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl))) 5657 TREE_ADDRESSABLE (current_function_decl) = 1; 5658 5659 immediate_size_expand = old_immediate_size_expand; 5660 5661 start_fname_decls (); 5662 5663 return 1; 5664} 5665 5666/* Subroutine of store_parm_decls which handles new-style function 5667 definitions (prototype format). The parms already have decls, so we 5668 need only record them as in effect and complain if any redundant 5669 old-style parm decls were written. */ 5670static void 5671store_parm_decls_newstyle (void) 5672{ 5673 tree decl, last; 5674 tree fndecl = current_function_decl; 5675 tree parms = current_function_parms; 5676 tree tags = current_function_parm_tags; 5677 tree others = current_function_parm_others; 5678 5679 if (current_scope->parms || current_scope->names || current_scope->tags) 5680 { 5681 error ("%Jold-style parameter declarations in prototyped " 5682 "function definition", fndecl); 5683 5684 /* Get rid of the old-style declarations. */ 5685 poplevel (0, 0, 0); 5686 pushlevel (0); 5687 } 5688 5689 /* Now make all the parameter declarations visible in the function body. 5690 We can bypass most of the grunt work of pushdecl. */ 5691 for (last = 0, decl = parms; decl; last = decl, decl = TREE_CHAIN (decl)) 5692 { 5693 DECL_CONTEXT (decl) = current_function_decl; 5694 if (DECL_NAME (decl) == 0) 5695 error ("%Jparameter name omitted", decl); 5696 else 5697 { 5698 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl))) 5699 current_scope->shadowed 5700 = tree_cons (DECL_NAME (decl), 5701 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)), 5702 current_scope->shadowed); 5703 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl; 5704 } 5705 } 5706 current_scope->parms = parms; 5707 current_scope->parms_last = last; 5708 5709 /* Record the parameter list in the function declaration. */ 5710 DECL_ARGUMENTS (fndecl) = parms; 5711 5712 /* Now make all the ancillary declarations visible, likewise. */ 5713 for (last = 0, decl = others; decl; last = decl, decl = TREE_CHAIN (decl)) 5714 { 5715 DECL_CONTEXT (decl) = current_function_decl; 5716 if (DECL_NAME (decl) 5717 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node) 5718 { 5719 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl))) 5720 current_scope->shadowed 5721 = tree_cons (DECL_NAME (decl), 5722 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)), 5723 current_scope->shadowed); 5724 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl; 5725 } 5726 } 5727 current_scope->names = others; 5728 current_scope->names_last = last; 5729 5730 /* And all the tag declarations. */ 5731 for (decl = tags; decl; decl = TREE_CHAIN (decl)) 5732 if (TREE_PURPOSE (decl)) 5733 { 5734 if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl))) 5735 current_scope->shadowed_tags 5736 = tree_cons (TREE_PURPOSE (decl), 5737 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)), 5738 current_scope->shadowed_tags); 5739 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl); 5740 } 5741 current_scope->tags = tags; 5742} 5743 5744/* Subroutine of store_parm_decls which handles old-style function 5745 definitions (separate parameter list and declarations). */ 5746 5747static void 5748store_parm_decls_oldstyle (void) 5749{ 5750 tree parm, decl, last; 5751 tree fndecl = current_function_decl; 5752 5753 /* This is the identifier list from the function declarator. */ 5754 tree parmids = current_function_parms; 5755 5756 /* We use DECL_WEAK as a flag to show which parameters have been 5757 seen already, since it is not used on PARM_DECL. */ 5758#ifdef ENABLE_CHECKING 5759 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm)) 5760 if (DECL_WEAK (parm)) 5761 abort (); 5762#endif 5763 5764 /* Match each formal parameter name with its declaration. Save each 5765 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */ 5766 for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 5767 { 5768 if (TREE_VALUE (parm) == 0) 5769 { 5770 error ("%Jparameter name missing from parameter list", fndecl); 5771 TREE_PURPOSE (parm) = 0; 5772 continue; 5773 } 5774 5775 decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm)); 5776 if (decl && DECL_CONTEXT (decl) == fndecl) 5777 { 5778 /* If we got something other than a PARM_DECL it is an error. */ 5779 if (TREE_CODE (decl) != PARM_DECL) 5780 error ("%J\"%D\" declared as a non-parameter", decl, decl); 5781 /* If the declaration is already marked, we have a duplicate 5782 name. Complain and ignore the duplicate. */ 5783 else if (DECL_WEAK (decl)) 5784 { 5785 error ("%Jmultiple parameters named \"%D\"", decl, decl); 5786 TREE_PURPOSE (parm) = 0; 5787 continue; 5788 } 5789 /* If the declaration says "void", complain and turn it into 5790 an int. */ 5791 else if (VOID_TYPE_P (TREE_TYPE (decl))) 5792 { 5793 error ("%Jparameter \"%D\" declared void", decl, decl); 5794 TREE_TYPE (decl) = integer_type_node; 5795 DECL_ARG_TYPE (decl) = integer_type_node; 5796 layout_decl (decl, 0); 5797 } 5798 } 5799 /* If no declaration found, default to int. */ 5800 else 5801 { 5802 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node); 5803 DECL_ARG_TYPE (decl) = TREE_TYPE (decl); 5804 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl); 5805 pushdecl (decl); 5806 5807 if (flag_isoc99) 5808 pedwarn ("%Jtype of \"%D\" defaults to \"int\"", decl, decl); 5809 else if (extra_warnings) 5810 warning ("%Jtype of \"%D\" defaults to \"int\"", decl, decl); 5811 } 5812 5813 TREE_PURPOSE (parm) = decl; 5814 DECL_WEAK (decl) = 1; 5815 } 5816 5817 /* Now examine the parms chain for incomplete declarations 5818 and declarations with no corresponding names. */ 5819 5820 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm)) 5821 { 5822 if (!COMPLETE_TYPE_P (TREE_TYPE (parm))) 5823 { 5824 error ("%Jparameter \"%D\" has incomplete type", parm, parm); 5825 TREE_TYPE (parm) = error_mark_node; 5826 } 5827 5828 if (! DECL_WEAK (parm)) 5829 { 5830 error ("%Jdeclaration for parameter \"%D\" but no such parameter", 5831 parm, parm); 5832 5833 /* Pretend the parameter was not missing. 5834 This gets us to a standard state and minimizes 5835 further error messages. */ 5836 parmids = chainon (parmids, tree_cons (parm, 0, 0)); 5837 } 5838 } 5839 5840 /* Chain the declarations together in the order of the list of 5841 names. Store that chain in the function decl, replacing the 5842 list of names. Update the current scope to match. */ 5843 DECL_ARGUMENTS (fndecl) = 0; 5844 5845 for (parm = parmids; parm; parm = TREE_CHAIN (parm)) 5846 if (TREE_PURPOSE (parm)) 5847 break; 5848 if (parm && TREE_PURPOSE (parm)) 5849 { 5850 last = TREE_PURPOSE (parm); 5851 DECL_ARGUMENTS (fndecl) = last; 5852 current_scope->parms = last; 5853 DECL_WEAK (last) = 0; 5854 5855 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm)) 5856 if (TREE_PURPOSE (parm)) 5857 { 5858 TREE_CHAIN (last) = TREE_PURPOSE (parm); 5859 last = TREE_PURPOSE (parm); 5860 DECL_WEAK (last) = 0; 5861 } 5862 current_scope->parms_last = last; 5863 TREE_CHAIN (last) = 0; 5864 } 5865 5866 /* If there was a previous prototype, 5867 set the DECL_ARG_TYPE of each argument according to 5868 the type previously specified, and report any mismatches. */ 5869 5870 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl))) 5871 { 5872 tree type; 5873 for (parm = DECL_ARGUMENTS (fndecl), 5874 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); 5875 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) 5876 != void_type_node)); 5877 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type)) 5878 { 5879 if (parm == 0 || type == 0 5880 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node) 5881 { 5882 error ("number of arguments doesn't match prototype"); 5883 error ("%Hprototype declaration", 5884 ¤t_function_prototype_locus); 5885 break; 5886 } 5887 /* Type for passing arg must be consistent with that 5888 declared for the arg. ISO C says we take the unqualified 5889 type for parameters declared with qualified type. */ 5890 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)), 5891 TYPE_MAIN_VARIANT (TREE_VALUE (type)), 5892 COMPARE_STRICT)) 5893 { 5894 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) 5895 == TYPE_MAIN_VARIANT (TREE_VALUE (type))) 5896 { 5897 /* Adjust argument to match prototype. E.g. a previous 5898 `int foo(float);' prototype causes 5899 `int foo(x) float x; {...}' to be treated like 5900 `int foo(float x) {...}'. This is particularly 5901 useful for argument types like uid_t. */ 5902 DECL_ARG_TYPE (parm) = TREE_TYPE (parm); 5903 5904 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl)) 5905 && INTEGRAL_TYPE_P (TREE_TYPE (parm)) 5906 && TYPE_PRECISION (TREE_TYPE (parm)) 5907 < TYPE_PRECISION (integer_type_node)) 5908 DECL_ARG_TYPE (parm) = integer_type_node; 5909 5910 if (pedantic) 5911 { 5912 pedwarn ("promoted argument \"%D\" " 5913 "doesn't match prototype", parm); 5914 pedwarn ("%Hprototype declaration", 5915 ¤t_function_prototype_locus); 5916 } 5917 } 5918 else 5919 { 5920 error ("argument \"%D\" doesn't match prototype", parm); 5921 error ("%Hprototype declaration", 5922 ¤t_function_prototype_locus); 5923 } 5924 } 5925 } 5926 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0; 5927 } 5928 5929 /* Otherwise, create a prototype that would match. */ 5930 5931 else 5932 { 5933 tree actual = 0, last = 0, type; 5934 5935 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm)) 5936 { 5937 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE); 5938 if (last) 5939 TREE_CHAIN (last) = type; 5940 else 5941 actual = type; 5942 last = type; 5943 } 5944 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE); 5945 if (last) 5946 TREE_CHAIN (last) = type; 5947 else 5948 actual = type; 5949 5950 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES 5951 of the type of this function, but we need to avoid having this 5952 affect the types of other similarly-typed functions, so we must 5953 first force the generation of an identical (but separate) type 5954 node for the relevant function type. The new node we create 5955 will be a variant of the main variant of the original function 5956 type. */ 5957 5958 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl)); 5959 5960 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual; 5961 } 5962} 5963 5964/* Store the parameter declarations into the current function declaration. 5965 This is called after parsing the parameter declarations, before 5966 digesting the body of the function. 5967 5968 For an old-style definition, construct a prototype out of the old-style 5969 parameter declarations and inject it into the function's type. */ 5970 5971void 5972store_parm_decls (void) 5973{ 5974 tree fndecl = current_function_decl; 5975 5976 /* The function containing FNDECL, if any. */ 5977 tree context = decl_function_context (fndecl); 5978 5979 /* True if this definition is written with a prototype. */ 5980 bool prototype = (current_function_parms 5981 && TREE_CODE (current_function_parms) != TREE_LIST); 5982 5983 if (prototype) 5984 store_parm_decls_newstyle (); 5985 else 5986 store_parm_decls_oldstyle (); 5987 5988 /* The next call to pushlevel will be a function body. */ 5989 5990 next_is_function_body = true; 5991 5992 /* Write a record describing this function definition to the prototypes 5993 file (if requested). */ 5994 5995 gen_aux_info_record (fndecl, 1, 0, prototype); 5996 5997 /* Initialize the RTL code for the function. */ 5998 allocate_struct_function (fndecl); 5999 6000 /* Begin the statement tree for this function. */ 6001 begin_stmt_tree (&DECL_SAVED_TREE (fndecl)); 6002 6003 /* If this is a nested function, save away the sizes of any 6004 variable-size types so that we can expand them when generating 6005 RTL. */ 6006 if (context) 6007 { 6008 tree t; 6009 6010 DECL_LANG_SPECIFIC (fndecl)->pending_sizes 6011 = nreverse (get_pending_sizes ()); 6012 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes; 6013 t; 6014 t = TREE_CHAIN (t)) 6015 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context; 6016 } 6017 6018 /* This function is being processed in whole-function mode. */ 6019 cfun->x_whole_function_mode_p = 1; 6020 6021 /* Even though we're inside a function body, we still don't want to 6022 call expand_expr to calculate the size of a variable-sized array. 6023 We haven't necessarily assigned RTL to all variables yet, so it's 6024 not safe to try to expand expressions involving them. */ 6025 immediate_size_expand = 0; 6026 cfun->x_dont_save_pending_sizes_p = 1; 6027} 6028 6029/* Finish up a function declaration and compile that function 6030 all the way to assembler language output. The free the storage 6031 for the function definition. 6032 6033 This is called after parsing the body of the function definition. */ 6034 6035void 6036finish_function (void) 6037{ 6038 tree fndecl = current_function_decl; 6039 6040 /* When a function declaration is totally empty, e.g. 6041 void foo(void) { } 6042 (the argument list is irrelevant) the compstmt rule will not 6043 bother calling pushlevel/poplevel, which means we get here with 6044 the scope stack out of sync. Detect this situation by noticing 6045 that current_scope is still as store_parm_decls left it, and do 6046 a dummy push/pop to get back to consistency. 6047 Note that the call to pushlevel does not actually push another 6048 scope - see there for details. */ 6049 6050 if (current_scope->parm_flag && next_is_function_body) 6051 { 6052 pushlevel (0); 6053 poplevel (0, 0, 0); 6054 } 6055 6056 if (TREE_CODE (fndecl) == FUNCTION_DECL 6057 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))) 6058 { 6059 tree args = DECL_ARGUMENTS (fndecl); 6060 for (; args; args = TREE_CHAIN (args)) 6061 { 6062 tree type = TREE_TYPE (args); 6063 if (INTEGRAL_TYPE_P (type) 6064 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 6065 DECL_ARG_TYPE (args) = integer_type_node; 6066 } 6067 } 6068 6069 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) 6070 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; 6071 6072 /* Must mark the RESULT_DECL as being in this function. */ 6073 6074 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node) 6075 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; 6076 6077 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted) 6078 { 6079 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))) 6080 != integer_type_node) 6081 { 6082 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned. 6083 If warn_main is -1 (-Wno-main) we don't want to be warned. */ 6084 if (!warn_main) 6085 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl); 6086 } 6087 else 6088 { 6089#ifdef DEFAULT_MAIN_RETURN 6090 /* Make it so that `main' always returns success by default. */ 6091 DEFAULT_MAIN_RETURN; 6092#else 6093 if (flag_isoc99) 6094 c_expand_return (integer_zero_node); 6095#endif 6096 } 6097 } 6098 6099 finish_fname_decls (); 6100 6101 /* Tie off the statement tree for this function. */ 6102 finish_stmt_tree (&DECL_SAVED_TREE (fndecl)); 6103 6104 /* Complain if there's just no return statement. */ 6105 if (warn_return_type 6106 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE 6107 && !current_function_returns_value && !current_function_returns_null 6108 /* Don't complain if we abort. */ 6109 && !current_function_returns_abnormally 6110 /* Don't warn for main(). */ 6111 && !MAIN_NAME_P (DECL_NAME (fndecl)) 6112 /* Or if they didn't actually specify a return type. */ 6113 && !C_FUNCTION_IMPLICIT_INT (fndecl) 6114 /* Normally, with -Wreturn-type, flow will complain. Unless we're an 6115 inline function, as we might never be compiled separately. */ 6116 && DECL_INLINE (fndecl)) 6117 warning ("no return statement in function returning non-void"); 6118 6119 /* With just -Wextra, complain only if function returns both with 6120 and without a value. */ 6121 if (extra_warnings 6122 && current_function_returns_value 6123 && current_function_returns_null) 6124 warning ("this function may return with or without a value"); 6125 6126 /* We're leaving the context of this function, so zap cfun. It's still in 6127 DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */ 6128 cfun = NULL; 6129 6130 /* ??? Objc emits functions after finalizing the compilation unit. 6131 This should be cleaned up later and this conditional removed. */ 6132 if (!cgraph_global_info_ready) 6133 cgraph_finalize_function (fndecl, false); 6134 else 6135 c_expand_body (fndecl); 6136 current_function_decl = NULL; 6137} 6138 6139/* Generate the RTL for a deferred function FNDECL. */ 6140 6141void 6142c_expand_deferred_function (tree fndecl) 6143{ 6144 /* DECL_INLINE or DECL_RESULT might got cleared after the inline 6145 function was deferred, e.g. in duplicate_decls. */ 6146 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl)) 6147 { 6148 if (flag_inline_trees) 6149 { 6150 timevar_push (TV_INTEGRATION); 6151 optimize_inline_calls (fndecl); 6152 timevar_pop (TV_INTEGRATION); 6153 } 6154 c_expand_body (fndecl); 6155 current_function_decl = NULL; 6156 } 6157} 6158 6159/* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero, 6160 then we are already in the process of generating RTL for another 6161 function. */ 6162 6163static void 6164c_expand_body_1 (tree fndecl, int nested_p) 6165{ 6166 if (nested_p) 6167 { 6168 /* Make sure that we will evaluate variable-sized types involved 6169 in our function's type. */ 6170 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes); 6171 6172 /* Squirrel away our current state. */ 6173 push_function_context (); 6174 } 6175 6176 tree_rest_of_compilation (fndecl, nested_p); 6177 6178 if (nested_p) 6179 /* Return to the enclosing function. */ 6180 pop_function_context (); 6181 6182 if (DECL_STATIC_CONSTRUCTOR (fndecl)) 6183 { 6184 if (targetm.have_ctors_dtors) 6185 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0), 6186 DEFAULT_INIT_PRIORITY); 6187 else 6188 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors); 6189 } 6190 6191 if (DECL_STATIC_DESTRUCTOR (fndecl)) 6192 { 6193 if (targetm.have_ctors_dtors) 6194 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0), 6195 DEFAULT_INIT_PRIORITY); 6196 else 6197 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors); 6198 } 6199} 6200 6201/* Like c_expand_body_1 but only for unnested functions. */ 6202 6203void 6204c_expand_body (tree fndecl) 6205{ 6206 6207 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) 6208 c_expand_body_1 (fndecl, 0); 6209} 6210 6211/* Check the declarations given in a for-loop for satisfying the C99 6212 constraints. */ 6213void 6214check_for_loop_decls (void) 6215{ 6216 tree t; 6217 6218 if (!flag_isoc99) 6219 { 6220 /* If we get here, declarations have been used in a for loop without 6221 the C99 for loop scope. This doesn't make much sense, so don't 6222 allow it. */ 6223 error ("'for' loop initial declaration used outside C99 mode"); 6224 return; 6225 } 6226 /* C99 subclause 6.8.5 paragraph 3: 6227 6228 [#3] The declaration part of a for statement shall only 6229 declare identifiers for objects having storage class auto or 6230 register. 6231 6232 It isn't clear whether, in this sentence, "identifiers" binds to 6233 "shall only declare" or to "objects" - that is, whether all identifiers 6234 declared must be identifiers for objects, or whether the restriction 6235 only applies to those that are. (A question on this in comp.std.c 6236 in November 2000 received no answer.) We implement the strictest 6237 interpretation, to avoid creating an extension which later causes 6238 problems. */ 6239 6240 for (t = current_scope->tags; t; t = TREE_CHAIN (t)) 6241 { 6242 if (TREE_PURPOSE (t) != 0) 6243 { 6244 enum tree_code code = TREE_CODE (TREE_VALUE (t)); 6245 6246 if (code == RECORD_TYPE) 6247 error ("'struct %s' declared in 'for' loop initial declaration", 6248 IDENTIFIER_POINTER (TREE_PURPOSE (t))); 6249 else if (code == UNION_TYPE) 6250 error ("'union %s' declared in 'for' loop initial declaration", 6251 IDENTIFIER_POINTER (TREE_PURPOSE (t))); 6252 else 6253 error ("'enum %s' declared in 'for' loop initial declaration", 6254 IDENTIFIER_POINTER (TREE_PURPOSE (t))); 6255 } 6256 } 6257 6258 for (t = getdecls (); t; t = TREE_CHAIN (t)) 6259 { 6260 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t)) 6261 error ("%Jdeclaration of non-variable '%D' in 'for' loop " 6262 "initial declaration", t, t); 6263 else if (TREE_STATIC (t)) 6264 error ("%Jdeclaration of static variable '%D' in 'for' loop " 6265 "initial declaration", t, t); 6266 else if (DECL_EXTERNAL (t)) 6267 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop " 6268 "initial declaration", t, t); 6269 } 6270} 6271 6272/* Save and reinitialize the variables 6273 used during compilation of a C function. */ 6274 6275void 6276c_push_function_context (struct function *f) 6277{ 6278 struct language_function *p; 6279 p = ggc_alloc (sizeof (struct language_function)); 6280 f->language = p; 6281 6282 p->base.x_stmt_tree = c_stmt_tree; 6283 p->base.x_scope_stmt_stack = c_scope_stmt_stack; 6284 p->x_in_iteration_stmt = c_in_iteration_stmt; 6285 p->x_in_case_stmt = c_in_case_stmt; 6286 p->returns_value = current_function_returns_value; 6287 p->returns_null = current_function_returns_null; 6288 p->returns_abnormally = current_function_returns_abnormally; 6289 p->warn_about_return_type = warn_about_return_type; 6290 p->extern_inline = current_extern_inline; 6291} 6292 6293/* Restore the variables used during compilation of a C function. */ 6294 6295void 6296c_pop_function_context (struct function *f) 6297{ 6298 struct language_function *p = f->language; 6299 6300 if (DECL_SAVED_INSNS (current_function_decl) == 0 6301 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE) 6302 { 6303 /* Stop pointing to the local nodes about to be freed. */ 6304 /* But DECL_INITIAL must remain nonzero so we know this 6305 was an actual function definition. */ 6306 DECL_INITIAL (current_function_decl) = error_mark_node; 6307 DECL_ARGUMENTS (current_function_decl) = 0; 6308 } 6309 6310 c_stmt_tree = p->base.x_stmt_tree; 6311 c_scope_stmt_stack = p->base.x_scope_stmt_stack; 6312 c_in_iteration_stmt = p->x_in_iteration_stmt; 6313 c_in_case_stmt = p->x_in_case_stmt; 6314 current_function_returns_value = p->returns_value; 6315 current_function_returns_null = p->returns_null; 6316 current_function_returns_abnormally = p->returns_abnormally; 6317 warn_about_return_type = p->warn_about_return_type; 6318 current_extern_inline = p->extern_inline; 6319 6320 f->language = NULL; 6321} 6322 6323/* Copy the DECL_LANG_SPECIFIC data associated with DECL. */ 6324 6325void 6326c_dup_lang_specific_decl (tree decl) 6327{ 6328 struct lang_decl *ld; 6329 6330 if (!DECL_LANG_SPECIFIC (decl)) 6331 return; 6332 6333 ld = ggc_alloc (sizeof (struct lang_decl)); 6334 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl)); 6335 DECL_LANG_SPECIFIC (decl) = ld; 6336} 6337 6338/* The functions below are required for functionality of doing 6339 function at once processing in the C front end. Currently these 6340 functions are not called from anywhere in the C front end, but as 6341 these changes continue, that will change. */ 6342 6343/* Returns nonzero if the current statement is a full expression, 6344 i.e. temporaries created during that statement should be destroyed 6345 at the end of the statement. */ 6346 6347int 6348stmts_are_full_exprs_p (void) 6349{ 6350 return 0; 6351} 6352 6353/* Returns the stmt_tree (if any) to which statements are currently 6354 being added. If there is no active statement-tree, NULL is 6355 returned. */ 6356 6357stmt_tree 6358current_stmt_tree (void) 6359{ 6360 return &c_stmt_tree; 6361} 6362 6363/* Returns the stack of SCOPE_STMTs for the current function. */ 6364 6365tree * 6366current_scope_stmt_stack (void) 6367{ 6368 return &c_scope_stmt_stack; 6369} 6370 6371/* Nonzero if TYPE is an anonymous union or struct type. Always 0 in 6372 C. */ 6373 6374int 6375anon_aggr_type_p (tree node ATTRIBUTE_UNUSED) 6376{ 6377 return 0; 6378} 6379 6380/* Dummy function in place of callback used by C++. */ 6381 6382void 6383extract_interface_info (void) 6384{ 6385} 6386 6387/* Return a new COMPOUND_STMT, after adding it to the current 6388 statement tree. */ 6389 6390tree 6391c_begin_compound_stmt (void) 6392{ 6393 tree stmt; 6394 6395 /* Create the COMPOUND_STMT. */ 6396 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE)); 6397 6398 return stmt; 6399} 6400 6401/* Expand T (a DECL_STMT) if it declares an entity not handled by the 6402 common code. */ 6403 6404void 6405c_expand_decl_stmt (tree t) 6406{ 6407 tree decl = DECL_STMT_DECL (t); 6408 6409 /* Expand nested functions. */ 6410 if (TREE_CODE (decl) == FUNCTION_DECL 6411 && DECL_CONTEXT (decl) == current_function_decl 6412 && DECL_SAVED_TREE (decl)) 6413 c_expand_body_1 (decl, 1); 6414} 6415 6416/* Return the global value of T as a symbol. */ 6417 6418tree 6419identifier_global_value (tree t) 6420{ 6421 tree decl = IDENTIFIER_SYMBOL_VALUE (t); 6422 if (decl == 0 || DECL_FILE_SCOPE_P (decl)) 6423 return decl; 6424 6425 /* Shadowed by something else; find the true global value. */ 6426 for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl)) 6427 if (DECL_NAME (decl) == t) 6428 return decl; 6429 6430 /* Only local values for this decl. */ 6431 return 0; 6432} 6433 6434/* Record a builtin type for C. If NAME is non-NULL, it is the name used; 6435 otherwise the name is found in ridpointers from RID_INDEX. */ 6436 6437void 6438record_builtin_type (enum rid rid_index, const char *name, tree type) 6439{ 6440 tree id; 6441 if (name == 0) 6442 id = ridpointers[(int) rid_index]; 6443 else 6444 id = get_identifier (name); 6445 pushdecl (build_decl (TYPE_DECL, id, type)); 6446} 6447 6448/* Build the void_list_node (void_type_node having been created). */ 6449tree 6450build_void_list_node (void) 6451{ 6452 tree t = build_tree_list (NULL_TREE, void_type_node); 6453 return t; 6454} 6455 6456/* Return something to represent absolute declarators containing a *. 6457 TARGET is the absolute declarator that the * contains. 6458 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile 6459 to apply to the pointer type, represented as identifiers, possible mixed 6460 with attributes. 6461 6462 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST, 6463 if attributes are present) and whose type is the modifier list. */ 6464 6465tree 6466make_pointer_declarator (tree type_quals_attrs, tree target) 6467{ 6468 tree quals, attrs; 6469 tree itarget = target; 6470 split_specs_attrs (type_quals_attrs, &quals, &attrs); 6471 if (attrs != NULL_TREE) 6472 itarget = tree_cons (attrs, target, NULL_TREE); 6473 return build1 (INDIRECT_REF, quals, itarget); 6474} 6475 6476/* A wrapper around lhd_set_decl_assembler_name that gives static 6477 variables their C names if they are at file scope and only one 6478 translation unit is being compiled, for backwards compatibility 6479 with certain bizarre assembler hacks (like crtstuff.c). */ 6480 6481void 6482c_static_assembler_name (tree decl) 6483{ 6484 if (num_in_fnames == 1 6485 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl) 6486 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL) 6487 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl)); 6488 else 6489 lhd_set_decl_assembler_name (decl); 6490} 6491 6492/* Hash and equality functions for link_hash_table: key off 6493 DECL_ASSEMBLER_NAME. */ 6494 6495static hashval_t 6496link_hash_hash (const void *x_p) 6497{ 6498 tree x = (tree)x_p; 6499 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x); 6500} 6501 6502static int 6503link_hash_eq (const void *x1_p, const void *x2_p) 6504{ 6505 tree x1 = (tree)x1_p; 6506 tree x2 = (tree)x2_p; 6507 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2); 6508} 6509 6510/* Propagate information between definitions and uses between multiple 6511 translation units in TU_LIST based on linkage rules. */ 6512 6513void 6514merge_translation_unit_decls (void) 6515{ 6516 const tree tu_list = current_file_decl; 6517 tree tu; 6518 tree decl; 6519 htab_t link_hash_table; 6520 tree block; 6521 6522 /* Create the BLOCK that poplevel would have created, but don't 6523 actually call poplevel since that's expensive. */ 6524 block = make_node (BLOCK); 6525 BLOCK_VARS (block) = current_scope->names; 6526 TREE_USED (block) = 1; 6527 DECL_INITIAL (current_file_decl) = block; 6528 6529 /* If only one translation unit seen, no copying necessary. */ 6530 if (TREE_CHAIN (tu_list) == NULL_TREE) 6531 return; 6532 6533 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL); 6534 6535 /* Enter any actual definitions into the hash table. */ 6536 for (tu = tu_list; tu; tu = TREE_CHAIN (tu)) 6537 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl)) 6538 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl)) 6539 { 6540 PTR *slot; 6541 slot = htab_find_slot (link_hash_table, decl, INSERT); 6542 6543 /* If we've already got a definition, work out which one is 6544 the real one, put it into the hash table, and make the 6545 other one DECL_EXTERNAL. This is important to avoid 6546 putting out two definitions of the same symbol in the 6547 assembly output. */ 6548 if (*slot != NULL) 6549 { 6550 tree old_decl = (tree) *slot; 6551 6552 /* If this is weak or common or whatever, suppress it 6553 in favor of the other definition. */ 6554 if (DECL_WEAK (decl)) 6555 DECL_EXTERNAL (decl) = 1; 6556 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl)) 6557 DECL_EXTERNAL (old_decl) = 1; 6558 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl)) 6559 DECL_EXTERNAL (decl) = 1; 6560 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl)) 6561 DECL_EXTERNAL (old_decl) = 1; 6562 6563 if (DECL_EXTERNAL (decl)) 6564 { 6565 DECL_INITIAL (decl) = NULL_TREE; 6566 DECL_COMMON (decl) = 0; 6567 DECL_ONE_ONLY (decl) = 0; 6568 DECL_WEAK (decl) = 0; 6569 } 6570 else if (DECL_EXTERNAL (old_decl)) 6571 { 6572 DECL_INITIAL (old_decl) = NULL_TREE; 6573 DECL_COMMON (old_decl) = 0; 6574 DECL_ONE_ONLY (old_decl) = 0; 6575 DECL_WEAK (old_decl) = 0; 6576 *slot = decl; 6577 } 6578 else 6579 { 6580 error ("%Jredefinition of global '%D'", decl, decl); 6581 error ("%J'%D' previously defined here", old_decl, old_decl); 6582 } 6583 } 6584 else 6585 *slot = decl; 6586 } 6587 6588 /* Now insert the desired information from all the definitions 6589 into any plain declarations. */ 6590 for (tu = tu_list; tu; tu = TREE_CHAIN (tu)) 6591 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl)) 6592 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl)) 6593 { 6594 tree global_decl; 6595 global_decl = htab_find (link_hash_table, decl); 6596 6597 if (! global_decl) 6598 continue; 6599 6600 /* Print any appropriate error messages, and partially merge 6601 the decls. */ 6602 (void) duplicate_decls (decl, global_decl); 6603 } 6604 6605 htab_delete (link_hash_table); 6606} 6607 6608/* Perform final processing on file-scope data. */ 6609 6610void 6611c_write_global_declarations(void) 6612{ 6613 tree link; 6614 6615 for (link = current_file_decl; link; link = TREE_CHAIN (link)) 6616 { 6617 tree globals = BLOCK_VARS (DECL_INITIAL (link)); 6618 int len = list_length (globals); 6619 tree *vec = xmalloc (sizeof (tree) * len); 6620 int i; 6621 tree decl; 6622 6623 /* Process the decls in the order they were written. */ 6624 6625 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl)) 6626 vec[i] = decl; 6627 6628 wrapup_global_declarations (vec, len); 6629 6630 check_global_declarations (vec, len); 6631 6632 /* Clean up. */ 6633 free (vec); 6634 } 6635} 6636 6637/* Reset the parser's state in preparation for a new file. */ 6638 6639void 6640c_reset_state (void) 6641{ 6642 tree link; 6643 tree file_scope_decl; 6644 6645 /* Pop the global scope. */ 6646 if (current_scope != global_scope) 6647 current_scope = global_scope; 6648 file_scope_decl = current_file_decl; 6649 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0); 6650 BLOCK_SUPERCONTEXT (DECL_INITIAL (file_scope_decl)) = file_scope_decl; 6651 truly_local_externals = NULL_TREE; 6652 6653 /* Start a new global binding level. */ 6654 pushlevel (0); 6655 global_scope = current_scope; 6656 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL); 6657 TREE_CHAIN (current_file_decl) = file_scope_decl; 6658 6659 /* Reintroduce the builtin declarations. */ 6660 for (link = first_builtin_decl; 6661 link != TREE_CHAIN (last_builtin_decl); 6662 link = TREE_CHAIN (link)) 6663 pushdecl (copy_node (link)); 6664} 6665 6666#include "gt-c-decl.h" 6667