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