oops - minor formatting tidy ups to previous delta
[official-gcc.git] / gcc / c-decl.c
blob734429274598d5d4a8aacbe6c7a9fd9538c28108
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "intl.h"
32 #include "tree.h"
33 #include "tree-inline.h"
34 #include "rtl.h"
35 #include "flags.h"
36 #include "function.h"
37 #include "output.h"
38 #include "expr.h"
39 #include "c-tree.h"
40 #include "toplev.h"
41 #include "ggc.h"
42 #include "tm_p.h"
43 #include "cpplib.h"
44 #include "target.h"
45 #include "debug.h"
46 #include "timevar.h"
47 #include "c-common.h"
48 #include "c-pragma.h"
50 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
51 enum decl_context
52 { NORMAL, /* Ordinary declaration */
53 FUNCDEF, /* Function definition */
54 PARM, /* Declaration of parm before function body */
55 FIELD, /* Declaration inside struct or union */
56 BITFIELD, /* Likewise but with specified width */
57 TYPENAME}; /* Typename (inside cast or sizeof) */
60 /* Nonzero if we have seen an invalid cross reference
61 to a struct, union, or enum, but not yet printed the message. */
63 tree pending_invalid_xref;
64 /* File and line to appear in the eventual error message. */
65 const char *pending_invalid_xref_file;
66 int pending_invalid_xref_line;
68 /* While defining an enum type, this is 1 plus the last enumerator
69 constant value. Note that will do not have to save this or `enum_overflow'
70 around nested function definition since such a definition could only
71 occur in an enum value expression and we don't use these variables in
72 that case. */
74 static tree enum_next_value;
76 /* Nonzero means that there was overflow computing enum_next_value. */
78 static int enum_overflow;
80 /* Parsing a function declarator leaves a list of parameter names
81 or a chain or parameter decls here. */
83 static tree last_function_parms;
85 /* Parsing a function declarator leaves here a chain of structure
86 and enum types declared in the parmlist. */
88 static tree last_function_parm_tags;
90 /* After parsing the declarator that starts a function definition,
91 `start_function' puts here the list of parameter names or chain of decls.
92 `store_parm_decls' finds it here. */
94 static tree current_function_parms;
96 /* Similar, for last_function_parm_tags. */
97 static tree current_function_parm_tags;
99 /* Similar, for the file and line that the prototype came from if this is
100 an old-style definition. */
101 static const char *current_function_prototype_file;
102 static int current_function_prototype_line;
104 /* The current statement tree. */
106 static GTY(()) struct stmt_tree_s c_stmt_tree;
108 /* The current scope statement stack. */
110 static GTY(()) tree c_scope_stmt_stack;
112 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
113 that have names. Here so we can clear out their names' definitions
114 at the end of the function. */
116 static GTY(()) tree named_labels;
118 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
120 static GTY(()) tree shadowed_labels;
122 /* Set to 0 at beginning of a function definition, set to 1 if
123 a return statement that specifies a return value is seen. */
125 int current_function_returns_value;
127 /* Set to 0 at beginning of a function definition, set to 1 if
128 a return statement with no argument is seen. */
130 int current_function_returns_null;
132 /* Set to 0 at beginning of a function definition, set to 1 if
133 a call to a noreturn function is seen. */
135 int current_function_returns_abnormally;
137 /* Set to nonzero by `grokdeclarator' for a function
138 whose return type is defaulted, if warnings for this are desired. */
140 static int warn_about_return_type;
142 /* Nonzero when starting a function declared `extern inline'. */
144 static int current_extern_inline;
146 /* For each binding contour we allocate a binding_level structure
147 * which records the names defined in that contour.
148 * Contours include:
149 * 0) the global one
150 * 1) one for each function definition,
151 * where internal declarations of the parameters appear.
152 * 2) one for each compound statement,
153 * to record its declarations.
155 * The current meaning of a name can be found by searching the levels from
156 * the current one out to the global one.
159 /* Note that the information in the `names' component of the global contour
160 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
162 struct binding_level GTY(())
164 /* A chain of _DECL nodes for all variables, constants, functions,
165 and typedef types. These are in the reverse of the order supplied.
167 tree names;
169 /* A list of structure, union and enum definitions,
170 * for looking up tag names.
171 * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
172 * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
173 * or ENUMERAL_TYPE node.
175 tree tags;
177 /* For each level, a list of shadowed outer-level local definitions
178 to be restored when this level is popped.
179 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
180 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
181 tree shadowed;
183 /* For each level (except not the global one),
184 a chain of BLOCK nodes for all the levels
185 that were entered and exited one level down. */
186 tree blocks;
188 /* The BLOCK node for this level, if one has been preallocated.
189 If 0, the BLOCK is allocated (if needed) when the level is popped. */
190 tree this_block;
192 /* The binding level which this one is contained in (inherits from). */
193 struct binding_level *level_chain;
195 /* Nonzero for the level that holds the parameters of a function. */
196 char parm_flag;
198 /* Nonzero if this level "doesn't exist" for tags. */
199 char tag_transparent;
201 /* Nonzero if sublevels of this level "don't exist" for tags.
202 This is set in the parm level of a function definition
203 while reading the function body, so that the outermost block
204 of the function body will be tag-transparent. */
205 char subblocks_tag_transparent;
207 /* Nonzero means make a BLOCK for this level regardless of all else. */
208 char keep;
210 /* Nonzero means make a BLOCK if this level has any subblocks. */
211 char keep_if_subblocks;
213 /* List of decls in `names' that have incomplete structure or
214 union types. */
215 tree incomplete_list;
217 /* A list of decls giving the (reversed) specified order of parms,
218 not including any forward-decls in the parmlist.
219 This is so we can put the parms in proper order for assign_parms. */
220 tree parm_order;
223 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
225 /* The binding level currently in effect. */
227 static GTY(()) struct binding_level *current_binding_level;
229 /* A chain of binding_level structures awaiting reuse. */
231 static GTY((deletable (""))) struct binding_level *free_binding_level;
233 /* The outermost binding level, for names of file scope.
234 This is created when the compiler is started and exists
235 through the entire run. */
237 static GTY(()) struct binding_level *global_binding_level;
239 /* Binding level structures are initialized by copying this one. */
241 static struct binding_level clear_binding_level
242 = {NULL, NULL, NULL, NULL, NULL, NULL_BINDING_LEVEL, 0, 0, 0, 0, 0, NULL,
243 NULL};
245 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
247 static int keep_next_level_flag;
249 /* Nonzero means make a BLOCK for the next level pushed
250 if it has subblocks. */
252 static int keep_next_if_subblocks;
254 /* The chain of outer levels of label scopes.
255 This uses the same data structure used for binding levels,
256 but it works differently: each link in the chain records
257 saved values of named_labels and shadowed_labels for
258 a label binding level outside the current one. */
260 static GTY(()) struct binding_level *label_level_chain;
262 /* Functions called automatically at the beginning and end of execution. */
264 tree static_ctors, static_dtors;
266 /* Forward declarations. */
268 static struct binding_level * make_binding_level PARAMS ((void));
269 static void pop_binding_level PARAMS ((struct binding_level **));
270 static void clear_limbo_values PARAMS ((tree));
271 static int duplicate_decls PARAMS ((tree, tree, int));
272 static int redeclaration_error_message PARAMS ((tree, tree));
273 static void storedecls PARAMS ((tree));
274 static void storetags PARAMS ((tree));
275 static tree lookup_tag PARAMS ((enum tree_code, tree,
276 struct binding_level *, int));
277 static tree lookup_tag_reverse PARAMS ((tree));
278 static tree grokdeclarator PARAMS ((tree, tree, enum decl_context,
279 int));
280 static tree grokparms PARAMS ((tree, int));
281 static void layout_array_type PARAMS ((tree));
282 static tree c_make_fname_decl PARAMS ((tree, int));
283 static void c_expand_body PARAMS ((tree, int, int));
284 static void warn_if_shadowing PARAMS ((tree, tree));
286 /* States indicating how grokdeclarator() should handle declspecs marked
287 with __attribute__((deprecated)). An object declared as
288 __attribute__((deprecated)) suppresses warnings of uses of other
289 deprecated items. */
291 enum deprecated_states {
292 DEPRECATED_NORMAL,
293 DEPRECATED_SUPPRESS
296 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
298 void
299 c_print_identifier (file, node, indent)
300 FILE *file;
301 tree node;
302 int indent;
304 print_node (file, "global", IDENTIFIER_GLOBAL_VALUE (node), indent + 4);
305 print_node (file, "local", IDENTIFIER_LOCAL_VALUE (node), indent + 4);
306 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
307 print_node (file, "implicit", IDENTIFIER_IMPLICIT_DECL (node), indent + 4);
308 print_node (file, "error locus", IDENTIFIER_ERROR_LOCUS (node), indent + 4);
309 print_node (file, "limbo value", IDENTIFIER_LIMBO_VALUE (node), indent + 4);
310 if (C_IS_RESERVED_WORD (node))
312 tree rid = ridpointers[C_RID_CODE (node)];
313 indent_to (file, indent + 4);
314 fprintf (file, "rid ");
315 fprintf (file, HOST_PTR_PRINTF, (void *)rid);
316 fprintf (file, " \"%s\"", IDENTIFIER_POINTER (rid));
320 /* Hook called at end of compilation to assume 1 elt
321 for a top-level tentative array defn that wasn't complete before. */
323 void
324 c_finish_incomplete_decl (decl)
325 tree decl;
327 if (TREE_CODE (decl) == VAR_DECL)
329 tree type = TREE_TYPE (decl);
330 if (type != error_mark_node
331 && TREE_CODE (type) == ARRAY_TYPE
332 && ! DECL_EXTERNAL (decl)
333 && TYPE_DOMAIN (type) == 0)
335 warning_with_decl (decl, "array `%s' assumed to have one element");
337 complete_array_type (type, NULL_TREE, 1);
339 layout_decl (decl, 0);
344 /* Reuse or create a struct for this binding level. */
346 static struct binding_level *
347 make_binding_level ()
349 if (free_binding_level)
351 struct binding_level *result = free_binding_level;
352 free_binding_level = result->level_chain;
353 return result;
355 else
356 return (struct binding_level *) ggc_alloc (sizeof (struct binding_level));
359 /* Remove a binding level from a list and add it to the level chain. */
361 static void
362 pop_binding_level (lp)
363 struct binding_level **lp;
365 struct binding_level *l = *lp;
366 *lp = l->level_chain;
368 memset (l, 0, sizeof (struct binding_level));
369 l->level_chain = free_binding_level;
370 free_binding_level = l;
373 /* Nonzero if we are currently in the global binding level. */
376 global_bindings_p ()
378 return current_binding_level == global_binding_level;
381 void
382 keep_next_level ()
384 keep_next_level_flag = 1;
387 /* Nonzero if the current level needs to have a BLOCK made. */
390 kept_level_p ()
392 return ((current_binding_level->keep_if_subblocks
393 && current_binding_level->blocks != 0)
394 || current_binding_level->keep
395 || current_binding_level->names != 0
396 || (current_binding_level->tags != 0
397 && !current_binding_level->tag_transparent));
400 /* Identify this binding level as a level of parameters.
401 DEFINITION_FLAG is 1 for a definition, 0 for a declaration.
402 But it turns out there is no way to pass the right value for
403 DEFINITION_FLAG, so we ignore it. */
405 void
406 declare_parm_level (definition_flag)
407 int definition_flag ATTRIBUTE_UNUSED;
409 current_binding_level->parm_flag = 1;
412 /* Nonzero if currently making parm declarations. */
415 in_parm_level_p ()
417 return current_binding_level->parm_flag;
420 /* Enter a new binding level.
421 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
422 not for that of tags. */
424 void
425 pushlevel (tag_transparent)
426 int tag_transparent;
428 struct binding_level *newlevel = NULL_BINDING_LEVEL;
430 /* If this is the top level of a function,
431 just make sure that NAMED_LABELS is 0. */
433 if (current_binding_level == global_binding_level)
435 named_labels = 0;
438 newlevel = make_binding_level ();
440 /* Add this level to the front of the chain (stack) of levels that
441 are active. */
443 *newlevel = clear_binding_level;
444 newlevel->tag_transparent
445 = (tag_transparent
446 || (current_binding_level
447 ? current_binding_level->subblocks_tag_transparent
448 : 0));
449 newlevel->level_chain = current_binding_level;
450 current_binding_level = newlevel;
451 newlevel->keep = keep_next_level_flag;
452 keep_next_level_flag = 0;
453 newlevel->keep_if_subblocks = keep_next_if_subblocks;
454 keep_next_if_subblocks = 0;
457 /* Clear the limbo values of all identifiers defined in BLOCK or a subblock. */
459 static void
460 clear_limbo_values (block)
461 tree block;
463 tree tem;
465 for (tem = BLOCK_VARS (block); tem; tem = TREE_CHAIN (tem))
466 if (DECL_NAME (tem) != 0)
467 IDENTIFIER_LIMBO_VALUE (DECL_NAME (tem)) = 0;
469 for (tem = BLOCK_SUBBLOCKS (block); tem; tem = TREE_CHAIN (tem))
470 clear_limbo_values (tem);
473 /* Exit a binding level.
474 Pop the level off, and restore the state of the identifier-decl mappings
475 that were in effect when this level was entered.
477 If KEEP is nonzero, this level had explicit declarations, so
478 and create a "block" (a BLOCK node) for the level
479 to record its declarations and subblocks for symbol table output.
481 If FUNCTIONBODY is nonzero, this level is the body of a function,
482 so create a block as if KEEP were set and also clear out all
483 label names.
485 If REVERSE is nonzero, reverse the order of decls before putting
486 them into the BLOCK. */
488 tree
489 poplevel (keep, reverse, functionbody)
490 int keep;
491 int reverse;
492 int functionbody;
494 tree link;
495 /* The chain of decls was accumulated in reverse order.
496 Put it into forward order, just for cleanliness. */
497 tree decls;
498 tree tags = current_binding_level->tags;
499 tree subblocks = current_binding_level->blocks;
500 tree block = 0;
501 tree decl;
502 int block_previously_created;
504 keep |= current_binding_level->keep;
506 /* This warning is turned off because it causes warnings for
507 declarations like `extern struct foo *x'. */
508 #if 0
509 /* Warn about incomplete structure types in this level. */
510 for (link = tags; link; link = TREE_CHAIN (link))
511 if (!COMPLETE_TYPE_P (TREE_VALUE (link)))
513 tree type = TREE_VALUE (link);
514 tree type_name = TYPE_NAME (type);
515 char *id = IDENTIFIER_POINTER (TREE_CODE (type_name) == IDENTIFIER_NODE
516 ? type_name
517 : DECL_NAME (type_name));
518 switch (TREE_CODE (type))
520 case RECORD_TYPE:
521 error ("`struct %s' incomplete in scope ending here", id);
522 break;
523 case UNION_TYPE:
524 error ("`union %s' incomplete in scope ending here", id);
525 break;
526 case ENUMERAL_TYPE:
527 error ("`enum %s' incomplete in scope ending here", id);
528 break;
531 #endif /* 0 */
533 /* Get the decls in the order they were written.
534 Usually current_binding_level->names is in reverse order.
535 But parameter decls were previously put in forward order. */
537 if (reverse)
538 current_binding_level->names
539 = decls = nreverse (current_binding_level->names);
540 else
541 decls = current_binding_level->names;
543 /* Output any nested inline functions within this block
544 if they weren't already output. */
546 for (decl = decls; decl; decl = TREE_CHAIN (decl))
547 if (TREE_CODE (decl) == FUNCTION_DECL
548 && ! TREE_ASM_WRITTEN (decl)
549 && DECL_INITIAL (decl) != 0
550 && TREE_ADDRESSABLE (decl))
552 /* If this decl was copied from a file-scope decl
553 on account of a block-scope extern decl,
554 propagate TREE_ADDRESSABLE to the file-scope decl.
556 DECL_ABSTRACT_ORIGIN can be set to itself if warn_return_type is
557 true, since then the decl goes through save_for_inline_copying. */
558 if (DECL_ABSTRACT_ORIGIN (decl) != 0
559 && DECL_ABSTRACT_ORIGIN (decl) != decl)
560 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
563 /* We used to warn about unused variables in expand_end_bindings,
564 i.e. while generating RTL. But in function-at-a-time mode we may
565 choose to never expand a function at all (e.g. auto inlining), so
566 we do this explicitly now. */
567 warn_about_unused_variables (getdecls ());
569 /* If there were any declarations or structure tags in that level,
570 or if this level is a function body,
571 create a BLOCK to record them for the life of this function. */
573 block = 0;
574 block_previously_created = (current_binding_level->this_block != 0);
575 if (block_previously_created)
576 block = current_binding_level->this_block;
577 else if (keep || functionbody
578 || (current_binding_level->keep_if_subblocks && subblocks != 0))
579 block = make_node (BLOCK);
580 if (block != 0)
582 BLOCK_VARS (block) = decls;
583 BLOCK_SUBBLOCKS (block) = subblocks;
586 /* In each subblock, record that this is its superior. */
588 for (link = subblocks; link; link = TREE_CHAIN (link))
589 BLOCK_SUPERCONTEXT (link) = block;
591 /* Clear out the meanings of the local variables of this level. */
593 for (link = decls; link; link = TREE_CHAIN (link))
595 if (DECL_NAME (link) != 0)
597 /* If the ident. was used or addressed via a local extern decl,
598 don't forget that fact. */
599 if (DECL_EXTERNAL (link))
601 if (TREE_USED (link))
602 TREE_USED (DECL_NAME (link)) = 1;
603 if (TREE_ADDRESSABLE (link))
604 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
606 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
610 /* Restore all name-meanings of the outer levels
611 that were shadowed by this level. */
613 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
614 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
616 /* If the level being exited is the top level of a function,
617 check over all the labels, and clear out the current
618 (function local) meanings of their names. */
620 if (functionbody)
622 clear_limbo_values (block);
624 /* If this is the top level block of a function,
625 the vars are the function's parameters.
626 Don't leave them in the BLOCK because they are
627 found in the FUNCTION_DECL instead. */
629 BLOCK_VARS (block) = 0;
631 /* Clear out the definitions of all label names,
632 since their scopes end here,
633 and add them to BLOCK_VARS. */
635 for (link = named_labels; link; link = TREE_CHAIN (link))
637 tree label = TREE_VALUE (link);
639 if (DECL_INITIAL (label) == 0)
641 error_with_decl (label, "label `%s' used but not defined");
642 /* Avoid crashing later. */
643 define_label (input_filename, lineno,
644 DECL_NAME (label));
646 else if (warn_unused_label && !TREE_USED (label))
647 warning_with_decl (label, "label `%s' defined but not used");
648 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
650 /* Put the labels into the "variables" of the
651 top-level block, so debugger can see them. */
652 TREE_CHAIN (label) = BLOCK_VARS (block);
653 BLOCK_VARS (block) = label;
657 /* Pop the current level, and free the structure for reuse. */
659 pop_binding_level (&current_binding_level);
661 /* Dispose of the block that we just made inside some higher level. */
662 if (functionbody)
663 DECL_INITIAL (current_function_decl) = block;
664 else if (block)
666 if (!block_previously_created)
667 current_binding_level->blocks
668 = chainon (current_binding_level->blocks, block);
670 /* If we did not make a block for the level just exited,
671 any blocks made for inner levels
672 (since they cannot be recorded as subblocks in that level)
673 must be carried forward so they will later become subblocks
674 of something else. */
675 else if (subblocks)
676 current_binding_level->blocks
677 = chainon (current_binding_level->blocks, subblocks);
679 /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
680 binding contour so that they point to the appropriate construct, i.e.
681 either to the current FUNCTION_DECL node, or else to the BLOCK node
682 we just constructed.
684 Note that for tagged types whose scope is just the formal parameter
685 list for some function type specification, we can't properly set
686 their TYPE_CONTEXTs here, because we don't have a pointer to the
687 appropriate FUNCTION_TYPE node readily available to us. For those
688 cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
689 in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
690 node which will represent the "scope" for these "parameter list local"
691 tagged types. */
693 if (functionbody)
694 for (link = tags; link; link = TREE_CHAIN (link))
695 TYPE_CONTEXT (TREE_VALUE (link)) = current_function_decl;
696 else if (block)
697 for (link = tags; link; link = TREE_CHAIN (link))
698 TYPE_CONTEXT (TREE_VALUE (link)) = block;
700 if (block)
701 TREE_USED (block) = 1;
703 return block;
706 /* Insert BLOCK at the end of the list of subblocks of the
707 current binding level. This is used when a BIND_EXPR is expanded,
708 to handle the BLOCK node inside the BIND_EXPR. */
710 void
711 insert_block (block)
712 tree block;
714 TREE_USED (block) = 1;
715 current_binding_level->blocks
716 = chainon (current_binding_level->blocks, block);
719 /* Set the BLOCK node for the innermost scope
720 (the one we are currently in). */
722 void
723 set_block (block)
724 tree block;
726 current_binding_level->this_block = block;
727 current_binding_level->names = chainon (current_binding_level->names,
728 BLOCK_VARS (block));
729 current_binding_level->blocks = chainon (current_binding_level->blocks,
730 BLOCK_SUBBLOCKS (block));
733 void
734 push_label_level ()
736 struct binding_level *newlevel;
738 newlevel = make_binding_level ();
740 /* Add this level to the front of the chain (stack) of label levels. */
742 newlevel->level_chain = label_level_chain;
743 label_level_chain = newlevel;
745 newlevel->names = named_labels;
746 newlevel->shadowed = shadowed_labels;
747 named_labels = 0;
748 shadowed_labels = 0;
751 void
752 pop_label_level ()
754 struct binding_level *level = label_level_chain;
755 tree link, prev;
757 /* Clear out the definitions of the declared labels in this level.
758 Leave in the list any ordinary, non-declared labels. */
759 for (link = named_labels, prev = 0; link;)
761 if (C_DECLARED_LABEL_FLAG (TREE_VALUE (link)))
763 if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0)
765 error_with_decl (TREE_VALUE (link),
766 "label `%s' used but not defined");
767 /* Avoid crashing later. */
768 define_label (input_filename, lineno,
769 DECL_NAME (TREE_VALUE (link)));
771 else if (warn_unused_label && !TREE_USED (TREE_VALUE (link)))
772 warning_with_decl (TREE_VALUE (link),
773 "label `%s' defined but not used");
774 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
776 /* Delete this element from the list. */
777 link = TREE_CHAIN (link);
778 if (prev)
779 TREE_CHAIN (prev) = link;
780 else
781 named_labels = link;
783 else
785 prev = link;
786 link = TREE_CHAIN (link);
790 /* Bring back all the labels that were shadowed. */
791 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
792 if (DECL_NAME (TREE_VALUE (link)) != 0)
793 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
794 = TREE_VALUE (link);
796 named_labels = chainon (named_labels, level->names);
797 shadowed_labels = level->shadowed;
799 /* Pop the current level, and free the structure for reuse. */
800 pop_binding_level (&label_level_chain);
803 /* Push a definition or a declaration of struct, union or enum tag "name".
804 "type" should be the type node.
805 We assume that the tag "name" is not already defined.
807 Note that the definition may really be just a forward reference.
808 In that case, the TYPE_SIZE will be zero. */
810 void
811 pushtag (name, type)
812 tree name, type;
814 struct binding_level *b;
816 /* Find the proper binding level for this type tag. */
818 for (b = current_binding_level; b->tag_transparent; b = b->level_chain)
819 continue;
821 if (name)
823 /* Record the identifier as the type's name if it has none. */
825 if (TYPE_NAME (type) == 0)
826 TYPE_NAME (type) = name;
829 b->tags = tree_cons (name, type, b->tags);
831 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
832 tagged type we just added to the current binding level. This fake
833 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
834 to output a representation of a tagged type, and it also gives
835 us a convenient place to record the "scope start" address for the
836 tagged type. */
838 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
840 /* An approximation for now, so we can tell this is a function-scope tag.
841 This will be updated in poplevel. */
842 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
845 /* Handle when a new declaration NEWDECL
846 has the same name as an old one OLDDECL
847 in the same binding contour.
848 Prints an error message if appropriate.
850 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
851 Otherwise, return 0.
853 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
854 and OLDDECL is in an outer binding level and should thus not be changed. */
856 static int
857 duplicate_decls (newdecl, olddecl, different_binding_level)
858 tree newdecl, olddecl;
859 int different_binding_level;
861 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
862 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
863 && DECL_INITIAL (newdecl) != 0);
864 tree oldtype = TREE_TYPE (olddecl);
865 tree newtype = TREE_TYPE (newdecl);
866 int errmsg = 0;
868 if (DECL_P (olddecl))
870 if (TREE_CODE (newdecl) == FUNCTION_DECL
871 && TREE_CODE (olddecl) == FUNCTION_DECL
872 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
874 if (DECL_DECLARED_INLINE_P (newdecl)
875 && DECL_UNINLINABLE (newdecl)
876 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
877 /* Already warned elsewhere. */;
878 else if (DECL_DECLARED_INLINE_P (olddecl)
879 && DECL_UNINLINABLE (olddecl)
880 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
881 /* Already warned. */;
882 else if (DECL_DECLARED_INLINE_P (newdecl)
883 && ! DECL_DECLARED_INLINE_P (olddecl)
884 && DECL_UNINLINABLE (olddecl)
885 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
887 warning_with_decl (newdecl,
888 "function `%s' redeclared as inline");
889 warning_with_decl (olddecl,
890 "previous declaration of function `%s' with attribute noinline");
892 else if (DECL_DECLARED_INLINE_P (olddecl)
893 && DECL_UNINLINABLE (newdecl)
894 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
896 warning_with_decl (newdecl,
897 "function `%s' redeclared with attribute noinline");
898 warning_with_decl (olddecl,
899 "previous declaration of function `%s' was inline");
903 DECL_ATTRIBUTES (newdecl)
904 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
907 if (TREE_CODE (newtype) == ERROR_MARK
908 || TREE_CODE (oldtype) == ERROR_MARK)
909 types_match = 0;
911 /* New decl is completely inconsistent with the old one =>
912 tell caller to replace the old one.
913 This is always an error except in the case of shadowing a builtin. */
914 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
916 if (TREE_CODE (olddecl) == FUNCTION_DECL
917 && (DECL_BUILT_IN (olddecl)
918 || DECL_BUILT_IN_NONANSI (olddecl)))
920 /* If you declare a built-in or predefined function name as static,
921 the old definition is overridden,
922 but optionally warn this was a bad choice of name. */
923 if (!TREE_PUBLIC (newdecl))
925 if (!warn_shadow)
927 else if (DECL_BUILT_IN (olddecl))
928 warning_with_decl (newdecl, "shadowing built-in function `%s'");
929 else
930 warning_with_decl (newdecl, "shadowing library function `%s'");
932 /* Likewise, if the built-in is not ansi, then programs can
933 override it even globally without an error. */
934 else if (! DECL_BUILT_IN (olddecl))
935 warning_with_decl (newdecl,
936 "library function `%s' declared as non-function");
938 else if (DECL_BUILT_IN_NONANSI (olddecl))
939 warning_with_decl (newdecl,
940 "built-in function `%s' declared as non-function");
941 else
942 warning_with_decl (newdecl,
943 "built-in function `%s' declared as non-function");
945 else
947 error_with_decl (newdecl, "`%s' redeclared as different kind of symbol");
948 error_with_decl (olddecl, "previous declaration of `%s'");
951 return 0;
954 /* For real parm decl following a forward decl,
955 return 1 so old decl will be reused. */
956 if (types_match && TREE_CODE (newdecl) == PARM_DECL
957 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
958 return 1;
960 /* The new declaration is the same kind of object as the old one.
961 The declarations may partially match. Print warnings if they don't
962 match enough. Ultimately, copy most of the information from the new
963 decl to the old one, and keep using the old one. */
965 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
967 /* A function declaration for a built-in function. */
968 if (!TREE_PUBLIC (newdecl))
970 /* If you declare a built-in function name as static, the
971 built-in definition is overridden,
972 but optionally warn this was a bad choice of name. */
973 if (warn_shadow)
974 warning_with_decl (newdecl, "shadowing built-in function `%s'");
975 /* Discard the old built-in function. */
976 return 0;
978 else if (!types_match)
980 /* Accept the return type of the new declaration if same modes. */
981 tree oldreturntype = TREE_TYPE (oldtype);
982 tree newreturntype = TREE_TYPE (newtype);
984 if (TYPE_MODE (oldreturntype) == TYPE_MODE (newreturntype))
986 /* Function types may be shared, so we can't just modify
987 the return type of olddecl's function type. */
988 tree trytype
989 = build_function_type (newreturntype,
990 TYPE_ARG_TYPES (oldtype));
991 trytype = build_type_attribute_variant (trytype,
992 TYPE_ATTRIBUTES (oldtype));
994 types_match = comptypes (newtype, trytype);
995 if (types_match)
996 oldtype = trytype;
998 /* Accept harmless mismatch in first argument type also.
999 This is for the ffs and fprintf builtins. */
1000 if (TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0
1001 && TYPE_ARG_TYPES (oldtype) != 0
1002 && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != 0
1003 && TREE_VALUE (TYPE_ARG_TYPES (oldtype)) != 0
1004 && (TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (newtype)))
1005 == TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (oldtype)))))
1007 /* Function types may be shared, so we can't just modify
1008 the return type of olddecl's function type. */
1009 tree trytype
1010 = build_function_type (TREE_TYPE (oldtype),
1011 tree_cons (NULL_TREE,
1012 TREE_VALUE (TYPE_ARG_TYPES (newtype)),
1013 TREE_CHAIN (TYPE_ARG_TYPES (oldtype))));
1014 trytype = build_type_attribute_variant (trytype,
1015 TYPE_ATTRIBUTES (oldtype));
1017 types_match = comptypes (newtype, trytype);
1018 if (types_match)
1019 oldtype = trytype;
1021 if (! different_binding_level)
1022 TREE_TYPE (olddecl) = oldtype;
1024 else if (TYPE_ARG_TYPES (oldtype) == NULL
1025 && TYPE_ARG_TYPES (newtype) != NULL)
1027 /* For bcmp, bzero, fputs the builtin type has arguments not
1028 specified. Use the ones from the prototype so that type checking
1029 is done for them. */
1030 tree trytype
1031 = build_function_type (TREE_TYPE (oldtype),
1032 TYPE_ARG_TYPES (newtype));
1033 trytype = build_type_attribute_variant (trytype,
1034 TYPE_ATTRIBUTES (oldtype));
1036 oldtype = trytype;
1037 if (! different_binding_level)
1038 TREE_TYPE (olddecl) = oldtype;
1040 if (!types_match)
1042 /* If types don't match for a built-in, throw away the built-in. */
1043 warning_with_decl (newdecl, "conflicting types for built-in function `%s'");
1044 return 0;
1047 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1048 && DECL_SOURCE_LINE (olddecl) == 0)
1050 /* A function declaration for a predeclared function
1051 that isn't actually built in. */
1052 if (!TREE_PUBLIC (newdecl))
1054 /* If you declare it as static, the
1055 default definition is overridden. */
1056 return 0;
1058 else if (!types_match)
1060 /* If the types don't match, preserve volatility indication.
1061 Later on, we will discard everything else about the
1062 default declaration. */
1063 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1066 /* Permit char *foo () to match void *foo (...) if not pedantic,
1067 if one of them came from a system header file. */
1068 else if (!types_match
1069 && TREE_CODE (olddecl) == FUNCTION_DECL
1070 && TREE_CODE (newdecl) == FUNCTION_DECL
1071 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
1072 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
1073 && (DECL_IN_SYSTEM_HEADER (olddecl)
1074 || DECL_IN_SYSTEM_HEADER (newdecl))
1075 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
1076 && TYPE_ARG_TYPES (oldtype) == 0
1077 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
1078 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
1080 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
1081 && TYPE_ARG_TYPES (newtype) == 0
1082 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
1083 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
1085 if (pedantic)
1086 pedwarn_with_decl (newdecl, "conflicting types for `%s'");
1087 /* Make sure we keep void * as ret type, not char *. */
1088 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
1089 TREE_TYPE (newdecl) = newtype = oldtype;
1091 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
1092 we will come back here again. */
1093 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1095 else if (!types_match
1096 /* Permit char *foo (int, ...); followed by char *foo ();
1097 if not pedantic. */
1098 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
1099 && ! pedantic
1100 /* Return types must still match. */
1101 && comptypes (TREE_TYPE (oldtype),
1102 TREE_TYPE (newtype))
1103 && TYPE_ARG_TYPES (newtype) == 0))
1105 error_with_decl (newdecl, "conflicting types for `%s'");
1106 /* Check for function type mismatch
1107 involving an empty arglist vs a nonempty one. */
1108 if (TREE_CODE (olddecl) == FUNCTION_DECL
1109 && comptypes (TREE_TYPE (oldtype),
1110 TREE_TYPE (newtype))
1111 && ((TYPE_ARG_TYPES (oldtype) == 0
1112 && DECL_INITIAL (olddecl) == 0)
1114 (TYPE_ARG_TYPES (newtype) == 0
1115 && DECL_INITIAL (newdecl) == 0)))
1117 /* Classify the problem further. */
1118 tree t = TYPE_ARG_TYPES (oldtype);
1119 if (t == 0)
1120 t = TYPE_ARG_TYPES (newtype);
1121 for (; t; t = TREE_CHAIN (t))
1123 tree type = TREE_VALUE (t);
1125 if (TREE_CHAIN (t) == 0
1126 && TYPE_MAIN_VARIANT (type) != void_type_node)
1128 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1129 break;
1132 if (c_type_promotes_to (type) != type)
1134 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1135 break;
1139 error_with_decl (olddecl, "previous declaration of `%s'");
1141 /* TLS cannot follow non-TLS declaration. */
1142 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1143 && !DECL_THREAD_LOCAL (olddecl) && DECL_THREAD_LOCAL (newdecl))
1145 error_with_decl (newdecl, "thread-local declaration of `%s' follows non thread-local declaration");
1146 error_with_decl (olddecl, "previous declaration of `%s'");
1148 /* non-TLS declaration cannot follow TLS declaration. */
1149 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1150 && DECL_THREAD_LOCAL (olddecl) && !DECL_THREAD_LOCAL (newdecl))
1152 error_with_decl (newdecl, "non thread-local declaration of `%s' follows thread-local declaration");
1153 error_with_decl (olddecl, "previous declaration of `%s'");
1155 else
1157 errmsg = redeclaration_error_message (newdecl, olddecl);
1158 if (errmsg)
1160 switch (errmsg)
1162 case 1:
1163 error_with_decl (newdecl, "redefinition of `%s'");
1164 break;
1165 case 2:
1166 error_with_decl (newdecl, "redeclaration of `%s'");
1167 break;
1168 case 3:
1169 error_with_decl (newdecl, "conflicting declarations of `%s'");
1170 break;
1171 default:
1172 abort ();
1175 error_with_decl (olddecl,
1176 ((DECL_INITIAL (olddecl)
1177 && current_binding_level == global_binding_level)
1178 ? "`%s' previously defined here"
1179 : "`%s' previously declared here"));
1180 return 0;
1182 else if (TREE_CODE (newdecl) == TYPE_DECL
1183 && (DECL_IN_SYSTEM_HEADER (olddecl)
1184 || DECL_IN_SYSTEM_HEADER (newdecl)))
1186 warning_with_decl (newdecl, "redefinition of `%s'");
1187 warning_with_decl
1188 (olddecl,
1189 ((DECL_INITIAL (olddecl)
1190 && current_binding_level == global_binding_level)
1191 ? "`%s' previously defined here"
1192 : "`%s' previously declared here"));
1194 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1195 && DECL_INITIAL (olddecl) != 0
1196 && TYPE_ARG_TYPES (oldtype) == 0
1197 && TYPE_ARG_TYPES (newtype) != 0
1198 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1200 tree type, parm;
1201 int nargs;
1202 /* Prototype decl follows defn w/o prototype. */
1204 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1205 type = TYPE_ARG_TYPES (newtype),
1206 nargs = 1;
1208 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1210 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1211 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1213 warning_with_decl (newdecl, "prototype for `%s' follows");
1214 warning_with_decl (olddecl, "non-prototype definition here");
1215 break;
1217 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1218 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1220 error_with_decl (newdecl,
1221 "prototype for `%s' follows and number of arguments doesn't match");
1222 error_with_decl (olddecl, "non-prototype definition here");
1223 errmsg = 1;
1224 break;
1226 /* Type for passing arg must be consistent
1227 with that declared for the arg. */
1228 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type)))
1230 error_with_decl (newdecl,
1231 "prototype for `%s' follows and argument %d doesn't match",
1232 nargs);
1233 error_with_decl (olddecl, "non-prototype definition here");
1234 errmsg = 1;
1235 break;
1239 /* Warn about mismatches in various flags. */
1240 else
1242 /* Warn if function is now inline
1243 but was previously declared not inline and has been called. */
1244 if (TREE_CODE (olddecl) == FUNCTION_DECL
1245 && ! DECL_DECLARED_INLINE_P (olddecl)
1246 && DECL_DECLARED_INLINE_P (newdecl)
1247 && TREE_USED (olddecl))
1248 warning_with_decl (newdecl,
1249 "`%s' declared inline after being called");
1250 if (TREE_CODE (olddecl) == FUNCTION_DECL
1251 && ! DECL_DECLARED_INLINE_P (olddecl)
1252 && DECL_DECLARED_INLINE_P (newdecl)
1253 && DECL_INITIAL (olddecl) != 0)
1254 warning_with_decl (newdecl,
1255 "`%s' declared inline after its definition");
1257 /* If pedantic, warn when static declaration follows a non-static
1258 declaration. Otherwise, do so only for functions. */
1259 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1260 && TREE_PUBLIC (olddecl)
1261 && !TREE_PUBLIC (newdecl))
1262 warning_with_decl (newdecl, "static declaration for `%s' follows non-static");
1264 /* If warn_traditional, warn when a non-static function
1265 declaration follows a static one. */
1266 if (warn_traditional && !in_system_header
1267 && TREE_CODE (olddecl) == FUNCTION_DECL
1268 && !TREE_PUBLIC (olddecl)
1269 && TREE_PUBLIC (newdecl))
1270 warning_with_decl (newdecl, "non-static declaration for `%s' follows static");
1272 /* Warn when const declaration follows a non-const
1273 declaration, but not for functions. */
1274 if (TREE_CODE (olddecl) != FUNCTION_DECL
1275 && !TREE_READONLY (olddecl)
1276 && TREE_READONLY (newdecl))
1277 warning_with_decl (newdecl, "const declaration for `%s' follows non-const");
1278 /* These bits are logically part of the type, for variables.
1279 But not for functions
1280 (where qualifiers are not valid ANSI anyway). */
1281 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1282 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1283 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1284 pedwarn_with_decl (newdecl, "type qualifiers for `%s' conflict with previous decl");
1288 /* Optionally warn about more than one declaration for the same name. */
1289 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1290 /* Don't warn about a function declaration
1291 followed by a definition. */
1292 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1293 && DECL_INITIAL (olddecl) == 0)
1294 /* Don't warn about extern decl followed by (tentative) definition. */
1295 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1297 warning_with_decl (newdecl, "redundant redeclaration of `%s' in same scope");
1298 warning_with_decl (olddecl, "previous declaration of `%s'");
1301 /* Copy all the DECL_... slots specified in the new decl
1302 except for any that we copy here from the old type.
1304 Past this point, we don't change OLDTYPE and NEWTYPE
1305 even if we change the types of NEWDECL and OLDDECL. */
1307 if (types_match)
1309 /* When copying info to olddecl, we store into write_olddecl
1310 instead. This allows us to avoid modifying olddecl when
1311 different_binding_level is true. */
1312 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1314 /* Merge the data types specified in the two decls. */
1315 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1317 if (different_binding_level)
1319 if (TYPE_ARG_TYPES (oldtype) != 0
1320 && TYPE_ARG_TYPES (newtype) == 0)
1321 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1322 else
1323 TREE_TYPE (newdecl)
1324 = build_type_attribute_variant
1325 (newtype,
1326 merge_attributes (TYPE_ATTRIBUTES (newtype),
1327 TYPE_ATTRIBUTES (oldtype)));
1329 else
1330 TREE_TYPE (newdecl)
1331 = TREE_TYPE (olddecl)
1332 = common_type (newtype, oldtype);
1335 /* Lay the type out, unless already done. */
1336 if (oldtype != TREE_TYPE (newdecl))
1338 if (TREE_TYPE (newdecl) != error_mark_node)
1339 layout_type (TREE_TYPE (newdecl));
1340 if (TREE_CODE (newdecl) != FUNCTION_DECL
1341 && TREE_CODE (newdecl) != TYPE_DECL
1342 && TREE_CODE (newdecl) != CONST_DECL)
1343 layout_decl (newdecl, 0);
1345 else
1347 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1348 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1349 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1350 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1351 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1352 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1354 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1355 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1359 /* Keep the old rtl since we can safely use it. */
1360 COPY_DECL_RTL (olddecl, newdecl);
1362 /* Merge the type qualifiers. */
1363 if (TREE_READONLY (newdecl))
1364 TREE_READONLY (write_olddecl) = 1;
1366 if (TREE_THIS_VOLATILE (newdecl))
1368 TREE_THIS_VOLATILE (write_olddecl) = 1;
1369 if (TREE_CODE (newdecl) == VAR_DECL
1370 /* If an automatic variable is re-declared in the same
1371 function scope, but the old declaration was not
1372 volatile, make_var_volatile() would crash because the
1373 variable would have been assigned to a pseudo, not a
1374 MEM. Since this duplicate declaration is invalid
1375 anyway, we just skip the call. */
1376 && errmsg == 0)
1377 make_var_volatile (newdecl);
1380 /* Keep source location of definition rather than declaration. */
1381 /* When called with different_binding_level set, keep the old
1382 information so that meaningful diagnostics can be given. */
1383 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1384 && ! different_binding_level)
1386 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1387 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1390 /* Merge the unused-warning information. */
1391 if (DECL_IN_SYSTEM_HEADER (olddecl))
1392 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1393 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1394 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1396 /* Merge the initialization information. */
1397 /* When called with different_binding_level set, don't copy over
1398 DECL_INITIAL, so that we don't accidentally change function
1399 declarations into function definitions. */
1400 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1401 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1403 /* Merge the section attribute.
1404 We want to issue an error if the sections conflict but that must be
1405 done later in decl_attributes since we are called before attributes
1406 are assigned. */
1407 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1408 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1410 /* Copy the assembler name.
1411 Currently, it can only be defined in the prototype. */
1412 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1414 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1416 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1417 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1418 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1419 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1420 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1423 /* If cannot merge, then use the new type and qualifiers,
1424 and don't preserve the old rtl. */
1425 else if (! different_binding_level)
1427 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1428 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1429 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1430 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1433 /* Merge the storage class information. */
1434 merge_weak (newdecl, olddecl);
1436 /* For functions, static overrides non-static. */
1437 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1439 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1440 /* This is since we don't automatically
1441 copy the attributes of NEWDECL into OLDDECL. */
1442 /* No need to worry about different_binding_level here because
1443 then TREE_PUBLIC (newdecl) was true. */
1444 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1445 /* If this clears `static', clear it in the identifier too. */
1446 if (! TREE_PUBLIC (olddecl))
1447 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1449 if (DECL_EXTERNAL (newdecl))
1451 if (! different_binding_level)
1453 /* Don't mess with these flags on local externs; they remain
1454 external even if there's a declaration at file scope which
1455 isn't. */
1456 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1457 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1459 /* An extern decl does not override previous storage class. */
1460 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1461 if (! DECL_EXTERNAL (newdecl))
1462 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1464 else
1466 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1467 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1470 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1472 /* If we're redefining a function previously defined as extern
1473 inline, make sure we emit debug info for the inline before we
1474 throw it away, in case it was inlined into a function that hasn't
1475 been written out yet. */
1476 if (new_is_definition && DECL_INITIAL (olddecl) && TREE_USED (olddecl))
1478 (*debug_hooks->outlining_inline_function) (olddecl);
1480 /* The new defn must not be inline. */
1481 DECL_INLINE (newdecl) = 0;
1482 DECL_UNINLINABLE (newdecl) = 1;
1484 else
1486 /* If either decl says `inline', this fn is inline,
1487 unless its definition was passed already. */
1488 if (DECL_DECLARED_INLINE_P (newdecl)
1489 || DECL_DECLARED_INLINE_P (olddecl))
1490 DECL_DECLARED_INLINE_P (newdecl) = 1;
1492 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1493 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1496 if (DECL_BUILT_IN (olddecl))
1498 /* Get rid of any built-in function if new arg types don't match it
1499 or if we have a function definition. */
1500 if (! types_match || new_is_definition)
1502 if (! different_binding_level)
1504 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1505 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1508 else
1510 /* If redeclaring a builtin function, and not a definition,
1511 it stays built in. */
1512 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1513 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1517 /* Also preserve various other info from the definition. */
1518 if (! new_is_definition)
1520 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1521 /* When called with different_binding_level set, don't copy over
1522 DECL_INITIAL, so that we don't accidentally change function
1523 declarations into function definitions. */
1524 if (! different_binding_level)
1525 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1526 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1527 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1528 DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
1529 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1531 /* Set DECL_INLINE on the declaration if we've got a body
1532 from which to instantiate. */
1533 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1535 DECL_INLINE (newdecl) = 1;
1536 DECL_ABSTRACT_ORIGIN (newdecl)
1537 = (different_binding_level
1538 ? DECL_ORIGIN (olddecl)
1539 : DECL_ABSTRACT_ORIGIN (olddecl));
1542 else
1544 /* If a previous declaration said inline, mark the
1545 definition as inlinable. */
1546 if (DECL_DECLARED_INLINE_P (newdecl)
1547 && ! DECL_UNINLINABLE (newdecl))
1548 DECL_INLINE (newdecl) = 1;
1551 if (different_binding_level)
1552 return 0;
1554 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1555 But preserve OLDDECL's DECL_UID. */
1557 unsigned olddecl_uid = DECL_UID (olddecl);
1559 memcpy ((char *) olddecl + sizeof (struct tree_common),
1560 (char *) newdecl + sizeof (struct tree_common),
1561 sizeof (struct tree_decl) - sizeof (struct tree_common));
1562 DECL_UID (olddecl) = olddecl_uid;
1565 /* NEWDECL contains the merged attribute lists.
1566 Update OLDDECL to be the same. */
1567 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1569 return 1;
1572 /* Check whether decl-node X shadows an existing declaration.
1573 OLDLOCAL is the old IDENTIFIER_LOCAL_VALUE of the DECL_NAME of X,
1574 which might be a NULL_TREE. */
1575 static void
1576 warn_if_shadowing (x, oldlocal)
1577 tree x, oldlocal;
1579 tree name;
1581 if (DECL_EXTERNAL (x))
1582 return;
1584 name = DECL_NAME (x);
1586 /* Warn if shadowing an argument at the top level of the body. */
1587 if (oldlocal != 0
1588 /* This warning doesn't apply to the parms of a nested fcn. */
1589 && ! current_binding_level->parm_flag
1590 /* Check that this is one level down from the parms. */
1591 && current_binding_level->level_chain->parm_flag
1592 /* Check that the decl being shadowed
1593 comes from the parm level, one level up. */
1594 && chain_member (oldlocal, current_binding_level->level_chain->names))
1596 if (TREE_CODE (oldlocal) == PARM_DECL)
1597 pedwarn ("declaration of `%s' shadows a parameter",
1598 IDENTIFIER_POINTER (name));
1599 else
1600 pedwarn ("declaration of `%s' shadows a symbol from the parameter list",
1601 IDENTIFIER_POINTER (name));
1603 /* Maybe warn if shadowing something else. */
1604 else if (warn_shadow
1605 /* No shadow warnings for internally generated vars. */
1606 && DECL_SOURCE_LINE (x) != 0
1607 /* No shadow warnings for vars made for inlining. */
1608 && ! DECL_FROM_INLINE (x))
1610 if (TREE_CODE (x) == PARM_DECL
1611 && current_binding_level->level_chain->parm_flag)
1612 /* Don't warn about the parm names in function declarator
1613 within a function declarator.
1614 It would be nice to avoid warning in any function
1615 declarator in a declaration, as opposed to a definition,
1616 but there is no way to tell it's not a definition. */
1618 else if (oldlocal)
1620 if (TREE_CODE (oldlocal) == PARM_DECL)
1621 shadow_warning ("a parameter", name, oldlocal);
1622 else
1623 shadow_warning ("a previous local", name, oldlocal);
1625 else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
1626 && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
1627 shadow_warning ("a global declaration", name,
1628 IDENTIFIER_GLOBAL_VALUE (name));
1632 /* Record a decl-node X as belonging to the current lexical scope.
1633 Check for errors (such as an incompatible declaration for the same
1634 name already seen in the same scope).
1636 Returns either X or an old decl for the same name.
1637 If an old decl is returned, it may have been smashed
1638 to agree with what X says. */
1640 tree
1641 pushdecl (x)
1642 tree x;
1644 tree t;
1645 tree name = DECL_NAME (x);
1646 struct binding_level *b = current_binding_level;
1648 /* Functions need the lang_decl data. */
1649 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1650 DECL_LANG_SPECIFIC (x) = (struct lang_decl *)
1651 ggc_alloc_cleared (sizeof (struct lang_decl));
1653 DECL_CONTEXT (x) = current_function_decl;
1654 /* A local extern declaration for a function doesn't constitute nesting.
1655 A local auto declaration does, since it's a forward decl
1656 for a nested function coming later. */
1657 if ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1658 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x))
1659 DECL_CONTEXT (x) = 0;
1661 if (name)
1663 int different_binding_level = 0;
1665 if (warn_nested_externs
1666 && DECL_EXTERNAL (x)
1667 && b != global_binding_level
1668 && x != IDENTIFIER_IMPLICIT_DECL (name)
1669 /* No error messages for __FUNCTION__ and __PRETTY_FUNCTION__. */
1670 && !DECL_IN_SYSTEM_HEADER (x))
1671 warning ("nested extern declaration of `%s'",
1672 IDENTIFIER_POINTER (name));
1674 t = lookup_name_current_level (name);
1675 if (! t && DECL_EXTERNAL (x) && TREE_PUBLIC (x))
1677 t = IDENTIFIER_GLOBAL_VALUE (name);
1678 /* Type decls at global scope don't conflict with externs declared
1679 inside lexical blocks. */
1680 if (! t || TREE_CODE (t) == TYPE_DECL)
1681 /* If there's no visible global declaration, try for an
1682 invisible one. */
1683 t = IDENTIFIER_LIMBO_VALUE (name);
1684 different_binding_level = 1;
1686 if (t != 0 && t == error_mark_node)
1687 /* error_mark_node is 0 for a while during initialization! */
1689 t = 0;
1690 error_with_decl (x, "`%s' used prior to declaration");
1693 /* If this decl is `static' and an implicit decl was seen previously,
1694 warn. */
1695 if (TREE_PUBLIC (name)
1696 /* Don't test for DECL_EXTERNAL, because grokdeclarator
1697 sets this for all functions. */
1698 && ! TREE_PUBLIC (x)
1699 && (TREE_CODE (x) == FUNCTION_DECL || b == global_binding_level)
1700 /* We used to warn also for explicit extern followed by static,
1701 but sometimes you need to do it that way. */
1702 && IDENTIFIER_IMPLICIT_DECL (name) != 0)
1704 pedwarn ("`%s' was declared implicitly `extern' and later `static'",
1705 IDENTIFIER_POINTER (name));
1706 pedwarn_with_file_and_line
1707 (DECL_SOURCE_FILE (IDENTIFIER_IMPLICIT_DECL (name)),
1708 DECL_SOURCE_LINE (IDENTIFIER_IMPLICIT_DECL (name)),
1709 "previous declaration of `%s'",
1710 IDENTIFIER_POINTER (name));
1711 TREE_THIS_VOLATILE (name) = 1;
1714 if (t != 0 && duplicate_decls (x, t, different_binding_level))
1716 if (TREE_CODE (t) == PARM_DECL)
1718 /* Don't allow more than one "real" duplicate
1719 of a forward parm decl. */
1720 TREE_ASM_WRITTEN (t) = TREE_ASM_WRITTEN (x);
1721 return t;
1723 return t;
1726 /* If we are processing a typedef statement, generate a whole new
1727 ..._TYPE node (which will be just an variant of the existing
1728 ..._TYPE node with identical properties) and then install the
1729 TYPE_DECL node generated to represent the typedef name as the
1730 TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1732 The whole point here is to end up with a situation where each
1733 and every ..._TYPE node the compiler creates will be uniquely
1734 associated with AT MOST one node representing a typedef name.
1735 This way, even though the compiler substitutes corresponding
1736 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1737 early on, later parts of the compiler can always do the reverse
1738 translation and get back the corresponding typedef name. For
1739 example, given:
1741 typedef struct S MY_TYPE;
1742 MY_TYPE object;
1744 Later parts of the compiler might only know that `object' was of
1745 type `struct S' if it were not for code just below. With this
1746 code however, later parts of the compiler see something like:
1748 struct S' == struct S
1749 typedef struct S' MY_TYPE;
1750 struct S' object;
1752 And they can then deduce (from the node for type struct S') that
1753 the original object declaration was:
1755 MY_TYPE object;
1757 Being able to do this is important for proper support of protoize,
1758 and also for generating precise symbolic debugging information
1759 which takes full account of the programmer's (typedef) vocabulary.
1761 Obviously, we don't want to generate a duplicate ..._TYPE node if
1762 the TYPE_DECL node that we are now processing really represents a
1763 standard built-in type.
1765 Since all standard types are effectively declared at line zero
1766 in the source file, we can easily check to see if we are working
1767 on a standard type by checking the current value of lineno. */
1769 if (TREE_CODE (x) == TYPE_DECL)
1771 if (DECL_SOURCE_LINE (x) == 0)
1773 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1774 TYPE_NAME (TREE_TYPE (x)) = x;
1776 else if (TREE_TYPE (x) != error_mark_node
1777 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1779 tree tt = TREE_TYPE (x);
1780 DECL_ORIGINAL_TYPE (x) = tt;
1781 tt = build_type_copy (tt);
1782 TYPE_NAME (tt) = x;
1783 TREE_USED (tt) = TREE_USED (x);
1784 TREE_TYPE (x) = tt;
1788 /* Multiple external decls of the same identifier ought to match.
1789 We get warnings about inline functions where they are defined.
1790 Avoid duplicate warnings where they are used. */
1791 if (TREE_PUBLIC (x)
1792 && ! (TREE_CODE (x) == FUNCTION_DECL && DECL_INLINE (x)))
1794 tree decl;
1796 if (IDENTIFIER_LIMBO_VALUE (name) != 0)
1797 /* Decls in limbo are always extern, so no need to check that. */
1798 decl = IDENTIFIER_LIMBO_VALUE (name);
1799 else
1800 decl = 0;
1802 if (decl && ! comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1803 /* If old decl is built-in, we already warned if we should. */
1804 && !DECL_BUILT_IN (decl))
1806 pedwarn_with_decl (x,
1807 "type mismatch with previous external decl");
1808 pedwarn_with_decl (decl, "previous external decl of `%s'");
1812 /* If a function has had an implicit declaration, and then is defined,
1813 make sure they are compatible. */
1815 if (IDENTIFIER_IMPLICIT_DECL (name) != 0
1816 && IDENTIFIER_GLOBAL_VALUE (name) == 0
1817 && TREE_CODE (x) == FUNCTION_DECL
1818 && ! comptypes (TREE_TYPE (x),
1819 TREE_TYPE (IDENTIFIER_IMPLICIT_DECL (name))))
1821 warning_with_decl (x, "type mismatch with previous implicit declaration");
1822 warning_with_decl (IDENTIFIER_IMPLICIT_DECL (name),
1823 "previous implicit declaration of `%s'");
1826 /* This name is new in its binding level.
1827 Install the new declaration and return it. */
1828 if (b == global_binding_level)
1830 /* Install a global value. */
1832 /* If the first global decl has external linkage,
1833 warn if we later see static one. */
1834 if (IDENTIFIER_GLOBAL_VALUE (name) == 0 && TREE_PUBLIC (x))
1835 TREE_PUBLIC (name) = 1;
1837 IDENTIFIER_GLOBAL_VALUE (name) = x;
1839 /* We no longer care about any previous block level declarations. */
1840 IDENTIFIER_LIMBO_VALUE (name) = 0;
1842 /* Don't forget if the function was used via an implicit decl. */
1843 if (IDENTIFIER_IMPLICIT_DECL (name)
1844 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
1845 TREE_USED (x) = 1, TREE_USED (name) = 1;
1847 /* Don't forget if its address was taken in that way. */
1848 if (IDENTIFIER_IMPLICIT_DECL (name)
1849 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
1850 TREE_ADDRESSABLE (x) = 1;
1852 /* Warn about mismatches against previous implicit decl. */
1853 if (IDENTIFIER_IMPLICIT_DECL (name) != 0
1854 /* If this real decl matches the implicit, don't complain. */
1855 && ! (TREE_CODE (x) == FUNCTION_DECL
1856 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (x)))
1857 == integer_type_node)))
1858 pedwarn ("`%s' was previously implicitly declared to return `int'",
1859 IDENTIFIER_POINTER (name));
1861 /* If this decl is `static' and an `extern' was seen previously,
1862 that is erroneous. */
1863 if (TREE_PUBLIC (name)
1864 && ! TREE_PUBLIC (x) && ! DECL_EXTERNAL (x))
1866 /* Okay to redeclare an ANSI built-in as static. */
1867 if (t != 0 && DECL_BUILT_IN (t))
1869 /* Okay to declare a non-ANSI built-in as anything. */
1870 else if (t != 0 && DECL_BUILT_IN_NONANSI (t))
1872 /* Okay to have global type decl after an earlier extern
1873 declaration inside a lexical block. */
1874 else if (TREE_CODE (x) == TYPE_DECL)
1876 else if (IDENTIFIER_IMPLICIT_DECL (name))
1878 if (! TREE_THIS_VOLATILE (name))
1879 pedwarn ("`%s' was declared implicitly `extern' and later `static'",
1880 IDENTIFIER_POINTER (name));
1882 else
1883 pedwarn ("`%s' was declared `extern' and later `static'",
1884 IDENTIFIER_POINTER (name));
1887 else
1889 /* Here to install a non-global value. */
1890 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1891 tree oldglobal = IDENTIFIER_GLOBAL_VALUE (name);
1893 IDENTIFIER_LOCAL_VALUE (name) = x;
1895 /* If this is an extern function declaration, see if we
1896 have a global definition or declaration for the function. */
1897 if (oldlocal == 0
1898 && oldglobal != 0
1899 && TREE_CODE (x) == FUNCTION_DECL
1900 && TREE_CODE (oldglobal) == FUNCTION_DECL
1901 && DECL_EXTERNAL (x)
1902 && ! DECL_DECLARED_INLINE_P (x))
1904 /* We have one. Their types must agree. */
1905 if (! comptypes (TREE_TYPE (x),
1906 TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (name))))
1907 pedwarn_with_decl (x, "extern declaration of `%s' doesn't match global one");
1908 else
1910 /* Inner extern decl is inline if global one is.
1911 Copy enough to really inline it. */
1912 if (DECL_DECLARED_INLINE_P (oldglobal))
1914 DECL_DECLARED_INLINE_P (x)
1915 = DECL_DECLARED_INLINE_P (oldglobal);
1916 DECL_INLINE (x) = DECL_INLINE (oldglobal);
1917 DECL_INITIAL (x) = (current_function_decl == oldglobal
1918 ? 0 : DECL_INITIAL (oldglobal));
1919 DECL_SAVED_INSNS (x) = DECL_SAVED_INSNS (oldglobal);
1920 DECL_NUM_STMTS (x) = DECL_NUM_STMTS (oldglobal);
1921 DECL_ARGUMENTS (x) = DECL_ARGUMENTS (oldglobal);
1922 DECL_RESULT (x) = DECL_RESULT (oldglobal);
1923 TREE_ASM_WRITTEN (x) = TREE_ASM_WRITTEN (oldglobal);
1924 DECL_ABSTRACT_ORIGIN (x)
1925 = DECL_ABSTRACT_ORIGIN (oldglobal);
1927 /* Inner extern decl is built-in if global one is. */
1928 if (DECL_BUILT_IN (oldglobal))
1930 DECL_BUILT_IN_CLASS (x) = DECL_BUILT_IN_CLASS (oldglobal);
1931 DECL_FUNCTION_CODE (x) = DECL_FUNCTION_CODE (oldglobal);
1933 /* Keep the arg types from a file-scope fcn defn. */
1934 if (TYPE_ARG_TYPES (TREE_TYPE (oldglobal)) != 0
1935 && DECL_INITIAL (oldglobal)
1936 && TYPE_ARG_TYPES (TREE_TYPE (x)) == 0)
1937 TREE_TYPE (x) = TREE_TYPE (oldglobal);
1941 #if 0
1942 /* This case is probably sometimes the right thing to do. */
1943 /* If we have a local external declaration,
1944 then any file-scope declaration should not
1945 have been static. */
1946 if (oldlocal == 0 && oldglobal != 0
1947 && !TREE_PUBLIC (oldglobal)
1948 && DECL_EXTERNAL (x) && TREE_PUBLIC (x))
1949 warning ("`%s' locally external but globally static",
1950 IDENTIFIER_POINTER (name));
1951 #endif
1953 /* If we have a local external declaration,
1954 and no file-scope declaration has yet been seen,
1955 then if we later have a file-scope decl it must not be static. */
1956 if (oldlocal == 0
1957 && DECL_EXTERNAL (x)
1958 && TREE_PUBLIC (x))
1960 if (oldglobal == 0)
1961 TREE_PUBLIC (name) = 1;
1963 /* Save this decl, so that we can do type checking against
1964 other decls after it falls out of scope.
1966 Only save it once. This prevents temporary decls created in
1967 expand_inline_function from being used here, since this
1968 will have been set when the inline function was parsed.
1969 It also helps give slightly better warnings. */
1970 if (IDENTIFIER_LIMBO_VALUE (name) == 0)
1971 IDENTIFIER_LIMBO_VALUE (name) = x;
1974 warn_if_shadowing (x, oldlocal);
1976 /* If storing a local value, there may already be one (inherited).
1977 If so, record it for restoration when this binding level ends. */
1978 if (oldlocal != 0)
1979 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1982 /* Keep list of variables in this level with incomplete type.
1983 If the input is erroneous, we can have error_mark in the type
1984 slot (e.g. "f(void a, ...)") - that doesn't count as an
1985 incomplete type. */
1986 if (TREE_TYPE (x) != error_mark_node
1987 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1989 tree element = TREE_TYPE (x);
1991 while (TREE_CODE (element) == ARRAY_TYPE)
1992 element = TREE_TYPE (element);
1993 if (TREE_CODE (element) == RECORD_TYPE
1994 || TREE_CODE (element) == UNION_TYPE)
1995 b->incomplete_list = tree_cons (NULL_TREE, x, b->incomplete_list);
1999 /* Put decls on list in reverse order.
2000 We will reverse them later if necessary. */
2001 TREE_CHAIN (x) = b->names;
2002 b->names = x;
2004 return x;
2007 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
2009 tree
2010 pushdecl_top_level (x)
2011 tree x;
2013 tree t;
2014 struct binding_level *b = current_binding_level;
2016 current_binding_level = global_binding_level;
2017 t = pushdecl (x);
2018 current_binding_level = b;
2019 return t;
2022 /* Generate an implicit declaration for identifier FUNCTIONID
2023 as a function of type int (). Print a warning if appropriate. */
2025 tree
2026 implicitly_declare (functionid)
2027 tree functionid;
2029 tree decl;
2030 int traditional_warning = 0;
2031 /* Only one "implicit declaration" warning per identifier. */
2032 int implicit_warning;
2034 /* We used to reuse an old implicit decl here,
2035 but this loses with inline functions because it can clobber
2036 the saved decl chains. */
2037 #if 0
2038 if (IDENTIFIER_IMPLICIT_DECL (functionid) != 0)
2039 decl = IDENTIFIER_IMPLICIT_DECL (functionid);
2040 else
2041 #endif
2042 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2044 /* Warn of implicit decl following explicit local extern decl.
2045 This is probably a program designed for traditional C. */
2046 if (TREE_PUBLIC (functionid) && IDENTIFIER_GLOBAL_VALUE (functionid) == 0)
2047 traditional_warning = 1;
2049 /* Warn once of an implicit declaration. */
2050 implicit_warning = (IDENTIFIER_IMPLICIT_DECL (functionid) == 0);
2052 DECL_EXTERNAL (decl) = 1;
2053 TREE_PUBLIC (decl) = 1;
2055 /* Record that we have an implicit decl and this is it. */
2056 IDENTIFIER_IMPLICIT_DECL (functionid) = decl;
2058 /* ANSI standard says implicit declarations are in the innermost block.
2059 So we record the decl in the standard fashion. */
2060 pushdecl (decl);
2062 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
2063 maybe_objc_check_decl (decl);
2065 rest_of_decl_compilation (decl, NULL, 0, 0);
2067 if (implicit_warning)
2068 implicit_decl_warning (functionid);
2069 else if (warn_traditional && traditional_warning)
2070 warning ("function `%s' was previously declared within a block",
2071 IDENTIFIER_POINTER (functionid));
2073 /* Write a record describing this implicit function declaration to the
2074 prototypes file (if requested). */
2076 gen_aux_info_record (decl, 0, 1, 0);
2078 /* Possibly apply some default attributes to this implicit declaration. */
2079 decl_attributes (&decl, NULL_TREE, 0);
2081 return decl;
2084 void
2085 implicit_decl_warning (id)
2086 tree id;
2088 const char *name = IDENTIFIER_POINTER (id);
2089 if (mesg_implicit_function_declaration == 2)
2090 error ("implicit declaration of function `%s'", name);
2091 else if (mesg_implicit_function_declaration == 1)
2092 warning ("implicit declaration of function `%s'", name);
2095 /* Return zero if the declaration NEWDECL is valid
2096 when the declaration OLDDECL (assumed to be for the same name)
2097 has already been seen.
2098 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
2099 and 3 if it is a conflicting declaration. */
2101 static int
2102 redeclaration_error_message (newdecl, olddecl)
2103 tree newdecl, olddecl;
2105 if (TREE_CODE (newdecl) == TYPE_DECL)
2107 /* Do not complain about type redeclarations where at least one
2108 declaration was in a system header. */
2109 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
2110 return 0;
2111 return 1;
2113 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2115 /* Declarations of functions can insist on internal linkage
2116 but they can't be inconsistent with internal linkage,
2117 so there can be no error on that account.
2118 However defining the same name twice is no good. */
2119 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
2120 /* However, defining once as extern inline and a second
2121 time in another way is ok. */
2122 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
2123 && ! (DECL_DECLARED_INLINE_P (newdecl)
2124 && DECL_EXTERNAL (newdecl))))
2125 return 1;
2126 return 0;
2128 else if (DECL_CONTEXT (newdecl) == NULL_TREE)
2130 /* Objects declared at top level: */
2131 /* If at least one is a reference, it's ok. */
2132 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2133 return 0;
2134 /* Reject two definitions. */
2135 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
2136 return 1;
2137 /* Now we have two tentative defs, or one tentative and one real def. */
2138 /* Insist that the linkage match. */
2139 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
2140 return 3;
2141 return 0;
2143 else if (current_binding_level->parm_flag
2144 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2145 return 0;
2146 else
2148 /* Newdecl has block scope. If olddecl has block scope also, then
2149 reject two definitions, and reject a definition together with an
2150 external reference. Otherwise, it is OK, because newdecl must
2151 be an extern reference to olddecl. */
2152 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
2153 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
2154 return 2;
2155 return 0;
2159 /* Get the LABEL_DECL corresponding to identifier ID as a label.
2160 Create one if none exists so far for the current function.
2161 This function is called for both label definitions and label references. */
2163 tree
2164 lookup_label (id)
2165 tree id;
2167 tree decl = IDENTIFIER_LABEL_VALUE (id);
2169 if (current_function_decl == 0)
2171 error ("label %s referenced outside of any function",
2172 IDENTIFIER_POINTER (id));
2173 return 0;
2176 /* Use a label already defined or ref'd with this name. */
2177 if (decl != 0)
2179 /* But not if it is inherited and wasn't declared to be inheritable. */
2180 if (DECL_CONTEXT (decl) != current_function_decl
2181 && ! C_DECLARED_LABEL_FLAG (decl))
2182 return shadow_label (id);
2183 return decl;
2186 decl = build_decl (LABEL_DECL, id, void_type_node);
2188 /* A label not explicitly declared must be local to where it's ref'd. */
2189 DECL_CONTEXT (decl) = current_function_decl;
2191 DECL_MODE (decl) = VOIDmode;
2193 /* Say where one reference is to the label,
2194 for the sake of the error if it is not defined. */
2195 DECL_SOURCE_LINE (decl) = lineno;
2196 DECL_SOURCE_FILE (decl) = input_filename;
2198 IDENTIFIER_LABEL_VALUE (id) = decl;
2200 named_labels = tree_cons (NULL_TREE, decl, named_labels);
2202 return decl;
2205 /* Make a label named NAME in the current function,
2206 shadowing silently any that may be inherited from containing functions
2207 or containing scopes.
2209 Note that valid use, if the label being shadowed
2210 comes from another scope in the same function,
2211 requires calling declare_nonlocal_label right away. */
2213 tree
2214 shadow_label (name)
2215 tree name;
2217 tree decl = IDENTIFIER_LABEL_VALUE (name);
2219 if (decl != 0)
2221 tree dup;
2223 /* Check to make sure that the label hasn't already been declared
2224 at this label scope */
2225 for (dup = named_labels; dup; dup = TREE_CHAIN (dup))
2226 if (TREE_VALUE (dup) == decl)
2228 error ("duplicate label declaration `%s'",
2229 IDENTIFIER_POINTER (name));
2230 error_with_decl (TREE_VALUE (dup),
2231 "this is a previous declaration");
2232 /* Just use the previous declaration. */
2233 return lookup_label (name);
2236 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2237 IDENTIFIER_LABEL_VALUE (name) = decl = 0;
2240 return lookup_label (name);
2243 /* Define a label, specifying the location in the source file.
2244 Return the LABEL_DECL node for the label, if the definition is valid.
2245 Otherwise return 0. */
2247 tree
2248 define_label (filename, line, name)
2249 const char *filename;
2250 int line;
2251 tree name;
2253 tree decl = lookup_label (name);
2255 /* If label with this name is known from an outer context, shadow it. */
2256 if (decl != 0 && DECL_CONTEXT (decl) != current_function_decl)
2258 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2259 IDENTIFIER_LABEL_VALUE (name) = 0;
2260 decl = lookup_label (name);
2263 if (warn_traditional && !in_system_header && lookup_name (name))
2264 warning_with_file_and_line (filename, line,
2265 "traditional C lacks a separate namespace for labels, identifier `%s' conflicts",
2266 IDENTIFIER_POINTER (name));
2268 if (DECL_INITIAL (decl) != 0)
2270 error_with_file_and_line (filename, line, "duplicate label `%s'",
2271 IDENTIFIER_POINTER (name));
2272 return 0;
2274 else
2276 /* Mark label as having been defined. */
2277 DECL_INITIAL (decl) = error_mark_node;
2278 /* Say where in the source. */
2279 DECL_SOURCE_FILE (decl) = filename;
2280 DECL_SOURCE_LINE (decl) = line;
2281 return decl;
2285 /* Return the list of declarations of the current level.
2286 Note that this list is in reverse order unless/until
2287 you nreverse it; and when you do nreverse it, you must
2288 store the result back using `storedecls' or you will lose. */
2290 tree
2291 getdecls ()
2293 return current_binding_level->names;
2296 /* Return the list of type-tags (for structs, etc) of the current level. */
2298 tree
2299 gettags ()
2301 return current_binding_level->tags;
2304 /* Store the list of declarations of the current level.
2305 This is done for the parameter declarations of a function being defined,
2306 after they are modified in the light of any missing parameters. */
2308 static void
2309 storedecls (decls)
2310 tree decls;
2312 current_binding_level->names = decls;
2315 /* Similarly, store the list of tags of the current level. */
2317 static void
2318 storetags (tags)
2319 tree tags;
2321 current_binding_level->tags = tags;
2324 /* Given NAME, an IDENTIFIER_NODE,
2325 return the structure (or union or enum) definition for that name.
2326 Searches binding levels from BINDING_LEVEL up to the global level.
2327 If THISLEVEL_ONLY is nonzero, searches only the specified context
2328 (but skips any tag-transparent contexts to find one that is
2329 meaningful for tags).
2330 CODE says which kind of type the caller wants;
2331 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2332 If the wrong kind of type is found, an error is reported. */
2334 static tree
2335 lookup_tag (code, name, binding_level, thislevel_only)
2336 enum tree_code code;
2337 struct binding_level *binding_level;
2338 tree name;
2339 int thislevel_only;
2341 struct binding_level *level;
2342 int thislevel = 1;
2344 for (level = binding_level; level; level = level->level_chain)
2346 tree tail;
2347 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
2349 if (TREE_PURPOSE (tail) == name)
2351 if (TREE_CODE (TREE_VALUE (tail)) != code)
2353 /* Definition isn't the kind we were looking for. */
2354 pending_invalid_xref = name;
2355 pending_invalid_xref_file = input_filename;
2356 pending_invalid_xref_line = lineno;
2357 /* If in the same binding level as a declaration as a tag
2358 of a different type, this must not be allowed to
2359 shadow that tag, so give the error immediately.
2360 (For example, "struct foo; union foo;" is invalid.) */
2361 if (thislevel)
2362 pending_xref_error ();
2364 return TREE_VALUE (tail);
2367 if (! level->tag_transparent)
2369 if (thislevel_only)
2370 return NULL_TREE;
2371 thislevel = 0;
2374 return NULL_TREE;
2377 /* Print an error message now
2378 for a recent invalid struct, union or enum cross reference.
2379 We don't print them immediately because they are not invalid
2380 when used in the `struct foo;' construct for shadowing. */
2382 void
2383 pending_xref_error ()
2385 if (pending_invalid_xref != 0)
2386 error_with_file_and_line (pending_invalid_xref_file,
2387 pending_invalid_xref_line,
2388 "`%s' defined as wrong kind of tag",
2389 IDENTIFIER_POINTER (pending_invalid_xref));
2390 pending_invalid_xref = 0;
2393 /* Given a type, find the tag that was defined for it and return the tag name.
2394 Otherwise return 0. */
2396 static tree
2397 lookup_tag_reverse (type)
2398 tree type;
2400 struct binding_level *level;
2402 for (level = current_binding_level; level; level = level->level_chain)
2404 tree tail;
2405 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
2407 if (TREE_VALUE (tail) == type)
2408 return TREE_PURPOSE (tail);
2411 return NULL_TREE;
2414 /* Look up NAME in the current binding level and its superiors
2415 in the namespace of variables, functions and typedefs.
2416 Return a ..._DECL node of some kind representing its definition,
2417 or return 0 if it is undefined. */
2419 tree
2420 lookup_name (name)
2421 tree name;
2423 tree val;
2425 if (current_binding_level != global_binding_level
2426 && IDENTIFIER_LOCAL_VALUE (name))
2427 val = IDENTIFIER_LOCAL_VALUE (name);
2428 else
2429 val = IDENTIFIER_GLOBAL_VALUE (name);
2430 return val;
2433 /* Similar to `lookup_name' but look only at current binding level. */
2435 tree
2436 lookup_name_current_level (name)
2437 tree name;
2439 tree t;
2441 if (current_binding_level == global_binding_level)
2442 return IDENTIFIER_GLOBAL_VALUE (name);
2444 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
2445 return 0;
2447 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
2448 if (DECL_NAME (t) == name)
2449 break;
2451 return t;
2454 /* Create the predefined scalar types of C,
2455 and some nodes representing standard constants (0, 1, (void *) 0).
2456 Initialize the global binding level.
2457 Make definitions for built-in primitive functions. */
2459 void
2460 c_init_decl_processing ()
2462 tree endlink;
2463 tree ptr_ftype_void, ptr_ftype_ptr;
2465 /* Adds some ggc roots, and reserved words for c-parse.in. */
2466 c_parse_init ();
2468 current_function_decl = NULL;
2469 named_labels = NULL;
2470 current_binding_level = NULL_BINDING_LEVEL;
2471 free_binding_level = NULL_BINDING_LEVEL;
2473 /* Make the binding_level structure for global names. */
2474 pushlevel (0);
2475 global_binding_level = current_binding_level;
2477 build_common_tree_nodes (flag_signed_char);
2479 c_common_nodes_and_builtins ();
2481 boolean_type_node = integer_type_node;
2482 boolean_true_node = integer_one_node;
2483 boolean_false_node = integer_zero_node;
2485 c_bool_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
2486 TREE_SET_CODE (c_bool_type_node, BOOLEAN_TYPE);
2487 TYPE_MAX_VALUE (c_bool_type_node) = build_int_2 (1, 0);
2488 TREE_TYPE (TYPE_MAX_VALUE (c_bool_type_node)) = c_bool_type_node;
2489 TYPE_PRECISION (c_bool_type_node) = 1;
2490 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2491 c_bool_type_node));
2492 c_bool_false_node = build_int_2 (0, 0);
2493 TREE_TYPE (c_bool_false_node) = c_bool_type_node;
2494 c_bool_true_node = build_int_2 (1, 0);
2495 TREE_TYPE (c_bool_true_node) = c_bool_type_node;
2497 endlink = void_list_node;
2498 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2499 ptr_ftype_ptr
2500 = build_function_type (ptr_type_node,
2501 tree_cons (NULL_TREE, ptr_type_node, endlink));
2503 pedantic_lvalues = pedantic;
2505 make_fname_decl = c_make_fname_decl;
2506 start_fname_decls ();
2509 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2510 decl, NAME is the initialization string and TYPE_DEP indicates whether
2511 NAME depended on the type of the function. As we don't yet implement
2512 delayed emission of static data, we mark the decl as emitted
2513 so it is not placed in the output. Anything using it must therefore pull
2514 out the STRING_CST initializer directly. This does mean that these names
2515 are string merging candidates, which is wrong for C99's __func__. FIXME. */
2517 static tree
2518 c_make_fname_decl (id, type_dep)
2519 tree id;
2520 int type_dep;
2522 const char *name = fname_as_string (type_dep);
2523 tree decl, type, init;
2524 size_t length = strlen (name);
2526 type = build_array_type
2527 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2528 build_index_type (size_int (length)));
2530 decl = build_decl (VAR_DECL, id, type);
2531 /* We don't push the decl, so have to set its context here. */
2532 DECL_CONTEXT (decl) = current_function_decl;
2534 TREE_STATIC (decl) = 1;
2535 TREE_READONLY (decl) = 1;
2536 DECL_ARTIFICIAL (decl) = 1;
2538 init = build_string (length + 1, name);
2539 TREE_TYPE (init) = type;
2540 DECL_INITIAL (decl) = init;
2542 TREE_USED (decl) = 1;
2544 finish_decl (decl, init, NULL_TREE);
2546 return decl;
2549 /* Return a definition for a builtin function named NAME and whose data type
2550 is TYPE. TYPE should be a function type with argument types.
2551 FUNCTION_CODE tells later passes how to compile calls to this function.
2552 See tree.h for its possible values.
2554 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2555 the name to be called if we can't opencode the function. If
2556 ATTRS is nonzero, use that for the function's attribute list. */
2558 tree
2559 builtin_function (name, type, function_code, class, library_name, attrs)
2560 const char *name;
2561 tree type;
2562 int function_code;
2563 enum built_in_class class;
2564 const char *library_name;
2565 tree attrs;
2567 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2568 DECL_EXTERNAL (decl) = 1;
2569 TREE_PUBLIC (decl) = 1;
2570 if (library_name)
2571 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2572 make_decl_rtl (decl, NULL);
2573 pushdecl (decl);
2574 DECL_BUILT_IN_CLASS (decl) = class;
2575 DECL_FUNCTION_CODE (decl) = function_code;
2577 /* Warn if a function in the namespace for users
2578 is used without an occasion to consider it declared. */
2579 if (name[0] != '_' || name[1] != '_')
2580 C_DECL_ANTICIPATED (decl) = 1;
2582 /* Possibly apply some default attributes to this built-in function. */
2583 if (attrs)
2584 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2585 else
2586 decl_attributes (&decl, NULL_TREE, 0);
2588 return decl;
2591 /* Apply default attributes to a function, if a system function with default
2592 attributes. */
2594 void
2595 c_insert_default_attributes (decl)
2596 tree decl;
2598 if (!TREE_PUBLIC (decl))
2599 return;
2600 c_common_insert_default_attributes (decl);
2603 /* Called when a declaration is seen that contains no names to declare.
2604 If its type is a reference to a structure, union or enum inherited
2605 from a containing scope, shadow that tag name for the current scope
2606 with a forward reference.
2607 If its type defines a new named structure or union
2608 or defines an enum, it is valid but we need not do anything here.
2609 Otherwise, it is an error. */
2611 void
2612 shadow_tag (declspecs)
2613 tree declspecs;
2615 shadow_tag_warned (declspecs, 0);
2618 void
2619 shadow_tag_warned (declspecs, warned)
2620 tree declspecs;
2621 int warned;
2622 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2623 no pedwarn. */
2625 int found_tag = 0;
2626 tree link;
2627 tree specs, attrs;
2629 pending_invalid_xref = 0;
2631 /* Remove the attributes from declspecs, since they will confuse the
2632 following code. */
2633 split_specs_attrs (declspecs, &specs, &attrs);
2635 for (link = specs; link; link = TREE_CHAIN (link))
2637 tree value = TREE_VALUE (link);
2638 enum tree_code code = TREE_CODE (value);
2640 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2641 /* Used to test also that TYPE_SIZE (value) != 0.
2642 That caused warning for `struct foo;' at top level in the file. */
2644 tree name = lookup_tag_reverse (value);
2645 tree t;
2647 found_tag++;
2649 if (name == 0)
2651 if (warned != 1 && code != ENUMERAL_TYPE)
2652 /* Empty unnamed enum OK */
2654 pedwarn ("unnamed struct/union that defines no instances");
2655 warned = 1;
2658 else
2660 t = lookup_tag (code, name, current_binding_level, 1);
2662 if (t == 0)
2664 t = make_node (code);
2665 pushtag (name, t);
2669 else
2671 if (!warned && ! in_system_header)
2673 warning ("useless keyword or type name in empty declaration");
2674 warned = 2;
2679 if (found_tag > 1)
2680 error ("two types specified in one empty declaration");
2682 if (warned != 1)
2684 if (found_tag == 0)
2685 pedwarn ("empty declaration");
2689 /* Construct an array declarator. EXPR is the expression inside [], or
2690 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2691 to the pointer to which a parameter array is converted). STATIC_P is
2692 non-zero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2693 is non-zero is the array is [*], a VLA of unspecified length which is
2694 nevertheless a complete type (not currently implemented by GCC),
2695 zero otherwise. The declarator is constructed as an ARRAY_REF
2696 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2697 left of the [] (filled by in set_array_declarator_type) and operand 1
2698 is the expression inside; whose TREE_TYPE is the type qualifiers and
2699 which has TREE_STATIC set if "static" is used. */
2701 tree
2702 build_array_declarator (expr, quals, static_p, vla_unspec_p)
2703 tree expr;
2704 tree quals;
2705 int static_p;
2706 int vla_unspec_p;
2708 tree decl;
2709 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2710 TREE_TYPE (decl) = quals;
2711 TREE_STATIC (decl) = (static_p ? 1 : 0);
2712 if (pedantic && !flag_isoc99)
2714 if (static_p || quals != NULL_TREE)
2715 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2716 if (vla_unspec_p)
2717 pedwarn ("ISO C90 does not support `[*]' array declarators");
2719 if (vla_unspec_p)
2720 warning ("GCC does not yet properly implement `[*]' array declarators");
2721 return decl;
2724 /* Set the type of an array declarator. DECL is the declarator, as
2725 constructed by build_array_declarator; TYPE is what appears on the left
2726 of the [] and goes in operand 0. ABSTRACT_P is non-zero if it is an
2727 abstract declarator, zero otherwise; this is used to reject static and
2728 type qualifiers in abstract declarators, where they are not in the
2729 C99 grammar. */
2731 tree
2732 set_array_declarator_type (decl, type, abstract_p)
2733 tree decl;
2734 tree type;
2735 int abstract_p;
2737 TREE_OPERAND (decl, 0) = type;
2738 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2739 error ("static or type qualifiers in abstract declarator");
2740 return decl;
2743 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2745 tree
2746 groktypename (typename)
2747 tree typename;
2749 tree specs, attrs;
2751 if (TREE_CODE (typename) != TREE_LIST)
2752 return typename;
2754 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2756 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2758 /* Apply attributes. */
2759 decl_attributes (&typename, attrs, 0);
2761 return typename;
2764 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2766 tree
2767 groktypename_in_parm_context (typename)
2768 tree typename;
2770 if (TREE_CODE (typename) != TREE_LIST)
2771 return typename;
2772 return grokdeclarator (TREE_VALUE (typename),
2773 TREE_PURPOSE (typename),
2774 PARM, 0);
2777 /* Decode a declarator in an ordinary declaration or data definition.
2778 This is called as soon as the type information and variable name
2779 have been parsed, before parsing the initializer if any.
2780 Here we create the ..._DECL node, fill in its type,
2781 and put it on the list of decls for the current context.
2782 The ..._DECL node is returned as the value.
2784 Exception: for arrays where the length is not specified,
2785 the type is left null, to be filled in by `finish_decl'.
2787 Function definitions do not come here; they go to start_function
2788 instead. However, external and forward declarations of functions
2789 do go through here. Structure field declarations are done by
2790 grokfield and not through here. */
2792 tree
2793 start_decl (declarator, declspecs, initialized, attributes)
2794 tree declarator, declspecs;
2795 int initialized;
2796 tree attributes;
2798 tree decl;
2799 tree tem;
2801 /* An object declared as __attribute__((deprecated)) suppresses
2802 warnings of uses of other deprecated items. */
2803 if (lookup_attribute ("deprecated", attributes))
2804 deprecated_state = DEPRECATED_SUPPRESS;
2806 decl = grokdeclarator (declarator, declspecs,
2807 NORMAL, initialized);
2809 deprecated_state = DEPRECATED_NORMAL;
2811 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2812 && MAIN_NAME_P (DECL_NAME (decl)))
2813 warning_with_decl (decl, "`%s' is usually a function");
2815 if (initialized)
2816 /* Is it valid for this decl to have an initializer at all?
2817 If not, set INITIALIZED to zero, which will indirectly
2818 tell `finish_decl' to ignore the initializer once it is parsed. */
2819 switch (TREE_CODE (decl))
2821 case TYPE_DECL:
2822 /* typedef foo = bar means give foo the same type as bar.
2823 We haven't parsed bar yet, so `finish_decl' will fix that up.
2824 Any other case of an initialization in a TYPE_DECL is an error. */
2825 if (pedantic || list_length (declspecs) > 1)
2827 error ("typedef `%s' is initialized",
2828 IDENTIFIER_POINTER (DECL_NAME (decl)));
2829 initialized = 0;
2831 break;
2833 case FUNCTION_DECL:
2834 error ("function `%s' is initialized like a variable",
2835 IDENTIFIER_POINTER (DECL_NAME (decl)));
2836 initialized = 0;
2837 break;
2839 case PARM_DECL:
2840 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2841 error ("parameter `%s' is initialized",
2842 IDENTIFIER_POINTER (DECL_NAME (decl)));
2843 initialized = 0;
2844 break;
2846 default:
2847 /* Don't allow initializations for incomplete types
2848 except for arrays which might be completed by the initialization. */
2850 /* This can happen if the array size is an undefined macro. We already
2851 gave a warning, so we don't need another one. */
2852 if (TREE_TYPE (decl) == error_mark_node)
2853 initialized = 0;
2854 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2856 /* A complete type is ok if size is fixed. */
2858 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2859 || C_DECL_VARIABLE_SIZE (decl))
2861 error ("variable-sized object may not be initialized");
2862 initialized = 0;
2865 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2867 error ("variable `%s' has initializer but incomplete type",
2868 IDENTIFIER_POINTER (DECL_NAME (decl)));
2869 initialized = 0;
2871 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2873 error ("elements of array `%s' have incomplete type",
2874 IDENTIFIER_POINTER (DECL_NAME (decl)));
2875 initialized = 0;
2879 if (initialized)
2881 #if 0
2882 /* Seems redundant with grokdeclarator. */
2883 if (current_binding_level != global_binding_level
2884 && DECL_EXTERNAL (decl)
2885 && TREE_CODE (decl) != FUNCTION_DECL)
2886 warning ("declaration of `%s' has `extern' and is initialized",
2887 IDENTIFIER_POINTER (DECL_NAME (decl)));
2888 #endif
2889 DECL_EXTERNAL (decl) = 0;
2890 if (current_binding_level == global_binding_level)
2891 TREE_STATIC (decl) = 1;
2893 /* Tell `pushdecl' this is an initialized decl
2894 even though we don't yet have the initializer expression.
2895 Also tell `finish_decl' it may store the real initializer. */
2896 DECL_INITIAL (decl) = error_mark_node;
2899 /* If this is a function declaration, write a record describing it to the
2900 prototypes file (if requested). */
2902 if (TREE_CODE (decl) == FUNCTION_DECL)
2903 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2905 /* ANSI specifies that a tentative definition which is not merged with
2906 a non-tentative definition behaves exactly like a definition with an
2907 initializer equal to zero. (Section 3.7.2)
2909 -fno-common gives strict ANSI behavior, though this tends to break
2910 a large body of code that grew up without this rule.
2912 Thread-local variables are never common, since there's no entrenched
2913 body of code to break, and it allows more efficient variable references
2914 in the presense of dynamic linking. */
2916 if (TREE_CODE (decl) == VAR_DECL
2917 && !initialized
2918 && TREE_PUBLIC (decl)
2919 && !DECL_THREAD_LOCAL (decl)
2920 && !flag_no_common)
2921 DECL_COMMON (decl) = 1;
2923 /* Set attributes here so if duplicate decl, will have proper attributes. */
2924 decl_attributes (&decl, attributes, 0);
2926 /* If #pragma weak was used, mark the decl weak now. */
2927 if (current_binding_level == global_binding_level)
2928 maybe_apply_pragma_weak (decl);
2930 if (TREE_CODE (decl) == FUNCTION_DECL
2931 && DECL_DECLARED_INLINE_P (decl)
2932 && DECL_UNINLINABLE (decl)
2933 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2934 warning_with_decl (decl,
2935 "inline function `%s' given attribute noinline");
2937 /* Add this decl to the current binding level.
2938 TEM may equal DECL or it may be a previous decl of the same name. */
2939 tem = pushdecl (decl);
2941 /* For a local variable, define the RTL now. */
2942 if (current_binding_level != global_binding_level
2943 /* But not if this is a duplicate decl
2944 and we preserved the rtl from the previous one
2945 (which may or may not happen). */
2946 && !DECL_RTL_SET_P (tem)
2947 && !DECL_CONTEXT (tem))
2949 if (TREE_TYPE (tem) != error_mark_node
2950 && COMPLETE_TYPE_P (TREE_TYPE (tem)))
2951 expand_decl (tem);
2952 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2953 && DECL_INITIAL (tem) != 0)
2954 expand_decl (tem);
2957 return tem;
2960 /* Finish processing of a declaration;
2961 install its initial value.
2962 If the length of an array type is not known before,
2963 it must be determined now, from the initial value, or it is an error. */
2965 void
2966 finish_decl (decl, init, asmspec_tree)
2967 tree decl, init;
2968 tree asmspec_tree;
2970 tree type = TREE_TYPE (decl);
2971 int was_incomplete = (DECL_SIZE (decl) == 0);
2972 const char *asmspec = 0;
2974 /* If a name was specified, get the string. */
2975 if (current_binding_level == global_binding_level)
2976 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2977 if (asmspec_tree)
2978 asmspec = TREE_STRING_POINTER (asmspec_tree);
2980 /* If `start_decl' didn't like having an initialization, ignore it now. */
2981 if (init != 0 && DECL_INITIAL (decl) == 0)
2982 init = 0;
2984 /* Don't crash if parm is initialized. */
2985 if (TREE_CODE (decl) == PARM_DECL)
2986 init = 0;
2988 if (init)
2990 if (TREE_CODE (decl) != TYPE_DECL)
2991 store_init_value (decl, init);
2992 else
2994 /* typedef foo = bar; store the type of bar as the type of foo. */
2995 TREE_TYPE (decl) = TREE_TYPE (init);
2996 DECL_INITIAL (decl) = init = 0;
3000 /* Deduce size of array from initialization, if not already known */
3001 if (TREE_CODE (type) == ARRAY_TYPE
3002 && TYPE_DOMAIN (type) == 0
3003 && TREE_CODE (decl) != TYPE_DECL)
3005 int do_default
3006 = (TREE_STATIC (decl)
3007 /* Even if pedantic, an external linkage array
3008 may have incomplete type at first. */
3009 ? pedantic && !TREE_PUBLIC (decl)
3010 : !DECL_EXTERNAL (decl));
3011 int failure
3012 = complete_array_type (type, DECL_INITIAL (decl), do_default);
3014 /* Get the completed type made by complete_array_type. */
3015 type = TREE_TYPE (decl);
3017 if (failure == 1)
3018 error_with_decl (decl, "initializer fails to determine size of `%s'");
3020 else if (failure == 2)
3022 if (do_default)
3023 error_with_decl (decl, "array size missing in `%s'");
3024 /* If a `static' var's size isn't known,
3025 make it extern as well as static, so it does not get
3026 allocated.
3027 If it is not `static', then do not mark extern;
3028 finish_incomplete_decl will give it a default size
3029 and it will get allocated. */
3030 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
3031 DECL_EXTERNAL (decl) = 1;
3034 /* TYPE_MAX_VALUE is always one less than the number of elements
3035 in the array, because we start counting at zero. Therefore,
3036 warn only if the value is less than zero. */
3037 else if (pedantic && TYPE_DOMAIN (type) != 0
3038 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
3039 error_with_decl (decl, "zero or negative size array `%s'");
3041 layout_decl (decl, 0);
3044 if (TREE_CODE (decl) == VAR_DECL)
3046 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3047 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3048 layout_decl (decl, 0);
3050 if (DECL_SIZE (decl) == 0
3051 /* Don't give an error if we already gave one earlier. */
3052 && TREE_TYPE (decl) != error_mark_node
3053 && (TREE_STATIC (decl)
3055 /* A static variable with an incomplete type
3056 is an error if it is initialized.
3057 Also if it is not file scope.
3058 Otherwise, let it through, but if it is not `extern'
3059 then it may cause an error message later. */
3060 (DECL_INITIAL (decl) != 0
3061 || DECL_CONTEXT (decl) != 0)
3063 /* An automatic variable with an incomplete type
3064 is an error. */
3065 !DECL_EXTERNAL (decl)))
3067 error_with_decl (decl, "storage size of `%s' isn't known");
3068 TREE_TYPE (decl) = error_mark_node;
3071 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3072 && DECL_SIZE (decl) != 0)
3074 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3075 constant_expression_warning (DECL_SIZE (decl));
3076 else
3077 error_with_decl (decl, "storage size of `%s' isn't constant");
3080 if (TREE_USED (type))
3081 TREE_USED (decl) = 1;
3084 /* If this is a function and an assembler name is specified, it isn't
3085 builtin any more. Also reset DECL_RTL so we can give it its new
3086 name. */
3087 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3089 DECL_BUILT_IN_CLASS (decl) = NOT_BUILT_IN;
3090 SET_DECL_RTL (decl, NULL_RTX);
3091 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
3094 /* Output the assembler code and/or RTL code for variables and functions,
3095 unless the type is an undefined structure or union.
3096 If not, it will get done when the type is completed. */
3098 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3100 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3101 maybe_objc_check_decl (decl);
3103 if (!DECL_CONTEXT (decl))
3105 if (DECL_INITIAL (decl) == NULL_TREE
3106 || DECL_INITIAL (decl) == error_mark_node)
3107 /* Don't output anything
3108 when a tentative file-scope definition is seen.
3109 But at end of compilation, do output code for them. */
3110 DECL_DEFER_OUTPUT (decl) = 1;
3111 rest_of_decl_compilation (decl, asmspec,
3112 (DECL_CONTEXT (decl) == 0
3113 || TREE_ASM_WRITTEN (decl)), 0);
3115 else
3117 /* This is a local variable. If there is an ASMSPEC, the
3118 user has requested that we handle it specially. */
3119 if (asmspec)
3121 /* In conjunction with an ASMSPEC, the `register'
3122 keyword indicates that we should place the variable
3123 in a particular register. */
3124 if (DECL_REGISTER (decl))
3125 DECL_C_HARD_REGISTER (decl) = 1;
3127 /* If this is not a static variable, issue a warning.
3128 It doesn't make any sense to give an ASMSPEC for an
3129 ordinary, non-register local variable. Historically,
3130 GCC has accepted -- but ignored -- the ASMSPEC in
3131 this case. */
3132 if (TREE_CODE (decl) == VAR_DECL
3133 && !DECL_REGISTER (decl)
3134 && !TREE_STATIC (decl))
3135 warning_with_decl (decl,
3136 "ignoring asm-specifier for non-static local variable `%s'");
3137 else
3138 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
3141 if (TREE_CODE (decl) != FUNCTION_DECL)
3142 add_decl_stmt (decl);
3145 if (DECL_CONTEXT (decl) != 0)
3147 /* Recompute the RTL of a local array now
3148 if it used to be an incomplete type. */
3149 if (was_incomplete
3150 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
3152 /* If we used it already as memory, it must stay in memory. */
3153 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3154 /* If it's still incomplete now, no init will save it. */
3155 if (DECL_SIZE (decl) == 0)
3156 DECL_INITIAL (decl) = 0;
3161 if (TREE_CODE (decl) == TYPE_DECL)
3163 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3164 maybe_objc_check_decl (decl);
3165 rest_of_decl_compilation (decl, NULL, DECL_CONTEXT (decl) == 0, 0);
3168 /* At the end of a declaration, throw away any variable type sizes
3169 of types defined inside that declaration. There is no use
3170 computing them in the following function definition. */
3171 if (current_binding_level == global_binding_level)
3172 get_pending_sizes ();
3175 /* Given a parsed parameter declaration,
3176 decode it into a PARM_DECL and push that on the current binding level.
3177 Also, for the sake of forward parm decls,
3178 record the given order of parms in `parm_order'. */
3180 void
3181 push_parm_decl (parm)
3182 tree parm;
3184 tree decl;
3185 int old_immediate_size_expand = immediate_size_expand;
3186 /* Don't try computing parm sizes now -- wait till fn is called. */
3187 immediate_size_expand = 0;
3189 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3190 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
3191 decl_attributes (&decl, TREE_VALUE (parm), 0);
3193 #if 0
3194 if (DECL_NAME (decl))
3196 tree olddecl;
3197 olddecl = lookup_name (DECL_NAME (decl));
3198 if (pedantic && olddecl != 0 && TREE_CODE (olddecl) == TYPE_DECL)
3199 pedwarn_with_decl (decl,
3200 "ISO C forbids parameter `%s' shadowing typedef");
3202 #endif
3204 decl = pushdecl (decl);
3206 immediate_size_expand = old_immediate_size_expand;
3208 current_binding_level->parm_order
3209 = tree_cons (NULL_TREE, decl, current_binding_level->parm_order);
3211 /* Add this decl to the current binding level. */
3212 finish_decl (decl, NULL_TREE, NULL_TREE);
3215 /* Clear the given order of parms in `parm_order'.
3216 Used at start of parm list,
3217 and also at semicolon terminating forward decls. */
3219 void
3220 clear_parm_order ()
3222 current_binding_level->parm_order = NULL_TREE;
3225 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3226 literal, which may be an incomplete array type completed by the
3227 initializer; INIT is a CONSTRUCTOR that initializes the compound
3228 literal. */
3230 tree
3231 build_compound_literal (type, init)
3232 tree type;
3233 tree init;
3235 /* We do not use start_decl here because we have a type, not a declarator;
3236 and do not use finish_decl because the decl should be stored inside
3237 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3238 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3239 tree complit;
3240 tree stmt;
3241 DECL_EXTERNAL (decl) = 0;
3242 TREE_PUBLIC (decl) = 0;
3243 TREE_STATIC (decl) = (current_binding_level == global_binding_level);
3244 DECL_CONTEXT (decl) = current_function_decl;
3245 TREE_USED (decl) = 1;
3246 TREE_TYPE (decl) = type;
3247 TREE_READONLY (decl) = TREE_READONLY (type);
3248 store_init_value (decl, init);
3250 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3252 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3253 if (failure)
3254 abort ();
3257 type = TREE_TYPE (decl);
3258 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3259 return error_mark_node;
3261 stmt = build_stmt (DECL_STMT, decl);
3262 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3263 TREE_SIDE_EFFECTS (complit) = 1;
3265 layout_decl (decl, 0);
3267 if (TREE_STATIC (decl))
3269 /* This decl needs a name for the assembler output. We also need
3270 a unique suffix to be added to the name. */
3271 char *name;
3272 extern int var_labelno;
3274 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal", var_labelno);
3275 var_labelno++;
3276 DECL_NAME (decl) = get_identifier (name);
3277 DECL_DEFER_OUTPUT (decl) = 1;
3278 DECL_COMDAT (decl) = 1;
3279 DECL_ARTIFICIAL (decl) = 1;
3280 pushdecl (decl);
3281 rest_of_decl_compilation (decl, NULL, 1, 0);
3284 return complit;
3287 /* Make TYPE a complete type based on INITIAL_VALUE.
3288 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3289 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3292 complete_array_type (type, initial_value, do_default)
3293 tree type;
3294 tree initial_value;
3295 int do_default;
3297 tree maxindex = NULL_TREE;
3298 int value = 0;
3300 if (initial_value)
3302 /* Note MAXINDEX is really the maximum index,
3303 one less than the size. */
3304 if (TREE_CODE (initial_value) == STRING_CST)
3306 int eltsize
3307 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3308 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3309 / eltsize) - 1, 0);
3311 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3313 tree elts = CONSTRUCTOR_ELTS (initial_value);
3314 maxindex = build_int_2 (-1, -1);
3315 for (; elts; elts = TREE_CHAIN (elts))
3317 if (TREE_PURPOSE (elts))
3318 maxindex = TREE_PURPOSE (elts);
3319 else
3320 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3321 maxindex, integer_one_node));
3323 maxindex = copy_node (maxindex);
3325 else
3327 /* Make an error message unless that happened already. */
3328 if (initial_value != error_mark_node)
3329 value = 1;
3331 /* Prevent further error messages. */
3332 maxindex = build_int_2 (0, 0);
3336 if (!maxindex)
3338 if (do_default)
3339 maxindex = build_int_2 (0, 0);
3340 value = 2;
3343 if (maxindex)
3345 TYPE_DOMAIN (type) = build_index_type (maxindex);
3346 if (!TREE_TYPE (maxindex))
3347 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3350 /* Lay out the type now that we can get the real answer. */
3352 layout_type (type);
3354 return value;
3357 /* Given declspecs and a declarator,
3358 determine the name and type of the object declared
3359 and construct a ..._DECL node for it.
3360 (In one case we can return a ..._TYPE node instead.
3361 For invalid input we sometimes return 0.)
3363 DECLSPECS is a chain of tree_list nodes whose value fields
3364 are the storage classes and type specifiers.
3366 DECL_CONTEXT says which syntactic context this declaration is in:
3367 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3368 FUNCDEF for a function definition. Like NORMAL but a few different
3369 error messages in each case. Return value may be zero meaning
3370 this definition is too screwy to try to parse.
3371 PARM for a parameter declaration (either within a function prototype
3372 or before a function body). Make a PARM_DECL, or return void_type_node.
3373 TYPENAME if for a typename (in a cast or sizeof).
3374 Don't make a DECL node; just return the ..._TYPE node.
3375 FIELD for a struct or union field; make a FIELD_DECL.
3376 BITFIELD for a field with specified width.
3377 INITIALIZED is 1 if the decl has an initializer.
3379 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3380 It may also be so in the PARM case, for a prototype where the
3381 argument type is specified but not the name.
3383 This function is where the complicated C meanings of `static'
3384 and `extern' are interpreted. */
3386 static tree
3387 grokdeclarator (declarator, declspecs, decl_context, initialized)
3388 tree declspecs;
3389 tree declarator;
3390 enum decl_context decl_context;
3391 int initialized;
3393 int specbits = 0;
3394 tree spec;
3395 tree type = NULL_TREE;
3396 int longlong = 0;
3397 int constp;
3398 int restrictp;
3399 int volatilep;
3400 int type_quals = TYPE_UNQUALIFIED;
3401 int inlinep;
3402 int explicit_int = 0;
3403 int explicit_char = 0;
3404 int defaulted_int = 0;
3405 tree typedef_decl = 0;
3406 const char *name;
3407 tree typedef_type = 0;
3408 int funcdef_flag = 0;
3409 enum tree_code innermost_code = ERROR_MARK;
3410 int bitfield = 0;
3411 int size_varies = 0;
3412 tree decl_attr = NULL_TREE;
3413 tree array_ptr_quals = NULL_TREE;
3414 int array_parm_static = 0;
3415 tree returned_attrs = NULL_TREE;
3417 if (decl_context == BITFIELD)
3418 bitfield = 1, decl_context = FIELD;
3420 if (decl_context == FUNCDEF)
3421 funcdef_flag = 1, decl_context = NORMAL;
3423 /* Look inside a declarator for the name being declared
3424 and get it as a string, for an error message. */
3426 tree decl = declarator;
3427 name = 0;
3429 while (decl)
3430 switch (TREE_CODE (decl))
3432 case ARRAY_REF:
3433 case INDIRECT_REF:
3434 case CALL_EXPR:
3435 innermost_code = TREE_CODE (decl);
3436 decl = TREE_OPERAND (decl, 0);
3437 break;
3439 case TREE_LIST:
3440 decl = TREE_VALUE (decl);
3441 break;
3443 case IDENTIFIER_NODE:
3444 name = IDENTIFIER_POINTER (decl);
3445 decl = 0;
3446 break;
3448 default:
3449 abort ();
3451 if (name == 0)
3452 name = "type name";
3455 /* A function definition's declarator must have the form of
3456 a function declarator. */
3458 if (funcdef_flag && innermost_code != CALL_EXPR)
3459 return 0;
3461 /* Anything declared one level down from the top level
3462 must be one of the parameters of a function
3463 (because the body is at least two levels down). */
3465 /* If this looks like a function definition, make it one,
3466 even if it occurs where parms are expected.
3467 Then store_parm_decls will reject it and not use it as a parm. */
3468 if (decl_context == NORMAL && !funcdef_flag
3469 && current_binding_level->parm_flag)
3470 decl_context = PARM;
3472 /* Look through the decl specs and record which ones appear.
3473 Some typespecs are defined as built-in typenames.
3474 Others, the ones that are modifiers of other types,
3475 are represented by bits in SPECBITS: set the bits for
3476 the modifiers that appear. Storage class keywords are also in SPECBITS.
3478 If there is a typedef name or a type, store the type in TYPE.
3479 This includes builtin typedefs such as `int'.
3481 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3482 and did not come from a user typedef.
3484 Set LONGLONG if `long' is mentioned twice. */
3486 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3488 tree id = TREE_VALUE (spec);
3490 /* If the entire declaration is itself tagged as deprecated then
3491 suppress reports of deprecated items. */
3492 if (id && TREE_DEPRECATED (id))
3494 if (deprecated_state != DEPRECATED_SUPPRESS)
3495 warn_deprecated_use (id);
3498 if (id == ridpointers[(int) RID_INT])
3499 explicit_int = 1;
3500 if (id == ridpointers[(int) RID_CHAR])
3501 explicit_char = 1;
3503 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3505 enum rid i = C_RID_CODE (id);
3506 if ((int) i <= (int) RID_LAST_MODIFIER)
3508 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3510 if (longlong)
3511 error ("`long long long' is too long for GCC");
3512 else
3514 if (pedantic && !flag_isoc99 && ! in_system_header
3515 && warn_long_long)
3516 pedwarn ("ISO C90 does not support `long long'");
3517 longlong = 1;
3520 else if (specbits & (1 << (int) i))
3521 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3523 /* Diagnose "__thread extern". Recall that this list
3524 is in the reverse order seen in the text. */
3525 if (i == RID_THREAD
3526 && (specbits & (1 << (int) RID_EXTERN
3527 | 1 << (int) RID_STATIC)))
3529 if (specbits & 1 << (int) RID_EXTERN)
3530 error ("`__thread' before `extern'");
3531 else
3532 error ("`__thread' before `static'");
3535 specbits |= 1 << (int) i;
3536 goto found;
3539 if (type)
3540 error ("two or more data types in declaration of `%s'", name);
3541 /* Actual typedefs come to us as TYPE_DECL nodes. */
3542 else if (TREE_CODE (id) == TYPE_DECL)
3544 if (TREE_TYPE (id) == error_mark_node)
3545 ; /* Allow the type to default to int to avoid cascading errors. */
3546 else
3548 type = TREE_TYPE (id);
3549 decl_attr = DECL_ATTRIBUTES (id);
3550 typedef_decl = id;
3553 /* Built-in types come as identifiers. */
3554 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3556 tree t = lookup_name (id);
3557 if (TREE_TYPE (t) == error_mark_node)
3559 else if (!t || TREE_CODE (t) != TYPE_DECL)
3560 error ("`%s' fails to be a typedef or built in type",
3561 IDENTIFIER_POINTER (id));
3562 else
3564 type = TREE_TYPE (t);
3565 typedef_decl = t;
3568 else if (TREE_CODE (id) != ERROR_MARK)
3569 type = id;
3571 found:
3575 typedef_type = type;
3576 if (type)
3577 size_varies = C_TYPE_VARIABLE_SIZE (type);
3579 /* No type at all: default to `int', and set DEFAULTED_INT
3580 because it was not a user-defined typedef. */
3582 if (type == 0)
3584 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3585 | (1 << (int) RID_SIGNED)
3586 | (1 << (int) RID_UNSIGNED)
3587 | (1 << (int) RID_COMPLEX))))
3588 /* Don't warn about typedef foo = bar. */
3589 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3590 && ! in_system_header)
3592 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3593 and this is a function, or if -Wimplicit; prefer the former
3594 warning since it is more explicit. */
3595 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3596 && funcdef_flag)
3597 warn_about_return_type = 1;
3598 else if (warn_implicit_int || flag_isoc99)
3599 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3600 name);
3603 defaulted_int = 1;
3604 type = integer_type_node;
3607 /* Now process the modifiers that were specified
3608 and check for invalid combinations. */
3610 /* Long double is a special combination. */
3612 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3613 && TYPE_MAIN_VARIANT (type) == double_type_node)
3615 specbits &= ~(1 << (int) RID_LONG);
3616 type = long_double_type_node;
3619 /* Check all other uses of type modifiers. */
3621 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3622 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3624 int ok = 0;
3626 if ((specbits & 1 << (int) RID_LONG)
3627 && (specbits & 1 << (int) RID_SHORT))
3628 error ("both long and short specified for `%s'", name);
3629 else if (((specbits & 1 << (int) RID_LONG)
3630 || (specbits & 1 << (int) RID_SHORT))
3631 && explicit_char)
3632 error ("long or short specified with char for `%s'", name);
3633 else if (((specbits & 1 << (int) RID_LONG)
3634 || (specbits & 1 << (int) RID_SHORT))
3635 && TREE_CODE (type) == REAL_TYPE)
3637 static int already = 0;
3639 error ("long or short specified with floating type for `%s'", name);
3640 if (! already && ! pedantic)
3642 error ("the only valid combination is `long double'");
3643 already = 1;
3646 else if ((specbits & 1 << (int) RID_SIGNED)
3647 && (specbits & 1 << (int) RID_UNSIGNED))
3648 error ("both signed and unsigned specified for `%s'", name);
3649 else if (TREE_CODE (type) != INTEGER_TYPE)
3650 error ("long, short, signed or unsigned invalid for `%s'", name);
3651 else
3653 ok = 1;
3654 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
3656 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
3657 name);
3658 if (flag_pedantic_errors)
3659 ok = 0;
3663 /* Discard the type modifiers if they are invalid. */
3664 if (! ok)
3666 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3667 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3668 longlong = 0;
3672 if ((specbits & (1 << (int) RID_COMPLEX))
3673 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3675 error ("complex invalid for `%s'", name);
3676 specbits &= ~(1 << (int) RID_COMPLEX);
3679 /* Decide whether an integer type is signed or not.
3680 Optionally treat bitfields as signed by default. */
3681 if (specbits & 1 << (int) RID_UNSIGNED
3682 || (bitfield && ! flag_signed_bitfields
3683 && (explicit_int || defaulted_int || explicit_char
3684 /* A typedef for plain `int' without `signed'
3685 can be controlled just like plain `int'. */
3686 || ! (typedef_decl != 0
3687 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3688 && TREE_CODE (type) != ENUMERAL_TYPE
3689 && !(specbits & 1 << (int) RID_SIGNED)))
3691 if (longlong)
3692 type = long_long_unsigned_type_node;
3693 else if (specbits & 1 << (int) RID_LONG)
3694 type = long_unsigned_type_node;
3695 else if (specbits & 1 << (int) RID_SHORT)
3696 type = short_unsigned_type_node;
3697 else if (type == char_type_node)
3698 type = unsigned_char_type_node;
3699 else if (typedef_decl)
3700 type = c_common_unsigned_type (type);
3701 else
3702 type = unsigned_type_node;
3704 else if ((specbits & 1 << (int) RID_SIGNED)
3705 && type == char_type_node)
3706 type = signed_char_type_node;
3707 else if (longlong)
3708 type = long_long_integer_type_node;
3709 else if (specbits & 1 << (int) RID_LONG)
3710 type = long_integer_type_node;
3711 else if (specbits & 1 << (int) RID_SHORT)
3712 type = short_integer_type_node;
3714 if (specbits & 1 << (int) RID_COMPLEX)
3716 if (pedantic && !flag_isoc99)
3717 pedwarn ("ISO C90 does not support complex types");
3718 /* If we just have "complex", it is equivalent to
3719 "complex double", but if any modifiers at all are specified it is
3720 the complex form of TYPE. E.g, "complex short" is
3721 "complex short int". */
3723 if (defaulted_int && ! longlong
3724 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3725 | (1 << (int) RID_SIGNED)
3726 | (1 << (int) RID_UNSIGNED))))
3728 if (pedantic)
3729 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3730 type = complex_double_type_node;
3732 else if (type == integer_type_node)
3734 if (pedantic)
3735 pedwarn ("ISO C does not support complex integer types");
3736 type = complex_integer_type_node;
3738 else if (type == float_type_node)
3739 type = complex_float_type_node;
3740 else if (type == double_type_node)
3741 type = complex_double_type_node;
3742 else if (type == long_double_type_node)
3743 type = complex_long_double_type_node;
3744 else
3746 if (pedantic)
3747 pedwarn ("ISO C does not support complex integer types");
3748 type = build_complex_type (type);
3752 /* Figure out the type qualifiers for the declaration. There are
3753 two ways a declaration can become qualified. One is something
3754 like `const int i' where the `const' is explicit. Another is
3755 something like `typedef const int CI; CI i' where the type of the
3756 declaration contains the `const'. */
3757 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3758 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3759 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3760 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3761 if (constp > 1 && ! flag_isoc99)
3762 pedwarn ("duplicate `const'");
3763 if (restrictp > 1 && ! flag_isoc99)
3764 pedwarn ("duplicate `restrict'");
3765 if (volatilep > 1 && ! flag_isoc99)
3766 pedwarn ("duplicate `volatile'");
3767 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3768 type = TYPE_MAIN_VARIANT (type);
3769 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3770 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3771 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3773 /* Warn if two storage classes are given. Default to `auto'. */
3776 int nclasses = 0;
3778 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3779 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3780 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3781 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3782 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3784 /* "static __thread" and "extern __thread" are allowed. */
3785 if ((specbits & (1 << (int) RID_THREAD
3786 | 1 << (int) RID_STATIC
3787 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3788 nclasses++;
3790 /* Warn about storage classes that are invalid for certain
3791 kinds of declarations (parameters, typenames, etc.). */
3793 if (nclasses > 1)
3794 error ("multiple storage classes in declaration of `%s'", name);
3795 else if (funcdef_flag
3796 && (specbits
3797 & ((1 << (int) RID_REGISTER)
3798 | (1 << (int) RID_AUTO)
3799 | (1 << (int) RID_TYPEDEF)
3800 | (1 << (int) RID_THREAD))))
3802 if (specbits & 1 << (int) RID_AUTO
3803 && (pedantic || current_binding_level == global_binding_level))
3804 pedwarn ("function definition declared `auto'");
3805 if (specbits & 1 << (int) RID_REGISTER)
3806 error ("function definition declared `register'");
3807 if (specbits & 1 << (int) RID_TYPEDEF)
3808 error ("function definition declared `typedef'");
3809 if (specbits & 1 << (int) RID_THREAD)
3810 error ("function definition declared `__thread'");
3811 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3812 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3814 else if (decl_context != NORMAL && nclasses > 0)
3816 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3818 else
3820 switch (decl_context)
3822 case FIELD:
3823 error ("storage class specified for structure field `%s'",
3824 name);
3825 break;
3826 case PARM:
3827 error ("storage class specified for parameter `%s'", name);
3828 break;
3829 default:
3830 error ("storage class specified for typename");
3831 break;
3833 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3834 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3835 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3838 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3840 /* `extern' with initialization is invalid if not at top level. */
3841 if (current_binding_level == global_binding_level)
3842 warning ("`%s' initialized and declared `extern'", name);
3843 else
3844 error ("`%s' has both `extern' and initializer", name);
3846 else if (current_binding_level == global_binding_level)
3848 if (specbits & 1 << (int) RID_AUTO)
3849 error ("top-level declaration of `%s' specifies `auto'", name);
3851 else
3853 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3854 error ("nested function `%s' declared `extern'", name);
3855 else if ((specbits & (1 << (int) RID_THREAD
3856 | 1 << (int) RID_EXTERN
3857 | 1 << (int) RID_STATIC))
3858 == (1 << (int) RID_THREAD))
3860 error ("function-scope `%s' implicitly auto and declared `__thread'",
3861 name);
3862 specbits &= ~(1 << (int) RID_THREAD);
3867 /* Now figure out the structure of the declarator proper.
3868 Descend through it, creating more complex types, until we reach
3869 the declared identifier (or NULL_TREE, in an absolute declarator). */
3871 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3873 if (type == error_mark_node)
3875 declarator = TREE_OPERAND (declarator, 0);
3876 continue;
3879 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3880 an INDIRECT_REF (for *...),
3881 a CALL_EXPR (for ...(...)),
3882 a TREE_LIST (for nested attributes),
3883 an identifier (for the name being declared)
3884 or a null pointer (for the place in an absolute declarator
3885 where the name was omitted).
3886 For the last two cases, we have just exited the loop.
3888 At this point, TYPE is the type of elements of an array,
3889 or for a function to return, or for a pointer to point to.
3890 After this sequence of ifs, TYPE is the type of the
3891 array or function or pointer, and DECLARATOR has had its
3892 outermost layer removed. */
3894 if (array_ptr_quals != NULL_TREE || array_parm_static)
3896 /* Only the innermost declarator (making a parameter be of
3897 array type which is converted to pointer type)
3898 may have static or type qualifiers. */
3899 error ("static or type qualifiers in non-parameter array declarator");
3900 array_ptr_quals = NULL_TREE;
3901 array_parm_static = 0;
3904 if (TREE_CODE (declarator) == TREE_LIST)
3906 /* We encode a declarator with embedded attributes using
3907 a TREE_LIST. */
3908 tree attrs = TREE_PURPOSE (declarator);
3909 tree inner_decl;
3910 int attr_flags = 0;
3911 declarator = TREE_VALUE (declarator);
3912 inner_decl = declarator;
3913 while (inner_decl != NULL_TREE
3914 && TREE_CODE (inner_decl) == TREE_LIST)
3915 inner_decl = TREE_VALUE (inner_decl);
3916 if (inner_decl == NULL_TREE
3917 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3918 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3919 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3920 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3921 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3922 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3923 returned_attrs = decl_attributes (&type,
3924 chainon (returned_attrs, attrs),
3925 attr_flags);
3927 else if (TREE_CODE (declarator) == ARRAY_REF)
3929 tree itype = NULL_TREE;
3930 tree size = TREE_OPERAND (declarator, 1);
3931 /* The index is a signed object `sizetype' bits wide. */
3932 tree index_type = c_common_signed_type (sizetype);
3934 array_ptr_quals = TREE_TYPE (declarator);
3935 array_parm_static = TREE_STATIC (declarator);
3937 declarator = TREE_OPERAND (declarator, 0);
3939 /* Check for some types that there cannot be arrays of. */
3941 if (VOID_TYPE_P (type))
3943 error ("declaration of `%s' as array of voids", name);
3944 type = error_mark_node;
3947 if (TREE_CODE (type) == FUNCTION_TYPE)
3949 error ("declaration of `%s' as array of functions", name);
3950 type = error_mark_node;
3953 if (size == error_mark_node)
3954 type = error_mark_node;
3956 if (type == error_mark_node)
3957 continue;
3959 /* If size was specified, set ITYPE to a range-type for that size.
3960 Otherwise, ITYPE remains null. finish_decl may figure it out
3961 from an initial value. */
3963 if (size)
3965 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3966 STRIP_TYPE_NOPS (size);
3968 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3970 error ("size of array `%s' has non-integer type", name);
3971 size = integer_one_node;
3974 if (pedantic && integer_zerop (size))
3975 pedwarn ("ISO C forbids zero-size array `%s'", name);
3977 if (TREE_CODE (size) == INTEGER_CST)
3979 constant_expression_warning (size);
3980 if (tree_int_cst_sgn (size) < 0)
3982 error ("size of array `%s' is negative", name);
3983 size = integer_one_node;
3986 else
3988 /* Make sure the array size remains visibly nonconstant
3989 even if it is (eg) a const variable with known value. */
3990 size_varies = 1;
3992 if (!flag_isoc99 && pedantic)
3994 if (TREE_CONSTANT (size))
3995 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3996 name);
3997 else
3998 pedwarn ("ISO C90 forbids variable-size array `%s'",
3999 name);
4003 if (integer_zerop (size))
4005 /* A zero-length array cannot be represented with an
4006 unsigned index type, which is what we'll get with
4007 build_index_type. Create an open-ended range instead. */
4008 itype = build_range_type (sizetype, size, NULL_TREE);
4010 else
4012 /* Compute the maximum valid index, that is, size - 1.
4013 Do the calculation in index_type, so that if it is
4014 a variable the computations will be done in the
4015 proper mode. */
4016 itype = fold (build (MINUS_EXPR, index_type,
4017 convert (index_type, size),
4018 convert (index_type, size_one_node)));
4020 /* If that overflowed, the array is too big.
4021 ??? While a size of INT_MAX+1 technically shouldn't
4022 cause an overflow (because we subtract 1), the overflow
4023 is recorded during the conversion to index_type, before
4024 the subtraction. Handling this case seems like an
4025 unnecessary complication. */
4026 if (TREE_OVERFLOW (itype))
4028 error ("size of array `%s' is too large", name);
4029 type = error_mark_node;
4030 continue;
4033 if (size_varies)
4034 itype = variable_size (itype);
4035 itype = build_index_type (itype);
4038 else if (decl_context == FIELD)
4040 if (pedantic && !flag_isoc99 && !in_system_header)
4041 pedwarn ("ISO C90 does not support flexible array members");
4043 /* ISO C99 Flexible array members are effectively identical
4044 to GCC's zero-length array extension. */
4045 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4048 /* If pedantic, complain about arrays of incomplete types. */
4050 if (pedantic && !COMPLETE_TYPE_P (type))
4051 pedwarn ("array type has incomplete element type");
4053 #if 0
4054 /* We shouldn't have a function type here at all!
4055 Functions aren't allowed as array elements. */
4056 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4057 && (constp || volatilep))
4058 pedwarn ("ISO C forbids const or volatile function types");
4059 #endif
4061 /* Build the array type itself, then merge any constancy or
4062 volatility into the target type. We must do it in this order
4063 to ensure that the TYPE_MAIN_VARIANT field of the array type
4064 is set correctly. */
4066 type = build_array_type (type, itype);
4067 if (type_quals)
4068 type = c_build_qualified_type (type, type_quals);
4070 if (size_varies)
4071 C_TYPE_VARIABLE_SIZE (type) = 1;
4073 /* The GCC extension for zero-length arrays differs from
4074 ISO flexible array members in that sizeof yields zero. */
4075 if (size && integer_zerop (size))
4077 layout_type (type);
4078 TYPE_SIZE (type) = bitsize_zero_node;
4079 TYPE_SIZE_UNIT (type) = size_zero_node;
4081 if (decl_context != PARM
4082 && (array_ptr_quals != NULL_TREE || array_parm_static))
4084 error ("static or type qualifiers in non-parameter array declarator");
4085 array_ptr_quals = NULL_TREE;
4086 array_parm_static = 0;
4089 else if (TREE_CODE (declarator) == CALL_EXPR)
4091 tree arg_types;
4093 /* Declaring a function type.
4094 Make sure we have a valid type for the function to return. */
4095 if (type == error_mark_node)
4096 continue;
4098 size_varies = 0;
4100 /* Warn about some types functions can't return. */
4102 if (TREE_CODE (type) == FUNCTION_TYPE)
4104 error ("`%s' declared as function returning a function", name);
4105 type = integer_type_node;
4107 if (TREE_CODE (type) == ARRAY_TYPE)
4109 error ("`%s' declared as function returning an array", name);
4110 type = integer_type_node;
4113 /* Construct the function type and go to the next
4114 inner layer of declarator. */
4116 arg_types = grokparms (TREE_OPERAND (declarator, 1),
4117 funcdef_flag
4118 /* Say it's a definition
4119 only for the CALL_EXPR
4120 closest to the identifier. */
4121 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
4122 /* Type qualifiers before the return type of the function
4123 qualify the return type, not the function type. */
4124 if (type_quals)
4126 /* Type qualifiers on a function return type are normally
4127 permitted by the standard but have no effect, so give a
4128 warning at -W. Qualifiers on a void return type have
4129 meaning as a GNU extension, and are banned on function
4130 definitions in ISO C. FIXME: strictly we shouldn't
4131 pedwarn for qualified void return types except on function
4132 definitions, but not doing so could lead to the undesirable
4133 state of a "volatile void" function return type not being
4134 warned about, and a use of the function being compiled
4135 with GNU semantics, with no diagnostics under -pedantic. */
4136 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4137 pedwarn ("ISO C forbids qualified void function return type");
4138 else if (extra_warnings
4139 && !(VOID_TYPE_P (type)
4140 && type_quals == TYPE_QUAL_VOLATILE))
4141 warning ("type qualifiers ignored on function return type");
4143 type = c_build_qualified_type (type, type_quals);
4145 type_quals = TYPE_UNQUALIFIED;
4147 type = build_function_type (type, arg_types);
4148 declarator = TREE_OPERAND (declarator, 0);
4150 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4151 the formal parameter list of this FUNCTION_TYPE to point to
4152 the FUNCTION_TYPE node itself. */
4155 tree link;
4157 for (link = last_function_parm_tags;
4158 link;
4159 link = TREE_CHAIN (link))
4160 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4163 else if (TREE_CODE (declarator) == INDIRECT_REF)
4165 /* Merge any constancy or volatility into the target type
4166 for the pointer. */
4168 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4169 && type_quals)
4170 pedwarn ("ISO C forbids qualified function types");
4171 if (type_quals)
4172 type = c_build_qualified_type (type, type_quals);
4173 type_quals = TYPE_UNQUALIFIED;
4174 size_varies = 0;
4176 type = build_pointer_type (type);
4178 /* Process a list of type modifier keywords
4179 (such as const or volatile) that were given inside the `*'. */
4181 if (TREE_TYPE (declarator))
4183 tree typemodlist;
4184 int erred = 0;
4186 constp = 0;
4187 volatilep = 0;
4188 restrictp = 0;
4189 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4190 typemodlist = TREE_CHAIN (typemodlist))
4192 tree qualifier = TREE_VALUE (typemodlist);
4194 if (C_IS_RESERVED_WORD (qualifier))
4196 if (C_RID_CODE (qualifier) == RID_CONST)
4197 constp++;
4198 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4199 volatilep++;
4200 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4201 restrictp++;
4202 else
4203 erred++;
4205 else
4206 erred++;
4209 if (erred)
4210 error ("invalid type modifier within pointer declarator");
4211 if (constp > 1 && ! flag_isoc99)
4212 pedwarn ("duplicate `const'");
4213 if (volatilep > 1 && ! flag_isoc99)
4214 pedwarn ("duplicate `volatile'");
4215 if (restrictp > 1 && ! flag_isoc99)
4216 pedwarn ("duplicate `restrict'");
4218 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4219 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4220 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4223 declarator = TREE_OPERAND (declarator, 0);
4225 else
4226 abort ();
4230 /* Now TYPE has the actual type. */
4232 /* Did array size calculations overflow? */
4234 if (TREE_CODE (type) == ARRAY_TYPE
4235 && COMPLETE_TYPE_P (type)
4236 && TREE_OVERFLOW (TYPE_SIZE (type)))
4238 error ("size of array `%s' is too large", name);
4239 /* If we proceed with the array type as it is, we'll eventually
4240 crash in tree_low_cst(). */
4241 type = error_mark_node;
4244 /* If this is declaring a typedef name, return a TYPE_DECL. */
4246 if (specbits & (1 << (int) RID_TYPEDEF))
4248 tree decl;
4249 /* Note that the grammar rejects storage classes
4250 in typenames, fields or parameters */
4251 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4252 && type_quals)
4253 pedwarn ("ISO C forbids qualified function types");
4254 if (type_quals)
4255 type = c_build_qualified_type (type, type_quals);
4256 decl = build_decl (TYPE_DECL, declarator, type);
4257 if ((specbits & (1 << (int) RID_SIGNED))
4258 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4259 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4260 decl_attributes (&decl, returned_attrs, 0);
4261 return decl;
4264 /* Detect the case of an array type of unspecified size
4265 which came, as such, direct from a typedef name.
4266 We must copy the type, so that each identifier gets
4267 a distinct type, so that each identifier's size can be
4268 controlled separately by its own initializer. */
4270 if (type != 0 && typedef_type != 0
4271 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4272 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4274 type = build_array_type (TREE_TYPE (type), 0);
4275 if (size_varies)
4276 C_TYPE_VARIABLE_SIZE (type) = 1;
4279 /* If this is a type name (such as, in a cast or sizeof),
4280 compute the type and return it now. */
4282 if (decl_context == TYPENAME)
4284 /* Note that the grammar rejects storage classes
4285 in typenames, fields or parameters */
4286 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4287 && type_quals)
4288 pedwarn ("ISO C forbids const or volatile function types");
4289 if (type_quals)
4290 type = c_build_qualified_type (type, type_quals);
4291 decl_attributes (&type, returned_attrs, 0);
4292 return type;
4295 /* Aside from typedefs and type names (handle above),
4296 `void' at top level (not within pointer)
4297 is allowed only in public variables.
4298 We don't complain about parms either, but that is because
4299 a better error message can be made later. */
4301 if (VOID_TYPE_P (type) && decl_context != PARM
4302 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4303 && ((specbits & (1 << (int) RID_EXTERN))
4304 || (current_binding_level == global_binding_level
4305 && !(specbits
4306 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4308 error ("variable or field `%s' declared void", name);
4309 type = integer_type_node;
4312 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4313 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4316 tree decl;
4318 if (decl_context == PARM)
4320 tree type_as_written;
4321 tree promoted_type;
4323 /* A parameter declared as an array of T is really a pointer to T.
4324 One declared as a function is really a pointer to a function. */
4326 if (TREE_CODE (type) == ARRAY_TYPE)
4328 /* Transfer const-ness of array into that of type pointed to. */
4329 type = TREE_TYPE (type);
4330 if (type_quals)
4331 type = c_build_qualified_type (type, type_quals);
4332 type = build_pointer_type (type);
4333 type_quals = TYPE_UNQUALIFIED;
4334 if (array_ptr_quals)
4336 tree new_ptr_quals, new_ptr_attrs;
4337 int erred = 0;
4338 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4339 /* We don't yet implement attributes in this context. */
4340 if (new_ptr_attrs != NULL_TREE)
4341 warning ("attributes in parameter array declarator ignored");
4343 constp = 0;
4344 volatilep = 0;
4345 restrictp = 0;
4346 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4348 tree qualifier = TREE_VALUE (new_ptr_quals);
4350 if (C_IS_RESERVED_WORD (qualifier))
4352 if (C_RID_CODE (qualifier) == RID_CONST)
4353 constp++;
4354 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4355 volatilep++;
4356 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4357 restrictp++;
4358 else
4359 erred++;
4361 else
4362 erred++;
4365 if (erred)
4366 error ("invalid type modifier within array declarator");
4368 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4369 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4370 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4372 size_varies = 0;
4374 else if (TREE_CODE (type) == FUNCTION_TYPE)
4376 if (pedantic && type_quals)
4377 pedwarn ("ISO C forbids qualified function types");
4378 if (type_quals)
4379 type = c_build_qualified_type (type, type_quals);
4380 type = build_pointer_type (type);
4381 type_quals = TYPE_UNQUALIFIED;
4383 else if (type_quals)
4384 type = c_build_qualified_type (type, type_quals);
4386 type_as_written = type;
4388 decl = build_decl (PARM_DECL, declarator, type);
4389 if (size_varies)
4390 C_DECL_VARIABLE_SIZE (decl) = 1;
4392 /* Compute the type actually passed in the parmlist,
4393 for the case where there is no prototype.
4394 (For example, shorts and chars are passed as ints.)
4395 When there is a prototype, this is overridden later. */
4397 if (type == error_mark_node)
4398 promoted_type = type;
4399 else
4400 promoted_type = c_type_promotes_to (type);
4402 DECL_ARG_TYPE (decl) = promoted_type;
4403 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4405 else if (decl_context == FIELD)
4407 /* Structure field. It may not be a function. */
4409 if (TREE_CODE (type) == FUNCTION_TYPE)
4411 error ("field `%s' declared as a function", name);
4412 type = build_pointer_type (type);
4414 else if (TREE_CODE (type) != ERROR_MARK
4415 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4417 error ("field `%s' has incomplete type", name);
4418 type = error_mark_node;
4420 /* Move type qualifiers down to element of an array. */
4421 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4423 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4424 type_quals),
4425 TYPE_DOMAIN (type));
4426 #if 0
4427 /* Leave the field const or volatile as well. */
4428 type_quals = TYPE_UNQUALIFIED;
4429 #endif
4431 decl = build_decl (FIELD_DECL, declarator, type);
4432 DECL_NONADDRESSABLE_P (decl) = bitfield;
4434 if (size_varies)
4435 C_DECL_VARIABLE_SIZE (decl) = 1;
4437 else if (TREE_CODE (type) == FUNCTION_TYPE)
4439 /* Every function declaration is "external"
4440 except for those which are inside a function body
4441 in which `auto' is used.
4442 That is a case not specified by ANSI C,
4443 and we use it for forward declarations for nested functions. */
4444 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4445 || current_binding_level == global_binding_level);
4447 if (specbits & (1 << (int) RID_AUTO)
4448 && (pedantic || current_binding_level == global_binding_level))
4449 pedwarn ("invalid storage class for function `%s'", name);
4450 if (specbits & (1 << (int) RID_REGISTER))
4451 error ("invalid storage class for function `%s'", name);
4452 if (specbits & (1 << (int) RID_THREAD))
4453 error ("invalid storage class for function `%s'", name);
4454 /* Function declaration not at top level.
4455 Storage classes other than `extern' are not allowed
4456 and `extern' makes no difference. */
4457 if (current_binding_level != global_binding_level
4458 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4459 && pedantic)
4460 pedwarn ("invalid storage class for function `%s'", name);
4462 decl = build_decl (FUNCTION_DECL, declarator, type);
4463 decl = build_decl_attribute_variant (decl, decl_attr);
4465 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
4466 ggc_alloc_cleared (sizeof (struct lang_decl));
4468 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4469 pedwarn ("ISO C forbids qualified function types");
4471 /* GNU C interprets a `volatile void' return type to indicate
4472 that the function does not return. */
4473 if ((type_quals & TYPE_QUAL_VOLATILE)
4474 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4475 warning ("`noreturn' function returns non-void value");
4477 if (extern_ref)
4478 DECL_EXTERNAL (decl) = 1;
4479 /* Record absence of global scope for `static' or `auto'. */
4480 TREE_PUBLIC (decl)
4481 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4483 if (defaulted_int)
4484 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4486 /* Record presence of `inline', if it is reasonable. */
4487 if (MAIN_NAME_P (declarator))
4489 if (inlinep)
4490 warning ("cannot inline function `main'");
4492 else if (inlinep)
4494 /* Assume that otherwise the function can be inlined. */
4495 DECL_DECLARED_INLINE_P (decl) = 1;
4497 /* Do not mark bare declarations as DECL_INLINE. Doing so
4498 in the presence of multiple declarations can result in
4499 the abstract origin pointing between the declarations,
4500 which will confuse dwarf2out. */
4501 if (initialized)
4503 DECL_INLINE (decl) = 1;
4504 if (specbits & (1 << (int) RID_EXTERN))
4505 current_extern_inline = 1;
4508 /* If -finline-functions, assume it can be inlined. This does
4509 two things: let the function be deferred until it is actually
4510 needed, and let dwarf2 know that the function is inlinable. */
4511 else if (flag_inline_trees == 2 && initialized)
4513 DECL_INLINE (decl) = 1;
4514 DECL_DECLARED_INLINE_P (decl) = 0;
4517 else
4519 /* It's a variable. */
4520 /* An uninitialized decl with `extern' is a reference. */
4521 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4523 /* Move type qualifiers down to element of an array. */
4524 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4526 int saved_align = TYPE_ALIGN(type);
4527 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4528 type_quals),
4529 TYPE_DOMAIN (type));
4530 TYPE_ALIGN (type) = saved_align;
4531 #if 0 /* Leave the variable const or volatile as well. */
4532 type_quals = TYPE_UNQUALIFIED;
4533 #endif
4535 else if (type_quals)
4536 type = c_build_qualified_type (type, type_quals);
4538 decl = build_decl (VAR_DECL, declarator, type);
4539 if (size_varies)
4540 C_DECL_VARIABLE_SIZE (decl) = 1;
4542 if (inlinep)
4543 pedwarn_with_decl (decl, "variable `%s' declared `inline'");
4545 DECL_EXTERNAL (decl) = extern_ref;
4547 /* At top level, the presence of a `static' or `register' storage
4548 class specifier, or the absence of all storage class specifiers
4549 makes this declaration a definition (perhaps tentative). Also,
4550 the absence of both `static' and `register' makes it public. */
4551 if (current_binding_level == global_binding_level)
4553 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4554 | (1 << (int) RID_REGISTER)));
4555 TREE_STATIC (decl) = !extern_ref;
4557 /* Not at top level, only `static' makes a static definition. */
4558 else
4560 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4561 TREE_PUBLIC (decl) = extern_ref;
4564 if (specbits & 1 << (int) RID_THREAD)
4566 if (targetm.have_tls)
4567 DECL_THREAD_LOCAL (decl) = 1;
4568 else
4569 /* A mere warning is sure to result in improper semantics
4570 at runtime. Don't bother to allow this to compile. */
4571 error ("thread-local storage not supported for this target");
4575 /* Record `register' declaration for warnings on &
4576 and in case doing stupid register allocation. */
4578 if (specbits & (1 << (int) RID_REGISTER))
4579 DECL_REGISTER (decl) = 1;
4581 /* Record constancy and volatility. */
4582 c_apply_type_quals_to_decl (type_quals, decl);
4584 /* If a type has volatile components, it should be stored in memory.
4585 Otherwise, the fact that those components are volatile
4586 will be ignored, and would even crash the compiler. */
4587 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4588 c_mark_addressable (decl);
4590 decl_attributes (&decl, returned_attrs, 0);
4592 return decl;
4596 /* Decode the parameter-list info for a function type or function definition.
4597 The argument is the value returned by `get_parm_info' (or made in parse.y
4598 if there is an identifier list instead of a parameter decl list).
4599 These two functions are separate because when a function returns
4600 or receives functions then each is called multiple times but the order
4601 of calls is different. The last call to `grokparms' is always the one
4602 that contains the formal parameter names of a function definition.
4604 Store in `last_function_parms' a chain of the decls of parms.
4605 Also store in `last_function_parm_tags' a chain of the struct, union,
4606 and enum tags declared among the parms.
4608 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4610 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4611 a mere declaration. A nonempty identifier-list gets an error message
4612 when FUNCDEF_FLAG is zero. */
4614 static tree
4615 grokparms (parms_info, funcdef_flag)
4616 tree parms_info;
4617 int funcdef_flag;
4619 tree first_parm = TREE_CHAIN (parms_info);
4621 last_function_parms = TREE_PURPOSE (parms_info);
4622 last_function_parm_tags = TREE_VALUE (parms_info);
4624 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4625 && !in_system_header)
4626 warning ("function declaration isn't a prototype");
4628 if (first_parm != 0
4629 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4631 if (! funcdef_flag)
4632 pedwarn ("parameter names (without types) in function declaration");
4634 last_function_parms = first_parm;
4635 return 0;
4637 else
4639 tree parm;
4640 tree typelt;
4641 /* We no longer test FUNCDEF_FLAG.
4642 If the arg types are incomplete in a declaration,
4643 they must include undefined tags.
4644 These tags can never be defined in the scope of the declaration,
4645 so the types can never be completed,
4646 and no call can be compiled successfully. */
4647 #if 0
4648 /* In a fcn definition, arg types must be complete. */
4649 if (funcdef_flag)
4650 #endif
4651 for (parm = last_function_parms, typelt = first_parm;
4652 parm;
4653 parm = TREE_CHAIN (parm))
4654 /* Skip over any enumeration constants declared here. */
4655 if (TREE_CODE (parm) == PARM_DECL)
4657 /* Barf if the parameter itself has an incomplete type. */
4658 tree type = TREE_VALUE (typelt);
4659 if (type == error_mark_node)
4660 continue;
4661 if (!COMPLETE_TYPE_P (type))
4663 if (funcdef_flag && DECL_NAME (parm) != 0)
4664 error ("parameter `%s' has incomplete type",
4665 IDENTIFIER_POINTER (DECL_NAME (parm)));
4666 else
4667 warning ("parameter has incomplete type");
4668 if (funcdef_flag)
4670 TREE_VALUE (typelt) = error_mark_node;
4671 TREE_TYPE (parm) = error_mark_node;
4674 #if 0
4675 /* This has been replaced by parm_tags_warning, which
4676 uses a more accurate criterion for what to warn
4677 about. */
4678 else
4680 /* Now warn if is a pointer to an incomplete type. */
4681 while (TREE_CODE (type) == POINTER_TYPE
4682 || TREE_CODE (type) == REFERENCE_TYPE)
4683 type = TREE_TYPE (type);
4684 type = TYPE_MAIN_VARIANT (type);
4685 if (!COMPLETE_TYPE_P (type))
4687 if (DECL_NAME (parm) != 0)
4688 warning ("parameter `%s' points to incomplete type",
4689 IDENTIFIER_POINTER (DECL_NAME (parm)));
4690 else
4691 warning ("parameter points to incomplete type");
4694 #endif
4695 typelt = TREE_CHAIN (typelt);
4698 return first_parm;
4702 /* Return a tree_list node with info on a parameter list just parsed.
4703 The TREE_PURPOSE is a chain of decls of those parms.
4704 The TREE_VALUE is a list of structure, union and enum tags defined.
4705 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4706 This tree_list node is later fed to `grokparms'.
4708 VOID_AT_END nonzero means append `void' to the end of the type-list.
4709 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4711 tree
4712 get_parm_info (void_at_end)
4713 int void_at_end;
4715 tree decl, t;
4716 tree types = 0;
4717 int erred = 0;
4718 tree tags = gettags ();
4719 tree parms = getdecls ();
4720 tree new_parms = 0;
4721 tree order = current_binding_level->parm_order;
4723 /* Just `void' (and no ellipsis) is special. There are really no parms.
4724 But if the `void' is qualified (by `const' or `volatile') or has a
4725 storage class specifier (`register'), then the behavior is undefined;
4726 by not counting it as the special case of `void' we will cause an
4727 error later. Typedefs for `void' are OK (see DR#157). */
4728 if (void_at_end && parms != 0
4729 && TREE_CHAIN (parms) == 0
4730 && VOID_TYPE_P (TREE_TYPE (parms))
4731 && ! TREE_THIS_VOLATILE (parms)
4732 && ! TREE_READONLY (parms)
4733 && ! DECL_REGISTER (parms)
4734 && DECL_NAME (parms) == 0)
4736 parms = NULL_TREE;
4737 storedecls (NULL_TREE);
4738 return tree_cons (NULL_TREE, NULL_TREE,
4739 tree_cons (NULL_TREE, void_type_node, NULL_TREE));
4742 /* Extract enumerator values and other non-parms declared with the parms.
4743 Likewise any forward parm decls that didn't have real parm decls. */
4744 for (decl = parms; decl;)
4746 tree next = TREE_CHAIN (decl);
4748 if (TREE_CODE (decl) != PARM_DECL)
4750 TREE_CHAIN (decl) = new_parms;
4751 new_parms = decl;
4753 else if (TREE_ASM_WRITTEN (decl))
4755 error_with_decl (decl,
4756 "parameter `%s' has just a forward declaration");
4757 TREE_CHAIN (decl) = new_parms;
4758 new_parms = decl;
4760 decl = next;
4763 /* Put the parm decls back in the order they were in in the parm list. */
4764 for (t = order; t; t = TREE_CHAIN (t))
4766 if (TREE_CHAIN (t))
4767 TREE_CHAIN (TREE_VALUE (t)) = TREE_VALUE (TREE_CHAIN (t));
4768 else
4769 TREE_CHAIN (TREE_VALUE (t)) = 0;
4772 new_parms = chainon (order ? nreverse (TREE_VALUE (order)) : 0,
4773 new_parms);
4775 /* Store the parmlist in the binding level since the old one
4776 is no longer a valid list. (We have changed the chain pointers.) */
4777 storedecls (new_parms);
4779 for (decl = new_parms; decl; decl = TREE_CHAIN (decl))
4780 /* There may also be declarations for enumerators if an enumeration
4781 type is declared among the parms. Ignore them here. */
4782 if (TREE_CODE (decl) == PARM_DECL)
4784 /* Since there is a prototype,
4785 args are passed in their declared types. */
4786 tree type = TREE_TYPE (decl);
4787 DECL_ARG_TYPE (decl) = type;
4788 if (PROMOTE_PROTOTYPES
4789 && INTEGRAL_TYPE_P (type)
4790 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4791 DECL_ARG_TYPE (decl) = integer_type_node;
4793 types = tree_cons (NULL_TREE, TREE_TYPE (decl), types);
4794 if (VOID_TYPE_P (TREE_VALUE (types)) && ! erred
4795 && DECL_NAME (decl) == 0)
4797 error ("`void' in parameter list must be the entire list");
4798 erred = 1;
4802 if (void_at_end)
4803 return tree_cons (new_parms, tags,
4804 nreverse (tree_cons (NULL_TREE, void_type_node, types)));
4806 return tree_cons (new_parms, tags, nreverse (types));
4809 /* At end of parameter list, warn about any struct, union or enum tags
4810 defined within. Do so because these types cannot ever become complete. */
4812 void
4813 parmlist_tags_warning ()
4815 tree elt;
4816 static int already;
4818 for (elt = current_binding_level->tags; elt; elt = TREE_CHAIN (elt))
4820 enum tree_code code = TREE_CODE (TREE_VALUE (elt));
4821 /* An anonymous union parm type is meaningful as a GNU extension.
4822 So don't warn for that. */
4823 if (code == UNION_TYPE && TREE_PURPOSE (elt) == 0 && !pedantic)
4824 continue;
4825 if (TREE_PURPOSE (elt) != 0)
4827 if (code == RECORD_TYPE)
4828 warning ("`struct %s' declared inside parameter list",
4829 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4830 else if (code == UNION_TYPE)
4831 warning ("`union %s' declared inside parameter list",
4832 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4833 else
4834 warning ("`enum %s' declared inside parameter list",
4835 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4837 else
4839 /* For translation these need to be separate warnings */
4840 if (code == RECORD_TYPE)
4841 warning ("anonymous struct declared inside parameter list");
4842 else if (code == UNION_TYPE)
4843 warning ("anonymous union declared inside parameter list");
4844 else
4845 warning ("anonymous enum declared inside parameter list");
4847 if (! already)
4849 warning ("its scope is only this definition or declaration, which is probably not what you want");
4850 already = 1;
4855 /* Get the struct, enum or union (CODE says which) with tag NAME.
4856 Define the tag as a forward-reference if it is not defined. */
4858 tree
4859 xref_tag (code, name)
4860 enum tree_code code;
4861 tree name;
4863 /* If a cross reference is requested, look up the type
4864 already defined for this tag and return it. */
4866 tree ref = lookup_tag (code, name, current_binding_level, 0);
4867 /* If this is the right type of tag, return what we found.
4868 (This reference will be shadowed by shadow_tag later if appropriate.)
4869 If this is the wrong type of tag, do not return it. If it was the
4870 wrong type in the same binding level, we will have had an error
4871 message already; if in a different binding level and declaring
4872 a name, pending_xref_error will give an error message; but if in a
4873 different binding level and not declaring a name, this tag should
4874 shadow the previous declaration of a different type of tag, and
4875 this would not work properly if we return the reference found.
4876 (For example, with "struct foo" in an outer scope, "union foo;"
4877 must shadow that tag with a new one of union type.) */
4878 if (ref && TREE_CODE (ref) == code)
4879 return ref;
4881 /* If no such tag is yet defined, create a forward-reference node
4882 and record it as the "definition".
4883 When a real declaration of this type is found,
4884 the forward-reference will be altered into a real type. */
4886 ref = make_node (code);
4887 if (code == ENUMERAL_TYPE)
4889 /* Give the type a default layout like unsigned int
4890 to avoid crashing if it does not get defined. */
4891 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4892 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4893 TYPE_USER_ALIGN (ref) = 0;
4894 TREE_UNSIGNED (ref) = 1;
4895 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4896 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4897 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4900 pushtag (name, ref);
4902 return ref;
4905 /* Make sure that the tag NAME is defined *in the current binding level*
4906 at least as a forward reference.
4907 CODE says which kind of tag NAME ought to be. */
4909 tree
4910 start_struct (code, name)
4911 enum tree_code code;
4912 tree name;
4914 /* If there is already a tag defined at this binding level
4915 (as a forward reference), just return it. */
4917 tree ref = 0;
4919 if (name != 0)
4920 ref = lookup_tag (code, name, current_binding_level, 1);
4921 if (ref && TREE_CODE (ref) == code)
4923 C_TYPE_BEING_DEFINED (ref) = 1;
4924 TYPE_PACKED (ref) = flag_pack_struct;
4925 if (TYPE_FIELDS (ref))
4927 if (code == UNION_TYPE)
4928 error ("redefinition of `union %s'",
4929 IDENTIFIER_POINTER (name));
4930 else
4931 error ("redefinition of `struct %s'",
4932 IDENTIFIER_POINTER (name));
4935 return ref;
4938 /* Otherwise create a forward-reference just so the tag is in scope. */
4940 ref = make_node (code);
4941 pushtag (name, ref);
4942 C_TYPE_BEING_DEFINED (ref) = 1;
4943 TYPE_PACKED (ref) = flag_pack_struct;
4944 return ref;
4947 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4948 of a structure component, returning a FIELD_DECL node.
4949 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4951 This is done during the parsing of the struct declaration.
4952 The FIELD_DECL nodes are chained together and the lot of them
4953 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4955 tree
4956 grokfield (filename, line, declarator, declspecs, width)
4957 const char *filename ATTRIBUTE_UNUSED;
4958 int line ATTRIBUTE_UNUSED;
4959 tree declarator, declspecs, width;
4961 tree value;
4963 if (declarator == NULL_TREE && width == NULL_TREE)
4965 /* This is an unnamed decl.
4967 If we have something of the form "union { list } ;" then this
4968 is the anonymous union extension. Similarly for struct.
4970 If this is something of the form "struct foo;", then
4971 If MS extensions are enabled, this is handled as an
4972 anonymous struct.
4973 Otherwise this is a forward declaration of a structure tag.
4975 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4976 If MS extensions are enabled and foo names a structure, then
4977 again this is an anonymous struct.
4978 Otherwise this is an error.
4980 Oh what a horrid tangled web we weave. I wonder if MS consiously
4981 took this from Plan 9 or if it was an accident of implementation
4982 that took root before someone noticed the bug... */
4984 tree type = TREE_VALUE (declspecs);
4986 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4987 type = TREE_TYPE (type);
4988 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4990 if (flag_ms_extensions)
4991 ; /* ok */
4992 else if (flag_iso)
4993 goto warn_unnamed_field;
4994 else if (TYPE_NAME (type) == NULL)
4995 ; /* ok */
4996 else
4997 goto warn_unnamed_field;
4999 else
5001 warn_unnamed_field:
5002 warning ("declaration does not declare anything");
5003 return NULL_TREE;
5007 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
5009 finish_decl (value, NULL_TREE, NULL_TREE);
5010 DECL_INITIAL (value) = width;
5012 maybe_objc_check_decl (value);
5013 return value;
5016 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5017 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5018 ATTRIBUTES are attributes to be applied to the structure. */
5020 tree
5021 finish_struct (t, fieldlist, attributes)
5022 tree t;
5023 tree fieldlist;
5024 tree attributes;
5026 tree x;
5027 int toplevel = global_binding_level == current_binding_level;
5028 int saw_named_field;
5030 /* If this type was previously laid out as a forward reference,
5031 make sure we lay it out again. */
5033 TYPE_SIZE (t) = 0;
5035 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5037 /* Nameless union parm types are useful as GCC extension. */
5038 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
5039 /* Otherwise, warn about any struct or union def. in parmlist. */
5040 if (in_parm_level_p ())
5042 if (pedantic)
5043 pedwarn ("%s defined inside parms",
5044 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
5045 else
5046 warning ("%s defined inside parms",
5047 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
5050 if (pedantic)
5052 for (x = fieldlist; x; x = TREE_CHAIN (x))
5053 if (DECL_NAME (x) != 0)
5054 break;
5056 if (x == 0)
5057 pedwarn ("%s has no %s",
5058 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5059 fieldlist ? _("named members") : _("members"));
5062 /* Install struct as DECL_CONTEXT of each field decl.
5063 Also process specified field sizes,m which is found in the DECL_INITIAL.
5064 Store 0 there, except for ": 0" fields (so we can find them
5065 and delete them, below). */
5067 saw_named_field = 0;
5068 for (x = fieldlist; x; x = TREE_CHAIN (x))
5070 DECL_CONTEXT (x) = t;
5071 DECL_PACKED (x) |= TYPE_PACKED (t);
5073 /* If any field is const, the structure type is pseudo-const. */
5074 if (TREE_READONLY (x))
5075 C_TYPE_FIELDS_READONLY (t) = 1;
5076 else
5078 /* A field that is pseudo-const makes the structure likewise. */
5079 tree t1 = TREE_TYPE (x);
5080 while (TREE_CODE (t1) == ARRAY_TYPE)
5081 t1 = TREE_TYPE (t1);
5082 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5083 && C_TYPE_FIELDS_READONLY (t1))
5084 C_TYPE_FIELDS_READONLY (t) = 1;
5087 /* Any field that is volatile means variables of this type must be
5088 treated in some ways as volatile. */
5089 if (TREE_THIS_VOLATILE (x))
5090 C_TYPE_FIELDS_VOLATILE (t) = 1;
5092 /* Any field of nominal variable size implies structure is too. */
5093 if (C_DECL_VARIABLE_SIZE (x))
5094 C_TYPE_VARIABLE_SIZE (t) = 1;
5096 /* Detect invalid nested redefinition. */
5097 if (TREE_TYPE (x) == t)
5098 error ("nested redefinition of `%s'",
5099 IDENTIFIER_POINTER (TYPE_NAME (t)));
5101 /* Detect invalid bit-field size. */
5102 if (DECL_INITIAL (x))
5103 STRIP_NOPS (DECL_INITIAL (x));
5104 if (DECL_INITIAL (x))
5106 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
5107 constant_expression_warning (DECL_INITIAL (x));
5108 else
5110 error_with_decl (x,
5111 "bit-field `%s' width not an integer constant");
5112 DECL_INITIAL (x) = NULL;
5116 /* Detect invalid bit-field type. */
5117 if (DECL_INITIAL (x)
5118 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
5119 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
5120 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
5122 error_with_decl (x, "bit-field `%s' has invalid type");
5123 DECL_INITIAL (x) = NULL;
5126 if (DECL_INITIAL (x) && pedantic
5127 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
5128 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
5129 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != c_bool_type_node
5130 /* Accept an enum that's equivalent to int or unsigned int. */
5131 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5132 && (TYPE_PRECISION (TREE_TYPE (x))
5133 == TYPE_PRECISION (integer_type_node))))
5134 pedwarn_with_decl (x, "bit-field `%s' type invalid in ISO C");
5136 /* Detect and ignore out of range field width and process valid
5137 field widths. */
5138 if (DECL_INITIAL (x))
5140 int max_width
5141 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == c_bool_type_node
5142 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
5144 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
5145 error_with_decl (x, "negative width in bit-field `%s'");
5146 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
5147 pedwarn_with_decl (x, "width of `%s' exceeds its type");
5148 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5149 error_with_decl (x, "zero width for bit-field `%s'");
5150 else
5152 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5153 unsigned HOST_WIDE_INT width
5154 = tree_low_cst (DECL_INITIAL (x), 1);
5156 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5157 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5158 TREE_UNSIGNED (TREE_TYPE (x)))
5159 || (width
5160 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5161 TREE_UNSIGNED (TREE_TYPE (x))))))
5162 warning_with_decl (x,
5163 "`%s' is narrower than values of its type");
5165 DECL_SIZE (x) = bitsize_int (width);
5166 DECL_BIT_FIELD (x) = 1;
5167 SET_DECL_C_BIT_FIELD (x);
5169 if (width == 0
5170 && ! (* targetm.ms_bitfield_layout_p) (t))
5172 /* field size 0 => force desired amount of alignment. */
5173 #ifdef EMPTY_FIELD_BOUNDARY
5174 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), EMPTY_FIELD_BOUNDARY);
5175 #endif
5176 #ifdef PCC_BITFIELD_TYPE_MATTERS
5177 if (PCC_BITFIELD_TYPE_MATTERS)
5179 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
5180 TYPE_ALIGN (TREE_TYPE (x)));
5181 DECL_USER_ALIGN (x) |= TYPE_USER_ALIGN (TREE_TYPE (x));
5183 #endif
5188 else if (TREE_TYPE (x) != error_mark_node)
5190 unsigned int min_align = (DECL_PACKED (x) ? BITS_PER_UNIT
5191 : TYPE_ALIGN (TREE_TYPE (x)));
5193 /* Non-bit-fields are aligned for their type, except packed
5194 fields which require only BITS_PER_UNIT alignment. */
5195 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), min_align);
5196 if (! DECL_PACKED (x))
5197 DECL_USER_ALIGN (x) |= TYPE_USER_ALIGN (TREE_TYPE (x));
5200 DECL_INITIAL (x) = 0;
5202 /* Detect flexible array member in an invalid context. */
5203 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5204 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5205 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5206 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5208 if (TREE_CODE (t) == UNION_TYPE)
5209 error_with_decl (x, "flexible array member in union");
5210 else if (TREE_CHAIN (x) != NULL_TREE)
5211 error_with_decl (x, "flexible array member not at end of struct");
5212 else if (! saw_named_field)
5213 error_with_decl (x, "flexible array member in otherwise empty struct");
5215 if (DECL_NAME (x))
5216 saw_named_field = 1;
5219 /* Delete all duplicate fields from the fieldlist */
5220 for (x = fieldlist; x && TREE_CHAIN (x);)
5221 /* Anonymous fields aren't duplicates. */
5222 if (DECL_NAME (TREE_CHAIN (x)) == 0)
5223 x = TREE_CHAIN (x);
5224 else
5226 tree y = fieldlist;
5228 while (1)
5230 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5231 break;
5232 if (y == x)
5233 break;
5234 y = TREE_CHAIN (y);
5236 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5238 error_with_decl (TREE_CHAIN (x), "duplicate member `%s'");
5239 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
5241 else
5242 x = TREE_CHAIN (x);
5245 /* Now we have the nearly final fieldlist. Record it,
5246 then lay out the structure or union (including the fields). */
5248 TYPE_FIELDS (t) = fieldlist;
5250 layout_type (t);
5252 /* Delete all zero-width bit-fields from the fieldlist */
5254 tree *fieldlistp = &fieldlist;
5255 while (*fieldlistp)
5256 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5257 *fieldlistp = TREE_CHAIN (*fieldlistp);
5258 else
5259 fieldlistp = &TREE_CHAIN (*fieldlistp);
5262 /* Now we have the truly final field list.
5263 Store it in this type and in the variants. */
5265 TYPE_FIELDS (t) = fieldlist;
5267 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5269 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5270 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5271 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5272 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5275 /* If this was supposed to be a transparent union, but we can't
5276 make it one, warn and turn off the flag. */
5277 if (TREE_CODE (t) == UNION_TYPE
5278 && TYPE_TRANSPARENT_UNION (t)
5279 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5281 TYPE_TRANSPARENT_UNION (t) = 0;
5282 warning ("union cannot be made transparent");
5285 /* If this structure or union completes the type of any previous
5286 variable declaration, lay it out and output its rtl. */
5288 if (current_binding_level->incomplete_list != NULL_TREE)
5290 tree prev = NULL_TREE;
5292 for (x = current_binding_level->incomplete_list; x; x = TREE_CHAIN (x))
5294 tree decl = TREE_VALUE (x);
5296 if (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TYPE_MAIN_VARIANT (t)
5297 && TREE_CODE (decl) != TYPE_DECL)
5299 layout_decl (decl, 0);
5300 /* This is a no-op in c-lang.c or something real in objc-actions.c. */
5301 maybe_objc_check_decl (decl);
5302 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5303 if (! toplevel)
5304 expand_decl (decl);
5305 /* Unlink X from the incomplete list. */
5306 if (prev)
5307 TREE_CHAIN (prev) = TREE_CHAIN (x);
5308 else
5309 current_binding_level->incomplete_list = TREE_CHAIN (x);
5311 else if (!COMPLETE_TYPE_P (TREE_TYPE (decl))
5312 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5314 tree element = TREE_TYPE (decl);
5315 while (TREE_CODE (element) == ARRAY_TYPE)
5316 element = TREE_TYPE (element);
5317 if (element == t)
5319 layout_array_type (TREE_TYPE (decl));
5320 if (TREE_CODE (decl) != TYPE_DECL)
5322 layout_decl (decl, 0);
5323 maybe_objc_check_decl (decl);
5324 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5325 if (! toplevel)
5326 expand_decl (decl);
5328 /* Unlink X from the incomplete list. */
5329 if (prev)
5330 TREE_CHAIN (prev) = TREE_CHAIN (x);
5331 else
5332 current_binding_level->incomplete_list = TREE_CHAIN (x);
5338 /* Finish debugging output for this type. */
5339 rest_of_type_compilation (t, toplevel);
5341 return t;
5344 /* Lay out the type T, and its element type, and so on. */
5346 static void
5347 layout_array_type (t)
5348 tree t;
5350 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5351 layout_array_type (TREE_TYPE (t));
5352 layout_type (t);
5355 /* Begin compiling the definition of an enumeration type.
5356 NAME is its name (or null if anonymous).
5357 Returns the type object, as yet incomplete.
5358 Also records info about it so that build_enumerator
5359 may be used to declare the individual values as they are read. */
5361 tree
5362 start_enum (name)
5363 tree name;
5365 tree enumtype = 0;
5367 /* If this is the real definition for a previous forward reference,
5368 fill in the contents in the same object that used to be the
5369 forward reference. */
5371 if (name != 0)
5372 enumtype = lookup_tag (ENUMERAL_TYPE, name, current_binding_level, 1);
5374 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5376 enumtype = make_node (ENUMERAL_TYPE);
5377 pushtag (name, enumtype);
5380 C_TYPE_BEING_DEFINED (enumtype) = 1;
5382 if (TYPE_VALUES (enumtype) != 0)
5384 /* This enum is a named one that has been declared already. */
5385 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5387 /* Completely replace its old definition.
5388 The old enumerators remain defined, however. */
5389 TYPE_VALUES (enumtype) = 0;
5392 enum_next_value = integer_zero_node;
5393 enum_overflow = 0;
5395 if (flag_short_enums)
5396 TYPE_PACKED (enumtype) = 1;
5398 return enumtype;
5401 /* After processing and defining all the values of an enumeration type,
5402 install their decls in the enumeration type and finish it off.
5403 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5404 and ATTRIBUTES are the specified attributes.
5405 Returns ENUMTYPE. */
5407 tree
5408 finish_enum (enumtype, values, attributes)
5409 tree enumtype;
5410 tree values;
5411 tree attributes;
5413 tree pair, tem;
5414 tree minnode = 0, maxnode = 0, enum_value_type;
5415 int precision, unsign;
5416 int toplevel = (global_binding_level == current_binding_level);
5418 if (in_parm_level_p ())
5419 warning ("enum defined inside parms");
5421 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5423 /* Calculate the maximum value of any enumerator in this type. */
5425 if (values == error_mark_node)
5426 minnode = maxnode = integer_zero_node;
5427 else
5429 minnode = maxnode = TREE_VALUE (values);
5430 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5432 tree value = TREE_VALUE (pair);
5433 if (tree_int_cst_lt (maxnode, value))
5434 maxnode = value;
5435 if (tree_int_cst_lt (value, minnode))
5436 minnode = value;
5440 /* Construct the final type of this enumeration. It is the same
5441 as one of the integral types - the narrowest one that fits, except
5442 that normally we only go as narrow as int - and signed iff any of
5443 the values are negative. */
5444 unsign = (tree_int_cst_sgn (minnode) >= 0);
5445 precision = MAX (min_precision (minnode, unsign),
5446 min_precision (maxnode, unsign));
5447 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5449 tree narrowest = c_common_type_for_size (precision, unsign);
5450 if (narrowest == 0)
5452 warning ("enumeration values exceed range of largest integer");
5453 narrowest = long_long_integer_type_node;
5456 precision = TYPE_PRECISION (narrowest);
5458 else
5459 precision = TYPE_PRECISION (integer_type_node);
5461 if (precision == TYPE_PRECISION (integer_type_node))
5462 enum_value_type = c_common_type_for_size (precision, 0);
5463 else
5464 enum_value_type = enumtype;
5466 TYPE_MIN_VALUE (enumtype) = minnode;
5467 TYPE_MAX_VALUE (enumtype) = maxnode;
5468 TYPE_PRECISION (enumtype) = precision;
5469 TREE_UNSIGNED (enumtype) = unsign;
5470 TYPE_SIZE (enumtype) = 0;
5471 layout_type (enumtype);
5473 if (values != error_mark_node)
5475 /* Change the type of the enumerators to be the enum type. We
5476 need to do this irrespective of the size of the enum, for
5477 proper type checking. Replace the DECL_INITIALs of the
5478 enumerators, and the value slots of the list, with copies
5479 that have the enum type; they cannot be modified in place
5480 because they may be shared (e.g. integer_zero_node) Finally,
5481 change the purpose slots to point to the names of the decls. */
5482 for (pair = values; pair; pair = TREE_CHAIN (pair))
5484 tree enu = TREE_PURPOSE (pair);
5486 TREE_TYPE (enu) = enumtype;
5487 DECL_SIZE (enu) = TYPE_SIZE (enumtype);
5488 DECL_SIZE_UNIT (enu) = TYPE_SIZE_UNIT (enumtype);
5489 DECL_ALIGN (enu) = TYPE_ALIGN (enumtype);
5490 DECL_USER_ALIGN (enu) = TYPE_USER_ALIGN (enumtype);
5491 DECL_MODE (enu) = TYPE_MODE (enumtype);
5493 /* The ISO C Standard mandates enumerators to have type int,
5494 even though the underlying type of an enum type is
5495 unspecified. Here we convert any enumerators that fit in
5496 an int to type int, to avoid promotions to unsigned types
5497 when comparing integers with enumerators that fit in the
5498 int range. When -pedantic is given, build_enumerator()
5499 would have already taken care of those that don't fit. */
5500 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5501 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5502 else
5503 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5505 TREE_PURPOSE (pair) = DECL_NAME (enu);
5506 TREE_VALUE (pair) = DECL_INITIAL (enu);
5509 TYPE_VALUES (enumtype) = values;
5512 /* Fix up all variant types of this enum type. */
5513 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5515 if (tem == enumtype)
5516 continue;
5517 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5518 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5519 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5520 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5521 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5522 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5523 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5524 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5525 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5526 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5529 /* Finish debugging output for this type. */
5530 rest_of_type_compilation (enumtype, toplevel);
5532 return enumtype;
5535 /* Build and install a CONST_DECL for one value of the
5536 current enumeration type (one that was begun with start_enum).
5537 Return a tree-list containing the CONST_DECL and its value.
5538 Assignment of sequential values by default is handled here. */
5540 tree
5541 build_enumerator (name, value)
5542 tree name, value;
5544 tree decl, type;
5546 /* Validate and default VALUE. */
5548 /* Remove no-op casts from the value. */
5549 if (value)
5550 STRIP_TYPE_NOPS (value);
5552 if (value != 0)
5554 if (TREE_CODE (value) == INTEGER_CST)
5556 value = default_conversion (value);
5557 constant_expression_warning (value);
5559 else
5561 error ("enumerator value for `%s' not integer constant",
5562 IDENTIFIER_POINTER (name));
5563 value = 0;
5567 /* Default based on previous value. */
5568 /* It should no longer be possible to have NON_LVALUE_EXPR
5569 in the default. */
5570 if (value == 0)
5572 value = enum_next_value;
5573 if (enum_overflow)
5574 error ("overflow in enumeration values");
5577 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5579 pedwarn ("ISO C restricts enumerator values to range of `int'");
5580 value = convert (integer_type_node, value);
5583 /* Set basis for default for next value. */
5584 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5585 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5587 /* Now create a declaration for the enum value name. */
5589 type = TREE_TYPE (value);
5590 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5591 TYPE_PRECISION (integer_type_node)),
5592 (TYPE_PRECISION (type)
5593 >= TYPE_PRECISION (integer_type_node)
5594 && TREE_UNSIGNED (type)));
5596 decl = build_decl (CONST_DECL, name, type);
5597 DECL_INITIAL (decl) = convert (type, value);
5598 pushdecl (decl);
5600 return tree_cons (decl, value, NULL_TREE);
5604 /* Create the FUNCTION_DECL for a function definition.
5605 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5606 the declaration; they describe the function's name and the type it returns,
5607 but twisted together in a fashion that parallels the syntax of C.
5609 This function creates a binding context for the function body
5610 as well as setting up the FUNCTION_DECL in current_function_decl.
5612 Returns 1 on success. If the DECLARATOR is not suitable for a function
5613 (it defines a datum instead), we return 0, which tells
5614 yyparse to report a parse error. */
5617 start_function (declspecs, declarator, attributes)
5618 tree declarator, declspecs, attributes;
5620 tree decl1, old_decl;
5621 tree restype;
5622 int old_immediate_size_expand = immediate_size_expand;
5624 current_function_returns_value = 0; /* Assume, until we see it does. */
5625 current_function_returns_null = 0;
5626 current_function_returns_abnormally = 0;
5627 warn_about_return_type = 0;
5628 current_extern_inline = 0;
5629 named_labels = 0;
5630 shadowed_labels = 0;
5632 /* Don't expand any sizes in the return type of the function. */
5633 immediate_size_expand = 0;
5635 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5637 /* If the declarator is not suitable for a function definition,
5638 cause a syntax error. */
5639 if (decl1 == 0)
5641 immediate_size_expand = old_immediate_size_expand;
5642 return 0;
5645 decl_attributes (&decl1, attributes, 0);
5647 /* If #pragma weak was used, mark the decl weak now. */
5648 if (current_binding_level == global_binding_level)
5649 maybe_apply_pragma_weak (decl1);
5651 if (DECL_DECLARED_INLINE_P (decl1)
5652 && DECL_UNINLINABLE (decl1)
5653 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5654 warning_with_decl (decl1,
5655 "inline function `%s' given attribute noinline");
5657 announce_function (decl1);
5659 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5661 error ("return type is an incomplete type");
5662 /* Make it return void instead. */
5663 TREE_TYPE (decl1)
5664 = build_function_type (void_type_node,
5665 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5668 if (warn_about_return_type)
5669 pedwarn_c99 ("return type defaults to `int'");
5671 /* Save the parm names or decls from this function's declarator
5672 where store_parm_decls will find them. */
5673 current_function_parms = last_function_parms;
5674 current_function_parm_tags = last_function_parm_tags;
5676 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5677 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5678 DECL_INITIAL (decl1) = error_mark_node;
5680 /* If this definition isn't a prototype and we had a prototype declaration
5681 before, copy the arg type info from that prototype.
5682 But not if what we had before was a builtin function. */
5683 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5684 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5685 && !DECL_BUILT_IN (old_decl)
5686 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5687 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5688 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5690 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5691 current_function_prototype_file = DECL_SOURCE_FILE (old_decl);
5692 current_function_prototype_line = DECL_SOURCE_LINE (old_decl);
5695 /* If there is no explicit declaration, look for any out-of-scope implicit
5696 declarations. */
5697 if (old_decl == 0)
5698 old_decl = IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1));
5700 /* Optionally warn of old-fashioned def with no previous prototype. */
5701 if (warn_strict_prototypes
5702 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5703 && !(old_decl != 0
5704 && (TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0
5705 || (DECL_BUILT_IN (old_decl)
5706 && ! C_DECL_ANTICIPATED (old_decl)))))
5707 warning ("function declaration isn't a prototype");
5708 /* Optionally warn of any global def with no previous prototype. */
5709 else if (warn_missing_prototypes
5710 && TREE_PUBLIC (decl1)
5711 && !(old_decl != 0
5712 && (TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0
5713 || (DECL_BUILT_IN (old_decl)
5714 && ! C_DECL_ANTICIPATED (old_decl))))
5715 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5716 warning_with_decl (decl1, "no previous prototype for `%s'");
5717 /* Optionally warn of any def with no previous prototype
5718 if the function has already been used. */
5719 else if (warn_missing_prototypes
5720 && old_decl != 0 && TREE_USED (old_decl)
5721 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5722 warning_with_decl (decl1,
5723 "`%s' was used with no prototype before its definition");
5724 /* Optionally warn of any global def with no previous declaration. */
5725 else if (warn_missing_declarations
5726 && TREE_PUBLIC (decl1)
5727 && old_decl == 0
5728 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5729 warning_with_decl (decl1, "no previous declaration for `%s'");
5730 /* Optionally warn of any def with no previous declaration
5731 if the function has already been used. */
5732 else if (warn_missing_declarations
5733 && old_decl != 0 && TREE_USED (old_decl)
5734 && old_decl == IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)))
5735 warning_with_decl (decl1,
5736 "`%s' was used with no declaration before its definition");
5738 /* This is a definition, not a reference.
5739 So normally clear DECL_EXTERNAL.
5740 However, `extern inline' acts like a declaration
5741 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5742 DECL_EXTERNAL (decl1) = current_extern_inline;
5744 /* This function exists in static storage.
5745 (This does not mean `static' in the C sense!) */
5746 TREE_STATIC (decl1) = 1;
5748 /* A nested function is not global. */
5749 if (current_function_decl != 0)
5750 TREE_PUBLIC (decl1) = 0;
5752 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5753 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5755 tree args;
5756 int argct = 0;
5758 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5759 != integer_type_node)
5760 pedwarn_with_decl (decl1, "return type of `%s' is not `int'");
5762 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5763 args = TREE_CHAIN (args))
5765 tree type = args ? TREE_VALUE (args) : 0;
5767 if (type == void_type_node)
5768 break;
5770 ++argct;
5771 switch (argct)
5773 case 1:
5774 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5775 pedwarn_with_decl (decl1,
5776 "first argument of `%s' should be `int'");
5777 break;
5779 case 2:
5780 if (TREE_CODE (type) != POINTER_TYPE
5781 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5782 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5783 != char_type_node))
5784 pedwarn_with_decl (decl1,
5785 "second argument of `%s' should be `char **'");
5786 break;
5788 case 3:
5789 if (TREE_CODE (type) != POINTER_TYPE
5790 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5791 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5792 != char_type_node))
5793 pedwarn_with_decl (decl1,
5794 "third argument of `%s' should probably be `char **'");
5795 break;
5799 /* It is intentional that this message does not mention the third
5800 argument because it's only mentioned in an appendix of the
5801 standard. */
5802 if (argct > 0 && (argct < 2 || argct > 3))
5803 pedwarn_with_decl (decl1, "`%s' takes only zero or two arguments");
5805 if (! TREE_PUBLIC (decl1))
5806 pedwarn_with_decl (decl1, "`%s' is normally a non-static function");
5809 /* Record the decl so that the function name is defined.
5810 If we already have a decl for this name, and it is a FUNCTION_DECL,
5811 use the old decl. */
5813 current_function_decl = pushdecl (decl1);
5815 pushlevel (0);
5816 declare_parm_level (1);
5817 current_binding_level->subblocks_tag_transparent = 1;
5819 make_decl_rtl (current_function_decl, NULL);
5821 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5822 /* Promote the value to int before returning it. */
5823 if (c_promoting_integer_type_p (restype))
5825 /* It retains unsignedness if not really getting wider. */
5826 if (TREE_UNSIGNED (restype)
5827 && (TYPE_PRECISION (restype)
5828 == TYPE_PRECISION (integer_type_node)))
5829 restype = unsigned_type_node;
5830 else
5831 restype = integer_type_node;
5833 DECL_RESULT (current_function_decl)
5834 = build_decl (RESULT_DECL, NULL_TREE, restype);
5836 /* If this fcn was already referenced via a block-scope `extern' decl
5837 (or an implicit decl), propagate certain information about the usage. */
5838 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5839 TREE_ADDRESSABLE (current_function_decl) = 1;
5841 immediate_size_expand = old_immediate_size_expand;
5843 start_fname_decls ();
5845 return 1;
5848 /* Store the parameter declarations into the current function declaration.
5849 This is called after parsing the parameter declarations, before
5850 digesting the body of the function.
5852 For an old-style definition, modify the function's type
5853 to specify at least the number of arguments. */
5855 void
5856 store_parm_decls ()
5858 tree fndecl = current_function_decl;
5859 tree parm;
5861 /* This is either a chain of PARM_DECLs (if a prototype was used)
5862 or a list of IDENTIFIER_NODEs (for an old-fashioned C definition). */
5863 tree specparms = current_function_parms;
5865 /* This is a list of types declared among parms in a prototype. */
5866 tree parmtags = current_function_parm_tags;
5868 /* This is a chain of PARM_DECLs from old-style parm declarations. */
5869 tree parmdecls = getdecls ();
5871 /* This is a chain of any other decls that came in among the parm
5872 declarations. If a parm is declared with enum {foo, bar} x;
5873 then CONST_DECLs for foo and bar are put here. */
5874 tree nonparms = 0;
5876 /* The function containing FNDECL, if any. */
5877 tree context = decl_function_context (fndecl);
5879 /* Nonzero if this definition is written with a prototype. */
5880 int prototype = 0;
5882 int saved_warn_shadow = warn_shadow;
5884 /* Don't re-emit shadow warnings. */
5885 warn_shadow = 0;
5887 if (specparms != 0 && TREE_CODE (specparms) != TREE_LIST)
5889 /* This case is when the function was defined with an ANSI prototype.
5890 The parms already have decls, so we need not do anything here
5891 except record them as in effect
5892 and complain if any redundant old-style parm decls were written. */
5894 tree next;
5895 tree others = 0;
5897 prototype = 1;
5899 if (parmdecls != 0)
5901 tree decl, link;
5903 error_with_decl (fndecl,
5904 "parm types given both in parmlist and separately");
5905 /* Get rid of the erroneous decls; don't keep them on
5906 the list of parms, since they might not be PARM_DECLs. */
5907 for (decl = current_binding_level->names;
5908 decl; decl = TREE_CHAIN (decl))
5909 if (DECL_NAME (decl))
5910 IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) = 0;
5911 for (link = current_binding_level->shadowed;
5912 link; link = TREE_CHAIN (link))
5913 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
5914 current_binding_level->names = 0;
5915 current_binding_level->shadowed = 0;
5918 specparms = nreverse (specparms);
5919 for (parm = specparms; parm; parm = next)
5921 next = TREE_CHAIN (parm);
5922 if (TREE_CODE (parm) == PARM_DECL)
5924 if (DECL_NAME (parm) == 0)
5925 error_with_decl (parm, "parameter name omitted");
5926 else if (TREE_CODE (TREE_TYPE (parm)) != ERROR_MARK
5927 && VOID_TYPE_P (TREE_TYPE (parm)))
5929 error_with_decl (parm, "parameter `%s' declared void");
5930 /* Change the type to error_mark_node so this parameter
5931 will be ignored by assign_parms. */
5932 TREE_TYPE (parm) = error_mark_node;
5934 pushdecl (parm);
5936 else
5938 /* If we find an enum constant or a type tag,
5939 put it aside for the moment. */
5940 TREE_CHAIN (parm) = 0;
5941 others = chainon (others, parm);
5945 /* Get the decls in their original chain order
5946 and record in the function. */
5947 DECL_ARGUMENTS (fndecl) = getdecls ();
5949 #if 0
5950 /* If this function takes a variable number of arguments,
5951 add a phony parameter to the end of the parm list,
5952 to represent the position of the first unnamed argument. */
5953 if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (fndecl))))
5954 != void_type_node)
5956 tree dummy = build_decl (PARM_DECL, NULL_TREE, void_type_node);
5957 /* Let's hope the address of the unnamed parm
5958 won't depend on its type. */
5959 TREE_TYPE (dummy) = integer_type_node;
5960 DECL_ARG_TYPE (dummy) = integer_type_node;
5961 DECL_ARGUMENTS (fndecl) = chainon (DECL_ARGUMENTS (fndecl), dummy);
5963 #endif
5965 /* Now pushdecl the enum constants. */
5966 for (parm = others; parm; parm = next)
5968 next = TREE_CHAIN (parm);
5969 if (DECL_NAME (parm) == 0)
5971 else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
5973 else if (TREE_CODE (parm) != PARM_DECL)
5974 pushdecl (parm);
5977 storetags (chainon (parmtags, gettags ()));
5979 else
5981 /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
5982 each with a parm name as the TREE_VALUE.
5984 PARMDECLS is a chain of declarations for parameters.
5985 Warning! It can also contain CONST_DECLs which are not parameters
5986 but are names of enumerators of any enum types
5987 declared among the parameters.
5989 First match each formal parameter name with its declaration.
5990 Associate decls with the names and store the decls
5991 into the TREE_PURPOSE slots. */
5993 /* We use DECL_WEAK as a flag to show which parameters have been
5994 seen already since it is not used on PARM_DECL or CONST_DECL. */
5995 for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
5996 DECL_WEAK (parm) = 0;
5998 for (parm = specparms; parm; parm = TREE_CHAIN (parm))
6000 tree tail, found = NULL;
6002 if (TREE_VALUE (parm) == 0)
6004 error_with_decl (fndecl,
6005 "parameter name missing from parameter list");
6006 TREE_PURPOSE (parm) = 0;
6007 continue;
6010 /* See if any of the parmdecls specifies this parm by name.
6011 Ignore any enumerator decls. */
6012 for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
6013 if (DECL_NAME (tail) == TREE_VALUE (parm)
6014 && TREE_CODE (tail) == PARM_DECL)
6016 found = tail;
6017 break;
6020 /* If declaration already marked, we have a duplicate name.
6021 Complain, and don't use this decl twice. */
6022 if (found && DECL_WEAK (found))
6024 error_with_decl (found, "multiple parameters named `%s'");
6025 found = 0;
6028 /* If the declaration says "void", complain and ignore it. */
6029 if (found && VOID_TYPE_P (TREE_TYPE (found)))
6031 error_with_decl (found, "parameter `%s' declared void");
6032 TREE_TYPE (found) = integer_type_node;
6033 DECL_ARG_TYPE (found) = integer_type_node;
6034 layout_decl (found, 0);
6037 /* If no declaration found, default to int. */
6038 if (!found)
6040 found = build_decl (PARM_DECL, TREE_VALUE (parm),
6041 integer_type_node);
6042 DECL_ARG_TYPE (found) = TREE_TYPE (found);
6043 DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
6044 DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
6045 if (flag_isoc99)
6046 pedwarn_with_decl (found, "type of `%s' defaults to `int'");
6047 else if (extra_warnings)
6048 warning_with_decl (found, "type of `%s' defaults to `int'");
6049 pushdecl (found);
6052 TREE_PURPOSE (parm) = found;
6054 /* Mark this decl as "already found". */
6055 DECL_WEAK (found) = 1;
6058 /* Put anything which is on the parmdecls chain and which is
6059 not a PARM_DECL onto the list NONPARMS. (The types of
6060 non-parm things which might appear on the list include
6061 enumerators and NULL-named TYPE_DECL nodes.) Complain about
6062 any actual PARM_DECLs not matched with any names. */
6064 nonparms = 0;
6065 for (parm = parmdecls; parm;)
6067 tree next = TREE_CHAIN (parm);
6068 TREE_CHAIN (parm) = 0;
6070 if (TREE_CODE (parm) != PARM_DECL)
6071 nonparms = chainon (nonparms, parm);
6072 else
6074 /* Complain about args with incomplete types. */
6075 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
6077 error_with_decl (parm, "parameter `%s' has incomplete type");
6078 TREE_TYPE (parm) = error_mark_node;
6081 if (! DECL_WEAK (parm))
6083 error_with_decl (parm,
6084 "declaration for parameter `%s' but no such parameter");
6085 /* Pretend the parameter was not missing.
6086 This gets us to a standard state and minimizes
6087 further error messages. */
6088 specparms
6089 = chainon (specparms,
6090 tree_cons (parm, NULL_TREE, NULL_TREE));
6094 parm = next;
6097 /* Chain the declarations together in the order of the list of
6098 names. Store that chain in the function decl, replacing the
6099 list of names. */
6100 parm = specparms;
6101 DECL_ARGUMENTS (fndecl) = 0;
6103 tree last;
6104 for (last = 0; parm; parm = TREE_CHAIN (parm))
6105 if (TREE_PURPOSE (parm))
6107 if (last == 0)
6108 DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
6109 else
6110 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6111 last = TREE_PURPOSE (parm);
6112 TREE_CHAIN (last) = 0;
6116 /* If there was a previous prototype,
6117 set the DECL_ARG_TYPE of each argument according to
6118 the type previously specified, and report any mismatches. */
6120 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6122 tree type;
6123 for (parm = DECL_ARGUMENTS (fndecl),
6124 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6125 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6126 != void_type_node));
6127 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6129 if (parm == 0 || type == 0
6130 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6132 error ("number of arguments doesn't match prototype");
6133 error_with_file_and_line (current_function_prototype_file,
6134 current_function_prototype_line,
6135 "prototype declaration");
6136 break;
6138 /* Type for passing arg must be consistent with that
6139 declared for the arg. ISO C says we take the unqualified
6140 type for parameters declared with qualified type. */
6141 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6142 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6144 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6145 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6147 /* Adjust argument to match prototype. E.g. a previous
6148 `int foo(float);' prototype causes
6149 `int foo(x) float x; {...}' to be treated like
6150 `int foo(float x) {...}'. This is particularly
6151 useful for argument types like uid_t. */
6152 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6154 if (PROMOTE_PROTOTYPES
6155 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6156 && TYPE_PRECISION (TREE_TYPE (parm))
6157 < TYPE_PRECISION (integer_type_node))
6158 DECL_ARG_TYPE (parm) = integer_type_node;
6160 if (pedantic)
6162 pedwarn ("promoted argument `%s' doesn't match prototype",
6163 IDENTIFIER_POINTER (DECL_NAME (parm)));
6164 warning_with_file_and_line
6165 (current_function_prototype_file,
6166 current_function_prototype_line,
6167 "prototype declaration");
6170 else
6172 error ("argument `%s' doesn't match prototype",
6173 IDENTIFIER_POINTER (DECL_NAME (parm)));
6174 error_with_file_and_line (current_function_prototype_file,
6175 current_function_prototype_line,
6176 "prototype declaration");
6180 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6183 /* Otherwise, create a prototype that would match. */
6185 else
6187 tree actual = 0, last = 0, type;
6189 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6191 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6192 if (last)
6193 TREE_CHAIN (last) = type;
6194 else
6195 actual = type;
6196 last = type;
6198 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6199 if (last)
6200 TREE_CHAIN (last) = type;
6201 else
6202 actual = type;
6204 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6205 of the type of this function, but we need to avoid having this
6206 affect the types of other similarly-typed functions, so we must
6207 first force the generation of an identical (but separate) type
6208 node for the relevant function type. The new node we create
6209 will be a variant of the main variant of the original function
6210 type. */
6212 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6214 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6217 /* Now store the final chain of decls for the arguments
6218 as the decl-chain of the current lexical scope.
6219 Put the enumerators in as well, at the front so that
6220 DECL_ARGUMENTS is not modified. */
6222 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
6225 /* Make sure the binding level for the top of the function body
6226 gets a BLOCK if there are any in the function.
6227 Otherwise, the dbx output is wrong. */
6229 keep_next_if_subblocks = 1;
6231 /* ??? This might be an improvement,
6232 but needs to be thought about some more. */
6233 #if 0
6234 keep_next_level_flag = 1;
6235 #endif
6237 /* Write a record describing this function definition to the prototypes
6238 file (if requested). */
6240 gen_aux_info_record (fndecl, 1, 0, prototype);
6242 /* Initialize the RTL code for the function. */
6243 init_function_start (fndecl, input_filename, lineno);
6245 /* Begin the statement tree for this function. */
6246 begin_stmt_tree (&DECL_SAVED_TREE (current_function_decl));
6248 /* If this is a nested function, save away the sizes of any
6249 variable-size types so that we can expand them when generating
6250 RTL. */
6251 if (context)
6253 tree t;
6255 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6256 = nreverse (get_pending_sizes ());
6257 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6259 t = TREE_CHAIN (t))
6260 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6263 /* This function is being processed in whole-function mode. */
6264 cfun->x_whole_function_mode_p = 1;
6266 /* Even though we're inside a function body, we still don't want to
6267 call expand_expr to calculate the size of a variable-sized array.
6268 We haven't necessarily assigned RTL to all variables yet, so it's
6269 not safe to try to expand expressions involving them. */
6270 immediate_size_expand = 0;
6271 cfun->x_dont_save_pending_sizes_p = 1;
6273 warn_shadow = saved_warn_shadow;
6276 /* Finish up a function declaration and compile that function
6277 all the way to assembler language output. The free the storage
6278 for the function definition.
6280 This is called after parsing the body of the function definition.
6282 NESTED is nonzero if the function being finished is nested in another.
6283 CAN_DEFER_P is nonzero if the function may be deferred. */
6285 void
6286 finish_function (nested, can_defer_p)
6287 int nested;
6288 int can_defer_p;
6290 tree fndecl = current_function_decl;
6292 #if 0
6293 /* This caused &foo to be of type ptr-to-const-function which then
6294 got a warning when stored in a ptr-to-function variable. */
6295 TREE_READONLY (fndecl) = 1;
6296 #endif
6298 poplevel (1, 0, 1);
6299 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6301 /* Must mark the RESULT_DECL as being in this function. */
6303 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6305 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6307 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6308 != integer_type_node)
6310 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6311 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6312 if (! warn_main)
6313 pedwarn_with_decl (fndecl, "return type of `%s' is not `int'");
6315 else
6317 #ifdef DEFAULT_MAIN_RETURN
6318 /* Make it so that `main' always returns success by default. */
6319 DEFAULT_MAIN_RETURN;
6320 #else
6321 if (flag_isoc99)
6322 c_expand_return (integer_zero_node);
6323 #endif
6327 finish_fname_decls ();
6329 /* Tie off the statement tree for this function. */
6330 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6332 /* Complain if there's just no return statement. */
6333 if (warn_return_type
6334 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6335 && !current_function_returns_value && !current_function_returns_null
6336 /* Don't complain if we abort. */
6337 && !current_function_returns_abnormally
6338 /* Don't warn for main(). */
6339 && !MAIN_NAME_P (DECL_NAME (fndecl))
6340 /* Or if they didn't actually specify a return type. */
6341 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6342 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6343 inline function, as we might never be compiled separately. */
6344 && DECL_INLINE (fndecl))
6345 warning ("no return statement in function returning non-void");
6347 /* Clear out memory we no longer need. */
6348 free_after_parsing (cfun);
6349 /* Since we never call rest_of_compilation, we never clear
6350 CFUN. Do so explicitly. */
6351 free_after_compilation (cfun);
6352 cfun = NULL;
6354 if (! nested)
6356 /* Generate RTL for the body of this function. */
6357 c_expand_body (fndecl, nested, can_defer_p);
6359 /* Let the error reporting routines know that we're outside a
6360 function. For a nested function, this value is used in
6361 c_pop_function_context and then reset via pop_function_context. */
6362 current_function_decl = NULL;
6366 /* Generate the RTL for a deferred function FNDECL. */
6368 void
6369 c_expand_deferred_function (fndecl)
6370 tree fndecl;
6372 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6373 function was deferred, e.g. in duplicate_decls. */
6374 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6376 c_expand_body (fndecl, 0, 0);
6377 current_function_decl = NULL;
6381 /* Generate the RTL for the body of FNDECL. If NESTED_P is non-zero,
6382 then we are already in the process of generating RTL for another
6383 function. If can_defer_p is zero, we won't attempt to defer the
6384 generation of RTL. */
6386 static void
6387 c_expand_body (fndecl, nested_p, can_defer_p)
6388 tree fndecl;
6389 int nested_p, can_defer_p;
6391 int uninlinable = 1;
6393 /* There's no reason to do any of the work here if we're only doing
6394 semantic analysis; this code just generates RTL. */
6395 if (flag_syntax_only)
6396 return;
6398 if (flag_inline_trees)
6400 /* First, cache whether the current function is inlinable. Some
6401 predicates depend on cfun and current_function_decl to
6402 function completely. */
6403 timevar_push (TV_INTEGRATION);
6404 uninlinable = ! tree_inlinable_function_p (fndecl);
6406 if (! uninlinable && can_defer_p
6407 /* Save function tree for inlining. Should return 0 if the
6408 language does not support function deferring or the
6409 function could not be deferred. */
6410 && defer_fn (fndecl))
6412 /* Let the back-end know that this function exists. */
6413 (*debug_hooks->deferred_inline_function) (fndecl);
6414 timevar_pop (TV_INTEGRATION);
6415 return;
6418 /* Then, inline any functions called in it. */
6419 optimize_inline_calls (fndecl);
6420 timevar_pop (TV_INTEGRATION);
6423 timevar_push (TV_EXPAND);
6425 if (nested_p)
6427 /* Make sure that we will evaluate variable-sized types involved
6428 in our function's type. */
6429 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6430 /* Squirrel away our current state. */
6431 push_function_context ();
6434 /* Initialize the RTL code for the function. */
6435 current_function_decl = fndecl;
6436 input_filename = DECL_SOURCE_FILE (fndecl);
6437 init_function_start (fndecl, input_filename, DECL_SOURCE_LINE (fndecl));
6439 /* This function is being processed in whole-function mode. */
6440 cfun->x_whole_function_mode_p = 1;
6442 /* Even though we're inside a function body, we still don't want to
6443 call expand_expr to calculate the size of a variable-sized array.
6444 We haven't necessarily assigned RTL to all variables yet, so it's
6445 not safe to try to expand expressions involving them. */
6446 immediate_size_expand = 0;
6447 cfun->x_dont_save_pending_sizes_p = 1;
6449 /* Set up parameters and prepare for return, for the function. */
6450 expand_function_start (fndecl, 0);
6452 /* If this function is `main', emit a call to `__main'
6453 to run global initializers, etc. */
6454 if (DECL_NAME (fndecl)
6455 && MAIN_NAME_P (DECL_NAME (fndecl))
6456 && DECL_CONTEXT (fndecl) == NULL_TREE)
6457 expand_main_function ();
6459 /* Generate the RTL for this function. */
6460 expand_stmt (DECL_SAVED_TREE (fndecl));
6461 if (uninlinable)
6463 /* Allow the body of the function to be garbage collected. */
6464 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6467 /* We hard-wired immediate_size_expand to zero above.
6468 expand_function_end will decrement this variable. So, we set the
6469 variable to one here, so that after the decrement it will remain
6470 zero. */
6471 immediate_size_expand = 1;
6473 /* Allow language dialects to perform special processing. */
6474 if (lang_expand_function_end)
6475 (*lang_expand_function_end) ();
6477 /* Generate rtl for function exit. */
6478 expand_function_end (input_filename, lineno, 0);
6480 /* If this is a nested function, protect the local variables in the stack
6481 above us from being collected while we're compiling this function. */
6482 if (nested_p)
6483 ggc_push_context ();
6485 /* Run the optimizers and output the assembler code for this function. */
6486 rest_of_compilation (fndecl);
6488 /* Undo the GC context switch. */
6489 if (nested_p)
6490 ggc_pop_context ();
6492 /* With just -W, complain only if function returns both with
6493 and without a value. */
6494 if (extra_warnings
6495 && current_function_returns_value
6496 && current_function_returns_null)
6497 warning ("this function may return with or without a value");
6499 /* If requested, warn about function definitions where the function will
6500 return a value (usually of some struct or union type) which itself will
6501 take up a lot of stack space. */
6503 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6505 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6507 if (ret_type && TYPE_SIZE_UNIT (ret_type)
6508 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
6509 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
6510 larger_than_size))
6512 unsigned int size_as_int
6513 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
6515 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
6516 warning_with_decl (fndecl,
6517 "size of return value of `%s' is %u bytes",
6518 size_as_int);
6519 else
6520 warning_with_decl (fndecl,
6521 "size of return value of `%s' is larger than %d bytes",
6522 larger_than_size);
6526 if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested_p
6527 && ! flag_inline_trees)
6529 /* Stop pointing to the local nodes about to be freed.
6530 But DECL_INITIAL must remain nonzero so we know this
6531 was an actual function definition.
6532 For a nested function, this is done in c_pop_function_context.
6533 If rest_of_compilation set this to 0, leave it 0. */
6534 if (DECL_INITIAL (fndecl) != 0)
6535 DECL_INITIAL (fndecl) = error_mark_node;
6537 DECL_ARGUMENTS (fndecl) = 0;
6540 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6542 if (targetm.have_ctors_dtors)
6543 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6544 DEFAULT_INIT_PRIORITY);
6545 else
6546 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6549 if (DECL_STATIC_DESTRUCTOR (fndecl))
6551 if (targetm.have_ctors_dtors)
6552 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6553 DEFAULT_INIT_PRIORITY);
6554 else
6555 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6558 if (nested_p)
6559 /* Return to the enclosing function. */
6560 pop_function_context ();
6561 timevar_pop (TV_EXPAND);
6564 /* Check the declarations given in a for-loop for satisfying the C99
6565 constraints. */
6566 void
6567 check_for_loop_decls ()
6569 tree t;
6571 if (!flag_isoc99)
6573 /* If we get here, declarations have been used in a for loop without
6574 the C99 for loop scope. This doesn't make much sense, so don't
6575 allow it. */
6576 error ("`for' loop initial declaration used outside C99 mode");
6577 return;
6579 /* C99 subclause 6.8.5 paragraph 3:
6581 [#3] The declaration part of a for statement shall only
6582 declare identifiers for objects having storage class auto or
6583 register.
6585 It isn't clear whether, in this sentence, "identifiers" binds to
6586 "shall only declare" or to "objects" - that is, whether all identifiers
6587 declared must be identifiers for objects, or whether the restriction
6588 only applies to those that are. (A question on this in comp.std.c
6589 in November 2000 received no answer.) We implement the strictest
6590 interpretation, to avoid creating an extension which later causes
6591 problems. */
6593 for (t = gettags (); t; t = TREE_CHAIN (t))
6595 if (TREE_PURPOSE (t) != 0)
6597 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6599 if (code == RECORD_TYPE)
6600 error ("`struct %s' declared in `for' loop initial declaration",
6601 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6602 else if (code == UNION_TYPE)
6603 error ("`union %s' declared in `for' loop initial declaration",
6604 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6605 else
6606 error ("`enum %s' declared in `for' loop initial declaration",
6607 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6611 for (t = getdecls (); t; t = TREE_CHAIN (t))
6613 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6614 error_with_decl (t, "declaration of non-variable `%s' in `for' loop initial declaration");
6615 else if (TREE_STATIC (t))
6616 error_with_decl (t, "declaration of static variable `%s' in `for' loop initial declaration");
6617 else if (DECL_EXTERNAL (t))
6618 error_with_decl (t, "declaration of `extern' variable `%s' in `for' loop initial declaration");
6622 /* Save and restore the variables in this file and elsewhere
6623 that keep track of the progress of compilation of the current function.
6624 Used for nested functions. */
6626 struct language_function GTY(())
6628 struct c_language_function base;
6629 tree named_labels;
6630 tree shadowed_labels;
6631 int returns_value;
6632 int returns_null;
6633 int returns_abnormally;
6634 int warn_about_return_type;
6635 int extern_inline;
6636 struct binding_level *binding_level;
6639 /* Save and reinitialize the variables
6640 used during compilation of a C function. */
6642 void
6643 c_push_function_context (f)
6644 struct function *f;
6646 struct language_function *p;
6647 p = ((struct language_function *)
6648 ggc_alloc (sizeof (struct language_function)));
6649 f->language = p;
6651 p->base.x_stmt_tree = c_stmt_tree;
6652 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6653 p->named_labels = named_labels;
6654 p->shadowed_labels = shadowed_labels;
6655 p->returns_value = current_function_returns_value;
6656 p->returns_null = current_function_returns_null;
6657 p->returns_abnormally = current_function_returns_abnormally;
6658 p->warn_about_return_type = warn_about_return_type;
6659 p->extern_inline = current_extern_inline;
6660 p->binding_level = current_binding_level;
6663 /* Restore the variables used during compilation of a C function. */
6665 void
6666 c_pop_function_context (f)
6667 struct function *f;
6669 struct language_function *p = f->language;
6670 tree link;
6672 /* Bring back all the labels that were shadowed. */
6673 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
6674 if (DECL_NAME (TREE_VALUE (link)) != 0)
6675 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
6676 = TREE_VALUE (link);
6678 if (DECL_SAVED_INSNS (current_function_decl) == 0
6679 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6681 /* Stop pointing to the local nodes about to be freed. */
6682 /* But DECL_INITIAL must remain nonzero so we know this
6683 was an actual function definition. */
6684 DECL_INITIAL (current_function_decl) = error_mark_node;
6685 DECL_ARGUMENTS (current_function_decl) = 0;
6688 c_stmt_tree = p->base.x_stmt_tree;
6689 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6690 named_labels = p->named_labels;
6691 shadowed_labels = p->shadowed_labels;
6692 current_function_returns_value = p->returns_value;
6693 current_function_returns_null = p->returns_null;
6694 current_function_returns_abnormally = p->returns_abnormally;
6695 warn_about_return_type = p->warn_about_return_type;
6696 current_extern_inline = p->extern_inline;
6697 current_binding_level = p->binding_level;
6699 f->language = NULL;
6702 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6704 void
6705 c_dup_lang_specific_decl (decl)
6706 tree decl;
6708 struct lang_decl *ld;
6710 if (!DECL_LANG_SPECIFIC (decl))
6711 return;
6713 ld = (struct lang_decl *) ggc_alloc (sizeof (struct lang_decl));
6714 memcpy ((char *) ld, (char *) DECL_LANG_SPECIFIC (decl),
6715 sizeof (struct lang_decl));
6716 DECL_LANG_SPECIFIC (decl) = ld;
6719 /* The functions below are required for functionality of doing
6720 function at once processing in the C front end. Currently these
6721 functions are not called from anywhere in the C front end, but as
6722 these changes continue, that will change. */
6724 /* Returns non-zero if the current statement is a full expression,
6725 i.e. temporaries created during that statement should be destroyed
6726 at the end of the statement. */
6729 stmts_are_full_exprs_p ()
6731 return 0;
6734 /* Returns the stmt_tree (if any) to which statements are currently
6735 being added. If there is no active statement-tree, NULL is
6736 returned. */
6738 stmt_tree
6739 current_stmt_tree ()
6741 return &c_stmt_tree;
6744 /* Returns the stack of SCOPE_STMTs for the current function. */
6746 tree *
6747 current_scope_stmt_stack ()
6749 return &c_scope_stmt_stack;
6752 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6753 C. */
6756 anon_aggr_type_p (node)
6757 tree node ATTRIBUTE_UNUSED;
6759 return 0;
6762 /* Dummy function in place of callback used by C++. */
6764 void
6765 extract_interface_info ()
6769 /* Return a new COMPOUND_STMT, after adding it to the current
6770 statement tree. */
6772 tree
6773 c_begin_compound_stmt ()
6775 tree stmt;
6777 /* Create the COMPOUND_STMT. */
6778 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6780 return stmt;
6783 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6784 common code. */
6786 void
6787 c_expand_decl_stmt (t)
6788 tree t;
6790 tree decl = DECL_STMT_DECL (t);
6792 /* Expand nested functions. */
6793 if (TREE_CODE (decl) == FUNCTION_DECL
6794 && DECL_CONTEXT (decl) == current_function_decl
6795 && DECL_SAVED_TREE (decl))
6796 c_expand_body (decl, /*nested_p=*/1, /*can_defer_p=*/0);
6799 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
6800 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
6802 tree
6803 identifier_global_value (t)
6804 tree t;
6806 return IDENTIFIER_GLOBAL_VALUE (t);
6809 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6810 otherwise the name is found in ridpointers from RID_INDEX. */
6812 void
6813 record_builtin_type (rid_index, name, type)
6814 enum rid rid_index;
6815 const char *name;
6816 tree type;
6818 tree id;
6819 if (name == 0)
6820 id = ridpointers[(int) rid_index];
6821 else
6822 id = get_identifier (name);
6823 pushdecl (build_decl (TYPE_DECL, id, type));
6826 /* Build the void_list_node (void_type_node having been created). */
6827 tree
6828 build_void_list_node ()
6830 tree t = build_tree_list (NULL_TREE, void_type_node);
6831 return t;
6834 /* Return something to represent absolute declarators containing a *.
6835 TARGET is the absolute declarator that the * contains.
6836 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6837 to apply to the pointer type, represented as identifiers, possible mixed
6838 with attributes.
6840 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6841 if attributes are present) and whose type is the modifier list. */
6843 tree
6844 make_pointer_declarator (type_quals_attrs, target)
6845 tree type_quals_attrs, target;
6847 tree quals, attrs;
6848 tree itarget = target;
6849 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6850 if (attrs != NULL_TREE)
6851 itarget = tree_cons (attrs, target, NULL_TREE);
6852 return build1 (INDIRECT_REF, quals, itarget);
6855 #include "gt-c-decl.h"